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