xref: /llvm-project/flang/lib/Semantics/resolve-names.cpp (revision d68332d0627f6492866298038e1085e4aff0f476)
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 "definable.h"
11 #include "mod-file.h"
12 #include "pointer-assignment.h"
13 #include "resolve-directives.h"
14 #include "resolve-names-utils.h"
15 #include "rewrite-parse-tree.h"
16 #include "flang/Common/Fortran.h"
17 #include "flang/Common/default-kinds.h"
18 #include "flang/Common/indirection.h"
19 #include "flang/Common/restorer.h"
20 #include "flang/Common/visit.h"
21 #include "flang/Evaluate/characteristics.h"
22 #include "flang/Evaluate/check-expression.h"
23 #include "flang/Evaluate/common.h"
24 #include "flang/Evaluate/fold-designator.h"
25 #include "flang/Evaluate/fold.h"
26 #include "flang/Evaluate/intrinsics.h"
27 #include "flang/Evaluate/tools.h"
28 #include "flang/Evaluate/type.h"
29 #include "flang/Parser/parse-tree-visitor.h"
30 #include "flang/Parser/parse-tree.h"
31 #include "flang/Parser/tools.h"
32 #include "flang/Semantics/attr.h"
33 #include "flang/Semantics/expression.h"
34 #include "flang/Semantics/program-tree.h"
35 #include "flang/Semantics/scope.h"
36 #include "flang/Semantics/semantics.h"
37 #include "flang/Semantics/symbol.h"
38 #include "flang/Semantics/tools.h"
39 #include "flang/Semantics/type.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <list>
42 #include <map>
43 #include <set>
44 #include <stack>
45 
46 namespace Fortran::semantics {
47 
48 using namespace parser::literals;
49 
50 template <typename T> using Indirection = common::Indirection<T>;
51 using Message = parser::Message;
52 using Messages = parser::Messages;
53 using MessageFixedText = parser::MessageFixedText;
54 using MessageFormattedText = parser::MessageFormattedText;
55 
56 class ResolveNamesVisitor;
57 class ScopeHandler;
58 
59 // ImplicitRules maps initial character of identifier to the DeclTypeSpec
60 // representing the implicit type; std::nullopt if none.
61 // It also records the presence of IMPLICIT NONE statements.
62 // When inheritFromParent is set, defaults come from the parent rules.
63 class ImplicitRules {
64 public:
65   ImplicitRules(SemanticsContext &context, const ImplicitRules *parent)
66       : parent_{parent}, context_{context},
67         inheritFromParent_{parent != nullptr} {}
68   bool isImplicitNoneType() const;
69   bool isImplicitNoneExternal() const;
70   void set_isImplicitNoneType(bool x) { isImplicitNoneType_ = x; }
71   void set_isImplicitNoneExternal(bool x) { isImplicitNoneExternal_ = x; }
72   void set_inheritFromParent(bool x) { inheritFromParent_ = x; }
73   // Get the implicit type for this name. May be null.
74   const DeclTypeSpec *GetType(
75       SourceName, bool respectImplicitNone = true) const;
76   // Record the implicit type for the range of characters [fromLetter,
77   // toLetter].
78   void SetTypeMapping(const DeclTypeSpec &type, parser::Location fromLetter,
79       parser::Location toLetter);
80 
81 private:
82   static char Incr(char ch);
83 
84   const ImplicitRules *parent_;
85   SemanticsContext &context_;
86   bool inheritFromParent_{false}; // look in parent if not specified here
87   bool isImplicitNoneType_{
88       context_.IsEnabled(common::LanguageFeature::ImplicitNoneTypeAlways)};
89   bool isImplicitNoneExternal_{false};
90   // map_ contains the mapping between letters and types that were defined
91   // by the IMPLICIT statements of the related scope. It does not contain
92   // the default Fortran mappings nor the mapping defined in parents.
93   std::map<char, common::Reference<const DeclTypeSpec>> map_;
94 
95   friend llvm::raw_ostream &operator<<(
96       llvm::raw_ostream &, const ImplicitRules &);
97   friend void ShowImplicitRule(
98       llvm::raw_ostream &, const ImplicitRules &, char);
99 };
100 
101 // scope -> implicit rules for that scope
102 using ImplicitRulesMap = std::map<const Scope *, ImplicitRules>;
103 
104 // Track statement source locations and save messages.
105 class MessageHandler {
106 public:
107   MessageHandler() { DIE("MessageHandler: default-constructed"); }
108   explicit MessageHandler(SemanticsContext &c) : context_{&c} {}
109   Messages &messages() { return context_->messages(); };
110   const std::optional<SourceName> &currStmtSource() {
111     return context_->location();
112   }
113   void set_currStmtSource(const std::optional<SourceName> &source) {
114     context_->set_location(source);
115   }
116 
117   // Emit a message associated with the current statement source.
118   Message &Say(MessageFixedText &&);
119   Message &Say(MessageFormattedText &&);
120   // Emit a message about a SourceName
121   Message &Say(const SourceName &, MessageFixedText &&);
122   // Emit a formatted message associated with a source location.
123   template <typename... A>
124   Message &Say(const SourceName &source, MessageFixedText &&msg, A &&...args) {
125     return context_->Say(source, std::move(msg), std::forward<A>(args)...);
126   }
127 
128 private:
129   SemanticsContext *context_;
130 };
131 
132 // Inheritance graph for the parse tree visitation classes that follow:
133 //   BaseVisitor
134 //   + AttrsVisitor
135 //   | + DeclTypeSpecVisitor
136 //   |   + ImplicitRulesVisitor
137 //   |     + ScopeHandler ------------------+
138 //   |       + ModuleVisitor -------------+ |
139 //   |       + GenericHandler -------+    | |
140 //   |       | + InterfaceVisitor    |    | |
141 //   |       +-+ SubprogramVisitor ==|==+ | |
142 //   + ArraySpecVisitor              |  | | |
143 //     + DeclarationVisitor <--------+  | | |
144 //       + ConstructVisitor             | | |
145 //         + ResolveNamesVisitor <------+-+-+
146 
147 class BaseVisitor {
148 public:
149   BaseVisitor() { DIE("BaseVisitor: default-constructed"); }
150   BaseVisitor(
151       SemanticsContext &c, ResolveNamesVisitor &v, ImplicitRulesMap &rules)
152       : implicitRulesMap_{&rules}, this_{&v}, context_{&c}, messageHandler_{c} {
153   }
154   template <typename T> void Walk(const T &);
155 
156   MessageHandler &messageHandler() { return messageHandler_; }
157   const std::optional<SourceName> &currStmtSource() {
158     return context_->location();
159   }
160   SemanticsContext &context() const { return *context_; }
161   evaluate::FoldingContext &GetFoldingContext() const {
162     return context_->foldingContext();
163   }
164   bool IsIntrinsic(
165       const SourceName &name, std::optional<Symbol::Flag> flag) const {
166     if (!flag) {
167       return context_->intrinsics().IsIntrinsic(name.ToString());
168     } else if (flag == Symbol::Flag::Function) {
169       return context_->intrinsics().IsIntrinsicFunction(name.ToString());
170     } else if (flag == Symbol::Flag::Subroutine) {
171       return context_->intrinsics().IsIntrinsicSubroutine(name.ToString());
172     } else {
173       DIE("expected Subroutine or Function flag");
174     }
175   }
176 
177   bool InModuleFile() const {
178     return GetFoldingContext().moduleFileName().has_value();
179   }
180 
181   // Make a placeholder symbol for a Name that otherwise wouldn't have one.
182   // It is not in any scope and always has MiscDetails.
183   void MakePlaceholder(const parser::Name &, MiscDetails::Kind);
184 
185   template <typename T> common::IfNoLvalue<T, T> FoldExpr(T &&expr) {
186     return evaluate::Fold(GetFoldingContext(), std::move(expr));
187   }
188 
189   template <typename T> MaybeExpr EvaluateExpr(const T &expr) {
190     return FoldExpr(AnalyzeExpr(*context_, expr));
191   }
192 
193   template <typename T>
194   MaybeExpr EvaluateNonPointerInitializer(
195       const Symbol &symbol, const T &expr, parser::CharBlock source) {
196     if (!context().HasError(symbol)) {
197       if (auto maybeExpr{AnalyzeExpr(*context_, expr)}) {
198         auto restorer{GetFoldingContext().messages().SetLocation(source)};
199         return evaluate::NonPointerInitializationExpr(
200             symbol, std::move(*maybeExpr), GetFoldingContext());
201       }
202     }
203     return std::nullopt;
204   }
205 
206   template <typename T> MaybeIntExpr EvaluateIntExpr(const T &expr) {
207     return semantics::EvaluateIntExpr(*context_, expr);
208   }
209 
210   template <typename T>
211   MaybeSubscriptIntExpr EvaluateSubscriptIntExpr(const T &expr) {
212     if (MaybeIntExpr maybeIntExpr{EvaluateIntExpr(expr)}) {
213       return FoldExpr(evaluate::ConvertToType<evaluate::SubscriptInteger>(
214           std::move(*maybeIntExpr)));
215     } else {
216       return std::nullopt;
217     }
218   }
219 
220   template <typename... A> Message &Say(A &&...args) {
221     return messageHandler_.Say(std::forward<A>(args)...);
222   }
223   template <typename... A>
224   Message &Say(
225       const parser::Name &name, MessageFixedText &&text, const A &...args) {
226     return messageHandler_.Say(name.source, std::move(text), args...);
227   }
228 
229 protected:
230   ImplicitRulesMap *implicitRulesMap_{nullptr};
231 
232 private:
233   ResolveNamesVisitor *this_;
234   SemanticsContext *context_;
235   MessageHandler messageHandler_;
236 };
237 
238 // Provide Post methods to collect attributes into a member variable.
239 class AttrsVisitor : public virtual BaseVisitor {
240 public:
241   bool BeginAttrs(); // always returns true
242   Attrs GetAttrs();
243   std::optional<common::CUDADataAttr> cudaDataAttr() { return cudaDataAttr_; }
244   Attrs EndAttrs();
245   bool SetPassNameOn(Symbol &);
246   void SetBindNameOn(Symbol &);
247   void Post(const parser::LanguageBindingSpec &);
248   bool Pre(const parser::IntentSpec &);
249   bool Pre(const parser::Pass &);
250 
251   bool CheckAndSet(Attr);
252 
253 // Simple case: encountering CLASSNAME causes ATTRNAME to be set.
254 #define HANDLE_ATTR_CLASS(CLASSNAME, ATTRNAME) \
255   bool Pre(const parser::CLASSNAME &) { \
256     CheckAndSet(Attr::ATTRNAME); \
257     return false; \
258   }
259   HANDLE_ATTR_CLASS(PrefixSpec::Elemental, ELEMENTAL)
260   HANDLE_ATTR_CLASS(PrefixSpec::Impure, IMPURE)
261   HANDLE_ATTR_CLASS(PrefixSpec::Module, MODULE)
262   HANDLE_ATTR_CLASS(PrefixSpec::Non_Recursive, NON_RECURSIVE)
263   HANDLE_ATTR_CLASS(PrefixSpec::Pure, PURE)
264   HANDLE_ATTR_CLASS(PrefixSpec::Recursive, RECURSIVE)
265   HANDLE_ATTR_CLASS(TypeAttrSpec::BindC, BIND_C)
266   HANDLE_ATTR_CLASS(BindAttr::Deferred, DEFERRED)
267   HANDLE_ATTR_CLASS(BindAttr::Non_Overridable, NON_OVERRIDABLE)
268   HANDLE_ATTR_CLASS(Abstract, ABSTRACT)
269   HANDLE_ATTR_CLASS(Allocatable, ALLOCATABLE)
270   HANDLE_ATTR_CLASS(Asynchronous, ASYNCHRONOUS)
271   HANDLE_ATTR_CLASS(Contiguous, CONTIGUOUS)
272   HANDLE_ATTR_CLASS(External, EXTERNAL)
273   HANDLE_ATTR_CLASS(Intrinsic, INTRINSIC)
274   HANDLE_ATTR_CLASS(NoPass, NOPASS)
275   HANDLE_ATTR_CLASS(Optional, OPTIONAL)
276   HANDLE_ATTR_CLASS(Parameter, PARAMETER)
277   HANDLE_ATTR_CLASS(Pointer, POINTER)
278   HANDLE_ATTR_CLASS(Protected, PROTECTED)
279   HANDLE_ATTR_CLASS(Save, SAVE)
280   HANDLE_ATTR_CLASS(Target, TARGET)
281   HANDLE_ATTR_CLASS(Value, VALUE)
282   HANDLE_ATTR_CLASS(Volatile, VOLATILE)
283 #undef HANDLE_ATTR_CLASS
284   bool Pre(const common::CUDADataAttr);
285 
286 protected:
287   std::optional<Attrs> attrs_;
288   std::optional<common::CUDADataAttr> cudaDataAttr_;
289 
290   Attr AccessSpecToAttr(const parser::AccessSpec &x) {
291     switch (x.v) {
292     case parser::AccessSpec::Kind::Public:
293       return Attr::PUBLIC;
294     case parser::AccessSpec::Kind::Private:
295       return Attr::PRIVATE;
296     }
297     llvm_unreachable("Switch covers all cases"); // suppress g++ warning
298   }
299   Attr IntentSpecToAttr(const parser::IntentSpec &x) {
300     switch (x.v) {
301     case parser::IntentSpec::Intent::In:
302       return Attr::INTENT_IN;
303     case parser::IntentSpec::Intent::Out:
304       return Attr::INTENT_OUT;
305     case parser::IntentSpec::Intent::InOut:
306       return Attr::INTENT_INOUT;
307     }
308     llvm_unreachable("Switch covers all cases"); // suppress g++ warning
309   }
310 
311 private:
312   bool IsDuplicateAttr(Attr);
313   bool HaveAttrConflict(Attr, Attr, Attr);
314   bool IsConflictingAttr(Attr);
315 
316   MaybeExpr bindName_; // from BIND(C, NAME="...")
317   bool isCDefined_{false}; // BIND(C, NAME="...", CDEFINED) extension
318   std::optional<SourceName> passName_; // from PASS(...)
319 };
320 
321 // Find and create types from declaration-type-spec nodes.
322 class DeclTypeSpecVisitor : public AttrsVisitor {
323 public:
324   using AttrsVisitor::Post;
325   using AttrsVisitor::Pre;
326   void Post(const parser::IntrinsicTypeSpec::DoublePrecision &);
327   void Post(const parser::IntrinsicTypeSpec::DoubleComplex &);
328   void Post(const parser::DeclarationTypeSpec::ClassStar &);
329   void Post(const parser::DeclarationTypeSpec::TypeStar &);
330   bool Pre(const parser::TypeGuardStmt &);
331   void Post(const parser::TypeGuardStmt &);
332   void Post(const parser::TypeSpec &);
333 
334   // Walk the parse tree of a type spec and return the DeclTypeSpec for it.
335   template <typename T>
336   const DeclTypeSpec *ProcessTypeSpec(const T &x, bool allowForward = false) {
337     auto restorer{common::ScopedSet(state_, State{})};
338     set_allowForwardReferenceToDerivedType(allowForward);
339     BeginDeclTypeSpec();
340     Walk(x);
341     const auto *type{GetDeclTypeSpec()};
342     EndDeclTypeSpec();
343     return type;
344   }
345 
346 protected:
347   struct State {
348     bool expectDeclTypeSpec{false}; // should see decl-type-spec only when true
349     const DeclTypeSpec *declTypeSpec{nullptr};
350     struct {
351       DerivedTypeSpec *type{nullptr};
352       DeclTypeSpec::Category category{DeclTypeSpec::TypeDerived};
353     } derived;
354     bool allowForwardReferenceToDerivedType{false};
355   };
356 
357   bool allowForwardReferenceToDerivedType() const {
358     return state_.allowForwardReferenceToDerivedType;
359   }
360   void set_allowForwardReferenceToDerivedType(bool yes) {
361     state_.allowForwardReferenceToDerivedType = yes;
362   }
363 
364   const DeclTypeSpec *GetDeclTypeSpec();
365   void BeginDeclTypeSpec();
366   void EndDeclTypeSpec();
367   void SetDeclTypeSpec(const DeclTypeSpec &);
368   void SetDeclTypeSpecCategory(DeclTypeSpec::Category);
369   DeclTypeSpec::Category GetDeclTypeSpecCategory() const {
370     return state_.derived.category;
371   }
372   KindExpr GetKindParamExpr(
373       TypeCategory, const std::optional<parser::KindSelector> &);
374   void CheckForAbstractType(const Symbol &typeSymbol);
375 
376 private:
377   State state_;
378 
379   void MakeNumericType(TypeCategory, int kind);
380 };
381 
382 // Visit ImplicitStmt and related parse tree nodes and updates implicit rules.
383 class ImplicitRulesVisitor : public DeclTypeSpecVisitor {
384 public:
385   using DeclTypeSpecVisitor::Post;
386   using DeclTypeSpecVisitor::Pre;
387   using ImplicitNoneNameSpec = parser::ImplicitStmt::ImplicitNoneNameSpec;
388 
389   void Post(const parser::ParameterStmt &);
390   bool Pre(const parser::ImplicitStmt &);
391   bool Pre(const parser::LetterSpec &);
392   bool Pre(const parser::ImplicitSpec &);
393   void Post(const parser::ImplicitSpec &);
394 
395   const DeclTypeSpec *GetType(
396       SourceName name, bool respectImplicitNoneType = true) {
397     return implicitRules_->GetType(name, respectImplicitNoneType);
398   }
399   bool isImplicitNoneType() const {
400     return implicitRules_->isImplicitNoneType();
401   }
402   bool isImplicitNoneType(const Scope &scope) const {
403     return implicitRulesMap_->at(&scope).isImplicitNoneType();
404   }
405   bool isImplicitNoneExternal() const {
406     return implicitRules_->isImplicitNoneExternal();
407   }
408   void set_inheritFromParent(bool x) {
409     implicitRules_->set_inheritFromParent(x);
410   }
411 
412 protected:
413   void BeginScope(const Scope &);
414   void SetScope(const Scope &);
415 
416 private:
417   // implicit rules in effect for current scope
418   ImplicitRules *implicitRules_{nullptr};
419   std::optional<SourceName> prevImplicit_;
420   std::optional<SourceName> prevImplicitNone_;
421   std::optional<SourceName> prevImplicitNoneType_;
422   std::optional<SourceName> prevParameterStmt_;
423 
424   bool HandleImplicitNone(const std::list<ImplicitNoneNameSpec> &nameSpecs);
425 };
426 
427 // Track array specifications. They can occur in AttrSpec, EntityDecl,
428 // ObjectDecl, DimensionStmt, CommonBlockObject, BasedPointer, and
429 // ComponentDecl.
430 // 1. INTEGER, DIMENSION(10) :: x
431 // 2. INTEGER :: x(10)
432 // 3. ALLOCATABLE :: x(:)
433 // 4. DIMENSION :: x(10)
434 // 5. COMMON x(10)
435 // 6. POINTER(p,x(10))
436 class ArraySpecVisitor : public virtual BaseVisitor {
437 public:
438   void Post(const parser::ArraySpec &);
439   void Post(const parser::ComponentArraySpec &);
440   void Post(const parser::CoarraySpec &);
441   void Post(const parser::AttrSpec &) { PostAttrSpec(); }
442   void Post(const parser::ComponentAttrSpec &) { PostAttrSpec(); }
443 
444 protected:
445   const ArraySpec &arraySpec();
446   void set_arraySpec(const ArraySpec arraySpec) { arraySpec_ = arraySpec; }
447   const ArraySpec &coarraySpec();
448   void BeginArraySpec();
449   void EndArraySpec();
450   void ClearArraySpec() { arraySpec_.clear(); }
451   void ClearCoarraySpec() { coarraySpec_.clear(); }
452 
453 private:
454   // arraySpec_/coarraySpec_ are populated from any ArraySpec/CoarraySpec
455   ArraySpec arraySpec_;
456   ArraySpec coarraySpec_;
457   // When an ArraySpec is under an AttrSpec or ComponentAttrSpec, it is moved
458   // into attrArraySpec_
459   ArraySpec attrArraySpec_;
460   ArraySpec attrCoarraySpec_;
461 
462   void PostAttrSpec();
463 };
464 
465 // Manages a stack of function result information.  We defer the processing
466 // of a type specification that appears in the prefix of a FUNCTION statement
467 // until the function result variable appears in the specification part
468 // or the end of the specification part.  This allows for forward references
469 // in the type specification to resolve to local names.
470 class FuncResultStack {
471 public:
472   explicit FuncResultStack(ScopeHandler &scopeHandler)
473       : scopeHandler_{scopeHandler} {}
474   ~FuncResultStack();
475 
476   struct FuncInfo {
477     FuncInfo(const Scope &s, SourceName at) : scope{s}, source{at} {}
478     const Scope &scope;
479     SourceName source;
480     // Parse tree of the type specification in the FUNCTION prefix
481     const parser::DeclarationTypeSpec *parsedType{nullptr};
482     // Name of the function RESULT in the FUNCTION suffix, if any
483     const parser::Name *resultName{nullptr};
484     // Result symbol
485     Symbol *resultSymbol{nullptr};
486     bool inFunctionStmt{false}; // true between Pre/Post of FunctionStmt
487   };
488 
489   // Completes the definition of the top function's result.
490   void CompleteFunctionResultType();
491   // Completes the definition of a symbol if it is the top function's result.
492   void CompleteTypeIfFunctionResult(Symbol &);
493 
494   FuncInfo *Top() { return stack_.empty() ? nullptr : &stack_.back(); }
495   FuncInfo &Push(const Scope &scope, SourceName at) {
496     return stack_.emplace_back(scope, at);
497   }
498   void Pop();
499 
500 private:
501   ScopeHandler &scopeHandler_;
502   std::vector<FuncInfo> stack_;
503 };
504 
505 // Manage a stack of Scopes
506 class ScopeHandler : public ImplicitRulesVisitor {
507 public:
508   using ImplicitRulesVisitor::Post;
509   using ImplicitRulesVisitor::Pre;
510 
511   Scope &currScope() { return DEREF(currScope_); }
512   // The enclosing host procedure if current scope is in an internal procedure
513   Scope *GetHostProcedure();
514   // The innermost enclosing program unit scope, ignoring BLOCK and other
515   // construct scopes.
516   Scope &InclusiveScope();
517   // The enclosing scope, skipping derived types.
518   Scope &NonDerivedTypeScope();
519 
520   // Create a new scope and push it on the scope stack.
521   void PushScope(Scope::Kind kind, Symbol *symbol);
522   void PushScope(Scope &scope);
523   void PopScope();
524   void SetScope(Scope &);
525 
526   template <typename T> bool Pre(const parser::Statement<T> &x) {
527     messageHandler().set_currStmtSource(x.source);
528     currScope_->AddSourceRange(x.source);
529     return true;
530   }
531   template <typename T> void Post(const parser::Statement<T> &) {
532     messageHandler().set_currStmtSource(std::nullopt);
533   }
534 
535   // Special messages: already declared; referencing symbol's declaration;
536   // about a type; two names & locations
537   void SayAlreadyDeclared(const parser::Name &, Symbol &);
538   void SayAlreadyDeclared(const SourceName &, Symbol &);
539   void SayAlreadyDeclared(const SourceName &, const SourceName &);
540   void SayWithReason(
541       const parser::Name &, Symbol &, MessageFixedText &&, Message &&);
542   template <typename... A>
543   Message &SayWithDecl(
544       const parser::Name &, Symbol &, MessageFixedText &&, A &&...args);
545   void SayLocalMustBeVariable(const parser::Name &, Symbol &);
546   Message &SayDerivedType(
547       const SourceName &, MessageFixedText &&, const Scope &);
548   Message &Say2(const SourceName &, MessageFixedText &&, const SourceName &,
549       MessageFixedText &&);
550   Message &Say2(
551       const SourceName &, MessageFixedText &&, Symbol &, MessageFixedText &&);
552   Message &Say2(
553       const parser::Name &, MessageFixedText &&, Symbol &, MessageFixedText &&);
554 
555   // Search for symbol by name in current, parent derived type, and
556   // containing scopes
557   Symbol *FindSymbol(const parser::Name &);
558   Symbol *FindSymbol(const Scope &, const parser::Name &);
559   // Search for name only in scope, not in enclosing scopes.
560   Symbol *FindInScope(const Scope &, const parser::Name &);
561   Symbol *FindInScope(const Scope &, const SourceName &);
562   template <typename T> Symbol *FindInScope(const T &name) {
563     return FindInScope(currScope(), name);
564   }
565   // Search for name in a derived type scope and its parents.
566   Symbol *FindInTypeOrParents(const Scope &, const parser::Name &);
567   Symbol *FindInTypeOrParents(const parser::Name &);
568   Symbol *FindInScopeOrBlockConstructs(const Scope &, SourceName);
569   Symbol *FindSeparateModuleProcedureInterface(const parser::Name &);
570   void EraseSymbol(const parser::Name &);
571   void EraseSymbol(const Symbol &symbol) { currScope().erase(symbol.name()); }
572   // Make a new symbol with the name and attrs of an existing one
573   Symbol &CopySymbol(const SourceName &, const Symbol &);
574 
575   // Make symbols in the current or named scope
576   Symbol &MakeSymbol(Scope &, const SourceName &, Attrs);
577   Symbol &MakeSymbol(const SourceName &, Attrs = Attrs{});
578   Symbol &MakeSymbol(const parser::Name &, Attrs = Attrs{});
579   Symbol &MakeHostAssocSymbol(const parser::Name &, const Symbol &);
580 
581   template <typename D>
582   common::IfNoLvalue<Symbol &, D> MakeSymbol(
583       const parser::Name &name, D &&details) {
584     return MakeSymbol(name, Attrs{}, std::move(details));
585   }
586 
587   template <typename D>
588   common::IfNoLvalue<Symbol &, D> MakeSymbol(
589       const parser::Name &name, const Attrs &attrs, D &&details) {
590     return Resolve(name, MakeSymbol(name.source, attrs, std::move(details)));
591   }
592 
593   template <typename D>
594   common::IfNoLvalue<Symbol &, D> MakeSymbol(
595       const SourceName &name, const Attrs &attrs, D &&details) {
596     // Note: don't use FindSymbol here. If this is a derived type scope,
597     // we want to detect whether the name is already declared as a component.
598     auto *symbol{FindInScope(name)};
599     if (!symbol) {
600       symbol = &MakeSymbol(name, attrs);
601       symbol->set_details(std::move(details));
602       return *symbol;
603     }
604     if constexpr (std::is_same_v<DerivedTypeDetails, D>) {
605       if (auto *d{symbol->detailsIf<GenericDetails>()}) {
606         if (!d->specific()) {
607           // derived type with same name as a generic
608           auto *derivedType{d->derivedType()};
609           if (!derivedType) {
610             derivedType =
611                 &currScope().MakeSymbol(name, attrs, std::move(details));
612             d->set_derivedType(*derivedType);
613           } else if (derivedType->CanReplaceDetails(details)) {
614             // was forward-referenced
615             CheckDuplicatedAttrs(name, *symbol, attrs);
616             SetExplicitAttrs(*derivedType, attrs);
617             derivedType->set_details(std::move(details));
618           } else {
619             SayAlreadyDeclared(name, *derivedType);
620           }
621           return *derivedType;
622         }
623       }
624     } else if constexpr (std::is_same_v<ProcEntityDetails, D>) {
625       if (auto *d{symbol->detailsIf<GenericDetails>()}) {
626         if (!d->derivedType()) {
627           // procedure pointer with same name as a generic
628           auto *specific{d->specific()};
629           if (!specific) {
630             specific = &currScope().MakeSymbol(name, attrs, std::move(details));
631             d->set_specific(*specific);
632           } else {
633             SayAlreadyDeclared(name, *specific);
634           }
635           return *specific;
636         }
637       }
638     }
639     if (symbol->CanReplaceDetails(details)) {
640       // update the existing symbol
641       CheckDuplicatedAttrs(name, *symbol, attrs);
642       SetExplicitAttrs(*symbol, attrs);
643       if constexpr (std::is_same_v<SubprogramDetails, D>) {
644         // Dummy argument defined by explicit interface?
645         details.set_isDummy(IsDummy(*symbol));
646       }
647       symbol->set_details(std::move(details));
648       return *symbol;
649     } else if constexpr (std::is_same_v<UnknownDetails, D>) {
650       CheckDuplicatedAttrs(name, *symbol, attrs);
651       SetExplicitAttrs(*symbol, attrs);
652       return *symbol;
653     } else {
654       if (!CheckPossibleBadForwardRef(*symbol)) {
655         if (name.empty() && symbol->name().empty()) {
656           // report the error elsewhere
657           return *symbol;
658         }
659         Symbol &errSym{*symbol};
660         if (auto *d{symbol->detailsIf<GenericDetails>()}) {
661           if (d->specific()) {
662             errSym = *d->specific();
663           } else if (d->derivedType()) {
664             errSym = *d->derivedType();
665           }
666         }
667         SayAlreadyDeclared(name, errSym);
668       }
669       // replace the old symbol with a new one with correct details
670       EraseSymbol(*symbol);
671       auto &result{MakeSymbol(name, attrs, std::move(details))};
672       context().SetError(result);
673       return result;
674     }
675   }
676 
677   void MakeExternal(Symbol &);
678 
679   // C815 duplicated attribute checking; returns false on error
680   bool CheckDuplicatedAttr(SourceName, Symbol &, Attr);
681   bool CheckDuplicatedAttrs(SourceName, Symbol &, Attrs);
682 
683   void SetExplicitAttr(Symbol &symbol, Attr attr) const {
684     symbol.attrs().set(attr);
685     symbol.implicitAttrs().reset(attr);
686   }
687   void SetExplicitAttrs(Symbol &symbol, Attrs attrs) const {
688     symbol.attrs() |= attrs;
689     symbol.implicitAttrs() &= ~attrs;
690   }
691   void SetImplicitAttr(Symbol &symbol, Attr attr) const {
692     symbol.attrs().set(attr);
693     symbol.implicitAttrs().set(attr);
694   }
695   void SetCUDADataAttr(
696       SourceName, Symbol &, std::optional<common::CUDADataAttr>);
697 
698 protected:
699   FuncResultStack &funcResultStack() { return funcResultStack_; }
700 
701   // Apply the implicit type rules to this symbol.
702   void ApplyImplicitRules(Symbol &, bool allowForwardReference = false);
703   bool ImplicitlyTypeForwardRef(Symbol &);
704   void AcquireIntrinsicProcedureFlags(Symbol &);
705   const DeclTypeSpec *GetImplicitType(
706       Symbol &, bool respectImplicitNoneType = true);
707   void CheckEntryDummyUse(SourceName, Symbol *);
708   bool ConvertToObjectEntity(Symbol &);
709   bool ConvertToProcEntity(Symbol &, std::optional<SourceName> = std::nullopt);
710 
711   const DeclTypeSpec &MakeNumericType(
712       TypeCategory, const std::optional<parser::KindSelector> &);
713   const DeclTypeSpec &MakeNumericType(TypeCategory, int);
714   const DeclTypeSpec &MakeLogicalType(
715       const std::optional<parser::KindSelector> &);
716   const DeclTypeSpec &MakeLogicalType(int);
717   void NotePossibleBadForwardRef(const parser::Name &);
718   std::optional<SourceName> HadForwardRef(const Symbol &) const;
719   bool CheckPossibleBadForwardRef(const Symbol &);
720 
721   bool inSpecificationPart_{false};
722   bool deferImplicitTyping_{false};
723   bool skipImplicitTyping_{false};
724   bool inEquivalenceStmt_{false};
725 
726   // Some information is collected from a specification part for deferred
727   // processing in DeclarationPartVisitor functions (e.g., CheckSaveStmts())
728   // that are called by ResolveNamesVisitor::FinishSpecificationPart().  Since
729   // specification parts can nest (e.g., INTERFACE bodies), the collected
730   // information that is not contained in the scope needs to be packaged
731   // and restorable.
732   struct SpecificationPartState {
733     std::set<SourceName> forwardRefs;
734     // Collect equivalence sets and process at end of specification part
735     std::vector<const std::list<parser::EquivalenceObject> *> equivalenceSets;
736     // Names of all common block objects in the scope
737     std::set<SourceName> commonBlockObjects;
738     // Info about SAVE statements and attributes in current scope
739     struct {
740       std::optional<SourceName> saveAll; // "SAVE" without entity list
741       std::set<SourceName> entities; // names of entities with save attr
742       std::set<SourceName> commons; // names of common blocks with save attr
743     } saveInfo;
744   } specPartState_;
745 
746   // Some declaration processing can and should be deferred to
747   // ResolveExecutionParts() to avoid prematurely creating implicitly-typed
748   // local symbols that should be host associations.
749   struct DeferredDeclarationState {
750     // The content of each namelist group
751     std::list<const parser::NamelistStmt::Group *> namelistGroups;
752   };
753   DeferredDeclarationState *GetDeferredDeclarationState(bool add = false) {
754     if (!add && deferred_.find(&currScope()) == deferred_.end()) {
755       return nullptr;
756     } else {
757       return &deferred_.emplace(&currScope(), DeferredDeclarationState{})
758                   .first->second;
759     }
760   }
761 
762   void SkipImplicitTyping(bool skip) {
763     deferImplicitTyping_ = skipImplicitTyping_ = skip;
764   }
765 
766 private:
767   Scope *currScope_{nullptr};
768   FuncResultStack funcResultStack_{*this};
769   std::map<Scope *, DeferredDeclarationState> deferred_;
770 };
771 
772 class ModuleVisitor : public virtual ScopeHandler {
773 public:
774   bool Pre(const parser::AccessStmt &);
775   bool Pre(const parser::Only &);
776   bool Pre(const parser::Rename::Names &);
777   bool Pre(const parser::Rename::Operators &);
778   bool Pre(const parser::UseStmt &);
779   void Post(const parser::UseStmt &);
780 
781   void BeginModule(const parser::Name &, bool isSubmodule);
782   bool BeginSubmodule(const parser::Name &, const parser::ParentIdentifier &);
783   void ApplyDefaultAccess();
784   Symbol &AddGenericUse(GenericDetails &, const SourceName &, const Symbol &);
785   void AddAndCheckModuleUse(SourceName, bool isIntrinsic);
786   void CollectUseRenames(const parser::UseStmt &);
787   void ClearUseRenames() { useRenames_.clear(); }
788   void ClearUseOnly() { useOnly_.clear(); }
789   void ClearModuleUses() {
790     intrinsicUses_.clear();
791     nonIntrinsicUses_.clear();
792   }
793 
794 private:
795   // The location of the last AccessStmt without access-ids, if any.
796   std::optional<SourceName> prevAccessStmt_;
797   // The scope of the module during a UseStmt
798   Scope *useModuleScope_{nullptr};
799   // Names that have appeared in a rename clause of USE statements
800   std::set<std::pair<SourceName, SourceName>> useRenames_;
801   // Names that have appeared in an ONLY clause of a USE statement
802   std::set<std::pair<SourceName, Scope *>> useOnly_;
803   // Intrinsic and non-intrinsic (explicit or not) module names that
804   // have appeared in USE statements; used for C1406 warnings.
805   std::set<SourceName> intrinsicUses_;
806   std::set<SourceName> nonIntrinsicUses_;
807 
808   Symbol &SetAccess(const SourceName &, Attr attr, Symbol * = nullptr);
809   // A rename in a USE statement: local => use
810   struct SymbolRename {
811     Symbol *local{nullptr};
812     Symbol *use{nullptr};
813   };
814   // Record a use from useModuleScope_ of use Name/Symbol as local Name/Symbol
815   SymbolRename AddUse(const SourceName &localName, const SourceName &useName);
816   SymbolRename AddUse(const SourceName &, const SourceName &, Symbol *);
817   void DoAddUse(
818       SourceName, SourceName, Symbol &localSymbol, const Symbol &useSymbol);
819   void AddUse(const GenericSpecInfo &);
820   // Record a name appearing as the target of a USE rename clause
821   void AddUseRename(SourceName name, SourceName moduleName) {
822     useRenames_.emplace(std::make_pair(name, moduleName));
823   }
824   bool IsUseRenamed(const SourceName &name) const {
825     return useModuleScope_ && useModuleScope_->symbol() &&
826         useRenames_.find({name, useModuleScope_->symbol()->name()}) !=
827         useRenames_.end();
828   }
829   // Record a name appearing in a USE ONLY clause
830   void AddUseOnly(const SourceName &name) {
831     useOnly_.emplace(std::make_pair(name, useModuleScope_));
832   }
833   bool IsUseOnly(const SourceName &name) const {
834     return useOnly_.find({name, useModuleScope_}) != useOnly_.end();
835   }
836   Scope *FindModule(const parser::Name &, std::optional<bool> isIntrinsic,
837       Scope *ancestor = nullptr);
838 };
839 
840 class GenericHandler : public virtual ScopeHandler {
841 protected:
842   using ProcedureKind = parser::ProcedureStmt::Kind;
843   void ResolveSpecificsInGeneric(Symbol &, bool isEndOfSpecificationPart);
844   void DeclaredPossibleSpecificProc(Symbol &);
845 
846   // Mappings of generics to their as-yet specific proc names and kinds
847   using SpecificProcMapType =
848       std::multimap<Symbol *, std::pair<const parser::Name *, ProcedureKind>>;
849   SpecificProcMapType specificsForGenericProcs_;
850   // inversion of SpecificProcMapType: maps pending proc names to generics
851   using GenericProcMapType = std::multimap<SourceName, Symbol *>;
852   GenericProcMapType genericsForSpecificProcs_;
853 };
854 
855 class InterfaceVisitor : public virtual ScopeHandler,
856                          public virtual GenericHandler {
857 public:
858   bool Pre(const parser::InterfaceStmt &);
859   void Post(const parser::InterfaceStmt &);
860   void Post(const parser::EndInterfaceStmt &);
861   bool Pre(const parser::GenericSpec &);
862   bool Pre(const parser::ProcedureStmt &);
863   bool Pre(const parser::GenericStmt &);
864   void Post(const parser::GenericStmt &);
865 
866   bool inInterfaceBlock() const;
867   bool isGeneric() const;
868   bool isAbstract() const;
869 
870 protected:
871   Symbol &GetGenericSymbol() { return DEREF(genericInfo_.top().symbol); }
872   // Add to generic the symbol for the subprogram with the same name
873   void CheckGenericProcedures(Symbol &);
874 
875 private:
876   // A new GenericInfo is pushed for each interface block and generic stmt
877   struct GenericInfo {
878     GenericInfo(bool isInterface, bool isAbstract = false)
879         : isInterface{isInterface}, isAbstract{isAbstract} {}
880     bool isInterface; // in interface block
881     bool isAbstract; // in abstract interface block
882     Symbol *symbol{nullptr}; // the generic symbol being defined
883   };
884   std::stack<GenericInfo> genericInfo_;
885   const GenericInfo &GetGenericInfo() const { return genericInfo_.top(); }
886   void SetGenericSymbol(Symbol &symbol) { genericInfo_.top().symbol = &symbol; }
887   void AddSpecificProcs(const std::list<parser::Name> &, ProcedureKind);
888   void ResolveNewSpecifics();
889 };
890 
891 class SubprogramVisitor : public virtual ScopeHandler, public InterfaceVisitor {
892 public:
893   bool HandleStmtFunction(const parser::StmtFunctionStmt &);
894   bool Pre(const parser::SubroutineStmt &);
895   bool Pre(const parser::FunctionStmt &);
896   void Post(const parser::FunctionStmt &);
897   bool Pre(const parser::EntryStmt &);
898   void Post(const parser::EntryStmt &);
899   bool Pre(const parser::InterfaceBody::Subroutine &);
900   void Post(const parser::InterfaceBody::Subroutine &);
901   bool Pre(const parser::InterfaceBody::Function &);
902   void Post(const parser::InterfaceBody::Function &);
903   bool Pre(const parser::Suffix &);
904   bool Pre(const parser::PrefixSpec &);
905   bool Pre(const parser::PrefixSpec::Attributes &);
906   void Post(const parser::PrefixSpec::Launch_Bounds &);
907   void Post(const parser::PrefixSpec::Cluster_Dims &);
908 
909   bool BeginSubprogram(const parser::Name &, Symbol::Flag,
910       bool hasModulePrefix = false,
911       const parser::LanguageBindingSpec * = nullptr,
912       const ProgramTree::EntryStmtList * = nullptr);
913   bool BeginMpSubprogram(const parser::Name &);
914   void PushBlockDataScope(const parser::Name &);
915   void EndSubprogram(std::optional<parser::CharBlock> stmtSource = std::nullopt,
916       const std::optional<parser::LanguageBindingSpec> * = nullptr,
917       const ProgramTree::EntryStmtList * = nullptr);
918 
919 protected:
920   // Set when we see a stmt function that is really an array element assignment
921   bool misparsedStmtFuncFound_{false};
922 
923 private:
924   // Edits an existing symbol created for earlier calls to a subprogram or ENTRY
925   // so that it can be replaced by a later definition.
926   bool HandlePreviousCalls(const parser::Name &, Symbol &, Symbol::Flag);
927   void CheckExtantProc(const parser::Name &, Symbol::Flag);
928   // Create a subprogram symbol in the current scope and push a new scope.
929   Symbol &PushSubprogramScope(const parser::Name &, Symbol::Flag,
930       const parser::LanguageBindingSpec * = nullptr,
931       bool hasModulePrefix = false);
932   Symbol *GetSpecificFromGeneric(const parser::Name &);
933   Symbol &PostSubprogramStmt();
934   void CreateDummyArgument(SubprogramDetails &, const parser::Name &);
935   void CreateEntry(const parser::EntryStmt &stmt, Symbol &subprogram);
936   void PostEntryStmt(const parser::EntryStmt &stmt);
937   void HandleLanguageBinding(Symbol *,
938       std::optional<parser::CharBlock> stmtSource,
939       const std::optional<parser::LanguageBindingSpec> *);
940 };
941 
942 class DeclarationVisitor : public ArraySpecVisitor,
943                            public virtual GenericHandler {
944 public:
945   using ArraySpecVisitor::Post;
946   using ScopeHandler::Post;
947   using ScopeHandler::Pre;
948 
949   bool Pre(const parser::Initialization &);
950   void Post(const parser::EntityDecl &);
951   void Post(const parser::ObjectDecl &);
952   void Post(const parser::PointerDecl &);
953   bool Pre(const parser::BindStmt &) { return BeginAttrs(); }
954   void Post(const parser::BindStmt &) { EndAttrs(); }
955   bool Pre(const parser::BindEntity &);
956   bool Pre(const parser::OldParameterStmt &);
957   bool Pre(const parser::NamedConstantDef &);
958   bool Pre(const parser::NamedConstant &);
959   void Post(const parser::EnumDef &);
960   bool Pre(const parser::Enumerator &);
961   bool Pre(const parser::AccessSpec &);
962   bool Pre(const parser::AsynchronousStmt &);
963   bool Pre(const parser::ContiguousStmt &);
964   bool Pre(const parser::ExternalStmt &);
965   bool Pre(const parser::IntentStmt &);
966   bool Pre(const parser::IntrinsicStmt &);
967   bool Pre(const parser::OptionalStmt &);
968   bool Pre(const parser::ProtectedStmt &);
969   bool Pre(const parser::ValueStmt &);
970   bool Pre(const parser::VolatileStmt &);
971   bool Pre(const parser::AllocatableStmt &) {
972     objectDeclAttr_ = Attr::ALLOCATABLE;
973     return true;
974   }
975   void Post(const parser::AllocatableStmt &) { objectDeclAttr_ = std::nullopt; }
976   bool Pre(const parser::TargetStmt &) {
977     objectDeclAttr_ = Attr::TARGET;
978     return true;
979   }
980   bool Pre(const parser::CUDAAttributesStmt &);
981   void Post(const parser::TargetStmt &) { objectDeclAttr_ = std::nullopt; }
982   void Post(const parser::DimensionStmt::Declaration &);
983   void Post(const parser::CodimensionDecl &);
984   bool Pre(const parser::TypeDeclarationStmt &);
985   void Post(const parser::TypeDeclarationStmt &);
986   void Post(const parser::IntegerTypeSpec &);
987   void Post(const parser::IntrinsicTypeSpec::Real &);
988   void Post(const parser::IntrinsicTypeSpec::Complex &);
989   void Post(const parser::IntrinsicTypeSpec::Logical &);
990   void Post(const parser::IntrinsicTypeSpec::Character &);
991   void Post(const parser::CharSelector::LengthAndKind &);
992   void Post(const parser::CharLength &);
993   void Post(const parser::LengthSelector &);
994   bool Pre(const parser::KindParam &);
995   bool Pre(const parser::VectorTypeSpec &);
996   void Post(const parser::VectorTypeSpec &);
997   bool Pre(const parser::DeclarationTypeSpec::Type &);
998   void Post(const parser::DeclarationTypeSpec::Type &);
999   bool Pre(const parser::DeclarationTypeSpec::Class &);
1000   void Post(const parser::DeclarationTypeSpec::Class &);
1001   void Post(const parser::DeclarationTypeSpec::Record &);
1002   void Post(const parser::DerivedTypeSpec &);
1003   bool Pre(const parser::DerivedTypeDef &);
1004   bool Pre(const parser::DerivedTypeStmt &);
1005   void Post(const parser::DerivedTypeStmt &);
1006   bool Pre(const parser::TypeParamDefStmt &) { return BeginDecl(); }
1007   void Post(const parser::TypeParamDefStmt &);
1008   bool Pre(const parser::TypeAttrSpec::Extends &);
1009   bool Pre(const parser::PrivateStmt &);
1010   bool Pre(const parser::SequenceStmt &);
1011   bool Pre(const parser::ComponentDefStmt &) { return BeginDecl(); }
1012   void Post(const parser::ComponentDefStmt &) { EndDecl(); }
1013   void Post(const parser::ComponentDecl &);
1014   void Post(const parser::FillDecl &);
1015   bool Pre(const parser::ProcedureDeclarationStmt &);
1016   void Post(const parser::ProcedureDeclarationStmt &);
1017   bool Pre(const parser::DataComponentDefStmt &); // returns false
1018   bool Pre(const parser::ProcComponentDefStmt &);
1019   void Post(const parser::ProcComponentDefStmt &);
1020   bool Pre(const parser::ProcPointerInit &);
1021   void Post(const parser::ProcInterface &);
1022   void Post(const parser::ProcDecl &);
1023   bool Pre(const parser::TypeBoundProcedurePart &);
1024   void Post(const parser::TypeBoundProcedurePart &);
1025   void Post(const parser::ContainsStmt &);
1026   bool Pre(const parser::TypeBoundProcBinding &) { return BeginAttrs(); }
1027   void Post(const parser::TypeBoundProcBinding &) { EndAttrs(); }
1028   void Post(const parser::TypeBoundProcedureStmt::WithoutInterface &);
1029   void Post(const parser::TypeBoundProcedureStmt::WithInterface &);
1030   bool Pre(const parser::FinalProcedureStmt &);
1031   bool Pre(const parser::TypeBoundGenericStmt &);
1032   bool Pre(const parser::StructureDef &); // returns false
1033   bool Pre(const parser::Union::UnionStmt &);
1034   bool Pre(const parser::StructureField &);
1035   void Post(const parser::StructureField &);
1036   bool Pre(const parser::AllocateStmt &);
1037   void Post(const parser::AllocateStmt &);
1038   bool Pre(const parser::StructureConstructor &);
1039   bool Pre(const parser::NamelistStmt::Group &);
1040   bool Pre(const parser::IoControlSpec &);
1041   bool Pre(const parser::CommonStmt::Block &);
1042   bool Pre(const parser::CommonBlockObject &);
1043   void Post(const parser::CommonBlockObject &);
1044   bool Pre(const parser::EquivalenceStmt &);
1045   bool Pre(const parser::SaveStmt &);
1046   bool Pre(const parser::BasedPointer &);
1047   void Post(const parser::BasedPointer &);
1048 
1049   void PointerInitialization(
1050       const parser::Name &, const parser::InitialDataTarget &);
1051   void PointerInitialization(
1052       const parser::Name &, const parser::ProcPointerInit &);
1053   void NonPointerInitialization(
1054       const parser::Name &, const parser::ConstantExpr &);
1055   void CheckExplicitInterface(const parser::Name &);
1056   void CheckBindings(const parser::TypeBoundProcedureStmt::WithoutInterface &);
1057 
1058   const parser::Name *ResolveDesignator(const parser::Designator &);
1059   int GetVectorElementKind(
1060       TypeCategory category, const std::optional<parser::KindSelector> &kind);
1061 
1062 protected:
1063   bool BeginDecl();
1064   void EndDecl();
1065   Symbol &DeclareObjectEntity(const parser::Name &, Attrs = Attrs{});
1066   // Make sure that there's an entity in an enclosing scope called Name
1067   Symbol &FindOrDeclareEnclosingEntity(const parser::Name &);
1068   // Declare a LOCAL/LOCAL_INIT/REDUCE entity while setting a locality flag. If
1069   // there isn't a type specified it comes from the entity in the containing
1070   // scope, or implicit rules.
1071   void DeclareLocalEntity(const parser::Name &, Symbol::Flag);
1072   // Declare a statement entity (i.e., an implied DO loop index for
1073   // a DATA statement or an array constructor).  If there isn't an explict
1074   // type specified, implicit rules apply. Return pointer to the new symbol,
1075   // or nullptr on error.
1076   Symbol *DeclareStatementEntity(const parser::DoVariable &,
1077       const std::optional<parser::IntegerTypeSpec> &);
1078   Symbol &MakeCommonBlockSymbol(const parser::Name &);
1079   Symbol &MakeCommonBlockSymbol(const std::optional<parser::Name> &);
1080   bool CheckUseError(const parser::Name &);
1081   void CheckAccessibility(const SourceName &, bool, Symbol &);
1082   void CheckCommonBlocks();
1083   void CheckSaveStmts();
1084   void CheckEquivalenceSets();
1085   bool CheckNotInBlock(const char *);
1086   bool NameIsKnownOrIntrinsic(const parser::Name &);
1087   void FinishNamelists();
1088 
1089   // Each of these returns a pointer to a resolved Name (i.e. with symbol)
1090   // or nullptr in case of error.
1091   const parser::Name *ResolveStructureComponent(
1092       const parser::StructureComponent &);
1093   const parser::Name *ResolveDataRef(const parser::DataRef &);
1094   const parser::Name *ResolveName(const parser::Name &);
1095   bool PassesSharedLocalityChecks(const parser::Name &name, Symbol &symbol);
1096   Symbol *NoteInterfaceName(const parser::Name &);
1097   bool IsUplevelReference(const Symbol &);
1098 
1099   std::optional<SourceName> BeginCheckOnIndexUseInOwnBounds(
1100       const parser::DoVariable &name) {
1101     std::optional<SourceName> result{checkIndexUseInOwnBounds_};
1102     checkIndexUseInOwnBounds_ = name.thing.thing.source;
1103     return result;
1104   }
1105   void EndCheckOnIndexUseInOwnBounds(const std::optional<SourceName> &restore) {
1106     checkIndexUseInOwnBounds_ = restore;
1107   }
1108   void NoteScalarSpecificationArgument(const Symbol &symbol) {
1109     mustBeScalar_.emplace(symbol);
1110   }
1111   // Declare an object or procedure entity.
1112   // T is one of: EntityDetails, ObjectEntityDetails, ProcEntityDetails
1113   template <typename T>
1114   Symbol &DeclareEntity(const parser::Name &name, Attrs attrs) {
1115     Symbol &symbol{MakeSymbol(name, attrs)};
1116     if (context().HasError(symbol) || symbol.has<T>()) {
1117       return symbol; // OK or error already reported
1118     } else if (symbol.has<UnknownDetails>()) {
1119       symbol.set_details(T{});
1120       return symbol;
1121     } else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
1122       symbol.set_details(T{std::move(*details)});
1123       return symbol;
1124     } else if (std::is_same_v<EntityDetails, T> &&
1125         (symbol.has<ObjectEntityDetails>() ||
1126             symbol.has<ProcEntityDetails>())) {
1127       return symbol; // OK
1128     } else if (auto *details{symbol.detailsIf<UseDetails>()}) {
1129       Say(name.source,
1130           "'%s' is use-associated from module '%s' and cannot be re-declared"_err_en_US,
1131           name.source, GetUsedModule(*details).name());
1132     } else if (auto *details{symbol.detailsIf<SubprogramNameDetails>()}) {
1133       if (details->kind() == SubprogramKind::Module) {
1134         Say2(name,
1135             "Declaration of '%s' conflicts with its use as module procedure"_err_en_US,
1136             symbol, "Module procedure definition"_en_US);
1137       } else if (details->kind() == SubprogramKind::Internal) {
1138         Say2(name,
1139             "Declaration of '%s' conflicts with its use as internal procedure"_err_en_US,
1140             symbol, "Internal procedure definition"_en_US);
1141       } else {
1142         DIE("unexpected kind");
1143       }
1144     } else if (std::is_same_v<ObjectEntityDetails, T> &&
1145         symbol.has<ProcEntityDetails>()) {
1146       SayWithDecl(
1147           name, symbol, "'%s' is already declared as a procedure"_err_en_US);
1148     } else if (std::is_same_v<ProcEntityDetails, T> &&
1149         symbol.has<ObjectEntityDetails>()) {
1150       if (FindCommonBlockContaining(symbol)) {
1151         SayWithDecl(name, symbol,
1152             "'%s' may not be a procedure as it is in a COMMON block"_err_en_US);
1153       } else {
1154         SayWithDecl(
1155             name, symbol, "'%s' is already declared as an object"_err_en_US);
1156       }
1157     } else if (!CheckPossibleBadForwardRef(symbol)) {
1158       SayAlreadyDeclared(name, symbol);
1159     }
1160     context().SetError(symbol);
1161     return symbol;
1162   }
1163 
1164 private:
1165   // The attribute corresponding to the statement containing an ObjectDecl
1166   std::optional<Attr> objectDeclAttr_;
1167   // Info about current character type while walking DeclTypeSpec.
1168   // Also captures any "*length" specifier on an individual declaration.
1169   struct {
1170     std::optional<ParamValue> length;
1171     std::optional<KindExpr> kind;
1172   } charInfo_;
1173   // Info about current derived type or STRUCTURE while walking
1174   // DerivedTypeDef / StructureDef
1175   struct {
1176     const parser::Name *extends{nullptr}; // EXTENDS(name)
1177     bool privateComps{false}; // components are private by default
1178     bool privateBindings{false}; // bindings are private by default
1179     bool sawContains{false}; // currently processing bindings
1180     bool sequence{false}; // is a sequence type
1181     const Symbol *type{nullptr}; // derived type being defined
1182     bool isStructure{false}; // is a DEC STRUCTURE
1183   } derivedTypeInfo_;
1184   // In a ProcedureDeclarationStmt or ProcComponentDefStmt, this is
1185   // the interface name, if any.
1186   const parser::Name *interfaceName_{nullptr};
1187   // Map type-bound generic to binding names of its specific bindings
1188   std::multimap<Symbol *, const parser::Name *> genericBindings_;
1189   // Info about current ENUM
1190   struct EnumeratorState {
1191     // Enum value must hold inside a C_INT (7.6.2).
1192     std::optional<int> value{0};
1193   } enumerationState_;
1194   // Set for OldParameterStmt processing
1195   bool inOldStyleParameterStmt_{false};
1196   // Set when walking DATA & array constructor implied DO loop bounds
1197   // to warn about use of the implied DO intex therein.
1198   std::optional<SourceName> checkIndexUseInOwnBounds_;
1199   bool isVectorType_{false};
1200   UnorderedSymbolSet mustBeScalar_;
1201 
1202   bool HandleAttributeStmt(Attr, const std::list<parser::Name> &);
1203   Symbol &HandleAttributeStmt(Attr, const parser::Name &);
1204   Symbol &DeclareUnknownEntity(const parser::Name &, Attrs);
1205   Symbol &DeclareProcEntity(
1206       const parser::Name &, Attrs, const Symbol *interface);
1207   void SetType(const parser::Name &, const DeclTypeSpec &);
1208   std::optional<DerivedTypeSpec> ResolveDerivedType(const parser::Name &);
1209   std::optional<DerivedTypeSpec> ResolveExtendsType(
1210       const parser::Name &, const parser::Name *);
1211   Symbol *MakeTypeSymbol(const SourceName &, Details &&);
1212   Symbol *MakeTypeSymbol(const parser::Name &, Details &&);
1213   bool OkToAddComponent(const parser::Name &, const Symbol *extends = nullptr);
1214   ParamValue GetParamValue(
1215       const parser::TypeParamValue &, common::TypeParamAttr attr);
1216   void CheckCommonBlockDerivedType(
1217       const SourceName &, const Symbol &, UnorderedSymbolSet &);
1218   Attrs HandleSaveName(const SourceName &, Attrs);
1219   void AddSaveName(std::set<SourceName> &, const SourceName &);
1220   bool HandleUnrestrictedSpecificIntrinsicFunction(const parser::Name &);
1221   const parser::Name *FindComponent(const parser::Name *, const parser::Name &);
1222   void Initialization(const parser::Name &, const parser::Initialization &,
1223       bool inComponentDecl);
1224   bool PassesLocalityChecks(
1225       const parser::Name &name, Symbol &symbol, Symbol::Flag flag);
1226   bool CheckForHostAssociatedImplicit(const parser::Name &);
1227   bool HasCycle(const Symbol &, const Symbol *interface);
1228   bool MustBeScalar(const Symbol &symbol) const {
1229     return mustBeScalar_.find(symbol) != mustBeScalar_.end();
1230   }
1231   void DeclareIntrinsic(const parser::Name &);
1232 };
1233 
1234 // Resolve construct entities and statement entities.
1235 // Check that construct names don't conflict with other names.
1236 class ConstructVisitor : public virtual DeclarationVisitor {
1237 public:
1238   bool Pre(const parser::ConcurrentHeader &);
1239   bool Pre(const parser::LocalitySpec::Local &);
1240   bool Pre(const parser::LocalitySpec::LocalInit &);
1241   bool Pre(const parser::LocalitySpec::Reduce &);
1242   bool Pre(const parser::LocalitySpec::Shared &);
1243   bool Pre(const parser::AcSpec &);
1244   bool Pre(const parser::AcImpliedDo &);
1245   bool Pre(const parser::DataImpliedDo &);
1246   bool Pre(const parser::DataIDoObject &);
1247   bool Pre(const parser::DataStmtObject &);
1248   bool Pre(const parser::DataStmtValue &);
1249   bool Pre(const parser::DoConstruct &);
1250   void Post(const parser::DoConstruct &);
1251   bool Pre(const parser::ForallConstruct &);
1252   void Post(const parser::ForallConstruct &);
1253   bool Pre(const parser::ForallStmt &);
1254   void Post(const parser::ForallStmt &);
1255   bool Pre(const parser::BlockConstruct &);
1256   void Post(const parser::Selector &);
1257   void Post(const parser::AssociateStmt &);
1258   void Post(const parser::EndAssociateStmt &);
1259   bool Pre(const parser::Association &);
1260   void Post(const parser::SelectTypeStmt &);
1261   void Post(const parser::SelectRankStmt &);
1262   bool Pre(const parser::SelectTypeConstruct &);
1263   void Post(const parser::SelectTypeConstruct &);
1264   bool Pre(const parser::SelectTypeConstruct::TypeCase &);
1265   void Post(const parser::SelectTypeConstruct::TypeCase &);
1266   // Creates Block scopes with neither symbol name nor symbol details.
1267   bool Pre(const parser::SelectRankConstruct::RankCase &);
1268   void Post(const parser::SelectRankConstruct::RankCase &);
1269   bool Pre(const parser::TypeGuardStmt::Guard &);
1270   void Post(const parser::TypeGuardStmt::Guard &);
1271   void Post(const parser::SelectRankCaseStmt::Rank &);
1272   bool Pre(const parser::ChangeTeamStmt &);
1273   void Post(const parser::EndChangeTeamStmt &);
1274   void Post(const parser::CoarrayAssociation &);
1275 
1276   // Definitions of construct names
1277   bool Pre(const parser::WhereConstructStmt &x) { return CheckDef(x.t); }
1278   bool Pre(const parser::ForallConstructStmt &x) { return CheckDef(x.t); }
1279   bool Pre(const parser::CriticalStmt &x) { return CheckDef(x.t); }
1280   bool Pre(const parser::LabelDoStmt &) {
1281     return false; // error recovery
1282   }
1283   bool Pre(const parser::NonLabelDoStmt &x) { return CheckDef(x.t); }
1284   bool Pre(const parser::IfThenStmt &x) { return CheckDef(x.t); }
1285   bool Pre(const parser::SelectCaseStmt &x) { return CheckDef(x.t); }
1286   bool Pre(const parser::SelectRankConstruct &);
1287   void Post(const parser::SelectRankConstruct &);
1288   bool Pre(const parser::SelectRankStmt &x) {
1289     return CheckDef(std::get<0>(x.t));
1290   }
1291   bool Pre(const parser::SelectTypeStmt &x) {
1292     return CheckDef(std::get<0>(x.t));
1293   }
1294 
1295   // References to construct names
1296   void Post(const parser::MaskedElsewhereStmt &x) { CheckRef(x.t); }
1297   void Post(const parser::ElsewhereStmt &x) { CheckRef(x.v); }
1298   void Post(const parser::EndWhereStmt &x) { CheckRef(x.v); }
1299   void Post(const parser::EndForallStmt &x) { CheckRef(x.v); }
1300   void Post(const parser::EndCriticalStmt &x) { CheckRef(x.v); }
1301   void Post(const parser::EndDoStmt &x) { CheckRef(x.v); }
1302   void Post(const parser::ElseIfStmt &x) { CheckRef(x.t); }
1303   void Post(const parser::ElseStmt &x) { CheckRef(x.v); }
1304   void Post(const parser::EndIfStmt &x) { CheckRef(x.v); }
1305   void Post(const parser::CaseStmt &x) { CheckRef(x.t); }
1306   void Post(const parser::EndSelectStmt &x) { CheckRef(x.v); }
1307   void Post(const parser::SelectRankCaseStmt &x) { CheckRef(x.t); }
1308   void Post(const parser::TypeGuardStmt &x) { CheckRef(x.t); }
1309   void Post(const parser::CycleStmt &x) { CheckRef(x.v); }
1310   void Post(const parser::ExitStmt &x) { CheckRef(x.v); }
1311 
1312   void HandleImpliedAsynchronousInScope(const parser::Block &);
1313 
1314 private:
1315   // R1105 selector -> expr | variable
1316   // expr is set in either case unless there were errors
1317   struct Selector {
1318     Selector() {}
1319     Selector(const SourceName &source, MaybeExpr &&expr)
1320         : source{source}, expr{std::move(expr)} {}
1321     operator bool() const { return expr.has_value(); }
1322     parser::CharBlock source;
1323     MaybeExpr expr;
1324   };
1325   // association -> [associate-name =>] selector
1326   struct Association {
1327     const parser::Name *name{nullptr};
1328     Selector selector;
1329   };
1330   std::vector<Association> associationStack_;
1331   Association *currentAssociation_{nullptr};
1332 
1333   template <typename T> bool CheckDef(const T &t) {
1334     return CheckDef(std::get<std::optional<parser::Name>>(t));
1335   }
1336   template <typename T> void CheckRef(const T &t) {
1337     CheckRef(std::get<std::optional<parser::Name>>(t));
1338   }
1339   bool CheckDef(const std::optional<parser::Name> &);
1340   void CheckRef(const std::optional<parser::Name> &);
1341   const DeclTypeSpec &ToDeclTypeSpec(evaluate::DynamicType &&);
1342   const DeclTypeSpec &ToDeclTypeSpec(
1343       evaluate::DynamicType &&, MaybeSubscriptIntExpr &&length);
1344   Symbol *MakeAssocEntity();
1345   void SetTypeFromAssociation(Symbol &);
1346   void SetAttrsFromAssociation(Symbol &);
1347   Selector ResolveSelector(const parser::Selector &);
1348   void ResolveIndexName(const parser::ConcurrentControl &control);
1349   void SetCurrentAssociation(std::size_t n);
1350   Association &GetCurrentAssociation();
1351   void PushAssociation();
1352   void PopAssociation(std::size_t count = 1);
1353 };
1354 
1355 // Create scopes for OpenACC constructs
1356 class AccVisitor : public virtual DeclarationVisitor {
1357 public:
1358   void AddAccSourceRange(const parser::CharBlock &);
1359 
1360   static bool NeedsScope(const parser::OpenACCBlockConstruct &);
1361 
1362   bool Pre(const parser::OpenACCBlockConstruct &);
1363   void Post(const parser::OpenACCBlockConstruct &);
1364   bool Pre(const parser::OpenACCCombinedConstruct &);
1365   void Post(const parser::OpenACCCombinedConstruct &);
1366   bool Pre(const parser::AccBeginBlockDirective &x) {
1367     AddAccSourceRange(x.source);
1368     return true;
1369   }
1370   void Post(const parser::AccBeginBlockDirective &) {
1371     messageHandler().set_currStmtSource(std::nullopt);
1372   }
1373   bool Pre(const parser::AccEndBlockDirective &x) {
1374     AddAccSourceRange(x.source);
1375     return true;
1376   }
1377   void Post(const parser::AccEndBlockDirective &) {
1378     messageHandler().set_currStmtSource(std::nullopt);
1379   }
1380   bool Pre(const parser::AccBeginLoopDirective &x) {
1381     AddAccSourceRange(x.source);
1382     return true;
1383   }
1384   void Post(const parser::AccBeginLoopDirective &x) {
1385     messageHandler().set_currStmtSource(std::nullopt);
1386   }
1387 };
1388 
1389 bool AccVisitor::NeedsScope(const parser::OpenACCBlockConstruct &x) {
1390   const auto &beginBlockDir{std::get<parser::AccBeginBlockDirective>(x.t)};
1391   const auto &beginDir{std::get<parser::AccBlockDirective>(beginBlockDir.t)};
1392   switch (beginDir.v) {
1393   case llvm::acc::Directive::ACCD_data:
1394   case llvm::acc::Directive::ACCD_host_data:
1395   case llvm::acc::Directive::ACCD_kernels:
1396   case llvm::acc::Directive::ACCD_parallel:
1397   case llvm::acc::Directive::ACCD_serial:
1398     return true;
1399   default:
1400     return false;
1401   }
1402 }
1403 
1404 void AccVisitor::AddAccSourceRange(const parser::CharBlock &source) {
1405   messageHandler().set_currStmtSource(source);
1406   currScope().AddSourceRange(source);
1407 }
1408 
1409 bool AccVisitor::Pre(const parser::OpenACCBlockConstruct &x) {
1410   if (NeedsScope(x)) {
1411     PushScope(Scope::Kind::OpenACCConstruct, nullptr);
1412   }
1413   return true;
1414 }
1415 
1416 void AccVisitor::Post(const parser::OpenACCBlockConstruct &x) {
1417   if (NeedsScope(x)) {
1418     PopScope();
1419   }
1420 }
1421 
1422 bool AccVisitor::Pre(const parser::OpenACCCombinedConstruct &x) {
1423   PushScope(Scope::Kind::OpenACCConstruct, nullptr);
1424   return true;
1425 }
1426 
1427 void AccVisitor::Post(const parser::OpenACCCombinedConstruct &x) { PopScope(); }
1428 
1429 // Create scopes for OpenMP constructs
1430 class OmpVisitor : public virtual DeclarationVisitor {
1431 public:
1432   void AddOmpSourceRange(const parser::CharBlock &);
1433 
1434   static bool NeedsScope(const parser::OpenMPBlockConstruct &);
1435   static bool NeedsScope(const parser::OmpClause &);
1436 
1437   bool Pre(const parser::OpenMPRequiresConstruct &x) {
1438     AddOmpSourceRange(x.source);
1439     return true;
1440   }
1441   bool Pre(const parser::OmpSimpleStandaloneDirective &x) {
1442     AddOmpSourceRange(x.source);
1443     return true;
1444   }
1445   bool Pre(const parser::OpenMPBlockConstruct &);
1446   void Post(const parser::OpenMPBlockConstruct &);
1447   bool Pre(const parser::OmpBeginBlockDirective &x) {
1448     AddOmpSourceRange(x.source);
1449     return true;
1450   }
1451   void Post(const parser::OmpBeginBlockDirective &) {
1452     messageHandler().set_currStmtSource(std::nullopt);
1453   }
1454   bool Pre(const parser::OmpEndBlockDirective &x) {
1455     AddOmpSourceRange(x.source);
1456     return true;
1457   }
1458   void Post(const parser::OmpEndBlockDirective &) {
1459     messageHandler().set_currStmtSource(std::nullopt);
1460   }
1461 
1462   bool Pre(const parser::OpenMPLoopConstruct &) {
1463     PushScope(Scope::Kind::OtherConstruct, nullptr);
1464     return true;
1465   }
1466   void Post(const parser::OpenMPLoopConstruct &) { PopScope(); }
1467   bool Pre(const parser::OmpBeginLoopDirective &x) {
1468     AddOmpSourceRange(x.source);
1469     return true;
1470   }
1471 
1472   bool Pre(const parser::OpenMPDeclareMapperConstruct &);
1473 
1474   void Post(const parser::OmpBeginLoopDirective &) {
1475     messageHandler().set_currStmtSource(std::nullopt);
1476   }
1477   bool Pre(const parser::OmpEndLoopDirective &x) {
1478     AddOmpSourceRange(x.source);
1479     return true;
1480   }
1481   void Post(const parser::OmpEndLoopDirective &) {
1482     messageHandler().set_currStmtSource(std::nullopt);
1483   }
1484 
1485   bool Pre(const parser::OpenMPSectionsConstruct &) {
1486     PushScope(Scope::Kind::OtherConstruct, nullptr);
1487     return true;
1488   }
1489   void Post(const parser::OpenMPSectionsConstruct &) { PopScope(); }
1490   bool Pre(const parser::OmpBeginSectionsDirective &x) {
1491     AddOmpSourceRange(x.source);
1492     return true;
1493   }
1494   void Post(const parser::OmpBeginSectionsDirective &) {
1495     messageHandler().set_currStmtSource(std::nullopt);
1496   }
1497   bool Pre(const parser::OmpEndSectionsDirective &x) {
1498     AddOmpSourceRange(x.source);
1499     return true;
1500   }
1501   void Post(const parser::OmpEndSectionsDirective &) {
1502     messageHandler().set_currStmtSource(std::nullopt);
1503   }
1504   bool Pre(const parser::OmpCriticalDirective &x) {
1505     AddOmpSourceRange(x.source);
1506     return true;
1507   }
1508   void Post(const parser::OmpCriticalDirective &) {
1509     messageHandler().set_currStmtSource(std::nullopt);
1510   }
1511   bool Pre(const parser::OmpEndCriticalDirective &x) {
1512     AddOmpSourceRange(x.source);
1513     return true;
1514   }
1515   void Post(const parser::OmpEndCriticalDirective &) {
1516     messageHandler().set_currStmtSource(std::nullopt);
1517   }
1518   bool Pre(const parser::OpenMPThreadprivate &) {
1519     SkipImplicitTyping(true);
1520     return true;
1521   }
1522   void Post(const parser::OpenMPThreadprivate &) { SkipImplicitTyping(false); }
1523   bool Pre(const parser::OpenMPDeclareTargetConstruct &) {
1524     SkipImplicitTyping(true);
1525     return true;
1526   }
1527   void Post(const parser::OpenMPDeclareTargetConstruct &) {
1528     SkipImplicitTyping(false);
1529   }
1530   bool Pre(const parser::OpenMPDeclarativeAllocate &) {
1531     SkipImplicitTyping(true);
1532     return true;
1533   }
1534   void Post(const parser::OpenMPDeclarativeAllocate &) {
1535     SkipImplicitTyping(false);
1536   }
1537   bool Pre(const parser::OpenMPDeclarativeConstruct &x) {
1538     AddOmpSourceRange(x.source);
1539     return true;
1540   }
1541   void Post(const parser::OpenMPDeclarativeConstruct &) {
1542     messageHandler().set_currStmtSource(std::nullopt);
1543   }
1544   bool Pre(const parser::OpenMPDepobjConstruct &x) {
1545     AddOmpSourceRange(x.source);
1546     return true;
1547   }
1548   void Post(const parser::OpenMPDepobjConstruct &x) {
1549     messageHandler().set_currStmtSource(std::nullopt);
1550   }
1551   bool Pre(const parser::OpenMPAtomicConstruct &x) {
1552     return common::visit(common::visitors{[&](const auto &u) -> bool {
1553       AddOmpSourceRange(u.source);
1554       return true;
1555     }},
1556         x.u);
1557   }
1558   void Post(const parser::OpenMPAtomicConstruct &) {
1559     messageHandler().set_currStmtSource(std::nullopt);
1560   }
1561   bool Pre(const parser::OmpClause &x) {
1562     if (NeedsScope(x)) {
1563       PushScope(Scope::Kind::OtherClause, nullptr);
1564     }
1565     return true;
1566   }
1567   void Post(const parser::OmpClause &x) {
1568     if (NeedsScope(x)) {
1569       PopScope();
1570     }
1571   }
1572 };
1573 
1574 bool OmpVisitor::NeedsScope(const parser::OpenMPBlockConstruct &x) {
1575   const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
1576   const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
1577   switch (beginDir.v) {
1578   case llvm::omp::Directive::OMPD_master:
1579   case llvm::omp::Directive::OMPD_ordered:
1580   case llvm::omp::Directive::OMPD_taskgroup:
1581     return false;
1582   default:
1583     return true;
1584   }
1585 }
1586 
1587 bool OmpVisitor::NeedsScope(const parser::OmpClause &x) {
1588   // Iterators contain declarations, whose scope extends until the end
1589   // the clause.
1590   return llvm::omp::canHaveIterator(x.Id());
1591 }
1592 
1593 void OmpVisitor::AddOmpSourceRange(const parser::CharBlock &source) {
1594   messageHandler().set_currStmtSource(source);
1595   currScope().AddSourceRange(source);
1596 }
1597 
1598 bool OmpVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
1599   if (NeedsScope(x)) {
1600     PushScope(Scope::Kind::OtherConstruct, nullptr);
1601   }
1602   return true;
1603 }
1604 
1605 void OmpVisitor::Post(const parser::OpenMPBlockConstruct &x) {
1606   if (NeedsScope(x)) {
1607     PopScope();
1608   }
1609 }
1610 
1611 // This "manually" walks the tree of the construct, because we need
1612 // to resolve the type before the map clauses are processed - when
1613 // just following the natural flow, the map clauses gets processed before
1614 // the type has been fully processed.
1615 bool OmpVisitor::Pre(const parser::OpenMPDeclareMapperConstruct &x) {
1616   AddOmpSourceRange(x.source);
1617   BeginDeclTypeSpec();
1618   const auto &spec{std::get<parser::OmpDeclareMapperSpecifier>(x.t)};
1619   Symbol *mapperSym{nullptr};
1620   if (const auto &mapperName{std::get<std::optional<parser::Name>>(spec.t)}) {
1621     mapperSym =
1622         &MakeSymbol(*mapperName, MiscDetails{MiscDetails::Kind::ConstructName});
1623     mapperName->symbol = mapperSym;
1624   } else {
1625     const parser::CharBlock defaultName{"default", 7};
1626     mapperSym = &MakeSymbol(
1627         defaultName, Attrs{}, MiscDetails{MiscDetails::Kind::ConstructName});
1628   }
1629 
1630   PushScope(Scope::Kind::OtherConstruct, nullptr);
1631   Walk(std::get<parser::TypeSpec>(spec.t));
1632   const auto &varName{std::get<parser::ObjectName>(spec.t)};
1633   DeclareObjectEntity(varName);
1634 
1635   Walk(std::get<parser::OmpClauseList>(x.t));
1636 
1637   EndDeclTypeSpec();
1638   PopScope();
1639   return false;
1640 }
1641 
1642 // Walk the parse tree and resolve names to symbols.
1643 class ResolveNamesVisitor : public virtual ScopeHandler,
1644                             public ModuleVisitor,
1645                             public SubprogramVisitor,
1646                             public ConstructVisitor,
1647                             public OmpVisitor,
1648                             public AccVisitor {
1649 public:
1650   using AccVisitor::Post;
1651   using AccVisitor::Pre;
1652   using ArraySpecVisitor::Post;
1653   using ConstructVisitor::Post;
1654   using ConstructVisitor::Pre;
1655   using DeclarationVisitor::Post;
1656   using DeclarationVisitor::Pre;
1657   using ImplicitRulesVisitor::Post;
1658   using ImplicitRulesVisitor::Pre;
1659   using InterfaceVisitor::Post;
1660   using InterfaceVisitor::Pre;
1661   using ModuleVisitor::Post;
1662   using ModuleVisitor::Pre;
1663   using OmpVisitor::Post;
1664   using OmpVisitor::Pre;
1665   using ScopeHandler::Post;
1666   using ScopeHandler::Pre;
1667   using SubprogramVisitor::Post;
1668   using SubprogramVisitor::Pre;
1669 
1670   ResolveNamesVisitor(
1671       SemanticsContext &context, ImplicitRulesMap &rules, Scope &top)
1672       : BaseVisitor{context, *this, rules}, topScope_{top} {
1673     PushScope(top);
1674   }
1675 
1676   Scope &topScope() const { return topScope_; }
1677 
1678   // Default action for a parse tree node is to visit children.
1679   template <typename T> bool Pre(const T &) { return true; }
1680   template <typename T> void Post(const T &) {}
1681 
1682   bool Pre(const parser::SpecificationPart &);
1683   bool Pre(const parser::Program &);
1684   void Post(const parser::Program &);
1685   bool Pre(const parser::ImplicitStmt &);
1686   void Post(const parser::PointerObject &);
1687   void Post(const parser::AllocateObject &);
1688   bool Pre(const parser::PointerAssignmentStmt &);
1689   void Post(const parser::Designator &);
1690   void Post(const parser::SubstringInquiry &);
1691   template <typename A, typename B>
1692   void Post(const parser::LoopBounds<A, B> &x) {
1693     ResolveName(*parser::Unwrap<parser::Name>(x.name));
1694   }
1695   void Post(const parser::ProcComponentRef &);
1696   bool Pre(const parser::FunctionReference &);
1697   bool Pre(const parser::CallStmt &);
1698   bool Pre(const parser::ImportStmt &);
1699   void Post(const parser::TypeGuardStmt &);
1700   bool Pre(const parser::StmtFunctionStmt &);
1701   bool Pre(const parser::DefinedOpName &);
1702   bool Pre(const parser::ProgramUnit &);
1703   void Post(const parser::AssignStmt &);
1704   void Post(const parser::AssignedGotoStmt &);
1705   void Post(const parser::CompilerDirective &);
1706 
1707   // These nodes should never be reached: they are handled in ProgramUnit
1708   bool Pre(const parser::MainProgram &) {
1709     llvm_unreachable("This node is handled in ProgramUnit");
1710   }
1711   bool Pre(const parser::FunctionSubprogram &) {
1712     llvm_unreachable("This node is handled in ProgramUnit");
1713   }
1714   bool Pre(const parser::SubroutineSubprogram &) {
1715     llvm_unreachable("This node is handled in ProgramUnit");
1716   }
1717   bool Pre(const parser::SeparateModuleSubprogram &) {
1718     llvm_unreachable("This node is handled in ProgramUnit");
1719   }
1720   bool Pre(const parser::Module &) {
1721     llvm_unreachable("This node is handled in ProgramUnit");
1722   }
1723   bool Pre(const parser::Submodule &) {
1724     llvm_unreachable("This node is handled in ProgramUnit");
1725   }
1726   bool Pre(const parser::BlockData &) {
1727     llvm_unreachable("This node is handled in ProgramUnit");
1728   }
1729 
1730   void NoteExecutablePartCall(Symbol::Flag, SourceName, bool hasCUDAChevrons);
1731 
1732   friend void ResolveSpecificationParts(SemanticsContext &, const Symbol &);
1733 
1734 private:
1735   // Kind of procedure we are expecting to see in a ProcedureDesignator
1736   std::optional<Symbol::Flag> expectedProcFlag_;
1737   std::optional<SourceName> prevImportStmt_;
1738   Scope &topScope_;
1739 
1740   void PreSpecificationConstruct(const parser::SpecificationConstruct &);
1741   void CreateCommonBlockSymbols(const parser::CommonStmt &);
1742   void CreateObjectSymbols(const std::list<parser::ObjectDecl> &, Attr);
1743   void CreateGeneric(const parser::GenericSpec &);
1744   void FinishSpecificationPart(const std::list<parser::DeclarationConstruct> &);
1745   void AnalyzeStmtFunctionStmt(const parser::StmtFunctionStmt &);
1746   void CheckImports();
1747   void CheckImport(const SourceName &, const SourceName &);
1748   void HandleCall(Symbol::Flag, const parser::Call &);
1749   void HandleProcedureName(Symbol::Flag, const parser::Name &);
1750   bool CheckImplicitNoneExternal(const SourceName &, const Symbol &);
1751   bool SetProcFlag(const parser::Name &, Symbol &, Symbol::Flag);
1752   void ResolveSpecificationParts(ProgramTree &);
1753   void AddSubpNames(ProgramTree &);
1754   bool BeginScopeForNode(const ProgramTree &);
1755   void EndScopeForNode(const ProgramTree &);
1756   void FinishSpecificationParts(const ProgramTree &);
1757   void FinishExecutionParts(const ProgramTree &);
1758   void FinishDerivedTypeInstantiation(Scope &);
1759   void ResolveExecutionParts(const ProgramTree &);
1760   void UseCUDABuiltinNames();
1761   void HandleDerivedTypesInImplicitStmts(const parser::ImplicitPart &,
1762       const std::list<parser::DeclarationConstruct> &);
1763 };
1764 
1765 // ImplicitRules implementation
1766 
1767 bool ImplicitRules::isImplicitNoneType() const {
1768   if (isImplicitNoneType_) {
1769     return true;
1770   } else if (map_.empty() && inheritFromParent_) {
1771     return parent_->isImplicitNoneType();
1772   } else {
1773     return false; // default if not specified
1774   }
1775 }
1776 
1777 bool ImplicitRules::isImplicitNoneExternal() const {
1778   if (isImplicitNoneExternal_) {
1779     return true;
1780   } else if (inheritFromParent_) {
1781     return parent_->isImplicitNoneExternal();
1782   } else {
1783     return false; // default if not specified
1784   }
1785 }
1786 
1787 const DeclTypeSpec *ImplicitRules::GetType(
1788     SourceName name, bool respectImplicitNoneType) const {
1789   char ch{name.begin()[0]};
1790   if (isImplicitNoneType_ && respectImplicitNoneType) {
1791     return nullptr;
1792   } else if (auto it{map_.find(ch)}; it != map_.end()) {
1793     return &*it->second;
1794   } else if (inheritFromParent_) {
1795     return parent_->GetType(name, respectImplicitNoneType);
1796   } else if (ch >= 'i' && ch <= 'n') {
1797     return &context_.MakeNumericType(TypeCategory::Integer);
1798   } else if (ch >= 'a' && ch <= 'z') {
1799     return &context_.MakeNumericType(TypeCategory::Real);
1800   } else {
1801     return nullptr;
1802   }
1803 }
1804 
1805 void ImplicitRules::SetTypeMapping(const DeclTypeSpec &type,
1806     parser::Location fromLetter, parser::Location toLetter) {
1807   for (char ch = *fromLetter; ch; ch = ImplicitRules::Incr(ch)) {
1808     auto res{map_.emplace(ch, type)};
1809     if (!res.second) {
1810       context_.Say(parser::CharBlock{fromLetter},
1811           "More than one implicit type specified for '%c'"_err_en_US, ch);
1812     }
1813     if (ch == *toLetter) {
1814       break;
1815     }
1816   }
1817 }
1818 
1819 // Return the next char after ch in a way that works for ASCII or EBCDIC.
1820 // Return '\0' for the char after 'z'.
1821 char ImplicitRules::Incr(char ch) {
1822   switch (ch) {
1823   case 'i':
1824     return 'j';
1825   case 'r':
1826     return 's';
1827   case 'z':
1828     return '\0';
1829   default:
1830     return ch + 1;
1831   }
1832 }
1833 
1834 llvm::raw_ostream &operator<<(
1835     llvm::raw_ostream &o, const ImplicitRules &implicitRules) {
1836   o << "ImplicitRules:\n";
1837   for (char ch = 'a'; ch; ch = ImplicitRules::Incr(ch)) {
1838     ShowImplicitRule(o, implicitRules, ch);
1839   }
1840   ShowImplicitRule(o, implicitRules, '_');
1841   ShowImplicitRule(o, implicitRules, '$');
1842   ShowImplicitRule(o, implicitRules, '@');
1843   return o;
1844 }
1845 void ShowImplicitRule(
1846     llvm::raw_ostream &o, const ImplicitRules &implicitRules, char ch) {
1847   auto it{implicitRules.map_.find(ch)};
1848   if (it != implicitRules.map_.end()) {
1849     o << "  " << ch << ": " << *it->second << '\n';
1850   }
1851 }
1852 
1853 template <typename T> void BaseVisitor::Walk(const T &x) {
1854   parser::Walk(x, *this_);
1855 }
1856 
1857 void BaseVisitor::MakePlaceholder(
1858     const parser::Name &name, MiscDetails::Kind kind) {
1859   if (!name.symbol) {
1860     name.symbol = &context_->globalScope().MakeSymbol(
1861         name.source, Attrs{}, MiscDetails{kind});
1862   }
1863 }
1864 
1865 // AttrsVisitor implementation
1866 
1867 bool AttrsVisitor::BeginAttrs() {
1868   CHECK(!attrs_ && !cudaDataAttr_);
1869   attrs_ = Attrs{};
1870   return true;
1871 }
1872 Attrs AttrsVisitor::GetAttrs() {
1873   CHECK(attrs_);
1874   return *attrs_;
1875 }
1876 Attrs AttrsVisitor::EndAttrs() {
1877   Attrs result{GetAttrs()};
1878   attrs_.reset();
1879   cudaDataAttr_.reset();
1880   passName_ = std::nullopt;
1881   bindName_.reset();
1882   isCDefined_ = false;
1883   return result;
1884 }
1885 
1886 bool AttrsVisitor::SetPassNameOn(Symbol &symbol) {
1887   if (!passName_) {
1888     return false;
1889   }
1890   common::visit(common::visitors{
1891                     [&](ProcEntityDetails &x) { x.set_passName(*passName_); },
1892                     [&](ProcBindingDetails &x) { x.set_passName(*passName_); },
1893                     [](auto &) { common::die("unexpected pass name"); },
1894                 },
1895       symbol.details());
1896   return true;
1897 }
1898 
1899 void AttrsVisitor::SetBindNameOn(Symbol &symbol) {
1900   if ((!attrs_ || !attrs_->test(Attr::BIND_C)) &&
1901       !symbol.attrs().test(Attr::BIND_C)) {
1902     return;
1903   }
1904   symbol.SetIsCDefined(isCDefined_);
1905   std::optional<std::string> label{
1906       evaluate::GetScalarConstantValue<evaluate::Ascii>(bindName_)};
1907   // 18.9.2(2): discard leading and trailing blanks
1908   if (label) {
1909     symbol.SetIsExplicitBindName(true);
1910     auto first{label->find_first_not_of(" ")};
1911     if (first == std::string::npos) {
1912       // Empty NAME= means no binding at all (18.10.2p2)
1913       return;
1914     }
1915     auto last{label->find_last_not_of(" ")};
1916     label = label->substr(first, last - first + 1);
1917   } else if (symbol.GetIsExplicitBindName()) {
1918     // don't try to override explicit binding name with default
1919     return;
1920   } else if (ClassifyProcedure(symbol) == ProcedureDefinitionClass::Internal) {
1921     // BIND(C) does not give an implicit binding label to internal procedures.
1922     return;
1923   } else {
1924     label = symbol.name().ToString();
1925   }
1926   // Checks whether a symbol has two Bind names.
1927   std::string oldBindName;
1928   if (const auto *bindName{symbol.GetBindName()}) {
1929     oldBindName = *bindName;
1930   }
1931   symbol.SetBindName(std::move(*label));
1932   if (!oldBindName.empty()) {
1933     if (const std::string * newBindName{symbol.GetBindName()}) {
1934       if (oldBindName != *newBindName) {
1935         Say(symbol.name(),
1936             "The entity '%s' has multiple BIND names ('%s' and '%s')"_err_en_US,
1937             symbol.name(), oldBindName, *newBindName);
1938       }
1939     }
1940   }
1941 }
1942 
1943 void AttrsVisitor::Post(const parser::LanguageBindingSpec &x) {
1944   if (CheckAndSet(Attr::BIND_C)) {
1945     if (const auto &name{
1946             std::get<std::optional<parser::ScalarDefaultCharConstantExpr>>(
1947                 x.t)}) {
1948       bindName_ = EvaluateExpr(*name);
1949     }
1950     isCDefined_ = std::get<bool>(x.t);
1951   }
1952 }
1953 bool AttrsVisitor::Pre(const parser::IntentSpec &x) {
1954   CheckAndSet(IntentSpecToAttr(x));
1955   return false;
1956 }
1957 bool AttrsVisitor::Pre(const parser::Pass &x) {
1958   if (CheckAndSet(Attr::PASS)) {
1959     if (x.v) {
1960       passName_ = x.v->source;
1961       MakePlaceholder(*x.v, MiscDetails::Kind::PassName);
1962     }
1963   }
1964   return false;
1965 }
1966 
1967 // C730, C743, C755, C778, C1543 say no attribute or prefix repetitions
1968 bool AttrsVisitor::IsDuplicateAttr(Attr attrName) {
1969   CHECK(attrs_);
1970   if (attrs_->test(attrName)) {
1971     context().Warn(common::LanguageFeature::RedundantAttribute,
1972         currStmtSource().value(),
1973         "Attribute '%s' cannot be used more than once"_warn_en_US,
1974         AttrToString(attrName));
1975     return true;
1976   }
1977   return false;
1978 }
1979 
1980 // See if attrName violates a constraint cause by a conflict.  attr1 and attr2
1981 // name attributes that cannot be used on the same declaration
1982 bool AttrsVisitor::HaveAttrConflict(Attr attrName, Attr attr1, Attr attr2) {
1983   CHECK(attrs_);
1984   if ((attrName == attr1 && attrs_->test(attr2)) ||
1985       (attrName == attr2 && attrs_->test(attr1))) {
1986     Say(currStmtSource().value(),
1987         "Attributes '%s' and '%s' conflict with each other"_err_en_US,
1988         AttrToString(attr1), AttrToString(attr2));
1989     return true;
1990   }
1991   return false;
1992 }
1993 // C759, C1543
1994 bool AttrsVisitor::IsConflictingAttr(Attr attrName) {
1995   return HaveAttrConflict(attrName, Attr::INTENT_IN, Attr::INTENT_INOUT) ||
1996       HaveAttrConflict(attrName, Attr::INTENT_IN, Attr::INTENT_OUT) ||
1997       HaveAttrConflict(attrName, Attr::INTENT_INOUT, Attr::INTENT_OUT) ||
1998       HaveAttrConflict(attrName, Attr::PASS, Attr::NOPASS) || // C781
1999       HaveAttrConflict(attrName, Attr::PURE, Attr::IMPURE) ||
2000       HaveAttrConflict(attrName, Attr::PUBLIC, Attr::PRIVATE) ||
2001       HaveAttrConflict(attrName, Attr::RECURSIVE, Attr::NON_RECURSIVE);
2002 }
2003 bool AttrsVisitor::CheckAndSet(Attr attrName) {
2004   if (IsConflictingAttr(attrName) || IsDuplicateAttr(attrName)) {
2005     return false;
2006   }
2007   attrs_->set(attrName);
2008   return true;
2009 }
2010 bool AttrsVisitor::Pre(const common::CUDADataAttr x) {
2011   if (cudaDataAttr_.value_or(x) != x) {
2012     Say(currStmtSource().value(),
2013         "CUDA data attributes '%s' and '%s' may not both be specified"_err_en_US,
2014         common::EnumToString(*cudaDataAttr_), common::EnumToString(x));
2015   }
2016   cudaDataAttr_ = x;
2017   return false;
2018 }
2019 
2020 // DeclTypeSpecVisitor implementation
2021 
2022 const DeclTypeSpec *DeclTypeSpecVisitor::GetDeclTypeSpec() {
2023   return state_.declTypeSpec;
2024 }
2025 
2026 void DeclTypeSpecVisitor::BeginDeclTypeSpec() {
2027   CHECK(!state_.expectDeclTypeSpec);
2028   CHECK(!state_.declTypeSpec);
2029   state_.expectDeclTypeSpec = true;
2030 }
2031 void DeclTypeSpecVisitor::EndDeclTypeSpec() {
2032   CHECK(state_.expectDeclTypeSpec);
2033   state_ = {};
2034 }
2035 
2036 void DeclTypeSpecVisitor::SetDeclTypeSpecCategory(
2037     DeclTypeSpec::Category category) {
2038   CHECK(state_.expectDeclTypeSpec);
2039   state_.derived.category = category;
2040 }
2041 
2042 bool DeclTypeSpecVisitor::Pre(const parser::TypeGuardStmt &) {
2043   BeginDeclTypeSpec();
2044   return true;
2045 }
2046 void DeclTypeSpecVisitor::Post(const parser::TypeGuardStmt &) {
2047   EndDeclTypeSpec();
2048 }
2049 
2050 void DeclTypeSpecVisitor::Post(const parser::TypeSpec &typeSpec) {
2051   // Record the resolved DeclTypeSpec in the parse tree for use by
2052   // expression semantics if the DeclTypeSpec is a valid TypeSpec.
2053   // The grammar ensures that it's an intrinsic or derived type spec,
2054   // not TYPE(*) or CLASS(*) or CLASS(T).
2055   if (const DeclTypeSpec * spec{state_.declTypeSpec}) {
2056     switch (spec->category()) {
2057     case DeclTypeSpec::Numeric:
2058     case DeclTypeSpec::Logical:
2059     case DeclTypeSpec::Character:
2060       typeSpec.declTypeSpec = spec;
2061       break;
2062     case DeclTypeSpec::TypeDerived:
2063       if (const DerivedTypeSpec * derived{spec->AsDerived()}) {
2064         CheckForAbstractType(derived->typeSymbol()); // C703
2065         typeSpec.declTypeSpec = spec;
2066       }
2067       break;
2068     default:
2069       CRASH_NO_CASE;
2070     }
2071   }
2072 }
2073 
2074 void DeclTypeSpecVisitor::Post(
2075     const parser::IntrinsicTypeSpec::DoublePrecision &) {
2076   MakeNumericType(TypeCategory::Real, context().doublePrecisionKind());
2077 }
2078 void DeclTypeSpecVisitor::Post(
2079     const parser::IntrinsicTypeSpec::DoubleComplex &) {
2080   MakeNumericType(TypeCategory::Complex, context().doublePrecisionKind());
2081 }
2082 void DeclTypeSpecVisitor::MakeNumericType(TypeCategory category, int kind) {
2083   SetDeclTypeSpec(context().MakeNumericType(category, kind));
2084 }
2085 
2086 void DeclTypeSpecVisitor::CheckForAbstractType(const Symbol &typeSymbol) {
2087   if (typeSymbol.attrs().test(Attr::ABSTRACT)) {
2088     Say("ABSTRACT derived type may not be used here"_err_en_US);
2089   }
2090 }
2091 
2092 void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::ClassStar &) {
2093   SetDeclTypeSpec(context().globalScope().MakeClassStarType());
2094 }
2095 void DeclTypeSpecVisitor::Post(const parser::DeclarationTypeSpec::TypeStar &) {
2096   SetDeclTypeSpec(context().globalScope().MakeTypeStarType());
2097 }
2098 
2099 // Check that we're expecting to see a DeclTypeSpec (and haven't seen one yet)
2100 // and save it in state_.declTypeSpec.
2101 void DeclTypeSpecVisitor::SetDeclTypeSpec(const DeclTypeSpec &declTypeSpec) {
2102   CHECK(state_.expectDeclTypeSpec);
2103   CHECK(!state_.declTypeSpec);
2104   state_.declTypeSpec = &declTypeSpec;
2105 }
2106 
2107 KindExpr DeclTypeSpecVisitor::GetKindParamExpr(
2108     TypeCategory category, const std::optional<parser::KindSelector> &kind) {
2109   return AnalyzeKindSelector(context(), category, kind);
2110 }
2111 
2112 // MessageHandler implementation
2113 
2114 Message &MessageHandler::Say(MessageFixedText &&msg) {
2115   return context_->Say(currStmtSource().value(), std::move(msg));
2116 }
2117 Message &MessageHandler::Say(MessageFormattedText &&msg) {
2118   return context_->Say(currStmtSource().value(), std::move(msg));
2119 }
2120 Message &MessageHandler::Say(const SourceName &name, MessageFixedText &&msg) {
2121   return Say(name, std::move(msg), name);
2122 }
2123 
2124 // ImplicitRulesVisitor implementation
2125 
2126 void ImplicitRulesVisitor::Post(const parser::ParameterStmt &) {
2127   prevParameterStmt_ = currStmtSource();
2128 }
2129 
2130 bool ImplicitRulesVisitor::Pre(const parser::ImplicitStmt &x) {
2131   bool result{
2132       common::visit(common::visitors{
2133                         [&](const std::list<ImplicitNoneNameSpec> &y) {
2134                           return HandleImplicitNone(y);
2135                         },
2136                         [&](const std::list<parser::ImplicitSpec> &) {
2137                           if (prevImplicitNoneType_) {
2138                             Say("IMPLICIT statement after IMPLICIT NONE or "
2139                                 "IMPLICIT NONE(TYPE) statement"_err_en_US);
2140                             return false;
2141                           }
2142                           implicitRules_->set_isImplicitNoneType(false);
2143                           return true;
2144                         },
2145                     },
2146           x.u)};
2147   prevImplicit_ = currStmtSource();
2148   return result;
2149 }
2150 
2151 bool ImplicitRulesVisitor::Pre(const parser::LetterSpec &x) {
2152   auto loLoc{std::get<parser::Location>(x.t)};
2153   auto hiLoc{loLoc};
2154   if (auto hiLocOpt{std::get<std::optional<parser::Location>>(x.t)}) {
2155     hiLoc = *hiLocOpt;
2156     if (*hiLoc < *loLoc) {
2157       Say(hiLoc, "'%s' does not follow '%s' alphabetically"_err_en_US,
2158           std::string(hiLoc, 1), std::string(loLoc, 1));
2159       return false;
2160     }
2161   }
2162   implicitRules_->SetTypeMapping(*GetDeclTypeSpec(), loLoc, hiLoc);
2163   return false;
2164 }
2165 
2166 bool ImplicitRulesVisitor::Pre(const parser::ImplicitSpec &) {
2167   BeginDeclTypeSpec();
2168   set_allowForwardReferenceToDerivedType(true);
2169   return true;
2170 }
2171 
2172 void ImplicitRulesVisitor::Post(const parser::ImplicitSpec &) {
2173   set_allowForwardReferenceToDerivedType(false);
2174   EndDeclTypeSpec();
2175 }
2176 
2177 void ImplicitRulesVisitor::SetScope(const Scope &scope) {
2178   implicitRules_ = &DEREF(implicitRulesMap_).at(&scope);
2179   prevImplicit_ = std::nullopt;
2180   prevImplicitNone_ = std::nullopt;
2181   prevImplicitNoneType_ = std::nullopt;
2182   prevParameterStmt_ = std::nullopt;
2183 }
2184 void ImplicitRulesVisitor::BeginScope(const Scope &scope) {
2185   // find or create implicit rules for this scope
2186   DEREF(implicitRulesMap_).try_emplace(&scope, context(), implicitRules_);
2187   SetScope(scope);
2188 }
2189 
2190 // TODO: for all of these errors, reference previous statement too
2191 bool ImplicitRulesVisitor::HandleImplicitNone(
2192     const std::list<ImplicitNoneNameSpec> &nameSpecs) {
2193   if (prevImplicitNone_) {
2194     Say("More than one IMPLICIT NONE statement"_err_en_US);
2195     Say(*prevImplicitNone_, "Previous IMPLICIT NONE statement"_en_US);
2196     return false;
2197   }
2198   if (prevParameterStmt_) {
2199     Say("IMPLICIT NONE statement after PARAMETER statement"_err_en_US);
2200     return false;
2201   }
2202   prevImplicitNone_ = currStmtSource();
2203   bool implicitNoneTypeNever{
2204       context().IsEnabled(common::LanguageFeature::ImplicitNoneTypeNever)};
2205   if (nameSpecs.empty()) {
2206     if (!implicitNoneTypeNever) {
2207       prevImplicitNoneType_ = currStmtSource();
2208       implicitRules_->set_isImplicitNoneType(true);
2209       if (prevImplicit_) {
2210         Say("IMPLICIT NONE statement after IMPLICIT statement"_err_en_US);
2211         return false;
2212       }
2213     }
2214   } else {
2215     int sawType{0};
2216     int sawExternal{0};
2217     for (const auto noneSpec : nameSpecs) {
2218       switch (noneSpec) {
2219       case ImplicitNoneNameSpec::External:
2220         implicitRules_->set_isImplicitNoneExternal(true);
2221         ++sawExternal;
2222         break;
2223       case ImplicitNoneNameSpec::Type:
2224         if (!implicitNoneTypeNever) {
2225           prevImplicitNoneType_ = currStmtSource();
2226           implicitRules_->set_isImplicitNoneType(true);
2227           if (prevImplicit_) {
2228             Say("IMPLICIT NONE(TYPE) after IMPLICIT statement"_err_en_US);
2229             return false;
2230           }
2231           ++sawType;
2232         }
2233         break;
2234       }
2235     }
2236     if (sawType > 1) {
2237       Say("TYPE specified more than once in IMPLICIT NONE statement"_err_en_US);
2238       return false;
2239     }
2240     if (sawExternal > 1) {
2241       Say("EXTERNAL specified more than once in IMPLICIT NONE statement"_err_en_US);
2242       return false;
2243     }
2244   }
2245   return true;
2246 }
2247 
2248 // ArraySpecVisitor implementation
2249 
2250 void ArraySpecVisitor::Post(const parser::ArraySpec &x) {
2251   CHECK(arraySpec_.empty());
2252   arraySpec_ = AnalyzeArraySpec(context(), x);
2253 }
2254 void ArraySpecVisitor::Post(const parser::ComponentArraySpec &x) {
2255   CHECK(arraySpec_.empty());
2256   arraySpec_ = AnalyzeArraySpec(context(), x);
2257 }
2258 void ArraySpecVisitor::Post(const parser::CoarraySpec &x) {
2259   CHECK(coarraySpec_.empty());
2260   coarraySpec_ = AnalyzeCoarraySpec(context(), x);
2261 }
2262 
2263 const ArraySpec &ArraySpecVisitor::arraySpec() {
2264   return !arraySpec_.empty() ? arraySpec_ : attrArraySpec_;
2265 }
2266 const ArraySpec &ArraySpecVisitor::coarraySpec() {
2267   return !coarraySpec_.empty() ? coarraySpec_ : attrCoarraySpec_;
2268 }
2269 void ArraySpecVisitor::BeginArraySpec() {
2270   CHECK(arraySpec_.empty());
2271   CHECK(coarraySpec_.empty());
2272   CHECK(attrArraySpec_.empty());
2273   CHECK(attrCoarraySpec_.empty());
2274 }
2275 void ArraySpecVisitor::EndArraySpec() {
2276   CHECK(arraySpec_.empty());
2277   CHECK(coarraySpec_.empty());
2278   attrArraySpec_.clear();
2279   attrCoarraySpec_.clear();
2280 }
2281 void ArraySpecVisitor::PostAttrSpec() {
2282   // Save dimension/codimension from attrs so we can process array/coarray-spec
2283   // on the entity-decl
2284   if (!arraySpec_.empty()) {
2285     if (attrArraySpec_.empty()) {
2286       attrArraySpec_ = arraySpec_;
2287       arraySpec_.clear();
2288     } else {
2289       Say(currStmtSource().value(),
2290           "Attribute 'DIMENSION' cannot be used more than once"_err_en_US);
2291     }
2292   }
2293   if (!coarraySpec_.empty()) {
2294     if (attrCoarraySpec_.empty()) {
2295       attrCoarraySpec_ = coarraySpec_;
2296       coarraySpec_.clear();
2297     } else {
2298       Say(currStmtSource().value(),
2299           "Attribute 'CODIMENSION' cannot be used more than once"_err_en_US);
2300     }
2301   }
2302 }
2303 
2304 // FuncResultStack implementation
2305 
2306 FuncResultStack::~FuncResultStack() { CHECK(stack_.empty()); }
2307 
2308 void FuncResultStack::CompleteFunctionResultType() {
2309   // If the function has a type in the prefix, process it now.
2310   FuncInfo *info{Top()};
2311   if (info && &info->scope == &scopeHandler_.currScope()) {
2312     if (info->parsedType && info->resultSymbol) {
2313       scopeHandler_.messageHandler().set_currStmtSource(info->source);
2314       if (const auto *type{
2315               scopeHandler_.ProcessTypeSpec(*info->parsedType, true)}) {
2316         Symbol &symbol{*info->resultSymbol};
2317         if (!scopeHandler_.context().HasError(symbol)) {
2318           if (symbol.GetType()) {
2319             scopeHandler_.Say(symbol.name(),
2320                 "Function cannot have both an explicit type prefix and a RESULT suffix"_err_en_US);
2321             scopeHandler_.context().SetError(symbol);
2322           } else {
2323             symbol.SetType(*type);
2324           }
2325         }
2326       }
2327       info->parsedType = nullptr;
2328     }
2329   }
2330 }
2331 
2332 // Called from ConvertTo{Object/Proc}Entity to cope with any appearance
2333 // of the function result in a specification expression.
2334 void FuncResultStack::CompleteTypeIfFunctionResult(Symbol &symbol) {
2335   if (FuncInfo * info{Top()}) {
2336     if (info->resultSymbol == &symbol) {
2337       CompleteFunctionResultType();
2338     }
2339   }
2340 }
2341 
2342 void FuncResultStack::Pop() {
2343   if (!stack_.empty() && &stack_.back().scope == &scopeHandler_.currScope()) {
2344     stack_.pop_back();
2345   }
2346 }
2347 
2348 // ScopeHandler implementation
2349 
2350 void ScopeHandler::SayAlreadyDeclared(const parser::Name &name, Symbol &prev) {
2351   SayAlreadyDeclared(name.source, prev);
2352 }
2353 void ScopeHandler::SayAlreadyDeclared(const SourceName &name, Symbol &prev) {
2354   if (context().HasError(prev)) {
2355     // don't report another error about prev
2356   } else {
2357     if (const auto *details{prev.detailsIf<UseDetails>()}) {
2358       Say(name, "'%s' is already declared in this scoping unit"_err_en_US)
2359           .Attach(details->location(),
2360               "It is use-associated with '%s' in module '%s'"_en_US,
2361               details->symbol().name(), GetUsedModule(*details).name());
2362     } else {
2363       SayAlreadyDeclared(name, prev.name());
2364     }
2365     context().SetError(prev);
2366   }
2367 }
2368 void ScopeHandler::SayAlreadyDeclared(
2369     const SourceName &name1, const SourceName &name2) {
2370   if (name1.begin() < name2.begin()) {
2371     SayAlreadyDeclared(name2, name1);
2372   } else {
2373     Say(name1, "'%s' is already declared in this scoping unit"_err_en_US)
2374         .Attach(name2, "Previous declaration of '%s'"_en_US, name2);
2375   }
2376 }
2377 
2378 void ScopeHandler::SayWithReason(const parser::Name &name, Symbol &symbol,
2379     MessageFixedText &&msg1, Message &&msg2) {
2380   bool isFatal{msg1.IsFatal()};
2381   Say(name, std::move(msg1), symbol.name()).Attach(std::move(msg2));
2382   context().SetError(symbol, isFatal);
2383 }
2384 
2385 template <typename... A>
2386 Message &ScopeHandler::SayWithDecl(const parser::Name &name, Symbol &symbol,
2387     MessageFixedText &&msg, A &&...args) {
2388   auto &message{
2389       Say(name.source, std::move(msg), symbol.name(), std::forward<A>(args)...)
2390           .Attach(symbol.name(),
2391               symbol.test(Symbol::Flag::Implicit)
2392                   ? "Implicit declaration of '%s'"_en_US
2393                   : "Declaration of '%s'"_en_US,
2394               name.source)};
2395   if (const auto *proc{symbol.detailsIf<ProcEntityDetails>()}) {
2396     if (auto usedAsProc{proc->usedAsProcedureHere()}) {
2397       if (usedAsProc->begin() != symbol.name().begin()) {
2398         message.Attach(*usedAsProc, "Referenced as a procedure"_en_US);
2399       }
2400     }
2401   }
2402   return message;
2403 }
2404 
2405 void ScopeHandler::SayLocalMustBeVariable(
2406     const parser::Name &name, Symbol &symbol) {
2407   SayWithDecl(name, symbol,
2408       "The name '%s' must be a variable to appear"
2409       " in a locality-spec"_err_en_US);
2410 }
2411 
2412 Message &ScopeHandler::SayDerivedType(
2413     const SourceName &name, MessageFixedText &&msg, const Scope &type) {
2414   const Symbol &typeSymbol{DEREF(type.GetSymbol())};
2415   return Say(name, std::move(msg), name, typeSymbol.name())
2416       .Attach(typeSymbol.name(), "Declaration of derived type '%s'"_en_US,
2417           typeSymbol.name());
2418 }
2419 Message &ScopeHandler::Say2(const SourceName &name1, MessageFixedText &&msg1,
2420     const SourceName &name2, MessageFixedText &&msg2) {
2421   return Say(name1, std::move(msg1)).Attach(name2, std::move(msg2), name2);
2422 }
2423 Message &ScopeHandler::Say2(const SourceName &name, MessageFixedText &&msg1,
2424     Symbol &symbol, MessageFixedText &&msg2) {
2425   bool isFatal{msg1.IsFatal()};
2426   Message &result{Say2(name, std::move(msg1), symbol.name(), std::move(msg2))};
2427   context().SetError(symbol, isFatal);
2428   return result;
2429 }
2430 Message &ScopeHandler::Say2(const parser::Name &name, MessageFixedText &&msg1,
2431     Symbol &symbol, MessageFixedText &&msg2) {
2432   bool isFatal{msg1.IsFatal()};
2433   Message &result{
2434       Say2(name.source, std::move(msg1), symbol.name(), std::move(msg2))};
2435   context().SetError(symbol, isFatal);
2436   return result;
2437 }
2438 
2439 // This is essentially GetProgramUnitContaining(), but it can return
2440 // a mutable Scope &, it ignores statement functions, and it fails
2441 // gracefully for error recovery (returning the original Scope).
2442 template <typename T> static T &GetInclusiveScope(T &scope) {
2443   for (T *s{&scope}; !s->IsGlobal(); s = &s->parent()) {
2444     switch (s->kind()) {
2445     case Scope::Kind::Module:
2446     case Scope::Kind::MainProgram:
2447     case Scope::Kind::Subprogram:
2448     case Scope::Kind::BlockData:
2449       if (!s->IsStmtFunction()) {
2450         return *s;
2451       }
2452       break;
2453     default:;
2454     }
2455   }
2456   return scope;
2457 }
2458 
2459 Scope &ScopeHandler::InclusiveScope() { return GetInclusiveScope(currScope()); }
2460 
2461 Scope *ScopeHandler::GetHostProcedure() {
2462   Scope &parent{InclusiveScope().parent()};
2463   switch (parent.kind()) {
2464   case Scope::Kind::Subprogram:
2465     return &parent;
2466   case Scope::Kind::MainProgram:
2467     return &parent;
2468   default:
2469     return nullptr;
2470   }
2471 }
2472 
2473 Scope &ScopeHandler::NonDerivedTypeScope() {
2474   return currScope_->IsDerivedType() ? currScope_->parent() : *currScope_;
2475 }
2476 
2477 void ScopeHandler::PushScope(Scope::Kind kind, Symbol *symbol) {
2478   PushScope(currScope().MakeScope(kind, symbol));
2479 }
2480 void ScopeHandler::PushScope(Scope &scope) {
2481   currScope_ = &scope;
2482   auto kind{currScope_->kind()};
2483   if (kind != Scope::Kind::BlockConstruct &&
2484       kind != Scope::Kind::OtherConstruct && kind != Scope::Kind::OtherClause) {
2485     BeginScope(scope);
2486   }
2487   // The name of a module or submodule cannot be "used" in its scope,
2488   // as we read 19.3.1(2), so we allow the name to be used as a local
2489   // identifier in the module or submodule too.  Same with programs
2490   // (14.1(3)) and BLOCK DATA.
2491   if (!currScope_->IsDerivedType() && kind != Scope::Kind::Module &&
2492       kind != Scope::Kind::MainProgram && kind != Scope::Kind::BlockData) {
2493     if (auto *symbol{scope.symbol()}) {
2494       // Create a dummy symbol so we can't create another one with the same
2495       // name. It might already be there if we previously pushed the scope.
2496       SourceName name{symbol->name()};
2497       if (!FindInScope(scope, name)) {
2498         auto &newSymbol{MakeSymbol(name)};
2499         if (kind == Scope::Kind::Subprogram) {
2500           // Allow for recursive references.  If this symbol is a function
2501           // without an explicit RESULT(), this new symbol will be discarded
2502           // and replaced with an object of the same name.
2503           newSymbol.set_details(HostAssocDetails{*symbol});
2504         } else {
2505           newSymbol.set_details(MiscDetails{MiscDetails::Kind::ScopeName});
2506         }
2507       }
2508     }
2509   }
2510 }
2511 void ScopeHandler::PopScope() {
2512   CHECK(currScope_ && !currScope_->IsGlobal());
2513   // Entities that are not yet classified as objects or procedures are now
2514   // assumed to be objects.
2515   // TODO: Statement functions
2516   for (auto &pair : currScope()) {
2517     ConvertToObjectEntity(*pair.second);
2518   }
2519   funcResultStack_.Pop();
2520   // If popping back into a global scope, pop back to the main global scope.
2521   SetScope(currScope_->parent().IsGlobal() ? context().globalScope()
2522                                            : currScope_->parent());
2523 }
2524 void ScopeHandler::SetScope(Scope &scope) {
2525   currScope_ = &scope;
2526   ImplicitRulesVisitor::SetScope(InclusiveScope());
2527 }
2528 
2529 Symbol *ScopeHandler::FindSymbol(const parser::Name &name) {
2530   return FindSymbol(currScope(), name);
2531 }
2532 Symbol *ScopeHandler::FindSymbol(const Scope &scope, const parser::Name &name) {
2533   if (scope.IsDerivedType()) {
2534     if (Symbol * symbol{scope.FindComponent(name.source)}) {
2535       if (symbol->has<TypeParamDetails>()) {
2536         return Resolve(name, symbol);
2537       }
2538     }
2539     return FindSymbol(scope.parent(), name);
2540   } else {
2541     // In EQUIVALENCE statements only resolve names in the local scope, see
2542     // 19.5.1.4, paragraph 2, item (10)
2543     return Resolve(name,
2544         inEquivalenceStmt_ ? FindInScope(scope, name)
2545                            : scope.FindSymbol(name.source));
2546   }
2547 }
2548 
2549 Symbol &ScopeHandler::MakeSymbol(
2550     Scope &scope, const SourceName &name, Attrs attrs) {
2551   if (Symbol * symbol{FindInScope(scope, name)}) {
2552     CheckDuplicatedAttrs(name, *symbol, attrs);
2553     SetExplicitAttrs(*symbol, attrs);
2554     return *symbol;
2555   } else {
2556     const auto pair{scope.try_emplace(name, attrs, UnknownDetails{})};
2557     CHECK(pair.second); // name was not found, so must be able to add
2558     return *pair.first->second;
2559   }
2560 }
2561 Symbol &ScopeHandler::MakeSymbol(const SourceName &name, Attrs attrs) {
2562   return MakeSymbol(currScope(), name, attrs);
2563 }
2564 Symbol &ScopeHandler::MakeSymbol(const parser::Name &name, Attrs attrs) {
2565   return Resolve(name, MakeSymbol(name.source, attrs));
2566 }
2567 Symbol &ScopeHandler::MakeHostAssocSymbol(
2568     const parser::Name &name, const Symbol &hostSymbol) {
2569   Symbol &symbol{*NonDerivedTypeScope()
2570                       .try_emplace(name.source, HostAssocDetails{hostSymbol})
2571                       .first->second};
2572   name.symbol = &symbol;
2573   symbol.attrs() = hostSymbol.attrs(); // TODO: except PRIVATE, PUBLIC?
2574   // These attributes can be redundantly reapplied without error
2575   // on the host-associated name, at most once (C815).
2576   symbol.implicitAttrs() =
2577       symbol.attrs() & Attrs{Attr::ASYNCHRONOUS, Attr::VOLATILE};
2578   // SAVE statement in the inner scope will create a new symbol.
2579   // If the host variable is used via host association,
2580   // we have to propagate whether SAVE is implicit in the host scope.
2581   // Otherwise, verifications that do not allow explicit SAVE
2582   // attribute would fail.
2583   symbol.implicitAttrs() |= hostSymbol.implicitAttrs() & Attrs{Attr::SAVE};
2584   symbol.flags() = hostSymbol.flags();
2585   return symbol;
2586 }
2587 Symbol &ScopeHandler::CopySymbol(const SourceName &name, const Symbol &symbol) {
2588   CHECK(!FindInScope(name));
2589   return MakeSymbol(currScope(), name, symbol.attrs());
2590 }
2591 
2592 // Look for name only in scope, not in enclosing scopes.
2593 
2594 Symbol *ScopeHandler::FindInScope(
2595     const Scope &scope, const parser::Name &name) {
2596   return Resolve(name, FindInScope(scope, name.source));
2597 }
2598 Symbol *ScopeHandler::FindInScope(const Scope &scope, const SourceName &name) {
2599   // all variants of names, e.g. "operator(.ne.)" for "operator(/=)"
2600   for (const std::string &n : GetAllNames(context(), name)) {
2601     auto it{scope.find(SourceName{n})};
2602     if (it != scope.end()) {
2603       return &*it->second;
2604     }
2605   }
2606   return nullptr;
2607 }
2608 
2609 // Find a component or type parameter by name in a derived type or its parents.
2610 Symbol *ScopeHandler::FindInTypeOrParents(
2611     const Scope &scope, const parser::Name &name) {
2612   return Resolve(name, scope.FindComponent(name.source));
2613 }
2614 Symbol *ScopeHandler::FindInTypeOrParents(const parser::Name &name) {
2615   return FindInTypeOrParents(currScope(), name);
2616 }
2617 Symbol *ScopeHandler::FindInScopeOrBlockConstructs(
2618     const Scope &scope, SourceName name) {
2619   if (Symbol * symbol{FindInScope(scope, name)}) {
2620     return symbol;
2621   }
2622   for (const Scope &child : scope.children()) {
2623     if (child.kind() == Scope::Kind::BlockConstruct) {
2624       if (Symbol * symbol{FindInScopeOrBlockConstructs(child, name)}) {
2625         return symbol;
2626       }
2627     }
2628   }
2629   return nullptr;
2630 }
2631 
2632 void ScopeHandler::EraseSymbol(const parser::Name &name) {
2633   currScope().erase(name.source);
2634   name.symbol = nullptr;
2635 }
2636 
2637 static bool NeedsType(const Symbol &symbol) {
2638   return !symbol.GetType() &&
2639       common::visit(common::visitors{
2640                         [](const EntityDetails &) { return true; },
2641                         [](const ObjectEntityDetails &) { return true; },
2642                         [](const AssocEntityDetails &) { return true; },
2643                         [&](const ProcEntityDetails &p) {
2644                           return symbol.test(Symbol::Flag::Function) &&
2645                               !symbol.attrs().test(Attr::INTRINSIC) &&
2646                               !p.type() && !p.procInterface();
2647                         },
2648                         [](const auto &) { return false; },
2649                     },
2650           symbol.details());
2651 }
2652 
2653 void ScopeHandler::ApplyImplicitRules(
2654     Symbol &symbol, bool allowForwardReference) {
2655   funcResultStack_.CompleteTypeIfFunctionResult(symbol);
2656   if (context().HasError(symbol) || !NeedsType(symbol)) {
2657     return;
2658   }
2659   if (const DeclTypeSpec * type{GetImplicitType(symbol)}) {
2660     if (!skipImplicitTyping_) {
2661       symbol.set(Symbol::Flag::Implicit);
2662       symbol.SetType(*type);
2663     }
2664     return;
2665   }
2666   if (symbol.has<ProcEntityDetails>() && !symbol.attrs().test(Attr::EXTERNAL)) {
2667     std::optional<Symbol::Flag> functionOrSubroutineFlag;
2668     if (symbol.test(Symbol::Flag::Function)) {
2669       functionOrSubroutineFlag = Symbol::Flag::Function;
2670     } else if (symbol.test(Symbol::Flag::Subroutine)) {
2671       functionOrSubroutineFlag = Symbol::Flag::Subroutine;
2672     }
2673     if (IsIntrinsic(symbol.name(), functionOrSubroutineFlag)) {
2674       // type will be determined in expression semantics
2675       AcquireIntrinsicProcedureFlags(symbol);
2676       return;
2677     }
2678   }
2679   if (allowForwardReference && ImplicitlyTypeForwardRef(symbol)) {
2680     return;
2681   }
2682   if (const auto *entity{symbol.detailsIf<EntityDetails>()};
2683       entity && entity->isDummy()) {
2684     // Dummy argument, no declaration or reference; if it turns
2685     // out to be a subroutine, it's fine, and if it is a function
2686     // or object, it'll be caught later.
2687     return;
2688   }
2689   if (deferImplicitTyping_) {
2690     return;
2691   }
2692   if (!context().HasError(symbol)) {
2693     Say(symbol.name(), "No explicit type declared for '%s'"_err_en_US);
2694     context().SetError(symbol);
2695   }
2696 }
2697 
2698 // Extension: Allow forward references to scalar integer dummy arguments
2699 // or variables in COMMON to appear in specification expressions under
2700 // IMPLICIT NONE(TYPE) when what would otherwise have been their implicit
2701 // type is default INTEGER.
2702 bool ScopeHandler::ImplicitlyTypeForwardRef(Symbol &symbol) {
2703   if (!inSpecificationPart_ || context().HasError(symbol) ||
2704       !(IsDummy(symbol) || FindCommonBlockContaining(symbol)) ||
2705       symbol.Rank() != 0 ||
2706       !context().languageFeatures().IsEnabled(
2707           common::LanguageFeature::ForwardRefImplicitNone)) {
2708     return false;
2709   }
2710   const DeclTypeSpec *type{
2711       GetImplicitType(symbol, false /*ignore IMPLICIT NONE*/)};
2712   if (!type || !type->IsNumeric(TypeCategory::Integer)) {
2713     return false;
2714   }
2715   auto kind{evaluate::ToInt64(type->numericTypeSpec().kind())};
2716   if (!kind || *kind != context().GetDefaultKind(TypeCategory::Integer)) {
2717     return false;
2718   }
2719   if (!ConvertToObjectEntity(symbol)) {
2720     return false;
2721   }
2722   // TODO: check no INTENT(OUT) if dummy?
2723   context().Warn(common::LanguageFeature::ForwardRefImplicitNone, symbol.name(),
2724       "'%s' was used without (or before) being explicitly typed"_warn_en_US,
2725       symbol.name());
2726   symbol.set(Symbol::Flag::Implicit);
2727   symbol.SetType(*type);
2728   return true;
2729 }
2730 
2731 // Ensure that the symbol for an intrinsic procedure is marked with
2732 // the INTRINSIC attribute.  Also set PURE &/or ELEMENTAL as
2733 // appropriate.
2734 void ScopeHandler::AcquireIntrinsicProcedureFlags(Symbol &symbol) {
2735   SetImplicitAttr(symbol, Attr::INTRINSIC);
2736   switch (context().intrinsics().GetIntrinsicClass(symbol.name().ToString())) {
2737   case evaluate::IntrinsicClass::elementalFunction:
2738   case evaluate::IntrinsicClass::elementalSubroutine:
2739     SetExplicitAttr(symbol, Attr::ELEMENTAL);
2740     SetExplicitAttr(symbol, Attr::PURE);
2741     break;
2742   case evaluate::IntrinsicClass::impureSubroutine:
2743     break;
2744   default:
2745     SetExplicitAttr(symbol, Attr::PURE);
2746   }
2747 }
2748 
2749 const DeclTypeSpec *ScopeHandler::GetImplicitType(
2750     Symbol &symbol, bool respectImplicitNoneType) {
2751   const Scope *scope{&symbol.owner()};
2752   if (scope->IsGlobal()) {
2753     scope = &currScope();
2754   }
2755   scope = &GetInclusiveScope(*scope);
2756   const auto *type{implicitRulesMap_->at(scope).GetType(
2757       symbol.name(), respectImplicitNoneType)};
2758   if (type) {
2759     if (const DerivedTypeSpec * derived{type->AsDerived()}) {
2760       // Resolve any forward-referenced derived type; a quick no-op else.
2761       auto &instantiatable{*const_cast<DerivedTypeSpec *>(derived)};
2762       instantiatable.Instantiate(currScope());
2763     }
2764   }
2765   return type;
2766 }
2767 
2768 void ScopeHandler::CheckEntryDummyUse(SourceName source, Symbol *symbol) {
2769   if (!inSpecificationPart_ && symbol &&
2770       symbol->test(Symbol::Flag::EntryDummyArgument)) {
2771     Say(source,
2772         "Dummy argument '%s' may not be used before its ENTRY statement"_err_en_US,
2773         symbol->name());
2774     symbol->set(Symbol::Flag::EntryDummyArgument, false);
2775   }
2776 }
2777 
2778 // Convert symbol to be a ObjectEntity or return false if it can't be.
2779 bool ScopeHandler::ConvertToObjectEntity(Symbol &symbol) {
2780   if (symbol.has<ObjectEntityDetails>()) {
2781     // nothing to do
2782   } else if (symbol.has<UnknownDetails>()) {
2783     // These are attributes that a name could have picked up from
2784     // an attribute statement or type declaration statement.
2785     if (symbol.attrs().HasAny({Attr::EXTERNAL, Attr::INTRINSIC})) {
2786       return false;
2787     }
2788     symbol.set_details(ObjectEntityDetails{});
2789   } else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
2790     if (symbol.attrs().HasAny({Attr::EXTERNAL, Attr::INTRINSIC})) {
2791       return false;
2792     }
2793     funcResultStack_.CompleteTypeIfFunctionResult(symbol);
2794     symbol.set_details(ObjectEntityDetails{std::move(*details)});
2795   } else if (auto *useDetails{symbol.detailsIf<UseDetails>()}) {
2796     return useDetails->symbol().has<ObjectEntityDetails>();
2797   } else if (auto *hostDetails{symbol.detailsIf<HostAssocDetails>()}) {
2798     return hostDetails->symbol().has<ObjectEntityDetails>();
2799   } else {
2800     return false;
2801   }
2802   return true;
2803 }
2804 // Convert symbol to be a ProcEntity or return false if it can't be.
2805 bool ScopeHandler::ConvertToProcEntity(
2806     Symbol &symbol, std::optional<SourceName> usedHere) {
2807   if (symbol.has<ProcEntityDetails>()) {
2808   } else if (symbol.has<UnknownDetails>()) {
2809     symbol.set_details(ProcEntityDetails{});
2810   } else if (auto *details{symbol.detailsIf<EntityDetails>()}) {
2811     if (IsFunctionResult(symbol) &&
2812         !(IsPointer(symbol) && symbol.attrs().test(Attr::EXTERNAL))) {
2813       // Don't turn function result into a procedure pointer unless both
2814       // POINTER and EXTERNAL
2815       return false;
2816     }
2817     funcResultStack_.CompleteTypeIfFunctionResult(symbol);
2818     symbol.set_details(ProcEntityDetails{std::move(*details)});
2819     if (symbol.GetType() && !symbol.test(Symbol::Flag::Implicit)) {
2820       CHECK(!symbol.test(Symbol::Flag::Subroutine));
2821       symbol.set(Symbol::Flag::Function);
2822     }
2823   } else if (auto *useDetails{symbol.detailsIf<UseDetails>()}) {
2824     return useDetails->symbol().has<ProcEntityDetails>();
2825   } else if (auto *hostDetails{symbol.detailsIf<HostAssocDetails>()}) {
2826     return hostDetails->symbol().has<ProcEntityDetails>();
2827   } else {
2828     return false;
2829   }
2830   auto &proc{symbol.get<ProcEntityDetails>()};
2831   if (usedHere && !proc.usedAsProcedureHere()) {
2832     proc.set_usedAsProcedureHere(*usedHere);
2833   }
2834   return true;
2835 }
2836 
2837 const DeclTypeSpec &ScopeHandler::MakeNumericType(
2838     TypeCategory category, const std::optional<parser::KindSelector> &kind) {
2839   KindExpr value{GetKindParamExpr(category, kind)};
2840   if (auto known{evaluate::ToInt64(value)}) {
2841     return MakeNumericType(category, static_cast<int>(*known));
2842   } else {
2843     return currScope_->MakeNumericType(category, std::move(value));
2844   }
2845 }
2846 
2847 const DeclTypeSpec &ScopeHandler::MakeNumericType(
2848     TypeCategory category, int kind) {
2849   return context().MakeNumericType(category, kind);
2850 }
2851 
2852 const DeclTypeSpec &ScopeHandler::MakeLogicalType(
2853     const std::optional<parser::KindSelector> &kind) {
2854   KindExpr value{GetKindParamExpr(TypeCategory::Logical, kind)};
2855   if (auto known{evaluate::ToInt64(value)}) {
2856     return MakeLogicalType(static_cast<int>(*known));
2857   } else {
2858     return currScope_->MakeLogicalType(std::move(value));
2859   }
2860 }
2861 
2862 const DeclTypeSpec &ScopeHandler::MakeLogicalType(int kind) {
2863   return context().MakeLogicalType(kind);
2864 }
2865 
2866 void ScopeHandler::NotePossibleBadForwardRef(const parser::Name &name) {
2867   if (inSpecificationPart_ && !deferImplicitTyping_ && name.symbol) {
2868     auto kind{currScope().kind()};
2869     if ((kind == Scope::Kind::Subprogram && !currScope().IsStmtFunction()) ||
2870         kind == Scope::Kind::BlockConstruct) {
2871       bool isHostAssociated{&name.symbol->owner() == &currScope()
2872               ? name.symbol->has<HostAssocDetails>()
2873               : name.symbol->owner().Contains(currScope())};
2874       if (isHostAssociated) {
2875         specPartState_.forwardRefs.insert(name.source);
2876       }
2877     }
2878   }
2879 }
2880 
2881 std::optional<SourceName> ScopeHandler::HadForwardRef(
2882     const Symbol &symbol) const {
2883   auto iter{specPartState_.forwardRefs.find(symbol.name())};
2884   if (iter != specPartState_.forwardRefs.end()) {
2885     return *iter;
2886   }
2887   return std::nullopt;
2888 }
2889 
2890 bool ScopeHandler::CheckPossibleBadForwardRef(const Symbol &symbol) {
2891   if (!context().HasError(symbol)) {
2892     if (auto fwdRef{HadForwardRef(symbol)}) {
2893       const Symbol *outer{symbol.owner().FindSymbol(symbol.name())};
2894       if (outer && symbol.has<UseDetails>() &&
2895           &symbol.GetUltimate() == &outer->GetUltimate()) {
2896         // e.g. IMPORT of host's USE association
2897         return false;
2898       }
2899       Say(*fwdRef,
2900           "Forward reference to '%s' is not allowed in the same specification part"_err_en_US,
2901           *fwdRef)
2902           .Attach(symbol.name(), "Later declaration of '%s'"_en_US, *fwdRef);
2903       context().SetError(symbol);
2904       return true;
2905     }
2906     if ((IsDummy(symbol) || FindCommonBlockContaining(symbol)) &&
2907         isImplicitNoneType() && symbol.test(Symbol::Flag::Implicit) &&
2908         !context().HasError(symbol)) {
2909       // Dummy or COMMON was implicitly typed despite IMPLICIT NONE(TYPE) in
2910       // ApplyImplicitRules() due to use in a specification expression,
2911       // and no explicit type declaration appeared later.
2912       Say(symbol.name(), "No explicit type declared for '%s'"_err_en_US);
2913       context().SetError(symbol);
2914       return true;
2915     }
2916   }
2917   return false;
2918 }
2919 
2920 void ScopeHandler::MakeExternal(Symbol &symbol) {
2921   if (!symbol.attrs().test(Attr::EXTERNAL)) {
2922     SetImplicitAttr(symbol, Attr::EXTERNAL);
2923     if (symbol.attrs().test(Attr::INTRINSIC)) { // C840
2924       Say(symbol.name(),
2925           "Symbol '%s' cannot have both EXTERNAL and INTRINSIC attributes"_err_en_US,
2926           symbol.name());
2927     }
2928   }
2929 }
2930 
2931 bool ScopeHandler::CheckDuplicatedAttr(
2932     SourceName name, Symbol &symbol, Attr attr) {
2933   if (attr == Attr::SAVE) {
2934     // checked elsewhere
2935   } else if (symbol.attrs().test(attr)) { // C815
2936     if (symbol.implicitAttrs().test(attr)) {
2937       // Implied attribute is now confirmed explicitly
2938       symbol.implicitAttrs().reset(attr);
2939     } else {
2940       Say(name, "%s attribute was already specified on '%s'"_err_en_US,
2941           EnumToString(attr), name);
2942       return false;
2943     }
2944   }
2945   return true;
2946 }
2947 
2948 bool ScopeHandler::CheckDuplicatedAttrs(
2949     SourceName name, Symbol &symbol, Attrs attrs) {
2950   bool ok{true};
2951   attrs.IterateOverMembers(
2952       [&](Attr x) { ok &= CheckDuplicatedAttr(name, symbol, x); });
2953   return ok;
2954 }
2955 
2956 void ScopeHandler::SetCUDADataAttr(SourceName source, Symbol &symbol,
2957     std::optional<common::CUDADataAttr> attr) {
2958   if (attr) {
2959     ConvertToObjectEntity(symbol);
2960     if (auto *object{symbol.detailsIf<ObjectEntityDetails>()}) {
2961       if (*attr != object->cudaDataAttr().value_or(*attr)) {
2962         Say(source,
2963             "'%s' already has another CUDA data attribute ('%s')"_err_en_US,
2964             symbol.name(),
2965             std::string{common::EnumToString(*object->cudaDataAttr())}.c_str());
2966       } else {
2967         object->set_cudaDataAttr(attr);
2968       }
2969     } else {
2970       Say(source,
2971           "'%s' is not an object and may not have a CUDA data attribute"_err_en_US,
2972           symbol.name());
2973     }
2974   }
2975 }
2976 
2977 // ModuleVisitor implementation
2978 
2979 bool ModuleVisitor::Pre(const parser::Only &x) {
2980   common::visit(common::visitors{
2981                     [&](const Indirection<parser::GenericSpec> &generic) {
2982                       GenericSpecInfo genericSpecInfo{generic.value()};
2983                       AddUseOnly(genericSpecInfo.symbolName());
2984                       AddUse(genericSpecInfo);
2985                     },
2986                     [&](const parser::Name &name) {
2987                       AddUseOnly(name.source);
2988                       Resolve(name, AddUse(name.source, name.source).use);
2989                     },
2990                     [&](const parser::Rename &rename) { Walk(rename); },
2991                 },
2992       x.u);
2993   return false;
2994 }
2995 
2996 void ModuleVisitor::CollectUseRenames(const parser::UseStmt &useStmt) {
2997   auto doRename{[&](const parser::Rename &rename) {
2998     if (const auto *names{std::get_if<parser::Rename::Names>(&rename.u)}) {
2999       AddUseRename(std::get<1>(names->t).source, useStmt.moduleName.source);
3000     }
3001   }};
3002   common::visit(
3003       common::visitors{
3004           [&](const std::list<parser::Rename> &renames) {
3005             for (const auto &rename : renames) {
3006               doRename(rename);
3007             }
3008           },
3009           [&](const std::list<parser::Only> &onlys) {
3010             for (const auto &only : onlys) {
3011               if (const auto *rename{std::get_if<parser::Rename>(&only.u)}) {
3012                 doRename(*rename);
3013               }
3014             }
3015           },
3016       },
3017       useStmt.u);
3018 }
3019 
3020 bool ModuleVisitor::Pre(const parser::Rename::Names &x) {
3021   const auto &localName{std::get<0>(x.t)};
3022   const auto &useName{std::get<1>(x.t)};
3023   SymbolRename rename{AddUse(localName.source, useName.source)};
3024   Resolve(useName, rename.use);
3025   Resolve(localName, rename.local);
3026   return false;
3027 }
3028 bool ModuleVisitor::Pre(const parser::Rename::Operators &x) {
3029   const parser::DefinedOpName &local{std::get<0>(x.t)};
3030   const parser::DefinedOpName &use{std::get<1>(x.t)};
3031   GenericSpecInfo localInfo{local};
3032   GenericSpecInfo useInfo{use};
3033   if (IsIntrinsicOperator(context(), local.v.source)) {
3034     Say(local.v,
3035         "Intrinsic operator '%s' may not be used as a defined operator"_err_en_US);
3036   } else if (IsLogicalConstant(context(), local.v.source)) {
3037     Say(local.v,
3038         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
3039   } else {
3040     SymbolRename rename{AddUse(localInfo.symbolName(), useInfo.symbolName())};
3041     useInfo.Resolve(rename.use);
3042     localInfo.Resolve(rename.local);
3043   }
3044   return false;
3045 }
3046 
3047 // Set useModuleScope_ to the Scope of the module being used.
3048 bool ModuleVisitor::Pre(const parser::UseStmt &x) {
3049   std::optional<bool> isIntrinsic;
3050   if (x.nature) {
3051     isIntrinsic = *x.nature == parser::UseStmt::ModuleNature::Intrinsic;
3052   } else if (currScope().IsModule() && currScope().symbol() &&
3053       currScope().symbol()->attrs().test(Attr::INTRINSIC)) {
3054     // Intrinsic modules USE only other intrinsic modules
3055     isIntrinsic = true;
3056   }
3057   useModuleScope_ = FindModule(x.moduleName, isIntrinsic);
3058   if (!useModuleScope_) {
3059     return false;
3060   }
3061   AddAndCheckModuleUse(x.moduleName.source,
3062       useModuleScope_->parent().kind() == Scope::Kind::IntrinsicModules);
3063   // use the name from this source file
3064   useModuleScope_->symbol()->ReplaceName(x.moduleName.source);
3065   return true;
3066 }
3067 
3068 void ModuleVisitor::Post(const parser::UseStmt &x) {
3069   if (const auto *list{std::get_if<std::list<parser::Rename>>(&x.u)}) {
3070     // Not a use-only: collect the names that were used in renames,
3071     // then add a use for each public name that was not renamed.
3072     std::set<SourceName> useNames;
3073     for (const auto &rename : *list) {
3074       common::visit(common::visitors{
3075                         [&](const parser::Rename::Names &names) {
3076                           useNames.insert(std::get<1>(names.t).source);
3077                         },
3078                         [&](const parser::Rename::Operators &ops) {
3079                           useNames.insert(std::get<1>(ops.t).v.source);
3080                         },
3081                     },
3082           rename.u);
3083     }
3084     for (const auto &[name, symbol] : *useModuleScope_) {
3085       if (symbol->attrs().test(Attr::PUBLIC) && !IsUseRenamed(symbol->name()) &&
3086           (!symbol->implicitAttrs().test(Attr::INTRINSIC) ||
3087               symbol->has<UseDetails>()) &&
3088           !symbol->has<MiscDetails>() && useNames.count(name) == 0) {
3089         SourceName location{x.moduleName.source};
3090         if (auto *localSymbol{FindInScope(name)}) {
3091           DoAddUse(location, localSymbol->name(), *localSymbol, *symbol);
3092         } else {
3093           DoAddUse(location, location, CopySymbol(name, *symbol), *symbol);
3094         }
3095       }
3096     }
3097   }
3098   useModuleScope_ = nullptr;
3099 }
3100 
3101 ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
3102     const SourceName &localName, const SourceName &useName) {
3103   return AddUse(localName, useName, FindInScope(*useModuleScope_, useName));
3104 }
3105 
3106 ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
3107     const SourceName &localName, const SourceName &useName, Symbol *useSymbol) {
3108   if (!useModuleScope_) {
3109     return {}; // error occurred finding module
3110   }
3111   if (!useSymbol) {
3112     Say(useName, "'%s' not found in module '%s'"_err_en_US, MakeOpName(useName),
3113         useModuleScope_->GetName().value());
3114     return {};
3115   }
3116   if (useSymbol->attrs().test(Attr::PRIVATE) &&
3117       !FindModuleFileContaining(currScope())) {
3118     // Privacy is not enforced in module files so that generic interfaces
3119     // can be resolved to private specific procedures in specification
3120     // expressions.
3121     Say(useName, "'%s' is PRIVATE in '%s'"_err_en_US, MakeOpName(useName),
3122         useModuleScope_->GetName().value());
3123     return {};
3124   }
3125   auto &localSymbol{MakeSymbol(localName)};
3126   DoAddUse(useName, localName, localSymbol, *useSymbol);
3127   return {&localSymbol, useSymbol};
3128 }
3129 
3130 // symbol must be either a Use or a Generic formed by merging two uses.
3131 // Convert it to a UseError with this additional location.
3132 static bool ConvertToUseError(
3133     Symbol &symbol, const SourceName &location, const Scope &module) {
3134   const auto *useDetails{symbol.detailsIf<UseDetails>()};
3135   if (!useDetails) {
3136     if (auto *genericDetails{symbol.detailsIf<GenericDetails>()}) {
3137       if (!genericDetails->uses().empty()) {
3138         useDetails = &genericDetails->uses().at(0)->get<UseDetails>();
3139       }
3140     }
3141   }
3142   if (useDetails) {
3143     symbol.set_details(
3144         UseErrorDetails{*useDetails}.add_occurrence(location, module));
3145     return true;
3146   } else {
3147     return false;
3148   }
3149 }
3150 
3151 void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
3152     Symbol &originalLocal, const Symbol &useSymbol) {
3153   Symbol *localSymbol{&originalLocal};
3154   if (auto *details{localSymbol->detailsIf<UseErrorDetails>()}) {
3155     details->add_occurrence(location, *useModuleScope_);
3156     return;
3157   }
3158   const Symbol &useUltimate{useSymbol.GetUltimate()};
3159   const auto *useGeneric{useUltimate.detailsIf<GenericDetails>()};
3160   if (localSymbol->has<UnknownDetails>()) {
3161     if (useGeneric &&
3162         ((useGeneric->specific() &&
3163              IsProcedurePointer(*useGeneric->specific())) ||
3164             (useGeneric->derivedType() &&
3165                 useUltimate.name() != localSymbol->name()))) {
3166       // We are use-associating a generic that either shadows a procedure
3167       // pointer or shadows a derived type with a distinct name.
3168       // Local references that might be made to the procedure pointer should
3169       // use a UseDetails symbol for proper data addressing, and a derived
3170       // type needs to be in scope with its local name.  So create an
3171       // empty local generic now into which the use-associated generic may
3172       // be copied.
3173       localSymbol->set_details(GenericDetails{});
3174       localSymbol->get<GenericDetails>().set_kind(useGeneric->kind());
3175     } else { // just create UseDetails
3176       localSymbol->set_details(UseDetails{localName, useSymbol});
3177       localSymbol->attrs() =
3178           useSymbol.attrs() & ~Attrs{Attr::PUBLIC, Attr::PRIVATE, Attr::SAVE};
3179       localSymbol->implicitAttrs() =
3180           localSymbol->attrs() & Attrs{Attr::ASYNCHRONOUS, Attr::VOLATILE};
3181       localSymbol->flags() = useSymbol.flags();
3182       return;
3183     }
3184   }
3185 
3186   Symbol &localUltimate{localSymbol->GetUltimate()};
3187   if (&localUltimate == &useUltimate) {
3188     // use-associating the same symbol again -- ok
3189     return;
3190   }
3191 
3192   // There are many possible combinations of symbol types that could arrive
3193   // with the same (local) name vie USE association from distinct modules.
3194   // Fortran allows a generic interface to share its name with a derived type,
3195   // or with the name of a non-generic procedure (which should be one of the
3196   // generic's specific procedures).  Implementing all these possibilities is
3197   // complicated.
3198   // Error cases are converted into UseErrorDetails symbols to trigger error
3199   // messages when/if bad combinations are actually used later in the program.
3200   // The error cases are:
3201   //   - two distinct derived types
3202   //   - two distinct non-generic procedures
3203   //   - a generic and a non-generic that is not already one of its specifics
3204   //   - anything other than a derived type, non-generic procedure, or
3205   //     generic procedure being combined with something other than an
3206   //     prior USE association of itself
3207   auto *localGeneric{localUltimate.detailsIf<GenericDetails>()};
3208   Symbol *localDerivedType{nullptr};
3209   if (localUltimate.has<DerivedTypeDetails>()) {
3210     localDerivedType = &localUltimate;
3211   } else if (localGeneric) {
3212     if (auto *dt{localGeneric->derivedType()};
3213         dt && !dt->attrs().test(Attr::PRIVATE)) {
3214       localDerivedType = dt;
3215     }
3216   }
3217   const Symbol *useDerivedType{nullptr};
3218   if (useUltimate.has<DerivedTypeDetails>()) {
3219     useDerivedType = &useUltimate;
3220   } else if (useGeneric) {
3221     if (const auto *dt{useGeneric->derivedType()};
3222         dt && !dt->attrs().test(Attr::PRIVATE)) {
3223       useDerivedType = dt;
3224     }
3225   }
3226 
3227   Symbol *localProcedure{nullptr};
3228   if (localGeneric) {
3229     if (localGeneric->specific() &&
3230         !localGeneric->specific()->attrs().test(Attr::PRIVATE)) {
3231       localProcedure = localGeneric->specific();
3232     }
3233   } else if (IsProcedure(localUltimate)) {
3234     localProcedure = &localUltimate;
3235   }
3236   const Symbol *useProcedure{nullptr};
3237   if (useGeneric) {
3238     if (useGeneric->specific() &&
3239         !useGeneric->specific()->attrs().test(Attr::PRIVATE)) {
3240       useProcedure = useGeneric->specific();
3241     }
3242   } else if (IsProcedure(useUltimate)) {
3243     useProcedure = &useUltimate;
3244   }
3245 
3246   // Creates a UseErrorDetails symbol in the current scope for a
3247   // current UseDetails symbol, but leaves the UseDetails in the
3248   // scope's name map.
3249   auto CreateLocalUseError{[&]() {
3250     EraseSymbol(*localSymbol);
3251     CHECK(localSymbol->has<UseDetails>());
3252     UseErrorDetails details{localSymbol->get<UseDetails>()};
3253     details.add_occurrence(location, *useModuleScope_);
3254     Symbol *newSymbol{&MakeSymbol(localName, Attrs{}, std::move(details))};
3255     // Restore *localSymbol in currScope
3256     auto iter{currScope().find(localName)};
3257     CHECK(iter != currScope().end() && &*iter->second == newSymbol);
3258     iter->second = MutableSymbolRef{*localSymbol};
3259     return newSymbol;
3260   }};
3261 
3262   // When two derived types arrived, try to combine them.
3263   const Symbol *combinedDerivedType{nullptr};
3264   if (!useDerivedType) {
3265     combinedDerivedType = localDerivedType;
3266   } else if (!localDerivedType) {
3267     if (useDerivedType->name() == localName) {
3268       combinedDerivedType = useDerivedType;
3269     } else {
3270       combinedDerivedType =
3271           &currScope().MakeSymbol(localSymbol->name(), useDerivedType->attrs(),
3272               UseDetails{localSymbol->name(), *useDerivedType});
3273     }
3274   } else if (&localDerivedType->GetUltimate() ==
3275       &useDerivedType->GetUltimate()) {
3276     combinedDerivedType = localDerivedType;
3277   } else {
3278     const Scope *localScope{localDerivedType->GetUltimate().scope()};
3279     const Scope *useScope{useDerivedType->GetUltimate().scope()};
3280     if (localScope && useScope && localScope->derivedTypeSpec() &&
3281         useScope->derivedTypeSpec() &&
3282         evaluate::AreSameDerivedType(
3283             *localScope->derivedTypeSpec(), *useScope->derivedTypeSpec())) {
3284       combinedDerivedType = localDerivedType;
3285     } else {
3286       // Create a local UseErrorDetails for the ambiguous derived type
3287       if (localGeneric) {
3288         combinedDerivedType = CreateLocalUseError();
3289       } else {
3290         ConvertToUseError(*localSymbol, location, *useModuleScope_);
3291         combinedDerivedType = localSymbol;
3292       }
3293     }
3294     if (!localGeneric && !useGeneric) {
3295       return; // both symbols are derived types; done
3296     }
3297   }
3298 
3299   auto AreSameProcedure{[&](const Symbol &p1, const Symbol &p2) {
3300     if (&p1 == &p2) {
3301       return true;
3302     } else if (p1.name() != p2.name()) {
3303       return false;
3304     } else if (p1.attrs().test(Attr::INTRINSIC) ||
3305         p2.attrs().test(Attr::INTRINSIC)) {
3306       return p1.attrs().test(Attr::INTRINSIC) &&
3307           p2.attrs().test(Attr::INTRINSIC);
3308     } else if (!IsProcedure(p1) || !IsProcedure(p2)) {
3309       return false;
3310     } else if (IsPointer(p1) || IsPointer(p2)) {
3311       return false;
3312     } else if (const auto *subp{p1.detailsIf<SubprogramDetails>()};
3313                subp && !subp->isInterface()) {
3314       return false; // defined in module, not an external
3315     } else if (const auto *subp{p2.detailsIf<SubprogramDetails>()};
3316                subp && !subp->isInterface()) {
3317       return false; // defined in module, not an external
3318     } else {
3319       // Both are external interfaces, perhaps to the same procedure
3320       auto class1{ClassifyProcedure(p1)};
3321       auto class2{ClassifyProcedure(p2)};
3322       if (class1 == ProcedureDefinitionClass::External &&
3323           class2 == ProcedureDefinitionClass::External) {
3324         auto chars1{evaluate::characteristics::Procedure::Characterize(
3325             p1, GetFoldingContext())};
3326         auto chars2{evaluate::characteristics::Procedure::Characterize(
3327             p2, GetFoldingContext())};
3328         // same procedure interface defined identically in two modules?
3329         return chars1 && chars2 && *chars1 == *chars2;
3330       } else {
3331         return false;
3332       }
3333     }
3334   }};
3335 
3336   // When two non-generic procedures arrived, try to combine them.
3337   const Symbol *combinedProcedure{nullptr};
3338   if (!localProcedure) {
3339     combinedProcedure = useProcedure;
3340   } else if (!useProcedure) {
3341     combinedProcedure = localProcedure;
3342   } else {
3343     if (AreSameProcedure(
3344             localProcedure->GetUltimate(), useProcedure->GetUltimate())) {
3345       if (!localGeneric && !useGeneric) {
3346         return; // both symbols are non-generic procedures
3347       }
3348       combinedProcedure = localProcedure;
3349     }
3350   }
3351 
3352   // Prepare to merge generics
3353   bool cantCombine{false};
3354   if (localGeneric) {
3355     if (useGeneric || useDerivedType) {
3356     } else if (&useUltimate == &BypassGeneric(localUltimate).GetUltimate()) {
3357       return; // nothing to do; used subprogram is local's specific
3358     } else if (useUltimate.attrs().test(Attr::INTRINSIC) &&
3359         useUltimate.name() == localSymbol->name()) {
3360       return; // local generic can extend intrinsic
3361     } else {
3362       for (const auto &ref : localGeneric->specificProcs()) {
3363         if (&ref->GetUltimate() == &useUltimate) {
3364           return; // used non-generic is already a specific of local generic
3365         }
3366       }
3367       cantCombine = true;
3368     }
3369   } else if (useGeneric) {
3370     if (localDerivedType) {
3371     } else if (&localUltimate == &BypassGeneric(useUltimate).GetUltimate() ||
3372         (localSymbol->attrs().test(Attr::INTRINSIC) &&
3373             localUltimate.name() == useUltimate.name())) {
3374       // Local is the specific of the used generic or an intrinsic with the
3375       // same name; replace it.
3376       EraseSymbol(*localSymbol);
3377       Symbol &newSymbol{MakeSymbol(localName,
3378           useUltimate.attrs() & ~Attrs{Attr::PUBLIC, Attr::PRIVATE},
3379           UseDetails{localName, useUltimate})};
3380       newSymbol.flags() = useSymbol.flags();
3381       return;
3382     } else {
3383       for (const auto &ref : useGeneric->specificProcs()) {
3384         if (&ref->GetUltimate() == &localUltimate) {
3385           return; // local non-generic is already a specific of used generic
3386         }
3387       }
3388       cantCombine = true;
3389     }
3390   } else {
3391     cantCombine = true;
3392   }
3393 
3394   // If symbols are not combinable, create a use error.
3395   if (cantCombine) {
3396     if (!ConvertToUseError(*localSymbol, location, *useModuleScope_)) {
3397       Say(location,
3398           "Cannot use-associate '%s'; it is already declared in this scope"_err_en_US,
3399           localName)
3400           .Attach(localSymbol->name(), "Previous declaration of '%s'"_en_US,
3401               localName);
3402     }
3403     return;
3404   }
3405 
3406   // At this point, there must be at least one generic interface.
3407   CHECK(localGeneric || (useGeneric && (localDerivedType || localProcedure)));
3408 
3409   // Ensure that a use-associated specific procedure that is a procedure
3410   // pointer is properly represented as a USE association of an entity.
3411   if (IsProcedurePointer(useProcedure)) {
3412     Symbol &combined{currScope().MakeSymbol(localSymbol->name(),
3413         useProcedure->attrs(), UseDetails{localName, *useProcedure})};
3414     combined.flags() |= useProcedure->flags();
3415     combinedProcedure = &combined;
3416   }
3417 
3418   if (localGeneric) {
3419     // Create a local copy of a previously use-associated generic so that
3420     // it can be locally extended without corrupting the original.
3421     if (localSymbol->has<UseDetails>()) {
3422       GenericDetails generic;
3423       generic.CopyFrom(DEREF(localGeneric));
3424       EraseSymbol(*localSymbol);
3425       Symbol &newSymbol{MakeSymbol(
3426           localSymbol->name(), localSymbol->attrs(), std::move(generic))};
3427       newSymbol.flags() = localSymbol->flags();
3428       localGeneric = &newSymbol.get<GenericDetails>();
3429       localGeneric->AddUse(*localSymbol);
3430       localSymbol = &newSymbol;
3431     }
3432     if (useGeneric) {
3433       // Combine two use-associated generics
3434       localSymbol->attrs() =
3435           useSymbol.attrs() & ~Attrs{Attr::PUBLIC, Attr::PRIVATE};
3436       localSymbol->flags() = useSymbol.flags();
3437       AddGenericUse(*localGeneric, localName, useUltimate);
3438       localGeneric->clear_derivedType();
3439       localGeneric->CopyFrom(*useGeneric);
3440     }
3441     localGeneric->clear_derivedType();
3442     if (combinedDerivedType) {
3443       localGeneric->set_derivedType(*const_cast<Symbol *>(combinedDerivedType));
3444     }
3445     localGeneric->clear_specific();
3446     if (combinedProcedure) {
3447       localGeneric->set_specific(*const_cast<Symbol *>(combinedProcedure));
3448     }
3449   } else {
3450     CHECK(localSymbol->has<UseDetails>());
3451     // Create a local copy of the use-associated generic, then extend it
3452     // with the combined derived type &/or non-generic procedure.
3453     GenericDetails generic;
3454     generic.CopyFrom(*useGeneric);
3455     EraseSymbol(*localSymbol);
3456     Symbol &newSymbol{MakeSymbol(localName,
3457         useUltimate.attrs() & ~Attrs{Attr::PUBLIC, Attr::PRIVATE},
3458         std::move(generic))};
3459     newSymbol.flags() = useUltimate.flags();
3460     auto &newUseGeneric{newSymbol.get<GenericDetails>()};
3461     AddGenericUse(newUseGeneric, localName, useUltimate);
3462     newUseGeneric.AddUse(*localSymbol);
3463     if (combinedDerivedType) {
3464       if (const auto *oldDT{newUseGeneric.derivedType()}) {
3465         CHECK(&oldDT->GetUltimate() == &combinedDerivedType->GetUltimate());
3466       } else {
3467         newUseGeneric.set_derivedType(
3468             *const_cast<Symbol *>(combinedDerivedType));
3469       }
3470     }
3471     if (combinedProcedure) {
3472       newUseGeneric.set_specific(*const_cast<Symbol *>(combinedProcedure));
3473     }
3474   }
3475 }
3476 
3477 void ModuleVisitor::AddUse(const GenericSpecInfo &info) {
3478   if (useModuleScope_) {
3479     const auto &name{info.symbolName()};
3480     auto rename{AddUse(name, name, FindInScope(*useModuleScope_, name))};
3481     info.Resolve(rename.use);
3482   }
3483 }
3484 
3485 // Create a UseDetails symbol for this USE and add it to generic
3486 Symbol &ModuleVisitor::AddGenericUse(
3487     GenericDetails &generic, const SourceName &name, const Symbol &useSymbol) {
3488   Symbol &newSymbol{
3489       currScope().MakeSymbol(name, {}, UseDetails{name, useSymbol})};
3490   generic.AddUse(newSymbol);
3491   return newSymbol;
3492 }
3493 
3494 // Enforce F'2023 C1406 as a warning
3495 void ModuleVisitor::AddAndCheckModuleUse(SourceName name, bool isIntrinsic) {
3496   if (isIntrinsic) {
3497     if (auto iter{nonIntrinsicUses_.find(name)};
3498         iter != nonIntrinsicUses_.end()) {
3499       if (auto *msg{context().Warn(common::LanguageFeature::MiscUseExtensions,
3500               name,
3501               "Should not USE the intrinsic module '%s' in the same scope as a USE of the non-intrinsic module"_port_en_US,
3502               name)}) {
3503         msg->Attach(*iter, "Previous USE of '%s'"_en_US, *iter);
3504       }
3505     }
3506     intrinsicUses_.insert(name);
3507   } else {
3508     if (auto iter{intrinsicUses_.find(name)}; iter != intrinsicUses_.end()) {
3509       if (auto *msg{context().Warn(common::LanguageFeature::MiscUseExtensions,
3510               name,
3511               "Should not USE the non-intrinsic module '%s' in the same scope as a USE of the intrinsic module"_port_en_US,
3512               name)}) {
3513         msg->Attach(*iter, "Previous USE of '%s'"_en_US, *iter);
3514       }
3515     }
3516     nonIntrinsicUses_.insert(name);
3517   }
3518 }
3519 
3520 bool ModuleVisitor::BeginSubmodule(
3521     const parser::Name &name, const parser::ParentIdentifier &parentId) {
3522   const auto &ancestorName{std::get<parser::Name>(parentId.t)};
3523   Scope *parentScope{nullptr};
3524   Scope *ancestor{FindModule(ancestorName, false /*not intrinsic*/)};
3525   if (ancestor) {
3526     if (const auto &parentName{
3527             std::get<std::optional<parser::Name>>(parentId.t)}) {
3528       parentScope = FindModule(*parentName, false /*not intrinsic*/, ancestor);
3529     } else {
3530       parentScope = ancestor;
3531     }
3532   }
3533   if (parentScope) {
3534     PushScope(*parentScope);
3535   } else {
3536     // Error recovery: there's no ancestor scope, so create a dummy one to
3537     // hold the submodule's scope.
3538     SourceName dummyName{context().GetTempName(currScope())};
3539     Symbol &dummySymbol{MakeSymbol(dummyName, Attrs{}, ModuleDetails{false})};
3540     PushScope(Scope::Kind::Module, &dummySymbol);
3541     parentScope = &currScope();
3542   }
3543   BeginModule(name, true);
3544   set_inheritFromParent(false); // submodules don't inherit parents' implicits
3545   if (ancestor && !ancestor->AddSubmodule(name.source, currScope())) {
3546     Say(name, "Module '%s' already has a submodule named '%s'"_err_en_US,
3547         ancestorName.source, name.source);
3548   }
3549   return true;
3550 }
3551 
3552 void ModuleVisitor::BeginModule(const parser::Name &name, bool isSubmodule) {
3553   // Submodule symbols are not visible in their parents' scopes.
3554   Symbol &symbol{isSubmodule ? Resolve(name,
3555                                    currScope().MakeSymbol(name.source, Attrs{},
3556                                        ModuleDetails{true}))
3557                              : MakeSymbol(name, ModuleDetails{false})};
3558   auto &details{symbol.get<ModuleDetails>()};
3559   PushScope(Scope::Kind::Module, &symbol);
3560   details.set_scope(&currScope());
3561   prevAccessStmt_ = std::nullopt;
3562 }
3563 
3564 // Find a module or submodule by name and return its scope.
3565 // If ancestor is present, look for a submodule of that ancestor module.
3566 // May have to read a .mod file to find it.
3567 // If an error occurs, report it and return nullptr.
3568 Scope *ModuleVisitor::FindModule(const parser::Name &name,
3569     std::optional<bool> isIntrinsic, Scope *ancestor) {
3570   ModFileReader reader{context()};
3571   Scope *scope{
3572       reader.Read(name.source, isIntrinsic, ancestor, /*silent=*/false)};
3573   if (!scope) {
3574     return nullptr;
3575   }
3576   if (DoesScopeContain(scope, currScope())) { // 14.2.2(1)
3577     Say(name, "Module '%s' cannot USE itself"_err_en_US);
3578   }
3579   Resolve(name, scope->symbol());
3580   return scope;
3581 }
3582 
3583 void ModuleVisitor::ApplyDefaultAccess() {
3584   const auto *moduleDetails{
3585       DEREF(currScope().symbol()).detailsIf<ModuleDetails>()};
3586   CHECK(moduleDetails);
3587   Attr defaultAttr{
3588       DEREF(moduleDetails).isDefaultPrivate() ? Attr::PRIVATE : Attr::PUBLIC};
3589   for (auto &pair : currScope()) {
3590     Symbol &symbol{*pair.second};
3591     if (!symbol.attrs().HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
3592       Attr attr{defaultAttr};
3593       if (auto *generic{symbol.detailsIf<GenericDetails>()}) {
3594         if (generic->derivedType()) {
3595           // If a generic interface has a derived type of the same
3596           // name that has an explicit accessibility attribute, then
3597           // the generic must have the same accessibility.
3598           if (generic->derivedType()->attrs().test(Attr::PUBLIC)) {
3599             attr = Attr::PUBLIC;
3600           } else if (generic->derivedType()->attrs().test(Attr::PRIVATE)) {
3601             attr = Attr::PRIVATE;
3602           }
3603         }
3604       }
3605       SetImplicitAttr(symbol, attr);
3606     }
3607   }
3608 }
3609 
3610 // InterfaceVistor implementation
3611 
3612 bool InterfaceVisitor::Pre(const parser::InterfaceStmt &x) {
3613   bool isAbstract{std::holds_alternative<parser::Abstract>(x.u)};
3614   genericInfo_.emplace(/*isInterface*/ true, isAbstract);
3615   return BeginAttrs();
3616 }
3617 
3618 void InterfaceVisitor::Post(const parser::InterfaceStmt &) { EndAttrs(); }
3619 
3620 void InterfaceVisitor::Post(const parser::EndInterfaceStmt &) {
3621   ResolveNewSpecifics();
3622   genericInfo_.pop();
3623 }
3624 
3625 // Create a symbol in genericSymbol_ for this GenericSpec.
3626 bool InterfaceVisitor::Pre(const parser::GenericSpec &x) {
3627   if (auto *symbol{FindInScope(GenericSpecInfo{x}.symbolName())}) {
3628     SetGenericSymbol(*symbol);
3629   }
3630   return false;
3631 }
3632 
3633 bool InterfaceVisitor::Pre(const parser::ProcedureStmt &x) {
3634   if (!isGeneric()) {
3635     Say("A PROCEDURE statement is only allowed in a generic interface block"_err_en_US);
3636   } else {
3637     auto kind{std::get<parser::ProcedureStmt::Kind>(x.t)};
3638     const auto &names{std::get<std::list<parser::Name>>(x.t)};
3639     AddSpecificProcs(names, kind);
3640   }
3641   return false;
3642 }
3643 
3644 bool InterfaceVisitor::Pre(const parser::GenericStmt &) {
3645   genericInfo_.emplace(/*isInterface*/ false);
3646   return BeginAttrs();
3647 }
3648 void InterfaceVisitor::Post(const parser::GenericStmt &x) {
3649   auto attrs{EndAttrs()};
3650   if (Symbol * symbol{GetGenericInfo().symbol}) {
3651     SetExplicitAttrs(*symbol, attrs);
3652   }
3653   const auto &names{std::get<std::list<parser::Name>>(x.t)};
3654   AddSpecificProcs(names, ProcedureKind::Procedure);
3655   ResolveNewSpecifics();
3656   genericInfo_.pop();
3657 }
3658 
3659 bool InterfaceVisitor::inInterfaceBlock() const {
3660   return !genericInfo_.empty() && GetGenericInfo().isInterface;
3661 }
3662 bool InterfaceVisitor::isGeneric() const {
3663   return !genericInfo_.empty() && GetGenericInfo().symbol;
3664 }
3665 bool InterfaceVisitor::isAbstract() const {
3666   return !genericInfo_.empty() && GetGenericInfo().isAbstract;
3667 }
3668 
3669 void InterfaceVisitor::AddSpecificProcs(
3670     const std::list<parser::Name> &names, ProcedureKind kind) {
3671   if (Symbol * symbol{GetGenericInfo().symbol};
3672       symbol && symbol->has<GenericDetails>()) {
3673     for (const auto &name : names) {
3674       specificsForGenericProcs_.emplace(symbol, std::make_pair(&name, kind));
3675       genericsForSpecificProcs_.emplace(name.source, symbol);
3676     }
3677   }
3678 }
3679 
3680 // By now we should have seen all specific procedures referenced by name in
3681 // this generic interface. Resolve those names to symbols.
3682 void GenericHandler::ResolveSpecificsInGeneric(
3683     Symbol &generic, bool isEndOfSpecificationPart) {
3684   auto &details{generic.get<GenericDetails>()};
3685   UnorderedSymbolSet symbolsSeen;
3686   for (const Symbol &symbol : details.specificProcs()) {
3687     symbolsSeen.insert(symbol.GetUltimate());
3688   }
3689   auto range{specificsForGenericProcs_.equal_range(&generic)};
3690   SpecificProcMapType retain;
3691   for (auto it{range.first}; it != range.second; ++it) {
3692     const parser::Name *name{it->second.first};
3693     auto kind{it->second.second};
3694     const Symbol *symbol{isEndOfSpecificationPart
3695             ? FindSymbol(*name)
3696             : FindInScope(generic.owner(), *name)};
3697     ProcedureDefinitionClass defClass{ProcedureDefinitionClass::None};
3698     const Symbol *specific{symbol};
3699     const Symbol *ultimate{nullptr};
3700     if (symbol) {
3701       // Subtlety: when *symbol is a use- or host-association, the specific
3702       // procedure that is recorded in the GenericDetails below must be *symbol,
3703       // not the specific procedure shadowed by a generic, because that specific
3704       // procedure may be a symbol from another module and its name unavailable
3705       // to emit to a module file.
3706       const Symbol &bypassed{BypassGeneric(*symbol)};
3707       if (symbol == &symbol->GetUltimate()) {
3708         specific = &bypassed;
3709       }
3710       ultimate = &bypassed.GetUltimate();
3711       defClass = ClassifyProcedure(*ultimate);
3712     }
3713     std::optional<MessageFixedText> error;
3714     if (defClass == ProcedureDefinitionClass::Module) {
3715       // ok
3716     } else if (kind == ProcedureKind::ModuleProcedure) {
3717       error = "'%s' is not a module procedure"_err_en_US;
3718     } else {
3719       switch (defClass) {
3720       case ProcedureDefinitionClass::Intrinsic:
3721       case ProcedureDefinitionClass::External:
3722       case ProcedureDefinitionClass::Internal:
3723       case ProcedureDefinitionClass::Dummy:
3724       case ProcedureDefinitionClass::Pointer:
3725         break;
3726       case ProcedureDefinitionClass::None:
3727         error = "'%s' is not a procedure"_err_en_US;
3728         break;
3729       default:
3730         error =
3731             "'%s' is not a procedure that can appear in a generic interface"_err_en_US;
3732         break;
3733       }
3734     }
3735     if (error) {
3736       if (isEndOfSpecificationPart) {
3737         Say(*name, std::move(*error));
3738       } else {
3739         // possible forward reference, catch it later
3740         retain.emplace(&generic, std::make_pair(name, kind));
3741       }
3742     } else if (!ultimate) {
3743     } else if (symbolsSeen.insert(*ultimate).second /*true if added*/) {
3744       // When a specific procedure is a USE association, that association
3745       // is saved in the generic's specifics, not its ultimate symbol,
3746       // so that module file output of interfaces can distinguish them.
3747       details.AddSpecificProc(*specific, name->source);
3748     } else if (specific == ultimate) {
3749       Say(name->source,
3750           "Procedure '%s' is already specified in generic '%s'"_err_en_US,
3751           name->source, MakeOpName(generic.name()));
3752     } else {
3753       Say(name->source,
3754           "Procedure '%s' from module '%s' is already specified in generic '%s'"_err_en_US,
3755           ultimate->name(), ultimate->owner().GetName().value(),
3756           MakeOpName(generic.name()));
3757     }
3758   }
3759   specificsForGenericProcs_.erase(range.first, range.second);
3760   specificsForGenericProcs_.merge(std::move(retain));
3761 }
3762 
3763 void GenericHandler::DeclaredPossibleSpecificProc(Symbol &proc) {
3764   auto range{genericsForSpecificProcs_.equal_range(proc.name())};
3765   for (auto iter{range.first}; iter != range.second; ++iter) {
3766     ResolveSpecificsInGeneric(*iter->second, false);
3767   }
3768 }
3769 
3770 void InterfaceVisitor::ResolveNewSpecifics() {
3771   if (Symbol * generic{genericInfo_.top().symbol};
3772       generic && generic->has<GenericDetails>()) {
3773     ResolveSpecificsInGeneric(*generic, false);
3774   }
3775 }
3776 
3777 // Mixed interfaces are allowed by the standard.
3778 // If there is a derived type with the same name, they must all be functions.
3779 void InterfaceVisitor::CheckGenericProcedures(Symbol &generic) {
3780   ResolveSpecificsInGeneric(generic, true);
3781   auto &details{generic.get<GenericDetails>()};
3782   if (auto *proc{details.CheckSpecific()}) {
3783     context().Warn(common::UsageWarning::HomonymousSpecific,
3784         proc->name().begin() > generic.name().begin() ? proc->name()
3785                                                       : generic.name(),
3786         "'%s' should not be the name of both a generic interface and a procedure unless it is a specific procedure of the generic"_warn_en_US,
3787         generic.name());
3788   }
3789   auto &specifics{details.specificProcs()};
3790   if (specifics.empty()) {
3791     if (details.derivedType()) {
3792       generic.set(Symbol::Flag::Function);
3793     }
3794     return;
3795   }
3796   const Symbol *function{nullptr};
3797   const Symbol *subroutine{nullptr};
3798   for (const Symbol &specific : specifics) {
3799     if (!function && specific.test(Symbol::Flag::Function)) {
3800       function = &specific;
3801     } else if (!subroutine && specific.test(Symbol::Flag::Subroutine)) {
3802       subroutine = &specific;
3803       if (details.derivedType() &&
3804           context().ShouldWarn(
3805               common::LanguageFeature::SubroutineAndFunctionSpecifics) &&
3806           !InModuleFile()) {
3807         SayDerivedType(generic.name(),
3808             "Generic interface '%s' should only contain functions due to derived type with same name"_warn_en_US,
3809             *details.derivedType()->GetUltimate().scope())
3810             .set_languageFeature(
3811                 common::LanguageFeature::SubroutineAndFunctionSpecifics);
3812       }
3813     }
3814     if (function && subroutine) { // F'2023 C1514
3815       if (auto *msg{context().Warn(
3816               common::LanguageFeature::SubroutineAndFunctionSpecifics,
3817               generic.name(),
3818               "Generic interface '%s' has both a function and a subroutine"_warn_en_US,
3819               generic.name())}) {
3820         msg->Attach(function->name(), "Function declaration"_en_US)
3821             .Attach(subroutine->name(), "Subroutine declaration"_en_US);
3822       }
3823       break;
3824     }
3825   }
3826   if (function && !subroutine) {
3827     generic.set(Symbol::Flag::Function);
3828   } else if (subroutine && !function) {
3829     generic.set(Symbol::Flag::Subroutine);
3830   }
3831 }
3832 
3833 // SubprogramVisitor implementation
3834 
3835 // Return false if it is actually an assignment statement.
3836 bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
3837   const auto &name{std::get<parser::Name>(x.t)};
3838   const DeclTypeSpec *resultType{nullptr};
3839   // Look up name: provides return type or tells us if it's an array
3840   if (auto *symbol{FindSymbol(name)}) {
3841     Symbol &ultimate{symbol->GetUltimate()};
3842     if (ultimate.has<ObjectEntityDetails>() ||
3843         ultimate.has<AssocEntityDetails>() ||
3844         CouldBeDataPointerValuedFunction(&ultimate) ||
3845         (&symbol->owner() == &currScope() && IsFunctionResult(*symbol))) {
3846       misparsedStmtFuncFound_ = true;
3847       return false;
3848     }
3849     if (IsHostAssociated(*symbol, currScope())) {
3850       context().Warn(common::LanguageFeature::StatementFunctionExtensions,
3851           name.source,
3852           "Name '%s' from host scope should have a type declaration before its local statement function definition"_port_en_US,
3853           name.source);
3854       MakeSymbol(name, Attrs{}, UnknownDetails{});
3855     } else if (auto *entity{ultimate.detailsIf<EntityDetails>()};
3856                entity && !ultimate.has<ProcEntityDetails>()) {
3857       resultType = entity->type();
3858       ultimate.details() = UnknownDetails{}; // will be replaced below
3859     } else {
3860       misparsedStmtFuncFound_ = true;
3861     }
3862   }
3863   if (misparsedStmtFuncFound_) {
3864     Say(name,
3865         "'%s' has not been declared as an array or pointer-valued function"_err_en_US);
3866     return false;
3867   }
3868   auto &symbol{PushSubprogramScope(name, Symbol::Flag::Function)};
3869   symbol.set(Symbol::Flag::StmtFunction);
3870   EraseSymbol(symbol); // removes symbol added by PushSubprogramScope
3871   auto &details{symbol.get<SubprogramDetails>()};
3872   for (const auto &dummyName : std::get<std::list<parser::Name>>(x.t)) {
3873     ObjectEntityDetails dummyDetails{true};
3874     if (auto *dummySymbol{FindInScope(currScope().parent(), dummyName)}) {
3875       if (auto *d{dummySymbol->GetType()}) {
3876         dummyDetails.set_type(*d);
3877       }
3878     }
3879     Symbol &dummy{MakeSymbol(dummyName, std::move(dummyDetails))};
3880     ApplyImplicitRules(dummy);
3881     details.add_dummyArg(dummy);
3882   }
3883   ObjectEntityDetails resultDetails;
3884   if (resultType) {
3885     resultDetails.set_type(*resultType);
3886   }
3887   resultDetails.set_funcResult(true);
3888   Symbol &result{MakeSymbol(name, std::move(resultDetails))};
3889   result.flags().set(Symbol::Flag::StmtFunction);
3890   ApplyImplicitRules(result);
3891   details.set_result(result);
3892   // The analysis of the expression that constitutes the body of the
3893   // statement function is deferred to FinishSpecificationPart() so that
3894   // all declarations and implicit typing are complete.
3895   PopScope();
3896   return true;
3897 }
3898 
3899 bool SubprogramVisitor::Pre(const parser::Suffix &suffix) {
3900   if (suffix.resultName) {
3901     if (IsFunction(currScope())) {
3902       if (FuncResultStack::FuncInfo * info{funcResultStack().Top()}) {
3903         if (info->inFunctionStmt) {
3904           info->resultName = &suffix.resultName.value();
3905         } else {
3906           // will check the result name in Post(EntryStmt)
3907         }
3908       }
3909     } else {
3910       Message &msg{Say(*suffix.resultName,
3911           "RESULT(%s) may appear only in a function"_err_en_US)};
3912       if (const Symbol * subprogram{InclusiveScope().symbol()}) {
3913         msg.Attach(subprogram->name(), "Containing subprogram"_en_US);
3914       }
3915     }
3916   }
3917   // LanguageBindingSpec deferred to Post(EntryStmt) or, for FunctionStmt,
3918   // all the way to EndSubprogram().
3919   return false;
3920 }
3921 
3922 bool SubprogramVisitor::Pre(const parser::PrefixSpec &x) {
3923   // Save this to process after UseStmt and ImplicitPart
3924   if (const auto *parsedType{std::get_if<parser::DeclarationTypeSpec>(&x.u)}) {
3925     if (FuncResultStack::FuncInfo * info{funcResultStack().Top()}) {
3926       if (info->parsedType) { // C1543
3927         Say(currStmtSource().value_or(info->source),
3928             "FUNCTION prefix cannot specify the type more than once"_err_en_US);
3929       } else {
3930         info->parsedType = parsedType;
3931         if (auto at{currStmtSource()}) {
3932           info->source = *at;
3933         }
3934       }
3935     } else {
3936       Say(currStmtSource().value(),
3937           "SUBROUTINE prefix cannot specify a type"_err_en_US);
3938     }
3939     return false;
3940   } else {
3941     return true;
3942   }
3943 }
3944 
3945 bool SubprogramVisitor::Pre(const parser::PrefixSpec::Attributes &attrs) {
3946   if (auto *subp{currScope().symbol()
3947               ? currScope().symbol()->detailsIf<SubprogramDetails>()
3948               : nullptr}) {
3949     for (auto attr : attrs.v) {
3950       if (auto current{subp->cudaSubprogramAttrs()}) {
3951         if (attr == *current ||
3952             (*current == common::CUDASubprogramAttrs::HostDevice &&
3953                 (attr == common::CUDASubprogramAttrs::Host ||
3954                     attr == common::CUDASubprogramAttrs::Device))) {
3955           context().Warn(common::LanguageFeature::RedundantAttribute,
3956               currStmtSource().value(),
3957               "ATTRIBUTES(%s) appears more than once"_warn_en_US,
3958               common::EnumToString(attr));
3959         } else if ((attr == common::CUDASubprogramAttrs::Host ||
3960                        attr == common::CUDASubprogramAttrs::Device) &&
3961             (*current == common::CUDASubprogramAttrs::Host ||
3962                 *current == common::CUDASubprogramAttrs::Device ||
3963                 *current == common::CUDASubprogramAttrs::HostDevice)) {
3964           // HOST,DEVICE or DEVICE,HOST -> HostDevice
3965           subp->set_cudaSubprogramAttrs(
3966               common::CUDASubprogramAttrs::HostDevice);
3967         } else {
3968           Say(currStmtSource().value(),
3969               "ATTRIBUTES(%s) conflicts with earlier ATTRIBUTES(%s)"_err_en_US,
3970               common::EnumToString(attr), common::EnumToString(*current));
3971         }
3972       } else {
3973         subp->set_cudaSubprogramAttrs(attr);
3974       }
3975     }
3976     if (auto attrs{subp->cudaSubprogramAttrs()}) {
3977       if (*attrs == common::CUDASubprogramAttrs::Global ||
3978           *attrs == common::CUDASubprogramAttrs::Device) {
3979         const Scope &scope{currScope()};
3980         const Scope *mod{FindModuleContaining(scope)};
3981         if (mod && mod->GetName().value() == "cudadevice") {
3982           return false;
3983         }
3984         // Implicitly USE the cudadevice module by copying its symbols in the
3985         // current scope.
3986         const Scope &cudaDeviceScope{context().GetCUDADeviceScope()};
3987         for (auto sym : cudaDeviceScope.GetSymbols()) {
3988           if (!currScope().FindSymbol(sym->name())) {
3989             auto &localSymbol{MakeSymbol(
3990                 sym->name(), Attrs{}, UseDetails{sym->name(), *sym})};
3991             localSymbol.flags() = sym->flags();
3992           }
3993         }
3994       }
3995     }
3996   }
3997   return false;
3998 }
3999 
4000 void SubprogramVisitor::Post(const parser::PrefixSpec::Launch_Bounds &x) {
4001   std::vector<std::int64_t> bounds;
4002   bool ok{true};
4003   for (const auto &sicx : x.v) {
4004     if (auto value{evaluate::ToInt64(EvaluateExpr(sicx))}) {
4005       bounds.push_back(*value);
4006     } else {
4007       ok = false;
4008     }
4009   }
4010   if (!ok || bounds.size() < 2 || bounds.size() > 3) {
4011     Say(currStmtSource().value(),
4012         "Operands of LAUNCH_BOUNDS() must be 2 or 3 integer constants"_err_en_US);
4013   } else if (auto *subp{currScope().symbol()
4014                      ? currScope().symbol()->detailsIf<SubprogramDetails>()
4015                      : nullptr}) {
4016     if (subp->cudaLaunchBounds().empty()) {
4017       subp->set_cudaLaunchBounds(std::move(bounds));
4018     } else {
4019       Say(currStmtSource().value(),
4020           "LAUNCH_BOUNDS() may only appear once"_err_en_US);
4021     }
4022   }
4023 }
4024 
4025 void SubprogramVisitor::Post(const parser::PrefixSpec::Cluster_Dims &x) {
4026   std::vector<std::int64_t> dims;
4027   bool ok{true};
4028   for (const auto &sicx : x.v) {
4029     if (auto value{evaluate::ToInt64(EvaluateExpr(sicx))}) {
4030       dims.push_back(*value);
4031     } else {
4032       ok = false;
4033     }
4034   }
4035   if (!ok || dims.size() != 3) {
4036     Say(currStmtSource().value(),
4037         "Operands of CLUSTER_DIMS() must be three integer constants"_err_en_US);
4038   } else if (auto *subp{currScope().symbol()
4039                      ? currScope().symbol()->detailsIf<SubprogramDetails>()
4040                      : nullptr}) {
4041     if (subp->cudaClusterDims().empty()) {
4042       subp->set_cudaClusterDims(std::move(dims));
4043     } else {
4044       Say(currStmtSource().value(),
4045           "CLUSTER_DIMS() may only appear once"_err_en_US);
4046     }
4047   }
4048 }
4049 
4050 static bool HasModulePrefix(const std::list<parser::PrefixSpec> &prefixes) {
4051   for (const auto &prefix : prefixes) {
4052     if (std::holds_alternative<parser::PrefixSpec::Module>(prefix.u)) {
4053       return true;
4054     }
4055   }
4056   return false;
4057 }
4058 
4059 bool SubprogramVisitor::Pre(const parser::InterfaceBody::Subroutine &x) {
4060   const auto &stmtTuple{
4061       std::get<parser::Statement<parser::SubroutineStmt>>(x.t).statement.t};
4062   return BeginSubprogram(std::get<parser::Name>(stmtTuple),
4063       Symbol::Flag::Subroutine,
4064       HasModulePrefix(std::get<std::list<parser::PrefixSpec>>(stmtTuple)));
4065 }
4066 void SubprogramVisitor::Post(const parser::InterfaceBody::Subroutine &x) {
4067   const auto &stmt{std::get<parser::Statement<parser::SubroutineStmt>>(x.t)};
4068   EndSubprogram(stmt.source,
4069       &std::get<std::optional<parser::LanguageBindingSpec>>(stmt.statement.t));
4070 }
4071 bool SubprogramVisitor::Pre(const parser::InterfaceBody::Function &x) {
4072   const auto &stmtTuple{
4073       std::get<parser::Statement<parser::FunctionStmt>>(x.t).statement.t};
4074   return BeginSubprogram(std::get<parser::Name>(stmtTuple),
4075       Symbol::Flag::Function,
4076       HasModulePrefix(std::get<std::list<parser::PrefixSpec>>(stmtTuple)));
4077 }
4078 void SubprogramVisitor::Post(const parser::InterfaceBody::Function &x) {
4079   const auto &stmt{std::get<parser::Statement<parser::FunctionStmt>>(x.t)};
4080   const auto &maybeSuffix{
4081       std::get<std::optional<parser::Suffix>>(stmt.statement.t)};
4082   EndSubprogram(stmt.source, maybeSuffix ? &maybeSuffix->binding : nullptr);
4083 }
4084 
4085 bool SubprogramVisitor::Pre(const parser::SubroutineStmt &stmt) {
4086   BeginAttrs();
4087   Walk(std::get<std::list<parser::PrefixSpec>>(stmt.t));
4088   Walk(std::get<parser::Name>(stmt.t));
4089   Walk(std::get<std::list<parser::DummyArg>>(stmt.t));
4090   // Don't traverse the LanguageBindingSpec now; it's deferred to EndSubprogram.
4091   Symbol &symbol{PostSubprogramStmt()};
4092   SubprogramDetails &details{symbol.get<SubprogramDetails>()};
4093   for (const auto &dummyArg : std::get<std::list<parser::DummyArg>>(stmt.t)) {
4094     if (const auto *dummyName{std::get_if<parser::Name>(&dummyArg.u)}) {
4095       CreateDummyArgument(details, *dummyName);
4096     } else {
4097       details.add_alternateReturn();
4098     }
4099   }
4100   return false;
4101 }
4102 bool SubprogramVisitor::Pre(const parser::FunctionStmt &) {
4103   FuncResultStack::FuncInfo &info{DEREF(funcResultStack().Top())};
4104   CHECK(!info.inFunctionStmt);
4105   info.inFunctionStmt = true;
4106   if (auto at{currStmtSource()}) {
4107     info.source = *at;
4108   }
4109   return BeginAttrs();
4110 }
4111 bool SubprogramVisitor::Pre(const parser::EntryStmt &) { return BeginAttrs(); }
4112 
4113 void SubprogramVisitor::Post(const parser::FunctionStmt &stmt) {
4114   const auto &name{std::get<parser::Name>(stmt.t)};
4115   Symbol &symbol{PostSubprogramStmt()};
4116   SubprogramDetails &details{symbol.get<SubprogramDetails>()};
4117   for (const auto &dummyName : std::get<std::list<parser::Name>>(stmt.t)) {
4118     CreateDummyArgument(details, dummyName);
4119   }
4120   const parser::Name *funcResultName;
4121   FuncResultStack::FuncInfo &info{DEREF(funcResultStack().Top())};
4122   CHECK(info.inFunctionStmt);
4123   info.inFunctionStmt = false;
4124   bool distinctResultName{
4125       info.resultName && info.resultName->source != name.source};
4126   if (distinctResultName) {
4127     // Note that RESULT is ignored if it has the same name as the function.
4128     // The symbol created by PushScope() is retained as a place-holder
4129     // for error detection.
4130     funcResultName = info.resultName;
4131   } else {
4132     EraseSymbol(name); // was added by PushScope()
4133     funcResultName = &name;
4134   }
4135   if (details.isFunction()) {
4136     CHECK(context().HasError(currScope().symbol()));
4137   } else {
4138     // RESULT(x) can be the same explicitly-named RESULT(x) as an ENTRY
4139     // statement.
4140     Symbol *result{nullptr};
4141     if (distinctResultName) {
4142       if (auto iter{currScope().find(funcResultName->source)};
4143           iter != currScope().end()) {
4144         Symbol &entryResult{*iter->second};
4145         if (IsFunctionResult(entryResult)) {
4146           result = &entryResult;
4147         }
4148       }
4149     }
4150     if (result) {
4151       Resolve(*funcResultName, *result);
4152     } else {
4153       // add function result to function scope
4154       EntityDetails funcResultDetails;
4155       funcResultDetails.set_funcResult(true);
4156       result = &MakeSymbol(*funcResultName, std::move(funcResultDetails));
4157     }
4158     info.resultSymbol = result;
4159     details.set_result(*result);
4160   }
4161   // C1560.
4162   if (info.resultName && !distinctResultName) {
4163     context().Warn(common::UsageWarning::HomonymousResult,
4164         info.resultName->source,
4165         "The function name should not appear in RESULT; references to '%s' "
4166         "inside the function will be considered as references to the "
4167         "result only"_warn_en_US,
4168         name.source);
4169     // RESULT name was ignored above, the only side effect from doing so will be
4170     // the inability to make recursive calls. The related parser::Name is still
4171     // resolved to the created function result symbol because every parser::Name
4172     // should be resolved to avoid internal errors.
4173     Resolve(*info.resultName, info.resultSymbol);
4174   }
4175   name.symbol = &symbol; // must not be function result symbol
4176   // Clear the RESULT() name now in case an ENTRY statement in the implicit-part
4177   // has a RESULT() suffix.
4178   info.resultName = nullptr;
4179 }
4180 
4181 Symbol &SubprogramVisitor::PostSubprogramStmt() {
4182   Symbol &symbol{*currScope().symbol()};
4183   SetExplicitAttrs(symbol, EndAttrs());
4184   if (symbol.attrs().test(Attr::MODULE)) {
4185     symbol.attrs().set(Attr::EXTERNAL, false);
4186     symbol.implicitAttrs().set(Attr::EXTERNAL, false);
4187   }
4188   return symbol;
4189 }
4190 
4191 void SubprogramVisitor::Post(const parser::EntryStmt &stmt) {
4192   if (const auto &suffix{std::get<std::optional<parser::Suffix>>(stmt.t)}) {
4193     Walk(suffix->binding);
4194   }
4195   PostEntryStmt(stmt);
4196   EndAttrs();
4197 }
4198 
4199 void SubprogramVisitor::CreateDummyArgument(
4200     SubprogramDetails &details, const parser::Name &name) {
4201   Symbol *dummy{FindInScope(name)};
4202   if (dummy) {
4203     if (IsDummy(*dummy)) {
4204       if (dummy->test(Symbol::Flag::EntryDummyArgument)) {
4205         dummy->set(Symbol::Flag::EntryDummyArgument, false);
4206       } else {
4207         Say(name,
4208             "'%s' appears more than once as a dummy argument name in this subprogram"_err_en_US,
4209             name.source);
4210         return;
4211       }
4212     } else {
4213       SayWithDecl(name, *dummy,
4214           "'%s' may not appear as a dummy argument name in this subprogram"_err_en_US);
4215       return;
4216     }
4217   } else {
4218     dummy = &MakeSymbol(name, EntityDetails{true});
4219   }
4220   details.add_dummyArg(DEREF(dummy));
4221 }
4222 
4223 void SubprogramVisitor::CreateEntry(
4224     const parser::EntryStmt &stmt, Symbol &subprogram) {
4225   const auto &entryName{std::get<parser::Name>(stmt.t)};
4226   Scope &outer{currScope().parent()};
4227   Symbol::Flag subpFlag{subprogram.test(Symbol::Flag::Function)
4228           ? Symbol::Flag::Function
4229           : Symbol::Flag::Subroutine};
4230   Attrs attrs;
4231   const auto &suffix{std::get<std::optional<parser::Suffix>>(stmt.t)};
4232   bool hasGlobalBindingName{outer.IsGlobal() && suffix && suffix->binding &&
4233       std::get<std::optional<parser::ScalarDefaultCharConstantExpr>>(
4234           suffix->binding->t)
4235           .has_value()};
4236   if (!hasGlobalBindingName) {
4237     if (Symbol * extant{FindSymbol(outer, entryName)}) {
4238       if (!HandlePreviousCalls(entryName, *extant, subpFlag)) {
4239         if (outer.IsTopLevel()) {
4240           Say2(entryName,
4241               "'%s' is already defined as a global identifier"_err_en_US,
4242               *extant, "Previous definition of '%s'"_en_US);
4243         } else {
4244           SayAlreadyDeclared(entryName, *extant);
4245         }
4246         return;
4247       }
4248       attrs = extant->attrs();
4249     }
4250   }
4251   std::optional<SourceName> distinctResultName;
4252   if (suffix && suffix->resultName &&
4253       suffix->resultName->source != entryName.source) {
4254     distinctResultName = suffix->resultName->source;
4255   }
4256   if (outer.IsModule() && !attrs.test(Attr::PRIVATE)) {
4257     attrs.set(Attr::PUBLIC);
4258   }
4259   Symbol *entrySymbol{nullptr};
4260   if (hasGlobalBindingName) {
4261     // Hide the entry's symbol in a new anonymous global scope so
4262     // that its name doesn't clash with anything.
4263     Symbol &symbol{MakeSymbol(outer, context().GetTempName(outer), Attrs{})};
4264     symbol.set_details(MiscDetails{MiscDetails::Kind::ScopeName});
4265     Scope &hidden{outer.MakeScope(Scope::Kind::Global, &symbol)};
4266     entrySymbol = &MakeSymbol(hidden, entryName.source, attrs);
4267   } else {
4268     entrySymbol = FindInScope(outer, entryName.source);
4269     if (entrySymbol) {
4270       if (auto *generic{entrySymbol->detailsIf<GenericDetails>()}) {
4271         if (auto *specific{generic->specific()}) {
4272           // Forward reference to ENTRY from a generic interface
4273           entrySymbol = specific;
4274           CheckDuplicatedAttrs(entryName.source, *entrySymbol, attrs);
4275           SetExplicitAttrs(*entrySymbol, attrs);
4276         }
4277       }
4278     } else {
4279       entrySymbol = &MakeSymbol(outer, entryName.source, attrs);
4280     }
4281   }
4282   SubprogramDetails entryDetails;
4283   entryDetails.set_entryScope(currScope());
4284   entrySymbol->set(subpFlag);
4285   if (subpFlag == Symbol::Flag::Function) {
4286     Symbol *result{nullptr};
4287     EntityDetails resultDetails;
4288     resultDetails.set_funcResult(true);
4289     if (distinctResultName) {
4290       // An explicit RESULT() can also be an explicit RESULT()
4291       // of the function or another ENTRY.
4292       if (auto iter{currScope().find(suffix->resultName->source)};
4293           iter != currScope().end()) {
4294         result = &*iter->second;
4295       }
4296       if (!result) {
4297         result =
4298             &MakeSymbol(*distinctResultName, Attrs{}, std::move(resultDetails));
4299       } else if (!result->has<EntityDetails>()) {
4300         Say(*distinctResultName,
4301             "ENTRY cannot have RESULT(%s) that is not a variable"_err_en_US,
4302             *distinctResultName)
4303             .Attach(result->name(), "Existing declaration of '%s'"_en_US,
4304                 result->name());
4305         result = nullptr;
4306       }
4307       if (result) {
4308         Resolve(*suffix->resultName, *result);
4309       }
4310     } else {
4311       result = &MakeSymbol(entryName.source, Attrs{}, std::move(resultDetails));
4312     }
4313     if (result) {
4314       entryDetails.set_result(*result);
4315     }
4316   }
4317   if (subpFlag == Symbol::Flag::Subroutine || distinctResultName) {
4318     Symbol &assoc{MakeSymbol(entryName.source)};
4319     assoc.set_details(HostAssocDetails{*entrySymbol});
4320     assoc.set(Symbol::Flag::Subroutine);
4321   }
4322   Resolve(entryName, *entrySymbol);
4323   std::set<SourceName> dummies;
4324   for (const auto &dummyArg : std::get<std::list<parser::DummyArg>>(stmt.t)) {
4325     if (const auto *dummyName{std::get_if<parser::Name>(&dummyArg.u)}) {
4326       auto pair{dummies.insert(dummyName->source)};
4327       if (!pair.second) {
4328         Say(*dummyName,
4329             "'%s' appears more than once as a dummy argument name in this ENTRY statement"_err_en_US,
4330             dummyName->source);
4331         continue;
4332       }
4333       Symbol *dummy{FindInScope(*dummyName)};
4334       if (dummy) {
4335         if (!IsDummy(*dummy)) {
4336           evaluate::AttachDeclaration(
4337               Say(*dummyName,
4338                   "'%s' may not appear as a dummy argument name in this ENTRY statement"_err_en_US,
4339                   dummyName->source),
4340               *dummy);
4341           continue;
4342         }
4343       } else {
4344         dummy = &MakeSymbol(*dummyName, EntityDetails{true});
4345         dummy->set(Symbol::Flag::EntryDummyArgument);
4346       }
4347       entryDetails.add_dummyArg(DEREF(dummy));
4348     } else if (subpFlag == Symbol::Flag::Function) { // C1573
4349       Say(entryName,
4350           "ENTRY in a function may not have an alternate return dummy argument"_err_en_US);
4351       break;
4352     } else {
4353       entryDetails.add_alternateReturn();
4354     }
4355   }
4356   entrySymbol->set_details(std::move(entryDetails));
4357 }
4358 
4359 void SubprogramVisitor::PostEntryStmt(const parser::EntryStmt &stmt) {
4360   // The entry symbol should have already been created and resolved
4361   // in CreateEntry(), called by BeginSubprogram(), with one exception (below).
4362   const auto &name{std::get<parser::Name>(stmt.t)};
4363   Scope &inclusiveScope{InclusiveScope()};
4364   if (!name.symbol) {
4365     if (inclusiveScope.kind() != Scope::Kind::Subprogram) {
4366       Say(name.source,
4367           "ENTRY '%s' may appear only in a subroutine or function"_err_en_US,
4368           name.source);
4369     } else if (FindSeparateModuleSubprogramInterface(inclusiveScope.symbol())) {
4370       Say(name.source,
4371           "ENTRY '%s' may not appear in a separate module procedure"_err_en_US,
4372           name.source);
4373     } else {
4374       // C1571 - entry is nested, so was not put into the program tree; error
4375       // is emitted from MiscChecker in semantics.cpp.
4376     }
4377     return;
4378   }
4379   Symbol &entrySymbol{*name.symbol};
4380   if (context().HasError(entrySymbol)) {
4381     return;
4382   }
4383   if (!entrySymbol.has<SubprogramDetails>()) {
4384     SayAlreadyDeclared(name, entrySymbol);
4385     return;
4386   }
4387   SubprogramDetails &entryDetails{entrySymbol.get<SubprogramDetails>()};
4388   CHECK(entryDetails.entryScope() == &inclusiveScope);
4389   SetCUDADataAttr(name.source, entrySymbol, cudaDataAttr());
4390   entrySymbol.attrs() |= GetAttrs();
4391   SetBindNameOn(entrySymbol);
4392   for (const auto &dummyArg : std::get<std::list<parser::DummyArg>>(stmt.t)) {
4393     if (const auto *dummyName{std::get_if<parser::Name>(&dummyArg.u)}) {
4394       if (Symbol * dummy{FindInScope(*dummyName)}) {
4395         if (dummy->test(Symbol::Flag::EntryDummyArgument)) {
4396           const auto *subp{dummy->detailsIf<SubprogramDetails>()};
4397           if (subp && subp->isInterface()) { // ok
4398           } else if (!dummy->has<EntityDetails>() &&
4399               !dummy->has<ObjectEntityDetails>() &&
4400               !dummy->has<ProcEntityDetails>()) {
4401             SayWithDecl(*dummyName, *dummy,
4402                 "ENTRY dummy argument '%s' was previously declared as an item that may not be used as a dummy argument"_err_en_US);
4403           }
4404           dummy->set(Symbol::Flag::EntryDummyArgument, false);
4405         }
4406       }
4407     }
4408   }
4409 }
4410 
4411 Symbol *ScopeHandler::FindSeparateModuleProcedureInterface(
4412     const parser::Name &name) {
4413   auto *symbol{FindSymbol(name)};
4414   if (symbol && symbol->has<SubprogramNameDetails>()) {
4415     const Scope *parent{nullptr};
4416     if (currScope().IsSubmodule()) {
4417       parent = currScope().symbol()->get<ModuleDetails>().parent();
4418     }
4419     symbol = parent ? FindSymbol(*parent, name) : nullptr;
4420   }
4421   if (symbol) {
4422     if (auto *generic{symbol->detailsIf<GenericDetails>()}) {
4423       symbol = generic->specific();
4424     }
4425   }
4426   if (const Symbol * defnIface{FindSeparateModuleSubprogramInterface(symbol)}) {
4427     // Error recovery in case of multiple definitions
4428     symbol = const_cast<Symbol *>(defnIface);
4429   }
4430   if (!IsSeparateModuleProcedureInterface(symbol)) {
4431     Say(name, "'%s' was not declared a separate module procedure"_err_en_US);
4432     symbol = nullptr;
4433   }
4434   return symbol;
4435 }
4436 
4437 // A subprogram declared with MODULE PROCEDURE
4438 bool SubprogramVisitor::BeginMpSubprogram(const parser::Name &name) {
4439   Symbol *symbol{FindSeparateModuleProcedureInterface(name)};
4440   if (!symbol) {
4441     return false;
4442   }
4443   if (symbol->owner() == currScope() && symbol->scope()) {
4444     // This is a MODULE PROCEDURE whose interface appears in its host.
4445     // Convert the module procedure's interface into a subprogram.
4446     SetScope(DEREF(symbol->scope()));
4447     symbol->get<SubprogramDetails>().set_isInterface(false);
4448     name.symbol = symbol;
4449   } else {
4450     // Copy the interface into a new subprogram scope.
4451     EraseSymbol(name);
4452     Symbol &newSymbol{MakeSymbol(name, SubprogramDetails{})};
4453     PushScope(Scope::Kind::Subprogram, &newSymbol);
4454     auto &newSubprogram{newSymbol.get<SubprogramDetails>()};
4455     newSubprogram.set_moduleInterface(*symbol);
4456     auto &subprogram{symbol->get<SubprogramDetails>()};
4457     if (const auto *name{subprogram.bindName()}) {
4458       newSubprogram.set_bindName(std::string{*name});
4459     }
4460     newSymbol.attrs() |= symbol->attrs();
4461     newSymbol.set(symbol->test(Symbol::Flag::Subroutine)
4462             ? Symbol::Flag::Subroutine
4463             : Symbol::Flag::Function);
4464     MapSubprogramToNewSymbols(*symbol, newSymbol, currScope());
4465   }
4466   return true;
4467 }
4468 
4469 // A subprogram or interface declared with SUBROUTINE or FUNCTION
4470 bool SubprogramVisitor::BeginSubprogram(const parser::Name &name,
4471     Symbol::Flag subpFlag, bool hasModulePrefix,
4472     const parser::LanguageBindingSpec *bindingSpec,
4473     const ProgramTree::EntryStmtList *entryStmts) {
4474   bool isValid{true};
4475   if (hasModulePrefix && !currScope().IsModule() &&
4476       !currScope().IsSubmodule()) { // C1547
4477     Say(name,
4478         "'%s' is a MODULE procedure which must be declared within a "
4479         "MODULE or SUBMODULE"_err_en_US);
4480     // Don't return here because it can be useful to have the scope set for
4481     // other semantic checks run before we print the errors
4482     isValid = false;
4483   }
4484   Symbol *moduleInterface{nullptr};
4485   if (isValid && hasModulePrefix && !inInterfaceBlock()) {
4486     moduleInterface = FindSeparateModuleProcedureInterface(name);
4487     if (moduleInterface && &moduleInterface->owner() == &currScope()) {
4488       // Subprogram is MODULE FUNCTION or MODULE SUBROUTINE with an interface
4489       // previously defined in the same scope.
4490       if (GenericDetails *
4491           generic{DEREF(FindSymbol(name)).detailsIf<GenericDetails>()}) {
4492         generic->clear_specific();
4493         name.symbol = nullptr;
4494       } else {
4495         EraseSymbol(name);
4496       }
4497     }
4498   }
4499   Symbol &newSymbol{
4500       PushSubprogramScope(name, subpFlag, bindingSpec, hasModulePrefix)};
4501   if (moduleInterface) {
4502     newSymbol.get<SubprogramDetails>().set_moduleInterface(*moduleInterface);
4503     if (moduleInterface->attrs().test(Attr::PRIVATE)) {
4504       SetImplicitAttr(newSymbol, Attr::PRIVATE);
4505     } else if (moduleInterface->attrs().test(Attr::PUBLIC)) {
4506       SetImplicitAttr(newSymbol, Attr::PUBLIC);
4507     }
4508   }
4509   if (entryStmts) {
4510     for (const auto &ref : *entryStmts) {
4511       CreateEntry(*ref, newSymbol);
4512     }
4513   }
4514   return true;
4515 }
4516 
4517 void SubprogramVisitor::HandleLanguageBinding(Symbol *symbol,
4518     std::optional<parser::CharBlock> stmtSource,
4519     const std::optional<parser::LanguageBindingSpec> *binding) {
4520   if (binding && *binding && symbol) {
4521     // Finally process the BIND(C,NAME=name) now that symbols in the name
4522     // expression will resolve to local names if needed.
4523     auto flagRestorer{common::ScopedSet(inSpecificationPart_, false)};
4524     auto originalStmtSource{messageHandler().currStmtSource()};
4525     messageHandler().set_currStmtSource(stmtSource);
4526     BeginAttrs();
4527     Walk(**binding);
4528     SetBindNameOn(*symbol);
4529     symbol->attrs() |= EndAttrs();
4530     messageHandler().set_currStmtSource(originalStmtSource);
4531   }
4532 }
4533 
4534 void SubprogramVisitor::EndSubprogram(
4535     std::optional<parser::CharBlock> stmtSource,
4536     const std::optional<parser::LanguageBindingSpec> *binding,
4537     const ProgramTree::EntryStmtList *entryStmts) {
4538   HandleLanguageBinding(currScope().symbol(), stmtSource, binding);
4539   if (entryStmts) {
4540     for (const auto &ref : *entryStmts) {
4541       const parser::EntryStmt &entryStmt{*ref};
4542       if (const auto &suffix{
4543               std::get<std::optional<parser::Suffix>>(entryStmt.t)}) {
4544         const auto &name{std::get<parser::Name>(entryStmt.t)};
4545         HandleLanguageBinding(name.symbol, name.source, &suffix->binding);
4546       }
4547     }
4548   }
4549   if (inInterfaceBlock() && currScope().symbol()) {
4550     DeclaredPossibleSpecificProc(*currScope().symbol());
4551   }
4552   PopScope();
4553 }
4554 
4555 bool SubprogramVisitor::HandlePreviousCalls(
4556     const parser::Name &name, Symbol &symbol, Symbol::Flag subpFlag) {
4557   // If the extant symbol is a generic, check its homonymous specific
4558   // procedure instead if it has one.
4559   if (auto *generic{symbol.detailsIf<GenericDetails>()}) {
4560     return generic->specific() &&
4561         HandlePreviousCalls(name, *generic->specific(), subpFlag);
4562   } else if (const auto *proc{symbol.detailsIf<ProcEntityDetails>()}; proc &&
4563              !proc->isDummy() &&
4564              !symbol.attrs().HasAny(Attrs{Attr::INTRINSIC, Attr::POINTER})) {
4565     // There's a symbol created for previous calls to this subprogram or
4566     // ENTRY's name.  We have to replace that symbol in situ to avoid the
4567     // obligation to rewrite symbol pointers in the parse tree.
4568     if (!symbol.test(subpFlag)) {
4569       auto other{subpFlag == Symbol::Flag::Subroutine
4570               ? Symbol::Flag::Function
4571               : Symbol::Flag::Subroutine};
4572       // External statements issue an explicit EXTERNAL attribute.
4573       if (symbol.attrs().test(Attr::EXTERNAL) &&
4574           !symbol.implicitAttrs().test(Attr::EXTERNAL)) {
4575         // Warn if external statement previously declared.
4576         context().Warn(common::LanguageFeature::RedundantAttribute, name.source,
4577             "EXTERNAL attribute was already specified on '%s'"_warn_en_US,
4578             name.source);
4579       } else if (symbol.test(other)) {
4580         Say2(name,
4581             subpFlag == Symbol::Flag::Function
4582                 ? "'%s' was previously called as a subroutine"_err_en_US
4583                 : "'%s' was previously called as a function"_err_en_US,
4584             symbol, "Previous call of '%s'"_en_US);
4585       } else {
4586         symbol.set(subpFlag);
4587       }
4588     }
4589     EntityDetails entity;
4590     if (proc->type()) {
4591       entity.set_type(*proc->type());
4592     }
4593     symbol.details() = std::move(entity);
4594     return true;
4595   } else {
4596     return symbol.has<UnknownDetails>() || symbol.has<SubprogramNameDetails>();
4597   }
4598 }
4599 
4600 void SubprogramVisitor::CheckExtantProc(
4601     const parser::Name &name, Symbol::Flag subpFlag) {
4602   if (auto *prev{FindSymbol(name)}) {
4603     if (IsDummy(*prev)) {
4604     } else if (auto *entity{prev->detailsIf<EntityDetails>()};
4605                IsPointer(*prev) && entity && !entity->type()) {
4606       // POINTER attribute set before interface
4607     } else if (inInterfaceBlock() && currScope() != prev->owner()) {
4608       // Procedures in an INTERFACE block do not resolve to symbols
4609       // in scopes between the global scope and the current scope.
4610     } else if (!HandlePreviousCalls(name, *prev, subpFlag)) {
4611       SayAlreadyDeclared(name, *prev);
4612     }
4613   }
4614 }
4615 
4616 Symbol &SubprogramVisitor::PushSubprogramScope(const parser::Name &name,
4617     Symbol::Flag subpFlag, const parser::LanguageBindingSpec *bindingSpec,
4618     bool hasModulePrefix) {
4619   Symbol *symbol{GetSpecificFromGeneric(name)};
4620   if (!symbol) {
4621     if (bindingSpec && currScope().IsGlobal() &&
4622         std::get<std::optional<parser::ScalarDefaultCharConstantExpr>>(
4623             bindingSpec->t)
4624             .has_value()) {
4625       // Create this new top-level subprogram with a binding label
4626       // in a new global scope, so that its symbol's name won't clash
4627       // with another symbol that has a distinct binding label.
4628       PushScope(Scope::Kind::Global,
4629           &MakeSymbol(context().GetTempName(currScope()), Attrs{},
4630               MiscDetails{MiscDetails::Kind::ScopeName}));
4631     }
4632     CheckExtantProc(name, subpFlag);
4633     symbol = &MakeSymbol(name, SubprogramDetails{});
4634   }
4635   symbol->ReplaceName(name.source);
4636   symbol->set(subpFlag);
4637   PushScope(Scope::Kind::Subprogram, symbol);
4638   if (subpFlag == Symbol::Flag::Function) {
4639     funcResultStack().Push(currScope(), name.source);
4640   }
4641   if (inInterfaceBlock()) {
4642     auto &details{symbol->get<SubprogramDetails>()};
4643     details.set_isInterface();
4644     if (isAbstract()) {
4645       SetExplicitAttr(*symbol, Attr::ABSTRACT);
4646     } else if (hasModulePrefix) {
4647       SetExplicitAttr(*symbol, Attr::MODULE);
4648     } else {
4649       MakeExternal(*symbol);
4650     }
4651     if (isGeneric()) {
4652       Symbol &genericSymbol{GetGenericSymbol()};
4653       if (auto *details{genericSymbol.detailsIf<GenericDetails>()}) {
4654         details->AddSpecificProc(*symbol, name.source);
4655       } else {
4656         CHECK(context().HasError(genericSymbol));
4657       }
4658     }
4659     set_inheritFromParent(false); // interfaces don't inherit, even if MODULE
4660   }
4661   if (Symbol * found{FindSymbol(name)};
4662       found && found->has<HostAssocDetails>()) {
4663     found->set(subpFlag); // PushScope() created symbol
4664   }
4665   return *symbol;
4666 }
4667 
4668 void SubprogramVisitor::PushBlockDataScope(const parser::Name &name) {
4669   if (auto *prev{FindSymbol(name)}) {
4670     if (prev->attrs().test(Attr::EXTERNAL) && prev->has<ProcEntityDetails>()) {
4671       if (prev->test(Symbol::Flag::Subroutine) ||
4672           prev->test(Symbol::Flag::Function)) {
4673         Say2(name, "BLOCK DATA '%s' has been called"_err_en_US, *prev,
4674             "Previous call of '%s'"_en_US);
4675       }
4676       EraseSymbol(name);
4677     }
4678   }
4679   if (name.source.empty()) {
4680     // Don't let unnamed BLOCK DATA conflict with unnamed PROGRAM
4681     PushScope(Scope::Kind::BlockData, nullptr);
4682   } else {
4683     PushScope(Scope::Kind::BlockData, &MakeSymbol(name, SubprogramDetails{}));
4684   }
4685 }
4686 
4687 // If name is a generic, return specific subprogram with the same name.
4688 Symbol *SubprogramVisitor::GetSpecificFromGeneric(const parser::Name &name) {
4689   // Search for the name but don't resolve it
4690   if (auto *symbol{currScope().FindSymbol(name.source)}) {
4691     if (symbol->has<SubprogramNameDetails>()) {
4692       if (inInterfaceBlock()) {
4693         // Subtle: clear any MODULE flag so that the new interface
4694         // symbol doesn't inherit it and ruin the ability to check it.
4695         symbol->attrs().reset(Attr::MODULE);
4696       }
4697     } else if (auto *details{symbol->detailsIf<GenericDetails>()}) {
4698       // found generic, want specific procedure
4699       auto *specific{details->specific()};
4700       Attrs moduleAttr;
4701       if (inInterfaceBlock()) {
4702         if (specific) {
4703           // Defining an interface in a generic of the same name which is
4704           // already shadowing another procedure.  In some cases, the shadowed
4705           // procedure is about to be replaced.
4706           if (specific->has<SubprogramNameDetails>() &&
4707               specific->attrs().test(Attr::MODULE)) {
4708             // The shadowed procedure is a separate module procedure that is
4709             // actually defined later in this (sub)module.
4710             // Define its interface now as a new symbol.
4711             moduleAttr.set(Attr::MODULE);
4712             specific = nullptr;
4713           } else if (&specific->owner() != &symbol->owner()) {
4714             // The shadowed procedure was from an enclosing scope and will be
4715             // overridden by this interface definition.
4716             specific = nullptr;
4717           }
4718           if (!specific) {
4719             details->clear_specific();
4720           }
4721         } else if (const auto *dType{details->derivedType()}) {
4722           if (&dType->owner() != &symbol->owner()) {
4723             // The shadowed derived type was from an enclosing scope and
4724             // will be overridden by this interface definition.
4725             details->clear_derivedType();
4726           }
4727         }
4728       }
4729       if (!specific) {
4730         specific = &currScope().MakeSymbol(
4731             name.source, std::move(moduleAttr), SubprogramDetails{});
4732         if (details->derivedType()) {
4733           // A specific procedure with the same name as a derived type
4734           SayAlreadyDeclared(name, *details->derivedType());
4735         } else {
4736           details->set_specific(Resolve(name, *specific));
4737         }
4738       } else if (isGeneric()) {
4739         SayAlreadyDeclared(name, *specific);
4740       }
4741       if (specific->has<SubprogramNameDetails>()) {
4742         specific->set_details(Details{SubprogramDetails{}});
4743       }
4744       return specific;
4745     }
4746   }
4747   return nullptr;
4748 }
4749 
4750 // DeclarationVisitor implementation
4751 
4752 bool DeclarationVisitor::BeginDecl() {
4753   BeginDeclTypeSpec();
4754   BeginArraySpec();
4755   return BeginAttrs();
4756 }
4757 void DeclarationVisitor::EndDecl() {
4758   EndDeclTypeSpec();
4759   EndArraySpec();
4760   EndAttrs();
4761 }
4762 
4763 bool DeclarationVisitor::CheckUseError(const parser::Name &name) {
4764   return HadUseError(context(), name.source, name.symbol);
4765 }
4766 
4767 // Report error if accessibility of symbol doesn't match isPrivate.
4768 void DeclarationVisitor::CheckAccessibility(
4769     const SourceName &name, bool isPrivate, Symbol &symbol) {
4770   if (symbol.attrs().test(Attr::PRIVATE) != isPrivate) {
4771     Say2(name,
4772         "'%s' does not have the same accessibility as its previous declaration"_err_en_US,
4773         symbol, "Previous declaration of '%s'"_en_US);
4774   }
4775 }
4776 
4777 bool DeclarationVisitor::Pre(const parser::TypeDeclarationStmt &x) {
4778   BeginDecl();
4779   // If INTRINSIC appears as an attr-spec, handle it now as if the
4780   // names had appeared on an INTRINSIC attribute statement beforehand.
4781   for (const auto &attr : std::get<std::list<parser::AttrSpec>>(x.t)) {
4782     if (std::holds_alternative<parser::Intrinsic>(attr.u)) {
4783       for (const auto &decl : std::get<std::list<parser::EntityDecl>>(x.t)) {
4784         DeclareIntrinsic(parser::GetFirstName(decl));
4785       }
4786       break;
4787     }
4788   }
4789   return true;
4790 }
4791 void DeclarationVisitor::Post(const parser::TypeDeclarationStmt &) {
4792   EndDecl();
4793 }
4794 
4795 void DeclarationVisitor::Post(const parser::DimensionStmt::Declaration &x) {
4796   DeclareObjectEntity(std::get<parser::Name>(x.t));
4797 }
4798 void DeclarationVisitor::Post(const parser::CodimensionDecl &x) {
4799   DeclareObjectEntity(std::get<parser::Name>(x.t));
4800 }
4801 
4802 bool DeclarationVisitor::Pre(const parser::Initialization &) {
4803   // Defer inspection of initializers to Initialization() so that the
4804   // symbol being initialized will be available within the initialization
4805   // expression.
4806   return false;
4807 }
4808 
4809 void DeclarationVisitor::Post(const parser::EntityDecl &x) {
4810   const auto &name{std::get<parser::ObjectName>(x.t)};
4811   Attrs attrs{attrs_ ? HandleSaveName(name.source, *attrs_) : Attrs{}};
4812   attrs.set(Attr::INTRINSIC, false); // dealt with in Pre(TypeDeclarationStmt)
4813   Symbol &symbol{DeclareUnknownEntity(name, attrs)};
4814   symbol.ReplaceName(name.source);
4815   SetCUDADataAttr(name.source, symbol, cudaDataAttr());
4816   if (const auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
4817     ConvertToObjectEntity(symbol) || ConvertToProcEntity(symbol);
4818     symbol.set(
4819         Symbol::Flag::EntryDummyArgument, false); // forestall excessive errors
4820     Initialization(name, *init, false);
4821   } else if (attrs.test(Attr::PARAMETER)) { // C882, C883
4822     Say(name, "Missing initialization for parameter '%s'"_err_en_US);
4823   }
4824   if (auto *scopeSymbol{currScope().symbol()}) {
4825     if (auto *details{scopeSymbol->detailsIf<DerivedTypeDetails>()}) {
4826       if (details->isDECStructure()) {
4827         details->add_component(symbol);
4828       }
4829     }
4830   }
4831 }
4832 
4833 void DeclarationVisitor::Post(const parser::PointerDecl &x) {
4834   const auto &name{std::get<parser::Name>(x.t)};
4835   if (const auto &deferredShapeSpecs{
4836           std::get<std::optional<parser::DeferredShapeSpecList>>(x.t)}) {
4837     CHECK(arraySpec().empty());
4838     BeginArraySpec();
4839     set_arraySpec(AnalyzeDeferredShapeSpecList(context(), *deferredShapeSpecs));
4840     Symbol &symbol{DeclareObjectEntity(name, Attrs{Attr::POINTER})};
4841     symbol.ReplaceName(name.source);
4842     EndArraySpec();
4843   } else {
4844     if (const auto *symbol{FindInScope(name)}) {
4845       const auto *subp{symbol->detailsIf<SubprogramDetails>()};
4846       if (!symbol->has<UseDetails>() && // error caught elsewhere
4847           !symbol->has<ObjectEntityDetails>() &&
4848           !symbol->has<ProcEntityDetails>() &&
4849           !symbol->CanReplaceDetails(ObjectEntityDetails{}) &&
4850           !symbol->CanReplaceDetails(ProcEntityDetails{}) &&
4851           !(subp && subp->isInterface())) {
4852         Say(name, "'%s' cannot have the POINTER attribute"_err_en_US);
4853       }
4854     }
4855     HandleAttributeStmt(Attr::POINTER, std::get<parser::Name>(x.t));
4856   }
4857 }
4858 
4859 bool DeclarationVisitor::Pre(const parser::BindEntity &x) {
4860   auto kind{std::get<parser::BindEntity::Kind>(x.t)};
4861   auto &name{std::get<parser::Name>(x.t)};
4862   Symbol *symbol;
4863   if (kind == parser::BindEntity::Kind::Object) {
4864     symbol = &HandleAttributeStmt(Attr::BIND_C, name);
4865   } else {
4866     symbol = &MakeCommonBlockSymbol(name);
4867     SetExplicitAttr(*symbol, Attr::BIND_C);
4868   }
4869   // 8.6.4(1)
4870   // Some entities such as named constant or module name need to checked
4871   // elsewhere. This is to skip the ICE caused by setting Bind name for non-name
4872   // things such as data type and also checks for procedures.
4873   if (symbol->has<CommonBlockDetails>() || symbol->has<ObjectEntityDetails>() ||
4874       symbol->has<EntityDetails>()) {
4875     SetBindNameOn(*symbol);
4876   } else {
4877     Say(name,
4878         "Only variable and named common block can be in BIND statement"_err_en_US);
4879   }
4880   return false;
4881 }
4882 bool DeclarationVisitor::Pre(const parser::OldParameterStmt &x) {
4883   inOldStyleParameterStmt_ = true;
4884   Walk(x.v);
4885   inOldStyleParameterStmt_ = false;
4886   return false;
4887 }
4888 bool DeclarationVisitor::Pre(const parser::NamedConstantDef &x) {
4889   auto &name{std::get<parser::NamedConstant>(x.t).v};
4890   auto &symbol{HandleAttributeStmt(Attr::PARAMETER, name)};
4891   ConvertToObjectEntity(symbol);
4892   auto *details{symbol.detailsIf<ObjectEntityDetails>()};
4893   if (!details || symbol.test(Symbol::Flag::CrayPointer) ||
4894       symbol.test(Symbol::Flag::CrayPointee)) {
4895     SayWithDecl(
4896         name, symbol, "PARAMETER attribute not allowed on '%s'"_err_en_US);
4897     return false;
4898   }
4899   const auto &expr{std::get<parser::ConstantExpr>(x.t)};
4900   if (details->init() || symbol.test(Symbol::Flag::InDataStmt)) {
4901     Say(name, "Named constant '%s' already has a value"_err_en_US);
4902   }
4903   if (inOldStyleParameterStmt_) {
4904     // non-standard extension PARAMETER statement (no parentheses)
4905     Walk(expr);
4906     auto folded{EvaluateExpr(expr)};
4907     if (details->type()) {
4908       SayWithDecl(name, symbol,
4909           "Alternative style PARAMETER '%s' must not already have an explicit type"_err_en_US);
4910     } else if (folded) {
4911       auto at{expr.thing.value().source};
4912       if (evaluate::IsActuallyConstant(*folded)) {
4913         if (const auto *type{currScope().GetType(*folded)}) {
4914           if (type->IsPolymorphic()) {
4915             Say(at, "The expression must not be polymorphic"_err_en_US);
4916           } else if (auto shape{ToArraySpec(
4917                          GetFoldingContext(), evaluate::GetShape(*folded))}) {
4918             // The type of the named constant is assumed from the expression.
4919             details->set_type(*type);
4920             details->set_init(std::move(*folded));
4921             details->set_shape(std::move(*shape));
4922           } else {
4923             Say(at, "The expression must have constant shape"_err_en_US);
4924           }
4925         } else {
4926           Say(at, "The expression must have a known type"_err_en_US);
4927         }
4928       } else {
4929         Say(at, "The expression must be a constant of known type"_err_en_US);
4930       }
4931     }
4932   } else {
4933     // standard-conforming PARAMETER statement (with parentheses)
4934     ApplyImplicitRules(symbol);
4935     Walk(expr);
4936     if (auto converted{EvaluateNonPointerInitializer(
4937             symbol, expr, expr.thing.value().source)}) {
4938       details->set_init(std::move(*converted));
4939     }
4940   }
4941   return false;
4942 }
4943 bool DeclarationVisitor::Pre(const parser::NamedConstant &x) {
4944   const parser::Name &name{x.v};
4945   if (!FindSymbol(name)) {
4946     Say(name, "Named constant '%s' not found"_err_en_US);
4947   } else {
4948     CheckUseError(name);
4949   }
4950   return false;
4951 }
4952 
4953 bool DeclarationVisitor::Pre(const parser::Enumerator &enumerator) {
4954   const parser::Name &name{std::get<parser::NamedConstant>(enumerator.t).v};
4955   Symbol *symbol{FindInScope(name)};
4956   if (symbol && !symbol->has<UnknownDetails>()) {
4957     // Contrary to named constants appearing in a PARAMETER statement,
4958     // enumerator names should not have their type, dimension or any other
4959     // attributes defined before they are declared in the enumerator statement,
4960     // with the exception of accessibility.
4961     // This is not explicitly forbidden by the standard, but they are scalars
4962     // which type is left for the compiler to chose, so do not let users try to
4963     // tamper with that.
4964     SayAlreadyDeclared(name, *symbol);
4965     symbol = nullptr;
4966   } else {
4967     // Enumerators are treated as PARAMETER (section 7.6 paragraph (4))
4968     symbol = &MakeSymbol(name, Attrs{Attr::PARAMETER}, ObjectEntityDetails{});
4969     symbol->SetType(context().MakeNumericType(
4970         TypeCategory::Integer, evaluate::CInteger::kind));
4971   }
4972 
4973   if (auto &init{std::get<std::optional<parser::ScalarIntConstantExpr>>(
4974           enumerator.t)}) {
4975     Walk(*init); // Resolve names in expression before evaluation.
4976     if (auto value{EvaluateInt64(context(), *init)}) {
4977       // Cast all init expressions to C_INT so that they can then be
4978       // safely incremented (see 7.6 Note 2).
4979       enumerationState_.value = static_cast<int>(*value);
4980     } else {
4981       Say(name,
4982           "Enumerator value could not be computed "
4983           "from the given expression"_err_en_US);
4984       // Prevent resolution of next enumerators value
4985       enumerationState_.value = std::nullopt;
4986     }
4987   }
4988 
4989   if (symbol) {
4990     if (enumerationState_.value) {
4991       symbol->get<ObjectEntityDetails>().set_init(SomeExpr{
4992           evaluate::Expr<evaluate::CInteger>{*enumerationState_.value}});
4993     } else {
4994       context().SetError(*symbol);
4995     }
4996   }
4997 
4998   if (enumerationState_.value) {
4999     (*enumerationState_.value)++;
5000   }
5001   return false;
5002 }
5003 
5004 void DeclarationVisitor::Post(const parser::EnumDef &) {
5005   enumerationState_ = EnumeratorState{};
5006 }
5007 
5008 bool DeclarationVisitor::Pre(const parser::AccessSpec &x) {
5009   Attr attr{AccessSpecToAttr(x)};
5010   if (!NonDerivedTypeScope().IsModule()) { // C817
5011     Say(currStmtSource().value(),
5012         "%s attribute may only appear in the specification part of a module"_err_en_US,
5013         EnumToString(attr));
5014   }
5015   CheckAndSet(attr);
5016   return false;
5017 }
5018 
5019 bool DeclarationVisitor::Pre(const parser::AsynchronousStmt &x) {
5020   return HandleAttributeStmt(Attr::ASYNCHRONOUS, x.v);
5021 }
5022 bool DeclarationVisitor::Pre(const parser::ContiguousStmt &x) {
5023   return HandleAttributeStmt(Attr::CONTIGUOUS, x.v);
5024 }
5025 bool DeclarationVisitor::Pre(const parser::ExternalStmt &x) {
5026   HandleAttributeStmt(Attr::EXTERNAL, x.v);
5027   for (const auto &name : x.v) {
5028     auto *symbol{FindSymbol(name)};
5029     if (!ConvertToProcEntity(DEREF(symbol), name.source)) {
5030       // Check if previous symbol is an interface.
5031       if (auto *details{symbol->detailsIf<SubprogramDetails>()}) {
5032         if (details->isInterface()) {
5033           // Warn if interface previously declared.
5034           context().Warn(common::LanguageFeature::RedundantAttribute,
5035               name.source,
5036               "EXTERNAL attribute was already specified on '%s'"_warn_en_US,
5037               name.source);
5038         }
5039       } else {
5040         SayWithDecl(
5041             name, *symbol, "EXTERNAL attribute not allowed on '%s'"_err_en_US);
5042       }
5043     } else if (symbol->attrs().test(Attr::INTRINSIC)) { // C840
5044       Say(symbol->name(),
5045           "Symbol '%s' cannot have both INTRINSIC and EXTERNAL attributes"_err_en_US,
5046           symbol->name());
5047     }
5048   }
5049   return false;
5050 }
5051 bool DeclarationVisitor::Pre(const parser::IntentStmt &x) {
5052   auto &intentSpec{std::get<parser::IntentSpec>(x.t)};
5053   auto &names{std::get<std::list<parser::Name>>(x.t)};
5054   return CheckNotInBlock("INTENT") && // C1107
5055       HandleAttributeStmt(IntentSpecToAttr(intentSpec), names);
5056 }
5057 bool DeclarationVisitor::Pre(const parser::IntrinsicStmt &x) {
5058   for (const auto &name : x.v) {
5059     DeclareIntrinsic(name);
5060   }
5061   return false;
5062 }
5063 void DeclarationVisitor::DeclareIntrinsic(const parser::Name &name) {
5064   HandleAttributeStmt(Attr::INTRINSIC, name);
5065   if (!IsIntrinsic(name.source, std::nullopt)) {
5066     Say(name.source, "'%s' is not a known intrinsic procedure"_err_en_US);
5067   }
5068   auto &symbol{DEREF(FindSymbol(name))};
5069   if (symbol.has<GenericDetails>()) {
5070     // Generic interface is extending intrinsic; ok
5071   } else if (!ConvertToProcEntity(symbol, name.source)) {
5072     SayWithDecl(
5073         name, symbol, "INTRINSIC attribute not allowed on '%s'"_err_en_US);
5074   } else if (symbol.attrs().test(Attr::EXTERNAL)) { // C840
5075     Say(symbol.name(),
5076         "Symbol '%s' cannot have both EXTERNAL and INTRINSIC attributes"_err_en_US,
5077         symbol.name());
5078   } else {
5079     if (symbol.GetType()) {
5080       // These warnings are worded so that they should make sense in either
5081       // order.
5082       if (auto *msg{context().Warn(
5083               common::UsageWarning::IgnoredIntrinsicFunctionType, symbol.name(),
5084               "Explicit type declaration ignored for intrinsic function '%s'"_warn_en_US,
5085               symbol.name())}) {
5086         msg->Attach(name.source,
5087             "INTRINSIC statement for explicitly-typed '%s'"_en_US, name.source);
5088       }
5089     }
5090     if (!symbol.test(Symbol::Flag::Function) &&
5091         !symbol.test(Symbol::Flag::Subroutine)) {
5092       if (context().intrinsics().IsIntrinsicFunction(name.source.ToString())) {
5093         symbol.set(Symbol::Flag::Function);
5094       } else if (context().intrinsics().IsIntrinsicSubroutine(
5095                      name.source.ToString())) {
5096         symbol.set(Symbol::Flag::Subroutine);
5097       }
5098     }
5099   }
5100 }
5101 bool DeclarationVisitor::Pre(const parser::OptionalStmt &x) {
5102   return CheckNotInBlock("OPTIONAL") && // C1107
5103       HandleAttributeStmt(Attr::OPTIONAL, x.v);
5104 }
5105 bool DeclarationVisitor::Pre(const parser::ProtectedStmt &x) {
5106   return HandleAttributeStmt(Attr::PROTECTED, x.v);
5107 }
5108 bool DeclarationVisitor::Pre(const parser::ValueStmt &x) {
5109   return CheckNotInBlock("VALUE") && // C1107
5110       HandleAttributeStmt(Attr::VALUE, x.v);
5111 }
5112 bool DeclarationVisitor::Pre(const parser::VolatileStmt &x) {
5113   return HandleAttributeStmt(Attr::VOLATILE, x.v);
5114 }
5115 bool DeclarationVisitor::Pre(const parser::CUDAAttributesStmt &x) {
5116   auto attr{std::get<common::CUDADataAttr>(x.t)};
5117   for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
5118     auto *symbol{FindInScope(name)};
5119     if (symbol && symbol->has<UseDetails>()) {
5120       Say(currStmtSource().value(),
5121           "Cannot apply CUDA data attribute to use-associated '%s'"_err_en_US,
5122           name.source);
5123     } else {
5124       if (!symbol) {
5125         symbol = &MakeSymbol(name, ObjectEntityDetails{});
5126       }
5127       SetCUDADataAttr(name.source, *symbol, attr);
5128     }
5129   }
5130   return false;
5131 }
5132 // Handle a statement that sets an attribute on a list of names.
5133 bool DeclarationVisitor::HandleAttributeStmt(
5134     Attr attr, const std::list<parser::Name> &names) {
5135   for (const auto &name : names) {
5136     HandleAttributeStmt(attr, name);
5137   }
5138   return false;
5139 }
5140 Symbol &DeclarationVisitor::HandleAttributeStmt(
5141     Attr attr, const parser::Name &name) {
5142   auto *symbol{FindInScope(name)};
5143   if (attr == Attr::ASYNCHRONOUS || attr == Attr::VOLATILE) {
5144     // these can be set on a symbol that is host-assoc or use-assoc
5145     if (!symbol &&
5146         (currScope().kind() == Scope::Kind::Subprogram ||
5147             currScope().kind() == Scope::Kind::BlockConstruct)) {
5148       if (auto *hostSymbol{FindSymbol(name)}) {
5149         symbol = &MakeHostAssocSymbol(name, *hostSymbol);
5150       }
5151     }
5152   } else if (symbol && symbol->has<UseDetails>()) {
5153     if (symbol->GetUltimate().attrs().test(attr)) {
5154       context().Warn(common::LanguageFeature::RedundantAttribute,
5155           currStmtSource().value(),
5156           "Use-associated '%s' already has '%s' attribute"_warn_en_US,
5157           name.source, EnumToString(attr));
5158     } else {
5159       Say(currStmtSource().value(),
5160           "Cannot change %s attribute on use-associated '%s'"_err_en_US,
5161           EnumToString(attr), name.source);
5162     }
5163     return *symbol;
5164   }
5165   if (!symbol) {
5166     symbol = &MakeSymbol(name, EntityDetails{});
5167   }
5168   if (CheckDuplicatedAttr(name.source, *symbol, attr)) {
5169     HandleSaveName(name.source, Attrs{attr});
5170     SetExplicitAttr(*symbol, attr);
5171   }
5172   return *symbol;
5173 }
5174 // C1107
5175 bool DeclarationVisitor::CheckNotInBlock(const char *stmt) {
5176   if (currScope().kind() == Scope::Kind::BlockConstruct) {
5177     Say(MessageFormattedText{
5178         "%s statement is not allowed in a BLOCK construct"_err_en_US, stmt});
5179     return false;
5180   } else {
5181     return true;
5182   }
5183 }
5184 
5185 void DeclarationVisitor::Post(const parser::ObjectDecl &x) {
5186   CHECK(objectDeclAttr_);
5187   const auto &name{std::get<parser::ObjectName>(x.t)};
5188   DeclareObjectEntity(name, Attrs{*objectDeclAttr_});
5189 }
5190 
5191 // Declare an entity not yet known to be an object or proc.
5192 Symbol &DeclarationVisitor::DeclareUnknownEntity(
5193     const parser::Name &name, Attrs attrs) {
5194   if (!arraySpec().empty() || !coarraySpec().empty()) {
5195     return DeclareObjectEntity(name, attrs);
5196   } else {
5197     Symbol &symbol{DeclareEntity<EntityDetails>(name, attrs)};
5198     if (auto *type{GetDeclTypeSpec()}) {
5199       SetType(name, *type);
5200     }
5201     charInfo_.length.reset();
5202     if (symbol.attrs().test(Attr::EXTERNAL)) {
5203       ConvertToProcEntity(symbol);
5204     } else if (symbol.attrs().HasAny(Attrs{Attr::ALLOCATABLE,
5205                    Attr::ASYNCHRONOUS, Attr::CONTIGUOUS, Attr::PARAMETER,
5206                    Attr::SAVE, Attr::TARGET, Attr::VALUE, Attr::VOLATILE})) {
5207       ConvertToObjectEntity(symbol);
5208     }
5209     if (attrs.test(Attr::BIND_C)) {
5210       SetBindNameOn(symbol);
5211     }
5212     return symbol;
5213   }
5214 }
5215 
5216 bool DeclarationVisitor::HasCycle(
5217     const Symbol &procSymbol, const Symbol *interface) {
5218   SourceOrderedSymbolSet procsInCycle;
5219   procsInCycle.insert(procSymbol);
5220   while (interface) {
5221     if (procsInCycle.count(*interface) > 0) {
5222       for (const auto &procInCycle : procsInCycle) {
5223         Say(procInCycle->name(),
5224             "The interface for procedure '%s' is recursively defined"_err_en_US,
5225             procInCycle->name());
5226         context().SetError(*procInCycle);
5227       }
5228       return true;
5229     } else if (const auto *procDetails{
5230                    interface->detailsIf<ProcEntityDetails>()}) {
5231       procsInCycle.insert(*interface);
5232       interface = procDetails->procInterface();
5233     } else {
5234       break;
5235     }
5236   }
5237   return false;
5238 }
5239 
5240 Symbol &DeclarationVisitor::DeclareProcEntity(
5241     const parser::Name &name, Attrs attrs, const Symbol *interface) {
5242   Symbol *proc{nullptr};
5243   if (auto *extant{FindInScope(name)}) {
5244     if (auto *d{extant->detailsIf<GenericDetails>()}; d && !d->derivedType()) {
5245       // procedure pointer with same name as a generic
5246       if (auto *specific{d->specific()}) {
5247         SayAlreadyDeclared(name, *specific);
5248       } else {
5249         // Create the ProcEntityDetails symbol in the scope as the "specific()"
5250         // symbol behind an existing GenericDetails symbol of the same name.
5251         proc = &Resolve(name,
5252             currScope().MakeSymbol(name.source, attrs, ProcEntityDetails{}));
5253         d->set_specific(*proc);
5254       }
5255     }
5256   }
5257   Symbol &symbol{proc ? *proc : DeclareEntity<ProcEntityDetails>(name, attrs)};
5258   if (auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
5259     if (context().HasError(symbol)) {
5260     } else if (HasCycle(symbol, interface)) {
5261       return symbol;
5262     } else if (interface && (details->procInterface() || details->type())) {
5263       SayWithDecl(name, symbol,
5264           "The interface for procedure '%s' has already been declared"_err_en_US);
5265       context().SetError(symbol);
5266     } else if (interface) {
5267       details->set_procInterfaces(
5268           *interface, BypassGeneric(interface->GetUltimate()));
5269       if (interface->test(Symbol::Flag::Function)) {
5270         symbol.set(Symbol::Flag::Function);
5271       } else if (interface->test(Symbol::Flag::Subroutine)) {
5272         symbol.set(Symbol::Flag::Subroutine);
5273       }
5274     } else if (auto *type{GetDeclTypeSpec()}) {
5275       SetType(name, *type);
5276       symbol.set(Symbol::Flag::Function);
5277     }
5278     SetBindNameOn(symbol);
5279     SetPassNameOn(symbol);
5280   }
5281   return symbol;
5282 }
5283 
5284 Symbol &DeclarationVisitor::DeclareObjectEntity(
5285     const parser::Name &name, Attrs attrs) {
5286   Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, attrs)};
5287   if (auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
5288     if (auto *type{GetDeclTypeSpec()}) {
5289       SetType(name, *type);
5290     }
5291     if (!arraySpec().empty()) {
5292       if (details->IsArray()) {
5293         if (!context().HasError(symbol)) {
5294           Say(name,
5295               "The dimensions of '%s' have already been declared"_err_en_US);
5296           context().SetError(symbol);
5297         }
5298       } else if (MustBeScalar(symbol)) {
5299         context().Warn(common::UsageWarning::PreviousScalarUse, name.source,
5300             "'%s' appeared earlier as a scalar actual argument to a specification function"_warn_en_US,
5301             name.source);
5302       } else if (details->init() || symbol.test(Symbol::Flag::InDataStmt)) {
5303         Say(name, "'%s' was initialized earlier as a scalar"_err_en_US);
5304       } else {
5305         details->set_shape(arraySpec());
5306       }
5307     }
5308     if (!coarraySpec().empty()) {
5309       if (details->IsCoarray()) {
5310         if (!context().HasError(symbol)) {
5311           Say(name,
5312               "The codimensions of '%s' have already been declared"_err_en_US);
5313           context().SetError(symbol);
5314         }
5315       } else {
5316         details->set_coshape(coarraySpec());
5317       }
5318     }
5319     SetBindNameOn(symbol);
5320   }
5321   ClearArraySpec();
5322   ClearCoarraySpec();
5323   charInfo_.length.reset();
5324   return symbol;
5325 }
5326 
5327 void DeclarationVisitor::Post(const parser::IntegerTypeSpec &x) {
5328   if (!isVectorType_) {
5329     SetDeclTypeSpec(MakeNumericType(TypeCategory::Integer, x.v));
5330   }
5331 }
5332 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Real &x) {
5333   if (!isVectorType_) {
5334     SetDeclTypeSpec(MakeNumericType(TypeCategory::Real, x.kind));
5335   }
5336 }
5337 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Complex &x) {
5338   SetDeclTypeSpec(MakeNumericType(TypeCategory::Complex, x.kind));
5339 }
5340 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Logical &x) {
5341   SetDeclTypeSpec(MakeLogicalType(x.kind));
5342 }
5343 void DeclarationVisitor::Post(const parser::IntrinsicTypeSpec::Character &) {
5344   if (!charInfo_.length) {
5345     charInfo_.length = ParamValue{1, common::TypeParamAttr::Len};
5346   }
5347   if (!charInfo_.kind) {
5348     charInfo_.kind =
5349         KindExpr{context().GetDefaultKind(TypeCategory::Character)};
5350   }
5351   SetDeclTypeSpec(currScope().MakeCharacterType(
5352       std::move(*charInfo_.length), std::move(*charInfo_.kind)));
5353   charInfo_ = {};
5354 }
5355 void DeclarationVisitor::Post(const parser::CharSelector::LengthAndKind &x) {
5356   charInfo_.kind = EvaluateSubscriptIntExpr(x.kind);
5357   std::optional<std::int64_t> intKind{ToInt64(charInfo_.kind)};
5358   if (intKind &&
5359       !context().targetCharacteristics().IsTypeEnabled(
5360           TypeCategory::Character, *intKind)) { // C715, C719
5361     Say(currStmtSource().value(),
5362         "KIND value (%jd) not valid for CHARACTER"_err_en_US, *intKind);
5363     charInfo_.kind = std::nullopt; // prevent further errors
5364   }
5365   if (x.length) {
5366     charInfo_.length = GetParamValue(*x.length, common::TypeParamAttr::Len);
5367   }
5368 }
5369 void DeclarationVisitor::Post(const parser::CharLength &x) {
5370   if (const auto *length{std::get_if<std::uint64_t>(&x.u)}) {
5371     charInfo_.length = ParamValue{
5372         static_cast<ConstantSubscript>(*length), common::TypeParamAttr::Len};
5373   } else {
5374     charInfo_.length = GetParamValue(
5375         std::get<parser::TypeParamValue>(x.u), common::TypeParamAttr::Len);
5376   }
5377 }
5378 void DeclarationVisitor::Post(const parser::LengthSelector &x) {
5379   if (const auto *param{std::get_if<parser::TypeParamValue>(&x.u)}) {
5380     charInfo_.length = GetParamValue(*param, common::TypeParamAttr::Len);
5381   }
5382 }
5383 
5384 bool DeclarationVisitor::Pre(const parser::KindParam &x) {
5385   if (const auto *kind{std::get_if<
5386           parser::Scalar<parser::Integer<parser::Constant<parser::Name>>>>(
5387           &x.u)}) {
5388     const parser::Name &name{kind->thing.thing.thing};
5389     if (!FindSymbol(name)) {
5390       Say(name, "Parameter '%s' not found"_err_en_US);
5391     }
5392   }
5393   return false;
5394 }
5395 
5396 int DeclarationVisitor::GetVectorElementKind(
5397     TypeCategory category, const std::optional<parser::KindSelector> &kind) {
5398   KindExpr value{GetKindParamExpr(category, kind)};
5399   if (auto known{evaluate::ToInt64(value)}) {
5400     return static_cast<int>(*known);
5401   }
5402   common::die("Vector element kind must be known at compile-time");
5403 }
5404 
5405 bool DeclarationVisitor::Pre(const parser::VectorTypeSpec &) {
5406   // PowerPC vector types are allowed only on Power architectures.
5407   if (!currScope().context().targetCharacteristics().isPPC()) {
5408     Say(currStmtSource().value(),
5409         "Vector type is only supported for PowerPC"_err_en_US);
5410     isVectorType_ = false;
5411     return false;
5412   }
5413   isVectorType_ = true;
5414   return true;
5415 }
5416 // Create semantic::DerivedTypeSpec for Vector types here.
5417 void DeclarationVisitor::Post(const parser::VectorTypeSpec &x) {
5418   llvm::StringRef typeName;
5419   llvm::SmallVector<ParamValue> typeParams;
5420   DerivedTypeSpec::Category vectorCategory;
5421 
5422   isVectorType_ = false;
5423   common::visit(
5424       common::visitors{
5425           [&](const parser::IntrinsicVectorTypeSpec &y) {
5426             vectorCategory = DerivedTypeSpec::Category::IntrinsicVector;
5427             int vecElemKind = 0;
5428             typeName = "__builtin_ppc_intrinsic_vector";
5429             common::visit(
5430                 common::visitors{
5431                     [&](const parser::IntegerTypeSpec &z) {
5432                       vecElemKind = GetVectorElementKind(
5433                           TypeCategory::Integer, std::move(z.v));
5434                       typeParams.push_back(ParamValue(
5435                           static_cast<common::ConstantSubscript>(
5436                               common::VectorElementCategory::Integer),
5437                           common::TypeParamAttr::Kind));
5438                     },
5439                     [&](const parser::IntrinsicTypeSpec::Real &z) {
5440                       vecElemKind = GetVectorElementKind(
5441                           TypeCategory::Real, std::move(z.kind));
5442                       typeParams.push_back(
5443                           ParamValue(static_cast<common::ConstantSubscript>(
5444                                          common::VectorElementCategory::Real),
5445                               common::TypeParamAttr::Kind));
5446                     },
5447                     [&](const parser::UnsignedTypeSpec &z) {
5448                       vecElemKind = GetVectorElementKind(
5449                           TypeCategory::Integer, std::move(z.v));
5450                       typeParams.push_back(ParamValue(
5451                           static_cast<common::ConstantSubscript>(
5452                               common::VectorElementCategory::Unsigned),
5453                           common::TypeParamAttr::Kind));
5454                     },
5455                 },
5456                 y.v.u);
5457             typeParams.push_back(
5458                 ParamValue(static_cast<common::ConstantSubscript>(vecElemKind),
5459                     common::TypeParamAttr::Kind));
5460           },
5461           [&](const parser::VectorTypeSpec::PairVectorTypeSpec &y) {
5462             vectorCategory = DerivedTypeSpec::Category::PairVector;
5463             typeName = "__builtin_ppc_pair_vector";
5464           },
5465           [&](const parser::VectorTypeSpec::QuadVectorTypeSpec &y) {
5466             vectorCategory = DerivedTypeSpec::Category::QuadVector;
5467             typeName = "__builtin_ppc_quad_vector";
5468           },
5469       },
5470       x.u);
5471 
5472   auto ppcBuiltinTypesScope = currScope().context().GetPPCBuiltinTypesScope();
5473   if (!ppcBuiltinTypesScope) {
5474     common::die("INTERNAL: The __ppc_types module was not found ");
5475   }
5476 
5477   auto iter{ppcBuiltinTypesScope->find(
5478       semantics::SourceName{typeName.data(), typeName.size()})};
5479   if (iter == ppcBuiltinTypesScope->cend()) {
5480     common::die("INTERNAL: The __ppc_types module does not define "
5481                 "the type '%s'",
5482         typeName.data());
5483   }
5484 
5485   const semantics::Symbol &typeSymbol{*iter->second};
5486   DerivedTypeSpec vectorDerivedType{typeName.data(), typeSymbol};
5487   vectorDerivedType.set_category(vectorCategory);
5488   if (typeParams.size()) {
5489     vectorDerivedType.AddRawParamValue(nullptr, std::move(typeParams[0]));
5490     vectorDerivedType.AddRawParamValue(nullptr, std::move(typeParams[1]));
5491     vectorDerivedType.CookParameters(GetFoldingContext());
5492   }
5493 
5494   if (const DeclTypeSpec *
5495       extant{ppcBuiltinTypesScope->FindInstantiatedDerivedType(
5496           vectorDerivedType, DeclTypeSpec::Category::TypeDerived)}) {
5497     // This derived type and parameter expressions (if any) are already present
5498     // in the __ppc_intrinsics scope.
5499     SetDeclTypeSpec(*extant);
5500   } else {
5501     DeclTypeSpec &type{ppcBuiltinTypesScope->MakeDerivedType(
5502         DeclTypeSpec::Category::TypeDerived, std::move(vectorDerivedType))};
5503     DerivedTypeSpec &derived{type.derivedTypeSpec()};
5504     auto restorer{
5505         GetFoldingContext().messages().SetLocation(currStmtSource().value())};
5506     derived.Instantiate(*ppcBuiltinTypesScope);
5507     SetDeclTypeSpec(type);
5508   }
5509 }
5510 
5511 bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Type &) {
5512   CHECK(GetDeclTypeSpecCategory() == DeclTypeSpec::Category::TypeDerived);
5513   return true;
5514 }
5515 
5516 void DeclarationVisitor::Post(const parser::DeclarationTypeSpec::Type &type) {
5517   const parser::Name &derivedName{std::get<parser::Name>(type.derived.t)};
5518   if (const Symbol * derivedSymbol{derivedName.symbol}) {
5519     CheckForAbstractType(*derivedSymbol); // C706
5520   }
5521 }
5522 
5523 bool DeclarationVisitor::Pre(const parser::DeclarationTypeSpec::Class &) {
5524   SetDeclTypeSpecCategory(DeclTypeSpec::Category::ClassDerived);
5525   return true;
5526 }
5527 
5528 void DeclarationVisitor::Post(
5529     const parser::DeclarationTypeSpec::Class &parsedClass) {
5530   const auto &typeName{std::get<parser::Name>(parsedClass.derived.t)};
5531   if (auto spec{ResolveDerivedType(typeName)};
5532       spec && !IsExtensibleType(&*spec)) { // C705
5533     SayWithDecl(typeName, *typeName.symbol,
5534         "Non-extensible derived type '%s' may not be used with CLASS"
5535         " keyword"_err_en_US);
5536   }
5537 }
5538 
5539 void DeclarationVisitor::Post(const parser::DerivedTypeSpec &x) {
5540   const auto &typeName{std::get<parser::Name>(x.t)};
5541   auto spec{ResolveDerivedType(typeName)};
5542   if (!spec) {
5543     return;
5544   }
5545   bool seenAnyName{false};
5546   for (const auto &typeParamSpec :
5547       std::get<std::list<parser::TypeParamSpec>>(x.t)) {
5548     const auto &optKeyword{
5549         std::get<std::optional<parser::Keyword>>(typeParamSpec.t)};
5550     std::optional<SourceName> name;
5551     if (optKeyword) {
5552       seenAnyName = true;
5553       name = optKeyword->v.source;
5554     } else if (seenAnyName) {
5555       Say(typeName.source, "Type parameter value must have a name"_err_en_US);
5556       continue;
5557     }
5558     const auto &value{std::get<parser::TypeParamValue>(typeParamSpec.t)};
5559     // The expressions in a derived type specifier whose values define
5560     // non-defaulted type parameters are evaluated (folded) in the enclosing
5561     // scope.  The KIND/LEN distinction is resolved later in
5562     // DerivedTypeSpec::CookParameters().
5563     ParamValue param{GetParamValue(value, common::TypeParamAttr::Kind)};
5564     if (!param.isExplicit() || param.GetExplicit()) {
5565       spec->AddRawParamValue(
5566           common::GetPtrFromOptional(optKeyword), std::move(param));
5567     }
5568   }
5569   // The DerivedTypeSpec *spec is used initially as a search key.
5570   // If it turns out to have the same name and actual parameter
5571   // value expressions as another DerivedTypeSpec in the current
5572   // scope does, then we'll use that extant spec; otherwise, when this
5573   // spec is distinct from all derived types previously instantiated
5574   // in the current scope, this spec will be moved into that collection.
5575   const auto &dtDetails{spec->typeSymbol().get<DerivedTypeDetails>()};
5576   auto category{GetDeclTypeSpecCategory()};
5577   if (dtDetails.isForwardReferenced()) {
5578     DeclTypeSpec &type{currScope().MakeDerivedType(category, std::move(*spec))};
5579     SetDeclTypeSpec(type);
5580     return;
5581   }
5582   // Normalize parameters to produce a better search key.
5583   spec->CookParameters(GetFoldingContext());
5584   if (!spec->MightBeParameterized()) {
5585     spec->EvaluateParameters(context());
5586   }
5587   if (const DeclTypeSpec *
5588       extant{currScope().FindInstantiatedDerivedType(*spec, category)}) {
5589     // This derived type and parameter expressions (if any) are already present
5590     // in this scope.
5591     SetDeclTypeSpec(*extant);
5592   } else {
5593     DeclTypeSpec &type{currScope().MakeDerivedType(category, std::move(*spec))};
5594     DerivedTypeSpec &derived{type.derivedTypeSpec()};
5595     if (derived.MightBeParameterized() &&
5596         currScope().IsParameterizedDerivedType()) {
5597       // Defer instantiation; use the derived type's definition's scope.
5598       derived.set_scope(DEREF(spec->typeSymbol().scope()));
5599     } else if (&currScope() == spec->typeSymbol().scope()) {
5600       // Direct recursive use of a type in the definition of one of its
5601       // components: defer instantiation
5602     } else {
5603       auto restorer{
5604           GetFoldingContext().messages().SetLocation(currStmtSource().value())};
5605       derived.Instantiate(currScope());
5606     }
5607     SetDeclTypeSpec(type);
5608   }
5609   // Capture the DerivedTypeSpec in the parse tree for use in building
5610   // structure constructor expressions.
5611   x.derivedTypeSpec = &GetDeclTypeSpec()->derivedTypeSpec();
5612 }
5613 
5614 void DeclarationVisitor::Post(const parser::DeclarationTypeSpec::Record &rec) {
5615   const auto &typeName{rec.v};
5616   if (auto spec{ResolveDerivedType(typeName)}) {
5617     spec->CookParameters(GetFoldingContext());
5618     spec->EvaluateParameters(context());
5619     if (const DeclTypeSpec *
5620         extant{currScope().FindInstantiatedDerivedType(
5621             *spec, DeclTypeSpec::TypeDerived)}) {
5622       SetDeclTypeSpec(*extant);
5623     } else {
5624       Say(typeName.source, "%s is not a known STRUCTURE"_err_en_US,
5625           typeName.source);
5626     }
5627   }
5628 }
5629 
5630 // The descendents of DerivedTypeDef in the parse tree are visited directly
5631 // in this Pre() routine so that recursive use of the derived type can be
5632 // supported in the components.
5633 bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
5634   auto &stmt{std::get<parser::Statement<parser::DerivedTypeStmt>>(x.t)};
5635   Walk(stmt);
5636   Walk(std::get<std::list<parser::Statement<parser::TypeParamDefStmt>>>(x.t));
5637   auto &scope{currScope()};
5638   CHECK(scope.symbol());
5639   CHECK(scope.symbol()->scope() == &scope);
5640   auto &details{scope.symbol()->get<DerivedTypeDetails>()};
5641   for (auto &paramName : std::get<std::list<parser::Name>>(stmt.statement.t)) {
5642     if (auto *symbol{FindInScope(scope, paramName)}) {
5643       if (auto *details{symbol->detailsIf<TypeParamDetails>()}) {
5644         if (!details->attr()) {
5645           Say(paramName,
5646               "No definition found for type parameter '%s'"_err_en_US); // C742
5647         }
5648       }
5649     }
5650   }
5651   Walk(std::get<std::list<parser::Statement<parser::PrivateOrSequence>>>(x.t));
5652   const auto &componentDefs{
5653       std::get<std::list<parser::Statement<parser::ComponentDefStmt>>>(x.t)};
5654   Walk(componentDefs);
5655   if (derivedTypeInfo_.sequence) {
5656     details.set_sequence(true);
5657     if (componentDefs.empty()) {
5658       // F'2023 C745 - not enforced by any compiler
5659       context().Warn(common::LanguageFeature::EmptySequenceType, stmt.source,
5660           "A sequence type should have at least one component"_warn_en_US);
5661     }
5662     if (!details.paramDeclOrder().empty()) { // C740
5663       Say(stmt.source,
5664           "A sequence type may not have type parameters"_err_en_US);
5665     }
5666     if (derivedTypeInfo_.extends) { // C735
5667       Say(stmt.source,
5668           "A sequence type may not have the EXTENDS attribute"_err_en_US);
5669     }
5670   }
5671   Walk(std::get<std::optional<parser::TypeBoundProcedurePart>>(x.t));
5672   Walk(std::get<parser::Statement<parser::EndTypeStmt>>(x.t));
5673   details.set_isForwardReferenced(false);
5674   derivedTypeInfo_ = {};
5675   PopScope();
5676   return false;
5677 }
5678 
5679 bool DeclarationVisitor::Pre(const parser::DerivedTypeStmt &) {
5680   return BeginAttrs();
5681 }
5682 void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
5683   auto &name{std::get<parser::Name>(x.t)};
5684   // Resolve the EXTENDS() clause before creating the derived
5685   // type's symbol to foil attempts to recursively extend a type.
5686   auto *extendsName{derivedTypeInfo_.extends};
5687   std::optional<DerivedTypeSpec> extendsType{
5688       ResolveExtendsType(name, extendsName)};
5689   DerivedTypeDetails derivedTypeDetails;
5690   // Catch any premature structure constructors within the definition
5691   derivedTypeDetails.set_isForwardReferenced(true);
5692   auto &symbol{MakeSymbol(name, GetAttrs(), std::move(derivedTypeDetails))};
5693   symbol.ReplaceName(name.source);
5694   derivedTypeInfo_.type = &symbol;
5695   PushScope(Scope::Kind::DerivedType, &symbol);
5696   if (extendsType) {
5697     // Declare the "parent component"; private if the type is.
5698     // Any symbol stored in the EXTENDS() clause is temporarily
5699     // hidden so that a new symbol can be created for the parent
5700     // component without producing spurious errors about already
5701     // existing.
5702     const Symbol &extendsSymbol{extendsType->typeSymbol()};
5703     auto restorer{common::ScopedSet(extendsName->symbol, nullptr)};
5704     if (OkToAddComponent(*extendsName, &extendsSymbol)) {
5705       auto &comp{DeclareEntity<ObjectEntityDetails>(*extendsName, Attrs{})};
5706       comp.attrs().set(
5707           Attr::PRIVATE, extendsSymbol.attrs().test(Attr::PRIVATE));
5708       comp.implicitAttrs().set(
5709           Attr::PRIVATE, extendsSymbol.implicitAttrs().test(Attr::PRIVATE));
5710       comp.set(Symbol::Flag::ParentComp);
5711       DeclTypeSpec &type{currScope().MakeDerivedType(
5712           DeclTypeSpec::TypeDerived, std::move(*extendsType))};
5713       type.derivedTypeSpec().set_scope(DEREF(extendsSymbol.scope()));
5714       comp.SetType(type);
5715       DerivedTypeDetails &details{symbol.get<DerivedTypeDetails>()};
5716       details.add_component(comp);
5717     }
5718   }
5719   // Create symbols now for type parameters so that they shadow names
5720   // from the enclosing specification part.
5721   if (auto *details{symbol.detailsIf<DerivedTypeDetails>()}) {
5722     for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
5723       if (Symbol * symbol{MakeTypeSymbol(name, TypeParamDetails{})}) {
5724         details->add_paramNameOrder(*symbol);
5725       }
5726     }
5727   }
5728   EndAttrs();
5729 }
5730 
5731 void DeclarationVisitor::Post(const parser::TypeParamDefStmt &x) {
5732   auto *type{GetDeclTypeSpec()};
5733   DerivedTypeDetails *derivedDetails{nullptr};
5734   if (Symbol * dtSym{currScope().symbol()}) {
5735     derivedDetails = dtSym->detailsIf<DerivedTypeDetails>();
5736   }
5737   auto attr{std::get<common::TypeParamAttr>(x.t)};
5738   for (auto &decl : std::get<std::list<parser::TypeParamDecl>>(x.t)) {
5739     auto &name{std::get<parser::Name>(decl.t)};
5740     if (Symbol * symbol{FindInScope(currScope(), name)}) {
5741       if (auto *paramDetails{symbol->detailsIf<TypeParamDetails>()}) {
5742         if (!paramDetails->attr()) {
5743           paramDetails->set_attr(attr);
5744           SetType(name, *type);
5745           if (auto &init{std::get<std::optional<parser::ScalarIntConstantExpr>>(
5746                   decl.t)}) {
5747             if (auto maybeExpr{AnalyzeExpr(context(), *init)}) {
5748               if (auto *intExpr{std::get_if<SomeIntExpr>(&maybeExpr->u)}) {
5749                 paramDetails->set_init(std::move(*intExpr));
5750               }
5751             }
5752           }
5753           if (derivedDetails) {
5754             derivedDetails->add_paramDeclOrder(*symbol);
5755           }
5756         } else {
5757           Say(name,
5758               "Type parameter '%s' was already declared in this derived type"_err_en_US);
5759         }
5760       }
5761     } else {
5762       Say(name, "'%s' is not a parameter of this derived type"_err_en_US);
5763     }
5764   }
5765   EndDecl();
5766 }
5767 bool DeclarationVisitor::Pre(const parser::TypeAttrSpec::Extends &x) {
5768   if (derivedTypeInfo_.extends) {
5769     Say(currStmtSource().value(),
5770         "Attribute 'EXTENDS' cannot be used more than once"_err_en_US);
5771   } else {
5772     derivedTypeInfo_.extends = &x.v;
5773   }
5774   return false;
5775 }
5776 
5777 bool DeclarationVisitor::Pre(const parser::PrivateStmt &) {
5778   if (!currScope().parent().IsModule()) {
5779     Say("PRIVATE is only allowed in a derived type that is"
5780         " in a module"_err_en_US); // C766
5781   } else if (derivedTypeInfo_.sawContains) {
5782     derivedTypeInfo_.privateBindings = true;
5783   } else if (!derivedTypeInfo_.privateComps) {
5784     derivedTypeInfo_.privateComps = true;
5785   } else { // C738
5786     context().Warn(common::LanguageFeature::RedundantAttribute,
5787         "PRIVATE should not appear more than once in derived type components"_warn_en_US);
5788   }
5789   return false;
5790 }
5791 bool DeclarationVisitor::Pre(const parser::SequenceStmt &) {
5792   if (derivedTypeInfo_.sequence) { // C738
5793     context().Warn(common::LanguageFeature::RedundantAttribute,
5794         "SEQUENCE should not appear more than once in derived type components"_warn_en_US);
5795   }
5796   derivedTypeInfo_.sequence = true;
5797   return false;
5798 }
5799 void DeclarationVisitor::Post(const parser::ComponentDecl &x) {
5800   const auto &name{std::get<parser::Name>(x.t)};
5801   auto attrs{GetAttrs()};
5802   if (derivedTypeInfo_.privateComps &&
5803       !attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
5804     attrs.set(Attr::PRIVATE);
5805   }
5806   if (const auto *declType{GetDeclTypeSpec()}) {
5807     if (const auto *derived{declType->AsDerived()}) {
5808       if (!attrs.HasAny({Attr::POINTER, Attr::ALLOCATABLE})) {
5809         if (derivedTypeInfo_.type == &derived->typeSymbol()) { // C744
5810           Say("Recursive use of the derived type requires "
5811               "POINTER or ALLOCATABLE"_err_en_US);
5812         }
5813       }
5814       // TODO: This would be more appropriate in CheckDerivedType()
5815       if (auto it{FindCoarrayUltimateComponent(*derived)}) { // C748
5816         std::string ultimateName{it.BuildResultDesignatorName()};
5817         // Strip off the leading "%"
5818         if (ultimateName.length() > 1) {
5819           ultimateName.erase(0, 1);
5820           if (attrs.HasAny({Attr::POINTER, Attr::ALLOCATABLE})) {
5821             evaluate::AttachDeclaration(
5822                 Say(name.source,
5823                     "A component with a POINTER or ALLOCATABLE attribute may "
5824                     "not "
5825                     "be of a type with a coarray ultimate component (named "
5826                     "'%s')"_err_en_US,
5827                     ultimateName),
5828                 derived->typeSymbol());
5829           }
5830           if (!arraySpec().empty() || !coarraySpec().empty()) {
5831             evaluate::AttachDeclaration(
5832                 Say(name.source,
5833                     "An array or coarray component may not be of a type with a "
5834                     "coarray ultimate component (named '%s')"_err_en_US,
5835                     ultimateName),
5836                 derived->typeSymbol());
5837           }
5838         }
5839       }
5840     }
5841   }
5842   if (OkToAddComponent(name)) {
5843     auto &symbol{DeclareObjectEntity(name, attrs)};
5844     SetCUDADataAttr(name.source, symbol, cudaDataAttr());
5845     if (symbol.has<ObjectEntityDetails>()) {
5846       if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
5847         Initialization(name, *init, true);
5848       }
5849     }
5850     currScope().symbol()->get<DerivedTypeDetails>().add_component(symbol);
5851   }
5852   ClearArraySpec();
5853   ClearCoarraySpec();
5854 }
5855 void DeclarationVisitor::Post(const parser::FillDecl &x) {
5856   // Replace "%FILL" with a distinct generated name
5857   const auto &name{std::get<parser::Name>(x.t)};
5858   const_cast<SourceName &>(name.source) = context().GetTempName(currScope());
5859   if (OkToAddComponent(name)) {
5860     auto &symbol{DeclareObjectEntity(name, GetAttrs())};
5861     currScope().symbol()->get<DerivedTypeDetails>().add_component(symbol);
5862   }
5863   ClearArraySpec();
5864 }
5865 bool DeclarationVisitor::Pre(const parser::ProcedureDeclarationStmt &x) {
5866   CHECK(!interfaceName_);
5867   const auto &procAttrSpec{std::get<std::list<parser::ProcAttrSpec>>(x.t)};
5868   for (const parser::ProcAttrSpec &procAttr : procAttrSpec) {
5869     if (auto *bindC{std::get_if<parser::LanguageBindingSpec>(&procAttr.u)}) {
5870       if (std::get<std::optional<parser::ScalarDefaultCharConstantExpr>>(
5871               bindC->t)
5872               .has_value()) {
5873         if (std::get<std::list<parser::ProcDecl>>(x.t).size() > 1) {
5874           Say(context().location().value(),
5875               "A procedure declaration statement with a binding name may not declare multiple procedures"_err_en_US);
5876         }
5877         break;
5878       }
5879     }
5880   }
5881   return BeginDecl();
5882 }
5883 void DeclarationVisitor::Post(const parser::ProcedureDeclarationStmt &) {
5884   interfaceName_ = nullptr;
5885   EndDecl();
5886 }
5887 bool DeclarationVisitor::Pre(const parser::DataComponentDefStmt &x) {
5888   // Overrides parse tree traversal so as to handle attributes first,
5889   // so POINTER & ALLOCATABLE enable forward references to derived types.
5890   Walk(std::get<std::list<parser::ComponentAttrSpec>>(x.t));
5891   set_allowForwardReferenceToDerivedType(
5892       GetAttrs().HasAny({Attr::POINTER, Attr::ALLOCATABLE}));
5893   Walk(std::get<parser::DeclarationTypeSpec>(x.t));
5894   set_allowForwardReferenceToDerivedType(false);
5895   if (derivedTypeInfo_.sequence) { // C740
5896     if (const auto *declType{GetDeclTypeSpec()}) {
5897       if (!declType->AsIntrinsic() && !declType->IsSequenceType() &&
5898           !InModuleFile()) {
5899         if (GetAttrs().test(Attr::POINTER) &&
5900             context().IsEnabled(common::LanguageFeature::PointerInSeqType)) {
5901           context().Warn(common::LanguageFeature::PointerInSeqType,
5902               "A sequence type data component that is a pointer to a non-sequence type is not standard"_port_en_US);
5903         } else {
5904           Say("A sequence type data component must either be of an intrinsic type or a derived sequence type"_err_en_US);
5905         }
5906       }
5907     }
5908   }
5909   Walk(std::get<std::list<parser::ComponentOrFill>>(x.t));
5910   return false;
5911 }
5912 bool DeclarationVisitor::Pre(const parser::ProcComponentDefStmt &) {
5913   CHECK(!interfaceName_);
5914   return true;
5915 }
5916 void DeclarationVisitor::Post(const parser::ProcComponentDefStmt &) {
5917   interfaceName_ = nullptr;
5918 }
5919 bool DeclarationVisitor::Pre(const parser::ProcPointerInit &x) {
5920   if (auto *name{std::get_if<parser::Name>(&x.u)}) {
5921     return !NameIsKnownOrIntrinsic(*name) && !CheckUseError(*name);
5922   } else {
5923     const auto &null{DEREF(std::get_if<parser::NullInit>(&x.u))};
5924     Walk(null);
5925     if (auto nullInit{EvaluateExpr(null)}) {
5926       if (!evaluate::IsNullPointer(*nullInit)) {
5927         Say(null.v.value().source,
5928             "Procedure pointer initializer must be a name or intrinsic NULL()"_err_en_US);
5929       }
5930     }
5931     return false;
5932   }
5933 }
5934 void DeclarationVisitor::Post(const parser::ProcInterface &x) {
5935   if (auto *name{std::get_if<parser::Name>(&x.u)}) {
5936     interfaceName_ = name;
5937     NoteInterfaceName(*name);
5938   }
5939 }
5940 void DeclarationVisitor::Post(const parser::ProcDecl &x) {
5941   const auto &name{std::get<parser::Name>(x.t)};
5942   // Don't use BypassGeneric or GetUltimate on this symbol, they can
5943   // lead to unusable names in module files.
5944   const Symbol *procInterface{
5945       interfaceName_ ? interfaceName_->symbol : nullptr};
5946   auto attrs{HandleSaveName(name.source, GetAttrs())};
5947   DerivedTypeDetails *dtDetails{nullptr};
5948   if (Symbol * symbol{currScope().symbol()}) {
5949     dtDetails = symbol->detailsIf<DerivedTypeDetails>();
5950   }
5951   if (!dtDetails) {
5952     attrs.set(Attr::EXTERNAL);
5953   }
5954   Symbol &symbol{DeclareProcEntity(name, attrs, procInterface)};
5955   SetCUDADataAttr(name.source, symbol, cudaDataAttr()); // for error
5956   symbol.ReplaceName(name.source);
5957   if (dtDetails) {
5958     dtDetails->add_component(symbol);
5959   }
5960   DeclaredPossibleSpecificProc(symbol);
5961 }
5962 
5963 bool DeclarationVisitor::Pre(const parser::TypeBoundProcedurePart &) {
5964   derivedTypeInfo_.sawContains = true;
5965   return true;
5966 }
5967 
5968 // Resolve binding names from type-bound generics, saved in genericBindings_.
5969 void DeclarationVisitor::Post(const parser::TypeBoundProcedurePart &) {
5970   // track specifics seen for the current generic to detect duplicates:
5971   const Symbol *currGeneric{nullptr};
5972   std::set<SourceName> specifics;
5973   for (const auto &[generic, bindingName] : genericBindings_) {
5974     if (generic != currGeneric) {
5975       currGeneric = generic;
5976       specifics.clear();
5977     }
5978     auto [it, inserted]{specifics.insert(bindingName->source)};
5979     if (!inserted) {
5980       Say(*bindingName, // C773
5981           "Binding name '%s' was already specified for generic '%s'"_err_en_US,
5982           bindingName->source, generic->name())
5983           .Attach(*it, "Previous specification of '%s'"_en_US, *it);
5984       continue;
5985     }
5986     auto *symbol{FindInTypeOrParents(*bindingName)};
5987     if (!symbol) {
5988       Say(*bindingName, // C772
5989           "Binding name '%s' not found in this derived type"_err_en_US);
5990     } else if (!symbol->has<ProcBindingDetails>()) {
5991       SayWithDecl(*bindingName, *symbol, // C772
5992           "'%s' is not the name of a specific binding of this type"_err_en_US);
5993     } else {
5994       generic->get<GenericDetails>().AddSpecificProc(
5995           *symbol, bindingName->source);
5996     }
5997   }
5998   genericBindings_.clear();
5999 }
6000 
6001 void DeclarationVisitor::Post(const parser::ContainsStmt &) {
6002   if (derivedTypeInfo_.sequence) {
6003     Say("A sequence type may not have a CONTAINS statement"_err_en_US); // C740
6004   }
6005 }
6006 
6007 void DeclarationVisitor::Post(
6008     const parser::TypeBoundProcedureStmt::WithoutInterface &x) {
6009   if (GetAttrs().test(Attr::DEFERRED)) { // C783
6010     Say("DEFERRED is only allowed when an interface-name is provided"_err_en_US);
6011   }
6012   for (auto &declaration : x.declarations) {
6013     auto &bindingName{std::get<parser::Name>(declaration.t)};
6014     auto &optName{std::get<std::optional<parser::Name>>(declaration.t)};
6015     const parser::Name &procedureName{optName ? *optName : bindingName};
6016     Symbol *procedure{FindSymbol(procedureName)};
6017     if (!procedure) {
6018       procedure = NoteInterfaceName(procedureName);
6019     }
6020     if (procedure) {
6021       const Symbol &bindTo{BypassGeneric(*procedure)};
6022       if (auto *s{MakeTypeSymbol(bindingName, ProcBindingDetails{bindTo})}) {
6023         SetPassNameOn(*s);
6024         if (GetAttrs().test(Attr::DEFERRED)) {
6025           context().SetError(*s);
6026         }
6027       }
6028     }
6029   }
6030 }
6031 
6032 void DeclarationVisitor::CheckBindings(
6033     const parser::TypeBoundProcedureStmt::WithoutInterface &tbps) {
6034   CHECK(currScope().IsDerivedType());
6035   for (auto &declaration : tbps.declarations) {
6036     auto &bindingName{std::get<parser::Name>(declaration.t)};
6037     if (Symbol * binding{FindInScope(bindingName)}) {
6038       if (auto *details{binding->detailsIf<ProcBindingDetails>()}) {
6039         const Symbol &ultimate{details->symbol().GetUltimate()};
6040         const Symbol &procedure{BypassGeneric(ultimate)};
6041         if (&procedure != &ultimate) {
6042           details->ReplaceSymbol(procedure);
6043         }
6044         if (!CanBeTypeBoundProc(procedure)) {
6045           if (details->symbol().name() != binding->name()) {
6046             Say(binding->name(),
6047                 "The binding of '%s' ('%s') must be either an accessible "
6048                 "module procedure or an external procedure with "
6049                 "an explicit interface"_err_en_US,
6050                 binding->name(), details->symbol().name());
6051           } else {
6052             Say(binding->name(),
6053                 "'%s' must be either an accessible module procedure "
6054                 "or an external procedure with an explicit interface"_err_en_US,
6055                 binding->name());
6056           }
6057           context().SetError(*binding);
6058         }
6059       }
6060     }
6061   }
6062 }
6063 
6064 void DeclarationVisitor::Post(
6065     const parser::TypeBoundProcedureStmt::WithInterface &x) {
6066   if (!GetAttrs().test(Attr::DEFERRED)) { // C783
6067     Say("DEFERRED is required when an interface-name is provided"_err_en_US);
6068   }
6069   if (Symbol * interface{NoteInterfaceName(x.interfaceName)}) {
6070     for (auto &bindingName : x.bindingNames) {
6071       if (auto *s{
6072               MakeTypeSymbol(bindingName, ProcBindingDetails{*interface})}) {
6073         SetPassNameOn(*s);
6074         if (!GetAttrs().test(Attr::DEFERRED)) {
6075           context().SetError(*s);
6076         }
6077       }
6078     }
6079   }
6080 }
6081 
6082 bool DeclarationVisitor::Pre(const parser::FinalProcedureStmt &x) {
6083   if (currScope().IsDerivedType() && currScope().symbol()) {
6084     if (auto *details{currScope().symbol()->detailsIf<DerivedTypeDetails>()}) {
6085       for (const auto &subrName : x.v) {
6086         Symbol *symbol{FindSymbol(subrName)};
6087         if (!symbol) {
6088           // FINAL procedures must be module subroutines
6089           symbol = &MakeSymbol(
6090               currScope().parent(), subrName.source, Attrs{Attr::MODULE});
6091           Resolve(subrName, symbol);
6092           symbol->set_details(ProcEntityDetails{});
6093           symbol->set(Symbol::Flag::Subroutine);
6094         }
6095         if (auto pair{details->finals().emplace(subrName.source, *symbol)};
6096             !pair.second) { // C787
6097           Say(subrName.source,
6098               "FINAL subroutine '%s' already appeared in this derived type"_err_en_US,
6099               subrName.source)
6100               .Attach(pair.first->first,
6101                   "earlier appearance of this FINAL subroutine"_en_US);
6102         }
6103       }
6104     }
6105   }
6106   return false;
6107 }
6108 
6109 bool DeclarationVisitor::Pre(const parser::TypeBoundGenericStmt &x) {
6110   const auto &accessSpec{std::get<std::optional<parser::AccessSpec>>(x.t)};
6111   const auto &genericSpec{std::get<Indirection<parser::GenericSpec>>(x.t)};
6112   const auto &bindingNames{std::get<std::list<parser::Name>>(x.t)};
6113   GenericSpecInfo info{genericSpec.value()};
6114   SourceName symbolName{info.symbolName()};
6115   bool isPrivate{accessSpec ? accessSpec->v == parser::AccessSpec::Kind::Private
6116                             : derivedTypeInfo_.privateBindings};
6117   auto *genericSymbol{FindInScope(symbolName)};
6118   if (genericSymbol) {
6119     if (!genericSymbol->has<GenericDetails>()) {
6120       genericSymbol = nullptr; // MakeTypeSymbol will report the error below
6121     }
6122   } else {
6123     // look in ancestor types for a generic of the same name
6124     for (const auto &name : GetAllNames(context(), symbolName)) {
6125       if (Symbol * inherited{currScope().FindComponent(SourceName{name})}) {
6126         if (inherited->has<GenericDetails>()) {
6127           CheckAccessibility(symbolName, isPrivate, *inherited); // C771
6128         } else {
6129           Say(symbolName,
6130               "Type bound generic procedure '%s' may not have the same name as a non-generic symbol inherited from an ancestor type"_err_en_US)
6131               .Attach(inherited->name(), "Inherited symbol"_en_US);
6132         }
6133         break;
6134       }
6135     }
6136   }
6137   if (genericSymbol) {
6138     CheckAccessibility(symbolName, isPrivate, *genericSymbol); // C771
6139   } else {
6140     genericSymbol = MakeTypeSymbol(symbolName, GenericDetails{});
6141     if (!genericSymbol) {
6142       return false;
6143     }
6144     if (isPrivate) {
6145       SetExplicitAttr(*genericSymbol, Attr::PRIVATE);
6146     }
6147   }
6148   for (const parser::Name &bindingName : bindingNames) {
6149     genericBindings_.emplace(genericSymbol, &bindingName);
6150   }
6151   info.Resolve(genericSymbol);
6152   return false;
6153 }
6154 
6155 // DEC STRUCTUREs are handled thus to allow for nested definitions.
6156 bool DeclarationVisitor::Pre(const parser::StructureDef &def) {
6157   const auto &structureStatement{
6158       std::get<parser::Statement<parser::StructureStmt>>(def.t)};
6159   auto saveDerivedTypeInfo{derivedTypeInfo_};
6160   derivedTypeInfo_ = {};
6161   derivedTypeInfo_.isStructure = true;
6162   derivedTypeInfo_.sequence = true;
6163   Scope *previousStructure{nullptr};
6164   if (saveDerivedTypeInfo.isStructure) {
6165     previousStructure = &currScope();
6166     PopScope();
6167   }
6168   const parser::StructureStmt &structStmt{structureStatement.statement};
6169   const auto &name{std::get<std::optional<parser::Name>>(structStmt.t)};
6170   if (!name) {
6171     // Construct a distinct generated name for an anonymous structure
6172     auto &mutableName{const_cast<std::optional<parser::Name> &>(name)};
6173     mutableName.emplace(
6174         parser::Name{context().GetTempName(currScope()), nullptr});
6175   }
6176   auto &symbol{MakeSymbol(*name, DerivedTypeDetails{})};
6177   symbol.ReplaceName(name->source);
6178   symbol.get<DerivedTypeDetails>().set_sequence(true);
6179   symbol.get<DerivedTypeDetails>().set_isDECStructure(true);
6180   derivedTypeInfo_.type = &symbol;
6181   PushScope(Scope::Kind::DerivedType, &symbol);
6182   const auto &fields{std::get<std::list<parser::StructureField>>(def.t)};
6183   Walk(fields);
6184   PopScope();
6185   // Complete the definition
6186   DerivedTypeSpec derivedTypeSpec{symbol.name(), symbol};
6187   derivedTypeSpec.set_scope(DEREF(symbol.scope()));
6188   derivedTypeSpec.CookParameters(GetFoldingContext());
6189   derivedTypeSpec.EvaluateParameters(context());
6190   DeclTypeSpec &type{currScope().MakeDerivedType(
6191       DeclTypeSpec::TypeDerived, std::move(derivedTypeSpec))};
6192   type.derivedTypeSpec().Instantiate(currScope());
6193   // Restore previous structure definition context, if any
6194   derivedTypeInfo_ = saveDerivedTypeInfo;
6195   if (previousStructure) {
6196     PushScope(*previousStructure);
6197   }
6198   // Handle any entity declarations on the STRUCTURE statement
6199   const auto &decls{std::get<std::list<parser::EntityDecl>>(structStmt.t)};
6200   if (!decls.empty()) {
6201     BeginDecl();
6202     SetDeclTypeSpec(type);
6203     Walk(decls);
6204     EndDecl();
6205   }
6206   return false;
6207 }
6208 
6209 bool DeclarationVisitor::Pre(const parser::Union::UnionStmt &) {
6210   Say("support for UNION"_todo_en_US); // TODO
6211   return true;
6212 }
6213 
6214 bool DeclarationVisitor::Pre(const parser::StructureField &x) {
6215   if (std::holds_alternative<parser::Statement<parser::DataComponentDefStmt>>(
6216           x.u)) {
6217     BeginDecl();
6218   }
6219   return true;
6220 }
6221 
6222 void DeclarationVisitor::Post(const parser::StructureField &x) {
6223   if (std::holds_alternative<parser::Statement<parser::DataComponentDefStmt>>(
6224           x.u)) {
6225     EndDecl();
6226   }
6227 }
6228 
6229 bool DeclarationVisitor::Pre(const parser::AllocateStmt &) {
6230   BeginDeclTypeSpec();
6231   return true;
6232 }
6233 void DeclarationVisitor::Post(const parser::AllocateStmt &) {
6234   EndDeclTypeSpec();
6235 }
6236 
6237 bool DeclarationVisitor::Pre(const parser::StructureConstructor &x) {
6238   auto &parsedType{std::get<parser::DerivedTypeSpec>(x.t)};
6239   const DeclTypeSpec *type{ProcessTypeSpec(parsedType)};
6240   if (!type) {
6241     return false;
6242   }
6243   const DerivedTypeSpec *spec{type->AsDerived()};
6244   const Scope *typeScope{spec ? spec->scope() : nullptr};
6245   if (!typeScope) {
6246     return false;
6247   }
6248 
6249   // N.B C7102 is implicitly enforced by having inaccessible types not
6250   // being found in resolution.
6251   // More constraints are enforced in expression.cpp so that they
6252   // can apply to structure constructors that have been converted
6253   // from misparsed function references.
6254   for (const auto &component :
6255       std::get<std::list<parser::ComponentSpec>>(x.t)) {
6256     // Visit the component spec expression, but not the keyword, since
6257     // we need to resolve its symbol in the scope of the derived type.
6258     Walk(std::get<parser::ComponentDataSource>(component.t));
6259     if (const auto &kw{std::get<std::optional<parser::Keyword>>(component.t)}) {
6260       FindInTypeOrParents(*typeScope, kw->v);
6261     }
6262   }
6263   return false;
6264 }
6265 
6266 bool DeclarationVisitor::Pre(const parser::BasedPointer &) {
6267   BeginArraySpec();
6268   return true;
6269 }
6270 
6271 void DeclarationVisitor::Post(const parser::BasedPointer &bp) {
6272   const parser::ObjectName &pointerName{std::get<0>(bp.t)};
6273   auto *pointer{FindSymbol(pointerName)};
6274   if (!pointer) {
6275     pointer = &MakeSymbol(pointerName, ObjectEntityDetails{});
6276   } else if (!ConvertToObjectEntity(*pointer)) {
6277     SayWithDecl(pointerName, *pointer, "'%s' is not a variable"_err_en_US);
6278   } else if (IsNamedConstant(*pointer)) {
6279     SayWithDecl(pointerName, *pointer,
6280         "'%s' is a named constant and may not be a Cray pointer"_err_en_US);
6281   } else if (pointer->Rank() > 0) {
6282     SayWithDecl(
6283         pointerName, *pointer, "Cray pointer '%s' must be a scalar"_err_en_US);
6284   } else if (pointer->test(Symbol::Flag::CrayPointee)) {
6285     Say(pointerName,
6286         "'%s' cannot be a Cray pointer as it is already a Cray pointee"_err_en_US);
6287   }
6288   pointer->set(Symbol::Flag::CrayPointer);
6289   const DeclTypeSpec &pointerType{MakeNumericType(
6290       TypeCategory::Integer, context().defaultKinds().subscriptIntegerKind())};
6291   const auto *type{pointer->GetType()};
6292   if (!type) {
6293     pointer->SetType(pointerType);
6294   } else if (*type != pointerType) {
6295     Say(pointerName.source, "Cray pointer '%s' must have type %s"_err_en_US,
6296         pointerName.source, pointerType.AsFortran());
6297   }
6298   const parser::ObjectName &pointeeName{std::get<1>(bp.t)};
6299   DeclareObjectEntity(pointeeName);
6300   if (Symbol * pointee{pointeeName.symbol}) {
6301     if (!ConvertToObjectEntity(*pointee)) {
6302       return;
6303     }
6304     if (IsNamedConstant(*pointee)) {
6305       Say(pointeeName,
6306           "'%s' is a named constant and may not be a Cray pointee"_err_en_US);
6307       return;
6308     }
6309     if (pointee->test(Symbol::Flag::CrayPointer)) {
6310       Say(pointeeName,
6311           "'%s' cannot be a Cray pointee as it is already a Cray pointer"_err_en_US);
6312     } else if (pointee->test(Symbol::Flag::CrayPointee)) {
6313       Say(pointeeName, "'%s' was already declared as a Cray pointee"_err_en_US);
6314     } else {
6315       pointee->set(Symbol::Flag::CrayPointee);
6316     }
6317     if (const auto *pointeeType{pointee->GetType()}) {
6318       if (const auto *derived{pointeeType->AsDerived()}) {
6319         if (!IsSequenceOrBindCType(derived)) {
6320           context().Warn(common::LanguageFeature::NonSequenceCrayPointee,
6321               pointeeName.source,
6322               "Type of Cray pointee '%s' is a derived type that is neither SEQUENCE nor BIND(C)"_warn_en_US,
6323               pointeeName.source);
6324         }
6325       }
6326     }
6327     currScope().add_crayPointer(pointeeName.source, *pointer);
6328   }
6329 }
6330 
6331 bool DeclarationVisitor::Pre(const parser::NamelistStmt::Group &x) {
6332   if (!CheckNotInBlock("NAMELIST")) { // C1107
6333     return false;
6334   }
6335   const auto &groupName{std::get<parser::Name>(x.t)};
6336   auto *groupSymbol{FindInScope(groupName)};
6337   if (!groupSymbol || !groupSymbol->has<NamelistDetails>()) {
6338     groupSymbol = &MakeSymbol(groupName, NamelistDetails{});
6339     groupSymbol->ReplaceName(groupName.source);
6340   }
6341   // Name resolution of group items is deferred to FinishNamelists()
6342   // so that host association is handled correctly.
6343   GetDeferredDeclarationState(true)->namelistGroups.emplace_back(&x);
6344   return false;
6345 }
6346 
6347 void DeclarationVisitor::FinishNamelists() {
6348   if (auto *deferred{GetDeferredDeclarationState()}) {
6349     for (const parser::NamelistStmt::Group *group : deferred->namelistGroups) {
6350       if (auto *groupSymbol{FindInScope(std::get<parser::Name>(group->t))}) {
6351         if (auto *details{groupSymbol->detailsIf<NamelistDetails>()}) {
6352           for (const auto &name : std::get<std::list<parser::Name>>(group->t)) {
6353             auto *symbol{FindSymbol(name)};
6354             if (!symbol) {
6355               symbol = &MakeSymbol(name, ObjectEntityDetails{});
6356               ApplyImplicitRules(*symbol);
6357             } else if (!ConvertToObjectEntity(symbol->GetUltimate())) {
6358               SayWithDecl(name, *symbol, "'%s' is not a variable"_err_en_US);
6359               context().SetError(*groupSymbol);
6360             }
6361             symbol->GetUltimate().set(Symbol::Flag::InNamelist);
6362             details->add_object(*symbol);
6363           }
6364         }
6365       }
6366     }
6367     deferred->namelistGroups.clear();
6368   }
6369 }
6370 
6371 bool DeclarationVisitor::Pre(const parser::IoControlSpec &x) {
6372   if (const auto *name{std::get_if<parser::Name>(&x.u)}) {
6373     auto *symbol{FindSymbol(*name)};
6374     if (!symbol) {
6375       Say(*name, "Namelist group '%s' not found"_err_en_US);
6376     } else if (!symbol->GetUltimate().has<NamelistDetails>()) {
6377       SayWithDecl(
6378           *name, *symbol, "'%s' is not the name of a namelist group"_err_en_US);
6379     }
6380   }
6381   return true;
6382 }
6383 
6384 bool DeclarationVisitor::Pre(const parser::CommonStmt::Block &x) {
6385   CheckNotInBlock("COMMON"); // C1107
6386   return true;
6387 }
6388 
6389 bool DeclarationVisitor::Pre(const parser::CommonBlockObject &) {
6390   BeginArraySpec();
6391   return true;
6392 }
6393 
6394 void DeclarationVisitor::Post(const parser::CommonBlockObject &x) {
6395   const auto &name{std::get<parser::Name>(x.t)};
6396   DeclareObjectEntity(name);
6397   auto pair{specPartState_.commonBlockObjects.insert(name.source)};
6398   if (!pair.second) {
6399     const SourceName &prev{*pair.first};
6400     Say2(name.source, "'%s' is already in a COMMON block"_err_en_US, prev,
6401         "Previous occurrence of '%s' in a COMMON block"_en_US);
6402   }
6403 }
6404 
6405 bool DeclarationVisitor::Pre(const parser::EquivalenceStmt &x) {
6406   // save equivalence sets to be processed after specification part
6407   if (CheckNotInBlock("EQUIVALENCE")) { // C1107
6408     for (const std::list<parser::EquivalenceObject> &set : x.v) {
6409       specPartState_.equivalenceSets.push_back(&set);
6410     }
6411   }
6412   return false; // don't implicitly declare names yet
6413 }
6414 
6415 void DeclarationVisitor::CheckEquivalenceSets() {
6416   EquivalenceSets equivSets{context()};
6417   inEquivalenceStmt_ = true;
6418   for (const auto *set : specPartState_.equivalenceSets) {
6419     const auto &source{set->front().v.value().source};
6420     if (set->size() <= 1) { // R871
6421       Say(source, "Equivalence set must have more than one object"_err_en_US);
6422     }
6423     for (const parser::EquivalenceObject &object : *set) {
6424       const auto &designator{object.v.value()};
6425       // The designator was not resolved when it was encountered, so do it now.
6426       // AnalyzeExpr causes array sections to be changed to substrings as needed
6427       Walk(designator);
6428       if (AnalyzeExpr(context(), designator)) {
6429         equivSets.AddToSet(designator);
6430       }
6431     }
6432     equivSets.FinishSet(source);
6433   }
6434   inEquivalenceStmt_ = false;
6435   for (auto &set : equivSets.sets()) {
6436     if (!set.empty()) {
6437       currScope().add_equivalenceSet(std::move(set));
6438     }
6439   }
6440   specPartState_.equivalenceSets.clear();
6441 }
6442 
6443 bool DeclarationVisitor::Pre(const parser::SaveStmt &x) {
6444   if (x.v.empty()) {
6445     specPartState_.saveInfo.saveAll = currStmtSource();
6446     currScope().set_hasSAVE();
6447   } else {
6448     for (const parser::SavedEntity &y : x.v) {
6449       auto kind{std::get<parser::SavedEntity::Kind>(y.t)};
6450       const auto &name{std::get<parser::Name>(y.t)};
6451       if (kind == parser::SavedEntity::Kind::Common) {
6452         MakeCommonBlockSymbol(name);
6453         AddSaveName(specPartState_.saveInfo.commons, name.source);
6454       } else {
6455         HandleAttributeStmt(Attr::SAVE, name);
6456       }
6457     }
6458   }
6459   return false;
6460 }
6461 
6462 void DeclarationVisitor::CheckSaveStmts() {
6463   for (const SourceName &name : specPartState_.saveInfo.entities) {
6464     auto *symbol{FindInScope(name)};
6465     if (!symbol) {
6466       // error was reported
6467     } else if (specPartState_.saveInfo.saveAll) {
6468       // C889 - note that pgi, ifort, xlf do not enforce this constraint
6469       if (context().ShouldWarn(common::LanguageFeature::RedundantAttribute)) {
6470         Say2(name,
6471             "Explicit SAVE of '%s' is redundant due to global SAVE statement"_warn_en_US,
6472             *specPartState_.saveInfo.saveAll, "Global SAVE statement"_en_US)
6473             .set_languageFeature(common::LanguageFeature::RedundantAttribute);
6474       }
6475     } else if (!IsSaved(*symbol)) {
6476       SetExplicitAttr(*symbol, Attr::SAVE);
6477     }
6478   }
6479   for (const SourceName &name : specPartState_.saveInfo.commons) {
6480     if (auto *symbol{currScope().FindCommonBlock(name)}) {
6481       auto &objects{symbol->get<CommonBlockDetails>().objects()};
6482       if (objects.empty()) {
6483         if (currScope().kind() != Scope::Kind::BlockConstruct) {
6484           Say(name,
6485               "'%s' appears as a COMMON block in a SAVE statement but not in"
6486               " a COMMON statement"_err_en_US);
6487         } else { // C1108
6488           Say(name,
6489               "SAVE statement in BLOCK construct may not contain a"
6490               " common block name '%s'"_err_en_US);
6491         }
6492       } else {
6493         for (auto &object : symbol->get<CommonBlockDetails>().objects()) {
6494           if (!IsSaved(*object)) {
6495             SetImplicitAttr(*object, Attr::SAVE);
6496           }
6497         }
6498       }
6499     }
6500   }
6501   specPartState_.saveInfo = {};
6502 }
6503 
6504 // Record SAVEd names in specPartState_.saveInfo.entities.
6505 Attrs DeclarationVisitor::HandleSaveName(const SourceName &name, Attrs attrs) {
6506   if (attrs.test(Attr::SAVE)) {
6507     AddSaveName(specPartState_.saveInfo.entities, name);
6508   }
6509   return attrs;
6510 }
6511 
6512 // Record a name in a set of those to be saved.
6513 void DeclarationVisitor::AddSaveName(
6514     std::set<SourceName> &set, const SourceName &name) {
6515   auto pair{set.insert(name)};
6516   if (!pair.second &&
6517       context().ShouldWarn(common::LanguageFeature::RedundantAttribute)) {
6518     Say2(name, "SAVE attribute was already specified on '%s'"_warn_en_US,
6519         *pair.first, "Previous specification of SAVE attribute"_en_US)
6520         .set_languageFeature(common::LanguageFeature::RedundantAttribute);
6521   }
6522 }
6523 
6524 // Check types of common block objects, now that they are known.
6525 void DeclarationVisitor::CheckCommonBlocks() {
6526   // check for empty common blocks
6527   for (const auto &pair : currScope().commonBlocks()) {
6528     const auto &symbol{*pair.second};
6529     if (symbol.get<CommonBlockDetails>().objects().empty() &&
6530         symbol.attrs().test(Attr::BIND_C)) {
6531       Say(symbol.name(),
6532           "'%s' appears as a COMMON block in a BIND statement but not in"
6533           " a COMMON statement"_err_en_US);
6534     }
6535   }
6536   // check objects in common blocks
6537   for (const auto &name : specPartState_.commonBlockObjects) {
6538     const auto *symbol{currScope().FindSymbol(name)};
6539     if (!symbol) {
6540       continue;
6541     }
6542     const auto &attrs{symbol->attrs()};
6543     if (attrs.test(Attr::ALLOCATABLE)) {
6544       Say(name,
6545           "ALLOCATABLE object '%s' may not appear in a COMMON block"_err_en_US);
6546     } else if (attrs.test(Attr::BIND_C)) {
6547       Say(name,
6548           "Variable '%s' with BIND attribute may not appear in a COMMON block"_err_en_US);
6549     } else if (IsNamedConstant(*symbol)) {
6550       Say(name,
6551           "A named constant '%s' may not appear in a COMMON block"_err_en_US);
6552     } else if (IsDummy(*symbol)) {
6553       Say(name,
6554           "Dummy argument '%s' may not appear in a COMMON block"_err_en_US);
6555     } else if (symbol->IsFuncResult()) {
6556       Say(name,
6557           "Function result '%s' may not appear in a COMMON block"_err_en_US);
6558     } else if (const DeclTypeSpec * type{symbol->GetType()}) {
6559       if (type->category() == DeclTypeSpec::ClassStar) {
6560         Say(name,
6561             "Unlimited polymorphic pointer '%s' may not appear in a COMMON block"_err_en_US);
6562       } else if (const auto *derived{type->AsDerived()}) {
6563         if (!IsSequenceOrBindCType(derived)) {
6564           Say(name,
6565               "Derived type '%s' in COMMON block must have the BIND or"
6566               " SEQUENCE attribute"_err_en_US);
6567         }
6568         UnorderedSymbolSet typeSet;
6569         CheckCommonBlockDerivedType(name, derived->typeSymbol(), typeSet);
6570       }
6571     }
6572   }
6573   specPartState_.commonBlockObjects = {};
6574 }
6575 
6576 Symbol &DeclarationVisitor::MakeCommonBlockSymbol(const parser::Name &name) {
6577   return Resolve(name, currScope().MakeCommonBlock(name.source));
6578 }
6579 Symbol &DeclarationVisitor::MakeCommonBlockSymbol(
6580     const std::optional<parser::Name> &name) {
6581   if (name) {
6582     return MakeCommonBlockSymbol(*name);
6583   } else {
6584     return MakeCommonBlockSymbol(parser::Name{});
6585   }
6586 }
6587 
6588 bool DeclarationVisitor::NameIsKnownOrIntrinsic(const parser::Name &name) {
6589   return FindSymbol(name) || HandleUnrestrictedSpecificIntrinsicFunction(name);
6590 }
6591 
6592 // Check if this derived type can be in a COMMON block.
6593 void DeclarationVisitor::CheckCommonBlockDerivedType(const SourceName &name,
6594     const Symbol &typeSymbol, UnorderedSymbolSet &typeSet) {
6595   if (auto iter{typeSet.find(SymbolRef{typeSymbol})}; iter != typeSet.end()) {
6596     return;
6597   }
6598   typeSet.emplace(typeSymbol);
6599   if (const auto *scope{typeSymbol.scope()}) {
6600     for (const auto &pair : *scope) {
6601       const Symbol &component{*pair.second};
6602       if (component.attrs().test(Attr::ALLOCATABLE)) {
6603         Say2(name,
6604             "Derived type variable '%s' may not appear in a COMMON block"
6605             " due to ALLOCATABLE component"_err_en_US,
6606             component.name(), "Component with ALLOCATABLE attribute"_en_US);
6607         return;
6608       }
6609       const auto *details{component.detailsIf<ObjectEntityDetails>()};
6610       if (component.test(Symbol::Flag::InDataStmt) ||
6611           (details && details->init())) {
6612         Say2(name,
6613             "Derived type variable '%s' may not appear in a COMMON block due to component with default initialization"_err_en_US,
6614             component.name(), "Component with default initialization"_en_US);
6615         return;
6616       }
6617       if (details) {
6618         if (const auto *type{details->type()}) {
6619           if (const auto *derived{type->AsDerived()}) {
6620             const Symbol &derivedTypeSymbol{derived->typeSymbol()};
6621             CheckCommonBlockDerivedType(name, derivedTypeSymbol, typeSet);
6622           }
6623         }
6624       }
6625     }
6626   }
6627 }
6628 
6629 bool DeclarationVisitor::HandleUnrestrictedSpecificIntrinsicFunction(
6630     const parser::Name &name) {
6631   if (auto interface{context().intrinsics().IsSpecificIntrinsicFunction(
6632           name.source.ToString())}) {
6633     // Unrestricted specific intrinsic function names (e.g., "cos")
6634     // are acceptable as procedure interfaces.  The presence of the
6635     // INTRINSIC flag will cause this symbol to have a complete interface
6636     // recreated for it later on demand, but capturing its result type here
6637     // will make GetType() return a correct result without having to
6638     // probe the intrinsics table again.
6639     Symbol &symbol{MakeSymbol(InclusiveScope(), name.source, Attrs{})};
6640     SetImplicitAttr(symbol, Attr::INTRINSIC);
6641     CHECK(interface->functionResult.has_value());
6642     evaluate::DynamicType dyType{
6643         DEREF(interface->functionResult->GetTypeAndShape()).type()};
6644     CHECK(common::IsNumericTypeCategory(dyType.category()));
6645     const DeclTypeSpec &typeSpec{
6646         MakeNumericType(dyType.category(), dyType.kind())};
6647     ProcEntityDetails details;
6648     details.set_type(typeSpec);
6649     symbol.set_details(std::move(details));
6650     symbol.set(Symbol::Flag::Function);
6651     if (interface->IsElemental()) {
6652       SetExplicitAttr(symbol, Attr::ELEMENTAL);
6653     }
6654     if (interface->IsPure()) {
6655       SetExplicitAttr(symbol, Attr::PURE);
6656     }
6657     Resolve(name, symbol);
6658     return true;
6659   } else {
6660     return false;
6661   }
6662 }
6663 
6664 // Checks for all locality-specs: LOCAL, LOCAL_INIT, and SHARED
6665 bool DeclarationVisitor::PassesSharedLocalityChecks(
6666     const parser::Name &name, Symbol &symbol) {
6667   if (!IsVariableName(symbol)) {
6668     SayLocalMustBeVariable(name, symbol); // C1124
6669     return false;
6670   }
6671   if (symbol.owner() == currScope()) { // C1125 and C1126
6672     SayAlreadyDeclared(name, symbol);
6673     return false;
6674   }
6675   return true;
6676 }
6677 
6678 // Checks for locality-specs LOCAL, LOCAL_INIT, and REDUCE
6679 bool DeclarationVisitor::PassesLocalityChecks(
6680     const parser::Name &name, Symbol &symbol, Symbol::Flag flag) {
6681   bool isReduce{flag == Symbol::Flag::LocalityReduce};
6682   const char *specName{
6683       flag == Symbol::Flag::LocalityLocalInit ? "LOCAL_INIT" : "LOCAL"};
6684   if (IsAllocatable(symbol) && !isReduce) { // F'2023 C1130
6685     SayWithDecl(name, symbol,
6686         "ALLOCATABLE variable '%s' not allowed in a %s locality-spec"_err_en_US,
6687         specName);
6688     return false;
6689   }
6690   if (IsOptional(symbol)) { // F'2023 C1130-C1131
6691     SayWithDecl(name, symbol,
6692         "OPTIONAL argument '%s' not allowed in a locality-spec"_err_en_US);
6693     return false;
6694   }
6695   if (IsIntentIn(symbol)) { // F'2023 C1130-C1131
6696     SayWithDecl(name, symbol,
6697         "INTENT IN argument '%s' not allowed in a locality-spec"_err_en_US);
6698     return false;
6699   }
6700   if (IsFinalizable(symbol) && !isReduce) { // F'2023 C1130
6701     SayWithDecl(name, symbol,
6702         "Finalizable variable '%s' not allowed in a %s locality-spec"_err_en_US,
6703         specName);
6704     return false;
6705   }
6706   if (evaluate::IsCoarray(symbol) && !isReduce) { // F'2023 C1130
6707     SayWithDecl(name, symbol,
6708         "Coarray '%s' not allowed in a %s locality-spec"_err_en_US, specName);
6709     return false;
6710   }
6711   if (const DeclTypeSpec * type{symbol.GetType()}) {
6712     if (type->IsPolymorphic() && IsDummy(symbol) && !IsPointer(symbol) &&
6713         !isReduce) { // F'2023 C1130
6714       SayWithDecl(name, symbol,
6715           "Nonpointer polymorphic argument '%s' not allowed in a %s locality-spec"_err_en_US,
6716           specName);
6717       return false;
6718     }
6719   }
6720   if (symbol.attrs().test(Attr::ASYNCHRONOUS) && isReduce) { // F'2023 C1131
6721     SayWithDecl(name, symbol,
6722         "ASYNCHRONOUS variable '%s' not allowed in a REDUCE locality-spec"_err_en_US);
6723     return false;
6724   }
6725   if (symbol.attrs().test(Attr::VOLATILE) && isReduce) { // F'2023 C1131
6726     SayWithDecl(name, symbol,
6727         "VOLATILE variable '%s' not allowed in a REDUCE locality-spec"_err_en_US);
6728     return false;
6729   }
6730   if (IsAssumedSizeArray(symbol)) { // F'2023 C1130-C1131
6731     SayWithDecl(name, symbol,
6732         "Assumed size array '%s' not allowed in a locality-spec"_err_en_US);
6733     return false;
6734   }
6735   if (std::optional<Message> whyNot{WhyNotDefinable(
6736           name.source, currScope(), DefinabilityFlags{}, symbol)}) {
6737     SayWithReason(name, symbol,
6738         "'%s' may not appear in a locality-spec because it is not definable"_err_en_US,
6739         std::move(whyNot->set_severity(parser::Severity::Because)));
6740     return false;
6741   }
6742   return PassesSharedLocalityChecks(name, symbol);
6743 }
6744 
6745 Symbol &DeclarationVisitor::FindOrDeclareEnclosingEntity(
6746     const parser::Name &name) {
6747   Symbol *prev{FindSymbol(name)};
6748   if (!prev) {
6749     // Declare the name as an object in the enclosing scope so that
6750     // the name can't be repurposed there later as something else.
6751     prev = &MakeSymbol(InclusiveScope(), name.source, Attrs{});
6752     ConvertToObjectEntity(*prev);
6753     ApplyImplicitRules(*prev);
6754   }
6755   return *prev;
6756 }
6757 
6758 void DeclarationVisitor::DeclareLocalEntity(
6759     const parser::Name &name, Symbol::Flag flag) {
6760   Symbol &prev{FindOrDeclareEnclosingEntity(name)};
6761   if (PassesLocalityChecks(name, prev, flag)) {
6762     if (auto *symbol{&MakeHostAssocSymbol(name, prev)}) {
6763       symbol->set(flag);
6764     }
6765   }
6766 }
6767 
6768 Symbol *DeclarationVisitor::DeclareStatementEntity(
6769     const parser::DoVariable &doVar,
6770     const std::optional<parser::IntegerTypeSpec> &type) {
6771   const parser::Name &name{doVar.thing.thing};
6772   const DeclTypeSpec *declTypeSpec{nullptr};
6773   if (auto *prev{FindSymbol(name)}) {
6774     if (prev->owner() == currScope()) {
6775       SayAlreadyDeclared(name, *prev);
6776       return nullptr;
6777     }
6778     name.symbol = nullptr;
6779     // F'2023 19.4 p5 ambiguous rule about outer declarations
6780     declTypeSpec = prev->GetType();
6781   }
6782   Symbol &symbol{DeclareEntity<ObjectEntityDetails>(name, {})};
6783   if (!symbol.has<ObjectEntityDetails>()) {
6784     return nullptr; // error was reported in DeclareEntity
6785   }
6786   if (type) {
6787     declTypeSpec = ProcessTypeSpec(*type);
6788   }
6789   if (declTypeSpec) {
6790     // Subtlety: Don't let a "*length" specifier (if any is pending) affect the
6791     // declaration of this implied DO loop control variable.
6792     auto restorer{
6793         common::ScopedSet(charInfo_.length, std::optional<ParamValue>{})};
6794     SetType(name, *declTypeSpec);
6795   } else {
6796     ApplyImplicitRules(symbol);
6797   }
6798   return Resolve(name, &symbol);
6799 }
6800 
6801 // Set the type of an entity or report an error.
6802 void DeclarationVisitor::SetType(
6803     const parser::Name &name, const DeclTypeSpec &type) {
6804   CHECK(name.symbol);
6805   auto &symbol{*name.symbol};
6806   if (charInfo_.length) { // Declaration has "*length" (R723)
6807     auto length{std::move(*charInfo_.length)};
6808     charInfo_.length.reset();
6809     if (type.category() == DeclTypeSpec::Character) {
6810       auto kind{type.characterTypeSpec().kind()};
6811       // Recurse with correct type.
6812       SetType(name,
6813           currScope().MakeCharacterType(std::move(length), std::move(kind)));
6814       return;
6815     } else { // C753
6816       Say(name,
6817           "A length specifier cannot be used to declare the non-character entity '%s'"_err_en_US);
6818     }
6819   }
6820   if (auto *proc{symbol.detailsIf<ProcEntityDetails>()}) {
6821     if (proc->procInterface()) {
6822       Say(name,
6823           "'%s' has an explicit interface and may not also have a type"_err_en_US);
6824       context().SetError(symbol);
6825       return;
6826     }
6827   }
6828   auto *prevType{symbol.GetType()};
6829   if (!prevType) {
6830     if (symbol.test(Symbol::Flag::InDataStmt) && isImplicitNoneType()) {
6831       context().Warn(common::LanguageFeature::ForwardRefImplicitNoneData,
6832           name.source,
6833           "'%s' appeared in a DATA statement before its type was declared under IMPLICIT NONE(TYPE)"_port_en_US,
6834           name.source);
6835     }
6836     symbol.SetType(type);
6837   } else if (symbol.has<UseDetails>()) {
6838     // error recovery case, redeclaration of use-associated name
6839   } else if (HadForwardRef(symbol)) {
6840     // error recovery after use of host-associated name
6841   } else if (!symbol.test(Symbol::Flag::Implicit)) {
6842     SayWithDecl(
6843         name, symbol, "The type of '%s' has already been declared"_err_en_US);
6844     context().SetError(symbol);
6845   } else if (type != *prevType) {
6846     SayWithDecl(name, symbol,
6847         "The type of '%s' has already been implicitly declared"_err_en_US);
6848     context().SetError(symbol);
6849   } else {
6850     symbol.set(Symbol::Flag::Implicit, false);
6851   }
6852 }
6853 
6854 std::optional<DerivedTypeSpec> DeclarationVisitor::ResolveDerivedType(
6855     const parser::Name &name) {
6856   Scope &outer{NonDerivedTypeScope()};
6857   Symbol *symbol{FindSymbol(outer, name)};
6858   Symbol *ultimate{symbol ? &symbol->GetUltimate() : nullptr};
6859   auto *generic{ultimate ? ultimate->detailsIf<GenericDetails>() : nullptr};
6860   if (generic) {
6861     if (Symbol * genDT{generic->derivedType()}) {
6862       symbol = genDT;
6863       generic = nullptr;
6864     }
6865   }
6866   if (!symbol || symbol->has<UnknownDetails>() ||
6867       (generic && &ultimate->owner() == &outer)) {
6868     if (allowForwardReferenceToDerivedType()) {
6869       if (!symbol) {
6870         symbol = &MakeSymbol(outer, name.source, Attrs{});
6871         Resolve(name, *symbol);
6872       } else if (generic) {
6873         // forward ref to type with later homonymous generic
6874         symbol = &outer.MakeSymbol(name.source, Attrs{}, UnknownDetails{});
6875         generic->set_derivedType(*symbol);
6876         name.symbol = symbol;
6877       }
6878       DerivedTypeDetails details;
6879       details.set_isForwardReferenced(true);
6880       symbol->set_details(std::move(details));
6881     } else { // C732
6882       Say(name, "Derived type '%s' not found"_err_en_US);
6883       return std::nullopt;
6884     }
6885   } else if (&DEREF(symbol).owner() != &outer &&
6886       !ultimate->has<GenericDetails>()) {
6887     // Prevent a later declaration in this scope of a host-associated
6888     // type name.
6889     outer.add_importName(name.source);
6890   }
6891   if (CheckUseError(name)) {
6892     return std::nullopt;
6893   } else if (symbol->GetUltimate().has<DerivedTypeDetails>()) {
6894     return DerivedTypeSpec{name.source, *symbol};
6895   } else {
6896     Say(name, "'%s' is not a derived type"_err_en_US);
6897     return std::nullopt;
6898   }
6899 }
6900 
6901 std::optional<DerivedTypeSpec> DeclarationVisitor::ResolveExtendsType(
6902     const parser::Name &typeName, const parser::Name *extendsName) {
6903   if (extendsName) {
6904     if (typeName.source == extendsName->source) {
6905       Say(extendsName->source,
6906           "Derived type '%s' cannot extend itself"_err_en_US);
6907     } else if (auto dtSpec{ResolveDerivedType(*extendsName)}) {
6908       if (!dtSpec->IsForwardReferenced()) {
6909         return dtSpec;
6910       }
6911       Say(typeName.source,
6912           "Derived type '%s' cannot extend type '%s' that has not yet been defined"_err_en_US,
6913           typeName.source, extendsName->source);
6914     }
6915   }
6916   return std::nullopt;
6917 }
6918 
6919 Symbol *DeclarationVisitor::NoteInterfaceName(const parser::Name &name) {
6920   // The symbol is checked later by CheckExplicitInterface() and
6921   // CheckBindings().  It can be a forward reference.
6922   if (!NameIsKnownOrIntrinsic(name)) {
6923     Symbol &symbol{MakeSymbol(InclusiveScope(), name.source, Attrs{})};
6924     Resolve(name, symbol);
6925   }
6926   return name.symbol;
6927 }
6928 
6929 void DeclarationVisitor::CheckExplicitInterface(const parser::Name &name) {
6930   if (const Symbol * symbol{name.symbol}) {
6931     const Symbol &ultimate{symbol->GetUltimate()};
6932     if (!context().HasError(*symbol) && !context().HasError(ultimate) &&
6933         !BypassGeneric(ultimate).HasExplicitInterface()) {
6934       Say(name,
6935           "'%s' must be an abstract interface or a procedure with an explicit interface"_err_en_US,
6936           symbol->name());
6937     }
6938   }
6939 }
6940 
6941 // Create a symbol for a type parameter, component, or procedure binding in
6942 // the current derived type scope. Return false on error.
6943 Symbol *DeclarationVisitor::MakeTypeSymbol(
6944     const parser::Name &name, Details &&details) {
6945   return Resolve(name, MakeTypeSymbol(name.source, std::move(details)));
6946 }
6947 Symbol *DeclarationVisitor::MakeTypeSymbol(
6948     const SourceName &name, Details &&details) {
6949   Scope &derivedType{currScope()};
6950   CHECK(derivedType.IsDerivedType());
6951   if (auto *symbol{FindInScope(derivedType, name)}) { // C742
6952     Say2(name,
6953         "Type parameter, component, or procedure binding '%s'"
6954         " already defined in this type"_err_en_US,
6955         *symbol, "Previous definition of '%s'"_en_US);
6956     return nullptr;
6957   } else {
6958     auto attrs{GetAttrs()};
6959     // Apply binding-private-stmt if present and this is a procedure binding
6960     if (derivedTypeInfo_.privateBindings &&
6961         !attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE}) &&
6962         std::holds_alternative<ProcBindingDetails>(details)) {
6963       attrs.set(Attr::PRIVATE);
6964     }
6965     Symbol &result{MakeSymbol(name, attrs, std::move(details))};
6966     SetCUDADataAttr(name, result, cudaDataAttr());
6967     return &result;
6968   }
6969 }
6970 
6971 // Return true if it is ok to declare this component in the current scope.
6972 // Otherwise, emit an error and return false.
6973 bool DeclarationVisitor::OkToAddComponent(
6974     const parser::Name &name, const Symbol *extends) {
6975   for (const Scope *scope{&currScope()}; scope;) {
6976     CHECK(scope->IsDerivedType());
6977     if (auto *prev{FindInScope(*scope, name.source)}) {
6978       std::optional<parser::MessageFixedText> msg;
6979       std::optional<common::UsageWarning> warning;
6980       if (context().HasError(*prev)) { // don't pile on
6981       } else if (extends) {
6982         msg = "Type cannot be extended as it has a component named"
6983               " '%s'"_err_en_US;
6984       } else if (CheckAccessibleSymbol(currScope(), *prev)) {
6985         // inaccessible component -- redeclaration is ok
6986         if (context().ShouldWarn(
6987                 common::UsageWarning::RedeclaredInaccessibleComponent)) {
6988           msg =
6989               "Component '%s' is inaccessibly declared in or as a parent of this derived type"_warn_en_US;
6990           warning = common::UsageWarning::RedeclaredInaccessibleComponent;
6991         }
6992       } else if (prev->test(Symbol::Flag::ParentComp)) {
6993         msg =
6994             "'%s' is a parent type of this type and so cannot be a component"_err_en_US;
6995       } else if (scope == &currScope()) {
6996         msg =
6997             "Component '%s' is already declared in this derived type"_err_en_US;
6998       } else {
6999         msg =
7000             "Component '%s' is already declared in a parent of this derived type"_err_en_US;
7001       }
7002       if (msg) {
7003         auto &said{Say2(name, std::move(*msg), *prev,
7004             "Previous declaration of '%s'"_en_US)};
7005         if (msg->severity() == parser::Severity::Error) {
7006           Resolve(name, *prev);
7007           return false;
7008         }
7009         if (warning) {
7010           said.set_usageWarning(*warning);
7011         }
7012       }
7013     }
7014     if (scope == &currScope() && extends) {
7015       // The parent component has not yet been added to the scope.
7016       scope = extends->scope();
7017     } else {
7018       scope = scope->GetDerivedTypeParent();
7019     }
7020   }
7021   return true;
7022 }
7023 
7024 ParamValue DeclarationVisitor::GetParamValue(
7025     const parser::TypeParamValue &x, common::TypeParamAttr attr) {
7026   return common::visit(
7027       common::visitors{
7028           [=](const parser::ScalarIntExpr &x) { // C704
7029             return ParamValue{EvaluateIntExpr(x), attr};
7030           },
7031           [=](const parser::Star &) { return ParamValue::Assumed(attr); },
7032           [=](const parser::TypeParamValue::Deferred &) {
7033             return ParamValue::Deferred(attr);
7034           },
7035       },
7036       x.u);
7037 }
7038 
7039 // ConstructVisitor implementation
7040 
7041 void ConstructVisitor::ResolveIndexName(
7042     const parser::ConcurrentControl &control) {
7043   const parser::Name &name{std::get<parser::Name>(control.t)};
7044   auto *prev{FindSymbol(name)};
7045   if (prev) {
7046     if (prev->owner() == currScope()) {
7047       SayAlreadyDeclared(name, *prev);
7048       return;
7049     } else if (prev->owner().kind() == Scope::Kind::Forall &&
7050         context().ShouldWarn(
7051             common::LanguageFeature::OddIndexVariableRestrictions)) {
7052       SayWithDecl(name, *prev,
7053           "Index variable '%s' should not also be an index in an enclosing FORALL or DO CONCURRENT"_port_en_US)
7054           .set_languageFeature(
7055               common::LanguageFeature::OddIndexVariableRestrictions);
7056     }
7057     name.symbol = nullptr;
7058   }
7059   auto &symbol{DeclareObjectEntity(name)};
7060   if (symbol.GetType()) {
7061     // type came from explicit type-spec
7062   } else if (!prev) {
7063     ApplyImplicitRules(symbol);
7064   } else {
7065     // Odd rules in F'2023 19.4 paras 6 & 8.
7066     Symbol &prevRoot{prev->GetUltimate()};
7067     if (const auto *type{prevRoot.GetType()}) {
7068       symbol.SetType(*type);
7069     } else {
7070       ApplyImplicitRules(symbol);
7071     }
7072     if (prevRoot.has<ObjectEntityDetails>() ||
7073         ConvertToObjectEntity(prevRoot)) {
7074       if (prevRoot.IsObjectArray() &&
7075           context().ShouldWarn(
7076               common::LanguageFeature::OddIndexVariableRestrictions)) {
7077         SayWithDecl(name, *prev,
7078             "Index variable '%s' should be scalar in the enclosing scope"_port_en_US)
7079             .set_languageFeature(
7080                 common::LanguageFeature::OddIndexVariableRestrictions);
7081       }
7082     } else if (!prevRoot.has<CommonBlockDetails>() &&
7083         context().ShouldWarn(
7084             common::LanguageFeature::OddIndexVariableRestrictions)) {
7085       SayWithDecl(name, *prev,
7086           "Index variable '%s' should be a scalar object or common block if it is present in the enclosing scope"_port_en_US)
7087           .set_languageFeature(
7088               common::LanguageFeature::OddIndexVariableRestrictions);
7089     }
7090   }
7091   EvaluateExpr(parser::Scalar{parser::Integer{common::Clone(name)}});
7092 }
7093 
7094 // We need to make sure that all of the index-names get declared before the
7095 // expressions in the loop control are evaluated so that references to the
7096 // index-names in the expressions are correctly detected.
7097 bool ConstructVisitor::Pre(const parser::ConcurrentHeader &header) {
7098   BeginDeclTypeSpec();
7099   Walk(std::get<std::optional<parser::IntegerTypeSpec>>(header.t));
7100   const auto &controls{
7101       std::get<std::list<parser::ConcurrentControl>>(header.t)};
7102   for (const auto &control : controls) {
7103     ResolveIndexName(control);
7104   }
7105   Walk(controls);
7106   Walk(std::get<std::optional<parser::ScalarLogicalExpr>>(header.t));
7107   EndDeclTypeSpec();
7108   return false;
7109 }
7110 
7111 bool ConstructVisitor::Pre(const parser::LocalitySpec::Local &x) {
7112   for (auto &name : x.v) {
7113     DeclareLocalEntity(name, Symbol::Flag::LocalityLocal);
7114   }
7115   return false;
7116 }
7117 
7118 bool ConstructVisitor::Pre(const parser::LocalitySpec::LocalInit &x) {
7119   for (auto &name : x.v) {
7120     DeclareLocalEntity(name, Symbol::Flag::LocalityLocalInit);
7121   }
7122   return false;
7123 }
7124 
7125 bool ConstructVisitor::Pre(const parser::LocalitySpec::Reduce &x) {
7126   for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
7127     DeclareLocalEntity(name, Symbol::Flag::LocalityReduce);
7128   }
7129   return false;
7130 }
7131 
7132 bool ConstructVisitor::Pre(const parser::LocalitySpec::Shared &x) {
7133   for (const auto &name : x.v) {
7134     if (!FindSymbol(name)) {
7135       context().Warn(common::UsageWarning::ImplicitShared, name.source,
7136           "Variable '%s' with SHARED locality implicitly declared"_warn_en_US,
7137           name.source);
7138     }
7139     Symbol &prev{FindOrDeclareEnclosingEntity(name)};
7140     if (PassesSharedLocalityChecks(name, prev)) {
7141       MakeHostAssocSymbol(name, prev).set(Symbol::Flag::LocalityShared);
7142     }
7143   }
7144   return false;
7145 }
7146 
7147 bool ConstructVisitor::Pre(const parser::AcSpec &x) {
7148   ProcessTypeSpec(x.type);
7149   Walk(x.values);
7150   return false;
7151 }
7152 
7153 // Section 19.4, paragraph 5 says that each ac-do-variable has the scope of the
7154 // enclosing ac-implied-do
7155 bool ConstructVisitor::Pre(const parser::AcImpliedDo &x) {
7156   auto &values{std::get<std::list<parser::AcValue>>(x.t)};
7157   auto &control{std::get<parser::AcImpliedDoControl>(x.t)};
7158   auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(control.t)};
7159   auto &bounds{std::get<parser::AcImpliedDoControl::Bounds>(control.t)};
7160   // F'2018 has the scope of the implied DO variable covering the entire
7161   // implied DO production (19.4(5)), which seems wrong in cases where the name
7162   // of the implied DO variable appears in one of the bound expressions. Thus
7163   // this extension, which shrinks the scope of the variable to exclude the
7164   // expressions in the bounds.
7165   auto restore{BeginCheckOnIndexUseInOwnBounds(bounds.name)};
7166   Walk(bounds.lower);
7167   Walk(bounds.upper);
7168   Walk(bounds.step);
7169   EndCheckOnIndexUseInOwnBounds(restore);
7170   PushScope(Scope::Kind::ImpliedDos, nullptr);
7171   DeclareStatementEntity(bounds.name, type);
7172   Walk(values);
7173   PopScope();
7174   return false;
7175 }
7176 
7177 bool ConstructVisitor::Pre(const parser::DataImpliedDo &x) {
7178   auto &objects{std::get<std::list<parser::DataIDoObject>>(x.t)};
7179   auto &type{std::get<std::optional<parser::IntegerTypeSpec>>(x.t)};
7180   auto &bounds{std::get<parser::DataImpliedDo::Bounds>(x.t)};
7181   // See comment in Pre(AcImpliedDo) above.
7182   auto restore{BeginCheckOnIndexUseInOwnBounds(bounds.name)};
7183   Walk(bounds.lower);
7184   Walk(bounds.upper);
7185   Walk(bounds.step);
7186   EndCheckOnIndexUseInOwnBounds(restore);
7187   bool pushScope{currScope().kind() != Scope::Kind::ImpliedDos};
7188   if (pushScope) {
7189     PushScope(Scope::Kind::ImpliedDos, nullptr);
7190   }
7191   DeclareStatementEntity(bounds.name, type);
7192   Walk(objects);
7193   if (pushScope) {
7194     PopScope();
7195   }
7196   return false;
7197 }
7198 
7199 // Sets InDataStmt flag on a variable (or misidentified function) in a DATA
7200 // statement so that the predicate IsInitialized() will be true
7201 // during semantic analysis before the symbol's initializer is constructed.
7202 bool ConstructVisitor::Pre(const parser::DataIDoObject &x) {
7203   common::visit(
7204       common::visitors{
7205           [&](const parser::Scalar<Indirection<parser::Designator>> &y) {
7206             Walk(y.thing.value());
7207             const parser::Name &first{parser::GetFirstName(y.thing.value())};
7208             if (first.symbol) {
7209               first.symbol->set(Symbol::Flag::InDataStmt);
7210             }
7211           },
7212           [&](const Indirection<parser::DataImpliedDo> &y) { Walk(y.value()); },
7213       },
7214       x.u);
7215   return false;
7216 }
7217 
7218 bool ConstructVisitor::Pre(const parser::DataStmtObject &x) {
7219   // Subtle: DATA statements may appear in both the specification and
7220   // execution parts, but should be treated as if in the execution part
7221   // for purposes of implicit variable declaration vs. host association.
7222   // When a name first appears as an object in a DATA statement, it should
7223   // be implicitly declared locally as if it had been assigned.
7224   auto flagRestorer{common::ScopedSet(inSpecificationPart_, false)};
7225   common::visit(
7226       common::visitors{
7227           [&](const Indirection<parser::Variable> &y) {
7228             auto restorer{common::ScopedSet(deferImplicitTyping_, true)};
7229             Walk(y.value());
7230             const parser::Name &first{parser::GetFirstName(y.value())};
7231             if (first.symbol) {
7232               first.symbol->set(Symbol::Flag::InDataStmt);
7233             }
7234           },
7235           [&](const parser::DataImpliedDo &y) {
7236             PushScope(Scope::Kind::ImpliedDos, nullptr);
7237             Walk(y);
7238             PopScope();
7239           },
7240       },
7241       x.u);
7242   return false;
7243 }
7244 
7245 bool ConstructVisitor::Pre(const parser::DataStmtValue &x) {
7246   const auto &data{std::get<parser::DataStmtConstant>(x.t)};
7247   auto &mutableData{const_cast<parser::DataStmtConstant &>(data)};
7248   if (auto *elem{parser::Unwrap<parser::ArrayElement>(mutableData)}) {
7249     if (const auto *name{std::get_if<parser::Name>(&elem->base.u)}) {
7250       if (const Symbol * symbol{FindSymbol(*name)};
7251           symbol && symbol->GetUltimate().has<DerivedTypeDetails>()) {
7252         mutableData.u = elem->ConvertToStructureConstructor(
7253             DerivedTypeSpec{name->source, *symbol});
7254       }
7255     }
7256   }
7257   return true;
7258 }
7259 
7260 bool ConstructVisitor::Pre(const parser::DoConstruct &x) {
7261   if (x.IsDoConcurrent()) {
7262     // The new scope has Kind::Forall for index variable name conflict
7263     // detection with nested FORALL/DO CONCURRENT constructs in
7264     // ResolveIndexName().
7265     PushScope(Scope::Kind::Forall, nullptr);
7266   }
7267   return true;
7268 }
7269 void ConstructVisitor::Post(const parser::DoConstruct &x) {
7270   if (x.IsDoConcurrent()) {
7271     PopScope();
7272   }
7273 }
7274 
7275 bool ConstructVisitor::Pre(const parser::ForallConstruct &) {
7276   PushScope(Scope::Kind::Forall, nullptr);
7277   return true;
7278 }
7279 void ConstructVisitor::Post(const parser::ForallConstruct &) { PopScope(); }
7280 bool ConstructVisitor::Pre(const parser::ForallStmt &) {
7281   PushScope(Scope::Kind::Forall, nullptr);
7282   return true;
7283 }
7284 void ConstructVisitor::Post(const parser::ForallStmt &) { PopScope(); }
7285 
7286 bool ConstructVisitor::Pre(const parser::BlockConstruct &x) {
7287   const auto &[blockStmt, specPart, execPart, endBlockStmt] = x.t;
7288   Walk(blockStmt);
7289   CheckDef(blockStmt.statement.v);
7290   PushScope(Scope::Kind::BlockConstruct, nullptr);
7291   Walk(specPart);
7292   HandleImpliedAsynchronousInScope(execPart);
7293   Walk(execPart);
7294   Walk(endBlockStmt);
7295   PopScope();
7296   CheckRef(endBlockStmt.statement.v);
7297   return false;
7298 }
7299 
7300 void ConstructVisitor::Post(const parser::Selector &x) {
7301   GetCurrentAssociation().selector = ResolveSelector(x);
7302 }
7303 
7304 void ConstructVisitor::Post(const parser::AssociateStmt &x) {
7305   CheckDef(x.t);
7306   PushScope(Scope::Kind::OtherConstruct, nullptr);
7307   const auto assocCount{std::get<std::list<parser::Association>>(x.t).size()};
7308   for (auto nthLastAssoc{assocCount}; nthLastAssoc > 0; --nthLastAssoc) {
7309     SetCurrentAssociation(nthLastAssoc);
7310     if (auto *symbol{MakeAssocEntity()}) {
7311       const MaybeExpr &expr{GetCurrentAssociation().selector.expr};
7312       if (ExtractCoarrayRef(expr)) { // C1103
7313         Say("Selector must not be a coindexed object"_err_en_US);
7314       }
7315       if (evaluate::IsAssumedRank(expr)) {
7316         Say("Selector must not be assumed-rank"_err_en_US);
7317       }
7318       SetTypeFromAssociation(*symbol);
7319       SetAttrsFromAssociation(*symbol);
7320     }
7321   }
7322   PopAssociation(assocCount);
7323 }
7324 
7325 void ConstructVisitor::Post(const parser::EndAssociateStmt &x) {
7326   PopScope();
7327   CheckRef(x.v);
7328 }
7329 
7330 bool ConstructVisitor::Pre(const parser::Association &x) {
7331   PushAssociation();
7332   const auto &name{std::get<parser::Name>(x.t)};
7333   GetCurrentAssociation().name = &name;
7334   return true;
7335 }
7336 
7337 bool ConstructVisitor::Pre(const parser::ChangeTeamStmt &x) {
7338   CheckDef(x.t);
7339   PushScope(Scope::Kind::OtherConstruct, nullptr);
7340   PushAssociation();
7341   return true;
7342 }
7343 
7344 void ConstructVisitor::Post(const parser::CoarrayAssociation &x) {
7345   const auto &decl{std::get<parser::CodimensionDecl>(x.t)};
7346   const auto &name{std::get<parser::Name>(decl.t)};
7347   if (auto *symbol{FindInScope(name)}) {
7348     const auto &selector{std::get<parser::Selector>(x.t)};
7349     if (auto sel{ResolveSelector(selector)}) {
7350       const Symbol *whole{UnwrapWholeSymbolDataRef(sel.expr)};
7351       if (!whole || whole->Corank() == 0) {
7352         Say(sel.source, // C1116
7353             "Selector in coarray association must name a coarray"_err_en_US);
7354       } else if (auto dynType{sel.expr->GetType()}) {
7355         if (!symbol->GetType()) {
7356           symbol->SetType(ToDeclTypeSpec(std::move(*dynType)));
7357         }
7358       }
7359     }
7360   }
7361 }
7362 
7363 void ConstructVisitor::Post(const parser::EndChangeTeamStmt &x) {
7364   PopAssociation();
7365   PopScope();
7366   CheckRef(x.t);
7367 }
7368 
7369 bool ConstructVisitor::Pre(const parser::SelectTypeConstruct &) {
7370   PushAssociation();
7371   return true;
7372 }
7373 
7374 void ConstructVisitor::Post(const parser::SelectTypeConstruct &) {
7375   PopAssociation();
7376 }
7377 
7378 void ConstructVisitor::Post(const parser::SelectTypeStmt &x) {
7379   auto &association{GetCurrentAssociation()};
7380   if (const std::optional<parser::Name> &name{std::get<1>(x.t)}) {
7381     // This isn't a name in the current scope, it is in each TypeGuardStmt
7382     MakePlaceholder(*name, MiscDetails::Kind::SelectTypeAssociateName);
7383     association.name = &*name;
7384     if (ExtractCoarrayRef(association.selector.expr)) { // C1103
7385       Say("Selector must not be a coindexed object"_err_en_US);
7386     }
7387     if (association.selector.expr) {
7388       auto exprType{association.selector.expr->GetType()};
7389       if (exprType && !exprType->IsPolymorphic()) { // C1159
7390         Say(association.selector.source,
7391             "Selector '%s' in SELECT TYPE statement must be "
7392             "polymorphic"_err_en_US);
7393       }
7394     }
7395   } else {
7396     if (const Symbol *
7397         whole{UnwrapWholeSymbolDataRef(association.selector.expr)}) {
7398       ConvertToObjectEntity(const_cast<Symbol &>(*whole));
7399       if (!IsVariableName(*whole)) {
7400         Say(association.selector.source, // C901
7401             "Selector is not a variable"_err_en_US);
7402         association = {};
7403       }
7404       if (const DeclTypeSpec * type{whole->GetType()}) {
7405         if (!type->IsPolymorphic()) { // C1159
7406           Say(association.selector.source,
7407               "Selector '%s' in SELECT TYPE statement must be "
7408               "polymorphic"_err_en_US);
7409         }
7410       }
7411     } else {
7412       Say(association.selector.source, // C1157
7413           "Selector is not a named variable: 'associate-name =>' is required"_err_en_US);
7414       association = {};
7415     }
7416   }
7417 }
7418 
7419 void ConstructVisitor::Post(const parser::SelectRankStmt &x) {
7420   auto &association{GetCurrentAssociation()};
7421   if (const std::optional<parser::Name> &name{std::get<1>(x.t)}) {
7422     // This isn't a name in the current scope, it is in each SelectRankCaseStmt
7423     MakePlaceholder(*name, MiscDetails::Kind::SelectRankAssociateName);
7424     association.name = &*name;
7425   }
7426 }
7427 
7428 bool ConstructVisitor::Pre(const parser::SelectTypeConstruct::TypeCase &) {
7429   PushScope(Scope::Kind::OtherConstruct, nullptr);
7430   return true;
7431 }
7432 void ConstructVisitor::Post(const parser::SelectTypeConstruct::TypeCase &) {
7433   PopScope();
7434 }
7435 
7436 bool ConstructVisitor::Pre(const parser::SelectRankConstruct::RankCase &) {
7437   PushScope(Scope::Kind::OtherConstruct, nullptr);
7438   return true;
7439 }
7440 void ConstructVisitor::Post(const parser::SelectRankConstruct::RankCase &) {
7441   PopScope();
7442 }
7443 
7444 bool ConstructVisitor::Pre(const parser::TypeGuardStmt::Guard &x) {
7445   if (std::holds_alternative<parser::DerivedTypeSpec>(x.u)) {
7446     // CLASS IS (t)
7447     SetDeclTypeSpecCategory(DeclTypeSpec::Category::ClassDerived);
7448   }
7449   return true;
7450 }
7451 
7452 void ConstructVisitor::Post(const parser::TypeGuardStmt::Guard &x) {
7453   if (auto *symbol{MakeAssocEntity()}) {
7454     if (std::holds_alternative<parser::Default>(x.u)) {
7455       SetTypeFromAssociation(*symbol);
7456     } else if (const auto *type{GetDeclTypeSpec()}) {
7457       symbol->SetType(*type);
7458     }
7459     SetAttrsFromAssociation(*symbol);
7460   }
7461 }
7462 
7463 void ConstructVisitor::Post(const parser::SelectRankCaseStmt::Rank &x) {
7464   if (auto *symbol{MakeAssocEntity()}) {
7465     SetTypeFromAssociation(*symbol);
7466     auto &details{symbol->get<AssocEntityDetails>()};
7467     // Don't call SetAttrsFromAssociation() for SELECT RANK.
7468     Attrs selectorAttrs{
7469         evaluate::GetAttrs(GetCurrentAssociation().selector.expr)};
7470     Attrs attrsToKeep{Attr::ASYNCHRONOUS, Attr::TARGET, Attr::VOLATILE};
7471     if (const auto *rankValue{
7472             std::get_if<parser::ScalarIntConstantExpr>(&x.u)}) {
7473       // RANK(n)
7474       if (auto expr{EvaluateIntExpr(*rankValue)}) {
7475         if (auto val{evaluate::ToInt64(*expr)}) {
7476           details.set_rank(*val);
7477           attrsToKeep |= Attrs{Attr::ALLOCATABLE, Attr::POINTER};
7478         } else {
7479           Say("RANK() expression must be constant"_err_en_US);
7480         }
7481       }
7482     } else if (std::holds_alternative<parser::Star>(x.u)) {
7483       // RANK(*): assumed-size
7484       details.set_IsAssumedSize();
7485     } else {
7486       CHECK(std::holds_alternative<parser::Default>(x.u));
7487       // RANK DEFAULT: assumed-rank
7488       details.set_IsAssumedRank();
7489       attrsToKeep |= Attrs{Attr::ALLOCATABLE, Attr::POINTER};
7490     }
7491     symbol->attrs() |= selectorAttrs & attrsToKeep;
7492   }
7493 }
7494 
7495 bool ConstructVisitor::Pre(const parser::SelectRankConstruct &) {
7496   PushAssociation();
7497   return true;
7498 }
7499 
7500 void ConstructVisitor::Post(const parser::SelectRankConstruct &) {
7501   PopAssociation();
7502 }
7503 
7504 bool ConstructVisitor::CheckDef(const std::optional<parser::Name> &x) {
7505   if (x && !x->symbol) {
7506     // Construct names are not scoped by BLOCK in the standard, but many,
7507     // but not all, compilers do treat them as if they were so scoped.
7508     if (Symbol * inner{FindInScope(currScope(), *x)}) {
7509       SayAlreadyDeclared(*x, *inner);
7510     } else {
7511       if (context().ShouldWarn(common::LanguageFeature::BenignNameClash)) {
7512         if (Symbol *
7513             other{FindInScopeOrBlockConstructs(InclusiveScope(), x->source)}) {
7514           SayWithDecl(*x, *other,
7515               "The construct name '%s' should be distinct at the subprogram level"_port_en_US)
7516               .set_languageFeature(common::LanguageFeature::BenignNameClash);
7517         }
7518       }
7519       MakeSymbol(*x, MiscDetails{MiscDetails::Kind::ConstructName});
7520     }
7521   }
7522   return true;
7523 }
7524 
7525 void ConstructVisitor::CheckRef(const std::optional<parser::Name> &x) {
7526   if (x) {
7527     // Just add an occurrence of this name; checking is done in ValidateLabels
7528     FindSymbol(*x);
7529   }
7530 }
7531 
7532 // Make a symbol for the associating entity of the current association.
7533 Symbol *ConstructVisitor::MakeAssocEntity() {
7534   Symbol *symbol{nullptr};
7535   auto &association{GetCurrentAssociation()};
7536   if (association.name) {
7537     symbol = &MakeSymbol(*association.name, UnknownDetails{});
7538     if (symbol->has<AssocEntityDetails>() && symbol->owner() == currScope()) {
7539       Say(*association.name, // C1102
7540           "The associate name '%s' is already used in this associate statement"_err_en_US);
7541       return nullptr;
7542     }
7543   } else if (const Symbol *
7544       whole{UnwrapWholeSymbolDataRef(association.selector.expr)}) {
7545     symbol = &MakeSymbol(whole->name());
7546   } else {
7547     return nullptr;
7548   }
7549   if (auto &expr{association.selector.expr}) {
7550     symbol->set_details(AssocEntityDetails{common::Clone(*expr)});
7551   } else {
7552     symbol->set_details(AssocEntityDetails{});
7553   }
7554   return symbol;
7555 }
7556 
7557 // Set the type of symbol based on the current association selector.
7558 void ConstructVisitor::SetTypeFromAssociation(Symbol &symbol) {
7559   auto &details{symbol.get<AssocEntityDetails>()};
7560   const MaybeExpr *pexpr{&details.expr()};
7561   if (!*pexpr) {
7562     pexpr = &GetCurrentAssociation().selector.expr;
7563   }
7564   if (*pexpr) {
7565     const SomeExpr &expr{**pexpr};
7566     if (std::optional<evaluate::DynamicType> type{expr.GetType()}) {
7567       if (const auto *charExpr{
7568               evaluate::UnwrapExpr<evaluate::Expr<evaluate::SomeCharacter>>(
7569                   expr)}) {
7570         symbol.SetType(ToDeclTypeSpec(std::move(*type),
7571             FoldExpr(common::visit(
7572                 [](const auto &kindChar) { return kindChar.LEN(); },
7573                 charExpr->u))));
7574       } else {
7575         symbol.SetType(ToDeclTypeSpec(std::move(*type)));
7576       }
7577     } else {
7578       // BOZ literals, procedure designators, &c. are not acceptable
7579       Say(symbol.name(), "Associate name '%s' must have a type"_err_en_US);
7580     }
7581   }
7582 }
7583 
7584 // If current selector is a variable, set some of its attributes on symbol.
7585 // For ASSOCIATE, CHANGE TEAM, and SELECT TYPE only; not SELECT RANK.
7586 void ConstructVisitor::SetAttrsFromAssociation(Symbol &symbol) {
7587   Attrs attrs{evaluate::GetAttrs(GetCurrentAssociation().selector.expr)};
7588   symbol.attrs() |=
7589       attrs & Attrs{Attr::TARGET, Attr::ASYNCHRONOUS, Attr::VOLATILE};
7590   if (attrs.test(Attr::POINTER)) {
7591     SetImplicitAttr(symbol, Attr::TARGET);
7592   }
7593 }
7594 
7595 ConstructVisitor::Selector ConstructVisitor::ResolveSelector(
7596     const parser::Selector &x) {
7597   return common::visit(common::visitors{
7598                            [&](const parser::Expr &expr) {
7599                              return Selector{expr.source, EvaluateExpr(x)};
7600                            },
7601                            [&](const parser::Variable &var) {
7602                              return Selector{var.GetSource(), EvaluateExpr(x)};
7603                            },
7604                        },
7605       x.u);
7606 }
7607 
7608 // Set the current association to the nth to the last association on the
7609 // association stack.  The top of the stack is at n = 1.  This allows access
7610 // to the interior of a list of associations at the top of the stack.
7611 void ConstructVisitor::SetCurrentAssociation(std::size_t n) {
7612   CHECK(n > 0 && n <= associationStack_.size());
7613   currentAssociation_ = &associationStack_[associationStack_.size() - n];
7614 }
7615 
7616 ConstructVisitor::Association &ConstructVisitor::GetCurrentAssociation() {
7617   CHECK(currentAssociation_);
7618   return *currentAssociation_;
7619 }
7620 
7621 void ConstructVisitor::PushAssociation() {
7622   associationStack_.emplace_back(Association{});
7623   currentAssociation_ = &associationStack_.back();
7624 }
7625 
7626 void ConstructVisitor::PopAssociation(std::size_t count) {
7627   CHECK(count > 0 && count <= associationStack_.size());
7628   associationStack_.resize(associationStack_.size() - count);
7629   currentAssociation_ =
7630       associationStack_.empty() ? nullptr : &associationStack_.back();
7631 }
7632 
7633 const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
7634     evaluate::DynamicType &&type) {
7635   switch (type.category()) {
7636     SWITCH_COVERS_ALL_CASES
7637   case common::TypeCategory::Integer:
7638   case common::TypeCategory::Real:
7639   case common::TypeCategory::Complex:
7640     return context().MakeNumericType(type.category(), type.kind());
7641   case common::TypeCategory::Logical:
7642     return context().MakeLogicalType(type.kind());
7643   case common::TypeCategory::Derived:
7644     if (type.IsAssumedType()) {
7645       return currScope().MakeTypeStarType();
7646     } else if (type.IsUnlimitedPolymorphic()) {
7647       return currScope().MakeClassStarType();
7648     } else {
7649       return currScope().MakeDerivedType(
7650           type.IsPolymorphic() ? DeclTypeSpec::ClassDerived
7651                                : DeclTypeSpec::TypeDerived,
7652           common::Clone(type.GetDerivedTypeSpec())
7653 
7654       );
7655     }
7656   case common::TypeCategory::Character:
7657     CRASH_NO_CASE;
7658   }
7659 }
7660 
7661 const DeclTypeSpec &ConstructVisitor::ToDeclTypeSpec(
7662     evaluate::DynamicType &&type, MaybeSubscriptIntExpr &&length) {
7663   CHECK(type.category() == common::TypeCategory::Character);
7664   if (length) {
7665     return currScope().MakeCharacterType(
7666         ParamValue{SomeIntExpr{*std::move(length)}, common::TypeParamAttr::Len},
7667         KindExpr{type.kind()});
7668   } else {
7669     return currScope().MakeCharacterType(
7670         ParamValue::Deferred(common::TypeParamAttr::Len),
7671         KindExpr{type.kind()});
7672   }
7673 }
7674 
7675 class ExecutionPartSkimmerBase {
7676 public:
7677   template <typename A> bool Pre(const A &) { return true; }
7678   template <typename A> void Post(const A &) {}
7679 
7680   bool InNestedBlockConstruct() const { return blockDepth_ > 0; }
7681 
7682   bool Pre(const parser::AssociateConstruct &) {
7683     PushScope();
7684     return true;
7685   }
7686   void Post(const parser::AssociateConstruct &) { PopScope(); }
7687   bool Pre(const parser::Association &x) {
7688     Hide(std::get<parser::Name>(x.t));
7689     return true;
7690   }
7691   bool Pre(const parser::BlockConstruct &) {
7692     PushScope();
7693     ++blockDepth_;
7694     return true;
7695   }
7696   void Post(const parser::BlockConstruct &) {
7697     --blockDepth_;
7698     PopScope();
7699   }
7700   // Note declarations of local names in BLOCK constructs.
7701   // Don't have to worry about INTENT(), VALUE, or OPTIONAL
7702   // (pertinent only to dummy arguments), ASYNCHRONOUS/VOLATILE,
7703   // or accessibility attributes,
7704   bool Pre(const parser::EntityDecl &x) {
7705     Hide(std::get<parser::ObjectName>(x.t));
7706     return true;
7707   }
7708   bool Pre(const parser::ObjectDecl &x) {
7709     Hide(std::get<parser::ObjectName>(x.t));
7710     return true;
7711   }
7712   bool Pre(const parser::PointerDecl &x) {
7713     Hide(std::get<parser::Name>(x.t));
7714     return true;
7715   }
7716   bool Pre(const parser::BindEntity &x) {
7717     Hide(std::get<parser::Name>(x.t));
7718     return true;
7719   }
7720   bool Pre(const parser::ContiguousStmt &x) {
7721     for (const parser::Name &name : x.v) {
7722       Hide(name);
7723     }
7724     return true;
7725   }
7726   bool Pre(const parser::DimensionStmt::Declaration &x) {
7727     Hide(std::get<parser::Name>(x.t));
7728     return true;
7729   }
7730   bool Pre(const parser::ExternalStmt &x) {
7731     for (const parser::Name &name : x.v) {
7732       Hide(name);
7733     }
7734     return true;
7735   }
7736   bool Pre(const parser::IntrinsicStmt &x) {
7737     for (const parser::Name &name : x.v) {
7738       Hide(name);
7739     }
7740     return true;
7741   }
7742   bool Pre(const parser::CodimensionStmt &x) {
7743     for (const parser::CodimensionDecl &decl : x.v) {
7744       Hide(std::get<parser::Name>(decl.t));
7745     }
7746     return true;
7747   }
7748   void Post(const parser::ImportStmt &x) {
7749     if (x.kind == common::ImportKind::None ||
7750         x.kind == common::ImportKind::Only) {
7751       if (!nestedScopes_.front().importOnly.has_value()) {
7752         nestedScopes_.front().importOnly.emplace();
7753       }
7754       for (const auto &name : x.names) {
7755         nestedScopes_.front().importOnly->emplace(name.source);
7756       }
7757     } else {
7758       // no special handling needed for explicit names or IMPORT, ALL
7759     }
7760   }
7761   void Post(const parser::UseStmt &x) {
7762     if (const auto *onlyList{std::get_if<std::list<parser::Only>>(&x.u)}) {
7763       for (const auto &only : *onlyList) {
7764         if (const auto *name{std::get_if<parser::Name>(&only.u)}) {
7765           Hide(*name);
7766         } else if (const auto *rename{std::get_if<parser::Rename>(&only.u)}) {
7767           if (const auto *names{
7768                   std::get_if<parser::Rename::Names>(&rename->u)}) {
7769             Hide(std::get<0>(names->t));
7770           }
7771         }
7772       }
7773     } else {
7774       // USE may or may not shadow symbols in host scopes
7775       nestedScopes_.front().hasUseWithoutOnly = true;
7776     }
7777   }
7778   bool Pre(const parser::DerivedTypeStmt &x) {
7779     Hide(std::get<parser::Name>(x.t));
7780     PushScope();
7781     return true;
7782   }
7783   void Post(const parser::DerivedTypeDef &) { PopScope(); }
7784   bool Pre(const parser::SelectTypeConstruct &) {
7785     PushScope();
7786     return true;
7787   }
7788   void Post(const parser::SelectTypeConstruct &) { PopScope(); }
7789   bool Pre(const parser::SelectTypeStmt &x) {
7790     if (const auto &maybeName{std::get<1>(x.t)}) {
7791       Hide(*maybeName);
7792     }
7793     return true;
7794   }
7795   bool Pre(const parser::SelectRankConstruct &) {
7796     PushScope();
7797     return true;
7798   }
7799   void Post(const parser::SelectRankConstruct &) { PopScope(); }
7800   bool Pre(const parser::SelectRankStmt &x) {
7801     if (const auto &maybeName{std::get<1>(x.t)}) {
7802       Hide(*maybeName);
7803     }
7804     return true;
7805   }
7806 
7807   // Iterator-modifiers contain variable declarations, and do introduce
7808   // a new scope. These variables can only have integer types, and their
7809   // scope only extends until the end of the clause. A potential alternative
7810   // to the code below may be to ignore OpenMP clauses, but it's not clear
7811   // if OMP-specific checks can be avoided altogether.
7812   bool Pre(const parser::OmpClause &x) {
7813     if (OmpVisitor::NeedsScope(x)) {
7814       PushScope();
7815     }
7816     return true;
7817   }
7818   void Post(const parser::OmpClause &x) {
7819     if (OmpVisitor::NeedsScope(x)) {
7820       PopScope();
7821     }
7822   }
7823 
7824 protected:
7825   bool IsHidden(SourceName name) {
7826     for (const auto &scope : nestedScopes_) {
7827       if (scope.locals.find(name) != scope.locals.end()) {
7828         return true; // shadowed by nested declaration
7829       }
7830       if (scope.hasUseWithoutOnly) {
7831         break;
7832       }
7833       if (scope.importOnly &&
7834           scope.importOnly->find(name) == scope.importOnly->end()) {
7835         return true; // not imported
7836       }
7837     }
7838     return false;
7839   }
7840 
7841   void EndWalk() { CHECK(nestedScopes_.empty()); }
7842 
7843 private:
7844   void PushScope() { nestedScopes_.emplace_front(); }
7845   void PopScope() { nestedScopes_.pop_front(); }
7846   void Hide(const parser::Name &name) {
7847     nestedScopes_.front().locals.emplace(name.source);
7848   }
7849 
7850   int blockDepth_{0};
7851   struct NestedScopeInfo {
7852     bool hasUseWithoutOnly{false};
7853     std::set<SourceName> locals;
7854     std::optional<std::set<SourceName>> importOnly;
7855   };
7856   std::list<NestedScopeInfo> nestedScopes_;
7857 };
7858 
7859 class ExecutionPartAsyncIOSkimmer : public ExecutionPartSkimmerBase {
7860 public:
7861   explicit ExecutionPartAsyncIOSkimmer(SemanticsContext &context)
7862       : context_{context} {}
7863 
7864   void Walk(const parser::Block &block) {
7865     parser::Walk(block, *this);
7866     EndWalk();
7867   }
7868 
7869   const std::set<SourceName> asyncIONames() const { return asyncIONames_; }
7870 
7871   using ExecutionPartSkimmerBase::Post;
7872   using ExecutionPartSkimmerBase::Pre;
7873 
7874   bool Pre(const parser::IoControlSpec::Asynchronous &async) {
7875     if (auto folded{evaluate::Fold(
7876             context_.foldingContext(), AnalyzeExpr(context_, async.v))}) {
7877       if (auto str{
7878               evaluate::GetScalarConstantValue<evaluate::Ascii>(*folded)}) {
7879         for (char ch : *str) {
7880           if (ch != ' ') {
7881             inAsyncIO_ = ch == 'y' || ch == 'Y';
7882             break;
7883           }
7884         }
7885       }
7886     }
7887     return true;
7888   }
7889   void Post(const parser::ReadStmt &) { inAsyncIO_ = false; }
7890   void Post(const parser::WriteStmt &) { inAsyncIO_ = false; }
7891   void Post(const parser::IoControlSpec::Size &size) {
7892     if (const auto *designator{
7893             std::get_if<common::Indirection<parser::Designator>>(
7894                 &size.v.thing.thing.u)}) {
7895       NoteAsyncIODesignator(designator->value());
7896     }
7897   }
7898   void Post(const parser::InputItem &x) {
7899     if (const auto *var{std::get_if<parser::Variable>(&x.u)}) {
7900       if (const auto *designator{
7901               std::get_if<common::Indirection<parser::Designator>>(&var->u)}) {
7902         NoteAsyncIODesignator(designator->value());
7903       }
7904     }
7905   }
7906   void Post(const parser::OutputItem &x) {
7907     if (const auto *expr{std::get_if<parser::Expr>(&x.u)}) {
7908       if (const auto *designator{
7909               std::get_if<common::Indirection<parser::Designator>>(&expr->u)}) {
7910         NoteAsyncIODesignator(designator->value());
7911       }
7912     }
7913   }
7914 
7915 private:
7916   void NoteAsyncIODesignator(const parser::Designator &designator) {
7917     if (inAsyncIO_ && !InNestedBlockConstruct()) {
7918       const parser::Name &name{parser::GetFirstName(designator)};
7919       if (!IsHidden(name.source)) {
7920         asyncIONames_.insert(name.source);
7921       }
7922     }
7923   }
7924 
7925   SemanticsContext &context_;
7926   bool inAsyncIO_{false};
7927   std::set<SourceName> asyncIONames_;
7928 };
7929 
7930 // Any data list item or SIZE= specifier of an I/O data transfer statement
7931 // with ASYNCHRONOUS="YES" implicitly has the ASYNCHRONOUS attribute in the
7932 // local scope.
7933 void ConstructVisitor::HandleImpliedAsynchronousInScope(
7934     const parser::Block &block) {
7935   ExecutionPartAsyncIOSkimmer skimmer{context()};
7936   skimmer.Walk(block);
7937   for (auto name : skimmer.asyncIONames()) {
7938     if (Symbol * symbol{currScope().FindSymbol(name)}) {
7939       if (!symbol->attrs().test(Attr::ASYNCHRONOUS)) {
7940         if (&symbol->owner() != &currScope()) {
7941           symbol = &*currScope()
7942                          .try_emplace(name, HostAssocDetails{*symbol})
7943                          .first->second;
7944         }
7945         if (symbol->has<AssocEntityDetails>()) {
7946           symbol = const_cast<Symbol *>(&GetAssociationRoot(*symbol));
7947         }
7948         SetImplicitAttr(*symbol, Attr::ASYNCHRONOUS);
7949       }
7950     }
7951   }
7952 }
7953 
7954 // ResolveNamesVisitor implementation
7955 
7956 bool ResolveNamesVisitor::Pre(const parser::FunctionReference &x) {
7957   HandleCall(Symbol::Flag::Function, x.v);
7958   return false;
7959 }
7960 bool ResolveNamesVisitor::Pre(const parser::CallStmt &x) {
7961   HandleCall(Symbol::Flag::Subroutine, x.call);
7962   Walk(x.chevrons);
7963   return false;
7964 }
7965 
7966 bool ResolveNamesVisitor::Pre(const parser::ImportStmt &x) {
7967   auto &scope{currScope()};
7968   // Check C896 and C899: where IMPORT statements are allowed
7969   switch (scope.kind()) {
7970   case Scope::Kind::Module:
7971     if (scope.IsModule()) {
7972       Say("IMPORT is not allowed in a module scoping unit"_err_en_US);
7973       return false;
7974     } else if (x.kind == common::ImportKind::None) {
7975       Say("IMPORT,NONE is not allowed in a submodule scoping unit"_err_en_US);
7976       return false;
7977     }
7978     break;
7979   case Scope::Kind::MainProgram:
7980     Say("IMPORT is not allowed in a main program scoping unit"_err_en_US);
7981     return false;
7982   case Scope::Kind::Subprogram:
7983     if (scope.parent().IsGlobal()) {
7984       Say("IMPORT is not allowed in an external subprogram scoping unit"_err_en_US);
7985       return false;
7986     }
7987     break;
7988   case Scope::Kind::BlockData: // C1415 (in part)
7989     Say("IMPORT is not allowed in a BLOCK DATA subprogram"_err_en_US);
7990     return false;
7991   default:;
7992   }
7993   if (auto error{scope.SetImportKind(x.kind)}) {
7994     Say(std::move(*error));
7995   }
7996   for (auto &name : x.names) {
7997     if (Symbol * outer{FindSymbol(scope.parent(), name)}) {
7998       scope.add_importName(name.source);
7999       if (Symbol * symbol{FindInScope(name)}) {
8000         if (outer->GetUltimate() == symbol->GetUltimate()) {
8001           context().Warn(common::LanguageFeature::BenignNameClash, name.source,
8002               "The same '%s' is already present in this scope"_port_en_US,
8003               name.source);
8004         } else {
8005           Say(name,
8006               "A distinct '%s' is already present in this scope"_err_en_US)
8007               .Attach(symbol->name(), "Previous declaration of '%s'"_en_US)
8008               .Attach(outer->name(), "Declaration of '%s' in host scope"_en_US);
8009         }
8010       }
8011     } else {
8012       Say(name, "'%s' not found in host scope"_err_en_US);
8013     }
8014   }
8015   prevImportStmt_ = currStmtSource();
8016   return false;
8017 }
8018 
8019 const parser::Name *DeclarationVisitor::ResolveStructureComponent(
8020     const parser::StructureComponent &x) {
8021   return FindComponent(ResolveDataRef(x.base), x.component);
8022 }
8023 
8024 const parser::Name *DeclarationVisitor::ResolveDesignator(
8025     const parser::Designator &x) {
8026   return common::visit(
8027       common::visitors{
8028           [&](const parser::DataRef &x) { return ResolveDataRef(x); },
8029           [&](const parser::Substring &x) {
8030             Walk(std::get<parser::SubstringRange>(x.t).t);
8031             return ResolveDataRef(std::get<parser::DataRef>(x.t));
8032           },
8033       },
8034       x.u);
8035 }
8036 
8037 const parser::Name *DeclarationVisitor::ResolveDataRef(
8038     const parser::DataRef &x) {
8039   return common::visit(
8040       common::visitors{
8041           [=](const parser::Name &y) { return ResolveName(y); },
8042           [=](const Indirection<parser::StructureComponent> &y) {
8043             return ResolveStructureComponent(y.value());
8044           },
8045           [&](const Indirection<parser::ArrayElement> &y) {
8046             Walk(y.value().subscripts);
8047             const parser::Name *name{ResolveDataRef(y.value().base)};
8048             if (name && name->symbol) {
8049               if (!IsProcedure(*name->symbol)) {
8050                 ConvertToObjectEntity(*name->symbol);
8051               } else if (!context().HasError(*name->symbol)) {
8052                 SayWithDecl(*name, *name->symbol,
8053                     "Cannot reference function '%s' as data"_err_en_US);
8054                 context().SetError(*name->symbol);
8055               }
8056             }
8057             return name;
8058           },
8059           [&](const Indirection<parser::CoindexedNamedObject> &y) {
8060             Walk(y.value().imageSelector);
8061             return ResolveDataRef(y.value().base);
8062           },
8063       },
8064       x.u);
8065 }
8066 
8067 // If implicit types are allowed, ensure name is in the symbol table.
8068 // Otherwise, report an error if it hasn't been declared.
8069 const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
8070   FindSymbol(name);
8071   if (CheckForHostAssociatedImplicit(name)) {
8072     NotePossibleBadForwardRef(name);
8073     return &name;
8074   }
8075   if (Symbol * symbol{name.symbol}) {
8076     if (CheckUseError(name)) {
8077       return nullptr; // reported an error
8078     }
8079     NotePossibleBadForwardRef(name);
8080     symbol->set(Symbol::Flag::ImplicitOrError, false);
8081     if (IsUplevelReference(*symbol)) {
8082       MakeHostAssocSymbol(name, *symbol);
8083     } else if (IsDummy(*symbol) ||
8084         (!symbol->GetType() && FindCommonBlockContaining(*symbol))) {
8085       CheckEntryDummyUse(name.source, symbol);
8086       ConvertToObjectEntity(*symbol);
8087       ApplyImplicitRules(*symbol);
8088     } else if (const auto *tpd{symbol->detailsIf<TypeParamDetails>()};
8089                tpd && !tpd->attr()) {
8090       Say(name,
8091           "Type parameter '%s' was referenced before being declared"_err_en_US,
8092           name.source);
8093       context().SetError(*symbol);
8094     }
8095     if (checkIndexUseInOwnBounds_ &&
8096         *checkIndexUseInOwnBounds_ == name.source && !InModuleFile()) {
8097       context().Warn(common::LanguageFeature::ImpliedDoIndexScope, name.source,
8098           "Implied DO index '%s' uses an object of the same name in its bounds expressions"_port_en_US,
8099           name.source);
8100     }
8101     return &name;
8102   }
8103   if (isImplicitNoneType() && !deferImplicitTyping_) {
8104     Say(name, "No explicit type declared for '%s'"_err_en_US);
8105     return nullptr;
8106   }
8107   // Create the symbol, then ensure that it is accessible
8108   if (checkIndexUseInOwnBounds_ && *checkIndexUseInOwnBounds_ == name.source) {
8109     Say(name,
8110         "Implied DO index '%s' uses itself in its own bounds expressions"_err_en_US,
8111         name.source);
8112   }
8113   MakeSymbol(InclusiveScope(), name.source, Attrs{});
8114   auto *symbol{FindSymbol(name)};
8115   if (!symbol) {
8116     Say(name,
8117         "'%s' from host scoping unit is not accessible due to IMPORT"_err_en_US);
8118     return nullptr;
8119   }
8120   ConvertToObjectEntity(*symbol);
8121   ApplyImplicitRules(*symbol);
8122   NotePossibleBadForwardRef(name);
8123   return &name;
8124 }
8125 
8126 // A specification expression may refer to a symbol in the host procedure that
8127 // is implicitly typed. Because specification parts are processed before
8128 // execution parts, this may be the first time we see the symbol. It can't be a
8129 // local in the current scope (because it's in a specification expression) so
8130 // either it is implicitly declared in the host procedure or it is an error.
8131 // We create a symbol in the host assuming it is the former; if that proves to
8132 // be wrong we report an error later in CheckDeclarations().
8133 bool DeclarationVisitor::CheckForHostAssociatedImplicit(
8134     const parser::Name &name) {
8135   if (!inSpecificationPart_ || inEquivalenceStmt_) {
8136     return false;
8137   }
8138   if (name.symbol) {
8139     ApplyImplicitRules(*name.symbol, true);
8140   }
8141   if (Scope * host{GetHostProcedure()}; host && !isImplicitNoneType(*host)) {
8142     Symbol *hostSymbol{nullptr};
8143     if (!name.symbol) {
8144       if (currScope().CanImport(name.source)) {
8145         hostSymbol = &MakeSymbol(*host, name.source, Attrs{});
8146         ConvertToObjectEntity(*hostSymbol);
8147         ApplyImplicitRules(*hostSymbol);
8148         hostSymbol->set(Symbol::Flag::ImplicitOrError);
8149       }
8150     } else if (name.symbol->test(Symbol::Flag::ImplicitOrError)) {
8151       hostSymbol = name.symbol;
8152     }
8153     if (hostSymbol) {
8154       Symbol &symbol{MakeHostAssocSymbol(name, *hostSymbol)};
8155       if (auto *assoc{symbol.detailsIf<HostAssocDetails>()}) {
8156         if (isImplicitNoneType()) {
8157           assoc->implicitOrExplicitTypeError = true;
8158         } else {
8159           assoc->implicitOrSpecExprError = true;
8160         }
8161         return true;
8162       }
8163     }
8164   }
8165   return false;
8166 }
8167 
8168 bool DeclarationVisitor::IsUplevelReference(const Symbol &symbol) {
8169   const Scope &symbolUnit{GetProgramUnitContaining(symbol)};
8170   if (symbolUnit == GetProgramUnitContaining(currScope())) {
8171     return false;
8172   } else {
8173     Scope::Kind kind{symbolUnit.kind()};
8174     return kind == Scope::Kind::Subprogram || kind == Scope::Kind::MainProgram;
8175   }
8176 }
8177 
8178 // base is a part-ref of a derived type; find the named component in its type.
8179 // Also handles intrinsic type parameter inquiries (%kind, %len) and
8180 // COMPLEX component references (%re, %im).
8181 const parser::Name *DeclarationVisitor::FindComponent(
8182     const parser::Name *base, const parser::Name &component) {
8183   if (!base || !base->symbol) {
8184     return nullptr;
8185   }
8186   if (auto *misc{base->symbol->detailsIf<MiscDetails>()}) {
8187     if (component.source == "kind") {
8188       if (misc->kind() == MiscDetails::Kind::ComplexPartRe ||
8189           misc->kind() == MiscDetails::Kind::ComplexPartIm ||
8190           misc->kind() == MiscDetails::Kind::KindParamInquiry ||
8191           misc->kind() == MiscDetails::Kind::LenParamInquiry) {
8192         // x%{re,im,kind,len}%kind
8193         MakePlaceholder(component, MiscDetails::Kind::KindParamInquiry);
8194         return &component;
8195       }
8196     }
8197   }
8198   CheckEntryDummyUse(base->source, base->symbol);
8199   auto &symbol{base->symbol->GetUltimate()};
8200   if (!symbol.has<AssocEntityDetails>() && !ConvertToObjectEntity(symbol)) {
8201     SayWithDecl(*base, symbol,
8202         "'%s' is not an object and may not be used as the base of a component reference or type parameter inquiry"_err_en_US);
8203     return nullptr;
8204   }
8205   auto *type{symbol.GetType()};
8206   if (!type) {
8207     return nullptr; // should have already reported error
8208   }
8209   if (const IntrinsicTypeSpec * intrinsic{type->AsIntrinsic()}) {
8210     auto category{intrinsic->category()};
8211     MiscDetails::Kind miscKind{MiscDetails::Kind::None};
8212     if (component.source == "kind") {
8213       miscKind = MiscDetails::Kind::KindParamInquiry;
8214     } else if (category == TypeCategory::Character) {
8215       if (component.source == "len") {
8216         miscKind = MiscDetails::Kind::LenParamInquiry;
8217       }
8218     } else if (category == TypeCategory::Complex) {
8219       if (component.source == "re") {
8220         miscKind = MiscDetails::Kind::ComplexPartRe;
8221       } else if (component.source == "im") {
8222         miscKind = MiscDetails::Kind::ComplexPartIm;
8223       }
8224     }
8225     if (miscKind != MiscDetails::Kind::None) {
8226       MakePlaceholder(component, miscKind);
8227       return &component;
8228     }
8229   } else if (DerivedTypeSpec * derived{type->AsDerived()}) {
8230     derived->Instantiate(currScope()); // in case of forward referenced type
8231     if (const Scope * scope{derived->scope()}) {
8232       if (Resolve(component, scope->FindComponent(component.source))) {
8233         if (auto msg{CheckAccessibleSymbol(currScope(), *component.symbol)}) {
8234           context().Say(component.source, *msg);
8235         }
8236         return &component;
8237       } else {
8238         SayDerivedType(component.source,
8239             "Component '%s' not found in derived type '%s'"_err_en_US, *scope);
8240       }
8241     }
8242     return nullptr;
8243   }
8244   if (symbol.test(Symbol::Flag::Implicit)) {
8245     Say(*base,
8246         "'%s' is not an object of derived type; it is implicitly typed"_err_en_US);
8247   } else {
8248     SayWithDecl(
8249         *base, symbol, "'%s' is not an object of derived type"_err_en_US);
8250   }
8251   return nullptr;
8252 }
8253 
8254 void DeclarationVisitor::Initialization(const parser::Name &name,
8255     const parser::Initialization &init, bool inComponentDecl) {
8256   // Traversal of the initializer was deferred to here so that the
8257   // symbol being declared can be available for use in the expression, e.g.:
8258   //   real, parameter :: x = tiny(x)
8259   if (!name.symbol) {
8260     return;
8261   }
8262   Symbol &ultimate{name.symbol->GetUltimate()};
8263   // TODO: check C762 - all bounds and type parameters of component
8264   // are colons or constant expressions if component is initialized
8265   common::visit(
8266       common::visitors{
8267           [&](const parser::ConstantExpr &expr) {
8268             Walk(expr);
8269             if (IsNamedConstant(ultimate) || inComponentDecl) {
8270               NonPointerInitialization(name, expr);
8271             } else {
8272               // Defer analysis so forward references to nested subprograms
8273               // can be properly resolved when they appear in structure
8274               // constructors.
8275               ultimate.set(Symbol::Flag::InDataStmt);
8276             }
8277           },
8278           [&](const parser::NullInit &null) { // => NULL()
8279             Walk(null);
8280             if (auto nullInit{EvaluateExpr(null)}) {
8281               if (!evaluate::IsNullPointer(*nullInit)) { // C813
8282                 Say(null.v.value().source,
8283                     "Pointer initializer must be intrinsic NULL()"_err_en_US);
8284               } else if (IsPointer(ultimate)) {
8285                 if (auto *object{ultimate.detailsIf<ObjectEntityDetails>()}) {
8286                   CHECK(!object->init());
8287                   object->set_init(std::move(*nullInit));
8288                 } else if (auto *procPtr{
8289                                ultimate.detailsIf<ProcEntityDetails>()}) {
8290                   CHECK(!procPtr->init());
8291                   procPtr->set_init(nullptr);
8292                 }
8293               } else {
8294                 Say(name,
8295                     "Non-pointer component '%s' initialized with null pointer"_err_en_US);
8296               }
8297             }
8298           },
8299           [&](const parser::InitialDataTarget &target) {
8300             // Defer analysis to the end of the specification part
8301             // so that forward references and attribute checks like SAVE
8302             // work better.
8303             auto restorer{common::ScopedSet(deferImplicitTyping_, true)};
8304             Walk(target);
8305             ultimate.set(Symbol::Flag::InDataStmt);
8306           },
8307           [&](const std::list<Indirection<parser::DataStmtValue>> &values) {
8308             // Handled later in data-to-inits conversion
8309             ultimate.set(Symbol::Flag::InDataStmt);
8310             Walk(values);
8311           },
8312       },
8313       init.u);
8314 }
8315 
8316 void DeclarationVisitor::PointerInitialization(
8317     const parser::Name &name, const parser::InitialDataTarget &target) {
8318   if (name.symbol) {
8319     Symbol &ultimate{name.symbol->GetUltimate()};
8320     if (!context().HasError(ultimate)) {
8321       if (IsPointer(ultimate)) {
8322         Walk(target);
8323         if (MaybeExpr expr{EvaluateExpr(target)}) {
8324           // Validation is done in declaration checking.
8325           if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
8326             CHECK(!details->init());
8327             details->set_init(std::move(*expr));
8328             ultimate.set(Symbol::Flag::InDataStmt, false);
8329           } else if (auto *details{ultimate.detailsIf<ProcEntityDetails>()}) {
8330             // something like "REAL, EXTERNAL, POINTER :: p => t"
8331             if (evaluate::IsNullProcedurePointer(*expr)) {
8332               CHECK(!details->init());
8333               details->set_init(nullptr);
8334             } else if (const Symbol *
8335                 targetSymbol{evaluate::UnwrapWholeSymbolDataRef(*expr)}) {
8336               CHECK(!details->init());
8337               details->set_init(*targetSymbol);
8338             } else {
8339               Say(name,
8340                   "Procedure pointer '%s' must be initialized with a procedure name or NULL()"_err_en_US);
8341               context().SetError(ultimate);
8342             }
8343           }
8344         }
8345       } else {
8346         Say(name,
8347             "'%s' is not a pointer but is initialized like one"_err_en_US);
8348         context().SetError(ultimate);
8349       }
8350     }
8351   }
8352 }
8353 void DeclarationVisitor::PointerInitialization(
8354     const parser::Name &name, const parser::ProcPointerInit &target) {
8355   if (name.symbol) {
8356     Symbol &ultimate{name.symbol->GetUltimate()};
8357     if (!context().HasError(ultimate)) {
8358       if (IsProcedurePointer(ultimate)) {
8359         auto &details{ultimate.get<ProcEntityDetails>()};
8360         CHECK(!details.init());
8361         if (const auto *targetName{std::get_if<parser::Name>(&target.u)}) {
8362           Walk(target);
8363           if (!CheckUseError(*targetName) && targetName->symbol) {
8364             // Validation is done in declaration checking.
8365             details.set_init(*targetName->symbol);
8366           }
8367         } else { // explicit NULL
8368           details.set_init(nullptr);
8369         }
8370       } else {
8371         Say(name,
8372             "'%s' is not a procedure pointer but is initialized "
8373             "like one"_err_en_US);
8374         context().SetError(ultimate);
8375       }
8376     }
8377   }
8378 }
8379 
8380 void DeclarationVisitor::NonPointerInitialization(
8381     const parser::Name &name, const parser::ConstantExpr &expr) {
8382   if (!context().HasError(name.symbol)) {
8383     Symbol &ultimate{name.symbol->GetUltimate()};
8384     if (!context().HasError(ultimate)) {
8385       if (IsPointer(ultimate)) {
8386         Say(name,
8387             "'%s' is a pointer but is not initialized like one"_err_en_US);
8388       } else if (auto *details{ultimate.detailsIf<ObjectEntityDetails>()}) {
8389         if (details->init()) {
8390           SayWithDecl(name, *name.symbol,
8391               "'%s' has already been initialized"_err_en_US);
8392         } else if (IsAllocatable(ultimate)) {
8393           Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
8394         } else if (ultimate.owner().IsParameterizedDerivedType()) {
8395           // Save the expression for per-instantiation analysis.
8396           details->set_unanalyzedPDTComponentInit(&expr.thing.value());
8397         } else if (MaybeExpr folded{EvaluateNonPointerInitializer(
8398                        ultimate, expr, expr.thing.value().source)}) {
8399           details->set_init(std::move(*folded));
8400           ultimate.set(Symbol::Flag::InDataStmt, false);
8401         }
8402       } else {
8403         Say(name, "'%s' is not an object that can be initialized"_err_en_US);
8404       }
8405     }
8406   }
8407 }
8408 
8409 void ResolveNamesVisitor::HandleCall(
8410     Symbol::Flag procFlag, const parser::Call &call) {
8411   common::visit(
8412       common::visitors{
8413           [&](const parser::Name &x) { HandleProcedureName(procFlag, x); },
8414           [&](const parser::ProcComponentRef &x) {
8415             Walk(x);
8416             const parser::Name &name{x.v.thing.component};
8417             if (Symbol * symbol{name.symbol}) {
8418               if (IsProcedure(*symbol)) {
8419                 SetProcFlag(name, *symbol, procFlag);
8420               }
8421             }
8422           },
8423       },
8424       std::get<parser::ProcedureDesignator>(call.t).u);
8425   const auto &arguments{std::get<std::list<parser::ActualArgSpec>>(call.t)};
8426   Walk(arguments);
8427   // Once an object has appeared in a specification function reference as
8428   // a whole scalar actual argument, it cannot be (re)dimensioned later.
8429   // The fact that it appeared to be a scalar may determine the resolution
8430   // or the result of an inquiry intrinsic function or generic procedure.
8431   if (inSpecificationPart_) {
8432     for (const auto &argSpec : arguments) {
8433       const auto &actual{std::get<parser::ActualArg>(argSpec.t)};
8434       if (const auto *expr{
8435               std::get_if<common::Indirection<parser::Expr>>(&actual.u)}) {
8436         if (const auto *designator{
8437                 std::get_if<common::Indirection<parser::Designator>>(
8438                     &expr->value().u)}) {
8439           if (const auto *dataRef{
8440                   std::get_if<parser::DataRef>(&designator->value().u)}) {
8441             if (const auto *name{std::get_if<parser::Name>(&dataRef->u)};
8442                 name && name->symbol) {
8443               const Symbol &symbol{*name->symbol};
8444               const auto *object{symbol.detailsIf<ObjectEntityDetails>()};
8445               if (symbol.has<EntityDetails>() ||
8446                   (object && !object->IsArray())) {
8447                 NoteScalarSpecificationArgument(symbol);
8448               }
8449             }
8450           }
8451         }
8452       }
8453     }
8454   }
8455 }
8456 
8457 void ResolveNamesVisitor::HandleProcedureName(
8458     Symbol::Flag flag, const parser::Name &name) {
8459   CHECK(flag == Symbol::Flag::Function || flag == Symbol::Flag::Subroutine);
8460   auto *symbol{FindSymbol(NonDerivedTypeScope(), name)};
8461   if (!symbol) {
8462     if (IsIntrinsic(name.source, flag)) {
8463       symbol = &MakeSymbol(InclusiveScope(), name.source, Attrs{});
8464       SetImplicitAttr(*symbol, Attr::INTRINSIC);
8465     } else if (const auto ppcBuiltinScope =
8466                    currScope().context().GetPPCBuiltinsScope()) {
8467       // Check if it is a builtin from the predefined module
8468       symbol = FindSymbol(*ppcBuiltinScope, name);
8469       if (!symbol) {
8470         symbol = &MakeSymbol(context().globalScope(), name.source, Attrs{});
8471       }
8472     } else {
8473       symbol = &MakeSymbol(context().globalScope(), name.source, Attrs{});
8474     }
8475     Resolve(name, *symbol);
8476     ConvertToProcEntity(*symbol, name.source);
8477     if (!symbol->attrs().test(Attr::INTRINSIC)) {
8478       if (CheckImplicitNoneExternal(name.source, *symbol)) {
8479         MakeExternal(*symbol);
8480         // Create a place-holder HostAssocDetails symbol to preclude later
8481         // use of this name as a local symbol; but don't actually use this new
8482         // HostAssocDetails symbol in expressions.
8483         MakeHostAssocSymbol(name, *symbol);
8484         name.symbol = symbol;
8485       }
8486     }
8487     CheckEntryDummyUse(name.source, symbol);
8488     SetProcFlag(name, *symbol, flag);
8489   } else if (CheckUseError(name)) {
8490     // error was reported
8491   } else {
8492     symbol = &symbol->GetUltimate();
8493     if (!name.symbol ||
8494         (name.symbol->has<HostAssocDetails>() && symbol->owner().IsGlobal() &&
8495             (symbol->has<ProcEntityDetails>() ||
8496                 (symbol->has<SubprogramDetails>() &&
8497                     symbol->scope() /*not ENTRY*/)))) {
8498       name.symbol = symbol;
8499     }
8500     CheckEntryDummyUse(name.source, symbol);
8501     bool convertedToProcEntity{ConvertToProcEntity(*symbol, name.source)};
8502     if (convertedToProcEntity && !symbol->attrs().test(Attr::EXTERNAL) &&
8503         IsIntrinsic(symbol->name(), flag) && !IsDummy(*symbol)) {
8504       AcquireIntrinsicProcedureFlags(*symbol);
8505     }
8506     if (!SetProcFlag(name, *symbol, flag)) {
8507       return; // reported error
8508     }
8509     CheckImplicitNoneExternal(name.source, *symbol);
8510     if (IsProcedure(*symbol) || symbol->has<DerivedTypeDetails>() ||
8511         symbol->has<AssocEntityDetails>()) {
8512       // Symbols with DerivedTypeDetails and AssocEntityDetails are accepted
8513       // here as procedure-designators because this means the related
8514       // FunctionReference are mis-parsed structure constructors or array
8515       // references that will be fixed later when analyzing expressions.
8516     } else if (symbol->has<ObjectEntityDetails>()) {
8517       // Symbols with ObjectEntityDetails are also accepted because this can be
8518       // a mis-parsed array reference that will be fixed later. Ensure that if
8519       // this is a symbol from a host procedure, a symbol with HostAssocDetails
8520       // is created for the current scope.
8521       // Operate on non ultimate symbol so that HostAssocDetails are also
8522       // created for symbols used associated in the host procedure.
8523       ResolveName(name);
8524     } else if (symbol->test(Symbol::Flag::Implicit)) {
8525       Say(name,
8526           "Use of '%s' as a procedure conflicts with its implicit definition"_err_en_US);
8527     } else {
8528       SayWithDecl(name, *symbol,
8529           "Use of '%s' as a procedure conflicts with its declaration"_err_en_US);
8530     }
8531   }
8532 }
8533 
8534 bool ResolveNamesVisitor::CheckImplicitNoneExternal(
8535     const SourceName &name, const Symbol &symbol) {
8536   if (symbol.has<ProcEntityDetails>() && isImplicitNoneExternal() &&
8537       !symbol.attrs().test(Attr::EXTERNAL) &&
8538       !symbol.attrs().test(Attr::INTRINSIC) && !symbol.HasExplicitInterface()) {
8539     Say(name,
8540         "'%s' is an external procedure without the EXTERNAL attribute in a scope with IMPLICIT NONE(EXTERNAL)"_err_en_US);
8541     return false;
8542   }
8543   return true;
8544 }
8545 
8546 // Variant of HandleProcedureName() for use while skimming the executable
8547 // part of a subprogram to catch calls to dummy procedures that are part
8548 // of the subprogram's interface, and to mark as procedures any symbols
8549 // that might otherwise have been miscategorized as objects.
8550 void ResolveNamesVisitor::NoteExecutablePartCall(
8551     Symbol::Flag flag, SourceName name, bool hasCUDAChevrons) {
8552   // Subtlety: The symbol pointers in the parse tree are not set, because
8553   // they might end up resolving elsewhere (e.g., construct entities in
8554   // SELECT TYPE).
8555   if (Symbol * symbol{currScope().FindSymbol(name)}) {
8556     Symbol::Flag other{flag == Symbol::Flag::Subroutine
8557             ? Symbol::Flag::Function
8558             : Symbol::Flag::Subroutine};
8559     if (!symbol->test(other)) {
8560       ConvertToProcEntity(*symbol, name);
8561       if (auto *details{symbol->detailsIf<ProcEntityDetails>()}) {
8562         symbol->set(flag);
8563         if (IsDummy(*symbol)) {
8564           SetImplicitAttr(*symbol, Attr::EXTERNAL);
8565         }
8566         ApplyImplicitRules(*symbol);
8567         if (hasCUDAChevrons) {
8568           details->set_isCUDAKernel();
8569         }
8570       }
8571     }
8572   }
8573 }
8574 
8575 static bool IsLocallyImplicitGlobalSymbol(
8576     const Symbol &symbol, const parser::Name &localName) {
8577   if (symbol.owner().IsGlobal()) {
8578     const auto *subp{symbol.detailsIf<SubprogramDetails>()};
8579     const Scope *scope{
8580         subp && subp->entryScope() ? subp->entryScope() : symbol.scope()};
8581     return !(scope && scope->sourceRange().Contains(localName.source));
8582   }
8583   return false;
8584 }
8585 
8586 static bool TypesMismatchIfNonNull(
8587     const DeclTypeSpec *type1, const DeclTypeSpec *type2) {
8588   return type1 && type2 && *type1 != *type2;
8589 }
8590 
8591 // Check and set the Function or Subroutine flag on symbol; false on error.
8592 bool ResolveNamesVisitor::SetProcFlag(
8593     const parser::Name &name, Symbol &symbol, Symbol::Flag flag) {
8594   if (symbol.test(Symbol::Flag::Function) && flag == Symbol::Flag::Subroutine) {
8595     SayWithDecl(
8596         name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
8597     context().SetError(symbol);
8598     return false;
8599   } else if (symbol.test(Symbol::Flag::Subroutine) &&
8600       flag == Symbol::Flag::Function) {
8601     SayWithDecl(
8602         name, symbol, "Cannot call subroutine '%s' like a function"_err_en_US);
8603     context().SetError(symbol);
8604     return false;
8605   } else if (flag == Symbol::Flag::Function &&
8606       IsLocallyImplicitGlobalSymbol(symbol, name) &&
8607       TypesMismatchIfNonNull(symbol.GetType(), GetImplicitType(symbol))) {
8608     SayWithDecl(name, symbol,
8609         "Implicit declaration of function '%s' has a different result type than in previous declaration"_err_en_US);
8610     return false;
8611   } else if (symbol.has<ProcEntityDetails>()) {
8612     symbol.set(flag); // in case it hasn't been set yet
8613     if (flag == Symbol::Flag::Function) {
8614       ApplyImplicitRules(symbol);
8615     }
8616     if (symbol.attrs().test(Attr::INTRINSIC)) {
8617       AcquireIntrinsicProcedureFlags(symbol);
8618     }
8619   } else if (symbol.GetType() && flag == Symbol::Flag::Subroutine) {
8620     SayWithDecl(
8621         name, symbol, "Cannot call function '%s' like a subroutine"_err_en_US);
8622     context().SetError(symbol);
8623   } else if (symbol.attrs().test(Attr::INTRINSIC)) {
8624     AcquireIntrinsicProcedureFlags(symbol);
8625   }
8626   return true;
8627 }
8628 
8629 bool ModuleVisitor::Pre(const parser::AccessStmt &x) {
8630   Attr accessAttr{AccessSpecToAttr(std::get<parser::AccessSpec>(x.t))};
8631   if (!currScope().IsModule()) { // C869
8632     Say(currStmtSource().value(),
8633         "%s statement may only appear in the specification part of a module"_err_en_US,
8634         EnumToString(accessAttr));
8635     return false;
8636   }
8637   const auto &accessIds{std::get<std::list<parser::AccessId>>(x.t)};
8638   if (accessIds.empty()) {
8639     if (prevAccessStmt_) { // C869
8640       Say("The default accessibility of this module has already been declared"_err_en_US)
8641           .Attach(*prevAccessStmt_, "Previous declaration"_en_US);
8642     }
8643     prevAccessStmt_ = currStmtSource();
8644     auto *moduleDetails{DEREF(currScope().symbol()).detailsIf<ModuleDetails>()};
8645     DEREF(moduleDetails).set_isDefaultPrivate(accessAttr == Attr::PRIVATE);
8646   } else {
8647     for (const auto &accessId : accessIds) {
8648       GenericSpecInfo info{accessId.v.value()};
8649       auto *symbol{FindInScope(info.symbolName())};
8650       if (!symbol && !info.kind().IsName()) {
8651         symbol = &MakeSymbol(info.symbolName(), Attrs{}, GenericDetails{});
8652       }
8653       info.Resolve(&SetAccess(info.symbolName(), accessAttr, symbol));
8654     }
8655   }
8656   return false;
8657 }
8658 
8659 // Set the access specification for this symbol.
8660 Symbol &ModuleVisitor::SetAccess(
8661     const SourceName &name, Attr attr, Symbol *symbol) {
8662   if (!symbol) {
8663     symbol = &MakeSymbol(name);
8664   }
8665   Attrs &attrs{symbol->attrs()};
8666   if (attrs.HasAny({Attr::PUBLIC, Attr::PRIVATE})) {
8667     // PUBLIC/PRIVATE already set: make it a fatal error if it changed
8668     Attr prev{attrs.test(Attr::PUBLIC) ? Attr::PUBLIC : Attr::PRIVATE};
8669     if (attr != prev) {
8670       Say(name,
8671           "The accessibility of '%s' has already been specified as %s"_err_en_US,
8672           MakeOpName(name), EnumToString(prev));
8673     } else {
8674       context().Warn(common::LanguageFeature::RedundantAttribute, name,
8675           "The accessibility of '%s' has already been specified as %s"_warn_en_US,
8676           MakeOpName(name), EnumToString(prev));
8677     }
8678   } else {
8679     attrs.set(attr);
8680   }
8681   return *symbol;
8682 }
8683 
8684 static bool NeedsExplicitType(const Symbol &symbol) {
8685   if (symbol.has<UnknownDetails>()) {
8686     return true;
8687   } else if (const auto *details{symbol.detailsIf<EntityDetails>()}) {
8688     return !details->type();
8689   } else if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) {
8690     return !details->type();
8691   } else if (const auto *details{symbol.detailsIf<ProcEntityDetails>()}) {
8692     return !details->procInterface() && !details->type();
8693   } else {
8694     return false;
8695   }
8696 }
8697 
8698 void ResolveNamesVisitor::HandleDerivedTypesInImplicitStmts(
8699     const parser::ImplicitPart &implicitPart,
8700     const std::list<parser::DeclarationConstruct> &decls) {
8701   // Detect derived type definitions and create symbols for them now if
8702   // they appear in IMPLICIT statements so that these forward-looking
8703   // references will not be ambiguous with host associations.
8704   std::set<SourceName> implicitDerivedTypes;
8705   for (const auto &ipStmt : implicitPart.v) {
8706     if (const auto *impl{std::get_if<
8707             parser::Statement<common::Indirection<parser::ImplicitStmt>>>(
8708             &ipStmt.u)}) {
8709       if (const auto *specs{std::get_if<std::list<parser::ImplicitSpec>>(
8710               &impl->statement.value().u)}) {
8711         for (const auto &spec : *specs) {
8712           const auto &declTypeSpec{
8713               std::get<parser::DeclarationTypeSpec>(spec.t)};
8714           if (const auto *dtSpec{common::visit(
8715                   common::visitors{
8716                       [](const parser::DeclarationTypeSpec::Type &x) {
8717                         return &x.derived;
8718                       },
8719                       [](const parser::DeclarationTypeSpec::Class &x) {
8720                         return &x.derived;
8721                       },
8722                       [](const auto &) -> const parser::DerivedTypeSpec * {
8723                         return nullptr;
8724                       }},
8725                   declTypeSpec.u)}) {
8726             implicitDerivedTypes.emplace(
8727                 std::get<parser::Name>(dtSpec->t).source);
8728           }
8729         }
8730       }
8731     }
8732   }
8733   if (!implicitDerivedTypes.empty()) {
8734     for (const auto &decl : decls) {
8735       if (const auto *spec{
8736               std::get_if<parser::SpecificationConstruct>(&decl.u)}) {
8737         if (const auto *dtDef{
8738                 std::get_if<common::Indirection<parser::DerivedTypeDef>>(
8739                     &spec->u)}) {
8740           const parser::DerivedTypeStmt &dtStmt{
8741               std::get<parser::Statement<parser::DerivedTypeStmt>>(
8742                   dtDef->value().t)
8743                   .statement};
8744           const parser::Name &name{std::get<parser::Name>(dtStmt.t)};
8745           if (implicitDerivedTypes.find(name.source) !=
8746                   implicitDerivedTypes.end() &&
8747               !FindInScope(name)) {
8748             DerivedTypeDetails details;
8749             details.set_isForwardReferenced(true);
8750             Resolve(name, MakeSymbol(name, std::move(details)));
8751             implicitDerivedTypes.erase(name.source);
8752           }
8753         }
8754       }
8755     }
8756   }
8757 }
8758 
8759 bool ResolveNamesVisitor::Pre(const parser::SpecificationPart &x) {
8760   const auto &[accDecls, ompDecls, compilerDirectives, useStmts, importStmts,
8761       implicitPart, decls] = x.t;
8762   auto flagRestorer{common::ScopedSet(inSpecificationPart_, true)};
8763   auto stateRestorer{
8764       common::ScopedSet(specPartState_, SpecificationPartState{})};
8765   Walk(accDecls);
8766   Walk(ompDecls);
8767   Walk(compilerDirectives);
8768   for (const auto &useStmt : useStmts) {
8769     CollectUseRenames(useStmt.statement.value());
8770   }
8771   Walk(useStmts);
8772   UseCUDABuiltinNames();
8773   ClearUseRenames();
8774   ClearUseOnly();
8775   ClearModuleUses();
8776   Walk(importStmts);
8777   HandleDerivedTypesInImplicitStmts(implicitPart, decls);
8778   Walk(implicitPart);
8779   for (const auto &decl : decls) {
8780     if (const auto *spec{
8781             std::get_if<parser::SpecificationConstruct>(&decl.u)}) {
8782       PreSpecificationConstruct(*spec);
8783     }
8784   }
8785   Walk(decls);
8786   FinishSpecificationPart(decls);
8787   return false;
8788 }
8789 
8790 void ResolveNamesVisitor::UseCUDABuiltinNames() {
8791   if (FindCUDADeviceContext(&currScope())) {
8792     for (const auto &[name, symbol] : context().GetCUDABuiltinsScope()) {
8793       if (!FindInScope(name)) {
8794         auto &localSymbol{MakeSymbol(name)};
8795         localSymbol.set_details(UseDetails{name, *symbol});
8796         localSymbol.flags() = symbol->flags();
8797       }
8798     }
8799   }
8800 }
8801 
8802 // Initial processing on specification constructs, before visiting them.
8803 void ResolveNamesVisitor::PreSpecificationConstruct(
8804     const parser::SpecificationConstruct &spec) {
8805   common::visit(
8806       common::visitors{
8807           [&](const parser::Statement<Indirection<parser::GenericStmt>> &y) {
8808             CreateGeneric(std::get<parser::GenericSpec>(y.statement.value().t));
8809           },
8810           [&](const Indirection<parser::InterfaceBlock> &y) {
8811             const auto &stmt{std::get<parser::Statement<parser::InterfaceStmt>>(
8812                 y.value().t)};
8813             if (const auto *spec{parser::Unwrap<parser::GenericSpec>(stmt)}) {
8814               CreateGeneric(*spec);
8815             }
8816           },
8817           [&](const parser::Statement<parser::OtherSpecificationStmt> &y) {
8818             common::visit(
8819                 common::visitors{
8820                     [&](const common::Indirection<parser::CommonStmt> &z) {
8821                       CreateCommonBlockSymbols(z.value());
8822                     },
8823                     [&](const common::Indirection<parser::TargetStmt> &z) {
8824                       CreateObjectSymbols(z.value().v, Attr::TARGET);
8825                     },
8826                     [](const auto &) {},
8827                 },
8828                 y.statement.u);
8829           },
8830           [](const auto &) {},
8831       },
8832       spec.u);
8833 }
8834 
8835 void ResolveNamesVisitor::CreateCommonBlockSymbols(
8836     const parser::CommonStmt &commonStmt) {
8837   for (const parser::CommonStmt::Block &block : commonStmt.blocks) {
8838     const auto &[name, objects] = block.t;
8839     Symbol &commonBlock{MakeCommonBlockSymbol(name)};
8840     for (const auto &object : objects) {
8841       Symbol &obj{DeclareObjectEntity(std::get<parser::Name>(object.t))};
8842       if (auto *details{obj.detailsIf<ObjectEntityDetails>()}) {
8843         details->set_commonBlock(commonBlock);
8844         commonBlock.get<CommonBlockDetails>().add_object(obj);
8845       }
8846     }
8847   }
8848 }
8849 
8850 void ResolveNamesVisitor::CreateObjectSymbols(
8851     const std::list<parser::ObjectDecl> &decls, Attr attr) {
8852   for (const parser::ObjectDecl &decl : decls) {
8853     SetImplicitAttr(DeclareEntity<ObjectEntityDetails>(
8854                         std::get<parser::ObjectName>(decl.t), Attrs{}),
8855         attr);
8856   }
8857 }
8858 
8859 void ResolveNamesVisitor::CreateGeneric(const parser::GenericSpec &x) {
8860   auto info{GenericSpecInfo{x}};
8861   SourceName symbolName{info.symbolName()};
8862   if (IsLogicalConstant(context(), symbolName)) {
8863     Say(symbolName,
8864         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
8865     return;
8866   }
8867   GenericDetails genericDetails;
8868   Symbol *existing{nullptr};
8869   // Check all variants of names, e.g. "operator(.ne.)" for "operator(/=)"
8870   for (const std::string &n : GetAllNames(context(), symbolName)) {
8871     existing = currScope().FindSymbol(SourceName{n});
8872     if (existing) {
8873       break;
8874     }
8875   }
8876   if (existing) {
8877     Symbol &ultimate{existing->GetUltimate()};
8878     if (auto *existingGeneric{ultimate.detailsIf<GenericDetails>()}) {
8879       if (&existing->owner() == &currScope()) {
8880         if (const auto *existingUse{existing->detailsIf<UseDetails>()}) {
8881           // Create a local copy of a use associated generic so that
8882           // it can be locally extended without corrupting the original.
8883           genericDetails.CopyFrom(*existingGeneric);
8884           if (existingGeneric->specific()) {
8885             genericDetails.set_specific(*existingGeneric->specific());
8886           }
8887           AddGenericUse(
8888               genericDetails, existing->name(), existingUse->symbol());
8889         } else if (existing == &ultimate) {
8890           // Extending an extant generic in the same scope
8891           info.Resolve(existing);
8892           return;
8893         } else {
8894           // Host association of a generic is handled elsewhere
8895           CHECK(existing->has<HostAssocDetails>());
8896         }
8897       } else {
8898         // Create a new generic for this scope.
8899       }
8900     } else if (ultimate.has<SubprogramDetails>() ||
8901         ultimate.has<SubprogramNameDetails>()) {
8902       genericDetails.set_specific(*existing);
8903     } else if (ultimate.has<ProcEntityDetails>()) {
8904       if (existing->name() != symbolName ||
8905           !ultimate.attrs().test(Attr::INTRINSIC)) {
8906         genericDetails.set_specific(*existing);
8907       }
8908     } else if (ultimate.has<DerivedTypeDetails>()) {
8909       genericDetails.set_derivedType(*existing);
8910     } else if (&existing->owner() == &currScope()) {
8911       SayAlreadyDeclared(symbolName, *existing);
8912       return;
8913     }
8914     if (&existing->owner() == &currScope()) {
8915       EraseSymbol(*existing);
8916     }
8917   }
8918   info.Resolve(&MakeSymbol(symbolName, Attrs{}, std::move(genericDetails)));
8919 }
8920 
8921 void ResolveNamesVisitor::FinishSpecificationPart(
8922     const std::list<parser::DeclarationConstruct> &decls) {
8923   misparsedStmtFuncFound_ = false;
8924   funcResultStack().CompleteFunctionResultType();
8925   CheckImports();
8926   for (auto &pair : currScope()) {
8927     auto &symbol{*pair.second};
8928     if (inInterfaceBlock()) {
8929       ConvertToObjectEntity(symbol);
8930     }
8931     if (NeedsExplicitType(symbol)) {
8932       ApplyImplicitRules(symbol);
8933     }
8934     if (IsDummy(symbol) && isImplicitNoneType() &&
8935         symbol.test(Symbol::Flag::Implicit) && !context().HasError(symbol)) {
8936       Say(symbol.name(),
8937           "No explicit type declared for dummy argument '%s'"_err_en_US);
8938       context().SetError(symbol);
8939     }
8940     if (symbol.has<GenericDetails>()) {
8941       CheckGenericProcedures(symbol);
8942     }
8943     if (!symbol.has<HostAssocDetails>()) {
8944       CheckPossibleBadForwardRef(symbol);
8945     }
8946     // Propagate BIND(C) attribute to procedure entities from their interfaces,
8947     // but not the NAME=, even if it is empty (which would be a reasonable
8948     // and useful behavior, actually).  This interpretation is not at all
8949     // clearly described in the standard, but matches the behavior of several
8950     // other compilers.
8951     if (auto *proc{symbol.detailsIf<ProcEntityDetails>()}; proc &&
8952         !proc->isDummy() && !IsPointer(symbol) &&
8953         !symbol.attrs().test(Attr::BIND_C)) {
8954       if (const Symbol * iface{proc->procInterface()};
8955           iface && IsBindCProcedure(*iface)) {
8956         SetImplicitAttr(symbol, Attr::BIND_C);
8957         SetBindNameOn(symbol);
8958       }
8959     }
8960   }
8961   currScope().InstantiateDerivedTypes();
8962   for (const auto &decl : decls) {
8963     if (const auto *statement{std::get_if<
8964             parser::Statement<common::Indirection<parser::StmtFunctionStmt>>>(
8965             &decl.u)}) {
8966       messageHandler().set_currStmtSource(statement->source);
8967       AnalyzeStmtFunctionStmt(statement->statement.value());
8968     }
8969   }
8970   // TODO: what about instantiations in BLOCK?
8971   CheckSaveStmts();
8972   CheckCommonBlocks();
8973   if (!inInterfaceBlock()) {
8974     // TODO: warn for the case where the EQUIVALENCE statement is in a
8975     // procedure declaration in an interface block
8976     CheckEquivalenceSets();
8977   }
8978 }
8979 
8980 // Analyze the bodies of statement functions now that the symbols in this
8981 // specification part have been fully declared and implicitly typed.
8982 // (Statement function references are not allowed in specification
8983 // expressions, so it's safe to defer processing their definitions.)
8984 void ResolveNamesVisitor::AnalyzeStmtFunctionStmt(
8985     const parser::StmtFunctionStmt &stmtFunc) {
8986   const auto &name{std::get<parser::Name>(stmtFunc.t)};
8987   Symbol *symbol{name.symbol};
8988   auto *details{symbol ? symbol->detailsIf<SubprogramDetails>() : nullptr};
8989   if (!details || !symbol->scope() ||
8990       &symbol->scope()->parent() != &currScope() || details->isInterface() ||
8991       details->isDummy() || details->entryScope() ||
8992       details->moduleInterface() || symbol->test(Symbol::Flag::Subroutine)) {
8993     return; // error recovery
8994   }
8995   // Resolve the symbols on the RHS of the statement function.
8996   PushScope(*symbol->scope());
8997   const auto &parsedExpr{std::get<parser::Scalar<parser::Expr>>(stmtFunc.t)};
8998   Walk(parsedExpr);
8999   PopScope();
9000   if (auto expr{AnalyzeExpr(context(), stmtFunc)}) {
9001     if (auto type{evaluate::DynamicType::From(*symbol)}) {
9002       if (auto converted{evaluate::ConvertToType(*type, std::move(*expr))}) {
9003         details->set_stmtFunction(std::move(*converted));
9004       } else {
9005         Say(name.source,
9006             "Defining expression of statement function '%s' cannot be converted to its result type %s"_err_en_US,
9007             name.source, type->AsFortran());
9008       }
9009     } else {
9010       details->set_stmtFunction(std::move(*expr));
9011     }
9012   }
9013   if (!details->stmtFunction()) {
9014     context().SetError(*symbol);
9015   }
9016 }
9017 
9018 void ResolveNamesVisitor::CheckImports() {
9019   auto &scope{currScope()};
9020   switch (scope.GetImportKind()) {
9021   case common::ImportKind::None:
9022     break;
9023   case common::ImportKind::All:
9024     // C8102: all entities in host must not be hidden
9025     for (const auto &pair : scope.parent()) {
9026       auto &name{pair.first};
9027       std::optional<SourceName> scopeName{scope.GetName()};
9028       if (!scopeName || name != *scopeName) {
9029         CheckImport(prevImportStmt_.value(), name);
9030       }
9031     }
9032     break;
9033   case common::ImportKind::Default:
9034   case common::ImportKind::Only:
9035     // C8102: entities named in IMPORT must not be hidden
9036     for (auto &name : scope.importNames()) {
9037       CheckImport(name, name);
9038     }
9039     break;
9040   }
9041 }
9042 
9043 void ResolveNamesVisitor::CheckImport(
9044     const SourceName &location, const SourceName &name) {
9045   if (auto *symbol{FindInScope(name)}) {
9046     const Symbol &ultimate{symbol->GetUltimate()};
9047     if (&ultimate.owner() == &currScope()) {
9048       Say(location, "'%s' from host is not accessible"_err_en_US, name)
9049           .Attach(symbol->name(), "'%s' is hidden by this entity"_because_en_US,
9050               symbol->name());
9051     }
9052   }
9053 }
9054 
9055 bool ResolveNamesVisitor::Pre(const parser::ImplicitStmt &x) {
9056   return CheckNotInBlock("IMPLICIT") && // C1107
9057       ImplicitRulesVisitor::Pre(x);
9058 }
9059 
9060 void ResolveNamesVisitor::Post(const parser::PointerObject &x) {
9061   common::visit(common::visitors{
9062                     [&](const parser::Name &x) { ResolveName(x); },
9063                     [&](const parser::StructureComponent &x) {
9064                       ResolveStructureComponent(x);
9065                     },
9066                 },
9067       x.u);
9068 }
9069 void ResolveNamesVisitor::Post(const parser::AllocateObject &x) {
9070   common::visit(common::visitors{
9071                     [&](const parser::Name &x) { ResolveName(x); },
9072                     [&](const parser::StructureComponent &x) {
9073                       ResolveStructureComponent(x);
9074                     },
9075                 },
9076       x.u);
9077 }
9078 
9079 bool ResolveNamesVisitor::Pre(const parser::PointerAssignmentStmt &x) {
9080   const auto &dataRef{std::get<parser::DataRef>(x.t)};
9081   const auto &bounds{std::get<parser::PointerAssignmentStmt::Bounds>(x.t)};
9082   const auto &expr{std::get<parser::Expr>(x.t)};
9083   ResolveDataRef(dataRef);
9084   Symbol *ptrSymbol{parser::GetLastName(dataRef).symbol};
9085   Walk(bounds);
9086   // Resolve unrestricted specific intrinsic procedures as in "p => cos".
9087   if (const parser::Name * name{parser::Unwrap<parser::Name>(expr)}) {
9088     if (NameIsKnownOrIntrinsic(*name)) {
9089       if (Symbol * symbol{name->symbol}) {
9090         if (IsProcedurePointer(ptrSymbol) &&
9091             !ptrSymbol->test(Symbol::Flag::Function) &&
9092             !ptrSymbol->test(Symbol::Flag::Subroutine)) {
9093           if (symbol->test(Symbol::Flag::Function)) {
9094             ApplyImplicitRules(*ptrSymbol);
9095           }
9096         }
9097         // If the name is known because it is an object entity from a host
9098         // procedure, create a host associated symbol.
9099         if (symbol->GetUltimate().has<ObjectEntityDetails>() &&
9100             IsUplevelReference(*symbol)) {
9101           MakeHostAssocSymbol(*name, *symbol);
9102         }
9103       }
9104       return false;
9105     }
9106     // Can also reference a global external procedure here
9107     if (auto it{context().globalScope().find(name->source)};
9108         it != context().globalScope().end()) {
9109       Symbol &global{*it->second};
9110       if (IsProcedure(global)) {
9111         Resolve(*name, global);
9112         return false;
9113       }
9114     }
9115     if (IsProcedurePointer(parser::GetLastName(dataRef).symbol) &&
9116         !FindSymbol(*name)) {
9117       // Unknown target of procedure pointer must be an external procedure
9118       Symbol &symbol{MakeSymbol(
9119           context().globalScope(), name->source, Attrs{Attr::EXTERNAL})};
9120       symbol.implicitAttrs().set(Attr::EXTERNAL);
9121       Resolve(*name, symbol);
9122       ConvertToProcEntity(symbol, name->source);
9123       return false;
9124     }
9125   }
9126   Walk(expr);
9127   return false;
9128 }
9129 void ResolveNamesVisitor::Post(const parser::Designator &x) {
9130   ResolveDesignator(x);
9131 }
9132 void ResolveNamesVisitor::Post(const parser::SubstringInquiry &x) {
9133   Walk(std::get<parser::SubstringRange>(x.v.t).t);
9134   ResolveDataRef(std::get<parser::DataRef>(x.v.t));
9135 }
9136 
9137 void ResolveNamesVisitor::Post(const parser::ProcComponentRef &x) {
9138   ResolveStructureComponent(x.v.thing);
9139 }
9140 void ResolveNamesVisitor::Post(const parser::TypeGuardStmt &x) {
9141   DeclTypeSpecVisitor::Post(x);
9142   ConstructVisitor::Post(x);
9143 }
9144 bool ResolveNamesVisitor::Pre(const parser::StmtFunctionStmt &x) {
9145   if (HandleStmtFunction(x)) {
9146     return false;
9147   } else {
9148     // This is an array element or pointer-valued function assignment:
9149     // resolve the names of indices/arguments
9150     const auto &names{std::get<std::list<parser::Name>>(x.t)};
9151     for (auto &name : names) {
9152       ResolveName(name);
9153     }
9154     return true;
9155   }
9156 }
9157 
9158 bool ResolveNamesVisitor::Pre(const parser::DefinedOpName &x) {
9159   const parser::Name &name{x.v};
9160   if (FindSymbol(name)) {
9161     // OK
9162   } else if (IsLogicalConstant(context(), name.source)) {
9163     Say(name,
9164         "Logical constant '%s' may not be used as a defined operator"_err_en_US);
9165   } else {
9166     // Resolved later in expression semantics
9167     MakePlaceholder(name, MiscDetails::Kind::TypeBoundDefinedOp);
9168   }
9169   return false;
9170 }
9171 
9172 void ResolveNamesVisitor::Post(const parser::AssignStmt &x) {
9173   if (auto *name{ResolveName(std::get<parser::Name>(x.t))}) {
9174     CheckEntryDummyUse(name->source, name->symbol);
9175     ConvertToObjectEntity(DEREF(name->symbol));
9176   }
9177 }
9178 void ResolveNamesVisitor::Post(const parser::AssignedGotoStmt &x) {
9179   if (auto *name{ResolveName(std::get<parser::Name>(x.t))}) {
9180     CheckEntryDummyUse(name->source, name->symbol);
9181     ConvertToObjectEntity(DEREF(name->symbol));
9182   }
9183 }
9184 
9185 void ResolveNamesVisitor::Post(const parser::CompilerDirective &x) {
9186   if (std::holds_alternative<parser::CompilerDirective::VectorAlways>(x.u)) {
9187     return;
9188   }
9189   if (const auto *tkr{
9190           std::get_if<std::list<parser::CompilerDirective::IgnoreTKR>>(&x.u)}) {
9191     if (currScope().IsTopLevel() ||
9192         GetProgramUnitContaining(currScope()).kind() !=
9193             Scope::Kind::Subprogram) {
9194       Say(x.source,
9195           "!DIR$ IGNORE_TKR directive must appear in a subroutine or function"_err_en_US);
9196       return;
9197     }
9198     if (!inSpecificationPart_) {
9199       Say(x.source,
9200           "!DIR$ IGNORE_TKR directive must appear in the specification part"_err_en_US);
9201       return;
9202     }
9203     if (tkr->empty()) {
9204       Symbol *symbol{currScope().symbol()};
9205       if (SubprogramDetails *
9206           subp{symbol ? symbol->detailsIf<SubprogramDetails>() : nullptr}) {
9207         subp->set_defaultIgnoreTKR(true);
9208       }
9209     } else {
9210       for (const parser::CompilerDirective::IgnoreTKR &item : *tkr) {
9211         common::IgnoreTKRSet set;
9212         if (const auto &maybeList{
9213                 std::get<std::optional<std::list<const char *>>>(item.t)}) {
9214           for (const char *p : *maybeList) {
9215             if (p) {
9216               switch (*p) {
9217               case 't':
9218                 set.set(common::IgnoreTKR::Type);
9219                 break;
9220               case 'k':
9221                 set.set(common::IgnoreTKR::Kind);
9222                 break;
9223               case 'r':
9224                 set.set(common::IgnoreTKR::Rank);
9225                 break;
9226               case 'd':
9227                 set.set(common::IgnoreTKR::Device);
9228                 break;
9229               case 'm':
9230                 set.set(common::IgnoreTKR::Managed);
9231                 break;
9232               case 'c':
9233                 set.set(common::IgnoreTKR::Contiguous);
9234                 break;
9235               case 'a':
9236                 set = common::ignoreTKRAll;
9237                 break;
9238               default:
9239                 Say(x.source,
9240                     "'%c' is not a valid letter for !DIR$ IGNORE_TKR directive"_err_en_US,
9241                     *p);
9242                 set = common::ignoreTKRAll;
9243                 break;
9244               }
9245             }
9246           }
9247           if (set.empty()) {
9248             Say(x.source,
9249                 "!DIR$ IGNORE_TKR directive may not have an empty parenthesized list of letters"_err_en_US);
9250           }
9251         } else { // no (list)
9252           set = common::ignoreTKRAll;
9253           ;
9254         }
9255         const auto &name{std::get<parser::Name>(item.t)};
9256         Symbol *symbol{FindSymbol(name)};
9257         if (!symbol) {
9258           symbol = &MakeSymbol(name, Attrs{}, ObjectEntityDetails{});
9259         }
9260         if (symbol->owner() != currScope()) {
9261           SayWithDecl(
9262               name, *symbol, "'%s' must be local to this subprogram"_err_en_US);
9263         } else {
9264           ConvertToObjectEntity(*symbol);
9265           if (auto *object{symbol->detailsIf<ObjectEntityDetails>()}) {
9266             object->set_ignoreTKR(set);
9267           } else {
9268             SayWithDecl(name, *symbol, "'%s' must be an object"_err_en_US);
9269           }
9270         }
9271       }
9272     }
9273   } else if (context().ShouldWarn(common::UsageWarning::IgnoredDirective)) {
9274     Say(x.source, "Unrecognized compiler directive was ignored"_warn_en_US)
9275         .set_usageWarning(common::UsageWarning::IgnoredDirective);
9276   }
9277 }
9278 
9279 bool ResolveNamesVisitor::Pre(const parser::ProgramUnit &x) {
9280   if (std::holds_alternative<common::Indirection<parser::CompilerDirective>>(
9281           x.u)) {
9282     // TODO: global directives
9283     return true;
9284   }
9285   if (std::holds_alternative<
9286           common::Indirection<parser::OpenACCRoutineConstruct>>(x.u)) {
9287     ResolveAccParts(context(), x, &topScope_);
9288     return false;
9289   }
9290   ProgramTree &root{ProgramTree::Build(x, context())};
9291   SetScope(topScope_);
9292   ResolveSpecificationParts(root);
9293   FinishSpecificationParts(root);
9294   ResolveExecutionParts(root);
9295   FinishExecutionParts(root);
9296   ResolveAccParts(context(), x, /*topScope=*/nullptr);
9297   ResolveOmpParts(context(), x);
9298   return false;
9299 }
9300 
9301 template <typename A> std::set<SourceName> GetUses(const A &x) {
9302   std::set<SourceName> uses;
9303   if constexpr (!std::is_same_v<A, parser::CompilerDirective> &&
9304       !std::is_same_v<A, parser::OpenACCRoutineConstruct>) {
9305     const auto &spec{std::get<parser::SpecificationPart>(x.t)};
9306     const auto &unitUses{std::get<
9307         std::list<parser::Statement<common::Indirection<parser::UseStmt>>>>(
9308         spec.t)};
9309     for (const auto &u : unitUses) {
9310       uses.insert(u.statement.value().moduleName.source);
9311     }
9312   }
9313   return uses;
9314 }
9315 
9316 bool ResolveNamesVisitor::Pre(const parser::Program &x) {
9317   std::map<SourceName, const parser::ProgramUnit *> modules;
9318   std::set<SourceName> uses;
9319   bool disordered{false};
9320   for (const auto &progUnit : x.v) {
9321     if (const auto *indMod{
9322             std::get_if<common::Indirection<parser::Module>>(&progUnit.u)}) {
9323       const parser::Module &mod{indMod->value()};
9324       const auto &moduleStmt{
9325           std::get<parser::Statement<parser::ModuleStmt>>(mod.t)};
9326       const SourceName &name{moduleStmt.statement.v.source};
9327       if (auto iter{modules.find(name)}; iter != modules.end()) {
9328         Say(name,
9329             "Module '%s' appears multiple times in a compilation unit"_err_en_US)
9330             .Attach(iter->first, "First definition of module"_en_US);
9331         return true;
9332       }
9333       modules.emplace(name, &progUnit);
9334       if (auto iter{uses.find(name)}; iter != uses.end()) {
9335         if (context().ShouldWarn(common::LanguageFeature::MiscUseExtensions)) {
9336           Say(name,
9337               "A USE statement referencing module '%s' appears earlier in this compilation unit"_port_en_US,
9338               name)
9339               .Attach(*iter, "First USE of module"_en_US);
9340         }
9341         disordered = true;
9342       }
9343     }
9344     for (SourceName used : common::visit(
9345              [](const auto &indUnit) { return GetUses(indUnit.value()); },
9346              progUnit.u)) {
9347       uses.insert(used);
9348     }
9349   }
9350   if (!disordered) {
9351     return true;
9352   }
9353   // Process modules in topological order
9354   std::vector<const parser::ProgramUnit *> moduleOrder;
9355   while (!modules.empty()) {
9356     bool ok;
9357     for (const auto &pair : modules) {
9358       const SourceName &name{pair.first};
9359       const parser::ProgramUnit &progUnit{*pair.second};
9360       const parser::Module &m{
9361           std::get<common::Indirection<parser::Module>>(progUnit.u).value()};
9362       ok = true;
9363       for (const SourceName &use : GetUses(m)) {
9364         if (modules.find(use) != modules.end()) {
9365           ok = false;
9366           break;
9367         }
9368       }
9369       if (ok) {
9370         moduleOrder.push_back(&progUnit);
9371         modules.erase(name);
9372         break;
9373       }
9374     }
9375     if (!ok) {
9376       Message *msg{nullptr};
9377       for (const auto &pair : modules) {
9378         if (msg) {
9379           msg->Attach(pair.first, "Module in a cycle"_en_US);
9380         } else {
9381           msg = &Say(pair.first,
9382               "Some modules in this compilation unit form one or more cycles of dependence"_err_en_US);
9383         }
9384       }
9385       return false;
9386     }
9387   }
9388   // Modules can be ordered.  Process them first, and then all of the other
9389   // program units.
9390   for (const parser::ProgramUnit *progUnit : moduleOrder) {
9391     Walk(*progUnit);
9392   }
9393   for (const auto &progUnit : x.v) {
9394     if (!std::get_if<common::Indirection<parser::Module>>(&progUnit.u)) {
9395       Walk(progUnit);
9396     }
9397   }
9398   return false;
9399 }
9400 
9401 // References to procedures need to record that their symbols are known
9402 // to be procedures, so that they don't get converted to objects by default.
9403 class ExecutionPartCallSkimmer : public ExecutionPartSkimmerBase {
9404 public:
9405   explicit ExecutionPartCallSkimmer(ResolveNamesVisitor &resolver)
9406       : resolver_{resolver} {}
9407 
9408   void Walk(const parser::ExecutionPart &exec) {
9409     parser::Walk(exec, *this);
9410     EndWalk();
9411   }
9412 
9413   using ExecutionPartSkimmerBase::Post;
9414   using ExecutionPartSkimmerBase::Pre;
9415 
9416   void Post(const parser::FunctionReference &fr) {
9417     NoteCall(Symbol::Flag::Function, fr.v, false);
9418   }
9419   void Post(const parser::CallStmt &cs) {
9420     NoteCall(Symbol::Flag::Subroutine, cs.call, cs.chevrons.has_value());
9421   }
9422 
9423 private:
9424   void NoteCall(
9425       Symbol::Flag flag, const parser::Call &call, bool hasCUDAChevrons) {
9426     auto &designator{std::get<parser::ProcedureDesignator>(call.t)};
9427     if (const auto *name{std::get_if<parser::Name>(&designator.u)}) {
9428       if (!IsHidden(name->source)) {
9429         resolver_.NoteExecutablePartCall(flag, name->source, hasCUDAChevrons);
9430       }
9431     }
9432   }
9433 
9434   ResolveNamesVisitor &resolver_;
9435 };
9436 
9437 // Build the scope tree and resolve names in the specification parts of this
9438 // node and its children
9439 void ResolveNamesVisitor::ResolveSpecificationParts(ProgramTree &node) {
9440   if (node.isSpecificationPartResolved()) {
9441     return; // been here already
9442   }
9443   node.set_isSpecificationPartResolved();
9444   if (!BeginScopeForNode(node)) {
9445     return; // an error prevented scope from being created
9446   }
9447   Scope &scope{currScope()};
9448   node.set_scope(scope);
9449   AddSubpNames(node);
9450   common::visit(
9451       [&](const auto *x) {
9452         if (x) {
9453           Walk(*x);
9454         }
9455       },
9456       node.stmt());
9457   Walk(node.spec());
9458   // If this is a function, convert result to an object. This is to prevent the
9459   // result from being converted later to a function symbol if it is called
9460   // inside the function.
9461   // If the result is function pointer, then ConvertToObjectEntity will not
9462   // convert the result to an object, and calling the symbol inside the function
9463   // will result in calls to the result pointer.
9464   // A function cannot be called recursively if RESULT was not used to define a
9465   // distinct result name (15.6.2.2 point 4.).
9466   if (Symbol * symbol{scope.symbol()}) {
9467     if (auto *details{symbol->detailsIf<SubprogramDetails>()}) {
9468       if (details->isFunction()) {
9469         ConvertToObjectEntity(const_cast<Symbol &>(details->result()));
9470       }
9471     }
9472   }
9473   if (node.IsModule()) {
9474     ApplyDefaultAccess();
9475   }
9476   for (auto &child : node.children()) {
9477     ResolveSpecificationParts(child);
9478   }
9479   if (node.exec()) {
9480     ExecutionPartCallSkimmer{*this}.Walk(*node.exec());
9481     HandleImpliedAsynchronousInScope(node.exec()->v);
9482   }
9483   EndScopeForNode(node);
9484   // Ensure that every object entity has a type.
9485   bool inModule{node.GetKind() == ProgramTree::Kind::Module ||
9486       node.GetKind() == ProgramTree::Kind::Submodule};
9487   for (auto &pair : *node.scope()) {
9488     Symbol &symbol{*pair.second};
9489     if (inModule && symbol.attrs().test(Attr::EXTERNAL) && !IsPointer(symbol) &&
9490         !symbol.test(Symbol::Flag::Function) &&
9491         !symbol.test(Symbol::Flag::Subroutine)) {
9492       // in a module, external proc without return type is subroutine
9493       symbol.set(
9494           symbol.GetType() ? Symbol::Flag::Function : Symbol::Flag::Subroutine);
9495     }
9496     ApplyImplicitRules(symbol);
9497   }
9498 }
9499 
9500 // Add SubprogramNameDetails symbols for module and internal subprograms and
9501 // their ENTRY statements.
9502 void ResolveNamesVisitor::AddSubpNames(ProgramTree &node) {
9503   auto kind{
9504       node.IsModule() ? SubprogramKind::Module : SubprogramKind::Internal};
9505   for (auto &child : node.children()) {
9506     auto &symbol{MakeSymbol(child.name(), SubprogramNameDetails{kind, child})};
9507     if (child.HasModulePrefix()) {
9508       SetExplicitAttr(symbol, Attr::MODULE);
9509     }
9510     if (child.bindingSpec()) {
9511       SetExplicitAttr(symbol, Attr::BIND_C);
9512     }
9513     auto childKind{child.GetKind()};
9514     if (childKind == ProgramTree::Kind::Function) {
9515       symbol.set(Symbol::Flag::Function);
9516     } else if (childKind == ProgramTree::Kind::Subroutine) {
9517       symbol.set(Symbol::Flag::Subroutine);
9518     } else {
9519       continue; // make ENTRY symbols only where valid
9520     }
9521     for (const auto &entryStmt : child.entryStmts()) {
9522       SubprogramNameDetails details{kind, child};
9523       auto &symbol{
9524           MakeSymbol(std::get<parser::Name>(entryStmt->t), std::move(details))};
9525       symbol.set(child.GetSubpFlag());
9526       if (child.HasModulePrefix()) {
9527         SetExplicitAttr(symbol, Attr::MODULE);
9528       }
9529       if (child.bindingSpec()) {
9530         SetExplicitAttr(symbol, Attr::BIND_C);
9531       }
9532     }
9533   }
9534   for (const auto &generic : node.genericSpecs()) {
9535     if (const auto *name{std::get_if<parser::Name>(&generic->u)}) {
9536       if (currScope().find(name->source) != currScope().end()) {
9537         // If this scope has both a generic interface and a contained
9538         // subprogram with the same name, create the generic's symbol
9539         // now so that any other generics of the same name that are pulled
9540         // into scope later via USE association will properly merge instead
9541         // of raising a bogus error due a conflict with the subprogram.
9542         CreateGeneric(*generic);
9543       }
9544     }
9545   }
9546 }
9547 
9548 // Push a new scope for this node or return false on error.
9549 bool ResolveNamesVisitor::BeginScopeForNode(const ProgramTree &node) {
9550   switch (node.GetKind()) {
9551     SWITCH_COVERS_ALL_CASES
9552   case ProgramTree::Kind::Program:
9553     PushScope(Scope::Kind::MainProgram,
9554         &MakeSymbol(node.name(), MainProgramDetails{}));
9555     return true;
9556   case ProgramTree::Kind::Function:
9557   case ProgramTree::Kind::Subroutine:
9558     return BeginSubprogram(node.name(), node.GetSubpFlag(),
9559         node.HasModulePrefix(), node.bindingSpec(), &node.entryStmts());
9560   case ProgramTree::Kind::MpSubprogram:
9561     return BeginMpSubprogram(node.name());
9562   case ProgramTree::Kind::Module:
9563     BeginModule(node.name(), false);
9564     return true;
9565   case ProgramTree::Kind::Submodule:
9566     return BeginSubmodule(node.name(), node.GetParentId());
9567   case ProgramTree::Kind::BlockData:
9568     PushBlockDataScope(node.name());
9569     return true;
9570   }
9571 }
9572 
9573 void ResolveNamesVisitor::EndScopeForNode(const ProgramTree &node) {
9574   std::optional<parser::CharBlock> stmtSource;
9575   const std::optional<parser::LanguageBindingSpec> *binding{nullptr};
9576   common::visit(
9577       common::visitors{
9578           [&](const parser::Statement<parser::FunctionStmt> *stmt) {
9579             if (stmt) {
9580               stmtSource = stmt->source;
9581               if (const auto &maybeSuffix{
9582                       std::get<std::optional<parser::Suffix>>(
9583                           stmt->statement.t)}) {
9584                 binding = &maybeSuffix->binding;
9585               }
9586             }
9587           },
9588           [&](const parser::Statement<parser::SubroutineStmt> *stmt) {
9589             if (stmt) {
9590               stmtSource = stmt->source;
9591               binding = &std::get<std::optional<parser::LanguageBindingSpec>>(
9592                   stmt->statement.t);
9593             }
9594           },
9595           [](const auto *) {},
9596       },
9597       node.stmt());
9598   EndSubprogram(stmtSource, binding, &node.entryStmts());
9599 }
9600 
9601 // Some analyses and checks, such as the processing of initializers of
9602 // pointers, are deferred until all of the pertinent specification parts
9603 // have been visited.  This deferred processing enables the use of forward
9604 // references in these circumstances.
9605 // Data statement objects with implicit derived types are finally
9606 // resolved here.
9607 class DeferredCheckVisitor {
9608 public:
9609   explicit DeferredCheckVisitor(ResolveNamesVisitor &resolver)
9610       : resolver_{resolver} {}
9611 
9612   template <typename A> void Walk(const A &x) { parser::Walk(x, *this); }
9613 
9614   template <typename A> bool Pre(const A &) { return true; }
9615   template <typename A> void Post(const A &) {}
9616 
9617   void Post(const parser::DerivedTypeStmt &x) {
9618     const auto &name{std::get<parser::Name>(x.t)};
9619     if (Symbol * symbol{name.symbol}) {
9620       if (Scope * scope{symbol->scope()}) {
9621         if (scope->IsDerivedType()) {
9622           CHECK(outerScope_ == nullptr);
9623           outerScope_ = &resolver_.currScope();
9624           resolver_.SetScope(*scope);
9625         }
9626       }
9627     }
9628   }
9629   void Post(const parser::EndTypeStmt &) {
9630     if (outerScope_) {
9631       resolver_.SetScope(*outerScope_);
9632       outerScope_ = nullptr;
9633     }
9634   }
9635 
9636   void Post(const parser::ProcInterface &pi) {
9637     if (const auto *name{std::get_if<parser::Name>(&pi.u)}) {
9638       resolver_.CheckExplicitInterface(*name);
9639     }
9640   }
9641   bool Pre(const parser::EntityDecl &decl) {
9642     Init(std::get<parser::Name>(decl.t),
9643         std::get<std::optional<parser::Initialization>>(decl.t));
9644     return false;
9645   }
9646   bool Pre(const parser::ComponentDecl &decl) {
9647     Init(std::get<parser::Name>(decl.t),
9648         std::get<std::optional<parser::Initialization>>(decl.t));
9649     return false;
9650   }
9651   bool Pre(const parser::ProcDecl &decl) {
9652     if (const auto &init{
9653             std::get<std::optional<parser::ProcPointerInit>>(decl.t)}) {
9654       resolver_.PointerInitialization(std::get<parser::Name>(decl.t), *init);
9655     }
9656     return false;
9657   }
9658   void Post(const parser::TypeBoundProcedureStmt::WithInterface &tbps) {
9659     resolver_.CheckExplicitInterface(tbps.interfaceName);
9660   }
9661   void Post(const parser::TypeBoundProcedureStmt::WithoutInterface &tbps) {
9662     if (outerScope_) {
9663       resolver_.CheckBindings(tbps);
9664     }
9665   }
9666   bool Pre(const parser::DataStmtObject &) {
9667     ++dataStmtObjectNesting_;
9668     return true;
9669   }
9670   void Post(const parser::DataStmtObject &) { --dataStmtObjectNesting_; }
9671   void Post(const parser::Designator &x) {
9672     if (dataStmtObjectNesting_ > 0) {
9673       resolver_.ResolveDesignator(x);
9674     }
9675   }
9676 
9677 private:
9678   void Init(const parser::Name &name,
9679       const std::optional<parser::Initialization> &init) {
9680     if (init) {
9681       if (const auto *target{
9682               std::get_if<parser::InitialDataTarget>(&init->u)}) {
9683         resolver_.PointerInitialization(name, *target);
9684       } else if (const auto *expr{
9685                      std::get_if<parser::ConstantExpr>(&init->u)}) {
9686         if (name.symbol) {
9687           if (const auto *object{name.symbol->detailsIf<ObjectEntityDetails>()};
9688               !object || !object->init()) {
9689             resolver_.NonPointerInitialization(name, *expr);
9690           }
9691         }
9692       }
9693     }
9694   }
9695 
9696   ResolveNamesVisitor &resolver_;
9697   Scope *outerScope_{nullptr};
9698   int dataStmtObjectNesting_{0};
9699 };
9700 
9701 // Perform checks and completions that need to happen after all of
9702 // the specification parts but before any of the execution parts.
9703 void ResolveNamesVisitor::FinishSpecificationParts(const ProgramTree &node) {
9704   if (!node.scope()) {
9705     return; // error occurred creating scope
9706   }
9707   auto flagRestorer{common::ScopedSet(inSpecificationPart_, true)};
9708   SetScope(*node.scope());
9709   // The initializers of pointers and non-PARAMETER objects, the default
9710   // initializers of components, and non-deferred type-bound procedure
9711   // bindings have not yet been traversed.
9712   // We do that now, when any forward references that appeared
9713   // in those initializers will resolve to the right symbols without
9714   // incurring spurious errors with IMPLICIT NONE or forward references
9715   // to nested subprograms.
9716   DeferredCheckVisitor{*this}.Walk(node.spec());
9717   for (Scope &childScope : currScope().children()) {
9718     if (childScope.IsParameterizedDerivedTypeInstantiation()) {
9719       FinishDerivedTypeInstantiation(childScope);
9720     }
9721   }
9722   for (const auto &child : node.children()) {
9723     FinishSpecificationParts(child);
9724   }
9725 }
9726 
9727 void ResolveNamesVisitor::FinishExecutionParts(const ProgramTree &node) {
9728   if (node.scope()) {
9729     SetScope(*node.scope());
9730     if (node.exec()) {
9731       DeferredCheckVisitor{*this}.Walk(*node.exec());
9732     }
9733     for (const auto &child : node.children()) {
9734       FinishExecutionParts(child);
9735     }
9736   }
9737 }
9738 
9739 // Duplicate and fold component object pointer default initializer designators
9740 // using the actual type parameter values of each particular instantiation.
9741 // Validation is done later in declaration checking.
9742 void ResolveNamesVisitor::FinishDerivedTypeInstantiation(Scope &scope) {
9743   CHECK(scope.IsDerivedType() && !scope.symbol());
9744   if (DerivedTypeSpec * spec{scope.derivedTypeSpec()}) {
9745     spec->Instantiate(currScope());
9746     const Symbol &origTypeSymbol{spec->typeSymbol()};
9747     if (const Scope * origTypeScope{origTypeSymbol.scope()}) {
9748       CHECK(origTypeScope->IsDerivedType() &&
9749           origTypeScope->symbol() == &origTypeSymbol);
9750       auto &foldingContext{GetFoldingContext()};
9751       auto restorer{foldingContext.WithPDTInstance(*spec)};
9752       for (auto &pair : scope) {
9753         Symbol &comp{*pair.second};
9754         const Symbol &origComp{DEREF(FindInScope(*origTypeScope, comp.name()))};
9755         if (IsPointer(comp)) {
9756           if (auto *details{comp.detailsIf<ObjectEntityDetails>()}) {
9757             auto origDetails{origComp.get<ObjectEntityDetails>()};
9758             if (const MaybeExpr & init{origDetails.init()}) {
9759               SomeExpr newInit{*init};
9760               MaybeExpr folded{FoldExpr(std::move(newInit))};
9761               details->set_init(std::move(folded));
9762             }
9763           }
9764         }
9765       }
9766     }
9767   }
9768 }
9769 
9770 // Resolve names in the execution part of this node and its children
9771 void ResolveNamesVisitor::ResolveExecutionParts(const ProgramTree &node) {
9772   if (!node.scope()) {
9773     return; // error occurred creating scope
9774   }
9775   SetScope(*node.scope());
9776   if (const auto *exec{node.exec()}) {
9777     Walk(*exec);
9778   }
9779   FinishNamelists();
9780   if (node.IsModule()) {
9781     // A second final pass to catch new symbols added from implicitly
9782     // typed names in NAMELIST groups or the specification parts of
9783     // module subprograms.
9784     ApplyDefaultAccess();
9785   }
9786   PopScope(); // converts unclassified entities into objects
9787   for (const auto &child : node.children()) {
9788     ResolveExecutionParts(child);
9789   }
9790 }
9791 
9792 void ResolveNamesVisitor::Post(const parser::Program &x) {
9793   // ensure that all temps were deallocated
9794   CHECK(!attrs_);
9795   CHECK(!cudaDataAttr_);
9796   CHECK(!GetDeclTypeSpec());
9797   // Top-level resolution to propagate information across program units after
9798   // each of them has been resolved separately.
9799   ResolveOmpTopLevelParts(context(), x);
9800 }
9801 
9802 // A singleton instance of the scope -> IMPLICIT rules mapping is
9803 // shared by all instances of ResolveNamesVisitor and accessed by this
9804 // pointer when the visitors (other than the top-level original) are
9805 // constructed.
9806 static ImplicitRulesMap *sharedImplicitRulesMap{nullptr};
9807 
9808 bool ResolveNames(
9809     SemanticsContext &context, const parser::Program &program, Scope &top) {
9810   ImplicitRulesMap implicitRulesMap;
9811   auto restorer{common::ScopedSet(sharedImplicitRulesMap, &implicitRulesMap)};
9812   ResolveNamesVisitor{context, implicitRulesMap, top}.Walk(program);
9813   return !context.AnyFatalError();
9814 }
9815 
9816 // Processes a module (but not internal) function when it is referenced
9817 // in a specification expression in a sibling procedure.
9818 void ResolveSpecificationParts(
9819     SemanticsContext &context, const Symbol &subprogram) {
9820   auto originalLocation{context.location()};
9821   ImplicitRulesMap implicitRulesMap;
9822   bool localImplicitRulesMap{false};
9823   if (!sharedImplicitRulesMap) {
9824     sharedImplicitRulesMap = &implicitRulesMap;
9825     localImplicitRulesMap = true;
9826   }
9827   ResolveNamesVisitor visitor{
9828       context, *sharedImplicitRulesMap, context.globalScope()};
9829   const auto &details{subprogram.get<SubprogramNameDetails>()};
9830   ProgramTree &node{details.node()};
9831   const Scope &moduleScope{subprogram.owner()};
9832   if (localImplicitRulesMap) {
9833     visitor.BeginScope(const_cast<Scope &>(moduleScope));
9834   } else {
9835     visitor.SetScope(const_cast<Scope &>(moduleScope));
9836   }
9837   visitor.ResolveSpecificationParts(node);
9838   context.set_location(std::move(originalLocation));
9839   if (localImplicitRulesMap) {
9840     sharedImplicitRulesMap = nullptr;
9841   }
9842 }
9843 
9844 } // namespace Fortran::semantics
9845