Skip to content

Commit

Permalink
fix resolver bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis committed Dec 10, 2023
1 parent 2c9d728 commit bf57cb1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/immcgen/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Vector* gen_case_stmt_immcode(Immcgen* immcgen) {
immcgen->label_id++;
int case_label_id = immcgen->label_id;

Pair* label_value_pair = new_pair(&t_int, &t_iliteral);
Pair* label_value_pair = new_pair(&t_integer, &t_iliteral);
pair_set(label_value_pair, new_integer(case_label_id), iliteral_copy(case_value_srt->iliteral));
vector_push(immcgen->case_label_values, label_value_pair);

Expand Down
2 changes: 1 addition & 1 deletion src/resolver/resolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void delete_resolver(Resolver* resolver) {
delete_symboltable(resolver->symbol_table);
delete_tagtable(resolver->tag_table);
if (resolver->switch_cases != NULL) {
delete_set(resolver->switch_cases);
delete_vector(resolver->switch_cases);
}
if (resolver->return_dtype != NULL) {
delete_dtype(resolver->return_dtype);
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef struct Resolver {
SymbolTable* symbol_table;
TagTable* tag_table;
int inside_loop;
Set* switch_cases;
Vector* switch_cases;
int switch_default_exists;
DType* return_dtype;
DType* specifier_dtype;
Expand Down
25 changes: 16 additions & 9 deletions src/resolver/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,33 @@ ResolverReturn* resolve_case_stmt(Resolver* resolver) {
return new_resolverret_errors(errs);
}

vector_push(srt->children, child_srt);

if (child_srt->type != SRT_ILITERAL_EXPR || iliteral_isunsigned(child_srt->iliteral)) {
errs = new_vector(&t_error);
err = new_error("only signed integer constant is supported as case label");
vector_push(errs, err);
delete_srt(child_srt);
delete_srt(srt);
return new_resolverret_errors(errs);
}

int* case_value_integer = new_integer(child_srt->iliteral->signed_value);
if (set_contains(resolver->switch_cases, case_value_integer)) {
child_srt = perform_integer_promotion(child_srt);
vector_push(srt->children, child_srt);

IntegerLiteral* case_value_iliteral = child_srt->iliteral;
int num_cases = vector_size(resolver->switch_cases);
for (int i = 0; i < num_cases; i++) {
IntegerLiteral* iliteral = vector_at(resolver->switch_cases, i);
if (iliteral->signed_value != case_value_iliteral->signed_value) {
continue;
}
errs = new_vector(&t_error);
err = new_error("value of case label is already used in switch");
vector_push(errs, err);
delete_srt(srt);
free(case_value_integer);
return new_resolverret_errors(errs);
}

set_add(resolver->switch_cases, case_value_integer);
vector_push(resolver->switch_cases, iliteral_copy(case_value_iliteral));

resolver->ast = vector_at(ast->children, 1);
resolverret_assign(&child_srt, &errs, resolve_stmt(resolver));
Expand Down Expand Up @@ -377,6 +383,7 @@ ResolverReturn* resolve_switch_stmt(Resolver* resolver) {

child_srt = convert_to_ptr_if_array(child_srt);
child_srt = convert_to_ptr_if_function(child_srt);
child_srt = perform_integer_promotion(child_srt);
vector_push(srt->children, child_srt);

if (!dtype_isinteger(child_srt->dtype)) {
Expand All @@ -387,15 +394,15 @@ ResolverReturn* resolve_switch_stmt(Resolver* resolver) {
return new_resolverret_errors(errs);
}

Set* original_switch_cases = resolver->switch_cases;
Vector* original_switch_cases = resolver->switch_cases;
int original_switch_default_exists = resolver->switch_default_exists;
resolver->switch_cases = new_set(&t_hashable_integer);
resolver->switch_cases = new_vector(&t_iliteral);

resolver->ast = vector_at(ast->children, 1);
resolverret_assign(&child_srt, &errs, resolve_stmt(resolver));
resolver->ast = ast;

delete_set(resolver->switch_cases);
delete_vector(resolver->switch_cases);
resolver->switch_cases = original_switch_cases;
resolver->switch_default_exists = original_switch_default_exists;

Expand Down
6 changes: 3 additions & 3 deletions tests/resolver/test_statement_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ void test_resolve_switch_stmt_error_condition_child(void) {
Vector* expected = new_vector(&t_error);
vector_push(expected, new_error("identifier 'x' is used before declared"));

run_stmt_resolver_error_test(input, NULL, NULL, expected);
run_stmt_resolver_error_test(input, local_table, NULL, expected);

delete_vector(expected);
}
Expand Down Expand Up @@ -592,9 +592,9 @@ void test_resolve_switch_stmt_error_body_child(void) {
symboltable_define_memory(local_table, new_string("x"), new_integer_dtype(DTYPE_INT));

Vector* expected = new_vector(&t_error);
vector_push(expected, new_error("identifier 'x' is used before declared"));
vector_push(expected, new_error("identifier 'y' is used before declared"));

run_stmt_resolver_error_test(input, NULL, NULL, expected);
run_stmt_resolver_error_test(input, local_table, NULL, expected);

delete_vector(expected);
}
Expand Down

0 comments on commit bf57cb1

Please sign in to comment.