loki.batch.transformation
Base class definition for Transformation pipelines.
Classes
Base class for source code transformations that manipulate source items like |
- class Transformation
Bases:
objectBase class for source code transformations that manipulate source items like
SubroutineorModulein place via theitem.apply(transform)method.The source transformations to be applied should be defined in the following class-specific methods:
The generic dispatch mechanism behind the
apply()method will ensure that all hierarchies of the data model are traversed and the specific method for each level is applied, if the relevant recursion mode is enabled in the transformation’s manifest (recurse_to_modulesand/orrecurse_to_procedures). Note that inSourcefileobjects,Modulemembers will be traversed before standaloneSubroutineobjects.Classes inheriting from
Transformationmay configure the invocation and behaviour during batch processing via a predefined set of class attributes. These flags determine the underlying graph traversal when processing complex call trees and determine how the transformations are invoked for a given type ofItemin theScheduler.- reverse_traversal
Forces scheduler traversal in reverse order from the leaf nodes upwards (default:
False).- Type:
- traverse_file_graph
Apply
Transformationto theSourcefileobject corresponding to theItembeing processed, instead of the program unit in question (default:False).- Type:
- item_filter
Filter by graph node types to prune the graph and change connectivity. By default, only calls to
Subroutineitems are used to construct the graph.- Type:
- recurse_to_modules
Apply transformation to all
Moduleobjects when processing aSourcefile(defaultFalse)- Type:
- recurse_to_procedures
Apply transformation to all
Subroutineobjects when processingSourcefileorModuleobjects (defaultFalse)- Type:
- recurse_to_internal_procedures
Apply transformation to all internal
Subroutineobjects when processingSubroutineobjects (defaultFalse)- Type:
- process_ignored_items
Apply transformation to “ignored”
Itemobjects for analysis. This might be needed if IPO-information needs to be passed across library boundaries.- Type:
- renames_items
Indicates to the
Schedulerthat a transformation may change the name of the IR node corresponding to the processedItem(e.g., by renaming a module or subroutine). The transformation has to take care of renaming processed theItemitself but theSchedulerwill update its internal cache after the transformation has been applied (defaultFalse).- Type:
- creates_items
Indicates to the
Schedulerthat a transformation may create new scopes or other dependency nodes (e.g., by adding new routines to a module). The scheduler will run a discovery step after the transformation has been applied to include these new items in the dependency graph (defaultFalse).- Type:
- reverse_traversal = False
- traverse_file_graph = False
- item_filter
alias of
ProcedureItem
- recurse_to_modules = False
- recurse_to_procedures = False
- recurse_to_internal_procedures = False
- process_ignored_items = False
- renames_items = False
- creates_items = False
- transform_subroutine(routine, **kwargs)
Defines the transformation to apply to
Subroutineitems.For transformations that modify
Subroutineobjects, this method should be implemented. It gets called via the dispatch methodapply().- Parameters:
routine (
Subroutine) – The subroutine to be transformed.**kwargs (optional) – Keyword arguments for the transformation.
- transform_module(module, **kwargs)
Defines the transformation to apply to
Moduleitems.For transformations that modify
Moduleobjects, this method should be implemented. It gets called via the dispatch methodapply().- Parameters:
module (
Module) – The module to be transformed.**kwargs (optional) – Keyword arguments for the transformation.
- transform_file(sourcefile, **kwargs)
Defines the transformation to apply to
Sourcefileitems.For transformations that modify
Sourcefileobjects, this method should be implemented. It gets called via the dispatch methodapply().- Parameters:
sourcefile (
Sourcefile) – The sourcefile to be transformed.**kwargs (optional) – Keyword arguments for the transformation.
- apply(source, post_apply_rescope_symbols=False, **kwargs)
Dispatch method to apply transformation to
source.It dispatches to one of the type-specific dispatch methods
apply_file(),apply_module(), orapply_subroutine().- Parameters:
source (
SourcefileorModuleorSubroutine) – The source item to transform.post_apply_rescope_symbols (bool, optional) – Call
rescope_symbolsonsourceafter applying the transformation to clean up any scoping issues.**kwargs (optional) – Keyword arguments that are passed on to the methods defining the actual transformation.
- apply_file(sourcefile, **kwargs)
Apply transformation to all items in
sourcefile.This calls
transform_file().If the
recurse_to_modulesclass property is set, it will also invokeapply()on allModuleobjects in thisSourcefile. Likewise, ifrecurse_to_proceduresis set, it will invokeapply()on all freeSubroutineobjects in thisSourcefile.- Parameters:
sourcefile (
Sourcefile) – The file to transform.**kwargs (optional) – Keyword arguments that are passed on to transformation methods.
- apply_subroutine(subroutine, **kwargs)
Apply transformation to a given
Subroutineobject and its members.This calls
transform_subroutine().If the
recurse_to_member_proceduresclass property is set, it will also invokeapply()on allSubroutineobjects in thecontainsclause of thisSubroutine.- Parameters:
subroutine (
Subroutine) – The subroutine to transform.**kwargs (optional) – Keyword arguments that are passed on to transformation methods.
- apply_module(module, **kwargs)
Apply transformation to a given
Moduleobject and its members.This calls
transform_module().If the
recurse_to_proceduresclass property is set, it will also invokeapply()on allSubroutineobjects in thecontainsclause of thisModule.- Parameters:
module (
Module) – The module to transform.**kwargs (optional) – Keyword arguments that are passed on to transformation methods.
- post_apply(source, rescope_symbols=False)
Dispatch method for actions to be carried out after applying a transformation to
source.It dispatches to one of the type-specific dispatch methods
post_apply_file(),post_apply_module(), orpost_apply_subroutine().- Parameters:
source (
SourcefileorModuleorSubroutine) – The source item to transform.rescope_symbols (bool, optional) – Call
rescope_symbolsonsource
- post_apply_file(sourcefile, rescope_symbols)
Apply actions after applying a transformation to
sourcefile.- Parameters:
sourcefile (
Sourcefile) – The file to transform.rescope_symbols (bool) – Call
rescope_symbolson modules and subroutines insourcefile
- post_apply_subroutine(subroutine, rescope_symbols)
Apply actions after applying a transformation to
subroutine.- Parameters:
subroutine (
Subroutine) – The file to transform.rescope_symbols (bool) – Call
rescope_symbolsonsubroutine