Reference Guide  2.5.0
psyclone.f2pygen.BaseGen Class Reference
Inheritance diagram for psyclone.f2pygen.BaseGen:

Public Member Functions

def __init__ (self, parent, root)
 
def parent (self)
 
def children (self)
 
def root (self)
 
def add (self, new_object, position=None)
 
def previous_loop (self)
 
def last_declaration (self)
 
def start_parent_loop (self, debug=False)
 

Detailed Description

 The base class for all classes that are responsible for generating
distinct code elements (modules, subroutines, do loops etc.) 

Definition at line 173 of file f2pygen.py.

Member Function Documentation

◆ add()

def psyclone.f2pygen.BaseGen.add (   self,
  new_object,
  position = None 
)
Adds a new object to the tree. The actual position is determined by
the position argument. Note, there are two trees, the first is
the f2pygen object tree, the other is the f2py generated code
tree. These are similar but different. At the moment we
specify where to add things in terms of the f2pygen tree
(which is a higher level api) but we also insert into the f2py
tree at exactly the same location which needs to be sorted out
at some point.

Reimplemented in psyclone.f2pygen.IfThenGen.

Definition at line 196 of file f2pygen.py.

196  def add(self, new_object, position=None):
197  '''Adds a new object to the tree. The actual position is determined by
198  the position argument. Note, there are two trees, the first is
199  the f2pygen object tree, the other is the f2py generated code
200  tree. These are similar but different. At the moment we
201  specify where to add things in terms of the f2pygen tree
202  (which is a higher level api) but we also insert into the f2py
203  tree at exactly the same location which needs to be sorted out
204  at some point.
205 
206  '''
207 
208  # By default the position is 'append'. We set it up this way for
209  # safety because in python, default arguments are instantiated
210  # as objects at the time of definition. If this object is
211  # subsequently modified then the value of the default argument
212  # is modified for subsequent calls of this routine.
213  if position is None:
214  position = ["append"]
215 
216  if position[0] == "auto":
217  raise Exception("Error: BaseGen:add: auto option must be "
218  "implemented by the sub class!")
219  options = ["append", "first", "after", "before", "insert",
220  "before_index", "after_index"]
221  if position[0] not in options:
222  raise Exception(f"Error: BaseGen:add: supported positions are "
223  f"{options} but found {position[0]}")
224  if position[0] == "append":
225  self.root.content.append(new_object.root)
226  elif position[0] == "first":
227  self.root.content.insert(0, new_object.root)
228  elif position[0] == "insert":
229  index = position[1]
230  self.root.content.insert(index, new_object.root)
231  elif position[0] == "after":
232  idx = index_of_object(self.root.content, position[1])
233  self.root.content.insert(idx+1, new_object.root)
234  elif position[0] == "after_index":
235  self.root.content.insert(position[1]+1, new_object.root)
236  elif position[0] == "before_index":
237  self.root.content.insert(position[1], new_object.root)
238  elif position[0] == "before":
239  try:
240  idx = index_of_object(self.root.content, position[1])
241  except Exception as err:
242  print(str(err))
243  raise RuntimeError(
244  "Failed to find supplied object in existing content - "
245  "is it a child of the parent?")
246  self.root.content.insert(idx, new_object.root)
247  else:
248  raise Exception("Error: BaseGen:add: internal error, should "
249  "not get to here")
250  self.children.append(new_object)
251 

References psyclone.f2pygen.BaseGen.children(), psyclone.psyGen.InlinedKern.children, psyclone.psyir.nodes.node.Node.children(), psyclone.f2pygen.BaseGen.root(), psyclone.f2pygen.BaseDeclGen.root(), and psyclone.psyir.nodes.node.Node.root().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ children()

def psyclone.f2pygen.BaseGen.children (   self)
 Returns the list of children of this object 

Definition at line 187 of file f2pygen.py.

187  def children(self):
188  ''' Returns the list of children of this object '''
189  return self._children
190 

References psyclone.f2pygen.BaseGen._children, psyclone.psyir.nodes.node.Node._children, and psyclone.psyir.nodes.omp_directives.OMPParallelDirective._children.

Here is the caller graph for this function:

◆ last_declaration()

def psyclone.f2pygen.BaseGen.last_declaration (   self)
Returns the *last* occurrence of a Declaration in the list of
    siblings of this node

Definition at line 261 of file f2pygen.py.

261  def last_declaration(self):
262  '''Returns the *last* occurrence of a Declaration in the list of
263  siblings of this node
264 
265  '''
266  from fparser.one.typedecl_statements import TypeDeclarationStatement
267  for sibling in reversed(self.root.content):
268  if isinstance(sibling, TypeDeclarationStatement):
269  return sibling
270 
271  raise RuntimeError("Error, no variable declarations found")
272 

