1 //===-- lib/Semantics/check-declarations.cpp ------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // Static declaration checking 10 11 #include "check-declarations.h" 12 #include "pointer-assignment.h" 13 #include "flang/Evaluate/check-expression.h" 14 #include "flang/Evaluate/fold.h" 15 #include "flang/Evaluate/tools.h" 16 #include "flang/Semantics/scope.h" 17 #include "flang/Semantics/semantics.h" 18 #include "flang/Semantics/symbol.h" 19 #include "flang/Semantics/tools.h" 20 #include "flang/Semantics/type.h" 21 #include <algorithm> 22 #include <map> 23 #include <string> 24 25 namespace Fortran::semantics { 26 27 namespace characteristics = evaluate::characteristics; 28 using characteristics::DummyArgument; 29 using characteristics::DummyDataObject; 30 using characteristics::DummyProcedure; 31 using characteristics::FunctionResult; 32 using characteristics::Procedure; 33 34 class CheckHelper { 35 public: 36 explicit CheckHelper(SemanticsContext &c) : context_{c} {} 37 CheckHelper(SemanticsContext &c, const Scope &s) : context_{c}, scope_{&s} {} 38 39 SemanticsContext &context() { return context_; } 40 void Check() { Check(context_.globalScope()); } 41 void Check(const ParamValue &, bool canBeAssumed); 42 void Check(const Bound &bound) { CheckSpecExpr(bound.GetExplicit()); } 43 void Check(const ShapeSpec &spec) { 44 Check(spec.lbound()); 45 Check(spec.ubound()); 46 } 47 void Check(const ArraySpec &); 48 void Check(const DeclTypeSpec &, bool canHaveAssumedTypeParameters); 49 void Check(const Symbol &); 50 void Check(const Scope &); 51 const Procedure *Characterize(const Symbol &); 52 53 private: 54 template <typename A> void CheckSpecExpr(const A &x) { 55 evaluate::CheckSpecificationExpr(x, DEREF(scope_), foldingContext_); 56 } 57 void CheckValue(const Symbol &, const DerivedTypeSpec *); 58 void CheckVolatile(const Symbol &, const DerivedTypeSpec *); 59 void CheckPointer(const Symbol &); 60 void CheckPassArg( 61 const Symbol &proc, const Symbol *interface, const WithPassArg &); 62 void CheckProcBinding(const Symbol &, const ProcBindingDetails &); 63 void CheckObjectEntity(const Symbol &, const ObjectEntityDetails &); 64 void CheckPointerInitialization(const Symbol &); 65 void CheckArraySpec(const Symbol &, const ArraySpec &); 66 void CheckProcEntity(const Symbol &, const ProcEntityDetails &); 67 void CheckSubprogram(const Symbol &, const SubprogramDetails &); 68 void CheckAssumedTypeEntity(const Symbol &, const ObjectEntityDetails &); 69 void CheckDerivedType(const Symbol &, const DerivedTypeDetails &); 70 bool CheckFinal( 71 const Symbol &subroutine, SourceName, const Symbol &derivedType); 72 bool CheckDistinguishableFinals(const Symbol &f1, SourceName f1name, 73 const Symbol &f2, SourceName f2name, const Symbol &derivedType); 74 void CheckGeneric(const Symbol &, const GenericDetails &); 75 void CheckHostAssoc(const Symbol &, const HostAssocDetails &); 76 bool CheckDefinedOperator( 77 SourceName, GenericKind, const Symbol &, const Procedure &); 78 std::optional<parser::MessageFixedText> CheckNumberOfArgs( 79 const GenericKind &, std::size_t); 80 bool CheckDefinedOperatorArg( 81 const SourceName &, const Symbol &, const Procedure &, std::size_t); 82 bool CheckDefinedAssignment(const Symbol &, const Procedure &); 83 bool CheckDefinedAssignmentArg(const Symbol &, const DummyArgument &, int); 84 void CheckSpecificsAreDistinguishable(const Symbol &, const GenericDetails &); 85 void CheckEquivalenceSet(const EquivalenceSet &); 86 void CheckBlockData(const Scope &); 87 void CheckGenericOps(const Scope &); 88 bool CheckConflicting(const Symbol &, Attr, Attr); 89 void WarnMissingFinal(const Symbol &); 90 bool InPure() const { 91 return innermostSymbol_ && IsPureProcedure(*innermostSymbol_); 92 } 93 bool InElemental() const { 94 return innermostSymbol_ && innermostSymbol_->attrs().test(Attr::ELEMENTAL); 95 } 96 bool InFunction() const { 97 return innermostSymbol_ && IsFunction(*innermostSymbol_); 98 } 99 template <typename... A> 100 void SayWithDeclaration(const Symbol &symbol, A &&...x) { 101 if (parser::Message * msg{messages_.Say(std::forward<A>(x)...)}) { 102 if (messages_.at().begin() != symbol.name().begin()) { 103 evaluate::AttachDeclaration(*msg, symbol); 104 } 105 } 106 } 107 bool IsResultOkToDiffer(const FunctionResult &); 108 void CheckBindCName(const Symbol &); 109 // Check functions for defined I/O procedures 110 void CheckDefinedIoProc( 111 const Symbol &, const GenericDetails &, GenericKind::DefinedIo); 112 bool CheckDioDummyIsData(const Symbol &, const Symbol *, std::size_t); 113 void CheckDioDummyIsDerived( 114 const Symbol &, const Symbol &, GenericKind::DefinedIo ioKind); 115 void CheckDioDummyIsDefaultInteger(const Symbol &, const Symbol &); 116 void CheckDioDummyIsScalar(const Symbol &, const Symbol &); 117 void CheckDioDummyAttrs(const Symbol &, const Symbol &, Attr); 118 void CheckDioDtvArg(const Symbol &, const Symbol *, GenericKind::DefinedIo); 119 void CheckDefaultIntegerArg(const Symbol &, const Symbol *, Attr); 120 void CheckDioAssumedLenCharacterArg( 121 const Symbol &, const Symbol *, std::size_t, Attr); 122 void CheckDioVlistArg(const Symbol &, const Symbol *, std::size_t); 123 void CheckDioArgCount( 124 const Symbol &, GenericKind::DefinedIo ioKind, std::size_t); 125 struct TypeWithDefinedIo { 126 const DerivedTypeSpec *type; 127 GenericKind::DefinedIo ioKind; 128 const Symbol &proc; 129 }; 130 void CheckAlreadySeenDefinedIo( 131 const DerivedTypeSpec *, GenericKind::DefinedIo, const Symbol &); 132 133 SemanticsContext &context_; 134 evaluate::FoldingContext &foldingContext_{context_.foldingContext()}; 135 parser::ContextualMessages &messages_{foldingContext_.messages()}; 136 const Scope *scope_{nullptr}; 137 bool scopeIsUninstantiatedPDT_{false}; 138 // This symbol is the one attached to the innermost enclosing scope 139 // that has a symbol. 140 const Symbol *innermostSymbol_{nullptr}; 141 // Cache of calls to Procedure::Characterize(Symbol) 142 std::map<SymbolRef, std::optional<Procedure>, SymbolAddressCompare> 143 characterizeCache_; 144 // Collection of symbols with BIND(C) names 145 std::map<std::string, SymbolRef> bindC_; 146 // Derived types that have defined input/output procedures 147 std::vector<TypeWithDefinedIo> seenDefinedIoTypes_; 148 }; 149 150 class DistinguishabilityHelper { 151 public: 152 DistinguishabilityHelper(SemanticsContext &context) : context_{context} {} 153 void Add(const Symbol &, GenericKind, const Symbol &, const Procedure &); 154 void Check(const Scope &); 155 156 private: 157 void SayNotDistinguishable(const Scope &, const SourceName &, GenericKind, 158 const Symbol &, const Symbol &); 159 void AttachDeclaration(parser::Message &, const Scope &, const Symbol &); 160 161 SemanticsContext &context_; 162 struct ProcedureInfo { 163 GenericKind kind; 164 const Symbol &symbol; 165 const Procedure &procedure; 166 }; 167 std::map<SourceName, std::vector<ProcedureInfo>> nameToInfo_; 168 }; 169 170 void CheckHelper::Check(const ParamValue &value, bool canBeAssumed) { 171 if (value.isAssumed()) { 172 if (!canBeAssumed) { // C795, C721, C726 173 messages_.Say( 174 "An assumed (*) type parameter may be used only for a (non-statement" 175 " function) dummy argument, associate name, named constant, or" 176 " external function result"_err_en_US); 177 } 178 } else { 179 CheckSpecExpr(value.GetExplicit()); 180 } 181 } 182 183 void CheckHelper::Check(const ArraySpec &shape) { 184 for (const auto &spec : shape) { 185 Check(spec); 186 } 187 } 188 189 void CheckHelper::Check( 190 const DeclTypeSpec &type, bool canHaveAssumedTypeParameters) { 191 if (type.category() == DeclTypeSpec::Character) { 192 Check(type.characterTypeSpec().length(), canHaveAssumedTypeParameters); 193 } else if (const DerivedTypeSpec * derived{type.AsDerived()}) { 194 for (auto &parm : derived->parameters()) { 195 Check(parm.second, canHaveAssumedTypeParameters); 196 } 197 } 198 } 199 200 void CheckHelper::Check(const Symbol &symbol) { 201 if (context_.HasError(symbol)) { 202 return; 203 } 204 auto restorer{messages_.SetLocation(symbol.name())}; 205 context_.set_location(symbol.name()); 206 const DeclTypeSpec *type{symbol.GetType()}; 207 const DerivedTypeSpec *derived{type ? type->AsDerived() : nullptr}; 208 bool isDone{false}; 209 std::visit( 210 common::visitors{ 211 [&](const UseDetails &x) { isDone = true; }, 212 [&](const HostAssocDetails &x) { 213 CheckHostAssoc(symbol, x); 214 isDone = true; 215 }, 216 [&](const ProcBindingDetails &x) { 217 CheckProcBinding(symbol, x); 218 isDone = true; 219 }, 220 [&](const ObjectEntityDetails &x) { CheckObjectEntity(symbol, x); }, 221 [&](const ProcEntityDetails &x) { CheckProcEntity(symbol, x); }, 222 [&](const SubprogramDetails &x) { CheckSubprogram(symbol, x); }, 223 [&](const DerivedTypeDetails &x) { CheckDerivedType(symbol, x); }, 224 [&](const GenericDetails &x) { CheckGeneric(symbol, x); }, 225 [](const auto &) {}, 226 }, 227 symbol.details()); 228 if (symbol.attrs().test(Attr::VOLATILE)) { 229 CheckVolatile(symbol, derived); 230 } 231 CheckBindCName(symbol); 232 if (isDone) { 233 return; // following checks do not apply 234 } 235 if (IsPointer(symbol)) { 236 CheckPointer(symbol); 237 } 238 if (InPure()) { 239 if (IsSaved(symbol)) { 240 messages_.Say( 241 "A pure subprogram may not have a variable with the SAVE attribute"_err_en_US); 242 } 243 if (symbol.attrs().test(Attr::VOLATILE)) { 244 messages_.Say( 245 "A pure subprogram may not have a variable with the VOLATILE attribute"_err_en_US); 246 } 247 if (IsProcedure(symbol) && !IsPureProcedure(symbol) && IsDummy(symbol)) { 248 messages_.Say( 249 "A dummy procedure of a pure subprogram must be pure"_err_en_US); 250 } 251 if (!IsDummy(symbol) && !IsFunctionResult(symbol)) { 252 if (IsPolymorphicAllocatable(symbol)) { 253 SayWithDeclaration(symbol, 254 "Deallocation of polymorphic object '%s' is not permitted in a pure subprogram"_err_en_US, 255 symbol.name()); 256 } else if (derived) { 257 if (auto bad{FindPolymorphicAllocatableUltimateComponent(*derived)}) { 258 SayWithDeclaration(*bad, 259 "Deallocation of polymorphic object '%s%s' is not permitted in a pure subprogram"_err_en_US, 260 symbol.name(), bad.BuildResultDesignatorName()); 261 } 262 } 263 } 264 } 265 if (type) { // Section 7.2, paragraph 7 266 bool canHaveAssumedParameter{IsNamedConstant(symbol) || 267 (IsAssumedLengthCharacter(symbol) && // C722 268 IsExternal(symbol)) || 269 symbol.test(Symbol::Flag::ParentComp)}; 270 if (!IsStmtFunctionDummy(symbol)) { // C726 271 if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) { 272 canHaveAssumedParameter |= object->isDummy() || 273 (object->isFuncResult() && 274 type->category() == DeclTypeSpec::Character) || 275 IsStmtFunctionResult(symbol); // Avoids multiple messages 276 } else { 277 canHaveAssumedParameter |= symbol.has<AssocEntityDetails>(); 278 } 279 } 280 Check(*type, canHaveAssumedParameter); 281 if (InPure() && InFunction() && IsFunctionResult(symbol)) { 282 if (derived && HasImpureFinal(*derived)) { // C1584 283 messages_.Say( 284 "Result of pure function may not have an impure FINAL subroutine"_err_en_US); 285 } 286 if (type->IsPolymorphic() && IsAllocatable(symbol)) { // C1585 287 messages_.Say( 288 "Result of pure function may not be both polymorphic and ALLOCATABLE"_err_en_US); 289 } 290 if (derived) { 291 if (auto bad{FindPolymorphicAllocatableUltimateComponent(*derived)}) { 292 SayWithDeclaration(*bad, 293 "Result of pure function may not have polymorphic ALLOCATABLE ultimate component '%s'"_err_en_US, 294 bad.BuildResultDesignatorName()); 295 } 296 } 297 } 298 } 299 if (IsAssumedLengthCharacter(symbol) && IsExternal(symbol)) { // C723 300 if (symbol.attrs().test(Attr::RECURSIVE)) { 301 messages_.Say( 302 "An assumed-length CHARACTER(*) function cannot be RECURSIVE"_err_en_US); 303 } 304 if (symbol.Rank() > 0) { 305 messages_.Say( 306 "An assumed-length CHARACTER(*) function cannot return an array"_err_en_US); 307 } 308 if (symbol.attrs().test(Attr::PURE)) { 309 messages_.Say( 310 "An assumed-length CHARACTER(*) function cannot be PURE"_err_en_US); 311 } 312 if (symbol.attrs().test(Attr::ELEMENTAL)) { 313 messages_.Say( 314 "An assumed-length CHARACTER(*) function cannot be ELEMENTAL"_err_en_US); 315 } 316 if (const Symbol * result{FindFunctionResult(symbol)}) { 317 if (IsPointer(*result)) { 318 messages_.Say( 319 "An assumed-length CHARACTER(*) function cannot return a POINTER"_err_en_US); 320 } 321 } 322 } 323 if (symbol.attrs().test(Attr::VALUE)) { 324 CheckValue(symbol, derived); 325 } 326 if (symbol.attrs().test(Attr::CONTIGUOUS) && IsPointer(symbol) && 327 symbol.Rank() == 0) { // C830 328 messages_.Say("CONTIGUOUS POINTER must be an array"_err_en_US); 329 } 330 if (IsDummy(symbol)) { 331 if (IsNamedConstant(symbol)) { 332 messages_.Say( 333 "A dummy argument may not also be a named constant"_err_en_US); 334 } 335 if (!symbol.test(Symbol::Flag::InDataStmt) /*caught elsewhere*/ && 336 IsSaved(symbol)) { 337 messages_.Say( 338 "A dummy argument may not have the SAVE attribute"_err_en_US); 339 } 340 } else if (IsFunctionResult(symbol)) { 341 if (!symbol.test(Symbol::Flag::InDataStmt) /*caught elsewhere*/ && 342 IsSaved(symbol)) { 343 messages_.Say( 344 "A function result may not have the SAVE attribute"_err_en_US); 345 } 346 } 347 if (symbol.owner().IsDerivedType() && 348 (symbol.attrs().test(Attr::CONTIGUOUS) && 349 !(IsPointer(symbol) && symbol.Rank() > 0))) { // C752 350 messages_.Say( 351 "A CONTIGUOUS component must be an array with the POINTER attribute"_err_en_US); 352 } 353 if (symbol.owner().IsModule() && IsAutomatic(symbol)) { 354 messages_.Say( 355 "Automatic data object '%s' may not appear in the specification part" 356 " of a module"_err_en_US, 357 symbol.name()); 358 } 359 } 360 361 void CheckHelper::CheckValue( 362 const Symbol &symbol, const DerivedTypeSpec *derived) { // C863 - C865 363 if (!IsDummy(symbol)) { 364 messages_.Say( 365 "VALUE attribute may apply only to a dummy argument"_err_en_US); 366 } 367 if (IsProcedure(symbol)) { 368 messages_.Say( 369 "VALUE attribute may apply only to a dummy data object"_err_en_US); 370 } 371 if (IsAssumedSizeArray(symbol)) { 372 messages_.Say( 373 "VALUE attribute may not apply to an assumed-size array"_err_en_US); 374 } 375 if (IsCoarray(symbol)) { 376 messages_.Say("VALUE attribute may not apply to a coarray"_err_en_US); 377 } 378 if (IsAllocatable(symbol)) { 379 messages_.Say("VALUE attribute may not apply to an ALLOCATABLE"_err_en_US); 380 } else if (IsPointer(symbol)) { 381 messages_.Say("VALUE attribute may not apply to a POINTER"_err_en_US); 382 } 383 if (IsIntentInOut(symbol)) { 384 messages_.Say( 385 "VALUE attribute may not apply to an INTENT(IN OUT) argument"_err_en_US); 386 } else if (IsIntentOut(symbol)) { 387 messages_.Say( 388 "VALUE attribute may not apply to an INTENT(OUT) argument"_err_en_US); 389 } 390 if (symbol.attrs().test(Attr::VOLATILE)) { 391 messages_.Say("VALUE attribute may not apply to a VOLATILE"_err_en_US); 392 } 393 if (innermostSymbol_ && IsBindCProcedure(*innermostSymbol_) && 394 IsOptional(symbol)) { 395 messages_.Say( 396 "VALUE attribute may not apply to an OPTIONAL in a BIND(C) procedure"_err_en_US); 397 } 398 if (derived) { 399 if (FindCoarrayUltimateComponent(*derived)) { 400 messages_.Say( 401 "VALUE attribute may not apply to a type with a coarray ultimate component"_err_en_US); 402 } 403 } 404 } 405 406 void CheckHelper::CheckAssumedTypeEntity( // C709 407 const Symbol &symbol, const ObjectEntityDetails &details) { 408 if (const DeclTypeSpec * type{symbol.GetType()}; 409 type && type->category() == DeclTypeSpec::TypeStar) { 410 if (!IsDummy(symbol)) { 411 messages_.Say( 412 "Assumed-type entity '%s' must be a dummy argument"_err_en_US, 413 symbol.name()); 414 } else { 415 if (symbol.attrs().test(Attr::ALLOCATABLE)) { 416 messages_.Say("Assumed-type argument '%s' cannot have the ALLOCATABLE" 417 " attribute"_err_en_US, 418 symbol.name()); 419 } 420 if (symbol.attrs().test(Attr::POINTER)) { 421 messages_.Say("Assumed-type argument '%s' cannot have the POINTER" 422 " attribute"_err_en_US, 423 symbol.name()); 424 } 425 if (symbol.attrs().test(Attr::VALUE)) { 426 messages_.Say("Assumed-type argument '%s' cannot have the VALUE" 427 " attribute"_err_en_US, 428 symbol.name()); 429 } 430 if (symbol.attrs().test(Attr::INTENT_OUT)) { 431 messages_.Say( 432 "Assumed-type argument '%s' cannot be INTENT(OUT)"_err_en_US, 433 symbol.name()); 434 } 435 if (IsCoarray(symbol)) { 436 messages_.Say( 437 "Assumed-type argument '%s' cannot be a coarray"_err_en_US, 438 symbol.name()); 439 } 440 if (details.IsArray() && details.shape().IsExplicitShape()) { 441 messages_.Say( 442 "Assumed-type array argument 'arg8' must be assumed shape," 443 " assumed size, or assumed rank"_err_en_US, 444 symbol.name()); 445 } 446 } 447 } 448 } 449 450 void CheckHelper::CheckObjectEntity( 451 const Symbol &symbol, const ObjectEntityDetails &details) { 452 CheckArraySpec(symbol, details.shape()); 453 Check(details.shape()); 454 Check(details.coshape()); 455 CheckAssumedTypeEntity(symbol, details); 456 WarnMissingFinal(symbol); 457 if (!details.coshape().empty()) { 458 bool isDeferredShape{details.coshape().IsDeferredShape()}; 459 if (IsAllocatable(symbol)) { 460 if (!isDeferredShape) { // C827 461 messages_.Say("'%s' is an ALLOCATABLE coarray and must have a deferred" 462 " coshape"_err_en_US, 463 symbol.name()); 464 } 465 } else if (symbol.owner().IsDerivedType()) { // C746 466 std::string deferredMsg{ 467 isDeferredShape ? "" : " and have a deferred coshape"}; 468 messages_.Say("Component '%s' is a coarray and must have the ALLOCATABLE" 469 " attribute%s"_err_en_US, 470 symbol.name(), deferredMsg); 471 } else { 472 if (!details.coshape().IsAssumedSize()) { // C828 473 messages_.Say( 474 "Component '%s' is a non-ALLOCATABLE coarray and must have" 475 " an explicit coshape"_err_en_US, 476 symbol.name()); 477 } 478 } 479 } 480 if (details.isDummy()) { 481 if (symbol.attrs().test(Attr::INTENT_OUT)) { 482 if (FindUltimateComponent(symbol, [](const Symbol &x) { 483 return IsCoarray(x) && IsAllocatable(x); 484 })) { // C846 485 messages_.Say( 486 "An INTENT(OUT) dummy argument may not be, or contain, an ALLOCATABLE coarray"_err_en_US); 487 } 488 if (IsOrContainsEventOrLockComponent(symbol)) { // C847 489 messages_.Say( 490 "An INTENT(OUT) dummy argument may not be, or contain, EVENT_TYPE or LOCK_TYPE"_err_en_US); 491 } 492 } 493 if (InPure() && !IsStmtFunction(DEREF(innermostSymbol_)) && 494 !IsPointer(symbol) && !IsIntentIn(symbol) && 495 !symbol.attrs().test(Attr::VALUE)) { 496 if (InFunction()) { // C1583 497 messages_.Say( 498 "non-POINTER dummy argument of pure function must be INTENT(IN) or VALUE"_err_en_US); 499 } else if (IsIntentOut(symbol)) { 500 if (const DeclTypeSpec * type{details.type()}) { 501 if (type && type->IsPolymorphic()) { // C1588 502 messages_.Say( 503 "An INTENT(OUT) dummy argument of a pure subroutine may not be polymorphic"_err_en_US); 504 } else if (const DerivedTypeSpec * derived{type->AsDerived()}) { 505 if (FindUltimateComponent(*derived, [](const Symbol &x) { 506 const DeclTypeSpec *type{x.GetType()}; 507 return type && type->IsPolymorphic(); 508 })) { // C1588 509 messages_.Say( 510 "An INTENT(OUT) dummy argument of a pure subroutine may not have a polymorphic ultimate component"_err_en_US); 511 } 512 if (HasImpureFinal(*derived)) { // C1587 513 messages_.Say( 514 "An INTENT(OUT) dummy argument of a pure subroutine may not have an impure FINAL subroutine"_err_en_US); 515 } 516 } 517 } 518 } else if (!IsIntentInOut(symbol)) { // C1586 519 messages_.Say( 520 "non-POINTER dummy argument of pure subroutine must have INTENT() or VALUE attribute"_err_en_US); 521 } 522 } 523 } else if (symbol.attrs().test(Attr::INTENT_IN) || 524 symbol.attrs().test(Attr::INTENT_OUT) || 525 symbol.attrs().test(Attr::INTENT_INOUT)) { 526 messages_.Say("INTENT attributes may apply only to a dummy " 527 "argument"_err_en_US); // C843 528 } else if (IsOptional(symbol)) { 529 messages_.Say("OPTIONAL attribute may apply only to a dummy " 530 "argument"_err_en_US); // C849 531 } 532 if (InElemental()) { 533 if (details.isDummy()) { // C15100 534 if (details.shape().Rank() > 0) { 535 messages_.Say( 536 "A dummy argument of an ELEMENTAL procedure must be scalar"_err_en_US); 537 } 538 if (IsAllocatable(symbol)) { 539 messages_.Say( 540 "A dummy argument of an ELEMENTAL procedure may not be ALLOCATABLE"_err_en_US); 541 } 542 if (IsCoarray(symbol)) { 543 messages_.Say( 544 "A dummy argument of an ELEMENTAL procedure may not be a coarray"_err_en_US); 545 } 546 if (IsPointer(symbol)) { 547 messages_.Say( 548 "A dummy argument of an ELEMENTAL procedure may not be a POINTER"_err_en_US); 549 } 550 if (!symbol.attrs().HasAny(Attrs{Attr::VALUE, Attr::INTENT_IN, 551 Attr::INTENT_INOUT, Attr::INTENT_OUT})) { // C15102 552 messages_.Say( 553 "A dummy argument of an ELEMENTAL procedure must have an INTENT() or VALUE attribute"_err_en_US); 554 } 555 } else if (IsFunctionResult(symbol)) { // C15101 556 if (details.shape().Rank() > 0) { 557 messages_.Say( 558 "The result of an ELEMENTAL function must be scalar"_err_en_US); 559 } 560 if (IsAllocatable(symbol)) { 561 messages_.Say( 562 "The result of an ELEMENTAL function may not be ALLOCATABLE"_err_en_US); 563 } 564 if (IsPointer(symbol)) { 565 messages_.Say( 566 "The result of an ELEMENTAL function may not be a POINTER"_err_en_US); 567 } 568 } 569 } 570 if (HasDeclarationInitializer(symbol)) { // C808; ignore DATA initialization 571 CheckPointerInitialization(symbol); 572 if (IsAutomatic(symbol)) { 573 messages_.Say( 574 "An automatic variable or component must not be initialized"_err_en_US); 575 } else if (IsDummy(symbol)) { 576 messages_.Say("A dummy argument must not be initialized"_err_en_US); 577 } else if (IsFunctionResult(symbol)) { 578 messages_.Say("A function result must not be initialized"_err_en_US); 579 } else if (IsInBlankCommon(symbol)) { 580 messages_.Say( 581 "A variable in blank COMMON should not be initialized"_en_US); 582 } 583 } 584 if (symbol.owner().kind() == Scope::Kind::BlockData) { 585 if (IsAllocatable(symbol)) { 586 messages_.Say( 587 "An ALLOCATABLE variable may not appear in a BLOCK DATA subprogram"_err_en_US); 588 } else if (IsInitialized(symbol) && !FindCommonBlockContaining(symbol)) { 589 messages_.Say( 590 "An initialized variable in BLOCK DATA must be in a COMMON block"_err_en_US); 591 } 592 } 593 if (const DeclTypeSpec * type{details.type()}) { // C708 594 if (type->IsPolymorphic() && 595 !(type->IsAssumedType() || IsAllocatableOrPointer(symbol) || 596 IsDummy(symbol))) { 597 messages_.Say("CLASS entity '%s' must be a dummy argument or have " 598 "ALLOCATABLE or POINTER attribute"_err_en_US, 599 symbol.name()); 600 } 601 } 602 } 603 604 void CheckHelper::CheckPointerInitialization(const Symbol &symbol) { 605 if (IsPointer(symbol) && !context_.HasError(symbol) && 606 !scopeIsUninstantiatedPDT_) { 607 if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) { 608 if (object->init()) { // C764, C765; C808 609 if (auto designator{evaluate::AsGenericExpr(symbol)}) { 610 auto restorer{messages_.SetLocation(symbol.name())}; 611 context_.set_location(symbol.name()); 612 CheckInitialTarget(foldingContext_, *designator, *object->init()); 613 } 614 } 615 } else if (const auto *proc{symbol.detailsIf<ProcEntityDetails>()}) { 616 if (proc->init() && *proc->init()) { 617 // C1519 - must be nonelemental external or module procedure, 618 // or an unrestricted specific intrinsic function. 619 const Symbol &ultimate{(*proc->init())->GetUltimate()}; 620 if (ultimate.attrs().test(Attr::INTRINSIC)) { 621 if (!context_.intrinsics().IsSpecificIntrinsicFunction( 622 ultimate.name().ToString())) { // C1030 623 context_.Say( 624 "Intrinsic procedure '%s' is not a specific intrinsic permitted for use as the initializer for procedure pointer '%s'"_err_en_US, 625 ultimate.name(), symbol.name()); 626 } 627 } else if (!ultimate.attrs().test(Attr::EXTERNAL) && 628 ultimate.owner().kind() != Scope::Kind::Module) { 629 context_.Say("Procedure pointer '%s' initializer '%s' is neither " 630 "an external nor a module procedure"_err_en_US, 631 symbol.name(), ultimate.name()); 632 } else if (ultimate.attrs().test(Attr::ELEMENTAL)) { 633 context_.Say("Procedure pointer '%s' cannot be initialized with the " 634 "elemental procedure '%s"_err_en_US, 635 symbol.name(), ultimate.name()); 636 } else { 637 // TODO: Check the "shalls" in the 15.4.3.6 paragraphs 7-10. 638 } 639 } 640 } 641 } 642 } 643 644 // The six different kinds of array-specs: 645 // array-spec -> explicit-shape-list | deferred-shape-list 646 // | assumed-shape-list | implied-shape-list 647 // | assumed-size | assumed-rank 648 // explicit-shape -> [ lb : ] ub 649 // deferred-shape -> : 650 // assumed-shape -> [ lb ] : 651 // implied-shape -> [ lb : ] * 652 // assumed-size -> [ explicit-shape-list , ] [ lb : ] * 653 // assumed-rank -> .. 654 // Note: 655 // - deferred-shape is also an assumed-shape 656 // - A single "*" or "lb:*" might be assumed-size or implied-shape-list 657 void CheckHelper::CheckArraySpec( 658 const Symbol &symbol, const ArraySpec &arraySpec) { 659 if (arraySpec.Rank() == 0) { 660 return; 661 } 662 bool isExplicit{arraySpec.IsExplicitShape()}; 663 bool isDeferred{arraySpec.IsDeferredShape()}; 664 bool isImplied{arraySpec.IsImpliedShape()}; 665 bool isAssumedShape{arraySpec.IsAssumedShape()}; 666 bool isAssumedSize{arraySpec.IsAssumedSize()}; 667 bool isAssumedRank{arraySpec.IsAssumedRank()}; 668 std::optional<parser::MessageFixedText> msg; 669 if (symbol.test(Symbol::Flag::CrayPointee) && !isExplicit && !isAssumedSize) { 670 msg = "Cray pointee '%s' must have must have explicit shape or" 671 " assumed size"_err_en_US; 672 } else if (IsAllocatableOrPointer(symbol) && !isDeferred && !isAssumedRank) { 673 if (symbol.owner().IsDerivedType()) { // C745 674 if (IsAllocatable(symbol)) { 675 msg = "Allocatable array component '%s' must have" 676 " deferred shape"_err_en_US; 677 } else { 678 msg = "Array pointer component '%s' must have deferred shape"_err_en_US; 679 } 680 } else { 681 if (IsAllocatable(symbol)) { // C832 682 msg = "Allocatable array '%s' must have deferred shape or" 683 " assumed rank"_err_en_US; 684 } else { 685 msg = "Array pointer '%s' must have deferred shape or" 686 " assumed rank"_err_en_US; 687 } 688 } 689 } else if (IsDummy(symbol)) { 690 if (isImplied && !isAssumedSize) { // C836 691 msg = "Dummy array argument '%s' may not have implied shape"_err_en_US; 692 } 693 } else if (isAssumedShape && !isDeferred) { 694 msg = "Assumed-shape array '%s' must be a dummy argument"_err_en_US; 695 } else if (isAssumedSize && !isImplied) { // C833 696 msg = "Assumed-size array '%s' must be a dummy argument"_err_en_US; 697 } else if (isAssumedRank) { // C837 698 msg = "Assumed-rank array '%s' must be a dummy argument"_err_en_US; 699 } else if (isImplied) { 700 if (!IsNamedConstant(symbol)) { // C835, C836 701 msg = "Implied-shape array '%s' must be a named constant or a " 702 "dummy argument"_err_en_US; 703 } 704 } else if (IsNamedConstant(symbol)) { 705 if (!isExplicit && !isImplied) { 706 msg = "Named constant '%s' array must have constant or" 707 " implied shape"_err_en_US; 708 } 709 } else if (!IsAllocatableOrPointer(symbol) && !isExplicit) { 710 if (symbol.owner().IsDerivedType()) { // C749 711 msg = "Component array '%s' without ALLOCATABLE or POINTER attribute must" 712 " have explicit shape"_err_en_US; 713 } else { // C816 714 msg = "Array '%s' without ALLOCATABLE or POINTER attribute must have" 715 " explicit shape"_err_en_US; 716 } 717 } 718 if (msg) { 719 context_.Say(std::move(*msg), symbol.name()); 720 } 721 } 722 723 void CheckHelper::CheckProcEntity( 724 const Symbol &symbol, const ProcEntityDetails &details) { 725 if (details.isDummy()) { 726 if (!symbol.attrs().test(Attr::POINTER) && // C843 727 (symbol.attrs().test(Attr::INTENT_IN) || 728 symbol.attrs().test(Attr::INTENT_OUT) || 729 symbol.attrs().test(Attr::INTENT_INOUT))) { 730 messages_.Say("A dummy procedure without the POINTER attribute" 731 " may not have an INTENT attribute"_err_en_US); 732 } 733 if (InElemental()) { // C15100 734 messages_.Say( 735 "An ELEMENTAL subprogram may not have a dummy procedure"_err_en_US); 736 } 737 const Symbol *interface { details.interface().symbol() }; 738 if (!symbol.attrs().test(Attr::INTRINSIC) && 739 (symbol.attrs().test(Attr::ELEMENTAL) || 740 (interface && !interface->attrs().test(Attr::INTRINSIC) && 741 interface->attrs().test(Attr::ELEMENTAL)))) { 742 // There's no explicit constraint or "shall" that we can find in the 743 // standard for this check, but it seems to be implied in multiple 744 // sites, and ELEMENTAL non-intrinsic actual arguments *are* 745 // explicitly forbidden. But we allow "PROCEDURE(SIN)::dummy" 746 // because it is explicitly legal to *pass* the specific intrinsic 747 // function SIN as an actual argument. 748 messages_.Say("A dummy procedure may not be ELEMENTAL"_err_en_US); 749 } 750 } else if (symbol.attrs().test(Attr::INTENT_IN) || 751 symbol.attrs().test(Attr::INTENT_OUT) || 752 symbol.attrs().test(Attr::INTENT_INOUT)) { 753 messages_.Say("INTENT attributes may apply only to a dummy " 754 "argument"_err_en_US); // C843 755 } else if (IsOptional(symbol)) { 756 messages_.Say("OPTIONAL attribute may apply only to a dummy " 757 "argument"_err_en_US); // C849 758 } else if (symbol.owner().IsDerivedType()) { 759 if (!symbol.attrs().test(Attr::POINTER)) { // C756 760 const auto &name{symbol.name()}; 761 messages_.Say(name, 762 "Procedure component '%s' must have POINTER attribute"_err_en_US, 763 name); 764 } 765 CheckPassArg(symbol, details.interface().symbol(), details); 766 } 767 if (symbol.attrs().test(Attr::POINTER)) { 768 CheckPointerInitialization(symbol); 769 if (const Symbol * interface{details.interface().symbol()}) { 770 if (interface->attrs().test(Attr::INTRINSIC)) { 771 if (!context_.intrinsics().IsSpecificIntrinsicFunction( 772 interface->name().ToString())) { // C1515 773 messages_.Say( 774 "Intrinsic procedure '%s' is not a specific intrinsic permitted for use as the definition of the interface to procedure pointer '%s'"_err_en_US, 775 interface->name(), symbol.name()); 776 } 777 } else if (interface->attrs().test(Attr::ELEMENTAL)) { 778 messages_.Say("Procedure pointer '%s' may not be ELEMENTAL"_err_en_US, 779 symbol.name()); // C1517 780 } 781 } 782 } else if (symbol.attrs().test(Attr::SAVE)) { 783 messages_.Say( 784 "Procedure '%s' with SAVE attribute must also have POINTER attribute"_err_en_US, 785 symbol.name()); 786 } 787 } 788 789 // When a module subprogram has the MODULE prefix the following must match 790 // with the corresponding separate module procedure interface body: 791 // - C1549: characteristics and dummy argument names 792 // - C1550: binding label 793 // - C1551: NON_RECURSIVE prefix 794 class SubprogramMatchHelper { 795 public: 796 explicit SubprogramMatchHelper(CheckHelper &checkHelper) 797 : checkHelper{checkHelper} {} 798 799 void Check(const Symbol &, const Symbol &); 800 801 private: 802 SemanticsContext &context() { return checkHelper.context(); } 803 void CheckDummyArg(const Symbol &, const Symbol &, const DummyArgument &, 804 const DummyArgument &); 805 void CheckDummyDataObject(const Symbol &, const Symbol &, 806 const DummyDataObject &, const DummyDataObject &); 807 void CheckDummyProcedure(const Symbol &, const Symbol &, 808 const DummyProcedure &, const DummyProcedure &); 809 bool CheckSameIntent( 810 const Symbol &, const Symbol &, common::Intent, common::Intent); 811 template <typename... A> 812 void Say( 813 const Symbol &, const Symbol &, parser::MessageFixedText &&, A &&...); 814 template <typename ATTRS> 815 bool CheckSameAttrs(const Symbol &, const Symbol &, ATTRS, ATTRS); 816 bool ShapesAreCompatible(const DummyDataObject &, const DummyDataObject &); 817 evaluate::Shape FoldShape(const evaluate::Shape &); 818 std::string AsFortran(DummyDataObject::Attr attr) { 819 return parser::ToUpperCaseLetters(DummyDataObject::EnumToString(attr)); 820 } 821 std::string AsFortran(DummyProcedure::Attr attr) { 822 return parser::ToUpperCaseLetters(DummyProcedure::EnumToString(attr)); 823 } 824 825 CheckHelper &checkHelper; 826 }; 827 828 // 15.6.2.6 para 3 - can the result of an ENTRY differ from its function? 829 bool CheckHelper::IsResultOkToDiffer(const FunctionResult &result) { 830 if (result.attrs.test(FunctionResult::Attr::Allocatable) || 831 result.attrs.test(FunctionResult::Attr::Pointer)) { 832 return false; 833 } 834 const auto *typeAndShape{result.GetTypeAndShape()}; 835 if (!typeAndShape || typeAndShape->Rank() != 0) { 836 return false; 837 } 838 auto category{typeAndShape->type().category()}; 839 if (category == TypeCategory::Character || 840 category == TypeCategory::Derived) { 841 return false; 842 } 843 int kind{typeAndShape->type().kind()}; 844 return kind == context_.GetDefaultKind(category) || 845 (category == TypeCategory::Real && 846 kind == context_.doublePrecisionKind()); 847 } 848 849 void CheckHelper::CheckSubprogram( 850 const Symbol &symbol, const SubprogramDetails &details) { 851 if (const Symbol * iface{FindSeparateModuleSubprogramInterface(&symbol)}) { 852 SubprogramMatchHelper{*this}.Check(symbol, *iface); 853 } 854 if (const Scope * entryScope{details.entryScope()}) { 855 // ENTRY 15.6.2.6, esp. C1571 856 std::optional<parser::MessageFixedText> error; 857 const Symbol *subprogram{entryScope->symbol()}; 858 const SubprogramDetails *subprogramDetails{nullptr}; 859 if (subprogram) { 860 subprogramDetails = subprogram->detailsIf<SubprogramDetails>(); 861 } 862 if (entryScope->kind() != Scope::Kind::Subprogram) { 863 error = "ENTRY may appear only in a subroutine or function"_err_en_US; 864 } else if (!(entryScope->parent().IsGlobal() || 865 entryScope->parent().IsModule() || 866 entryScope->parent().IsSubmodule())) { 867 error = "ENTRY may not appear in an internal subprogram"_err_en_US; 868 } else if (FindSeparateModuleSubprogramInterface(subprogram)) { 869 error = "ENTRY may not appear in a separate module procedure"_err_en_US; 870 } else if (subprogramDetails && details.isFunction() && 871 subprogramDetails->isFunction() && 872 !context_.HasError(details.result()) && 873 !context_.HasError(subprogramDetails->result())) { 874 auto result{FunctionResult::Characterize( 875 details.result(), context_.foldingContext())}; 876 auto subpResult{FunctionResult::Characterize( 877 subprogramDetails->result(), context_.foldingContext())}; 878 if (result && subpResult && *result != *subpResult && 879 (!IsResultOkToDiffer(*result) || !IsResultOkToDiffer(*subpResult))) { 880 error = 881 "Result of ENTRY is not compatible with result of containing function"_err_en_US; 882 } 883 } 884 if (error) { 885 if (auto *msg{messages_.Say(symbol.name(), *error)}) { 886 if (subprogram) { 887 msg->Attach(subprogram->name(), "Containing subprogram"_en_US); 888 } 889 } 890 } 891 } 892 if (symbol.attrs().test(Attr::ELEMENTAL)) { 893 // See comment on the similar check in CheckProcEntity() 894 if (details.isDummy()) { 895 messages_.Say("A dummy procedure may not be ELEMENTAL"_err_en_US); 896 } else if (details.dummyArgs().empty()) { 897 messages_.Say( 898 "An ELEMENTAL subprogram must have at least one dummy argument"_err_en_US); 899 } else { 900 for (const Symbol *dummy : details.dummyArgs()) { 901 if (!dummy) { // C15100 902 messages_.Say( 903 "An ELEMENTAL subroutine may not have an alternate return dummy argument"_err_en_US); 904 } 905 } 906 } 907 } 908 } 909 910 void CheckHelper::CheckDerivedType( 911 const Symbol &derivedType, const DerivedTypeDetails &details) { 912 if (details.isForwardReferenced() && !context_.HasError(derivedType)) { 913 messages_.Say("The derived type '%s' has not been defined"_err_en_US, 914 derivedType.name()); 915 } 916 const Scope *scope{derivedType.scope()}; 917 if (!scope) { 918 CHECK(details.isForwardReferenced()); 919 return; 920 } 921 CHECK(scope->symbol() == &derivedType); 922 CHECK(scope->IsDerivedType()); 923 if (derivedType.attrs().test(Attr::ABSTRACT) && // C734 924 (derivedType.attrs().test(Attr::BIND_C) || details.sequence())) { 925 messages_.Say("An ABSTRACT derived type must be extensible"_err_en_US); 926 } 927 if (const DeclTypeSpec * parent{FindParentTypeSpec(derivedType)}) { 928 const DerivedTypeSpec *parentDerived{parent->AsDerived()}; 929 if (!IsExtensibleType(parentDerived)) { // C705 930 messages_.Say("The parent type is not extensible"_err_en_US); 931 } 932 if (!derivedType.attrs().test(Attr::ABSTRACT) && parentDerived && 933 parentDerived->typeSymbol().attrs().test(Attr::ABSTRACT)) { 934 ScopeComponentIterator components{*parentDerived}; 935 for (const Symbol &component : components) { 936 if (component.attrs().test(Attr::DEFERRED)) { 937 if (scope->FindComponent(component.name()) == &component) { 938 SayWithDeclaration(component, 939 "Non-ABSTRACT extension of ABSTRACT derived type '%s' lacks a binding for DEFERRED procedure '%s'"_err_en_US, 940 parentDerived->typeSymbol().name(), component.name()); 941 } 942 } 943 } 944 } 945 DerivedTypeSpec derived{derivedType.name(), derivedType}; 946 derived.set_scope(*scope); 947 if (FindCoarrayUltimateComponent(derived) && // C736 948 !(parentDerived && FindCoarrayUltimateComponent(*parentDerived))) { 949 messages_.Say( 950 "Type '%s' has a coarray ultimate component so the type at the base " 951 "of its type extension chain ('%s') must be a type that has a " 952 "coarray ultimate component"_err_en_US, 953 derivedType.name(), scope->GetDerivedTypeBase().GetSymbol()->name()); 954 } 955 if (FindEventOrLockPotentialComponent(derived) && // C737 956 !(FindEventOrLockPotentialComponent(*parentDerived) || 957 IsEventTypeOrLockType(parentDerived))) { 958 messages_.Say( 959 "Type '%s' has an EVENT_TYPE or LOCK_TYPE component, so the type " 960 "at the base of its type extension chain ('%s') must either have an " 961 "EVENT_TYPE or LOCK_TYPE component, or be EVENT_TYPE or " 962 "LOCK_TYPE"_err_en_US, 963 derivedType.name(), scope->GetDerivedTypeBase().GetSymbol()->name()); 964 } 965 } 966 if (HasIntrinsicTypeName(derivedType)) { // C729 967 messages_.Say("A derived type name cannot be the name of an intrinsic" 968 " type"_err_en_US); 969 } 970 std::map<SourceName, SymbolRef> previous; 971 for (const auto &pair : details.finals()) { 972 SourceName source{pair.first}; 973 const Symbol &ref{*pair.second}; 974 if (CheckFinal(ref, source, derivedType) && 975 std::all_of(previous.begin(), previous.end(), 976 [&](std::pair<SourceName, SymbolRef> prev) { 977 return CheckDistinguishableFinals( 978 ref, source, *prev.second, prev.first, derivedType); 979 })) { 980 previous.emplace(source, ref); 981 } 982 } 983 } 984 985 // C786 986 bool CheckHelper::CheckFinal( 987 const Symbol &subroutine, SourceName finalName, const Symbol &derivedType) { 988 if (!IsModuleProcedure(subroutine)) { 989 SayWithDeclaration(subroutine, finalName, 990 "FINAL subroutine '%s' of derived type '%s' must be a module procedure"_err_en_US, 991 subroutine.name(), derivedType.name()); 992 return false; 993 } 994 const Procedure *proc{Characterize(subroutine)}; 995 if (!proc) { 996 return false; // error recovery 997 } 998 if (!proc->IsSubroutine()) { 999 SayWithDeclaration(subroutine, finalName, 1000 "FINAL subroutine '%s' of derived type '%s' must be a subroutine"_err_en_US, 1001 subroutine.name(), derivedType.name()); 1002 return false; 1003 } 1004 if (proc->dummyArguments.size() != 1) { 1005 SayWithDeclaration(subroutine, finalName, 1006 "FINAL subroutine '%s' of derived type '%s' must have a single dummy argument"_err_en_US, 1007 subroutine.name(), derivedType.name()); 1008 return false; 1009 } 1010 const auto &arg{proc->dummyArguments[0]}; 1011 const Symbol *errSym{&subroutine}; 1012 if (const auto *details{subroutine.detailsIf<SubprogramDetails>()}) { 1013 if (!details->dummyArgs().empty()) { 1014 if (const Symbol * argSym{details->dummyArgs()[0]}) { 1015 errSym = argSym; 1016 } 1017 } 1018 } 1019 const auto *ddo{std::get_if<DummyDataObject>(&arg.u)}; 1020 if (!ddo) { 1021 SayWithDeclaration(subroutine, finalName, 1022 "FINAL subroutine '%s' of derived type '%s' must have a single dummy argument that is a data object"_err_en_US, 1023 subroutine.name(), derivedType.name()); 1024 return false; 1025 } 1026 bool ok{true}; 1027 if (arg.IsOptional()) { 1028 SayWithDeclaration(*errSym, finalName, 1029 "FINAL subroutine '%s' of derived type '%s' must not have an OPTIONAL dummy argument"_err_en_US, 1030 subroutine.name(), derivedType.name()); 1031 ok = false; 1032 } 1033 if (ddo->attrs.test(DummyDataObject::Attr::Allocatable)) { 1034 SayWithDeclaration(*errSym, finalName, 1035 "FINAL subroutine '%s' of derived type '%s' must not have an ALLOCATABLE dummy argument"_err_en_US, 1036 subroutine.name(), derivedType.name()); 1037 ok = false; 1038 } 1039 if (ddo->attrs.test(DummyDataObject::Attr::Pointer)) { 1040 SayWithDeclaration(*errSym, finalName, 1041 "FINAL subroutine '%s' of derived type '%s' must not have a POINTER dummy argument"_err_en_US, 1042 subroutine.name(), derivedType.name()); 1043 ok = false; 1044 } 1045 if (ddo->intent == common::Intent::Out) { 1046 SayWithDeclaration(*errSym, finalName, 1047 "FINAL subroutine '%s' of derived type '%s' must not have a dummy argument with INTENT(OUT)"_err_en_US, 1048 subroutine.name(), derivedType.name()); 1049 ok = false; 1050 } 1051 if (ddo->attrs.test(DummyDataObject::Attr::Value)) { 1052 SayWithDeclaration(*errSym, finalName, 1053 "FINAL subroutine '%s' of derived type '%s' must not have a dummy argument with the VALUE attribute"_err_en_US, 1054 subroutine.name(), derivedType.name()); 1055 ok = false; 1056 } 1057 if (ddo->type.corank() > 0) { 1058 SayWithDeclaration(*errSym, finalName, 1059 "FINAL subroutine '%s' of derived type '%s' must not have a coarray dummy argument"_err_en_US, 1060 subroutine.name(), derivedType.name()); 1061 ok = false; 1062 } 1063 if (ddo->type.type().IsPolymorphic()) { 1064 SayWithDeclaration(*errSym, finalName, 1065 "FINAL subroutine '%s' of derived type '%s' must not have a polymorphic dummy argument"_err_en_US, 1066 subroutine.name(), derivedType.name()); 1067 ok = false; 1068 } else if (ddo->type.type().category() != TypeCategory::Derived || 1069 &ddo->type.type().GetDerivedTypeSpec().typeSymbol() != &derivedType) { 1070 SayWithDeclaration(*errSym, finalName, 1071 "FINAL subroutine '%s' of derived type '%s' must have a TYPE(%s) dummy argument"_err_en_US, 1072 subroutine.name(), derivedType.name(), derivedType.name()); 1073 ok = false; 1074 } else { // check that all LEN type parameters are assumed 1075 for (auto ref : OrderParameterDeclarations(derivedType)) { 1076 if (IsLenTypeParameter(*ref)) { 1077 const auto *value{ 1078 ddo->type.type().GetDerivedTypeSpec().FindParameter(ref->name())}; 1079 if (!value || !value->isAssumed()) { 1080 SayWithDeclaration(*errSym, finalName, 1081 "FINAL subroutine '%s' of derived type '%s' must have a dummy argument with an assumed LEN type parameter '%s=*'"_err_en_US, 1082 subroutine.name(), derivedType.name(), ref->name()); 1083 ok = false; 1084 } 1085 } 1086 } 1087 } 1088 return ok; 1089 } 1090 1091 bool CheckHelper::CheckDistinguishableFinals(const Symbol &f1, 1092 SourceName f1Name, const Symbol &f2, SourceName f2Name, 1093 const Symbol &derivedType) { 1094 const Procedure *p1{Characterize(f1)}; 1095 const Procedure *p2{Characterize(f2)}; 1096 if (p1 && p2) { 1097 if (characteristics::Distinguishable(*p1, *p2)) { 1098 return true; 1099 } 1100 if (auto *msg{messages_.Say(f1Name, 1101 "FINAL subroutines '%s' and '%s' of derived type '%s' cannot be distinguished by rank or KIND type parameter value"_err_en_US, 1102 f1Name, f2Name, derivedType.name())}) { 1103 msg->Attach(f2Name, "FINAL declaration of '%s'"_en_US, f2.name()) 1104 .Attach(f1.name(), "Definition of '%s'"_en_US, f1Name) 1105 .Attach(f2.name(), "Definition of '%s'"_en_US, f2Name); 1106 } 1107 } 1108 return false; 1109 } 1110 1111 void CheckHelper::CheckHostAssoc( 1112 const Symbol &symbol, const HostAssocDetails &details) { 1113 const Symbol &hostSymbol{details.symbol()}; 1114 if (hostSymbol.test(Symbol::Flag::ImplicitOrError)) { 1115 if (details.implicitOrSpecExprError) { 1116 messages_.Say("Implicitly typed local entity '%s' not allowed in" 1117 " specification expression"_err_en_US, 1118 symbol.name()); 1119 } else if (details.implicitOrExplicitTypeError) { 1120 messages_.Say( 1121 "No explicit type declared for '%s'"_err_en_US, symbol.name()); 1122 } 1123 } 1124 } 1125 1126 void CheckHelper::CheckGeneric( 1127 const Symbol &symbol, const GenericDetails &details) { 1128 CheckSpecificsAreDistinguishable(symbol, details); 1129 std::visit(common::visitors{ 1130 [&](const GenericKind::DefinedIo &io) { 1131 CheckDefinedIoProc(symbol, details, io); 1132 }, 1133 [](const auto &) {}, 1134 }, 1135 details.kind().u); 1136 } 1137 1138 // Check that the specifics of this generic are distinguishable from each other 1139 void CheckHelper::CheckSpecificsAreDistinguishable( 1140 const Symbol &generic, const GenericDetails &details) { 1141 GenericKind kind{details.kind()}; 1142 const SymbolVector &specifics{details.specificProcs()}; 1143 std::size_t count{specifics.size()}; 1144 if (count < 2 || !kind.IsName()) { 1145 return; 1146 } 1147 DistinguishabilityHelper helper{context_}; 1148 for (const Symbol &specific : specifics) { 1149 if (const Procedure * procedure{Characterize(specific)}) { 1150 helper.Add(generic, kind, specific, *procedure); 1151 } 1152 } 1153 helper.Check(generic.owner()); 1154 } 1155 1156 static bool ConflictsWithIntrinsicAssignment(const Procedure &proc) { 1157 auto lhs{std::get<DummyDataObject>(proc.dummyArguments[0].u).type}; 1158 auto rhs{std::get<DummyDataObject>(proc.dummyArguments[1].u).type}; 1159 return Tristate::No == 1160 IsDefinedAssignment(lhs.type(), lhs.Rank(), rhs.type(), rhs.Rank()); 1161 } 1162 1163 static bool ConflictsWithIntrinsicOperator( 1164 const GenericKind &kind, const Procedure &proc) { 1165 if (!kind.IsIntrinsicOperator()) { 1166 return false; 1167 } 1168 auto arg0{std::get<DummyDataObject>(proc.dummyArguments[0].u).type}; 1169 auto type0{arg0.type()}; 1170 if (proc.dummyArguments.size() == 1) { // unary 1171 return std::visit( 1172 common::visitors{ 1173 [&](common::NumericOperator) { return IsIntrinsicNumeric(type0); }, 1174 [&](common::LogicalOperator) { return IsIntrinsicLogical(type0); }, 1175 [](const auto &) -> bool { DIE("bad generic kind"); }, 1176 }, 1177 kind.u); 1178 } else { // binary 1179 int rank0{arg0.Rank()}; 1180 auto arg1{std::get<DummyDataObject>(proc.dummyArguments[1].u).type}; 1181 auto type1{arg1.type()}; 1182 int rank1{arg1.Rank()}; 1183 return std::visit( 1184 common::visitors{ 1185 [&](common::NumericOperator) { 1186 return IsIntrinsicNumeric(type0, rank0, type1, rank1); 1187 }, 1188 [&](common::LogicalOperator) { 1189 return IsIntrinsicLogical(type0, rank0, type1, rank1); 1190 }, 1191 [&](common::RelationalOperator opr) { 1192 return IsIntrinsicRelational(opr, type0, rank0, type1, rank1); 1193 }, 1194 [&](GenericKind::OtherKind x) { 1195 CHECK(x == GenericKind::OtherKind::Concat); 1196 return IsIntrinsicConcat(type0, rank0, type1, rank1); 1197 }, 1198 [](const auto &) -> bool { DIE("bad generic kind"); }, 1199 }, 1200 kind.u); 1201 } 1202 } 1203 1204 // Check if this procedure can be used for defined operators (see 15.4.3.4.2). 1205 bool CheckHelper::CheckDefinedOperator(SourceName opName, GenericKind kind, 1206 const Symbol &specific, const Procedure &proc) { 1207 if (context_.HasError(specific)) { 1208 return false; 1209 } 1210 std::optional<parser::MessageFixedText> msg; 1211 auto checkDefinedOperatorArgs{ 1212 [&](SourceName opName, const Symbol &specific, const Procedure &proc) { 1213 bool arg0Defined{CheckDefinedOperatorArg(opName, specific, proc, 0)}; 1214 bool arg1Defined{CheckDefinedOperatorArg(opName, specific, proc, 1)}; 1215 return arg0Defined && arg1Defined; 1216 }}; 1217 if (specific.attrs().test(Attr::NOPASS)) { // C774 1218 msg = "%s procedure '%s' may not have NOPASS attribute"_err_en_US; 1219 } else if (!proc.functionResult.has_value()) { 1220 msg = "%s procedure '%s' must be a function"_err_en_US; 1221 } else if (proc.functionResult->IsAssumedLengthCharacter()) { 1222 msg = "%s function '%s' may not have assumed-length CHARACTER(*)" 1223 " result"_err_en_US; 1224 } else if (auto m{CheckNumberOfArgs(kind, proc.dummyArguments.size())}) { 1225 msg = std::move(m); 1226 } else if (!checkDefinedOperatorArgs(opName, specific, proc)) { 1227 return false; // error was reported 1228 } else if (ConflictsWithIntrinsicOperator(kind, proc)) { 1229 msg = "%s function '%s' conflicts with intrinsic operator"_err_en_US; 1230 } else { 1231 return true; // OK 1232 } 1233 SayWithDeclaration( 1234 specific, std::move(*msg), MakeOpName(opName), specific.name()); 1235 context_.SetError(specific); 1236 return false; 1237 } 1238 1239 // If the number of arguments is wrong for this intrinsic operator, return 1240 // false and return the error message in msg. 1241 std::optional<parser::MessageFixedText> CheckHelper::CheckNumberOfArgs( 1242 const GenericKind &kind, std::size_t nargs) { 1243 if (!kind.IsIntrinsicOperator()) { 1244 return std::nullopt; 1245 } 1246 std::size_t min{2}, max{2}; // allowed number of args; default is binary 1247 std::visit(common::visitors{ 1248 [&](const common::NumericOperator &x) { 1249 if (x == common::NumericOperator::Add || 1250 x == common::NumericOperator::Subtract) { 1251 min = 1; // + and - are unary or binary 1252 } 1253 }, 1254 [&](const common::LogicalOperator &x) { 1255 if (x == common::LogicalOperator::Not) { 1256 min = 1; // .NOT. is unary 1257 max = 1; 1258 } 1259 }, 1260 [](const common::RelationalOperator &) { 1261 // all are binary 1262 }, 1263 [](const GenericKind::OtherKind &x) { 1264 CHECK(x == GenericKind::OtherKind::Concat); 1265 }, 1266 [](const auto &) { DIE("expected intrinsic operator"); }, 1267 }, 1268 kind.u); 1269 if (nargs >= min && nargs <= max) { 1270 return std::nullopt; 1271 } else if (max == 1) { 1272 return "%s function '%s' must have one dummy argument"_err_en_US; 1273 } else if (min == 2) { 1274 return "%s function '%s' must have two dummy arguments"_err_en_US; 1275 } else { 1276 return "%s function '%s' must have one or two dummy arguments"_err_en_US; 1277 } 1278 } 1279 1280 bool CheckHelper::CheckDefinedOperatorArg(const SourceName &opName, 1281 const Symbol &symbol, const Procedure &proc, std::size_t pos) { 1282 if (pos >= proc.dummyArguments.size()) { 1283 return true; 1284 } 1285 auto &arg{proc.dummyArguments.at(pos)}; 1286 std::optional<parser::MessageFixedText> msg; 1287 if (arg.IsOptional()) { 1288 msg = "In %s function '%s', dummy argument '%s' may not be" 1289 " OPTIONAL"_err_en_US; 1290 } else if (const auto *dataObject{std::get_if<DummyDataObject>(&arg.u)}; 1291 dataObject == nullptr) { 1292 msg = "In %s function '%s', dummy argument '%s' must be a" 1293 " data object"_err_en_US; 1294 } else if (dataObject->intent != common::Intent::In && 1295 !dataObject->attrs.test(DummyDataObject::Attr::Value)) { 1296 msg = "In %s function '%s', dummy argument '%s' must have INTENT(IN)" 1297 " or VALUE attribute"_err_en_US; 1298 } 1299 if (msg) { 1300 SayWithDeclaration(symbol, std::move(*msg), 1301 parser::ToUpperCaseLetters(opName.ToString()), symbol.name(), arg.name); 1302 return false; 1303 } 1304 return true; 1305 } 1306 1307 // Check if this procedure can be used for defined assignment (see 15.4.3.4.3). 1308 bool CheckHelper::CheckDefinedAssignment( 1309 const Symbol &specific, const Procedure &proc) { 1310 if (context_.HasError(specific)) { 1311 return false; 1312 } 1313 std::optional<parser::MessageFixedText> msg; 1314 if (specific.attrs().test(Attr::NOPASS)) { // C774 1315 msg = "Defined assignment procedure '%s' may not have" 1316 " NOPASS attribute"_err_en_US; 1317 } else if (!proc.IsSubroutine()) { 1318 msg = "Defined assignment procedure '%s' must be a subroutine"_err_en_US; 1319 } else if (proc.dummyArguments.size() != 2) { 1320 msg = "Defined assignment subroutine '%s' must have" 1321 " two dummy arguments"_err_en_US; 1322 } else if (!CheckDefinedAssignmentArg(specific, proc.dummyArguments[0], 0) | 1323 !CheckDefinedAssignmentArg(specific, proc.dummyArguments[1], 1)) { 1324 return false; // error was reported 1325 } else if (ConflictsWithIntrinsicAssignment(proc)) { 1326 msg = "Defined assignment subroutine '%s' conflicts with" 1327 " intrinsic assignment"_err_en_US; 1328 } else { 1329 return true; // OK 1330 } 1331 SayWithDeclaration(specific, std::move(msg.value()), specific.name()); 1332 context_.SetError(specific); 1333 return false; 1334 } 1335 1336 bool CheckHelper::CheckDefinedAssignmentArg( 1337 const Symbol &symbol, const DummyArgument &arg, int pos) { 1338 std::optional<parser::MessageFixedText> msg; 1339 if (arg.IsOptional()) { 1340 msg = "In defined assignment subroutine '%s', dummy argument '%s'" 1341 " may not be OPTIONAL"_err_en_US; 1342 } else if (const auto *dataObject{std::get_if<DummyDataObject>(&arg.u)}) { 1343 if (pos == 0) { 1344 if (dataObject->intent != common::Intent::Out && 1345 dataObject->intent != common::Intent::InOut) { 1346 msg = "In defined assignment subroutine '%s', first dummy argument '%s'" 1347 " must have INTENT(OUT) or INTENT(INOUT)"_err_en_US; 1348 } 1349 } else if (pos == 1) { 1350 if (dataObject->intent != common::Intent::In && 1351 !dataObject->attrs.test(DummyDataObject::Attr::Value)) { 1352 msg = 1353 "In defined assignment subroutine '%s', second dummy" 1354 " argument '%s' must have INTENT(IN) or VALUE attribute"_err_en_US; 1355 } 1356 } else { 1357 DIE("pos must be 0 or 1"); 1358 } 1359 } else { 1360 msg = "In defined assignment subroutine '%s', dummy argument '%s'" 1361 " must be a data object"_err_en_US; 1362 } 1363 if (msg) { 1364 SayWithDeclaration(symbol, std::move(*msg), symbol.name(), arg.name); 1365 context_.SetError(symbol); 1366 return false; 1367 } 1368 return true; 1369 } 1370 1371 // Report a conflicting attribute error if symbol has both of these attributes 1372 bool CheckHelper::CheckConflicting(const Symbol &symbol, Attr a1, Attr a2) { 1373 if (symbol.attrs().test(a1) && symbol.attrs().test(a2)) { 1374 messages_.Say("'%s' may not have both the %s and %s attributes"_err_en_US, 1375 symbol.name(), AttrToString(a1), AttrToString(a2)); 1376 return true; 1377 } else { 1378 return false; 1379 } 1380 } 1381 1382 void CheckHelper::WarnMissingFinal(const Symbol &symbol) { 1383 const auto *object{symbol.detailsIf<ObjectEntityDetails>()}; 1384 if (!object || IsPointer(symbol)) { 1385 return; 1386 } 1387 const DeclTypeSpec *type{object->type()}; 1388 const DerivedTypeSpec *derived{type ? type->AsDerived() : nullptr}; 1389 const Symbol *derivedSym{derived ? &derived->typeSymbol() : nullptr}; 1390 int rank{object->shape().Rank()}; 1391 const Symbol *initialDerivedSym{derivedSym}; 1392 while (const auto *derivedDetails{ 1393 derivedSym ? derivedSym->detailsIf<DerivedTypeDetails>() : nullptr}) { 1394 if (!derivedDetails->finals().empty() && 1395 !derivedDetails->GetFinalForRank(rank)) { 1396 if (auto *msg{derivedSym == initialDerivedSym 1397 ? messages_.Say(symbol.name(), 1398 "'%s' of derived type '%s' does not have a FINAL subroutine for its rank (%d)"_en_US, 1399 symbol.name(), derivedSym->name(), rank) 1400 : messages_.Say(symbol.name(), 1401 "'%s' of derived type '%s' extended from '%s' does not have a FINAL subroutine for its rank (%d)"_en_US, 1402 symbol.name(), initialDerivedSym->name(), 1403 derivedSym->name(), rank)}) { 1404 msg->Attach(derivedSym->name(), 1405 "Declaration of derived type '%s'"_en_US, derivedSym->name()); 1406 } 1407 return; 1408 } 1409 derived = derivedSym->GetParentTypeSpec(); 1410 derivedSym = derived ? &derived->typeSymbol() : nullptr; 1411 } 1412 } 1413 1414 const Procedure *CheckHelper::Characterize(const Symbol &symbol) { 1415 auto it{characterizeCache_.find(symbol)}; 1416 if (it == characterizeCache_.end()) { 1417 auto pair{characterizeCache_.emplace(SymbolRef{symbol}, 1418 Procedure::Characterize(symbol, context_.foldingContext()))}; 1419 it = pair.first; 1420 } 1421 return common::GetPtrFromOptional(it->second); 1422 } 1423 1424 void CheckHelper::CheckVolatile(const Symbol &symbol, 1425 const DerivedTypeSpec *derived) { // C866 - C868 1426 if (IsIntentIn(symbol)) { 1427 messages_.Say( 1428 "VOLATILE attribute may not apply to an INTENT(IN) argument"_err_en_US); 1429 } 1430 if (IsProcedure(symbol)) { 1431 messages_.Say("VOLATILE attribute may apply only to a variable"_err_en_US); 1432 } 1433 if (symbol.has<UseDetails>() || symbol.has<HostAssocDetails>()) { 1434 const Symbol &ultimate{symbol.GetUltimate()}; 1435 if (IsCoarray(ultimate)) { 1436 messages_.Say( 1437 "VOLATILE attribute may not apply to a coarray accessed by USE or host association"_err_en_US); 1438 } 1439 if (derived) { 1440 if (FindCoarrayUltimateComponent(*derived)) { 1441 messages_.Say( 1442 "VOLATILE attribute may not apply to a type with a coarray ultimate component accessed by USE or host association"_err_en_US); 1443 } 1444 } 1445 } 1446 } 1447 1448 void CheckHelper::CheckPointer(const Symbol &symbol) { // C852 1449 CheckConflicting(symbol, Attr::POINTER, Attr::TARGET); 1450 CheckConflicting(symbol, Attr::POINTER, Attr::ALLOCATABLE); // C751 1451 CheckConflicting(symbol, Attr::POINTER, Attr::INTRINSIC); 1452 // Prohibit constant pointers. The standard does not explicitly prohibit 1453 // them, but the PARAMETER attribute requires a entity-decl to have an 1454 // initialization that is a constant-expr, and the only form of 1455 // initialization that allows a constant-expr is the one that's not a "=>" 1456 // pointer initialization. See C811, C807, and section 8.5.13. 1457 CheckConflicting(symbol, Attr::POINTER, Attr::PARAMETER); 1458 if (symbol.Corank() > 0) { 1459 messages_.Say( 1460 "'%s' may not have the POINTER attribute because it is a coarray"_err_en_US, 1461 symbol.name()); 1462 } 1463 } 1464 1465 // C760 constraints on the passed-object dummy argument 1466 // C757 constraints on procedure pointer components 1467 void CheckHelper::CheckPassArg( 1468 const Symbol &proc, const Symbol *interface, const WithPassArg &details) { 1469 if (proc.attrs().test(Attr::NOPASS)) { 1470 return; 1471 } 1472 const auto &name{proc.name()}; 1473 if (!interface) { 1474 messages_.Say(name, 1475 "Procedure component '%s' must have NOPASS attribute or explicit interface"_err_en_US, 1476 name); 1477 return; 1478 } 1479 const auto *subprogram{interface->detailsIf<SubprogramDetails>()}; 1480 if (!subprogram) { 1481 messages_.Say(name, 1482 "Procedure component '%s' has invalid interface '%s'"_err_en_US, name, 1483 interface->name()); 1484 return; 1485 } 1486 std::optional<SourceName> passName{details.passName()}; 1487 const auto &dummyArgs{subprogram->dummyArgs()}; 1488 if (!passName) { 1489 if (dummyArgs.empty()) { 1490 messages_.Say(name, 1491 proc.has<ProcEntityDetails>() 1492 ? "Procedure component '%s' with no dummy arguments" 1493 " must have NOPASS attribute"_err_en_US 1494 : "Procedure binding '%s' with no dummy arguments" 1495 " must have NOPASS attribute"_err_en_US, 1496 name); 1497 context_.SetError(*interface); 1498 return; 1499 } 1500 Symbol *argSym{dummyArgs[0]}; 1501 if (!argSym) { 1502 messages_.Say(interface->name(), 1503 "Cannot use an alternate return as the passed-object dummy " 1504 "argument"_err_en_US); 1505 return; 1506 } 1507 passName = dummyArgs[0]->name(); 1508 } 1509 std::optional<int> passArgIndex{}; 1510 for (std::size_t i{0}; i < dummyArgs.size(); ++i) { 1511 if (dummyArgs[i] && dummyArgs[i]->name() == *passName) { 1512 passArgIndex = i; 1513 break; 1514 } 1515 } 1516 if (!passArgIndex) { // C758 1517 messages_.Say(*passName, 1518 "'%s' is not a dummy argument of procedure interface '%s'"_err_en_US, 1519 *passName, interface->name()); 1520 return; 1521 } 1522 const Symbol &passArg{*dummyArgs[*passArgIndex]}; 1523 std::optional<parser::MessageFixedText> msg; 1524 if (!passArg.has<ObjectEntityDetails>()) { 1525 msg = "Passed-object dummy argument '%s' of procedure '%s'" 1526 " must be a data object"_err_en_US; 1527 } else if (passArg.attrs().test(Attr::POINTER)) { 1528 msg = "Passed-object dummy argument '%s' of procedure '%s'" 1529 " may not have the POINTER attribute"_err_en_US; 1530 } else if (passArg.attrs().test(Attr::ALLOCATABLE)) { 1531 msg = "Passed-object dummy argument '%s' of procedure '%s'" 1532 " may not have the ALLOCATABLE attribute"_err_en_US; 1533 } else if (passArg.attrs().test(Attr::VALUE)) { 1534 msg = "Passed-object dummy argument '%s' of procedure '%s'" 1535 " may not have the VALUE attribute"_err_en_US; 1536 } else if (passArg.Rank() > 0) { 1537 msg = "Passed-object dummy argument '%s' of procedure '%s'" 1538 " must be scalar"_err_en_US; 1539 } 1540 if (msg) { 1541 messages_.Say(name, std::move(*msg), passName.value(), name); 1542 return; 1543 } 1544 const DeclTypeSpec *type{passArg.GetType()}; 1545 if (!type) { 1546 return; // an error already occurred 1547 } 1548 const Symbol &typeSymbol{*proc.owner().GetSymbol()}; 1549 const DerivedTypeSpec *derived{type->AsDerived()}; 1550 if (!derived || derived->typeSymbol() != typeSymbol) { 1551 messages_.Say(name, 1552 "Passed-object dummy argument '%s' of procedure '%s'" 1553 " must be of type '%s' but is '%s'"_err_en_US, 1554 passName.value(), name, typeSymbol.name(), type->AsFortran()); 1555 return; 1556 } 1557 if (IsExtensibleType(derived) != type->IsPolymorphic()) { 1558 messages_.Say(name, 1559 type->IsPolymorphic() 1560 ? "Passed-object dummy argument '%s' of procedure '%s'" 1561 " may not be polymorphic because '%s' is not extensible"_err_en_US 1562 : "Passed-object dummy argument '%s' of procedure '%s'" 1563 " must be polymorphic because '%s' is extensible"_err_en_US, 1564 passName.value(), name, typeSymbol.name()); 1565 return; 1566 } 1567 for (const auto &[paramName, paramValue] : derived->parameters()) { 1568 if (paramValue.isLen() && !paramValue.isAssumed()) { 1569 messages_.Say(name, 1570 "Passed-object dummy argument '%s' of procedure '%s'" 1571 " has non-assumed length parameter '%s'"_err_en_US, 1572 passName.value(), name, paramName); 1573 } 1574 } 1575 } 1576 1577 void CheckHelper::CheckProcBinding( 1578 const Symbol &symbol, const ProcBindingDetails &binding) { 1579 const Scope &dtScope{symbol.owner()}; 1580 CHECK(dtScope.kind() == Scope::Kind::DerivedType); 1581 if (symbol.attrs().test(Attr::DEFERRED)) { 1582 if (const Symbol * dtSymbol{dtScope.symbol()}) { 1583 if (!dtSymbol->attrs().test(Attr::ABSTRACT)) { // C733 1584 SayWithDeclaration(*dtSymbol, 1585 "Procedure bound to non-ABSTRACT derived type '%s' may not be DEFERRED"_err_en_US, 1586 dtSymbol->name()); 1587 } 1588 } 1589 if (symbol.attrs().test(Attr::NON_OVERRIDABLE)) { 1590 messages_.Say( 1591 "Type-bound procedure '%s' may not be both DEFERRED and NON_OVERRIDABLE"_err_en_US, 1592 symbol.name()); 1593 } 1594 } 1595 if (binding.symbol().attrs().test(Attr::INTRINSIC) && 1596 !context_.intrinsics().IsSpecificIntrinsicFunction( 1597 binding.symbol().name().ToString())) { 1598 messages_.Say( 1599 "Intrinsic procedure '%s' is not a specific intrinsic permitted for use in the definition of binding '%s'"_err_en_US, 1600 binding.symbol().name(), symbol.name()); 1601 } 1602 if (const Symbol * overridden{FindOverriddenBinding(symbol)}) { 1603 if (overridden->attrs().test(Attr::NON_OVERRIDABLE)) { 1604 SayWithDeclaration(*overridden, 1605 "Override of NON_OVERRIDABLE '%s' is not permitted"_err_en_US, 1606 symbol.name()); 1607 } 1608 if (const auto *overriddenBinding{ 1609 overridden->detailsIf<ProcBindingDetails>()}) { 1610 if (!IsPureProcedure(symbol) && IsPureProcedure(*overridden)) { 1611 SayWithDeclaration(*overridden, 1612 "An overridden pure type-bound procedure binding must also be pure"_err_en_US); 1613 return; 1614 } 1615 if (!binding.symbol().attrs().test(Attr::ELEMENTAL) && 1616 overriddenBinding->symbol().attrs().test(Attr::ELEMENTAL)) { 1617 SayWithDeclaration(*overridden, 1618 "A type-bound procedure and its override must both, or neither, be ELEMENTAL"_err_en_US); 1619 return; 1620 } 1621 bool isNopass{symbol.attrs().test(Attr::NOPASS)}; 1622 if (isNopass != overridden->attrs().test(Attr::NOPASS)) { 1623 SayWithDeclaration(*overridden, 1624 isNopass 1625 ? "A NOPASS type-bound procedure may not override a passed-argument procedure"_err_en_US 1626 : "A passed-argument type-bound procedure may not override a NOPASS procedure"_err_en_US); 1627 } else { 1628 const auto *bindingChars{Characterize(binding.symbol())}; 1629 const auto *overriddenChars{Characterize(overriddenBinding->symbol())}; 1630 if (bindingChars && overriddenChars) { 1631 if (isNopass) { 1632 if (!bindingChars->CanOverride(*overriddenChars, std::nullopt)) { 1633 SayWithDeclaration(*overridden, 1634 "A type-bound procedure and its override must have compatible interfaces"_err_en_US); 1635 } 1636 } else if (!context_.HasError(binding.symbol())) { 1637 int passIndex{bindingChars->FindPassIndex(binding.passName())}; 1638 int overriddenPassIndex{ 1639 overriddenChars->FindPassIndex(overriddenBinding->passName())}; 1640 if (passIndex != overriddenPassIndex) { 1641 SayWithDeclaration(*overridden, 1642 "A type-bound procedure and its override must use the same PASS argument"_err_en_US); 1643 } else if (!bindingChars->CanOverride( 1644 *overriddenChars, passIndex)) { 1645 SayWithDeclaration(*overridden, 1646 "A type-bound procedure and its override must have compatible interfaces apart from their passed argument"_err_en_US); 1647 } 1648 } 1649 } 1650 } 1651 if (symbol.attrs().test(Attr::PRIVATE) && 1652 overridden->attrs().test(Attr::PUBLIC)) { 1653 SayWithDeclaration(*overridden, 1654 "A PRIVATE procedure may not override a PUBLIC procedure"_err_en_US); 1655 } 1656 } else { 1657 SayWithDeclaration(*overridden, 1658 "A type-bound procedure binding may not have the same name as a parent component"_err_en_US); 1659 } 1660 } 1661 CheckPassArg(symbol, &binding.symbol(), binding); 1662 } 1663 1664 void CheckHelper::Check(const Scope &scope) { 1665 scope_ = &scope; 1666 common::Restorer<const Symbol *> restorer{innermostSymbol_, innermostSymbol_}; 1667 if (const Symbol * symbol{scope.symbol()}) { 1668 innermostSymbol_ = symbol; 1669 } 1670 if (scope.IsParameterizedDerivedTypeInstantiation()) { 1671 auto restorer{common::ScopedSet(scopeIsUninstantiatedPDT_, false)}; 1672 auto restorer2{context_.foldingContext().messages().SetContext( 1673 scope.instantiationContext().get())}; 1674 for (const auto &pair : scope) { 1675 CheckPointerInitialization(*pair.second); 1676 } 1677 } else { 1678 auto restorer{common::ScopedSet( 1679 scopeIsUninstantiatedPDT_, scope.IsParameterizedDerivedType())}; 1680 for (const auto &set : scope.equivalenceSets()) { 1681 CheckEquivalenceSet(set); 1682 } 1683 for (const auto &pair : scope) { 1684 Check(*pair.second); 1685 } 1686 for (const Scope &child : scope.children()) { 1687 Check(child); 1688 } 1689 if (scope.kind() == Scope::Kind::BlockData) { 1690 CheckBlockData(scope); 1691 } 1692 CheckGenericOps(scope); 1693 } 1694 } 1695 1696 void CheckHelper::CheckEquivalenceSet(const EquivalenceSet &set) { 1697 auto iter{ 1698 std::find_if(set.begin(), set.end(), [](const EquivalenceObject &object) { 1699 return FindCommonBlockContaining(object.symbol) != nullptr; 1700 })}; 1701 if (iter != set.end()) { 1702 const Symbol &commonBlock{DEREF(FindCommonBlockContaining(iter->symbol))}; 1703 for (auto &object : set) { 1704 if (&object != &*iter) { 1705 if (auto *details{object.symbol.detailsIf<ObjectEntityDetails>()}) { 1706 if (details->commonBlock()) { 1707 if (details->commonBlock() != &commonBlock) { // 8.10.3 paragraph 1 1708 if (auto *msg{messages_.Say(object.symbol.name(), 1709 "Two objects in the same EQUIVALENCE set may not be members of distinct COMMON blocks"_err_en_US)}) { 1710 msg->Attach(iter->symbol.name(), 1711 "Other object in EQUIVALENCE set"_en_US) 1712 .Attach(details->commonBlock()->name(), 1713 "COMMON block containing '%s'"_en_US, 1714 object.symbol.name()) 1715 .Attach(commonBlock.name(), 1716 "COMMON block containing '%s'"_en_US, 1717 iter->symbol.name()); 1718 } 1719 } 1720 } else { 1721 // Mark all symbols in the equivalence set with the same COMMON 1722 // block to prevent spurious error messages about initialization 1723 // in BLOCK DATA outside COMMON 1724 details->set_commonBlock(commonBlock); 1725 } 1726 } 1727 } 1728 } 1729 } 1730 // TODO: Move C8106 (&al.) checks here from resolve-names-utils.cpp 1731 } 1732 1733 void CheckHelper::CheckBlockData(const Scope &scope) { 1734 // BLOCK DATA subprograms should contain only named common blocks. 1735 // C1415 presents a list of statements that shouldn't appear in 1736 // BLOCK DATA, but so long as the subprogram contains no executable 1737 // code and allocates no storage outside named COMMON, we're happy 1738 // (e.g., an ENUM is strictly not allowed). 1739 for (const auto &pair : scope) { 1740 const Symbol &symbol{*pair.second}; 1741 if (!(symbol.has<CommonBlockDetails>() || symbol.has<UseDetails>() || 1742 symbol.has<UseErrorDetails>() || symbol.has<DerivedTypeDetails>() || 1743 symbol.has<SubprogramDetails>() || 1744 symbol.has<ObjectEntityDetails>() || 1745 (symbol.has<ProcEntityDetails>() && 1746 !symbol.attrs().test(Attr::POINTER)))) { 1747 messages_.Say(symbol.name(), 1748 "'%s' may not appear in a BLOCK DATA subprogram"_err_en_US, 1749 symbol.name()); 1750 } 1751 } 1752 } 1753 1754 // Check distinguishability of generic assignment and operators. 1755 // For these, generics and generic bindings must be considered together. 1756 void CheckHelper::CheckGenericOps(const Scope &scope) { 1757 DistinguishabilityHelper helper{context_}; 1758 auto addSpecifics{[&](const Symbol &generic) { 1759 const auto *details{generic.GetUltimate().detailsIf<GenericDetails>()}; 1760 if (!details) { 1761 return; 1762 } 1763 GenericKind kind{details->kind()}; 1764 if (!kind.IsAssignment() && !kind.IsOperator()) { 1765 return; 1766 } 1767 const SymbolVector &specifics{details->specificProcs()}; 1768 const std::vector<SourceName> &bindingNames{details->bindingNames()}; 1769 for (std::size_t i{0}; i < specifics.size(); ++i) { 1770 const Symbol &specific{*specifics[i]}; 1771 if (const Procedure * proc{Characterize(specific)}) { 1772 auto restorer{messages_.SetLocation(bindingNames[i])}; 1773 if (kind.IsAssignment()) { 1774 if (!CheckDefinedAssignment(specific, *proc)) { 1775 continue; 1776 } 1777 } else { 1778 if (!CheckDefinedOperator(generic.name(), kind, specific, *proc)) { 1779 continue; 1780 } 1781 } 1782 helper.Add(generic, kind, specific, *proc); 1783 } 1784 } 1785 }}; 1786 for (const auto &pair : scope) { 1787 const Symbol &symbol{*pair.second}; 1788 addSpecifics(symbol); 1789 const Symbol &ultimate{symbol.GetUltimate()}; 1790 if (ultimate.has<DerivedTypeDetails>()) { 1791 if (const Scope * typeScope{ultimate.scope()}) { 1792 for (const auto &pair2 : *typeScope) { 1793 addSpecifics(*pair2.second); 1794 } 1795 } 1796 } 1797 } 1798 helper.Check(scope); 1799 } 1800 1801 static const std::string *DefinesBindCName(const Symbol &symbol) { 1802 const auto *subp{symbol.detailsIf<SubprogramDetails>()}; 1803 if ((subp && !subp->isInterface()) || symbol.has<ObjectEntityDetails>()) { 1804 // Symbol defines data or entry point 1805 return symbol.GetBindName(); 1806 } else { 1807 return nullptr; 1808 } 1809 } 1810 1811 // Check that BIND(C) names are distinct 1812 void CheckHelper::CheckBindCName(const Symbol &symbol) { 1813 if (const std::string * name{DefinesBindCName(symbol)}) { 1814 auto pair{bindC_.emplace(*name, symbol)}; 1815 if (!pair.second) { 1816 const Symbol &other{*pair.first->second}; 1817 if (DefinesBindCName(other) && !context_.HasError(other)) { 1818 if (auto *msg{messages_.Say( 1819 "Two symbols have the same BIND(C) name '%s'"_err_en_US, 1820 *name)}) { 1821 msg->Attach(other.name(), "Conflicting symbol"_en_US); 1822 } 1823 context_.SetError(symbol); 1824 context_.SetError(other); 1825 } 1826 } 1827 } 1828 } 1829 1830 bool CheckHelper::CheckDioDummyIsData( 1831 const Symbol &subp, const Symbol *arg, std::size_t position) { 1832 if (arg && arg->detailsIf<ObjectEntityDetails>()) { 1833 return true; 1834 } else { 1835 if (arg) { 1836 messages_.Say(arg->name(), 1837 "Dummy argument '%s' must be a data object"_err_en_US, arg->name()); 1838 } else { 1839 messages_.Say(subp.name(), 1840 "Dummy argument %d of '%s' must be a data object"_err_en_US, position, 1841 subp.name()); 1842 } 1843 return false; 1844 } 1845 } 1846 1847 void CheckHelper::CheckAlreadySeenDefinedIo(const DerivedTypeSpec *derivedType, 1848 GenericKind::DefinedIo ioKind, const Symbol &proc) { 1849 for (TypeWithDefinedIo definedIoType : seenDefinedIoTypes_) { 1850 if (*derivedType == *definedIoType.type && ioKind == definedIoType.ioKind && 1851 proc != definedIoType.proc) { 1852 SayWithDeclaration(proc, definedIoType.proc.name(), 1853 "Derived type '%s' already has defined input/output procedure" 1854 " '%s'"_err_en_US, 1855 derivedType->name(), 1856 parser::ToUpperCaseLetters(GenericKind::EnumToString(ioKind))); 1857 return; 1858 } 1859 } 1860 seenDefinedIoTypes_.emplace_back( 1861 TypeWithDefinedIo{derivedType, ioKind, proc}); 1862 } 1863 1864 void CheckHelper::CheckDioDummyIsDerived( 1865 const Symbol &subp, const Symbol &arg, GenericKind::DefinedIo ioKind) { 1866 if (const DeclTypeSpec * type{arg.GetType()}) { 1867 if (const DerivedTypeSpec * derivedType{type->AsDerived()}) { 1868 CheckAlreadySeenDefinedIo(derivedType, ioKind, subp); 1869 bool isPolymorphic{type->IsPolymorphic()}; 1870 if (isPolymorphic != IsExtensibleType(derivedType)) { 1871 messages_.Say(arg.name(), 1872 "Dummy argument '%s' of a defined input/output procedure must be %s when the derived type is %s"_err_en_US, 1873 arg.name(), isPolymorphic ? "TYPE()" : "CLASS()", 1874 isPolymorphic ? "not extensible" : "extensible"); 1875 } 1876 } else { 1877 messages_.Say(arg.name(), 1878 "Dummy argument '%s' of a defined input/output procedure must have a" 1879 " derived type"_err_en_US, 1880 arg.name()); 1881 } 1882 } 1883 } 1884 1885 void CheckHelper::CheckDioDummyIsDefaultInteger( 1886 const Symbol &subp, const Symbol &arg) { 1887 if (const DeclTypeSpec * type{arg.GetType()}; 1888 type && type->IsNumeric(TypeCategory::Integer)) { 1889 if (const auto kind{evaluate::ToInt64(type->numericTypeSpec().kind())}; 1890 kind && *kind == context_.GetDefaultKind(TypeCategory::Integer)) { 1891 return; 1892 } 1893 } 1894 messages_.Say(arg.name(), 1895 "Dummy argument '%s' of a defined input/output procedure" 1896 " must be an INTEGER of default KIND"_err_en_US, 1897 arg.name()); 1898 } 1899 1900 void CheckHelper::CheckDioDummyIsScalar(const Symbol &subp, const Symbol &arg) { 1901 if (arg.Rank() > 0 || arg.Corank() > 0) { 1902 messages_.Say(arg.name(), 1903 "Dummy argument '%s' of a defined input/output procedure" 1904 " must be a scalar"_err_en_US, 1905 arg.name()); 1906 } 1907 } 1908 1909 void CheckHelper::CheckDioDtvArg( 1910 const Symbol &subp, const Symbol *arg, GenericKind::DefinedIo ioKind) { 1911 // Dtv argument looks like: dtv-type-spec, INTENT(INOUT) :: dtv 1912 if (CheckDioDummyIsData(subp, arg, 0)) { 1913 CheckDioDummyIsDerived(subp, *arg, ioKind); 1914 CheckDioDummyAttrs(subp, *arg, 1915 ioKind == GenericKind::DefinedIo::ReadFormatted || 1916 ioKind == GenericKind::DefinedIo::ReadUnformatted 1917 ? Attr::INTENT_INOUT 1918 : Attr::INTENT_IN); 1919 } 1920 } 1921 1922 void CheckHelper::CheckDefaultIntegerArg( 1923 const Symbol &subp, const Symbol *arg, Attr intent) { 1924 // Argument looks like: INTEGER, INTENT(intent) :: arg 1925 if (CheckDioDummyIsData(subp, arg, 1)) { 1926 CheckDioDummyIsDefaultInteger(subp, *arg); 1927 CheckDioDummyIsScalar(subp, *arg); 1928 CheckDioDummyAttrs(subp, *arg, intent); 1929 } 1930 } 1931 1932 void CheckHelper::CheckDioAssumedLenCharacterArg(const Symbol &subp, 1933 const Symbol *arg, std::size_t argPosition, Attr intent) { 1934 // Argument looks like: CHARACTER (LEN=*), INTENT(intent) :: (iotype OR iomsg) 1935 if (CheckDioDummyIsData(subp, arg, argPosition)) { 1936 CheckDioDummyAttrs(subp, *arg, intent); 1937 if (!IsAssumedLengthCharacter(*arg)) { 1938 messages_.Say(arg->name(), 1939 "Dummy argument '%s' of a defined input/output procedure" 1940 " must be assumed-length CHARACTER"_err_en_US, 1941 arg->name()); 1942 } 1943 } 1944 } 1945 1946 void CheckHelper::CheckDioVlistArg( 1947 const Symbol &subp, const Symbol *arg, std::size_t argPosition) { 1948 // Vlist argument looks like: INTEGER, INTENT(IN) :: v_list(:) 1949 if (CheckDioDummyIsData(subp, arg, argPosition)) { 1950 CheckDioDummyIsDefaultInteger(subp, *arg); 1951 CheckDioDummyAttrs(subp, *arg, Attr::INTENT_IN); 1952 if (const auto *objectDetails{arg->detailsIf<ObjectEntityDetails>()}) { 1953 if (objectDetails->shape().IsDeferredShape()) { 1954 return; 1955 } 1956 } 1957 messages_.Say(arg->name(), 1958 "Dummy argument '%s' of a defined input/output procedure must be" 1959 " deferred shape"_err_en_US, 1960 arg->name()); 1961 } 1962 } 1963 1964 void CheckHelper::CheckDioArgCount( 1965 const Symbol &subp, GenericKind::DefinedIo ioKind, std::size_t argCount) { 1966 const std::size_t requiredArgCount{ 1967 (std::size_t)(ioKind == GenericKind::DefinedIo::ReadFormatted || 1968 ioKind == GenericKind::DefinedIo::WriteFormatted 1969 ? 6 1970 : 4)}; 1971 if (argCount != requiredArgCount) { 1972 SayWithDeclaration(subp, 1973 "Defined input/output procedure '%s' must have" 1974 " %d dummy arguments rather than %d"_err_en_US, 1975 subp.name(), requiredArgCount, argCount); 1976 context_.SetError(subp); 1977 } 1978 } 1979 1980 void CheckHelper::CheckDioDummyAttrs( 1981 const Symbol &subp, const Symbol &arg, Attr goodIntent) { 1982 // Defined I/O procedures can't have attributes other than INTENT 1983 Attrs attrs{arg.attrs()}; 1984 if (!attrs.test(goodIntent)) { 1985 messages_.Say(arg.name(), 1986 "Dummy argument '%s' of a defined input/output procedure" 1987 " must have intent '%s'"_err_en_US, 1988 arg.name(), AttrToString(goodIntent)); 1989 } 1990 attrs = attrs - Attr::INTENT_IN - Attr::INTENT_OUT - Attr::INTENT_INOUT; 1991 if (!attrs.empty()) { 1992 messages_.Say(arg.name(), 1993 "Dummy argument '%s' of a defined input/output procedure may not have" 1994 " any attributes"_err_en_US, 1995 arg.name()); 1996 } 1997 } 1998 1999 // Enforce semantics for defined input/output procedures (12.6.4.8.2) and C777 2000 void CheckHelper::CheckDefinedIoProc(const Symbol &symbol, 2001 const GenericDetails &details, GenericKind::DefinedIo ioKind) { 2002 for (auto ref : details.specificProcs()) { 2003 const auto *binding{ref->detailsIf<ProcBindingDetails>()}; 2004 const Symbol &specific{*(binding ? &binding->symbol() : &*ref)}; 2005 if (ref->attrs().test(Attr::NOPASS)) { // C774 2006 messages_.Say("Defined input/output procedure '%s' may not have NOPASS " 2007 "attribute"_err_en_US, 2008 ref->name()); 2009 context_.SetError(*ref); 2010 } 2011 if (const auto *subpDetails{specific.detailsIf<SubprogramDetails>()}) { 2012 const std::vector<Symbol *> &dummyArgs{subpDetails->dummyArgs()}; 2013 CheckDioArgCount(specific, ioKind, dummyArgs.size()); 2014 int argCount{0}; 2015 for (auto *arg : dummyArgs) { 2016 switch (argCount++) { 2017 case 0: 2018 // dtv-type-spec, INTENT(INOUT) :: dtv 2019 CheckDioDtvArg(specific, arg, ioKind); 2020 break; 2021 case 1: 2022 // INTEGER, INTENT(IN) :: unit 2023 CheckDefaultIntegerArg(specific, arg, Attr::INTENT_IN); 2024 break; 2025 case 2: 2026 if (ioKind == GenericKind::DefinedIo::ReadFormatted || 2027 ioKind == GenericKind::DefinedIo::WriteFormatted) { 2028 // CHARACTER (LEN=*), INTENT(IN) :: iotype 2029 CheckDioAssumedLenCharacterArg( 2030 specific, arg, argCount, Attr::INTENT_IN); 2031 } else { 2032 // INTEGER, INTENT(OUT) :: iostat 2033 CheckDefaultIntegerArg(specific, arg, Attr::INTENT_OUT); 2034 } 2035 break; 2036 case 3: 2037 if (ioKind == GenericKind::DefinedIo::ReadFormatted || 2038 ioKind == GenericKind::DefinedIo::WriteFormatted) { 2039 // INTEGER, INTENT(IN) :: v_list(:) 2040 CheckDioVlistArg(specific, arg, argCount); 2041 } else { 2042 // CHARACTER (LEN=*), INTENT(INOUT) :: iomsg 2043 CheckDioAssumedLenCharacterArg( 2044 specific, arg, argCount, Attr::INTENT_INOUT); 2045 } 2046 break; 2047 case 4: 2048 // INTEGER, INTENT(OUT) :: iostat 2049 CheckDefaultIntegerArg(specific, arg, Attr::INTENT_OUT); 2050 break; 2051 case 5: 2052 // CHARACTER (LEN=*), INTENT(INOUT) :: iomsg 2053 CheckDioAssumedLenCharacterArg( 2054 specific, arg, argCount, Attr::INTENT_INOUT); 2055 break; 2056 default:; 2057 } 2058 } 2059 } 2060 } 2061 } 2062 2063 void SubprogramMatchHelper::Check( 2064 const Symbol &symbol1, const Symbol &symbol2) { 2065 const auto details1{symbol1.get<SubprogramDetails>()}; 2066 const auto details2{symbol2.get<SubprogramDetails>()}; 2067 if (details1.isFunction() != details2.isFunction()) { 2068 Say(symbol1, symbol2, 2069 details1.isFunction() 2070 ? "Module function '%s' was declared as a subroutine in the" 2071 " corresponding interface body"_err_en_US 2072 : "Module subroutine '%s' was declared as a function in the" 2073 " corresponding interface body"_err_en_US); 2074 return; 2075 } 2076 const auto &args1{details1.dummyArgs()}; 2077 const auto &args2{details2.dummyArgs()}; 2078 int nargs1{static_cast<int>(args1.size())}; 2079 int nargs2{static_cast<int>(args2.size())}; 2080 if (nargs1 != nargs2) { 2081 Say(symbol1, symbol2, 2082 "Module subprogram '%s' has %d args but the corresponding interface" 2083 " body has %d"_err_en_US, 2084 nargs1, nargs2); 2085 return; 2086 } 2087 bool nonRecursive1{symbol1.attrs().test(Attr::NON_RECURSIVE)}; 2088 if (nonRecursive1 != symbol2.attrs().test(Attr::NON_RECURSIVE)) { // C1551 2089 Say(symbol1, symbol2, 2090 nonRecursive1 2091 ? "Module subprogram '%s' has NON_RECURSIVE prefix but" 2092 " the corresponding interface body does not"_err_en_US 2093 : "Module subprogram '%s' does not have NON_RECURSIVE prefix but " 2094 "the corresponding interface body does"_err_en_US); 2095 } 2096 const std::string *bindName1{details1.bindName()}; 2097 const std::string *bindName2{details2.bindName()}; 2098 if (!bindName1 && !bindName2) { 2099 // OK - neither has a binding label 2100 } else if (!bindName1) { 2101 Say(symbol1, symbol2, 2102 "Module subprogram '%s' does not have a binding label but the" 2103 " corresponding interface body does"_err_en_US); 2104 } else if (!bindName2) { 2105 Say(symbol1, symbol2, 2106 "Module subprogram '%s' has a binding label but the" 2107 " corresponding interface body does not"_err_en_US); 2108 } else if (*bindName1 != *bindName2) { 2109 Say(symbol1, symbol2, 2110 "Module subprogram '%s' has binding label '%s' but the corresponding" 2111 " interface body has '%s'"_err_en_US, 2112 *details1.bindName(), *details2.bindName()); 2113 } 2114 const Procedure *proc1{checkHelper.Characterize(symbol1)}; 2115 const Procedure *proc2{checkHelper.Characterize(symbol2)}; 2116 if (!proc1 || !proc2) { 2117 return; 2118 } 2119 if (proc1->functionResult && proc2->functionResult && 2120 *proc1->functionResult != *proc2->functionResult) { 2121 Say(symbol1, symbol2, 2122 "Return type of function '%s' does not match return type of" 2123 " the corresponding interface body"_err_en_US); 2124 } 2125 for (int i{0}; i < nargs1; ++i) { 2126 const Symbol *arg1{args1[i]}; 2127 const Symbol *arg2{args2[i]}; 2128 if (arg1 && !arg2) { 2129 Say(symbol1, symbol2, 2130 "Dummy argument %2$d of '%1$s' is not an alternate return indicator" 2131 " but the corresponding argument in the interface body is"_err_en_US, 2132 i + 1); 2133 } else if (!arg1 && arg2) { 2134 Say(symbol1, symbol2, 2135 "Dummy argument %2$d of '%1$s' is an alternate return indicator but" 2136 " the corresponding argument in the interface body is not"_err_en_US, 2137 i + 1); 2138 } else if (arg1 && arg2) { 2139 SourceName name1{arg1->name()}; 2140 SourceName name2{arg2->name()}; 2141 if (name1 != name2) { 2142 Say(*arg1, *arg2, 2143 "Dummy argument name '%s' does not match corresponding name '%s'" 2144 " in interface body"_err_en_US, 2145 name2); 2146 } else { 2147 CheckDummyArg( 2148 *arg1, *arg2, proc1->dummyArguments[i], proc2->dummyArguments[i]); 2149 } 2150 } 2151 } 2152 } 2153 2154 void SubprogramMatchHelper::CheckDummyArg(const Symbol &symbol1, 2155 const Symbol &symbol2, const DummyArgument &arg1, 2156 const DummyArgument &arg2) { 2157 std::visit(common::visitors{ 2158 [&](const DummyDataObject &obj1, const DummyDataObject &obj2) { 2159 CheckDummyDataObject(symbol1, symbol2, obj1, obj2); 2160 }, 2161 [&](const DummyProcedure &proc1, const DummyProcedure &proc2) { 2162 CheckDummyProcedure(symbol1, symbol2, proc1, proc2); 2163 }, 2164 [&](const DummyDataObject &, const auto &) { 2165 Say(symbol1, symbol2, 2166 "Dummy argument '%s' is a data object; the corresponding" 2167 " argument in the interface body is not"_err_en_US); 2168 }, 2169 [&](const DummyProcedure &, const auto &) { 2170 Say(symbol1, symbol2, 2171 "Dummy argument '%s' is a procedure; the corresponding" 2172 " argument in the interface body is not"_err_en_US); 2173 }, 2174 [&](const auto &, const auto &) { 2175 llvm_unreachable("Dummy arguments are not data objects or" 2176 "procedures"); 2177 }, 2178 }, 2179 arg1.u, arg2.u); 2180 } 2181 2182 void SubprogramMatchHelper::CheckDummyDataObject(const Symbol &symbol1, 2183 const Symbol &symbol2, const DummyDataObject &obj1, 2184 const DummyDataObject &obj2) { 2185 if (!CheckSameIntent(symbol1, symbol2, obj1.intent, obj2.intent)) { 2186 } else if (!CheckSameAttrs(symbol1, symbol2, obj1.attrs, obj2.attrs)) { 2187 } else if (obj1.type.type() != obj2.type.type()) { 2188 Say(symbol1, symbol2, 2189 "Dummy argument '%s' has type %s; the corresponding argument in the" 2190 " interface body has type %s"_err_en_US, 2191 obj1.type.type().AsFortran(), obj2.type.type().AsFortran()); 2192 } else if (!ShapesAreCompatible(obj1, obj2)) { 2193 Say(symbol1, symbol2, 2194 "The shape of dummy argument '%s' does not match the shape of the" 2195 " corresponding argument in the interface body"_err_en_US); 2196 } 2197 // TODO: coshape 2198 } 2199 2200 void SubprogramMatchHelper::CheckDummyProcedure(const Symbol &symbol1, 2201 const Symbol &symbol2, const DummyProcedure &proc1, 2202 const DummyProcedure &proc2) { 2203 if (!CheckSameIntent(symbol1, symbol2, proc1.intent, proc2.intent)) { 2204 } else if (!CheckSameAttrs(symbol1, symbol2, proc1.attrs, proc2.attrs)) { 2205 } else if (proc1 != proc2) { 2206 Say(symbol1, symbol2, 2207 "Dummy procedure '%s' does not match the corresponding argument in" 2208 " the interface body"_err_en_US); 2209 } 2210 } 2211 2212 bool SubprogramMatchHelper::CheckSameIntent(const Symbol &symbol1, 2213 const Symbol &symbol2, common::Intent intent1, common::Intent intent2) { 2214 if (intent1 == intent2) { 2215 return true; 2216 } else { 2217 Say(symbol1, symbol2, 2218 "The intent of dummy argument '%s' does not match the intent" 2219 " of the corresponding argument in the interface body"_err_en_US); 2220 return false; 2221 } 2222 } 2223 2224 // Report an error referring to first symbol with declaration of second symbol 2225 template <typename... A> 2226 void SubprogramMatchHelper::Say(const Symbol &symbol1, const Symbol &symbol2, 2227 parser::MessageFixedText &&text, A &&...args) { 2228 auto &message{context().Say(symbol1.name(), std::move(text), symbol1.name(), 2229 std::forward<A>(args)...)}; 2230 evaluate::AttachDeclaration(message, symbol2); 2231 } 2232 2233 template <typename ATTRS> 2234 bool SubprogramMatchHelper::CheckSameAttrs( 2235 const Symbol &symbol1, const Symbol &symbol2, ATTRS attrs1, ATTRS attrs2) { 2236 if (attrs1 == attrs2) { 2237 return true; 2238 } 2239 attrs1.IterateOverMembers([&](auto attr) { 2240 if (!attrs2.test(attr)) { 2241 Say(symbol1, symbol2, 2242 "Dummy argument '%s' has the %s attribute; the corresponding" 2243 " argument in the interface body does not"_err_en_US, 2244 AsFortran(attr)); 2245 } 2246 }); 2247 attrs2.IterateOverMembers([&](auto attr) { 2248 if (!attrs1.test(attr)) { 2249 Say(symbol1, symbol2, 2250 "Dummy argument '%s' does not have the %s attribute; the" 2251 " corresponding argument in the interface body does"_err_en_US, 2252 AsFortran(attr)); 2253 } 2254 }); 2255 return false; 2256 } 2257 2258 bool SubprogramMatchHelper::ShapesAreCompatible( 2259 const DummyDataObject &obj1, const DummyDataObject &obj2) { 2260 return characteristics::ShapesAreCompatible( 2261 FoldShape(obj1.type.shape()), FoldShape(obj2.type.shape())); 2262 } 2263 2264 evaluate::Shape SubprogramMatchHelper::FoldShape(const evaluate::Shape &shape) { 2265 evaluate::Shape result; 2266 for (const auto &extent : shape) { 2267 result.emplace_back( 2268 evaluate::Fold(context().foldingContext(), common::Clone(extent))); 2269 } 2270 return result; 2271 } 2272 2273 void DistinguishabilityHelper::Add(const Symbol &generic, GenericKind kind, 2274 const Symbol &specific, const Procedure &procedure) { 2275 if (!context_.HasError(specific)) { 2276 nameToInfo_[generic.name()].emplace_back( 2277 ProcedureInfo{kind, specific, procedure}); 2278 } 2279 } 2280 2281 void DistinguishabilityHelper::Check(const Scope &scope) { 2282 for (const auto &[name, info] : nameToInfo_) { 2283 auto count{info.size()}; 2284 for (std::size_t i1{0}; i1 < count - 1; ++i1) { 2285 const auto &[kind, symbol, proc]{info[i1]}; 2286 for (std::size_t i2{i1 + 1}; i2 < count; ++i2) { 2287 auto distinguishable{kind.IsName() 2288 ? evaluate::characteristics::Distinguishable 2289 : evaluate::characteristics::DistinguishableOpOrAssign}; 2290 if (!distinguishable(proc, info[i2].procedure)) { 2291 SayNotDistinguishable(GetTopLevelUnitContaining(scope), name, kind, 2292 symbol, info[i2].symbol); 2293 } 2294 } 2295 } 2296 } 2297 } 2298 2299 void DistinguishabilityHelper::SayNotDistinguishable(const Scope &scope, 2300 const SourceName &name, GenericKind kind, const Symbol &proc1, 2301 const Symbol &proc2) { 2302 std::string name1{proc1.name().ToString()}; 2303 std::string name2{proc2.name().ToString()}; 2304 if (kind.IsOperator() || kind.IsAssignment()) { 2305 // proc1 and proc2 may come from different scopes so qualify their names 2306 if (proc1.owner().IsDerivedType()) { 2307 name1 = proc1.owner().GetName()->ToString() + '%' + name1; 2308 } 2309 if (proc2.owner().IsDerivedType()) { 2310 name2 = proc2.owner().GetName()->ToString() + '%' + name2; 2311 } 2312 } 2313 parser::Message *msg; 2314 if (scope.sourceRange().Contains(name)) { 2315 msg = &context_.Say(name, 2316 "Generic '%s' may not have specific procedures '%s' and" 2317 " '%s' as their interfaces are not distinguishable"_err_en_US, 2318 MakeOpName(name), name1, name2); 2319 } else { 2320 msg = &context_.Say(*GetTopLevelUnitContaining(proc1).GetName(), 2321 "USE-associated generic '%s' may not have specific procedures '%s' " 2322 "and" 2323 " '%s' as their interfaces are not distinguishable"_err_en_US, 2324 MakeOpName(name), name1, name2); 2325 } 2326 AttachDeclaration(*msg, scope, proc1); 2327 AttachDeclaration(*msg, scope, proc2); 2328 } 2329 2330 // `evaluate::AttachDeclaration` doesn't handle the generic case where `proc` 2331 // comes from a different module but is not necessarily use-associated. 2332 void DistinguishabilityHelper::AttachDeclaration( 2333 parser::Message &msg, const Scope &scope, const Symbol &proc) { 2334 const Scope &unit{GetTopLevelUnitContaining(proc)}; 2335 if (unit == scope) { 2336 evaluate::AttachDeclaration(msg, proc); 2337 } else { 2338 msg.Attach(unit.GetName().value(), 2339 "'%s' is USE-associated from module '%s'"_en_US, proc.name(), 2340 unit.GetName().value()); 2341 } 2342 } 2343 2344 void CheckDeclarations(SemanticsContext &context) { 2345 CheckHelper{context}.Check(); 2346 } 2347 } // namespace Fortran::semantics 2348