Merge branch 'features/fc_submodules' into 'master'

Support for Fortran 2008 submodules.

See merge request ita1024/waf!2205
This commit is contained in:
ita1024 2018-12-22 00:14:42 +00:00
commit a77deaebdb
8 changed files with 141 additions and 5 deletions

View File

@ -0,0 +1,27 @@
submodule (parent) container
implicit none
contains
module procedure init
p%mother = mother
p%father = father
end procedure init
module subroutine harmonize(p)
type(parent_type), intent(inout) :: p
real :: avg
avg = 0.5 * (p%father + p%mother)
p%father = avg
p%mother = avg
end subroutine harmonize
module function parent_weight(p) result(w)
type(parent_type), intent(in) :: p
real :: w
w = p%mother**2 + p%father**2
end function parent_weight
end submodule container

View File

@ -0,0 +1,13 @@
submodule (parent:container) helper
implicit none
contains
module function parent_distance(pa, pb) result(dist)
type(parent_type), intent(in) :: pa, pb
real :: dist
dist = sqrt(parent_weight(pa) + parent_weight(pb))
end function parent_distance
end submodule helper

View File

@ -0,0 +1,30 @@
module parent
implicit none
type parent_type
real :: mother
real :: father
end type parent_type
interface
module subroutine init(p, mother, father)
type(parent_type), intent(out) :: p
real, intent(in) :: mother, father
end subroutine init
module subroutine harmonize(p)
type(parent_type), intent(inout) :: p
end subroutine harmonize
module function parent_weight(p) result(w)
type(parent_type), intent(in) :: p
real :: w
end function parent_weight
module function parent_distance(pa, pb) result(dist)
type(parent_type), intent(in) :: pa, pb
real :: dist
end function parent_distance
end interface
end module parent

View File

@ -0,0 +1,18 @@
program submain
use parent
implicit none
type(parent_type) :: a,b
real :: dist, weight
call init(a, 1.0, 2.0)
call init(b, 10.0, 12.0)
call harmonize(a)
weight = parent_weight(b)
write(*,*) weight
dist = parent_distance(a, b)
write(*,*) dist
end program submain

View File

@ -0,0 +1,26 @@
#! /usr/bin/env python
top = '.'
out = 'build'
def options(opt):
opt.load('compiler_c')
opt.load('compiler_fc')
def configure(conf):
conf.load('compiler_c')
conf.load('compiler_fc')
def build(bld):
bld(
features = 'fc fcshlib',
source = 'parent.f90 container.f90 helper.f90',
target = 'fudge',
)
bld(
features = 'fc fcprogram',
source = 'submain.f90',
use = 'fudge',
target = 'submain',
)

View File

@ -78,3 +78,5 @@ def build(bld):
target = 'mod/two_mods')
bld.recurse('typemap')
if bld.env.FC_NAME == 'GFORTRAN' and int(bld.env.FC_VERSION[0]) >= 6:
bld.recurse('submodules')

View File

@ -28,10 +28,24 @@ def modfile(conf, name):
Turns a module name into the right module file name.
Defaults to all lower case.
"""
return {'lower' :name.lower() + '.mod',
'lower.MOD' :name.lower() + '.MOD',
'UPPER.mod' :name.upper() + '.mod',
'UPPER' :name.upper() + '.MOD'}[conf.env.FC_MOD_CAPITALIZATION or 'lower']
if name.find(':') >= 0:
# Depending on a submodule!
separator = conf.env.FC_SUBMOD_SEPARATOR or '@'
# Ancestors of the submodule will be prefixed to the
# submodule name, separated by a colon.
modpath = name.split(':')
# Only the ancestor (actual) module and the submodule name
# will be used for the filename.
modname = modpath[0] + separator + modpath[-1]
suffix = conf.env.FC_SUBMOD_SUFFIX or '.smod'
else:
modname = name
suffix = '.mod'
return {'lower' :modname.lower() + suffix.lower(),
'lower.MOD' :modname.lower() + suffix.upper(),
'UPPER.mod' :modname.upper() + suffix.lower(),
'UPPER' :modname.upper() + suffix.upper()}[conf.env.FC_MOD_CAPITALIZATION or 'lower']
def get_fortran_tasks(tsk):
"""

View File

@ -7,11 +7,13 @@ import re
INC_REGEX = """(?:^|['">]\s*;)\s*(?:|#\s*)INCLUDE\s+(?:\w+_)?[<"'](.+?)(?=["'>])"""
USE_REGEX = """(?:^|;)\s*USE(?:\s+|(?:(?:\s*,\s*(?:NON_)?INTRINSIC)?\s*::))\s*(\w+)"""
MOD_REGEX = """(?:^|;)\s*MODULE(?!\s*PROCEDURE)(?:\s+|(?:(?:\s*,\s*(?:NON_)?INTRINSIC)?\s*::))\s*(\w+)"""
MOD_REGEX = """(?:^|;)\s*MODULE(?!\s+(?:PROCEDURE|SUBROUTINE|FUNCTION))\s+(\w+)"""
SMD_REGEX = """(?:^|;)\s*SUBMODULE\s*\(([\w:]+)\)\s*(\w+)"""
re_inc = re.compile(INC_REGEX, re.I)
re_use = re.compile(USE_REGEX, re.I)
re_mod = re.compile(MOD_REGEX, re.I)
re_smd = re.compile(SMD_REGEX, re.I)
class fortran_parser(object):
"""
@ -58,6 +60,10 @@ class fortran_parser(object):
m = re_mod.search(line)
if m:
mods.append(m.group(1))
m = re_smd.search(line)
if m:
uses.append(m.group(1))
mods.append('{0}:{1}'.format(m.group(1),m.group(2)))
return (incs, uses, mods)
def start(self, node):