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