PyForTool
Python-fortran-tool
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
pyfortool.scope.PYFTscope Class Reference
Inheritance diagram for pyfortool.scope.PYFTscope:
Inheritance graph
[legend]
Collaboration diagram for pyfortool.scope.PYFTscope:
Collaboration graph
[legend]

Public Member Functions

 __init__ (self, xml, scopePath='/', parentScope=None, enableCache=False, tree=None, excludeContains=False)
 
 __copy__ (self)
 
 __deepcopy__ (self, memo)
 
 __getattr__ (self, attr)
 
 path (self)
 
 mainScope (self)
 
 parentScope (self)
 
 getParent (self, item, level=1)
 
 getSiblings (self, item, before=True, after=True)
 
 showScopesList (self, includeItself=False)
 
 getScopes (self, level=-1, excludeContains=True, excludeKinds=None, includeItself=True)
 
 getScopeNode (self, scopePath, excludeContains=True, includeItself=True)
 
 isScopeNode (self, node)
 
 getParentScopeNode (self, item, mustRaise=True)
 
 getScopePath (self, item, includeItself=True)
 
 getFileName (self)
 
 empty (self, addStmt=None, simplify=False)
 
- Public Member Functions inherited from pyfortool.scope.ElementView
 tag (self)
 
 tail (self)
 
 text (self)
 
 findtext (self, *args, **kwargs)
 
 iterfind (self, *args, **kwargs)
 
 itertext (self, *args, **kwargs)
 
 __getitem__ (self, *args, **kwargs)
 
 __len__ (self, *args, **kwargs)
 
 __iter__ (self)
 
 find (self, *args, **kwargs)
 
 findall (self, *args, **kwargs)
 
 iter (self, *args, **kwargs)
 
 items (self, *args, **kwargs)
 
 clear (self)
 
 append (self, *args, **kwargs)
 
 extend (self, *args, **kwargs)
 
 __setitem__ (self, index, item)
 
 __delitem__ (self, index)
 
 insert (self, index, item)
 
 remove (self, node)
 
- Public Member Functions inherited from pyfortool.variables.Variables
 varList (self)
 
 attachArraySpecToEntity (self)
 
 checkImplicitNone (self, mustRaise=False)
 
 checkIntent (self, mustRaise=False)
 
 checkONLY (self, mustRaise=False)
 
 removeVar (self, varList, simplify=False)
 
 addVar (self, varList)
 
 addModuleVar (self, moduleVarList)
 
 showUnusedVar (self)
 
 checkUnusedLocalVar (self, mustRaise=False, excludeList=None)
 
 removeUnusedLocalVar (self, excludeList=None, simplify=False)
 
 addExplicitArrayBounds (self, node=None)
 
 addArrayParentheses (self)
 
 addArrayParenthesesInNode (self, node)
 
 removeArrayParenthesesInNode (self, node)
 
 modifyAutomaticArrays (self, declTemplate=None, startTemplate=None, endTemplate=None)
 
 findIndexArrayBounds (self, arr, index, loopVar)
 
 arrayR2parensR (self, namedE, table)
 
 findArrayBounds (self, arr, loopVar, extraVarList=None)
 
 renameVar (self, oldName, newName)
 
 removeVarIfUnused (self, varList, excludeDummy=False, excludeModule=False, simplify=False)
 
 isVarUsed (self, varList, exactScope=False, dummyAreAlwaysUsed=False)
 
 addONLY (self, parserOptions=None, wrapH=False)
 
 addArgInTree (self, varName, declStmt, pos, stopScopes, moduleVarList=None, otherNames=None, parserOptions=None, wrapH=False)
 
