Unified · Split
| @@ -4536,7 +4536,7 @@ static void expr_lor(void) · 4535 unmodified lines | ||
| 4536 | 4536 | |
| 4537 | 4537 | static void expr_cond(void) |
| 4538 | 4538 | { |
| 4539 | − int tt, u, r1, r2, rc, t1, t2, bt1, bt2; | |
| 4539 | + int tt, u, r1, r2, rc, t1, t2, bt1, bt2, islv; | |
| 4540 | 4540 | SValue sv; |
| 4541 | 4541 | CType type, type1, type2; |
| 4542 | 4542 | |
| @@ -4655,10 +4655,17 @@ static void expr_cond(void) · 112 unmodified lines | ||
| 4655 | 4655 | (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) |
| 4656 | 4656 | type.t |= VT_UNSIGNED; |
| 4657 | 4657 | } |
| 4658 | − | |
| 4658 | + /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so | |
| 4659 | + that `(expr ? a : b).mem` does not error with "lvalue expected" */ | |
| 4660 | + islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE); | |
| 4661 | + | |
| 4659 | 4662 | /* now we convert second operand */ |
| 4660 | 4663 | gen_cast(&type); |
| 4661 | − if (VT_STRUCT == (vtop->type.t & VT_BTYPE)) | |
| 4664 | + if (islv) { | |
| 4665 | + mk_pointer(&vtop->type); | |
| 4666 | + gaddrof(); | |
| 4667 | + } | |
| 4668 | + else if (VT_STRUCT == (vtop->type.t & VT_BTYPE)) | |
| 4662 | 4669 | gaddrof(); |
| 4663 | 4670 | rc = RC_INT; |
| 4664 | 4671 | if (is_float(type.t)) { |
| @@ -4682,12 +4689,18 @@ static void expr_cond(void) · 17 unmodified lines | ||
| 4682 | 4689 | /* put again first value and cast it */ |
| 4683 | 4690 | *vtop = sv; |
| 4684 | 4691 | gen_cast(&type); |
| 4685 | − if (VT_STRUCT == (vtop->type.t & VT_BTYPE)) | |
| 4692 | + if (islv) { | |
| 4693 | + mk_pointer(&vtop->type); | |
| 4694 | + gaddrof(); | |
| 4695 | + } | |
| 4696 | + else if (VT_STRUCT == (vtop->type.t & VT_BTYPE)) | |
| 4686 | 4697 | gaddrof(); |
| 4687 | 4698 | r1 = gv(rc); |
| 4688 | 4699 | move_reg(r2, r1, type.t); |
| 4689 | 4700 | vtop->r = r2; |
| 4690 | 4701 | gsym(tt); |
| 4702 | + if (islv) | |
| 4703 | + indir(); | |
| 4691 | 4704 | } |
| 4692 | 4705 | } |
| 4693 | 4706 | } |