From 943b50289b6ee51ffe2d4092dd7a545ff2b24058 Mon Sep 17 00:00:00 2001 From: peter klausler Date: Fri, 20 Jul 2018 13:52:52 -0700 Subject: [PATCH] [flang] Real->Integer conversion folding Original-commit: flang-compiler/f18@5ad6505e782c9f5d748ff0c2d06be0f02c283694 Reviewed-on: https://github.com/flang-compiler/f18/pull/144 Tree-same-pre-rewrite: false --- flang/lib/evaluate/expression.cc | 23 +++++++++++++++++++++++ flang/lib/evaluate/expression.h | 4 +--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/flang/lib/evaluate/expression.cc b/flang/lib/evaluate/expression.cc index 9b499cd7b29f..b453435214ab 100644 --- a/flang/lib/evaluate/expression.cc +++ b/flang/lib/evaluate/expression.cc @@ -241,6 +241,29 @@ auto IntegerExpr::ConvertInteger::FoldScalar(FoldingContext &context, c.u); } +template +auto IntegerExpr::ConvertReal::FoldScalar(FoldingContext &context, + const CategoryScalar &c) -> std::optional { + return std::visit( + [&](auto &x) -> std::optional { + auto converted{x.template ToInteger()}; + if (context.messages != nullptr) { + if (converted.flags.test(RealFlag::Overflow)) { + context.messages->Say( + context.at, "real->integer conversion overflowed"_en_US); + return {}; + } + if (converted.flags.test(RealFlag::InvalidArgument)) { + context.messages->Say( + context.at, "real->integer conversion: invalid argument"_en_US); + return {}; + } + } + return {std::move(converted.value)}; + }, + c.u); +} + template auto IntegerExpr::Negate::FoldScalar( FoldingContext &context, const Scalar &c) -> std::optional { diff --git a/flang/lib/evaluate/expression.h b/flang/lib/evaluate/expression.h index 899b45b00c62..263f4220d349 100644 --- a/flang/lib/evaluate/expression.h +++ b/flang/lib/evaluate/expression.h @@ -152,9 +152,7 @@ public: using Unary>::Unary; static std::optional FoldScalar( - FoldingContext &, const CategoryScalar &) { - return {}; - } // TODO + FoldingContext &, const CategoryScalar &); }; template using Un = Unary;