Commit 36564459 authored by frmdstryr's avatar frmdstryr Committed by Sven Brauch
Browse files

Add ast dump

parent c6bcbd30
......@@ -823,6 +823,8 @@ void PyDUChainTest::testTypes()
QEXPECT_FAIL("property_wrong", "visitCall uses declaration if no type", Continue);
QEXPECT_FAIL("property_setter", "very basic property support", Continue);
QEXPECT_FAIL("assignment_expr_context", "not implemented", Continue);
if (!visitor->found)
qDebug() << m_ast->dump();
QCOMPARE(visitor->found, true);
}
......
......@@ -13,6 +13,88 @@
namespace Python
{
static void dumpNode(QString &r, QString prefix, const Ast* node)
{
r.append(prefix);
r.append(node ? node->dump(): "None");
}
static void dumpContext(QString &r, QString prefix, ExpressionAst::Context context)
{
r.append(prefix);
switch(context) {
case ExpressionAst::Context::Load:
r.append("Load()");
break;
case ExpressionAst::Context::Store:
r.append("Store()");
break;
case ExpressionAst::Context::Delete:
r.append("Delete()");
break;
default:
r.append("Invalid");
}
}
static void dumpOperator(QString &r, QString prefix, Ast::OperatorTypes op)
{
r.append(prefix);
switch(op) {
case Ast::OperatorAdd:
r.append("Add()");
break;
case Ast::OperatorSub:
r.append("Sub()");
break;
case Ast::OperatorMult:
r.append("Mult()");
break;
case Ast::OperatorMatMult:
r.append("MatMult()");
break;
case Ast::OperatorDiv:
r.append("Div()");
break;
case Ast::OperatorPow:
r.append("Pow()");
break;
case Ast::OperatorLeftShift:
r.append("LShift()");
break;
case Ast::OperatorRightShift:
r.append("RShift()");
break;
case Ast::OperatorBitwiseOr:
r.append("BitwiseOr");
break;
case Ast::OperatorBitwiseXor:
r.append("BitwiseXor()");
break;
case Ast::OperatorFloorDivision:
r.append("FloorDivision()");
break;
default:
r.append("Invalid");
}
}
template<class T>
static void dumpList(QString &r, QString prefix, const T list, QString sep=", ")
{
int i = 0;
r.append(prefix);
r.append("[");
foreach(const Ast* node, list) {
i += 1;
dumpNode(r, "", node);
if (i < list.size())
r.append(sep);
}
r.append("]");
}
// We never need actual constructors for AST nodes, but it seems to be required, at least for some platforms
// so we provide pseudo implementations
// there's nothing happening here, don't bother reading the code
......@@ -25,36 +107,109 @@ ArgumentsAst::ArgumentsAst(Ast* parent): Ast(parent, Ast::ArgumentsAstType)
}
QString ArgumentsAst::dump() const
{
QString r = "arguments(";
dumpList(r, "posonlyargs=", posonlyargs);
dumpList(r, ", args=", arguments);
dumpList(r, ", kwonlyargs=", kwonlyargs);
dumpList(r, ", defaults=", defaultValues);
if (vararg)
dumpNode(r, ", vararg=", vararg);
if (kwarg)
dumpNode(r, ", kwarg=", kwarg);
r.append(")");
return r;
}
ArgAst::ArgAst(Ast* parent): Ast(parent, Ast::ArgAstType), argumentName(nullptr), annotation(nullptr)
{
}
QString ArgAst::dump() const
{
QString r = "arg(";
dumpNode(r, "name=", argumentName);
dumpNode(r, ", annotation=", annotation);
r.append(")");
return r;
}
AssertionAst::AssertionAst(Ast* parent): StatementAst(parent, Ast::AssertionAstType)
{
}
QString AssertionAst::dump() const
{
QString r = "Assertion(";
dumpNode(r, "condition=", condition);
dumpNode(r, ", message=", message);
r.append(")");
return r;
}
AssignmentAst::AssignmentAst(Ast* parent): StatementAst(parent, Ast::AssignmentAstType), value(nullptr)
{
}
QString AssignmentAst::dump() const
{
QString r = "Assign(";
dumpList(r, "targets=", targets);
dumpNode(r, ", value=", value);
r.append(")");
return r;
}
AttributeAst::AttributeAst(Ast* parent): ExpressionAst(parent, Ast::AttributeAstType), value(nullptr), depth(0)
{
}
QString AttributeAst::dump() const
{
QString r = "Attribute(";
dumpNode(r, "value=", value);
dumpNode(r, ", attr=", attribute);
dumpContext(r, ", ctx=", context);
r.append(")");
return r;
}
AugmentedAssignmentAst::AugmentedAssignmentAst(Ast* parent): StatementAst(parent, Ast::AugmentedAssignmentAstType), value(nullptr)
{
}
QString AugmentedAssignmentAst::dump() const
{
QString r = "AugmentedAssignment(";
dumpNode(r, "target=", target);
dumpNode(r, ", value=", value);
dumpOperator(r, ", op=", op);
r.append(")");
return r;
}
AnnotationAssignmentAst::AnnotationAssignmentAst(Ast* parent): StatementAst(parent, Ast::AnnotationAssignmentAstType), target(nullptr), value(nullptr), annotation(nullptr)
{
}
QString AnnotationAssignmentAst::dump() const
{
QString r = "AnnotationAssignment(";
dumpNode(r, "target=", target);
dumpNode(r, ", value=", value);
dumpNode(r, ", annotation=", annotation);
r.append(")");
return r;
}
BinaryOperationAst::BinaryOperationAst(Ast* parent): ExpressionAst(parent, Ast::BinaryOperationAstType), lhs(nullptr), rhs(nullptr)
{
......@@ -75,11 +230,37 @@ CallAst::CallAst(Ast* parent): ExpressionAst(parent, Ast::CallAstType), function
}
QString CallAst::dump() const
{
QString r;
r.append("Call(");
dumpNode(r, "func=", function);
dumpList(r, ", args=", arguments);
dumpList(r, ", keywords=", keywords);
r.append(")");
return r;
}
ClassDefinitionAst::ClassDefinitionAst(Ast* parent): StatementAst(parent, Ast::ClassDefinitionAstType), name(nullptr)
{
}
QString ClassDefinitionAst::dump() const
{
QString r;
r.append("ClassDef(");
dumpNode(r, "name=", name);
dumpList(r, ", bases=", baseClasses);
dumpList(r, ", body=", body, ",\n ");
// TODO: Keywords?
dumpList(r, ", decorator_list=", decorators);
r.append(")");
return r;
}
CodeAst::CodeAst() : Ast(nullptr, Ast::CodeAstType), name(nullptr)
{
}
......@@ -89,6 +270,16 @@ CodeAst::~CodeAst()
free_ast_recursive(this);
}
QString CodeAst::dump() const
{
QString r;
r.append("Module(");
dumpNode(r, "name=", name);
dumpList(r, ", body=", body, ",\n ");
r.append(")");
return r;
}
CompareAst::CompareAst(Ast* parent): ExpressionAst(parent, Ast::CompareAstType), leftmostElement(nullptr)
{
......@@ -109,16 +300,45 @@ DeleteAst::DeleteAst(Ast* parent): StatementAst(parent, Ast::DeleteAstType)
}
QString DeleteAst::dump() const
{
QString r = "Delete(";
dumpList(r, "targets=", targets);
r.append(")");
return r;
}
DictAst::DictAst(Ast* parent): ExpressionAst(parent, Ast::DictAstType)
{
}
QString DictAst::dump() const
{
QString r = "Dict(";
dumpList(r, "keys=", keys);
dumpList(r, ", values=", values);
r.append(")");
return r;
}
SliceAst::SliceAst(Ast* parent): ExpressionAst(parent, Ast::SliceAstType), lower(nullptr), upper(nullptr), step(nullptr)
{
}
QString SliceAst::dump() const
{
QString r;
r.append("Slice(");
dumpNode(r, "lower=", lower);
dumpNode(r, ", upper=", upper);
dumpNode(r, ", step=", step);
r.append(")");
return r;
}
DictionaryComprehensionAst::DictionaryComprehensionAst(Ast* parent): ExpressionAst(parent, Ast::DictionaryComprehensionAstType), key(nullptr), value(nullptr)
{
......@@ -149,21 +369,64 @@ AssignmentExpressionAst::AssignmentExpressionAst(Ast* parent): ExpressionAst(par
}
QString AssignmentExpressionAst::dump() const
{
QString r = "AssignmentExpression(";
dumpNode(r, "target=", target);
dumpNode(r, ", value=", value);
r.append(")");
return r;
}
YieldFromAst::YieldFromAst(Ast* parent) : ExpressionAst(parent, Ast::YieldFromAstType)
{
}
QString YieldFromAst::dump() const
{
QString r = "YieldFrom(";
dumpNode(r, "value=", value);
r.append(")");
return r;
}
ForAst::ForAst(Ast* parent): StatementAst(parent, Ast::ForAstType), target(nullptr), iterator(nullptr)
{
}
QString ForAst::dump() const
{
QString r = async ? "AsyncFor(": "For(";
dumpNode(r, "target=", target);
dumpNode(r, ", iterator=", iterator);
dumpList(r, ", body=", body, ",\n ");
if (orelse.size())
dumpList(r, ", orelse=", orelse, ",\n ");
r.append(")");
return r;
}
FunctionDefinitionAst::FunctionDefinitionAst(Ast* parent): StatementAst(parent, Ast::FunctionDefinitionAstType), name(nullptr), arguments(nullptr), async(false)
{
}
QString FunctionDefinitionAst::dump() const
{
QString r = async ? "AsyncFuncDef(": "FuncDef(";
dumpNode(r, "name=", name);
dumpNode(r, ", args=", arguments);
dumpList(r, ", body=", body, ",\n ");
if (decorators.size())
dumpList(r, ", decorator_list=", decorators);
if (returns)
dumpNode(r, ", returns=", returns);
r.append(")");
return r;
}
GeneratorExpressionAst::GeneratorExpressionAst(Ast* parent): ExpressionAst(parent, Ast::GeneratorExpressionAstType), element(nullptr)
{
......@@ -174,36 +437,99 @@ GlobalAst::GlobalAst(Ast* parent): StatementAst(parent, Ast::GlobalAstType)
}
QString GlobalAst::dump() const
{
QString r = "Global(";
dumpList(r, "names=", names);
r.append(")");
return r;
}
Identifier::Identifier(QString value) : Ast(nullptr, Ast::IdentifierAstType), value(value)
{
}
QString Identifier::dump() const
{
return "'" + value + "'";
}
IfAst::IfAst(Ast* parent): StatementAst(parent, Ast::IfAstType), condition(nullptr)
{
}
QString IfAst::dump() const
{
QString r = "If(";
dumpNode(r, "condition=", condition);
dumpList(r, ", body=", body, ",\n ");
if (orelse.size())
dumpList(r, ", orelse=", orelse, ",\n ");
r.append(")");
return r;
}
IfExpressionAst::IfExpressionAst(Ast* parent): ExpressionAst(parent, Ast::IfExpressionAstType), condition(nullptr)
{
}
QString IfExpressionAst::dump() const
{
QString r = "IfExpr(";
dumpNode(r, "condition=", condition);
dumpNode(r, ", body=", body);
if (orelse)
dumpNode(r, ", orelse=", orelse);
r.append(")");
return r;
}
ImportAst::ImportAst(Ast* parent): StatementAst(parent, Ast::ImportAstType)
{
}
QString ImportAst::dump() const
{
QString r = "Import(";
dumpList(r, "names=", names);
r.append(")");
return r;
}
ImportFromAst::ImportFromAst(Ast* parent): StatementAst(parent, Ast::ImportFromAstType), module(nullptr), level(0)
{
}
QString ImportFromAst::dump() const
{
QString r = "ImportFrom(";
dumpNode(r, "module=", module);
dumpList(r, ", names=", names);
r.append(")");
return r;
}
KeywordAst::KeywordAst(Ast* parent): Ast(parent, Ast::KeywordAstType), argumentName(nullptr), value(nullptr)
{
}
QString KeywordAst::dump() const
{
QString r;
r.append("Keyword(");
dumpNode(r, "arg=", argumentName);
dumpNode(r, ", value=", value);
r.append(")");
return r;
}
LambdaAst::LambdaAst(Ast* parent): ExpressionAst(parent, Ast::LambdaAstType), arguments(nullptr)
{
......@@ -214,26 +540,74 @@ ListAst::ListAst(Ast* parent): ExpressionAst(parent, Ast::ListAstType)
}
QString ListAst::dump() const
{
QString r = "List(";
dumpList(r, "elts=", elements);
dumpContext(r, ", ctx=", context);
r.append(")");
return r;
}
NameAst::NameAst(Ast* parent): ExpressionAst(parent, Ast::NameAstType), identifier(nullptr)
{
}
QString NameAst::dump() const
{
QString r = "Name(";
dumpNode(r, "id=", identifier);
dumpContext(r, ", ctx=", context);
r.append(")");
return r;
}
AwaitAst::AwaitAst(Ast* parent): ExpressionAst(parent, Ast::AwaitAstType), value(nullptr)
{
}
QString AwaitAst::dump() const
{
QString r = "AwaitAst(";
dumpNode(r, "value=", value);
r.append(")");
return r;
}
NameConstantAst::NameConstantAst(Ast* parent): ExpressionAst(parent, Ast::NameConstantAstType), value(Invalid)
{
}
QString NameConstantAst::dump() const
{
switch (value) {
case NameConstantAst::NameConstantTypes::False:
return "False";
case NameConstantAst::NameConstantTypes::True:
return "True";
case NameConstantAst::NameConstantTypes::None:
return "None";
default:
return "Invalid";
}
}
NumberAst::NumberAst(Ast* parent): ExpressionAst(parent, Ast::NumberAstType), value(0), isInt(false)
{
}
QString NumberAst::dump() const
{
if (isInt)
return "Number()";
else
return "Float()";
}
PassAst::PassAst(Ast* parent): StatementAst(parent, Ast::PassAstType)
{
......@@ -249,16 +623,43 @@ RaiseAst::RaiseAst(Ast* parent): StatementAst(parent, Ast::RaiseAstType), type(n
}
QString RaiseAst::dump() const
{
QString r;
r.append("Raise(");
dumpNode(r, "type=", type);
r.append(")");
return r;
}
ReturnAst::ReturnAst(Ast* parent): StatementAst(parent, Ast::ReturnAstType), value(nullptr)
{
}
QString ReturnAst::dump() const
{
QString r;
r.append("Return(");
dumpNode(r, "value=", value);
r.append(")");
return r;
}
SetAst::SetAst(Ast* parent): ExpressionAst(parent, Ast::SetAstType)
{
}
QString SetAst::dump() const
{
QString r = "Set(";
dumpList(r, "elts=", elements);
r.append(")");
return r;
}
SetComprehensionAst::SetComprehensionAst(Ast* parent): ExpressionAst(parent, Ast::SetComprehensionAstType), element(nullptr)
{
......@@ -294,16 +695,47 @@ SubscriptAst::SubscriptAst(Ast* parent): ExpressionAst(parent, Ast::SubscriptAst
}
QString SubscriptAst::dump() const
{
QString r;
r.append("Subscript(");
dumpNode(r, "value=", value);
dumpNode(r, ", slice=", slice);
dumpContext(r, ", context=", context);
r.append(")");
return r;
}
StarredAst::StarredAst(Ast* parent): ExpressionAst(parent, Ast::StarredAstType)
{
}
QString StarredAst::dump() const
{
QString r;
r.append("Starred(");
dumpNode(r, "value=", value);
dumpContext(r, ", context=", context);
r.append(")");
return r;
}
TupleAst::TupleAst(Ast* parent): ExpressionAst(parent, Ast::TupleAstType)
{
}
QString TupleAst::dump() const
{
QString r = "Tuple(";
dumpList(r, "elts=", elements);
dumpContext(r, ", context=", context);
r.append(")");
return r;
}