1193326Sed//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===// 2193326Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193326Sed// 7193326Sed//===----------------------------------------------------------------------===// 8193326Sed// 9198092Srdivacky// This is the code that handles AST -> LLVM type lowering. 10193326Sed// 11193326Sed//===----------------------------------------------------------------------===// 12193326Sed 13280031Sdim#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H 14280031Sdim#define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H 15193326Sed 16212904Sdim#include "CGCall.h" 17314564Sdim#include "clang/Basic/ABI.h" 18261991Sdim#include "clang/CodeGen/CGFunctionInfo.h" 19193326Sed#include "llvm/ADT/DenseMap.h" 20249423Sdim#include "llvm/IR/Module.h" 21193326Sed 22193326Sednamespace llvm { 23280031Sdimclass FunctionType; 24280031Sdimclass DataLayout; 25280031Sdimclass Type; 26280031Sdimclass LLVMContext; 27280031Sdimclass StructType; 28193326Sed} 29193326Sed 30193326Sednamespace clang { 31280031Sdimclass ASTContext; 32280031Sdimtemplate <typename> class CanQual; 33280031Sdimclass CXXConstructorDecl; 34280031Sdimclass CXXDestructorDecl; 35280031Sdimclass CXXMethodDecl; 36280031Sdimclass CodeGenOptions; 37280031Sdimclass FieldDecl; 38280031Sdimclass FunctionProtoType; 39280031Sdimclass ObjCInterfaceDecl; 40280031Sdimclass ObjCIvarDecl; 41280031Sdimclass PointerType; 42280031Sdimclass QualType; 43280031Sdimclass RecordDecl; 44280031Sdimclass TagDecl; 45280031Sdimclass TargetInfo; 46280031Sdimclass Type; 47280031Sdimtypedef CanQual<Type> CanQualType; 48314564Sdimclass GlobalDecl; 49193326Sed 50193326Sednamespace CodeGen { 51309124Sdimclass ABIInfo; 52280031Sdimclass CGCXXABI; 53280031Sdimclass CGRecordLayout; 54280031Sdimclass CodeGenModule; 55280031Sdimclass RequiredArgs; 56193326Sed 57288943Sdim/// This class organizes the cross-module state that is used while lowering 58288943Sdim/// AST types to LLVM types. 59193326Sedclass CodeGenTypes { 60261991Sdim CodeGenModule &CGM; 61234353Sdim // Some of this stuff should probably be left on the CGM. 62193326Sed ASTContext &Context; 63224145Sdim llvm::Module &TheModule; 64251662Sdim const TargetInfo &Target; 65212904Sdim CGCXXABI &TheCXXABI; 66198092Srdivacky 67251662Sdim // This should not be moved earlier, since its initialization depends on some 68251662Sdim // of the previous reference members being already initialized 69251662Sdim const ABIInfo &TheABIInfo; 70251662Sdim 71193326Sed /// The opaque type map for Objective-C interfaces. All direct 72193326Sed /// manipulation is done by the runtime interfaces, which are 73193326Sed /// responsible for coercing to the appropriate type; these opaque 74193326Sed /// types are never refined. 75224145Sdim llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes; 76193326Sed 77288943Sdim /// Maps clang struct type with corresponding record layout info. 78193326Sed llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 79193326Sed 80288943Sdim /// Contains the LLVM IR type for any converted RecordDecl. 81224145Sdim llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes; 82341825Sdim 83288943Sdim /// Hold memoized CGFunctionInfo results. 84193326Sed llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 85193326Sed 86288943Sdim /// This set keeps track of records that we're currently converting 87288943Sdim /// to an IR type. For example, when converting: 88224145Sdim /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B' 89224145Sdim /// types will be in this set. 90224145Sdim llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut; 91341825Sdim 92224145Sdim llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed; 93341825Sdim 94288943Sdim /// True if we didn't layout a function due to a being inside 95224145Sdim /// a recursive struct conversion, set this to true. 96224145Sdim bool SkippedLayout; 97224145Sdim 98226633Sdim SmallVector<const RecordDecl *, 8> DeferredRecords; 99341825Sdim 100288943Sdim /// This map keeps cache of llvm::Types and maps clang::Type to 101288943Sdim /// corresponding llvm::Type. 102224145Sdim llvm::DenseMap<const Type *, llvm::Type *> TypeCache; 103193326Sed 104309124Sdim llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers; 105309124Sdim 106353358Sdim /// Helper for ConvertType. 107353358Sdim llvm::Type *ConvertFunctionTypeInternal(QualType FT); 108353358Sdim 109193326Sedpublic: 110251662Sdim CodeGenTypes(CodeGenModule &cgm); 111193326Sed ~CodeGenTypes(); 112198092Srdivacky 113296417Sdim const llvm::DataLayout &getDataLayout() const { 114296417Sdim return TheModule.getDataLayout(); 115296417Sdim } 116193326Sed ASTContext &getContext() const { return Context; } 117202379Srdivacky const ABIInfo &getABIInfo() const { return TheABIInfo; } 118251662Sdim const TargetInfo &getTarget() const { return Target; } 119212904Sdim CGCXXABI &getCXXABI() const { return TheCXXABI; } 120198092Srdivacky llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 121323112Sdim const CodeGenOptions &getCodeGenOpts() const; 122193326Sed 123327952Sdim /// Convert clang calling convention to LLVM callilng convention. 124327952Sdim unsigned ClangCallConvToLLVMCallConv(CallingConv CC); 125327952Sdim 126353358Sdim /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR 127353358Sdim /// qualification. 128353358Sdim CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD); 129353358Sdim 130198092Srdivacky /// ConvertType - Convert type T into a llvm::Type. 131224145Sdim llvm::Type *ConvertType(QualType T); 132198092Srdivacky 133193326Sed /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 134193326Sed /// ConvertType in that it is used to convert to the memory representation for 135193326Sed /// a type. For example, the scalar representation for _Bool is i1, but the 136193326Sed /// memory representation is usually i8 or i32, depending on the target. 137224145Sdim llvm::Type *ConvertTypeForMem(QualType T); 138193326Sed 139193326Sed /// GetFunctionType - Get the LLVM function type for \arg Info. 140234353Sdim llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info); 141198092Srdivacky 142224145Sdim llvm::FunctionType *GetFunctionType(GlobalDecl GD); 143199990Srdivacky 144224145Sdim /// isFuncTypeConvertible - Utility to check whether a function type can 145210299Sed /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 146210299Sed /// type). 147224145Sdim bool isFuncTypeConvertible(const FunctionType *FT); 148276479Sdim bool isFuncParamTypeConvertible(QualType Ty); 149276479Sdim 150309124Sdim /// Determine if a C++ inheriting constructor should have parameters matching 151309124Sdim /// those of its inherited constructor. 152309124Sdim bool inheritingCtorHasParams(const InheritedConstructor &Inherited, 153309124Sdim CXXCtorType Type); 154309124Sdim 155207619Srdivacky /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 156218893Sdim /// given a CXXMethodDecl. If the method to has an incomplete return type, 157199990Srdivacky /// and/or incomplete argument types, this will return the opaque type. 158226633Sdim llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD); 159198092Srdivacky 160218893Sdim const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 161218893Sdim 162193326Sed /// UpdateCompletedType - When we find the full definition for a TagDecl, 163193326Sed /// replace the 'opaque' type we previously made for it if applicable. 164193326Sed void UpdateCompletedType(const TagDecl *TD); 165193326Sed 166341825Sdim /// Remove stale types from the type cache when an inheritance model 167309124Sdim /// gets assigned to a class. 168309124Sdim void RefreshTypeCacheForClass(const CXXRecordDecl *RD); 169221345Sdim 170234353Sdim // The arrangement methods are split into three families: 171234353Sdim // - those meant to drive the signature and prologue/epilogue 172234353Sdim // of a function declaration or definition, 173234353Sdim // - those meant for the computation of the LLVM type for an abstract 174234353Sdim // appearance of a function, and 175234353Sdim // - those meant for performing the IR-generation of a call. 176234353Sdim // They differ mainly in how they deal with optional (i.e. variadic) 177234353Sdim // arguments, as well as unprototyped functions. 178234353Sdim // 179234353Sdim // Key points: 180234353Sdim // - The CGFunctionInfo for emitting a specific call site must include 181234353Sdim // entries for the optional arguments. 182234353Sdim // - The function type used at the call site must reflect the formal 183234353Sdim // signature of the declaration being called, or else the call will 184234353Sdim // go awry. 185234353Sdim // - For the most part, unprototyped functions are called by casting to 186234353Sdim // a formal signature inferred from the specific argument types used 187234353Sdim // at the call-site. However, some targets (e.g. x86-64) screw with 188234353Sdim // this for compatibility reasons. 189218893Sdim 190234353Sdim const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD); 191309124Sdim 192309124Sdim /// Given a function info for a declaration, return the function info 193309124Sdim /// for a call with the given arguments. 194309124Sdim /// 195309124Sdim /// Often this will be able to simply return the declaration info. 196309124Sdim const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI, 197309124Sdim const CallArgList &args); 198309124Sdim 199309124Sdim /// Free functions are functions that are compatible with an ordinary 200309124Sdim /// C function pointer type. 201234353Sdim const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD); 202309124Sdim const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args, 203309124Sdim const FunctionType *Ty, 204309124Sdim bool ChainCall); 205353358Sdim const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty); 206309124Sdim const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty); 207309124Sdim 208309124Sdim /// A nullary function is a freestanding function of type 'void ()'. 209309124Sdim /// This method works for both calls and declarations. 210309124Sdim const CGFunctionInfo &arrangeNullaryFunction(); 211309124Sdim 212309124Sdim /// A builtin function is a freestanding function using the default 213309124Sdim /// C conventions. 214276479Sdim const CGFunctionInfo & 215309124Sdim arrangeBuiltinFunctionDeclaration(QualType resultType, 216309124Sdim const FunctionArgList &args); 217309124Sdim const CGFunctionInfo & 218309124Sdim arrangeBuiltinFunctionDeclaration(CanQualType resultType, 219309124Sdim ArrayRef<CanQualType> argTypes); 220309124Sdim const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType, 221309124Sdim const CallArgList &args); 222199990Srdivacky 223309124Sdim /// Objective-C methods are C functions with some implicit parameters. 224234353Sdim const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD); 225234353Sdim const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 226234353Sdim QualType receiverType); 227309124Sdim const CGFunctionInfo &arrangeUnprototypedObjCMessageSend( 228309124Sdim QualType returnType, 229309124Sdim const CallArgList &args); 230203955Srdivacky 231309124Sdim /// Block invocation functions are C functions with an implicit parameter. 232309124Sdim const CGFunctionInfo &arrangeBlockFunctionDeclaration( 233309124Sdim const FunctionProtoType *type, 234309124Sdim const FunctionArgList &args); 235309124Sdim const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args, 236309124Sdim const FunctionType *type); 237309124Sdim 238309124Sdim /// C++ methods have some special rules and also have implicit parameters. 239234353Sdim const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD); 240353358Sdim const CGFunctionInfo &arrangeCXXStructorDeclaration(GlobalDecl GD); 241276479Sdim const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args, 242276479Sdim const CXXConstructorDecl *D, 243276479Sdim CXXCtorType CtorKind, 244321369Sdim unsigned ExtraPrefixArgs, 245321369Sdim unsigned ExtraSuffixArgs, 246321369Sdim bool PassProtoArgs = true); 247218893Sdim 248239462Sdim const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args, 249239462Sdim const FunctionProtoType *type, 250321369Sdim RequiredArgs required, 251321369Sdim unsigned numPrefixArgs); 252341825Sdim const CGFunctionInfo & 253341825Sdim arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD); 254288943Sdim const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD, 255288943Sdim CXXCtorType CT); 256234353Sdim const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD, 257296417Sdim const FunctionProtoType *FTP, 258296417Sdim const CXXMethodDecl *MD); 259204643Srdivacky 260239462Sdim /// "Arrange" the LLVM information for a call or type with the given 261239462Sdim /// signature. This is largely an internal method; other clients 262239462Sdim /// should use one of the above routines, which ultimately defer to 263239462Sdim /// this. 264218893Sdim /// 265234353Sdim /// \param argTypes - must all actually be canonical as params 266239462Sdim const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType, 267280031Sdim bool instanceMethod, 268280031Sdim bool chainCall, 269239462Sdim ArrayRef<CanQualType> argTypes, 270239462Sdim FunctionType::ExtInfo info, 271309124Sdim ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, 272239462Sdim RequiredArgs args); 273198092Srdivacky 274341825Sdim /// Compute a new LLVM record layout object for the given record. 275224145Sdim CGRecordLayout *ComputeRecordLayout(const RecordDecl *D, 276224145Sdim llvm::StructType *Ty); 277206084Srdivacky 278224145Sdim /// addRecordTypeName - Compute a name from the given record decl with an 279224145Sdim /// optional suffix and name the given LLVM type using it. 280224145Sdim void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, 281226633Sdim StringRef suffix); 282224145Sdim 283341825Sdim 284193326Sedpublic: // These are internal details of CGT that shouldn't be used externally. 285224145Sdim /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 286224145Sdim llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD); 287193326Sed 288280031Sdim /// getExpandedTypes - Expand the type \arg Ty into the LLVM 289280031Sdim /// argument types it would be passed as. See ABIArgInfo::Expand. 290280031Sdim void getExpandedTypes(QualType Ty, 291280031Sdim SmallVectorImpl<llvm::Type *>::iterator &TI); 292218893Sdim 293212904Sdim /// IsZeroInitializable - Return whether a type can be 294212904Sdim /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 295212904Sdim bool isZeroInitializable(QualType T); 296218893Sdim 297314564Sdim /// Check if the pointer type can be zero-initialized (in the C++ sense) 298314564Sdim /// with an LLVM zeroinitializer. 299314564Sdim bool isPointerZeroInitializable(QualType T); 300314564Sdim 301212904Sdim /// IsZeroInitializable - Return whether a record type can be 302212904Sdim /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 303288943Sdim bool isZeroInitializable(const RecordDecl *RD); 304341825Sdim 305224145Sdim bool isRecordLayoutComplete(const Type *Ty) const; 306224145Sdim bool noRecordsBeingLaidOut() const { 307224145Sdim return RecordsBeingLaidOut.empty(); 308224145Sdim } 309224145Sdim bool isRecordBeingLaidOut(const Type *Ty) const { 310224145Sdim return RecordsBeingLaidOut.count(Ty); 311224145Sdim } 312341825Sdim 313193326Sed}; 314193326Sed 315193326Sed} // end namespace CodeGen 316193326Sed} // end namespace clang 317193326Sed 318193326Sed#endif 319