FLANG
expression.h
1//===-- include/flang/Evaluate/expression.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_EXPRESSION_H_
10#define FORTRAN_EVALUATE_EXPRESSION_H_
11
12// Represent Fortran expressions in a type-safe manner.
13// Expressions are the sole owners of their constituents; i.e., there is no
14// context-independent hash table or sharing of common subexpressions, and
15// thus these are trees, not DAGs. Both deep copy and move semantics are
16// supported for expression construction. Expressions may be compared
17// for equality.
18
19#include "common.h"
20#include "constant.h"
21#include "formatting.h"
22#include "type.h"
23#include "variable.h"
24#include "flang/Common/idioms.h"
25#include "flang/Common/indirection.h"
26#include "flang/Common/template.h"
27#include "flang/Parser/char-block.h"
28#include "flang/Support/Fortran.h"
29#include <algorithm>
30#include <list>
31#include <tuple>
32#include <type_traits>
33#include <variant>
34
35namespace llvm {
36class raw_ostream;
37}
38
39namespace Fortran::evaluate {
40
41using common::LogicalOperator;
42using common::RelationalOperator;
43
44// Expressions are represented by specializations of the class template Expr.
45// Each of these specializations wraps a single data member "u" that
46// is a std::variant<> discriminated union over all of the representational
47// types for the constants, variables, operations, and other entities that
48// can be valid expressions in that context:
49// - Expr<Type<CATEGORY, KIND>> represents an expression whose result is of a
50// specific intrinsic type category and kind, e.g. Type<TypeCategory::Real, 4>
51// - Expr<SomeDerived> wraps data and procedure references that result in an
52// instance of a derived type (or CLASS(*) unlimited polymorphic)
53// - Expr<SomeKind<CATEGORY>> is a union of Expr<Type<CATEGORY, K>> for each
54// kind type parameter value K in that intrinsic type category. It represents
55// an expression with known category and any kind.
56// - Expr<SomeType> is a union of Expr<SomeKind<CATEGORY>> over the five
57// intrinsic type categories of Fortran. It represents any valid expression.
58//
59// Everything that can appear in, or as, a valid Fortran expression must be
60// represented with an instance of some class containing a Result typedef that
61// maps to some instantiation of Type<CATEGORY, KIND>, SomeKind<CATEGORY>,
62// or SomeType. (Exception: BOZ literal constants in generic Expr<SomeType>.)
63template <typename A> using ResultType = typename std::decay_t<A>::Result;
64
65// Common Expr<> behaviors: every Expr<T> derives from ExpressionBase<T>.
66template <typename RESULT> class ExpressionBase {
67public:
68 using Result = RESULT;
69
70private:
71 using Derived = Expr<Result>;
72#if defined(__APPLE__) && defined(__GNUC__)
73 Derived &derived();
74 const Derived &derived() const;
75#else
76 Derived &derived() { return *static_cast<Derived *>(this); }
77 const Derived &derived() const { return *static_cast<const Derived *>(this); }
78#endif
79
80public:
81 template <typename A> Derived &operator=(const A &x) {
82 Derived &d{derived()};
83 d.u = x;
84 return d;
85 }
86
87 template <typename A> common::IfNoLvalue<Derived &, A> operator=(A &&x) {
88 Derived &d{derived()};
89 d.u = std::move(x);
90 return d;
91 }
92
93 std::optional<DynamicType> GetType() const;
94 int Rank() const;
95 int Corank() const;
96 std::string AsFortran() const;
97#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
98 LLVM_DUMP_METHOD void dump() const;
99#endif
100 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
101 static Derived Rewrite(FoldingContext &, Derived &&);
102};
103
104// Operations always have specific Fortran result types (i.e., with known
105// intrinsic type category and kind parameter value). The classes that
106// represent the operations all inherit from this Operation<> base class
107// template. Note that Operation has as its first type parameter (DERIVED) a
108// "curiously reoccurring template pattern (CRTP)" reference to the specific
109// operation class being derived from Operation; e.g., Add is defined with
110// struct Add : public Operation<Add, ...>. Uses of instances of Operation<>,
111// including its own member functions, can access each specific class derived
112// from it via its derived() member function with compile-time type safety.
113template <typename DERIVED, typename RESULT, typename... OPERANDS>
114class Operation {
115 // The extra final member is a dummy that allows a safe unused reference
116 // to element 1 to arise indirectly in the definition of "right()" below
117 // when the operation has but a single operand.
118 using OperandTypes = std::tuple<OPERANDS..., std::monostate>;
119
120public:
121 using Derived = DERIVED;
122 using Result = RESULT;
123 static constexpr std::size_t operands{sizeof...(OPERANDS)};
124 // Allow specific intrinsic types and Parentheses<SomeDerived>
125 static_assert(IsSpecificIntrinsicType<Result> ||
126 (operands == 1 && std::is_same_v<Result, SomeDerived>));
127 template <int J> using Operand = std::tuple_element_t<J, OperandTypes>;
128
129 // Unary operations wrap a single Expr with a CopyableIndirection.
130 // Binary operations wrap a tuple of CopyableIndirections to Exprs.
131private:
132 using Container = std::conditional_t<operands == 1,
133 common::CopyableIndirection<Expr<Operand<0>>>,
134 std::tuple<common::CopyableIndirection<Expr<OPERANDS>>...>>;
135
136public:
137 CLASS_BOILERPLATE(Operation)
138 explicit Operation(const Expr<OPERANDS> &...x) : operand_{x...} {}
139 explicit Operation(Expr<OPERANDS> &&...x) : operand_{std::move(x)...} {}
140
141 Derived &derived() { return *static_cast<Derived *>(this); }
142 const Derived &derived() const { return *static_cast<const Derived *>(this); }
143
144 // References to operand expressions from member functions of derived
145 // classes for specific operators can be made by index, e.g. operand<0>(),
146 // which must be spelled like "this->template operand<0>()" when
147 // inherited in a derived class template. There are convenience aliases
148 // left() and right() that are not templates.
149 template <int J> Expr<Operand<J>> &operand() {
150 if constexpr (operands == 1) {
151 static_assert(J == 0);
152 return operand_.value();
153 } else {
154 return std::get<J>(operand_).value();
155 }
156 }
157 template <int J> const Expr<Operand<J>> &operand() const {
158 if constexpr (operands == 1) {
159 static_assert(J == 0);
160 return operand_.value();
161 } else {
162 return std::get<J>(operand_).value();
163 }
164 }
165
166 Expr<Operand<0>> &left() { return operand<0>(); }
167 const Expr<Operand<0>> &left() const { return operand<0>(); }
168
169 std::conditional_t<(operands > 1), Expr<Operand<1>> &, void> right() {
170 if constexpr (operands > 1) {
171 return operand<1>();
172 }
173 }
174 std::conditional_t<(operands > 1), const Expr<Operand<1>> &, void>
175 right() const {
176 if constexpr (operands > 1) {
177 return operand<1>();
178 }
179 }
180
181 static constexpr std::conditional_t<Result::category != TypeCategory::Derived,
182 std::optional<DynamicType>, void>
183 GetType() {
184 return Result::GetType();
185 }
186 int Rank() const {
187 int rank{left().Rank()};
188 if constexpr (operands > 1) {
189 return std::max(rank, right().Rank());
190 } else {
191 return rank;
192 }
193 }
194 static constexpr int Corank() { return 0; }
195
196 bool operator==(const Operation &that) const {
197 return operand_ == that.operand_;
198 }
199
200 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
201
202private:
203 Container operand_;
204};
205
206// Unary operations
207
208// Conversions to specific types from expressions of known category and
209// dynamic kind.
210template <typename TO, TypeCategory FROMCAT = TO::category>
211struct Convert : public Operation<Convert<TO, FROMCAT>, TO, SomeKind<FROMCAT>> {
212 // Fortran doesn't have conversions between kinds of CHARACTER apart from
213 // assignments, and in those the data must be convertible to/from 7-bit ASCII.
214 static_assert(
215 ((TO::category == TypeCategory::Integer ||
216 TO::category == TypeCategory::Real ||
217 TO::category == TypeCategory::Unsigned) &&
218 (FROMCAT == TypeCategory::Integer || FROMCAT == TypeCategory::Real ||
219 FROMCAT == TypeCategory::Unsigned)) ||
220 TO::category == FROMCAT);
221 using Result = TO;
222 using Operand = SomeKind<FROMCAT>;
223 using Base = Operation<Convert, Result, Operand>;
224 using Base::Base;
225 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
226};
227
228template <typename A>
229struct Parentheses : public Operation<Parentheses<A>, A, A> {
230 using Result = A;
231 using Operand = A;
232 using Base = Operation<Parentheses, A, A>;
233 using Base::Base;
234};
235
236template <>
237struct Parentheses<SomeDerived>
238 : public Operation<Parentheses<SomeDerived>, SomeDerived, SomeDerived> {
239public:
240 using Result = SomeDerived;
241 using Operand = SomeDerived;
242 using Base = Operation<Parentheses, SomeDerived, SomeDerived>;
243 using Base::Base;
244 DynamicType GetType() const;
245};
246
247template <typename A> struct Negate : public Operation<Negate<A>, A, A> {
248 using Result = A;
249 using Operand = A;
250 using Base = Operation<Negate, A, A>;
251 using Base::Base;
252};
253
254template <int KIND>
255struct ComplexComponent
256 : public Operation<ComplexComponent<KIND>, Type<TypeCategory::Real, KIND>,
257 Type<TypeCategory::Complex, KIND>> {
258 using Result = Type<TypeCategory::Real, KIND>;
259 using Operand = Type<TypeCategory::Complex, KIND>;
260 using Base = Operation<ComplexComponent, Result, Operand>;
261 CLASS_BOILERPLATE(ComplexComponent)
262 ComplexComponent(bool isImaginary, const Expr<Operand> &x)
263 : Base{x}, isImaginaryPart{isImaginary} {}
264 ComplexComponent(bool isImaginary, Expr<Operand> &&x)
265 : Base{std::move(x)}, isImaginaryPart{isImaginary} {}
266
267 bool isImaginaryPart{true};
268};
269
270template <int KIND>
271struct Not : public Operation<Not<KIND>, Type<TypeCategory::Logical, KIND>,
272 Type<TypeCategory::Logical, KIND>> {
274 using Operand = Result;
275 using Base = Operation<Not, Result, Operand>;
276 using Base::Base;
277};
278
279// Character lengths are determined by context in Fortran and do not
280// have explicit syntax for changing them. Expressions represent
281// changes of length (e.g., for assignments and structure constructors)
282// with this operation.
283template <int KIND>
285 : public Operation<SetLength<KIND>, Type<TypeCategory::Character, KIND>,
286 Type<TypeCategory::Character, KIND>, SubscriptInteger> {
288 using CharacterOperand = Result;
289 using LengthOperand = SubscriptInteger;
290 using Base = Operation<SetLength, Result, CharacterOperand, LengthOperand>;
291 using Base::Base;
292};
293
294// Binary operations
295
296template <typename A> struct Add : public Operation<Add<A>, A, A, A> {
297 using Result = A;
298 using Operand = A;
299 using Base = Operation<Add, A, A, A>;
300 using Base::Base;
301};
302
303template <typename A> struct Subtract : public Operation<Subtract<A>, A, A, A> {
304 using Result = A;
305 using Operand = A;
306 using Base = Operation<Subtract, A, A, A>;
307 using Base::Base;
308};
309
310template <typename A> struct Multiply : public Operation<Multiply<A>, A, A, A> {
311 using Result = A;
312 using Operand = A;
313 using Base = Operation<Multiply, A, A, A>;
314 using Base::Base;
315};
316
317template <typename A> struct Divide : public Operation<Divide<A>, A, A, A> {
318 using Result = A;
319 using Operand = A;
320 using Base = Operation<Divide, A, A, A>;
321 using Base::Base;
322};
323
324template <typename A> struct Power : public Operation<Power<A>, A, A, A> {
325 using Result = A;
326 using Operand = A;
327 using Base = Operation<Power, A, A, A>;
328 using Base::Base;
329};
330
331template <typename A>
332struct RealToIntPower : public Operation<RealToIntPower<A>, A, A, SomeInteger> {
333 using Base = Operation<RealToIntPower, A, A, SomeInteger>;
334 using Result = A;
335 using BaseOperand = A;
336 using ExponentOperand = SomeInteger;
337 using Base::Base;
338};
339
340template <typename A> struct Extremum : public Operation<Extremum<A>, A, A, A> {
341 using Result = A;
342 using Operand = A;
343 using Base = Operation<Extremum, A, A, A>;
344 CLASS_BOILERPLATE(Extremum)
345 Extremum(Ordering ord, const Expr<Operand> &x, const Expr<Operand> &y)
346 : Base{x, y}, ordering{ord} {}
347 Extremum(Ordering ord, Expr<Operand> &&x, Expr<Operand> &&y)
348 : Base{std::move(x), std::move(y)}, ordering{ord} {}
349 bool operator==(const Extremum &) const;
350 Ordering ordering{Ordering::Greater};
351};
352
353template <int KIND>
355 : public Operation<ComplexConstructor<KIND>,
356 Type<TypeCategory::Complex, KIND>, Type<TypeCategory::Real, KIND>,
357 Type<TypeCategory::Real, KIND>> {
359 using Operand = Type<TypeCategory::Real, KIND>;
360 using Base = Operation<ComplexConstructor, Result, Operand, Operand>;
361 using Base::Base;
362};
363
364template <int KIND>
365struct Concat
366 : public Operation<Concat<KIND>, Type<TypeCategory::Character, KIND>,
367 Type<TypeCategory::Character, KIND>,
368 Type<TypeCategory::Character, KIND>> {
370 using Operand = Result;
371 using Base = Operation<Concat, Result, Operand, Operand>;
372 using Base::Base;
373};
374
375template <int KIND>
376struct LogicalOperation
377 : public Operation<LogicalOperation<KIND>,
378 Type<TypeCategory::Logical, KIND>, Type<TypeCategory::Logical, KIND>,
379 Type<TypeCategory::Logical, KIND>> {
381 using Operand = Result;
382 using Base = Operation<LogicalOperation, Result, Operand, Operand>;
383 CLASS_BOILERPLATE(LogicalOperation)
384 LogicalOperation(
385 LogicalOperator opr, const Expr<Operand> &x, const Expr<Operand> &y)
386 : Base{x, y}, logicalOperator{opr} {}
387 LogicalOperation(LogicalOperator opr, Expr<Operand> &&x, Expr<Operand> &&y)
388 : Base{std::move(x), std::move(y)}, logicalOperator{opr} {}
389 bool operator==(const LogicalOperation &) const;
390 LogicalOperator logicalOperator;
391};
392
393// Fortran 2023 conditional expression: (cond ? val : cond ? val : ... : else)
394// All branches have the same type and rank (verified during semantic analysis).
395template <typename T> class ConditionalExpr {
396public:
397 using Result = T;
398 CLASS_BOILERPLATE(ConditionalExpr)
399 ConditionalExpr(Expr<LogicalResult> &&cond, Expr<Result> &&thenVal,
400 Expr<Result> &&elseVal)
401 : condition_{std::move(cond)}, thenValue_{std::move(thenVal)},
402 elseValue_{std::move(elseVal)} {}
403 bool operator==(const ConditionalExpr &) const;
404 Expr<LogicalResult> &condition() { return condition_.value(); }
405 const Expr<LogicalResult> &condition() const { return condition_.value(); }
406 Expr<Result> &thenValue() { return thenValue_.value(); }
407 const Expr<Result> &thenValue() const { return thenValue_.value(); }
408 Expr<Result> &elseValue() { return elseValue_.value(); }
409 const Expr<Result> &elseValue() const { return elseValue_.value(); }
410 int Rank() const { return thenValue().Rank(); }
411 std::optional<DynamicType> GetType() const { return thenValue().GetType(); }
412 static constexpr int Corank() { return 0; }
413 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
414
415private:
416 common::CopyableIndirection<Expr<LogicalResult>> condition_;
417 common::CopyableIndirection<Expr<Result>> thenValue_;
418 common::CopyableIndirection<Expr<Result>> elseValue_;
419};
420
421// Array constructors
422template <typename RESULT> class ArrayConstructorValues;
423
425 using Result = SubscriptInteger;
426 bool operator==(const ImpliedDoIndex &) const;
427 static constexpr int Rank() { return 0; }
428 static constexpr int Corank() { return 0; }
429 parser::CharBlock name; // nested implied DOs must use distinct names
430};
431
432template <typename RESULT> class ImpliedDo {
433public:
434 using Result = RESULT;
435 using Index = ResultType<ImpliedDoIndex>;
436 ImpliedDo(parser::CharBlock name, Expr<Index> &&lower, Expr<Index> &&upper,
438 : name_{name}, lower_{std::move(lower)}, upper_{std::move(upper)},
439 stride_{std::move(stride)}, values_{std::move(values)} {}
440 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ImpliedDo)
441 bool operator==(const ImpliedDo &) const;
442 parser::CharBlock name() const { return name_; }
443 Expr<Index> &lower() { return lower_.value(); }
444 const Expr<Index> &lower() const { return lower_.value(); }
445 Expr<Index> &upper() { return upper_.value(); }
446 const Expr<Index> &upper() const { return upper_.value(); }
447 Expr<Index> &stride() { return stride_.value(); }
448 const Expr<Index> &stride() const { return stride_.value(); }
449 ArrayConstructorValues<Result> &values() { return values_.value(); }
450 const ArrayConstructorValues<Result> &values() const {
451 return values_.value();
452 }
453
454private:
455 parser::CharBlock name_;
456 common::CopyableIndirection<Expr<Index>> lower_, upper_, stride_;
457 common::CopyableIndirection<ArrayConstructorValues<Result>> values_;
458};
459
460template <typename RESULT> struct ArrayConstructorValue {
461 using Result = RESULT;
462 EVALUATE_UNION_CLASS_BOILERPLATE(ArrayConstructorValue)
463 std::variant<Expr<Result>, ImpliedDo<Result>> u;
464};
465
466template <typename RESULT> class ArrayConstructorValues {
467public:
468 using Result = RESULT;
469 using Values = std::vector<ArrayConstructorValue<Result>>;
470 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ArrayConstructorValues)
471 ArrayConstructorValues() {}
472
473 bool operator==(const ArrayConstructorValues &) const;
474 static constexpr int Rank() { return 1; }
475 static constexpr int Corank() { return 0; }
476 template <typename A> common::NoLvalue<A> Push(A &&x) {
477 values_.emplace_back(std::move(x));
478 }
479
480 typename Values::iterator begin() { return values_.begin(); }
481 typename Values::const_iterator begin() const { return values_.begin(); }
482 typename Values::iterator end() { return values_.end(); }
483 typename Values::const_iterator end() const { return values_.end(); }
484
485protected:
486 Values values_;
487};
488
489// Note that there are specializations of ArrayConstructor for character
490// and derived types, since they must carry additional type information,
491// but that an empty ArrayConstructor can be constructed for any type
492// given an expression from which such type information may be gleaned.
493template <typename RESULT>
494class ArrayConstructor : public ArrayConstructorValues<RESULT> {
495public:
496 using Result = RESULT;
497 using Base = ArrayConstructorValues<Result>;
498 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ArrayConstructor)
499 explicit ArrayConstructor(Base &&values) : Base{std::move(values)} {}
500 template <typename T> explicit ArrayConstructor(const Expr<T> &) {}
501 static constexpr Result result() { return Result{}; }
502 static constexpr DynamicType GetType() { return Result::GetType(); }
503 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
504};
505
506template <int KIND>
507class ArrayConstructor<Type<TypeCategory::Character, KIND>>
508 : public ArrayConstructorValues<Type<TypeCategory::Character, KIND>> {
509public:
511 using Base = ArrayConstructorValues<Result>;
512 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ArrayConstructor)
513 explicit ArrayConstructor(Base &&values) : Base{std::move(values)} {}
514 template <typename T> explicit ArrayConstructor(const Expr<T> &) {}
515 ArrayConstructor &set_LEN(Expr<SubscriptInteger> &&);
516 bool operator==(const ArrayConstructor &) const;
517 static constexpr Result result() { return Result{}; }
518 static constexpr DynamicType GetType() { return Result::GetType(); }
519 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
520 const Expr<SubscriptInteger> *LEN() const {
521 return length_ ? &length_->value() : nullptr;
522 }
523
524private:
525 std::optional<common::CopyableIndirection<Expr<SubscriptInteger>>> length_;
526};
527
528template <>
529class ArrayConstructor<SomeDerived>
530 : public ArrayConstructorValues<SomeDerived> {
531public:
532 using Result = SomeDerived;
533 using Base = ArrayConstructorValues<Result>;
534 CLASS_BOILERPLATE(ArrayConstructor)
535
536 ArrayConstructor(const semantics::DerivedTypeSpec &spec, Base &&v)
537 : Base{std::move(v)}, result_{spec} {}
538 template <typename A>
539 explicit ArrayConstructor(const A &prototype)
540 : result_{prototype.GetType().value().GetDerivedTypeSpec()} {}
541
542 bool operator==(const ArrayConstructor &) const;
543 constexpr Result result() const { return result_; }
544 constexpr DynamicType GetType() const { return result_.GetType(); }
545 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
546
547private:
548 Result result_;
549};
550
551// Expression representations for each type category.
552
553template <int KIND>
554class Expr<Type<TypeCategory::Integer, KIND>>
555 : public ExpressionBase<Type<TypeCategory::Integer, KIND>> {
556public:
558
559 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
560
561private:
562 using Conversions = std::tuple<Convert<Result, TypeCategory::Integer>,
565 using Operations = std::tuple<Parentheses<Result>, Negate<Result>,
568 using Indices = std::conditional_t<KIND == ImpliedDoIndex::Result::kind,
569 std::tuple<ImpliedDoIndex>, std::tuple<>>;
570 using TypeParamInquiries =
571 std::conditional_t<KIND == TypeParamInquiry::Result::kind,
572 std::tuple<TypeParamInquiry>, std::tuple<>>;
573 using DescriptorInquiries =
574 std::conditional_t<KIND == DescriptorInquiry::Result::kind,
575 std::tuple<DescriptorInquiry>, std::tuple<>>;
576 using Others = std::tuple<Constant<Result>, ArrayConstructor<Result>,
578
579public:
580 common::TupleToVariant<common::CombineTuples<Operations, Conversions, Indices,
581 TypeParamInquiries, DescriptorInquiries, Others>>
582 u;
583};
584
585template <int KIND>
586class Expr<Type<TypeCategory::Unsigned, KIND>>
587 : public ExpressionBase<Type<TypeCategory::Unsigned, KIND>> {
588public:
590
591 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
592
593private:
594 using Conversions = std::tuple<Convert<Result, TypeCategory::Integer>,
597 using Operations = std::tuple<Parentheses<Result>, Negate<Result>,
600 using Others = std::tuple<Constant<Result>, ArrayConstructor<Result>,
602
603public:
604 common::TupleToVariant<common::CombineTuples<Operations, Conversions, Others>>
605 u;
606};
607
608template <int KIND>
609class Expr<Type<TypeCategory::Real, KIND>>
610 : public ExpressionBase<Type<TypeCategory::Real, KIND>> {
611public:
612 using Result = Type<TypeCategory::Real, KIND>;
613
614 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
615 explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}
616
617private:
618 // N.B. Real->Complex and Complex->Real conversions are done with CMPLX
619 // and part access operations (resp.).
620 using Conversions = std::variant<Convert<Result, TypeCategory::Integer>,
623 using Operations = std::variant<ComplexComponent<KIND>, Parentheses<Result>,
627 using Others = std::variant<Constant<Result>, ArrayConstructor<Result>,
629
630public:
631 common::CombineVariants<Operations, Conversions, Others> u;
632};
633
634template <int KIND>
635class Expr<Type<TypeCategory::Complex, KIND>>
636 : public ExpressionBase<Type<TypeCategory::Complex, KIND>> {
637public:
639 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
640 explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}
641 using Operations = std::variant<Parentheses<Result>, Negate<Result>,
645 using Others = std::variant<Constant<Result>, ArrayConstructor<Result>,
647
648public:
649 common::CombineVariants<Operations, Others> u;
650};
651
652FOR_EACH_INTEGER_KIND(extern template class Expr, )
653FOR_EACH_UNSIGNED_KIND(extern template class Expr, )
654FOR_EACH_REAL_KIND(extern template class Expr, )
655FOR_EACH_COMPLEX_KIND(extern template class Expr, )
656
657template <int KIND>
658class Expr<Type<TypeCategory::Character, KIND>>
659 : public ExpressionBase<Type<TypeCategory::Character, KIND>> {
660public:
662 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
663 explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}
664 explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} {}
665
666 std::optional<Expr<SubscriptInteger>> LEN() const;
667
668 std::variant<Constant<Result>, ArrayConstructor<Result>, Designator<Result>,
671 u;
672};
673
674FOR_EACH_CHARACTER_KIND(extern template class Expr, )
675
676// The Relational class template is a helper for constructing logical
677// expressions with polymorphism over the cross product of the possible
678// categories and kinds of comparable operands.
679// Fortran defines a numeric relation with distinct types or kinds as
680// first undergoing the same operand conversions that occur with the intrinsic
681// addition operator. Character relations must have the same kind.
682// There are no relations between LOGICAL values.
683
684template <typename T>
685class Relational : public Operation<Relational<T>, LogicalResult, T, T> {
686public:
687 using Result = LogicalResult;
688 using Base = Operation<Relational, LogicalResult, T, T>;
689 using Operand = typename Base::template Operand<0>;
690 static_assert(Operand::category == TypeCategory::Integer ||
691 Operand::category == TypeCategory::Real ||
692 Operand::category == TypeCategory::Complex ||
693 Operand::category == TypeCategory::Character ||
694 Operand::category == TypeCategory::Unsigned);
695 CLASS_BOILERPLATE(Relational)
696 Relational(
697 RelationalOperator r, const Expr<Operand> &a, const Expr<Operand> &b)
698 : Base{a, b}, opr{r} {}
699 Relational(RelationalOperator r, Expr<Operand> &&a, Expr<Operand> &&b)
700 : Base{std::move(a), std::move(b)}, opr{r} {}
701 bool operator==(const Relational &) const;
702 RelationalOperator opr;
703};
704
705template <> class Relational<SomeType> {
706 using DirectlyComparableTypes = common::CombineTuples<IntegerTypes, RealTypes,
707 ComplexTypes, CharacterTypes, UnsignedTypes>;
708
709public:
710 using Result = LogicalResult;
711 EVALUATE_UNION_CLASS_BOILERPLATE(Relational)
712 static constexpr DynamicType GetType() { return Result::GetType(); }
713 int Rank() const {
714 return common::visit([](const auto &x) { return x.Rank(); }, u);
715 }
716 static constexpr int Corank() { return 0; }
717 llvm::raw_ostream &AsFortran(llvm::raw_ostream &o) const;
718 common::MapTemplate<Relational, DirectlyComparableTypes> u;
719};
720
721FOR_EACH_INTEGER_KIND(extern template class Relational, )
722FOR_EACH_UNSIGNED_KIND(extern template class Relational, )
723FOR_EACH_REAL_KIND(extern template class Relational, )
724FOR_EACH_CHARACTER_KIND(extern template class Relational, )
725extern template class Relational<SomeType>;
726
727// Logical expressions of a kind bigger than LogicalResult
728// do not include Relational<> operations as possibilities,
729// since the results of Relationals are always LogicalResult
730// (kind=4).
731template <int KIND>
732class Expr<Type<TypeCategory::Logical, KIND>>
733 : public ExpressionBase<Type<TypeCategory::Logical, KIND>> {
734public:
736 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
737 explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}
738 explicit Expr(bool x) : u{Constant<Result>{x}} {}
739
740private:
741 using Operations = std::tuple<Convert<Result>, Parentheses<Result>, Not<KIND>,
743 using Relations = std::conditional_t<KIND == LogicalResult::kind,
744 std::tuple<Relational<SomeType>>, std::tuple<>>;
745 using Others = std::tuple<Constant<Result>, ArrayConstructor<Result>,
747
748public:
749 common::TupleToVariant<common::CombineTuples<Operations, Relations, Others>>
750 u;
751};
752
753FOR_EACH_LOGICAL_KIND(extern template class Expr, )
754
755// StructureConstructor pairs a StructureConstructorValues instance
756// (a map associating symbols with expressions) with a derived type
757// specification. There are two other similar classes:
758// - ArrayConstructor<SomeDerived> comprises a derived type spec &
759// zero or more instances of Expr<SomeDerived>; it has rank 1
760// but not (in the most general case) a known shape.
761// - Constant<SomeDerived> comprises a derived type spec, zero or more
762// homogeneous instances of StructureConstructorValues whose type
763// parameters and component expressions are all constant, and a
764// known shape (possibly scalar).
765// StructureConstructor represents a scalar value of derived type that
766// is not necessarily a constant. It is used only as an Expr<SomeDerived>
767// alternative and as the type Scalar<SomeDerived> (with an assumption
768// of constant component value expressions).
769class StructureConstructor {
770public:
771 using Result = SomeDerived;
772
773 explicit StructureConstructor(const semantics::DerivedTypeSpec &spec)
774 : result_{spec} {}
775 StructureConstructor(
776 const semantics::DerivedTypeSpec &, const StructureConstructorValues &);
777 StructureConstructor(
778 const semantics::DerivedTypeSpec &, StructureConstructorValues &&);
779 CLASS_BOILERPLATE(StructureConstructor)
780
781 constexpr Result result() const { return result_; }
782 const semantics::DerivedTypeSpec &derivedTypeSpec() const {
783 return result_.derivedTypeSpec();
784 }
785 StructureConstructorValues &values() { return values_; }
786 const StructureConstructorValues &values() const { return values_; }
787
788 bool operator==(const StructureConstructor &) const;
789
790 StructureConstructorValues::iterator begin() { return values_.begin(); }
791 StructureConstructorValues::const_iterator begin() const {
792 return values_.begin();
793 }
794 StructureConstructorValues::iterator end() { return values_.end(); }
795 StructureConstructorValues::const_iterator end() const {
796 return values_.end();
797 }
798
799 // can return nullopt
800 std::optional<Expr<SomeType>> Find(const Symbol &) const;
801
802 StructureConstructor &Add(const semantics::Symbol &, Expr<SomeType> &&);
803 static constexpr int Rank() { return 0; }
804 static constexpr int Corank() { return 0; }
805 DynamicType GetType() const;
806 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
807
808private:
809 std::optional<Expr<SomeType>> CreateParentComponent(const Symbol &) const;
810 Result result_;
811 StructureConstructorValues values_;
812};
813
814// An expression whose result has a derived type.
815template <> class Expr<SomeDerived> : public ExpressionBase<SomeDerived> {
816public:
817 using Result = SomeDerived;
818 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
819 std::variant<Constant<Result>, ArrayConstructor<Result>, StructureConstructor,
822 u;
823};
824
825// A polymorphic expression of known intrinsic type category, but dynamic
826// kind, represented as a discriminated union over Expr<Type<CAT, K>>
827// for each supported kind K in the category.
828template <TypeCategory CAT>
829class Expr<SomeKind<CAT>> : public ExpressionBase<SomeKind<CAT>> {
830public:
831 using Result = SomeKind<CAT>;
832 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
833 int GetKind() const;
834 common::MapTemplate<evaluate::Expr, CategoryTypes<CAT>> u;
835};
836
837template <> class Expr<SomeCharacter> : public ExpressionBase<SomeCharacter> {
838public:
839 using Result = SomeCharacter;
840 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
841 int GetKind() const;
842 std::optional<Expr<SubscriptInteger>> LEN() const;
843 common::MapTemplate<Expr, CategoryTypes<TypeCategory::Character>> u;
844};
845
846// A variant comprising the Expr<> instantiations over SomeDerived and
847// SomeKind<CATEGORY>.
848using CategoryExpression = common::MapTemplate<Expr, SomeCategory>;
849
850// BOZ literal "typeless" constants must be wide enough to hold a numeric
851// value of any supported kind of INTEGER or REAL. They must also be
852// distinguishable from other integer constants, since they are permitted
853// to be used in only a few situations.
854using BOZLiteralConstant = typename LargestReal::Scalar::Word;
855
856// Null pointers without MOLD= arguments are typed by context.
858 constexpr bool operator==(const NullPointer &) const { return true; }
859 static constexpr int Rank() { return 0; }
860 static constexpr int Corank() { return 0; }
861};
862
863// Procedure pointer targets are treated as if they were typeless.
864// They are either procedure designators or values returned from
865// references to functions that return procedure (not object) pointers.
866using TypelessExpression = std::variant<BOZLiteralConstant, NullPointer,
868
869// A completely generic expression, polymorphic across all of the intrinsic type
870// categories and each of their kinds.
871template <> class Expr<SomeType> : public ExpressionBase<SomeType> {
872public:
873 using Result = SomeType;
874 EVALUATE_UNION_CLASS_BOILERPLATE(Expr)
875
876 // Owning references to these generic expressions can appear in other
877 // compiler data structures (viz., the parse tree and symbol table), so
878 // its destructor is externalized to reduce redundant default instances.
879 ~Expr();
880
881 template <TypeCategory CAT, int KIND>
882 explicit Expr(const Expr<Type<CAT, KIND>> &x) : u{Expr<SomeKind<CAT>>{x}} {}
883
884 template <TypeCategory CAT, int KIND>
885 explicit Expr(Expr<Type<CAT, KIND>> &&x)
886 : u{Expr<SomeKind<CAT>>{std::move(x)}} {}
887
888 template <TypeCategory CAT, int KIND>
889 Expr &operator=(const Expr<Type<CAT, KIND>> &x) {
890 u = Expr<SomeKind<CAT>>{x};
891 return *this;
892 }
893
894 template <TypeCategory CAT, int KIND>
895 Expr &operator=(Expr<Type<CAT, KIND>> &&x) {
896 u = Expr<SomeKind<CAT>>{std::move(x)};
897 return *this;
898 }
899
900public:
901 common::CombineVariants<TypelessExpression, CategoryExpression> u;
902};
903
904// An assignment is either intrinsic, user-defined (with a ProcedureRef to
905// specify the procedure to call), or pointer assignment (with possibly empty
906// BoundsSpec or non-empty BoundsRemapping). In all cases there are Exprs
907// representing the LHS and RHS of the assignment.
908class Assignment {
909public:
910 Assignment(Expr<SomeType> &&lhs, Expr<SomeType> &&rhs)
911 : lhs(std::move(lhs)), rhs(std::move(rhs)) {}
912
913 struct Intrinsic {};
914 using BoundsSpec = std::vector<Expr<SubscriptInteger>>;
915 using BoundsRemapping =
916 std::vector<std::pair<Expr<SubscriptInteger>, Expr<SubscriptInteger>>>;
917 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
918
919 Expr<SomeType> lhs;
920 Expr<SomeType> rhs;
921 std::variant<Intrinsic, ProcedureRef, BoundsSpec, BoundsRemapping> u;
922};
923
924// This wrapper class is used, by means of a forward reference with
925// an owning pointer, to cache analyzed expressions in parse tree nodes.
926struct GenericExprWrapper {
927 GenericExprWrapper() {}
928 explicit GenericExprWrapper(std::optional<Expr<SomeType>> &&x)
929 : v{std::move(x)} {}
930 ~GenericExprWrapper();
931 static void Deleter(GenericExprWrapper *);
932 std::optional<Expr<SomeType>> v; // vacant if error
933};
934
935// Like GenericExprWrapper but for analyzed assignments
936struct GenericAssignmentWrapper {
937 GenericAssignmentWrapper() {}
938 explicit GenericAssignmentWrapper(Assignment &&x) : v{std::move(x)} {}
939 explicit GenericAssignmentWrapper(std::optional<Assignment> &&x)
940 : v{std::move(x)} {}
941 ~GenericAssignmentWrapper();
942 static void Deleter(GenericAssignmentWrapper *);
943 std::optional<Assignment> v; // vacant if error
944};
945
946FOR_EACH_CATEGORY_TYPE(extern template class Expr, )
947FOR_EACH_TYPE_AND_KIND(extern template class ExpressionBase, )
948FOR_EACH_INTRINSIC_KIND(extern template class ArrayConstructorValues, )
949FOR_EACH_INTRINSIC_KIND(extern template class ArrayConstructor, )
950
951// Template instantiations to resolve these "extern template" declarations.
952#define INSTANTIATE_EXPRESSION_TEMPLATES \
953 FOR_EACH_INTRINSIC_KIND(template class Expr, ) \
954 FOR_EACH_CATEGORY_TYPE(template class Expr, ) \
955 FOR_EACH_INTEGER_KIND(template class Relational, ) \
956 FOR_EACH_UNSIGNED_KIND(template class Relational, ) \
957 FOR_EACH_REAL_KIND(template class Relational, ) \
958 FOR_EACH_CHARACTER_KIND(template class Relational, ) \
959 template class Relational<SomeType>; \
960 FOR_EACH_TYPE_AND_KIND(template class ExpressionBase, ) \
961 FOR_EACH_INTRINSIC_KIND(template class ArrayConstructorValues, ) \
962 FOR_EACH_INTRINSIC_KIND(template class ArrayConstructor, ) \
963 FOR_EACH_INTRINSIC_KIND(template class ConditionalExpr, )
964} // namespace Fortran::evaluate
965#endif // FORTRAN_EVALUATE_EXPRESSION_H_
Definition expression.h:494
Definition expression.h:908
Definition expression.h:395
Definition constant.h:147
Definition variable.h:381
Definition type.h:74
Definition common.h:215
Definition expression.h:66
Definition common.h:217
Definition call.h:293
Definition expression.h:432
Definition call.h:233
Definition expression.h:685
Definition expression.h:769
Definition type.h:57
Definition char-block.h:28
Definition symbol.h:832
Definition call.h:34
Definition expression.h:296
Definition expression.h:460
Definition expression.h:913
Definition expression.h:357
Definition expression.h:368
Definition expression.h:211
Definition expression.h:317
Definition expression.h:340
Definition expression.h:424
Definition expression.h:379
Definition expression.h:310
Definition expression.h:247
Definition expression.h:272
Definition expression.h:857
Definition expression.h:229
Definition expression.h:324
Definition expression.h:332
Definition expression.h:286
Definition type.h:399
Definition type.h:417
Definition expression.h:303