CodeGenTypes.h 15 KB

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