CGRecordLayout.h revision 206084
1//===--- CGRecordLayout.h - LLVM Record Layout Information ------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef CLANG_CODEGEN_CGRECORDLAYOUT_H 11#define CLANG_CODEGEN_CGRECORDLAYOUT_H 12 13#include "llvm/ADT/DenseMap.h" 14#include "clang/AST/Decl.h" 15namespace llvm { 16 class Type; 17} 18 19namespace clang { 20namespace CodeGen { 21 22/// CGRecordLayout - This class handles struct and union layout info while 23/// lowering AST types to LLVM types. 24/// 25/// These layout objects are only created on demand as IR generation requires. 26class CGRecordLayout { 27 friend class CodeGenTypes; 28 29 CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT 30 void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT 31 32public: 33 struct BitFieldInfo { 34 BitFieldInfo(unsigned FieldNo, 35 unsigned Start, 36 unsigned Size) 37 : FieldNo(FieldNo), Start(Start), Size(Size) {} 38 39 unsigned FieldNo; 40 unsigned Start; 41 unsigned Size; 42 }; 43 44private: 45 /// The LLVMType corresponding to this record layout. 46 const llvm::Type *LLVMType; 47 48 /// Map from (non-bit-field) struct field to the corresponding llvm struct 49 /// type field no. This info is populated by record builder. 50 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; 51 52 /// Map from (bit-field) struct field to the corresponding llvm struct type 53 /// field no. This info is populated by record builder. 54 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields; 55 56 /// Whether one of the fields in this record layout is a pointer to data 57 /// member, or a struct that contains pointer to data member. 58 bool ContainsPointerToDataMember : 1; 59 60public: 61 CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember) 62 : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) {} 63 64 /// \brief Return the LLVM type associated with this record. 65 const llvm::Type *getLLVMType() const { 66 return LLVMType; 67 } 68 69 /// \brief Check whether this struct contains pointers to data members. 70 bool containsPointerToDataMember() const { 71 return ContainsPointerToDataMember; 72 } 73 74 /// \brief Return the BitFieldInfo that corresponds to the field FD. 75 unsigned getLLVMFieldNo(const FieldDecl *FD) const { 76 assert(!FD->isBitField() && "Invalid call for bit-field decl!"); 77 assert(FieldInfo.count(FD) && "Invalid field for record!"); 78 return FieldInfo.lookup(FD); 79 } 80 81 /// \brief Return llvm::StructType element number that corresponds to the 82 /// field FD. 83 const BitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const { 84 assert(FD->isBitField() && "Invalid call for non bit-field decl!"); 85 llvm::DenseMap<const FieldDecl *, BitFieldInfo>::const_iterator 86 it = BitFields.find(FD); 87 assert(it != BitFields.end() && "Unable to find bitfield info"); 88 return it->second; 89 } 90}; 91 92} // end namespace CodeGen 93} // end namespace clang 94 95#endif 96