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