[cfe-commits] r39297 - in /cfe/cfe/trunk: AST/Decl.cpp AST/SemaDecl.cpp Sema/SemaDecl.cpp clang.xcodeproj/project.pbxproj include/clang/AST/Decl.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:42:40 PDT 2007


Author: sabre
Date: Wed Jul 11 11:42:40 2007
New Revision: 39297

URL: http://llvm.org/viewvc/llvm-project?rev=39297&view=rev
Log:
Save the member list of a struct/union in the RecordDecl for the struct.

Modified:
    cfe/cfe/trunk/AST/Decl.cpp
    cfe/cfe/trunk/AST/SemaDecl.cpp
    cfe/cfe/trunk/Sema/SemaDecl.cpp
    cfe/cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/cfe/trunk/include/clang/AST/Decl.h

Modified: cfe/cfe/trunk/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Decl.cpp?rev=39297&r1=39296&r2=39297&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/Decl.cpp (original)
+++ cfe/cfe/trunk/AST/Decl.cpp Wed Jul 11 11:42:40 2007
@@ -45,3 +45,17 @@
     memcpy(ParamInfo, NewParamInfo, sizeof(VarDecl*)*NumParams);
   }
 }
+
+
+/// defineBody - When created, RecordDecl's correspond to a forward declared
+/// record.  This method is used to mark the decl as being defined, with the
+/// specified contents.
+void RecordDecl::defineBody(Decl **fields, unsigned numFields) {
+  assert(!isDefinition() && "Cannot redefine record!");
+  setDefinition(true);
+  NumFields = numFields;
+  if (numFields) {
+    Fields = new Decl*[numFields];
+    memcpy(Fields, fields, numFields*sizeof(Decl*));
+  }
+}

Modified: cfe/cfe/trunk/AST/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaDecl.cpp?rev=39297&r1=39296&r2=39297&view=diff

==============================================================================
--- cfe/cfe/trunk/AST/SemaDecl.cpp (original)
+++ cfe/cfe/trunk/AST/SemaDecl.cpp Wed Jul 11 11:42:40 2007
@@ -385,7 +385,7 @@
 
 void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
                            DeclTy **Fields, unsigned NumFields) {
-  TagDecl *Record = static_cast<TagDecl*>(RecDecl);
+  RecordDecl *Record = cast<RecordDecl>(static_cast<TagDecl*>(RecDecl));
   if (Record->isDefinition()) {
     // Diagnose code like:
     //     struct S { struct S {} X; };
@@ -399,6 +399,7 @@
 
   // Verify that all the fields are okay.
   unsigned NumNamedMembers = 0;
+  SmallVector<Decl*, 32> RecFields;
   for (unsigned i = 0; i != NumFields; ++i) {
     FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
     if (!FD) continue;  // Already issued a diagnostic.
@@ -431,7 +432,7 @@
       }
       
       // Okay, we have a legal flexible array member at the end of the struct.
-      cast<RecordDecl>(Record)->setHasFlexibleArrayMember(true);
+      Record->setHasFlexibleArrayMember(true);
     }
     
     
@@ -441,7 +442,7 @@
       if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
         // If this is a member of a union, then entire union becomes "flexible".
         if (Record->getKind() == Decl::Union) {
-          cast<RecordDecl>(Record)->setHasFlexibleArrayMember(true);
+          Record->setHasFlexibleArrayMember(true);
         } else {
           // If this is a struct/class and this is not the last element, reject
           // it.  Note that GCC supports variable sized arrays in the middle of
@@ -457,7 +458,7 @@
           // as an extension.
           Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
                FD->getName());
-          cast<RecordDecl>(Record)->setHasFlexibleArrayMember(true);
+          Record->setHasFlexibleArrayMember(true);
         }
       }
     }
@@ -465,13 +466,14 @@
     // Keep track of the number of named members.
     if (FD->getIdentifier())
       ++NumNamedMembers;
+    
+    // Remember good fields.
+    RecFields.push_back(FD);
   }
  
   
   // Okay, we successfully defined 'Record'.
-  Record->setDefinition(true);
-  
-  
+  Record->defineBody(&RecFields[0], RecFields.size());
 }
 
 

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

