blob: 7682f2af7dc6a66930c5de0773fda38a691dd54c [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;
Devang Patel7a4718e2007-10-31 20:01:01 +000026 class TargetData;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000027 class Type;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000028 class LLVMContext;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000029 class StructType;
Reid Spencer5f016e22007-07-11 17:01:13 +000030}
31
32namespace clang {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000033 class ABIInfo;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000034 class ASTContext;
John McCallead608a2010-02-26 00:48:12 +000035 template <typename> class CanQual;
Anders Carlssonf6c56e22009-11-25 03:15:49 +000036 class CXXConstructorDecl;
37 class CXXDestructorDecl;
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000038 class CXXMethodDecl;
Daniel Dunbar08d47922011-06-21 18:54:39 +000039 class CodeGenOptions;
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;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000061 llvm::Module &TheModule;
62 const llvm::TargetData &TheTargetData;
63 const ABIInfo &TheABIInfo;
John McCallf16aa102010-08-22 21:01:12 +000064 CGCXXABI &TheCXXABI;
Daniel Dunbar08d47922011-06-21 18:54:39 +000065 const CodeGenOptions &CodeGenOpts;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Daniel Dunbar412f59b2009-04-22 10:28:39 +000067 /// The opaque type map for Objective-C interfaces. All direct
68 /// manipulation is done by the runtime interfaces, which are
69 /// responsible for coercing to the appropriate type; these opaque
70 /// types are never refined.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000071 llvm::DenseMap<const ObjCInterfaceType*, llvm::Type *> InterfaceTypes;
Daniel Dunbar412f59b2009-04-22 10:28:39 +000072
Mike Stump1eb44332009-09-09 15:08:12 +000073 /// CGRecordLayouts - This maps llvm struct type with corresponding
74 /// record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000075 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000076
Chris Lattner9cbe4f02011-07-09 17:41:47 +000077 /// RecordDeclTypes - This contains the LLVM IR type for any converted
78 /// RecordDecl.
79 llvm::DenseMap<const Type*, llvm::StructType *> RecordDeclTypes;
80
Daniel Dunbar40a6be62009-02-03 00:07:12 +000081 /// FunctionInfos - Hold memoized CGFunctionInfo results.
82 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
83
Chris Lattner71305cc2011-07-15 05:16:14 +000084 /// RecordsBeingLaidOut - This set keeps track of records that we're currently
85 /// converting to an IR type. For example, when converting:
86 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
87 /// types will be in this set.
88 llvm::SmallPtrSet<const Type*, 4> RecordsBeingLaidOut;
89
90 llvm::SmallPtrSet<const CGFunctionInfo*, 4> FunctionsBeingProcessed;
91
92 /// SkippedLayout - True if we didn't layout a function due to a being inside
93 /// a recursive struct conversion, set this to true.
Chris Lattner57eb23f2011-07-10 05:39:13 +000094 bool SkippedLayout;
95
Chris Lattner9cbe4f02011-07-09 17:41:47 +000096 llvm::SmallVector<const RecordDecl *, 8> DeferredRecords;
97
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000098private:
Chris Lattner9cbe4f02011-07-09 17:41:47 +000099 /// TypeCache - This map keeps cache of llvm::Types
100 /// and maps llvm::Types to corresponding clang::Type.
101 llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
Anders Carlssone9742b02011-04-17 21:36:59 +0000102
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,
Daniel Dunbar08d47922011-06-21 18:54:39 +0000105 const ABIInfo &Info, CGCXXABI &CXXABI,
106 const CodeGenOptions &Opts);
Devang Patelb84a06e2007-10-23 02:10:49 +0000107 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Devang Pateld9e9ede2007-10-31 20:08:22 +0000109 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000110 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000111 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000112 const ABIInfo &getABIInfo() const { return TheABIInfo; }
Daniel Dunbar08d47922011-06-21 18:54:39 +0000113 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
John McCallf16aa102010-08-22 21:01:12 +0000114 CGCXXABI &getCXXABI() const { return TheCXXABI; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000115 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000116
Mike Stump1eb44332009-09-09 15:08:12 +0000117 /// ConvertType - Convert type T into a llvm::Type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000118 llvm::Type *ConvertType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner4581fff2008-02-06 05:21:55 +0000120 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
121 /// ConvertType in that it is used to convert to the memory representation for
122 /// a type. For example, the scalar representation for _Bool is i1, but the
123 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000124 llvm::Type *ConvertTypeForMem(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000125
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000126 /// GetFunctionType - Get the LLVM function type for \arg Info.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000127 llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
128 bool IsVariadic);
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000130 llvm::FunctionType *GetFunctionType(GlobalDecl GD);
John McCallc0bf4622010-02-23 00:48:20 +0000131
Chris Lattnerf742eb02011-07-10 00:18:59 +0000132 /// isFuncTypeConvertible - Utility to check whether a function type can
Eli Friedmanc00129a2010-05-30 06:03:20 +0000133 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
134 /// type).
Chris Lattnerf742eb02011-07-10 00:18:59 +0000135 bool isFuncTypeConvertible(const FunctionType *FT);
136 bool isFuncTypeArgumentConvertible(QualType Ty);
137
Anders Carlsson046c2942010-04-17 20:15:18 +0000138 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000139 /// given a CXXMethodDecl. If the method to has an incomplete return type,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000140 /// and/or incomplete argument types, this will return the opaque type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000141 llvm::Type *GetFunctionTypeForVTable(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000142
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000143 const CGRecordLayout &getCGRecordLayout(const RecordDecl*);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chris Lattnerc5b88062008-02-06 05:08:19 +0000145 /// UpdateCompletedType - When we find the full definition for a TagDecl,
146 /// replace the 'opaque' type we previously made for it if applicable.
147 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000148
John McCalld26bc762011-03-09 04:27:21 +0000149 /// getNullaryFunctionInfo - Get the function info for a void()
150 /// function with standard CC.
151 const CGFunctionInfo &getNullaryFunctionInfo();
152
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000153 /// getFunctionInfo - Get the function info for the specified function decl.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000154 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000155
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000156 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000157 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000158 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000159 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
160 CXXCtorType Type);
161 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
162 CXXDtorType Type);
163
John McCall04a67a62010-02-05 21:31:56 +0000164 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +0000165 const FunctionType *Ty) {
166 return getFunctionInfo(Ty->getResultType(), Args,
167 Ty->getExtInfo());
John McCall04a67a62010-02-05 21:31:56 +0000168 }
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000169
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000170 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
171 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
John McCall04a67a62010-02-05 21:31:56 +0000172
John McCall4c40d982010-08-31 07:33:07 +0000173 /// getFunctionInfo - Get the function info for a member function of
174 /// the given type. This is used for calls through member function
175 /// pointers.
Anders Carlsson375c31c2009-10-03 19:43:08 +0000176 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
177 const FunctionProtoType *FTP);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000178
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000179 /// getFunctionInfo - Get the function info for a function described by a
180 /// return type and argument types. If the calling convention is not
181 /// specified, the "C" calling convention will be used.
Mike Stump1eb44332009-09-09 15:08:12 +0000182 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000183 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000184 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000185 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000186 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000187 const FunctionType::ExtInfo &Info);
John McCallead608a2010-02-26 00:48:12 +0000188
189 /// Retrieves the ABI information for the given function signature.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000190 ///
John McCallead608a2010-02-26 00:48:12 +0000191 /// \param ArgTys - must all actually be canonical as params
192 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
193 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000194 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Daniel Dunbar270e2032010-03-31 00:11:27 +0000196 /// \brief Compute a new LLVM record layout object for the given record.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000197 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D,
198 llvm::StructType *Ty);
199
200 /// addRecordTypeName - Compute a name from the given record decl with an
201 /// optional suffix and name the given LLVM type using it.
202 void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty,
203 llvm::StringRef suffix);
204
Daniel Dunbar270e2032010-03-31 00:11:27 +0000205
Chris Lattner19009e62008-01-09 18:47:25 +0000206public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000207 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
208 llvm::StructType *ConvertRecordDeclType(const RecordDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000209
210 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
211 /// argument types it would be passed as on the provided vector \arg
212 /// ArgTys. See ABIArgInfo::Expand.
John McCall42e06112011-05-15 02:19:42 +0000213 void GetExpandedTypes(QualType type,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000214 llvm::SmallVectorImpl<llvm::Type*> &expanded);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000215
John McCallf16aa102010-08-22 21:01:12 +0000216 /// IsZeroInitializable - Return whether a type can be
217 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
218 bool isZeroInitializable(QualType T);
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000219
John McCallf16aa102010-08-22 21:01:12 +0000220 /// IsZeroInitializable - Return whether a record type can be
221 /// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
222 bool isZeroInitializable(const CXXRecordDecl *RD);
Chris Lattner71305cc2011-07-15 05:16:14 +0000223
224 bool isRecordLayoutComplete(const Type *Ty) const;
225 bool noRecordsBeingLaidOut() const {
226 return RecordsBeingLaidOut.empty();
227 }
228 bool isRecordBeingLaidOut(const Type *Ty) const {
229 return RecordsBeingLaidOut.count(Ty);
230 }
231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232};
Chris Lattnera7674d82007-07-13 22:13:22 +0000233
Reid Spencer5f016e22007-07-11 17:01:13 +0000234} // end namespace CodeGen
235} // end namespace clang
236
237#endif