Visitor.py 27.6 KB
Newer Older
1 2
# cython: infer_types=True

3 4 5
#
#   Tree visitor and transform framework
#
6

Stefan Behnel's avatar
Stefan Behnel committed
7
from __future__ import absolute_import, print_function
8

9
import sys
10
import inspect
11

12 13 14 15 16 17
from . import TypeSlots
from . import Builtin
from . import Nodes
from . import ExprNodes
from . import Errors
from . import DebugFlags
18
from . import Future
19

20 21
import cython

22

23 24
cython.declare(_PRINTABLE=tuple)

25
if sys.version_info[0] >= 3:
26 27 28 29 30
    _PRINTABLE = (bytes, str, int, float)
else:
    _PRINTABLE = (str, unicode, long, int, float)


31
class TreeVisitor(object):
32 33 34 35 36 37 38
    """
    Base class for writing visitors for a Cython tree, contains utilities for
    recursing such trees using visitors. Each node is
    expected to have a child_attrs iterable containing the names of attributes
    containing child nodes or lists of child nodes. Lists are not considered
    part of the tree structure (i.e. contained nodes are considered direct
    children of the parent node).
39

40 41 42 43 44 45
    visit_children visits each of the children of a given node (see the visit_children
    documentation). When recursing the tree using visit_children, an attribute
    access_path is maintained which gives information about the current location
    in the tree as a stack of tuples: (parent_node, attrname, index), representing
    the node, attribute and optional list index that was taken in each step in the path to
    the current node.
46

47
    Example:
48

Stefan Behnel's avatar
Stefan Behnel committed
49
    >>> class SampleNode(object):
50 51 52 53 54 55 56 57 58 59
    ...     child_attrs = ["head", "body"]
    ...     def __init__(self, value, head=None, body=None):
    ...         self.value = value
    ...         self.head = head
    ...         self.body = body
    ...     def __repr__(self): return "SampleNode(%s)" % self.value
    ...
    >>> tree = SampleNode(0, SampleNode(1), [SampleNode(2), SampleNode(3)])
    >>> class MyVisitor(TreeVisitor):
    ...     def visit_SampleNode(self, node):
60
    ...         print("in %s %s" % (node.value, self.access_path))
61
    ...         self.visitchildren(node)
62
    ...         print("out %s" % node.value)
63 64 65 66 67 68 69 70 71 72 73 74 75
    ...
    >>> MyVisitor().visit(tree)
    in 0 []
    in 1 [(SampleNode(0), 'head', None)]
    out 1
    in 2 [(SampleNode(0), 'body', 0)]
    out 2
    in 3 [(SampleNode(0), 'body', 1)]
    out 3
    out 0
    """
    def __init__(self):
        super(TreeVisitor, self).__init__()
76
        self.dispatch_table = {}
77 78
        self.access_path = []

79
    def dump_node(self, node, indent=0):
80
        ignored = list(node.child_attrs or []) + [u'child_attrs', u'pos',
81
                                            u'gil_message', u'cpp_message',
82
                                            u'subexprs']
83
        values = []
84
        pos = getattr(node, 'pos', None)
85 86 87 88 89 90 91 92 93 94 95 96 97
        if pos:
            source = pos[0]
            if source:
                import os.path
                source = os.path.basename(source.get_description())
            values.append(u'%s:%s:%s' % (source, pos[1], pos[2]))
        attribute_names = dir(node)
        attribute_names.sort()
        for attr in attribute_names:
            if attr in ignored:
                continue
            if attr.startswith(u'_') or attr.endswith(u'_'):
                continue
98 99 100 101
            try:
                value = getattr(node, attr)
            except AttributeError:
                continue
102
            if value is None or value == 0:
103 104
                continue
            elif isinstance(value, list):
105
                value = u'[...]/%d' % len(value)
106
            elif not isinstance(value, _PRINTABLE):
107 108 109 110 111 112 113
                continue
            else:
                value = repr(value)
            values.append(u'%s = %s' % (attr, value))
        return u'%s(%s)' % (node.__class__.__name__,
                           u',\n    '.join(values))

