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