Skip to content

Commit 3c07644

Browse files
committed
fix: enable array compound literals to work with pointers
1 parent 3336ed7 commit 3c07644

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
@@ -2143,6 +2143,7 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
21432143
opstack_push(compound_var);
21442144
add_insn(parent, *bb, OP_load_constant, compound_var, NULL,
21452145
NULL, 0, NULL);
2146+
return;
21462147
} else if (cast_or_literal_type->base_type == TYPE_struct ||
21472148
cast_or_literal_type->base_type == TYPE_typedef) {
21482149
/* Struct compound literal support (following proposed solution
@@ -2258,21 +2259,14 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
22582259
/* Store first element value for array-to-scalar */
22592260
compound_var->init_val = first_element->init_val;
22602261

2261-
/* Create result that provides first element access.
2262-
* This enables array compound literals in scalar
2263-
* contexts: int x = (int[]){1,2,3}; // x gets 1 int y
2264-
* = 5 + (int[]){10}; // adds 5 + 10
2262+
/* Return the array itself, let normal array decay handle conversion.
2263+
* This enables both scalar and pointer contexts:
2264+
* int x = (int[]){1,2,3}; // array decays to first element
2265+
* int *p = (int[]){1,2,3}; // array decays to pointer
22652266
*/
2266-
var_t *result_var = require_var(parent);
2267-
gen_name_to(result_var->var_name);
2268-
result_var->type = compound_var->type;
2269-
result_var->ptr_level = 0;
2270-
result_var->array_size = 0;
2271-
2272-
/* Read first element from the array */
2273-
add_insn(parent, *bb, OP_read, result_var, compound_var,
2274-
NULL, compound_var->type->size, NULL);
2275-
opstack_push(result_var);
2267+
compound_var->array_size = element_count;
2268+
compound_var->ptr_level = 0;
2269+
opstack_push(compound_var);
22762270
} else {
22772271
/* Single value: (int){42} - scalar compound literal */
22782272
compound_var = opstack_pop();

0 commit comments

Comments
 (0)