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.IsPure()) { 565 // 15.5.2.9(1): if dummy is not pure, actual need not be. 566 argInterface.attrs.reset(characteristics::Procedure::Attr::Pure); 567 } 568 if (interface.HasExplicitInterface()) { 569 if (interface != argInterface) { 570 // 15.5.2.9(1): Explicit interfaces must match 571 if (argInterface.HasExplicitInterface()) { 572 messages.Say( 573 "Actual procedure argument has interface incompatible with %s"_err_en_US, 574 dummyName); 575 return; 576 } else if (proc.IsPure()) { 577 messages.Say( 578 "Actual procedure argument for %s of a PURE procedure must have an explicit interface"_err_en_US, 579 dummyName); 580 } else { 581 messages.Say( 582 "Actual procedure argument has an implicit interface " 583 "which is not known to be compatible with %s which has an " 584 "explicit interface"_en_US, 585 dummyName); 586 } 587 } 588 } else { // 15.5.2.9(2,3) 589 if (interface.IsSubroutine() && argInterface.IsFunction()) { 590 messages.Say( 591 "Actual argument associated with procedure %s is a function but must be a subroutine"_err_en_US, 592 dummyName); 593 } else if (interface.IsFunction()) { 594 if (argInterface.IsFunction()) { 595 if (interface.functionResult != argInterface.functionResult) { 596 messages.Say( 597 "Actual argument function associated with procedure %s has incompatible result type"_err_en_US, 598 dummyName); 599 } 600 } else if (argInterface.IsSubroutine()) { 601 messages.Say( 602 "Actual argument associated with procedure %s is a subroutine but must be a function"_err_en_US, 603 dummyName); 604 } 605 } 606 } 607 } else { 608 messages.Say( 609 "Actual argument associated with procedure %s is not a procedure"_err_en_US, 610 dummyName); 611 } 612 } else if (IsNullPointer(*expr)) { 613 if (!dummyIsPointer) { 614 messages.Say( 615 "Actual argument associated with procedure %s is a null pointer"_err_en_US, 616 dummyName); 617 } 618 } else { 619 messages.Say( 620 "Actual argument associated with procedure %s is typeless"_err_en_US, 621 dummyName); 622 } 623 } 624 if (interface.HasExplicitInterface() && dummyIsPointer && 625 dummy.intent != common::Intent::In) { 626 const Symbol *last{GetLastSymbol(*expr)}; 627 if (!(last && IsProcedurePointer(*last))) { 628 // 15.5.2.9(5) -- dummy procedure POINTER 629 // Interface compatibility has already been checked above by comparison. 630 messages.Say( 631 "Actual argument associated with procedure pointer %s must be a POINTER unless INTENT(IN)"_err_en_US, 632 dummyName); 633 } 634 } 635 } else { 636 messages.Say( 637 "Assumed-type argument may not be forwarded as procedure %s"_err_en_US, 638 dummyName); 639 } 640 } 641 642 // Allow BOZ literal actual arguments when they can be converted to a known 643 // dummy argument type 644 static void ConvertBOZLiteralArg( 645 evaluate::ActualArgument &arg, const evaluate::DynamicType &type) { 646 if (auto *expr{arg.UnwrapExpr()}) { 647 if (IsBOZLiteral(*expr)) { 648 if (auto converted{evaluate::ConvertToType(type, SomeExpr{*expr})}) { 649 arg = std::move(*converted); 650 } 651 } 652 } 653 } 654 655 static void CheckExplicitInterfaceArg(evaluate::ActualArgument &arg, 656 const characteristics::DummyArgument &dummy, 657 const characteristics::Procedure &proc, evaluate::FoldingContext &context, 658 const Scope *scope, const evaluate::SpecificIntrinsic *intrinsic, 659 bool allowIntegerConversions) { 660 auto &messages{context.messages()}; 661 std::string dummyName{"dummy argument"}; 662 if (!dummy.name.empty()) { 663 dummyName += " '"s + parser::ToLowerCaseLetters(dummy.name) + "='"; 664 } 665 auto restorer{ 666 messages.SetLocation(arg.sourceLocation().value_or(messages.at()))}; 667 std::visit( 668 common::visitors{ 669 [&](const characteristics::DummyDataObject &object) { 670 ConvertBOZLiteralArg(arg, object.type.type()); 671 if (auto *expr{arg.UnwrapExpr()}) { 672 if (auto type{characteristics::TypeAndShape::Characterize( 673 *expr, context)}) { 674 arg.set_dummyIntent(object.intent); 675 bool isElemental{object.type.Rank() == 0 && proc.IsElemental()}; 676 CheckExplicitDataArg(object, dummyName, *expr, *type, 677 isElemental, context, scope, intrinsic, 678 allowIntegerConversions); 679 } else if (object.type.type().IsTypelessIntrinsicArgument() && 680 IsBOZLiteral(*expr)) { 681 // ok 682 } else if (object.type.type().IsTypelessIntrinsicArgument() && 683 evaluate::IsNullPointer(*expr)) { 684 // ok, ASSOCIATED(NULL()) 685 } else if ((object.attrs.test(characteristics::DummyDataObject:: 686 Attr::Pointer) || 687 object.attrs.test(characteristics:: 688 DummyDataObject::Attr::Optional)) && 689 evaluate::IsNullPointer(*expr)) { 690 // ok, FOO(NULL()) 691 } else { 692 messages.Say( 693 "Actual argument '%s' associated with %s is not a variable or typed expression"_err_en_US, 694 expr->AsFortran(), dummyName); 695 } 696 } else { 697 const Symbol &assumed{DEREF(arg.GetAssumedTypeDummy())}; 698 if (!object.type.type().IsAssumedType()) { 699 messages.Say( 700 "Assumed-type '%s' may be associated only with an assumed-type %s"_err_en_US, 701 assumed.name(), dummyName); 702 } else if (object.type.attrs().test(evaluate::characteristics:: 703 TypeAndShape::Attr::AssumedRank) && 704 !IsAssumedShape(assumed) && 705 !evaluate::IsAssumedRank(assumed)) { 706 messages.Say( // C711 707 "Assumed-type '%s' must be either assumed shape or assumed rank to be associated with assumed rank %s"_err_en_US, 708 assumed.name(), dummyName); 709 } 710 } 711 }, 712 [&](const characteristics::DummyProcedure &dummy) { 713 CheckProcedureArg(arg, proc, dummy, dummyName, context); 714 }, 715 [&](const characteristics::AlternateReturn &) { 716 // All semantic checking is done elsewhere 717 }, 718 }, 719 dummy.u); 720 } 721 722 static void RearrangeArguments(const characteristics::Procedure &proc, 723 evaluate::ActualArguments &actuals, parser::ContextualMessages &messages) { 724 CHECK(proc.HasExplicitInterface()); 725 if (actuals.size() < proc.dummyArguments.size()) { 726 actuals.resize(proc.dummyArguments.size()); 727 } else if (actuals.size() > proc.dummyArguments.size()) { 728 messages.Say( 729 "Too many actual arguments (%zd) passed to procedure that expects only %zd"_err_en_US, 730 actuals.size(), proc.dummyArguments.size()); 731 } 732 std::map<std::string, evaluate::ActualArgument> kwArgs; 733 for (auto &x : actuals) { 734 if (x && x->keyword()) { 735 auto emplaced{ 736 kwArgs.try_emplace(x->keyword()->ToString(), std::move(*x))}; 737 if (!emplaced.second) { 738 messages.Say(*x->keyword(), 739 "Argument keyword '%s=' appears on more than one effective argument in this procedure reference"_err_en_US, 740 *x->keyword()); 741 } 742 x.reset(); 743 } 744 } 745 if (!kwArgs.empty()) { 746 int index{0}; 747 for (const auto &dummy : proc.dummyArguments) { 748 if (!dummy.name.empty()) { 749 auto iter{kwArgs.find(dummy.name)}; 750 if (iter != kwArgs.end()) { 751 evaluate::ActualArgument &x{iter->second}; 752 if (actuals[index]) { 753 messages.Say(*x.keyword(), 754 "Keyword argument '%s=' has already been specified positionally (#%d) in this procedure reference"_err_en_US, 755 *x.keyword(), index + 1); 756 } else { 757 actuals[index] = std::move(x); 758 } 759 kwArgs.erase(iter); 760 } 761 } 762 ++index; 763 } 764 for (auto &bad : kwArgs) { 765 evaluate::ActualArgument &x{bad.second}; 766 messages.Say(*x.keyword(), 767 "Argument keyword '%s=' is not recognized for this procedure reference"_err_en_US, 768 *x.keyword()); 769 } 770 } 771 } 772 773 // The actual argument arrays to an ELEMENTAL procedure must conform. 774 static bool CheckElementalConformance(parser::ContextualMessages &messages, 775 const characteristics::Procedure &proc, evaluate::ActualArguments &actuals, 776 evaluate::FoldingContext &context) { 777 std::optional<evaluate::Shape> shape; 778 std::string shapeName; 779 int index{0}; 780 for (const auto &arg : actuals) { 781 const auto &dummy{proc.dummyArguments.at(index++)}; 782 if (arg) { 783 if (const auto *expr{arg->UnwrapExpr()}) { 784 if (auto argShape{evaluate::GetShape(context, *expr)}) { 785 if (GetRank(*argShape) > 0) { 786 std::string argName{"actual argument ("s + expr->AsFortran() + 787 ") corresponding to dummy argument #" + std::to_string(index) + 788 " ('" + dummy.name + "')"}; 789 if (shape) { 790 auto tristate{evaluate::CheckConformance(messages, *shape, 791 *argShape, evaluate::CheckConformanceFlags::None, 792 shapeName.c_str(), argName.c_str())}; 793 if (tristate && !*tristate) { 794 return false; 795 } 796 } else { 797 shape = std::move(argShape); 798 shapeName = argName; 799 } 800 } 801 } 802 } 803 } 804 } 805 return true; 806 } 807 808 static parser::Messages CheckExplicitInterface( 809 const characteristics::Procedure &proc, evaluate::ActualArguments &actuals, 810 const evaluate::FoldingContext &context, const Scope *scope, 811 const evaluate::SpecificIntrinsic *intrinsic, 812 bool allowIntegerConversions) { 813 parser::Messages buffer; 814 parser::ContextualMessages messages{context.messages().at(), &buffer}; 815 RearrangeArguments(proc, actuals, messages); 816 if (buffer.empty()) { 817 int index{0}; 818 evaluate::FoldingContext localContext{context, messages}; 819 for (auto &actual : actuals) { 820 const auto &dummy{proc.dummyArguments.at(index++)}; 821 if (actual) { 822 CheckExplicitInterfaceArg(*actual, dummy, proc, localContext, scope, 823 intrinsic, allowIntegerConversions); 824 } else if (!dummy.IsOptional()) { 825 if (dummy.name.empty()) { 826 messages.Say( 827 "Dummy argument #%d is not OPTIONAL and is not associated with " 828 "an actual argument in this procedure reference"_err_en_US, 829 index); 830 } else { 831 messages.Say("Dummy argument '%s=' (#%d) is not OPTIONAL and is not " 832 "associated with an actual argument in this procedure " 833 "reference"_err_en_US, 834 dummy.name, index); 835 } 836 } 837 } 838 if (proc.IsElemental() && !buffer.AnyFatalError()) { 839 CheckElementalConformance(messages, proc, actuals, localContext); 840 } 841 } 842 return buffer; 843 } 844 845 parser::Messages CheckExplicitInterface(const characteristics::Procedure &proc, 846 evaluate::ActualArguments &actuals, const evaluate::FoldingContext &context, 847 const Scope &scope, const evaluate::SpecificIntrinsic *intrinsic) { 848 return CheckExplicitInterface( 849 proc, actuals, context, &scope, intrinsic, true); 850 } 851 852 bool CheckInterfaceForGeneric(const characteristics::Procedure &proc, 853 evaluate::ActualArguments &actuals, const evaluate::FoldingContext &context, 854 bool allowIntegerConversions) { 855 return !CheckExplicitInterface( 856 proc, actuals, context, nullptr, nullptr, allowIntegerConversions) 857 .AnyFatalError(); 858 } 859 860 void CheckArguments(const characteristics::Procedure &proc, 861 evaluate::ActualArguments &actuals, evaluate::FoldingContext &context, 862 const Scope &scope, bool treatingExternalAsImplicit, 863 const evaluate::SpecificIntrinsic *intrinsic) { 864 bool explicitInterface{proc.HasExplicitInterface()}; 865 parser::ContextualMessages &messages{context.messages()}; 866 if (!explicitInterface || treatingExternalAsImplicit) { 867 parser::Messages buffer; 868 { 869 auto restorer{messages.SetMessages(buffer)}; 870 for (auto &actual : actuals) { 871 if (actual) { 872 CheckImplicitInterfaceArg(*actual, messages); 873 } 874 } 875 } 876 if (!buffer.empty()) { 877 if (auto *msgs{messages.messages()}) { 878 msgs->Annex(std::move(buffer)); 879 } 880 return; // don't pile on 881 } 882 } 883 if (explicitInterface) { 884 auto buffer{ 885 CheckExplicitInterface(proc, actuals, context, scope, intrinsic)}; 886 if (treatingExternalAsImplicit && !buffer.empty()) { 887 if (auto *msg{messages.Say( 888 "Warning: if the procedure's interface were explicit, this reference would be in error:"_en_US)}) { 889 buffer.AttachTo(*msg); 890 } 891 } 892 if (auto *msgs{messages.messages()}) { 893 msgs->Annex(std::move(buffer)); 894 } 895 } 896 } 897 } // namespace Fortran::semantics 898