1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This is the code that handles AST -> LLVM type lowering.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_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"
32 template <typename> class CanQual;
33 class CXXConstructorDecl;
34 class CXXDestructorDecl;
38 class FunctionProtoType;
39 class ObjCInterfaceDecl;
47 typedef CanQual<Type> CanQualType;
57 /// This class organizes the cross-module state that is used while lowering
58 /// AST types to LLVM types.
61 // Some of this stuff should probably be left on the CGM.
63 llvm::Module &TheModule;
64 const TargetInfo &Target;
67 // This should not be moved earlier, since its initialization depends on some
68 // of the previous reference members being already initialized
69 const ABIInfo &TheABIInfo;
71 /// The opaque type map for Objective-C interfaces. All direct
72 /// manipulation is done by the runtime interfaces, which are
73 /// responsible for coercing to the appropriate type; these opaque
74 /// types are never refined.
75 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
77 /// Maps clang struct type with corresponding record layout info.
78 llvm::DenseMap<const Type*, std::unique_ptr<CGRecordLayout>> CGRecordLayouts;
80 /// Contains the LLVM IR type for any converted RecordDecl.
81 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
83 /// Hold memoized CGFunctionInfo results.
84 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
86 /// This set keeps track of records that we're currently converting
87 /// to an IR type. For example, when converting:
88 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
89 /// types will be in this set.
90 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
92 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
94 /// True if we didn't layout a function due to a being inside
95 /// a recursive struct conversion, set this to true.
98 SmallVector<const RecordDecl *, 8> DeferredRecords;
100 /// This map keeps cache of llvm::Types and maps clang::Type to
101 /// corresponding llvm::Type.
102 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
104 llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers;
106 /// Helper for ConvertType.
107 llvm::Type *ConvertFunctionTypeInternal(QualType FT);
110 CodeGenTypes(CodeGenModule &cgm);
113 const llvm::DataLayout &getDataLayout() const {
114 return TheModule.getDataLayout();
116 ASTContext &getContext() const { return Context; }
117 const ABIInfo &getABIInfo() const { return TheABIInfo; }
118 const TargetInfo &getTarget() const { return Target; }
119 CGCXXABI &getCXXABI() const { return TheCXXABI; }
120 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
121 const CodeGenOptions &getCodeGenOpts() const;
123 /// Convert clang calling convention to LLVM callilng convention.
124 unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
126 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
128 CanQualType DeriveThisType(const CXXRecordDecl *RD, const CXXMethodDecl *MD);
130 /// ConvertType - Convert type T into a llvm::Type.
131 llvm::Type *ConvertType(QualType T);
133 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
134 /// ConvertType in that it is used to convert to the memory representation for
135 /// a type. For example, the scalar representation for _Bool is i1, but the
136 /// memory representation is usually i8 or i32, depending on the target.
137 llvm::Type *ConvertTypeForMem(QualType T, bool ForBitField = false);
139 /// GetFunctionType - Get the LLVM function type for \arg Info.
140 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
142 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
144 /// isFuncTypeConvertible - Utility to check whether a function type can
145 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
147 bool isFuncTypeConvertible(const FunctionType *FT);
148 bool isFuncParamTypeConvertible(QualType Ty);
150 /// Determine if a C++ inheriting constructor should have parameters matching
151 /// those of its inherited constructor.
152 bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
155 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
156 /// given a CXXMethodDecl. If the method to has an incomplete return type,
157 /// and/or incomplete argument types, this will return the opaque type.
158 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
160 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
162 /// UpdateCompletedType - When we find the full definition for a TagDecl,
163 /// replace the 'opaque' type we previously made for it if applicable.
164 void UpdateCompletedType(const TagDecl *TD);
166 /// Remove stale types from the type cache when an inheritance model
167 /// gets assigned to a class.
168 void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
170 // The arrangement methods are split into three families:
171 // - those meant to drive the signature and prologue/epilogue
172 // of a function declaration or definition,
173 // - those meant for the computation of the LLVM type for an abstract
174 // appearance of a function, and
175 // - those meant for performing the IR-generation of a call.
176 // They differ mainly in how they deal with optional (i.e. variadic)
177 // arguments, as well as unprototyped functions.
180 // - The CGFunctionInfo for emitting a specific call site must include
181 // entries for the optional arguments.
182 // - The function type used at the call site must reflect the formal
183 // signature of the declaration being called, or else the call will
185 // - For the most part, unprototyped functions are called by casting to
186 // a formal signature inferred from the specific argument types used
187 // at the call-site. However, some targets (e.g. x86-64) screw with
188 // this for compatibility reasons.
190 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
192 /// Given a function info for a declaration, return the function info
193 /// for a call with the given arguments.
195 /// Often this will be able to simply return the declaration info.
196 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
197 const CallArgList &args);
199 /// Free functions are functions that are compatible with an ordinary
200 /// C function pointer type.
201 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
202 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
203 const FunctionType *Ty,
205 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
206 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
208 /// A nullary function is a freestanding function of type 'void ()'.
209 /// This method works for both calls and declarations.
210 const CGFunctionInfo &arrangeNullaryFunction();
212 /// A builtin function is a freestanding function using the default
214 const CGFunctionInfo &
215 arrangeBuiltinFunctionDeclaration(QualType resultType,
216 const FunctionArgList &args);
217 const CGFunctionInfo &
218 arrangeBuiltinFunctionDeclaration(CanQualType resultType,
219 ArrayRef<CanQualType> argTypes);
220 const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
221 const CallArgList &args);
223 /// Objective-C methods are C functions with some implicit parameters.
224 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
225 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
226 QualType receiverType);
227 const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
229 const CallArgList &args);
231 /// Block invocation functions are C functions with an implicit parameter.
232 const CGFunctionInfo &arrangeBlockFunctionDeclaration(
233 const FunctionProtoType *type,
234 const FunctionArgList &args);
235 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
236 const FunctionType *type);
238 /// C++ methods have some special rules and also have implicit parameters.
239 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
240 const CGFunctionInfo &arrangeCXXStructorDeclaration(GlobalDecl GD);
241 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
242 const CXXConstructorDecl *D,
243 CXXCtorType CtorKind,
244 unsigned ExtraPrefixArgs,
245 unsigned ExtraSuffixArgs,
246 bool PassProtoArgs = true);
248 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
249 const FunctionProtoType *type,
250 RequiredArgs required,
251 unsigned numPrefixArgs);
252 const CGFunctionInfo &
253 arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD);
254 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
256 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
257 const FunctionProtoType *FTP,
258 const CXXMethodDecl *MD);
260 /// "Arrange" the LLVM information for a call or type with the given
261 /// signature. This is largely an internal method; other clients
262 /// should use one of the above routines, which ultimately defer to
265 /// \param argTypes - must all actually be canonical as params
266 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
269 ArrayRef<CanQualType> argTypes,
270 FunctionType::ExtInfo info,
271 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
274 /// Compute a new LLVM record layout object for the given record.
275 std::unique_ptr<CGRecordLayout> ComputeRecordLayout(const RecordDecl *D,
276 llvm::StructType *Ty);
278 /// addRecordTypeName - Compute a name from the given record decl with an
279 /// optional suffix and name the given LLVM type using it.
280 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
284 public: // These are internal details of CGT that shouldn't be used externally.
285 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
286 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
288 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
289 /// argument types it would be passed as. See ABIArgInfo::Expand.
290 void getExpandedTypes(QualType Ty,
291 SmallVectorImpl<llvm::Type *>::iterator &TI);
293 /// IsZeroInitializable - Return whether a type can be
294 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
295 bool isZeroInitializable(QualType T);
297 /// Check if the pointer type can be zero-initialized (in the C++ sense)
298 /// with an LLVM zeroinitializer.
299 bool isPointerZeroInitializable(QualType T);
301 /// IsZeroInitializable - Return whether a record type can be
302 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
303 bool isZeroInitializable(const RecordDecl *RD);
305 bool isRecordLayoutComplete(const Type *Ty) const;
306 bool noRecordsBeingLaidOut() const {
307 return RecordsBeingLaidOut.empty();
309 bool isRecordBeingLaidOut(const Type *Ty) const {
310 return RecordsBeingLaidOut.count(Ty);
315 } // end namespace CodeGen
316 } // end namespace clang