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