Make sure methods with no return type default to "id".

This fixes a crasher in Sema::MatchTwoMethodDeclarations(), identified by selector-overload.m (just added).

Added Action::ActOnTranslationUnitScope() and renamed Action::PopScope to ActOnPopScope.

Added a Translation Unit Scope instance variable to Sema (will be very useful to ObjC-related actions, since ObjC declarations are always file-scoped).


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42817 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp
index c9959df..6608330 100644
--- a/AST/ASTContext.cpp
+++ b/AST/ASTContext.cpp
@@ -110,7 +110,6 @@
   Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
 }
 
-
 void ASTContext::InitBuiltinTypes() {
   assert(VoidTy.isNull() && "Context reinitialized?");
   
diff --git a/Parse/Parser.cpp b/Parse/Parser.cpp
index 117afab..fc5c4ae 100644
--- a/Parse/Parser.cpp
+++ b/Parse/Parser.cpp
@@ -192,7 +192,7 @@
   // Inform the actions module that this scope is going away if there are any
   // decls in it.
   if (!CurScope->decl_empty())
-    Actions.PopScope(Tok.getLocation(), CurScope);
+    Actions.ActOnPopScope(Tok.getLocation(), CurScope);
   
   Scope *OldScope = CurScope;
   CurScope = OldScope->getParent();
@@ -228,7 +228,8 @@
   // Create the translation unit scope.  Install it as the current scope.
   assert(CurScope == 0 && "A scope is already active?");
   EnterScope(Scope::DeclScope);
-    
+  Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
+  
   // Install builtin types.
   // TODO: Move this someplace more useful.
   {
diff --git a/Sema/Sema.cpp b/Sema/Sema.cpp
index 1a01270..125b0cb 100644
--- a/Sema/Sema.cpp
+++ b/Sema/Sema.cpp
@@ -19,6 +19,10 @@
 
 using namespace clang;
 
+void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
+  TUScope = S;
+}
+
 Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
   : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
   
diff --git a/Sema/Sema.h b/Sema/Sema.h
index e2ec394..6c2b41e 100644
--- a/Sema/Sema.h
+++ b/Sema/Sema.h
@@ -113,6 +113,9 @@
   /// This list is populated upon the creation of a Sema object.    
   IdentifierInfo* KnownFunctionIDs[ id_num_known_functions ];
   
+  /// Translation Unit Scope - useful to many Objective-C actions that need
+  /// to lookup file scope declarations (like classes, protocols, etc.).
+  Scope *TUScope;
 public:
   Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
   
@@ -161,7 +164,10 @@
 
   virtual DeclTy *ActOnStartOfFunctionDef(Scope *S, Declarator &D);
   virtual DeclTy *ActOnFunctionDefBody(DeclTy *Decl, StmtTy *Body);
-  virtual void PopScope(SourceLocation Loc, Scope *S);
+  
+  /// Scope actions.
+  virtual void ActOnPopScope(SourceLocation Loc, Scope *S);
+  virtual void ActOnTranslationUnitScope(SourceLocation Loc, Scope *S);
 
   /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
   /// no declarator (e.g. "struct foo;") is parsed.
diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp
index d85ee6b..ba6c7d0 100644
--- a/Sema/SemaDecl.cpp
+++ b/Sema/SemaDecl.cpp
@@ -34,7 +34,7 @@
   return 0;
 }
 
-void Sema::PopScope(SourceLocation Loc, Scope *S) {
+void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
   if (S->decl_empty()) return;
   assert((S->getFlags() & Scope::DeclScope) &&"Scope shouldn't contain decls!");
          
@@ -1782,7 +1782,18 @@
                               VarDecl::None, 0);
     Params.push_back(Param);
   }
-  QualType resultDeclType = QualType::getFromOpaquePtr(ReturnType);
+  QualType resultDeclType;
+  
+  if (ReturnType)
+    resultDeclType = QualType::getFromOpaquePtr(ReturnType);
+  else { // get the type for "id".
+    IdentifierInfo *IdIdent = &Context.Idents.get("id");
+    ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary, 
+                                          SourceLocation(), TUScope);
+    TypedefDecl *IdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl);
+    assert(IdTypedef && "ActOnMethodDeclaration(): Couldn't find 'id' type");
+    resultDeclType = IdTypedef->getUnderlyingType();
+  }
   ObjcMethodDecl* ObjcMethod =  new ObjcMethodDecl(MethodLoc, Sel,
                                       resultDeclType, 0, -1, AttrList, 
                                       MethodType == tok::minus,
diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj
index c7f4908..0db725b 100644
--- a/clang.xcodeproj/project.pbxproj
+++ b/clang.xcodeproj/project.pbxproj
@@ -237,7 +237,7 @@
 		84AF36A00CB17A3B00C820A5 /* DeclObjC.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = DeclObjC.h; path = clang/AST/DeclObjC.h; sourceTree = "<group>"; };
 		84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; };
 		84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; };
-		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
 		DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
 		DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
 		DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };
@@ -739,7 +739,6 @@
 		08FB7793FE84155DC02AAC07 /* Project object */ = {
 			isa = PBXProject;
 			buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
-			compatibilityVersion = "Xcode 2.4";
 			hasScannedForEncodings = 1;
 			mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
 			projectDirPath = "";
diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h
index 31045f3..af8170f 100644
--- a/include/clang/Parse/Action.h
+++ b/include/clang/Parse/Action.h
@@ -135,10 +135,14 @@
   }
 
   
-  /// PopScope - This callback is called immediately before the specified scope
-  /// is popped and deleted.
-  virtual void PopScope(SourceLocation Loc, Scope *S) {}
-  
+  /// ActOnPopScope - This callback is called immediately before the specified
+  /// scope is popped and deleted.
+  virtual void ActOnPopScope(SourceLocation Loc, Scope *S) {}
+
+  /// ActOnTranslationUnitScope - This callback is called once, immediately
+  /// after creating the translation unit scope (in Parser::Initialize).
+  virtual void ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {}
+    
   /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
   /// no declarator (e.g. "struct foo;") is parsed.
   virtual DeclTy *ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
diff --git a/test/Sema/selector-overload.m b/test/Sema/selector-overload.m
new file mode 100644
index 0000000..8b433a4
--- /dev/null
+++ b/test/Sema/selector-overload.m
@@ -0,0 +1,43 @@
+// RUN: clang %s -parse-ast
+#import <Foundation/NSObject.h>
+
+struct D {
+  double d;
+};
+
+@interface Foo : NSObject 
+
+- method:(int)a;
+- method:(int)a;
+
+@end
+
+@interface Bar : NSObject 
+
+- method:(void *)a;
+
+@end
+
+@interface Car : NSObject 
+
+- method:(struct D)a;
+
+@end
+
+@interface Zar : NSObject 
+
+- method:(float)a;
+
+@end
+
+@interface Rar : NSObject 
+
+- method:(float)a;
+
+@end
+
+int main() {
+//  id xx = [[Car alloc] init];
+
+//  [xx method:4];
+}