f90wrap.transform module

class f90wrap.transform.AccessUpdater(force_public=None)[source]

Bases: FortranTransformer

Visit module contents and update public_symbols and private_symbols lists to be consistent with (i) default module access; (ii) public and private statements at module level; (iii) public and private statement in types; (iv) public and private attributes of individual elements.

update_access(node, mod, default_access, in_type=False)[source]
visit_Element(node)[source]
visit_Interface(node)[source]
visit_Module(mod)[source]
visit_Procedure(node)[source]
visit_Type(node)[source]
class f90wrap.transform.ArgumentNameConflictResolver[source]

Bases: FortranTransformer

Resolves name conflicts between procedure arguments and USE-imported symbols.

When f90wrap generates wrappers, it creates use module_name statements that import ALL symbols from a module. This can cause Fortran name collisions when an argument has the same name as another procedure in the same module.

This transformer detects such conflicts and renames arguments with an _in suffix.

visit_Module(node)[source]

Track current module while visiting

visit_Procedure(node)[source]

Detect and resolve argument name conflicts in procedures

class f90wrap.transform.ArrayDimensionConverter[source]

Bases: FortranVisitor

Transform unspecified dimensions into additional dummy arguments

e.g. the following code

subroutine foo(a)

integer a(:)

end subroutine foo

becomes:

subroutine foo(a, n0)

integer a(n0) integer n0 !f2py intent(hide), depend(a) :: n0 = shape(a,0)

end subroutine foo

valid_dim_re = re.compile('^(((\\d+:)?\\d+)|([-0-9.e]+)|(size\\([_a-zA-Z0-9\\+\\-\\*\\/,]*\\))|(len\\(.*\\)))$')
visit_Procedure(node)[source]
class f90wrap.transform.BindConstructorInterfaces[source]

Bases: FortranTransformer

Moves interfaces named after a type into that type and marks as constructor.

The Fortran idiom for defining custom constructors is to create an interface with the same name as the type which references one or more free functions that return initialized instances of the type. This transformer locates such interfaces, moves them into the type, and marks them as constructors.

visit_Module(node)[source]
class f90wrap.transform.ConstructorExcessToClassMethod[source]

Bases: FortranTransformer

Handle classes with multiple constructors

Count the number of constructors per class and choose only one. Method of choice: the one with the shortest name. The rest are relabeled to classmethods

visit_Module(node)
visit_Type(node)[source]
class f90wrap.transform.DuplicateAbstractImplementation[source]

Bases: FortranVisitor

Duplicate bound procedure node from abstract class to childs type

visit_Type(node)[source]
class f90wrap.transform.FunctionToSubroutineConverter[source]

Bases: FortranTransformer

Convert all functions to subroutines, with return value as an intent(out) argument after the last non-optional argument

visit_Function(node)[source]
class f90wrap.transform.IntentOutToReturnValues[source]

Bases: FortranTransformer

Convert all Subroutine and Function intent(out) arguments to return values

visit_Procedure(node)[source]
class f90wrap.transform.LinkBoundDType[source]

Bases: FortranVisitor

Add bound info in procedure nodes

visit_Type(node)[source]
class f90wrap.transform.MethodFinder(types, constructor_names, destructor_names, short_names, move_methods, shorten_routine_names=True, modules_for_type=None)[source]

Bases: FortranTransformer

visit_Binding(node)[source]
visit_Interface(node)[source]
visit_Procedure(node, interface=None)[source]
class f90wrap.transform.NormaliseTypes(kind_map)[source]

Bases: FortranVisitor

Convert all type names to standard form and resolve kind names

visit_Argument(node)
visit_Declaration(node)[source]
class f90wrap.transform.OnlyAndSkip(kept_subs, kept_mods)[source]

Bases: FortranTransformer

This class does the job of removing nodes from the tree which are not necessary to write wrappers for (given user-supplied values for only and skip).

Currently it takes a list of subroutines and a list of modules to write wrappers for. If empty, it does all of them.

visit_Module(node)[source]
visit_Procedure(node)[source]
class f90wrap.transform.PrivateSymbolsRemover[source]

Bases: FortranTransformer

Transform a tree by removing private symbols

visit_Binding(node)[source]
visit_Element(node)
visit_Interface(node)[source]
visit_Module(mod)[source]
visit_Procedure(node)[source]
visit_Type(node)
class f90wrap.transform.RenameArgumentsPython(types)[source]

Bases: FortranVisitor

visit_Argument(node)[source]
visit_Procedure(node)[source]
class f90wrap.transform.RenameInterfacesBindingsPython[source]

Bases: FortranVisitor

visit_Binding(node)[source]
visit_Interface(node)[source]
class f90wrap.transform.RenameReservedWords(types, name_map=None)[source]

Bases: FortranVisitor

visit_Argument(node)[source]
visit_Element(node)
visit_Module(node)
visit_Procedure(node)
visit_Type(node)
class f90wrap.transform.ReorderForInheritance[source]

Bases: FortranVisitor

Reorder so that parent classes are defined defore child

visit_Module(node)[source]
visit_Root(node)[source]
class f90wrap.transform.ReorderOptionalArgumentsPython[source]

Bases: FortranVisitor

Move optional arguments after non-optional arguments: in Fortran they can come in any order, but in Python optional arguments must come at the end of argument list.

visit_Procedure(node)[source]
class f90wrap.transform.ResolveBindingPrototypes[source]

Bases: FortranTransformer

Replaces prototypes in type binding declarations with referenced procedure.

