pyccel.ast.basic module#
This module contains classes from which all pyccel nodes inherit. They are:
PyccelAstNode, which provides a base class for our Python AST nodes;
TypedAstNode, which inherits from PyccelAstNode and provides a base class for AST nodes requiring type descriptors.
- class pyccel.ast.basic.Immutable[source]#
Bases:
object
Superclass for classes which cannot inherit from PyccelAstNode
- class pyccel.ast.basic.PyccelAstNode[source]#
Bases:
object
PyccelAstNode class from which all objects in the Pyccel AST inherit.
This foundational class provides all the functionalities that are common to objects in the Pyccel AST. This includes the construction and navigation of the AST tree as well as an indication of the stage in which the object is valid (syntactic/semantic/etc).
- clear_syntactic_user_nodes()[source]#
Delete all information about syntactic user nodes.
Delete all user nodes which are only valid for the syntactic stage from the list of user nodes. This is useful if the same node is used for the syntactic and semantic stages.
- property current_user_node#
Get the user node for an object with only one user node
- get_all_user_nodes()[source]#
Returns all the objects user nodes. This function should only be called in PyccelAstNode
- get_attribute_nodes(search_type, excluded_nodes=())[source]#
Get all objects of the requested type in the current object.
Returns all objects of the requested type which are stored in the current object.
- Parameters:
search_type (ClassType or tuple of ClassTypes) – The types which we are looking for.
excluded_nodes (tuple of types) – Types for which get_attribute_nodes should not be called.
- Returns:
List containing all objects of the requested type which exist in self.
- Return type:
list
- get_direct_user_nodes(condition)[source]#
Get the direct user nodes which satisfy the condition.
This function returns all the direct user nodes which satisfy the provided condition. A “direct” user node is a node which uses the instance directly (e.g. a FunctionCall uses a FunctionDef directly while a FunctionDef uses a Variable indirectly via a FunctionDefArgument or a CodeBlock). Most objects only have 1 direct user node so this function only makes sense for an object with multiple user nodes. E.g. a Variable, or a FunctionDef.
- Parameters:
condition (lambda) – The condition which the user nodes must satisfy to be returned.
- Returns:
The user nodes which satisfy the condition.
- Return type:
list
- get_user_nodes(search_type, excluded_nodes=())[source]#
Returns all objects of the requested type which use the current object
- Parameters:
search_type (ClassType or tuple of ClassTypes) – The types which we are looking for
excluded_nodes (tuple of types) – Types for which get_user_nodes should not be called
Results
-------
list (List containing all objects of the) – requested type which contain self
- invalidate_node()[source]#
Indicate that this node is no longer used.
Indicate that this node is temporary and is no longer used. This will allow it to remove itself from its attributes’ users. If an attribute subsequently has no users, invalidate_node is called recursively. This prevents the tree from becoming filled with temporary objects and prevents obsolete objects being retrieved when searching for attribute nodes.
- property is_atomic#
Indicates whether the object has any attribute nodes. Returns true if it is an atom (no attribute nodes) and false otherwise
- is_attribute_of(node)[source]#
Identifies whether this object is an attribute of node. The function searches recursively down the attribute tree.
- Parameters:
node (PyccelAstNode) – The object whose attributes we are interested in
Results
-------
bool
- property is_unused#
Indicates whether the class has any users
- is_user_of(node, excluded_nodes=())[source]#
Identifies whether this object is a user of node. The function searches recursively up the user tree
- Parameters:
node (PyccelAstNode) – The object whose users we are interested in
excluded_nodes (tuple of types) – Types for which is_user_of should not be called
Results
-------
bool
- property pyccel_staging#
Indicate the stage at which the object was created.
Indicate the stage at which the object was created [syntactic/semantic/codegen/cwrapper].
- property python_ast#
Get an ast.AST object describing the parsed code that this node represents.
Get the AST (abstract syntax tree) object which Python parsed in the original code. This object describes the Python code being translated. It provides line numbers and columns which can be used to report the origin of any potential errors. If this object appears in multiple places in the code (e.g. Variables) then this property returns None so as not to accidentally print the wrong location.
- Returns:
The AST object which was parsed.
- Return type:
ast.AST
- property recursion_in_progress#
Recursion state used to avoid infinite loops
- remove_user_node(user_node, invalidate=True)[source]#
Remove the specified user node from the AST tree.
Indicate that the current node is no longer used by the user_node. This function is usually called by the substitute method. It removes the specified user node from the user nodes internal property meaning that the node cannot appear in the results when searching through the tree.
- Parameters:
user_node (PyccelAstNode) – Node which previously used the current node.
invalidate (bool) – Indicates whether the removed object should be invalidated.
- set_current_ast(ast_node)[source]#
Set the ast.AST object which describes the parsed code that this node currently represents.
Set the AST (abstract syntax tree) object which Python parsed in the original code and which resulted in the creation (or use) of this PyccelAstNode. This object describes the Python code being translated. It provides line numbers and columns which can be used to report the origin of any potential errors. If this function is called multiple times then accessing the AST object will result in None so as not to accidentally print the wrong code location.
- Parameters:
ast_node (ast.AST) – The AST object which was parsed.
- substitute(original, replacement, excluded_nodes=(), invalidate=True, is_equivalent=None)[source]#
Substitute object ‘original’ for object ‘replacement’ in the code.
Substitute object ‘original’ for object ‘replacement’ in the code. Any types in excluded_nodes will not be visited.
- Parameters:
original (object or tuple of objects) – The original object to be replaced.
replacement (object or tuple of objects) – The object which will be inserted instead.
excluded_nodes (tuple of types) – Types for which substitute should not be called.
invalidate (bool) – Indicates whether the removed object should be invalidated.
is_equivalent (function, optional) – A function that compares the original object to the object in the PyccelAstNode to determine if it is the object that we are searching for. Usually this is an equality in the syntactic stage and an identity comparison in the semantic stage, but occasionally a different choice may be useful.
- class pyccel.ast.basic.ScopedAstNode(scope=None)[source]#
Bases:
PyccelAstNode
Class from which all objects with a scope inherit
- property scope#
Local scope of the current object This contains all available objects in this part of the code
- class pyccel.ast.basic.TypedAstNode[source]#
Bases:
PyccelAstNode
Class from which all typed objects inherit.
The class from which all objects which can be described with type information must inherit. Objects with type information are objects which take up memory in a running program (e.g. a variable or the result of a function call). Each typed object is described by an underlying datatype, a rank, a shape, and a data layout ordering.
- property class_type#
The type of the object.
The Python type of the object. In the case of scalars this is equivalent to the datatype. For objects in (homogeneous) containers (e.g. list/ndarray/tuple), this is the type of the container.
- copy_attributes(x)[source]#
Copy the attributes describing a TypedAstNode into this node.
Copy the attributes which describe the TypedAstNode passed as argument (dtype, shape, rank, order) into this node so that the two nodes can be stored in the same object.
- Parameters:
x (TypedAstNode) – The node from which the attributes should be copied.
- property dtype#
Datatype of the object.
The underlying datatype of the object. In the case of scalars this is equivalent to the type of the object in Python. For objects in (homogeneous) containers (e.g. list/ndarray/tuple), this is the type of an arbitrary element of the container.
- property order#
The data layout ordering in memory.
Indicates whether the data is stored in row-major (‘C’) or column-major (‘F’) format. This is only relevant if rank > 1. When it is not relevant this function returns None.
- property rank#
Number of dimensions of the object.
Number of dimensions of the object. If the object is a scalar then this is equal to 0.
- property shape#
Tuple containing the length of each dimension of the object or None.
A tuple containing the length of each dimension of the object if the object is an array (with rank>0). Otherwise None.
- classmethod static_type()[source]#
The type of the object.
The Python type of the object. In the case of scalars this is equivalent to the datatype. For objects in (homogeneous) containers (e.g. list/ndarray/tuple), this is the type of the container.
This function is static and will return an AttributeError if the class does not have a predetermined order.