Skip to content

Commit

Permalink
Transforamtion: Re-instate per-item recursion for complex traversals
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange05 committed Nov 17, 2023
1 parent cd34068 commit d9549a2
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions loki/transform/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,44 @@ def apply_file(self, sourcefile, **kwargs):
if sourcefile._incomplete:
raise RuntimeError('Transformation.apply_file requires Sourcefile to be complete')

item = kwargs.pop('item')
items = kwargs.pop('items')
role = kwargs.pop('role', None)
targets = kwargs.pop('targets', None)

if 'items' in kwargs:
# TODO: This special logic is required for the
# DependencyTransformation to capture certain corner
# cases. Once the module wrapping is split into its
# own transformation, we can probably simplify this.

# We consider the sourcefile to be a "kernel" file if all items are kernels
if all(item.role == 'kernel' for item in items):
role = 'kernel'

if targets is None:
# We collect the targets for file/module-level imports from all items
targets = [target for item in items for target in item.targets]

# Apply file-level transformations
self.transform_file(sourcefile, **kwargs)
self.transform_file(sourcefile, item=item, role=role, targets=targets, **kwargs)

# Recurse to modules, if configured
if self.recurse_to_modules:
for module in sourcefile.modules:
self.apply_module(module, **kwargs)
self.transform_module(module, item=item, role=role, targets=targets, **kwargs)

# Recurse into subroutine, if configured
if self.recurse_to_subroutines:
# Recurse into free subroutines (will skip module-contained ones)
for routine in sourcefile.subroutines:
self.apply_subroutine(routine, **kwargs)
if items:
# Recursion into all subroutine items in the current file
for item in items:
self.transform_subroutine(
item.routine, item=item, role=item.role, targets=item.targets, **kwargs
)
else:
for routine in sourcefile.all_subroutines:
self.transform_subroutine(routine, item=item, role=role, targets=targets, **kwargs)

def apply_subroutine(self, subroutine, **kwargs):
"""
Expand Down

0 comments on commit d9549a2

Please sign in to comment.