clang 24.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
22namespace llvm {
23class FunctionType;
24class DataLayout;
25class Type;
26class LLVMContext;
27class StructType;
28}
29
30namespace clang {
31class ASTContext;
32template <typename> class CanQual;
34class CXXMethodDecl;
35class CodeGenOptions;
36class FunctionDecl;
38class QualType;
39class RecordDecl;
40class TagDecl;
41class TargetInfo;
42class Type;
44class GlobalDecl;
45
46namespace CodeGen {
47class ABIInfo;
48class CGCXXABI;
49class CGRecordLayout;
50class CodeGenModule;
51class RequiredArgs;
52
53/// This class organizes the cross-module state that is used while lowering
54/// AST types to LLVM types.
56 CodeGenModule &CGM;
57 // Some of this stuff should probably be left on the CGM.
58 ASTContext &Context;
59 llvm::Module &TheModule;
60 const TargetInfo &Target;
61
62 /// The opaque type map for Objective-C interfaces. All direct
63 /// manipulation is done by the runtime interfaces, which are
64 /// responsible for coercing to the appropriate type; these opaque
65 /// types are never refined.
66 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
67
68 /// Maps clang struct type with corresponding record layout info.
69 llvm::DenseMap<const Type*, std::unique_ptr<CGRecordLayout>> CGRecordLayouts;
70
71 /// Contains the LLVM IR type for any converted RecordDecl.
72 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
73
74 /// Hold memoized CGFunctionInfo results.
75 llvm::FoldingSet<CGFunctionInfo> FunctionInfos{FunctionInfosLog2InitSize};
76
77 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
78
79 /// True if we didn't layout a function due to a being inside
80 /// a recursive struct conversion, set this to true.
81 bool SkippedLayout;
82
83 /// True if any instance of long double types are used.
84 bool LongDoubleReferenced;
85
86 /// This map keeps cache of llvm::Types and maps clang::Type to
87 /// corresponding llvm::Type.
88 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
89
90 llvm::DenseMap<const Type *, llvm::Type *> RecordsWithOpaqueMemberPointers;
91
92 static constexpr unsigned FunctionInfosLog2InitSize = 9;
93
94 /// Helper for ConvertType.
95 llvm::Type *ConvertFunctionTypeInternal(QualType FT);
96
97 // Helper to insert CGFunctionInfo objects
98 CGFunctionInfo *findOrInsertCGFunctionInfo(
99 bool isInstanceMethod, bool isChainCall, bool isDelegateCall,
100 unsigned X86ABIAVXLevel, const FunctionType::ExtInfo &info,
102 RequiredArgs required, CanQualType resultType,
103 ArrayRef<CanQualType> argTypes, ArrayRef<unsigned> gridConstantArgs = {});
104
105public:
108
109 const llvm::DataLayout &getDataLayout() const {
110 return TheModule.getDataLayout();
111 }
112 CodeGenModule &getCGM() const { return CGM; }
113 ASTContext &getContext() const { return Context; }
114 const TargetInfo &getTarget() const { return Target; }
115 CGCXXABI &getCXXABI() const;
116 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
117 const CodeGenOptions &getCodeGenOpts() const;
118
119 /// Convert clang calling convention to LLVM callilng convention.
121
122 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
123 /// qualification.
125
126 /// ConvertType - Convert type T into a llvm::Type.
127 llvm::Type *ConvertType(QualType T);
128
129 /// Convert a Clang cooperative matrix type to the corresponding LLVM type.
130 /// The element type and cooperative matrix properties, including scope,
131 /// dimensions, and use, are preserved in the resulting LLVM type.
132 llvm::Type *ConvertTypeForCoopMat(const CooperativeMatrixType *CMT);
133
134 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
135 /// ConvertType in that it is used to convert to the memory representation for
136 /// a type. For example, the scalar representation for _Bool is i1, but the
137 /// memory representation is usually i8 or i32, depending on the target.
138 llvm::Type *ConvertTypeForMem(QualType T);
139
140 /// Check whether the given type needs to be laid out in memory
141 /// using an opaque byte-array type because its load/store type
142 /// does not have the correct alloc size in the LLVM data layout.
143 /// If this is false, the load/store type (convertTypeForLoadStore)
144 /// and memory representation type (ConvertTypeForMem) will
145 /// be the same type.
147 llvm::Type *LLVMTy = nullptr);
148
149 /// Given that T is a scalar type, return the IR type that should
150 /// be used for load and store operations. For example, this might
151 /// be i8 for _Bool or i96 for _BitInt(65). The store size of the
152 /// load/store type (as reported by LLVM's data layout) is always
153 /// the same as the alloc size of the memory representation type
154 /// returned by ConvertTypeForMem.
155 ///
156 /// As an optimization, if you already know the scalar value type
157 /// for T (as would be returned by ConvertType), you can pass
158 /// it as the second argument so that it does not need to be
159 /// recomputed in common cases where the value type and
160 /// load/store type are the same.
161 llvm::Type *convertTypeForLoadStore(QualType T, llvm::Type *LLVMTy = nullptr);
162
163 /// GetFunctionType - Get the LLVM function type for \arg Info.
164 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
165
166 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
167
168 /// isFuncTypeConvertible - Utility to check whether a function type can
169 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
170 /// type).
171 bool isFuncTypeConvertible(const FunctionType *FT);
173
174 /// Determine if a C++ inheriting constructor should have parameters matching
175 /// those of its inherited constructor.
176 bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
178
179 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
180 /// given a CXXMethodDecl. If the method to has an incomplete return type,
181 /// and/or incomplete argument types, this will return the opaque type.
182 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
183
185
186 /// UpdateCompletedType - When we find the full definition for a TagDecl,
187 /// replace the 'opaque' type we previously made for it if applicable.
188 void UpdateCompletedType(const TagDecl *TD);
189
190 /// Remove stale types from the type cache when an inheritance model
191 /// gets assigned to a class.
193
194 // The arrangement methods are split into three families:
195 // - those meant to drive the signature and prologue/epilogue
196 // of a function declaration or definition,
197 // - those meant for the computation of the LLVM type for an abstract
198 // appearance of a function, and
199 // - those meant for performing the IR-generation of a call.
200 // They differ mainly in how they deal with optional (i.e. variadic)
201 // arguments, as well as unprototyped functions.
202 //
203 // Key points:
204 // - The CGFunctionInfo for emitting a specific call site must include
205 // entries for the optional arguments.
206 // - The function type used at the call site must reflect the formal
207 // signature of the declaration being called, or else the call will
208 // go awry.
209 // - For the most part, unprototyped functions are called by casting to
210 // a formal signature inferred from the specific argument types used
211 // at the call-site. However, some targets (e.g. x86-64) screw with
212 // this for compatibility reasons.
213
215
216 /// Given a function info for a declaration, return the function info
217 /// for a call with the given arguments.
218 ///
219 /// Often this will be able to simply return the declaration info.
220 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
221 const CallArgList &args,
222 const FunctionDecl *ABIInfoFD);
223
224 /// Free functions are functions that are compatible with an ordinary
225 /// C function pointer type.
228 const FunctionType *Ty,
229 bool ChainCall,
230 const FunctionDecl *ABIInfoFD);
233
234 /// A nullary function is a freestanding function of type 'void ()'.
235 /// This method works for both calls and declarations.
237
238 /// A builtin function is a freestanding function using the default
239 /// C conventions.
240 const CGFunctionInfo &
242 const FunctionArgList &args);
243 const CGFunctionInfo &
245 ArrayRef<CanQualType> argTypes);
247 const CallArgList &args);
248
249 /// A device kernel caller function is an offload device entry point function
250 /// with a target device dependent calling convention such as amdgpu_kernel,
251 /// ptx_kernel, or spir_kernel.
252 const CGFunctionInfo &
254 const FunctionArgList &args);
255
256 /// Objective-C methods are C functions with some implicit parameters.
259 QualType receiverType);
260 const CGFunctionInfo &
262 const CallArgList &args);
263
264 /// Block invocation functions are C functions with an implicit parameter.
266 const FunctionProtoType *type,
267 const FunctionArgList &args);
269 const FunctionType *type);
270
271 /// C++ methods have some special rules and also have implicit parameters.
275 const CallArgList &Args, const CXXConstructorDecl *D,
276 CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs,
277 const FunctionDecl *ABIInfoFD, bool PassProtoArgs = true);
278
280 const FunctionProtoType *type,
281 RequiredArgs required,
282 unsigned numPrefixArgs,
283 const FunctionDecl *ABIInfoFD);
284 const CGFunctionInfo &
287 CXXCtorType CT);
289 const FunctionProtoType *FTP,
290 const CXXMethodDecl *MD);
291
292 /// "Arrange" the LLVM information for a call or type with the given
293 /// signature. This is largely an internal method; other clients
294 /// should use one of the above routines, which ultimately defer to
295 /// this.
296 ///
297 /// \param argTypes - must all actually be canonical as params
299 CanQualType returnType, FnInfoOpts opts, ArrayRef<CanQualType> argTypes,
302 RequiredArgs args, const FunctionDecl *ABIInfoFD);
303
304 /// Compute a new LLVM record layout object for the given record.
305 std::unique_ptr<CGRecordLayout> ComputeRecordLayout(const RecordDecl *D,
306 llvm::StructType *Ty);
307
308 /// addRecordTypeName - Compute a name from the given record decl with an
309 /// optional suffix and name the given LLVM type using it.
310 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
311 StringRef suffix);
312
313
314public: // These are internal details of CGT that shouldn't be used externally.
315 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
316 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
317
318 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
319 /// argument types it would be passed as. See ABIArgInfo::Expand.
322
323 /// IsZeroInitializable - Return whether a type can be
324 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
326
327 /// Check if the pointer type can be zero-initialized (in the C++ sense)
328 /// with an LLVM zeroinitializer.
330
331 /// IsZeroInitializable - Return whether a record type can be
332 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
333 bool isZeroInitializable(const RecordDecl *RD);
334
335 bool isLongDoubleReferenced() const { return LongDoubleReferenced; }
336 bool isRecordLayoutComplete(const Type *Ty) const;
337 unsigned getTargetAddressSpace(QualType T) const;
338};
339
340} // end namespace CodeGen
341} // end namespace clang
342
343#endif
Enums/classes describing ABI related information about constructors, destructors and thunks.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
Represents a C++ constructor within a class.
Definition DeclCXX.h:2645
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2153
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Represents a canonical, potentially-qualified type.
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:49
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:277
This class organizes the cross-function state that is used while generating LLVM code.
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, FnInfoOpts opts, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args, const FunctionDecl *ABIInfoFD)
"Arrange" the LLVM information for a call or type with the given signature.
Definition CGCall.cpp:1121
CodeGenTypes(CodeGenModule &cgm)
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall, const FunctionDecl *ABIInfoFD)
Figure out the rules for calling a function with the given formal type using the given arguments.
Definition CGCall.cpp:754
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:375
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, const FunctionDecl *ABIInfoFD, bool PassProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
Definition CGCall.cpp:528
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
CGCXXABI & getCXXABI() const
const CGFunctionInfo & arrangeCXXMethodCall(const CallArgList &args, const FunctionProtoType *type, RequiredArgs required, unsigned numPrefixArgs, const FunctionDecl *ABIInfoFD)
Arrange a call to a C++ method, passing the given arguments.
Definition CGCall.cpp:829
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition CGCall.cpp:410
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
ASTContext & getContext() const
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Arrange the argument and result information for a value of the given freestanding function type.
Definition CGCall.cpp:257
CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD)
Derives the 'this' type for codegen purposes, i.e.
Definition CGCall.cpp:140
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition CGCall.cpp:2135
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:435
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:764
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition CGCall.cpp:798
const CGFunctionInfo & arrangeUnprototypedObjCMessageSend(QualType returnType, const CallArgList &args)
Definition CGCall.cpp:655
const TargetInfo & getTarget() const
std::unique_ptr< CGRecordLayout > ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty)
Compute a new LLVM record layout object for the given record.
llvm::Type * convertTypeForLoadStore(QualType T, llvm::Type *LLVMTy=nullptr)
Given that T is a scalar type, return the IR type that should be used for load and store operations.
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:1411
llvm::StructType * ConvertRecordDeclType(const RecordDecl *TD)
ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
llvm::Type * ConvertTypeForCoopMat(const CooperativeMatrixType *CMT)
Convert a Clang cooperative matrix type to the corresponding LLVM type.
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.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD)
Objective-C methods are C functions with some implicit parameters.
Definition CGCall.cpp:608
CodeGenModule & getCGM() const
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
llvm::LLVMContext & getLLVMContext()
const CGFunctionInfo & arrangeDeviceKernelCallerDeclaration(QualType resultType, const FunctionArgList &args)
A device kernel caller function is an offload device entry point function with a target device depend...
Definition CGCall.cpp:814
bool typeRequiresSplitIntoByteArray(QualType ASTTy, llvm::Type *LLVMTy=nullptr)
Check whether the given type needs to be laid out in memory using an opaque byte-array type because i...
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition CGCall.cpp:665
const CGFunctionInfo & arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD)
Arrange a thunk that takes 'this' as the first parameter followed by varargs.
Definition CGCall.cpp:680
const llvm::DataLayout & getDataLayout() const
bool isFuncParamTypeConvertible(QualType Ty)
isFuncParamTypeConvertible - Return true if the specified type in a function parameter or result posi...
const CGFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
Definition CGCall.cpp:787
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:773
unsigned ClangCallConvToLLVMCallConv(CallingConv CC)
Convert clang calling convention to LLVM callilng convention.
Definition CGCall.cpp:62
llvm::Type * GetFunctionTypeForVTable(GlobalDecl GD)
GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, given a CXXMethodDecl.
Definition CGCall.cpp:2263
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...
const CGFunctionInfo & arrangeCall(const CGFunctionInfo &declFI, const CallArgList &args, const FunctionDecl *ABIInfoFD)
Given a function info for a declaration, return the function info for a call with the given arguments...
Definition CGCall.cpp:854
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:621
const CGFunctionInfo & arrangeCXXStructorDeclaration(GlobalDecl GD)
Definition CGCall.cpp:445
const CGFunctionInfo & arrangeFunctionDeclaration(const GlobalDecl GD)
Free functions are functions that are compatible with an ordinary C function pointer type.
Definition CGCall.cpp:569
const CGFunctionInfo & arrangeMSCtorClosure(const CXXConstructorDecl *CD, CXXCtorType CT)
Definition CGCall.cpp:689
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition CGCall.cpp:848
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition CGCall.h:378
A class for recording the number of arguments that a function signature requires.
Represents an opaque OpenCL cooperative matrix type.
Definition TypeBase.h:4718
Represents a function declaration or definition.
Definition Decl.h:2082
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5650
A class which abstracts out some details necessary for making a call.
Definition TypeBase.h:4957
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4846
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
Description of a constructor that was inherited from a base class.
Definition DeclCXX.h:2616
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4506
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3898
Exposes information about the current target.
Definition TargetInfo.h:228
The base class of the type hierarchy.
Definition TypeBase.h:1879
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
Definition CGValue.h:155
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Top level wrappers for InstallAPI frontend operations.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
bool isInstanceMethod(const Decl *D)
Definition Attr.h:158
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition Specifiers.h:282
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30