- Public Member Functions inherited from pyfortool.cosmetics.Cosmetics
 upperCase (self)
 
 lowerCase (self)
 
 indent (self, nodeToUpdate=None, indentProgramunit=0, indentBranch=2, exclDirectives=None)
 
 removeEmptyLines (self)
 
 removeComments (self, exclDirectives=None, pattern=None)
 
 updateContinuation (self, nodeToUpdate=None, align=True, removeALL=False, addBegin=True, removeBegin=False)
 
 updateSpaces (self, beforeOp=1, afterOp=1, inOperator=True, beforeComma=0, afterComma=1, beforeParenthesis=0, afterParenthesis=0, beforeAffectation=1, afterAffectation=1, inAffectation=True, beforeRangeDelim=0, afterRangeDelim=0, beforeUseDelim=0, afterUseDelim=1, beforeDeclDelim=1, afterDeclDelim=1, inDeclDelim=True, afterTypeDecl=1, beforeEqDo=0, afterEqDo=0, beforeEqCall=0, afterEqCall=0, beforeEqInit=0, afterEqInit=0, beforeEndcnt=1, afterBegincnt=1, afterIfwherecase=1, beforeThen=1, beforeIfaction=1, afterProgunit=1, endOfLine=True, afterName=0, inName=True, beforeCmdsep=0, afterCmdsep=1, adjacentKeywords=__NO_VALUE__, afterKeywords=__NO_VALUE__)
 
 changeIfStatementsInIfConstructs (self, singleItem=None)
 
 removeEmptyCONTAINS (self)
 
- Public Member Functions inherited from pyfortool.applications.Applications
 splitModuleRoutineFile (self)
 
 buildModi (self)
 
 deleteNonColumnCallsPHYEX (self, simplify=False)
 
 removeExtraDOinMnhDoConcurrent (self)
 
 convertuseModuleToIncludes (self)
 
 convertTypesInCompute (self)
 
 deleteDrHook (self, simplify=False)
 
 addDrHook (self)
 
 deleteBudgetDDH (self, simplify=False)
 
 deleteRoutineCallsMesoNHGPU (self, simplify=True)
 
 addSubmodulePHYEX (self)
 
 addMPPDB_CHECKS (self, printsMode=False)
 
 addStack (self, model, stopScopes, parserOptions=None, wrapH=False)
 
 inlineContainedSubroutinesPHYEX (self, simplify=False)
 
 removeIJDim (self, stopScopes, parserOptions=None, wrapH=False, simplify=False)
 
 removePHYEXUnusedLocalVar (self, excludeList=None, simplify=False)
 
 checkPHYEXUnusedLocalVar (self, mustRaise=False, excludeList=None)
 
 expandAllArraysPHYEX (self, concurrent=False)
 
 mathFunctoBRFunc (self)
 
 shumanFUNCtoCALL (self)
 
 buildACCTypeHelpers (self)
 
- Public Member Functions inherited from pyfortool.statements.Statements
 isNodeInProcedure (self, node, procList)
 
 isNodeInCall (self, node)
 
 removeCall (self, callName, simplify=False)
 
 removePrints (self, simplify=False)
 
 removeArraySyntax (self, concurrent=False, useMnhExpand=True, everywhere=True, loopVar=None, reuseLoop=True, funcList=None, updateMemSet=False, updateCopy=False, addAccIndependentCollapse=True)
 
 inlineContainedSubroutines (self, simplify=False, loopVar=None)
 
 inline (self, subContained, callStmt, mainScope, simplify=False, loopVar=None)
 
 setFalseIfStmt (self, flags, simplify=False)
 
 evalFalseIfStmt (self, nodes, simplify=False)
 
 checkOpInCall (self, mustRaise=False)
 
 checkEmptyParensInCall (self, mustRaise=False)
 
 insertStatement (self, stmt, first)
 
 removeStmtNode (self, nodes, simplifyVar, simplifyStruct)
 
 removeConstructNode (self, node, simplifyVar, simplifyStruct)
 
 removeFromList (self, item, itemPar)
 
- Public Member Functions inherited from pyfortool.cpp.Cpp
 applyCPPifdef (self, keys)
 
- Public Member Functions inherited from pyfortool.openacc.Openacc
 removeACC (self)
 
 removebyPassDOCONCURRENT (self)
 
 craybyPassDOCONCURRENT (self)
 
 allocatetoHIP (self)
 
 addACCData (self)
 
 addACCRoutineSeq (self, stopScopes)
 

Static Public Member Functions

 normalizeScope (scopePath)
 
- Static Public Member Functions inherited from pyfortool.variables.Variables
 varSpec2stmt (varSpec, implicitDeclaration=False)
 
- Static Public Member Functions inherited from pyfortool.statements.Statements
 createDoConstruct (loopVariables, indent=0, concurrent=False)
 
 insertInList (pos, item, parent)
 

Public Attributes

 tree
 
 path
 
- Public Attributes inherited from pyfortool.scope.ElementView
 text
 
 tail
 
