blob: ff1eb4c45b598fb113e9d46315a9fb9a1c9b566f [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
Chris Lattner800588f2010-07-29 06:26:06 +000017#include "CGCall.h"
18#include "GlobalDecl.h"
Owen Anderson47a434f2009-08-05 23:18:46 +000019#include "llvm/Module.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000020#include "llvm/ADT/DenseMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include <vector>
22
23namespace llvm {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000024 class FunctionType;
Anders Carlsson4e533282007-08-17 22:00:32 +000025 class Module;
Chris Lattnerfce71b82008-04-03 05:50:42 +000026 class OpaqueType;
Devang Patel30ec9972007-10-25 18:32:36 +000027 class PATypeHolder;
Devang Patel7a4718e2007-10-31 20:01:01 +000028 class TargetData;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000029 class Type;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000030 class LLVMContext;
Reid Spencer5f016e22007-07-11 17:01:13 +000031}
32
33namespace clang {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000034 class ABIInfo;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000035 class ASTContext;
John McCallead608a2010-02-26 00:48:12 +000036 template <typename> class CanQual;
Anders Carlssonf6c56e22009-11-25 03:15:49 +000037 class CXXConstructorDecl;
38 class CXXDestructorDecl;
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000039 class CXXMethodDecl;
Devang Patelb1e39892007-10-23 23:26:46 +000040 class FieldDecl;
Douglas Gregor72564e72009-02-26 23:50:07 +000041 class FunctionProtoType;
Chris Lattner391d77a2008-03-30 23:03:07 +000042 class ObjCInterfaceDecl;
43 class ObjCIvarDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000044 class PointerType;
45 class QualType;
46 class RecordDecl;
47 class TagDecl;
48 class TargetInfo;
49 class Type;
John McCallead608a2010-02-26 00:48:12 +000050 typedef CanQual<Type> CanQualType;
Devang Patelb84a06e2007-10-23 02:10:49 +000051
Reid Spencer5f016e22007-07-11 17:01:13 +000052namespace CodeGen {
John McCallf16aa102010-08-22 21:01:12 +000053 class CGCXXABI;
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;
John McCallf16aa102010-08-22 21:01:12 +000064 CGCXXABI &TheCXXABI;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000066 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000067 llvm::OpaqueType *>, 8> PointersToResolve;
68
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000069 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000070
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000071 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
72
Daniel Dunbar412f59b2009-04-22 10:28:39 +000073 /// The opaque type map for Objective-C interfaces. All direct
74 /// manipulation is done by the runtime interfaces, which are
75 /// responsible for coercing to the appropriate type; these opaque
76 /// types are never refined.
77 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
78
Mike Stump1eb44332009-09-09 15:08:12 +000079 /// CGRecordLayouts - This maps llvm struct type with corresponding
80 /// record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000081 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000082
Daniel Dunbar40a6be62009-02-03 00:07:12 +000083 /// FunctionInfos - Hold memoized CGFunctionInfo results.
84 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
85
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000086private:
Chris Lattner4581fff2008-02-06 05:21:55 +000087 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +000088 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
Mike Stump1eb44332009-09-09 15:08:12 +000089 /// used instead of llvm::Type because it allows us to bypass potential
Devang Patel30ec9972007-10-25 18:32:36 +000090 /// dangling type pointers due to type refinement on llvm side.
John McCallf4c73712011-01-19 06:33:43 +000091 llvm::DenseMap<const Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +000092
93 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
94 /// method directly because it does not do any type caching. This method
95 /// is available only for ConvertType(). CovertType() is preferred
96 /// interface to convert type T into a llvm::Type.
97 const llvm::Type *ConvertNewType(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +000098
Chris Lattnera9fa8582010-07-01 06:20:47 +000099 /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
100 /// pointers that are referenced but have not been converted yet. This is
101 /// used to handle cyclic structures properly.
102 void HandleLateResolvedPointers();
103
Anders Carlssone0047b12011-04-20 23:51:43 +0000104 /// addRecordTypeName - Compute a name from the given record decl with an
105 /// optional suffix and name the given LLVM type using it.
106 void addRecordTypeName(const RecordDecl *RD, const llvm::Type *Ty,
107 llvm::StringRef suffix);
Anders Carlssone9742b02011-04-17 21:36:59 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109public:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000110 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
John McCallf16aa102010-08-22 21:01:12 +0000111 const ABIInfo &Info, CGCXXABI &CXXABI);
Devang Patelb84a06e2007-10-23 02:10:49 +0000112 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Devang Pateld9e9ede2007-10-31 20:08:22 +0000114 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000115 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000116 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000117 const ABIInfo &getABIInfo() const { return TheABIInfo; }
John McCallf16aa102010-08-22 21:01:12 +0000118 CGCXXABI &getCXXABI() const { return TheCXXABI; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000119 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000120
Mike Stump1eb44332009-09-09 15:08:12 +0000121 /// ConvertType - Convert type T into a llvm::Type.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000122 const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000123 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner4581fff2008-02-06 05:21:55 +0000125 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
126 /// ConvertType in that it is used to convert to the memory representation for
127 /// a type. For example, the scalar representation for _Bool is i1, but the
128 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000129 const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
130 const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
131 return ConvertTypeForMem(T, true);
132 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000133
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000134 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000135 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000136 bool IsVariadic,
137 bool IsRecursive = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000138
John McCallc0bf4622010-02-23 00:48:20 +0000139 const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
140
Eli Friedmanc00129a2010-05-30 06:03:20 +0000141 /// VerifyFuncTypeComplete - Utility to check whether a function type can
142 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
143 /// type).
144 static const TagType *VerifyFuncTypeComplete(const Type* T);
Anders Carlssonecf282b2009-11-24 05:08:52 +0000145
Anders Carlsson046c2942010-04-17 20:15:18 +0000146 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000147 /// given a CXXMethodDecl. If the method to has an incomplete return type,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000148 /// and/or incomplete argument types, this will return the opaque type.
John McCall4c40d982010-08-31 07:33:07 +0000149 const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000150
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000151 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anders Carlsson6aed2a12011-04-17 21:40:34 +0000153 /// addBaseSubobjectTypeName - Add a type name for the base subobject of the
154 /// given record layout.
155 void addBaseSubobjectTypeName(const CXXRecordDecl *RD,
156 const CGRecordLayout &layout);
157
Chris Lattnerc5b88062008-02-06 05:08:19 +0000158 /// UpdateCompletedType - When we find the full definition for a TagDecl,
159 /// replace the 'opaque' type we previously made for it if applicable.
160 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000161
John McCalld26bc762011-03-09 04:27:21 +0000162 /// getNullaryFunctionInfo - Get the function info for a void()
163 /// function with standard CC.
164 const CGFunctionInfo &getNullaryFunctionInfo();
165
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000166 /// getFunctionInfo - Get the function info for the specified function decl.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000167 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000168
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000169 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000170 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000171 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000172 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
173 CXXCtorType Type);
174 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
175 CXXDtorType Type);
176
John McCall04a67a62010-02-05 21:31:56 +0000177 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000178 const FunctionType *Ty) {
179 return getFunctionInfo(Ty->getResultType(), Args,
180 Ty->getExtInfo());
John McCall04a67a62010-02-05 21:31:56 +0000181 }
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000182
183 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
184 bool IsRecursive = false);
185 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
186 bool IsRecursive = false);
John McCall04a67a62010-02-05 21:31:56 +0000187
John McCall4c40d982010-08-31 07:33:07 +0000188 /// getFunctionInfo - Get the function info for a member function of
189 /// the given type. This is used for calls through member function
190 /// pointers.
Anders Carlsson375c31c2009-10-03 19:43:08 +0000191 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
192 const FunctionProtoType *FTP);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000193
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000194 /// getFunctionInfo - Get the function info for a function described by a
195 /// return type and argument types. If the calling convention is not
196 /// specified, the "C" calling convention will be used.
Mike Stump1eb44332009-09-09 15:08:12 +0000197 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000198 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000199 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000200 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000201 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000202 const FunctionType::ExtInfo &Info);
John McCallead608a2010-02-26 00:48:12 +0000203
204 /// Retrieves the ABI information for the given function signature.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000205 ///
John McCallead608a2010-02-26 00:48:12 +0000206 /// \param ArgTys - must all actually be canonical as params
207 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
208 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000209 const FunctionType::ExtInfo &Info,
210 bool IsRecursive = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Daniel Dunbar270e2032010-03-31 00:11:27 +0000212 /// \brief Compute a new LLVM record layout object for the given record.
213 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
214
Chris Lattner19009e62008-01-09 18:47:25 +0000215public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000216 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
217 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000218 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000219
220 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
221 /// argument types it would be passed as on the provided vector \arg
222 /// ArgTys. See ABIArgInfo::Expand.
John McCall42e06112011-05-15 02:19:42 +0000223 void GetExpandedTypes(QualType type,
224 llvm::SmallVectorImpl<const llvm::Type*> &expanded,
225 bool isRecursive);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000226
John McCallf16aa102010-08-22 21:01:12 +0000227 /// IsZeroInitializable - Return whether a type can be
228 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
229 bool isZeroInitializable(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000230
John McCallf16aa102010-08-22 21:01:12 +0000231 /// IsZeroInitializable - Return whether a record type can be
232 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
233 bool isZeroInitializable(const CXXRecordDecl *RD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234};
Chris Lattnera7674d82007-07-13 22:13:22 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236} // end namespace CodeGen
237} // end namespace clang
238
239#endif