summaryrefslogtreecommitdiff
path: root/src/passes/Print.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/Print.cpp')
-rw-r--r--src/passes/Print.cpp98
1 files changed, 71 insertions, 27 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index ca2985240..440ff114f 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -72,6 +72,62 @@ static std::ostream& printLocal(Index index, Function* func, std::ostream& o) {
namespace {
+static bool maybePrintRefShorthand(std::ostream& o, Type type) {
+ if (!type.isRef()) {
+ return false;
+ }
+ auto heapType = type.getHeapType();
+ if (heapType.isBasic()) {
+ if (type.isNullable()) {
+ switch (heapType.getBasic()) {
+ case HeapType::func:
+ o << "funcref";
+ return true;
+ case HeapType::any:
+ o << "anyref";
+ return true;
+ case HeapType::eq:
+ o << "eqref";
+ return true;
+ case HeapType::i31:
+ case HeapType::data:
+ break;
+ case HeapType::string:
+ o << "stringref";
+ return true;
+ case HeapType::stringview_wtf8:
+ o << "stringview_wtf8";
+ return true;
+ case HeapType::stringview_wtf16:
+ o << "stringview_wtf16";
+ return true;
+ case HeapType::stringview_iter:
+ o << "stringview_iter";
+ return true;
+ }
+ } else {
+ switch (heapType.getBasic()) {
+ case HeapType::func:
+ case HeapType::any:
+ case HeapType::eq:
+ break;
+ case HeapType::i31:
+ o << "i31ref";
+ return true;
+ case HeapType::data:
+ o << "dataref";
+ return true;
+ case HeapType::string:
+ case HeapType::stringview_wtf8:
+ case HeapType::stringview_wtf16:
+ case HeapType::stringview_iter:
+ break;
+ }
+ }
+ }
+ return false;
+}
+
// Helper for printing the name of a type. This output is guaranteed to not
// contain spaces.
struct TypeNamePrinter {
@@ -126,13 +182,15 @@ void TypeNamePrinter::print(Type type) {
} else if (type.isRtt()) {
print(type.getRtt());
} else if (type.isRef()) {
- os << "ref";
- if (type.isNullable()) {
- os << "?";
+ if (!maybePrintRefShorthand(os, type)) {
+ os << "ref";
+ if (type.isNullable()) {
+ os << "?";
+ }
+ os << '|';
+ print(type.getHeapType());
+ os << '|';
}
- os << '|';
- print(type.getHeapType());
- os << '|';
} else {
WASM_UNREACHABLE("unexpected type");
}
@@ -273,29 +331,15 @@ static std::ostream& printType(std::ostream& o, Type type, Module* wasm) {
}
TypeNamePrinter(o, wasm).print(rtt.heapType);
o << ')';
- } else if (type.isRef() && !type.isBasic()) {
- auto heapType = type.getHeapType();
- if (type.isNullable() && heapType.isBasic()) {
- // Print shorthands for certain nullable basic heap types.
- switch (heapType.getBasic()) {
- case HeapType::string:
- return o << "stringref";
- case HeapType::stringview_wtf8:
- return o << "stringview_wtf8";
- case HeapType::stringview_wtf16:
- return o << "stringview_wtf16";
- case HeapType::stringview_iter:
- return o << "stringview_iter";
- default:
- break;
+ } else if (type.isRef()) {
+ if (!maybePrintRefShorthand(o, type)) {
+ o << "(ref ";
+ if (type.isNullable()) {
+ o << "null ";
}
+ TypeNamePrinter(o, wasm).print(type.getHeapType());
+ o << ')';
}
- o << "(ref ";
- if (type.isNullable()) {
- o << "null ";
- }
- TypeNamePrinter(o, wasm).print(type.getHeapType());
- o << ')';
} else {
WASM_UNREACHABLE("unexpected type");
}