Skip to content

Commit

Permalink
[flang] Change Scope::name() to Scope::GetName()
Browse files Browse the repository at this point in the history
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@0addb79
Reviewed-on: flang-compiler/f18#634
Tree-same-pre-rewrite: false
  • Loading branch information
jeanPerier committed Aug 21, 2019
1 parent 281d41c commit 52e72ab
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion flang/lib/semantics/check-do.cc
Expand Up @@ -206,7 +206,8 @@ class DoConcurrentBodyEnforce {
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;
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/semantics/mod-file.cc
Expand Up @@ -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());
Expand Down Expand Up @@ -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()) {
Expand Down
11 changes: 6 additions & 5 deletions flang/lib/semantics/resolve-names.cc
Expand Up @@ -1968,15 +1968,15 @@ 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)) {
Say(useName,
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)};
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
Expand Down
10 changes: 7 additions & 3 deletions flang/lib/semantics/scope.h
Expand Up @@ -89,9 +89,13 @@ class Scope {
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);
Expand Down
6 changes: 3 additions & 3 deletions flang/lib/semantics/symbol.cc
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 &) {},
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/semantics/tools.cc
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 52e72ab

Please sign in to comment.