Skip to content

Commit

Permalink
Move some output stream parameters forward.
Browse files Browse the repository at this point in the history
This allows us to avoid removing parameter defaults for these functions.
  • Loading branch information
zrax committed Jun 5, 2023
1 parent 46ea76c commit bf3599c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
28 changes: 16 additions & 12 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -2540,15 +2543,15 @@ 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());
}
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";
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
48 changes: 25 additions & 23 deletions bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>";
Expand All @@ -169,23 +170,23 @@ 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:
case PycObject::TYPE_ASCII_INTERNED:
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:
Expand All @@ -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)
Expand All @@ -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 << "]";
Expand All @@ -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 << "}";
Expand All @@ -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 << "}";
Expand All @@ -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 << "})";
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
3 changes: 2 additions & 1 deletion pyc_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions pyc_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PycString : public PycObject {
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
10 changes: 5 additions & 5 deletions pycdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit bf3599c

Please sign in to comment.