Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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
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)