[flang] Simplify Semantics::Perform
authorTim Keith <tkeith@nvidia.com>
Thu, 7 Mar 2019 01:07:25 +0000 (17:07 -0800)
committerTim Keith <tkeith@nvidia.com>
Thu, 7 Mar 2019 01:07:25 +0000 (17:07 -0800)
`Semantics::Perform` is mostly a series of calls followed by a check
for fatal errors. There is more error checking logic than real code.

To make it clearer, change each of the phases it calls to return true
on success so that `Perform` can just call them one after the other.

Original-commit: flang-compiler/f18@a218cac788fdc4018645651964e75221c6f31653
Reviewed-on: https://github.com/flang-compiler/f18/pull/317

flang/lib/semantics/canonicalize-do.cc
flang/lib/semantics/canonicalize-do.h
flang/lib/semantics/mod-file.cc
flang/lib/semantics/mod-file.h
flang/lib/semantics/resolve-names.cc
flang/lib/semantics/resolve-names.h
flang/lib/semantics/rewrite-parse-tree.cc
flang/lib/semantics/rewrite-parse-tree.h
flang/lib/semantics/semantics.cc

index 3e3c382..ba2a726 100644 (file)
@@ -89,8 +89,10 @@ private:
   }
 };
 
-void CanonicalizeDo(Program &program) {
+bool CanonicalizeDo(Program &program) {
   CanonicalizationOfDoLoops canonicalizationOfDoLoops;
   Walk(program, canonicalizationOfDoLoops);
+  return true;
 }
+
 }
index 7f46910..5b76a6c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
+// Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
 // logically nested) into the more structured DoConstruct (explicitly nested)
 namespace Fortran::parser {
 struct Program;
-void CanonicalizeDo(Program &program);
+bool CanonicalizeDo(Program &program);
 }
 
 #endif  // FORTRAN_SEMANTICS_CANONICALIZE_DO_H_
index 329ed8b..503a527 100644 (file)
@@ -59,7 +59,10 @@ static bool FileContentsMatch(
 static std::string GetHeader(const std::string &);
 static std::size_t GetFileSize(const std::string &);
 
-void ModFileWriter::WriteAll() { WriteAll(context_.globalScope()); }
+bool ModFileWriter::WriteAll() {
+  WriteAll(context_.globalScope());
+  return !context_.AnyFatalError();
+}
 
 void ModFileWriter::WriteAll(const Scope &scope) {
   for (const auto &child : scope.children()) {
index 38d7bc0..3721551 100644 (file)
@@ -37,7 +37,7 @@ class SemanticsContext;
 class ModFileWriter {
 public:
   ModFileWriter(SemanticsContext &context) : context_{context} {}
-  void WriteAll();
+  bool WriteAll();
 
 private:
   SemanticsContext &context_;
index f8f5017..a8ea545 100644 (file)
@@ -4637,8 +4637,9 @@ void ResolveNamesVisitor::Post(const parser::Program &) {
   CHECK(!GetDeclTypeSpec());
 }
 
-void ResolveNames(SemanticsContext &context, const parser::Program &program) {
+bool ResolveNames(SemanticsContext &context, const parser::Program &program) {
   ResolveNamesVisitor{context}.Walk(program);
+  return !context.AnyFatalError();
 }
 
 // Get the Name out of a GenericSpec, or nullptr if none.
index 8423029..16f97f4 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
+// Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -27,8 +27,9 @@ namespace Fortran::semantics {
 
 class SemanticsContext;
 
-void ResolveNames(SemanticsContext &, const parser::Program &);
+bool ResolveNames(SemanticsContext &, const parser::Program &);
 void DumpSymbols(std::ostream &);
+
 }
 
 #endif  // FORTRAN_SEMANTICS_RESOLVE_NAMES_H_
index 9193f17..02748ca 100644 (file)
@@ -120,8 +120,10 @@ bool RewriteMutator::Pre(parser::ExecutionPart &x) {
   return true;
 }
 
-void RewriteParseTree(SemanticsContext &context, parser::Program &program) {
+bool RewriteParseTree(SemanticsContext &context, parser::Program &program) {
   RewriteMutator mutator{context.messages()};
   parser::Walk(program, mutator);
+  return !context.AnyFatalError();
 }
+
 }
index 9fb2aff..ee846aa 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
+// Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@ class SemanticsContext;
 }
 
 namespace Fortran::semantics {
-void RewriteParseTree(SemanticsContext &, parser::Program &);
+bool RewriteParseTree(SemanticsContext &, parser::Program &);
 }
 
 #endif  // FORTRAN_SEMANTICS_REWRITE_PARSE_TREE_H_
index c48abef..088c26f 100644 (file)
@@ -37,18 +37,26 @@ static void PutIndent(std::ostream &, int indent);
 // children are visited, Leave is called after. No two checkers may have the
 // same Enter or Leave function. Each checker must be constructible from
 // SemanticsContext and have BaseChecker as a virtual base class.
-template<typename... C> struct SemanticsVisitor : public virtual C... {
+template<typename... C> class SemanticsVisitor : public virtual C... {
+public:
   using C::Enter...;
   using C::Leave...;
   using BaseChecker::Enter;
   using BaseChecker::Leave;
-  SemanticsVisitor(SemanticsContext &context) : C{context}... {}
+  SemanticsVisitor(SemanticsContext &context)
+    : C{context}..., context_{context} {}
   template<typename N> bool Pre(const N &node) {
     Enter(node);
     return true;
   }
   template<typename N> void Post(const N &node) { Leave(node); }
-  void Walk(const parser::Program &program) { parser::Walk(program, *this); }
+  bool Walk(const parser::Program &program) {
+    parser::Walk(program, *this);
+    return !context_.AnyFatalError();
+  }
+
+private:
+  SemanticsContext &context_;
 };
 
 using StatementSemanticsPass1 = SemanticsVisitor<ExprChecker>;
@@ -91,30 +99,13 @@ const Scope &SemanticsContext::FindScope(
 }
 
 bool Semantics::Perform() {
-  ValidateLabels(context_.messages(), program_);
-  if (AnyFatalError()) {
-    return false;
-  }
-  parser::CanonicalizeDo(program_);
-  ResolveNames(context_, program_);
-  if (AnyFatalError()) {
-    return false;
-  }
-  RewriteParseTree(context_, program_);
-  if (AnyFatalError()) {
-    return false;
-  }
-  StatementSemanticsPass1{context_}.Walk(program_);
-  if (AnyFatalError()) {
-    return false;
-  }
-  StatementSemanticsPass2{context_}.Walk(program_);
-  if (AnyFatalError()) {
-    return false;
-  }
-  ModFileWriter writer{context_};
-  writer.WriteAll();
-  return !AnyFatalError();
+  return ValidateLabels(context_.messages(), program_) &&
+      parser::CanonicalizeDo(program_) &&  // force line break
+      ResolveNames(context_, program_) &&
+      RewriteParseTree(context_, program_) &&
+      StatementSemanticsPass1{context_}.Walk(program_) &&
+      StatementSemanticsPass2{context_}.Walk(program_) &&
+      ModFileWriter{context_}.WriteAll();
 }
 
 void Semantics::EmitMessages(std::ostream &os) const {