From: Devang Patel Date: Wed, 31 Oct 2007 23:17:19 +0000 (+0000) Subject: Refactor code into a separate method. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0bd41f2cea0639395e8ab7ef8fd6df2c1999b6ae;p=clang Refactor code into a separate method. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43587 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/CodeGen/CodeGenTypes.cpp b/CodeGen/CodeGenTypes.cpp index 7cd2ece34d..6d283f0e45 100644 --- a/CodeGen/CodeGenTypes.cpp +++ b/CodeGen/CodeGenTypes.cpp @@ -32,11 +32,17 @@ namespace { /// - 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 @@ namespace { void clear(); private: llvm::Type *STy; + unsigned FieldNo; + uint64_t Cursor; llvm::SmallVector FieldDecls; + std::vector LLVMFields; }; } @@ -358,26 +367,35 @@ void RecordOrganizer::addField(const FieldDecl *FD) { void RecordOrganizer::layoutStructFields(CodeGenTypes &CGT, const RecordLayout &RL) { // FIXME : Use SmallVector - std::vector Fields; - unsigned FieldNo = 0; uint64_t Cursor = 0; - + FieldNo = 0; + LLVMFields.clear(); for (llvm::SmallVector::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