clang  19.0.0git
CodeGenTypes.h
Go to the documentation of this file.
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"
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 CXXMethodDecl;
35 class CodeGenOptions;
36 class FunctionProtoType;
37 class QualType;
38 class RecordDecl;
39 class TagDecl;
40 class TargetInfo;
41 class Type;
42 typedef CanQual<Type> CanQualType;
43 class GlobalDecl;
44 
45 namespace CodeGen {
46 class ABIInfo;
47 class CGCXXABI;
48 class CGRecordLayout;
49 class CodeGenModule;
50 class RequiredArgs;
51 
52 /// This class organizes the cross-module state that is used while lowering
53 /// AST types to LLVM types.
54 class CodeGenTypes {
55  CodeGenModule &CGM;
56  // Some of this stuff should probably be left on the CGM.
57  ASTContext &Context;
58  llvm::Module &TheModule;
59  const TargetInfo &Target;
60  CGCXXABI &TheCXXABI;
61 
62  // This should not be moved earlier, since its initialization depends on some
63  // of the previous reference members being already initialized
64  const ABIInfo &TheABIInfo;
65 
66  /// The opaque type map for Objective-C interfaces. All direct
67  /// manipulation is done by the runtime interfaces, which are
68  /// responsible for coercing to the appropriate type; these opaque
69  /// types are never refined.
70  llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
71 
72  /// Maps clang struct type with corresponding record layout info.
73  llvm::DenseMap<const Type*, std::unique_ptr<CGRecordLayout>> CGRecordLayouts;
74 
75  /// Contains the LLVM IR type for any converted RecordDecl.
76  llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
77 
78  /// Hold memoized CGFunctionInfo results.
79  llvm::FoldingSet<CGFunctionInfo> FunctionInfos{FunctionInfosLog2InitSize};
80 
81  llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
82 
83  /// 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 
87  /// True if any instance of long double types are used.
88  bool LongDoubleReferenced;
89 
90  /// This map keeps cache of llvm::Types and maps clang::Type to
91  /// corresponding llvm::Type.
92  llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
93 
94  llvm::DenseMap<const Type *, llvm::Type *> RecordsWithOpaqueMemberPointers;
95 
96  static constexpr unsigned FunctionInfosLog2InitSize = 9;
97  /// Helper for ConvertType.
98  llvm::Type *ConvertFunctionTypeInternal(QualType FT);
99 
100 public:
102  ~CodeGenTypes();
103 
104  const llvm::DataLayout &getDataLayout() const {
105  return TheModule.getDataLayout();
106  }
107  CodeGenModule &getCGM() const { return CGM; }
108  ASTContext &getContext() const { return Context; }
109  const ABIInfo &getABIInfo() const { return TheABIInfo; }
110  const TargetInfo &getTarget() const { return Target; }
111  CGCXXABI &getCXXABI() const { return TheCXXABI; }
112  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
113  const CodeGenOptions &getCodeGenOpts() const;
114 
115  /// Convert clang calling convention to LLVM callilng convention.
117 
118  /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
119  /// qualification.
121 
122  /// ConvertType - Convert type T into a llvm::Type.
123  llvm::Type *ConvertType(QualType T);
124 
125  /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
126  /// ConvertType in that it is used to convert to the memory representation for
127  /// a type. For example, the scalar representation for _Bool is i1, but the
128  /// memory representation is usually i8 or i32, depending on the target.
129  llvm::Type *ConvertTypeForMem(QualType T, bool ForBitField = false);
130 
131  /// ConvertSYCLJointMatrixINTELType - Convert SYCL joint_matrix type
132  /// which is represented as a pointer to a structure to LLVM extension type
133  /// with the parameters that follow SPIR-V JointMatrixINTEL type.
134  /// The expected representation is:
135  /// target("spirv.JointMatrixINTEL", %element_type, %rows%, %cols%, %scope%,
136  /// %use%, (optional) %element_type_interpretation%)
138 
139  /// ConvertSPVCooperativeMatrixType - Convert SYCL joint_matrix type
140  /// which is represented as a pointer to a structure to LLVM extension type
141  /// with the parameters that follow SPIR-V CooperativeMatrixKHR type.
142  /// The expected representation is:
143  /// target("spirv.CooperativeMatrixKHR", %element_type, %scope%, %rows%,
144  /// %cols%, %use%)
145  ///
147 
148  /// GetFunctionType - Get the LLVM function type for \arg Info.
149  llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
150 
151  llvm::FunctionType *GetFunctionType(GlobalDecl GD);
152 
153  /// isFuncTypeConvertible - Utility to check whether a function type can
154  /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
155  /// type).
156  bool isFuncTypeConvertible(const FunctionType *FT);
158 
159  /// Determine if a C++ inheriting constructor should have parameters matching
160  /// those of its inherited constructor.
161  bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
162  CXXCtorType Type);
163 
164  /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
165  /// given a CXXMethodDecl. If the method to has an incomplete return type,
166  /// and/or incomplete argument types, this will return the opaque type.
167  llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
168 
170 
171  /// UpdateCompletedType - When we find the full definition for a TagDecl,
172  /// replace the 'opaque' type we previously made for it if applicable.
173  void UpdateCompletedType(const TagDecl *TD);
174 
175  /// Remove stale types from the type cache when an inheritance model
176  /// gets assigned to a class.
177  void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
178 
179  // The arrangement methods are split into three families:
180  // - those meant to drive the signature and prologue/epilogue
181  // of a function declaration or definition,
182  // - those meant for the computation of the LLVM type for an abstract
183  // appearance of a function, and
184  // - those meant for performing the IR-generation of a call.
185  // They differ mainly in how they deal with optional (i.e. variadic)
186  // arguments, as well as unprototyped functions.
187  //
188  // Key points:
189  // - The CGFunctionInfo for emitting a specific call site must include
190  // entries for the optional arguments.
191  // - The function type used at the call site must reflect the formal
192  // signature of the declaration being called, or else the call will
193  // go awry.
194  // - For the most part, unprototyped functions are called by casting to
195  // a formal signature inferred from the specific argument types used
196  // at the call-site. However, some targets (e.g. x86-64) screw with
197  // this for compatibility reasons.
198 
200 
201  /// Given a function info for a declaration, return the function info
202  /// for a call with the given arguments.
203  ///
204  /// Often this will be able to simply return the declaration info.
205  const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
206  const CallArgList &args);
207 
208  /// Free functions are functions that are compatible with an ordinary
209  /// C function pointer type.
212  const FunctionType *Ty,
213  bool ChainCall);
216 
217  /// A nullary function is a freestanding function of type 'void ()'.
218  /// This method works for both calls and declarations.
220 
221  /// A builtin function is a freestanding function using the default
222  /// C conventions.
223  const CGFunctionInfo &
225  const FunctionArgList &args);
226  const CGFunctionInfo &
228  ArrayRef<CanQualType> argTypes);
230  const CallArgList &args);
231 
232  /// Objective-C methods are C functions with some implicit parameters.
235  QualType receiverType);
237  QualType returnType,
238  const CallArgList &args);
239 
240  /// Block invocation functions are C functions with an implicit parameter.
242  const FunctionProtoType *type,
243  const FunctionArgList &args);
245  const FunctionType *type);
246 
247  /// C++ methods have some special rules and also have implicit parameters.
251  const CXXConstructorDecl *D,
252  CXXCtorType CtorKind,
253  unsigned ExtraPrefixArgs,
254  unsigned ExtraSuffixArgs,
255  bool PassProtoArgs = true);
256 
258  const FunctionProtoType *type,
259  RequiredArgs required,
260  unsigned numPrefixArgs);
261  const CGFunctionInfo &
264  CXXCtorType CT);
266  const FunctionProtoType *FTP,
267  const CXXMethodDecl *MD);
268 
269  /// "Arrange" the LLVM information for a call or type with the given
270  /// signature. This is largely an internal method; other clients
271  /// should use one of the above routines, which ultimately defer to
272  /// this.
273  ///
274  /// \param argTypes - must all actually be canonical as params
276  CanQualType returnType, FnInfoOpts opts, ArrayRef<CanQualType> argTypes,
279  RequiredArgs args);
280 
281  /// Compute a new LLVM record layout object for the given record.
282  std::unique_ptr<CGRecordLayout> ComputeRecordLayout(const RecordDecl *D,
283  llvm::StructType *Ty);
284 
285  /// addRecordTypeName - Compute a name from the given record decl with an
286  /// optional suffix and name the given LLVM type using it.
287  void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
288  StringRef suffix);
289 
290 
291 public: // These are internal details of CGT that shouldn't be used externally.
292  /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
293  llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
294 
295  /// getExpandedTypes - Expand the type \arg Ty into the LLVM
296  /// argument types it would be passed as. See ABIArgInfo::Expand.
297  void getExpandedTypes(QualType Ty,
299 
300  /// IsZeroInitializable - Return whether a type can be
301  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
303 
304  /// Check if the pointer type can be zero-initialized (in the C++ sense)
305  /// with an LLVM zeroinitializer.
307 
308  /// IsZeroInitializable - Return whether a record type can be
309  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
310  bool isZeroInitializable(const RecordDecl *RD);
311 
312  bool isLongDoubleReferenced() const { return LongDoubleReferenced; }
313  bool isRecordLayoutComplete(const Type *Ty) const;
314  unsigned getTargetAddressSpace(QualType T) const;
315 };
316 
317 } // end namespace CodeGen
318 } // end namespace clang
319 
320 #endif
Enums/classes describing ABI related information about constructors, destructors and thunks.
MatchType Type
llvm::MachO::Target Target
Definition: MachO.h:50
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2535
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2060
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions.
Definition: ABIInfo.h:45
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:43
CGFunctionInfo - Class to encapsulate the information about a function definition.
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:257
This class organizes the cross-function state that is used while generating LLVM code.
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
Definition: CodeGenTypes.h:54
CodeGenTypes(CodeGenModule &cgm)
const CGFunctionInfo & arrangeCXXMethodType(const CXXRecordDecl *RD, const FunctionProtoType *FTP, const CXXMethodDecl *MD)
Arrange the argument and result information for a call to an unknown C++ non-static member function o...
Definition: CGCall.cpp:281
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition: CGCall.cpp:309
bool isPointerZeroInitializable(QualType T)
Check if the pointer type can be zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
const CodeGenOptions & getCodeGenOpts() const
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, FnInfoOpts opts, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args)
"Arrange" the LLVM information for a call or type with the given signature.
Definition: CGCall.cpp:768
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Arrange the argument and result information for a value of the given freestanding function type.
Definition: CGCall.cpp:208
CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD)
Derives the 'this' type for codegen purposes, i.e.
Definition: CGCall.cpp:89
CodeGenModule & getCGM() const
Definition: CodeGenTypes.h:107
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1641
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Definition: CGCall.cpp:326
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i....
const CGFunctionInfo & arrangeBlockFunctionCall(const CallArgList &args, const FunctionType *type)
A block function is essentially a free function with an extra implicit argument.
Definition: CGCall.cpp:651
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:682
const CGFunctionInfo & arrangeUnprototypedObjCMessageSend(QualType returnType, const CallArgList &args)
Definition: CGCall.cpp:535
const ABIInfo & getABIInfo() const
Definition: CodeGenTypes.h:109
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:104
std::unique_ptr< CGRecordLayout > ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty)
Compute a new LLVM record layout object for the given record.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
unsigned getTargetAddressSpace(QualType T) const
void getExpandedTypes(QualType Ty, SmallVectorImpl< llvm::Type * >::iterator &TI)
getExpandedTypes - Expand the type
Definition: CGCall.cpp:1023
llvm::StructType * ConvertRecordDeclType(const RecordDecl *TD)
ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
void RefreshTypeCacheForClass(const CXXRecordDecl *RD)
Remove stale types from the type cache when an inheritance model gets assigned to a class.
bool isRecordLayoutComplete(const Type *Ty) const
isRecordLayoutComplete - Return true if the specified type is already completely laid out.
const CGFunctionInfo & arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD)
Objective-C methods are C functions with some implicit parameters.
Definition: CGCall.cpp:489
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
llvm::Type * ConvertSPVCooperativeMatrixType(RecordDecl *RD)
ConvertSPVCooperativeMatrixType - Convert SYCL joint_matrix type which is represented as a pointer to...
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:545
bool isLongDoubleReferenced() const
Definition: CodeGenTypes.h:312
const TargetInfo & getTarget() const
Definition: CodeGenTypes.h:110
const CGFunctionInfo & arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD)
Arrange a thunk that takes 'this' as the first parameter followed by varargs.
Definition: CGCall.cpp:562
const CGFunctionInfo & arrangeCXXMethodCall(const CallArgList &args, const FunctionProtoType *type, RequiredArgs required, unsigned numPrefixArgs)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:704
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Figure out the rules for calling a function with the given formal type using the given arguments.
Definition: CGCall.cpp:641
ASTContext & getContext() const
Definition: CodeGenTypes.h:108
bool isFuncParamTypeConvertible(QualType Ty)
isFuncParamTypeConvertible - Return true if the specified type in a function parameter or result posi...
llvm::Type * ConvertSYCLJointMatrixINTELType(RecordDecl *RD)
ConvertSYCLJointMatrixINTELType - Convert SYCL joint_matrix type which is represented as a pointer to...
const CGFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
Definition: CGCall.cpp:670
const CGFunctionInfo & arrangeFunctionDeclaration(const FunctionDecl *FD)
Free functions are functions that are compatible with an ordinary C function pointer type.
Definition: CGCall.cpp:465
bool isZeroInitializable(QualType T)
IsZeroInitializable - Return whether a type can be zero-initialized (in the C++ sense) with an LLVM z...
const CGFunctionInfo & arrangeBlockFunctionDeclaration(const FunctionProtoType *type, const FunctionArgList &args)
Block invocation functions are C functions with an implicit parameter.
Definition: CGCall.cpp:658
unsigned ClangCallConvToLLVMCallConv(CallingConv CC)
Convert clang calling convention to LLVM callilng convention.
Definition: CGCall.cpp:52
llvm::Type * ConvertTypeForMem(QualType T, bool ForBitField=false)
ConvertTypeForMem - Convert type T into a llvm::Type.
llvm::Type * GetFunctionTypeForVTable(GlobalDecl GD)
GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, given a CXXMethodDecl.
Definition: CGCall.cpp:1768
void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, StringRef suffix)
addRecordTypeName - Compute a name from the given record decl with an optional suffix and name the gi...
CGCXXABI & getCXXABI() const
Definition: CodeGenTypes.h:111
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, bool PassProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:421
const CGFunctionInfo & arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, QualType receiverType)
Arrange the argument and result information for the function type through which to perform a send to ...
Definition: CGCall.cpp:502
const CGFunctionInfo & arrangeCXXStructorDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:336
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:112
const CGFunctionInfo & arrangeMSCtorClosure(const CXXConstructorDecl *CD, CXXCtorType CT)
Definition: CGCall.cpp:571
const CGFunctionInfo & arrangeCall(const CGFunctionInfo &declFI, const CallArgList &args)
Given a function info for a declaration, return the function info for a call with the given arguments...
Definition: CGCall.cpp:731
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition: CGCall.cpp:724
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:351
A class for recording the number of arguments that a function signature requires.
Represents a function declaration or definition.
Definition: Decl.h:1972
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4668
A class which abstracts out some details necessary for making a call.
Definition: Type.h:4379
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:4268
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
Description of a constructor that was inherited from a base class.
Definition: DeclCXX.h:2506
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
A (possibly-)qualified type.
Definition: Type.h:940
Represents a struct/union/class.
Definition: Decl.h:4171
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3587
Exposes information about the current target.
Definition: TargetInfo.h:218
The base class of the type hierarchy.
Definition: Type.h:1813
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
CXXCtorType
C++ constructor types.
Definition: ABI.h:24
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
const FunctionProtoType * T
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:275
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30