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