CodeGenTypes.h revision 199482
1193326Sed//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10198092Srdivacky// This is the code that handles AST -> LLVM type lowering.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef CLANG_CODEGEN_CODEGENTYPES_H
15193326Sed#define CLANG_CODEGEN_CODEGENTYPES_H
16193326Sed
17198092Srdivacky#include "llvm/Module.h"
18193326Sed#include "llvm/ADT/DenseMap.h"
19193326Sed#include "llvm/ADT/SmallSet.h"
20193326Sed#include <vector>
21193326Sed
22193326Sed#include "CGCall.h"
23193326Sed
24193326Sednamespace llvm {
25193326Sed  class FunctionType;
26193326Sed  class Module;
27193326Sed  class OpaqueType;
28193326Sed  class PATypeHolder;
29193326Sed  class TargetData;
30193326Sed  class Type;
31198092Srdivacky  class LLVMContext;
32193326Sed}
33193326Sed
34193326Sednamespace clang {
35193326Sed  class ABIInfo;
36193326Sed  class ASTContext;
37193326Sed  class CXXMethodDecl;
38193326Sed  class FieldDecl;
39193326Sed  class FunctionProtoType;
40193326Sed  class ObjCInterfaceDecl;
41193326Sed  class ObjCIvarDecl;
42193326Sed  class PointerType;
43193326Sed  class QualType;
44193326Sed  class RecordDecl;
45193326Sed  class TagDecl;
46193326Sed  class TargetInfo;
47193326Sed  class Type;
48193326Sed
49193326Sednamespace CodeGen {
50193326Sed  class CodeGenTypes;
51193326Sed
52198092Srdivacky  /// CGRecordLayout - This class handles struct and union layout info while
53193326Sed  /// lowering AST types to LLVM types.
54193326Sed  class CGRecordLayout {
55193326Sed    CGRecordLayout(); // DO NOT IMPLEMENT
56198092Srdivacky
57198092Srdivacky    /// LLVMType - The LLVMType corresponding to this record layout.
58198092Srdivacky    const llvm::Type *LLVMType;
59198092Srdivacky
60198092Srdivacky    /// ContainsMemberPointer - Whether one of the fields in this record layout
61198092Srdivacky    /// is a member pointer, or a struct that contains a member pointer.
62198092Srdivacky    bool ContainsMemberPointer;
63198092Srdivacky
64198092Srdivacky    /// KeyFunction - The key function of the record layout (if one exists),
65198092Srdivacky    /// which is the first non-pure virtual function that is not inline at the
66198092Srdivacky    /// point of class definition.
67198092Srdivacky    /// See http://www.codesourcery.com/public/cxx-abi/abi.html#vague-vtable.
68198092Srdivacky    const CXXMethodDecl *KeyFunction;
69198092Srdivacky
70193326Sed  public:
71198092Srdivacky    CGRecordLayout(const llvm::Type *T, bool ContainsMemberPointer,
72198092Srdivacky                   const CXXMethodDecl *KeyFunction)
73198092Srdivacky      : LLVMType(T), ContainsMemberPointer(ContainsMemberPointer),
74198092Srdivacky        KeyFunction(KeyFunction) { }
75193326Sed
76193326Sed    /// getLLVMType - Return llvm type associated with this record.
77198092Srdivacky    const llvm::Type *getLLVMType() const {
78198092Srdivacky      return LLVMType;
79193326Sed    }
80193326Sed
81198092Srdivacky    bool containsMemberPointer() const {
82198092Srdivacky      return ContainsMemberPointer;
83193326Sed    }
84193326Sed
85198092Srdivacky    const CXXMethodDecl *getKeyFunction() const {
86198092Srdivacky      return KeyFunction;
87193326Sed    }
88198092Srdivacky  };
89193326Sed
90193326Sed/// CodeGenTypes - This class organizes the cross-module state that is used
91193326Sed/// while lowering AST types to LLVM types.
92193326Sedclass CodeGenTypes {
93193326Sed  ASTContext &Context;
94199482Srdivacky  const TargetInfo &Target;
95193326Sed  llvm::Module& TheModule;
96193326Sed  const llvm::TargetData& TheTargetData;
97193326Sed  mutable const ABIInfo* TheABIInfo;
98198092Srdivacky
99193326Sed  llvm::SmallVector<std::pair<QualType,
100193326Sed                              llvm::OpaqueType *>, 8>  PointersToResolve;
101193326Sed
102193326Sed  llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
103193326Sed
104193326Sed  llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
105193326Sed
106193326Sed  /// The opaque type map for Objective-C interfaces. All direct
107193326Sed  /// manipulation is done by the runtime interfaces, which are
108193326Sed  /// responsible for coercing to the appropriate type; these opaque
109193326Sed  /// types are never refined.
110193326Sed  llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
111193326Sed
112198092Srdivacky  /// CGRecordLayouts - This maps llvm struct type with corresponding
113198092Srdivacky  /// record layout info.
114198092Srdivacky  /// FIXME : If CGRecordLayout is less than 16 bytes then use
115193326Sed  /// inline it in the map.
116193326Sed  llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
117193326Sed
118193326Sed  /// FieldInfo - This maps struct field with corresponding llvm struct type
119193326Sed  /// field no. This info is populated by record organizer.
120193326Sed  llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
121193326Sed
122193326Sed  /// FunctionInfos - Hold memoized CGFunctionInfo results.
123193326Sed  llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
124193326Sed
125193326Sedpublic:
126198092Srdivacky  struct BitFieldInfo {
127198092Srdivacky    BitFieldInfo(unsigned FieldNo,
128198092Srdivacky                 unsigned Start,
129198092Srdivacky                 unsigned Size)
130198092Srdivacky      : FieldNo(FieldNo), Start(Start), Size(Size) {}
131193326Sed
132198092Srdivacky    unsigned FieldNo;
133198092Srdivacky    unsigned Start;
134198092Srdivacky    unsigned Size;
135193326Sed  };
136193326Sed
137193326Sedprivate:
138193326Sed  llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
139193326Sed
140193326Sed  /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
141193326Sed  /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
142198092Srdivacky  /// used instead of llvm::Type because it allows us to bypass potential
143193326Sed  /// dangling type pointers due to type refinement on llvm side.
144193326Sed  llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
145193326Sed
146193326Sed  /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
147193326Sed  /// method directly because it does not do any type caching. This method
148193326Sed  /// is available only for ConvertType(). CovertType() is preferred
149193326Sed  /// interface to convert type T into a llvm::Type.
150193326Sed  const llvm::Type *ConvertNewType(QualType T);
151193326Sedpublic:
152193326Sed  CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
153193326Sed  ~CodeGenTypes();
154198092Srdivacky
155193326Sed  const llvm::TargetData &getTargetData() const { return TheTargetData; }
156199482Srdivacky  const TargetInfo &getTarget() const { return Target; }
157193326Sed  ASTContext &getContext() const { return Context; }
158193326Sed  const ABIInfo &getABIInfo() const;
159198092Srdivacky  llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
160193326Sed
161198092Srdivacky  /// ConvertType - Convert type T into a llvm::Type.
162193326Sed  const llvm::Type *ConvertType(QualType T);
163193326Sed  const llvm::Type *ConvertTypeRecursive(QualType T);
164198092Srdivacky
165193326Sed  /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
166193326Sed  /// ConvertType in that it is used to convert to the memory representation for
167193326Sed  /// a type.  For example, the scalar representation for _Bool is i1, but the
168193326Sed  /// memory representation is usually i8 or i32, depending on the target.
169193326Sed  const llvm::Type *ConvertTypeForMem(QualType T);
170193326Sed  const llvm::Type *ConvertTypeForMemRecursive(QualType T);
171193326Sed
172193326Sed  /// GetFunctionType - Get the LLVM function type for \arg Info.
173193326Sed  const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
174193326Sed                                            bool IsVariadic);
175198092Srdivacky
176198092Srdivacky  const CGRecordLayout &getCGRecordLayout(const TagDecl*) const;
177198092Srdivacky
178193326Sed  /// getLLVMFieldNo - Return llvm::StructType element number
179193326Sed  /// that corresponds to the field FD.
180193326Sed  unsigned getLLVMFieldNo(const FieldDecl *FD);
181198092Srdivacky
182193326Sed  /// UpdateCompletedType - When we find the full definition for a TagDecl,
183193326Sed  /// replace the 'opaque' type we previously made for it if applicable.
184193326Sed  void UpdateCompletedType(const TagDecl *TD);
185193326Sed
186198092Srdivackyprivate:
187193326Sed  const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
188193326Sed  const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
189198092Srdivacky
190198092Srdivackypublic:
191198092Srdivacky  /// getFunctionInfo - Get the function info for the specified function decl.
192193326Sed  const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
193193326Sed  const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
194193326Sed  const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
195193326Sed
196198092Srdivacky  // getFunctionInfo - Get the function info for a member function.
197198092Srdivacky  const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
198198092Srdivacky                                        const FunctionProtoType *FTP);
199198092Srdivacky
200198092Srdivacky  /// getFunctionInfo - Get the function info for a function described by a
201198092Srdivacky  /// return type and argument types. If the calling convention is not
202198092Srdivacky  /// specified, the "C" calling convention will be used.
203198092Srdivacky  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
204198092Srdivacky                                        const CallArgList &Args,
205198092Srdivacky                                        unsigned CallingConvention = 0);
206198092Srdivacky  const CGFunctionInfo &getFunctionInfo(QualType ResTy,
207198092Srdivacky                                        const FunctionArgList &Args,
208198092Srdivacky                                        unsigned CallingConvention = 0);
209198092Srdivacky  const CGFunctionInfo &getFunctionInfo(QualType RetTy,
210198092Srdivacky                                  const llvm::SmallVector<QualType, 16> &ArgTys,
211198092Srdivacky                                        unsigned CallingConvention = 0);
212198092Srdivacky
213193326Sedpublic:  // These are internal details of CGT that shouldn't be used externally.
214193326Sed  /// addFieldInfo - Assign field number to field FD.
215198092Srdivacky  void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
216193326Sed
217193326Sed  /// addBitFieldInfo - Assign a start bit and a size to field FD.
218198092Srdivacky  void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
219198092Srdivacky                       unsigned Start, unsigned Size);
220193326Sed
221193326Sed  /// getBitFieldInfo - Return the BitFieldInfo  that corresponds to the field
222193326Sed  /// FD.
223193326Sed  BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
224193326Sed
225193326Sed  /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
226193326Sed  /// enum.
227193326Sed  const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
228193326Sed
229193326Sed  /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
230193326Sed  /// argument types it would be passed as on the provided vector \arg
231193326Sed  /// ArgTys. See ABIArgInfo::Expand.
232193326Sed  void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
233193326Sed};
234193326Sed
235193326Sed}  // end namespace CodeGen
236193326Sed}  // end namespace clang
237193326Sed
238193326Sed#endif
239