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 FunctionProtoType;
37class QualType;
38class RecordDecl;
39class TagDecl;
40class TargetInfo;
41class Type;
42typedef CanQual<Type> CanQualType;
43class GlobalDecl;
44
45namespace CodeGen {
46class ABIInfo;
47class CGCXXABI;
48class CGRecordLayout;
49class CodeGenModule;
50class RequiredArgs;
51
52/// This class organizes the cross-module state that is used while lowering
53/// AST types to LLVM types.
54class 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 /// This map keeps cache of llvm::Types and maps clang::Type to
88 /// corresponding llvm::Type.
89 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
90
91 llvm::DenseMap<const Type *, llvm::Type *> RecordsWithOpaqueMemberPointers;
92
93 static constexpr unsigned FunctionInfosLog2InitSize = 9;
94 /// Helper for ConvertType.
95 llvm::Type *ConvertFunctionTypeInternal(QualType FT);
96
97public:
98 CodeGenTypes(CodeGenModule &cgm);
99 ~CodeGenTypes();
100
101 const llvm::DataLayout &getDataLayout() const {
102 return TheModule.getDataLayout();
103 }
104 CodeGenModule &getCGM() const { return CGM; }
105 ASTContext &getContext() const { return Context; }
106 const ABIInfo &getABIInfo() const { return TheABIInfo; }
107 const TargetInfo &getTarget() const { return Target; }
108 CGCXXABI &getCXXABI() const { return TheCXXABI; }
109 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
110 const CodeGenOptions &getCodeGenOpts() const;
111
112 /// Convert clang calling convention to LLVM callilng convention.
113 unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
114
115 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
116 /// qualification.
117 CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD);
118
119 /// ConvertType - Convert type T into a llvm::Type.
120 llvm::Type *ConvertType(QualType T);
121
122 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
123 /// ConvertType in that it is used to convert to the memory representation for
124 /// a type. For example, the scalar representation for _Bool is i1, but the
125 /// memory representation is usually i8 or i32, depending on the target.
126 llvm::Type *ConvertTypeForMem(QualType T, bool ForBitField = false);
127
128 /// GetFunctionType - Get the LLVM function type for \arg Info.
129 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
130
131 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
132
133 /// isFuncTypeConvertible - Utility to check whether a function type can
134 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
135 /// type).
136 bool isFuncTypeConvertible(const FunctionType *FT);
137 bool isFuncParamTypeConvertible(QualType Ty);
138
139 /// Determine if a C++ inheriting constructor should have parameters matching
140 /// those of its inherited constructor.
141 bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
142 CXXCtorType Type);
143
144 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
145 /// given a CXXMethodDecl. If the method to has an incomplete return type,
146 /// and/or incomplete argument types, this will return the opaque type.
147 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
148
149 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
150
151 /// UpdateCompletedType - When we find the full definition for a TagDecl,
152 /// replace the 'opaque' type we previously made for it if applicable.
153 void UpdateCompletedType(const TagDecl *TD);
154
155 /// Remove stale types from the type cache when an inheritance model
156 /// gets assigned to a class.
157 void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
158
159 // The arrangement methods are split into three families:
160 // - those meant to drive the signature and prologue/epilogue
161 // of a function declaration or definition,
162 // - those meant for the computation of the LLVM type for an abstract
163 // appearance of a function, and
164 // - those meant for performing the IR-generation of a call.
165 // They differ mainly in how they deal with optional (i.e. variadic)
166 // arguments, as well as unprototyped functions.
167 //
168 // Key points:
169 // - The CGFunctionInfo for emitting a specific call site must include
170 // entries for the optional arguments.
171 // - The function type used at the call site must reflect the formal
172 // signature of the declaration being called, or else the call will
173 // go awry.
174 // - For the most part, unprototyped functions are called by casting to
175 // a formal signature inferred from the specific argument types used
176 // at the call-site. However, some targets (e.g. x86-64) screw with
177 // this for compatibility reasons.
178
179 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
180
181 /// Given a function info for a declaration, return the function info
182 /// for a call with the given arguments.
183 ///
184 /// Often this will be able to simply return the declaration info.
185 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
186 const CallArgList &args);
187
188 /// Free functions are functions that are compatible with an ordinary
189 /// C function pointer type.
190 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
191 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
192 const FunctionType *Ty,
193 bool ChainCall);
194 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
195 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
196
197 /// A nullary function is a freestanding function of type 'void ()'.
198 /// This method works for both calls and declarations.
199 const CGFunctionInfo &arrangeNullaryFunction();
200
201 /// A builtin function is a freestanding function using the default
202 /// C conventions.
203 const CGFunctionInfo &
204 arrangeBuiltinFunctionDeclaration(QualType resultType,
205 const FunctionArgList &args);
206 const CGFunctionInfo &
207 arrangeBuiltinFunctionDeclaration(CanQualType resultType,
208 ArrayRef<CanQualType> argTypes);
209 const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
210 const CallArgList &args);
211
212 /// Objective-C methods are C functions with some implicit parameters.
213 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
214 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
215 QualType receiverType);
216 const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
217 QualType returnType,
218 const CallArgList &args);
219
220 /// Block invocation functions are C functions with an implicit parameter.
221 const CGFunctionInfo &arrangeBlockFunctionDeclaration(
222 const FunctionProtoType *type,
223 const FunctionArgList &args);
224 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
225 const FunctionType *type);
226
227 /// C++ methods have some special rules and also have implicit parameters.
228 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
229 const CGFunctionInfo &arrangeCXXStructorDeclaration(GlobalDecl GD);
230 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
231 const CXXConstructorDecl *D,
232 CXXCtorType CtorKind,
233 unsigned ExtraPrefixArgs,
234 unsigned ExtraSuffixArgs,
235 bool PassProtoArgs = true);
236
237 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
238 const FunctionProtoType *type,
239 RequiredArgs required,
240 unsigned numPrefixArgs);
241 const CGFunctionInfo &
242 arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD);
243 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
244 CXXCtorType CT);
245 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
246 const FunctionProtoType *FTP,
247 const CXXMethodDecl *MD);
248
249 /// "Arrange" the LLVM information for a call or type with the given
250 /// signature. This is largely an internal method; other clients
251 /// should use one of the above routines, which ultimately defer to
252 /// this.
253 ///
254 /// \param argTypes - must all actually be canonical as params
255 const CGFunctionInfo &arrangeLLVMFunctionInfo(
256 CanQualType returnType, FnInfoOpts opts, ArrayRef<CanQualType> argTypes,
257 FunctionType::ExtInfo info,
258 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
259 RequiredArgs args);
260
261 /// Compute a new LLVM record layout object for the given record.
262 std::unique_ptr<CGRecordLayout> ComputeRecordLayout(const RecordDecl *D,
263 llvm::StructType *Ty);
264
265 /// addRecordTypeName - Compute a name from the given record decl with an
266 /// optional suffix and name the given LLVM type using it.
267 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
268 StringRef suffix);
269
270
271public: // These are internal details of CGT that shouldn't be used externally.
272 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
273 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
274
275 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
276 /// argument types it would be passed as. See ABIArgInfo::Expand.
277 void getExpandedTypes(QualType Ty,
278 SmallVectorImpl<llvm::Type *>::iterator &TI);
279
280 /// IsZeroInitializable - Return whether a type can be
281 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
282 bool isZeroInitializable(QualType T);
283
284 /// Check if the pointer type can be zero-initialized (in the C++ sense)
285 /// with an LLVM zeroinitializer.
286 bool isPointerZeroInitializable(QualType T);
287
288 /// IsZeroInitializable - Return whether a record type can be
289 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
290 bool isZeroInitializable(const RecordDecl *RD);
291
292 bool isRecordLayoutComplete(const Type *Ty) const;
293 unsigned getTargetAddressSpace(QualType T) const;
294};
295
296} // end namespace CodeGen
297} // end namespace clang
298
299#endif
300