From 251e0196e4b4eb1ac2e58d3d20232cc34902072c Mon Sep 17 00:00:00 2001 From: Tim Keith Date: Wed, 6 Mar 2019 17:07:25 -0800 Subject: [PATCH] [flang] Simplify Semantics::Perform `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 | 4 ++- flang/lib/semantics/canonicalize-do.h | 4 +-- flang/lib/semantics/mod-file.cc | 5 +++- flang/lib/semantics/mod-file.h | 2 +- flang/lib/semantics/resolve-names.cc | 3 ++- flang/lib/semantics/resolve-names.h | 5 ++-- flang/lib/semantics/rewrite-parse-tree.cc | 4 ++- flang/lib/semantics/rewrite-parse-tree.h | 4 +-- flang/lib/semantics/semantics.cc | 45 +++++++++++++------------------ 9 files changed, 38 insertions(+), 38 deletions(-) diff --git a/flang/lib/semantics/canonicalize-do.cc b/flang/lib/semantics/canonicalize-do.cc index 3e3c382..ba2a726a 100644 --- a/flang/lib/semantics/canonicalize-do.cc +++ b/flang/lib/semantics/canonicalize-do.cc @@ -89,8 +89,10 @@ private: } }; -void CanonicalizeDo(Program &program) { +bool CanonicalizeDo(Program &program) { CanonicalizationOfDoLoops canonicalizationOfDoLoops; Walk(program, canonicalizationOfDoLoops); + return true; } + } diff --git a/flang/lib/semantics/canonicalize-do.h b/flang/lib/semantics/canonicalize-do.h index 7f46910..5b76a6c 100644 --- a/flang/lib/semantics/canonicalize-do.h +++ b/flang/lib/semantics/canonicalize-do.h @@ -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_ diff --git a/flang/lib/semantics/mod-file.cc b/flang/lib/semantics/mod-file.cc index 329ed8b..503a5278 100644 --- a/flang/lib/semantics/mod-file.cc +++ b/flang/lib/semantics/mod-file.cc @@ -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()) { diff --git a/flang/lib/semantics/mod-file.h b/flang/lib/semantics/mod-file.h index 38d7bc0..3721551 100644 --- a/flang/lib/semantics/mod-file.h +++ b/flang/lib/semantics/mod-file.h @@ -37,7 +37,7 @@ class SemanticsContext; class ModFileWriter { public: ModFileWriter(SemanticsContext &context) : context_{context} {} - void WriteAll(); + bool WriteAll(); private: SemanticsContext &context_; diff --git a/flang/lib/semantics/resolve-names.cc b/flang/lib/semantics/resolve-names.cc index f8f5017..a8ea545 100644 --- a/flang/lib/semantics/resolve-names.cc +++ b/flang/lib/semantics/resolve-names.cc @@ -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. diff --git a/flang/lib/semantics/resolve-names.h b/flang/lib/semantics/resolve-names.h index 8423029..16f97f4 100644 --- a/flang/lib/semantics/resolve-names.h +++ b/flang/lib/semantics/resolve-names.h @@ -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_ diff --git a/flang/lib/semantics/rewrite-parse-tree.cc b/flang/lib/semantics/rewrite-parse-tree.cc index 9193f17..02748ca 100644 --- a/flang/lib/semantics/rewrite-parse-tree.cc +++ b/flang/lib/semantics/rewrite-parse-tree.cc @@ -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(); } + } diff --git a/flang/lib/semantics/rewrite-parse-tree.h b/flang/lib/semantics/rewrite-parse-tree.h index 9fb2affb..ee846aa 100644 --- a/flang/lib/semantics/rewrite-parse-tree.h +++ b/flang/lib/semantics/rewrite-parse-tree.h @@ -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_ diff --git a/flang/lib/semantics/semantics.cc b/flang/lib/semantics/semantics.cc index c48abef..088c26f 100644 --- a/flang/lib/semantics/semantics.cc +++ b/flang/lib/semantics/semantics.cc @@ -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 struct SemanticsVisitor : public virtual C... { +template 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 bool Pre(const N &node) { Enter(node); return true; } template 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; @@ -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 { -- 2.7.4