diff options
author | Søren Sandmann <sandmann@redhat.com> | 2007-09-06 20:37:23 -0400 |
---|---|---|
committer | Søren Sandmann <sandmann@redhat.com> | 2007-09-06 20:37:23 -0400 |
commit | ee773c38d6dace105eeea050b2328bc4f4e3195d (patch) | |
tree | 17df72e0e5a03ef9079f3c56fe02a3b08c596f59 /switch.c | |
parent | 1a2b6b7545353b8914d35a67284574103f37b6dd (diff) |
Check for unique cases
Diffstat (limited to 'switch.c')
-rw-r--r-- | switch.c | 49 |
1 files changed, 40 insertions, 9 deletions
@@ -25,10 +25,15 @@ static int compare_cases (const void *a, const void *b) { -#if 0 const ast_case_t *case_a = *(ast_case_t **)a; const ast_case_t *case_b = *(ast_case_t **)b; + ast_type_spec_t *type_a; + ast_type_spec_t *type_b; + value_t result; + g_assert (case_a); + g_assert (case_b); + if (case_a->common.type == AST_DEFAULT_CASE && case_b->common.type == AST_DEFAULT_CASE) { @@ -44,9 +49,30 @@ compare_cases (const void *a, g_assert (case_a->common.type == AST_EXPRESSION_CASE); g_assert (case_b->common.type == AST_EXPRESSION_CASE); - return case_a->int_.value - case_b->int_.value; -#endif - return 0; + type_a = case_a->expression.expression->common.type_spec; + type_b = case_b->expression.expression->common.type_spec; + + evaluate_binary_operator (AST_EQUAL, + type_a, &case_a->expression.value, + type_b, &case_b->expression.value, + ast_type_spec_new (AST_BOOL_TYPE), &result); + + if (result.bool_val == TRUE) + { + return 0; + } + else + { + /* We only care about two things + * + * (a) that 'default' cases end up at the end, and + * (b) that identical values end up next to each other, + * + * so if they don't compare equal, just return a random, + * but consistent, value. + */ + return (char *)a - (char *)b; + } } static gboolean @@ -156,6 +182,12 @@ check_constant_case_expressions (ast_t *ast) return FALSE; } + + g_print ("constant expression evaluates to "); + print_value (&value, + ast->case_.expression.expression->common.type_spec); + + ast->case_.expression.value = value; } return TRUE; @@ -164,7 +196,6 @@ check_constant_case_expressions (ast_t *ast) static gboolean check_unique_cases (ast_t *ast) { -#if 0 GList *list; for (list = ast->common.children->head; list; list = list->next) @@ -202,15 +233,15 @@ check_unique_cases (ast_t *ast) if (case_a->common.type == AST_EXPRESSION_CASE && case_b->common.type == AST_EXPRESSION_CASE && - case_a->int_.value == case_b->int_.value) + compare_cases (&case_a, &case_b) == 0) { - g_print ("More than one case with value %d\n", - case_a->int_.value); + g_print ("More than one case with value "); + print_value (&case_a->expression.value, + case_a->expression.expression->common.type_spec); return FALSE; } } } -#endif return TRUE; } |