cc1: Allow struct assigments in initialization - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit dfbe125c3b0d001a9f0d671c6adfdb41e488e8cf
 (DIR) parent 11236c88e689f884619332bedbffd3501916f887
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Mon, 28 Apr 2025 10:55:23 +0200
       
       cc1: Allow struct assigments in initialization
       
       Currently, the code was only supporting initializers for struct
       but it should support also arbitrary expressions emitting
       the correct assignment statement.
       
       Diffstat:
         M src/cmd/scc-cc/cc1/init.c           |      34 ++++++++++++++++---------------
         M tests/cc/execute/scc-tests.lst      |       3 ++-
       
       2 files changed, 20 insertions(+), 17 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/scc-cc/cc1/init.c b/src/cmd/scc-cc/cc1/init.c
       @@ -6,6 +6,9 @@
        #include <scc/scc.h>
        #include "cc1.h"
        
       +#define NOLIST 0
       +#define INLIST 1
       +
        
        typedef struct init Init;
        
       @@ -111,7 +114,7 @@ str2ary(Type *tp)
        }
        
        static Node *
       -initialize(Type *tp)
       +initialize(Type *tp, int inlist)
        {
                Node *np;
                Symbol *sym;
       @@ -119,7 +122,7 @@ initialize(Type *tp)
                if (tp->op == ARY && yytoken == STRING)
                        return str2ary(tp);
        
       -        if (yytoken == '{' || tp->op == STRUCT || tp->op == ARY)
       +        if (yytoken == '{' || inlist && (tp->op == STRUCT || tp->op == ARY))
                        return initlist(tp);
        
                np = assign();
       @@ -277,7 +280,7 @@ initlist_helper(Type *tp)
                                        outbound = 1;
                                        break;
                                }
       -                        np = initialize(curtp);
       +                        np = initialize(curtp, INLIST);
                                if (outbound) {
                                        freetree(np);
                                        np = NULL;
       @@ -343,18 +346,17 @@ repeat:
                        goto repeat;
                case ARY:
                case STRUCT:
       -                if (!(np->flags & NCONST))
       -                        abort(); /* TODO */
       -                hidden = newsym(NS_IDEN, NULL);
       -                hidden->id = newid();
       -                hidden->type = sym->type;
       -                hidden->flags |= SLOCAL | SHASINIT;
       -                emit(ODECL, hidden);
       -                emit(OINIT, np);
       -                emit(ODECL, sym);
       -                emit(OEXPR,
       -                     node(OASSIGN, tp, varnode(sym), varnode(hidden)));
       -                break;
       +                if (np->op == OSYM && np->sym->flags & SINITLST) {
       +                        if (!(np->flags & NCONST))
       +                                abort(); /* TODO */
       +                        hidden = newsym(NS_IDEN, NULL);
       +                        hidden->id = newid();
       +                        hidden->type = sym->type;
       +                        hidden->flags |= SLOCAL | SHASINIT;
       +                        emit(ODECL, hidden);
       +                        emit(OINIT, np);
       +                        np = varnode(hidden);
       +                }
                default:
                        emit(ODECL, sym);
                        np = node(OASSIGN, tp, varnode(sym), np);
       @@ -374,7 +376,7 @@ initializer(Symbol *sym, Type *tp)
                               sym->name);
                        tp = inttype;
                }
       -        np = initialize(tp);
       +        np = initialize(tp, NOLIST);
        
                if (flags & SDEFINED) {
                        errorp("redeclaration of '%s'", sym->name);
 (DIR) diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst
       @@ -119,7 +119,7 @@
        0126-macropar.c
        0127-doublecte.c [TODO]
        0128-kr_names.c
       -0129-initi.c [TODO]
       +0129-initi.c
        0130-mulpars.c
        0131-hello.c
        0132-forward.c
       @@ -221,3 +221,4 @@
        0228-extrcast.c
        0229-commalog.c
        0230-init.c
       +0231-init.c