Don't create special objects for NULL, just use NULL directly.

Also move null check into PycRef for nullable references.
This commit is contained in:
Michael Hansen
2017-07-05 16:36:04 -07:00
parent 1329626215
commit b9dd99d518
12 changed files with 160 additions and 155 deletions

View File

@@ -1,7 +1,5 @@
#include "ASTNode.h" #include "ASTNode.h"
PycRef<ASTNode> Node_NULL = (ASTNode*)0;
/* ASTNodeList */ /* ASTNodeList */
void ASTNodeList::removeLast() void ASTNodeList::removeLast()
{ {

View File

@@ -52,9 +52,6 @@ public:
void delRef() { internalDelRef(this); } void delRef() { internalDelRef(this); }
}; };
/* A NULL node for comparison */
extern PycRef<ASTNode> Node_NULL;
class ASTNodeList : public ASTNode { class ASTNodeList : public ASTNode {
public: public:
@@ -157,8 +154,8 @@ public:
SLICE0, SLICE1, SLICE2, SLICE3 SLICE0, SLICE1, SLICE2, SLICE3
}; };
ASTSlice(int op, PycRef<ASTNode> left = Node_NULL, ASTSlice(int op, PycRef<ASTNode> left = NULL,
PycRef<ASTNode> right = Node_NULL) PycRef<ASTNode> right = NULL)
: ASTBinary(left, right, op, NODE_SLICE) { } : ASTBinary(left, right, op, NODE_SLICE) { }
}; };
@@ -258,7 +255,7 @@ public:
ASTCall(PycRef<ASTNode> func, pparam_t pparams, kwparam_t kwparams) ASTCall(PycRef<ASTNode> func, pparam_t pparams, kwparam_t kwparams)
: ASTNode(NODE_CALL), m_func(func), m_pparams(pparams), m_kwparams(kwparams), : ASTNode(NODE_CALL), m_func(func), m_pparams(pparams), m_kwparams(kwparams),
m_var(Node_NULL), m_kw(Node_NULL) { } m_var(), m_kw() { }
PycRef<ASTNode> func() const { return m_func; } PycRef<ASTNode> func() const { return m_func; }
const pparam_t& pparams() const { return m_pparams; } const pparam_t& pparams() const { return m_pparams; }
@@ -266,8 +263,8 @@ public:
PycRef<ASTNode> var() const { return m_var; } PycRef<ASTNode> var() const { return m_var; }
PycRef<ASTNode> kw() const { return m_kw; } PycRef<ASTNode> kw() const { return m_kw; }
bool hasVar() const { return m_var != Node_NULL; } bool hasVar() const { return m_var != NULL; }
bool hasKW() const { return m_kw != Node_NULL; } bool hasKW() const { return m_kw != NULL; }
void setVar(PycRef<ASTNode> var) { m_var = var; } void setVar(PycRef<ASTNode> var) { m_var = var; }
void setKW(PycRef<ASTNode> kw) { m_kw = kw; } void setKW(PycRef<ASTNode> kw) { m_kw = kw; }
@@ -363,7 +360,7 @@ private:
class ASTPrint : public ASTNode { class ASTPrint : public ASTNode {
public: public:
ASTPrint(PycRef<ASTNode> value, PycRef<ASTNode> stream = Node_NULL) ASTPrint(PycRef<ASTNode> value, PycRef<ASTNode> stream = NULL)
: ASTNode(NODE_PRINT), m_value(value), m_stream(stream) { } : ASTNode(NODE_PRINT), m_value(value), m_stream(stream) { }
PycRef<ASTNode> value() const { return m_value; } PycRef<ASTNode> value() const { return m_value; }

View File

@@ -293,21 +293,21 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> start = stack.top(); PycRef<ASTNode> start = stack.top();
stack.pop(); stack.pop();
if (start->type() == ASTNode::NODE_OBJECT if (start.type() == ASTNode::NODE_OBJECT
&& start.cast<ASTObject>()->object() == Pyc_None) { && start.cast<ASTObject>()->object() == Pyc_None) {
start = Node_NULL; start = NULL;
} }
if (end->type() == ASTNode::NODE_OBJECT if (end.type() == ASTNode::NODE_OBJECT
&& end.cast<ASTObject>()->object() == Pyc_None) { && end.cast<ASTObject>()->object() == Pyc_None) {
end = Node_NULL; end = NULL;
} }
if (start == Node_NULL && end == Node_NULL) { if (start == NULL && end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE0)); stack.push(new ASTSlice(ASTSlice::SLICE0));
} else if (start == Node_NULL) { } else if (start == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE2, start, end)); stack.push(new ASTSlice(ASTSlice::SLICE2, start, end));
} else if (end == Node_NULL) { } else if (end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE1, start, end)); stack.push(new ASTSlice(ASTSlice::SLICE1, start, end));
} else { } else {
stack.push(new ASTSlice(ASTSlice::SLICE3, start, end)); stack.push(new ASTSlice(ASTSlice::SLICE3, start, end));
@@ -320,29 +320,29 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> start = stack.top(); PycRef<ASTNode> start = stack.top();
stack.pop(); stack.pop();
if (start->type() == ASTNode::NODE_OBJECT if (start.type() == ASTNode::NODE_OBJECT
&& start.cast<ASTObject>()->object() == Pyc_None) { && start.cast<ASTObject>()->object() == Pyc_None) {
start = Node_NULL; start = NULL;
} }
if (end->type() == ASTNode::NODE_OBJECT if (end.type() == ASTNode::NODE_OBJECT
&& end.cast<ASTObject>()->object() == Pyc_None) { && end.cast<ASTObject>()->object() == Pyc_None) {
end = Node_NULL; end = NULL;
} }
if (step->type() == ASTNode::NODE_OBJECT if (step.type() == ASTNode::NODE_OBJECT
&& step.cast<ASTObject>()->object() == Pyc_None) { && step.cast<ASTObject>()->object() == Pyc_None) {
step = Node_NULL; step = NULL;
} }
/* We have to do this as a slice where one side is another slice */ /* We have to do this as a slice where one side is another slice */
/* [[a:b]:c] */ /* [[a:b]:c] */
if (start == Node_NULL && end == Node_NULL) { if (start == NULL && end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE0)); stack.push(new ASTSlice(ASTSlice::SLICE0));
} else if (start == Node_NULL) { } else if (start == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE2, start, end)); stack.push(new ASTSlice(ASTSlice::SLICE2, start, end));
} else if (end == Node_NULL) { } else if (end == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE1, start, end)); stack.push(new ASTSlice(ASTSlice::SLICE1, start, end));
} else { } else {
stack.push(new ASTSlice(ASTSlice::SLICE3, start, end)); stack.push(new ASTSlice(ASTSlice::SLICE3, start, end));
@@ -351,7 +351,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> lhs = stack.top(); PycRef<ASTNode> lhs = stack.top();
stack.pop(); stack.pop();
if (step == Node_NULL) { if (step == NULL) {
stack.push(new ASTSlice(ASTSlice::SLICE1, lhs, step)); stack.push(new ASTSlice(ASTSlice::SLICE1, lhs, step));
} else { } else {
stack.push(new ASTSlice(ASTSlice::SLICE3, lhs, step)); stack.push(new ASTSlice(ASTSlice::SLICE3, lhs, step));
@@ -383,7 +383,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
ASTTuple::value_t bases; ASTTuple::value_t bases;
bases.resize(basecnt); bases.resize(basecnt);
PycRef<ASTNode> TOS = stack.top(); PycRef<ASTNode> TOS = stack.top();
int TOS_type = TOS->type(); int TOS_type = TOS.type();
// bases are NODE_NAME at TOS // bases are NODE_NAME at TOS
while (TOS_type == ASTNode::NODE_NAME) { while (TOS_type == ASTNode::NODE_NAME) {
bases.resize(basecnt + 1); bases.resize(basecnt + 1);
@@ -391,7 +391,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
basecnt++; basecnt++;
stack.pop(); stack.pop();
TOS = stack.top(); TOS = stack.top();
TOS_type = TOS->type(); TOS_type = TOS.type();
} }
// qualified name is PycString at TOS // qualified name is PycString at TOS
PycRef<ASTNode> name = stack.top(); PycRef<ASTNode> name = stack.top();
@@ -400,7 +400,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.pop(); stack.pop();
PycRef<ASTNode> loadbuild = stack.top(); PycRef<ASTNode> loadbuild = stack.top();
stack.pop(); stack.pop();
int loadbuild_type = loadbuild->type(); int loadbuild_type = loadbuild.type();
if (loadbuild_type == ASTNode::NODE_LOADBUILDCLASS) { if (loadbuild_type == ASTNode::NODE_LOADBUILDCLASS) {
PycRef<ASTNode> call = new ASTCall(function, pparamList, kwparamList); PycRef<ASTNode> call = new ASTCall(function, pparamList, kwparamList);
stack.push(new ASTClass(call, new ASTTuple(bases), name)); stack.push(new ASTClass(call, new ASTTuple(bases), name));
@@ -423,7 +423,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
for (int i=0; i<pparams; i++) { for (int i=0; i<pparams; i++) {
PycRef<ASTNode> param = stack.top(); PycRef<ASTNode> param = stack.top();
stack.pop(); stack.pop();
if (param->type() == ASTNode::NODE_FUNCTION) { if (param.type() == ASTNode::NODE_FUNCTION) {
PycRef<ASTNode> code = param.cast<ASTFunction>()->code(); PycRef<ASTNode> code = param.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>(); PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>();
PycRef<PycString> function_name = code_src->name(); PycRef<PycString> function_name = code_src->name();
@@ -607,7 +607,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> name = stack.top(); PycRef<ASTNode> name = stack.top();
stack.pop(); stack.pop();
curblock->append(new ASTDelete(new ASTSubscr(name, new ASTSlice(ASTSlice::SLICE2, Node_NULL, lower)))); curblock->append(new ASTDelete(new ASTSubscr(name, new ASTSlice(ASTSlice::SLICE2, NULL, lower))));
} }
break; break;
case Pyc::DELETE_SLICE_3: case Pyc::DELETE_SLICE_3:
@@ -744,7 +744,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
blocks.push(forblk.cast<ASTBlock>()); blocks.push(forblk.cast<ASTBlock>());
curblock = blocks.top(); curblock = blocks.top();
stack.push(Node_NULL); stack.push(NULL);
} }
break; break;
case Pyc::FOR_LOOP_A: case Pyc::FOR_LOOP_A:
@@ -771,7 +771,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
and the current item onto the stack." */ and the current item onto the stack." */
stack.push(iter); stack.push(iter);
stack.push(curidx); stack.push(curidx);
stack.push(Node_NULL); // We can totally hack this >_> stack.push(NULL); // We can totally hack this >_>
} }
break; break;
case Pyc::GET_ITER: case Pyc::GET_ITER:
@@ -779,7 +779,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break; break;
case Pyc::IMPORT_NAME_A: case Pyc::IMPORT_NAME_A:
if (mod->majorVer() == 1) { if (mod->majorVer() == 1) {
stack.push(new ASTImport(new ASTName(code->getName(operand)), Node_NULL)); stack.push(new ASTImport(new ASTName(code->getName(operand)), NULL));
} else { } else {
PycRef<ASTNode> fromlist = stack.top(); PycRef<ASTNode> fromlist = stack.top();
stack.pop(); stack.pop();
@@ -795,7 +795,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
{ {
PycRef<ASTNode> import = stack.top(); PycRef<ASTNode> import = stack.top();
stack.pop(); stack.pop();
curblock->append(new ASTStore(import, Node_NULL)); curblock->append(new ASTStore(import, NULL));
} }
break; break;
case Pyc::INPLACE_ADD: case Pyc::INPLACE_ADD:
@@ -964,10 +964,10 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
offs = pos + operand; offs = pos + operand;
} }
if (cond->type() == ASTNode::NODE_COMPARE if (cond.type() == ASTNode::NODE_COMPARE
&& cond.cast<ASTCompare>()->op() == ASTCompare::CMP_EXCEPTION) { && cond.cast<ASTCompare>()->op() == ASTCompare::CMP_EXCEPTION) {
if (curblock->blktype() == ASTBlock::BLK_EXCEPT if (curblock->blktype() == ASTBlock::BLK_EXCEPT
&& curblock.cast<ASTCondBlock>()->cond() == Node_NULL) { && curblock.cast<ASTCondBlock>()->cond() == NULL) {
blocks.pop(); blocks.pop();
curblock = blocks.top(); curblock = blocks.top();
@@ -1036,7 +1036,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
&& curblock.cast<ASTIterBlock>()->isComprehension()) { && curblock.cast<ASTIterBlock>()->isComprehension()) {
PycRef<ASTNode> top = stack.top(); PycRef<ASTNode> top = stack.top();
if (top->type() == ASTNode::NODE_COMPREHENSION) { if (top.type() == ASTNode::NODE_COMPREHENSION) {
PycRef<ASTComprehension> comp = top.cast<ASTComprehension>(); PycRef<ASTComprehension> comp = top.cast<ASTComprehension>();
comp->addGenerator(curblock.cast<ASTIterBlock>()); comp->addGenerator(curblock.cast<ASTIterBlock>());
@@ -1070,7 +1070,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (curblock->blktype() == ASTBlock::BLK_CONTAINER) { if (curblock->blktype() == ASTBlock::BLK_CONTAINER) {
PycRef<ASTContainerBlock> cont = curblock.cast<ASTContainerBlock>(); PycRef<ASTContainerBlock> cont = curblock.cast<ASTContainerBlock>();
if (cont->hasExcept() && pos < cont->except()) { if (cont->hasExcept() && pos < cont->except()) {
PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, 0, Node_NULL, false); PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, 0, NULL, false);
except->init(); except->init();
blocks.push(except); blocks.push(except);
curblock = blocks.top(); curblock = blocks.top();
@@ -1106,7 +1106,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (push) { if (push) {
stack_hist.push(stack); stack_hist.push(stack);
} }
PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_EXCEPT, blocks.top()->end(), Node_NULL, false); PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_EXCEPT, blocks.top()->end(), NULL, false);
next->init(); next->init();
blocks.push(next.cast<ASTBlock>()); blocks.push(next.cast<ASTBlock>());
@@ -1136,7 +1136,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack_hist.push(stack); stack_hist.push(stack);
curblock->setEnd(pos+operand); curblock->setEnd(pos+operand);
PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, Node_NULL, false); PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, NULL, false);
except->init(); except->init();
blocks.push(except); blocks.push(except);
curblock = blocks.top(); curblock = blocks.top();
@@ -1196,7 +1196,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (push) { if (push) {
stack_hist.push(stack); stack_hist.push(stack);
} }
PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, Node_NULL, false); PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, NULL, false);
next->init(); next->init();
blocks.push(next.cast<ASTBlock>()); blocks.push(next.cast<ASTBlock>());
@@ -1227,7 +1227,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack_hist.push(stack); stack_hist.push(stack);
} }
PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, Node_NULL, false); PycRef<ASTBlock> except = new ASTCondBlock(ASTBlock::BLK_EXCEPT, pos+operand, NULL, false);
except->init(); except->init();
blocks.push(except); blocks.push(except);
} }
@@ -1268,7 +1268,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
case Pyc::LOAD_ATTR_A: case Pyc::LOAD_ATTR_A:
{ {
PycRef<ASTNode> name = stack.top(); PycRef<ASTNode> name = stack.top();
if (name->type() != ASTNode::NODE_IMPORT) { if (name.type() != ASTNode::NODE_IMPORT) {
stack.pop(); stack.pop();
stack.push(new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR)); stack.push(new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR));
} }
@@ -1281,13 +1281,13 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
{ {
PycRef<ASTObject> t_ob = new ASTObject(code->getConst(operand)); PycRef<ASTObject> t_ob = new ASTObject(code->getConst(operand));
if ((t_ob->object()->type() == PycObject::TYPE_TUPLE || if ((t_ob->object().type() == PycObject::TYPE_TUPLE ||
t_ob->object()->type() == PycObject::TYPE_SMALL_TUPLE) && t_ob->object().type() == PycObject::TYPE_SMALL_TUPLE) &&
!t_ob->object().cast<PycTuple>()->values().size()) { !t_ob->object().cast<PycTuple>()->values().size()) {
ASTTuple::value_t values; ASTTuple::value_t values;
stack.push(new ASTTuple(values)); stack.push(new ASTTuple(values));
} else if (t_ob->object()->type() == PycObject::TYPE_NONE) { } else if (t_ob->object().type() == PycObject::TYPE_NONE) {
stack.push(Node_NULL); stack.push(NULL);
} else { } else {
stack.push(t_ob.cast<ASTNode>()); stack.push(t_ob.cast<ASTNode>());
} }
@@ -1318,7 +1318,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.pop(); stack.pop();
/* Test for the qualified name of the function (at TOS) */ /* Test for the qualified name of the function (at TOS) */
int tos_type = code.cast<ASTObject>()->object()->type(); int tos_type = code.cast<ASTObject>()->object().type();
if (tos_type != PycObject::TYPE_CODE && if (tos_type != PycObject::TYPE_CODE &&
tos_type != PycObject::TYPE_CODE2) { tos_type != PycObject::TYPE_CODE2) {
code = stack.top(); code = stack.top();
@@ -1351,7 +1351,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTBlock> tmp; PycRef<ASTBlock> tmp;
if (curblock->nodes().size() && if (curblock->nodes().size() &&
curblock->nodes().back()->type() == ASTNode::NODE_KEYWORD) { curblock->nodes().back().type() == ASTNode::NODE_KEYWORD) {
curblock->removeLast(); curblock->removeLast();
} }
@@ -1444,11 +1444,11 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
curblock.cast<ASTCondBlock>()->init(); curblock.cast<ASTCondBlock>()->init();
} }
break; break;
} else if (value->type() == ASTNode::NODE_INVALID } else if (value.type() == ASTNode::NODE_INVALID
|| value->type() == ASTNode::NODE_BINARY || value.type() == ASTNode::NODE_BINARY
|| value->type() == ASTNode::NODE_NAME) { || value.type() == ASTNode::NODE_NAME) {
break; break;
} else if (value->type() == ASTNode::NODE_COMPARE } else if (value.type() == ASTNode::NODE_COMPARE
&& value.cast<ASTCompare>()->op() == ASTCompare::CMP_EXCEPTION) { && value.cast<ASTCompare>()->op() == ASTCompare::CMP_EXCEPTION) {
break; break;
} }
@@ -1461,7 +1461,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
* If it's a comprehension, the only POP_TOP should be * If it's a comprehension, the only POP_TOP should be
* a call to append the iter to the list. * a call to append the iter to the list.
*/ */
if (value->type() == ASTNode::NODE_CALL) { if (value.type() == ASTNode::NODE_CALL) {
PycRef<ASTNode> res = value.cast<ASTCall>()->pparams().front(); PycRef<ASTNode> res = value.cast<ASTCall>()->pparams().front();
stack.push(new ASTComprehension(res)); stack.push(new ASTComprehension(res));
@@ -1483,10 +1483,10 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break; break;
} }
case Pyc::PRINT_NEWLINE: case Pyc::PRINT_NEWLINE:
curblock->append(new ASTPrint(Node_NULL)); curblock->append(new ASTPrint(NULL));
break; break;
case Pyc::PRINT_NEWLINE_TO: case Pyc::PRINT_NEWLINE_TO:
curblock->append(new ASTPrint(Node_NULL, stack.top())); curblock->append(new ASTPrint(NULL, stack.top()));
stack.pop(); stack.pop();
break; break;
case Pyc::RAISE_VARARGS_A: case Pyc::RAISE_VARARGS_A:
@@ -1592,7 +1592,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> none = stack.top(); PycRef<ASTNode> none = stack.top();
stack.pop(); stack.pop();
if (none != Node_NULL) { if (none != NULL) {
fprintf(stderr, "Something TERRIBLE happened!\n"); fprintf(stderr, "Something TERRIBLE happened!\n");
break; break;
} }
@@ -1638,7 +1638,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break; break;
case Pyc::SETUP_LOOP_A: case Pyc::SETUP_LOOP_A:
{ {
PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_WHILE, pos+operand, Node_NULL, false); PycRef<ASTBlock> next = new ASTCondBlock(ASTBlock::BLK_WHILE, pos+operand, NULL, false);
blocks.push(next.cast<ASTBlock>()); blocks.push(next.cast<ASTBlock>());
curblock = blocks.top(); curblock = blocks.top();
} }
@@ -1670,7 +1670,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> name = stack.top(); PycRef<ASTNode> name = stack.top();
stack.pop(); stack.pop();
PycRef<ASTNode> slice = new ASTSlice(ASTSlice::SLICE2, Node_NULL, upper); PycRef<ASTNode> slice = new ASTSlice(ASTSlice::SLICE2, NULL, upper);
stack.push(new ASTSubscr(name, slice)); stack.push(new ASTSubscr(name, slice));
} }
break; break;
@@ -1695,7 +1695,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> attr = new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR); PycRef<ASTNode> attr = new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR);
PycRef<ASTNode> tup = stack.top(); PycRef<ASTNode> tup = stack.top();
if (tup->type() == ASTNode::NODE_TUPLE) { if (tup.type() == ASTNode::NODE_TUPLE) {
stack.pop(); stack.pop();
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>(); PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
@@ -1731,7 +1731,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> name = new ASTName(code->getCellVar(operand).cast<PycString>()); PycRef<ASTNode> name = new ASTName(code->getCellVar(operand).cast<PycString>());
PycRef<ASTNode> tup = stack.top(); PycRef<ASTNode> tup = stack.top();
if (tup->type() == ASTNode::NODE_TUPLE) { if (tup.type() == ASTNode::NODE_TUPLE) {
stack.pop(); stack.pop();
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>(); PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
@@ -1769,7 +1769,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
name = new ASTName(code->getVarName(operand)); name = new ASTName(code->getVarName(operand));
PycRef<ASTNode> tup = stack.top(); PycRef<ASTNode> tup = stack.top();
if (tup->type() == ASTNode::NODE_TUPLE) { if (tup.type() == ASTNode::NODE_TUPLE) {
stack.pop(); stack.pop();
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>(); PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
@@ -1828,7 +1828,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (unpack) { if (unpack) {
PycRef<ASTNode> tup = stack.top(); PycRef<ASTNode> tup = stack.top();
if (tup->type() == ASTNode::NODE_TUPLE) { if (tup.type() == ASTNode::NODE_TUPLE) {
stack.pop(); stack.pop();
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>(); PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
@@ -1868,7 +1868,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> name = new ASTName(code->getName(operand)); PycRef<ASTNode> name = new ASTName(code->getName(operand));
PycRef<ASTNode> tup = stack.top(); PycRef<ASTNode> tup = stack.top();
if (tup->type() == ASTNode::NODE_TUPLE) { if (tup.type() == ASTNode::NODE_TUPLE) {
stack.pop(); stack.pop();
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>(); PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
@@ -1907,7 +1907,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (curblock->blktype() == ASTBlock::BLK_FOR if (curblock->blktype() == ASTBlock::BLK_FOR
&& !curblock->inited()) { && !curblock->inited()) {
curblock.cast<ASTIterBlock>()->setIndex(name); curblock.cast<ASTIterBlock>()->setIndex(name);
} else if (stack.top()->type() == ASTNode::NODE_IMPORT) { } else if (stack.top().type() == ASTNode::NODE_IMPORT) {
PycRef<ASTImport> import = stack.top().cast<ASTImport>(); PycRef<ASTImport> import = stack.top().cast<ASTImport>();
import->add_store(new ASTStore(value, name)); import->add_store(new ASTStore(value, name));
@@ -1918,7 +1918,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
} else { } else {
curblock->append(new ASTStore(value, name)); curblock->append(new ASTStore(value, name));
if (value->type() == ASTNode::NODE_INVALID) if (value.type() == ASTNode::NODE_INVALID)
break; break;
} }
} }
@@ -1955,7 +1955,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> value = stack.top(); PycRef<ASTNode> value = stack.top();
stack.pop(); stack.pop();
curblock->append(new ASTStore(value, new ASTSubscr(dest, new ASTSlice(ASTSlice::SLICE2, Node_NULL, lower)))); curblock->append(new ASTStore(value, new ASTSubscr(dest, new ASTSlice(ASTSlice::SLICE2, NULL, lower))));
} }
break; break;
case Pyc::STORE_SLICE_3: case Pyc::STORE_SLICE_3:
@@ -1983,7 +1983,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> save = new ASTSubscr(dest, subscr); PycRef<ASTNode> save = new ASTSubscr(dest, subscr);
PycRef<ASTNode> tup = stack.top(); PycRef<ASTNode> tup = stack.top();
if (tup->type() == ASTNode::NODE_TUPLE) { if (tup.type() == ASTNode::NODE_TUPLE) {
stack.pop(); stack.pop();
PycRef<ASTTuple> tuple = tup.cast<ASTTuple>(); PycRef<ASTTuple> tuple = tup.cast<ASTTuple>();
@@ -2010,7 +2010,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> src = stack.top(); PycRef<ASTNode> src = stack.top();
stack.pop(); stack.pop();
if (dest->type() == ASTNode::NODE_MAP) { if (dest.type() == ASTNode::NODE_MAP) {
dest.cast<ASTMap>()->add(subscr, src); dest.cast<ASTMap>()->add(subscr, src);
} else { } else {
curblock->append(new ASTStore(src, new ASTSubscr(dest, subscr))); curblock->append(new ASTStore(src, new ASTSubscr(dest, subscr)));
@@ -2125,20 +2125,20 @@ static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
Else we'd have expressions like (((a + b) + c) + d) when therefore Else we'd have expressions like (((a + b) + c) + d) when therefore
equivalent, a + b + c + d would suffice. */ equivalent, a + b + c + d would suffice. */
if (parent->type() == ASTNode::NODE_UNARY && parent.cast<ASTUnary>()->op() == ASTUnary::UN_NOT) if (parent.type() == ASTNode::NODE_UNARY && parent.cast<ASTUnary>()->op() == ASTUnary::UN_NOT)
return 1; // Always parenthesize not(x) return 1; // Always parenthesize not(x)
if (child->type() == ASTNode::NODE_BINARY) { if (child.type() == ASTNode::NODE_BINARY) {
PycRef<ASTBinary> binChild = child.cast<ASTBinary>(); PycRef<ASTBinary> binChild = child.cast<ASTBinary>();
if (parent->type() == ASTNode::NODE_BINARY) if (parent.type() == ASTNode::NODE_BINARY)
return binChild->op() - parent.cast<ASTBinary>()->op(); return binChild->op() - parent.cast<ASTBinary>()->op();
else if (parent->type() == ASTNode::NODE_COMPARE) else if (parent.type() == ASTNode::NODE_COMPARE)
return (binChild->op() == ASTBinary::BIN_LOG_AND || return (binChild->op() == ASTBinary::BIN_LOG_AND ||
binChild->op() == ASTBinary::BIN_LOG_OR) ? 1 : -1; binChild->op() == ASTBinary::BIN_LOG_OR) ? 1 : -1;
else if (parent->type() == ASTNode::NODE_UNARY) else if (parent.type() == ASTNode::NODE_UNARY)
return (binChild->op() == ASTBinary::BIN_POWER) ? -1 : 1; return (binChild->op() == ASTBinary::BIN_POWER) ? -1 : 1;
} else if (child->type() == ASTNode::NODE_UNARY) { } else if (child.type() == ASTNode::NODE_UNARY) {
PycRef<ASTUnary> unChild = child.cast<ASTUnary>(); PycRef<ASTUnary> unChild = child.cast<ASTUnary>();
if (parent->type() == ASTNode::NODE_BINARY) { if (parent.type() == ASTNode::NODE_BINARY) {
PycRef<ASTBinary> binParent = parent.cast<ASTBinary>(); PycRef<ASTBinary> binParent = parent.cast<ASTBinary>();
if (binParent->op() == ASTBinary::BIN_LOG_AND || if (binParent->op() == ASTBinary::BIN_LOG_AND ||
binParent->op() == ASTBinary::BIN_LOG_OR) binParent->op() == ASTBinary::BIN_LOG_OR)
@@ -2149,19 +2149,19 @@ static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
return 1; return 1;
else else
return -1; return -1;
} else if (parent->type() == ASTNode::NODE_COMPARE) { } else if (parent.type() == ASTNode::NODE_COMPARE) {
return (unChild->op() == ASTUnary::UN_NOT) ? 1 : -1; return (unChild->op() == ASTUnary::UN_NOT) ? 1 : -1;
} else if (parent->type() == ASTNode::NODE_UNARY) { } else if (parent.type() == ASTNode::NODE_UNARY) {
return unChild->op() - parent.cast<ASTUnary>()->op(); return unChild->op() - parent.cast<ASTUnary>()->op();
} }
} else if (child->type() == ASTNode::NODE_COMPARE) { } else if (child.type() == ASTNode::NODE_COMPARE) {
PycRef<ASTCompare> cmpChild = child.cast<ASTCompare>(); PycRef<ASTCompare> cmpChild = child.cast<ASTCompare>();
if (parent->type() == ASTNode::NODE_BINARY) if (parent.type() == ASTNode::NODE_BINARY)
return (parent.cast<ASTBinary>()->op() == ASTBinary::BIN_LOG_AND || return (parent.cast<ASTBinary>()->op() == ASTBinary::BIN_LOG_AND ||
parent.cast<ASTBinary>()->op() == ASTBinary::BIN_LOG_OR) ? -1 : 1; parent.cast<ASTBinary>()->op() == ASTBinary::BIN_LOG_OR) ? -1 : 1;
else if (parent->type() == ASTNode::NODE_COMPARE) else if (parent.type() == ASTNode::NODE_COMPARE)
return cmpChild->op() - parent.cast<ASTCompare>()->op(); return cmpChild->op() - parent.cast<ASTCompare>()->op();
else if (parent->type() == ASTNode::NODE_UNARY) else if (parent.type() == ASTNode::NODE_UNARY)
return (parent.cast<ASTUnary>()->op() == ASTUnary::UN_NOT) ? -1 : 1; return (parent.cast<ASTUnary>()->op() == ASTUnary::UN_NOT) ? -1 : 1;
} }
@@ -2172,8 +2172,8 @@ static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
static void print_ordered(PycRef<ASTNode> parent, PycRef<ASTNode> child, static void print_ordered(PycRef<ASTNode> parent, PycRef<ASTNode> child,
PycModule* mod) PycModule* mod)
{ {
if (child->type() == ASTNode::NODE_BINARY || if (child.type() == ASTNode::NODE_BINARY ||
child->type() == ASTNode::NODE_COMPARE) { child.type() == ASTNode::NODE_COMPARE) {
if (cmp_prec(parent, child) > 0) { if (cmp_prec(parent, child) > 0) {
fprintf(pyc_output, "("); fprintf(pyc_output, "(");
print_src(child, mod); print_src(child, mod);
@@ -2181,7 +2181,7 @@ static void print_ordered(PycRef<ASTNode> parent, PycRef<ASTNode> child,
} else { } else {
print_src(child, mod); print_src(child, mod);
} }
} else if (child->type() == ASTNode::NODE_UNARY) { } else if (child.type() == ASTNode::NODE_UNARY) {
if (cmp_prec(parent, child) > 0) { if (cmp_prec(parent, child) > 0) {
fprintf(pyc_output, "("); fprintf(pyc_output, "(");
print_src(child, mod); print_src(child, mod);
@@ -2220,7 +2220,7 @@ static void print_block(PycRef<ASTBlock> blk, PycModule* mod) {
} }
for (ASTBlock::list_t::const_iterator ln = lines.begin(); ln != lines.end();) { for (ASTBlock::list_t::const_iterator ln = lines.begin(); ln != lines.end();) {
if ((*ln).cast<ASTNode>()->type() != ASTNode::NODE_NODELIST) { if ((*ln).cast<ASTNode>().type() != ASTNode::NODE_NODELIST) {
start_line(cur_indent); start_line(cur_indent);
} }
print_src(*ln, mod); print_src(*ln, mod);
@@ -2232,7 +2232,7 @@ static void print_block(PycRef<ASTBlock> blk, PycModule* mod) {
void print_src(PycRef<ASTNode> node, PycModule* mod) void print_src(PycRef<ASTNode> node, PycModule* mod)
{ {
if (node == Node_NULL) { if (node == NULL) {
fprintf(pyc_output, "None"); fprintf(pyc_output, "None");
cleanBuild = true; cleanBuild = true;
return; return;
@@ -2303,11 +2303,11 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
fprintf(pyc_output, "exec "); fprintf(pyc_output, "exec ");
print_src(exec->statement(), mod); print_src(exec->statement(), mod);
if (exec->globals() != Node_NULL) { if (exec->globals() != NULL) {
fprintf(pyc_output, " in "); fprintf(pyc_output, " in ");
print_src(exec->globals(), mod); print_src(exec->globals(), mod);
if (exec->locals() != Node_NULL if (exec->locals() != NULL
&& exec->globals() != exec->locals()) { && exec->globals() != exec->locals()) {
fprintf(pyc_output, ", "); fprintf(pyc_output, ", ");
print_src(exec->locals(), mod); print_src(exec->locals(), mod);
@@ -2383,7 +2383,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
cur_indent++; cur_indent++;
ASTNodeList::list_t lines = node.cast<ASTNodeList>()->nodes(); ASTNodeList::list_t lines = node.cast<ASTNodeList>()->nodes();
for (ASTNodeList::list_t::const_iterator ln = lines.begin(); ln != lines.end(); ++ln) { for (ASTNodeList::list_t::const_iterator ln = lines.begin(); ln != lines.end(); ++ln) {
if ((*ln).cast<ASTNode>()->type() != ASTNode::NODE_NODELIST) { if ((*ln).cast<ASTNode>().type() != ASTNode::NODE_NODELIST) {
start_line(cur_indent); start_line(cur_indent);
} }
print_src(*ln, mod); print_src(*ln, mod);
@@ -2424,14 +2424,14 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
fprintf(pyc_output, " in "); fprintf(pyc_output, " in ");
print_src(blk.cast<ASTIterBlock>()->iter(), mod); print_src(blk.cast<ASTIterBlock>()->iter(), mod);
} else if (blk->blktype() == ASTBlock::BLK_EXCEPT && } else if (blk->blktype() == ASTBlock::BLK_EXCEPT &&
blk.cast<ASTCondBlock>()->cond() != Node_NULL) { blk.cast<ASTCondBlock>()->cond() != NULL) {
fprintf(pyc_output, " "); fprintf(pyc_output, " ");
print_src(blk.cast<ASTCondBlock>()->cond(), mod); print_src(blk.cast<ASTCondBlock>()->cond(), mod);
} else if (blk->blktype() == ASTBlock::BLK_WITH) { } else if (blk->blktype() == ASTBlock::BLK_WITH) {
fprintf(pyc_output, " "); fprintf(pyc_output, " ");
print_src(blk.cast<ASTWithBlock>()->expr(), mod); print_src(blk.cast<ASTWithBlock>()->expr(), mod);
PycRef<ASTNode> var = blk.cast<ASTWithBlock>()->var(); PycRef<ASTNode> var = blk.cast<ASTWithBlock>()->var();
if (var != Node_NULL) { if (var != NULL) {
fprintf(pyc_output, " as "); fprintf(pyc_output, " as ");
print_src(var, mod); print_src(var, mod);
} }
@@ -2450,7 +2450,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
case ASTNode::NODE_OBJECT: case ASTNode::NODE_OBJECT:
{ {
PycRef<PycObject> obj = node.cast<ASTObject>()->object(); PycRef<PycObject> obj = node.cast<ASTObject>()->object();
if (obj->type() == PycObject::TYPE_CODE) { if (obj.type() == PycObject::TYPE_CODE) {
PycRef<PycCode> code = obj.cast<PycCode>(); PycRef<PycCode> code = obj.cast<PycCode>();
decompyle(code, mod); decompyle(code, mod);
} else { } else {
@@ -2462,10 +2462,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
fprintf(pyc_output, "pass"); fprintf(pyc_output, "pass");
break; break;
case ASTNode::NODE_PRINT: case ASTNode::NODE_PRINT:
if (node.cast<ASTPrint>()->value() == Node_NULL) { if (node.cast<ASTPrint>()->value() == NULL) {
if (!inPrint) { if (!inPrint) {
fprintf(pyc_output, "print "); fprintf(pyc_output, "print ");
if (node.cast<ASTPrint>()->stream() != Node_NULL) { if (node.cast<ASTPrint>()->stream() != NULL) {
fprintf(pyc_output, ">>"); fprintf(pyc_output, ">>");
print_src(node.cast<ASTPrint>()->stream(), mod); print_src(node.cast<ASTPrint>()->stream(), mod);
} }
@@ -2473,7 +2473,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
inPrint = false; inPrint = false;
} else if (!inPrint) { } else if (!inPrint) {
fprintf(pyc_output, "print "); fprintf(pyc_output, "print ");
if (node.cast<ASTPrint>()->stream() != Node_NULL) { if (node.cast<ASTPrint>()->stream() != NULL) {
fprintf(pyc_output, ">>"); fprintf(pyc_output, ">>");
print_src(node.cast<ASTPrint>()->stream(), mod); print_src(node.cast<ASTPrint>()->stream(), mod);
fprintf(pyc_output, ", "); fprintf(pyc_output, ", ");
@@ -2534,7 +2534,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
ASTImport::list_t stores = import->stores(); ASTImport::list_t stores = import->stores();
fprintf(pyc_output, "from "); fprintf(pyc_output, "from ");
if (import->name()->type() == ASTNode::NODE_IMPORT) if (import->name().type() == ASTNode::NODE_IMPORT)
print_src(import->name().cast<ASTImport>()->name(), mod); print_src(import->name().cast<ASTImport>()->name(), mod);
else else
print_src(import->name(), mod); print_src(import->name(), mod);
@@ -2598,7 +2598,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
{ {
PycRef<ASTNode> src = node.cast<ASTStore>()->src(); PycRef<ASTNode> src = node.cast<ASTStore>()->src();
PycRef<ASTNode> dest = node.cast<ASTStore>()->dest(); PycRef<ASTNode> dest = node.cast<ASTStore>()->dest();
if (src->type() == ASTNode::NODE_FUNCTION) { if (src.type() == ASTNode::NODE_FUNCTION) {
PycRef<ASTNode> code = src.cast<ASTFunction>()->code(); PycRef<ASTNode> code = src.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>(); PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>();
bool isLambda = false; bool isLambda = false;
@@ -2663,7 +2663,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
print_src(code, mod); print_src(code, mod);
inLambda = preLambda; inLambda = preLambda;
} else if (src->type() == ASTNode::NODE_CLASS) { } else if (src.type() == ASTNode::NODE_CLASS) {
fprintf(pyc_output, "\n"); fprintf(pyc_output, "\n");
start_line(cur_indent); start_line(cur_indent);
fprintf(pyc_output, "class "); fprintf(pyc_output, "class ");
@@ -2687,19 +2687,19 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
PycRef<ASTNode> code = src.cast<ASTClass>()->code().cast<ASTCall>() PycRef<ASTNode> code = src.cast<ASTClass>()->code().cast<ASTCall>()
->func().cast<ASTFunction>()->code(); ->func().cast<ASTFunction>()->code();
print_src(code, mod); print_src(code, mod);
} else if (src->type() == ASTNode::NODE_IMPORT) { } else if (src.type() == ASTNode::NODE_IMPORT) {
PycRef<ASTImport> import = src.cast<ASTImport>(); PycRef<ASTImport> import = src.cast<ASTImport>();
if (import->fromlist() != Node_NULL) { if (import->fromlist() != NULL) {
PycRef<PycObject> fromlist = import->fromlist().cast<ASTObject>()->object(); PycRef<PycObject> fromlist = import->fromlist().cast<ASTObject>()->object();
if (fromlist != Pyc_None) { if (fromlist != Pyc_None) {
fprintf(pyc_output, "from "); fprintf(pyc_output, "from ");
if (import->name()->type() == ASTNode::NODE_IMPORT) if (import->name().type() == ASTNode::NODE_IMPORT)
print_src(import->name().cast<ASTImport>()->name(), mod); print_src(import->name().cast<ASTImport>()->name(), mod);
else else
print_src(import->name(), mod); print_src(import->name(), mod);
fprintf(pyc_output, " import "); fprintf(pyc_output, " import ");
if (fromlist->type() == PycObject::TYPE_TUPLE || if (fromlist.type() == PycObject::TYPE_TUPLE ||
fromlist->type() == PycObject::TYPE_SMALL_TUPLE) { fromlist.type() == PycObject::TYPE_SMALL_TUPLE) {
bool first = true; bool first = true;
PycTuple::value_t::const_iterator ii = fromlist.cast<PycTuple>()->values().begin(); PycTuple::value_t::const_iterator ii = fromlist.cast<PycTuple>()->values().begin();
for (; ii != fromlist.cast<PycTuple>()->values().end(); ++ii) { for (; ii != fromlist.cast<PycTuple>()->values().end(); ++ii) {
@@ -2724,7 +2724,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
print_src(dest, mod); print_src(dest, mod);
} }
} }
} else if (src->type() == ASTNode::NODE_BINARY && } else if (src.type() == ASTNode::NODE_BINARY &&
src.cast<ASTBinary>()->is_inplace() == true) { src.cast<ASTBinary>()->is_inplace() == true) {
print_src(src, mod); print_src(src, mod);
} else { } else {
@@ -2780,16 +2780,16 @@ bool print_docstring(PycRef<PycObject> obj, int indent, PycModule* mod)
{ {
// docstrings are translated from the bytecode __doc__ = 'string' to simply '''string''' // docstrings are translated from the bytecode __doc__ = 'string' to simply '''string'''
signed char prefix = -1; signed char prefix = -1;
if (obj->type() == PycObject::TYPE_STRING) if (obj.type() == PycObject::TYPE_STRING)
prefix = mod->majorVer() == 3 ? 'b' : 0; prefix = mod->majorVer() == 3 ? 'b' : 0;
else if (obj->type() == PycObject::TYPE_UNICODE) else if (obj.type() == PycObject::TYPE_UNICODE)
prefix = mod->majorVer() == 3 ? 0 : 'u'; prefix = mod->majorVer() == 3 ? 0 : 'u';
else if (obj->type() == PycObject::TYPE_INTERNED || else if (obj.type() == PycObject::TYPE_INTERNED ||
obj->type() == PycObject::TYPE_STRINGREF || obj.type() == PycObject::TYPE_STRINGREF ||
obj->type() == PycObject::TYPE_ASCII || obj.type() == PycObject::TYPE_ASCII ||
obj->type() == PycObject::TYPE_ASCII_INTERNED || obj.type() == PycObject::TYPE_ASCII_INTERNED ||
obj->type() == PycObject::TYPE_SHORT_ASCII || obj.type() == PycObject::TYPE_SHORT_ASCII ||
obj->type() == PycObject::TYPE_SHORT_ASCII_INTERNED) obj.type() == PycObject::TYPE_SHORT_ASCII_INTERNED)
prefix = 0; prefix = 0;
if (prefix != -1) { if (prefix != -1) {
start_line(indent); start_line(indent);
@@ -2810,10 +2810,10 @@ void decompyle(PycRef<PycCode> code, PycModule* mod)
// about, and would add extra code for re-compilation anyway. // about, and would add extra code for re-compilation anyway.
// We strip these lines out here, and then add a "pass" statement // We strip these lines out here, and then add a "pass" statement
// if the cleaned up code is empty // if the cleaned up code is empty
if (clean->nodes().front()->type() == ASTNode::NODE_STORE) { if (clean->nodes().front().type() == ASTNode::NODE_STORE) {
PycRef<ASTStore> store = clean->nodes().front().cast<ASTStore>(); PycRef<ASTStore> store = clean->nodes().front().cast<ASTStore>();
if (store->src()->type() == ASTNode::NODE_NAME && if (store->src().type() == ASTNode::NODE_NAME &&
store->dest()->type() == ASTNode::NODE_NAME) { store->dest().type() == ASTNode::NODE_NAME) {
PycRef<ASTName> src = store->src().cast<ASTName>(); PycRef<ASTName> src = store->src().cast<ASTName>();
PycRef<ASTName> dest = store->dest().cast<ASTName>(); PycRef<ASTName> dest = store->dest().cast<ASTName>();
if (src->name()->isEqual("__name__") && if (src->name()->isEqual("__name__") &&
@@ -2825,20 +2825,20 @@ void decompyle(PycRef<PycCode> code, PycModule* mod)
} }
} }
// Class and module docstrings may only appear at the beginning of their source // Class and module docstrings may only appear at the beginning of their source
if (printClassDocstring && clean->nodes().front()->type() == ASTNode::NODE_STORE) { if (printClassDocstring && clean->nodes().front().type() == ASTNode::NODE_STORE) {
PycRef<ASTStore> store = clean->nodes().front().cast<ASTStore>(); PycRef<ASTStore> store = clean->nodes().front().cast<ASTStore>();
if (store->dest()->type() == ASTNode::NODE_NAME && if (store->dest().type() == ASTNode::NODE_NAME &&
store->dest().cast<ASTName>()->name()->isEqual("__doc__") && store->dest().cast<ASTName>()->name()->isEqual("__doc__") &&
store->src()->type() == ASTNode::NODE_OBJECT) { store->src().type() == ASTNode::NODE_OBJECT) {
if (print_docstring(store->src().cast<ASTObject>()->object(), if (print_docstring(store->src().cast<ASTObject>()->object(),
cur_indent + (code->name()->isEqual("<module>") ? 0 : 1), mod)) cur_indent + (code->name()->isEqual("<module>") ? 0 : 1), mod))
clean->removeFirst(); clean->removeFirst();
} }
} }
if (clean->nodes().back()->type() == ASTNode::NODE_RETURN) { if (clean->nodes().back().type() == ASTNode::NODE_RETURN) {
PycRef<ASTReturn> ret = clean->nodes().back().cast<ASTReturn>(); PycRef<ASTReturn> ret = clean->nodes().back().cast<ASTReturn>();
if (ret->value() == Node_NULL || ret->value()->type() == ASTNode::NODE_LOCALS) { if (ret->value() == NULL || ret->value().type() == ASTNode::NODE_LOCALS) {
clean->removeLast(); // Always an extraneous return statement clean->removeLast(); // Always an extraneous return statement
} }
} }

View File

@@ -35,7 +35,7 @@ public:
void pop() { void pop() {
if (m_ptr > -1) if (m_ptr > -1)
m_stack[m_ptr--] = Node_NULL; m_stack[m_ptr--] = NULL;
} }
PycRef<ASTNode> top() const PycRef<ASTNode> top() const
@@ -43,7 +43,7 @@ public:
if (m_ptr > -1) if (m_ptr > -1)
return m_stack[m_ptr]; return m_stack[m_ptr];
else else
return Node_NULL; return NULL;
} }
void replace(const FastStack& copy) void replace(const FastStack& copy)

View File

@@ -141,6 +141,11 @@ bool Pyc::IsCompareArg(int opcode)
void print_const(PycRef<PycObject> obj, PycModule* mod) void print_const(PycRef<PycObject> obj, PycModule* mod)
{ {
if (obj == NULL) {
fprintf(pyc_output, "<NULL>");
return;
}
switch (obj->type()) { switch (obj->type()) {
case PycObject::TYPE_STRING: case PycObject::TYPE_STRING:
OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 'b' : 0); OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 'b' : 0);

View File

@@ -35,7 +35,7 @@ void PycLong::load(PycData* stream, PycModule*)
bool PycLong::isEqual(PycRef<PycObject> obj) const bool PycLong::isEqual(PycRef<PycObject> obj) const
{ {
if (type() != obj->type()) if (type() != obj.type())
return false; return false;
PycRef<PycLong> longObj = obj.cast<PycLong>(); PycRef<PycLong> longObj = obj.cast<PycLong>();
@@ -110,7 +110,7 @@ void PycFloat::load(PycData* stream, PycModule*)
bool PycFloat::isEqual(PycRef<PycObject> obj) const bool PycFloat::isEqual(PycRef<PycObject> obj) const
{ {
if (type() != obj->type()) if (type() != obj.type())
return false; return false;
PycRef<PycFloat> floatObj = obj.cast<PycFloat>(); PycRef<PycFloat> floatObj = obj.cast<PycFloat>();

View File

@@ -13,7 +13,7 @@ public:
bool isEqual(PycRef<PycObject> obj) const bool isEqual(PycRef<PycObject> obj) const
{ {
return (type() == obj->type()) && return (type() == obj.type()) &&
(m_value == obj.cast<PycInt>()->m_value); (m_value == obj.cast<PycInt>()->m_value);
} }
@@ -85,7 +85,7 @@ public:
bool isEqual(PycRef<PycObject> obj) const bool isEqual(PycRef<PycObject> obj) const
{ {
return (type() == obj->type()) && return (type() == obj.type()) &&
(m_value == obj.cast<PycCFloat>()->m_value); (m_value == obj.cast<PycCFloat>()->m_value);
} }

View File

@@ -5,7 +5,6 @@
#include "data.h" #include "data.h"
#include <cstdio> #include <cstdio>
PycRef<PycObject> Pyc_NULL = (PycObject*)0;
PycRef<PycObject> Pyc_None = new PycObject(PycObject::TYPE_NONE); PycRef<PycObject> Pyc_None = new PycObject(PycObject::TYPE_NONE);
PycRef<PycObject> Pyc_Ellipsis = new PycObject(PycObject::TYPE_ELLIPSIS); PycRef<PycObject> Pyc_Ellipsis = new PycObject(PycObject::TYPE_ELLIPSIS);
PycRef<PycObject> Pyc_StopIteration = new PycObject(PycObject::TYPE_STOPITER); PycRef<PycObject> Pyc_StopIteration = new PycObject(PycObject::TYPE_STOPITER);
@@ -16,7 +15,7 @@ PycRef<PycObject> CreateObject(int type)
{ {
switch (type) { switch (type) {
case PycObject::TYPE_NULL: case PycObject::TYPE_NULL:
return Pyc_NULL; return NULL;
case PycObject::TYPE_NONE: case PycObject::TYPE_NONE:
return Pyc_None; return Pyc_None;
case PycObject::TYPE_FALSE: case PycObject::TYPE_FALSE:
@@ -65,7 +64,7 @@ PycRef<PycObject> CreateObject(int type)
return new PycSet(type); return new PycSet(type);
default: default:
fprintf(stderr, "CreateObject: Got unsupported type 0x%X\n", type); fprintf(stderr, "CreateObject: Got unsupported type 0x%X\n", type);
return Pyc_NULL; return NULL;
} }
} }
@@ -79,7 +78,7 @@ PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod)
obj = mod->getRef(index); obj = mod->getRef(index);
} else { } else {
obj = CreateObject(type & 0x7F); obj = CreateObject(type & 0x7F);
if (obj != Pyc_NULL) { if (obj != NULL) {
if (type & 0x80) if (type & 0x80)
mod->refObject(obj); mod->refObject(obj);
obj->load(stream, mod); obj->load(stream, mod);

View File

@@ -53,10 +53,14 @@ public:
_Obj* operator->() const { return m_obj; } _Obj* operator->() const { return m_obj; }
operator _Obj*() const { return m_obj; } operator _Obj*() const { return m_obj; }
inline int type() const;
/* This is just for coding convenience -- no type checking is done! */ /* This is just for coding convenience -- no type checking is done! */
template <class _Cast> template <class _Cast>
PycRef<_Cast> cast() const { return static_cast<_Cast*>(m_obj); } PycRef<_Cast> cast() const { return static_cast<_Cast*>(m_obj); }
bool isIdent(const _Obj* obj) { return m_obj == obj; }
private: private:
_Obj* m_obj; _Obj* m_obj;
}; };
@@ -106,10 +110,10 @@ public:
PycObject(int type = TYPE_UNKNOWN) : m_refs(0), m_type(type) { } PycObject(int type = TYPE_UNKNOWN) : m_refs(0), m_type(type) { }
virtual ~PycObject() { } virtual ~PycObject() { }
int type() const { return internalGetType(this); } int type() const { return m_type; }
virtual bool isEqual(PycRef<PycObject> obj) const virtual bool isEqual(PycRef<PycObject> obj) const
{ return (this == (PycObject*)obj); } { return obj.isIdent(this); }
virtual void load(PycData*, PycModule*) { } virtual void load(PycData*, PycModule*) { }
@@ -117,22 +121,19 @@ private:
int m_refs; int m_refs;
int m_type; int m_type;
// Hack to make clang happy :(
static int internalGetType(const PycObject *obj)
{
return obj ? obj->m_type : TYPE_NULL;
}
public: public:
void addRef() { ++m_refs; } void addRef() { ++m_refs; }
void delRef() { if (--m_refs == 0) delete this; } void delRef() { if (--m_refs == 0) delete this; }
}; };
template <class _Obj>
int PycRef<_Obj>::type() const
{ return m_obj ? m_obj->type() : PycObject::TYPE_NULL; }
PycRef<PycObject> CreateObject(int type); PycRef<PycObject> CreateObject(int type);
PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod); PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod);
/* Static Singleton objects */ /* Static Singleton objects */
extern PycRef<PycObject> Pyc_NULL;
extern PycRef<PycObject> Pyc_None; extern PycRef<PycObject> Pyc_None;
extern PycRef<PycObject> Pyc_Ellipsis; extern PycRef<PycObject> Pyc_Ellipsis;
extern PycRef<PycObject> Pyc_StopIteration; extern PycRef<PycObject> Pyc_StopIteration;

View File

@@ -17,7 +17,7 @@ void PycTuple::load(PycData* stream, PycModule* mod)
bool PycTuple::isEqual(PycRef<PycObject> obj) const bool PycTuple::isEqual(PycRef<PycObject> obj) const
{ {
if (type() != obj->type()) if (type() != obj.type())
return false; return false;
PycRef<PycTuple> tupleObj = obj.cast<PycTuple>(); PycRef<PycTuple> tupleObj = obj.cast<PycTuple>();
@@ -44,7 +44,7 @@ void PycList::load(PycData* stream, PycModule* mod)
bool PycList::isEqual(PycRef<PycObject> obj) const bool PycList::isEqual(PycRef<PycObject> obj) const
{ {
if (type() != obj->type()) if (type() != obj.type())
return false; return false;
PycRef<PycList> listObj = obj.cast<PycList>(); PycRef<PycList> listObj = obj.cast<PycList>();
@@ -67,7 +67,7 @@ void PycDict::load(PycData* stream, PycModule* mod)
PycRef<PycObject> key, val; PycRef<PycObject> key, val;
for (;;) { for (;;) {
key = LoadObject(stream, mod); key = LoadObject(stream, mod);
if (key == Pyc_NULL) if (key == NULL)
break; break;
val = LoadObject(stream, mod); val = LoadObject(stream, mod);
m_keys.push_back(key); m_keys.push_back(key);
@@ -77,7 +77,7 @@ void PycDict::load(PycData* stream, PycModule* mod)
bool PycDict::isEqual(PycRef<PycObject> obj) const bool PycDict::isEqual(PycRef<PycObject> obj) const
{ {
if (type() != obj->type()) if (type() != obj.type())
return false; return false;
PycRef<PycDict> dictObj = obj.cast<PycDict>(); PycRef<PycDict> dictObj = obj.cast<PycDict>();
@@ -111,7 +111,7 @@ PycRef<PycObject> PycDict::get(PycRef<PycObject> key) const
return *vi; return *vi;
++ki, ++vi; ++ki, ++vi;
} }
return Pyc_NULL; // Disassembly shouldn't get non-existant keys return NULL; // Disassembly shouldn't get non-existant keys
} }
@@ -125,7 +125,7 @@ void PycSet::load(PycData* stream, PycModule* mod)
bool PycSet::isEqual(PycRef<PycObject> obj) const bool PycSet::isEqual(PycRef<PycObject> obj) const
{ {
if (type() != obj->type()) if (type() != obj.type())
return false; return false;
PycRef<PycSet> setObj = obj.cast<PycSet>(); PycRef<PycSet> setObj = obj.cast<PycSet>();

View File

@@ -84,7 +84,7 @@ void PycString::load(PycData* stream, PycModule* mod)
bool PycString::isEqual(PycRef<PycObject> obj) const bool PycString::isEqual(PycRef<PycObject> obj) const
{ {
if (type() != obj->type()) if (type() != obj.type())
return false; return false;
PycRef<PycString> strObj = obj.cast<PycString>(); PycRef<PycString> strObj = obj.cast<PycString>();

View File

@@ -65,6 +65,11 @@ static void iprintf(int indent, const char* fmt, ...)
void output_object(PycRef<PycObject> obj, PycModule* mod, int indent) void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
{ {
if (obj == NULL) {
iprintf(indent, "<NULL>");
return;
}
switch (obj->type()) { switch (obj->type()) {
case PycObject::TYPE_CODE: case PycObject::TYPE_CODE:
case PycObject::TYPE_CODE2: case PycObject::TYPE_CODE2:
@@ -81,31 +86,31 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
iprintf(indent + 1, "Flags: 0x%08X", codeObj->flags()); iprintf(indent + 1, "Flags: 0x%08X", codeObj->flags());
print_coflags(codeObj->flags()); print_coflags(codeObj->flags());
if (codeObj->names() != Pyc_NULL) { if (codeObj->names() != NULL) {
iprintf(indent + 1, "[Names]\n"); iprintf(indent + 1, "[Names]\n");
for (int i=0; i<codeObj->names()->size(); i++) for (int i=0; i<codeObj->names()->size(); i++)
output_object(codeObj->names()->get(i), mod, indent + 2); output_object(codeObj->names()->get(i), mod, indent + 2);
} }
if (codeObj->varNames() != Pyc_NULL) { if (codeObj->varNames() != NULL) {
iprintf(indent + 1, "[Var Names]\n"); iprintf(indent + 1, "[Var Names]\n");
for (int i=0; i<codeObj->varNames()->size(); i++) for (int i=0; i<codeObj->varNames()->size(); i++)
output_object(codeObj->varNames()->get(i), mod, indent + 2); output_object(codeObj->varNames()->get(i), mod, indent + 2);
} }
if (codeObj->freeVars() != Pyc_NULL) { if (codeObj->freeVars() != NULL) {
iprintf(indent + 1, "[Free Vars]\n"); iprintf(indent + 1, "[Free Vars]\n");
for (int i=0; i<codeObj->freeVars()->size(); i++) for (int i=0; i<codeObj->freeVars()->size(); i++)
output_object(codeObj->freeVars()->get(i), mod, indent + 2); output_object(codeObj->freeVars()->get(i), mod, indent + 2);
} }
if (codeObj->cellVars() != Pyc_NULL) { if (codeObj->cellVars() != NULL) {
iprintf(indent + 1, "[Cell Vars]\n"); iprintf(indent + 1, "[Cell Vars]\n");
for (int i=0; i<codeObj->cellVars()->size(); i++) for (int i=0; i<codeObj->cellVars()->size(); i++)
output_object(codeObj->cellVars()->get(i), mod, indent + 2); output_object(codeObj->cellVars()->get(i), mod, indent + 2);
} }
if (codeObj->consts() != Pyc_NULL) { if (codeObj->consts() != NULL) {
iprintf(indent + 1, "[Constants]\n"); iprintf(indent + 1, "[Constants]\n");
for (int i=0; i<codeObj->consts()->size(); i++) for (int i=0; i<codeObj->consts()->size(); i++)
output_object(codeObj->consts()->get(i), mod, indent + 2); output_object(codeObj->consts()->get(i), mod, indent + 2);