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
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;
// 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());
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()) {
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)) {
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)};
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;
}
}
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,
// 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);
}
}
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);
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;
}
[&](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 &) {},
} else {
const auto &symbol{derived->typeSymbol()};
return symbol.name() == name && symbol.owner().IsModule() &&
- symbol.owner().name() == module;
+ symbol.owner().GetName().value() == module;
}
}