Skip to content

Commit d5c05ce

Browse files
committed
[flang][NFC] Add accessors to equivalence and common blocks
Add a way to get mutable equivalence sets to Scope so that they can have sizes and offsets assigned to them. Change CommonBlockDetails to have mutable symbols so that they can have sizes and offets assigned to them. This also allows the removal of some `const_cast`s. Add MutableSymbolRef and MutableSymbolVector as mutable analogs to SymbolRef and SymbolVector. Replace uses of equivalent types with those names. Differential Revision: https://reviews.llvm.org/D79346
1 parent 947f78a commit d5c05ce

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

flang/include/flang/Semantics/scope.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct EquivalenceObject {
5252
using EquivalenceSet = std::vector<EquivalenceObject>;
5353

5454
class Scope {
55-
using mapType = std::map<SourceName, common::Reference<Symbol>>;
55+
using mapType = std::map<SourceName, MutableSymbolRef>;
5656

5757
public:
5858
ENUM_CLASS(Kind, Global, Module, MainProgram, Subprogram, BlockData,
@@ -110,7 +110,7 @@ class Scope {
110110

111111
// Return symbols in declaration order (the iterators above are in name order)
112112
SymbolVector GetSymbols() const;
113-
std::vector<common::Reference<Symbol>> GetSymbols();
113+
MutableSymbolVector GetSymbols();
114114

115115
iterator find(const SourceName &name);
116116
const_iterator find(const SourceName &name) const {
@@ -147,7 +147,10 @@ class Scope {
147147
// Make a copy of a symbol in this scope; nullptr if one is already there
148148
Symbol *CopySymbol(const Symbol &);
149149

150-
const std::list<EquivalenceSet> &equivalenceSets() const;
150+
std::list<EquivalenceSet> &equivalenceSets() { return equivalenceSets_; }
151+
const std::list<EquivalenceSet> &equivalenceSets() const {
152+
return equivalenceSets_;
153+
}
151154
void add_equivalenceSet(EquivalenceSet &&);
152155
// Cray pointers are saved as map of pointee name -> pointer symbol
153156
const mapType &crayPointers() const { return crayPointers_; }

flang/include/flang/Semantics/symbol.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ProgramTree;
3535

3636
using SymbolRef = common::Reference<const Symbol>;
3737
using SymbolVector = std::vector<SymbolRef>;
38+
using MutableSymbolRef = common::Reference<Symbol>;
39+
using MutableSymbolVector = std::vector<MutableSymbolRef>;
3840

3941
// A module or submodule.
4042
class ModuleDetails {
@@ -299,15 +301,16 @@ class NamelistDetails {
299301

300302
class CommonBlockDetails {
301303
public:
302-
const SymbolVector &objects() const { return objects_; }
303-
void add_object(const Symbol &object) { objects_.emplace_back(object); }
304+
MutableSymbolVector &objects() { return objects_; }
305+
const MutableSymbolVector &objects() const { return objects_; }
306+
void add_object(Symbol &object) { objects_.emplace_back(object); }
304307
MaybeExpr bindName() const { return bindName_; }
305308
void set_bindName(MaybeExpr &&expr) { bindName_ = std::move(expr); }
306309
std::size_t align() const { return align_; }
307310
void set_align(std::size_t align) { align_ = align; }
308311

309312
private:
310-
SymbolVector objects_;
313+
MutableSymbolVector objects_;
311314
MaybeExpr bindName_;
312315
std::size_t align_{0}; // required alignment in bytes
313316
};
@@ -739,8 +742,7 @@ inline bool ProcEntityDetails::HasExplicitInterface() const {
739742
}
740743

741744
inline bool operator<(SymbolRef x, SymbolRef y) { return *x < *y; }
742-
inline bool operator<(
743-
common::Reference<Symbol> x, common::Reference<Symbol> y) {
745+
inline bool operator<(MutableSymbolRef x, MutableSymbolRef y) {
744746
return *x < *y;
745747
}
746748
using SymbolSet = std::set<SymbolRef>;

flang/lib/Semantics/mod-file.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ void ModFileWriter::PutSymbol(
243243
[&](const CommonBlockDetails &x) {
244244
decls_ << "common/" << symbol.name();
245245
char sep = '/';
246-
for (const Symbol &object : x.objects()) {
247-
decls_ << sep << object.name();
246+
for (const auto &object : x.objects()) {
247+
decls_ << sep << object->name();
248248
sep = ',';
249249
}
250250
decls_ << '\n';
@@ -875,8 +875,8 @@ void SubprogramSymbolCollector::DoSymbol(
875875
}
876876
},
877877
[this](const CommonBlockDetails &details) {
878-
for (const Symbol &object : details.objects()) {
879-
DoSymbol(object);
878+
for (const auto &object : details.objects()) {
879+
DoSymbol(*object);
880880
}
881881
},
882882
[](const auto &) {},

flang/lib/Semantics/resolve-names.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -4376,9 +4376,8 @@ void DeclarationVisitor::CheckSaveStmts() {
43764376
" common block name '%s'"_err_en_US);
43774377
}
43784378
} else {
4379-
for (const Symbol &object :
4380-
symbol->get<CommonBlockDetails>().objects()) {
4381-
SetSaveAttr(*const_cast<Symbol *>(&object));
4379+
for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
4380+
SetSaveAttr(*object);
43824381
}
43834382
}
43844383
}
@@ -6692,11 +6691,9 @@ void OmpAttributeVisitor::ResolveOmpObject(
66926691
// 2.15.3 When a named common block appears in a list, it has the
66936692
// same meaning as if every explicit member of the common block
66946693
// appeared in the list
6695-
for (const Symbol &object :
6696-
symbol->get<CommonBlockDetails>().objects()) {
6697-
Symbol &mutableObject{const_cast<Symbol &>(object)};
6694+
for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
66986695
if (auto *resolvedObject{
6699-
ResolveOmp(mutableObject, ompFlag, currScope())}) {
6696+
ResolveOmp(*object, ompFlag, currScope())}) {
67006697
AddToContextObjectWithDSA(*resolvedObject, ompFlag);
67016698
}
67026699
}

flang/lib/Semantics/scope.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Scope &Scope::MakeScope(Kind kind, Symbol *symbol) {
6262

6363
template <typename T>
6464
static std::vector<common::Reference<T>> GetSortedSymbols(
65-
std::map<SourceName, common::Reference<Symbol>> symbols) {
65+
std::map<SourceName, MutableSymbolRef> symbols) {
6666
std::vector<common::Reference<T>> result;
6767
result.reserve(symbols.size());
6868
for (auto &pair : symbols) {
@@ -72,7 +72,7 @@ static std::vector<common::Reference<T>> GetSortedSymbols(
7272
return result;
7373
}
7474

75-
std::vector<common::Reference<Symbol>> Scope::GetSymbols() {
75+
MutableSymbolVector Scope::GetSymbols() {
7676
return GetSortedSymbols<Symbol>(symbols_);
7777
}
7878
SymbolVector Scope::GetSymbols() const {
@@ -145,9 +145,6 @@ Symbol *Scope::CopySymbol(const Symbol &symbol) {
145145
}
146146
}
147147

148-
const std::list<EquivalenceSet> &Scope::equivalenceSets() const {
149-
return equivalenceSets_;
150-
}
151148
void Scope::add_equivalenceSet(EquivalenceSet &&set) {
152149
equivalenceSets_.emplace_back(std::move(set));
153150
}

flang/lib/Semantics/symbol.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Details &details) {
438438
os << " align=" << x.align();
439439
}
440440
os << ':';
441-
for (const Symbol &object : x.objects()) {
442-
os << ' ' << object.name();
441+
for (const auto &object : x.objects()) {
442+
os << ' ' << object->name();
443443
}
444444
},
445445
[&](const FinalProcDetails &) {},

0 commit comments

Comments
 (0)