clang  3.7.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 ABIInfo;
35 class ASTContext;
36 template <typename> class CanQual;
37 class CXXConstructorDecl;
38 class CXXDestructorDecl;
39 class CXXMethodDecl;
40 class CodeGenOptions;
41 class FieldDecl;
42 class FunctionProtoType;
43 class ObjCInterfaceDecl;
44 class ObjCIvarDecl;
45 class PointerType;
46 class QualType;
47 class RecordDecl;
48 class TagDecl;
49 class TargetInfo;
50 class Type;
51 typedef CanQual<Type> CanQualType;
52 
53 namespace CodeGen {
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 llvm::DataLayout &TheDataLayout;
126  const TargetInfo &Target;
127  CGCXXABI &TheCXXABI;
128 
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;
132 
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;
138 
139  /// Maps clang struct type with corresponding record layout info.
140  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
141 
142  /// Contains the LLVM IR type for any converted RecordDecl.
143  llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
144 
145  /// Hold memoized CGFunctionInfo results.
146  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
147 
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;
153 
154  llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
155 
156  /// True if we didn't layout a function due to a being inside
157  /// a recursive struct conversion, set this to true.
158  bool SkippedLayout;
159 
160  SmallVector<const RecordDecl *, 8> DeferredRecords;
161 
162 private:
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;
166 
167 public:
169  ~CodeGenTypes();
170 
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(); }
177 
178  /// ConvertType - Convert type T into a llvm::Type.
179  llvm::Type *ConvertType(QualType T);
180 
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);
186 
187  /// GetFunctionType - Get the LLVM function type for \arg Info.
188  llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
189 
190  llvm::FunctionType *GetFunctionType(GlobalDecl GD);
191 
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
194  /// type).
195  bool isFuncTypeConvertible(const FunctionType *FT);
197 
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);
202 
204 
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);
208 
209  /// getNullaryFunctionInfo - Get the function info for a void()
210  /// function with standard CC.
212 
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.
221  //
222  // Key points:
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
227  // go awry.
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.
232 
235  const CGFunctionInfo &
237  const FunctionType::ExtInfo &Info,
238  bool isVariadic);
239 
242  QualType receiverType);
243 
248  const CXXConstructorDecl *D,
249  CXXCtorType CtorKind,
250  unsigned ExtraArgs);
252  const FunctionType *Ty,
253  bool ChainCall);
255  const CallArgList &args,
257  RequiredArgs required);
259  const FunctionType *type);
260 
262  const FunctionProtoType *type,
263  RequiredArgs required);
266  CXXCtorType CT);
267 
271  const FunctionProtoType *FTP);
272 
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
276  /// this.
277  ///
278  /// \param argTypes - must all actually be canonical as params
280  bool instanceMethod,
281  bool chainCall,
282  ArrayRef<CanQualType> argTypes,
284  RequiredArgs args);
285 
286  /// \brief Compute a new LLVM record layout object for the given record.
288  llvm::StructType *Ty);
289 
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,
293  StringRef suffix);
294 
295 
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);
299 
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,
304 
305  /// IsZeroInitializable - Return whether a type can be
306  /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
308 
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);
312 
313  bool isRecordLayoutComplete(const Type *Ty) const;
314  bool noRecordsBeingLaidOut() const {
315  return RecordsBeingLaidOut.empty();
316  }
317  bool isRecordBeingLaidOut(const Type *Ty) const {
318  return RecordsBeingLaidOut.count(Ty);
319  }
320 
321 };
322 
323 } // end namespace CodeGen
324 } // end namespace clang
325 
326 #endif
void getExpandedTypes(QualType Ty, SmallVectorImpl< llvm::Type * >::iterator &TI)
Definition: CGCall.cpp:707
Complete object ctor.
Definition: ABI.h:26
void UpdateCompletedType(const TagDecl *TD)
The COMDAT used for ctors.
Definition: ABI.h:28
void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, StringRef suffix)
CGCXXABI & getCXXABI() const
Definition: CodeGenTypes.h:175
ASTContext & getContext() const
Definition: CodeGenTypes.h:172
bool isFuncTypeConvertible(const FunctionType *FT)
const CGFunctionInfo & arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args, const FunctionType::ExtInfo &Info, bool isVariadic)
Definition: CGCall.cpp:460
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2147
Default closure variant of a ctor.
Definition: ABI.h:30
const CGFunctionInfo & arrangeCXXStructorDeclaration(const CXXMethodDecl *MD, StructorType Type)
Definition: CGCall.cpp:193
llvm::Type * ConvertTypeForMem(QualType T)
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Definition: CGCall.cpp:112
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:89
StructorType getFromDtorType(CXXDtorType T)
Definition: CodeGenTypes.h:104
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, bool instanceMethod, bool chainCall, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, RequiredArgs args)
Definition: CGCall.cpp:485
Base object ctor.
Definition: ABI.h:27
Deleting dtor.
Definition: ABI.h:35
const CGFunctionInfo & arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, QualType receiverType)
Definition: CGCall.cpp:295
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
Definition: CGCall.cpp:177
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
Definition: CGCall.cpp:322
Exposes information about the current target.
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
CodeGenTypes(CodeGenModule &cgm)
const CGFunctionInfo & arrangeNullaryFunction()
Definition: CGCall.cpp:475
Base object dtor.
Definition: ABI.h:37
The COMDAT used for dtors.
Definition: ABI.h:38
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:171
TagDecl - Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:2694
const CGFunctionInfo & arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD)
Definition: CGCall.cpp:282
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1717
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:176
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:446
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
const CGFunctionInfo & arrangeCXXMethodType(const CXXRecordDecl *RD, const FunctionProtoType *FTP)
Definition: CGCall.cpp:157
const CGFunctionInfo & arrangeBlockFunctionCall(const CallArgList &args, const FunctionType *type)
Definition: CGCall.cpp:424
bool isRecordBeingLaidOut(const Type *Ty) const
Definition: CodeGenTypes.h:317
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1639
CXXDtorType toCXXDtorType(StructorType T)
Definition: CodeGenTypes.h:92
const CGFunctionInfo & arrangeFunctionDeclaration(const FunctionDecl *FD)
Definition: CGCall.cpp:257
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
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:42
const CGFunctionInfo & arrangeMSMemberPointerThunk(const CXXMethodDecl *MD)
Definition: CGCall.cpp:341
bool isRecordLayoutComplete(const Type *Ty) const
llvm::Type * GetFunctionTypeForVTable(GlobalDecl GD)
Definition: CGCall.cpp:1372
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
bool isFuncParamTypeConvertible(QualType Ty)
const TargetInfo & getTarget() const
Definition: CodeGenTypes.h:174
Copying closure variant of a ctor.
Definition: ABI.h:29
const CGFunctionInfo & arrangeMSCtorClosure(const CXXConstructorDecl *CD, CXXCtorType CT)
Definition: CGCall.cpp:351
const ABIInfo & getABIInfo() const
Definition: CodeGenTypes.h:173
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Definition: CGCall.cpp:414
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.
bool noRecordsBeingLaidOut() const
Definition: CodeGenTypes.h:314
bool isZeroInitializable(QualType T)
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:230
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1253