Reference Guide  2.5.0
structure_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 and N. Nobre, STFC Daresbury Lab
35 # Modified by J. Henrichs, Bureau of Meteorology
36 # -----------------------------------------------------------------------------
37 
38 ''' This module contains the implementation of the StructureMember node.'''
39 
40 from __future__ import absolute_import
41 from psyclone.core import Signature
42 from psyclone.psyir.nodes.member import Member
43 from psyclone.errors import InternalError
44 
45 
47  '''
48  Node representing a membership expression of the parent's Reference that
49  resolves into another structure.
50 
51  '''
52  # Textual description of the node. Since it represents a reference to a
53  # structure it may have a single child which is a reference to one of its
54  # members.
55  _children_valid_format = "[Member]"
56  _text_name = "StructureMember"
57 
58  @staticmethod
59  def create(member_name, inner_member):
60  '''
61  Given the name of a structure member of a structure and a Member
62  node describing the access to a component of it, construct a
63  StructureMember.
64 
65  e.g. if we had the following Fortran access: grid%subdomain%xstart
66  then 'subdomain' must itself be of structure type and we are accessing
67  the 'xstart' component of it. We would therefore create the
68  `StructureMember` for this by calling:
69 
70  >>> smem = StructureMember.create("subdomain", Member("xstart"))
71 
72  :param str member_name: name of the structure member.
73  :param inner_member: a Member describing the access to a component \
74  of the named structure member.
75  :type inner_member: sub-class of \
76  :py:class:`psyclone.psyir.nodes.Member`
77 
78  '''
79  smem = StructureMember(member_name)
80  smem.addchild(inner_member)
81  return smem
82 
83  def __str__(self):
84  result = super(StructureMember, self).__str__()
85  if self._children_children:
86  result += "\n" + str(self._children_children[0])
87  return result
88 
89  @staticmethod
90  def _validate_child(position, child):
91  '''
92  :param int position: the position to be validated.
93  :param child: a child to be validated.
94  :type child: :py:class:`psyclone.psyir.nodes.Node`
95 
96  :return: whether the given child and position are valid for this node.
97  :rtype: bool
98 
99  '''
100  if position == 0:
101  # The first child must be a Member
102  return isinstance(child, Member)
103  # Only one child is permitted
104  return False
105 
106  @property
107  def member(self):
108  '''
109  :returns: the member of the structure that is being accessed.
110  :rtype: (sub-class of) :py:class:`psyclone.psyir.nodes.Member`
111 
112  :raises InternalError: if the first child of this node is not an \
113  instance of Member.
114  '''
115  if not isinstance(self.childrenchildrenchildren[0], Member):
116  raise InternalError(
117  f"{type(self).__name__} malformed or incomplete. The first "
118  f"child must be an instance of Member, but found "
119  f"'{type(self.children[0]).__name__}'")
120  return self.childrenchildrenchildren[0]
121 
123  ''':returns: the Signature of this structure member, and \
124  a list of the indices used for each component (empty list \
125  for this component, since the access is not an array - but \
126  other components might have indices).
127  :rtype: tuple(:py:class:`psyclone.core.Signature`, list of \
128  list of indices)
129  '''
130  sub_sig, indices = self.childrenchildrenchildren[0].get_signature_and_indices()
131  return (Signature(self.namenamename, sub_sig), [[]]+indices)
132 
133 
134 # For Sphinx AutoAPI documentation generation
135 __all__ = ['StructureMember']
def children(self, my_children)
Definition: node.py:935