[BACK]Return to init.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / xlint / lint1

Annotation of src/usr.bin/xlint/lint1/init.c, Revision 1.204

1.204   ! rillig      1: /*     $NetBSD: init.c,v 1.203 2021/07/20 19:44:36 rillig Exp $        */
1.2       cgd         2:
1.1       cgd         3: /*
                      4:  * Copyright (c) 1994, 1995 Jochen Pohl
1.179     rillig      5:  * Copyright (c) 2021 Roland Illig
1.1       cgd         6:  * All Rights Reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *      This product includes software developed by Jochen Pohl for
                     19:  *     The NetBSD Project.
                     20:  * 4. The name of the author may not be used to endorse or promote products
                     21:  *    derived from this software without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     24:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     25:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     26:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     27:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     28:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     29:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     30:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     31:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     32:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     33:  */
                     34:
1.18      jmc        35: #if HAVE_NBTOOL_CONFIG_H
                     36: #include "nbtool_config.h"
                     37: #endif
                     38:
1.5       christos   39: #include <sys/cdefs.h>
1.10      tv         40: #if defined(__RCSID) && !defined(lint)
1.204   ! rillig     41: __RCSID("$NetBSD: init.c,v 1.203 2021/07/20 19:44:36 rillig Exp $");
1.1       cgd        42: #endif
                     43:
                     44: #include <stdlib.h>
1.17      thorpej    45: #include <string.h>
1.1       cgd        46:
                     47: #include "lint1.h"
                     48:
1.53      rillig     49:
                     50: /*
1.179     rillig     51:  * Initialization of global or local objects, like in:
1.93      rillig     52:  *
                     53:  *     int number = 12345;
                     54:  *     int number_with_braces = { 12345 };
                     55:  *
                     56:  *     int array_of_unknown_size[] = { 111, 222, 333 };
                     57:  *     int array_flat[2][2] = { 11, 12, 21, 22 };
                     58:  *     int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
                     59:  *
                     60:  *     struct { int x, y; } point = { 3, 4 };
1.119     rillig     61:  *     struct { int x, y; } point = { .y = 4, .x = 3 };
1.93      rillig     62:  *
1.119     rillig     63:  * Any scalar expression in the initializer may be surrounded by arbitrarily
                     64:  * many extra pairs of braces, like in the example 'number_with_braces' (C99
                     65:  * 6.7.8p11).
                     66:  *
                     67:  * For multi-dimensional arrays, the inner braces may be omitted like in
1.179     rillig     68:  * array_flat or spelled out like in array_nested.  This is unusual in
                     69:  * practice and therefore only supported very basically.
1.93      rillig     70:  *
1.198     rillig     71:  * During an initialization, the grammar parser calls these functions:
1.93      rillig     72:  *
1.119     rillig     73:  *     begin_initialization
                     74:  *             init_lbrace                     for each '{'
1.157     rillig     75:  *             add_designator_member           for each '.member' before '='
                     76:  *             add_designator_subscript        for each '[123]' before '='
1.179     rillig     77:  *             init_expr                       for each expression
1.119     rillig     78:  *             init_rbrace                     for each '}'
                     79:  *     end_initialization
1.93      rillig     80:  *
1.139     rillig     81:  * Each '{' begins a new brace level, each '}' ends the current brace level.
1.198     rillig     82:  * Each brace level has an associated "current object", which is the starting
                     83:  * point for resolving the optional designations such as '.member[3]'.
1.93      rillig     84:  *
                     85:  * See also:
                     86:  *     C99 6.7.8 "Initialization"
                     87:  *     d_c99_init.c for more examples
                     88:  */
                     89:
1.55      rillig     90: /*
1.135     rillig     91:  * A single component on the path to the sub-object that is initialized by an
                     92:  * initializer expression.  Either a struct or union member, or an array
                     93:  * subscript.
1.55      rillig     94:  *
1.179     rillig     95:  * C99 6.7.8p6, 6.7.8p7
1.128     rillig     96:  */
1.140     rillig     97: struct designator {
1.179     rillig     98:        const char      *dr_name;       /* for struct and union */
                     99:        size_t          dr_subscript;   /* for array */
1.176     rillig    100:        struct designator *dr_next;
1.140     rillig    101: };
1.128     rillig    102:
                    103: /*
1.135     rillig    104:  * The optional designation for an initializer, saying which sub-object to
                    105:  * initialize.  Examples for designations are '.member' or
                    106:  * '.member[123].member.member[1][1]'.
                    107:  *
1.179     rillig    108:  * C99 6.7.8p6, 6.7.8p7
1.55      rillig    109:  */
1.140     rillig    110: struct designation {
1.176     rillig    111:        struct designator *dn_head;
                    112:        struct designator *dn_tail;
1.140     rillig    113: };
1.53      rillig    114:
1.179     rillig    115: /*
                    116:  * Describes a single brace level of an ongoing initialization.
                    117:  *
                    118:  * See C99 6.7.8p17.
                    119:  */
                    120: struct brace_level {
1.201     rillig    121:        /*
                    122:         * The type of the current object that is initialized at this brace
                    123:         * level.
                    124:         */
                    125:        const type_t    *bl_type;
1.193     rillig    126:
                    127:        struct designation bl_designation;      /* .member[123].member */
1.192     rillig    128:
1.201     rillig    129:        /*
                    130:         * The next member of the struct or union that is to be initialized,
                    131:         * unless a specific member is selected by a designator.
                    132:         */
                    133:        const sym_t     *bl_member;
                    134:        /*
                    135:         * The subscript of the next array element that is to be initialized,
                    136:         * unless a specific subscript is selected by a designator.
                    137:         */
                    138:        size_t          bl_subscript;
                    139:        /*
                    140:         * The maximum subscript that has ever be seen; only relevant for an
                    141:         * array of unknown size at the outermost brace level.
                    142:         */
                    143:        size_t          bl_max_subscript;
1.179     rillig    144:        bool            bl_scalar_done: 1;      /* for scalars */
1.185     rillig    145:        bool            bl_confused: 1;         /* skip further checks */
1.192     rillig    146:
1.179     rillig    147:        struct brace_level *bl_enclosing;
                    148: };
                    149:
