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 "llvm/Module.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include <vector>
21 
22 #include "CGCall.h"
23 
24 namespace llvm {
25   class FunctionType;
26   class Module;
27   class OpaqueType;
28   class PATypeHolder;
29   class TargetData;
30   class Type;
31   class LLVMContext;
32 }
33 
34 namespace clang {
35   class ABIInfo;
36   class ASTContext;
37   class CXXMethodDecl;
38   class FieldDecl;
39   class FunctionProtoType;
40   class ObjCInterfaceDecl;
41   class ObjCIvarDecl;
42   class PointerType;
43   class QualType;
44   class RecordDecl;
45   class TagDecl;
46   class TargetInfo;
47   class Type;
48 
49 namespace CodeGen {
50   class CodeGenTypes;
51 
52   /// CGRecordLayout - This class handles struct and union layout info while
53   /// lowering AST types to LLVM types.
54   class CGRecordLayout {
55     CGRecordLayout(); // DO NOT IMPLEMENT
56 
57     /// LLVMType - The LLVMType corresponding to this record layout.
58     const llvm::Type *LLVMType;
59 
60     /// ContainsMemberPointer - Whether one of the fields in this record layout
61     /// is a member pointer, or a struct that contains a member pointer.
62     bool ContainsMemberPointer;
63 
64   public:
65     CGRecordLayout(const llvm::Type *T, bool ContainsMemberPointer)
66       : LLVMType(T), ContainsMemberPointer(ContainsMemberPointer) { }
67 
68     /// getLLVMType - Return llvm type associated with this record.
69     const llvm::Type *getLLVMType() const {
70       return LLVMType;
71     }
72 
73     bool containsMemberPointer() const {
74       return ContainsMemberPointer;
75     }
76 
77   };
78 
79 /// CodeGenTypes - This class organizes the cross-module state that is used
80 /// while lowering AST types to LLVM types.
81 class CodeGenTypes {
82   ASTContext &Context;
83   TargetInfo &Target;
84   llvm::Module& TheModule;
85   const llvm::TargetData& TheTargetData;
86   mutable const ABIInfo* TheABIInfo;
87 
88   llvm::SmallVector<std::pair<QualType,
89                               llvm::OpaqueType *>, 8>  PointersToResolve;
90 
91   llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
92 
93   llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
94 
95   /// The opaque type map for Objective-C interfaces. All direct
96   /// manipulation is done by the runtime interfaces, which are
97   /// responsible for coercing to the appropriate type; these opaque
98   /// types are never refined.
99   llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
100 
101   /// CGRecordLayouts - This maps llvm struct type with corresponding
102   /// record layout info.
103   /// FIXME : If CGRecordLayout is less than 16 bytes then use
104   /// inline it in the map.
105   llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
106 
107   /// FieldInfo - This maps struct field with corresponding llvm struct type
108   /// field no. This info is populated by record organizer.
109   llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
110 
111   /// FunctionInfos - Hold memoized CGFunctionInfo results.
112   llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
113 
114 public:
115   struct BitFieldInfo {
116     BitFieldInfo(unsigned FieldNo,
117                  unsigned Start,
118                  unsigned Size)
119       : FieldNo(FieldNo), Start(Start), Size(Size) {}
120 
121     unsigned FieldNo;
122     unsigned Start;
123     unsigned Size;
124   };
125 
126 private:
127   llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
128 
129   /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
130   /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
131   /// used instead of llvm::Type because it allows us to bypass potential
132   /// dangling type pointers due to type refinement on llvm side.
133   llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
134 
135   /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
136   /// method directly because it does not do any type caching. This method
137   /// is available only for ConvertType(). CovertType() is preferred
138   /// interface to convert type T into a llvm::Type.
139   const llvm::Type *ConvertNewType(QualType T);
140 public:
141   CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
142   ~CodeGenTypes();
143 
144   const llvm::TargetData &getTargetData() const { return TheTargetData; }
145   TargetInfo &getTarget() const { return Target; }
146   ASTContext &getContext() const { return Context; }
147   const ABIInfo &getABIInfo() const;
148   llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
149 
150   /// ConvertType - Convert type T into a llvm::Type.
151   const llvm::Type *ConvertType(QualType T);
152   const llvm::Type *ConvertTypeRecursive(QualType T);
153 
154   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
155   /// ConvertType in that it is used to convert to the memory representation for
156   /// a type.  For example, the scalar representation for _Bool is i1, but the
157   /// memory representation is usually i8 or i32, depending on the target.
158   const llvm::Type *ConvertTypeForMem(QualType T);
159   const llvm::Type *ConvertTypeForMemRecursive(QualType T);
160 
161   /// GetFunctionType - Get the LLVM function type for \arg Info.
162   const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
163                                             bool IsVariadic);
164 
165   const CGRecordLayout &getCGRecordLayout(const TagDecl*) const;
166 
167   /// getLLVMFieldNo - Return llvm::StructType element number
168   /// that corresponds to the field FD.
169   unsigned getLLVMFieldNo(const FieldDecl *FD);
170 
171   /// UpdateCompletedType - When we find the full definition for a TagDecl,
172   /// replace the 'opaque' type we previously made for it if applicable.
173   void UpdateCompletedType(const TagDecl *TD);
174 
175 private:
176   const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
177   const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
178 
179 public:
180   /// getFunctionInfo - Get the function info for the specified function decl.
181   const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
182   const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
183   const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
184 
185   /// getFunctionInfo - Get the function info for a function described by a
186   /// return type and argument types. If the calling convention is not
187   /// specified, the "C" calling convention will be used.
188   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
189                                         const CallArgList &Args,
190                                         unsigned CallingConvention = 0);
191   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
192                                         const FunctionArgList &Args,
193                                         unsigned CallingConvention = 0);
194   const CGFunctionInfo &getFunctionInfo(QualType RetTy,
195                                   const llvm::SmallVector<QualType, 16> &ArgTys,
196                                         unsigned CallingConvention = 0);
197 
198 public:  // These are internal details of CGT that shouldn't be used externally.
199   /// addFieldInfo - Assign field number to field FD.
200   void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
201 
202   /// addBitFieldInfo - Assign a start bit and a size to field FD.
203   void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
204                        unsigned Start, unsigned Size);
205 
206   /// getBitFieldInfo - Return the BitFieldInfo  that corresponds to the field
207   /// FD.
208   BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
209 
210   /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
211   /// enum.
212   const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
213 
214   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
215   /// argument types it would be passed as on the provided vector \arg
216   /// ArgTys. See ABIArgInfo::Expand.
217   void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
218 };
219 
220 }  // end namespace CodeGen
221 }  // end namespace clang
222 
223 #endif
224