blob: 87ba0bcfba1d00271fd2b895f5c916f0f3b36aea [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;
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;
Devang Patelb84a06e2007-10-23 02:10:49 +000051
Reid Spencer5f016e22007-07-11 17:01:13 +000052namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000053 class CodeGenTypes;
54
Mike Stump1eb44332009-09-09 15:08:12 +000055 /// CGRecordLayout - This class handles struct and union layout info while
Devang Patelb84a06e2007-10-23 02:10:49 +000056 /// lowering AST types to LLVM types.
Devang Patel88a981b2007-11-01 19:11:01 +000057 class CGRecordLayout {
58 CGRecordLayout(); // DO NOT IMPLEMENT
Mike Stump1eb44332009-09-09 15:08:12 +000059
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000060 /// LLVMType - The LLVMType corresponding to this record layout.
61 const llvm::Type *LLVMType;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Anders Carlsson2c12d032010-02-02 05:17:25 +000063 /// ContainsPointerToDataMember - Whether one of the fields in this record
64 /// layout is a pointer to data member, or a struct that contains pointer to
65 /// data member.
66 bool ContainsPointerToDataMember;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Devang Patelb84a06e2007-10-23 02:10:49 +000068 public:
Anders Carlsson2c12d032010-02-02 05:17:25 +000069 CGRecordLayout(const llvm::Type *T, bool ContainsPointerToDataMember)
70 : LLVMType(T), ContainsPointerToDataMember(ContainsPointerToDataMember) { }
Devang Patelb84a06e2007-10-23 02:10:49 +000071
72 /// getLLVMType - Return llvm type associated with this record.
Anders Carlsson45372a62009-07-23 03:17:50 +000073 const llvm::Type *getLLVMType() const {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000074 return LLVMType;
Devang Patelb84a06e2007-10-23 02:10:49 +000075 }
76
Anders Carlsson2c12d032010-02-02 05:17:25 +000077 /// containsPointerToDataMember - Whether this struct contains pointers to
78 /// data members.
79 bool containsPointerToDataMember() const {
80 return ContainsPointerToDataMember;
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000081 }
Devang Patelb84a06e2007-10-23 02:10:49 +000082 };
Mike Stump1eb44332009-09-09 15:08:12 +000083
Reid Spencer5f016e22007-07-11 17:01:13 +000084/// CodeGenTypes - This class organizes the cross-module state that is used
85/// while lowering AST types to LLVM types.
86class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000087 ASTContext &Context;
Daniel Dunbar444be732009-11-13 05:51:54 +000088 const TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000089 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000090 const llvm::TargetData& TheTargetData;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000091 const ABIInfo& TheABIInfo;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000093 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000094 llvm::OpaqueType *>, 8> PointersToResolve;
95
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000096 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000097
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000098 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
99
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000100 /// The opaque type map for Objective-C interfaces. All direct
101 /// manipulation is done by the runtime interfaces, which are
102 /// responsible for coercing to the appropriate type; these opaque
103 /// types are never refined.
104 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
105
Mike Stump1eb44332009-09-09 15:08:12 +0000106 /// CGRecordLayouts - This maps llvm struct type with corresponding
107 /// record layout info.
108 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +0000109 /// inline it in the map.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000110 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +0000111
112 /// FieldInfo - This maps struct field with corresponding llvm struct type
113 /// field no. This info is populated by record organizer.
114 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
115
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000116 /// FunctionInfos - Hold memoized CGFunctionInfo results.
117 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
118
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000119public:
Anders Carlsson8330cee2009-07-23 17:01:21 +0000120 struct BitFieldInfo {
Mike Stump1eb44332009-09-09 15:08:12 +0000121 BitFieldInfo(unsigned FieldNo,
122 unsigned Start,
Anders Carlsson8330cee2009-07-23 17:01:21 +0000123 unsigned Size)
124 : FieldNo(FieldNo), Start(Start), Size(Size) {}
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000125
Anders Carlsson8330cee2009-07-23 17:01:21 +0000126 unsigned FieldNo;
127 unsigned Start;
128 unsigned Size;
Devang Patel159e3302007-11-07 01:57:13 +0000129 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000130
131private:
Devang Patel159e3302007-11-07 01:57:13 +0000132 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
133
Chris Lattner4581fff2008-02-06 05:21:55 +0000134 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000135 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
Mike Stump1eb44332009-09-09 15:08:12 +0000136 /// used instead of llvm::Type because it allows us to bypass potential
Devang Patel30ec9972007-10-25 18:32:36 +0000137 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000138 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000139
140 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
141 /// method directly because it does not do any type caching. This method
142 /// is available only for ConvertType(). CovertType() is preferred
143 /// interface to convert type T into a llvm::Type.
144 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000145public:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000146 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD,
147 const ABIInfo &Info);
Devang Patelb84a06e2007-10-23 02:10:49 +0000148 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Devang Pateld9e9ede2007-10-31 20:08:22 +0000150 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000151 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000152 ASTContext &getContext() const { return Context; }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000153 const ABIInfo &getABIInfo() const { return TheABIInfo; }
Owen Anderson47a434f2009-08-05 23:18:46 +0000154 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000155
Mike Stump1eb44332009-09-09 15:08:12 +0000156 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000158 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattner4581fff2008-02-06 05:21:55 +0000160 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
161 /// ConvertType in that it is used to convert to the memory representation for
162 /// a type. For example, the scalar representation for _Bool is i1, but the
163 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000164 const llvm::Type *ConvertTypeForMem(QualType T);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000165 const llvm::Type *ConvertTypeForMemRecursive(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000166
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000167 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000168 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
169 bool IsVariadic);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Anders Carlssonecf282b2009-11-24 05:08:52 +0000171
172 /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable,
173 /// given a CXXMethodDecl. If the method to has an incomplete return type,
174 /// and/or incomplete argument types, this will return the opaque type.
175 const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD);
176
Anders Carlssonad3e7112009-08-24 17:16:23 +0000177 const CGRecordLayout &getCGRecordLayout(const TagDecl*) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Devang Patelb84a06e2007-10-23 02:10:49 +0000179 /// getLLVMFieldNo - Return llvm::StructType element number
180 /// that corresponds to the field FD.
181 unsigned getLLVMFieldNo(const FieldDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattnerc5b88062008-02-06 05:08:19 +0000183 /// UpdateCompletedType - When we find the full definition for a TagDecl,
184 /// replace the 'opaque' type we previously made for it if applicable.
185 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000186
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000187private:
Douglas Gregor72564e72009-02-26 23:50:07 +0000188 const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
189 const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000190
191public:
192 /// getFunctionInfo - Get the function info for the specified function decl.
Anders Carlssonb2bcf1c2010-02-06 02:44:09 +0000193 const CGFunctionInfo &getFunctionInfo(GlobalDecl GD);
194
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000195 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000196 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000197 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000198 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
199 CXXCtorType Type);
200 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
201 CXXDtorType Type);
202
John McCall04a67a62010-02-05 21:31:56 +0000203 const CGFunctionInfo &getFunctionInfo(const CallArgList &Args,
204 const FunctionType *Ty) {
205 return getFunctionInfo(Ty->getResultType(), Args,
206 Ty->getCallConv(), Ty->getNoReturnAttr());
207 }
208
Anders Carlsson375c31c2009-10-03 19:43:08 +0000209 // getFunctionInfo - Get the function info for a member function.
210 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
211 const FunctionProtoType *FTP);
212
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000213 /// getFunctionInfo - Get the function info for a function described by a
214 /// return type and argument types. If the calling convention is not
215 /// specified, the "C" calling convention will be used.
Mike Stump1eb44332009-09-09 15:08:12 +0000216 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000217 const CallArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000218 CallingConv CC,
219 bool NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +0000220 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000221 const FunctionArgList &Args,
John McCall04a67a62010-02-05 21:31:56 +0000222 CallingConv CC,
223 bool NoReturn);
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000224 const CGFunctionInfo &getFunctionInfo(QualType RetTy,
225 const llvm::SmallVector<QualType, 16> &ArgTys,
John McCall04a67a62010-02-05 21:31:56 +0000226 CallingConv CC,
227 bool NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Chris Lattner19009e62008-01-09 18:47:25 +0000229public: // These are internal details of CGT that shouldn't be used externally.
Devang Patelb84a06e2007-10-23 02:10:49 +0000230 /// addFieldInfo - Assign field number to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000231 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000232
233 /// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000234 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
235 unsigned Start, unsigned Size);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000236
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000237 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
238 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000239 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000240
241 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
242 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000243 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000244
245 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
246 /// argument types it would be passed as on the provided vector \arg
247 /// ArgTys. See ABIArgInfo::Expand.
248 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Reid Spencer5f016e22007-07-11 17:01:13 +0000249};
Chris Lattnera7674d82007-07-13 22:13:22 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251} // end namespace CodeGen
252} // end namespace clang
253
254#endif