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/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include <vector>
20 
21 #include "CGCall.h"
22 
23 namespace llvm {
24   class FunctionType;
25   class Module;
26   class OpaqueType;
27   class PATypeHolder;
28   class TargetData;
29   class Type;
30 }
31 
32 namespace clang {
33   class ABIInfo;
34   class ASTContext;
35   class CXXMethodDecl;
36   class FieldDecl;
37   class FunctionProtoType;
38   class ObjCInterfaceDecl;
39   class ObjCIvarDecl;
40   class PointerType;
41   class QualType;
42   class RecordDecl;
43   class TagDecl;
44   class TargetInfo;
45   class Type;
46 
47 namespace CodeGen {
48   class CodeGenTypes;
49 
50   /// CGRecordLayout - This class handles struct and union layout info while
51   /// lowering AST types to LLVM types.
52   class CGRecordLayout {
53     CGRecordLayout(); // DO NOT IMPLEMENT
54   public:
55     CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF)
56       : STy(T), PaddingFields(PF) {
57       // FIXME : Collect info about fields that requires adjustments
58       // (i.e. fields that do not directly map to llvm struct fields.)
59     }
60 
61     /// getLLVMType - Return llvm type associated with this record.
62     llvm::Type *getLLVMType() const {
63       return STy;
64     }
65 
66     bool isPaddingField(unsigned No) const {
67       return PaddingFields.count(No) != 0;
68     }
69 
70     unsigned getNumPaddingFields() {
71       return PaddingFields.size();
72     }
73 
74   private:
75     llvm::Type *STy;
76     llvm::SmallSet<unsigned, 8> PaddingFields;
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   class BitFieldInfo {
116   public:
117     explicit BitFieldInfo(unsigned short B, unsigned short S)
118       : Begin(B), Size(S) {}
119 
120     unsigned short Begin;
121     unsigned short Size;
122   };
123 
124 private:
125   llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
126 
127   /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
128   /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
129   /// used instead of llvm::Type because it allows us to bypass potential
130   /// dangling type pointers due to type refinement on llvm side.
131   llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
132 
133   /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
134   /// method directly because it does not do any type caching. This method
135   /// is available only for ConvertType(). CovertType() is preferred
136   /// interface to convert type T into a llvm::Type.
137   const llvm::Type *ConvertNewType(QualType T);
138 public:
139   CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
140   ~CodeGenTypes();
141 
142   const llvm::TargetData &getTargetData() const { return TheTargetData; }
143   TargetInfo &getTarget() const { return Target; }
144   ASTContext &getContext() const { return Context; }
145   const ABIInfo &getABIInfo() const;
146 
147   /// ConvertType - Convert type T into a llvm::Type.
148   const llvm::Type *ConvertType(QualType T);
149   const llvm::Type *ConvertTypeRecursive(QualType T);
150 
151   /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
152   /// ConvertType in that it is used to convert to the memory representation for
153   /// a type.  For example, the scalar representation for _Bool is i1, but the
154   /// memory representation is usually i8 or i32, depending on the target.
155   const llvm::Type *ConvertTypeForMem(QualType T);
156   const llvm::Type *ConvertTypeForMemRecursive(QualType T);
157 
158   /// GetFunctionType - Get the LLVM function type for \arg Info.
159   const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
160                                             bool IsVariadic);
161 
162   const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
163 
164   /// getLLVMFieldNo - Return llvm::StructType element number
165   /// that corresponds to the field FD.
166   unsigned getLLVMFieldNo(const FieldDecl *FD);
167 
168   /// UpdateCompletedType - When we find the full definition for a TagDecl,
169   /// replace the 'opaque' type we previously made for it if applicable.
170   void UpdateCompletedType(const TagDecl *TD);
171 
172   /// getFunctionInfo - Get the CGFunctionInfo for this function signature.
173   const CGFunctionInfo &getFunctionInfo(QualType RetTy,
174                                         const llvm::SmallVector<QualType,16>
175                                         &ArgTys);
176 
177   const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
178   const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
179   const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
180   const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
181   const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
182   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
183                                         const CallArgList &Args);
184 public:
185   const CGFunctionInfo &getFunctionInfo(QualType ResTy,
186                                         const FunctionArgList &Args);
187 
188 public:  // These are internal details of CGT that shouldn't be used externally.
189   /// addFieldInfo - Assign field number to field FD.
190   void addFieldInfo(const FieldDecl *FD, unsigned No);
191 
192   /// addBitFieldInfo - Assign a start bit and a size to field FD.
193   void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
194 
195   /// getBitFieldInfo - Return the BitFieldInfo  that corresponds to the field
196   /// FD.
197   BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
198 
199   /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
200   /// enum.
201   const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
202 
203   /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
204   /// argument types it would be passed as on the provided vector \arg
205   /// ArgTys. See ABIArgInfo::Expand.
206   void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
207 };
208 
209 }  // end namespace CodeGen
210 }  // end namespace clang
211 
212 #endif
213