Skip to content

Commit 5883f77

Browse files
committed
[sil] Add the ability to parse SILOptionalAttr of the form [NAME=STRING].
1 parent e44757a commit 5883f77

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -494,40 +494,66 @@ static SILLinkage resolveSILLinkage(llvm::Optional<SILLinkage> linkage,
494494
}
495495
}
496496

497-
// Returns false if no optional exists. Returns true on both success and
498-
// failure. On success, the Result string is nonempty. If the optional is
499-
// assigned to an integer value, \p value contains the parsed value. Otherwise,
500-
// value is set to the maximum uint64_t.
501-
static bool parseSILOptional(StringRef &Result, uint64_t &value, SourceLoc &Loc,
502-
SILParser &SP) {
503-
if (!SP.P.consumeIf(tok::l_square))
504-
return false;
497+
namespace {
505498

506-
value = ~uint64_t(0);
499+
using SILOptionalAttrValue = std::optional<std::variant<uint64_t, StringRef>>;
507500

508-
Identifier Id;
509-
if (SP.parseSILIdentifier(Id, Loc, diag::expected_in_attribute_list))
501+
} // namespace
502+
503+
/// Returns false if no optional exists. Returns true on both success and
504+
/// failure. On success, the Result string is nonempty. If the optional is
505+
/// assigned to an integer value using an equal, \p value contains the parsed
506+
/// value. Otherwise, value is set to the maximum uint64_t.
507+
///
508+
/// Example: [alignment=$NUM]
509+
static bool parseSILOptional(StringRef &parsedName, SourceLoc &parsedNameLoc,
510+
SILOptionalAttrValue &parsedValue,
511+
SourceLoc &parsedValueLoc, SILParser &parser) {
512+
if (!parser.P.consumeIf(tok::l_square))
513+
return false;
514+
515+
Identifier parsedNameId;
516+
if (parser.parseSILIdentifier(parsedNameId, parsedNameLoc,
517+
diag::expected_in_attribute_list))
510518
return true;
519+
parsedName = parsedNameId.str();
511520

512-
if (SP.P.consumeIf(tok::equal)) {
513-
if (SP.parseInteger(value, diag::expected_in_attribute_list))
514-
return true;
521+
uint64_t parsedIntValue = ~uint64_t(0);
522+
Identifier parsedStringId;
523+
if (parser.P.consumeIf(tok::equal)) {
524+
auto currentTok = parser.P.Tok;
525+
parsedValueLoc = currentTok.getLoc();
526+
527+
if (currentTok.is(tok::integer_literal)) {
528+
if (parser.parseInteger(parsedIntValue,
529+
diag::expected_in_attribute_list)) {
530+
return true;
531+
}
532+
parsedValue = parsedIntValue;
533+
} else {
534+
if (parser.parseSILIdentifier(parsedStringId, parsedValueLoc,
535+
diag::expected_in_attribute_list)) {
536+
return true;
537+
}
538+
parsedValue = parsedStringId.str();
539+
}
515540
}
516-
if (SP.P.parseToken(tok::r_square, diag::expected_in_attribute_list))
541+
542+
if (parser.P.parseToken(tok::r_square, diag::expected_in_attribute_list))
517543
return true;
518544

519-
Result = Id.str();
520545
return true;
521546
}
522547

523548
static bool parseSILOptional(StringRef &Result, SourceLoc &Loc, SILParser &SP) {
524-
uint64_t value;
525-
return parseSILOptional(Result, value, Loc, SP);
549+
SILOptionalAttrValue value;
550+
SourceLoc valueLoc;
551+
return parseSILOptional(Result, Loc, value, valueLoc, SP);
526552
}
527553

528-
static bool parseSILOptional(StringRef &Result, SILParser &SP) {
529-
SourceLoc Loc;
530-
return parseSILOptional(Result, Loc, SP);
554+
static bool parseSILOptional(StringRef &attrName, SILParser &SP) {
555+
SourceLoc attrLoc;
556+
return parseSILOptional(attrName, attrLoc, SP);
531557
}
532558

533559
/// Parse an option attribute ('[' Expected ']')?
@@ -4056,8 +4082,9 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
40564082
bool isStrict = false;
40574083
bool isInvariant = false;
40584084
llvm::MaybeAlign alignment;
4059-
uint64_t parsedValue = 0;
4060-
while (parseSILOptional(attr, parsedValue, ToLoc, *this)) {
4085+
SILOptionalAttrValue parsedValue;
4086+
SourceLoc parsedValueLoc;
4087+
while (parseSILOptional(attr, ToLoc, parsedValue, parsedValueLoc, *this)) {
40614088
if (attr.empty())
40624089
return true;
40634090

@@ -4068,7 +4095,7 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
40684095
isInvariant = true;
40694096

40704097
if (attr.equals("align"))
4071-
alignment = llvm::Align(parsedValue);
4098+
alignment = llvm::Align(std::get<uint64_t>(*parsedValue));
40724099
}
40734100

40744101
if (parseSILType(Ty) || parseSILDebugLocation(InstLoc, B))

0 commit comments

Comments
 (0)