2506 visibility_map=
None):
2508 Transform the variable declarations in the fparser2 parse tree into
2509 symbols in the symbol table of the PSyIR parent node. The default
2510 visibility of any new symbol is taken from the symbol table associated
2511 with the `parent` node if necessary. The `visibility_map` provides
2512 information on any explicit symbol visibilities that are specified
2513 for the declarations.
2515 :param parent: PSyIR node in which to insert the symbols found.
2516 :type parent: :py:class:`psyclone.psyir.nodes.KernelSchedule`
2517 :param nodes: fparser2 AST nodes containing declaration statements.
2518 :type nodes: List[:py:class:`fparser.two.utils.Base`]
2519 :param arg_list: fparser2 AST node containing the argument list.
2520 :type arg_list: :py:class:`fparser.Fortran2003.Dummy_Arg_List`
2521 :param visibility_map: mapping of symbol names to explicit
2523 :type visibility_map: dict[
2524 str, :py:class:`psyclone.psyir.symbols.Symbol.Visibility`]
2526 :raises GenerationError: if an INCLUDE statement is encountered.
2527 :raises NotImplementedError: the provided declarations contain
2528 attributes which are not supported yet.
2529 :raises GenerationError: if the parse tree for a USE statement does
2530 not have the expected structure.
2531 :raises SymbolError: if a declaration is found for a Symbol that is
2532 already in the symbol table with a defined interface.
2533 :raises InternalError: if the provided declaration is an unexpected
2534 or invalid fparser or Fortran expression.
2537 if visibility_map
is None:
2541 self._process_use_stmts(parent, nodes, visibility_map)
2544 statics_list = self._process_save_statements(nodes, parent)
2549 for decl
in walk(nodes, Fortran2003.Derived_Type_Def):
2550 self._process_derived_type_decln(parent, decl, visibility_map)
2555 incl_nodes = walk(nodes, (Fortran2003.Include_Stmt,
2556 C99Preprocessor.Cpp_Include_Stmt))
2560 self._include_handler(incl_nodes[0], parent)
2566 if isinstance(node, Fortran2003.Interface_Block):
2568 self._process_interface_block(node, parent.symbol_table,
2571 elif isinstance(node, Fortran2003.Type_Declaration_Stmt):
2573 self._process_decln(parent, parent.symbol_table, node,
2574 visibility_map, statics_list)
2575 except NotImplementedError:
2581 specs = walk(node, Fortran2003.Access_Spec)
2583 decln_vis = _process_access_spec(specs[0])
2585 decln_vis = parent.symbol_table.default_visibility
2587 orig_children = list(node.children[2].children[:])
2588 for child
in orig_children:
2592 node.children[2].items = (child,)
2593 symbol_name = str(child.children[0]).lower()
2594 vis = visibility_map.get(symbol_name, decln_vis)
2603 routine_sym = parent.symbol_table.lookup_with_tag(
2604 "own_routine_symbol")
2605 if routine_sym.name.lower() == symbol_name:
2606 parent.symbol_table.remove(routine_sym)
2607 tag =
"own_routine_symbol"
2612 datatype, init = self._get_partial_datatype(
2613 node, parent, visibility_map)
2619 parent.symbol_table.add(
2621 symbol_name, UnsupportedFortranType(
2623 partial_datatype=datatype),
2624 interface=UnknownInterface(),
2626 initial_value=init),
2629 except KeyError
as err:
2630 if len(orig_children) == 1:
2632 f
"Error while processing unsupported "
2633 f
"declaration ('{node}'). An entry for "
2634 f
"symbol '{symbol_name}' is already in "
2635 f
"the symbol table.")
from err
2637 node.children[2].items = tuple(orig_children)
2639 elif isinstance(node, (Fortran2003.Access_Stmt,
2640 Fortran2003.Save_Stmt,
2641 Fortran2003.Derived_Type_Def,
2642 Fortran2003.Stmt_Function_Stmt,
2643 Fortran2003.Common_Stmt,
2644 Fortran2003.Use_Stmt)):
2648 elif isinstance(node, Fortran2003.Implicit_Part):
2654 child_nodes = walk(node, Fortran2003.Format_Stmt)
2656 raise NotImplementedError(
2657 f
"Error processing implicit-part: Format statements "
2658 f
"are not supported but found '{child_nodes[0]}'")
2659 child_nodes = walk(node, Fortran2003.Implicit_Stmt)
2660 if any(imp.children != (
'NONE',)
for imp
in child_nodes):
2661 raise NotImplementedError(
2662 f
"Error processing implicit-part: implicit variable "
2663 f
"declarations not supported but found '{node}'")
2665 elif isinstance(node, Fortran2003.Namelist_Stmt):
2672 parent.symbol_table.new_symbol(
2673 root_name=
"_PSYCLONE_INTERNAL_NAMELIST",
2674 symbol_type=DataSymbol,
2675 datatype=UnsupportedFortranType(str(node)))
2677 raise NotImplementedError(
2678 f
"Error processing declarations: fparser2 node of type "
2679 f
"'{type(node).__name__}' not supported")
2684 self._process_parameter_stmts(nodes, parent)
2689 self._process_common_blocks(nodes, parent)
2691 if visibility_map
is not None:
2695 for name, vis
in visibility_map.items():
2696 if name
not in parent.symbol_table:
2699 _find_or_create_unresolved_symbol(parent, name,
2704 for arg_name
in [x.string.lower()
for x
in arg_list]:
2705 symbol = parent.symbol_table.lookup(arg_name)
2706 if not symbol.is_argument:
2715 symbol.interface = ArgumentInterface(
2716 ArgumentInterface.Access.UNKNOWN)
2717 arg_symbols.append(symbol)
2720 parent.symbol_table.specify_argument_list(arg_symbols)
2721 except KeyError
as info:
2722 decls_str_list = [str(node)
for node
in nodes]
2723 arg_str_list = [arg.string.lower()
for arg
in arg_list]
2724 raise InternalError(
2725 f
"The argument list {arg_str_list} for routine '{parent.name}'"
2726 f
" does not match the variable declarations:\n"
2727 f
"{os.linesep.join(decls_str_list)}\n"
2728 f
"(Note that PSyclone does not support implicit declarations.)"
2729 f
" Specific PSyIR error is {info}.")
from info
2735 for stmtfn
in walk(nodes, Fortran2003.Stmt_Function_Stmt):
2736 (fn_name, arg_list, scalar_expr) = stmtfn.items
2738 symbol = parent.symbol_table.lookup(fn_name.string.lower())
2742 array_subscript = arg_list.items
2744 assignment_rhs = scalar_expr
2747 assignment = Assignment(parent=parent)
2748 parent.addchild(assignment)
2751 lhs = ArrayReference(symbol, parent=assignment)
2752 self.process_nodes(parent=lhs, nodes=array_subscript)
2753 assignment.addchild(lhs)
2756 self.process_nodes(parent=assignment,
2757 nodes=[assignment_rhs])
2759 raise InternalError(
2760 f
"Could not process '{stmtfn}'. Symbol "
2761 f
"'{symbol.name}' is in the SymbolTable but it is not "
2762 f
"an array as expected, so it can not be recovered as "
2763 f
"an array assignment.")
2764 except KeyError
as err:
2765 raise NotImplementedError(
2766 f
"Could not process '{stmtfn}'. Statement Function "
2767 f
"declarations are not supported.")
from err