pyccel.ast.internals module#

File containing basic classes which are used throughout pyccel. To avoid circular imports this file should only import from basic, datatypes, and literals

class pyccel.ast.internals.Iterable(num_indices_required)[source]#

Bases: TypedAstNode

Wrapper around iterable types helping to convert between those types and a range.

Wrapper around iterable types helping to convert between those types and a range (necessary in low level languages, e.g. C and Fortran).

If an iterable can be iterated over using a range then this can be done automatically by defining the following 3 functions:

  • get_range : Returns the range required for the iteration.

  • get_python_iterable_itemReturns the item of the iterable that will be saved to the

    variables which are the loop targets. E.g. for the loop: >>> for idx, v in enumerate(var)

    this function should return the range index idx (this may be set using set_loop_counter) and var[idx]. These objects are used for type deductions.

  • get_assign_targetsReturns any objects that should be assigned to targets

    E.g. for the loop: >>> for idx, v in enumerate(var)

    The object var[idx] is returned. The printer is then responsible for creating the Assign(v, var[idx]) E.g. for the loop: >>> for r,p in zip(r_var,p_var)

    The objects r_var[idx] and p_var[idx] are returned. The index is retrieved from this class where it was set using set_loop_counter. The printer is then responsible for creating the Assign(r, r_var[idx]) and Assign(p, p_var[idx]).

Parameters:

num_indices_required (int) – The number of indices that the semantic stage should generate to correctly iterate over the object.

property loop_counters#

Returns the iterator(s) of the generated range.

Returns the iterator(s) of the generated range.

property modified_args#

Return a tuple of all the arguments which may be modified by this function.

Return a tuple of all the arguments which may be modified by this function. This is notably useful in order to determine the constness of arguments.

property num_loop_counters_required#

Number of indices that should be generate by the semantic stage.

Number of indices which must be generated in order to convert this iterable to a range. This is usually 1.

set_loop_counter(*indices)[source]#

Set the iterator(s) for the generated range.

These are iterators generated by Pyccel that were not needed for the original Python code. Ideally they will also not be necessary in the generated Python code so these objects should only be inserted into the scope during printing.

Parameters:

*indices (TypedAstNode) – The iterator(s) generated by Pyccel.

class pyccel.ast.internals.PrecomputedCode(code)[source]#

Bases: PyccelAstNode

Internal helper class for storing code which must be defined by the printer before it is needed chronologically (e.g. for inline functions as arguments to the same function). This class should be avoided if at all possible as it may break code which searches through attribute nodes, where possible use PyccelAstNode’s methods, e.g. substitute

Parameters:

code (str) – A string containing the precomputed code

property code#

The string containing the precomputed code

class pyccel.ast.internals.PyccelArrayShapeElement(arg, index)[source]#

Bases: PyccelFunction

Gets the number of elements in a given dimension of an array.

Class representing a call to a function which would return the shape of a multi-dimensional array in a given dimension.

Parameters:
  • arg (TypedAstNode) – An array of unknown shape.

  • index (int) – The dimension along which the shape should be provided.

property arg#

Object whose size is investigated.

The first argument of the function call, i.e. the array whose size is investigated.

property index#

Dimension along which the size is calculated.

The second argument of the function call, i.e. the dimension along which the array size is calculated.

name = 'shape'#
class pyccel.ast.internals.PyccelArraySize(arg)[source]#

Bases: PyccelFunction

Gets the total number of elements in an array.

Class representing a call to a function which would return the total number of elements in a multi-dimensional array.

Parameters:

arg (TypedAstNode) – An array of unknown size.

property arg#

Object whose size is investigated.

The argument of the function call, i.e. the object whose size is investigated.

name = 'size'#
class pyccel.ast.internals.PyccelFunction(*args)[source]#

Bases: TypedAstNode

Abstract class for function calls translated to Pyccel objects.

A subclass of this base class represents calls to a specific internal function of Pyccel, which may be simplified at a later stage, or made available in the target language when printing the generated code.

Parameters:

*args (iterable) – The arguments passed to the function call.

property args#

The arguments passed to the function.

Tuple containing all the arguments passed to the function call.

property is_elemental#

Whether the function acts elementwise on an array argument.

Boolean indicating whether the (scalar) function should be called elementwise on an array argument. Here we set the default to False.

property modified_args#

Return a tuple of all the arguments which may be modified by this function.

Return a tuple of all the arguments which may be modified by this function. This is notably useful in order to determine the constness of arguments.

name = None#
class pyccel.ast.internals.PyccelSymbol(name, is_temp=False)[source]#

Bases: str, Immutable

Class representing a symbol in the code.

Symbolic placeholder for a Python variable, which has a name but no type yet. This is very generic, and it can also represent a function or a module.

Parameters:
  • name (str) – Name of the symbol.

  • is_temp (bool) – Indicates if the symbol is a temporary object. This either means that the symbol represents an object originally named _ in the code, or that the symbol represents an object created by Pyccel in order to assign a temporary object. This is sometimes necessary to facilitate the translation.

Examples

>>> from pyccel.ast.internals import PyccelSymbol
>>> x = PyccelSymbol('x')
x
property is_temp#

Indicates if this symbol represents a temporary variable created by Pyccel, and was not present in the original Python code [default value : False].

class pyccel.ast.internals.Slice(start, stop, step=None, slice_type=Literal(1))[source]#

Bases: PyccelAstNode

Represents a slice in the code.

An object of this class represents the slicing of a Numpy array along one of its dimensions. In most cases this corresponds to a Python slice in the user code, where it is represented by a python.ast.Slice object.

In addition, at the wrapper and code generation stages, an integer index i used to create a view of a Numpy array is converted to an object Slice(i, i+1, 1, slice_type = Slice.Element). This allows using C variadic arguments in the function array_slicing (in file pyccel/stdlib/ndarrays/ndarrays.c).

Parameters:
  • start (PyccelSymbol or int) – Starting index.

  • stop (PyccelSymbol or int) – Ending index.

  • step (PyccelSymbol or int, default=None) – The step between indices.

  • slice_type (LiteralInteger) – The type of the slice. Either Slice.Range or Slice.Element.

Examples

>>> from pyccel.ast.internals import Slice, symbols
>>> start, end, step = symbols('start, stop, step', integer=True)
>>> Slice(start, stop)
start : stop
>>> Slice(None, stop)
 : stop
>>> Slice(start, None)
start :
>>> Slice(start, stop, step)
start : stop : step
Element = Literal(0)#
Range = Literal(1)#
property slice_type#

The type of the slice (Range or Element) Range <=> […, :, …] Element <=> […, 3, …]

property start#

Index where the slicing of the object starts

property step#

The difference between each index of the objects in the slice

property stop#

Index until which the slicing takes place