1 //===-- lib/Evaluate/check-expression.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/check-expression.h"
10 #include "flang/Evaluate/characteristics.h"
11 #include "flang/Evaluate/intrinsics.h"
12 #include "flang/Evaluate/traverse.h"
13 #include "flang/Evaluate/type.h"
14 #include "flang/Semantics/symbol.h"
15 #include "flang/Semantics/tools.h"
16 #include <set>
17 #include <string>
18 
19 namespace Fortran::evaluate {
20 
21 // Constant expression predicates IsConstantExpr() & IsScopeInvariantExpr().
22 // This code determines whether an expression is a "constant expression"
23 // in the sense of section 10.1.12.  This is not the same thing as being
24 // able to fold it (yet) into a known constant value; specifically,
25 // the expression may reference derived type kind parameters whose values
26 // are not yet known.
27 //
28 // The variant form (IsScopeInvariantExpr()) also accepts symbols that are
29 // INTENT(IN) dummy arguments without the VALUE attribute.
30 template <bool INVARIANT>
31 class IsConstantExprHelper
32     : public AllTraverse<IsConstantExprHelper<INVARIANT>, true> {
33 public:
34   using Base = AllTraverse<IsConstantExprHelper, true>;
35   IsConstantExprHelper() : Base{*this} {}
36   using Base::operator();
37 
38   // A missing expression is not considered to be constant.
39   template <typename A> bool operator()(const std::optional<A> &x) const {
40     return x && (*this)(*x);
41   }
42 
43   bool operator()(const TypeParamInquiry &inq) const {
44     return INVARIANT || semantics::IsKindTypeParameter(inq.parameter());
45   }
46   bool operator()(const semantics::Symbol &symbol) const {
47     const auto &ultimate{GetAssociationRoot(symbol)};
48     return IsNamedConstant(ultimate) || IsImpliedDoIndex(ultimate) ||
49         IsInitialProcedureTarget(ultimate) ||
50         ultimate.has<semantics::TypeParamDetails>() ||
51         (INVARIANT && IsIntentIn(symbol) &&
52             !symbol.attrs().test(semantics::Attr::VALUE));
53   }
54   bool operator()(const CoarrayRef &) const { return false; }
55   bool operator()(const semantics::ParamValue &param) const {
56     return param.isExplicit() && (*this)(param.GetExplicit());
57   }
58   bool operator()(const ProcedureRef &) const;
59   bool operator()(const StructureConstructor &constructor) const {
60     for (const auto &[symRef, expr] : constructor) {
61       if (!IsConstantStructureConstructorComponent(*symRef, expr.value())) {
62         return false;
63       }
64     }
65     return true;
66   }
67   bool operator()(const Component &component) const {
68     return (*this)(component.base());
69   }
70   // Forbid integer division by zero in constants.
71   template <int KIND>
72   bool operator()(
73       const Divide<Type<TypeCategory::Integer, KIND>> &division) const {
74     using T = Type<TypeCategory::Integer, KIND>;
75     if (const auto divisor{GetScalarConstantValue<T>(division.right())}) {
76       return !divisor->IsZero() && (*this)(division.left());
77     } else {
78       return false;
79     }
80   }
81 
82   bool operator()(const Constant<SomeDerived> &) const { return true; }
83   bool operator()(const DescriptorInquiry &x) const {
84     const Symbol &sym{x.base().GetLastSymbol()};
85     return INVARIANT && !IsAllocatable(sym) &&
86         (!IsDummy(sym) ||
87             (IsIntentIn(sym) && !sym.attrs().test(semantics::Attr::VALUE)));
88   }
89 
90 private:
91   bool IsConstantStructureConstructorComponent(
92       const Symbol &, const Expr<SomeType> &) const;
93   bool IsConstantExprShape(const Shape &) const;
94 };
95 
96 template <bool INVARIANT>
97 bool IsConstantExprHelper<INVARIANT>::IsConstantStructureConstructorComponent(
98     const Symbol &component, const Expr<SomeType> &expr) const {
99   if (IsAllocatable(component)) {
100     return IsNullPointer(expr);
101   } else if (IsPointer(component)) {
102     return IsNullPointer(expr) || IsInitialDataTarget(expr) ||
103         IsInitialProcedureTarget(expr);
104   } else {
105     return (*this)(expr);
106   }
107 }
108 
109 template <bool INVARIANT>
110 bool IsConstantExprHelper<INVARIANT>::operator()(
111     const ProcedureRef &call) const {
112   // LBOUND, UBOUND, and SIZE with DIM= arguments will have been rewritten
113   // into DescriptorInquiry operations.
114   if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&call.proc().u)}) {
115     if (intrinsic->name == "kind" ||
116         intrinsic->name == IntrinsicProcTable::InvalidName) {
117       // kind is always a constant, and we avoid cascading errors by considering
118       // invalid calls to intrinsics to be constant
119       return true;
120     } else if (intrinsic->name == "lbound" && call.arguments().size() == 1) {
121       // LBOUND(x) without DIM=
122       auto base{ExtractNamedEntity(call.arguments()[0]->UnwrapExpr())};
123       return base && IsConstantExprShape(GetLowerBounds(*base));
124     } else if (intrinsic->name == "ubound" && call.arguments().size() == 1) {
125       // UBOUND(x) without DIM=
126       auto base{ExtractNamedEntity(call.arguments()[0]->UnwrapExpr())};
127       return base && IsConstantExprShape(GetUpperBounds(*base));
128     } else if (intrinsic->name == "shape") {
129       auto shape{GetShape(call.arguments()[0]->UnwrapExpr())};
130       return shape && IsConstantExprShape(*shape);
131     } else if (intrinsic->name == "size" && call.arguments().size() == 1) {
132       // SIZE(x) without DIM
133       auto shape{GetShape(call.arguments()[0]->UnwrapExpr())};
134       return shape && IsConstantExprShape(*shape);
135     }
136     // TODO: STORAGE_SIZE
137   }
138   return false;
139 }
140 
141 template <bool INVARIANT>
142 bool IsConstantExprHelper<INVARIANT>::IsConstantExprShape(
143     const Shape &shape) const {
144   for (const auto &extent : shape) {
145     if (!(*this)(extent)) {
146       return false;
147     }
148   }
149   return true;
150 }
151 
152 template <typename A> bool IsConstantExpr(const A &x) {
153   return IsConstantExprHelper<false>{}(x);
154 }
155 template bool IsConstantExpr(const Expr<SomeType> &);
156 template bool IsConstantExpr(const Expr<SomeInteger> &);
157 template bool IsConstantExpr(const Expr<SubscriptInteger> &);
158 template bool IsConstantExpr(const StructureConstructor &);
159 
160 // IsScopeInvariantExpr()
161 template <typename A> bool IsScopeInvariantExpr(const A &x) {
162   return IsConstantExprHelper<true>{}(x);
163 }
164 template bool IsScopeInvariantExpr(const Expr<SomeType> &);
165 template bool IsScopeInvariantExpr(const Expr<SomeInteger> &);
166 template bool IsScopeInvariantExpr(const Expr<SubscriptInteger> &);
167 
168 // IsActuallyConstant()
169 struct IsActuallyConstantHelper {
170   template <typename A> bool operator()(const A &) { return false; }
171   template <typename T> bool operator()(const Constant<T> &) { return true; }
172   template <typename T> bool operator()(const Parentheses<T> &x) {
173     return (*this)(x.left());
174   }
175   template <typename T> bool operator()(const Expr<T> &x) {
176     return std::visit([=](const auto &y) { return (*this)(y); }, x.u);
177   }
178   template <typename A> bool operator()(const A *x) { return x && (*this)(*x); }
179   template <typename A> bool operator()(const std::optional<A> &x) {
180     return x && (*this)(*x);
181   }
182 };
183 
184 template <typename A> bool IsActuallyConstant(const A &x) {
185   return IsActuallyConstantHelper{}(x);
186 }
187 
188 template bool IsActuallyConstant(const Expr<SomeType> &);
189 
190 // Object pointer initialization checking predicate IsInitialDataTarget().
191 // This code determines whether an expression is allowable as the static
192 // data address used to initialize a pointer with "=> x".  See C765.
193 class IsInitialDataTargetHelper
194     : public AllTraverse<IsInitialDataTargetHelper, true> {
195 public:
196   using Base = AllTraverse<IsInitialDataTargetHelper, true>;
197   using Base::operator();
198   explicit IsInitialDataTargetHelper(parser::ContextualMessages *m)
199       : Base{*this}, messages_{m} {}
200 
201   bool emittedMessage() const { return emittedMessage_; }
202 
203   bool operator()(const BOZLiteralConstant &) const { return false; }
204   bool operator()(const NullPointer &) const { return true; }
205   template <typename T> bool operator()(const Constant<T> &) const {
206     return false;
207   }
208   bool operator()(const semantics::Symbol &symbol) {
209     // This function checks only base symbols, not components.
210     const Symbol &ultimate{symbol.GetUltimate()};
211     if (const auto *assoc{
212             ultimate.detailsIf<semantics::AssocEntityDetails>()}) {
213       if (const auto &expr{assoc->expr()}) {
214         if (IsVariable(*expr)) {
215           return (*this)(*expr);
216         } else if (messages_) {
217           messages_->Say(
218               "An initial data target may not be an associated expression ('%s')"_err_en_US,
219               ultimate.name());
220           emittedMessage_ = true;
221         }
222       }
223       return false;
224     } else if (!ultimate.attrs().test(semantics::Attr::TARGET)) {
225       if (messages_) {
226         messages_->Say(
227             "An initial data target may not be a reference to an object '%s' that lacks the TARGET attribute"_err_en_US,
228             ultimate.name());
229         emittedMessage_ = true;
230       }
231       return false;
232     } else if (!IsSaved(ultimate)) {
233       if (messages_) {
234         messages_->Say(
235             "An initial data target may not be a reference to an object '%s' that lacks the SAVE attribute"_err_en_US,
236             ultimate.name());
237         emittedMessage_ = true;
238       }
239       return false;
240     } else {
241       return CheckVarOrComponent(ultimate);
242     }
243   }
244   bool operator()(const StaticDataObject &) const { return false; }
245   bool operator()(const TypeParamInquiry &) const { return false; }
246   bool operator()(const Triplet &x) const {
247     return IsConstantExpr(x.lower()) && IsConstantExpr(x.upper()) &&
248         IsConstantExpr(x.stride());
249   }
250   bool operator()(const Subscript &x) const {
251     return std::visit(common::visitors{
252                           [&](const Triplet &t) { return (*this)(t); },
253                           [&](const auto &y) {
254                             return y.value().Rank() == 0 &&
255                                 IsConstantExpr(y.value());
256                           },
257                       },
258         x.u);
259   }
260   bool operator()(const CoarrayRef &) const { return false; }
261   bool operator()(const Component &x) {
262     return CheckVarOrComponent(x.GetLastSymbol()) && (*this)(x.base());
263   }
264   bool operator()(const Substring &x) const {
265     return IsConstantExpr(x.lower()) && IsConstantExpr(x.upper()) &&
266         (*this)(x.parent());
267   }
268   bool operator()(const DescriptorInquiry &) const { return false; }
269   template <typename T> bool operator()(const ArrayConstructor<T> &) const {
270     return false;
271   }
272   bool operator()(const StructureConstructor &) const { return false; }
273   template <typename T> bool operator()(const FunctionRef<T> &) {
274     return false;
275   }
276   template <typename D, typename R, typename... O>
277   bool operator()(const Operation<D, R, O...> &) const {
278     return false;
279   }
280   template <typename T> bool operator()(const Parentheses<T> &x) const {
281     return (*this)(x.left());
282   }
283   template <typename T> bool operator()(const FunctionRef<T> &x) const {
284     return false;
285   }
286   bool operator()(const Relational<SomeType> &) const { return false; }
287 
288 private:
289   bool CheckVarOrComponent(const semantics::Symbol &symbol) {
290     const Symbol &ultimate{symbol.GetUltimate()};
291     if (IsAllocatable(ultimate)) {
292       if (messages_) {
293         messages_->Say(
294             "An initial data target may not be a reference to an ALLOCATABLE '%s'"_err_en_US,
295             ultimate.name());
296         emittedMessage_ = true;
297       }
298       return false;
299     } else if (ultimate.Corank() > 0) {
300       if (messages_) {
301         messages_->Say(
302             "An initial data target may not be a reference to a coarray '%s'"_err_en_US,
303             ultimate.name());
304         emittedMessage_ = true;
305       }
306       return false;
307     }
308     return true;
309   }
310 
311   parser::ContextualMessages *messages_;
312   bool emittedMessage_{false};
313 };
314 
315 bool IsInitialDataTarget(
316     const Expr<SomeType> &x, parser::ContextualMessages *messages) {
317   IsInitialDataTargetHelper helper{messages};
318   bool result{helper(x)};
319   if (!result && messages && !helper.emittedMessage()) {
320     messages->Say(
321         "An initial data target must be a designator with constant subscripts"_err_en_US);
322   }
323   return result;
324 }
325 
326 bool IsInitialProcedureTarget(const semantics::Symbol &symbol) {
327   const auto &ultimate{symbol.GetUltimate()};
328   return std::visit(
329       common::visitors{
330           [](const semantics::SubprogramDetails &subp) {
331             return !subp.isDummy();
332           },
333           [](const semantics::SubprogramNameDetails &) { return true; },
334           [&](const semantics::ProcEntityDetails &proc) {
335             return !semantics::IsPointer(ultimate) && !proc.isDummy();
336           },
337           [](const auto &) { return false; },
338       },
339       ultimate.details());
340 }
341 
342 bool IsInitialProcedureTarget(const ProcedureDesignator &proc) {
343   if (const auto *intrin{proc.GetSpecificIntrinsic()}) {
344     return !intrin->isRestrictedSpecific;
345   } else if (proc.GetComponent()) {
346     return false;
347   } else {
348     return IsInitialProcedureTarget(DEREF(proc.GetSymbol()));
349   }
350 }
351 
352 bool IsInitialProcedureTarget(const Expr<SomeType> &expr) {
353   if (const auto *proc{std::get_if<ProcedureDesignator>(&expr.u)}) {
354     return IsInitialProcedureTarget(*proc);
355   } else {
356     return IsNullPointer(expr);
357   }
358 }
359 
360 class ArrayConstantBoundChanger {
361 public:
362   ArrayConstantBoundChanger(ConstantSubscripts &&lbounds)
363       : lbounds_{std::move(lbounds)} {}
364 
365   template <typename A> A ChangeLbounds(A &&x) const {
366     return std::move(x); // default case
367   }
368   template <typename T> Constant<T> ChangeLbounds(Constant<T> &&x) {
369     x.set_lbounds(std::move(lbounds_));
370     return std::move(x);
371   }
372   template <typename T> Expr<T> ChangeLbounds(Parentheses<T> &&x) {
373     return ChangeLbounds(
374         std::move(x.left())); // Constant<> can be parenthesized
375   }
376   template <typename T> Expr<T> ChangeLbounds(Expr<T> &&x) {
377     return std::visit(
378         [&](auto &&x) { return Expr<T>{ChangeLbounds(std::move(x))}; },
379         std::move(x.u)); // recurse until we hit a constant
380   }
381 
382 private:
383   ConstantSubscripts &&lbounds_;
384 };
385 
386 // Converts, folds, and then checks type, rank, and shape of an
387 // initialization expression for a named constant, a non-pointer
388 // variable static initialization, a component default initializer,
389 // a type parameter default value, or instantiated type parameter value.
390 std::optional<Expr<SomeType>> NonPointerInitializationExpr(const Symbol &symbol,
391     Expr<SomeType> &&x, FoldingContext &context,
392     const semantics::Scope *instantiation) {
393   CHECK(!IsPointer(symbol));
394   if (auto symTS{
395           characteristics::TypeAndShape::Characterize(symbol, context)}) {
396     auto xType{x.GetType()};
397     auto converted{ConvertToType(symTS->type(), Expr<SomeType>{x})};
398     if (!converted &&
399         symbol.owner().context().IsEnabled(
400             common::LanguageFeature::LogicalIntegerAssignment)) {
401       converted = DataConstantConversionExtension(context, symTS->type(), x);
402       if (converted &&
403           symbol.owner().context().ShouldWarn(
404               common::LanguageFeature::LogicalIntegerAssignment)) {
405         context.messages().Say(
406             "nonstandard usage: initialization of %s with %s"_en_US,
407             symTS->type().AsFortran(), x.GetType().value().AsFortran());
408       }
409     }
410     if (converted) {
411       auto folded{Fold(context, std::move(*converted))};
412       if (IsActuallyConstant(folded)) {
413         int symRank{GetRank(symTS->shape())};
414         if (IsImpliedShape(symbol)) {
415           if (folded.Rank() == symRank) {
416             return {std::move(folded)};
417           } else {
418             context.messages().Say(
419                 "Implied-shape parameter '%s' has rank %d but its initializer has rank %d"_err_en_US,
420                 symbol.name(), symRank, folded.Rank());
421           }
422         } else if (auto extents{AsConstantExtents(context, symTS->shape())}) {
423           if (folded.Rank() == 0 && symRank == 0) {
424             // symbol and constant are both scalars
425             return {std::move(folded)};
426           } else if (folded.Rank() == 0 && symRank > 0) {
427             // expand the scalar constant to an array
428             return ScalarConstantExpander{std::move(*extents),
429                 AsConstantExtents(
430                     context, GetLowerBounds(context, NamedEntity{symbol}))}
431                 .Expand(std::move(folded));
432           } else if (auto resultShape{GetShape(context, folded)}) {
433             if (CheckConformance(context.messages(), symTS->shape(),
434                     *resultShape, CheckConformanceFlags::None,
435                     "initialized object", "initialization expression")
436                     .value_or(false /*fail if not known now to conform*/)) {
437               // make a constant array with adjusted lower bounds
438               return ArrayConstantBoundChanger{
439                   std::move(*AsConstantExtents(
440                       context, GetLowerBounds(context, NamedEntity{symbol})))}
441                   .ChangeLbounds(std::move(folded));
442             }
443           }
444         } else if (IsNamedConstant(symbol)) {
445           if (IsExplicitShape(symbol)) {
446             context.messages().Say(
447                 "Named constant '%s' array must have constant shape"_err_en_US,
448                 symbol.name());
449           } else {
450             // Declaration checking handles other cases
451           }
452         } else {
453           context.messages().Say(
454               "Shape of initialized object '%s' must be constant"_err_en_US,
455               symbol.name());
456         }
457       } else if (IsErrorExpr(folded)) {
458       } else if (IsLenTypeParameter(symbol)) {
459         return {std::move(folded)};
460       } else if (IsKindTypeParameter(symbol)) {
461         if (instantiation) {
462           context.messages().Say(
463               "Value of kind type parameter '%s' (%s) must be a scalar INTEGER constant"_err_en_US,
464               symbol.name(), folded.AsFortran());
465         } else {
466           return {std::move(folded)};
467         }
468       } else if (IsNamedConstant(symbol)) {
469         context.messages().Say(
470             "Value of named constant '%s' (%s) cannot be computed as a constant value"_err_en_US,
471             symbol.name(), folded.AsFortran());
472       } else {
473         context.messages().Say(
474             "Initialization expression for '%s' (%s) cannot be computed as a constant value"_err_en_US,
475             symbol.name(), folded.AsFortran());
476       }
477     } else if (xType) {
478       context.messages().Say(
479           "Initialization expression cannot be converted to declared type of '%s' from %s"_err_en_US,
480           symbol.name(), xType->AsFortran());
481     } else {
482       context.messages().Say(
483           "Initialization expression cannot be converted to declared type of '%s'"_err_en_US,
484           symbol.name());
485     }
486   }
487   return std::nullopt;
488 }
489 
490 // Specification expression validation (10.1.11(2), C1010)
491 class CheckSpecificationExprHelper
492     : public AnyTraverse<CheckSpecificationExprHelper,
493           std::optional<std::string>> {
494 public:
495   using Result = std::optional<std::string>;
496   using Base = AnyTraverse<CheckSpecificationExprHelper, Result>;
497   explicit CheckSpecificationExprHelper(
498       const semantics::Scope &s, FoldingContext &context)
499       : Base{*this}, scope_{s}, context_{context} {}
500   using Base::operator();
501 
502   Result operator()(const CoarrayRef &) const { return "coindexed reference"; }
503 
504   Result operator()(const semantics::Symbol &symbol) const {
505     const auto &ultimate{symbol.GetUltimate()};
506     if (const auto *assoc{
507             ultimate.detailsIf<semantics::AssocEntityDetails>()}) {
508       return (*this)(assoc->expr());
509     } else if (semantics::IsNamedConstant(ultimate) ||
510         ultimate.owner().IsModule() || ultimate.owner().IsSubmodule()) {
511       return std::nullopt;
512     } else if (scope_.IsDerivedType() &&
513         IsVariableName(ultimate)) { // C750, C754
514       return "derived type component or type parameter value not allowed to "
515              "reference variable '"s +
516           ultimate.name().ToString() + "'";
517     } else if (IsDummy(ultimate)) {
518       if (ultimate.attrs().test(semantics::Attr::OPTIONAL)) {
519         return "reference to OPTIONAL dummy argument '"s +
520             ultimate.name().ToString() + "'";
521       } else if (ultimate.attrs().test(semantics::Attr::INTENT_OUT)) {
522         return "reference to INTENT(OUT) dummy argument '"s +
523             ultimate.name().ToString() + "'";
524       } else if (ultimate.has<semantics::ObjectEntityDetails>()) {
525         return std::nullopt;
526       } else {
527         return "dummy procedure argument";
528       }
529     } else if (&symbol.owner() != &scope_ || &ultimate.owner() != &scope_) {
530       return std::nullopt; // host association is in play
531     } else if (const auto *object{
532                    ultimate.detailsIf<semantics::ObjectEntityDetails>()}) {
533       if (object->commonBlock()) {
534         return std::nullopt;
535       }
536     }
537     return "reference to local entity '"s + ultimate.name().ToString() + "'";
538   }
539 
540   Result operator()(const Component &x) const {
541     // Don't look at the component symbol.
542     return (*this)(x.base());
543   }
544   Result operator()(const DescriptorInquiry &) const {
545     // Subtle: Uses of SIZE(), LBOUND(), &c. that are valid in specification
546     // expressions will have been converted to expressions over descriptor
547     // inquiries by Fold().
548     return std::nullopt;
549   }
550 
551   Result operator()(const TypeParamInquiry &inq) const {
552     if (scope_.IsDerivedType() && !IsConstantExpr(inq) &&
553         inq.base() /* X%T, not local T */) { // C750, C754
554       return "non-constant reference to a type parameter inquiry not "
555              "allowed for derived type components or type parameter values";
556     }
557     return std::nullopt;
558   }
559 
560   template <typename T> Result operator()(const FunctionRef<T> &x) const {
561     if (const auto *symbol{x.proc().GetSymbol()}) {
562       const Symbol &ultimate{symbol->GetUltimate()};
563       if (!semantics::IsPureProcedure(ultimate)) {
564         return "reference to impure function '"s + ultimate.name().ToString() +
565             "'";
566       }
567       if (semantics::IsStmtFunction(ultimate)) {
568         return "reference to statement function '"s +
569             ultimate.name().ToString() + "'";
570       }
571       if (scope_.IsDerivedType()) { // C750, C754
572         return "reference to function '"s + ultimate.name().ToString() +
573             "' not allowed for derived type components or type parameter"
574             " values";
575       }
576       if (auto procChars{
577               characteristics::Procedure::Characterize(x.proc(), context_)}) {
578         const auto iter{std::find_if(procChars->dummyArguments.begin(),
579             procChars->dummyArguments.end(),
580             [](const characteristics::DummyArgument &dummy) {
581               return std::holds_alternative<characteristics::DummyProcedure>(
582                   dummy.u);
583             })};
584         if (iter != procChars->dummyArguments.end()) {
585           return "reference to function '"s + ultimate.name().ToString() +
586               "' with dummy procedure argument '" + iter->name + '\'';
587         }
588       }
589       // References to internal functions are caught in expression semantics.
590       // TODO: other checks for standard module procedures
591     } else {
592       const SpecificIntrinsic &intrin{DEREF(x.proc().GetSpecificIntrinsic())};
593       if (scope_.IsDerivedType()) { // C750, C754
594         if ((context_.intrinsics().IsIntrinsic(intrin.name) &&
595                 badIntrinsicsForComponents_.find(intrin.name) !=
596                     badIntrinsicsForComponents_.end()) ||
597             IsProhibitedFunction(intrin.name)) {
598           return "reference to intrinsic '"s + intrin.name +
599               "' not allowed for derived type components or type parameter"
600               " values";
601         }
602         if (context_.intrinsics().GetIntrinsicClass(intrin.name) ==
603                 IntrinsicClass::inquiryFunction &&
604             !IsConstantExpr(x)) {
605           return "non-constant reference to inquiry intrinsic '"s +
606               intrin.name +
607               "' not allowed for derived type components or type"
608               " parameter values";
609         }
610       } else if (intrin.name == "present") {
611         return std::nullopt; // no need to check argument(s)
612       }
613       if (IsConstantExpr(x)) {
614         // inquiry functions may not need to check argument(s)
615         return std::nullopt;
616       }
617     }
618     return (*this)(x.arguments());
619   }
620 
621 private:
622   const semantics::Scope &scope_;
623   FoldingContext &context_;
624   const std::set<std::string> badIntrinsicsForComponents_{
625       "allocated", "associated", "extends_type_of", "present", "same_type_as"};
626   static bool IsProhibitedFunction(std::string name) { return false; }
627 };
628 
629 template <typename A>
630 void CheckSpecificationExpr(
631     const A &x, const semantics::Scope &scope, FoldingContext &context) {
632   if (auto why{CheckSpecificationExprHelper{scope, context}(x)}) {
633     context.messages().Say(
634         "Invalid specification expression: %s"_err_en_US, *why);
635   }
636 }
637 
638 template void CheckSpecificationExpr(
639     const Expr<SomeType> &, const semantics::Scope &, FoldingContext &);
640 template void CheckSpecificationExpr(
641     const Expr<SomeInteger> &, const semantics::Scope &, FoldingContext &);
642 template void CheckSpecificationExpr(
643     const Expr<SubscriptInteger> &, const semantics::Scope &, FoldingContext &);
644 template void CheckSpecificationExpr(const std::optional<Expr<SomeType>> &,
645     const semantics::Scope &, FoldingContext &);
646 template void CheckSpecificationExpr(const std::optional<Expr<SomeInteger>> &,
647     const semantics::Scope &, FoldingContext &);
648 template void CheckSpecificationExpr(
649     const std::optional<Expr<SubscriptInteger>> &, const semantics::Scope &,
650     FoldingContext &);
651 
652 // IsSimplyContiguous() -- 9.5.4
653 class IsSimplyContiguousHelper
654     : public AnyTraverse<IsSimplyContiguousHelper, std::optional<bool>> {
655 public:
656   using Result = std::optional<bool>; // tri-state
657   using Base = AnyTraverse<IsSimplyContiguousHelper, Result>;
658   explicit IsSimplyContiguousHelper(FoldingContext &c)
659       : Base{*this}, context_{c} {}
660   using Base::operator();
661 
662   Result operator()(const semantics::Symbol &symbol) const {
663     const auto &ultimate{symbol.GetUltimate()};
664     if (ultimate.attrs().test(semantics::Attr::CONTIGUOUS)) {
665       return true;
666     } else if (ultimate.Rank() == 0) {
667       // Extension: accept scalars as a degenerate case of
668       // simple contiguity to allow their use in contexts like
669       // data targets in pointer assignments with remapping.
670       return true;
671     } else if (semantics::IsPointer(ultimate) ||
672         semantics::IsAssumedShape(ultimate)) {
673       return false;
674     } else if (const auto *details{
675                    ultimate.detailsIf<semantics::ObjectEntityDetails>()}) {
676       return !details->IsAssumedRank();
677     } else if (auto assoc{Base::operator()(ultimate)}) {
678       return assoc;
679     } else {
680       return false;
681     }
682   }
683 
684   Result operator()(const ArrayRef &x) const {
685     const auto &symbol{x.GetLastSymbol()};
686     if (!(*this)(symbol).has_value()) {
687       return false;
688     } else if (auto rank{CheckSubscripts(x.subscript())}) {
689       if (x.Rank() == 0) {
690         return true;
691       } else if (*rank > 0) {
692         // a(1)%b(:,:) is contiguous if an only if a(1)%b is contiguous.
693         return (*this)(x.base());
694       } else {
695         // a(:)%b(1,1) is not contiguous.
696         return false;
697       }
698     } else {
699       return false;
700     }
701   }
702   Result operator()(const CoarrayRef &x) const {
703     return CheckSubscripts(x.subscript()).has_value();
704   }
705   Result operator()(const Component &x) const {
706     return x.base().Rank() == 0 && (*this)(x.GetLastSymbol()).value_or(false);
707   }
708   Result operator()(const ComplexPart &) const { return false; }
709   Result operator()(const Substring &) const { return false; }
710 
711   template <typename T> Result operator()(const FunctionRef<T> &x) const {
712     if (auto chars{
713             characteristics::Procedure::Characterize(x.proc(), context_)}) {
714       if (chars->functionResult) {
715         const auto &result{*chars->functionResult};
716         return !result.IsProcedurePointer() &&
717             result.attrs.test(characteristics::FunctionResult::Attr::Pointer) &&
718             result.attrs.test(
719                 characteristics::FunctionResult::Attr::Contiguous);
720       }
721     }
722     return false;
723   }
724 
725 private:
726   // If the subscripts can possibly be on a simply-contiguous array reference,
727   // return the rank.
728   static std::optional<int> CheckSubscripts(
729       const std::vector<Subscript> &subscript) {
730     bool anyTriplet{false};
731     int rank{0};
732     for (auto j{subscript.size()}; j-- > 0;) {
733       if (const auto *triplet{std::get_if<Triplet>(&subscript[j].u)}) {
734         if (!triplet->IsStrideOne()) {
735           return std::nullopt;
736         } else if (anyTriplet) {
737           if (triplet->lower() || triplet->upper()) {
738             // all triplets before the last one must be just ":"
739             return std::nullopt;
740           }
741         } else {
742           anyTriplet = true;
743         }
744         ++rank;
745       } else if (anyTriplet || subscript[j].Rank() > 0) {
746         return std::nullopt;
747       }
748     }
749     return rank;
750   }
751 
752   FoldingContext &context_;
753 };
754 
755 template <typename A>
756 bool IsSimplyContiguous(const A &x, FoldingContext &context) {
757   if (IsVariable(x)) {
758     auto known{IsSimplyContiguousHelper{context}(x)};
759     return known && *known;
760   } else {
761     return true; // not a variable
762   }
763 }
764 
765 template bool IsSimplyContiguous(const Expr<SomeType> &, FoldingContext &);
766 
767 // IsErrorExpr()
768 struct IsErrorExprHelper : public AnyTraverse<IsErrorExprHelper, bool> {
769   using Result = bool;
770   using Base = AnyTraverse<IsErrorExprHelper, Result>;
771   IsErrorExprHelper() : Base{*this} {}
772   using Base::operator();
773 
774   bool operator()(const SpecificIntrinsic &x) {
775     return x.name == IntrinsicProcTable::InvalidName;
776   }
777 };
778 
779 template <typename A> bool IsErrorExpr(const A &x) {
780   return IsErrorExprHelper{}(x);
781 }
782 
783 template bool IsErrorExpr(const Expr<SomeType> &);
784 
785 } // namespace Fortran::evaluate
786