114 115 116 117 118 119 120 121 122 123 124
    def _find_node_path(self, stacktrace):
        import os.path
        last_traceback = stacktrace
        nodes = []
        while hasattr(stacktrace, 'tb_frame'):
            frame = stacktrace.tb_frame
            node = frame.f_locals.get(u'self')
            if isinstance(node, Nodes.Node):
                code = frame.f_code
                method_name = code.co_name
                pos = (os.path.basename(code.co_filename),
125
                       frame.f_lineno)
126 127 128 129 130
                nodes.append((node, method_name, pos))
                last_traceback = stacktrace
            stacktrace = stacktrace.tb_next
        return (last_traceback, nodes)

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    def _raise_compiler_error(self, child, e):
        import sys
        trace = ['']
        for parent, attribute, index in self.access_path:
            node = getattr(parent, attribute)
            if index is None:
                index = ''
            else:
                node = node[index]
                index = u'[%d]' % index
            trace.append(u'%s.%s%s = %s' % (
                parent.__class__.__name__, attribute, index,
                self.dump_node(node)))
        stacktrace, called_nodes = self._find_node_path(sys.exc_info()[2])
        last_node = child
        for node, method_name, pos in called_nodes:
            last_node = node
            trace.append(u"File '%s', line %d, in %s: %s" % (
                pos[0], pos[1], method_name, self.dump_node(node)))
        raise Errors.CompilerCrash(
151
            getattr(last_node, 'pos', None), self.__class__.__name__,
152 153
            u'\n'.join(trace), e, stacktrace)

154
    @cython.final
155 156 157 158 159 160 161
    def find_handler(self, obj):
        # to resolve, try entire hierarchy
        cls = type(obj)
        pattern = "visit_%s"
        mro = inspect.getmro(cls)
        handler_method = None
        for mro_cls in mro:
Stefan Behnel's avatar
Stefan Behnel committed
162 163 164
            handler_method = getattr(self, pattern % mro_cls.__name__, None)
            if handler_method is not None:
                return handler_method
Stefan Behnel's avatar
Stefan Behnel committed
165
        print(type(self), cls)
Stefan Behnel's avatar
Stefan Behnel committed
166
        if self.access_path:
167 168 169
            print(self.access_path)
            print(self.access_path[-1][0].pos)
            print(self.access_path[-1][0].__dict__)
Stefan Behnel's avatar
Stefan Behnel committed
170
        raise RuntimeError("Visitor %r does not accept object: %s" % (self, obj))
171 172 173 174

    def visit(self, obj):
        return self._visit(obj)

175
    @cython.final
176
    def _visit(self, obj):
177
        try:
178
            try:
179
                handler_method = self.dispatch_table[type(obj)]
180
            except KeyError:
181 182 183
                handler_method = self.find_handler(obj)
                self.dispatch_table[type(obj)] = handler_method
            return handler_method(obj)
184
        except Errors.CompileError:
185
            raise
186 187
        except Errors.AbortError:
            raise
188
        except Exception as e:
189 190
            if DebugFlags.debug_no_exception_intercept:
                raise
191 192 193 194 195 196
            self._raise_compiler_error(obj, e)

    @cython.final
    def _visitchild(self, child, parent, attrname, idx):
        self.access_path.append((parent, attrname, idx))
        result = self._visit(child)
197 198 199 200
        self.access_path.pop()
        return result

    def visitchildren(self, parent, attrs=None):
201 202
        return self._visitchildren(parent, attrs)

203
    @cython.final
Stefan Behnel's avatar
Stefan Behnel committed
204
    @cython.locals(idx=int)
205
    def _visitchildren(self, parent, attrs):
206 207 208
        """
        Visits the children of the given parent. If parent is None, returns
        immediately (returning None).
209

210 211 212 213 214 215 216 217 218 219 220
        The return value is a dictionary giving the results for each
        child (mapping the attribute name to either the return value
        or a list of return values (in the case of multiple children
        in an attribute)).
        """
        if parent is None: return None
        result = {}
        for attr in parent.child_attrs:
            if attrs is not None and attr not in attrs: continue
            child = getattr(parent, attr)
            if child is not None:
221
                if type(child) is list:
222
                    childretval = [self._visitchild(x, parent, attr, idx) for idx, x in enumerate(child)]
223
                else:
224
                    childretval = self._visitchild(child, parent, attr, None)
225
                    assert not isinstance(childretval, list), 'Cannot insert list here: %s in %r' % (attr, parent)
226 227 228 229 230 231 232 233
                result[attr] = childretval
        return result


class VisitorTransform(TreeVisitor):
    """
    A tree transform is a base class for visitors that wants to do stream
    processing of the structure (rather than attributes etc.) of a tree.
234

235
    It implements __call__ to simply visit the argument node.
236

237 238 239 240
    It requires the visitor methods to return the nodes which should take
    the place of the visited node in the result tree (which can be the same
    or one or more replacement). Specifically, if the return value from
    a visitor method is:
241

242 243 244 245 246 247 248 249 250
    - [] or None; the visited node will be removed (set to None if an attribute and
    removed if in a list)
    - A single node; the visited node will be replaced by the returned node.
    - A list of nodes; the visited nodes will be replaced by all the nodes in the
    list. This will only work if the node was already a member of a list; if it
    was not, an exception will be raised. (Typically you want to ensure that you
    are within a StatListNode or similar before doing this.)
    """
    def visitchildren(self, parent, attrs=None):
251
        result = self._visitchildren(parent, attrs)
252
        for attr, newnode in result.items():
Stefan Behnel's avatar
Stefan Behnel committed
253
            if type(newnode) is not list:
254 255 256 257 258 259
                setattr(parent, attr, newnode)
            else:
                # Flatten the list one level and remove any None
                newlist = []
                for x in newnode:
                    if x is not None:
260
                        if type(x) is list:
261 262 263 264
                            newlist += x
                        else:
                            newlist.append(x)
                setattr(parent, attr, newlist)
265
        return result
266 267 268 269

    def recurse_to_children(self, node):
        self.visitchildren(node)
        return node
270

271
    def __call__(self, root):
272
        return self._visit(root)
273

274 275
class CythonTransform(VisitorTransform):
    """
276
    Certain common conventions and utilities for Cython transforms.
277 278 279

     - Sets up the context of the pipeline in self.context
     - Tracks directives in effect in self.current_directives
280 281 282 283 284
    """
    def __init__(self, context):
        super(CythonTransform, self).__init__()
        self.context = context

285
    def __call__(self, node):
286
        from . import ModuleNode
287 288 289 290 291 292 293 294 295 296 297
        if isinstance(node, ModuleNode.ModuleNode):
            self.current_directives = node.directives
        return super(CythonTransform, self).__call__(node)

    def visit_CompilerDirectivesNode(self, node):
        old = self.current_directives
        self.current_directives = node.directives
        self.visitchildren(node)
        self.current_directives = old
        return node

298 299 300 301
    def visit_Node(self, node):
        self.visitchildren(node)
        return node

Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
302 303
class ScopeTrackingTransform(CythonTransform):
    # Keeps track of type of scopes
304 305
    #scope_type: can be either of 'module', 'function', 'cclass', 'pyclass', 'struct'
    #scope_node: the node that owns the current scope
306

Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
307 308 309 310 311 312 313 314 315 316 317 318 319
    def visit_ModuleNode(self, node):
        self.scope_type = 'module'
        self.scope_node = node
        self.visitchildren(node)
        return node

    def visit_scope(self, node, scope_type):
        prev = self.scope_type, self.scope_node
        self.scope_type = scope_type
        self.scope_node = node
        self.visitchildren(node)
        self.scope_type, self.scope_node = prev
        return node
320

Dag Sverre Seljebotn's avatar
Dag Sverre Seljebotn committed
321 322 323 324 325 326 327 328 329 330 331 332
    def visit_CClassDefNode(self, node):
        return self.visit_scope(node, 'cclass')

    def visit_PyClassDefNode(self, node):
        return self.visit_scope(node, 'pyclass')

    def visit_FuncDefNode(self, node):
        return self.visit_scope(node, 'function')

    def visit_CStructOrUnionDefNode(self, node):
        return self.visit_scope(node, 'struct')

333 334 335

class EnvTransform(CythonTransform):
    """
336
    This transformation keeps a stack of the environments.
337 338
    """
    def __call__(self, root):
339 340
        self.env_stack = []
        self.enter_scope(root, root.scope)
341
        return super(EnvTransform, self).__call__(root)
342 343

    def current_env(self):
344 345 346 347
        return self.env_stack[-1][1]

    def current_scope_node(self):
        return self.env_stack[-1][0]
348

349 350 351
    def global_scope(self):
        return self.current_env().global_scope()

352 353 354 355 356 357
    def enter_scope(self, node, scope):
        self.env_stack.append((node, scope))

    def exit_scope(self):
        self.env_stack.pop()

358
    def visit_FuncDefNode(self, node):
359
        self.enter_scope(node, node.local_scope)
360
        self.visitchildren(node)
361
        self.exit_scope()
362 363
        return node

364 365 366 367
    def visit_GeneratorBodyDefNode(self, node):
        self.visitchildren(node)
        return node

368
    def visit_ClassDefNode(self, node):
369
        self.enter_scope(node, node.scope)
370
        self.visitchildren(node)
371
        self.exit_scope()
372 373
        return node

374
    def visit_CStructOrUnionDefNode(self, node):
375
        self.enter_scope(node, node.scope)
376
        self.visitchildren(node)
377
        self.exit_scope()
378 379
        return node

380 381
    def visit_ScopedExprNode(self, node):
        if node.expr_scope:
382
            self.enter_scope(node, node.expr_scope)
383
            self.visitchildren(node)
384
            self.exit_scope()
385 386 387 388
        else:
            self.visitchildren(node)
        return node

389 390 391 392 393
    def visit_CArgDeclNode(self, node):
        # default arguments are evaluated in the outer scope
        if node.default:
            attrs = [ attr for attr in node.child_attrs if attr != 'default' ]
            self.visitchildren(node, attrs)
394
            self.enter_scope(node, self.current_env().outer_scope)
395
            self.visitchildren(node, ('default',))
396
            self.exit_scope()
397 398 399 400
        else:
            self.visitchildren(node)
        return node

401

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
class NodeRefCleanupMixin(object):
    """
    Clean up references to nodes that were replaced.

    NOTE: this implementation assumes that the replacement is
    done first, before hitting any further references during
    normal tree traversal.  This needs to be arranged by calling
    "self.visitchildren()" at a proper place in the transform
    and by ordering the "child_attrs" of nodes appropriately.
    """
    def __init__(self, *args):
        super(NodeRefCleanupMixin, self).__init__(*args)
        self._replacements = {}

    def visit_CloneNode(self, node):
        arg = node.arg
        if arg not in self._replacements:
            self.visitchildren(node)
            arg = node.arg
        node.arg = self._replacements.get(arg, arg)
        return node

    def visit_ResultRefNode(self, node):
        expr = node.expression
        if expr is None or expr not in self._replacements:
            self.visitchildren(node)
            expr = node.expression
        if expr is not None:
            node.expression = self._replacements.get(expr, expr)
        return node

    def replace(self, node, replacement):
        self._replacements[node] = replacement
        return replacement


438 439 440 441 442 443 444 445 446
find_special_method_for_binary_operator = {
    '<':  '__lt__',
    '<=': '__le__',
    '==': '__eq__',
    '!=': '__ne__',
    '>=': '__ge__',
    '>':  '__gt__',
    '+':  '__add__',
    '&':  '__and__',
447
    '/':  '__div__',
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    '//': '__floordiv__',
    '<<': '__lshift__',
    '%':  '__mod__',
    '*':  '__mul__',
    '|':  '__or__',
    '**': '__pow__',
    '>>': '__rshift__',
    '-':  '__sub__',
    '^':  '__xor__',
    'in': '__contains__',
}.get


find_special_method_for_unary_operator = {
    'not': '__not__',
    '~':   '__inv__',
    '-':   '__neg__',
    '+':   '__pos__',
}.get


