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
;
36 class FunctionProtoType
;
42 typedef CanQual
<Type
> CanQualType
;
52 /// This class organizes the cross-module state that is used while lowering
53 /// AST types to LLVM types.
56 // Some of this stuff should probably be left on the CGM.
58 llvm::Module
&TheModule
;
59 const TargetInfo
&Target
;
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
;
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
;
72 /// Maps clang struct type with corresponding record layout info.
73 llvm::DenseMap
<const Type
*, std::unique_ptr
<CGRecordLayout
>> CGRecordLayouts
;
75 /// Contains the LLVM IR type for any converted RecordDecl.
76 llvm::DenseMap
<const Type
*, llvm::StructType
*> RecordDeclTypes
;
78 /// Hold memoized CGFunctionInfo results.
79 llvm::FoldingSet
<CGFunctionInfo
> FunctionInfos
{FunctionInfosLog2InitSize
};
81 llvm::SmallPtrSet
<const CGFunctionInfo
*, 4> FunctionsBeingProcessed
;
83 /// True if we didn't layout a function due to a being inside
84 /// a recursive struct conversion, set this to true.
87 /// True if any instance of long double types are used.
88 bool LongDoubleReferenced
;
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
;
94 llvm::DenseMap
<const Type
*, llvm::Type
*> RecordsWithOpaqueMemberPointers
;
96 static constexpr unsigned FunctionInfosLog2InitSize
= 9;
97 /// Helper for ConvertType.
98 llvm::Type
*ConvertFunctionTypeInternal(QualType FT
);
101 CodeGenTypes(CodeGenModule
&cgm
);
104 const llvm::DataLayout
&getDataLayout() const {
105 return TheModule
.getDataLayout();
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;
115 /// Convert clang calling convention to LLVM callilng convention.
116 unsigned ClangCallConvToLLVMCallConv(CallingConv CC
);
118 /// Derives the 'this' type for codegen purposes, i.e. ignoring method CVR
120 CanQualType
DeriveThisType(const CXXRecordDecl
*RD
, const CXXMethodDecl
*MD
);
122 /// ConvertType - Convert type T into a llvm::Type.
123 llvm::Type
*ConvertType(QualType T
);
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);
131 /// GetFunctionType - Get the LLVM function type for \arg Info.
132 llvm::FunctionType
*GetFunctionType(const CGFunctionInfo
&Info
);
134 llvm::FunctionType
*GetFunctionType(GlobalDecl GD
);
136 /// isFuncTypeConvertible - Utility to check whether a function type can
137 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
139 bool isFuncTypeConvertible(const FunctionType
*FT
);
140 bool isFuncParamTypeConvertible(QualType Ty
);
142 /// Determine if a C++ inheriting constructor should have parameters matching
143 /// those of its inherited constructor.
144 bool inheritingCtorHasParams(const InheritedConstructor
&Inherited
,
147 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
148 /// given a CXXMethodDecl. If the method to has an incomplete return type,
149 /// and/or incomplete argument types, this will return the opaque type.
150 llvm::Type
*GetFunctionTypeForVTable(GlobalDecl GD
);
152 const CGRecordLayout
&getCGRecordLayout(const RecordDecl
*);
154 /// UpdateCompletedType - When we find the full definition for a TagDecl,
155 /// replace the 'opaque' type we previously made for it if applicable.
156 void UpdateCompletedType(const TagDecl
*TD
);
158 /// Remove stale types from the type cache when an inheritance model
159 /// gets assigned to a class.
160 void RefreshTypeCacheForClass(const CXXRecordDecl
*RD
);
162 // The arrangement methods are split into three families:
163 // - those meant to drive the signature and prologue/epilogue
164 // of a function declaration or definition,
165 // - those meant for the computation of the LLVM type for an abstract
166 // appearance of a function, and
167 // - those meant for performing the IR-generation of a call.
168 // They differ mainly in how they deal with optional (i.e. variadic)
169 // arguments, as well as unprototyped functions.
172 // - The CGFunctionInfo for emitting a specific call site must include
173 // entries for the optional arguments.
174 // - The function type used at the call site must reflect the formal
175 // signature of the declaration being called, or else the call will
177 // - For the most part, unprototyped functions are called by casting to
178 // a formal signature inferred from the specific argument types used
179 // at the call-site. However, some targets (e.g. x86-64) screw with
180 // this for compatibility reasons.
182 const CGFunctionInfo
&arrangeGlobalDeclaration(GlobalDecl GD
);
184 /// Given a function info for a declaration, return the function info
185 /// for a call with the given arguments.
187 /// Often this will be able to simply return the declaration info.
188 const CGFunctionInfo
&arrangeCall(const CGFunctionInfo
&declFI
,
189 const CallArgList
&args
);
191 /// Free functions are functions that are compatible with an ordinary
192 /// C function pointer type.
193 const CGFunctionInfo
&arrangeFunctionDeclaration(const FunctionDecl
*FD
);
194 const CGFunctionInfo
&arrangeFreeFunctionCall(const CallArgList
&Args
,
195 const FunctionType
*Ty
,
197 const CGFunctionInfo
&arrangeFreeFunctionType(CanQual
<FunctionProtoType
> Ty
);
198 const CGFunctionInfo
&arrangeFreeFunctionType(CanQual
<FunctionNoProtoType
> Ty
);
200 /// A nullary function is a freestanding function of type 'void ()'.
201 /// This method works for both calls and declarations.
202 const CGFunctionInfo
&arrangeNullaryFunction();
204 /// A builtin function is a freestanding function using the default
206 const CGFunctionInfo
&
207 arrangeBuiltinFunctionDeclaration(QualType resultType
,
208 const FunctionArgList
&args
);
209 const CGFunctionInfo
&
210 arrangeBuiltinFunctionDeclaration(CanQualType resultType
,
211 ArrayRef
<CanQualType
> argTypes
);
212 const CGFunctionInfo
&arrangeBuiltinFunctionCall(QualType resultType
,
213 const CallArgList
&args
);
215 /// Objective-C methods are C functions with some implicit parameters.
216 const CGFunctionInfo
&arrangeObjCMethodDeclaration(const ObjCMethodDecl
*MD
);
217 const CGFunctionInfo
&arrangeObjCMessageSendSignature(const ObjCMethodDecl
*MD
,
218 QualType receiverType
);
219 const CGFunctionInfo
&arrangeUnprototypedObjCMessageSend(
221 const CallArgList
&args
);
223 /// Block invocation functions are C functions with an implicit parameter.
224 const CGFunctionInfo
&arrangeBlockFunctionDeclaration(
225 const FunctionProtoType
*type
,
226 const FunctionArgList
&args
);
227 const CGFunctionInfo
&arrangeBlockFunctionCall(const CallArgList
&args
,
228 const FunctionType
*type
);
230 /// C++ methods have some special rules and also have implicit parameters.
231 const CGFunctionInfo
&arrangeCXXMethodDeclaration(const CXXMethodDecl
*MD
);
232 const CGFunctionInfo
&arrangeCXXStructorDeclaration(GlobalDecl GD
);
233 const CGFunctionInfo
&arrangeCXXConstructorCall(const CallArgList
&Args
,
234 const CXXConstructorDecl
*D
,
235 CXXCtorType CtorKind
,
236 unsigned ExtraPrefixArgs
,
237 unsigned ExtraSuffixArgs
,
238 bool PassProtoArgs
= true);
240 const CGFunctionInfo
&arrangeCXXMethodCall(const CallArgList
&args
,
241 const FunctionProtoType
*type
,
242 RequiredArgs required
,
243 unsigned numPrefixArgs
);
244 const CGFunctionInfo
&
245 arrangeUnprototypedMustTailThunk(const CXXMethodDecl
*MD
);
246 const CGFunctionInfo
&arrangeMSCtorClosure(const CXXConstructorDecl
*CD
,
248 const CGFunctionInfo
&arrangeCXXMethodType(const CXXRecordDecl
*RD
,
249 const FunctionProtoType
*FTP
,
250 const CXXMethodDecl
*MD
);
252 /// "Arrange" the LLVM information for a call or type with the given
253 /// signature. This is largely an internal method; other clients
254 /// should use one of the above routines, which ultimately defer to
257 /// \param argTypes - must all actually be canonical as params
258 const CGFunctionInfo
&arrangeLLVMFunctionInfo(
259 CanQualType returnType
, FnInfoOpts opts
, ArrayRef
<CanQualType
> argTypes
,
260 FunctionType::ExtInfo info
,
261 ArrayRef
<FunctionProtoType::ExtParameterInfo
> paramInfos
,
264 /// Compute a new LLVM record layout object for the given record.
265 std::unique_ptr
<CGRecordLayout
> ComputeRecordLayout(const RecordDecl
*D
,
266 llvm::StructType
*Ty
);
268 /// addRecordTypeName - Compute a name from the given record decl with an
269 /// optional suffix and name the given LLVM type using it.
270 void addRecordTypeName(const RecordDecl
*RD
, llvm::StructType
*Ty
,
274 public: // These are internal details of CGT that shouldn't be used externally.
275 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
276 llvm::StructType
*ConvertRecordDeclType(const RecordDecl
*TD
);
278 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
279 /// argument types it would be passed as. See ABIArgInfo::Expand.
280 void getExpandedTypes(QualType Ty
,
281 SmallVectorImpl
<llvm::Type
*>::iterator
&TI
);
283 /// IsZeroInitializable - Return whether a type can be
284 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
285 bool isZeroInitializable(QualType T
);
287 /// Check if the pointer type can be zero-initialized (in the C++ sense)
288 /// with an LLVM zeroinitializer.
289 bool isPointerZeroInitializable(QualType T
);
291 /// IsZeroInitializable - Return whether a record type can be
292 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
293 bool isZeroInitializable(const RecordDecl
*RD
);
295 bool isLongDoubleReferenced() const { return LongDoubleReferenced
; }
296 bool isRecordLayoutComplete(const Type
*Ty
) const;
297 unsigned getTargetAddressSpace(QualType T
) const;
300 } // end namespace CodeGen
301 } // end namespace clang