==============================================================================
--- cfe/cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaDecl.cpp Wed Jul 11 11:42:40 2007
@@ -385,7 +385,7 @@
 
 void Sema::ParseRecordBody(SourceLocation RecLoc, DeclTy *RecDecl,
                            DeclTy **Fields, unsigned NumFields) {
-  TagDecl *Record = static_cast<TagDecl*>(RecDecl);
+  RecordDecl *Record = cast<RecordDecl>(static_cast<TagDecl*>(RecDecl));
   if (Record->isDefinition()) {
     // Diagnose code like:
     //     struct S { struct S {} X; };
@@ -399,6 +399,7 @@
 
   // Verify that all the fields are okay.
   unsigned NumNamedMembers = 0;
+  SmallVector<Decl*, 32> RecFields;
   for (unsigned i = 0; i != NumFields; ++i) {
     FieldDecl *FD = cast_or_null<FieldDecl>(static_cast<Decl*>(Fields[i]));
     if (!FD) continue;  // Already issued a diagnostic.
@@ -431,7 +432,7 @@
       }
       
       // Okay, we have a legal flexible array member at the end of the struct.
-      cast<RecordDecl>(Record)->setHasFlexibleArrayMember(true);
+      Record->setHasFlexibleArrayMember(true);
     }
     
     
@@ -441,7 +442,7 @@
       if (FDTTy->getDecl()->hasFlexibleArrayMember()) {
         // If this is a member of a union, then entire union becomes "flexible".
         if (Record->getKind() == Decl::Union) {
-          cast<RecordDecl>(Record)->setHasFlexibleArrayMember(true);
+          Record->setHasFlexibleArrayMember(true);
         } else {
           // If this is a struct/class and this is not the last element, reject
           // it.  Note that GCC supports variable sized arrays in the middle of
@@ -457,7 +458,7 @@
           // as an extension.
           Diag(FD->getLocation(), diag::ext_flexible_array_in_struct,
                FD->getName());
-          cast<RecordDecl>(Record)->setHasFlexibleArrayMember(true);
+          Record->setHasFlexibleArrayMember(true);
         }
       }
     }
@@ -465,13 +466,14 @@
     // Keep track of the number of named members.
     if (FD->getIdentifier())
       ++NumNamedMembers;
+    
+    // Remember good fields.
+    RecFields.push_back(FD);
   }
  
   
   // Okay, we successfully defined 'Record'.
-  Record->setDefinition(true);
-  
-  
+  Record->defineBody(&RecFields[0], RecFields.size());
 }
 
 

Modified: cfe/cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=39297&r1=39296&r2=39297&view=diff

==============================================================================
--- cfe/cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/cfe/trunk/clang.xcodeproj/project.pbxproj Wed Jul 11 11:42:40 2007
@@ -140,7 +140,7 @@
 /* End PBXCopyFilesBuildPhase section */
 
 /* Begin PBXFileReference section */
-		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
+		8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
 		DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
 		DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };
 		DE06BECA0A854E4B0050E87E /* Scope.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Scope.h; path = clang/Parse/Scope.h; sourceTree = "<group>"; };

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

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Decl.h Wed Jul 11 11:42:40 2007
@@ -205,7 +205,7 @@
 class TagDecl : public Decl {
   /// IsDefinition - True if this is a definition ("struct foo {};"), false if
   /// it is a declaration ("struct foo;").
-  bool IsDefinition;
+  bool IsDefinition : 1;
 protected:
   TagDecl(Kind DK, SourceLocation L, IdentifierInfo *Id) : Decl(DK, L, Id) {
     IsDefinition = false;
@@ -216,7 +216,6 @@
   bool isDefinition() const {
     return IsDefinition;
   }
-  void setDefinition(bool V) { IsDefinition = V; }
   
   const char *getKindName() const {
     switch (getKind()) {
@@ -234,6 +233,8 @@
            D->getKind() == Class || D->getKind() == Enum;
   }
   static bool classof(const ObjectDecl *D) { return true; }
+protected:
+  void setDefinition(bool V) { IsDefinition = V; }
 };
 
 /// RecordDecl - Represents a struct/union/class.
@@ -241,15 +242,26 @@
   /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
   /// array member (e.g. int X[]) or if this union contains a struct that does.
   /// If so, this cannot be contained in arrays or other structs as a member.
-  bool HasFlexibleArrayMember;
+  bool HasFlexibleArrayMember : 1;
+
+  /// Fields/NumFields - This is a new[]'d array of pointers to FieldDecls.
+  Decl **Fields;   // Null if not defined.
+  int NumFields;   // -1 if not defined.
 public:
   RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id) :TagDecl(DK, L, Id){
     HasFlexibleArrayMember = false;
     assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
+    Fields = 0;
+    NumFields = -1;
   }
   
   bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
   void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
+
+  /// defineBody - When created, RecordDecl's correspond to a forward declared
+  /// record.  This method is used to mark the decl as being defined, with the
+  /// specified contents.
+  void defineBody(Decl **fields, unsigned numFields);
   
   static bool classof(const Decl *D) {
     return D->getKind() == Struct || D->getKind() == Union ||





More information about the cfe-commits mailing list