Annotation of researchv10no/cmd/cfront/ptcfront/error.c, revision 1.1.1.1

1.1       root        1: /*ident        "@(#)ctrans:src/error.c 1.6" */
                      2: /**************************************************************************
                      3: 
                      4:        C++ source for cfront, the C++ compiler front-end
                      5:        written in the computer science research center of Bell Labs
                      6: 
                      7:        Copyright (c) 1984 AT&T, Inc. All Rights Reserved
                      8:        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T, INC.
                      9: 
                     10:  error.c :
                     11: 
                     12:        write error messages
                     13: 
                     14:        Until scan_started != 0 no context can be assumed
                     15: 
                     16: ***************************************************************************/
                     17: 
                     18: #ifdef __cplusplus
                     19: #include <stdlib.h>
                     20: #endif
                     21: 
                     22: #include "cfront.h"
                     23: #include "size.h"
                     24: #include "template.h"
                     25: 
                     26: int error_count;
                     27: static int no_of_warnings;
                     28: char scan_started;
                     29: 
                     30: #define ERRTRACE    20
                     31: 
                     32: static char* abbrev_tbl[] = {
                     33:        " argument",
                     34:        " base",
                     35:        " class",
                     36:        " declaration",
                     37:        " expression",
                     38:        " function",
                     39:        " global",
                     40:        "H",
                     41:        " initialize",
                     42:        "J",
                     43:        " constructor", // 'K' !
                     44:        " list",
                     45:        " member",
                     46:        " name",
                     47:        " object",
                     48:        " pointer",
                     49:        " qualifie",
                     50:        " reference",
                     51:        " statement",
                     52:        " type",
                     53:        " undefined",
                     54:        " variable",
                     55:        " with",
                     56:        " expected", // 'X'
                     57:        " template", // 'Y'???
                     58:        " parameter", // 'Z'???
                     59: };
                     60: 
                     61: ea* ea0;
                     62: 
                     63: void error_init()
                     64: {
                     65:        static char errbuf[BUFSIZ];
                     66:        setbuf(stderr,errbuf);
                     67:        ea0 = new ea;
                     68: }
                     69: 
                     70: #define INTERNAL 127
                     71: 
                     72: void ext(int n)
                     73: {
                     74:        int useit=n; // to avoid n not used warning during build
                     75: // for testing only
                     76: //     if (n == INTERNAL)
                     77: //     abort();
                     78:        exit(error_count?error_count:1);
                     79: }
                     80: 
                     81: /* static */ 
                     82: void print_loc()
                     83: {
                     84:        loc* sl = (Cstmt) ? &Cstmt->where : 0;
                     85:        loc* dl = (Cdcl && (Cdcl->base==NAME || Cdcl->base==TNAME)) ? &Cdcl->where : 0;
                     86:        if (sl && dl && sl->file==dl->file) {   // Cstmt and Cdcl in same file
                     87:                if (sl->line<=dl->line) {
                     88:                        if (curloc.file==dl->file && curloc.line<dl->line)
                     89:                                // hack to compensate for YACC's
                     90:                                // bad manners in the use of line numbers
                     91:                                sl->put(out_file);
                     92:                        else
                     93:                                dl->put(out_file);
                     94:                }
                     95:                else
                     96:                        sl->put(out_file);
                     97:        }
                     98:        else if (sl && sl->file==curr_file)     // Cstmt in current file
                     99:                sl->put(out_file);
                    100:        else if (dl && dl->file==curr_file)     // Cdcl in current file
                    101:                dl->put(out_file);
                    102:        else
                    103:                curloc.put(out_file);
                    104: }
                    105: 
                    106: static void print_context()
                    107: {
                    108:        putc('\n',out_file);
                    109: }
                    110: 
                    111: static char in_error = 0;
                    112: static loc dummy_loc;
                    113: 
                    114: void yyerror(char* s)
                    115: {
                    116:        error(s);
                    117: }
                    118: 
                    119: int error(const char* s)
                    120: {
                    121:        return error(0,s);
                    122: }
                    123: 
                    124: int error(int t, const char* s)
                    125: {
                    126:        return error(t,&dummy_loc,s,*ea0,*ea0,*ea0,*ea0);
                    127: }
                    128: 
                    129: int error(const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
                    130: {
                    131:        return error(0,&dummy_loc,s,a0,a1,a2,a3);
                    132: }
                    133: 
                    134: int error(loc* lc, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
                    135: {
                    136:        return error(0,lc,s,a0,a1,a2,a3);
                    137: }
                    138: 
                    139: int error(int t, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
                    140: {
                    141:        return error(t,&dummy_loc,s,a0,a1,a2,a3);
                    142: }
                    143: 
                    144: int suppress_error;
                    145: 
                    146: int error(int t, loc* lc, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
                    147: /*
                    148:        "int" not "void" because of "pch" in lex.c
                    149: 
                    150:        legal error types are:
                    151:                'w'             warning  (not counted in error count)
                    152:                'd'             debug
                    153:                'D'             debug -- no prefix
                    154:                's'             "not implemented" message
                    155:                'l'             "compiler limit exceeded" message
                    156:                0               error 
                    157:                'i'             internal error (causes abort)
                    158:                't'             error while printing error message
                    159: */
                    160: {
                    161:        if (suppress_error) return 0;
                    162: 
                    163:        if (in_error++)
                    164:                if (t == 't')
                    165:                        t = 'i';
                    166:                else if (4 < in_error) {
                    167:                        fprintf(stderr,"\nOops!, error while handling error\n");
                    168:                        ext(13);
                    169:                }
                    170: 
                    171:        FILE * of = out_file;
                    172:        out_file = stderr;
                    173: 
                    174:        if (!scan_started || t=='t')
                    175:                putch('\n');
                    176:        else if (lc != &dummy_loc) {
                    177:                if(t != 'D' && t != 'c') lc->put(out_file);
                    178:        } else {
                    179:                if(t != 'D' && t != 'c') print_loc();
                    180:        }
                    181: 
                    182:        switch (t) {
                    183:         case 'C':
                    184:         case 'c':
                    185:                break;
                    186:        case 0:
                    187:        case 'e':
                    188:                putstring("error: ");
                    189:                break;
                    190:         case 'd':
                    191:                putstring("DEBUG: ");
                    192:         case 'D':
                    193:                break;
                    194:         case 'w':
                    195:                no_of_warnings++;
                    196:                putstring("warning: ");
                    197:                break;
                    198:         case 'l':
                    199:                putstring("compiler limit exceeded: ");
                    200:                break;
                    201:         case 's':
                    202:                putstring("sorry, not implemented: ");
                    203:                break;
                    204:         case 'i':
                    205:                if (error_count++) {
                    206:                        fprintf(out_file,"sorry, cannot recover from earlier errors\n");
                    207:                        ext(INTERNAL);
                    208:                }
                    209:                else
                    210:                        fprintf(out_file,"internal %s error: ",prog_name);
                    211:         }
                    212: 
                    213:        ea argv[4];
                    214:        ea* a = argv;
                    215:        argv[0] = a0;
                    216:        argv[1] = a1;
                    217:        argv[2] = a2;
                    218:        argv[3] = a3;
                    219: 
                    220:        int c;
                    221: 
                    222:        while (c = *s++) {
                    223:                if ('A'<=c && c<='Z')
                    224:                        putstring(abbrev_tbl[c-'A']);
                    225:                else if (c == '%') {
                    226:                        switch (c = *s++) {
                    227:                        case 'k':       // TOK assumed passed as an int
                    228:                        {       TOK x = TOK(a->i);
                    229:                                if (0<x && x<=MAXTOK && keys[x])
                    230:                                        fprintf(out_file," %s",keys[x]);
                    231:                                else
                    232:                                        fprintf(out_file," token(%d)",x);
                    233:                                break;
                    234:                        }
                    235:                        case 't':       // Ptype 
                    236:                        {       Ptype tt = Ptype(a->p);
                    237:                                if (tt == 0) break;
                    238: 
                    239:                                putch(' ');
                    240:                        
                    241:                                int nt = ntok;
                    242:                                emode = 1;
                    243:                                tt->dcl_print(0);
                    244:                                emode = 0;
                    245:                                ntok = nt;
                    246:                                break;
                    247:                        }
                    248:                        case 'n':       // Pname
                    249:                        {       Pname nn = Pname(a->p);
                    250:                                if (nn && nn->string) {
                    251:                                        // suppress generated class names:
                    252:                                        if (nn->string[0]=='_'
                    253:                                        && nn->string[1]=='_'
                    254:                                        && nn->string[2]=='C') break;
                    255:                                        emode = 1;
                    256:                                        putch(' ');
                    257:                                        nn->print();
                    258:                                        emode = 0;
                    259:                                }
                    260:                                else
                    261:                                        putstring(" ?");
                    262:                                break;
                    263:                        }
                    264:                        case 'p':       // pointer
                    265:                        {       char* f = sizeof(char*)==sizeof(int)?" %d":" %ld";
                    266:                                fprintf(out_file,f,a->p);
                    267:                                break;
                    268:                        }
                    269:                        case 'c':       // char assumed passed as an int
                    270:                                putch(a->i);
                    271:                                break;
                    272: 
                    273:                        case 'd':       // int
                    274:                                fprintf(out_file," %d",a->i);
                    275:                                break;
                    276: 
                    277:                        case 'o':       // int
                    278:                                fprintf(out_file," %o",a->i);
                    279:                                break;
                    280: 
                    281:                        case 's':       // char*
                    282:                                char *s = ((char *)a->p);
                    283:                                if ( s ) putst((char*)a->p);
                    284:                                break;
                    285:                        }
                    286:                        a++;
                    287:                }
                    288:                else
                    289:                        putch(c);
                    290:        }
                    291: 
                    292: /*
                    293:        switch (t) {
                    294:        case 'd':
                    295:        case 't':
                    296:        case 'w':
                    297:                putch('\n');
                    298:                break;
                    299:        default:
                    300: */
                    301:                if (t != 'c' && t != 'e' && t != 'C')
                    302:                        print_context();
                    303: /*
                    304:        }
                    305: */
                    306: 
                    307:         templ_inst::head->print_error_loc();
                    308:        fflush(stderr);
                    309:        if (!scan_started && t!='d' && t!='w') ext(4);
                    310: 
                    311:         // now we may want to carry on 
                    312:        out_file = of;
                    313:        switch (t) {
                    314:        case 't':
                    315:                if (--in_error) return 0;
                    316:        case 'i': 
                    317:                ext(INTERNAL);
                    318:        case 0:
                    319:        case 'e':
                    320:        case 'l':
                    321:        case 's':
                    322:                if (MAXERR<++error_count) {
                    323:                        fprintf(stderr,"Sorry, too many errors\n");
                    324:                        ext(7);
                    325:                }
                    326:        }
                    327: 
                    328:        in_error = 0;
                    329:        return 0;
                    330: }
                    331: 
                    332: 
                    333: 
                    334: #ifdef DBG
                    335: #define OPEREP(v) ((v)>MAXTOK || (v)<=0 ? 0 : keys[v])
                    336: void
                    337: display_type( Ptype t )
                    338: {
                    339:        if ( t ) { putc(' ',stderr);
                    340:                FILE * of = out_file;
                    341:                out_file = stderr;
                    342:                extern int ntok; int nt = ntok;
                    343:                emode=1; (t)->dcl_print(0); emode=0;
                    344:                //fprintf(stderr," <node %d",t->node::id);
                    345:                if(!t->allocated)fprintf(stderr," UNALLOCATED!");
                    346:                //putc('>',stderr);
                    347:                ntok = nt;
                    348:                out_file = of;
                    349:        } else fprintf(stderr," <null type>");
                    350: }
                    351: #define INDENT(in) { for ( int i = in;  i > 0;  --i ) fprintf(stderr,"  "); }
                    352: static indent = 0;
                    353: 
                    354: void
                    355: display_expr( Pexpr ptr, char* label, int oneline )
                    356: {
                    357:        INDENT(indent);
                    358:        if ( label ) fprintf(stderr, "%s:", label);
                    359:        if ( ptr == 0 ) {
                    360:                fprintf(stderr, "NULL EXPR\n" );
                    361:                return;
                    362:        }
                    363:        fprintf(stderr,"%d",ptr->node::id);
                    364:        if(!ptr->allocated)fprintf(stderr," UNALLOCATED!");
                    365:        putc(':',stderr);
                    366:        char* s = OPEREP(ptr->base);
                    367:        if ( s == 0 )
                    368:                fprintf(stderr, "token(%d)", ptr->base );
                    369:        else
                    370:                fprintf(stderr,"%s",s);
                    371:        if ( ptr->displayed ) { // recursion!!!
                    372:                if ( ptr->base == NAME )
                    373:                        fprintf(stderr," '%s'",Pname(ptr)->string);
                    374:                fprintf(stderr,"   RECURSION!!!\n");
                    375:                ptr->displayed = 0;
                    376:                return;
                    377:        }
                    378:        ptr->displayed = 1;
                    379:        switch ( ptr->base ) {
                    380:        case QUEST:
                    381:                display_type(ptr->tp);
                    382:                putc('\n',stderr);
                    383:                if ( !oneline ) {
                    384:                        ++indent;
                    385:                        display_expr( ptr->cond, "cond" ); 
                    386:                        display_expr( ptr->e1, "e1" ); 
                    387:                        display_expr( ptr->e2, "e2" ); 
                    388:                        --indent;
                    389:                }
                    390:                break;
                    391:        case REF: case DOT:
                    392:                display_type(ptr->tp);
                    393:                putc('\n',stderr);
                    394:                if ( !oneline ) {
                    395:                        ++indent;
                    396:                        display_expr( ptr->e1, "e1" );
                    397:                        display_expr( ptr->mem, "mem" );
                    398:                        --indent;
                    399:                }
                    400:                break;
                    401:        case MDOT:
                    402:                display_type(ptr->tp);
                    403:                fprintf(stderr," string2:'%s'\n",ptr->string2?ptr->string2:"");
                    404:                if ( !oneline ) {
                    405:                        ++indent;
                    406:                        display_expr( ptr->mem, "mem" );
                    407:                        --indent;
                    408:                }
                    409:                break;
                    410:        case ICALL:
                    411:                fprintf(stderr," fn=='%s'",ptr->il->fct_name->string);
                    412:                display_type(ptr->tp);
                    413:                putc('\n',stderr);
                    414:                if ( !oneline ) {
                    415:                        ++indent;
                    416:                        for ( int i = 0;  i < ptr->il->i_slots;  ++i ) {
                    417:                                ia *aa = &ptr->il->i_args[i];
                    418:                                INDENT(indent);
                    419:                                fprintf(stderr,"arg:'%s'",aa->local&&aa->local->string?aa->local->string:"");
                    420:                                display_type(aa->tp);
                    421:                                putc('\n',stderr);
                    422:                                ++indent;
                    423:                                display_expr( aa->arg, "actual" );
                    424:                                --indent;
                    425:                        }
                    426:                        display_expr( ptr->e1, "e1" );
                    427:                        display_expr( ptr->e2, "e2" );
                    428:                        --indent;
                    429:                }
                    430:                break;
                    431:        case SIZEOF:
                    432:                if ( ptr->tp2 ) {
                    433:                        putc('(',stderr);
                    434:                        display_type(ptr->tp2);
                    435:                        putc(')',stderr);
                    436:                }
                    437:                display_type(ptr->tp);
                    438:                putc('\n',stderr);
                    439:                if ( !oneline ) {
                    440:                    if ( ptr->e1 ) {
                    441:                        ++indent;
                    442:                        display_expr(ptr->e1,"e1");
                    443:                        --indent;
                    444:                    }
                    445:                    if ( ptr->e2 ) {
                    446:                        ++indent;
                    447:                        display_expr(ptr->e2,"e2");
                    448:                        --indent;
                    449:                    }
                    450:                }
                    451:                break;
                    452:        case ZERO:
                    453:                display_type(ptr->tp);
                    454:                putc('\n',stderr);
                    455:                break;
                    456:        case NAME: case TNAME: case STRING:
                    457:        case ICON: case ID:
                    458:        case FCON: case CCON:
                    459:        case IVAL:
                    460:                fprintf(stderr," '%s'",(ptr->string)?ptr->string:"<0>");
                    461:                display_type(ptr->tp);
                    462:                if(ptr->string2)fprintf(stderr," string2=='%s'",ptr->string2);
                    463:                if ( ptr->permanent ) fprintf(stderr, " (permanent)");
                    464:                if ( ptr->base == IVAL ) fprintf(stderr, " i1==%d", ptr->i1);
                    465:                putc('\n',stderr);
                    466:                if ( !oneline && (ptr->base == NAME || ptr->base == TNAME) ) {
                    467:                        Pname n = Pname(ptr);
                    468:                        ++indent;
                    469:                        INDENT(indent);
                    470:                        fprintf(stderr, "n_sto==%d", n->n_sto );
                    471:                        fprintf(stderr, " n_stclass==%d",n->n_stclass);
                    472:                        fprintf(stderr, " n_scope==%d",n->n_scope);
                    473:                        fprintf(stderr, " n_protected==%d\n", n->n_protect );
                    474:                        INDENT(indent);
                    475:                        fprintf(stderr, "n_oper=='%s'", (s=OPEREP(n->base))?s:"0");
                    476:                        fprintf(stderr, " n_val==%d", n->n_val );
                    477:                        fprintf(stderr, " n_xref==%d",n->n_xref);
                    478:                        fprintf(stderr, " lex_level==%d\n", n->lex_level );
                    479:                        INDENT(indent);
                    480:                        fprintf(stderr, "n_used==%d",n->n_used);
                    481:                        fprintf(stderr, " n_assigned_to==%d",n->n_assigned_to);
                    482:                        fprintf(stderr, " n_addr_taken==%d\n",n->n_addr_taken );
                    483:                        INDENT(indent);
                    484:                        fprintf(stderr, "n_union==%d", n->n_union);
                    485:                        fprintf(stderr, " n_list=='%s'", n->n_list?n->n_list->string:"<0>" );
                    486:                        fprintf(stderr, " n_qualifier=='%s'", n->n_qualifier?n->n_qualifier->string:"<0>" ); 
                    487:                        if ( n->n_initializer ) {
                    488:                                fprintf(stderr, " n_initializer:\n" );
                    489:                                ++indent;
                    490:                                display_expr( n->n_initializer );
                    491:                                --indent;
                    492:                        } else fprintf(stderr, " n_initializer==<0>\n");
                    493:                        --indent;
                    494:                }
                    495:                break;
                    496:        case BLOCK:
                    497:                ((Pstmt)ptr)->where.put(stderr); putc(' ',stderr);
                    498:                putc('\n',stderr);
                    499:                break;
                    500:        default:
                    501:                display_type(ptr->tp);
                    502:                putc('\n',stderr);
                    503:                if ( !oneline && ptr->base > 0
                    504:                &&   (ptr->base<165 || ptr->base==MEMPTR) ) {
                    505:                        ++indent;
                    506:                        display_expr( ptr->e1, "e1" ); 
                    507:                        display_expr( ptr->e2, "e2" );
                    508:                        --indent;
                    509:                }
                    510:                break;
                    511:        }
                    512:        ptr->displayed = 0;
                    513:        return;
                    514: }
                    515: void
                    516: display_stmt( Pstmt ptr, char* label, int oneline )
                    517: {
                    518:        INDENT(indent);
                    519:        if ( label ) fprintf(stderr, "%s:", label);
                    520:        if ( ptr == 0 ) {
                    521:                fprintf(stderr, "NULL STMT\n" );
                    522:                return;
                    523:        }
                    524:        fprintf(stderr,"%d",ptr->node::id);
                    525:        if(!ptr->allocated)fprintf(stderr," UNALLOCATED!");
                    526:        putc(':',stderr);
                    527:        char* s = OPEREP(ptr->base);
                    528:        if ( s == 0 )
                    529:                fprintf(stderr, "token(%d)", ptr->base );
                    530:        else
                    531:                fprintf(stderr,"%s",s);
                    532:        if ( ptr->displayed ) { // recursion!!!
                    533:                if ( ptr->base == NAME )
                    534:                        fprintf(stderr," '%s'",Pname(ptr)->string);
                    535:                fprintf(stderr,"   RECURSION!!!\n");
                    536:                ptr->displayed = 0;
                    537:                return;
                    538:        }
                    539:        putc(' ',stderr);
                    540:        ptr->where.put(stderr);
                    541:        if ( oneline ) { putc('\n',stderr); return; }
                    542:        ptr->displayed = 1;
                    543:        switch ( ptr->base ) {
                    544:        case BLOCK:
                    545:                fprintf(stderr," .. ");
                    546:                ptr->where2.put(stderr);
                    547:                putc('\n',stderr);
                    548:                ++indent;
                    549:                { for( Pstmt st = ptr->s;  st;  st = st->s_list )
                    550:                        display_stmt( ptr->s, "s" ); 
                    551:                }
                    552:                --indent;
                    553:                break;
                    554:        case IF:
                    555:                putc('\n',stderr);
                    556:                ++indent;
                    557:                display_expr(ptr->e,"cond");
                    558:                display_stmt(ptr->s,"if-clause");
                    559:                --indent;
                    560:                if ( ptr->else_stmt ) {
                    561:                        INDENT(indent);
                    562:                        fprintf(stderr,"else\n");
                    563:                        ++indent;
                    564:                        display_stmt(ptr->else_stmt,"else-clause");
                    565:                        --indent;
                    566:                }
                    567:                break;
                    568:        case DO:
                    569:                putc('\n',stderr);
                    570:                ++indent;
                    571:                display_stmt(ptr->s,"do-stmt");
                    572:                display_expr(ptr->e,"cond");
                    573:                --indent;
                    574:                break;
                    575:        case WHILE:
                    576:                putc('\n',stderr);
                    577:                ++indent;
                    578:                display_expr(ptr->e,"cond");
                    579:                display_stmt(ptr->s,"while-stmt");
                    580:                --indent;
                    581:                break;
                    582:        case FOR:
                    583:                putc('\n',stderr);
                    584:                ++indent;
                    585:                display_stmt(ptr->for_init,"init");
                    586:                display_expr(ptr->e,"cond");
                    587:                display_expr(ptr->e2,"incr");
                    588:                display_stmt(ptr->s,"stmt");
                    589:                --indent;
                    590:                break;
                    591:        case RETURN:
                    592:                putc('\n',stderr);
                    593:                ++indent;
                    594:                display_expr(ptr->e,"e");
                    595:                --indent;
                    596:                break;
                    597:        case SM:
                    598:                putc('\n',stderr);
                    599:                ++indent;
                    600:                display_expr(ptr->e,"e");
                    601:                --indent;
                    602:                break;
                    603:        default:
                    604:                putc('\n',stderr);
                    605:                break;
                    606:        }
                    607:        ptr->displayed = 0;
                    608:        return;
                    609: }
                    610: 
                    611: void
                    612: display_namelist( Plist nl, char* s, int verbose )
                    613: {
                    614:        error('d',"namelist:    %s",s?s:"");
                    615:        ++indent;
                    616:        for ( Plist l = nl;  l;  l = l->l ) {
                    617:                Pname n = l->f;
                    618:                INDENT(indent);
                    619:                fprintf(stderr,"        %d",n);
                    620:                error('D'," %n %k n_key %d",n,n?n->tp->base:0,n?n->n_key:0);
                    621:                if ( verbose ) {
                    622:                        ++indent;
                    623:                        display_expr(n);
                    624:                        --indent;
                    625:                }
                    626:        }
                    627:        --indent;
                    628: }
                    629: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.