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