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 "CGCall.h" 18 #include "GlobalDecl.h" 19 #include "llvm/Module.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include <vector> 22 23 namespace llvm { 24 class FunctionType; 25 class Module; 26 class OpaqueType; 27 class PATypeHolder; 28 class TargetData; 29 class Type; 30 class LLVMContext; 31 } 32 33 namespace clang { 34 class ABIInfo; 35 class ASTContext; 36 template <typename> class CanQual; 37 class CXXConstructorDecl; 38 class CXXDestructorDecl; 39 class CXXMethodDecl; 40 class FieldDecl; 41 class FunctionProtoType; 42 class ObjCInterfaceDecl; 43 class ObjCIvarDecl; 44 class PointerType; 45 class QualType; 46 class RecordDecl; 47 class TagDecl; 48 class TargetInfo; 49 class Type; 50 typedef CanQual<Type> CanQualType; 51 52 namespace CodeGen { 53 class CGCXXABI; 54 class CGRecordLayout; 55 56 /// CodeGenTypes - This class organizes the cross-module state that is used 57 /// while lowering AST types to LLVM types. 58 class CodeGenTypes { 59 ASTContext &Context; 60 const TargetInfo &Target; 61 llvm::Module& TheModule; 62 const llvm::TargetData& TheTargetData; 63 const ABIInfo& TheABIInfo; 64 CGCXXABI &TheCXXABI; 65 66 llvm::SmallVector<std::pair<QualType, 67 llvm::OpaqueType *>, 8> PointersToResolve; 68 69 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes; 70 71 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes; 72 73 /// The opaque type map for Objective-C interfaces. All direct 74 /// manipulation is done by the runtime interfaces, which are 75 /// responsible for coercing to the appropriate type; these opaque 76 /// types are never refined. 77 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes; 78 79 /// CGRecordLayouts - This maps llvm struct type with corresponding 80 /// record layout info. 81 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 82 83 /// FunctionInfos - Hold memoized CGFunctionInfo results. 84 llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 85 86 private: 87 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder) 88 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is 89 /// used instead of llvm::Type because it allows us to bypass potential 90 /// dangling type pointers due to type refinement on llvm side. 91 llvm::DenseMap<const Type *, llvm::PATypeHolder> TypeCache; 92 93 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this 94 /// method directly because it does not do any type caching. This method 95 /// is available only for ConvertType(). CovertType() is preferred 96 /// interface to convert type T into a llvm::Type. 97 const llvm::Type *ConvertNewType(QualType T); 98 99 /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles 100 /// pointers that are referenced but have not been converted yet. This is 101 /// used to handle cyclic structures properly. 102 void HandleLateResolvedPointers(); 103 104 public: 105 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD, 106 const ABIInfo &Info, CGCXXABI &CXXABI); 107 ~CodeGenTypes(); 108 109 const llvm::TargetData &getTargetData() const { return TheTargetData; } 110 const TargetInfo &getTarget() const { return Target; } 111 ASTContext &getContext() const { return Context; } 112 const ABIInfo &getABIInfo() const { return TheABIInfo; } 113 CGCXXABI &getCXXABI() const { return TheCXXABI; } 114 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 115 116 /// ConvertType - Convert type T into a llvm::Type. 117 const llvm::Type *ConvertType(QualType T, bool IsRecursive = false); 118 const llvm::Type *ConvertTypeRecursive(QualType T); 119 120 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 121 /// ConvertType in that it is used to convert to the memory representation for 122 /// a type. For example, the scalar representation for _Bool is i1, but the 123 /// memory representation is usually i8 or i32, depending on the target. 124 const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false); 125 const llvm::Type *ConvertTypeForMemRecursive(QualType T) { 126 return ConvertTypeForMem(T, true); 127 } 128 129 /// GetFunctionType - Get the LLVM function type for \arg Info. 130 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 131 bool IsVariadic, 132 bool IsRecursive = false); 133 134 const llvm::FunctionType *GetFunctionType(GlobalDecl GD); 135 136 /// VerifyFuncTypeComplete - Utility to check whether a function type can 137 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 138 /// type). 139 static const TagType *VerifyFuncTypeComplete(const Type* T); 140 141 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 142 /// given a CXXMethodDecl. If the method to has an incomplete return type, 143 /// and/or incomplete argument types, this will return the opaque type. 144 const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD); 145 146 const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 147 148 /// UpdateCompletedType - When we find the full definition for a TagDecl, 149 /// replace the 'opaque' type we previously made for it if applicable. 150 void UpdateCompletedType(const TagDecl *TD); 151 152 /// getFunctionInfo - Get the function info for the specified function decl. 153 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD); 154 155 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 156 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 157 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 158 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 159 CXXCtorType Type); 160 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 161 CXXDtorType Type); 162 163 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args, 164 const FunctionType *Ty) { 165 return getFunctionInfo(Ty->getResultType(), Args, 166 Ty->getExtInfo()); 167 } 168 169 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty, 170 bool IsRecursive = false); 171 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty, 172 bool IsRecursive = false); 173 174 /// getFunctionInfo - Get the function info for a member function of 175 /// the given type. This is used for calls through member function 176 /// pointers. 177 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 178 const FunctionProtoType *FTP); 179 180 /// getFunctionInfo - Get the function info for a function described by a 181 /// return type and argument types. If the calling convention is not 182 /// specified, the "C" calling convention will be used. 183 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 184 const CallArgList &Args, 185 const FunctionType::ExtInfo &Info); 186 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 187 const FunctionArgList &Args, 188 const FunctionType::ExtInfo &Info); 189 190 /// Retrieves the ABI information for the given function signature. 191 /// 192 /// \param ArgTys - must all actually be canonical as params 193 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy, 194 const llvm::SmallVectorImpl<CanQualType> &ArgTys, 195 const FunctionType::ExtInfo &Info, 196 bool IsRecursive = false); 197 198 /// \brief Compute a new LLVM record layout object for the given record. 199 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D); 200 201 public: // These are internal details of CGT that shouldn't be used externally. 202 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 203 /// enum. 204 const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 205 206 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 207 /// argument types it would be passed as on the provided vector \arg 208 /// ArgTys. See ABIArgInfo::Expand. 209 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys, 210 bool IsRecursive); 211 212 /// IsZeroInitializable - Return whether a type can be 213 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 214 bool isZeroInitializable(QualType T); 215 216 /// IsZeroInitializable - Return whether a record type can be 217 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 218 bool isZeroInitializable(const CXXRecordDecl *RD); 219 }; 220 221 } // end namespace CodeGen 222 } // end namespace clang 223 224 #endif 225