summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Chisnall <csdavec@swan.ac.uk>2010-02-03 02:09:30 +0000
committerDavid Chisnall <csdavec@swan.ac.uk>2010-02-03 02:09:30 +0000
commit92b762e256d62ae85daedcf0af04fbd2dcbecdeb (patch)
treef4fe6ffe767541411f8c12859c5c82f03df89919
parentd85be0cc60b067bc8ee594c63ddf1318cfbd8f3d (diff)
downloadbcm5719-llvm-92b762e256d62ae85daedcf0af04fbd2dcbecdeb.tar.gz
bcm5719-llvm-92b762e256d62ae85daedcf0af04fbd2dcbecdeb.zip
Numerous changes to selector handling:
- Don't use GlobalAliases with non-0 GEPs (GNU runtime) - this was unsupported and LLVM will be generating errors if you do it soon. This also simplifies the code generated by the GNU runtime a bit. - Make GetSelector() return a constant (GNU runtime), not a load of a store of a constant. - Recognise @selector() expressions as valid static initialisers (as GCC does). - Add methods to GCObjCRuntime to emit selectors as constants (needed for using @selector() expressions as constants. These need implementing for the Mac runtimes - I couldn't figure out how to do this, they seem to require a load. - Store an ObjCMethodDecl in an ObjCSelectorExpr so that we can get at the type information for the selector. This is needed for generating typed selectors from @selector() expressions (as GCC does). Ideally, this information should be stored in the Selector, but that would be an invasive change. We should eventually add checks for common uses of @selector() expressions. Possibly adding an attribute that can be applied to method args providing the types of a selector so, for example, you'd do something like this: - (id)performSelector: __attribute__((selector_types(id, SEL, id)))(SEL) withObject: (id)object; Then, any @selector() expressions passed to the method will be check to ensure that it conforms to this signature. We do this at run time on the GNU runtime already, but it would be nice to do it at compile time on all runtimes. - Made @selector() expressions emit type info if available and the runtime supports it. Someone more familiar with the Mac runtime needs to implement the GetConstantSelector() function in CGObjCMac. This currently just assert()s. llvm-svn: 95189
-rw-r--r--clang/include/clang/AST/ExprObjC.h8
-rw-r--r--clang/lib/AST/Expr.cpp1
-rw-r--r--clang/lib/CodeGen/CGExprConstant.cpp8
-rw-r--r--clang/lib/CodeGen/CGObjC.cpp6
-rw-r--r--clang/lib/CodeGen/CGObjCGNU.cpp61
-rw-r--r--clang/lib/CodeGen/CGObjCMac.cpp8
-rw-r--r--clang/lib/CodeGen/CGObjCRuntime.h6
-rw-r--r--clang/lib/Sema/SemaExprObjC.cpp15
8 files changed, 84 insertions, 29 deletions
diff --git a/clang/include/clang/AST/ExprObjC.h b/clang/include/clang/AST/ExprObjC.h
index 0b0cd64ad72..3a625273ad1 100644
--- a/clang/include/clang/AST/ExprObjC.h
+++ b/clang/include/clang/AST/ExprObjC.h
@@ -96,18 +96,22 @@ public:
/// ObjCSelectorExpr used for @selector in Objective-C.
class ObjCSelectorExpr : public Expr {
Selector SelName;
+ ObjCMethodDecl *Method;
SourceLocation AtLoc, RParenLoc;
public:
ObjCSelectorExpr(QualType T, Selector selInfo,
SourceLocation at, SourceLocation rp)
- : Expr(ObjCSelectorExprClass, T, false, false), SelName(selInfo), AtLoc(at),
- RParenLoc(rp){}
+ : Expr(ObjCSelectorExprClass, T, false, false), SelName(selInfo), Method(0),
+ AtLoc(at), RParenLoc(rp){}
explicit ObjCSelectorExpr(EmptyShell Empty)
: Expr(ObjCSelectorExprClass, Empty) {}
Selector getSelector() const { return SelName; }
void setSelector(Selector S) { SelName = S; }
+ ObjCMethodDecl *getMethodDecl() const { return Method; }
+ void setMethodDecl(ObjCMethodDecl *M) { Method = M; }
+
SourceLocation getAtLoc() const { return AtLoc; }
SourceLocation getRParenLoc() const { return RParenLoc; }
void setAtLoc(SourceLocation L) { AtLoc = L; }
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 50ddc9156a9..4811a6954a7 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1454,6 +1454,7 @@ bool Expr::isConstantInitializer(ASTContext &Ctx) const {
case StringLiteralClass:
case ObjCStringLiteralClass:
case ObjCEncodeExprClass:
+ case ObjCSelectorExprClass:
return true;
case CompoundLiteralExprClass: {
// This handles gcc's extension that allows global initializers like
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index 81209da6c6f..ca775bf0509 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -701,6 +701,14 @@ public:
CGM.GetStringForStringLiteral(E), false);
}
+ llvm::Constant *VisitObjCSelectorExpr(const ObjCSelectorExpr *E) {
+ ObjCMethodDecl *OMD = E->getMethodDecl();
+ if (OMD)
+ return CGM.getObjCRuntime().GetConstantTypedSelector(OMD);
+ else
+ return CGM.getObjCRuntime().GetConstantSelector(E->getSelector());
+ }
+
llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
// This must be an @encode initializing an array in a static initializer.
// Don't emit it as the address of the string, emit the string data itself
diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp
index 896d2207ea4..e4421670ff5 100644
--- a/clang/lib/CodeGen/CGObjC.cpp
+++ b/clang/lib/CodeGen/CGObjC.cpp
@@ -38,7 +38,11 @@ llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
// Note that this implementation allows for non-constant strings to be passed
// as arguments to @selector(). Currently, the only thing preventing this
// behaviour is the type checking in the front end.
- return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
+ ObjCMethodDecl *OMD = E->getMethodDecl();
+ if (OMD)
+ return CGM.getObjCRuntime().GetSelector(Builder, OMD);
+ else
+ return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
}
llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 3a0ac994d8c..d69477852a7 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -146,9 +146,17 @@ public:
const ObjCMethodDecl *Method);
virtual llvm::Value *GetClass(CGBuilderTy &Builder,
const ObjCInterfaceDecl *OID);
- virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
- virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
- *Method);
+ virtual llvm::Constant *GetConstantSelector(Selector Sel);
+ virtual llvm::Constant *GetConstantTypedSelector(
+ const ObjCMethodDecl *Method);
+ llvm::Value *GetSelector(CGBuilderTy &Builder,
+ Selector Sel) {
+ return cast<llvm::Constant>((GetConstantSelector(Sel)));
+ }
+ llvm::Value *GetSelector(CGBuilderTy &Builder,
+ const ObjCMethodDecl *Method) {
+ return cast<llvm::Constant>(GetConstantTypedSelector(Method));
+ }
virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
const ObjCContainerDecl *CD);
@@ -287,18 +295,18 @@ llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
return Builder.CreateCall(ClassLookupFn, ClassName);
}
-llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
+llvm::Constant *CGObjCGNU::GetConstantSelector(Selector Sel) {
llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
if (US == 0)
- US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
+ US = new llvm::GlobalAlias(SelectorTy,
llvm::GlobalValue::PrivateLinkage,
".objc_untyped_selector_alias"+Sel.getAsString(),
NULL, &TheModule);
- return Builder.CreateLoad(US);
+ return US;
}
-llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
+llvm::Constant *CGObjCGNU::GetConstantTypedSelector(const ObjCMethodDecl
*Method) {
std::string SelName = Method->getSelector().getAsString();
@@ -310,17 +318,17 @@ llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
// If it's already cached, return it.
if (TypedSelectors[Selector]) {
- return Builder.CreateLoad(TypedSelectors[Selector]);
+ return TypedSelectors[Selector];
}
// If it isn't, cache it.
llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
- llvm::PointerType::getUnqual(SelectorTy),
+ SelectorTy,
llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName,
NULL, &TheModule);
TypedSelectors[Selector] = Sel;
- return Builder.CreateLoad(Sel);
+ return Sel;
}
llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
@@ -1461,40 +1469,43 @@ llvm::Function *CGObjCGNU::ModuleInitFunction() {
// Now that all of the static selectors exist, create pointers to them.
int index = 0;
+ llvm::SmallVector<std::pair<llvm::GlobalAlias*,llvm::Value*>, 16> selectors;
for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
iter != iterEnd; ++iter) {
llvm::Constant *Idxs[] = {Zeros[0],
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
- llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
- true, llvm::GlobalValue::InternalLinkage,
- llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
- ".objc_sel_ptr");
+ llvm::Constant *SelPtr =
+ llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2);
// If selectors are defined as an opaque type, cast the pointer to this
// type.
if (isSelOpaque) {
- SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
- llvm::PointerType::getUnqual(SelectorTy));
+ SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,SelectorTy);
}
- (*iter).second->setAliasee(SelPtr);
+ selectors.push_back(
+ std::pair<llvm::GlobalAlias*,llvm::Value*>((*iter).second, SelPtr));
}
for (llvm::StringMap<llvm::GlobalAlias*>::iterator
iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
iter != iterEnd; iter++) {
llvm::Constant *Idxs[] = {Zeros[0],
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
- llvm::Constant *SelPtr = new llvm::GlobalVariable
- (TheModule, SelStructPtrTy,
- true, llvm::GlobalValue::InternalLinkage,
- llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
- ".objc_sel_ptr");
+ llvm::Constant *SelPtr =
+ llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2);
// If selectors are defined as an opaque type, cast the pointer to this
// type.
if (isSelOpaque) {
- SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
- llvm::PointerType::getUnqual(SelectorTy));
+ SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
}
- (*iter).second->setAliasee(SelPtr);
+ selectors.push_back(
+ std::pair<llvm::GlobalAlias*,llvm::Value*>((*iter).second, SelPtr));
+ }
+ for (llvm::SmallVectorImpl<std::pair<
+ llvm::GlobalAlias*,llvm::Value*> >::iterator
+ iter=selectors.begin(), iterEnd =selectors.end();
+ iter != iterEnd; ++iter) {
+ iter->first->replaceAllUsesWith(iter->second);
+ iter->first->eraseFromParent();
}
// Number of classes defined.
Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp
index 0dcbe829e23..361afbc58ee 100644
--- a/clang/lib/CodeGen/CGObjCMac.cpp
+++ b/clang/lib/CodeGen/CGObjCMac.cpp
@@ -953,6 +953,14 @@ public:
CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
CGM(cgm), VMContext(cgm.getLLVMContext()) { }
+ virtual llvm::Constant *GetConstantSelector(Selector Sel) {
+ assert(0 && "Constant Selectors are not yet supported on the Mac runtimes");
+ return 0;
+ }
+ virtual llvm::Constant *GetConstantTypedSelector(
+ const ObjCMethodDecl *Method) {
+ return GetConstantSelector(Method->getSelector());
+ }
virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
diff --git a/clang/lib/CodeGen/CGObjCRuntime.h b/clang/lib/CodeGen/CGObjCRuntime.h
index ff5d40bfbc8..2c27c140314 100644
--- a/clang/lib/CodeGen/CGObjCRuntime.h
+++ b/clang/lib/CodeGen/CGObjCRuntime.h
@@ -95,6 +95,12 @@ public:
/// this compilation unit with the runtime library.
virtual llvm::Function *ModuleInitFunction() = 0;
+ virtual llvm::Constant *GetConstantSelector(Selector Sel) = 0;
+
+ /// Get a typed selector.
+ virtual llvm::Constant *GetConstantTypedSelector(
+ const ObjCMethodDecl *Method) = 0;
+
/// Get a selector for the specified name and type values. The
/// return value should have the LLVM type for pointer-to
/// ASTContext::getObjCSelType().
diff --git a/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp
index 85956c3e7e0..8ce782b961e 100644
--- a/clang/lib/Sema/SemaExprObjC.cpp
+++ b/clang/lib/Sema/SemaExprObjC.cpp
@@ -140,7 +140,20 @@ Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
QualType Ty = Context.getObjCSelType();
- return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
+ ObjCSelectorExpr *E =
+ new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
+ // Make sure that we have seen this selector. There are lots of checks we
+ // should be doing on this selector. For example, when this is passed as the
+ // second argument to objc_msgSend() on the Mac runtime, or as the selector
+ // argument to the -performSelector:. We can do these checks at run time
+ // with the GNU runtimes, but the Apple runtimes let you sneak stack
+ // corruption in easily by passing the wrong selector to these functions if
+ // there is no static checking.
+ //
+ // Only log a warning on the GNU runtime.
+ E->setMethodDecl(LookupInstanceMethodInGlobalPool(Sel,
+ SourceRange(LParenLoc, LParenLoc), !LangOpts.NeXTRuntime));
+ return E;
}
Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
OpenPOWER on IntegriCloud