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