From bf3599c87a73691786d634a2afad3eaa16787b8f Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Mon, 5 Jun 2023 13:56:25 -0700 Subject: [PATCH] Move some output stream parameters forward. This allows us to avoid removing parameter defaults for these functions. --- ASTree.cpp | 28 ++++++++++++++++------------ bytecode.cpp | 48 +++++++++++++++++++++++++----------------------- bytecode.h | 6 ++++-- pyc_string.cpp | 3 ++- pyc_string.h | 4 ++-- pycdas.cpp | 10 +++++----- 6 files changed, 54 insertions(+), 45 deletions(-) diff --git a/ASTree.cpp b/ASTree.cpp index dfba04c8e..e7c926286 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -14,8 +14,8 @@ // NOTE: Nested f-strings not supported. #define F_STRING_QUOTE "'''" -static void append_to_chain_store(PycRef &chainStore, PycRef item, - FastStack& stack, PycRef &curblock); +static void append_to_chain_store(const PycRef& chainStore, + PycRef item, FastStack& stack, const PycRef& curblock); /* Use this to determine if an error occurred (and therefore, if we should * avoid cleaning the output tree) */ @@ -2398,8 +2398,8 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) return new ASTNodeList(defblock->nodes()); } -static void append_to_chain_store(PycRef &chainStore, PycRef item, - FastStack& stack, PycRef &curblock) +static void append_to_chain_store(const PycRef &chainStore, + PycRef item, FastStack& stack, const PycRef& curblock) { stack.pop(); // ignore identical source object. chainStore.cast()->append(item); @@ -2502,7 +2502,9 @@ static void end_line(std::ostream& pyc_output) } int cur_indent = -1; -static void print_block(PycRef blk, PycModule* mod, std::ostream& pyc_output) { +static void print_block(PycRef blk, PycModule* mod, + std::ostream& pyc_output) +{ ASTBlock::list_t lines = blk->nodes(); if (lines.size() == 0) { @@ -2522,7 +2524,8 @@ static void print_block(PycRef blk, PycModule* mod, std::ostream& pyc_ } } -void print_formatted_value(PycRef formatted_value, PycModule* mod, std::ostream& pyc_output) +void print_formatted_value(PycRef formatted_value, PycModule* mod, + std::ostream& pyc_output) { pyc_output << "{"; print_src(formatted_value->val(), mod, pyc_output); @@ -2540,7 +2543,7 @@ void print_formatted_value(PycRef formatted_value, PycModule* pyc_output << "!a"; break; case ASTFormattedValue::ConversionFlag::FMTSPEC: - pyc_output << ":" << formatted_value->format_spec().cast()->object().cast()->value(); + pyc_output << ":" << formatted_value->format_spec().cast()->object().cast()->value(); break; default: fprintf(stderr, "Unsupported NODE_FORMATTEDVALUE conversion flag: %d\n", formatted_value->conversion()); @@ -2548,7 +2551,7 @@ void print_formatted_value(PycRef formatted_value, PycModule* pyc_output << "}"; } -void print_src(PycRef node, PycModule* mod, std::ostream &pyc_output) +void print_src(PycRef node, PycModule* mod, std::ostream& pyc_output) { if (node == NULL) { pyc_output << "None"; @@ -2653,7 +2656,7 @@ void print_src(PycRef node, PycModule* mod, std::ostream &pyc_output) case ASTNode::NODE_OBJECT: // When printing a piece of the f-string, keep the quote style consistent. // This avoids problems when ''' or """ is part of the string. - print_const(val.cast()->object(), mod, F_STRING_QUOTE, pyc_output); + print_const(pyc_output, val.cast()->object(), mod, F_STRING_QUOTE); break; default: fprintf(stderr, "Unsupported node type %d in NODE_JOINEDSTR\n", val.type()); @@ -2829,7 +2832,7 @@ void print_src(PycRef node, PycModule* mod, std::ostream &pyc_output) PycRef code = obj.cast(); decompyle(code, mod, pyc_output); } else { - print_const(obj, mod, nullptr, pyc_output); + print_const(pyc_output, obj, mod); } } break; @@ -3217,7 +3220,8 @@ void print_src(PycRef node, PycModule* mod, std::ostream &pyc_output) cleanBuild = true; } -bool print_docstring(PycRef obj, int indent, PycModule* mod, std::ostream& pyc_output) +bool print_docstring(PycRef obj, int indent, PycModule* mod, + std::ostream& pyc_output) { // docstrings are translated from the bytecode __doc__ = 'string' to simply '''string''' signed char prefix = -1; @@ -3241,7 +3245,7 @@ bool print_docstring(PycRef obj, int indent, PycModule* mod, std::ost } if (prefix != -1) { start_line(indent, pyc_output); - OutputString(obj.cast(), prefix, true, pyc_output); + OutputString(pyc_output, obj.cast(), prefix, true); pyc_output << "\n"; return true; } else diff --git a/bytecode.cpp b/bytecode.cpp index d663350d0..b3bb13652 100644 --- a/bytecode.cpp +++ b/bytecode.cpp @@ -160,7 +160,8 @@ bool Pyc::IsCompareArg(int opcode) return (opcode == Pyc::COMPARE_OP_A); } -void print_const(PycRef obj, PycModule* mod, const char* parent_f_string_quote, std::ostream& pyc_output) +void print_const(std::ostream& pyc_output, PycRef obj, PycModule* mod, + const char* parent_f_string_quote) { if (obj == NULL) { pyc_output << ""; @@ -169,12 +170,12 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str switch (obj->type()) { case PycObject::TYPE_STRING: - OutputString(obj.cast(), mod->strIsUnicode() ? 'b' : 0, - false, pyc_output, parent_f_string_quote); + OutputString(pyc_output, obj.cast(), mod->strIsUnicode() ? 'b' : 0, + false, parent_f_string_quote); break; case PycObject::TYPE_UNICODE: - OutputString(obj.cast(), mod->strIsUnicode() ? 0 : 'u', - false, pyc_output, parent_f_string_quote); + OutputString(pyc_output, obj.cast(), mod->strIsUnicode() ? 0 : 'u', + false, parent_f_string_quote); break; case PycObject::TYPE_INTERNED: case PycObject::TYPE_ASCII: @@ -182,10 +183,10 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str case PycObject::TYPE_SHORT_ASCII: case PycObject::TYPE_SHORT_ASCII_INTERNED: if (mod->majorVer() >= 3) - OutputString(obj.cast(), 0, false, pyc_output, parent_f_string_quote); + OutputString(pyc_output, obj.cast(), 0, false, parent_f_string_quote); else - OutputString(obj.cast(), mod->strIsUnicode() ? 'b' : 0, - false, pyc_output, parent_f_string_quote); + OutputString(pyc_output, obj.cast(), mod->strIsUnicode() ? 'b' : 0, + false, parent_f_string_quote); break; case PycObject::TYPE_TUPLE: case PycObject::TYPE_SMALL_TUPLE: @@ -194,10 +195,10 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str PycTuple::value_t values = obj.cast()->values(); auto it = values.cbegin(); if (it != values.cend()) { - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); while (++it != values.cend()) { pyc_output << ", "; - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); } } if (values.size() == 1) @@ -212,10 +213,10 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str PycList::value_t values = obj.cast()->values(); auto it = values.cbegin(); if (it != values.cend()) { - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); while (++it != values.cend()) { pyc_output << ", "; - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); } } pyc_output << "]"; @@ -229,15 +230,15 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str auto ki = keys.cbegin(); auto vi = values.cbegin(); if (ki != keys.cend()) { - print_const(*ki, mod, nullptr, pyc_output); + print_const(pyc_output, *ki, mod); pyc_output << ": "; - print_const(*vi, mod, nullptr, pyc_output); + print_const(pyc_output, *vi, mod); while (++ki != keys.cend()) { ++vi; pyc_output << ", "; - print_const(*ki, mod, nullptr, pyc_output); + print_const(pyc_output, *ki, mod); pyc_output << ": "; - print_const(*vi, mod, nullptr, pyc_output); + print_const(pyc_output, *vi, mod); } } pyc_output << "}"; @@ -249,10 +250,10 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str PycSet::value_t values = obj.cast()->values(); auto it = values.cbegin(); if (it != values.cend()) { - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); while (++it != values.cend()) { pyc_output << ", "; - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); } } pyc_output << "}"; @@ -264,10 +265,10 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str PycSet::value_t values = obj.cast()->values(); auto it = values.cbegin(); if (it != values.cend()) { - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); while (++it != values.cend()) { pyc_output << ", "; - print_const(*it, mod, nullptr, pyc_output); + print_const(pyc_output, *it, mod); } } pyc_output << "})"; @@ -326,7 +327,7 @@ void print_const(PycRef obj, PycModule* mod, const char* parent_f_str break; case PycObject::TYPE_CODE: case PycObject::TYPE_CODE2: - formatted_print(pyc_output, " %s", obj.cast()->name()->value()); + pyc_output << " " << obj.cast()->name()->value(); break; default: formatted_print(pyc_output, "\n", obj->type()); @@ -363,7 +364,8 @@ void bc_next(PycBuffer& source, PycModule* mod, int& opcode, int& operand, int& } } -void bc_disasm(PycRef code, PycModule* mod, int indent, unsigned flags, std::ostream &pyc_output) +void bc_disasm(std::ostream& pyc_output, PycRef code, PycModule* mod, + int indent, unsigned flags) { static const char *cmp_strings[] = { "<", "<=", "==", "!=", ">", ">=", "in", "not in", "is", "is not", @@ -396,7 +398,7 @@ void bc_disasm(PycRef code, PycModule* mod, int indent, unsigned flags, try { auto constParam = code->getConst(operand); formatted_print(pyc_output, "%d: ", operand); - print_const(constParam, mod, nullptr, pyc_output); + print_const(pyc_output, constParam, mod); } catch (const std::out_of_range &) { formatted_print(pyc_output, "%d ", operand); } diff --git a/bytecode.h b/bytecode.h index 09fb19e62..f35376afd 100644 --- a/bytecode.h +++ b/bytecode.h @@ -35,6 +35,8 @@ bool IsCompareArg(int opcode); } -void print_const(PycRef obj, PycModule* mod, const char* parent_f_string_quote, std::ostream& pyc_output); +void print_const(std::ostream& pyc_output, PycRef obj, PycModule* mod, + const char* parent_f_string_quote = nullptr); void bc_next(PycBuffer& source, PycModule* mod, int& opcode, int& operand, int& pos); -void bc_disasm(PycRef code, PycModule* mod, int indent, unsigned flags, std::ostream& pyc_output); +void bc_disasm(std::ostream& pyc_output, PycRef code, PycModule* mod, + int indent, unsigned flags); diff --git a/pyc_string.cpp b/pyc_string.cpp index cf8985630..69287c611 100644 --- a/pyc_string.cpp +++ b/pyc_string.cpp @@ -56,7 +56,8 @@ bool PycString::isEqual(PycRef obj) const return isEqual(strObj->m_value); } -void OutputString(PycRef str, char prefix, bool triple, std::ostream &pyc_output, const char* parent_f_string_quote) +void OutputString(std::ostream &pyc_output, PycRef str, char prefix, + bool triple, const char* parent_f_string_quote) { if (prefix != 0) pyc_output << prefix; diff --git a/pyc_string.h b/pyc_string.h index 886cce2db..c01e3c979 100644 --- a/pyc_string.h +++ b/pyc_string.h @@ -31,7 +31,7 @@ class PycString : public PycObject { std::string m_value; }; -void OutputString(PycRef str, char prefix, bool triple, - std::ostream& stream, const char* parent_f_string_quote = nullptr); +void OutputString(std::ostream& stream, PycRef str, char prefix, + bool triple = false, const char* parent_f_string_quote = nullptr); #endif diff --git a/pycdas.cpp b/pycdas.cpp index a47f786f4..f827ed505 100644 --- a/pycdas.cpp +++ b/pycdas.cpp @@ -139,7 +139,7 @@ void output_object(PycRef obj, PycModule* mod, int indent, output_object(codeObj->consts()->get(i), mod, indent + 2, flags, pyc_output); iputs(pyc_output, indent + 1, "[Disassembly]\n"); - bc_disasm(codeObj, mod, indent + 2, flags, pyc_output); + bc_disasm(pyc_output, codeObj, mod, indent + 2, flags); if (mod->verCompare(1, 5) >= 0 && (flags & Pyc::DISASM_PYCODE_VERBOSE) != 0) { iputs(pyc_output, indent + 1, "[Line Number Table]\n"); @@ -154,12 +154,12 @@ void output_object(PycRef obj, PycModule* mod, int indent, break; case PycObject::TYPE_STRING: iputs(pyc_output, indent, ""); - OutputString(obj.cast(), mod->strIsUnicode() ? 'b' : 0, false, pyc_output); + OutputString(pyc_output, obj.cast(), mod->strIsUnicode() ? 'b' : 0); pyc_output << "\n"; break; case PycObject::TYPE_UNICODE: iputs(pyc_output, indent, ""); - OutputString(obj.cast(), mod->strIsUnicode() ? 0 : 'u', false, pyc_output); + OutputString(pyc_output, obj.cast(), mod->strIsUnicode() ? 0 : 'u'); pyc_output << "\n"; break; case PycObject::TYPE_INTERNED: @@ -169,9 +169,9 @@ void output_object(PycRef obj, PycModule* mod, int indent, case PycObject::TYPE_SHORT_ASCII_INTERNED: iputs(pyc_output, indent, ""); if (mod->majorVer() >= 3) - OutputString(obj.cast(), 0, false, pyc_output); + OutputString(pyc_output, obj.cast(), 0); else - OutputString(obj.cast(), mod->strIsUnicode() ? 'b' : 0, false, pyc_output); + OutputString(pyc_output, obj.cast(), mod->strIsUnicode() ? 'b' : 0); pyc_output << "\n"; break; case PycObject::TYPE_TUPLE: