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 CLANG_CODEGEN_CODEGENTYPES_H
15 #define CLANG_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 /// CodeGenTypes - This class organizes the cross-module state that is used
60 /// while lowering AST types to LLVM types.
63 // Some of this stuff should probably be left on the CGM.
65 llvm::Module &TheModule;
66 const llvm::DataLayout &TheDataLayout;
67 const TargetInfo &Target;
70 // This should not be moved earlier, since its initialization depends on some
71 // of the previous reference members being already initialized
72 const ABIInfo &TheABIInfo;
74 /// The opaque type map for Objective-C interfaces. All direct
75 /// manipulation is done by the runtime interfaces, which are
76 /// responsible for coercing to the appropriate type; these opaque
77 /// types are never refined.
78 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
80 /// CGRecordLayouts - This maps llvm struct type with corresponding
81 /// record layout info.
82 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
84 /// RecordDeclTypes - This contains the LLVM IR type for any converted
86 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
88 /// FunctionInfos - Hold memoized CGFunctionInfo results.
89 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
91 /// RecordsBeingLaidOut - This set keeps track of records that we're currently
92 /// converting to an IR type. For example, when converting:
93 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
94 /// types will be in this set.
95 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
97 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
99 /// SkippedLayout - True if we didn't layout a function due to a being inside
100 /// a recursive struct conversion, set this to true.
103 SmallVector<const RecordDecl *, 8> DeferredRecords;
106 /// TypeCache - This map keeps cache of llvm::Types
107 /// and maps llvm::Types to corresponding clang::Type.
108 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
111 CodeGenTypes(CodeGenModule &cgm);
114 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
115 ASTContext &getContext() const { return Context; }
116 const ABIInfo &getABIInfo() const { return TheABIInfo; }
117 const TargetInfo &getTarget() const { return Target; }
118 CGCXXABI &getCXXABI() const { return TheCXXABI; }
119 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
121 /// ConvertType - Convert type T into a llvm::Type.
122 llvm::Type *ConvertType(QualType T);
124 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
125 /// ConvertType in that it is used to convert to the memory representation for
126 /// a type. For example, the scalar representation for _Bool is i1, but the
127 /// memory representation is usually i8 or i32, depending on the target.
128 llvm::Type *ConvertTypeForMem(QualType T);
130 /// GetFunctionType - Get the LLVM function type for \arg Info.
131 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
133 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
135 /// isFuncTypeConvertible - Utility to check whether a function type can
136 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
138 bool isFuncTypeConvertible(const FunctionType *FT);
139 bool isFuncTypeArgumentConvertible(QualType Ty);
141 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
142 /// given a CXXMethodDecl. If the method to has an incomplete return type,
143 /// and/or incomplete argument types, this will return the opaque type.
144 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
146 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
148 /// UpdateCompletedType - When we find the full definition for a TagDecl,
149 /// replace the 'opaque' type we previously made for it if applicable.
150 void UpdateCompletedType(const TagDecl *TD);
152 /// getNullaryFunctionInfo - Get the function info for a void()
153 /// function with standard CC.
154 const CGFunctionInfo &arrangeNullaryFunction();
156 // The arrangement methods are split into three families:
157 // - those meant to drive the signature and prologue/epilogue
158 // of a function declaration or definition,
159 // - those meant for the computation of the LLVM type for an abstract
160 // appearance of a function, and
161 // - those meant for performing the IR-generation of a call.
162 // They differ mainly in how they deal with optional (i.e. variadic)
163 // arguments, as well as unprototyped functions.
166 // - The CGFunctionInfo for emitting a specific call site must include
167 // entries for the optional arguments.
168 // - The function type used at the call site must reflect the formal
169 // signature of the declaration being called, or else the call will
171 // - For the most part, unprototyped functions are called by casting to
172 // a formal signature inferred from the specific argument types used
173 // at the call-site. However, some targets (e.g. x86-64) screw with
174 // this for compatibility reasons.
176 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
177 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
178 const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
179 const FunctionArgList &Args,
180 const FunctionType::ExtInfo &Info,
183 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
184 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
185 QualType receiverType);
187 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
188 const CGFunctionInfo &arrangeCXXConstructorDeclaration(
189 const CXXConstructorDecl *D,
191 const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
194 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
195 const FunctionType *Ty);
196 const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
197 const CallArgList &args,
198 FunctionType::ExtInfo info,
199 RequiredArgs required);
200 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
201 const FunctionType *type);
203 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
204 const FunctionProtoType *type,
205 RequiredArgs required);
207 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
208 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
209 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
210 const FunctionProtoType *FTP);
212 /// "Arrange" the LLVM information for a call or type with the given
213 /// signature. This is largely an internal method; other clients
214 /// should use one of the above routines, which ultimately defer to
217 /// \param argTypes - must all actually be canonical as params
218 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
219 ArrayRef<CanQualType> argTypes,
220 FunctionType::ExtInfo info,
223 /// \brief Compute a new LLVM record layout object for the given record.
224 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
225 llvm::StructType *Ty);
227 /// addRecordTypeName - Compute a name from the given record decl with an
228 /// optional suffix and name the given LLVM type using it.
229 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
233 public: // These are internal details of CGT that shouldn't be used externally.
234 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
235 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
237 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
238 /// argument types it would be passed as on the provided vector \arg
239 /// ArgTys. See ABIArgInfo::Expand.
240 void GetExpandedTypes(QualType type,
241 SmallVectorImpl<llvm::Type*> &expanded);
243 /// IsZeroInitializable - Return whether a type can be
244 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
245 bool isZeroInitializable(QualType T);
247 /// IsZeroInitializable - Return whether a record type can be
248 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
249 bool isZeroInitializable(const CXXRecordDecl *RD);
251 bool isRecordLayoutComplete(const Type *Ty) const;
252 bool noRecordsBeingLaidOut() const {
253 return RecordsBeingLaidOut.empty();
255 bool isRecordBeingLaidOut(const Type *Ty) const {
256 return RecordsBeingLaidOut.count(Ty);
261 } // end namespace CodeGen
262 } // end namespace clang