xref: /llvm-project/flang/lib/Evaluate/tools.cpp (revision 7b8012338745ab16a88d78b3772d21dd6f87224b)
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 common::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 common::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, bool intoComplexPart) {
64   return ExtractDataRef(arg.UnwrapExpr(), intoSubstring, intoComplexPart);
65 }
66 
67 std::optional<DataRef> ExtractSubstringBase(const Substring &substring) {
68   return common::visit(
69       common::visitors{
70           [&](const DataRef &x) -> std::optional<DataRef> { return x; },
71           [&](const StaticDataObject::Pointer &) -> std::optional<DataRef> {
72             return std::nullopt;
73           },
74       },
75       substring.parent());
76 }
77 
78 // IsVariable()
79 
80 auto IsVariableHelper::operator()(const Symbol &symbol) const -> Result {
81   // ASSOCIATE(x => expr) -- x counts as a variable, but undefinable
82   const Symbol &ultimate{symbol.GetUltimate()};
83   return !IsNamedConstant(ultimate) &&
84       (ultimate.has<semantics::ObjectEntityDetails>() ||
85           ultimate.has<semantics::AssocEntityDetails>());
86 }
87 auto IsVariableHelper::operator()(const Component &x) const -> Result {
88   const Symbol &comp{x.GetLastSymbol()};
89   return (*this)(comp) && (IsPointer(comp) || (*this)(x.base()));
90 }
91 auto IsVariableHelper::operator()(const ArrayRef &x) const -> Result {
92   return (*this)(x.base());
93 }
94 auto IsVariableHelper::operator()(const Substring &x) const -> Result {
95   return (*this)(x.GetBaseObject());
96 }
97 auto IsVariableHelper::operator()(const ProcedureDesignator &x) const
98     -> Result {
99   if (const Symbol * symbol{x.GetSymbol()}) {
100     const Symbol *result{FindFunctionResult(*symbol)};
101     return result && IsPointer(*result) && !IsProcedurePointer(*result);
102   }
103   return false;
104 }
105 
106 // Conversions of COMPLEX component expressions to REAL.
107 ConvertRealOperandsResult ConvertRealOperands(
108     parser::ContextualMessages &messages, Expr<SomeType> &&x,
109     Expr<SomeType> &&y, int defaultRealKind) {
110   return common::visit(
111       common::visitors{
112           [&](Expr<SomeInteger> &&ix,
113               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
114             // Can happen in a CMPLX() constructor.  Per F'2018,
115             // both integer operands are converted to default REAL.
116             return {AsSameKindExprs<TypeCategory::Real>(
117                 ConvertToKind<TypeCategory::Real>(
118                     defaultRealKind, std::move(ix)),
119                 ConvertToKind<TypeCategory::Real>(
120                     defaultRealKind, std::move(iy)))};
121           },
122           [&](Expr<SomeInteger> &&ix,
123               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
124             return {AsSameKindExprs<TypeCategory::Real>(
125                 ConvertTo(ry, std::move(ix)), std::move(ry))};
126           },
127           [&](Expr<SomeReal> &&rx,
128               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
129             return {AsSameKindExprs<TypeCategory::Real>(
130                 std::move(rx), ConvertTo(rx, std::move(iy)))};
131           },
132           [&](Expr<SomeReal> &&rx,
133               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
134             return {AsSameKindExprs<TypeCategory::Real>(
135                 std::move(rx), std::move(ry))};
136           },
137           [&](Expr<SomeInteger> &&ix,
138               BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
139             return {AsSameKindExprs<TypeCategory::Real>(
140                 ConvertToKind<TypeCategory::Real>(
141                     defaultRealKind, std::move(ix)),
142                 ConvertToKind<TypeCategory::Real>(
143                     defaultRealKind, std::move(by)))};
144           },
145           [&](BOZLiteralConstant &&bx,
146               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
147             return {AsSameKindExprs<TypeCategory::Real>(
148                 ConvertToKind<TypeCategory::Real>(
149                     defaultRealKind, std::move(bx)),
150                 ConvertToKind<TypeCategory::Real>(
151                     defaultRealKind, std::move(iy)))};
152           },
153           [&](Expr<SomeReal> &&rx,
154               BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
155             return {AsSameKindExprs<TypeCategory::Real>(
156                 std::move(rx), ConvertTo(rx, std::move(by)))};
157           },
158           [&](BOZLiteralConstant &&bx,
159               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
160             return {AsSameKindExprs<TypeCategory::Real>(
161                 ConvertTo(ry, std::move(bx)), std::move(ry))};
162           },
163           [&](auto &&, auto &&) -> ConvertRealOperandsResult { // C718
164             messages.Say("operands must be INTEGER or REAL"_err_en_US);
165             return std::nullopt;
166           },
167       },
168       std::move(x.u), std::move(y.u));
169 }
170 
171 // Helpers for NumericOperation and its subroutines below.
172 static std::optional<Expr<SomeType>> NoExpr() { return std::nullopt; }
173 
174 template <TypeCategory CAT>
175 std::optional<Expr<SomeType>> Package(Expr<SomeKind<CAT>> &&catExpr) {
176   return {AsGenericExpr(std::move(catExpr))};
177 }
178 template <TypeCategory CAT>
179 std::optional<Expr<SomeType>> Package(
180     std::optional<Expr<SomeKind<CAT>>> &&catExpr) {
181   if (catExpr) {
182     return {AsGenericExpr(std::move(*catExpr))};
183   } else {
184     return std::nullopt;
185   }
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(common::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 template <int KIND>
209 Expr<SomeComplex> MakeComplex(Expr<Type<TypeCategory::Real, KIND>> &&re,
210     Expr<Type<TypeCategory::Real, KIND>> &&im) {
211   return AsCategoryExpr(ComplexConstructor<KIND>{std::move(re), std::move(im)});
212 }
213 
214 std::optional<Expr<SomeComplex>> ConstructComplex(
215     parser::ContextualMessages &messages, Expr<SomeType> &&real,
216     Expr<SomeType> &&imaginary, int defaultRealKind) {
217   if (auto converted{ConvertRealOperands(
218           messages, std::move(real), std::move(imaginary), defaultRealKind)}) {
219     return {common::visit(
220         [](auto &&pair) {
221           return MakeComplex(std::move(pair[0]), std::move(pair[1]));
222         },
223         std::move(*converted))};
224   }
225   return std::nullopt;
226 }
227 
228 std::optional<Expr<SomeComplex>> ConstructComplex(
229     parser::ContextualMessages &messages, std::optional<Expr<SomeType>> &&real,
230     std::optional<Expr<SomeType>> &&imaginary, int defaultRealKind) {
231   if (auto parts{common::AllPresent(std::move(real), std::move(imaginary))}) {
232     return ConstructComplex(messages, std::get<0>(std::move(*parts)),
233         std::get<1>(std::move(*parts)), defaultRealKind);
234   }
235   return std::nullopt;
236 }
237 
238 // Extracts the real or imaginary part of the result of a COMPLEX
239 // expression, when that expression is simple enough to be duplicated.
240 template <bool GET_IMAGINARY> struct ComplexPartExtractor {
241   template <typename A> static std::optional<Expr<SomeReal>> Get(const A &) {
242     return std::nullopt;
243   }
244 
245   template <int KIND>
246   static std::optional<Expr<SomeReal>> Get(
247       const Parentheses<Type<TypeCategory::Complex, KIND>> &kz) {
248     if (auto x{Get(kz.left())}) {
249       return AsGenericExpr(AsSpecificExpr(
250           Parentheses<Type<TypeCategory::Real, KIND>>{std::move(*x)}));
251     } else {
252       return std::nullopt;
253     }
254   }
255 
256   template <int KIND>
257   static std::optional<Expr<SomeReal>> Get(
258       const Negate<Type<TypeCategory::Complex, KIND>> &kz) {
259     if (auto x{Get(kz.left())}) {
260       return AsGenericExpr(AsSpecificExpr(
261           Negate<Type<TypeCategory::Real, KIND>>{std::move(*x)}));
262     } else {
263       return std::nullopt;
264     }
265   }
266 
267   template <int KIND>
268   static std::optional<Expr<SomeReal>> Get(
269       const Convert<Type<TypeCategory::Complex, KIND>, TypeCategory::Complex>
270           &kz) {
271     if (auto x{Get(kz.left())}) {
272       return AsGenericExpr(AsSpecificExpr(
273           Convert<Type<TypeCategory::Real, KIND>, TypeCategory::Real>{
274               AsGenericExpr(std::move(*x))}));
275     } else {
276       return std::nullopt;
277     }
278   }
279 
280   template <int KIND>
281   static std::optional<Expr<SomeReal>> Get(const ComplexConstructor<KIND> &kz) {
282     return GET_IMAGINARY ? Get(kz.right()) : Get(kz.left());
283   }
284 
285   template <int KIND>
286   static std::optional<Expr<SomeReal>> Get(
287       const Constant<Type<TypeCategory::Complex, KIND>> &kz) {
288     if (auto cz{kz.GetScalarValue()}) {
289       return AsGenericExpr(
290           AsSpecificExpr(GET_IMAGINARY ? cz->AIMAG() : cz->REAL()));
291     } else {
292       return std::nullopt;
293     }
294   }
295 
296   template <int KIND>
297   static std::optional<Expr<SomeReal>> Get(
298       const Designator<Type<TypeCategory::Complex, KIND>> &kz) {
299     if (const auto *symbolRef{std::get_if<SymbolRef>(&kz.u)}) {
300       return AsGenericExpr(AsSpecificExpr(
301           Designator<Type<TypeCategory::Complex, KIND>>{ComplexPart{
302               DataRef{*symbolRef},
303               GET_IMAGINARY ? ComplexPart::Part::IM : ComplexPart::Part::RE}}));
304     } else {
305       return std::nullopt;
306     }
307   }
308 
309   template <int KIND>
310   static std::optional<Expr<SomeReal>> Get(
311       const Expr<Type<TypeCategory::Complex, KIND>> &kz) {
312     return Get(kz.u);
313   }
314 
315   static std::optional<Expr<SomeReal>> Get(const Expr<SomeComplex> &z) {
316     return Get(z.u);
317   }
318 };
319 
320 // Convert REAL to COMPLEX of the same kind. Preserving the real operand kind
321 // and then applying complex operand promotion rules allows the result to have
322 // the highest precision of REAL and COMPLEX operands as required by Fortran
323 // 2018 10.9.1.3.
324 Expr<SomeComplex> PromoteRealToComplex(Expr<SomeReal> &&someX) {
325   return common::visit(
326       [](auto &&x) {
327         using RT = ResultType<decltype(x)>;
328         return AsCategoryExpr(ComplexConstructor<RT::kind>{
329             std::move(x), AsExpr(Constant<RT>{Scalar<RT>{}})});
330       },
331       std::move(someX.u));
332 }
333 
334 // Handle mixed COMPLEX+REAL (or INTEGER) operations in a better way
335 // than just converting the second operand to COMPLEX and performing the
336 // corresponding COMPLEX+COMPLEX operation.
337 template <template <typename> class OPR, TypeCategory RCAT>
338 std::optional<Expr<SomeType>> MixedComplexLeft(
339     parser::ContextualMessages &messages, const Expr<SomeComplex> &zx,
340     const Expr<SomeKind<RCAT>> &iry, [[maybe_unused]] int defaultRealKind) {
341   if constexpr (RCAT == TypeCategory::Integer &&
342       std::is_same_v<OPR<LargestReal>, Power<LargestReal>>) {
343     // COMPLEX**INTEGER is a special case that doesn't convert the exponent.
344     return Package(common::visit(
345         [&](const auto &zxk) {
346           using Ty = ResultType<decltype(zxk)>;
347           return AsCategoryExpr(AsExpr(
348               RealToIntPower<Ty>{common::Clone(zxk), common::Clone(iry)}));
349         },
350         zx.u));
351   }
352   std::optional<Expr<SomeReal>> zr{ComplexPartExtractor<false>{}.Get(zx)};
353   std::optional<Expr<SomeReal>> zi{ComplexPartExtractor<true>{}.Get(zx)};
354   if (!zr || !zi) {
355   } else if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>> ||
356       std::is_same_v<OPR<LargestReal>, Subtract<LargestReal>>) {
357     // (a,b) + x -> (a+x, b)
358     // (a,b) - x -> (a-x, b)
359     if (std::optional<Expr<SomeType>> rr{
360             NumericOperation<OPR>(messages, AsGenericExpr(std::move(*zr)),
361                 AsGenericExpr(common::Clone(iry)), defaultRealKind)}) {
362       return Package(ConstructComplex(messages, std::move(*rr),
363           AsGenericExpr(std::move(*zi)), defaultRealKind));
364     }
365   } else if constexpr (allowOperandDuplication &&
366       (std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>> ||
367           std::is_same_v<OPR<LargestReal>, Divide<LargestReal>>)) {
368     // (a,b) * x -> (a*x, b*x)
369     // (a,b) / x -> (a/x, b/x)
370     auto copy{iry};
371     auto rr{NumericOperation<OPR>(messages, AsGenericExpr(std::move(*zr)),
372         AsGenericExpr(common::Clone(iry)), defaultRealKind)};
373     auto ri{NumericOperation<OPR>(messages, AsGenericExpr(std::move(*zi)),
374         AsGenericExpr(std::move(copy)), defaultRealKind)};
375     if (auto parts{common::AllPresent(std::move(rr), std::move(ri))}) {
376       return Package(ConstructComplex(messages, std::get<0>(std::move(*parts)),
377           std::get<1>(std::move(*parts)), defaultRealKind));
378     }
379   }
380   return std::nullopt;
381 }
382 
383 // Mixed COMPLEX operations with the COMPLEX operand on the right.
384 //  x + (a,b) -> (x+a, b)
385 //  x - (a,b) -> (x-a, -b)
386 //  x * (a,b) -> (x*a, x*b)
387 //  x / (a,b) -> (x,0) / (a,b)   (and **)
388 template <template <typename> class OPR, TypeCategory LCAT>
389 std::optional<Expr<SomeType>> MixedComplexRight(
390     parser::ContextualMessages &messages, const Expr<SomeKind<LCAT>> &irx,
391     const Expr<SomeComplex> &zy, [[maybe_unused]] int defaultRealKind) {
392   if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>>) {
393     // x + (a,b) -> (a,b) + x -> (a+x, b)
394     return MixedComplexLeft<OPR, LCAT>(messages, zy, irx, defaultRealKind);
395   } else if constexpr (allowOperandDuplication &&
396       std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>>) {
397     // x * (a,b) -> (a,b) * x -> (a*x, b*x)
398     return MixedComplexLeft<OPR, LCAT>(messages, zy, irx, defaultRealKind);
399   } else if constexpr (std::is_same_v<OPR<LargestReal>,
400                            Subtract<LargestReal>>) {
401     // x - (a,b) -> (x-a, -b)
402     std::optional<Expr<SomeReal>> zr{ComplexPartExtractor<false>{}.Get(zy)};
403     std::optional<Expr<SomeReal>> zi{ComplexPartExtractor<true>{}.Get(zy)};
404     if (zr && zi) {
405       if (std::optional<Expr<SomeType>> rr{NumericOperation<Subtract>(messages,
406               AsGenericExpr(common::Clone(irx)), AsGenericExpr(std::move(*zr)),
407               defaultRealKind)}) {
408         return Package(ConstructComplex(messages, std::move(*rr),
409             AsGenericExpr(-std::move(*zi)), defaultRealKind));
410       }
411     }
412   }
413   return std::nullopt;
414 }
415 
416 // Promotes REAL(rk) and COMPLEX(zk) operands COMPLEX(max(rk,zk))
417 // then combine them with an operator.
418 template <template <typename> class OPR, TypeCategory XCAT, TypeCategory YCAT>
419 Expr<SomeComplex> PromoteMixedComplexReal(
420     Expr<SomeKind<XCAT>> &&x, Expr<SomeKind<YCAT>> &&y) {
421   static_assert(XCAT == TypeCategory::Complex || YCAT == TypeCategory::Complex);
422   static_assert(XCAT == TypeCategory::Real || YCAT == TypeCategory::Real);
423   return common::visit(
424       [&](const auto &kx, const auto &ky) {
425         constexpr int maxKind{std::max(
426             ResultType<decltype(kx)>::kind, ResultType<decltype(ky)>::kind)};
427         using ZTy = Type<TypeCategory::Complex, maxKind>;
428         return Expr<SomeComplex>{
429             Expr<ZTy>{OPR<ZTy>{ConvertToType<ZTy>(std::move(x)),
430                 ConvertToType<ZTy>(std::move(y))}}};
431       },
432       x.u, y.u);
433 }
434 
435 // N.B. When a "typeless" BOZ literal constant appears as one (not both!) of
436 // the operands to a dyadic operation where one is permitted, it assumes the
437 // type and kind of the other operand.
438 template <template <typename> class OPR>
439 std::optional<Expr<SomeType>> NumericOperation(
440     parser::ContextualMessages &messages, Expr<SomeType> &&x,
441     Expr<SomeType> &&y, int defaultRealKind) {
442   return common::visit(
443       common::visitors{
444           [](Expr<SomeInteger> &&ix, Expr<SomeInteger> &&iy) {
445             return Package(PromoteAndCombine<OPR, TypeCategory::Integer>(
446                 std::move(ix), std::move(iy)));
447           },
448           [](Expr<SomeReal> &&rx, Expr<SomeReal> &&ry) {
449             return Package(PromoteAndCombine<OPR, TypeCategory::Real>(
450                 std::move(rx), std::move(ry)));
451           },
452           // Mixed REAL/INTEGER operations
453           [](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
454             return MixedRealLeft<OPR>(std::move(rx), std::move(iy));
455           },
456           [](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
457             return Package(common::visit(
458                 [&](auto &&ryk) -> Expr<SomeReal> {
459                   using resultType = ResultType<decltype(ryk)>;
460                   return AsCategoryExpr(
461                       OPR<resultType>{ConvertToType<resultType>(std::move(ix)),
462                           std::move(ryk)});
463                 },
464                 std::move(ry.u)));
465           },
466           // Homogeneous and mixed COMPLEX operations
467           [](Expr<SomeComplex> &&zx, Expr<SomeComplex> &&zy) {
468             return Package(PromoteAndCombine<OPR, TypeCategory::Complex>(
469                 std::move(zx), std::move(zy)));
470           },
471           [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) {
472             if (auto result{
473                     MixedComplexLeft<OPR>(messages, zx, iy, defaultRealKind)}) {
474               return result;
475             } else {
476               return Package(PromoteAndCombine<OPR, TypeCategory::Complex>(
477                   std::move(zx), ConvertTo(zx, std::move(iy))));
478             }
479           },
480           [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) {
481             if (auto result{
482                     MixedComplexLeft<OPR>(messages, zx, ry, defaultRealKind)}) {
483               return result;
484             } else {
485               return Package(
486                   PromoteMixedComplexReal<OPR>(std::move(zx), std::move(ry)));
487             }
488           },
489           [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) {
490             if (auto result{MixedComplexRight<OPR>(
491                     messages, ix, zy, defaultRealKind)}) {
492               return result;
493             } else {
494               return Package(PromoteAndCombine<OPR, TypeCategory::Complex>(
495                   ConvertTo(zy, std::move(ix)), std::move(zy)));
496             }
497           },
498           [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) {
499             if (auto result{MixedComplexRight<OPR>(
500                     messages, rx, zy, defaultRealKind)}) {
501               return result;
502             } else {
503               return Package(
504                   PromoteMixedComplexReal<OPR>(std::move(rx), std::move(zy)));
505             }
506           },
507           // Operations with one typeless operand
508           [&](BOZLiteralConstant &&bx, Expr<SomeInteger> &&iy) {
509             return NumericOperation<OPR>(messages,
510                 AsGenericExpr(ConvertTo(iy, std::move(bx))), std::move(y),
511                 defaultRealKind);
512           },
513           [&](BOZLiteralConstant &&bx, Expr<SomeReal> &&ry) {
514             return NumericOperation<OPR>(messages,
515                 AsGenericExpr(ConvertTo(ry, std::move(bx))), std::move(y),
516                 defaultRealKind);
517           },
518           [&](Expr<SomeInteger> &&ix, BOZLiteralConstant &&by) {
519             return NumericOperation<OPR>(messages, std::move(x),
520                 AsGenericExpr(ConvertTo(ix, std::move(by))), defaultRealKind);
521           },
522           [&](Expr<SomeReal> &&rx, BOZLiteralConstant &&by) {
523             return NumericOperation<OPR>(messages, std::move(x),
524                 AsGenericExpr(ConvertTo(rx, std::move(by))), defaultRealKind);
525           },
526           // Default case
527           [&](auto &&, auto &&) {
528             messages.Say("non-numeric operands to numeric operation"_err_en_US);
529             return NoExpr();
530           },
531       },
532       std::move(x.u), std::move(y.u));
533 }
534 
535 template std::optional<Expr<SomeType>> NumericOperation<Power>(
536     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
537     int defaultRealKind);
538 template std::optional<Expr<SomeType>> NumericOperation<Multiply>(
539     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
540     int defaultRealKind);
541 template std::optional<Expr<SomeType>> NumericOperation<Divide>(
542     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
543     int defaultRealKind);
544 template std::optional<Expr<SomeType>> NumericOperation<Add>(
545     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
546     int defaultRealKind);
547 template std::optional<Expr<SomeType>> NumericOperation<Subtract>(
548     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
549     int defaultRealKind);
550 
551 std::optional<Expr<SomeType>> Negation(
552     parser::ContextualMessages &messages, Expr<SomeType> &&x) {
553   return common::visit(
554       common::visitors{
555           [&](BOZLiteralConstant &&) {
556             messages.Say("BOZ literal cannot be negated"_err_en_US);
557             return NoExpr();
558           },
559           [&](NullPointer &&) {
560             messages.Say("NULL() cannot be negated"_err_en_US);
561             return NoExpr();
562           },
563           [&](ProcedureDesignator &&) {
564             messages.Say("Subroutine cannot be negated"_err_en_US);
565             return NoExpr();
566           },
567           [&](ProcedureRef &&) {
568             messages.Say("Pointer to subroutine cannot be negated"_err_en_US);
569             return NoExpr();
570           },
571           [&](Expr<SomeInteger> &&x) { return Package(-std::move(x)); },
572           [&](Expr<SomeReal> &&x) { return Package(-std::move(x)); },
573           [&](Expr<SomeComplex> &&x) { return Package(-std::move(x)); },
574           [&](Expr<SomeCharacter> &&) {
575             messages.Say("CHARACTER cannot be negated"_err_en_US);
576             return NoExpr();
577           },
578           [&](Expr<SomeLogical> &&) {
579             messages.Say("LOGICAL cannot be negated"_err_en_US);
580             return NoExpr();
581           },
582           [&](Expr<SomeDerived> &&) {
583             messages.Say("Operand cannot be negated"_err_en_US);
584             return NoExpr();
585           },
586       },
587       std::move(x.u));
588 }
589 
590 Expr<SomeLogical> LogicalNegation(Expr<SomeLogical> &&x) {
591   return common::visit(
592       [](auto &&xk) { return AsCategoryExpr(LogicalNegation(std::move(xk))); },
593       std::move(x.u));
594 }
595 
596 template <TypeCategory CAT>
597 Expr<LogicalResult> PromoteAndRelate(
598     RelationalOperator opr, Expr<SomeKind<CAT>> &&x, Expr<SomeKind<CAT>> &&y) {
599   return common::visit(
600       [=](auto &&xy) {
601         return PackageRelation(opr, std::move(xy[0]), std::move(xy[1]));
602       },
603       AsSameKindExprs(std::move(x), std::move(y)));
604 }
605 
606 std::optional<Expr<LogicalResult>> Relate(parser::ContextualMessages &messages,
607     RelationalOperator opr, Expr<SomeType> &&x, Expr<SomeType> &&y) {
608   return common::visit(
609       common::visitors{
610           [=](Expr<SomeInteger> &&ix,
611               Expr<SomeInteger> &&iy) -> std::optional<Expr<LogicalResult>> {
612             return PromoteAndRelate(opr, std::move(ix), std::move(iy));
613           },
614           [=](Expr<SomeReal> &&rx,
615               Expr<SomeReal> &&ry) -> std::optional<Expr<LogicalResult>> {
616             return PromoteAndRelate(opr, std::move(rx), std::move(ry));
617           },
618           [&](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
619             return Relate(messages, opr, std::move(x),
620                 AsGenericExpr(ConvertTo(rx, std::move(iy))));
621           },
622           [&](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
623             return Relate(messages, opr,
624                 AsGenericExpr(ConvertTo(ry, std::move(ix))), std::move(y));
625           },
626           [&](Expr<SomeComplex> &&zx,
627               Expr<SomeComplex> &&zy) -> std::optional<Expr<LogicalResult>> {
628             if (opr == RelationalOperator::EQ ||
629                 opr == RelationalOperator::NE) {
630               return PromoteAndRelate(opr, std::move(zx), std::move(zy));
631             } else {
632               messages.Say(
633                   "COMPLEX data may be compared only for equality"_err_en_US);
634               return std::nullopt;
635             }
636           },
637           [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) {
638             return Relate(messages, opr, std::move(x),
639                 AsGenericExpr(ConvertTo(zx, std::move(iy))));
640           },
641           [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) {
642             return Relate(messages, opr, std::move(x),
643                 AsGenericExpr(ConvertTo(zx, std::move(ry))));
644           },
645           [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) {
646             return Relate(messages, opr,
647                 AsGenericExpr(ConvertTo(zy, std::move(ix))), std::move(y));
648           },
649           [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) {
650             return Relate(messages, opr,
651                 AsGenericExpr(ConvertTo(zy, std::move(rx))), std::move(y));
652           },
653           [&](Expr<SomeCharacter> &&cx, Expr<SomeCharacter> &&cy) {
654             return common::visit(
655                 [&](auto &&cxk,
656                     auto &&cyk) -> std::optional<Expr<LogicalResult>> {
657                   using Ty = ResultType<decltype(cxk)>;
658                   if constexpr (std::is_same_v<Ty, ResultType<decltype(cyk)>>) {
659                     return PackageRelation(opr, std::move(cxk), std::move(cyk));
660                   } else {
661                     messages.Say(
662                         "CHARACTER operands do not have same KIND"_err_en_US);
663                     return std::nullopt;
664                   }
665                 },
666                 std::move(cx.u), std::move(cy.u));
667           },
668           // Default case
669           [&](auto &&, auto &&) {
670             DIE("invalid types for relational operator");
671             return std::optional<Expr<LogicalResult>>{};
672           },
673       },
674       std::move(x.u), std::move(y.u));
675 }
676 
677 Expr<SomeLogical> BinaryLogicalOperation(
678     LogicalOperator opr, Expr<SomeLogical> &&x, Expr<SomeLogical> &&y) {
679   CHECK(opr != LogicalOperator::Not);
680   return common::visit(
681       [=](auto &&xy) {
682         using Ty = ResultType<decltype(xy[0])>;
683         return Expr<SomeLogical>{BinaryLogicalOperation<Ty::kind>(
684             opr, std::move(xy[0]), std::move(xy[1]))};
685       },
686       AsSameKindExprs(std::move(x), std::move(y)));
687 }
688 
689 template <TypeCategory TO>
690 std::optional<Expr<SomeType>> ConvertToNumeric(int kind, Expr<SomeType> &&x) {
691   static_assert(common::IsNumericTypeCategory(TO));
692   return common::visit(
693       [=](auto &&cx) -> std::optional<Expr<SomeType>> {
694         using cxType = std::decay_t<decltype(cx)>;
695         if constexpr (!common::HasMember<cxType, TypelessExpression>) {
696           if constexpr (IsNumericTypeCategory(ResultType<cxType>::category)) {
697             return Expr<SomeType>{ConvertToKind<TO>(kind, std::move(cx))};
698           }
699         }
700         return std::nullopt;
701       },
702       std::move(x.u));
703 }
704 
705 std::optional<Expr<SomeType>> ConvertToType(
706     const DynamicType &type, Expr<SomeType> &&x) {
707   if (type.IsTypelessIntrinsicArgument()) {
708     return std::nullopt;
709   }
710   switch (type.category()) {
711   case TypeCategory::Integer:
712     if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
713       // Extension to C7109: allow BOZ literals to appear in integer contexts
714       // when the type is unambiguous.
715       return Expr<SomeType>{
716           ConvertToKind<TypeCategory::Integer>(type.kind(), std::move(*boz))};
717     }
718     return ConvertToNumeric<TypeCategory::Integer>(type.kind(), std::move(x));
719   case TypeCategory::Real:
720     if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
721       return Expr<SomeType>{
722           ConvertToKind<TypeCategory::Real>(type.kind(), std::move(*boz))};
723     }
724     return ConvertToNumeric<TypeCategory::Real>(type.kind(), std::move(x));
725   case TypeCategory::Complex:
726     return ConvertToNumeric<TypeCategory::Complex>(type.kind(), std::move(x));
727   case TypeCategory::Character:
728     if (auto *cx{UnwrapExpr<Expr<SomeCharacter>>(x)}) {
729       auto converted{
730           ConvertToKind<TypeCategory::Character>(type.kind(), std::move(*cx))};
731       if (auto length{type.GetCharLength()}) {
732         converted = common::visit(
733             [&](auto &&x) {
734               using CharacterType = ResultType<decltype(x)>;
735               return Expr<SomeCharacter>{
736                   Expr<CharacterType>{SetLength<CharacterType::kind>{
737                       std::move(x), std::move(*length)}}};
738             },
739             std::move(converted.u));
740       }
741       return Expr<SomeType>{std::move(converted)};
742     }
743     break;
744   case TypeCategory::Logical:
745     if (auto *cx{UnwrapExpr<Expr<SomeLogical>>(x)}) {
746       return Expr<SomeType>{
747           ConvertToKind<TypeCategory::Logical>(type.kind(), std::move(*cx))};
748     }
749     break;
750   case TypeCategory::Derived:
751     if (auto fromType{x.GetType()}) {
752       if (type.IsTkCompatibleWith(*fromType)) {
753         // "x" could be assigned or passed to "type", or appear in a
754         // structure constructor as a value for a component with "type"
755         return std::move(x);
756       }
757     }
758     break;
759   }
760   return std::nullopt;
761 }
762 
763 std::optional<Expr<SomeType>> ConvertToType(
764     const DynamicType &to, std::optional<Expr<SomeType>> &&x) {
765   if (x) {
766     return ConvertToType(to, std::move(*x));
767   } else {
768     return std::nullopt;
769   }
770 }
771 
772 std::optional<Expr<SomeType>> ConvertToType(
773     const Symbol &symbol, Expr<SomeType> &&x) {
774   if (auto symType{DynamicType::From(symbol)}) {
775     return ConvertToType(*symType, std::move(x));
776   }
777   return std::nullopt;
778 }
779 
780 std::optional<Expr<SomeType>> ConvertToType(
781     const Symbol &to, std::optional<Expr<SomeType>> &&x) {
782   if (x) {
783     return ConvertToType(to, std::move(*x));
784   } else {
785     return std::nullopt;
786   }
787 }
788 
789 bool IsAssumedRank(const Symbol &original) {
790   if (const auto *assoc{original.detailsIf<semantics::AssocEntityDetails>()}) {
791     if (assoc->rank()) {
792       return false; // in RANK(n) or RANK(*)
793     } else if (assoc->IsAssumedRank()) {
794       return true; // RANK DEFAULT
795     }
796   }
797   const Symbol &symbol{semantics::ResolveAssociations(original)};
798   const auto *object{symbol.detailsIf<semantics::ObjectEntityDetails>()};
799   return object && object->IsAssumedRank();
800 }
801 
802 bool IsAssumedRank(const ActualArgument &arg) {
803   if (const auto *expr{arg.UnwrapExpr()}) {
804     return IsAssumedRank(*expr);
805   } else {
806     const Symbol *assumedTypeDummy{arg.GetAssumedTypeDummy()};
807     CHECK(assumedTypeDummy);
808     return IsAssumedRank(*assumedTypeDummy);
809   }
810 }
811 
812 bool IsCoarray(const ActualArgument &arg) {
813   const auto *expr{arg.UnwrapExpr()};
814   return expr && IsCoarray(*expr);
815 }
816 
817 bool IsCoarray(const Symbol &symbol) {
818   return GetAssociationRoot(symbol).Corank() > 0;
819 }
820 
821 bool IsProcedure(const Expr<SomeType> &expr) {
822   return std::holds_alternative<ProcedureDesignator>(expr.u);
823 }
824 bool IsFunction(const Expr<SomeType> &expr) {
825   const auto *designator{std::get_if<ProcedureDesignator>(&expr.u)};
826   return designator && designator->GetType().has_value();
827 }
828 
829 bool IsPointer(const Expr<SomeType> &expr) {
830   return IsObjectPointer(expr) || IsProcedurePointer(expr);
831 }
832 
833 bool IsProcedurePointer(const Expr<SomeType> &expr) {
834   if (IsNullProcedurePointer(expr)) {
835     return true;
836   } else if (const auto *funcRef{UnwrapProcedureRef(expr)}) {
837     if (const Symbol * proc{funcRef->proc().GetSymbol()}) {
838       const Symbol *result{FindFunctionResult(*proc)};
839       return result && IsProcedurePointer(*result);
840     } else {
841       return false;
842     }
843   } else if (const auto *proc{std::get_if<ProcedureDesignator>(&expr.u)}) {
844     return IsProcedurePointer(proc->GetSymbol());
845   } else {
846     return false;
847   }
848 }
849 
850 bool IsProcedurePointerTarget(const Expr<SomeType> &expr) {
851   return common::visit(common::visitors{
852                            [](const NullPointer &) { return true; },
853                            [](const ProcedureDesignator &) { return true; },
854                            [](const ProcedureRef &) { return true; },
855                            [&](const auto &) {
856                              const Symbol *last{GetLastSymbol(expr)};
857                              return last && IsProcedurePointer(*last);
858                            },
859                        },
860       expr.u);
861 }
862 
863 bool IsObjectPointer(const Expr<SomeType> &expr) {
864   if (IsNullObjectPointer(expr)) {
865     return true;
866   } else if (IsProcedurePointerTarget(expr)) {
867     return false;
868   } else if (const auto *funcRef{UnwrapProcedureRef(expr)}) {
869     return IsVariable(*funcRef);
870   } else if (const Symbol * symbol{UnwrapWholeSymbolOrComponentDataRef(expr)}) {
871     return IsPointer(symbol->GetUltimate());
872   } else {
873     return false;
874   }
875 }
876 
877 // IsNullPointer() & variations
878 
879 template <bool IS_PROC_PTR> struct IsNullPointerHelper {
880   template <typename A> bool operator()(const A &) const { return false; }
881   bool operator()(const ProcedureRef &call) const {
882     if constexpr (IS_PROC_PTR) {
883       const auto *intrinsic{call.proc().GetSpecificIntrinsic()};
884       return intrinsic &&
885           intrinsic->characteristics.value().attrs.test(
886               characteristics::Procedure::Attr::NullPointer);
887     } else {
888       return false;
889     }
890   }
891   template <typename T> bool operator()(const FunctionRef<T> &call) const {
892     if constexpr (IS_PROC_PTR) {
893       return false;
894     } else {
895       const auto *intrinsic{call.proc().GetSpecificIntrinsic()};
896       return intrinsic &&
897           intrinsic->characteristics.value().attrs.test(
898               characteristics::Procedure::Attr::NullPointer);
899     }
900   }
901   template <typename T> bool operator()(const Designator<T> &x) const {
902     if (const auto *component{std::get_if<Component>(&x.u)}) {
903       if (const auto *baseSym{std::get_if<SymbolRef>(&component->base().u)}) {
904         const Symbol &base{**baseSym};
905         if (const auto *object{
906                 base.detailsIf<semantics::ObjectEntityDetails>()}) {
907           // TODO: nested component and array references
908           if (IsNamedConstant(base) && object->init()) {
909             if (auto structCons{
910                     GetScalarConstantValue<SomeDerived>(*object->init())}) {
911               auto iter{structCons->values().find(component->GetLastSymbol())};
912               if (iter != structCons->values().end()) {
913                 return (*this)(iter->second.value());
914               }
915             }
916           }
917         }
918       }
919     }
920     return false;
921   }
922   bool operator()(const NullPointer &) const { return true; }
923   template <typename T> bool operator()(const Parentheses<T> &x) const {
924     return (*this)(x.left());
925   }
926   template <typename T> bool operator()(const Expr<T> &x) const {
927     return common::visit(*this, x.u);
928   }
929 };
930 
931 bool IsNullObjectPointer(const Expr<SomeType> &expr) {
932   return IsNullPointerHelper<false>{}(expr);
933 }
934 
935 bool IsNullProcedurePointer(const Expr<SomeType> &expr) {
936   return IsNullPointerHelper<true>{}(expr);
937 }
938 
939 bool IsNullPointer(const Expr<SomeType> &expr) {
940   return IsNullObjectPointer(expr) || IsNullProcedurePointer(expr);
941 }
942 
943 bool IsBareNullPointer(const Expr<SomeType> *expr) {
944   return expr && std::holds_alternative<NullPointer>(expr->u);
945 }
946 
947 // GetSymbolVector()
948 auto GetSymbolVectorHelper::operator()(const Symbol &x) const -> Result {
949   if (const auto *details{x.detailsIf<semantics::AssocEntityDetails>()}) {
950     if (IsVariable(details->expr()) && !UnwrapProcedureRef(*details->expr())) {
951       // associate(x => variable that is not a pointer returned by a function)
952       return (*this)(details->expr());
953     }
954   }
955   return {x.GetUltimate()};
956 }
957 auto GetSymbolVectorHelper::operator()(const Component &x) const -> Result {
958   Result result{(*this)(x.base())};
959   result.emplace_back(x.GetLastSymbol());
960   return result;
961 }
962 auto GetSymbolVectorHelper::operator()(const ArrayRef &x) const -> Result {
963   return GetSymbolVector(x.base());
964 }
965 auto GetSymbolVectorHelper::operator()(const CoarrayRef &x) const -> Result {
966   return x.base();
967 }
968 
969 const Symbol *GetLastTarget(const SymbolVector &symbols) {
970   auto end{std::crend(symbols)};
971   // N.B. Neither clang nor g++ recognizes "symbols.crbegin()" here.
972   auto iter{std::find_if(std::crbegin(symbols), end, [](const Symbol &x) {
973     return x.attrs().HasAny(
974         {semantics::Attr::POINTER, semantics::Attr::TARGET});
975   })};
976   return iter == end ? nullptr : &**iter;
977 }
978 
979 struct CollectSymbolsHelper
980     : public SetTraverse<CollectSymbolsHelper, semantics::UnorderedSymbolSet> {
981   using Base = SetTraverse<CollectSymbolsHelper, semantics::UnorderedSymbolSet>;
982   CollectSymbolsHelper() : Base{*this} {}
983   using Base::operator();
984   semantics::UnorderedSymbolSet operator()(const Symbol &symbol) const {
985     return {symbol};
986   }
987 };
988 template <typename A> semantics::UnorderedSymbolSet CollectSymbols(const A &x) {
989   return CollectSymbolsHelper{}(x);
990 }
991 template semantics::UnorderedSymbolSet CollectSymbols(const Expr<SomeType> &);
992 template semantics::UnorderedSymbolSet CollectSymbols(
993     const Expr<SomeInteger> &);
994 template semantics::UnorderedSymbolSet CollectSymbols(
995     const Expr<SubscriptInteger> &);
996 
997 // HasVectorSubscript()
998 struct HasVectorSubscriptHelper : public AnyTraverse<HasVectorSubscriptHelper> {
999   using Base = AnyTraverse<HasVectorSubscriptHelper>;
1000   HasVectorSubscriptHelper() : Base{*this} {}
1001   using Base::operator();
1002   bool operator()(const Subscript &ss) const {
1003     return !std::holds_alternative<Triplet>(ss.u) && ss.Rank() > 0;
1004   }
1005   bool operator()(const ProcedureRef &) const {
1006     return false; // don't descend into function call arguments
1007   }
1008 };
1009 
1010 bool HasVectorSubscript(const Expr<SomeType> &expr) {
1011   return HasVectorSubscriptHelper{}(expr);
1012 }
1013 
1014 parser::Message *AttachDeclaration(
1015     parser::Message &message, const Symbol &symbol) {
1016   const Symbol *unhosted{&symbol};
1017   while (
1018       const auto *assoc{unhosted->detailsIf<semantics::HostAssocDetails>()}) {
1019     unhosted = &assoc->symbol();
1020   }
1021   if (const auto *binding{
1022           unhosted->detailsIf<semantics::ProcBindingDetails>()}) {
1023     if (binding->symbol().name() != symbol.name()) {
1024       message.Attach(binding->symbol().name(),
1025           "Procedure '%s' of type '%s' is bound to '%s'"_en_US, symbol.name(),
1026           symbol.owner().GetName().value(), binding->symbol().name());
1027       return &message;
1028     }
1029     unhosted = &binding->symbol();
1030   }
1031   if (const auto *use{symbol.detailsIf<semantics::UseDetails>()}) {
1032     message.Attach(use->location(),
1033         "'%s' is USE-associated with '%s' in module '%s'"_en_US, symbol.name(),
1034         unhosted->name(), GetUsedModule(*use).name());
1035   } else {
1036     message.Attach(
1037         unhosted->name(), "Declaration of '%s'"_en_US, unhosted->name());
1038   }
1039   return &message;
1040 }
1041 
1042 parser::Message *AttachDeclaration(
1043     parser::Message *message, const Symbol &symbol) {
1044   return message ? AttachDeclaration(*message, symbol) : nullptr;
1045 }
1046 
1047 class FindImpureCallHelper
1048     : public AnyTraverse<FindImpureCallHelper, std::optional<std::string>> {
1049   using Result = std::optional<std::string>;
1050   using Base = AnyTraverse<FindImpureCallHelper, Result>;
1051 
1052 public:
1053   explicit FindImpureCallHelper(FoldingContext &c) : Base{*this}, context_{c} {}
1054   using Base::operator();
1055   Result operator()(const ProcedureRef &call) const {
1056     if (auto chars{
1057             characteristics::Procedure::Characterize(call.proc(), context_)}) {
1058       if (chars->attrs.test(characteristics::Procedure::Attr::Pure)) {
1059         return (*this)(call.arguments());
1060       }
1061     }
1062     return call.proc().GetName();
1063   }
1064 
1065 private:
1066   FoldingContext &context_;
1067 };
1068 
1069 std::optional<std::string> FindImpureCall(
1070     FoldingContext &context, const Expr<SomeType> &expr) {
1071   return FindImpureCallHelper{context}(expr);
1072 }
1073 std::optional<std::string> FindImpureCall(
1074     FoldingContext &context, const ProcedureRef &proc) {
1075   return FindImpureCallHelper{context}(proc);
1076 }
1077 
1078 // Common handling for procedure pointer compatibility of left- and right-hand
1079 // sides.  Returns nullopt if they're compatible.  Otherwise, it returns a
1080 // message that needs to be augmented by the names of the left and right sides
1081 // and the content of the "whyNotCompatible" string.
1082 std::optional<parser::MessageFixedText> CheckProcCompatibility(bool isCall,
1083     const std::optional<characteristics::Procedure> &lhsProcedure,
1084     const characteristics::Procedure *rhsProcedure,
1085     const SpecificIntrinsic *specificIntrinsic, std::string &whyNotCompatible,
1086     std::optional<std::string> &warning) {
1087   std::optional<parser::MessageFixedText> msg;
1088   if (!lhsProcedure) {
1089     msg = "In assignment to object %s, the target '%s' is a procedure"
1090           " designator"_err_en_US;
1091   } else if (!rhsProcedure) {
1092     msg = "In assignment to procedure %s, the characteristics of the target"
1093           " procedure '%s' could not be determined"_err_en_US;
1094   } else if (!isCall && lhsProcedure->functionResult &&
1095       rhsProcedure->functionResult &&
1096       !lhsProcedure->functionResult->IsCompatibleWith(
1097           *rhsProcedure->functionResult, &whyNotCompatible)) {
1098     msg =
1099         "Function %s associated with incompatible function designator '%s': %s"_err_en_US;
1100   } else if (lhsProcedure->IsCompatibleWith(*rhsProcedure, &whyNotCompatible,
1101                  specificIntrinsic, &warning)) {
1102     // OK
1103   } else if (isCall) {
1104     msg = "Procedure %s associated with result of reference to function '%s'"
1105           " that is an incompatible procedure pointer: %s"_err_en_US;
1106   } else if (lhsProcedure->IsPure() && !rhsProcedure->IsPure()) {
1107     msg = "PURE procedure %s may not be associated with non-PURE"
1108           " procedure designator '%s'"_err_en_US;
1109   } else if (lhsProcedure->IsFunction() && rhsProcedure->IsSubroutine()) {
1110     msg = "Function %s may not be associated with subroutine"
1111           " designator '%s'"_err_en_US;
1112   } else if (lhsProcedure->IsSubroutine() && rhsProcedure->IsFunction()) {
1113     msg = "Subroutine %s may not be associated with function"
1114           " designator '%s'"_err_en_US;
1115   } else if (lhsProcedure->HasExplicitInterface() &&
1116       !rhsProcedure->HasExplicitInterface()) {
1117     // Section 10.2.2.4, paragraph 3 prohibits associating a procedure pointer
1118     // that has an explicit interface with a procedure whose characteristics
1119     // don't match.  That's the case if the target procedure has an implicit
1120     // interface.  But this case is allowed by several other compilers as long
1121     // as the explicit interface can be called via an implicit interface.
1122     if (!lhsProcedure->CanBeCalledViaImplicitInterface()) {
1123       msg = "Procedure %s with explicit interface that cannot be called via "
1124             "an implicit interface cannot be associated with procedure "
1125             "designator with an implicit interface"_err_en_US;
1126     }
1127   } else if (!lhsProcedure->HasExplicitInterface() &&
1128       rhsProcedure->HasExplicitInterface()) {
1129     // OK if the target can be called via an implicit interface
1130     if (!rhsProcedure->CanBeCalledViaImplicitInterface() &&
1131         !specificIntrinsic) {
1132       msg = "Procedure %s with implicit interface may not be associated "
1133             "with procedure designator '%s' with explicit interface that "
1134             "cannot be called via an implicit interface"_err_en_US;
1135     }
1136   } else {
1137     msg = "Procedure %s associated with incompatible procedure"
1138           " designator '%s': %s"_err_en_US;
1139   }
1140   return msg;
1141 }
1142 
1143 // GetLastPointerSymbol()
1144 static const Symbol *GetLastPointerSymbol(const Symbol &symbol) {
1145   return IsPointer(GetAssociationRoot(symbol)) ? &symbol : nullptr;
1146 }
1147 static const Symbol *GetLastPointerSymbol(const SymbolRef &symbol) {
1148   return GetLastPointerSymbol(*symbol);
1149 }
1150 static const Symbol *GetLastPointerSymbol(const Component &x) {
1151   const Symbol &c{x.GetLastSymbol()};
1152   return IsPointer(c) ? &c : GetLastPointerSymbol(x.base());
1153 }
1154 static const Symbol *GetLastPointerSymbol(const NamedEntity &x) {
1155   const auto *c{x.UnwrapComponent()};
1156   return c ? GetLastPointerSymbol(*c) : GetLastPointerSymbol(x.GetLastSymbol());
1157 }
1158 static const Symbol *GetLastPointerSymbol(const ArrayRef &x) {
1159   return GetLastPointerSymbol(x.base());
1160 }
1161 static const Symbol *GetLastPointerSymbol(const CoarrayRef &x) {
1162   return nullptr;
1163 }
1164 const Symbol *GetLastPointerSymbol(const DataRef &x) {
1165   return common::visit(
1166       [](const auto &y) { return GetLastPointerSymbol(y); }, x.u);
1167 }
1168 
1169 template <TypeCategory TO, TypeCategory FROM>
1170 static std::optional<Expr<SomeType>> DataConstantConversionHelper(
1171     FoldingContext &context, const DynamicType &toType,
1172     const Expr<SomeType> &expr) {
1173   DynamicType sizedType{FROM, toType.kind()};
1174   if (auto sized{
1175           Fold(context, ConvertToType(sizedType, Expr<SomeType>{expr}))}) {
1176     if (const auto *someExpr{UnwrapExpr<Expr<SomeKind<FROM>>>(*sized)}) {
1177       return common::visit(
1178           [](const auto &w) -> std::optional<Expr<SomeType>> {
1179             using FromType = ResultType<decltype(w)>;
1180             static constexpr int kind{FromType::kind};
1181             if constexpr (IsValidKindOfIntrinsicType(TO, kind)) {
1182               if (const auto *fromConst{UnwrapExpr<Constant<FromType>>(w)}) {
1183                 using FromWordType = typename FromType::Scalar;
1184                 using LogicalType = value::Logical<FromWordType::bits>;
1185                 using ElementType =
1186                     std::conditional_t<TO == TypeCategory::Logical, LogicalType,
1187                         typename LogicalType::Word>;
1188                 std::vector<ElementType> values;
1189                 auto at{fromConst->lbounds()};
1190                 auto shape{fromConst->shape()};
1191                 for (auto n{GetSize(shape)}; n-- > 0;
1192                      fromConst->IncrementSubscripts(at)) {
1193                   auto elt{fromConst->At(at)};
1194                   if constexpr (TO == TypeCategory::Logical) {
1195                     values.emplace_back(std::move(elt));
1196                   } else {
1197                     values.emplace_back(elt.word());
1198                   }
1199                 }
1200                 return {AsGenericExpr(AsExpr(Constant<Type<TO, kind>>{
1201                     std::move(values), std::move(shape)}))};
1202               }
1203             }
1204             return std::nullopt;
1205           },
1206           someExpr->u);
1207     }
1208   }
1209   return std::nullopt;
1210 }
1211 
1212 std::optional<Expr<SomeType>> DataConstantConversionExtension(
1213     FoldingContext &context, const DynamicType &toType,
1214     const Expr<SomeType> &expr0) {
1215   Expr<SomeType> expr{Fold(context, Expr<SomeType>{expr0})};
1216   if (!IsActuallyConstant(expr)) {
1217     return std::nullopt;
1218   }
1219   if (auto fromType{expr.GetType()}) {
1220     if (toType.category() == TypeCategory::Logical &&
1221         fromType->category() == TypeCategory::Integer) {
1222       return DataConstantConversionHelper<TypeCategory::Logical,
1223           TypeCategory::Integer>(context, toType, expr);
1224     }
1225     if (toType.category() == TypeCategory::Integer &&
1226         fromType->category() == TypeCategory::Logical) {
1227       return DataConstantConversionHelper<TypeCategory::Integer,
1228           TypeCategory::Logical>(context, toType, expr);
1229     }
1230   }
1231   return std::nullopt;
1232 }
1233 
1234 bool IsAllocatableOrPointerObject(const Expr<SomeType> &expr) {
1235   const semantics::Symbol *sym{UnwrapWholeSymbolOrComponentDataRef(expr)};
1236   return (sym &&
1237              semantics::IsAllocatableOrObjectPointer(&sym->GetUltimate())) ||
1238       evaluate::IsObjectPointer(expr);
1239 }
1240 
1241 bool IsAllocatableDesignator(const Expr<SomeType> &expr) {
1242   // Allocatable sub-objects are not themselves allocatable (9.5.3.1 NOTE 2).
1243   if (const semantics::Symbol *
1244       sym{UnwrapWholeSymbolOrComponentOrCoarrayRef(expr)}) {
1245     return semantics::IsAllocatable(sym->GetUltimate());
1246   }
1247   return false;
1248 }
1249 
1250 bool MayBePassedAsAbsentOptional(const Expr<SomeType> &expr) {
1251   const semantics::Symbol *sym{UnwrapWholeSymbolOrComponentDataRef(expr)};
1252   // 15.5.2.12 1. is pretty clear that an unallocated allocatable/pointer actual
1253   // may be passed to a non-allocatable/non-pointer optional dummy. Note that
1254   // other compilers (like nag, nvfortran, ifort, gfortran and xlf) seems to
1255   // ignore this point in intrinsic contexts (e.g CMPLX argument).
1256   return (sym && semantics::IsOptional(*sym)) ||
1257       IsAllocatableOrPointerObject(expr);
1258 }
1259 
1260 std::optional<Expr<SomeType>> HollerithToBOZ(FoldingContext &context,
1261     const Expr<SomeType> &expr, const DynamicType &type) {
1262   if (std::optional<std::string> chValue{GetScalarConstantValue<Ascii>(expr)}) {
1263     // Pad on the right with spaces when short, truncate the right if long.
1264     // TODO: big-endian targets
1265     auto bytes{static_cast<std::size_t>(
1266         ToInt64(type.MeasureSizeInBytes(context, false)).value())};
1267     BOZLiteralConstant bits{0};
1268     for (std::size_t j{0}; j < bytes; ++j) {
1269       char ch{j >= chValue->size() ? ' ' : chValue->at(j)};
1270       BOZLiteralConstant chBOZ{static_cast<unsigned char>(ch)};
1271       bits = bits.IOR(chBOZ.SHIFTL(8 * j));
1272     }
1273     return ConvertToType(type, Expr<SomeType>{bits});
1274   } else {
1275     return std::nullopt;
1276   }
1277 }
1278 
1279 // Extracts a whole symbol being used as a bound of a dummy argument,
1280 // possibly wrapped with parentheses or MAX(0, ...).
1281 template <int KIND>
1282 static const Symbol *GetBoundSymbol(
1283     const Expr<Type<TypeCategory::Integer, KIND>> &expr) {
1284   using T = Type<TypeCategory::Integer, KIND>;
1285   return common::visit(
1286       common::visitors{
1287           [](const Extremum<T> &max) -> const Symbol * {
1288             if (max.ordering == Ordering::Greater) {
1289               if (auto zero{ToInt64(max.left())}; zero && *zero == 0) {
1290                 return GetBoundSymbol(max.right());
1291               }
1292             }
1293             return nullptr;
1294           },
1295           [](const Parentheses<T> &x) { return GetBoundSymbol(x.left()); },
1296           [](const Designator<T> &x) -> const Symbol * {
1297             if (const auto *ref{std::get_if<SymbolRef>(&x.u)}) {
1298               return &**ref;
1299             }
1300             return nullptr;
1301           },
1302           [](const Convert<T, TypeCategory::Integer> &x) {
1303             return common::visit(
1304                 [](const auto &y) -> const Symbol * {
1305                   using yType = std::decay_t<decltype(y)>;
1306                   using yResult = typename yType::Result;
1307                   if constexpr (yResult::kind <= KIND) {
1308                     return GetBoundSymbol(y);
1309                   } else {
1310                     return nullptr;
1311                   }
1312                 },
1313                 x.left().u);
1314           },
1315           [](const auto &) -> const Symbol * { return nullptr; },
1316       },
1317       expr.u);
1318 }
1319 
1320 std::optional<bool> AreEquivalentInInterface(
1321     const Expr<SubscriptInteger> &x, const Expr<SubscriptInteger> &y) {
1322   auto xVal{ToInt64(x)};
1323   auto yVal{ToInt64(y)};
1324   if (xVal && yVal) {
1325     return *xVal == *yVal;
1326   } else if (xVal || yVal) {
1327     return false;
1328   }
1329   const Symbol *xSym{GetBoundSymbol(x)};
1330   const Symbol *ySym{GetBoundSymbol(y)};
1331   if (xSym && ySym) {
1332     if (&xSym->GetUltimate() == &ySym->GetUltimate()) {
1333       return true; // USE/host associated same symbol
1334     }
1335     auto xNum{semantics::GetDummyArgumentNumber(xSym)};
1336     auto yNum{semantics::GetDummyArgumentNumber(ySym)};
1337     if (xNum && yNum) {
1338       if (*xNum == *yNum) {
1339         auto xType{DynamicType::From(*xSym)};
1340         auto yType{DynamicType::From(*ySym)};
1341         return xType && yType && xType->IsEquivalentTo(*yType);
1342       }
1343     }
1344     return false;
1345   } else if (xSym || ySym) {
1346     return false;
1347   }
1348   // Neither expression is an integer constant or a whole symbol.
1349   if (x == y) {
1350     return true;
1351   } else {
1352     return std::nullopt; // not sure
1353   }
1354 }
1355 
1356 bool CheckForCoindexedObject(parser::ContextualMessages &messages,
1357     const std::optional<ActualArgument> &arg, const std::string &procName,
1358     const std::string &argName) {
1359   if (arg && ExtractCoarrayRef(arg->UnwrapExpr())) {
1360     messages.Say(arg->sourceLocation(),
1361         "'%s' argument to '%s' may not be a coindexed object"_err_en_US,
1362         argName, procName);
1363     return false;
1364   } else {
1365     return true;
1366   }
1367 }
1368 
1369 } // namespace Fortran::evaluate
1370 
1371 namespace Fortran::semantics {
1372 
1373 const Symbol &ResolveAssociations(const Symbol &original) {
1374   const Symbol &symbol{original.GetUltimate()};
1375   if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) {
1376     if (!details->rank()) { // Not RANK(n) or RANK(*)
1377       if (const Symbol * nested{UnwrapWholeSymbolDataRef(details->expr())}) {
1378         return ResolveAssociations(*nested);
1379       }
1380     }
1381   }
1382   return symbol;
1383 }
1384 
1385 // When a construct association maps to a variable, and that variable
1386 // is not an array with a vector-valued subscript, return the base
1387 // Symbol of that variable, else nullptr.  Descends into other construct
1388 // associations when one associations maps to another.
1389 static const Symbol *GetAssociatedVariable(const AssocEntityDetails &details) {
1390   if (const auto &expr{details.expr()}) {
1391     if (IsVariable(*expr) && !HasVectorSubscript(*expr)) {
1392       if (const Symbol * varSymbol{GetFirstSymbol(*expr)}) {
1393         return &GetAssociationRoot(*varSymbol);
1394       }
1395     }
1396   }
1397   return nullptr;
1398 }
1399 
1400 const Symbol &GetAssociationRoot(const Symbol &original) {
1401   const Symbol &symbol{ResolveAssociations(original)};
1402   if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) {
1403     if (const Symbol * root{GetAssociatedVariable(*details)}) {
1404       return *root;
1405     }
1406   }
1407   return symbol;
1408 }
1409 
1410 const Symbol *GetMainEntry(const Symbol *symbol) {
1411   if (symbol) {
1412     if (const auto *subpDetails{symbol->detailsIf<SubprogramDetails>()}) {
1413       if (const Scope * scope{subpDetails->entryScope()}) {
1414         if (const Symbol * main{scope->symbol()}) {
1415           return main;
1416         }
1417       }
1418     }
1419   }
1420   return symbol;
1421 }
1422 
1423 bool IsVariableName(const Symbol &original) {
1424   const Symbol &ultimate{original.GetUltimate()};
1425   return !IsNamedConstant(ultimate) &&
1426       (ultimate.has<ObjectEntityDetails>() ||
1427           ultimate.has<AssocEntityDetails>());
1428 }
1429 
1430 static bool IsPureProcedureImpl(
1431     const Symbol &original, semantics::UnorderedSymbolSet &set) {
1432   // An ENTRY is pure if its containing subprogram is
1433   const Symbol &symbol{DEREF(GetMainEntry(&original.GetUltimate()))};
1434   if (set.find(symbol) != set.end()) {
1435     return true;
1436   }
1437   set.emplace(symbol);
1438   if (const auto *procDetails{symbol.detailsIf<ProcEntityDetails>()}) {
1439     if (procDetails->procInterface()) {
1440       // procedure with a pure interface
1441       return IsPureProcedureImpl(*procDetails->procInterface(), set);
1442     }
1443   } else if (const auto *details{symbol.detailsIf<ProcBindingDetails>()}) {
1444     return IsPureProcedureImpl(details->symbol(), set);
1445   } else if (!IsProcedure(symbol)) {
1446     return false;
1447   }
1448   if (IsStmtFunction(symbol)) {
1449     // Section 15.7(1) states that a statement function is PURE if it does not
1450     // reference an IMPURE procedure or a VOLATILE variable
1451     if (const auto &expr{symbol.get<SubprogramDetails>().stmtFunction()}) {
1452       for (const SymbolRef &ref : evaluate::CollectSymbols(*expr)) {
1453         if (&*ref == &symbol) {
1454           return false; // error recovery, recursion is caught elsewhere
1455         }
1456         if (IsFunction(*ref) && !IsPureProcedureImpl(*ref, set)) {
1457           return false;
1458         }
1459         if (ref->GetUltimate().attrs().test(Attr::VOLATILE)) {
1460           return false;
1461         }
1462       }
1463     }
1464     return true; // statement function was not found to be impure
1465   }
1466   return symbol.attrs().test(Attr::PURE) ||
1467       (symbol.attrs().test(Attr::ELEMENTAL) &&
1468           !symbol.attrs().test(Attr::IMPURE));
1469 }
1470 
1471 bool IsPureProcedure(const Symbol &original) {
1472   semantics::UnorderedSymbolSet set;
1473   return IsPureProcedureImpl(original, set);
1474 }
1475 
1476 bool IsPureProcedure(const Scope &scope) {
1477   const Symbol *symbol{scope.GetSymbol()};
1478   return symbol && IsPureProcedure(*symbol);
1479 }
1480 
1481 bool IsExplicitlyImpureProcedure(const Symbol &original) {
1482   // An ENTRY is IMPURE if its containing subprogram is so
1483   return DEREF(GetMainEntry(&original.GetUltimate()))
1484       .attrs()
1485       .test(Attr::IMPURE);
1486 }
1487 
1488 bool IsElementalProcedure(const Symbol &original) {
1489   // An ENTRY is elemental if its containing subprogram is
1490   const Symbol &symbol{DEREF(GetMainEntry(&original.GetUltimate()))};
1491   if (IsProcedure(symbol)) {
1492     auto &foldingContext{symbol.owner().context().foldingContext()};
1493     auto restorer{foldingContext.messages().DiscardMessages()};
1494     auto proc{evaluate::characteristics::Procedure::Characterize(
1495         symbol, foldingContext)};
1496     return proc &&
1497         proc->attrs.test(evaluate::characteristics::Procedure::Attr::Elemental);
1498   } else {
1499     return false;
1500   }
1501 }
1502 
1503 bool IsFunction(const Symbol &symbol) {
1504   const Symbol &ultimate{symbol.GetUltimate()};
1505   return ultimate.test(Symbol::Flag::Function) ||
1506       (!ultimate.test(Symbol::Flag::Subroutine) &&
1507           common::visit(
1508               common::visitors{
1509                   [](const SubprogramDetails &x) { return x.isFunction(); },
1510                   [](const ProcEntityDetails &x) {
1511                     const Symbol *ifc{x.procInterface()};
1512                     return x.type() || (ifc && IsFunction(*ifc));
1513                   },
1514                   [](const ProcBindingDetails &x) {
1515                     return IsFunction(x.symbol());
1516                   },
1517                   [](const auto &) { return false; },
1518               },
1519               ultimate.details()));
1520 }
1521 
1522 bool IsFunction(const Scope &scope) {
1523   const Symbol *symbol{scope.GetSymbol()};
1524   return symbol && IsFunction(*symbol);
1525 }
1526 
1527 bool IsProcedure(const Symbol &symbol) {
1528   return common::visit(common::visitors{
1529                            [&symbol](const SubprogramDetails &) {
1530                              const Scope *scope{symbol.scope()};
1531                              // Main programs & BLOCK DATA are not procedures.
1532                              return !scope ||
1533                                  scope->kind() == Scope::Kind::Subprogram;
1534                            },
1535                            [](const SubprogramNameDetails &) { return true; },
1536                            [](const ProcEntityDetails &) { return true; },
1537                            [](const GenericDetails &) { return true; },
1538                            [](const ProcBindingDetails &) { return true; },
1539                            [](const auto &) { return false; },
1540                        },
1541       symbol.GetUltimate().details());
1542 }
1543 
1544 bool IsProcedure(const Scope &scope) {
1545   const Symbol *symbol{scope.GetSymbol()};
1546   return symbol && IsProcedure(*symbol);
1547 }
1548 
1549 bool IsProcedurePointer(const Symbol &original) {
1550   const Symbol &symbol{GetAssociationRoot(original)};
1551   return IsPointer(symbol) && IsProcedure(symbol);
1552 }
1553 
1554 bool IsProcedurePointer(const Symbol *symbol) {
1555   return symbol && IsProcedurePointer(*symbol);
1556 }
1557 
1558 bool IsObjectPointer(const Symbol *original) {
1559   if (original) {
1560     const Symbol &symbol{GetAssociationRoot(*original)};
1561     return IsPointer(symbol) && !IsProcedure(symbol);
1562   } else {
1563     return false;
1564   }
1565 }
1566 
1567 bool IsAllocatableOrObjectPointer(const Symbol *original) {
1568   if (original) {
1569     const Symbol &ultimate{original->GetUltimate()};
1570     if (const auto *assoc{ultimate.detailsIf<AssocEntityDetails>()}) {
1571       // Only SELECT RANK construct entities can be ALLOCATABLE/POINTER.
1572       return (assoc->rank() || assoc->IsAssumedSize() ||
1573                  assoc->IsAssumedRank()) &&
1574           IsAllocatableOrObjectPointer(UnwrapWholeSymbolDataRef(assoc->expr()));
1575     } else {
1576       return IsAllocatable(ultimate) ||
1577           (IsPointer(ultimate) && !IsProcedure(ultimate));
1578     }
1579   } else {
1580     return false;
1581   }
1582 }
1583 
1584 const Symbol *FindCommonBlockContaining(const Symbol &original) {
1585   const Symbol &root{GetAssociationRoot(original)};
1586   const auto *details{root.detailsIf<ObjectEntityDetails>()};
1587   return details ? details->commonBlock() : nullptr;
1588 }
1589 
1590 // 3.11 automatic data object
1591 bool IsAutomatic(const Symbol &original) {
1592   const Symbol &symbol{original.GetUltimate()};
1593   if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) {
1594     if (!object->isDummy() && !IsAllocatable(symbol) && !IsPointer(symbol)) {
1595       if (const DeclTypeSpec * type{symbol.GetType()}) {
1596         // If a type parameter value is not a constant expression, the
1597         // object is automatic.
1598         if (type->category() == DeclTypeSpec::Character) {
1599           if (const auto &length{
1600                   type->characterTypeSpec().length().GetExplicit()}) {
1601             if (!evaluate::IsConstantExpr(*length)) {
1602               return true;
1603             }
1604           }
1605         } else if (const DerivedTypeSpec * derived{type->AsDerived()}) {
1606           for (const auto &pair : derived->parameters()) {
1607             if (const auto &value{pair.second.GetExplicit()}) {
1608               if (!evaluate::IsConstantExpr(*value)) {
1609                 return true;
1610               }
1611             }
1612           }
1613         }
1614       }
1615       // If an array bound is not a constant expression, the object is
1616       // automatic.
1617       for (const ShapeSpec &dim : object->shape()) {
1618         if (const auto &lb{dim.lbound().GetExplicit()}) {
1619           if (!evaluate::IsConstantExpr(*lb)) {
1620             return true;
1621           }
1622         }
1623         if (const auto &ub{dim.ubound().GetExplicit()}) {
1624           if (!evaluate::IsConstantExpr(*ub)) {
1625             return true;
1626           }
1627         }
1628       }
1629     }
1630   }
1631   return false;
1632 }
1633 
1634 bool IsSaved(const Symbol &original) {
1635   const Symbol &symbol{GetAssociationRoot(original)};
1636   const Scope &scope{symbol.owner()};
1637   const common::LanguageFeatureControl &features{
1638       scope.context().languageFeatures()};
1639   auto scopeKind{scope.kind()};
1640   if (symbol.has<AssocEntityDetails>()) {
1641     return false; // ASSOCIATE(non-variable)
1642   } else if (scopeKind == Scope::Kind::DerivedType) {
1643     return false; // this is a component
1644   } else if (symbol.attrs().test(Attr::SAVE)) {
1645     return true; // explicit SAVE attribute
1646   } else if (IsDummy(symbol) || IsFunctionResult(symbol) ||
1647       IsAutomatic(symbol) || IsNamedConstant(symbol)) {
1648     return false;
1649   } else if (scopeKind == Scope::Kind::Module ||
1650       (scopeKind == Scope::Kind::MainProgram &&
1651           (symbol.attrs().test(Attr::TARGET) || evaluate::IsCoarray(symbol)))) {
1652     // 8.5.16p4
1653     // In main programs, implied SAVE matters only for pointer
1654     // initialization targets and coarrays.
1655     return true;
1656   } else if (scopeKind == Scope::Kind::MainProgram &&
1657       (features.IsEnabled(common::LanguageFeature::SaveMainProgram) ||
1658           (features.IsEnabled(
1659                common::LanguageFeature::SaveBigMainProgramVariables) &&
1660               symbol.size() > 32))) {
1661     // With SaveBigMainProgramVariables, keeping all unsaved main program
1662     // variables of 32 bytes or less on the stack allows keeping numerical and
1663     // logical scalars, small scalar characters or derived, small arrays, and
1664     // scalar descriptors on the stack. This leaves more room for lower level
1665     // optimizers to do register promotion or get easy aliasing information.
1666     return true;
1667   } else if (features.IsEnabled(common::LanguageFeature::DefaultSave) &&
1668       (scopeKind == Scope::Kind::MainProgram ||
1669           (scope.kind() == Scope::Kind::Subprogram &&
1670               !(scope.symbol() &&
1671                   scope.symbol()->attrs().test(Attr::RECURSIVE))))) {
1672     // -fno-automatic/-save/-Msave option applies to all objects in executable
1673     // main programs and subprograms unless they are explicitly RECURSIVE.
1674     return true;
1675   } else if (symbol.test(Symbol::Flag::InDataStmt)) {
1676     return true;
1677   } else if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()};
1678              object && object->init()) {
1679     return true;
1680   } else if (IsProcedurePointer(symbol) && symbol.has<ProcEntityDetails>() &&
1681       symbol.get<ProcEntityDetails>().init()) {
1682     return true;
1683   } else if (scope.hasSAVE()) {
1684     return true; // bare SAVE statement
1685   } else if (const Symbol * block{FindCommonBlockContaining(symbol)};
1686              block && block->attrs().test(Attr::SAVE)) {
1687     return true; // in COMMON with SAVE
1688   } else {
1689     return false;
1690   }
1691 }
1692 
1693 bool IsDummy(const Symbol &symbol) {
1694   return common::visit(
1695       common::visitors{[](const EntityDetails &x) { return x.isDummy(); },
1696           [](const ObjectEntityDetails &x) { return x.isDummy(); },
1697           [](const ProcEntityDetails &x) { return x.isDummy(); },
1698           [](const SubprogramDetails &x) { return x.isDummy(); },
1699           [](const auto &) { return false; }},
1700       ResolveAssociations(symbol).details());
1701 }
1702 
1703 bool IsAssumedShape(const Symbol &symbol) {
1704   const Symbol &ultimate{ResolveAssociations(symbol)};
1705   const auto *object{ultimate.detailsIf<ObjectEntityDetails>()};
1706   return object && object->IsAssumedShape() &&
1707       !semantics::IsAllocatableOrObjectPointer(&ultimate);
1708 }
1709 
1710 bool IsDeferredShape(const Symbol &symbol) {
1711   const Symbol &ultimate{ResolveAssociations(symbol)};
1712   const auto *object{ultimate.detailsIf<ObjectEntityDetails>()};
1713   return object && object->CanBeDeferredShape() &&
1714       semantics::IsAllocatableOrObjectPointer(&ultimate);
1715 }
1716 
1717 bool IsFunctionResult(const Symbol &original) {
1718   const Symbol &symbol{GetAssociationRoot(original)};
1719   return common::visit(
1720       common::visitors{
1721           [](const EntityDetails &x) { return x.isFuncResult(); },
1722           [](const ObjectEntityDetails &x) { return x.isFuncResult(); },
1723           [](const ProcEntityDetails &x) { return x.isFuncResult(); },
1724           [](const auto &) { return false; },
1725       },
1726       symbol.details());
1727 }
1728 
1729 bool IsKindTypeParameter(const Symbol &symbol) {
1730   const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()};
1731   return param && param->attr() == common::TypeParamAttr::Kind;
1732 }
1733 
1734 bool IsLenTypeParameter(const Symbol &symbol) {
1735   const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()};
1736   return param && param->attr() == common::TypeParamAttr::Len;
1737 }
1738 
1739 bool IsExtensibleType(const DerivedTypeSpec *derived) {
1740   return !IsSequenceOrBindCType(derived) && !IsIsoCType(derived);
1741 }
1742 
1743 bool IsSequenceOrBindCType(const DerivedTypeSpec *derived) {
1744   return derived &&
1745       (derived->typeSymbol().attrs().test(Attr::BIND_C) ||
1746           derived->typeSymbol().get<DerivedTypeDetails>().sequence());
1747 }
1748 
1749 bool IsBuiltinDerivedType(const DerivedTypeSpec *derived, const char *name) {
1750   if (!derived) {
1751     return false;
1752   } else {
1753     const auto &symbol{derived->typeSymbol()};
1754     return &symbol.owner() == symbol.owner().context().GetBuiltinsScope() &&
1755         symbol.name() == "__builtin_"s + name;
1756   }
1757 }
1758 
1759 bool IsBuiltinCPtr(const Symbol &symbol) {
1760   if (const DeclTypeSpec *declType = symbol.GetType()) {
1761     if (const DerivedTypeSpec *derived = declType->AsDerived()) {
1762       return IsIsoCType(derived);
1763     }
1764   }
1765   return false;
1766 }
1767 
1768 bool IsIsoCType(const DerivedTypeSpec *derived) {
1769   return IsBuiltinDerivedType(derived, "c_ptr") ||
1770       IsBuiltinDerivedType(derived, "c_funptr");
1771 }
1772 
1773 bool IsEventType(const DerivedTypeSpec *derived) {
1774   return IsBuiltinDerivedType(derived, "event_type");
1775 }
1776 
1777 bool IsLockType(const DerivedTypeSpec *derived) {
1778   return IsBuiltinDerivedType(derived, "lock_type");
1779 }
1780 
1781 bool IsNotifyType(const DerivedTypeSpec *derived) {
1782   return IsBuiltinDerivedType(derived, "notify_type");
1783 }
1784 
1785 bool IsTeamType(const DerivedTypeSpec *derived) {
1786   return IsBuiltinDerivedType(derived, "team_type");
1787 }
1788 
1789 bool IsBadCoarrayType(const DerivedTypeSpec *derived) {
1790   return IsTeamType(derived) || IsIsoCType(derived);
1791 }
1792 
1793 bool IsEventTypeOrLockType(const DerivedTypeSpec *derivedTypeSpec) {
1794   return IsEventType(derivedTypeSpec) || IsLockType(derivedTypeSpec);
1795 }
1796 
1797 int CountLenParameters(const DerivedTypeSpec &type) {
1798   return llvm::count_if(
1799       type.parameters(), [](const auto &pair) { return pair.second.isLen(); });
1800 }
1801 
1802 int CountNonConstantLenParameters(const DerivedTypeSpec &type) {
1803   return llvm::count_if(type.parameters(), [](const auto &pair) {
1804     if (!pair.second.isLen()) {
1805       return false;
1806     } else if (const auto &expr{pair.second.GetExplicit()}) {
1807       return !IsConstantExpr(*expr);
1808     } else {
1809       return true;
1810     }
1811   });
1812 }
1813 
1814 const Symbol &GetUsedModule(const UseDetails &details) {
1815   return DEREF(details.symbol().owner().symbol());
1816 }
1817 
1818 static const Symbol *FindFunctionResult(
1819     const Symbol &original, UnorderedSymbolSet &seen) {
1820   const Symbol &root{GetAssociationRoot(original)};
1821   ;
1822   if (!seen.insert(root).second) {
1823     return nullptr; // don't loop
1824   }
1825   return common::visit(
1826       common::visitors{[](const SubprogramDetails &subp) {
1827                          return subp.isFunction() ? &subp.result() : nullptr;
1828                        },
1829           [&](const ProcEntityDetails &proc) {
1830             const Symbol *iface{proc.procInterface()};
1831             return iface ? FindFunctionResult(*iface, seen) : nullptr;
1832           },
1833           [&](const ProcBindingDetails &binding) {
1834             return FindFunctionResult(binding.symbol(), seen);
1835           },
1836           [](const auto &) -> const Symbol * { return nullptr; }},
1837       root.details());
1838 }
1839 
1840 const Symbol *FindFunctionResult(const Symbol &symbol) {
1841   UnorderedSymbolSet seen;
1842   return FindFunctionResult(symbol, seen);
1843 }
1844 
1845 // These are here in Evaluate/tools.cpp so that Evaluate can use
1846 // them; they cannot be defined in symbol.h due to the dependence
1847 // on Scope.
1848 
1849 bool SymbolSourcePositionCompare::operator()(
1850     const SymbolRef &x, const SymbolRef &y) const {
1851   return x->GetSemanticsContext().allCookedSources().Precedes(
1852       x->name(), y->name());
1853 }
1854 bool SymbolSourcePositionCompare::operator()(
1855     const MutableSymbolRef &x, const MutableSymbolRef &y) const {
1856   return x->GetSemanticsContext().allCookedSources().Precedes(
1857       x->name(), y->name());
1858 }
1859 
1860 SemanticsContext &Symbol::GetSemanticsContext() const {
1861   return DEREF(owner_).context();
1862 }
1863 
1864 bool AreTkCompatibleTypes(const DeclTypeSpec *x, const DeclTypeSpec *y) {
1865   if (x && y) {
1866     if (auto xDt{evaluate::DynamicType::From(*x)}) {
1867       if (auto yDt{evaluate::DynamicType::From(*y)}) {
1868         return xDt->IsTkCompatibleWith(*yDt);
1869       }
1870     }
1871   }
1872   return false;
1873 }
1874 
1875 common::IgnoreTKRSet GetIgnoreTKR(const Symbol &symbol) {
1876   common::IgnoreTKRSet result;
1877   if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) {
1878     result = object->ignoreTKR();
1879     if (const Symbol * ownerSymbol{symbol.owner().symbol()}) {
1880       if (const auto *ownerSubp{ownerSymbol->detailsIf<SubprogramDetails>()}) {
1881         if (ownerSubp->defaultIgnoreTKR()) {
1882           result |= common::ignoreTKRAll;
1883         }
1884       }
1885     }
1886   }
1887   return result;
1888 }
1889 
1890 std::optional<int> GetDummyArgumentNumber(const Symbol *symbol) {
1891   if (symbol) {
1892     if (IsDummy(*symbol)) {
1893       if (const Symbol * subpSym{symbol->owner().symbol()}) {
1894         if (const auto *subp{subpSym->detailsIf<SubprogramDetails>()}) {
1895           int j{0};
1896           for (const Symbol *dummy : subp->dummyArgs()) {
1897             if (dummy == symbol) {
1898               return j;
1899             }
1900             ++j;
1901           }
1902         }
1903       }
1904     }
1905   }
1906   return std::nullopt;
1907 }
1908 
1909 } // namespace Fortran::semantics
1910