blob: 4a84dff620409ecbcda59d01468487e97777210b [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"
Peter Collingbournefd05ca02011-06-14 04:02:39 +000018#include "clang/AST/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;
Daniel Dunbar08d47922011-06-21 18:54:39 +000040 class CodeGenOptions;
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 {
John McCallf16aa102010-08-22 21:01:12 +000054 class CGCXXABI;
Daniel Dunbar2924ade2010-03-30 22:26:10 +000055 class CGRecordLayout;
Devang Patelb84a06e2007-10-23 02:10:49 +000056
Reid Spencer5f016e22007-07-11 17:01:13 +000057/// CodeGenTypes - This class organizes the cross-module state that is used
58/// while lowering AST types to LLVM types.
59class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000060 ASTContext &Context;
Daniel Dunbar444be732009-11-13 05:51:54 +000061 const TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000062 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000063 const llvm::TargetData& TheTargetData;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000064 const ABIInfo& TheABIInfo;
John McCallf16aa102010-08-22 21:01:12 +000065 CGCXXABI &TheCXXABI;
Daniel Dunbar08d47922011-06-21 18:54:39 +000066 const CodeGenOptions &CodeGenOpts;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000068 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000069 llvm::OpaqueType *>, 8> PointersToResolve;
70
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000071 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000072
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000073 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
74
Daniel Dunbar412f59b2009-04-22 10:28:39 +000075 /// The opaque type map for Objective-C interfaces. All direct
76 /// manipulation is done by the runtime interfaces, which are
77 /// responsible for coercing to the appropriate type; these opaque
78 /// types are never refined.
79 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
80
Mike Stump1eb44332009-09-09 15:08:12 +000081 /// CGRecordLayouts - This maps llvm struct type with corresponding
82 /// record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000083 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000084
Daniel Dunbar40a6be62009-02-03 00:07:12 +000085 /// FunctionInfos - Hold memoized CGFunctionInfo results.
86 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
87
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000088private:
Chris Lattner4581fff2008-02-06 05:21:55 +000089 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +000090 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
Mike Stump1eb44332009-09-09 15:08:12 +000091 /// used instead of llvm::Type because it allows us to bypass potential
Devang Patel30ec9972007-10-25 18:32:36 +000092 /// dangling type pointers due to type refinement on llvm side.
John McCallf4c73712011-01-19 06:33:43 +000093 llvm::DenseMap<const Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +000094
95 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
96 /// method directly because it does not do any type caching. This method
97 /// is available only for ConvertType(). CovertType() is preferred
98 /// interface to convert type T into a llvm::Type.
99 const llvm::Type *ConvertNewType(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000100
Chris Lattnera9fa8582010-07-01 06:20:47 +0000101 /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
102 /// pointers that are referenced but have not been converted yet. This is
103 /// used to handle cyclic structures properly.
104 void HandleLateResolvedPointers();
105
Anders Carlssone0047b12011-04-20 23:51:43 +0000106 /// addRecordTypeName - Compute a name from the given record decl with an
107 /// optional suffix and name the given LLVM type using it.
108 void addRecordTypeName(const RecordDecl *RD, const llvm::Type *Ty,
109 llvm::StringRef suffix);
Anders Carlssone9742b02011-04-17 21:36:59 +0000110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111public:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000112 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
Daniel Dunbar08d47922011-06-21 18:54:39 +0000113 const ABIInfo &Info, CGCXXABI &CXXABI,
114 const CodeGenOptions &Opts);
Devang Patelb84a06e2007-10-23 02:10:49 +0000115 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Devang Pateld9e9ede2007-10-31 20:08:22 +0000117 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000118 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000119 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000120 const ABIInfo &getABIInfo() const { return TheABIInfo; }
Daniel Dunbar08d47922011-06-21 18:54:39 +0000121 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
John McCallf16aa102010-08-22 21:01:12 +0000122 CGCXXABI &getCXXABI() const { return TheCXXABI; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000123 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000124
Mike Stump1eb44332009-09-09 15:08:12 +0000125 /// ConvertType - Convert type T into a llvm::Type.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000126 const llvm::Type *ConvertType(QualType T, bool IsRecursive = false);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000127 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Chris Lattner4581fff2008-02-06 05:21:55 +0000129 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
130 /// ConvertType in that it is used to convert to the memory representation for
131 /// a type. For example, the scalar representation for _Bool is i1, but the
132 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000133 const llvm::Type *ConvertTypeForMem(QualType T, bool IsRecursive = false);
134 const llvm::Type *ConvertTypeForMemRecursive(QualType T) {
135 return ConvertTypeForMem(T, true);
136 }
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000137
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000138 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000139 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000140 bool IsVariadic,
141 bool IsRecursive = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
John McCallc0bf4622010-02-23 00:48:20 +0000143 const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
144
Eli Friedmanc00129a2010-05-30 06:03:20 +0000145 /// VerifyFuncTypeComplete - Utility to check whether a function type can
146 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
147 /// type).
148 static const TagType *VerifyFuncTypeComplete(const Type* T);
Anders Carlssonecf282b2009-11-24 05:08:52 +0000149
Anders Carlsson046c2942010-04-17 20:15:18 +0000150 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000151 /// given a CXXMethodDecl. If the method to has an incomplete return type,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000152 /// and/or incomplete argument types, this will return the opaque type.
John McCall4c40d982010-08-31 07:33:07 +0000153 const llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000154
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000155 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlsson6aed2a12011-04-17 21:40:34 +0000157 /// addBaseSubobjectTypeName - Add a type name for the base subobject of the
158 /// given record layout.
159 void addBaseSubobjectTypeName(const CXXRecordDecl *RD,
160 const CGRecordLayout &layout);
161
Chris Lattnerc5b88062008-02-06 05:08:19 +0000162 /// UpdateCompletedType - When we find the full definition for a TagDecl,
163 /// replace the 'opaque' type we previously made for it if applicable.
164 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000165
John McCalld26bc762011-03-09 04:27:21 +0000166 /// getNullaryFunctionInfo - Get the function info for a void()
167 /// function with standard CC.
168 const CGFunctionInfo &getNullaryFunctionInfo();
169
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000170 /// getFunctionInfo - Get the function info for the specified function decl.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000171 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000172
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000173 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000174 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000175 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000176 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
177 CXXCtorType Type);
178 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
179 CXXDtorType Type);
180
John McCall04a67a62010-02-05 21:31:56 +0000181 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000182 const FunctionType *Ty) {
183 return getFunctionInfo(Ty->getResultType(), Args,
184 Ty->getExtInfo());
John McCall04a67a62010-02-05 21:31:56 +0000185 }
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000186
187 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty,
188 bool IsRecursive = false);
189 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty,
190 bool IsRecursive = false);
John McCall04a67a62010-02-05 21:31:56 +0000191
John McCall4c40d982010-08-31 07:33:07 +0000192 /// getFunctionInfo - Get the function info for a member function of
193 /// the given type. This is used for calls through member function
194 /// pointers.
Anders Carlsson375c31c2009-10-03 19:43:08 +0000195 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
196 const FunctionProtoType *FTP);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000197
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000198 /// getFunctionInfo - Get the function info for a function described by a
199 /// return type and argument types. If the calling convention is not
200 /// specified, the "C" calling convention will be used.
Mike Stump1eb44332009-09-09 15:08:12 +0000201 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000202 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000203 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000204 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000205 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000206 const FunctionType::ExtInfo &Info);
John McCallead608a2010-02-26 00:48:12 +0000207
208 /// Retrieves the ABI information for the given function signature.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000209 ///
John McCallead608a2010-02-26 00:48:12 +0000210 /// \param ArgTys - must all actually be canonical as params
211 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
212 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000213 const FunctionType::ExtInfo &Info,
214 bool IsRecursive = false);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Daniel Dunbar270e2032010-03-31 00:11:27 +0000216 /// \brief Compute a new LLVM record layout object for the given record.
217 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
218
Chris Lattner19009e62008-01-09 18:47:25 +0000219public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000220 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
221 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000222 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000223
224 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
225 /// argument types it would be passed as on the provided vector \arg
226 /// ArgTys. See ABIArgInfo::Expand.
John McCall42e06112011-05-15 02:19:42 +0000227 void GetExpandedTypes(QualType type,
228 llvm::SmallVectorImpl<const llvm::Type*> &expanded,
229 bool isRecursive);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000230
John McCallf16aa102010-08-22 21:01:12 +0000231 /// IsZeroInitializable - Return whether a type can be
232 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
233 bool isZeroInitializable(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000234
John McCallf16aa102010-08-22 21:01:12 +0000235 /// IsZeroInitializable - Return whether a record type can be
236 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
237 bool isZeroInitializable(const CXXRecordDecl *RD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238};
Chris Lattnera7674d82007-07-13 22:13:22 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240} // end namespace CodeGen
241} // end namespace clang
242
243#endif