[flang-commits] [flang] d5c05ce - [flang][NFC] Add accessors to equivalence and common blocks

Tim Keith via flang-commits flang-commits at lists.llvm.org
Wed May 6 12:23:31 PDT 2020


Author: Tim Keith
Date: 2020-05-06T12:22:28-07:00
New Revision: d5c05ced82ff698fad3da0537cf9b7cdb38dce85

URL: https://github.com/llvm/llvm-project/commit/d5c05ced82ff698fad3da0537cf9b7cdb38dce85
DIFF: https://github.com/llvm/llvm-project/commit/d5c05ced82ff698fad3da0537cf9b7cdb38dce85.diff

LOG: [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

Added: 
    

Modified: 
    flang/include/flang/Semantics/scope.h
    flang/include/flang/Semantics/symbol.h
    flang/lib/Semantics/mod-file.cpp
    flang/lib/Semantics/resolve-names.cpp
    flang/lib/Semantics/scope.cpp
    flang/lib/Semantics/symbol.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Semantics/scope.h b/flang/include/flang/Semantics/scope.h
index b653619f3542..17c91033bb9e 100644
--- a/flang/include/flang/Semantics/scope.h
+++ b/flang/include/flang/Semantics/scope.h
@@ -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,
@@ -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 {
@@ -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_; }

diff  --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index 4b22e57c0690..09f319513313 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -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 {
@@ -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
 };
@@ -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>;

diff  --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index 8e95b823a04f..d5b754816640 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -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';
@@ -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 &) {},

diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index e51c33988d0d..04acacb18fc1 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -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);
         }
       }
     }
@@ -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);
                 }
               }

diff  --git a/flang/lib/Semantics/scope.cpp b/flang/lib/Semantics/scope.cpp
index 4f89730537a0..92b09dd55ab4 100644
--- a/flang/lib/Semantics/scope.cpp
+++ b/flang/lib/Semantics/scope.cpp
@@ -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) {
@@ -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 {
@@ -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));
 }

diff  --git a/flang/lib/Semantics/symbol.cpp b/flang/lib/Semantics/symbol.cpp
index 35a3197963c3..5ba6d667a81b 100644
--- a/flang/lib/Semantics/symbol.cpp
+++ b/flang/lib/Semantics/symbol.cpp
@@ -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 &) {},


        


More information about the flang-commits mailing list