verifyCustomDirectiveArguments(SMLoc loc,
ArrayRef<FormatElement *> arguments) override;
/// Verify the elements of an optional group.
- LogicalResult
- verifyOptionalGroupElements(SMLoc loc, ArrayRef<FormatElement *> elements,
- Optional<unsigned> anchorIndex) override;
+ LogicalResult verifyOptionalGroupElements(SMLoc loc,
+ ArrayRef<FormatElement *> elements,
+ FormatElement *anchor) override;
/// Parse an attribute or type variable.
FailureOr<FormatElement *> parseVariableImpl(SMLoc loc, StringRef name,
LogicalResult
DefFormatParser::verifyOptionalGroupElements(llvm::SMLoc loc,
ArrayRef<FormatElement *> elements,
- Optional<unsigned> anchorIndex) {
+ FormatElement *anchor) {
// `params` and `struct` directives are allowed only if all the contained
// parameters are optional.
for (FormatElement *el : elements) {
}
}
// The anchor must be a parameter or one of the aforementioned directives.
- if (anchorIndex && !isa<ParameterElement, ParamsDirective, StructDirective>(
- elements[*anchorIndex])) {
+ if (anchor &&
+ !isa<ParameterElement, ParamsDirective, StructDirective>(anchor)) {
return emitError(loc,
"optional group anchor must be a parameter or directive");
}
// Parse the child elements for this optional group.
std::vector<FormatElement *> thenElements, elseElements;
- Optional<unsigned> anchorIndex;
+ FormatElement *anchor = nullptr;
do {
FailureOr<FormatElement *> element = parseElement(TopLevelContext);
if (failed(element))
return failure();
// Check for an anchor.
if (curToken.is(FormatToken::caret)) {
- if (anchorIndex)
+ if (anchor)
return emitError(curToken.getLoc(), "only one element can be marked as "
"the anchor of an optional group");
- anchorIndex = thenElements.size();
+ anchor = *element;
consumeToken();
}
thenElements.push_back(*element);
return failure();
// The optional group is required to have an anchor.
- if (!anchorIndex)
+ if (!anchor)
return emitError(loc, "optional group has no anchor element");
// Verify the child elements.
- if (failed(verifyOptionalGroupElements(loc, thenElements, anchorIndex)) ||
- failed(verifyOptionalGroupElements(loc, elseElements, llvm::None)))
+ if (failed(verifyOptionalGroupElements(loc, thenElements, anchor)) ||
+ failed(verifyOptionalGroupElements(loc, elseElements, nullptr)))
return failure();
// Get the first parsable element. It must be an element that can be
unsigned parseStart = std::distance(thenElements.begin(), parseBegin);
return create<OptionalElement>(std::move(thenElements),
- std::move(elseElements), *anchorIndex,
- parseStart);
+ std::move(elseElements), anchor, parseStart);
}
FailureOr<FormatElement *> FormatParser::parseCustomDirective(SMLoc loc,
/// Create an optional group with the given child elements.
OptionalElement(std::vector<FormatElement *> &&thenElements,
std::vector<FormatElement *> &&elseElements,
- unsigned anchorIndex, unsigned parseStart)
+ FormatElement *anchor, unsigned parseStart)
: thenElements(std::move(thenElements)),
- elseElements(std::move(elseElements)), anchorIndex(anchorIndex),
+ elseElements(std::move(elseElements)), anchor(anchor),
parseStart(parseStart) {}
/// Return the `then` elements of the optional group.
ArrayRef<FormatElement *> getElseElements() const { return elseElements; }
/// Return the anchor of the optional group.
- FormatElement *getAnchor() const { return thenElements[anchorIndex]; }
+ FormatElement *getAnchor() const { return anchor; }
/// Return the index of the first element to be parsed.
unsigned getParseStart() const { return parseStart; }
std::vector<FormatElement *> thenElements;
/// The child elements emitted when the anchor is not present.
std::vector<FormatElement *> elseElements;
- /// The index of the anchor element of the optional group within
- /// `thenElements`.
- unsigned anchorIndex;
+ /// The anchor element of the optional group.
+ FormatElement *anchor;
/// The index of the first element that is parsed in `thenElements`. That is,
/// the first non-whitespace element.
unsigned parseStart;
virtual LogicalResult
verifyOptionalGroupElements(llvm::SMLoc loc,
ArrayRef<FormatElement *> elements,
- Optional<unsigned> anchorIndex) = 0;
+ FormatElement *anchor) = 0;
//===--------------------------------------------------------------------===//
// Lexer Utilities
verifyCustomDirectiveArguments(SMLoc loc,
ArrayRef<FormatElement *> arguments) override;
/// Verify the elements of an optional group.
- LogicalResult
- verifyOptionalGroupElements(SMLoc loc, ArrayRef<FormatElement *> elements,
- Optional<unsigned> anchorIndex) override;
+ LogicalResult verifyOptionalGroupElements(SMLoc loc,
+ ArrayRef<FormatElement *> elements,
+ FormatElement *anchor) override;
LogicalResult verifyOptionalGroupElement(SMLoc loc, FormatElement *element,
bool isAnchor);
return element;
}
-LogicalResult
-OpFormatParser::verifyOptionalGroupElements(SMLoc loc,
- ArrayRef<FormatElement *> elements,
- Optional<unsigned> anchorIndex) {
- for (auto &it : llvm::enumerate(elements)) {
- if (failed(verifyOptionalGroupElement(
- loc, it.value(), anchorIndex && *anchorIndex == it.index())))
+LogicalResult OpFormatParser::verifyOptionalGroupElements(
+ SMLoc loc, ArrayRef<FormatElement *> elements, FormatElement *anchor) {
+ for (FormatElement *element : elements) {
+ if (failed(verifyOptionalGroupElement(loc, element, element == anchor)))
return failure();
}
return success();