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"
19 #include "clang/CodeGen/CGFunctionInfo.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
toCXXCtorType(StructorType T)65 inline CXXCtorType toCXXCtorType(StructorType T) {
66 switch (T) {
67 case StructorType::Complete:
68 return Ctor_Complete;
69 case StructorType::Base:
70 return Ctor_Base;
71 case StructorType::Deleting:
72 llvm_unreachable("cannot have a deleting ctor");
73 }
74 llvm_unreachable("not a StructorType");
75 }
76
getFromCtorType(CXXCtorType T)77 inline StructorType getFromCtorType(CXXCtorType T) {
78 switch (T) {
79 case Ctor_Complete:
80 return StructorType::Complete;
81 case Ctor_Base:
82 return StructorType::Base;
83 case Ctor_Comdat:
84 llvm_unreachable("not expecting a COMDAT");
85 }
86 llvm_unreachable("not a CXXCtorType");
87 }
88
toCXXDtorType(StructorType T)89 inline CXXDtorType toCXXDtorType(StructorType T) {
90 switch (T) {
91 case StructorType::Complete:
92 return Dtor_Complete;
93 case StructorType::Base:
94 return Dtor_Base;
95 case StructorType::Deleting:
96 return Dtor_Deleting;
97 }
98 llvm_unreachable("not a StructorType");
99 }
100
getFromDtorType(CXXDtorType T)101 inline StructorType getFromDtorType(CXXDtorType T) {
102 switch (T) {
103 case Dtor_Deleting:
104 return StructorType::Deleting;
105 case Dtor_Complete:
106 return StructorType::Complete;
107 case Dtor_Base:
108 return StructorType::Base;
109 case Dtor_Comdat:
110 llvm_unreachable("not expecting a COMDAT");
111 }
112 llvm_unreachable("not a CXXDtorType");
113 }
114
115 /// CodeGenTypes - This class organizes the cross-module state that is used
116 /// while lowering AST types to LLVM types.
117 class CodeGenTypes {
118 CodeGenModule &CGM;
119 // Some of this stuff should probably be left on the CGM.
120 ASTContext &Context;
121 llvm::Module &TheModule;
122 const llvm::DataLayout &TheDataLayout;
123 const TargetInfo &Target;
124 CGCXXABI &TheCXXABI;
125
126 // This should not be moved earlier, since its initialization depends on some
127 // of the previous reference members being already initialized
128 const ABIInfo &TheABIInfo;
129
130 /// The opaque type map for Objective-C interfaces. All direct
131 /// manipulation is done by the runtime interfaces, which are
132 /// responsible for coercing to the appropriate type; these opaque
133 /// types are never refined.
134 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
135
136 /// CGRecordLayouts - This maps llvm struct type with corresponding
137 /// record layout info.
138 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
139
140 /// RecordDeclTypes - This contains the LLVM IR type for any converted
141 /// RecordDecl.
142 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
143
144 /// FunctionInfos - Hold memoized CGFunctionInfo results.
145 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
146
147 /// RecordsBeingLaidOut - This set keeps track of records that we're currently
148 /// converting 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 /// SkippedLayout - 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 private:
162 /// TypeCache - This map keeps cache of llvm::Types
163 /// and maps clang::Type to corresponding llvm::Type.
164 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
165
166 public:
167 CodeGenTypes(CodeGenModule &cgm);
168 ~CodeGenTypes();
169
getDataLayout()170 const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
getContext()171 ASTContext &getContext() const { return Context; }
getABIInfo()172 const ABIInfo &getABIInfo() const { return TheABIInfo; }
getTarget()173 const TargetInfo &getTarget() const { return Target; }
getCXXABI()174 CGCXXABI &getCXXABI() const { return TheCXXABI; }
getLLVMContext()175 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
176
177 /// ConvertType - Convert type T into a llvm::Type.
178 llvm::Type *ConvertType(QualType T);
179
180 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
181 /// ConvertType in that it is used to convert to the memory representation for
182 /// a type. For example, the scalar representation for _Bool is i1, but the
183 /// memory representation is usually i8 or i32, depending on the target.
184 llvm::Type *ConvertTypeForMem(QualType T);
185
186 /// GetFunctionType - Get the LLVM function type for \arg Info.
187 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
188
189 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
190
191 /// isFuncTypeConvertible - Utility to check whether a function type can
192 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
193 /// type).
194 bool isFuncTypeConvertible(const FunctionType *FT);
195 bool isFuncParamTypeConvertible(QualType Ty);
196
197 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
198 /// given a CXXMethodDecl. If the method to has an incomplete return type,
199 /// and/or incomplete argument types, this will return the opaque type.
200 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
201
202 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
203
204 /// UpdateCompletedType - When we find the full definition for a TagDecl,
205 /// replace the 'opaque' type we previously made for it if applicable.
206 void UpdateCompletedType(const TagDecl *TD);
207
208 /// getNullaryFunctionInfo - Get the function info for a void()
209 /// function with standard CC.
210 const CGFunctionInfo &arrangeNullaryFunction();
211
212 // The arrangement methods are split into three families:
213 // - those meant to drive the signature and prologue/epilogue
214 // of a function declaration or definition,
215 // - those meant for the computation of the LLVM type for an abstract
216 // appearance of a function, and
217 // - those meant for performing the IR-generation of a call.
218 // They differ mainly in how they deal with optional (i.e. variadic)
219 // arguments, as well as unprototyped functions.
220 //
221 // Key points:
222 // - The CGFunctionInfo for emitting a specific call site must include
223 // entries for the optional arguments.
224 // - The function type used at the call site must reflect the formal
225 // signature of the declaration being called, or else the call will
226 // go awry.
227 // - For the most part, unprototyped functions are called by casting to
228 // a formal signature inferred from the specific argument types used
229 // at the call-site. However, some targets (e.g. x86-64) screw with
230 // this for compatibility reasons.
231
232 const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
233 const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
234 const CGFunctionInfo &
235 arrangeFreeFunctionDeclaration(QualType ResTy, const FunctionArgList &Args,
236 const FunctionType::ExtInfo &Info,
237 bool isVariadic);
238
239 const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
240 const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
241 QualType receiverType);
242
243 const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
244 const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
245 StructorType Type);
246 const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
247 const CXXConstructorDecl *D,
248 CXXCtorType CtorKind,
249 unsigned ExtraArgs);
250 const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
251 const FunctionType *Ty,
252 bool ChainCall);
253 const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
254 const CallArgList &args,
255 FunctionType::ExtInfo info,
256 RequiredArgs required);
257 const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
258 const FunctionType *type);
259
260 const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
261 const FunctionProtoType *type,
262 RequiredArgs required);
263 const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
264
265 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
266 const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
267 const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
268 const FunctionProtoType *FTP);
269
270 /// "Arrange" the LLVM information for a call or type with the given
271 /// signature. This is largely an internal method; other clients
272 /// should use one of the above routines, which ultimately defer to
273 /// this.
274 ///
275 /// \param argTypes - must all actually be canonical as params
276 const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
277 bool instanceMethod,
278 bool chainCall,
279 ArrayRef<CanQualType> argTypes,
280 FunctionType::ExtInfo info,
281 RequiredArgs args);
282
283 /// \brief Compute a new LLVM record layout object for the given record.
284 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
285 llvm::StructType *Ty);
286
287 /// addRecordTypeName - Compute a name from the given record decl with an
288 /// optional suffix and name the given LLVM type using it.
289 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
290 StringRef suffix);
291
292
293 public: // These are internal details of CGT that shouldn't be used externally.
294 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
295 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
296
297 /// getExpandedTypes - Expand the type \arg Ty into the LLVM
298 /// argument types it would be passed as. See ABIArgInfo::Expand.
299 void getExpandedTypes(QualType Ty,
300 SmallVectorImpl<llvm::Type *>::iterator &TI);
301
302 /// IsZeroInitializable - Return whether a type can be
303 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
304 bool isZeroInitializable(QualType T);
305
306 /// IsZeroInitializable - Return whether a record type can be
307 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
308 bool isZeroInitializable(const CXXRecordDecl *RD);
309
310 bool isRecordLayoutComplete(const Type *Ty) const;
noRecordsBeingLaidOut()311 bool noRecordsBeingLaidOut() const {
312 return RecordsBeingLaidOut.empty();
313 }
isRecordBeingLaidOut(const Type * Ty)314 bool isRecordBeingLaidOut(const Type *Ty) const {
315 return RecordsBeingLaidOut.count(Ty);
316 }
317
318 };
319
320 } // end namespace CodeGen
321 } // end namespace clang
322
323 #endif
324