[cfe-commits] r43587 - /cfe/trunk/CodeGen/CodeGenTypes.cpp

Devang Patel dpatel at apple.com
Wed Oct 31 16:17:19 PDT 2007


Author: dpatel
Date: Wed Oct 31 18:17:19 2007
New Revision: 43587

URL: http://llvm.org/viewvc/llvm-project?rev=43587&view=rev
Log:
Refactor code into a separate method.

Modified:
    cfe/trunk/CodeGen/CodeGenTypes.cpp

Modified: cfe/trunk/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenTypes.cpp?rev=43587&r1=43586&r2=43587&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/CodeGen/CodeGenTypes.cpp Wed Oct 31 18:17:19 2007
@@ -32,11 +32,17 @@
   ///    - Ignore packed structs
   class RecordOrganizer {
   public:
-    RecordOrganizer() : STy(NULL) {}
+    RecordOrganizer() : STy(NULL), FieldNo(0), Cursor(0) {}
     
     /// addField - Add new field.
     void addField(const FieldDecl *FD);
 
+    /// addLLVMField - Add llvm struct field that corresponds to llvm type Ty. Update
+    /// cursor and increment field count.
+    void addLLVMField(const llvm::Type *Ty, CodeGenTypes &CGT,
+                      const FieldDecl *FD = NULL);
+
+
     /// layoutStructFields - Do the actual work and lay out all fields. Create
     /// corresponding llvm struct type.  This should be invoked only after
     /// all fields are added.
@@ -57,7 +63,10 @@
     void clear();
   private:
     llvm::Type *STy;
+    unsigned FieldNo;
+    uint64_t Cursor;
     llvm::SmallVector<const FieldDecl *, 8> FieldDecls;
+    std::vector<const llvm::Type*> LLVMFields;
   };
 }
 
@@ -358,26 +367,35 @@
 void RecordOrganizer::layoutStructFields(CodeGenTypes &CGT,
                                          const RecordLayout &RL) {
   // FIXME : Use SmallVector
-  std::vector<const llvm::Type*> Fields;
-  unsigned FieldNo = 0;
   uint64_t Cursor = 0;
-
+  FieldNo = 0;
+  LLVMFields.clear();
   for (llvm::SmallVector<const FieldDecl *, 8>::iterator I = FieldDecls.begin(),
          E = FieldDecls.end(); I != E; ++I) {
     const FieldDecl *FD = *I;
     const llvm::Type *Ty = CGT.ConvertType(FD->getType());
 
     uint64_t Offset = RL.getFieldOffset(FieldNo);
-    unsigned align = CGT.getTargetData().getABITypeAlignment(Ty);
-    if (Cursor % align != 0)
+    unsigned AlignmentInBits = CGT.getTargetData().getABITypeAlignment(Ty) * 8;
+    if (Cursor % AlignmentInBits != 0)
       assert (Offset == Cursor && "FIXME Invalid struct layout");
-    
-    Cursor += CGT.getTargetData().getTypeSizeInBits(Ty);
 
-    Fields.push_back(Ty);
-    CGT.addFieldInfo(FD, FieldNo++);
+    addLLVMField(Ty, CGT, FD);
   }
-  STy = llvm::StructType::get(Fields);
+  STy = llvm::StructType::get(LLVMFields);
+}
+
+/// addLLVMField - Add llvm struct field that corresponds to llvm type Ty. Update
+/// cursor and increment field count. If field decl FD is available than update
+/// update field info at CodeGenTypes level.
+void RecordOrganizer::addLLVMField(const llvm::Type *Ty, 
+                                   CodeGenTypes &CGT,
+                                   const FieldDecl *FD) {
+  Cursor += CGT.getTargetData().getTypeSizeInBits(Ty);
+  LLVMFields.push_back(Ty);
+  if (FD)
+    CGT.addFieldInfo(FD, FieldNo);
+  ++FieldNo;
 }
 
 /// layoutUnionFields - Do the actual work and lay out all fields. Create





More information about the cfe-commits mailing list