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'"_warn_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)"_port_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 bool actualIsArrayElement{IsArrayElement(actual)}; 313 bool actualIsCKindCharacter{ 314 actualType.type().category() == TypeCategory::Character && 315 actualType.type().kind() == 1}; 316 if (!actualIsCKindCharacter) { 317 if (!actualIsArrayElement && 318 !(dummy.type.type().IsAssumedType() && dummyIsAssumedSize) && 319 !dummyIsAssumedRank) { 320 messages.Say( 321 "Whole scalar actual argument may not be associated with a %s array"_err_en_US, 322 dummyName); 323 } 324 if (actualIsPolymorphic) { 325 messages.Say( 326 "Polymorphic scalar may not be associated with a %s array"_err_en_US, 327 dummyName); 328 } 329 if (actualIsArrayElement && actualLastSymbol && 330 IsPointer(*actualLastSymbol)) { 331 messages.Say( 332 "Element of pointer array may not be associated with a %s array"_err_en_US, 333 dummyName); 334 } 335 if (actualLastSymbol && IsAssumedShape(*actualLastSymbol)) { 336 messages.Say( 337 "Element of assumed-shape array may not be associated with a %s array"_err_en_US, 338 dummyName); 339 } 340 } 341 } 342 if (actualLastObject && actualLastObject->IsCoarray() && 343 IsAllocatable(*actualLastSymbol) && dummy.intent == common::Intent::Out && 344 !(intrinsic && 345 evaluate::AcceptsIntentOutAllocatableCoarray( 346 intrinsic->name))) { // C846 347 messages.Say( 348 "ALLOCATABLE coarray '%s' may not be associated with INTENT(OUT) %s"_err_en_US, 349 actualLastSymbol->name(), dummyName); 350 } 351 352 // Definability 353 const char *reason{nullptr}; 354 if (dummy.intent == common::Intent::Out) { 355 reason = "INTENT(OUT)"; 356 } else if (dummy.intent == common::Intent::InOut) { 357 reason = "INTENT(IN OUT)"; 358 } else if (dummyIsAsynchronous) { 359 reason = "ASYNCHRONOUS"; 360 } else if (dummyIsVolatile) { 361 reason = "VOLATILE"; 362 } 363 if (reason && scope) { 364 bool vectorSubscriptIsOk{isElemental || dummyIsValue}; // 15.5.2.4(21) 365 if (auto why{WhyNotModifiable( 366 messages.at(), actual, *scope, vectorSubscriptIsOk)}) { 367 if (auto *msg{messages.Say( 368 "Actual argument associated with %s %s must be definable"_err_en_US, // C1158 369 reason, dummyName)}) { 370 msg->Attach(*why); 371 } 372 } 373 } 374 375 // Cases when temporaries might be needed but must not be permitted. 376 bool actualIsContiguous{IsSimplyContiguous(actual, context)}; 377 bool dummyIsAssumedShape{dummy.type.attrs().test( 378 characteristics::TypeAndShape::Attr::AssumedShape)}; 379 bool dummyIsPointer{ 380 dummy.attrs.test(characteristics::DummyDataObject::Attr::Pointer)}; 381 bool dummyIsContiguous{ 382 dummy.attrs.test(characteristics::DummyDataObject::Attr::Contiguous)}; 383 if ((actualIsAsynchronous || actualIsVolatile) && 384 (dummyIsAsynchronous || dummyIsVolatile) && !dummyIsValue) { 385 if (actualIsCoindexed) { // C1538 386 messages.Say( 387 "Coindexed ASYNCHRONOUS or VOLATILE actual argument may not be associated with %s with ASYNCHRONOUS or VOLATILE attributes unless VALUE"_err_en_US, 388 dummyName); 389 } 390 if (actualRank > 0 && !actualIsContiguous) { 391 if (dummyIsContiguous || 392 !(dummyIsAssumedShape || dummyIsAssumedRank || 393 (actualIsPointer && dummyIsPointer))) { // C1539 & C1540 394 messages.Say( 395 "ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous %s"_err_en_US, 396 dummyName); 397 } 398 } 399 } 400 401 // 15.5.2.6 -- dummy is ALLOCATABLE 402 bool dummyIsAllocatable{ 403 dummy.attrs.test(characteristics::DummyDataObject::Attr::Allocatable)}; 404 bool actualIsAllocatable{ 405 actualLastSymbol && IsAllocatable(*actualLastSymbol)}; 406 if (dummyIsAllocatable) { 407 if (!actualIsAllocatable) { 408 messages.Say( 409 "ALLOCATABLE %s must be associated with an ALLOCATABLE actual argument"_err_en_US, 410 dummyName); 411 } 412 if (actualIsAllocatable && actualIsCoindexed && 413 dummy.intent != common::Intent::In) { 414 messages.Say( 415 "ALLOCATABLE %s must have INTENT(IN) to be associated with a coindexed actual argument"_err_en_US, 416 dummyName); 417 } 418 if (!actualIsCoindexed && actualLastSymbol && 419 actualLastSymbol->Corank() != dummy.type.corank()) { 420 messages.Say( 421 "ALLOCATABLE %s has corank %d but actual argument has corank %d"_err_en_US, 422 dummyName, dummy.type.corank(), actualLastSymbol->Corank()); 423 } 424 } 425 426 // 15.5.2.7 -- dummy is POINTER 427 if (dummyIsPointer) { 428 if (dummyIsContiguous && !actualIsContiguous) { 429 messages.Say( 430 "Actual argument associated with CONTIGUOUS POINTER %s must be simply contiguous"_err_en_US, 431 dummyName); 432 } 433 if (!actualIsPointer) { 434 if (dummy.intent == common::Intent::In) { 435 semantics::CheckPointerAssignment( 436 context, parser::CharBlock{}, dummyName, dummy, actual); 437 } else { 438 messages.Say( 439 "Actual argument associated with POINTER %s must also be POINTER unless INTENT(IN)"_err_en_US, 440 dummyName); 441 } 442 } 443 } 444 445 // 15.5.2.5 -- actual & dummy are both POINTER or both ALLOCATABLE 446 if ((actualIsPointer && dummyIsPointer) || 447 (actualIsAllocatable && dummyIsAllocatable)) { 448 bool actualIsUnlimited{actualType.type().IsUnlimitedPolymorphic()}; 449 bool dummyIsUnlimited{dummy.type.type().IsUnlimitedPolymorphic()}; 450 if (actualIsUnlimited != dummyIsUnlimited) { 451 if (typesCompatible) { 452 messages.Say( 453 "If a POINTER or ALLOCATABLE dummy or actual argument is unlimited polymorphic, both must be so"_err_en_US); 454 } 455 } else if (dummyIsPolymorphic != actualIsPolymorphic) { 456 if (dummy.intent == common::Intent::In && typesCompatible) { 457 // extension: allow with warning, rule is only relevant for definables 458 messages.Say( 459 "If a POINTER or ALLOCATABLE dummy or actual argument is polymorphic, both should be so"_port_en_US); 460 } else { 461 messages.Say( 462 "If a POINTER or ALLOCATABLE dummy or actual argument is polymorphic, both must be so"_err_en_US); 463 } 464 } else if (!actualIsUnlimited && typesCompatible) { 465 if (!actualType.type().IsTkCompatibleWith(dummy.type.type())) { 466 if (dummy.intent == common::Intent::In) { 467 // extension: allow with warning, rule is only relevant for definables 468 messages.Say( 469 "POINTER or ALLOCATABLE dummy and actual arguments should have the same declared type and kind"_port_en_US); 470 } else { 471 messages.Say( 472 "POINTER or ALLOCATABLE dummy and actual arguments must have the same declared type and kind"_err_en_US); 473 } 474 } 475 if (const auto *derived{ 476 evaluate::GetDerivedTypeSpec(actualType.type())}) { 477 if (!DefersSameTypeParameters( 478 *derived, *evaluate::GetDerivedTypeSpec(dummy.type.type()))) { 479 messages.Say( 480 "Dummy and actual arguments must defer the same type parameters when POINTER or ALLOCATABLE"_err_en_US); 481 } 482 } 483 } 484 } 485 486 // 15.5.2.8 -- coarray dummy arguments 487 if (dummy.type.corank() > 0) { 488 if (actualType.corank() == 0) { 489 messages.Say( 490 "Actual argument associated with coarray %s must be a coarray"_err_en_US, 491 dummyName); 492 } 493 if (dummyIsVolatile) { 494 if (!actualIsVolatile) { 495 messages.Say( 496 "non-VOLATILE coarray may not be associated with VOLATILE coarray %s"_err_en_US, 497 dummyName); 498 } 499 } else { 500 if (actualIsVolatile) { 501 messages.Say( 502 "VOLATILE coarray may not be associated with non-VOLATILE coarray %s"_err_en_US, 503 dummyName); 504 } 505 } 506 if (actualRank == dummy.type.Rank() && !actualIsContiguous) { 507 if (dummyIsContiguous) { 508 messages.Say( 509 "Actual argument associated with a CONTIGUOUS coarray %s must be simply contiguous"_err_en_US, 510 dummyName); 511 } else if (!dummyIsAssumedShape && !dummyIsAssumedRank) { 512 messages.Say( 513 "Actual argument associated with coarray %s (not assumed shape or rank) must be simply contiguous"_err_en_US, 514 dummyName); 515 } 516 } 517 } 518 519 // NULL(MOLD=) checking for non-intrinsic procedures 520 bool dummyIsOptional{ 521 dummy.attrs.test(characteristics::DummyDataObject::Attr::Optional)}; 522 bool actualIsNull{evaluate::IsNullPointer(actual)}; 523 if (!intrinsic && !dummyIsPointer && !dummyIsOptional && actualIsNull) { 524 messages.Say( 525 "Actual argument associated with %s may not be null pointer %s"_err_en_US, 526 dummyName, actual.AsFortran()); 527 } 528 } 529 530 static void CheckProcedureArg(evaluate::ActualArgument &arg, 531 const characteristics::Procedure &proc, 532 const characteristics::DummyProcedure &dummy, const std::string &dummyName, 533 evaluate::FoldingContext &context) { 534 parser::ContextualMessages &messages{context.messages()}; 535 auto restorer{ 536 messages.SetLocation(arg.sourceLocation().value_or(messages.at()))}; 537 const characteristics::Procedure &interface { dummy.procedure.value() }; 538 if (const auto *expr{arg.UnwrapExpr()}) { 539 bool dummyIsPointer{ 540 dummy.attrs.test(characteristics::DummyProcedure::Attr::Pointer)}; 541 const auto *argProcDesignator{ 542 std::get_if<evaluate::ProcedureDesignator>(&expr->u)}; 543 const auto *argProcSymbol{ 544 argProcDesignator ? argProcDesignator->GetSymbol() : nullptr}; 545 if (auto argChars{characteristics::DummyArgument::FromActual( 546 "actual argument", *expr, context)}) { 547 if (!argChars->IsTypelessIntrinsicDummy()) { 548 if (auto *argProc{ 549 std::get_if<characteristics::DummyProcedure>(&argChars->u)}) { 550 characteristics::Procedure &argInterface{argProc->procedure.value()}; 551 argInterface.attrs.reset( 552 characteristics::Procedure::Attr::NullPointer); 553 if (!argProcSymbol || argProcSymbol->attrs().test(Attr::INTRINSIC)) { 554 // It's ok to pass ELEMENTAL unrestricted intrinsic functions. 555 argInterface.attrs.reset( 556 characteristics::Procedure::Attr::Elemental); 557 } else if (argInterface.attrs.test( 558 characteristics::Procedure::Attr::Elemental)) { 559 if (argProcSymbol) { // C1533 560 evaluate::SayWithDeclaration(messages, *argProcSymbol, 561 "Non-intrinsic ELEMENTAL procedure '%s' may not be passed as an actual argument"_err_en_US, 562 argProcSymbol->name()); 563 return; // avoid piling on with checks below 564 } else { 565 argInterface.attrs.reset( 566 characteristics::Procedure::Attr::NullPointer); 567 } 568 } 569 if (interface.HasExplicitInterface()) { 570 if (!interface.IsCompatibleWith(argInterface)) { 571 // 15.5.2.9(1): Explicit interfaces must match 572 if (argInterface.HasExplicitInterface()) { 573 messages.Say( 574 "Actual procedure argument has interface incompatible with %s"_err_en_US, 575 dummyName); 576 return; 577 } else if (proc.IsPure()) { 578 messages.Say( 579 "Actual procedure argument for %s of a PURE procedure must have an explicit interface"_err_en_US, 580 dummyName); 581 } else { 582 messages.Say( 583 "Actual procedure argument has an implicit interface " 584 "which is not known to be compatible with %s which has an " 585 "explicit interface"_warn_en_US, 586 dummyName); 587 } 588 } 589 } else { // 15.5.2.9(2,3) 590 if (interface.IsSubroutine() && argInterface.IsFunction()) { 591 messages.Say( 592 "Actual argument associated with procedure %s is a function but must be a subroutine"_err_en_US, 593 dummyName); 594 } else if (interface.IsFunction()) { 595 if (argInterface.IsFunction()) { 596 if (!interface.functionResult->IsCompatibleWith( 597 *argInterface.functionResult)) { 598 messages.Say( 599 "Actual argument function associated with procedure %s has incompatible result type"_err_en_US, 600 dummyName); 601 } 602 } else if (argInterface.IsSubroutine()) { 603 messages.Say( 604 "Actual argument associated with procedure %s is a subroutine but must be a function"_err_en_US, 605 dummyName); 606 } 607 } 608 } 609 } else { 610 messages.Say( 611 "Actual argument associated with procedure %s is not a procedure"_err_en_US, 612 dummyName); 613 } 614 } else if (IsNullPointer(*expr)) { 615 if (!dummyIsPointer) { 616 messages.Say( 617 "Actual argument associated with procedure %s is a null pointer"_err_en_US, 618 dummyName); 619 } 620 } else { 621 messages.Say( 622 "Actual argument associated with procedure %s is typeless"_err_en_US, 623 dummyName); 624 } 625 } 626 if (interface.HasExplicitInterface() && dummyIsPointer && 627 dummy.intent != common::Intent::In) { 628 const Symbol *last{GetLastSymbol(*expr)}; 629 if (!(last && IsProcedurePointer(*last))) { 630 // 15.5.2.9(5) -- dummy procedure POINTER 631 // Interface compatibility has already been checked above 632 messages.Say( 633 "Actual argument associated with procedure pointer %s must be a POINTER unless INTENT(IN)"_err_en_US, 634 dummyName); 635 } 636 } 637 } else { 638 messages.Say( 639 "Assumed-type argument may not be forwarded as procedure %s"_err_en_US, 640 dummyName); 641 } 642 } 643 644 // Allow BOZ literal actual arguments when they can be converted to a known 645 // dummy argument type 646 static void ConvertBOZLiteralArg( 647 evaluate::ActualArgument &arg, const evaluate::DynamicType &type) { 648 if (auto *expr{arg.UnwrapExpr()}) { 649 if (IsBOZLiteral(*expr)) { 650 if (auto converted{evaluate::ConvertToType(type, SomeExpr{*expr})}) { 651 arg = std::move(*converted); 652 } 653 } 654 } 655 } 656 657 static void CheckExplicitInterfaceArg(evaluate::ActualArgument &arg, 658 const characteristics::DummyArgument &dummy, 659 const characteristics::Procedure &proc, evaluate::FoldingContext &context, 660 const Scope *scope, const evaluate::SpecificIntrinsic *intrinsic, 661 bool allowIntegerConversions) { 662 auto &messages{context.messages()}; 663 std::string dummyName{"dummy argument"}; 664 if (!dummy.name.empty()) { 665 dummyName += " '"s + parser::ToLowerCaseLetters(dummy.name) + "='"; 666 } 667 auto restorer{ 668 messages.SetLocation(arg.sourceLocation().value_or(messages.at()))}; 669 std::visit( 670 common::visitors{ 671 [&](const characteristics::DummyDataObject &object) { 672 ConvertBOZLiteralArg(arg, object.type.type()); 673 if (auto *expr{arg.UnwrapExpr()}) { 674 if (auto type{characteristics::TypeAndShape::Characterize( 675 *expr, context)}) { 676 arg.set_dummyIntent(object.intent); 677 bool isElemental{object.type.Rank() == 0 && proc.IsElemental()}; 678 CheckExplicitDataArg(object, dummyName, *expr, *type, 679 isElemental, context, scope, intrinsic, 680 allowIntegerConversions); 681 } else if (object.type.type().IsTypelessIntrinsicArgument() && 682 IsBOZLiteral(*expr)) { 683 // ok 684 } else if (object.type.type().IsTypelessIntrinsicArgument() && 685 evaluate::IsNullPointer(*expr)) { 686 // ok, ASSOCIATED(NULL()) 687 } else if ((object.attrs.test(characteristics::DummyDataObject:: 688 Attr::Pointer) || 689 object.attrs.test(characteristics:: 690 DummyDataObject::Attr::Optional)) && 691 evaluate::IsNullPointer(*expr)) { 692 // ok, FOO(NULL()) 693 } else { 694 messages.Say( 695 "Actual argument '%s' associated with %s is not a variable or typed expression"_err_en_US, 696 expr->AsFortran(), dummyName); 697 } 698 } else { 699 const Symbol &assumed{DEREF(arg.GetAssumedTypeDummy())}; 700 if (!object.type.type().IsAssumedType()) { 701 messages.Say( 702 "Assumed-type '%s' may be associated only with an assumed-type %s"_err_en_US, 703 assumed.name(), dummyName); 704 } else if (object.type.attrs().test(evaluate::characteristics:: 705 TypeAndShape::Attr::AssumedRank) && 706 !IsAssumedShape(assumed) && 707 !evaluate::IsAssumedRank(assumed)) { 708 messages.Say( // C711 709 "Assumed-type '%s' must be either assumed shape or assumed rank to be associated with assumed rank %s"_err_en_US, 710 assumed.name(), dummyName); 711 } 712 } 713 }, 714 [&](const characteristics::DummyProcedure &dummy) { 715 CheckProcedureArg(arg, proc, dummy, dummyName, context); 716 }, 717 [&](const characteristics::AlternateReturn &) { 718 // All semantic checking is done elsewhere 719 }, 720 }, 721 dummy.u); 722 } 723 724 static void RearrangeArguments(const characteristics::Procedure &proc, 725 evaluate::ActualArguments &actuals, parser::ContextualMessages &messages) { 726 CHECK(proc.HasExplicitInterface()); 727 if (actuals.size() < proc.dummyArguments.size()) { 728 actuals.resize(proc.dummyArguments.size()); 729 } else if (actuals.size() > proc.dummyArguments.size()) { 730 messages.Say( 731 "Too many actual arguments (%zd) passed to procedure that expects only %zd"_err_en_US, 732 actuals.size(), proc.dummyArguments.size()); 733 } 734 std::map<std::string, evaluate::ActualArgument> kwArgs; 735 for (auto &x : actuals) { 736 if (x && x->keyword()) { 737 auto emplaced{ 738 kwArgs.try_emplace(x->keyword()->ToString(), std::move(*x))}; 739 if (!emplaced.second) { 740 messages.Say(*x->keyword(), 741 "Argument keyword '%s=' appears on more than one effective argument in this procedure reference"_err_en_US, 742 *x->keyword()); 743 } 744 x.reset(); 745 } 746 } 747 if (!kwArgs.empty()) { 748 int index{0}; 749 for (const auto &dummy : proc.dummyArguments) { 750 if (!dummy.name.empty()) { 751 auto iter{kwArgs.find(dummy.name)}; 752 if (iter != kwArgs.end()) { 753 evaluate::ActualArgument &x{iter->second}; 754 if (actuals[index]) { 755 messages.Say(*x.keyword(), 756 "Keyword argument '%s=' has already been specified positionally (#%d) in this procedure reference"_err_en_US, 757 *x.keyword(), index + 1); 758 } else { 759 actuals[index] = std::move(x); 760 } 761 kwArgs.erase(iter); 762 } 763 } 764 ++index; 765 } 766 for (auto &bad : kwArgs) { 767 evaluate::ActualArgument &x{bad.second}; 768 messages.Say(*x.keyword(), 769 "Argument keyword '%s=' is not recognized for this procedure reference"_err_en_US, 770 *x.keyword()); 771 } 772 } 773 } 774 775 // The actual argument arrays to an ELEMENTAL procedure must conform. 776 static bool CheckElementalConformance(parser::ContextualMessages &messages, 777 const characteristics::Procedure &proc, evaluate::ActualArguments &actuals, 778 evaluate::FoldingContext &context) { 779 std::optional<evaluate::Shape> shape; 780 std::string shapeName; 781 int index{0}; 782 for (const auto &arg : actuals) { 783 const auto &dummy{proc.dummyArguments.at(index++)}; 784 if (arg) { 785 if (const auto *expr{arg->UnwrapExpr()}) { 786 if (auto argShape{evaluate::GetShape(context, *expr)}) { 787 if (GetRank(*argShape) > 0) { 788 std::string argName{"actual argument ("s + expr->AsFortran() + 789 ") corresponding to dummy argument #" + std::to_string(index) + 790 " ('" + dummy.name + "')"}; 791 if (shape) { 792 auto tristate{evaluate::CheckConformance(messages, *shape, 793 *argShape, evaluate::CheckConformanceFlags::None, 794 shapeName.c_str(), argName.c_str())}; 795 if (tristate && !*tristate) { 796 return false; 797 } 798 } else { 799 shape = std::move(argShape); 800 shapeName = argName; 801 } 802 } 803 } 804 } 805 } 806 } 807 return true; 808 } 809 810 static parser::Messages CheckExplicitInterface( 811 const characteristics::Procedure &proc, evaluate::ActualArguments &actuals, 812 const evaluate::FoldingContext &context, const Scope *scope, 813 const evaluate::SpecificIntrinsic *intrinsic, 814 bool allowIntegerConversions) { 815 parser::Messages buffer; 816 parser::ContextualMessages messages{context.messages().at(), &buffer}; 817 RearrangeArguments(proc, actuals, messages); 818 if (buffer.empty()) { 819 int index{0}; 820 evaluate::FoldingContext localContext{context, messages}; 821 for (auto &actual : actuals) { 822 const auto &dummy{proc.dummyArguments.at(index++)}; 823 if (actual) { 824 CheckExplicitInterfaceArg(*actual, dummy, proc, localContext, scope, 825 intrinsic, allowIntegerConversions); 826 } else if (!dummy.IsOptional()) { 827 if (dummy.name.empty()) { 828 messages.Say( 829 "Dummy argument #%d is not OPTIONAL and is not associated with " 830 "an actual argument in this procedure reference"_err_en_US, 831 index); 832 } else { 833 messages.Say("Dummy argument '%s=' (#%d) is not OPTIONAL and is not " 834 "associated with an actual argument in this procedure " 835 "reference"_err_en_US, 836 dummy.name, index); 837 } 838 } 839 } 840 if (proc.IsElemental() && !buffer.AnyFatalError()) { 841 CheckElementalConformance(messages, proc, actuals, localContext); 842 } 843 } 844 return buffer; 845 } 846 847 parser::Messages CheckExplicitInterface(const characteristics::Procedure &proc, 848 evaluate::ActualArguments &actuals, const evaluate::FoldingContext &context, 849 const Scope &scope, const evaluate::SpecificIntrinsic *intrinsic) { 850 return CheckExplicitInterface( 851 proc, actuals, context, &scope, intrinsic, true); 852 } 853 854 bool CheckInterfaceForGeneric(const characteristics::Procedure &proc, 855 evaluate::ActualArguments &actuals, const evaluate::FoldingContext &context, 856 bool allowIntegerConversions) { 857 return !CheckExplicitInterface( 858 proc, actuals, context, nullptr, nullptr, allowIntegerConversions) 859 .AnyFatalError(); 860 } 861 862 void CheckArguments(const characteristics::Procedure &proc, 863 evaluate::ActualArguments &actuals, evaluate::FoldingContext &context, 864 const Scope &scope, bool treatingExternalAsImplicit, 865 const evaluate::SpecificIntrinsic *intrinsic) { 866 bool explicitInterface{proc.HasExplicitInterface()}; 867 parser::ContextualMessages &messages{context.messages()}; 868 if (!explicitInterface || treatingExternalAsImplicit) { 869 parser::Messages buffer; 870 { 871 auto restorer{messages.SetMessages(buffer)}; 872 for (auto &actual : actuals) { 873 if (actual) { 874 CheckImplicitInterfaceArg(*actual, messages); 875 } 876 } 877 } 878 if (!buffer.empty()) { 879 if (auto *msgs{messages.messages()}) { 880 msgs->Annex(std::move(buffer)); 881 } 882 return; // don't pile on 883 } 884 } 885 if (explicitInterface) { 886 auto buffer{ 887 CheckExplicitInterface(proc, actuals, context, scope, intrinsic)}; 888 if (treatingExternalAsImplicit && !buffer.empty()) { 889 if (auto *msg{messages.Say( 890 "If the procedure's interface were explicit, this reference would be in error:"_warn_en_US)}) { 891 buffer.AttachTo(*msg); 892 } 893 } 894 if (auto *msgs{messages.messages()}) { 895 msgs->Annex(std::move(buffer)); 896 } 897 } 898 } 899 } // namespace Fortran::semantics 900