clang  3.9.0
CodeGenTypes.h
Go to the documentation of this file.
1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the code that handles AST -> LLVM type lowering.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
16 
17 #include "CGCall.h"
18 #include "clang/AST/GlobalDecl.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/Module.h"
22 #include <vector>
23 
24 namespace llvm {
25 class FunctionType;
26 class Module;
27 class DataLayout;
28 class Type;
29 class LLVMContext;
30 class StructType;
31 }
32 
33 namespace clang {
34 class ASTContext;
35 template <typename> class CanQual;
36 class CXXConstructorDecl;
37 class CXXDestructorDecl;
38 class CXXMethodDecl;
39 class CodeGenOptions;
40 class FieldDecl;
41 class FunctionProtoType;
42 class ObjCInterfaceDecl;
43 class ObjCIvarDecl;
44 class PointerType;
45 class QualType;
46 class RecordDecl;
47 class TagDecl;
48 class TargetInfo;
49 class Type;
50 typedef CanQual<Type> CanQualType;
51 
52 namespace CodeGen {
53 class ABIInfo;
54 class CGCXXABI;
55 class CGRecordLayout;
56 class CodeGenModule;
57 class RequiredArgs;
58 
59 enum class StructorType {
60  Complete, // constructor or destructor
61  Base, // constructor or destructor
62  Deleting // destructor only
63 };
64 
66  switch (T) {
68  return Ctor_Complete;
69  case StructorType::Base:
70  return Ctor_Base;
72  llvm_unreachable("cannot have a deleting ctor");
73  }
74  llvm_unreachable("not a StructorType");
75 }
76 
78  switch (T) {
79  case Ctor_Complete:
81  case Ctor_Base:
82  return StructorType::Base;
83  case Ctor_Comdat:
84  llvm_unreachable("not expecting a COMDAT");
87  llvm_unreachable("not expecting a closure");
88  }
89  llvm_unreachable("not a CXXCtorType");
90 }
91 
93  switch (T) {
95  return Dtor_Complete;
96  case StructorType::Base:
97  return Dtor_Base;
99  return Dtor_Deleting;
100  }
101  llvm_unreachable("not a StructorType");
102 }
103 
105  switch (T) {
106  case Dtor_Deleting:
107  return StructorType::Deleting;
108  case Dtor_Complete:
109  return StructorType::Complete;
110  case Dtor_Base:
111  return StructorType::Base;
112  case Dtor_Comdat:
113  llvm_unreachable("not expecting a COMDAT");
114  }
115  llvm_unreachable("not a CXXDtorType");
116 }
117 
118 /// This class organizes the cross-module state that is used while lowering
119 /// AST types to LLVM types.
121  CodeGenModule &CGM;
122  // Some of this stuff should probably be left on the CGM.
123  ASTContext &Context;
124  llvm::Module &TheModule;
125  const TargetInfo &Target;
126  CGCXXABI &TheCXXABI;
127 
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;
131 
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;
137 
138  /// Maps clang struct type with corresponding record layout info.
139  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
140 
141  /// Contains the LLVM IR type for any converted RecordDecl.
142  llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
143 
144  /// Hold memoized CGFunctionInfo results.
145  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
146 
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;
152 
153  llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
154 
155  /// True if we didn't layout a function due to a being inside
156  /// a recursive struct conversion, set this to true.
157  bool SkippedLayout;
158 
159  SmallVector<const RecordDecl *, 8> DeferredRecords;
160 
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;
164 
165  llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers;
166 
167  unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
168 
169 public:
171  ~CodeGenTypes();
172 
173  const llvm::DataLayout &getDataLayout() const {
174  return TheModule.getDataLayout();
175  }
176  ASTContext &getContext() const { return Context; }
177  const ABIInfo &getABIInfo() const { return TheABIInfo; }
178  const TargetInfo &getTarget() const { return Target; }
179  CGCXXABI &getCXXABI() const { return TheCXXABI; }
180  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
181 
182  /// ConvertType - Convert type T into a llvm::Type.
184 
185  /// \brief Converts the GlobalDecl into an llvm::Type. This should be used
186  /// when we know the target of the function we want to convert. This is
187  /// because some functions (explicitly, those with pass_object_size
188  /// parameters) may not have the same signature as their type portrays, and
189  /// can only be called directly.
191  const FunctionDecl *FD = nullptr);
192 
193  /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
194  /// ConvertType in that it is used to convert to the memory representation for
195  /// a type. For example, the scalar representation for _Bool is i1, but the
196  /// memory representation is usually i8 or i32, depending on the target.
198 
199  /// GetFunctionType - Get the LLVM function type for \arg Info.
200  llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
201 
202  llvm::FunctionType *GetFunctionType(GlobalDecl GD);
203 
204  /// isFuncTypeConvertible - Utility to check whether a function type can
205  /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
206  /// type).
207  bool isFuncTypeConvertible(const FunctionType *FT);
209 
210  /// Determine if a C++ inheriting constructor should have parameters matching
211  /// those of its inherited constructor.
212  bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
213  CXXCtorType Type);
214 
215  /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
216  /// given a CXXMethodDecl. If the method to has an incomplete return type,
217  /// and/or incomplete argument types, this will return the opaque type.
219 
221 
222  /// UpdateCompletedType - When we find the full definition for a TagDecl,
223  /// replace the 'opaque' type we previously made for it if applicable.
224  void UpdateCompletedType(const TagDecl *TD);
225 
226  /// \brief Remove stale types from the type cache when an inheritance model
227  /// gets assigned to a class.
228  void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
229 
230  // The arrangement methods are split into three families:
231  // - those meant to drive the signature and prologue/epilogue
232  // of a function declaration or definition,
233  // - those meant for the computation of the LLVM type for an abstract
234  // appearance of a function, and
235  // - those meant for performing the IR-generation of a call.
236  // They differ mainly in how they deal with optional (i.e. variadic)
237  // arguments, as well as unprototyped functions.
238  //
239  // Key points:
240  // - The CGFunctionInfo for emitting a specific call site must include
241  // entries for the optional arguments.
242  // - The function type used at the call site must reflect the formal
243  // signature of the declaration being called, or else the call will
244  // go awry.
245  // - For the most part, unprototyped functions are called by casting to
246  // a formal signature inferred from the specific argument types used
247  // at the call-site. However, some targets (e.g. x86-64) screw with
248  // this for compatibility reasons.
249 
251 
252  /// Given a function info for a declaration, return the function info
253  /// for a call with the given arguments.
254  ///
255  /// Often this will be able to simply return the declaration info.
256  const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
257  const CallArgList &args);
258 
259  /// Free functions are functions that are compatible with an ordinary
260  /// C function pointer type.
263  const FunctionType *Ty,
264  bool ChainCall);
266  const FunctionDecl *FD);
268 
269  /// A nullary function is a freestanding function of type 'void ()'.
270  /// This method works for both calls and declarations.
272 
273  /// A builtin function is a freestanding function using the default
274  /// C conventions.
275  const CGFunctionInfo &
277  const FunctionArgList &args);
278  const CGFunctionInfo &
280  ArrayRef<CanQualType> argTypes);
282  const CallArgList &args);
283 
284  /// Objective-C methods are C functions with some implicit parameters.
287  QualType receiverType);
289  QualType returnType,
290  const CallArgList &args);
291 
292  /// Block invocation functions are C functions with an implicit parameter.
294  const FunctionProtoType *type,
295  const FunctionArgList &args);
297  const FunctionType *type);
298 
299  /// C++ methods have some special rules and also have implicit parameters.
304  const CXXConstructorDecl *D,
305  CXXCtorType CtorKind,
306  unsigned ExtraArgs);
307 
309  const FunctionProtoType *type,
310  RequiredArgs required);
313  CXXCtorType CT);
315  const FunctionProtoType *FTP,
316  const CXXMethodDecl *MD);
317 
318  /// "Arrange" the LLVM information for a call or type with the given
319  /// signature. This is largely an internal method; other clients
320  /// should use one of the above routines, which ultimately defer to
321  /// this.
322  ///
323  /// \param argTypes - must all actually be canonical as params
325  bool instanceMethod,
326  bool chainCall,
327  ArrayRef<CanQualType> argTypes,
330  RequiredArgs args);
331 
332  /// \brief Compute a new LLVM record layout object for the given record.
334  llvm::StructType *Ty);
335 
336  /// addRecordTypeName - Compute a name from the given record decl with an
337  /// optional suffix and name the given LLVM type using it.
338  void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
339  StringRef suffix);
340 
341 
342 public: // These are internal details of CGT that shouldn't be used externally.
343  /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
344  llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
345 
346  /// getExpandedTypes - Expand the type \arg Ty into the LLVM
347  /// argument types it would be passed as. See ABIArgInfo::Expand.
348  void getExpandedTypes(QualType Ty,
350 
351  /// IsZeroInitializable - Return whether a type can be
352  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
354 
355  /// IsZeroInitializable - Return whether a record type can be
356  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
357  bool isZeroInitializable(const RecordDecl *RD);
358 
359  bool isRecordLayoutComplete(const Type *Ty) const;
360  bool noRecordsBeingLaidOut() const {
361  return RecordsBeingLaidOut.empty();
362  }
363  bool isRecordBeingLaidOut(const Type *Ty) const {
364  return RecordsBeingLaidOut.count(Ty);
365  }
366 
367 };
368 
369 } // end namespace CodeGen
370 } // end namespace clang
371 
372 #endif
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:599
void getExpandedTypes(QualType Ty, SmallVectorImpl< llvm::Type * >::iterator &TI)
getExpandedTypes - Expand the type
Definition: CGCall.cpp:909
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
const CGFunctionInfo & arrangeBlockFunctionDeclaration(const FunctionProtoType *type, const FunctionArgList &args)
Block invocation functions are C functions with an implicit parameter.
Definition: CGCall.cpp:573
Complete object ctor.
Definition: ABI.h:26
A (possibly-)qualified type.
Definition: Type.h:598
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
The COMDAT used for ctors.
Definition: ABI.h:28
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:2879
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty, const FunctionDecl *FD)
Arrange the argument and result information for a value of the given freestanding function type...
Definition: CGCall.cpp:160
void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, StringRef suffix)
addRecordTypeName - Compute a name from the given record decl with an optional suffix and name the gi...
const CGFunctionInfo & arrangeCXXMethodType(const CXXRecordDecl *RD, const FunctionProtoType *FTP, const CXXMethodDecl *MD)
Arrange the argument and result information for a call to an unknown C++ non-static member function o...
Definition: CGCall.cpp:212
CGCXXABI & getCXXABI() const
Definition: CodeGenTypes.h:179
ASTContext & getContext() const
Definition: CodeGenTypes.h:176
The base class of the type hierarchy.
Definition: Type.h:1281
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i...
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2187
Default closure variant of a ctor.
Definition: ABI.h:30
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
const CGFunctionInfo & arrangeCXXStructorDeclaration(const CXXMethodDecl *MD, StructorType Type)
Definition: CGCall.cpp:258
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
RecordDecl - Represents a struct/union/class.
Definition: Decl.h:3253
Description of a constructor that was inherited from a base class.
Definition: DeclCXX.h:2161
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
StructorType getFromDtorType(CXXDtorType T)
Definition: CodeGenTypes.h:104
Base object ctor.
Definition: ABI.h:27
Deleting dtor.
Definition: ABI.h:35
const CGFunctionInfo & arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, QualType receiverType)
Arrange the argument and result information for the function type through which to perform a send to ...
Definition: CGCall.cpp:418
const CGFunctionInfo & arrangeCall(const CGFunctionInfo &declFI, const CallArgList &args)
Given a function info for a declaration, return the function info for a call with the given arguments...
Definition: CGCall.cpp:643
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
Definition: CGCall.cpp:233
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, bool instanceMethod, bool chainCall, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args)
"Arrange" the LLVM information for a call or type with the given signature.
Definition: CGCall.cpp:672
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:456
const CGFunctionInfo & arrangeUnprototypedObjCMessageSend(QualType returnType, const CallArgList &args)
Definition: CGCall.cpp:445
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3073
Exposes information about the current target.
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
friend class ASTContext
Definition: Type.h:4178
CodeGenTypes(CodeGenModule &cgm)
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition: CGCall.cpp:636
A class for recording the number of arguments that a function signature requires. ...
const CGFunctionInfo & arrangeBuiltinFunctionCall(QualType resultType, const CallArgList &args)
Definition: CGCall.cpp:586
Base object dtor.
Definition: ABI.h:37
The COMDAT used for dtors.
Definition: ABI.h:38
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:231
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:29
The l-value was considered opaque, so the alignment was determined from a type.
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:173
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Definition: CGCall.cpp:248
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2727
const CGFunctionInfo & arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD)
Objective-C methods are C functions with some implicit parameters.
Definition: CGCall.cpp:405
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1736
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:180
Complete object dtor.
Definition: ABI.h:36
const CGFunctionInfo & arrangeCXXMethodCall(const CallArgList &args, const FunctionProtoType *type, RequiredArgs required)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:618
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
const CGFunctionInfo & arrangeBlockFunctionCall(const CallArgList &args, const FunctionType *type)
A block function is essentially a free function with an extra implicit argument.
Definition: CGCall.cpp:566
bool isRecordBeingLaidOut(const Type *Ty) const
Definition: CodeGenTypes.h:363
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:146
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1983
CXXDtorType toCXXDtorType(StructorType T)
Definition: CodeGenTypes.h:92
const CGFunctionInfo & arrangeFunctionDeclaration(const FunctionDecl *FD)
Free functions are functions that are compatible with an ordinary C function pointer type...
Definition: CGCall.cpp:380
llvm::StructType * ConvertRecordDeclType(const RecordDecl *TD)
ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
StructorType getFromCtorType(CXXCtorType T)
Definition: CodeGenTypes.h:77
llvm::Type * ConvertFunctionType(QualType FT, const FunctionDecl *FD=nullptr)
Converts the GlobalDecl into an llvm::Type.
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:43
This class organizes the cross-module state that is used while lowering AST types to LLVM types...
Definition: CodeGenTypes.h:120
const CGFunctionInfo & arrangeMSMemberPointerThunk(const CXXMethodDecl *MD)
Arrange a thunk that takes 'this' as the first parameter followed by varargs.
Definition: CGCall.cpp:475
bool isRecordLayoutComplete(const Type *Ty) const
isRecordLayoutComplete - Return true if the specified type is already completely laid out...
llvm::Type * GetFunctionTypeForVTable(GlobalDecl GD)
GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable, given a CXXMethodDecl...
Definition: CGCall.cpp:1593
Represents a C++ struct/union/class.
Definition: DeclCXX.h:263
bool isFuncParamTypeConvertible(QualType Ty)
isFuncParamTypeConvertible - Return true if the specified type in a function parameter or result posi...
const TargetInfo & getTarget() const
Definition: CodeGenTypes.h:178
Copying closure variant of a ctor.
Definition: ABI.h:29
void RefreshTypeCacheForClass(const CXXRecordDecl *RD)
Remove stale types from the type cache when an inheritance model gets assigned to a class...
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions...
Definition: ABIInfo.h:49
const CGFunctionInfo & arrangeMSCtorClosure(const CXXConstructorDecl *CD, CXXCtorType CT)
Definition: CGCall.cpp:485
const ABIInfo & getABIInfo() const
Definition: CodeGenTypes.h:177
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Figure out the rules for calling a function with the given formal type using the given arguments...
Definition: CGCall.cpp:556
CXXCtorType toCXXCtorType(StructorType T)
Definition: CodeGenTypes.h:65
CGRecordLayout * ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty)
Compute a new LLVM record layout object for the given record.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:56
bool noRecordsBeingLaidOut() const
Definition: CodeGenTypes.h:360
bool isZeroInitializable(QualType T)
IsZeroInitializable - Return whether a type can be zero-initialized (in the C++ sense) with an LLVM z...
A class which abstracts out some details necessary for making a call.
Definition: Type.h:2904
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraArgs)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:351
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1466