[cfe-commits] r72951 - in /cfe/trunk: include/clang/AST/DeclObjC.h include/clang/Parse/Action.h lib/AST/DeclObjC.cpp lib/Frontend/PrintParserCallbacks.cpp lib/Parse/ParseObjc.cpp lib/Sema/Sema.h lib/Sema/SemaDecl.cpp

Fariborz Jahanian fjahanian at apple.com
Fri Jun 5 11:16:35 PDT 2009


Author: fjahanian
Date: Fri Jun  5 13:16:35 2009
New Revision: 72951

URL: http://llvm.org/viewvc/llvm-project?rev=72951&view=rev
Log:
Use of DeclContext for objc's ivars. No functionality
change. More to follow.

Modified:
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/lib/AST/DeclObjC.cpp
    cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp
    cfe/trunk/lib/Parse/ParseObjc.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp

Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=72951&r1=72950&r2=72951&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Fri Jun  5 13:16:35 2009
@@ -330,6 +330,7 @@
   // Get the local instance/class method declared in this interface.
   ObjCMethodDecl *getInstanceMethod(ASTContext &Context, Selector Sel) const;
   ObjCMethodDecl *getClassMethod(ASTContext &Context, Selector Sel) const;
+  ObjCIvarDecl *getIvarDecl(ASTContext &Context, IdentifierInfo *Id) const;
 
   ObjCMethodDecl *
   getMethod(ASTContext &Context, Selector Sel, bool isInstance) const {

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=72951&r1=72950&r2=72951&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Fri Jun  5 13:16:35 2009
@@ -417,6 +417,7 @@
   }
   
   virtual DeclPtrTy ActOnIvar(Scope *S, SourceLocation DeclStart,
+                              DeclPtrTy IntfDecl,
                               Declarator &D, ExprTy *BitfieldWidth,
                               tok::ObjCKeywordKind visibility) {
     return DeclPtrTy();

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=72951&r1=72950&r2=72951&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Fri Jun  5 13:16:35 2009
@@ -42,6 +42,19 @@
 // ObjCInterfaceDecl
 //===----------------------------------------------------------------------===//
 
+/// getIvarDecl - This method looks up an ivar in this ContextDecl.
+///
+ObjCIvarDecl *
+ObjCContainerDecl::getIvarDecl(ASTContext &Context, IdentifierInfo *Id) const {
+  lookup_const_iterator Ivar, IvarEnd;
+  for (llvm::tie(Ivar, IvarEnd) = lookup(Context, Id);
+       Ivar != IvarEnd; ++Ivar) {
+    if (ObjCIvarDecl *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
+      return ivar;
+  }
+  return 0;
+}
+
 // Get the local instance method declared in this interface.
 ObjCMethodDecl *
 ObjCContainerDecl::getInstanceMethod(ASTContext &Context, Selector Sel) const {
@@ -139,12 +152,9 @@
   ASTContext &Context, IdentifierInfo *ID, ObjCInterfaceDecl *&clsDeclared) {
   ObjCInterfaceDecl* ClassDecl = this;
   while (ClassDecl != NULL) {
-    for (ivar_iterator I = ClassDecl->ivar_begin(), E = ClassDecl->ivar_end();
-         I != E; ++I) {
-      if ((*I)->getIdentifier() == ID) {
-        clsDeclared = ClassDecl;
-        return *I;
-      }
+    if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(Context, ID)) {
+      clsDeclared = ClassDecl;
+      return I;
     }
     // look into properties.
     for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(Context),

Modified: cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp?rev=72951&r1=72950&r2=72951&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp Fri Jun  5 13:16:35 2009
@@ -220,6 +220,7 @@
     }
   
     virtual DeclPtrTy ActOnIvar(Scope *S, SourceLocation DeclStart,
+                                DeclPtrTy IntfDecl,
                                 Declarator &D, ExprTy *BitfieldWidth,
                                 tok::ObjCKeywordKind visibility) {
       Out << __FUNCTION__ << "\n";

Modified: cfe/trunk/lib/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseObjc.cpp?rev=72951&r1=72950&r2=72951&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/lib/Parse/ParseObjc.cpp Fri Jun  5 13:16:35 2009
@@ -891,6 +891,7 @@
       // Install the declarator into interfaceDecl.
       DeclPtrTy Field = Actions.ActOnIvar(CurScope,
                                           DS.getSourceRange().getBegin(),
+                                          interfaceDecl,
                                           FD.D, FD.BitfieldSize, visibility);
       AllIvarDecls.push_back(Field);
     }

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Jun  5 13:16:35 2009
@@ -472,6 +472,7 @@
                             Declarator *D = 0);
   
   virtual DeclPtrTy ActOnIvar(Scope *S, SourceLocation DeclStart,
+                              DeclPtrTy IntfDecl,
                               Declarator &D, ExprTy *BitfieldWidth,
                               tok::ObjCKeywordKind visibility);
 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Jun  5 13:16:35 2009
@@ -3914,6 +3914,7 @@
 /// in order to create an IvarDecl object for it.
 Sema::DeclPtrTy Sema::ActOnIvar(Scope *S,
                                 SourceLocation DeclStart, 
+                                DeclPtrTy IntfDecl,
                                 Declarator &D, ExprTy *BitfieldWidth,
                                 tok::ObjCKeywordKind Visibility) {
   
@@ -3952,14 +3953,28 @@
   ObjCIvarDecl::AccessControl ac = 
     Visibility != tok::objc_not_keyword ? TranslateIvarVisibility(Visibility)
                                         : ObjCIvarDecl::None;
-
+  // Must set ivar's DeclContext to its enclosing interface.
+  Decl *EnclosingDecl = IntfDecl.getAs<Decl>();
+  DeclContext *EnclosingContext;
+  if (ObjCImplementationDecl *IMPDecl = 
+      dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
+    // Case of ivar declared in an implementation. Context is that of its class.
+    ObjCInterfaceDecl* IDecl = IMPDecl->getClassInterface();
+    assert(IDecl && "No class- ActOnIvar");
+    EnclosingContext = cast_or_null<DeclContext>(IDecl);
+  }
+  else
+    EnclosingContext = dyn_cast<DeclContext>(EnclosingDecl);
+  assert(EnclosingContext && "null DeclContext for ivar - ActOnIvar");
+  
   // Construct the decl.
-  ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, CurContext, Loc, II, T,ac,
+  ObjCIvarDecl *NewID = ObjCIvarDecl::Create(Context, 
+                                             EnclosingContext, Loc, II, T,ac,
                                              (Expr *)BitfieldWidth);
   
   if (II) {
     NamedDecl *PrevDecl = LookupName(S, II, LookupMemberName, true);
-    if (PrevDecl && isDeclInScope(PrevDecl, CurContext, S)
+    if (PrevDecl && isDeclInScope(PrevDecl, EnclosingContext, S)
         && !isa<TagDecl>(PrevDecl)) {
       Diag(Loc, diag::err_duplicate_member) << II;
       Diag(PrevDecl->getLocation(), diag::note_previous_declaration);
@@ -4100,7 +4115,11 @@
     if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) {
       ID->setIVarList(ClsFields, RecFields.size(), Context);
       ID->setLocEnd(RBrac);
-      
+      // Add ivar's to class's DeclContext.
+      for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
+        ClsFields[i]->setLexicalDeclContext(ID);
+        ID->addDecl(Context, ClsFields[i]);
+      }
       // Must enforce the rule that ivars in the base classes may not be
       // duplicates.
       if (ID->getSuperClass()) {
@@ -4121,12 +4140,10 @@
     } else if (ObjCImplementationDecl *IMPDecl = 
                   dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
       assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");
-      for (unsigned I = 0, N = RecFields.size(); I != N; ++I) {
-        // FIXME: Set the DeclContext correctly when we build the
-        // declarations.
+      for (unsigned I = 0, N = RecFields.size(); I != N; ++I)
+        // Ivar declared in @implementation never belongs to the implementation.
+        // Only it is in implementation's lexical context.
         ClsFields[I]->setLexicalDeclContext(IMPDecl);
-        IMPDecl->addDecl(Context, ClsFields[I]);
-      }
       CheckImplementationIvars(IMPDecl, ClsFields, RecFields.size(), RBrac);
     }
   }





More information about the cfe-commits mailing list