[flang] Add IsGlobal and IsDerivedType to Scope
authorTim Keith <tkeith@nvidia.com>
Thu, 11 Jul 2019 16:27:25 +0000 (09:27 -0700)
committerTim Keith <tkeith@nvidia.com>
Fri, 12 Jul 2019 19:17:56 +0000 (12:17 -0700)
These are properties of Scopes that are checked frequently
so add IsGlobal and IsDerivedType for convenience.

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

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

index d41d552..90477b6 100644 (file)
@@ -22,7 +22,7 @@ namespace Fortran::semantics {
 
 const Scope *FindContainingSubprogram(const Scope &start) {
   const Scope *scope{&start};
-  while (scope->kind() != Scope::Kind::Global) {
+  while (!scope->IsGlobal()) {
     switch (scope->kind()) {
     case Scope::Kind::MainProgram:
     case Scope::Kind::Subprogram: return scope;
index 52f29e9..beb52cb 100644 (file)
@@ -174,7 +174,7 @@ void ModFileWriter::PutSymbols(const Scope &scope) {
     PutSymbol(typeBindings, symbol);
   }
   if (auto str{typeBindings.str()}; !str.empty()) {
-    CHECK(scope.kind() == Scope::Kind::DerivedType);
+    CHECK(scope.IsDerivedType());
     decls_ << "contains\n" << str;
   }
 }
@@ -806,7 +806,7 @@ void SubprogramSymbolCollector::Collect() {
 // Do symbols this one depends on; then add to need_
 void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) {
   const auto &scope{symbol.owner()};
-  if (scope != scope_ && scope.kind() != Scope::Kind::DerivedType) {
+  if (scope != scope_ && !scope.IsDerivedType()) {
     if (scope != scope_.parent()) {
       useSet_.insert(&symbol);
     } else if (isInterface_) {
@@ -843,7 +843,7 @@ void SubprogramSymbolCollector::DoSymbol(const Symbol &symbol) {
   if (!symbol.has<UseDetails>()) {
     DoType(symbol.GetType());
   }
-  if (scope.kind() != Scope::Kind::DerivedType) {
+  if (!scope.IsDerivedType()) {
     need_.push_back(&symbol);
   }
 }
index cd90205..62ab64a 100644 (file)
@@ -510,7 +510,7 @@ bool EquivalenceSets::CheckObject(const parser::Name &name) {
   currObject_.symbol = name.symbol;
   parser::MessageFixedText msg{"", 0};
   const Symbol &symbol{*name.symbol};
-  if (symbol.owner().kind() == Scope::Kind::DerivedType) {  // C8107
+  if (symbol.owner().IsDerivedType()) {  // C8107
     msg = "Derived type component '%s'"
           " is not allowed in an equivalence set"_err_en_US;
   } else if (symbol.IsDummy()) {  // C8106
index 5757093..1acfc57 100644 (file)
@@ -1557,8 +1557,7 @@ void ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
 
 Scope &ScopeHandler::InclusiveScope() {
   for (auto *scope{&currScope()};; scope = &scope->parent()) {
-    if (scope->kind() != Scope::Kind::Block &&
-        scope->kind() != Scope::Kind::DerivedType) {
+    if (scope->kind() != Scope::Kind::Block && !scope->IsDerivedType()) {
       return *scope;
     }
   }
@@ -1566,7 +1565,7 @@ Scope &ScopeHandler::InclusiveScope() {
 }
 Scope &ScopeHandler::GlobalScope() {
   for (auto *scope = currScope_; scope; scope = &scope->parent()) {
-    if (scope->kind() == Scope::Kind::Global) {
+    if (scope->IsGlobal()) {
       return *scope;
     }
   }
@@ -1581,7 +1580,7 @@ void ScopeHandler::PushScope(Scope &scope) {
   if (kind != Scope::Kind::Block) {
     ImplicitRulesVisitor::BeginScope(scope);
   }
-  if (kind != Scope::Kind::DerivedType) {
+  if (!currScope_->IsDerivedType()) {
     if (auto *symbol{scope.symbol()}) {
       // Create a dummy symbol so we can't create another one with the same
       // name. It might already be there if we previously pushed the scope.
@@ -1665,7 +1664,7 @@ Symbol *ScopeHandler::FindInScope(const Scope &scope, const SourceName &name) {
 
 // Find a component or type parameter by name in a derived type or its parents.
 Symbol *ScopeHandler::FindInTypeOrParents(const Scope &scope, SourceName name) {
-  if (scope.kind() == Scope::Kind::DerivedType) {
+  if (scope.IsDerivedType()) {
     if (Symbol * symbol{FindInScope(scope, name)}) {
       return symbol;
     }
@@ -2542,13 +2541,13 @@ bool DeclarationVisitor::CheckAccessibleComponent(
   }
   // component must be in a module/submodule because of PRIVATE:
   const Scope *moduleScope{&symbol.owner()};
-  CHECK(moduleScope->kind() == Scope::Kind::DerivedType);
-  while (moduleScope->kind() != Scope::Kind::Module &&
-      moduleScope->kind() != Scope::Kind::Global) {
+  CHECK(moduleScope->IsDerivedType());
+  while (
+      moduleScope->kind() != Scope::Kind::Module && !moduleScope->IsGlobal()) {
     moduleScope = &moduleScope->parent();
   }
   if (moduleScope->kind() == Scope::Kind::Module) {
-    for (auto *scope{&currScope()}; scope->kind() != Scope::Kind::Global;
+    for (auto *scope{&currScope()}; !scope->IsGlobal();
          scope = &scope->parent()) {
       if (scope == moduleScope) {
         return true;
@@ -3940,7 +3939,7 @@ Symbol *DeclarationVisitor::MakeTypeSymbol(
 Symbol *DeclarationVisitor::MakeTypeSymbol(
     const SourceName &name, Details &&details) {
   Scope &derivedType{currScope()};
-  CHECK(derivedType.kind() == Scope::Kind::DerivedType);
+  CHECK(derivedType.IsDerivedType());
   if (auto *symbol{FindInScope(derivedType, name)}) {
     Say2(name,
         "Type parameter, component, or procedure binding '%s'"
@@ -3968,7 +3967,7 @@ Symbol *DeclarationVisitor::MakeTypeSymbol(
 bool DeclarationVisitor::OkToAddComponent(
     const parser::Name &name, const Symbol *extends) {
   for (const Scope *scope{&currScope()}; scope != nullptr;) {
-    CHECK(scope->kind() == Scope::Kind::DerivedType);
+    CHECK(scope->IsDerivedType());
     if (auto *prev{FindInScope(*scope, name)}) {
       auto msg{""_en_US};
       if (extends != nullptr) {
@@ -4454,7 +4453,7 @@ bool ResolveNamesVisitor::Pre(const parser::ImportStmt &x) {
     Say("IMPORT is not allowed in a main program scoping unit"_err_en_US);
     return false;
   case Scope::Kind::Subprogram:
-    if (scope.parent().kind() == Scope::Kind::Global) {
+    if (scope.parent().IsGlobal()) {
       Say("IMPORT is not allowed in an external subprogram scoping unit"_err_en_US);
       return false;
     }
@@ -5148,7 +5147,7 @@ void ResolveNamesVisitor::FinishSpecificationParts(const ProgramTree &node) {
     }
   }
   for (Scope &childScope : currScope().children()) {
-    if (childScope.kind() == Scope::Kind::DerivedType && childScope.symbol()) {
+    if (childScope.IsDerivedType() && childScope.symbol()) {
       FinishDerivedType(childScope);
     }
   }
@@ -5169,7 +5168,7 @@ static int FindIndexOfName(
 
 // Perform checks on procedure bindings of this type
 void ResolveNamesVisitor::FinishDerivedType(Scope &scope) {
-  CHECK(scope.kind() == Scope::Kind::DerivedType);
+  CHECK(scope.IsDerivedType());
   for (auto &pair : scope) {
     Symbol &comp{*pair.second};
     std::visit(
index 2a37014..9767f3c 100644 (file)
@@ -75,7 +75,7 @@ Scope::size_type Scope::erase(const SourceName &name) {
   }
 }
 Symbol *Scope::FindSymbol(const SourceName &name) const {
-  if (kind() == Kind::DerivedType) {
+  if (IsDerivedType()) {
     return parent_.FindSymbol(name);
   }
   auto it{find(name)};
@@ -173,7 +173,7 @@ DeclTypeSpec &Scope::MakeDerivedType(
 
 void Scope::set_chars(parser::CookedSource &cooked) {
   CHECK(kind_ == Kind::Module);
-  CHECK(parent_.kind_ == Kind::Global || parent_.IsModuleFile());
+  CHECK(parent_.IsGlobal() || parent_.IsModuleFile());
   CHECK(symbol_ != nullptr);
   CHECK(symbol_->test(Symbol::Flag::ModFile));
   // TODO: Preserve the CookedSource rather than acquiring its string.
@@ -220,7 +220,7 @@ void Scope::add_importName(const SourceName &name) {
 
 // true if name can be imported or host-associated from parent scope.
 bool Scope::CanImport(const SourceName &name) const {
-  if (kind_ == Kind::Global) {
+  if (IsGlobal()) {
     return false;
   }
   switch (GetImportKind()) {
@@ -238,7 +238,7 @@ const Scope *Scope::FindScope(parser::CharBlock source) const {
 
 Scope *Scope::FindScope(parser::CharBlock source) {
   bool isContained{sourceRange_.Contains(source)};
-  if (!isContained && kind_ != Kind::Global && !IsModuleFile()) {
+  if (!isContained && !IsGlobal() && !IsModuleFile()) {
     return nullptr;
   }
   for (auto &child : children_) {
@@ -281,7 +281,7 @@ std::ostream &operator<<(std::ostream &os, const Scope &scope) {
 }
 
 bool Scope::IsParameterizedDerivedType() const {
-  if (kind_ != Kind::DerivedType) {
+  if (!IsDerivedType()) {
     return false;
   }
   if (const Scope * parent{GetDerivedTypeParent()}) {
@@ -302,7 +302,7 @@ const DeclTypeSpec *Scope::FindInstantiatedDerivedType(
   DeclTypeSpec type{category, spec};
   if (const auto *result{FindType(type)}) {
     return result;
-  } else if (kind() == Kind::Global) {
+  } else if (IsGlobal()) {
     return nullptr;
   } else {
     return parent().FindInstantiatedDerivedType(spec, category);
index dba7565..281a5f5 100644 (file)
@@ -79,7 +79,9 @@ public:
     return parent_;
   }
   Kind kind() const { return kind_; }
+  bool IsGlobal() const { return kind_ == Kind::Global; }
   bool IsModule() const;  // only module, not submodule
+  bool IsDerivedType() const { return kind_ == Kind::DerivedType; }
   bool IsParameterizedDerivedType() const;
   Symbol *symbol() { return symbol_; }
   const Symbol *symbol() const { return symbol_; }
index 4c55c64..e193844 100644 (file)
@@ -311,8 +311,7 @@ bool Symbol::IsSeparateModuleProc() const {
 
 bool Symbol::IsFromModFile() const {
   return test(Flag::ModFile) ||
-      (owner_->kind() != Scope::Kind::Global &&
-          owner_->symbol()->IsFromModFile());
+      (!owner_->IsGlobal() && owner_->symbol()->IsFromModFile());
 }
 
 ObjectEntityDetails::ObjectEntityDetails(EntityDetails &&d)
@@ -483,7 +482,7 @@ std::ostream &operator<<(std::ostream &os, const Symbol &symbol) {
 // parent scopes. For scopes without corresponding symbols, use the kind
 // with an index (e.g. Block1, Block2, etc.).
 static void DumpUniqueName(std::ostream &os, const Scope &scope) {
-  if (scope.kind() != Scope::Kind::Global) {
+  if (!scope.IsGlobal()) {
     DumpUniqueName(os, scope.parent());
     os << '/';
     if (auto *scopeSymbol{scope.symbol()};
index 8be6317..834769f 100644 (file)
@@ -39,8 +39,8 @@ static const Symbol *FindCommonBlockInScope(
 }
 
 const Symbol *FindCommonBlockContaining(const Symbol &object) {
-  for (const Scope *scope{&object.owner()};
-       scope->kind() != Scope::Kind::Global; scope = &scope->parent()) {
+  for (const Scope *scope{&object.owner()}; !scope->IsGlobal();
+       scope = &scope->parent()) {
     if (const Symbol * block{FindCommonBlockInScope(*scope, object)}) {
       return block;
     }
@@ -96,7 +96,7 @@ bool DoesScopeContain(
     const Scope *maybeAncestor, const Scope &maybeDescendent) {
   if (maybeAncestor != nullptr) {
     const Scope *scope{&maybeDescendent};
-    while (scope->kind() != Scope::Kind::Global) {
+    while (!scope->IsGlobal()) {
       scope = &scope->parent();
       if (scope == maybeAncestor) {
         return true;
@@ -188,7 +188,7 @@ bool IsProcedurePointer(const Symbol &symbol) {
 
 static const Symbol *FindPointerComponent(
     const Scope &scope, std::set<const Scope *> &visited) {
-  if (scope.kind() != Scope::Kind::DerivedType) {
+  if (!scope.IsDerivedType()) {
     return nullptr;
   }
   if (!visited.insert(&scope).second) {
@@ -450,7 +450,7 @@ static const DeclTypeSpec *FindInstantiatedDerivedType(const Scope &scope,
   DeclTypeSpec type{category, spec};
   if (const auto *found{scope.FindType(type)}) {
     return found;
-  } else if (scope.kind() == Scope::Kind::Global) {
+  } else if (scope.IsGlobal()) {
     return nullptr;
   } else {
     return FindInstantiatedDerivedType(scope.parent(), spec, category);
index 33876ed..0b78f8e 100644 (file)
@@ -36,7 +36,7 @@ void DerivedTypeSpec::set_scope(const Scope &scope) {
   ReplaceScope(scope);
 }
 void DerivedTypeSpec::ReplaceScope(const Scope &scope) {
-  CHECK(scope.kind() == Scope::Kind::DerivedType);
+  CHECK(scope.IsDerivedType());
   scope_ = &scope;
 }