1 //===-- lib/Semantics/resolve-names.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 "resolve-names.h"
10 #include "assignment.h"
11 #include "check-omp-structure.h"
12 #include "mod-file.h"
13 #include "program-tree.h"
14 #include "resolve-names-utils.h"
15 #include "rewrite-parse-tree.h"
16 #include "flang/Common/Fortran.h"
17 #include "flang/Common/default-kinds.h"
18 #include "flang/Common/indirection.h"
19 #include "flang/Common/restorer.h"
20 #include "flang/Evaluate/characteristics.h"
21 #include "flang/Evaluate/common.h"
22 #include "flang/Evaluate/fold.h"
23 #include "flang/Evaluate/intrinsics.h"
24 #include "flang/Evaluate/tools.h"
25 #include "flang/Evaluate/type.h"
26 #include "flang/Parser/parse-tree-visitor.h"
27 #include "flang/Parser/parse-tree.h"
28 #include "flang/Parser/tools.h"
29 #include "flang/Semantics/attr.h"
30 #include "flang/Semantics/expression.h"
31 #include "flang/Semantics/scope.h"
32 #include "flang/Semantics/semantics.h"
33 #include "flang/Semantics/symbol.h"
34 #include "flang/Semantics/tools.h"
35 #include "flang/Semantics/type.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <list>
38 #include <map>
39 #include <set>
40 #include <stack>
41 
42 namespace Fortran::semantics {
43 
44 using namespace parser::literals;
45 
46 template <typename T> using Indirection = common::Indirection<T>;
47 using Message = parser::Message;
48 using Messages = parser::Messages;
49 using MessageFixedText = parser::MessageFixedText;
50 using MessageFormattedText = parser::MessageFormattedText;
51 
52 class ResolveNamesVisitor;
53 
54 // ImplicitRules maps initial character of identifier to the DeclTypeSpec
55 // representing the implicit type; std::nullopt if none.
56 // It also records the presence of IMPLICIT NONE statements.
57 // When inheritFromParent is set, defaults come from the parent rules.
58 class ImplicitRules {
59 public:
60   ImplicitRules(SemanticsContext &context, ImplicitRules *parent)
61       : parent_{parent}, context_{context} {
62     inheritFromParent_ = parent != nullptr;
63   }
64   bool isImplicitNoneType() const;
65   bool isImplicitNoneExternal() const;
66   void set_isImplicitNoneType(bool x) { isImplicitNoneType_ = x; }
67   void set_isImplicitNoneExternal(bool x) { isImplicitNoneExternal_ = x; }
68   void set_inheritFromParent(bool x) { inheritFromParent_ = x; }
69   // Get the implicit type for identifiers starting with ch. May be null.
70   const DeclTypeSpec *GetType(char ch) const;
71   // Record the implicit type for the range of characters [fromLetter,
72   // toLetter].
73   void SetTypeMapping(const DeclTypeSpec &type, parser::Location fromLetter,
74       parser::Location toLetter);
75 
76 private:
77   static char Incr(char ch);
78 
79   ImplicitRules *parent_;
80   SemanticsContext &context_;
81   bool inheritFromParent_{false}; // look in parent if not specified here
82   bool isImplicitNoneType_{false};
83   bool isImplicitNoneExternal_{false};
84   // map_ contains the mapping between letters and types that were defined
85   // by the IMPLICIT statements of the related scope. It does not contain
86   // the default Fortran mappings nor the mapping defined in parents.
87   std::map<char, common::Reference<const DeclTypeSpec>> map_;
88 
89   friend llvm::raw_ostream &operator<<(
90       llvm::raw_ostream &, const ImplicitRules &);
91   friend void ShowImplicitRule(
92       llvm::raw_ostream &, const ImplicitRules &, char);
93 };
94 
95 // scope -> implicit rules for that scope
96 using ImplicitRulesMap = std::map<const Scope *, ImplicitRules>;
97 
98 // Track statement source locations and save messages.
99 class MessageHandler {
100 public:
101   MessageHandler() { DIE("MessageHandler: default-constructed"); }
102   explicit MessageHandler(SemanticsContext &c) : context_{&c} {}
103   Messages &messages() { return context_->messages(); };
104   const std::optional<SourceName> &currStmtSource() {
105     return context_->location();
106   }
107   void set_currStmtSource(const std::optional<SourceName> &source) {
108     context_->set_location(source);
109   }
110 
111   // Emit a message associated with the current statement source.
112   Message &Say(MessageFixedText &&);
113   Message &Say(MessageFormattedText &&);
114   // Emit a message about a SourceName
115   Message &Say(const SourceName &, MessageFixedText &&);
116   // Emit a formatted message associated with a source location.
117   template <typename... A>
118   Message &Say(const SourceName &source, MessageFixedText &&msg, A &&... args) {
119     return context_->Say(source, std::move(msg), std::forward<A>(args)...);
120   }
121 
122 private:
123   SemanticsContext *context_;
124 };
125 
126 // Inheritance graph for the parse tree visitation classes that follow:
127 //   BaseVisitor
128 //   + AttrsVisitor
129 //   | + DeclTypeSpecVisitor
130 //   |   + ImplicitRulesVisitor
131 //   |     + ScopeHandler -----------+--+
132 //   |       + ModuleVisitor ========|==+
133 //   |       + InterfaceVisitor      |  |
134 //   |       +-+ SubprogramVisitor ==|==+
135 //   + ArraySpecVisitor              |  |
136 //     + DeclarationVisitor <--------+  |
137 //       + ConstructVisitor             |
138 //         + ResolveNamesVisitor <------+
139 
140 class BaseVisitor {
141 public:
142   BaseVisitor() { DIE("BaseVisitor: default-constructed"); }
143   BaseVisitor(
144       SemanticsContext &c, ResolveNamesVisitor &v, ImplicitRulesMap &rules)
145       : implicitRulesMap_{&rules}, this_{&v}, context_{&c}, messageHandler_{c} {
146   }
147   template <typename T> void Walk(const T &);
148 
149   MessageHandler &messageHandler() { return messageHandler_; }
150   const std::optional<SourceName> &currStmtSource() {
151     return context_->location();
152   }
153   SemanticsContext &context() const { return *context_; }
154   evaluate::FoldingContext &GetFoldingContext() const {
155     return context_->foldingContext();
156   }
157 
158   // Make a placeholder symbol for a Name that otherwise wouldn't have one.
159   // It is not in any scope and always has MiscDetails.
160   void MakePlaceholder(const parser::Name &, MiscDetails::Kind);
161 
162   template <typename T> common::IfNoLvalue<T, T> FoldExpr(T &&expr) {
163     return evaluate::Fold(GetFoldingContext(), std::move(expr));
164   }
165 
166   template <typename T> MaybeExpr EvaluateExpr(const T &expr) {
167     return FoldExpr(AnalyzeExpr(*context_, expr));
168   }
169 
170   template <typename T>
171   MaybeExpr EvaluateConvertedExpr(
172       const Symbol &symbol, const T &expr, parser::CharBlock source) {
173     if (context().HasError(symbol)) {
174       return std::nullopt;
175     }
176     auto maybeExpr{AnalyzeExpr(*context_, expr)};
177     if (!maybeExpr) {
178       return std::nullopt;
179     }
180     auto exprType{maybeExpr->GetType()};
181     auto converted{evaluate::ConvertToType(symbol, std::move(*maybeExpr))};
182     if (!converted) {
183       if (exprType) {
184         Say(source,
185             "Initialization expression could not be converted to declared type of '%s' from %s"_err_en_US,
186             symbol.name(), exprType->AsFortran());
187       } else {
188         Say(source,
189             "Initialization expression could not be converted to declared type of '%s'"_err_en_US,
190             symbol.name());
191       }
192       return std::nullopt;
193     }
194     return FoldExpr(std::move(*converted));
195   }
196 
197   template <typename T> MaybeIntExpr EvaluateIntExpr(const T &expr) {
198     if (MaybeExpr maybeExpr{EvaluateExpr(expr)}) {
199       if (auto *intExpr{evaluate::UnwrapExpr<SomeIntExpr>(*maybeExpr)}) {
200         return std::move(*intExpr);
201       }
202     }
203     return std::nullopt;
204   }
205 
206   template <typename T>
207   MaybeSubscriptIntExpr EvaluateSubscriptIntExpr(const T &expr) {
208     if (MaybeIntExpr maybeIntExpr{EvaluateIntExpr(expr)}) {
209       return FoldExpr(evaluate::ConvertToType<evaluate::SubscriptInteger>(
210           std::move(*maybeIntExpr)));
211     } else {
212       return std::nullopt;
213     }
214   }
215 
216   template <typename... A> Message &Say(A &&... args) {
217     return messageHandler_.Say(std::forward<A>(args)...);
218   }
219   template <typename... A>
220   Message &Say(
221       const parser::Name &name, MessageFixedText &&text, const A &... args) {
222     return messageHandler_.Say(name.source, std::move(text), args...);
223   }
224 
225 protected:
226   ImplicitRulesMap *implicitRulesMap_{nullptr};
227 
228 private:
229   ResolveNamesVisitor *this_;
230   SemanticsContext *context_;
231   MessageHandler messageHandler_;
232 };
233 
234 // Provide Post methods to collect attributes into a member variable.
235 class AttrsVisitor : public virtual BaseVisitor {
236 public:
237   bool BeginAttrs(); // always returns true
238   Attrs GetAttrs();
239   Attrs EndAttrs();
240   bool SetPassNameOn(Symbol &);
241   bool SetBindNameOn(Symbol &);
242   void Post(const parser::LanguageBindingSpec &);
243   bool Pre(const parser::IntentSpec &);
244   bool Pre(const parser::Pass &);
245 
246   bool CheckAndSet(Attr);
247 
248 // Simple case: encountering CLASSNAME causes ATTRNAME to be set.
249 #define HANDLE_ATTR_CLASS(CLASSNAME, ATTRNAME) \
250   bool Pre(const parser::CLASSNAME &) { \
251     CheckAndSet(Attr::ATTRNAME); \
252     return false; \
253   }
254   HANDLE_ATTR_CLASS(PrefixSpec::Elemental, ELEMENTAL)
255   HANDLE_ATTR_CLASS(PrefixSpec::Impure, IMPURE)
256   HANDLE_ATTR_CLASS(PrefixSpec::Module, MODULE)
257   HANDLE_ATTR_CLASS(PrefixSpec::Non_Recursive, NON_RECURSIVE)
258   HANDLE_ATTR_CLASS(PrefixSpec::Pure, PURE)
259   HANDLE_ATTR_CLASS(PrefixSpec::Recursive, RECURSIVE)
260   HANDLE_ATTR_CLASS(TypeAttrSpec::BindC, BIND_C)
261   HANDLE_ATTR_CLASS(BindAttr::Deferred, DEFERRED)
262   HANDLE_ATTR_CLASS(BindAttr::Non_Overridable, NON_OVERRIDABLE)
263   HANDLE_ATTR_CLASS(Abstract, ABSTRACT)
264   HANDLE_ATTR_CLASS(Allocatable, ALLOCATABLE)
265   HANDLE_ATTR_CLASS(Asynchronous, ASYNCHRONOUS)
266   HANDLE_ATTR_CLASS(Contiguous, CONTIGUOUS)
267   HANDLE_ATTR_CLASS(External, EXTERNAL)
268   HANDLE_ATTR_CLASS(Intrinsic, INTRINSIC)
269   HANDLE_ATTR_CLASS(NoPass, NOPASS)
270   HANDLE_ATTR_CLASS(Optional, OPTIONAL)
271   HANDLE_ATTR_CLASS(Parameter, PARAMETER)
272   HANDLE_ATTR_CLASS(Pointer, POINTER)
273   HANDLE_ATTR_CLASS(Protected, PROTECTED)
274   HANDLE_ATTR_CLASS(Save, SAVE)
275   HANDLE_ATTR_CLASS(Target, TARGET)
276   HANDLE_ATTR_CLASS(Value, VALUE)
277   HANDLE_ATTR_CLASS(Volatile, VOLATILE)
278 #undef HANDLE_ATTR_CLASS
279 
280 protected:
281   std::optional<Attrs> attrs_;
282 
283   Attr AccessSpecToAttr(const parser::AccessSpec &x) {
284     switch (x.v) {
285     case parser::AccessSpec::Kind::Public:
286       return Attr::PUBLIC;
287     case parser::AccessSpec::Kind::Private:
288       return Attr::PRIVATE;
289     }
290     common::die("unreachable"); // suppress g++ warning
291   }
292   Attr IntentSpecToAttr(const parser::IntentSpec &x) {
293     switch (x.v) {
294     case parser::IntentSpec::Intent::In:
295       return Attr::INTENT_IN;
296     case parser::IntentSpec::Intent::Out:
297       return Attr::INTENT_OUT;
298     case parser::IntentSpec::Intent::InOut:
299       return Attr::INTENT_INOUT;
300     }
301     common::die("unreachable"); // suppress g++ warning
302   }
303 
304 private:
305   bool IsDuplicateAttr(Attr);
306   bool HaveAttrConflict(Attr, Attr, Attr);
307   bool IsConflictingAttr(Attr);
308 
309   MaybeExpr bindName_; // from BIND(C, NAME="...")
310   std::optional<SourceName> passName_; // from PASS(...)
311 };
312 
313 // Find and create types from declaration-type-spec nodes.
314 class DeclTypeSpecVisitor : public AttrsVisitor {
315 public:
316   using AttrsVisitor::Post;
317   using AttrsVisitor::Pre;
318   void Post(const parser::IntrinsicTypeSpec::DoublePrecision &);
319   void Post(const parser::IntrinsicTypeSpec::DoubleComplex &);
320   void Post(const parser::DeclarationTypeSpec::ClassStar &);
321   void Post(const parser::DeclarationTypeSpec::TypeStar &);
322   bool Pre(const parser::TypeGuardStmt &);
323   void Post(const parser::TypeGuardStmt &);
324   void Post(const parser::TypeSpec &);
325 
326 protected:
327   struct State {
328     bool expectDeclTypeSpec{false}; // should see decl-type-spec only when true
329     const DeclTypeSpec *declTypeSpec{nullptr};
330     struct {
331       DerivedTypeSpec *type{nullptr};
332       DeclTypeSpec::Category category{DeclTypeSpec::TypeDerived};
333     } derived;
334     bool allowForwardReferenceToDerivedType{false};
335   };
336 
337   bool allowForwardReferenceToDerivedType() const {
338     return state_.allowForwardReferenceToDerivedType;
339   }
340   void set_allowForwardReferenceToDerivedType(bool yes) {
341     state_.allowForwardReferenceToDerivedType = yes;
342   }
343 
344   // Walk the parse tree of a type spec and return the DeclTypeSpec for it.
345   template <typename T>
346   const DeclTypeSpec *ProcessTypeSpec(const T &x, bool allowForward = false) {
347     auto restorer{common::ScopedSet(state_, State{})};
348     set_allowForwardReferenceToDerivedType(allowForward);
349     BeginDeclTypeSpec();
350     Walk(x);
351     const auto *type{GetDeclTypeSpec()};
352     EndDeclTypeSpec();
353     return type;
354   }
355 
356   const DeclTypeSpec *GetDeclTypeSpec();
357   void BeginDeclTypeSpec();
358   void EndDeclTypeSpec();
359   void SetDeclTypeSpec(const DeclTypeSpec &);
360   void SetDeclTypeSpecCategory(DeclTypeSpec::Category);
361   DeclTypeSpec::Category GetDeclTypeSpecCategory() const {
362     return state_.derived.category;
363   }
364   KindExpr GetKindParamExpr(
365       TypeCategory, const std::optional<parser::KindSelector> &);
366   void CheckForAbstractType(const Symbol &typeSymbol);
367 
368 private:
369   State state_;
370 
371   void MakeNumericType(TypeCategory, int kind);
372 };
373 
374 // Visit ImplicitStmt and related parse tree nodes and updates implicit rules.
375 class ImplicitRulesVisitor : public DeclTypeSpecVisitor {
376 public:
377   using DeclTypeSpecVisitor::Post;
378   using DeclTypeSpecVisitor::Pre;
379   using ImplicitNoneNameSpec = parser::ImplicitStmt::ImplicitNoneNameSpec;
380 
381   void Post(const parser::ParameterStmt &);
382   bool Pre(const parser::ImplicitStmt &);
383   bool Pre(const parser::LetterSpec &);
384   bool Pre(const parser::ImplicitSpec &);
385   void Post(const parser::ImplicitSpec &);
386 
387   ImplicitRules &implicitRules() { return *implicitRules_; }
388   const ImplicitRules &implicitRules() const { return *implicitRules_; }
389   bool isImplicitNoneType() const {
390     return implicitRules().isImplicitNoneType();
391   }
392   bool isImplicitNoneExternal() const {
393     return implicitRules().isImplicitNoneExternal();
394   }
395 
396 protected:
397   void BeginScope(const Scope &);
398   void SetScope(const Scope &);
399 
400 private:
401   // implicit rules in effect for current scope
402   ImplicitRules *implicitRules_{nullptr};
403   std::optional<SourceName> prevImplicit_;
404   std::optional<SourceName> prevImplicitNone_;
405   std::optional<SourceName> prevImplicitNoneType_;
406   std::optional<SourceName> prevParameterStmt_;
407 
408   bool HandleImplicitNone(const std::list<ImplicitNoneNameSpec> &nameSpecs);
409 };
410 
411 // Track array specifications. They can occur in AttrSpec, EntityDecl,
412 // ObjectDecl, DimensionStmt, CommonBlockObject, or BasedPointerStmt.
413 // 1. INTEGER, DIMENSION(10) :: x
414 // 2. INTEGER :: x(10)
415 // 3. ALLOCATABLE :: x(:)
416 // 4. DIMENSION :: x(10)
417 // 5. COMMON x(10)
418 // 6. BasedPointerStmt
419 class ArraySpecVisitor : public virtual BaseVisitor {
420 public:
421   void Post(const parser::ArraySpec &);
422   void Post(const parser::ComponentArraySpec &);
423   void Post(const parser::CoarraySpec &);
424   void Post(const parser::AttrSpec &) { PostAttrSpec(); }
425   void Post(const parser::ComponentAttrSpec &) { PostAttrSpec(); }
426 
427 protected:
428   const ArraySpec &arraySpec();
429   const ArraySpec &coarraySpec();
430   void BeginArraySpec();
431   void EndArraySpec();
432   void ClearArraySpec() { arraySpec_.clear(); }
433   void ClearCoarraySpec() { coarraySpec_.clear(); }
434 
435 private:
436   // arraySpec_/coarraySpec_ are populated from any ArraySpec/CoarraySpec
437   ArraySpec arraySpec_;
438   ArraySpec coarraySpec_;
439   // When an ArraySpec is under an AttrSpec or ComponentAttrSpec, it is moved
440   // into attrArraySpec_
441   ArraySpec attrArraySpec_;
442   ArraySpec attrCoarraySpec_;
443 
444   void PostAttrSpec();
445 };
446 
447 // Manage a stack of Scopes
448 class ScopeHandler : public ImplicitRulesVisitor {
449 public:
450   using ImplicitRulesVisitor::Post;
451   using ImplicitRulesVisitor::Pre;
452 
453   Scope &currScope() { return DEREF(currScope_); }
454   // The enclosing scope, skipping blocks and derived types.
455   // TODO: Will return the scope of a FORALL or implied DO loop; is this ok?
456   // If not, should call FindProgramUnitContaining() instead.
457   Scope &InclusiveScope();
458 
459   // Create a new scope and push it on the scope stack.
460   void PushScope(Scope::Kind kind, Symbol *symbol);
461   void PushScope(Scope &scope);
462   void PopScope();
463   void SetScope(Scope &);
464 
465   template <typename T> bool Pre(const parser::Statement<T> &x) {
466     messageHandler().set_currStmtSource(x.source);
467     currScope_->AddSourceRange(x.source);
468     return true;
469   }
470   template <typename T> void Post(const parser::Statement<T> &) {
471     messageHandler().set_currStmtSource(std::nullopt);
472   }
473 
474   // Special messages: already declared; referencing symbol's declaration;
475   // about a type; two names & locations
476   void SayAlreadyDeclared(const parser::Name &, Symbol &);
477   void SayAlreadyDeclared(const SourceName &, Symbol &);
478   void SayAlreadyDeclared(const SourceName &, const SourceName &);
479   void SayWithReason(
480       const parser::Name &, Symbol &, MessageFixedText &&, MessageFixedText &&);
481   void SayWithDecl(const parser::Name &, Symbol &, MessageFixedText &&);
482   void SayLocalMustBeVariable(const parser::Name &, Symbol &);
483   void SayDerivedType(const SourceName &, MessageFixedText &&, const Scope &);
484   void Say2(const SourceName &, MessageFixedText &&, const SourceName &,
485       MessageFixedText &&);
486   void Say2(
487       const SourceName &, MessageFixedText &&, Symbol &, MessageFixedText &&);
488   void Say2(
489       const parser::Name &, MessageFixedText &&, Symbol &, MessageFixedText &&);
490 
491   // Search for symbol by name in current, parent derived type, and
492   // containing scopes
493   Symbol *FindSymbol(const parser::Name &);
494   Symbol *FindSymbol(const Scope &, const parser::Name &);
495   // Search for name only in scope, not in enclosing scopes.
496   Symbol *FindInScope(const Scope &, const parser::Name &);
497   Symbol *FindInScope(const Scope &, const SourceName &);
498   // Search for name in a derived type scope and its parents.
499   Symbol *FindInTypeOrParents(const Scope &, const parser::Name &);
500   Symbol *FindInTypeOrParents(const parser::Name &);
501   void EraseSymbol(const parser::Name &);
502   void EraseSymbol(const Symbol &symbol) { currScope().erase(symbol.name()); }
503   // Make a new symbol with the name and attrs of an existing one
504   Symbol &CopySymbol(const SourceName &, const Symbol &);
505 
506   // Make symbols in the current or named scope
507   Symbol &MakeSymbol(Scope &, const SourceName &, Attrs);
508   Symbol &MakeSymbol(const SourceName &, Attrs = Attrs{});
509   Symbol &MakeSymbol(const parser::Name &, Attrs = Attrs{});
510 
511   template <typename D>
512   common::IfNoLvalue<Symbol &, D> MakeSymbol(
513       const parser::Name &name, D &&details) {
514     return MakeSymbol(name, Attrs{}, std::move(details));
515   }
516 
517   template <typename D>
518   common::IfNoLvalue<Symbol &, D> MakeSymbol(
519       const parser::Name &name, const Attrs &attrs, D &&details) {
520     return Resolve(name, MakeSymbol(name.source, attrs, std::move(details)));
521   }
522 
523   template <typename D>
524   common::IfNoLvalue<Symbol &, D> MakeSymbol(
525       const SourceName &name, const Attrs &attrs, D &&details) {
526     // Note: don't use FindSymbol here. If this is a derived type scope,
527     // we want to detect whether the name is already declared as a component.
528     auto *symbol{FindInScope(currScope(), name)};
529     if (!symbol) {
530       symbol = &MakeSymbol(name, attrs);
531       symbol->set_details(std::move(details));
532       return *symbol;
533     }
534     if constexpr (std::is_same_v<DerivedTypeDetails, D>) {
535       if (auto *d{symbol->detailsIf<GenericDetails>()}) {
536         if (!d->specific()) {
537           // derived type with same name as a generic
538           auto *derivedType{d->derivedType()};
539           if (!derivedType) {
540             derivedType =
541                 &currScope().MakeSymbol(name, attrs, std::move(details));
542             d->set_derivedType(*derivedType);
543           } else {
544             SayAlreadyDeclared(name, *derivedType);
545           }
546           return *derivedType;
547         }
548       }
549     }
550     if (symbol->CanReplaceDetails(details)) {
551       // update the existing symbol
552       symbol->attrs() |= attrs;
553       symbol->set_details(std::move(details));
554       return *symbol;
555     } else if constexpr (std::is_same_v<UnknownDetails, D>) {
556       symbol->attrs() |= attrs;
557       return *symbol;
558     } else {
559       SayAlreadyDeclared(name, *symbol);
560       // replace the old symbol with a new one with correct details
561       EraseSymbol(*symbol);
562       auto &result{MakeSymbol(name, attrs, std::move(details))};
563       context().SetError(result);
564       return result;
565     }
566   }
567 
568   void MakeExternal(Symbol &);
569 
570 protected:
571   // Apply the implicit type rules to this symbol.
572   void ApplyImplicitRules(Symbol &);
573   const DeclTypeSpec *GetImplicitType(Symbol &);
574   bool ConvertToObjectEntity(Symbol &);
575   bool ConvertToProcEntity(Symbol &);
576 
577   const DeclTypeSpec &MakeNumericType(
578       TypeCategory, const std::optional<parser::KindSelector> &);
579   const DeclTypeSpec &MakeLogicalType(
580       const std::optional<parser::KindSelector> &);
581 
582 private:
583   Scope *currScope_{nullptr};
584 };
585 
586 class ModuleVisitor : public virtual ScopeHandler {
587 public:
588   bool Pre(const parser::AccessStmt &);
589   bool Pre(const parser::Only &);
590   bool Pre(const parser::Rename::Names &);
591   bool Pre(const parser::Rename::Operators &);
592   bool Pre(const parser::UseStmt &);
593   void Post(const parser::UseStmt &);
594 
595   void BeginModule(const parser::Name &, bool isSubmodule);
596   bool BeginSubmodule(const parser::Name &, const parser::ParentIdentifier &);
597   void ApplyDefaultAccess();
598 
599 private:
600   // The default access spec for this module.
601   Attr defaultAccess_{Attr::PUBLIC};
602   // The location of the last AccessStmt without access-ids, if any.
603   std::optional<SourceName> prevAccessStmt_;
604   // The scope of the module during a UseStmt
605   const Scope *useModuleScope_{nullptr};
606 
607   Symbol &SetAccess(const SourceName &, Attr attr, Symbol * = nullptr);
608   // A rename in a USE statement: local => use
609   struct SymbolRename {
610     Symbol *local{nullptr};
611     Symbol *use{nullptr};
612   };
613   // Record a use from useModuleScope_ of use Name/Symbol as local Name/Symbol
614   SymbolRename AddUse(const SourceName &localName, const SourceName &useName);
615   SymbolRename AddUse(const SourceName &, const SourceName &, Symbol *);
616   void AddUse(const SourceName &, Symbol &localSymbol, const Symbol &useSymbol);
617   void AddUse(const GenericSpecInfo &);
618   Scope *FindModule(const parser::Name &, Scope *ancestor = nullptr);
619 };
620 
621 class InterfaceVisitor : public virtual ScopeHandler {
622 public:
623   bool Pre(const parser::InterfaceStmt &);
624   void Post(const parser::InterfaceStmt &);
625   void Post(const parser::EndInterfaceStmt &);
626   bool Pre(const parser::GenericSpec &);
627   bool Pre(const parser::ProcedureStmt &);
628   bool Pre(const parser::GenericStmt &);
629   void Post(const parser::GenericStmt &);
630 
631   bool inInterfaceBlock() const;
632   bool isGeneric() const;
633   bool isAbstract() const;
634 
635 protected:
636   GenericDetails &GetGenericDetails();
637   // Add to generic the symbol for the subprogram with the same name
638   void CheckGenericProcedures(Symbol &);
639 
640 private:
641   // A new GenericInfo is pushed for each interface block and generic stmt
642   struct GenericInfo {
643     GenericInfo(bool isInterface, bool isAbstract = false)
644         : isInterface{isInterface}, isAbstract{isAbstract} {}
645     bool isInterface; // in interface block
646     bool isAbstract; // in abstract interface block
647     Symbol *symbol{nullptr}; // the generic symbol being defined
648   };
649   std::stack<GenericInfo> genericInfo_;
650   const GenericInfo &GetGenericInfo() const { return genericInfo_.top(); }
651   void SetGenericSymbol(Symbol &symbol) { genericInfo_.top().symbol = &symbol; }
652 
653   using ProcedureKind = parser::ProcedureStmt::Kind;
654   // mapping of generic to its specific proc names and kinds
655   std::multimap<Symbol *, std::pair<const parser::Name *, ProcedureKind>>
656       specificProcs_;
657 
658   void AddSpecificProcs(const std::list<parser::Name> &, ProcedureKind);
659   void ResolveSpecificsInGeneric(Symbol &generic);
660 };
661 
662 class SubprogramVisitor : public virtual ScopeHandler, public InterfaceVisitor {
663 public:
664   bool HandleStmtFunction(const parser::StmtFunctionStmt &);
665   bool Pre(const parser::SubroutineStmt &);
666   void Post(const parser::SubroutineStmt &);
667   bool Pre(const parser::FunctionStmt &);
668   void Post(const parser::FunctionStmt &);
669   bool Pre(const parser::EntryStmt &);
670   void Post(const parser::EntryStmt &);
671   bool Pre(const parser::InterfaceBody::Subroutine &);
672   void Post(const parser::InterfaceBody::Subroutine &);
673   bool Pre(const parser::InterfaceBody::Function &);
674   void Post(const parser::InterfaceBody::Function &);
675   bool Pre(const parser::Suffix &);
676   bool Pre(const parser::PrefixSpec &);
677   void Post(const parser::ImplicitPart &);
678 
679   bool BeginSubprogram(
680       const parser::Name &, Symbol::Flag, bool hasModulePrefix = false);
681   bool BeginMpSubprogram(const parser::Name &);
682   void PushBlockDataScope(const parser::Name &);
683   void EndSubprogram();
684 
685 protected:
686   // Set when we see a stmt function that is really an array element assignment
687   bool badStmtFuncFound_{false};
688   bool inExecutionPart_{false};
689 
690 private:
691   // Info about the current function: parse tree of the type in the PrefixSpec;
692   // name and symbol of the function result from the Suffix; source location.
693   struct {
694     const parser::DeclarationTypeSpec *parsedType{nullptr};
695     const parser::Name *resultName{nullptr};
696     Symbol *resultSymbol{nullptr};
697     std::optional<SourceName> source;
698   } funcInfo_;
699 
700   // Create a subprogram symbol in the current scope and push a new scope.
701   void CheckExtantExternal(const parser::Name &, Symbol::Flag);
702   Symbol &PushSubprogramScope(const parser::Name &, Symbol::Flag);
703   Symbol *GetSpecificFromGeneric(const parser::Name &);
704   SubprogramDetails &PostSubprogramStmt(const parser::Name &);
705 };
706 
707 class DeclarationVisitor : public ArraySpecVisitor,
708                            public virtual ScopeHandler {
709 public:
710   using ArraySpecVisitor::Post;
711   using ScopeHandler::Post;
712   using ScopeHandler::Pre;
713 
714   bool Pre(const parser::Initialization &);
715   void Post(const parser::EntityDecl &);
716   void Post(const parser::ObjectDecl &);
717   void Post(const parser::PointerDecl &);
718   bool Pre(const parser::BindStmt &) { return BeginAttrs(); }
719   void Post(const parser::BindStmt &) { EndAttrs(); }
720   bool Pre(const parser::BindEntity &);
721   bool Pre(const parser::NamedConstantDef &);
722   bool Pre(const parser::NamedConstant &);
723   void Post(const parser::EnumDef &);
724   bool Pre(const parser::Enumerator &);
725   bool Pre(const parser::AccessSpec &);
726   bool Pre(const parser::AsynchronousStmt &);
727   bool Pre(const parser::ContiguousStmt &);
728   bool Pre(const parser::ExternalStmt &);
729   bool Pre(const parser::IntentStmt &);
730   bool Pre(const parser::IntrinsicStmt &);
731   bool Pre(const parser::OptionalStmt &);
732   bool Pre(const parser::ProtectedStmt &);
733   bool Pre(const parser::ValueStmt &);
734   bool Pre(const parser::VolatileStmt &);
735   bool Pre(const parser::AllocatableStmt &) {
736     objectDeclAttr_ = Attr::ALLOCATABLE;
737     return true;
738   }
739   void Post(const parser::AllocatableStmt &) { objectDeclAttr_ = std::nullopt; }
740   bool Pre(const parser::TargetStmt &) {
741     objectDeclAttr_ = Attr::TARGET;
742     return true;
743   }
744   void Post(const parser::TargetStmt &) { objectDeclAttr_ = std::nullopt; }
745   void Post(const parser::DimensionStmt::Declaration &);
746   void Post(const parser::CodimensionDecl &);
747   bool Pre(const parser::TypeDeclarationStmt &) { return BeginDecl(); }
748   void Post(const parser::TypeDeclarationStmt &);
749   void Post(const parser::IntegerTypeSpec &);
750   void Post(const parser::IntrinsicTypeSpec::Real &);
751   void Post(const parser::IntrinsicTypeSpec::Complex &);
752   void Post(const parser::IntrinsicTypeSpec::Logical &);
753   void Post(const parser::IntrinsicTypeSpec::Character &);
754   void Post(const parser::CharSelector::LengthAndKind &);
755   void Post(const parser::CharLength &);
756   void Post(const parser::LengthSelector &);
757   bool Pre(const parser::KindParam &);
758   bool Pre(const parser::DeclarationTypeSpec::Type &);
759   void Post(const parser::DeclarationTypeSpec::Type &);
760   bool Pre(const parser::DeclarationTypeSpec::Class &);
761   void Post(const parser::DeclarationTypeSpec::Class &);
762   bool Pre(const parser::DeclarationTypeSpec::Record &);
763   void Post(const parser::DerivedTypeSpec &);
764   bool Pre(const parser::DerivedTypeDef &);
765   bool Pre(const parser::DerivedTypeStmt &);
766   void Post(const parser::DerivedTypeStmt &);
767   bool Pre(const parser::TypeParamDefStmt &) { return BeginDecl(); }
768   void Post(const parser::TypeParamDefStmt &);
769   bool Pre(const parser::TypeAttrSpec::Extends &);
770   bool Pre(const parser::PrivateStmt &);
771   bool Pre(const parser::SequenceStmt &);
772   bool Pre(const parser::ComponentDefStmt &) { return BeginDecl(); }
773   void Post(const parser::ComponentDefStmt &) { EndDecl(); }
774   void Post(const parser::ComponentDecl &);
775   bool Pre(const parser::ProcedureDeclarationStmt &);
776   void Post(const parser::ProcedureDeclarationStmt &);
777   bool Pre(const parser::DataComponentDefStmt &); // returns false
778   bool Pre(const parser::ProcComponentDefStmt &);
779   void Post(const parser::ProcComponentDefStmt &);
780   bool Pre(const parser::ProcPointerInit &);
781   void Post(const parser::ProcInterface &);
782   void Post(const parser::ProcDecl &);
783   bool Pre(const parser::TypeBoundProcedurePart &);
784   void Post(const parser::TypeBoundProcedurePart &);
785   void Post(const parser::ContainsStmt &);
786   bool Pre(const parser::TypeBoundProcBinding &) { return BeginAttrs(); }
787   void Post(const parser::TypeBoundProcBinding &) { EndAttrs(); }
788   void Post(const parser::TypeBoundProcedureStmt::WithoutInterface &);
789   void Post(const parser::TypeBoundProcedureStmt::WithInterface &);
790   void Post(const parser::FinalProcedureStmt &);
791   bool Pre(const parser::TypeBoundGenericStmt &);
792   bool Pre(const parser::AllocateStmt &);
793   void Post(const parser::AllocateStmt &);
794   bool Pre(const parser::StructureConstructor &);
795   bool Pre(const parser::NamelistStmt::Group &);
796   bool Pre(const parser::IoControlSpec &);
797   bool Pre(const parser::CommonStmt::Block &);
798   void Post(const parser::CommonStmt::Block &);
799   bool Pre(const parser::CommonBlockObject &);
800   void Post(const parser::CommonBlockObject &);
801   bool Pre(const parser::EquivalenceStmt &);
802   bool Pre(const parser::SaveStmt &);
803   bool Pre(const parser::BasedPointerStmt &);
804 
805   void PointerInitialization(
806       const parser::Name &, const parser::InitialDataTarget &);
807   void PointerInitialization(
808       const parser::Name &, const parser::ProcPointerInit &);
809   void CheckExplicitInterface(const parser::Name &);
810   void CheckBindings(const parser::TypeBoundProcedureStmt::WithoutInterface &);
811 
812   const parser::Name *ResolveDesignator(const parser::Designator &);
813 
814 protected:
815   bool BeginDecl();
816   void EndDecl();
817   Symbol &DeclareObjectEntity(const parser::Name &, Attrs);
818   // Make sure that there's an entity in an enclosing scope called Name
819   Symbol &FindOrDeclareEnclosingEntity(const parser::Name &);
820   // Declare a LOCAL/LOCAL_INIT entity. If there isn't a type specified
821   // it comes from the entity in the containing scope, or implicit rules.
822   // Return pointer to the new symbol, or nullptr on error.
823   Symbol *DeclareLocalEntity(const parser::Name &);
824   // Declare a statement entity (e.g., an implied DO loop index).
825   // If there isn't a type specified, implicit rules apply.
826   // Return pointer to the new symbol, or nullptr on error.
827   Symbol *DeclareStatementEntity(
828       const parser::Name &, const std::optional<parser::IntegerTypeSpec> &);
829   bool CheckUseError(const parser::Name &);
830   void CheckAccessibility(const SourceName &, bool, Symbol &);
831   void CheckCommonBlocks();
832   void CheckSaveStmts();
833   void CheckEquivalenceSets();
834   bool CheckNotInBlock(const char *);
835   bool NameIsKnownOrIntrinsic(const parser::Name &);
836 
837   // Each of these returns a pointer to a resolved Name (i.e. with symbol)
838   // or nullptr in case of error.
839   const parser::Name *ResolveStructureComponent(
840       const parser::StructureComponent &);
841   const parser::Name *ResolveDataRef(const parser::DataRef &);
842   const parser::Name *ResolveVariable(const parser::Variable &);
843   const parser::Name *ResolveName(const parser::Name &);
844   bool PassesSharedLocalityChecks(const parser::Name &name, Symbol &symbol);
845   Symbol *NoteInterfaceName(const parser::Name &);
846 
847 private:
848   // The attribute corresponding to the statement containing an ObjectDecl
849   std::optional<Attr> objectDeclAttr_;
850   // Info about current character type while walking DeclTypeSpec.
851   // Also captures any "*length" specifier on an individual declaration.
852   struct {
853     std::optional<ParamValue> length;
854     std::optional<KindExpr> kind;
855   } charInfo_;
856   // Info about current derived type while walking DerivedTypeDef
857   struct {
858     const parser::Name *extends{nullptr}; // EXTENDS(name)
859     bool privateComps{false}; // components are private by default
860     bool privateBindings{false}; // bindings are private by default
861     bool sawContains{false}; // currently processing bindings
862     bool sequence{false}; // is a sequence type
863     const Symbol *type{nullptr}; // derived type being defined
864   } derivedTypeInfo_;
865   // Collect equivalence sets and process at end of specification part
866   std::vector<const std::list<parser::EquivalenceObject> *> equivalenceSets_;
867   // Info about common blocks in the current scope
868   struct {
869     Symbol *curr{nullptr}; // common block currently being processed
870     std::set<SourceName> names; // names in any common block of scope
871   } commonBlockInfo_;
872   // Info about about SAVE statements and attributes in current scope
873   struct {
874     std::optional<SourceName> saveAll; // "SAVE" without entity list
875     std::set<SourceName> entities; // names of entities with save attr
876     std::set<SourceName> commons; // names of common blocks with save attr
877   } saveInfo_;
878   // In a ProcedureDeclarationStmt or ProcComponentDefStmt, this is
879   // the interface name, if any.
880   const parser::Name *interfaceName_{nullptr};
881   // Map type-bound generic to binding names of its specific bindings
882   std::multimap<Symbol *, const parser::Name *> genericBindings_;
883   // Info about current ENUM
884   struct EnumeratorState {
885     // Enum value must hold inside a C_INT (7.6.2).
886     std::optional<int> value{0};
887   } enumerationState_;
888 
889   bool HandleAttributeStmt(Attr, const std::list<parser::Name> &);
890   Symbol &HandleAttributeStmt(Attr, const parser::Name &);
891   Symbol &DeclareUnknownEntity(const parser::Name &, Attrs);
892   Symbol &DeclareProcEntity(const parser::Name &, Attrs, const ProcInterface &);
893   void SetType(const parser::Name &, const DeclTypeSpec &);
894   std::optional<DerivedTypeSpec> ResolveDerivedType(const parser::Name &);
895   std::optional<DerivedTypeSpec> ResolveExtendsType(
896       const parser::Name &, const parser::Name *);
897   Symbol *MakeTypeSymbol(const SourceName &, Details &&);
898   Symbol *MakeTypeSymbol(const parser::Name &, Details &&);
899   bool OkToAddComponent(const parser::Name &, const Symbol * = nullptr);
900   ParamValue GetParamValue(
901       const parser::TypeParamValue &, common::TypeParamAttr attr);
902   Symbol &MakeCommonBlockSymbol(const parser::Name &);
903   void CheckCommonBlockDerivedType(const SourceName &, const Symbol &);
904   std::optional<MessageFixedText> CheckSaveAttr(const Symbol &);
905   Attrs HandleSaveName(const SourceName &, Attrs);
906   void AddSaveName(std::set<SourceName> &, const SourceName &);
907   void SetSaveAttr(Symbol &);
908   bool HandleUnrestrictedSpecificIntrinsicFunction(const parser::Name &);
909   const parser::Name *FindComponent(const parser::Name *, const parser::Name &);
910   void CheckInitialDataTarget(const Symbol &, const SomeExpr &, SourceName);
911   void CheckInitialProcTarget(const Symbol &, const parser::Name &, SourceName);
912   void Initialization(const parser::Name &, const parser::Initialization &,
913       bool inComponentDecl);
914   bool PassesLocalityChecks(const parser::Name &name, Symbol &symbol);
915 
916   // Declare an object or procedure entity.
917   // T is one of: EntityDetails, ObjectEntityDetails, ProcEntityDetails
918   template <typename T>
919   Symbol &DeclareEntity(const parser::Name &name, Attrs attrs) {
920     Symbol &symbol{MakeSymbol(name, attrs)};
921     if (symbol.has<T>()) {
922       // OK
923     } else if (symbol.has<UnknownDetails>()) {
924       symbol.set_details(T{});
925     } else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
926       symbol.set_details(T{std::move(*details)});
927     } else if (std::is_same_v<EntityDetails, T> &&
928         (symbol.has<ObjectEntityDetails>() ||
929             symbol.has<ProcEntityDetails>())) {
930       // OK
931     } else if (auto *details{symbol.detailsIf<UseDetails>()}) {
932       Say(name.source,
933           "'%s' is use-associated from module '%s' and cannot be re-declared"_err_en_US,
934           name.source, details->module().name());
935     } else if (auto *details{symbol.detailsIf<SubprogramNameDetails>()}) {
936       if (details->kind() == SubprogramKind::Module) {
937         Say2(name,
938             "Declaration of '%s' conflicts with its use as module procedure"_err_en_US,
939             symbol, "Module procedure definition"_en_US);
940       } else if (details->kind() == SubprogramKind::Internal) {
941         Say2(name,
942             "Declaration of '%s' conflicts with its use as internal procedure"_err_en_US,
943             symbol, "Internal procedure definition"_en_US);
944       } else {
945         DIE("unexpected kind");
946       }
947     } else if (std::is_same_v<ObjectEntityDetails, T> &&
948         symbol.has<ProcEntityDetails>()) {
949       SayWithDecl(
950           name, symbol, "'%s' is already declared as a procedure"_err_en_US);
951     } else if (std::is_same_v<ProcEntityDetails, T> &&
952         symbol.has<ObjectEntityDetails>()) {
953       SayWithDecl(
954           name, symbol, "'%s' is already declared as an object"_err_en_US);
955     } else {
956       SayAlreadyDeclared(name, symbol);
957     }
958     return symbol;
959   }
960 };
961 
962 // Resolve construct entities and statement entities.
963 // Check that construct names don't conflict with other names.
964 class ConstructVisitor : public virtual DeclarationVisitor {
965 public:
966   bool Pre(const parser::ConcurrentHeader &);
967   bool Pre(const parser::LocalitySpec::Local &);
968   bool Pre(const parser::LocalitySpec::LocalInit &);
969   bool Pre(const parser::LocalitySpec::Shared &);
970   bool Pre(const parser::AcSpec &);
971   bool Pre(const parser::AcImpliedDo &);
972   bool Pre(const parser::DataImpliedDo &);
973   bool Pre(const parser::DataStmtObject &);
974   bool Pre(const parser::DataStmtValue &);
975   bool Pre(const parser::DoConstruct &);
976   void Post(const parser::DoConstruct &);
977   bool Pre(const parser::ForallConstruct &);
978   void Post(const parser::ForallConstruct &);
979   bool Pre(const parser::ForallStmt &);
980   void Post(const parser::ForallStmt &);
981   bool Pre(const parser::BlockStmt &);
982   bool Pre(const parser::EndBlockStmt &);
983   void Post(const parser::Selector &);
984   bool Pre(const parser::AssociateStmt &);
985   void Post(const parser::EndAssociateStmt &);
986   void Post(const parser::Association &);
987   void Post(const parser::SelectTypeStmt &);
988   void Post(const parser::SelectRankStmt &);
989   bool Pre(const parser::SelectTypeConstruct &);
990   void Post(const parser::SelectTypeConstruct &);
991   bool Pre(const parser::SelectTypeConstruct::TypeCase &);
992   void Post(const parser::SelectTypeConstruct::TypeCase &);
993   // Creates Block scopes with neither symbol name nor symbol details.
994   bool Pre(const parser::SelectRankConstruct::RankCase &);
995   void Post(const parser::SelectRankConstruct::RankCase &);
996   void Post(const parser::TypeGuardStmt::Guard &);
997   void Post(const parser::SelectRankCaseStmt::Rank &);
998   bool Pre(const parser::ChangeTeamStmt &);
999   void Post(const parser::EndChangeTeamStmt &);
1000   void Post(const parser::CoarrayAssociation &);
1001 
1002   // Definitions of construct names
1003   bool Pre(const parser::WhereConstructStmt &x) { return CheckDef(x.t); }
1004   bool Pre(const parser::ForallConstructStmt &x) { return CheckDef(x.t); }
1005   bool Pre(const parser::CriticalStmt &x) { return CheckDef(x.t); }
1006   bool Pre(const parser::LabelDoStmt &) {
1007     return false; // error recovery
1008   }
1009   bool Pre(const parser::NonLabelDoStmt &x) { return CheckDef(x.t); }
1010   bool Pre(const parser::IfThenStmt &x) { return CheckDef(x.t); }
1011   bool Pre(const parser::SelectCaseStmt &x) { return CheckDef(x.t); }
1012   bool Pre(const parser::SelectRankConstruct &);
1013   void Post(const parser::SelectRankConstruct &);
1014   bool Pre(const parser::SelectRankStmt &x) {
1015     return CheckDef(std::get<0>(x.t));
1016   }
1017   bool Pre(const parser::SelectTypeStmt &x) {
1018     return CheckDef(std::get<0>(x.t));
1019   }
1020 
1021   // References to construct names
1022   void Post(const parser::MaskedElsewhereStmt &x) { CheckRef(x.t); }
1023   void Post(const parser::ElsewhereStmt &x) { CheckRef(x.v); }
1024   void Post(const parser::EndWhereStmt &x) { CheckRef(x.v); }
1025   void Post(const parser::EndForallStmt &x) { CheckRef(x.v); }
1026   void Post(const parser::EndCriticalStmt &x) { CheckRef(x.v); }
1027   void Post(const parser::EndDoStmt &x) { CheckRef(x.v); }
1028   void Post(const parser::ElseIfStmt &x) { CheckRef(x.t); }
1029   void Post(const parser::ElseStmt &x) { CheckRef(x.v); }
1030   void Post(const parser::EndIfStmt &x) { CheckRef(x.v); }
1031   void Post(const parser::CaseStmt &x) { CheckRef(x.t); }
1032   void Post(const parser::EndSelectStmt &x) { CheckRef(x.v); }
1033   void Post(const parser::SelectRankCaseStmt &x) { CheckRef(x.t); }
1034   void Post(const parser::TypeGuardStmt &x) { CheckRef(x.t); }
1035   void Post(const parser::CycleStmt &x) { CheckRef(x.v); }
1036   void Post(const parser::ExitStmt &x) { CheckRef(x.v); }
1037 
1038 private:
1039   // R1105 selector -> expr | variable
1040   // expr is set in either case unless there were errors
1041   struct Selector {
1042     Selector() {}
1043     Selector(const SourceName &source, MaybeExpr &&expr)
1044         : source{source}, expr{std::move(expr)} {}
1045     operator bool() const { return expr.has_value(); }
1046     parser::CharBlock source;
1047     MaybeExpr expr;
1048   };
1049   // association -> [associate-name =>] selector
1050   struct Association {
1051     const parser::Name *name{nullptr};
1052     Selector selector;
1053   };
1054   std::vector<Association> associationStack_;
1055 
1056   template <typename T> bool CheckDef(const T &t) {
1057     return CheckDef(std::get<std::optional<parser::Name>>(t));
1058   }
1059   template <typename T> void CheckRef(const T &t) {
1060     CheckRef(std::get<std::optional<parser::Name>>(t));
1061   }
1062   bool CheckDef(const std::optional<parser::Name> &);
1063   void CheckRef(const std::optional<parser::Name> &);
1064   const DeclTypeSpec &ToDeclTypeSpec(evaluate::DynamicType &&);
1065   const DeclTypeSpec &ToDeclTypeSpec(
1066       evaluate::DynamicType &&, MaybeSubscriptIntExpr &&length);
1067   Symbol *MakeAssocEntity();
1068   void SetTypeFromAssociation(Symbol &);
1069   void SetAttrsFromAssociation(Symbol &);
1070   Selector ResolveSelector(const parser::Selector &);
1071   void ResolveIndexName(const parser::ConcurrentControl &control);
1072   Association &GetCurrentAssociation();
1073   void PushAssociation();
1074   void PopAssociation();
1075 };
1076 
1077 // Create scopes for OpenMP constructs
1078 class OmpVisitor : public virtual DeclarationVisitor {
1079 public:
1080   void AddOmpSourceRange(const parser::CharBlock &);
1081 
1082   static bool NeedsScope(const parser::OpenMPBlockConstruct &);
1083 
1084   bool Pre(const parser::OpenMPBlockConstruct &);
1085   void Post(const parser::OpenMPBlockConstruct &);
1086   bool Pre(const parser::OmpBeginBlockDirective &x) {
1087     AddOmpSourceRange(x.source);
1088     return true;
1089   }
1090   void Post(const parser::OmpBeginBlockDirective &) {
1091     messageHandler().set_currStmtSource(std::nullopt);
1092   }
1093   bool Pre(const parser::OmpEndBlockDirective &x) {
1094     AddOmpSourceRange(x.source);
1095     return true;
1096   }
1097   void Post(const parser::OmpEndBlockDirective &) {
1098     messageHandler().set_currStmtSource(std::nullopt);
1099   }
1100 
1101   bool Pre(const parser::OpenMPLoopConstruct &) {
1102     PushScope(Scope::Kind::Block, nullptr);
1103     return true;
1104   }
1105   void Post(const parser::OpenMPLoopConstruct &) { PopScope(); }
1106   bool Pre(const parser::OmpBeginLoopDirective &x) {
1107     AddOmpSourceRange(x.source);
1108     return true;
1109   }
1110   void Post(const parser::OmpBeginLoopDirective &) {
1111     messageHandler().set_currStmtSource(std::nullopt);
1112   }
1113   bool Pre(const parser::OmpEndLoopDirective &x) {
1114     AddOmpSourceRange(x.source);
1115     return true;
1116   }
1117   void Post(const parser::OmpEndLoopDirective &) {
1118     messageHandler().set_currStmtSource(std::nullopt);
1119   }
1120 
1121   bool Pre(const parser::OpenMPSectionsConstruct &) {
1122     PushScope(Scope::Kind::Block, nullptr);
1123     return true;
1124   }
1125   void Post(const parser::OpenMPSectionsConstruct &) { PopScope(); }
1126   bool Pre(const parser::OmpBeginSectionsDirective &x) {
1127     AddOmpSourceRange(x.source);
1128     return true;
1129   }
1130   void Post(const parser::OmpBeginSectionsDirective &) {
1131     messageHandler().set_currStmtSource(std::nullopt);
1132   }
1133   bool Pre(const parser::OmpEndSectionsDirective &x) {
1134     AddOmpSourceRange(x.source);
1135     return true;
1136   }
1137   void Post(const parser::OmpEndSectionsDirective &) {
1138     messageHandler().set_currStmtSource(std::nullopt);
1139   }
1140 };
1141 
1142 bool OmpVisitor::NeedsScope(const parser::OpenMPBlockConstruct &x) {
1143   const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
1144   const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
1145   switch (beginDir.v) {
1146   case parser::OmpBlockDirective::Directive::TargetData:
1147   case parser::OmpBlockDirective::Directive::Master:
1148   case parser::OmpBlockDirective::Directive::Ordered:
1149     return false;
1150   default:
1151     return true;
1152   }
1153 }
1154 
1155 void OmpVisitor::AddOmpSourceRange(const parser::CharBlock &source) {
1156   messageHandler().set_currStmtSource(source);
1157   currScope().AddSourceRange(source);
1158 }
1159 
1160 bool OmpVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
1161   if (NeedsScope(x)) {
1162     PushScope(Scope::Kind::Block, nullptr);
1163   }
1164   return true;
1165 }
1166 
1167 void OmpVisitor::Post(const parser::OpenMPBlockConstruct &x) {
1168   if (NeedsScope(x)) {
1169     PopScope();
1170   }
1171 }
1172 
1173 // Data-sharing and Data-mapping attributes for data-refs in OpenMP construct
1174 class OmpAttributeVisitor {
1175 public:
1176   explicit OmpAttributeVisitor(
1177       SemanticsContext &context, ResolveNamesVisitor &resolver)
1178       : context_{context}, resolver_{resolver} {}
1179 
1180   template <typename A> void Walk(const A &x) { parser::Walk(x, *this); }
1181 
1182   template <typename A> bool Pre(const A &) { return true; }
1183   template <typename A> void Post(const A &) {}
1184 
1185   bool Pre(const parser::SpecificationPart &x) {
1186     Walk(std::get<std::list<parser::OpenMPDeclarativeConstruct>>(x.t));
1187     return false;
1188   }
1189 
1190   bool Pre(const parser::OpenMPBlockConstruct &);
1191   void Post(const parser::OpenMPBlockConstruct &) { PopContext(); }
1192   void Post(const parser::OmpBeginBlockDirective &) {
1193     GetContext().withinConstruct = true;
1194   }
1195 
1196   bool Pre(const parser::OpenMPLoopConstruct &);
1197   void Post(const parser::OpenMPLoopConstruct &) { PopContext(); }
1198   void Post(const parser::OmpBeginLoopDirective &) {
1199     GetContext().withinConstruct = true;
1200   }
1201   bool Pre(const parser::DoConstruct &);
1202 
1203   bool Pre(const parser::OpenMPSectionsConstruct &);
1204   void Post(const parser::OpenMPSectionsConstruct &) { PopContext(); }
1205 
1206   bool Pre(const parser::OpenMPThreadprivate &);
1207   void Post(const parser::OpenMPThreadprivate &) { PopContext(); }
1208 
1209   // 2.15.3 Data-Sharing Attribute Clauses
1210   void Post(const parser::OmpDefaultClause &);
1211   bool Pre(const parser::OmpClause::Shared &x) {
1212     ResolveOmpObjectList(x.v, Symbol::Flag::OmpShared);
1213     return false;
1214   }
1215   bool Pre(const parser::OmpClause::Private &x) {
1216     ResolveOmpObjectList(x.v, Symbol::Flag::OmpPrivate);
1217     return false;
1218   }
1219   bool Pre(const parser::OmpClause::Firstprivate &x) {
1220     ResolveOmpObjectList(x.v, Symbol::Flag::OmpFirstPrivate);
1221     return false;
1222   }
1223   bool Pre(const parser::OmpClause::Lastprivate &x) {
1224     ResolveOmpObjectList(x.v, Symbol::Flag::OmpLastPrivate);
1225     return false;
1226   }
1227 
1228   void Post(const parser::Name &);
1229 
1230 private:
1231   struct OmpContext {
1232     OmpContext(const parser::CharBlock &source, OmpDirective d, Scope &s)
1233         : directiveSource{source}, directive{d}, scope{s} {}
1234     parser::CharBlock directiveSource;
1235     OmpDirective directive;
1236     Scope &scope;
1237     // TODO: default DSA is implicitly determined in different ways
1238     Symbol::Flag defaultDSA{Symbol::Flag::OmpShared};
1239     // variables on Data-sharing attribute clauses
1240     std::map<const Symbol *, Symbol::Flag> objectWithDSA;
1241     bool withinConstruct{false};
1242     std::size_t associatedLoopLevel{0};
1243   };
1244   // back() is the top of the stack
1245   OmpContext &GetContext() {
1246     CHECK(!ompContext_.empty());
1247     return ompContext_.back();
1248   }
1249   void PushContext(const parser::CharBlock &source, OmpDirective dir) {
1250     ompContext_.emplace_back(source, dir, context_.FindScope(source));
1251   }
1252   void PopContext() { ompContext_.pop_back(); }
1253   void SetContextDirectiveSource(parser::CharBlock &dir) {
1254     GetContext().directiveSource = dir;
1255   }
1256   void SetContextDirectiveEnum(OmpDirective dir) {
1257     GetContext().directive = dir;
1258   }
1259   Scope &currScope() { return GetContext().scope; }
1260   void SetContextDefaultDSA(Symbol::Flag flag) {
1261     GetContext().defaultDSA = flag;
1262   }
1263   void AddToContextObjectWithDSA(
1264       const Symbol &symbol, Symbol::Flag flag, OmpContext &context) {
1265     context.objectWithDSA.emplace(&symbol, flag);
1266   }
1267   void AddToContextObjectWithDSA(const Symbol &symbol, Symbol::Flag flag) {
1268     AddToContextObjectWithDSA(symbol, flag, GetContext());
1269   }
1270   bool IsObjectWithDSA(const Symbol &symbol) {
1271     auto it{GetContext().objectWithDSA.find(&symbol)};
1272     return it != GetContext().objectWithDSA.end();
1273   }
1274 
1275   void SetContextAssociatedLoopLevel(std::size_t level) {
1276     GetContext().associatedLoopLevel = level;
1277   }
1278   std::size_t GetAssociatedLoopLevelFromClauses(const parser::OmpClauseList &);
1279 
1280   Symbol &MakeAssocSymbol(const SourceName &name, Symbol &prev, Scope &scope) {
1281     const auto pair{scope.try_emplace(name, Attrs{}, HostAssocDetails{prev})};
1282     return *pair.first->second;
1283   }
1284   Symbol &MakeAssocSymbol(const SourceName &name, Symbol &prev) {
1285     return MakeAssocSymbol(name, prev, currScope());
1286   }
1287 
1288   static const parser::Name *GetDesignatorNameIfDataRef(
1289       const parser::Designator &designator) {
1290     const auto *dataRef{std::get_if<parser::DataRef>(&designator.u)};
1291     return dataRef ? std::get_if<parser::Name>(&dataRef->u) : nullptr;
1292   }
1293 
1294   static constexpr Symbol::Flags dataSharingAttributeFlags{
1295       Symbol::Flag::OmpShared, Symbol::Flag::OmpPrivate,
1296       Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate,
1297       Symbol::Flag::OmpReduction, Symbol::Flag::OmpLinear};
1298 
1299   static constexpr Symbol::Flags ompFlagsRequireNewSymbol{
1300       Symbol::Flag::OmpPrivate, Symbol::Flag::OmpLinear,
1301       Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate,
1302       Symbol::Flag::OmpReduction};
1303 
1304   static constexpr Symbol::Flags ompFlagsRequireMark{
1305       Symbol::Flag::OmpThreadprivate};
1306 
1307   void AddDataSharingAttributeObject(SymbolRef object) {
1308     dataSharingAttributeObjects_.insert(object);
1309   }
1310   void ClearDataSharingAttributeObjects() {
1311     dataSharingAttributeObjects_.clear();
1312   }
1313   bool HasDataSharingAttributeObject(const Symbol &);
1314 
1315   const parser::DoConstruct *GetDoConstructIf(
1316       const parser::ExecutionPartConstruct &);
1317   // Predetermined DSA rules
1318   void PrivatizeAssociatedLoopIndex(const parser::OpenMPLoopConstruct &);
1319   const parser::Name &GetLoopIndex(const parser::DoConstruct &);
1320   void ResolveSeqLoopIndexInParallelOrTaskConstruct(const parser::Name &);
1321 
1322   void ResolveOmpObjectList(const parser::OmpObjectList &, Symbol::Flag);
1323   void ResolveOmpObject(const parser::OmpObject &, Symbol::Flag);
1324   Symbol *ResolveOmp(const parser::Name &, Symbol::Flag, Scope &);
1325   Symbol *ResolveOmp(Symbol &, Symbol::Flag, Scope &);
1326   Symbol *ResolveOmpCommonBlockName(const parser::Name *);
1327   Symbol *DeclarePrivateAccessEntity(
1328       const parser::Name &, Symbol::Flag, Scope &);
1329   Symbol *DeclarePrivateAccessEntity(Symbol &, Symbol::Flag, Scope &);
1330   Symbol *DeclareOrMarkOtherAccessEntity(const parser::Name &, Symbol::Flag);
1331   Symbol *DeclareOrMarkOtherAccessEntity(Symbol &, Symbol::Flag);
1332   void CheckMultipleAppearances(
1333       const parser::Name &, const Symbol &, Symbol::Flag);
1334   SymbolSet dataSharingAttributeObjects_; // on one directive
1335 
1336   SemanticsContext &context_;
1337   ResolveNamesVisitor &resolver_;
1338   std::vector<OmpContext> ompContext_; // used as a stack
1339 };
1340 
1341 // Walk the parse tree and resolve names to symbols.
1342 class ResolveNamesVisitor : public virtual ScopeHandler,
1343                             public ModuleVisitor,
1344                             public SubprogramVisitor,
1345                             public ConstructVisitor,
1346                             public OmpVisitor {
1347 public:
1348   using ArraySpecVisitor::Post;
1349   using ConstructVisitor::Post;
1350   using ConstructVisitor::Pre;
1351   using DeclarationVisitor::Post;
1352   using DeclarationVisitor::Pre;
1353   using ImplicitRulesVisitor::Post;
1354   using ImplicitRulesVisitor::Pre;
1355   using InterfaceVisitor::Post;
1356   using InterfaceVisitor::Pre;
1357   using ModuleVisitor::Post;
1358   using ModuleVisitor::Pre;
1359   using OmpVisitor::Post;
1360   using OmpVisitor::Pre;
1361   using ScopeHandler::Post;
1362   using ScopeHandler::Pre;
1363   using SubprogramVisitor::Post;
1364   using SubprogramVisitor::Pre;
1365 
1366   ResolveNamesVisitor(SemanticsContext &context, ImplicitRulesMap &rules)
1367       : BaseVisitor{context, *this, rules} {
1368     PushScope(context.globalScope());
1369   }
1370 
1371   // Default action for a parse tree node is to visit children.
1372   template <typename T> bool Pre(const T &) { return true; }
1373   template <typename T> void Post(const T &) {}
1374 
1375   bool Pre(const parser::SpecificationPart &);
1376   void Post(const parser::Program &);
1377   bool Pre(const parser::ImplicitStmt &);
1378   void Post(const parser::PointerObject &);
1379   void Post(const parser::AllocateObject &);
1380   bool Pre(const parser::PointerAssignmentStmt &);
1381   void Post(const parser::Designator &);
1382   template <typename A, typename B>
1383   void Post(const parser::LoopBounds<A, B> &x) {
1384     ResolveName(*parser::Unwrap<parser::Name>(x.name));
1385   }
1386   void Post(const parser::ProcComponentRef &);
1387   bool Pre(const parser::ActualArg &);
1388   bool Pre(const parser::FunctionReference &);
1389   bool Pre(const parser::CallStmt &);
1390   bool Pre(const parser::ImportStmt &);
1391   void Post(const parser::TypeGuardStmt &);
1392   bool Pre(const parser::StmtFunctionStmt &);
1393   bool Pre(const parser::DefinedOpName &);
1394   bool Pre(const parser::ProgramUnit &);
1395   void Post(const parser::AssignStmt &);
1396   void Post(const parser::AssignedGotoStmt &);
1397 
1398   // These nodes should never be reached: they are handled in ProgramUnit
1399   bool Pre(const parser::MainProgram &) { DIE("unreachable"); }
1400   bool Pre(const parser::FunctionSubprogram &) { DIE("unreachable"); }
1401   bool Pre(const parser::SubroutineSubprogram &) { DIE("unreachable"); }
1402   bool Pre(const parser::SeparateModuleSubprogram &) { DIE("unreachable"); }
1403   bool Pre(const parser::Module &) { DIE("unreachable"); }
1404   bool Pre(const parser::Submodule &) { DIE("unreachable"); }
1405   bool Pre(const parser::BlockData &) { DIE("unreachable"); }
1406 
1407   void NoteExecutablePartCall(Symbol::Flag, const parser::Call &);
1408 
1409   friend void ResolveSpecificationParts(SemanticsContext &, const Symbol &);
1410 
1411 private:
1412   // Kind of procedure we are expecting to see in a ProcedureDesignator
1413   std::optional<Symbol::Flag> expectedProcFlag_;
1414   std::optional<SourceName> prevImportStmt_;
1415 
1416   void PreSpecificationConstruct(const parser::SpecificationConstruct &);
1417   void CreateGeneric(const parser::GenericSpec &);
1418   void FinishSpecificationPart();
1419   void CheckImports();
1420   void CheckImport(const SourceName &, const SourceName &);
1421   void HandleCall(Symbol::Flag, const parser::Call &);
1422   void HandleProcedureName(Symbol::Flag, const parser::Name &);
1423   bool SetProcFlag(const parser::Name &, Symbol &, Symbol::Flag);
1424   void ResolveSpecificationParts(ProgramTree &);
1425   void AddSubpNames(ProgramTree &);
1426   bool BeginScopeForNode(const ProgramTree &);
1427   void FinishSpecificationParts(const ProgramTree &);
1428   void FinishDerivedTypeInstantiation(Scope &);
1429   void ResolveExecutionParts(const ProgramTree &);
1430   void ResolveOmpParts(const parser::ProgramUnit &);
1431 };
1432 
1433 // ImplicitRules implementation
1434 
1435 bool ImplicitRules::isImplicitNoneType() const {
1436   if (isImplicitNoneType_) {
1437     return true;
1438   } else if (map_.empty() && inheritFromParent_) {
1439     return parent_->isImplicitNoneType();
1440   } else {
1441     return false; // default if not specified
1442   }
1443 }
1444 
1445 bool ImplicitRules::isImplicitNoneExternal() const {
1446   if (isImplicitNoneExternal_) {
1447     return true;
1448   } else if (inheritFromParent_) {
1449     return parent_->isImplicitNoneExternal();
1450   } else {
1451     return false; // default if not specified
1452   }
1453 }
1454 
1455 const DeclTypeSpec *ImplicitRules::GetType(char ch) const {
1456   if (isImplicitNoneType_) {
1457     return nullptr;
1458   } else if (auto it{map_.find(ch)}; it != map_.end()) {
1459     return &*it->second;
1460   } else if (inheritFromParent_) {
1461     return parent_->GetType(ch);
1462   } else if (ch >= 'i' && ch <= 'n') {
1463     return &context_.MakeNumericType(TypeCategory::Integer);
1464   } else if (ch >= 'a' && ch <= 'z') {
1465     return &context_.MakeNumericType(TypeCategory::Real);
1466   } else {
1467     return nullptr;
1468   }
1469 }
1470 
1471 void ImplicitRules::SetTypeMapping(const DeclTypeSpec &type,
1472     parser::Location fromLetter, parser::Location toLetter) {
1473   for (char ch = *fromLetter; ch; ch = ImplicitRules::Incr(ch)) {
1474     auto res{map_.emplace(ch, type)};
1475     if (!res.second) {
1476       context_.Say(parser::CharBlock{fromLetter},
1477           "More than one implicit type specified for '%c'"_err_en_US, ch);
1478     }
1479     if (ch == *toLetter) {
1480       break;
1481     }
1482   }
1483 }
1484 
1485 // Return the next char after ch in a way that works for ASCII or EBCDIC.
1486 // Return '\0' for the char after 'z'.
1487 char ImplicitRules::Incr(char ch) {
1488   switch (ch) {
1489   case 'i':
1490     return 'j';
1491   case 'r':
1492     return 's';
1493   case 'z':
1494     return '\0';
1495   default:
1496     return ch + 1;
1497   }
1498 }
1499 
1500 llvm::raw_ostream &operator<<(
1501     llvm::raw_ostream &o, const ImplicitRules &implicitRules) {
1502   o << "ImplicitRules:\n";
1503   for (char ch = 'a'; ch; ch = ImplicitRules::Incr(ch)) {
1504     ShowImplicitRule(o, implicitRules, ch);
1505   }
1506   ShowImplicitRule(o, implicitRules, '_');
1507   ShowImplicitRule(o, implicitRules, '$');
1508   ShowImplicitRule(o, implicitRules, '@');
1509   return o;
1510 }
1511 void ShowImplicitRule(
1512     llvm::raw_ostream &o, const ImplicitRules &implicitRules, char ch) {
1513   auto it{implicitRules.map_.find(ch)};
1514   if (it != implicitRules.map_.end()) {
1515     o << "  " << ch << ": " << *it->second << '\n';
1516   }
1517 }
1518 
1519 template <typename T> void BaseVisitor::Walk(const T &x) {
1520   parser::Walk(x, *this_);
1521 }
1522 
1523 void BaseVisitor::MakePlaceholder(
1524     const parser::Name &name, MiscDetails::Kind kind) {
1525   if (!name.symbol) {
1526     name.symbol = &context_->globalScope().MakeSymbol(
1527         name.source, Attrs{}, MiscDetails{kind});
1528   }
1529 }
1530 
1531 // AttrsVisitor implementation
1532 
1533 bool AttrsVisitor::BeginAttrs() {
1534   CHECK(!attrs_);
1535   attrs_ = std::make_optional<Attrs>();
1536   return true;
1537 }
1538 Attrs AttrsVisitor::GetAttrs() {
1539   CHECK(attrs_);
1540   return *attrs_;
1541 }
1542 Attrs AttrsVisitor::EndAttrs() {
1543   Attrs result{GetAttrs()};
1544   attrs_.reset();
1545   passName_ = std::nullopt;
1546   bindName_.reset();
1547   return result;
1548 }
1549 
1550 bool AttrsVisitor::SetPassNameOn(Symbol &symbol) {
1551   if (!passName_) {
1552     return false;
1553   }
1554   std::visit(common::visitors{
1555                  [&](ProcEntityDetails &x) { x.set_passName(*passName_); },
1556                  [&](ProcBindingDetails &x) { x.set_passName(*passName_); },
1557                  [](auto &) { common::die("unexpected pass name"); },
1558              },
1559       symbol.details());
1560   return true;
1561 }
1562 
1563 bool AttrsVisitor::SetBindNameOn(Symbol &symbol) {
1564   if (!bindName_) {
1565     return false;
1566   }
1567   std::visit(
1568       common::visitors{
1569           [&](EntityDetails &x) { x.set_bindName(std::move(bindName_)); },
1570           [&](ObjectEntityDetails &x) { x.set_bindName(std::move(bindName_)); },
1571           [&](ProcEntityDetails &x) { x.set_bindName(std::move(bindName_)); },
1572           [&](SubprogramDetails &x) { x.set_bindName(std::move(bindName_)); },
1573           [&](CommonBlockDetails &x) { x.set_bindName(std::move(bindName_)); },
1574           [](auto &) { common::die("unexpected bind name"); },
1575       },
1576       symbol.details());
1577   return true;
1578 }
1579 
1580 void AttrsVisitor::Post(const parser::LanguageBindingSpec &x) {
1581   CHECK(attrs_);
1582   if (CheckAndSet(Attr::BIND_C)) {
1583     if (x.v) {
1584       bindName_ = EvaluateExpr(*x.v);
1585     }
1586   }
1587 }
1588 bool AttrsVisitor::Pre(const parser::IntentSpec &x) {
1589   CHECK(attrs_);
1590   CheckAndSet(IntentSpecToAttr(x));
1591   return false;
1592 }
1593 bool AttrsVisitor::Pre(const parser::Pass &x) {
1594   if (CheckAndSet(Attr::PASS)) {
1595     if (x.v) {
1596       passName_ = x.v->source;
1597       MakePlaceholder(*x.v, MiscDetails::Kind::PassName);
1598     }
1599   }
1600   return false;
1601 }
1602 
1603 // C730, C743, C755, C778, C1543 say no attribute or prefix repetitions
1604 bool AttrsVisitor::IsDuplicateAttr(Attr attrName) {
1605   if (attrs_->test(attrName)) {
1606     Say(currStmtSource().value(),
1607         "Attribute '%s' cannot be used more than once"_en_US,
1608         AttrToString(attrName));
1609     return true;
1610   }
1611   return false;
1612 }
1613 
1614 // See if attrName violates a constraint cause by a conflict.  attr1 and attr2
1615 // name attributes that cannot be used on the same declaration
1616 bool AttrsVisitor::HaveAttrConflict(Attr attrName, Attr attr1, Attr attr2) {
1617   if ((attrName == attr1 && attrs_->test(attr2)) ||
1618       (attrName == attr2 && attrs_->test(attr1))) {
1619     Say(currStmtSource().value(),
1620         "Attributes '%s' and '%s' conflict with each other"_err_en_US,
1621         AttrToString(attr1), AttrToString(attr2));
1622     return true;
1623   }
1624   return false;
1625 }
1626 // C759, C1543
1627 bool AttrsVisitor::IsConflictingAttr(Attr attrName) {
1628   return HaveAttrConflict(attrName, Attr::INTENT_IN, Attr::INTENT_INOUT) ||
1629       HaveAttrConflict(attrName, Attr::INTENT_IN, Attr::INTENT_OUT) ||
1630       HaveAttrConflict(attrName, Attr::INTENT_INOUT, Attr::INTENT_OUT) ||
1631       HaveAttrConflict(attrName, Attr::PASS, Attr::NOPASS) ||
1632       HaveAttrConflict(attrName, Attr::PURE, Attr::IMPURE) ||
1633       HaveAttrConflict(attrName, Attr::PUBLIC, Attr::PRIVATE) ||
1634       HaveAttrConflict(attrName, Attr::RECURSIVE, Attr::NON_RECURSIVE);
1635 }
1636 bool AttrsVisitor::CheckAndSet(Attr attrName) {
1637   CHECK(attrs_);
1638   if (IsConflictingAttr(attrName) || IsDuplicateAttr(attrName)) {
1639     return false;
1640   }
1641   attrs_->set(attrName);
1642   return true;
1643 }
1644 
1645 // DeclTypeSpecVisitor implementation
1646 
1647 const DeclTypeSpec *DeclTypeSpecVisitor::GetDeclTypeSpec() {
1648   return state_.declTypeSpec;
1649 }
1650 
1651 void DeclTypeSpecVisitor::BeginDeclTypeSpec() {
1652   CHECK(!state_.expectDeclTypeSpec);
1653   CHECK(!state_.declTypeSpec);
1654   state_.expectDeclTypeSpec = true;
1655 }
1656 void DeclTypeSpecVisitor::EndDeclTypeSpec() {
1657   CHECK(state_.expectDeclTypeSpec);
1658   state_ = {};
1659 }
1660 
1661 void DeclTypeSpecVisitor::SetDeclTypeSpecCategory(
1662     DeclTypeSpec::Category category) {
1663   CHECK(state_.expectDeclTypeSpec);
1664   state_.derived.category = category;
1665 }
1666 
1667 bool DeclTypeSpecVisitor::Pre(const parser::TypeGuardStmt &) {
1668   BeginDeclTypeSpec();
1669   return true;
1670 }
1671 void DeclTypeSpecVisitor::Post(const parser::TypeGuardStmt &) {
1672   EndDeclTypeSpec();
1673 }
1674 
1675 void DeclTypeSpecVisitor::Post(const parser::TypeSpec &typeSpec) {
1676   // Record the resolved DeclTypeSpec in the parse tree for use by
1677   // expression semantics if the DeclTypeSpec is a valid TypeSpec.
1678   // The grammar ensures that it's an intrinsic or derived type spec,
1679   // not TYPE(*) or CLASS(*) or CLASS(T).
1680   if (const DeclTypeSpec * spec{state_.declTypeSpec}) {
1681     switch (spec->category()) {
1682     case DeclTypeSpec::Numeric:
1683     case DeclTypeSpec::Logical:
1684     case DeclTypeSpec::Character:
1685       typeSpec.declTypeSpec = spec;
1686       break;
1687     case DeclTypeSpec::TypeDerived:
1688       if (const DerivedTypeSpec * derived{spec->AsDerived()}) {
1689         CheckForAbstractType(derived->typeSymbol()); // C703
1690         typeSpec.declTypeSpec = spec;
1691       }
1692       break;
1693     default:
1694       CRASH_NO_CASE;
1695     }
1696   }
1697 }
1698 
1699 void DeclTypeSpecVisitor::Post(
1700     const parser::IntrinsicTypeSpec::DoublePrecision &) {
1701   MakeNumericType(TypeCategory::Real, context().doublePrecisionKind());
1702 }
1703 void DeclTypeSpecVisitor::Post(
1704     const parser::IntrinsicTypeSpec::DoubleComplex &) {
1705   MakeNumericType(TypeCategory::Complex, context().doublePrecisionKind());
1706 }
1707 void DeclTypeSpecVisitor::MakeNumericType(TypeCategory category, int kind) {
1708   SetDeclTypeSpec(context().MakeNumericType(category, kind));
1709 }
1710 
1711 void DeclTypeSpecVisitor::CheckForAbstractType(const Symbol &typeSymbol) {
1712   if (typeSymbol.attrs().test(Attr::ABSTRACT)) {
1713     Say("ABSTRACT derived type may not be used here"_err_en_US);
1714   }
1715 }
1716 
1717 void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::ClassStar &) {
1718   SetDeclTypeSpec(context().globalScope().MakeClassStarType());
1719 }
1720 void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::TypeStar &) {
1721   SetDeclTypeSpec(context().globalScope().MakeTypeStarType());
1722 }
1723 
1724 // Check that we're expecting to see a DeclTypeSpec (and haven't seen one yet)
1725 // and save it in state_.declTypeSpec.
1726 void DeclTypeSpecVisitor::SetDeclTypeSpec(const DeclTypeSpec &declTypeSpec) {
1727   CHECK(state_.expectDeclTypeSpec);
1728   CHECK(!state_.declTypeSpec);
1729   state_.declTypeSpec = &declTypeSpec;
1730 }
1731 
1732 KindExpr DeclTypeSpecVisitor::GetKindParamExpr(
1733     TypeCategory category, const std::optional<parser::KindSelector> &kind) {
1734   return AnalyzeKindSelector(context(), category, kind);
1735 }
1736 
1737 // MessageHandler implementation
1738 
1739 Message &MessageHandler::Say(MessageFixedText &&msg) {
1740   return context_->Say(currStmtSource().value(), std::move(msg));
1741 }
1742 Message &MessageHandler::Say(MessageFormattedText &&msg) {
1743   return context_->Say(currStmtSource().value(), std::move(msg));
1744 }
1745 Message &MessageHandler::Say(const SourceName &name, MessageFixedText &&msg) {
1746   return Say(name, std::move(msg), name);
1747 }
1748 
1749 // ImplicitRulesVisitor implementation
1750 
1751 void ImplicitRulesVisitor::Post(const parser::ParameterStmt &) {
1752   prevParameterStmt_ = currStmtSource();
1753 }
1754 
1755 bool ImplicitRulesVisitor::Pre(const parser::ImplicitStmt &x) {
1756   bool result{
1757       std::visit(common::visitors{
1758                      [&](const std::list<ImplicitNoneNameSpec> &y) {
1759                        return HandleImplicitNone(y);
1760                      },
1761                      [&](const std::list<parser::ImplicitSpec> &) {
1762                        if (prevImplicitNoneType_) {
1763                          Say("IMPLICIT statement after IMPLICIT NONE or "
1764                              "IMPLICIT NONE(TYPE) statement"_err_en_US);
1765                          return false;
1766                        } else {
1767                          implicitRules().set_isImplicitNoneType(false);
1768                        }
1769                        return true;
1770                      },
1771                  },
1772           x.u)};
1773   prevImplicit_ = currStmtSource();
1774   return result;
1775 }
1776 
1777 bool ImplicitRulesVisitor::Pre(const parser::LetterSpec &x) {
1778   auto loLoc{std::get<parser::Location>(x.t)};
1779   auto hiLoc{loLoc};
1780   if (auto hiLocOpt{std::get<std::optional<parser::Location>>(x.t)}) {
1781     hiLoc = *hiLocOpt;
1782     if (*hiLoc < *loLoc) {
1783       Say(hiLoc, "'%s' does not follow '%s' alphabetically"_err_en_US,
1784           std::string(hiLoc, 1), std::string(loLoc, 1));
1785       return false;
1786     }
1787   }
1788   implicitRules().SetTypeMapping(*GetDeclTypeSpec(), loLoc, hiLoc);
1789   return false;
1790 }
1791 
1792 bool ImplicitRulesVisitor::Pre(const parser::ImplicitSpec &) {
1793   BeginDeclTypeSpec();
1794   set_allowForwardReferenceToDerivedType(true);
1795   return true;
1796 }
1797 
1798 void ImplicitRulesVisitor::Post(const parser::ImplicitSpec &) {
1799   EndDeclTypeSpec();
1800 }
1801 
1802 void ImplicitRulesVisitor::SetScope(const Scope &scope) {
1803   implicitRules_ = &DEREF(implicitRulesMap_).at(&scope);
1804   prevImplicit_ = std::nullopt;
1805   prevImplicitNone_ = std::nullopt;
1806   prevImplicitNoneType_ = std::nullopt;
1807   prevParameterStmt_ = std::nullopt;
1808 }
1809 void ImplicitRulesVisitor::BeginScope(const Scope &scope) {
1810   // find or create implicit rules for this scope
1811   DEREF(implicitRulesMap_).try_emplace(&scope, context(), implicitRules_);
1812   SetScope(scope);
1813 }
1814 
1815 // TODO: for all of these errors, reference previous statement too
1816 bool ImplicitRulesVisitor::HandleImplicitNone(
1817     const std::list<ImplicitNoneNameSpec> &nameSpecs) {
1818   if (prevImplicitNone_) {
1819     Say("More than one IMPLICIT NONE statement"_err_en_US);
1820     Say(*prevImplicitNone_, "Previous IMPLICIT NONE statement"_en_US);
1821     return false;
1822   }
1823   if (prevParameterStmt_) {
1824     Say("IMPLICIT NONE statement after PARAMETER statement"_err_en_US);
1825     return false;
1826   }
1827   prevImplicitNone_ = currStmtSource();
1828   if (nameSpecs.empty()) {
1829     prevImplicitNoneType_ = currStmtSource();
1830     implicitRules().set_isImplicitNoneType(true);
1831     if (prevImplicit_) {
1832       Say("IMPLICIT NONE statement after IMPLICIT statement"_err_en_US);
1833       return false;
1834     }
1835   } else {
1836     int sawType{0};
1837     int sawExternal{0};
1838     for (const auto noneSpec : nameSpecs) {
1839       switch (noneSpec) {
1840       case ImplicitNoneNameSpec::External:
1841         implicitRules().set_isImplicitNoneExternal(true);
1842         ++sawExternal;
1843         break;
1844       case ImplicitNoneNameSpec::Type:
1845         prevImplicitNoneType_ = currStmtSource();
1846         implicitRules().set_isImplicitNoneType(true);
1847         if (prevImplicit_) {
1848           Say("IMPLICIT NONE(TYPE) after IMPLICIT statement"_err_en_US);
1849           return false;
1850         }
1851         ++sawType;
1852         break;
1853       }
1854     }
1855     if (sawType > 1) {
1856       Say("TYPE specified more than once in IMPLICIT NONE statement"_err_en_US);
1857       return false;
1858     }
1859     if (sawExternal > 1) {
1860       Say("EXTERNAL specified more than once in IMPLICIT NONE statement"_err_en_US);
1861       return false;
1862     }
1863   }
1864   return true;
1865 }
1866 
1867 // ArraySpecVisitor implementation
1868 
1869 void ArraySpecVisitor::Post(const parser::ArraySpec &x) {
1870   CHECK(arraySpec_.empty());
1871   arraySpec_ = AnalyzeArraySpec(context(), x);
1872 }
1873 void ArraySpecVisitor::Post(const parser::ComponentArraySpec &x) {
1874   CHECK(arraySpec_.empty());
1875   arraySpec_ = AnalyzeArraySpec(context(), x);
1876 }
1877 void ArraySpecVisitor::Post(const parser::CoarraySpec &x) {
1878   CHECK(coarraySpec_.empty());
1879   coarraySpec_ = AnalyzeCoarraySpec(context(), x);
1880 }
1881 
1882 const ArraySpec &ArraySpecVisitor::arraySpec() {
1883   return !arraySpec_.empty() ? arraySpec_ : attrArraySpec_;
1884 }
1885 const ArraySpec &ArraySpecVisitor::coarraySpec() {
1886   return !coarraySpec_.empty() ? coarraySpec_ : attrCoarraySpec_;
1887 }
1888 void ArraySpecVisitor::BeginArraySpec() {
1889   CHECK(arraySpec_.empty());
1890   CHECK(coarraySpec_.empty());
1891   CHECK(attrArraySpec_.empty());
1892   CHECK(attrCoarraySpec_.empty());
1893 }
1894 void ArraySpecVisitor::EndArraySpec() {
1895   CHECK(arraySpec_.empty());
1896   CHECK(coarraySpec_.empty());
1897   attrArraySpec_.clear();
1898   attrCoarraySpec_.clear();
1899 }
1900 void ArraySpecVisitor::PostAttrSpec() {
1901   // Save dimension/codimension from attrs so we can process array/coarray-spec
1902   // on the entity-decl
1903   if (!arraySpec_.empty()) {
1904     if (attrArraySpec_.empty()) {
1905       attrArraySpec_ = arraySpec_;
1906       arraySpec_.clear();
1907     } else {
1908       Say(currStmtSource().value(),
1909           "Attribute 'DIMENSION' cannot be used more than once"_err_en_US);
1910     }
1911   }
1912   if (!coarraySpec_.empty()) {
1913     if (attrCoarraySpec_.empty()) {
1914       attrCoarraySpec_ = coarraySpec_;
1915       coarraySpec_.clear();
1916     } else {
1917       Say(currStmtSource().value(),
1918           "Attribute 'CODIMENSION' cannot be used more than once"_err_en_US);
1919     }
1920   }
1921 }
1922 
1923 // ScopeHandler implementation
1924 
1925 void ScopeHandler::SayAlreadyDeclared(const parser::Name &name, Symbol &prev) {
1926   SayAlreadyDeclared(name.source, prev);
1927 }
1928 void ScopeHandler::SayAlreadyDeclared(const SourceName &name, Symbol &prev) {
1929   if (context().HasError(prev)) {
1930     // don't report another error about prev
1931   } else if (const auto *details{prev.detailsIf<UseDetails>()}) {
1932     Say(name, "'%s' is already declared in this scoping unit"_err_en_US)
1933         .Attach(details->location(),
1934             "It is use-associated with '%s' in module '%s'"_err_en_US,
1935             details->symbol().name(), details->module().name());
1936   } else {
1937     SayAlreadyDeclared(name, prev.name());
1938   }
1939   context().SetError(prev);
1940 }
1941 void ScopeHandler::SayAlreadyDeclared(
1942     const SourceName &name1, const SourceName &name2) {
1943   if (name1.begin() < name2.begin()) {
1944     SayAlreadyDeclared(name2, name1);
1945   } else {
1946     Say(name1, "'%s' is already declared in this scoping unit"_err_en_US)
1947         .Attach(name2, "Previous declaration of '%s'"_en_US, name2);
1948   }
1949 }
1950 
1951 void ScopeHandler::SayWithReason(const parser::Name &name, Symbol &symbol,
1952     MessageFixedText &&msg1, MessageFixedText &&msg2) {
1953   Say2(name, std::move(msg1), symbol, std::move(msg2));
1954   context().SetError(symbol, msg1.isFatal());
1955 }
1956 
1957 void ScopeHandler::SayWithDecl(
1958     const parser::Name &name, Symbol &symbol, MessageFixedText &&msg) {
1959   SayWithReason(name, symbol, std::move(msg),
1960       symbol.test(Symbol::Flag::Implicit) ? "Implicit declaration of '%s'"_en_US
1961                                           : "Declaration of '%s'"_en_US);
1962 }
1963 
1964 void ScopeHandler::SayLocalMustBeVariable(
1965     const parser::Name &name, Symbol &symbol) {
1966   SayWithDecl(name, symbol,
1967       "The name '%s' must be a variable to appear"
1968       " in a locality-spec"_err_en_US);
1969 }
1970 
1971 void ScopeHandler::SayDerivedType(
1972     const SourceName &name, MessageFixedText &&msg, const Scope &type) {
1973   const Symbol &typeSymbol{DEREF(type.GetSymbol())};
1974   Say(name, std::move(msg), name, typeSymbol.name())
1975       .Attach(typeSymbol.name(), "Declaration of derived type '%s'"_en_US,
1976           typeSymbol.name());
1977 }
1978 void ScopeHandler::Say2(const SourceName &name1, MessageFixedText &&msg1,
1979     const SourceName &name2, MessageFixedText &&msg2) {
1980   Say(name1, std::move(msg1)).Attach(name2, std::move(msg2), name2);
1981 }
1982 void ScopeHandler::Say2(const SourceName &name, MessageFixedText &&msg1,
1983     Symbol &symbol, MessageFixedText &&msg2) {
1984   Say2(name, std::move(msg1), symbol.name(), std::move(msg2));
1985   context().SetError(symbol, msg1.isFatal());
1986 }
1987 void ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
1988     Symbol &symbol, MessageFixedText &&msg2) {
1989   Say2(name.source, std::move(msg1), symbol.name(), std::move(msg2));
1990   context().SetError(symbol, msg1.isFatal());
1991 }
1992 
1993 Scope &ScopeHandler::InclusiveScope() {
1994   for (auto *scope{&currScope()};; scope = &scope->parent()) {
1995     if (scope->kind() != Scope::Kind::Block && !scope->IsDerivedType()) {
1996       return *scope;
1997     }
1998   }
1999   DIE("inclusive scope not found");
2000 }
2001 
2002 void ScopeHandler::PushScope(Scope::Kind kind, Symbol *symbol) {
2003   PushScope(currScope().MakeScope(kind, symbol));
2004 }
2005 void ScopeHandler::PushScope(Scope &scope) {
2006   currScope_ = &scope;
2007   auto kind{currScope_->kind()};
2008   if (kind != Scope::Kind::Block) {
2009     BeginScope(scope);
2010   }
2011   // The name of a module or submodule cannot be "used" in its scope,
2012   // as we read 19.3.1(2), so we allow the name to be used as a local
2013   // identifier in the module or submodule too.  Same with programs
2014   // (14.1(3)) and BLOCK DATA.
2015   if (!currScope_->IsDerivedType() && kind != Scope::Kind::Module &&
2016       kind != Scope::Kind::MainProgram && kind != Scope::Kind::BlockData) {
2017     if (auto *symbol{scope.symbol()}) {
2018       // Create a dummy symbol so we can't create another one with the same
2019       // name. It might already be there if we previously pushed the scope.
2020       if (!FindInScope(scope, symbol->name())) {
2021         auto &newSymbol{MakeSymbol(symbol->name())};
2022         if (kind == Scope::Kind::Subprogram) {
2023           // Allow for recursive references.  If this symbol is a function
2024           // without an explicit RESULT(), this new symbol will be discarded
2025           // and replaced with an object of the same name.
2026           newSymbol.set_details(HostAssocDetails{*symbol});
2027         } else {
2028           newSymbol.set_details(MiscDetails{MiscDetails::Kind::ScopeName});
2029         }
2030       }
2031     }
2032   }
2033 }
2034 void ScopeHandler::PopScope() {
2035   // Entities that are not yet classified as objects or procedures are now
2036   // assumed to be objects.
2037   // TODO: Statement functions
2038   for (auto &pair : currScope()) {
2039     ConvertToObjectEntity(*pair.second);
2040   }
2041   SetScope(currScope_->parent());
2042 }
2043 void ScopeHandler::SetScope(Scope &scope) {
2044   currScope_ = &scope;
2045   ImplicitRulesVisitor::SetScope(InclusiveScope());
2046 }
2047 
2048 Symbol *ScopeHandler::FindSymbol(const parser::Name &name) {
2049   return FindSymbol(currScope(), name);
2050 }
2051 Symbol *ScopeHandler::FindSymbol(const Scope &scope, const parser::Name &name) {
2052   if (scope.IsDerivedType()) {
2053     if (Symbol * symbol{scope.FindComponent(name.source)}) {
2054       if (!symbol->has<ProcBindingDetails>() &&
2055           !symbol->test(Symbol::Flag::ParentComp)) {
2056         return Resolve(name, symbol);
2057       }
2058     }
2059     return FindSymbol(scope.parent(), name);
2060   } else {
2061     return Resolve(name, scope.FindSymbol(name.source));
2062   }
2063 }
2064 
2065 Symbol &ScopeHandler::MakeSymbol(
2066     Scope &scope, const SourceName &name, Attrs attrs) {
2067   if (Symbol * symbol{FindInScope(scope, name)}) {
2068     symbol->attrs() |= attrs;
2069     return *symbol;
2070   } else {
2071     const auto pair{scope.try_emplace(name, attrs, UnknownDetails{})};
2072     CHECK(pair.second); // name was not found, so must be able to add
2073     return *pair.first->second;
2074   }
2075 }
2076 Symbol &ScopeHandler::MakeSymbol(const SourceName &name, Attrs attrs) {
2077   return MakeSymbol(currScope(), name, attrs);
2078 }
2079 Symbol &ScopeHandler::MakeSymbol(const parser::Name &name, Attrs attrs) {
2080   return Resolve(name, MakeSymbol(name.source, attrs));
2081 }
2082 Symbol &ScopeHandler::CopySymbol(const SourceName &name, const Symbol &symbol) {
2083   CHECK(!FindInScope(currScope(), name));
2084   return MakeSymbol(currScope(), name, symbol.attrs());
2085 }
2086 
2087 // Look for name only in scope, not in enclosing scopes.
2088 Symbol *ScopeHandler::FindInScope(
2089     const Scope &scope, const parser::Name &name) {
2090   return Resolve(name, FindInScope(scope, name.source));
2091 }
2092 Symbol *ScopeHandler::FindInScope(const Scope &scope, const SourceName &name) {
2093   if (auto it{scope.find(name)}; it != scope.end()) {
2094     return &*it->second;
2095   } else {
2096     return nullptr;
2097   }
2098 }
2099 
2100 // Find a component or type parameter by name in a derived type or its parents.
2101 Symbol *ScopeHandler::FindInTypeOrParents(
2102     const Scope &scope, const parser::Name &name) {
2103   return Resolve(name, scope.FindComponent(name.source));
2104 }
2105 Symbol *ScopeHandler::FindInTypeOrParents(const parser::Name &name) {
2106   return FindInTypeOrParents(currScope(), name);
2107 }
2108 
2109 void ScopeHandler::EraseSymbol(const parser::Name &name) {
2110   currScope().erase(name.source);
2111   name.symbol = nullptr;
2112 }
2113 
2114 static bool NeedsType(const Symbol &symbol) {
2115   return !symbol.GetType() &&
2116       std::visit(common::visitors{
2117                      [](const EntityDetails &) { return true; },
2118                      [](const ObjectEntityDetails &) { return true; },
2119                      [](const AssocEntityDetails &) { return true; },
2120                      [&](const ProcEntityDetails &p) {
2121                        return symbol.test(Symbol::Flag::Function) &&
2122                            !symbol.attrs().test(Attr::INTRINSIC) &&
2123                            !p.interface().type() && !p.interface().symbol();
2124                      },
2125                      [](const auto &) { return false; },
2126                  },
2127           symbol.details());
2128 }
2129 void ScopeHandler::ApplyImplicitRules(Symbol &symbol) {
2130   if (NeedsType(symbol)) {
2131     if (const DeclTypeSpec * type{GetImplicitType(symbol)}) {
2132       symbol.set(Symbol::Flag::Implicit);
2133       symbol.SetType(*type);
2134     } else if (symbol.has<ProcEntityDetails>() &&
2135         !symbol.attrs().test(Attr::EXTERNAL) &&
2136         context().intrinsics().IsIntrinsic(symbol.name().ToString())) {
2137       // type will be determined in expression semantics
2138       symbol.attrs().set(Attr::INTRINSIC);
2139     } else if (!context().HasError(symbol)) {
2140       Say(symbol.name(), "No explicit type declared for '%s'"_err_en_US);
2141       context().SetError(symbol);
2142     }
2143   }
2144 }
2145 const DeclTypeSpec *ScopeHandler::GetImplicitType(Symbol &symbol) {
2146   const DeclTypeSpec *type{implicitRules().GetType(symbol.name().begin()[0])};
2147   if (type) {
2148     if (const DerivedTypeSpec * derived{type->AsDerived()}) {
2149       // Resolve any forward-referenced derived type; a quick no-op else.
2150       auto &instantiatable{*const_cast<DerivedTypeSpec *>(derived)};
2151       instantiatable.Instantiate(currScope(), context());
2152     }
2153   }
2154   return type;
2155 }
2156 
2157 // Convert symbol to be a ObjectEntity or return false if it can't be.
2158 bool ScopeHandler::ConvertToObjectEntity(Symbol &symbol) {
2159   if (symbol.has<ObjectEntityDetails>()) {
2160     // nothing to do
2161   } else if (symbol.has<UnknownDetails>()) {
2162     symbol.set_details(ObjectEntityDetails{});
2163   } else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
2164     symbol.set_details(ObjectEntityDetails{std::move(*details)});
2165   } else if (auto *useDetails{symbol.detailsIf<UseDetails>()}) {
2166     return useDetails->symbol().has<ObjectEntityDetails>();
2167   } else {
2168     return false;
2169   }
2170   return true;
2171 }
2172 // Convert symbol to be a ProcEntity or return false if it can't be.
2173 bool ScopeHandler::ConvertToProcEntity(Symbol &symbol) {
2174   if (symbol.has<ProcEntityDetails>()) {
2175     // nothing to do
2176   } else if (symbol.has<UnknownDetails>()) {
2177     symbol.set_details(ProcEntityDetails{});
2178   } else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
2179     symbol.set_details(ProcEntityDetails{std::move(*details)});
2180     if (symbol.GetType() && !symbol.test(Symbol::Flag::Implicit)) {
2181       CHECK(!symbol.test(Symbol::Flag::Subroutine));
2182       symbol.set(Symbol::Flag::Function);
2183     }
2184   } else {
2185     return false;
2186   }
2187   return true;
2188 }
2189 
2190 const DeclTypeSpec &ScopeHandler::MakeNumericType(
2191     TypeCategory category, const std::optional<parser::KindSelector> &kind) {
2192   KindExpr value{GetKindParamExpr(category, kind)};
2193   if (auto known{evaluate::ToInt64(value)}) {
2194     return context().MakeNumericType(category, static_cast<int>(*known));
2195   } else {
2196     return currScope_->MakeNumericType(category, std::move(value));
2197   }
2198 }
2199 
2200 const DeclTypeSpec &ScopeHandler::MakeLogicalType(
2201     const std::optional<parser::KindSelector> &kind) {
2202   KindExpr value{GetKindParamExpr(TypeCategory::Logical, kind)};
2203   if (auto known{evaluate::ToInt64(value)}) {
2204     return context().MakeLogicalType(static_cast<int>(*known));
2205   } else {
2206     return currScope_->MakeLogicalType(std::move(value));
2207   }
2208 }
2209 
2210 void ScopeHandler::MakeExternal(Symbol &symbol) {
2211   if (!symbol.attrs().test(Attr::EXTERNAL)) {
2212     symbol.attrs().set(Attr::EXTERNAL);
2213     if (symbol.attrs().test(Attr::INTRINSIC)) { // C840
2214       Say(symbol.name(),
2215           "Symbol '%s' cannot have both EXTERNAL and INTRINSIC attributes"_err_en_US,
2216           symbol.name());
2217     }
2218   }
2219 }
2220 
2221 // ModuleVisitor implementation
2222 
2223 bool ModuleVisitor::Pre(const parser::Only &x) {
2224   std::visit(common::visitors{
2225                  [&](const Indirection<parser::GenericSpec> &generic) {
2226                    AddUse(GenericSpecInfo{generic.value()});
2227                  },
2228                  [&](const parser::Name &name) {
2229                    Resolve(name, AddUse(name.source, name.source).use);
2230                  },
2231                  [&](const parser::Rename &rename) { Walk(rename); },
2232              },
2233       x.u);
2234   return false;
2235 }
2236 
2237 bool ModuleVisitor::Pre(const parser::Rename::Names &x) {
2238   const auto &localName{std::get<0>(x.t)};
2239   const auto &useName{std::get<1>(x.t)};
2240   SymbolRename rename{AddUse(localName.source, useName.source)};
2241   Resolve(useName, rename.use);
2242   Resolve(localName, rename.local);
2243   return false;
2244 }
2245 bool ModuleVisitor::Pre(const parser::Rename::Operators &x) {
2246   const parser::DefinedOpName &local{std::get<0>(x.t)};
2247   const parser::DefinedOpName &use{std::get<1>(x.t)};
2248   GenericSpecInfo localInfo{local};
2249   GenericSpecInfo useInfo{use};
2250   if (IsIntrinsicOperator(context(), local.v.source)) {
2251     Say(local.v,
2252         "Intrinsic operator '%s' may not be used as a defined operator"_err_en_US);
2253   } else if (IsLogicalConstant(context(), local.v.source)) {
2254     Say(local.v,
2255         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
2256   } else {
2257     SymbolRename rename{AddUse(localInfo.symbolName(), useInfo.symbolName())};
2258     useInfo.Resolve(rename.use);
2259     localInfo.Resolve(rename.local);
2260   }
2261   return false;
2262 }
2263 
2264 // Set useModuleScope_ to the Scope of the module being used.
2265 bool ModuleVisitor::Pre(const parser::UseStmt &x) {
2266   useModuleScope_ = FindModule(x.moduleName);
2267   return useModuleScope_ != nullptr;
2268 }
2269 void ModuleVisitor::Post(const parser::UseStmt &x) {
2270   if (const auto *list{std::get_if<std::list<parser::Rename>>(&x.u)}) {
2271     // Not a use-only: collect the names that were used in renames,
2272     // then add a use for each public name that was not renamed.
2273     std::set<SourceName> useNames;
2274     for (const auto &rename : *list) {
2275       std::visit(common::visitors{
2276                      [&](const parser::Rename::Names &names) {
2277                        useNames.insert(std::get<1>(names.t).source);
2278                      },
2279                      [&](const parser::Rename::Operators &ops) {
2280                        useNames.insert(std::get<1>(ops.t).v.source);
2281                      },
2282                  },
2283           rename.u);
2284     }
2285     for (const auto &[name, symbol] : *useModuleScope_) {
2286       if (symbol->attrs().test(Attr::PUBLIC) &&
2287           !symbol->detailsIf<MiscDetails>()) {
2288         if (useNames.count(name) == 0) {
2289           auto *localSymbol{FindInScope(currScope(), name)};
2290           if (!localSymbol) {
2291             localSymbol = &CopySymbol(name, *symbol);
2292           }
2293           AddUse(x.moduleName.source, *localSymbol, *symbol);
2294         }
2295       }
2296     }
2297   }
2298   useModuleScope_ = nullptr;
2299 }
2300 
2301 ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
2302     const SourceName &localName, const SourceName &useName) {
2303   return AddUse(localName, useName, FindInScope(*useModuleScope_, useName));
2304 }
2305 
2306 ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
2307     const SourceName &localName, const SourceName &useName, Symbol *useSymbol) {
2308   if (!useModuleScope_) {
2309     return {}; // error occurred finding module
2310   }
2311   if (!useSymbol) {
2312     Say(useName,
2313         IsDefinedOperator(useName)
2314             ? "Operator '%s' not found in module '%s'"_err_en_US
2315             : "'%s' not found in module '%s'"_err_en_US,
2316         useName, useModuleScope_->GetName().value());
2317     return {};
2318   }
2319   if (useSymbol->attrs().test(Attr::PRIVATE)) {
2320     Say(useName,
2321         IsDefinedOperator(useName)
2322             ? "Operator '%s' is PRIVATE in '%s'"_err_en_US
2323             : "'%s' is PRIVATE in '%s'"_err_en_US,
2324         useName, useModuleScope_->GetName().value());
2325     return {};
2326   }
2327   auto &localSymbol{MakeSymbol(localName)};
2328   AddUse(useName, localSymbol, *useSymbol);
2329   return {&localSymbol, useSymbol};
2330 }
2331 
2332 // symbol must be either a Use or a Generic formed by merging two uses.
2333 // Convert it to a UseError with this additional location.
2334 static void ConvertToUseError(
2335     Symbol &symbol, const SourceName &location, const Scope &module) {
2336   const auto *useDetails{symbol.detailsIf<UseDetails>()};
2337   if (!useDetails) {
2338     auto &genericDetails{symbol.get<GenericDetails>()};
2339     useDetails = &genericDetails.useDetails().value();
2340   }
2341   symbol.set_details(
2342       UseErrorDetails{*useDetails}.add_occurrence(location, module));
2343 }
2344 
2345 void ModuleVisitor::AddUse(
2346     const SourceName &location, Symbol &localSymbol, const Symbol &useSymbol) {
2347   localSymbol.attrs() = useSymbol.attrs() & ~Attrs{Attr::PUBLIC, Attr::PRIVATE};
2348   localSymbol.flags() = useSymbol.flags();
2349   if (auto *useDetails{localSymbol.detailsIf<UseDetails>()}) {
2350     const Symbol &ultimate{localSymbol.GetUltimate()};
2351     if (ultimate == useSymbol.GetUltimate()) {
2352       // use-associating the same symbol again -- ok
2353     } else if (ultimate.has<GenericDetails>() &&
2354         useSymbol.has<GenericDetails>()) {
2355       // use-associating generics with the same names: merge them into a
2356       // new generic in this scope
2357       auto generic1{ultimate.get<GenericDetails>()};
2358       generic1.set_useDetails(*useDetails);
2359       // useSymbol has specific g and so does generic1
2360       auto &generic2{useSymbol.get<GenericDetails>()};
2361       if (generic1.specific() && generic2.specific() &&
2362           generic1.specific() != generic2.specific()) {
2363         Say(location,
2364             "Generic interface '%s' has ambiguous specific procedures"
2365             " from modules '%s' and '%s'"_err_en_US,
2366             localSymbol.name(), useDetails->module().name(),
2367             useSymbol.owner().GetName().value());
2368       } else if (generic1.derivedType() && generic2.derivedType() &&
2369           generic1.derivedType() != generic2.derivedType()) {
2370         Say(location,
2371             "Generic interface '%s' has ambiguous derived types"
2372             " from modules '%s' and '%s'"_err_en_US,
2373             localSymbol.name(), useDetails->module().name(),
2374             useSymbol.owner().GetName().value());
2375       } else {
2376         generic1.CopyFrom(generic2);
2377       }
2378       EraseSymbol(localSymbol);
2379       MakeSymbol(localSymbol.name(), ultimate.attrs(), std::move(generic1));
2380     } else {
2381       ConvertToUseError(localSymbol, location, *useModuleScope_);
2382     }
2383   } else {
2384     auto *genericDetails{localSymbol.detailsIf<GenericDetails>()};
2385     if (genericDetails && genericDetails->useDetails()) {
2386       // localSymbol came from merging two use-associated generics
2387       if (auto *useDetails{useSymbol.detailsIf<GenericDetails>()}) {
2388         genericDetails->CopyFrom(*useDetails);
2389       } else {
2390         ConvertToUseError(localSymbol, location, *useModuleScope_);
2391       }
2392     } else if (auto *details{localSymbol.detailsIf<UseErrorDetails>()}) {
2393       details->add_occurrence(location, *useModuleScope_);
2394     } else if (!localSymbol.has<UnknownDetails>()) {
2395       Say(location,
2396           "Cannot use-associate '%s'; it is already declared in this scope"_err_en_US,
2397           localSymbol.name())
2398           .Attach(localSymbol.name(), "Previous declaration of '%s'"_en_US,
2399               localSymbol.name());
2400     } else {
2401       localSymbol.set_details(UseDetails{location, useSymbol});
2402     }
2403   }
2404 }
2405 
2406 void ModuleVisitor::AddUse(const GenericSpecInfo &info) {
2407   if (useModuleScope_) {
2408     const auto &name{info.symbolName()};
2409     auto rename{
2410         AddUse(name, name, info.FindInScope(context(), *useModuleScope_))};
2411     info.Resolve(rename.use);
2412   }
2413 }
2414 
2415 bool ModuleVisitor::BeginSubmodule(
2416     const parser::Name &name, const parser::ParentIdentifier &parentId) {
2417   auto &ancestorName{std::get<parser::Name>(parentId.t)};
2418   auto &parentName{std::get<std::optional<parser::Name>>(parentId.t)};
2419   Scope *ancestor{FindModule(ancestorName)};
2420   if (!ancestor) {
2421     return false;
2422   }
2423   Scope *parentScope{parentName ? FindModule(*parentName, ancestor) : ancestor};
2424   if (!parentScope) {
2425     return false;
2426   }
2427   PushScope(*parentScope); // submodule is hosted in parent
2428   BeginModule(name, true);
2429   if (!ancestor->AddSubmodule(name.source, currScope())) {
2430     Say(name, "Module '%s' already has a submodule named '%s'"_err_en_US,
2431         ancestorName.source, name.source);
2432   }
2433   return true;
2434 }
2435 
2436 void ModuleVisitor::BeginModule(const parser::Name &name, bool isSubmodule) {
2437   auto &symbol{MakeSymbol(name, ModuleDetails{isSubmodule})};
2438   auto &details{symbol.get<ModuleDetails>()};
2439   PushScope(Scope::Kind::Module, &symbol);
2440   details.set_scope(&currScope());
2441   defaultAccess_ = Attr::PUBLIC;
2442   prevAccessStmt_ = std::nullopt;
2443 }
2444 
2445 // Find a module or submodule by name and return its scope.
2446 // If ancestor is present, look for a submodule of that ancestor module.
2447 // May have to read a .mod file to find it.
2448 // If an error occurs, report it and return nullptr.
2449 Scope *ModuleVisitor::FindModule(const parser::Name &name, Scope *ancestor) {
2450   ModFileReader reader{context()};
2451   Scope *scope{reader.Read(name.source, ancestor)};
2452   if (!scope) {
2453     return nullptr;
2454   }
2455   if (scope->kind() != Scope::Kind::Module) {
2456     Say(name, "'%s' is not a module"_err_en_US);
2457     return nullptr;
2458   }
2459   if (DoesScopeContain(scope, currScope())) { // 14.2.2(1)
2460     Say(name, "Module '%s' cannot USE itself"_err_en_US);
2461   }
2462   Resolve(name, scope->symbol());
2463   return scope;
2464 }
2465 
2466 void ModuleVisitor::ApplyDefaultAccess() {
2467   for (auto &pair : currScope()) {
2468     Symbol &symbol = *pair.second;
2469     if (!symbol.attrs().HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
2470       symbol.attrs().set(defaultAccess_);
2471     }
2472   }
2473 }
2474 
2475 // InterfaceVistor implementation
2476 
2477 bool InterfaceVisitor::Pre(const parser::InterfaceStmt &x) {
2478   bool isAbstract{std::holds_alternative<parser::Abstract>(x.u)};
2479   genericInfo_.emplace(/*isInterface*/ true, isAbstract);
2480   return BeginAttrs();
2481 }
2482 
2483 void InterfaceVisitor::Post(const parser::InterfaceStmt &) { EndAttrs(); }
2484 
2485 void InterfaceVisitor::Post(const parser::EndInterfaceStmt &) {
2486   genericInfo_.pop();
2487 }
2488 
2489 // Create a symbol in genericSymbol_ for this GenericSpec.
2490 bool InterfaceVisitor::Pre(const parser::GenericSpec &x) {
2491   if (auto *symbol{GenericSpecInfo{x}.FindInScope(context(), currScope())}) {
2492     SetGenericSymbol(*symbol);
2493   }
2494   return false;
2495 }
2496 
2497 bool InterfaceVisitor::Pre(const parser::ProcedureStmt &x) {
2498   if (!isGeneric()) {
2499     Say("A PROCEDURE statement is only allowed in a generic interface block"_err_en_US);
2500     return false;
2501   }
2502   auto kind{std::get<parser::ProcedureStmt::Kind>(x.t)};
2503   const auto &names{std::get<std::list<parser::Name>>(x.t)};
2504   AddSpecificProcs(names, kind);
2505   return false;
2506 }
2507 
2508 bool InterfaceVisitor::Pre(const parser::GenericStmt &) {
2509   genericInfo_.emplace(/*isInterface*/ false);
2510   return true;
2511 }
2512 void InterfaceVisitor::Post(const parser::GenericStmt &x) {
2513   if (auto &accessSpec{std::get<std::optional<parser::AccessSpec>>(x.t)}) {
2514     GetGenericInfo().symbol->attrs().set(AccessSpecToAttr(*accessSpec));
2515   }
2516   const auto &names{std::get<std::list<parser::Name>>(x.t)};
2517   AddSpecificProcs(names, ProcedureKind::Procedure);
2518   genericInfo_.pop();
2519 }
2520 
2521 bool InterfaceVisitor::inInterfaceBlock() const {
2522   return !genericInfo_.empty() && GetGenericInfo().isInterface;
2523 }
2524 bool InterfaceVisitor::isGeneric() const {
2525   return !genericInfo_.empty() && GetGenericInfo().symbol;
2526 }
2527 bool InterfaceVisitor::isAbstract() const {
2528   return !genericInfo_.empty() && GetGenericInfo().isAbstract;
2529 }
2530 GenericDetails &InterfaceVisitor::GetGenericDetails() {
2531   return GetGenericInfo().symbol->get<GenericDetails>();
2532 }
2533 
2534 void InterfaceVisitor::AddSpecificProcs(
2535     const std::list<parser::Name> &names, ProcedureKind kind) {
2536   for (const auto &name : names) {
2537     specificProcs_.emplace(
2538         GetGenericInfo().symbol, std::make_pair(&name, kind));
2539   }
2540 }
2541 
2542 // By now we should have seen all specific procedures referenced by name in
2543 // this generic interface. Resolve those names to symbols.
2544 void InterfaceVisitor::ResolveSpecificsInGeneric(Symbol &generic) {
2545   auto &details{generic.get<GenericDetails>()};
2546   std::set<SourceName> namesSeen; // to check for duplicate names
2547   for (const Symbol &symbol : details.specificProcs()) {
2548     namesSeen.insert(symbol.name());
2549   }
2550   auto range{specificProcs_.equal_range(&generic)};
2551   for (auto it{range.first}; it != range.second; ++it) {
2552     auto *name{it->second.first};
2553     auto kind{it->second.second};
2554     const auto *symbol{FindSymbol(*name)};
2555     if (!symbol) {
2556       Say(*name, "Procedure '%s' not found"_err_en_US);
2557       continue;
2558     }
2559     symbol = &symbol->GetUltimate();
2560     if (symbol == &generic) {
2561       if (auto *specific{generic.get<GenericDetails>().specific()}) {
2562         symbol = specific;
2563       }
2564     }
2565     if (!symbol->has<SubprogramDetails>() &&
2566         !symbol->has<SubprogramNameDetails>()) {
2567       Say(*name, "'%s' is not a subprogram"_err_en_US);
2568       continue;
2569     }
2570     if (kind == ProcedureKind::ModuleProcedure) {
2571       if (const auto *nd{symbol->detailsIf<SubprogramNameDetails>()}) {
2572         if (nd->kind() != SubprogramKind::Module) {
2573           Say(*name, "'%s' is not a module procedure"_err_en_US);
2574         }
2575       } else {
2576         // USE-associated procedure
2577         const auto *sd{symbol->detailsIf<SubprogramDetails>()};
2578         CHECK(sd);
2579         if (symbol->owner().kind() != Scope::Kind::Module ||
2580             sd->isInterface()) {
2581           Say(*name, "'%s' is not a module procedure"_err_en_US);
2582         }
2583       }
2584     }
2585     if (!namesSeen.insert(name->source).second) {
2586       Say(*name,
2587           details.kind().IsDefinedOperator()
2588               ? "Procedure '%s' is already specified in generic operator '%s'"_err_en_US
2589               : "Procedure '%s' is already specified in generic '%s'"_err_en_US,
2590           name->source, generic.name());
2591       continue;
2592     }
2593     details.AddSpecificProc(*symbol, name->source);
2594   }
2595   specificProcs_.erase(range.first, range.second);
2596 }
2597 
2598 // Check that the specific procedures are all functions or all subroutines.
2599 // If there is a derived type with the same name they must be functions.
2600 // Set the corresponding flag on generic.
2601 void InterfaceVisitor::CheckGenericProcedures(Symbol &generic) {
2602   ResolveSpecificsInGeneric(generic);
2603   auto &details{generic.get<GenericDetails>()};
2604   if (auto *proc{details.CheckSpecific()}) {
2605     auto msg{
2606         "'%s' may not be the name of both a generic interface and a"
2607         " procedure unless it is a specific procedure of the generic"_err_en_US};
2608     if (proc->name().begin() > generic.name().begin()) {
2609       Say(proc->name(), std::move(msg));
2610     } else {
2611       Say(generic.name(), std::move(msg));
2612     }
2613   }
2614   auto &specifics{details.specificProcs()};
2615   if (specifics.empty()) {
2616     if (details.derivedType()) {
2617       generic.set(Symbol::Flag::Function);
2618     }
2619     return;
2620   }
2621   const Symbol &firstSpecific{specifics.front()};
2622   bool isFunction{firstSpecific.test(Symbol::Flag::Function)};
2623   for (const Symbol &specific : specifics) {
2624     if (isFunction != specific.test(Symbol::Flag::Function)) { // C1514
2625       auto &msg{Say(generic.name(),
2626           "Generic interface '%s' has both a function and a subroutine"_err_en_US)};
2627       if (isFunction) {
2628         msg.Attach(firstSpecific.name(), "Function declaration"_en_US);
2629         msg.Attach(specific.name(), "Subroutine declaration"_en_US);
2630       } else {
2631         msg.Attach(firstSpecific.name(), "Subroutine declaration"_en_US);
2632         msg.Attach(specific.name(), "Function declaration"_en_US);
2633       }
2634     }
2635   }
2636   if (!isFunction && details.derivedType()) {
2637     SayDerivedType(generic.name(),
2638         "Generic interface '%s' may only contain functions due to derived type"
2639         " with same name"_err_en_US,
2640         *details.derivedType()->scope());
2641   }
2642   generic.set(isFunction ? Symbol::Flag::Function : Symbol::Flag::Subroutine);
2643 }
2644 
2645 // SubprogramVisitor implementation
2646 
2647 // Return false if it is actually an assignment statement.
2648 bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
2649   const auto &name{std::get<parser::Name>(x.t)};
2650   const DeclTypeSpec *resultType{nullptr};
2651   // Look up name: provides return type or tells us if it's an array
2652   if (auto *symbol{FindSymbol(name)}) {
2653     auto *details{symbol->detailsIf<EntityDetails>()};
2654     if (!details) {
2655       badStmtFuncFound_ = true;
2656       return false;
2657     }
2658     // TODO: check that attrs are compatible with stmt func
2659     resultType = details->type();
2660     symbol->details() = UnknownDetails{}; // will be replaced below
2661   }
2662   if (badStmtFuncFound_) {
2663     Say(name, "'%s' has not been declared as an array"_err_en_US);
2664     return true;
2665   }
2666   auto &symbol{PushSubprogramScope(name, Symbol::Flag::Function)};
2667   EraseSymbol(symbol); // removes symbol added by PushSubprogramScope
2668   auto &details{symbol.get<SubprogramDetails>()};
2669   for (const auto &dummyName : std::get<std::list<parser::Name>>(x.t)) {
2670     ObjectEntityDetails dummyDetails{true};
2671     if (auto *dummySymbol{FindInScope(currScope().parent(), dummyName)}) {
2672       if (auto *d{dummySymbol->detailsIf<EntityDetails>()}) {
2673         if (d->type()) {
2674           dummyDetails.set_type(*d->type());
2675         }
2676       }
2677     }
2678     Symbol &dummy{MakeSymbol(dummyName, std::move(dummyDetails))};
2679     ApplyImplicitRules(dummy);
2680     details.add_dummyArg(dummy);
2681   }
2682   ObjectEntityDetails resultDetails;
2683   if (resultType) {
2684     resultDetails.set_type(*resultType);
2685   }
2686   resultDetails.set_funcResult(true);
2687   Symbol &result{MakeSymbol(name, std::move(resultDetails))};
2688   ApplyImplicitRules(result);
2689   details.set_result(result);
2690   const auto &parsedExpr{std::get<parser::Scalar<parser::Expr>>(x.t)};
2691   Walk(parsedExpr);
2692   if (auto expr{AnalyzeExpr(context(), parsedExpr)}) {
2693     details.set_stmtFunction(std::move(*expr));
2694   } else {
2695     context().SetError(symbol);
2696   }
2697   PopScope();
2698   return true;
2699 }
2700 
2701 bool SubprogramVisitor::Pre(const parser::Suffix &suffix) {
2702   if (suffix.resultName) {
2703     funcInfo_.resultName = &suffix.resultName.value();
2704   }
2705   return true;
2706 }
2707 
2708 bool SubprogramVisitor::Pre(const parser::PrefixSpec &x) {
2709   // Save this to process after UseStmt and ImplicitPart
2710   if (const auto *parsedType{std::get_if<parser::DeclarationTypeSpec>(&x.u)}) {
2711     if (funcInfo_.parsedType) { // C1543
2712       Say(currStmtSource().value(),
2713           "FUNCTION prefix cannot specify the type more than once"_err_en_US);
2714       return false;
2715     } else {
2716       funcInfo_.parsedType = parsedType;
2717       funcInfo_.source = currStmtSource();
2718       return false;
2719     }
2720   } else {
2721     return true;
2722   }
2723 }
2724 
2725 void SubprogramVisitor::Post(const parser::ImplicitPart &) {
2726   // If the function has a type in the prefix, process it now
2727   if (funcInfo_.parsedType) {
2728     messageHandler().set_currStmtSource(funcInfo_.source);
2729     if (const auto *type{ProcessTypeSpec(*funcInfo_.parsedType, true)}) {
2730       funcInfo_.resultSymbol->SetType(*type);
2731     }
2732   }
2733   funcInfo_ = {};
2734 }
2735 
2736 bool SubprogramVisitor::Pre(const parser::InterfaceBody::Subroutine &x) {
2737   const auto &name{std::get<parser::Name>(
2738       std::get<parser::Statement<parser::SubroutineStmt>>(x.t).statement.t)};
2739   return BeginSubprogram(name, Symbol::Flag::Subroutine);
2740 }
2741 void SubprogramVisitor::Post(const parser::InterfaceBody::Subroutine &) {
2742   EndSubprogram();
2743 }
2744 bool SubprogramVisitor::Pre(const parser::InterfaceBody::Function &x) {
2745   const auto &name{std::get<parser::Name>(
2746       std::get<parser::Statement<parser::FunctionStmt>>(x.t).statement.t)};
2747   return BeginSubprogram(name, Symbol::Flag::Function);
2748 }
2749 void SubprogramVisitor::Post(const parser::InterfaceBody::Function &) {
2750   EndSubprogram();
2751 }
2752 
2753 bool SubprogramVisitor::Pre(const parser::SubroutineStmt &) {
2754   return BeginAttrs();
2755 }
2756 bool SubprogramVisitor::Pre(const parser::FunctionStmt &) {
2757   return BeginAttrs();
2758 }
2759 bool SubprogramVisitor::Pre(const parser::EntryStmt &) { return BeginAttrs(); }
2760 
2761 void SubprogramVisitor::Post(const parser::SubroutineStmt &stmt) {
2762   const auto &name{std::get<parser::Name>(stmt.t)};
2763   auto &details{PostSubprogramStmt(name)};
2764   for (const auto &dummyArg : std::get<std::list<parser::DummyArg>>(stmt.t)) {
2765     if (const auto *dummyName{std::get_if<parser::Name>(&dummyArg.u)}) {
2766       Symbol &dummy{MakeSymbol(*dummyName, EntityDetails(true))};
2767       details.add_dummyArg(dummy);
2768     } else {
2769       details.add_alternateReturn();
2770     }
2771   }
2772 }
2773 
2774 void SubprogramVisitor::Post(const parser::FunctionStmt &stmt) {
2775   const auto &name{std::get<parser::Name>(stmt.t)};
2776   auto &details{PostSubprogramStmt(name)};
2777   for (const auto &dummyName : std::get<std::list<parser::Name>>(stmt.t)) {
2778     Symbol &dummy{MakeSymbol(dummyName, EntityDetails(true))};
2779     details.add_dummyArg(dummy);
2780   }
2781   const parser::Name *funcResultName;
2782   if (funcInfo_.resultName && funcInfo_.resultName->source != name.source) {
2783     // Note that RESULT is ignored if it has the same name as the function.
2784     funcResultName = funcInfo_.resultName;
2785   } else {
2786     EraseSymbol(name); // was added by PushSubprogramScope
2787     funcResultName = &name;
2788   }
2789   // add function result to function scope
2790   EntityDetails funcResultDetails;
2791   funcResultDetails.set_funcResult(true);
2792   funcInfo_.resultSymbol =
2793       &MakeSymbol(*funcResultName, std::move(funcResultDetails));
2794   details.set_result(*funcInfo_.resultSymbol);
2795 
2796   // C1560.
2797   if (funcInfo_.resultName && funcInfo_.resultName->source == name.source) {
2798     Say(funcInfo_.resultName->source,
2799         "The function name should not appear in RESULT, references to '%s' "
2800         "inside"
2801         " the function will be considered as references to the result only"_en_US,
2802         name.source);
2803     // RESULT name was ignored above, the only side effect from doing so will be
2804     // the inability to make recursive calls. The related parser::Name is still
2805     // resolved to the created function result symbol because every parser::Name
2806     // should be resolved to avoid internal errors.
2807     Resolve(*funcInfo_.resultName, funcInfo_.resultSymbol);
2808   }
2809   name.symbol = currScope().symbol(); // must not be function result symbol
2810   // Clear the RESULT() name now in case an ENTRY statement in the implicit-part
2811   // has a RESULT() suffix.
2812   funcInfo_.resultName = nullptr;
2813 }
2814 
2815 SubprogramDetails &SubprogramVisitor::PostSubprogramStmt(
2816     const parser::Name &name) {
2817   Symbol &symbol{*currScope().symbol()};
2818   CHECK(name.source == symbol.name());
2819   SetBindNameOn(symbol);
2820   symbol.attrs() |= EndAttrs();
2821   if (symbol.attrs().test(Attr::MODULE)) {
2822     symbol.attrs().set(Attr::EXTERNAL, false);
2823   }
2824   return symbol.get<SubprogramDetails>();
2825 }
2826 
2827 void SubprogramVisitor::Post(const parser::EntryStmt &stmt) {
2828   auto attrs{EndAttrs()}; // needs to be called even if early return
2829   Scope &inclusiveScope{InclusiveScope()};
2830   const Symbol *subprogram{inclusiveScope.symbol()};
2831   if (!subprogram) {
2832     CHECK(context().AnyFatalError());
2833     return;
2834   }
2835   const auto &name{std::get<parser::Name>(stmt.t)};
2836   const auto *parentDetails{subprogram->detailsIf<SubprogramDetails>()};
2837   bool inFunction{parentDetails && parentDetails->isFunction()};
2838   const parser::Name *resultName{funcInfo_.resultName};
2839   if (resultName) { // RESULT(result) is present
2840     funcInfo_.resultName = nullptr;
2841     if (!inFunction) {
2842       Say2(resultName->source,
2843           "RESULT(%s) may appear only in a function"_err_en_US,
2844           subprogram->name(), "Containing subprogram"_en_US);
2845     } else if (resultName->source == subprogram->name()) { // C1574
2846       Say2(resultName->source,
2847           "RESULT(%s) may not have the same name as the function"_err_en_US,
2848           subprogram->name(), "Containing function"_en_US);
2849     } else if (const Symbol *
2850         symbol{FindSymbol(inclusiveScope.parent(), *resultName)}) { // C1574
2851       if (const auto *details{symbol->detailsIf<SubprogramDetails>()}) {
2852         if (details->entryScope() == &inclusiveScope) {
2853           Say2(resultName->source,
2854               "RESULT(%s) may not have the same name as an ENTRY in the function"_err_en_US,
2855               symbol->name(), "Conflicting ENTRY"_en_US);
2856         }
2857       }
2858     }
2859     if (Symbol * symbol{FindSymbol(name)}) { // C1570
2860       // When RESULT() appears, ENTRY name can't have been already declared
2861       if (inclusiveScope.Contains(symbol->owner())) {
2862         Say2(name,
2863             "ENTRY name '%s' may not be declared when RESULT() is present"_err_en_US,
2864             *symbol, "Previous declaration of '%s'"_en_US);
2865       }
2866     }
2867     if (resultName->source == name.source) {
2868       // ignore RESULT() hereafter when it's the same name as the ENTRY
2869       resultName = nullptr;
2870     }
2871   }
2872   SubprogramDetails entryDetails;
2873   entryDetails.set_entryScope(inclusiveScope);
2874   if (inFunction) {
2875     // Create the entity to hold the function result, if necessary.
2876     Symbol *resultSymbol{nullptr};
2877     auto &effectiveResultName{*(resultName ? resultName : &name)};
2878     resultSymbol = FindInScope(currScope(), effectiveResultName);
2879     if (resultSymbol) { // C1574
2880       std::visit(
2881           common::visitors{[](EntityDetails &x) { x.set_funcResult(true); },
2882               [](ObjectEntityDetails &x) { x.set_funcResult(true); },
2883               [](ProcEntityDetails &x) { x.set_funcResult(true); },
2884               [&](const auto &) {
2885                 Say2(effectiveResultName.source,
2886                     "'%s' was previously declared as an item that may not be used as a function result"_err_en_US,
2887                     resultSymbol->name(), "Previous declaration of '%s'"_en_US);
2888               }},
2889           resultSymbol->details());
2890     } else if (inExecutionPart_) {
2891       ObjectEntityDetails entity;
2892       entity.set_funcResult(true);
2893       resultSymbol = &MakeSymbol(effectiveResultName, std::move(entity));
2894       ApplyImplicitRules(*resultSymbol);
2895     } else {
2896       EntityDetails entity;
2897       entity.set_funcResult(true);
2898       resultSymbol = &MakeSymbol(effectiveResultName, std::move(entity));
2899     }
2900     if (!resultName) {
2901       name.symbol = nullptr; // symbol will be used for entry point below
2902     }
2903     entryDetails.set_result(*resultSymbol);
2904   }
2905 
2906   for (const auto &dummyArg : std::get<std::list<parser::DummyArg>>(stmt.t)) {
2907     if (const auto *dummyName{std::get_if<parser::Name>(&dummyArg.u)}) {
2908       Symbol *dummy{FindSymbol(*dummyName)};
2909       if (dummy) {
2910         std::visit(
2911             common::visitors{[](EntityDetails &x) { x.set_isDummy(); },
2912                 [](ObjectEntityDetails &x) { x.set_isDummy(); },
2913                 [](ProcEntityDetails &x) { x.set_isDummy(); },
2914                 [&](const auto &) {
2915                   Say2(dummyName->source,
2916                       "ENTRY dummy argument '%s' is previously declared as an item that may not be used as a dummy argument"_err_en_US,
2917                       dummy->name(), "Previous declaration of '%s'"_en_US);
2918                 }},
2919             dummy->details());
2920       } else {
2921         dummy = &MakeSymbol(*dummyName, EntityDetails(true));
2922       }
2923       entryDetails.add_dummyArg(*dummy);
2924     } else {
2925       if (inFunction) { // C1573
2926         Say(name,
2927             "ENTRY in a function may not have an alternate return dummy argument"_err_en_US);
2928         break;
2929       }
2930       entryDetails.add_alternateReturn();
2931     }
2932   }
2933 
2934   Symbol::Flag subpFlag{
2935       inFunction ? Symbol::Flag::Function : Symbol::Flag::Subroutine};
2936   CheckExtantExternal(name, subpFlag);
2937   Scope &outer{inclusiveScope.parent()}; // global or module scope
2938   if (Symbol * extant{FindSymbol(outer, name)}) {
2939     if (extant->has<ProcEntityDetails>()) {
2940       if (!extant->test(subpFlag)) {
2941         Say2(name,
2942             subpFlag == Symbol::Flag::Function
2943                 ? "'%s' was previously called as a subroutine"_err_en_US
2944                 : "'%s' was previously called as a function"_err_en_US,
2945             *extant, "Previous call of '%s'"_en_US);
2946       }
2947       if (extant->attrs().test(Attr::PRIVATE)) {
2948         attrs.set(Attr::PRIVATE);
2949       }
2950       outer.erase(extant->name());
2951     } else {
2952       if (outer.IsGlobal()) {
2953         Say2(name, "'%s' is already defined as a global identifier"_err_en_US,
2954             *extant, "Previous definition of '%s'"_en_US);
2955       } else {
2956         SayAlreadyDeclared(name, *extant);
2957       }
2958       return;
2959     }
2960   }
2961   if (outer.IsModule() && !attrs.test(Attr::PRIVATE)) {
2962     attrs.set(Attr::PUBLIC);
2963   }
2964   Symbol &entrySymbol{MakeSymbol(outer, name.source, attrs)};
2965   entrySymbol.set_details(std::move(entryDetails));
2966   if (outer.IsGlobal()) {
2967     MakeExternal(entrySymbol);
2968   }
2969   SetBindNameOn(entrySymbol);
2970   entrySymbol.set(subpFlag);
2971   Resolve(name, entrySymbol);
2972 }
2973 
2974 // A subprogram declared with MODULE PROCEDURE
2975 bool SubprogramVisitor::BeginMpSubprogram(const parser::Name &name) {
2976   auto *symbol{FindSymbol(name)};
2977   if (symbol && symbol->has<SubprogramNameDetails>()) {
2978     symbol = FindSymbol(currScope().parent(), name);
2979   }
2980   if (!IsSeparateModuleProcedureInterface(symbol)) {
2981     Say(name, "'%s' was not declared a separate module procedure"_err_en_US);
2982     return false;
2983   }
2984   if (symbol->owner() == currScope()) {
2985     PushScope(Scope::Kind::Subprogram, symbol);
2986   } else {
2987     Symbol &newSymbol{MakeSymbol(name, SubprogramDetails{})};
2988     PushScope(Scope::Kind::Subprogram, &newSymbol);
2989     const auto &details{symbol->get<SubprogramDetails>()};
2990     auto &newDetails{newSymbol.get<SubprogramDetails>()};
2991     for (const Symbol *dummyArg : details.dummyArgs()) {
2992       if (!dummyArg) {
2993         newDetails.add_alternateReturn();
2994       } else if (Symbol * copy{currScope().CopySymbol(*dummyArg)}) {
2995         newDetails.add_dummyArg(*copy);
2996       }
2997     }
2998     if (details.isFunction()) {
2999       currScope().erase(symbol->name());
3000       newDetails.set_result(*currScope().CopySymbol(details.result()));
3001     }
3002   }
3003   return true;
3004 }
3005 
3006 // A subprogram declared with SUBROUTINE or FUNCTION
3007 bool SubprogramVisitor::BeginSubprogram(
3008     const parser::Name &name, Symbol::Flag subpFlag, bool hasModulePrefix) {
3009   if (hasModulePrefix && !inInterfaceBlock() &&
3010       !IsSeparateModuleProcedureInterface(
3011           FindSymbol(currScope().parent(), name))) {
3012     Say(name, "'%s' was not declared a separate module procedure"_err_en_US);
3013     return false;
3014   }
3015   PushSubprogramScope(name, subpFlag);
3016   return true;
3017 }
3018 
3019 void SubprogramVisitor::EndSubprogram() { PopScope(); }
3020 
3021 void SubprogramVisitor::CheckExtantExternal(
3022     const parser::Name &name, Symbol::Flag subpFlag) {
3023   if (auto *prev{FindSymbol(name)}) {
3024     if (prev->attrs().test(Attr::EXTERNAL) && prev->has<ProcEntityDetails>()) {
3025       // this subprogram was previously called, now being declared
3026       if (!prev->test(subpFlag)) {
3027         Say2(name,
3028             subpFlag == Symbol::Flag::Function
3029                 ? "'%s' was previously called as a subroutine"_err_en_US
3030                 : "'%s' was previously called as a function"_err_en_US,
3031             *prev, "Previous call of '%s'"_en_US);
3032       }
3033       EraseSymbol(name);
3034     }
3035   }
3036 }
3037 
3038 Symbol &SubprogramVisitor::PushSubprogramScope(
3039     const parser::Name &name, Symbol::Flag subpFlag) {
3040   auto *symbol{GetSpecificFromGeneric(name)};
3041   if (!symbol) {
3042     CheckExtantExternal(name, subpFlag);
3043     symbol = &MakeSymbol(name, SubprogramDetails{});
3044   }
3045   symbol->set(subpFlag);
3046   PushScope(Scope::Kind::Subprogram, symbol);
3047   auto &details{symbol->get<SubprogramDetails>()};
3048   if (inInterfaceBlock()) {
3049     details.set_isInterface();
3050     if (!isAbstract()) {
3051       MakeExternal(*symbol);
3052     }
3053     if (isGeneric()) {
3054       GetGenericDetails().AddSpecificProc(*symbol, name.source);
3055     }
3056     implicitRules().set_inheritFromParent(false);
3057   }
3058   FindSymbol(name)->set(subpFlag); // PushScope() created symbol
3059   return *symbol;
3060 }
3061 
3062 void SubprogramVisitor::PushBlockDataScope(const parser::Name &name) {
3063   if (auto *prev{FindSymbol(name)}) {
3064     if (prev->attrs().test(Attr::EXTERNAL) && prev->has<ProcEntityDetails>()) {
3065       if (prev->test(Symbol::Flag::Subroutine) ||
3066           prev->test(Symbol::Flag::Function)) {
3067         Say2(name, "BLOCK DATA '%s' has been called"_err_en_US, *prev,
3068             "Previous call of '%s'"_en_US);
3069       }
3070       EraseSymbol(name);
3071     }
3072   }
3073   if (name.source.empty()) {
3074     // Don't let unnamed BLOCK DATA conflict with unnamed PROGRAM
3075     PushScope(Scope::Kind::BlockData, nullptr);
3076   } else {
3077     PushScope(Scope::Kind::BlockData, &MakeSymbol(name, SubprogramDetails{}));
3078   }
3079 }
3080 
3081 // If name is a generic, return specific subprogram with the same name.
3082 Symbol *SubprogramVisitor::GetSpecificFromGeneric(const parser::Name &name) {
3083   if (auto *symbol{FindSymbol(name)}) {
3084     if (auto *details{symbol->detailsIf<GenericDetails>()}) {
3085       // found generic, want subprogram
3086       auto *specific{details->specific()};
3087       if (!specific) {
3088         specific =
3089             &currScope().MakeSymbol(name.source, Attrs{}, SubprogramDetails{});
3090         details->set_specific(Resolve(name, *specific));
3091       } else if (isGeneric()) {
3092         SayAlreadyDeclared(name, *specific);
3093       } else if (!specific->has<SubprogramDetails>()) {
3094         specific->set_details(SubprogramDetails{});
3095       }
3096       return specific;
3097     }
3098   }
3099   return nullptr;
3100 }
3101 
3102 // DeclarationVisitor implementation
3103 
3104 bool DeclarationVisitor::BeginDecl() {
3105   BeginDeclTypeSpec();
3106   BeginArraySpec();
3107   return BeginAttrs();
3108 }
3109 void DeclarationVisitor::EndDecl() {
3110   EndDeclTypeSpec();
3111   EndArraySpec();
3112   EndAttrs();
3113 }
3114 
3115 bool DeclarationVisitor::CheckUseError(const parser::Name &name) {
3116   const auto *details{name.symbol->detailsIf<UseErrorDetails>()};
3117   if (!details) {
3118     return false;
3119   }
3120   Message &msg{Say(name, "Reference to '%s' is ambiguous"_err_en_US)};
3121   for (const auto &[location, module] : details->occurrences()) {
3122     msg.Attach(location, "'%s' was use-associated from module '%s'"_en_US,
3123         name.source, module->GetName().value());
3124   }
3125   return true;
3126 }
3127 
3128 // Report error if accessibility of symbol doesn't match isPrivate.
3129 void DeclarationVisitor::CheckAccessibility(
3130     const SourceName &name, bool isPrivate, Symbol &symbol) {
3131   if (symbol.attrs().test(Attr::PRIVATE) != isPrivate) {
3132     Say2(name,
3133         "'%s' does not have the same accessibility as its previous declaration"_err_en_US,
3134         symbol, "Previous declaration of '%s'"_en_US);
3135   }
3136 }
3137 
3138 void DeclarationVisitor::Post(const parser::TypeDeclarationStmt &) {
3139   if (!GetAttrs().HasAny({Attr::POINTER, Attr::ALLOCATABLE})) { // C702
3140     if (const auto *typeSpec{GetDeclTypeSpec()}) {
3141       if (typeSpec->category() == DeclTypeSpec::Character) {
3142         if (typeSpec->characterTypeSpec().length().isDeferred()) {
3143           Say("The type parameter LEN cannot be deferred without"
3144               " the POINTER or ALLOCATABLE attribute"_err_en_US);
3145         }
3146       } else if (const DerivedTypeSpec * derivedSpec{typeSpec->AsDerived()}) {
3147         for (const auto &pair : derivedSpec->parameters()) {
3148           if (pair.second.isDeferred()) {
3149             Say(currStmtSource().value(),
3150                 "The value of type parameter '%s' cannot be deferred"
3151                 " without the POINTER or ALLOCATABLE attribute"_err_en_US,
3152                 pair.first);
3153           }
3154         }
3155       }
3156     }
3157   }
3158   EndDecl();
3159 }
3160 
3161 void DeclarationVisitor::Post(const parser::DimensionStmt::Declaration &x) {
3162   const auto &name{std::get<parser::Name>(x.t)};
3163   DeclareObjectEntity(name, Attrs{});
3164 }
3165 void DeclarationVisitor::Post(const parser::CodimensionDecl &x) {
3166   const auto &name{std::get<parser::Name>(x.t)};
3167   DeclareObjectEntity(name, Attrs{});
3168 }
3169 
3170 bool DeclarationVisitor::Pre(const parser::Initialization &) {
3171   // Defer inspection of initializers to Initialization() so that the
3172   // symbol being initialized will be available within the initialization
3173   // expression.
3174   return false;
3175 }
3176 
3177 void DeclarationVisitor::Post(const parser::EntityDecl &x) {
3178   // TODO: may be under StructureStmt
3179   const auto &name{std::get<parser::ObjectName>(x.t)};
3180   Attrs attrs{attrs_ ? HandleSaveName(name.source, *attrs_) : Attrs{}};
3181   Symbol &symbol{DeclareUnknownEntity(name, attrs)};
3182   symbol.ReplaceName(name.source);
3183   if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
3184     if (ConvertToObjectEntity(symbol)) {
3185       Initialization(name, *init, false);
3186     }
3187   } else if (attrs.test(Attr::PARAMETER)) { // C882, C883
3188     Say(name, "Missing initialization for parameter '%s'"_err_en_US);
3189   }
3190 }
3191 
3192 void DeclarationVisitor::Post(const parser::PointerDecl &x) {
3193   const auto &name{std::get<parser::Name>(x.t)};
3194   Symbol &symbol{DeclareUnknownEntity(name, Attrs{Attr::POINTER})};
3195   symbol.ReplaceName(name.source);
3196 }
3197 
3198 bool DeclarationVisitor::Pre(const parser::BindEntity &x) {
3199   auto kind{std::get<parser::BindEntity::Kind>(x.t)};
3200   auto &name{std::get<parser::Name>(x.t)};
3201   Symbol *symbol;
3202   if (kind == parser::BindEntity::Kind::Object) {
3203     symbol = &HandleAttributeStmt(Attr::BIND_C, name);
3204   } else {
3205     symbol = &MakeCommonBlockSymbol(name);
3206     symbol->attrs().set(Attr::BIND_C);
3207   }
3208   SetBindNameOn(*symbol);
3209   return false;
3210 }
3211 bool DeclarationVisitor::Pre(const parser::NamedConstantDef &x) {
3212   auto &name{std::get<parser::NamedConstant>(x.t).v};
3213   auto &symbol{HandleAttributeStmt(Attr::PARAMETER, name)};
3214   if (!ConvertToObjectEntity(symbol) ||
3215       symbol.test(Symbol::Flag::CrayPointer) ||
3216       symbol.test(Symbol::Flag::CrayPointee)) {
3217     SayWithDecl(
3218         name, symbol, "PARAMETER attribute not allowed on '%s'"_err_en_US);
3219     return false;
3220   }
3221   const auto &expr{std::get<parser::ConstantExpr>(x.t)};
3222   ApplyImplicitRules(symbol);
3223   Walk(expr);
3224   if (auto converted{
3225           EvaluateConvertedExpr(symbol, expr, expr.thing.value().source)}) {
3226     symbol.get<ObjectEntityDetails>().set_init(std::move(*converted));
3227   }
3228   return false;
3229 }
3230 bool DeclarationVisitor::Pre(const parser::NamedConstant &x) {
3231   const parser::Name &name{x.v};
3232   if (!FindSymbol(name)) {
3233     Say(name, "Named constant '%s' not found"_err_en_US);
3234   } else {
3235     CheckUseError(name);
3236   }
3237   return false;
3238 }
3239 
3240 bool DeclarationVisitor::Pre(const parser::Enumerator &enumerator) {
3241   const parser::Name &name{std::get<parser::NamedConstant>(enumerator.t).v};
3242   Symbol *symbol{FindSymbol(name)};
3243   if (symbol) {
3244     // Contrary to named constants appearing in a PARAMETER statement,
3245     // enumerator names should not have their type, dimension or any other
3246     // attributes defined before they are declared in the enumerator statement.
3247     // This is not explicitly forbidden by the standard, but they are scalars
3248     // which type is left for the compiler to chose, so do not let users try to
3249     // tamper with that.
3250     SayAlreadyDeclared(name, *symbol);
3251     symbol = nullptr;
3252   } else {
3253     // Enumerators are treated as PARAMETER (section 7.6 paragraph (4))
3254     symbol = &MakeSymbol(name, Attrs{Attr::PARAMETER}, ObjectEntityDetails{});
3255     symbol->SetType(context().MakeNumericType(
3256         TypeCategory::Integer, evaluate::CInteger::kind));
3257   }
3258 
3259   if (auto &init{std::get<std::optional<parser::ScalarIntConstantExpr>>(
3260           enumerator.t)}) {
3261     Walk(*init); // Resolve names in expression before evaluation.
3262     MaybeIntExpr expr{EvaluateIntExpr(*init)};
3263     if (auto value{evaluate::ToInt64(expr)}) {
3264       // Cast all init expressions to C_INT so that they can then be
3265       // safely incremented (see 7.6 Note 2).
3266       enumerationState_.value = static_cast<int>(*value);
3267     } else {
3268       Say(name,
3269           "Enumerator value could not be computed "
3270           "from the given expression"_err_en_US);
3271       // Prevent resolution of next enumerators value
3272       enumerationState_.value = std::nullopt;
3273     }
3274   }
3275 
3276   if (symbol) {
3277     if (enumerationState_.value) {
3278       symbol->get<ObjectEntityDetails>().set_init(SomeExpr{
3279           evaluate::Expr<evaluate::CInteger>{*enumerationState_.value}});
3280     } else {
3281       context().SetError(*symbol);
3282     }
3283   }
3284 
3285   if (enumerationState_.value) {
3286     (*enumerationState_.value)++;
3287   }
3288   return false;
3289 }
3290 
3291 void DeclarationVisitor::Post(const parser::EnumDef &) {
3292   enumerationState_ = EnumeratorState{};
3293 }
3294 
3295 bool DeclarationVisitor::Pre(const parser::AccessSpec &x) {
3296   Attr attr{AccessSpecToAttr(x)};
3297   const Scope &scope{
3298       currScope().IsDerivedType() ? currScope().parent() : currScope()};
3299   if (!scope.IsModule()) { // C817
3300     Say(currStmtSource().value(),
3301         "%s attribute may only appear in the specification part of a module"_err_en_US,
3302         EnumToString(attr));
3303   }
3304   CheckAndSet(attr);
3305   return false;
3306 }
3307 
3308 bool DeclarationVisitor::Pre(const parser::AsynchronousStmt &x) {
3309   return HandleAttributeStmt(Attr::ASYNCHRONOUS, x.v);
3310 }
3311 bool DeclarationVisitor::Pre(const parser::ContiguousStmt &x) {
3312   return HandleAttributeStmt(Attr::CONTIGUOUS, x.v);
3313 }
3314 bool DeclarationVisitor::Pre(const parser::ExternalStmt &x) {
3315   HandleAttributeStmt(Attr::EXTERNAL, x.v);
3316   for (const auto &name : x.v) {
3317     auto *symbol{FindSymbol(name)};
3318     if (!ConvertToProcEntity(*symbol)) {
3319       SayWithDecl(
3320           name, *symbol, "EXTERNAL attribute not allowed on '%s'"_err_en_US);
3321     }
3322   }
3323   return false;
3324 }
3325 bool DeclarationVisitor::Pre(const parser::IntentStmt &x) {
3326   auto &intentSpec{std::get<parser::IntentSpec>(x.t)};
3327   auto &names{std::get<std::list<parser::Name>>(x.t)};
3328   return CheckNotInBlock("INTENT") && // C1107
3329       HandleAttributeStmt(IntentSpecToAttr(intentSpec), names);
3330 }
3331 bool DeclarationVisitor::Pre(const parser::IntrinsicStmt &x) {
3332   HandleAttributeStmt(Attr::INTRINSIC, x.v);
3333   for (const auto &name : x.v) {
3334     auto *symbol{FindSymbol(name)};
3335     if (!ConvertToProcEntity(*symbol)) {
3336       SayWithDecl(
3337           name, *symbol, "INTRINSIC attribute not allowed on '%s'"_err_en_US);
3338     } else if (symbol->attrs().test(Attr::EXTERNAL)) { // C840
3339       Say(symbol->name(),
3340           "Symbol '%s' cannot have both EXTERNAL and INTRINSIC attributes"_err_en_US,
3341           symbol->name());
3342     }
3343   }
3344   return false;
3345 }
3346 bool DeclarationVisitor::Pre(const parser::OptionalStmt &x) {
3347   return CheckNotInBlock("OPTIONAL") && // C1107
3348       HandleAttributeStmt(Attr::OPTIONAL, x.v);
3349 }
3350 bool DeclarationVisitor::Pre(const parser::ProtectedStmt &x) {
3351   return HandleAttributeStmt(Attr::PROTECTED, x.v);
3352 }
3353 bool DeclarationVisitor::Pre(const parser::ValueStmt &x) {
3354   return CheckNotInBlock("VALUE") && // C1107
3355       HandleAttributeStmt(Attr::VALUE, x.v);
3356 }
3357 bool DeclarationVisitor::Pre(const parser::VolatileStmt &x) {
3358   return HandleAttributeStmt(Attr::VOLATILE, x.v);
3359 }
3360 // Handle a statement that sets an attribute on a list of names.
3361 bool DeclarationVisitor::HandleAttributeStmt(
3362     Attr attr, const std::list<parser::Name> &names) {
3363   for (const auto &name : names) {
3364     HandleAttributeStmt(attr, name);
3365   }
3366   return false;
3367 }
3368 Symbol &DeclarationVisitor::HandleAttributeStmt(
3369     Attr attr, const parser::Name &name) {
3370   if (attr == Attr::INTRINSIC &&
3371       !context().intrinsics().IsIntrinsic(name.source.ToString())) {
3372     Say(name.source, "'%s' is not a known intrinsic procedure"_err_en_US);
3373   }
3374   auto *symbol{FindInScope(currScope(), name)};
3375   if (attr == Attr::ASYNCHRONOUS || attr == Attr::VOLATILE) {
3376     // these can be set on a symbol that is host-assoc or use-assoc
3377     if (!symbol &&
3378         (currScope().kind() == Scope::Kind::Subprogram ||
3379             currScope().kind() == Scope::Kind::Block)) {
3380       if (auto *hostSymbol{FindSymbol(name)}) {
3381         name.symbol = nullptr;
3382         symbol = &MakeSymbol(name, HostAssocDetails{*hostSymbol});
3383       }
3384     }
3385   } else if (symbol && symbol->has<UseDetails>()) {
3386     Say(currStmtSource().value(),
3387         "Cannot change %s attribute on use-associated '%s'"_err_en_US,
3388         EnumToString(attr), name.source);
3389     return *symbol;
3390   }
3391   if (!symbol) {
3392     symbol = &MakeSymbol(name, EntityDetails{});
3393   }
3394   symbol->attrs().set(attr);
3395   symbol->attrs() = HandleSaveName(name.source, symbol->attrs());
3396   return *symbol;
3397 }
3398 // C1107
3399 bool DeclarationVisitor::CheckNotInBlock(const char *stmt) {
3400   if (currScope().kind() == Scope::Kind::Block) {
3401     Say(MessageFormattedText{
3402         "%s statement is not allowed in a BLOCK construct"_err_en_US, stmt});
3403     return false;
3404   } else {
3405     return true;
3406   }
3407 }
3408 
3409 void DeclarationVisitor::Post(const parser::ObjectDecl &x) {
3410   CHECK(objectDeclAttr_);
3411   const auto &name{std::get<parser::ObjectName>(x.t)};
3412   DeclareObjectEntity(name, Attrs{*objectDeclAttr_});
3413 }
3414 
3415 // Declare an entity not yet known to be an object or proc.
3416 Symbol &DeclarationVisitor::DeclareUnknownEntity(
3417     const parser::Name &name, Attrs attrs) {
3418   if (!arraySpec().empty() || !coarraySpec().empty()) {
3419     return DeclareObjectEntity(name, attrs);
3420   } else {
3421     Symbol &symbol{DeclareEntity<EntityDetails>(name, attrs)};
3422     if (auto *type{GetDeclTypeSpec()}) {
3423       SetType(name, *type);
3424     }
3425     charInfo_.length.reset();
3426     SetBindNameOn(symbol);
3427     if (symbol.attrs().test(Attr::EXTERNAL)) {
3428       ConvertToProcEntity(symbol);
3429     }
3430     return symbol;
3431   }
3432 }
3433 
3434 Symbol &DeclarationVisitor::DeclareProcEntity(
3435     const parser::Name &name, Attrs attrs, const ProcInterface &interface) {
3436   Symbol &symbol{DeclareEntity<ProcEntityDetails>(name, attrs)};
3437   if (auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
3438     if (interface.type()) {
3439       symbol.set(Symbol::Flag::Function);
3440     } else if (interface.symbol()) {
3441       if (interface.symbol()->test(Symbol::Flag::Function)) {
3442         symbol.set(Symbol::Flag::Function);
3443       } else if (interface.symbol()->test(Symbol::Flag::Subroutine)) {
3444         symbol.set(Symbol::Flag::Subroutine);
3445       }
3446     }
3447     details->set_interface(interface);
3448     SetBindNameOn(symbol);
3449     SetPassNameOn(symbol);
3450   }
3451   return symbol;
3452 }
3453 
3454 Symbol &DeclarationVisitor::DeclareObjectEntity(
3455     const parser::Name &name, Attrs attrs) {
3456   Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, attrs)};
3457   if (auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
3458     if (auto *type{GetDeclTypeSpec()}) {
3459       SetType(name, *type);
3460     }
3461     if (!arraySpec().empty()) {
3462       if (details->IsArray()) {
3463         Say(name,
3464             "The dimensions of '%s' have already been declared"_err_en_US);
3465         context().SetError(symbol);
3466       } else {
3467         details->set_shape(arraySpec());
3468       }
3469     }
3470     if (!coarraySpec().empty()) {
3471       if (details->IsCoarray()) {
3472         Say(name,
3473             "The codimensions of '%s' have already been declared"_err_en_US);
3474         context().SetError(symbol);
3475       } else {
3476         details->set_coshape(coarraySpec());
3477       }
3478     }
3479     SetBindNameOn(symbol);
3480   }
3481   ClearArraySpec();
3482   ClearCoarraySpec();
3483   charInfo_.length.reset();
3484   return symbol;
3485 }
3486 
3487 void DeclarationVisitor::Post(const parser::IntegerTypeSpec &x) {
3488   SetDeclTypeSpec(MakeNumericType(TypeCategory::Integer, x.v));
3489 }
3490 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Real &x) {
3491   SetDeclTypeSpec(MakeNumericType(TypeCategory::Real, x.kind));
3492 }
3493 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Complex &x) {
3494   SetDeclTypeSpec(MakeNumericType(TypeCategory::Complex, x.kind));
3495 }
3496 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Logical &x) {
3497   SetDeclTypeSpec(MakeLogicalType(x.kind));
3498 }
3499 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Character &) {
3500   if (!charInfo_.length) {
3501     charInfo_.length = ParamValue{1, common::TypeParamAttr::Len};
3502   }
3503   if (!charInfo_.kind) {
3504     charInfo_.kind =
3505         KindExpr{context().GetDefaultKind(TypeCategory::Character)};
3506   }
3507   SetDeclTypeSpec(currScope().MakeCharacterType(
3508       std::move(*charInfo_.length), std::move(*charInfo_.kind)));
3509   charInfo_ = {};
3510 }
3511 void DeclarationVisitor::Post(const parser::CharSelector::LengthAndKind &x) {
3512   charInfo_.kind = EvaluateSubscriptIntExpr(x.kind);
3513   std::optional<std::int64_t> intKind{ToInt64(charInfo_.kind)};
3514   if (intKind &&
3515       !evaluate::IsValidKindOfIntrinsicType(
3516           TypeCategory::Character, *intKind)) { // C715, C719
3517     Say(currStmtSource().value(),
3518         "KIND value (%jd) not valid for CHARACTER"_err_en_US, *intKind);
3519   }
3520   if (x.length) {
3521     charInfo_.length = GetParamValue(*x.length, common::TypeParamAttr::Len);
3522   }
3523 }
3524 void DeclarationVisitor::Post(const parser::CharLength &x) {
3525   if (const auto *length{std::get_if<std::uint64_t>(&x.u)}) {
3526     charInfo_.length = ParamValue{
3527         static_cast<ConstantSubscript>(*length), common::TypeParamAttr::Len};
3528   } else {
3529     charInfo_.length = GetParamValue(
3530         std::get<parser::TypeParamValue>(x.u), common::TypeParamAttr::Len);
3531   }
3532 }
3533 void DeclarationVisitor::Post(const parser::LengthSelector &x) {
3534   if (const auto *param{std::get_if<parser::TypeParamValue>(&x.u)}) {
3535     charInfo_.length = GetParamValue(*param, common::TypeParamAttr::Len);
3536   }
3537 }
3538 
3539 bool DeclarationVisitor::Pre(const parser::KindParam &x) {
3540   if (const auto *kind{std::get_if<
3541           parser::Scalar<parser::Integer<parser::Constant<parser::Name>>>>(
3542           &x.u)}) {
3543     const parser::Name &name{kind->thing.thing.thing};
3544     if (!FindSymbol(name)) {
3545       Say(name, "Parameter '%s' not found"_err_en_US);
3546     }
3547   }
3548   return false;
3549 }
3550 
3551 bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Type &) {
3552   CHECK(GetDeclTypeSpecCategory() == DeclTypeSpec::Category::TypeDerived);
3553   return true;
3554 }
3555 
3556 void DeclarationVisitor::Post(const parser::DeclarationTypeSpec::Type &type) {
3557   const parser::Name &derivedName{std::get<parser::Name>(type.derived.t)};
3558   if (const Symbol * derivedSymbol{derivedName.symbol}) {
3559     CheckForAbstractType(*derivedSymbol); // C706
3560   }
3561 }
3562 
3563 bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Class &) {
3564   SetDeclTypeSpecCategory(DeclTypeSpec::Category::ClassDerived);
3565   return true;
3566 }
3567 
3568 void DeclarationVisitor::Post(
3569     const parser::DeclarationTypeSpec::Class &parsedClass) {
3570   const auto &typeName{std::get<parser::Name>(parsedClass.derived.t)};
3571   if (auto spec{ResolveDerivedType(typeName)};
3572       spec && !IsExtensibleType(&*spec)) { // C705
3573     SayWithDecl(typeName, *typeName.symbol,
3574         "Non-extensible derived type '%s' may not be used with CLASS"
3575         " keyword"_err_en_US);
3576   }
3577 }
3578 
3579 bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Record &) {
3580   // TODO
3581   return true;
3582 }
3583 
3584 void DeclarationVisitor::Post(const parser::DerivedTypeSpec &x) {
3585   const auto &typeName{std::get<parser::Name>(x.t)};
3586   auto spec{ResolveDerivedType(typeName)};
3587   if (!spec) {
3588     return;
3589   }
3590   bool seenAnyName{false};
3591   for (const auto &typeParamSpec :
3592       std::get<std::list<parser::TypeParamSpec>>(x.t)) {
3593     const auto &optKeyword{
3594         std::get<std::optional<parser::Keyword>>(typeParamSpec.t)};
3595     std::optional<SourceName> name;
3596     if (optKeyword) {
3597       seenAnyName = true;
3598       name = optKeyword->v.source;
3599     } else if (seenAnyName) {
3600       Say(typeName.source, "Type parameter value must have a name"_err_en_US);
3601       continue;
3602     }
3603     const auto &value{std::get<parser::TypeParamValue>(typeParamSpec.t)};
3604     // The expressions in a derived type specifier whose values define
3605     // non-defaulted type parameters are evaluated (folded) in the enclosing
3606     // scope.  The KIND/LEN distinction is resolved later in
3607     // DerivedTypeSpec::CookParameters().
3608     ParamValue param{GetParamValue(value, common::TypeParamAttr::Kind)};
3609     if (!param.isExplicit() || param.GetExplicit()) {
3610       spec->AddRawParamValue(optKeyword, std::move(param));
3611     }
3612   }
3613 
3614   // The DerivedTypeSpec *spec is used initially as a search key.
3615   // If it turns out to have the same name and actual parameter
3616   // value expressions as another DerivedTypeSpec in the current
3617   // scope does, then we'll use that extant spec; otherwise, when this
3618   // spec is distinct from all derived types previously instantiated
3619   // in the current scope, this spec will be moved into that collection.
3620   const auto &dtDetails{spec->typeSymbol().get<DerivedTypeDetails>()};
3621   auto category{GetDeclTypeSpecCategory()};
3622   if (dtDetails.isForwardReferenced()) {
3623     DeclTypeSpec &type{currScope().MakeDerivedType(category, std::move(*spec))};
3624     SetDeclTypeSpec(type);
3625     return;
3626   }
3627   // Normalize parameters to produce a better search key.
3628   spec->CookParameters(GetFoldingContext());
3629   if (!spec->MightBeParameterized()) {
3630     spec->EvaluateParameters(GetFoldingContext());
3631   }
3632   if (const DeclTypeSpec *
3633       extant{currScope().FindInstantiatedDerivedType(*spec, category)}) {
3634     // This derived type and parameter expressions (if any) are already present
3635     // in this scope.
3636     SetDeclTypeSpec(*extant);
3637   } else {
3638     DeclTypeSpec &type{currScope().MakeDerivedType(category, std::move(*spec))};
3639     DerivedTypeSpec &derived{type.derivedTypeSpec()};
3640     if (derived.MightBeParameterized() &&
3641         currScope().IsParameterizedDerivedType()) {
3642       // Defer instantiation; use the derived type's definition's scope.
3643       derived.set_scope(DEREF(spec->typeSymbol().scope()));
3644     } else {
3645       auto restorer{
3646           GetFoldingContext().messages().SetLocation(currStmtSource().value())};
3647       derived.Instantiate(currScope(), context());
3648     }
3649     SetDeclTypeSpec(type);
3650   }
3651   // Capture the DerivedTypeSpec in the parse tree for use in building
3652   // structure constructor expressions.
3653   x.derivedTypeSpec = &GetDeclTypeSpec()->derivedTypeSpec();
3654 }
3655 
3656 // The descendents of DerivedTypeDef in the parse tree are visited directly
3657 // in this Pre() routine so that recursive use of the derived type can be
3658 // supported in the components.
3659 bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
3660   auto &stmt{std::get<parser::Statement<parser::DerivedTypeStmt>>(x.t)};
3661   Walk(stmt);
3662   Walk(std::get<std::list<parser::Statement<parser::TypeParamDefStmt>>>(x.t));
3663   auto &scope{currScope()};
3664   CHECK(scope.symbol());
3665   CHECK(scope.symbol()->scope() == &scope);
3666   auto &details{scope.symbol()->get<DerivedTypeDetails>()};
3667   std::set<SourceName> paramNames;
3668   for (auto &paramName : std::get<std::list<parser::Name>>(stmt.statement.t)) {
3669     details.add_paramName(paramName.source);
3670     auto *symbol{FindInScope(scope, paramName)};
3671     if (!symbol) {
3672       Say(paramName,
3673           "No definition found for type parameter '%s'"_err_en_US); // C742
3674     } else if (!symbol->has<TypeParamDetails>()) {
3675       Say2(paramName, "'%s' is not defined as a type parameter"_err_en_US,
3676           *symbol, "Definition of '%s'"_en_US); // C741
3677     }
3678     if (!paramNames.insert(paramName.source).second) {
3679       Say(paramName,
3680           "Duplicate type parameter name: '%s'"_err_en_US); // C731
3681     }
3682   }
3683   for (const auto &[name, symbol] : currScope()) {
3684     if (symbol->has<TypeParamDetails>() && !paramNames.count(name)) {
3685       SayDerivedType(name,
3686           "'%s' is not a type parameter of this derived type"_err_en_US,
3687           currScope()); // C741
3688     }
3689   }
3690   Walk(std::get<std::list<parser::Statement<parser::PrivateOrSequence>>>(x.t));
3691   const auto &componentDefs{
3692       std::get<std::list<parser::Statement<parser::ComponentDefStmt>>>(x.t)};
3693   Walk(componentDefs);
3694   if (derivedTypeInfo_.sequence) {
3695     details.set_sequence(true);
3696     if (componentDefs.empty()) { // C740
3697       Say(stmt.source,
3698           "A sequence type must have at least one component"_err_en_US);
3699     }
3700     if (!details.paramNames().empty()) { // C740
3701       Say(stmt.source,
3702           "A sequence type may not have type parameters"_err_en_US);
3703     }
3704     if (derivedTypeInfo_.extends) { // C735
3705       Say(stmt.source,
3706           "A sequence type may not have the EXTENDS attribute"_err_en_US);
3707     } else {
3708       for (const auto &componentName : details.componentNames()) {
3709         const Symbol *componentSymbol{scope.FindComponent(componentName)};
3710         if (componentSymbol && componentSymbol->has<ObjectEntityDetails>()) {
3711           const auto &componentDetails{
3712               componentSymbol->get<ObjectEntityDetails>()};
3713           const DeclTypeSpec *componentType{componentDetails.type()};
3714           if (componentType && // C740
3715               !componentType->AsIntrinsic() &&
3716               !componentType->IsSequenceType()) {
3717             Say(componentSymbol->name(),
3718                 "A sequence type data component must either be of an"
3719                 " intrinsic type or a derived sequence type"_err_en_US);
3720           }
3721         }
3722       }
3723     }
3724   }
3725   Walk(std::get<std::optional<parser::TypeBoundProcedurePart>>(x.t));
3726   Walk(std::get<parser::Statement<parser::EndTypeStmt>>(x.t));
3727   derivedTypeInfo_ = {};
3728   PopScope();
3729   return false;
3730 }
3731 bool DeclarationVisitor::Pre(const parser::DerivedTypeStmt &) {
3732   return BeginAttrs();
3733 }
3734 void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
3735   auto &name{std::get<parser::Name>(x.t)};
3736   // Resolve the EXTENDS() clause before creating the derived
3737   // type's symbol to foil attempts to recursively extend a type.
3738   auto *extendsName{derivedTypeInfo_.extends};
3739   std::optional<DerivedTypeSpec> extendsType{
3740       ResolveExtendsType(name, extendsName)};
3741   auto &symbol{MakeSymbol(name, GetAttrs(), DerivedTypeDetails{})};
3742   symbol.ReplaceName(name.source);
3743   derivedTypeInfo_.type = &symbol;
3744   PushScope(Scope::Kind::DerivedType, &symbol);
3745   if (extendsType) {
3746     // Declare the "parent component"; private if the type is.
3747     // Any symbol stored in the EXTENDS() clause is temporarily
3748     // hidden so that a new symbol can be created for the parent
3749     // component without producing spurious errors about already
3750     // existing.
3751     const Symbol &extendsSymbol{extendsType->typeSymbol()};
3752     auto restorer{common::ScopedSet(extendsName->symbol, nullptr)};
3753     if (OkToAddComponent(*extendsName, &extendsSymbol)) {
3754       auto &comp{DeclareEntity<ObjectEntityDetails>(*extendsName, Attrs{})};
3755       comp.attrs().set(
3756           Attr::PRIVATE, extendsSymbol.attrs().test(Attr::PRIVATE));
3757       comp.set(Symbol::Flag::ParentComp);
3758       DeclTypeSpec &type{currScope().MakeDerivedType(
3759           DeclTypeSpec::TypeDerived, std::move(*extendsType))};
3760       type.derivedTypeSpec().set_scope(*extendsSymbol.scope());
3761       comp.SetType(type);
3762       DerivedTypeDetails &details{symbol.get<DerivedTypeDetails>()};
3763       details.add_component(comp);
3764     }
3765   }
3766   EndAttrs();
3767 }
3768 
3769 void DeclarationVisitor::Post(const parser::TypeParamDefStmt &x) {
3770   auto *type{GetDeclTypeSpec()};
3771   auto attr{std::get<common::TypeParamAttr>(x.t)};
3772   for (auto &decl : std::get<std::list<parser::TypeParamDecl>>(x.t)) {
3773     auto &name{std::get<parser::Name>(decl.t)};
3774     if (Symbol * symbol{MakeTypeSymbol(name, TypeParamDetails{attr})}) {
3775       SetType(name, *type);
3776       if (auto &init{
3777               std::get<std::optional<parser::ScalarIntConstantExpr>>(decl.t)}) {
3778         if (auto maybeExpr{EvaluateConvertedExpr(
3779                 *symbol, *init, init->thing.thing.thing.value().source)}) {
3780           auto *intExpr{std::get_if<SomeIntExpr>(&maybeExpr->u)};
3781           CHECK(intExpr);
3782           symbol->get<TypeParamDetails>().set_init(std::move(*intExpr));
3783         }
3784       }
3785     }
3786   }
3787   EndDecl();
3788 }
3789 bool DeclarationVisitor::Pre(const parser::TypeAttrSpec::Extends &x) {
3790   if (derivedTypeInfo_.extends) {
3791     Say(currStmtSource().value(),
3792         "Attribute 'EXTENDS' cannot be used more than once"_err_en_US);
3793   } else {
3794     derivedTypeInfo_.extends = &x.v;
3795   }
3796   return false;
3797 }
3798 
3799 bool DeclarationVisitor::Pre(const parser::PrivateStmt &) {
3800   if (!currScope().parent().IsModule()) {
3801     Say("PRIVATE is only allowed in a derived type that is"
3802         " in a module"_err_en_US); // C766
3803   } else if (derivedTypeInfo_.sawContains) {
3804     derivedTypeInfo_.privateBindings = true;
3805   } else if (!derivedTypeInfo_.privateComps) {
3806     derivedTypeInfo_.privateComps = true;
3807   } else {
3808     Say("PRIVATE may not appear more than once in"
3809         " derived type components"_en_US); // C738
3810   }
3811   return false;
3812 }
3813 bool DeclarationVisitor::Pre(const parser::SequenceStmt &) {
3814   if (derivedTypeInfo_.sequence) {
3815     Say("SEQUENCE may not appear more than once in"
3816         " derived type components"_en_US); // C738
3817   }
3818   derivedTypeInfo_.sequence = true;
3819   return false;
3820 }
3821 void DeclarationVisitor::Post(const parser::ComponentDecl &x) {
3822   const auto &name{std::get<parser::Name>(x.t)};
3823   auto attrs{GetAttrs()};
3824   if (derivedTypeInfo_.privateComps &&
3825       !attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
3826     attrs.set(Attr::PRIVATE);
3827   }
3828   if (const auto *declType{GetDeclTypeSpec()}) {
3829     if (const auto *derived{declType->AsDerived()}) {
3830       if (!attrs.HasAny({Attr::POINTER, Attr::ALLOCATABLE})) {
3831         if (derivedTypeInfo_.type == &derived->typeSymbol()) { // C744
3832           Say("Recursive use of the derived type requires "
3833               "POINTER or ALLOCATABLE"_err_en_US);
3834         }
3835       }
3836       if (!coarraySpec().empty()) { // C747
3837         if (IsTeamType(derived)) {
3838           Say("A coarray component may not be of type TEAM_TYPE from "
3839               "ISO_FORTRAN_ENV"_err_en_US);
3840         } else {
3841           if (IsIsoCType(derived)) {
3842             Say("A coarray component may not be of type C_PTR or C_FUNPTR from "
3843                 "ISO_C_BINDING"_err_en_US);
3844           }
3845         }
3846       }
3847       if (auto it{FindCoarrayUltimateComponent(*derived)}) { // C748
3848         std::string ultimateName{it.BuildResultDesignatorName()};
3849         // Strip off the leading "%"
3850         if (ultimateName.length() > 1) {
3851           ultimateName.erase(0, 1);
3852           if (attrs.HasAny({Attr::POINTER, Attr::ALLOCATABLE})) {
3853             evaluate::AttachDeclaration(
3854                 Say(name.source,
3855                     "A component with a POINTER or ALLOCATABLE attribute may "
3856                     "not "
3857                     "be of a type with a coarray ultimate component (named "
3858                     "'%s')"_err_en_US,
3859                     ultimateName),
3860                 derived->typeSymbol());
3861           }
3862           if (!arraySpec().empty() || !coarraySpec().empty()) {
3863             evaluate::AttachDeclaration(
3864                 Say(name.source,
3865                     "An array or coarray component may not be of a type with a "
3866                     "coarray ultimate component (named '%s')"_err_en_US,
3867                     ultimateName),
3868                 derived->typeSymbol());
3869           }
3870         }
3871       }
3872     }
3873   }
3874   if (OkToAddComponent(name)) {
3875     auto &symbol{DeclareObjectEntity(name, attrs)};
3876     if (symbol.has<ObjectEntityDetails>()) {
3877       if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
3878         Initialization(name, *init, true);
3879       }
3880     }
3881     currScope().symbol()->get<DerivedTypeDetails>().add_component(symbol);
3882   }
3883   ClearArraySpec();
3884   ClearCoarraySpec();
3885 }
3886 bool DeclarationVisitor::Pre(const parser::ProcedureDeclarationStmt &) {
3887   CHECK(!interfaceName_);
3888   return BeginDecl();
3889 }
3890 void DeclarationVisitor::Post(const parser::ProcedureDeclarationStmt &) {
3891   interfaceName_ = nullptr;
3892   EndDecl();
3893 }
3894 bool DeclarationVisitor::Pre(const parser::DataComponentDefStmt &x) {
3895   // Overrides parse tree traversal so as to handle attributes first,
3896   // so POINTER & ALLOCATABLE enable forward references to derived types.
3897   Walk(std::get<std::list<parser::ComponentAttrSpec>>(x.t));
3898   set_allowForwardReferenceToDerivedType(
3899       GetAttrs().HasAny({Attr::POINTER, Attr::ALLOCATABLE}));
3900   Walk(std::get<parser::DeclarationTypeSpec>(x.t));
3901   set_allowForwardReferenceToDerivedType(false);
3902   Walk(std::get<std::list<parser::ComponentDecl>>(x.t));
3903   return false;
3904 }
3905 bool DeclarationVisitor::Pre(const parser::ProcComponentDefStmt &) {
3906   CHECK(!interfaceName_);
3907   return true;
3908 }
3909 void DeclarationVisitor::Post(const parser::ProcComponentDefStmt &) {
3910   interfaceName_ = nullptr;
3911 }
3912 bool DeclarationVisitor::Pre(const parser::ProcPointerInit &x) {
3913   if (auto *name{std::get_if<parser::Name>(&x.u)}) {
3914     return !NameIsKnownOrIntrinsic(*name);
3915   }
3916   return true;
3917 }
3918 void DeclarationVisitor::Post(const parser::ProcInterface &x) {
3919   if (auto *name{std::get_if<parser::Name>(&x.u)}) {
3920     interfaceName_ = name;
3921     NoteInterfaceName(*name);
3922   }
3923 }
3924 
3925 void DeclarationVisitor::Post(const parser::ProcDecl &x) {
3926   const auto &name{std::get<parser::Name>(x.t)};
3927   ProcInterface interface;
3928   if (interfaceName_) {
3929     interface.set_symbol(*interfaceName_->symbol);
3930   } else if (auto *type{GetDeclTypeSpec()}) {
3931     interface.set_type(*type);
3932   }
3933   auto attrs{HandleSaveName(name.source, GetAttrs())};
3934   DerivedTypeDetails *dtDetails{nullptr};
3935   if (Symbol * symbol{currScope().symbol()}) {
3936     dtDetails = symbol->detailsIf<DerivedTypeDetails>();
3937   }
3938   if (!dtDetails) {
3939     attrs.set(Attr::EXTERNAL);
3940   }
3941   Symbol &symbol{DeclareProcEntity(name, attrs, interface)};
3942   symbol.ReplaceName(name.source);
3943   if (dtDetails) {
3944     dtDetails->add_component(symbol);
3945   }
3946 }
3947 
3948 bool DeclarationVisitor::Pre(const parser::TypeBoundProcedurePart &) {
3949   derivedTypeInfo_.sawContains = true;
3950   return true;
3951 }
3952 
3953 // Resolve binding names from type-bound generics, saved in genericBindings_.
3954 void DeclarationVisitor::Post(const parser::TypeBoundProcedurePart &) {
3955   // track specifics seen for the current generic to detect duplicates:
3956   const Symbol *currGeneric{nullptr};
3957   std::set<SourceName> specifics;
3958   for (const auto &[generic, bindingName] : genericBindings_) {
3959     if (generic != currGeneric) {
3960       currGeneric = generic;
3961       specifics.clear();
3962     }
3963     auto [it, inserted]{specifics.insert(bindingName->source)};
3964     if (!inserted) {
3965       Say(*bindingName, // C773
3966           "Binding name '%s' was already specified for generic '%s'"_err_en_US,
3967           bindingName->source, generic->name())
3968           .Attach(*it, "Previous specification of '%s'"_en_US, *it);
3969       continue;
3970     }
3971     auto *symbol{FindInTypeOrParents(*bindingName)};
3972     if (!symbol) {
3973       Say(*bindingName, // C772
3974           "Binding name '%s' not found in this derived type"_err_en_US);
3975     } else if (!symbol->has<ProcBindingDetails>()) {
3976       SayWithDecl(*bindingName, *symbol, // C772
3977           "'%s' is not the name of a specific binding of this type"_err_en_US);
3978     } else {
3979       generic->get<GenericDetails>().AddSpecificProc(
3980           *symbol, bindingName->source);
3981     }
3982   }
3983   genericBindings_.clear();
3984 }
3985 
3986 void DeclarationVisitor::Post(const parser::ContainsStmt &) {
3987   if (derivedTypeInfo_.sequence) {
3988     Say("A sequence type may not have a CONTAINS statement"_err_en_US); // C740
3989   }
3990 }
3991 
3992 void DeclarationVisitor::Post(
3993     const parser::TypeBoundProcedureStmt::WithoutInterface &x) {
3994   if (GetAttrs().test(Attr::DEFERRED)) { // C783
3995     Say("DEFERRED is only allowed when an interface-name is provided"_err_en_US);
3996   }
3997   for (auto &declaration : x.declarations) {
3998     auto &bindingName{std::get<parser::Name>(declaration.t)};
3999     auto &optName{std::get<std::optional<parser::Name>>(declaration.t)};
4000     const parser::Name &procedureName{optName ? *optName : bindingName};
4001     Symbol *procedure{FindSymbol(procedureName)};
4002     if (!procedure) {
4003       procedure = NoteInterfaceName(procedureName);
4004     }
4005     if (auto *s{MakeTypeSymbol(bindingName, ProcBindingDetails{*procedure})}) {
4006       SetPassNameOn(*s);
4007       if (GetAttrs().test(Attr::DEFERRED)) {
4008         context().SetError(*s);
4009       }
4010     }
4011   }
4012 }
4013 
4014 void DeclarationVisitor::CheckBindings(
4015     const parser::TypeBoundProcedureStmt::WithoutInterface &tbps) {
4016   CHECK(currScope().IsDerivedType());
4017   for (auto &declaration : tbps.declarations) {
4018     auto &bindingName{std::get<parser::Name>(declaration.t)};
4019     if (Symbol * binding{FindInScope(currScope(), bindingName)}) {
4020       if (auto *details{binding->detailsIf<ProcBindingDetails>()}) {
4021         const Symbol *procedure{FindSubprogram(details->symbol())};
4022         if (!CanBeTypeBoundProc(procedure)) {
4023           if (details->symbol().name() != binding->name()) {
4024             Say(binding->name(),
4025                 "The binding of '%s' ('%s') must be either an accessible "
4026                 "module procedure or an external procedure with "
4027                 "an explicit interface"_err_en_US,
4028                 binding->name(), details->symbol().name());
4029           } else {
4030             Say(binding->name(),
4031                 "'%s' must be either an accessible module procedure "
4032                 "or an external procedure with an explicit interface"_err_en_US,
4033                 binding->name());
4034           }
4035           context().SetError(*binding);
4036         }
4037       }
4038     }
4039   }
4040 }
4041 
4042 void DeclarationVisitor::Post(
4043     const parser::TypeBoundProcedureStmt::WithInterface &x) {
4044   if (!GetAttrs().test(Attr::DEFERRED)) { // C783
4045     Say("DEFERRED is required when an interface-name is provided"_err_en_US);
4046   }
4047   if (Symbol * interface{NoteInterfaceName(x.interfaceName)}) {
4048     for (auto &bindingName : x.bindingNames) {
4049       if (auto *s{
4050               MakeTypeSymbol(bindingName, ProcBindingDetails{*interface})}) {
4051         SetPassNameOn(*s);
4052         if (!GetAttrs().test(Attr::DEFERRED)) {
4053           context().SetError(*s);
4054         }
4055       }
4056     }
4057   }
4058 }
4059 
4060 void DeclarationVisitor::Post(const parser::FinalProcedureStmt &x) {
4061   for (auto &name : x.v) {
4062     MakeTypeSymbol(name, FinalProcDetails{});
4063   }
4064 }
4065 
4066 bool DeclarationVisitor::Pre(const parser::TypeBoundGenericStmt &x) {
4067   const auto &accessSpec{std::get<std::optional<parser::AccessSpec>>(x.t)};
4068   const auto &genericSpec{std::get<Indirection<parser::GenericSpec>>(x.t)};
4069   const auto &bindingNames{std::get<std::list<parser::Name>>(x.t)};
4070   auto info{GenericSpecInfo{genericSpec.value()}};
4071   SourceName symbolName{info.symbolName()};
4072   bool isPrivate{accessSpec ? accessSpec->v == parser::AccessSpec::Kind::Private
4073                             : derivedTypeInfo_.privateBindings};
4074   auto *genericSymbol{info.FindInScope(context(), currScope())};
4075   if (genericSymbol) {
4076     if (!genericSymbol->has<GenericDetails>()) {
4077       genericSymbol = nullptr; // MakeTypeSymbol will report the error below
4078     }
4079   } else {
4080     // look in parent types:
4081     Symbol *inheritedSymbol{nullptr};
4082     for (const auto &name : info.GetAllNames(context())) {
4083       inheritedSymbol = currScope().FindComponent(SourceName{name});
4084       if (inheritedSymbol) {
4085         break;
4086       }
4087     }
4088     if (inheritedSymbol && inheritedSymbol->has<GenericDetails>()) {
4089       CheckAccessibility(symbolName, isPrivate, *inheritedSymbol); // C771
4090     }
4091   }
4092   if (genericSymbol) {
4093     CheckAccessibility(symbolName, isPrivate, *genericSymbol); // C771
4094   } else {
4095     genericSymbol = MakeTypeSymbol(symbolName, GenericDetails{});
4096     if (!genericSymbol) {
4097       return false;
4098     }
4099     if (isPrivate) {
4100       genericSymbol->attrs().set(Attr::PRIVATE);
4101     }
4102   }
4103   for (const parser::Name &bindingName : bindingNames) {
4104     genericBindings_.emplace(genericSymbol, &bindingName);
4105   }
4106   info.Resolve(genericSymbol);
4107   return false;
4108 }
4109 
4110 bool DeclarationVisitor::Pre(const parser::AllocateStmt &) {
4111   BeginDeclTypeSpec();
4112   return true;
4113 }
4114 void DeclarationVisitor::Post(const parser::AllocateStmt &) {
4115   EndDeclTypeSpec();
4116 }
4117 
4118 bool DeclarationVisitor::Pre(const parser::StructureConstructor &x) {
4119   auto &parsedType{std::get<parser::DerivedTypeSpec>(x.t)};
4120   const DeclTypeSpec *type{ProcessTypeSpec(parsedType)};
4121   if (!type) {
4122     return false;
4123   }
4124   const DerivedTypeSpec *spec{type->AsDerived()};
4125   const Scope *typeScope{spec ? spec->scope() : nullptr};
4126   if (!typeScope) {
4127     return false;
4128   }
4129 
4130   // N.B C7102 is implicitly enforced by having inaccessible types not
4131   // being found in resolution.
4132   // More constraints are enforced in expression.cpp so that they
4133   // can apply to structure constructors that have been converted
4134   // from misparsed function references.
4135   for (const auto &component :
4136       std::get<std::list<parser::ComponentSpec>>(x.t)) {
4137     // Visit the component spec expression, but not the keyword, since
4138     // we need to resolve its symbol in the scope of the derived type.
4139     Walk(std::get<parser::ComponentDataSource>(component.t));
4140     if (const auto &kw{std::get<std::optional<parser::Keyword>>(component.t)}) {
4141       FindInTypeOrParents(*typeScope, kw->v);
4142     }
4143   }
4144   return false;
4145 }
4146 
4147 bool DeclarationVisitor::Pre(const parser::BasedPointerStmt &x) {
4148   for (const parser::BasedPointer &bp : x.v) {
4149     const parser::ObjectName &pointerName{std::get<0>(bp.t)};
4150     const parser::ObjectName &pointeeName{std::get<1>(bp.t)};
4151     auto *pointer{FindSymbol(pointerName)};
4152     if (!pointer) {
4153       pointer = &MakeSymbol(pointerName, ObjectEntityDetails{});
4154     } else if (!ConvertToObjectEntity(*pointer) || IsNamedConstant(*pointer)) {
4155       SayWithDecl(pointerName, *pointer, "'%s' is not a variable"_err_en_US);
4156     } else if (pointer->Rank() > 0) {
4157       SayWithDecl(pointerName, *pointer,
4158           "Cray pointer '%s' must be a scalar"_err_en_US);
4159     } else if (pointer->test(Symbol::Flag::CrayPointee)) {
4160       Say(pointerName,
4161           "'%s' cannot be a Cray pointer as it is already a Cray pointee"_err_en_US);
4162     }
4163     pointer->set(Symbol::Flag::CrayPointer);
4164     const DeclTypeSpec &pointerType{MakeNumericType(TypeCategory::Integer,
4165         context().defaultKinds().subscriptIntegerKind())};
4166     const auto *type{pointer->GetType()};
4167     if (!type) {
4168       pointer->SetType(pointerType);
4169     } else if (*type != pointerType) {
4170       Say(pointerName.source, "Cray pointer '%s' must have type %s"_err_en_US,
4171           pointerName.source, pointerType.AsFortran());
4172     }
4173     if (ResolveName(pointeeName)) {
4174       Symbol &pointee{*pointeeName.symbol};
4175       if (pointee.has<UseDetails>()) {
4176         Say(pointeeName,
4177             "'%s' cannot be a Cray pointee as it is use-associated"_err_en_US);
4178         continue;
4179       } else if (!ConvertToObjectEntity(pointee) || IsNamedConstant(pointee)) {
4180         Say(pointeeName, "'%s' is not a variable"_err_en_US);
4181         continue;
4182       } else if (pointee.test(Symbol::Flag::CrayPointer)) {
4183         Say(pointeeName,
4184             "'%s' cannot be a Cray pointee as it is already a Cray pointer"_err_en_US);
4185       } else if (pointee.test(Symbol::Flag::CrayPointee)) {
4186         Say(pointeeName,
4187             "'%s' was already declared as a Cray pointee"_err_en_US);
4188       } else {
4189         pointee.set(Symbol::Flag::CrayPointee);
4190       }
4191       if (const auto *pointeeType{pointee.GetType()}) {
4192         if (const auto *derived{pointeeType->AsDerived()}) {
4193           if (!derived->typeSymbol().get<DerivedTypeDetails>().sequence()) {
4194             Say(pointeeName,
4195                 "Type of Cray pointee '%s' is a non-sequence derived type"_err_en_US);
4196           }
4197         }
4198       }
4199       // process the pointee array-spec, if present
4200       BeginArraySpec();
4201       Walk(std::get<std::optional<parser::ArraySpec>>(bp.t));
4202       const auto &spec{arraySpec()};
4203       if (!spec.empty()) {
4204         auto &details{pointee.get<ObjectEntityDetails>()};
4205         if (details.shape().empty()) {
4206           details.set_shape(spec);
4207         } else {
4208           SayWithDecl(pointeeName, pointee,
4209               "Array spec was already declared for '%s'"_err_en_US);
4210         }
4211       }
4212       ClearArraySpec();
4213       currScope().add_crayPointer(pointeeName.source, *pointer);
4214     }
4215   }
4216   return false;
4217 }
4218 
4219 bool DeclarationVisitor::Pre(const parser::NamelistStmt::Group &x) {
4220   if (!CheckNotInBlock("NAMELIST")) { // C1107
4221     return false;
4222   }
4223 
4224   NamelistDetails details;
4225   for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
4226     auto *symbol{FindSymbol(name)};
4227     if (!symbol) {
4228       symbol = &MakeSymbol(name, ObjectEntityDetails{});
4229       ApplyImplicitRules(*symbol);
4230     } else if (!ConvertToObjectEntity(*symbol)) {
4231       SayWithDecl(name, *symbol, "'%s' is not a variable"_err_en_US);
4232     }
4233     details.add_object(*symbol);
4234   }
4235 
4236   const auto &groupName{std::get<parser::Name>(x.t)};
4237   auto *groupSymbol{FindInScope(currScope(), groupName)};
4238   if (!groupSymbol || !groupSymbol->has<NamelistDetails>()) {
4239     groupSymbol = &MakeSymbol(groupName, std::move(details));
4240     groupSymbol->ReplaceName(groupName.source);
4241   }
4242   groupSymbol->get<NamelistDetails>().add_objects(details.objects());
4243   return false;
4244 }
4245 
4246 bool DeclarationVisitor::Pre(const parser::IoControlSpec &x) {
4247   if (const auto *name{std::get_if<parser::Name>(&x.u)}) {
4248     auto *symbol{FindSymbol(*name)};
4249     if (!symbol) {
4250       Say(*name, "Namelist group '%s' not found"_err_en_US);
4251     } else if (!symbol->GetUltimate().has<NamelistDetails>()) {
4252       SayWithDecl(
4253           *name, *symbol, "'%s' is not the name of a namelist group"_err_en_US);
4254     }
4255   }
4256   return true;
4257 }
4258 
4259 bool DeclarationVisitor::Pre(const parser::CommonStmt::Block &x) {
4260   CheckNotInBlock("COMMON"); // C1107
4261   const auto &optName{std::get<std::optional<parser::Name>>(x.t)};
4262   parser::Name blankCommon;
4263   blankCommon.source =
4264       SourceName{currStmtSource().value().begin(), std::size_t{0}};
4265   CHECK(!commonBlockInfo_.curr);
4266   commonBlockInfo_.curr =
4267       &MakeCommonBlockSymbol(optName ? *optName : blankCommon);
4268   return true;
4269 }
4270 
4271 void DeclarationVisitor::Post(const parser::CommonStmt::Block &) {
4272   commonBlockInfo_.curr = nullptr;
4273 }
4274 
4275 bool DeclarationVisitor::Pre(const parser::CommonBlockObject &) {
4276   BeginArraySpec();
4277   return true;
4278 }
4279 
4280 void DeclarationVisitor::Post(const parser::CommonBlockObject &x) {
4281   CHECK(commonBlockInfo_.curr);
4282   const auto &name{std::get<parser::Name>(x.t)};
4283   auto &symbol{DeclareObjectEntity(name, Attrs{})};
4284   ClearArraySpec();
4285   ClearCoarraySpec();
4286   auto *details{symbol.detailsIf<ObjectEntityDetails>()};
4287   if (!details) {
4288     return; // error was reported
4289   }
4290   commonBlockInfo_.curr->get<CommonBlockDetails>().add_object(symbol);
4291   auto pair{commonBlockInfo_.names.insert(name.source)};
4292   if (!pair.second) {
4293     const SourceName &prev{*pair.first};
4294     Say2(name.source, "'%s' is already in a COMMON block"_err_en_US, prev,
4295         "Previous occurrence of '%s' in a COMMON block"_en_US);
4296     return;
4297   }
4298   details->set_commonBlock(*commonBlockInfo_.curr);
4299 }
4300 
4301 bool DeclarationVisitor::Pre(const parser::EquivalenceStmt &x) {
4302   // save equivalence sets to be processed after specification part
4303   CheckNotInBlock("EQUIVALENCE"); // C1107
4304   for (const std::list<parser::EquivalenceObject> &set : x.v) {
4305     equivalenceSets_.push_back(&set);
4306   }
4307   return false; // don't implicitly declare names yet
4308 }
4309 
4310 void DeclarationVisitor::CheckEquivalenceSets() {
4311   EquivalenceSets equivSets{context()};
4312   for (const auto *set : equivalenceSets_) {
4313     const auto &source{set->front().v.value().source};
4314     if (set->size() <= 1) { // R871
4315       Say(source, "Equivalence set must have more than one object"_err_en_US);
4316     }
4317     for (const parser::EquivalenceObject &object : *set) {
4318       const auto &designator{object.v.value()};
4319       // The designator was not resolved when it was encountered so do it now.
4320       // AnalyzeExpr causes array sections to be changed to substrings as needed
4321       Walk(designator);
4322       if (AnalyzeExpr(context(), designator)) {
4323         equivSets.AddToSet(designator);
4324       }
4325     }
4326     equivSets.FinishSet(source);
4327   }
4328   for (auto &set : equivSets.sets()) {
4329     if (!set.empty()) {
4330       currScope().add_equivalenceSet(std::move(set));
4331     }
4332   }
4333   equivalenceSets_.clear();
4334 }
4335 
4336 bool DeclarationVisitor::Pre(const parser::SaveStmt &x) {
4337   if (x.v.empty()) {
4338     saveInfo_.saveAll = currStmtSource();
4339   } else {
4340     for (const parser::SavedEntity &y : x.v) {
4341       auto kind{std::get<parser::SavedEntity::Kind>(y.t)};
4342       const auto &name{std::get<parser::Name>(y.t)};
4343       if (kind == parser::SavedEntity::Kind::Common) {
4344         MakeCommonBlockSymbol(name);
4345         AddSaveName(saveInfo_.commons, name.source);
4346       } else {
4347         HandleAttributeStmt(Attr::SAVE, name);
4348       }
4349     }
4350   }
4351   return false;
4352 }
4353 
4354 void DeclarationVisitor::CheckSaveStmts() {
4355   for (const SourceName &name : saveInfo_.entities) {
4356     auto *symbol{FindInScope(currScope(), name)};
4357     if (!symbol) {
4358       // error was reported
4359     } else if (saveInfo_.saveAll) {
4360       // C889 - note that pgi, ifort, xlf do not enforce this constraint
4361       Say2(name,
4362           "Explicit SAVE of '%s' is redundant due to global SAVE statement"_err_en_US,
4363           *saveInfo_.saveAll, "Global SAVE statement"_en_US);
4364     } else if (auto msg{CheckSaveAttr(*symbol)}) {
4365       Say(name, std::move(*msg));
4366     } else {
4367       SetSaveAttr(*symbol);
4368     }
4369   }
4370   for (const SourceName &name : saveInfo_.commons) {
4371     if (auto *symbol{currScope().FindCommonBlock(name)}) {
4372       auto &objects{symbol->get<CommonBlockDetails>().objects()};
4373       if (objects.empty()) {
4374         if (currScope().kind() != Scope::Kind::Block) {
4375           Say(name,
4376               "'%s' appears as a COMMON block in a SAVE statement but not in"
4377               " a COMMON statement"_err_en_US);
4378         } else { // C1108
4379           Say(name,
4380               "SAVE statement in BLOCK construct may not contain a"
4381               " common block name '%s'"_err_en_US);
4382         }
4383       } else {
4384         for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
4385           SetSaveAttr(*object);
4386         }
4387       }
4388     }
4389   }
4390   if (saveInfo_.saveAll) {
4391     // Apply SAVE attribute to applicable symbols
4392     for (auto pair : currScope()) {
4393       auto &symbol{*pair.second};
4394       if (!CheckSaveAttr(symbol)) {
4395         SetSaveAttr(symbol);
4396       }
4397     }
4398   }
4399   saveInfo_ = {};
4400 }
4401 
4402 // If SAVE attribute can't be set on symbol, return error message.
4403 std::optional<MessageFixedText> DeclarationVisitor::CheckSaveAttr(
4404     const Symbol &symbol) {
4405   if (symbol.IsDummy()) {
4406     return "SAVE attribute may not be applied to dummy argument '%s'"_err_en_US;
4407   } else if (symbol.IsFuncResult()) {
4408     return "SAVE attribute may not be applied to function result '%s'"_err_en_US;
4409   } else if (symbol.has<ProcEntityDetails>() &&
4410       !symbol.attrs().test(Attr::POINTER)) {
4411     return "Procedure '%s' with SAVE attribute must also have POINTER attribute"_err_en_US;
4412   } else {
4413     return std::nullopt;
4414   }
4415 }
4416 
4417 // Instead of setting SAVE attribute, record the name in saveInfo_.entities.
4418 Attrs DeclarationVisitor::HandleSaveName(const SourceName &name, Attrs attrs) {
4419   if (attrs.test(Attr::SAVE)) {
4420     attrs.set(Attr::SAVE, false);
4421     AddSaveName(saveInfo_.entities, name);
4422   }
4423   return attrs;
4424 }
4425 
4426 // Record a name in a set of those to be saved.
4427 void DeclarationVisitor::AddSaveName(
4428     std::set<SourceName> &set, const SourceName &name) {
4429   auto pair{set.insert(name)};
4430   if (!pair.second) {
4431     Say2(name, "SAVE attribute was already specified on '%s'"_err_en_US,
4432         *pair.first, "Previous specification of SAVE attribute"_en_US);
4433   }
4434 }
4435 
4436 // Set the SAVE attribute on symbol unless it is implicitly saved anyway.
4437 void DeclarationVisitor::SetSaveAttr(Symbol &symbol) {
4438   if (!IsSaved(symbol)) {
4439     symbol.attrs().set(Attr::SAVE);
4440   }
4441 }
4442 
4443 // Check types of common block objects, now that they are known.
4444 void DeclarationVisitor::CheckCommonBlocks() {
4445   // check for empty common blocks
4446   for (const auto &pair : currScope().commonBlocks()) {
4447     const auto &symbol{*pair.second};
4448     if (symbol.get<CommonBlockDetails>().objects().empty() &&
4449         symbol.attrs().test(Attr::BIND_C)) {
4450       Say(symbol.name(),
4451           "'%s' appears as a COMMON block in a BIND statement but not in"
4452           " a COMMON statement"_err_en_US);
4453     }
4454   }
4455   // check objects in common blocks
4456   for (const auto &name : commonBlockInfo_.names) {
4457     const auto *symbol{currScope().FindSymbol(name)};
4458     if (!symbol) {
4459       continue;
4460     }
4461     const auto &attrs{symbol->attrs()};
4462     if (attrs.test(Attr::ALLOCATABLE)) {
4463       Say(name,
4464           "ALLOCATABLE object '%s' may not appear in a COMMON block"_err_en_US);
4465     } else if (attrs.test(Attr::BIND_C)) {
4466       Say(name,
4467           "Variable '%s' with BIND attribute may not appear in a COMMON block"_err_en_US);
4468     } else if (symbol->IsDummy()) {
4469       Say(name,
4470           "Dummy argument '%s' may not appear in a COMMON block"_err_en_US);
4471     } else if (symbol->IsFuncResult()) {
4472       Say(name,
4473           "Function result '%s' may not appear in a COMMON block"_err_en_US);
4474     } else if (const DeclTypeSpec * type{symbol->GetType()}) {
4475       if (type->category() == DeclTypeSpec::ClassStar) {
4476         Say(name,
4477             "Unlimited polymorphic pointer '%s' may not appear in a COMMON block"_err_en_US);
4478       } else if (const auto *derived{type->AsDerived()}) {
4479         auto &typeSymbol{derived->typeSymbol()};
4480         if (!typeSymbol.attrs().test(Attr::BIND_C) &&
4481             !typeSymbol.get<DerivedTypeDetails>().sequence()) {
4482           Say(name,
4483               "Derived type '%s' in COMMON block must have the BIND or"
4484               " SEQUENCE attribute"_err_en_US);
4485         }
4486         CheckCommonBlockDerivedType(name, typeSymbol);
4487       }
4488     }
4489   }
4490   commonBlockInfo_ = {};
4491 }
4492 
4493 Symbol &DeclarationVisitor::MakeCommonBlockSymbol(const parser::Name &name) {
4494   return Resolve(name, currScope().MakeCommonBlock(name.source));
4495 }
4496 
4497 bool DeclarationVisitor::NameIsKnownOrIntrinsic(const parser::Name &name) {
4498   return FindSymbol(name) || HandleUnrestrictedSpecificIntrinsicFunction(name);
4499 }
4500 
4501 // Check if this derived type can be in a COMMON block.
4502 void DeclarationVisitor::CheckCommonBlockDerivedType(
4503     const SourceName &name, const Symbol &typeSymbol) {
4504   if (const auto *scope{typeSymbol.scope()}) {
4505     for (const auto &pair : *scope) {
4506       const Symbol &component{*pair.second};
4507       if (component.attrs().test(Attr::ALLOCATABLE)) {
4508         Say2(name,
4509             "Derived type variable '%s' may not appear in a COMMON block"
4510             " due to ALLOCATABLE component"_err_en_US,
4511             component.name(), "Component with ALLOCATABLE attribute"_en_US);
4512         return;
4513       }
4514       if (const auto *details{component.detailsIf<ObjectEntityDetails>()}) {
4515         if (details->init()) {
4516           Say2(name,
4517               "Derived type variable '%s' may not appear in a COMMON block"
4518               " due to component with default initialization"_err_en_US,
4519               component.name(), "Component with default initialization"_en_US);
4520           return;
4521         }
4522         if (const auto *type{details->type()}) {
4523           if (const auto *derived{type->AsDerived()}) {
4524             CheckCommonBlockDerivedType(name, derived->typeSymbol());
4525           }
4526         }
4527       }
4528     }
4529   }
4530 }
4531 
4532 bool DeclarationVisitor::HandleUnrestrictedSpecificIntrinsicFunction(
4533     const parser::Name &name) {
4534   if (auto interface{context().intrinsics().IsSpecificIntrinsicFunction(
4535           name.source.ToString())}) {
4536     // Unrestricted specific intrinsic function names (e.g., "cos")
4537     // are acceptable as procedure interfaces.
4538     Symbol &symbol{
4539         MakeSymbol(InclusiveScope(), name.source, Attrs{Attr::INTRINSIC})};
4540     if (interface->IsElemental()) {
4541       symbol.attrs().set(Attr::ELEMENTAL);
4542     }
4543     symbol.set_details(ProcEntityDetails{});
4544     Resolve(name, symbol);
4545     return true;
4546   } else {
4547     return false;
4548   }
4549 }
4550 
4551 // Checks for all locality-specs: LOCAL, LOCAL_INIT, and SHARED
4552 bool DeclarationVisitor::PassesSharedLocalityChecks(
4553     const parser::Name &name, Symbol &symbol) {
4554   if (!IsVariableName(symbol)) {
4555     SayLocalMustBeVariable(name, symbol); // C1124
4556     return false;
4557   }
4558   if (symbol.owner() == currScope()) { // C1125 and C1126
4559     SayAlreadyDeclared(name, symbol);
4560     return false;
4561   }
4562   return true;
4563 }
4564 
4565 // Checks for locality-specs LOCAL and LOCAL_INIT
4566 bool DeclarationVisitor::PassesLocalityChecks(
4567     const parser::Name &name, Symbol &symbol) {
4568   if (IsAllocatable(symbol)) { // C1128
4569     SayWithDecl(name, symbol,
4570         "ALLOCATABLE variable '%s' not allowed in a locality-spec"_err_en_US);
4571     return false;
4572   }
4573   if (IsOptional(symbol)) { // C1128
4574     SayWithDecl(name, symbol,
4575         "OPTIONAL argument '%s' not allowed in a locality-spec"_err_en_US);
4576     return false;
4577   }
4578   if (IsIntentIn(symbol)) { // C1128
4579     SayWithDecl(name, symbol,
4580         "INTENT IN argument '%s' not allowed in a locality-spec"_err_en_US);
4581     return false;
4582   }
4583   if (IsFinalizable(symbol)) { // C1128
4584     SayWithDecl(name, symbol,
4585         "Finalizable variable '%s' not allowed in a locality-spec"_err_en_US);
4586     return false;
4587   }
4588   if (IsCoarray(symbol)) { // C1128
4589     SayWithDecl(
4590         name, symbol, "Coarray '%s' not allowed in a locality-spec"_err_en_US);
4591     return false;
4592   }
4593   if (const DeclTypeSpec * type{symbol.GetType()}) {
4594     if (type->IsPolymorphic() && symbol.IsDummy() &&
4595         !IsPointer(symbol)) { // C1128
4596       SayWithDecl(name, symbol,
4597           "Nonpointer polymorphic argument '%s' not allowed in a "
4598           "locality-spec"_err_en_US);
4599       return false;
4600     }
4601   }
4602   if (IsAssumedSizeArray(symbol)) { // C1128
4603     SayWithDecl(name, symbol,
4604         "Assumed size array '%s' not allowed in a locality-spec"_err_en_US);
4605     return false;
4606   }
4607   if (std::optional<MessageFixedText> msg{
4608           WhyNotModifiable(symbol, currScope())}) {
4609     SayWithReason(name, symbol,
4610         "'%s' may not appear in a locality-spec because it is not "
4611         "definable"_err_en_US,
4612         std::move(*msg));
4613     return false;
4614   }
4615   return PassesSharedLocalityChecks(name, symbol);
4616 }
4617 
4618 Symbol &DeclarationVisitor::FindOrDeclareEnclosingEntity(
4619     const parser::Name &name) {
4620   Symbol *prev{FindSymbol(name)};
4621   if (!prev) {
4622     // Declare the name as an object in the enclosing scope so that
4623     // the name can't be repurposed there later as something else.
4624     prev = &MakeSymbol(InclusiveScope(), name.source, Attrs{});
4625     ConvertToObjectEntity(*prev);
4626     ApplyImplicitRules(*prev);
4627   }
4628   return *prev;
4629 }
4630 
4631 Symbol *DeclarationVisitor::DeclareLocalEntity(const parser::Name &name) {
4632   Symbol &prev{FindOrDeclareEnclosingEntity(name)};
4633   if (!PassesLocalityChecks(name, prev)) {
4634     return nullptr;
4635   }
4636   Symbol &symbol{MakeSymbol(name, HostAssocDetails{prev})};
4637   name.symbol = &symbol;
4638   return &symbol;
4639 }
4640 
4641 Symbol *DeclarationVisitor::DeclareStatementEntity(const parser::Name &name,
4642     const std::optional<parser::IntegerTypeSpec> &type) {
4643   const DeclTypeSpec *declTypeSpec{nullptr};
4644   if (auto *prev{FindSymbol(name)}) {
4645     if (prev->owner() == currScope()) {
4646       SayAlreadyDeclared(name, *prev);
4647       return nullptr;
4648     }
4649     name.symbol = nullptr;
4650     declTypeSpec = prev->GetType();
4651   }
4652   Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, {})};
4653   if (!symbol.has<ObjectEntityDetails>()) {
4654     return nullptr; // error was reported in DeclareEntity
4655   }
4656   if (type) {
4657     declTypeSpec = ProcessTypeSpec(*type);
4658   }
4659   if (declTypeSpec) {
4660     // Subtlety: Don't let a "*length" specifier (if any is pending) affect the
4661     // declaration of this implied DO loop control variable.
4662     auto restorer{
4663         common::ScopedSet(charInfo_.length, std::optional<ParamValue>{})};
4664     SetType(name, *declTypeSpec);
4665   } else {
4666     ApplyImplicitRules(symbol);
4667   }
4668   return Resolve(name, &symbol);
4669 }
4670 
4671 // Set the type of an entity or report an error.
4672 void DeclarationVisitor::SetType(
4673     const parser::Name &name, const DeclTypeSpec &type) {
4674   CHECK(name.symbol);
4675   auto &symbol{*name.symbol};
4676   if (charInfo_.length) { // Declaration has "*length" (R723)
4677     auto length{std::move(*charInfo_.length)};
4678     charInfo_.length.reset();
4679     if (type.category() == DeclTypeSpec::Character) {
4680       auto kind{type.characterTypeSpec().kind()};
4681       // Recurse with correct type.
4682       SetType(name,
4683           currScope().MakeCharacterType(std::move(length), std::move(kind)));
4684       return;
4685     } else {
4686       Say(name,
4687           "A length specifier cannot be used to declare the non-character entity '%s'"_err_en_US);
4688     }
4689   }
4690   auto *prevType{symbol.GetType()};
4691   if (!prevType) {
4692     symbol.SetType(type);
4693   } else if (symbol.has<UseDetails>()) {
4694     // error recovery case, redeclaration of use-associated name
4695   } else if (!symbol.test(Symbol::Flag::Implicit)) {
4696     SayWithDecl(
4697         name, symbol, "The type of '%s' has already been declared"_err_en_US);
4698   } else if (type != *prevType) {
4699     SayWithDecl(name, symbol,
4700         "The type of '%s' has already been implicitly declared"_err_en_US);
4701   } else {
4702     symbol.set(Symbol::Flag::Implicit, false);
4703   }
4704 }
4705 
4706 std::optional<DerivedTypeSpec> DeclarationVisitor::ResolveDerivedType(
4707     const parser::Name &name) {
4708   Symbol *symbol{FindSymbol(name)};
4709   if (!symbol || symbol->has<UnknownDetails>()) {
4710     if (allowForwardReferenceToDerivedType()) {
4711       if (!symbol) {
4712         symbol = &MakeSymbol(InclusiveScope(), name.source, Attrs{});
4713         Resolve(name, *symbol);
4714       };
4715       DerivedTypeDetails details;
4716       details.set_isForwardReferenced();
4717       symbol->set_details(std::move(details));
4718     } else { // C732
4719       Say(name, "Derived type '%s' not found"_err_en_US);
4720       return std::nullopt;
4721     }
4722   }
4723   if (CheckUseError(name)) {
4724     return std::nullopt;
4725   }
4726   symbol = &symbol->GetUltimate();
4727   if (auto *details{symbol->detailsIf<GenericDetails>()}) {
4728     if (details->derivedType()) {
4729       symbol = details->derivedType();
4730     }
4731   }
4732   if (symbol->has<DerivedTypeDetails>()) {
4733     return DerivedTypeSpec{name.source, *symbol};
4734   } else {
4735     Say(name, "'%s' is not a derived type"_err_en_US);
4736     return std::nullopt;
4737   }
4738 }
4739 
4740 std::optional<DerivedTypeSpec> DeclarationVisitor::ResolveExtendsType(
4741     const parser::Name &typeName, const parser::Name *extendsName) {
4742   if (!extendsName) {
4743     return std::nullopt;
4744   } else if (typeName.source == extendsName->source) {
4745     Say(extendsName->source,
4746         "Derived type '%s' cannot extend itself"_err_en_US);
4747     return std::nullopt;
4748   } else {
4749     return ResolveDerivedType(*extendsName);
4750   }
4751 }
4752 
4753 Symbol *DeclarationVisitor::NoteInterfaceName(const parser::Name &name) {
4754   // The symbol is checked later by CheckExplicitInterface() and
4755   // CheckBindings().  It can be a forward reference.
4756   if (!NameIsKnownOrIntrinsic(name)) {
4757     Symbol &symbol{MakeSymbol(InclusiveScope(), name.source, Attrs{})};
4758     Resolve(name, symbol);
4759   }
4760   return name.symbol;
4761 }
4762 
4763 void DeclarationVisitor::CheckExplicitInterface(const parser::Name &name) {
4764   if (const Symbol * symbol{name.symbol}) {
4765     if (!symbol->HasExplicitInterface()) {
4766       Say(name,
4767           "'%s' must be an abstract interface or a procedure with "
4768           "an explicit interface"_err_en_US,
4769           symbol->name());
4770     }
4771   }
4772 }
4773 
4774 // Create a symbol for a type parameter, component, or procedure binding in
4775 // the current derived type scope. Return false on error.
4776 Symbol *DeclarationVisitor::MakeTypeSymbol(
4777     const parser::Name &name, Details &&details) {
4778   return Resolve(name, MakeTypeSymbol(name.source, std::move(details)));
4779 }
4780 Symbol *DeclarationVisitor::MakeTypeSymbol(
4781     const SourceName &name, Details &&details) {
4782   Scope &derivedType{currScope()};
4783   CHECK(derivedType.IsDerivedType());
4784   if (auto *symbol{FindInScope(derivedType, name)}) { // C742
4785     Say2(name,
4786         "Type parameter, component, or procedure binding '%s'"
4787         " already defined in this type"_err_en_US,
4788         *symbol, "Previous definition of '%s'"_en_US);
4789     return nullptr;
4790   } else {
4791     auto attrs{GetAttrs()};
4792     // Apply binding-private-stmt if present and this is a procedure binding
4793     if (derivedTypeInfo_.privateBindings &&
4794         !attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE}) &&
4795         std::holds_alternative<ProcBindingDetails>(details)) {
4796       attrs.set(Attr::PRIVATE);
4797     }
4798     Symbol &result{MakeSymbol(name, attrs, std::move(details))};
4799     if (result.has<TypeParamDetails>()) {
4800       derivedType.symbol()->get<DerivedTypeDetails>().add_paramDecl(result);
4801     }
4802     return &result;
4803   }
4804 }
4805 
4806 // Return true if it is ok to declare this component in the current scope.
4807 // Otherwise, emit an error and return false.
4808 bool DeclarationVisitor::OkToAddComponent(
4809     const parser::Name &name, const Symbol *extends) {
4810   for (const Scope *scope{&currScope()}; scope;) {
4811     CHECK(scope->IsDerivedType());
4812     if (auto *prev{FindInScope(*scope, name)}) {
4813       auto msg{""_en_US};
4814       if (extends) {
4815         msg = "Type cannot be extended as it has a component named"
4816               " '%s'"_err_en_US;
4817       } else if (prev->test(Symbol::Flag::ParentComp)) {
4818         msg = "'%s' is a parent type of this type and so cannot be"
4819               " a component"_err_en_US;
4820       } else if (scope != &currScope()) {
4821         msg = "Component '%s' is already declared in a parent of this"
4822               " derived type"_err_en_US;
4823       } else {
4824         msg = "Component '%s' is already declared in this"
4825               " derived type"_err_en_US;
4826       }
4827       Say2(name, std::move(msg), *prev, "Previous declaration of '%s'"_en_US);
4828       return false;
4829     }
4830     if (scope == &currScope() && extends) {
4831       // The parent component has not yet been added to the scope.
4832       scope = extends->scope();
4833     } else {
4834       scope = scope->GetDerivedTypeParent();
4835     }
4836   }
4837   return true;
4838 }
4839 
4840 ParamValue DeclarationVisitor::GetParamValue(
4841     const parser::TypeParamValue &x, common::TypeParamAttr attr) {
4842   return std::visit(
4843       common::visitors{
4844           [=](const parser::ScalarIntExpr &x) { // C704
4845             return ParamValue{EvaluateIntExpr(x), attr};
4846           },
4847           [=](const parser::Star &) { return ParamValue::Assumed(attr); },
4848           [=](const parser::TypeParamValue::Deferred &) {
4849             return ParamValue::Deferred(attr);
4850           },
4851       },
4852       x.u);
4853 }
4854 
4855 // ConstructVisitor implementation
4856 
4857 void ConstructVisitor::ResolveIndexName(
4858     const parser::ConcurrentControl &control) {
4859   const parser::Name &name{std::get<parser::Name>(control.t)};
4860   auto *prev{FindSymbol(name)};
4861   if (prev) {
4862     if (prev->owner().kind() == Scope::Kind::Forall ||
4863         prev->owner() == currScope()) {
4864       SayAlreadyDeclared(name, *prev);
4865       return;
4866     }
4867     name.symbol = nullptr;
4868   }
4869   auto &symbol{DeclareObjectEntity(name, {})};
4870 
4871   if (symbol.GetType()) {
4872     // type came from explicit type-spec
4873   } else if (!prev) {
4874     ApplyImplicitRules(symbol);
4875   } else if (!prev->has<ObjectEntityDetails>() && !prev->has<EntityDetails>()) {
4876     Say2(name, "Index name '%s' conflicts with existing identifier"_err_en_US,
4877         *prev, "Previous declaration of '%s'"_en_US);
4878     return;
4879   } else {
4880     if (const auto *type{prev->GetType()}) {
4881       symbol.SetType(*type);
4882     }
4883     if (prev->IsObjectArray()) {
4884       SayWithDecl(name, *prev, "Index variable '%s' is not scalar"_err_en_US);
4885       return;
4886     }
4887   }
4888   EvaluateExpr(parser::Scalar{parser::Integer{common::Clone(name)}});
4889 }
4890 
4891 // We need to make sure that all of the index-names get declared before the
4892 // expressions in the loop control are evaluated so that references to the
4893 // index-names in the expressions are correctly detected.
4894 bool ConstructVisitor::Pre(const parser::ConcurrentHeader &header) {
4895   BeginDeclTypeSpec();
4896   Walk(std::get<std::optional<parser::IntegerTypeSpec>>(header.t));
4897   const auto &controls{
4898       std::get<std::list<parser::ConcurrentControl>>(header.t)};
4899   for (const auto &control : controls) {
4900     ResolveIndexName(control);
4901   }
4902   Walk(controls);
4903   Walk(std::get<std::optional<parser::ScalarLogicalExpr>>(header.t));
4904   EndDeclTypeSpec();
4905   return false;
4906 }
4907 
4908 bool ConstructVisitor::Pre(const parser::LocalitySpec::Local &x) {
4909   for (auto &name : x.v) {
4910     if (auto *symbol{DeclareLocalEntity(name)}) {
4911       symbol->set(Symbol::Flag::LocalityLocal);
4912     }
4913   }
4914   return false;
4915 }
4916 
4917 bool ConstructVisitor::Pre(const parser::LocalitySpec::LocalInit &x) {
4918   for (auto &name : x.v) {
4919     if (auto *symbol{DeclareLocalEntity(name)}) {
4920       symbol->set(Symbol::Flag::LocalityLocalInit);
4921     }
4922   }
4923   return false;
4924 }
4925 
4926 bool ConstructVisitor::Pre(const parser::LocalitySpec::Shared &x) {
4927   for (const auto &name : x.v) {
4928     if (!FindSymbol(name)) {
4929       Say(name, "Variable '%s' with SHARED locality implicitly declared"_en_US);
4930     }
4931     Symbol &prev{FindOrDeclareEnclosingEntity(name)};
4932     if (PassesSharedLocalityChecks(name, prev)) {
4933       auto &symbol{MakeSymbol(name, HostAssocDetails{prev})};
4934       symbol.set(Symbol::Flag::LocalityShared);
4935       name.symbol = &symbol; // override resolution to parent
4936     }
4937   }
4938   return false;
4939 }
4940 
4941 bool ConstructVisitor::Pre(const parser::AcSpec &x) {
4942   ProcessTypeSpec(x.type);
4943   PushScope(Scope::Kind::ImpliedDos, nullptr);
4944   Walk(x.values);
4945   PopScope();
4946   return false;
4947 }
4948 
4949 bool ConstructVisitor::Pre(const parser::AcImpliedDo &x) {
4950   auto &values{std::get<std::list<parser::AcValue>>(x.t)};
4951   auto &control{std::get<parser::AcImpliedDoControl>(x.t)};
4952   auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(control.t)};
4953   auto &bounds{std::get<parser::AcImpliedDoControl::Bounds>(control.t)};
4954   DeclareStatementEntity(bounds.name.thing.thing, type);
4955   Walk(bounds);
4956   Walk(values);
4957   return false;
4958 }
4959 
4960 bool ConstructVisitor::Pre(const parser::DataImpliedDo &x) {
4961   auto &objects{std::get<std::list<parser::DataIDoObject>>(x.t)};
4962   auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(x.t)};
4963   auto &bounds{std::get<parser::DataImpliedDo::Bounds>(x.t)};
4964   DeclareStatementEntity(bounds.name.thing.thing, type);
4965   Walk(bounds);
4966   Walk(objects);
4967   return false;
4968 }
4969 
4970 bool ConstructVisitor::Pre(const parser::DataStmtObject &x) {
4971   std::visit(common::visitors{
4972                  [&](const Indirection<parser::Variable> &y) {
4973                    Walk(y.value());
4974                    if (const auto *designator{
4975                            std::get_if<Indirection<parser::Designator>>(
4976                                &y.value().u)}) {
4977                      if (const parser::Name *
4978                          name{ResolveDesignator(designator->value())}) {
4979                        if (name->symbol) {
4980                          name->symbol->set(Symbol::Flag::InDataStmt);
4981                        }
4982                      }
4983                      // TODO check C874 - C881
4984                    } else {
4985                      // TODO report C875 error: variable is not a designator
4986                      // here?
4987                    }
4988                  },
4989                  [&](const parser::DataImpliedDo &y) {
4990                    PushScope(Scope::Kind::ImpliedDos, nullptr);
4991                    Walk(y);
4992                    PopScope();
4993                  },
4994              },
4995       x.u);
4996   return false;
4997 }
4998 
4999 bool ConstructVisitor::Pre(const parser::DataStmtValue &x) {
5000   const auto &data{std::get<parser::DataStmtConstant>(x.t)};
5001   auto &mutableData{const_cast<parser::DataStmtConstant &>(data)};
5002   if (auto *elem{parser::Unwrap<parser::ArrayElement>(mutableData)}) {
5003     if (const auto *name{std::get_if<parser::Name>(&elem->base.u)}) {
5004       if (const Symbol * symbol{FindSymbol(*name)}) {
5005         if (const Symbol * ultimate{GetAssociationRoot(*symbol)}) {
5006           if (ultimate->has<DerivedTypeDetails>()) {
5007             mutableData.u = elem->ConvertToStructureConstructor(
5008                 DerivedTypeSpec{name->source, *ultimate});
5009           }
5010         }
5011       }
5012     }
5013   }
5014   return true;
5015 }
5016 
5017 bool ConstructVisitor::Pre(const parser::DoConstruct &x) {
5018   if (x.IsDoConcurrent()) {
5019     PushScope(Scope::Kind::Block, nullptr);
5020   }
5021   return true;
5022 }
5023 void ConstructVisitor::Post(const parser::DoConstruct &x) {
5024   if (x.IsDoConcurrent()) {
5025     PopScope();
5026   }
5027 }
5028 
5029 bool ConstructVisitor::Pre(const parser::ForallConstruct &) {
5030   PushScope(Scope::Kind::Forall, nullptr);
5031   return true;
5032 }
5033 void ConstructVisitor::Post(const parser::ForallConstruct &) { PopScope(); }
5034 bool ConstructVisitor::Pre(const parser::ForallStmt &) {
5035   PushScope(Scope::Kind::Forall, nullptr);
5036   return true;
5037 }
5038 void ConstructVisitor::Post(const parser::ForallStmt &) { PopScope(); }
5039 
5040 bool ConstructVisitor::Pre(const parser::BlockStmt &x) {
5041   CheckDef(x.v);
5042   PushScope(Scope::Kind::Block, nullptr);
5043   return false;
5044 }
5045 bool ConstructVisitor::Pre(const parser::EndBlockStmt &x) {
5046   PopScope();
5047   CheckRef(x.v);
5048   return false;
5049 }
5050 
5051 void ConstructVisitor::Post(const parser::Selector &x) {
5052   GetCurrentAssociation().selector = ResolveSelector(x);
5053 }
5054 
5055 bool ConstructVisitor::Pre(const parser::AssociateStmt &x) {
5056   CheckDef(x.t);
5057   PushScope(Scope::Kind::Block, nullptr);
5058   PushAssociation();
5059   return true;
5060 }
5061 void ConstructVisitor::Post(const parser::EndAssociateStmt &x) {
5062   PopAssociation();
5063   PopScope();
5064   CheckRef(x.v);
5065 }
5066 
5067 void ConstructVisitor::Post(const parser::Association &x) {
5068   const auto &name{std::get<parser::Name>(x.t)};
5069   GetCurrentAssociation().name = &name;
5070   if (auto *symbol{MakeAssocEntity()}) {
5071     SetTypeFromAssociation(*symbol);
5072     SetAttrsFromAssociation(*symbol);
5073   }
5074   GetCurrentAssociation() = {}; // clean for further parser::Association.
5075 }
5076 
5077 bool ConstructVisitor::Pre(const parser::ChangeTeamStmt &x) {
5078   CheckDef(x.t);
5079   PushScope(Scope::Kind::Block, nullptr);
5080   PushAssociation();
5081   return true;
5082 }
5083 
5084 void ConstructVisitor::Post(const parser::CoarrayAssociation &x) {
5085   const auto &decl{std::get<parser::CodimensionDecl>(x.t)};
5086   const auto &name{std::get<parser::Name>(decl.t)};
5087   if (auto *symbol{FindInScope(currScope(), name)}) {
5088     const auto &selector{std::get<parser::Selector>(x.t)};
5089     if (auto sel{ResolveSelector(selector)}) {
5090       const Symbol *whole{UnwrapWholeSymbolDataRef(sel.expr)};
5091       if (!whole || whole->Corank() == 0) {
5092         Say(sel.source, // C1116
5093             "Selector in coarray association must name a coarray"_err_en_US);
5094       } else if (auto dynType{sel.expr->GetType()}) {
5095         if (!symbol->GetType()) {
5096           symbol->SetType(ToDeclTypeSpec(std::move(*dynType)));
5097         }
5098       }
5099     }
5100   }
5101 }
5102 
5103 void ConstructVisitor::Post(const parser::EndChangeTeamStmt &x) {
5104   PopAssociation();
5105   PopScope();
5106   CheckRef(x.t);
5107 }
5108 
5109 bool ConstructVisitor::Pre(const parser::SelectTypeConstruct &) {
5110   PushAssociation();
5111   return true;
5112 }
5113 
5114 void ConstructVisitor::Post(const parser::SelectTypeConstruct &) {
5115   PopAssociation();
5116 }
5117 
5118 void ConstructVisitor::Post(const parser::SelectTypeStmt &x) {
5119   auto &association{GetCurrentAssociation()};
5120   if (const std::optional<parser::Name> &name{std::get<1>(x.t)}) {
5121     // This isn't a name in the current scope, it is in each TypeGuardStmt
5122     MakePlaceholder(*name, MiscDetails::Kind::SelectTypeAssociateName);
5123     association.name = &*name;
5124   } else {
5125     if (const Symbol *
5126         whole{UnwrapWholeSymbolDataRef(association.selector.expr)}) {
5127       ConvertToObjectEntity(const_cast<Symbol &>(*whole));
5128       if (!IsVariableName(*whole)) {
5129         Say(association.selector.source, // C901
5130             "Selector is not a variable"_err_en_US);
5131         association = {};
5132       }
5133     } else {
5134       Say(association.selector.source, // C1157
5135           "Selector is not a named variable: 'associate-name =>' is required"_err_en_US);
5136       association = {};
5137     }
5138   }
5139 }
5140 
5141 void ConstructVisitor::Post(const parser::SelectRankStmt &x) {
5142   auto &association{GetCurrentAssociation()};
5143   if (const std::optional<parser::Name> &name{std::get<1>(x.t)}) {
5144     // This isn't a name in the current scope, it is in each SelectRankCaseStmt
5145     MakePlaceholder(*name, MiscDetails::Kind::SelectRankAssociateName);
5146     association.name = &*name;
5147   }
5148 }
5149 
5150 bool ConstructVisitor::Pre(const parser::SelectTypeConstruct::TypeCase &) {
5151   PushScope(Scope::Kind::Block, nullptr);
5152   return true;
5153 }
5154 void ConstructVisitor::Post(const parser::SelectTypeConstruct::TypeCase &) {
5155   PopScope();
5156 }
5157 
5158 bool ConstructVisitor::Pre(const parser::SelectRankConstruct::RankCase &) {
5159   PushScope(Scope::Kind::Block, nullptr);
5160   return true;
5161 }
5162 void ConstructVisitor::Post(const parser::SelectRankConstruct::RankCase &) {
5163   PopScope();
5164 }
5165 
5166 void ConstructVisitor::Post(const parser::TypeGuardStmt::Guard &x) {
5167   if (auto *symbol{MakeAssocEntity()}) {
5168     if (std::holds_alternative<parser::Default>(x.u)) {
5169       SetTypeFromAssociation(*symbol);
5170     } else if (const auto *type{GetDeclTypeSpec()}) {
5171       symbol->SetType(*type);
5172     }
5173     SetAttrsFromAssociation(*symbol);
5174   }
5175 }
5176 
5177 void ConstructVisitor::Post(const parser::SelectRankCaseStmt::Rank &x) {
5178   if (auto *symbol{MakeAssocEntity()}) {
5179     SetTypeFromAssociation(*symbol);
5180     SetAttrsFromAssociation(*symbol);
5181     if (const auto *init{std::get_if<parser::ScalarIntConstantExpr>(&x.u)}) {
5182       MaybeIntExpr expr{EvaluateIntExpr(*init)};
5183       if (auto val{evaluate::ToInt64(expr)}) {
5184         auto &details{symbol->get<AssocEntityDetails>()};
5185         details.set_rank(*val);
5186       }
5187     }
5188   }
5189 }
5190 
5191 bool ConstructVisitor::Pre(const parser::SelectRankConstruct &) {
5192   PushAssociation();
5193   return true;
5194 }
5195 
5196 void ConstructVisitor::Post(const parser::SelectRankConstruct &) {
5197   PopAssociation();
5198 }
5199 
5200 bool ConstructVisitor::CheckDef(const std::optional<parser::Name> &x) {
5201   if (x) {
5202     MakeSymbol(*x, MiscDetails{MiscDetails::Kind::ConstructName});
5203   }
5204   return true;
5205 }
5206 
5207 void ConstructVisitor::CheckRef(const std::optional<parser::Name> &x) {
5208   if (x) {
5209     // Just add an occurrence of this name; checking is done in ValidateLabels
5210     FindSymbol(*x);
5211   }
5212 }
5213 
5214 // Make a symbol representing an associating entity from current association.
5215 Symbol *ConstructVisitor::MakeAssocEntity() {
5216   Symbol *symbol{nullptr};
5217   auto &association{GetCurrentAssociation()};
5218   if (association.name) {
5219     symbol = &MakeSymbol(*association.name, UnknownDetails{});
5220     if (symbol->has<AssocEntityDetails>() && symbol->owner() == currScope()) {
5221       Say(*association.name, // C1104
5222           "The associate name '%s' is already used in this associate statement"_err_en_US);
5223       return nullptr;
5224     }
5225   } else if (const Symbol *
5226       whole{UnwrapWholeSymbolDataRef(association.selector.expr)}) {
5227     symbol = &MakeSymbol(whole->name());
5228   } else {
5229     return nullptr;
5230   }
5231   if (auto &expr{association.selector.expr}) {
5232     symbol->set_details(AssocEntityDetails{common::Clone(*expr)});
5233   } else {
5234     symbol->set_details(AssocEntityDetails{});
5235   }
5236   return symbol;
5237 }
5238 
5239 // Set the type of symbol based on the current association selector.
5240 void ConstructVisitor::SetTypeFromAssociation(Symbol &symbol) {
5241   auto &details{symbol.get<AssocEntityDetails>()};
5242   const MaybeExpr *pexpr{&details.expr()};
5243   if (!*pexpr) {
5244     pexpr = &GetCurrentAssociation().selector.expr;
5245   }
5246   if (*pexpr) {
5247     const SomeExpr &expr{**pexpr};
5248     if (std::optional<evaluate::DynamicType> type{expr.GetType()}) {
5249       if (const auto *charExpr{
5250               evaluate::UnwrapExpr<evaluate::Expr<evaluate::SomeCharacter>>(
5251                   expr)}) {
5252         symbol.SetType(ToDeclTypeSpec(std::move(*type),
5253             FoldExpr(
5254                 std::visit([](const auto &kindChar) { return kindChar.LEN(); },
5255                     charExpr->u))));
5256       } else {
5257         symbol.SetType(ToDeclTypeSpec(std::move(*type)));
5258       }
5259     } else {
5260       // BOZ literals, procedure designators, &c. are not acceptable
5261       Say(symbol.name(), "Associate name '%s' must have a type"_err_en_US);
5262     }
5263   }
5264 }
5265 
5266 // If current selector is a variable, set some of its attributes on symbol.
5267 void ConstructVisitor::SetAttrsFromAssociation(Symbol &symbol) {
5268   Attrs attrs{evaluate::GetAttrs(GetCurrentAssociation().selector.expr)};
5269   symbol.attrs() |= attrs &
5270       Attrs{Attr::TARGET, Attr::ASYNCHRONOUS, Attr::VOLATILE, Attr::CONTIGUOUS};
5271   if (attrs.test(Attr::POINTER)) {
5272     symbol.attrs().set(Attr::TARGET);
5273   }
5274 }
5275 
5276 ConstructVisitor::Selector ConstructVisitor::ResolveSelector(
5277     const parser::Selector &x) {
5278   return std::visit(common::visitors{
5279                         [&](const parser::Expr &expr) {
5280                           return Selector{expr.source, EvaluateExpr(expr)};
5281                         },
5282                         [&](const parser::Variable &var) {
5283                           return Selector{var.GetSource(), EvaluateExpr(var)};
5284                         },
5285                     },
5286       x.u);
5287 }
5288 
5289 ConstructVisitor::Association &ConstructVisitor::GetCurrentAssociation() {
5290   CHECK(!associationStack_.empty());
5291   return associationStack_.back();
5292 }
5293 
5294 void ConstructVisitor::PushAssociation() {
5295   associationStack_.emplace_back(Association{});
5296 }
5297 
5298 void ConstructVisitor::PopAssociation() {
5299   CHECK(!associationStack_.empty());
5300   associationStack_.pop_back();
5301 }
5302 
5303 const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
5304     evaluate::DynamicType &&type) {
5305   switch (type.category()) {
5306     SWITCH_COVERS_ALL_CASES
5307   case common::TypeCategory::Integer:
5308   case common::TypeCategory::Real:
5309   case common::TypeCategory::Complex:
5310     return context().MakeNumericType(type.category(), type.kind());
5311   case common::TypeCategory::Logical:
5312     return context().MakeLogicalType(type.kind());
5313   case common::TypeCategory::Derived:
5314     if (type.IsAssumedType()) {
5315       return currScope().MakeTypeStarType();
5316     } else if (type.IsUnlimitedPolymorphic()) {
5317       return currScope().MakeClassStarType();
5318     } else {
5319       return currScope().MakeDerivedType(
5320           type.IsPolymorphic() ? DeclTypeSpec::ClassDerived
5321                                : DeclTypeSpec::TypeDerived,
5322           common::Clone(type.GetDerivedTypeSpec())
5323 
5324       );
5325     }
5326   case common::TypeCategory::Character:
5327     CRASH_NO_CASE;
5328   }
5329 }
5330 
5331 const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
5332     evaluate::DynamicType &&type, MaybeSubscriptIntExpr &&length) {
5333   CHECK(type.category() == common::TypeCategory::Character);
5334   if (length) {
5335     return currScope().MakeCharacterType(
5336         ParamValue{SomeIntExpr{*std::move(length)}, common::TypeParamAttr::Len},
5337         KindExpr{type.kind()});
5338   } else {
5339     return currScope().MakeCharacterType(
5340         ParamValue::Deferred(common::TypeParamAttr::Len),
5341         KindExpr{type.kind()});
5342   }
5343 }
5344 
5345 // ResolveNamesVisitor implementation
5346 
5347 // Ensures that bare undeclared intrinsic procedure names passed as actual
5348 // arguments get recognized as being intrinsics.
5349 bool ResolveNamesVisitor::Pre(const parser::ActualArg &arg) {
5350   if (const auto *expr{std::get_if<Indirection<parser::Expr>>(&arg.u)}) {
5351     if (const auto *designator{
5352             std::get_if<Indirection<parser::Designator>>(&expr->value().u)}) {
5353       if (const auto *dataRef{
5354               std::get_if<parser::DataRef>(&designator->value().u)}) {
5355         if (const auto *name{std::get_if<parser::Name>(&dataRef->u)}) {
5356           NameIsKnownOrIntrinsic(*name);
5357         }
5358       }
5359     }
5360   }
5361   return true;
5362 }
5363 
5364 bool ResolveNamesVisitor::Pre(const parser::FunctionReference &x) {
5365   HandleCall(Symbol::Flag::Function, x.v);
5366   return false;
5367 }
5368 bool ResolveNamesVisitor::Pre(const parser::CallStmt &x) {
5369   HandleCall(Symbol::Flag::Subroutine, x.v);
5370   return false;
5371 }
5372 
5373 bool ResolveNamesVisitor::Pre(const parser::ImportStmt &x) {
5374   auto &scope{currScope()};
5375   // Check C896 and C899: where IMPORT statements are allowed
5376   switch (scope.kind()) {
5377   case Scope::Kind::Module:
5378     if (scope.IsModule()) {
5379       Say("IMPORT is not allowed in a module scoping unit"_err_en_US);
5380       return false;
5381     } else if (x.kind == common::ImportKind::None) {
5382       Say("IMPORT,NONE is not allowed in a submodule scoping unit"_err_en_US);
5383       return false;
5384     }
5385     break;
5386   case Scope::Kind::MainProgram:
5387     Say("IMPORT is not allowed in a main program scoping unit"_err_en_US);
5388     return false;
5389   case Scope::Kind::Subprogram:
5390     if (scope.parent().IsGlobal()) {
5391       Say("IMPORT is not allowed in an external subprogram scoping unit"_err_en_US);
5392       return false;
5393     }
5394     break;
5395   case Scope::Kind::BlockData: // C1415 (in part)
5396     Say("IMPORT is not allowed in a BLOCK DATA subprogram"_err_en_US);
5397     return false;
5398   default:;
5399   }
5400   if (auto error{scope.SetImportKind(x.kind)}) {
5401     Say(std::move(*error));
5402   }
5403   for (auto &name : x.names) {
5404     if (FindSymbol(scope.parent(), name)) {
5405       scope.add_importName(name.source);
5406     } else {
5407       Say(name, "'%s' not found in host scope"_err_en_US);
5408     }
5409   }
5410   prevImportStmt_ = currStmtSource();
5411   return false;
5412 }
5413 
5414 const parser::Name *DeclarationVisitor::ResolveStructureComponent(
5415     const parser::StructureComponent &x) {
5416   return FindComponent(ResolveDataRef(x.base), x.component);
5417 }
5418 
5419 const parser::Name *DeclarationVisitor::ResolveDesignator(
5420     const parser::Designator &x) {
5421   return std::visit(
5422       common::visitors{
5423           [&](const parser::DataRef &x) { return ResolveDataRef(x); },
5424           [&](const parser::Substring &x) {
5425             return ResolveDataRef(std::get<parser::DataRef>(x.t));
5426           },
5427       },
5428       x.u);
5429 }
5430 
5431 const parser::Name *DeclarationVisitor::ResolveDataRef(
5432     const parser::DataRef &x) {
5433   return std::visit(
5434       common::visitors{
5435           [=](const parser::Name &y) { return ResolveName(y); },
5436           [=](const Indirection<parser::StructureComponent> &y) {
5437             return ResolveStructureComponent(y.value());
5438           },
5439           [&](const Indirection<parser::ArrayElement> &y) {
5440             Walk(y.value().subscripts);
5441             return ResolveDataRef(y.value().base);
5442           },
5443           [&](const Indirection<parser::CoindexedNamedObject> &y) {
5444             Walk(y.value().imageSelector);
5445             return ResolveDataRef(y.value().base);
5446           },
5447       },
5448       x.u);
5449 }
5450 
5451 const parser::Name *DeclarationVisitor::ResolveVariable(
5452     const parser::Variable &x) {
5453   return std::visit(
5454       common::visitors{
5455           [&](const Indirection<parser::Designator> &y) {
5456             return ResolveDesignator(y.value());
5457           },
5458           [&](const Indirection<parser::FunctionReference> &y) {
5459             const auto &proc{
5460                 std::get<parser::ProcedureDesignator>(y.value().v.t)};
5461             return std::visit(common::visitors{
5462                                   [&](const parser::Name &z) { return &z; },
5463                                   [&](const parser::ProcComponentRef &z) {
5464                                     return ResolveStructureComponent(z.v.thing);
5465                                   },
5466                               },
5467                 proc.u);
5468           },
5469       },
5470       x.u);
5471 }
5472 
5473 // If implicit types are allowed, ensure name is in the symbol table.
5474 // Otherwise, report an error if it hasn't been declared.
5475 const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
5476   if (Symbol * symbol{FindSymbol(name)}) {
5477     if (CheckUseError(name)) {
5478       return nullptr; // reported an error
5479     }
5480     if (symbol->IsDummy() ||
5481         (!symbol->GetType() && FindCommonBlockContaining(*symbol))) {
5482       ConvertToObjectEntity(*symbol);
5483       ApplyImplicitRules(*symbol);
5484     }
5485     return &name;
5486   }
5487   if (isImplicitNoneType()) {
5488     Say(name, "No explicit type declared for '%s'"_err_en_US);
5489     return nullptr;
5490   }
5491   // Create the symbol then ensure it is accessible
5492   MakeSymbol(InclusiveScope(), name.source, Attrs{});
5493   auto *symbol{FindSymbol(name)};
5494   if (!symbol) {
5495     Say(name,
5496         "'%s' from host scoping unit is not accessible due to IMPORT"_err_en_US);
5497     return nullptr;
5498   }
5499   ConvertToObjectEntity(*symbol);
5500   ApplyImplicitRules(*symbol);
5501   return &name;
5502 }
5503 
5504 // base is a part-ref of a derived type; find the named component in its type.
5505 // Also handles intrinsic type parameter inquiries (%kind, %len) and
5506 // COMPLEX component references (%re, %im).
5507 const parser::Name *DeclarationVisitor::FindComponent(
5508     const parser::Name *base, const parser::Name &component) {
5509   if (!base || !base->symbol) {
5510     return nullptr;
5511   }
5512   auto &symbol{base->symbol->GetUltimate()};
5513   if (!symbol.has<AssocEntityDetails>() && !ConvertToObjectEntity(symbol)) {
5514     SayWithDecl(*base, symbol,
5515         "'%s' is an invalid base for a component reference"_err_en_US);
5516     return nullptr;
5517   }
5518   auto *type{symbol.GetType()};
5519   if (!type) {
5520     return nullptr; // should have already reported error
5521   }
5522   if (const IntrinsicTypeSpec * intrinsic{type->AsIntrinsic()}) {
5523     auto name{component.ToString()};
5524     auto category{intrinsic->category()};
5525     MiscDetails::Kind miscKind{MiscDetails::Kind::None};
5526     if (name == "kind") {
5527       miscKind = MiscDetails::Kind::KindParamInquiry;
5528     } else if (category == TypeCategory::Character) {
5529       if (name == "len") {
5530         miscKind = MiscDetails::Kind::LenParamInquiry;
5531       }
5532     } else if (category == TypeCategory::Complex) {
5533       if (name == "re") {
5534         miscKind = MiscDetails::Kind::ComplexPartRe;
5535       } else if (name == "im") {
5536         miscKind = MiscDetails::Kind::ComplexPartIm;
5537       }
5538     }
5539     if (miscKind != MiscDetails::Kind::None) {
5540       MakePlaceholder(component, miscKind);
5541       return nullptr;
5542     }
5543   } else if (const DerivedTypeSpec * derived{type->AsDerived()}) {
5544     if (const Scope * scope{derived->scope()}) {
5545       if (Resolve(component, scope->FindComponent(component.source))) {
5546         if (auto msg{
5547                 CheckAccessibleComponent(currScope(), *component.symbol)}) {
5548           context().Say(component.source, *msg);
5549         }
5550         return &component;
5551       } else {
5552         SayDerivedType(component.source,
5553             "Component '%s' not found in derived type '%s'"_err_en_US, *scope);
5554       }
5555     }
5556     return nullptr;
5557   }
5558   if (symbol.test(Symbol::Flag::Implicit)) {
5559     Say(*base,
5560         "'%s' is not an object of derived type; it is implicitly typed"_err_en_US);
5561   } else {
5562     SayWithDecl(
5563         *base, symbol, "'%s' is not an object of derived type"_err_en_US);
5564   }
5565   return nullptr;
5566 }
5567 
5568 // C764, C765
5569 void DeclarationVisitor::CheckInitialDataTarget(
5570     const Symbol &pointer, const SomeExpr &expr, SourceName source) {
5571   auto &messages{GetFoldingContext().messages()};
5572   auto restorer{messages.SetLocation(source)};
5573   if (!evaluate::IsInitialDataTarget(expr, &messages)) {
5574     Say(source,
5575         "Pointer '%s' cannot be initialized with a reference to a designator with non-constant subscripts"_err_en_US,
5576         pointer.name());
5577     return;
5578   }
5579   if (pointer.Rank() != expr.Rank()) {
5580     Say(source,
5581         "Pointer '%s' of rank %d cannot be initialized with a target of different rank (%d)"_err_en_US,
5582         pointer.name(), pointer.Rank(), expr.Rank());
5583     return;
5584   }
5585   // TODO: check type compatibility
5586   // TODO: check non-deferred type parameter values
5587   // TODO: check contiguity if pointer is CONTIGUOUS
5588 }
5589 
5590 void DeclarationVisitor::CheckInitialProcTarget(
5591     const Symbol &pointer, const parser::Name &target, SourceName source) {
5592   // C1519 - must be nonelemental external or module procedure,
5593   // or an unrestricted specific intrinsic function.
5594   if (const Symbol * targetSym{target.symbol}) {
5595     const Symbol &ultimate{targetSym->GetUltimate()};
5596     if (ultimate.attrs().test(Attr::INTRINSIC)) {
5597     } else if (!ultimate.attrs().test(Attr::EXTERNAL) &&
5598         ultimate.owner().kind() != Scope::Kind::Module) {
5599       Say(source,
5600           "Procedure pointer '%s' initializer '%s' is neither "
5601           "an external nor a module procedure"_err_en_US,
5602           pointer.name(), ultimate.name());
5603     } else if (ultimate.attrs().test(Attr::ELEMENTAL)) {
5604       Say(source,
5605           "Procedure pointer '%s' cannot be initialized with the "
5606           "elemental procedure '%s"_err_en_US,
5607           pointer.name(), ultimate.name());
5608     } else {
5609       // TODO: Check the "shalls" in the 15.4.3.6 paragraphs 7-10.
5610     }
5611   }
5612 }
5613 
5614 void DeclarationVisitor::Initialization(const parser::Name &name,
5615     const parser::Initialization &init, bool inComponentDecl) {
5616   if (!name.symbol) {
5617     return;
5618   }
5619   if (std::holds_alternative<parser::InitialDataTarget>(init.u)) {
5620     // Defer analysis to the end of the specification parts so that forward
5621     // references work better.
5622     return;
5623   }
5624   // Traversal of the initializer was deferred to here so that the
5625   // symbol being declared can be available for use in the expression, e.g.:
5626   //   real, parameter :: x = tiny(x)
5627   Walk(init.u);
5628   Symbol &ultimate{name.symbol->GetUltimate()};
5629   if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
5630     // TODO: check C762 - all bounds and type parameters of component
5631     // are colons or constant expressions if component is initialized
5632     bool isPointer{false};
5633     std::visit(
5634         common::visitors{
5635             [&](const parser::ConstantExpr &expr) {
5636               if (inComponentDecl) {
5637                 // Can't convert to type of component, which might not yet
5638                 // be known; that's done later during instantiation.
5639                 if (MaybeExpr value{EvaluateExpr(expr)}) {
5640                   details->set_init(std::move(*value));
5641                 }
5642               } else {
5643                 if (MaybeExpr folded{EvaluateConvertedExpr(
5644                         ultimate, expr, expr.thing.value().source)}) {
5645                   details->set_init(std::move(*folded));
5646                 }
5647               }
5648             },
5649             [&](const parser::NullInit &) {
5650               isPointer = true;
5651               details->set_init(SomeExpr{evaluate::NullPointer{}});
5652             },
5653             [&](const parser::InitialDataTarget &initExpr) {
5654               isPointer = true;
5655               if (MaybeExpr expr{EvaluateExpr(initExpr)}) {
5656                 CheckInitialDataTarget(
5657                     ultimate, *expr, initExpr.value().source);
5658                 details->set_init(std::move(*expr));
5659               }
5660             },
5661             [&](const std::list<Indirection<parser::DataStmtValue>> &) {
5662               if (inComponentDecl) {
5663                 Say(name,
5664                     "Component '%s' initialized with DATA statement values"_err_en_US);
5665               } else {
5666                 // TODO - DATA statements and DATA-like initialization extension
5667               }
5668             },
5669         },
5670         init.u);
5671     if (isPointer) {
5672       if (!IsPointer(ultimate)) {
5673         Say(name,
5674             "Non-pointer component '%s' initialized with pointer target"_err_en_US);
5675       }
5676     } else {
5677       if (IsPointer(ultimate)) {
5678         Say(name,
5679             "Object pointer component '%s' initialized with non-pointer expression"_err_en_US);
5680       } else if (IsAllocatable(ultimate)) {
5681         Say(name, "Allocatable component '%s' cannot be initialized"_err_en_US);
5682       }
5683     }
5684   }
5685 }
5686 
5687 void DeclarationVisitor::PointerInitialization(
5688     const parser::Name &name, const parser::InitialDataTarget &target) {
5689   if (name.symbol) {
5690     Symbol &ultimate{name.symbol->GetUltimate()};
5691     if (IsPointer(ultimate)) {
5692       if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
5693         CHECK(!details->init());
5694         Walk(target);
5695         if (MaybeExpr expr{EvaluateExpr(target)}) {
5696           CheckInitialDataTarget(ultimate, *expr, target.value().source);
5697           details->set_init(std::move(*expr));
5698         }
5699       }
5700     } else {
5701       Say(name, "'%s' is not a pointer but is initialized like one"_err_en_US);
5702     }
5703   }
5704 }
5705 void DeclarationVisitor::PointerInitialization(
5706     const parser::Name &name, const parser::ProcPointerInit &target) {
5707   if (name.symbol) {
5708     Symbol &ultimate{name.symbol->GetUltimate()};
5709     if (IsProcedurePointer(ultimate)) {
5710       auto &details{ultimate.get<ProcEntityDetails>()};
5711       CHECK(!details.init());
5712       Walk(target);
5713       if (const auto *targetName{std::get_if<parser::Name>(&target.u)}) {
5714         CheckInitialProcTarget(ultimate, *targetName, name.source);
5715         if (targetName->symbol) {
5716           details.set_init(*targetName->symbol);
5717         }
5718       } else {
5719         details.set_init(nullptr); // explicit NULL()
5720       }
5721     } else {
5722       Say(name,
5723           "'%s' is not a procedure pointer but is initialized "
5724           "like one"_err_en_US);
5725     }
5726   }
5727 }
5728 
5729 void ResolveNamesVisitor::HandleCall(
5730     Symbol::Flag procFlag, const parser::Call &call) {
5731   std::visit(
5732       common::visitors{
5733           [&](const parser::Name &x) { HandleProcedureName(procFlag, x); },
5734           [&](const parser::ProcComponentRef &x) { Walk(x); },
5735       },
5736       std::get<parser::ProcedureDesignator>(call.t).u);
5737   Walk(std::get<std::list<parser::ActualArgSpec>>(call.t));
5738 }
5739 
5740 void ResolveNamesVisitor::HandleProcedureName(
5741     Symbol::Flag flag, const parser::Name &name) {
5742   CHECK(flag == Symbol::Flag::Function || flag == Symbol::Flag::Subroutine);
5743   auto *symbol{FindSymbol(name)};
5744   if (!symbol) {
5745     if (context().intrinsics().IsIntrinsic(name.source.ToString())) {
5746       symbol =
5747           &MakeSymbol(InclusiveScope(), name.source, Attrs{Attr::INTRINSIC});
5748     } else {
5749       symbol = &MakeSymbol(context().globalScope(), name.source, Attrs{});
5750     }
5751     Resolve(name, *symbol);
5752     if (symbol->has<ModuleDetails>()) {
5753       SayWithDecl(name, *symbol,
5754           "Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
5755       return;
5756     }
5757     if (!symbol->attrs().test(Attr::INTRINSIC)) {
5758       if (isImplicitNoneExternal() && !symbol->attrs().test(Attr::EXTERNAL)) {
5759         Say(name,
5760             "'%s' is an external procedure without the EXTERNAL"
5761             " attribute in a scope with IMPLICIT NONE(EXTERNAL)"_err_en_US);
5762         return;
5763       }
5764       MakeExternal(*symbol);
5765     }
5766     ConvertToProcEntity(*symbol);
5767     SetProcFlag(name, *symbol, flag);
5768   } else if (symbol->has<UnknownDetails>()) {
5769     DIE("unexpected UnknownDetails");
5770   } else if (CheckUseError(name)) {
5771     // error was reported
5772   } else {
5773     symbol = &Resolve(name, symbol)->GetUltimate();
5774     ConvertToProcEntity(*symbol);
5775     if (!SetProcFlag(name, *symbol, flag)) {
5776       return; // reported error
5777     }
5778     if (IsProcedure(*symbol) || symbol->has<DerivedTypeDetails>() ||
5779         symbol->has<ObjectEntityDetails>() ||
5780         symbol->has<AssocEntityDetails>()) {
5781       // Symbols with DerivedTypeDetails, ObjectEntityDetails and
5782       // AssocEntityDetails are accepted here as procedure-designators because
5783       // this means the related FunctionReference are mis-parsed structure
5784       // constructors or array references that will be fixed later when
5785       // analyzing expressions.
5786     } else if (symbol->test(Symbol::Flag::Implicit)) {
5787       Say(name,
5788           "Use of '%s' as a procedure conflicts with its implicit definition"_err_en_US);
5789     } else {
5790       SayWithDecl(name, *symbol,
5791           "Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
5792     }
5793   }
5794 }
5795 
5796 // Variant of HandleProcedureName() for use while skimming the executable
5797 // part of a subprogram to catch calls to dummy procedures that are part
5798 // of the subprogram's interface, and to mark as procedures any symbols
5799 // that might otherwise have been miscategorized as objects.
5800 void ResolveNamesVisitor::NoteExecutablePartCall(
5801     Symbol::Flag flag, const parser::Call &call) {
5802   auto &designator{std::get<parser::ProcedureDesignator>(call.t)};
5803   if (const auto *name{std::get_if<parser::Name>(&designator.u)}) {
5804     // Subtlety: The symbol pointers in the parse tree are not set, because
5805     // they might end up resolving elsewhere (e.g., construct entities in
5806     // SELECT TYPE).
5807     if (Symbol * symbol{currScope().FindSymbol(name->source)}) {
5808       Symbol::Flag other{flag == Symbol::Flag::Subroutine
5809               ? Symbol::Flag::Function
5810               : Symbol::Flag::Subroutine};
5811       if (!symbol->test(other)) {
5812         ConvertToProcEntity(*symbol);
5813         if (symbol->has<ProcEntityDetails>()) {
5814           symbol->set(flag);
5815           if (symbol->IsDummy()) {
5816             symbol->attrs().set(Attr::EXTERNAL);
5817           }
5818           ApplyImplicitRules(*symbol);
5819         }
5820       }
5821     }
5822   }
5823 }
5824 
5825 // Check and set the Function or Subroutine flag on symbol; false on error.
5826 bool ResolveNamesVisitor::SetProcFlag(
5827     const parser::Name &name, Symbol &symbol, Symbol::Flag flag) {
5828   if (symbol.test(Symbol::Flag::Function) && flag == Symbol::Flag::Subroutine) {
5829     SayWithDecl(
5830         name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
5831     return false;
5832   } else if (symbol.test(Symbol::Flag::Subroutine) &&
5833       flag == Symbol::Flag::Function) {
5834     SayWithDecl(
5835         name, symbol, "Cannot call subroutine '%s' like a function"_err_en_US);
5836     return false;
5837   } else if (symbol.has<ProcEntityDetails>()) {
5838     symbol.set(flag); // in case it hasn't been set yet
5839     if (flag == Symbol::Flag::Function) {
5840       ApplyImplicitRules(symbol);
5841     }
5842   } else if (symbol.GetType() && flag == Symbol::Flag::Subroutine) {
5843     SayWithDecl(
5844         name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
5845   }
5846   return true;
5847 }
5848 
5849 bool ModuleVisitor::Pre(const parser::AccessStmt &x) {
5850   Attr accessAttr{AccessSpecToAttr(std::get<parser::AccessSpec>(x.t))};
5851   if (!currScope().IsModule()) { // C869
5852     Say(currStmtSource().value(),
5853         "%s statement may only appear in the specification part of a module"_err_en_US,
5854         EnumToString(accessAttr));
5855     return false;
5856   }
5857   const auto &accessIds{std::get<std::list<parser::AccessId>>(x.t)};
5858   if (accessIds.empty()) {
5859     if (prevAccessStmt_) { // C869
5860       Say("The default accessibility of this module has already been declared"_err_en_US)
5861           .Attach(*prevAccessStmt_, "Previous declaration"_en_US);
5862     }
5863     prevAccessStmt_ = currStmtSource();
5864     defaultAccess_ = accessAttr;
5865   } else {
5866     for (const auto &accessId : accessIds) {
5867       std::visit(
5868           common::visitors{
5869               [=](const parser::Name &y) {
5870                 Resolve(y, SetAccess(y.source, accessAttr));
5871               },
5872               [=](const Indirection<parser::GenericSpec> &y) {
5873                 auto info{GenericSpecInfo{y.value()}};
5874                 const auto &symbolName{info.symbolName()};
5875                 if (auto *symbol{info.FindInScope(context(), currScope())}) {
5876                   info.Resolve(&SetAccess(symbolName, accessAttr, symbol));
5877                 } else if (info.kind().IsName()) {
5878                   info.Resolve(&SetAccess(symbolName, accessAttr));
5879                 } else {
5880                   Say(symbolName, "Generic spec '%s' not found"_err_en_US);
5881                 }
5882               },
5883           },
5884           accessId.u);
5885     }
5886   }
5887   return false;
5888 }
5889 
5890 // Set the access specification for this symbol.
5891 Symbol &ModuleVisitor::SetAccess(
5892     const SourceName &name, Attr attr, Symbol *symbol) {
5893   if (!symbol) {
5894     symbol = &MakeSymbol(name);
5895   }
5896   Attrs &attrs{symbol->attrs()};
5897   if (attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
5898     // PUBLIC/PRIVATE already set: make it a fatal error if it changed
5899     Attr prev = attrs.test(Attr::PUBLIC) ? Attr::PUBLIC : Attr::PRIVATE;
5900     auto msg{IsDefinedOperator(name)
5901             ? "The accessibility of operator '%s' has already been specified as %s"_en_US
5902             : "The accessibility of '%s' has already been specified as %s"_en_US};
5903     Say(name, WithIsFatal(msg, attr != prev), name, EnumToString(prev));
5904   } else {
5905     attrs.set(attr);
5906   }
5907   return *symbol;
5908 }
5909 
5910 static bool NeedsExplicitType(const Symbol &symbol) {
5911   if (symbol.has<UnknownDetails>()) {
5912     return true;
5913   } else if (const auto *details{symbol.detailsIf<EntityDetails>()}) {
5914     return !details->type();
5915   } else if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
5916     return !details->type();
5917   } else if (const auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
5918     return !details->interface().symbol() && !details->interface().type();
5919   } else {
5920     return false;
5921   }
5922 }
5923 
5924 bool ResolveNamesVisitor::Pre(const parser::SpecificationPart &x) {
5925   Walk(std::get<0>(x.t));
5926   Walk(std::get<1>(x.t));
5927   Walk(std::get<2>(x.t));
5928   Walk(std::get<3>(x.t));
5929   const std::list<parser::DeclarationConstruct> &decls{std::get<4>(x.t)};
5930   for (const auto &decl : decls) {
5931     if (const auto *spec{
5932             std::get_if<parser::SpecificationConstruct>(&decl.u)}) {
5933       PreSpecificationConstruct(*spec);
5934     }
5935   }
5936   Walk(decls);
5937   FinishSpecificationPart();
5938   return false;
5939 }
5940 
5941 // Initial processing on specification constructs, before visiting them.
5942 void ResolveNamesVisitor::PreSpecificationConstruct(
5943     const parser::SpecificationConstruct &spec) {
5944   std::visit(
5945       common::visitors{
5946           [&](const Indirection<parser::DerivedTypeDef> &) {},
5947           [&](const parser::Statement<Indirection<parser::GenericStmt>> &y) {
5948             CreateGeneric(std::get<parser::GenericSpec>(y.statement.value().t));
5949           },
5950           [&](const Indirection<parser::InterfaceBlock> &y) {
5951             const auto &stmt{std::get<parser::Statement<parser::InterfaceStmt>>(
5952                 y.value().t)};
5953             const auto *spec{std::get_if<std::optional<parser::GenericSpec>>(
5954                 &stmt.statement.u)};
5955             if (spec && *spec) {
5956               CreateGeneric(**spec);
5957             }
5958           },
5959           [&](const auto &) {},
5960       },
5961       spec.u);
5962 }
5963 
5964 void ResolveNamesVisitor::CreateGeneric(const parser::GenericSpec &x) {
5965   auto info{GenericSpecInfo{x}};
5966   const SourceName &symbolName{info.symbolName()};
5967   if (IsLogicalConstant(context(), symbolName)) {
5968     Say(symbolName,
5969         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
5970     return;
5971   }
5972   GenericDetails genericDetails;
5973   if (Symbol * existing{info.FindInScope(context(), currScope())}) {
5974     if (existing->has<GenericDetails>()) {
5975       info.Resolve(existing);
5976       return; // already have generic, add to it
5977     }
5978     Symbol &ultimate{existing->GetUltimate()};
5979     if (auto *ultimateDetails{ultimate.detailsIf<GenericDetails>()}) {
5980       genericDetails.CopyFrom(*ultimateDetails);
5981     } else if (ultimate.has<SubprogramDetails>() ||
5982         ultimate.has<SubprogramNameDetails>()) {
5983       genericDetails.set_specific(ultimate);
5984     } else if (ultimate.has<DerivedTypeDetails>()) {
5985       genericDetails.set_derivedType(ultimate);
5986     } else {
5987       SayAlreadyDeclared(symbolName, *existing);
5988     }
5989     EraseSymbol(*existing);
5990   }
5991   info.Resolve(&MakeSymbol(symbolName, Attrs{}, std::move(genericDetails)));
5992 }
5993 
5994 void ResolveNamesVisitor::FinishSpecificationPart() {
5995   badStmtFuncFound_ = false;
5996   CheckImports();
5997   bool inModule{currScope().kind() == Scope::Kind::Module};
5998   for (auto &pair : currScope()) {
5999     auto &symbol{*pair.second};
6000     if (NeedsExplicitType(symbol)) {
6001       ApplyImplicitRules(symbol);
6002     }
6003     if (symbol.has<GenericDetails>()) {
6004       CheckGenericProcedures(symbol);
6005     }
6006     if (inModule && symbol.attrs().test(Attr::EXTERNAL) &&
6007         !symbol.test(Symbol::Flag::Function)) {
6008       // in a module, external proc without return type is subroutine
6009       symbol.set(Symbol::Flag::Subroutine);
6010     }
6011   }
6012   currScope().InstantiateDerivedTypes(context());
6013   // TODO: what about instantiations in BLOCK?
6014   CheckSaveStmts();
6015   CheckCommonBlocks();
6016   CheckEquivalenceSets();
6017 }
6018 
6019 void ResolveNamesVisitor::CheckImports() {
6020   auto &scope{currScope()};
6021   switch (scope.GetImportKind()) {
6022   case common::ImportKind::None:
6023     break;
6024   case common::ImportKind::All:
6025     // C8102: all entities in host must not be hidden
6026     for (const auto &pair : scope.parent()) {
6027       auto &name{pair.first};
6028       std::optional<SourceName> scopeName{scope.GetName()};
6029       if (!scopeName || name != *scopeName) {
6030         CheckImport(prevImportStmt_.value(), name);
6031       }
6032     }
6033     break;
6034   case common::ImportKind::Default:
6035   case common::ImportKind::Only:
6036     // C8102: entities named in IMPORT must not be hidden
6037     for (auto &name : scope.importNames()) {
6038       CheckImport(name, name);
6039     }
6040     break;
6041   }
6042 }
6043 
6044 void ResolveNamesVisitor::CheckImport(
6045     const SourceName &location, const SourceName &name) {
6046   if (auto *symbol{FindInScope(currScope(), name)}) {
6047     Say(location, "'%s' from host is not accessible"_err_en_US, name)
6048         .Attach(symbol->name(), "'%s' is hidden by this entity"_en_US,
6049             symbol->name());
6050   }
6051 }
6052 
6053 bool ResolveNamesVisitor::Pre(const parser::ImplicitStmt &x) {
6054   return CheckNotInBlock("IMPLICIT") && // C1107
6055       ImplicitRulesVisitor::Pre(x);
6056 }
6057 
6058 void ResolveNamesVisitor::Post(const parser::PointerObject &x) {
6059   std::visit(common::visitors{
6060                  [&](const parser::Name &x) { ResolveName(x); },
6061                  [&](const parser::StructureComponent &x) {
6062                    ResolveStructureComponent(x);
6063                  },
6064              },
6065       x.u);
6066 }
6067 void ResolveNamesVisitor::Post(const parser::AllocateObject &x) {
6068   std::visit(common::visitors{
6069                  [&](const parser::Name &x) { ResolveName(x); },
6070                  [&](const parser::StructureComponent &x) {
6071                    ResolveStructureComponent(x);
6072                  },
6073              },
6074       x.u);
6075 }
6076 
6077 bool ResolveNamesVisitor::Pre(const parser::PointerAssignmentStmt &x) {
6078   const auto &dataRef{std::get<parser::DataRef>(x.t)};
6079   const auto &bounds{std::get<parser::PointerAssignmentStmt::Bounds>(x.t)};
6080   const auto &expr{std::get<parser::Expr>(x.t)};
6081   ResolveDataRef(dataRef);
6082   Walk(bounds);
6083   // Resolve unrestricted specific intrinsic procedures as in "p => cos".
6084   if (const parser::Name * name{parser::Unwrap<parser::Name>(expr)}) {
6085     if (NameIsKnownOrIntrinsic(*name)) {
6086       return false;
6087     }
6088   }
6089   Walk(expr);
6090   return false;
6091 }
6092 void ResolveNamesVisitor::Post(const parser::Designator &x) {
6093   ResolveDesignator(x);
6094 }
6095 
6096 void ResolveNamesVisitor::Post(const parser::ProcComponentRef &x) {
6097   ResolveStructureComponent(x.v.thing);
6098 }
6099 void ResolveNamesVisitor::Post(const parser::TypeGuardStmt &x) {
6100   DeclTypeSpecVisitor::Post(x);
6101   ConstructVisitor::Post(x);
6102 }
6103 bool ResolveNamesVisitor::Pre(const parser::StmtFunctionStmt &x) {
6104   CheckNotInBlock("STATEMENT FUNCTION"); // C1107
6105   if (HandleStmtFunction(x)) {
6106     return false;
6107   } else {
6108     // This is an array element assignment: resolve names of indices
6109     const auto &names{std::get<std::list<parser::Name>>(x.t)};
6110     for (auto &name : names) {
6111       ResolveName(name);
6112     }
6113     return true;
6114   }
6115 }
6116 
6117 bool ResolveNamesVisitor::Pre(const parser::DefinedOpName &x) {
6118   const parser::Name &name{x.v};
6119   if (FindSymbol(name)) {
6120     // OK
6121   } else if (IsLogicalConstant(context(), name.source)) {
6122     Say(name,
6123         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
6124   } else {
6125     // Resolved later in expression semantics
6126     MakePlaceholder(name, MiscDetails::Kind::TypeBoundDefinedOp);
6127   }
6128   return false;
6129 }
6130 
6131 void ResolveNamesVisitor::Post(const parser::AssignStmt &x) {
6132   if (auto *name{ResolveName(std::get<parser::Name>(x.t))}) {
6133     ConvertToObjectEntity(DEREF(name->symbol));
6134   }
6135 }
6136 void ResolveNamesVisitor::Post(const parser::AssignedGotoStmt &x) {
6137   if (auto *name{ResolveName(std::get<parser::Name>(x.t))}) {
6138     ConvertToObjectEntity(DEREF(name->symbol));
6139   }
6140 }
6141 
6142 bool ResolveNamesVisitor::Pre(const parser::ProgramUnit &x) {
6143   auto root{ProgramTree::Build(x)};
6144   SetScope(context().globalScope());
6145   ResolveSpecificationParts(root);
6146   FinishSpecificationParts(root);
6147   inExecutionPart_ = true;
6148   ResolveExecutionParts(root);
6149   inExecutionPart_ = false;
6150   ResolveOmpParts(x);
6151   return false;
6152 }
6153 
6154 // References to procedures need to record that their symbols are known
6155 // to be procedures, so that they don't get converted to objects by default.
6156 class ExecutionPartSkimmer {
6157 public:
6158   explicit ExecutionPartSkimmer(ResolveNamesVisitor &resolver)
6159       : resolver_{resolver} {}
6160 
6161   void Walk(const parser::ExecutionPart *exec) {
6162     if (exec) {
6163       parser::Walk(*exec, *this);
6164     }
6165   }
6166 
6167   template <typename A> bool Pre(const A &) { return true; }
6168   template <typename A> void Post(const A &) {}
6169   void Post(const parser::FunctionReference &fr) {
6170     resolver_.NoteExecutablePartCall(Symbol::Flag::Function, fr.v);
6171   }
6172   void Post(const parser::CallStmt &cs) {
6173     resolver_.NoteExecutablePartCall(Symbol::Flag::Subroutine, cs.v);
6174   }
6175 
6176 private:
6177   ResolveNamesVisitor &resolver_;
6178 };
6179 
6180 // Build the scope tree and resolve names in the specification parts of this
6181 // node and its children
6182 void ResolveNamesVisitor::ResolveSpecificationParts(ProgramTree &node) {
6183   if (node.isSpecificationPartResolved()) {
6184     return; // been here already
6185   }
6186   node.set_isSpecificationPartResolved();
6187   if (!BeginScopeForNode(node)) {
6188     return; // an error prevented scope from being created
6189   }
6190   Scope &scope{currScope()};
6191   node.set_scope(scope);
6192   AddSubpNames(node);
6193   std::visit(
6194       [&](const auto *x) {
6195         if (x) {
6196           Walk(*x);
6197         }
6198       },
6199       node.stmt());
6200   Walk(node.spec());
6201   // If this is a function, convert result to an object. This is to prevent the
6202   // result to be converted later to a function symbol if it is called inside
6203   // the function.
6204   // If the result is function pointer, then ConvertToObjectEntity will not
6205   // convert the result to an object, and calling the symbol inside the function
6206   // will result in calls to the result pointer.
6207   // A function cannot be called recursively if RESULT was not used to define a
6208   // distinct result name (15.6.2.2 point 4.).
6209   if (Symbol * symbol{scope.symbol()}) {
6210     if (auto *details{symbol->detailsIf<SubprogramDetails>()}) {
6211       if (details->isFunction()) {
6212         ConvertToObjectEntity(const_cast<Symbol &>(details->result()));
6213       }
6214     }
6215   }
6216   if (node.IsModule()) {
6217     ApplyDefaultAccess();
6218   }
6219   for (auto &child : node.children()) {
6220     ResolveSpecificationParts(child);
6221   }
6222   ExecutionPartSkimmer{*this}.Walk(node.exec());
6223   PopScope();
6224   // Ensure that every object entity has a type.
6225   for (auto &pair : *node.scope()) {
6226     ApplyImplicitRules(*pair.second);
6227   }
6228 }
6229 
6230 // Add SubprogramNameDetails symbols for module and internal subprograms
6231 void ResolveNamesVisitor::AddSubpNames(ProgramTree &node) {
6232   auto kind{
6233       node.IsModule() ? SubprogramKind::Module : SubprogramKind::Internal};
6234   for (auto &child : node.children()) {
6235     auto &symbol{MakeSymbol(child.name(), SubprogramNameDetails{kind, child})};
6236     symbol.set(child.GetSubpFlag());
6237   }
6238 }
6239 
6240 // Push a new scope for this node or return false on error.
6241 bool ResolveNamesVisitor::BeginScopeForNode(const ProgramTree &node) {
6242   switch (node.GetKind()) {
6243     SWITCH_COVERS_ALL_CASES
6244   case ProgramTree::Kind::Program:
6245     PushScope(Scope::Kind::MainProgram,
6246         &MakeSymbol(node.name(), MainProgramDetails{}));
6247     return true;
6248   case ProgramTree::Kind::Function:
6249   case ProgramTree::Kind::Subroutine:
6250     return BeginSubprogram(
6251         node.name(), node.GetSubpFlag(), node.HasModulePrefix());
6252   case ProgramTree::Kind::MpSubprogram:
6253     return BeginMpSubprogram(node.name());
6254   case ProgramTree::Kind::Module:
6255     BeginModule(node.name(), false);
6256     return true;
6257   case ProgramTree::Kind::Submodule:
6258     return BeginSubmodule(node.name(), node.GetParentId());
6259   case ProgramTree::Kind::BlockData:
6260     PushBlockDataScope(node.name());
6261     return true;
6262   }
6263 }
6264 
6265 // Some analyses and checks, such as the processing of initializers of
6266 // pointers, are deferred until all of the pertinent specification parts
6267 // have been visited.  This deferred processing enables the use of forward
6268 // references in these circumstances.
6269 class DeferredCheckVisitor {
6270 public:
6271   explicit DeferredCheckVisitor(ResolveNamesVisitor &resolver)
6272       : resolver_{resolver} {}
6273 
6274   template <typename A> void Walk(const A &x) { parser::Walk(x, *this); }
6275 
6276   template <typename A> bool Pre(const A &) { return true; }
6277   template <typename A> void Post(const A &) {}
6278 
6279   void Post(const parser::DerivedTypeStmt &x) {
6280     const auto &name{std::get<parser::Name>(x.t)};
6281     if (Symbol * symbol{name.symbol}) {
6282       if (Scope * scope{symbol->scope()}) {
6283         if (scope->IsDerivedType()) {
6284           resolver_.PushScope(*scope);
6285           pushedScope_ = true;
6286         }
6287       }
6288     }
6289   }
6290   void Post(const parser::EndTypeStmt &) {
6291     if (pushedScope_) {
6292       resolver_.PopScope();
6293       pushedScope_ = false;
6294     }
6295   }
6296 
6297   void Post(const parser::ProcInterface &pi) {
6298     if (const auto *name{std::get_if<parser::Name>(&pi.u)}) {
6299       resolver_.CheckExplicitInterface(*name);
6300     }
6301   }
6302   bool Pre(const parser::EntityDecl &decl) {
6303     Init(std::get<parser::Name>(decl.t),
6304         std::get<std::optional<parser::Initialization>>(decl.t));
6305     return false;
6306   }
6307   bool Pre(const parser::ComponentDecl &decl) {
6308     Init(std::get<parser::Name>(decl.t),
6309         std::get<std::optional<parser::Initialization>>(decl.t));
6310     return false;
6311   }
6312   bool Pre(const parser::ProcDecl &decl) {
6313     if (const auto &init{
6314             std::get<std::optional<parser::ProcPointerInit>>(decl.t)}) {
6315       resolver_.PointerInitialization(std::get<parser::Name>(decl.t), *init);
6316     }
6317     return false;
6318   }
6319   void Post(const parser::TypeBoundProcedureStmt::WithInterface &tbps) {
6320     resolver_.CheckExplicitInterface(tbps.interfaceName);
6321   }
6322   void Post(const parser::TypeBoundProcedureStmt::WithoutInterface &tbps) {
6323     if (pushedScope_) {
6324       resolver_.CheckBindings(tbps);
6325     }
6326   }
6327 
6328 private:
6329   void Init(const parser::Name &name,
6330       const std::optional<parser::Initialization> &init) {
6331     if (init) {
6332       if (const auto *target{
6333               std::get_if<parser::InitialDataTarget>(&init->u)}) {
6334         resolver_.PointerInitialization(name, *target);
6335       }
6336     }
6337   }
6338 
6339   ResolveNamesVisitor &resolver_;
6340   bool pushedScope_{false};
6341 };
6342 
6343 bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
6344   const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
6345   const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
6346   switch (beginDir.v) {
6347   case parser::OmpBlockDirective::Directive::Master:
6348     PushContext(beginDir.source, OmpDirective::MASTER);
6349     break;
6350   case parser::OmpBlockDirective::Directive::Ordered:
6351     PushContext(beginDir.source, OmpDirective::ORDERED);
6352     break;
6353   case parser::OmpBlockDirective::Directive::Parallel:
6354     PushContext(beginDir.source, OmpDirective::PARALLEL);
6355     break;
6356   case parser::OmpBlockDirective::Directive::Single:
6357     PushContext(beginDir.source, OmpDirective::SINGLE);
6358     break;
6359   case parser::OmpBlockDirective::Directive::Target:
6360     PushContext(beginDir.source, OmpDirective::TARGET);
6361     break;
6362   case parser::OmpBlockDirective::Directive::TargetData:
6363     PushContext(beginDir.source, OmpDirective::TARGET_DATA);
6364     break;
6365   case parser::OmpBlockDirective::Directive::Task:
6366     PushContext(beginDir.source, OmpDirective::TASK);
6367     break;
6368   case parser::OmpBlockDirective::Directive::Teams:
6369     PushContext(beginDir.source, OmpDirective::TEAMS);
6370     break;
6371   case parser::OmpBlockDirective::Directive::Workshare:
6372     PushContext(beginDir.source, OmpDirective::WORKSHARE);
6373     break;
6374   case parser::OmpBlockDirective::Directive::ParallelWorkshare:
6375     PushContext(beginDir.source, OmpDirective::PARALLEL_WORKSHARE);
6376     break;
6377   case parser::OmpBlockDirective::Directive::TargetTeams:
6378     PushContext(beginDir.source, OmpDirective::TARGET_TEAMS);
6379     break;
6380   case parser::OmpBlockDirective::Directive::TargetParallel:
6381     PushContext(beginDir.source, OmpDirective::TARGET_PARALLEL);
6382     break;
6383   default:
6384     // TODO others
6385     break;
6386   }
6387   ClearDataSharingAttributeObjects();
6388   return true;
6389 }
6390 
6391 bool OmpAttributeVisitor::Pre(const parser::OpenMPLoopConstruct &x) {
6392   const auto &beginLoopDir{std::get<parser::OmpBeginLoopDirective>(x.t)};
6393   const auto &beginDir{std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
6394   const auto &clauseList{std::get<parser::OmpClauseList>(beginLoopDir.t)};
6395   switch (beginDir.v) {
6396   case parser::OmpLoopDirective::Directive::Distribute:
6397     PushContext(beginDir.source, OmpDirective::DISTRIBUTE);
6398     break;
6399   case parser::OmpLoopDirective::Directive::DistributeParallelDo:
6400     PushContext(beginDir.source, OmpDirective::DISTRIBUTE_PARALLEL_DO);
6401     break;
6402   case parser::OmpLoopDirective::Directive::DistributeParallelDoSimd:
6403     PushContext(beginDir.source, OmpDirective::DISTRIBUTE_PARALLEL_DO_SIMD);
6404     break;
6405   case parser::OmpLoopDirective::Directive::DistributeSimd:
6406     PushContext(beginDir.source, OmpDirective::DISTRIBUTE_SIMD);
6407     break;
6408   case parser::OmpLoopDirective::Directive::Do:
6409     PushContext(beginDir.source, OmpDirective::DO);
6410     break;
6411   case parser::OmpLoopDirective::Directive::DoSimd:
6412     PushContext(beginDir.source, OmpDirective::DO_SIMD);
6413     break;
6414   case parser::OmpLoopDirective::Directive::ParallelDo:
6415     PushContext(beginDir.source, OmpDirective::PARALLEL_DO);
6416     break;
6417   case parser::OmpLoopDirective::Directive::ParallelDoSimd:
6418     PushContext(beginDir.source, OmpDirective::PARALLEL_DO_SIMD);
6419     break;
6420   case parser::OmpLoopDirective::Directive::Simd:
6421     PushContext(beginDir.source, OmpDirective::SIMD);
6422     break;
6423   case parser::OmpLoopDirective::Directive::TargetParallelDo:
6424     PushContext(beginDir.source, OmpDirective::TARGET_PARALLEL_DO);
6425     break;
6426   case parser::OmpLoopDirective::Directive::TargetParallelDoSimd:
6427     PushContext(beginDir.source, OmpDirective::TARGET_PARALLEL_DO_SIMD);
6428     break;
6429   case parser::OmpLoopDirective::Directive::TargetTeamsDistribute:
6430     PushContext(beginDir.source, OmpDirective::TARGET_TEAMS_DISTRIBUTE);
6431     break;
6432   case parser::OmpLoopDirective::Directive::TargetTeamsDistributeParallelDo:
6433     PushContext(
6434         beginDir.source, OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO);
6435     break;
6436   case parser::OmpLoopDirective::Directive::TargetTeamsDistributeParallelDoSimd:
6437     PushContext(beginDir.source,
6438         OmpDirective::TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
6439     break;
6440   case parser::OmpLoopDirective::Directive::TargetTeamsDistributeSimd:
6441     PushContext(beginDir.source, OmpDirective::TARGET_TEAMS_DISTRIBUTE_SIMD);
6442     break;
6443   case parser::OmpLoopDirective::Directive::TargetSimd:
6444     PushContext(beginDir.source, OmpDirective::TARGET_SIMD);
6445     break;
6446   case parser::OmpLoopDirective::Directive::Taskloop:
6447     PushContext(beginDir.source, OmpDirective::TASKLOOP);
6448     break;
6449   case parser::OmpLoopDirective::Directive::TaskloopSimd:
6450     PushContext(beginDir.source, OmpDirective::TASKLOOP_SIMD);
6451     break;
6452   case parser::OmpLoopDirective::Directive::TeamsDistribute:
6453     PushContext(beginDir.source, OmpDirective::TEAMS_DISTRIBUTE);
6454     break;
6455   case parser::OmpLoopDirective::Directive::TeamsDistributeParallelDo:
6456     PushContext(beginDir.source, OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO);
6457     break;
6458   case parser::OmpLoopDirective::Directive::TeamsDistributeParallelDoSimd:
6459     PushContext(
6460         beginDir.source, OmpDirective::TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD);
6461     break;
6462   case parser::OmpLoopDirective::Directive::TeamsDistributeSimd:
6463     PushContext(beginDir.source, OmpDirective::TEAMS_DISTRIBUTE_SIMD);
6464     break;
6465   }
6466   ClearDataSharingAttributeObjects();
6467   SetContextAssociatedLoopLevel(GetAssociatedLoopLevelFromClauses(clauseList));
6468   PrivatizeAssociatedLoopIndex(x);
6469   return true;
6470 }
6471 
6472 const parser::Name &OmpAttributeVisitor::GetLoopIndex(
6473     const parser::DoConstruct &x) {
6474   auto &loopControl{x.GetLoopControl().value()};
6475   using Bounds = parser::LoopControl::Bounds;
6476   const Bounds &bounds{std::get<Bounds>(loopControl.u)};
6477   return bounds.name.thing;
6478 }
6479 
6480 void OmpAttributeVisitor::ResolveSeqLoopIndexInParallelOrTaskConstruct(
6481     const parser::Name &iv) {
6482   auto targetIt{ompContext_.rbegin()};
6483   for (;; ++targetIt) {
6484     if (targetIt == ompContext_.rend()) {
6485       return;
6486     }
6487     if (parallelSet.test(targetIt->directive) ||
6488         taskGeneratingSet.test(targetIt->directive)) {
6489       break;
6490     }
6491   }
6492   if (auto *symbol{ResolveOmp(iv, Symbol::Flag::OmpPrivate, targetIt->scope)}) {
6493     targetIt++;
6494     symbol->set(Symbol::Flag::OmpPreDetermined);
6495     iv.symbol = symbol; // adjust the symbol within region
6496     for (auto it{ompContext_.rbegin()}; it != targetIt; ++it) {
6497       AddToContextObjectWithDSA(*symbol, Symbol::Flag::OmpPrivate, *it);
6498     }
6499   }
6500 }
6501 
6502 // 2.15.1.1 Data-sharing Attribute Rules - Predetermined
6503 //   - A loop iteration variable for a sequential loop in a parallel
6504 //     or task generating construct is private in the innermost such
6505 //     construct that encloses the loop
6506 bool OmpAttributeVisitor::Pre(const parser::DoConstruct &x) {
6507   if (!ompContext_.empty() && GetContext().withinConstruct) {
6508     if (const auto &iv{GetLoopIndex(x)}; iv.symbol) {
6509       if (!iv.symbol->test(Symbol::Flag::OmpPreDetermined)) {
6510         ResolveSeqLoopIndexInParallelOrTaskConstruct(iv);
6511       } else {
6512         // TODO: conflict checks with explicitly determined DSA
6513       }
6514     }
6515   }
6516   return true;
6517 }
6518 
6519 const parser::DoConstruct *OmpAttributeVisitor::GetDoConstructIf(
6520     const parser::ExecutionPartConstruct &x) {
6521   if (auto *y{std::get_if<parser::ExecutableConstruct>(&x.u)}) {
6522     if (auto *z{std::get_if<Indirection<parser::DoConstruct>>(&y->u)}) {
6523       return &z->value();
6524     }
6525   }
6526   return nullptr;
6527 }
6528 
6529 std::size_t OmpAttributeVisitor::GetAssociatedLoopLevelFromClauses(
6530     const parser::OmpClauseList &x) {
6531   std::size_t orderedLevel{0};
6532   std::size_t collapseLevel{0};
6533   for (const auto &clause : x.v) {
6534     if (const auto *orderedClause{
6535             std::get_if<parser::OmpClause::Ordered>(&clause.u)}) {
6536       if (const auto v{
6537               evaluate::ToInt64(resolver_.EvaluateIntExpr(orderedClause->v))}) {
6538         orderedLevel = *v;
6539       }
6540     }
6541     if (const auto *collapseClause{
6542             std::get_if<parser::OmpClause::Collapse>(&clause.u)}) {
6543       if (const auto v{evaluate::ToInt64(
6544               resolver_.EvaluateIntExpr(collapseClause->v))}) {
6545         collapseLevel = *v;
6546       }
6547     }
6548   }
6549 
6550   if (orderedLevel && (!collapseLevel || orderedLevel >= collapseLevel)) {
6551     return orderedLevel;
6552   } else if (!orderedLevel && collapseLevel) {
6553     return collapseLevel;
6554   } // orderedLevel < collapseLevel is an error handled in structural checks
6555   return 1; // default is outermost loop
6556 }
6557 
6558 // 2.15.1.1 Data-sharing Attribute Rules - Predetermined
6559 //   - The loop iteration variable(s) in the associated do-loop(s) of a do,
6560 //     parallel do, taskloop, or distribute construct is (are) private.
6561 //   - The loop iteration variable in the associated do-loop of a simd construct
6562 //     with just one associated do-loop is linear with a linear-step that is the
6563 //     increment of the associated do-loop.
6564 //   - The loop iteration variables in the associated do-loops of a simd
6565 //     construct with multiple associated do-loops are lastprivate.
6566 //
6567 // TODO: This assumes that the do-loops association for collapse/ordered
6568 //       clause has been performed (the number of nested do-loops >= n).
6569 void OmpAttributeVisitor::PrivatizeAssociatedLoopIndex(
6570     const parser::OpenMPLoopConstruct &x) {
6571   std::size_t level{GetContext().associatedLoopLevel};
6572   Symbol::Flag ivDSA{Symbol::Flag::OmpPrivate};
6573   if (simdSet.test(GetContext().directive)) {
6574     if (level == 1) {
6575       ivDSA = Symbol::Flag::OmpLinear;
6576     } else {
6577       ivDSA = Symbol::Flag::OmpLastPrivate;
6578     }
6579   }
6580 
6581   auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t)};
6582   for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
6583     // go through all the nested do-loops and resolve index variables
6584     const parser::Name &iv{GetLoopIndex(*loop)};
6585     if (auto *symbol{ResolveOmp(iv, ivDSA, currScope())}) {
6586       symbol->set(Symbol::Flag::OmpPreDetermined);
6587       iv.symbol = symbol; // adjust the symbol within region
6588       AddToContextObjectWithDSA(*symbol, ivDSA);
6589     }
6590 
6591     const auto &block{std::get<parser::Block>(loop->t)};
6592     const auto it{block.begin()};
6593     loop = it != block.end() ? GetDoConstructIf(*it) : nullptr;
6594   }
6595   CHECK(level == 0);
6596 }
6597 
6598 bool OmpAttributeVisitor::Pre(const parser::OpenMPSectionsConstruct &x) {
6599   const auto &beginSectionsDir{
6600       std::get<parser::OmpBeginSectionsDirective>(x.t)};
6601   const auto &beginDir{
6602       std::get<parser::OmpSectionsDirective>(beginSectionsDir.t)};
6603   switch (beginDir.v) {
6604   case parser::OmpSectionsDirective::Directive::ParallelSections:
6605     PushContext(beginDir.source, OmpDirective::PARALLEL_SECTIONS);
6606     break;
6607   case parser::OmpSectionsDirective::Directive::Sections:
6608     PushContext(beginDir.source, OmpDirective::SECTIONS);
6609     break;
6610   }
6611   ClearDataSharingAttributeObjects();
6612   return true;
6613 }
6614 
6615 bool OmpAttributeVisitor::Pre(const parser::OpenMPThreadprivate &x) {
6616   PushContext(x.source, OmpDirective::THREADPRIVATE);
6617   const auto &list{std::get<parser::OmpObjectList>(x.t)};
6618   ResolveOmpObjectList(list, Symbol::Flag::OmpThreadprivate);
6619   return false;
6620 }
6621 
6622 void OmpAttributeVisitor::Post(const parser::OmpDefaultClause &x) {
6623   if (!ompContext_.empty()) {
6624     switch (x.v) {
6625     case parser::OmpDefaultClause::Type::Private:
6626       SetContextDefaultDSA(Symbol::Flag::OmpPrivate);
6627       break;
6628     case parser::OmpDefaultClause::Type::Firstprivate:
6629       SetContextDefaultDSA(Symbol::Flag::OmpFirstPrivate);
6630       break;
6631     case parser::OmpDefaultClause::Type::Shared:
6632       SetContextDefaultDSA(Symbol::Flag::OmpShared);
6633       break;
6634     case parser::OmpDefaultClause::Type::None:
6635       SetContextDefaultDSA(Symbol::Flag::OmpNone);
6636       break;
6637     }
6638   }
6639 }
6640 
6641 // For OpenMP constructs, check all the data-refs within the constructs
6642 // and adjust the symbol for each Name if necessary
6643 void OmpAttributeVisitor::Post(const parser::Name &name) {
6644   auto *symbol{name.symbol};
6645   if (symbol && !ompContext_.empty() && GetContext().withinConstruct) {
6646     if (!symbol->owner().IsDerivedType() && !symbol->has<ProcEntityDetails>() &&
6647         !IsObjectWithDSA(*symbol)) {
6648       // TODO: create a separate function to go through the rules for
6649       //       predetermined, explicitly determined, and implicitly
6650       //       determined data-sharing attributes (2.15.1.1).
6651       if (Symbol * found{currScope().FindSymbol(name.source)}) {
6652         if (symbol != found) {
6653           name.symbol = found; // adjust the symbol within region
6654         } else if (GetContext().defaultDSA == Symbol::Flag::OmpNone) {
6655           context_.Say(name.source,
6656               "The DEFAULT(NONE) clause requires that '%s' must be listed in "
6657               "a data-sharing attribute clause"_err_en_US,
6658               symbol->name());
6659         }
6660       }
6661     }
6662   } // within OpenMP construct
6663 }
6664 
6665 bool OmpAttributeVisitor::HasDataSharingAttributeObject(const Symbol &object) {
6666   auto it{dataSharingAttributeObjects_.find(object)};
6667   return it != dataSharingAttributeObjects_.end();
6668 }
6669 
6670 Symbol *OmpAttributeVisitor::ResolveOmpCommonBlockName(
6671     const parser::Name *name) {
6672   if (auto *prev{name
6673               ? GetContext().scope.parent().FindCommonBlock(name->source)
6674               : nullptr}) {
6675     name->symbol = prev;
6676     return prev;
6677   } else {
6678     return nullptr;
6679   }
6680 }
6681 
6682 void OmpAttributeVisitor::ResolveOmpObjectList(
6683     const parser::OmpObjectList &ompObjectList, Symbol::Flag ompFlag) {
6684   for (const auto &ompObject : ompObjectList.v) {
6685     ResolveOmpObject(ompObject, ompFlag);
6686   }
6687 }
6688 
6689 void OmpAttributeVisitor::ResolveOmpObject(
6690     const parser::OmpObject &ompObject, Symbol::Flag ompFlag) {
6691   std::visit(
6692       common::visitors{
6693           [&](const parser::Designator &designator) {
6694             if (const auto *name{GetDesignatorNameIfDataRef(designator)}) {
6695               if (auto *symbol{ResolveOmp(*name, ompFlag, currScope())}) {
6696                 AddToContextObjectWithDSA(*symbol, ompFlag);
6697                 if (dataSharingAttributeFlags.test(ompFlag)) {
6698                   CheckMultipleAppearances(*name, *symbol, ompFlag);
6699                 }
6700               }
6701             } else if (const auto *designatorName{
6702                            resolver_.ResolveDesignator(designator)};
6703                        designatorName->symbol) {
6704               // Array sections to be changed to substrings as needed
6705               if (AnalyzeExpr(context_, designator)) {
6706                 if (std::holds_alternative<parser::Substring>(designator.u)) {
6707                   context_.Say(designator.source,
6708                       "Substrings are not allowed on OpenMP "
6709                       "directives or clauses"_err_en_US);
6710                 }
6711               }
6712               // other checks, more TBD
6713               if (const auto *details{designatorName->symbol
6714                                           ->detailsIf<ObjectEntityDetails>()}) {
6715                 if (details->IsArray()) {
6716                   // TODO: check Array Sections
6717                 } else if (designatorName->symbol->owner().IsDerivedType()) {
6718                   // TODO: check Structure Component
6719                 }
6720               }
6721             }
6722           },
6723           [&](const parser::Name &name) { // common block
6724             if (auto *symbol{ResolveOmpCommonBlockName(&name)}) {
6725               CheckMultipleAppearances(
6726                   name, *symbol, Symbol::Flag::OmpCommonBlock);
6727               // 2.15.3 When a named common block appears in a list, it has the
6728               // same meaning as if every explicit member of the common block
6729               // appeared in the list
6730               for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
6731                 if (auto *resolvedObject{
6732                         ResolveOmp(*object, ompFlag, currScope())}) {
6733                   AddToContextObjectWithDSA(*resolvedObject, ompFlag);
6734                 }
6735               }
6736             } else {
6737               context_.Say(name.source, // 2.15.3
6738                   "COMMON block must be declared in the same scoping unit "
6739                   "in which the OpenMP directive or clause appears"_err_en_US);
6740             }
6741           },
6742       },
6743       ompObject.u);
6744 }
6745 
6746 Symbol *OmpAttributeVisitor::ResolveOmp(
6747     const parser::Name &name, Symbol::Flag ompFlag, Scope &scope) {
6748   if (ompFlagsRequireNewSymbol.test(ompFlag)) {
6749     return DeclarePrivateAccessEntity(name, ompFlag, scope);
6750   } else {
6751     return DeclareOrMarkOtherAccessEntity(name, ompFlag);
6752   }
6753 }
6754 
6755 Symbol *OmpAttributeVisitor::ResolveOmp(
6756     Symbol &symbol, Symbol::Flag ompFlag, Scope &scope) {
6757   if (ompFlagsRequireNewSymbol.test(ompFlag)) {
6758     return DeclarePrivateAccessEntity(symbol, ompFlag, scope);
6759   } else {
6760     return DeclareOrMarkOtherAccessEntity(symbol, ompFlag);
6761   }
6762 }
6763 
6764 Symbol *OmpAttributeVisitor::DeclarePrivateAccessEntity(
6765     const parser::Name &name, Symbol::Flag ompFlag, Scope &scope) {
6766   if (!name.symbol) {
6767     return nullptr; // not resolved by Name Resolution step, do nothing
6768   }
6769   name.symbol = DeclarePrivateAccessEntity(*name.symbol, ompFlag, scope);
6770   return name.symbol;
6771 }
6772 
6773 Symbol *OmpAttributeVisitor::DeclarePrivateAccessEntity(
6774     Symbol &object, Symbol::Flag ompFlag, Scope &scope) {
6775   if (object.owner() != currScope()) {
6776     auto &symbol{MakeAssocSymbol(object.name(), object, scope)};
6777     symbol.set(ompFlag);
6778     return &symbol;
6779   } else {
6780     object.set(ompFlag);
6781     return &object;
6782   }
6783 }
6784 
6785 Symbol *OmpAttributeVisitor::DeclareOrMarkOtherAccessEntity(
6786     const parser::Name &name, Symbol::Flag ompFlag) {
6787   Symbol *prev{currScope().FindSymbol(name.source)};
6788   if (!name.symbol || !prev) {
6789     return nullptr;
6790   } else if (prev != name.symbol) {
6791     name.symbol = prev;
6792   }
6793   return DeclareOrMarkOtherAccessEntity(*prev, ompFlag);
6794 }
6795 
6796 Symbol *OmpAttributeVisitor::DeclareOrMarkOtherAccessEntity(
6797     Symbol &object, Symbol::Flag ompFlag) {
6798   if (ompFlagsRequireMark.test(ompFlag)) {
6799     object.set(ompFlag);
6800   }
6801   return &object;
6802 }
6803 
6804 static bool WithMultipleAppearancesException(
6805     const Symbol &symbol, Symbol::Flag ompFlag) {
6806   return (ompFlag == Symbol::Flag::OmpFirstPrivate &&
6807              symbol.test(Symbol::Flag::OmpLastPrivate)) ||
6808       (ompFlag == Symbol::Flag::OmpLastPrivate &&
6809           symbol.test(Symbol::Flag::OmpFirstPrivate));
6810 }
6811 
6812 void OmpAttributeVisitor::CheckMultipleAppearances(
6813     const parser::Name &name, const Symbol &symbol, Symbol::Flag ompFlag) {
6814   const auto *target{&symbol};
6815   if (ompFlagsRequireNewSymbol.test(ompFlag)) {
6816     if (const auto *details{symbol.detailsIf<HostAssocDetails>()}) {
6817       target = &details->symbol();
6818     }
6819   }
6820   if (HasDataSharingAttributeObject(*target) &&
6821       !WithMultipleAppearancesException(symbol, ompFlag)) {
6822     context_.Say(name.source,
6823         "'%s' appears in more than one data-sharing clause "
6824         "on the same OpenMP directive"_err_en_US,
6825         name.ToString());
6826   } else {
6827     AddDataSharingAttributeObject(*target);
6828   }
6829 }
6830 
6831 // Perform checks and completions that need to happen after all of
6832 // the specification parts but before any of the execution parts.
6833 void ResolveNamesVisitor::FinishSpecificationParts(const ProgramTree &node) {
6834   if (!node.scope()) {
6835     return; // error occurred creating scope
6836   }
6837   SetScope(*node.scope());
6838   // The initializers of pointers, pointer components, and non-deferred
6839   // type-bound procedure bindings have not yet been traversed.
6840   // We do that now, when any (formerly) forward references that appear
6841   // in those initializers will resolve to the right symbols.
6842   DeferredCheckVisitor{*this}.Walk(node.spec());
6843   DeferredCheckVisitor{*this}.Walk(node.exec()); // for BLOCK
6844   for (Scope &childScope : currScope().children()) {
6845     if (childScope.IsDerivedType() && !childScope.symbol()) {
6846       FinishDerivedTypeInstantiation(childScope);
6847     }
6848   }
6849   for (const auto &child : node.children()) {
6850     FinishSpecificationParts(child);
6851   }
6852 }
6853 
6854 // Fold object pointer initializer designators with the actual
6855 // type parameter values of a particular instantiation.
6856 void ResolveNamesVisitor::FinishDerivedTypeInstantiation(Scope &scope) {
6857   CHECK(scope.IsDerivedType() && !scope.symbol());
6858   if (DerivedTypeSpec * spec{scope.derivedTypeSpec()}) {
6859     spec->Instantiate(currScope(), context());
6860     const Symbol &origTypeSymbol{spec->typeSymbol()};
6861     if (const Scope * origTypeScope{origTypeSymbol.scope()}) {
6862       CHECK(origTypeScope->IsDerivedType() &&
6863           origTypeScope->symbol() == &origTypeSymbol);
6864       auto &foldingContext{GetFoldingContext()};
6865       auto restorer{foldingContext.WithPDTInstance(*spec)};
6866       for (auto &pair : scope) {
6867         Symbol &comp{*pair.second};
6868         const Symbol &origComp{DEREF(FindInScope(*origTypeScope, comp.name()))};
6869         if (IsPointer(comp)) {
6870           if (auto *details{comp.detailsIf<ObjectEntityDetails>()}) {
6871             auto origDetails{origComp.get<ObjectEntityDetails>()};
6872             if (const MaybeExpr & init{origDetails.init()}) {
6873               SomeExpr newInit{*init};
6874               MaybeExpr folded{
6875                   evaluate::Fold(foldingContext, std::move(newInit))};
6876               details->set_init(std::move(folded));
6877             }
6878           }
6879         }
6880       }
6881     }
6882   }
6883 }
6884 
6885 // Resolve names in the execution part of this node and its children
6886 void ResolveNamesVisitor::ResolveExecutionParts(const ProgramTree &node) {
6887   if (!node.scope()) {
6888     return; // error occurred creating scope
6889   }
6890   SetScope(*node.scope());
6891   if (const auto *exec{node.exec()}) {
6892     Walk(*exec);
6893   }
6894   PopScope(); // converts unclassified entities into objects
6895   for (const auto &child : node.children()) {
6896     ResolveExecutionParts(child);
6897   }
6898 }
6899 
6900 void ResolveNamesVisitor::ResolveOmpParts(const parser::ProgramUnit &node) {
6901   OmpAttributeVisitor{context(), *this}.Walk(node);
6902   if (!context().AnyFatalError()) {
6903     // The data-sharing attribute of the loop iteration variable for a
6904     // sequential loop (2.15.1.1) can only be determined when visiting
6905     // the corresponding DoConstruct, a second walk is to adjust the
6906     // symbols for all the data-refs of that loop iteration variable
6907     // prior to the DoConstruct.
6908     OmpAttributeVisitor{context(), *this}.Walk(node);
6909   }
6910 }
6911 
6912 void ResolveNamesVisitor::Post(const parser::Program &) {
6913   // ensure that all temps were deallocated
6914   CHECK(!attrs_);
6915   CHECK(!GetDeclTypeSpec());
6916 }
6917 
6918 // A singleton instance of the scope -> IMPLICIT rules mapping is
6919 // shared by all instances of ResolveNamesVisitor and accessed by this
6920 // pointer when the visitors (other than the top-level original) are
6921 // constructed.
6922 static ImplicitRulesMap *sharedImplicitRulesMap{nullptr};
6923 
6924 bool ResolveNames(SemanticsContext &context, const parser::Program &program) {
6925   ImplicitRulesMap implicitRulesMap;
6926   auto restorer{common::ScopedSet(sharedImplicitRulesMap, &implicitRulesMap)};
6927   ResolveNamesVisitor{context, implicitRulesMap}.Walk(program);
6928   return !context.AnyFatalError();
6929 }
6930 
6931 // Processes a module (but not internal) function when it is referenced
6932 // in a specification expression in a sibling procedure.
6933 void ResolveSpecificationParts(
6934     SemanticsContext &context, const Symbol &subprogram) {
6935   auto originalLocation{context.location()};
6936   ResolveNamesVisitor visitor{context, DEREF(sharedImplicitRulesMap)};
6937   ProgramTree &node{subprogram.get<SubprogramNameDetails>().node()};
6938   const Scope &moduleScope{subprogram.owner()};
6939   visitor.SetScope(const_cast<Scope &>(moduleScope));
6940   visitor.ResolveSpecificationParts(node);
6941   context.set_location(std::move(originalLocation));
6942 }
6943 } // namespace Fortran::semantics
6944