Skip to content

Commit

Permalink
[flang][NFC] Add accessors to equivalence and common blocks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tskeith committed May 6, 2020
1 parent 947f78a commit d5c05ce
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
9 changes: 6 additions & 3 deletions flang/include/flang/Semantics/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct EquivalenceObject {
using EquivalenceSet = std::vector<EquivalenceObject>;

class Scope {
using mapType = std::map<SourceName, common::Reference<Symbol>>;
using mapType = std::map<SourceName, MutableSymbolRef>;

public:
ENUM_CLASS(Kind, Global, Module, MainProgram, Subprogram, BlockData,
Expand Down Expand Up @@ -110,7 +110,7 @@ class Scope {

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

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

const std::list<EquivalenceSet> &equivalenceSets() const;
std::list<EquivalenceSet> &equivalenceSets() { return equivalenceSets_; }
const std::list<EquivalenceSet> &equivalenceSets() const {
return equivalenceSets_;
}
void add_equivalenceSet(EquivalenceSet &&);
// Cray pointers are saved as map of pointee name -> pointer symbol
const mapType &crayPointers() const { return crayPointers_; }
Expand Down
12 changes: 7 additions & 5 deletions flang/include/flang/Semantics/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ProgramTree;

using SymbolRef = common::Reference<const Symbol>;
using SymbolVector = std::vector<SymbolRef>;
using MutableSymbolRef = common::Reference<Symbol>;
using MutableSymbolVector = std::vector<MutableSymbolRef>;

// A module or submodule.
class ModuleDetails {
Expand Down Expand Up @@ -299,15 +301,16 @@ class NamelistDetails {

class CommonBlockDetails {
public:
const SymbolVector &objects() const { return objects_; }
void add_object(const Symbol &object) { objects_.emplace_back(object); }
MutableSymbolVector &objects() { return objects_; }
const MutableSymbolVector &objects() const { return objects_; }
void add_object(Symbol &object) { objects_.emplace_back(object); }
MaybeExpr bindName() const { return bindName_; }
void set_bindName(MaybeExpr &&expr) { bindName_ = std::move(expr); }
std::size_t align() const { return align_; }
void set_align(std::size_t align) { align_ = align; }

private:
SymbolVector objects_;
MutableSymbolVector objects_;
MaybeExpr bindName_;
std::size_t align_{0}; // required alignment in bytes
};
Expand Down Expand Up @@ -739,8 +742,7 @@ inline bool ProcEntityDetails::HasExplicitInterface() const {
}

inline bool operator<(SymbolRef x, SymbolRef y) { return *x < *y; }
inline bool operator<(
common::Reference<Symbol> x, common::Reference<Symbol> y) {
inline bool operator<(MutableSymbolRef x, MutableSymbolRef y) {
return *x < *y;
}
using SymbolSet = std::set<SymbolRef>;
Expand Down
8 changes: 4 additions & 4 deletions flang/lib/Semantics/mod-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ void ModFileWriter::PutSymbol(
[&](const CommonBlockDetails &x) {
decls_ << "common/" << symbol.name();
char sep = '/';
for (const Symbol &object : x.objects()) {
decls_ << sep << object.name();
for (const auto &object : x.objects()) {
decls_ << sep << object->name();
sep = ',';
}
decls_ << '\n';
Expand Down Expand Up @@ -875,8 +875,8 @@ void SubprogramSymbolCollector::DoSymbol(
}
},
[this](const CommonBlockDetails &details) {
for (const Symbol &object : details.objects()) {
DoSymbol(object);
for (const auto &object : details.objects()) {
DoSymbol(*object);
}
},
[](const auto &) {},
Expand Down
11 changes: 4 additions & 7 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4376,9 +4376,8 @@ void DeclarationVisitor::CheckSaveStmts() {
" common block name '%s'"_err_en_US);
}
} else {
for (const Symbol &object :
symbol->get<CommonBlockDetails>().objects()) {
SetSaveAttr(*const_cast<Symbol *>(&object));
for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
SetSaveAttr(*object);
}
}
}
Expand Down Expand Up @@ -6692,11 +6691,9 @@ void OmpAttributeVisitor::ResolveOmpObject(
// 2.15.3 When a named common block appears in a list, it has the
// same meaning as if every explicit member of the common block
// appeared in the list
for (const Symbol &object :
symbol->get<CommonBlockDetails>().objects()) {
Symbol &mutableObject{const_cast<Symbol &>(object)};
for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
if (auto *resolvedObject{
ResolveOmp(mutableObject, ompFlag, currScope())}) {
ResolveOmp(*object, ompFlag, currScope())}) {
AddToContextObjectWithDSA(*resolvedObject, ompFlag);
}
}
Expand Down
7 changes: 2 additions & 5 deletions flang/lib/Semantics/scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Scope &Scope::MakeScope(Kind kind, Symbol *symbol) {

template <typename T>
static std::vector<common::Reference<T>> GetSortedSymbols(
std::map<SourceName, common::Reference<Symbol>> symbols) {
std::map<SourceName, MutableSymbolRef> symbols) {
std::vector<common::Reference<T>> result;
result.reserve(symbols.size());
for (auto &pair : symbols) {
Expand All @@ -72,7 +72,7 @@ static std::vector<common::Reference<T>> GetSortedSymbols(
return result;
}

std::vector<common::Reference<Symbol>> Scope::GetSymbols() {
MutableSymbolVector Scope::GetSymbols() {
return GetSortedSymbols<Symbol>(symbols_);
}
SymbolVector Scope::GetSymbols() const {
Expand Down Expand Up @@ -145,9 +145,6 @@ Symbol *Scope::CopySymbol(const Symbol &symbol) {
}
}

const std::list<EquivalenceSet> &Scope::equivalenceSets() const {
return equivalenceSets_;
}
void Scope::add_equivalenceSet(EquivalenceSet &&set) {
equivalenceSets_.emplace_back(std::move(set));
}
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Semantics/symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Details &details) {
os << " align=" << x.align();
}
os << ':';
for (const Symbol &object : x.objects()) {
os << ' ' << object.name();
for (const auto &object : x.objects()) {
os << ' ' << object->name();
}
},
[&](const FinalProcDetails &) {},
Expand Down

0 comments on commit d5c05ce

Please sign in to comment.