Skip to content

Commit e19ef09

Browse files
committed
[ConstraintSystem] Add a new locator element to denote branches of ternary operator
`TernaryBranch` with a boolean flag to identify "then" (true) or "else" (false) branches of the ternary operator expression.
1 parent 36fff23 commit e19ef09

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

lib/Sema/ConstraintLocator.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
6161
case ConditionalRequirement:
6262
case TypeParameterRequirement:
6363
case ContextualType:
64-
case SynthesizedArgument: {
64+
case SynthesizedArgument:
65+
case TernaryBranch: {
6566
auto numValues = numNumericValuesInPathElement(elt.getKind());
6667
for (unsigned i = 0; i < numValues; ++i)
6768
id.AddInteger(elt.getValue(i));
6869
break;
6970
}
7071
#define SIMPLE_LOCATOR_PATH_ELT(Name) case Name :
7172
#include "ConstraintLocatorPathElts.def"
72-
// Nothing to do for simple locator elements.
73-
break;
73+
// Nothing to do for simple locator elements.
74+
break;
7475
}
7576
}
7677
}
@@ -115,6 +116,7 @@ unsigned LocatorPathElt::getNewSummaryFlags() const {
115116
case ConstraintLocator::Condition:
116117
case ConstraintLocator::DynamicCallable:
117118
case ConstraintLocator::ImplicitCallAsFunction:
119+
case ConstraintLocator::TernaryBranch:
118120
return 0;
119121

120122
case ConstraintLocator::FunctionArgument:
@@ -463,6 +465,12 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) const {
463465
case ImplicitCallAsFunction:
464466
out << "implicit reference to callAsFunction";
465467
break;
468+
469+
case TernaryBranch:
470+
auto branchElt = elt.castTo<LocatorPathElt::TernaryBranch>();
471+
out << (branchElt.forThen() ? "'then'" : "'else'")
472+
<< " branch of a ternary operator";
473+
break;
466474
}
467475
}
468476
out << ']';

lib/Sema/ConstraintLocator.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
7878
case KeyPathComponent:
7979
case SynthesizedArgument:
8080
case KeyPathDynamicMember:
81+
case TernaryBranch:
8182
return 1;
8283

8384
case TypeParameterRequirement:
@@ -782,6 +783,20 @@ class LocatorPathElt::KeyPathDynamicMember final : public LocatorPathElt {
782783
}
783784
};
784785

786+
class LocatorPathElt::TernaryBranch final : public LocatorPathElt {
787+
public:
788+
TernaryBranch(bool side)
789+
: LocatorPathElt(ConstraintLocator::TernaryBranch, side) {}
790+
791+
bool forThen() const { return bool(getValue(0)); }
792+
793+
bool forElse() const { return !bool(getValue(0)); }
794+
795+
static bool classof(const LocatorPathElt *elt) {
796+
return elt->getKind() == ConstraintLocator::TernaryBranch;
797+
}
798+
};
799+
785800
/// A simple stack-only builder object that constructs a
786801
/// constraint locator without allocating memory.
787802
///

lib/Sema/ConstraintLocatorPathElts.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ SIMPLE_LOCATOR_PATH_ELT(Condition)
172172

173173
SIMPLE_LOCATOR_PATH_ELT(DynamicCallable)
174174

175+
/// The 'true' or 'false' branch of a ternary operator.
176+
CUSTOM_LOCATOR_PATH_ELT(TernaryBranch)
177+
175178
#undef LOCATOR_PATH_ELT
176179
#undef CUSTOM_LOCATOR_PATH_ELT
177180
#undef SIMPLE_LOCATOR_PATH_ELT

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,15 @@ void constraints::simplifyLocator(Expr *&anchor,
33133313
continue;
33143314
}
33153315

3316+
case ConstraintLocator::TernaryBranch: {
3317+
auto branch = path[0].castTo<LocatorPathElt::TernaryBranch>();
3318+
auto *ifExpr = cast<IfExpr>(anchor);
3319+
3320+
anchor = branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
3321+
path = path.slice(1);
3322+
continue;
3323+
}
3324+
33163325
default:
33173326
// FIXME: Lots of other cases to handle.
33183327
break;

0 commit comments

Comments
 (0)