def find_referenced_types(mods, tree): """ 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 : the full fortran parse tree from which the mods have been taken. tree : `fortran.Root` object. the full fortran parse tree from which the mods have been taken. Returns ------- kept_types : set of Type() objects which are referenced or defined in the modules given, or recursively referenced by those types. """ # Get used types now kept_types = set() for mod in mods: for t in mod.types: kept_types.add(t) for el in mod.elements: if el.type.startswith('type'): for mod2 in ft.walk_modules(tree): for mt in mod2.types: if mt.name in el.type: kept_types.add(mt) # kept_types is now all types defined/referenced directly in kept_mods. But we also # need those referenced by them. new_set = copy.copy(kept_types) while new_set != set(): temp_set = list(new_set) for t in temp_set: for el in t.elements: if el.type.startswith( 'type'): # a referenced type, need to find def for mod2 in ft.walk_modules(tree): for mt in mod2.types: if mt.name in el.type: new_set.add(mt) # take out all the original types from new_set new_set -= kept_types # update the kept_types with new ones kept_types |= new_set return kept_types
def find_referenced_types(mods, tree): """ 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 : the full fortran parse tree from which the mods have been taken. tree : `fortran.Root` object. the full fortran parse tree from which the mods have been taken. Returns ------- kept_types : set of Type() objects which are referenced or defined in the modules given, or recursively referenced by those types. """ # Get used types now kept_types = set() for mod in mods: for t in mod.types: kept_types.add(t) for el in mod.elements: if el.type.startswith('type'): for mod2 in ft.walk_modules(tree): for mt in mod2.types: if mt.name in el.type: kept_types.add(mt) # kept_types is now all types defined/referenced directly in kept_mods. But we also # need those referenced by them. new_set = copy.copy(kept_types) while new_set != set(): temp_set = list(new_set) for t in temp_set: for el in t.elements: if el.type.startswith('type'): # a referenced type, need to find def for mod2 in ft.walk_modules(tree): for mt in mod2.types: if mt.name in el.type: new_set.add(mt) # take out all the original types from new_set new_set -= kept_types # update the kept_types with new ones kept_types |= new_set return kept_types
def find_referenced_modules(mods, tree): """ 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 : set Module() objects which are recursively used by the given modules. """ new_mods = copy.copy(mods) while new_mods != set(): temp = list(new_mods) for m in temp: for m2 in m.uses: for m3 in ft.walk_modules(tree): if m3.name == m2: new_mods.add(m3) new_mods -= mods mods |= new_mods return mods
def fix_element_uses_clauses(tree, types): """ Add uses clauses to derived type elements in modules """ for mod in ft.walk_modules(tree): for el in mod.elements: el.uses = set() if el.type.startswith('type') and ft.strip_type(el.type) in types: el.uses.add((types[el.type].mod_name, (ft.strip_type(el.type),))) return tree
def fix_element_uses_clauses(tree, types): """ Add uses clauses to derived type elements in modules """ for mod in ft.walk_modules(tree): for el in mod.elements: el.uses = set() if el.type.startswith('type') and el.type in types: el.uses.add( (types[el.type].mod_name, (ft.strip_type(el.type), ))) return tree