1.198     rillig    150: /*
                    151:  * An ongoing initialization.
                    152:  *
                    153:  * In most cases there is only ever a single initialization going on.  See
                    154:  * pointer_to_compound_literal in msg_171.c for an exception.
                    155:  */
1.110     rillig    156: struct initialization {
1.193     rillig    157:        /* The symbol that is to be initialized. */
                    158:        sym_t           *in_sym;
                    159:
                    160:        /* The innermost brace level. */
                    161:        struct brace_level *in_brace_level;
                    162:
1.110     rillig    163:        /*
1.179     rillig    164:         * Is set as soon as a fatal error occurred in the initialization.
1.111     rillig    165:         * The effect is that the rest of the initialization is ignored
                    166:         * (parsed by yacc, expression trees built, but no initialization
                    167:         * takes place).
1.110     rillig    168:         */
1.176     rillig    169:        bool            in_err;
1.110     rillig    170:
1.179     rillig    171:        struct initialization *in_enclosing;
1.110     rillig    172: };
1.1       cgd       173:
1.145     rillig    174:
                    175: #ifdef DEBUG
1.179     rillig    176: static int debug_indentation = 0;
1.145     rillig    177: #endif
                    178:
                    179:
                    180: #ifdef DEBUG
                    181:
                    182: static void __printflike(1, 2)
                    183: debug_printf(const char *fmt, ...)
                    184: {
                    185:        va_list va;
                    186:
                    187:        va_start(va, fmt);
                    188:        vfprintf(stdout, fmt, va);
                    189:        va_end(va);
                    190: }
                    191:
                    192: static void
                    193: debug_indent(void)
                    194: {
1.175     rillig    195:
1.179     rillig    196:        debug_printf("%*s", 2 * debug_indentation, "");
1.145     rillig    197: }
                    198:
                    199: static void
                    200: debug_enter(const char *func)
                    201: {
1.175     rillig    202:
1.179     rillig    203:        printf("%*s+ %s\n", 2 * debug_indentation++, "", func);
1.145     rillig    204: }
                    205:
                    206: static void __printflike(1, 2)
                    207: debug_step(const char *fmt, ...)
                    208: {
                    209:        va_list va;
                    210:
                    211:        debug_indent();
                    212:        va_start(va, fmt);
                    213:        vfprintf(stdout, fmt, va);
                    214:        va_end(va);
                    215:        printf("\n");
                    216: }
1.186     rillig    217: #define debug_step0            debug_step
                    218: #define debug_step1            debug_step
                    219: #define debug_step2            debug_step
1.145     rillig    220:
                    221: static void
                    222: debug_leave(const char *func)
                    223: {
1.175     rillig    224:
1.179     rillig    225:        printf("%*s- %s\n", 2 * --debug_indentation, "", func);
1.145     rillig    226: }
                    227:
1.175     rillig    228: #define debug_enter()          (debug_enter)(__func__)
                    229: #define debug_leave()          (debug_leave)(__func__)
1.151     rillig    230:
1.145     rillig    231: #else
                    232:
                    233: #define debug_indent()         do { } while (false)
1.151     rillig    234: #define debug_enter()          do { } while (false)
1.198     rillig    235: #define debug_step0(fmt)       do { } while (false)
1.186     rillig    236: #define debug_step1(fmt, arg0) do { } while (false)
                    237: #define debug_step2(fmt, arg1, arg2) do { } while (false)
1.151     rillig    238: #define debug_leave()          do { } while (false)
1.145     rillig    239:
                    240: #endif
                    241:
1.1       cgd       242:
1.175     rillig    243: static void *
                    244: unconst_cast(const void *p)
                    245: {
                    246:        void *r;
                    247:
1.190     rillig    248:        memcpy(&r, &p, sizeof(r));
1.175     rillig    249:        return r;
                    250: }
                    251:
1.177     rillig    252: static bool
                    253: has_automatic_storage_duration(const sym_t *sym)
                    254: {
                    255:
                    256:        return sym->s_scl == AUTO || sym->s_scl == REG;
                    257: }
                    258:
                    259: /* C99 6.7.8p14, 6.7.8p15 */
                    260: static bool
                    261: is_string_array(const type_t *tp, tspec_t t)
                    262: {
                    263:        tspec_t st;
                    264:
                    265:        if (tp == NULL || tp->t_tspec != ARRAY)
                    266:                return false;
                    267:
                    268:        st = tp->t_subt->t_tspec;
                    269:        return t == CHAR
                    270:            ? st == CHAR || st == UCHAR || st == SCHAR
                    271:            : st == WCHAR;
                    272: }
                    273:
                    274: /* C99 6.7.8p9 */
                    275: static bool
1.179     rillig    276: is_unnamed(const sym_t *m)
1.177     rillig    277: {
                    278:
                    279:        return m->s_bitfield && m->s_name == unnamed;
                    280: }
                    281:
1.179     rillig    282: /* C99 6.7.8p9 */
                    283: static const sym_t *
                    284: skip_unnamed(const sym_t *m)
                    285: {
                    286:
                    287:        while (m != NULL && is_unnamed(m))
                    288:                m = m->s_next;
                    289:        return m;
                    290: }
                    291:
