Add SelectorInfo (similar in spirit to IdentifierInfo). The key difference is SelectorInfo is not string-oriented, it is a unique aggregate of IdentifierInfo's (using a folding set). SelectorInfo also has a richer API that simplifies the parser/action interface. 3 noteworthy benefits:
#1: It is cleaner. I never "liked" storing keyword selectors (i.e. foo:bar:baz) in the IdentifierTable.
#2: It is more space efficient. Since Cocoa keyword selectors can be quite long, this technique is space saving. For Cocoa.h, pulling the keyword selectors out saves ~180k. The cost of the SelectorInfo data is ~100k. Saves ~80k, or 43%.
#3: It results in many API simplifications. Here are some highlights:
- Removed 3 actions (ActOnKeywordMessage, ActOnUnaryMessage, & one flavor of ObjcBuildMethodDeclaration that was specific to unary messages).
- Removed 3 funky structs from DeclSpec.h (ObjcKeywordMessage, ObjcKeywordDecl, and ObjcKeywordInfo).
- Removed 2 ivars and 2 constructors from ObjCMessageExpr (fyi, this space savings has not been measured).
I am happy with the way it turned out (though it took a bit more hacking than I expected). Given the central role of selectors in ObjC, making sure this is "right" will pay dividends later.
Thanks to Chris for talking this through with me and suggesting this approach.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42395 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp
index 15bf4fd..67aa04d 100644
--- a/Sema/SemaExpr.cpp
+++ b/Sema/SemaExpr.cpp
@@ -1871,76 +1871,31 @@
return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
}
-static IdentifierInfo &DeriveSelector(ObjcKeywordMessage *Keywords,
- unsigned NumKeywords,
- ASTContext &Context) {
- // Derive the selector name from the keyword declarations.
- int len=0;
- for (unsigned int i = 0; i < NumKeywords; i++) {
- if (Keywords[i].SelectorName)
- len += strlen(Keywords[i].SelectorName->getName());
- len++;
- }
- llvm::SmallString<128> methodName;
- methodName[0] = '\0';
- for (unsigned int i = 0; i < NumKeywords; i++) {
- if (Keywords[i].SelectorName)
- methodName += Keywords[i].SelectorName->getName();
- methodName += ":";
- }
- methodName[len] = '\0';
- return Context.Idents.get(&methodName[0], &methodName[0]+len);
-}
-
-// This actions handles keyword message to classes.
-Sema::ExprResult Sema::ActOnKeywordMessage(
- IdentifierInfo *receivingClassName,
- ObjcKeywordMessage *Keywords, unsigned NumKeywords,
- SourceLocation lbrac, SourceLocation rbrac)
+// ActOnClassMessage - used for both unary and keyword messages.
+// ArgExprs is optional - if it is present, the number of expressions
+// is obtained from Sel.getNumArgs().
+Sema::ExprResult Sema::ActOnClassMessage(
+ IdentifierInfo *receivingClassName, SelectorInfo *Sel,
+ SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
{
- IdentifierInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
assert(receivingClassName && "missing receiver class name");
- return new ObjCMessageExpr(receivingClassName, SelName, Keywords, NumKeywords,
- Context.IntTy/*FIXME*/, lbrac, rbrac);
+ Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
+ return new ObjCMessageExpr(receivingClassName, Sel,
+ Context.IntTy/*FIXME*/, lbrac, rbrac, ArgExprs);
}
-// This action handles keyword messages to instances.
-Sema::ExprResult Sema::ActOnKeywordMessage(
- ExprTy *receiver, ObjcKeywordMessage *Keywords, unsigned NumKeywords,
- SourceLocation lbrac, SourceLocation rbrac) {
- IdentifierInfo &SelName = DeriveSelector(Keywords, NumKeywords, Context);
+// ActOnInstanceMessage - used for both unary and keyword messages.
+// ArgExprs is optional - if it is present, the number of expressions
+// is obtained from Sel.getNumArgs().
+Sema::ExprResult Sema::ActOnInstanceMessage(
+ ExprTy *receiver, SelectorInfo *Sel,
+ SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args)
+{
assert(receiver && "missing receiver expression");
Expr *RExpr = static_cast<Expr *>(receiver);
- return new ObjCMessageExpr(RExpr, SelName, Keywords, NumKeywords,
- Context.IntTy/*FIXME*/, lbrac, rbrac);
+ Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
+ return new ObjCMessageExpr(RExpr, Sel,
+ Context.IntTy/*FIXME*/, lbrac, rbrac, ArgExprs);
}
-
-// This actions handles unary message to classes.
-Sema::ExprResult Sema::ActOnUnaryMessage(
- IdentifierInfo *receivingClassName, IdentifierInfo *selName,
- SourceLocation lbrac, SourceLocation rbrac) {
- assert(receivingClassName && "missing receiver class name");
-
- // FIXME: this should be passed in...
- IdentifierInfo &SName = Context.Idents.get(
- selName->getName(), selName->getName()+strlen(selName->getName()));
- return new ObjCMessageExpr(receivingClassName, SName,
- Context.IntTy/*FIXME*/, lbrac, rbrac);
-}
-
-// This action handles unary messages to instances.
-Sema::ExprResult Sema::ActOnUnaryMessage(
- ExprTy *receiver, IdentifierInfo *selName,
- SourceLocation lbrac, SourceLocation rbrac) {
- assert(receiver && "missing receiver expression");
-
- Expr *RExpr = static_cast<Expr *>(receiver);
- // FIXME: this should be passed in...
- IdentifierInfo &SName = Context.Idents.get(
- selName->getName(), selName->getName()+strlen(selName->getName()));
- return new ObjCMessageExpr(RExpr, SName,
- Context.IntTy/*FIXME*/, lbrac, rbrac);
-}
-