From 72fd06b389cc128aef36dd9869b25bead5f58887 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Tue, 30 Oct 2018 13:30:56 -0700 Subject: [PATCH] [flang] Clean up Designator and GetSymbol() Original-commit: flang-compiler/f18@61c0af323c13f7ad97521332603497d21638e2f9 Reviewed-on: https://github.com/flang-compiler/f18/pull/225 Tree-same-pre-rewrite: false --- flang/lib/evaluate/tools.h | 2 +- flang/lib/evaluate/variable.cc | 96 ++++++++++++++++++++++++++++++++------- flang/lib/evaluate/variable.h | 65 +++++++------------------- flang/lib/semantics/expression.cc | 6 +-- 4 files changed, 100 insertions(+), 69 deletions(-) diff --git a/flang/lib/evaluate/tools.h b/flang/lib/evaluate/tools.h index 4d4f0e9..5c04716 100644 --- a/flang/lib/evaluate/tools.h +++ b/flang/lib/evaluate/tools.h @@ -58,7 +58,7 @@ template bool IsVariable(const A &) { return false; } template bool IsVariable(const Designator &designator) { if constexpr (common::HasMember::u)>) { if (const auto *substring{std::get_if(&designator.u)}) { - return substring->GetSymbol(false) != nullptr; + return substring->GetLastSymbol() != nullptr; } } return true; diff --git a/flang/lib/evaluate/variable.cc b/flang/lib/evaluate/variable.cc index 7a1be3e..0ce8754 100644 --- a/flang/lib/evaluate/variable.cc +++ b/flang/lib/evaluate/variable.cc @@ -313,6 +313,14 @@ std::ostream &ProcedureDesignator::Dump(std::ostream &o) const { return Emit(o, u); } +template std::ostream &Designator::Dump(std::ostream &o) const { + std::visit( + common::visitors{[&](const Symbol *sym) { o << sym->name().ToString(); }, + [&](const auto &x) { x.Dump(o); }}, + u); + return o; +} + // LEN() static Expr SymbolLEN(const Symbol &sym) { return AsExpr(Constant{0}); // TODO @@ -338,12 +346,17 @@ Expr Substring::LEN() const { Extremum{AsExpr(Constant{0}), last() - first() + AsExpr(Constant{1})}); } -template Expr Designator::LEN() const { - return std::visit( - common::visitors{[](const Symbol *s) { return SymbolLEN(*s); }, - [](const Component &c) { return c.LEN(); }, - [](const auto &x) { return x.LEN(); }}, - u); +template Expr Designator::LEN() const { + if constexpr (Result::category == TypeCategory::Character) { + return std::visit( + common::visitors{[](const Symbol *s) { return SymbolLEN(*s); }, + [](const Component &c) { return c.LEN(); }, + [](const auto &x) { return x.LEN(); }}, + u); + } else { + CHECK(!"LEN() on non-character Designator"); + return AsExpr(Constant{0}); + } } Expr ProcedureDesignator::LEN() const { return std::visit( @@ -419,6 +432,12 @@ int Substring::Rank() const { u_); } int ComplexPart::Rank() const { return complex_.Rank(); } +template int Designator::Rank() const { + return std::visit( + common::visitors{[](const Symbol *sym) { return sym->Rank(); }, + [](const auto &x) { return x.Rank(); }}, + u); +} int ProcedureDesignator::Rank() const { if (const Symbol * symbol{GetSymbol()}) { return symbol->Rank(); @@ -441,36 +460,79 @@ bool ProcedureDesignator::IsElemental() const { return 0; } -// GetSymbol -const Symbol *Component::GetSymbol(bool first) const { - return base_->GetSymbol(first); +// GetFirstSymbol(), GetLastSymbol() +const Symbol *Component::GetFirstSymbol() const { + return base_->GetFirstSymbol(); } -const Symbol *ArrayRef::GetSymbol(bool first) const { +const Symbol *Component::GetLastSymbol() const { + return base_->GetLastSymbol(); +} +const Symbol *ArrayRef::GetFirstSymbol() const { return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, [=](const Component &component) { - return component.GetSymbol(first); + return component.GetFirstSymbol(); }}, u); } -const Symbol *DataRef::GetSymbol(bool first) const { +const Symbol *ArrayRef::GetLastSymbol() const { return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, - [=](const auto &x) { return x.GetSymbol(first); }}, + [=](const Component &component) { + return component.GetLastSymbol(); + }}, u); } -const Symbol *Substring::GetSymbol(bool first) const { +const Symbol *DataRef::GetFirstSymbol() const { + return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, + [=](const auto &x) { return x.GetFirstSymbol(); }}, + u); +} +const Symbol *DataRef::GetLastSymbol() const { + return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, + [=](const auto &x) { return x.GetLastSymbol(); }}, + u); +} +const Symbol *Substring::GetFirstSymbol() const { + if (const DataRef * dataRef{std::get_if(&u_)}) { + return dataRef->GetFirstSymbol(); + } else { + return nullptr; // substring of character literal + } +} +const Symbol *Substring::GetLastSymbol() const { if (const DataRef * dataRef{std::get_if(&u_)}) { - return dataRef->GetSymbol(first); + return dataRef->GetLastSymbol(); } else { return nullptr; // substring of character literal } } +template const Symbol *Designator::GetFirstSymbol() const { + return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, + [=](const auto &x) { return x.GetFirstSymbol(); }}, + u); +} +template const Symbol *Designator::GetLastSymbol() const { + return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, + [=](const auto &x) { return x.GetLastSymbol(); }}, + u); +} const Symbol *ProcedureDesignator::GetSymbol() const { return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, - [](const Component &c) { return c.GetSymbol(false); }, + [](const Component &c) { return c.GetLastSymbol(); }, [](const auto &) -> const Symbol * { return nullptr; }}, u); } +template std::optional Designator::GetType() const { + if constexpr (std::is_same_v) { + if (const Symbol * sym{GetLastSymbol()}) { + return GetSymbolType(*sym); + } else { + return std::nullopt; + } + } else { + return Result::GetType(); + } +} std::optional ProcedureDesignator::GetType() const { if (const Symbol * symbol{GetSymbol()}) { return {GetSymbolType(*symbol)}; @@ -481,5 +543,5 @@ std::optional ProcedureDesignator::GetType() const { return std::nullopt; } -FOR_EACH_CHARACTER_KIND(template class Designator) +FOR_EACH_SPECIFIC_TYPE(template class Designator) } diff --git a/flang/lib/evaluate/variable.h b/flang/lib/evaluate/variable.h index 52e365c..a190308 100644 --- a/flang/lib/evaluate/variable.h +++ b/flang/lib/evaluate/variable.h @@ -68,7 +68,8 @@ public: DataRef &base() { return *base_; } const Symbol &symbol() const { return *symbol_; } int Rank() const; - const Symbol *GetSymbol(bool first) const; + const Symbol *GetFirstSymbol() const; + const Symbol *GetLastSymbol() const; Expr LEN() const; std::ostream &Dump(std::ostream &) const; @@ -117,7 +118,8 @@ struct ArrayRef { : u{std::move(c)}, subscript(std::move(ss)) {} int Rank() const; - const Symbol *GetSymbol(bool first) const; + const Symbol *GetFirstSymbol() const; + const Symbol *GetLastSymbol() const; Expr LEN() const; std::ostream &Dump(std::ostream &) const; @@ -144,13 +146,8 @@ public: CoarrayRef &set_team(Expr &&, bool isTeamNumber = false); int Rank() const; - const Symbol *GetSymbol(bool first) const { - if (first) { - return base_.front(); - } else { - return base_.back(); - } - } + const Symbol *GetFirstSymbol() const { return base_.front(); } + const Symbol *GetLastSymbol() const { return base_.back(); } Expr LEN() const; std::ostream &Dump(std::ostream &) const; @@ -171,7 +168,8 @@ struct DataRef { explicit DataRef(const Symbol &n) : u{&n} {} int Rank() const; - const Symbol *GetSymbol(bool first) const; + const Symbol *GetFirstSymbol() const; + const Symbol *GetLastSymbol() const; Expr LEN() const; std::ostream &Dump(std::ostream &) const; @@ -196,7 +194,8 @@ public: Expr first() const; Expr last() const; int Rank() const; - const Symbol *GetSymbol(bool first) const; + const Symbol *GetFirstSymbol() const; + const Symbol *GetLastSymbol() const; Expr LEN() const; std::optional Fold(FoldingContext &); std::ostream &Dump(std::ostream &) const; @@ -220,9 +219,8 @@ public: const DataRef &complex() const { return complex_; } Part part() const { return part_; } int Rank() const; - const Symbol *GetSymbol(bool first) const { - return complex_.GetSymbol(first); - } + const Symbol *GetFirstSymbol() const { return complex_.GetFirstSymbol(); } + const Symbol *GetLastSymbol() const { return complex_.GetLastSymbol(); } std::ostream &Dump(std::ostream &) const; private: @@ -253,41 +251,12 @@ public: Designator(DataRef &&that) : u{common::MoveVariant(std::move(that.u))} {} - std::optional GetType() const { - if constexpr (std::is_same_v) { - if (const Symbol * sym{GetSymbol(false)}) { - return GetSymbolType(*sym); - } else { - return std::nullopt; - } - } else { - return Result::GetType(); - } - } - - int Rank() const { - return std::visit( - common::visitors{[](const Symbol *sym) { return GetSymbolRank(*sym); }, - [](const auto &x) { return x.Rank(); }}, - u); - } - - const Symbol *GetSymbol(bool first) const { - return std::visit(common::visitors{[](const Symbol *sym) { return sym; }, - [=](const auto &x) { return x.GetSymbol(first); }}, - u); - } - + std::optional GetType() const; + int Rank() const; + const Symbol *GetFirstSymbol() const; + const Symbol *GetLastSymbol() const; Expr LEN() const; - - std::ostream &Dump(std::ostream &o) const { - std::visit(common::visitors{[&](const Symbol *sym) { - o << GetSymbolName(*sym).ToString(); - }, - [&](const auto &x) { x.Dump(o); }}, - u); - return o; - } + std::ostream &Dump(std::ostream &o) const; Variant u; }; diff --git a/flang/lib/semantics/expression.cc b/flang/lib/semantics/expression.cc index 9770109..f1bc7e4 100644 --- a/flang/lib/semantics/expression.cc +++ b/flang/lib/semantics/expression.cc @@ -520,7 +520,7 @@ MaybeExpr TypedWrapper(DynamicType &&dyType, WRAPPED &&x) { // Wraps a data reference in a typed Designator<>. static MaybeExpr Designate(DataRef &&dataRef) { - const Symbol &symbol{*dataRef.GetSymbol(false)}; + const Symbol &symbol{*dataRef.GetLastSymbol()}; if (std::optional dyType{GetSymbolType(symbol)}) { return TypedWrapper( std::move(*dyType), std::move(dataRef)); @@ -577,7 +577,7 @@ MaybeExpr ExprAnalyzer::Analyze(const parser::Substring &ss) { GetSubstringBound(std::get<0>(range.t))}; std::optional> last{ GetSubstringBound(std::get<1>(range.t))}; - const Symbol &symbol{*checked->GetSymbol(false)}; + const Symbol &symbol{*checked->GetLastSymbol()}; if (std::optional dynamicType{GetSymbolType(symbol)}) { if (dynamicType->category == TypeCategory::Character) { return WrapperHelper A(:,:) -- 2.7.4