loki.types.scope

Representation of symbol tables and scopes in Loki’s internal representation (IR)

Classes

Scope([parent])

Scoping object that manages type caching and derivation for typed symbols.

class Scope(parent: dataclasses.InitVar[object] | None = None)

Bases: object

Scoping object that manages type caching and derivation for typed symbols.

The Scope provides a symbol table that uniquely maps a symbol’s name to its SymbolAttributes or, for a derived type definition, directly to its DerivedType.

See SymbolTable for more details on how to look-up symbols.

Parameters:
  • parent (Scope, optional) – The enclosing scope, thus allowing recursive look-ups

  • symbol_attrs (SymbolTable, optional) – Use the given symbol table instead of instantiating a new

symbol_attrs: SymbolTable
parent: dataclasses.InitVar[object] = None
property parents

All parent scopes enclosing the current scope, with the top-level scope at the end of the list

Returns:

The list of parent scopes

Return type:

tuple

rescope_symbols()

Make sure all symbols declared and used inside this node belong to a scope in the scope hierarchy

make_complete(**frontend_args)

Trigger a re-parse of the object if incomplete to produce a full Loki IR

See ProgramUnit.make_complete for more details.

This method relays the call only to the parent.

clone(**kwargs)

Create a copy of the scope object with the option to override individual parameters

Note that this will also create a copy of the symbol table via SymbolTable.clone and force rescoping of variables, unless symbol_attrs and rescope_symbols are explicitly specified.

Parameters:

**kwargs (Any parameter from the constructor)

Returns:

The cloned scope object

Return type:

type(self)

get_symbol_scope(name)

Find the scope in which name is declared

This performs a recursive lookup in the SymbolTable to find the scope in which name is declared. Note, that this may be the scope with a Import of this name and not the original declaration.

Parameters:

name (str) – The name of the symbol to look for

Returns:

The scope object in which the symbol is declared, or None if not found

Return type:

Scope or None

declare(name, dtype, fail=True, **kwargs)

Method to add type information, including the data type :param:`dtype`, for a new variable in a Scope and update the symbol table accordingly.

This method should be used for the initial type declaration of a variable and will by default fail if the variable has already been declared. To update an existing variable, the :method:`update` should be used.

To completely re-declare an existing variable, fail=True can be passed to this method. This case prior type information will be removed from the symbol table before the re-declaration.

Parameters:
  • name (str) – Name of the variable to declare symbol information for.

  • dtype (DataType or str) – Basic data typer of the variable

  • fail (bool, optional) – Flag to override the default failure on attempted re-declaration.

  • **kwargs (optional) – Any additional attributes that should be stored as properties in the symbol table.

update(name, fail=True, **kwargs)

Method to update the type information of a variable in a the symbol table of a Scope.

This method should only be used update symbol attributes after the initial declaration of the variable via the update(). If the symbol has not already been declared, a ValueError is raised, unless the fail=True override flag is set.

Parameters:
  • name (str) – Name of the variable to update symbol information for.

  • fail (bool, optional) – Flag to override the default failure when variable was not declared.

  • **kwargs (optional) – Any additional attributes that should be stored as properties in the symbol table.

get_type(name, recursive=True, fail=True)

Method to retrieve the type information (set of symbol attributes) from the internal symbol table for a given variable name.

This method will be default fail if no type information is found and may recurse to the parent Scope. This default behaviour can be overriden with the respective flags.

Parameters:
  • name (str) – Name of the variable to look up

  • recursive (bool) – Use recursive look-up in parent scopes

  • fail (bool, optional) – Flag to override the default failure when variable was not declared.

Returns:

The collection of attributes associated with this symbol

Return type:

SymbolAttributes

get_dtype(name, recursive=True, fail=True)

Method to retrieve the data type from the internal symbol table for a given variable name.

This convenience method is equivalent to using scope.get_type(name).dtype.

This method will be default fail if no type information is found and may recurse to the parent Scope. This default behaviour can be overriden with the respective flags.

Parameters:
  • name (str) – Name of the variable to look up

  • recursive (bool) – Use recursive look-up in parent scopes

  • fail (bool, optional) – Flag to override the default failure when variable was not declared.

Returns:

The collection of attributes associated with this symbol

Return type:

SymbolAttributes