[OpAsmParser] Simplify logic for requiredOperandCount in parseOperandList.

I would ideally like to eliminate 'requiredOperandCount' as a bit of
verification that should be in the client side, but it is much more
widely used than I expected.  Just tidy some pieces up around it given
we can't drop it immediately.

NFC.

Differential Revision: https://reviews.llvm.org/D124629
This commit is contained in:
Chris Lattner 2022-04-28 11:37:17 -07:00
parent 463790bfc7
commit 99499c3ea7
5 changed files with 27 additions and 56 deletions

View File

@ -1122,29 +1122,30 @@ public:
/// Parse zero or more SSA comma-separated operand references with a specified /// Parse zero or more SSA comma-separated operand references with a specified
/// surrounding delimiter, and an optional required operand count. /// surrounding delimiter, and an optional required operand count.
virtual ParseResult parseOperandList( virtual ParseResult
SmallVectorImpl<UnresolvedOperand> &result, int requiredOperandCount = -1, parseOperandList(SmallVectorImpl<UnresolvedOperand> &result,
Delimiter delimiter = Delimiter::None, bool allowResultNumber = true) = 0; Delimiter delimiter = Delimiter::None,
bool allowResultNumber = true,
int requiredOperandCount = -1) = 0;
/// Parse a specified number of comma separated operands.
ParseResult parseOperandList(SmallVectorImpl<UnresolvedOperand> &result, ParseResult parseOperandList(SmallVectorImpl<UnresolvedOperand> &result,
Delimiter delimiter, int requiredOperandCount,
bool allowResultNumber = true) { Delimiter delimiter = Delimiter::None) {
return parseOperandList(result, /*requiredOperandCount=*/-1, delimiter, return parseOperandList(result, delimiter,
allowResultNumber); /*allowResultNumber=*/true, requiredOperandCount);
} }
/// Parse zero or more trailing SSA comma-separated trailing operand /// Parse zero or more trailing SSA comma-separated trailing operand
/// references with a specified surrounding delimiter, and an optional /// references with a specified surrounding delimiter, and an optional
/// required operand count. A leading comma is expected before the operands. /// required operand count. A leading comma is expected before the
virtual ParseResult /// operands.
parseTrailingOperandList(SmallVectorImpl<UnresolvedOperand> &result,
int requiredOperandCount = -1,
Delimiter delimiter = Delimiter::None) = 0;
ParseResult ParseResult
parseTrailingOperandList(SmallVectorImpl<UnresolvedOperand> &result, parseTrailingOperandList(SmallVectorImpl<UnresolvedOperand> &result,
Delimiter delimiter) { Delimiter delimiter = Delimiter::None) {
return parseTrailingOperandList(result, /*requiredOperandCount=*/-1, if (failed(parseOptionalComma()))
delimiter); return success(); // The comma is optional.
return parseOperandList(result, delimiter);
} }
/// Resolve an operand to an SSA value, emitting an error on failure. /// Resolve an operand to an SSA value, emitting an error on failure.
@ -1297,14 +1298,6 @@ public:
parseOptionalAssignmentListWithTypes(SmallVectorImpl<UnresolvedOperand> &lhs, parseOptionalAssignmentListWithTypes(SmallVectorImpl<UnresolvedOperand> &lhs,
SmallVectorImpl<UnresolvedOperand> &rhs, SmallVectorImpl<UnresolvedOperand> &rhs,
SmallVectorImpl<Type> &types) = 0; SmallVectorImpl<Type> &types) = 0;
private:
/// Parse either an operand list or a region argument list depending on
/// whether isOperandList is true.
ParseResult
parseOperandOrRegionArgList(SmallVectorImpl<UnresolvedOperand> &result,
bool isOperandList, int requiredOperandCount,
Delimiter delimiter);
}; };
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//

View File