469 470 471
class MethodDispatcherTransform(EnvTransform):
    """
    Base class for transformations that want to intercept on specific
472 473 474
    builtin functions or methods of builtin types, including special
    methods triggered by Python operators.  Must run after declaration
    analysis when entries were assigned.
475 476 477 478 479 480 481

    Naming pattern for handler methods is as follows:

    * builtin functions: _handle_(general|simple|any)_function_NAME

    * builtin methods: _handle_(general|simple|any)_method_TYPENAME_METHODNAME
    """
482
    # only visit call nodes and Python operations
483 484 485 486 487 488 489 490 491 492 493 494 495
    def visit_GeneralCallNode(self, node):
        self.visitchildren(node)
        function = node.function
        if not function.type.is_pyobject:
            return node
        arg_tuple = node.positional_args
        if not isinstance(arg_tuple, ExprNodes.TupleNode):
            return node
        keyword_args = node.keyword_args
        if keyword_args and not isinstance(keyword_args, ExprNodes.DictNode):
            # can't handle **kwargs
            return node
        args = arg_tuple.args
496
        return self._dispatch_to_handler(node, function, args, keyword_args)
497 498 499 500 501 502 503 504 505 506 507

    def visit_SimpleCallNode(self, node):
        self.visitchildren(node)
        function = node.function
        if function.type.is_pyobject:
            arg_tuple = node.arg_tuple
            if not isinstance(arg_tuple, ExprNodes.TupleNode):
                return node
            args = arg_tuple.args
        else:
            args = node.args
508 509
        return self._dispatch_to_handler(node, function, args, None)

510
    def visit_PrimaryCmpNode(self, node):
511
        if node.cascade:
Stefan Behnel's avatar
Stefan Behnel committed
512 513 514
            # not currently handled below
            self.visitchildren(node)
            return node
515 516
        return self._visit_binop_node(node)

517
    def visit_BinopNode(self, node):
518 519 520
        return self._visit_binop_node(node)

    def _visit_binop_node(self, node):
521 522 523 524 525 526 527
        self.visitchildren(node)
        # FIXME: could special case 'not_in'
        special_method_name = find_special_method_for_binary_operator(node.operator)
        if special_method_name:
            operand1, operand2 = node.operand1, node.operand2
            if special_method_name == '__contains__':
                operand1, operand2 = operand2, operand1
528 529 530
            elif special_method_name == '__div__':
                if Future.division in self.current_env().global_scope().context.future_directives:
                    special_method_name = '__truediv__'
531 532 533 534 535 536 537
            obj_type = operand1.type
            if obj_type.is_builtin_type:
                type_name = obj_type.name
            else:
                type_name = "object"  # safety measure
            node = self._dispatch_to_method_handler(
                special_method_name, None, False, type_name,
538
                node, None, [operand1, operand2], None)
539 540 541 542 543 544 545 546 547 548 549 550 551 552
        return node

    def visit_UnopNode(self, node):
        self.visitchildren(node)
        special_method_name = find_special_method_for_unary_operator(node.operator)
        if special_method_name:
            operand = node.operand
            obj_type = operand.type
            if obj_type.is_builtin_type:
                type_name = obj_type.name
            else:
                type_name = "object"  # safety measure
            node = self._dispatch_to_method_handler(
                special_method_name, None, False, type_name,
553
                node, None, [operand], None)
554
        return node
555 556 557 558 559 560 561 562 563 564

    ### dispatch to specific handlers

    def _find_handler(self, match_name, has_kwargs):
        call_type = has_kwargs and 'general' or 'simple'
        handler = getattr(self, '_handle_%s_%s' % (call_type, match_name), None)
        if handler is None:
            handler = getattr(self, '_handle_any_%s' % match_name, None)
        return handler

565 566 567 568 569 570 571
    def _delegate_to_assigned_value(self, node, function, arg_list, kwargs):
        assignment = function.cf_state[0]
        value = assignment.rhs
        if value.is_name:
            if not value.entry or len(value.entry.cf_assignments) > 1:
                # the variable might have been reassigned => play safe
                return node
