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 (IsNamedConstant(*symbol)) {
5345       Say(name,
5346           "A named constant '%s' may not appear in a COMMON block"_err_en_US);
5347     } else if (IsDummy(*symbol)) {
5348       Say(name,
5349           "Dummy argument '%s' may not appear in a COMMON block"_err_en_US);
5350     } else if (symbol->IsFuncResult()) {
5351       Say(name,
5352           "Function result '%s' may not appear in a COMMON block"_err_en_US);
5353     } else if (const DeclTypeSpec * type{symbol->GetType()}) {
5354       if (type->category() == DeclTypeSpec::ClassStar) {
5355         Say(name,
5356             "Unlimited polymorphic pointer '%s' may not appear in a COMMON block"_err_en_US);
5357       } else if (const auto *derived{type->AsDerived()}) {
5358         auto &typeSymbol{derived->typeSymbol()};
5359         if (!typeSymbol.attrs().test(Attr::BIND_C) &&
5360             !typeSymbol.get<DerivedTypeDetails>().sequence()) {
5361           Say(name,
5362               "Derived type '%s' in COMMON block must have the BIND or"
5363               " SEQUENCE attribute"_err_en_US);
5364         }
5365         CheckCommonBlockDerivedType(name, typeSymbol);
5366       }
5367     }
5368   }
5369   specPartState_.commonBlockObjects = {};
5370 }
5371 
5372 Symbol &DeclarationVisitor::MakeCommonBlockSymbol(const parser::Name &name) {
5373   return Resolve(name, currScope().MakeCommonBlock(name.source));
5374 }
5375 Symbol &DeclarationVisitor::MakeCommonBlockSymbol(
5376     const std::optional<parser::Name> &name) {
5377   if (name) {
5378     return MakeCommonBlockSymbol(*name);
5379   } else {
5380     return MakeCommonBlockSymbol(parser::Name{});
5381   }
5382 }
5383 
5384 bool DeclarationVisitor::NameIsKnownOrIntrinsic(const parser::Name &name) {
5385   return FindSymbol(name) || HandleUnrestrictedSpecificIntrinsicFunction(name);
5386 }
5387 
5388 // Check if this derived type can be in a COMMON block.
5389 void DeclarationVisitor::CheckCommonBlockDerivedType(
5390     const SourceName &name, const Symbol &typeSymbol) {
5391   if (const auto *scope{typeSymbol.scope()}) {
5392     for (const auto &pair : *scope) {
5393       const Symbol &component{*pair.second};
5394       if (component.attrs().test(Attr::ALLOCATABLE)) {
5395         Say2(name,
5396             "Derived type variable '%s' may not appear in a COMMON block"
5397             " due to ALLOCATABLE component"_err_en_US,
5398             component.name(), "Component with ALLOCATABLE attribute"_en_US);
5399         return;
5400       }
5401       const auto *details{component.detailsIf<ObjectEntityDetails>()};
5402       if (component.test(Symbol::Flag::InDataStmt) ||
5403           (details && details->init())) {
5404         Say2(name,
5405             "Derived type variable '%s' may not appear in a COMMON block due to component with default initialization"_err_en_US,
5406             component.name(), "Component with default initialization"_en_US);
5407         return;
5408       }
5409       if (details) {
5410         if (const auto *type{details->type()}) {
5411           if (const auto *derived{type->AsDerived()}) {
5412             CheckCommonBlockDerivedType(name, derived->typeSymbol());
5413           }
5414         }
5415       }
5416     }
5417   }
5418 }
5419 
5420 bool DeclarationVisitor::HandleUnrestrictedSpecificIntrinsicFunction(
5421     const parser::Name &name) {
5422   if (auto interface{context().intrinsics().IsSpecificIntrinsicFunction(
5423           name.source.ToString())}) {
5424     // Unrestricted specific intrinsic function names (e.g., "cos")
5425     // are acceptable as procedure interfaces.  The presence of the
5426     // INTRINSIC flag will cause this symbol to have a complete interface
5427     // recreated for it later on demand, but capturing its result type here
5428     // will make GetType() return a correct result without having to
5429     // probe the intrinsics table again.
5430     Symbol &symbol{
5431         MakeSymbol(InclusiveScope(), name.source, Attrs{Attr::INTRINSIC})};
5432     CHECK(interface->functionResult.has_value());
5433     evaluate::DynamicType dyType{
5434         DEREF(interface->functionResult->GetTypeAndShape()).type()};
5435     CHECK(common::IsNumericTypeCategory(dyType.category()));
5436     const DeclTypeSpec &typeSpec{
5437         MakeNumericType(dyType.category(), dyType.kind())};
5438     ProcEntityDetails details;
5439     ProcInterface procInterface;
5440     procInterface.set_type(typeSpec);
5441     details.set_interface(procInterface);
5442     symbol.set_details(std::move(details));
5443     symbol.set(Symbol::Flag::Function);
5444     if (interface->IsElemental()) {
5445       symbol.attrs().set(Attr::ELEMENTAL);
5446     }
5447     if (interface->IsPure()) {
5448       symbol.attrs().set(Attr::PURE);
5449     }
5450     Resolve(name, symbol);
5451     return true;
5452   } else {
5453     return false;
5454   }
5455 }
5456 
5457 // Checks for all locality-specs: LOCAL, LOCAL_INIT, and SHARED
5458 bool DeclarationVisitor::PassesSharedLocalityChecks(
5459     const parser::Name &name, Symbol &symbol) {
5460   if (!IsVariableName(symbol)) {
5461     SayLocalMustBeVariable(name, symbol); // C1124
5462     return false;
5463   }
5464   if (symbol.owner() == currScope()) { // C1125 and C1126
5465     SayAlreadyDeclared(name, symbol);
5466     return false;
5467   }
5468   return true;
5469 }
5470 
5471 // Checks for locality-specs LOCAL and LOCAL_INIT
5472 bool DeclarationVisitor::PassesLocalityChecks(
5473     const parser::Name &name, Symbol &symbol) {
5474   if (IsAllocatable(symbol)) { // C1128
5475     SayWithDecl(name, symbol,
5476         "ALLOCATABLE variable '%s' not allowed in a locality-spec"_err_en_US);
5477     return false;
5478   }
5479   if (IsOptional(symbol)) { // C1128
5480     SayWithDecl(name, symbol,
5481         "OPTIONAL argument '%s' not allowed in a locality-spec"_err_en_US);
5482     return false;
5483   }
5484   if (IsIntentIn(symbol)) { // C1128
5485     SayWithDecl(name, symbol,
5486         "INTENT IN argument '%s' not allowed in a locality-spec"_err_en_US);
5487     return false;
5488   }
5489   if (IsFinalizable(symbol)) { // C1128
5490     SayWithDecl(name, symbol,
5491         "Finalizable variable '%s' not allowed in a locality-spec"_err_en_US);
5492     return false;
5493   }
5494   if (evaluate::IsCoarray(symbol)) { // C1128
5495     SayWithDecl(
5496         name, symbol, "Coarray '%s' not allowed in a locality-spec"_err_en_US);
5497     return false;
5498   }
5499   if (const DeclTypeSpec * type{symbol.GetType()}) {
5500     if (type->IsPolymorphic() && IsDummy(symbol) &&
5501         !IsPointer(symbol)) { // C1128
5502       SayWithDecl(name, symbol,
5503           "Nonpointer polymorphic argument '%s' not allowed in a "
5504           "locality-spec"_err_en_US);
5505       return false;
5506     }
5507   }
5508   if (IsAssumedSizeArray(symbol)) { // C1128
5509     SayWithDecl(name, symbol,
5510         "Assumed size array '%s' not allowed in a locality-spec"_err_en_US);
5511     return false;
5512   }
5513   if (std::optional<Message> msg{WhyNotModifiable(symbol, currScope())}) {
5514     SayWithReason(name, symbol,
5515         "'%s' may not appear in a locality-spec because it is not "
5516         "definable"_err_en_US,
5517         std::move(*msg));
5518     return false;
5519   }
5520   return PassesSharedLocalityChecks(name, symbol);
5521 }
5522 
5523 Symbol &DeclarationVisitor::FindOrDeclareEnclosingEntity(
5524     const parser::Name &name) {
5525   Symbol *prev{FindSymbol(name)};
5526   if (!prev) {
5527     // Declare the name as an object in the enclosing scope so that
5528     // the name can't be repurposed there later as something else.
5529     prev = &MakeSymbol(InclusiveScope(), name.source, Attrs{});
5530     ConvertToObjectEntity(*prev);
5531     ApplyImplicitRules(*prev);
5532   }
5533   return *prev;
5534 }
5535 
5536 Symbol *DeclarationVisitor::DeclareLocalEntity(const parser::Name &name) {
5537   Symbol &prev{FindOrDeclareEnclosingEntity(name)};
5538   if (!PassesLocalityChecks(name, prev)) {
5539     return nullptr;
5540   }
5541   return &MakeHostAssocSymbol(name, prev);
5542 }
5543 
5544 Symbol *DeclarationVisitor::DeclareStatementEntity(
5545     const parser::DoVariable &doVar,
5546     const std::optional<parser::IntegerTypeSpec> &type) {
5547   const parser::Name &name{doVar.thing.thing};
5548   const DeclTypeSpec *declTypeSpec{nullptr};
5549   if (auto *prev{FindSymbol(name)}) {
5550     if (prev->owner() == currScope()) {
5551       SayAlreadyDeclared(name, *prev);
5552       return nullptr;
5553     }
5554     name.symbol = nullptr;
5555     declTypeSpec = prev->GetType();
5556   }
5557   Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, {})};
5558   if (!symbol.has<ObjectEntityDetails>()) {
5559     return nullptr; // error was reported in DeclareEntity
5560   }
5561   if (type) {
5562     declTypeSpec = ProcessTypeSpec(*type);
5563   }
5564   if (declTypeSpec) {
5565     // Subtlety: Don't let a "*length" specifier (if any is pending) affect the
5566     // declaration of this implied DO loop control variable.
5567     auto restorer{
5568         common::ScopedSet(charInfo_.length, std::optional<ParamValue>{})};
5569     SetType(name, *declTypeSpec);
5570   } else {
5571     ApplyImplicitRules(symbol);
5572   }
5573   Symbol *result{Resolve(name, &symbol)};
5574   AnalyzeExpr(context(), doVar); // enforce INTEGER type
5575   return result;
5576 }
5577 
5578 // Set the type of an entity or report an error.
5579 void DeclarationVisitor::SetType(
5580     const parser::Name &name, const DeclTypeSpec &type) {
5581   CHECK(name.symbol);
5582   auto &symbol{*name.symbol};
5583   if (charInfo_.length) { // Declaration has "*length" (R723)
5584     auto length{std::move(*charInfo_.length)};
5585     charInfo_.length.reset();
5586     if (type.category() == DeclTypeSpec::Character) {
5587       auto kind{type.characterTypeSpec().kind()};
5588       // Recurse with correct type.
5589       SetType(name,
5590           currScope().MakeCharacterType(std::move(length), std::move(kind)));
5591       return;
5592     } else { // C753
5593       Say(name,
5594           "A length specifier cannot be used to declare the non-character entity '%s'"_err_en_US);
5595     }
5596   }
5597   auto *prevType{symbol.GetType()};
5598   if (!prevType) {
5599     symbol.SetType(type);
5600   } else if (symbol.has<UseDetails>()) {
5601     // error recovery case, redeclaration of use-associated name
5602   } else if (HadForwardRef(symbol)) {
5603     // error recovery after use of host-associated name
5604   } else if (!symbol.test(Symbol::Flag::Implicit)) {
5605     SayWithDecl(
5606         name, symbol, "The type of '%s' has already been declared"_err_en_US);
5607     context().SetError(symbol);
5608   } else if (type != *prevType) {
5609     SayWithDecl(name, symbol,
5610         "The type of '%s' has already been implicitly declared"_err_en_US);
5611     context().SetError(symbol);
5612   } else {
5613     symbol.set(Symbol::Flag::Implicit, false);
5614   }
5615 }
5616 
5617 std::optional<DerivedTypeSpec> DeclarationVisitor::ResolveDerivedType(
5618     const parser::Name &name) {
5619   Scope &outer{NonDerivedTypeScope()};
5620   Symbol *symbol{FindSymbol(outer, name)};
5621   Symbol *ultimate{symbol ? &symbol->GetUltimate() : nullptr};
5622   auto *generic{ultimate ? ultimate->detailsIf<GenericDetails>() : nullptr};
5623   if (generic) {
5624     if (Symbol * genDT{generic->derivedType()}) {
5625       symbol = genDT;
5626       generic = nullptr;
5627     }
5628   }
5629   if (!symbol || symbol->has<UnknownDetails>() ||
5630       (generic && &ultimate->owner() == &outer)) {
5631     if (allowForwardReferenceToDerivedType()) {
5632       if (!symbol) {
5633         symbol = &MakeSymbol(outer, name.source, Attrs{});
5634         Resolve(name, *symbol);
5635       } else if (generic) {
5636         // forward ref to type with later homonymous generic
5637         symbol = &outer.MakeSymbol(name.source, Attrs{}, UnknownDetails{});
5638         generic->set_derivedType(*symbol);
5639         name.symbol = symbol;
5640       }
5641       DerivedTypeDetails details;
5642       details.set_isForwardReferenced(true);
5643       symbol->set_details(std::move(details));
5644     } else { // C732
5645       Say(name, "Derived type '%s' not found"_err_en_US);
5646       return std::nullopt;
5647     }
5648   }
5649   if (CheckUseError(name)) {
5650     return std::nullopt;
5651   }
5652   symbol = &symbol->GetUltimate();
5653   if (symbol->has<DerivedTypeDetails>()) {
5654     return DerivedTypeSpec{name.source, *symbol};
5655   } else {
5656     Say(name, "'%s' is not a derived type"_err_en_US);
5657     return std::nullopt;
5658   }
5659 }
5660 
5661 std::optional<DerivedTypeSpec> DeclarationVisitor::ResolveExtendsType(
5662     const parser::Name &typeName, const parser::Name *extendsName) {
5663   if (!extendsName) {
5664     return std::nullopt;
5665   } else if (typeName.source == extendsName->source) {
5666     Say(extendsName->source,
5667         "Derived type '%s' cannot extend itself"_err_en_US);
5668     return std::nullopt;
5669   } else {
5670     return ResolveDerivedType(*extendsName);
5671   }
5672 }
5673 
5674 Symbol *DeclarationVisitor::NoteInterfaceName(const parser::Name &name) {
5675   // The symbol is checked later by CheckExplicitInterface() and
5676   // CheckBindings().  It can be a forward reference.
5677   if (!NameIsKnownOrIntrinsic(name)) {
5678     Symbol &symbol{MakeSymbol(InclusiveScope(), name.source, Attrs{})};
5679     Resolve(name, symbol);
5680   }
5681   return name.symbol;
5682 }
5683 
5684 void DeclarationVisitor::CheckExplicitInterface(const parser::Name &name) {
5685   if (const Symbol * symbol{name.symbol}) {
5686     if (!context().HasError(*symbol) && !symbol->HasExplicitInterface()) {
5687       Say(name,
5688           "'%s' must be an abstract interface or a procedure with "
5689           "an explicit interface"_err_en_US,
5690           symbol->name());
5691     }
5692   }
5693 }
5694 
5695 // Create a symbol for a type parameter, component, or procedure binding in
5696 // the current derived type scope. Return false on error.
5697 Symbol *DeclarationVisitor::MakeTypeSymbol(
5698     const parser::Name &name, Details &&details) {
5699   return Resolve(name, MakeTypeSymbol(name.source, std::move(details)));
5700 }
5701 Symbol *DeclarationVisitor::MakeTypeSymbol(
5702     const SourceName &name, Details &&details) {
5703   Scope &derivedType{currScope()};
5704   CHECK(derivedType.IsDerivedType());
5705   if (auto *symbol{FindInScope(derivedType, name)}) { // C742
5706     Say2(name,
5707         "Type parameter, component, or procedure binding '%s'"
5708         " already defined in this type"_err_en_US,
5709         *symbol, "Previous definition of '%s'"_en_US);
5710     return nullptr;
5711   } else {
5712     auto attrs{GetAttrs()};
5713     // Apply binding-private-stmt if present and this is a procedure binding
5714     if (derivedTypeInfo_.privateBindings &&
5715         !attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE}) &&
5716         std::holds_alternative<ProcBindingDetails>(details)) {
5717       attrs.set(Attr::PRIVATE);
5718     }
5719     Symbol &result{MakeSymbol(name, attrs, std::move(details))};
5720     if (result.has<TypeParamDetails>()) {
5721       derivedType.symbol()->get<DerivedTypeDetails>().add_paramDecl(result);
5722     }
5723     return &result;
5724   }
5725 }
5726 
5727 // Return true if it is ok to declare this component in the current scope.
5728 // Otherwise, emit an error and return false.
5729 bool DeclarationVisitor::OkToAddComponent(
5730     const parser::Name &name, const Symbol *extends) {
5731   for (const Scope *scope{&currScope()}; scope;) {
5732     CHECK(scope->IsDerivedType());
5733     if (auto *prev{FindInScope(*scope, name)}) {
5734       if (!context().HasError(*prev)) {
5735         parser::MessageFixedText msg;
5736         if (extends) {
5737           msg = "Type cannot be extended as it has a component named"
5738                 " '%s'"_err_en_US;
5739         } else if (prev->test(Symbol::Flag::ParentComp)) {
5740           msg = "'%s' is a parent type of this type and so cannot be"
5741                 " a component"_err_en_US;
5742         } else if (scope != &currScope()) {
5743           msg = "Component '%s' is already declared in a parent of this"
5744                 " derived type"_err_en_US;
5745         } else {
5746           msg = "Component '%s' is already declared in this"
5747                 " derived type"_err_en_US;
5748         }
5749         Say2(name, std::move(msg), *prev, "Previous declaration of '%s'"_en_US);
5750       }
5751       return false;
5752     }
5753     if (scope == &currScope() && extends) {
5754       // The parent component has not yet been added to the scope.
5755       scope = extends->scope();
5756     } else {
5757       scope = scope->GetDerivedTypeParent();
5758     }
5759   }
5760   return true;
5761 }
5762 
5763 ParamValue DeclarationVisitor::GetParamValue(
5764     const parser::TypeParamValue &x, common::TypeParamAttr attr) {
5765   return common::visit(
5766       common::visitors{
5767           [=](const parser::ScalarIntExpr &x) { // C704
5768             return ParamValue{EvaluateIntExpr(x), attr};
5769           },
5770           [=](const parser::Star &) { return ParamValue::Assumed(attr); },
5771           [=](const parser::TypeParamValue::Deferred &) {
5772             return ParamValue::Deferred(attr);
5773           },
5774       },
5775       x.u);
5776 }
5777 
5778 // ConstructVisitor implementation
5779 
5780 void ConstructVisitor::ResolveIndexName(
5781     const parser::ConcurrentControl &control) {
5782   const parser::Name &name{std::get<parser::Name>(control.t)};
5783   auto *prev{FindSymbol(name)};
5784   if (prev) {
5785     if (prev->owner().kind() == Scope::Kind::Forall ||
5786         prev->owner() == currScope()) {
5787       SayAlreadyDeclared(name, *prev);
5788       return;
5789     }
5790     name.symbol = nullptr;
5791   }
5792   auto &symbol{DeclareObjectEntity(name)};
5793   if (symbol.GetType()) {
5794     // type came from explicit type-spec
5795   } else if (!prev) {
5796     ApplyImplicitRules(symbol);
5797   } else {
5798     const Symbol &prevRoot{ResolveAssociations(*prev)};
5799     // prev could be host- use- or construct-associated with another symbol
5800     if (!prevRoot.has<ObjectEntityDetails>() &&
5801         !prevRoot.has<EntityDetails>()) {
5802       Say2(name, "Index name '%s' conflicts with existing identifier"_err_en_US,
5803           *prev, "Previous declaration of '%s'"_en_US);
5804       context().SetError(symbol);
5805       return;
5806     } else {
5807       if (const auto *type{prevRoot.GetType()}) {
5808         symbol.SetType(*type);
5809       }
5810       if (prevRoot.IsObjectArray()) {
5811         SayWithDecl(name, *prev, "Index variable '%s' is not scalar"_err_en_US);
5812         return;
5813       }
5814     }
5815   }
5816   EvaluateExpr(parser::Scalar{parser::Integer{common::Clone(name)}});
5817 }
5818 
5819 // We need to make sure that all of the index-names get declared before the
5820 // expressions in the loop control are evaluated so that references to the
5821 // index-names in the expressions are correctly detected.
5822 bool ConstructVisitor::Pre(const parser::ConcurrentHeader &header) {
5823   BeginDeclTypeSpec();
5824   Walk(std::get<std::optional<parser::IntegerTypeSpec>>(header.t));
5825   const auto &controls{
5826       std::get<std::list<parser::ConcurrentControl>>(header.t)};
5827   for (const auto &control : controls) {
5828     ResolveIndexName(control);
5829   }
5830   Walk(controls);
5831   Walk(std::get<std::optional<parser::ScalarLogicalExpr>>(header.t));
5832   EndDeclTypeSpec();
5833   return false;
5834 }
5835 
5836 bool ConstructVisitor::Pre(const parser::LocalitySpec::Local &x) {
5837   for (auto &name : x.v) {
5838     if (auto *symbol{DeclareLocalEntity(name)}) {
5839       symbol->set(Symbol::Flag::LocalityLocal);
5840     }
5841   }
5842   return false;
5843 }
5844 
5845 bool ConstructVisitor::Pre(const parser::LocalitySpec::LocalInit &x) {
5846   for (auto &name : x.v) {
5847     if (auto *symbol{DeclareLocalEntity(name)}) {
5848       symbol->set(Symbol::Flag::LocalityLocalInit);
5849     }
5850   }
5851   return false;
5852 }
5853 
5854 bool ConstructVisitor::Pre(const parser::LocalitySpec::Shared &x) {
5855   for (const auto &name : x.v) {
5856     if (!FindSymbol(name)) {
5857       Say(name,
5858           "Variable '%s' with SHARED locality implicitly declared"_warn_en_US);
5859     }
5860     Symbol &prev{FindOrDeclareEnclosingEntity(name)};
5861     if (PassesSharedLocalityChecks(name, prev)) {
5862       MakeHostAssocSymbol(name, prev).set(Symbol::Flag::LocalityShared);
5863     }
5864   }
5865   return false;
5866 }
5867 
5868 bool ConstructVisitor::Pre(const parser::AcSpec &x) {
5869   ProcessTypeSpec(x.type);
5870   Walk(x.values);
5871   return false;
5872 }
5873 
5874 // Section 19.4, paragraph 5 says that each ac-do-variable has the scope of the
5875 // enclosing ac-implied-do
5876 bool ConstructVisitor::Pre(const parser::AcImpliedDo &x) {
5877   auto &values{std::get<std::list<parser::AcValue>>(x.t)};
5878   auto &control{std::get<parser::AcImpliedDoControl>(x.t)};
5879   auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(control.t)};
5880   auto &bounds{std::get<parser::AcImpliedDoControl::Bounds>(control.t)};
5881   // F'2018 has the scope of the implied DO variable covering the entire
5882   // implied DO production (19.4(5)), which seems wrong in cases where the name
5883   // of the implied DO variable appears in one of the bound expressions. Thus
5884   // this extension, which shrinks the scope of the variable to exclude the
5885   // expressions in the bounds.
5886   auto restore{BeginCheckOnIndexUseInOwnBounds(bounds.name)};
5887   Walk(bounds.lower);
5888   Walk(bounds.upper);
5889   Walk(bounds.step);
5890   EndCheckOnIndexUseInOwnBounds(restore);
5891   PushScope(Scope::Kind::ImpliedDos, nullptr);
5892   DeclareStatementEntity(bounds.name, type);
5893   Walk(values);
5894   PopScope();
5895   return false;
5896 }
5897 
5898 bool ConstructVisitor::Pre(const parser::DataImpliedDo &x) {
5899   auto &objects{std::get<std::list<parser::DataIDoObject>>(x.t)};
5900   auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(x.t)};
5901   auto &bounds{std::get<parser::DataImpliedDo::Bounds>(x.t)};
5902   // See comment in Pre(AcImpliedDo) above.
5903   auto restore{BeginCheckOnIndexUseInOwnBounds(bounds.name)};
5904   Walk(bounds.lower);
5905   Walk(bounds.upper);
5906   Walk(bounds.step);
5907   EndCheckOnIndexUseInOwnBounds(restore);
5908   bool pushScope{currScope().kind() != Scope::Kind::ImpliedDos};
5909   if (pushScope) {
5910     PushScope(Scope::Kind::ImpliedDos, nullptr);
5911   }
5912   DeclareStatementEntity(bounds.name, type);
5913   Walk(objects);
5914   if (pushScope) {
5915     PopScope();
5916   }
5917   return false;
5918 }
5919 
5920 // Sets InDataStmt flag on a variable (or misidentified function) in a DATA
5921 // statement so that the predicate IsInitialized() will be true
5922 // during semantic analysis before the symbol's initializer is constructed.
5923 bool ConstructVisitor::Pre(const parser::DataIDoObject &x) {
5924   common::visit(
5925       common::visitors{
5926           [&](const parser::Scalar<Indirection<parser::Designator>> &y) {
5927             Walk(y.thing.value());
5928             const parser::Name &first{parser::GetFirstName(y.thing.value())};
5929             if (first.symbol) {
5930               first.symbol->set(Symbol::Flag::InDataStmt);
5931             }
5932           },
5933           [&](const Indirection<parser::DataImpliedDo> &y) { Walk(y.value()); },
5934       },
5935       x.u);
5936   return false;
5937 }
5938 
5939 bool ConstructVisitor::Pre(const parser::DataStmtObject &x) {
5940   // Subtle: DATA statements may appear in both the specification and
5941   // execution parts, but should be treated as if in the execution part
5942   // for purposes of implicit variable declaration vs. host association.
5943   // When a name first appears as an object in a DATA statement, it should
5944   // be implicitly declared locally as if it had been assigned.
5945   auto flagRestorer{common::ScopedSet(inSpecificationPart_, false)};
5946   common::visit(common::visitors{
5947                     [&](const Indirection<parser::Variable> &y) {
5948                       Walk(y.value());
5949                       const parser::Name &first{
5950                           parser::GetFirstName(y.value())};
5951                       if (first.symbol) {
5952                         first.symbol->set(Symbol::Flag::InDataStmt);
5953                       }
5954                     },
5955                     [&](const parser::DataImpliedDo &y) {
5956                       PushScope(Scope::Kind::ImpliedDos, nullptr);
5957                       Walk(y);
5958                       PopScope();
5959                     },
5960                 },
5961       x.u);
5962   return false;
5963 }
5964 
5965 bool ConstructVisitor::Pre(const parser::DataStmtValue &x) {
5966   const auto &data{std::get<parser::DataStmtConstant>(x.t)};
5967   auto &mutableData{const_cast<parser::DataStmtConstant &>(data)};
5968   if (auto *elem{parser::Unwrap<parser::ArrayElement>(mutableData)}) {
5969     if (const auto *name{std::get_if<parser::Name>(&elem->base.u)}) {
5970       if (const Symbol * symbol{FindSymbol(*name)}) {
5971         const Symbol &ultimate{symbol->GetUltimate()};
5972         if (ultimate.has<DerivedTypeDetails>()) {
5973           mutableData.u = elem->ConvertToStructureConstructor(
5974               DerivedTypeSpec{name->source, ultimate});
5975         }
5976       }
5977     }
5978   }
5979   return true;
5980 }
5981 
5982 bool ConstructVisitor::Pre(const parser::DoConstruct &x) {
5983   if (x.IsDoConcurrent()) {
5984     PushScope(Scope::Kind::Block, nullptr);
5985   }
5986   return true;
5987 }
5988 void ConstructVisitor::Post(const parser::DoConstruct &x) {
5989   if (x.IsDoConcurrent()) {
5990     PopScope();
5991   }
5992 }
5993 
5994 bool ConstructVisitor::Pre(const parser::ForallConstruct &) {
5995   PushScope(Scope::Kind::Forall, nullptr);
5996   return true;
5997 }
5998 void ConstructVisitor::Post(const parser::ForallConstruct &) { PopScope(); }
5999 bool ConstructVisitor::Pre(const parser::ForallStmt &) {
6000   PushScope(Scope::Kind::Forall, nullptr);
6001   return true;
6002 }
6003 void ConstructVisitor::Post(const parser::ForallStmt &) { PopScope(); }
6004 
6005 bool ConstructVisitor::Pre(const parser::BlockStmt &x) {
6006   CheckDef(x.v);
6007   PushScope(Scope::Kind::Block, nullptr);
6008   return false;
6009 }
6010 bool ConstructVisitor::Pre(const parser::EndBlockStmt &x) {
6011   PopScope();
6012   CheckRef(x.v);
6013   return false;
6014 }
6015 
6016 void ConstructVisitor::Post(const parser::Selector &x) {
6017   GetCurrentAssociation().selector = ResolveSelector(x);
6018 }
6019 
6020 void ConstructVisitor::Post(const parser::AssociateStmt &x) {
6021   CheckDef(x.t);
6022   PushScope(Scope::Kind::Block, nullptr);
6023   const auto assocCount{std::get<std::list<parser::Association>>(x.t).size()};
6024   for (auto nthLastAssoc{assocCount}; nthLastAssoc > 0; --nthLastAssoc) {
6025     SetCurrentAssociation(nthLastAssoc);
6026     if (auto *symbol{MakeAssocEntity()}) {
6027       if (ExtractCoarrayRef(GetCurrentAssociation().selector.expr)) { // C1103
6028         Say("Selector must not be a coindexed object"_err_en_US);
6029       }
6030       SetTypeFromAssociation(*symbol);
6031       SetAttrsFromAssociation(*symbol);
6032     }
6033   }
6034   PopAssociation(assocCount);
6035 }
6036 
6037 void ConstructVisitor::Post(const parser::EndAssociateStmt &x) {
6038   PopScope();
6039   CheckRef(x.v);
6040 }
6041 
6042 bool ConstructVisitor::Pre(const parser::Association &x) {
6043   PushAssociation();
6044   const auto &name{std::get<parser::Name>(x.t)};
6045   GetCurrentAssociation().name = &name;
6046   return true;
6047 }
6048 
6049 bool ConstructVisitor::Pre(const parser::ChangeTeamStmt &x) {
6050   CheckDef(x.t);
6051   PushScope(Scope::Kind::Block, nullptr);
6052   PushAssociation();
6053   return true;
6054 }
6055 
6056 void ConstructVisitor::Post(const parser::CoarrayAssociation &x) {
6057   const auto &decl{std::get<parser::CodimensionDecl>(x.t)};
6058   const auto &name{std::get<parser::Name>(decl.t)};
6059   if (auto *symbol{FindInScope(name)}) {
6060     const auto &selector{std::get<parser::Selector>(x.t)};
6061     if (auto sel{ResolveSelector(selector)}) {
6062       const Symbol *whole{UnwrapWholeSymbolDataRef(sel.expr)};
6063       if (!whole || whole->Corank() == 0) {
6064         Say(sel.source, // C1116
6065             "Selector in coarray association must name a coarray"_err_en_US);
6066       } else if (auto dynType{sel.expr->GetType()}) {
6067         if (!symbol->GetType()) {
6068           symbol->SetType(ToDeclTypeSpec(std::move(*dynType)));
6069         }
6070       }
6071     }
6072   }
6073 }
6074 
6075 void ConstructVisitor::Post(const parser::EndChangeTeamStmt &x) {
6076   PopAssociation();
6077   PopScope();
6078   CheckRef(x.t);
6079 }
6080 
6081 bool ConstructVisitor::Pre(const parser::SelectTypeConstruct &) {
6082   PushAssociation();
6083   return true;
6084 }
6085 
6086 void ConstructVisitor::Post(const parser::SelectTypeConstruct &) {
6087   PopAssociation();
6088 }
6089 
6090 void ConstructVisitor::Post(const parser::SelectTypeStmt &x) {
6091   auto &association{GetCurrentAssociation()};
6092   if (const std::optional<parser::Name> &name{std::get<1>(x.t)}) {
6093     // This isn't a name in the current scope, it is in each TypeGuardStmt
6094     MakePlaceholder(*name, MiscDetails::Kind::SelectTypeAssociateName);
6095     association.name = &*name;
6096     auto exprType{association.selector.expr->GetType()};
6097     if (ExtractCoarrayRef(association.selector.expr)) { // C1103
6098       Say("Selector must not be a coindexed object"_err_en_US);
6099     }
6100     if (exprType && !exprType->IsPolymorphic()) { // C1159
6101       Say(association.selector.source,
6102           "Selector '%s' in SELECT TYPE statement must be "
6103           "polymorphic"_err_en_US);
6104     }
6105   } else {
6106     if (const Symbol *
6107         whole{UnwrapWholeSymbolDataRef(association.selector.expr)}) {
6108       ConvertToObjectEntity(const_cast<Symbol &>(*whole));
6109       if (!IsVariableName(*whole)) {
6110         Say(association.selector.source, // C901
6111             "Selector is not a variable"_err_en_US);
6112         association = {};
6113       }
6114       if (const DeclTypeSpec * type{whole->GetType()}) {
6115         if (!type->IsPolymorphic()) { // C1159
6116           Say(association.selector.source,
6117               "Selector '%s' in SELECT TYPE statement must be "
6118               "polymorphic"_err_en_US);
6119         }
6120       }
6121     } else {
6122       Say(association.selector.source, // C1157
6123           "Selector is not a named variable: 'associate-name =>' is required"_err_en_US);
6124       association = {};
6125     }
6126   }
6127 }
6128 
6129 void ConstructVisitor::Post(const parser::SelectRankStmt &x) {
6130   auto &association{GetCurrentAssociation()};
6131   if (const std::optional<parser::Name> &name{std::get<1>(x.t)}) {
6132     // This isn't a name in the current scope, it is in each SelectRankCaseStmt
6133     MakePlaceholder(*name, MiscDetails::Kind::SelectRankAssociateName);
6134     association.name = &*name;
6135   }
6136 }
6137 
6138 bool ConstructVisitor::Pre(const parser::SelectTypeConstruct::TypeCase &) {
6139   PushScope(Scope::Kind::Block, nullptr);
6140   return true;
6141 }
6142 void ConstructVisitor::Post(const parser::SelectTypeConstruct::TypeCase &) {
6143   PopScope();
6144 }
6145 
6146 bool ConstructVisitor::Pre(const parser::SelectRankConstruct::RankCase &) {
6147   PushScope(Scope::Kind::Block, nullptr);
6148   return true;
6149 }
6150 void ConstructVisitor::Post(const parser::SelectRankConstruct::RankCase &) {
6151   PopScope();
6152 }
6153 
6154 void ConstructVisitor::Post(const parser::TypeGuardStmt::Guard &x) {
6155   if (auto *symbol{MakeAssocEntity()}) {
6156     if (std::holds_alternative<parser::Default>(x.u)) {
6157       SetTypeFromAssociation(*symbol);
6158     } else if (const auto *type{GetDeclTypeSpec()}) {
6159       symbol->SetType(*type);
6160     }
6161     SetAttrsFromAssociation(*symbol);
6162   }
6163 }
6164 
6165 void ConstructVisitor::Post(const parser::SelectRankCaseStmt::Rank &x) {
6166   if (auto *symbol{MakeAssocEntity()}) {
6167     SetTypeFromAssociation(*symbol);
6168     SetAttrsFromAssociation(*symbol);
6169     if (const auto *init{std::get_if<parser::ScalarIntConstantExpr>(&x.u)}) {
6170       if (auto val{EvaluateInt64(context(), *init)}) {
6171         auto &details{symbol->get<AssocEntityDetails>()};
6172         details.set_rank(*val);
6173       }
6174     }
6175   }
6176 }
6177 
6178 bool ConstructVisitor::Pre(const parser::SelectRankConstruct &) {
6179   PushAssociation();
6180   return true;
6181 }
6182 
6183 void ConstructVisitor::Post(const parser::SelectRankConstruct &) {
6184   PopAssociation();
6185 }
6186 
6187 bool ConstructVisitor::CheckDef(const std::optional<parser::Name> &x) {
6188   if (x) {
6189     MakeSymbol(*x, MiscDetails{MiscDetails::Kind::ConstructName});
6190   }
6191   return true;
6192 }
6193 
6194 void ConstructVisitor::CheckRef(const std::optional<parser::Name> &x) {
6195   if (x) {
6196     // Just add an occurrence of this name; checking is done in ValidateLabels
6197     FindSymbol(*x);
6198   }
6199 }
6200 
6201 // Make a symbol for the associating entity of the current association.
6202 Symbol *ConstructVisitor::MakeAssocEntity() {
6203   Symbol *symbol{nullptr};
6204   auto &association{GetCurrentAssociation()};
6205   if (association.name) {
6206     symbol = &MakeSymbol(*association.name, UnknownDetails{});
6207     if (symbol->has<AssocEntityDetails>() && symbol->owner() == currScope()) {
6208       Say(*association.name, // C1102
6209           "The associate name '%s' is already used in this associate statement"_err_en_US);
6210       return nullptr;
6211     }
6212   } else if (const Symbol *
6213       whole{UnwrapWholeSymbolDataRef(association.selector.expr)}) {
6214     symbol = &MakeSymbol(whole->name());
6215   } else {
6216     return nullptr;
6217   }
6218   if (auto &expr{association.selector.expr}) {
6219     symbol->set_details(AssocEntityDetails{common::Clone(*expr)});
6220   } else {
6221     symbol->set_details(AssocEntityDetails{});
6222   }
6223   return symbol;
6224 }
6225 
6226 // Set the type of symbol based on the current association selector.
6227 void ConstructVisitor::SetTypeFromAssociation(Symbol &symbol) {
6228   auto &details{symbol.get<AssocEntityDetails>()};
6229   const MaybeExpr *pexpr{&details.expr()};
6230   if (!*pexpr) {
6231     pexpr = &GetCurrentAssociation().selector.expr;
6232   }
6233   if (*pexpr) {
6234     const SomeExpr &expr{**pexpr};
6235     if (std::optional<evaluate::DynamicType> type{expr.GetType()}) {
6236       if (const auto *charExpr{
6237               evaluate::UnwrapExpr<evaluate::Expr<evaluate::SomeCharacter>>(
6238                   expr)}) {
6239         symbol.SetType(ToDeclTypeSpec(std::move(*type),
6240             FoldExpr(common::visit(
6241                 [](const auto &kindChar) { return kindChar.LEN(); },
6242                 charExpr->u))));
6243       } else {
6244         symbol.SetType(ToDeclTypeSpec(std::move(*type)));
6245       }
6246     } else {
6247       // BOZ literals, procedure designators, &c. are not acceptable
6248       Say(symbol.name(), "Associate name '%s' must have a type"_err_en_US);
6249     }
6250   }
6251 }
6252 
6253 // If current selector is a variable, set some of its attributes on symbol.
6254 void ConstructVisitor::SetAttrsFromAssociation(Symbol &symbol) {
6255   Attrs attrs{evaluate::GetAttrs(GetCurrentAssociation().selector.expr)};
6256   symbol.attrs() |= attrs &
6257       Attrs{Attr::TARGET, Attr::ASYNCHRONOUS, Attr::VOLATILE, Attr::CONTIGUOUS};
6258   if (attrs.test(Attr::POINTER)) {
6259     symbol.attrs().set(Attr::TARGET);
6260   }
6261 }
6262 
6263 ConstructVisitor::Selector ConstructVisitor::ResolveSelector(
6264     const parser::Selector &x) {
6265   return common::visit(common::visitors{
6266                            [&](const parser::Expr &expr) {
6267                              return Selector{expr.source, EvaluateExpr(x)};
6268                            },
6269                            [&](const parser::Variable &var) {
6270                              return Selector{var.GetSource(), EvaluateExpr(x)};
6271                            },
6272                        },
6273       x.u);
6274 }
6275 
6276 // Set the current association to the nth to the last association on the
6277 // association stack.  The top of the stack is at n = 1.  This allows access
6278 // to the interior of a list of associations at the top of the stack.
6279 void ConstructVisitor::SetCurrentAssociation(std::size_t n) {
6280   CHECK(n > 0 && n <= associationStack_.size());
6281   currentAssociation_ = &associationStack_[associationStack_.size() - n];
6282 }
6283 
6284 ConstructVisitor::Association &ConstructVisitor::GetCurrentAssociation() {
6285   CHECK(currentAssociation_);
6286   return *currentAssociation_;
6287 }
6288 
6289 void ConstructVisitor::PushAssociation() {
6290   associationStack_.emplace_back(Association{});
6291   currentAssociation_ = &associationStack_.back();
6292 }
6293 
6294 void ConstructVisitor::PopAssociation(std::size_t count) {
6295   CHECK(count > 0 && count <= associationStack_.size());
6296   associationStack_.resize(associationStack_.size() - count);
6297   currentAssociation_ =
6298       associationStack_.empty() ? nullptr : &associationStack_.back();
6299 }
6300 
6301 const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
6302     evaluate::DynamicType &&type) {
6303   switch (type.category()) {
6304     SWITCH_COVERS_ALL_CASES
6305   case common::TypeCategory::Integer:
6306   case common::TypeCategory::Real:
6307   case common::TypeCategory::Complex:
6308     return context().MakeNumericType(type.category(), type.kind());
6309   case common::TypeCategory::Logical:
6310     return context().MakeLogicalType(type.kind());
6311   case common::TypeCategory::Derived:
6312     if (type.IsAssumedType()) {
6313       return currScope().MakeTypeStarType();
6314     } else if (type.IsUnlimitedPolymorphic()) {
6315       return currScope().MakeClassStarType();
6316     } else {
6317       return currScope().MakeDerivedType(
6318           type.IsPolymorphic() ? DeclTypeSpec::ClassDerived
6319                                : DeclTypeSpec::TypeDerived,
6320           common::Clone(type.GetDerivedTypeSpec())
6321 
6322       );
6323     }
6324   case common::TypeCategory::Character:
6325     CRASH_NO_CASE;
6326   }
6327 }
6328 
6329 const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
6330     evaluate::DynamicType &&type, MaybeSubscriptIntExpr &&length) {
6331   CHECK(type.category() == common::TypeCategory::Character);
6332   if (length) {
6333     return currScope().MakeCharacterType(
6334         ParamValue{SomeIntExpr{*std::move(length)}, common::TypeParamAttr::Len},
6335         KindExpr{type.kind()});
6336   } else {
6337     return currScope().MakeCharacterType(
6338         ParamValue::Deferred(common::TypeParamAttr::Len),
6339         KindExpr{type.kind()});
6340   }
6341 }
6342 
6343 // ResolveNamesVisitor implementation
6344 
6345 bool ResolveNamesVisitor::Pre(const parser::FunctionReference &x) {
6346   HandleCall(Symbol::Flag::Function, x.v);
6347   return false;
6348 }
6349 bool ResolveNamesVisitor::Pre(const parser::CallStmt &x) {
6350   HandleCall(Symbol::Flag::Subroutine, x.v);
6351   return false;
6352 }
6353 
6354 bool ResolveNamesVisitor::Pre(const parser::ImportStmt &x) {
6355   auto &scope{currScope()};
6356   // Check C896 and C899: where IMPORT statements are allowed
6357   switch (scope.kind()) {
6358   case Scope::Kind::Module:
6359     if (scope.IsModule()) {
6360       Say("IMPORT is not allowed in a module scoping unit"_err_en_US);
6361       return false;
6362     } else if (x.kind == common::ImportKind::None) {
6363       Say("IMPORT,NONE is not allowed in a submodule scoping unit"_err_en_US);
6364       return false;
6365     }
6366     break;
6367   case Scope::Kind::MainProgram:
6368     Say("IMPORT is not allowed in a main program scoping unit"_err_en_US);
6369     return false;
6370   case Scope::Kind::Subprogram:
6371     if (scope.parent().IsGlobal()) {
6372       Say("IMPORT is not allowed in an external subprogram scoping unit"_err_en_US);
6373       return false;
6374     }
6375     break;
6376   case Scope::Kind::BlockData: // C1415 (in part)
6377     Say("IMPORT is not allowed in a BLOCK DATA subprogram"_err_en_US);
6378     return false;
6379   default:;
6380   }
6381   if (auto error{scope.SetImportKind(x.kind)}) {
6382     Say(std::move(*error));
6383   }
6384   for (auto &name : x.names) {
6385     if (FindSymbol(scope.parent(), name)) {
6386       scope.add_importName(name.source);
6387     } else {
6388       Say(name, "'%s' not found in host scope"_err_en_US);
6389     }
6390   }
6391   prevImportStmt_ = currStmtSource();
6392   return false;
6393 }
6394 
6395 const parser::Name *DeclarationVisitor::ResolveStructureComponent(
6396     const parser::StructureComponent &x) {
6397   return FindComponent(ResolveDataRef(x.base), x.component);
6398 }
6399 
6400 const parser::Name *DeclarationVisitor::ResolveDesignator(
6401     const parser::Designator &x) {
6402   return common::visit(
6403       common::visitors{
6404           [&](const parser::DataRef &x) { return ResolveDataRef(x); },
6405           [&](const parser::Substring &x) {
6406             return ResolveDataRef(std::get<parser::DataRef>(x.t));
6407           },
6408       },
6409       x.u);
6410 }
6411 
6412 const parser::Name *DeclarationVisitor::ResolveDataRef(
6413     const parser::DataRef &x) {
6414   return common::visit(
6415       common::visitors{
6416           [=](const parser::Name &y) { return ResolveName(y); },
6417           [=](const Indirection<parser::StructureComponent> &y) {
6418             return ResolveStructureComponent(y.value());
6419           },
6420           [&](const Indirection<parser::ArrayElement> &y) {
6421             Walk(y.value().subscripts);
6422             const parser::Name *name{ResolveDataRef(y.value().base)};
6423             if (name && name->symbol) {
6424               if (!IsProcedure(*name->symbol)) {
6425                 ConvertToObjectEntity(*name->symbol);
6426               } else if (!context().HasError(*name->symbol)) {
6427                 SayWithDecl(*name, *name->symbol,
6428                     "Cannot reference function '%s' as data"_err_en_US);
6429               }
6430             }
6431             return name;
6432           },
6433           [&](const Indirection<parser::CoindexedNamedObject> &y) {
6434             Walk(y.value().imageSelector);
6435             return ResolveDataRef(y.value().base);
6436           },
6437       },
6438       x.u);
6439 }
6440 
6441 // If implicit types are allowed, ensure name is in the symbol table.
6442 // Otherwise, report an error if it hasn't been declared.
6443 const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
6444   FindSymbol(name);
6445   if (CheckForHostAssociatedImplicit(name)) {
6446     NotePossibleBadForwardRef(name);
6447     return &name;
6448   }
6449   if (Symbol * symbol{name.symbol}) {
6450     if (CheckUseError(name)) {
6451       return nullptr; // reported an error
6452     }
6453     NotePossibleBadForwardRef(name);
6454     symbol->set(Symbol::Flag::ImplicitOrError, false);
6455     if (IsUplevelReference(*symbol)) {
6456       MakeHostAssocSymbol(name, *symbol);
6457     } else if (IsDummy(*symbol) ||
6458         (!symbol->GetType() && FindCommonBlockContaining(*symbol))) {
6459       ConvertToObjectEntity(*symbol);
6460       ApplyImplicitRules(*symbol);
6461     }
6462     if (checkIndexUseInOwnBounds_ &&
6463         *checkIndexUseInOwnBounds_ == name.source) {
6464       Say(name,
6465           "Implied DO index '%s' uses an object of the same name in its bounds expressions"_port_en_US,
6466           name.source);
6467     }
6468     return &name;
6469   }
6470   if (isImplicitNoneType()) {
6471     Say(name, "No explicit type declared for '%s'"_err_en_US);
6472     return nullptr;
6473   }
6474   // Create the symbol then ensure it is accessible
6475   if (checkIndexUseInOwnBounds_ && *checkIndexUseInOwnBounds_ == name.source) {
6476     Say(name,
6477         "Implied DO index '%s' uses itself in its own bounds expressions"_err_en_US,
6478         name.source);
6479   }
6480   MakeSymbol(InclusiveScope(), name.source, Attrs{});
6481   auto *symbol{FindSymbol(name)};
6482   if (!symbol) {
6483     Say(name,
6484         "'%s' from host scoping unit is not accessible due to IMPORT"_err_en_US);
6485     return nullptr;
6486   }
6487   ConvertToObjectEntity(*symbol);
6488   ApplyImplicitRules(*symbol);
6489   NotePossibleBadForwardRef(name);
6490   return &name;
6491 }
6492 
6493 // A specification expression may refer to a symbol in the host procedure that
6494 // is implicitly typed. Because specification parts are processed before
6495 // execution parts, this may be the first time we see the symbol. It can't be a
6496 // local in the current scope (because it's in a specification expression) so
6497 // either it is implicitly declared in the host procedure or it is an error.
6498 // We create a symbol in the host assuming it is the former; if that proves to
6499 // be wrong we report an error later in CheckDeclarations().
6500 bool DeclarationVisitor::CheckForHostAssociatedImplicit(
6501     const parser::Name &name) {
6502   if (!inSpecificationPart_) {
6503     return false;
6504   }
6505   if (name.symbol) {
6506     ApplyImplicitRules(*name.symbol, true);
6507   }
6508   Symbol *hostSymbol;
6509   Scope *host{GetHostProcedure()};
6510   if (!host || isImplicitNoneType(*host)) {
6511     return false;
6512   }
6513   if (!name.symbol) {
6514     hostSymbol = &MakeSymbol(*host, name.source, Attrs{});
6515     ConvertToObjectEntity(*hostSymbol);
6516     ApplyImplicitRules(*hostSymbol);
6517     hostSymbol->set(Symbol::Flag::ImplicitOrError);
6518   } else if (name.symbol->test(Symbol::Flag::ImplicitOrError)) {
6519     hostSymbol = name.symbol;
6520   } else {
6521     return false;
6522   }
6523   Symbol &symbol{MakeHostAssocSymbol(name, *hostSymbol)};
6524   if (isImplicitNoneType()) {
6525     symbol.get<HostAssocDetails>().implicitOrExplicitTypeError = true;
6526   } else {
6527     symbol.get<HostAssocDetails>().implicitOrSpecExprError = true;
6528   }
6529   return true;
6530 }
6531 
6532 bool DeclarationVisitor::IsUplevelReference(const Symbol &symbol) {
6533   const Scope &symbolUnit{GetProgramUnitContaining(symbol)};
6534   if (symbolUnit == GetProgramUnitContaining(currScope())) {
6535     return false;
6536   } else {
6537     Scope::Kind kind{symbolUnit.kind()};
6538     return kind == Scope::Kind::Subprogram || kind == Scope::Kind::MainProgram;
6539   }
6540 }
6541 
6542 // base is a part-ref of a derived type; find the named component in its type.
6543 // Also handles intrinsic type parameter inquiries (%kind, %len) and
6544 // COMPLEX component references (%re, %im).
6545 const parser::Name *DeclarationVisitor::FindComponent(
6546     const parser::Name *base, const parser::Name &component) {
6547   if (!base || !base->symbol) {
6548     return nullptr;
6549   }
6550   if (auto *misc{base->symbol->detailsIf<MiscDetails>()}) {
6551     if (component.source == "kind") {
6552       if (misc->kind() == MiscDetails::Kind::ComplexPartRe ||
6553           misc->kind() == MiscDetails::Kind::ComplexPartIm ||
6554           misc->kind() == MiscDetails::Kind::KindParamInquiry ||
6555           misc->kind() == MiscDetails::Kind::LenParamInquiry) {
6556         // x%{re,im,kind,len}%kind
6557         MakePlaceholder(component, MiscDetails::Kind::KindParamInquiry);
6558         return &component;
6559       }
6560     }
6561   }
6562   auto &symbol{base->symbol->GetUltimate()};
6563   if (!symbol.has<AssocEntityDetails>() && !ConvertToObjectEntity(symbol)) {
6564     SayWithDecl(*base, symbol,
6565         "'%s' is an invalid base for a component reference"_err_en_US);
6566     return nullptr;
6567   }
6568   auto *type{symbol.GetType()};
6569   if (!type) {
6570     return nullptr; // should have already reported error
6571   }
6572   if (const IntrinsicTypeSpec * intrinsic{type->AsIntrinsic()}) {
6573     auto category{intrinsic->category()};
6574     MiscDetails::Kind miscKind{MiscDetails::Kind::None};
6575     if (component.source == "kind") {
6576       miscKind = MiscDetails::Kind::KindParamInquiry;
6577     } else if (category == TypeCategory::Character) {
6578       if (component.source == "len") {
6579         miscKind = MiscDetails::Kind::LenParamInquiry;
6580       }
6581     } else if (category == TypeCategory::Complex) {
6582       if (component.source == "re") {
6583         miscKind = MiscDetails::Kind::ComplexPartRe;
6584       } else if (component.source == "im") {
6585         miscKind = MiscDetails::Kind::ComplexPartIm;
6586       }
6587     }
6588     if (miscKind != MiscDetails::Kind::None) {
6589       MakePlaceholder(component, miscKind);
6590       return &component;
6591     }
6592   } else if (const DerivedTypeSpec * derived{type->AsDerived()}) {
6593     if (const Scope * scope{derived->scope()}) {
6594       if (Resolve(component, scope->FindComponent(component.source))) {
6595         if (auto msg{
6596                 CheckAccessibleComponent(currScope(), *component.symbol)}) {
6597           context().Say(component.source, *msg);
6598         }
6599         return &component;
6600       } else {
6601         SayDerivedType(component.source,
6602             "Component '%s' not found in derived type '%s'"_err_en_US, *scope);
6603       }
6604     }
6605     return nullptr;
6606   }
6607   if (symbol.test(Symbol::Flag::Implicit)) {
6608     Say(*base,
6609         "'%s' is not an object of derived type; it is implicitly typed"_err_en_US);
6610   } else {
6611     SayWithDecl(
6612         *base, symbol, "'%s' is not an object of derived type"_err_en_US);
6613   }
6614   return nullptr;
6615 }
6616 
6617 void DeclarationVisitor::Initialization(const parser::Name &name,
6618     const parser::Initialization &init, bool inComponentDecl) {
6619   // Traversal of the initializer was deferred to here so that the
6620   // symbol being declared can be available for use in the expression, e.g.:
6621   //   real, parameter :: x = tiny(x)
6622   if (!name.symbol) {
6623     return;
6624   }
6625   Symbol &ultimate{name.symbol->GetUltimate()};
6626   if (IsAllocatable(ultimate)) {
6627     Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
6628     return;
6629   }
6630   if (auto *object{ultimate.detailsIf<ObjectEntityDetails>()}) {
6631     // TODO: check C762 - all bounds and type parameters of component
6632     // are colons or constant expressions if component is initialized
6633     common::visit(
6634         common::visitors{
6635             [&](const parser::ConstantExpr &expr) {
6636               NonPointerInitialization(name, expr);
6637             },
6638             [&](const parser::NullInit &null) {
6639               Walk(null);
6640               if (auto nullInit{EvaluateExpr(null)}) {
6641                 if (!evaluate::IsNullPointer(*nullInit)) {
6642                   Say(name,
6643                       "Pointer initializer must be intrinsic NULL()"_err_en_US); // C813
6644                 } else if (IsPointer(ultimate)) {
6645                   object->set_init(std::move(*nullInit));
6646                 } else {
6647                   Say(name,
6648                       "Non-pointer component '%s' initialized with null pointer"_err_en_US);
6649                 }
6650               }
6651             },
6652             [&](const parser::InitialDataTarget &) {
6653               // Defer analysis to the end of the specification part
6654               // so that forward references and attribute checks like SAVE
6655               // work better.
6656               ultimate.set(Symbol::Flag::InDataStmt);
6657             },
6658             [&](const std::list<Indirection<parser::DataStmtValue>> &values) {
6659               // Handled later in data-to-inits conversion
6660               ultimate.set(Symbol::Flag::InDataStmt);
6661               Walk(values);
6662             },
6663         },
6664         init.u);
6665   }
6666 }
6667 
6668 void DeclarationVisitor::PointerInitialization(
6669     const parser::Name &name, const parser::InitialDataTarget &target) {
6670   if (name.symbol) {
6671     Symbol &ultimate{name.symbol->GetUltimate()};
6672     if (!context().HasError(ultimate)) {
6673       if (IsPointer(ultimate)) {
6674         if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
6675           CHECK(!details->init());
6676           Walk(target);
6677           if (MaybeExpr expr{EvaluateExpr(target)}) {
6678             // Validation is done in declaration checking.
6679             details->set_init(std::move(*expr));
6680           }
6681         }
6682       } else {
6683         Say(name,
6684             "'%s' is not a pointer but is initialized like one"_err_en_US);
6685         context().SetError(ultimate);
6686       }
6687     }
6688   }
6689 }
6690 void DeclarationVisitor::PointerInitialization(
6691     const parser::Name &name, const parser::ProcPointerInit &target) {
6692   if (name.symbol) {
6693     Symbol &ultimate{name.symbol->GetUltimate()};
6694     if (!context().HasError(ultimate)) {
6695       if (IsProcedurePointer(ultimate)) {
6696         auto &details{ultimate.get<ProcEntityDetails>()};
6697         CHECK(!details.init());
6698         Walk(target);
6699         if (const auto *targetName{std::get_if<parser::Name>(&target.u)}) {
6700           if (targetName->symbol) {
6701             // Validation is done in declaration checking.
6702             details.set_init(*targetName->symbol);
6703           }
6704         } else {
6705           details.set_init(nullptr); // explicit NULL()
6706         }
6707       } else {
6708         Say(name,
6709             "'%s' is not a procedure pointer but is initialized "
6710             "like one"_err_en_US);
6711         context().SetError(ultimate);
6712       }
6713     }
6714   }
6715 }
6716 
6717 void DeclarationVisitor::NonPointerInitialization(
6718     const parser::Name &name, const parser::ConstantExpr &expr) {
6719   if (name.symbol) {
6720     Symbol &ultimate{name.symbol->GetUltimate()};
6721     if (!context().HasError(ultimate) && !context().HasError(name.symbol)) {
6722       if (IsPointer(ultimate)) {
6723         Say(name,
6724             "'%s' is a pointer but is not initialized like one"_err_en_US);
6725       } else if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
6726         CHECK(!details->init());
6727         Walk(expr);
6728         if (ultimate.owner().IsParameterizedDerivedType()) {
6729           // Save the expression for per-instantiation analysis.
6730           details->set_unanalyzedPDTComponentInit(&expr.thing.value());
6731         } else {
6732           if (MaybeExpr folded{EvaluateNonPointerInitializer(
6733                   ultimate, expr, expr.thing.value().source)}) {
6734             details->set_init(std::move(*folded));
6735           }
6736         }
6737       }
6738     }
6739   }
6740 }
6741 
6742 void ResolveNamesVisitor::HandleCall(
6743     Symbol::Flag procFlag, const parser::Call &call) {
6744   common::visit(
6745       common::visitors{
6746           [&](const parser::Name &x) { HandleProcedureName(procFlag, x); },
6747           [&](const parser::ProcComponentRef &x) { Walk(x); },
6748       },
6749       std::get<parser::ProcedureDesignator>(call.t).u);
6750   Walk(std::get<std::list<parser::ActualArgSpec>>(call.t));
6751 }
6752 
6753 void ResolveNamesVisitor::HandleProcedureName(
6754     Symbol::Flag flag, const parser::Name &name) {
6755   CHECK(flag == Symbol::Flag::Function || flag == Symbol::Flag::Subroutine);
6756   auto *symbol{FindSymbol(NonDerivedTypeScope(), name)};
6757   if (!symbol) {
6758     if (IsIntrinsic(name.source, flag)) {
6759       symbol =
6760           &MakeSymbol(InclusiveScope(), name.source, Attrs{Attr::INTRINSIC});
6761     } else {
6762       symbol = &MakeSymbol(context().globalScope(), name.source, Attrs{});
6763     }
6764     Resolve(name, *symbol);
6765     if (!symbol->attrs().test(Attr::INTRINSIC)) {
6766       if (CheckImplicitNoneExternal(name.source, *symbol)) {
6767         MakeExternal(*symbol);
6768       }
6769     }
6770     ConvertToProcEntity(*symbol);
6771     SetProcFlag(name, *symbol, flag);
6772   } else if (CheckUseError(name)) {
6773     // error was reported
6774   } else {
6775     auto &nonUltimateSymbol{*symbol};
6776     symbol = &Resolve(name, symbol)->GetUltimate();
6777     bool convertedToProcEntity{ConvertToProcEntity(*symbol)};
6778     if (convertedToProcEntity && !symbol->attrs().test(Attr::EXTERNAL) &&
6779         IsIntrinsic(symbol->name(), flag) && !IsDummy(*symbol)) {
6780       AcquireIntrinsicProcedureFlags(*symbol);
6781     }
6782     if (!SetProcFlag(name, *symbol, flag)) {
6783       return; // reported error
6784     }
6785     if (!symbol->has<GenericDetails>()) {
6786       CheckImplicitNoneExternal(name.source, *symbol);
6787     }
6788     if (symbol->has<SubprogramDetails>() &&
6789         symbol->attrs().test(Attr::ABSTRACT)) {
6790       Say(name, "Abstract interface '%s' may not be called"_err_en_US);
6791     } else if (IsProcedure(*symbol) || symbol->has<DerivedTypeDetails>() ||
6792         symbol->has<AssocEntityDetails>()) {
6793       // Symbols with DerivedTypeDetails and AssocEntityDetails are accepted
6794       // here as procedure-designators because this means the related
6795       // FunctionReference are mis-parsed structure constructors or array
6796       // references that will be fixed later when analyzing expressions.
6797     } else if (symbol->has<ObjectEntityDetails>()) {
6798       // Symbols with ObjectEntityDetails are also accepted because this can be
6799       // a mis-parsed array references that will be fixed later. Ensure that if
6800       // this is a symbol from a host procedure, a symbol with HostAssocDetails
6801       // is created for the current scope.
6802       // Operate on non ultimate symbol so that HostAssocDetails are also
6803       // created for symbols used associated in the host procedure.
6804       if (IsUplevelReference(nonUltimateSymbol)) {
6805         MakeHostAssocSymbol(name, nonUltimateSymbol);
6806       }
6807     } else if (symbol->test(Symbol::Flag::Implicit)) {
6808       Say(name,
6809           "Use of '%s' as a procedure conflicts with its implicit definition"_err_en_US);
6810     } else {
6811       SayWithDecl(name, *symbol,
6812           "Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
6813     }
6814   }
6815 }
6816 
6817 bool ResolveNamesVisitor::CheckImplicitNoneExternal(
6818     const SourceName &name, const Symbol &symbol) {
6819   if (isImplicitNoneExternal() && !symbol.attrs().test(Attr::EXTERNAL) &&
6820       !symbol.attrs().test(Attr::INTRINSIC) && !symbol.HasExplicitInterface()) {
6821     Say(name,
6822         "'%s' is an external procedure without the EXTERNAL"
6823         " attribute in a scope with IMPLICIT NONE(EXTERNAL)"_err_en_US);
6824     return false;
6825   }
6826   return true;
6827 }
6828 
6829 // Variant of HandleProcedureName() for use while skimming the executable
6830 // part of a subprogram to catch calls to dummy procedures that are part
6831 // of the subprogram's interface, and to mark as procedures any symbols
6832 // that might otherwise have been miscategorized as objects.
6833 void ResolveNamesVisitor::NoteExecutablePartCall(
6834     Symbol::Flag flag, const parser::Call &call) {
6835   auto &designator{std::get<parser::ProcedureDesignator>(call.t)};
6836   if (const auto *name{std::get_if<parser::Name>(&designator.u)}) {
6837     // Subtlety: The symbol pointers in the parse tree are not set, because
6838     // they might end up resolving elsewhere (e.g., construct entities in
6839     // SELECT TYPE).
6840     if (Symbol * symbol{currScope().FindSymbol(name->source)}) {
6841       Symbol::Flag other{flag == Symbol::Flag::Subroutine
6842               ? Symbol::Flag::Function
6843               : Symbol::Flag::Subroutine};
6844       if (!symbol->test(other)) {
6845         ConvertToProcEntity(*symbol);
6846         if (symbol->has<ProcEntityDetails>()) {
6847           symbol->set(flag);
6848           if (IsDummy(*symbol)) {
6849             symbol->attrs().set(Attr::EXTERNAL);
6850           }
6851           ApplyImplicitRules(*symbol);
6852         }
6853       }
6854     }
6855   }
6856 }
6857 
6858 static bool IsLocallyImplicitGlobalSymbol(
6859     const Symbol &symbol, const parser::Name &localName) {
6860   return symbol.owner().IsGlobal() &&
6861       (!symbol.scope() ||
6862           !symbol.scope()->sourceRange().Contains(localName.source));
6863 }
6864 
6865 static bool TypesMismatchIfNonNull(
6866     const DeclTypeSpec *type1, const DeclTypeSpec *type2) {
6867   return type1 && type2 && *type1 != *type2;
6868 }
6869 
6870 // Check and set the Function or Subroutine flag on symbol; false on error.
6871 bool ResolveNamesVisitor::SetProcFlag(
6872     const parser::Name &name, Symbol &symbol, Symbol::Flag flag) {
6873   if (symbol.test(Symbol::Flag::Function) && flag == Symbol::Flag::Subroutine) {
6874     SayWithDecl(
6875         name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
6876     return false;
6877   } else if (symbol.test(Symbol::Flag::Subroutine) &&
6878       flag == Symbol::Flag::Function) {
6879     SayWithDecl(
6880         name, symbol, "Cannot call subroutine '%s' like a function"_err_en_US);
6881     return false;
6882   } else if (flag == Symbol::Flag::Function &&
6883       IsLocallyImplicitGlobalSymbol(symbol, name) &&
6884       TypesMismatchIfNonNull(symbol.GetType(), GetImplicitType(symbol))) {
6885     SayWithDecl(name, symbol,
6886         "Implicit declaration of function '%s' has a different result type than in previous declaration"_err_en_US);
6887     return false;
6888   } else if (symbol.has<ProcEntityDetails>()) {
6889     symbol.set(flag); // in case it hasn't been set yet
6890     if (flag == Symbol::Flag::Function) {
6891       ApplyImplicitRules(symbol);
6892     }
6893     if (symbol.attrs().test(Attr::INTRINSIC)) {
6894       AcquireIntrinsicProcedureFlags(symbol);
6895     }
6896   } else if (symbol.GetType() && flag == Symbol::Flag::Subroutine) {
6897     SayWithDecl(
6898         name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
6899   } else if (symbol.attrs().test(Attr::INTRINSIC)) {
6900     AcquireIntrinsicProcedureFlags(symbol);
6901   }
6902   return true;
6903 }
6904 
6905 bool ModuleVisitor::Pre(const parser::AccessStmt &x) {
6906   Attr accessAttr{AccessSpecToAttr(std::get<parser::AccessSpec>(x.t))};
6907   if (!currScope().IsModule()) { // C869
6908     Say(currStmtSource().value(),
6909         "%s statement may only appear in the specification part of a module"_err_en_US,
6910         EnumToString(accessAttr));
6911     return false;
6912   }
6913   const auto &accessIds{std::get<std::list<parser::AccessId>>(x.t)};
6914   if (accessIds.empty()) {
6915     if (prevAccessStmt_) { // C869
6916       Say("The default accessibility of this module has already been declared"_err_en_US)
6917           .Attach(*prevAccessStmt_, "Previous declaration"_en_US);
6918     }
6919     prevAccessStmt_ = currStmtSource();
6920     defaultAccess_ = accessAttr;
6921   } else {
6922     for (const auto &accessId : accessIds) {
6923       common::visit(
6924           common::visitors{
6925               [=](const parser::Name &y) {
6926                 Resolve(y, SetAccess(y.source, accessAttr));
6927               },
6928               [=](const Indirection<parser::GenericSpec> &y) {
6929                 auto info{GenericSpecInfo{y.value()}};
6930                 const auto &symbolName{info.symbolName()};
6931                 if (auto *symbol{FindInScope(symbolName)}) {
6932                   info.Resolve(&SetAccess(symbolName, accessAttr, symbol));
6933                 } else if (info.kind().IsName()) {
6934                   info.Resolve(&SetAccess(symbolName, accessAttr));
6935                 } else {
6936                   Say(symbolName, "Generic spec '%s' not found"_err_en_US);
6937                 }
6938               },
6939           },
6940           accessId.u);
6941     }
6942   }
6943   return false;
6944 }
6945 
6946 // Set the access specification for this symbol.
6947 Symbol &ModuleVisitor::SetAccess(
6948     const SourceName &name, Attr attr, Symbol *symbol) {
6949   if (!symbol) {
6950     symbol = &MakeSymbol(name);
6951   }
6952   Attrs &attrs{symbol->attrs()};
6953   if (attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
6954     // PUBLIC/PRIVATE already set: make it a fatal error if it changed
6955     Attr prev = attrs.test(Attr::PUBLIC) ? Attr::PUBLIC : Attr::PRIVATE;
6956     Say(name,
6957         WithSeverity(
6958             "The accessibility of '%s' has already been specified as %s"_warn_en_US,
6959             attr != prev ? parser::Severity::Error : parser::Severity::Warning),
6960         MakeOpName(name), EnumToString(prev));
6961   } else {
6962     attrs.set(attr);
6963   }
6964   return *symbol;
6965 }
6966 
6967 static bool NeedsExplicitType(const Symbol &symbol) {
6968   if (symbol.has<UnknownDetails>()) {
6969     return true;
6970   } else if (const auto *details{symbol.detailsIf<EntityDetails>()}) {
6971     return !details->type();
6972   } else if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
6973     return !details->type();
6974   } else if (const auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
6975     return !details->interface().symbol() && !details->interface().type();
6976   } else {
6977     return false;
6978   }
6979 }
6980 
6981 bool ResolveNamesVisitor::Pre(const parser::SpecificationPart &x) {
6982   const auto &[accDecls, ompDecls, compilerDirectives, useStmts, importStmts,
6983       implicitPart, decls] = x.t;
6984   auto flagRestorer{common::ScopedSet(inSpecificationPart_, true)};
6985   auto stateRestorer{
6986       common::ScopedSet(specPartState_, SpecificationPartState{})};
6987   Walk(accDecls);
6988   Walk(ompDecls);
6989   Walk(compilerDirectives);
6990   Walk(useStmts);
6991   ClearUseRenames();
6992   ClearUseOnly();
6993   ClearExplicitIntrinsicUses();
6994   Walk(importStmts);
6995   Walk(implicitPart);
6996   for (const auto &decl : decls) {
6997     if (const auto *spec{
6998             std::get_if<parser::SpecificationConstruct>(&decl.u)}) {
6999       PreSpecificationConstruct(*spec);
7000     }
7001   }
7002   Walk(decls);
7003   FinishSpecificationPart(decls);
7004   return false;
7005 }
7006 
7007 // Initial processing on specification constructs, before visiting them.
7008 void ResolveNamesVisitor::PreSpecificationConstruct(
7009     const parser::SpecificationConstruct &spec) {
7010   common::visit(
7011       common::visitors{
7012           [&](const parser::Statement<Indirection<parser::GenericStmt>> &y) {
7013             CreateGeneric(std::get<parser::GenericSpec>(y.statement.value().t));
7014           },
7015           [&](const Indirection<parser::InterfaceBlock> &y) {
7016             const auto &stmt{std::get<parser::Statement<parser::InterfaceStmt>>(
7017                 y.value().t)};
7018             if (const auto *spec{parser::Unwrap<parser::GenericSpec>(stmt)}) {
7019               CreateGeneric(*spec);
7020             }
7021           },
7022           [&](const parser::Statement<parser::OtherSpecificationStmt> &y) {
7023             if (const auto *commonStmt{parser::Unwrap<parser::CommonStmt>(y)}) {
7024               CreateCommonBlockSymbols(*commonStmt);
7025             }
7026           },
7027           [&](const auto &) {},
7028       },
7029       spec.u);
7030 }
7031 
7032 void ResolveNamesVisitor::CreateCommonBlockSymbols(
7033     const parser::CommonStmt &commonStmt) {
7034   for (const parser::CommonStmt::Block &block : commonStmt.blocks) {
7035     const auto &[name, objects] = block.t;
7036     Symbol &commonBlock{MakeCommonBlockSymbol(name)};
7037     for (const auto &object : objects) {
7038       Symbol &obj{DeclareObjectEntity(std::get<parser::Name>(object.t))};
7039       if (auto *details{obj.detailsIf<ObjectEntityDetails>()}) {
7040         details->set_commonBlock(commonBlock);
7041         commonBlock.get<CommonBlockDetails>().add_object(obj);
7042       }
7043     }
7044   }
7045 }
7046 
7047 void ResolveNamesVisitor::CreateGeneric(const parser::GenericSpec &x) {
7048   auto info{GenericSpecInfo{x}};
7049   SourceName symbolName{info.symbolName()};
7050   if (IsLogicalConstant(context(), symbolName)) {
7051     Say(symbolName,
7052         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
7053     return;
7054   }
7055   GenericDetails genericDetails;
7056   Symbol *existing{nullptr};
7057   // Check all variants of names, e.g. "operator(.ne.)" for "operator(/=)"
7058   for (const std::string &n : GetAllNames(context(), symbolName)) {
7059     if (auto iter{currScope().find(n)}; iter != currScope().end()) {
7060       existing = &*iter->second;
7061       break;
7062     }
7063   }
7064   if (existing) {
7065     Symbol &ultimate{existing->GetUltimate()};
7066     if (const auto *existingGeneric{ultimate.detailsIf<GenericDetails>()}) {
7067       if (const auto *existingUse{existing->detailsIf<UseDetails>()}) {
7068         // Create a local copy of a use associated generic so that
7069         // it can be locally extended without corrupting the original.
7070         genericDetails.CopyFrom(*existingGeneric);
7071         AddGenericUse(genericDetails, existing->name(), existingUse->symbol());
7072       } else if (existing == &ultimate) {
7073         // Extending an extant generic in the same scope
7074         info.Resolve(existing);
7075         return;
7076       } else {
7077         // Host association of a generic is handled in ResolveGeneric()
7078         CHECK(existing->has<HostAssocDetails>());
7079       }
7080     } else if (ultimate.has<SubprogramDetails>() ||
7081         ultimate.has<SubprogramNameDetails>()) {
7082       genericDetails.set_specific(ultimate);
7083     } else if (ultimate.has<DerivedTypeDetails>()) {
7084       genericDetails.set_derivedType(ultimate);
7085     } else {
7086       SayAlreadyDeclared(symbolName, *existing);
7087       return;
7088     }
7089     EraseSymbol(*existing);
7090   }
7091   info.Resolve(&MakeSymbol(symbolName, Attrs{}, std::move(genericDetails)));
7092 }
7093 
7094 void ResolveNamesVisitor::FinishSpecificationPart(
7095     const std::list<parser::DeclarationConstruct> &decls) {
7096   badStmtFuncFound_ = false;
7097   funcResultStack().CompleteFunctionResultType();
7098   CheckImports();
7099   bool inModule{currScope().kind() == Scope::Kind::Module};
7100   for (auto &pair : currScope()) {
7101     auto &symbol{*pair.second};
7102     if (NeedsExplicitType(symbol)) {
7103       ApplyImplicitRules(symbol);
7104     }
7105     if (IsDummy(symbol) && isImplicitNoneType() &&
7106         symbol.test(Symbol::Flag::Implicit) && !context().HasError(symbol)) {
7107       Say(symbol.name(),
7108           "No explicit type declared for dummy argument '%s'"_err_en_US);
7109       context().SetError(symbol);
7110     }
7111     if (symbol.has<GenericDetails>()) {
7112       CheckGenericProcedures(symbol);
7113     }
7114     if (inModule && symbol.attrs().test(Attr::EXTERNAL) &&
7115         !symbol.test(Symbol::Flag::Function) &&
7116         !symbol.test(Symbol::Flag::Subroutine)) {
7117       // in a module, external proc without return type is subroutine
7118       symbol.set(
7119           symbol.GetType() ? Symbol::Flag::Function : Symbol::Flag::Subroutine);
7120     }
7121     if (!symbol.has<HostAssocDetails>()) {
7122       CheckPossibleBadForwardRef(symbol);
7123     }
7124   }
7125   currScope().InstantiateDerivedTypes();
7126   for (const auto &decl : decls) {
7127     if (const auto *statement{std::get_if<
7128             parser::Statement<common::Indirection<parser::StmtFunctionStmt>>>(
7129             &decl.u)}) {
7130       AnalyzeStmtFunctionStmt(statement->statement.value());
7131     }
7132   }
7133   // TODO: what about instantiations in BLOCK?
7134   CheckSaveStmts();
7135   CheckCommonBlocks();
7136   if (!inInterfaceBlock()) {
7137     // TODO: warn for the case where the EQUIVALENCE statement is in a
7138     // procedure declaration in an interface block
7139     CheckEquivalenceSets();
7140   }
7141 }
7142 
7143 // Analyze the bodies of statement functions now that the symbols in this
7144 // specification part have been fully declared and implicitly typed.
7145 void ResolveNamesVisitor::AnalyzeStmtFunctionStmt(
7146     const parser::StmtFunctionStmt &stmtFunc) {
7147   Symbol *symbol{std::get<parser::Name>(stmtFunc.t).symbol};
7148   if (!symbol || !symbol->has<SubprogramDetails>()) {
7149     return;
7150   }
7151   auto &details{symbol->get<SubprogramDetails>()};
7152   auto expr{AnalyzeExpr(
7153       context(), std::get<parser::Scalar<parser::Expr>>(stmtFunc.t))};
7154   if (!expr) {
7155     context().SetError(*symbol);
7156     return;
7157   }
7158   if (auto type{evaluate::DynamicType::From(*symbol)}) {
7159     auto converted{ConvertToType(*type, std::move(*expr))};
7160     if (!converted) {
7161       context().SetError(*symbol);
7162       return;
7163     }
7164     details.set_stmtFunction(std::move(*converted));
7165   } else {
7166     details.set_stmtFunction(std::move(*expr));
7167   }
7168 }
7169 
7170 void ResolveNamesVisitor::CheckImports() {
7171   auto &scope{currScope()};
7172   switch (scope.GetImportKind()) {
7173   case common::ImportKind::None:
7174     break;
7175   case common::ImportKind::All:
7176     // C8102: all entities in host must not be hidden
7177     for (const auto &pair : scope.parent()) {
7178       auto &name{pair.first};
7179       std::optional<SourceName> scopeName{scope.GetName()};
7180       if (!scopeName || name != *scopeName) {
7181         CheckImport(prevImportStmt_.value(), name);
7182       }
7183     }
7184     break;
7185   case common::ImportKind::Default:
7186   case common::ImportKind::Only:
7187     // C8102: entities named in IMPORT must not be hidden
7188     for (auto &name : scope.importNames()) {
7189       CheckImport(name, name);
7190     }
7191     break;
7192   }
7193 }
7194 
7195 void ResolveNamesVisitor::CheckImport(
7196     const SourceName &location, const SourceName &name) {
7197   if (auto *symbol{FindInScope(name)}) {
7198     const Symbol &ultimate{symbol->GetUltimate()};
7199     if (&ultimate.owner() == &currScope()) {
7200       Say(location, "'%s' from host is not accessible"_err_en_US, name)
7201           .Attach(symbol->name(), "'%s' is hidden by this entity"_en_US,
7202               symbol->name());
7203     }
7204   }
7205 }
7206 
7207 bool ResolveNamesVisitor::Pre(const parser::ImplicitStmt &x) {
7208   return CheckNotInBlock("IMPLICIT") && // C1107
7209       ImplicitRulesVisitor::Pre(x);
7210 }
7211 
7212 void ResolveNamesVisitor::Post(const parser::PointerObject &x) {
7213   common::visit(common::visitors{
7214                     [&](const parser::Name &x) { ResolveName(x); },
7215                     [&](const parser::StructureComponent &x) {
7216                       ResolveStructureComponent(x);
7217                     },
7218                 },
7219       x.u);
7220 }
7221 void ResolveNamesVisitor::Post(const parser::AllocateObject &x) {
7222   common::visit(common::visitors{
7223                     [&](const parser::Name &x) { ResolveName(x); },
7224                     [&](const parser::StructureComponent &x) {
7225                       ResolveStructureComponent(x);
7226                     },
7227                 },
7228       x.u);
7229 }
7230 
7231 bool ResolveNamesVisitor::Pre(const parser::PointerAssignmentStmt &x) {
7232   const auto &dataRef{std::get<parser::DataRef>(x.t)};
7233   const auto &bounds{std::get<parser::PointerAssignmentStmt::Bounds>(x.t)};
7234   const auto &expr{std::get<parser::Expr>(x.t)};
7235   ResolveDataRef(dataRef);
7236   Walk(bounds);
7237   // Resolve unrestricted specific intrinsic procedures as in "p => cos".
7238   if (const parser::Name * name{parser::Unwrap<parser::Name>(expr)}) {
7239     if (NameIsKnownOrIntrinsic(*name)) {
7240       // If the name is known because it is an object entity from a host
7241       // procedure, create a host associated symbol.
7242       if (Symbol * symbol{name->symbol}; symbol &&
7243           symbol->GetUltimate().has<ObjectEntityDetails>() &&
7244           IsUplevelReference(*symbol)) {
7245         MakeHostAssocSymbol(*name, *symbol);
7246       }
7247       return false;
7248     }
7249   }
7250   Walk(expr);
7251   return false;
7252 }
7253 void ResolveNamesVisitor::Post(const parser::Designator &x) {
7254   ResolveDesignator(x);
7255 }
7256 
7257 void ResolveNamesVisitor::Post(const parser::ProcComponentRef &x) {
7258   ResolveStructureComponent(x.v.thing);
7259 }
7260 void ResolveNamesVisitor::Post(const parser::TypeGuardStmt &x) {
7261   DeclTypeSpecVisitor::Post(x);
7262   ConstructVisitor::Post(x);
7263 }
7264 bool ResolveNamesVisitor::Pre(const parser::StmtFunctionStmt &x) {
7265   CheckNotInBlock("STATEMENT FUNCTION"); // C1107
7266   if (HandleStmtFunction(x)) {
7267     return false;
7268   } else {
7269     // This is an array element assignment: resolve names of indices
7270     const auto &names{std::get<std::list<parser::Name>>(x.t)};
7271     for (auto &name : names) {
7272       ResolveName(name);
7273     }
7274     return true;
7275   }
7276 }
7277 
7278 bool ResolveNamesVisitor::Pre(const parser::DefinedOpName &x) {
7279   const parser::Name &name{x.v};
7280   if (FindSymbol(name)) {
7281     // OK
7282   } else if (IsLogicalConstant(context(), name.source)) {
7283     Say(name,
7284         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
7285   } else {
7286     // Resolved later in expression semantics
7287     MakePlaceholder(name, MiscDetails::Kind::TypeBoundDefinedOp);
7288   }
7289   return false;
7290 }
7291 
7292 void ResolveNamesVisitor::Post(const parser::AssignStmt &x) {
7293   if (auto *name{ResolveName(std::get<parser::Name>(x.t))}) {
7294     ConvertToObjectEntity(DEREF(name->symbol));
7295   }
7296 }
7297 void ResolveNamesVisitor::Post(const parser::AssignedGotoStmt &x) {
7298   if (auto *name{ResolveName(std::get<parser::Name>(x.t))}) {
7299     ConvertToObjectEntity(DEREF(name->symbol));
7300   }
7301 }
7302 
7303 bool ResolveNamesVisitor::Pre(const parser::ProgramUnit &x) {
7304   if (std::holds_alternative<common::Indirection<parser::CompilerDirective>>(
7305           x.u)) {
7306     // TODO: global directives
7307     return true;
7308   }
7309   auto root{ProgramTree::Build(x)};
7310   SetScope(topScope_);
7311   ResolveSpecificationParts(root);
7312   FinishSpecificationParts(root);
7313   ResolveExecutionParts(root);
7314   ResolveAccParts(context(), x);
7315   ResolveOmpParts(context(), x);
7316   return false;
7317 }
7318 
7319 // References to procedures need to record that their symbols are known
7320 // to be procedures, so that they don't get converted to objects by default.
7321 class ExecutionPartSkimmer {
7322 public:
7323   explicit ExecutionPartSkimmer(ResolveNamesVisitor &resolver)
7324       : resolver_{resolver} {}
7325 
7326   void Walk(const parser::ExecutionPart *exec) {
7327     if (exec) {
7328       parser::Walk(*exec, *this);
7329     }
7330   }
7331 
7332   template <typename A> bool Pre(const A &) { return true; }
7333   template <typename A> void Post(const A &) {}
7334   void Post(const parser::FunctionReference &fr) {
7335     resolver_.NoteExecutablePartCall(Symbol::Flag::Function, fr.v);
7336   }
7337   void Post(const parser::CallStmt &cs) {
7338     resolver_.NoteExecutablePartCall(Symbol::Flag::Subroutine, cs.v);
7339   }
7340 
7341 private:
7342   ResolveNamesVisitor &resolver_;
7343 };
7344 
7345 // Build the scope tree and resolve names in the specification parts of this
7346 // node and its children
7347 void ResolveNamesVisitor::ResolveSpecificationParts(ProgramTree &node) {
7348   if (node.isSpecificationPartResolved()) {
7349     return; // been here already
7350   }
7351   node.set_isSpecificationPartResolved();
7352   if (!BeginScopeForNode(node)) {
7353     return; // an error prevented scope from being created
7354   }
7355   Scope &scope{currScope()};
7356   node.set_scope(scope);
7357   AddSubpNames(node);
7358   common::visit(
7359       [&](const auto *x) {
7360         if (x) {
7361           Walk(*x);
7362         }
7363       },
7364       node.stmt());
7365   Walk(node.spec());
7366   // If this is a function, convert result to an object. This is to prevent the
7367   // result from being converted later to a function symbol if it is called
7368   // inside the function.
7369   // If the result is function pointer, then ConvertToObjectEntity will not
7370   // convert the result to an object, and calling the symbol inside the function
7371   // will result in calls to the result pointer.
7372   // A function cannot be called recursively if RESULT was not used to define a
7373   // distinct result name (15.6.2.2 point 4.).
7374   if (Symbol * symbol{scope.symbol()}) {
7375     if (auto *details{symbol->detailsIf<SubprogramDetails>()}) {
7376       if (details->isFunction()) {
7377         ConvertToObjectEntity(const_cast<Symbol &>(details->result()));
7378       }
7379     }
7380   }
7381   if (node.IsModule()) {
7382     ApplyDefaultAccess();
7383   }
7384   for (auto &child : node.children()) {
7385     ResolveSpecificationParts(child);
7386   }
7387   ExecutionPartSkimmer{*this}.Walk(node.exec());
7388   EndScopeForNode(node);
7389   // Ensure that every object entity has a type.
7390   for (auto &pair : *node.scope()) {
7391     ApplyImplicitRules(*pair.second);
7392   }
7393 }
7394 
7395 // Add SubprogramNameDetails symbols for module and internal subprograms and
7396 // their ENTRY statements.
7397 void ResolveNamesVisitor::AddSubpNames(ProgramTree &node) {
7398   auto kind{
7399       node.IsModule() ? SubprogramKind::Module : SubprogramKind::Internal};
7400   for (auto &child : node.children()) {
7401     auto &symbol{MakeSymbol(child.name(), SubprogramNameDetails{kind, child})};
7402     auto childKind{child.GetKind()};
7403     if (childKind == ProgramTree::Kind::Function) {
7404       symbol.set(Symbol::Flag::Function);
7405     } else if (childKind == ProgramTree::Kind::Subroutine) {
7406       symbol.set(Symbol::Flag::Subroutine);
7407     } else {
7408       continue; // make ENTRY symbols only where valid
7409     }
7410     for (const auto &entryStmt : child.entryStmts()) {
7411       SubprogramNameDetails details{kind, child};
7412       auto &symbol{
7413           MakeSymbol(std::get<parser::Name>(entryStmt->t), std::move(details))};
7414       symbol.set(child.GetSubpFlag());
7415     }
7416   }
7417   for (const auto &generic : node.genericSpecs()) {
7418     if (const auto *name{std::get_if<parser::Name>(&generic->u)}) {
7419       if (currScope().find(name->source) != currScope().end()) {
7420         // If this scope has both a generic interface and a contained
7421         // subprogram with the same name, create the generic's symbol
7422         // now so that any other generics of the same name that are pulled
7423         // into scope later via USE association will properly merge instead
7424         // of raising a bogus error due a conflict with the subprogram.
7425         CreateGeneric(*generic);
7426       }
7427     }
7428   }
7429 }
7430 
7431 // Push a new scope for this node or return false on error.
7432 bool ResolveNamesVisitor::BeginScopeForNode(const ProgramTree &node) {
7433   switch (node.GetKind()) {
7434     SWITCH_COVERS_ALL_CASES
7435   case ProgramTree::Kind::Program:
7436     PushScope(Scope::Kind::MainProgram,
7437         &MakeSymbol(node.name(), MainProgramDetails{}));
7438     return true;
7439   case ProgramTree::Kind::Function:
7440   case ProgramTree::Kind::Subroutine:
7441     return BeginSubprogram(node.name(), node.GetSubpFlag(),
7442         node.HasModulePrefix(), node.bindingSpec(), &node.entryStmts());
7443   case ProgramTree::Kind::MpSubprogram:
7444     return BeginMpSubprogram(node.name());
7445   case ProgramTree::Kind::Module:
7446     BeginModule(node.name(), false);
7447     return true;
7448   case ProgramTree::Kind::Submodule:
7449     return BeginSubmodule(node.name(), node.GetParentId());
7450   case ProgramTree::Kind::BlockData:
7451     PushBlockDataScope(node.name());
7452     return true;
7453   }
7454 }
7455 
7456 void ResolveNamesVisitor::EndScopeForNode(const ProgramTree &node) {
7457   std::optional<parser::CharBlock> stmtSource;
7458   const std::optional<parser::LanguageBindingSpec> *binding{nullptr};
7459   common::visit(
7460       common::visitors{
7461           [&](const parser::Statement<parser::FunctionStmt> *stmt) {
7462             if (stmt) {
7463               stmtSource = stmt->source;
7464               if (const auto &maybeSuffix{
7465                       std::get<std::optional<parser::Suffix>>(
7466                           stmt->statement.t)}) {
7467                 binding = &maybeSuffix->binding;
7468               }
7469             }
7470           },
7471           [&](const parser::Statement<parser::SubroutineStmt> *stmt) {
7472             if (stmt) {
7473               stmtSource = stmt->source;
7474               binding = &std::get<std::optional<parser::LanguageBindingSpec>>(
7475                   stmt->statement.t);
7476             }
7477           },
7478           [](const auto *) {},
7479       },
7480       node.stmt());
7481   EndSubprogram(stmtSource, binding);
7482 }
7483 
7484 // Some analyses and checks, such as the processing of initializers of
7485 // pointers, are deferred until all of the pertinent specification parts
7486 // have been visited.  This deferred processing enables the use of forward
7487 // references in these circumstances.
7488 class DeferredCheckVisitor {
7489 public:
7490   explicit DeferredCheckVisitor(ResolveNamesVisitor &resolver)
7491       : resolver_{resolver} {}
7492 
7493   template <typename A> void Walk(const A &x) { parser::Walk(x, *this); }
7494 
7495   template <typename A> bool Pre(const A &) { return true; }
7496   template <typename A> void Post(const A &) {}
7497 
7498   void Post(const parser::DerivedTypeStmt &x) {
7499     const auto &name{std::get<parser::Name>(x.t)};
7500     if (Symbol * symbol{name.symbol}) {
7501       if (Scope * scope{symbol->scope()}) {
7502         if (scope->IsDerivedType()) {
7503           resolver_.PushScope(*scope);
7504           pushedScope_ = true;
7505         }
7506       }
7507     }
7508   }
7509   void Post(const parser::EndTypeStmt &) {
7510     if (pushedScope_) {
7511       resolver_.PopScope();
7512       pushedScope_ = false;
7513     }
7514   }
7515 
7516   void Post(const parser::ProcInterface &pi) {
7517     if (const auto *name{std::get_if<parser::Name>(&pi.u)}) {
7518       resolver_.CheckExplicitInterface(*name);
7519     }
7520   }
7521   bool Pre(const parser::EntityDecl &decl) {
7522     Init(std::get<parser::Name>(decl.t),
7523         std::get<std::optional<parser::Initialization>>(decl.t));
7524     return false;
7525   }
7526   bool Pre(const parser::ComponentDecl &decl) {
7527     Init(std::get<parser::Name>(decl.t),
7528         std::get<std::optional<parser::Initialization>>(decl.t));
7529     return false;
7530   }
7531   bool Pre(const parser::ProcDecl &decl) {
7532     if (const auto &init{
7533             std::get<std::optional<parser::ProcPointerInit>>(decl.t)}) {
7534       resolver_.PointerInitialization(std::get<parser::Name>(decl.t), *init);
7535     }
7536     return false;
7537   }
7538   void Post(const parser::TypeBoundProcedureStmt::WithInterface &tbps) {
7539     resolver_.CheckExplicitInterface(tbps.interfaceName);
7540   }
7541   void Post(const parser::TypeBoundProcedureStmt::WithoutInterface &tbps) {
7542     if (pushedScope_) {
7543       resolver_.CheckBindings(tbps);
7544     }
7545   }
7546 
7547 private:
7548   void Init(const parser::Name &name,
7549       const std::optional<parser::Initialization> &init) {
7550     if (init) {
7551       if (const auto *target{
7552               std::get_if<parser::InitialDataTarget>(&init->u)}) {
7553         resolver_.PointerInitialization(name, *target);
7554       }
7555     }
7556   }
7557 
7558   ResolveNamesVisitor &resolver_;
7559   bool pushedScope_{false};
7560 };
7561 
7562 // Perform checks and completions that need to happen after all of
7563 // the specification parts but before any of the execution parts.
7564 void ResolveNamesVisitor::FinishSpecificationParts(const ProgramTree &node) {
7565   if (!node.scope()) {
7566     return; // error occurred creating scope
7567   }
7568   SetScope(*node.scope());
7569   // The initializers of pointers, the default initializers of pointer
7570   // components, and non-deferred type-bound procedure bindings have not
7571   // yet been traversed.
7572   // We do that now, when any (formerly) forward references that appear
7573   // in those initializers will resolve to the right symbols without
7574   // incurring spurious errors with IMPLICIT NONE.
7575   DeferredCheckVisitor{*this}.Walk(node.spec());
7576   DeferredCheckVisitor{*this}.Walk(node.exec()); // for BLOCK
7577   for (Scope &childScope : currScope().children()) {
7578     if (childScope.IsParameterizedDerivedTypeInstantiation()) {
7579       FinishDerivedTypeInstantiation(childScope);
7580     }
7581   }
7582   for (const auto &child : node.children()) {
7583     FinishSpecificationParts(child);
7584   }
7585 }
7586 
7587 // Duplicate and fold component object pointer default initializer designators
7588 // using the actual type parameter values of each particular instantiation.
7589 // Validation is done later in declaration checking.
7590 void ResolveNamesVisitor::FinishDerivedTypeInstantiation(Scope &scope) {
7591   CHECK(scope.IsDerivedType() && !scope.symbol());
7592   if (DerivedTypeSpec * spec{scope.derivedTypeSpec()}) {
7593     spec->Instantiate(currScope());
7594     const Symbol &origTypeSymbol{spec->typeSymbol()};
7595     if (const Scope * origTypeScope{origTypeSymbol.scope()}) {
7596       CHECK(origTypeScope->IsDerivedType() &&
7597           origTypeScope->symbol() == &origTypeSymbol);
7598       auto &foldingContext{GetFoldingContext()};
7599       auto restorer{foldingContext.WithPDTInstance(*spec)};
7600       for (auto &pair : scope) {
7601         Symbol &comp{*pair.second};
7602         const Symbol &origComp{DEREF(FindInScope(*origTypeScope, comp.name()))};
7603         if (IsPointer(comp)) {
7604           if (auto *details{comp.detailsIf<ObjectEntityDetails>()}) {
7605             auto origDetails{origComp.get<ObjectEntityDetails>()};
7606             if (const MaybeExpr & init{origDetails.init()}) {
7607               SomeExpr newInit{*init};
7608               MaybeExpr folded{
7609                   evaluate::Fold(foldingContext, std::move(newInit))};
7610               details->set_init(std::move(folded));
7611             }
7612           }
7613         }
7614       }
7615     }
7616   }
7617 }
7618 
7619 // Resolve names in the execution part of this node and its children
7620 void ResolveNamesVisitor::ResolveExecutionParts(const ProgramTree &node) {
7621   if (!node.scope()) {
7622     return; // error occurred creating scope
7623   }
7624   SetScope(*node.scope());
7625   if (const auto *exec{node.exec()}) {
7626     Walk(*exec);
7627   }
7628   FinishNamelists();
7629   PopScope(); // converts unclassified entities into objects
7630   for (const auto &child : node.children()) {
7631     ResolveExecutionParts(child);
7632   }
7633 }
7634 
7635 void ResolveNamesVisitor::Post(const parser::Program &) {
7636   // ensure that all temps were deallocated
7637   CHECK(!attrs_);
7638   CHECK(!GetDeclTypeSpec());
7639 }
7640 
7641 // A singleton instance of the scope -> IMPLICIT rules mapping is
7642 // shared by all instances of ResolveNamesVisitor and accessed by this
7643 // pointer when the visitors (other than the top-level original) are
7644 // constructed.
7645 static ImplicitRulesMap *sharedImplicitRulesMap{nullptr};
7646 
7647 bool ResolveNames(
7648     SemanticsContext &context, const parser::Program &program, Scope &top) {
7649   ImplicitRulesMap implicitRulesMap;
7650   auto restorer{common::ScopedSet(sharedImplicitRulesMap, &implicitRulesMap)};
7651   ResolveNamesVisitor{context, implicitRulesMap, top}.Walk(program);
7652   return !context.AnyFatalError();
7653 }
7654 
7655 // Processes a module (but not internal) function when it is referenced
7656 // in a specification expression in a sibling procedure.
7657 void ResolveSpecificationParts(
7658     SemanticsContext &context, const Symbol &subprogram) {
7659   auto originalLocation{context.location()};
7660   ImplicitRulesMap implicitRulesMap;
7661   bool localImplicitRulesMap{false};
7662   if (!sharedImplicitRulesMap) {
7663     sharedImplicitRulesMap = &implicitRulesMap;
7664     localImplicitRulesMap = true;
7665   }
7666   ResolveNamesVisitor visitor{
7667       context, *sharedImplicitRulesMap, context.globalScope()};
7668   const auto &details{subprogram.get<SubprogramNameDetails>()};
7669   ProgramTree &node{details.node()};
7670   const Scope &moduleScope{subprogram.owner()};
7671   if (localImplicitRulesMap) {
7672     visitor.BeginScope(const_cast<Scope &>(moduleScope));
7673   } else {
7674     visitor.SetScope(const_cast<Scope &>(moduleScope));
7675   }
7676   visitor.ResolveSpecificationParts(node);
7677   context.set_location(std::move(originalLocation));
7678   if (localImplicitRulesMap) {
7679     sharedImplicitRulesMap = nullptr;
7680   }
7681 }
7682 
7683 } // namespace Fortran::semantics
7684