1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that handles AST -> LLVM type lowering.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
18 #include "clang/AST/GlobalDecl.h"
19 #include "clang/CodeGen/CGFunctionInfo.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/Module.h"
36 template <typename> class CanQual;
37 class CXXConstructorDecl;
38 class CXXDestructorDecl;
42 class FunctionProtoType;
43 class ObjCInterfaceDecl;
51 typedef CanQual<Type> CanQualType;
59 enum class StructorType {
60 Complete, // constructor or destructor
61 Base, // constructor or destructor
62 Deleting // destructor only
65 inline CXXCtorType toCXXCtorType(StructorType T) {
67 case StructorType::Complete:
69 case StructorType::Base:
71 case StructorType::Deleting:
72 llvm_unreachable("cannot have a deleting ctor");
74 llvm_unreachable("not a StructorType");
77 inline StructorType getFromCtorType(CXXCtorType T) {
80 return StructorType::Complete;
82 return StructorType::Base;
84 llvm_unreachable("not expecting a COMDAT");
85 case Ctor_CopyingClosure:
86 case Ctor_DefaultClosure:
87 llvm_unreachable("not expecting a closure");
89 llvm_unreachable("not a CXXCtorType");
92 inline CXXDtorType toCXXDtorType(StructorType T) {
94 case StructorType::Complete:
96 case StructorType::Base:
98 case StructorType::Deleting:
101 llvm_unreachable("not a StructorType");
104 inline StructorType getFromDtorType(CXXDtorType T) {
107 return StructorType::Deleting;
109 return StructorType::Complete;
111 return StructorType::Base;
113 llvm_unreachable("not expecting a COMDAT");
115 llvm_unreachable("not a CXXDtorType");
118 /// This class organizes the cross-module state that is used while lowering
119 /// AST types to LLVM types.
122 // Some of this stuff should probably be left on the CGM.
124 llvm::Module &TheModule;
125 const TargetInfo &Target;
128 // This should not be moved earlier, since its initialization depends on some
129 // of the previous reference members being already initialized
130 const ABIInfo &TheABIInfo;
132 /// The opaque type map for Objective-C interfaces. All direct
133 /// manipulation is done by the runtime interfaces, which are
134 /// responsible for coercing to the appropriate type; these opaque
135 /// types are never refined.
136 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
138 /// Maps clang struct type with corresponding record layout info.
139 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
141 /// Contains the LLVM IR type for any converted RecordDecl.
142 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
144 /// Hold memoized CGFunctionInfo results.
145 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
147 /// This set keeps track of records that we're currently converting
148 /// to an IR type. For example, when converting:
149 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
150 /// types will be in this set.
151 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
153 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
155 /// True if we didn't layout a function due to a being inside
156 /// a recursive struct conversion, set this to true.
159 SmallVector<const RecordDecl *, 8> DeferredRecords;
161 /// This map keeps cache of llvm::Types and maps clang::Type to
162 /// corresponding llvm::Type.
163 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
166 CodeGenTypes(CodeGenModule &cgm);
169 const llvm::DataLayout &getDataLayout() const {
170 return TheModule.getDataLayout();
172 ASTContext &getContext() const { return Context; }
173 const ABIInfo &getABIInfo() const { return TheABIInfo; }
174 const TargetInfo &getTarget() const { return Target; }
175 CGCXXABI &getCXXABI() const { return TheCXXABI; }
176 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
178 /// ConvertType - Convert type T into a llvm::Type.
179 llvm::Type *ConvertType(QualType T);
181 /// \brief Converts the GlobalDecl into an llvm::Type. This should be used
182 /// when we know the target of the function we want to convert. This is
183 /// because some functions (explicitly, those with pass_object_size
184 /// parameters) may not have the same signature as their type portrays, and
185 /// can only be called directly.
186 llvm::Type *ConvertFunctionType(QualType FT,
187 const FunctionDecl *FD = nullptr);
189 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
190 /// ConvertType in that it is used to convert to the memory representation for
191 /// a type. For example, the scalar representation for _Bool is i1, but the
192 /// memory representation is usually i8 or i32, depending on the target.
193 llvm::Type *ConvertTypeForMem(QualType T);
195 /// GetFunctionType - Get the LLVM function type for \arg Info.
196 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
198 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
200 /// isFuncTypeConvertible - Utility to check whether a function type can
201 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
203 bool isFuncTypeConvertible(const FunctionType *FT);
204 bool isFuncParamTypeConvertible(QualType Ty);
206 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
207 /// given a CXXMethodDecl. If the method to has an incomplete return type,
208 /// and/or incomplete argument types, this will return the opaque type.
209 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
211 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
213 /// UpdateCompletedType - When we find the full definition for a TagDecl,
214 /// replace the 'opaque' type we previously made for it if applicable.
215 void UpdateCompletedType(const TagDecl *TD);
217 /// getNullaryFunctionInfo - Get the function info for a void()
218 /// function with standard CC.
219 const CGFunctionInfo &arrangeNullaryFunction();
221 // The arrangement methods are split into three families:
222 // - those meant to drive the signature and prologue/epilogue
223 // of a function declaration or definition,
224 // - those meant for the computation of the LLVM type for an abstract
225 // appearance of a function, and
226 // - those meant for performing the IR-generation of a call.
227 // They differ mainly in how they deal with optional (i.e. variadic)
228 // arguments, as well as unprototyped functions.
231 // - The CGFunctionInfo for emitting a specific call site must include
232 // entries for the optional arguments.
233 // - The function type used at the call site must reflect the formal
234 // signature of the declaration being called, or else the call will
236 // - For the most part, unprototyped functions are called by casting to
237 // a formal signature inferred from the specific argument types used
238 // at the call-site. However, some targets (e.g. x86-64) screw with
239 // this for compatibility reasons.
241 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
242 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
243 const CGFunctionInfo &
244 arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args,
245 const FunctionType::ExtInfo &Info,
248 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
249 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
250 QualType receiverType);
252 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
253 const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
255 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
256 const CXXConstructorDecl *D,
257 CXXCtorType CtorKind,
259 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
260 const FunctionType *Ty,
262 const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
263 const CallArgList &args,
264 FunctionType::ExtInfo info,
265 RequiredArgs required);
266 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
267 const FunctionType *type);
269 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
270 const FunctionProtoType *type,
271 RequiredArgs required);
272 const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
273 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
275 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty,
276 const FunctionDecl *FD);
277 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
278 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
279 const FunctionProtoType *FTP,
280 const CXXMethodDecl *MD);
282 /// "Arrange" the LLVM information for a call or type with the given
283 /// signature. This is largely an internal method; other clients
284 /// should use one of the above routines, which ultimately defer to
287 /// \param argTypes - must all actually be canonical as params
288 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
291 ArrayRef<CanQualType> argTypes,
292 FunctionType::ExtInfo info,
295 /// \brief Compute a new LLVM record layout object for the given record.
296 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
297 llvm::StructType *Ty);
299 /// addRecordTypeName - Compute a name from the given record decl with an
300 /// optional suffix and name the given LLVM type using it.
301 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
305 public: // These are internal details of CGT that shouldn't be used externally.
306 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
307 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
309 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
310 /// argument types it would be passed as. See ABIArgInfo::Expand.
311 void getExpandedTypes(QualType Ty,
312 SmallVectorImpl<llvm::Type *>::iterator &TI);
314 /// IsZeroInitializable - Return whether a type can be
315 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
316 bool isZeroInitializable(QualType T);
318 /// IsZeroInitializable - Return whether a record type can be
319 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
320 bool isZeroInitializable(const RecordDecl *RD);
322 bool isRecordLayoutComplete(const Type *Ty) const;
323 bool noRecordsBeingLaidOut() const {
324 return RecordsBeingLaidOut.empty();
326 bool isRecordBeingLaidOut(const Type *Ty) const {
327 return RecordsBeingLaidOut.count(Ty);
332 } // end namespace CodeGen
333 } // end namespace clang