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 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 const ABIInfo &Info); 145 ~CodeGenTypes(); 146 147 const llvm::TargetData &getTargetData() const { return TheTargetData; } 148 const TargetInfo &getTarget() const { return Target; } 149 ASTContext &getContext() const { return Context; } 150 const ABIInfo &getABIInfo() const { return TheABIInfo; } 151 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 152 153 /// ConvertType - Convert type T into a llvm::Type. 154 const llvm::Type *ConvertType(QualType T); 155 const llvm::Type *ConvertTypeRecursive(QualType T); 156 157 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 158 /// ConvertType in that it is used to convert to the memory representation for 159 /// a type. For example, the scalar representation for _Bool is i1, but the 160 /// memory representation is usually i8 or i32, depending on the target. 161 const llvm::Type *ConvertTypeForMem(QualType T); 162 const llvm::Type *ConvertTypeForMemRecursive(QualType T); 163 164 /// GetFunctionType - Get the LLVM function type for \arg Info. 165 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 166 bool IsVariadic); 167 168 169 /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable, 170 /// given a CXXMethodDecl. If the method to has an incomplete return type, 171 /// and/or incomplete argument types, this will return the opaque type. 172 const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD); 173 174 const CGRecordLayout &getCGRecordLayout(const TagDecl*) const; 175 176 /// getLLVMFieldNo - Return llvm::StructType element number 177 /// that corresponds to the field FD. 178 unsigned getLLVMFieldNo(const FieldDecl *FD); 179 180 /// UpdateCompletedType - When we find the full definition for a TagDecl, 181 /// replace the 'opaque' type we previously made for it if applicable. 182 void UpdateCompletedType(const TagDecl *TD); 183 184 private: 185 const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP); 186 const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP); 187 188 public: 189 /// getFunctionInfo - Get the function info for the specified function decl. 190 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 191 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 192 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 193 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 194 CXXCtorType Type); 195 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 196 CXXDtorType Type); 197 198 // getFunctionInfo - Get the function info for a member function. 199 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 200 const FunctionProtoType *FTP); 201 202 /// getFunctionInfo - Get the function info for a function described by a 203 /// return type and argument types. If the calling convention is not 204 /// specified, the "C" calling convention will be used. 205 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 206 const CallArgList &Args, 207 unsigned CallingConvention = 0); 208 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 209 const FunctionArgList &Args, 210 unsigned CallingConvention = 0); 211 const CGFunctionInfo &getFunctionInfo(QualType RetTy, 212 const llvm::SmallVector<QualType, 16> &ArgTys, 213 unsigned CallingConvention = 0); 214 215 public: // These are internal details of CGT that shouldn't be used externally. 216 /// addFieldInfo - Assign field number to field FD. 217 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo); 218 219 /// addBitFieldInfo - Assign a start bit and a size to field FD. 220 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, 221 unsigned Start, unsigned Size); 222 223 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field 224 /// FD. 225 BitFieldInfo getBitFieldInfo(const FieldDecl *FD); 226 227 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 228 /// enum. 229 const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 230 231 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 232 /// argument types it would be passed as on the provided vector \arg 233 /// ArgTys. See ABIArgInfo::Expand. 234 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys); 235 }; 236 237 } // end namespace CodeGen 238 } // end namespace clang 239 240 #endif 241