From 8a69bd70d7193253325bb94195a9e049921112d3 Mon Sep 17 00:00:00 2001 From: Søren Sandmann Date: Thu, 6 Sep 2007 19:53:21 -0400 Subject: better constant_expression --- switch.c | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) (limited to 'switch.c') diff --git a/switch.c b/switch.c index 868a4f2..305914c 100644 --- a/switch.c +++ b/switch.c @@ -50,23 +50,22 @@ compare_cases (const void *a, } static gboolean -constant_expression (ast_t *ast) +constant_expression (ast_expression_t *expr) { - GList *list; - - if (ast->common.type != AST_EXPRESSION) - return FALSE; - - switch (ast->expression.common.type) + g_assert (((ast_t *)expr)->common.type == AST_EXPRESSION); + + switch (expr->common.type) { case AST_INT_LITERAL_EXPRESSION: case AST_BOOL_LITERAL_EXPRESSION: case AST_NULL_EXPRESSION: return TRUE; - + break; + case AST_IDENTIFIER_EXPRESSION: /* FIXME: should check if it's a constant */ return FALSE; + break; case AST_LAMBDA_EXPRESSION: case AST_CALL_EXPRESSION: @@ -76,20 +75,36 @@ constant_expression (ast_t *ast) case AST_THIS_EXPRESSION: case AST_VOID_EXPRESSION: return FALSE; + break; case AST_UNARY_EXPRESSION: + if (!constant_expression (expr->unary.expr)) + return FALSE; + break; + case AST_BINARY_EXPRESSION: + if (!constant_expression (expr->binary.left)) + return FALSE; + + if (!constant_expression (expr->binary.right)) + return FALSE; + break; + case AST_TERNARY_EXPRESSION: - for (list = ast->common.children->head; list; list = list->next) - { - if (!constant_expression (list->data)) - return FALSE; - } + if (!constant_expression (expr->ternary.cond)) + return FALSE; - return TRUE; + /* FIXME: we could require only the taken branch to be constant */ + + if (!constant_expression (expr->ternary.then_expr)) + return FALSE; + + if (!constant_expression (expr->ternary.else_expr)) + return FALSE; + break; } - return FALSE; + return TRUE; } static gboolean @@ -108,7 +123,7 @@ check_constant_case_expressions (ast_t *ast) /* FIXME: check that we are dealing with a constant expression, * and store the value of that expression in the case clause */ - if (!constant_expression ((ast_t *)ast->case_.expression.expression)) + if (!constant_expression (ast->case_.expression.expression)) { g_print ("Expression in case clause must be constant\n"); -- cgit v1.2.3