summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-11-12 20:52:55 -0500
committerGitHub <noreply@github.com>2024-11-12 17:52:55 -0800
commit24b5bdfd1bf550dae38a7a990379b315d9f11bae (patch)
tree0d8386080bb8666eae27785e0cdf993033d27348 /src
parent496d92bfcf848d7c888b83cc354819ed87ba0b87 (diff)
downloadbinaryen-24b5bdfd1bf550dae38a7a990379b315d9f11bae.tar.gz
binaryen-24b5bdfd1bf550dae38a7a990379b315d9f11bae.tar.bz2
binaryen-24b5bdfd1bf550dae38a7a990379b315d9f11bae.zip
Consolidate printing of function signatures (#7073)
There were previously two separate code paths for printing function signatures, one for imported functions and one for declared functions. The only intended difference was that parameter names were printed for declared functions but not for imported functions. Reduce duplication by consolidating the code paths, and add support for printing names for imported function parameters that have them. Also fix a bug where empty names were printed as `$` rather than the correct `$""`.
Diffstat (limited to 'src')
-rw-r--r--src/passes/Print.cpp99
-rw-r--r--src/support/name.cpp2
2 files changed, 44 insertions, 57 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 854ea0b5b..5a5d01299 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -382,7 +382,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
}
// Module-level visitors
- void handleSignature(HeapType curr, Name name = Name());
+ void handleSignature(Function* curr, bool printImplicitNames = false);
void visitExport(Export* curr);
void emitImportHeader(Importable* curr);
void visitGlobal(Global* curr);
@@ -2883,41 +2883,51 @@ static bool requiresExplicitFuncType(HeapType type) {
return type.isOpen() || type.isShared() || type.getRecGroup().size() > 1;
}
-void PrintSExpression::handleSignature(HeapType curr, Name name) {
- Signature sig = curr.getSignature();
- o << "(func";
- if (name.is()) {
- o << ' ';
- name.print(o);
- if ((currModule && currModule->features.hasGC()) ||
- requiresExplicitFuncType(curr)) {
- o << " (type ";
- printHeapType(curr) << ')';
- }
+void PrintSExpression::handleSignature(Function* curr,
+ bool printImplicitNames) {
+ o << '(';
+ printMajor(o, "func ");
+ curr->name.print(o);
+ if ((currModule && currModule->features.hasGC()) ||
+ requiresExplicitFuncType(curr->type)) {
+ o << " (type ";
+ printHeapType(curr->type) << ')';
}
- if (sig.params.size() > 0) {
- o << maybeSpace;
- o << "(param ";
- auto sep = "";
- for (auto type : sig.params) {
- o << sep;
- printType(type);
- sep = " ";
+ bool inParam = false;
+ Index i = 0;
+ for (const auto& param : curr->getParams()) {
+ auto hasName = printImplicitNames || curr->hasLocalName(i);
+ if (hasName && inParam) {
+ o << ')' << maybeSpace;
+ inParam = false;
+ } else if (inParam) {
+ o << ' ';
+ } else {
+ o << maybeSpace;
+ }
+ if (!inParam) {
+ o << '(';
+ printMinor(o, "param ");
+ inParam = true;
+ }
+ if (hasName) {
+ printLocal(i, currFunction, o);
+ o << ' ';
+ }
+ printType(param);
+ if (hasName) {
+ o << ')';
+ inParam = false;
}
+ ++i;
+ }
+ if (inParam) {
o << ')';
}
- if (sig.results.size() > 0) {
+ if (curr->getResults() != Type::none) {
o << maybeSpace;
- o << "(result ";
- auto sep = "";
- for (auto type : sig.results) {
- o << sep;
- printType(type);
- sep = " ";
- }
- o << ')';
+ printResultType(curr->getResults());
}
- o << ")";
}
void PrintSExpression::visitExport(Export* curr) {
@@ -3014,8 +3024,8 @@ void PrintSExpression::visitImportedFunction(Function* curr) {
lastPrintedLocation = std::nullopt;
o << '(';
emitImportHeader(curr);
- handleSignature(curr->type, curr->name);
- o << ')';
+ handleSignature(curr);
+ o << "))";
o << maybeNewLine;
}
@@ -3027,30 +3037,7 @@ void PrintSExpression::visitDefinedFunction(Function* curr) {
if (currFunction->prologLocation.size()) {
printDebugLocation(*currFunction->prologLocation.begin());
}
- o << '(';
- printMajor(o, "func ");
- curr->name.print(o);
- if ((currModule && currModule->features.hasGC()) ||
- requiresExplicitFuncType(curr->type)) {
- o << " (type ";
- printHeapType(curr->type) << ')';
- }
- if (curr->getParams().size() > 0) {
- Index i = 0;
- for (const auto& param : curr->getParams()) {
- o << maybeSpace;
- o << '(';
- printMinor(o, "param ");
- printLocal(i, currFunction, o);
- o << ' ';
- printType(param) << ')';
- ++i;
- }
- }
- if (curr->getResults() != Type::none) {
- o << maybeSpace;
- printResultType(curr->getResults());
- }
+ handleSignature(curr, true);
incIndent();
for (size_t i = curr->getVarIndexBase(); i < curr->getNumLocals(); i++) {
doIndent(o, indent);
diff --git a/src/support/name.cpp b/src/support/name.cpp
index 4e53e3f83..4c599defc 100644
--- a/src/support/name.cpp
+++ b/src/support/name.cpp
@@ -46,7 +46,7 @@ std::ostream& Name::print(std::ostream& o) const {
// TODO: This is not spec-compliant since the spec does not yet support
// quoted identifiers and has a limited set of valid idchars.
o << '$';
- if (std::all_of(str.begin(), str.end(), isIDChar)) {
+ if (size() >= 1 && std::all_of(str.begin(), str.end(), isIDChar)) {
return o << str;
} else {
return String::printEscaped(o, str);