1 //===-- lib/Semantics/check-call.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 #include "check-call.h" 10 #include "pointer-assignment.h" 11 #include "flang/Evaluate/characteristics.h" 12 #include "flang/Evaluate/check-expression.h" 13 #include "flang/Evaluate/shape.h" 14 #include "flang/Evaluate/tools.h" 15 #include "flang/Parser/characters.h" 16 #include "flang/Parser/message.h" 17 #include "flang/Semantics/scope.h" 18 #include "flang/Semantics/tools.h" 19 #include <map> 20 #include <string> 21 22 using namespace Fortran::parser::literals; 23 namespace characteristics = Fortran::evaluate::characteristics; 24 25 namespace Fortran::semantics { 26 27 static void CheckImplicitInterfaceArg( 28 evaluate::ActualArgument &arg, parser::ContextualMessages &messages) { 29 if (auto kw{arg.keyword()}) { 30 messages.Say(*kw, 31 "Keyword '%s=' may not appear in a reference to a procedure with an implicit interface"_err_en_US, 32 *kw); 33 } 34 if (auto type{arg.GetType()}) { 35 if (type->IsAssumedType()) { 36 messages.Say( 37 "Assumed type argument requires an explicit interface"_err_en_US); 38 } else if (type->IsPolymorphic()) { 39 messages.Say( 40 "Polymorphic argument requires an explicit interface"_err_en_US); 41 } else if (const DerivedTypeSpec * derived{GetDerivedTypeSpec(type)}) { 42 if (!derived->parameters().empty()) { 43 messages.Say( 44 "Parameterized derived type argument requires an explicit interface"_err_en_US); 45 } 46 } 47 } 48 if (const auto *expr{arg.UnwrapExpr()}) { 49 if (auto named{evaluate::ExtractNamedEntity(*expr)}) { 50 const Symbol &symbol{named->GetLastSymbol()}; 51 if (symbol.Corank() > 0) { 52 messages.Say( 53 "Coarray argument requires an explicit interface"_err_en_US); 54 } 55 if (const auto *details{symbol.detailsIf<ObjectEntityDetails>()}) { 56 if (details->IsAssumedRank()) { 57 messages.Say( 58 "Assumed rank argument requires an explicit interface"_err_en_US); 59 } 60 } 61 if (symbol.attrs().test(Attr::ASYNCHRONOUS)) { 62 messages.Say( 63 "ASYNCHRONOUS argument requires an explicit interface"_err_en_US); 64 } 65 if (symbol.attrs().test(Attr::VOLATILE)) { 66 messages.Say( 67 "VOLATILE argument requires an explicit interface"_err_en_US); 68 } 69 } 70 } 71 } 72 73 // When scalar CHARACTER actual arguments are known to be short, 74 // we extend them on the right with spaces and a warning. 75 static void PadShortCharacterActual(evaluate::Expr<evaluate::SomeType> &actual, 76 const characteristics::TypeAndShape &dummyType, 77 characteristics::TypeAndShape &actualType, 78 evaluate::FoldingContext &context, parser::ContextualMessages &messages) { 79 if (dummyType.type().category() == TypeCategory::Character && 80 actualType.type().category() == TypeCategory::Character && 81 dummyType.type().kind() == actualType.type().kind() && 82 GetRank(actualType.shape()) == 0) { 83 if (dummyType.LEN() && actualType.LEN()) { 84 auto dummyLength{ToInt64(Fold(context, common::Clone(*dummyType.LEN())))}; 85 auto actualLength{ 86 ToInt64(Fold(context, common::Clone(*actualType.LEN())))}; 87 if (dummyLength && actualLength && *actualLength < *dummyLength) { 88 messages.Say( 89 "Actual length '%jd' is less than expected length '%jd'"_en_US, 90 *actualLength, *dummyLength); 91 auto converted{ConvertToType(dummyType.type(), std::move(actual))}; 92 CHECK(converted); 93 actual = std::move(*converted); 94 actualType.set_LEN(SubscriptIntExpr{*dummyLength}); 95 } 96 } 97 } 98 } 99 100 // Automatic conversion of different-kind INTEGER scalar actual 101 // argument expressions (not variables) to INTEGER scalar dummies. 102 // We return nonstandard INTEGER(8) results from intrinsic functions 103 // like SIZE() by default in order to facilitate the use of large 104 // arrays. Emit a warning when downconverting. 105 static void ConvertIntegerActual(evaluate::Expr<evaluate::SomeType> &actual, 106 const characteristics::TypeAndShape &dummyType, 107 characteristics::TypeAndShape &actualType, 108 parser::ContextualMessages &messages) { 109 if (dummyType.type().category() == TypeCategory::Integer && 110 actualType.type().category() == TypeCategory::Integer && 111 dummyType.type().kind() != actualType.type().kind() && 112 GetRank(dummyType.shape()) == 0 && GetRank(actualType.shape()) == 0 && 113 !evaluate::IsVariable(actual)) { 114 auto converted{ 115 evaluate::ConvertToType(dummyType.type(), std::move(actual))}; 116 CHECK(converted); 117 actual = std::move(*converted); 118 if (dummyType.type().kind() < actualType.type().kind()) { 119 messages.Say( 120 "Actual argument scalar expression of type INTEGER(%d) was converted to smaller dummy argument type INTEGER(%d)"_en_US, 121 actualType.type().kind(), dummyType.type().kind()); 122 } 123 actualType = dummyType; 124 } 125 } 126 127 static bool DefersSameTypeParameters( 128 const DerivedTypeSpec &actual, const DerivedTypeSpec &dummy) { 129 for (const auto &pair : actual.parameters()) { 130 const ParamValue &actualValue{pair.second}; 131 const ParamValue *dummyValue{dummy.FindParameter(pair.first)}; 132 if (!dummyValue || (actualValue.isDeferred() != dummyValue->isDeferred())) { 133 return false; 134 } 135 } 136 return true; 137 } 138 139 static void CheckExplicitDataArg(const characteristics::DummyDataObject &dummy, 140 const std::string &dummyName, evaluate::Expr<evaluate::SomeType> &actual, 141 characteristics::TypeAndShape &actualType, bool isElemental, 142 evaluate::FoldingContext &context, const Scope *scope, 143 const evaluate::SpecificIntrinsic *intrinsic) { 144 145 // Basic type & rank checking 146 parser::ContextualMessages &messages{context.messages()}; 147 PadShortCharacterActual(actual, dummy.type, actualType, context, messages); 148 ConvertIntegerActual(actual, dummy.type, actualType, messages); 149 bool typesCompatible{dummy.type.type().IsTkCompatibleWith(actualType.type())}; 150 if (typesCompatible) { 151 if (isElemental) { 152 } else if (dummy.type.attrs().test( 153 characteristics::TypeAndShape::Attr::AssumedRank)) { 154 } else if (!dummy.type.attrs().test( 155 characteristics::TypeAndShape::Attr::AssumedShape) && 156 (actualType.Rank() > 0 || IsArrayElement(actual))) { 157 // Sequence association (15.5.2.11) applies -- rank need not match 158 // if the actual argument is an array or array element designator. 159 } else { 160 // Let CheckConformance accept scalars; storage association 161 // cases are checked here below. 162 CheckConformance(messages, dummy.type.shape(), actualType.shape(), 163 "dummy argument", "actual argument", true, true); 164 } 165 } else { 166 const auto &len{actualType.LEN()}; 167 messages.Say( 168 "Actual argument type '%s' is not compatible with dummy argument type '%s'"_err_en_US, 169 actualType.type().AsFortran(len ? len->AsFortran() : ""), 170 dummy.type.type().AsFortran()); 171 } 172 173 bool actualIsPolymorphic{actualType.type().IsPolymorphic()}; 174 bool dummyIsPolymorphic{dummy.type.type().IsPolymorphic()}; 175 bool actualIsCoindexed{ExtractCoarrayRef(actual).has_value()}; 176 bool actualIsAssumedSize{actualType.attrs().test( 177 characteristics::TypeAndShape::Attr::AssumedSize)}; 178 bool dummyIsAssumedSize{dummy.type.attrs().test( 179 characteristics::TypeAndShape::Attr::AssumedSize)}; 180 bool dummyIsAsynchronous{ 181 dummy.attrs.test(characteristics::DummyDataObject::Attr::Asynchronous)}; 182 bool dummyIsVolatile{ 183 dummy.attrs.test(characteristics::DummyDataObject::Attr::Volatile)}; 184 bool dummyIsValue{ 185 dummy.attrs.test(characteristics::DummyDataObject::Attr::Value)}; 186 187 if (actualIsPolymorphic && dummyIsPolymorphic && 188 actualIsCoindexed) { // 15.5.2.4(2) 189 messages.Say( 190 "Coindexed polymorphic object may not be associated with a polymorphic %s"_err_en_US, 191 dummyName); 192 } 193 if (actualIsPolymorphic && !dummyIsPolymorphic && 194 actualIsAssumedSize) { // 15.5.2.4(2) 195 messages.Say( 196 "Assumed-size polymorphic array may not be associated with a monomorphic %s"_err_en_US, 197 dummyName); 198 } 199 200 // Derived type actual argument checks 201 const Symbol *actualFirstSymbol{evaluate::GetFirstSymbol(actual)}; 202 bool actualIsAsynchronous{ 203 actualFirstSymbol && actualFirstSymbol->attrs().test(Attr::ASYNCHRONOUS)}; 204 bool actualIsVolatile{ 205 actualFirstSymbol && actualFirstSymbol->attrs().test(Attr::VOLATILE)}; 206 if (const auto *derived{evaluate::GetDerivedTypeSpec(actualType.type())}) { 207 if (dummy.type.type().IsAssumedType()) { 208 if (!derived->parameters().empty()) { // 15.5.2.4(2) 209 messages.Say( 210 "Actual argument associated with TYPE(*) %s may not have a parameterized derived type"_err_en_US, 211 dummyName); 212 } 213 if (const Symbol * 214 tbp{FindImmediateComponent(*derived, [](const Symbol &symbol) { 215 return symbol.has<ProcBindingDetails>(); 216 })}) { // 15.5.2.4(2) 217 evaluate::SayWithDeclaration(messages, *tbp, 218 "Actual argument associated with TYPE(*) %s may not have type-bound procedure '%s'"_err_en_US, 219 dummyName, tbp->name()); 220 } 221 const auto &finals{ 222 derived->typeSymbol().get<DerivedTypeDetails>().finals()}; 223 if (!finals.empty()) { // 15.5.2.4(2) 224 if (auto *msg{messages.Say( 225 "Actual argument associated with TYPE(*) %s may not have derived type '%s' with FINAL subroutine '%s'"_err_en_US, 226 dummyName, derived->typeSymbol().name(), 227 finals.begin()->first)}) { 228 msg->Attach(finals.begin()->first, 229 "FINAL subroutine '%s' in derived type '%s'"_en_US, 230 finals.begin()->first, derived->typeSymbol().name()); 231 } 232 } 233 } 234 if (actualIsCoindexed) { 235 if (dummy.intent != common::Intent::In && !dummyIsValue) { 236 if (auto bad{ 237 FindAllocatableUltimateComponent(*derived)}) { // 15.5.2.4(6) 238 evaluate::SayWithDeclaration(messages, *bad, 239 "Coindexed actual argument with ALLOCATABLE ultimate component '%s' must be associated with a %s with VALUE or INTENT(IN) attributes"_err_en_US, 240 bad.BuildResultDesignatorName(), dummyName); 241 } 242 } 243 if (auto coarrayRef{evaluate::ExtractCoarrayRef(actual)}) { // C1537 244 const Symbol &coarray{coarrayRef->GetLastSymbol()}; 245 if (const DeclTypeSpec * type{coarray.GetType()}) { 246 if (const DerivedTypeSpec * derived{type->AsDerived()}) { 247 if (auto bad{semantics::FindPointerUltimateComponent(*derived)}) { 248 evaluate::SayWithDeclaration(messages, coarray, 249 "Coindexed object '%s' with POINTER ultimate component '%s' cannot be associated with %s"_err_en_US, 250 coarray.name(), bad.BuildResultDesignatorName(), dummyName); 251 } 252 } 253 } 254 } 255 } 256 if (actualIsVolatile != dummyIsVolatile) { // 15.5.2.4(22) 257 if (auto bad{semantics::FindCoarrayUltimateComponent(*derived)}) { 258 evaluate::SayWithDeclaration(messages, *bad, 259 "VOLATILE attribute must match for %s when actual argument has a coarray ultimate component '%s'"_err_en_US, 260 dummyName, bad.BuildResultDesignatorName()); 261 } 262 } 263 } 264 265 // Rank and shape checks 266 const auto *actualLastSymbol{evaluate::GetLastSymbol(actual)}; 267 if (actualLastSymbol) { 268 actualLastSymbol = &ResolveAssociations(*actualLastSymbol); 269 } 270 const ObjectEntityDetails *actualLastObject{actualLastSymbol 271 ? actualLastSymbol->detailsIf<ObjectEntityDetails>() 272 : nullptr}; 273 int actualRank{evaluate::GetRank(actualType.shape())}; 274 bool actualIsPointer{evaluate::IsObjectPointer(actual, context)}; 275 bool dummyIsAssumedRank{dummy.type.attrs().test( 276 characteristics::TypeAndShape::Attr::AssumedRank)}; 277 if (dummy.type.attrs().test( 278 characteristics::TypeAndShape::Attr::AssumedShape)) { 279 // 15.5.2.4(16) 280 if (actualRank == 0) { 281 messages.Say( 282 "Scalar actual argument may not be associated with assumed-shape %s"_err_en_US, 283 dummyName); 284 } 285 if (actualIsAssumedSize && actualLastSymbol) { 286 evaluate::SayWithDeclaration(messages, *actualLastSymbol, 287 "Assumed-size array may not be associated with assumed-shape %s"_err_en_US, 288 dummyName); 289 } 290 } else if (actualRank == 0 && dummy.type.Rank() > 0) { 291 // Actual is scalar, dummy is an array. 15.5.2.4(14), 15.5.2.11 292 if (actualIsCoindexed) { 293 messages.Say( 294 "Coindexed scalar actual argument must be associated with a scalar %s"_err_en_US, 295 dummyName); 296 } 297 if (!IsArrayElement(actual) && 298 !(actualType.type().category() == TypeCategory::Character && 299 actualType.type().kind() == 1) && 300 !(dummy.type.type().IsAssumedType() && dummyIsAssumedSize) && 301 !dummyIsAssumedRank) { 302 messages.Say( 303 "Whole scalar actual argument may not be associated with a %s array"_err_en_US, 304 dummyName); 305 } 306 if (actualIsPolymorphic) { 307 messages.Say( 308 "Polymorphic scalar may not be associated with a %s array"_err_en_US, 309 dummyName); 310 } 311 if (actualIsPointer) { 312 messages.Say( 313 "Scalar POINTER target may not be associated with a %s array"_err_en_US, 314 dummyName); 315 } 316 if (actualLastObject && actualLastObject->IsAssumedShape()) { 317 messages.Say( 318 "Element of assumed-shape array may not be associated with a %s array"_err_en_US, 319 dummyName); 320 } 321 } 322 if (actualLastObject && actualLastObject->IsCoarray() && 323 IsAllocatable(*actualLastSymbol) && dummy.intent == common::Intent::Out && 324 !(intrinsic && 325 evaluate::AcceptsIntentOutAllocatableCoarray( 326 intrinsic->name))) { // C846 327 messages.Say( 328 "ALLOCATABLE coarray '%s' may not be associated with INTENT(OUT) %s"_err_en_US, 329 actualLastSymbol->name(), dummyName); 330 } 331 332 // Definability 333 const char *reason{nullptr}; 334 if (dummy.intent == common::Intent::Out) { 335 reason = "INTENT(OUT)"; 336 } else if (dummy.intent == common::Intent::InOut) { 337 reason = "INTENT(IN OUT)"; 338 } else if (dummyIsAsynchronous) { 339 reason = "ASYNCHRONOUS"; 340 } else if (dummyIsVolatile) { 341 reason = "VOLATILE"; 342 } 343 if (reason && scope) { 344 bool vectorSubscriptIsOk{isElemental || dummyIsValue}; // 15.5.2.4(21) 345 if (auto why{WhyNotModifiable( 346 messages.at(), actual, *scope, vectorSubscriptIsOk)}) { 347 if (auto *msg{messages.Say( 348 "Actual argument associated with %s %s must be definable"_err_en_US, // C1158 349 reason, dummyName)}) { 350 msg->Attach(*why); 351 } 352 } 353 } 354 355 // Cases when temporaries might be needed but must not be permitted. 356 bool dummyIsPointer{ 357 dummy.attrs.test(characteristics::DummyDataObject::Attr::Pointer)}; 358 bool dummyIsContiguous{ 359 dummy.attrs.test(characteristics::DummyDataObject::Attr::Contiguous)}; 360 bool actualIsContiguous{IsSimplyContiguous(actual, context)}; 361 bool dummyIsAssumedShape{dummy.type.attrs().test( 362 characteristics::TypeAndShape::Attr::AssumedShape)}; 363 if ((actualIsAsynchronous || actualIsVolatile) && 364 (dummyIsAsynchronous || dummyIsVolatile) && !dummyIsValue) { 365 if (actualIsCoindexed) { // C1538 366 messages.Say( 367 "Coindexed ASYNCHRONOUS or VOLATILE actual argument may not be associated with %s with ASYNCHRONOUS or VOLATILE attributes unless VALUE"_err_en_US, 368 dummyName); 369 } 370 if (actualRank > 0 && !actualIsContiguous) { 371 if (dummyIsContiguous || 372 !(dummyIsAssumedShape || dummyIsAssumedRank || 373 (actualIsPointer && dummyIsPointer))) { // C1539 & C1540 374 messages.Say( 375 "ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous %s"_err_en_US, 376 dummyName); 377 } 378 } 379 } 380 381 // 15.5.2.6 -- dummy is ALLOCATABLE 382 bool dummyIsAllocatable{ 383 dummy.attrs.test(characteristics::DummyDataObject::Attr::Allocatable)}; 384 bool actualIsAllocatable{ 385 actualLastSymbol && IsAllocatable(*actualLastSymbol)}; 386 if (dummyIsAllocatable) { 387 if (!actualIsAllocatable) { 388 messages.Say( 389 "ALLOCATABLE %s must be associated with an ALLOCATABLE actual argument"_err_en_US, 390 dummyName); 391 } 392 if (actualIsAllocatable && actualIsCoindexed && 393 dummy.intent != common::Intent::In) { 394 messages.Say( 395 "ALLOCATABLE %s must have INTENT(IN) to be associated with a coindexed actual argument"_err_en_US, 396 dummyName); 397 } 398 if (!actualIsCoindexed && actualLastSymbol && 399 actualLastSymbol->Corank() != dummy.type.corank()) { 400 messages.Say( 401 "ALLOCATABLE %s has corank %d but actual argument has corank %d"_err_en_US, 402 dummyName, dummy.type.corank(), actualLastSymbol->Corank()); 403 } 404 } 405 406 // 15.5.2.7 -- dummy is POINTER 407 if (dummyIsPointer) { 408 if (dummyIsContiguous && !actualIsContiguous) { 409 messages.Say( 410 "Actual argument associated with CONTIGUOUS POINTER %s must be simply contiguous"_err_en_US, 411 dummyName); 412 } 413 if (!actualIsPointer) { 414 if (dummy.intent == common::Intent::In) { 415 semantics::CheckPointerAssignment( 416 context, parser::CharBlock{}, dummyName, dummy, actual); 417 } else { 418 messages.Say( 419 "Actual argument associated with POINTER %s must also be POINTER unless INTENT(IN)"_err_en_US, 420 dummyName); 421 } 422 } 423 } 424 425 // 15.5.2.5 -- actual & dummy are both POINTER or both ALLOCATABLE 426 if ((actualIsPointer && dummyIsPointer) || 427 (actualIsAllocatable && dummyIsAllocatable)) { 428 bool actualIsUnlimited{actualType.type().IsUnlimitedPolymorphic()}; 429 bool dummyIsUnlimited{dummy.type.type().IsUnlimitedPolymorphic()}; 430 if (actualIsUnlimited != dummyIsUnlimited) { 431 if (typesCompatible) { 432 messages.Say( 433 "If a POINTER or ALLOCATABLE dummy or actual argument is unlimited polymorphic, both must be so"_err_en_US); 434 } 435 } else if (dummyIsPolymorphic != actualIsPolymorphic) { 436 if (dummy.intent == common::Intent::In && typesCompatible) { 437 // extension: allow with warning, rule is only relevant for definables 438 messages.Say( 439 "If a POINTER or ALLOCATABLE dummy or actual argument is polymorphic, both should be so"_en_US); 440 } else { 441 messages.Say( 442 "If a POINTER or ALLOCATABLE dummy or actual argument is polymorphic, both must be so"_err_en_US); 443 } 444 } else if (!actualIsUnlimited && typesCompatible) { 445 if (!actualType.type().IsTkCompatibleWith(dummy.type.type())) { 446 if (dummy.intent == common::Intent::In) { 447 // extension: allow with warning, rule is only relevant for definables 448 messages.Say( 449 "POINTER or ALLOCATABLE dummy and actual arguments should have the same declared type and kind"_en_US); 450 } else { 451 messages.Say( 452 "POINTER or ALLOCATABLE dummy and actual arguments must have the same declared type and kind"_err_en_US); 453 } 454 } 455 if (const auto *derived{ 456 evaluate::GetDerivedTypeSpec(actualType.type())}) { 457 if (!DefersSameTypeParameters( 458 *derived, *evaluate::GetDerivedTypeSpec(dummy.type.type()))) { 459 messages.Say( 460 "Dummy and actual arguments must defer the same type parameters when POINTER or ALLOCATABLE"_err_en_US); 461 } 462 } 463 } 464 } 465 466 // 15.5.2.8 -- coarray dummy arguments 467 if (dummy.type.corank() > 0) { 468 if (actualType.corank() == 0) { 469 messages.Say( 470 "Actual argument associated with coarray %s must be a coarray"_err_en_US, 471 dummyName); 472 } 473 if (dummyIsVolatile) { 474 if (!actualIsVolatile) { 475 messages.Say( 476 "non-VOLATILE coarray may not be associated with VOLATILE coarray %s"_err_en_US, 477 dummyName); 478 } 479 } else { 480 if (actualIsVolatile) { 481 messages.Say( 482 "VOLATILE coarray may not be associated with non-VOLATILE coarray %s"_err_en_US, 483 dummyName); 484 } 485 } 486 if (actualRank == dummy.type.Rank() && !actualIsContiguous) { 487 if (dummyIsContiguous) { 488 messages.Say( 489 "Actual argument associated with a CONTIGUOUS coarray %s must be simply contiguous"_err_en_US, 490 dummyName); 491 } else if (!dummyIsAssumedShape && !dummyIsAssumedRank) { 492 messages.Say( 493 "Actual argument associated with coarray %s (not assumed shape or rank) must be simply contiguous"_err_en_US, 494 dummyName); 495 } 496 } 497 } 498 } 499 500 static void CheckProcedureArg(evaluate::ActualArgument &arg, 501 const characteristics::DummyProcedure &proc, const std::string &dummyName, 502 evaluate::FoldingContext &context) { 503 parser::ContextualMessages &messages{context.messages()}; 504 const characteristics::Procedure &interface{proc.procedure.value()}; 505 if (const auto *expr{arg.UnwrapExpr()}) { 506 bool dummyIsPointer{ 507 proc.attrs.test(characteristics::DummyProcedure::Attr::Pointer)}; 508 const auto *argProcDesignator{ 509 std::get_if<evaluate::ProcedureDesignator>(&expr->u)}; 510 const auto *argProcSymbol{ 511 argProcDesignator ? argProcDesignator->GetSymbol() : nullptr}; 512 if (auto argChars{characteristics::DummyArgument::FromActual( 513 "actual argument", *expr, context)}) { 514 if (!argChars->IsTypelessIntrinsicDummy()) { 515 if (auto *argProc{ 516 std::get_if<characteristics::DummyProcedure>(&argChars->u)}) { 517 characteristics::Procedure &argInterface{argProc->procedure.value()}; 518 argInterface.attrs.reset( 519 characteristics::Procedure::Attr::NullPointer); 520 if (!argProcSymbol || argProcSymbol->attrs().test(Attr::INTRINSIC)) { 521 // It's ok to pass ELEMENTAL unrestricted intrinsic functions. 522 argInterface.attrs.reset( 523 characteristics::Procedure::Attr::Elemental); 524 } else if (argInterface.attrs.test( 525 characteristics::Procedure::Attr::Elemental)) { 526 if (argProcSymbol) { // C1533 527 evaluate::SayWithDeclaration(messages, *argProcSymbol, 528 "Non-intrinsic ELEMENTAL procedure '%s' may not be passed as an actual argument"_err_en_US, 529 argProcSymbol->name()); 530 return; // avoid piling on with checks below 531 } else { 532 argInterface.attrs.reset( 533 characteristics::Procedure::Attr::NullPointer); 534 } 535 } 536 if (!interface.IsPure()) { 537 // 15.5.2.9(1): if dummy is not pure, actual need not be. 538 argInterface.attrs.reset(characteristics::Procedure::Attr::Pure); 539 } 540 if (interface.HasExplicitInterface()) { 541 if (interface != argInterface) { 542 // 15.5.2.9(1): Explicit interfaces must match 543 if (argInterface.HasExplicitInterface()) { 544 messages.Say( 545 "Actual procedure argument has interface incompatible with %s"_err_en_US, 546 dummyName); 547 return; 548 } else { 549 messages.Say( 550 "Actual procedure argument has an implicit interface " 551 "which is not known to be compatible with %s which has an " 552 "explicit interface"_err_en_US, 553 dummyName); 554 return; 555 } 556 } 557 } else { // 15.5.2.9(2,3) 558 if (interface.IsSubroutine() && argInterface.IsFunction()) { 559 messages.Say( 560 "Actual argument associated with procedure %s is a function but must be a subroutine"_err_en_US, 561 dummyName); 562 } else if (interface.IsFunction()) { 563 if (argInterface.IsFunction()) { 564 if (interface.functionResult != argInterface.functionResult) { 565 messages.Say( 566 "Actual argument function associated with procedure %s has incompatible result type"_err_en_US, 567 dummyName); 568 } 569 } else if (argInterface.IsSubroutine()) { 570 messages.Say( 571 "Actual argument associated with procedure %s is a subroutine but must be a function"_err_en_US, 572 dummyName); 573 } 574 } 575 } 576 } else { 577 messages.Say( 578 "Actual argument associated with procedure %s is not a procedure"_err_en_US, 579 dummyName); 580 } 581 } else if (!(dummyIsPointer && IsNullPointer(*expr))) { 582 messages.Say( 583 "Actual argument associated with procedure %s is not a procedure"_err_en_US, 584 dummyName); 585 } 586 } 587 if (interface.HasExplicitInterface()) { 588 if (dummyIsPointer) { 589 // 15.5.2.9(5) -- dummy procedure POINTER 590 // Interface compatibility has already been checked above by comparison. 591 if (proc.intent != common::Intent::In && !IsVariable(*expr)) { 592 messages.Say( 593 "Actual argument associated with procedure pointer %s must be a POINTER unless INTENT(IN)"_err_en_US, 594 dummyName); 595 } 596 } else { // 15.5.2.9(4) -- dummy procedure is not POINTER 597 if (!argProcDesignator) { 598 messages.Say( 599 "Actual argument associated with non-POINTER procedure %s must be a procedure (and not a procedure pointer)"_err_en_US, 600 dummyName); 601 } 602 } 603 } 604 } else { 605 messages.Say( 606 "Assumed-type argument may not be forwarded as procedure %s"_err_en_US, 607 dummyName); 608 } 609 } 610 611 static void CheckExplicitInterfaceArg(evaluate::ActualArgument &arg, 612 const characteristics::DummyArgument &dummy, 613 const characteristics::Procedure &proc, evaluate::FoldingContext &context, 614 const Scope *scope, const evaluate::SpecificIntrinsic *intrinsic) { 615 auto &messages{context.messages()}; 616 std::string dummyName{"dummy argument"}; 617 if (!dummy.name.empty()) { 618 dummyName += " '"s + parser::ToLowerCaseLetters(dummy.name) + "='"; 619 } 620 std::visit( 621 common::visitors{ 622 [&](const characteristics::DummyDataObject &object) { 623 if (auto *expr{arg.UnwrapExpr()}) { 624 if (auto type{characteristics::TypeAndShape::Characterize( 625 *expr, context)}) { 626 arg.set_dummyIntent(object.intent); 627 bool isElemental{object.type.Rank() == 0 && proc.IsElemental()}; 628 CheckExplicitDataArg(object, dummyName, *expr, *type, 629 isElemental, context, scope, intrinsic); 630 } else if (object.type.type().IsTypelessIntrinsicArgument() && 631 std::holds_alternative<evaluate::BOZLiteralConstant>( 632 expr->u)) { 633 // ok 634 } else if (object.type.type().IsTypelessIntrinsicArgument() && 635 evaluate::IsNullPointer(*expr)) { 636 // ok, ASSOCIATED(NULL()) 637 } else if (object.attrs.test( 638 characteristics::DummyDataObject::Attr::Pointer) && 639 evaluate::IsNullPointer(*expr)) { 640 // ok, FOO(NULL()) 641 } else { 642 messages.Say( 643 "Actual argument '%s' associated with %s is not a variable or typed expression"_err_en_US, 644 expr->AsFortran(), dummyName); 645 } 646 } else { 647 const Symbol &assumed{DEREF(arg.GetAssumedTypeDummy())}; 648 if (!object.type.type().IsAssumedType()) { 649 messages.Say( 650 "Assumed-type '%s' may be associated only with an assumed-type %s"_err_en_US, 651 assumed.name(), dummyName); 652 } else if (const auto *details{ 653 assumed.detailsIf<ObjectEntityDetails>()}) { 654 if (!(details->IsAssumedShape() || details->IsAssumedRank())) { 655 messages.Say( // C711 656 "Assumed-type '%s' must be either assumed shape or assumed rank to be associated with assumed-type %s"_err_en_US, 657 assumed.name(), dummyName); 658 } 659 } 660 } 661 }, 662 [&](const characteristics::DummyProcedure &proc) { 663 CheckProcedureArg(arg, proc, dummyName, context); 664 }, 665 [&](const characteristics::AlternateReturn &) { 666 // All semantic checking is done elsewhere 667 }, 668 }, 669 dummy.u); 670 } 671 672 static void RearrangeArguments(const characteristics::Procedure &proc, 673 evaluate::ActualArguments &actuals, parser::ContextualMessages &messages) { 674 CHECK(proc.HasExplicitInterface()); 675 if (actuals.size() < proc.dummyArguments.size()) { 676 actuals.resize(proc.dummyArguments.size()); 677 } else if (actuals.size() > proc.dummyArguments.size()) { 678 messages.Say( 679 "Too many actual arguments (%zd) passed to procedure that expects only %zd"_err_en_US, 680 actuals.size(), proc.dummyArguments.size()); 681 } 682 std::map<std::string, evaluate::ActualArgument> kwArgs; 683 for (auto &x : actuals) { 684 if (x && x->keyword()) { 685 auto emplaced{ 686 kwArgs.try_emplace(x->keyword()->ToString(), std::move(*x))}; 687 if (!emplaced.second) { 688 messages.Say(*x->keyword(), 689 "Argument keyword '%s=' appears on more than one effective argument in this procedure reference"_err_en_US, 690 *x->keyword()); 691 } 692 x.reset(); 693 } 694 } 695 if (!kwArgs.empty()) { 696 int index{0}; 697 for (const auto &dummy : proc.dummyArguments) { 698 if (!dummy.name.empty()) { 699 auto iter{kwArgs.find(dummy.name)}; 700 if (iter != kwArgs.end()) { 701 evaluate::ActualArgument &x{iter->second}; 702 if (actuals[index]) { 703 messages.Say(*x.keyword(), 704 "Keyword argument '%s=' has already been specified positionally (#%d) in this procedure reference"_err_en_US, 705 *x.keyword(), index + 1); 706 } else { 707 actuals[index] = std::move(x); 708 } 709 kwArgs.erase(iter); 710 } 711 } 712 ++index; 713 } 714 for (auto &bad : kwArgs) { 715 evaluate::ActualArgument &x{bad.second}; 716 messages.Say(*x.keyword(), 717 "Argument keyword '%s=' is not recognized for this procedure reference"_err_en_US, 718 *x.keyword()); 719 } 720 } 721 } 722 723 static parser::Messages CheckExplicitInterface( 724 const characteristics::Procedure &proc, evaluate::ActualArguments &actuals, 725 const evaluate::FoldingContext &context, const Scope *scope, 726 const evaluate::SpecificIntrinsic *intrinsic) { 727 parser::Messages buffer; 728 parser::ContextualMessages messages{context.messages().at(), &buffer}; 729 RearrangeArguments(proc, actuals, messages); 730 if (buffer.empty()) { 731 int index{0}; 732 evaluate::FoldingContext localContext{context, messages}; 733 for (auto &actual : actuals) { 734 const auto &dummy{proc.dummyArguments.at(index++)}; 735 if (actual) { 736 CheckExplicitInterfaceArg( 737 *actual, dummy, proc, localContext, scope, intrinsic); 738 } else if (!dummy.IsOptional()) { 739 if (dummy.name.empty()) { 740 messages.Say( 741 "Dummy argument #%d is not OPTIONAL and is not associated with " 742 "an actual argument in this procedure reference"_err_en_US, 743 index); 744 } else { 745 messages.Say("Dummy argument '%s=' (#%d) is not OPTIONAL and is not " 746 "associated with an actual argument in this procedure " 747 "reference"_err_en_US, 748 dummy.name, index); 749 } 750 } 751 } 752 } 753 return buffer; 754 } 755 756 parser::Messages CheckExplicitInterface(const characteristics::Procedure &proc, 757 evaluate::ActualArguments &actuals, const evaluate::FoldingContext &context, 758 const Scope &scope, const evaluate::SpecificIntrinsic *intrinsic) { 759 return CheckExplicitInterface(proc, actuals, context, &scope, intrinsic); 760 } 761 762 bool CheckInterfaceForGeneric(const characteristics::Procedure &proc, 763 evaluate::ActualArguments &actuals, 764 const evaluate::FoldingContext &context) { 765 return CheckExplicitInterface(proc, actuals, context, nullptr, nullptr) 766 .empty(); 767 } 768 769 void CheckArguments(const characteristics::Procedure &proc, 770 evaluate::ActualArguments &actuals, evaluate::FoldingContext &context, 771 const Scope &scope, bool treatingExternalAsImplicit, 772 const evaluate::SpecificIntrinsic *intrinsic) { 773 bool explicitInterface{proc.HasExplicitInterface()}; 774 if (explicitInterface) { 775 auto buffer{ 776 CheckExplicitInterface(proc, actuals, context, scope, intrinsic)}; 777 if (treatingExternalAsImplicit && !buffer.empty()) { 778 if (auto *msg{context.messages().Say( 779 "Warning: if the procedure's interface were explicit, this reference would be in error:"_en_US)}) { 780 buffer.AttachTo(*msg); 781 } 782 } 783 if (auto *msgs{context.messages().messages()}) { 784 msgs->Merge(std::move(buffer)); 785 } 786 } 787 if (!explicitInterface || treatingExternalAsImplicit) { 788 for (auto &actual : actuals) { 789 if (actual) { 790 CheckImplicitInterfaceArg(*actual, context.messages()); 791 } 792 } 793 } 794 } 795 } // namespace Fortran::semantics 796