/// ObjcIdTypedef - built-in typedef for "id".
TypedefDecl *ObjcIdTypedef;
+
+ /// ObjCMethodList - a linked list of methods with different signatures.
+ struct ObjcMethodList {
+ ObjcMethodDecl *Method;
+ ObjcMethodList *Next;
+
+ ObjcMethodList() {
+ Method = 0;
+ Next = 0;
+ }
+ ObjcMethodList(ObjcMethodDecl *M, ObjcMethodList *C) {
+ Method = M;
+ Next = C;
+ }
+ };
+ /// Instance/Factory Method Pools - allows efficient lookup when typechecking
+ /// messages to "id". We need to maintain a list, since selectors can have
+ /// differing signatures across classes. In Cocoa, this happens to be
+ /// extremely uncommon (only 1% of selectors are "overloaded").
+ llvm::DenseMap<Selector, ObjcMethodList> InstanceMethodPool;
+ llvm::DenseMap<Selector, ObjcMethodList> FactoryMethodPool;
public:
Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
/// GetObjcIdType - Getter for the build-in "id" type.
QualType GetObjcIdType(SourceLocation Loc = SourceLocation());
+ /// AddInstanceMethodToGlobalPool - All instance methods in a translation
+ /// unit are added to a global pool. This allows us to efficiently associate
+ /// a selector with a method declaraation for purposes of typechecking
+ /// messages sent to "id" (where the class of the object is unknown).
+ void AddInstanceMethodToGlobalPool(ObjcMethodDecl *Method);
+
+ /// AddFactoryMethodToGlobalPool - Same as above, but for factory methods.
+ void AddFactoryMethodToGlobalPool(ObjcMethodDecl *Method);
//===--------------------------------------------------------------------===//
// Statement Parsing Callbacks: SemaStmt.cpp.
public:
return true;
}
+void Sema::AddInstanceMethodToGlobalPool(ObjcMethodDecl *Method) {
+ ObjcMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
+ if (!FirstMethod.Method) {
+ // Haven't seen a method with this selector name yet - add it.
+ FirstMethod.Method = Method;
+ FirstMethod.Next = 0;
+ } else {
+ // We've seen a method with this name, now check the type signature(s).
+ bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
+
+ for (ObjcMethodList *Next = FirstMethod.Next; !match && Next;
+ Next = Next->Next)
+ match = MatchTwoMethodDeclarations(Method, Next->Method);
+
+ if (!match) {
+ // We have a new signature for an existing method - add it.
+ // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
+ struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
+ FirstMethod.Next = OMI;
+ }
+ }
+}
+
+void Sema::AddFactoryMethodToGlobalPool(ObjcMethodDecl *Method) {
+ ObjcMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
+ if (!FirstMethod.Method) {
+ // Haven't seen a method with this selector name yet - add it.
+ FirstMethod.Method = Method;
+ FirstMethod.Next = 0;
+ } else {
+ // We've seen a method with this name, now check the type signature(s).
+ bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
+
+ for (ObjcMethodList *Next = FirstMethod.Next; !match && Next;
+ Next = Next->Next)
+ match = MatchTwoMethodDeclarations(Method, Next->Method);
+
+ if (!match) {
+ // We have a new signature for an existing method - add it.
+ // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
+ struct ObjcMethodList *OMI = new ObjcMethodList(Method, FirstMethod.Next);
+ FirstMethod.Next = OMI;
+ }
+ }
+}
+
void Sema::ActOnAddMethodsToObjcDecl(Scope* S, DeclTy *classDecl,
DeclTy **allMethods, unsigned allNum) {
Decl *ClassDecl = static_cast<Decl *>(classDecl);
for (unsigned i = 0; i < allNum; i++ ) {
ObjcMethodDecl *Method =
cast_or_null<ObjcMethodDecl>(static_cast<Decl*>(allMethods[i]));
+
if (!Method) continue; // Already issued a diagnostic.
if (Method->isInstance()) {
if (checkDuplicateMethods) {
Diag(Method->getLocation(), diag::error_duplicate_method_decl,
Method->getSelector().getName());
Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
- }
- else {
+ } else {
insMethods.push_back(Method);
InsMap[Method->getSelector()] = Method;
}
}
else
insMethods.push_back(Method);
- }
- else {
+
+ /// The following allows us to typecheck messages to "id".
+ AddInstanceMethodToGlobalPool(Method);
+ } else {
if (checkDuplicateMethods) {
/// Check for class method of the same name with incompatible types
const ObjcMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Diag(Method->getLocation(), diag::error_duplicate_method_decl,
Method->getSelector().getName());
Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
- }
- else {
+ } else {
clsMethods.push_back(Method);
ClsMap[Method->getSelector()] = Method;
}
}
else
clsMethods.push_back(Method);
+
+ /// The following allows us to typecheck messages to "id".
+ AddFactoryMethodToGlobalPool(Method);
}
}