blob: 2ff602f900fa85af506dd68143a6adf77540858a [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 Carlssonf6c56e22009-11-25 03:15:49 +000023#include "CGCXX.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 Carlssonfc3eaa42009-08-23 01:25:01 +000063 /// ContainsMemberPointer - Whether one of the fields in this record layout
64 /// is a member pointer, or a struct that contains a member pointer.
65 bool ContainsMemberPointer;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Devang Patelb84a06e2007-10-23 02:10:49 +000067 public:
Anders Carlsson1a5e0d72009-11-30 23:41:22 +000068 CGRecordLayout(const llvm::Type *T, bool ContainsMemberPointer)
69 : LLVMType(T), ContainsMemberPointer(ContainsMemberPointer) { }
Devang Patelb84a06e2007-10-23 02:10:49 +000070
71 /// getLLVMType - Return llvm type associated with this record.
Anders Carlsson45372a62009-07-23 03:17:50 +000072 const llvm::Type *getLLVMType() const {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000073 return LLVMType;
Devang Patelb84a06e2007-10-23 02:10:49 +000074 }
75
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000076 bool containsMemberPointer() const {
77 return ContainsMemberPointer;
78 }
Devang Patelb84a06e2007-10-23 02:10:49 +000079 };
Mike Stump1eb44332009-09-09 15:08:12 +000080
Reid Spencer5f016e22007-07-11 17:01:13 +000081/// CodeGenTypes - This class organizes the cross-module state that is used
82/// while lowering AST types to LLVM types.
83class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000084 ASTContext &Context;
Daniel Dunbar444be732009-11-13 05:51:54 +000085 const TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000086 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000087 const llvm::TargetData& TheTargetData;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000088 mutable const ABIInfo* TheABIInfo;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000090 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000091 llvm::OpaqueType *>, 8> PointersToResolve;
92
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000093 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000094
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000095 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
96
Daniel Dunbar412f59b2009-04-22 10:28:39 +000097 /// The opaque type map for Objective-C interfaces. All direct
98 /// manipulation is done by the runtime interfaces, which are
99 /// responsible for coercing to the appropriate type; these opaque
100 /// types are never refined.
101 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
102
Mike Stump1eb44332009-09-09 15:08:12 +0000103 /// CGRecordLayouts - This maps llvm struct type with corresponding
104 /// record layout info.
105 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +0000106 /// inline it in the map.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000107 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +0000108
109 /// FieldInfo - This maps struct field with corresponding llvm struct type
110 /// field no. This info is populated by record organizer.
111 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
112
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000113 /// FunctionInfos - Hold memoized CGFunctionInfo results.
114 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
115
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000116public:
Anders Carlsson8330cee2009-07-23 17:01:21 +0000117 struct BitFieldInfo {
Mike Stump1eb44332009-09-09 15:08:12 +0000118 BitFieldInfo(unsigned FieldNo,
119 unsigned Start,
Anders Carlsson8330cee2009-07-23 17:01:21 +0000120 unsigned Size)
121 : FieldNo(FieldNo), Start(Start), Size(Size) {}
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000122
Anders Carlsson8330cee2009-07-23 17:01:21 +0000123 unsigned FieldNo;
124 unsigned Start;
125 unsigned Size;
Devang Patel159e3302007-11-07 01:57:13 +0000126 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000127
128private:
Devang Patel159e3302007-11-07 01:57:13 +0000129 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
130
Chris Lattner4581fff2008-02-06 05:21:55 +0000131 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000132 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
Mike Stump1eb44332009-09-09 15:08:12 +0000133 /// used instead of llvm::Type because it allows us to bypass potential
Devang Patel30ec9972007-10-25 18:32:36 +0000134 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000135 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000136
137 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
138 /// method directly because it does not do any type caching. This method
139 /// is available only for ConvertType(). CovertType() is preferred
140 /// interface to convert type T into a llvm::Type.
141 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000142public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000143 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000144 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Devang Pateld9e9ede2007-10-31 20:08:22 +0000146 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar444be732009-11-13 05:51:54 +0000147 const TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000148 ASTContext &getContext() const { return Context; }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000149 const ABIInfo &getABIInfo() const;
Owen Anderson47a434f2009-08-05 23:18:46 +0000150 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000151
Mike Stump1eb44332009-09-09 15:08:12 +0000152 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000154 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner4581fff2008-02-06 05:21:55 +0000156 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
157 /// ConvertType in that it is used to convert to the memory representation for
158 /// a type. For example, the scalar representation for _Bool is i1, but the
159 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000160 const llvm::Type *ConvertTypeForMem(QualType T);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000161 const llvm::Type *ConvertTypeForMemRecursive(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000162
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000163 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000164 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
165 bool IsVariadic);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Anders Carlssonecf282b2009-11-24 05:08:52 +0000167
168 /// GetFunctionTypeForVtable - Get the LLVM function type for use in a vtable,
169 /// given a CXXMethodDecl. If the method to has an incomplete return type,
170 /// and/or incomplete argument types, this will return the opaque type.
171 const llvm::Type *GetFunctionTypeForVtable(const CXXMethodDecl *MD);
172
Anders Carlssonad3e7112009-08-24 17:16:23 +0000173 const CGRecordLayout &getCGRecordLayout(const TagDecl*) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Devang Patelb84a06e2007-10-23 02:10:49 +0000175 /// getLLVMFieldNo - Return llvm::StructType element number
176 /// that corresponds to the field FD.
177 unsigned getLLVMFieldNo(const FieldDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattnerc5b88062008-02-06 05:08:19 +0000179 /// UpdateCompletedType - When we find the full definition for a TagDecl,
180 /// replace the 'opaque' type we previously made for it if applicable.
181 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000182
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000183private:
Douglas Gregor72564e72009-02-26 23:50:07 +0000184 const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
185 const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000186
187public:
188 /// getFunctionInfo - Get the function info for the specified function decl.
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000189 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000190 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000191 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000192 const CGFunctionInfo &getFunctionInfo(const CXXConstructorDecl *D,
193 CXXCtorType Type);
194 const CGFunctionInfo &getFunctionInfo(const CXXDestructorDecl *D,
195 CXXDtorType Type);
196
Anders Carlsson375c31c2009-10-03 19:43:08 +0000197 // getFunctionInfo - Get the function info for a member function.
198 const CGFunctionInfo &getFunctionInfo(const CXXRecordDecl *RD,
199 const FunctionProtoType *FTP);
200
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000201 /// getFunctionInfo - Get the function info for a function described by a
202 /// return type and argument types. If the calling convention is not
203 /// specified, the "C" calling convention will be used.
Mike Stump1eb44332009-09-09 15:08:12 +0000204 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000205 const CallArgList &Args,
206 unsigned CallingConvention = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000207 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000208 const FunctionArgList &Args,
209 unsigned CallingConvention = 0);
210 const CGFunctionInfo &getFunctionInfo(QualType RetTy,
211 const llvm::SmallVector<QualType, 16> &ArgTys,
212 unsigned CallingConvention = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattner19009e62008-01-09 18:47:25 +0000214public: // These are internal details of CGT that shouldn't be used externally.
Devang Patelb84a06e2007-10-23 02:10:49 +0000215 /// addFieldInfo - Assign field number to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000216 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000217
218 /// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000219 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
220 unsigned Start, unsigned Size);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000221
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000222 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
223 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000224 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000225
226 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
227 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000228 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000229
230 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
231 /// argument types it would be passed as on the provided vector \arg
232 /// ArgTys. See ABIArgInfo::Expand.
233 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234};
Chris Lattnera7674d82007-07-13 22:13:22 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236} // end namespace CodeGen
237} // end namespace clang
238
239#endif