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 CLANG_CODEGEN_CODEGENTYPES_H
15 #define CLANG_CODEGEN_CODEGENTYPES_H
16 
17 #include "CGCall.h"
18 #include "clang/AST/GlobalDecl.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/Module.h"
21 #include <vector>
22 
23 namespace llvm {
24   class FunctionType;
25   class Module;
26   class DataLayout;
27   class Type;
28   class LLVMContext;
29   class StructType;
30 }
31 
32 namespace clang {
33   class ABIInfo;
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 CGCXXABI;
54   class CGRecordLayout;
55   class CodeGenModule;
56   class RequiredArgs;
57 
58 /// CodeGenTypes - This class organizes the cross-module state that is used
59 /// while lowering AST types to LLVM types.
60 class CodeGenTypes {
61   CodeGenModule &CGM;
62   // Some of this stuff should probably be left on the CGM.
63   ASTContext &Context;
64   llvm::Module &TheModule;
65   const llvm::DataLayout &TheDataLayout;
66   const TargetInfo &Target;
67   CGCXXABI &TheCXXABI;
68   const CodeGenOptions &CodeGenOpts;
69 
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;
73 
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;
79 
80   /// CGRecordLayouts - This maps llvm struct type with corresponding
81   /// record layout info.
82   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
83 
84   /// RecordDeclTypes - This contains the LLVM IR type for any converted
85   /// RecordDecl.
86   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
87 
88   /// FunctionInfos - Hold memoized CGFunctionInfo results.
89   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
90 
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;
96 
97   llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
98 
99   /// SkippedLayout - True if we didn't layout a function due to a being inside
100   /// a recursive struct conversion, set this to true.
101   bool SkippedLayout;
102 
103   SmallVector<const RecordDecl *, 8> DeferredRecords;
104 
105 private:
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;
109 
110 public:
111   CodeGenTypes(CodeGenModule &cgm);
112   ~CodeGenTypes();
113 
114   const llvm::DataLayout &getDataLayout() const { return TheDataLayout; }
115   ASTContext &getContext() const { return Context; }
116   const ABIInfo &getABIInfo() const { return TheABIInfo; }
117   const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
118   const TargetInfo &getTarget() const { return Target; }
119   CGCXXABI &getCXXABI() const { return TheCXXABI; }
120   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
121 
122   /// ConvertType - Convert type T into a llvm::Type.
123   llvm::Type *ConvertType(QualType T);
124 
125   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
126   /// ConvertType in that it is used to convert to the memory representation for
127   /// a type.  For example, the scalar representation for _Bool is i1, but the
128   /// memory representation is usually i8 or i32, depending on the target.
129   llvm::Type *ConvertTypeForMem(QualType T);
130 
131   /// GetFunctionType - Get the LLVM function type for \arg Info.
132   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
133 
134   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
135 
136   /// isFuncTypeConvertible - Utility to check whether a function type can
137   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
138   /// type).
139   bool isFuncTypeConvertible(const FunctionType *FT);
140   bool isFuncTypeArgumentConvertible(QualType Ty);
141 
142   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
143   /// given a CXXMethodDecl. If the method to has an incomplete return type,
144   /// and/or incomplete argument types, this will return the opaque type.
145   llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
146 
147   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
148 
149   /// UpdateCompletedType - When we find the full definition for a TagDecl,
150   /// replace the 'opaque' type we previously made for it if applicable.
151   void UpdateCompletedType(const TagDecl *TD);
152 
153   /// getNullaryFunctionInfo - Get the function info for a void()
154   /// function with standard CC.
155   const CGFunctionInfo &arrangeNullaryFunction();
156 
157   // The arrangement methods are split into three families:
158   //   - those meant to drive the signature and prologue/epilogue
159   //     of a function declaration or definition,
160   //   - those meant for the computation of the LLVM type for an abstract
161   //     appearance of a function, and
162   //   - those meant for performing the IR-generation of a call.
163   // They differ mainly in how they deal with optional (i.e. variadic)
164   // arguments, as well as unprototyped functions.
165   //
166   // Key points:
167   // - The CGFunctionInfo for emitting a specific call site must include
168   //   entries for the optional arguments.
169   // - The function type used at the call site must reflect the formal
170   //   signature of the declaration being called, or else the call will
171   //   go awry.
172   // - For the most part, unprototyped functions are called by casting to
173   //   a formal signature inferred from the specific argument types used
174   //   at the call-site.  However, some targets (e.g. x86-64) screw with
175   //   this for compatibility reasons.
176 
177   const CGFunctionInfo &arrangeGlobalDeclaration(GlobalDecl GD);
178   const CGFunctionInfo &arrangeFunctionDeclaration(const FunctionDecl *FD);
179   const CGFunctionInfo &arrangeFunctionDeclaration(QualType ResTy,
180                                                    const FunctionArgList &Args,
181                                              const FunctionType::ExtInfo &Info,
182                                                    bool isVariadic);
183 
184   const CGFunctionInfo &arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD);
185   const CGFunctionInfo &arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
186                                                         QualType receiverType);
187 
188   const CGFunctionInfo &arrangeCXXMethodDeclaration(const CXXMethodDecl *MD);
189   const CGFunctionInfo &arrangeCXXConstructorDeclaration(
190                                                     const CXXConstructorDecl *D,
191                                                     CXXCtorType Type);
192   const CGFunctionInfo &arrangeCXXDestructor(const CXXDestructorDecl *D,
193                                              CXXDtorType Type);
194 
195   const CGFunctionInfo &arrangeFreeFunctionCall(const CallArgList &Args,
196                                                 const FunctionType *Ty);
197   const CGFunctionInfo &arrangeFreeFunctionCall(QualType ResTy,
198                                                 const CallArgList &args,
199                                                 FunctionType::ExtInfo info,
200                                                 RequiredArgs required);
201   const CGFunctionInfo &arrangeBlockFunctionCall(const CallArgList &args,
202                                                  const FunctionType *type);
203 
204   const CGFunctionInfo &arrangeCXXMethodCall(const CallArgList &args,
205                                              const FunctionProtoType *type,
206                                              RequiredArgs required);
207 
208   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionProtoType> Ty);
209   const CGFunctionInfo &arrangeFreeFunctionType(CanQual<FunctionNoProtoType> Ty);
210   const CGFunctionInfo &arrangeCXXMethodType(const CXXRecordDecl *RD,
211                                              const FunctionProtoType *FTP);
212 
213   /// "Arrange" the LLVM information for a call or type with the given
214   /// signature.  This is largely an internal method; other clients
215   /// should use one of the above routines, which ultimately defer to
216   /// this.
217   ///
218   /// \param argTypes - must all actually be canonical as params
219   const CGFunctionInfo &arrangeLLVMFunctionInfo(CanQualType returnType,
220                                                 ArrayRef<CanQualType> argTypes,
221                                                 FunctionType::ExtInfo info,
222                                                 RequiredArgs args);
223 
224   /// \brief Compute a new LLVM record layout object for the given record.
225   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
226                                       llvm::StructType *Ty);
227 
228   /// addRecordTypeName - Compute a name from the given record decl with an
229   /// optional suffix and name the given LLVM type using it.
230   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
231                          StringRef suffix);
232 
233 
234 public:  // These are internal details of CGT that shouldn't be used externally.
235   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
236   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
237 
238   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
239   /// argument types it would be passed as on the provided vector \arg
240   /// ArgTys. See ABIArgInfo::Expand.
241   void GetExpandedTypes(QualType type,
242                         SmallVectorImpl<llvm::Type*> &expanded);
243 
244   /// IsZeroInitializable - Return whether a type can be
245   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
246   bool isZeroInitializable(QualType T);
247 
248   /// IsZeroInitializable - Return whether a record type can be
249   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
250   bool isZeroInitializable(const CXXRecordDecl *RD);
251 
252   bool isRecordLayoutComplete(const Type *Ty) const;
253   bool noRecordsBeingLaidOut() const {
254     return RecordsBeingLaidOut.empty();
255   }
256   bool isRecordBeingLaidOut(const Type *Ty) const {
257     return RecordsBeingLaidOut.count(Ty);
258   }
259 
260 };
261 
262 }  // end namespace CodeGen
263 }  // end namespace clang
264 
265 #endif
266