Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is the code that handles AST -> LLVM type lowering. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef CODEGEN_CODEGENTYPES_H |
| 15 | #define CODEGEN_CODEGENTYPES_H |
| 16 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | namespace llvm { |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 22 | class Module; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | class Type; |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 24 | class PATypeHolder; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 25 | class TargetData; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | namespace clang { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 29 | class ASTContext; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 30 | class TagDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | class TargetInfo; |
| 32 | class QualType; |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 33 | class Type; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | class FunctionTypeProto; |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 35 | class FieldDecl; |
| 36 | class RecordDecl; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 37 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | namespace CodeGen { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 39 | class CodeGenTypes; |
| 40 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 41 | /// CGRecordLayout - This class handles struct and union layout info while |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 42 | /// lowering AST types to LLVM types. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 43 | class CGRecordLayout { |
| 44 | CGRecordLayout(); // DO NOT IMPLEMENT |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 45 | public: |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 46 | CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF) |
| 47 | : STy(T), PaddingFields(PF) { |
Devang Patel | 057afdd | 2007-10-24 20:38:06 +0000 | [diff] [blame] | 48 | // FIXME : Collect info about fields that requires adjustments |
| 49 | // (i.e. fields that do not directly map to llvm struct fields.) |
| 50 | } |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 51 | |
| 52 | /// getLLVMType - Return llvm type associated with this record. |
Devang Patel | c4c429a | 2007-10-24 00:56:23 +0000 | [diff] [blame] | 53 | llvm::Type *getLLVMType() const { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 54 | return STy; |
| 55 | } |
| 56 | |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 57 | bool isPaddingField(unsigned No) const { |
| 58 | return PaddingFields.count(No) != 0; |
| 59 | } |
| 60 | |
| 61 | unsigned getNumPaddingFields() { |
| 62 | return PaddingFields.size(); |
| 63 | } |
| 64 | |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 65 | private: |
| 66 | llvm::Type *STy; |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 67 | llvm::SmallSet<unsigned, 8> PaddingFields; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 68 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | |
| 70 | /// CodeGenTypes - This class organizes the cross-module state that is used |
| 71 | /// while lowering AST types to LLVM types. |
| 72 | class CodeGenTypes { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 73 | ASTContext &Context; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | TargetInfo &Target; |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 75 | llvm::Module& TheModule; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 76 | const llvm::TargetData& TheTargetData; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 77 | |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 78 | llvm::DenseMap<const TagDecl*, llvm::PATypeHolder> TagDeclTypes; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 79 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 80 | /// CGRecordLayouts - This maps llvm struct type with corresponding |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 81 | /// record layout info. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 82 | /// FIXME : If CGRecordLayout is less than 16 bytes then use |
Devang Patel | 71bcb09 | 2007-10-24 00:32:16 +0000 | [diff] [blame] | 83 | /// inline it in the map. |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 84 | llvm::DenseMap<const TagDecl*, CGRecordLayout *> CGRecordLayouts; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 85 | |
| 86 | /// FieldInfo - This maps struct field with corresponding llvm struct type |
| 87 | /// field no. This info is populated by record organizer. |
| 88 | llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; |
| 89 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 90 | public: |
Devang Patel | 159e330 | 2007-11-07 01:57:13 +0000 | [diff] [blame] | 91 | class BitFieldInfo { |
| 92 | public: |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 93 | explicit BitFieldInfo(unsigned short B, unsigned short S) |
Lauro Ramos Venancio | 2c46ce8 | 2008-01-21 22:54:57 +0000 | [diff] [blame] | 94 | : Begin(B), Size(S) {} |
| 95 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 96 | unsigned short Begin; |
| 97 | unsigned short Size; |
Devang Patel | 159e330 | 2007-11-07 01:57:13 +0000 | [diff] [blame] | 98 | }; |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 99 | |
| 100 | private: |
Devang Patel | 159e330 | 2007-11-07 01:57:13 +0000 | [diff] [blame] | 101 | llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields; |
| 102 | |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 103 | /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder) |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 104 | /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is |
| 105 | /// used instead of llvm::Type because it allows us to bypass potential |
| 106 | /// dangling type pointers due to type refinement on llvm side. |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 107 | llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache; |
Devang Patel | 5825ac2 | 2007-10-25 21:40:12 +0000 | [diff] [blame] | 108 | |
| 109 | /// ConvertNewType - Convert type T into a llvm::Type. Do not use this |
| 110 | /// method directly because it does not do any type caching. This method |
| 111 | /// is available only for ConvertType(). CovertType() is preferred |
| 112 | /// interface to convert type T into a llvm::Type. |
| 113 | const llvm::Type *ConvertNewType(QualType T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | public: |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 115 | CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 116 | ~CodeGenTypes(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | |
Devang Patel | d9e9ede | 2007-10-31 20:08:22 +0000 | [diff] [blame] | 118 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | TargetInfo &getTarget() const { return Target; } |
Devang Patel | 86522b9 | 2007-10-29 20:50:19 +0000 | [diff] [blame] | 120 | ASTContext &getContext() const { return Context; } |
Devang Patel | 5825ac2 | 2007-10-25 21:40:12 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 122 | /// ConvertType - Convert type T into a llvm::Type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | const llvm::Type *ConvertType(QualType T); |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 125 | /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from |
| 126 | /// ConvertType in that it is used to convert to the memory representation for |
| 127 | /// a type. For example, the scalar representation for _Bool is i1, but the |
| 128 | /// memory representation is usually i8 or i32, depending on the target. |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 129 | const llvm::Type *ConvertTypeForMem(QualType T); |
| 130 | |
| 131 | |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 132 | const CGRecordLayout *getCGRecordLayout(const TagDecl*) const; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 133 | |
| 134 | /// getLLVMFieldNo - Return llvm::StructType element number |
| 135 | /// that corresponds to the field FD. |
| 136 | unsigned getLLVMFieldNo(const FieldDecl *FD); |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 137 | |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 138 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 139 | /// UpdateCompletedType - When we find the full definition for a TagDecl, |
| 140 | /// replace the 'opaque' type we previously made for it if applicable. |
| 141 | void UpdateCompletedType(const TagDecl *TD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 143 | public: // These are internal details of CGT that shouldn't be used externally. |
| 144 | void DecodeArgumentTypes(const FunctionTypeProto &FTP, |
| 145 | std::vector<const llvm::Type*> &ArgTys); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 146 | |
| 147 | /// addFieldInfo - Assign field number to field FD. |
Lauro Ramos Venancio | 2c46ce8 | 2008-01-21 22:54:57 +0000 | [diff] [blame] | 148 | void addFieldInfo(const FieldDecl *FD, unsigned No); |
| 149 | |
| 150 | /// addBitFieldInfo - Assign a start bit and a size to field FD. |
| 151 | void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size); |
| 152 | |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 153 | /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field |
| 154 | /// FD. |
Lauro Ramos Venancio | 2c46ce8 | 2008-01-21 22:54:57 +0000 | [diff] [blame] | 155 | BitFieldInfo getBitFieldInfo(const FieldDecl *FD); |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 156 | |
| 157 | /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or |
| 158 | /// enum. |
| 159 | const llvm::Type *ConvertTagDeclType(QualType T, const TagDecl *TD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | }; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 161 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } // end namespace CodeGen |
| 163 | } // end namespace clang |
| 164 | |
| 165 | #endif |