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); 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); 117 const llvm::Type *ConvertTypeForMemRecursive(QualType T); 118 119 /// GetFunctionType - Get the LLVM function type for \arg Info. 120 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 121 bool IsVariadic); 122 123 const llvm::FunctionType *GetFunctionType(GlobalDecl GD); 124 125 /// VerifyFuncTypeComplete - Utility to check whether a function type can 126 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 127 /// type). 128 static const TagType *VerifyFuncTypeComplete(const Type* T); 129 130 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 131 /// given a CXXMethodDecl. If the method to has an incomplete return type, 132 /// and/or incomplete argument types, this will return the opaque type. 133 const llvm::Type *GetFunctionTypeForVTable(const CXXMethodDecl *MD); 134 135 const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const; 136 137 /// UpdateCompletedType - When we find the full definition for a TagDecl, 138 /// replace the 'opaque' type we previously made for it if applicable. 139 void UpdateCompletedType(const TagDecl *TD); 140 141 /// getFunctionInfo - Get the function info for the specified function decl. 142 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD); 143 144 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 145 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 146 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 147 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 148 CXXCtorType Type); 149 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 150 CXXDtorType Type); 151 152 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args, 153 const FunctionType *Ty) { 154 return getFunctionInfo(Ty->getResultType(), Args, 155 Ty->getExtInfo()); 156 } 157 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty); 158 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty); 159 160 // getFunctionInfo - Get the function info for a member function. 161 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 162 const FunctionProtoType *FTP); 163 164 /// getFunctionInfo - Get the function info for a function described by a 165 /// return type and argument types. If the calling convention is not 166 /// specified, the "C" calling convention will be used. 167 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 168 const CallArgList &Args, 169 const FunctionType::ExtInfo &Info); 170 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 171 const FunctionArgList &Args, 172 const FunctionType::ExtInfo &Info); 173 174 /// Retrieves the ABI information for the given function signature. 175 /// 176 /// \param ArgTys - must all actually be canonical as params 177 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy, 178 const llvm::SmallVectorImpl<CanQualType> &ArgTys, 179 const FunctionType::ExtInfo &Info); 180 181 /// \brief Compute a new LLVM record layout object for the given record. 182 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D); 183 184 public: // These are internal details of CGT that shouldn't be used externally. 185 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 186 /// enum. 187 const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 188 189 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 190 /// argument types it would be passed as on the provided vector \arg 191 /// ArgTys. See ABIArgInfo::Expand. 192 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys); 193 194 /// ContainsPointerToDataMember - Return whether the given type contains a 195 /// pointer to a data member. 196 bool ContainsPointerToDataMember(QualType T); 197 198 /// ContainsPointerToDataMember - Return whether the record decl contains a 199 /// pointer to a data member. 200 bool ContainsPointerToDataMember(const CXXRecordDecl *RD); 201 }; 202 203 } // end namespace CodeGen 204 } // end namespace clang 205 206 #endif 207