-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathresolve.rs
1426 lines (1338 loc) · 55.6 KB
/
resolve.rs
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use super::{Error, ParamList, ResultList, WorldOrInterface};
use crate::ast::toposort::toposort;
use crate::*;
use anyhow::{bail, Result};
use indexmap::IndexMap;
use std::collections::{HashMap, HashSet};
use std::mem;
#[derive(Default)]
pub struct Resolver<'a> {
/// Current package name learned through the ASTs pushed onto this resolver.
package_name: Option<PackageName>,
/// Package docs.
package_docs: Docs,
/// All WIT files which are going to be resolved together.
asts: Vec<ast::Ast<'a>>,
// Arenas that get plumbed to the final `UnresolvedPackage`
types: Arena<TypeDef>,
interfaces: Arena<Interface>,
worlds: Arena<World>,
// Interning structure for types which-need-not-be-named such as
// `list<string>` and such.
anon_types: HashMap<Key, TypeId>,
/// The index within `self.ast_items` that lookups should go through. This
/// is updated as the ASTs are walked.
cur_ast_index: usize,
/// A map per `ast::Ast` which keeps track of the file's top level names in
/// scope. This maps each name onto either a world or an interface, handling
/// things like `use` at the top level.
ast_items: Vec<IndexMap<&'a str, AstItem>>,
/// A map for the entire package being created of all names defined within,
/// along with the ID they're mapping to.
package_items: IndexMap<&'a str, AstItem>,
/// A per-interface map of name to item-in-the-interface. This is the same
/// length as `self.types` and is pushed to whenever `self.types` is pushed
/// to.
interface_types: Vec<IndexMap<&'a str, (TypeOrItem, Span)>>,
/// Metadata about foreign dependencies which are not defined in this
/// package. This map is keyed by the name of the package being imported
/// from. The next level of key is the name of the interface being imported
/// from, and the final value is the assigned ID of the interface.
foreign_deps: IndexMap<PackageName, IndexMap<&'a str, AstItem>>,
/// All interfaces that are present within `self.foreign_deps`.
foreign_interfaces: HashSet<InterfaceId>,
foreign_worlds: HashSet<WorldId>,
/// The current type lookup scope which will eventually make its way into
/// `self.interface_types`.
type_lookup: IndexMap<&'a str, (TypeOrItem, Span)>,
/// An assigned span for where all types inserted into `self.types` as
/// imported from foreign interfaces. These types all show up first in the
/// `self.types` arena and this span is used to generate an error message
/// pointing to it if the item isn't actually defined.
unknown_type_spans: Vec<Span>,
/// Spans for each world in `self.world` to assign for each import/export
/// for later error reporting.
world_item_spans: Vec<(Vec<Span>, Vec<Span>)>,
/// Spans for each world in `self.world`
world_spans: Vec<Span>,
/// The span of each interface's definition which is used for error
/// reporting during the final `Resolve` phase.
interface_spans: Vec<Span>,
/// Spans per entry in `self.foreign_deps` for where the dependency was
/// introduced to print an error message if necessary.
foreign_dep_spans: Vec<Span>,
include_world_spans: Vec<Span>,
/// A list of `TypeDefKind::Unknown` types which are required to be
/// resources when this package is resolved against its dependencies.
required_resource_types: Vec<(TypeId, Span)>,
}
#[derive(PartialEq, Eq, Hash)]
enum Key {
Variant(Vec<(String, Option<Type>)>),
BorrowHandle(TypeId),
Record(Vec<(String, Type)>),
Flags(Vec<String>),
Tuple(Vec<Type>),
Enum(Vec<String>),
List(Type),
Option(Type),
Result(Option<Type>, Option<Type>),
Future(Option<Type>),
Stream(Option<Type>, Option<Type>),
}
enum TypeItem<'a, 'b> {
Use(&'b ast::Use<'a>),
Def(&'b ast::TypeDef<'a>),
}
enum TypeOrItem {
Type(TypeId),
Item(&'static str),
}
impl<'a> Resolver<'a> {
pub(crate) fn push(&mut self, ast: ast::Ast<'a>) -> Result<()> {
// As each WIT file is pushed into this resolver keep track of the
// current package name assigned. Only one file needs to mention it, but
// if multiple mention it then they must all match.
if let Some(cur) = &ast.package_id {
let cur_name = cur.package_name();
if let Some(prev) = &self.package_name {
if cur_name != *prev {
bail!(Error {
span: cur.span,
msg: format!(
"package identifier `{cur_name}` does not match \
previous package name of `{prev}`"
),
})
}
}
self.package_name = Some(cur_name);
// At most one 'package' item can have doc comments.
let docs = self.docs(&cur.docs);
if docs.contents.is_some() {
if self.package_docs.contents.is_some() {
bail!(Error {
span: cur.docs.span,
msg: "found doc comments on multiple 'package' items".into(),
})
}
self.package_docs = docs;
}
}
self.asts.push(ast);
Ok(())
}
pub(crate) fn resolve(&mut self) -> Result<UnresolvedPackage> {
// At least one of the WIT files must have a `package` annotation.
let name = match &self.package_name {
Some(name) => name.clone(),
None => bail!("no `package` header was found in any WIT file for this package"),
};
// First populate information about foreign dependencies and the general
// structure of the package. This should resolve the "base" of many
// `use` statements and additionally generate a topological ordering of
// all interfaces in the package to visit.
let asts = mem::take(&mut self.asts);
self.populate_foreign_deps(&asts);
let (iface_order, world_order) = self.populate_ast_items(&asts)?;
self.populate_foreign_types(&asts)?;
// Use the topological ordering of all interfaces to resolve all
// interfaces in-order. Note that a reverse-mapping from ID to AST is
// generated here to assist with this.
let mut iface_id_to_ast = IndexMap::new();
let mut world_id_to_ast = IndexMap::new();
for (i, ast) in asts.iter().enumerate() {
for item in ast.items.iter() {
match item {
ast::AstItem::Interface(iface) => {
let id = match self.ast_items[i][iface.name.name] {
AstItem::Interface(id) => id,
AstItem::World(_) => unreachable!(),
};
iface_id_to_ast.insert(id, (iface, i));
}
ast::AstItem::World(world) => {
let id = match self.ast_items[i][world.name.name] {
AstItem::World(id) => id,
AstItem::Interface(_) => unreachable!(),
};
world_id_to_ast.insert(id, (world, i));
}
ast::AstItem::Use(_) => {}
}
}
}
for id in iface_order {
let (interface, i) = &iface_id_to_ast[&id];
self.cur_ast_index = *i;
self.resolve_interface(id, &interface.items, &interface.docs)?;
}
for id in world_order {
let (world, i) = &world_id_to_ast[&id];
self.cur_ast_index = *i;
self.resolve_world(id, world)?;
}
Ok(UnresolvedPackage {
name,
docs: mem::take(&mut self.package_docs),
worlds: mem::take(&mut self.worlds),
types: mem::take(&mut self.types),
interfaces: mem::take(&mut self.interfaces),
foreign_deps: self
.foreign_deps
.iter()
.map(|(name, deps)| {
(
name.clone(),
deps.iter()
.map(|(name, id)| (name.to_string(), *id))
.collect(),
)
})
.collect(),
unknown_type_spans: mem::take(&mut self.unknown_type_spans),
world_item_spans: mem::take(&mut self.world_item_spans),
interface_spans: mem::take(&mut self.interface_spans),
world_spans: mem::take(&mut self.world_spans),
foreign_dep_spans: mem::take(&mut self.foreign_dep_spans),
source_map: SourceMap::default(),
include_world_spans: mem::take(&mut self.include_world_spans),
required_resource_types: mem::take(&mut self.required_resource_types),
})
}
/// Registers all foreign dependencies made within the ASTs provided.
///
/// This will populate the `self.foreign_{deps,interfaces,worlds}` maps with all
/// `UsePath::Package` entries.
fn populate_foreign_deps(&mut self, asts: &[ast::Ast<'a>]) {
let mut foreign_deps = mem::take(&mut self.foreign_deps);
let mut foreign_interfaces = mem::take(&mut self.foreign_interfaces);
let mut foreign_worlds = mem::take(&mut self.foreign_worlds);
for ast in asts {
ast.for_each_path(|_, path, _names, world_or_iface| {
let (id, name) = match path {
ast::UsePath::Package { id, name } => (id, name),
_ => return Ok(()),
};
let deps = foreign_deps.entry(id.package_name()).or_insert_with(|| {
self.foreign_dep_spans.push(id.span);
IndexMap::new()
});
let id = *deps.entry(name.name).or_insert_with(|| {
match world_or_iface {
WorldOrInterface::World => {
log::trace!(
"creating a world for foreign dep: {}/{}",
id.package_name(),
name.name
);
AstItem::World(self.alloc_world(name.span, true))
}
WorldOrInterface::Interface | WorldOrInterface::Unknown => {
// Currently top-level `use` always assumes an interface, so the
// `Unknown` case is the same as `Interface`.
log::trace!(
"creating an interface for foreign dep: {}/{}",
id.package_name(),
name.name
);
AstItem::Interface(self.alloc_interface(name.span))
}
}
});
let _ = match id {
AstItem::Interface(id) => foreign_interfaces.insert(id),
AstItem::World(id) => foreign_worlds.insert(id),
};
Ok(())
})
.unwrap();
}
self.foreign_deps = foreign_deps;
self.foreign_interfaces = foreign_interfaces;
self.foreign_worlds = foreign_worlds;
}
fn alloc_interface(&mut self, span: Span) -> InterfaceId {
self.interface_types.push(IndexMap::new());
self.interface_spans.push(span);
self.interfaces.alloc(Interface {
name: None,
types: IndexMap::new(),
docs: Docs::default(),
functions: IndexMap::new(),
package: None,
})
}
fn alloc_world(&mut self, span: Span, dummy_span: bool) -> WorldId {
self.world_spans.push(span);
if dummy_span {
self.world_item_spans.push((Vec::new(), Vec::new()));
}
self.worlds.alloc(World {
name: String::new(),
docs: Docs::default(),
exports: IndexMap::new(),
imports: IndexMap::new(),
package: None,
includes: Default::default(),
include_names: Default::default(),
})
}
/// This method will create a `World` and an `Interface` for all items
/// present in the specified set of ASTs. Additionally maps for each AST are
/// generated for resolving use-paths later on.
fn populate_ast_items(
&mut self,
asts: &[ast::Ast<'a>],
) -> Result<(Vec<InterfaceId>, Vec<WorldId>)> {
let mut package_items = IndexMap::new();
// Validate that all worlds and interfaces have unique names within this
// package across all ASTs which make up the package.
let mut names = HashMap::new();
let mut ast_namespaces = Vec::new();
let mut order = IndexMap::new();
for ast in asts {
let mut ast_ns = IndexMap::new();
for item in ast.items.iter() {
match item {
ast::AstItem::Interface(i) => {
if package_items.insert(i.name.name, i.name.span).is_some() {
bail!(Error {
span: i.name.span,
msg: format!("duplicate item named `{}`", i.name.name),
})
}
let prev = ast_ns.insert(i.name.name, ());
assert!(prev.is_none());
let prev = order.insert(i.name.name, Vec::new());
assert!(prev.is_none());
let prev = names.insert(i.name.name, item);
assert!(prev.is_none());
}
ast::AstItem::World(w) => {
if package_items.insert(w.name.name, w.name.span).is_some() {
bail!(Error {
span: w.name.span,
msg: format!("duplicate item named `{}`", w.name.name),
})
}
let prev = ast_ns.insert(w.name.name, ());
assert!(prev.is_none());
let prev = order.insert(w.name.name, Vec::new());
assert!(prev.is_none());
let prev = names.insert(w.name.name, item);
assert!(prev.is_none());
}
// These are processed down below.
ast::AstItem::Use(_) => {}
}
}
ast_namespaces.push(ast_ns);
}
// Next record dependencies between interfaces as induced via `use`
// paths. This step is used to perform a topological sort of all
// interfaces to ensure there are no cycles and to generate an ordering
// which we can resolve in.
enum ItemSource<'a> {
Foreign,
Local(ast::Id<'a>),
}
for ast in asts {
// Record, in the context of this file, what all names are defined
// at the top level and whether they point to other items in this
// package or foreign items. Foreign deps are ignored for
// topological ordering.
let mut ast_ns = IndexMap::new();
for item in ast.items.iter() {
let (name, src) = match item {
ast::AstItem::Use(u) => {
let name = u.as_.as_ref().unwrap_or(u.item.name());
let src = match &u.item {
ast::UsePath::Id(id) => ItemSource::Local(id.clone()),
ast::UsePath::Package { .. } => ItemSource::Foreign,
};
(name, src)
}
ast::AstItem::Interface(i) => (&i.name, ItemSource::Local(i.name.clone())),
ast::AstItem::World(w) => (&w.name, ItemSource::Local(w.name.clone())),
};
if ast_ns.insert(name.name, (name.span, src)).is_some() {
bail!(Error {
span: name.span,
msg: format!("duplicate name `{}` in this file", name.name),
});
}
}
// With this file's namespace information look at all `use` paths
// and record dependencies between interfaces.
ast.for_each_path(|iface, path, _names, _| {
// If this import isn't contained within an interface then it's
// in a world and it doesn't need to participate in our
// topo-sort.
let iface = match iface {
Some(name) => name,
None => return Ok(()),
};
let used_name = match path {
ast::UsePath::Id(id) => id,
ast::UsePath::Package { .. } => return Ok(()),
};
match ast_ns.get(used_name.name) {
Some((_, ItemSource::Foreign)) => return Ok(()),
Some((_, ItemSource::Local(id))) => {
order[iface.name].push(id.clone());
}
None => match package_items.get(used_name.name) {
Some(_) => {
order[iface.name].push(used_name.clone());
}
None => {
bail!(Error {
span: used_name.span,
msg: format!(
"interface or world `{name}` not found in package",
name = used_name.name
),
})
}
},
}
Ok(())
})?;
}
let order = toposort("interface or world", &order)?;
log::debug!("toposort for interfaces and worlds in order: {:?}", order);
// Allocate interfaces in-order now that the ordering is defined. This
// is then used to build up internal maps for each AST which are stored
// in `self.ast_items`.
let mut ids = IndexMap::new();
let mut iface_id_order = Vec::new();
let mut world_id_order = Vec::new();
for name in order {
match names.get(name).unwrap() {
ast::AstItem::Interface(_) => {
let id = self.alloc_interface(package_items[name]);
self.interfaces[id].name = Some(name.to_string());
let prev = ids.insert(name, AstItem::Interface(id));
assert!(prev.is_none());
iface_id_order.push(id);
}
ast::AstItem::World(_) => {
// No dummy span needs to be created because they will be created at `resolve_world`
let id = self.alloc_world(package_items[name], false);
self.worlds[id].name = name.to_string();
let prev = ids.insert(name, AstItem::World(id));
assert!(prev.is_none());
world_id_order.push(id);
}
ast::AstItem::Use(_) => unreachable!(),
};
}
for ast in asts {
let mut items = IndexMap::new();
for item in ast.items.iter() {
let (name, ast_item) = match item {
ast::AstItem::Use(u) => {
let name = u.as_.as_ref().unwrap_or(u.item.name());
let item = match &u.item {
ast::UsePath::Id(name) => *ids.get(name.name).ok_or_else(|| Error {
span: name.span,
msg: format!(
"interface or world `{name}` does not exist",
name = name.name
),
})?,
ast::UsePath::Package { id, name } => {
self.foreign_deps[&id.package_name()][name.name]
}
};
(name.name, item)
}
ast::AstItem::Interface(i) => {
let iface_item = ids[i.name.name];
assert!(matches!(iface_item, AstItem::Interface(_)));
(i.name.name, iface_item)
}
ast::AstItem::World(w) => {
let world_item = ids[w.name.name];
assert!(matches!(world_item, AstItem::World(_)));
(w.name.name, world_item)
}
};
let prev = items.insert(name, ast_item);
assert!(prev.is_none());
// Items defined via `use` don't go into the package namespace,
// only the file namespace.
if !matches!(item, ast::AstItem::Use(_)) {
let prev = self.package_items.insert(name, ast_item);
assert!(prev.is_none());
}
}
self.ast_items.push(items);
}
Ok((iface_id_order, world_id_order))
}
/// Generate a `Type::Unknown` entry for all types imported from foreign
/// packages.
///
/// This is done after all interfaces are generated so `self.resolve_path`
/// can be used to determine if what's being imported from is a foreign
/// interface or not.
fn populate_foreign_types(&mut self, asts: &[ast::Ast<'a>]) -> Result<()> {
for (i, ast) in asts.iter().enumerate() {
self.cur_ast_index = i;
ast.for_each_path(|_, path, names, _| {
let names = match names {
Some(names) => names,
None => return Ok(()),
};
let (item, name, span) = self.resolve_ast_item_path(path)?;
let iface = self.extract_iface_from_item(&item, &name, span)?;
if !self.foreign_interfaces.contains(&iface) {
return Ok(());
}
let lookup = &mut self.interface_types[iface.index()];
for name in names {
// If this name has already been defined then use that prior
// definition, otherwise create a new type with an unknown
// representation and insert it into the various maps.
if lookup.contains_key(name.name.name) {
continue;
}
let id = self.types.alloc(TypeDef {
docs: Docs::default(),
kind: TypeDefKind::Unknown,
name: Some(name.name.name.to_string()),
owner: TypeOwner::Interface(iface),
});
self.unknown_type_spans.push(name.name.span);
lookup.insert(name.name.name, (TypeOrItem::Type(id), name.name.span));
self.interfaces[iface]
.types
.insert(name.name.name.to_string(), id);
}
Ok(())
})?;
}
Ok(())
}
fn resolve_world(&mut self, world_id: WorldId, world: &ast::World<'a>) -> Result<WorldId> {
let docs = self.docs(&world.docs);
self.worlds[world_id].docs = docs;
self.resolve_types(
TypeOwner::World(world_id),
world.items.iter().filter_map(|i| match i {
ast::WorldItem::Use(u) => Some(TypeItem::Use(u)),
ast::WorldItem::Type(t) => Some(TypeItem::Def(t)),
ast::WorldItem::Import(_) | ast::WorldItem::Export(_) => None,
// should be handled in `wit-parser::resolve`
ast::WorldItem::Include(_) => None,
}),
)?;
// resolve include items
let items = world.items.iter().filter_map(|i| match i {
ast::WorldItem::Include(i) => Some(i),
_ => None,
});
for include in items {
self.resolve_include(TypeOwner::World(world_id), include)?;
}
let mut export_spans = Vec::new();
let mut import_spans = Vec::new();
let mut used_names = HashMap::new();
for (name, (item, span)) in self.type_lookup.iter() {
match *item {
TypeOrItem::Type(id) => {
let prev = used_names.insert(*name, "import");
if let Some(prev) = prev {
return Err(Error {
span: *span,
msg: format!(
"import `{name}` conflicts with prior {prev} of same name",
),
}
.into());
}
self.worlds[world_id]
.imports
.insert(WorldKey::Name(name.to_string()), WorldItem::Type(id));
import_spans.push(*span);
}
TypeOrItem::Item(_) => unreachable!(),
}
}
let mut imported_interfaces = HashSet::new();
let mut exported_interfaces = HashSet::new();
for item in world.items.iter() {
let (docs, kind, desc, spans, interfaces) = match item {
ast::WorldItem::Import(import) => (
&import.docs,
&import.kind,
"import",
&mut import_spans,
&mut imported_interfaces,
),
ast::WorldItem::Export(export) => (
&export.docs,
&export.kind,
"export",
&mut export_spans,
&mut exported_interfaces,
),
ast::WorldItem::Type(ast::TypeDef {
name,
ty: ast::Type::Resource(r),
..
}) => {
for func in r.funcs.iter() {
import_spans.push(func.named_func().name.span);
let func = self.resolve_resource_func(func, name)?;
let prev = self.worlds[world_id]
.imports
.insert(WorldKey::Name(func.name.clone()), WorldItem::Function(func));
// Resource names themselves are unique, and methods are
// uniquely named, so this should be possible to assert
// at this point and never trip.
assert!(prev.is_none());
}
continue;
}
// handled in `resolve_types`
ast::WorldItem::Use(_) | ast::WorldItem::Type(_) | ast::WorldItem::Include(_) => {
continue
}
};
let key = match kind {
ast::ExternKind::Interface(name, _) | ast::ExternKind::Func(name, _) => {
let prev = used_names.insert(name.name, desc);
if let Some(prev) = prev {
return Err(Error {
span: kind.span(),
msg: format!(
"{desc} `{name}` conflicts with prior {prev} of same name",
name = name.name
),
}
.into());
}
WorldKey::Name(name.name.to_string())
}
ast::ExternKind::Path(path) => {
let (item, name, span) = self.resolve_ast_item_path(path)?;
let id = self.extract_iface_from_item(&item, &name, span)?;
WorldKey::Interface(id)
}
};
let world_item = self.resolve_world_item(docs, kind)?;
if let WorldItem::Interface(id) = world_item {
if !interfaces.insert(id) {
bail!(Error {
span: kind.span(),
msg: format!("interface cannot be {desc}ed more than once"),
})
}
}
let dst = if desc == "import" {
&mut self.worlds[world_id].imports
} else {
&mut self.worlds[world_id].exports
};
let prev = dst.insert(key, world_item);
assert!(prev.is_none());
spans.push(kind.span());
}
self.world_item_spans.push((import_spans, export_spans));
self.type_lookup.clear();
Ok(world_id)
}
fn resolve_world_item(
&mut self,
docs: &ast::Docs<'a>,
kind: &ast::ExternKind<'a>,
) -> Result<WorldItem> {
match kind {
ast::ExternKind::Interface(name, items) => {
let prev = mem::take(&mut self.type_lookup);
let id = self.alloc_interface(name.span);
self.resolve_interface(id, items, docs)?;
self.type_lookup = prev;
Ok(WorldItem::Interface(id))
}
ast::ExternKind::Path(path) => {
let (item, name, span) = self.resolve_ast_item_path(path)?;
let id = self.extract_iface_from_item(&item, &name, span)?;
Ok(WorldItem::Interface(id))
}
ast::ExternKind::Func(name, func) => {
let func =
self.resolve_function(docs, name.name, func, FunctionKind::Freestanding)?;
Ok(WorldItem::Function(func))
}
}
}
fn resolve_interface(
&mut self,
interface_id: InterfaceId,
fields: &[ast::InterfaceItem<'a>],
docs: &ast::Docs<'a>,
) -> Result<()> {
let docs = self.docs(docs);
self.interfaces[interface_id].docs = docs;
self.resolve_types(
TypeOwner::Interface(interface_id),
fields.iter().filter_map(|i| match i {
ast::InterfaceItem::Use(u) => Some(TypeItem::Use(u)),
ast::InterfaceItem::TypeDef(t) => Some(TypeItem::Def(t)),
ast::InterfaceItem::Func(_) => None,
}),
)?;
for (name, (ty, _)) in self.type_lookup.iter() {
match *ty {
TypeOrItem::Type(id) => {
self.interfaces[interface_id]
.types
.insert(name.to_string(), id);
}
TypeOrItem::Item(_) => unreachable!(),
}
}
// Finally process all function definitions now that all types are
// defined.
let mut funcs = Vec::new();
for field in fields {
match field {
ast::InterfaceItem::Func(f) => {
self.define_interface_name(&f.name, TypeOrItem::Item("function"))?;
funcs.push(self.resolve_function(
&f.docs,
&f.name.name,
&f.func,
FunctionKind::Freestanding,
)?);
}
ast::InterfaceItem::Use(_) => {}
ast::InterfaceItem::TypeDef(ast::TypeDef {
name,
ty: ast::Type::Resource(r),
..
}) => {
for func in r.funcs.iter() {
funcs.push(self.resolve_resource_func(func, name)?);
}
}
ast::InterfaceItem::TypeDef(_) => {}
}
}
for func in funcs {
let prev = self.interfaces[interface_id]
.functions
.insert(func.name.clone(), func);
assert!(prev.is_none());
}
let lookup = mem::take(&mut self.type_lookup);
self.interface_types[interface_id.index()] = lookup;
Ok(())
}
fn resolve_types<'b>(
&mut self,
owner: TypeOwner,
fields: impl Iterator<Item = TypeItem<'a, 'b>> + Clone,
) -> Result<()>
where
'a: 'b,
{
assert!(self.type_lookup.is_empty());
// First, populate our namespace with `use` statements
for field in fields.clone() {
match field {
TypeItem::Use(u) => {
self.resolve_use(owner, u)?;
}
TypeItem::Def(_) => {}
}
}
// Next determine dependencies between types, perform a topological
// sort, and then define all types. This will define types in a
// topological fashion, forbid cycles, and weed out references to
// undefined types all in one go.
let mut type_deps = IndexMap::new();
let mut type_defs = IndexMap::new();
for field in fields {
match field {
TypeItem::Def(t) => {
let prev = type_defs.insert(t.name.name, Some(t));
if prev.is_some() {
return Err(Error {
span: t.name.span,
msg: format!("name `{}` is defined more than once", t.name.name),
}
.into());
}
let mut deps = Vec::new();
collect_deps(&t.ty, &mut deps);
type_deps.insert(t.name.name, deps);
}
TypeItem::Use(u) => {
for name in u.names.iter() {
let name = name.as_.as_ref().unwrap_or(&name.name);
type_deps.insert(name.name, Vec::new());
type_defs.insert(name.name, None);
}
}
}
}
let order = toposort("type", &type_deps)?;
for ty in order {
let def = match type_defs.remove(&ty).unwrap() {
Some(def) => def,
None => continue,
};
let docs = self.docs(&def.docs);
let kind = self.resolve_type_def(&def.ty)?;
let id = self.types.alloc(TypeDef {
docs,
kind,
name: Some(def.name.name.to_string()),
owner,
});
self.define_interface_name(&def.name, TypeOrItem::Type(id))?;
}
Ok(())
}
fn resolve_use(&mut self, owner: TypeOwner, u: &ast::Use<'a>) -> Result<()> {
let (item, name, span) = self.resolve_ast_item_path(&u.from)?;
let use_from = self.extract_iface_from_item(&item, &name, span)?;
for name in u.names.iter() {
let lookup = &self.interface_types[use_from.index()];
let id = match lookup.get(name.name.name) {
Some((TypeOrItem::Type(id), _)) => *id,
Some((TypeOrItem::Item(s), _)) => {
bail!(Error {
span: name.name.span,
msg: format!("cannot import {s} `{}`", name.name.name),
})
}
None => bail!(Error {
span: name.name.span,
msg: format!("name `{}` is not defined", name.name.name),
}),
};
let name = name.as_.as_ref().unwrap_or(&name.name);
let id = self.types.alloc(TypeDef {
docs: Docs::default(),
kind: TypeDefKind::Type(Type::Id(id)),
name: Some(name.name.to_string()),
owner,
});
self.define_interface_name(name, TypeOrItem::Type(id))?;
}
Ok(())
}
/// For each name in the `include`, resolve the path of the include, add it to the self.includes
fn resolve_include(&mut self, owner: TypeOwner, i: &ast::Include<'a>) -> Result<()> {
let (item, name, span) = self.resolve_ast_item_path(&i.from)?;
let include_from = self.extract_world_from_item(&item, &name, span)?;
self.include_world_spans.push(span);
let world_id = match owner {
TypeOwner::World(id) => id,
_ => unreachable!(),
};
self.worlds[world_id].includes.push(include_from);
self.worlds[world_id].include_names.push(
i.names
.iter()
.map(|n| IncludeName {
name: n.name.name.to_string(),
as_: n.as_.name.to_string(),
})
.collect(),
);
Ok(())
}
fn resolve_resource_func(
&mut self,
func: &ast::ResourceFunc<'_>,
resource: &ast::Id<'_>,
) -> Result<Function> {
let resource_id = match self.type_lookup.get(resource.name) {
Some((TypeOrItem::Type(id), _)) => *id,
_ => panic!("type lookup for resource failed"),
};
let (name, kind);
match func {
ast::ResourceFunc::Method(f) => {
name = format!("[method]{}.{}", resource.name, f.name.name);
kind = FunctionKind::Method(resource_id);
}
ast::ResourceFunc::Static(f) => {
name = format!("[static]{}.{}", resource.name, f.name.name);
kind = FunctionKind::Static(resource_id);
}
ast::ResourceFunc::Constructor(_) => {
name = format!("[constructor]{}", resource.name);
kind = FunctionKind::Constructor(resource_id);
}
}
let named_func = func.named_func();
self.resolve_function(&named_func.docs, &name, &named_func.func, kind)
}
fn resolve_function(
&mut self,
docs: &ast::Docs<'_>,
name: &str,
func: &ast::Func,
kind: FunctionKind,
) -> Result<Function> {
let docs = self.docs(docs);
let params = self.resolve_params(&func.params, &kind)?;
let results = self.resolve_results(&func.results, &kind)?;
Ok(Function {
docs,
name: name.to_string(),
kind,
params,
results,
})
}
fn resolve_ast_item_path(&self, path: &ast::UsePath<'a>) -> Result<(AstItem, String, Span)> {
match path {
ast::UsePath::Id(id) => {
let item = self.ast_items[self.cur_ast_index]
.get(id.name)
.or_else(|| self.package_items.get(id.name));
match item {
Some(item) => Ok((*item, id.name.into(), id.span)),
None => {
bail!(Error {
span: id.span,
msg: format!("interface or world `{}` does not exist", id.name),
})
}
}
}
ast::UsePath::Package { id, name } => Ok((
self.foreign_deps[&id.package_name()][name.name],
name.name.into(),
name.span,
)),
}
}
fn extract_iface_from_item(
&self,
item: &AstItem,
name: &str,
span: Span,
) -> Result<InterfaceId> {
match item {
AstItem::Interface(id) => Ok(*id),