kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

init_aggregate_field_from_call.c (1043B)


      1 /* An aggregate-typed field initialized by a struct-returning call inside a
      2  * braced initializer: `Wrap w = { mk(a), t };`. The initializer's
      3  * type-compatibility probe parses the call in an unevaluated context, where the
      4  * call result must still be pushed as a PLACE (an aggregate can never be a value
      5  * on the CG stack). Pushing it as a value tripped "aggregate must be a place,
      6  * not a value" while compiling src/arch/riscv/isa.c (a compound literal whose
      7  * first field is a KitSlice returned by slice_from_cstr). Returns a checksum of
      8  * the copied fields (expect 120). */
      9 
     10 typedef struct {
     11   int x, y;
     12 } Pair;
     13 
     14 typedef struct {
     15   Pair p;
     16   int tag;
     17 } Wrap;
     18 
     19 __attribute__((noinline)) static Pair mk(int a) {
     20   Pair r;
     21   r.x = a;
     22   r.y = a * 2;
     23   return r;
     24 }
     25 
     26 __attribute__((noinline)) static Wrap build(int a, int t) {
     27   Wrap w = {mk(a), t}; /* aggregate field <- struct-returning call */
     28   return w;
     29 }
     30 
     31 int test_main(void) {
     32   Wrap w = build(7, 99);
     33   /* x=7, y=14, tag=99 -> 7 + 14 + 99 = 120 */
     34   return w.p.x + w.p.y + w.tag;
     35 }