[cfe-commits] r42842 - in /cfe/trunk: Sema/Sema.cpp Sema/Sema.h Sema/SemaDecl.cpp Sema/SemaExpr.cpp test/Sema/selector-overload.m

Steve Naroff snaroff at apple.com
Wed Oct 10 14:53:09 PDT 2007


Author: snaroff
Date: Wed Oct 10 16:53:07 2007
New Revision: 42842

URL: http://llvm.org/viewvc/llvm-project?rev=42842&view=rev
Log:

- Make sure default return/argument types (for methods) default to "id".
- Cache the "id" type in Sema...initialize ObjcIdType and TUScope (oops).
- Fix ActOnInstanceMessage to allow for "id" type receivers...still work to do (next).

Modified:
    cfe/trunk/Sema/Sema.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/selector-overload.m

Modified: cfe/trunk/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.cpp?rev=42842&r1=42841&r2=42842&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.cpp (original)
+++ cfe/trunk/Sema/Sema.cpp Wed Oct 10 16:53:07 2007
@@ -23,6 +23,20 @@
   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 @@
   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) {

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=42842&r1=42841&r2=42842&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Wed Oct 10 16:53:07 2007
@@ -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 @@
   /// 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 @@
   /// 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.

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=42842&r1=42841&r2=42842&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Wed Oct 10 16:53:07 2007
@@ -1774,23 +1774,23 @@
 
   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,

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=42842&r1=42841&r2=42842&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Wed Oct 10 16:53:07 2007
@@ -1900,19 +1900,25 @@
   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);
 }

Modified: cfe/trunk/test/Sema/selector-overload.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/selector-overload.m?rev=42842&r1=42841&r2=42842&view=diff

==============================================================================
--- cfe/trunk/test/Sema/selector-overload.m (original)
+++ cfe/trunk/test/Sema/selector-overload.m Wed Oct 10 16:53:07 2007
@@ -37,7 +37,7 @@
 @end
 
 int main() {
-//  id xx = [[Car alloc] init];
+  id xx = [[Car alloc] init];
 
-//  [xx method:4];
+  [xx method:4];
 }





More information about the cfe-commits mailing list