1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
|
//===--- ConstraintLocator.cpp - Constraint Locator -----------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the \c ConstraintLocator class and its related types,
// which is used by the constraint-based type checker to describe how
// a particular constraint was derived.
//
//===----------------------------------------------------------------------===//
#include "swift/Sema/ConstraintLocator.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Types.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Sema/Constraint.h"
#include "swift/Sema/ConstraintLocator.h"
#include "swift/Sema/ConstraintSystem.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
using namespace constraints;
void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, ASTNode anchor,
ArrayRef<PathElement> path) {
id.AddPointer(anchor.getOpaqueValue());
id.AddInteger(path.size());
for (auto elt : path) {
id.AddInteger(elt.getKind());
id.AddInteger(elt.getRawStorage());
}
}
unsigned LocatorPathElt::getNewSummaryFlags() const {
switch (getKind()) {
case ConstraintLocator::ApplyArgument:
case ConstraintLocator::ApplyFunction:
case ConstraintLocator::SequenceElementType:
case ConstraintLocator::ClosureResult:
case ConstraintLocator::ClosureThrownError:
case ConstraintLocator::ClosureBody:
case ConstraintLocator::ConstructorMember:
case ConstraintLocator::ConstructorMemberType:
case ConstraintLocator::ResultBuilderBodyResult:
case ConstraintLocator::InstanceType:
case ConstraintLocator::AutoclosureResult:
case ConstraintLocator::OptionalPayload:
case ConstraintLocator::Member:
case ConstraintLocator::MemberRefBase:
case ConstraintLocator::UnresolvedMember:
case ConstraintLocator::ParentType:
case ConstraintLocator::ExistentialConstraintType:
case ConstraintLocator::ProtocolCompositionSuperclassType:
case ConstraintLocator::LValueConversion:
case ConstraintLocator::DynamicType:
case ConstraintLocator::SubscriptMember:
case ConstraintLocator::OpenedGeneric:
case ConstraintLocator::OpenedOpaqueArchetype:
case ConstraintLocator::WrappedValue:
case ConstraintLocator::GenericParameter:
case ConstraintLocator::GenericArgument:
case ConstraintLocator::TupleType:
case ConstraintLocator::GenericType:
case ConstraintLocator::NamedTupleElement:
case ConstraintLocator::TupleElement:
case ConstraintLocator::ProtocolRequirement:
case ConstraintLocator::Witness:
case ConstraintLocator::KeyPathComponent:
case ConstraintLocator::ConditionalRequirement:
case ConstraintLocator::TypeParameterRequirement:
case ConstraintLocator::ConformanceRequirement:
case ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice:
case ConstraintLocator::DynamicLookupResult:
case ConstraintLocator::ContextualType:
case ConstraintLocator::SynthesizedArgument:
case ConstraintLocator::KeyPathDynamicMember:
case ConstraintLocator::KeyPathType:
case ConstraintLocator::KeyPathRoot:
case ConstraintLocator::KeyPathValue:
case ConstraintLocator::KeyPathComponentResult:
case ConstraintLocator::Condition:
case ConstraintLocator::DynamicCallable:
case ConstraintLocator::ImplicitCallAsFunction:
case ConstraintLocator::TernaryBranch:
case ConstraintLocator::PatternMatch:
case ConstraintLocator::EnumPatternImplicitCastMatch:
case ConstraintLocator::ArgumentAttribute:
case ConstraintLocator::UnresolvedMemberChainResult:
case ConstraintLocator::PlaceholderType:
case ConstraintLocator::ImplicitConversion:
case ConstraintLocator::ImplicitDynamicMemberSubscript:
case ConstraintLocator::SyntacticElement:
case ConstraintLocator::PackType:
case ConstraintLocator::PackElement:
case ConstraintLocator::PackShape:
case ConstraintLocator::PackExpansionPattern:
case ConstraintLocator::PatternBindingElement:
case ConstraintLocator::NamedPatternDecl:
case ConstraintLocator::SingleValueStmtResult:
case ConstraintLocator::AnyPatternDecl:
case ConstraintLocator::GlobalActorType:
case ConstraintLocator::CoercionOperand:
case ConstraintLocator::PackExpansionType:
case ConstraintLocator::ThrownErrorType:
case ConstraintLocator::FallbackType:
case ConstraintLocator::KeyPathSubscriptIndex:
return 0;
case ConstraintLocator::FunctionArgument:
case ConstraintLocator::FunctionResult:
return IsFunctionConversion;
case ConstraintLocator::ApplyArgToParam: {
auto flags = castTo<LocatorPathElt::ApplyArgToParam>().getParameterFlags();
return flags.isNonEphemeral() ? IsNonEphemeralParam : 0;
}
}
llvm_unreachable("Unhandled PathElementKind in switch.");
}
void LocatorPathElt::dump(raw_ostream &out) const {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
auto dumpReqKind = [&out](RequirementKind kind) {
out << " (";
switch (kind) {
case RequirementKind::SameShape:
out << "same_shape";
break;
case RequirementKind::Conformance:
out << "conformance";
break;
case RequirementKind::Superclass:
out << "superclass";
break;
case RequirementKind::SameType:
out << "same-type";
break;
case RequirementKind::Layout:
out << "layout";
break;
}
out << ")";
};
const LocatorPathElt &elt = *this;
switch (getKind()) {
case ConstraintLocator::GenericParameter: {
auto gpElt = elt.castTo<LocatorPathElt::GenericParameter>();
out << "generic parameter '" << gpElt.getType()->getString(PO) << "'";
break;
}
case ConstraintLocator::WrappedValue: {
auto wrappedValueElt = elt.castTo<LocatorPathElt::WrappedValue>();
out << "composed property wrapper type '"
<< wrappedValueElt.getType()->getString(PO) << "'";
break;
}
case ConstraintLocator::ApplyArgument:
out << "apply argument";
break;
case ConstraintLocator::ApplyFunction:
out << "apply function";
break;
case ConstraintLocator::OptionalPayload:
out << "optional payload";
break;
case ConstraintLocator::ApplyArgToParam: {
auto argElt = elt.castTo<LocatorPathElt::ApplyArgToParam>();
out << "comparing call argument #" << llvm::utostr(argElt.getArgIdx())
<< " to parameter #" << llvm::utostr(argElt.getParamIdx());
if (argElt.getParameterFlags().isNonEphemeral())
out << " (non-ephemeral)";
break;
}
case ConstraintLocator::ClosureResult:
out << "closure result";
break;
case ConstraintLocator::ClosureThrownError:
out << "closure thrown error";
break;
case ConstraintLocator::ClosureBody:
out << "type of a closure body";
break;
case ConstraintLocator::ConstructorMember:
out << "constructor member";
break;
case ConstraintLocator::ConstructorMemberType: {
auto memberTypeElt = elt.castTo<LocatorPathElt::ConstructorMemberType>();
out << "constructor member type";
if (memberTypeElt.isShortFormOrSelfDelegatingConstructor())
out << " (for short-form or self.init call)";
break;
}
case ConstraintLocator::FunctionArgument:
out << "function argument";
break;
case ConstraintLocator::FunctionResult:
out << "function result";
break;
case ConstraintLocator::ResultBuilderBodyResult:
out << "result builder body result";
break;
case ConstraintLocator::SequenceElementType:
out << "sequence element type";
break;
case ConstraintLocator::GenericArgument: {
auto genericElt = elt.castTo<LocatorPathElt::GenericArgument>();
out << "generic argument #" << llvm::utostr(genericElt.getIndex());
break;
}
case ConstraintLocator::InstanceType:
out << "instance type";
break;
case ConstraintLocator::AutoclosureResult:
out << "@autoclosure result";
break;
case ConstraintLocator::Member:
out << "member";
break;
case ConstraintLocator::MemberRefBase:
out << "member reference base";
break;
case ConstraintLocator::TupleType: {
auto tupleElt = elt.castTo<LocatorPathElt::TupleType>();
out << "tuple type '" << tupleElt.getType()->getString(PO) << "'";
break;
}
case ConstraintLocator::GenericType: {
auto genericTyElt = elt.castTo<LocatorPathElt::GenericType>();
out << "generic type '" << genericTyElt.getType()->getString(PO) << "'";
break;
}
case ConstraintLocator::NamedTupleElement: {
auto tupleElt = elt.castTo<LocatorPathElt::NamedTupleElement>();
out << "named tuple element #" << llvm::utostr(tupleElt.getIndex());
break;
}
case ConstraintLocator::UnresolvedMember:
out << "unresolved member";
break;
case ConstraintLocator::ParentType:
out << "parent type";
break;
case ConstraintLocator::ExistentialConstraintType:
out << "existential constraint type";
break;
case ConstraintLocator::ProtocolCompositionSuperclassType:
out << "protocol composition superclass type";
break;
case ConstraintLocator::LValueConversion:
out << "@lvalue-to-inout conversion";
break;
case ConstraintLocator::DynamicType:
out << "`.dynamicType` reference";
break;
case ConstraintLocator::SubscriptMember:
out << "subscript member";
break;
case ConstraintLocator::TupleElement: {
auto tupleElt = elt.castTo<LocatorPathElt::TupleElement>();
out << "tuple element #" << llvm::utostr(tupleElt.getIndex());
break;
}
case ConstraintLocator::KeyPathComponent: {
auto kpElt = elt.castTo<LocatorPathElt::KeyPathComponent>();
out << "key path component #" << llvm::utostr(kpElt.getIndex());
break;
}
case ConstraintLocator::ProtocolRequirement: {
out << "protocol requirement";
break;
}
case ConstraintLocator::Witness: {
auto witnessElt = elt.castTo<LocatorPathElt::Witness>();
out << "witness ";
witnessElt.getDecl()->dumpRef(out);
break;
}
case ConstraintLocator::OpenedGeneric:
out << "opened generic";
break;
case ConstraintLocator::OpenedOpaqueArchetype:
out << "opened opaque archetype";
break;
case ConstraintLocator::ConditionalRequirement: {
auto reqElt = elt.castTo<LocatorPathElt::ConditionalRequirement>();
out << "conditional requirement #" << llvm::utostr(reqElt.getIndex());
dumpReqKind(reqElt.getRequirementKind());
break;
}
case ConstraintLocator::TypeParameterRequirement: {
auto reqElt = elt.castTo<LocatorPathElt::TypeParameterRequirement>();
out << "type parameter requirement #" << llvm::utostr(reqElt.getIndex());
dumpReqKind(reqElt.getRequirementKind());
break;
}
case ConstraintLocator::ConformanceRequirement: {
auto *conformance =
elt.castTo<LocatorPathElt::ConformanceRequirement>().getConformance();
out << "conformance requirement (";
conformance->getProtocol()->dumpRef(out);
out << ")";
break;
}
case ConstraintLocator::ImplicitlyUnwrappedDisjunctionChoice:
out << "implicitly unwrapped disjunction choice";
break;
case ConstraintLocator::DynamicLookupResult:
out << "dynamic lookup result";
break;
case ConstraintLocator::ContextualType:
out << "contextual type";
break;
case ConstraintLocator::SynthesizedArgument: {
auto argElt = elt.castTo<LocatorPathElt::SynthesizedArgument>();
out << "synthesized argument #" << llvm::utostr(argElt.getIndex());
break;
}
case ConstraintLocator::KeyPathDynamicMember:
out << "key path dynamic member lookup";
break;
case ConstraintLocator::KeyPathType:
out << "key path type";
break;
case ConstraintLocator::KeyPathRoot:
out << "key path root";
break;
case ConstraintLocator::KeyPathValue:
out << "key path value";
break;
case ConstraintLocator::KeyPathComponentResult:
out << "key path component result";
break;
case ConstraintLocator::Condition:
out << "condition expression";
break;
case ConstraintLocator::DynamicCallable:
out << "implicit call to @dynamicCallable method";
break;
case ConstraintLocator::ImplicitCallAsFunction:
out << "implicit reference to callAsFunction";
break;
case ConstraintLocator::TernaryBranch: {
auto branchElt = elt.castTo<LocatorPathElt::TernaryBranch>();
out << (branchElt.forThen() ? "'then'" : "'else'")
<< " branch of a ternary operator";
break;
}
case ConstraintLocator::SingleValueStmtResult: {
auto branch = elt.castTo<LocatorPathElt::SingleValueStmtResult>();
out << "result [" << branch.getIndex() << "] of "
"single value stmt";
break;
}
case ConstraintLocator::PatternMatch:
out << "pattern match";
break;
case ConstraintLocator::EnumPatternImplicitCastMatch:
out << "enum pattern implicit cast match";
break;
case ConstraintLocator::ArgumentAttribute: {
using AttrLoc = LocatorPathElt::ArgumentAttribute;
auto attrElt = elt.castTo<AttrLoc>();
out << "argument attribute: ";
switch (attrElt.getAttr()) {
case AttrLoc::Attribute::InOut:
out << "inout";
break;
case AttrLoc::Attribute::Escaping:
out << "@escaping";
break;
case AttrLoc::Attribute::Concurrent:
out << "@Sendable";
break;
case AttrLoc::Attribute::GlobalActor:
out << "@<global actor>";
break;
}
break;
}
case ConstraintLocator::UnresolvedMemberChainResult:
out << "unresolved chain result";
break;
case ConstraintLocator::PlaceholderType:
out << "placeholder type";
break;
case ConstraintLocator::ConstraintLocator::SyntacticElement:
// TODO: Would be great to print a kind of element this is e.g.
// "if", "for each", "switch" etc.
out << "syntactic element";
break;
case ConstraintLocator::ConstraintLocator::ImplicitDynamicMemberSubscript:
out << "implicit dynamic member subscript";
break;
case ConstraintLocator::ConstraintLocator::ImplicitConversion: {
auto convElt = elt.castTo<LocatorPathElt::ImplicitConversion>();
out << "implicit conversion " << getName(convElt.getConversionKind());
break;
}
case ConstraintLocator::ConstraintLocator::PackType: {
auto packElt = elt.castTo<LocatorPathElt::PackType>();
out << "pack type '" << packElt.getType()->getString(PO) << "'";
break;
}
case ConstraintLocator::PackElement: {
auto packElt = elt.castTo<LocatorPathElt::PackElement>();
out << "pack element #" << llvm::utostr(packElt.getIndex());
break;
}
case ConstraintLocator::PackShape: {
out << "pack shape";
break;
}
case ConstraintLocator::PackExpansionPattern: {
out << "pack expansion pattern";
break;
}
case ConstraintLocator::PatternBindingElement: {
auto patternBindingElt =
elt.castTo<LocatorPathElt::PatternBindingElement>();
out << "pattern binding element #"
<< llvm::utostr(patternBindingElt.getIndex());
break;
}
case ConstraintLocator::NamedPatternDecl: {
out << "named pattern decl";
break;
}
case ConstraintLocator::AnyPatternDecl: {
out << "'_' pattern decl";
break;
}
case ConstraintLocator::GlobalActorType: {
out << "global actor type";
break;
}
case ConstraintLocator::CoercionOperand: {
out << "coercion operand";
break;
case ConstraintLocator::PackExpansionType:
auto expansionElt = elt.castTo<LocatorPathElt::PackExpansionType>();
out << "pack expansion type ("
<< expansionElt.getOpenedType()->getString(PO) << ")";
break;
}
case ConstraintLocator::ThrownErrorType: {
out << "thrown error type";
break;
}
case ConstraintLocator::FallbackType: {
out << "fallback type";
break;
case ConstraintLocator::KeyPathSubscriptIndex:
out << "key path subscript index parameter";
break;
}
}
}
/// Determine whether given locator points to the subscript reference
/// e.g. `foo[0]` or `\Foo.[0]`
bool ConstraintLocator::isSubscriptMemberRef() const {
auto anchor = getAnchor();
auto path = getPath();
if (!anchor || path.empty())
return false;
return path.back().getKind() == ConstraintLocator::SubscriptMember;
}
bool ConstraintLocator::isKeyPathType() const {
auto anchor = getAnchor();
auto path = getPath();
// The format of locator should be `<keypath expr> -> key path type`
if (!anchor || !isExpr<KeyPathExpr>(anchor) || path.size() != 1)
return false;
return path.back().is<LocatorPathElt::KeyPathType>();
}
bool ConstraintLocator::isKeyPathRoot() const {
auto anchor = getAnchor();
auto path = getPath();
if (!anchor || path.empty())
return false;
return path.back().getKind() == ConstraintLocator::KeyPathRoot;
}
bool ConstraintLocator::isKeyPathValue() const {
auto anchor = getAnchor();
auto path = getPath();
if (!anchor || path.empty())
return false;
return path.back().getKind() == ConstraintLocator::KeyPathValue;
}
bool ConstraintLocator::isResultOfKeyPathDynamicMemberLookup() const {
return llvm::any_of(getPath(), [](const LocatorPathElt &elt) {
return elt.isKeyPathDynamicMember();
});
}
bool ConstraintLocator::isKeyPathSubscriptComponent() const {
auto *anchor = getAsExpr(getAnchor());
auto *KPE = dyn_cast_or_null<KeyPathExpr>(anchor);
if (!KPE)
return false;
using ComponentKind = KeyPathExpr::Component::Kind;
return llvm::any_of(getPath(), [&](const LocatorPathElt &elt) {
auto keyPathElt = elt.getAs<LocatorPathElt::KeyPathComponent>();
if (!keyPathElt)
return false;
auto index = keyPathElt->getIndex();
auto &component = KPE->getComponents()[index];
return component.getKind() == ComponentKind::Subscript ||
component.getKind() == ComponentKind::UnresolvedSubscript;
});
}
bool ConstraintLocator::isForKeyPathDynamicMemberLookup() const {
auto path = getPath();
return !path.empty() && path.back().isKeyPathDynamicMember();
}
bool ConstraintLocator::isInKeyPathComponent() const {
return llvm::any_of(getPath(), [&](const LocatorPathElt &elt) {
return elt.isKeyPathComponent();
});
}
bool ConstraintLocator::isForKeyPathComponentResult() const {
return isLastElement<LocatorPathElt::KeyPathComponentResult>();
}
bool ConstraintLocator::isForKeyPathComponent() const {
return isLastElement<LocatorPathElt::KeyPathComponent>();
}
bool ConstraintLocator::isForGenericParameter() const {
return isLastElement<LocatorPathElt::GenericParameter>();
}
bool ConstraintLocator::isForWrappedValue() const {
return isLastElement<LocatorPathElt::WrappedValue>();
}
bool ConstraintLocator::isForSequenceElementType() const {
return isLastElement<LocatorPathElt::SequenceElementType>();
}
bool ConstraintLocator::isForContextualType() const {
return isLastElement<LocatorPathElt::ContextualType>();
}
bool ConstraintLocator::isForContextualType(ContextualTypePurpose ctp) const {
auto elt = getLastElementAs<LocatorPathElt::ContextualType>();
return elt && elt->getPurpose() == ctp;
}
bool ConstraintLocator::isForAssignment() const {
return directlyAt<AssignExpr>();
}
bool ConstraintLocator::isForCoercion() const {
return isLastElement<LocatorPathElt::CoercionOperand>();
}
bool ConstraintLocator::isForOptionalTry() const {
return directlyAt<OptionalTryExpr>();
}
bool ConstraintLocator::isForResultBuilderBodyResult() const {
return isFirstElement<LocatorPathElt::ResultBuilderBodyResult>();
}
bool ConstraintLocator::isForMacroExpansion() const {
return directlyAt<MacroExpansionExpr>();
}
static bool isForSingleValueStmtConjunction(ASTNode anchor,
ArrayRef<LocatorPathElt> path) {
auto *SVE = getAsExpr<SingleValueStmtExpr>(anchor);
if (!SVE)
return false;
if (!path.empty()) {
// Ignore a trailing SyntacticElement path element for the statement.
if (auto elt = path.back().getAs<LocatorPathElt::SyntacticElement>()) {
if (elt->getElement() == ASTNode(SVE->getStmt()))
path = path.drop_back();
}
}
// Other than the trailing SyntaticElement, we must be at the anchor.
return path.empty();
}
bool ConstraintLocator::isForSingleValueStmtConjunction() const {
return ::isForSingleValueStmtConjunction(getAnchor(), getPath());
}
bool ConstraintLocator::isForSingleValueStmtConjunctionOrBrace() const {
auto *SVE = getAsExpr<SingleValueStmtExpr>(getAnchor());
if (!SVE)
return false;
auto path = getPath();
while (!path.empty()) {
// Ignore a trailing TernaryBranch locator, which is used for if statements.
if (path.back().is<LocatorPathElt::TernaryBranch>()) {
path = path.drop_back();
continue;
}
// Ignore a SyntaticElement path element for a case statement of a switch,
// or the catch of a do-catch, or the brace of a do-statement.
if (auto elt = path.back().getAs<LocatorPathElt::SyntacticElement>()) {
if (elt->getElement().isStmt(StmtKind::Case)) {
path = path.drop_back();
break;
}
if (elt->getElement().isStmt(StmtKind::Brace) &&
isa<DoCatchStmt>(SVE->getStmt())) {
path = path.drop_back();
break;
}
}
break;
}
return ::isForSingleValueStmtConjunction(getAnchor(), path);
}
bool ConstraintLocator::isForSingleValueStmtBranch() const {
if (!isExpr<SingleValueStmtExpr>(getAnchor()))
return false;
// Ignore a trailing ContextualType path element.
auto path = getPath();
if (isLastElement<LocatorPathElt::ContextualType>())
path = path.drop_back();
if (path.empty())
return false;
return path.back().is<LocatorPathElt::SingleValueStmtResult>();
}
NullablePtr<Pattern> ConstraintLocator::getPatternMatch() const {
auto matchElt = findLast<LocatorPathElt::PatternMatch>();
if (!matchElt)
return nullptr;
return matchElt->getPattern();
}
bool ConstraintLocator::isForPatternMatch() const {
return getPatternMatch() != nullptr;
}
bool ConstraintLocator::isMemberRef() const {
if (isLastElement<LocatorPathElt::Member>()) {
return true;
} else if (isLastElement<LocatorPathElt::KeyPathDynamicMember>()) {
auto path = getPath();
if (path.size() >= 2 &&
path[path.size() - 2].is<LocatorPathElt::Member>()) {
return true;
}
}
return false;
}
GenericTypeParamType *ConstraintLocator::getGenericParameter() const {
// Check whether we have a path that terminates at a generic parameter.
return isForGenericParameter() ?
castLastElementTo<LocatorPathElt::GenericParameter>().getType() : nullptr;
}
Type ConstraintLocator::getWrappedValue() const {
return isForWrappedValue()
? castLastElementTo<LocatorPathElt::WrappedValue>().getType()
: Type();
}
void ConstraintLocator::dump(SourceManager *sm) const {
dump(sm, llvm::errs());
llvm::errs() << "\n";
}
void ConstraintLocator::dump(ConstraintSystem *CS) const {
dump(&CS->getASTContext().SourceMgr, llvm::errs());
llvm::errs() << "\n";
}
void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) const {
PrintOptions PO;
PO.PrintTypesForDebugging = true;
out << "locator@" << (void*) this << " [";
constraints::dumpAnchor(anchor, sm, out);
for (auto elt : getPath()) {
out << " → ";
elt.dump(out);
}
out << ']';
}
void ConstraintLocatorBuilder::dump(SourceManager *sm) const {
dump(sm, llvm::errs());
llvm::errs() << "\n";
}
void ConstraintLocatorBuilder::dump(ConstraintSystem *CS) const {
dump(&CS->getASTContext().SourceMgr, llvm::errs());
llvm::errs() << "\n";
}
void ConstraintLocatorBuilder::dump(SourceManager *SM, llvm::raw_ostream &out) const {
if (auto prev = previous.dyn_cast<ConstraintLocator *>()) {
prev->dump(SM, out);
} else if (auto prev = previous.dyn_cast<ConstraintLocatorBuilder *>()) {
prev->dump(SM, out);
}
if (element) {
out << " -> ";
element->dump(out);
}
}
|