|
|
1.1 ! root 1: /*ident "@(#)ctrans:src/error.c 1.4" */ ! 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') lc->put(out_file); ! 178: } else { ! 179: if(t != 'D') print_loc(); ! 180: } ! 181: ! 182: switch (t) { ! 183: case 0: ! 184: putstring("error: "); ! 185: break; ! 186: case 'd': ! 187: putstring("DEBUG: "); ! 188: case 'D': ! 189: break; ! 190: case 'w': ! 191: no_of_warnings++; ! 192: putstring("warning: "); ! 193: break; ! 194: case 'l': ! 195: putstring("compiler limit exceeded: "); ! 196: break; ! 197: case 's': ! 198: putstring("sorry, not implemented: "); ! 199: break; ! 200: case 'i': ! 201: if (error_count++) { ! 202: fprintf(out_file,"sorry, %s cannot recover from earlier errors\n",prog_name); ! 203: ext(INTERNAL); ! 204: } ! 205: else ! 206: fprintf(out_file,"internal %s error: ",prog_name); ! 207: } ! 208: ! 209: ea argv[4]; ! 210: ea* a = argv; ! 211: argv[0] = a0; ! 212: argv[1] = a1; ! 213: argv[2] = a2; ! 214: argv[3] = a3; ! 215: ! 216: int c; ! 217: ! 218: while (c = *s++) { ! 219: if ('A'<=c && c<='Z') ! 220: putstring(abbrev_tbl[c-'A']); ! 221: else if (c == '%') { ! 222: switch (c = *s++) { ! 223: case 'k': // TOK assumed passed as an int ! 224: { TOK x = TOK(a->i); ! 225: if (0<x && x<=MAXTOK && keys[x]) ! 226: fprintf(out_file," %s",keys[x]); ! 227: else ! 228: fprintf(out_file," token(%d)",x); ! 229: break; ! 230: } ! 231: case 't': // Ptype ! 232: { Ptype tt = Ptype(a->p); ! 233: if (tt == 0) break; ! 234: ! 235: putch(' '); ! 236: ! 237: int nt = ntok; ! 238: emode = 1; ! 239: tt->dcl_print(0); ! 240: emode = 0; ! 241: ntok = nt; ! 242: break; ! 243: } ! 244: case 'n': // Pname ! 245: { Pname nn = Pname(a->p); ! 246: if (nn && nn->string) { ! 247: // suppress generated class names: ! 248: if (nn->string[0]=='_' ! 249: && nn->string[1]=='_' ! 250: && nn->string[2]=='C') break; ! 251: emode = 1; ! 252: putch(' '); ! 253: nn->print(); ! 254: emode = 0; ! 255: } ! 256: else ! 257: putstring(" ?"); ! 258: break; ! 259: } ! 260: case 'p': // pointer ! 261: { char* f = sizeof(char*)==sizeof(int)?" %d":" %ld"; ! 262: fprintf(out_file,f,a->p); ! 263: break; ! 264: } ! 265: case 'c': // char assumed passed as an int ! 266: putch(a->i); ! 267: break; ! 268: ! 269: case 'd': // int ! 270: fprintf(out_file," %d",a->i); ! 271: break; ! 272: ! 273: case 'o': // int ! 274: fprintf(out_file," %o",a->i); ! 275: break; ! 276: ! 277: case 's': // char* ! 278: char *s = ((char *)a->p); ! 279: if ( s ) putst((char*)a->p); ! 280: break; ! 281: } ! 282: a++; ! 283: } ! 284: else ! 285: putch(c); ! 286: } ! 287: ! 288: /* ! 289: switch (t) { ! 290: case 'd': ! 291: case 't': ! 292: case 'w': ! 293: putch('\n'); ! 294: break; ! 295: default: ! 296: */ ! 297: print_context(); ! 298: /* ! 299: } ! 300: */ ! 301: ! 302: templ_inst::head->print_error_loc(); ! 303: fflush(stderr); ! 304: if (!scan_started && t!='d' && t!='w') ext(4); ! 305: ! 306: // now we may want to carry on ! 307: out_file = of; ! 308: switch (t) { ! 309: case 't': ! 310: if (--in_error) return 0; ! 311: case 'i': ! 312: ext(INTERNAL); ! 313: case 0: ! 314: case 'l': ! 315: case 's': ! 316: if (MAXERR<++error_count) { ! 317: fprintf(stderr,"Sorry, too many errors\n"); ! 318: ext(7); ! 319: } ! 320: } ! 321: ! 322: in_error = 0; ! 323: return 0; ! 324: } ! 325: ! 326: ! 327: ! 328: #ifdef DBG ! 329: #define OPEREP(v) ((v)>MAXTOK || (v)<=0 ? 0 : keys[v]) ! 330: void ! 331: display_type( Ptype t ) ! 332: { ! 333: if ( t ) { putc(' ',stderr); ! 334: FILE * of = out_file; ! 335: out_file = stderr; ! 336: extern int ntok; int nt = ntok; ! 337: emode=1; (t)->dcl_print(0); emode=0; ! 338: //fprintf(stderr," <node %d",t->node::id); ! 339: if(!t->allocated)fprintf(stderr," UNALLOCATED!"); ! 340: //putc('>',stderr); ! 341: ntok = nt; ! 342: out_file = of; ! 343: } else fprintf(stderr," <null type>"); ! 344: } ! 345: #define INDENT(in) { for ( int i = in; i > 0; --i ) fprintf(stderr," "); } ! 346: static indent = 0; ! 347: ! 348: void ! 349: display_expr( Pexpr ptr, char* label, int oneline ) ! 350: { ! 351: INDENT(indent); ! 352: if ( label ) fprintf(stderr, "%s:", label); ! 353: if ( ptr == 0 ) { ! 354: fprintf(stderr, "NULL EXPR\n" ); ! 355: return; ! 356: } ! 357: fprintf(stderr,"%d",ptr->node::id); ! 358: if(!ptr->allocated)fprintf(stderr," UNALLOCATED!"); ! 359: putc(':',stderr); ! 360: char* s = OPEREP(ptr->base); ! 361: if ( s == 0 ) ! 362: fprintf(stderr, "token(%d)", ptr->base ); ! 363: else ! 364: fprintf(stderr,"%s",s); ! 365: if ( ptr->displayed ) { // recursion!!! ! 366: if ( ptr->base == NAME ) ! 367: fprintf(stderr," '%s'",Pname(ptr)->string); ! 368: fprintf(stderr," RECURSION!!!\n"); ! 369: ptr->displayed = 0; ! 370: return; ! 371: } ! 372: ptr->displayed = 1; ! 373: switch ( ptr->base ) { ! 374: case QUEST: ! 375: display_type(ptr->tp); ! 376: putc('\n',stderr); ! 377: if ( !oneline ) { ! 378: ++indent; ! 379: display_expr( ptr->cond, "cond" ); ! 380: display_expr( ptr->e1, "e1" ); ! 381: display_expr( ptr->e2, "e2" ); ! 382: --indent; ! 383: } ! 384: break; ! 385: case REF: case DOT: ! 386: display_type(ptr->tp); ! 387: putc('\n',stderr); ! 388: if ( !oneline ) { ! 389: ++indent; ! 390: display_expr( ptr->e1, "e1" ); ! 391: display_expr( ptr->mem, "mem" ); ! 392: --indent; ! 393: } ! 394: break; ! 395: case MDOT: ! 396: display_type(ptr->tp); ! 397: fprintf(stderr," string2:'%s'\n",ptr->string2?ptr->string2:""); ! 398: if ( !oneline ) { ! 399: ++indent; ! 400: display_expr( ptr->mem, "mem" ); ! 401: --indent; ! 402: } ! 403: break; ! 404: case ICALL: ! 405: fprintf(stderr," fn=='%s'",ptr->il->fct_name->string); ! 406: display_type(ptr->tp); ! 407: putc('\n',stderr); ! 408: if ( !oneline ) { ! 409: ++indent; ! 410: for ( int i = 0; i < ptr->il->i_slots; ++i ) { ! 411: ia *aa = &ptr->il->i_args[i]; ! 412: INDENT(indent); ! 413: fprintf(stderr,"arg:'%s'",aa->local&&aa->local->string?aa->local->string:""); ! 414: display_type(aa->tp); ! 415: putc('\n',stderr); ! 416: ++indent; ! 417: display_expr( aa->arg, "actual" ); ! 418: --indent; ! 419: } ! 420: display_expr( ptr->e1, "e1" ); ! 421: display_expr( ptr->e2, "e2" ); ! 422: --indent; ! 423: } ! 424: break; ! 425: case SIZEOF: ! 426: if ( ptr->tp2 ) { ! 427: putc('(',stderr); ! 428: display_type(ptr->tp2); ! 429: putc(')',stderr); ! 430: } ! 431: display_type(ptr->tp); ! 432: putc('\n',stderr); ! 433: if ( !oneline ) { ! 434: if ( ptr->e1 ) { ! 435: ++indent; ! 436: display_expr(ptr->e1,"e1"); ! 437: --indent; ! 438: } ! 439: if ( ptr->e2 ) { ! 440: ++indent; ! 441: display_expr(ptr->e2,"e2"); ! 442: --indent; ! 443: } ! 444: } ! 445: break; ! 446: case ZERO: ! 447: display_type(ptr->tp); ! 448: putc('\n',stderr); ! 449: break; ! 450: case NAME: case TNAME: case STRING: ! 451: case ICON: case ID: ! 452: case FCON: case CCON: ! 453: case IVAL: ! 454: fprintf(stderr," '%s'",(ptr->string)?ptr->string:"<0>"); ! 455: display_type(ptr->tp); ! 456: if(ptr->string2)fprintf(stderr," string2=='%s'",ptr->string2); ! 457: if ( ptr->permanent ) fprintf(stderr, " (permanent)"); ! 458: if ( ptr->base == IVAL ) fprintf(stderr, " i1==%d", ptr->i1); ! 459: putc('\n',stderr); ! 460: if ( !oneline && (ptr->base == NAME || ptr->base == TNAME) ) { ! 461: Pname n = Pname(ptr); ! 462: ++indent; ! 463: INDENT(indent); ! 464: fprintf(stderr, "n_sto==%d", n->n_sto ); ! 465: fprintf(stderr, " n_stclass==%d",n->n_stclass); ! 466: fprintf(stderr, " n_scope==%d",n->n_scope); ! 467: fprintf(stderr, " n_protected==%d\n", n->n_protect ); ! 468: INDENT(indent); ! 469: fprintf(stderr, "n_oper=='%s'", (s=OPEREP(n->base))?s:"0"); ! 470: fprintf(stderr, " n_val==%d", n->n_val ); ! 471: fprintf(stderr, " n_xref==%d",n->n_xref); ! 472: fprintf(stderr, " lex_level==%d\n", n->lex_level ); ! 473: INDENT(indent); ! 474: fprintf(stderr, "n_used==%d",n->n_used); ! 475: fprintf(stderr, " n_assigned_to==%d",n->n_assigned_to); ! 476: fprintf(stderr, " n_addr_taken==%d\n",n->n_addr_taken ); ! 477: INDENT(indent); ! 478: fprintf(stderr, "n_union==%d", n->n_union); ! 479: fprintf(stderr, " n_list=='%s'", n->n_list?n->n_list->string:"<0>" ); ! 480: fprintf(stderr, " n_qualifier=='%s'", n->n_qualifier?n->n_qualifier->string:"<0>" ); ! 481: if ( n->n_initializer ) { ! 482: fprintf(stderr, " n_initializer:\n" ); ! 483: ++indent; ! 484: display_expr( n->n_initializer ); ! 485: --indent; ! 486: } else fprintf(stderr, " n_initializer==<0>\n"); ! 487: --indent; ! 488: } ! 489: break; ! 490: case BLOCK: ! 491: ((Pstmt)ptr)->where.put(stderr); putc(' ',stderr); ! 492: putc('\n',stderr); ! 493: break; ! 494: default: ! 495: display_type(ptr->tp); ! 496: putc('\n',stderr); ! 497: if ( !oneline && ptr->base > 0 ! 498: && (ptr->base<165 || ptr->base==MEMPTR) ) { ! 499: ++indent; ! 500: display_expr( ptr->e1, "e1" ); ! 501: display_expr( ptr->e2, "e2" ); ! 502: --indent; ! 503: } ! 504: break; ! 505: } ! 506: ptr->displayed = 0; ! 507: return; ! 508: } ! 509: void ! 510: display_stmt( Pstmt ptr, char* label, int oneline ) ! 511: { ! 512: INDENT(indent); ! 513: if ( label ) fprintf(stderr, "%s:", label); ! 514: if ( ptr == 0 ) { ! 515: fprintf(stderr, "NULL STMT\n" ); ! 516: return; ! 517: } ! 518: fprintf(stderr,"%d",ptr->node::id); ! 519: if(!ptr->allocated)fprintf(stderr," UNALLOCATED!"); ! 520: putc(':',stderr); ! 521: char* s = OPEREP(ptr->base); ! 522: if ( s == 0 ) ! 523: fprintf(stderr, "token(%d)", ptr->base ); ! 524: else ! 525: fprintf(stderr,"%s",s); ! 526: if ( ptr->displayed ) { // recursion!!! ! 527: if ( ptr->base == NAME ) ! 528: fprintf(stderr," '%s'",Pname(ptr)->string); ! 529: fprintf(stderr," RECURSION!!!\n"); ! 530: ptr->displayed = 0; ! 531: return; ! 532: } ! 533: putc(' ',stderr); ! 534: ptr->where.put(stderr); ! 535: if ( oneline ) { putc('\n',stderr); return; } ! 536: ptr->displayed = 1; ! 537: switch ( ptr->base ) { ! 538: case BLOCK: ! 539: fprintf(stderr," .. "); ! 540: ptr->where2.put(stderr); ! 541: putc('\n',stderr); ! 542: ++indent; ! 543: { for( Pstmt st = ptr->s; st; st = st->s_list ) ! 544: display_stmt( ptr->s, "s" ); ! 545: } ! 546: --indent; ! 547: break; ! 548: case IF: ! 549: putc('\n',stderr); ! 550: ++indent; ! 551: display_expr(ptr->e,"cond"); ! 552: display_stmt(ptr->s,"if-clause"); ! 553: --indent; ! 554: if ( ptr->else_stmt ) { ! 555: INDENT(indent); ! 556: fprintf(stderr,"else\n"); ! 557: ++indent; ! 558: display_stmt(ptr->else_stmt,"else-clause"); ! 559: --indent; ! 560: } ! 561: break; ! 562: case DO: ! 563: putc('\n',stderr); ! 564: ++indent; ! 565: display_stmt(ptr->s,"do-stmt"); ! 566: display_expr(ptr->e,"cond"); ! 567: --indent; ! 568: break; ! 569: case WHILE: ! 570: putc('\n',stderr); ! 571: ++indent; ! 572: display_expr(ptr->e,"cond"); ! 573: display_stmt(ptr->s,"while-stmt"); ! 574: --indent; ! 575: break; ! 576: case FOR: ! 577: putc('\n',stderr); ! 578: ++indent; ! 579: display_stmt(ptr->for_init,"init"); ! 580: display_expr(ptr->e,"cond"); ! 581: display_expr(ptr->e2,"incr"); ! 582: display_stmt(ptr->s,"stmt"); ! 583: --indent; ! 584: break; ! 585: case RETURN: ! 586: putc('\n',stderr); ! 587: ++indent; ! 588: display_expr(ptr->e,"e"); ! 589: --indent; ! 590: break; ! 591: case SM: ! 592: putc('\n',stderr); ! 593: ++indent; ! 594: display_expr(ptr->e,"e"); ! 595: --indent; ! 596: break; ! 597: default: ! 598: putc('\n',stderr); ! 599: break; ! 600: } ! 601: ptr->displayed = 0; ! 602: return; ! 603: } ! 604: ! 605: void ! 606: display_namelist( Plist nl, char* s, int verbose ) ! 607: { ! 608: error('d',"namelist: %s",s?s:""); ! 609: ++indent; ! 610: for ( Plist l = nl; l; l = l->l ) { ! 611: Pname n = l->f; ! 612: INDENT(indent); ! 613: fprintf(stderr," %d",n); ! 614: error('D'," %n %k n_key %d",n,n?n->tp->base:0,n?n->n_key:0); ! 615: if ( verbose ) { ! 616: ++indent; ! 617: display_expr(n); ! 618: --indent; ! 619: } ! 620: } ! 621: --indent; ! 622: } ! 623: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.