blob: 674fe8f8b419d8c32dcf2d3c8abf1efeb41f6a30 [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);
Reid Spencer5f016e22007-07-11 17:01:13 +000097public:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000098 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
99 const ABIInfo &Info);
Devang Patelb84a06e2007-10-23 02:10:49 +0000100 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Devang Pateld9e9ede2007-10-31 20:08:22 +0000102 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000103 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000104 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000105 const ABIInfo &getABIInfo() const { return TheABIInfo; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000106 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000107
Mike Stump1eb44332009-09-09 15:08:12 +0000108 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000110 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner4581fff2008-02-06 05:21:55 +0000112 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
113 /// ConvertType in that it is used to convert to the memory representation for
114 /// a type. For example, the scalar representation for _Bool is i1, but the
115 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000116 const llvm::Type *ConvertTypeForMem(QualType T);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000117 const llvm::Type *ConvertTypeForMemRecursive(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000118
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000119 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000120 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
121 bool IsVariadic);
Mike Stump1eb44332009-09-09 15:08:12 +0000122
John McCallc0bf4622010-02-23 00:48:20 +0000123 const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
124
Eli Friedmanc00129a2010-05-30 06:03:20 +0000125 /// VerifyFuncTypeComplete - Utility to check whether a function type can
126 /// be converted to an LLVM type (i.e. doesn't depend on an incomplete tag
127 /// type).
128 static const TagType *VerifyFuncTypeComplete(const Type* T);
Anders Carlssonecf282b2009-11-24 05:08:52 +0000129
Anders Carlsson046c2942010-04-17 20:15:18 +0000130 /// GetFunctionTypeForVTable - Get the LLVM function type for use in a vtable,
Anders Carlssonecf282b2009-11-24 05:08:52 +0000131 /// given a CXXMethodDecl. If the method to has an incomplete return type,
132 /// and/or incomplete argument types, this will return the opaque type.
Anders Carlsson046c2942010-04-17 20:15:18 +0000133 const llvm::Type *GetFunctionTypeForVTable(const CXXMethodDecl *MD);
Anders Carlssonecf282b2009-11-24 05:08:52 +0000134
Daniel Dunbar7c465b92010-03-30 22:26:12 +0000135 const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattnerc5b88062008-02-06 05:08:19 +0000137 /// UpdateCompletedType - When we find the full definition for a TagDecl,
138 /// replace the 'opaque' type we previously made for it if applicable.
139 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000140
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000141 /// getFunctionInfo - Get the function info for the specified function decl.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000142 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
143
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000144 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000145 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000146 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000147 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
148 CXXCtorType Type);
149 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
150 CXXDtorType Type);
151
John McCall04a67a62010-02-05 21:31:56 +0000152 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
153 const FunctionType *Ty) {
154 return getFunctionInfo(Ty->getResultType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000155 Ty->getExtInfo());
John McCall04a67a62010-02-05 21:31:56 +0000156 }
John McCallead608a2010-02-26 00:48:12 +0000157 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
158 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
John McCall04a67a62010-02-05 21:31:56 +0000159
Anders Carlsson375c31c2009-10-03 19:43:08 +0000160 // getFunctionInfo - Get the function info for a member function.
161 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
162 const FunctionProtoType *FTP);
163
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000164 /// getFunctionInfo - Get the function info for a function described by a
165 /// return type and argument types. If the calling convention is not
166 /// specified, the "C" calling convention will be used.
Mike Stump1eb44332009-09-09 15:08:12 +0000167 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000168 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000169 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000170 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000171 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 const FunctionType::ExtInfo &Info);
John McCallead608a2010-02-26 00:48:12 +0000173
174 /// Retrieves the ABI information for the given function signature.
175 ///
176 /// \param ArgTys - must all actually be canonical as params
177 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
178 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000179 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Daniel Dunbar270e2032010-03-31 00:11:27 +0000181 /// \brief Compute a new LLVM record layout object for the given record.
182 CGRecordLayout *ComputeRecordLayout(const RecordDecl *D);
183
Chris Lattner19009e62008-01-09 18:47:25 +0000184public: // These are internal details of CGT that shouldn't be used externally.
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000185 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
186 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000187 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000188
189 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
190 /// argument types it would be passed as on the provided vector \arg
191 /// ArgTys. See ABIArgInfo::Expand.
192 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000193
194 /// ContainsPointerToDataMember - Return whether the given type contains a
195 /// pointer to a data member.
196 bool ContainsPointerToDataMember(QualType T);
Anders Carlssonc39211d2010-05-18 03:47:15 +0000197
198 /// ContainsPointerToDataMember - Return whether the record decl contains a
199 /// pointer to a data member.
200 bool ContainsPointerToDataMember(const CXXRecordDecl *RD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201};
Chris Lattnera7674d82007-07-13 22:13:22 +0000202
Reid Spencer5f016e22007-07-11 17:01:13 +0000203} // end namespace CodeGen
204} // end namespace clang
205
206#endif