Skip to content

Commit 8a32be6

Browse files
authored
Merge pull request #3534 from gottesmm/silparser_use_stringswitch
[sil-parser] Small refactoring to use a string switch.
2 parents 5bee5b1 + 508e6a0 commit 8a32be6

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

lib/Parse/ParseSIL.cpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -596,36 +596,43 @@ void SILParser::setLocalValue(ValueBase *Value, StringRef Name,
596596
/// 'hidden_external'
597597
/// 'private_external'
598598
static bool parseSILLinkage(Optional<SILLinkage> &Result, Parser &P) {
599+
// Begin by initializing result to our base value of None.
600+
Result = None;
601+
602+
// Unfortunate collision with access control keywords.
599603
if (P.Tok.is(tok::kw_public)) {
600-
// Unfortunate collision with access control keywords.
601604
Result = SILLinkage::Public;
602605
P.consumeToken();
603-
} else if (P.Tok.is(tok::kw_private)) {
606+
return false;
607+
}
608+
609+
// Unfortunate collision with access control keywords.
610+
if (P.Tok.is(tok::kw_private)) {
604611
Result = SILLinkage::Private;
605612
P.consumeToken();
606-
} else if (P.Tok.isNot(tok::identifier)) {
607-
Result = None;
608-
} else if (P.Tok.getText() == "hidden") {
609-
Result = SILLinkage::Hidden;
610-
P.consumeToken(tok::identifier);
611-
} else if (P.Tok.getText() == "shared") {
612-
Result = SILLinkage::Shared;
613-
P.consumeToken(tok::identifier);
614-
} else if (P.Tok.getText() == "public_external") {
615-
Result = SILLinkage::PublicExternal;
616-
P.consumeToken(tok::identifier);
617-
} else if (P.Tok.getText() == "hidden_external") {
618-
Result = SILLinkage::HiddenExternal;
619-
P.consumeToken(tok::identifier);
620-
} else if (P.Tok.getText() == "shared_external") {
621-
Result = SILLinkage::SharedExternal;
622-
P.consumeToken(tok::identifier);
623-
} else if (P.Tok.getText() == "private_external") {
624-
Result = SILLinkage::PrivateExternal;
613+
return false;
614+
}
615+
616+
// If we do not have an identifier, bail. All SILLinkages that we are parsing
617+
// are identifiers.
618+
if (P.Tok.isNot(tok::identifier))
619+
return false;
620+
621+
// Then use a string switch to try and parse the identifier.
622+
Result = llvm::StringSwitch<Optional<SILLinkage>>(P.Tok.getText())
623+
.Case("hidden", SILLinkage::Hidden)
624+
.Case("shared", SILLinkage::Shared)
625+
.Case("public_external", SILLinkage::PublicExternal)
626+
.Case("hidden_external", SILLinkage::HiddenExternal)
627+
.Case("shared_external", SILLinkage::SharedExternal)
628+
.Case("private_external", SILLinkage::PrivateExternal)
629+
.Default(None);
630+
631+
// If we succeed, consume the token.
632+
if (Result) {
625633
P.consumeToken(tok::identifier);
626-
} else {
627-
Result = None;
628634
}
635+
629636
return false;
630637
}
631638

0 commit comments

Comments
 (0)