572
        elif value.is_attribute and value.obj.is_name:
573 574 575 576 577 578 579 580
            if not value.obj.entry or len(value.obj.entry.cf_assignments) > 1:
                # the underlying variable might have been reassigned => play safe
                return node
        else:
            return node
        return self._dispatch_to_handler(
            node, value, arg_list, kwargs)

Stefan Behnel's avatar
Stefan Behnel committed
581
    def _dispatch_to_handler(self, node, function, arg_list, kwargs):
582 583 584 585 586 587
        if function.is_name:
            # we only consider functions that are either builtin
            # Python functions or builtins that were already replaced
            # into a C function call (defined in the builtin scope)
            if not function.entry:
                return node
588 589 590
            is_builtin = (
                function.entry.is_builtin or
                function.entry is self.current_env().builtin_scope().lookup_here(function.name))
591
            if not is_builtin:
592 593 594 595 596
                if function.cf_state and function.cf_state.is_single:
                    # we know the value of the variable
                    # => see if it's usable instead
                    return self._delegate_to_assigned_value(
                        node, function, arg_list, kwargs)
597 598 599 600
                return node
            function_handler = self._find_handler(
                "function_%s" % function.name, kwargs)
            if function_handler is None:
601
                return self._handle_function(node, function.name, function, arg_list, kwargs)
602
            if kwargs:
603
                return function_handler(node, function, arg_list, kwargs)
604
            else:
605
                return function_handler(node, function, arg_list)
606
        elif function.is_attribute:
607
            attr_name = function.attribute
608 609
            if function.type.is_pyobject:
                self_arg = function.obj
610 611 612 613 614
            elif node.self and function.entry:
                entry = function.entry.as_variable
                if not entry or not entry.is_builtin:
                    return node
                # C implementation of a Python builtin method - see if we find further matches
615 616 617 618
                self_arg = node.self
                arg_list = arg_list[1:]  # drop CloneNode of self argument
            else:
                return node
619 620 621
            obj_type = self_arg.type
            is_unbound_method = False
            if obj_type.is_builtin_type:
622 623
                if (obj_type is Builtin.type_type and self_arg.is_name and
                        arg_list and arg_list[0].type.is_pyobject):
624 625
                    # calling an unbound method like 'list.append(L,x)'
                    # (ignoring 'type.mro()' here ...)
626
                    type_name = self_arg.name
627 628 629 630 631
                    self_arg = None
                    is_unbound_method = True
                else:
                    type_name = obj_type.name
            else:
632 633 634
                type_name = "object"  # safety measure
            return self._dispatch_to_method_handler(
                attr_name, self_arg, is_unbound_method, type_name,
635
                node, function, arg_list, kwargs)
636 637 638
        else:
            return node

639 640
    def _dispatch_to_method_handler(self, attr_name, self_arg,
                                    is_unbound_method, type_name,
641
                                    node, function, arg_list, kwargs):
642 643 644 645 646 647 648 649
        method_handler = self._find_handler(
            "method_%s_%s" % (type_name, attr_name), kwargs)
        if method_handler is None:
            if (attr_name in TypeSlots.method_name_to_slot
                    or attr_name == '__new__'):
                method_handler = self._find_handler(
                    "slot%s" % attr_name, kwargs)
            if method_handler is None:
650 651 652
                return self._handle_method(
                    node, type_name, attr_name, function,
                    arg_list, is_unbound_method, kwargs)
653 654 655
        if self_arg is not None:
            arg_list = [self_arg] + list(arg_list)
        if kwargs:
656
            result = method_handler(
657
                node, function, arg_list, is_unbound_method, kwargs)
658
        else:
659
            result = method_handler(
660
                node, function, arg_list, is_unbound_method)
661
        return result
662

663 664 665 666 667 668 669 670 671
    def _handle_function(self, node, function_name, function, arg_list, kwargs):
        """Fallback handler"""
        return node

    def _handle_method(self, node, type_name, attr_name, function,
                       arg_list, is_unbound_method, kwargs):
        """Fallback handler"""
        return node

