blob: 536e881f8d24203f54a5e5e47f55ef6d3b94f9aa [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
21namespace llvm {
Anders Carlsson4e533282007-08-17 22:00:32 +000022 class Module;
Reid Spencer5f016e22007-07-11 17:01:13 +000023 class Type;
Chris Lattnerfce71b82008-04-03 05:50:42 +000024 class OpaqueType;
Devang Patel30ec9972007-10-25 18:32:36 +000025 class PATypeHolder;
Devang Patel7a4718e2007-10-31 20:01:01 +000026 class TargetData;
Reid Spencer5f016e22007-07-11 17:01:13 +000027}
28
29namespace clang {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000030 class ASTContext;
Anders Carlssonc9e20912007-08-21 00:21:21 +000031 class TagDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 class TargetInfo;
33 class QualType;
Chris Lattnerfce71b82008-04-03 05:50:42 +000034 class PointerType;
35 class PointerLikeType;
Devang Patel30ec9972007-10-25 18:32:36 +000036 class Type;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 class FunctionTypeProto;
Devang Patelb1e39892007-10-23 23:26:46 +000038 class FieldDecl;
39 class RecordDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +000040 class ObjCInterfaceDecl;
41 class ObjCIvarDecl;
Devang Patelb84a06e2007-10-23 02:10:49 +000042
Reid Spencer5f016e22007-07-11 17:01:13 +000043namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000044 class CodeGenTypes;
45
Devang Patel88a981b2007-11-01 19:11:01 +000046 /// CGRecordLayout - This class handles struct and union layout info while
Devang Patelb84a06e2007-10-23 02:10:49 +000047 /// lowering AST types to LLVM types.
Devang Patel88a981b2007-11-01 19:11:01 +000048 class CGRecordLayout {
49 CGRecordLayout(); // DO NOT IMPLEMENT
Devang Patelb84a06e2007-10-23 02:10:49 +000050 public:
Devang Pateleae15602008-02-05 02:39:50 +000051 CGRecordLayout(llvm::Type *T, llvm::SmallSet<unsigned, 8> &PF)
52 : STy(T), PaddingFields(PF) {
Devang Patel057afdd2007-10-24 20:38:06 +000053 // FIXME : Collect info about fields that requires adjustments
54 // (i.e. fields that do not directly map to llvm struct fields.)
55 }
Devang Patelb84a06e2007-10-23 02:10:49 +000056
57 /// getLLVMType - Return llvm type associated with this record.
Devang Patelc4c429a2007-10-24 00:56:23 +000058 llvm::Type *getLLVMType() const {
Devang Patelb84a06e2007-10-23 02:10:49 +000059 return STy;
60 }
61
Devang Pateleae15602008-02-05 02:39:50 +000062 bool isPaddingField(unsigned No) const {
63 return PaddingFields.count(No) != 0;
64 }
65
66 unsigned getNumPaddingFields() {
67 return PaddingFields.size();
68 }
69
Devang Patelb84a06e2007-10-23 02:10:49 +000070 private:
71 llvm::Type *STy;
Devang Pateleae15602008-02-05 02:39:50 +000072 llvm::SmallSet<unsigned, 8> PaddingFields;
Devang Patelb84a06e2007-10-23 02:10:49 +000073 };
Reid Spencer5f016e22007-07-11 17:01:13 +000074
75/// CodeGenTypes - This class organizes the cross-module state that is used
76/// while lowering AST types to LLVM types.
77class CodeGenTypes {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000078 ASTContext &Context;
Reid Spencer5f016e22007-07-11 17:01:13 +000079 TargetInfo &Target;
Anders Carlsson4e533282007-08-17 22:00:32 +000080 llvm::Module& TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000081 const llvm::TargetData& TheTargetData;
Anders Carlssonc9e20912007-08-21 00:21:21 +000082
Chris Lattnerfce71b82008-04-03 05:50:42 +000083
84 llvm::SmallVector<std::pair<const PointerLikeType *,
85 llvm::OpaqueType *>, 8> PointersToResolve;
86
Chris Lattnerd86e6bc2008-02-05 08:06:13 +000087 llvm::DenseMap<const TagDecl*, llvm::PATypeHolder> TagDeclTypes;
Devang Patelb84a06e2007-10-23 02:10:49 +000088
Devang Patel88a981b2007-11-01 19:11:01 +000089 /// CGRecordLayouts - This maps llvm struct type with corresponding
Devang Patelb84a06e2007-10-23 02:10:49 +000090 /// record layout info.
Devang Patel88a981b2007-11-01 19:11:01 +000091 /// FIXME : If CGRecordLayout is less than 16 bytes then use
Devang Patel71bcb092007-10-24 00:32:16 +000092 /// inline it in the map.
Chris Lattneraf319132008-02-05 06:55:31 +000093 llvm::DenseMap<const TagDecl*, CGRecordLayout *> CGRecordLayouts;
Devang Patelb84a06e2007-10-23 02:10:49 +000094
95 /// FieldInfo - This maps struct field with corresponding llvm struct type
96 /// field no. This info is populated by record organizer.
97 llvm::DenseMap<const FieldDecl *, unsigned> FieldInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +000098 llvm::DenseMap<const ObjCIvarDecl *, unsigned> ObjCIvarInfo;
Devang Patelb84a06e2007-10-23 02:10:49 +000099
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000100public:
Devang Patel159e3302007-11-07 01:57:13 +0000101 class BitFieldInfo {
102 public:
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000103 explicit BitFieldInfo(unsigned short B, unsigned short S)
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000104 : Begin(B), Size(S) {}
105
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000106 unsigned short Begin;
107 unsigned short Size;
Devang Patel159e3302007-11-07 01:57:13 +0000108 };
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000109
110private:
Devang Patel159e3302007-11-07 01:57:13 +0000111 llvm::DenseMap<const FieldDecl *, BitFieldInfo> BitFields;
112
Chris Lattner4581fff2008-02-06 05:21:55 +0000113 /// TypeCache - This map keeps cache of llvm::Types (through PATypeHolder)
Devang Patel30ec9972007-10-25 18:32:36 +0000114 /// and maps llvm::Types to corresponding clang::Type. llvm::PATypeHolder is
115 /// used instead of llvm::Type because it allows us to bypass potential
116 /// dangling type pointers due to type refinement on llvm side.
Chris Lattner4581fff2008-02-06 05:21:55 +0000117 llvm::DenseMap<Type *, llvm::PATypeHolder> TypeCache;
Devang Patel5825ac22007-10-25 21:40:12 +0000118
119 /// ConvertNewType - Convert type T into a llvm::Type. Do not use this
120 /// method directly because it does not do any type caching. This method
121 /// is available only for ConvertType(). CovertType() is preferred
122 /// interface to convert type T into a llvm::Type.
123 const llvm::Type *ConvertNewType(QualType T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000124public:
Devang Patel7a4718e2007-10-31 20:01:01 +0000125 CodeGenTypes(ASTContext &Ctx, llvm::Module &M, const llvm::TargetData &TD);
Devang Patelb84a06e2007-10-23 02:10:49 +0000126 ~CodeGenTypes();
Reid Spencer5f016e22007-07-11 17:01:13 +0000127
Devang Pateld9e9ede2007-10-31 20:08:22 +0000128 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 TargetInfo &getTarget() const { return Target; }
Devang Patel86522b92007-10-29 20:50:19 +0000130 ASTContext &getContext() const { return Context; }
Devang Patel5825ac22007-10-25 21:40:12 +0000131
Chris Lattner4581fff2008-02-06 05:21:55 +0000132 /// ConvertType - Convert type T into a llvm::Type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 const llvm::Type *ConvertType(QualType T);
Chris Lattnerfce71b82008-04-03 05:50:42 +0000134 const llvm::Type *ConvertTypeRecursive(QualType T);
Chris Lattner41110242008-06-17 18:05:57 +0000135 /// ConvertReturnType - Convert T into an llvm::Type assuming that it will be
136 /// used as a function return type.
137 const llvm::Type *ConvertReturnType(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);
144
Chris Lattner391d77a2008-03-30 23:03:07 +0000145 void CollectObjCIvarTypes(ObjCInterfaceDecl *ObjCClass,
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000146 std::vector<const llvm::Type*> &IvarTypes);
Chris Lattner19009e62008-01-09 18:47:25 +0000147
Chris Lattneraf319132008-02-05 06:55:31 +0000148 const CGRecordLayout *getCGRecordLayout(const TagDecl*) const;
Chris Lattner41110242008-06-17 18:05:57 +0000149 /// Returns a StructType representing an Objective-C object
150 const llvm::Type *ConvertObjCInterfaceToStruct(const ObjCInterfaceDecl *OID);
Devang Patelb84a06e2007-10-23 02:10:49 +0000151
152 /// getLLVMFieldNo - Return llvm::StructType element number
153 /// that corresponds to the field FD.
154 unsigned getLLVMFieldNo(const FieldDecl *FD);
Chris Lattner391d77a2008-03-30 23:03:07 +0000155 unsigned getLLVMFieldNo(const ObjCIvarDecl *OID);
Chris Lattner19009e62008-01-09 18:47:25 +0000156
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000157
Chris Lattnerc5b88062008-02-06 05:08:19 +0000158 /// UpdateCompletedType - When we find the full definition for a TagDecl,
159 /// replace the 'opaque' type we previously made for it if applicable.
160 void UpdateCompletedType(const TagDecl *TD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000161
Chris Lattner19009e62008-01-09 18:47:25 +0000162public: // These are internal details of CGT that shouldn't be used externally.
163 void DecodeArgumentTypes(const FunctionTypeProto &FTP,
164 std::vector<const llvm::Type*> &ArgTys);
Devang Patelb84a06e2007-10-23 02:10:49 +0000165
166 /// addFieldInfo - Assign field number to field FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000167 void addFieldInfo(const FieldDecl *FD, unsigned No);
168
169 /// addBitFieldInfo - Assign a start bit and a size to field FD.
170 void addBitFieldInfo(const FieldDecl *FD, unsigned Begin, unsigned Size);
171
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000172 /// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field
173 /// FD.
Lauro Ramos Venancio2c46ce82008-01-21 22:54:57 +0000174 BitFieldInfo getBitFieldInfo(const FieldDecl *FD);
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000175
176 /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
177 /// enum.
Chris Lattner8fb1dd02008-02-06 06:06:49 +0000178 const llvm::Type *ConvertTagDeclType(const TagDecl *TD);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179};
Chris Lattnera7674d82007-07-13 22:13:22 +0000180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181} // end namespace CodeGen
182} // end namespace clang
183
184#endif