Skip to content

Commit 12357f1

Browse files
committed
fix: enable array compound literals to work with pointers
1 parent 2e36658 commit 12357f1

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/parser.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,7 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
20402040
opstack_push(compound_var);
20412041
add_insn(parent, *bb, OP_load_constant, compound_var, NULL,
20422042
NULL, 0, NULL);
2043+
return;
20432044
} else if (cast_or_literal_type->base_type == TYPE_struct ||
20442045
cast_or_literal_type->base_type == TYPE_typedef) {
20452046
/* Struct compound literal support (following proposed solution
@@ -2154,21 +2155,14 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
21542155
/* Store first element value for array-to-scalar */
21552156
compound_var->init_val = first_element->init_val;
21562157

2157-
/* Create result that provides first element access.
2158-
* This enables array compound literals in scalar
2159-
* contexts: int x = (int[]){1,2,3}; // x gets 1 int y
2160-
* = 5 + (int[]){10}; // adds 5 + 10
2158+
/* Return the array itself, let normal array decay handle conversion.
2159+
* This enables both scalar and pointer contexts:
2160+
* int x = (int[]){1,2,3}; // array decays to first element
2161+
* int *p = (int[]){1,2,3}; // array decays to pointer
21612162
*/
2162-
var_t *result_var = require_var(parent);
2163-
gen_name_to(result_var->var_name);
2164-
result_var->type = compound_var->type;
2165-
result_var->ptr_level = 0;
2166-
result_var->array_size = 0;
2167-
2168-
/* Read first element from the array */
2169-
add_insn(parent, *bb, OP_read, result_var, compound_var,
2170-
NULL, compound_var->type->size, NULL);
2171-
opstack_push(result_var);
2163+
compound_var->array_size = element_count;
2164+
compound_var->ptr_level = 0;
2165+
opstack_push(compound_var);
21722166
} else {
21732167
/* Single value: (int){42} - scalar compound literal */
21742168
compound_var = opstack_pop();

0 commit comments

Comments
 (0)