clang  8.0.0svn
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/Basic/ABI.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/IR/Module.h"
22 
23 namespace llvm {
24 class FunctionType;
25 class DataLayout;
26 class Type;
27 class LLVMContext;
28 class StructType;
29 }
30 
31 namespace clang {
32 class ASTContext;
33 template <typename> class CanQual;
34 class CXXConstructorDecl;
35 class CXXDestructorDecl;
36 class CXXMethodDecl;
37 class CodeGenOptions;
38 class FieldDecl;
39 class FunctionProtoType;
40 class ObjCInterfaceDecl;
41 class ObjCIvarDecl;
42 class PointerType;
43 class QualType;
44 class RecordDecl;
45 class TagDecl;
46 class TargetInfo;
47 class Type;
48 typedef CanQual<Type> CanQualType;
49 class GlobalDecl;
50 
51 namespace CodeGen {
52 class ABIInfo;
53 class CGCXXABI;
54 class CGRecordLayout;
55 class CodeGenModule;
56 class RequiredArgs;
57 
58 enum class StructorType {
59  Complete, // constructor or destructor
60  Base, // constructor or destructor
61  Deleting // destructor only
62 };
63 
65  switch (T) {
66  case StructorType::Complete:
67  return Ctor_Complete;
68  case StructorType::Base:
69  return Ctor_Base;
70  case StructorType::Deleting:
71  llvm_unreachable("cannot have a deleting ctor");
72  }
73  llvm_unreachable("not a StructorType");
74 }
75 
77  switch (T) {
78  case Ctor_Complete:
79  return StructorType::Complete;
80  case Ctor_Base:
81  return StructorType::Base;
82  case Ctor_Comdat:
83  llvm_unreachable("not expecting a COMDAT");
86  llvm_unreachable("not expecting a closure");
87  }
88  llvm_unreachable("not a CXXCtorType");
89 }
90 
92  switch (T) {
93  case StructorType::Complete:
94  return Dtor_Complete;
95  case StructorType::Base:
96  return Dtor_Base;
97  case StructorType::Deleting:
98  return Dtor_Deleting;
99  }
100  llvm_unreachable("not a StructorType");
101 }
102 
104  switch (T) {
105  case Dtor_Deleting:
106  return StructorType::Deleting;
107  case Dtor_Complete:
108  return StructorType::Complete;
109  case Dtor_Base:
110  return StructorType::Base;
111  case Dtor_Comdat:
112  llvm_unreachable("not expecting a COMDAT");
113  }
114  llvm_unreachable("not a CXXDtorType");
115 }
116 
117 /// This class organizes the cross-module state that is used while lowering
118 /// AST types to LLVM types.
120  CodeGenModule &CGM;
121  // Some of this stuff should probably be left on the CGM.
122  ASTContext &Context;
123  llvm::Module &TheModule;
124  const TargetInfo &Target;
125  CGCXXABI &TheCXXABI;
126 
127  // This should not be moved earlier, since its initialization depends on some
128  // of the previous reference members being already initialized
129  const ABIInfo &TheABIInfo;
130 
131  /// The opaque type map for Objective-C interfaces. All direct
132  /// manipulation is done by the runtime interfaces, which are
133  /// responsible for coercing to the appropriate type; these opaque
134  /// types are never refined.
135  llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
136 
137  /// Maps clang struct type with corresponding record layout info.
138  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
139 
140  /// Contains the LLVM IR type for any converted RecordDecl.
141  llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
142 
143  /// Hold memoized CGFunctionInfo results.
144  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
145 
146  /// This set keeps track of records that we're currently converting
147  /// to an IR type. For example, when converting:
148  /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
149  /// types will be in this set.
150  llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
151 
152  llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
153 
154  /// True if we didn't layout a function due to a being inside
155  /// a recursive struct conversion, set this to true.
156  bool SkippedLayout;
157 
158  SmallVector<const RecordDecl *, 8> DeferredRecords;
159 
160  /// This map keeps cache of llvm::Types and maps clang::Type to
161  /// corresponding llvm::Type.
162  llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
163 
164  llvm::SmallSet<const Type *, 8> RecordsWithOpaqueMemberPointers;
165 
166 public:
168  ~CodeGenTypes();
169 
170  const llvm::DataLayout &getDataLayout() const {
171  return TheModule.getDataLayout();
172  }
173  ASTContext &getContext() const { return Context; }
174  const ABIInfo &getABIInfo() const { return TheABIInfo; }
175  const TargetInfo &getTarget() const { return Target; }
176  CGCXXABI &getCXXABI() const { return TheCXXABI; }
177  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
178  const CodeGenOptions &getCodeGenOpts() const;
179 
180  /// Convert clang calling convention to LLVM callilng convention.
181  unsigned ClangCallConvToLLVMCallConv(CallingConv CC);
182 
183  /// ConvertType - Convert type T into a llvm::Type.
184  llvm::Type *ConvertType(QualType T);
185 
186  /// Converts the GlobalDecl into an llvm::Type. This should be used
187  /// when we know the target of the function we want to convert. This is
188  /// because some functions (explicitly, those with pass_object_size
189  /// parameters) may not have the same signature as their type portrays, and
190  /// can only be called directly.
191  llvm::Type *ConvertFunctionType(QualType FT,
192  const FunctionDecl *FD = nullptr);
193 
194  /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
195  /// ConvertType in that it is used to convert to the memory representation for
196  /// a type. For example, the scalar representation for _Bool is i1, but the
197  /// memory representation is usually i8 or i32, depending on the target.
198  llvm::Type *ConvertTypeForMem(QualType T);
199 
200  /// GetFunctionType - Get the LLVM function type for \arg Info.
201  llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
202 
203  llvm::FunctionType *GetFunctionType(GlobalDecl GD);
204 
205  /// isFuncTypeConvertible - Utility to check whether a function type can
206  /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
207  /// type).
208  bool isFuncTypeConvertible(const FunctionType *FT);
209  bool isFuncParamTypeConvertible(QualType Ty);
210 
211  /// Determine if a C++ inheriting constructor should have parameters matching
212  /// those of its inherited constructor.
213  bool inheritingCtorHasParams(const InheritedConstructor &Inherited,
214  CXXCtorType Type);
215 
216  /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
217  /// given a CXXMethodDecl. If the method to has an incomplete return type,
218  /// and/or incomplete argument types, this will return the opaque type.
219  llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
220 
221  const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
222 
223  /// UpdateCompletedType - When we find the full definition for a TagDecl,
224  /// replace the 'opaque' type we previously made for it if applicable.
225  void UpdateCompletedType(const TagDecl *TD);
226 
227  /// Remove stale types from the type cache when an inheritance model
228  /// gets assigned to a class.
229  void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
230 
231  // The arrangement methods are split into three families:
232  // - those meant to drive the signature and prologue/epilogue
233  // of a function declaration or definition,
234  // - those meant for the computation of the LLVM type for an abstract
235  // appearance of a function, and
236  // - those meant for performing the IR-generation of a call.
237  // They differ mainly in how they deal with optional (i.e. variadic)
238  // arguments, as well as unprototyped functions.
239  //
240  // Key points:
241  // - The CGFunctionInfo for emitting a specific call site must include
242  // entries for the optional arguments.
243  // - The function type used at the call site must reflect the formal
244  // signature of the declaration being called, or else the call will
245  // go awry.
246  // - For the most part, unprototyped functions are called by casting to
247  // a formal signature inferred from the specific argument types used
248  // at the call-site. However, some targets (e.g. x86-64) screw with
249  // this for compatibility reasons.
250 
251  const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
252 
253  /// Given a function info for a declaration, return the function info
254  /// for a call with the given arguments.
255  ///
256  /// Often this will be able to simply return the declaration info.
257  const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
258  const CallArgList &args);
259 
260  /// Free functions are functions that are compatible with an ordinary
261  /// C function pointer type.
262  const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
264  const FunctionType *Ty,
265  bool ChainCall);
267  const FunctionDecl *FD);
269 
270  /// A nullary function is a freestanding function of type 'void ()'.
271  /// This method works for both calls and declarations.
272  const CGFunctionInfo &arrangeNullaryFunction();
273 
274  /// A builtin function is a freestanding function using the default
275  /// C conventions.
276  const CGFunctionInfo &
277  arrangeBuiltinFunctionDeclaration(QualType resultType,
278  const FunctionArgList &args);
279  const CGFunctionInfo &
280  arrangeBuiltinFunctionDeclaration(CanQualType resultType,
281  ArrayRef<CanQualType> argTypes);
282  const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
283  const CallArgList &args);
284 
285  /// Objective-C methods are C functions with some implicit parameters.
286  const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
288  QualType receiverType);
289  const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
290  QualType returnType,
291  const CallArgList &args);
292 
293  /// Block invocation functions are C functions with an implicit parameter.
294  const CGFunctionInfo &arrangeBlockFunctionDeclaration(
295  const FunctionProtoType *type,
296  const FunctionArgList &args);
297  const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
298  const FunctionType *type);
299 
300  /// C++ methods have some special rules and also have implicit parameters.
301  const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
302  const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
303  StructorType Type);
304  const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
305  const CXXConstructorDecl *D,
306  CXXCtorType CtorKind,
307  unsigned ExtraPrefixArgs,
308  unsigned ExtraSuffixArgs,
309  bool PassProtoArgs = true);
310 
311  const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
312  const FunctionProtoType *type,
313  RequiredArgs required,
314  unsigned numPrefixArgs);
315  const CGFunctionInfo &
316  arrangeUnprototypedMustTailThunk(const CXXMethodDecl *MD);
317  const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
318  CXXCtorType CT);
320  const FunctionProtoType *FTP,
321  const CXXMethodDecl *MD);
322 
323  /// "Arrange" the LLVM information for a call or type with the given
324  /// signature. This is largely an internal method; other clients
325  /// should use one of the above routines, which ultimately defer to
326  /// this.
327  ///
328  /// \param argTypes - must all actually be canonical as params
329  const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
330  bool instanceMethod,
331  bool chainCall,
332  ArrayRef<CanQualType> argTypes,
335  RequiredArgs args);
336 
337  /// Compute a new LLVM record layout object for the given record.
338  CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
339  llvm::StructType *Ty);
340 
341  /// addRecordTypeName - Compute a name from the given record decl with an
342  /// optional suffix and name the given LLVM type using it.
343  void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
344  StringRef suffix);
345 
346 
347 public: // These are internal details of CGT that shouldn't be used externally.
348  /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
349  llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
350 
351  /// getExpandedTypes - Expand the type \arg Ty into the LLVM
352  /// argument types it would be passed as. See ABIArgInfo::Expand.
353  void getExpandedTypes(QualType Ty,
355 
356  /// IsZeroInitializable - Return whether a type can be
357  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
358  bool isZeroInitializable(QualType T);
359 
360  /// Check if the pointer type can be zero-initialized (in the C++ sense)
361  /// with an LLVM zeroinitializer.
362  bool isPointerZeroInitializable(QualType T);
363 
364  /// IsZeroInitializable - Return whether a record type can be
365  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
366  bool isZeroInitializable(const RecordDecl *RD);
367 
368  bool isRecordLayoutComplete(const Type *Ty) const;
369  bool noRecordsBeingLaidOut() const {
370  return RecordsBeingLaidOut.empty();
371  }
372  bool isRecordBeingLaidOut(const Type *Ty) const {
373  return RecordsBeingLaidOut.count(Ty);
374  }
375 
376 };
377 
378 } // end namespace CodeGen
379 } // end namespace clang
380 
381 #endif
bool noRecordsBeingLaidOut() const
Definition: CodeGenTypes.h:369
CGCXXABI & getCXXABI() const
Definition: CodeGenTypes.h:176
Represents a function declaration or definition.
Definition: Decl.h:1739
Complete object ctor.
Definition: ABI.h:26
A (possibly-)qualified type.
Definition: Type.h:642
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
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
const ABIInfo & getABIInfo() const
Definition: CodeGenTypes.h:174
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3361
bool isRecordBeingLaidOut(const Type *Ty) const
Definition: CodeGenTypes.h:372
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:170
The base class of the type hierarchy.
Definition: Type.h:1415
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2481
Default closure variant of a ctor.
Definition: ABI.h:30
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
const CGFunctionInfo & arrangeCXXMethodType(CodeGenModule &CGM, const CXXRecordDecl *RD, const FunctionProtoType *FTP, const CXXMethodDecl *MD)
Represents a struct/union/class.
Definition: Decl.h:3602
Description of a constructor that was inherited from a base class.
Definition: DeclCXX.h:2455
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
StructorType getFromDtorType(CXXDtorType T)
Definition: CodeGenTypes.h:103
const CGFunctionInfo & arrangeFreeFunctionType(CodeGenModule &CGM, CanQual< FunctionProtoType > Ty, const FunctionDecl *FD)
Base object ctor.
Definition: ABI.h:27
Deleting dtor.
Definition: ABI.h:35
Enums/classes describing ABI related information about constructors, destructors and thunks...
const CGFunctionInfo & arrangeObjCMessageSendSignature(CodeGenModule &CGM, const ObjCMethodDecl *MD, QualType receiverType)
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3686
Exposes information about the current target.
Definition: TargetInfo.h:54
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
A class for recording the number of arguments that a function signature requires. ...
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:236
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:35
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3073
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2041
const CGFunctionInfo & arrangeFreeFunctionCall(CodeGenModule &CGM, CanQualType returnType, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, RequiredArgs args)
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:177
Complete object dtor.
Definition: ABI.h:36
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:356
const TargetInfo & getTarget() const
Definition: CodeGenTypes.h:175
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
Optional< types::ID > Type
Dataflow Directional Tag Classes.
CXXDtorType toCXXDtorType(StructorType T)
Definition: CodeGenTypes.h:91
static const CGFunctionInfo & arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool instanceMethod, SmallVectorImpl< CanQualType > &prefix, CanQual< FunctionProtoType > FTP, const FunctionDecl *FD)
Arrange the LLVM function layout for a value of the given function type, on top of any implicit param...
Definition: CGCall.cpp:168
StructorType getFromCtorType(CXXCtorType T)
Definition: CodeGenTypes.h:76
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:44
This class organizes the cross-module state that is used while lowering AST types to LLVM types...
Definition: CodeGenTypes.h:119
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
ASTContext & getContext() const
Definition: CodeGenTypes.h:173
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
Copying closure variant of a ctor.
Definition: ABI.h:29
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions...
Definition: ABIInfo.h:51
CXXCtorType toCXXCtorType(StructorType T)
Definition: CodeGenTypes.h:64
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:260
A class which abstracts out some details necessary for making a call.
Definition: Type.h:3472