1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- 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 the code that handles AST -> LLVM type lowering. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef CLANG_CODEGEN_CODEGENTYPES_H 15 #define CLANG_CODEGEN_CODEGENTYPES_H 16 17 #include "llvm/Module.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/SmallSet.h" 20 #include <vector> 21 22 #include "CGCall.h" 23 #include "CGCXX.h" 24 25 namespace llvm { 26 class FunctionType; 27 class Module; 28 class OpaqueType; 29 class PATypeHolder; 30 class TargetData; 31 class Type; 32 class LLVMContext; 33 } 34 35 namespace clang { 36 class ABIInfo; 37 class ASTContext; 38 class CXXConstructorDecl; 39 class CXXDestructorDecl; 40 class CXXMethodDecl; 41 class FieldDecl; 42 class FunctionProtoType; 43 class ObjCInterfaceDecl; 44 class ObjCIvarDecl; 45 class PointerType; 46 class QualType; 47 class RecordDecl; 48 class TagDecl; 49 class TargetInfo; 50 class Type; 51 52 namespace CodeGen { 53 class CodeGenTypes; 54 55 /// CGRecordLayout - This class handles struct and union layout info while 56 /// lowering AST types to LLVM types. 57 class CGRecordLayout { 58 CGRecordLayout(); // DO NOT IMPLEMENT 59 60 /// LLVMType - The LLVMType corresponding to this record layout. 61 const llvm::Type *LLVMType; 62 63 /// ContainsMemberPointer - Whether one of the fields in this record layout 64 /// is a member pointer, or a struct that contains a member pointer. 65 bool ContainsMemberPointer; 66 67 public: 68 CGRecordLayout(const llvm::Type *T, bool ContainsMemberPointer) 69 : LLVMType(T), ContainsMemberPointer(ContainsMemberPointer) { } 70 71 /// getLLVMType - Return llvm type associated with this record. 72 const llvm::Type *getLLVMType() const { 73 return LLVMType; 74 } 75 76 bool containsMemberPointer() const { 77 return ContainsMemberPointer; 78 } 79 }; 80 81 /// CodeGenTypes - This class organizes the cross-module state that is used 82 /// while lowering AST types to LLVM types. 83 class CodeGenTypes { 84 ASTContext &Context; 85 const TargetInfo &Target; 86 llvm::Module& TheModule; 87 const llvm::TargetData& TheTargetData; 88 mutable const ABIInfo* TheABIInfo; 89 90 llvm::SmallVector<std::pair<QualType, 91 llvm::OpaqueType *>, 8> PointersToResolve; 92 93 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes; 94 95 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes; 96 97 /// The opaque type map for Objective-C interfaces. All direct 98 /// manipulation is done by the runtime interfaces, which are 99 /// responsible for coercing to the appropriate type; these opaque 100 /// types are never refined. 101 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes; 102 103 /// CGRecordLayouts - This maps llvm struct type with corresponding 104 /// record layout info. 105 /// FIXME : If CGRecordLayout is less than 16 bytes then use 106 /// inline it in the map. 107 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 108 109 /// FieldInfo - This maps struct field with corresponding llvm struct type 110 /// field no. This info is populated by record organizer. 111 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo; 112 113 /// FunctionInfos - Hold memoized CGFunctionInfo results. 114 llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 115 116 public: 117 struct BitFieldInfo { 118 BitFieldInfo(unsigned FieldNo, 119 unsigned Start, 120 unsigned Size) 121 : FieldNo(FieldNo), Start(Start), Size(Size) {} 122 123 unsigned FieldNo; 124 unsigned Start; 125 unsigned Size; 126 }; 127 128 private: 129 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields; 130 131 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder) 132 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is 133 /// used instead of llvm::Type because it allows us to bypass potential 134 /// dangling type pointers due to type refinement on llvm side. 135 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache; 136 137 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this 138 /// method directly because it does not do any type caching. This method 139 /// is available only for ConvertType(). CovertType() is preferred 140 /// interface to convert type T into a llvm::Type. 141 const llvm::Type *ConvertNewType(QualType T); 142 public: 143 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD); 144 ~CodeGenTypes(); 145 146 const llvm::TargetData &getTargetData() const { return TheTargetData; } 147 const TargetInfo &getTarget() const { return Target; } 148 ASTContext &getContext() const { return Context; } 149 const ABIInfo &getABIInfo() const; 150 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 151 152 /// ConvertType - Convert type T into a llvm::Type. 153 const llvm::Type *ConvertType(QualType T); 154 const llvm::Type *ConvertTypeRecursive(QualType T); 155 156 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 157 /// ConvertType in that it is used to convert to the memory representation for 158 /// a type. For example, the scalar representation for _Bool is i1, but the 159 /// memory representation is usually i8 or i32, depending on the target. 160 const llvm::Type *ConvertTypeForMem(QualType T); 161 const llvm::Type *ConvertTypeForMemRecursive(QualType T); 162 163 /// GetFunctionType - Get the LLVM function type for \arg Info. 164 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 165 bool IsVariadic); 166 167 168 /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable, 169 /// given a CXXMethodDecl. If the method to has an incomplete return type, 170 /// and/or incomplete argument types, this will return the opaque type. 171 const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD); 172 173 const CGRecordLayout &getCGRecordLayout(const TagDecl*) const; 174 175 /// getLLVMFieldNo - Return llvm::StructType element number 176 /// that corresponds to the field FD. 177 unsigned getLLVMFieldNo(const FieldDecl *FD); 178 179 /// UpdateCompletedType - When we find the full definition for a TagDecl, 180 /// replace the 'opaque' type we previously made for it if applicable. 181 void UpdateCompletedType(const TagDecl *TD); 182 183 private: 184 const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP); 185 const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP); 186 187 public: 188 /// getFunctionInfo - Get the function info for the specified function decl. 189 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 190 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 191 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 192 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 193 CXXCtorType Type); 194 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 195 CXXDtorType Type); 196 197 // getFunctionInfo - Get the function info for a member function. 198 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 199 const FunctionProtoType *FTP); 200 201 /// getFunctionInfo - Get the function info for a function described by a 202 /// return type and argument types. If the calling convention is not 203 /// specified, the "C" calling convention will be used. 204 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 205 const CallArgList &Args, 206 unsigned CallingConvention = 0); 207 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 208 const FunctionArgList &Args, 209 unsigned CallingConvention = 0); 210 const CGFunctionInfo &getFunctionInfo(QualType RetTy, 211 const llvm::SmallVector<QualType, 16> &ArgTys, 212 unsigned CallingConvention = 0); 213 214 public: // These are internal details of CGT that shouldn't be used externally. 215 /// addFieldInfo - Assign field number to field FD. 216 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo); 217 218 /// addBitFieldInfo - Assign a start bit and a size to field FD. 219 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, 220 unsigned Start, unsigned Size); 221 222 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field 223 /// FD. 224 BitFieldInfo getBitFieldInfo(const FieldDecl *FD); 225 226 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 227 /// enum. 228 const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 229 230 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 231 /// argument types it would be passed as on the provided vector \arg 232 /// ArgTys. See ABIArgInfo::Expand. 233 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys); 234 }; 235 236 } // end namespace CodeGen 237 } // end namespace clang 238 239 #endif 240