blob: a30f8e7f3c93abe80e492de2a7f06ceb341db08f [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//
10// This is the code that handles AST -> LLVM type lowering.
11//
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"
23
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;
Anders Carlssonf6f8ae52009-04-03 22:48:58 +000037 class CXXMethodDecl;
Devang Patelb1e39892007-10-23 23:26:46 +000038 class FieldDecl;
Douglas Gregor72564e72009-02-26 23:50:07 +000039 class FunctionProtoType;
Chris Lattner391d77a2008-03-30 23:03:07 +000040 class ObjCInterfaceDecl;
41 class ObjCIvarDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000042 class PointerType;
43 class QualType;
44 class RecordDecl;
45 class TagDecl;
46 class TargetInfo;
47 class Type;
Devang Patelb84a06e2007-10-23 02:10:49 +000048
Reid Spencer5f016e22007-07-11 17:01:13 +000049namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000050 class CodeGenTypes;
51
Devang Patel88a981b2007-11-01 19:11:01 +000052 /// CGRecordLayout - This class handles struct and union layout info while
Devang Patelb84a06e2007-10-23 02:10:49 +000053 /// lowering AST types to LLVM types.
Devang Patel88a981b2007-11-01 19:11:01 +000054 class CGRecordLayout {
55 CGRecordLayout(); // DO NOT IMPLEMENT
Devang Patelb84a06e2007-10-23 02:10:49 +000056 public:
Anders Carlsson45372a62009-07-23 03:17:50 +000057 CGRecordLayout(const llvm::Type *T,
58 const llvm::SmallSet<unsigned, 8> &PF)
Devang Pateleae15602008-02-05 02:39:50 +000059 : STy(T), PaddingFields(PF) {
Devang Patel057afdd2007-10-24 20:38:06 +000060 // FIXME : Collect info about fields that requires adjustments
61 // (i.e. fields that do not directly map to llvm struct fields.)
62 }
Devang Patelb84a06e2007-10-23 02:10:49 +000063
64 /// getLLVMType - Return llvm type associated with this record.
Anders Carlsson45372a62009-07-23 03:17:50 +000065 const llvm::Type *getLLVMType() const {
Devang Patelb84a06e2007-10-23 02:10:49 +000066 return STy;
67 }
68
Devang Pateleae15602008-02-05 02:39:50 +000069 bool isPaddingField(unsigned No) const {
70 return PaddingFields.count(No) != 0;
71 }
72
73 unsigned getNumPaddingFields() {
74 return PaddingFields.size();
75 }
76
Devang Patelb84a06e2007-10-23 02:10:49 +000077 private:
Anders Carlsson45372a62009-07-23 03:17:50 +000078 const llvm::Type *STy;
Devang Pateleae15602008-02-05 02:39:50 +000079 llvm::SmallSet<unsigned, 8> PaddingFields;
Devang Patelb84a06e2007-10-23 02:10:49 +000080 };
Reid Spencer5f016e22007-07-11 17:01:13 +000081
82/// CodeGenTypes - This class organizes the cross-module state that is used
83/// while lowering AST types to LLVM types.
84class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000085 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000087 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000088 const llvm::TargetData& TheTargetData;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000089 mutable const ABIInfo* TheABIInfo;
Anders Carlssonc9e20912007-08-21 00:21:21 +000090
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000091 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000092 llvm::OpaqueType *>, 8> PointersToResolve;
93
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000094 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000095
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000096 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
97
Daniel Dunbar412f59b2009-04-22 10:28:39 +000098 /// The opaque type map for Objective-C interfaces. All direct
99 /// manipulation is done by the runtime interfaces, which are
100 /// responsible for coercing to the appropriate type; these opaque
101 /// types are never refined.
102 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
103
Devang Patel88a981b2007-11-01 19:11:01 +0000104 /// CGRecordLayouts - This maps llvm struct type with corresponding
Devang Patelb84a06e2007-10-23 02:10:49 +0000105 /// record layout info.
Devang Patel88a981b2007-11-01 19:11:01 +0000106 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +0000107 /// inline it in the map.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000108 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +0000109
110 /// FieldInfo - This maps struct field with corresponding llvm struct type
111 /// field no. This info is populated by record organizer.
112 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
113
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000114 /// FunctionInfos - Hold memoized CGFunctionInfo results.
115 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
116
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000117public:
Anders Carlsson8330cee2009-07-23 17:01:21 +0000118 struct BitFieldInfo {
119 BitFieldInfo(unsigned FieldNo,
120 unsigned Start,
121 unsigned Size)
122 : FieldNo(FieldNo), Start(Start), Size(Size) {}
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000123
Anders Carlsson8330cee2009-07-23 17:01:21 +0000124 unsigned FieldNo;
125 unsigned Start;
126 unsigned Size;
Devang Patel159e3302007-11-07 01:57:13 +0000127 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000128
129private:
Devang Patel159e3302007-11-07 01:57:13 +0000130 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
131
Chris Lattner4581fff2008-02-06 05:21:55 +0000132 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000133 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
134 /// used instead of llvm::Type because it allows us to bypass potential
135 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000136 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000137
138 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
139 /// method directly because it does not do any type caching. This method
140 /// is available only for ConvertType(). CovertType() is preferred
141 /// interface to convert type T into a llvm::Type.
142 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000144 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000145 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000146
Devang Pateld9e9ede2007-10-31 20:08:22 +0000147 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000149 ASTContext &getContext() const { return Context; }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000150 const ABIInfo &getABIInfo() const;
Owen Anderson47a434f2009-08-05 23:18:46 +0000151 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000152
Chris Lattner4581fff2008-02-06 05:21:55 +0000153 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000155 const llvm::Type *ConvertTypeRecursive(QualType T);
Chris Lattner19009e62008-01-09 18:47:25 +0000156
Chris Lattner4581fff2008-02-06 05:21:55 +0000157 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
158 /// ConvertType in that it is used to convert to the memory representation for
159 /// a type. For example, the scalar representation for _Bool is i1, but the
160 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000161 const llvm::Type *ConvertTypeForMem(QualType T);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000162 const llvm::Type *ConvertTypeForMemRecursive(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000163
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000164 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000165 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
166 bool IsVariadic);
Chris Lattner19009e62008-01-09 18:47:25 +0000167
Chris Lattneraf319132008-02-05 06:55:31 +0000168 const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
Devang Patelb84a06e2007-10-23 02:10:49 +0000169
170 /// getLLVMFieldNo - Return llvm::StructType element number
171 /// that corresponds to the field FD.
172 unsigned getLLVMFieldNo(const FieldDecl *FD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000173
Chris Lattnerc5b88062008-02-06 05:08:19 +0000174 /// UpdateCompletedType - When we find the full definition for a TagDecl,
175 /// replace the 'opaque' type we previously made for it if applicable.
176 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000177
178 /// getFunctionInfo - Get the CGFunctionInfo for this function signature.
179 const CGFunctionInfo &getFunctionInfo(QualType RetTy,
180 const llvm::SmallVector<QualType,16>
181 &ArgTys);
182
Douglas Gregor72564e72009-02-26 23:50:07 +0000183 const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
184 const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000185 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000186 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000187 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
188 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
189 const CallArgList &Args);
Anders Carlsson5529b242009-04-06 18:05:26 +0000190public:
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000191 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
192 const FunctionArgList &Args);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000193
Chris Lattner19009e62008-01-09 18:47:25 +0000194public: // These are internal details of CGT that shouldn't be used externally.
Devang Patelb84a06e2007-10-23 02:10:49 +0000195 /// addFieldInfo - Assign field number to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000196 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000197
198 /// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000199 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
200 unsigned Start, unsigned Size);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000201
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000202 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
203 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000204 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000205
206 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
207 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000208 const llvm::Type *ConvertTagDeclType(const TagDecl *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.
213 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Reid Spencer5f016e22007-07-11 17:01:13 +0000214};
Chris Lattnera7674d82007-07-13 22:13:22 +0000215
Reid Spencer5f016e22007-07-11 17:01:13 +0000216} // end namespace CodeGen
217} // end namespace clang
218
219#endif