- Public Attributes inherited from pyfortool.variables.Variables

Static Public Attributes

dict SCOPE_STMT
 
dict SCOPE_CONSTRUCT
 

Protected Member Functions

 _getNodePath (self, node)
 
- Protected Member Functions inherited from pyfortool.scope.ElementView
 _virtual (self)
 
 _getIndex (self, index)
 
- Protected Member Functions inherited from pyfortool.variables.Variables
 _normalizeScopeVar (self, scopeVarList)
 
 _normalizeUniqVar (self, scopeVarList)
 
- Protected Member Functions inherited from pyfortool.applications.Applications
 _mnh_expand_var (self)
 

Static Protected Member Functions

 _getNodeName (node)
 

Protected Attributes

 _mainScope
 
 _path
 
 _parentScope
 
 _cacheParent
 
- Protected Attributes inherited from pyfortool.scope.ElementView
 _excludeContains
 
 _xml
 
 _virtual
 
- Protected Attributes inherited from pyfortool.variables.Variables
 _varList
 
 _excludeContains
 

Detailed Description

Wrap an XML node representing a FORTRAN scope.

PYFTscope provides methods to navigate, query, and modify FORTRAN
source code at the scope level (modules, subroutines, functions, types).

Scope Path Format
-----------------
Scope paths are '/' separated strings identifying the location in the
source tree. Examples:
- 'module:MODULE' - a module
- 'module:MOD/sub:SUB' - subroutine SUB in module MODULE
- 'module:MOD/type:TYPE' - type TYPE in module MODULE
- 'module:MOD/sub:SUB/func:FUNC' - function FUNC in subroutine SUB

Examples
--------
>>> pft = PYFT('myfile.F90')
>>> scopes = pft.getScopes()  # Get all scopes
>>> sub = pft.getScopeNode('module:MOD/sub:SUB')  # Get specific scope
>>> for scope in pft.getScopes(excludeKinds=['type']):
...     print(scope.path)

Definition at line 232 of file scope.py.

Constructor & Destructor Documentation

◆ __init__()

pyfortool.scope.PYFTscope.__init__ (   self,
  xml,
  scopePath = '/',
  parentScope = None,
  enableCache = False,
  tree = None,
  excludeContains = False 
)
Initialize a PYFTscope instance.

Parameters
----------
xml : xml element
    XML element representing the scope.
scopePath : str, optional
    Path string identifying this scope (e.g., 'module:MOD/sub:SUB').
parentScope : PYFTscope, optional
    Parent scope instance.
enableCache : bool, optional
    If True, cache parent nodes for faster traversal.
tree : Tree, optional
    Tree instance for cross-file analysis.
excludeContains : bool, optional
    If True, ignore CONTAINS sections in scope traversal.

Reimplemented from pyfortool.scope.ElementView.

Reimplemented in pyfortool.pyfortool.PYFT.

Definition at line 271 of file scope.py.

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

Member Function Documentation

◆ __copy__()

pyfortool.scope.PYFTscope.__copy__ (   self)

Definition at line 304 of file scope.py.

◆ __deepcopy__()

pyfortool.scope.PYFTscope.__deepcopy__ (   self,
  memo 
)

Definition at line 310 of file scope.py.

◆ __getattr__()

pyfortool.scope.PYFTscope.__getattr__ (   self,
  attr 
)
Get some attributes defined in the PYFT class

Definition at line 318 of file scope.py.

◆ _getNodeName()

pyfortool.scope.PYFTscope._getNodeName (   node)
staticprotected
Internal methode to compute the name of a scope
:param node: program-unit node
:return: name

Definition at line 434 of file scope.py.

Here is the caller graph for this function:

◆ _getNodePath()

pyfortool.scope.PYFTscope._getNodePath (   self,
  node 
)
protected
Internal methode to compute a path part from a node
:param node: program-unit node
:return: path part (e.g. module:MODU)

Definition at line 457 of file scope.py.

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

◆ empty()

pyfortool.scope.PYFTscope.empty (   self,
  addStmt = None,
  simplify = False 
)
Remove all statements from scopes.

Removes all executable statements from scopes while preserving:
- Dummy argument declarations
- USE statements
- Scope declarations and endings

Parameters
----------
addStmt : str or list, optional
    Statement(s) to insert into the body of emptied scopes.
