Skip to content

Commit 1f525ec

Browse files
committed
[flang][NFC] Add GetTopLevelUnitContaining functions
`GetTopLevelUnitContaining` returns the Scope nested in the global scope that contains the given Scope or Symbol. Use "Get" rather than "Find" in the name because "Find" implies it might not be found, which can't happen. Following that logic, rename `FindProgramUnitContaining` to `GetProgramUnitContaining` and have it also return a reference rather that a pointer. Note that the use of "ProgramUnit" is slightly confusing. In the Fortran standard, "program-unit" refers to what is called a "TopLevelUnit" here. What we are calling a "ProgramUnit" (here and in `ProgramTree`) includes internal subprograms while "TopLevelUnit" does not. Differential Revision: https://reviews.llvm.org/D92491
1 parent 291cc1b commit 1f525ec

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

flang/include/flang/Semantics/tools.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ class DerivedTypeSpec;
3030
class Scope;
3131
class Symbol;
3232

33+
// Note: Here ProgramUnit includes internal subprograms while TopLevelUnit
34+
// does not. "program-unit" in the Fortran standard matches TopLevelUnit.
35+
const Scope &GetTopLevelUnitContaining(const Scope &);
36+
const Scope &GetTopLevelUnitContaining(const Symbol &);
37+
const Scope &GetProgramUnitContaining(const Scope &);
38+
const Scope &GetProgramUnitContaining(const Symbol &);
39+
3340
const Scope *FindModuleContaining(const Scope &);
34-
const Scope *FindProgramUnitContaining(const Scope &);
35-
const Scope *FindProgramUnitContaining(const Symbol &);
3641
const Scope *FindPureProcedureContaining(const Scope &);
3742
const Scope *FindPureProcedureContaining(const Symbol &);
3843
const Symbol *FindPointerComponent(const Scope &);

flang/lib/Semantics/check-return.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
namespace Fortran::semantics {
1717

1818
static const Scope *FindContainingSubprogram(const Scope &start) {
19-
const Scope *scope{FindProgramUnitContaining(start)};
20-
return scope &&
21-
(scope->kind() == Scope::Kind::MainProgram ||
22-
scope->kind() == Scope::Kind::Subprogram)
23-
? scope
19+
const Scope &scope{GetProgramUnitContaining(start)};
20+
return scope.kind() == Scope::Kind::MainProgram ||
21+
scope.kind() == Scope::Kind::Subprogram
22+
? &scope
2423
: nullptr;
2524
}
2625

flang/lib/Semantics/resolve-names.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5602,11 +5602,11 @@ bool DeclarationVisitor::CheckForHostAssociatedImplicit(
56025602
}
56035603

56045604
bool DeclarationVisitor::IsUplevelReference(const Symbol &symbol) {
5605-
const Scope *symbolUnit{FindProgramUnitContaining(symbol)};
5606-
if (symbolUnit == FindProgramUnitContaining(currScope())) {
5605+
const Scope &symbolUnit{GetProgramUnitContaining(symbol)};
5606+
if (symbolUnit == GetProgramUnitContaining(currScope())) {
56075607
return false;
56085608
} else {
5609-
Scope::Kind kind{DEREF(symbolUnit).kind()};
5609+
Scope::Kind kind{symbolUnit.kind()};
56105610
return kind == Scope::Kind::Subprogram || kind == Scope::Kind::MainProgram;
56115611
}
56125612
}

flang/lib/Semantics/tools.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,24 @@ static const Scope *FindScopeContaining(
3737
}
3838
}
3939

40+
const Scope &GetTopLevelUnitContaining(const Scope &start) {
41+
CHECK(!start.IsGlobal());
42+
return DEREF(FindScopeContaining(
43+
start, [](const Scope &scope) { return scope.parent().IsGlobal(); }));
44+
}
45+
46+
const Scope &GetTopLevelUnitContaining(const Symbol &symbol) {
47+
return GetTopLevelUnitContaining(symbol.owner());
48+
}
49+
4050
const Scope *FindModuleContaining(const Scope &start) {
4151
return FindScopeContaining(
4252
start, [](const Scope &scope) { return scope.IsModule(); });
4353
}
4454

45-
const Scope *FindProgramUnitContaining(const Scope &start) {
46-
return FindScopeContaining(start, [](const Scope &scope) {
55+
const Scope &GetProgramUnitContaining(const Scope &start) {
56+
CHECK(!start.IsGlobal());
57+
return DEREF(FindScopeContaining(start, [](const Scope &scope) {
4758
switch (scope.kind()) {
4859
case Scope::Kind::Module:
4960
case Scope::Kind::MainProgram:
@@ -53,23 +64,19 @@ const Scope *FindProgramUnitContaining(const Scope &start) {
5364
default:
5465
return false;
5566
}
56-
});
67+
}));
5768
}
5869

59-
const Scope *FindProgramUnitContaining(const Symbol &symbol) {
60-
return FindProgramUnitContaining(symbol.owner());
70+
const Scope &GetProgramUnitContaining(const Symbol &symbol) {
71+
return GetProgramUnitContaining(symbol.owner());
6172
}
6273

6374
const Scope *FindPureProcedureContaining(const Scope &start) {
6475
// N.B. We only need to examine the innermost containing program unit
6576
// because an internal subprogram of a pure subprogram must also
6677
// be pure (C1592).
67-
if (const Scope * scope{FindProgramUnitContaining(start)}) {
68-
if (IsPureProcedure(*scope)) {
69-
return scope;
70-
}
71-
}
72-
return nullptr;
78+
const Scope &scope{GetProgramUnitContaining(start)};
79+
return IsPureProcedure(scope) ? &scope : nullptr;
7380
}
7481

7582
Tristate IsDefinedAssignment(
@@ -176,9 +183,9 @@ bool IsCommonBlockContaining(const Symbol &block, const Symbol &object) {
176183
}
177184

178185
bool IsUseAssociated(const Symbol &symbol, const Scope &scope) {
179-
const Scope *owner{FindProgramUnitContaining(symbol.GetUltimate().owner())};
180-
return owner && owner->kind() == Scope::Kind::Module &&
181-
owner != FindProgramUnitContaining(scope);
186+
const Scope &owner{GetProgramUnitContaining(symbol.GetUltimate().owner())};
187+
return owner.kind() == Scope::Kind::Module &&
188+
owner != GetProgramUnitContaining(scope);
182189
}
183190

184191
bool DoesScopeContain(
@@ -203,10 +210,9 @@ static const Symbol &FollowHostAssoc(const Symbol &symbol) {
203210
}
204211

205212
bool IsHostAssociated(const Symbol &symbol, const Scope &scope) {
206-
const Scope *subprogram{FindProgramUnitContaining(scope)};
207-
return subprogram &&
208-
DoesScopeContain(
209-
FindProgramUnitContaining(FollowHostAssoc(symbol)), *subprogram);
213+
const Scope &subprogram{GetProgramUnitContaining(scope)};
214+
return DoesScopeContain(
215+
&GetProgramUnitContaining(FollowHostAssoc(symbol)), subprogram);
210216
}
211217

212218
bool IsInStmtFunction(const Symbol &symbol) {

0 commit comments

Comments
 (0)