From: peter klausler Date: Tue, 14 Aug 2018 00:05:15 +0000 (-0700) Subject: [flang] clean up naming in expressions and types X-Git-Tag: llvmorg-12-init~9537^2~2196 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=94540975f393bbc9a9b43da7d2f46142890e8135;p=platform%2Fupstream%2Fllvm.git [flang] clean up naming in expressions and types Original-commit: flang-compiler/f18@ab17ef4d4bc7e5ab8fae2dbe43edd47f25f9ba5a Reviewed-on: https://github.com/flang-compiler/f18/pull/183 Tree-same-pre-rewrite: false --- diff --git a/flang/lib/evaluate/expression.cc b/flang/lib/evaluate/expression.cc index 717e401..338f564 100644 --- a/flang/lib/evaluate/expression.cc +++ b/flang/lib/evaluate/expression.cc @@ -236,7 +236,7 @@ auto Binary::Fold(FoldingContext &context) template auto IntegerExpr::ConvertInteger::FoldScalar(FoldingContext &context, - const ScalarConstant &c) -> std::optional { + const SomeKindScalar &c) -> std::optional { return std::visit( [&](auto &x) -> std::optional { auto converted{Scalar::ConvertSigned(x)}; @@ -251,7 +251,7 @@ auto IntegerExpr::ConvertInteger::FoldScalar(FoldingContext &context, template auto IntegerExpr::ConvertReal::FoldScalar(FoldingContext &context, - const ScalarConstant &c) -> std::optional { + const SomeKindScalar &c) -> std::optional { return std::visit( [&](auto &x) -> std::optional { auto converted{x.template ToInteger()}; @@ -369,7 +369,7 @@ template auto IntegerExpr::Fold(FoldingContext &context) -> std::optional { return std::visit( [&](auto &x) -> std::optional { - using Ty = typename std::decay::type; + using Ty = std::decay_t; if constexpr (std::is_same_v) { return {x}; } @@ -387,7 +387,7 @@ auto IntegerExpr::Fold(FoldingContext &context) -> std::optional { template auto RealExpr::ConvertInteger::FoldScalar(FoldingContext &context, - const ScalarConstant &c) -> std::optional { + const SomeKindScalar &c) -> std::optional { return std::visit( [&](auto &x) -> std::optional { auto converted{Scalar::FromInteger(x)}; @@ -399,7 +399,7 @@ auto RealExpr::ConvertInteger::FoldScalar(FoldingContext &context, template auto RealExpr::ConvertReal::FoldScalar(FoldingContext &context, - const ScalarConstant &c) -> std::optional { + const SomeKindScalar &c) -> std::optional { return std::visit( [&](auto &x) -> std::optional { auto converted{Scalar::Convert(x)}; @@ -455,7 +455,7 @@ auto RealExpr::Power::FoldScalar(FoldingContext &context, const Scalar &a, template auto RealExpr::IntPower::FoldScalar(FoldingContext &context, - const Scalar &a, const ScalarConstant &b) + const Scalar &a, const SomeKindScalar &b) -> std::optional { return std::visit( [&](const auto &pow) -> std::optional { @@ -486,13 +486,13 @@ auto RealExpr::Min::FoldScalar(FoldingContext &context, const Scalar &a, template auto RealExpr::RealPart::FoldScalar(FoldingContext &context, - const SameKindComplexScalar &z) -> std::optional { + const evaluate::Scalar &z) -> std::optional { return {z.REAL()}; } template auto RealExpr::AIMAG::FoldScalar(FoldingContext &context, - const SameKindComplexScalar &z) -> std::optional { + const evaluate::Scalar &z) -> std::optional { return {z.AIMAG()}; } @@ -500,7 +500,7 @@ template auto RealExpr::Fold(FoldingContext &context) -> std::optional { return std::visit( [&](auto &x) -> std::optional { - using Ty = typename std::decay::type; + using Ty = std::decay_t; if constexpr (std::is_same_v) { return {x}; } @@ -565,7 +565,7 @@ auto ComplexExpr::Power::FoldScalar(FoldingContext &context, template auto ComplexExpr::IntPower::FoldScalar(FoldingContext &context, - const Scalar &a, const ScalarConstant &b) + const Scalar &a, const SomeKindScalar &b) -> std::optional { return std::visit( [&](const auto &pow) -> std::optional { @@ -578,7 +578,7 @@ auto ComplexExpr::IntPower::FoldScalar(FoldingContext &context, template auto ComplexExpr::CMPLX::FoldScalar(FoldingContext &context, - const SameKindRealScalar &a, const SameKindRealScalar &b) + const evaluate::Scalar &a, const evaluate::Scalar &b) -> std::optional { return {Scalar{a, b}}; } @@ -587,7 +587,7 @@ template auto ComplexExpr::Fold(FoldingContext &context) -> std::optional { return std::visit( [&](auto &x) -> std::optional { - using Ty = typename std::decay::type; + using Ty = std::decay_t; if constexpr (std::is_same_v) { return {x}; } @@ -638,7 +638,7 @@ auto CharacterExpr::Fold(FoldingContext &context) -> std::optional { return std::visit( [&](auto &x) -> std::optional { - using Ty = typename std::decay::type; + using Ty = std::decay_t; if constexpr (std::is_same_v) { return {x}; } @@ -740,7 +740,7 @@ template auto LogicalExpr::Fold(FoldingContext &context) -> std::optional { return std::visit( [&](auto &x) -> std::optional { - using Ty = typename std::decay::type; + using Ty = std::decay_t; if constexpr (std::is_same_v) { return {x}; } diff --git a/flang/lib/evaluate/expression.h b/flang/lib/evaluate/expression.h index 0b0f3cc..c8ab9a9 100644 --- a/flang/lib/evaluate/expression.h +++ b/flang/lib/evaluate/expression.h @@ -38,11 +38,11 @@ template class Unary { protected: using OperandType = A; using Operand = Expr; - using OperandScalarConstant = typename OperandType::Value; + using OperandScalarConstant = Scalar; public: using Result = RESULT; - using Scalar = typename Result::Value; + using Scalar = Scalar; using FoldableTrait = std::true_type; CLASS_BOILERPLATE(Unary) Unary(const Operand &a) : operand_{a} {} @@ -62,14 +62,14 @@ class Binary { protected: using LeftType = A; using Left = Expr; - using LeftScalar = typename LeftType::Value; + using LeftScalar = Scalar; using RightType = B; using Right = Expr; - using RightScalar = typename RightType::Value; + using RightScalar = Scalar; public: using Result = RESULT; - using Scalar = typename Result::Value; + using Scalar = Scalar; using FoldableTrait = std::true_type; CLASS_BOILERPLATE(Binary) Binary(const Left &a, const Right &b) : left_{a}, right_{b} {} @@ -96,21 +96,21 @@ private: template class Expr> { public: using Result = Type; - using Scalar = typename Result::Value; + using Scalar = Scalar; using FoldableTrait = std::true_type; struct ConvertInteger : public Unary> { using Unary>::Unary; static std::optional FoldScalar( - FoldingContext &, const ScalarConstant &); + FoldingContext &, const SomeKindScalar &); }; struct ConvertReal : public Unary> { using Unary>::Unary; static std::optional FoldScalar( - FoldingContext &, const ScalarConstant &); + FoldingContext &, const SomeKindScalar &); }; template using Un = Unary; @@ -203,8 +203,9 @@ private: template class Expr> { public: using Result = Type; - using Scalar = typename Result::Value; + using Scalar = Scalar; using FoldableTrait = std::true_type; + using Complex = typename Result::Complex; // N.B. Real->Complex and Complex->Real conversions are done with CMPLX // and part access operations (resp.). Conversions between kinds of @@ -213,13 +214,13 @@ public: : public Unary> { using Unary>::Unary; static std::optional FoldScalar( - FoldingContext &, const ScalarConstant &); + FoldingContext &, const SomeKindScalar &); }; struct ConvertReal : public Unary> { using Unary>::Unary; static std::optional FoldScalar( - FoldingContext &, const ScalarConstant &); + FoldingContext &, const SomeKindScalar &); }; template using Un = Unary; template using Bin = Binary; @@ -263,7 +264,7 @@ public: using Binary>::Binary; static std::optional FoldScalar(FoldingContext &, const Scalar &, - const ScalarConstant &); + const SomeKindScalar &); }; struct Max : public Bin { using Bin::Bin; @@ -275,19 +276,16 @@ public: static std::optional FoldScalar( FoldingContext &, const Scalar &, const Scalar &); }; - using SameKindComplex = Type; - using SameKindComplexScalar = typename SameKindComplex::Value; - template - using SameKindComplexUn = Unary; - struct RealPart : public SameKindComplexUn { - using SameKindComplexUn::SameKindComplexUn; + template using ComplexUn = Unary; + struct RealPart : public ComplexUn { + using ComplexUn::ComplexUn; static std::optional FoldScalar( - FoldingContext &, const SameKindComplexScalar &); + FoldingContext &, const evaluate::Scalar &); }; - struct AIMAG : public SameKindComplexUn { - using SameKindComplexUn::SameKindComplexUn; + struct AIMAG : public ComplexUn { + using ComplexUn::ComplexUn; static std::optional FoldScalar( - FoldingContext &, const SameKindComplexScalar &); + FoldingContext &, const evaluate::Scalar &); }; CLASS_BOILERPLATE(Expr) @@ -326,8 +324,10 @@ private: template class Expr> { public: using Result = Type; - using Scalar = typename Result::Value; + using Scalar = Scalar; + using Part = typename Result::Part; using FoldableTrait = std::true_type; + template using Un = Unary; template using Bin = Binary; struct Parentheses : public Un { @@ -370,14 +370,12 @@ public: using Binary>::Binary; static std::optional FoldScalar(FoldingContext &, const Scalar &, - const ScalarConstant &); + const SomeKindScalar &); }; - using SameKindReal = Type; - using SameKindRealScalar = typename SameKindReal::Value; - struct CMPLX : public Binary { - using Binary::Binary; + struct CMPLX : public Binary { + using Binary::Binary; static std::optional FoldScalar(FoldingContext &, - const SameKindRealScalar &, const SameKindRealScalar &); + const evaluate::Scalar &, const evaluate::Scalar &); }; CLASS_BOILERPLATE(Expr) @@ -402,7 +400,7 @@ private: template class Expr> { public: using Result = Type; - using Scalar = typename Result::Value; + using Scalar = Scalar; using FoldableTrait = std::true_type; template using Bin = Binary; struct Concat : public Bin { @@ -483,7 +481,7 @@ extern template struct Comparison>; // Dynamically polymorphic comparisons whose operands are expressions of // the same supported kind of a particular type category. template struct CategoryComparison { - using Scalar = typename Type::Value; + using Scalar = Scalar>; CLASS_BOILERPLATE(CategoryComparison) template using KindComparison = Comparison>; template CategoryComparison(const KindComparison &x) : u{x} {} @@ -496,7 +494,7 @@ template struct CategoryComparison { template class Expr> { public: using Result = Type; - using Scalar = typename Result::Value; + using Scalar = Scalar; using FoldableTrait = std::true_type; struct Not : Unary { using Unary::Unary; @@ -578,7 +576,7 @@ extern template class Expr>; template class Expr> { public: using Result = SomeKind; - using Scalar = typename Result::Value; + using Scalar = Scalar; using FoldableTrait = std::true_type; CLASS_BOILERPLATE(Expr) template using KindExpr = Expr>; diff --git a/flang/lib/evaluate/tools.cc b/flang/lib/evaluate/tools.cc index 44a12dd87..2da628f 100644 --- a/flang/lib/evaluate/tools.cc +++ b/flang/lib/evaluate/tools.cc @@ -35,8 +35,8 @@ SomeKindRealExpr ConvertToTypeOf( static void ConvertToSameRealKind(SomeKindRealExpr &x, SomeKindRealExpr &y) { std::visit( [&](auto &xk, auto &yk) { - using xt = typename std::decay::type; - using yt = typename std::decay::type; + using xt = std::decay_t; + using yt = std::decay_t; constexpr int kindDiff{xt::Result::kind - yt::Result::kind}; if constexpr (kindDiff < 0) { x.u = yt{xk}; diff --git a/flang/lib/evaluate/type.cc b/flang/lib/evaluate/type.cc index aae0fec..c61048c 100644 --- a/flang/lib/evaluate/type.cc +++ b/flang/lib/evaluate/type.cc @@ -22,7 +22,7 @@ namespace Fortran::evaluate { std::optional GenericScalar::ToInt64() const { - if (const auto *j{std::get_if>(&u)}) { + if (const auto *j{std::get_if>(&u)}) { return std::visit( [](const auto &k) { return std::optional{k.ToInt64()}; }, j->u); @@ -31,7 +31,7 @@ std::optional GenericScalar::ToInt64() const { } std::optional GenericScalar::ToString() const { - if (const auto *c{std::get_if>(&u)}) { + if (const auto *c{std::get_if>(&u)}) { if (const std::string * s{std::get_if(&c->u)}) { return std::optional{*s}; } @@ -39,25 +39,33 @@ std::optional GenericScalar::ToString() const { return std::nullopt; } -// There's some opaque type-fu going on below. Given a GenericScalar, we -// figure out its intrinsic type category, and then (for each category), -// we figure out its kind from the type of the constant. Then, given -// the category, kind, and constant, we construct a GenericExpr around -// the constant. +// TODO pmk: maybe transplant these templates to type.h/expression.h? + +// There's some admittedly opaque type-fu going on below. +// Given a GenericScalar value, we want to be able to (re-)wrap it as +// a GenericExpr. So we extract its value, then build up an expression +// around it. The subtle magic is in the first template, whose result +// is a specific expression whose Fortran type category and kind are inferred +// from the type of the scalar constant. +template Expr> ScalarConstantToExpr(const A &x) { + return {x}; +} + +template +Expr> ToSomeKindExpr(const Expr &x) { + return {x}; +} + +template +Expr> SomeKindScalarToExpr(const SomeKindScalar &x) { + return std::visit( + [](const auto &c) { return ToSomeKindExpr(ScalarConstantToExpr(c)); }, + x.u); +} + GenericExpr GenericScalar::ToGenericExpr() const { return std::visit( - [](const auto &c) -> GenericExpr { - using cType = typename std::decay::type; - constexpr TypeCategory cat{cType::category}; - return {std::visit( - [&](const auto &value) -> Expr> { - using valueType = typename std::decay::type; - using Ty = typename TypeOfScalarValue::type; - return {Expr{value}}; - }, - c.u)}; - }, - u); + [&](const auto &c) { return GenericExpr{SomeKindScalarToExpr(c)}; }, u); } } // namespace Fortran::evaluate diff --git a/flang/lib/evaluate/type.h b/flang/lib/evaluate/type.h index 4220425..049d20a 100644 --- a/flang/lib/evaluate/type.h +++ b/flang/lib/evaluate/type.h @@ -39,6 +39,7 @@ struct GenericExpr; template struct TypeBase { static constexpr TypeCategory category{C}; + static constexpr TypeCategory GetCategory() { return C; }; static constexpr int kind{KIND}; static constexpr bool hasLen{false}; static std::string Dump() { @@ -46,43 +47,44 @@ template struct TypeBase { } }; +template using Scalar = typename T::Scalar; + template struct Type; template struct Type : public TypeBase { - using Value = value::Integer<8 * KIND>; + using Scalar = value::Integer<8 * KIND>; }; template<> struct Type : public TypeBase { - using Value = value::Real::Value, 11>; + using Scalar = value::Real>, 11>; using Complex = Type; }; template<> struct Type : public TypeBase { - using Value = value::Real::Value, 24>; - using Complex = Type; + using Scalar = value::Real>, 24>; + using Complex = Type; }; template<> struct Type : public TypeBase { - using Value = value::Real::Value, 53>; - using Complex = Type; + using Scalar = value::Real>, 53>; + using Complex = Type; }; template<> struct Type : public TypeBase { - using Value = value::Real, 64, false>; - using Complex = Type; + using Scalar = value::Real, 64, false>; + using Complex = Type; }; template<> struct Type : public TypeBase { - using Value = - value::Real::Value, 112>; - using Complex = Type; + using Scalar = value::Real, 112>; + using Complex = Type; }; // The KIND type parameter on COMPLEX is the kind of each of its components. @@ -90,14 +92,14 @@ template struct Type : public TypeBase { using Part = Type; - using Value = value::Complex; + using Scalar = value::Complex>; }; template struct Type { static constexpr TypeCategory category{TypeCategory::Character}; static constexpr int kind{KIND}; static constexpr bool hasLen{true}; - using Value = std::string; + using Scalar = std::string; static std::string Dump() { return EnumToString(category) + '(' + std::to_string(kind) + ')'; } @@ -106,7 +108,7 @@ template struct Type { template struct Type : public TypeBase { - using Value = value::Logical<8 * KIND>; + using Scalar = value::Logical<8 * KIND>; }; // Convenience type aliases: @@ -139,8 +141,7 @@ using SubscriptInteger = Type; #define FOR_EACH_CATEGORY(M) \ M(Integer, INTEGER) \ - M(Real, REAL) M(Complex, COMPLEX) M(Character, CHARACTER) \ - M(Logical, LOGICAL) + M(Real, REAL) M(Complex, COMPLEX) M(Character, CHARACTER) M(Logical, LOGICAL) // These macros and template create instances of std::variant<> that can contain // applications of some class template to all of the supported kinds of @@ -155,13 +156,13 @@ FOR_EACH_CATEGORY(MAKE) #undef MAKE #undef TKIND -// Map scalar constant value types back to their Fortran types. -// For every type T = Type, TypeOfScalarValue::type == T. +// Map scalar value types back to their Fortran types. +// For every type T = Type, TypeOfScalarValue> == T. // E.g., TypeOfScalarValue> is Type. -template struct TypeOfScalarValue; +template struct GetTypeOfScalarValue; #define TOSV(cat, kind) \ template<> \ - struct TypeOfScalarValue::Value> { \ + struct GetTypeOfScalarValue>> { \ using type = Type; \ }; #define M(k) TOSV(Integer, k) @@ -181,16 +182,19 @@ FOR_EACH_LOGICAL_KIND(M, ) #undef M #undef TOSV -// Holds a scalar constant of any kind within a particular intrinsic type +template +using ScalarValueType = typename GetTypeOfScalarValue::type; + +// Holds a scalar value of any kind within a particular intrinsic type // category. -template struct ScalarConstant { +template struct SomeKindScalar { static constexpr TypeCategory category{CAT}; - CLASS_BOILERPLATE(ScalarConstant) + CLASS_BOILERPLATE(SomeKindScalar) - template using KindScalar = typename Type::Value; - template ScalarConstant(const A &x) : u{x} {} + template using KindScalar = Scalar>; + template SomeKindScalar(const A &x) : u{x} {} template - ScalarConstant(std::enable_if_t, A> &&x) + SomeKindScalar(std::enable_if_t, A> &&x) : u{std::move(x)} {} typename KindsVariant::type u; @@ -201,11 +205,10 @@ struct GenericScalar { CLASS_BOILERPLATE(GenericScalar) template - GenericScalar(const typename Type::Value &x) - : u{ScalarConstant{x}} {} + GenericScalar(const Scalar> &x) : u{SomeKindScalar{x}} {} template - GenericScalar(typename Type::Value &&x) - : u{ScalarConstant{std::move(x)}} {} + GenericScalar(Scalar> &&x) + : u{SomeKindScalar{std::move(x)}} {} template GenericScalar(const A &x) : u{x} {} template @@ -216,17 +219,17 @@ struct GenericScalar { std::optional ToString() const; GenericExpr ToGenericExpr() const; - std::variant, - ScalarConstant, ScalarConstant, - ScalarConstant, - ScalarConstant> + std::variant, + SomeKindScalar, SomeKindScalar, + SomeKindScalar, + SomeKindScalar> u; }; // Represents a type of any supported kind within a particular category. template struct SomeKind { static constexpr TypeCategory category{CAT}; - using Value = ScalarConstant; + using Scalar = SomeKindScalar; }; } // namespace Fortran::evaluate #endif // FORTRAN_EVALUATE_TYPE_H_ diff --git a/flang/lib/parser/basic-parsers.h b/flang/lib/parser/basic-parsers.h index 6c25d1b..47790df 100644 --- a/flang/lib/parser/basic-parsers.h +++ b/flang/lib/parser/basic-parsers.h @@ -311,7 +311,7 @@ private: state = backtrack; const auto &parser{std::get(ps_)}; static_assert(std::is_same_v::type::resultType>); + typename std::decay_t::resultType>); result = parser.Parse(state); if (!result.has_value()) { state.CombineFailedParses(std::move(prevState)); diff --git a/flang/lib/semantics/expression.cc b/flang/lib/semantics/expression.cc index f3ee629..5105f7d 100644 --- a/flang/lib/semantics/expression.cc +++ b/flang/lib/semantics/expression.cc @@ -269,7 +269,7 @@ static std::optional AnalyzeLiteral( if (sign == parser::Sign::Negative) { std::visit( [](auto &rk) { - using t = typename std::decay::type; + using t = std::decay_t; rk = typename t::Negate{rk}; }, result->u); @@ -600,8 +600,8 @@ ExpressionAnalyzer::ConstructComplex(MaybeExpr &&real, MaybeExpr &&imaginary) { if (auto joined{common::JoinOptionals(std::move(converted))}) { return {std::visit( [](auto &&rx, auto &&ix) -> evaluate::SomeKindComplexExpr { - using realExpr = typename std::decay::type; - using zExpr = evaluate::Expr; + using realExpr = std::decay_t; + using zExpr = evaluate::Expr; return {zExpr{typename zExpr::CMPLX{std::move(rx), std::move(ix)}}}; }, std::move(joined->first.u), std::move(joined->second.u))}; diff --git a/flang/test/evaluate/logical.cc b/flang/test/evaluate/logical.cc index 7c81dc9..5b92b79 100644 --- a/flang/test/evaluate/logical.cc +++ b/flang/test/evaluate/logical.cc @@ -22,7 +22,7 @@ template void testKind() { TEST(Type::category == Fortran::common::TypeCategory::Logical); TEST(Type::kind == KIND); TEST(!Type::hasLen); - using Value = typename Type::Value; + using Value = Fortran::evaluate::Scalar; MATCH(8 * KIND, Value::bits); TEST(!Value{}.IsTrue()); TEST(!Value{false}.IsTrue()); diff --git a/flang/test/evaluate/real.cc b/flang/test/evaluate/real.cc index 7e8f86e..e14c3c2 100644 --- a/flang/test/evaluate/real.cc +++ b/flang/test/evaluate/real.cc @@ -21,13 +21,13 @@ using namespace Fortran::evaluate; using namespace Fortran::common; -using Real2 = typename Type::Value; -using Real4 = typename Type::Value; -using Real8 = typename Type::Value; -using Real10 = typename Type::Value; -using Real16 = typename Type::Value; -using Integer4 = typename Type::Value; -using Integer8 = typename Type::Value; +using Real2 = Scalar>; +using Real4 = Scalar>; +using Real8 = Scalar>; +using Real10 = Scalar>; +using Real16 = Scalar>; +using Integer4 = Scalar>; +using Integer8 = Scalar>; void dumpTest() { struct {