Skip to content

Commit

Permalink
depgen.py: not every module is extendable.
Browse files Browse the repository at this point in the history
  • Loading branch information
skosukhin committed May 3, 2024
1 parent cb616d5 commit 41f2470
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
7 changes: 6 additions & 1 deletion mkhelper/depgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def format_debug_line(line, msg):
parser.include_callback = include_callback
parser.module_start_callback = lambda module: provided_modules.add(
module
) or (args.fc_root_smod and provided_submodules.add((module, None)))
)
parser.submodule_start_callback = (
lambda submodule, parent, module: provided_submodules.add(
(module, submodule)
Expand All @@ -547,6 +547,11 @@ def format_debug_line(line, msg):
)
parser.module_use_callback = lambda module: required_modules.add(module)

if args.fc_root_smod:
parser.extendable_module_callback = (
lambda module: provided_submodules.add((module, None))
)

if args.debug:
parser.debug_callback = lambda line, msg: ftn_debug_info.append(
format_debug_line(line, msg)
Expand Down
43 changes: 42 additions & 1 deletion mkhelper/depgen/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class Parser:
r"^\s*use(?:\s+|(?:\s*,\s*((?:non_)?intrinsic))?\s*::\s*)(\w+)", re.I
)
_re_module_end = re.compile(r"^\s*end\s+module(?:\s+(\w+))?\s*$", re.I)
_re_module_prefixed_procedure = re.compile(
r"^\s*(?:\w+(?:\(.*\))?\s+)*"
r"module\s+"
r"(?:\w+(?:\(.*\))?\s+)*(?:function|subroutine)",
re.I,
)

def __init__(
self,
Expand Down Expand Up @@ -84,6 +90,7 @@ def __init__(
self.module_start_callback = None
self.submodule_start_callback = None
self.module_use_callback = None
self.extendable_module_callback = None
self.debug_callback = None

self._include_finder = IncludeFinder(include_order, include_dirs)
Expand All @@ -95,6 +102,7 @@ def parse(self, stream, stream_name):
include_stack.push(stream, stream_name)

current_module = None
current_module_is_extendable = False

for line in Parser.streamline_input(include_stack):
# module definition start
Expand Down Expand Up @@ -213,7 +221,39 @@ def parse(self, stream, stream_name):
self.debug_callback(line, "ignored (file not found)")
continue

if self.debug_callback is None:
if not (self.extendable_module_callback or self.debug_callback):
continue

# procedure with the module prefix
match = Parser._re_module_prefixed_procedure.match(line)
if match:
if current_module:
if current_module_is_extendable:
if self.debug_callback:
self.debug_callback(
line,
"ignored module subroutine/function "
"(module '{0}' is already known "
"to be extendable)".format(current_module),
)
else:
current_module_is_extendable = True
if self.extendable_module_callback:
self.extendable_module_callback(current_module)
if self.debug_callback:
self.debug_callback(
line,
"module subroutine/function "
"(module '{0}' is extendable)".format(
current_module
),
)
elif self.debug_callback:
self.debug_callback(
line,
"ignored module subroutine/function "
"(not in the module scope)".format(current_module),
)
continue

# module definition end
Expand Down Expand Up @@ -245,6 +285,7 @@ def parse(self, stream, stream_name):
),
)
current_module = None
current_module_is_extendable = False
continue

include_stack.clear()
Expand Down

0 comments on commit 41f2470

Please sign in to comment.