FLANG
fold.h
1//===-- include/flang/Evaluate/fold.h ---------------------------*- C++ -*-===//
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_H_
10#define FORTRAN_EVALUATE_FOLD_H_
11
12// Implements expression tree rewriting, particularly constant expression
13// and designator reference evaluation.
14
15#include "common.h"
16#include "constant.h"
17#include "expression.h"
18#include "tools.h"
19#include "type.h"
20
21namespace Fortran::evaluate::characteristics {
22class TypeAndShape;
23}
24
25namespace Fortran::evaluate {
26
27using namespace Fortran::parser::literals;
28
29// Fold() rewrites an expression and returns it. When the rewritten expression
30// is a constant, UnwrapConstantValue() and GetScalarConstantValue() below will
31// be able to extract it.
32// Note the rvalue reference argument: the rewrites are performed in place
33// for efficiency.
34template <typename T> Expr<T> Fold(FoldingContext &context, Expr<T> &&expr) {
35 return Expr<T>::Rewrite(context, std::move(expr));
36}
37
40
41template <typename A>
42std::optional<A> Fold(FoldingContext &context, std::optional<A> &&x) {
43 if (x) {
44 return Fold(context, std::move(*x));
45 } else {
46 return std::nullopt;
47 }
48}
49
50// UnwrapConstantValue() isolates the known constant value of
51// an expression, if it has one. It returns a pointer, which is
52// const-qualified when the expression is so. The value can be
53// parenthesized.
54template <typename T, typename EXPR>
55auto UnwrapConstantValue(EXPR &expr) -> common::Constify<Constant<T>, EXPR> * {
56 if (auto *c{UnwrapExpr<Constant<T>>(expr)}) {
57 return c;
58 } else {
59 if (auto *parens{UnwrapExpr<Parentheses<T>>(expr)}) {
60 return UnwrapConstantValue<T>(parens->left());
61 }
62 return nullptr;
63 }
64}
65template <typename T, typename EXPR>
66auto UnwrapConstantValue(int kind, EXPR &expr)
67 -> common::Constify<Constant<T>, EXPR> * {
68 if (auto *c{UnwrapConstantValue<T>(expr)}) {
69 if (c->kind() == kind) {
70 return c;
71 }
72 }
73 return nullptr;
74}
75
76// GetScalarConstantValue() extracts the known scalar constant value of
77// an expression, if it has one. The value can be parenthesized.
78template <typename T, typename EXPR>
79constexpr auto GetScalarConstantValue(const EXPR &expr)
80 -> std::optional<Scalar<T>> {
81 if (const Constant<T> *constant{UnwrapConstantValue<T>(expr)}) {
82 return constant->GetScalarValue();
83 } else {
84 return std::nullopt;
85 }
86}
87template <typename T, typename EXPR>
88constexpr auto GetScalarConstantValue(int kind, const EXPR &expr)
89 -> std::optional<Scalar<T>> {
90 if (const Constant<T> *constant{UnwrapConstantValue<T>(kind, expr)}) {
91 return constant->GetScalarValue();
92 } else {
93 return std::nullopt;
94 }
95}
96
97// When an expression is a constant integer, ToInt64() extracts its value.
98// Ensure that the expression has been folded beforehand when folding might
99// be required.
100inline std::optional<std::int64_t> ToInt64(
101 const Expr<Type<TypeCategory::Integer>> &expr) {
102 if (auto scalar{GetScalarConstantValue<Type<TypeCategory::Integer>>(expr)}) {
103 return scalar->ToInt64();
104 } else {
105 return std::nullopt;
106 }
107}
108inline std::optional<std::int64_t> ToInt64(
109 const Expr<Type<TypeCategory::Unsigned>> &expr) {
110 if (auto scalar{GetScalarConstantValue<Type<TypeCategory::Unsigned>>(expr)}) {
111 return scalar->ToInt64();
112 } else {
113 return std::nullopt;
114 }
115}
116
117std::optional<std::int64_t> ToInt64(const Expr<SomeType> &);
118std::optional<std::int64_t> ToInt64(const ActualArgument &);
119
120// When an expression is a constant logical scalar, ToLogical() extracts its
121// value.
122inline std::optional<bool> ToLogical(const Expr<LogicalResult> &expr) {
123 if (auto val{
124 GetScalarConstantValue<LogicalResult>(LogicalResultKind, expr)}) {
125 return val->IsTrue();
126 }
127 return std::nullopt;
128}
129
130template <typename A>
131std::optional<std::int64_t> ToInt64(const std::optional<A> &x) {
132 if (x) {
133 return ToInt64(*x);
134 } else {
135 return std::nullopt;
136 }
137}
138
139template <typename A> std::optional<std::int64_t> ToInt64(A *p) {
140 if (p) {
141 return ToInt64(std::as_const(*p));
142 } else {
143 return std::nullopt;
144 }
145}
146
147// Fold a conditional argument (F2023 R1526). Folds all sub-expressions
148// and simplifies constant conditions: .TRUE. selects the consequent,
149// .FALSE. skips to the tail. If fully resolved, replaces the
150// ActualArgument with a plain expression or sets it to std::nullopt
151// for .NIL.
152void FoldConditionalArg(
153 FoldingContext &context, std::optional<ActualArgument> &arg);
154
155} // namespace Fortran::evaluate
156#endif // FORTRAN_EVALUATE_FOLD_H_
Definition constant.h:181
Definition common.h:222
Definition common.h:224
Definition type.h:61
Definition call.h:34