Skip to content

Commit

Permalink
Mangling: fix wrong re-mangling of identifier-like node substitutions.
Browse files Browse the repository at this point in the history
rdar://problem/30172848
  • Loading branch information
eeckstein committed Jan 25, 2017
1 parent 1f86b41 commit 21ff1ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
48 changes: 17 additions & 31 deletions lib/Basic/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ namespace {
class SubstitutionEntry {
Node *TheNode = nullptr;
size_t StoredHash = 0;
bool NewMangling = false;
bool treatAsIdentifier = false;

public:
void setNode(Node *node, bool UseNewMangling) {
NewMangling = UseNewMangling;
void setNode(Node *node, bool treatAsIdentifier) {
this->treatAsIdentifier = treatAsIdentifier;
TheNode = node;
deepHash(node);
}
Expand All @@ -65,26 +65,13 @@ class SubstitutionEntry {
private:
friend bool operator==(const SubstitutionEntry &lhs,
const SubstitutionEntry &rhs) {
return (lhs.StoredHash == rhs.StoredHash &&
lhs.deepEquals(lhs.TheNode, rhs.TheNode));
}

bool treatAsIdentifier(Node *node) const {
if (!NewMangling)
if (lhs.StoredHash != rhs.StoredHash)
return false;

switch (node->getKind()) {
case Node::Kind::Module:
case Node::Kind::TupleElementName:
case Node::Kind::InfixOperator:
case Node::Kind::PrefixOperator:
case Node::Kind::PostfixOperator:
case Node::Kind::DependentAssociatedTypeRef:
case Node::Kind::Identifier:
return true;
default:
return false;
}
if (lhs.treatAsIdentifier != rhs.treatAsIdentifier)
return false;
if (lhs.treatAsIdentifier)
return lhs.TheNode->getText() == rhs.TheNode->getText();
return lhs.deepEquals(lhs.TheNode, rhs.TheNode);
}

void combineHash(size_t newValue) {
Expand All @@ -98,7 +85,7 @@ class SubstitutionEntry {
}

void deepHash(Node *node) {
if (treatAsIdentifier(node)) {
if (treatAsIdentifier) {
combineHash((size_t) Node::Kind::Identifier);
combineHash(node->getText());
return;
Expand All @@ -118,9 +105,6 @@ class SubstitutionEntry {
};

bool SubstitutionEntry::deepEquals(Node *lhs, Node *rhs) const {
if (treatAsIdentifier(lhs) && treatAsIdentifier(rhs))
return lhs->getText() == rhs->getText();

if (lhs->getKind() != rhs->getKind())
return false;
if (lhs->hasIndex()) {
Expand All @@ -140,7 +124,7 @@ bool SubstitutionEntry::deepEquals(Node *lhs, Node *rhs) const {
if (lhs->getNumChildren() != rhs->getNumChildren())
return false;

for (auto li = lhs->begin(), ri = lhs->begin(), le = lhs->end();
for (auto li = lhs->begin(), ri = rhs->begin(), le = lhs->end();
li != le; ++li, ++ri) {
if (!deepEquals(li->get(), ri->get()))
return false;
Expand Down Expand Up @@ -263,7 +247,8 @@ class Remangler {
mangleChildNodes(Proto);
}

bool trySubstitution(Node *node, SubstitutionEntry &entry);
bool trySubstitution(Node *node, SubstitutionEntry &entry,
bool treatAsIdentifier = false);
void addSubstitution(const SubstitutionEntry &entry);

void mangleIdentifierImpl(Node *node, bool isOperator);
Expand Down Expand Up @@ -301,12 +286,13 @@ class Remangler {
}
};

bool Remangler::trySubstitution(Node *node, SubstitutionEntry &entry) {
bool Remangler::trySubstitution(Node *node, SubstitutionEntry &entry,
bool treatAsIdentifier) {
if (mangleStandardSubstitution(node, Buffer))
return true;

// Go ahead and initialize the substitution entry.
entry.setNode(node, /*UseNewMangling*/ true);
entry.setNode(node, treatAsIdentifier);

auto it = Substitutions.find(entry);
if (it == Substitutions.end())
Expand Down Expand Up @@ -349,7 +335,7 @@ void Remangler::addSubstitution(const SubstitutionEntry &entry) {

void Remangler::mangleIdentifierImpl(Node *node, bool isOperator) {
SubstitutionEntry entry;
if (trySubstitution(node, entry)) return;
if (trySubstitution(node, entry, /*treatAsIdentifier*/ true)) return;
if (isOperator) {
NewMangling::mangleIdentifier(*this,
NewMangling::translateOperator(node->getText()));
Expand Down
1 change: 1 addition & 0 deletions test/Demangle/Inputs/manglings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,5 @@ _TFVFC15nested_generics7HotDogs11applyRelishFT_T_L_6RelishCfT8materialx_GS1_x_ -
_TFVFE15nested_genericsSS3fooFT_T_L_6CheeseCfT8materialx_GS0_x_ ---> (extension in nested_generics):Swift.String.(foo () -> ()).(Cheese #1).init (material : A) -> (extension in nested_generics):Swift.String.(foo () -> ()).(Cheese #1)<A>
_TTWOE5imojiCSo5Imoji14ImojiMatchRankS_9RankValueS_FS2_g9rankValueqq_Ss16RawRepresentable8RawValue ---> _TTWOE5imojiCSo5Imoji14ImojiMatchRankS_9RankValueS_FS2_g9rankValueqq_Ss16RawRepresentable8RawValue
_TtFzas4VoidGC16FusionXBaseUtils6FutureQq_ZFVS_7Futures6futureurFFzT_GS0_x_GS0_x__ ---> _TtFzas4VoidGC16FusionXBaseUtils6FutureQq_ZFVS_7Futures6futureurFFzT_GS0_x_GS0_x__
_T0s17MutableCollectionP1asAARzs012RandomAccessB0RzsAA11SubSequences013BidirectionalB0PRpzsAdHRQlE06rotatecD05Indexs01_A9IndexablePQzAM15shiftingToStart_tFAJs01_J4BasePQzAQcfU_ ---> (extension in a):Swift.MutableCollection<A where A: Swift.MutableCollection, A: Swift.RandomAccessCollection, A.SubSequence: Swift.MutableCollection, A.SubSequence: Swift.RandomAccessCollection>.(rotateRandomAccess (shiftingToStart : A.Index) -> A.Index).(closure #1)

0 comments on commit 21ff1ba

Please sign in to comment.