summaryrefslogtreecommitdiff
path: root/switch.c
diff options
context:
space:
mode:
authorSøren Sandmann <sandmann@redhat.com>2007-09-06 19:53:21 -0400
committerSøren Sandmann <sandmann@redhat.com>2007-09-06 19:53:21 -0400
commit8a69bd70d7193253325bb94195a9e049921112d3 (patch)
tree6795ee4b4faf64ed3339a9ff85abd5fb0c7f43a0 /switch.c
parent095e37313db3c71cbb97d5eee785d18f65a26172 (diff)
better constant_expression
Diffstat (limited to 'switch.c')
-rw-r--r--switch.c47
1 files changed, 31 insertions, 16 deletions
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");