References psyclone.f2pygen.BaseGen.root(), psyclone.f2pygen.BaseDeclGen.root(), and psyclone.psyir.nodes.node.Node.root().

Here is the call graph for this function:

◆ parent()

def psyclone.f2pygen.BaseGen.parent (   self)
 Returns the parent of this object 

Definition at line 182 of file f2pygen.py.

182  def parent(self):
183  ''' Returns the parent of this object '''
184  return self._parent
185 

References psyclone.domain.gocean.kernel.psyir.GOceanKernelMetadata.GridArg._parent, psyclone.domain.gocean.kernel.psyir.GOceanKernelMetadata.FieldArg._parent, psyclone.domain.gocean.kernel.psyir.GOceanKernelMetadata.ScalarArg._parent, psyclone.domain.lfric.lfric_kern.LFRicKern._parent, psyclone.f2pygen.BaseGen._parent, and psyclone.psyir.nodes.node.Node._parent.

Here is the caller graph for this function:

◆ previous_loop()

def psyclone.f2pygen.BaseGen.previous_loop (   self)
 Returns the *last* occurrence of a loop in the list of
siblings of this node 

Definition at line 252 of file f2pygen.py.

252  def previous_loop(self):
253  ''' Returns the *last* occurrence of a loop in the list of
254  siblings of this node '''
255  from fparser.one.block_statements import Do
256  for sibling in reversed(self.root.content):
257  if isinstance(sibling, Do):
258  return sibling
259  raise RuntimeError("Error, no loop found - there is no previous loop")
260 

References psyclone.f2pygen.BaseGen.root(), psyclone.f2pygen.BaseDeclGen.root(), and psyclone.psyir.nodes.node.Node.root().

Here is the call graph for this function:

◆ root()

def psyclone.f2pygen.BaseGen.root (   self)
 Returns the root of the tree containing this object 

Reimplemented in psyclone.f2pygen.BaseDeclGen.

Definition at line 192 of file f2pygen.py.

192  def root(self):
193  ''' Returns the root of the tree containing this object '''
194  return self._root
195 

References psyclone.f2pygen.BaseGen._root.

Here is the caller graph for this function:

◆ start_parent_loop()

def psyclone.f2pygen.BaseGen.start_parent_loop (   self,
  debug = False 
)
 Searches for the outer-most loop containing this object. Returns
the index of that line in the content of the parent. 

Definition at line 273 of file f2pygen.py.

273  def start_parent_loop(self, debug=False):
274  ''' Searches for the outer-most loop containing this object. Returns
275  the index of that line in the content of the parent. '''
276  from fparser.one.block_statements import Do
277  if debug:
278  print("Entered before_parent_loop")
279  print(f"The type of the current node is {type(self.root)}")
280  print(("If the current node is a Do loop then move up to the "
281  "top of the do loop nest"))
282 
283  # First off, check that we do actually have an enclosing Do loop
284  current = self.root
285  while not isinstance(current, Do) and getattr(current, 'parent', None):
286  current = current.parent
287  if not isinstance(current, Do):
288  raise RuntimeError("This node has no enclosing Do loop")
289 
290  current = self.root
291  local_current = self
292  while isinstance(current.parent, Do):
293  if debug:
294  print("Parent is a do loop so moving to the parent")
295  current = current.parent
296  local_current = local_current.parent
297  if debug:
298  print("The type of the current node is now " + str(type(current)))
299  print("The type of parent is " + str(type(current.parent)))
300  print("Finding the loops position in its parent ...")
301  index = current.parent.content.index(current)
302  if debug:
303  print("The loop's index is ", index)
304  parent = current.parent
305  local_current = local_current.parent
306  if debug:
307  print("The type of the object at the index is " +
308  str(type(parent.content[index])))
309  print("If preceding node is a directive then move back one")
310  if index == 0:
311  if debug:
312  print("current index is 0 so finish")
313  elif isinstance(parent.content[index-1], Directive):
314  if debug:
315  print(
316  f"preceding node is a directive so find out what type ..."
317  f"\n type is {parent.content[index-1].position}"
318  f"\n diretive is {parent.content[index-1]}")
319  if parent.content[index-1].position == "begin":
320  if debug:
321  print("type of directive is begin so move back one")
322  index -= 1
323  else:
324  if debug:
325  print("directive type is not begin so finish")
326  else:
327  if debug:
328  print("preceding node is not a directive so finish")
329  if debug:
330  print("type of final location ", type(parent.content[index]))
331  print("code for final location ", str(parent.content[index]))
332  return local_current, parent.content[index]
333 
334 

References psyclone.f2pygen.BaseGen.root(), psyclone.f2pygen.BaseDeclGen.root(), and psyclone.psyir.nodes.node.Node.root().

Here is the call graph for this function:

The documentation for this class was generated from the following file: