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 17212904Sdim#include "CGCall.h" 18224145Sdim#include "clang/AST/GlobalDecl.h" 19193326Sed#include "llvm/ADT/DenseMap.h" 20249423Sdim#include "llvm/IR/Module.h" 21193326Sed#include <vector> 22193326Sed 23193326Sednamespace llvm { 24193326Sed class FunctionType; 25193326Sed class Module; 26243830Sdim class DataLayout; 27193326Sed class Type; 28198092Srdivacky class LLVMContext; 29224145Sdim class StructType; 30193326Sed} 31193326Sed 32193326Sednamespace clang { 33193326Sed class ABIInfo; 34193326Sed class ASTContext; 35204643Srdivacky template <typename> class CanQual; 36199990Srdivacky class CXXConstructorDecl; 37199990Srdivacky class CXXDestructorDecl; 38193326Sed class CXXMethodDecl; 39224145Sdim class CodeGenOptions; 40193326Sed class FieldDecl; 41193326Sed class FunctionProtoType; 42193326Sed class ObjCInterfaceDecl; 43193326Sed class ObjCIvarDecl; 44193326Sed class PointerType; 45193326Sed class QualType; 46193326Sed class RecordDecl; 47193326Sed class TagDecl; 48193326Sed class TargetInfo; 49193326Sed class Type; 50204643Srdivacky typedef CanQual<Type> CanQualType; 51193326Sed 52193326Sednamespace CodeGen { 53212904Sdim class CGCXXABI; 54206084Srdivacky class CGRecordLayout; 55234353Sdim class CodeGenModule; 56234353Sdim class RequiredArgs; 57193326Sed 58193326Sed/// CodeGenTypes - This class organizes the cross-module state that is used 59193326Sed/// while lowering AST types to LLVM types. 60193326Sedclass CodeGenTypes { 61249423Sdimpublic: 62234353Sdim // Some of this stuff should probably be left on the CGM. 63251662Sdim CodeGenModule &CGM; 64193326Sed ASTContext &Context; 65224145Sdim llvm::Module &TheModule; 66243830Sdim const llvm::DataLayout &TheDataLayout; 67251662Sdim const TargetInfo &Target; 68212904Sdim CGCXXABI &TheCXXABI; 69224145Sdim const CodeGenOptions &CodeGenOpts; 70198092Srdivacky 71251662Sdim // This should not be moved earlier, since its initialization depends on some 72251662Sdim // of the previous reference members being already initialized 73251662Sdim const ABIInfo &TheABIInfo; 74251662Sdim 75249423Sdimprivate: 76193326Sed /// The opaque type map for Objective-C interfaces. All direct 77193326Sed /// manipulation is done by the runtime interfaces, which are 78193326Sed /// responsible for coercing to the appropriate type; these opaque 79193326Sed /// types are never refined. 80224145Sdim llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes; 81193326Sed 82198092Srdivacky /// CGRecordLayouts - This maps llvm struct type with corresponding 83198092Srdivacky /// record layout info. 84193326Sed llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 85193326Sed 86224145Sdim /// RecordDeclTypes - This contains the LLVM IR type for any converted 87224145Sdim /// RecordDecl. 88224145Sdim llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes; 89224145Sdim 90193326Sed /// FunctionInfos - Hold memoized CGFunctionInfo results. 91193326Sed llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 92193326Sed 93224145Sdim /// RecordsBeingLaidOut - This set keeps track of records that we're currently 94224145Sdim /// converting to an IR type. For example, when converting: 95224145Sdim /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B' 96224145Sdim /// types will be in this set. 97224145Sdim llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut; 98224145Sdim 99224145Sdim llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed; 100224145Sdim 101224145Sdim /// SkippedLayout - True if we didn't layout a function due to a being inside 102224145Sdim /// a recursive struct conversion, set this to true. 103224145Sdim bool SkippedLayout; 104224145Sdim 105226633Sdim SmallVector<const RecordDecl *, 8> DeferredRecords; 106224145Sdim 107193326Sedprivate: 108224145Sdim /// TypeCache - This map keeps cache of llvm::Types 109224145Sdim /// and maps llvm::Types to corresponding clang::Type. 110224145Sdim llvm::DenseMap<const Type *, llvm::Type *> TypeCache; 111193326Sed 112193326Sedpublic: 113251662Sdim CodeGenTypes(CodeGenModule &cgm); 114193326Sed ~CodeGenTypes(); 115198092Srdivacky 116243830Sdim const llvm::DataLayout &getDataLayout() const { return TheDataLayout; } 117193326Sed ASTContext &getContext() const { return Context; } 118202379Srdivacky const ABIInfo &getABIInfo() const { return TheABIInfo; } 119224145Sdim const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } 120251662Sdim const TargetInfo &getTarget() const { return Target; } 121212904Sdim CGCXXABI &getCXXABI() const { return TheCXXABI; } 122198092Srdivacky llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 123193326Sed 124198092Srdivacky /// ConvertType - Convert type T into a llvm::Type. 125224145Sdim llvm::Type *ConvertType(QualType T); 126198092Srdivacky 127193326Sed /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 128193326Sed /// ConvertType in that it is used to convert to the memory representation for 129193326Sed /// a type. For example, the scalar representation for _Bool is i1, but the 130193326Sed /// memory representation is usually i8 or i32, depending on the target. 131224145Sdim llvm::Type *ConvertTypeForMem(QualType T); 132193326Sed 133193326Sed /// GetFunctionType - Get the LLVM function type for \arg Info. 134234353Sdim llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info); 135198092Srdivacky 136224145Sdim llvm::FunctionType *GetFunctionType(GlobalDecl GD); 137199990Srdivacky 138224145Sdim /// isFuncTypeConvertible - Utility to check whether a function type can 139210299Sed /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 140210299Sed /// type). 141224145Sdim bool isFuncTypeConvertible(const FunctionType *FT); 142224145Sdim bool isFuncTypeArgumentConvertible(QualType Ty); 143224145Sdim 144207619Srdivacky /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 145218893Sdim /// given a CXXMethodDecl. If the method to has an incomplete return type, 146199990Srdivacky /// and/or incomplete argument types, this will return the opaque type. 147226633Sdim llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD); 148198092Srdivacky 149218893Sdim const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 150218893Sdim 151193326Sed /// UpdateCompletedType - When we find the full definition for a TagDecl, 152193326Sed /// replace the 'opaque' type we previously made for it if applicable. 153193326Sed void UpdateCompletedType(const TagDecl *TD); 154193326Sed 155221345Sdim /// getNullaryFunctionInfo - Get the function info for a void() 156221345Sdim /// function with standard CC. 157234353Sdim const CGFunctionInfo &arrangeNullaryFunction(); 158221345Sdim 159234353Sdim // The arrangement methods are split into three families: 160234353Sdim // - those meant to drive the signature and prologue/epilogue 161234353Sdim // of a function declaration or definition, 162234353Sdim // - those meant for the computation of the LLVM type for an abstract 163234353Sdim // appearance of a function, and 164234353Sdim // - those meant for performing the IR-generation of a call. 165234353Sdim // They differ mainly in how they deal with optional (i.e. variadic) 166234353Sdim // arguments, as well as unprototyped functions. 167234353Sdim // 168234353Sdim // Key points: 169234353Sdim // - The CGFunctionInfo for emitting a specific call site must include 170234353Sdim // entries for the optional arguments. 171234353Sdim // - The function type used at the call site must reflect the formal 172234353Sdim // signature of the declaration being called, or else the call will 173234353Sdim // go awry. 174234353Sdim // - For the most part, unprototyped functions are called by casting to 175234353Sdim // a formal signature inferred from the specific argument types used 176234353Sdim // at the call-site. However, some targets (e.g. x86-64) screw with 177234353Sdim // this for compatibility reasons. 178218893Sdim 179234353Sdim const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD); 180234353Sdim const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD); 181234353Sdim const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy, 182234353Sdim const FunctionArgList &Args, 183234353Sdim const FunctionType::ExtInfo &Info, 184234353Sdim bool isVariadic); 185199990Srdivacky 186234353Sdim const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD); 187234353Sdim const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 188234353Sdim QualType receiverType); 189203955Srdivacky 190234353Sdim const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD); 191234353Sdim const CGFunctionInfo &arrangeCXXConstructorDeclaration( 192234353Sdim const CXXConstructorDecl *D, 193234353Sdim CXXCtorType Type); 194234353Sdim const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D, 195234353Sdim CXXDtorType Type); 196210299Sed 197239462Sdim const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args, 198239462Sdim const FunctionType *Ty); 199239462Sdim const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy, 200239462Sdim const CallArgList &args, 201239462Sdim FunctionType::ExtInfo info, 202239462Sdim RequiredArgs required); 203249423Sdim const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args, 204249423Sdim const FunctionType *type); 205218893Sdim 206239462Sdim const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args, 207239462Sdim const FunctionProtoType *type, 208239462Sdim RequiredArgs required); 209239462Sdim 210239462Sdim const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty); 211239462Sdim const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty); 212234353Sdim const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD, 213234353Sdim const FunctionProtoType *FTP); 214204643Srdivacky 215239462Sdim /// "Arrange" the LLVM information for a call or type with the given 216239462Sdim /// signature. This is largely an internal method; other clients 217239462Sdim /// should use one of the above routines, which ultimately defer to 218239462Sdim /// this. 219218893Sdim /// 220234353Sdim /// \param argTypes - must all actually be canonical as params 221239462Sdim const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType, 222239462Sdim ArrayRef<CanQualType> argTypes, 223239462Sdim FunctionType::ExtInfo info, 224239462Sdim RequiredArgs args); 225198092Srdivacky 226206084Srdivacky /// \brief Compute a new LLVM record layout object for the given record. 227224145Sdim CGRecordLayout *ComputeRecordLayout(const RecordDecl *D, 228224145Sdim llvm::StructType *Ty); 229206084Srdivacky 230224145Sdim /// addRecordTypeName - Compute a name from the given record decl with an 231224145Sdim /// optional suffix and name the given LLVM type using it. 232224145Sdim void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, 233226633Sdim StringRef suffix); 234224145Sdim 235224145Sdim 236193326Sedpublic: // These are internal details of CGT that shouldn't be used externally. 237224145Sdim /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 238224145Sdim llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD); 239193326Sed 240193326Sed /// GetExpandedTypes - Expand the type \arg Ty into the LLVM 241193326Sed /// argument types it would be passed as on the provided vector \arg 242193326Sed /// ArgTys. See ABIArgInfo::Expand. 243223017Sdim void GetExpandedTypes(QualType type, 244226633Sdim SmallVectorImpl<llvm::Type*> &expanded); 245218893Sdim 246212904Sdim /// IsZeroInitializable - Return whether a type can be 247212904Sdim /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 248212904Sdim bool isZeroInitializable(QualType T); 249218893Sdim 250212904Sdim /// IsZeroInitializable - Return whether a record type can be 251212904Sdim /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 252212904Sdim bool isZeroInitializable(const CXXRecordDecl *RD); 253224145Sdim 254224145Sdim bool isRecordLayoutComplete(const Type *Ty) const; 255224145Sdim bool noRecordsBeingLaidOut() const { 256224145Sdim return RecordsBeingLaidOut.empty(); 257224145Sdim } 258224145Sdim bool isRecordBeingLaidOut(const Type *Ty) const { 259224145Sdim return RecordsBeingLaidOut.count(Ty); 260224145Sdim } 261224145Sdim 262193326Sed}; 263193326Sed 264193326Sed} // end namespace CodeGen 265193326Sed} // end namespace clang 266193326Sed 267193326Sed#endif 268