[cfe-commits] r102500 - in /cfe/trunk: include/clang/AST/DeclObjC.h lib/Frontend/PCHReaderDecl.cpp lib/Frontend/PCHWriterDecl.cpp lib/Sema/Sema.h lib/Sema/SemaDeclCXX.cpp lib/Sema/SemaDeclObjC.cpp

Fariborz Jahanian fjahanian at apple.com
Wed Apr 28 09:11:27 PDT 2010


Author: fjahanian
Date: Wed Apr 28 11:11:27 2010
New Revision: 102500

URL: http://llvm.org/viewvc/llvm-project?rev=102500&view=rev
Log:
More of Sema to implement initialization of
ivar of c++ object types.


Modified:
    cfe/trunk/include/clang/AST/DeclObjC.h
    cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
    cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp

Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=102500&r1=102499&r2=102500&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Wed Apr 28 11:11:27 2010
@@ -29,6 +29,7 @@
 class ObjCCategoryDecl;
 class ObjCPropertyDecl;
 class ObjCPropertyImplDecl;
+class CXXBaseOrMemberInitializer;
 
 class ObjCListBase {
   void operator=(const ObjCListBase &);     // DO NOT IMPLEMENT
@@ -1152,18 +1153,54 @@
 class ObjCImplementationDecl : public ObjCImplDecl {
   /// Implementation Class's super class.
   ObjCInterfaceDecl *SuperClass;
-
+  /// Support for ivar initialization.
+  /// IvarInitializers - The arguments used to initialize the ivars
+  CXXBaseOrMemberInitializer **IvarInitializers;
+  unsigned NumIvarInitializers;
+  
   ObjCImplementationDecl(DeclContext *DC, SourceLocation L,
                          ObjCInterfaceDecl *classInterface,
                          ObjCInterfaceDecl *superDecl)
     : ObjCImplDecl(ObjCImplementation, DC, L, classInterface),
-       SuperClass(superDecl){}
+       SuperClass(superDecl), IvarInitializers(0), NumIvarInitializers(0) {}
 public:
   static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
                                         SourceLocation L,
                                         ObjCInterfaceDecl *classInterface,
                                         ObjCInterfaceDecl *superDecl);
-
+  
+  /// init_iterator - Iterates through the ivar initializer list.
+  typedef CXXBaseOrMemberInitializer **init_iterator;
+  
+  /// init_const_iterator - Iterates through the ivar initializer list.
+  typedef CXXBaseOrMemberInitializer * const * init_const_iterator;
+  
+  /// init_begin() - Retrieve an iterator to the first initializer.
+  init_iterator       init_begin()       { return IvarInitializers; }
+  /// begin() - Retrieve an iterator to the first initializer.
+  init_const_iterator init_begin() const { return IvarInitializers; }
+  
+  /// init_end() - Retrieve an iterator past the last initializer.
+  init_iterator       init_end()       {
+    return IvarInitializers + NumIvarInitializers;
+  }
+  /// end() - Retrieve an iterator past the last initializer.
+  init_const_iterator init_end() const {
+    return IvarInitializers + NumIvarInitializers;
+  }
+  /// getNumArgs - Number of ivars which must be initialized.
+  unsigned getNumIvarInitializers() const {
+    return NumIvarInitializers;
+  }
+  
+  void setNumIvarInitializers(unsigned numNumIvarInitializers) {
+    NumIvarInitializers = numNumIvarInitializers;
+  }
+  
+  void setIvarInitializers(ASTContext &C,
+                           CXXBaseOrMemberInitializer ** initializers,
+                           unsigned numInitializers);
+    
   /// getIdentifier - Get the identifier that names the class
   /// interface associated with this implementation.
   IdentifierInfo *getIdentifier() const {

Modified: cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderDecl.cpp?rev=102500&r1=102499&r2=102500&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderDecl.cpp Wed Apr 28 11:11:27 2010
@@ -383,6 +383,7 @@
   VisitObjCImplDecl(D);
   D->setSuperClass(
               cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
+  // FIXME. Add reading of IvarInitializers and NumIvarInitializers.
 }
 
 

Modified: cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterDecl.cpp?rev=102500&r1=102499&r2=102500&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriterDecl.cpp Wed Apr 28 11:11:27 2010
@@ -361,6 +361,7 @@
 void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
   VisitObjCImplDecl(D);
   Writer.AddDeclRef(D->getSuperClass(), Record);
+  // FIXME add writing of IvarInitializers and NumIvarInitializers.
   Code = pch::DECL_OBJC_IMPLEMENTATION;
 }
 

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=102500&r1=102499&r2=102500&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Apr 28 11:11:27 2010
@@ -1592,10 +1592,9 @@
   void AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method);
   
   /// CollectIvarsToConstructOrDestruct - Collect those ivars which require
-  /// construction (construct=true) or destruction (construct=false)
+  /// initialization.
   void CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
-                                    llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
-                                    bool construct=true);
+                                  llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars);
   //===--------------------------------------------------------------------===//
   // Statement Parsing Callbacks: SemaStmt.cpp.
 public:
@@ -2510,6 +2509,9 @@
   bool SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor,
                                    CXXBaseOrMemberInitializer **Initializers,
                                    unsigned NumInitializers, bool AnyErrors);
