blob: 0e73d481cd86f739d0251690754c70d943342058 [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"
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
Mike Stump1eb44332009-09-09 15:08:12 +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
Mike Stump1eb44332009-09-09 15:08:12 +000056
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000057 /// LLVMType - The LLVMType corresponding to this record layout.
58 const llvm::Type *LLVMType;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000060 /// ContainsMemberPointer - Whether one of the fields in this record layout
61 /// is a member pointer, or a struct that contains a member pointer.
62 bool ContainsMemberPointer;
Mike Stump1eb44332009-09-09 15:08:12 +000063
Devang Patelb84a06e2007-10-23 02:10:49 +000064 public:
Mike Stump1eb44332009-09-09 15:08:12 +000065 CGRecordLayout(const llvm::Type *T, bool ContainsMemberPointer)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000066 : LLVMType(T), ContainsMemberPointer(ContainsMemberPointer) { }
Devang Patelb84a06e2007-10-23 02:10:49 +000067
68 /// getLLVMType - Return llvm type associated with this record.
Anders Carlsson45372a62009-07-23 03:17:50 +000069 const llvm::Type *getLLVMType() const {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000070 return LLVMType;
Devang Patelb84a06e2007-10-23 02:10:49 +000071 }
72
Anders Carlssonfc3eaa42009-08-23 01:25:01 +000073 bool containsMemberPointer() const {
74 return ContainsMemberPointer;
75 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Devang Patelb84a06e2007-10-23 02:10:49 +000077 };
Mike Stump1eb44332009-09-09 15:08:12 +000078
Reid Spencer5f016e22007-07-11 17:01:13 +000079/// CodeGenTypes - This class organizes the cross-module state that is used
80/// while lowering AST types to LLVM types.
81class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000082 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000083 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000084 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000085 const llvm::TargetData& TheTargetData;
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000086 mutable const ABIInfo* TheABIInfo;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +000088 llvm::SmallVector<std::pair<QualType,
Chris Lattnerfce71b82008-04-03 05:50:42 +000089 llvm::OpaqueType *>, 8> PointersToResolve;
90
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000091 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000092
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +000093 llvm::DenseMap<const Type*, llvm::PATypeHolder> FunctionTypes;
94
Daniel Dunbar412f59b2009-04-22 10:28:39 +000095 /// The opaque type map for Objective-C interfaces. All direct
96 /// manipulation is done by the runtime interfaces, which are
97 /// responsible for coercing to the appropriate type; these opaque
98 /// types are never refined.
99 llvm::DenseMap<const ObjCInterfaceType*, const llvm::Type *> InterfaceTypes;
100
Mike Stump1eb44332009-09-09 15:08:12 +0000101 /// CGRecordLayouts - This maps llvm struct type with corresponding
102 /// record layout info.
103 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +0000104 /// inline it in the map.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000105 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +0000106
107 /// FieldInfo - This maps struct field with corresponding llvm struct type
108 /// field no. This info is populated by record organizer.
109 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
110
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000111 /// FunctionInfos - Hold memoized CGFunctionInfo results.
112 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
113
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000114public:
Anders Carlsson8330cee2009-07-23 17:01:21 +0000115 struct BitFieldInfo {
Mike Stump1eb44332009-09-09 15:08:12 +0000116 BitFieldInfo(unsigned FieldNo,
117 unsigned Start,
Anders Carlsson8330cee2009-07-23 17:01:21 +0000118 unsigned Size)
119 : FieldNo(FieldNo), Start(Start), Size(Size) {}
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000120
Anders Carlsson8330cee2009-07-23 17:01:21 +0000121 unsigned FieldNo;
122 unsigned Start;
123 unsigned Size;
Devang Patel159e3302007-11-07 01:57:13 +0000124 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000125
126private:
Devang Patel159e3302007-11-07 01:57:13 +0000127 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
128
Chris Lattner4581fff2008-02-06 05:21:55 +0000129 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000130 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
Mike Stump1eb44332009-09-09 15:08:12 +0000131 /// used instead of llvm::Type because it allows us to bypass potential
Devang Patel30ec9972007-10-25 18:32:36 +0000132 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000133 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000134
135 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
136 /// method directly because it does not do any type caching. This method
137 /// is available only for ConvertType(). CovertType() is preferred
138 /// interface to convert type T into a llvm::Type.
139 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000140public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000141 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000142 ~CodeGenTypes();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Devang Pateld9e9ede2007-10-31 20:08:22 +0000144 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000146 ASTContext &getContext() const { return Context; }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000147 const ABIInfo &getABIInfo() const;
Owen Anderson47a434f2009-08-05 23:18:46 +0000148 llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
Devang Patel5825ac22007-10-25 21:40:12 +0000149
Mike Stump1eb44332009-09-09 15:08:12 +0000150 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000152 const llvm::Type *ConvertTypeRecursive(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Chris Lattner4581fff2008-02-06 05:21:55 +0000154 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
155 /// ConvertType in that it is used to convert to the memory representation for
156 /// a type. For example, the scalar representation for _Bool is i1, but the
157 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000158 const llvm::Type *ConvertTypeForMem(QualType T);
Eli Friedman57a84fb2009-03-03 04:48:01 +0000159 const llvm::Type *ConvertTypeForMemRecursive(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000160
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000161 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000162 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
163 bool IsVariadic);
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Anders Carlssonad3e7112009-08-24 17:16:23 +0000165 const CGRecordLayout &getCGRecordLayout(const TagDecl*) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Devang Patelb84a06e2007-10-23 02:10:49 +0000167 /// getLLVMFieldNo - Return llvm::StructType element number
168 /// that corresponds to the field FD.
169 unsigned getLLVMFieldNo(const FieldDecl *FD);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattnerc5b88062008-02-06 05:08:19 +0000171 /// UpdateCompletedType - When we find the full definition for a TagDecl,
172 /// replace the 'opaque' type we previously made for it if applicable.
173 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000174
175 /// getFunctionInfo - Get the CGFunctionInfo for this function signature.
Mike Stump1eb44332009-09-09 15:08:12 +0000176 const CGFunctionInfo &getFunctionInfo(QualType RetTy,
177 const llvm::SmallVector<QualType,16>
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000178 &ArgTys);
179
Douglas Gregor72564e72009-02-26 23:50:07 +0000180 const CGFunctionInfo &getFunctionInfo(const FunctionNoProtoType *FTNP);
181 const CGFunctionInfo &getFunctionInfo(const FunctionProtoType *FTP);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000182 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
Anders Carlssonf6f8ae52009-04-03 22:48:58 +0000183 const CGFunctionInfo &getFunctionInfo(const CXXMethodDecl *MD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000184 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
Mike Stump1eb44332009-09-09 15:08:12 +0000185 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000186 const CallArgList &Args);
Anders Carlsson5529b242009-04-06 18:05:26 +0000187public:
Mike Stump1eb44332009-09-09 15:08:12 +0000188 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000189 const FunctionArgList &Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Chris Lattner19009e62008-01-09 18:47:25 +0000191public: // These are internal details of CGT that shouldn't be used externally.
Devang Patelb84a06e2007-10-23 02:10:49 +0000192 /// addFieldInfo - Assign field number to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000193 void addFieldInfo(const FieldDecl *FD, unsigned FieldNo);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000194
195 /// addBitFieldInfo - Assign a start bit and a size to field FD.
Anders Carlsson8330cee2009-07-23 17:01:21 +0000196 void addBitFieldInfo(const FieldDecl *FD, unsigned FieldNo,
197 unsigned Start, unsigned Size);
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000198
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000199 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
200 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000201 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000202
203 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
204 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000205 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000206
207 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
208 /// argument types it would be passed as on the provided vector \arg
209 /// ArgTys. See ABIArgInfo::Expand.
210 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Reid Spencer5f016e22007-07-11 17:01:13 +0000211};
Chris Lattnera7674d82007-07-13 22:13:22 +0000212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213} // end namespace CodeGen
214} // end namespace clang
215
216#endif