Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 23ee4b7 | 2010-03-31 00:11:27 +0000 | [diff] [blame] | 15 | namespace llvm { |
| 16 | class Type; |
| 17 | } |
| 18 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 19 | namespace clang { |
| 20 | namespace CodeGen { |
| 21 | |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame^] | 22 | class CGBitFieldInfo { |
| 23 | public: |
| 24 | CGBitFieldInfo(unsigned FieldNo, unsigned Start, unsigned Size) |
| 25 | : FieldNo(FieldNo), Start(Start), Size(Size) {} |
| 26 | |
| 27 | unsigned FieldNo; |
| 28 | unsigned Start; |
| 29 | unsigned Size; |
| 30 | }; |
| 31 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 32 | /// CGRecordLayout - This class handles struct and union layout info while |
| 33 | /// lowering AST types to LLVM types. |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// These layout objects are only created on demand as IR generation requires. |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 36 | class CGRecordLayout { |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 37 | friend class CodeGenTypes; |
| 38 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 39 | CGRecordLayout(const CGRecordLayout&); // DO NOT IMPLEMENT |
| 40 | void operator=(const CGRecordLayout&); // DO NOT IMPLEMENT |
| 41 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 42 | private: |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 43 | /// The LLVMType corresponding to this record layout. |
| 44 | const llvm::Type *LLVMType; |
| 45 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 46 | /// Map from (non-bit-field) struct field to the corresponding llvm struct |
| 47 | /// type field no. This info is populated by record builder. |
| 48 | llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; |
| 49 | |
| 50 | /// Map from (bit-field) struct field to the corresponding llvm struct type |
| 51 | /// field no. This info is populated by record builder. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame^] | 52 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields; |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 53 | |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 54 | /// Whether one of the fields in this record layout is a pointer to data |
| 55 | /// member, or a struct that contains pointer to data member. |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 56 | bool ContainsPointerToDataMember : 1; |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 57 | |
| 58 | public: |
| 59 | CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember) |
| 60 | : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) {} |
| 61 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 62 | /// \brief Return the LLVM type associated with this record. |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 63 | const llvm::Type *getLLVMType() const { |
| 64 | return LLVMType; |
| 65 | } |
| 66 | |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 67 | /// \brief Check whether this struct contains pointers to data members. |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 68 | bool containsPointerToDataMember() const { |
| 69 | return ContainsPointerToDataMember; |
| 70 | } |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 71 | |
| 72 | /// \brief Return the BitFieldInfo that corresponds to the field FD. |
| 73 | unsigned getLLVMFieldNo(const FieldDecl *FD) const { |
| 74 | assert(!FD->isBitField() && "Invalid call for bit-field decl!"); |
| 75 | assert(FieldInfo.count(FD) && "Invalid field for record!"); |
| 76 | return FieldInfo.lookup(FD); |
| 77 | } |
| 78 | |
| 79 | /// \brief Return llvm::StructType element number that corresponds to the |
| 80 | /// field FD. |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame^] | 81 | const CGBitFieldInfo &getBitFieldInfo(const FieldDecl *FD) const { |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 82 | assert(FD->isBitField() && "Invalid call for non bit-field decl!"); |
Daniel Dunbar | cd3d5e7 | 2010-04-05 16:20:44 +0000 | [diff] [blame^] | 83 | llvm::DenseMap<const FieldDecl *, CGBitFieldInfo>::const_iterator |
Daniel Dunbar | 034299e | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 84 | it = BitFields.find(FD); |
| 85 | assert(it != BitFields.end() && "Unable to find bitfield info"); |
| 86 | return it->second; |
| 87 | } |
Daniel Dunbar | 072d0bb | 2010-03-30 22:26:10 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // end namespace CodeGen |
| 91 | } // end namespace clang |
| 92 | |
| 93 | #endif |