CodeGenTypes.h revision 206084
1193326Sed//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10198092Srdivacky// This is the code that handles AST -> LLVM type lowering. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#ifndef CLANG_CODEGEN_CODEGENTYPES_H 15193326Sed#define CLANG_CODEGEN_CODEGENTYPES_H 16193326Sed 17198092Srdivacky#include "llvm/Module.h" 18193326Sed#include "llvm/ADT/DenseMap.h" 19193326Sed#include <vector> 20193326Sed 21193326Sed#include "CGCall.h" 22203955Srdivacky#include "GlobalDecl.h" 23193326Sed 24193326Sednamespace llvm { 25193326Sed class FunctionType; 26193326Sed class Module; 27193326Sed class OpaqueType; 28193326Sed class PATypeHolder; 29193326Sed class TargetData; 30193326Sed class Type; 31198092Srdivacky class LLVMContext; 32193326Sed} 33193326Sed 34193326Sednamespace clang { 35193326Sed class ABIInfo; 36193326Sed class ASTContext; 37204643Srdivacky template <typename> class CanQual; 38199990Srdivacky class CXXConstructorDecl; 39199990Srdivacky class CXXDestructorDecl; 40193326Sed class CXXMethodDecl; 41193326Sed class FieldDecl; 42193326Sed class FunctionProtoType; 43193326Sed class ObjCInterfaceDecl; 44193326Sed class ObjCIvarDecl; 45193326Sed class PointerType; 46193326Sed class QualType; 47193326Sed class RecordDecl; 48193326Sed class TagDecl; 49193326Sed class TargetInfo; 50193326Sed class Type; 51204643Srdivacky typedef CanQual<Type> CanQualType; 52193326Sed 53193326Sednamespace CodeGen { 54206084Srdivacky class CGRecordLayout; 55193326Sed class CodeGenTypes; 56193326Sed 57193326Sed/// CodeGenTypes - This class organizes the cross-module state that is used 58193326Sed/// while lowering AST types to LLVM types. 59193326Sedclass CodeGenTypes { 60193326Sed ASTContext &Context; 61199482Srdivacky const TargetInfo &Target; 62193326Sed llvm::Module& TheModule; 63193326Sed const llvm::TargetData& TheTargetData; 64202379Srdivacky const ABIInfo& TheABIInfo; 65198092Srdivacky 66193326Sed llvm::SmallVector<std::pair<QualType, 67193326Sed llvm::OpaqueType *>, 8> PointersToResolve; 68193326Sed 69193326Sed llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes; 70193326Sed 71193326Sed llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes; 72193326Sed 73193326Sed /// The opaque type map for Objective-C interfaces. All direct 74193326Sed /// manipulation is done by the runtime interfaces, which are 75193326Sed /// responsible for coercing to the appropriate type; these opaque 76193326Sed /// types are never refined. 77193326Sed llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes; 78193326Sed 79198092Srdivacky /// CGRecordLayouts - This maps llvm struct type with corresponding 80198092Srdivacky /// record layout info. 81193326Sed llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 82193326Sed 83193326Sed /// FunctionInfos - Hold memoized CGFunctionInfo results. 84193326Sed llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 85193326Sed 86193326Sedprivate: 87193326Sed /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder) 88193326Sed /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is 89198092Srdivacky /// used instead of llvm::Type because it allows us to bypass potential 90193326Sed /// dangling type pointers due to type refinement on llvm side. 91193326Sed llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache; 92193326Sed 93193326Sed /// ConvertNewType - Convert type T into a llvm::Type. Do not use this 94193326Sed /// method directly because it does not do any type caching. This method 95193326Sed /// is available only for ConvertType(). CovertType() is preferred 96193326Sed /// interface to convert type T into a llvm::Type. 97193326Sed const llvm::Type *ConvertNewType(QualType T); 98193326Sedpublic: 99202379Srdivacky CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD, 100202379Srdivacky const ABIInfo &Info); 101193326Sed ~CodeGenTypes(); 102198092Srdivacky 103193326Sed const llvm::TargetData &getTargetData() const { return TheTargetData; } 104199482Srdivacky const TargetInfo &getTarget() const { return Target; } 105193326Sed ASTContext &getContext() const { return Context; } 106202379Srdivacky const ABIInfo &getABIInfo() const { return TheABIInfo; } 107198092Srdivacky llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 108193326Sed 109198092Srdivacky /// ConvertType - Convert type T into a llvm::Type. 110193326Sed const llvm::Type *ConvertType(QualType T); 111193326Sed const llvm::Type *ConvertTypeRecursive(QualType T); 112198092Srdivacky 113193326Sed /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 114193326Sed /// ConvertType in that it is used to convert to the memory representation for 115193326Sed /// a type. For example, the scalar representation for _Bool is i1, but the 116193326Sed /// memory representation is usually i8 or i32, depending on the target. 117193326Sed const llvm::Type *ConvertTypeForMem(QualType T); 118193326Sed const llvm::Type *ConvertTypeForMemRecursive(QualType T); 119193326Sed 120193326Sed /// GetFunctionType - Get the LLVM function type for \arg Info. 121193326Sed const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info, 122193326Sed bool IsVariadic); 123198092Srdivacky 124204643Srdivacky const llvm::FunctionType *GetFunctionType(GlobalDecl GD); 125199990Srdivacky 126204643Srdivacky 127199990Srdivacky /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable, 128199990Srdivacky /// given a CXXMethodDecl. If the method to has an incomplete return type, 129199990Srdivacky /// and/or incomplete argument types, this will return the opaque type. 130199990Srdivacky const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD); 131199990Srdivacky 132206084Srdivacky const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const; 133198092Srdivacky 134193326Sed /// UpdateCompletedType - When we find the full definition for a TagDecl, 135193326Sed /// replace the 'opaque' type we previously made for it if applicable. 136193326Sed void UpdateCompletedType(const TagDecl *TD); 137193326Sed 138198092Srdivacky /// getFunctionInfo - Get the function info for the specified function decl. 139203955Srdivacky const CGFunctionInfo &getFunctionInfo(GlobalDecl GD); 140203955Srdivacky 141193326Sed const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD); 142193326Sed const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD); 143193326Sed const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD); 144199990Srdivacky const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D, 145199990Srdivacky CXXCtorType Type); 146199990Srdivacky const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D, 147199990Srdivacky CXXDtorType Type); 148199990Srdivacky 149203955Srdivacky const CGFunctionInfo &getFunctionInfo(const CallArgList &Args, 150203955Srdivacky const FunctionType *Ty) { 151203955Srdivacky return getFunctionInfo(Ty->getResultType(), Args, 152206084Srdivacky Ty->getExtInfo()); 153203955Srdivacky } 154204643Srdivacky const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty); 155204643Srdivacky const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty); 156203955Srdivacky 157198092Srdivacky // getFunctionInfo - Get the function info for a member function. 158198092Srdivacky const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD, 159198092Srdivacky const FunctionProtoType *FTP); 160198092Srdivacky 161198092Srdivacky /// getFunctionInfo - Get the function info for a function described by a 162198092Srdivacky /// return type and argument types. If the calling convention is not 163198092Srdivacky /// specified, the "C" calling convention will be used. 164198092Srdivacky const CGFunctionInfo &getFunctionInfo(QualType ResTy, 165198092Srdivacky const CallArgList &Args, 166206084Srdivacky const FunctionType::ExtInfo &Info); 167198092Srdivacky const CGFunctionInfo &getFunctionInfo(QualType ResTy, 168198092Srdivacky const FunctionArgList &Args, 169206084Srdivacky const FunctionType::ExtInfo &Info); 170204643Srdivacky 171204643Srdivacky /// Retrieves the ABI information for the given function signature. 172204643Srdivacky /// 173204643Srdivacky /// \param ArgTys - must all actually be canonical as params 174204643Srdivacky const CGFunctionInfo &getFunctionInfo(CanQualType RetTy, 175204643Srdivacky const llvm::SmallVectorImpl<CanQualType> &ArgTys, 176206084Srdivacky const FunctionType::ExtInfo &Info); 177198092Srdivacky 178206084Srdivacky /// \brief Compute a new LLVM record layout object for the given record. 179206084Srdivacky CGRecordLayout *ComputeRecordLayout(const RecordDecl *D); 180206084Srdivacky 181193326Sedpublic: // These are internal details of CGT that shouldn't be used externally. 182193326Sed /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or 183193326Sed /// enum. 184193326Sed const llvm::Type *ConvertTagDeclType(const TagDecl *TD); 185193326Sed 186193326Sed /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 187193326Sed /// argument types it would be passed as on the provided vector \arg 188193326Sed /// ArgTys. See ABIArgInfo::Expand. 189193326Sed void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys); 190193326Sed}; 191193326Sed 192193326Sed} // end namespace CodeGen 193193326Sed} // end namespace clang 194193326Sed 195193326Sed#endif 196