FLANG
fold-implementation.h
1//===-- lib/Evaluate/fold-implementation.h --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_EVALUATE_FOLD_IMPLEMENTATION_H_
10#define FORTRAN_EVALUATE_FOLD_IMPLEMENTATION_H_
11
12#include "character.h"
13#include "host.h"
14#include "int-power.h"
15#include "flang/Common/indirection.h"
16#include "flang/Common/template.h"
17#include "flang/Common/unwrap.h"
18#include "flang/Evaluate/characteristics.h"
19#include "flang/Evaluate/common.h"
20#include "flang/Evaluate/constant.h"
21#include "flang/Evaluate/expression.h"
22#include "flang/Evaluate/fold.h"
23#include "flang/Evaluate/formatting.h"
24#include "flang/Evaluate/intrinsics-library.h"
25#include "flang/Evaluate/intrinsics.h"
26#include "flang/Evaluate/shape.h"
27#include "flang/Evaluate/tools.h"
28#include "flang/Evaluate/traverse.h"
29#include "flang/Evaluate/type.h"
30#include "flang/Parser/message.h"
31#include "flang/Semantics/scope.h"
32#include "flang/Semantics/symbol.h"
33#include "flang/Semantics/tools.h"
34#include <algorithm>
35#include <cmath>
36#include <complex>
37#include <cstdio>
38#include <optional>
39#include <type_traits>
40#include <variant>
41
42// Some environments, viz. glibc 2.17 and *BSD, allow the macro HUGE
43// to leak out of <math.h>.
44#undef HUGE
45
46namespace Fortran::evaluate {
47
48// Don't use Kahan extended precision summation any more when folding
49// transformational intrinsic functions other than SUM, since it is
50// not used in the runtime implementations of those functions and we
51// want results to match.
52static constexpr bool useKahanSummation{false};
53
54// Utilities
55template <typename T> class Folder {
56public:
57 explicit Folder(FoldingContext &c, bool forOptionalArgument = false)
58 : context_{c}, forOptionalArgument_{forOptionalArgument} {}
59 std::optional<Constant<T>> GetNamedConstant(const Symbol &);
60 std::optional<Constant<T>> ApplySubscripts(const Constant<T> &array,
61 const std::vector<Constant<SubscriptInteger>> &subscripts);
62 std::optional<Constant<T>> ApplyComponent(Constant<SomeDerived> &&,
63 const Symbol &component,
64 const std::vector<Constant<SubscriptInteger>> * = nullptr);
65 std::optional<Constant<T>> GetConstantComponent(
66 Component &, const std::vector<Constant<SubscriptInteger>> * = nullptr);
67 std::optional<Constant<T>> Folding(ArrayRef &);
68 std::optional<Constant<T>> Folding(DataRef &);
69 Expr<T> Folding(Designator<T> &&);
70 Constant<T> *Folding(std::optional<ActualArgument> &);
71
72 Expr<T> CSHIFT(FunctionRef<T> &&);
73 Expr<T> EOSHIFT(FunctionRef<T> &&);
74 Expr<T> MERGE(FunctionRef<T> &&);
75 Expr<T> PACK(FunctionRef<T> &&);
76 Expr<T> RESHAPE(FunctionRef<T> &&);
77 Expr<T> SPREAD(FunctionRef<T> &&);
78 Expr<T> TRANSPOSE(FunctionRef<T> &&);
79 Expr<T> UNPACK(FunctionRef<T> &&);
80
81 Expr<T> TRANSFER(FunctionRef<T> &&);
82
83private:
84 FoldingContext &context_;
85 bool forOptionalArgument_{false};
86};
87
88std::optional<Constant<SubscriptInteger>> GetConstantSubscript(
89 FoldingContext &, Subscript &, const NamedEntity &, int dim);
90
91// Helper to use host runtime on scalars for folding.
92template <typename TR, typename... TA>
93std::optional<std::function<Scalar<TR>(FoldingContext &, Scalar<TA>...)>>
94GetHostRuntimeWrapper(const std::string &name) {
95 std::vector<DynamicType> argTypes{TA{}.GetType()...};
96 if (auto hostWrapper{GetHostRuntimeWrapper(name, TR{}.GetType(), argTypes)}) {
97 return [hostWrapper](
98 FoldingContext &context, Scalar<TA>... args) -> Scalar<TR> {
99 std::vector<Expr<SomeType>> genericArgs{
100 AsGenericExpr(Constant<TA>{args})...};
101 return GetScalarConstantValue<TR>(
102 (*hostWrapper)(context, std::move(genericArgs)))
103 .value();
104 };
105 }
106 return std::nullopt;
107}
108
109// FoldOperation() rewrites expression tree nodes.
110// If there is any possibility that the rewritten node will
111// not have the same representation type, the result of
112// FoldOperation() will be packaged in an Expr<> of the same
113// specific type.
114
115// no-op base case
116template <typename A>
117common::IfNoLvalue<Expr<ResultType<A>>, A> FoldOperation(
118 FoldingContext &, A &&x) {
119 static_assert(!std::is_same_v<A, Expr<ResultType<A>>>,
120 "call Fold() instead for Expr<>");
121 return Expr<ResultType<A>>{std::move(x)};
122}
123
124Component FoldOperation(FoldingContext &, Component &&);
125NamedEntity FoldOperation(FoldingContext &, NamedEntity &&);
126Triplet FoldOperation(FoldingContext &, Triplet &&);
127Subscript FoldOperation(FoldingContext &, Subscript &&);
128ArrayRef FoldOperation(FoldingContext &, ArrayRef &&);
129CoarrayRef FoldOperation(FoldingContext &, CoarrayRef &&);
130DataRef FoldOperation(FoldingContext &, DataRef &&);
131Substring FoldOperation(FoldingContext &, Substring &&);
132ComplexPart FoldOperation(FoldingContext &, ComplexPart &&);
133template <typename T>
134Expr<T> FoldOperation(FoldingContext &, FunctionRef<T> &&);
135template <typename T>
136Expr<T> FoldOperation(FoldingContext &context, Designator<T> &&designator) {
137 return Folder<T>{context}.Folding(std::move(designator));
138}
141Expr<ImpliedDoIndex::Result> FoldOperation(
142 FoldingContext &context, ImpliedDoIndex &&);
143template <typename T>
144Expr<T> FoldOperation(FoldingContext &, ArrayConstructor<T> &&);
146template <typename T>
147Expr<T> FoldOperation(FoldingContext &, ConditionalExpr<T> &&);
148
149template <typename T>
150std::optional<Constant<T>> Folder<T>::GetNamedConstant(const Symbol &symbol0) {
151 const Symbol &symbol{ResolveAssociations(symbol0)};
152 if (IsNamedConstant(symbol)) {
153 if (const auto *object{
154 symbol.detailsIf<semantics::ObjectEntityDetails>()}) {
155 if (const auto *constant{UnwrapConstantValue<T>(object->init())}) {
156 return *constant;
157 }
158 }
159 }
160 return std::nullopt;
161}
162
163template <typename T>
164std::optional<Constant<T>> Folder<T>::Folding(ArrayRef &aRef) {
165 std::vector<Constant<SubscriptInteger>> subscripts;
166 int dim{0};
167 for (Subscript &ss : aRef.subscript()) {
168 if (auto constant{GetConstantSubscript(context_, ss, aRef.base(), dim++)}) {
169 subscripts.emplace_back(std::move(*constant));
170 } else {
171 return std::nullopt;
172 }
173 }
174 if (Component * component{aRef.base().UnwrapComponent()}) {
175 return GetConstantComponent(*component, &subscripts);
176 } else if (std::optional<Constant<T>> array{
177 GetNamedConstant(aRef.base().GetLastSymbol())}) {
178 return ApplySubscripts(*array, subscripts);
179 } else {
180 return std::nullopt;
181 }
182}
183
184template <typename T>
185std::optional<Constant<T>> Folder<T>::Folding(DataRef &ref) {
186 return common::visit(
187 common::visitors{
188 [this](SymbolRef &sym) { return GetNamedConstant(*sym); },
189 [this](Component &comp) {
190 comp = FoldOperation(context_, std::move(comp));
191 return GetConstantComponent(comp);
192 },
193 [this](ArrayRef &aRef) {
194 aRef = FoldOperation(context_, std::move(aRef));
195 return Folding(aRef);
196 },
197 [](CoarrayRef &) { return std::optional<Constant<T>>{}; },
198 },
199 ref.u);
200}
201
202// TODO: This would be more natural as a member function of Constant<T>.
203template <typename T>
204std::optional<Constant<T>> Folder<T>::ApplySubscripts(const Constant<T> &array,
205 const std::vector<Constant<SubscriptInteger>> &subscripts) {
206 const auto &shape{array.shape()};
207 const auto &lbounds{array.lbounds()};
208 int rank{GetRank(shape)};
209 CHECK(rank == static_cast<int>(subscripts.size()));
210 std::size_t elements{1};
211 ConstantSubscripts resultShape;
212 ConstantSubscripts ssLB;
213 for (const auto &ss : subscripts) {
214 if (ss.Rank() == 1) {
215 resultShape.push_back(static_cast<ConstantSubscript>(ss.size()));
216 elements *= ss.size();
217 ssLB.push_back(ss.lbounds().front());
218 } else if (ss.Rank() > 1) {
219 return std::nullopt; // error recovery
220 }
221 }
222 ConstantSubscripts ssAt(rank, 0), at(rank, 0), tmp(1, 0);
223 std::vector<Scalar<T>> values;
224 while (elements-- > 0) {
225 bool increment{true};
226 int k{0};
227 for (int j{0}; j < rank; ++j) {
228 if (subscripts[j].Rank() == 0) {
229 at[j] = subscripts[j].GetScalarValue().value().ToInt64();
230 } else {
231 CHECK(k < GetRank(resultShape));
232 tmp[0] = ssLB.at(k) + ssAt.at(k);
233 at[j] = subscripts[j].At(tmp).ToInt64();
234 if (increment) {
235 if (++ssAt[k] == resultShape[k]) {
236 ssAt[k] = 0;
237 } else {
238 increment = false;
239 }
240 }
241 ++k;
242 }
243 if (at[j] < lbounds[j] || at[j] >= lbounds[j] + shape[j]) {
244 context_.messages().Say(
245 "Subscript value (%jd) is out of range on dimension %d in reference to a constant array value"_err_en_US,
246 at[j], j + 1);
247 return std::nullopt;
248 }
249 }
250 values.emplace_back(array.At(at));
251 CHECK(!increment || elements == 0);
252 CHECK(k == GetRank(resultShape));
253 }
254 if constexpr (T::category == TypeCategory::Character) {
255 return Constant<T>{array.LEN(), std::move(values), std::move(resultShape)};
256 } else if constexpr (std::is_same_v<T, SomeDerived>) {
257 return Constant<T>{array.result().derivedTypeSpec(), std::move(values),
258 std::move(resultShape)};
259 } else {
260 return Constant<T>{std::move(values), std::move(resultShape)};
261 }
262}
263
264template <typename T>
265std::optional<Constant<T>> Folder<T>::ApplyComponent(
266 Constant<SomeDerived> &&structures, const Symbol &component,
267 const std::vector<Constant<SubscriptInteger>> *subscripts) {
268 if (auto scalar{structures.GetScalarValue()}) {
269 if (std::optional<Expr<SomeType>> expr{scalar->Find(component)}) {
270 if (const Constant<T> *value{UnwrapConstantValue<T>(*expr)}) {
271 if (subscripts) {
272 return ApplySubscripts(*value, *subscripts);
273 } else {
274 return *value;
275 }
276 }
277 }
278 } else {
279 // A(:)%scalar_component & A(:)%array_component(subscripts)
280 std::unique_ptr<ArrayConstructor<T>> array;
281 if (structures.empty()) {
282 return std::nullopt;
283 }
284 ConstantSubscripts at{structures.lbounds()};
285 do {
286 StructureConstructor scalar{structures.At(at)};
287 if (std::optional<Expr<SomeType>> expr{scalar.Find(component)}) {
288 if (const Constant<T> *value{UnwrapConstantValue<T>(expr.value())}) {
289 if (!array.get()) {
290 // This technique ensures that character length or derived type
291 // information is propagated to the array constructor.
292 auto *typedExpr{UnwrapExpr<Expr<T>>(expr.value())};
293 CHECK(typedExpr);
294 array = std::make_unique<ArrayConstructor<T>>(*typedExpr);
295 if constexpr (T::category == TypeCategory::Character) {
296 array->set_LEN(Expr<SubscriptInteger>{value->LEN()});
297 }
298 }
299 if (subscripts) {
300 if (auto element{ApplySubscripts(*value, *subscripts)}) {
301 CHECK(element->Rank() == 0);
302 array->Push(Expr<T>{std::move(*element)});
303 } else {
304 return std::nullopt;
305 }
306 } else {
307 CHECK(value->Rank() == 0);
308 array->Push(Expr<T>{*value});
309 }
310 } else {
311 return std::nullopt;
312 }
313 }
314 } while (structures.IncrementSubscripts(at));
315 // Fold the ArrayConstructor<> into a Constant<>.
316 CHECK(array);
317 Expr<T> result{Fold(context_, Expr<T>{std::move(*array)})};
318 if (auto *constant{UnwrapConstantValue<T>(result)}) {
319 return constant->Reshape(common::Clone(structures.shape()));
320 }
321 }
322 return std::nullopt;
323}
324
325template <typename T>
326std::optional<Constant<T>> Folder<T>::GetConstantComponent(Component &component,
327 const std::vector<Constant<SubscriptInteger>> *subscripts) {
328 if (std::optional<Constant<SomeDerived>> structures{common::visit(
329 common::visitors{
330 [&](const Symbol &symbol) {
331 return Folder<SomeDerived>{context_}.GetNamedConstant(symbol);
332 },
333 [&](ArrayRef &aRef) {
334 return Folder<SomeDerived>{context_}.Folding(aRef);
335 },
336 [&](Component &base) {
337 return Folder<SomeDerived>{context_}.GetConstantComponent(base);
338 },
339 [&](CoarrayRef &) {
340 return std::optional<Constant<SomeDerived>>{};
341 },
342 },
343 component.base().u)}) {
344 return ApplyComponent(
345 std::move(*structures), component.GetLastSymbol(), subscripts);
346 } else {
347 return std::nullopt;
348 }
349}
350
351template <typename T> Expr<T> Folder<T>::Folding(Designator<T> &&designator) {
352 if constexpr (T::category == TypeCategory::Character) {
353 if (auto *substring{common::Unwrap<Substring>(designator.u)}) {
354 if (std::optional<Expr<SomeCharacter>> folded{
355 substring->Fold(context_)}) {
356 if (const auto *specific{std::get_if<Expr<T>>(&folded->u)}) {
357 return std::move(*specific);
358 }
359 }
360 // We used to fold zero-length substrings into zero-length
361 // constants here, but that led to problems in variable
362 // definition contexts.
363 }
364 } else if constexpr (T::category == TypeCategory::Real) {
365 if (auto *zPart{std::get_if<ComplexPart>(&designator.u)}) {
366 *zPart = FoldOperation(context_, std::move(*zPart));
368 if (auto zConst{Folder<ComplexT>{context_}.Folding(zPart->complex())}) {
369 return Fold(context_,
371 zPart->part() == ComplexPart::Part::IM,
372 Expr<ComplexT>{std::move(*zConst)}}});
373 } else {
374 return Expr<T>{Designator<T>{std::move(*zPart)}};
375 }
376 }
377 }
378 return common::visit(
379 common::visitors{
380 [&](SymbolRef &&symbol) {
381 if (auto constant{GetNamedConstant(*symbol)}) {
382 return Expr<T>{std::move(*constant)};
383 }
384 return Expr<T>{std::move(designator)};
385 },
386 [&](ArrayRef &&aRef) {
387 aRef = FoldOperation(context_, std::move(aRef));
388 if (auto c{Folding(aRef)}) {
389 return Expr<T>{std::move(*c)};
390 } else {
391 return Expr<T>{Designator<T>{std::move(aRef)}};
392 }
393 },
394 [&](Component &&component) {
395 component = FoldOperation(context_, std::move(component));
396 if (auto c{GetConstantComponent(component)}) {
397 return Expr<T>{std::move(*c)};
398 } else {
399 return Expr<T>{Designator<T>{std::move(component)}};
400 }
401 },
402 [&](auto &&x) {
403 return Expr<T>{
404 Designator<T>{FoldOperation(context_, std::move(x))}};
405 },
406 },
407 std::move(designator.u));
408}
409
410// Apply type conversion and re-folding if necessary.
411// This is where BOZ arguments are converted.
412template <typename T>
413Constant<T> *Folder<T>::Folding(std::optional<ActualArgument> &arg) {
414 if (auto *expr{UnwrapExpr<Expr<SomeType>>(arg)}) {
415 *expr = Fold(context_, std::move(*expr));
416 if constexpr (T::category != TypeCategory::Derived) {
417 if (!UnwrapExpr<Expr<T>>(*expr)) {
418 if (const Symbol *
419 var{forOptionalArgument_
420 ? UnwrapWholeSymbolOrComponentDataRef(*expr)
421 : nullptr};
422 var && (IsOptional(*var) || IsAllocatableOrObjectPointer(var))) {
423 // can't safely convert item that may not be present
424 } else if (auto converted{
425 ConvertToType(T::GetType(), std::move(*expr))}) {
426 *expr = Fold(context_, std::move(*converted));
427 }
428 }
429 }
430 return UnwrapConstantValue<T>(*expr);
431 }
432 return nullptr;
433}
434
435template <typename... A, std::size_t... I>
436std::optional<std::tuple<const Constant<A> *...>> GetConstantArgumentsHelper(
437 FoldingContext &context, ActualArguments &arguments,
438 bool hasOptionalArgument, std::index_sequence<I...>) {
439 static_assert(sizeof...(A) > 0);
440 std::tuple<const Constant<A> *...> args{
441 Folder<A>{context, hasOptionalArgument}.Folding(arguments.at(I))...};
442 if ((... && (std::get<I>(args)))) {
443 return args;
444 } else {
445 return std::nullopt;
446 }
447}
448
449template <typename... A>
450std::optional<std::tuple<const Constant<A> *...>> GetConstantArguments(
451 FoldingContext &context, ActualArguments &args, bool hasOptionalArgument) {
452 return GetConstantArgumentsHelper<A...>(
453 context, args, hasOptionalArgument, std::index_sequence_for<A...>{});
454}
455
456template <typename... A, std::size_t... I>
457std::optional<std::tuple<Scalar<A>...>> GetScalarConstantArgumentsHelper(
458 FoldingContext &context, ActualArguments &args, bool hasOptionalArgument,
459 std::index_sequence<I...>) {
460 if (auto constArgs{
461 GetConstantArguments<A...>(context, args, hasOptionalArgument)}) {
462 return std::tuple<Scalar<A>...>{
463 std::get<I>(*constArgs)->GetScalarValue().value()...};
464 } else {
465 return std::nullopt;
466 }
467}
468
469template <typename... A>
470std::optional<std::tuple<Scalar<A>...>> GetScalarConstantArguments(
471 FoldingContext &context, ActualArguments &args, bool hasOptionalArgument) {
472 return GetScalarConstantArgumentsHelper<A...>(
473 context, args, hasOptionalArgument, std::index_sequence_for<A...>{});
474}
475
476// helpers to fold intrinsic function references
477// Define callable types used in a common utility that
478// takes care of array and cast/conversion aspects for elemental intrinsics
479
480template <typename TR, typename... TArgs>
481using ScalarFunc = std::function<Scalar<TR>(const Scalar<TArgs> &...)>;
482template <typename TR, typename... TArgs>
483using ScalarFuncWithContext =
484 std::function<Scalar<TR>(FoldingContext &, const Scalar<TArgs> &...)>;
485
486template <template <typename, typename...> typename WrapperType, typename TR,
487 typename... TA, std::size_t... I>
488Expr<TR> FoldElementalIntrinsicHelper(FoldingContext &context,
489 FunctionRef<TR> &&funcRef, WrapperType<TR, TA...> func,
490 bool hasOptionalArgument, std::index_sequence<I...>) {
491 if (std::optional<std::tuple<const Constant<TA> *...>> args{
492 GetConstantArguments<TA...>(
493 context, funcRef.arguments(), hasOptionalArgument)}) {
494 // Compute the shape of the result based on shapes of arguments
495 ConstantSubscripts shape;
496 int rank{0};
497 const ConstantSubscripts *shapes[]{&std::get<I>(*args)->shape()...};
498 const int ranks[]{std::get<I>(*args)->Rank()...};
499 for (unsigned int i{0}; i < sizeof...(TA); ++i) {
500 if (ranks[i] > 0) {
501 if (rank == 0) {
502 rank = ranks[i];
503 shape = *shapes[i];
504 } else {
505 if (shape != *shapes[i]) {
506 // TODO: Rank compatibility was already checked but it seems to be
507 // the first place where the actual shapes are checked to be the
508 // same. Shouldn't this be checked elsewhere so that this is also
509 // checked for non constexpr call to elemental intrinsics function?
510 context.messages().Say(
511 "Arguments in elemental intrinsic function are not conformable"_err_en_US);
512 return Expr<TR>{std::move(funcRef)};
513 }
514 }
515 }
516 }
517 CHECK(rank == GetRank(shape));
518 // Compute all the scalar values of the results
519 std::vector<Scalar<TR>> results;
520 std::optional<uint64_t> n{TotalElementCount(shape)};
521 if (!n) {
522 context.messages().Say(
523 "Too many elements in elemental intrinsic function result"_err_en_US);
524 return Expr<TR>{std::move(funcRef)};
525 }
526 if (*n > 0) {
527 ConstantBounds bounds{shape};
528 ConstantSubscripts resultIndex(rank, 1);
529 ConstantSubscripts argIndex[]{std::get<I>(*args)->lbounds()...};
530 do {
531 if constexpr (std::is_same_v<WrapperType<TR, TA...>,
532 ScalarFuncWithContext<TR, TA...>>) {
533 results.emplace_back(
534 func(context, std::get<I>(*args)->At(argIndex[I])...));
535 } else if constexpr (std::is_same_v<WrapperType<TR, TA...>,
536 ScalarFunc<TR, TA...>>) {
537 results.emplace_back(func(std::get<I>(*args)->At(argIndex[I])...));
538 }
539 (std::get<I>(*args)->IncrementSubscripts(argIndex[I]), ...);
540 } while (bounds.IncrementSubscripts(resultIndex));
541 }
542 // Build and return constant result
543 if constexpr (TR::category == TypeCategory::Character) {
544 auto len{static_cast<ConstantSubscript>(
545 results.empty() ? 0 : results[0].length())};
546 return Expr<TR>{Constant<TR>{len, std::move(results), std::move(shape)}};
547 } else if constexpr (TR::category == TypeCategory::Derived) {
548 if (!results.empty()) {
549 return Expr<TR>{rank == 0
550 ? Constant<TR>{results.front()}
551 : Constant<TR>{results.front().derivedTypeSpec(),
552 std::move(results), std::move(shape)}};
553 }
554 } else {
555 return Expr<TR>{Constant<TR>{std::move(results), std::move(shape)}};
556 }
557 }
558 return Expr<TR>{std::move(funcRef)};
559}
560
561template <typename TR, typename... TA>
562Expr<TR> FoldElementalIntrinsic(FoldingContext &context,
563 FunctionRef<TR> &&funcRef, ScalarFunc<TR, TA...> func,
564 bool hasOptionalArgument = false) {
565 return FoldElementalIntrinsicHelper<ScalarFunc, TR, TA...>(context,
566 std::move(funcRef), func, hasOptionalArgument,
567 std::index_sequence_for<TA...>{});
568}
569template <typename TR, typename... TA>
570Expr<TR> FoldElementalIntrinsic(FoldingContext &context,
571 FunctionRef<TR> &&funcRef, ScalarFuncWithContext<TR, TA...> func,
572 bool hasOptionalArgument = false) {
573 return FoldElementalIntrinsicHelper<ScalarFuncWithContext, TR, TA...>(context,
574 std::move(funcRef), func, hasOptionalArgument,
575 std::index_sequence_for<TA...>{});
576}
577
578std::optional<std::int64_t> GetInt64ArgOr(
579 const std::optional<ActualArgument> &, std::int64_t defaultValue);
580
581template <typename A, typename B>
582std::optional<std::vector<A>> GetIntegerVector(const B &x) {
583 static_assert(std::is_integral_v<A>);
584 if (const auto *someInteger{UnwrapExpr<Expr<SomeInteger>>(x)}) {
585 return common::visit(
586 [](const auto &typedExpr) -> std::optional<std::vector<A>> {
587 using T = ResultType<decltype(typedExpr)>;
588 if (const auto *constant{UnwrapConstantValue<T>(typedExpr)}) {
589 if (constant->Rank() == 1) {
590 std::vector<A> result;
591 for (const auto &value : constant->values()) {
592 result.push_back(static_cast<A>(value.ToInt64()));
593 }
594 return result;
595 }
596 }
597 return std::nullopt;
598 },
599 someInteger->u);
600 }
601 return std::nullopt;
602}
603
604// Transform an intrinsic function reference that contains user errors
605// into an intrinsic with the same characteristic but the "invalid" name.
606// This to prevent generating warnings over and over if the expression
607// gets re-folded.
608template <typename T> Expr<T> MakeInvalidIntrinsic(FunctionRef<T> &&funcRef) {
609 SpecificIntrinsic invalid{std::get<SpecificIntrinsic>(funcRef.proc().u)};
610 invalid.name = IntrinsicProcTable::InvalidName;
611 return Expr<T>{FunctionRef<T>{ProcedureDesignator{std::move(invalid)},
612 ActualArguments{std::move(funcRef.arguments())}}};
613}
614
615template <typename T> Expr<T> Folder<T>::CSHIFT(FunctionRef<T> &&funcRef) {
616 auto args{funcRef.arguments()};
617 CHECK(args.size() == 3);
618 const auto *array{UnwrapConstantValue<T>(args[0])};
619 const auto *shiftExpr{UnwrapExpr<Expr<SomeInteger>>(args[1])};
620 auto dim{GetInt64ArgOr(args[2], 1)};
621 if (!array || !shiftExpr || !dim) {
622 return Expr<T>{std::move(funcRef)};
623 }
624 auto convertedShift{Fold(context_,
625 ConvertToType<SubscriptInteger>(Expr<SomeInteger>{*shiftExpr}))};
626 const auto *shift{UnwrapConstantValue<SubscriptInteger>(convertedShift)};
627 if (!shift) {
628 return Expr<T>{std::move(funcRef)};
629 }
630 // Arguments are constant
631 if (*dim < 1 || *dim > array->Rank()) {
632 context_.messages().Say("Invalid 'dim=' argument (%jd) in CSHIFT"_err_en_US,
633 static_cast<std::intmax_t>(*dim));
634 } else if (shift->Rank() > 0 && shift->Rank() != array->Rank() - 1) {
635 // message already emitted from intrinsic look-up
636 } else {
637 int rank{array->Rank()};
638 int zbDim{static_cast<int>(*dim) - 1};
639 bool ok{true};
640 if (shift->Rank() > 0) {
641 int k{0};
642 for (int j{0}; j < rank; ++j) {
643 if (j != zbDim) {
644 if (array->shape()[j] != shift->shape()[k]) {
645 context_.messages().Say(
646 "Invalid 'shift=' argument in CSHIFT: extent on dimension %d is %jd but must be %jd"_err_en_US,
647 k + 1, static_cast<std::intmax_t>(shift->shape()[k]),
648 static_cast<std::intmax_t>(array->shape()[j]));
649 ok = false;
650 }
651 ++k;
652 }
653 }
654 }
655 if (ok) {
656 std::vector<Scalar<T>> resultElements;
657 ConstantSubscripts arrayLB{array->lbounds()};
658 ConstantSubscripts arrayAt{arrayLB};
659 ConstantSubscript &dimIndex{arrayAt[zbDim]};
660 ConstantSubscript dimLB{dimIndex}; // initial value
661 ConstantSubscript dimExtent{array->shape()[zbDim]};
662 ConstantSubscripts shiftLB{shift->lbounds()};
663 for (auto n{GetSize(array->shape())}; n > 0; --n) {
664 ConstantSubscript origDimIndex{dimIndex};
665 ConstantSubscripts shiftAt;
666 if (shift->Rank() > 0) {
667 int k{0};
668 for (int j{0}; j < rank; ++j) {
669 if (j != zbDim) {
670 shiftAt.emplace_back(shiftLB[k++] + arrayAt[j] - arrayLB[j]);
671 }
672 }
673 }
674 ConstantSubscript shiftCount{shift->At(shiftAt).ToInt64()};
675 dimIndex = dimLB + ((dimIndex - dimLB + shiftCount) % dimExtent);
676 if (dimIndex < dimLB) {
677 dimIndex += dimExtent;
678 } else if (dimIndex >= dimLB + dimExtent) {
679 dimIndex -= dimExtent;
680 }
681 resultElements.push_back(array->At(arrayAt));
682 dimIndex = origDimIndex;
683 array->IncrementSubscripts(arrayAt);
684 }
685 return Expr<T>{PackageConstant<T>(
686 std::move(resultElements), *array, array->shape())};
687 }
688 }
689 // Invalid, prevent re-folding
690 return MakeInvalidIntrinsic(std::move(funcRef));
691}
692
693template <typename T> Expr<T> Folder<T>::EOSHIFT(FunctionRef<T> &&funcRef) {
694 auto args{funcRef.arguments()};
695 CHECK(args.size() == 4);
696 const auto *array{UnwrapConstantValue<T>(args[0])};
697 const auto *shiftExpr{UnwrapExpr<Expr<SomeInteger>>(args[1])};
698 auto dim{GetInt64ArgOr(args[3], 1)};
699 if (!array || !shiftExpr || !dim) {
700 return Expr<T>{std::move(funcRef)};
701 }
702 // Apply type conversions to the shift= and boundary= arguments.
703 auto convertedShift{Fold(context_,
704 ConvertToType<SubscriptInteger>(Expr<SomeInteger>{*shiftExpr}))};
705 const auto *shift{UnwrapConstantValue<SubscriptInteger>(convertedShift)};
706 if (!shift) {
707 return Expr<T>{std::move(funcRef)};
708 }
709 const Constant<T> *boundary{nullptr};
710 std::optional<Expr<SomeType>> convertedBoundary;
711 if (const auto *boundaryExpr{UnwrapExpr<Expr<SomeType>>(args[2])}) {
712 convertedBoundary = Fold(context_,
713 ConvertToType(array->GetType(), Expr<SomeType>{*boundaryExpr}));
714 boundary = UnwrapExpr<Constant<T>>(convertedBoundary);
715 if (!boundary) {
716 return Expr<T>{std::move(funcRef)};
717 }
718 }
719 // Arguments are constant
720 if (*dim < 1 || *dim > array->Rank()) {
721 context_.messages().Say(
722 "Invalid 'dim=' argument (%jd) in EOSHIFT"_err_en_US,
723 static_cast<std::intmax_t>(*dim));
724 } else if (shift->Rank() > 0 && shift->Rank() != array->Rank() - 1) {
725 // message already emitted from intrinsic look-up
726 } else if (boundary && boundary->Rank() > 0 &&
727 boundary->Rank() != array->Rank() - 1) {
728 // ditto
729 } else {
730 int rank{array->Rank()};
731 int zbDim{static_cast<int>(*dim) - 1};
732 bool ok{true};
733 if (shift->Rank() > 0) {
734 int k{0};
735 for (int j{0}; j < rank; ++j) {
736 if (j != zbDim) {
737 if (array->shape()[j] != shift->shape()[k]) {
738 context_.messages().Say(
739 "Invalid 'shift=' argument in EOSHIFT: extent on dimension %d is %jd but must be %jd"_err_en_US,
740 k + 1, static_cast<std::intmax_t>(shift->shape()[k]),
741 static_cast<std::intmax_t>(array->shape()[j]));
742 ok = false;
743 }
744 ++k;
745 }
746 }
747 }
748 if (boundary && boundary->Rank() > 0) {
749 int k{0};
750 for (int j{0}; j < rank; ++j) {
751 if (j != zbDim) {
752 if (array->shape()[j] != boundary->shape()[k]) {
753 context_.messages().Say(
754 "Invalid 'boundary=' argument in EOSHIFT: extent on dimension %d is %jd but must be %jd"_err_en_US,
755 k + 1, static_cast<std::intmax_t>(boundary->shape()[k]),
756 static_cast<std::intmax_t>(array->shape()[j]));
757 ok = false;
758 }
759 ++k;
760 }
761 }
762 }
763 if (ok) {
764 std::vector<Scalar<T>> resultElements;
765 ConstantSubscripts arrayLB{array->lbounds()};
766 ConstantSubscripts arrayAt{arrayLB};
767 ConstantSubscript &dimIndex{arrayAt[zbDim]};
768 ConstantSubscript dimLB{dimIndex}; // initial value
769 ConstantSubscript dimExtent{array->shape()[zbDim]};
770 ConstantSubscripts shiftLB{shift->lbounds()};
771 ConstantSubscripts boundaryLB;
772 if (boundary) {
773 boundaryLB = boundary->lbounds();
774 }
775 for (auto n{GetSize(array->shape())}; n > 0; --n) {
776 ConstantSubscript origDimIndex{dimIndex};
777 ConstantSubscripts shiftAt;
778 if (shift->Rank() > 0) {
779 int k{0};
780 for (int j{0}; j < rank; ++j) {
781 if (j != zbDim) {
782 shiftAt.emplace_back(shiftLB[k++] + arrayAt[j] - arrayLB[j]);
783 }
784 }
785 }
786 ConstantSubscript shiftCount{shift->At(shiftAt).ToInt64()};
787 dimIndex += shiftCount;
788 if (dimIndex >= dimLB && dimIndex < dimLB + dimExtent) {
789 resultElements.push_back(array->At(arrayAt));
790 } else if (boundary) {
791 ConstantSubscripts boundaryAt;
792 if (boundary->Rank() > 0) {
793 for (int j{0}; j < rank; ++j) {
794 int k{0};
795 if (j != zbDim) {
796 boundaryAt.emplace_back(
797 boundaryLB[k++] + arrayAt[j] - arrayLB[j]);
798 }
799 }
800 }
801 resultElements.push_back(boundary->At(boundaryAt));
802 } else if constexpr (T::category == TypeCategory::Integer ||
803 T::category == TypeCategory::Unsigned ||
804 T::category == TypeCategory::Real ||
805 T::category == TypeCategory::Complex ||
806 T::category == TypeCategory::Logical) {
807 resultElements.emplace_back();
808 } else if constexpr (T::category == TypeCategory::Character) {
809 auto len{static_cast<std::size_t>(array->LEN())};
810 typename Scalar<T>::value_type space{' '};
811 resultElements.emplace_back(len, space);
812 } else {
813 DIE("no derived type boundary");
814 }
815 dimIndex = origDimIndex;
816 array->IncrementSubscripts(arrayAt);
817 }
818 return Expr<T>{PackageConstant<T>(
819 std::move(resultElements), *array, array->shape())};
820 }
821 }
822 // Invalid, prevent re-folding
823 return MakeInvalidIntrinsic(std::move(funcRef));
824}
825
826template <typename T> Expr<T> Folder<T>::MERGE(FunctionRef<T> &&funcRef) {
827 return FoldElementalIntrinsic<T, T, T, LogicalResult>(context_,
828 std::move(funcRef),
829 ScalarFunc<T, T, T, LogicalResult>(
830 [](const Scalar<T> &ifTrue, const Scalar<T> &ifFalse,
831 const Scalar<LogicalResult> &predicate) -> Scalar<T> {
832 return predicate.IsTrue() ? ifTrue : ifFalse;
833 }));
834}
835
836template <typename T> Expr<T> Folder<T>::PACK(FunctionRef<T> &&funcRef) {
837 auto args{funcRef.arguments()};
838 CHECK(args.size() == 3);
839 const auto *array{UnwrapConstantValue<T>(args[0])};
840 const auto *vector{UnwrapConstantValue<T>(args[2])};
841 auto convertedMask{Fold(context_,
842 ConvertToType<LogicalResult>(
843 Expr<SomeLogical>{DEREF(UnwrapExpr<Expr<SomeLogical>>(args[1]))}))};
844 const auto *mask{UnwrapConstantValue<LogicalResult>(convertedMask)};
845 if (!array || !mask || (args[2] && !vector)) {
846 return Expr<T>{std::move(funcRef)};
847 }
848 // Arguments are constant.
849 ConstantSubscript arrayElements{GetSize(array->shape())};
850 ConstantSubscript truths{0};
851 ConstantSubscripts maskAt{mask->lbounds()};
852 if (mask->Rank() == 0) {
853 if (mask->At(maskAt).IsTrue()) {
854 truths = arrayElements;
855 }
856 } else if (array->shape() != mask->shape()) {
857 // Error already emitted from intrinsic processing
858 return MakeInvalidIntrinsic(std::move(funcRef));
859 } else {
860 for (ConstantSubscript j{0}; j < arrayElements;
861 ++j, mask->IncrementSubscripts(maskAt)) {
862 if (mask->At(maskAt).IsTrue()) {
863 ++truths;
864 }
865 }
866 }
867 std::vector<Scalar<T>> resultElements;
868 ConstantSubscripts arrayAt{array->lbounds()};
869 ConstantSubscript resultSize{truths};
870 if (vector) {
871 resultSize = vector->shape().at(0);
872 if (resultSize < truths) {
873 context_.messages().Say(
874 "Invalid 'vector=' argument in PACK: the 'mask=' argument has %jd true elements, but the vector has only %jd elements"_err_en_US,
875 static_cast<std::intmax_t>(truths),
876 static_cast<std::intmax_t>(resultSize));
877 return MakeInvalidIntrinsic(std::move(funcRef));
878 }
879 }
880 for (ConstantSubscript j{0}; j < truths;) {
881 if (mask->At(maskAt).IsTrue()) {
882 resultElements.push_back(array->At(arrayAt));
883 ++j;
884 }
885 array->IncrementSubscripts(arrayAt);
886 mask->IncrementSubscripts(maskAt);
887 }
888 if (vector) {
889 ConstantSubscripts vectorAt{vector->lbounds()};
890 vectorAt.at(0) += truths;
891 for (ConstantSubscript j{truths}; j < resultSize; ++j) {
892 resultElements.push_back(vector->At(vectorAt));
893 ++vectorAt[0];
894 }
895 }
896 return Expr<T>{PackageConstant<T>(std::move(resultElements), *array,
897 ConstantSubscripts{static_cast<ConstantSubscript>(resultSize)})};
898}
899
900template <typename T> Expr<T> Folder<T>::RESHAPE(FunctionRef<T> &&funcRef) {
901 auto args{funcRef.arguments()};
902 CHECK(args.size() == 4);
903 const auto *source{UnwrapConstantValue<T>(args[0])};
904 const auto *pad{UnwrapConstantValue<T>(args[2])};
905 std::optional<std::vector<ConstantSubscript>> shape{
906 GetIntegerVector<ConstantSubscript>(args[1])};
907 std::optional<std::vector<int>> order{GetIntegerVector<int>(args[3])};
908 std::optional<uint64_t> optResultElement;
909 std::optional<std::vector<int>> dimOrder;
910 bool ok{true};
911 if (shape) {
912 if (shape->size() > common::maxRank) {
913 context_.messages().Say(
914 "Size of 'shape=' argument (%zd) must not be greater than %d"_err_en_US,
915 shape->size(), common::maxRank);
916 ok = false;
917 } else if (HasNegativeExtent(*shape)) {
918 context_.messages().Say(
919 "'shape=' argument (%s) must not have a negative extent"_err_en_US,
920 DEREF(args[1]->UnwrapExpr()).AsFortran());
921 ok = false;
922 } else {
923 optResultElement = TotalElementCount(*shape);
924 if (!optResultElement) {
925 context_.messages().Say(
926 "'shape=' argument (%s) specifies an array with too many elements"_err_en_US,
927 DEREF(args[1]->UnwrapExpr()).AsFortran());
928 ok = false;
929 }
930 }
931 if (order) {
932 dimOrder = ValidateDimensionOrder(GetRank(*shape), *order);
933 if (!dimOrder) {
934 context_.messages().Say(
935 "Invalid 'order=' argument (%s) in RESHAPE"_err_en_US,
936 DEREF(args[3]->UnwrapExpr()).AsFortran());
937 ok = false;
938 }
939 }
940 }
941 if (!ok) {
942 // convert into an invalid intrinsic procedure call below
943 } else if (!source || !shape || (args[2] && !pad) || (args[3] && !order)) {
944 return Expr<T>{std::move(funcRef)}; // Non-constant arguments
945 } else {
946 uint64_t resultElements{*optResultElement};
947 std::vector<int> *dimOrderPtr{dimOrder ? &dimOrder.value() : nullptr};
948 if (resultElements > source->size() && (!pad || pad->empty())) {
949 context_.messages().Say(
950 "Too few elements in 'source=' argument and 'pad=' "
951 "argument is not present or has null size"_err_en_US);
952 ok = false;
953 } else {
954 Constant<T> result{!source->empty() || !pad
955 ? source->Reshape(std::move(shape.value()))
956 : pad->Reshape(std::move(shape.value()))};
957 ConstantSubscripts subscripts{result.lbounds()};
958 auto copied{result.CopyFrom(*source,
959 std::min(static_cast<uint64_t>(source->size()), resultElements),
960 subscripts, dimOrderPtr)};
961 if (copied < resultElements) {
962 CHECK(pad);
963 copied += result.CopyFrom(
964 *pad, resultElements - copied, subscripts, dimOrderPtr);
965 }
966 CHECK(copied == resultElements);
967 return Expr<T>{std::move(result)};
968 }
969 }
970 // Invalid, prevent re-folding
971 return MakeInvalidIntrinsic(std::move(funcRef));
972}
973
974template <typename T> Expr<T> Folder<T>::SPREAD(FunctionRef<T> &&funcRef) {
975 auto args{funcRef.arguments()};
976 CHECK(args.size() == 3);
977 const Constant<T> *source{UnwrapConstantValue<T>(args[0])};
978 auto dim{ToInt64(args[1])};
979 auto ncopies{ToInt64(args[2])};
980 if (!source || !dim) {
981 return Expr<T>{std::move(funcRef)};
982 }
983 int sourceRank{source->Rank()};
984 if (sourceRank >= common::maxRank) {
985 context_.messages().Say(
986 "SOURCE= argument to SPREAD has rank %d but must have rank less than %d"_err_en_US,
987 sourceRank, common::maxRank);
988 } else if (*dim < 1 || *dim > sourceRank + 1) {
989 context_.messages().Say(
990 "DIM=%d argument to SPREAD must be between 1 and %d"_err_en_US, *dim,
991 sourceRank + 1);
992 } else if (!ncopies) {
993 return Expr<T>{std::move(funcRef)};
994 } else {
995 if (*ncopies < 0) {
996 ncopies = 0;
997 }
998 // TODO: Consider moving this implementation (after the user error
999 // checks), along with other transformational intrinsics, into
1000 // constant.h (or a new header) so that the transformationals
1001 // are available for all Constant<>s without needing to be packaged
1002 // as references to intrinsic functions for folding.
1003 ConstantSubscripts shape{source->shape()};
1004 shape.insert(shape.begin() + *dim - 1, *ncopies);
1005 Constant<T> spread{source->Reshape(std::move(shape))};
1006 std::optional<uint64_t> n{TotalElementCount(spread.shape())};
1007 if (!n) {
1008 context_.messages().Say("Too many elements in SPREAD result"_err_en_US);
1009 } else {
1010 std::vector<int> dimOrder;
1011 for (int j{0}; j < sourceRank; ++j) {
1012 dimOrder.push_back(j < *dim - 1 ? j : j + 1);
1013 }
1014 dimOrder.push_back(*dim - 1);
1015 ConstantSubscripts at{spread.lbounds()}; // all 1
1016 spread.CopyFrom(*source, *n, at, &dimOrder);
1017 return Expr<T>{std::move(spread)};
1018 }
1019 }
1020 // Invalid, prevent re-folding
1021 return MakeInvalidIntrinsic(std::move(funcRef));
1022}
1023
1024template <typename T> Expr<T> Folder<T>::TRANSPOSE(FunctionRef<T> &&funcRef) {
1025 auto args{funcRef.arguments()};
1026 CHECK(args.size() == 1);
1027 const auto *matrix{UnwrapConstantValue<T>(args[0])};
1028 if (!matrix) {
1029 return Expr<T>{std::move(funcRef)};
1030 }
1031 // Argument is constant. Traverse its elements in transposed order.
1032 std::vector<Scalar<T>> resultElements;
1033 ConstantSubscripts at(2);
1034 for (ConstantSubscript j{0}; j < matrix->shape()[0]; ++j) {
1035 at[0] = matrix->lbounds()[0] + j;
1036 for (ConstantSubscript k{0}; k < matrix->shape()[1]; ++k) {
1037 at[1] = matrix->lbounds()[1] + k;
1038 resultElements.push_back(matrix->At(at));
1039 }
1040 }
1041 at = matrix->shape();
1042 std::swap(at[0], at[1]);
1043 return Expr<T>{PackageConstant<T>(std::move(resultElements), *matrix, at)};
1044}
1045
1046template <typename T> Expr<T> Folder<T>::UNPACK(FunctionRef<T> &&funcRef) {
1047 auto args{funcRef.arguments()};
1048 CHECK(args.size() == 3);
1049 const auto *vector{UnwrapConstantValue<T>(args[0])};
1050 auto convertedMask{Fold(context_,
1051 ConvertToType<LogicalResult>(
1052 Expr<SomeLogical>{DEREF(UnwrapExpr<Expr<SomeLogical>>(args[1]))}))};
1053 const auto *mask{UnwrapConstantValue<LogicalResult>(convertedMask)};
1054 const auto *field{UnwrapConstantValue<T>(args[2])};
1055 if (!vector || !mask || !field) {
1056 return Expr<T>{std::move(funcRef)};
1057 }
1058 // Arguments are constant.
1059 if (field->Rank() > 0 && field->shape() != mask->shape()) {
1060 // Error already emitted from intrinsic processing
1061 return MakeInvalidIntrinsic(std::move(funcRef));
1062 }
1063 ConstantSubscript maskElements{GetSize(mask->shape())};
1064 ConstantSubscript truths{0};
1065 ConstantSubscripts maskAt{mask->lbounds()};
1066 for (ConstantSubscript j{0}; j < maskElements;
1067 ++j, mask->IncrementSubscripts(maskAt)) {
1068 if (mask->At(maskAt).IsTrue()) {
1069 ++truths;
1070 }
1071 }
1072 if (truths > GetSize(vector->shape())) {
1073 context_.messages().Say(
1074 "Invalid 'vector=' argument in UNPACK: the 'mask=' argument has %jd true elements, but the vector has only %jd elements"_err_en_US,
1075 static_cast<std::intmax_t>(truths),
1076 static_cast<std::intmax_t>(GetSize(vector->shape())));
1077 return MakeInvalidIntrinsic(std::move(funcRef));
1078 }
1079 std::vector<Scalar<T>> resultElements;
1080 ConstantSubscripts vectorAt{vector->lbounds()};
1081 ConstantSubscripts fieldAt{field->lbounds()};
1082 for (ConstantSubscript j{0}; j < maskElements; ++j) {
1083 if (mask->At(maskAt).IsTrue()) {
1084 resultElements.push_back(vector->At(vectorAt));
1085 vector->IncrementSubscripts(vectorAt);
1086 } else {
1087 resultElements.push_back(field->At(fieldAt));
1088 }
1089 mask->IncrementSubscripts(maskAt);
1090 field->IncrementSubscripts(fieldAt);
1091 }
1092 return Expr<T>{
1093 PackageConstant<T>(std::move(resultElements), *vector, mask->shape())};
1094}
1095
1096std::optional<Expr<SomeType>> FoldTransfer(
1097 FoldingContext &, const ActualArguments &);
1098
1099template <typename T> Expr<T> Folder<T>::TRANSFER(FunctionRef<T> &&funcRef) {
1100 if (auto folded{FoldTransfer(context_, funcRef.arguments())}) {
1101 return DEREF(UnwrapExpr<Expr<T>>(*folded));
1102 } else {
1103 return Expr<T>{std::move(funcRef)};
1104 }
1105}
1106
1107// TODO: Once the backend supports character extremums we could support
1108// min/max with non-optional arguments to trees of extremum operations.
1109template <typename T>
1110Expr<T> FoldMINorMAX(
1111 FoldingContext &context, FunctionRef<T> &&funcRef, Ordering order) {
1112 static_assert(T::category == TypeCategory::Integer ||
1113 T::category == TypeCategory::Unsigned ||
1114 T::category == TypeCategory::Real ||
1115 T::category == TypeCategory::Character);
1116
1117 // Lots of constraints:
1118 // - We want Extremum<T> generated by semantics to compare equal to
1119 // Extremum<T> written out to module files as max or min calls.
1120 // - Users can also write min/max calls that must also compare equal
1121 // to min/max calls that wind up being written to module files.
1122 // - Extremeum<T> is binary and can't currently handle processing
1123 // optional arguments that may show up in 3rd + argument.
1124 // - The code below only accepts more than 2 arguments if all the
1125 // arguments are constant (and hence known to be present).
1126 // - ConvertExprToHLFIR can't currently handle Extremum<Character>
1127 // - Semantics doesn't currently generate Extremum<Character>
1128 // The original code did the folding of arguments and the overall extremum
1129 // operation in a single pass. This was shorter code-wise, but took me
1130 // a while to tease out all the logic and was doing redundant work.
1131 // So I split it into two passes:
1132 // 1) fold the arguments and check if they are constant,
1133 // 2) Decide if we:
1134 // - can constant-fold the min/max operation, or
1135 // - need to generate an extremum anyway,
1136 // and do it if so.
1137 // Otherwise, return the original call.
1138 auto &args{funcRef.arguments()};
1139 std::size_t nargs{args.size()};
1140 bool allArgsConstant{true};
1141 bool extremumAnyway{nargs == 2 && T::category != TypeCategory::Character};
1142 // 1a)Fold the first two arguments.
1143 {
1144 Folder<T> folder{context, /*forOptionalArgument=*/false};
1145 if (!folder.Folding(args[0])) {
1146 allArgsConstant = false;
1147 }
1148 if (!folder.Folding(args[1])) {
1149 allArgsConstant = false;
1150 }
1151 }
1152 // 1b) Fold any optional arguments.
1153 if (nargs > 2) {
1154 Folder<T> folder{context, /*forOptionalArgument=*/true};
1155 for (std::size_t i{2}; i < nargs; ++i) {
1156 if (args[i]) {
1157 if (!folder.Folding(args[i])) {
1158 allArgsConstant = false;
1159 }
1160 }
1161 }
1162 }
1163 // 2) If we can fold the result or the call to min/max may compare equal to
1164 // an extremum generated by semantics go ahead and convert to an extremum,
1165 // and try to fold the result.
1166 if (allArgsConstant || extremumAnyway) {
1167 // Folding updates the argument expressions in place, no need to call
1168 // Fold() on each argument again.
1169 if (const auto *resultp{UnwrapExpr<Expr<T>>(args[0])}) {
1170 Expr<T> result{*resultp};
1171 for (std::size_t i{1}; i < nargs; ++i) {
1172 if (const auto *tExpr{UnwrapExpr<Expr<T>>(args[i])}) {
1173 result = FoldOperation(
1174 context, Extremum<T>{order, std::move(result), *tExpr});
1175 } else {
1176 // This should never happen, but here is a value to return.
1177 return Expr<T>{std::move(funcRef)};
1178 }
1179 }
1180 return result;
1181 }
1182 }
1183 // If we decided to not generate an extremum just return the original call,
1184 // with the arguments folded.
1185 return Expr<T>{std::move(funcRef)};
1186}
1187
1188// For AMAX0, AMIN0, AMAX1, AMIN1, DMAX1, DMIN1, MAX0, MIN0, MAX1, and MIN1
1189// a special care has to be taken to insert the conversion on the result
1190// of the MIN/MAX. This is made slightly more complex by the extension
1191// supported by f18 that arguments may have different kinds. This implies
1192// that the created MIN/MAX result type cannot be deduced from the standard but
1193// has to be deduced from the arguments.
1194// e.g. AMAX0(int8, int4) is rewritten to REAL(MAX(int8, INT(int4, 8)))).
1195template <typename T>
1196Expr<T> RewriteSpecificMINorMAX(
1197 FoldingContext &context, FunctionRef<T> &&funcRef) {
1198 ActualArguments &args{funcRef.arguments()};
1199 auto &intrinsic{DEREF(std::get_if<SpecificIntrinsic>(&funcRef.proc().u))};
1200 // Rewrite MAX1(args) to INT(MAX(args)) and fold. Same logic for MIN1.
1201 // Find result type for max/min based on the arguments.
1202 std::optional<DynamicType> resultType;
1203 ActualArgument *resultTypeArg{nullptr};
1204 for (auto j{args.size()}; j-- > 0;) {
1205 if (args[j]) {
1206 DynamicType type{args[j]->GetType().value()};
1207 // Handle mixed real/integer arguments: all the previous arguments were
1208 // integers and this one is real. The type of the MAX/MIN result will
1209 // be the one of the real argument.
1210 if (!resultType ||
1211 (type.category() == resultType->category() &&
1212 type.kind() > resultType->kind()) ||
1213 resultType->category() == TypeCategory::Integer) {
1214 resultType = type;
1215 resultTypeArg = &*args[j];
1216 }
1217 }
1218 }
1219 if (!resultType) { // error recovery
1220 return Expr<T>{std::move(funcRef)};
1221 }
1222 intrinsic.name =
1223 intrinsic.name.find("max") != std::string::npos ? "max"s : "min"s;
1224 intrinsic.characteristics.value().functionResult.value().SetType(*resultType);
1225 auto insertConversion{[&](const auto &x) -> Expr<T> {
1226 using TR = ResultType<decltype(x)>;
1227 FunctionRef<TR> maxRef{
1228 ProcedureDesignator{funcRef.proc()}, ActualArguments{args}};
1229 return Fold(context, ConvertToType<T>(AsCategoryExpr(std::move(maxRef))));
1230 }};
1231 if (auto *sx{UnwrapExpr<Expr<SomeReal>>(*resultTypeArg)}) {
1232 return common::visit(insertConversion, sx->u);
1233 } else if (auto *sx{UnwrapExpr<Expr<SomeInteger>>(*resultTypeArg)}) {
1234 return common::visit(insertConversion, sx->u);
1235 } else {
1236 return Expr<T>{std::move(funcRef)}; // error recovery
1237 }
1238}
1239
1240// FoldIntrinsicFunction()
1241template <int KIND>
1242Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
1244template <int KIND>
1245Expr<Type<TypeCategory::Unsigned, KIND>> FoldIntrinsicFunction(
1246 FoldingContext &context,
1248template <int KIND>
1249Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction(
1251template <int KIND>
1252Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction(
1254template <int KIND>
1255Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
1257
1258template <typename T>
1259Expr<T> FoldOperation(FoldingContext &context, FunctionRef<T> &&funcRef) {
1260 ActualArguments &args{funcRef.arguments()};
1261 const auto *intrinsic{std::get_if<SpecificIntrinsic>(&funcRef.proc().u)};
1262 if (!intrinsic || intrinsic->name != "kind") {
1263 // Don't fold the argument to KIND(); it might be a TypeParamInquiry
1264 // with a forced result type that doesn't match the parameter.
1265 for (std::optional<ActualArgument> &arg : args) {
1266 if (auto *expr{UnwrapExpr<Expr<SomeType>>(arg)}) {
1267 *expr = Fold(context, std::move(*expr));
1268 }
1269 }
1270 }
1271 if (intrinsic) {
1272 const std::string name{intrinsic->name};
1273 if (name == "cshift") {
1274 return Folder<T>{context}.CSHIFT(std::move(funcRef));
1275 } else if (name == "eoshift") {
1276 return Folder<T>{context}.EOSHIFT(std::move(funcRef));
1277 } else if (name == "merge") {
1278 return Folder<T>{context}.MERGE(std::move(funcRef));
1279 } else if (name == "pack") {
1280 return Folder<T>{context}.PACK(std::move(funcRef));
1281 } else if (name == "reshape") {
1282 return Folder<T>{context}.RESHAPE(std::move(funcRef));
1283 } else if (name == "spread") {
1284 return Folder<T>{context}.SPREAD(std::move(funcRef));
1285 } else if (name == "transfer") {
1286 return Folder<T>{context}.TRANSFER(std::move(funcRef));
1287 } else if (name == "transpose") {
1288 return Folder<T>{context}.TRANSPOSE(std::move(funcRef));
1289 } else if (name == "unpack") {
1290 return Folder<T>{context}.UNPACK(std::move(funcRef));
1291 }
1292 // TODO: extends_type_of, same_type_as
1293 if constexpr (!std::is_same_v<T, SomeDerived>) {
1294 return FoldIntrinsicFunction(context, std::move(funcRef));
1295 }
1296 }
1297 return Expr<T>{std::move(funcRef)};
1298}
1299
1300// Array constructor folding
1301template <typename T> class ArrayConstructorFolder {
1302public:
1303 explicit ArrayConstructorFolder(FoldingContext &c) : context_{c} {}
1304
1305 Expr<T> FoldArray(ArrayConstructor<T> &&array) {
1306 if constexpr (T::category == TypeCategory::Character) {
1307 if (const auto *len{array.LEN()}) {
1308 charLength_ = ToInt64(Fold(context_, common::Clone(*len)));
1309 knownCharLength_ = charLength_.has_value();
1310 }
1311 }
1312 // Calls FoldArray(const ArrayConstructorValues<T> &) below
1313 if (FoldArray(array)) {
1314 auto n{static_cast<ConstantSubscript>(elements_.size())};
1315 if constexpr (std::is_same_v<T, SomeDerived>) {
1316 return Expr<T>{Constant<T>{array.GetType().GetDerivedTypeSpec(),
1317 std::move(elements_), ConstantSubscripts{n}}};
1318 } else if constexpr (T::category == TypeCategory::Character) {
1319 if (charLength_) {
1320 return Expr<T>{Constant<T>{
1321 *charLength_, std::move(elements_), ConstantSubscripts{n}}};
1322 }
1323 } else {
1324 return Expr<T>{Constant<T>{
1325 std::move(elements_), ConstantSubscripts{n}, resultInfo_}};
1326 }
1327 }
1328 return Expr<T>{std::move(array)};
1329 }
1330
1331private:
1332 bool FoldArray(const Expr<T> &expr) {
1333 Expr<T> folded{Fold(context_, common::Clone(expr))};
1334 if (const auto *c{UnwrapConstantValue<T>(folded)}) {
1335 // Copy elements in Fortran array element order
1336 if (!c->empty()) {
1337 ConstantSubscripts index{c->lbounds()};
1338 do {
1339 elements_.emplace_back(c->At(index));
1340 } while (c->IncrementSubscripts(index));
1341 }
1342 if constexpr (T::category == TypeCategory::Character) {
1343 if (!knownCharLength_) {
1344 charLength_ = std::max(c->LEN(), charLength_.value_or(-1));
1345 }
1346 } else if constexpr (T::category == TypeCategory::Real ||
1347 T::category == TypeCategory::Complex) {
1348 if (c->result().isFromInexactLiteralConversion()) {
1349 resultInfo_.set_isFromInexactLiteralConversion();
1350 }
1351 }
1352 return true;
1353 } else {
1354 return false;
1355 }
1356 }
1357 bool FoldArray(const common::CopyableIndirection<Expr<T>> &expr) {
1358 return FoldArray(expr.value());
1359 }
1360 bool FoldArray(const ImpliedDo<T> &iDo) {
1362 Fold(context_, Expr<SubscriptInteger>{iDo.lower()})};
1364 Fold(context_, Expr<SubscriptInteger>{iDo.upper()})};
1366 Fold(context_, Expr<SubscriptInteger>{iDo.stride()})};
1367 std::optional<ConstantSubscript> start{ToInt64(lower)}, end{ToInt64(upper)},
1368 step{ToInt64(stride)};
1369 if (start && end && step && *step != 0) {
1370 bool result{true};
1371 ConstantSubscript &j{context_.StartImpliedDo(iDo.name(), *start)};
1372 if (*step > 0) {
1373 for (; j <= *end; j += *step) {
1374 result &= FoldArray(iDo.values());
1375 }
1376 } else {
1377 for (; j >= *end; j += *step) {
1378 result &= FoldArray(iDo.values());
1379 }
1380 }
1381 context_.EndImpliedDo(iDo.name());
1382 return result;
1383 } else {
1384 return false;
1385 }
1386 }
1387 bool FoldArray(const ArrayConstructorValue<T> &x) {
1388 return common::visit([&](const auto &y) { return FoldArray(y); }, x.u);
1389 }
1390 bool FoldArray(const ArrayConstructorValues<T> &xs) {
1391 for (const auto &x : xs) {
1392 if (!FoldArray(x)) {
1393 return false;
1394 }
1395 }
1396 return true;
1397 }
1398
1399 FoldingContext &context_;
1400 std::vector<Scalar<T>> elements_;
1401 std::optional<ConstantSubscript> charLength_;
1402 bool knownCharLength_{false};
1403 typename Constant<T>::Result resultInfo_;
1404};
1405
1406template <typename T>
1407Expr<T> FoldOperation(FoldingContext &context, ArrayConstructor<T> &&array) {
1408 return ArrayConstructorFolder<T>{context}.FoldArray(std::move(array));
1409}
1410
1411// Array operation elemental application: When all operands to an operation
1412// are constant arrays, array constructors without any implied DO loops,
1413// &/or expanded scalars, pull the operation "into" the array result by
1414// applying it in an elementwise fashion. For example, [A,1]+[B,2]
1415// is rewritten into [A+B,1+2] and then partially folded to [A+B,3].
1416
1417// If possible, restructures an array expression into an array constructor
1418// that comprises a "flat" ArrayConstructorValues with no implied DO loops.
1419template <typename T>
1420bool ArrayConstructorIsFlat(const ArrayConstructorValues<T> &values) {
1421 for (const ArrayConstructorValue<T> &x : values) {
1422 if (!std::holds_alternative<Expr<T>>(x.u)) {
1423 return false;
1424 }
1425 }
1426 return true;
1427}
1428
1429template <typename T>
1430std::optional<Expr<T>> AsFlatArrayConstructor(const Expr<T> &expr) {
1431 if (const auto *c{UnwrapConstantValue<T>(expr)}) {
1432 ArrayConstructor<T> result{expr};
1433 if (!c->empty()) {
1434 ConstantSubscripts at{c->lbounds()};
1435 do {
1436 result.Push(Expr<T>{Constant<T>{c->At(at)}});
1437 } while (c->IncrementSubscripts(at));
1438 }
1439 return std::make_optional<Expr<T>>(std::move(result));
1440 } else if (const auto *a{UnwrapExpr<ArrayConstructor<T>>(expr)}) {
1441 if (ArrayConstructorIsFlat(*a)) {
1442 return std::make_optional<Expr<T>>(expr);
1443 }
1444 } else if (const auto *p{UnwrapExpr<Parentheses<T>>(expr)}) {
1445 return AsFlatArrayConstructor(Expr<T>{p->left()});
1446 }
1447 return std::nullopt;
1448}
1449
1450template <TypeCategory CAT>
1451std::enable_if_t<CAT != TypeCategory::Derived,
1452 std::optional<Expr<SomeKind<CAT>>>>
1453AsFlatArrayConstructor(const Expr<SomeKind<CAT>> &expr) {
1454 return common::visit(
1455 [&](const auto &kindExpr) -> std::optional<Expr<SomeKind<CAT>>> {
1456 if (auto flattened{AsFlatArrayConstructor(kindExpr)}) {
1457 return Expr<SomeKind<CAT>>{std::move(*flattened)};
1458 } else {
1459 return std::nullopt;
1460 }
1461 },
1462 expr.u);
1463}
1464
1465// FromArrayConstructor is a subroutine for MapOperation() below.
1466// Given a flat ArrayConstructor<T> and a shape, it wraps the array
1467// into an Expr<T>, folds it, and returns the resulting wrapped
1468// array constructor or constant array value.
1469template <typename T>
1470std::optional<Expr<T>> FromArrayConstructor(
1471 FoldingContext &context, ArrayConstructor<T> &&values, const Shape &shape) {
1472 if (auto constShape{AsConstantExtents(context, shape)};
1473 constShape && !HasNegativeExtent(*constShape)) {
1474 Expr<T> result{Fold(context, Expr<T>{std::move(values)})};
1475 if (auto *constant{UnwrapConstantValue<T>(result)}) {
1476 // Elements and shape are both constant.
1477 return Expr<T>{constant->Reshape(std::move(*constShape))};
1478 }
1479 if (constShape->size() == 1) {
1480 if (auto elements{GetShape(context, result)}) {
1481 if (auto constElements{AsConstantExtents(context, *elements)}) {
1482 if (constElements->size() == 1 &&
1483 constElements->at(0) == constShape->at(0)) {
1484 // Elements are not constant, but array constructor has
1485 // the right known shape and can be simply returned as is.
1486 return std::move(result);
1487 }
1488 }
1489 }
1490 }
1491 }
1492 return std::nullopt;
1493}
1494
1495// MapOperation is a utility for various specializations of ApplyElementwise()
1496// that follow. Given one or two flat ArrayConstructor<OPERAND> (wrapped in an
1497// Expr<OPERAND>) for some specific operand type(s), apply a given function f
1498// to each of their corresponding elements to produce a flat
1499// ArrayConstructor<RESULT> (wrapped in an Expr<RESULT>).
1500// Preserves shape.
1501
1502// Unary case
1503template <typename RESULT, typename OPERAND>
1504std::optional<Expr<RESULT>> MapOperation(FoldingContext &context,
1505 std::function<Expr<RESULT>(Expr<OPERAND> &&)> &&f, const Shape &shape,
1506 [[maybe_unused]] std::optional<Expr<SubscriptInteger>> &&length,
1507 Expr<OPERAND> &&values) {
1508 ArrayConstructor<RESULT> result{values};
1509 if constexpr (common::HasMember<OPERAND, AllIntrinsicCategoryTypes>) {
1510 common::visit(
1511 [&](auto &&kindExpr) {
1512 using kindType = ResultType<decltype(kindExpr)>;
1513 auto &aConst{std::get<ArrayConstructor<kindType>>(kindExpr.u)};
1514 for (auto &acValue : aConst) {
1515 auto &scalar{std::get<Expr<kindType>>(acValue.u)};
1516 result.Push(Fold(context, f(Expr<OPERAND>{std::move(scalar)})));
1517 }
1518 },
1519 std::move(values.u));
1520 } else {
1521 auto &aConst{std::get<ArrayConstructor<OPERAND>>(values.u)};
1522 for (auto &acValue : aConst) {
1523 auto &scalar{std::get<Expr<OPERAND>>(acValue.u)};
1524 result.Push(Fold(context, f(std::move(scalar))));
1525 }
1526 }
1527 if constexpr (RESULT::category == TypeCategory::Character) {
1528 if (length) {
1529 result.set_LEN(std::move(*length));
1530 }
1531 }
1532 return FromArrayConstructor(context, std::move(result), shape);
1533}
1534
1535template <typename RESULT, typename A>
1536ArrayConstructor<RESULT> ArrayConstructorFromMold(
1537 const A &prototype, std::optional<Expr<SubscriptInteger>> &&length) {
1538 ArrayConstructor<RESULT> result{prototype};
1539 if constexpr (RESULT::category == TypeCategory::Character) {
1540 if (length) {
1541 result.set_LEN(std::move(*length));
1542 }
1543 }
1544 return result;
1545}
1546
1547template <typename LEFT, typename RIGHT>
1548bool ShapesMatch(FoldingContext &context,
1549 const ArrayConstructor<LEFT> &leftArrConst,
1550 const ArrayConstructor<RIGHT> &rightArrConst) {
1551 auto rightIter{rightArrConst.begin()};
1552 for (auto &leftValue : leftArrConst) {
1553 CHECK(rightIter != rightArrConst.end());
1554 auto &leftExpr{std::get<Expr<LEFT>>(leftValue.u)};
1555 auto &rightExpr{std::get<Expr<RIGHT>>(rightIter->u)};
1556 if (leftExpr.Rank() != rightExpr.Rank()) {
1557 return false;
1558 }
1559 std::optional<Shape> leftShape{GetShape(context, leftExpr)};
1560 std::optional<Shape> rightShape{GetShape(context, rightExpr)};
1561 if (!leftShape || !rightShape || *leftShape != *rightShape) {
1562 return false;
1563 }
1564 ++rightIter;
1565 }
1566 return true;
1567}
1568
1569// array * array case
1570template <typename RESULT, typename LEFT, typename RIGHT>
1571auto MapOperation(FoldingContext &context,
1572 std::function<Expr<RESULT>(Expr<LEFT> &&, Expr<RIGHT> &&)> &&f,
1573 const Shape &shape, std::optional<Expr<SubscriptInteger>> &&length,
1574 Expr<LEFT> &&leftValues, Expr<RIGHT> &&rightValues)
1575 -> std::optional<Expr<RESULT>> {
1576 auto result{ArrayConstructorFromMold<RESULT>(leftValues, std::move(length))};
1577 auto &leftArrConst{std::get<ArrayConstructor<LEFT>>(leftValues.u)};
1578 if constexpr (common::HasMember<RIGHT, AllIntrinsicCategoryTypes>) {
1579 bool mapped{common::visit(
1580 [&](auto &&kindExpr) -> bool {
1581 using kindType = ResultType<decltype(kindExpr)>;
1582
1583 auto &rightArrConst{std::get<ArrayConstructor<kindType>>(kindExpr.u)};
1584 if (!ShapesMatch(context, leftArrConst, rightArrConst)) {
1585 return false;
1586 }
1587 auto rightIter{rightArrConst.begin()};
1588 for (auto &leftValue : leftArrConst) {
1589 CHECK(rightIter != rightArrConst.end());
1590 auto &leftScalar{std::get<Expr<LEFT>>(leftValue.u)};
1591 auto &rightScalar{std::get<Expr<kindType>>(rightIter->u)};
1592 result.Push(Fold(context,
1593 f(std::move(leftScalar), Expr<RIGHT>{std::move(rightScalar)})));
1594 ++rightIter;
1595 }
1596 return true;
1597 },
1598 std::move(rightValues.u))};
1599 if (!mapped) {
1600 return std::nullopt;
1601 }
1602 } else {
1603 auto &rightArrConst{std::get<ArrayConstructor<RIGHT>>(rightValues.u)};
1604 if (!ShapesMatch(context, leftArrConst, rightArrConst)) {
1605 return std::nullopt;
1606 }
1607 auto rightIter{rightArrConst.begin()};
1608 for (auto &leftValue : leftArrConst) {
1609 CHECK(rightIter != rightArrConst.end());
1610 auto &leftScalar{std::get<Expr<LEFT>>(leftValue.u)};
1611 auto &rightScalar{std::get<Expr<RIGHT>>(rightIter->u)};
1612 result.Push(
1613 Fold(context, f(std::move(leftScalar), std::move(rightScalar))));
1614 ++rightIter;
1615 }
1616 }
1617 return FromArrayConstructor(context, std::move(result), shape);
1618}
1619
1620// array * scalar case
1621template <typename RESULT, typename LEFT, typename RIGHT>
1622auto MapOperation(FoldingContext &context,
1623 std::function<Expr<RESULT>(Expr<LEFT> &&, Expr<RIGHT> &&)> &&f,
1624 const Shape &shape, std::optional<Expr<SubscriptInteger>> &&length,
1625 Expr<LEFT> &&leftValues, const Expr<RIGHT> &rightScalar)
1626 -> std::optional<Expr<RESULT>> {
1627 auto result{ArrayConstructorFromMold<RESULT>(leftValues, std::move(length))};
1628 auto &leftArrConst{std::get<ArrayConstructor<LEFT>>(leftValues.u)};
1629 for (auto &leftValue : leftArrConst) {
1630 auto &leftScalar{std::get<Expr<LEFT>>(leftValue.u)};
1631 result.Push(
1632 Fold(context, f(std::move(leftScalar), Expr<RIGHT>{rightScalar})));
1633 }
1634 return FromArrayConstructor(context, std::move(result), shape);
1635}
1636
1637// scalar * array case
1638template <typename RESULT, typename LEFT, typename RIGHT>
1639auto MapOperation(FoldingContext &context,
1640 std::function<Expr<RESULT>(Expr<LEFT> &&, Expr<RIGHT> &&)> &&f,
1641 const Shape &shape, std::optional<Expr<SubscriptInteger>> &&length,
1642 const Expr<LEFT> &leftScalar, Expr<RIGHT> &&rightValues)
1643 -> std::optional<Expr<RESULT>> {
1644 auto result{ArrayConstructorFromMold<RESULT>(leftScalar, std::move(length))};
1645 if constexpr (common::HasMember<RIGHT, AllIntrinsicCategoryTypes>) {
1646 common::visit(
1647 [&](auto &&kindExpr) {
1648 using kindType = ResultType<decltype(kindExpr)>;
1649 auto &rightArrConst{std::get<ArrayConstructor<kindType>>(kindExpr.u)};
1650 for (auto &rightValue : rightArrConst) {
1651 auto &rightScalar{std::get<Expr<kindType>>(rightValue.u)};
1652 result.Push(Fold(context,
1653 f(Expr<LEFT>{leftScalar},
1654 Expr<RIGHT>{std::move(rightScalar)})));
1655 }
1656 },
1657 std::move(rightValues.u));
1658 } else {
1659 auto &rightArrConst{std::get<ArrayConstructor<RIGHT>>(rightValues.u)};
1660 for (auto &rightValue : rightArrConst) {
1661 auto &rightScalar{std::get<Expr<RIGHT>>(rightValue.u)};
1662 result.Push(
1663 Fold(context, f(Expr<LEFT>{leftScalar}, std::move(rightScalar))));
1664 }
1665 }
1666 return FromArrayConstructor(context, std::move(result), shape);
1667}
1668
1669template <typename DERIVED, typename RESULT, typename... OPD>
1670std::optional<Expr<SubscriptInteger>> ComputeResultLength(
1672 if constexpr (RESULT::category == TypeCategory::Character) {
1673 return Expr<RESULT>{operation.derived()}.LEN();
1674 }
1675 return std::nullopt;
1676}
1677
1678// ApplyElementwise() recursively folds the operand expression(s) of an
1679// operation, then attempts to apply the operation to the (corresponding)
1680// scalar element(s) of those operands. Returns std::nullopt for scalars
1681// or unlinearizable operands.
1682template <typename DERIVED, typename RESULT, typename OPERAND>
1683auto ApplyElementwise(FoldingContext &context,
1685 std::function<Expr<RESULT>(Expr<OPERAND> &&)> &&f)
1686 -> std::optional<Expr<RESULT>> {
1687 auto &expr{operation.left()};
1688 expr = Fold(context, std::move(expr));
1689 if (expr.Rank() > 0) {
1690 if (std::optional<Shape> shape{GetShape(context, expr)}) {
1691 if (auto values{AsFlatArrayConstructor(expr)}) {
1692 return MapOperation(context, std::move(f), *shape,
1693 ComputeResultLength(operation), std::move(*values));
1694 }
1695 }
1696 }
1697 return std::nullopt;
1698}
1699
1700template <typename DERIVED, typename RESULT, typename OPERAND>
1701auto ApplyElementwise(
1703 -> std::optional<Expr<RESULT>> {
1704 return ApplyElementwise(context, operation,
1705 std::function<Expr<RESULT>(Expr<OPERAND> &&)>{
1706 [](Expr<OPERAND> &&operand) {
1707 return Expr<RESULT>{DERIVED{std::move(operand)}};
1708 }});
1709}
1710
1711template <typename DERIVED, typename RESULT, typename LEFT, typename RIGHT>
1712auto ApplyElementwise(FoldingContext &context,
1714 std::function<Expr<RESULT>(Expr<LEFT> &&, Expr<RIGHT> &&)> &&f)
1715 -> std::optional<Expr<RESULT>> {
1716 auto resultLength{ComputeResultLength(operation)};
1717 auto &leftExpr{operation.left()};
1718 auto &rightExpr{operation.right()};
1719 if (leftExpr.Rank() != rightExpr.Rank() && leftExpr.Rank() != 0 &&
1720 rightExpr.Rank() != 0) {
1721 return std::nullopt; // error recovery
1722 }
1723 leftExpr = Fold(context, std::move(leftExpr));
1724 rightExpr = Fold(context, std::move(rightExpr));
1725 if (leftExpr.Rank() > 0) {
1726 if (std::optional<Shape> leftShape{GetShape(context, leftExpr)}) {
1727 if (auto left{AsFlatArrayConstructor(leftExpr)}) {
1728 if (rightExpr.Rank() > 0) {
1729 if (std::optional<Shape> rightShape{GetShape(context, rightExpr)}) {
1730 if (auto right{AsFlatArrayConstructor(rightExpr)}) {
1731 if (CheckConformance(context.messages(), *leftShape, *rightShape,
1732 CheckConformanceFlags::EitherScalarExpandable)
1733 .value_or(false /*fail if not known now to conform*/)) {
1734 return MapOperation(context, std::move(f), *leftShape,
1735 std::move(resultLength), std::move(*left),
1736 std::move(*right));
1737 } else {
1738 return std::nullopt;
1739 }
1740 return MapOperation(context, std::move(f), *leftShape,
1741 std::move(resultLength), std::move(*left), std::move(*right));
1742 }
1743 }
1744 } else if (IsExpandableScalar(rightExpr, context, *leftShape)) {
1745 return MapOperation(context, std::move(f), *leftShape,
1746 std::move(resultLength), std::move(*left), rightExpr);
1747 }
1748 }
1749 }
1750 } else if (rightExpr.Rank() > 0) {
1751 if (std::optional<Shape> rightShape{GetShape(context, rightExpr)}) {
1752 if (IsExpandableScalar(leftExpr, context, *rightShape)) {
1753 if (auto right{AsFlatArrayConstructor(rightExpr)}) {
1754 return MapOperation(context, std::move(f), *rightShape,
1755 std::move(resultLength), leftExpr, std::move(*right));
1756 }
1757 }
1758 }
1759 }
1760 return std::nullopt;
1761}
1762
1763template <typename DERIVED, typename RESULT, typename LEFT, typename RIGHT>
1764auto ApplyElementwise(
1766 -> std::optional<Expr<RESULT>> {
1767 return ApplyElementwise(context, operation,
1768 std::function<Expr<RESULT>(Expr<LEFT> &&, Expr<RIGHT> &&)>{
1769 [](Expr<LEFT> &&left, Expr<RIGHT> &&right) {
1770 return Expr<RESULT>{DERIVED{std::move(left), std::move(right)}};
1771 }});
1772}
1773
1774// Unary operations
1775
1776template <typename TO, typename FROM>
1777common::IfNoLvalue<std::optional<TO>, FROM> ConvertString(FROM &&s) {
1778 if constexpr (std::is_same_v<TO, FROM>) {
1779 return std::make_optional<TO>(std::move(s));
1780 } else {
1781 // Fortran character conversion is well defined between distinct kinds
1782 // only when the actual characters are valid 7-bit ASCII.
1783 TO str;
1784 for (auto iter{s.cbegin()}; iter != s.cend(); ++iter) {
1785 if (static_cast<std::uint64_t>(*iter) > 127) {
1786 return std::nullopt;
1787 }
1788 str.push_back(static_cast<typename TO::value_type>(*iter));
1789 }
1790 return std::make_optional<TO>(std::move(str));
1791 }
1792}
1793
1794template <typename TO, TypeCategory FROMCAT>
1795Expr<TO> FoldOperation(
1796 FoldingContext &context, Convert<TO, FROMCAT> &&convert) {
1797 if (auto array{ApplyElementwise(context, convert)}) {
1798 return *array;
1799 }
1800 struct {
1801 FoldingContext &context;
1802 Convert<TO, FROMCAT> &convert;
1803 } msvcWorkaround{context, convert};
1804 return common::visit(
1805 [&msvcWorkaround](auto &kindExpr) -> Expr<TO> {
1806 using Operand = ResultType<decltype(kindExpr)>;
1807 // This variable is a workaround for msvc which emits an error when
1808 // using the FROMCAT template parameter below.
1809 TypeCategory constexpr FromCat{FROMCAT};
1810 static_assert(FromCat == Operand::category);
1811 auto &convert{msvcWorkaround.convert};
1812 if (auto value{GetScalarConstantValue<Operand>(kindExpr)}) {
1813 FoldingContext &ctx{msvcWorkaround.context};
1814 if constexpr (TO::category == TypeCategory::Integer) {
1815 if constexpr (FromCat == TypeCategory::Integer) {
1816 auto converted{Scalar<TO>::ConvertSigned(*value)};
1817 if (converted.overflow) {
1818 ctx.Warn(common::UsageWarning::FoldingException,
1819 "conversion of %s_%d to INTEGER(%d) overflowed; result is %s"_warn_en_US,
1820 value->SignedDecimal(), Operand::kind, TO::kind,
1821 converted.value.SignedDecimal());
1822 }
1823 return ScalarConstantToExpr(std::move(converted.value));
1824 } else if constexpr (FromCat == TypeCategory::Unsigned) {
1825 auto converted{Scalar<TO>::ConvertUnsigned(*value)};
1826 if ((converted.overflow || converted.value.IsNegative())) {
1827 ctx.Warn(common::UsageWarning::FoldingException,
1828 "conversion of %s_U%d to INTEGER(%d) overflowed; result is %s"_warn_en_US,
1829 value->UnsignedDecimal(), Operand::kind, TO::kind,
1830 converted.value.SignedDecimal());
1831 }
1832 return ScalarConstantToExpr(std::move(converted.value));
1833 } else if constexpr (FromCat == TypeCategory::Real) {
1834 auto converted{value->template ToInteger<Scalar<TO>>()};
1835 if (converted.flags.test(RealFlag::InvalidArgument)) {
1836 ctx.Warn(common::UsageWarning::FoldingException,
1837 "REAL(%d) to INTEGER(%d) conversion: invalid argument"_warn_en_US,
1838 Operand::kind, TO::kind);
1839 } else if (converted.flags.test(RealFlag::Overflow)) {
1840 ctx.Warn(common::UsageWarning::FoldingException,
1841 "REAL(%d) to INTEGER(%d) conversion overflowed"_warn_en_US,
1842 Operand::kind, TO::kind);
1843 }
1844 return ScalarConstantToExpr(std::move(converted.value));
1845 }
1846 } else if constexpr (TO::category == TypeCategory::Unsigned) {
1847 if constexpr (FromCat == TypeCategory::Integer ||
1848 FromCat == TypeCategory::Unsigned) {
1849 return Expr<TO>{
1850 Constant<TO>{Scalar<TO>::ConvertUnsigned(*value).value}};
1851 } else if constexpr (FromCat == TypeCategory::Real) {
1852 return Expr<TO>{
1853 Constant<TO>{value->template ToInteger<Scalar<TO>>().value}};
1854 }
1855 } else if constexpr (TO::category == TypeCategory::Real) {
1856 if constexpr (FromCat == TypeCategory::Integer ||
1857 FromCat == TypeCategory::Unsigned) {
1858 auto converted{Scalar<TO>::FromInteger(
1859 *value, FromCat == TypeCategory::Unsigned)};
1860 if (!converted.flags.empty()) {
1861 char buffer[64];
1862 std::snprintf(buffer, sizeof buffer,
1863 "INTEGER(%d) to REAL(%d) conversion", Operand::kind,
1864 TO::kind);
1865 ctx.RealFlagWarnings(converted.flags, buffer);
1866 }
1867 return ScalarConstantToExpr(std::move(converted.value));
1868 } else if constexpr (FromCat == TypeCategory::Real) {
1869 auto converted{Scalar<TO>::Convert(*value)};
1870 char buffer[64];
1871 if (!converted.flags.empty()) {
1872 std::snprintf(buffer, sizeof buffer,
1873 "REAL(%d) to REAL(%d) conversion", Operand::kind, TO::kind);
1874 ctx.RealFlagWarnings(converted.flags, buffer);
1875 }
1876 if (ctx.targetCharacteristics().areSubnormalsFlushedToZero()) {
1877 converted.value = converted.value.FlushSubnormalToZero();
1878 }
1879 return ScalarConstantToExpr(std::move(converted.value));
1880 }
1881 } else if constexpr (TO::category == TypeCategory::Complex) {
1882 if constexpr (FromCat == TypeCategory::Complex) {
1883 return FoldOperation(ctx,
1885 AsExpr(Convert<typename TO::Part>{AsCategoryExpr(
1886 Constant<typename Operand::Part>{value->REAL()})}),
1887 AsExpr(Convert<typename TO::Part>{AsCategoryExpr(
1888 Constant<typename Operand::Part>{value->AIMAG()})})});
1889 }
1890 } else if constexpr (TO::category == TypeCategory::Character &&
1891 FromCat == TypeCategory::Character) {
1892 if (auto converted{ConvertString<Scalar<TO>>(std::move(*value))}) {
1893 return ScalarConstantToExpr(std::move(*converted));
1894 }
1895 } else if constexpr (TO::category == TypeCategory::Logical &&
1896 FromCat == TypeCategory::Logical) {
1897 return Expr<TO>{value->IsTrue()};
1898 }
1899 } else if constexpr (TO::category == FromCat &&
1900 FromCat != TypeCategory::Character) {
1901 // Conversion of non-constant in same type category
1902 if constexpr (std::is_same_v<Operand, TO>) {
1903 return std::move(kindExpr); // remove needless conversion
1904 } else if constexpr (TO::category == TypeCategory::Logical ||
1905 TO::category == TypeCategory::Integer) {
1906 if (auto *innerConv{
1907 std::get_if<Convert<Operand, TO::category>>(&kindExpr.u)}) {
1908 // Conversion of conversion of same category & kind
1909 if (auto *x{std::get_if<Expr<TO>>(&innerConv->left().u)}) {
1910 if constexpr (TO::category == TypeCategory::Logical ||
1911 TO::kind <= Operand::kind) {
1912 return std::move(*x); // no-op Logical or Integer
1913 // widening/narrowing conversion pair
1914 } else if constexpr (std::is_same_v<TO,
1915 DescriptorInquiry::Result>) {
1916 if (std::holds_alternative<DescriptorInquiry>(x->u) ||
1917 std::holds_alternative<TypeParamInquiry>(x->u)) {
1918 // int(int(size(...),kind=k),kind=8) -> size(...)
1919 return std::move(*x);
1920 }
1921 }
1922 }
1923 }
1924 }
1925 }
1926 return Expr<TO>{std::move(convert)};
1927 },
1928 convert.left().u);
1929}
1930
1931template <typename T>
1932Expr<T> FoldOperation(FoldingContext &context, Parentheses<T> &&x) {
1933 auto &operand{x.left()};
1934 operand = Fold(context, std::move(operand));
1935 if (auto value{GetScalarConstantValue<T>(operand)}) {
1936 // Preserve parentheses, even around constants.
1937 return Expr<T>{Parentheses<T>{Expr<T>{Constant<T>{*value}}}};
1938 } else if (std::holds_alternative<Parentheses<T>>(operand.u)) {
1939 // ((x)) -> (x)
1940 return std::move(operand);
1941 } else {
1942 return Expr<T>{Parentheses<T>{std::move(operand)}};
1943 }
1944}
1945
1946template <typename T>
1947Expr<T> FoldOperation(FoldingContext &context, Negate<T> &&x) {
1948 if (auto array{ApplyElementwise(context, x)}) {
1949 return *array;
1950 }
1951 auto &operand{x.left()};
1952 if (auto *nn{std::get_if<Negate<T>>(&x.left().u)}) {
1953 // -(-x) -> (x)
1954 if (IsVariable(nn->left())) {
1955 return FoldOperation(context, Parentheses<T>{std::move(nn->left())});
1956 } else {
1957 return std::move(nn->left());
1958 }
1959 } else if (auto value{GetScalarConstantValue<T>(operand)}) {
1960 if constexpr (T::category == TypeCategory::Integer) {
1961 auto negated{value->Negate()};
1962 if (negated.overflow) {
1963 context.Warn(common::UsageWarning::FoldingException,
1964 "INTEGER(%d) negation overflowed"_warn_en_US, T::kind);
1965 }
1966 return Expr<T>{Constant<T>{std::move(negated.value)}};
1967 } else if constexpr (T::category == TypeCategory::Unsigned) {
1968 return Expr<T>{Constant<T>{std::move(value->Negate().value)}};
1969 } else {
1970 // REAL & COMPLEX negation: no exceptions possible
1971 return Expr<T>{Constant<T>{value->Negate()}};
1972 }
1973 }
1974 return Expr<T>{std::move(x)};
1975}
1976
1977// Binary (dyadic) operations
1978
1979template <typename LEFT, typename RIGHT>
1980std::optional<std::pair<Scalar<LEFT>, Scalar<RIGHT>>> OperandsAreConstants(
1981 const Expr<LEFT> &x, const Expr<RIGHT> &y) {
1982 if (auto xvalue{GetScalarConstantValue<LEFT>(x)}) {
1983 if (auto yvalue{GetScalarConstantValue<RIGHT>(y)}) {
1984 return {std::make_pair(*xvalue, *yvalue)};
1985 }
1986 }
1987 return std::nullopt;
1988}
1989
1990template <typename DERIVED, typename RESULT, typename LEFT, typename RIGHT>
1991std::optional<std::pair<Scalar<LEFT>, Scalar<RIGHT>>> OperandsAreConstants(
1992 const Operation<DERIVED, RESULT, LEFT, RIGHT> &operation) {
1993 return OperandsAreConstants(operation.left(), operation.right());
1994}
1995
1996template <typename T>
1997Expr<T> FoldOperation(FoldingContext &context, Add<T> &&x) {
1998 if (auto array{ApplyElementwise(context, x)}) {
1999 return *array;
2000 }
2001 if (auto folded{OperandsAreConstants(x)}) {
2002 if constexpr (T::category == TypeCategory::Integer) {
2003 auto sum{folded->first.AddSigned(folded->second)};
2004 if (sum.overflow) {
2005 context.Warn(common::UsageWarning::FoldingException,
2006 "INTEGER(%d) addition overflowed"_warn_en_US, T::kind);
2007 }
2008 return Expr<T>{Constant<T>{sum.value}};
2009 } else if constexpr (T::category == TypeCategory::Unsigned) {
2010 return Expr<T>{
2011 Constant<T>{folded->first.AddUnsigned(folded->second).value}};
2012 } else {
2013 auto sum{folded->first.Add(
2014 folded->second, context.targetCharacteristics().roundingMode())};
2015 context.RealFlagWarnings(sum.flags, "addition");
2016 if (context.targetCharacteristics().areSubnormalsFlushedToZero()) {
2017 sum.value = sum.value.FlushSubnormalToZero();
2018 }
2019 return Expr<T>{Constant<T>{sum.value}};
2020 }
2021 } else if constexpr (T::category == TypeCategory::Integer ||
2022 T::category == TypeCategory::Unsigned) {
2023 if (auto c{GetScalarConstantValue<T>(x.right())}) {
2024 if (c->IsZero() && x.left().Rank() == 0) {
2025 if (IsVariable(x.left())) {
2026 return FoldOperation(context, Parentheses<T>{std::move(x.left())});
2027 } else {
2028 return std::move(x.left());
2029 }
2030 }
2031 } else if (auto c{GetScalarConstantValue<T>(x.left())}) {
2032 if (c->IsZero() && x.right().Rank() == 0) {
2033 if (IsVariable(x.right())) {
2034 return FoldOperation(context, Parentheses<T>{std::move(x.right())});
2035 } else {
2036 return std::move(x.right());
2037 }
2038 }
2039 }
2040 }
2041 return Expr<T>{std::move(x)};
2042}
2043
2044template <typename T>
2045Expr<T> FoldOperation(FoldingContext &context, Subtract<T> &&x) {
2046 if (auto array{ApplyElementwise(context, x)}) {
2047 return *array;
2048 }
2049 if (auto folded{OperandsAreConstants(x)}) {
2050 if constexpr (T::category == TypeCategory::Integer) {
2051 auto difference{folded->first.SubtractSigned(folded->second)};
2052 if (difference.overflow) {
2053 context.Warn(common::UsageWarning::FoldingException,
2054 "INTEGER(%d) subtraction overflowed"_warn_en_US, T::kind);
2055 }
2056 return Expr<T>{Constant<T>{difference.value}};
2057 } else if constexpr (T::category == TypeCategory::Unsigned) {
2058 return Expr<T>{
2059 Constant<T>{folded->first.SubtractSigned(folded->second).value}};
2060 } else {
2061 auto difference{folded->first.Subtract(
2062 folded->second, context.targetCharacteristics().roundingMode())};
2063 context.RealFlagWarnings(difference.flags, "subtraction");
2064 if (context.targetCharacteristics().areSubnormalsFlushedToZero()) {
2065 difference.value = difference.value.FlushSubnormalToZero();
2066 }
2067 return Expr<T>{Constant<T>{difference.value}};
2068 }
2069 } else if constexpr (T::category == TypeCategory::Integer ||
2070 T::category == TypeCategory::Unsigned) {
2071 if (auto c{GetScalarConstantValue<T>(x.right())}) {
2072 if (c->IsZero() && x.left().Rank() == 0) {
2073 if (IsVariable(x.left())) {
2074 return FoldOperation(context, Parentheses<T>{std::move(x.left())});
2075 } else {
2076 return std::move(x.left());
2077 }
2078 }
2079 }
2080 }
2081 return Expr<T>{std::move(x)};
2082}
2083
2084template <typename T>
2085Expr<T> FoldOperation(FoldingContext &context, Multiply<T> &&x) {
2086 if (auto array{ApplyElementwise(context, x)}) {
2087 return *array;
2088 }
2089 if (auto folded{OperandsAreConstants(x)}) {
2090 if constexpr (T::category == TypeCategory::Integer) {
2091 auto product{folded->first.MultiplySigned(folded->second)};
2092 if (product.SignedMultiplicationOverflowed()) {
2093 context.Warn(common::UsageWarning::FoldingException,
2094 "INTEGER(%d) multiplication overflowed"_warn_en_US, T::kind);
2095 }
2096 return Expr<T>{Constant<T>{product.lower}};
2097 } else if constexpr (T::category == TypeCategory::Unsigned) {
2098 return Expr<T>{
2099 Constant<T>{folded->first.MultiplyUnsigned(folded->second).lower}};
2100 } else {
2101 auto product{folded->first.Multiply(
2102 folded->second, context.targetCharacteristics().roundingMode())};
2103 context.RealFlagWarnings(product.flags, "multiplication");
2104 if (context.targetCharacteristics().areSubnormalsFlushedToZero()) {
2105 product.value = product.value.FlushSubnormalToZero();
2106 }
2107 return Expr<T>{Constant<T>{product.value}};
2108 }
2109 } else if constexpr (T::category == TypeCategory::Integer) {
2110 if (auto c{GetScalarConstantValue<T>(x.right())}) {
2111 x.right() = std::move(x.left());
2112 x.left() = Expr<T>{std::move(*c)};
2113 }
2114 if (auto c{GetScalarConstantValue<T>(x.left())}) {
2115 if (c->IsZero() && x.right().Rank() == 0) {
2116 return std::move(x.left());
2117 } else if (c->CompareSigned(Scalar<T>{1}) == Ordering::Equal) {
2118 if (IsVariable(x.right())) {
2119 return FoldOperation(context, Parentheses<T>{std::move(x.right())});
2120 } else {
2121 return std::move(x.right());
2122 }
2123 } else if (c->CompareSigned(Scalar<T>{-1}) == Ordering::Equal) {
2124 return FoldOperation(context, Negate<T>{std::move(x.right())});
2125 }
2126 }
2127 }
2128 return Expr<T>{std::move(x)};
2129}
2130
2131template <typename T>
2132Expr<T> FoldOperation(FoldingContext &context, Divide<T> &&x) {
2133 if (auto array{ApplyElementwise(context, x)}) {
2134 return *array;
2135 }
2136 if (auto folded{OperandsAreConstants(x)}) {
2137 if constexpr (T::category == TypeCategory::Integer) {
2138 auto quotAndRem{folded->first.DivideSigned(folded->second)};
2139 if (quotAndRem.divisionByZero) {
2140 context.Warn(common::UsageWarning::FoldingException,
2141 "INTEGER(%d) division by zero"_warn_en_US, T::kind);
2142 return Expr<T>{std::move(x)};
2143 }
2144 if (quotAndRem.overflow) {
2145 context.Warn(common::UsageWarning::FoldingException,
2146 "INTEGER(%d) division overflowed"_warn_en_US, T::kind);
2147 }
2148 return Expr<T>{Constant<T>{quotAndRem.quotient}};
2149 } else if constexpr (T::category == TypeCategory::Unsigned) {
2150 auto quotAndRem{folded->first.DivideUnsigned(folded->second)};
2151 if (quotAndRem.divisionByZero) {
2152 context.Warn(common::UsageWarning::FoldingException,
2153 "UNSIGNED(%d) division by zero"_warn_en_US, T::kind);
2154 return Expr<T>{std::move(x)};
2155 }
2156 return Expr<T>{Constant<T>{quotAndRem.quotient}};
2157 } else {
2158 auto quotient{folded->first.Divide(
2159 folded->second, context.targetCharacteristics().roundingMode())};
2160 // Don't warn about -1./0., 0./0., or 1./0. from a module file
2161 // they are interpreted as canonical Fortran representations of -Inf,
2162 // NaN, and Inf respectively.
2163 bool isCanonicalNaNOrInf{false};
2164 if constexpr (T::category == TypeCategory::Real) {
2165 if (folded->second.IsZero() && context.moduleFileName().has_value()) {
2166 using IntType = typename T::Scalar::Word;
2167 auto intNumerator{folded->first.template ToInteger<IntType>()};
2168 isCanonicalNaNOrInf = intNumerator.flags == RealFlags{} &&
2169 intNumerator.value >= IntType{-1} &&
2170 intNumerator.value <= IntType{1};
2171 }
2172 }
2173 if (!isCanonicalNaNOrInf) {
2174 context.RealFlagWarnings(quotient.flags, "division");
2175 }
2176 if (context.targetCharacteristics().areSubnormalsFlushedToZero()) {
2177 quotient.value = quotient.value.FlushSubnormalToZero();
2178 }
2179 return Expr<T>{Constant<T>{quotient.value}};
2180 }
2181 }
2182 return Expr<T>{std::move(x)};
2183}
2184
2185template <typename T>
2186Expr<T> FoldOperation(FoldingContext &context, Power<T> &&x) {
2187 if (auto array{ApplyElementwise(context, x)}) {
2188 return *array;
2189 }
2190 if (auto folded{OperandsAreConstants(x)}) {
2191 if constexpr (T::category == TypeCategory::Integer) {
2192 auto power{folded->first.Power(folded->second)};
2193 if (power.divisionByZero) {
2194 context.Warn(common::UsageWarning::FoldingException,
2195 "INTEGER(%d) zero to negative power"_warn_en_US, T::kind);
2196 } else if (power.overflow) {
2197 context.Warn(common::UsageWarning::FoldingException,
2198 "INTEGER(%d) power overflowed"_warn_en_US, T::kind);
2199 } else if (power.zeroToZero) {
2200 context.Warn(common::UsageWarning::FoldingException,
2201 "INTEGER(%d) 0**0 is not defined"_warn_en_US, T::kind);
2202 }
2203 return Expr<T>{Constant<T>{power.power}};
2204 } else {
2205 if (folded->first.IsZero()) {
2206 if (folded->second.IsZero()) {
2207 context.Warn(common::UsageWarning::FoldingException,
2208 "REAL/COMPLEX 0**0 is not defined"_warn_en_US);
2209 } else {
2210 return Expr<T>(Constant<T>{folded->first}); // 0. ** nonzero -> 0.
2211 }
2212 } else if (auto callable{GetHostRuntimeWrapper<T, T, T>("pow")}) {
2213 return Expr<T>{
2214 Constant<T>{(*callable)(context, folded->first, folded->second)}};
2215 } else {
2216 context.Warn(common::UsageWarning::FoldingFailure,
2217 "Power for %s cannot be folded on host"_warn_en_US,
2218 T{}.AsFortran());
2219 }
2220 }
2221 }
2222 return Expr<T>{std::move(x)};
2223}
2224
2225template <typename T>
2226Expr<T> FoldOperation(FoldingContext &context, RealToIntPower<T> &&x) {
2227 if (auto array{ApplyElementwise(context, x)}) {
2228 return *array;
2229 }
2230 return common::visit(
2231 [&](auto &y) -> Expr<T> {
2232 if (auto folded{OperandsAreConstants(x.left(), y)}) {
2233 auto power{evaluate::IntPower(folded->first, folded->second)};
2234 context.RealFlagWarnings(power.flags, "power with INTEGER exponent");
2235 if (context.targetCharacteristics().areSubnormalsFlushedToZero()) {
2236 power.value = power.value.FlushSubnormalToZero();
2237 }
2238 return Expr<T>{Constant<T>{power.value}};
2239 } else {
2240 return Expr<T>{std::move(x)};
2241 }
2242 },
2243 x.right().u);
2244}
2245
2246template <typename T>
2247Expr<T> FoldOperation(FoldingContext &context, ConditionalExpr<T> &&x) {
2248 x.condition() = Fold(context, std::move(x.condition()));
2249 // If the condition is a scalar logical constant, select the branch.
2250 if (auto cst{GetScalarConstantValue<LogicalResult>(x.condition())}) {
2251 return cst->IsTrue() ? Fold(context, std::move(x.thenValue()))
2252 : Fold(context, std::move(x.elseValue()));
2253 }
2254 return Expr<T>{std::move(x)};
2255}
2256
2257template <typename T>
2258Expr<T> FoldOperation(FoldingContext &context, Extremum<T> &&x) {
2259 if (auto array{ApplyElementwise(context, x,
2260 std::function<Expr<T>(Expr<T> &&, Expr<T> &&)>{[=](Expr<T> &&l,
2261 Expr<T> &&r) {
2262 return Expr<T>{Extremum<T>{x.ordering, std::move(l), std::move(r)}};
2263 }})}) {
2264 return *array;
2265 }
2266 if (auto folded{OperandsAreConstants(x)}) {
2267 if constexpr (T::category == TypeCategory::Integer) {
2268 if (folded->first.CompareSigned(folded->second) == x.ordering) {
2269 return Expr<T>{Constant<T>{folded->first}};
2270 }
2271 } else if constexpr (T::category == TypeCategory::Unsigned) {
2272 if (folded->first.CompareUnsigned(folded->second) == x.ordering) {
2273 return Expr<T>{Constant<T>{folded->first}};
2274 }
2275 } else if constexpr (T::category == TypeCategory::Real) {
2276 if (folded->first.IsNotANumber() ||
2277 (folded->first.Compare(folded->second) == Relation::Less) ==
2278 (x.ordering == Ordering::Less)) {
2279 return Expr<T>{Constant<T>{folded->first}};
2280 }
2281 } else {
2282 static_assert(T::category == TypeCategory::Character);
2283 // Result of MIN and MAX on character has the length of
2284 // the longest argument.
2285 auto maxLen{std::max(folded->first.length(), folded->second.length())};
2286 bool isFirst{x.ordering == Compare(folded->first, folded->second)};
2287 auto res{isFirst ? std::move(folded->first) : std::move(folded->second)};
2288 res = res.length() == maxLen
2289 ? std::move(res)
2290 : CharacterUtils<T::kind>::Resize(res, maxLen);
2291 return Expr<T>{Constant<T>{std::move(res)}};
2292 }
2293 return Expr<T>{Constant<T>{folded->second}};
2294 }
2295 return Expr<T>{std::move(x)};
2296}
2297
2298template <int KIND>
2300 FoldingContext &context, Expr<SomeType> &&expr) {
2301 using Result = Type<TypeCategory::Real, KIND>;
2302 std::optional<Expr<Result>> result;
2303 common::visit(
2304 [&](auto &&x) {
2305 using From = std::decay_t<decltype(x)>;
2306 if constexpr (std::is_same_v<From, BOZLiteralConstant>) {
2307 // Move the bits without any integer->real conversion
2308 From original{x};
2309 result = ConvertToType<Result>(std::move(x));
2310 const auto *constant{UnwrapExpr<Constant<Result>>(*result)};
2311 CHECK(constant);
2312 Scalar<Result> real{constant->GetScalarValue().value()};
2313 From converted{From::ConvertUnsigned(real.RawBits()).value};
2314 if (original != converted) { // C1601
2315 context.Warn(common::UsageWarning::FoldingValueChecks,
2316 "Nonzero bits truncated from BOZ literal constant in REAL intrinsic"_warn_en_US);
2317 }
2318 } else if constexpr (IsNumericCategoryExpr<From>()) {
2319 result = Fold(context, ConvertToType<Result>(std::move(x)));
2320 } else {
2321 common::die("ToReal: bad argument expression");
2322 }
2323 },
2324 std::move(expr.u));
2325 return result.value();
2326}
2327
2328// REAL(z) and AIMAG(z)
2329template <int KIND>
2331 FoldingContext &context, ComplexComponent<KIND> &&x) {
2332 using Operand = Type<TypeCategory::Complex, KIND>;
2333 using Result = Type<TypeCategory::Real, KIND>;
2334 if (auto array{ApplyElementwise(context, x,
2335 std::function<Expr<Result>(Expr<Operand> &&)>{
2336 [=](Expr<Operand> &&operand) {
2338 x.isImaginaryPart, std::move(operand)}};
2339 }})}) {
2340 return *array;
2341 }
2342 auto &operand{x.left()};
2343 if (auto value{GetScalarConstantValue<Operand>(operand)}) {
2344 if (x.isImaginaryPart) {
2345 return Expr<Result>{Constant<Result>{value->AIMAG()}};
2346 } else {
2347 return Expr<Result>{Constant<Result>{value->REAL()}};
2348 }
2349 }
2350 return Expr<Result>{std::move(x)};
2351}
2352
2353template <typename T>
2354Expr<T> ExpressionBase<T>::Rewrite(FoldingContext &context, Expr<T> &&expr) {
2355 return common::visit(
2356 [&](auto &&x) -> Expr<T> {
2357 if constexpr (IsSpecificIntrinsicType<T>) {
2358 return FoldOperation(context, std::move(x));
2359 } else if constexpr (std::is_same_v<T, SomeDerived>) {
2360 return FoldOperation(context, std::move(x));
2361 } else if constexpr (common::HasMember<decltype(x),
2362 TypelessExpression>) {
2363 return std::move(expr);
2364 } else {
2365 return Expr<T>{Fold(context, std::move(x))};
2366 }
2367 },
2368 std::move(expr.u));
2369}
2370
2371FOR_EACH_TYPE_AND_KIND(extern template class ExpressionBase, )
2372} // namespace Fortran::evaluate
2373#endif // FORTRAN_EVALUATE_FOLD_IMPLEMENTATION_H_
Definition fold-implementation.h:1301
Definition expression.h:507
Definition variable.h:205
Definition variable.h:243
Definition variable.h:357
Definition variable.h:73
Definition expression.h:395
Definition constant.h:60
Definition constant.h:147
Definition variable.h:381
Definition type.h:74
Definition common.h:215
Definition expression.h:66
Definition fold-implementation.h:55
Definition common.h:217
Definition call.h:294
Definition expression.h:445
Definition variable.h:101
Definition expression.h:114
Definition expression.h:782
Definition variable.h:304
Definition variable.h:160
Definition variable.h:136
Definition type.h:57
Definition symbol.h:832
Definition call.h:34
Definition ParserActions.h:24
Definition expression.h:296
Definition expression.h:473
Definition expression.h:257
Definition expression.h:357
Definition expression.h:211
Definition variable.h:288
Definition expression.h:317
Definition expression.h:340
Definition expression.h:437
Definition expression.h:310
Definition expression.h:247
Definition expression.h:229
Definition expression.h:324
Definition expression.h:332
Definition type.h:399
Definition variable.h:191
Definition expression.h:303