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 CXXDestructorDecl; 35 class CXXMethodDecl; 36 class CodeGenOptions; 37 class FieldDecl; 38 class FunctionProtoType; 39 class ObjCInterfaceDecl; 40 class ObjCIvarDecl; 41 class PointerType; 42 class QualType; 43 class RecordDecl; 44 class TagDecl; 45 class TargetInfo; 46 class Type; 47 typedef CanQual<Type> CanQualType; 48 class GlobalDecl; 49 50 namespace CodeGen { 51 class ABIInfo; 52 class CGCXXABI; 53 class CGRecordLayout; 54 class CodeGenModule; 55 class RequiredArgs; 56 57 enum class StructorType { 58 Complete, // constructor or destructor 59 Base, // constructor or destructor 60 Deleting // destructor only 61 }; 62 63 inline CXXCtorType toCXXCtorType(StructorType T) { 64 switch (T) { 65 case StructorType::Complete: 66 return Ctor_Complete; 67 case StructorType::Base: 68 return Ctor_Base; 69 case StructorType::Deleting: 70 llvm_unreachable("cannot have a deleting ctor"); 71 } 72 llvm_unreachable("not a StructorType"); 73 } 74 75 inline StructorType getFromCtorType(CXXCtorType T) { 76 switch (T) { 77 case Ctor_Complete: 78 return StructorType::Complete; 79 case Ctor_Base: 80 return StructorType::Base; 81 case Ctor_Comdat: 82 llvm_unreachable("not expecting a COMDAT"); 83 case Ctor_CopyingClosure: 84 case Ctor_DefaultClosure: 85 llvm_unreachable("not expecting a closure"); 86 } 87 llvm_unreachable("not a CXXCtorType"); 88 } 89 90 inline CXXDtorType toCXXDtorType(StructorType T) { 91 switch (T) { 92 case StructorType::Complete: 93 return Dtor_Complete; 94 case StructorType::Base: 95 return Dtor_Base; 96 case StructorType::Deleting: 97 return Dtor_Deleting; 98 } 99 llvm_unreachable("not a StructorType"); 100 } 101 102 inline StructorType getFromDtorType(CXXDtorType T) { 103 switch (T) { 104 case Dtor_Deleting: 105 return StructorType::Deleting; 106 case Dtor_Complete: 107 return StructorType::Complete; 108 case Dtor_Base: 109 return StructorType::Base; 110 case Dtor_Comdat: 111 llvm_unreachable("not expecting a COMDAT"); 112 } 113 llvm_unreachable("not a CXXDtorType"); 114 } 115 116 /// This class organizes the cross-module state that is used while lowering 117 /// AST types to LLVM types. 118 class CodeGenTypes { 119 CodeGenModule &CGM; 120 // Some of this stuff should probably be left on the CGM. 121 ASTContext &Context; 122 llvm::Module &TheModule; 123 const TargetInfo &Target; 124 CGCXXABI &TheCXXABI; 125 126 // This should not be moved earlier, since its initialization depends on some 127 // of the previous reference members being already initialized 128 const ABIInfo &TheABIInfo; 129 130 /// The opaque type map for Objective-C interfaces. All direct 131 /// manipulation is done by the runtime interfaces, which are 132 /// responsible for coercing to the appropriate type; these opaque 133 /// types are never refined. 134 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes; 135 136 /// Maps clang struct type with corresponding record layout info. 137 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts; 138 139 /// Contains the LLVM IR type for any converted RecordDecl. 140 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes; 141 142 /// Hold memoized CGFunctionInfo results. 143 llvm::FoldingSet<CGFunctionInfo> FunctionInfos; 144 145 /// This set keeps track of records that we're currently converting 146 /// to an IR type. For example, when converting: 147 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B' 148 /// types will be in this set. 149 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut; 150 151 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed; 152 153 /// True if we didn't layout a function due to a being inside 154 /// a recursive struct conversion, set this to true. 155 bool SkippedLayout; 156 157 SmallVector<const RecordDecl *, 8> DeferredRecords; 158 159 /// This map keeps cache of llvm::Types and maps clang::Type to 160 /// corresponding llvm::Type. 161 llvm::DenseMap<const Type *, llvm::Type *> TypeCache; 162 163 llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers; 164 165 /// Helper for ConvertType. 166 llvm::Type *ConvertFunctionTypeInternal(QualType FT); 167 168 public: 169 CodeGenTypes(CodeGenModule &cgm); 170 ~CodeGenTypes(); 171 172 const llvm::DataLayout &getDataLayout() const { 173 return TheModule.getDataLayout(); 174 } 175 ASTContext &getContext() const { return Context; } 176 const ABIInfo &getABIInfo() const { return TheABIInfo; } 177 const TargetInfo &getTarget() const { return Target; } 178 CGCXXABI &getCXXABI() const { return TheCXXABI; } 179 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); } 180 const CodeGenOptions &getCodeGenOpts() const; 181 182 /// Convert clang calling convention to LLVM callilng convention. 183 unsigned ClangCallConvToLLVMCallConv(CallingConv CC); 184 185 /// ConvertType - Convert type T into a llvm::Type. 186 llvm::Type *ConvertType(QualType T); 187 188 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 189 /// ConvertType in that it is used to convert to the memory representation for 190 /// a type. For example, the scalar representation for _Bool is i1, but the 191 /// memory representation is usually i8 or i32, depending on the target. 192 llvm::Type *ConvertTypeForMem(QualType T); 193 194 /// GetFunctionType - Get the LLVM function type for \arg Info. 195 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info); 196 197 llvm::FunctionType *GetFunctionType(GlobalDecl GD); 198 199 /// isFuncTypeConvertible - Utility to check whether a function type can 200 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag 201 /// type). 202 bool isFuncTypeConvertible(const FunctionType *FT); 203 bool isFuncParamTypeConvertible(QualType Ty); 204 205 /// Determine if a C++ inheriting constructor should have parameters matching 206 /// those of its inherited constructor. 207 bool inheritingCtorHasParams(const InheritedConstructor &Inherited, 208 CXXCtorType Type); 209 210 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, 211 /// given a CXXMethodDecl. If the method to has an incomplete return type, 212 /// and/or incomplete argument types, this will return the opaque type. 213 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD); 214 215 const CGRecordLayout &getCGRecordLayout(const RecordDecl*); 216 217 /// UpdateCompletedType - When we find the full definition for a TagDecl, 218 /// replace the 'opaque' type we previously made for it if applicable. 219 void UpdateCompletedType(const TagDecl *TD); 220 221 /// Remove stale types from the type cache when an inheritance model 222 /// gets assigned to a class. 223 void RefreshTypeCacheForClass(const CXXRecordDecl *RD); 224 225 // The arrangement methods are split into three families: 226 // - those meant to drive the signature and prologue/epilogue 227 // of a function declaration or definition, 228 // - those meant for the computation of the LLVM type for an abstract 229 // appearance of a function, and 230 // - those meant for performing the IR-generation of a call. 231 // They differ mainly in how they deal with optional (i.e. variadic) 232 // arguments, as well as unprototyped functions. 233 // 234 // Key points: 235 // - The CGFunctionInfo for emitting a specific call site must include 236 // entries for the optional arguments. 237 // - The function type used at the call site must reflect the formal 238 // signature of the declaration being called, or else the call will 239 // go awry. 240 // - For the most part, unprototyped functions are called by casting to 241 // a formal signature inferred from the specific argument types used 242 // at the call-site. However, some targets (e.g. x86-64) screw with 243 // this for compatibility reasons. 244 245 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD); 246 247 /// Given a function info for a declaration, return the function info 248 /// for a call with the given arguments. 249 /// 250 /// Often this will be able to simply return the declaration info. 251 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI, 252 const CallArgList &args); 253 254 /// Free functions are functions that are compatible with an ordinary 255 /// C function pointer type. 256 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD); 257 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args, 258 const FunctionType *Ty, 259 bool ChainCall); 260 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty); 261 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty); 262 263 /// A nullary function is a freestanding function of type 'void ()'. 264 /// This method works for both calls and declarations. 265 const CGFunctionInfo &arrangeNullaryFunction(); 266 267 /// A builtin function is a freestanding function using the default 268 /// C conventions. 269 const CGFunctionInfo & 270 arrangeBuiltinFunctionDeclaration(QualType resultType, 271 const FunctionArgList &args); 272 const CGFunctionInfo & 273 arrangeBuiltinFunctionDeclaration(CanQualType resultType, 274 ArrayRef<CanQualType> argTypes); 275 const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType, 276 const CallArgList &args); 277 278 /// Objective-C methods are C functions with some implicit parameters. 279 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD); 280 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 281 QualType receiverType); 282 const CGFunctionInfo &arrangeUnprototypedObjCMessageSend( 283 QualType returnType, 284 const CallArgList &args); 285 286 /// Block invocation functions are C functions with an implicit parameter. 287 const CGFunctionInfo &arrangeBlockFunctionDeclaration( 288 const FunctionProtoType *type, 289 const FunctionArgList &args); 290 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args, 291 const FunctionType *type); 292 293 /// C++ methods have some special rules and also have implicit parameters. 294 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD); 295 const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD, 296 StructorType Type); 297 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args, 298 const CXXConstructorDecl *D, 299 CXXCtorType CtorKind, 300 unsigned ExtraPrefixArgs, 301 unsigned ExtraSuffixArgs, 302 bool PassProtoArgs = true); 303 304 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args, 305 const FunctionProtoType *type, 306 RequiredArgs required, 307 unsigned numPrefixArgs); 308 const CGFunctionInfo & 309 arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD); 310 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD, 311 CXXCtorType CT); 312 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD, 313 const FunctionProtoType *FTP, 314 const CXXMethodDecl *MD); 315 316 /// "Arrange" the LLVM information for a call or type with the given 317 /// signature. This is largely an internal method; other clients 318 /// should use one of the above routines, which ultimately defer to 319 /// this. 320 /// 321 /// \param argTypes - must all actually be canonical as params 322 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType, 323 bool instanceMethod, 324 bool chainCall, 325 ArrayRef<CanQualType> argTypes, 326 FunctionType::ExtInfo info, 327 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, 328 RequiredArgs args); 329 330 /// Compute a new LLVM record layout object for the given record. 331 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D, 332 llvm::StructType *Ty); 333 334 /// addRecordTypeName - Compute a name from the given record decl with an 335 /// optional suffix and name the given LLVM type using it. 336 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, 337 StringRef suffix); 338 339 340 public: // These are internal details of CGT that shouldn't be used externally. 341 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 342 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD); 343 344 /// getExpandedTypes - Expand the type \arg Ty into the LLVM 345 /// argument types it would be passed as. See ABIArgInfo::Expand. 346 void getExpandedTypes(QualType Ty, 347 SmallVectorImpl<llvm::Type *>::iterator &TI); 348 349 /// IsZeroInitializable - Return whether a type can be 350 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 351 bool isZeroInitializable(QualType T); 352 353 /// Check if the pointer type can be zero-initialized (in the C++ sense) 354 /// with an LLVM zeroinitializer. 355 bool isPointerZeroInitializable(QualType T); 356 357 /// IsZeroInitializable - Return whether a record type can be 358 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer. 359 bool isZeroInitializable(const RecordDecl *RD); 360 361 bool isRecordLayoutComplete(const Type *Ty) const; 362 bool noRecordsBeingLaidOut() const { 363 return RecordsBeingLaidOut.empty(); 364 } 365 bool isRecordBeingLaidOut(const Type *Ty) const { 366 return RecordsBeingLaidOut.count(Ty); 367 } 368 369 }; 370 371 } // end namespace CodeGen 372 } // end namespace clang 373 374 #endif 375