Reference Guide  2.5.0
debug_writer.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2023-2024, Science and Technology Facilities Council.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 #
17 # * Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 # -----------------------------------------------------------------------------
34 # Author: S. Siso, STFC Daresbury Lab
35 
36 '''A PSyIR backend to create Fortran-like strings without lowering the
37 higher level nodes. This is useful for printing debug information. '''
38 
39 from psyclone.psyir.nodes import DataNode
40 from psyclone.psyir.backend.fortran import FortranWriter
41 
42 
44  '''Implements a PSyIR backend to generate Fortran-like strings but without
45  lowering the higher level concepts to language constructs. This special
46  backend can do it because instead of failing for an unknown construct it
47  will just print the name of the higher level Node.
48  The resulting code will not be compilable but it will be usable for
49  generating debug information much faster than the FortranWriter because
50  it does not need to lower the nodes and therefore it does not need to
51  deepcopy the tree.
52 
53  '''
54  # This option will disable the lowering of abstract nodes into language
55  # level nodes, and as a consequence the backend does not need to deep-copy
56  # the tree and is much faster to execute.
57  # Be careful not to modify anything from the input tree when this option
58  # is set to True as the modifications will persist after the Writer!
59  _DISABLE_LOWERING = True
60 
61  def __init__(self):
62  super().__init__(check_global_constraints=False)
63 
64  def node_node(self, node):
65  ''' The DebugWriter must never fail with unrecognized nodes, this
66  generic visitor will capture any Node that the super Fortran backend
67  did not catch and output it as "< Node string >".
68 
69  :param node: the PSyIR node to translate.
70  :type node: :py:class:`psyclone.psyir.node.Node`
71 
72  :returns: A string representing a node unrecognized by the other \
73  visitor methods of this Writer.
74  :rtype: str
75 
76  '''
77  # If its not part of an expression it needs indentation and \n
78  if not isinstance(node, DataNode):
79  return f"{self._nindent}< {str(node)} >\n"
80  return f"< {str(node)} >"