CodeGenTypes.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
  13. #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
  14. #include "CGCall.h"
  15. #include "clang/Basic/ABI.h"
  16. #include "clang/CodeGen/CGFunctionInfo.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/IR/Module.h"
  19. namespace llvm {
  20. class FunctionType;
  21. class DataLayout;
  22. class Type;
  23. class LLVMContext;
  24. class StructType;
  25. }
  26. namespace clang {
  27. class ASTContext;
  28. template <typename> class CanQual;
  29. class CXXConstructorDecl;
  30. class CXXDestructorDecl;
  31. class CXXMethodDecl;
  32. class CodeGenOptions;
  33. class FieldDecl;
  34. class FunctionProtoType;
  35. class ObjCInterfaceDecl;
  36. class ObjCIvarDecl;
  37. class PointerType;
  38. class QualType;
  39. class RecordDecl;
  40. class TagDecl;
  41. class TargetInfo;
  42. class Type;
  43. typedef CanQual<Type> CanQualType;
  44. class GlobalDecl;
  45. namespace CodeGen {
  46. class ABIInfo;
  47. class CGCXXABI;
  48. class CGRecordLayout;
  49. class CodeGenModule;
  50. class RequiredArgs;
  51. /// This class organizes the cross-module state that is used while lowering
  52. /// AST types to LLVM types.
  53. class CodeGenTypes {
  54. CodeGenModule &CGM;
  55. // Some of this stuff should probably be left on the CGM.
  56. ASTContext &Context;
  57. llvm::Module &TheModule;
  58. const TargetInfo &Target;
  59. CGCXXABI &TheCXXABI;
  60. // This should not be moved earlier, since its initialization depends on some
  61. // of the previous reference members being already initialized
  62. const ABIInfo &TheABIInfo;
  63. /// The opaque type map for Objective-C interfaces. All direct
  64. /// manipulation is done by the runtime interfaces, which are
  65. /// responsible for coercing to the appropriate type; these opaque
  66. /// types are never refined.
  67. llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
  68. /// Maps clang struct type with corresponding record layout info.
  69. llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
  70. /// Contains the LLVM IR type for any converted RecordDecl.
  71. llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
  72. /// Hold memoized CGFunctionInfo results.
  73. llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
  74. /// This set keeps track of records that we're currently converting
  75. /// to an IR type. For example, when converting:
  76. /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
  77. /// types will be in this set.
  78. llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
  79. llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
  80. /// True if we didn't layout a function due to a being inside
  81. /// a recursive struct conversion, set this to true.
  82. bool SkippedLayout;
  83. SmallVector<const RecordDecl *, 8> DeferredRecords;
  84. /// This map keeps cache of llvm::Types and maps clang::Type to
  85. /// corresponding llvm::Type.
  86. llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
  87. llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers;
  88. /// Helper for ConvertType.
  89. llvm::Type *ConvertFunctionTypeInternal(QualType FT);
  90. public:
  91. CodeGenTypes(CodeGenModule &cgm);
  92. ~CodeGenTypes();
  93. const llvm::DataLayout &getDataLayout() const {
  94. return TheModule.getDataLayout();
  95. }
  96. ASTContext &getContext() const { return Context; }
  97. const ABIInfo &getABIInfo() const { return TheABIInfo; }
  98. const TargetInfo &getTarget() const { return Target; }
  99. CGCXXABI &getCXXABI() const { return TheCXXABI; }
  100. llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
  101. const CodeGenOptions &getCodeGenOpts() const;
  102. /// Convert clang calling convention to LLVM callilng convention.
  103. unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
  104. /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
  105. /// qualification.
  106. CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD);
  107. /// ConvertType - Convert type T into a llvm::Type.
  108. llvm::Type *ConvertType(QualType T);
  109. /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
  110. /// ConvertType in that it is used to convert to the memory representation for
  111. /// a type. For example, the scalar representation for _Bool is i1, but the
  112. /// memory representation is usually i8 or i32, depending on the target.
  113. llvm::Type *ConvertTypeForMem(QualType T);
  114. /// GetFunctionType - Get the LLVM function type for \arg Info.
  115. llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
  116. llvm::FunctionType *GetFunctionType(GlobalDecl GD);
  117. /// isFuncTypeConvertible - Utility to check whether a function type can
  118. /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
  119. /// type).
  120. bool isFuncTypeConvertible(const FunctionType *FT);
  121. bool isFuncParamTypeConvertible(QualType Ty);
  122. /// Determine if a C++ inheriting constructor should have parameters matching
  123. /// those of its inherited constructor.
  124. bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
  125. CXXCtorType Type);
  126. /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
  127. /// given a CXXMethodDecl. If the method to has an incomplete return type,
  128. /// and/or incomplete argument types, this will return the opaque type.
  129. llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
  130. const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
  131. /// UpdateCompletedType - When we find the full definition for a TagDecl,
  132. /// replace the 'opaque' type we previously made for it if applicable.
  133. void UpdateCompletedType(const TagDecl *TD);
  134. /// Remove stale types from the type cache when an inheritance model
  135. /// gets assigned to a class.
  136. void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
  137. // The arrangement methods are split into three families:
  138. // - those meant to drive the signature and prologue/epilogue
  139. // of a function declaration or definition,
  140. // - those meant for the computation of the LLVM type for an abstract
  141. // appearance of a function, and
  142. // - those meant for performing the IR-generation of a call.
  143. // They differ mainly in how they deal with optional (i.e. variadic)
  144. // arguments, as well as unprototyped functions.
  145. //
  146. // Key points:
  147. // - The CGFunctionInfo for emitting a specific call site must include
  148. // entries for the optional arguments.
  149. // - The function type used at the call site must reflect the formal
  150. // signature of the declaration being called, or else the call will
  151. // go awry.
  152. // - For the most part, unprototyped functions are called by casting to
  153. // a formal signature inferred from the specific argument types used
  154. // at the call-site. However, some targets (e.g. x86-64) screw with
  155. // this for compatibility reasons.
  156. const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
  157. /// Given a function info for a declaration, return the function info
  158. /// for a call with the given arguments.
  159. ///
  160. /// Often this will be able to simply return the declaration info.
  161. const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
  162. const CallArgList &args);
  163. /// Free functions are functions that are compatible with an ordinary
  164. /// C function pointer type.
  165. const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
  166. const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
  167. const FunctionType *Ty,
  168. bool ChainCall);
  169. const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
  170. const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
  171. /// A nullary function is a freestanding function of type 'void ()'.
  172. /// This method works for both calls and declarations.
  173. const CGFunctionInfo &arrangeNullaryFunction();
  174. /// A builtin function is a freestanding function using the default
  175. /// C conventions.
  176. const CGFunctionInfo &
  177. arrangeBuiltinFunctionDeclaration(QualType resultType,
  178. const FunctionArgList &args);
  179. const CGFunctionInfo &
  180. arrangeBuiltinFunctionDeclaration(CanQualType resultType,
  181. ArrayRef<CanQualType> argTypes);
  182. const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
  183. const CallArgList &args);
  184. /// Objective-C methods are C functions with some implicit parameters.
  185. const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
  186. const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
  187. QualType receiverType);
  188. const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
  189. QualType returnType,
  190. const CallArgList &args);
  191. /// Block invocation functions are C functions with an implicit parameter.
  192. const CGFunctionInfo &arrangeBlockFunctionDeclaration(
  193. const FunctionProtoType *type,
  194. const FunctionArgList &args);
  195. const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
  196. const FunctionType *type);
  197. /// C++ methods have some special rules and also have implicit parameters.
  198. const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
  199. const CGFunctionInfo &arrangeCXXStructorDeclaration(GlobalDecl GD);
  200. const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
  201. const CXXConstructorDecl *D,
  202. CXXCtorType CtorKind,
  203. unsigned ExtraPrefixArgs,
  204. unsigned ExtraSuffixArgs,
  205. bool PassProtoArgs = true);
  206. const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
  207. const FunctionProtoType *type,
  208. RequiredArgs required,
  209. unsigned numPrefixArgs);
  210. const CGFunctionInfo &
  211. arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD);
  212. const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
  213. CXXCtorType CT);
  214. const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
  215. const FunctionProtoType *FTP,
  216. const CXXMethodDecl *MD);
  217. /// "Arrange" the LLVM information for a call or type with the given
  218. /// signature. This is largely an internal method; other clients
  219. /// should use one of the above routines, which ultimately defer to
  220. /// this.
  221. ///
  222. /// \param argTypes - must all actually be canonical as params
  223. const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
  224. bool instanceMethod,
  225. bool chainCall,
  226. ArrayRef<CanQualType> argTypes,
  227. FunctionType::ExtInfo info,
  228. ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
  229. RequiredArgs args);
  230. /// Compute a new LLVM record layout object for the given record.
  231. CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
  232. llvm::StructType *Ty);
  233. /// addRecordTypeName - Compute a name from the given record decl with an
  234. /// optional suffix and name the given LLVM type using it.
  235. void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
  236. StringRef suffix);
  237. public: // These are internal details of CGT that shouldn't be used externally.
  238. /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
  239. llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
  240. /// getExpandedTypes - Expand the type \arg Ty into the LLVM
  241. /// argument types it would be passed as. See ABIArgInfo::Expand.
  242. void getExpandedTypes(QualType Ty,
  243. SmallVectorImpl<llvm::Type *>::iterator &TI);
  244. /// IsZeroInitializable - Return whether a type can be
  245. /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
  246. bool isZeroInitializable(QualType T);
  247. /// Check if the pointer type can be zero-initialized (in the C++ sense)
  248. /// with an LLVM zeroinitializer.
  249. bool isPointerZeroInitializable(QualType T);
  250. /// IsZeroInitializable - Return whether a record type can be
  251. /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
  252. bool isZeroInitializable(const RecordDecl *RD);
  253. bool isRecordLayoutComplete(const Type *Ty) const;
  254. bool noRecordsBeingLaidOut() const {
  255. return RecordsBeingLaidOut.empty();
  256. }
  257. bool isRecordBeingLaidOut(const Type *Ty) const {
  258. return RecordsBeingLaidOut.count(Ty);
  259. }
  260. };
  261. } // end namespace CodeGen
  262. } // end namespace clang
  263. #endif