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 llvm::DataLayout &TheDataLayout;
126 const TargetInfo &Target;
129 // This should not be moved earlier, since its initialization depends on some
130 // of the previous reference members being already initialized
131 const ABIInfo &TheABIInfo;
133 /// The opaque type map for Objective-C interfaces. All direct
134 /// manipulation is done by the runtime interfaces, which are
135 /// responsible for coercing to the appropriate type; these opaque
136 /// types are never refined.
137 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
139 /// Maps clang struct type with corresponding record layout info.
140 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
142 /// Contains the LLVM IR type for any converted RecordDecl.
143 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
145 /// Hold memoized CGFunctionInfo results.
146 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
148 /// This set keeps track of records that we're currently converting
149 /// to an IR type. For example, when converting:
150 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
151 /// types will be in this set.
152 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
154 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
156 /// True if we didn't layout a function due to a being inside
157 /// a recursive struct conversion, set this to true.
160 SmallVector<const RecordDecl *, 8> DeferredRecords;
163 /// This map keeps cache of llvm::Types and maps clang::Type to
164 /// corresponding llvm::Type.
165 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
168 CodeGenTypes(CodeGenModule &cgm);
171 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
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 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
182 /// ConvertType in that it is used to convert to the memory representation for
183 /// a type. For example, the scalar representation for _Bool is i1, but the
184 /// memory representation is usually i8 or i32, depending on the target.
185 llvm::Type *ConvertTypeForMem(QualType T);
187 /// GetFunctionType - Get the LLVM function type for \arg Info.
188 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
190 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
192 /// isFuncTypeConvertible - Utility to check whether a function type can
193 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
195 bool isFuncTypeConvertible(const FunctionType *FT);
196 bool isFuncParamTypeConvertible(QualType Ty);
198 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
199 /// given a CXXMethodDecl. If the method to has an incomplete return type,
200 /// and/or incomplete argument types, this will return the opaque type.
201 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
203 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
205 /// UpdateCompletedType - When we find the full definition for a TagDecl,
206 /// replace the 'opaque' type we previously made for it if applicable.
207 void UpdateCompletedType(const TagDecl *TD);
209 /// getNullaryFunctionInfo - Get the function info for a void()
210 /// function with standard CC.
211 const CGFunctionInfo &arrangeNullaryFunction();
213 // The arrangement methods are split into three families:
214 // - those meant to drive the signature and prologue/epilogue
215 // of a function declaration or definition,
216 // - those meant for the computation of the LLVM type for an abstract
217 // appearance of a function, and
218 // - those meant for performing the IR-generation of a call.
219 // They differ mainly in how they deal with optional (i.e. variadic)
220 // arguments, as well as unprototyped functions.
223 // - The CGFunctionInfo for emitting a specific call site must include
224 // entries for the optional arguments.
225 // - The function type used at the call site must reflect the formal
226 // signature of the declaration being called, or else the call will
228 // - For the most part, unprototyped functions are called by casting to
229 // a formal signature inferred from the specific argument types used
230 // at the call-site. However, some targets (e.g. x86-64) screw with
231 // this for compatibility reasons.
233 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
234 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
235 const CGFunctionInfo &
236 arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args,
237 const FunctionType::ExtInfo &Info,
240 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
241 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
242 QualType receiverType);
244 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
245 const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
247 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
248 const CXXConstructorDecl *D,
249 CXXCtorType CtorKind,
251 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
252 const FunctionType *Ty,
254 const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
255 const CallArgList &args,
256 FunctionType::ExtInfo info,
257 RequiredArgs required);
258 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
259 const FunctionType *type);
261 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
262 const FunctionProtoType *type,
263 RequiredArgs required);
264 const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
265 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
268 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
269 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
270 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
271 const FunctionProtoType *FTP);
273 /// "Arrange" the LLVM information for a call or type with the given
274 /// signature. This is largely an internal method; other clients
275 /// should use one of the above routines, which ultimately defer to
278 /// \param argTypes - must all actually be canonical as params
279 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
282 ArrayRef<CanQualType> argTypes,
283 FunctionType::ExtInfo info,
286 /// \brief Compute a new LLVM record layout object for the given record.
287 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
288 llvm::StructType *Ty);
290 /// addRecordTypeName - Compute a name from the given record decl with an
291 /// optional suffix and name the given LLVM type using it.
292 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
296 public: // These are internal details of CGT that shouldn't be used externally.
297 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
298 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
300 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
301 /// argument types it would be passed as. See ABIArgInfo::Expand.
302 void getExpandedTypes(QualType Ty,
303 SmallVectorImpl<llvm::Type *>::iterator &TI);
305 /// IsZeroInitializable - Return whether a type can be
306 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
307 bool isZeroInitializable(QualType T);
309 /// IsZeroInitializable - Return whether a record type can be
310 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
311 bool isZeroInitializable(const RecordDecl *RD);
313 bool isRecordLayoutComplete(const Type *Ty) const;
314 bool noRecordsBeingLaidOut() const {
315 return RecordsBeingLaidOut.empty();
317 bool isRecordBeingLaidOut(const Type *Ty) const {
318 return RecordsBeingLaidOut.count(Ty);
323 } // end namespace CodeGen
324 } // end namespace clang