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 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 
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 
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   case Ctor_CopyingClosure:
86   case Ctor_DefaultClosure:
87     llvm_unreachable("not expecting a closure");
88   }
89   llvm_unreachable("not a CXXCtorType");
90 }
91 
92 inline CXXDtorType toCXXDtorType(StructorType T) {
93   switch (T) {
94   case StructorType::Complete:
95     return Dtor_Complete;
96   case StructorType::Base:
97     return Dtor_Base;
98   case StructorType::Deleting:
99     return Dtor_Deleting;
100   }
101   llvm_unreachable("not a StructorType");
102 }
103 
104 inline StructorType getFromDtorType(CXXDtorType T) {
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.
120 class CodeGenTypes {
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 public:
168   CodeGenTypes(CodeGenModule &cgm);
169   ~CodeGenTypes();
170 
171   const llvm::DataLayout &getDataLayout() const {
172     return TheModule.getDataLayout();
173   }
174   ASTContext &getContext() const { return Context; }
175   const ABIInfo &getABIInfo() const { return TheABIInfo; }
176   const TargetInfo &getTarget() const { return Target; }
177   CGCXXABI &getCXXABI() const { return TheCXXABI; }
178   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
179 
180   /// ConvertType - Convert type T into a llvm::Type.
181   llvm::Type *ConvertType(QualType T);
182 
183   /// \brief Converts the GlobalDecl into an llvm::Type. This should be used
184   /// when we know the target of the function we want to convert.  This is
185   /// because some functions (explicitly, those with pass_object_size
186   /// parameters) may not have the same signature as their type portrays, and
187   /// can only be called directly.
188   llvm::Type *ConvertFunctionType(QualType FT,
189                                   const FunctionDecl *FD = nullptr);
190 
191   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
192   /// ConvertType in that it is used to convert to the memory representation for
193   /// a type.  For example, the scalar representation for _Bool is i1, but the
194   /// memory representation is usually i8 or i32, depending on the target.
195   llvm::Type *ConvertTypeForMem(QualType T);
196 
197   /// GetFunctionType - Get the LLVM function type for \arg Info.
198   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
199 
200   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
201 
202   /// isFuncTypeConvertible - Utility to check whether a function type can
203   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
204   /// type).
205   bool isFuncTypeConvertible(const FunctionType *FT);
206   bool isFuncParamTypeConvertible(QualType Ty);
207 
208   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
209   /// given a CXXMethodDecl. If the method to has an incomplete return type,
210   /// and/or incomplete argument types, this will return the opaque type.
211   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
212 
213   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
214 
215   /// UpdateCompletedType - When we find the full definition for a TagDecl,
216   /// replace the 'opaque' type we previously made for it if applicable.
217   void UpdateCompletedType(const TagDecl *TD);
218 
219   /// \brief Remove stale types from the type cache when an inheritance model
220   /// gets assigned to a class.
221   void RefreshTypeCacheForClass(const CXXRecordDecl *RD);
222 
223   // The arrangement methods are split into three families:
224   //   - those meant to drive the signature and prologue/epilogue
225   //     of a function declaration or definition,
226   //   - those meant for the computation of the LLVM type for an abstract
227   //     appearance of a function, and
228   //   - those meant for performing the IR-generation of a call.
229   // They differ mainly in how they deal with optional (i.e. variadic)
230   // arguments, as well as unprototyped functions.
231   //
232   // Key points:
233   // - The CGFunctionInfo for emitting a specific call site must include
234   //   entries for the optional arguments.
235   // - The function type used at the call site must reflect the formal
236   //   signature of the declaration being called, or else the call will
237   //   go awry.
238   // - For the most part, unprototyped functions are called by casting to
239   //   a formal signature inferred from the specific argument types used
240   //   at the call-site.  However, some targets (e.g. x86-64) screw with
241   //   this for compatibility reasons.
242 
243   const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
244 
245   /// Given a function info for a declaration, return the function info
246   /// for a call with the given arguments.
247   ///
248   /// Often this will be able to simply return the declaration info.
249   const CGFunctionInfo &arrangeCall(const CGFunctionInfo &declFI,
250                                     const CallArgList &args);
251 
252   /// Free functions are functions that are compatible with an ordinary
253   /// C function pointer type.
254   const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
255   const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
256                                                 const FunctionType *Ty,
257                                                 bool ChainCall);
258   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty,
259                                                 const FunctionDecl *FD);
260   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
261 
262   /// A nullary function is a freestanding function of type 'void ()'.
263   /// This method works for both calls and declarations.
264   const CGFunctionInfo &arrangeNullaryFunction();
265 
266   /// A builtin function is a freestanding function using the default
267   /// C conventions.
268   const CGFunctionInfo &
269   arrangeBuiltinFunctionDeclaration(QualType resultType,
270                                     const FunctionArgList &args);
271   const CGFunctionInfo &
272   arrangeBuiltinFunctionDeclaration(CanQualType resultType,
273                                     ArrayRef<CanQualType> argTypes);
274   const CGFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
275                                                    const CallArgList &args);
276 
277   /// Objective-C methods are C functions with some implicit parameters.
278   const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
279   const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
280                                                         QualType receiverType);
281   const CGFunctionInfo &arrangeUnprototypedObjCMessageSend(
282                                                      QualType returnType,
283                                                      const CallArgList &args);
284 
285   /// Block invocation functions are C functions with an implicit parameter.
286   const CGFunctionInfo &arrangeBlockFunctionDeclaration(
287                                                  const FunctionProtoType *type,
288                                                  const FunctionArgList &args);
289   const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
290                                                  const FunctionType *type);
291 
292   /// C++ methods have some special rules and also have implicit parameters.
293   const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
294   const CGFunctionInfo &arrangeCXXStructorDeclaration(const CXXMethodDecl *MD,
295                                                       StructorType Type);
296   const CGFunctionInfo &arrangeCXXConstructorCall(const CallArgList &Args,
297                                                   const CXXConstructorDecl *D,
298                                                   CXXCtorType CtorKind,
299                                                   unsigned ExtraArgs);
300 
301   const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
302                                              const FunctionProtoType *type,
303                                              RequiredArgs required);
304   const CGFunctionInfo &arrangeMSMemberPointerThunk(const CXXMethodDecl *MD);
305   const CGFunctionInfo &arrangeMSCtorClosure(const CXXConstructorDecl *CD,
306                                                  CXXCtorType CT);
307   const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
308                                              const FunctionProtoType *FTP,
309                                              const CXXMethodDecl *MD);
310 
311   /// "Arrange" the LLVM information for a call or type with the given
312   /// signature.  This is largely an internal method; other clients
313   /// should use one of the above routines, which ultimately defer to
314   /// this.
315   ///
316   /// \param argTypes - must all actually be canonical as params
317   const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
318                                                 bool instanceMethod,
319                                                 bool chainCall,
320                                                 ArrayRef<CanQualType> argTypes,
321                                                 FunctionType::ExtInfo info,
322                     ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos,
323                                                 RequiredArgs args);
324 
325   /// \brief Compute a new LLVM record layout object for the given record.
326   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
327                                       llvm::StructType *Ty);
328 
329   /// addRecordTypeName - Compute a name from the given record decl with an
330   /// optional suffix and name the given LLVM type using it.
331   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
332                          StringRef suffix);
333 
334 
335 public:  // These are internal details of CGT that shouldn't be used externally.
336   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
337   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
338 
339   /// getExpandedTypes - Expand the type \arg Ty into the LLVM
340   /// argument types it would be passed as. See ABIArgInfo::Expand.
341   void getExpandedTypes(QualType Ty,
342                         SmallVectorImpl<llvm::Type *>::iterator &TI);
343 
344   /// IsZeroInitializable - Return whether a type can be
345   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
346   bool isZeroInitializable(QualType T);
347 
348   /// IsZeroInitializable - Return whether a record type can be
349   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
350   bool isZeroInitializable(const RecordDecl *RD);
351 
352   bool isRecordLayoutComplete(const Type *Ty) const;
353   bool noRecordsBeingLaidOut() const {
354     return RecordsBeingLaidOut.empty();
355   }
356   bool isRecordBeingLaidOut(const Type *Ty) const {
357     return RecordsBeingLaidOut.count(Ty);
358   }
359 
360 };
361 
362 }  // end namespace CodeGen
363 }  // end namespace clang
364 
365 #endif
366