[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@a218cac788
Reviewed-on: https://github.com/flang-compiler/f18/pull/317
This commit is contained in:
Tim Keith 2019-03-06 17:07:25 -08:00
parent c1970da239
commit 251e0196e4
9 changed files with 38 additions and 38 deletions

View File

@ -89,8 +89,10 @@ private:
}
};
void CanonicalizeDo(Program &program) {
bool CanonicalizeDo(Program &program) {
CanonicalizationOfDoLoops canonicalizationOfDoLoops;
Walk(program, canonicalizationOfDoLoops);
return true;
}
}

View 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_

View 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()) {

View File

@ -37,7 +37,7 @@ class SemanticsContext;
class ModFileWriter {
public:
ModFileWriter(SemanticsContext &context) : context_{context} {}
void WriteAll();
bool WriteAll();
private:
SemanticsContext &context_;

View 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.

View 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_

View 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();
}
}

View 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_

View 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 {