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/Module.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include <vector>
22 
23 namespace llvm {
24   class FunctionType;
25   class Module;
26   class TargetData;
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 
56 /// CodeGenTypes - This class organizes the cross-module state that is used
57 /// while lowering AST types to LLVM types.
58 class CodeGenTypes {
59   ASTContext &Context;
60   const TargetInfo &Target;
61   llvm::Module &TheModule;
62   const llvm::TargetData &TheTargetData;
63   const ABIInfo &TheABIInfo;
64   CGCXXABI &TheCXXABI;
65   const CodeGenOptions &CodeGenOpts;
66 
67   /// The opaque type map for Objective-C interfaces. All direct
68   /// manipulation is done by the runtime interfaces, which are
69   /// responsible for coercing to the appropriate type; these opaque
70   /// types are never refined.
71   llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
72 
73   /// CGRecordLayouts - This maps llvm struct type with corresponding
74   /// record layout info.
75   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
76 
77   /// RecordDeclTypes - This contains the LLVM IR type for any converted
78   /// RecordDecl.
79   llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
80 
81   /// FunctionInfos - Hold memoized CGFunctionInfo results.
82   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
83 
84   enum RecursionStateTy {
85     RS_Normal,        // Normal type conversion.
86     RS_Struct,        // Recursively inside a struct conversion.
87     RS_StructPointer  // Recursively inside a pointer in a struct.
88   } RecursionState;
89 
90   /// SkippedLayout - True if we didn't layout a function bit due to a
91   /// RS_StructPointer RecursionState.
92   bool SkippedLayout;
93 
94   llvm::SmallVector<const RecordDecl *, 8> DeferredRecords;
95 
96   struct RecursionStatePointerRAII {
97     RecursionStateTy &Val;
98     RecursionStateTy Saved;
99 
100     RecursionStatePointerRAII(RecursionStateTy &V) : Val(V), Saved(V) {
101       if (Val == RS_Struct)
102         Val = RS_StructPointer;
103     }
104 
105     ~RecursionStatePointerRAII() {
106       Val = Saved;
107     }
108   };
109 
110 private:
111   /// TypeCache - This map keeps cache of llvm::Types
112   /// and maps llvm::Types to corresponding clang::Type.
113   llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
114 
115 public:
116   CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
117                const ABIInfo &Info, CGCXXABI &CXXABI,
118                const CodeGenOptions &Opts);
119   ~CodeGenTypes();
120 
121   const llvm::TargetData &getTargetData() const { return TheTargetData; }
122   const TargetInfo &getTarget() const { return Target; }
123   ASTContext &getContext() const { return Context; }
124   const ABIInfo &getABIInfo() const { return TheABIInfo; }
125   const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
126   CGCXXABI &getCXXABI() const { return TheCXXABI; }
127   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
128 
129   /// ConvertType - Convert type T into a llvm::Type.
130   llvm::Type *ConvertType(QualType T);
131 
132   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
133   /// ConvertType in that it is used to convert to the memory representation for
134   /// a type.  For example, the scalar representation for _Bool is i1, but the
135   /// memory representation is usually i8 or i32, depending on the target.
136   llvm::Type *ConvertTypeForMem(QualType T);
137 
138   /// GetFunctionType - Get the LLVM function type for \arg Info.
139   llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
140                                       bool IsVariadic);
141 
142   llvm::FunctionType *GetFunctionType(GlobalDecl GD);
143 
144   /// isFuncTypeConvertible - Utility to check whether a function type can
145   /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
146   /// type).
147   bool isFuncTypeConvertible(const FunctionType *FT);
148   bool isFuncTypeArgumentConvertible(QualType Ty);
149 
150   /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
151   /// given a CXXMethodDecl. If the method to has an incomplete return type,
152   /// and/or incomplete argument types, this will return the opaque type.
153   const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
154 
155   const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
156 
157   /// UpdateCompletedType - When we find the full definition for a TagDecl,
158   /// replace the 'opaque' type we previously made for it if applicable.
159   void UpdateCompletedType(const TagDecl *TD);
160 
161   /// getNullaryFunctionInfo - Get the function info for a void()
162   /// function with standard CC.
163   const CGFunctionInfo &getNullaryFunctionInfo();
164 
165   /// getFunctionInfo - Get the function info for the specified function decl.
166   const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
167 
168   const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
169   const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
170   const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
171   const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
172                                         CXXCtorType Type);
173   const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
174                                         CXXDtorType Type);
175 
176   const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
177                                         const FunctionType *Ty) {
178     return getFunctionInfo(Ty->getResultType(), Args,
179                            Ty->getExtInfo());
180   }
181 
182   const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
183   const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
184 
185   /// getFunctionInfo - Get the function info for a member function of
186   /// the given type.  This is used for calls through member function
187   /// pointers.
188   const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
189                                         const FunctionProtoType *FTP);
190 
191   /// getFunctionInfo - Get the function info for a function described by a
192   /// return type and argument types. If the calling convention is not
193   /// specified, the "C" calling convention will be used.
194   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
195                                         const CallArgList &Args,
196                                         const FunctionType::ExtInfo &Info);
197   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
198                                         const FunctionArgList &Args,
199                                         const FunctionType::ExtInfo &Info);
200 
201   /// Retrieves the ABI information for the given function signature.
202   ///
203   /// \param ArgTys - must all actually be canonical as params
204   const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
205                                const llvm::SmallVectorImpl<CanQualType> &ArgTys,
206                                         const FunctionType::ExtInfo &Info);
207 
208   /// \brief Compute a new LLVM record layout object for the given record.
209   CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
210                                       llvm::StructType *Ty);
211 
212   /// addRecordTypeName - Compute a name from the given record decl with an
213   /// optional suffix and name the given LLVM type using it.
214   void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
215                          llvm::StringRef suffix);
216 
217 
218 public:  // These are internal details of CGT that shouldn't be used externally.
219   /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
220   llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
221 
222   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
223   /// argument types it would be passed as on the provided vector \arg
224   /// ArgTys. See ABIArgInfo::Expand.
225   void GetExpandedTypes(QualType type,
226                         llvm::SmallVectorImpl<llvm::Type*> &expanded);
227 
228   /// IsZeroInitializable - Return whether a type can be
229   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
230   bool isZeroInitializable(QualType T);
231 
232   /// IsZeroInitializable - Return whether a record type can be
233   /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
234   bool isZeroInitializable(const CXXRecordDecl *RD);
235 };
236 
237 }  // end namespace CodeGen
238 }  // end namespace clang
239 
240 #endif
241