loki.sourcefile
Contains the declaration of Sourcefile that is used to represent and
manipulate (Fortran) source code files.
Classes
|
Class to handle and manipulate source files, storing |
- class Sourcefile(path, ir=None, ast=None, source=None, incomplete=False, parser_classes=None)
Bases:
objectClass to handle and manipulate source files, storing
ModuleandSubroutineobjects.Reading existing source code from file or string can be done via
from_file()orfrom_source().- Parameters:
path (str) – The name of the source file.
ir (
Section, optional) – The IR of the file content (includingSubroutine,Module,Commentetc.)ast (optional) – Parser-AST of the original source file.
source (
Source, optional) – Raw source string and line information about the original source file.incomplete (bool, optional) – Mark the object as incomplete, i.e. only partially parsed. This is typically the case when it was instantiated using the
Frontend.REGEXfrontend and a full parse using one of the other frontends is pending.parser_classes (
RegexParserClass, optional) – Provide the list of parser classes used during incomplete regex parsing
- classmethod from_file(filename, definitions=None, preprocess=False, includes=None, defines=None, omni_includes=None, xmods=None, frontend=Frontend.FP, parser_classes=None)
Constructor from raw source files that can apply a C-preprocessor before invoking frontend parsers.
- Parameters:
filename (str) – Name of the file to parse into a
Sourcefileobject.definitions (list of
Module, optional) –Moduleobject(s) that may supply external type or procedure definitions.preprocess (bool, optional) –
Flag to trigger CPP preprocessing (by default False).
Attention
Please note that, when using the OMNI frontend, C-preprocessing will always be applied, so
includesanddefinesmay have to be defined even when disablingpreprocess.includes (list of str, optional) – Include paths to pass to the C-preprocessor.
defines (list of str, optional) – Symbol definitions to pass to the C-preprocessor.
xmods (str, optional) – Path to directory to find and store
.xmodfiles when using the OMNI frontend.omni_includes (list of str, optional) – Additional include paths to pass to the preprocessor run as part of the OMNI frontend parse. If set, this replaces (!)
includes, otherwiseomni_includesdefaults to the value ofincludes.frontend (
Frontend, optional) – Frontend to use for producing the AST (defaultFP).
- classmethod from_omni(raw_source, filepath, definitions=None, includes=None, defines=None, xmods=None, omni_includes=None)
Parse a given source string using the OMNI frontend
- Parameters:
raw_source (str) – Fortran source string
filepath (str or
pathlib.Path) – The filepath of this source filedefinitions (list) – List of external
Moduleto provide derived-type and procedure declarationsincludes (list of str, optional) – Include paths to pass to the C-preprocessor.
defines (list of str, optional) – Symbol definitions to pass to the C-preprocessor.
xmods (str, optional) – Path to directory to find and store
.xmodfiles when using the OMNI frontend.omni_includes (list of str, optional) – Additional include paths to pass to the preprocessor run as part of the OMNI frontend parse. If set, this replaces (!)
includes, otherwiseomni_includesdefaults to the value ofincludes.
- classmethod from_ofp(raw_source, filepath, definitions=None)
Parse a given source string using the Open Fortran Parser (OFP) frontend
- Parameters:
raw_source (str) – Fortran source string
filepath (str or
pathlib.Path) – The filepath of this source filedefinitions (list) – List of external
Moduleto provide derived-type and procedure declarations
- classmethod from_fparser(raw_source, filepath, definitions=None)
Parse a given source string using the fparser frontend
- Parameters:
raw_source (str) – Fortran source string
filepath (str or
pathlib.Path) – The filepath of this source filedefinitions (list) – List of external
Moduleto provide derived-type and procedure declarations
- classmethod from_regex(raw_source, filepath, parser_classes=None)
Parse a given source string using the REGEX frontend
- classmethod from_source(source, definitions=None, preprocess=False, includes=None, defines=None, omni_includes=None, xmods=None, frontend=Frontend.FP, parser_classes=None)
Constructor from raw source string that invokes specified frontend parser
- Parameters:
source (str) – Fortran source string
definitions (list of
Module, optional) –Moduleobject(s) that may supply external type or procedure definitions.preprocess (bool, optional) –
Flag to trigger CPP preprocessing (by default False).
Attention
Please note that, when using the OMNI frontend, C-preprocessing will always be applied, so
includesanddefinesmay have to be defined even when disablingpreprocess.includes (list of str, optional) – Include paths to pass to the C-preprocessor.
defines (list of str, optional) – Symbol definitions to pass to the C-preprocessor.
xmods (str, optional) – Path to directory to find and store
.xmodfiles when using the OMNI frontend.omni_includes (list of str, optional) – Additional include paths to pass to the preprocessor run as part of the OMNI frontend parse. If set, this replaces (!)
includes, otherwiseomni_includesdefaults to the value ofincludes.frontend (
Frontend, optional) – Frontend to use for producing the AST (defaultFP).
- make_complete(**frontend_args)
Trigger a re-parse of the source file if incomplete to produce a full Loki IR
If the source file is marked to be incomplete, i.e. when using the lazy constructor option, this triggers a new parsing of all
ProgramUnitobjects and anyRawSourcenodes in theSourcefile.ir.Existing
ModuleandSubroutineobjects continue to exist and references to them stay valid, as they will only be updated instead of replaced.
- property source
- to_fortran(conservative=False, cuf=False)
- property modules
List of
Moduleobjects that are members of thisSourcefile.
- property routines
List of
Subroutineobjects that are members of thisSourcefile.
- property subroutines
List of
Subroutineobjects that are members of thisSourcefile.
- property typedefs
List of
TypeDefobjects that are declared in theModulein thisSourcefile.
- property all_subroutines
- property definitions
List of all definitions made in this sourcefile, i.e. modules and subroutines
- apply(op, **kwargs)
Apply a given transformation to the source file object.
Note that the dispatch routine op.apply(source) will ensure that all entities of this Sourcefile are correctly traversed.
- write(path=None, source=None, conservative=False, cuf=False)
Write content as Fortran source code to file
- Parameters:
path (str, optional) – Filepath of target file; if not provided,
Sourcefile.pathis usedsource (str, optional) – Write the provided string instead of generating via
Sourcefile.to_fortranconservative (bool, optional) – Enable conservative output in the backend, aiming to be as much string-identical as possible (default: False)
cuf (bool, optional) – To use either Cuda Fortran or Fortran backend