blob: c7f48e6c9dd429778c1223216e30fbf555b1cd01 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenTypes.h - Type translation for LLVM CodeGen -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This is the code that handles AST -> LLVM type lowering.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENTYPES_H
15#define CLANG_CODEGEN_CODEGENTYPES_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
Owen Anderson47a434f2009-08-05 23:18:46 +000017#include "llvm/Module.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000018#include "llvm/ADT/DenseMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include <vector>
20
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +000021#include "CGCall.h"
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +000022#include "GlobalDecl.h"
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +000023
Reid Spencer5f016e22007-07-11 17:01:13 +000024namespace llvm {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000025 class FunctionType;
Anders Carlsson4e533282007-08-17 22:00:32 +000026 class Module;
Chris Lattnerfce71b82008-04-03 05:50:42 +000027 class OpaqueType;
Devang Patel30ec9972007-10-25 18:32:36 +000028 class PATypeHolder;
Devang Patel7a4718e2007-10-31 20:01:01 +000029 class TargetData;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000030 class Type;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000031 class LLVMContext;
Reid Spencer5f016e22007-07-11 17:01:13 +000032}
33
34namespace clang {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000035 class ABIInfo;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000036 class ASTContext;
John McCallead608a2010-02-26 00:48:12 +000037 template <typename> class CanQual;
Anders Carlssonf6c56e22009-11-25 03:15:49 +000038 class CXXConstructorDecl;
39 class CXXDestructorDecl;
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000040 class CXXMethodDecl;
Devang Patelb1e39892007-10-23 23:26:46 +000041 class FieldDecl;
Douglas Gregor72564e72009-02-26 23:50:07 +000042 class FunctionProtoType;
Chris Lattner391d77a2008-03-30 23:03:07 +000043 class ObjCInterfaceDecl;
44 class ObjCIvarDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000045 class PointerType;
46 class QualType;
47 class RecordDecl;
48 class TagDecl;
49 class TargetInfo;
50 class Type;
John McCallead608a2010-02-26 00:48:12 +000051 typedef CanQual<Type> CanQualType;
Devang Patelb84a06e2007-10-23 02:10:49 +000052
Reid Spencer5f016e22007-07-11 17:01:13 +000053namespace CodeGen {
Daniel Dunbar2924ade2010-03-30 22:26:10 +000054 class CGRecordLayout;
Devang Patelb84a06e2007-10-23 02:10:49 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056/// CodeGenTypes - This class organizes the cross-module state that is used
57/// while lowering AST types to LLVM types.
58class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000059 ASTContext &Context;
Daniel Dunbar444be732009-11-13 05:51:54 +000060 const TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000061 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000062 const llvm::TargetData& TheTargetData;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000063 const ABIInfo& TheABIInfo;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000065 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000066 llvm::OpaqueType *>, 8> PointersToResolve;
67
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000068 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000069
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000070 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
71
Daniel Dunbar412f59b2009-04-22 10:28:39 +000072 /// 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;
77
Mike Stump1eb44332009-09-09 15:08:12 +000078 /// CGRecordLayouts - This maps llvm struct type with corresponding
79 /// record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000080 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000081
Daniel Dunbar40a6be62009-02-03 00:07:12 +000082 /// FunctionInfos - Hold memoized CGFunctionInfo results.
83 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
84
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000085private:
Chris Lattner4581fff2008-02-06 05:21:55 +000086 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +000087 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
Mike Stump1eb44332009-09-09 15:08:12 +000088 /// used instead of llvm::Type because it allows us to bypass potential
Devang Patel30ec9972007-10-25 18:32:36 +000089 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +000090 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +000091
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);
Chris Lattnera9fa8582010-07-01 06:20:47 +000097
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();
102
Reid Spencer5f016e22007-07-11 17:01:13 +0000103public:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000104 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
105 const ABIInfo &Info);
Devang Patelb84a06e2007-10-23 02:10:49 +0000106 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Devang Pateld9e9ede2007-10-31 20:08:22 +0000108 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000109 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000110 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000111 const ABIInfo &getABIInfo() const { return TheABIInfo; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000112 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000113
Mike Stump1eb44332009-09-09 15:08:12 +0000114 /// ConvertType - Convert type T into a llvm::Type.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000115 const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000116 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner4581fff2008-02-06 05:21:55 +0000118 /// 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.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000122 const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
123 const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
124 return ConvertTypeForMem(T, true);
125 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000126
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000127 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000128 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000129 bool IsVariadic,
130 bool IsRecursive = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
John McCallc0bf4622010-02-23 00:48:20 +0000132 const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
133
Eli Friedmanc00129a2010-05-30 06:03:20 +0000134 /// 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
136 /// type).
137 static const TagType *VerifyFuncTypeComplete(const Type* T);
Anders Carlssonecf282b2009-11-24 05:08:52 +0000138
Anders Carlsson046c2942010-04-17 20:15:18 +0000139 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000140 /// given a CXXMethodDecl. If the method to has an incomplete return type,
141 /// and/or incomplete argument types, this will return the opaque type.
Anders Carlsson046c2942010-04-17 20:15:18 +0000142 const llvm::Type *GetFunctionTypeForVTable(const CXXMethodDecl *MD);
Anders Carlssonecf282b2009-11-24 05:08:52 +0000143
Daniel Dunbar7c465b92010-03-30 22:26:12 +0000144 const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattnerc5b88062008-02-06 05:08:19 +0000146 /// 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);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000149
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000150 /// getFunctionInfo - Get the function info for the specified function decl.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000151 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
152
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000153 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000154 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000155 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000156 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
157 CXXCtorType Type);
158 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
159 CXXDtorType Type);
160
John McCall04a67a62010-02-05 21:31:56 +0000161 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
162 const FunctionType *Ty) {
163 return getFunctionInfo(Ty->getResultType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000164 Ty->getExtInfo());
John McCall04a67a62010-02-05 21:31:56 +0000165 }
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000166
167 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
168 bool IsRecursive = false);
169 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
170 bool IsRecursive = false);
John McCall04a67a62010-02-05 21:31:56 +0000171
Anders Carlsson375c31c2009-10-03 19:43:08 +0000172 // getFunctionInfo - Get the function info for a member function.
173 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
174 const FunctionProtoType *FTP);
175
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000176 /// 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.
Mike Stump1eb44332009-09-09 15:08:12 +0000179 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000180 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000181 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000182 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000183 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000184 const FunctionType::ExtInfo &Info);
John McCallead608a2010-02-26 00:48:12 +0000185
186 /// Retrieves the ABI information for the given function signature.
187 ///
188 /// \param ArgTys - must all actually be canonical as params
189 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
190 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000191 const FunctionType::ExtInfo &Info,
192 bool IsRecursive = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Daniel Dunbar270e2032010-03-31 00:11:27 +0000194 /// \brief Compute a new LLVM record layout object for the given record.
195 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
196
Chris Lattner19009e62008-01-09 18:47:25 +0000197public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000198 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
199 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000200 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000201
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.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000205 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys,
206 bool IsRecursive);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000207
208 /// ContainsPointerToDataMember - Return whether the given type contains a
209 /// pointer to a data member.
210 bool ContainsPointerToDataMember(QualType T);
Anders Carlssonc39211d2010-05-18 03:47:15 +0000211
212 /// ContainsPointerToDataMember - Return whether the record decl contains a
213 /// pointer to a data member.
214 bool ContainsPointerToDataMember(const CXXRecordDecl *RD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000215};
Chris Lattnera7674d82007-07-13 22:13:22 +0000216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217} // end namespace CodeGen
218} // end namespace clang
219
220#endif