Move some output stream parameters forward.

This allows us to avoid removing parameter defaults for these functions.
This commit is contained in:
Michael Hansen
2023-06-05 13:56:25 -07:00
parent 46ea76c0e9
commit bf3599c87a
6 changed files with 54 additions and 45 deletions

View File

@@ -14,8 +14,8 @@
// NOTE: Nested f-strings not supported.
#define F_STRING_QUOTE "'''"
static void append_to_chain_store(PycRef<ASTNode> &chainStore, PycRef<ASTNode> item,
FastStack& stack, PycRef<ASTBlock> &curblock);
static void append_to_chain_store(const PycRef<ASTNode>& chainStore,
PycRef<ASTNode> item, FastStack& stack, const PycRef<ASTBlock>& curblock);
/* Use this to determine if an error occurred (and therefore, if we should
* avoid cleaning the output tree) */
@@ -2398,8 +2398,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
return new ASTNodeList(defblock->nodes());
}
static void append_to_chain_store(PycRef<ASTNode> &chainStore, PycRef<ASTNode> item,
FastStack& stack, PycRef<ASTBlock> &curblock)
static void append_to_chain_store(const PycRef<ASTNode> &chainStore,
PycRef<ASTNode> item, FastStack& stack, const PycRef<ASTBlock>& curblock)
{
stack.pop(); // ignore identical source object.
chainStore.cast<ASTChainStore>()->append(item);
@@ -2502,7 +2502,9 @@ static void end_line(std::ostream& pyc_output)
}
int cur_indent = -1;
static void print_block(PycRef<ASTBlock> blk, PycModule* mod, std::ostream& pyc_output) {
static void print_block(PycRef<ASTBlock> 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<ASTBlock> blk, PycModule* mod, std::ostream& pyc_
}
}
void print_formatted_value(PycRef<ASTFormattedValue> formatted_value, PycModule* mod, std::ostream& pyc_output)
void print_formatted_value(PycRef<ASTFormattedValue> 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<ASTFormattedValue> formatted_value, PycModule*
pyc_output << "!a";
break;
case ASTFormattedValue::ConversionFlag::FMTSPEC:
pyc_output << ":" << formatted_value->format_spec().cast<ASTObject>()->object().cast<PycString>()->value();
pyc_output << ":" << formatted_value->format_spec().cast<ASTObject>()->object().cast<PycString>()->value();
break;
default:
fprintf(stderr, "Unsupported NODE_FORMATTEDVALUE conversion flag: %d\n", formatted_value->conversion());
@@ -2548,7 +2551,7 @@ void print_formatted_value(PycRef<ASTFormattedValue> formatted_value, PycModule*
pyc_output << "}";
}
void print_src(PycRef<ASTNode> node, PycModule* mod, std::ostream &pyc_output)
void print_src(PycRef<ASTNode> node, PycModule* mod, std::ostream& pyc_output)
{
if (node == NULL) {
pyc_output << "None";
@@ -2653,7 +2656,7 @@ void print_src(PycRef<ASTNode> 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<ASTObject>()->object(), mod, F_STRING_QUOTE, pyc_output);
print_const(pyc_output, val.cast<ASTObject>()->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<ASTNode> node, PycModule* mod, std::ostream &pyc_output)
PycRef<PycCode> code = obj.cast<PycCode>();
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<ASTNode> node, PycModule* mod, std::ostream &pyc_output)
cleanBuild = true;
}
bool print_docstring(PycRef<PycObject> obj, int indent, PycModule* mod, std::ostream& pyc_output)
bool print_docstring(PycRef<PycObject> 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<PycObject> obj, int indent, PycModule* mod, std::ost
}
if (prefix != -1) {
start_line(indent, pyc_output);
OutputString(obj.cast<PycString>(), prefix, true, pyc_output);
OutputString(pyc_output, obj.cast<PycString>(), prefix, true);
pyc_output << "\n";
return true;
} else

View File

@@ -160,7 +160,8 @@ bool Pyc::IsCompareArg(int opcode)
return (opcode == Pyc::COMPARE_OP_A);
}
void print_const(PycRef<PycObject> obj, PycModule* mod, const char* parent_f_string_quote, std::ostream& pyc_output)
void print_const(std::ostream& pyc_output, PycRef<PycObject> obj, PycModule* mod,
const char* parent_f_string_quote)
{
if (obj == NULL) {
pyc_output << "<NULL>";
@@ -169,12 +170,12 @@ void print_const(PycRef<PycObject> obj, PycModule* mod, const char* parent_f_str
switch (obj->type()) {
case PycObject::TYPE_STRING:
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0,
false, pyc_output, parent_f_string_quote);
OutputString(pyc_output, obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0,
false, parent_f_string_quote);
break;
case PycObject::TYPE_UNICODE:
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 0 : 'u',
false, pyc_output, parent_f_string_quote);
OutputString(pyc_output, obj.cast<PycString>(), 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<PycObject> 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<PycString>(), 0, false, pyc_output, parent_f_string_quote);
OutputString(pyc_output, obj.cast<PycString>(), 0, false, parent_f_string_quote);
else
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0,
false, pyc_output, parent_f_string_quote);
OutputString(pyc_output, obj.cast<PycString>(), 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<PycObject> obj, PycModule* mod, const char* parent_f_str
PycTuple::value_t values = obj.cast<PycTuple>()->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<PycObject> obj, PycModule* mod, const char* parent_f_str
PycList::value_t values = obj.cast<PycList>()->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<PycObject> 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<PycObject> obj, PycModule* mod, const char* parent_f_str
PycSet::value_t values = obj.cast<PycSet>()->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<PycObject> obj, PycModule* mod, const char* parent_f_str
PycSet::value_t values = obj.cast<PycSet>()->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<PycObject> obj, PycModule* mod, const char* parent_f_str
break;
case PycObject::TYPE_CODE:
case PycObject::TYPE_CODE2:
formatted_print(pyc_output, "<CODE> %s", obj.cast<PycCode>()->name()->value());
pyc_output << "<CODE> " << obj.cast<PycCode>()->name()->value();
break;
default:
formatted_print(pyc_output, "<TYPE: %d>\n", obj->type());
@@ -363,7 +364,8 @@ void bc_next(PycBuffer& source, PycModule* mod, int& opcode, int& operand, int&
}
}
void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent, unsigned flags, std::ostream &pyc_output)
void bc_disasm(std::ostream& pyc_output, PycRef<PycCode> 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<PycCode> 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 <INVALID>", operand);
}

