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 template <TypeCategory TO, TypeCategory FROM>
1014 static std::optional<Expr<SomeType>> DataConstantConversionHelper(
1015     FoldingContext &context, const DynamicType &toType,
1016     const Expr<SomeType> &expr) {
1017   DynamicType sizedType{FROM, toType.kind()};
1018   if (auto sized{
1019           Fold(context, ConvertToType(sizedType, Expr<SomeType>{expr}))}) {
1020     if (const auto *someExpr{UnwrapExpr<Expr<SomeKind<FROM>>>(*sized)}) {
1021       return std::visit(
1022           [](const auto &w) -> std::optional<Expr<SomeType>> {
1023             using FromType = typename std::decay_t<decltype(w)>::Result;
1024             static constexpr int kind{FromType::kind};
1025             if constexpr (IsValidKindOfIntrinsicType(TO, kind)) {
1026               if (const auto *fromConst{UnwrapExpr<Constant<FromType>>(w)}) {
1027                 using FromWordType = typename FromType::Scalar;
1028                 using LogicalType = value::Logical<FromWordType::bits>;
1029                 using ElementType =
1030                     std::conditional_t<TO == TypeCategory::Logical, LogicalType,
1031                         typename LogicalType::Word>;
1032                 std::vector<ElementType> values;
1033                 auto at{fromConst->lbounds()};
1034                 auto shape{fromConst->shape()};
1035                 for (auto n{GetSize(shape)}; n-- > 0;
1036                      fromConst->IncrementSubscripts(at)) {
1037                   auto elt{fromConst->At(at)};
1038                   if constexpr (TO == TypeCategory::Logical) {
1039                     values.emplace_back(std::move(elt));
1040                   } else {
1041                     values.emplace_back(elt.word());
1042                   }
1043                 }
1044                 return {AsGenericExpr(AsExpr(Constant<Type<TO, kind>>{
1045                     std::move(values), std::move(shape)}))};
1046               }
1047             }
1048             return std::nullopt;
1049           },
1050           someExpr->u);
1051     }
1052   }
1053   return std::nullopt;
1054 }
1055 
1056 std::optional<Expr<SomeType>> DataConstantConversionExtension(
1057     FoldingContext &context, const DynamicType &toType,
1058     const Expr<SomeType> &expr0) {
1059   Expr<SomeType> expr{Fold(context, Expr<SomeType>{expr0})};
1060   if (!IsActuallyConstant(expr)) {
1061     return std::nullopt;
1062   }
1063   if (auto fromType{expr.GetType()}) {
1064     if (toType.category() == TypeCategory::Logical &&
1065         fromType->category() == TypeCategory::Integer) {
1066       return DataConstantConversionHelper<TypeCategory::Logical,
1067           TypeCategory::Integer>(context, toType, expr);
1068     }
1069     if (toType.category() == TypeCategory::Integer &&
1070         fromType->category() == TypeCategory::Logical) {
1071       return DataConstantConversionHelper<TypeCategory::Integer,
1072           TypeCategory::Logical>(context, toType, expr);
1073     }
1074   }
1075   return std::nullopt;
1076 }
1077 
1078 } // namespace Fortran::evaluate
1079 
1080 namespace Fortran::semantics {
1081 
1082 const Symbol &ResolveAssociations(const Symbol &original) {
1083   const Symbol &symbol{original.GetUltimate()};
1084   if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) {
1085     if (const Symbol * nested{UnwrapWholeSymbolDataRef(details->expr())}) {
1086       return ResolveAssociations(*nested);
1087     }
1088   }
1089   return symbol;
1090 }
1091 
1092 // When a construct association maps to a variable, and that variable
1093 // is not an array with a vector-valued subscript, return the base
1094 // Symbol of that variable, else nullptr.  Descends into other construct
1095 // associations when one associations maps to another.
1096 static const Symbol *GetAssociatedVariable(const AssocEntityDetails &details) {
1097   if (const auto &expr{details.expr()}) {
1098     if (IsVariable(*expr) && !HasVectorSubscript(*expr)) {
1099       if (const Symbol * varSymbol{GetFirstSymbol(*expr)}) {
1100         return &GetAssociationRoot(*varSymbol);
1101       }
1102     }
1103   }
1104   return nullptr;
1105 }
1106 
1107 const Symbol &GetAssociationRoot(const Symbol &original) {
1108   const Symbol &symbol{ResolveAssociations(original)};
1109   if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) {
1110     if (const Symbol * root{GetAssociatedVariable(*details)}) {
1111       return *root;
1112     }
1113   }
1114   return symbol;
1115 }
1116 
1117 const Symbol *GetMainEntry(const Symbol *symbol) {
1118   if (symbol) {
1119     if (const auto *subpDetails{symbol->detailsIf<SubprogramDetails>()}) {
1120       if (const Scope * scope{subpDetails->entryScope()}) {
1121         if (const Symbol * main{scope->symbol()}) {
1122           return main;
1123         }
1124       }
1125     }
1126   }
1127   return symbol;
1128 }
1129 
1130 bool IsVariableName(const Symbol &original) {
1131   const Symbol &symbol{ResolveAssociations(original)};
1132   if (symbol.has<ObjectEntityDetails>()) {
1133     return !IsNamedConstant(symbol);
1134   } else if (const auto *assoc{symbol.detailsIf<AssocEntityDetails>()}) {
1135     const auto &expr{assoc->expr()};
1136     return expr && IsVariable(*expr) && !HasVectorSubscript(*expr);
1137   } else {
1138     return false;
1139   }
1140 }
1141 
1142 bool IsPureProcedure(const Symbol &original) {
1143   // An ENTRY is pure if its containing subprogram is
1144   const Symbol &symbol{DEREF(GetMainEntry(&original.GetUltimate()))};
1145   if (const auto *procDetails{symbol.detailsIf<ProcEntityDetails>()}) {
1146     if (const Symbol * procInterface{procDetails->interface().symbol()}) {
1147       // procedure component with a pure interface
1148       return IsPureProcedure(*procInterface);
1149     }
1150   } else if (const auto *details{symbol.detailsIf<ProcBindingDetails>()}) {
1151     return IsPureProcedure(details->symbol());
1152   } else if (!IsProcedure(symbol)) {
1153     return false;
1154   }
1155   if (IsStmtFunction(symbol)) {
1156     // Section 15.7(1) states that a statement function is PURE if it does not
1157     // reference an IMPURE procedure or a VOLATILE variable
1158     if (const auto &expr{symbol.get<SubprogramDetails>().stmtFunction()}) {
1159       for (const SymbolRef &ref : evaluate::CollectSymbols(*expr)) {
1160         if (IsFunction(*ref) && !IsPureProcedure(*ref)) {
1161           return false;
1162         }
1163         if (ref->GetUltimate().attrs().test(Attr::VOLATILE)) {
1164           return false;
1165         }
1166       }
1167     }
1168     return true; // statement function was not found to be impure
1169   }
1170   return symbol.attrs().test(Attr::PURE) ||
1171       (symbol.attrs().test(Attr::ELEMENTAL) &&
1172           !symbol.attrs().test(Attr::IMPURE));
1173 }
1174 
1175 bool IsPureProcedure(const Scope &scope) {
1176   const Symbol *symbol{scope.GetSymbol()};
1177   return symbol && IsPureProcedure(*symbol);
1178 }
1179 
1180 bool IsFunction(const Symbol &symbol) {
1181   return std::visit(
1182       common::visitors{
1183           [](const SubprogramDetails &x) { return x.isFunction(); },
1184           [&](const SubprogramNameDetails &) {
1185             return symbol.test(Symbol::Flag::Function);
1186           },
1187           [](const ProcEntityDetails &x) {
1188             const auto &ifc{x.interface()};
1189             return ifc.type() || (ifc.symbol() && IsFunction(*ifc.symbol()));
1190           },
1191           [](const ProcBindingDetails &x) { return IsFunction(x.symbol()); },
1192           [](const auto &) { return false; },
1193       },
1194       symbol.GetUltimate().details());
1195 }
1196 
1197 bool IsFunction(const Scope &scope) {
1198   const Symbol *symbol{scope.GetSymbol()};
1199   return symbol && IsFunction(*symbol);
1200 }
1201 
1202 bool IsProcedure(const Symbol &symbol) {
1203   return std::visit(common::visitors{
1204                         [](const SubprogramDetails &) { return true; },
1205                         [](const SubprogramNameDetails &) { return true; },
1206                         [](const ProcEntityDetails &) { return true; },
1207                         [](const GenericDetails &) { return true; },
1208                         [](const ProcBindingDetails &) { return true; },
1209                         [](const auto &) { return false; },
1210                     },
1211       symbol.GetUltimate().details());
1212 }
1213 
1214 bool IsProcedure(const Scope &scope) {
1215   const Symbol *symbol{scope.GetSymbol()};
1216   return symbol && IsProcedure(*symbol);
1217 }
1218 
1219 const Symbol *FindCommonBlockContaining(const Symbol &original) {
1220   const Symbol &root{GetAssociationRoot(original)};
1221   const auto *details{root.detailsIf<ObjectEntityDetails>()};
1222   return details ? details->commonBlock() : nullptr;
1223 }
1224 
1225 bool IsProcedurePointer(const Symbol &original) {
1226   const Symbol &symbol{GetAssociationRoot(original)};
1227   return symbol.has<ProcEntityDetails>() && IsPointer(symbol);
1228 }
1229 
1230 // 3.11 automatic data object
1231 bool IsAutomatic(const Symbol &original) {
1232   const Symbol &symbol{original.GetUltimate()};
1233   if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) {
1234     if (!object->isDummy() && !IsAllocatable(symbol) && !IsPointer(symbol)) {
1235       if (const DeclTypeSpec * type{symbol.GetType()}) {
1236         // If a type parameter value is not a constant expression, the
1237         // object is automatic.
1238         if (type->category() == DeclTypeSpec::Character) {
1239           if (const auto &length{
1240                   type->characterTypeSpec().length().GetExplicit()}) {
1241             if (!evaluate::IsConstantExpr(*length)) {
1242               return true;
1243             }
1244           }
1245         } else if (const DerivedTypeSpec * derived{type->AsDerived()}) {
1246           for (const auto &pair : derived->parameters()) {
1247             if (const auto &value{pair.second.GetExplicit()}) {
1248               if (!evaluate::IsConstantExpr(*value)) {
1249                 return true;
1250               }
1251             }
1252           }
1253         }
1254       }
1255       // If an array bound is not a constant expression, the object is
1256       // automatic.
1257       for (const ShapeSpec &dim : object->shape()) {
1258         if (const auto &lb{dim.lbound().GetExplicit()}) {
1259           if (!evaluate::IsConstantExpr(*lb)) {
1260             return true;
1261           }
1262         }
1263         if (const auto &ub{dim.ubound().GetExplicit()}) {
1264           if (!evaluate::IsConstantExpr(*ub)) {
1265             return true;
1266           }
1267         }
1268       }
1269     }
1270   }
1271   return false;
1272 }
1273 
1274 bool IsSaved(const Symbol &original) {
1275   const Symbol &symbol{GetAssociationRoot(original)};
1276   const Scope &scope{symbol.owner()};
1277   auto scopeKind{scope.kind()};
1278   if (symbol.has<AssocEntityDetails>()) {
1279     return false; // ASSOCIATE(non-variable)
1280   } else if (scopeKind == Scope::Kind::DerivedType) {
1281     return false; // this is a component
1282   } else if (symbol.attrs().test(Attr::SAVE)) {
1283     return true; // explicit SAVE attribute
1284   } else if (IsDummy(symbol) || IsFunctionResult(symbol) ||
1285       IsAutomatic(symbol) || IsNamedConstant(symbol)) {
1286     return false;
1287   } else if (scopeKind == Scope::Kind::Module ||
1288       (scopeKind == Scope::Kind::MainProgram &&
1289           (symbol.attrs().test(Attr::TARGET) || evaluate::IsCoarray(symbol)))) {
1290     // 8.5.16p4
1291     // In main programs, implied SAVE matters only for pointer
1292     // initialization targets and coarrays.
1293     // BLOCK DATA entities must all be in COMMON,
1294     // which was checked above.
1295     return true;
1296   } else if (scope.kind() == Scope::Kind::Subprogram &&
1297       scope.context().languageFeatures().IsEnabled(
1298           common::LanguageFeature::DefaultSave) &&
1299       !(scope.symbol() && scope.symbol()->attrs().test(Attr::RECURSIVE))) {
1300     // -fno-automatic/-save/-Msave option applies to objects in
1301     // executable subprograms unless they are explicitly RECURSIVE.
1302     return true;
1303   } else if (symbol.test(Symbol::Flag::InDataStmt)) {
1304     return true;
1305   } else if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()};
1306              object && object->init()) {
1307     return true;
1308   } else if (IsProcedurePointer(symbol) &&
1309       symbol.get<ProcEntityDetails>().init()) {
1310     return true;
1311   } else if (scope.hasSAVE()) {
1312     return true; // bare SAVE statement
1313   } else if (const Symbol * block{FindCommonBlockContaining(symbol)};
1314              block && block->attrs().test(Attr::SAVE)) {
1315     return true; // in COMMON with SAVE
1316   } else {
1317     return false;
1318   }
1319 }
1320 
1321 bool IsDummy(const Symbol &symbol) {
1322   return std::visit(
1323       common::visitors{[](const EntityDetails &x) { return x.isDummy(); },
1324           [](const ObjectEntityDetails &x) { return x.isDummy(); },
1325           [](const ProcEntityDetails &x) { return x.isDummy(); },
1326           [](const SubprogramDetails &x) { return x.isDummy(); },
1327           [](const auto &) { return false; }},
1328       ResolveAssociations(symbol).details());
1329 }
1330 
1331 bool IsAssumedShape(const Symbol &symbol) {
1332   const Symbol &ultimate{ResolveAssociations(symbol)};
1333   const auto *object{ultimate.detailsIf<ObjectEntityDetails>()};
1334   return object && object->CanBeAssumedShape() &&
1335       !evaluate::IsAllocatableOrPointer(ultimate);
1336 }
1337 
1338 bool IsDeferredShape(const Symbol &symbol) {
1339   const Symbol &ultimate{ResolveAssociations(symbol)};
1340   const auto *object{ultimate.detailsIf<ObjectEntityDetails>()};
1341   return object && object->CanBeDeferredShape() &&
1342       evaluate::IsAllocatableOrPointer(ultimate);
1343 }
1344 
1345 bool IsFunctionResult(const Symbol &original) {
1346   const Symbol &symbol{GetAssociationRoot(original)};
1347   return (symbol.has<ObjectEntityDetails>() &&
1348              symbol.get<ObjectEntityDetails>().isFuncResult()) ||
1349       (symbol.has<ProcEntityDetails>() &&
1350           symbol.get<ProcEntityDetails>().isFuncResult());
1351 }
1352 
1353 bool IsKindTypeParameter(const Symbol &symbol) {
1354   const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()};
1355   return param && param->attr() == common::TypeParamAttr::Kind;
1356 }
1357 
1358 bool IsLenTypeParameter(const Symbol &symbol) {
1359   const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()};
1360   return param && param->attr() == common::TypeParamAttr::Len;
1361 }
1362 
1363 bool IsExtensibleType(const DerivedTypeSpec *derived) {
1364   return derived && !IsIsoCType(derived) &&
1365       !derived->typeSymbol().attrs().test(Attr::BIND_C) &&
1366       !derived->typeSymbol().get<DerivedTypeDetails>().sequence();
1367 }
1368 
1369 bool IsBuiltinDerivedType(const DerivedTypeSpec *derived, const char *name) {
1370   if (!derived) {
1371     return false;
1372   } else {
1373     const auto &symbol{derived->typeSymbol()};
1374     return &symbol.owner() == symbol.owner().context().GetBuiltinsScope() &&
1375         symbol.name() == "__builtin_"s + name;
1376   }
1377 }
1378 
1379 bool IsIsoCType(const DerivedTypeSpec *derived) {
1380   return IsBuiltinDerivedType(derived, "c_ptr") ||
1381       IsBuiltinDerivedType(derived, "c_funptr");
1382 }
1383 
1384 bool IsTeamType(const DerivedTypeSpec *derived) {
1385   return IsBuiltinDerivedType(derived, "team_type");
1386 }
1387 
1388 bool IsBadCoarrayType(const DerivedTypeSpec *derived) {
1389   return IsTeamType(derived) || IsIsoCType(derived);
1390 }
1391 
1392 bool IsEventTypeOrLockType(const DerivedTypeSpec *derivedTypeSpec) {
1393   return IsBuiltinDerivedType(derivedTypeSpec, "event_type") ||
1394       IsBuiltinDerivedType(derivedTypeSpec, "lock_type");
1395 }
1396 
1397 int CountLenParameters(const DerivedTypeSpec &type) {
1398   return std::count_if(type.parameters().begin(), type.parameters().end(),
1399       [](const auto &pair) { return pair.second.isLen(); });
1400 }
1401 
1402 int CountNonConstantLenParameters(const DerivedTypeSpec &type) {
1403   return std::count_if(
1404       type.parameters().begin(), type.parameters().end(), [](const auto &pair) {
1405         if (!pair.second.isLen()) {
1406           return false;
1407         } else if (const auto &expr{pair.second.GetExplicit()}) {
1408           return !IsConstantExpr(*expr);
1409         } else {
1410           return true;
1411         }
1412       });
1413 }
1414 
1415 // Are the type parameters of type1 compile-time compatible with the
1416 // corresponding kind type parameters of type2?  Return true if all constant
1417 // valued parameters are equal.
1418 // Used to check assignment statements and argument passing.  See 15.5.2.4(4)
1419 bool AreTypeParamCompatible(const semantics::DerivedTypeSpec &type1,
1420     const semantics::DerivedTypeSpec &type2) {
1421   for (const auto &[name, param1] : type1.parameters()) {
1422     if (semantics::MaybeIntExpr paramExpr1{param1.GetExplicit()}) {
1423       if (IsConstantExpr(*paramExpr1)) {
1424         const semantics::ParamValue *param2{type2.FindParameter(name)};
1425         if (param2) {
1426           if (semantics::MaybeIntExpr paramExpr2{param2->GetExplicit()}) {
1427             if (IsConstantExpr(*paramExpr2)) {
1428               if (ToInt64(*paramExpr1) != ToInt64(*paramExpr2)) {
1429                 return false;
1430               }
1431             }
1432           }
1433         }
1434       }
1435     }
1436   }
1437   return true;
1438 }
1439 
1440 const Symbol &GetUsedModule(const UseDetails &details) {
1441   return DEREF(details.symbol().owner().symbol());
1442 }
1443 
1444 static const Symbol *FindFunctionResult(
1445     const Symbol &original, UnorderedSymbolSet &seen) {
1446   const Symbol &root{GetAssociationRoot(original)};
1447   ;
1448   if (!seen.insert(root).second) {
1449     return nullptr; // don't loop
1450   }
1451   return std::visit(
1452       common::visitors{[](const SubprogramDetails &subp) {
1453                          return subp.isFunction() ? &subp.result() : nullptr;
1454                        },
1455           [&](const ProcEntityDetails &proc) {
1456             const Symbol *iface{proc.interface().symbol()};
1457             return iface ? FindFunctionResult(*iface, seen) : nullptr;
1458           },
1459           [&](const ProcBindingDetails &binding) {
1460             return FindFunctionResult(binding.symbol(), seen);
1461           },
1462           [](const auto &) -> const Symbol * { return nullptr; }},
1463       root.details());
1464 }
1465 
1466 const Symbol *FindFunctionResult(const Symbol &symbol) {
1467   UnorderedSymbolSet seen;
1468   return FindFunctionResult(symbol, seen);
1469 }
1470 
1471 // These are here in Evaluate/tools.cpp so that Evaluate can use
1472 // them; they cannot be defined in symbol.h due to the dependence
1473 // on Scope.
1474 
1475 bool SymbolSourcePositionCompare::operator()(
1476     const SymbolRef &x, const SymbolRef &y) const {
1477   return x->GetSemanticsContext().allCookedSources().Precedes(
1478       x->name(), y->name());
1479 }
1480 bool SymbolSourcePositionCompare::operator()(
1481     const MutableSymbolRef &x, const MutableSymbolRef &y) const {
1482   return x->GetSemanticsContext().allCookedSources().Precedes(
1483       x->name(), y->name());
1484 }
1485 
1486 SemanticsContext &Symbol::GetSemanticsContext() const {
1487   return DEREF(owner_).context();
1488 }
1489 
1490 } // namespace Fortran::semantics
1491