Reference Guide  2.5.0
scalar_arg_metadata.py
1 # -----------------------------------------------------------------------------
2 # BSD 3-Clause License
3 #
4 # Copyright (c) 2022-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 R. W. Ford, STFC Daresbury Lab
35 
36 '''Module containing the ScalarArgMetadata class which captures the metadata
37 associated with a scalar argument. Supports the creation, modification
38 and Fortran output of a Scalar argument.
39 
40 '''
41 from psyclone.domain.lfric import LFRicConstants
43  CommonMetaArgMetadata
44 
45 
47  '''Class to capture LFRic kernel metadata information for a scalar
48  argument.
49 
50  '''
51  # The name used to specify a scalar argument in LFRic metadata.
52  form = "gh_scalar"
53  # The relative positions of LFRic metadata. Metadata for a scalar
54  # argument is provided in the following format 'arg_type(form,
55  # datatype, access)'. Therefore, for example, the index of the
56  # form argument (form_arg_index) is 0.
57  form_arg_index = 0
58  datatype_arg_index = 1
59  access_arg_index = 2
60  # The name to use for any exceptions.
61  check_name = "scalar"
62  # The number of arguments in the language-level metadata.
63  nargs = 3
64 
65  @classmethod
66  def _get_metadata(cls, fparser2_tree):
67  '''Extract the required metadata from the fparser2 tree and return it
68  as strings.
69 
70  :param fparser2_tree: fparser2 tree containing the metadata \
71  for this argument.
72  :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref` | \
73  :py:class:`fparser.two.Fortran2003.Structure_Constructor`
74 
75  :returns: a tuple containing the datatype and access metadata.
76  :rtype: Tuple[str, str]
77 
78  '''
79  return cls._get_datatype_access_metadata_get_datatype_access_metadata(fparser2_tree)
80 
81  @classmethod
82  def _get_datatype_access_metadata(cls, fparser2_tree):
83  '''Extract the datatype and access metadata from the fparser2 tree and
84  return them as strings. Also check that the metadata is in the
85  expected form (but do not check the metadata values as that is
86  done separately).
87 
88  :param fparser2_tree: fparser2 tree containing the metadata \
89  for this argument.
90  :type fparser2_tree: :py:class:`fparser.two.Fortran2003.Part_Ref` | \
91  :py:class:`fparser.two.Fortran2003.Structure_Constructor`
92 
93  :returns: a tuple containing the datatype and access metadata.
94  :rtype: Tuple[str, str]
95 
96  '''
97  cls.check_fparser2_argcheck_fparser2_arg(fparser2_tree, "arg_type")
98  cls.check_nargscheck_nargscheck_nargs(fparser2_tree)
99  cls.check_first_argcheck_first_arg(fparser2_tree)
100  datatype = cls.get_argget_arg(fparser2_tree, cls.datatype_arg_indexdatatype_arg_indexdatatype_arg_index)
101  access = cls.get_argget_arg(fparser2_tree, cls.access_arg_indexaccess_arg_indexaccess_arg_index)
102  return (datatype, access)
103 
104  def fortran_string(self):
105  '''
106  :returns: the metadata represented by this class as Fortran.
107  :rtype: str
108  '''
109  return f"arg_type({self.form}, {self.datatype}, {self.access})"
110 
111  @staticmethod
112  def check_datatype(value):
113  '''
114  :param str value: the datatype to check for validity.
115  '''
116  const = LFRicConstants()
117  ScalarArgMetadata.validate_scalar_value(
118  value, const.VALID_SCALAR_DATA_TYPES, "datatype descriptor")
119 
120  @staticmethod
121  def check_access(value):
122  '''
123  :param str value: the access descriptor to validate.
124  '''
125  const = LFRicConstants()
126  ScalarArgMetadata.validate_scalar_value(
127  value, const.VALID_SCALAR_ACCESS_TYPES, "access descriptor")
128 
129 
130 __all__ = ["ScalarArgMetadata"]