+  
+  void SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation);
+                           
 
   /// MarkBaseAndMemberDestructorsReferenced - Given a record decl,
   /// mark all the non-trivial destructors of its members and bases as

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=102500&r1=102499&r2=102500&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Wed Apr 28 11:11:27 2010
@@ -5699,3 +5699,44 @@
     MarkVirtualMembersReferenced(Loc, Base);
   }
 }
+
+/// SetIvarInitializers - This routine builds initialization ASTs for the
+/// Objective-C implementation whose ivars need be initialized.
+void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) {
+  if (!getLangOptions().CPlusPlus)
+    return;
+  if (const ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) {
+    llvm::SmallVector<ObjCIvarDecl*, 8> ivars;
+    CollectIvarsToConstructOrDestruct(OID, ivars);
+    if (ivars.empty())
+      return;
+    llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit;
+    for (unsigned i = 0; i < ivars.size(); i++) {
+      FieldDecl *Field = ivars[i];
+      CXXBaseOrMemberInitializer *Member;
+      InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field);
+      InitializationKind InitKind = 
+        InitializationKind::CreateDefault(ObjCImplementation->getLocation());
+      
+      InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0);
+      Sema::OwningExprResult MemberInit = 
+        InitSeq.Perform(*this, InitEntity, InitKind, 
+                        Sema::MultiExprArg(*this, 0, 0));
+      MemberInit = MaybeCreateCXXExprWithTemporaries(move(MemberInit));
+      // Note, MemberInit could actually come back empty if no initialization 
+      // is required (e.g., because it would call a trivial default constructor)
+      if (!MemberInit.get() || MemberInit.isInvalid())
+        continue;
+      
+      Member =
+        new (Context) CXXBaseOrMemberInitializer(Context,
+                                                 Field, SourceLocation(),
+                                                 SourceLocation(),
+                                                 MemberInit.takeAs<Expr>(),
+                                                 SourceLocation());
+      AllToInit.push_back(Member);
+    }
+    ObjCImplementation->setIvarInitializers(Context, 
+                                            AllToInit.data(), AllToInit.size());
+  }
+}

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=102500&r1=102499&r2=102500&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Wed Apr 28 11:11:27 2010
@@ -1441,6 +1441,7 @@
           IDecl = IDecl->getSuperClass();
         }
     }
+    SetIvarInitializers(IC);
   } else if (ObjCCategoryImplDecl* CatImplClass =
                                    dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
     CatImplClass->setAtEndRange(AtEnd);
@@ -1800,23 +1801,15 @@
 }
 
 /// CollectIvarsToConstructOrDestruct - Collect those ivars which require
-/// construction (construct=true) or destruction (construct=false)
-///
+/// initialization.
 void Sema::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
-                                    llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
-                                    bool construct) {
-  if (!getLangOptions().CPlusPlus)
-    return;
+                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
   for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
        E = OI->ivar_end(); I != E; ++I) {
     ObjCIvarDecl *Iv = (*I);
     QualType QT = Context.getBaseElementType(Iv->getType());
-    if (const RecordType *RT = QT->getAs<RecordType>()) {
-      if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
-        if (construct && !RD->hasTrivialConstructor() ||
-            !construct && !RD->hasTrivialDestructor())
-          Ivars.push_back(*I);
-    }
+    if (isa<RecordType>(QT))
+      Ivars.push_back(*I);
   }
   
   // Find ivars to construct/destruct in class extension.
@@ -1825,12 +1818,8 @@
          E = CDecl->ivar_end(); I != E; ++I) {
       ObjCIvarDecl *Iv = (*I);
       QualType QT = Context.getBaseElementType(Iv->getType());
-      if (const RecordType *RT = QT->getAs<RecordType>()) {
-        if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
-          if (construct && !RD->hasTrivialConstructor() ||
-              !construct && !RD->hasTrivialDestructor())
-            Ivars.push_back(*I);
-      }
+      if (isa<RecordType>(QT))
+        Ivars.push_back(*I);
     }
   }
   
@@ -1841,12 +1830,22 @@
          E = ImplDecl->ivar_end(); I != E; ++I) {
       ObjCIvarDecl *Iv = (*I);
       QualType QT = Context.getBaseElementType(Iv->getType());
-      if (const RecordType *RT = QT->getAs<RecordType>()) {
-        if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
-          if (construct && !RD->hasTrivialConstructor() ||
-              !construct && !RD->hasTrivialDestructor())
-            Ivars.push_back(*I);
-      }
+      if (isa<RecordType>(QT))
+        Ivars.push_back(*I);
     }
   }
 }
+
+void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
+                                    CXXBaseOrMemberInitializer ** initializers,
+                                                 unsigned numInitializers) {
+  if (numInitializers > 0) {
+    NumIvarInitializers = numInitializers;
+    CXXBaseOrMemberInitializer **ivarInitializers =
+    new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
+    memcpy(ivarInitializers, initializers,
+           numInitializers * sizeof(CXXBaseOrMemberInitializer*));
+    IvarInitializers = ivarInitializers;
+  }
+}
+





More information about the cfe-commits mailing list