psyclone.parse

This directory contains classes related to parsing Fortran.

Submodules

Classes

  • ModuleInfo: This class stores mostly cached information about modules: it stores

  • ModuleManager: This class implements a singleton that manages module

class psyclone.parse.ModuleInfo(name, filename)

This class stores mostly cached information about modules: it stores the original filename, if requested it will read the file and then caches the plain text file, and if required it will parse the file, and then cache the fparser AST.

Parameters:
  • name (str) – the module name.

  • filename (str) – the name of the source file that stores this module (including path).

Inheritance

Inheritance diagram of ModuleInfo
contains_routine(routine_name)
Returns:

whether the specified routine name is part of this module or not. It will also return False if the file could not be parsed.

Return type:

bool

property filename
Returns:

the filename that contains the source code for this module.

Return type:

str

get_parse_tree()

Returns the fparser AST for this module. The first time, the file will be parsed by fparser using the Fortran 2008 standard. The AST is then cached for any future uses.

Returns:

the fparser AST for this module.

Return type:

fparser.two.Fortran2003.Program

get_psyir()

Returns the PSyIR representation of this module. This is based on the fparser tree (see get_parse_tree), and the information is cached. If the PSyIR must be modified, it needs to be copied, otherwise the modified tree will be returned from the cache in the future. If the conversion to PSyIR fails, a dummy FileContainer with an empty Container (module) is returned, which avoids additional error handling in many other subroutines. #TODO 2120: This should be revisited when improving on the error handling.

Parameters:

routine_name (Optional[str]) – optional the name of a routine.

Returns:

PSyIR representing this module.

Return type:

list[psyclone.psyir.nodes.Node]

get_source_code()

Returns the source code for the module. The first time, it will be read from the file, but the data is then cached.

Returns:

the source code.

Return type:

str

Raises:

ModuleInfoError – when the file cannot be read.

get_used_modules()

This function returns a set of all modules used in this module. Fortran intrinsic modules will be ignored. The information is based on the fparser parse tree of the module (since fparser can handle more files than PSyir, like LFRic’s constants_mod which has pre-processor directives).

Returns:

a set with all imported module names.

Return type:

set[str]

get_used_symbols_from_modules()

This function returns information about which modules are used by this module, and also which symbols are imported. The return value is a dictionary with the used module name as key, and a set of all imported symbol names as value.

Returns:

a dictionary that gives for each module name the set of symbols imported from it.

Return type:

dict[str, set[str]]

property name
Returns:

the name of this module.

Return type:

str

resolve_routine(routine_name)

This function returns a list of function names that might be actually called when the routine name is called. In most cases this is exactly name, but in case of a generic subroutine the name might change. For now (since we cannot resolve generic interfaces yet), we return the list of all possible functions that might be called.

Parameters:

routine_name (str) – the name of the routine to resolve

Returns:

list of routine name(s) that could be called.

Return type:

list[str]

class psyclone.parse.ModuleManager

This class implements a singleton that manages module dependencies.

Inheritance

Inheritance diagram of ModuleManager
add_ignore_module(module_name)

Adds the specified module name to the modules to be ignored.

Parameters:

module_name (str) – name of the module to ignore.

add_search_path(directories, recursive=True)

If the directory is not already contained in the search path, add it. Directory can either be a string, in which case it is a single directory, or a list of directories, each one a string.

Parameters:
  • directories (str | list[str]) – the directory/directories to add.

  • recursive (bool) – whether recursively all subdirectories should be added to the search path.

static get()

Static function that if necessary creates and returns the singleton ModuleManager instance.

get_all_dependencies_recursively(all_mods)

This function collects recursively all module dependencies for any of the modules in the all_mods set. I.e. it will add all modules used by any module listed in all_mods, and any modules used by the just added modules etc. In the end, it will return a dictionary that for each module lists which modules it depends on. This dictionary will be complete, i.e. all modules that are required for the original set of modules (and that could be found) will be a key in the dictionary. It will include the original set of modules as well.

If a module cannot be found (e.g. its path was not given to the ModuleManager, or it might be a system module for which the sources are not available, a message will be printed, and this module will be ignored (i.e. not listed in any dependencies). # TODO 2120: allow a choice to abort or ignore.

Parameters:

all_mods (set[str]) – the set of all modules for which to collect module dependencies.

Returns:

a dictionary with all modules that are required (directly or indirectly) for the modules in all_mods.

Return type:

dict[str, set[str]]

get_module_info(module_name)

This function returns the ModuleInformation for the specified module.

Parameters:

module_name (str) – name of the module.

Returns:

the filename that contains the module

Return type:

str

Raises:

FileNotFoundError – if the module_name is not found in either the cached data nor in the search path.

get_modules_in_file(filename)

This function returns the list of modules defined in the specified file. The base implementation assumes the use of the LFRic coding style: the file a_mod.f90 implements the module a_mod. This function can be implemented in a derived class to actually parse the source file if required.

Parameters:

filename (str) – the file name for which to find the list of modules it contains.

Returns:

the list of all modules contained in the specified file.

Return type:

list[str]

ignores()
Returns:

the set of modules to ignore.

Return type:

set[str]

sort_modules(module_dependencies)

This function sorts the given dependencies so that all dependencies of a module are before any module that needs it. Input is a dictionary that contains all modules to be sorted as keys, and the value for each module is the set of dependencies that the module depends on.

Parameters:

module_dependencies (dict[str, set[str]]) – the list of modules required as keys, with all their dependencies as value.

Returns:

the sorted list of modules.

Return type:

list[str]

Exceptions

  • ModuleInfoError: PSyclone-specific exception for use when an error with the module manager

exception psyclone.parse.ModuleInfoError(value)

PSyclone-specific exception for use when an error with the module manager happens - typically indicating that some module information cannot be found.

Parameters:

value (str) – the message associated with the error.

Inheritance

Inheritance diagram of ModuleInfoError