pyccel.parser.semantic module#
File containing SemanticParser. This class handles the semantic stage of the translation. See the developer docs for more details
- class pyccel.parser.semantic.SemanticParser(inputs, *, parents=(), d_parsers=(), context_dict=None, **kwargs)[source]#
Bases:
BasicParser
Class which handles the semantic stage as described in the developer docs.
This class is described in detail in developer_docs/semantic_stage.md. It determines all semantic information which must be deduced in order to print a representation of the AST resulting from the syntactic stage in one of the target languages.
- Parameters:
inputs (SyntaxParser) – A syntactic parser which has been used to generate a representation of the input code using Pyccel nodes.
parents (list) – A list of parsers describing the files which import this file.
d_parsers (list) – A list of parsers describing files imported by this file.
context_dict (dict, optional) – A dictionary describing any variables in the context where the translated objected was defined.
**kwargs (dict) – Additional keyword arguments for BasicParser.
- annotate()[source]#
Add type information to the AST.
This function is the entry point for this class. It annotates the AST object created by the syntactic stage which was collected in the constructor. The annotation adds all necessary information about the type etc to describe the object sufficiently well for printing. See the developer docs for more details.
- Returns:
An annotated object which can be printed.
- Return type:
- check_for_variable(name)[source]#
Search for a Variable object with the given name in the current scope.
Search for a Variable object with the given name in the current scope, defined by the local and global Python scopes. Return None if not found.
- Parameters:
name (str | DottedName) – The object describing the variable.
- Returns:
Returns the variable if found or None.
- Return type:
See also
get_variable
A similar function which raises an error if the Variable is not found instead of returning None.
- create_new_function_scope(syntactic_name, semantic_name, **kwargs)[source]#
Create a new Scope object for a Python function.
Create a new Scope object for a Python function with the given name, and attach any decorators’ information to the scope. The new scope is a child of the current one, and can be accessed from the dictionary of its children using the function name as key.
Before returning control to the caller, the current scope (stored in self._scope) is changed to the one just created, and the function’s name is stored in self._current_function_name.
- Parameters:
syntactic_name (str) – Function’s original name in the translated code, used as a key to retrieve the new scope.
semantic_name (str) – The new name of the function by which it will be known in the target language.
**kwargs (dict) – Keyword arguments passed through to the new scope.
- Returns:
The new scope for the function.
- Return type:
- create_tuple_of_inhomogeneous_elements(tuple_var)[source]#
Create a tuple of variables from a variable representing an inhomogeneous object.
Create a tuple of variables that can be printed in a low-level language. An inhomogeneous object cannot be represented as is in a low-level language so it must be unpacked into a PythonTuple. This function is recursive so that variables with a type such as tuple[tuple[int,bool],float] generate PythonTuple(PythonTuple(var_0_0, var_0_1), var_1).
- Parameters:
tuple_var (Variable) – A variable which may or may not be an inhomogeneous tuple.
- Returns:
An object containing only variables that can be printed in a low-level language.
- Return type:
- property d_parsers#
Returns the d_parsers parser.
- env_var_to_pyccel(env_var, *, name=None)[source]#
Convert an environment variable to a Pyccel AST node.
Convert an environment variable (i.e. a variable deduced from the context where epyccel was called) into a Pyccel AST node as though the object had been declared explicitly in the code.
- Parameters:
env_var (object) – The environment variable.
name (str, optional) – The name that was used to identify the variable.
- Returns:
The usable Pyccel AST node.
- Return type:
- get_class_construct(name)[source]#
Return the class datatype associated with name.
Return the class datatype for name if it exists. Raise an error otherwise.
- Parameters:
name (str) – The name of the class.
- Returns:
The datatype for the class.
- Return type:
- Raises:
PyccelSemanticError – Raised if the datatype cannot be found.
- get_class_prefix(name)[source]#
Search for the class prefix of a dotted name in the current scope.
Search for a Variable object with the class prefix found in the given name inside the current scope, defined by the local and global Python scopes. Return None if not found.
- Parameters:
name (DottedName) – The dotted name which begins with a class definition.
- Returns:
Returns the class definition if found or None otherwise.
- Return type:
- get_variable(name)[source]#
Get a Variable object with the given name from the current scope.
Search for a Variable object with the given name in the current scope, defined by the local and global Python scopes. Raise an error if not found.
- Parameters:
name (str) – The object describing the variable.
- Returns:
Returns the variable found in the scope.
- Return type:
- Raises:
PyccelSemanticError – Error raised if variable is not found.
See also
check_for_variable
A similar function which returns None if the Variable is not found instead of raising an error.
- get_variables(container)[source]#
Get all variables in the scope of interest.
Get a list of all variables which are
- Parameters:
container (Scope) – The object describing the relevant scope.
- Returns:
A list of variables.
- Return type:
list
- insert_attribute_to_class(class_def, self_var, attrib)[source]#
Insert a new attribute into an existing class.
Insert a new attribute into an existing class definition. In order to do this a dotted variable must be created. If the new attribute is an inhomogeneous tuple then this function is called recursively to insert each variable comprising the tuple into the class definition.
- Parameters:
- Returns:
The object that was inserted into the class definition.
- Return type:
- insert_import(name, target, storage_name=None)[source]#
Insert a new import into the scope.
Create and insert a new import in scope if it’s not defined otherwise append target to existing import.
- Parameters:
name (str-like) – The source from which the object is imported.
target (AsName) – The imported object.
storage_name (str-like) – The name which will be used to identify the Import in the container.
- property parents#
Returns the parents parser.