psyclone.psyir.frontend.fparser2#
This module provides the fparser2 to PSyIR front-end, it follows a Visitor Pattern to traverse relevant fparser2 nodes and contains the logic to transform each node into the equivalent PSyIR representation.
Classes#
Fparser2Reader: Class to encapsulate the functionality for processing the fparser2 AST and
- class psyclone.psyir.frontend.fparser2.Fparser2Reader(ignore_directives=True, last_comments_as_codeblocks=False, resolve_modules=False)[source]#
Class to encapsulate the functionality for processing the fparser2 AST and convert the nodes to PSyIR.
- Parameters:
ignore_directives (
bool) – Whether directives should be ignored or not (default True). Only has an effect if comments were not ignored when creating the fparser2 AST.last_comments_as_codeblocks (
bool) – Whether the last comments in the a given block (e.g. subroutine, do, if-then body, etc.) should be kept as CodeBlocks or lost (default False). Only has an effect if comments were not ignored when creating the fparser2 AST.resolve_modules (
bool) – Whether to resolve modules while parsing a file, for more precise control it also accepts a list of module names. Defaults to False.
- Raises:
TypeError – if the constructor argument is not of the expected type.
Inheritance

- class SelectTypeInfo(guard_type=<factory>, guard_type_name=<factory>, intrinsic_type_name=<factory>, clause_type=<factory>, stmts=<factory>, selector='', num_clauses=-1, default_idx=-1)[source]#
Class for storing required information from an fparser2 Select_Type_Construct.
- Parameters:
guard_type (
list[Optional[str]]) – the guard types used by ‘type is’ and ‘class is’ select-type clauses e.g. ‘REAL’, ‘REAL(KIND = 4), or ‘mytype’ in ‘type_is(REAL)’ ‘type_is(REAL(KIND = 4)’ and ‘class is(mytype)’ respectively. These are stored as a list of str, ordered as found within the select-type construct’s ‘type is’, ‘class is’ and ‘class default’ clauses with None indicating the ‘class default’ clause.guard_type_name (
list[Optional[str]]) – a string representation of the guard types used by ‘type is’ and ‘class is’ select-type clauses e.g. ‘REAL’, ‘REAL(KIND = 4)’, or ‘mytype’ are stored as ‘REAL’, ‘REAL_4’ and ‘mytype’ respectively. These are designed to be used as base variable names in the code. These are ordered as they are found in the the select type construct ‘type is, ‘class is’ and ‘class default’ clauses with None representing the ‘class default’.intrinsic_type_name (
list[Optional[str]]) – the base intrinsic string name for the particular clause or None if there is no intrinsic type. e.g. ‘type is(REAL*4)’ becomes ‘REAL’ and ‘type is(mytype)’ becomes None. These are ordered as they occur in the select-type construct’s clauses.clause_type (
list[str]) – the name of the clause in the select-type construct i.e. one of ‘type is’, ‘class is’ and ‘class default’. These are ordered as they occur within the select-type construct.stmts (
list[list[StmtBase]]) – a list of fparser2 statements holding the content of each of the select-type construct ‘type is, ‘class is’ and ‘class default’ clauses. These are ordered as they occur within the select-type construct.selector (
str) – the name of the select-type construct selector e.g. ‘selector’ in ‘select type(selector)’.num_clauses (
int) – the number of ‘type is’, ‘class is’ and ‘class default’ clauses in the select type construct.default_idx (
int) – index of the ‘default’ clause as it appears within the select-type construct’s ‘type is, ‘class is’ and ‘class default’ clauses, or -1 if no default clause is found.
- generate_psyir(parse_tree, filename='')[source]#
Translate the supplied fparser2 parse_tree into PSyIR.
- Parameters:
parse_tree (
fparser.two.Fortran2003.Program) – the supplied fparser2 parse tree.filename (Optional[str]) – associated name for FileContainer.
- Returns:
PSyIR of the supplied fparser2 parse_tree.
- Return type:
- Raises:
GenerationError – if the root of the supplied fparser2 parse tree is not a Program.
- get_routine_schedules(name, module_ast)[source]#
Create one or more schedules for routines corresponding to the supplied name in the supplied fparser2 AST. (There can be more than one routine if the supplied name corresponds to an interface block in the AST.)
- Parameters:
name (str) – name of the subroutine represented by the kernel.
module_ast (
fparser.two.Fortran2003.Program) – fparser2 AST of the full module where the kernel code is located.
- Returns:
PSyIR schedules representing the matching subroutine(s).
- Return type:
- Raises:
GenerationError – if supplied parse tree contains more than one module.
GenerationError – unable to generate a kernel schedule from the provided fpaser2 parse tree.
- static nodes_to_code_block(parent, fp2_nodes, message=None)[source]#
Create a CodeBlock for the supplied list of fparser2 nodes and then wipe the list. A CodeBlock is a node in the PSyIR (Schedule) that represents a sequence of one or more Fortran statements and/or expressions which PSyclone does not attempt to handle.
- Parameters:
parent (
psyclone.psyir.nodes.Node) – Node in the PSyclone AST to which to add this CodeBlock.fp2_nodes (list of
fparser.two.utils.Base) – list of fparser2 AST nodes constituting the CodeBlock.message (Optional[str]) – Include a preceeding comment attached to the CodeBlock.
- Returns:
a CodeBlock instance.
- Return type:
psyclone.CodeBlock
- static process_access_statements(nodes)[source]#
Search the supplied list of fparser2 nodes (which must represent a complete Specification Part) for any accessibility statements (e.g. “PUBLIC :: my_var”) to determine the default visibility of symbols as well as identifying those that are explicitly declared as public or private.
- Parameters:
nodes (list of
fparser.two.utils.Base) – nodes in the fparser2 parse tree describing a Specification Part that will be searched.- Returns:
default visibility of symbols within the current scoping unit and dict of symbol names with explicit visibilities.
- Return type:
2-tuple of (
psyclone.symbols.Symbol.Visibility, dict)- Raises:
InternalError – if an accessibility attribute which is not ‘public’ or ‘private’ is encountered.
GenerationError – if the parse tree is found to contain more than one bare accessibility statement (i.e. ‘PUBLIC’ or ‘PRIVATE’)
GenerationError – if a symbol is explicitly declared as being both public and private.
- process_comment(comment, preceding_comments)[source]#
Process a comment and attach it to the last PSyIR object (Symbol or Node) if it is an inline comment. Otherwise append it to the preceding_comments list. Ignore empty comments.
- Parameters:
comment (
fparser.two.utils.Comment) – Comment to process.preceding_comments (List[
fparser.two.utils.Comment]) – List of comments that precede the next node.
- process_declarations(parent, nodes, arg_list, visibility_map=None)[source]#
Transform the variable declarations in the fparser2 parse tree into symbols in the symbol table of the PSyIR parent node. The default visibility of any new symbol is taken from the symbol table associated with the parent node if necessary. The visibility_map provides information on any explicit symbol visibilities that are specified for the declarations.
- Parameters:
parent (
psyclone.psyir.nodes.KernelSchedule) – PSyIR node in which to insert the symbols found.nodes (List[
fparser.two.utils.Base]) – fparser2 AST nodes containing declaration statements.arg_list (
fparser.Fortran2003.Dummy_Arg_List) – fparser2 AST node containing the argument list.visibility_map (dict[ str,
psyclone.psyir.symbols.Symbol.Visibility]) – mapping of symbol names to explicit visibilities.
- Raises:
GenerationError – if an INCLUDE statement is encountered.
NotImplementedError – the provided declarations contain attributes which are not supported yet.
GenerationError – if the parse tree for a USE statement does not have the expected structure.
SymbolError – if a declaration is found for a Symbol that is already in the symbol table with a defined interface.
InternalError – if the provided declaration is an unexpected or invalid fparser or Fortran expression.
- process_nodes(parent, nodes)[source]#
Create the PSyIR of the supplied list of nodes in the fparser2 AST.
- Parameters:
parent (
psyclone.psyir.nodes.Node) – Parent node in the PSyIR we are constructing.nodes (list[
fparser.two.utils.Base]) – List of sibling nodes in fparser2 AST.