@@ -590,11 +590,46 @@ namespace {
590
590
591
591
void classifyTypeVisibility (Module& wasm,
592
592
InsertOrderedMap<HeapType, HeapTypeInfo>& types) {
593
- // We will need to traverse the types used by public types and mark them
594
- // public as well.
593
+ for (auto type : getPublicHeapTypes (wasm)) {
594
+ if (auto it = types.find (type); it != types.end ()) {
595
+ it->second .visibility = Visibility::Public;
596
+ }
597
+ }
598
+ for (auto & [type, info] : types) {
599
+ if (info.visibility != Visibility::Public) {
600
+ info.visibility = Visibility::Private;
601
+ }
602
+ }
603
+ }
604
+
605
+ void setIndices (IndexedHeapTypes& indexedTypes) {
606
+ for (Index i = 0 ; i < indexedTypes.types .size (); i++) {
607
+ indexedTypes.indices [indexedTypes.types [i]] = i;
608
+ }
609
+ }
610
+
611
+ } // anonymous namespace
612
+
613
+ std::vector<HeapType> collectHeapTypes (Module& wasm) {
614
+ auto info = collectHeapTypeInfo (wasm);
615
+ std::vector<HeapType> types;
616
+ types.reserve (info.size ());
617
+ for (auto & [type, _] : info) {
618
+ types.push_back (type);
619
+ }
620
+ return types;
621
+ }
622
+
623
+ std::vector<HeapType> getPublicHeapTypes (Module& wasm) {
624
+ // Look at the types of imports as exports to get an initial set of public
625
+ // types, then traverse the types used by public types and collect the
626
+ // transitively reachable public types as well.
595
627
std::vector<HeapType> workList;
596
628
std::unordered_set<RecGroup> publicGroups;
597
629
630
+ // The collected types.
631
+ std::vector<HeapType> publicTypes;
632
+
598
633
auto notePublic = [&](HeapType type) {
599
634
if (type.isBasic ()) {
600
635
return ;
@@ -604,12 +639,8 @@ void classifyTypeVisibility(Module& wasm,
604
639
// The groups in this type have already been marked public.
605
640
return ;
606
641
}
607
- for (auto member : type.getRecGroup ()) {
608
- if (auto it = types.find (member); it != types.end ()) {
609
- it->second .visibility = Visibility::Public;
610
- }
611
- workList.push_back (member);
612
- }
642
+ publicTypes.insert (publicTypes.end (), group.begin (), group.end ());
643
+ workList.insert (workList.end (), group.begin (), group.end ());
613
644
};
614
645
615
646
ModuleUtils::iterImportedTags (wasm, [&](Tag* tag) { notePublic (tag->type ); });
@@ -675,46 +706,10 @@ void classifyTypeVisibility(Module& wasm,
675
706
}
676
707
}
677
708
678
- for (auto & [_, info] : types) {
679
- if (info.visibility != Visibility::Public) {
680
- info.visibility = Visibility::Private;
681
- }
682
- }
683
-
684
709
// TODO: In an open world, we need to consider subtypes of public types public
685
710
// as well, or potentially even consider all types to be public unless
686
711
// otherwise annotated.
687
- }
688
-
689
- void setIndices (IndexedHeapTypes& indexedTypes) {
690
- for (Index i = 0 ; i < indexedTypes.types .size (); i++) {
691
- indexedTypes.indices [indexedTypes.types [i]] = i;
692
- }
693
- }
694
-
695
- } // anonymous namespace
696
-
697
- std::vector<HeapType> collectHeapTypes (Module& wasm) {
698
- auto info = collectHeapTypeInfo (wasm);
699
- std::vector<HeapType> types;
700
- types.reserve (info.size ());
701
- for (auto & [type, _] : info) {
702
- types.push_back (type);
703
- }
704
- return types;
705
- }
706
-
707
- std::vector<HeapType> getPublicHeapTypes (Module& wasm) {
708
- auto info = collectHeapTypeInfo (
709
- wasm, TypeInclusion::BinaryTypes, VisibilityHandling::FindVisibility);
710
- std::vector<HeapType> types;
711
- types.reserve (info.size ());
712
- for (auto & [type, typeInfo] : info) {
713
- if (typeInfo.visibility == Visibility::Public) {
714
- types.push_back (type);
715
- }
716
- }
717
- return types;
712
+ return publicTypes;
718
713
}
719
714
720
715
std::vector<HeapType> getPrivateHeapTypes (Module& wasm) {
0 commit comments