-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathresolve.rs
3923 lines (3612 loc) · 154 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 std::cmp::Ordering;
use std::collections::hash_map;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::mem;
use std::path::{Path, PathBuf};
use anyhow::{anyhow, bail, Context, Result};
use id_arena::{Arena, Id};
use indexmap::{IndexMap, IndexSet};
use semver::Version;
#[cfg(feature = "serde")]
use serde_derive::Serialize;
use crate::ast::lex::Span;
use crate::ast::{parse_use_path, ParsedUsePath};
#[cfg(feature = "serde")]
use crate::serde_::{serialize_arena, serialize_id_map};
use crate::{
AstItem, Docs, Error, Function, FunctionKind, Handle, IncludeName, Interface, InterfaceId,
InterfaceSpan, LiftLowerAbi, ManglingAndAbi, PackageName, PackageNotFoundError, SourceMap,
Stability, Type, TypeDef, TypeDefKind, TypeId, TypeIdVisitor, TypeOwner, UnresolvedPackage,
UnresolvedPackageGroup, World, WorldId, WorldItem, WorldKey, WorldSpan,
};
mod clone;
/// Representation of a fully resolved set of WIT packages.
///
/// This structure contains a graph of WIT packages and all of their contents
/// merged together into the contained arenas. All items are sorted
/// topologically and everything here is fully resolved, so with a `Resolve` no
/// name lookups are necessary and instead everything is index-based.
///
/// Working with a WIT package requires inserting it into a `Resolve` to ensure
/// that all of its dependencies are satisfied. This will give the full picture
/// of that package's types and such.
///
/// Each item in a `Resolve` has a parent link to trace it back to the original
/// package as necessary.
#[derive(Default, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Resolve {
/// All known worlds within this `Resolve`.
///
/// Each world points at a `PackageId` which is stored below. No ordering is
/// guaranteed between this list of worlds.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub worlds: Arena<World>,
/// All known interfaces within this `Resolve`.
///
/// Each interface points at a `PackageId` which is stored below. No
/// ordering is guaranteed between this list of interfaces.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub interfaces: Arena<Interface>,
/// All known types within this `Resolve`.
///
/// Types are topologically sorted such that any type referenced from one
/// type is guaranteed to be defined previously. Otherwise though these are
/// not sorted by interface for example.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub types: Arena<TypeDef>,
/// All known packages within this `Resolve`.
///
/// This list of packages is not sorted. Sorted packages can be queried
/// through [`Resolve::topological_packages`].
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
pub packages: Arena<Package>,
/// A map of package names to the ID of the package with that name.
#[cfg_attr(feature = "serde", serde(skip))]
pub package_names: IndexMap<PackageName, PackageId>,
/// Activated features for this [`Resolve`].
///
/// This set of features is empty by default. This is consulted for
/// `@unstable` annotations in loaded WIT documents. Any items with
/// `@unstable` are filtered out unless their feature is present within this
/// set.
#[cfg_attr(feature = "serde", serde(skip))]
pub features: IndexSet<String>,
/// Activate all features for this [`Resolve`].
#[cfg_attr(feature = "serde", serde(skip))]
pub all_features: bool,
}
/// A WIT package within a `Resolve`.
///
/// A package is a collection of interfaces and worlds. Packages additionally
/// have a unique identifier that affects generated components and uniquely
/// identifiers this particular package.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Package {
/// A unique name corresponding to this package.
pub name: PackageName,
/// Documentation associated with this package.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
/// All interfaces contained in this packaged, keyed by the interface's
/// name.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id_map"))]
pub interfaces: IndexMap<String, InterfaceId>,
/// All worlds contained in this package, keyed by the world's name.
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id_map"))]
pub worlds: IndexMap<String, WorldId>,
}
pub type PackageId = Id<Package>;
/// All the sources used during resolving a directory or path.
#[derive(Clone, Debug)]
pub struct PackageSourceMap {
sources: Vec<Vec<PathBuf>>,
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
}
impl PackageSourceMap {
fn from_single_source(package_id: PackageId, source: &Path) -> Self {
Self {
sources: vec![vec![source.to_path_buf()]],
package_id_to_source_map_idx: BTreeMap::from([(package_id, 0)]),
}
}
fn from_source_maps(
source_maps: Vec<SourceMap>,
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
) -> PackageSourceMap {
for (package_id, idx) in &package_id_to_source_map_idx {
if *idx >= source_maps.len() {
panic!(
"Invalid source map index: {}, package id: {:?}, source maps size: {}",
idx,
package_id,
source_maps.len()
)
}
}
Self {
sources: source_maps
.into_iter()
.map(|source_map| {
source_map
.source_files()
.map(|path| path.to_path_buf())
.collect()
})
.collect(),
package_id_to_source_map_idx,
}
}
/// All unique source paths.
pub fn paths(&self) -> impl Iterator<Item = &Path> {
// Usually any two source map should not have duplicated source paths,
// but it can happen, e.g. with using [`Resolve::push_str`] directly.
// To be sure we use a set for deduplication here.
self.sources
.iter()
.flatten()
.map(|path_buf| path_buf.as_ref())
.collect::<HashSet<&Path>>()
.into_iter()
}
/// Source paths for package
pub fn package_paths(&self, id: PackageId) -> Option<impl Iterator<Item = &Path>> {
self.package_id_to_source_map_idx
.get(&id)
.map(|&idx| self.sources[idx].iter().map(|path_buf| path_buf.as_ref()))
}
}
enum ParsedFile {
#[cfg(feature = "decoding")]
Package(PackageId),
Unresolved(UnresolvedPackageGroup),
}
/// Visitor helper for performing topological sort on a group of packages.
fn visit<'a>(
pkg: &'a UnresolvedPackage,
pkg_details_map: &'a BTreeMap<PackageName, (UnresolvedPackage, usize)>,
order: &mut IndexSet<PackageName>,
visiting: &mut HashSet<&'a PackageName>,
source_maps: &[SourceMap],
) -> Result<()> {
if order.contains(&pkg.name) {
return Ok(());
}
match pkg_details_map.get(&pkg.name) {
Some(pkg_details) => {
let (_, source_maps_index) = pkg_details;
source_maps[*source_maps_index].rewrite_error(|| {
for (i, (dep, _)) in pkg.foreign_deps.iter().enumerate() {
let span = pkg.foreign_dep_spans[i];
if !visiting.insert(dep) {
bail!(Error::new(span, "package depends on itself"));
}
if let Some(dep) = pkg_details_map.get(dep) {
let (dep_pkg, _) = dep;
visit(dep_pkg, pkg_details_map, order, visiting, source_maps)?;
}
assert!(visiting.remove(dep));
}
assert!(order.insert(pkg.name.clone()));
Ok(())
})
}
None => panic!("No pkg_details found for package when doing topological sort"),
}
}
impl Resolve {
/// Creates a new [`Resolve`] with no packages/items inside of it.
pub fn new() -> Resolve {
Resolve::default()
}
/// Parse WIT packages from the input `path`.
///
/// The input `path` can be one of:
///
/// * A directory containing a WIT package with an optional `deps` directory
/// for any dependent WIT packages it references.
/// * A single standalone WIT file.
/// * A wasm-encoded WIT package as a single file in the wasm binary format.
/// * A wasm-encoded WIT package as a single file in the wasm text format.
///
/// In all of these cases packages are allowed to depend on previously
/// inserted packages into this `Resolve`. Resolution for packages is based
/// on the name of each package and reference.
///
/// This method returns a `PackageId` and additionally a `PackageSourceMap`.
/// The `PackageId` represent the main package that was parsed. For example if a single WIT
/// file was specified this will be the main package found in the file. For a directory this
/// will be all the main package in the directory itself. The `PackageId` value is useful
/// to pass to [`Resolve::select_world`] to take a user-specified world in a
/// conventional fashion and select which to use for bindings generation.
///
/// The returned [`PackageSourceMap`] contains all the sources used during this operation.
/// This can be useful for systems that want to rebuild or regenerate bindings based on files modified,
/// or for ones which like to identify the used files for a package.
///
/// More information can also be found at [`Resolve::push_dir`] and
/// [`Resolve::push_file`].
pub fn push_path(&mut self, path: impl AsRef<Path>) -> Result<(PackageId, PackageSourceMap)> {
self._push_path(path.as_ref())
}
fn _push_path(&mut self, path: &Path) -> Result<(PackageId, PackageSourceMap)> {
if path.is_dir() {
self.push_dir(path).with_context(|| {
format!(
"failed to resolve directory while parsing WIT for path [{}]",
path.display()
)
})
} else {
let id = self.push_file(path)?;
Ok((id, PackageSourceMap::from_single_source(id, path)))
}
}
fn sort_unresolved_packages(
&mut self,
main: UnresolvedPackageGroup,
deps: Vec<UnresolvedPackageGroup>,
) -> Result<(PackageId, PackageSourceMap)> {
let mut pkg_details_map = BTreeMap::new();
let mut source_maps = Vec::new();
let mut insert = |group: UnresolvedPackageGroup| {
let UnresolvedPackageGroup {
main,
nested,
source_map,
} = group;
let i = source_maps.len();
source_maps.push(source_map);
for pkg in nested.into_iter().chain([main]) {
let name = pkg.name.clone();
let my_span = pkg.package_name_span;
let (prev_pkg, prev_i) = match pkg_details_map.insert(name.clone(), (pkg, i)) {
Some(pair) => pair,
None => continue,
};
let loc1 = source_maps[i].render_location(my_span);
let loc2 = source_maps[prev_i].render_location(prev_pkg.package_name_span);
bail!(
"\
package {name} is defined in two different locations:\n\
* {loc1}\n\
* {loc2}\n\
"
)
}
Ok(())
};
let main_name = main.main.name.clone();
insert(main)?;
for dep in deps {
insert(dep)?;
}
// Perform a simple topological sort which will bail out on cycles
// and otherwise determine the order that packages must be added to
// this `Resolve`.
let mut order = IndexSet::new();
let mut visiting = HashSet::new();
for pkg_details in pkg_details_map.values() {
let (pkg, _) = pkg_details;
visit(
pkg,
&pkg_details_map,
&mut order,
&mut visiting,
&source_maps,
)?;
}
// Ensure that the final output is topologically sorted. Use a set to ensure that we render
// the buffers for each `SourceMap` only once, even though multiple packages may reference
// the same `SourceMap`.
let mut package_id_to_source_map_idx = BTreeMap::new();
let mut main_pkg_id = None;
for name in order {
let (pkg, source_map_index) = pkg_details_map.remove(&name).unwrap();
let source_map = &source_maps[source_map_index];
let is_main = pkg.name == main_name;
let id = self.push(pkg, source_map)?;
if is_main {
assert!(main_pkg_id.is_none());
main_pkg_id = Some(id);
}
package_id_to_source_map_idx.insert(id, source_map_index);
}
Ok((
main_pkg_id.unwrap(),
PackageSourceMap::from_source_maps(source_maps, package_id_to_source_map_idx),
))
}
/// Parses the filesystem directory at `path` as a WIT package and returns
/// a fully resolved [`PackageId`] list as a result.
///
/// The directory itself is parsed with [`UnresolvedPackageGroup::parse_dir`]
/// and then all packages found are inserted into this `Resolve`. The `path`
/// specified may have a `deps` subdirectory which is probed automatically
/// for any other WIT dependencies.
///
/// The `deps` folder may contain:
///
/// * `$path/deps/my-package/*.wit` - a directory that may contain multiple
/// WIT files. This is parsed with [`UnresolvedPackageGroup::parse_dir`]
/// and then inserted into this [`Resolve`]. Note that cannot recursively
/// contain a `deps` directory.
/// * `$path/deps/my-package.wit` - a single-file WIT package. This is
/// parsed with [`Resolve::push_file`] and then added to `self` for
/// name reoslution.
/// * `$path/deps/my-package.{wasm,wat}` - a wasm-encoded WIT package either
/// in the text for binary format.
///
/// In all cases entries in the `deps` folder are added to `self` first
/// before adding files found in `path` itself. All WIT packages found are
/// candidates for name-based resolution that other packages may use.
///
/// This function returns a tuple of two values. The first value is a
/// [`PackageId`], which represents the main WIT package found within
/// `path`. This argument is useful for passing to [`Resolve::select_world`]
/// for choosing something to bindgen with.
///
/// The second value returned is a [`PackageSourceMap`], which contains all the sources
/// that were parsed during resolving. This can be useful for:
/// * build systems that want to rebuild bindings whenever one of the files changed
/// * or other tools, which want to identify the sources for the resolved packages
pub fn push_dir(&mut self, path: impl AsRef<Path>) -> Result<(PackageId, PackageSourceMap)> {
self._push_dir(path.as_ref())
}
fn _push_dir(&mut self, path: &Path) -> Result<(PackageId, PackageSourceMap)> {
let top_pkg = UnresolvedPackageGroup::parse_dir(path)
.with_context(|| format!("failed to parse package: {}", path.display()))?;
let deps = path.join("deps");
let deps = self
.parse_deps_dir(&deps)
.with_context(|| format!("failed to parse dependency directory: {}", deps.display()))?;
self.sort_unresolved_packages(top_pkg, deps)
}
fn parse_deps_dir(&mut self, path: &Path) -> Result<Vec<UnresolvedPackageGroup>> {
let mut ret = Vec::new();
if !path.exists() {
return Ok(ret);
}
let mut entries = path
.read_dir()
.and_then(|i| i.collect::<std::io::Result<Vec<_>>>())
.context("failed to read directory")?;
entries.sort_by_key(|e| e.file_name());
for dep in entries {
let path = dep.path();
let pkg = if dep.file_type()?.is_dir() || path.metadata()?.is_dir() {
// If this entry is a directory or a symlink point to a
// directory then always parse it as an `UnresolvedPackage`
// since it's intentional to not support recursive `deps`
// directories.
UnresolvedPackageGroup::parse_dir(&path)
.with_context(|| format!("failed to parse package: {}", path.display()))?
} else {
// If this entry is a file then we may want to ignore it but
// this may also be a standalone WIT file or a `*.wasm` or
// `*.wat` encoded package.
let filename = dep.file_name();
match Path::new(&filename).extension().and_then(|s| s.to_str()) {
Some("wit") | Some("wat") | Some("wasm") => match self._push_file(&path)? {
#[cfg(feature = "decoding")]
ParsedFile::Package(_) => continue,
ParsedFile::Unresolved(pkg) => pkg,
},
// Other files in deps dir are ignored for now to avoid
// accidentally including things like `.DS_Store` files in
// the call below to `parse_dir`.
_ => continue,
}
};
ret.push(pkg);
}
Ok(ret)
}
/// Parses the contents of `path` from the filesystem and pushes the result
/// into this `Resolve`.
///
/// The `path` referenced here can be one of:
///
/// * A WIT file. Note that in this case this single WIT file will be the
/// entire package and any dependencies it has must already be in `self`.
/// * A WIT package encoded as WebAssembly, either in text or binary form.
/// In this the package and all of its dependencies are automatically
/// inserted into `self`.
///
/// In both situations the `PackageId`s of the resulting resolved packages
/// are returned from this method. The return value is mostly useful in
/// conjunction with [`Resolve::select_world`].
pub fn push_file(&mut self, path: impl AsRef<Path>) -> Result<PackageId> {
match self._push_file(path.as_ref())? {
#[cfg(feature = "decoding")]
ParsedFile::Package(id) => Ok(id),
ParsedFile::Unresolved(pkg) => self.push_group(pkg),
}
}
fn _push_file(&mut self, path: &Path) -> Result<ParsedFile> {
let contents = std::fs::read(path)
.with_context(|| format!("failed to read path for WIT [{}]", path.display()))?;
// If decoding is enabled at compile time then try to see if this is a
// wasm file.
#[cfg(feature = "decoding")]
{
use crate::decoding::{decode, DecodedWasm};
#[cfg(feature = "wat")]
let is_wasm = wat::Detect::from_bytes(&contents).is_wasm();
#[cfg(not(feature = "wat"))]
let is_wasm = wasmparser::Parser::is_component(&contents);
if is_wasm {
#[cfg(feature = "wat")]
let contents = wat::parse_bytes(&contents).map_err(|mut e| {
e.set_path(path);
e
})?;
match decode(&contents)? {
DecodedWasm::Component(..) => {
bail!("found an actual component instead of an encoded WIT package in wasm")
}
DecodedWasm::WitPackage(resolve, pkg) => {
let remap = self.merge(resolve)?;
return Ok(ParsedFile::Package(remap.packages[pkg.index()]));
}
}
}
}
// If this wasn't a wasm file then assume it's a WIT file.
let text = match std::str::from_utf8(&contents) {
Ok(s) => s,
Err(_) => bail!("input file is not valid utf-8 [{}]", path.display()),
};
let pkgs = UnresolvedPackageGroup::parse(path, text)?;
Ok(ParsedFile::Unresolved(pkgs))
}
/// Appends a new [`UnresolvedPackage`] to this [`Resolve`], creating a
/// fully resolved package with no dangling references.
///
/// All the dependencies of `unresolved` must already have been loaded
/// within this `Resolve` via previous calls to `push` or other methods such
/// as [`Resolve::push_path`].
///
/// Any dependency resolution error or otherwise world-elaboration error
/// will be returned here, if successful a package identifier is returned
/// which corresponds to the package that was just inserted.
pub fn push(
&mut self,
unresolved: UnresolvedPackage,
source_map: &SourceMap,
) -> Result<PackageId> {
source_map.rewrite_error(|| Remap::default().append(self, unresolved))
}
/// Appends new [`UnresolvedPackageGroup`] to this [`Resolve`], creating a
/// fully resolved package with no dangling references.
///
/// Any dependency resolution error or otherwise world-elaboration error
/// will be returned here, if successful a package identifier is returned
/// which corresponds to the package that was just inserted.
///
/// The returned [`PackageId`]s are listed in topologically sorted order.
pub fn push_group(&mut self, unresolved_group: UnresolvedPackageGroup) -> Result<PackageId> {
let (pkg_id, _) = self.sort_unresolved_packages(unresolved_group, Vec::new())?;
Ok(pkg_id)
}
/// Convenience method for combining [`UnresolvedPackageGroup::parse`] and
/// [`Resolve::push_group`].
///
/// The `path` provided is used for error messages but otherwise is not
/// read. This method does not touch the filesystem. The `contents` provided
/// are the contents of a WIT package.
pub fn push_str(&mut self, path: impl AsRef<Path>, contents: &str) -> Result<PackageId> {
self.push_group(UnresolvedPackageGroup::parse(path.as_ref(), contents)?)
}
pub fn all_bits_valid(&self, ty: &Type) -> bool {
match ty {
Type::U8
| Type::S8
| Type::U16
| Type::S16
| Type::U32
| Type::S32
| Type::U64
| Type::S64
| Type::F32
| Type::F64 => true,
Type::Bool | Type::Char | Type::String => false,
Type::Id(id) => match &self.types[*id].kind {
TypeDefKind::List(_)
| TypeDefKind::Variant(_)
| TypeDefKind::Enum(_)
| TypeDefKind::Option(_)
| TypeDefKind::Result(_)
| TypeDefKind::Future(_)
| TypeDefKind::Stream(_)
| TypeDefKind::ErrorContext => false,
TypeDefKind::Type(t) => self.all_bits_valid(t),
TypeDefKind::Handle(h) => match h {
crate::Handle::Own(_) => true,
crate::Handle::Borrow(_) => true,
},
TypeDefKind::Resource => false,
TypeDefKind::Record(r) => r.fields.iter().all(|f| self.all_bits_valid(&f.ty)),
TypeDefKind::Tuple(t) => t.types.iter().all(|t| self.all_bits_valid(t)),
// FIXME: this could perhaps be `true` for multiples-of-32 but
// seems better to probably leave this as unconditionally
// `false` for now, may want to reconsider later?
TypeDefKind::Flags(_) => false,
TypeDefKind::Unknown => unreachable!(),
},
}
}
/// Merges all the contents of a different `Resolve` into this one. The
/// `Remap` structure returned provides a mapping from all old indices to
/// new indices
///
/// This operation can fail if `resolve` disagrees with `self` about the
/// packages being inserted. Otherwise though this will additionally attempt
/// to "union" packages found in `resolve` with those found in `self`.
/// Unioning packages is keyed on the name/url of packages for those with
/// URLs present. If found then it's assumed that both `Resolve` instances
/// were originally created from the same contents and are two views
/// of the same package.
pub fn merge(&mut self, resolve: Resolve) -> Result<Remap> {
log::trace!(
"merging {} packages into {} packages",
resolve.packages.len(),
self.packages.len()
);
let mut map = MergeMap::new(&resolve, &self);
map.build()?;
let MergeMap {
package_map,
interface_map,
type_map,
world_map,
interfaces_to_add,
worlds_to_add,
..
} = map;
// With a set of maps from ids in `resolve` to ids in `self` the next
// operation is to start moving over items and building a `Remap` to
// update ids.
//
// Each component field of `resolve` is moved into `self` so long as
// its ID is not within one of the maps above. If it's present in a map
// above then that means the item is already present in `self` so a new
// one need not be added. If it's not present in a map that means it's
// not present in `self` so it must be added to an arena.
//
// When adding an item to an arena one of the `remap.update_*` methods
// is additionally called to update all identifiers from pointers within
// `resolve` to becoming pointers within `self`.
//
// Altogether this should weave all the missing items in `self` from
// `resolve` into one structure while updating all identifiers to
// be local within `self`.
let mut remap = Remap::default();
let Resolve {
types,
worlds,
interfaces,
packages,
package_names,
features: _,
..
} = resolve;
let mut moved_types = Vec::new();
for (id, mut ty) in types {
let new_id = match type_map.get(&id).copied() {
Some(id) => {
update_stability(&ty.stability, &mut self.types[id].stability)?;
id
}
None => {
log::debug!("moving type {:?}", ty.name);
moved_types.push(id);
remap.update_typedef(self, &mut ty, None)?;
self.types.alloc(ty)
}
};
assert_eq!(remap.types.len(), id.index());
remap.types.push(Some(new_id));
}
let mut moved_interfaces = Vec::new();
for (id, mut iface) in interfaces {
let new_id = match interface_map.get(&id).copied() {
Some(id) => {
update_stability(&iface.stability, &mut self.interfaces[id].stability)?;
id
}
None => {
log::debug!("moving interface {:?}", iface.name);
moved_interfaces.push(id);
remap.update_interface(self, &mut iface, None)?;
self.interfaces.alloc(iface)
}
};
assert_eq!(remap.interfaces.len(), id.index());
remap.interfaces.push(Some(new_id));
}
let mut moved_worlds = Vec::new();
for (id, mut world) in worlds {
let new_id = match world_map.get(&id).copied() {
Some(id) => {
update_stability(&world.stability, &mut self.worlds[id].stability)?;
id
}
None => {
log::debug!("moving world {}", world.name);
moved_worlds.push(id);
let mut update = |map: &mut IndexMap<WorldKey, WorldItem>| -> Result<_> {
for (mut name, mut item) in mem::take(map) {
remap.update_world_key(&mut name, None)?;
match &mut item {
WorldItem::Function(f) => remap.update_function(self, f, None)?,
WorldItem::Interface { id, .. } => {
*id = remap.map_interface(*id, None)?
}
WorldItem::Type(i) => *i = remap.map_type(*i, None)?,
}
map.insert(name, item);
}
Ok(())
};
update(&mut world.imports)?;
update(&mut world.exports)?;
self.worlds.alloc(world)
}
};
assert_eq!(remap.worlds.len(), id.index());
remap.worlds.push(Some(new_id));
}
for (id, mut pkg) in packages {
let new_id = match package_map.get(&id).copied() {
Some(id) => id,
None => {
for (_, id) in pkg.interfaces.iter_mut() {
*id = remap.map_interface(*id, None)?;
}
for (_, id) in pkg.worlds.iter_mut() {
*id = remap.map_world(*id, None)?;
}
self.packages.alloc(pkg)
}
};
assert_eq!(remap.packages.len(), id.index());
remap.packages.push(new_id);
}
for (name, id) in package_names {
let id = remap.packages[id.index()];
if let Some(prev) = self.package_names.insert(name, id) {
assert_eq!(prev, id);
}
}
// Fixup all "parent" links now.
//
// Note that this is only done for items that are actually moved from
// `resolve` into `self`, which is tracked by the various `moved_*`
// lists built incrementally above. The ids in the `moved_*` lists
// are ids within `resolve`, so they're translated through `remap` to
// ids within `self`.
for id in moved_worlds {
let id = remap.map_world(id, None)?;
if let Some(pkg) = self.worlds[id].package.as_mut() {
*pkg = remap.packages[pkg.index()];
}
}
for id in moved_interfaces {
let id = remap.map_interface(id, None)?;
if let Some(pkg) = self.interfaces[id].package.as_mut() {
*pkg = remap.packages[pkg.index()];
}
}
for id in moved_types {
let id = remap.map_type(id, None)?;
match &mut self.types[id].owner {
TypeOwner::Interface(id) => *id = remap.map_interface(*id, None)?,
TypeOwner::World(id) => *id = remap.map_world(*id, None)?,
TypeOwner::None => {}
}
}
// And finally process items that were present in `resolve` but were
// not present in `self`. This is only done for merged packages as
// documents may be added to `self.documents` but wouldn't otherwise be
// present in the `documents` field of the corresponding package.
for (name, pkg, iface) in interfaces_to_add {
let prev = self.packages[pkg]
.interfaces
.insert(name, remap.map_interface(iface, None)?);
assert!(prev.is_none());
}
for (name, pkg, world) in worlds_to_add {
let prev = self.packages[pkg]
.worlds
.insert(name, remap.map_world(world, None)?);
assert!(prev.is_none());
}
log::trace!("now have {} packages", self.packages.len());
#[cfg(debug_assertions)]
self.assert_valid();
Ok(remap)
}
/// Merges the world `from` into the world `into`.
///
/// This will attempt to merge one world into another, unioning all of its
/// imports and exports together. This is an operation performed by
/// `wit-component`, for example where two different worlds from two
/// different libraries were linked into the same core wasm file and are
/// producing a singular world that will be the final component's
/// interface.
///
/// This operation can fail if the imports/exports overlap.
pub fn merge_worlds(&mut self, from: WorldId, into: WorldId) -> Result<()> {
let mut new_imports = Vec::new();
let mut new_exports = Vec::new();
let from_world = &self.worlds[from];
let into_world = &self.worlds[into];
log::trace!("merging {} into {}", from_world.name, into_world.name);
// First walk over all the imports of `from` world and figure out what
// to do with them.
//
// If the same item exists in `from` and `into` then merge it together
// below with `merge_world_item` which basically asserts they're the
// same. Otherwise queue up a new import since if `from` has more
// imports than `into` then it's fine to add new imports.
for (name, from_import) in from_world.imports.iter() {
let name_str = self.name_world_key(name);
match into_world.imports.get(name) {
Some(into_import) => {
log::trace!("info/from shared import on `{name_str}`");
self.merge_world_item(from_import, into_import)
.with_context(|| format!("failed to merge world import {name_str}"))?;
}
None => {
log::trace!("new import: `{name_str}`");
new_imports.push((name.clone(), from_import.clone()));
}
}
}
// Build a set of interfaces which are required to be imported because
// of `into`'s exports. This set is then used below during
// `ensure_can_add_world_export`.
//
// This is the set of interfaces which exports depend on that are
// themselves not exports.
let mut must_be_imported = HashMap::new();
for (key, export) in into_world.exports.iter() {
for dep in self.world_item_direct_deps(export) {
if into_world.exports.contains_key(&WorldKey::Interface(dep)) {
continue;
}
self.foreach_interface_dep(dep, &mut |id| {
must_be_imported.insert(id, key.clone());
});
}
}
// Next walk over exports of `from` and process these similarly to
// imports.
for (name, from_export) in from_world.exports.iter() {
let name_str = self.name_world_key(name);
match into_world.exports.get(name) {
Some(into_export) => {
log::trace!("info/from shared export on `{name_str}`");
self.merge_world_item(from_export, into_export)
.with_context(|| format!("failed to merge world export {name_str}"))?;
}
None => {
log::trace!("new export `{name_str}`");
// See comments in `ensure_can_add_world_export` for why
// this is slightly different than imports.
self.ensure_can_add_world_export(
into_world,
name,
from_export,
&must_be_imported,
)
.with_context(|| {
format!("failed to add export `{}`", self.name_world_key(name))
})?;
new_exports.push((name.clone(), from_export.clone()));
}
}
}
// For all the new imports and exports they may need to be "cloned" to
// be able to belong to the new world. For example:
//
// * Anonymous interfaces have a `package` field which points to the
// package of the containing world, but `from` and `into` may not be
// in the same package.
//
// * Type imports have an `owner` field that point to `from`, but they
// now need to point to `into` instead.
//
// Cloning is no trivial task, however, so cloning is delegated to a
// submodule to perform a "deep" clone and copy items into new arena
// entries as necessary.
let mut cloner = clone::Cloner::new(self, TypeOwner::World(from), TypeOwner::World(into));
cloner.register_world_type_overlap(from, into);
for (name, item) in new_imports.iter_mut().chain(&mut new_exports) {
cloner.world_item(name, item);
}
// Insert any new imports and new exports found first.
let into_world = &mut self.worlds[into];
for (name, import) in new_imports {
let prev = into_world.imports.insert(name, import);
assert!(prev.is_none());
}
for (name, export) in new_exports {
let prev = into_world.exports.insert(name, export);
assert!(prev.is_none());
}
#[cfg(debug_assertions)]
self.assert_valid();
Ok(())
}
fn merge_world_item(&self, from: &WorldItem, into: &WorldItem) -> Result<()> {
let mut map = MergeMap::new(self, self);
match (from, into) {
(WorldItem::Interface { id: from, .. }, WorldItem::Interface { id: into, .. }) => {
// If these imports are the same that can happen, for
// example, when both worlds to `import foo:bar/baz;`. That
// foreign interface will point to the same interface within
// `Resolve`.
if from == into {
return Ok(());
}
// .. otherwise this MUST be a case of
// `import foo: interface { ... }`. If `from != into` but
// both `from` and `into` have the same name then the
// `WorldKey::Interface` case is ruled out as otherwise
// they'd have different names.
//
// In the case of an anonymous interface all we can do is
// ensure that the interfaces both match, so use `MergeMap`
// for that.
map.build_interface(*from, *into)
.context("failed to merge interfaces")?;
}
// Like `WorldKey::Name` interfaces for functions and types the
// structure is asserted to be the same.
(WorldItem::Function(from), WorldItem::Function(into)) => {
map.build_function(from, into)
.context("failed to merge functions")?;
}
(WorldItem::Type(from), WorldItem::Type(into)) => {
map.build_type_id(*from, *into)
.context("failed to merge types")?;
}
// Kind-level mismatches are caught here.
(WorldItem::Interface { .. }, _)
| (WorldItem::Function { .. }, _)
| (WorldItem::Type { .. }, _) => {
bail!("different kinds of items");
}
}
assert!(map.interfaces_to_add.is_empty());
assert!(map.worlds_to_add.is_empty());
Ok(())
}
/// This method ensures that the world export of `name` and `item` can be
/// added to the world `into` without changing the meaning of `into`.
///
/// All dependencies of world exports must either be:
///
/// * An export themselves
/// * An import with all transitive dependencies of the import also imported
///
/// It's not possible to depend on an import which then also depends on an
/// export at some point, for example. This method ensures that if `name`
/// and `item` are added that this property is upheld.
fn ensure_can_add_world_export(
&self,
into: &World,
name: &WorldKey,
item: &WorldItem,
must_be_imported: &HashMap<InterfaceId, WorldKey>,
) -> Result<()> {
assert!(!into.exports.contains_key(name));
let name = self.name_world_key(name);
// First make sure that all of this item's dependencies are either
// exported or the entire chain of imports rooted at that dependency are
// all imported.
for dep in self.world_item_direct_deps(item) {
if into.exports.contains_key(&WorldKey::Interface(dep)) {
continue;
}
self.ensure_not_exported(into, dep)