Changeset 1113c9e in mainline for uspace/app/sbi/src/parse.c


Ignore:
Timestamp:
2010-06-09T19:03:24Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8eec3c8
Parents:
8f80c77 (diff), c5cb943d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jsvoboda/helenos/sysel.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/parse.c

    r8f80c77 r1113c9e  
    3535#include <assert.h>
    3636#include <stdlib.h>
     37#include "cspan.h"
    3738#include "debug.h"
    3839#include "lex.h"
     
    6465static stree_prop_t *parse_prop(parse_t *parse, stree_csi_t *outer_csi);
    6566
     67static void parse_symbol_attrs(parse_t *parse, stree_symbol_t *symbol);
    6668static stree_symbol_attr_t *parse_symbol_attr(parse_t *parse);
    6769
     
    7072static stree_fun_sig_t *parse_fun_sig(parse_t *parse);
    7173
     74static void parse_prop_get(parse_t *parse, stree_prop_t *prop);
     75static void parse_prop_set(parse_t *parse, stree_prop_t *prop);
    7276
    7377/*
     
    175179        stree_ident_t *targ_name;
    176180        stree_targ_t *targ;
     181        stree_texpr_t *pref;
    177182
    178183        switch (dclass) {
     
    212217                /* Inheritance list */
    213218                lskip(parse);
    214                 csi->base_csi_ref = parse_texpr(parse);
    215         } else {
    216                 csi->base_csi_ref = NULL;
     219
     220                while (b_true) {
     221                        pref = parse_texpr(parse);
     222                        if (parse_is_error(parse))
     223                                break;
     224
     225                        list_append(&csi->inherit, pref);
     226                        if (lcur_lc(parse) != lc_plus)
     227                                break;
     228
     229                        lskip(parse);
     230                }
    217231        }
    218232
     
    224238                csimbr = parse_csimbr(parse, csi);
    225239                if (csimbr == NULL)
     240                        continue;
     241
     242                list_append(&csi->members, csimbr);
     243        }
     244
     245        lmatch(parse, lc_end);
     246
     247        if (outer_csi != NULL) {
     248                switch (outer_csi->cc) {
     249                case csi_class:
     250                case csi_struct:
    226251                        break;
    227 
    228                 list_append(&csi->members, csimbr);
    229         }
    230 
    231         lmatch(parse, lc_end);
     252                case csi_interface:
     253                        cspan_print(csi->name->cspan);
     254                        printf(" Error: CSI declared inside interface.\n");
     255                        parse_note_error(parse);
     256                        /* XXX Free csi */
     257                        return NULL;
     258                }
     259        }
    232260
    233261        return csi;
     
    253281        stree_prop_t *prop;
    254282
     283        csimbr = NULL;
     284
    255285        switch (lcur_lc(parse)) {
    256286        case lc_class:
     
    258288        case lc_interface:
    259289                csi = parse_csi(parse, lcur_lc(parse), outer_csi);
    260                 csimbr = stree_csimbr_new(csimbr_csi);
    261                 csimbr->u.csi = csi;
     290                if (csi != NULL) {
     291                        csimbr = stree_csimbr_new(csimbr_csi);
     292                        csimbr->u.csi = csi;
     293                }
    262294                break;
    263295        case lc_new:
    264296                ctor = parse_ctor(parse, outer_csi);
    265                 csimbr = stree_csimbr_new(csimbr_ctor);
    266                 csimbr->u.ctor = ctor;
     297                if (ctor != NULL) {
     298                        csimbr = stree_csimbr_new(csimbr_ctor);
     299                        csimbr->u.ctor = ctor;
     300                }
    267301                break;
    268302        case lc_deleg:
    269303                deleg = parse_deleg(parse, outer_csi);
    270                 csimbr = stree_csimbr_new(csimbr_deleg);
    271                 csimbr->u.deleg = deleg;
     304                if (deleg != NULL) {
     305                        csimbr = stree_csimbr_new(csimbr_deleg);
     306                        csimbr->u.deleg = deleg;
     307                }
    272308                break;
    273309        case lc_enum:
    274310                enum_d = parse_enum(parse, outer_csi);
    275                 csimbr = stree_csimbr_new(csimbr_enum);
    276                 csimbr->u.enum_d = enum_d;
     311                if (enum_d != NULL) {
     312                        csimbr = stree_csimbr_new(csimbr_enum);
     313                        csimbr->u.enum_d = enum_d;
     314                }
    277315                break;
    278316        case lc_fun:
     
    283321        case lc_var:
    284322                var = parse_var(parse, outer_csi);
    285                 csimbr = stree_csimbr_new(csimbr_var);
    286                 csimbr->u.var = var;
     323                if (var != NULL) {
     324                        csimbr = stree_csimbr_new(csimbr_var);
     325                        csimbr->u.var = var;
     326                }
    287327                break;
    288328        case lc_prop:
     
    294334                lunexpected_error(parse);
    295335                lex_next(parse->lex);
    296                 csimbr = NULL;
    297336                break;
    298337        }
     
    311350        stree_ctor_t *ctor;
    312351        stree_symbol_t *symbol;
    313         stree_symbol_attr_t *attr;
     352        cspan_t *cspan;
    314353
    315354        ctor = stree_ctor_new();
     
    321360
    322361        lmatch(parse, lc_new);
     362        cspan = lprev_span(parse);
    323363
    324364        /* Fake identifier. */
     
    334374        ctor->sig = parse_fun_sig(parse);
    335375        if (ctor->sig->rtype != NULL) {
    336                 printf("Error: Constructor of CSI '");
     376                cspan_print(cspan);
     377                printf(" Error: Constructor of CSI '");
    337378                symbol_print_fqn(csi_to_symbol(outer_csi));
    338379                printf("' has a return type.\n");
     
    340381        }
    341382
    342         list_init(&symbol->attr);
    343 
    344383        /* Parse attributes. */
    345         while (lcur_lc(parse) == lc_comma && !parse_is_error(parse)) {
    346                 lskip(parse);
    347                 attr = parse_symbol_attr(parse);
    348                 list_append(&symbol->attr, attr);
    349         }
     384        parse_symbol_attrs(parse, symbol);
    350385
    351386        ctor->proc = stree_proc_new();
     
    356391
    357392                /* This constructor has no body. */
    358                 printf("Error: Constructor of CSI '");
     393                cspan_print(cspan);
     394                printf(" Error: Constructor of CSI '");
    359395                symbol_print_fqn(csi_to_symbol(outer_csi));
    360396                printf("' has no body.\n");
     
    368404        }
    369405
     406        switch (outer_csi->cc) {
     407        case csi_class:
     408        case csi_struct:
     409                break;
     410        case csi_interface:
     411                cspan_print(ctor->name->cspan);
     412                printf(" Error: Constructor declared inside interface.\n");
     413                parse_note_error(parse);
     414                /* XXX Free ctor */
     415                return NULL;
     416        }
     417
    370418        return ctor;
    371419}
     
    409457
    410458        if (list_is_empty(&enum_d->members)) {
     459                cspan_print(enum_d->name->cspan);
    411460                printf("Error: Enum type '%s' has no members.\n",
    412461                    strtab_get_str(enum_d->name->sid));
     
    416465        lmatch(parse, lc_end);
    417466
     467        if (outer_csi != NULL) {
     468                switch (outer_csi->cc) {
     469                case csi_class:
     470                case csi_struct:
     471                        break;
     472                case csi_interface:
     473                        cspan_print(enum_d->name->cspan);
     474                        printf(" Error: Enum declared inside interface.\n");
     475                        parse_note_error(parse);
     476                        /* XXX Free enum */
     477                        return NULL;
     478                }
     479        }
     480
    418481        return enum_d;
    419482}
     
    449512        stree_deleg_t *deleg;
    450513        stree_symbol_t *symbol;
    451         stree_symbol_attr_t *attr;
    452514
    453515        deleg = stree_deleg_new();
     
    467529        deleg->sig = parse_fun_sig(parse);
    468530
    469         list_init(&symbol->attr);
    470 
    471531        /* Parse attributes. */
    472         while (lcur_lc(parse) == lc_comma && !parse_is_error(parse)) {
    473                 lskip(parse);
    474                 attr = parse_symbol_attr(parse);
    475                 list_append(&symbol->attr, attr);
    476         }
     532        parse_symbol_attrs(parse, symbol);
    477533
    478534        lmatch(parse, lc_scolon);
     535
     536        switch (outer_csi->cc) {
     537        case csi_class:
     538        case csi_struct:
     539                break;
     540        case csi_interface:
     541                cspan_print(deleg->name->cspan);
     542                printf(" Error: Delegate declared inside interface.\n");
     543                parse_note_error(parse);
     544                /* XXX Free deleg */
     545                return NULL;
     546        }
    479547
    480548        return deleg;
     
    491559        stree_fun_t *fun;
    492560        stree_symbol_t *symbol;
    493         stree_symbol_attr_t *attr;
     561        bool_t body_expected;
    494562
    495563        fun = stree_fun_new();
     
    508576        fun->sig = parse_fun_sig(parse);
    509577
    510         list_init(&symbol->attr);
    511 
    512578        /* Parse attributes. */
    513         while (lcur_lc(parse) == lc_comma && !parse_is_error(parse)) {
    514                 lskip(parse);
    515                 attr = parse_symbol_attr(parse);
    516                 list_append(&symbol->attr, attr);
    517         }
     579        parse_symbol_attrs(parse, symbol);
     580
     581        body_expected = !stree_symbol_has_attr(symbol, sac_builtin) &&
     582            (outer_csi->cc != csi_interface);
    518583
    519584        fun->proc = stree_proc_new();
     
    523588                lskip(parse);
    524589
    525                 /* This function has no body. */
    526                 if (!stree_symbol_has_attr(symbol, sac_builtin)) {
    527                         printf("Error: Function '");
     590                /* Body not present */
     591                if (body_expected) {
     592                        cspan_print(fun->name->cspan);
     593                        printf(" Error: Function '");
    528594                        symbol_print_fqn(symbol);
    529                         printf("' has no body.\n");
     595                        printf("' should have a body.\n");
    530596                        parse_note_error(parse);
    531597                }
     598
    532599                fun->proc->body = NULL;
    533600        } else {
     
    535602                fun->proc->body = parse_block(parse);
    536603                lmatch(parse, lc_end);
     604
     605                /* Body present */
     606                if (!body_expected) {
     607                        cspan_print(fun->name->cspan);
     608                        printf(" Error: Function declaration '");
     609                        symbol_print_fqn(symbol);
     610                        printf("' should not have a body.\n");
     611                        parse_note_error(parse);
     612                }
    537613        }
    538614
     
    561637        lmatch(parse, lc_colon);
    562638        var->type =  parse_texpr(parse);
     639
     640        parse_symbol_attrs(parse, symbol);
     641
    563642        lmatch(parse, lc_scolon);
     643
     644        switch (outer_csi->cc) {
     645        case csi_class:
     646        case csi_struct:
     647                break;
     648        case csi_interface:
     649                cspan_print(var->name->cspan);
     650                printf(" Error: Variable declared inside interface.\n");
     651                parse_note_error(parse);
     652                /* XXX Free var */
     653                return NULL;
     654        }
    564655
    565656        return var;
     
    576667        stree_prop_t *prop;
    577668        stree_symbol_t *symbol;
     669        bool_t body_expected;
    578670
    579671        stree_ident_t *ident;
     
    625717        lmatch(parse, lc_colon);
    626718        prop->type = parse_texpr(parse);
     719
     720        /* Parse attributes. */
     721        parse_symbol_attrs(parse, symbol);
     722
     723        body_expected = (outer_csi->cc != csi_interface);
     724
    627725        lmatch(parse, lc_is);
    628726
     
    630728                switch (lcur_lc(parse)) {
    631729                case lc_get:
    632                         lskip(parse);
    633                         lmatch(parse, lc_is);
    634                         if (prop->getter != NULL) {
    635                                 printf("Error: Duplicate getter.\n");
    636                                 (void) parse_block(parse); /* XXX Free */
    637                                 lmatch(parse, lc_end);
    638                                 parse_note_error(parse);
    639                                 break;
    640                         }
    641 
    642                         /* Create setter procedure */
    643                         prop->getter = stree_proc_new();
    644                         prop->getter->body = parse_block(parse);
    645                         prop->getter->outer_symbol = symbol;
    646 
    647                         lmatch(parse, lc_end);
     730                        parse_prop_get(parse, prop);
    648731                        break;
    649732                case lc_set:
    650                         lskip(parse);
    651                         prop->setter_arg = stree_proc_arg_new();
    652                         prop->setter_arg->name = parse_ident(parse);
    653                         prop->setter_arg->type = prop->type;
    654                         lmatch(parse, lc_is);
    655                         if (prop->setter != NULL) {
    656                                 printf("Error: Duplicate setter.\n");
    657                                 (void) parse_block(parse); /* XXX Free */
    658                                 lmatch(parse, lc_end);
    659                                 parse_note_error(parse);
    660                         }
    661 
    662                         /* Create setter procedure */
    663                         prop->setter = stree_proc_new();
    664                         prop->setter->body = parse_block(parse);
    665                         prop->setter->outer_symbol = symbol;
    666 
    667                         lmatch(parse, lc_end);
     733                        parse_prop_set(parse, prop);
    668734                        break;
    669735                default:
     
    677743}
    678744
     745/** Parse symbol attributes.
     746 *
     747 * Parse list of attributes and add them to @a symbol.
     748 *
     749 * @param parse         Parser object
     750 * @param symbol        Symbol to add these attributes to
     751 */
     752static void parse_symbol_attrs(parse_t *parse, stree_symbol_t *symbol)
     753{
     754        stree_symbol_attr_t *attr;
     755
     756        /* Parse attributes. */
     757        while (lcur_lc(parse) == lc_comma && !parse_is_error(parse)) {
     758                lskip(parse);
     759                attr = parse_symbol_attr(parse);
     760                list_append(&symbol->attr, attr);
     761        }
     762}
     763
    679764/** Parse symbol attribute.
    680765 *
    681  * @param parse         Parser object.
    682  * @param outer_csi     CSI containing this declaration or @c NULL if global.
    683  * @return              New syntax tree node.
     766 * @param parse         Parser object
     767 * @return              New syntax tree node
    684768 */
    685769static stree_symbol_attr_t *parse_symbol_attr(parse_t *parse)
    686770{
    687771        stree_symbol_attr_t *attr;
    688 
    689         if (lcur_lc(parse) != lc_builtin) {
    690                 printf("Error: Unexpected attribute '");
     772        symbol_attr_class_t sac;
     773
     774        /* Make compiler happy. */
     775        sac = 0;
     776
     777        switch (lcur_lc(parse)) {
     778        case lc_builtin: sac = sac_builtin; break;
     779        case lc_static: sac = sac_static; break;
     780        default:
     781                cspan_print(lcur_span(parse));
     782                printf(" Error: Unexpected attribute '");
    691783                lem_print(lcur(parse));
    692784                printf("'.\n");
    693785                parse_note_error(parse);
     786                break;
    694787        }
    695788
    696789        lskip(parse);
    697790
    698         attr = stree_symbol_attr_new(sac_builtin);
     791        attr = stree_symbol_attr_new(sac);
    699792        return attr;
    700793}
     
    718811        printf("Parse procedure argument.\n");
    719812#endif
    720         list_init(&arg->attr);
     813        list_init(&arg->attr);
    721814
    722815        /* Parse attributes. */
     
    740833
    741834        if (lcur_lc(parse) != lc_packed) {
    742                 printf("Error: Unexpected attribute '");
     835                cspan_print(lcur_span(parse));
     836                printf(" Error: Unexpected attribute '");
    743837                lem_print(lcur(parse));
    744838                printf("'.\n");
     
    802896
    803897        return sig;
     898}
     899
     900/** Parse member property getter.
     901 *
     902 * @param parse         Parser object.
     903 * @param prop          Property containing this declaration.
     904 */
     905static void parse_prop_get(parse_t *parse, stree_prop_t *prop)
     906{
     907        cspan_t *cspan;
     908        stree_block_t *block;
     909        stree_proc_t *getter;
     910        bool_t body_expected;
     911
     912        body_expected = (prop->symbol->outer_csi->cc != csi_interface);
     913
     914        lskip(parse);
     915        cspan = lprev_span(parse);
     916
     917        if (prop->getter != NULL) {
     918                cspan_print(cspan);
     919                printf(" Error: Duplicate getter.\n");
     920                parse_note_error(parse);
     921                return;
     922        }
     923
     924        if (lcur_lc(parse) == lc_scolon) {
     925                /* Body not present */
     926                lskip(parse);
     927                block = NULL;
     928
     929                if (body_expected) {
     930                        cspan_print(prop->name->cspan);
     931                        printf(" Error: Property '");
     932                        symbol_print_fqn(prop->symbol);
     933                        printf("' getter should have "
     934                            "a body.\n");
     935                        parse_note_error(parse);
     936                }
     937        } else {
     938                /* Body present */
     939                lmatch(parse, lc_is);
     940                block = parse_block(parse);
     941                lmatch(parse, lc_end);
     942
     943                if (!body_expected) {
     944                        cspan_print(prop->name->cspan);
     945                        printf(" Error: Property '");
     946                        symbol_print_fqn(prop->symbol);
     947                        printf("' getter declaration should "
     948                            "not have a body.\n");
     949                        parse_note_error(parse);
     950
     951                        /* XXX Free block */
     952                        block = NULL;
     953                }
     954        }
     955
     956        /* Create getter procedure */
     957        getter = stree_proc_new();
     958        getter->body = block;
     959        getter->outer_symbol = prop->symbol;
     960
     961        /* Store getter in property. */
     962        prop->getter = getter;
     963}
     964
     965
     966/** Parse member property setter.
     967 *
     968 * @param parse         Parser object.
     969 * @param prop          Property containing this declaration.
     970 */
     971static void parse_prop_set(parse_t *parse, stree_prop_t *prop)
     972{
     973        cspan_t *cspan;
     974        stree_block_t *block;
     975        stree_proc_t *setter;
     976        bool_t body_expected;
     977
     978        body_expected = (prop->symbol->outer_csi->cc != csi_interface);
     979
     980        lskip(parse);
     981        cspan = lprev_span(parse);
     982
     983        if (prop->setter != NULL) {
     984                cspan_print(cspan);
     985                printf(" Error: Duplicate setter.\n");
     986                parse_note_error(parse);
     987                return;
     988        }
     989
     990        prop->setter_arg = stree_proc_arg_new();
     991        prop->setter_arg->name = parse_ident(parse);
     992        prop->setter_arg->type = prop->type;
     993
     994        if (lcur_lc(parse) == lc_scolon) {
     995                /* Body not present */
     996                lskip(parse);
     997
     998                block = NULL;
     999
     1000                if (body_expected) {
     1001                        cspan_print(prop->name->cspan);
     1002                        printf(" Error: Property '");
     1003                        symbol_print_fqn(prop->symbol);
     1004                        printf("' setter should have "
     1005                            "a body.\n");
     1006                        parse_note_error(parse);
     1007                }
     1008        } else {
     1009                /* Body present */
     1010                lmatch(parse, lc_is);
     1011                block = parse_block(parse);
     1012                lmatch(parse, lc_end);
     1013
     1014                if (!body_expected) {
     1015                        cspan_print(prop->name->cspan);
     1016                        printf(" Error: Property '");
     1017                        symbol_print_fqn(prop->symbol);
     1018                        printf("' setter declaration should "
     1019                            "not have a body.\n");
     1020                        parse_note_error(parse);
     1021                }
     1022        }
     1023
     1024
     1025        /* Create setter procedure */
     1026        setter = stree_proc_new();
     1027        setter->body = block;
     1028        setter->outer_symbol = prop->symbol;
     1029
     1030        /* Store setter in property. */
     1031        prop->setter = setter;
    8041032}
    8051033
Note: See TracChangeset for help on using the changeset viewer.