1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This is the code that handles AST -> LLVM type lowering. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H 14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H 15 16 #include "CGCall.h" 17 #include "clang/Basic/ABI.h" 18 #include "clang/CodeGen/CGFunctionInfo.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/IR/Module.h" 21 22 namespace llvm { 23 class FunctionType; 24 class DataLayout; 25 class Type; 26 class LLVMContext; 27 class StructType; 28 } 29 30 namespace clang { 31 class ASTContext; 32 template <typename> class CanQual; 33 class CXXConstructorDecl; 34 class CXXMethodDecl; 35 class CodeGenOptions; 36 class FunctionProtoType; 37 class QualType; 38 class RecordDecl; 39 class TagDecl; 40 class TargetInfo; 41 class Type; 42 typedef CanQual<Type> CanQualType; 43 class GlobalDecl; 44 45 namespace CodeGen { 46 class ABIInfo; 47 class CGCXXABI; 48 class CGRecordLayout; 49 class CodeGenModule; 50 class RequiredArgs; 51 52 /// This class organizes the cross-module state that is used while lowering 53 /// AST types to LLVM types. 54 class CodeGenTypes { 55 CodeGenModule &CGM; 56 // Some of this stuff should probably be left on the CGM. 57 ASTContext &Context; 58 llvm::Module &TheModule; 59 const TargetInfo &Target; 60 CGCXXABI &TheCXXABI; 61 62 // This should not be moved earlier, since its initialization depends on some 63 // of the previous reference members being already initialized 64 const ABIInfo &TheABIInfo; 65 66 /// The opaque type map for Objective-C interfaces. All direct 67 /// manipulation is done by the runtime interfaces, which are 68 /// responsible for coercing to the appropriate type; these opaque 69 /// types are never refined. 70 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes; 71 72 /// Maps clang struct type with corresponding record layout info. 73 llvm::DenseMap<const Type*, std::unique_ptr<CGRecordLayout>> CGRecordLayouts; 74 75 /// Contains the LLVM IR type for any converted RecordDecl. 76 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes; 77 78 /// Hold memoized CGFunctionInfo results. 79 llvm::FoldingSet<CGFunctionInfo> FunctionInfos{FunctionInfosLog2InitSize}; 80 81 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed; 82 83 /// True if we didn't layout a function due to a being inside 84 /// a recursive struct conversion, set this to true. 85 bool SkippedLayout; 86 87 /// True if any instance of long double types are used. 88 bool LongDoubleReferenced; 89 90 /// This map keeps cache of llvm::Types and maps clang::Type to 91 /// corresponding llvm::Type. 92 llvm::DenseMap<const Type *, llvm::Type *> TypeCache; 93 94 llvm::DenseMap<const Type *, llvm::Type *> RecordsWithOpaqueMemberPointers; 95 96 static constexpr unsigned FunctionInfosLog2InitSize = 9; 97 /// Helper for ConvertType. 98 llvm::Type *ConvertFunctionTypeInternal(QualType FT); 99 100 public: 101 CodeGenTypes(CodeGenModule &cgm); 102 ~CodeGenTypes(); 103 104 const llvm::DataLayout &getDataLayout() const { 105 return TheModule.getDataLayout(); 106 } 107 CodeGenModule &getCGM() const { return CGM; } 108 ASTContext &getContext() const { return Context; } 109 const ABIInfo &getABIInfo() const { return TheABIInfo; } 110 const TargetInfo &getTarget() const { return Target; } 111 CGCXXABI &getCXXABI() const { return TheCXXABI; } 112 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 113 const CodeGenOptions &getCodeGenOpts() const; 114 115 /// Convert clang calling convention to LLVM callilng convention. 116 unsigned ClangCallConvToLLVMCallConv(CallingConv CC); 117 118 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR 119 /// qualification. 120 CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD); 121 122 /// ConvertType - Convert type T into a llvm::Type. 123 llvm::Type *ConvertType(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 llvm::Type *ConvertTypeForMem(QualType T); 130 131 /// Check whether the given type needs to be laid out in memory 132 /// using an opaque byte-array type because its load/store type 133 /// does not have the correct alloc size in the LLVM data layout. 134 /// If this is false, the load/store type (convertTypeForLoadStore) 135 /// and memory representation type (ConvertTypeForMem) will 136 /// be the same type. 137 bool typeRequiresSplitIntoByteArray(QualType ASTTy, 138 llvm::Type *LLVMTy = nullptr); 139 140 /// Given that T is a scalar type, return the IR type that should 141 /// be used for load and store operations. For example, this might 142 /// be i8 for _Bool or i96 for _BitInt(65). The store size of the 143 /// load/store type (as reported by LLVM's data layout) is always 144 /// the same as the alloc size of the memory representation type 145 /// returned by ConvertTypeForMem. 146 /// 147 /// As an optimization, if you already know the scalar value type 148 /// for T (as would be returned by ConvertType), you can pass 149 /// it as the second argument so that it does not need to be 150 /// recomputed in common cases where the value type and 151 /// load/store type are the same. 152 llvm::Type *convertTypeForLoadStore(QualType T, llvm::Type *LLVMTy = nullptr); 153 154 /// GetFunctionType - Get the LLVM function type for \arg Info. 155 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info); 156 157 llvm::FunctionType *GetFunctionType(GlobalDecl GD); 158 159 /// isFuncTypeConvertible - Utility to check whether a function type can 160 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 161 /// type). 162 bool isFuncTypeConvertible(const FunctionType *FT); 163 bool isFuncParamTypeConvertible(QualType Ty); 164 165 /// Determine if a C++ inheriting constructor should have parameters matching 166 /// those of its inherited constructor. 167 bool inheritingCtorHasParams(const InheritedConstructor &Inherited, 168 CXXCtorType Type); 169 170 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 171 /// given a CXXMethodDecl. If the method to has an incomplete return type, 172 /// and/or incomplete argument types, this will return the opaque type. 173 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD); 174 175 const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 176 177 /// UpdateCompletedType - When we find the full definition for a TagDecl, 178 /// replace the 'opaque' type we previously made for it if applicable. 179 void UpdateCompletedType(const TagDecl *TD); 180 181 /// Remove stale types from the type cache when an inheritance model 182 /// gets assigned to a class. 183 void RefreshTypeCacheForClass(const CXXRecordDecl *RD); 184 185 // The arrangement methods are split into three families: 186 // - those meant to drive the signature and prologue/epilogue 187 // of a function declaration or definition, 188 // - those meant for the computation of the LLVM type for an abstract 189 // appearance of a function, and 190 // - those meant for performing the IR-generation of a call. 191 // They differ mainly in how they deal with optional (i.e. variadic) 192 // arguments, as well as unprototyped functions. 193 // 194 // Key points: 195 // - The CGFunctionInfo for emitting a specific call site must include 196 // entries for the optional arguments. 197 // - The function type used at the call site must reflect the formal 198 // signature of the declaration being called, or else the call will 199 // go awry. 200 // - For the most part, unprototyped functions are called by casting to 201 // a formal signature inferred from the specific argument types used 202 // at the call-site. However, some targets (e.g. x86-64) screw with 203 // this for compatibility reasons. 204 205 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD); 206 207 /// Given a function info for a declaration, return the function info 208 /// for a call with the given arguments. 209 /// 210 /// Often this will be able to simply return the declaration info. 211 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI, 212 const CallArgList &args); 213 214 /// Free functions are functions that are compatible with an ordinary 215 /// C function pointer type. 216 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD); 217 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args, 218 const FunctionType *Ty, 219 bool ChainCall); 220 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty); 221 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty); 222 223 /// A nullary function is a freestanding function of type 'void ()'. 224 /// This method works for both calls and declarations. 225 const CGFunctionInfo &arrangeNullaryFunction(); 226 227 /// A builtin function is a freestanding function using the default 228 /// C conventions. 229 const CGFunctionInfo & 230 arrangeBuiltinFunctionDeclaration(QualType resultType, 231 const FunctionArgList &args); 232 const CGFunctionInfo & 233 arrangeBuiltinFunctionDeclaration(CanQualType resultType, 234 ArrayRef<CanQualType> argTypes); 235 const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType, 236 const CallArgList &args); 237 238 /// Objective-C methods are C functions with some implicit parameters. 239 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD); 240 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 241 QualType receiverType); 242 const CGFunctionInfo &arrangeUnprototypedObjCMessageSend( 243 QualType returnType, 244 const CallArgList &args); 245 246 /// Block invocation functions are C functions with an implicit parameter. 247 const CGFunctionInfo &arrangeBlockFunctionDeclaration( 248 const FunctionProtoType *type, 249 const FunctionArgList &args); 250 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args, 251 const FunctionType *type); 252 253 /// C++ methods have some special rules and also have implicit parameters. 254 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD); 255 const CGFunctionInfo &arrangeCXXStructorDeclaration(GlobalDecl GD); 256 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args, 257 const CXXConstructorDecl *D, 258 CXXCtorType CtorKind, 259 unsigned ExtraPrefixArgs, 260 unsigned ExtraSuffixArgs, 261 bool PassProtoArgs = true); 262 263 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args, 264 const FunctionProtoType *type, 265 RequiredArgs required, 266 unsigned numPrefixArgs); 267 const CGFunctionInfo & 268 arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD); 269 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD, 270 CXXCtorType CT); 271 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD, 272 const FunctionProtoType *FTP, 273 const CXXMethodDecl *MD); 274 275 /// "Arrange" the LLVM information for a call or type with the given 276 /// signature. This is largely an internal method; other clients 277 /// should use one of the above routines, which ultimately defer to 278 /// this. 279 /// 280 /// \param argTypes - must all actually be canonical as params 281 const CGFunctionInfo &arrangeLLVMFunctionInfo( 282 CanQualType returnType, FnInfoOpts opts, ArrayRef<CanQualType> argTypes, 283 FunctionType::ExtInfo info, 284 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, 285 RequiredArgs args); 286 287 /// Compute a new LLVM record layout object for the given record. 288 std::unique_ptr<CGRecordLayout> ComputeRecordLayout(const RecordDecl *D, 289 llvm::StructType *Ty); 290 291 /// addRecordTypeName - Compute a name from the given record decl with an 292 /// optional suffix and name the given LLVM type using it. 293 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, 294 StringRef suffix); 295 296 297 public: // These are internal details of CGT that shouldn't be used externally. 298 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 299 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD); 300 301 /// getExpandedTypes - Expand the type \arg Ty into the LLVM 302 /// argument types it would be passed as. See ABIArgInfo::Expand. 303 void getExpandedTypes(QualType Ty, 304 SmallVectorImpl<llvm::Type *>::iterator &TI); 305 306 /// IsZeroInitializable - Return whether a type can be 307 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 308 bool isZeroInitializable(QualType T); 309 310 /// Check if the pointer type can be zero-initialized (in the C++ sense) 311 /// with an LLVM zeroinitializer. 312 bool isPointerZeroInitializable(QualType T); 313 314 /// IsZeroInitializable - Return whether a record type can be 315 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 316 bool isZeroInitializable(const RecordDecl *RD); 317 318 bool isLongDoubleReferenced() const { return LongDoubleReferenced; } 319 bool isRecordLayoutComplete(const Type *Ty) const; 320 unsigned getTargetAddressSpace(QualType T) const; 321 }; 322 323 } // end namespace CodeGen 324 } // end namespace clang 325 326 #endif 327