Skip to content

Commit 74de636

Browse files
Merge branch '3-acc-common' into 2-acc-common
2 parents adcd69b + 63662d8 commit 74de636

File tree

8 files changed

+93
-22
lines changed

8 files changed

+93
-22
lines changed

flang/include/flang/Semantics/scope.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,23 @@ 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);
193+
194+
/// Find COMMON block in the current scope
195+
Symbol *FindCB(const SourceName &name) const {
196+
if (const auto it{commonBlocks_.find(name)}; it != commonBlocks_.end()) {
197+
return &*it->second;
198+
}
199+
return nullptr;
200+
}
201+
202+
/// Find COMMON block that is not USE-associated in the current scope
192203
Symbol *FindCommonBlock(const SourceName &) const;
193204

205+
/// Find COMMON block in current and surrounding scopes, follow USE
206+
/// associations
207+
Symbol *FindCommonBlockInScopes(const SourceName &) const;
208+
194209
/// Make a Symbol but don't add it to the scope.
195210
template <typename D>
196211
common::IfNoLvalue<Symbol &, D> MakeSymbol(

flang/include/flang/Semantics/symbol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ class Symbol {
884884
const Details &details() const { return details_; }
885885
// Assign the details of the symbol from one of the variants.
886886
// Only allowed in certain cases.
887-
void set_details(Details &&);
887+
void set_details(Details &&, bool force = false);
888888

889889
// Can the details of this symbol be replaced with the given details?
890890
bool CanReplaceDetails(const Details &details) 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-directives.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,21 +1698,10 @@ Symbol *AccAttributeVisitor::ResolveAccCommonBlockName(
16981698
if (!name) {
16991699
return nullptr;
17001700
}
1701-
// Check the local and surrounding scopes first
1702-
if (auto *cb{GetProgramUnitOrBlockConstructContaining(GetContext().scope)
1703-
.FindCommonBlock(name->source)}) {
1701+
if (auto *cb{GetContext().scope.FindCommonBlockInScopes(name->source)}) {
17041702
name->symbol = cb;
17051703
return cb;
17061704
}
1707-
// Look for COMMON block in the modules
1708-
for (const Scope &childScope : context_.globalScope().children()) {
1709-
if (childScope.kind() == Scope::Kind::Module) {
1710-
if (auto *cb{childScope.FindCommonBlock(name->source)}) {
1711-
name->symbol = cb;
1712-
return cb;
1713-
}
1714-
}
1715-
}
17161705
return nullptr;
17171706
}
17181707

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: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,58 @@ 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+
if (cb->has<UseDetails>()) {
149+
// COMMON blocks could be re-declared. Example:
150+
// module test
151+
// integer :: a
152+
// common /blk/ a
153+
// end module test
154+
// program main
155+
// use test ! Initially get /blk/ with UseDetails
156+
// integer :: a1
157+
// common /blk/ a1 ! Update with CommonBlockDetails
158+
// end program main
159+
// Reset details with real COMMON block details.
160+
cb->set_details(CommonBlockDetails{name.empty() ? location : name},
161+
/*force*/ true);
162+
}
163+
return *cb;
150164
} else {
151165
Symbol &symbol{MakeSymbol(
152166
name, Attrs{}, CommonBlockDetails{name.empty() ? location : name})};
153167
commonBlocks_.emplace(name, symbol);
154168
return symbol;
155169
}
156170
}
171+
157172
Symbol *Scope::FindCommonBlock(const SourceName &name) const {
158-
const auto it{commonBlocks_.find(name)};
159-
return it != commonBlocks_.end() ? &*it->second : nullptr;
173+
if (auto *cb{FindCB(name)}) {
174+
// Ensure this COMMON block is not USE-associated
175+
if (cb->has<UseDetails>()) {
176+
cb = nullptr;
177+
}
178+
return cb;
179+
}
180+
return nullptr;
181+
}
182+
183+
Symbol *Scope::FindCommonBlockInScopes(const SourceName &name) const {
184+
if (auto *cb{FindCB(name)}) {
185+
return &cb->GetUltimate();
186+
} else if (IsSubmodule()) {
187+
if (const Scope *parent{
188+
symbol_ ? symbol_->get<ModuleDetails>().parent() : nullptr}) {
189+
if (auto *cb{parent->FindCommonBlockInScopes(name)}) {
190+
return &cb->GetUltimate();
191+
}
192+
}
193+
} else if (!IsTopLevel() && parent_) {
194+
if (auto *cb{parent_->FindCommonBlockInScopes(name)}) {
195+
return &cb->GetUltimate();
196+
}
197+
}
198+
return nullptr;
160199
}
161200

162201
Scope *Scope::FindSubmodule(const SourceName &name) const {
@@ -167,6 +206,11 @@ Scope *Scope::FindSubmodule(const SourceName &name) const {
167206
return &*it->second;
168207
}
169208
}
209+
210+
bool Scope::AddCommonBlock(const SourceName &name, Symbol &cbSymbol) {
211+
return commonBlocks_.emplace(name, cbSymbol).second;
212+
}
213+
170214
bool Scope::AddSubmodule(const SourceName &name, Scope &submodule) {
171215
return submodules_.emplace(name, submodule).second;
172216
}

flang/lib/Semantics/symbol.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ std::string DetailsToString(const Details &details) {
317317

318318
std::string Symbol::GetDetailsName() const { return DetailsToString(details_); }
319319

320-
void Symbol::set_details(Details &&details) {
321-
CHECK(CanReplaceDetails(details));
320+
void Symbol::set_details(Details &&details, bool force) {
321+
if (!force) {
322+
CHECK(CanReplaceDetails(details));
323+
}
322324
details_ = std::move(details);
323325
}
324326

0 commit comments

Comments
 (0)