Skip to content

Commit 0be8938

Browse files
committed
PDLL: Fix crash when negation doesn't apply to call; prepare precedence
1 parent 2c8cec1 commit 0be8938

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

mlir/lib/Tools/PDLL/Parser/Parser.cpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ class Parser {
323323
// Exprs
324324

325325
FailureOr<ast::Expr *> parseExpr();
326+
FailureOr<ast::Expr *> parseLogicalOrExpr();
327+
FailureOr<ast::Expr *> parseLogicalAndExpr();
328+
FailureOr<ast::Expr *> parseEqualityExpr();
329+
FailureOr<ast::Expr *> parseRelationExpr();
330+
FailureOr<ast::Expr *> parseAddSubExpr();
331+
FailureOr<ast::Expr *> parseMulDivExpr();
332+
FailureOr<ast::Expr *> parseLogicalNotExpr();
333+
FailureOr<ast::Expr *> parseOtherExpr();
326334

327335
/// Identifier expressions.
328336
FailureOr<ast::Expr *> parseArrayAttrExpr();
@@ -1871,7 +1879,53 @@ FailureOr<ast::ConstraintRef> Parser::parseArgOrResultConstraint() {
18711879
//===----------------------------------------------------------------------===//
18721880
// Exprs
18731881

1874-
FailureOr<ast::Expr *> Parser::parseExpr() {
1882+
// Operator precedence follows C++:
1883+
// When parsing an expression, an operator which is listed on some rows below
1884+
// with a precedence will be bound tighter (as if by parentheses) to its
1885+
// arguments than any operator that is listed on a row further below it with a
1886+
// lower precedence. Operators that have the same precedence are bound to their
1887+
// arguments left-to-right. Highest precedence first:
1888+
// - call, member access
1889+
// - logical not
1890+
// - multipication, division, remainder
1891+
// - addition, subtraction
1892+
// - relation operators
1893+
// - equality operators
1894+
// - logical and
1895+
// - logical or
1896+
FailureOr<ast::Expr *> Parser::parseExpr() { return parseLogicalOrExpr(); }
1897+
1898+
FailureOr<ast::Expr *> Parser::parseLogicalOrExpr() {
1899+
return parseLogicalAndExpr();
1900+
}
1901+
1902+
FailureOr<ast::Expr *> Parser::parseLogicalAndExpr() {
1903+
return parseEqualityExpr();
1904+
}
1905+
1906+
FailureOr<ast::Expr *> Parser::parseEqualityExpr() {
1907+
return parseRelationExpr();
1908+
}
1909+
1910+
FailureOr<ast::Expr *> Parser::parseRelationExpr() { return parseAddSubExpr(); }
1911+
1912+
FailureOr<ast::Expr *> Parser::parseAddSubExpr() { return parseMulDivExpr(); }
1913+
1914+
FailureOr<ast::Expr *> Parser::parseMulDivExpr() {
1915+
return parseLogicalNotExpr();
1916+
}
1917+
1918+
FailureOr<ast::Expr *> Parser::parseLogicalNotExpr() {
1919+
switch (curToken.getKind()) {
1920+
case Token::kw_not:
1921+
return parseNegatedExpr();
1922+
break;
1923+
default:
1924+
return parseOtherExpr();
1925+
}
1926+
}
1927+
1928+
FailureOr<ast::Expr *> Parser::parseOtherExpr() {
18751929
if (curToken.is(Token::underscore))
18761930
return parseUnderscoreExpr();
18771931

@@ -1884,9 +1938,6 @@ FailureOr<ast::Expr *> Parser::parseExpr() {
18841938
case Token::kw_Constraint:
18851939
lhsExpr = parseInlineConstraintLambdaExpr();
18861940
break;
1887-
case Token::kw_not:
1888-
lhsExpr = parseNegatedExpr();
1889-
break;
18901941
case Token::identifier:
18911942
lhsExpr = parseIdentifierExpr();
18921943
break;
@@ -2155,6 +2206,8 @@ FailureOr<ast::Expr *> Parser::parseNegatedExpr() {
21552206
FailureOr<ast::Expr *> identifierExpr = parseIdentifierExpr();
21562207
if (failed(identifierExpr))
21572208
return failure();
2209+
if (!curToken.is(Token::l_paren))
2210+
return emitError("expected `(` after function name");
21582211
return parseCallExpr(*identifierExpr, /*isNegated = */ true);
21592212
}
21602213

mlir/test/mlir-pdll/Parser/expr-failure.pdll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ Pattern {
215215

216216
// -----
217217

218+
Pattern {
219+
// CHECK: expected native constraint
220+
not attr<"0 : i1">
221+
erase _;
222+
}
223+
224+
// -----
225+
226+
Pattern {
227+
let tuple = (attr<"3 : i34">);
228+
// CHECK: expected `(` after function name
229+
not tuple.0;
230+
erase _;
231+
}
232+
233+
// -----
234+
218235
Pattern {
219236
// CHECK: expected colon after integer literal
220237
let tuple = (10 = _: Value);

0 commit comments

Comments
 (0)