@ -1075,9 +1075,9 @@ ParseResult AffineDmaStartOp::parse(OpAsmParser &parser,
return failure(); return failure();
// Parse optional stride and elements per stride. // Parse optional stride and elements per stride.
if (parser.parseTrailingOperandList(strideInfo)) { if (parser.parseTrailingOperandList(strideInfo))
return failure(); return failure();
}
if (!strideInfo.empty() && strideInfo.size() != 2) { if (!strideInfo.empty() && strideInfo.size() != 2) {
return parser.emitError(parser.getNameLoc(), return parser.emitError(parser.getNameLoc(),
"expected two stride related operands"); "expected two stride related operands");

View File

@ -530,22 +530,18 @@ parseWsLoopControl(OpAsmParser &parser, Region &region,
size_t numIVs = ivs.size(); size_t numIVs = ivs.size();
Type loopVarType; Type loopVarType;
if (parser.parseColonType(loopVarType)) if (parser.parseColonType(loopVarType) ||
return failure(); // Parse loop bounds.
parser.parseEqual() ||
// Parse loop bounds.
if (parser.parseEqual() ||
parser.parseOperandList(lowerBound, numIVs, parser.parseOperandList(lowerBound, numIVs,
OpAsmParser::Delimiter::Paren)) OpAsmParser::Delimiter::Paren) ||
return failure(); parser.parseKeyword("to") ||
if (parser.parseKeyword("to") ||
parser.parseOperandList(upperBound, numIVs, parser.parseOperandList(upperBound, numIVs,
OpAsmParser::Delimiter::Paren)) OpAsmParser::Delimiter::Paren))
return failure(); return failure();
if (succeeded(parser.parseOptionalKeyword("inclusive"))) { if (succeeded(parser.parseOptionalKeyword("inclusive")))
inclusive = UnitAttr::get(parser.getBuilder().getContext()); inclusive = UnitAttr::get(parser.getBuilder().getContext());
}
// Parse step values. // Parse step values.
if (parser.parseKeyword("step") || if (parser.parseKeyword("step") ||

View File

@ -2006,8 +2006,7 @@ ParseResult ParallelOp::parse(OpAsmParser &parser, OperationState &result) {
// Parse init values. // Parse init values.
SmallVector<OpAsmParser::UnresolvedOperand, 4> initVals; SmallVector<OpAsmParser::UnresolvedOperand, 4> initVals;
if (succeeded(parser.parseOptionalKeyword("init"))) { if (succeeded(parser.parseOptionalKeyword("init"))) {
if (parser.parseOperandList(initVals, /*requiredOperandCount=*/-1, if (parser.parseOperandList(initVals, OpAsmParser::Delimiter::Paren))
OpAsmParser::Delimiter::Paren))
return failure(); return failure();
} }

View File

@ -1297,9 +1297,9 @@ public:
/// Parse zero or more SSA comma-separated operand references with a specified /// Parse zero or more SSA comma-separated operand references with a specified
/// surrounding delimiter, and an optional required operand count. /// surrounding delimiter, and an optional required operand count.
ParseResult parseOperandList(SmallVectorImpl<UnresolvedOperand> &result, ParseResult parseOperandList(SmallVectorImpl<UnresolvedOperand> &result,
int requiredOperandCount = -1,
Delimiter delimiter = Delimiter::None, Delimiter delimiter = Delimiter::None,
bool allowResultNumber = true) override { bool allowResultNumber = true,
int requiredOperandCount = -1) override {
auto startLoc = parser.getToken().getLoc(); auto startLoc = parser.getToken().getLoc();
// The no-delimiter case has some special handling for better diagnostics. // The no-delimiter case has some special handling for better diagnostics.
@ -1339,23 +1339,6 @@ public:
return success(); return success();
} }
/// Parse zero or more trailing SSA comma-separated trailing operand
/// references with a specified surrounding delimiter, and an optional
/// required operand count. A leading comma is expected before the operands.
ParseResult
parseTrailingOperandList(SmallVectorImpl<UnresolvedOperand> &result,
int requiredOperandCount,
Delimiter delimiter) override {
if (parser.getToken().is(Token::comma)) {
parseComma();
return parseOperandList(result, requiredOperandCount, delimiter);
}
if (requiredOperandCount != -1)
return emitError(parser.getToken().getLoc(), "expected ")
<< requiredOperandCount << " operands";
return success();
}
/// Resolve an operand to an SSA value, emitting an error on failure. /// Resolve an operand to an SSA value, emitting an error on failure.
ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, ParseResult resolveOperand(const UnresolvedOperand &operand, Type type,
SmallVectorImpl<Value> &result) override { SmallVectorImpl<Value> &result) override {