-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclass.rs
More file actions
418 lines (382 loc) · 14.3 KB
/
Copy pathclass.rs
File metadata and controls
418 lines (382 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use super::*;
pub struct ClassAssoc {
pub name: SrcNode<Ident>,
}
pub struct ClassField {
pub name: SrcNode<Ident>,
pub ty: SrcNode<TyId>,
}
pub struct Class {
pub name: SrcNode<Ident>,
pub attr: Vec<SrcNode<ast::Attr>>,
pub gen_scope: GenScopeId,
pub assoc: Option<Vec<ClassAssoc>>,
pub fields: Option<Vec<ClassField>>,
}
impl Class {
pub fn assoc_ty(&self, assoc: Ident) -> Option<()> {
self.assoc
.as_ref()
.expect("Class associated types must be known here")
.iter()
.find_map(|a| if *a.name == assoc { Some(()) } else { None })
}
pub fn field(&self, field: Ident) -> Option<&SrcNode<TyId>> {
self.fields
.as_ref()
.expect("Class fields must be known here")
.iter()
.find_map(|f| if *f.name == field { Some(&f.ty) } else { None })
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ClassId(usize);
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MemberId(pub usize);
#[derive(Default)]
pub struct Lang {
pub not: Option<ClassId>,
pub neg: Option<ClassId>,
pub add: Option<ClassId>,
pub sub: Option<ClassId>,
pub mul: Option<ClassId>,
pub div: Option<ClassId>,
pub eq: Option<ClassId>,
pub ord_ext: Option<ClassId>,
pub and: Option<ClassId>,
pub or: Option<ClassId>,
pub join: Option<ClassId>,
}
#[derive(Default)]
pub struct Classes {
lut: HashMap<Ident, (Span, ClassId)>,
classes: Vec<Class>,
members: Vec<Member>,
member_lut: HashMap<ClassId, Vec<MemberId>>,
pub lang: Lang,
}
impl Classes {
pub fn get(&self, class: ClassId) -> &Class {
&self.classes[class.0]
}
pub fn iter(&self) -> impl Iterator<Item = (ClassId, &Class)> {
self.classes
.iter()
.enumerate()
.map(|(i, class)| (ClassId(i), class))
}
pub fn iter_members(&self) -> impl Iterator<Item = (MemberId, &Member)> {
self.members
.iter()
.enumerate()
.map(|(i, member)| (MemberId(i), member))
}
pub fn class_gen_scope(&self, class: ClassId) -> GenScopeId {
self.classes[class.0].gen_scope
}
pub fn lookup(&self, name: Ident) -> Option<ClassId> {
self.lut.get(&name).map(|(_, id)| *id)
}
pub fn declare(&mut self, name: SrcNode<Ident>, class: Class) -> Result<ClassId, Error> {
let id = ClassId(self.classes.len());
let span = name.span();
if let Err(old) = self.lut.try_insert(*name, (span, id)) {
Err(Error::DuplicateClassName(*name, old.entry.get().0, span))
} else {
if let Some(lang) = class
.attr
.iter()
.find(|a| &**a.name == "lang")
.and_then(|a| a.args.as_ref())
{
if lang.iter().any(|a| &**a.name == "not") {
self.lang.not = Some(id);
}
if lang.iter().any(|a| &**a.name == "neg") {
self.lang.neg = Some(id);
}
if lang.iter().any(|a| &**a.name == "add") {
self.lang.add = Some(id);
}
if lang.iter().any(|a| &**a.name == "sub") {
self.lang.sub = Some(id);
}
if lang.iter().any(|a| &**a.name == "mul") {
self.lang.mul = Some(id);
}
if lang.iter().any(|a| &**a.name == "div") {
self.lang.div = Some(id);
}
if lang.iter().any(|a| &**a.name == "eq") {
self.lang.eq = Some(id);
}
if lang.iter().any(|a| &**a.name == "ord_ext") {
self.lang.ord_ext = Some(id);
}
if lang.iter().any(|a| &**a.name == "and_") {
self.lang.and = Some(id);
}
if lang.iter().any(|a| &**a.name == "or_") {
self.lang.or = Some(id);
}
if lang.iter().any(|a| &**a.name == "join") {
self.lang.join = Some(id);
}
}
self.classes.push(class);
Ok(id)
}
}
pub fn check_lang_items(&self) -> Vec<Error> {
let mut errors = Vec::new();
if self.lang.not.is_none() {
errors.push(Error::MissingLangItem("not"));
}
if self.lang.neg.is_none() {
errors.push(Error::MissingLangItem("neg"));
}
if self.lang.add.is_none() {
errors.push(Error::MissingLangItem("add"));
}
if self.lang.sub.is_none() {
errors.push(Error::MissingLangItem("sub"));
}
if self.lang.mul.is_none() {
errors.push(Error::MissingLangItem("mul"));
}
if self.lang.div.is_none() {
errors.push(Error::MissingLangItem("div"));
}
if self.lang.eq.is_none() {
errors.push(Error::MissingLangItem("eq"));
}
if self.lang.ord_ext.is_none() {
errors.push(Error::MissingLangItem("ord_ext"));
}
if self.lang.and.is_none() {
errors.push(Error::MissingLangItem("and_"));
}
if self.lang.or.is_none() {
errors.push(Error::MissingLangItem("or_"));
}
if self.lang.join.is_none() {
errors.push(Error::MissingLangItem("join"));
}
errors
}
pub fn define_assoc(&mut self, id: ClassId, assoc: Vec<ClassAssoc>) {
self.classes[id.0].assoc = Some(assoc);
}
pub fn define_fields(&mut self, id: ClassId, fields: Vec<ClassField>) {
self.classes[id.0].fields = Some(fields);
}
pub fn get_member(&self, id: MemberId) -> &Member {
&self.members[id.0]
}
// TODO: Pre-insert member here so we can do inference inside members themselves
pub fn declare_member(&mut self, class: ClassId, member: Member) -> MemberId {
let id = MemberId(self.members.len());
self.members.push(member);
self.member_lut.entry(class).or_default().push(id);
id
}
pub fn define_member_assoc(
&mut self,
id: MemberId,
_class: ClassId,
assoc: HashMap<Ident, TyId>,
) {
self.members[id.0].assoc = Some(assoc);
}
pub fn define_member_fields(
&mut self,
id: MemberId,
_class: ClassId,
fields: HashMap<Ident, TyExpr>,
) {
self.members[id.0].fields = Some(fields);
}
pub fn lookup_member(
&self,
hir: &Context,
ctx: &ConContext,
ty: ConTyId,
(class, gen_tys, gen_effs): (ClassId, Vec<ConTyId>, Vec<Vec<ConEffectId>>),
) -> Option<MemberId> {
// Returns true if member covers ty
fn covers(
hir: &Context,
ctx: &ConContext,
member: TyId,
ty: ConTyId,
gen_ty_links: &mut HashMap<usize, ConTyId>,
) -> bool {
match (hir.tys.get(member), ctx.get_ty(ty)) {
(Ty::Gen(idx, _), _) => *gen_ty_links.entry(idx).or_insert(ty) == ty,
(Ty::Prim(a), ConTy::Prim(b)) if a == *b => true,
(Ty::List(x), ConTy::List(y)) => {
covers(hir, ctx, x, *y, gen_ty_links)
}
// TODO: Care about field names!
(Ty::Record(xs, _), ConTy::Record(ys)) if xs.len() == ys.len() => xs
.into_iter()
.zip(ys.iter())
.all(|((_, x), (_, y))| covers(hir, ctx, x, *y, gen_ty_links)),
(Ty::Func(x_i, x_o), ConTy::Func(y_i, y_o)) => {
covers(hir, ctx, x_i, *y_i, gen_ty_links)
&& covers(hir, ctx, x_o, *y_o, gen_ty_links)
}
(Ty::Data(x, xs), ConTy::Data(y)) if x == y.0 .0 && xs.len() == y.0 .1.len() => xs
.into_iter()
.zip(y.0 .1.iter())
.all(|(x, y)| covers(hir, ctx, x, *y, gen_ty_links)),
// Flatten empty effects
(_, ConTy::Effect(eff, ty)) if eff.is_empty() => {
covers(hir, ctx, member, *ty, gen_ty_links)
}
(Ty::Effect(_, _), ConTy::Effect(_, _)) => todo!(),
(Ty::Error(_), _) => panic!("Error ty during monomorphisation"),
_ => false,
}
}
fn covers_eff(
hir: &Context,
_ctx: &ConContext,
member: EffectId,
effs: &[ConEffectId],
_gen_ty_links: &mut HashMap<usize, ConTyId>,
_gen_eff_links: &mut HashMap<usize, ConEffectId>,
) -> bool {
match (hir.tys.get_effect(member), effs) {
(Effect::Known(member_effs), effs) if member_effs.len() <= 1 || effs.len() <= 1 => effs
.iter()
.all(|eff| member_effs
.iter()
.any(|member_eff| match (member_eff, eff) {
(Ok(EffectInst::Gen(_idx, _)), _eff) => {
// println!("Compare {:?} with {:?}", gen_eff_links.get(idx), eff);
// gen_eff_links.entry(*idx).or_insert(*eff) == eff
// TODO: Is this correct? Can all generics be assumed to match?!
true
},
(x, y) => todo!("You know, you should really impl effect-polymorphic class monomorphisation: {:?} covers {:?}", x, y),
})),
(Effect::Error, _) => panic!("Error eff during monomorphisation"),
(x, y) => todo!("{:?} covers {:?}", x, y),
}
}
self.member_lut.get(&class).and_then(|xs| {
let candidates =
xs.iter()
.filter(|m| {
let member = self.get_member(**m);
let mut gen_ty_links = HashMap::new();
let mut gen_eff_links = HashMap::new();
covers(
hir,
ctx,
member.member,
ty,
&mut gen_ty_links,
) && member.gen_tys.iter().zip(gen_tys.iter()).all(
|(member_gen_ty, gen_ty)| {
covers(
hir,
ctx,
*member_gen_ty,
*gen_ty,
&mut gen_ty_links,
)
},
) && member.gen_effs.iter().zip(gen_effs.iter()).all(
|(member_gen_eff, gen_eff)| {
covers_eff(
hir,
ctx,
member_gen_eff.expect("Error eff during monomorphisation"),
gen_eff,
&mut gen_ty_links,
&mut gen_eff_links,
)
},
)
})
.collect::<Vec<_>>();
assert!(
candidates.len() <= 1,
"Multiple member candidates detected during lowering <{:?} as {:?}>.\n\
This means that incoherence has occurred!\n\
Candidate members:\n{}",
ctx.get_ty(ty),
**self.get(class).name,
candidates
.iter()
.map(|c| {
let c = self.get_member(**c);
let gen_scope = hir.tys.get_gen_scope(c.gen_scope);
format!(
"- {}member {} of {}{} (in {})\n",
if gen_scope.is_empty() {
String::new()
} else {
format!(
"for {} ",
(0..gen_scope.len())
.map(|idx| format!("{}", *gen_scope.get(idx).name))
.collect::<Vec<_>>()
.join(", ")
)
},
hir.tys.display(hir, c.member),
**self.get(class).name,
c.gen_tys
.iter()
.map(|ty| format!(" {}", hir.tys.display(hir, *ty)))
.collect::<String>(),
hir.tys.get_span(c.member).src(),
)
})
.collect::<Vec<_>>()
.join("")
);
candidates.first().copied().copied()
})
}
pub fn members_of(&self, class: ClassId) -> impl Iterator<Item = (MemberId, &Member)> {
self.member_lut
.get(&class)
.map(|m| m.as_slice())
.unwrap_or(&[])
.iter()
.map(|m| (*m, self.get_member(*m)))
}
}
pub enum MemberItem {
Value { name: SrcNode<Ident>, val: TyExpr },
Type { name: SrcNode<Ident>, ty: TyId },
}
pub struct Member {
pub gen_scope: GenScopeId,
pub attr: Vec<SrcNode<ast::Attr>>,
pub member: TyId,
pub class: ClassId,
pub gen_tys: Vec<TyId>,
pub gen_effs: Vec<Option<EffectId>>,
pub assoc: Option<HashMap<Ident, TyId>>,
pub fields: Option<HashMap<Ident, TyExpr>>,
}
impl Member {
pub fn assoc_ty(&self, assoc: Ident) -> Option<TyId> {
self.assoc
.as_ref()
.expect("Member associated types not initialised")
.get(&assoc)
.copied()
}
pub fn field(&self, field: Ident) -> Option<&TyExpr> {
self.fields
.as_ref()
.expect("Member fields not initialised")
.get(&field)
}
}