How to use the f90wrap.fortran.Type function in f90wrap

To help you get started, we’ve selected a few f90wrap examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jameskermode / f90wrap / f90wrap / transform.py View on Github external
(mod.name, i) for (i, mod) in enumerate(tree.modules))  # name to index map, compatible python 2.6
    containers = []
    for ty in types.values():
        for dimensions_attribute in ty.super_types_dimensions:
            # each type might have many "dimension" attributes since "append_type_dimension"
            dimensions = ArrayDimensionConverter.split_dimensions(dimensions_attribute)
            if len(dimensions) == 1:  # at this point, only 1D arrays are supported
                d = dimensions[0]
                if str(d) == ':':
                    continue  # at this point, only fixed-length arrays are supported
                # populate the super type with the array of types
                el = ft.Element(name='items', attributes=[dimensions_attribute], type='type(' + ty.name + ')')
                name = ty.name + '_x' + str(d) + '_array'
                # populate the tree with the super-type
                if name not in (t.name for t in tree.modules[modules_indexes[ty.mod_name]].types):
                    super_type = ft.Type(name=name, filename=ty.filename, lineno=ty.lineno,
                                         doc=['super-type',
                                              'Automatically generated to handle derived type arrays as a new derived type'],
                                         elements=[el], mod_name=ty.mod_name)
                    # uses clauses from the base type
                    # super_type.uses = ty.uses  # this causes unwanted growth of the normal type "uses" when we add parameters to the super-type in the next step
                    super_type.uses = set([(ty.mod_name, (ty.name,))])
                    # uses clause if the dimension is a parameter (which is handled through a n=shape(array) hidden argument in the case of regular arrays)
                    param = extract_dimensions_parameters(d, tree)
                    if param:
                        super_type.uses.add((param[0], (param[1],)))

                    tree.modules[modules_indexes[ty.mod_name]].types.append(super_type)
                    containers.append(tree.modules[modules_indexes[ty.mod_name]].types[-1])

    for ty in containers:
        types['type(%s)' % ty.name] = types[ty.name] = ty
github jameskermode / f90wrap / f90wrap / fortran.py View on Github external
def find_types(tree):
    """
    Walk over all the nodes in tree, building up a dictionary:
      types: maps type names to Type instances

    Returns a pair (types, types_to_mod_names)
    """
    types = {}

    for mod in walk_modules(tree):
        for node in walk(mod):
            if isinstance(node, Type):
                logging.debug('type %s defined in module %s' % (node.name, mod.name))
                node.mod_name = mod.name  # save module name in Type instance
                node.uses = set([(mod.name, (node.name,))])
                types['type(%s)' % node.name] = types[node.name] = node

    return types
github jameskermode / f90wrap / f90wrap / f90wrapgen.py View on Github external
if isinstance(t, ft.Type):
            self.write_type_lines(t.name)
        self.write_type_lines(el.type)

        self.write('integer, intent(in) :: %s(%d)' % (this, sizeof_fortran_t))
        if isinstance(t, ft.Type):
            self.write('type(%s_ptr_type) :: this_ptr' % t.name)
            array_name = 'this_ptr%%p%%%s' % el.name
        else:
            array_name = '%s_%s' % (t.name, el.name)
        self.write('integer, intent(in) :: %s' % (safe_i))
        self.write('integer, intent(%s) :: %s(%d)' % (inout, el.name + 'item', sizeof_fortran_t))
        self.write('type(%s_ptr_type) :: %s_ptr' % (ft.strip_type(el.type), el.name))
        self.write()
        if isinstance(t, ft.Type):
            self.write('this_ptr = transfer(%s, this_ptr)' % (this))

        if 'allocatable' in el.attributes:
            self.write('if (allocated(%s)) then' % array_name)
            self.indent()

        self.write('if (%s < 1 .or. %s > size(%s)) then' % (safe_i, safe_i, array_name))
        self.indent()
        self.write('call %s("array index out of range")' % self.abort_func)
        self.dedent()
        self.write('else')
        self.indent()

        if getset == "get":
            self.write('%s_ptr%%p => %s(%s)' % (el.name, array_name, safe_i))
            self.write('%s = transfer(%s_ptr,%s)' % (el.name + 'item', el.name, el.name + 'item'))
github jameskermode / f90wrap / f90wrap / pywrapgen.py View on Github external
dct = dict(el_name=el.name,
                   el_orig_name=el.orig_name,
                   el_name_get=el.name,
                   el_name_set=el.name,
                   mod_name=self.f90_mod_name,
                   prefix=self.prefix, type_name=node.name,
                   self='self',
                   selfdot='self.',
                   selfcomma='self, ',
                   handle=isinstance(node, ft.Type) and 'self._handle' or '')

        if hasattr(el, 'py_name'):
            dct['el_name_get'] = el.py_name
            dct['el_name_set'] = el.py_name

        if isinstance(node, ft.Type):
            dct['set_args'] = '%(handle)s, %(el_name_get)s' % dct
        else:
            dct['set_args'] = '%(el_name_get)s' % dct

        if not isinstance(node, ft.Module) or not self.make_package:
            self.write('@property')
            properties.append(el)
        else:
            dct['el_name_get'] = 'get_' + el.name
            dct['el_name_set'] = 'set_' + el.name
            dct['self'] = ''
            dct['selfdot'] = ''
            dct['selfcomma'] = ''

        # check for name clashes with pre-existing routines
        if hasattr(node, 'procedures'):
github jameskermode / f90wrap / f90wrap / transform.py View on Github external
def add_missing_constructors(tree):
    for node in ft.walk(tree):
        if not isinstance(node, ft.Type):
            continue
        for child in ft.iter_child_nodes(node):
            if isinstance(child, ft.Procedure):
                if 'constructor' in child.attributes:
                    logging.info('found constructor %s' % child.name)
                    break
        else:

            logging.info('adding missing constructor for %s' % node.name)
            new_node = ft.Subroutine('%s_initialise' % node.name,
                                     node.filename,
                                     ['Automatically generated constructor for %s' % node.name],
                                     node.lineno,
                                     [ft.Argument(name='this',  # self.prefix + 'this' would probably be safer
                                                  filename=node.filename,
                                                  doc=['Object to be constructed'],