1 //===-- lib/Semantics/resolve-names-utils.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 "resolve-names-utils.h" 10 #include "flang/Common/Fortran-features.h" 11 #include "flang/Common/Fortran.h" 12 #include "flang/Common/idioms.h" 13 #include "flang/Common/indirection.h" 14 #include "flang/Evaluate/fold.h" 15 #include "flang/Evaluate/tools.h" 16 #include "flang/Evaluate/traverse.h" 17 #include "flang/Evaluate/type.h" 18 #include "flang/Parser/char-block.h" 19 #include "flang/Parser/parse-tree.h" 20 #include "flang/Semantics/expression.h" 21 #include "flang/Semantics/semantics.h" 22 #include "flang/Semantics/tools.h" 23 #include <initializer_list> 24 #include <variant> 25 26 namespace Fortran::semantics { 27 28 using common::LanguageFeature; 29 using common::LogicalOperator; 30 using common::NumericOperator; 31 using common::RelationalOperator; 32 using IntrinsicOperator = parser::DefinedOperator::IntrinsicOperator; 33 34 static constexpr const char *operatorPrefix{"operator("}; 35 36 static GenericKind MapIntrinsicOperator(IntrinsicOperator); 37 38 Symbol *Resolve(const parser::Name &name, Symbol *symbol) { 39 if (symbol && !name.symbol) { 40 name.symbol = symbol; 41 } 42 return symbol; 43 } 44 Symbol &Resolve(const parser::Name &name, Symbol &symbol) { 45 return *Resolve(name, &symbol); 46 } 47 48 parser::MessageFixedText WithSeverity( 49 const parser::MessageFixedText &msg, parser::Severity severity) { 50 return parser::MessageFixedText{ 51 msg.text().begin(), msg.text().size(), severity}; 52 } 53 54 bool IsIntrinsicOperator( 55 const SemanticsContext &context, const SourceName &name) { 56 std::string str{name.ToString()}; 57 for (int i{0}; i != common::LogicalOperator_enumSize; ++i) { 58 auto names{context.languageFeatures().GetNames(LogicalOperator{i})}; 59 if (llvm::is_contained(names, str)) { 60 return true; 61 } 62 } 63 for (int i{0}; i != common::RelationalOperator_enumSize; ++i) { 64 auto names{context.languageFeatures().GetNames(RelationalOperator{i})}; 65 if (llvm::is_contained(names, str)) { 66 return true; 67 } 68 } 69 return false; 70 } 71 72 template <typename E> 73 std::forward_list<std::string> GetOperatorNames( 74 const SemanticsContext &context, E opr) { 75 std::forward_list<std::string> result; 76 for (const char *name : context.languageFeatures().GetNames(opr)) { 77 result.emplace_front(std::string{operatorPrefix} + name + ')'); 78 } 79 return result; 80 } 81 82 std::forward_list<std::string> GetAllNames( 83 const SemanticsContext &context, const SourceName &name) { 84 std::string str{name.ToString()}; 85 if (!name.empty() && name.end()[-1] == ')' && 86 name.ToString().rfind(std::string{operatorPrefix}, 0) == 0) { 87 for (int i{0}; i != common::LogicalOperator_enumSize; ++i) { 88 auto names{GetOperatorNames(context, LogicalOperator{i})}; 89 if (llvm::is_contained(names, str)) { 90 return names; 91 } 92 } 93 for (int i{0}; i != common::RelationalOperator_enumSize; ++i) { 94 auto names{GetOperatorNames(context, RelationalOperator{i})}; 95 if (llvm::is_contained(names, str)) { 96 return names; 97 } 98 } 99 } 100 return {str}; 101 } 102 103 bool IsLogicalConstant( 104 const SemanticsContext &context, const SourceName &name) { 105 std::string str{name.ToString()}; 106 return str == ".true." || str == ".false." || 107 (context.IsEnabled(LanguageFeature::LogicalAbbreviations) && 108 (str == ".t" || str == ".f.")); 109 } 110 111 void GenericSpecInfo::Resolve(Symbol *symbol) const { 112 if (symbol) { 113 if (auto *details{symbol->detailsIf<GenericDetails>()}) { 114 details->set_kind(kind_); 115 } 116 if (parseName_) { 117 semantics::Resolve(*parseName_, symbol); 118 } 119 } 120 } 121 122 void GenericSpecInfo::Analyze(const parser::DefinedOpName &name) { 123 kind_ = GenericKind::OtherKind::DefinedOp; 124 parseName_ = &name.v; 125 symbolName_ = name.v.source; 126 } 127 128 void GenericSpecInfo::Analyze(const parser::GenericSpec &x) { 129 symbolName_ = x.source; 130 kind_ = common::visit( 131 common::visitors{ 132 [&](const parser::Name &y) -> GenericKind { 133 parseName_ = &y; 134 symbolName_ = y.source; 135 return GenericKind::OtherKind::Name; 136 }, 137 [&](const parser::DefinedOperator &y) { 138 return common::visit( 139 common::visitors{ 140 [&](const parser::DefinedOpName &z) -> GenericKind { 141 Analyze(z); 142 return GenericKind::OtherKind::DefinedOp; 143 }, 144 [&](const IntrinsicOperator &z) { 145 return MapIntrinsicOperator(z); 146 }, 147 }, 148 y.u); 149 }, 150 [&](const parser::GenericSpec::Assignment &) -> GenericKind { 151 return GenericKind::OtherKind::Assignment; 152 }, 153 [&](const parser::GenericSpec::ReadFormatted &) -> GenericKind { 154 return common::DefinedIo::ReadFormatted; 155 }, 156 [&](const parser::GenericSpec::ReadUnformatted &) -> GenericKind { 157 return common::DefinedIo::ReadUnformatted; 158 }, 159 [&](const parser::GenericSpec::WriteFormatted &) -> GenericKind { 160 return common::DefinedIo::WriteFormatted; 161 }, 162 [&](const parser::GenericSpec::WriteUnformatted &) -> GenericKind { 163 return common::DefinedIo::WriteUnformatted; 164 }, 165 }, 166 x.u); 167 } 168 169 llvm::raw_ostream &operator<<( 170 llvm::raw_ostream &os, const GenericSpecInfo &info) { 171 os << "GenericSpecInfo: kind=" << info.kind_.ToString(); 172 os << " parseName=" 173 << (info.parseName_ ? info.parseName_->ToString() : "null"); 174 os << " symbolName=" 175 << (info.symbolName_ ? info.symbolName_->ToString() : "null"); 176 return os; 177 } 178 179 // parser::DefinedOperator::IntrinsicOperator -> GenericKind 180 static GenericKind MapIntrinsicOperator(IntrinsicOperator op) { 181 switch (op) { 182 SWITCH_COVERS_ALL_CASES 183 case IntrinsicOperator::Concat: 184 return GenericKind::OtherKind::Concat; 185 case IntrinsicOperator::Power: 186 return NumericOperator::Power; 187 case IntrinsicOperator::Multiply: 188 return NumericOperator::Multiply; 189 case IntrinsicOperator::Divide: 190 return NumericOperator::Divide; 191 case IntrinsicOperator::Add: 192 return NumericOperator::Add; 193 case IntrinsicOperator::Subtract: 194 return NumericOperator::Subtract; 195 case IntrinsicOperator::AND: 196 return LogicalOperator::And; 197 case IntrinsicOperator::OR: 198 return LogicalOperator::Or; 199 case IntrinsicOperator::EQV: 200 return LogicalOperator::Eqv; 201 case IntrinsicOperator::NEQV: 202 return LogicalOperator::Neqv; 203 case IntrinsicOperator::NOT: 204 return LogicalOperator::Not; 205 case IntrinsicOperator::LT: 206 return RelationalOperator::LT; 207 case IntrinsicOperator::LE: 208 return RelationalOperator::LE; 209 case IntrinsicOperator::EQ: 210 return RelationalOperator::EQ; 211 case IntrinsicOperator::NE: 212 return RelationalOperator::NE; 213 case IntrinsicOperator::GE: 214 return RelationalOperator::GE; 215 case IntrinsicOperator::GT: 216 return RelationalOperator::GT; 217 } 218 } 219 220 class ArraySpecAnalyzer { 221 public: 222 ArraySpecAnalyzer(SemanticsContext &context) : context_{context} {} 223 ArraySpec Analyze(const parser::ArraySpec &); 224 ArraySpec AnalyzeDeferredShapeSpecList(const parser::DeferredShapeSpecList &); 225 ArraySpec Analyze(const parser::ComponentArraySpec &); 226 ArraySpec Analyze(const parser::CoarraySpec &); 227 228 private: 229 SemanticsContext &context_; 230 ArraySpec arraySpec_; 231 232 template <typename T> void Analyze(const std::list<T> &list) { 233 for (const auto &elem : list) { 234 Analyze(elem); 235 } 236 } 237 void Analyze(const parser::AssumedShapeSpec &); 238 void Analyze(const parser::ExplicitShapeSpec &); 239 void Analyze(const parser::AssumedImpliedSpec &); 240 void Analyze(const parser::DeferredShapeSpecList &); 241 void Analyze(const parser::AssumedRankSpec &); 242 void MakeExplicit(const std::optional<parser::SpecificationExpr> &, 243 const parser::SpecificationExpr &); 244 void MakeImplied(const std::optional<parser::SpecificationExpr> &); 245 void MakeDeferred(int); 246 Bound GetBound(const std::optional<parser::SpecificationExpr> &); 247 Bound GetBound(const parser::SpecificationExpr &); 248 }; 249 250 ArraySpec AnalyzeArraySpec( 251 SemanticsContext &context, const parser::ArraySpec &arraySpec) { 252 return ArraySpecAnalyzer{context}.Analyze(arraySpec); 253 } 254 ArraySpec AnalyzeArraySpec( 255 SemanticsContext &context, const parser::ComponentArraySpec &arraySpec) { 256 return ArraySpecAnalyzer{context}.Analyze(arraySpec); 257 } 258 ArraySpec AnalyzeDeferredShapeSpecList(SemanticsContext &context, 259 const parser::DeferredShapeSpecList &deferredShapeSpecs) { 260 return ArraySpecAnalyzer{context}.AnalyzeDeferredShapeSpecList( 261 deferredShapeSpecs); 262 } 263 ArraySpec AnalyzeCoarraySpec( 264 SemanticsContext &context, const parser::CoarraySpec &coarraySpec) { 265 return ArraySpecAnalyzer{context}.Analyze(coarraySpec); 266 } 267 268 ArraySpec ArraySpecAnalyzer::Analyze(const parser::ComponentArraySpec &x) { 269 common::visit([this](const auto &y) { Analyze(y); }, x.u); 270 CHECK(!arraySpec_.empty()); 271 return arraySpec_; 272 } 273 ArraySpec ArraySpecAnalyzer::Analyze(const parser::ArraySpec &x) { 274 common::visit(common::visitors{ 275 [&](const parser::AssumedSizeSpec &y) { 276 Analyze( 277 std::get<std::list<parser::ExplicitShapeSpec>>(y.t)); 278 Analyze(std::get<parser::AssumedImpliedSpec>(y.t)); 279 }, 280 [&](const parser::ImpliedShapeSpec &y) { Analyze(y.v); }, 281 [&](const auto &y) { Analyze(y); }, 282 }, 283 x.u); 284 CHECK(!arraySpec_.empty()); 285 return arraySpec_; 286 } 287 ArraySpec ArraySpecAnalyzer::AnalyzeDeferredShapeSpecList( 288 const parser::DeferredShapeSpecList &x) { 289 Analyze(x); 290 CHECK(!arraySpec_.empty()); 291 return arraySpec_; 292 } 293 ArraySpec ArraySpecAnalyzer::Analyze(const parser::CoarraySpec &x) { 294 common::visit( 295 common::visitors{ 296 [&](const parser::DeferredCoshapeSpecList &y) { MakeDeferred(y.v); }, 297 [&](const parser::ExplicitCoshapeSpec &y) { 298 Analyze(std::get<std::list<parser::ExplicitShapeSpec>>(y.t)); 299 MakeImplied( 300 std::get<std::optional<parser::SpecificationExpr>>(y.t)); 301 }, 302 }, 303 x.u); 304 CHECK(!arraySpec_.empty()); 305 return arraySpec_; 306 } 307 308 void ArraySpecAnalyzer::Analyze(const parser::AssumedShapeSpec &x) { 309 arraySpec_.push_back(ShapeSpec::MakeAssumedShape(GetBound(x.v))); 310 } 311 void ArraySpecAnalyzer::Analyze(const parser::ExplicitShapeSpec &x) { 312 MakeExplicit(std::get<std::optional<parser::SpecificationExpr>>(x.t), 313 std::get<parser::SpecificationExpr>(x.t)); 314 } 315 void ArraySpecAnalyzer::Analyze(const parser::AssumedImpliedSpec &x) { 316 MakeImplied(x.v); 317 } 318 void ArraySpecAnalyzer::Analyze(const parser::DeferredShapeSpecList &x) { 319 MakeDeferred(x.v); 320 } 321 void ArraySpecAnalyzer::Analyze(const parser::AssumedRankSpec &) { 322 arraySpec_.push_back(ShapeSpec::MakeAssumedRank()); 323 } 324 325 void ArraySpecAnalyzer::MakeExplicit( 326 const std::optional<parser::SpecificationExpr> &lb, 327 const parser::SpecificationExpr &ub) { 328 arraySpec_.push_back(ShapeSpec::MakeExplicit(GetBound(lb), GetBound(ub))); 329 } 330 void ArraySpecAnalyzer::MakeImplied( 331 const std::optional<parser::SpecificationExpr> &lb) { 332 arraySpec_.push_back(ShapeSpec::MakeImplied(GetBound(lb))); 333 } 334 void ArraySpecAnalyzer::MakeDeferred(int n) { 335 for (int i = 0; i < n; ++i) { 336 arraySpec_.push_back(ShapeSpec::MakeDeferred()); 337 } 338 } 339 340 Bound ArraySpecAnalyzer::GetBound( 341 const std::optional<parser::SpecificationExpr> &x) { 342 return x ? GetBound(*x) : Bound{1}; 343 } 344 Bound ArraySpecAnalyzer::GetBound(const parser::SpecificationExpr &x) { 345 MaybeSubscriptIntExpr expr; 346 if (MaybeExpr maybeExpr{AnalyzeExpr(context_, x.v)}) { 347 if (auto *intExpr{evaluate::UnwrapExpr<SomeIntExpr>(*maybeExpr)}) { 348 expr = evaluate::Fold(context_.foldingContext(), 349 evaluate::ConvertToType<evaluate::SubscriptInteger>( 350 std::move(*intExpr))); 351 } 352 } 353 return Bound{std::move(expr)}; 354 } 355 356 // If src is SAVE (explicitly or implicitly), 357 // set SAVE attribute on all members of dst. 358 static void PropagateSaveAttr( 359 const EquivalenceObject &src, EquivalenceSet &dst) { 360 if (IsSaved(src.symbol)) { 361 for (auto &obj : dst) { 362 if (!obj.symbol.attrs().test(Attr::SAVE)) { 363 obj.symbol.attrs().set(Attr::SAVE); 364 // If the other equivalenced symbol itself is not SAVE, 365 // then adding SAVE here implies that it has to be implicit. 366 obj.symbol.implicitAttrs().set(Attr::SAVE); 367 } 368 } 369 } 370 } 371 static void PropagateSaveAttr(const EquivalenceSet &src, EquivalenceSet &dst) { 372 if (!src.empty()) { 373 PropagateSaveAttr(src.front(), dst); 374 } 375 } 376 377 void EquivalenceSets::AddToSet(const parser::Designator &designator) { 378 if (CheckDesignator(designator)) { 379 Symbol &symbol{*currObject_.symbol}; 380 if (!currSet_.empty()) { 381 // check this symbol against first of set for compatibility 382 Symbol &first{currSet_.front().symbol}; 383 CheckCanEquivalence(designator.source, first, symbol) && 384 CheckCanEquivalence(designator.source, symbol, first); 385 } 386 auto subscripts{currObject_.subscripts}; 387 if (subscripts.empty() && symbol.IsObjectArray()) { 388 // record a whole array as its first element 389 for (const ShapeSpec &spec : symbol.get<ObjectEntityDetails>().shape()) { 390 auto &lbound{spec.lbound().GetExplicit().value()}; 391 subscripts.push_back(evaluate::ToInt64(lbound).value()); 392 } 393 } 394 auto substringStart{currObject_.substringStart}; 395 currSet_.emplace_back( 396 symbol, subscripts, substringStart, designator.source); 397 PropagateSaveAttr(currSet_.back(), currSet_); 398 } 399 currObject_ = {}; 400 } 401 402 void EquivalenceSets::FinishSet(const parser::CharBlock &source) { 403 std::set<std::size_t> existing; // indices of sets intersecting this one 404 for (auto &obj : currSet_) { 405 auto it{objectToSet_.find(obj)}; 406 if (it != objectToSet_.end()) { 407 existing.insert(it->second); // symbol already in this set 408 } 409 } 410 if (existing.empty()) { 411 sets_.push_back({}); // create a new equivalence set 412 MergeInto(source, currSet_, sets_.size() - 1); 413 } else { 414 auto it{existing.begin()}; 415 std::size_t dstIndex{*it}; 416 MergeInto(source, currSet_, dstIndex); 417 while (++it != existing.end()) { 418 MergeInto(source, sets_[*it], dstIndex); 419 } 420 } 421 currSet_.clear(); 422 } 423 424 // Report an error or warning if sym1 and sym2 cannot be in the same equivalence 425 // set. 426 bool EquivalenceSets::CheckCanEquivalence( 427 const parser::CharBlock &source, const Symbol &sym1, const Symbol &sym2) { 428 std::optional<parser::MessageFixedText> msg; 429 const DeclTypeSpec *type1{sym1.GetType()}; 430 const DeclTypeSpec *type2{sym2.GetType()}; 431 bool isDefaultNum1{IsDefaultNumericSequenceType(type1)}; 432 bool isAnyNum1{IsAnyNumericSequenceType(type1)}; 433 bool isDefaultNum2{IsDefaultNumericSequenceType(type2)}; 434 bool isAnyNum2{IsAnyNumericSequenceType(type2)}; 435 bool isChar1{IsCharacterSequenceType(type1)}; 436 bool isChar2{IsCharacterSequenceType(type2)}; 437 if (sym1.attrs().test(Attr::PROTECTED) && 438 !sym2.attrs().test(Attr::PROTECTED)) { // C8114 439 msg = "Equivalence set cannot contain '%s'" 440 " with PROTECTED attribute and '%s' without"_err_en_US; 441 } else if ((isDefaultNum1 && isDefaultNum2) || (isChar1 && isChar2)) { 442 // ok & standard conforming 443 } else if (!(isAnyNum1 || isChar1) && 444 !(isAnyNum2 || isChar2)) { // C8110 - C8113 445 if (AreTkCompatibleTypes(type1, type2)) { 446 if (context_.ShouldWarn(LanguageFeature::EquivalenceSameNonSequence)) { 447 msg = 448 "nonstandard: Equivalence set contains '%s' and '%s' with same " 449 "type that is neither numeric nor character sequence type"_port_en_US; 450 } 451 } else { 452 msg = "Equivalence set cannot contain '%s' and '%s' with distinct types " 453 "that are not both numeric or character sequence types"_err_en_US; 454 } 455 } else if (isAnyNum1) { 456 if (isChar2) { 457 if (context_.ShouldWarn( 458 LanguageFeature::EquivalenceNumericWithCharacter)) { 459 msg = "nonstandard: Equivalence set contains '%s' that is numeric " 460 "sequence type and '%s' that is character"_port_en_US; 461 } 462 } else if (isAnyNum2 && 463 context_.ShouldWarn(LanguageFeature::EquivalenceNonDefaultNumeric)) { 464 if (isDefaultNum1) { 465 msg = 466 "nonstandard: Equivalence set contains '%s' that is a default " 467 "numeric sequence type and '%s' that is numeric with non-default kind"_port_en_US; 468 } else if (!isDefaultNum2) { 469 msg = "nonstandard: Equivalence set contains '%s' and '%s' that are " 470 "numeric sequence types with non-default kinds"_port_en_US; 471 } 472 } 473 } 474 if (msg && 475 (!context_.IsInModuleFile(source) || 476 msg->severity() == parser::Severity::Error)) { 477 context_.Say(source, std::move(*msg), sym1.name(), sym2.name()); 478 return false; 479 } 480 return true; 481 } 482 483 // Move objects from src to sets_[dstIndex] 484 void EquivalenceSets::MergeInto(const parser::CharBlock &source, 485 EquivalenceSet &src, std::size_t dstIndex) { 486 EquivalenceSet &dst{sets_[dstIndex]}; 487 PropagateSaveAttr(dst, src); 488 for (const auto &obj : src) { 489 dst.push_back(obj); 490 objectToSet_[obj] = dstIndex; 491 } 492 PropagateSaveAttr(src, dst); 493 src.clear(); 494 } 495 496 // If set has an object with this symbol, return it. 497 const EquivalenceObject *EquivalenceSets::Find( 498 const EquivalenceSet &set, const Symbol &symbol) { 499 for (const auto &obj : set) { 500 if (obj.symbol == symbol) { 501 return &obj; 502 } 503 } 504 return nullptr; 505 } 506 507 bool EquivalenceSets::CheckDesignator(const parser::Designator &designator) { 508 return common::visit( 509 common::visitors{ 510 [&](const parser::DataRef &x) { 511 return CheckDataRef(designator.source, x); 512 }, 513 [&](const parser::Substring &x) { 514 const auto &dataRef{std::get<parser::DataRef>(x.t)}; 515 const auto &range{std::get<parser::SubstringRange>(x.t)}; 516 bool ok{CheckDataRef(designator.source, dataRef)}; 517 if (const auto &lb{std::get<0>(range.t)}) { 518 ok &= CheckSubstringBound(lb->thing.thing.value(), true); 519 } else { 520 currObject_.substringStart = 1; 521 } 522 if (const auto &ub{std::get<1>(range.t)}) { 523 ok &= CheckSubstringBound(ub->thing.thing.value(), false); 524 } 525 return ok; 526 }, 527 }, 528 designator.u); 529 } 530 531 bool EquivalenceSets::CheckDataRef( 532 const parser::CharBlock &source, const parser::DataRef &x) { 533 return common::visit( 534 common::visitors{ 535 [&](const parser::Name &name) { return CheckObject(name); }, 536 [&](const common::Indirection<parser::StructureComponent> &) { 537 context_.Say(source, // C8107 538 "Derived type component '%s' is not allowed in an equivalence set"_err_en_US, 539 source); 540 return false; 541 }, 542 [&](const common::Indirection<parser::ArrayElement> &elem) { 543 bool ok{CheckDataRef(source, elem.value().base)}; 544 for (const auto &subscript : elem.value().subscripts) { 545 ok &= common::visit( 546 common::visitors{ 547 [&](const parser::SubscriptTriplet &) { 548 context_.Say(source, // C924, R872 549 "Array section '%s' is not allowed in an equivalence set"_err_en_US, 550 source); 551 return false; 552 }, 553 [&](const parser::IntExpr &y) { 554 return CheckArrayBound(y.thing.value()); 555 }, 556 }, 557 subscript.u); 558 } 559 return ok; 560 }, 561 [&](const common::Indirection<parser::CoindexedNamedObject> &) { 562 context_.Say(source, // C924 (R872) 563 "Coindexed object '%s' is not allowed in an equivalence set"_err_en_US, 564 source); 565 return false; 566 }, 567 }, 568 x.u); 569 } 570 571 bool EquivalenceSets::CheckObject(const parser::Name &name) { 572 currObject_.symbol = name.symbol; 573 return currObject_.symbol != nullptr; 574 } 575 576 bool EquivalenceSets::CheckArrayBound(const parser::Expr &bound) { 577 MaybeExpr expr{ 578 evaluate::Fold(context_.foldingContext(), AnalyzeExpr(context_, bound))}; 579 if (!expr) { 580 return false; 581 } 582 if (expr->Rank() > 0) { 583 context_.Say(bound.source, // C924, R872 584 "Array with vector subscript '%s' is not allowed in an equivalence set"_err_en_US, 585 bound.source); 586 return false; 587 } 588 auto subscript{evaluate::ToInt64(*expr)}; 589 if (!subscript) { 590 context_.Say(bound.source, // C8109 591 "Array with nonconstant subscript '%s' is not allowed in an equivalence set"_err_en_US, 592 bound.source); 593 return false; 594 } 595 currObject_.subscripts.push_back(*subscript); 596 return true; 597 } 598 599 bool EquivalenceSets::CheckSubstringBound( 600 const parser::Expr &bound, bool isStart) { 601 MaybeExpr expr{ 602 evaluate::Fold(context_.foldingContext(), AnalyzeExpr(context_, bound))}; 603 if (!expr) { 604 return false; 605 } 606 auto subscript{evaluate::ToInt64(*expr)}; 607 if (!subscript) { 608 context_.Say(bound.source, // C8109 609 "Substring with nonconstant bound '%s' is not allowed in an equivalence set"_err_en_US, 610 bound.source); 611 return false; 612 } 613 if (!isStart) { 614 auto start{currObject_.substringStart}; 615 if (*subscript < (start ? *start : 1)) { 616 context_.Say(bound.source, // C8116 617 "Substring with zero length is not allowed in an equivalence set"_err_en_US); 618 return false; 619 } 620 } else if (*subscript != 1) { 621 currObject_.substringStart = *subscript; 622 } 623 return true; 624 } 625 626 bool EquivalenceSets::IsCharacterSequenceType(const DeclTypeSpec *type) { 627 return IsSequenceType(type, [&](const IntrinsicTypeSpec &type) { 628 auto kind{evaluate::ToInt64(type.kind())}; 629 return type.category() == TypeCategory::Character && kind && 630 kind.value() == context_.GetDefaultKind(TypeCategory::Character); 631 }); 632 } 633 634 // Numeric or logical type of default kind or DOUBLE PRECISION or DOUBLE COMPLEX 635 bool EquivalenceSets::IsDefaultKindNumericType(const IntrinsicTypeSpec &type) { 636 if (auto kind{evaluate::ToInt64(type.kind())}) { 637 switch (type.category()) { 638 case TypeCategory::Integer: 639 case TypeCategory::Logical: 640 return *kind == context_.GetDefaultKind(TypeCategory::Integer); 641 case TypeCategory::Real: 642 case TypeCategory::Complex: 643 return *kind == context_.GetDefaultKind(TypeCategory::Real) || 644 *kind == context_.doublePrecisionKind(); 645 default: 646 return false; 647 } 648 } 649 return false; 650 } 651 652 bool EquivalenceSets::IsDefaultNumericSequenceType(const DeclTypeSpec *type) { 653 return IsSequenceType(type, [&](const IntrinsicTypeSpec &type) { 654 return IsDefaultKindNumericType(type); 655 }); 656 } 657 658 bool EquivalenceSets::IsAnyNumericSequenceType(const DeclTypeSpec *type) { 659 return IsSequenceType(type, [&](const IntrinsicTypeSpec &type) { 660 return type.category() == TypeCategory::Logical || 661 common::IsNumericTypeCategory(type.category()); 662 }); 663 } 664 665 // Is type an intrinsic type that satisfies predicate or a sequence type 666 // whose components do. 667 bool EquivalenceSets::IsSequenceType(const DeclTypeSpec *type, 668 std::function<bool(const IntrinsicTypeSpec &)> predicate) { 669 if (!type) { 670 return false; 671 } else if (const IntrinsicTypeSpec * intrinsic{type->AsIntrinsic()}) { 672 return predicate(*intrinsic); 673 } else if (const DerivedTypeSpec * derived{type->AsDerived()}) { 674 for (const auto &pair : *derived->typeSymbol().scope()) { 675 const Symbol &component{*pair.second}; 676 if (IsAllocatableOrPointer(component) || 677 !IsSequenceType(component.GetType(), predicate)) { 678 return false; 679 } 680 } 681 return true; 682 } else { 683 return false; 684 } 685 } 686 687 // MapSubprogramToNewSymbols() relies on the following recursive symbol/scope 688 // copying infrastructure to duplicate an interface's symbols and map all 689 // of the symbol references in their contained expressions and interfaces 690 // to the new symbols. 691 692 struct SymbolAndTypeMappings { 693 std::map<const Symbol *, const Symbol *> symbolMap; 694 std::map<const DeclTypeSpec *, const DeclTypeSpec *> typeMap; 695 }; 696 697 class SymbolMapper : public evaluate::AnyTraverse<SymbolMapper, bool> { 698 public: 699 using Base = evaluate::AnyTraverse<SymbolMapper, bool>; 700 SymbolMapper(Scope &scope, SymbolAndTypeMappings &map) 701 : Base{*this}, scope_{scope}, map_{map} {} 702 using Base::operator(); 703 bool operator()(const SymbolRef &ref) const { 704 if (const Symbol *mapped{MapSymbol(*ref)}) { 705 const_cast<SymbolRef &>(ref) = *mapped; 706 } 707 return false; 708 } 709 bool operator()(const Symbol &x) const { 710 if (MapSymbol(x)) { 711 DIE("SymbolMapper hit symbol outside SymbolRef"); 712 } 713 return false; 714 } 715 void MapSymbolExprs(Symbol &); 716 Symbol *CopySymbol(const Symbol *); 717 718 private: 719 void MapParamValue(ParamValue ¶m) const { (*this)(param.GetExplicit()); } 720 void MapBound(Bound &bound) const { (*this)(bound.GetExplicit()); } 721 void MapShapeSpec(ShapeSpec &spec) const { 722 MapBound(spec.lbound()); 723 MapBound(spec.ubound()); 724 } 725 const Symbol *MapSymbol(const Symbol &) const; 726 const Symbol *MapSymbol(const Symbol *) const; 727 const DeclTypeSpec *MapType(const DeclTypeSpec &); 728 const DeclTypeSpec *MapType(const DeclTypeSpec *); 729 const Symbol *MapInterface(const Symbol *); 730 731 Scope &scope_; 732 SymbolAndTypeMappings &map_; 733 }; 734 735 Symbol *SymbolMapper::CopySymbol(const Symbol *symbol) { 736 if (symbol) { 737 if (auto *subp{symbol->detailsIf<SubprogramDetails>()}) { 738 if (subp->isInterface()) { 739 if (auto pair{scope_.try_emplace(symbol->name(), symbol->attrs())}; 740 pair.second) { 741 Symbol ©{*pair.first->second}; 742 map_.symbolMap[symbol] = © 743 copy.set(symbol->test(Symbol::Flag::Subroutine) 744 ? Symbol::Flag::Subroutine 745 : Symbol::Flag::Function); 746 Scope &newScope{scope_.MakeScope(Scope::Kind::Subprogram, ©)}; 747 copy.set_scope(&newScope); 748 copy.set_details(SubprogramDetails{}); 749 auto &newSubp{copy.get<SubprogramDetails>()}; 750 newSubp.set_isInterface(true); 751 newSubp.set_isDummy(subp->isDummy()); 752 newSubp.set_defaultIgnoreTKR(subp->defaultIgnoreTKR()); 753 MapSubprogramToNewSymbols(*symbol, copy, newScope, &map_); 754 return © 755 } 756 } 757 } else if (Symbol * copy{scope_.CopySymbol(*symbol)}) { 758 map_.symbolMap[symbol] = copy; 759 return copy; 760 } 761 } 762 return nullptr; 763 } 764 765 void SymbolMapper::MapSymbolExprs(Symbol &symbol) { 766 common::visit( 767 common::visitors{[&](ObjectEntityDetails &object) { 768 if (const DeclTypeSpec * type{object.type()}) { 769 if (const DeclTypeSpec * newType{MapType(*type)}) { 770 object.ReplaceType(*newType); 771 } 772 } 773 for (ShapeSpec &spec : object.shape()) { 774 MapShapeSpec(spec); 775 } 776 for (ShapeSpec &spec : object.coshape()) { 777 MapShapeSpec(spec); 778 } 779 }, 780 [&](ProcEntityDetails &proc) { 781 if (const Symbol * 782 mappedSymbol{MapInterface(proc.rawProcInterface())}) { 783 proc.set_procInterfaces( 784 *mappedSymbol, BypassGeneric(mappedSymbol->GetUltimate())); 785 } else if (const DeclTypeSpec * mappedType{MapType(proc.type())}) { 786 proc.set_type(*mappedType); 787 } 788 if (proc.init()) { 789 if (const Symbol * mapped{MapSymbol(*proc.init())}) { 790 proc.set_init(*mapped); 791 } 792 } 793 }, 794 [&](const HostAssocDetails &hostAssoc) { 795 if (const Symbol * mapped{MapSymbol(hostAssoc.symbol())}) { 796 symbol.set_details(HostAssocDetails{*mapped}); 797 } 798 }, 799 [](const auto &) {}}, 800 symbol.details()); 801 } 802 803 const Symbol *SymbolMapper::MapSymbol(const Symbol &symbol) const { 804 if (auto iter{map_.symbolMap.find(&symbol)}; iter != map_.symbolMap.end()) { 805 return iter->second; 806 } 807 return nullptr; 808 } 809 810 const Symbol *SymbolMapper::MapSymbol(const Symbol *symbol) const { 811 return symbol ? MapSymbol(*symbol) : nullptr; 812 } 813 814 const DeclTypeSpec *SymbolMapper::MapType(const DeclTypeSpec &type) { 815 if (auto iter{map_.typeMap.find(&type)}; iter != map_.typeMap.end()) { 816 return iter->second; 817 } 818 const DeclTypeSpec *newType{nullptr}; 819 if (type.category() == DeclTypeSpec::Category::Character) { 820 const CharacterTypeSpec &charType{type.characterTypeSpec()}; 821 if (charType.length().GetExplicit()) { 822 ParamValue newLen{charType.length()}; 823 (*this)(newLen.GetExplicit()); 824 newType = &scope_.MakeCharacterType( 825 std::move(newLen), KindExpr{charType.kind()}); 826 } 827 } else if (const DerivedTypeSpec *derived{type.AsDerived()}) { 828 if (!derived->parameters().empty()) { 829 DerivedTypeSpec newDerived{derived->name(), derived->typeSymbol()}; 830 newDerived.CookParameters(scope_.context().foldingContext()); 831 for (const auto &[paramName, paramValue] : derived->parameters()) { 832 ParamValue newParamValue{paramValue}; 833 MapParamValue(newParamValue); 834 newDerived.AddParamValue(paramName, std::move(newParamValue)); 835 } 836 // Scope::InstantiateDerivedTypes() instantiates it later. 837 newType = &scope_.MakeDerivedType(type.category(), std::move(newDerived)); 838 } 839 } 840 if (newType) { 841 map_.typeMap[&type] = newType; 842 } 843 return newType; 844 } 845 846 const DeclTypeSpec *SymbolMapper::MapType(const DeclTypeSpec *type) { 847 return type ? MapType(*type) : nullptr; 848 } 849 850 const Symbol *SymbolMapper::MapInterface(const Symbol *interface) { 851 if (const Symbol *mapped{MapSymbol(interface)}) { 852 return mapped; 853 } 854 if (interface) { 855 if (&interface->owner() != &scope_) { 856 return interface; 857 } else if (const auto *subp{interface->detailsIf<SubprogramDetails>()}; 858 subp && subp->isInterface()) { 859 return CopySymbol(interface); 860 } 861 } 862 return nullptr; 863 } 864 865 void MapSubprogramToNewSymbols(const Symbol &oldSymbol, Symbol &newSymbol, 866 Scope &newScope, SymbolAndTypeMappings *mappings) { 867 SymbolAndTypeMappings newMappings; 868 if (!mappings) { 869 mappings = &newMappings; 870 } 871 mappings->symbolMap[&oldSymbol] = &newSymbol; 872 const auto &oldDetails{oldSymbol.get<SubprogramDetails>()}; 873 auto &newDetails{newSymbol.get<SubprogramDetails>()}; 874 SymbolMapper mapper{newScope, *mappings}; 875 for (const Symbol *dummyArg : oldDetails.dummyArgs()) { 876 if (!dummyArg) { 877 newDetails.add_alternateReturn(); 878 } else if (Symbol * copy{mapper.CopySymbol(dummyArg)}) { 879 copy->set(Symbol::Flag::Implicit, false); 880 newDetails.add_dummyArg(*copy); 881 mappings->symbolMap[dummyArg] = copy; 882 } 883 } 884 if (oldDetails.isFunction()) { 885 newScope.erase(newSymbol.name()); 886 const Symbol &result{oldDetails.result()}; 887 if (Symbol * copy{mapper.CopySymbol(&result)}) { 888 newDetails.set_result(*copy); 889 mappings->symbolMap[&result] = copy; 890 } 891 } 892 for (auto &[_, ref] : newScope) { 893 mapper.MapSymbolExprs(*ref); 894 } 895 newScope.InstantiateDerivedTypes(); 896 } 897 898 } // namespace Fortran::semantics 899