]> granicus.if.org Git - clang/commitdiff
- Make sure default return/argument types (for methods) default to "id".
authorSteve Naroff <snaroff@apple.com>
Wed, 10 Oct 2007 21:53:07 +0000 (21:53 +0000)
committerSteve Naroff <snaroff@apple.com>
Wed, 10 Oct 2007 21:53:07 +0000 (21:53 +0000)
- Cache the "id" type in Sema...initialize ObjcIdType and TUScope (oops).
- Fix ActOnInstanceMessage to allow for "id" type receivers...still work to do (next).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42842 91177308-0d34-0410-b5e6-96231b3b80d8

Sema/Sema.cpp
Sema/Sema.h
Sema/SemaDecl.cpp
Sema/SemaExpr.cpp
test/Sema/selector-overload.m

index 125b0cb63fd97194a403b234b05f68373add0b9c..f34aa6362fcbf133c6ec1ff7c721d0c1c7b3a673 100644 (file)
@@ -23,6 +23,20 @@ void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
   TUScope = S;
 }
 
+QualType Sema::GetObjcIdType() {
+  assert(TUScope && "GetObjcIdType(): Top-level scope is null");
+  if (ObjcIdType.isNull()) {
+    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 && "GetObjcIdType(): Couldn't find 'id' type");
+    ObjcIdType = Context.getTypedefType(IdTypedef);
+  }
+  return ObjcIdType;
+}
+
+
 Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
   : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
   
@@ -40,6 +54,9 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
   KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
   KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
   KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
+  
+  TUScope = 0;
+  ObjcIdType = QualType();
 }
 
 void Sema::DeleteExpr(ExprTy *E) {
index b1f1dcde8d6bb3bf3bde39cc938e1cbd69ef2bc1..0ac513e2de22b83258e0ae5eab0507925eaf0e9a 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "clang/AST/Type.h"
 #include <vector>
 #include <string>
 
@@ -117,6 +118,9 @@ class Sema : public Action {
   /// to lookup file scope declarations in the "ordinary" C decl namespace.
   /// For example, user-defined classes, built-in "id" type, etc.
   Scope *TUScope;
+  
+  /// ObjcIdType - built-in type for "id".
+  QualType ObjcIdType;
 public:
   Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup);
   
@@ -251,6 +255,9 @@ private:
   /// true, or false, accordingly.
   bool MatchTwoMethodDeclarations(const ObjcMethodDecl *Method, 
                                   const ObjcMethodDecl *PrevMethod); 
+
+  /// GetObjcIdType - Getter for the build-in "id" type.
+  QualType GetObjcIdType();
   
   //===--------------------------------------------------------------------===//
   // Statement Parsing Callbacks: SemaStmt.cpp.
index 4fc6f78fbed242cbffc73eb350a05e2b156a020c..eb1b15839d1506ef31ae7ff0ad541edb1a4b8678 100644 (file)
@@ -1774,23 +1774,23 @@ Sema::DeclTy *Sema::ActOnMethodDeclaration(SourceLocation MethodLoc,
 
   for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
     // FIXME: arg->AttrList must be stored too!
+    QualType argType;
+    
+    if (ArgTypes[i])
+      argType = QualType::getFromOpaquePtr(ArgTypes[i]);
+    else
+      argType = GetObjcIdType();
     ParmVarDecl* Param = new ParmVarDecl(SourceLocation(/*FIXME*/), ArgNames[i], 
-                              QualType::getFromOpaquePtr(ArgTypes[i]), 
-                              VarDecl::None, 0);
+                                         argType, VarDecl::None, 0);
     Params.push_back(Param);
   }
   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();
-  }
+  else // get the type for "id".
+    resultDeclType = GetObjcIdType();
+
   ObjcMethodDecl* ObjcMethod =  new ObjcMethodDecl(MethodLoc, Sel,
                                       resultDeclType, 0, -1, AttrList, 
                                       MethodType == tok::minus,
index 29f422c04ab5fbf06bde867a048e14728e5cd4e4..08e13685135ffc271f541efc1047859ff242839c 100644 (file)
@@ -1900,19 +1900,25 @@ Sema::ExprResult Sema::ActOnInstanceMessage(
   assert(receiver && "missing receiver expression");
   
   Expr *RExpr = static_cast<Expr *>(receiver);
-  // FIXME (snaroff): checking in this code from Patrick. Needs to be revisited.
-  // how do we get the ClassDecl from the receiver expression?
   QualType receiverType = RExpr->getType();
-  while (receiverType->isPointerType()) {
-    PointerType *pointerType = static_cast<PointerType*>(receiverType.getTypePtr());
-    receiverType = pointerType->getPointeeType();
+  QualType returnType;
+  
+  if (receiverType == GetObjcIdType()) {
+    returnType = Context.IntTy; // FIXME:just a placeholder
+  } else {
+    // FIXME (snaroff): checking in this code from Patrick. Needs to be revisited.
+    // how do we get the ClassDecl from the receiver expression?
+    while (receiverType->isPointerType()) {
+      PointerType *pointerType = static_cast<PointerType*>(receiverType.getTypePtr());
+      receiverType = pointerType->getPointeeType();
+    }
+    assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) && "bad receiver type");
+    ObjcInterfaceDecl* ClassDecl = static_cast<ObjcInterfaceType*>(
+                                     receiverType.getTypePtr())->getDecl();
+    ObjcMethodDecl *Method = ClassDecl->lookupInstanceMethod(Sel);
+    assert(Method && "missing method declaration");
+    returnType = Method->getMethodType();
   }
-  assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) && "bad receiver type");
-  ObjcInterfaceDecl* ClassDecl = static_cast<ObjcInterfaceType*>(
-                                   receiverType.getTypePtr())->getDecl();
-  ObjcMethodDecl *Method = ClassDecl->lookupInstanceMethod(Sel);
-  assert(Method && "missing method declaration");
-  QualType returnType = Method->getMethodType();
   Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
   return new ObjCMessageExpr(RExpr, Sel, returnType, lbrac, rbrac, ArgExprs);
 }
index 8b433a480308b3b9f1f430228078bc869d1aaa5e..a542e57d2ba093db3c08898ee4bbc7110b4f1577 100644 (file)
@@ -37,7 +37,7 @@ struct D {
 @end
 
 int main() {
-//  id xx = [[Car alloc] init];
+  id xx = [[Car alloc] init];
 
-//  [xx method:4];
+  [xx method:4];
 }