Commit fe6885a6 authored by Marius Wachtler's avatar Marius Wachtler

PrintVisitor: use raw_ostream

parent 1782dd56
...@@ -1060,14 +1060,14 @@ void print_ast(AST* ast) { ...@@ -1060,14 +1060,14 @@ void print_ast(AST* ast) {
void PrintVisitor::printIndent() { void PrintVisitor::printIndent() {
for (int i = 0; i < indent; i++) { for (int i = 0; i < indent; i++) {
putchar(' '); stream << ' ';
} }
} }
bool PrintVisitor::visit_alias(AST_alias* node) { bool PrintVisitor::visit_alias(AST_alias* node) {
printf("%s", node->name.c_str()); stream << node->name.s();
if (node->asname.s().size()) if (node->asname.s().size())
printf(" as %s", node->asname.c_str()); stream << " as " << node->asname.s();
return true; return true;
} }
...@@ -1076,11 +1076,11 @@ bool PrintVisitor::visit_arguments(AST_arguments* node) { ...@@ -1076,11 +1076,11 @@ bool PrintVisitor::visit_arguments(AST_arguments* node) {
int ndefault = node->defaults.size(); int ndefault = node->defaults.size();
for (int i = 0; i < nargs; i++) { for (int i = 0; i < nargs; i++) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->args[i]->accept(this); node->args[i]->accept(this);
if (i >= nargs - ndefault) { if (i >= nargs - ndefault) {
printf("="); stream << "=";
node->defaults[i - (nargs - ndefault)]->accept(this); node->defaults[i - (nargs - ndefault)]->accept(this);
} }
} }
...@@ -1088,10 +1088,10 @@ bool PrintVisitor::visit_arguments(AST_arguments* node) { ...@@ -1088,10 +1088,10 @@ bool PrintVisitor::visit_arguments(AST_arguments* node) {
} }
bool PrintVisitor::visit_assert(AST_Assert* node) { bool PrintVisitor::visit_assert(AST_Assert* node) {
printf("assert "); stream << "assert ";
node->test->accept(this); node->test->accept(this);
if (node->msg) { if (node->msg) {
printf(", "); stream << ", ";
node->msg->accept(this); node->msg->accept(this);
} }
return true; return true;
...@@ -1100,49 +1100,49 @@ bool PrintVisitor::visit_assert(AST_Assert* node) { ...@@ -1100,49 +1100,49 @@ bool PrintVisitor::visit_assert(AST_Assert* node) {
bool PrintVisitor::visit_assign(AST_Assign* node) { bool PrintVisitor::visit_assign(AST_Assign* node) {
for (int i = 0; i < node->targets.size(); i++) { for (int i = 0; i < node->targets.size(); i++) {
node->targets[i]->accept(this); node->targets[i]->accept(this);
printf(" = "); stream << " = ";
} }
node->value->accept(this); node->value->accept(this);
return true; return true;
} }
static void printOp(AST_TYPE::AST_TYPE op_type) { void PrintVisitor::printOp(AST_TYPE::AST_TYPE op_type) {
switch (op_type) { switch (op_type) {
case AST_TYPE::Add: case AST_TYPE::Add:
putchar('+'); stream << '+';
break; break;
case AST_TYPE::BitAnd: case AST_TYPE::BitAnd:
putchar('&'); stream << '&';
break; break;
case AST_TYPE::BitOr: case AST_TYPE::BitOr:
putchar('|'); stream << '|';
break; break;
case AST_TYPE::BitXor: case AST_TYPE::BitXor:
putchar('^'); stream << '^';
break; break;
case AST_TYPE::Div: case AST_TYPE::Div:
putchar('/'); stream << '/';
break; break;
case AST_TYPE::LShift: case AST_TYPE::LShift:
printf("<<"); stream << "<<";
break; break;
case AST_TYPE::RShift: case AST_TYPE::RShift:
printf(">>"); stream << ">>";
break; break;
case AST_TYPE::Pow: case AST_TYPE::Pow:
printf("**"); stream << "**";
break; break;
case AST_TYPE::Mod: case AST_TYPE::Mod:
putchar('%'); stream << '%';
break; break;
case AST_TYPE::Mult: case AST_TYPE::Mult:
putchar('*'); stream << '*';
break; break;
case AST_TYPE::Sub: case AST_TYPE::Sub:
putchar('-'); stream << '-';
break; break;
default: default:
printf("<%d>", op_type); stream << "<" << (int)op_type << ">";
break; break;
} }
} }
...@@ -1150,14 +1150,14 @@ static void printOp(AST_TYPE::AST_TYPE op_type) { ...@@ -1150,14 +1150,14 @@ static void printOp(AST_TYPE::AST_TYPE op_type) {
bool PrintVisitor::visit_augassign(AST_AugAssign* node) { bool PrintVisitor::visit_augassign(AST_AugAssign* node) {
node->target->accept(this); node->target->accept(this);
printOp(node->op_type); printOp(node->op_type);
putchar('='); stream << '=';
node->value->accept(this); node->value->accept(this);
return true; return true;
} }
bool PrintVisitor::visit_augbinop(AST_AugBinOp* node) { bool PrintVisitor::visit_augbinop(AST_AugBinOp* node) {
node->left->accept(this); node->left->accept(this);
printf("="); stream << '=';
printOp(node->op_type); printOp(node->op_type);
node->right->accept(this); node->right->accept(this);
return true; return true;
...@@ -1165,8 +1165,8 @@ bool PrintVisitor::visit_augbinop(AST_AugBinOp* node) { ...@@ -1165,8 +1165,8 @@ bool PrintVisitor::visit_augbinop(AST_AugBinOp* node) {
bool PrintVisitor::visit_attribute(AST_Attribute* node) { bool PrintVisitor::visit_attribute(AST_Attribute* node) {
node->value->accept(this); node->value->accept(this);
putchar('.'); stream << '.';
printf("%s", node->attr.c_str()); stream << node->attr.s();
return true; return true;
} }
...@@ -1185,10 +1185,10 @@ bool PrintVisitor::visit_boolop(AST_BoolOp* node) { ...@@ -1185,10 +1185,10 @@ bool PrintVisitor::visit_boolop(AST_BoolOp* node) {
continue; continue;
switch (node->op_type) { switch (node->op_type) {
case AST_TYPE::And: case AST_TYPE::And:
printf(" and "); stream << " and ";
break; break;
case AST_TYPE::Or: case AST_TYPE::Or:
printf(" or "); stream << " or ";
break; break;
default: default:
ASSERT(0, "%d", node->op_type); ASSERT(0, "%d", node->op_type);
...@@ -1199,40 +1199,40 @@ bool PrintVisitor::visit_boolop(AST_BoolOp* node) { ...@@ -1199,40 +1199,40 @@ bool PrintVisitor::visit_boolop(AST_BoolOp* node) {
} }
bool PrintVisitor::visit_break(AST_Break* node) { bool PrintVisitor::visit_break(AST_Break* node) {
printf("break"); stream << "break";
return true; return true;
} }
bool PrintVisitor::visit_call(AST_Call* node) { bool PrintVisitor::visit_call(AST_Call* node) {
node->func->accept(this); node->func->accept(this);
printf("("); stream << "(";
bool prevarg = false; bool prevarg = false;
for (int i = 0; i < node->args.size(); i++) { for (int i = 0; i < node->args.size(); i++) {
if (prevarg) if (prevarg)
printf(", "); stream << ", ";
node->args[i]->accept(this); node->args[i]->accept(this);
prevarg = true; prevarg = true;
} }
for (int i = 0; i < node->keywords.size(); i++) { for (int i = 0; i < node->keywords.size(); i++) {
if (prevarg) if (prevarg)
printf(", "); stream << ", ";
node->keywords[i]->accept(this); node->keywords[i]->accept(this);
prevarg = true; prevarg = true;
} }
if (node->starargs) { if (node->starargs) {
if (prevarg) if (prevarg)
printf(", "); stream << ", ";
node->starargs->accept(this); node->starargs->accept(this);
prevarg = true; prevarg = true;
} }
if (node->kwargs) { if (node->kwargs) {
if (prevarg) if (prevarg)
printf(", "); stream << ", ";
node->kwargs->accept(this); node->kwargs->accept(this);
prevarg = true; prevarg = true;
} }
printf(")"); stream << ")";
return true; return true;
} }
...@@ -1241,7 +1241,7 @@ bool PrintVisitor::visit_compare(AST_Compare* node) { ...@@ -1241,7 +1241,7 @@ bool PrintVisitor::visit_compare(AST_Compare* node) {
for (int i = 0; i < node->ops.size(); i++) { for (int i = 0; i < node->ops.size(); i++) {
std::string symbol = getOpSymbol(node->ops[i]); std::string symbol = getOpSymbol(node->ops[i]);
printf(" %s ", symbol.c_str()); stream << " " << symbol << " ";
node->comparators[i]->accept(this); node->comparators[i]->accept(this);
} }
...@@ -1250,13 +1250,13 @@ bool PrintVisitor::visit_compare(AST_Compare* node) { ...@@ -1250,13 +1250,13 @@ bool PrintVisitor::visit_compare(AST_Compare* node) {
} }
bool PrintVisitor::visit_comprehension(AST_comprehension* node) { bool PrintVisitor::visit_comprehension(AST_comprehension* node) {
printf("for "); stream << "for ";
node->target->accept(this); node->target->accept(this);
printf(" in "); stream << " in ";
node->iter->accept(this); node->iter->accept(this);
for (AST_expr* i : node->ifs) { for (AST_expr* i : node->ifs) {
printf(" if "); stream << " if ";
i->accept(this); i->accept(this);
} }
...@@ -1265,22 +1265,22 @@ bool PrintVisitor::visit_comprehension(AST_comprehension* node) { ...@@ -1265,22 +1265,22 @@ bool PrintVisitor::visit_comprehension(AST_comprehension* node) {
bool PrintVisitor::visit_classdef(AST_ClassDef* node) { bool PrintVisitor::visit_classdef(AST_ClassDef* node) {
for (int i = 0, n = node->decorator_list.size(); i < n; i++) { for (int i = 0, n = node->decorator_list.size(); i < n; i++) {
printf("@"); stream << "@";
node->decorator_list[i]->accept(this); node->decorator_list[i]->accept(this);
printf("\n"); stream << "\n";
printIndent(); printIndent();
} }
printf("class %s(", node->name.c_str()); stream << "class " << node->name.s() << "(";
for (int i = 0, n = node->bases.size(); i < n; i++) { for (int i = 0, n = node->bases.size(); i < n; i++) {
if (i) if (i)
printf(", "); stream << ", ";
node->bases[i]->accept(this); node->bases[i]->accept(this);
} }
printf(")"); stream << ")";
indent += 4; indent += 4;
for (int i = 0, n = node->body.size(); i < n; i++) { for (int i = 0, n = node->body.size(); i < n; i++) {
printf("\n"); stream << "\n";
printIndent(); printIndent();
node->body[i]->accept(this); node->body[i]->accept(this);
} }
...@@ -1290,87 +1290,87 @@ bool PrintVisitor::visit_classdef(AST_ClassDef* node) { ...@@ -1290,87 +1290,87 @@ bool PrintVisitor::visit_classdef(AST_ClassDef* node) {
} }
bool PrintVisitor::visit_continue(AST_Continue* node) { bool PrintVisitor::visit_continue(AST_Continue* node) {
printf("continue"); stream << "continue";
return true; return true;
} }
bool PrintVisitor::visit_delete(AST_Delete* node) { bool PrintVisitor::visit_delete(AST_Delete* node) {
printf("del "); stream << "del ";
for (int i = 0; i < node->targets.size(); i++) { for (int i = 0; i < node->targets.size(); i++) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->targets[i]->accept(this); node->targets[i]->accept(this);
} }
return true; return true;
} }
bool PrintVisitor::visit_dict(AST_Dict* node) { bool PrintVisitor::visit_dict(AST_Dict* node) {
printf("{"); stream << "{";
for (int i = 0; i < node->keys.size(); i++) { for (int i = 0; i < node->keys.size(); i++) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->keys[i]->accept(this); node->keys[i]->accept(this);
printf(":"); stream << ":";
node->values[i]->accept(this); node->values[i]->accept(this);
} }
printf("}"); stream << "}";
return true; return true;
} }
bool PrintVisitor::visit_dictcomp(AST_DictComp* node) { bool PrintVisitor::visit_dictcomp(AST_DictComp* node) {
printf("{"); stream << "{";
node->key->accept(this); node->key->accept(this);
printf(":"); stream << ":";
node->value->accept(this); node->value->accept(this);
for (auto c : node->generators) { for (auto c : node->generators) {
printf(" "); stream << " ";
c->accept(this); c->accept(this);
} }
printf("}"); stream << "}";
return true; return true;
} }
bool PrintVisitor::visit_ellipsis(AST_Ellipsis*) { bool PrintVisitor::visit_ellipsis(AST_Ellipsis*) {
printf("..."); stream << "...";
return true; return true;
} }
bool PrintVisitor::visit_excepthandler(AST_ExceptHandler* node) { bool PrintVisitor::visit_excepthandler(AST_ExceptHandler* node) {
printf("except"); stream << "except";
if (node->type) { if (node->type) {
printf(" "); stream << " ";
node->type->accept(this); node->type->accept(this);
} }
if (node->name) { if (node->name) {
printf(" as "); stream << " as ";
node->name->accept(this); node->name->accept(this);
} }
printf(":\n"); stream << ":\n";
indent += 4; indent += 4;
for (AST* subnode : node->body) { for (AST* subnode : node->body) {
printIndent(); printIndent();
subnode->accept(this); subnode->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
return true; return true;
} }
bool PrintVisitor::visit_exec(AST_Exec* node) { bool PrintVisitor::visit_exec(AST_Exec* node) {
printf("exec "); stream << "exec ";
node->body->accept(this); node->body->accept(this);
if (node->globals) { if (node->globals) {
printf(" in "); stream << " in ";
node->globals->accept(this); node->globals->accept(this);
if (node->locals) { if (node->locals) {
printf(", "); stream << ", ";
node->locals->accept(this); node->locals->accept(this);
} }
} }
printf("\n"); stream << "\n";
return true; return true;
} }
...@@ -1381,32 +1381,32 @@ bool PrintVisitor::visit_expr(AST_Expr* node) { ...@@ -1381,32 +1381,32 @@ bool PrintVisitor::visit_expr(AST_Expr* node) {
bool PrintVisitor::visit_extslice(AST_ExtSlice* node) { bool PrintVisitor::visit_extslice(AST_ExtSlice* node) {
for (int i = 0; i < node->dims.size(); ++i) { for (int i = 0; i < node->dims.size(); ++i) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->dims[i]->accept(this); node->dims[i]->accept(this);
} }
return true; return true;
} }
bool PrintVisitor::visit_for(AST_For* node) { bool PrintVisitor::visit_for(AST_For* node) {
printf("<for loop>\n"); stream << "<for loop>\n";
return true; return true;
} }
bool PrintVisitor::visit_functiondef(AST_FunctionDef* node) { bool PrintVisitor::visit_functiondef(AST_FunctionDef* node) {
for (auto d : node->decorator_list) { for (auto d : node->decorator_list) {
printf("@"); stream << "@";
d->accept(this); d->accept(this);
printf("\n"); stream << "\n";
printIndent(); printIndent();
} }
printf("def %s(", node->name.c_str()); stream << "def " << node->name.s() << "(";
node->args->accept(this); node->args->accept(this);
printf(")"); stream << ")";
indent += 4; indent += 4;
for (int i = 0; i < node->body.size(); i++) { for (int i = 0; i < node->body.size(); i++) {
printf("\n"); stream << "\n";
printIndent(); printIndent();
node->body[i]->accept(this); node->body[i]->accept(this);
} }
...@@ -1415,36 +1415,36 @@ bool PrintVisitor::visit_functiondef(AST_FunctionDef* node) { ...@@ -1415,36 +1415,36 @@ bool PrintVisitor::visit_functiondef(AST_FunctionDef* node) {
} }
bool PrintVisitor::visit_generatorexp(AST_GeneratorExp* node) { bool PrintVisitor::visit_generatorexp(AST_GeneratorExp* node) {
printf("["); stream << "[";
node->elt->accept(this); node->elt->accept(this);
for (auto c : node->generators) { for (auto c : node->generators) {
printf(" "); stream << " ";
c->accept(this); c->accept(this);
} }
printf("]"); stream << "]";
return true; return true;
} }
bool PrintVisitor::visit_global(AST_Global* node) { bool PrintVisitor::visit_global(AST_Global* node) {
printf("global "); stream << "global ";
for (int i = 0; i < node->names.size(); i++) { for (int i = 0; i < node->names.size(); i++) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
printf("%s", node->names[i].c_str()); stream << node->names[i].s();
} }
return true; return true;
} }
bool PrintVisitor::visit_if(AST_If* node) { bool PrintVisitor::visit_if(AST_If* node) {
printf("if "); stream << "if ";
node->test->accept(this); node->test->accept(this);
printf(":\n"); stream << ":\n";
indent += 4; indent += 4;
for (int i = 0; i < node->body.size(); i++) { for (int i = 0; i < node->body.size(); i++) {
printIndent(); printIndent();
node->body[i]->accept(this); node->body[i]->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
...@@ -1456,14 +1456,14 @@ bool PrintVisitor::visit_if(AST_If* node) { ...@@ -1456,14 +1456,14 @@ bool PrintVisitor::visit_if(AST_If* node) {
elif = true; elif = true;
if (elif) { if (elif) {
printf("el"); stream << "el";
} else { } else {
printf("else:\n"); stream << "else:\n";
indent += 4; indent += 4;
} }
for (int i = 0; i < node->orelse.size(); i++) { for (int i = 0; i < node->orelse.size(); i++) {
if (i) if (i)
printf("\n"); stream << "\n";
printIndent(); printIndent();
node->orelse[i]->accept(this); node->orelse[i]->accept(this);
} }
...@@ -1475,28 +1475,28 @@ bool PrintVisitor::visit_if(AST_If* node) { ...@@ -1475,28 +1475,28 @@ bool PrintVisitor::visit_if(AST_If* node) {
bool PrintVisitor::visit_ifexp(AST_IfExp* node) { bool PrintVisitor::visit_ifexp(AST_IfExp* node) {
node->body->accept(this); node->body->accept(this);
printf(" if "); stream << " if ";
node->test->accept(this); node->test->accept(this);
printf(" else "); stream << " else ";
node->orelse->accept(this); node->orelse->accept(this);
return true; return true;
} }
bool PrintVisitor::visit_import(AST_Import* node) { bool PrintVisitor::visit_import(AST_Import* node) {
printf("import "); stream << "import ";
for (int i = 0; i < node->names.size(); i++) { for (int i = 0; i < node->names.size(); i++) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->names[i]->accept(this); node->names[i]->accept(this);
} }
return true; return true;
} }
bool PrintVisitor::visit_importfrom(AST_ImportFrom* node) { bool PrintVisitor::visit_importfrom(AST_ImportFrom* node) {
printf("from %s import ", node->module.c_str()); stream << "from " << node->module.s() << " import ";
for (int i = 0; i < node->names.size(); i++) { for (int i = 0; i < node->names.size(); i++) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->names[i]->accept(this); node->names[i]->accept(this);
} }
return true; return true;
...@@ -1507,111 +1507,111 @@ bool PrintVisitor::visit_index(AST_Index* node) { ...@@ -1507,111 +1507,111 @@ bool PrintVisitor::visit_index(AST_Index* node) {
} }
bool PrintVisitor::visit_invoke(AST_Invoke* node) { bool PrintVisitor::visit_invoke(AST_Invoke* node) {
printf("invoke %d %d: ", node->normal_dest->idx, node->exc_dest->idx); stream << "invoke " << node->normal_dest->idx << " " << node->exc_dest->idx << ": ";
node->stmt->accept(this); node->stmt->accept(this);
return true; return true;
} }
bool PrintVisitor::visit_lambda(AST_Lambda* node) { bool PrintVisitor::visit_lambda(AST_Lambda* node) {
printf("lambda "); stream << "lambda ";
node->args->accept(this); node->args->accept(this);
printf(": "); stream << ": ";
node->body->accept(this); node->body->accept(this);
return true; return true;
} }
bool PrintVisitor::visit_langprimitive(AST_LangPrimitive* node) { bool PrintVisitor::visit_langprimitive(AST_LangPrimitive* node) {
printf(":"); stream << ":";
switch (node->opcode) { switch (node->opcode) {
case AST_LangPrimitive::CHECK_EXC_MATCH: case AST_LangPrimitive::CHECK_EXC_MATCH:
printf("CHECK_EXC_MATCH"); stream << "CHECK_EXC_MATCH";
break; break;
case AST_LangPrimitive::LANDINGPAD: case AST_LangPrimitive::LANDINGPAD:
printf("LANDINGPAD"); stream << "LANDINGPAD";
break; break;
case AST_LangPrimitive::LOCALS: case AST_LangPrimitive::LOCALS:
printf("LOCALS"); stream << "LOCALS";
break; break;
case AST_LangPrimitive::GET_ITER: case AST_LangPrimitive::GET_ITER:
printf("GET_ITER"); stream << "GET_ITER";
break; break;
case AST_LangPrimitive::IMPORT_FROM: case AST_LangPrimitive::IMPORT_FROM:
printf("IMPORT_FROM"); stream << "IMPORT_FROM";
break; break;
case AST_LangPrimitive::IMPORT_NAME: case AST_LangPrimitive::IMPORT_NAME:
printf("IMPORT_NAME"); stream << "IMPORT_NAME";
break; break;
case AST_LangPrimitive::IMPORT_STAR: case AST_LangPrimitive::IMPORT_STAR:
printf("IMPORT_STAR"); stream << "IMPORT_STAR";
break; break;
case AST_LangPrimitive::NONE: case AST_LangPrimitive::NONE:
printf("NONE"); stream << "NONE";
break; break;
case AST_LangPrimitive::NONZERO: case AST_LangPrimitive::NONZERO:
printf("NONZERO"); stream << "NONZERO";
break; break;
case AST_LangPrimitive::SET_EXC_INFO: case AST_LangPrimitive::SET_EXC_INFO:
printf("SET_EXC_INFO"); stream << "SET_EXC_INFO";
break; break;
case AST_LangPrimitive::UNCACHE_EXC_INFO: case AST_LangPrimitive::UNCACHE_EXC_INFO:
printf("UNCACHE_EXC_INFO"); stream << "UNCACHE_EXC_INFO";
break; break;
case AST_LangPrimitive::HASNEXT: case AST_LangPrimitive::HASNEXT:
printf("HASNEXT"); stream << "HASNEXT";
break; break;
default: default:
RELEASE_ASSERT(0, "%d", node->opcode); RELEASE_ASSERT(0, "%d", node->opcode);
} }
printf("("); stream << "(";
for (int i = 0, n = node->args.size(); i < n; ++i) { for (int i = 0, n = node->args.size(); i < n; ++i) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->args[i]->accept(this); node->args[i]->accept(this);
} }
printf(")"); stream << ")";
return true; return true;
} }
bool PrintVisitor::visit_list(AST_List* node) { bool PrintVisitor::visit_list(AST_List* node) {
printf("["); stream << "[";
for (int i = 0, n = node->elts.size(); i < n; ++i) { for (int i = 0, n = node->elts.size(); i < n; ++i) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->elts[i]->accept(this); node->elts[i]->accept(this);
} }
printf("]"); stream << "]";
return true; return true;
} }
bool PrintVisitor::visit_listcomp(AST_ListComp* node) { bool PrintVisitor::visit_listcomp(AST_ListComp* node) {
printf("["); stream << "[";
node->elt->accept(this); node->elt->accept(this);
for (auto c : node->generators) { for (auto c : node->generators) {
printf(" "); stream << " ";
c->accept(this); c->accept(this);
} }
printf("]"); stream << "]";
return true; return true;
} }
bool PrintVisitor::visit_keyword(AST_keyword* node) { bool PrintVisitor::visit_keyword(AST_keyword* node) {
printf("%s=", node->arg.c_str()); stream << node->arg.s() << "=";
node->value->accept(this); node->value->accept(this);
return true; return true;
} }
bool PrintVisitor::visit_module(AST_Module* node) { bool PrintVisitor::visit_module(AST_Module* node) {
// printf("<module>\n"); // stream << "<module>\n";
for (int i = 0; i < node->body.size(); i++) { for (int i = 0; i < node->body.size(); i++) {
node->body[i]->accept(this); node->body[i]->accept(this);
printf("\n"); stream << "\n";
} }
return true; return true;
} }
bool PrintVisitor::visit_expression(AST_Expression* node) { bool PrintVisitor::visit_expression(AST_Expression* node) {
node->body->accept(this); node->body->accept(this);
printf("\n"); stream << "\n";
return true; return true;
} }
...@@ -1619,26 +1619,26 @@ bool PrintVisitor::visit_suite(AST_Suite* node) { ...@@ -1619,26 +1619,26 @@ bool PrintVisitor::visit_suite(AST_Suite* node) {
for (int i = 0; i < node->body.size(); i++) { for (int i = 0; i < node->body.size(); i++) {
printIndent(); printIndent();
node->body[i]->accept(this); node->body[i]->accept(this);
printf("\n"); stream << "\n";
} }
return true; return true;
} }
bool PrintVisitor::visit_name(AST_Name* node) { bool PrintVisitor::visit_name(AST_Name* node) {
printf("%s", node->id.c_str()); stream << node->id.s();
// printf("%s(%d)", node->id.c_str(), node->ctx_type); // printf("%s(%d)", node->id.c_str(), node->ctx_type);
return false; return false;
} }
bool PrintVisitor::visit_num(AST_Num* node) { bool PrintVisitor::visit_num(AST_Num* node) {
if (node->num_type == AST_Num::INT) { if (node->num_type == AST_Num::INT) {
printf("%ld", node->n_int); stream << node->n_int;
} else if (node->num_type == AST_Num::LONG) { } else if (node->num_type == AST_Num::LONG) {
printf("%sL", node->n_long.c_str()); stream << node->n_long << "L";
} else if (node->num_type == AST_Num::FLOAT) { } else if (node->num_type == AST_Num::FLOAT) {
printf("%f", node->n_float); stream << node->n_float;
} else if (node->num_type == AST_Num::COMPLEX) { } else if (node->num_type == AST_Num::COMPLEX) {
printf("%fj", node->n_float); stream << node->n_float << "j";
} else { } else {
RELEASE_ASSERT(0, ""); RELEASE_ASSERT(0, "");
} }
...@@ -1646,53 +1646,53 @@ bool PrintVisitor::visit_num(AST_Num* node) { ...@@ -1646,53 +1646,53 @@ bool PrintVisitor::visit_num(AST_Num* node) {
} }
bool PrintVisitor::visit_pass(AST_Pass* node) { bool PrintVisitor::visit_pass(AST_Pass* node) {
printf("pass"); stream << "pass";
return true; return true;
} }
bool PrintVisitor::visit_print(AST_Print* node) { bool PrintVisitor::visit_print(AST_Print* node) {
printf("print "); stream << "print ";
if (node->dest) { if (node->dest) {
printf(">>"); stream << ">>";
node->dest->accept(this); node->dest->accept(this);
printf(", "); stream << ", ";
} }
for (int i = 0; i < node->values.size(); i++) { for (int i = 0; i < node->values.size(); i++) {
if (i > 0) if (i > 0)
printf(", "); stream << ", ";
node->values[i]->accept(this); node->values[i]->accept(this);
} }
if (!node->nl) if (!node->nl)
printf(","); stream << ",";
return true; return true;
} }
bool PrintVisitor::visit_raise(AST_Raise* node) { bool PrintVisitor::visit_raise(AST_Raise* node) {
printf("raise"); stream << "raise";
if (node->arg0) { if (node->arg0) {
printf(" "); stream << " ";
node->arg0->accept(this); node->arg0->accept(this);
} }
if (node->arg1) { if (node->arg1) {
printf(", "); stream << ", ";
node->arg1->accept(this); node->arg1->accept(this);
} }
if (node->arg2) { if (node->arg2) {
printf(", "); stream << ", ";
node->arg2->accept(this); node->arg2->accept(this);
} }
return true; return true;
} }
bool PrintVisitor::visit_repr(AST_Repr* node) { bool PrintVisitor::visit_repr(AST_Repr* node) {
printf("`"); stream << "`";
node->value->accept(this); node->value->accept(this);
printf("`"); stream << "`";
return true; return true;
} }
bool PrintVisitor::visit_return(AST_Return* node) { bool PrintVisitor::visit_return(AST_Return* node) {
printf("return "); stream << "return ";
return false; return false;
} }
...@@ -1701,55 +1701,55 @@ bool PrintVisitor::visit_set(AST_Set* node) { ...@@ -1701,55 +1701,55 @@ bool PrintVisitor::visit_set(AST_Set* node) {
// but we sometimes generate it (ex in set comprehension lowering). // but we sometimes generate it (ex in set comprehension lowering).
// Just to make it clear when printing, print empty set literals as "SET{}". // Just to make it clear when printing, print empty set literals as "SET{}".
if (!node->elts.size()) if (!node->elts.size())
printf("SET"); stream << "SET";
printf("{"); stream << "{";
bool first = true; bool first = true;
for (auto e : node->elts) { for (auto e : node->elts) {
if (!first) if (!first)
printf(", "); stream << ", ";
first = false; first = false;
e->accept(this); e->accept(this);
} }
printf("}"); stream << "}";
return true; return true;
} }
bool PrintVisitor::visit_setcomp(AST_SetComp* node) { bool PrintVisitor::visit_setcomp(AST_SetComp* node) {
printf("{"); stream << "{";
node->elt->accept(this); node->elt->accept(this);
for (auto c : node->generators) { for (auto c : node->generators) {
printf(" "); stream << " ";
c->accept(this); c->accept(this);
} }
printf("}"); stream << "}";
return true; return true;
} }
bool PrintVisitor::visit_slice(AST_Slice* node) { bool PrintVisitor::visit_slice(AST_Slice* node) {
printf("<slice>("); stream << "<slice>(";
if (node->lower) if (node->lower)
node->lower->accept(this); node->lower->accept(this);
if (node->upper || node->step) if (node->upper || node->step)
putchar(':'); stream << ':';
if (node->upper) if (node->upper)
node->upper->accept(this); node->upper->accept(this);
if (node->step) { if (node->step) {
putchar(':'); stream << ':';
node->step->accept(this); node->step->accept(this);
} }
printf(")"); stream << ")";
return true; return true;
} }
bool PrintVisitor::visit_str(AST_Str* node) { bool PrintVisitor::visit_str(AST_Str* node) {
if (node->str_type == AST_Str::STR) { if (node->str_type == AST_Str::STR) {
printf("\"%s\"", node->str_data.c_str()); stream << "\"" << node->str_data << "\"";
} else if (node->str_type == AST_Str::UNICODE) { } else if (node->str_type == AST_Str::UNICODE) {
printf("<unicode value>"); stream << "<unicode value>";
} else { } else {
RELEASE_ASSERT(0, "%d", node->str_type); RELEASE_ASSERT(0, "%d", node->str_type);
} }
...@@ -1758,19 +1758,19 @@ bool PrintVisitor::visit_str(AST_Str* node) { ...@@ -1758,19 +1758,19 @@ bool PrintVisitor::visit_str(AST_Str* node) {
bool PrintVisitor::visit_subscript(AST_Subscript* node) { bool PrintVisitor::visit_subscript(AST_Subscript* node) {
node->value->accept(this); node->value->accept(this);
printf("["); stream << "[";
node->slice->accept(this); node->slice->accept(this);
printf("]"); stream << "]";
return true; return true;
} }
bool PrintVisitor::visit_tryexcept(AST_TryExcept* node) { bool PrintVisitor::visit_tryexcept(AST_TryExcept* node) {
printf("try:\n"); stream << "try:\n";
indent += 4; indent += 4;
for (AST* subnode : node->body) { for (AST* subnode : node->body) {
printIndent(); printIndent();
subnode->accept(this); subnode->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
for (AST_ExceptHandler* handler : node->handlers) { for (AST_ExceptHandler* handler : node->handlers) {
...@@ -1780,12 +1780,12 @@ bool PrintVisitor::visit_tryexcept(AST_TryExcept* node) { ...@@ -1780,12 +1780,12 @@ bool PrintVisitor::visit_tryexcept(AST_TryExcept* node) {
if (node->orelse.size()) { if (node->orelse.size()) {
printIndent(); printIndent();
printf("else:\n"); stream << "else:\n";
indent += 4; indent += 4;
for (AST* subnode : node->orelse) { for (AST* subnode : node->orelse) {
printIndent(); printIndent();
subnode->accept(this); subnode->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
} }
...@@ -1796,32 +1796,32 @@ bool PrintVisitor::visit_tryfinally(AST_TryFinally* node) { ...@@ -1796,32 +1796,32 @@ bool PrintVisitor::visit_tryfinally(AST_TryFinally* node) {
if (node->body.size() == 1 && node->body[0]->type == AST_TYPE::TryExcept) { if (node->body.size() == 1 && node->body[0]->type == AST_TYPE::TryExcept) {
node->body[0]->accept(this); node->body[0]->accept(this);
printIndent(); printIndent();
printf("finally:\n"); stream << "finally:\n";
indent += 4; indent += 4;
for (AST* subnode : node->finalbody) { for (AST* subnode : node->finalbody) {
printIndent(); printIndent();
subnode->accept(this); subnode->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
} else { } else {
printf("try:\n"); stream << "try:\n";
indent += 4; indent += 4;
for (AST* subnode : node->body) { for (AST* subnode : node->body) {
printIndent(); printIndent();
subnode->accept(this); subnode->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
printIndent(); printIndent();
printf("finally:\n"); stream << "finally:\n";
indent += 4; indent += 4;
for (AST* subnode : node->finalbody) { for (AST* subnode : node->finalbody) {
printIndent(); printIndent();
subnode->accept(this); subnode->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
} }
...@@ -1829,64 +1829,64 @@ bool PrintVisitor::visit_tryfinally(AST_TryFinally* node) { ...@@ -1829,64 +1829,64 @@ bool PrintVisitor::visit_tryfinally(AST_TryFinally* node) {
} }
bool PrintVisitor::visit_tuple(AST_Tuple* node) { bool PrintVisitor::visit_tuple(AST_Tuple* node) {
printf("("); stream << "(";
int n = node->elts.size(); int n = node->elts.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
if (i) if (i)
printf(", "); stream << ", ";
node->elts[i]->accept(this); node->elts[i]->accept(this);
} }
if (n == 1) if (n == 1)
printf(","); stream << ",";
printf(")"); stream << ")";
return true; return true;
} }
bool PrintVisitor::visit_unaryop(AST_UnaryOp* node) { bool PrintVisitor::visit_unaryop(AST_UnaryOp* node) {
switch (node->op_type) { switch (node->op_type) {
case AST_TYPE::Invert: case AST_TYPE::Invert:
printf("~"); stream << "~";
break; break;
case AST_TYPE::Not: case AST_TYPE::Not:
printf("not "); stream << "not ";
break; break;
case AST_TYPE::UAdd: case AST_TYPE::UAdd:
printf("+"); stream << "+";
break; break;
case AST_TYPE::USub: case AST_TYPE::USub:
printf("-"); stream << "-";
break; break;
default: default:
RELEASE_ASSERT(0, "%s", getOpName(node->op_type)->c_str()); RELEASE_ASSERT(0, "%s", getOpName(node->op_type)->c_str());
break; break;
} }
printf("("); stream << "(";
node->operand->accept(this); node->operand->accept(this);
printf(")"); stream << ")";
return true; return true;
} }
bool PrintVisitor::visit_while(AST_While* node) { bool PrintVisitor::visit_while(AST_While* node) {
printf("while "); stream << "while ";
node->test->accept(this); node->test->accept(this);
printf("\n"); stream << "\n";
indent += 4; indent += 4;
for (int i = 0; i < node->body.size(); i++) { for (int i = 0; i < node->body.size(); i++) {
printIndent(); printIndent();
node->body[i]->accept(this); node->body[i]->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
if (node->orelse.size()) { if (node->orelse.size()) {
printIndent(); printIndent();
printf("else\n"); stream << "else\n";
indent += 4; indent += 4;
for (int i = 0; i < node->orelse.size(); i++) { for (int i = 0; i < node->orelse.size(); i++) {
printIndent(); printIndent();
node->orelse[i]->accept(this); node->orelse[i]->accept(this);
printf("\n"); stream << "\n";
} }
indent -= 4; indent -= 4;
} }
...@@ -1894,18 +1894,18 @@ bool PrintVisitor::visit_while(AST_While* node) { ...@@ -1894,18 +1894,18 @@ bool PrintVisitor::visit_while(AST_While* node) {
} }
bool PrintVisitor::visit_with(AST_With* node) { bool PrintVisitor::visit_with(AST_With* node) {
printf("with "); stream << "with ";
node->context_expr->accept(this); node->context_expr->accept(this);
if (node->optional_vars) { if (node->optional_vars) {
printf(" as "); stream << " as ";
node->optional_vars->accept(this); node->optional_vars->accept(this);
printf(":\n"); stream << ":\n";
} }
indent += 4; indent += 4;
for (int i = 0; i < node->body.size(); i++) { for (int i = 0; i < node->body.size(); i++) {
if (i > 0) if (i > 0)
printf("\n"); stream << "\n";
printIndent(); printIndent();
node->body[i]->accept(this); node->body[i]->accept(this);
} }
...@@ -1915,21 +1915,21 @@ bool PrintVisitor::visit_with(AST_With* node) { ...@@ -1915,21 +1915,21 @@ bool PrintVisitor::visit_with(AST_With* node) {
} }
bool PrintVisitor::visit_yield(AST_Yield* node) { bool PrintVisitor::visit_yield(AST_Yield* node) {
printf("yield "); stream << "yield ";
if (node->value) if (node->value)
node->value->accept(this); node->value->accept(this);
return true; return true;
} }
bool PrintVisitor::visit_branch(AST_Branch* node) { bool PrintVisitor::visit_branch(AST_Branch* node) {
printf("if "); stream << "if ";
node->test->accept(this); node->test->accept(this);
printf(" goto %d else goto %d", node->iftrue->idx, node->iffalse->idx); stream << " goto " << node->iftrue->idx << " else goto " << node->iffalse->idx;
return true; return true;
} }
bool PrintVisitor::visit_jump(AST_Jump* node) { bool PrintVisitor::visit_jump(AST_Jump* node) {
printf("goto %d", node->target->idx); stream << "goto " << node->target->idx;
return true; return true;
} }
...@@ -1938,17 +1938,17 @@ bool PrintVisitor::visit_clsattribute(AST_ClsAttribute* node) { ...@@ -1938,17 +1938,17 @@ bool PrintVisitor::visit_clsattribute(AST_ClsAttribute* node) {
// node->value->accept(this); // node->value->accept(this);
// printf(", '%s')", node->attr.c_str()); // printf(", '%s')", node->attr.c_str());
node->value->accept(this); node->value->accept(this);
printf(":%s", node->attr.c_str()); stream << ":" << node->attr.s();
return true; return true;
} }
bool PrintVisitor::visit_makefunction(AST_MakeFunction* node) { bool PrintVisitor::visit_makefunction(AST_MakeFunction* node) {
printf("make_"); stream << "make_";
return false; return false;
} }
bool PrintVisitor::visit_makeclass(AST_MakeClass* node) { bool PrintVisitor::visit_makeclass(AST_MakeClass* node) {
printf("make_"); stream << "make_";
return false; return false;
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <vector> #include <vector>
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "analysis/scoping_analysis.h" #include "analysis/scoping_analysis.h"
#include "core/common.h" #include "core/common.h"
...@@ -1329,11 +1330,13 @@ public: ...@@ -1329,11 +1330,13 @@ public:
void print_ast(AST* ast); void print_ast(AST* ast);
class PrintVisitor : public ASTVisitor { class PrintVisitor : public ASTVisitor {
private: private:
llvm::raw_ostream& stream;
int indent; int indent;
void printIndent(); void printIndent();
void printOp(AST_TYPE::AST_TYPE op_type);
public: public:
PrintVisitor(int indent = 0) : indent(indent) {} PrintVisitor(int indent = 0, llvm::raw_ostream& stream = llvm::outs()) : stream(stream), indent(indent) {}
virtual ~PrintVisitor() {} virtual ~PrintVisitor() {}
virtual bool visit_alias(AST_alias* node); virtual bool visit_alias(AST_alias* node);
......
...@@ -56,26 +56,26 @@ void CFGBlock::unconnectFrom(CFGBlock* successor) { ...@@ -56,26 +56,26 @@ void CFGBlock::unconnectFrom(CFGBlock* successor) {
successor->predecessors.end()); successor->predecessors.end());
} }
void CFGBlock::print() { void CFGBlock::print(llvm::raw_ostream& stream) {
printf("Block %d", idx); stream << "Block " << idx;
if (info) if (info)
printf(" '%s'", info); stream << " '" << info << "'";
printf("; Predecessors:"); stream << "; Predecessors:";
for (int j = 0; j < predecessors.size(); j++) { for (int j = 0; j < predecessors.size(); j++) {
printf(" %d", predecessors[j]->idx); stream << " " << predecessors[j]->idx;
} }
printf(" Successors:"); stream << " Successors:";
for (int j = 0; j < successors.size(); j++) { for (int j = 0; j < successors.size(); j++) {
printf(" %d", successors[j]->idx); stream << " " << successors[j]->idx;
} }
printf("\n"); stream << "\n";
PrintVisitor pv(4); PrintVisitor pv(4);
for (int j = 0; j < body.size(); j++) { for (int j = 0; j < body.size(); j++) {
printf(" "); stream << " ";
body[j]->accept(&pv); body[j]->accept(&pv);
printf("\n"); stream << "\n";
} }
} }
...@@ -2518,11 +2518,11 @@ public: ...@@ -2518,11 +2518,11 @@ public:
} }
}; };
void CFG::print() { void CFG::print(llvm::raw_ostream& stream) {
printf("CFG:\n"); stream << "CFG:\n";
printf("%ld blocks\n", blocks.size()); stream << blocks.size() << " blocks\n";
for (int i = 0; i < blocks.size(); i++) for (int i = 0; i < blocks.size(); i++)
blocks[i]->print(); blocks[i]->print(stream);
} }
class AssignVRegsVisitor : public NoopASTVisitor { class AssignVRegsVisitor : public NoopASTVisitor {
......
...@@ -66,7 +66,7 @@ public: ...@@ -66,7 +66,7 @@ public:
void unconnectFrom(CFGBlock* successor); void unconnectFrom(CFGBlock* successor);
void push_back(AST_stmt* node) { body.push_back(node); } void push_back(AST_stmt* node) { body.push_back(node); }
void print(); void print(llvm::raw_ostream& stream = llvm::outs());
}; };
// Control Flow Graph // Control Flow Graph
...@@ -108,7 +108,7 @@ public: ...@@ -108,7 +108,7 @@ public:
blocks.push_back(block); blocks.push_back(block);
} }
void print(); void print(llvm::raw_ostream& stream = llvm::outs());
bool hasVregsAssigned() { return has_vregs_assigned; } bool hasVregsAssigned() { return has_vregs_assigned; }
void assignVRegs(const ParamNames& param_names, ScopeInfo* scope_info); void assignVRegs(const ParamNames& param_names, ScopeInfo* scope_info);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment