Skip to content

Commit 41336f1

Browse files
Add USE association details to COMMON blocks
1 parent 66028ef commit 41336f1

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

flang/include/flang/Semantics/scope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class Scope {
189189
mapType &commonBlocks() { return commonBlocks_; }
190190
const mapType &commonBlocks() const { return commonBlocks_; }
191191
Symbol &MakeCommonBlock(SourceName, SourceName location);
192+
bool AddCommonBlock(const SourceName &name, Symbol &cbSymbol);
192193

193194
/// Find COMMON block in the current scope
194195
Symbol *FindCB(const SourceName &name) const {

flang/lib/Semantics/check-declarations.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2782,7 +2782,10 @@ void CheckHelper::Check(const Scope &scope) {
27822782
Check(*scope.symbol());
27832783
}
27842784
for (const auto &pair : scope.commonBlocks()) {
2785-
CheckCommonBlock(*pair.second);
2785+
if (pair.second->has<CommonBlockDetails>()) {
2786+
// Only process actual COMMON block objects, not their uses
2787+
CheckCommonBlock(*pair.second);
2788+
}
27862789
}
27872790
int mainProgCnt{0};
27882791
for (const Scope &child : scope.children()) {

flang/lib/Semantics/compute-offsets.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ void ComputeOffsetsHelper::Compute(Scope &scope) {
207207
// where COMMON blocks are illegal (C1107 and C1108).
208208
if (scope.kind() != Scope::Kind::BlockConstruct) {
209209
for (auto &pair : scope.commonBlocks()) {
210-
DoCommonBlock(*pair.second);
210+
// Only process actual COMMON block objects, not their uses
211+
if (pair.second->has<CommonBlockDetails>()) {
212+
DoCommonBlock(*pair.second);
213+
}
211214
}
212215
}
213216
for (auto &[symbol, dep] : dependents_) {

flang/lib/Semantics/resolve-names.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,6 +3627,17 @@ void ModuleVisitor::Post(const parser::UseStmt &x) {
36273627
}
36283628
}
36293629
}
3630+
// Go through the list of COMMON block symbols in the module scope and add
3631+
// their USE association to the current scope's COMMON blocks.
3632+
for (const auto &[name, symbol] : useModuleScope_->commonBlocks()) {
3633+
if (auto *localCB{currScope().FindCommonBlockInScopes(name)}; !localCB) {
3634+
// Make a symbol, but don't add it to the Scope, since it needs to
3635+
// be added to the COMMON blocks
3636+
localCB = &currScope().MakeSymbol(
3637+
name, symbol->attrs(), UseDetails{name, symbol->GetUltimate()});
3638+
currScope().AddCommonBlock(name, *localCB);
3639+
}
3640+
}
36303641
useModuleScope_ = nullptr;
36313642
}
36323643

@@ -7284,6 +7295,10 @@ void DeclarationVisitor::CheckCommonBlocks() {
72847295
// check for empty common blocks
72857296
for (const auto &pair : currScope().commonBlocks()) {
72867297
const auto &symbol{*pair.second};
7298+
if (!pair.second->has<CommonBlockDetails>()) {
7299+
// Skip USE associated COMMON blocks
7300+
continue;
7301+
}
72877302
if (symbol.get<CommonBlockDetails>().objects().empty() &&
72887303
symbol.attrs().test(Attr::BIND_C)) {
72897304
Say(symbol.name(),

flang/lib/Semantics/scope.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ void Scope::add_crayPointer(const SourceName &name, Symbol &pointer) {
144144
}
145145

146146
Symbol &Scope::MakeCommonBlock(SourceName name, SourceName location) {
147-
const auto it{commonBlocks_.find(name)};
148-
if (it != commonBlocks_.end()) {
149-
return *it->second;
147+
if (auto *cb{FindCB(name)}) {
148+
return *cb;
150149
} else {
151150
Symbol &symbol{MakeSymbol(
152151
name, Attrs{}, CommonBlockDetails{name.empty() ? location : name})};
@@ -191,6 +190,11 @@ Scope *Scope::FindSubmodule(const SourceName &name) const {
191190
return &*it->second;
192191
}
193192
}
193+
194+
bool Scope::AddCommonBlock(const SourceName &name, Symbol &cbSymbol) {
195+
return commonBlocks_.emplace(name, cbSymbol).second;
196+
}
197+
194198
bool Scope::AddSubmodule(const SourceName &name, Scope &submodule) {
195199
return submodules_.emplace(name, submodule).second;
196200
}

0 commit comments

Comments
 (0)