1 //===-- lib/Evaluate/tools.cpp --------------------------------------------===//
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 #include "flang/Evaluate/tools.h"
10 #include "flang/Common/idioms.h"
11 #include "flang/Evaluate/characteristics.h"
12 #include "flang/Evaluate/traverse.h"
13 #include "flang/Parser/message.h"
14 #include "flang/Semantics/tools.h"
15 #include <algorithm>
16 #include <variant>
17 
18 using namespace Fortran::parser::literals;
19 
20 namespace Fortran::evaluate {
21 
22 // Can x*(a,b) be represented as (x*a,x*b)?  This code duplication
23 // of the subexpression "x" cannot (yet?) be reliably undone by
24 // common subexpression elimination in lowering, so it's disabled
25 // here for now to avoid the risk of potential duplication of
26 // expensive subexpressions (e.g., large array expressions, references
27 // to expensive functions) in generate code.
28 static constexpr bool allowOperandDuplication{false};
29 
30 std::optional<Expr<SomeType>> AsGenericExpr(DataRef &&ref) {
31   const Symbol &symbol{ref.GetLastSymbol()};
32   if (auto dyType{DynamicType::From(symbol)}) {
33     return TypedWrapper<Designator, DataRef>(*dyType, std::move(ref));
34   }
35   return std::nullopt;
36 }
37 
38 std::optional<Expr<SomeType>> AsGenericExpr(const Symbol &symbol) {
39   return AsGenericExpr(DataRef{symbol});
40 }
41 
42 Expr<SomeType> Parenthesize(Expr<SomeType> &&expr) {
43   return std::visit(
44       [&](auto &&x) {
45         using T = std::decay_t<decltype(x)>;
46         if constexpr (common::HasMember<T, TypelessExpression>) {
47           return expr; // no parentheses around typeless
48         } else if constexpr (std::is_same_v<T, Expr<SomeDerived>>) {
49           return AsGenericExpr(Parentheses<SomeDerived>{std::move(x)});
50         } else {
51           return std::visit(
52               [](auto &&y) {
53                 using T = ResultType<decltype(y)>;
54                 return AsGenericExpr(Parentheses<T>{std::move(y)});
55               },
56               std::move(x.u));
57         }
58       },
59       std::move(expr.u));
60 }
61 
62 std::optional<DataRef> ExtractDataRef(
63     const ActualArgument &arg, bool intoSubstring) {
64   if (const Expr<SomeType> *expr{arg.UnwrapExpr()}) {
65     return ExtractDataRef(*expr, intoSubstring);
66   } else {
67     return std::nullopt;
68   }
69 }
70 
71 std::optional<DataRef> ExtractSubstringBase(const Substring &substring) {
72   return std::visit(
73       common::visitors{
74           [&](const DataRef &x) -> std::optional<DataRef> { return x; },
75           [&](const StaticDataObject::Pointer &) -> std::optional<DataRef> {
76             return std::nullopt;
77           },
78       },
79       substring.parent());
80 }
81 
82 // IsVariable()
83 
84 auto IsVariableHelper::operator()(const Symbol &symbol) const -> Result {
85   const Symbol &root{GetAssociationRoot(symbol)};
86   return !IsNamedConstant(root) && root.has<semantics::ObjectEntityDetails>();
87 }
88 auto IsVariableHelper::operator()(const Component &x) const -> Result {
89   const Symbol &comp{x.GetLastSymbol()};
90   return (*this)(comp) && (IsPointer(comp) || (*this)(x.base()));
91 }
92 auto IsVariableHelper::operator()(const ArrayRef &x) const -> Result {
93   return (*this)(x.base());
94 }
95 auto IsVariableHelper::operator()(const Substring &x) const -> Result {
96   return (*this)(x.GetBaseObject());
97 }
98 auto IsVariableHelper::operator()(const ProcedureDesignator &x) const
99     -> Result {
100   if (const Symbol * symbol{x.GetSymbol()}) {
101     const Symbol *result{FindFunctionResult(*symbol)};
102     return result && IsPointer(*result) && !IsProcedurePointer(*result);
103   }
104   return false;
105 }
106 
107 // Conversions of COMPLEX component expressions to REAL.
108 ConvertRealOperandsResult ConvertRealOperands(
109     parser::ContextualMessages &messages, Expr<SomeType> &&x,
110     Expr<SomeType> &&y, int defaultRealKind) {
111   return std::visit(
112       common::visitors{
113           [&](Expr<SomeInteger> &&ix,
114               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
115             // Can happen in a CMPLX() constructor.  Per F'2018,
116             // both integer operands are converted to default REAL.
117             return {AsSameKindExprs<TypeCategory::Real>(
118                 ConvertToKind<TypeCategory::Real>(
119                     defaultRealKind, std::move(ix)),
120                 ConvertToKind<TypeCategory::Real>(
121                     defaultRealKind, std::move(iy)))};
122           },
123           [&](Expr<SomeInteger> &&ix,
124               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
125             return {AsSameKindExprs<TypeCategory::Real>(
126                 ConvertTo(ry, std::move(ix)), std::move(ry))};
127           },
128           [&](Expr<SomeReal> &&rx,
129               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
130             return {AsSameKindExprs<TypeCategory::Real>(
131                 std::move(rx), ConvertTo(rx, std::move(iy)))};
132           },
133           [&](Expr<SomeReal> &&rx,
134               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
135             return {AsSameKindExprs<TypeCategory::Real>(
136                 std::move(rx), std::move(ry))};
137           },
138           [&](Expr<SomeInteger> &&ix,
139               BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
140             return {AsSameKindExprs<TypeCategory::Real>(
141                 ConvertToKind<TypeCategory::Real>(
142                     defaultRealKind, std::move(ix)),
143                 ConvertToKind<TypeCategory::Real>(
144                     defaultRealKind, std::move(by)))};
145           },
146           [&](BOZLiteralConstant &&bx,
147               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
148             return {AsSameKindExprs<TypeCategory::Real>(
149                 ConvertToKind<TypeCategory::Real>(
150                     defaultRealKind, std::move(bx)),
151                 ConvertToKind<TypeCategory::Real>(
152                     defaultRealKind, std::move(iy)))};
153           },
154           [&](Expr<SomeReal> &&rx,
155               BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
156             return {AsSameKindExprs<TypeCategory::Real>(
157                 std::move(rx), ConvertTo(rx, std::move(by)))};
158           },
159           [&](BOZLiteralConstant &&bx,
160               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
161             return {AsSameKindExprs<TypeCategory::Real>(
162                 ConvertTo(ry, std::move(bx)), std::move(ry))};
163           },
164           [&](auto &&, auto &&) -> ConvertRealOperandsResult { // C718
165             messages.Say("operands must be INTEGER or REAL"_err_en_US);
166             return std::nullopt;
167           },
168       },
169       std::move(x.u), std::move(y.u));
170 }
171 
172 // Helpers for NumericOperation and its subroutines below.
173 static std::optional<Expr<SomeType>> NoExpr() { return std::nullopt; }
174 
175 template <TypeCategory CAT>
176 std::optional<Expr<SomeType>> Package(Expr<SomeKind<CAT>> &&catExpr) {
177   return {AsGenericExpr(std::move(catExpr))};
178 }
179 template <TypeCategory CAT>
180 std::optional<Expr<SomeType>> Package(
181     std::optional<Expr<SomeKind<CAT>>> &&catExpr) {
182   if (catExpr) {
183     return {AsGenericExpr(std::move(*catExpr))};
184   }
185   return NoExpr();
186 }
187 
188 // Mixed REAL+INTEGER operations.  REAL**INTEGER is a special case that
189 // does not require conversion of the exponent expression.
190 template <template <typename> class OPR>
191 std::optional<Expr<SomeType>> MixedRealLeft(
192     Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
193   return Package(std::visit(
194       [&](auto &&rxk) -> Expr<SomeReal> {
195         using resultType = ResultType<decltype(rxk)>;
196         if constexpr (std::is_same_v<OPR<resultType>, Power<resultType>>) {
197           return AsCategoryExpr(
198               RealToIntPower<resultType>{std::move(rxk), std::move(iy)});
199         }
200         // G++ 8.1.0 emits bogus warnings about missing return statements if
201         // this statement is wrapped in an "else", as it should be.
202         return AsCategoryExpr(OPR<resultType>{
203             std::move(rxk), ConvertToType<resultType>(std::move(iy))});
204       },
205       std::move(rx.u)));
206 }
207 
208 std::optional<Expr<SomeComplex>> ConstructComplex(
209     parser::ContextualMessages &messages, Expr<SomeType> &&real,
210     Expr<SomeType> &&imaginary, int defaultRealKind) {
211   if (auto converted{ConvertRealOperands(
212           messages, std::move(real), std::move(imaginary), defaultRealKind)}) {
213     return {std::visit(
214         [](auto &&pair) {
215           return MakeComplex(std::move(pair[0]), std::move(pair[1]));
216         },
217         std::move(*converted))};
218   }
219   return std::nullopt;
220 }
221 
222 std::optional<Expr<SomeComplex>> ConstructComplex(
223     parser::ContextualMessages &messages, std::optional<Expr<SomeType>> &&real,
224     std::optional<Expr<SomeType>> &&imaginary, int defaultRealKind) {
225   if (auto parts{common::AllPresent(std::move(real), std::move(imaginary))}) {
226     return ConstructComplex(messages, std::get<0>(std::move(*parts)),
227         std::get<1>(std::move(*parts)), defaultRealKind);
228   }
229   return std::nullopt;
230 }
231 
232 Expr<SomeReal> GetComplexPart(const Expr<SomeComplex> &z, bool isImaginary) {
233   return std::visit(
234       [&](const auto &zk) {
235         static constexpr int kind{ResultType<decltype(zk)>::kind};
236         return AsCategoryExpr(ComplexComponent<kind>{isImaginary, zk});
237       },
238       z.u);
239 }
240 
241 // Convert REAL to COMPLEX of the same kind. Preserving the real operand kind
242 // and then applying complex operand promotion rules allows the result to have
243 // the highest precision of REAL and COMPLEX operands as required by Fortran
244 // 2018 10.9.1.3.
245 Expr<SomeComplex> PromoteRealToComplex(Expr<SomeReal> &&someX) {
246   return std::visit(
247       [](auto &&x) {
248         using RT = ResultType<decltype(x)>;
249         return AsCategoryExpr(ComplexConstructor<RT::kind>{
250             std::move(x), AsExpr(Constant<RT>{Scalar<RT>{}})});
251       },
252       std::move(someX.u));
253 }
254 
255 // Handle mixed COMPLEX+REAL (or INTEGER) operations in a better way
256 // than just converting the second operand to COMPLEX and performing the
257 // corresponding COMPLEX+COMPLEX operation.
258 template <template <typename> class OPR, TypeCategory RCAT>
259 std::optional<Expr<SomeType>> MixedComplexLeft(
260     parser::ContextualMessages &messages, Expr<SomeComplex> &&zx,
261     Expr<SomeKind<RCAT>> &&iry, [[maybe_unused]] int defaultRealKind) {
262   Expr<SomeReal> zr{GetComplexPart(zx, false)};
263   Expr<SomeReal> zi{GetComplexPart(zx, true)};
264   if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>> ||
265       std::is_same_v<OPR<LargestReal>, Subtract<LargestReal>>) {
266     // (a,b) + x -> (a+x, b)
267     // (a,b) - x -> (a-x, b)
268     if (std::optional<Expr<SomeType>> rr{
269             NumericOperation<OPR>(messages, AsGenericExpr(std::move(zr)),
270                 AsGenericExpr(std::move(iry)), defaultRealKind)}) {
271       return Package(ConstructComplex(messages, std::move(*rr),
272           AsGenericExpr(std::move(zi)), defaultRealKind));
273     }
274   } else if constexpr (allowOperandDuplication &&
275       (std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>> ||
276           std::is_same_v<OPR<LargestReal>, Divide<LargestReal>>)) {
277     // (a,b) * x -> (a*x, b*x)
278     // (a,b) / x -> (a/x, b/x)
279     auto copy{iry};
280     auto rr{NumericOperation<OPR>(messages, AsGenericExpr(std::move(zr)),
281         AsGenericExpr(std::move(iry)), defaultRealKind)};
282     auto ri{NumericOperation<OPR>(messages, AsGenericExpr(std::move(zi)),
283         AsGenericExpr(std::move(copy)), defaultRealKind)};
284     if (auto parts{common::AllPresent(std::move(rr), std::move(ri))}) {
285       return Package(ConstructComplex(messages, std::get<0>(std::move(*parts)),
286           std::get<1>(std::move(*parts)), defaultRealKind));
287     }
288   } else if constexpr (RCAT == TypeCategory::Integer &&
289       std::is_same_v<OPR<LargestReal>, Power<LargestReal>>) {
290     // COMPLEX**INTEGER is a special case that doesn't convert the exponent.
291     static_assert(RCAT == TypeCategory::Integer);
292     return Package(std::visit(
293         [&](auto &&zxk) {
294           using Ty = ResultType<decltype(zxk)>;
295           return AsCategoryExpr(
296               AsExpr(RealToIntPower<Ty>{std::move(zxk), std::move(iry)}));
297         },
298         std::move(zx.u)));
299   } else {
300     // (a,b) ** x -> (a,b) ** (x,0)
301     if constexpr (RCAT == TypeCategory::Integer) {
302       Expr<SomeComplex> zy{ConvertTo(zx, std::move(iry))};
303       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
304     } else {
305       Expr<SomeComplex> zy{PromoteRealToComplex(std::move(iry))};
306       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
307     }
308   }
309   return NoExpr();
310 }
311 
312 // Mixed COMPLEX operations with the COMPLEX operand on the right.
313 //  x + (a,b) -> (x+a, b)
314 //  x - (a,b) -> (x-a, -b)
315 //  x * (a,b) -> (x*a, x*b)
316 //  x / (a,b) -> (x,0) / (a,b)   (and **)
317 template <template <typename> class OPR, TypeCategory LCAT>
318 std::optional<Expr<SomeType>> MixedComplexRight(
319     parser::ContextualMessages &messages, Expr<SomeKind<LCAT>> &&irx,
320     Expr<SomeComplex> &&zy, [[maybe_unused]] int defaultRealKind) {
321   if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>>) {
322     // x + (a,b) -> (a,b) + x -> (a+x, b)
323     return MixedComplexLeft<OPR, LCAT>(
324         messages, std::move(zy), std::move(irx), defaultRealKind);
325   } else if constexpr (allowOperandDuplication &&
326       std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>>) {
327     // x * (a,b) -> (a,b) * x -> (a*x, b*x)
328     return MixedComplexLeft<OPR, LCAT>(
329         messages, std::move(zy), std::move(irx), defaultRealKind);
330   } else if constexpr (std::is_same_v<OPR<LargestReal>,
331                            Subtract<LargestReal>>) {
332     // x - (a,b) -> (x-a, -b)
333     Expr<SomeReal> zr{GetComplexPart(zy, false)};
334     Expr<SomeReal> zi{GetComplexPart(zy, true)};
335     if (std::optional<Expr<SomeType>> rr{
336             NumericOperation<Subtract>(messages, AsGenericExpr(std::move(irx)),
337                 AsGenericExpr(std::move(zr)), defaultRealKind)}) {
338       return Package(ConstructComplex(messages, std::move(*rr),
339           AsGenericExpr(-std::move(zi)), defaultRealKind));
340     }
341   } else {
342     // x / (a,b) -> (x,0) / (a,b)
343     if constexpr (LCAT == TypeCategory::Integer) {
344       Expr<SomeComplex> zx{ConvertTo(zy, std::move(irx))};
345       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
346     } else {
347       Expr<SomeComplex> zx{PromoteRealToComplex(std::move(irx))};
348       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
349     }
350   }
351   return NoExpr();
352 }
353 
354 // N.B. When a "typeless" BOZ literal constant appears as one (not both!) of
355 // the operands to a dyadic operation where one is permitted, it assumes the
356 // type and kind of the other operand.
357 template <template <typename> class OPR>
358 std::optional<Expr<SomeType>> NumericOperation(
359     parser::ContextualMessages &messages, Expr<SomeType> &&x,
360     Expr<SomeType> &&y, int defaultRealKind) {
361   return std::visit(
362       common::visitors{
363           [](Expr<SomeInteger> &&ix, Expr<SomeInteger> &&iy) {
364             return Package(PromoteAndCombine<OPR, TypeCategory::Integer>(
365                 std::move(ix), std::move(iy)));
366           },
367           [](Expr<SomeReal> &&rx, Expr<SomeReal> &&ry) {
368             return Package(PromoteAndCombine<OPR, TypeCategory::Real>(
369                 std::move(rx), std::move(ry)));
370           },
371           // Mixed REAL/INTEGER operations
372           [](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
373             return MixedRealLeft<OPR>(std::move(rx), std::move(iy));
374           },
375           [](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
376             return Package(std::visit(
377                 [&](auto &&ryk) -> Expr<SomeReal> {
378                   using resultType = ResultType<decltype(ryk)>;
379                   return AsCategoryExpr(
380                       OPR<resultType>{ConvertToType<resultType>(std::move(ix)),
381                           std::move(ryk)});
382                 },
383                 std::move(ry.u)));
384           },
385           // Homogeneous and mixed COMPLEX operations
386           [](Expr<SomeComplex> &&zx, Expr<SomeComplex> &&zy) {
387             return Package(PromoteAndCombine<OPR, TypeCategory::Complex>(
388                 std::move(zx), std::move(zy)));
389           },
390           [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) {
391             return MixedComplexLeft<OPR>(
392                 messages, std::move(zx), std::move(iy), defaultRealKind);
393           },
394           [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) {
395             return MixedComplexLeft<OPR>(
396                 messages, std::move(zx), std::move(ry), defaultRealKind);
397           },
398           [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) {
399             return MixedComplexRight<OPR>(
400                 messages, std::move(ix), std::move(zy), defaultRealKind);
401           },
402           [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) {
403             return MixedComplexRight<OPR>(
404                 messages, std::move(rx), std::move(zy), defaultRealKind);
405           },
406           // Operations with one typeless operand
407           [&](BOZLiteralConstant &&bx, Expr<SomeInteger> &&iy) {
408             return NumericOperation<OPR>(messages,
409                 AsGenericExpr(ConvertTo(iy, std::move(bx))), std::move(y),
410                 defaultRealKind);
411           },
412           [&](BOZLiteralConstant &&bx, Expr<SomeReal> &&ry) {
413             return NumericOperation<OPR>(messages,
414                 AsGenericExpr(ConvertTo(ry, std::move(bx))), std::move(y),
415                 defaultRealKind);
416           },
417           [&](Expr<SomeInteger> &&ix, BOZLiteralConstant &&by) {
418             return NumericOperation<OPR>(messages, std::move(x),
419                 AsGenericExpr(ConvertTo(ix, std::move(by))), defaultRealKind);
420           },
421           [&](Expr<SomeReal> &&rx, BOZLiteralConstant &&by) {
422             return NumericOperation<OPR>(messages, std::move(x),
423                 AsGenericExpr(ConvertTo(rx, std::move(by))), defaultRealKind);
424           },
425           // Default case
426           [&](auto &&, auto &&) {
427             // TODO: defined operator
428             messages.Say("non-numeric operands to numeric operation"_err_en_US);
429             return NoExpr();
430           },
431       },
432       std::move(x.u), std::move(y.u));
433 }
434 
435 template std::optional<Expr<SomeType>> NumericOperation<Power>(
436     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
437     int defaultRealKind);
438 template std::optional<Expr<SomeType>> NumericOperation<Multiply>(
439     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
440     int defaultRealKind);
441 template std::optional<Expr<SomeType>> NumericOperation<Divide>(
442     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
443     int defaultRealKind);
444 template std::optional<Expr<SomeType>> NumericOperation<Add>(
445     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
446     int defaultRealKind);
447 template std::optional<Expr<SomeType>> NumericOperation<Subtract>(
448     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
449     int defaultRealKind);
450 
451 std::optional<Expr<SomeType>> Negation(
452     parser::ContextualMessages &messages, Expr<SomeType> &&x) {
453   return std::visit(
454       common::visitors{
455           [&](BOZLiteralConstant &&) {
456             messages.Say("BOZ literal cannot be negated"_err_en_US);
457             return NoExpr();
458           },
459           [&](NullPointer &&) {
460             messages.Say("NULL() cannot be negated"_err_en_US);
461             return NoExpr();
462           },
463           [&](ProcedureDesignator &&) {
464             messages.Say("Subroutine cannot be negated"_err_en_US);
465             return NoExpr();
466           },
467           [&](ProcedureRef &&) {
468             messages.Say("Pointer to subroutine cannot be negated"_err_en_US);
469             return NoExpr();
470           },
471           [&](Expr<SomeInteger> &&x) { return Package(-std::move(x)); },
472           [&](Expr<SomeReal> &&x) { return Package(-std::move(x)); },
473           [&](Expr<SomeComplex> &&x) { return Package(-std::move(x)); },
474           [&](Expr<SomeCharacter> &&) {
475             // TODO: defined operator
476             messages.Say("CHARACTER cannot be negated"_err_en_US);
477             return NoExpr();
478           },
479           [&](Expr<SomeLogical> &&) {
480             // TODO: defined operator
481             messages.Say("LOGICAL cannot be negated"_err_en_US);
482             return NoExpr();
483           },
484           [&](Expr<SomeDerived> &&) {
485             // TODO: defined operator
486             messages.Say("Operand cannot be negated"_err_en_US);
487             return NoExpr();
488           },
489       },
490       std::move(x.u));
491 }
492 
493 Expr<SomeLogical> LogicalNegation(Expr<SomeLogical> &&x) {
494   return std::visit(
495       [](auto &&xk) { return AsCategoryExpr(LogicalNegation(std::move(xk))); },
496       std::move(x.u));
497 }
498 
499 template <TypeCategory CAT>
500 Expr<LogicalResult> PromoteAndRelate(
501     RelationalOperator opr, Expr<SomeKind<CAT>> &&x, Expr<SomeKind<CAT>> &&y) {
502   return std::visit(
503       [=](auto &&xy) {
504         return PackageRelation(opr, std::move(xy[0]), std::move(xy[1]));
505       },
506       AsSameKindExprs(std::move(x), std::move(y)));
507 }
508 
509 std::optional<Expr<LogicalResult>> Relate(parser::ContextualMessages &messages,
510     RelationalOperator opr, Expr<SomeType> &&x, Expr<SomeType> &&y) {
511   return std::visit(
512       common::visitors{
513           [=](Expr<SomeInteger> &&ix,
514               Expr<SomeInteger> &&iy) -> std::optional<Expr<LogicalResult>> {
515             return PromoteAndRelate(opr, std::move(ix), std::move(iy));
516           },
517           [=](Expr<SomeReal> &&rx,
518               Expr<SomeReal> &&ry) -> std::optional<Expr<LogicalResult>> {
519             return PromoteAndRelate(opr, std::move(rx), std::move(ry));
520           },
521           [&](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
522             return Relate(messages, opr, std::move(x),
523                 AsGenericExpr(ConvertTo(rx, std::move(iy))));
524           },
525           [&](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
526             return Relate(messages, opr,
527                 AsGenericExpr(ConvertTo(ry, std::move(ix))), std::move(y));
528           },
529           [&](Expr<SomeComplex> &&zx,
530               Expr<SomeComplex> &&zy) -> std::optional<Expr<LogicalResult>> {
531             if (opr == RelationalOperator::EQ ||
532                 opr == RelationalOperator::NE) {
533               return PromoteAndRelate(opr, std::move(zx), std::move(zy));
534             } else {
535               messages.Say(
536                   "COMPLEX data may be compared only for equality"_err_en_US);
537               return std::nullopt;
538             }
539           },
540           [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) {
541             return Relate(messages, opr, std::move(x),
542                 AsGenericExpr(ConvertTo(zx, std::move(iy))));
543           },
544           [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) {
545             return Relate(messages, opr, std::move(x),
546                 AsGenericExpr(ConvertTo(zx, std::move(ry))));
547           },
548           [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) {
549             return Relate(messages, opr,
550                 AsGenericExpr(ConvertTo(zy, std::move(ix))), std::move(y));
551           },
552           [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) {
553             return Relate(messages, opr,
554                 AsGenericExpr(ConvertTo(zy, std::move(rx))), std::move(y));
555           },
556           [&](Expr<SomeCharacter> &&cx, Expr<SomeCharacter> &&cy) {
557             return std::visit(
558                 [&](auto &&cxk,
559                     auto &&cyk) -> std::optional<Expr<LogicalResult>> {
560                   using Ty = ResultType<decltype(cxk)>;
561                   if constexpr (std::is_same_v<Ty, ResultType<decltype(cyk)>>) {
562                     return PackageRelation(opr, std::move(cxk), std::move(cyk));
563                   } else {
564                     messages.Say(
565                         "CHARACTER operands do not have same KIND"_err_en_US);
566                     return std::nullopt;
567                   }
568                 },
569                 std::move(cx.u), std::move(cy.u));
570           },
571           // Default case
572           [&](auto &&, auto &&) {
573             DIE("invalid types for relational operator");
574             return std::optional<Expr<LogicalResult>>{};
575           },
576       },
577       std::move(x.u), std::move(y.u));
578 }
579 
580 Expr<SomeLogical> BinaryLogicalOperation(
581     LogicalOperator opr, Expr<SomeLogical> &&x, Expr<SomeLogical> &&y) {
582   CHECK(opr != LogicalOperator::Not);
583   return std::visit(
584       [=](auto &&xy) {
585         using Ty = ResultType<decltype(xy[0])>;
586         return Expr<SomeLogical>{BinaryLogicalOperation<Ty::kind>(
587             opr, std::move(xy[0]), std::move(xy[1]))};
588       },
589       AsSameKindExprs(std::move(x), std::move(y)));
590 }
591 
592 template <TypeCategory TO>
593 std::optional<Expr<SomeType>> ConvertToNumeric(int kind, Expr<SomeType> &&x) {
594   static_assert(common::IsNumericTypeCategory(TO));
595   return std::visit(
596       [=](auto &&cx) -> std::optional<Expr<SomeType>> {
597         using cxType = std::decay_t<decltype(cx)>;
598         if constexpr (!common::HasMember<cxType, TypelessExpression>) {
599           if constexpr (IsNumericTypeCategory(ResultType<cxType>::category)) {
600             return Expr<SomeType>{ConvertToKind<TO>(kind, std::move(cx))};
601           }
602         }
603         return std::nullopt;
604       },
605       std::move(x.u));
606 }
607 
608 std::optional<Expr<SomeType>> ConvertToType(
609     const DynamicType &type, Expr<SomeType> &&x) {
610   switch (type.category()) {
611   case TypeCategory::Integer:
612     if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
613       // Extension to C7109: allow BOZ literals to appear in integer contexts
614       // when the type is unambiguous.
615       return Expr<SomeType>{
616           ConvertToKind<TypeCategory::Integer>(type.kind(), std::move(*boz))};
617     }
618     return ConvertToNumeric<TypeCategory::Integer>(type.kind(), std::move(x));
619   case TypeCategory::Real:
620     if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
621       return Expr<SomeType>{
622           ConvertToKind<TypeCategory::Real>(type.kind(), std::move(*boz))};
623     }
624     return ConvertToNumeric<TypeCategory::Real>(type.kind(), std::move(x));
625   case TypeCategory::Complex:
626     return ConvertToNumeric<TypeCategory::Complex>(type.kind(), std::move(x));
627   case TypeCategory::Character:
628     if (auto *cx{UnwrapExpr<Expr<SomeCharacter>>(x)}) {
629       auto converted{
630           ConvertToKind<TypeCategory::Character>(type.kind(), std::move(*cx))};
631       if (auto length{type.GetCharLength()}) {
632         converted = std::visit(
633             [&](auto &&x) {
634               using Ty = std::decay_t<decltype(x)>;
635               using CharacterType = typename Ty::Result;
636               return Expr<SomeCharacter>{
637                   Expr<CharacterType>{SetLength<CharacterType::kind>{
638                       std::move(x), std::move(*length)}}};
639             },
640             std::move(converted.u));
641       }
642       return Expr<SomeType>{std::move(converted)};
643     }
644     break;
645   case TypeCategory::Logical:
646     if (auto *cx{UnwrapExpr<Expr<SomeLogical>>(x)}) {
647       return Expr<SomeType>{
648           ConvertToKind<TypeCategory::Logical>(type.kind(), std::move(*cx))};
649     }
650     break;
651   case TypeCategory::Derived:
652     if (auto fromType{x.GetType()}) {
653       if (type == *fromType) {
654         return std::move(x);
655       }
656     }
657     break;
658   }
659   return std::nullopt;
660 }
661 
662 std::optional<Expr<SomeType>> ConvertToType(
663     const DynamicType &to, std::optional<Expr<SomeType>> &&x) {
664   if (x) {
665     return ConvertToType(to, std::move(*x));
666   } else {
667     return std::nullopt;
668   }
669 }
670 
671 std::optional<Expr<SomeType>> ConvertToType(
672     const Symbol &symbol, Expr<SomeType> &&x) {
673   if (auto symType{DynamicType::From(symbol)}) {
674     return ConvertToType(*symType, std::move(x));
675   }
676   return std::nullopt;
677 }
678 
679 std::optional<Expr<SomeType>> ConvertToType(
680     const Symbol &to, std::optional<Expr<SomeType>> &&x) {
681   if (x) {
682     return ConvertToType(to, std::move(*x));
683   } else {
684     return std::nullopt;
685   }
686 }
687 
688 bool IsAssumedRank(const Symbol &original) {
689   if (const auto *assoc{original.detailsIf<semantics::AssocEntityDetails>()}) {
690     if (assoc->rank()) {
691       return false; // in SELECT RANK case
692     }
693   }
694   const Symbol &symbol{semantics::ResolveAssociations(original)};
695   if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) {
696     return details->IsAssumedRank();
697   } else {
698     return false;
699   }
700 }
701 
702 bool IsAssumedRank(const ActualArgument &arg) {
703   if (const auto *expr{arg.UnwrapExpr()}) {
704     return IsAssumedRank(*expr);
705   } else {
706     const Symbol *assumedTypeDummy{arg.GetAssumedTypeDummy()};
707     CHECK(assumedTypeDummy);
708     return IsAssumedRank(*assumedTypeDummy);
709   }
710 }
711 
712 bool IsCoarray(const ActualArgument &arg) {
713   const auto *expr{arg.UnwrapExpr()};
714   return expr && IsCoarray(*expr);
715 }
716 
717 bool IsCoarray(const Symbol &symbol) {
718   return GetAssociationRoot(symbol).Corank() > 0;
719 }
720 
721 bool IsProcedure(const Expr<SomeType> &expr) {
722   return std::holds_alternative<ProcedureDesignator>(expr.u);
723 }
724 bool IsFunction(const Expr<SomeType> &expr) {
725   const auto *designator{std::get_if<ProcedureDesignator>(&expr.u)};
726   return designator && designator->GetType().has_value();
727 }
728 
729 bool IsProcedurePointerTarget(const Expr<SomeType> &expr) {
730   return std::visit(common::visitors{
731                         [](const NullPointer &) { return true; },
732                         [](const ProcedureDesignator &) { return true; },
733                         [](const ProcedureRef &) { return true; },
734                         [&](const auto &) {
735                           const Symbol *last{GetLastSymbol(expr)};
736                           return last && IsProcedurePointer(*last);
737                         },
738                     },
739       expr.u);
740 }
741 
742 template <typename A> inline const ProcedureRef *UnwrapProcedureRef(const A &) {
743   return nullptr;
744 }
745 
746 template <typename T>
747 inline const ProcedureRef *UnwrapProcedureRef(const FunctionRef<T> &func) {
748   return &func;
749 }
750 
751 template <typename T>
752 inline const ProcedureRef *UnwrapProcedureRef(const Expr<T> &expr) {
753   return std::visit(
754       [](const auto &x) { return UnwrapProcedureRef(x); }, expr.u);
755 }
756 
757 // IsObjectPointer()
758 bool IsObjectPointer(const Expr<SomeType> &expr, FoldingContext &context) {
759   if (IsNullPointer(expr)) {
760     return true;
761   } else if (IsProcedurePointerTarget(expr)) {
762     return false;
763   } else if (const auto *funcRef{UnwrapProcedureRef(expr)}) {
764     return IsVariable(*funcRef);
765   } else if (const Symbol * symbol{GetLastSymbol(expr)}) {
766     return IsPointer(symbol->GetUltimate());
767   } else {
768     return false;
769   }
770 }
771 
772 bool IsBareNullPointer(const Expr<SomeType> *expr) {
773   return expr && std::holds_alternative<NullPointer>(expr->u);
774 }
775 
776 // IsNullPointer()
777 struct IsNullPointerHelper {
778   template <typename A> bool operator()(const A &) const { return false; }
779   template <typename T> bool operator()(const FunctionRef<T> &call) const {
780     const auto *intrinsic{call.proc().GetSpecificIntrinsic()};
781     return intrinsic &&
782         intrinsic->characteristics.value().attrs.test(
783             characteristics::Procedure::Attr::NullPointer);
784   }
785   bool operator()(const NullPointer &) const { return true; }
786   template <typename T> bool operator()(const Parentheses<T> &x) const {
787     return (*this)(x.left());
788   }
789   template <typename T> bool operator()(const Expr<T> &x) const {
790     return std::visit(*this, x.u);
791   }
792 };
793 
794 bool IsNullPointer(const Expr<SomeType> &expr) {
795   return IsNullPointerHelper{}(expr);
796 }
797 
798 // GetSymbolVector()
799 auto GetSymbolVectorHelper::operator()(const Symbol &x) const -> Result {
800   if (const auto *details{x.detailsIf<semantics::AssocEntityDetails>()}) {
801     return (*this)(details->expr());
802   } else {
803     return {x.GetUltimate()};
804   }
805 }
806 auto GetSymbolVectorHelper::operator()(const Component &x) const -> Result {
807   Result result{(*this)(x.base())};
808   result.emplace_back(x.GetLastSymbol());
809   return result;
810 }
811 auto GetSymbolVectorHelper::operator()(const ArrayRef &x) const -> Result {
812   return GetSymbolVector(x.base());
813 }
814 auto GetSymbolVectorHelper::operator()(const CoarrayRef &x) const -> Result {
815   return x.base();
816 }
817 
818 const Symbol *GetLastTarget(const SymbolVector &symbols) {
819   auto end{std::crend(symbols)};
820   // N.B. Neither clang nor g++ recognizes "symbols.crbegin()" here.
821   auto iter{std::find_if(std::crbegin(symbols), end, [](const Symbol &x) {
822     return x.attrs().HasAny(
823         {semantics::Attr::POINTER, semantics::Attr::TARGET});
824   })};
825   return iter == end ? nullptr : &**iter;
826 }
827 
828 struct CollectSymbolsHelper
829     : public SetTraverse<CollectSymbolsHelper, semantics::UnorderedSymbolSet> {
830   using Base = SetTraverse<CollectSymbolsHelper, semantics::UnorderedSymbolSet>;
831   CollectSymbolsHelper() : Base{*this} {}
832   using Base::operator();
833   semantics::UnorderedSymbolSet operator()(const Symbol &symbol) const {
834     return {symbol};
835   }
836 };
837 template <typename A> semantics::UnorderedSymbolSet CollectSymbols(const A &x) {
838   return CollectSymbolsHelper{}(x);
839 }
840 template semantics::UnorderedSymbolSet CollectSymbols(const Expr<SomeType> &);
841 template semantics::UnorderedSymbolSet CollectSymbols(
842     const Expr<SomeInteger> &);
843 template semantics::UnorderedSymbolSet CollectSymbols(
844     const Expr<SubscriptInteger> &);
845 
846 // HasVectorSubscript()
847 struct HasVectorSubscriptHelper : public AnyTraverse<HasVectorSubscriptHelper> {
848   using Base = AnyTraverse<HasVectorSubscriptHelper>;
849   HasVectorSubscriptHelper() : Base{*this} {}
850   using Base::operator();
851   bool operator()(const Subscript &ss) const {
852     return !std::holds_alternative<Triplet>(ss.u) && ss.Rank() > 0;
853   }
854   bool operator()(const ProcedureRef &) const {
855     return false; // don't descend into function call arguments
856   }
857 };
858 
859 bool HasVectorSubscript(const Expr<SomeType> &expr) {
860   return HasVectorSubscriptHelper{}(expr);
861 }
862 
863 parser::Message *AttachDeclaration(
864     parser::Message &message, const Symbol &symbol) {
865   const Symbol *unhosted{&symbol};
866   while (
867       const auto *assoc{unhosted->detailsIf<semantics::HostAssocDetails>()}) {
868     unhosted = &assoc->symbol();
869   }
870   if (const auto *binding{
871           unhosted->detailsIf<semantics::ProcBindingDetails>()}) {
872     if (binding->symbol().name() != symbol.name()) {
873       message.Attach(binding->symbol().name(),
874           "Procedure '%s' of type '%s' is bound to '%s'"_en_US, symbol.name(),
875           symbol.owner().GetName().value(), binding->symbol().name());
876       return &message;
877     }
878     unhosted = &binding->symbol();
879   }
880   if (const auto *use{symbol.detailsIf<semantics::UseDetails>()}) {
881     message.Attach(use->location(),
882         "'%s' is USE-associated with '%s' in module '%s'"_en_US, symbol.name(),
883         unhosted->name(), GetUsedModule(*use).name());
884   } else {
885     message.Attach(
886         unhosted->name(), "Declaration of '%s'"_en_US, unhosted->name());
887   }
888   return &message;
889 }
890 
891 parser::Message *AttachDeclaration(
892     parser::Message *message, const Symbol &symbol) {
893   return message ? AttachDeclaration(*message, symbol) : nullptr;
894 }
895 
896 class FindImpureCallHelper
897     : public AnyTraverse<FindImpureCallHelper, std::optional<std::string>> {
898   using Result = std::optional<std::string>;
899   using Base = AnyTraverse<FindImpureCallHelper, Result>;
900 
901 public:
902   explicit FindImpureCallHelper(FoldingContext &c) : Base{*this}, context_{c} {}
903   using Base::operator();
904   Result operator()(const ProcedureRef &call) const {
905     if (auto chars{
906             characteristics::Procedure::Characterize(call.proc(), context_)}) {
907       if (chars->attrs.test(characteristics::Procedure::Attr::Pure)) {
908         return (*this)(call.arguments());
909       }
910     }
911     return call.proc().GetName();
912   }
913 
914 private:
915   FoldingContext &context_;
916 };
917 
918 std::optional<std::string> FindImpureCall(
919     FoldingContext &context, const Expr<SomeType> &expr) {
920   return FindImpureCallHelper{context}(expr);
921 }
922 std::optional<std::string> FindImpureCall(
923     FoldingContext &context, const ProcedureRef &proc) {
924   return FindImpureCallHelper{context}(proc);
925 }
926 
927 // Compare procedure characteristics for equality except that rhs may be
928 // Pure or Elemental when lhs is not.
929 static bool CharacteristicsMatch(const characteristics::Procedure &lhs,
930     const characteristics::Procedure &rhs) {
931   using Attr = characteristics::Procedure::Attr;
932   auto lhsAttrs{lhs.attrs};
933   lhsAttrs.set(
934       Attr::Pure, lhs.attrs.test(Attr::Pure) || rhs.attrs.test(Attr::Pure));
935   lhsAttrs.set(Attr::Elemental,
936       lhs.attrs.test(Attr::Elemental) || rhs.attrs.test(Attr::Elemental));
937   return lhsAttrs == rhs.attrs && lhs.functionResult == rhs.functionResult &&
938       lhs.dummyArguments == rhs.dummyArguments;
939 }
940 
941 // Common handling for procedure pointer compatibility of left- and right-hand
942 // sides.  Returns nullopt if they're compatible.  Otherwise, it returns a
943 // message that needs to be augmented by the names of the left and right sides
944 std::optional<parser::MessageFixedText> CheckProcCompatibility(bool isCall,
945     const std::optional<characteristics::Procedure> &lhsProcedure,
946     const characteristics::Procedure *rhsProcedure) {
947   std::optional<parser::MessageFixedText> msg;
948   if (!lhsProcedure) {
949     msg = "In assignment to object %s, the target '%s' is a procedure"
950           " designator"_err_en_US;
951   } else if (!rhsProcedure) {
952     msg = "In assignment to procedure %s, the characteristics of the target"
953           " procedure '%s' could not be determined"_err_en_US;
954   } else if (CharacteristicsMatch(*lhsProcedure, *rhsProcedure)) {
955     // OK
956   } else if (isCall) {
957     msg = "Procedure %s associated with result of reference to function '%s'"
958           " that is an incompatible procedure pointer"_err_en_US;
959   } else if (lhsProcedure->IsPure() && !rhsProcedure->IsPure()) {
960     msg = "PURE procedure %s may not be associated with non-PURE"
961           " procedure designator '%s'"_err_en_US;
962   } else if (lhsProcedure->IsFunction() && !rhsProcedure->IsFunction()) {
963     msg = "Function %s may not be associated with subroutine"
964           " designator '%s'"_err_en_US;
965   } else if (!lhsProcedure->IsFunction() && rhsProcedure->IsFunction()) {
966     msg = "Subroutine %s may not be associated with function"
967           " designator '%s'"_err_en_US;
968   } else if (lhsProcedure->HasExplicitInterface() &&
969       !rhsProcedure->HasExplicitInterface()) {
970     // Section 10.2.2.4, paragraph 3 prohibits associating a procedure pointer
971     // with an explicit interface with a procedure with an implicit interface
972     msg = "Procedure %s with explicit interface may not be associated with"
973           " procedure designator '%s' with implicit interface"_err_en_US;
974   } else if (!lhsProcedure->HasExplicitInterface() &&
975       rhsProcedure->HasExplicitInterface()) {
976     if (!rhsProcedure->CanBeCalledViaImplicitInterface()) {
977       msg = "Procedure %s with implicit interface may not be associated "
978             "with procedure designator '%s' with explicit interface that "
979             "cannot be called via an implicit interface"_err_en_US;
980     }
981   } else {
982     msg = "Procedure %s associated with incompatible procedure"
983           " designator '%s'"_err_en_US;
984   }
985   return msg;
986 }
987 
988 // GetLastPointerSymbol()
989 static const Symbol *GetLastPointerSymbol(const Symbol &symbol) {
990   return IsPointer(GetAssociationRoot(symbol)) ? &symbol : nullptr;
991 }
992 static const Symbol *GetLastPointerSymbol(const SymbolRef &symbol) {
993   return GetLastPointerSymbol(*symbol);
994 }
995 static const Symbol *GetLastPointerSymbol(const Component &x) {
996   const Symbol &c{x.GetLastSymbol()};
997   return IsPointer(c) ? &c : GetLastPointerSymbol(x.base());
998 }
999 static const Symbol *GetLastPointerSymbol(const NamedEntity &x) {
1000   const auto *c{x.UnwrapComponent()};
1001   return c ? GetLastPointerSymbol(*c) : GetLastPointerSymbol(x.GetLastSymbol());
1002 }
1003 static const Symbol *GetLastPointerSymbol(const ArrayRef &x) {
1004   return GetLastPointerSymbol(x.base());
1005 }
1006 static const Symbol *GetLastPointerSymbol(const CoarrayRef &x) {
1007   return nullptr;
1008 }
1009 const Symbol *GetLastPointerSymbol(const DataRef &x) {
1010   return std::visit([](const auto &y) { return GetLastPointerSymbol(y); }, x.u);
1011 }
1012 
1013 } // namespace Fortran::evaluate
1014 
1015 namespace Fortran::semantics {
1016 
1017 const Symbol &ResolveAssociations(const Symbol &original) {
1018   const Symbol &symbol{original.GetUltimate()};
1019   if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) {
1020     if (const Symbol * nested{UnwrapWholeSymbolDataRef(details->expr())}) {
1021       return ResolveAssociations(*nested);
1022     }
1023   }
1024   return symbol;
1025 }
1026 
1027 // When a construct association maps to a variable, and that variable
1028 // is not an array with a vector-valued subscript, return the base
1029 // Symbol of that variable, else nullptr.  Descends into other construct
1030 // associations when one associations maps to another.
1031 static const Symbol *GetAssociatedVariable(const AssocEntityDetails &details) {
1032   if (const auto &expr{details.expr()}) {
1033     if (IsVariable(*expr) && !HasVectorSubscript(*expr)) {
1034       if (const Symbol * varSymbol{GetFirstSymbol(*expr)}) {
1035         return &GetAssociationRoot(*varSymbol);
1036       }
1037     }
1038   }
1039   return nullptr;
1040 }
1041 
1042 const Symbol &GetAssociationRoot(const Symbol &original) {
1043   const Symbol &symbol{ResolveAssociations(original)};
1044   if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) {
1045     if (const Symbol * root{GetAssociatedVariable(*details)}) {
1046       return *root;
1047     }
1048   }
1049   return symbol;
1050 }
1051 
1052 const Symbol *GetMainEntry(const Symbol *symbol) {
1053   if (symbol) {
1054     if (const auto *subpDetails{symbol->detailsIf<SubprogramDetails>()}) {
1055       if (const Scope * scope{subpDetails->entryScope()}) {
1056         if (const Symbol * main{scope->symbol()}) {
1057           return main;
1058         }
1059       }
1060     }
1061   }
1062   return symbol;
1063 }
1064 
1065 bool IsVariableName(const Symbol &original) {
1066   const Symbol &symbol{ResolveAssociations(original)};
1067   if (symbol.has<ObjectEntityDetails>()) {
1068     return !IsNamedConstant(symbol);
1069   } else if (const auto *assoc{symbol.detailsIf<AssocEntityDetails>()}) {
1070     const auto &expr{assoc->expr()};
1071     return expr && IsVariable(*expr) && !HasVectorSubscript(*expr);
1072   } else {
1073     return false;
1074   }
1075 }
1076 
1077 bool IsPureProcedure(const Symbol &original) {
1078   // An ENTRY is pure if its containing subprogram is
1079   const Symbol &symbol{DEREF(GetMainEntry(&original.GetUltimate()))};
1080   if (const auto *procDetails{symbol.detailsIf<ProcEntityDetails>()}) {
1081     if (const Symbol * procInterface{procDetails->interface().symbol()}) {
1082       // procedure component with a pure interface
1083       return IsPureProcedure(*procInterface);
1084     }
1085   } else if (const auto *details{symbol.detailsIf<ProcBindingDetails>()}) {
1086     return IsPureProcedure(details->symbol());
1087   } else if (!IsProcedure(symbol)) {
1088     return false;
1089   }
1090   if (IsStmtFunction(symbol)) {
1091     // Section 15.7(1) states that a statement function is PURE if it does not
1092     // reference an IMPURE procedure or a VOLATILE variable
1093     if (const auto &expr{symbol.get<SubprogramDetails>().stmtFunction()}) {
1094       for (const SymbolRef &ref : evaluate::CollectSymbols(*expr)) {
1095         if (IsFunction(*ref) && !IsPureProcedure(*ref)) {
1096           return false;
1097         }
1098         if (ref->GetUltimate().attrs().test(Attr::VOLATILE)) {
1099           return false;
1100         }
1101       }
1102     }
1103     return true; // statement function was not found to be impure
1104   }
1105   return symbol.attrs().test(Attr::PURE) ||
1106       (symbol.attrs().test(Attr::ELEMENTAL) &&
1107           !symbol.attrs().test(Attr::IMPURE));
1108 }
1109 
1110 bool IsPureProcedure(const Scope &scope) {
1111   const Symbol *symbol{scope.GetSymbol()};
1112   return symbol && IsPureProcedure(*symbol);
1113 }
1114 
1115 bool IsFunction(const Symbol &symbol) {
1116   return std::visit(
1117       common::visitors{
1118           [](const SubprogramDetails &x) { return x.isFunction(); },
1119           [&](const SubprogramNameDetails &) {
1120             return symbol.test(Symbol::Flag::Function);
1121           },
1122           [](const ProcEntityDetails &x) {
1123             const auto &ifc{x.interface()};
1124             return ifc.type() || (ifc.symbol() && IsFunction(*ifc.symbol()));
1125           },
1126           [](const ProcBindingDetails &x) { return IsFunction(x.symbol()); },
1127           [](const auto &) { return false; },
1128       },
1129       symbol.GetUltimate().details());
1130 }
1131 
1132 bool IsFunction(const Scope &scope) {
1133   const Symbol *symbol{scope.GetSymbol()};
1134   return symbol && IsFunction(*symbol);
1135 }
1136 
1137 bool IsProcedure(const Symbol &symbol) {
1138   return std::visit(common::visitors{
1139                         [](const SubprogramDetails &) { return true; },
1140                         [](const SubprogramNameDetails &) { return true; },
1141                         [](const ProcEntityDetails &) { return true; },
1142                         [](const GenericDetails &) { return true; },
1143                         [](const ProcBindingDetails &) { return true; },
1144                         [](const auto &) { return false; },
1145                     },
1146       symbol.GetUltimate().details());
1147 }
1148 
1149 bool IsProcedure(const Scope &scope) {
1150   const Symbol *symbol{scope.GetSymbol()};
1151   return symbol && IsProcedure(*symbol);
1152 }
1153 
1154 const Symbol *FindCommonBlockContaining(const Symbol &original) {
1155   const Symbol &root{GetAssociationRoot(original)};
1156   const auto *details{root.detailsIf<ObjectEntityDetails>()};
1157   return details ? details->commonBlock() : nullptr;
1158 }
1159 
1160 bool IsProcedurePointer(const Symbol &original) {
1161   const Symbol &symbol{GetAssociationRoot(original)};
1162   return symbol.has<ProcEntityDetails>() && IsPointer(symbol);
1163 }
1164 
1165 // 3.11 automatic data object
1166 bool IsAutomatic(const Symbol &original) {
1167   const Symbol &symbol{original.GetUltimate()};
1168   if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) {
1169     if (!object->isDummy() && !IsAllocatable(symbol) && !IsPointer(symbol)) {
1170       if (const DeclTypeSpec * type{symbol.GetType()}) {
1171         // If a type parameter value is not a constant expression, the
1172         // object is automatic.
1173         if (type->category() == DeclTypeSpec::Character) {
1174           if (const auto &length{
1175                   type->characterTypeSpec().length().GetExplicit()}) {
1176             if (!evaluate::IsConstantExpr(*length)) {
1177               return true;
1178             }
1179           }
1180         } else if (const DerivedTypeSpec * derived{type->AsDerived()}) {
1181           for (const auto &pair : derived->parameters()) {
1182             if (const auto &value{pair.second.GetExplicit()}) {
1183               if (!evaluate::IsConstantExpr(*value)) {
1184                 return true;
1185               }
1186             }
1187           }
1188         }
1189       }
1190       // If an array bound is not a constant expression, the object is
1191       // automatic.
1192       for (const ShapeSpec &dim : object->shape()) {
1193         if (const auto &lb{dim.lbound().GetExplicit()}) {
1194           if (!evaluate::IsConstantExpr(*lb)) {
1195             return true;
1196           }
1197         }
1198         if (const auto &ub{dim.ubound().GetExplicit()}) {
1199           if (!evaluate::IsConstantExpr(*ub)) {
1200             return true;
1201           }
1202         }
1203       }
1204     }
1205   }
1206   return false;
1207 }
1208 
1209 bool IsSaved(const Symbol &original) {
1210   const Symbol &symbol{GetAssociationRoot(original)};
1211   const Scope &scope{symbol.owner()};
1212   auto scopeKind{scope.kind()};
1213   if (symbol.has<AssocEntityDetails>()) {
1214     return false; // ASSOCIATE(non-variable)
1215   } else if (scopeKind == Scope::Kind::DerivedType) {
1216     return false; // this is a component
1217   } else if (symbol.attrs().test(Attr::SAVE)) {
1218     return true; // explicit SAVE attribute
1219   } else if (IsDummy(symbol) || IsFunctionResult(symbol) ||
1220       IsAutomatic(symbol) || IsNamedConstant(symbol)) {
1221     return false;
1222   } else if (scopeKind == Scope::Kind::Module ||
1223       (scopeKind == Scope::Kind::MainProgram &&
1224           (symbol.attrs().test(Attr::TARGET) || evaluate::IsCoarray(symbol)))) {
1225     // 8.5.16p4
1226     // In main programs, implied SAVE matters only for pointer
1227     // initialization targets and coarrays.
1228     // BLOCK DATA entities must all be in COMMON,
1229     // which was checked above.
1230     return true;
1231   } else if (scope.kind() == Scope::Kind::Subprogram &&
1232       scope.context().languageFeatures().IsEnabled(
1233           common::LanguageFeature::DefaultSave) &&
1234       !(scope.symbol() && scope.symbol()->attrs().test(Attr::RECURSIVE))) {
1235     // -fno-automatic/-save/-Msave option applies to objects in
1236     // executable subprograms unless they are explicitly RECURSIVE.
1237     return true;
1238   } else if (symbol.test(Symbol::Flag::InDataStmt)) {
1239     return true;
1240   } else if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()};
1241              object && object->init()) {
1242     return true;
1243   } else if (IsProcedurePointer(symbol) &&
1244       symbol.get<ProcEntityDetails>().init()) {
1245     return true;
1246   } else if (scope.hasSAVE()) {
1247     return true; // bare SAVE statement
1248   } else if (const Symbol * block{FindCommonBlockContaining(symbol)};
1249              block && block->attrs().test(Attr::SAVE)) {
1250     return true; // in COMMON with SAVE
1251   } else {
1252     return false;
1253   }
1254 }
1255 
1256 bool IsDummy(const Symbol &symbol) {
1257   return std::visit(
1258       common::visitors{[](const EntityDetails &x) { return x.isDummy(); },
1259           [](const ObjectEntityDetails &x) { return x.isDummy(); },
1260           [](const ProcEntityDetails &x) { return x.isDummy(); },
1261           [](const SubprogramDetails &x) { return x.isDummy(); },
1262           [](const auto &) { return false; }},
1263       ResolveAssociations(symbol).details());
1264 }
1265 
1266 bool IsAssumedShape(const Symbol &symbol) {
1267   const Symbol &ultimate{ResolveAssociations(symbol)};
1268   const auto *object{ultimate.detailsIf<ObjectEntityDetails>()};
1269   return object && object->CanBeAssumedShape() &&
1270       !evaluate::IsAllocatableOrPointer(ultimate);
1271 }
1272 
1273 bool IsDeferredShape(const Symbol &symbol) {
1274   const Symbol &ultimate{ResolveAssociations(symbol)};
1275   const auto *object{ultimate.detailsIf<ObjectEntityDetails>()};
1276   return object && object->CanBeDeferredShape() &&
1277       evaluate::IsAllocatableOrPointer(ultimate);
1278 }
1279 
1280 bool IsFunctionResult(const Symbol &original) {
1281   const Symbol &symbol{GetAssociationRoot(original)};
1282   return (symbol.has<ObjectEntityDetails>() &&
1283              symbol.get<ObjectEntityDetails>().isFuncResult()) ||
1284       (symbol.has<ProcEntityDetails>() &&
1285           symbol.get<ProcEntityDetails>().isFuncResult());
1286 }
1287 
1288 bool IsKindTypeParameter(const Symbol &symbol) {
1289   const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()};
1290   return param && param->attr() == common::TypeParamAttr::Kind;
1291 }
1292 
1293 bool IsLenTypeParameter(const Symbol &symbol) {
1294   const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()};
1295   return param && param->attr() == common::TypeParamAttr::Len;
1296 }
1297 
1298 bool IsExtensibleType(const DerivedTypeSpec *derived) {
1299   return derived && !IsIsoCType(derived) &&
1300       !derived->typeSymbol().attrs().test(Attr::BIND_C) &&
1301       !derived->typeSymbol().get<DerivedTypeDetails>().sequence();
1302 }
1303 
1304 bool IsBuiltinDerivedType(const DerivedTypeSpec *derived, const char *name) {
1305   if (!derived) {
1306     return false;
1307   } else {
1308     const auto &symbol{derived->typeSymbol()};
1309     return &symbol.owner() == symbol.owner().context().GetBuiltinsScope() &&
1310         symbol.name() == "__builtin_"s + name;
1311   }
1312 }
1313 
1314 bool IsIsoCType(const DerivedTypeSpec *derived) {
1315   return IsBuiltinDerivedType(derived, "c_ptr") ||
1316       IsBuiltinDerivedType(derived, "c_funptr");
1317 }
1318 
1319 bool IsTeamType(const DerivedTypeSpec *derived) {
1320   return IsBuiltinDerivedType(derived, "team_type");
1321 }
1322 
1323 bool IsBadCoarrayType(const DerivedTypeSpec *derived) {
1324   return IsTeamType(derived) || IsIsoCType(derived);
1325 }
1326 
1327 bool IsEventTypeOrLockType(const DerivedTypeSpec *derivedTypeSpec) {
1328   return IsBuiltinDerivedType(derivedTypeSpec, "event_type") ||
1329       IsBuiltinDerivedType(derivedTypeSpec, "lock_type");
1330 }
1331 
1332 int CountLenParameters(const DerivedTypeSpec &type) {
1333   return std::count_if(type.parameters().begin(), type.parameters().end(),
1334       [](const auto &pair) { return pair.second.isLen(); });
1335 }
1336 
1337 int CountNonConstantLenParameters(const DerivedTypeSpec &type) {
1338   return std::count_if(
1339       type.parameters().begin(), type.parameters().end(), [](const auto &pair) {
1340         if (!pair.second.isLen()) {
1341           return false;
1342         } else if (const auto &expr{pair.second.GetExplicit()}) {
1343           return !IsConstantExpr(*expr);
1344         } else {
1345           return true;
1346         }
1347       });
1348 }
1349 
1350 // Are the type parameters of type1 compile-time compatible with the
1351 // corresponding kind type parameters of type2?  Return true if all constant
1352 // valued parameters are equal.
1353 // Used to check assignment statements and argument passing.  See 15.5.2.4(4)
1354 bool AreTypeParamCompatible(const semantics::DerivedTypeSpec &type1,
1355     const semantics::DerivedTypeSpec &type2) {
1356   for (const auto &[name, param1] : type1.parameters()) {
1357     if (semantics::MaybeIntExpr paramExpr1{param1.GetExplicit()}) {
1358       if (IsConstantExpr(*paramExpr1)) {
1359         const semantics::ParamValue *param2{type2.FindParameter(name)};
1360         if (param2) {
1361           if (semantics::MaybeIntExpr paramExpr2{param2->GetExplicit()}) {
1362             if (IsConstantExpr(*paramExpr2)) {
1363               if (ToInt64(*paramExpr1) != ToInt64(*paramExpr2)) {
1364                 return false;
1365               }
1366             }
1367           }
1368         }
1369       }
1370     }
1371   }
1372   return true;
1373 }
1374 
1375 const Symbol &GetUsedModule(const UseDetails &details) {
1376   return DEREF(details.symbol().owner().symbol());
1377 }
1378 
1379 static const Symbol *FindFunctionResult(
1380     const Symbol &original, UnorderedSymbolSet &seen) {
1381   const Symbol &root{GetAssociationRoot(original)};
1382   ;
1383   if (!seen.insert(root).second) {
1384     return nullptr; // don't loop
1385   }
1386   return std::visit(
1387       common::visitors{[](const SubprogramDetails &subp) {
1388                          return subp.isFunction() ? &subp.result() : nullptr;
1389                        },
1390           [&](const ProcEntityDetails &proc) {
1391             const Symbol *iface{proc.interface().symbol()};
1392             return iface ? FindFunctionResult(*iface, seen) : nullptr;
1393           },
1394           [&](const ProcBindingDetails &binding) {
1395             return FindFunctionResult(binding.symbol(), seen);
1396           },
1397           [](const auto &) -> const Symbol * { return nullptr; }},
1398       root.details());
1399 }
1400 
1401 const Symbol *FindFunctionResult(const Symbol &symbol) {
1402   UnorderedSymbolSet seen;
1403   return FindFunctionResult(symbol, seen);
1404 }
1405 
1406 // These are here in Evaluate/tools.cpp so that Evaluate can use
1407 // them; they cannot be defined in symbol.h due to the dependence
1408 // on Scope.
1409 
1410 bool SymbolSourcePositionCompare::operator()(
1411     const SymbolRef &x, const SymbolRef &y) const {
1412   return x->GetSemanticsContext().allCookedSources().Precedes(
1413       x->name(), y->name());
1414 }
1415 bool SymbolSourcePositionCompare::operator()(
1416     const MutableSymbolRef &x, const MutableSymbolRef &y) const {
1417   return x->GetSemanticsContext().allCookedSources().Precedes(
1418       x->name(), y->name());
1419 }
1420 
1421 SemanticsContext &Symbol::GetSemanticsContext() const {
1422   return DEREF(owner_).context();
1423 }
1424 
1425 } // namespace Fortran::semantics
1426