blob: 1443516b0d799b922f171e669087f6c2a7c27dad [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"
Devang Pateleae15602008-02-05 02:39:50 +000019#include "llvm/ADT/SmallSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include <vector>
21
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +000022#include "CGCall.h"
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +000023#include "GlobalDecl.h"
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +000024
Reid Spencer5f016e22007-07-11 17:01:13 +000025namespace llvm {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000026 class FunctionType;
Anders Carlsson4e533282007-08-17 22:00:32 +000027 class Module;
Chris Lattnerfce71b82008-04-03 05:50:42 +000028 class OpaqueType;
Devang Patel30ec9972007-10-25 18:32:36 +000029 class PATypeHolder;
Devang Patel7a4718e2007-10-31 20:01:01 +000030 class TargetData;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000031 class Type;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000032 class LLVMContext;
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
35namespace clang {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000036 class ABIInfo;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000037 class ASTContext;
John McCallead608a2010-02-26 00:48:12 +000038 template <typename> class CanQual;
Anders Carlssonf6c56e22009-11-25 03:15:49 +000039 class CXXConstructorDecl;
40 class CXXDestructorDecl;
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000041 class CXXMethodDecl;
Devang Patelb1e39892007-10-23 23:26:46 +000042 class FieldDecl;
Douglas Gregor72564e72009-02-26 23:50:07 +000043 class FunctionProtoType;
Chris Lattner391d77a2008-03-30 23:03:07 +000044 class ObjCInterfaceDecl;
45 class ObjCIvarDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000046 class PointerType;
47 class QualType;
48 class RecordDecl;
49 class TagDecl;
50 class TargetInfo;
51 class Type;
John McCallead608a2010-02-26 00:48:12 +000052 typedef CanQual<Type> CanQualType;
Devang Patelb84a06e2007-10-23 02:10:49 +000053
Reid Spencer5f016e22007-07-11 17:01:13 +000054namespace CodeGen {
Daniel Dunbar2924ade2010-03-30 22:26:10 +000055 class CGRecordLayout;
Devang Patelb84a06e2007-10-23 02:10:49 +000056 class CodeGenTypes;
57
Reid Spencer5f016e22007-07-11 17:01:13 +000058/// CodeGenTypes - This class organizes the cross-module state that is used
59/// while lowering AST types to LLVM types.
60class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000061 ASTContext &Context;
Daniel Dunbar444be732009-11-13 05:51:54 +000062 const TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000063 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000064 const llvm::TargetData& TheTargetData;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000065 const ABIInfo& TheABIInfo;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000067 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000068 llvm::OpaqueType *>, 8> PointersToResolve;
69
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000070 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000071
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000072 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
73
Daniel Dunbar412f59b2009-04-22 10:28:39 +000074 /// The opaque type map for Objective-C interfaces. All direct
75 /// manipulation is done by the runtime interfaces, which are
76 /// responsible for coercing to the appropriate type; these opaque
77 /// types are never refined.
78 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
79
Mike Stump1eb44332009-09-09 15:08:12 +000080 /// CGRecordLayouts - This maps llvm struct type with corresponding
81 /// record layout info.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000082 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000083
84 /// FieldInfo - This maps struct field with corresponding llvm struct type
85 /// field no. This info is populated by record organizer.
86 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
87
Daniel Dunbar40a6be62009-02-03 00:07:12 +000088 /// FunctionInfos - Hold memoized CGFunctionInfo results.
89 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
90
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +000091public:
Anders Carlsson8330cee2009-07-23 17:01:21 +000092 struct BitFieldInfo {
Mike Stump1eb44332009-09-09 15:08:12 +000093 BitFieldInfo(unsigned FieldNo,
94 unsigned Start,
Anders Carlsson8330cee2009-07-23 17:01:21 +000095 unsigned Size)
96 : FieldNo(FieldNo), Start(Start), Size(Size) {}
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +000097
Anders Carlsson8330cee2009-07-23 17:01:21 +000098 unsigned FieldNo;
99 unsigned Start;
100 unsigned Size;
Devang Patel159e3302007-11-07 01:57:13 +0000101 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000102
103private:
Devang Patel159e3302007-11-07 01:57:13 +0000104 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
105
Chris Lattner4581fff2008-02-06 05:21:55 +0000106 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000107 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
Mike Stump1eb44332009-09-09 15:08:12 +0000108 /// used instead of llvm::Type because it allows us to bypass potential
Devang Patel30ec9972007-10-25 18:32:36 +0000109 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000110 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000111
112 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
113 /// method directly because it does not do any type caching. This method
114 /// is available only for ConvertType(). CovertType() is preferred
115 /// interface to convert type T into a llvm::Type.
116 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000117public:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000118 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
119 const ABIInfo &Info);
Devang Patelb84a06e2007-10-23 02:10:49 +0000120 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Devang Pateld9e9ede2007-10-31 20:08:22 +0000122 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000123 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000124 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000125 const ABIInfo &getABIInfo() const { return TheABIInfo; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000126 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000127
Mike Stump1eb44332009-09-09 15:08:12 +0000128 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000130 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattner4581fff2008-02-06 05:21:55 +0000132 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
133 /// ConvertType in that it is used to convert to the memory representation for
134 /// a type. For example, the scalar representation for _Bool is i1, but the
135 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000136 const llvm::Type *ConvertTypeForMem(QualType T);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000137 const llvm::Type *ConvertTypeForMemRecursive(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000138
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000139 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000140 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
141 bool IsVariadic);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
John McCallc0bf4622010-02-23 00:48:20 +0000143 const llvm::FunctionType *GetFunctionType(GlobalDecl GD);
144
Anders Carlssonecf282b2009-11-24 05:08:52 +0000145
146 /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable,
147 /// given a CXXMethodDecl. If the method to has an incomplete return type,
148 /// and/or incomplete argument types, this will return the opaque type.
149 const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD);
150
Daniel Dunbar7c465b92010-03-30 22:26:12 +0000151 const CGRecordLayout &getCGRecordLayout(const RecordDecl*) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Devang Patelb84a06e2007-10-23 02:10:49 +0000153 /// getLLVMFieldNo - Return llvm::StructType element number
154 /// that corresponds to the field FD.
155 unsigned getLLVMFieldNo(const FieldDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattnerc5b88062008-02-06 05:08:19 +0000157 /// UpdateCompletedType - When we find the full definition for a TagDecl,
158 /// replace the 'opaque' type we previously made for it if applicable.
159 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000160
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000161 /// getFunctionInfo - Get the function info for the specified function decl.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000162 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
163
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000164 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000165 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000166 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000167 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
168 CXXCtorType Type);
169 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
170 CXXDtorType Type);
171
John McCall04a67a62010-02-05 21:31:56 +0000172 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
173 const FunctionType *Ty) {
174 return getFunctionInfo(Ty->getResultType(), Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000175 Ty->getExtInfo());
John McCall04a67a62010-02-05 21:31:56 +0000176 }
John McCallead608a2010-02-26 00:48:12 +0000177 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionProtoType> Ty);
178 const CGFunctionInfo &getFunctionInfo(CanQual<FunctionNoProtoType> Ty);
John McCall04a67a62010-02-05 21:31:56 +0000179
Anders Carlsson375c31c2009-10-03 19:43:08 +0000180 // getFunctionInfo - Get the function info for a member function.
181 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
182 const FunctionProtoType *FTP);
183
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000184 /// getFunctionInfo - Get the function info for a function described by a
185 /// return type and argument types. If the calling convention is not
186 /// specified, the "C" calling convention will be used.
Mike Stump1eb44332009-09-09 15:08:12 +0000187 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000188 const CallArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000189 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000190 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000191 const FunctionArgList &Args,
Rafael Espindola264ba482010-03-30 20:24:48 +0000192 const FunctionType::ExtInfo &Info);
John McCallead608a2010-02-26 00:48:12 +0000193
194 /// Retrieves the ABI information for the given function signature.
195 ///
196 /// \param ArgTys - must all actually be canonical as params
197 const CGFunctionInfo &getFunctionInfo(CanQualType RetTy,
198 const llvm::SmallVectorImpl<CanQualType> &ArgTys,
Rafael Espindola264ba482010-03-30 20:24:48 +0000199 const FunctionType::ExtInfo &Info);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattner19009e62008-01-09 18:47:25 +0000201public: // These are internal details of CGT that shouldn't be used externally.
Devang Patelb84a06e2007-10-23 02:10:49 +0000202 /// addFieldInfo - Assign field number to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000203 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000204
205 /// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000206 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
207 unsigned Start, unsigned Size);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000208
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000209 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
210 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000211 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000212
213 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
214 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000215 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000216
217 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
218 /// argument types it would be passed as on the provided vector \arg
219 /// ArgTys. See ABIArgInfo::Expand.
220 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221};
Chris Lattnera7674d82007-07-13 22:13:22 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223} // end namespace CodeGen
224} // end namespace clang
225
226#endif