1.177     rillig    292: static const sym_t *
1.179     rillig    293: first_named_member(const type_t *tp)
1.177     rillig    294: {
                    295:
1.179     rillig    296:        lint_assert(is_struct_or_union(tp->t_tspec));
                    297:        return skip_unnamed(tp->t_str->sou_first_member);
                    298: }
                    299:
                    300: static const sym_t *
                    301: look_up_member(const type_t *tp, const char *name)
                    302: {
                    303:        const sym_t *m;
                    304:
                    305:        lint_assert(is_struct_or_union(tp->t_tspec));
                    306:        for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next)
                    307:                if (!is_unnamed(m) && strcmp(m->s_name, name) == 0)
1.177     rillig    308:                        return m;
                    309:        return NULL;
                    310: }
                    311:
1.179     rillig    312: static const type_t *
                    313: sym_type(const sym_t *sym)
                    314: {
                    315:
                    316:        return sym != NULL ? sym->s_type : NULL;
                    317: }
                    318:
                    319: static const type_t *
                    320: look_up_member_type(const type_t *tp, const char *name)
                    321: {
                    322:        const sym_t *member;
                    323:
                    324:        member = look_up_member(tp, name);
                    325:        if (member == NULL) {
1.182     rillig    326:                /* type '%s' does not have member '%s' */
                    327:                error(101, type_name(tp), name);
1.179     rillig    328:        }
                    329:
                    330:        return sym_type(member);
                    331: }
                    332:
1.198     rillig    333: /*
                    334:  * C99 6.7.8p22 says that the type of an array of unknown size becomes known
                    335:  * at the end of its initializer list.
                    336:  */
1.179     rillig    337: static void
                    338: update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
                    339: {
                    340:        type_t *tp;
                    341:
1.189     rillig    342:        tp = dup_type(sym->s_type);
1.179     rillig    343:        tp->t_dim = (int)size;
                    344:        tp->t_incomplete_array = false;
                    345:        sym->s_type = tp;
                    346: }
                    347:
1.167     rillig    348:
1.158     rillig    349: /* In traditional C, bit-fields can be initialized only by integer constants. */
                    350: static void
                    351: check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
                    352: {
1.175     rillig    353:
1.158     rillig    354:        if (tflag &&
                    355:            is_integer(lt) &&
                    356:            ln->tn_type->t_bitfield &&
                    357:            !is_integer(rt)) {
                    358:                /* bit-field initialization is illegal in traditional C */
                    359:                warning(186);
                    360:        }
                    361: }
                    362:
                    363: static void
1.177     rillig    364: check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
1.158     rillig    365: {
1.177     rillig    366:        const sym_t *unused_sym;
                    367:        ptrdiff_t unused_offs;
1.175     rillig    368:
1.158     rillig    369:        if (tn == NULL || tn->tn_op == CON)
                    370:                return;
                    371:
1.177     rillig    372:        if (constant_addr(tn, &unused_sym, &unused_offs))
1.158     rillig    373:                return;
                    374:
1.177     rillig    375:        if (has_automatic_storage_duration(sym)) {
1.158     rillig    376:                /* non-constant initializer */
                    377:                c99ism(177);
                    378:        } else {
                    379:                /* non-constant initializer */
                    380:                error(177);
                    381:        }
                    382: }
                    383:
                    384: static void
1.197     rillig    385: check_trad_no_auto_aggregate(const sym_t *sym)
1.179     rillig    386: {
                    387:
1.197     rillig    388:        if (has_automatic_storage_duration(sym) &&
1.179     rillig    389:            !is_scalar(sym->s_type->t_tspec)) {
                    390:                /* no automatic aggregate initialization in trad. C */
                    391:                warning(188);
                    392:        }
                    393: }
                    394:
                    395: static void
1.177     rillig    396: check_init_expr(const type_t *tp, sym_t *sym, tnode_t *tn)
1.158     rillig    397: {
                    398:        tnode_t *ln;
1.200     rillig    399:        type_t *ltp;
1.158     rillig    400:        tspec_t lt, rt;
1.187     rillig    401:        struct memory_block *tmem;
1.158     rillig    402:
1.204   ! rillig    403:        ltp = expr_unqualified_type(tp);
1.200     rillig    404:
1.158     rillig    405:        /* Create a temporary node for the left side. */
1.190     rillig    406:        ln = expr_zalloc(sizeof(*ln));
1.158     rillig    407:        ln->tn_op = NAME;
1.200     rillig    408:        ln->tn_type = ltp;
1.158     rillig    409:        ln->tn_lvalue = true;
                    410:        ln->tn_sym = sym;
                    411:
                    412:        tn = cconv(tn);
                    413:
                    414:        lt = ln->tn_type->t_tspec;
                    415:        rt = tn->tn_type->t_tspec;
                    416:
1.186     rillig    417:        debug_step2("typeok '%s', '%s'",
1.158     rillig    418:            type_name(ln->tn_type), type_name(tn->tn_type));
                    419:        if (!typeok(INIT, 0, ln, tn))
                    420:                return;
                    421:
                    422:        /*
                    423:         * Preserve the tree memory. This is necessary because otherwise
                    424:         * expr() would free it.
                    425:         */
1.188     rillig    426:        tmem = expr_save_memory();
1.158     rillig    427:        expr(tn, true, false, true, false);
1.188     rillig    428:        expr_restore_memory(tmem);
1.158     rillig    429:
                    430:        check_bit_field_init(ln, lt, rt);
                    431:
                    432:        /*
                    433:         * XXX: Is it correct to do this conversion _after_ the typeok above?
                    434:         */
                    435:        if (lt != rt || (tp->t_bitfield && tn->tn_op == CON))
1.175     rillig    436:                tn = convert(INIT, 0, unconst_cast(tp), tn);
1.158     rillig    437:
1.177     rillig    438:        check_non_constant_initializer(tn, sym);
1.158     rillig    439: }
                    440:
                    441:
