Reference Guide  2.5.0
member.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2020-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: A. R. Porter, STFC Daresbury Lab
35 # Modified by: R. W. Ford, A. B. G. Chalk and N. Nobre, STFC Daresbury Lab
36 # Modified by: J. Henrichs, Bureau of Meteorology
37 # -----------------------------------------------------------------------------
38 
39 ''' This module contains the implementation of the Member node.'''
40 
41 
42 from psyclone.core import Signature
43 from psyclone.psyir.nodes.node import Node
44 
45 
46 class Member(Node):
47  '''
48  Node representing a membership expression of a structure.
49  As such it is a leaf in the PSyIR tree.
50 
51  :param str member_name: the name of the member of the structure that is \
52  being referenced.
53  :param parent: the parent of this node in the PSyIR tree.
54  :type parent: :py:class:`psyclone.psyir.nodes.StructureReference` or \
55  :py:class:`psyclone.psyir.nodes.Member`
56 
57  :raises TypeError: if the supplied parent is of the wrong type.
58 
59  '''
60  # Textual description of the node.
61  _children_valid_format = "<LeafNode>"
62  _text_name = "Member"
63  _colour = "yellow"
64 
65  def __init__(self, member_name, parent=None):
66  # Avoid circular dependency
67  # pylint: disable=import-outside-toplevel
68  from psyclone.psyir.nodes.structure_reference import StructureReference
69  from psyclone.psyir.nodes.structure_member import StructureMember
70 
71  # Since this node represents a membership expression of a structure,
72  # its parent (if supplied) *must* subclass StructureReference or
73  # StructureMember.
74  if (parent and
75  not isinstance(parent, (StructureReference, StructureMember))):
76  raise TypeError(
77  f"The parent of a {type(self).__name__} must be either a "
78  f"(ArrayOf)Structure(s)Reference or (ArrayOf)Structure(s)"
79  f"Member but found '{type(parent).__name__}'.")
80 
81  super().__init__(parent=parent)
82  # Store the name of the component that this member represents
83  self._component_name_component_name = member_name
84 
85  def __eq__(self, other):
86  '''
87  Members are assumed to be equivalent if they have the same
88  component name associated with them, and are the same type.
89 
90  :param object other: the object to check equality to.
91 
92  :returns: whether other is equal to self.
93  :rtype: bool
94  '''
95  is_eq = super().__eq__(other)
96  is_eq = is_eq and self.namenamename == other.name
97  return is_eq
98 
99  @property
100  def name(self):
101  '''
102  :returns: the name of this member.
103  :rtype: str
104  '''
105  return self._component_name_component_name
106 
107  def node_str(self, colour=True):
108  ''' Create a text description of this node in the schedule, optionally
109  including control codes for colour.
110 
111  :param bool colour: whether or not to include colour control codes.
112 
113  :return: text description of this node.
114  :rtype: str
115  '''
116  return self.coloured_namecoloured_name(colour) + "[name:'" + self.namenamename + "']"
117 
118  def __str__(self):
119  return self.node_strnode_strnode_str(False)
120 
121  @property
122  def is_array(self):
123  ''':returns: whether this member is an array.
124  :rtype: bool
125 
126  '''
127  return False
128 
130  ''':returns: the Signature of this member access, and a list \
131  of list of the indices used for each component, which is empty \
132  in this case since it is not an array access.
133  :rtype: tuple(:py:class:`psyclone.core.Signature`, list of \
134  lists of indices)
135  '''
136  return (Signature(self.namenamename), [[]])
137 
138 
139 # For Sphinx AutoAPI documentation generation
140 __all__ = ['Member']
def node_str(self, colour=True)
Definition: member.py:107
def node_str(self, colour=True)
Definition: node.py:483
def coloured_name(self, colour=True)
Definition: node.py:453