diff options
author | Heejin Ahn <aheejin@gmail.com> | 2019-05-29 16:46:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-29 16:46:30 -0700 |
commit | fc54b7c9fbbf3b87339bd55b86fdb51afae97c9c (patch) | |
tree | d0a03249008cdbffc833b362f15c553cd3f841f4 /src/wasm/wasm-s-parser.cpp | |
parent | b60dad3428ae61efe8ed38d0f2ad5a45081d4c39 (diff) | |
download | binaryen-fc54b7c9fbbf3b87339bd55b86fdb51afae97c9c.tar.gz binaryen-fc54b7c9fbbf3b87339bd55b86fdb51afae97c9c.tar.bz2 binaryen-fc54b7c9fbbf3b87339bd55b86fdb51afae97c9c.zip |
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.
Diffstat (limited to 'src/wasm/wasm-s-parser.cpp')
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index c3a8fb940..3a5dfbced 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -579,6 +579,11 @@ size_t SExpressionWasmBuilder::parseTypeUse(Element& s, paramOrResultExists = true; result = parseResult(*s[i++]); } + // If none of type/param/result exists, this is equivalent to a type that does + // not have parameters and returns nothing. + if (!typeExists && !paramOrResultExists) { + paramOrResultExists = true; + } // verify if (type) and (params)/(result) match, if both are specified if (typeExists && paramOrResultExists) { @@ -597,8 +602,8 @@ size_t SExpressionWasmBuilder::parseTypeUse(Element& s, } } - // If (type) does not exist, check if there's a matching type, and if there - // isn't, create one. + // If only (param)/(result) is specified, check if there's a matching type, + // and if there isn't, create one. if (!typeExists) { bool need = true; std::vector<Type> params; @@ -617,6 +622,17 @@ size_t SExpressionWasmBuilder::parseTypeUse(Element& s, } } + // If only (type) is specified, populate params and result. + if (!paramOrResultExists) { + assert(functionType); + result = functionType->result; + for (size_t index = 0, e = functionType->params.size(); index < e; + index++) { + Type type = functionType->params[index]; + namedParams.emplace_back(Name::fromInt(index), type); + } + } + return i; } @@ -661,7 +677,6 @@ void SExpressionWasmBuilder::preParseFunctionType(Element& s) { functionNames.push_back(name); functionCounter++; FunctionType* type = nullptr; - functionTypes[name] = none; std::vector<Type> params; parseTypeUse(s, i, type); assert(type && "type should've been set by parseTypeUse"); @@ -775,13 +790,6 @@ void SExpressionWasmBuilder::parseFunction(Element& s, bool preParseImport) { throw ParseException("preParseImport in func"); } - // in case only (type) is specified, populate params and result with temporary - // names - if (params.empty()) { - for (size_t j = 0; j < functionType->params.size(); j++) { - params.emplace_back(Name::fromInt(j), functionType->params[j]); - } - } result = functionType->result; size_t localIndex = params.size(); // local index for params and locals |