Skip to content

Commit fc54b7c

Browse files
authored
Refactor typeuse parsing more (NFC) (#2146)
Now `parseTypeUse` always returns `FunctionType*` and params/return pair and makes sure the two are consistent, so the caller does not have to populate params/results when only `(type)` is specified.
1 parent b60dad3 commit fc54b7c

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/wasm/wasm-s-parser.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,11 @@ size_t SExpressionWasmBuilder::parseTypeUse(Element& s,
579579
paramOrResultExists = true;
580580
result = parseResult(*s[i++]);
581581
}
582+
// If none of type/param/result exists, this is equivalent to a type that does
583+
// not have parameters and returns nothing.
584+
if (!typeExists && !paramOrResultExists) {
585+
paramOrResultExists = true;
586+
}
582587

583588
// verify if (type) and (params)/(result) match, if both are specified
584589
if (typeExists && paramOrResultExists) {
@@ -597,8 +602,8 @@ size_t SExpressionWasmBuilder::parseTypeUse(Element& s,
597602
}
598603
}
599604

600-
// If (type) does not exist, check if there's a matching type, and if there
601-
// isn't, create one.
605+
// If only (param)/(result) is specified, check if there's a matching type,
606+
// and if there isn't, create one.
602607
if (!typeExists) {
603608
bool need = true;
604609
std::vector<Type> params;
@@ -617,6 +622,17 @@ size_t SExpressionWasmBuilder::parseTypeUse(Element& s,
617622
}
618623
}
619624

625+
// If only (type) is specified, populate params and result.
626+
if (!paramOrResultExists) {
627+
assert(functionType);
628+
result = functionType->result;
629+
for (size_t index = 0, e = functionType->params.size(); index < e;
630+
index++) {
631+
Type type = functionType->params[index];
632+
namedParams.emplace_back(Name::fromInt(index), type);
633+
}
634+
}
635+
620636
return i;
621637
}
622638

@@ -661,7 +677,6 @@ void SExpressionWasmBuilder::preParseFunctionType(Element& s) {
661677
functionNames.push_back(name);
662678
functionCounter++;
663679
FunctionType* type = nullptr;
664-
functionTypes[name] = none;
665680
std::vector<Type> params;
666681
parseTypeUse(s, i, type);
667682
assert(type && "type should've been set by parseTypeUse");
@@ -775,13 +790,6 @@ void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) {
775790
throw ParseException("preParseImport in func");
776791
}
777792

778-
// in case only (type) is specified, populate params and result with temporary
779-
// names
780-
if (params.empty()) {
781-
for (size_t j = 0; j < functionType->params.size(); j++) {
782-
params.emplace_back(Name::fromInt(j), functionType->params[j]);
783-
}
784-
}
785793
result = functionType->result;
786794
size_t localIndex = params.size(); // local index for params and locals
787795

0 commit comments

Comments
 (0)