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