FIXME: Fortran allows module procedures to be bound to more than one type

procedure, i.e. x%fun1() and x%fun2() can both bind to module procedure x_fun(). The approach below only support 1-to-1 mappings.

visit_Module(node)[source]
class f90wrap.transform.ResolveInterfacePrototypes[source]

Bases: FortranTransformer

Replaces prototypes in interface declarations with referenced procedure

visit_Module(node)[source]
class f90wrap.transform.SetInterfaceProcedureCallNames[source]

Bases: FortranVisitor

Set call names of procedures within overloaded interfaces to the name of the interface

visit_Interface(node)[source]
class f90wrap.transform.StringLengthConverter(string_lengths, default_string_length)[source]

Bases: FortranVisitor

Convert lengths of all character strings to standard format

Looks in all Procedure arguments and Type elements. Changes from ‘(len=*)’ or ‘(*)’ syntax to () syntax.

visit_Declaration(node)[source]
class f90wrap.transform.UnwrappablesRemover(callbacks, types, constructors, destructors, remove_optional_arguments)[source]

Bases: FortranTransformer

visit_Argument(node)[source]
visit_Binding(node)[source]
visit_Interface(node)[source]
visit_Module(node)[source]

Remove unwrappable elements inside modules.

As above, but also includes derived type elements from modules that do not have the “target” attribute

visit_Procedure(node)[source]
visit_Type(node)[source]

Remove unwrappable elements inside derived types

f90wrap.transform.add_missing_constructors(tree)[source]
f90wrap.transform.add_missing_destructors(tree)[source]
f90wrap.transform.append_type_dimension(tree, types)[source]
f90wrap.transform.collapse_single_interfaces(tree, keep_single_interfaces=False)[source]

Collapse interfaces which contain only a single procedure.

f90wrap.transform.convert_array_intent_out_to_intent_inout(tree)[source]

Find all intent(out) array arguments and convert to intent(inout)

f90wrap.transform.convert_derived_type_arguments(tree, init_lines, sizeof_fortran_t)[source]
f90wrap.transform.create_super_types(tree, types)[source]
f90wrap.transform.extract_dimensions_parameters(d, tree)[source]
f90wrap.transform.find_inheritence_relations(tree)[source]

Fill child and parent member with fortran inheritences

f90wrap.transform.find_referenced_modules(mods, tree)[source]

Given a set of modules in a parse tree, find any modules (recursively) used by these.

Parameters:
  • mods (set) – initial modules to search, must be included in the tree.

  • tree (fortran.Root() object.) – the full fortran parse tree from which the mods have been taken.

Returns:

all_mods – Module() objects which are recursively used by the given modules.

Return type:

set

f90wrap.transform.find_referenced_types(mods, tree)[source]

Given a set of modules in a parse tree, find any types either defined in or referenced by the module, recursively.

Parameters:
  • mods (set) – initial modules to search, must be included in the tree.

  • tree (fortran.Root object.)

  • tree – the full fortran parse tree from which the mods have been taken.

Returns:

kept_types – modules given, or recursively referenced by those types.

Return type:

set of Type() objects which are referenced or defined in the

f90wrap.transform.fix_element_uses_clauses(tree, types)[source]

Add uses clauses to derived type elements in modules

f90wrap.transform.fix_subroutine_type_arrays(tree, types)[source]
f90wrap.transform.fix_subroutine_uses_clauses(tree, types)[source]

Walk over all nodes in tree, updating subroutine uses clauses to include the parent module and all necessary modules from types

Also rename any arguments that clash with module names.

f90wrap.transform.procedures_and_abstract_interfaces(procedure_map, node)[source]

Add procedures from abstract interfaces to the procedure map

This is needed because abstract interfaces are not included in the module interfaces list, and so are not resolved by ResolveInterfacePrototypes.

f90wrap.transform.remove_private_symbols(node, force_public=None)[source]

Walk the tree starting at node, removing all private symbols.

This function first applies the AccessUpdater transformer to ensure module public_symbols and private_symbols are up to date with default_access and individual public and private attributes.

f90wrap.transform.set_intent(attributes, intent)[source]

Remove any current “intent” from attributes and replace with intent given

f90wrap.transform.shorten_long_name(name)[source]
f90wrap.transform.transform_to_f90_wrapper(tree, types, callbacks, constructors, destructors, short_names, init_lines, string_lengths, default_string_length, sizeof_fortran_t, kind_map)[source]
Additional Fortran-specific transformations:
  • Conversion of derived type arguments to opaque integer arrays via Fortran transfer() intrinsic.

  • Normalise type declarations

f90wrap.transform.transform_to_generic_wrapper(tree, types, callbacks, constructors, destructors, short_names, init_lines, kept_subs, kept_mods, argument_name_map, move_methods, shorten_routine_names, modules_for_type, remove_optional_arguments, force_public=None, keep_single_interfaces=False)[source]

Apply a number of rules to tree to make it suitable for passing to a F90 and Python wrapper generators. Transformations performed are:

  • Removal of procedures and modules not provided by the user

  • Removal of private symbols

  • Removal of unwrappable routines and optional arguments

  • Addition of missing constructor and destructor wrappers

  • Conversion of all functions to subroutines

  • Updating call names of procedures within interfaces

  • Update of subroutine uses clauses

f90wrap.transform.transform_to_py_wrapper(tree, types)[source]
Additional Python-specific transformations:
  • Convert intent(out) arguments to additional return values

  • Rename arguments (e.g. this -> self)

  • Prefix procedure names within interfaces with an underscore