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