PyForTool
Python-fortran-tool
Loading...
Searching...
No Matches
Public Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
pyfortool.variables.VarList Class Reference

Public Member Functions

 __init__ (self, mainScope, _preCompute=None)
 
 __getitem__ (self, *args, **kwargs)
 
 __setitem__ (self, *args, **kwargs)
 
 __delitem__ (self, *args, **kwargs)
 
 __len__ (self, *args, **kwargs)
 
 restrict (self, scopePath, excludeContains)
 
 findVar (self, varName, array=None, exactScope=False, extraVarList=None)
 
 showVarList (self)
 

Static Protected Member Functions

 _fromScope (mainScope)
 

Protected Attributes

 _varList
 
 _scopePath
 
 _fullVarList
 

Detailed Description

Store the characteristics of all variables contained in a source code.

This class provides methods to query, search, and manage variables
declared in a FORTRAN scope (module, subroutine, function, or type).

Attributes
----------
_varList : list
    List of variable descriptors for the current scope.
_fullVarList : list
    Complete list of variable descriptors for the whole file.
_scopePath : str
    Path of the current scope (e.g., 'module:MODULE/sub:SUB').

Notes
-----
- Variables are found in modules only if the 'ONLY' attribute is used.
- Array specification and type are unknown for module variables.
- 'ASSOCIATE' statements are not followed.

Examples
--------
>>> pft = PYFT('myfile.F90')
>>> vl = pft.varList
>>> vl.findVar('X')
{'n': 'X', 't': 'REAL', 'as': [(None, '10')], 'i': None, ...}
>>> vl.findVar('Y', array=True)  # Search only arrays
{'n': 'Y', 't': 'REAL', 'as': [(None, '100'), (None, '100')], ...}

Definition at line 84 of file variables.py.

Constructor & Destructor Documentation

◆ __init__()

pyfortool.variables.VarList.__init__ (   self,
  mainScope,
  _preCompute = None 
)
:param mainScope: a PYFT object
:param _preCompute: pre-computed values (_varList, _fullVarList, _scopePath)

Notes: - variables are found in modules only if the 'ONLY' attribute is used
       - array specification and type is unknown for module variables
       - 'ASSOCIATE' statements are not followed

Definition at line 115 of file variables.py.

Here is the call graph for this function:

Member Function Documentation

◆ __delitem__()

pyfortool.variables.VarList.__delitem__ (   self,
args,
**  kwargs 
)

Definition at line 144 of file variables.py.

◆ __getitem__()

pyfortool.variables.VarList.__getitem__ (   self,
args,
**  kwargs 
)

Definition at line 138 of file variables.py.

◆ __len__()

pyfortool.variables.VarList.__len__ (   self,
args,
**  kwargs 
)

Definition at line 147 of file variables.py.

◆ __setitem__()

pyfortool.variables.VarList.__setitem__ (   self,
args,
**  kwargs 
)

Definition at line 141 of file variables.py.

◆ _fromScope()

pyfortool.variables.VarList._fromScope (   mainScope)
staticprotected
:param mainScope: a PYFT object

Definition at line 151 of file variables.py.

Here is the caller graph for this function:

◆ findVar()

pyfortool.variables.VarList.findVar (   self,
  varName,
  array = None,
  exactScope = False,
  extraVarList = None 
)
Search for a variable in the list of declared variables.

Parameters
----------
varName : str
    Name of the variable to search for.
array : bool, optional
    True to limit search to arrays only,
    False to limit search to non-array (scalar) variables only,
    None (default) to return any variable type.
exactScope : bool, optional
    If True, only search for variables declared in the current scope path.
    If False (default), search in current scope and all parent scopes.
extraVarList : list, optional
    Additional list of variable descriptors to search in.
    Useful when variables are defined but not yet in varList.

Returns
-------
dict or None
    Variable descriptor dictionary with keys:
    - 'n': variable name (uppercase)
    - 't': type specification (e.g., 'REAL', 'INTEGER')
    - 'as': array specification as list of (lower, upper) tuples
    - 'i': intent specification (e.g., 'IN', 'OUT', 'INOUT')
    - 'arg': True if dummy argument
    - 'scopePath': path where variable is declared
    - 'use': module name if imported via USE statement
    Returns None if variable not found.

Notes
-----
The function returns the declaration of the variable found in the
deepest (most specific) scope. If `array` is specified, only
returns the variable if its declaration matches the expected kind.

Examples
--------
>>> vl = pft.varList
>>> vl.findVar('X')  # Find any variable named X
{'n': 'X', 't': 'REAL', 'as': [], 'i': None, ...}
>>> vl.findVar('Y', array=True)  # Find array Y
{'n': 'Y', 't': 'REAL', 'as': [(None, '100')], ...}
>>> vl.findVar('Z', array=False)  # Find scalar Z
{'n': 'Z', 't': 'INTEGER', 'as': [], ...}
>>> vl.findVar('P', exactScope=True)  # Only in current scope
{'n': 'P', 't': 'REAL', ...}

Definition at line 275 of file variables.py.

◆ restrict()

pyfortool.variables.VarList.restrict (   self,
  scopePath,
  excludeContains 
)
Return a VarList restricted to a specific scope.

Parameters
----------
scopePath : str
    Scope path to restrict to (e.g., 'module:MOD/sub:SUB').
    Use empty string or '/' for root scope.
excludeContains : bool
    If True, exclude variables declared in CONTAINS sections
    (nested subroutines/functions).

Returns
-------
VarList
    New VarList instance restricted to the specified scope.

Examples
--------
>>> vl = pft.varList
>>> sub_vl = vl.restrict('module:MOD/sub:SUB', excludeContains=True)
>>> sub_vl.findVar('X')
{'n': 'X', ...}

Definition at line 241 of file variables.py.

◆ showVarList()

pyfortool.variables.VarList.showVarList (   self)
Display a formatted list of all variables.

Prints detailed information about each variable including:
- Name and type
- Whether it's scalar or array (with dimensions)
- Whether it's a dummy argument (with intent) or local variable
- Module origin for imported variables

Examples
--------
>>> pft = PYFT('input.F90')
>>> pft.varList.showVarList()
List of variables declared in /module:MOD/sub:SUB:
  Variable X:
    is scalar
    is a dummy argument with intent IN
  Variable Y:
    is of rank 2, with dimensions (:,1:10)
    is a local variable

Definition at line 344 of file variables.py.

Member Data Documentation

◆ _fullVarList

pyfortool.variables.VarList._fullVarList
protected

Definition at line 133 of file variables.py.

◆ _scopePath

pyfortool.variables.VarList._scopePath
protected

Definition at line 132 of file variables.py.

◆ _varList

pyfortool.variables.VarList._varList
protected

Definition at line 131 of file variables.py.


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