CodeGenTypes.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 CLANG_CODEGEN_CODEGENTYPES_H
  14. #define CLANG_CODEGEN_CODEGENTYPES_H
  15. #include "CGCall.h"
  16. #include "clang/AST/GlobalDecl.h"
  17. #include "llvm/Module.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include <vector>
  20. namespace llvm {
  21. class FunctionType;
  22. class Module;
  23. class TargetData;
  24. class Type;
  25. class LLVMContext;
  26. class StructType;
  27. }
  28. namespace clang {
  29. class ABIInfo;
  30. class ASTContext;
  31. template <typename> class CanQual;
  32. class CXXConstructorDecl;
  33. class CXXDestructorDecl;
  34. class CXXMethodDecl;
  35. class CodeGenOptions;
  36. class FieldDecl;
  37. class FunctionProtoType;
  38. class ObjCInterfaceDecl;
  39. class ObjCIvarDecl;
  40. class PointerType;
  41. class QualType;
  42. class RecordDecl;
  43. class TagDecl;
  44. class TargetInfo;
  45. class Type;
  46. typedef CanQual<Type> CanQualType;
  47. namespace CodeGen {
  48. class CGCXXABI;
  49. class CGRecordLayout;
  50. class CodeGenModule;
  51. class RequiredArgs;
  52. /// CodeGenTypes - This class organizes the cross-module state that is used
  53. /// while lowering AST types to LLVM types.
  54. class CodeGenTypes {
  55. // Some of this stuff should probably be left on the CGM.
  56. ASTContext &Context;
  57. const TargetInfo &Target;
  58. llvm::Module &TheModule;
  59. const llvm::TargetData &TheTargetData;
  60. const ABIInfo &TheABIInfo;
  61. CGCXXABI &TheCXXABI;
  62. const CodeGenOptions &CodeGenOpts;
  63. CodeGenModule &CGM;
  64. /// The opaque type map for Objective-C interfaces. All direct
  65. /// manipulation is done by the runtime interfaces, which are
  66. /// responsible for coercing to the appropriate type; these opaque
  67. /// types are never refined.
  68. llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
  69. /// CGRecordLayouts - This maps llvm struct type with corresponding
  70. /// record layout info.
  71. llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
  72. /// RecordDeclTypes - This contains the LLVM IR type for any converted
  73. /// RecordDecl.
  74. llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
  75. /// FunctionInfos - Hold memoized CGFunctionInfo results.
  76. llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
  77. /// RecordsBeingLaidOut - This set keeps track of records that we're currently
  78. /// converting to an IR type. For example, when converting:
  79. /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
  80. /// types will be in this set.
  81. llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
  82. llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
  83. /// SkippedLayout - 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. SmallVector<const RecordDecl *, 8> DeferredRecords;
  87. private:
  88. /// TypeCache - This map keeps cache of llvm::Types
  89. /// and maps llvm::Types to corresponding clang::Type.
  90. llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
  91. public:
  92. CodeGenTypes(CodeGenModule &CGM);
  93. ~CodeGenTypes();
  94. const llvm::TargetData &getTargetData() const { return TheTargetData; }
  95. const TargetInfo &getTarget() const { return Target; }
  96. ASTContext &getContext() const { return Context; }
  97. const ABIInfo &getABIInfo() const { return TheABIInfo; }
  98. const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
  99. CGCXXABI &getCXXABI() const { return TheCXXABI; }
  100. llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
  101. /// ConvertType - Convert type T into a llvm::Type.
  102. llvm::Type *ConvertType(QualType T);
  103. /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
  104. /// ConvertType in that it is used to convert to the memory representation for
  105. /// a type. For example, the scalar representation for _Bool is i1, but the
  106. /// memory representation is usually i8 or i32, depending on the target.
  107. llvm::Type *ConvertTypeForMem(QualType T);
  108. /// GetFunctionType - Get the LLVM function type for \arg Info.
  109. llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
  110. llvm::FunctionType *GetFunctionType(GlobalDecl GD);
  111. /// isFuncTypeConvertible - Utility to check whether a function type can
  112. /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
  113. /// type).
  114. bool isFuncTypeConvertible(const FunctionType *FT);
  115. bool isFuncTypeArgumentConvertible(QualType Ty);
  116. /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
  117. /// given a CXXMethodDecl. If the method to has an incomplete return type,
  118. /// and/or incomplete argument types, this will return the opaque type.
  119. llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
  120. const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
  121. /// UpdateCompletedType - When we find the full definition for a TagDecl,
  122. /// replace the 'opaque' type we previously made for it if applicable.
  123. void UpdateCompletedType(const TagDecl *TD);
  124. /// getNullaryFunctionInfo - Get the function info for a void()
  125. /// function with standard CC.
  126. const CGFunctionInfo &arrangeNullaryFunction();
  127. // The arrangement methods are split into three families:
  128. // - those meant to drive the signature and prologue/epilogue
  129. // of a function declaration or definition,
  130. // - those meant for the computation of the LLVM type for an abstract
  131. // appearance of a function, and
  132. // - those meant for performing the IR-generation of a call.
  133. // They differ mainly in how they deal with optional (i.e. variadic)
  134. // arguments, as well as unprototyped functions.
  135. //
  136. // Key points:
  137. // - The CGFunctionInfo for emitting a specific call site must include
  138. // entries for the optional arguments.
  139. // - The function type used at the call site must reflect the formal
  140. // signature of the declaration being called, or else the call will
  141. // go awry.
  142. // - For the most part, unprototyped functions are called by casting to
  143. // a formal signature inferred from the specific argument types used
  144. // at the call-site. However, some targets (e.g. x86-64) screw with
  145. // this for compatibility reasons.
  146. const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
  147. const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
  148. const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
  149. const FunctionArgList &Args,
  150. const FunctionType::ExtInfo &Info,
  151. bool isVariadic);
  152. const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
  153. const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
  154. QualType receiverType);
  155. const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
  156. const CGFunctionInfo &arrangeCXXConstructorDeclaration(
  157. const CXXConstructorDecl *D,
  158. CXXCtorType Type);
  159. const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
  160. CXXDtorType Type);
  161. const CGFunctionInfo &arrangeFunctionCall(const CallArgList &Args,
  162. const FunctionType *Ty);
  163. const CGFunctionInfo &arrangeFunctionCall(QualType ResTy,
  164. const CallArgList &args,
  165. const FunctionType::ExtInfo &info,
  166. RequiredArgs required);
  167. const CGFunctionInfo &arrangeFunctionType(CanQual<FunctionProtoType> Ty);
  168. const CGFunctionInfo &arrangeFunctionType(CanQual<FunctionNoProtoType> Ty);
  169. const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
  170. const FunctionProtoType *FTP);
  171. /// Retrieves the ABI information for the given function signature.
  172. /// This is the "core" routine to which all the others defer.
  173. ///
  174. /// \param argTypes - must all actually be canonical as params
  175. const CGFunctionInfo &arrangeFunctionType(CanQualType returnType,
  176. ArrayRef<CanQualType> argTypes,
  177. const FunctionType::ExtInfo &info,
  178. RequiredArgs args);
  179. /// \brief Compute a new LLVM record layout object for the given record.
  180. CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
  181. llvm::StructType *Ty);
  182. /// addRecordTypeName - Compute a name from the given record decl with an
  183. /// optional suffix and name the given LLVM type using it.
  184. void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
  185. StringRef suffix);
  186. public: // These are internal details of CGT that shouldn't be used externally.
  187. /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
  188. llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
  189. /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
  190. /// argument types it would be passed as on the provided vector \arg
  191. /// ArgTys. See ABIArgInfo::Expand.
  192. void GetExpandedTypes(QualType type,
  193. SmallVectorImpl<llvm::Type*> &expanded);
  194. /// IsZeroInitializable - Return whether a type can be
  195. /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
  196. bool isZeroInitializable(QualType T);
  197. /// IsZeroInitializable - Return whether a record type can be
  198. /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
  199. bool isZeroInitializable(const CXXRecordDecl *RD);
  200. bool isRecordLayoutComplete(const Type *Ty) const;
  201. bool noRecordsBeingLaidOut() const {
  202. return RecordsBeingLaidOut.empty();
  203. }
  204. bool isRecordBeingLaidOut(const Type *Ty) const {
  205. return RecordsBeingLaidOut.count(Ty);
  206. }
  207. };
  208. } // end namespace CodeGen
  209. } // end namespace clang
  210. #endif