simplify : bool, optional
    If True, also remove unused local variables.

Examples
--------
>>> pft = PYFT('myfile.F90')
>>> pft.empty()  # Remove all statements

Empty and add a comment:
>>> pft.empty(addStmt='! TODO: Implement')

Definition at line 725 of file scope.py.

Here is the call graph for this function:

◆ getFileName()

pyfortool.scope.PYFTscope.getFileName (   self)
Get the source filename.

Returns
-------
str
    Normalized path to the source file.

Examples
--------
>>> pft = PYFT('/path/to/file.F90')
>>> pft.getFileName()
'/path/to/file.F90'

Definition at line 706 of file scope.py.

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

◆ getParent()

pyfortool.scope.PYFTscope.getParent (   self,
  item,
  level = 1 
)
Get the parent element of an XML node.

Parameters
----------
item : xml element
    Element whose parent to find.
level : int, optional
    Number of levels to traverse (1 = direct parent, 2 = grandparent, etc.).

Returns
-------
xml element or None
    Parent element at the specified level.

Definition at line 363 of file scope.py.

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

◆ getParentScopeNode()

pyfortool.scope.PYFTscope.getParentScopeNode (   self,
  item,
  mustRaise = True 
)
Get the scope containing an element.

Parameters
----------
item : xml element
    Element whose containing scope to find.
mustRaise : bool, optional
    If True (default), raise PYFTError if scope not found.

Returns
-------
xml element or None
    The scope node containing the item.

Examples
--------
>>> call = pft.find('.//{*}call-stmt')
>>> scope = pft.getParentScopeNode(call)

Definition at line 645 of file scope.py.

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

◆ getScopeNode()

pyfortool.scope.PYFTscope.getScopeNode (   self,
  scopePath,
  excludeContains = True,
  includeItself = True 
)
Get a specific scope by path.

Parameters
----------
scopePath : str
    Scope path to search for (e.g., 'module:MOD/sub:SUB').
excludeContains : bool, optional
    See getScopes. Default is True.
includeItself : bool, optional
    See getScopes. Default is True.

Returns
-------
PYFTscope
    Scope instance matching the path.

Raises
------
PYFTError
    If scope not found or found multiple times.

Examples
--------
>>> pft = PYFT('myfile.F90')
>>> sub = pft.getScopeNode('module:MOD/sub:SUB')
>>> func = pft.getScopeNode('/module:MOD/func:FUNC')

Definition at line 582 of file scope.py.

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

◆ getScopePath()

pyfortool.scope.PYFTscope.getScopePath (   self,
  item,
  includeItself = True 
)
Get the scope path for an element.

Parameters
----------
item : xml element
    Element whose scope path to determine.
includeItself : bool, optional
    If True (default) and item is a scope node, include it.

Returns
-------
str
    Full scope path of the element's containing scope.

Examples
--------
>>> node = pft.find('.//{*}a-stmt')
>>> pft.getScopePath(node)
'/module:MOD/sub:SUB'

Definition at line 674 of file scope.py.

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

◆ getScopes()

pyfortool.scope.PYFTscope.getScopes (   self,
  level = -1,
  excludeContains = True,
  excludeKinds = None,
  includeItself = True 
)
Get child scopes from the current scope.

Parameters
----------
level : int, optional
    Depth of scope traversal:
    - -1 (default): All child scopes recursively.
    - 1: Direct children only.
    - 2: Children and grandchildren, etc.
excludeContains : bool, optional
    If True (default), exclude scopes from CONTAINS sections
    (nested subroutines/functions).
excludeKinds : list of str, optional
    Scope kinds to exclude. Options: 'module', 'sub', 'func',
    'type', 'prog', 'interface', 'submodule'.
includeItself : bool, optional
    If True (default), include the current scope in results.

Returns
-------
list of PYFTscope
    List of scope instances matching the criteria.

Examples
--------
>>> pft = PYFT('myfile.F90')
>>> all_scopes = pft.getScopes()  # All scopes
>>> subs = pft.getScopes(excludeKinds=['module', 'func', 'type'])
>>> direct = pft.getScopes(level=1)

Definition at line 519 of file scope.py.

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

◆ getSiblings()

pyfortool.scope.PYFTscope.getSiblings (   self,
  item,
  before = True,
  after = True 
)
Get sibling elements.

