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/Basic/ABI.h"
19 #include "clang/CodeGen/CGFunctionInfo.h"
20 #include "clang/Sema/Sema.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/IR/Module.h"
34 template <typename> class CanQual;
35 class CXXConstructorDecl;
36 class CXXDestructorDecl;
40 class FunctionProtoType;
41 class ObjCInterfaceDecl;
49 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;
165 llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers;
168 CodeGenTypes(CodeGenModule &cgm);
171 const llvm::DataLayout &getDataLayout() const {
172 return TheModule.getDataLayout();
174 ASTContext &getContext() const { return Context; }
175 const ABIInfo &getABIInfo() const { return TheABIInfo; }
176 const TargetInfo &getTarget() const { return Target; }
177 CGCXXABI &getCXXABI() const { return TheCXXABI; }
178 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
179 const CodeGenOptions &getCodeGenOpts() const;
181 /// Convert clang calling convention to LLVM callilng convention.
182 unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
184 /// ConvertType - Convert type T into a llvm::Type.
185 llvm::Type *ConvertType(QualType T);
187 /// Converts the GlobalDecl into an llvm::Type. This should be used
188 /// when we know the target of the function we want to convert. This is
189 /// because some functions (explicitly, those with pass_object_size
190 /// parameters) may not have the same signature as their type portrays, and
191 /// can only be called directly.
192 llvm::Type *ConvertFunctionType(QualType FT,
193 const FunctionDecl *FD = nullptr);
195 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
196 /// ConvertType in that it is used to convert to the memory representation for
197 /// a type. For example, the scalar representation for _Bool is i1, but the
198 /// memory representation is usually i8 or i32, depending on the target.
199 llvm::Type *ConvertTypeForMem(QualType T);
201 /// GetFunctionType - Get the LLVM function type for \arg Info.
202 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
204 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
206 /// isFuncTypeConvertible - Utility to check whether a function type can
207 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
209 bool isFuncTypeConvertible(const FunctionType *FT);
210 bool isFuncParamTypeConvertible(QualType Ty);
212 /// Determine if a C++ inheriting constructor should have parameters matching
213 /// those of its inherited constructor.
214 bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
217 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
218 /// given a CXXMethodDecl. If the method to has an incomplete return type,
219 /// and/or incomplete argument types, this will return the opaque type.
220 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
222 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
224 /// UpdateCompletedType - When we find the full definition for a TagDecl,
225 /// replace the 'opaque' type we previously made for it if applicable.
226 void UpdateCompletedType(const TagDecl *TD);
228 /// Remove stale types from the type cache when an inheritance model
229 /// gets assigned to a class.
230 void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
232 // The arrangement methods are split into three families:
233 // - those meant to drive the signature and prologue/epilogue
234 // of a function declaration or definition,
235 // - those meant for the computation of the LLVM type for an abstract
236 // appearance of a function, and
237 // - those meant for performing the IR-generation of a call.
238 // They differ mainly in how they deal with optional (i.e. variadic)
239 // arguments, as well as unprototyped functions.
242 // - The CGFunctionInfo for emitting a specific call site must include
243 // entries for the optional arguments.
244 // - The function type used at the call site must reflect the formal
245 // signature of the declaration being called, or else the call will
247 // - For the most part, unprototyped functions are called by casting to
248 // a formal signature inferred from the specific argument types used
249 // at the call-site. However, some targets (e.g. x86-64) screw with
250 // this for compatibility reasons.
252 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
254 /// Given a function info for a declaration, return the function info
255 /// for a call with the given arguments.
257 /// Often this will be able to simply return the declaration info.
258 const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
259 const CallArgList &args);
261 /// Free functions are functions that are compatible with an ordinary
262 /// C function pointer type.
263 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
264 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
265 const FunctionType *Ty,
267 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty,
268 const FunctionDecl *FD);
269 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
271 /// A nullary function is a freestanding function of type 'void ()'.
272 /// This method works for both calls and declarations.
273 const CGFunctionInfo &arrangeNullaryFunction();
275 /// A builtin function is a freestanding function using the default
277 const CGFunctionInfo &
278 arrangeBuiltinFunctionDeclaration(QualType resultType,
279 const FunctionArgList &args);
280 const CGFunctionInfo &
281 arrangeBuiltinFunctionDeclaration(CanQualType resultType,
282 ArrayRef<CanQualType> argTypes);
283 const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
284 const CallArgList &args);
286 /// Objective-C methods are C functions with some implicit parameters.
287 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
288 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
289 QualType receiverType);
290 const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
292 const CallArgList &args);
294 /// Block invocation functions are C functions with an implicit parameter.
295 const CGFunctionInfo &arrangeBlockFunctionDeclaration(
296 const FunctionProtoType *type,
297 const FunctionArgList &args);
298 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
299 const FunctionType *type);
301 /// C++ methods have some special rules and also have implicit parameters.
302 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
303 const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
305 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
306 const CXXConstructorDecl *D,
307 CXXCtorType CtorKind,
308 unsigned ExtraPrefixArgs,
309 unsigned ExtraSuffixArgs,
310 bool PassProtoArgs = true);
312 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
313 const FunctionProtoType *type,
314 RequiredArgs required,
315 unsigned numPrefixArgs);
316 const CGFunctionInfo &
317 arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD);
318 const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
320 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
321 const FunctionProtoType *FTP,
322 const CXXMethodDecl *MD);
324 /// "Arrange" the LLVM information for a call or type with the given
325 /// signature. This is largely an internal method; other clients
326 /// should use one of the above routines, which ultimately defer to
329 /// \param argTypes - must all actually be canonical as params
330 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
333 ArrayRef<CanQualType> argTypes,
334 FunctionType::ExtInfo info,
335 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
338 /// Compute a new LLVM record layout object for the given record.
339 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
340 llvm::StructType *Ty);
342 /// addRecordTypeName - Compute a name from the given record decl with an
343 /// optional suffix and name the given LLVM type using it.
344 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
348 public: // These are internal details of CGT that shouldn't be used externally.
349 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
350 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
352 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
353 /// argument types it would be passed as. See ABIArgInfo::Expand.
354 void getExpandedTypes(QualType Ty,
355 SmallVectorImpl<llvm::Type *>::iterator &TI);
357 /// IsZeroInitializable - Return whether a type can be
358 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
359 bool isZeroInitializable(QualType T);
361 /// Check if the pointer type can be zero-initialized (in the C++ sense)
362 /// with an LLVM zeroinitializer.
363 bool isPointerZeroInitializable(QualType T);
365 /// IsZeroInitializable - Return whether a record type can be
366 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
367 bool isZeroInitializable(const RecordDecl *RD);
369 bool isRecordLayoutComplete(const Type *Ty) const;
370 bool noRecordsBeingLaidOut() const {
371 return RecordsBeingLaidOut.empty();
373 bool isRecordBeingLaidOut(const Type *Ty) const {
374 return RecordsBeingLaidOut.count(Ty);
379 } // end namespace CodeGen
380 } // end namespace clang