From e933eb8fae7417a0576b88cde458d80986aad8e3 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 14 Sep 2024 08:31:26 +0900 Subject: [PATCH] [qfcc] Clean up expr parenthesis setting Thare are still many const casts, but this does get rid of one set. I had tried to replace the paren flag with a () unary expression, but that brought out a whole pile of places that had problems (especially anything to do with boolean expressions). --- tools/qfcc/include/expr.h | 2 ++ tools/qfcc/source/expr.c | 18 +++++++++++------- tools/qfcc/source/expr_assign.c | 2 +- tools/qfcc/source/expr_binary.c | 18 ++++++------------ 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/tools/qfcc/include/expr.h b/tools/qfcc/include/expr.h index d41a7fa59..134fd149b 100644 --- a/tools/qfcc/include/expr.h +++ b/tools/qfcc/include/expr.h @@ -545,6 +545,8 @@ expr_t *new_binary_expr (int op, const expr_t *e1, const expr_t *e2); */ expr_t *new_unary_expr (int op, const expr_t *e1); +const expr_t *paren_expr (const expr_t *e); + /** Create a new horizontal vector operantion node. If \a vec is an error expression, then it will be returned instead of a diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index e166dbb94..b538f048c 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -607,6 +607,15 @@ new_unary_expr (int op, const expr_t *e1) return e; } +const expr_t * +paren_expr (const expr_t *e) +{ + auto paren = new_expr (); + *paren = *e; + paren->paren = 1; + return paren; +} + expr_t * new_horizontal_expr (int op, const expr_t *vec, type_t *type) { @@ -1929,13 +1938,8 @@ asx_expr (int op, const expr_t *e1, const expr_t *e2) else if (e2->type == ex_error) return e2; else { - expr_t *e = new_expr (); - auto paren = new_expr (); - - *e = *e1; - *paren = *e2; - paren->paren = 1; - return assign_expr (e, binary_expr (op, e1, paren)); + e2 = paren_expr (e2); + return assign_expr (e1, binary_expr (op, e1, e2)); } } diff --git a/tools/qfcc/source/expr_assign.c b/tools/qfcc/source/expr_assign.c index 37462157e..e691e6ef7 100644 --- a/tools/qfcc/source/expr_assign.c +++ b/tools/qfcc/source/expr_assign.c @@ -75,7 +75,7 @@ check_assign_logic_precedence (const expr_t *dst, const expr_t *src) // change {a = (b logic c)} to {(a = b) logic c} auto assignment = assign_expr (dst, src->expr.e1); // protect assignment from binary_expr - ((expr_t *) assignment)->paren = 1; + assignment = paren_expr (assignment); return binary_expr (src->expr.op, assignment, src->expr.e2); } return 0; diff --git a/tools/qfcc/source/expr_binary.c b/tools/qfcc/source/expr_binary.c index 5e673206b..f7802a64f 100644 --- a/tools/qfcc/source/expr_binary.c +++ b/tools/qfcc/source/expr_binary.c @@ -1177,12 +1177,6 @@ reimplement_binary_expr (int op, const expr_t *e1, const expr_t *e2) return 0; } -static void -set_paren (const expr_t *e) -{ - ((expr_t *) e)->paren = 1; -} - static const expr_t * check_precedence (int op, const expr_t *e1, const expr_t *e2) { @@ -1191,8 +1185,8 @@ check_precedence (int op, const expr_t *e1, const expr_t *e2) if (op != QC_AND && op != QC_OR && op != '=') { notice (e1, "precedence of `!' and `%s' inverted for " "traditional code", get_op_string (op)); - set_paren (e1->expr.e1); - return unary_expr ('!', binary_expr (op, e1->expr.e1, e2)); + e1 = paren_expr (e1->expr.e1); + return unary_expr ('!', binary_expr (op, e1, e2)); } } else if (op == '&' || op == '|') { if (options.warnings.precedence) @@ -1211,7 +1205,7 @@ check_precedence (int op, const expr_t *e1, const expr_t *e2) "traditional code", get_op_string (op), get_op_string (e2->expr.op)); e1 = binary_expr (op, e1, e2->expr.e1); - set_paren (e1); + e1 = paren_expr (e1); return binary_expr (e2->expr.op, e1, e2->expr.e2); } if (((op == QC_EQ || op == QC_NE) && is_compare (e2->expr.op)) @@ -1221,7 +1215,7 @@ check_precedence (int op, const expr_t *e1, const expr_t *e2) "traditional code", get_op_string (op), get_op_string (e2->expr.op)); e1 = binary_expr (op, e1, e2->expr.e1); - set_paren (e1); + e1 = paren_expr (e1); return binary_expr (e2->expr.op, e1, e2->expr.e2); } } else if (e1->type == ex_expr && !e1->paren) { @@ -1233,8 +1227,8 @@ check_precedence (int op, const expr_t *e1, const expr_t *e2) "traditional code", get_op_string (op), get_op_string (e1->expr.op)); e2 = binary_expr (op, e1->expr.e2, e2); - set_paren (e1); - return binary_expr (e1->expr.op, e1->expr.e1, e2); + e1 = paren_expr (e1->expr.e1); + return binary_expr (e1->expr.op, e1, e2); } } } else {