1.159     rillig    442: static struct designator *
1.179     rillig    443: designator_new(const char *name, size_t subscript)
1.159     rillig    444: {
1.176     rillig    445:        struct designator *dr;
                    446:
1.190     rillig    447:        dr = xcalloc(1, sizeof(*dr));
1.176     rillig    448:        dr->dr_name = name;
1.179     rillig    449:        dr->dr_subscript = subscript;
1.176     rillig    450:        return dr;
1.159     rillig    451: }
                    452:
                    453: static void
1.176     rillig    454: designator_free(struct designator *dr)
1.159     rillig    455: {
1.175     rillig    456:
1.176     rillig    457:        free(dr);
1.159     rillig    458: }
                    459:
                    460:
1.179     rillig    461: static const type_t *
                    462: designator_look_up(const struct designator *dr, const type_t *tp)
                    463: {
                    464:        switch (tp->t_tspec) {
                    465:        case STRUCT:
                    466:        case UNION:
                    467:                if (dr->dr_name == NULL) {
                    468:                        /* syntax error '%s' */
                    469:                        error(249, "designator '[...]' is only for arrays");
                    470:                        return sym_type(first_named_member(tp));
                    471:                }
                    472:
                    473:                return look_up_member_type(tp, dr->dr_name);
                    474:        case ARRAY:
                    475:                if (dr->dr_name != NULL) {
                    476:                        /* syntax error '%s' */
                    477:                        error(249,
1.181     rillig    478:                            "designator '.member' is only for struct/union");
1.179     rillig    479:                }
1.183     rillig    480:                if (!tp->t_incomplete_array &&
                    481:                    dr->dr_subscript >= (size_t)tp->t_dim) {
                    482:                        /* array subscript cannot be > %d: %ld */
                    483:                        error(168, tp->t_dim - 1, (long)dr->dr_subscript);
                    484:                }
1.179     rillig    485:                return tp->t_subt;
                    486:        default:
                    487:                /* syntax error '%s' */
                    488:                error(249, "scalar type cannot use designator");
                    489:                return tp;
                    490:        }
                    491: }
                    492:
                    493:
1.159     rillig    494: #ifdef DEBUG
                    495: static void
                    496: designation_debug(const struct designation *dn)
                    497: {
1.176     rillig    498:        const struct designator *dr;
1.159     rillig    499:
1.176     rillig    500:        if (dn->dn_head == NULL)
1.159     rillig    501:                return;
                    502:
                    503:        debug_indent();
                    504:        debug_printf("designation: ");
1.179     rillig    505:        for (dr = dn->dn_head; dr != NULL; dr = dr->dr_next) {
                    506:                if (dr->dr_name != NULL) {
                    507:                        debug_printf(".%s", dr->dr_name);
                    508:                        lint_assert(dr->dr_subscript == 0);
                    509:                } else
                    510:                        debug_printf("[%zu]", dr->dr_subscript);
                    511:        }
1.159     rillig    512:        debug_printf("\n");
                    513: }
                    514: #else
                    515: #define designation_debug(dn) do { } while (false)
                    516: #endif
                    517:
                    518: static void
1.179     rillig    519: designation_add(struct designation *dn, const char *name, size_t subscript)
1.159     rillig    520: {
1.179     rillig    521:        struct designator *dr;
                    522:
                    523:        dr = designator_new(name, subscript);
1.159     rillig    524:
1.176     rillig    525:        if (dn->dn_head != NULL) {
                    526:                dn->dn_tail->dr_next = dr;
                    527:                dn->dn_tail = dr;
1.159     rillig    528:        } else {
1.176     rillig    529:                dn->dn_head = dr;
                    530:                dn->dn_tail = dr;
1.159     rillig    531:        }
                    532: }
                    533:
                    534: /*
1.179     rillig    535:  * Starting at the type of the current object, resolve the type of the
                    536:  * sub-object by following each designator in the list.
1.198     rillig    537:  *
                    538:  * C99 6.7.8p18
1.159     rillig    539:  */
1.179     rillig    540: static const type_t *
                    541: designation_look_up(const struct designation *dn, const type_t *tp)
                    542: {
                    543:        const struct designator *dr;
                    544:
                    545:        for (dr = dn->dn_head; dr != NULL && tp != NULL; dr = dr->dr_next)
                    546:                tp = designator_look_up(dr, tp);
                    547:        return tp;
                    548: }
                    549:
1.159     rillig    550: static void
1.179     rillig    551: designation_reset(struct designation *dn)
1.159     rillig    552: {
1.179     rillig    553:        struct designator *dr, *next;
1.159     rillig    554:
1.179     rillig    555:        for (dr = dn->dn_head; dr != NULL; dr = next) {
                    556:                next = dr->dr_next;
                    557:                designator_free(dr);
1.159     rillig    558:        }
1.184     rillig    559:
                    560:        dn->dn_head = NULL;
                    561:        dn->dn_tail = NULL;
1.159     rillig    562: }
                    563:
                    564:
1.152     rillig    565: static struct brace_level *
1.179     rillig    566: brace_level_new(const type_t *tp, struct brace_level *enclosing)
1.152     rillig    567: {
1.179     rillig    568:        struct brace_level *bl;
1.152     rillig    569:
1.179     rillig    570:        bl = xcalloc(1, sizeof(*bl));
                    571:        bl->bl_type = tp;
1.178     rillig    572:        bl->bl_enclosing = enclosing;
1.179     rillig    573:        if (is_struct_or_union(tp->t_tspec))
1.192     rillig    574:                bl->bl_member = first_named_member(tp);
1.152     rillig    575:
1.178     rillig    576:        return bl;
1.152     rillig    577: }
                    578:
                    579: static void
