/// 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), Method(0),
- AtLoc(at), RParenLoc(rp){}
+ : Expr(ObjCSelectorExprClass, T, false, false), SelName(selInfo), 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; }
const ObjCMethodDecl *Method);
virtual llvm::Value *GetClass(CGBuilderTy &Builder,
const ObjCInterfaceDecl *OID);
- 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::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
+ virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
+ *Method);
virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
const ObjCContainerDecl *CD);
return Builder.CreateCall(ClassLookupFn, ClassName);
}
-llvm::Constant *CGObjCGNU::GetConstantSelector(Selector Sel) {
+llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
if (US == 0)
- US = new llvm::GlobalAlias(SelectorTy,
+ US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
llvm::GlobalValue::PrivateLinkage,
".objc_untyped_selector_alias"+Sel.getAsString(),
NULL, &TheModule);
- return US;
+ return Builder.CreateLoad(US);
}
-llvm::Constant *CGObjCGNU::GetConstantTypedSelector(const ObjCMethodDecl
+llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
*Method) {
std::string SelName = Method->getSelector().getAsString();
// If it's already cached, return it.
if (TypedSelectors[Selector]) {
- return TypedSelectors[Selector];
+ return Builder.CreateLoad(TypedSelectors[Selector]);
}
// If it isn't, cache it.
llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
- SelectorTy,
+ llvm::PointerType::getUnqual(SelectorTy),
llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName,
NULL, &TheModule);
TypedSelectors[Selector] = Sel;
- return Sel;
+ return Builder.CreateLoad(Sel);
}
llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
// 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 =
- llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2);
+ llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
+ true, llvm::GlobalValue::InternalLinkage,
+ llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
+ ".objc_sel_ptr");
// If selectors are defined as an opaque type, cast the pointer to this
// type.
if (isSelOpaque) {
- SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,SelectorTy);
+ SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
+ llvm::PointerType::getUnqual(SelectorTy));
}
- selectors.push_back(
- std::pair<llvm::GlobalAlias*,llvm::Value*>((*iter).second, SelPtr));
+ (*iter).second->setAliasee(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 =
- llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2);
+ llvm::Constant *SelPtr = new llvm::GlobalVariable
+ (TheModule, SelStructPtrTy,
+ true, llvm::GlobalValue::InternalLinkage,
+ llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
+ ".objc_sel_ptr");
// If selectors are defined as an opaque type, cast the pointer to this
// type.
if (isSelOpaque) {
- SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
+ SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
+ llvm::PointerType::getUnqual(SelectorTy));
}
- 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();
+ (*iter).second->setAliasee(SelPtr);
}
// Number of classes defined.
Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
QualType Ty = Context.getObjCSelType();
- 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;
+ return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
}
Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,