blob: 3070c85260987af4d445c83a3edbeca950a2c479 [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
Anders Carlssonc9e20912007-08-21 00:21:21 +000017#include "llvm/ADT/DenseMap.h"
Devang Pateleae15602008-02-05 02:39:50 +000018#include "llvm/ADT/SmallSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include <vector>
20
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +000021#include "CGCall.h"
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023namespace llvm {
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000024 class FunctionType;
Anders Carlsson4e533282007-08-17 22:00:32 +000025 class Module;
Chris Lattnerfce71b82008-04-03 05:50:42 +000026 class OpaqueType;
Devang Patel30ec9972007-10-25 18:32:36 +000027 class PATypeHolder;
Devang Patel7a4718e2007-10-31 20:01:01 +000028 class TargetData;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000029 class Type;
Reid Spencer5f016e22007-07-11 17:01:13 +000030}
31
32namespace clang {
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +000033 class ABIInfo;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000034 class ASTContext;
Devang Patelb1e39892007-10-23 23:26:46 +000035 class FieldDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000036 class FunctionTypeProto;
Chris Lattner391d77a2008-03-30 23:03:07 +000037 class ObjCInterfaceDecl;
38 class ObjCIvarDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000039 class PointerLikeType;
40 class PointerType;
41 class QualType;
42 class RecordDecl;
43 class TagDecl;
44 class TargetInfo;
45 class Type;
Devang Patelb84a06e2007-10-23 02:10:49 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000048 class CodeGenTypes;
49
Devang Patel88a981b2007-11-01 19:11:01 +000050 /// CGRecordLayout - This class handles struct and union layout info while
Devang Patelb84a06e2007-10-23 02:10:49 +000051 /// lowering AST types to LLVM types.
Devang Patel88a981b2007-11-01 19:11:01 +000052 class CGRecordLayout {
53 CGRecordLayout(); // DO NOT IMPLEMENT
Devang Patelb84a06e2007-10-23 02:10:49 +000054 public:
Devang Pateleae15602008-02-05 02:39:50 +000055 CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF)
56 : STy(T), PaddingFields(PF) {
Devang Patel057afdd2007-10-24 20:38:06 +000057 // FIXME : Collect info about fields that requires adjustments
58 // (i.e. fields that do not directly map to llvm struct fields.)
59 }
Devang Patelb84a06e2007-10-23 02:10:49 +000060
61 /// getLLVMType - Return llvm type associated with this record.
Devang Patelc4c429a2007-10-24 00:56:23 +000062 llvm::Type *getLLVMType() const {
Devang Patelb84a06e2007-10-23 02:10:49 +000063 return STy;
64 }
65
Devang Pateleae15602008-02-05 02:39:50 +000066 bool isPaddingField(unsigned No) const {
67 return PaddingFields.count(No) != 0;
68 }
69
70 unsigned getNumPaddingFields() {
71 return PaddingFields.size();
72 }
73
Devang Patelb84a06e2007-10-23 02:10:49 +000074 private:
75 llvm::Type *STy;
Devang Pateleae15602008-02-05 02:39:50 +000076 llvm::SmallSet<unsigned, 8> PaddingFields;
Devang Patelb84a06e2007-10-23 02:10:49 +000077 };
Reid Spencer5f016e22007-07-11 17:01:13 +000078
79/// 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;
Anders Carlssonc9e20912007-08-21 00:21:21 +000087
Chris Lattnerfce71b82008-04-03 05:50:42 +000088 llvm::SmallVector<std::pair<const PointerLikeType *,
89 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
Devang Patel88a981b2007-11-01 19:11:01 +000093 /// CGRecordLayouts - This maps llvm struct type with corresponding
Devang Patelb84a06e2007-10-23 02:10:49 +000094 /// record layout info.
Devang Patel88a981b2007-11-01 19:11:01 +000095 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +000096 /// inline it in the map.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000097 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000098
99 /// FieldInfo - This maps struct field with corresponding llvm struct type
100 /// field no. This info is populated by record organizer.
101 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
102
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000103 /// FunctionInfos - Hold memoized CGFunctionInfo results.
104 llvm::FoldingSet<CGFunctionInfo> FunctionInfos;
105
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000106public:
Devang Patel159e3302007-11-07 01:57:13 +0000107 class BitFieldInfo {
108 public:
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000109 explicit BitFieldInfo(unsigned short B, unsigned short S)
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000110 : Begin(B), Size(S) {}
111
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000112 unsigned short Begin;
113 unsigned short Size;
Devang Patel159e3302007-11-07 01:57:13 +0000114 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000115
116private:
Devang Patel159e3302007-11-07 01:57:13 +0000117 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
118
Chris Lattner4581fff2008-02-06 05:21:55 +0000119 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000120 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
121 /// used instead of llvm::Type because it allows us to bypass potential
122 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000123 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000124
125 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
126 /// method directly because it does not do any type caching. This method
127 /// is available only for ConvertType(). CovertType() is preferred
128 /// interface to convert type T into a llvm::Type.
129 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000131 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000132 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000133
Devang Pateld9e9ede2007-10-31 20:08:22 +0000134 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000136 ASTContext &getContext() const { return Context; }
Daniel Dunbar6b1da0e2008-10-13 17:02:26 +0000137 const ABIInfo &getABIInfo() const;
Devang Patel5825ac22007-10-25 21:40:12 +0000138
Chris Lattner4581fff2008-02-06 05:21:55 +0000139 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000141 const llvm::Type *ConvertTypeRecursive(QualType T);
Chris Lattner19009e62008-01-09 18:47:25 +0000142
Chris Lattner4581fff2008-02-06 05:21:55 +0000143 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
144 /// ConvertType in that it is used to convert to the memory representation for
145 /// a type. For example, the scalar representation for _Bool is i1, but the
146 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000147 const llvm::Type *ConvertTypeForMem(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000148
Daniel Dunbar36b5f5e2009-01-31 03:05:44 +0000149 /// GetFunctionType - Get the LLVM function type for \arg Info.
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000150 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info,
151 bool IsVariadic);
Chris Lattner19009e62008-01-09 18:47:25 +0000152
Chris Lattneraf319132008-02-05 06:55:31 +0000153 const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
Chris Lattner41110242008-06-17 18:05:57 +0000154 /// Returns a StructType representing an Objective-C object
155 const llvm::Type *ConvertObjCInterfaceToStruct(const ObjCInterfaceDecl *OID);
Devang Patelb84a06e2007-10-23 02:10:49 +0000156
157 /// getLLVMFieldNo - Return llvm::StructType element number
158 /// that corresponds to the field FD.
159 unsigned getLLVMFieldNo(const FieldDecl *FD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000160
Chris Lattnerc5b88062008-02-06 05:08:19 +0000161 /// UpdateCompletedType - When we find the full definition for a TagDecl,
162 /// replace the 'opaque' type we previously made for it if applicable.
163 void UpdateCompletedType(const TagDecl *TD);
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000164
165 /// getFunctionInfo - Get the CGFunctionInfo for this function signature.
166 const CGFunctionInfo &getFunctionInfo(QualType RetTy,
167 const llvm::SmallVector<QualType,16>
168 &ArgTys);
169
170 const CGFunctionInfo &getFunctionInfo(const FunctionTypeNoProto *FTNP);
171 const CGFunctionInfo &getFunctionInfo(const FunctionTypeProto *FTP);
172 const CGFunctionInfo &getFunctionInfo(const FunctionDecl *FD);
173 const CGFunctionInfo &getFunctionInfo(const ObjCMethodDecl *MD);
174 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
175 const CallArgList &Args);
176 const CGFunctionInfo &getFunctionInfo(QualType ResTy,
177 const FunctionArgList &Args);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000178
Chris Lattner19009e62008-01-09 18:47:25 +0000179public: // These are internal details of CGT that shouldn't be used externally.
Devang Patelb84a06e2007-10-23 02:10:49 +0000180 /// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000181 void addFieldInfo(const FieldDecl *FD, unsigned No);
182
183 /// addBitFieldInfo - Assign a start bit and a size to field FD.
184 void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
185
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000186 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
187 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000188 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000189
190 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
191 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000192 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000193
194 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
195 /// argument types it would be passed as on the provided vector \arg
196 /// ArgTys. See ABIArgInfo::Expand.
197 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198};
Chris Lattnera7674d82007-07-13 22:13:22 +0000199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200} // end namespace CodeGen
201} // end namespace clang
202
203#endif