View File

@@ -35,6 +35,8 @@ bool IsCompareArg(int opcode);
}
void print_const(PycRef<PycObject> obj, PycModule* mod, const char* parent_f_string_quote, std::ostream& pyc_output);
void print_const(std::ostream& pyc_output, PycRef<PycObject> 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<PycCode> code, PycModule* mod, int indent, unsigned flags, std::ostream& pyc_output);
void bc_disasm(std::ostream& pyc_output, PycRef<PycCode> code, PycModule* mod,
int indent, unsigned flags);

View File

@@ -56,7 +56,8 @@ bool PycString::isEqual(PycRef<PycObject> obj) const
return isEqual(strObj->m_value);
}
void OutputString(PycRef<PycString> str, char prefix, bool triple, std::ostream &pyc_output, const char* parent_f_string_quote)
void OutputString(std::ostream &pyc_output, PycRef<PycString> str, char prefix,
bool triple, const char* parent_f_string_quote)
{
if (prefix != 0)
pyc_output << prefix;

View File

@@ -31,7 +31,7 @@ private:
std::string m_value;
};
void OutputString(PycRef<PycString> str, char prefix, bool triple,
std::ostream& stream, const char* parent_f_string_quote = nullptr);
void OutputString(std::ostream& stream, PycRef<PycString> str, char prefix,
bool triple = false, const char* parent_f_string_quote = nullptr);
#endif

View File

@@ -139,7 +139,7 @@ void output_object(PycRef<PycObject> 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<PycObject> obj, PycModule* mod, int indent,
break;
case PycObject::TYPE_STRING:
iputs(pyc_output, indent, "");
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0, false, pyc_output);
OutputString(pyc_output, obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0);
pyc_output << "\n";
break;
case PycObject::TYPE_UNICODE:
iputs(pyc_output, indent, "");
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 0 : 'u', false, pyc_output);
OutputString(pyc_output, obj.cast<PycString>(), mod->strIsUnicode() ? 0 : 'u');
pyc_output << "\n";
break;
case PycObject::TYPE_INTERNED:
@@ -169,9 +169,9 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent,
case PycObject::TYPE_SHORT_ASCII_INTERNED:
iputs(pyc_output, indent, "");
if (mod->majorVer() >= 3)
OutputString(obj.cast<PycString>(), 0, false, pyc_output);
OutputString(pyc_output, obj.cast<PycString>(), 0);
else
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0, false, pyc_output);
OutputString(pyc_output, obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0);
pyc_output << "\n";
break;
case PycObject::TYPE_TUPLE: