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 Expr<SomeType> Parenthesize(Expr<SomeType> &&expr) {
23   return std::visit(
24       [&](auto &&x) {
25         using T = std::decay_t<decltype(x)>;
26         if constexpr (common::HasMember<T, TypelessExpression> ||
27             std::is_same_v<T, Expr<SomeDerived>>) {
28           return expr; // no parentheses around typeless or derived type
29         } else {
30           return std::visit(
31               [](auto &&y) {
32                 using T = ResultType<decltype(y)>;
33                 return AsGenericExpr(Parentheses<T>{std::move(y)});
34               },
35               std::move(x.u));
36         }
37       },
38       std::move(expr.u));
39 }
40 
41 std::optional<DataRef> ExtractSubstringBase(const Substring &substring) {
42   return std::visit(
43       common::visitors{
44           [&](const DataRef &x) -> std::optional<DataRef> { return x; },
45           [&](const StaticDataObject::Pointer &) -> std::optional<DataRef> {
46             return std::nullopt;
47           },
48       },
49       substring.parent());
50 }
51 
52 // IsVariable()
53 
54 auto IsVariableHelper::operator()(const Symbol &symbol) const -> Result {
55   return !symbol.attrs().test(semantics::Attr::PARAMETER);
56 }
57 auto IsVariableHelper::operator()(const Component &x) const -> Result {
58   return (*this)(x.base());
59 }
60 auto IsVariableHelper::operator()(const ArrayRef &x) const -> Result {
61   return (*this)(x.base());
62 }
63 auto IsVariableHelper::operator()(const Substring &x) const -> Result {
64   return (*this)(x.GetBaseObject());
65 }
66 auto IsVariableHelper::operator()(const ProcedureDesignator &x) const
67     -> Result {
68   const Symbol *symbol{x.GetSymbol()};
69   return symbol && symbol->attrs().test(semantics::Attr::POINTER);
70 }
71 
72 // Conversions of COMPLEX component expressions to REAL.
73 ConvertRealOperandsResult ConvertRealOperands(
74     parser::ContextualMessages &messages, Expr<SomeType> &&x,
75     Expr<SomeType> &&y, int defaultRealKind) {
76   return std::visit(
77       common::visitors{
78           [&](Expr<SomeInteger> &&ix,
79               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
80             // Can happen in a CMPLX() constructor.  Per F'2018,
81             // both integer operands are converted to default REAL.
82             return {AsSameKindExprs<TypeCategory::Real>(
83                 ConvertToKind<TypeCategory::Real>(
84                     defaultRealKind, std::move(ix)),
85                 ConvertToKind<TypeCategory::Real>(
86                     defaultRealKind, std::move(iy)))};
87           },
88           [&](Expr<SomeInteger> &&ix,
89               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
90             return {AsSameKindExprs<TypeCategory::Real>(
91                 ConvertTo(ry, std::move(ix)), std::move(ry))};
92           },
93           [&](Expr<SomeReal> &&rx,
94               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
95             return {AsSameKindExprs<TypeCategory::Real>(
96                 std::move(rx), ConvertTo(rx, std::move(iy)))};
97           },
98           [&](Expr<SomeReal> &&rx,
99               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
100             return {AsSameKindExprs<TypeCategory::Real>(
101                 std::move(rx), std::move(ry))};
102           },
103           [&](Expr<SomeInteger> &&ix,
104               BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
105             return {AsSameKindExprs<TypeCategory::Real>(
106                 ConvertToKind<TypeCategory::Real>(
107                     defaultRealKind, std::move(ix)),
108                 ConvertToKind<TypeCategory::Real>(
109                     defaultRealKind, std::move(by)))};
110           },
111           [&](BOZLiteralConstant &&bx,
112               Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult {
113             return {AsSameKindExprs<TypeCategory::Real>(
114                 ConvertToKind<TypeCategory::Real>(
115                     defaultRealKind, std::move(bx)),
116                 ConvertToKind<TypeCategory::Real>(
117                     defaultRealKind, std::move(iy)))};
118           },
119           [&](Expr<SomeReal> &&rx,
120               BOZLiteralConstant &&by) -> ConvertRealOperandsResult {
121             return {AsSameKindExprs<TypeCategory::Real>(
122                 std::move(rx), ConvertTo(rx, std::move(by)))};
123           },
124           [&](BOZLiteralConstant &&bx,
125               Expr<SomeReal> &&ry) -> ConvertRealOperandsResult {
126             return {AsSameKindExprs<TypeCategory::Real>(
127                 ConvertTo(ry, std::move(bx)), std::move(ry))};
128           },
129           [&](auto &&, auto &&) -> ConvertRealOperandsResult { // C718
130             messages.Say("operands must be INTEGER or REAL"_err_en_US);
131             return std::nullopt;
132           },
133       },
134       std::move(x.u), std::move(y.u));
135 }
136 
137 // Helpers for NumericOperation and its subroutines below.
138 static std::optional<Expr<SomeType>> NoExpr() { return std::nullopt; }
139 
140 template <TypeCategory CAT>
141 std::optional<Expr<SomeType>> Package(Expr<SomeKind<CAT>> &&catExpr) {
142   return {AsGenericExpr(std::move(catExpr))};
143 }
144 template <TypeCategory CAT>
145 std::optional<Expr<SomeType>> Package(
146     std::optional<Expr<SomeKind<CAT>>> &&catExpr) {
147   if (catExpr) {
148     return {AsGenericExpr(std::move(*catExpr))};
149   }
150   return NoExpr();
151 }
152 
153 // Mixed REAL+INTEGER operations.  REAL**INTEGER is a special case that
154 // does not require conversion of the exponent expression.
155 template <template <typename> class OPR>
156 std::optional<Expr<SomeType>> MixedRealLeft(
157     Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
158   return Package(std::visit(
159       [&](auto &&rxk) -> Expr<SomeReal> {
160         using resultType = ResultType<decltype(rxk)>;
161         if constexpr (std::is_same_v<OPR<resultType>, Power<resultType>>) {
162           return AsCategoryExpr(
163               RealToIntPower<resultType>{std::move(rxk), std::move(iy)});
164         }
165         // G++ 8.1.0 emits bogus warnings about missing return statements if
166         // this statement is wrapped in an "else", as it should be.
167         return AsCategoryExpr(OPR<resultType>{
168             std::move(rxk), ConvertToType<resultType>(std::move(iy))});
169       },
170       std::move(rx.u)));
171 }
172 
173 std::optional<Expr<SomeComplex>> ConstructComplex(
174     parser::ContextualMessages &messages, Expr<SomeType> &&real,
175     Expr<SomeType> &&imaginary, int defaultRealKind) {
176   if (auto converted{ConvertRealOperands(
177           messages, std::move(real), std::move(imaginary), defaultRealKind)}) {
178     return {std::visit(
179         [](auto &&pair) {
180           return MakeComplex(std::move(pair[0]), std::move(pair[1]));
181         },
182         std::move(*converted))};
183   }
184   return std::nullopt;
185 }
186 
187 std::optional<Expr<SomeComplex>> ConstructComplex(
188     parser::ContextualMessages &messages, std::optional<Expr<SomeType>> &&real,
189     std::optional<Expr<SomeType>> &&imaginary, int defaultRealKind) {
190   if (auto parts{common::AllPresent(std::move(real), std::move(imaginary))}) {
191     return ConstructComplex(messages, std::get<0>(std::move(*parts)),
192         std::get<1>(std::move(*parts)), defaultRealKind);
193   }
194   return std::nullopt;
195 }
196 
197 Expr<SomeReal> GetComplexPart(const Expr<SomeComplex> &z, bool isImaginary) {
198   return std::visit(
199       [&](const auto &zk) {
200         static constexpr int kind{ResultType<decltype(zk)>::kind};
201         return AsCategoryExpr(ComplexComponent<kind>{isImaginary, zk});
202       },
203       z.u);
204 }
205 
206 // Convert REAL to COMPLEX of the same kind. Preserving the real operand kind
207 // and then applying complex operand promotion rules allows the result to have
208 // the highest precision of REAL and COMPLEX operands as required by Fortran
209 // 2018 10.9.1.3.
210 Expr<SomeComplex> PromoteRealToComplex(Expr<SomeReal> &&someX) {
211   return std::visit(
212       [](auto &&x) {
213         using RT = ResultType<decltype(x)>;
214         return AsCategoryExpr(ComplexConstructor<RT::kind>{
215             std::move(x), AsExpr(Constant<RT>{Scalar<RT>{}})});
216       },
217       std::move(someX.u));
218 }
219 
220 // Handle mixed COMPLEX+REAL (or INTEGER) operations in a better way
221 // than just converting the second operand to COMPLEX and performing the
222 // corresponding COMPLEX+COMPLEX operation.
223 template <template <typename> class OPR, TypeCategory RCAT>
224 std::optional<Expr<SomeType>> MixedComplexLeft(
225     parser::ContextualMessages &messages, Expr<SomeComplex> &&zx,
226     Expr<SomeKind<RCAT>> &&iry, int defaultRealKind) {
227   Expr<SomeReal> zr{GetComplexPart(zx, false)};
228   Expr<SomeReal> zi{GetComplexPart(zx, true)};
229   if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>> ||
230       std::is_same_v<OPR<LargestReal>, Subtract<LargestReal>>) {
231     // (a,b) + x -> (a+x, b)
232     // (a,b) - x -> (a-x, b)
233     if (std::optional<Expr<SomeType>> rr{
234             NumericOperation<OPR>(messages, AsGenericExpr(std::move(zr)),
235                 AsGenericExpr(std::move(iry)), defaultRealKind)}) {
236       return Package(ConstructComplex(messages, std::move(*rr),
237           AsGenericExpr(std::move(zi)), defaultRealKind));
238     }
239   } else if constexpr (std::is_same_v<OPR<LargestReal>,
240                            Multiply<LargestReal>> ||
241       std::is_same_v<OPR<LargestReal>, Divide<LargestReal>>) {
242     // (a,b) * x -> (a*x, b*x)
243     // (a,b) / x -> (a/x, b/x)
244     auto copy{iry};
245     auto rr{NumericOperation<OPR>(messages, AsGenericExpr(std::move(zr)),
246         AsGenericExpr(std::move(iry)), defaultRealKind)};
247     auto ri{NumericOperation<OPR>(messages, AsGenericExpr(std::move(zi)),
248         AsGenericExpr(std::move(copy)), defaultRealKind)};
249     if (auto parts{common::AllPresent(std::move(rr), std::move(ri))}) {
250       return Package(ConstructComplex(messages, std::get<0>(std::move(*parts)),
251           std::get<1>(std::move(*parts)), defaultRealKind));
252     }
253   } else if constexpr (RCAT == TypeCategory::Integer &&
254       std::is_same_v<OPR<LargestReal>, Power<LargestReal>>) {
255     // COMPLEX**INTEGER is a special case that doesn't convert the exponent.
256     static_assert(RCAT == TypeCategory::Integer);
257     return Package(std::visit(
258         [&](auto &&zxk) {
259           using Ty = ResultType<decltype(zxk)>;
260           return AsCategoryExpr(
261               AsExpr(RealToIntPower<Ty>{std::move(zxk), std::move(iry)}));
262         },
263         std::move(zx.u)));
264   } else if (defaultRealKind != 666) { // dodge unused parameter warning
265     // (a,b) ** x -> (a,b) ** (x,0)
266     if constexpr (RCAT == TypeCategory::Integer) {
267       Expr<SomeComplex> zy{ConvertTo(zx, std::move(iry))};
268       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
269     } else {
270       Expr<SomeComplex> zy{PromoteRealToComplex(std::move(iry))};
271       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
272     }
273   }
274   return NoExpr();
275 }
276 
277 // Mixed COMPLEX operations with the COMPLEX operand on the right.
278 //  x + (a,b) -> (x+a, b)
279 //  x - (a,b) -> (x-a, -b)
280 //  x * (a,b) -> (x*a, x*b)
281 //  x / (a,b) -> (x,0) / (a,b)   (and **)
282 template <template <typename> class OPR, TypeCategory LCAT>
283 std::optional<Expr<SomeType>> MixedComplexRight(
284     parser::ContextualMessages &messages, Expr<SomeKind<LCAT>> &&irx,
285     Expr<SomeComplex> &&zy, int defaultRealKind) {
286   if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>> ||
287       std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>>) {
288     // x + (a,b) -> (a,b) + x -> (a+x, b)
289     // x * (a,b) -> (a,b) * x -> (a*x, b*x)
290     return MixedComplexLeft<OPR, LCAT>(
291         messages, std::move(zy), std::move(irx), defaultRealKind);
292   } else if constexpr (std::is_same_v<OPR<LargestReal>,
293                            Subtract<LargestReal>>) {
294     // x - (a,b) -> (x-a, -b)
295     Expr<SomeReal> zr{GetComplexPart(zy, false)};
296     Expr<SomeReal> zi{GetComplexPart(zy, true)};
297     if (std::optional<Expr<SomeType>> rr{
298             NumericOperation<Subtract>(messages, AsGenericExpr(std::move(irx)),
299                 AsGenericExpr(std::move(zr)), defaultRealKind)}) {
300       return Package(ConstructComplex(messages, std::move(*rr),
301           AsGenericExpr(-std::move(zi)), defaultRealKind));
302     }
303   } else if (defaultRealKind != 666) { // dodge unused parameter warning
304     // x / (a,b) -> (x,0) / (a,b)
305     if constexpr (LCAT == TypeCategory::Integer) {
306       Expr<SomeComplex> zx{ConvertTo(zy, std::move(irx))};
307       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
308     } else {
309       Expr<SomeComplex> zx{PromoteRealToComplex(std::move(irx))};
310       return Package(PromoteAndCombine<OPR>(std::move(zx), std::move(zy)));
311     }
312   }
313   return NoExpr();
314 }
315 
316 // N.B. When a "typeless" BOZ literal constant appears as one (not both!) of
317 // the operands to a dyadic operation where one is permitted, it assumes the
318 // type and kind of the other operand.
319 template <template <typename> class OPR>
320 std::optional<Expr<SomeType>> NumericOperation(
321     parser::ContextualMessages &messages, Expr<SomeType> &&x,
322     Expr<SomeType> &&y, int defaultRealKind) {
323   return std::visit(
324       common::visitors{
325           [](Expr<SomeInteger> &&ix, Expr<SomeInteger> &&iy) {
326             return Package(PromoteAndCombine<OPR, TypeCategory::Integer>(
327                 std::move(ix), std::move(iy)));
328           },
329           [](Expr<SomeReal> &&rx, Expr<SomeReal> &&ry) {
330             return Package(PromoteAndCombine<OPR, TypeCategory::Real>(
331                 std::move(rx), std::move(ry)));
332           },
333           // Mixed REAL/INTEGER operations
334           [](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
335             return MixedRealLeft<OPR>(std::move(rx), std::move(iy));
336           },
337           [](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
338             return Package(std::visit(
339                 [&](auto &&ryk) -> Expr<SomeReal> {
340                   using resultType = ResultType<decltype(ryk)>;
341                   return AsCategoryExpr(
342                       OPR<resultType>{ConvertToType<resultType>(std::move(ix)),
343                           std::move(ryk)});
344                 },
345                 std::move(ry.u)));
346           },
347           // Homogeneous and mixed COMPLEX operations
348           [](Expr<SomeComplex> &&zx, Expr<SomeComplex> &&zy) {
349             return Package(PromoteAndCombine<OPR, TypeCategory::Complex>(
350                 std::move(zx), std::move(zy)));
351           },
352           [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) {
353             return MixedComplexLeft<OPR>(
354                 messages, std::move(zx), std::move(iy), defaultRealKind);
355           },
356           [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) {
357             return MixedComplexLeft<OPR>(
358                 messages, std::move(zx), std::move(ry), defaultRealKind);
359           },
360           [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) {
361             return MixedComplexRight<OPR>(
362                 messages, std::move(ix), std::move(zy), defaultRealKind);
363           },
364           [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) {
365             return MixedComplexRight<OPR>(
366                 messages, std::move(rx), std::move(zy), defaultRealKind);
367           },
368           // Operations with one typeless operand
369           [&](BOZLiteralConstant &&bx, Expr<SomeInteger> &&iy) {
370             return NumericOperation<OPR>(messages,
371                 AsGenericExpr(ConvertTo(iy, std::move(bx))), std::move(y),
372                 defaultRealKind);
373           },
374           [&](BOZLiteralConstant &&bx, Expr<SomeReal> &&ry) {
375             return NumericOperation<OPR>(messages,
376                 AsGenericExpr(ConvertTo(ry, std::move(bx))), std::move(y),
377                 defaultRealKind);
378           },
379           [&](Expr<SomeInteger> &&ix, BOZLiteralConstant &&by) {
380             return NumericOperation<OPR>(messages, std::move(x),
381                 AsGenericExpr(ConvertTo(ix, std::move(by))), defaultRealKind);
382           },
383           [&](Expr<SomeReal> &&rx, BOZLiteralConstant &&by) {
384             return NumericOperation<OPR>(messages, std::move(x),
385                 AsGenericExpr(ConvertTo(rx, std::move(by))), defaultRealKind);
386           },
387           // Default case
388           [&](auto &&, auto &&) {
389             // TODO: defined operator
390             messages.Say("non-numeric operands to numeric operation"_err_en_US);
391             return NoExpr();
392           },
393       },
394       std::move(x.u), std::move(y.u));
395 }
396 
397 template std::optional<Expr<SomeType>> NumericOperation<Power>(
398     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
399     int defaultRealKind);
400 template std::optional<Expr<SomeType>> NumericOperation<Multiply>(
401     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
402     int defaultRealKind);
403 template std::optional<Expr<SomeType>> NumericOperation<Divide>(
404     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
405     int defaultRealKind);
406 template std::optional<Expr<SomeType>> NumericOperation<Add>(
407     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
408     int defaultRealKind);
409 template std::optional<Expr<SomeType>> NumericOperation<Subtract>(
410     parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&,
411     int defaultRealKind);
412 
413 std::optional<Expr<SomeType>> Negation(
414     parser::ContextualMessages &messages, Expr<SomeType> &&x) {
415   return std::visit(
416       common::visitors{
417           [&](BOZLiteralConstant &&) {
418             messages.Say("BOZ literal cannot be negated"_err_en_US);
419             return NoExpr();
420           },
421           [&](NullPointer &&) {
422             messages.Say("NULL() cannot be negated"_err_en_US);
423             return NoExpr();
424           },
425           [&](ProcedureDesignator &&) {
426             messages.Say("Subroutine cannot be negated"_err_en_US);
427             return NoExpr();
428           },
429           [&](ProcedureRef &&) {
430             messages.Say("Pointer to subroutine cannot be negated"_err_en_US);
431             return NoExpr();
432           },
433           [&](Expr<SomeInteger> &&x) { return Package(-std::move(x)); },
434           [&](Expr<SomeReal> &&x) { return Package(-std::move(x)); },
435           [&](Expr<SomeComplex> &&x) { return Package(-std::move(x)); },
436           [&](Expr<SomeCharacter> &&) {
437             // TODO: defined operator
438             messages.Say("CHARACTER cannot be negated"_err_en_US);
439             return NoExpr();
440           },
441           [&](Expr<SomeLogical> &&) {
442             // TODO: defined operator
443             messages.Say("LOGICAL cannot be negated"_err_en_US);
444             return NoExpr();
445           },
446           [&](Expr<SomeDerived> &&) {
447             // TODO: defined operator
448             messages.Say("Operand cannot be negated"_err_en_US);
449             return NoExpr();
450           },
451       },
452       std::move(x.u));
453 }
454 
455 Expr<SomeLogical> LogicalNegation(Expr<SomeLogical> &&x) {
456   return std::visit(
457       [](auto &&xk) { return AsCategoryExpr(LogicalNegation(std::move(xk))); },
458       std::move(x.u));
459 }
460 
461 template <typename T>
462 Expr<LogicalResult> PackageRelation(
463     RelationalOperator opr, Expr<T> &&x, Expr<T> &&y) {
464   static_assert(IsSpecificIntrinsicType<T>);
465   return Expr<LogicalResult>{
466       Relational<SomeType>{Relational<T>{opr, std::move(x), std::move(y)}}};
467 }
468 
469 template <TypeCategory CAT>
470 Expr<LogicalResult> PromoteAndRelate(
471     RelationalOperator opr, Expr<SomeKind<CAT>> &&x, Expr<SomeKind<CAT>> &&y) {
472   return std::visit(
473       [=](auto &&xy) {
474         return PackageRelation(opr, std::move(xy[0]), std::move(xy[1]));
475       },
476       AsSameKindExprs(std::move(x), std::move(y)));
477 }
478 
479 std::optional<Expr<LogicalResult>> Relate(parser::ContextualMessages &messages,
480     RelationalOperator opr, Expr<SomeType> &&x, Expr<SomeType> &&y) {
481   return std::visit(
482       common::visitors{
483           [=](Expr<SomeInteger> &&ix,
484               Expr<SomeInteger> &&iy) -> std::optional<Expr<LogicalResult>> {
485             return PromoteAndRelate(opr, std::move(ix), std::move(iy));
486           },
487           [=](Expr<SomeReal> &&rx,
488               Expr<SomeReal> &&ry) -> std::optional<Expr<LogicalResult>> {
489             return PromoteAndRelate(opr, std::move(rx), std::move(ry));
490           },
491           [&](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) {
492             return Relate(messages, opr, std::move(x),
493                 AsGenericExpr(ConvertTo(rx, std::move(iy))));
494           },
495           [&](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) {
496             return Relate(messages, opr,
497                 AsGenericExpr(ConvertTo(ry, std::move(ix))), std::move(y));
498           },
499           [&](Expr<SomeComplex> &&zx,
500               Expr<SomeComplex> &&zy) -> std::optional<Expr<LogicalResult>> {
501             if (opr == RelationalOperator::EQ ||
502                 opr == RelationalOperator::NE) {
503               return PromoteAndRelate(opr, std::move(zx), std::move(zy));
504             } else {
505               messages.Say(
506                   "COMPLEX data may be compared only for equality"_err_en_US);
507               return std::nullopt;
508             }
509           },
510           [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) {
511             return Relate(messages, opr, std::move(x),
512                 AsGenericExpr(ConvertTo(zx, std::move(iy))));
513           },
514           [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) {
515             return Relate(messages, opr, std::move(x),
516                 AsGenericExpr(ConvertTo(zx, std::move(ry))));
517           },
518           [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) {
519             return Relate(messages, opr,
520                 AsGenericExpr(ConvertTo(zy, std::move(ix))), std::move(y));
521           },
522           [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) {
523             return Relate(messages, opr,
524                 AsGenericExpr(ConvertTo(zy, std::move(rx))), std::move(y));
525           },
526           [&](Expr<SomeCharacter> &&cx, Expr<SomeCharacter> &&cy) {
527             return std::visit(
528                 [&](auto &&cxk,
529                     auto &&cyk) -> std::optional<Expr<LogicalResult>> {
530                   using Ty = ResultType<decltype(cxk)>;
531                   if constexpr (std::is_same_v<Ty, ResultType<decltype(cyk)>>) {
532                     return PackageRelation(opr, std::move(cxk), std::move(cyk));
533                   } else {
534                     messages.Say(
535                         "CHARACTER operands do not have same KIND"_err_en_US);
536                     return std::nullopt;
537                   }
538                 },
539                 std::move(cx.u), std::move(cy.u));
540           },
541           // Default case
542           [&](auto &&, auto &&) {
543             DIE("invalid types for relational operator");
544             return std::optional<Expr<LogicalResult>>{};
545           },
546       },
547       std::move(x.u), std::move(y.u));
548 }
549 
550 Expr<SomeLogical> BinaryLogicalOperation(
551     LogicalOperator opr, Expr<SomeLogical> &&x, Expr<SomeLogical> &&y) {
552   CHECK(opr != LogicalOperator::Not);
553   return std::visit(
554       [=](auto &&xy) {
555         using Ty = ResultType<decltype(xy[0])>;
556         return Expr<SomeLogical>{BinaryLogicalOperation<Ty::kind>(
557             opr, std::move(xy[0]), std::move(xy[1]))};
558       },
559       AsSameKindExprs(std::move(x), std::move(y)));
560 }
561 
562 template <TypeCategory TO>
563 std::optional<Expr<SomeType>> ConvertToNumeric(int kind, Expr<SomeType> &&x) {
564   static_assert(common::IsNumericTypeCategory(TO));
565   return std::visit(
566       [=](auto &&cx) -> std::optional<Expr<SomeType>> {
567         using cxType = std::decay_t<decltype(cx)>;
568         if constexpr (!common::HasMember<cxType, TypelessExpression>) {
569           if constexpr (IsNumericTypeCategory(ResultType<cxType>::category)) {
570             return Expr<SomeType>{ConvertToKind<TO>(kind, std::move(cx))};
571           }
572         }
573         return std::nullopt;
574       },
575       std::move(x.u));
576 }
577 
578 std::optional<Expr<SomeType>> ConvertToType(
579     const DynamicType &type, Expr<SomeType> &&x) {
580   switch (type.category()) {
581   case TypeCategory::Integer:
582     if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
583       // Extension to C7109: allow BOZ literals to appear in integer contexts
584       // when the type is unambiguous.
585       return Expr<SomeType>{
586           ConvertToKind<TypeCategory::Integer>(type.kind(), std::move(*boz))};
587     }
588     return ConvertToNumeric<TypeCategory::Integer>(type.kind(), std::move(x));
589   case TypeCategory::Real:
590     if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) {
591       return Expr<SomeType>{
592           ConvertToKind<TypeCategory::Real>(type.kind(), std::move(*boz))};
593     }
594     return ConvertToNumeric<TypeCategory::Real>(type.kind(), std::move(x));
595   case TypeCategory::Complex:
596     return ConvertToNumeric<TypeCategory::Complex>(type.kind(), std::move(x));
597   case TypeCategory::Character:
598     if (auto *cx{UnwrapExpr<Expr<SomeCharacter>>(x)}) {
599       auto converted{
600           ConvertToKind<TypeCategory::Character>(type.kind(), std::move(*cx))};
601       if (type.charLength()) {
602         if (const auto &len{type.charLength()->GetExplicit()}) {
603           Expr<SomeInteger> lenParam{*len};
604           Expr<SubscriptInteger> length{Convert<SubscriptInteger>{lenParam}};
605           converted = std::visit(
606               [&](auto &&x) {
607                 using Ty = std::decay_t<decltype(x)>;
608                 using CharacterType = typename Ty::Result;
609                 return Expr<SomeCharacter>{
610                     Expr<CharacterType>{SetLength<CharacterType::kind>{
611                         std::move(x), std::move(length)}}};
612               },
613               std::move(converted.u));
614         }
615       }
616       return Expr<SomeType>{std::move(converted)};
617     }
618     break;
619   case TypeCategory::Logical:
620     if (auto *cx{UnwrapExpr<Expr<SomeLogical>>(x)}) {
621       return Expr<SomeType>{
622           ConvertToKind<TypeCategory::Logical>(type.kind(), std::move(*cx))};
623     }
624     break;
625   case TypeCategory::Derived:
626     if (auto fromType{x.GetType()}) {
627       if (type == *fromType) {
628         return std::move(x);
629       }
630     }
631     break;
632   }
633   return std::nullopt;
634 }
635 
636 std::optional<Expr<SomeType>> ConvertToType(
637     const DynamicType &to, std::optional<Expr<SomeType>> &&x) {
638   if (x) {
639     return ConvertToType(to, std::move(*x));
640   } else {
641     return std::nullopt;
642   }
643 }
644 
645 std::optional<Expr<SomeType>> ConvertToType(
646     const Symbol &symbol, Expr<SomeType> &&x) {
647   if (auto symType{DynamicType::From(symbol)}) {
648     return ConvertToType(*symType, std::move(x));
649   }
650   return std::nullopt;
651 }
652 
653 std::optional<Expr<SomeType>> ConvertToType(
654     const Symbol &to, std::optional<Expr<SomeType>> &&x) {
655   if (x) {
656     return ConvertToType(to, std::move(*x));
657   } else {
658     return std::nullopt;
659   }
660 }
661 
662 bool IsAssumedRank(const Symbol &symbol0) {
663   const Symbol &symbol{ResolveAssociations(symbol0)};
664   if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) {
665     return details->IsAssumedRank();
666   } else {
667     return false;
668   }
669 }
670 
671 bool IsAssumedRank(const ActualArgument &arg) {
672   if (const auto *expr{arg.UnwrapExpr()}) {
673     return IsAssumedRank(*expr);
674   } else {
675     const Symbol *assumedTypeDummy{arg.GetAssumedTypeDummy()};
676     CHECK(assumedTypeDummy);
677     return IsAssumedRank(*assumedTypeDummy);
678   }
679 }
680 
681 bool IsProcedure(const Expr<SomeType> &expr) {
682   return std::holds_alternative<ProcedureDesignator>(expr.u);
683 }
684 bool IsFunction(const Expr<SomeType> &expr) {
685   const auto *designator{std::get_if<ProcedureDesignator>(&expr.u)};
686   return designator && designator->GetType().has_value();
687 }
688 
689 bool IsProcedurePointer(const Expr<SomeType> &expr) {
690   return std::visit(common::visitors{
691                         [](const NullPointer &) { return true; },
692                         [](const ProcedureDesignator &) { return true; },
693                         [](const ProcedureRef &) { return true; },
694                         [](const auto &) { return false; },
695                     },
696       expr.u);
697 }
698 
699 // IsNullPointer()
700 struct IsNullPointerHelper : public AllTraverse<IsNullPointerHelper, false> {
701   using Base = AllTraverse<IsNullPointerHelper, false>;
702   IsNullPointerHelper() : Base(*this) {}
703   using Base::operator();
704   bool operator()(const ProcedureRef &call) const {
705     auto *intrinsic{call.proc().GetSpecificIntrinsic()};
706     return intrinsic &&
707         intrinsic->characteristics.value().attrs.test(
708             characteristics::Procedure::Attr::NullPointer);
709   }
710   bool operator()(const NullPointer &) const { return true; }
711 };
712 bool IsNullPointer(const Expr<SomeType> &expr) {
713   return IsNullPointerHelper{}(expr);
714 }
715 
716 // GetSymbolVector()
717 auto GetSymbolVectorHelper::operator()(const Symbol &x) const -> Result {
718   if (const auto *details{x.detailsIf<semantics::AssocEntityDetails>()}) {
719     return (*this)(details->expr());
720   } else {
721     return {x.GetUltimate()};
722   }
723 }
724 auto GetSymbolVectorHelper::operator()(const Component &x) const -> Result {
725   Result result{(*this)(x.base())};
726   result.emplace_back(x.GetLastSymbol());
727   return result;
728 }
729 auto GetSymbolVectorHelper::operator()(const ArrayRef &x) const -> Result {
730   return GetSymbolVector(x.base());
731 }
732 auto GetSymbolVectorHelper::operator()(const CoarrayRef &x) const -> Result {
733   return x.base();
734 }
735 
736 const Symbol *GetLastTarget(const SymbolVector &symbols) {
737   auto end{std::crend(symbols)};
738   // N.B. Neither clang nor g++ recognizes "symbols.crbegin()" here.
739   auto iter{std::find_if(std::crbegin(symbols), end, [](const Symbol &x) {
740     return x.attrs().HasAny(
741         {semantics::Attr::POINTER, semantics::Attr::TARGET});
742   })};
743   return iter == end ? nullptr : &**iter;
744 }
745 
746 const Symbol &ResolveAssociations(const Symbol &symbol) {
747   if (const auto *details{symbol.detailsIf<semantics::AssocEntityDetails>()}) {
748     if (const Symbol * nested{UnwrapWholeSymbolDataRef(details->expr())}) {
749       return ResolveAssociations(*nested);
750     }
751   }
752   return symbol.GetUltimate();
753 }
754 
755 struct CollectSymbolsHelper
756     : public SetTraverse<CollectSymbolsHelper, semantics::SymbolSet> {
757   using Base = SetTraverse<CollectSymbolsHelper, semantics::SymbolSet>;
758   CollectSymbolsHelper() : Base{*this} {}
759   using Base::operator();
760   semantics::SymbolSet operator()(const Symbol &symbol) const {
761     return {symbol};
762   }
763 };
764 template <typename A> semantics::SymbolSet CollectSymbols(const A &x) {
765   return CollectSymbolsHelper{}(x);
766 }
767 template semantics::SymbolSet CollectSymbols(const Expr<SomeType> &);
768 template semantics::SymbolSet CollectSymbols(const Expr<SomeInteger> &);
769 template semantics::SymbolSet CollectSymbols(const Expr<SubscriptInteger> &);
770 
771 // HasVectorSubscript()
772 struct HasVectorSubscriptHelper : public AnyTraverse<HasVectorSubscriptHelper> {
773   using Base = AnyTraverse<HasVectorSubscriptHelper>;
774   HasVectorSubscriptHelper() : Base{*this} {}
775   using Base::operator();
776   bool operator()(const Subscript &ss) const {
777     return !std::holds_alternative<Triplet>(ss.u) && ss.Rank() > 0;
778   }
779   bool operator()(const ProcedureRef &) const {
780     return false; // don't descend into function call arguments
781   }
782 };
783 
784 bool HasVectorSubscript(const Expr<SomeType> &expr) {
785   return HasVectorSubscriptHelper{}(expr);
786 }
787 
788 parser::Message *AttachDeclaration(
789     parser::Message &message, const Symbol &symbol) {
790   const Symbol *unhosted{&symbol};
791   while (
792       const auto *assoc{unhosted->detailsIf<semantics::HostAssocDetails>()}) {
793     unhosted = &assoc->symbol();
794   }
795   if (const auto *binding{
796           unhosted->detailsIf<semantics::ProcBindingDetails>()}) {
797     if (binding->symbol().name() != symbol.name()) {
798       message.Attach(binding->symbol().name(),
799           "Procedure '%s' of type '%s' is bound to '%s'"_en_US, symbol.name(),
800           symbol.owner().GetName().value(), binding->symbol().name());
801       return &message;
802     }
803     unhosted = &binding->symbol();
804   }
805   if (const auto *use{symbol.detailsIf<semantics::UseDetails>()}) {
806     message.Attach(use->location(),
807         "'%s' is USE-associated with '%s' in module '%s'"_en_US, symbol.name(),
808         unhosted->name(), GetUsedModule(*use).name());
809   } else {
810     message.Attach(
811         unhosted->name(), "Declaration of '%s'"_en_US, unhosted->name());
812   }
813   return &message;
814 }
815 
816 parser::Message *AttachDeclaration(
817     parser::Message *message, const Symbol &symbol) {
818   if (message) {
819     AttachDeclaration(*message, symbol);
820   }
821   return message;
822 }
823 
824 class FindImpureCallHelper
825     : public AnyTraverse<FindImpureCallHelper, std::optional<std::string>> {
826   using Result = std::optional<std::string>;
827   using Base = AnyTraverse<FindImpureCallHelper, Result>;
828 
829 public:
830   explicit FindImpureCallHelper(const IntrinsicProcTable &intrinsics)
831       : Base{*this}, intrinsics_{intrinsics} {}
832   using Base::operator();
833   Result operator()(const ProcedureRef &call) const {
834     if (auto chars{characteristics::Procedure::Characterize(
835             call.proc(), intrinsics_)}) {
836       if (chars->attrs.test(characteristics::Procedure::Attr::Pure)) {
837         return (*this)(call.arguments());
838       }
839     }
840     return call.proc().GetName();
841   }
842 
843 private:
844   const IntrinsicProcTable &intrinsics_;
845 };
846 
847 std::optional<std::string> FindImpureCall(
848     const IntrinsicProcTable &intrinsics, const Expr<SomeType> &expr) {
849   return FindImpureCallHelper{intrinsics}(expr);
850 }
851 std::optional<std::string> FindImpureCall(
852     const IntrinsicProcTable &intrinsics, const ProcedureRef &proc) {
853   return FindImpureCallHelper{intrinsics}(proc);
854 }
855 
856 // Compare procedure characteristics for equality except that lhs may be
857 // Pure or Elemental when rhs is not.
858 static bool CharacteristicsMatch(const characteristics::Procedure &lhs,
859     const characteristics::Procedure &rhs) {
860   using Attr = characteristics::Procedure::Attr;
861   auto lhsAttrs{rhs.attrs};
862   lhsAttrs.set(
863       Attr::Pure, lhs.attrs.test(Attr::Pure) | rhs.attrs.test(Attr::Pure));
864   lhsAttrs.set(Attr::Elemental,
865       lhs.attrs.test(Attr::Elemental) | rhs.attrs.test(Attr::Elemental));
866   return lhsAttrs == rhs.attrs && lhs.functionResult == rhs.functionResult &&
867       lhs.dummyArguments == rhs.dummyArguments;
868 }
869 
870 // Common handling for procedure pointer compatibility of left- and right-hand
871 // sides.  Returns nullopt if they're compatible.  Otherwise, it returns a
872 // message that needs to be augmented by the names of the left and right sides
873 std::optional<parser::MessageFixedText> CheckProcCompatibility(bool isCall,
874     const std::optional<characteristics::Procedure> &lhsProcedure,
875     const characteristics::Procedure *rhsProcedure) {
876   std::optional<parser::MessageFixedText> msg;
877   if (!lhsProcedure) {
878     msg = "In assignment to object %s, the target '%s' is a procedure"
879           " designator"_err_en_US;
880   } else if (!rhsProcedure) {
881     msg = "In assignment to procedure %s, the characteristics of the target"
882           " procedure '%s' could not be determined"_err_en_US;
883   } else if (CharacteristicsMatch(*lhsProcedure, *rhsProcedure)) {
884     // OK
885   } else if (isCall) {
886     msg = "Procedure %s associated with result of reference to function '%s'"
887           " that is an incompatible procedure pointer"_err_en_US;
888   } else if (lhsProcedure->IsPure() && !rhsProcedure->IsPure()) {
889     msg = "PURE procedure %s may not be associated with non-PURE"
890           " procedure designator '%s'"_err_en_US;
891   } else if (lhsProcedure->IsFunction() && !rhsProcedure->IsFunction()) {
892     msg = "Function %s may not be associated with subroutine"
893           " designator '%s'"_err_en_US;
894   } else if (!lhsProcedure->IsFunction() && rhsProcedure->IsFunction()) {
895     msg = "Subroutine %s may not be associated with function"
896           " designator '%s'"_err_en_US;
897   } else if (lhsProcedure->HasExplicitInterface() &&
898       !rhsProcedure->HasExplicitInterface()) {
899     msg = "Procedure %s with explicit interface may not be associated with"
900           " procedure designator '%s' with implicit interface"_err_en_US;
901   } else if (!lhsProcedure->HasExplicitInterface() &&
902       rhsProcedure->HasExplicitInterface()) {
903     msg = "Procedure %s with implicit interface may not be associated with"
904           " procedure designator '%s' with explicit interface"_err_en_US;
905   } else {
906     msg = "Procedure %s associated with incompatible procedure"
907           " designator '%s'"_err_en_US;
908   }
909   return msg;
910 }
911 
912 } // namespace Fortran::evaluate
913 
914 namespace Fortran::semantics {
915 
916 // When a construct association maps to a variable, and that variable
917 // is not an array with a vector-valued subscript, return the base
918 // Symbol of that variable, else nullptr.  Descends into other construct
919 // associations when one associations maps to another.
920 static const Symbol *GetAssociatedVariable(
921     const semantics::AssocEntityDetails &details) {
922   if (const auto &expr{details.expr()}) {
923     if (IsVariable(*expr) && !HasVectorSubscript(*expr)) {
924       if (const Symbol * varSymbol{GetFirstSymbol(*expr)}) {
925         return GetAssociationRoot(*varSymbol);
926       }
927     }
928   }
929   return nullptr;
930 }
931 
932 const Symbol *GetAssociationRoot(const Symbol &symbol) {
933   const Symbol &ultimate{symbol.GetUltimate()};
934   const auto *details{ultimate.detailsIf<semantics::AssocEntityDetails>()};
935   return details ? GetAssociatedVariable(*details) : &ultimate;
936 }
937 
938 bool IsVariableName(const Symbol &symbol) {
939   const Symbol *root{GetAssociationRoot(symbol)};
940   return root && root->has<ObjectEntityDetails>() && !IsNamedConstant(*root);
941 }
942 
943 bool IsPureProcedure(const Symbol &symbol) {
944   if (const auto *procDetails{symbol.detailsIf<ProcEntityDetails>()}) {
945     if (const Symbol * procInterface{procDetails->interface().symbol()}) {
946       // procedure component with a pure interface
947       return IsPureProcedure(*procInterface);
948     }
949   } else if (const auto *details{symbol.detailsIf<ProcBindingDetails>()}) {
950     return IsPureProcedure(details->symbol());
951   } else if (!IsProcedure(symbol)) {
952     return false;
953   }
954   if (IsStmtFunction(symbol)) {
955     // Section 15.7(1) states that a statement function is PURE if it does not
956     // reference an IMPURE procedure or a VOLATILE variable
957     if (const auto &expr{symbol.get<SubprogramDetails>().stmtFunction()}) {
958       for (const SymbolRef &ref : evaluate::CollectSymbols(*expr)) {
959         if (IsFunction(*ref) && !IsPureProcedure(*ref)) {
960           return false;
961         }
962         const Symbol *root{GetAssociationRoot(*ref)};
963         if (root && root->attrs().test(Attr::VOLATILE)) {
964           return false;
965         }
966       }
967     }
968     return true; // statement function was not found to be impure
969   }
970   return symbol.attrs().test(Attr::PURE) ||
971       (symbol.attrs().test(Attr::ELEMENTAL) &&
972           !symbol.attrs().test(Attr::IMPURE));
973 }
974 
975 bool IsPureProcedure(const Scope &scope) {
976   const Symbol *symbol{scope.GetSymbol()};
977   return symbol && IsPureProcedure(*symbol);
978 }
979 
980 bool IsFunction(const Symbol &symbol) {
981   return std::visit(
982       common::visitors{
983           [](const SubprogramDetails &x) { return x.isFunction(); },
984           [&](const SubprogramNameDetails &) {
985             return symbol.test(Symbol::Flag::Function);
986           },
987           [](const ProcEntityDetails &x) {
988             const auto &ifc{x.interface()};
989             return ifc.type() || (ifc.symbol() && IsFunction(*ifc.symbol()));
990           },
991           [](const ProcBindingDetails &x) { return IsFunction(x.symbol()); },
992           [](const UseDetails &x) { return IsFunction(x.symbol()); },
993           [](const auto &) { return false; },
994       },
995       symbol.details());
996 }
997 
998 bool IsProcedure(const Symbol &symbol) {
999   return std::visit(
1000       common::visitors{
1001           [](const SubprogramDetails &) { return true; },
1002           [](const SubprogramNameDetails &) { return true; },
1003           [](const ProcEntityDetails &) { return true; },
1004           [](const GenericDetails &) { return true; },
1005           [](const ProcBindingDetails &) { return true; },
1006           [](const UseDetails &x) { return IsProcedure(x.symbol()); },
1007           [](const auto &) { return false; },
1008       },
1009       symbol.details());
1010 }
1011 
1012 const Symbol *FindCommonBlockContaining(const Symbol &object) {
1013   const auto *details{object.detailsIf<ObjectEntityDetails>()};
1014   return details ? details->commonBlock() : nullptr;
1015 }
1016 
1017 bool IsProcedurePointer(const Symbol &symbol) {
1018   return symbol.has<ProcEntityDetails>() && IsPointer(symbol);
1019 }
1020 
1021 bool IsSaved(const Symbol &original) {
1022   if (const Symbol * root{GetAssociationRoot(original)}) {
1023     const Symbol &symbol{*root};
1024     const Scope *scope{&symbol.owner()};
1025     auto scopeKind{scope->kind()};
1026     if (scopeKind == Scope::Kind::Module) {
1027       return true; // BLOCK DATA entities must all be in COMMON, handled below
1028     } else if (symbol.attrs().test(Attr::SAVE)) {
1029       return true;
1030     } else if (scopeKind == Scope::Kind::DerivedType) {
1031       return false; // this is a component
1032     } else if (IsNamedConstant(symbol)) {
1033       return false;
1034     } else if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()};
1035                object && object->init()) {
1036       return true;
1037     } else if (IsProcedurePointer(symbol) &&
1038         symbol.get<ProcEntityDetails>().init()) {
1039       return true;
1040     } else if (const Symbol * block{FindCommonBlockContaining(symbol)};
1041                block && block->attrs().test(Attr::SAVE)) {
1042       return true;
1043     } else if (IsDummy(symbol) || IsFunctionResult(symbol)) {
1044       return false;
1045     } else if (scope->hasSAVE() ) {
1046       return true;
1047     }
1048   }
1049   return false;
1050 }
1051 
1052 bool IsDummy(const Symbol &symbol) {
1053   return std::visit(
1054       common::visitors{[](const EntityDetails &x) { return x.isDummy(); },
1055           [](const ObjectEntityDetails &x) { return x.isDummy(); },
1056           [](const ProcEntityDetails &x) { return x.isDummy(); },
1057           [](const HostAssocDetails &x) { return IsDummy(x.symbol()); },
1058           [](const auto &) { return false; }},
1059       symbol.details());
1060 }
1061 
1062 bool IsFunctionResult(const Symbol &symbol) {
1063   return (symbol.has<ObjectEntityDetails>() &&
1064              symbol.get<ObjectEntityDetails>().isFuncResult()) ||
1065       (symbol.has<ProcEntityDetails>() &&
1066           symbol.get<ProcEntityDetails>().isFuncResult());
1067 }
1068 
1069 int CountLenParameters(const DerivedTypeSpec &type) {
1070   return std::count_if(type.parameters().begin(), type.parameters().end(),
1071       [](const auto &pair) { return pair.second.isLen(); });
1072 }
1073 
1074 int CountNonConstantLenParameters(const DerivedTypeSpec &type) {
1075   return std::count_if(
1076       type.parameters().begin(), type.parameters().end(), [](const auto &pair) {
1077         if (!pair.second.isLen()) {
1078           return false;
1079         } else if (const auto &expr{pair.second.GetExplicit()}) {
1080           return !IsConstantExpr(*expr);
1081         } else {
1082           return true;
1083         }
1084       });
1085 }
1086 
1087 const Symbol &GetUsedModule(const UseDetails &details) {
1088   return DEREF(details.symbol().owner().symbol());
1089 }
1090 
1091 } // namespace Fortran::semantics
1092