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"
18#include "clang/CodeGen/CGFunctionInfo.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;
33class CXXConstructorDecl;
34class CXXMethodDecl;
35class CodeGenOptions;
36class FunctionDecl;
37class FunctionProtoType;
38class QualType;
39class RecordDecl;
40class TagDecl;
41class TargetInfo;
42class Type;
43typedef CanQual<Type> CanQualType;
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.
55class CodeGenTypes {
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,
101 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
102 RequiredArgs required, CanQualType resultType,
103 ArrayRef<CanQualType> argTypes);
104
105public:
106 CodeGenTypes(CodeGenModule &cgm);
107 ~CodeGenTypes();
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.
120 unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
121
122 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
123 /// qualification.
124 CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD);
125
126 /// ConvertType - Convert type T into a llvm::Type.
127 llvm::Type *ConvertType(QualType T);
128
129 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
130 /// ConvertType in that it is used to convert to the memory representation for
131 /// a type. For example, the scalar representation for _Bool is i1, but the
132 /// memory representation is usually i8 or i32, depending on the target.
133 llvm::Type *ConvertTypeForMem(QualType T);
134
135 /// Check whether the given type needs to be laid out in memory
136 /// using an opaque byte-array type because its load/store type
137 /// does not have the correct alloc size in the LLVM data layout.
138 /// If this is false, the load/store type (convertTypeForLoadStore)
139 /// and memory representation type (ConvertTypeForMem) will
140 /// be the same type.
141 bool typeRequiresSplitIntoByteArray(QualType ASTTy,
142 llvm::Type *LLVMTy = nullptr);
143
144 /// Given that T is a scalar type, return the IR type that should
145 /// be used for load and store operations. For example, this might
146 /// be i8 for _Bool or i96 for _BitInt(65). The store size of the
147 /// load/store type (as reported by LLVM's data layout) is always
148 /// the same as the alloc size of the memory representation type
149 /// returned by ConvertTypeForMem.
150 ///
151 /// As an optimization, if you already know the scalar value type
152 /// for T (as would be returned by ConvertType), you can pass
153 /// it as the second argument so that it does not need to be
154 /// recomputed in common cases where the value type and
155 /// load/store type are the same.
156 llvm::Type *convertTypeForLoadStore(QualType T, llvm::Type *LLVMTy = nullptr);
157
158 /// GetFunctionType - Get the LLVM function type for \arg Info.
159 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
160
161 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
162
163 /// isFuncTypeConvertible - Utility to check whether a function type can
164 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
165 /// type).
166 bool isFuncTypeConvertible(const FunctionType *FT);
167 bool isFuncParamTypeConvertible(QualType Ty);
168
169 /// Determine if a C++ inheriting constructor should have parameters matching
170 /// those of its inherited constructor.
171 bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
172 CXXCtorType Type);
173
174 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
175 /// given a CXXMethodDecl. If the method to has an incomplete return type,
176 /// and/or incomplete argument types, this will return the opaque type.
177 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
178
179 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
180
181 /// UpdateCompletedType - When we find the full definition for a TagDecl,
182 /// replace the 'opaque' type we previously made for it if applicable.
183 void UpdateCompletedType(const TagDecl *TD);
184
185 /// Remove stale types from the type cache when an inheritance model
186 /// gets assigned to a class.
187 void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
188
189 // The arrangement methods are split into three families:
190 // - those meant to drive the signature and prologue/epilogue
191 // of a function declaration or definition,
192 // - those meant for the computation of the LLVM type for an abstract
193 // appearance of a function, and
194 // - those meant for performing the IR-generation of a call.
195 // They differ mainly in how they deal with optional (i.e. variadic)
196 // arguments, as well as unprototyped functions.
197 //
198 // Key points:
199 // - The CGFunctionInfo for emitting a specific call site must include
200 // entries for the optional arguments.
201 // - The function type used at the call site must reflect the formal
202 // signature of the declaration being called, or else the call will
203 // go awry.
204 // - For the most part, unprototyped functions are called by casting to
205 // a formal signature inferred from the specific argument types used
206 // at the call-site. However, some targets (e.g. x86-64) screw with
207 // this for compatibility reasons.
208
209 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
210
211 /// Given a function info for a declaration, return the function info
212 /// for a call with the given arguments.
213 ///
214 /// Often this will be able to simply return the declaration info.
215 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
216 const CallArgList &args,
217 const FunctionDecl *ABIInfoFD);
218
219 /// Free functions are functions that are compatible with an ordinary
220 /// C function pointer type.
221 const CGFunctionInfo &arrangeFunctionDeclaration(const GlobalDecl GD);
222 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
223 const FunctionType *Ty,
224 bool ChainCall,
225 const FunctionDecl *ABIInfoFD);
226 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
227 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
228
229 /// A nullary function is a freestanding function of type 'void ()'.
230 /// This method works for both calls and declarations.
231 const CGFunctionInfo &arrangeNullaryFunction();
232
233 /// A builtin function is a freestanding function using the default
234 /// C conventions.
235 const CGFunctionInfo &
236 arrangeBuiltinFunctionDeclaration(QualType resultType,
237 const FunctionArgList &args);
238 const CGFunctionInfo &
239 arrangeBuiltinFunctionDeclaration(CanQualType resultType,
240 ArrayRef<CanQualType> argTypes);
241 const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
242 const CallArgList &args);
243
244 /// A device kernel caller function is an offload device entry point function
245 /// with a target device dependent calling convention such as amdgpu_kernel,
246 /// ptx_kernel, or spir_kernel.
247 const CGFunctionInfo &
248 arrangeDeviceKernelCallerDeclaration(QualType resultType,
249 const FunctionArgList &args);
250
251 /// Objective-C methods are C functions with some implicit parameters.
252 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
253 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
254 QualType receiverType);
255 const CGFunctionInfo &
256 arrangeUnprototypedObjCMessageSend(QualType returnType,
257 const CallArgList &args);
258
259 /// Block invocation functions are C functions with an implicit parameter.
260 const CGFunctionInfo &arrangeBlockFunctionDeclaration(
261 const FunctionProtoType *type,
262 const FunctionArgList &args);
263 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
264 const FunctionType *type);
265
266 /// C++ methods have some special rules and also have implicit parameters.
267 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
268 const CGFunctionInfo &arrangeCXXStructorDeclaration(GlobalDecl GD);
269 const CGFunctionInfo &arrangeCXXConstructorCall(
270 const CallArgList &Args, const CXXConstructorDecl *D,
271 CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs,
272 const FunctionDecl *ABIInfoFD, bool PassProtoArgs = true);
273
274 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
275 const FunctionProtoType *type,
276 RequiredArgs required,
277 unsigned numPrefixArgs,
278 const FunctionDecl *ABIInfoFD);
279 const CGFunctionInfo &
280 arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD);
281 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
282 CXXCtorType CT);
283 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
284 const FunctionProtoType *FTP,
285 const CXXMethodDecl *MD);
286
287 /// "Arrange" the LLVM information for a call or type with the given
288 /// signature. This is largely an internal method; other clients
289 /// should use one of the above routines, which ultimately defer to
290 /// this.
291 ///
292 /// \param argTypes - must all actually be canonical as params
293 const CGFunctionInfo &arrangeLLVMFunctionInfo(
294 CanQualType returnType, FnInfoOpts opts, ArrayRef<CanQualType> argTypes,
295 FunctionType::ExtInfo info,
296 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
297 RequiredArgs args, const FunctionDecl *ABIInfoFD);
298
299 /// Compute a new LLVM record layout object for the given record.
300 std::unique_ptr<CGRecordLayout> ComputeRecordLayout(const RecordDecl *D,
301 llvm::StructType *Ty);
302
303 /// addRecordTypeName - Compute a name from the given record decl with an
304 /// optional suffix and name the given LLVM type using it.
305 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
306 StringRef suffix);
307
308
309public: // These are internal details of CGT that shouldn't be used externally.
310 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
311 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
312
313 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
314 /// argument types it would be passed as. See ABIArgInfo::Expand.
315 void getExpandedTypes(QualType Ty,
316 SmallVectorImpl<llvm::Type *>::iterator &TI);
317
318 /// IsZeroInitializable - Return whether a type can be
319 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
320 bool isZeroInitializable(QualType T);
321
322 /// Check if the pointer type can be zero-initialized (in the C++ sense)
323 /// with an LLVM zeroinitializer.
324 bool isPointerZeroInitializable(QualType T);
325
326 /// IsZeroInitializable - Return whether a record type can be
327 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
328 bool isZeroInitializable(const RecordDecl *RD);
329
330 bool isLongDoubleReferenced() const { return LongDoubleReferenced; }
331 bool isRecordLayoutComplete(const Type *Ty) const;
332 unsigned getTargetAddressSpace(QualType T) const;
333};
334
335} // end namespace CodeGen
336} // end namespace clang
337
338#endif
339