1.178     rillig    580: brace_level_free(struct brace_level *bl)
1.152     rillig    581: {
1.179     rillig    582:
                    583:        designation_reset(&bl->bl_designation);
1.178     rillig    584:        free(bl);
1.152     rillig    585: }
                    586:
1.146     rillig    587: #ifdef DEBUG
1.78      rillig    588: static void
1.178     rillig    589: brace_level_debug(const struct brace_level *bl)
1.74      rillig    590: {
1.78      rillig    591:
1.179     rillig    592:        lint_assert(bl->bl_type != NULL);
1.192     rillig    593:        lint_assert(bl->bl_member == NULL || !is_unnamed(bl->bl_member));
1.179     rillig    594:
                    595:        debug_printf("type '%s'", type_name(bl->bl_type));
                    596:
1.192     rillig    597:        if (is_struct_or_union(bl->bl_type->t_tspec) && bl->bl_member != NULL)
                    598:                debug_printf(", member '%s'", bl->bl_member->s_name);
1.179     rillig    599:        if (bl->bl_type->t_tspec == ARRAY)
1.192     rillig    600:                debug_printf(", subscript %zu", bl->bl_subscript);
1.78      rillig    601:
1.179     rillig    602:        debug_printf("\n");
1.74      rillig    603: }
1.146     rillig    604: #else
1.179     rillig    605: #define brace_level_debug(level) do { } while (false)
1.146     rillig    606: #endif
                    607:
1.179     rillig    608: static const type_t *
                    609: brace_level_sub_type_struct_or_union(const struct brace_level *bl)
1.152     rillig    610: {
                    611:
1.192     rillig    612:        if (bl->bl_member == NULL) {
1.179     rillig    613:                /* too many struct/union initializers */
                    614:                error(172);
                    615:                return NULL;
                    616:        }
1.152     rillig    617:
1.192     rillig    618:        lint_assert(!is_unnamed(bl->bl_member));
                    619:        return sym_type(bl->bl_member);
1.152     rillig    620: }
                    621:
1.179     rillig    622: static const type_t *
1.194     rillig    623: brace_level_sub_type_array(const struct brace_level *bl, bool is_string)
1.147     rillig    624: {
                    625:
1.185     rillig    626:        if (!bl->bl_confused && !bl->bl_type->t_incomplete_array &&
1.192     rillig    627:            bl->bl_subscript >= (size_t)bl->bl_type->t_dim) {
1.179     rillig    628:                /* too many array initializers, expected %d */
                    629:                error(173, bl->bl_type->t_dim);
                    630:        }
                    631:
1.194     rillig    632:        if (is_string && bl->bl_subscript == 0 &&
                    633:            bl->bl_type->t_subt->t_tspec != ARRAY)
                    634:                return bl->bl_type;
                    635:
1.179     rillig    636:        return bl->bl_type->t_subt;
1.147     rillig    637: }
                    638:
1.179     rillig    639: static const type_t *
                    640: brace_level_sub_type_scalar(const struct brace_level *bl)
1.147     rillig    641: {
                    642:
1.179     rillig    643:        if (bl->bl_scalar_done) {
                    644:                /* too many initializers */
                    645:                error(174);
1.172     rillig    646:        }
                    647:
1.179     rillig    648:        return bl->bl_type;
1.172     rillig    649: }
                    650:
1.179     rillig    651: /* Return the type of the sub-object that is currently being initialized. */
                    652: static const type_t *
1.194     rillig    653: brace_level_sub_type(const struct brace_level *bl, bool is_string)
1.172     rillig    654: {
1.147     rillig    655:
1.179     rillig    656:        if (bl->bl_designation.dn_head != NULL)
                    657:                return designation_look_up(&bl->bl_designation, bl->bl_type);
1.174     rillig    658:
1.179     rillig    659:        switch (bl->bl_type->t_tspec) {
                    660:        case STRUCT:
                    661:        case UNION:
                    662:                return brace_level_sub_type_struct_or_union(bl);
                    663:        case ARRAY:
1.194     rillig    664:                return brace_level_sub_type_array(bl, is_string);
1.179     rillig    665:        default:
                    666:                return brace_level_sub_type_scalar(bl);
1.147     rillig    667:        }
                    668: }
                    669:
1.192     rillig    670: /* C99 6.7.8p17 */
1.179     rillig    671: static void
                    672: brace_level_apply_designation(struct brace_level *bl)
1.155     rillig    673: {
1.179     rillig    674:        const struct designator *dr = bl->bl_designation.dn_head;
                    675:
                    676:        if (dr == NULL)
                    677:                return;
1.174     rillig    678:
1.179     rillig    679:        designation_debug(&bl->bl_designation);
1.155     rillig    680:
1.179     rillig    681:        switch (bl->bl_type->t_tspec) {
                    682:        case STRUCT:
                    683:        case UNION:
                    684:                if (dr->dr_name == NULL)
                    685:                        return; /* error, silently ignored */
1.192     rillig    686:                bl->bl_member = look_up_member(bl->bl_type, dr->dr_name);
1.179     rillig    687:                break;
                    688:        case ARRAY:
                    689:                if (dr->dr_name != NULL)
                    690:                        return; /* error, silently ignored */
1.192     rillig    691:                bl->bl_subscript = dr->dr_subscript;
1.179     rillig    692:                break;
                    693:        default:
                    694:                break;          /* error, silently ignored */
1.155     rillig    695:        }
                    696: }
                    697:
