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 | |
Chris Lattner | ef52a2f | 2008-02-29 17:10:38 +0000 | [diff] [blame] | 14 | #ifndef CLANG_CODEGEN_CODEGENTYPES_H |
| 15 | #define CLANG_CODEGEN_CODEGENTYPES_H |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Devang Patel | eae1560 | 2008-02-05 02:39:50 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 22 | #include "CGCall.h" |
| 23 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | namespace llvm { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 25 | class FunctionType; |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 26 | class Module; |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 27 | class OpaqueType; |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 28 | class PATypeHolder; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 29 | class TargetData; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 30 | class Type; |
Benjamin Kramer | f21efe9 | 2009-08-11 17:46:57 +0000 | [diff] [blame] | 31 | class LLVMContext; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | namespace clang { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 35 | class ABIInfo; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 36 | class ASTContext; |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 37 | class CXXMethodDecl; |
Devang Patel | b1e3989 | 2007-10-23 23:26:46 +0000 | [diff] [blame] | 38 | class FieldDecl; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 39 | class FunctionProtoType; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 40 | class ObjCInterfaceDecl; |
| 41 | class ObjCIvarDecl; |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 42 | class PointerType; |
| 43 | class QualType; |
| 44 | class RecordDecl; |
| 45 | class TagDecl; |
| 46 | class TargetInfo; |
| 47 | class Type; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 48 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | namespace CodeGen { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 50 | class CodeGenTypes; |
| 51 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 52 | /// CGRecordLayout - This class handles struct and union layout info while |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 53 | /// lowering AST types to LLVM types. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 54 | class CGRecordLayout { |
| 55 | CGRecordLayout(); // DO NOT IMPLEMENT |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 56 | |
| 57 | /// LLVMType - The LLVMType corresponding to this record layout. |
| 58 | const llvm::Type *LLVMType; |
| 59 | |
| 60 | /// ContainsMemberPointer - Whether one of the fields in this record layout |
| 61 | /// is a member pointer, or a struct that contains a member pointer. |
| 62 | bool ContainsMemberPointer; |
| 63 | |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 64 | public: |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 65 | CGRecordLayout(const llvm::Type *T, bool ContainsMemberPointer) |
| 66 | : LLVMType(T), ContainsMemberPointer(ContainsMemberPointer) { } |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 67 | |
| 68 | /// getLLVMType - Return llvm type associated with this record. |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 69 | const llvm::Type *getLLVMType() const { |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 70 | return LLVMType; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Anders Carlsson | fc3eaa4 | 2009-08-23 01:25:01 +0000 | [diff] [blame] | 73 | bool containsMemberPointer() const { |
| 74 | return ContainsMemberPointer; |
| 75 | } |
| 76 | |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 77 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | |
| 79 | /// CodeGenTypes - This class organizes the cross-module state that is used |
| 80 | /// while lowering AST types to LLVM types. |
| 81 | class CodeGenTypes { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 82 | ASTContext &Context; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | TargetInfo &Target; |
Anders Carlsson | 4e53328 | 2007-08-17 22:00:32 +0000 | [diff] [blame] | 84 | llvm::Module& TheModule; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 85 | const llvm::TargetData& TheTargetData; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 86 | mutable const ABIInfo* TheABIInfo; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | 6aeae7f | 2009-02-26 19:48:14 +0000 | [diff] [blame] | 88 | llvm::SmallVector<std::pair<QualType, |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 89 | llvm::OpaqueType *>, 8> PointersToResolve; |
| 90 | |
Daniel Dunbar | efb6d0d | 2008-09-06 02:26:43 +0000 | [diff] [blame] | 91 | llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 92 | |
Eli Friedman | b3b6b9b | 2009-03-05 03:16:41 +0000 | [diff] [blame] | 93 | llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes; |
| 94 | |
Daniel Dunbar | 412f59b | 2009-04-22 10:28:39 +0000 | [diff] [blame] | 95 | /// The opaque type map for Objective-C interfaces. All direct |
| 96 | /// manipulation is done by the runtime interfaces, which are |
| 97 | /// responsible for coercing to the appropriate type; these opaque |
| 98 | /// types are never refined. |
| 99 | llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes; |
| 100 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 101 | /// CGRecordLayouts - This maps llvm struct type with corresponding |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 102 | /// record layout info. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 103 | /// FIXME : If CGRecordLayout is less than 16 bytes then use |
Devang Patel | 71bcb09 | 2007-10-24 00:32:16 +0000 | [diff] [blame] | 104 | /// inline it in the map. |
Daniel Dunbar | efb6d0d | 2008-09-06 02:26:43 +0000 | [diff] [blame] | 105 | llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 106 | |
| 107 | /// FieldInfo - This maps struct field with corresponding llvm struct type |
| 108 | /// field no. This info is populated by record organizer. |
| 109 | llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; |
| 110 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 111 | /// FunctionInfos - Hold memoized CGFunctionInfo results. |
| 112 | llvm::FoldingSet<CGFunctionInfo> FunctionInfos; |
| 113 | |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 114 | public: |
Anders Carlsson | 8330cee | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 115 | struct BitFieldInfo { |
| 116 | BitFieldInfo(unsigned FieldNo, |
| 117 | unsigned Start, |
| 118 | unsigned Size) |
| 119 | : FieldNo(FieldNo), Start(Start), Size(Size) {} |
Lauro Ramos Venancio | 2c46ce8 | 2008-01-21 22:54:57 +0000 | [diff] [blame] | 120 | |
Anders Carlsson | 8330cee | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 121 | unsigned FieldNo; |
| 122 | unsigned Start; |
| 123 | unsigned Size; |
Devang Patel | 159e330 | 2007-11-07 01:57:13 +0000 | [diff] [blame] | 124 | }; |
Lauro Ramos Venancio | 3b8c22d | 2008-01-22 20:17:04 +0000 | [diff] [blame] | 125 | |
| 126 | private: |
Devang Patel | 159e330 | 2007-11-07 01:57:13 +0000 | [diff] [blame] | 127 | llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields; |
| 128 | |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 129 | /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder) |
Devang Patel | 30ec997 | 2007-10-25 18:32:36 +0000 | [diff] [blame] | 130 | /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is |
| 131 | /// used instead of llvm::Type because it allows us to bypass potential |
| 132 | /// dangling type pointers due to type refinement on llvm side. |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 133 | llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache; |
Devang Patel | 5825ac2 | 2007-10-25 21:40:12 +0000 | [diff] [blame] | 134 | |
| 135 | /// ConvertNewType - Convert type T into a llvm::Type. Do not use this |
| 136 | /// method directly because it does not do any type caching. This method |
| 137 | /// is available only for ConvertType(). CovertType() is preferred |
| 138 | /// interface to convert type T into a llvm::Type. |
| 139 | const llvm::Type *ConvertNewType(QualType T); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | public: |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 141 | CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 142 | ~CodeGenTypes(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | |
Devang Patel | d9e9ede | 2007-10-31 20:08:22 +0000 | [diff] [blame] | 144 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | TargetInfo &getTarget() const { return Target; } |
Devang Patel | 86522b9 | 2007-10-29 20:50:19 +0000 | [diff] [blame] | 146 | ASTContext &getContext() const { return Context; } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 147 | const ABIInfo &getABIInfo() const; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 148 | llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } |
Devang Patel | 5825ac2 | 2007-10-25 21:40:12 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 150 | /// ConvertType - Convert type T into a llvm::Type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | const llvm::Type *ConvertType(QualType T); |
Chris Lattner | fce71b8 | 2008-04-03 05:50:42 +0000 | [diff] [blame] | 152 | const llvm::Type *ConvertTypeRecursive(QualType T); |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 4581fff | 2008-02-06 05:21:55 +0000 | [diff] [blame] | 154 | /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from |
| 155 | /// ConvertType in that it is used to convert to the memory representation for |
| 156 | /// a type. For example, the scalar representation for _Bool is i1, but the |
| 157 | /// memory representation is usually i8 or i32, depending on the target. |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 158 | const llvm::Type *ConvertTypeForMem(QualType T); |
Eli Friedman | 57a84fb | 2009-03-03 04:48:01 +0000 | [diff] [blame] | 159 | const llvm::Type *ConvertTypeForMemRecursive(QualType T); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 160 | |
Daniel Dunbar | 36b5f5e | 2009-01-31 03:05:44 +0000 | [diff] [blame] | 161 | /// GetFunctionType - Get the LLVM function type for \arg Info. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 162 | const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, |
| 163 | bool IsVariadic); |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 164 | |
Anders Carlsson | ad3e711 | 2009-08-24 17:16:23 +0000 | [diff] [blame] | 165 | const CGRecordLayout &getCGRecordLayout(const TagDecl*) const; |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 166 | |
| 167 | /// getLLVMFieldNo - Return llvm::StructType element number |
| 168 | /// that corresponds to the field FD. |
| 169 | unsigned getLLVMFieldNo(const FieldDecl *FD); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 170 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 171 | /// UpdateCompletedType - When we find the full definition for a TagDecl, |
| 172 | /// replace the 'opaque' type we previously made for it if applicable. |
| 173 | void UpdateCompletedType(const TagDecl *TD); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 174 | |
| 175 | /// getFunctionInfo - Get the CGFunctionInfo for this function signature. |
| 176 | const CGFunctionInfo &getFunctionInfo(QualType RetTy, |
| 177 | const llvm::SmallVector<QualType,16> |
| 178 | &ArgTys); |
| 179 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 180 | const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP); |
| 181 | const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 182 | const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 183 | const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 184 | const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); |
| 185 | const CGFunctionInfo &getFunctionInfo(QualType ResTy, |
| 186 | const CallArgList &Args); |
Anders Carlsson | 5529b24 | 2009-04-06 18:05:26 +0000 | [diff] [blame] | 187 | public: |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 188 | const CGFunctionInfo &getFunctionInfo(QualType ResTy, |
| 189 | const FunctionArgList &Args); |
Chris Lattner | d86e6bc | 2008-02-05 08:06:13 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 19009e6 | 2008-01-09 18:47:25 +0000 | [diff] [blame] | 191 | public: // These are internal details of CGT that shouldn't be used externally. |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 192 | /// addFieldInfo - Assign field number to field FD. |
Anders Carlsson | 8330cee | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 193 | void addFieldInfo(const FieldDecl *FD, unsigned FieldNo); |
Lauro Ramos Venancio | 2c46ce8 | 2008-01-21 22:54:57 +0000 | [diff] [blame] | 194 | |
| 195 | /// addBitFieldInfo - Assign a start bit and a size to field FD. |
Anders Carlsson | 8330cee | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 196 | void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, |
| 197 | unsigned Start, unsigned Size); |
Lauro Ramos Venancio | 2c46ce8 | 2008-01-21 22:54:57 +0000 | [diff] [blame] | 198 | |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 199 | /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field |
| 200 | /// FD. |
Lauro Ramos Venancio | 2c46ce8 | 2008-01-21 22:54:57 +0000 | [diff] [blame] | 201 | BitFieldInfo getBitFieldInfo(const FieldDecl *FD); |
Chris Lattner | fc3b8e9 | 2008-02-06 05:18:32 +0000 | [diff] [blame] | 202 | |
| 203 | /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or |
| 204 | /// enum. |
Chris Lattner | 8fb1dd0 | 2008-02-06 06:06:49 +0000 | [diff] [blame] | 205 | const llvm::Type *ConvertTagDeclType(const TagDecl *TD); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 206 | |
| 207 | /// GetExpandedTypes - Expand the type \arg Ty into the LLVM |
| 208 | /// argument types it would be passed as on the provided vector \arg |
| 209 | /// ArgTys. See ABIArgInfo::Expand. |
| 210 | void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 211 | }; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 212 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | } // end namespace CodeGen |
| 214 | } // end namespace clang |
| 215 | |
| 216 | #endif |