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