Skip to content

Commit 2b0d014

Browse files
committed
[flang] Propogate the BIND(C) attribute into procedures from their interfaces
In "PROCEDURE(iface) :: proc", if "iface" has the BIND(C) attribute, then so should proc, as if the declaration had been "PROCEDURE(iface), BIND(C) :: proc". This had been working in name resolution only in cases where "iface" had been declared before "proc". Note that if "iface" is declared with an empty binding name ("BIND(C,NAME='')"), "proc" does not inherit that property. Use an explicit "BIND(C,NAME='')" on the "PROCEDURE" statement for that. This behavior is not clearly defined in the standard, but seems to match what some other Fortran compilers do.
1 parent 82bd7ad commit 2b0d014

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5068,13 +5068,6 @@ Symbol &DeclarationVisitor::DeclareProcEntity(
50685068
} else if (interface->test(Symbol::Flag::Subroutine)) {
50695069
symbol.set(Symbol::Flag::Subroutine);
50705070
}
5071-
if (IsBindCProcedure(*interface) && !IsPointer(symbol) &&
5072-
!IsDummy(symbol)) {
5073-
// Inherit BIND_C attribute from the interface, but not the NAME="..."
5074-
// if any. This is not clearly described in the standard, but matches
5075-
// the behavior of other compilers.
5076-
SetImplicitAttr(symbol, Attr::BIND_C);
5077-
}
50785071
} else if (auto *type{GetDeclTypeSpec()}) {
50795072
SetType(name, *type);
50805073
symbol.set(Symbol::Flag::Function);
@@ -8631,6 +8624,20 @@ void ResolveNamesVisitor::FinishSpecificationPart(
86318624
if (!symbol.has<HostAssocDetails>()) {
86328625
CheckPossibleBadForwardRef(symbol);
86338626
}
8627+
// Propagate BIND(C) attribute to procedure entities from their interfaces,
8628+
// but not the NAME=, even if it is empty (which would be a reasonable
8629+
// and useful behavior, actually). This interpretation is not at all
8630+
// clearly described in the standard, but matches the behavior of several
8631+
// other compilers.
8632+
if (auto *proc{symbol.detailsIf<ProcEntityDetails>()}; proc &&
8633+
!proc->isDummy() && !IsPointer(symbol) &&
8634+
!symbol.attrs().test(Attr::BIND_C)) {
8635+
if (const Symbol * iface{proc->procInterface()};
8636+
iface && IsBindCProcedure(*iface)) {
8637+
SetImplicitAttr(symbol, Attr::BIND_C);
8638+
SetBindNameOn(symbol);
8639+
}
8640+
}
86348641
}
86358642
currScope().InstantiateDerivedTypes();
86368643
for (const auto &decl : decls) {
@@ -9176,6 +9183,9 @@ void ResolveNamesVisitor::AddSubpNames(ProgramTree &node) {
91769183
if (child.HasModulePrefix()) {
91779184
SetExplicitAttr(symbol, Attr::MODULE);
91789185
}
9186+
if (child.bindingSpec()) {
9187+
SetExplicitAttr(symbol, Attr::BIND_C);
9188+
}
91799189
auto childKind{child.GetKind()};
91809190
if (childKind == ProgramTree::Kind::Function) {
91819191
symbol.set(Symbol::Flag::Function);
@@ -9192,6 +9202,9 @@ void ResolveNamesVisitor::AddSubpNames(ProgramTree &node) {
91929202
if (child.HasModulePrefix()) {
91939203
SetExplicitAttr(symbol, Attr::MODULE);
91949204
}
9205+
if (child.bindingSpec()) {
9206+
SetExplicitAttr(symbol, Attr::BIND_C);
9207+
}
91959208
}
91969209
}
91979210
for (const auto &generic : node.genericSpecs()) {

flang/test/Semantics/bind-c02.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ subroutine proc()
1515
!ERROR: Only variable and named common block can be in BIND statement
1616
bind(c) :: pc1
1717

18+
!ERROR: BIND_C attribute was already specified on 'sub'
1819
!ERROR: Only variable and named common block can be in BIND statement
1920
bind(c) :: sub
2021

flang/test/Semantics/bind-c16.f90

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
!RUN: %flang_fc1 -fdebug-dump-symbols %s 2>&1 | FileCheck %s
2+
!CHECK: p1a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:p1a
3+
!CHECK: p1b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:p1b
4+
!CHECK: p1c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:P1c
5+
!CHECK: p2a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:p2a
6+
!CHECK: p2b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:p2b
7+
!CHECK: p2c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:P2c
8+
!CHECK: p3a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:p3a
9+
!CHECK: p3b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:p3b
10+
!CHECK: p3c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:P3c
11+
module m1
12+
procedure(s1) :: p1a
13+
procedure(s1), bind(c) :: p1b
14+
procedure(s1), bind(c,name='P1c') :: p1c
15+
procedure(s2) :: p2a
16+
procedure(s2), bind(c) :: p2b
17+
procedure(s2), bind(c,name='P2c') :: p2c
18+
procedure(s3) :: p3a
19+
procedure(s3), bind(c) :: p3b
20+
procedure(s3), bind(c,name='P3c') :: p3c
21+
contains
22+
subroutine s1() bind(c)
23+
end
24+
subroutine s2() bind(c,name='')
25+
end
26+
subroutine s3() bind(c,name='foo')
27+
end
28+
end
29+
30+
!CHECK: p1a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:p1a
31+
!CHECK: p1b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:p1b
32+
!CHECK: p1c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:P1c
33+
!CHECK: p2a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:p2a
34+
!CHECK: p2b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:p2b
35+
!CHECK: p2c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:P2c
36+
!CHECK: p3a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:p3a
37+
!CHECK: p3b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:p3b
38+
!CHECK: p3c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:P3c
39+
module m2
40+
interface
41+
subroutine s1() bind(c)
42+
end
43+
subroutine s2() bind(c,name='')
44+
end
45+
subroutine s3() bind(c,name='foo')
46+
end
47+
end interface
48+
procedure(s1) :: p1a
49+
procedure(s1), bind(c) :: p1b
50+
procedure(s1), bind(c,name='P1c') :: p1c
51+
procedure(s2) :: p2a
52+
procedure(s2), bind(c) :: p2b
53+
procedure(s2), bind(c,name='P2c') :: p2c
54+
procedure(s3) :: p3a
55+
procedure(s3), bind(c) :: p3b
56+
procedure(s3), bind(c,name='P3c') :: p3c
57+
end
58+
59+
!CHECK: p1a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:p1a
60+
!CHECK: p1b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:p1b
61+
!CHECK: p1c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s1 bindName:P1c
62+
!CHECK: p2a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:p2a
63+
!CHECK: p2b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:p2b
64+
!CHECK: p2c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s2 bindName:P2c
65+
!CHECK: p3a, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:p3a
66+
!CHECK: p3b, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:p3b
67+
!CHECK: p3c, BIND(C), EXTERNAL, PUBLIC (Subroutine): ProcEntity s3 bindName:P3c
68+
module m3
69+
procedure(s1) :: p1a
70+
procedure(s1), bind(c) :: p1b
71+
procedure(s1), bind(c,name='P1c') :: p1c
72+
procedure(s2) :: p2a
73+
procedure(s2), bind(c) :: p2b
74+
procedure(s2), bind(c,name='P2c') :: p2c
75+
procedure(s3) :: p3a
76+
procedure(s3), bind(c) :: p3b
77+
procedure(s3), bind(c,name='P3c') :: p3c
78+
interface
79+
subroutine s1() bind(c)
80+
end
81+
subroutine s2() bind(c,name='')
82+
end
83+
subroutine s3() bind(c,name='foo')
84+
end
85+
end interface
86+
end

0 commit comments

Comments
 (0)