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