Parameters
----------
item : xml element
    Element whose siblings to find.
before : bool, optional
    If True, include siblings before the item. Default is True.
after : bool, optional
    If True, include siblings after the item. Default is True.

Returns
-------
list
    List of sibling elements.

Examples
--------
>>> siblings = scope.getSiblings(node)  # All siblings
>>> before = scope.getSiblings(node, after=False)  # Only before

Definition at line 401 of file scope.py.

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

◆ isScopeNode()

pyfortool.scope.PYFTscope.isScopeNode (   self,
  node 
)
Check if a node is a scope-level construct.

Parameters
----------
node : xml element
    Node to check.

Returns
-------
bool
    True if node is a scope construct (program-unit, interface-construct,
    or T-construct).

Examples
--------
>>> node = pft.find('.//{*}subroutine-stmt/..')
>>> pft.isScopeNode(node)
True

Definition at line 621 of file scope.py.

Here is the caller graph for this function:

◆ mainScope()

pyfortool.scope.PYFTscope.mainScope (   self)
Get the main (root) scope.

Returns
-------
PYFTscope
    The top-level scope in the file.

Definition at line 339 of file scope.py.

Here is the caller graph for this function:

◆ normalizeScope()

pyfortool.scope.PYFTscope.normalizeScope (   scopePath)
static
Normalize a scope path to standard format.

Converts scope path to lowercase prefix and uppercase names.

Parameters
----------
scopePath : str
    Scope path to normalize.

Returns
-------
str
    Normalized scope path.

Examples
--------
>>> PYFTscope.normalizeScope('module:Test/sub:Sub')
'module:TEST/sub:SUB'

Definition at line 469 of file scope.py.

Here is the caller graph for this function:

◆ parentScope()

pyfortool.scope.PYFTscope.parentScope (   self)
Get the parent scope.

Returns
-------
PYFTscope or None
    Parent scope, or None if this is the root scope.

Definition at line 351 of file scope.py.

◆ path()

pyfortool.scope.PYFTscope.path (   self)
Get the scope path.

Returns
-------
str
    Scope path string (e.g., 'module:MOD/sub:SUB').

Reimplemented from pyfortool.variables.Variables.

Definition at line 327 of file scope.py.

Here is the caller graph for this function:

◆ showScopesList()

pyfortool.scope.PYFTscope.showScopesList (   self,
  includeItself = False 
)
Display all scopes found in the source code.

Parameters
----------
includeItself : bool, optional
    If True, include the current scope in the output.
    Default is False.

Examples
--------
>>> pft = PYFT('myfile.F90')
>>> pft.showScopesList()
These scopes have been found in the source code:
  - /module:MOD
  - /module:MOD/sub:SUB
  - /module:MOD/func:FUNC

Definition at line 495 of file scope.py.

Here is the call graph for this function:

Member Data Documentation

◆ _cacheParent

pyfortool.scope.PYFTscope._cacheParent
protected

Definition at line 296 of file scope.py.

◆ _mainScope

pyfortool.scope.PYFTscope._mainScope
protected

Definition at line 292 of file scope.py.

◆ _parentScope

pyfortool.scope.PYFTscope._parentScope
protected

Definition at line 294 of file scope.py.

◆ _path

pyfortool.scope.PYFTscope._path
protected

Definition at line 293 of file scope.py.

◆ path

pyfortool.scope.PYFTscope.path

Definition at line 579 of file scope.py.

◆ SCOPE_CONSTRUCT

dict pyfortool.scope.PYFTscope.SCOPE_CONSTRUCT
static
Initial value:
= {'module': 'program-unit',
'func': 'program-unit',
'sub': 'program-unit',
'type': 'T-construct',
'prog': 'program-unit',
'interface': 'interface-construct',
'submodule': 'program-unit'}

Definition at line 263 of file scope.py.

◆ SCOPE_STMT

dict pyfortool.scope.PYFTscope.SCOPE_STMT
static
Initial value:
= {'module': 'module-stmt',
'func': 'function-stmt',
'sub': 'subroutine-stmt',
'type': 'T-stmt',
'prog': 'program-stmt',
'interface': 'interface-stmt',
'submodule': 'submodule-stmt'}

Definition at line 256 of file scope.py.

◆ tree

pyfortool.scope.PYFTscope.tree

Definition at line 295 of file scope.py.


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