1.158     rillig    698: /*
1.179     rillig    699:  * After initializing a sub-object, advance to the next sub-object.
1.158     rillig    700:  *
1.179     rillig    701:  * C99 6.7.8p17
1.158     rillig    702:  */
                    703: static void
1.179     rillig    704: brace_level_advance(struct brace_level *bl)
1.158     rillig    705: {
                    706:
1.179     rillig    707:        switch (bl->bl_type->t_tspec) {
                    708:        case STRUCT:
1.192     rillig    709:                lint_assert(bl->bl_member != NULL);
                    710:                bl->bl_member = skip_unnamed(bl->bl_member->s_next);
1.179     rillig    711:                break;
                    712:        case UNION:
1.192     rillig    713:                bl->bl_member = NULL;
1.179     rillig    714:                break;
                    715:        case ARRAY:
1.192     rillig    716:                bl->bl_subscript++;
1.201     rillig    717:                if (bl->bl_subscript > bl->bl_max_subscript)
                    718:                        bl->bl_max_subscript = bl->bl_subscript;
1.179     rillig    719:                break;
                    720:        default:
                    721:                bl->bl_scalar_done = true;
                    722:                break;
1.158     rillig    723:        }
1.160     rillig    724: }
1.158     rillig    725:
1.146     rillig    726:
                    727: static struct initialization *
                    728: initialization_new(sym_t *sym)
                    729: {
1.179     rillig    730:        struct initialization *in;
1.146     rillig    731:
1.179     rillig    732:        in = xcalloc(1, sizeof(*in));
1.176     rillig    733:        in->in_sym = sym;
1.146     rillig    734:
                    735:        return in;
                    736: }
1.74      rillig    737:
1.146     rillig    738: static void
                    739: initialization_free(struct initialization *in)
                    740: {
1.176     rillig    741:        struct brace_level *bl, *next;
1.146     rillig    742:
1.176     rillig    743:        for (bl = in->in_brace_level; bl != NULL; bl = next) {
                    744:                next = bl->bl_enclosing;
                    745:                brace_level_free(bl);
1.146     rillig    746:        }
                    747:
                    748:        free(in);
                    749: }
                    750:
                    751: #ifdef DEBUG
1.73      rillig    752: static void
1.146     rillig    753: initialization_debug(const struct initialization *in)
1.73      rillig    754: {
1.179     rillig    755:        size_t i;
                    756:        const struct brace_level *bl;
                    757:
1.176     rillig    758:        if (in->in_brace_level == NULL) {
1.179     rillig    759:                debug_step("no brace level");
1.73      rillig    760:                return;
                    761:        }
                    762:
1.179     rillig    763:        i = 0;
                    764:        for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
1.120     rillig    765:                debug_indent();
1.139     rillig    766:                debug_printf("brace level %zu: ", i);
1.178     rillig    767:                brace_level_debug(bl);
1.73      rillig    768:                i++;
                    769:        }
                    770: }
1.146     rillig    771: #else
                    772: #define initialization_debug(in) do { } while (false)
                    773: #endif
                    774:
1.1       cgd       775: /*
1.179     rillig    776:  * Return the type of the object or sub-object that is currently being
                    777:  * initialized.
1.1       cgd       778:  */
1.179     rillig    779: static const type_t *
1.194     rillig    780: initialization_sub_type(struct initialization *in, bool is_string)
1.179     rillig    781: {
                    782:        const type_t *tp;
                    783:
                    784:        tp = in->in_brace_level != NULL
1.194     rillig    785:            ? brace_level_sub_type(in->in_brace_level, is_string)
1.179     rillig    786:            : in->in_sym->s_type;
                    787:        if (tp == NULL)
                    788:                in->in_err = true;
                    789:        return tp;
                    790: }
                    791:
1.158     rillig    792: static void
1.179     rillig    793: initialization_begin_brace_level(struct initialization *in)
1.1       cgd       794: {
1.179     rillig    795:        const type_t *tp;
                    796:
1.176     rillig    797:        if (in->in_err)
1.1       cgd       798:                return;
                    799:
1.70      rillig    800:        debug_enter();
1.38      rillig    801:
1.194     rillig    802:        tp = initialization_sub_type(in, false);
1.179     rillig    803:        if (tp == NULL) {
                    804:                in->in_err = true;
                    805:                goto done;
                    806:        }
1.1       cgd       807:
1.179     rillig    808:        if (tflag && in->in_brace_level == NULL)
1.197     rillig    809:                check_trad_no_auto_aggregate(in->in_sym);
1.70      rillig    810:
1.179     rillig    811:        if (tflag && tp->t_tspec == UNION) {
                    812:                /* initialization of union is illegal in traditional C */
                    813:                warning(238);
                    814:        }
1.1       cgd       815:
1.179     rillig    816:        if (tp->t_tspec == STRUCT && tp->t_str->sou_incomplete) {
1.180     rillig    817:                /* initialization of incomplete type '%s' */
                    818:                error(175, type_name(tp));
1.179     rillig    819:                in->in_err = true;
                    820:                goto done;
1.104     rillig    821:        }
                    822:
1.179     rillig    823:        if (in->in_brace_level != NULL)
                    824:                brace_level_apply_designation(in->in_brace_level);
1.1       cgd       825:
1.179     rillig    826:        in->in_brace_level = brace_level_new(tp, in->in_brace_level);
1.68      rillig    827:
1.179     rillig    828: done:
1.153     rillig    829:        initialization_debug(in);
1.68      rillig    830:        debug_leave();
1.1       cgd       831: }
                    832:
1.179     rillig    833: /* C99 6.7.8p22 */
1.1       cgd       834: static void
1.179     rillig    835: initialization_set_size_of_unknown_array(struct initialization *in)
1.1       cgd       836: {
1.158     rillig    837:
1.179     rillig    838:        if (in->in_sym->s_type->t_incomplete_array &&
                    839:            in->in_brace_level->bl_enclosing == NULL)
                    840:                update_type_of_array_of_unknown_size(in->in_sym,
1.201     rillig    841:                    in->in_brace_level->bl_max_subscript);
1.158     rillig    842: }
                    843:
                    844: static void
1.179     rillig    845: initialization_end_brace_level(struct initialization *in)
1.158     rillig    846: {
1.178     rillig    847:        struct brace_level *bl;
1.158     rillig    848:
1.179     rillig    849:        if (in->in_err)
                    850:                return;
                    851:
1.158     rillig    852:        debug_enter();
                    853:
1.179     rillig    854:        initialization_set_size_of_unknown_array(in);
                    855:
1.178     rillig    856:        bl = in->in_brace_level;
                    857:        in->in_brace_level = bl->bl_enclosing;
                    858:        brace_level_free(bl);
1.184     rillig    859:        bl = in->in_brace_level;
1.7       lukem     860:
1.184     rillig    861:        if (bl != NULL)
                    862:                brace_level_advance(bl);
                    863:        if (bl != NULL)
                    864:                designation_reset(&bl->bl_designation);
1.68      rillig    865:
1.153     rillig    866:        initialization_debug(in);
1.68      rillig    867:        debug_leave();
1.37      rillig    868: }
                    869:
                    870: static void
1.179     rillig    871: initialization_add_designator(struct initialization *in,
                    872:                              const char *name, size_t subscript)
1.164     rillig    873: {
                    874:
1.176     rillig    875:        if (in->in_err)
1.1       cgd       876:                return;
                    877:
1.179     rillig    878:        lint_assert(in->in_brace_level != NULL);
                    879:        designation_add(&in->in_brace_level->bl_designation, name, subscript);
1.1       cgd       880: }
                    881:
1.114     rillig    882: /*
1.179     rillig    883:  * An object with automatic storage duration that has a single initializer
                    884:  * expression without braces and is not an array is initialized by delegating
                    885:  * to the ASSIGN operator.
1.114     rillig    886:  */
1.179     rillig    887: static bool
                    888: initialization_expr_using_assign(struct initialization *in, tnode_t *rn)
1.116     rillig    889: {
1.179     rillig    890:        tnode_t *ln, *tn;
                    891:
                    892:        if (!has_automatic_storage_duration(in->in_sym))
                    893:                return false;
                    894:        if (in->in_brace_level != NULL)
                    895:                return false;
                    896:        if (in->in_sym->s_type->t_tspec == ARRAY)
                    897:                return false;
1.1       cgd       898:
1.186     rillig    899:        debug_step0("handing over to ASSIGN");
1.55      rillig    900:
1.203     rillig    901:        ln = build_name(in->in_sym, 0);
1.204   ! rillig    902:        ln->tn_type = expr_unqualified_type(ln->tn_type);
1.1       cgd       903:
1.202     rillig    904:        tn = build_binary(ln, ASSIGN, rn);
1.179     rillig    905:        expr(tn, false, false, false, false);
1.1       cgd       906:
1.179     rillig    907:        return true;
1.1       cgd       908: }
                    909:
1.82      rillig    910: /* Initialize a character array or wchar_t array with a string literal. */
1.62      rillig    911: static bool
1.158     rillig    912: initialization_init_array_using_string(struct initialization *in, tnode_t *tn)
1.1       cgd       913: {
1.178     rillig    914:        struct brace_level *bl;
1.179     rillig    915:        const type_t *tp;
1.1       cgd       916:        strg_t  *strg;
                    917:
                    918:        if (tn->tn_op != STRING)
1.63      rillig    919:                return false;
1.1       cgd       920:
1.178     rillig    921:        bl = in->in_brace_level;
1.194     rillig    922:        tp = initialization_sub_type(in, true);
1.47      rillig    923:        strg = tn->tn_string;
1.1       cgd       924:
1.179     rillig    925:        if (!is_string_array(tp, strg->st_tspec))
                    926:                return false;
1.192     rillig    927:        if (bl != NULL && tp->t_tspec != ARRAY && bl->bl_subscript != 0)
1.179     rillig    928:                return false;
1.119     rillig    929:
1.194     rillig    930:        if (!tp->t_incomplete_array && tp->t_dim < (int)strg->st_len) {
1.179     rillig    931:                /* non-null byte ignored in string initializer */
                    932:                warning(187);
                    933:        }
1.119     rillig    934:
1.179     rillig    935:        if (tp == in->in_sym->s_type && tp->t_incomplete_array) {
                    936:                if (bl != NULL) {
1.194     rillig    937:                        bl->bl_subscript = strg->st_len;
                    938:                        /* see brace_level_advance for the +1 */
1.179     rillig    939:                        /* see initialization_set_size_of_unknown_array */
                    940:                } else
                    941:                        update_type_of_array_of_unknown_size(in->in_sym,
                    942:                            strg->st_len + 1);
1.1       cgd       943:        }
                    944:
1.63      rillig    945:        return true;
1.1       cgd       946: }
1.158     rillig    947:
                    948: /*
1.179     rillig    949:  * Initialize a single sub-object as part of the currently ongoing
                    950:  * initialization.
1.158     rillig    951:  */
1.179     rillig    952: static void
                    953: initialization_expr(struct initialization *in, tnode_t *tn)
1.158     rillig    954: {
1.184     rillig    955:        struct brace_level *bl;
1.179     rillig    956:        const type_t *tp;
1.158     rillig    957:
1.179     rillig    958:        if (in->in_err)
                    959:                return;
1.158     rillig    960:
1.184     rillig    961:        bl = in->in_brace_level;
1.185     rillig    962:        if (bl != NULL && bl->bl_confused)
1.179     rillig    963:                return;
1.158     rillig    964:
1.179     rillig    965:        debug_enter();
1.158     rillig    966:
1.179     rillig    967:        if (tn == NULL)
                    968:                goto advance;
                    969:        if (initialization_expr_using_assign(in, tn))
                    970:                goto done;
                    971:        if (initialization_init_array_using_string(in, tn))
                    972:                goto advance;
1.158     rillig    973:
1.184     rillig    974:        if (bl != NULL)
                    975:                brace_level_apply_designation(bl);
1.194     rillig    976:        tp = initialization_sub_type(in, false);
1.179     rillig    977:        if (tp == NULL)
                    978:                goto done;
1.158     rillig    979:
1.184     rillig    980:        if (bl == NULL && !is_scalar(tp->t_tspec)) {
1.158     rillig    981:                /* {}-enclosed initializer required */
                    982:                error(181);
1.179     rillig    983:                goto done;
1.158     rillig    984:        }
                    985:
1.179     rillig    986:        /*
                    987:         * Hack to accept initializations with omitted braces, see
                    988:         * c99_6_7_8_p28_example5 in test d_c99_init.c.  Since both GCC and
                    989:         * Clang already warn about this at level -Wall, there is no point
1.185     rillig    990:         * in repeating the same check in lint.  If needed, support for these
                    991:         * edge cases could be added, but that would increase the complexity.
1.179     rillig    992:         */
                    993:        if (is_scalar(tn->tn_type->t_tspec) &&
1.185     rillig    994:            (tp->t_tspec == ARRAY || is_struct_or_union(tp->t_tspec)) &&
1.184     rillig    995:            bl != NULL) {
1.185     rillig    996:                bl->bl_confused = true;
1.179     rillig    997:                goto done;
1.158     rillig    998:        }
                    999:
1.186     rillig   1000:        debug_step2("expecting '%s', expression has '%s'",
1.179     rillig   1001:            type_name(tp), type_name(tn->tn_type));
                   1002:        check_init_expr(tp, in->in_sym, tn);
                   1003:
                   1004: advance:
1.184     rillig   1005:        if (bl != NULL)
                   1006:                brace_level_advance(bl);
1.179     rillig   1007: done:
1.184     rillig   1008:        if (bl != NULL)
                   1009:                designation_reset(&bl->bl_designation);
                   1010:
1.158     rillig   1011:        initialization_debug(in);
                   1012:        debug_leave();
                   1013: }
                   1014:
                   1015:
1.179     rillig   1016: static struct initialization *init;
1.158     rillig   1017:
                   1018:
                   1019: static struct initialization *
                   1020: current_init(void)
                   1021: {
1.175     rillig   1022:
1.158     rillig   1023:        lint_assert(init != NULL);
                   1024:        return init;
                   1025: }
                   1026:
                   1027: sym_t **
                   1028: current_initsym(void)
                   1029: {
1.175     rillig   1030:
1.176     rillig   1031:        return &current_init()->in_sym;
1.158     rillig   1032: }
                   1033:
                   1034: void
                   1035: begin_initialization(sym_t *sym)
                   1036: {
                   1037:        struct initialization *in;
                   1038:
1.186     rillig   1039:        debug_step1("begin initialization of '%s'", type_name(sym->s_type));
1.179     rillig   1040: #ifdef DEBUG
                   1041:        debug_indentation++;
                   1042: #endif
                   1043:
1.158     rillig   1044:        in = initialization_new(sym);
1.179     rillig   1045:        in->in_enclosing = init;
1.158     rillig   1046:        init = in;
                   1047: }
                   1048:
                   1049: void
                   1050: end_initialization(void)
                   1051: {
                   1052:        struct initialization *in;
                   1053:
                   1054:        in = init;
1.179     rillig   1055:        init = in->in_enclosing;
1.158     rillig   1056:        initialization_free(in);
1.179     rillig   1057:
                   1058: #ifdef DEBUG
                   1059:        debug_indentation--;
                   1060: #endif
1.186     rillig   1061:        debug_step0("end initialization");
1.158     rillig   1062: }
                   1063:
                   1064: void
                   1065: add_designator_member(sbuf_t *sb)
                   1066: {
1.175     rillig   1067:
1.179     rillig   1068:        initialization_add_designator(current_init(), sb->sb_name, 0);
1.158     rillig   1069: }
                   1070:
                   1071: void
                   1072: add_designator_subscript(range_t range)
                   1073: {
1.175     rillig   1074:
1.179     rillig   1075:        initialization_add_designator(current_init(), NULL, range.hi);
1.158     rillig   1076: }
                   1077:
                   1078: void
                   1079: init_lbrace(void)
                   1080: {
1.175     rillig   1081:
1.179     rillig   1082:        initialization_begin_brace_level(current_init());
1.158     rillig   1083: }
                   1084:
                   1085: void
1.179     rillig   1086: init_expr(tnode_t *tn)
1.158     rillig   1087: {
1.175     rillig   1088:
1.158     rillig   1089:        initialization_expr(current_init(), tn);
                   1090: }
                   1091:
                   1092: void
                   1093: init_rbrace(void)
                   1094: {
1.175     rillig   1095:
1.179     rillig   1096:        initialization_end_brace_level(current_init());
1.158     rillig   1097: }

CVSweb <webmaster@jp.NetBSD.org>