forked from OSchip/llvm-project
[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:
parent
fd6312ea05
commit
ab9d0987a2
|
@ -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>
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue