[flang] Change Scope::name() to Scope::GetName()
authorJean Perier <jperier@nvidia.com>
Wed, 21 Aug 2019 13:29:11 +0000 (06:29 -0700)
committerJean Perier <jperier@nvidia.com>
Wed, 21 Aug 2019 13:29:11 +0000 (06:29 -0700)
Address comments. Not all scopes are related to
a name. This change makes this more visible to compiler
programers by changing `scope:name()` into `Scope::GetName()`
that returns an optional `SourceName` instead of always
returning a `SourceName` and dying when it cannot.

Original-commit: flang-compiler/f18@0addb79919f69a6530b0919356a24e68f21507c1
Reviewed-on: https://github.com/flang-compiler/f18/pull/634
Tree-same-pre-rewrite: false

flang/lib/semantics/check-do.cc
flang/lib/semantics/mod-file.cc
flang/lib/semantics/resolve-names.cc
flang/lib/semantics/scope.h
flang/lib/semantics/symbol.cc
flang/lib/semantics/tools.cc

index e6db1ac..ddcd4e0 100644 (file)
@@ -206,7 +206,8 @@ private:
   bool EndTDeallocatesCoarray() { return false; }  // FIXME placeholder
   bool fromScope(const Symbol &symbol, const std::string &moduleName) {
     if (symbol.GetUltimate().owner().IsModule() &&
-        symbol.GetUltimate().owner().name().ToString() == moduleName) {
+        symbol.GetUltimate().owner().GetName().value().ToString() ==
+            moduleName) {
       return true;
     }
     return false;
index 2581cf1..1a99343 100644 (file)
@@ -126,7 +126,7 @@ void ModFileWriter::WriteOne(const Scope &scope) {
 // Write the module file for symbol, which must be a module or submodule.
 void ModFileWriter::Write(const Symbol &symbol) {
   auto *ancestor{symbol.get<ModuleDetails>().ancestor()};
-  auto ancestorName{ancestor ? ancestor->name().ToString() : ""s};
+  auto ancestorName{ancestor ? ancestor->GetName().value().ToString() : ""s};
   auto path{ModFilePath(context_.moduleDirectory(), symbol.name(), ancestorName,
       context_.moduleFileSuffix())};
   PutSymbols(*symbol.scope());
@@ -680,7 +680,7 @@ Scope *ModFileReader::Read(const SourceName &name, Scope *ancestor) {
     if (auto *scope{ancestor->FindSubmodule(name)}) {
       return scope;
     }
-    ancestorName = ancestor->name().ToString();
+    ancestorName = ancestor->GetName().value().ToString();
   } else {
     auto it{context_.globalScope().find(name)};
     if (it != context_.globalScope().end()) {
index deceb48..061a197 100644 (file)
@@ -1968,7 +1968,7 @@ ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
         IsDefinedOperator(useName)
             ? "Operator '%s' not found in module '%s'"_err_en_US
             : "'%s' not found in module '%s'"_err_en_US,
-        useName, useModuleScope_->name());
+        useName, useModuleScope_->GetName().value());
     return {};
   }
   if (useSymbol->attrs().test(Attr::PRIVATE)) {
@@ -1976,7 +1976,7 @@ ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
         IsDefinedOperator(useName)
             ? "Operator '%s' is PRIVATE in '%s'"_err_en_US
             : "'%s' is PRIVATE in '%s'"_err_en_US,
-        useName, useModuleScope_->name());
+        useName, useModuleScope_->GetName().value());
     return {};
   }
   auto &localSymbol{MakeSymbol(localName)};
@@ -2583,7 +2583,7 @@ bool DeclarationVisitor::CheckUseError(const parser::Name &name) {
   Message &msg{Say(name, "Reference to '%s' is ambiguous"_err_en_US)};
   for (const auto &[location, module] : details->occurrences()) {
     msg.Attach(location, "'%s' was use-associated from module '%s'"_en_US,
-        name.source, module->name());
+        name.source, module->GetName().value());
   }
   return true;
 }
@@ -2620,7 +2620,7 @@ bool DeclarationVisitor::CheckAccessibleComponent(
     }
     Say(name,
         "PRIVATE component '%s' is only accessible within module '%s'"_err_en_US,
-        name.ToString(), moduleScope->name());
+        name.ToString(), moduleScope->GetName().value());
   } else {
     Say(name,
         "PRIVATE component '%s' is only accessible within its module"_err_en_US,
@@ -5303,7 +5303,8 @@ void ResolveNamesVisitor::CheckImports() {
     // C8102: all entities in host must not be hidden
     for (const auto &pair : scope.parent()) {
       auto &name{pair.first};
-      if (!scope.GetSymbol() || name != scope.name()) {
+      std::optional<SourceName> scopeName{scope.GetName()};
+      if (!scopeName.has_value() || name != *scopeName) {
         CheckImport(prevImportStmt_.value(), name);
       }
     }
index cc2a3ac..f801aa6 100644 (file)
@@ -89,9 +89,13 @@ public:
   const Symbol *GetSymbol() const;
   const Scope *GetDerivedTypeParent() const;
 
-  // It is only safe to call name() for kind of scopes for which GetSymbol
-  // will return a symbol (e.g, it will die if the scope is a Block).
-  const SourceName &name() const { return DEREF(GetSymbol()).name(); }
+  std::optional<SourceName> GetName() const {
+    if (const auto *sym{GetSymbol()}) {
+      return sym->name();
+    } else {
+      return std::nullopt;
+    }
+  }
 
   /// Make a scope nested in this one
   Scope &MakeScope(Kind kind, Symbol *symbol = nullptr);
index 381b130..bf37c38 100644 (file)
@@ -392,10 +392,10 @@ std::ostream &operator<<(std::ostream &os, const Details &details) {
             if (x.isSubmodule()) {
               os << " (";
               if (x.ancestor()) {
-                auto &ancestor{x.ancestor()->name()};
+                auto ancestor{x.ancestor()->GetName().value()};
                 os << ancestor;
                 if (x.parent()) {
-                  auto &parent{x.parent()->name()};
+                  auto parent{x.parent()->GetName().value()};
                   if (ancestor != parent) {
                     os << ':' << parent;
                   }
@@ -430,7 +430,7 @@ std::ostream &operator<<(std::ostream &os, const Details &details) {
           [&](const UseErrorDetails &x) {
             os << " uses:";
             for (const auto &[location, module] : x.occurrences()) {
-              os << " from " << module->name() << " at " << location;
+              os << " from " << module->GetName().value() << " at " << location;
             }
           },
           [](const HostAssocDetails &) {},
index f4a59fa..d0f7b54 100644 (file)
@@ -367,7 +367,7 @@ bool IsDerivedTypeFromModule(
   } else {
     const auto &symbol{derived->typeSymbol()};
     return symbol.name() == name && symbol.owner().IsModule() &&
-        symbol.owner().name() == module;
+        symbol.owner().GetName().value() == module;
   }
 }