blob: 0c9ab7ef63d2a8c3330d425562e92a64a7cab35d [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 {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000033 class ASTContext;
Devang Patelb1e39892007-10-23 23:26:46 +000034 class FieldDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000035 class FunctionTypeProto;
Chris Lattner391d77a2008-03-30 23:03:07 +000036 class ObjCInterfaceDecl;
37 class ObjCIvarDecl;
Daniel Dunbar45c25ba2008-09-10 04:01:49 +000038 class PointerLikeType;
39 class PointerType;
40 class QualType;
41 class RecordDecl;
42 class TagDecl;
43 class TargetInfo;
44 class Type;
Devang Patelb84a06e2007-10-23 02:10:49 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000047 class CodeGenTypes;
48
Devang Patel88a981b2007-11-01 19:11:01 +000049 /// CGRecordLayout - This class handles struct and union layout info while
Devang Patelb84a06e2007-10-23 02:10:49 +000050 /// lowering AST types to LLVM types.
Devang Patel88a981b2007-11-01 19:11:01 +000051 class CGRecordLayout {
52 CGRecordLayout(); // DO NOT IMPLEMENT
Devang Patelb84a06e2007-10-23 02:10:49 +000053 public:
Devang Pateleae15602008-02-05 02:39:50 +000054 CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF)
55 : STy(T), PaddingFields(PF) {
Devang Patel057afdd2007-10-24 20:38:06 +000056 // FIXME : Collect info about fields that requires adjustments
57 // (i.e. fields that do not directly map to llvm struct fields.)
58 }
Devang Patelb84a06e2007-10-23 02:10:49 +000059
60 /// getLLVMType - Return llvm type associated with this record.
Devang Patelc4c429a2007-10-24 00:56:23 +000061 llvm::Type *getLLVMType() const {
Devang Patelb84a06e2007-10-23 02:10:49 +000062 return STy;
63 }
64
Devang Pateleae15602008-02-05 02:39:50 +000065 bool isPaddingField(unsigned No) const {
66 return PaddingFields.count(No) != 0;
67 }
68
69 unsigned getNumPaddingFields() {
70 return PaddingFields.size();
71 }
72
Devang Patelb84a06e2007-10-23 02:10:49 +000073 private:
74 llvm::Type *STy;
Devang Pateleae15602008-02-05 02:39:50 +000075 llvm::SmallSet<unsigned, 8> PaddingFields;
Devang Patelb84a06e2007-10-23 02:10:49 +000076 };
Reid Spencer5f016e22007-07-11 17:01:13 +000077
78/// CodeGenTypes - This class organizes the cross-module state that is used
79/// while lowering AST types to LLVM types.
80class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000081 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000082 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000083 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000084 const llvm::TargetData& TheTargetData;
Anders Carlssonc9e20912007-08-21 00:21:21 +000085
Chris Lattnerfce71b82008-04-03 05:50:42 +000086
87 llvm::SmallVector<std::pair<const PointerLikeType *,
88 llvm::OpaqueType *>, 8> PointersToResolve;
89
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000090 llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000091
Devang Patel88a981b2007-11-01 19:11:01 +000092 /// CGRecordLayouts - This maps llvm struct type with corresponding
Devang Patelb84a06e2007-10-23 02:10:49 +000093 /// record layout info.
Devang Patel88a981b2007-11-01 19:11:01 +000094 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +000095 /// inline it in the map.
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +000096 llvm::DenseMap<const Type*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000097
98 /// FieldInfo - This maps struct field with corresponding llvm struct type
99 /// field no. This info is populated by record organizer.
100 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +0000101 llvm::DenseMap<const ObjCIvarDecl *, unsigned> ObjCIvarInfo;
Devang Patelb84a06e2007-10-23 02:10:49 +0000102
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000103public:
Devang Patel159e3302007-11-07 01:57:13 +0000104 class BitFieldInfo {
105 public:
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000106 explicit BitFieldInfo(unsigned short B, unsigned short S)
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000107 : Begin(B), Size(S) {}
108
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000109 unsigned short Begin;
110 unsigned short Size;
Devang Patel159e3302007-11-07 01:57:13 +0000111 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000112
113private:
Devang Patel159e3302007-11-07 01:57:13 +0000114 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
115
Chris Lattner4581fff2008-02-06 05:21:55 +0000116 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000117 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
118 /// used instead of llvm::Type because it allows us to bypass potential
119 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000120 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000121
122 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
123 /// method directly because it does not do any type caching. This method
124 /// is available only for ConvertType(). CovertType() is preferred
125 /// interface to convert type T into a llvm::Type.
126 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000128 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000129 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000130
Devang Pateld9e9ede2007-10-31 20:08:22 +0000131 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000133 ASTContext &getContext() const { return Context; }
Devang Patel5825ac22007-10-25 21:40:12 +0000134
Chris Lattner4581fff2008-02-06 05:21:55 +0000135 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000137 const llvm::Type *ConvertTypeRecursive(QualType T);
Chris Lattner19009e62008-01-09 18:47:25 +0000138
Chris Lattner4581fff2008-02-06 05:21:55 +0000139 /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
140 /// ConvertType in that it is used to convert to the memory representation for
141 /// a type. For example, the scalar representation for _Bool is i1, but the
142 /// memory representation is usually i8 or i32, depending on the target.
Chris Lattner19009e62008-01-09 18:47:25 +0000143 const llvm::Type *ConvertTypeForMem(QualType T);
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000144
145 /// GetFunctionType - Get the LLVM function type from Info.
146 const llvm::FunctionType *GetFunctionType(const CGFunctionInfo &Info);
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000147 /// GetFunctionType - Get the LLVM function type from Info.
148 /// \param IsVariadic Should the resulting type be variadic?
149 const llvm::FunctionType *GetFunctionType(const CGCallInfo &Info,
150 bool IsVariadic);
151
152 /// GetFunctionType - Get the LLVM function type for the given types
153 /// and variadicness.
154 // FIXME: Do we even need IsVariadic here?
155 const llvm::FunctionType *GetFunctionType(ArgTypeIterator begin,
156 ArgTypeIterator end,
157 bool IsVariadic);
Chris Lattner19009e62008-01-09 18:47:25 +0000158
Chris Lattner391d77a2008-03-30 23:03:07 +0000159 void CollectObjCIvarTypes(ObjCInterfaceDecl *ObjCClass,
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000160 std::vector<const llvm::Type*> &IvarTypes);
Chris Lattner19009e62008-01-09 18:47:25 +0000161
Chris Lattneraf319132008-02-05 06:55:31 +0000162 const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
Chris Lattner41110242008-06-17 18:05:57 +0000163 /// Returns a StructType representing an Objective-C object
164 const llvm::Type *ConvertObjCInterfaceToStruct(const ObjCInterfaceDecl *OID);
Devang Patelb84a06e2007-10-23 02:10:49 +0000165
166 /// getLLVMFieldNo - Return llvm::StructType element number
167 /// that corresponds to the field FD.
168 unsigned getLLVMFieldNo(const FieldDecl *FD);
Chris Lattner391d77a2008-03-30 23:03:07 +0000169 unsigned getLLVMFieldNo(const ObjCIvarDecl *OID);
Chris Lattner19009e62008-01-09 18:47:25 +0000170
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000171
Chris Lattnerc5b88062008-02-06 05:08:19 +0000172 /// UpdateCompletedType - When we find the full definition for a TagDecl,
173 /// replace the 'opaque' type we previously made for it if applicable.
174 void UpdateCompletedType(const TagDecl *TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000175
Chris Lattner19009e62008-01-09 18:47:25 +0000176public: // These are internal details of CGT that shouldn't be used externally.
Devang Patelb84a06e2007-10-23 02:10:49 +0000177 /// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000178 void addFieldInfo(const FieldDecl *FD, unsigned No);
179
180 /// addBitFieldInfo - Assign a start bit and a size to field FD.
181 void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
182
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000183 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
184 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000185 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000186
187 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
188 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000189 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Daniel Dunbar56273772008-09-17 00:51:38 +0000190
191 /// GetExpandedTypes - Expand the type \arg Ty into the LLVM
192 /// argument types it would be passed as on the provided vector \arg
193 /// ArgTys. See ABIArgInfo::Expand.
194 void GetExpandedTypes(QualType Ty, std::vector<const llvm::Type*> &ArgTys);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195};
Chris Lattnera7674d82007-07-13 22:13:22 +0000196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197} // end namespace CodeGen
198} // end namespace clang
199
200#endif