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