CodeGenTypes.h revision 42e06119496e84e74cfc60230b18fcb53b35eb1c
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 23namespace llvm { 24 class FunctionType; 25 class Module; 26 class OpaqueType; 27 class PATypeHolder; 28 class TargetData; 29 class Type; 30 class LLVMContext; 31} 32 33namespace 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 52namespace 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. 58class 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 86private: 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 /// addRecordTypeName - Compute a name from the given record decl with an 105 /// optional suffix and name the given LLVM type using it. 106 void addRecordTypeName(const RecordDecl *RD, const llvm::Type *Ty, 107 llvm::StringRef suffix); 108 109public: 110 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD, 111 const ABIInfo &Info, CGCXXABI &CXXABI); 112 ~CodeGenTypes(); 113 114 const llvm::TargetData &getTargetData() const { return TheTargetData; } 115 const TargetInfo &getTarget() const { return Target; } 116 ASTContext &getContext() const { return Context; } 117 const ABIInfo &getABIInfo() const { return TheABIInfo; } 118 CGCXXABI &getCXXABI() const { return TheCXXABI; } 119 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 120 121 /// ConvertType - Convert type T into a llvm::Type. 122 const llvm::Type *ConvertType(QualType T, bool IsRecursive = false); 123 const llvm::Type *ConvertTypeRecursive(QualType T); 124 125 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 126 /// ConvertType in that it is used to convert to the memory representation for 127 /// a type. For example, the scalar representation for _Bool is i1, but the 128 /// memory representation is usually i8 or i32, depending on the target. 129 const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false); 130 const llvm::Type *ConvertTypeForMemRecursive(QualType T) { 131 return ConvertTypeForMem(T, true); 132 } 133 134 /// GetFunctionType - Get the LLVM function type for \arg Info. 135 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 136 bool IsVariadic, 137 bool IsRecursive = false); 138 139 const llvm::FunctionType *GetFunctionType(GlobalDecl GD); 140 141 /// VerifyFuncTypeComplete - Utility to check whether a function type can 142 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 143 /// type). 144 static const TagType *VerifyFuncTypeComplete(const Type* T); 145 146 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 147 /// given a CXXMethodDecl. If the method to has an incomplete return type, 148 /// and/or incomplete argument types, this will return the opaque type. 149 const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD); 150 151 const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 152 153 /// addBaseSubobjectTypeName - Add a type name for the base subobject of the 154 /// given record layout. 155 void addBaseSubobjectTypeName(const CXXRecordDecl *RD, 156 const CGRecordLayout &layout); 157 158 /// UpdateCompletedType - When we find the full definition for a TagDecl, 159 /// replace the 'opaque' type we previously made for it if applicable. 160 void UpdateCompletedType(const TagDecl *TD); 161 162 /// getNullaryFunctionInfo - Get the function info for a void() 163 /// function with standard CC. 164 const CGFunctionInfo &getNullaryFunctionInfo(); 165 166 /// getFunctionInfo - Get the function info for the specified function decl. 167 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD); 168 169 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 170 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 171 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 172 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 173 CXXCtorType Type); 174 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 175 CXXDtorType Type); 176 177 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args, 178 const FunctionType *Ty) { 179 return getFunctionInfo(Ty->getResultType(), Args, 180 Ty->getExtInfo()); 181 } 182 183 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty, 184 bool IsRecursive = false); 185 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty, 186 bool IsRecursive = false); 187 188 /// getFunctionInfo - Get the function info for a member function of 189 /// the given type. This is used for calls through member function 190 /// pointers. 191 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 192 const FunctionProtoType *FTP); 193 194 /// getFunctionInfo - Get the function info for a function described by a 195 /// return type and argument types. If the calling convention is not 196 /// specified, the "C" calling convention will be used. 197 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 198 const CallArgList &Args, 199 const FunctionType::ExtInfo &Info); 200 const CGFunctionInfo &getFunctionInfo(QualType ResTy, 201 const FunctionArgList &Args, 202 const FunctionType::ExtInfo &Info); 203 204 /// Retrieves the ABI information for the given function signature. 205 /// 206 /// \param ArgTys - must all actually be canonical as params 207 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy, 208 const llvm::SmallVectorImpl<CanQualType> &ArgTys, 209 const FunctionType::ExtInfo &Info, 210 bool IsRecursive = false); 211 212 /// \brief Compute a new LLVM record layout object for the given record. 213 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D); 214 215public: // These are internal details of CGT that shouldn't be used externally. 216 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 217 /// enum. 218 const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 219 220 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 221 /// argument types it would be passed as on the provided vector \arg 222 /// ArgTys. See ABIArgInfo::Expand. 223 void GetExpandedTypes(QualType type, 224 llvm::SmallVectorImpl<const llvm::Type*> &expanded, 225 bool isRecursive); 226 227 /// IsZeroInitializable - Return whether a type can be 228 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 229 bool isZeroInitializable(QualType T); 230 231 /// IsZeroInitializable - Return whether a record type can be 232 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 233 bool isZeroInitializable(const CXXRecordDecl *RD); 234}; 235 236} // end namespace CodeGen 237} // end namespace clang 238 239#endif 240