[flang] character folding

Original-commit: flang-compiler/f18@5adc208bf2
Reviewed-on: https://github.com/flang-compiler/f18/pull/162
Tree-same-pre-rewrite: false
This commit is contained in:
peter klausler 2018-07-25 16:02:08 -07:00
parent fd6312ea05
commit ab9d0987a2
2 changed files with 49 additions and 1 deletions

View File

@ -620,10 +620,52 @@ auto ComplexExpr<KIND>::Fold(FoldingContext &context) -> std::optional<Scalar> {
u_);
}
template<int KIND>
auto CharacterExpr<KIND>::Concat::FoldScalar(FoldingContext &context,
const Scalar &a, const Scalar &b) -> std::optional<Scalar> {
if constexpr (KIND == 1) {
return {a + b};
}
return std::nullopt;
}
template<int KIND>
auto CharacterExpr<KIND>::Max::FoldScalar(FoldingContext &context,
const Scalar &a, const Scalar &b) -> std::optional<Scalar> {
if (Compare(a, b) == Ordering::Less) {
return {b};
}
return {a};
}
template<int KIND>
auto CharacterExpr<KIND>::Min::FoldScalar(FoldingContext &context,
const Scalar &a, const Scalar &b) -> std::optional<Scalar> {
if (Compare(a, b) == Ordering::Greater) {
return {b};
}
return {a};
}
template<int KIND>
auto CharacterExpr<KIND>::Fold(FoldingContext &context)
-> std::optional<Scalar> {
return std::nullopt; // TODO
return std::visit(
[&](auto &x) -> std::optional<Scalar> {
using Ty = typename std::decay<decltype(x)>::type;
if constexpr (std::is_same_v<Ty, Scalar>) {
return {x};
}
if constexpr (evaluate::FoldableTrait<Ty>) {
auto c{x.Fold(context)};
if (c.has_value()) {
u_ = *c;
return c;
}
}
return std::nullopt;
},
u_);
}
template<typename A>

View File

@ -405,12 +405,18 @@ public:
template<typename CRTP> using Bin = Binary<CRTP, Result>;
struct Concat : public Bin<Concat> {
using Bin<Concat>::Bin;
static std::optional<Scalar> FoldScalar(
FoldingContext &, const Scalar &, const Scalar &);
};
struct Max : public Bin<Max> {
using Bin<Max>::Bin;
static std::optional<Scalar> FoldScalar(
FoldingContext &, const Scalar &, const Scalar &);
};
struct Min : public Bin<Min> {
using Bin<Min>::Bin;
static std::optional<Scalar> FoldScalar(
FoldingContext &, const Scalar &, const Scalar &);
};
CLASS_BOILERPLATE(Expr)