672

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
class RecursiveNodeReplacer(VisitorTransform):
    """
    Recursively replace all occurrences of a node in a subtree by
    another node.
    """
    def __init__(self, orig_node, new_node):
        super(RecursiveNodeReplacer, self).__init__()
        self.orig_node, self.new_node = orig_node, new_node

    def visit_Node(self, node):
        self.visitchildren(node)
        if node is self.orig_node:
            return self.new_node
        else:
            return node

689 690 691
def recursively_replace_node(tree, old_node, new_node):
    replace_in = RecursiveNodeReplacer(old_node, new_node)
    replace_in(tree)
692 693


694 695 696 697 698 699 700 701 702 703
class NodeFinder(TreeVisitor):
    """
    Find out if a node appears in a subtree.
    """
    def __init__(self, node):
        super(NodeFinder, self).__init__()
        self.node = node
        self.found = False

    def visit_Node(self, node):
Stefan Behnel's avatar
Stefan Behnel committed
704 705 706
        if self.found:
            pass  # short-circuit
        elif node is self.node:
707 708
            self.found = True
        else:
Stefan Behnel's avatar
Stefan Behnel committed
709
            self._visitchildren(node, None)
710 711 712 713 714 715 716

def tree_contains(tree, node):
    finder = NodeFinder(node)
    finder.visit(tree)
    return finder.found


717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
# Utils
def replace_node(ptr, value):
    """Replaces a node. ptr is of the form used on the access path stack
    (parent, attrname, listidx|None)
    """
    parent, attrname, listidx = ptr
    if listidx is None:
        setattr(parent, attrname, value)
    else:
        getattr(parent, attrname)[listidx] = value

class PrintTree(TreeVisitor):
    """Prints a representation of the tree to standard output.
    Subclass and override repr_of to provide more information
    about nodes. """
732
    def __init__(self, start=None, end=None):
733
        TreeVisitor.__init__(self)
734
        self._indent = ""
735 736 737 738
        if start is not None or end is not None:
            self._line_range = (start or 0, end or 2**30)
        else:
            self._line_range = None
739 740 741

    def indent(self):
        self._indent += "  "
742

743 744 745 746 747
    def unindent(self):
        self._indent = self._indent[:-2]

    def __call__(self, tree, phase=None):
        print("Parse tree dump at phase '%s'" % phase)
748
        self.visit(tree)
Robert Bradshaw's avatar
Robert Bradshaw committed
749
        return tree
750 751 752 753 754 755

    # Don't do anything about process_list, the defaults gives
    # nice-looking name[idx] nodes which will visually appear
    # under the parent-node, not displaying the list itself in
    # the hierarchy.
    def visit_Node(self, node):
756 757 758 759
        line = node.pos[1]
        if self._line_range is None or self._line_range[0] <= line <= self._line_range[1]:
            if len(self.access_path) == 0:
                name = "(root)"
760
            else:
761 762 763 764 765 766
                parent, attr, idx = self.access_path[-1]
                if idx is not None:
                    name = "%s[%d]" % (attr, idx)
                else:
                    name = attr
            print("%s- %s: %s" % (self._indent, name, self.repr_of(node)))
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
        self.indent()
        self.visitchildren(node)
        self.unindent()
        return node

    def repr_of(self, node):
        if node is None:
            return "(none)"
        else:
            result = node.__class__.__name__
            if isinstance(node, ExprNodes.NameNode):
                result += "(type=%s, name=\"%s\")" % (repr(node.type), node.name)
            elif isinstance(node, Nodes.DefNode):
                result += "(name=\"%s\")" % node.name
            elif isinstance(node, ExprNodes.ExprNode):
                t = node.type
                result += "(type=%s)" % repr(t)
784 785 786 787 788 789 790 791 792
            elif node.pos:
                pos = node.pos
                path = pos[0].get_description()
                if '/' in path:
                    path = path.split('/')[-1]
                if '\\' in path:
                    path = path.split('\\')[-1]
                result += "(pos=(%s:%s:%s))" % (path, pos[1], pos[2])

793 794 795 796 797
            return result

if __name__ == "__main__":
    import doctest
    doctest.testmod()