Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 1 | //===--- CGRecordLayoutBuilder.h - Record builder helper --------*- 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 | // This is a helper class used to build CGRecordLayout objects and LLVM types. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef CLANG_CODEGEN_CGRECORDLAYOUTBUILDER_H |
| 15 | #define CLANG_CODEGEN_CGRECORDLAYOUTBUILDER_H |
| 16 | |
| 17 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | a3f084c | 2009-10-26 01:37:10 +0000 | [diff] [blame] | 18 | #include "llvm/System/DataTypes.h" |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | namespace llvm { |
| 22 | class Type; |
| 23 | } |
| 24 | |
| 25 | namespace clang { |
Anders Carlsson | d681a29 | 2009-12-16 17:27:20 +0000 | [diff] [blame] | 26 | class ASTRecordLayout; |
| 27 | class CXXRecordDecl; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 28 | class FieldDecl; |
| 29 | class RecordDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 31 | namespace CodeGen { |
| 32 | class CGRecordLayout; |
| 33 | class CodeGenTypes; |
| 34 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | class CGRecordLayoutBuilder { |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 36 | CodeGenTypes &Types; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 38 | /// Packed - Whether the resulting LLVM struct will be packed or not. |
| 39 | bool Packed; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 40 | |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 41 | /// ContainsMemberPointer - Whether one of the fields is a member pointer |
| 42 | /// or is a struct that contains a member pointer. |
| 43 | bool ContainsMemberPointer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Anders Carlsson | 28a5fa2 | 2009-08-08 19:38:24 +0000 | [diff] [blame] | 45 | /// Alignment - Contains the alignment of the RecordDecl. |
| 46 | unsigned Alignment; |
| 47 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 48 | /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the |
| 49 | /// LLVM types. |
| 50 | unsigned AlignmentAsLLVMStruct; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 52 | /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field, |
| 53 | /// this will have the number of bits still available in the field. |
| 54 | char BitsAvailableInLastField; |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 55 | |
| 56 | /// NextFieldOffsetInBytes - Holds the next field offset in bytes. |
| 57 | uint64_t NextFieldOffsetInBytes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 59 | /// FieldTypes - Holds the LLVM types that the struct is created from. |
| 60 | std::vector<const llvm::Type *> FieldTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 62 | /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number. |
| 63 | typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo; |
| 64 | llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields; |
| 65 | |
| 66 | /// LLVMBitFieldInfo - Holds location and size information about a bit field. |
| 67 | struct LLVMBitFieldInfo { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | LLVMBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, unsigned Start, |
Anders Carlsson | 8af896c | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 69 | unsigned Size) |
| 70 | : FD(FD), FieldNo(FieldNo), Start(Start), Size(Size) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 72 | const FieldDecl *FD; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Anders Carlsson | 8af896c | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 74 | unsigned FieldNo; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 75 | unsigned Start; |
| 76 | unsigned Size; |
| 77 | }; |
| 78 | llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
| 80 | CGRecordLayoutBuilder(CodeGenTypes &Types) |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 81 | : Types(Types), Packed(false), ContainsMemberPointer(false) |
| 82 | , Alignment(0), AlignmentAsLLVMStruct(1) |
Anders Carlsson | d5d6413 | 2009-07-28 17:56:36 +0000 | [diff] [blame] | 83 | , BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { } |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 84 | |
| 85 | /// Layout - Will layout a RecordDecl. |
| 86 | void Layout(const RecordDecl *D); |
| 87 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 88 | /// LayoutUnion - Will layout a union RecordDecl. |
| 89 | void LayoutUnion(const RecordDecl *D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 91 | /// LayoutField - try to layout all fields in the record decl. |
| 92 | /// Returns false if the operation failed because the struct is not packed. |
| 93 | bool LayoutFields(const RecordDecl *D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Anders Carlsson | d681a29 | 2009-12-16 17:27:20 +0000 | [diff] [blame] | 95 | /// LayoutBases - layout the bases and vtable pointer of a record decl. |
| 96 | void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout); |
| 97 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 98 | /// LayoutField - layout a single field. Returns false if the operation failed |
| 99 | /// because the current struct is not packed. |
| 100 | bool LayoutField(const FieldDecl *D, uint64_t FieldOffset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 102 | /// LayoutBitField - layout a single bit field. |
| 103 | void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset); |
| 104 | |
Anders Carlsson | 6e853bf | 2009-07-24 02:45:50 +0000 | [diff] [blame] | 105 | /// AppendField - Appends a field with the given offset and type. |
| 106 | void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 107 | |
| 108 | /// AppendPadding - Appends enough padding bytes so that the total struct |
| 109 | /// size matches the alignment of the passed in type. |
| 110 | void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy); |
| 111 | |
| 112 | /// AppendPadding - Appends enough padding bytes so that the total |
| 113 | /// struct size is a multiple of the field alignment. |
| 114 | void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 116 | /// AppendBytes - Append a given number of bytes to the record. |
| 117 | void AppendBytes(uint64_t NumBytes); |
| 118 | |
Anders Carlsson | b97a3ec | 2009-07-27 14:55:54 +0000 | [diff] [blame] | 119 | /// AppendTailPadding - Append enough tail padding so that the type will have |
| 120 | /// the passed size. |
| 121 | void AppendTailPadding(uint64_t RecordSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 123 | unsigned getTypeAlignment(const llvm::Type *Ty) const; |
| 124 | uint64_t getTypeSizeInBytes(const llvm::Type *Ty) const; |
| 125 | |
Anders Carlsson | d606de7 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 126 | /// CheckForMemberPointer - Check if the field contains a member pointer. |
| 127 | void CheckForMemberPointer(const FieldDecl *FD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 129 | public: |
| 130 | /// ComputeLayout - Return the right record layout for a given record decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | static CGRecordLayout *ComputeLayout(CodeGenTypes &Types, |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 132 | const RecordDecl *D); |
| 133 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 135 | } // end namespace CodeGen |
| 136 | } // end namespace clang |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 137 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
| 139 | #endif |