1 //===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that handles AST -> LLVM type lowering.
12 //===----------------------------------------------------------------------===//
14 #ifndef CLANG_CODEGEN_CODEGENTYPES_H
15 #define CLANG_CODEGEN_CODEGENTYPES_H
17 #include "llvm/Module.h"
18 #include "llvm/ADT/DenseMap.h"
22 #include "GlobalDecl.h"
37 template <typename> class CanQual;
38 class CXXConstructorDecl;
39 class CXXDestructorDecl;
42 class FunctionProtoType;
43 class ObjCInterfaceDecl;
51 typedef CanQual<Type> CanQualType;
56 /// CodeGenTypes - This class organizes the cross-module state that is used
57 /// while lowering AST types to LLVM types.
60 const TargetInfo &Target;
61 llvm::Module& TheModule;
62 const llvm::TargetData& TheTargetData;
63 const ABIInfo& TheABIInfo;
65 llvm::SmallVector<std::pair<QualType,
66 llvm::OpaqueType *>, 8> PointersToResolve;
68 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
70 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
72 /// The opaque type map for Objective-C interfaces. All direct
73 /// manipulation is done by the runtime interfaces, which are
74 /// responsible for coercing to the appropriate type; these opaque
75 /// types are never refined.
76 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
78 /// CGRecordLayouts - This maps llvm struct type with corresponding
79 /// record layout info.
80 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
82 /// FunctionInfos - Hold memoized CGFunctionInfo results.
83 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
86 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
87 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
88 /// used instead of llvm::Type because it allows us to bypass potential
89 /// dangling type pointers due to type refinement on llvm side.
90 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
92 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
93 /// method directly because it does not do any type caching. This method
94 /// is available only for ConvertType(). CovertType() is preferred
95 /// interface to convert type T into a llvm::Type.
96 const llvm::Type *ConvertNewType(QualType T);
98 /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
99 /// pointers that are referenced but have not been converted yet. This is
100 /// used to handle cyclic structures properly.
101 void HandleLateResolvedPointers();
104 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
105 const ABIInfo &Info);
108 const llvm::TargetData &getTargetData() const { return TheTargetData; }
109 const TargetInfo &getTarget() const { return Target; }
110 ASTContext &getContext() const { return Context; }
111 const ABIInfo &getABIInfo() const { return TheABIInfo; }
112 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
114 /// ConvertType - Convert type T into a llvm::Type.
115 const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
116 const llvm::Type *ConvertTypeRecursive(QualType T);
118 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
119 /// ConvertType in that it is used to convert to the memory representation for
120 /// a type. For example, the scalar representation for _Bool is i1, but the
121 /// memory representation is usually i8 or i32, depending on the target.
122 const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
123 const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
124 return ConvertTypeForMem(T, true);
127 /// GetFunctionType - Get the LLVM function type for \arg Info.
128 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
130 bool IsRecursive = false);
132 const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
134 /// VerifyFuncTypeComplete - Utility to check whether a function type can
135 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
137 static const TagType *VerifyFuncTypeComplete(const Type* T);
139 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
140 /// given a CXXMethodDecl. If the method to has an incomplete return type,
141 /// and/or incomplete argument types, this will return the opaque type.
142 const llvm::Type *GetFunctionTypeForVTable(const CXXMethodDecl *MD);
144 const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
146 /// UpdateCompletedType - When we find the full definition for a TagDecl,
147 /// replace the 'opaque' type we previously made for it if applicable.
148 void UpdateCompletedType(const TagDecl *TD);
150 /// getFunctionInfo - Get the function info for the specified function decl.
151 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
153 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
154 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
155 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
156 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
158 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
161 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
162 const FunctionType *Ty) {
163 return getFunctionInfo(Ty->getResultType(), Args,
167 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
168 bool IsRecursive = false);
169 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
170 bool IsRecursive = false);
172 // getFunctionInfo - Get the function info for a member function.
173 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
174 const FunctionProtoType *FTP);
176 /// getFunctionInfo - Get the function info for a function described by a
177 /// return type and argument types. If the calling convention is not
178 /// specified, the "C" calling convention will be used.
179 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
180 const CallArgList &Args,
181 const FunctionType::ExtInfo &Info);
182 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
183 const FunctionArgList &Args,
184 const FunctionType::ExtInfo &Info);
186 /// Retrieves the ABI information for the given function signature.
188 /// \param ArgTys - must all actually be canonical as params
189 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
190 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
191 const FunctionType::ExtInfo &Info,
192 bool IsRecursive = false);
194 /// \brief Compute a new LLVM record layout object for the given record.
195 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
197 public: // These are internal details of CGT that shouldn't be used externally.
198 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
200 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
202 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
203 /// argument types it would be passed as on the provided vector \arg
204 /// ArgTys. See ABIArgInfo::Expand.
205 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys,
208 /// ContainsPointerToDataMember - Return whether the given type contains a
209 /// pointer to a data member.
210 bool ContainsPointerToDataMember(QualType T);
212 /// ContainsPointerToDataMember - Return whether the record decl contains a
213 /// pointer to a data member.
214 bool ContainsPointerToDataMember(const CXXRecordDecl *RD);
217 } // end namespace CodeGen
218 } // end namespace clang