github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/cmd/gc/lex.c (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 #include <u.h> 6 #include <libc.h> 7 #include "go.h" 8 #include "y.tab.h" 9 #include <ar.h> 10 11 #undef getc 12 #undef ungetc 13 #define getc ccgetc 14 #define ungetc ccungetc 15 16 extern int yychar; 17 int yyprev; 18 int yylast; 19 20 static void lexinit(void); 21 static void lexinit1(void); 22 static void lexfini(void); 23 static void yytinit(void); 24 static int getc(void); 25 static void ungetc(int); 26 static int32 getr(void); 27 static int escchar(int, int*, vlong*); 28 static void addidir(char*); 29 static int getlinepragma(void); 30 static char *goos, *goarch, *goroot; 31 32 #define BOM 0xFEFF 33 34 // Compiler experiments. 35 // These are controlled by the GOEXPERIMENT environment 36 // variable recorded when the compiler is built. 37 static struct { 38 char *name; 39 int *val; 40 } exper[] = { 41 // {"rune32", &rune32}, 42 {"fieldtrack", &fieldtrack_enabled}, 43 {"precisestack", &precisestack_enabled}, 44 {nil, nil}, 45 }; 46 47 // Debug arguments. 48 // These can be specified with the -d flag, as in "-d checknil" 49 // to set the debug_checknil variable. In general the list passed 50 // to -d can be comma-separated. 51 static struct { 52 char *name; 53 int *val; 54 } debugtab[] = { 55 {"nil", &debug_checknil}, 56 {nil, nil}, 57 }; 58 59 static void 60 addexp(char *s) 61 { 62 int i; 63 64 for(i=0; exper[i].name != nil; i++) { 65 if(strcmp(exper[i].name, s) == 0) { 66 *exper[i].val = 1; 67 return; 68 } 69 } 70 71 print("unknown experiment %s\n", s); 72 exits("unknown experiment"); 73 } 74 75 static void 76 setexp(void) 77 { 78 char *f[20]; 79 int i, nf; 80 81 precisestack_enabled = 1; // on by default 82 83 // cmd/dist #defines GOEXPERIMENT for us. 84 nf = getfields(GOEXPERIMENT, f, nelem(f), 1, ","); 85 for(i=0; i<nf; i++) 86 addexp(f[i]); 87 } 88 89 char* 90 expstring(void) 91 { 92 int i; 93 static char buf[512]; 94 95 strcpy(buf, "X"); 96 for(i=0; exper[i].name != nil; i++) 97 if(*exper[i].val) 98 seprint(buf+strlen(buf), buf+sizeof buf, ",%s", exper[i].name); 99 if(strlen(buf) == 1) 100 strcpy(buf, "X,none"); 101 buf[1] = ':'; 102 return buf; 103 } 104 105 // Our own isdigit, isspace, isalpha, isalnum that take care 106 // of EOF and other out of range arguments. 107 static int 108 yy_isdigit(int c) 109 { 110 return c >= 0 && c <= 0xFF && isdigit(c); 111 } 112 113 static int 114 yy_isspace(int c) 115 { 116 return c == ' ' || c == '\t' || c == '\n' || c == '\r'; 117 } 118 119 static int 120 yy_isalpha(int c) 121 { 122 return c >= 0 && c <= 0xFF && isalpha(c); 123 } 124 125 static int 126 yy_isalnum(int c) 127 { 128 return c >= 0 && c <= 0xFF && isalnum(c); 129 } 130 131 // Disallow use of isdigit etc. 132 #undef isdigit 133 #undef isspace 134 #undef isalpha 135 #undef isalnum 136 #define isdigit use_yy_isdigit_instead_of_isdigit 137 #define isspace use_yy_isspace_instead_of_isspace 138 #define isalpha use_yy_isalpha_instead_of_isalpha 139 #define isalnum use_yy_isalnum_instead_of_isalnum 140 141 #define DBG if(!debug['x']){}else print 142 enum 143 { 144 EOF = -1, 145 }; 146 147 void 148 usage(void) 149 { 150 print("usage: %cg [options] file.go...\n", thechar); 151 flagprint(1); 152 exits("usage"); 153 } 154 155 void 156 fault(int s) 157 { 158 USED(s); 159 160 // If we've already complained about things 161 // in the program, don't bother complaining 162 // about the seg fault too; let the user clean up 163 // the code and try again. 164 if(nsavederrors + nerrors > 0) 165 errorexit(); 166 fatal("fault"); 167 } 168 169 #ifdef PLAN9 170 void 171 catcher(void *v, char *s) 172 { 173 USED(v); 174 175 if(strncmp(s, "sys: trap: fault read", 21) == 0) { 176 if(nsavederrors + nerrors > 0) 177 errorexit(); 178 fatal("fault"); 179 } 180 noted(NDFLT); 181 } 182 #endif 183 184 void 185 doversion(void) 186 { 187 char *p; 188 189 p = expstring(); 190 if(strcmp(p, "X:none") == 0) 191 p = ""; 192 print("%cg version %s%s%s\n", thechar, getgoversion(), *p ? " " : "", p); 193 exits(0); 194 } 195 196 int 197 main(int argc, char *argv[]) 198 { 199 int i; 200 NodeList *l; 201 char *p; 202 203 #ifdef SIGBUS 204 signal(SIGBUS, fault); 205 signal(SIGSEGV, fault); 206 #endif 207 208 #ifdef PLAN9 209 notify(catcher); 210 // Tell the FPU to handle all exceptions. 211 setfcr(FPPDBL|FPRNR); 212 #endif 213 // Allow GOARCH=thestring or GOARCH=thestringsuffix, 214 // but not other values. 215 p = getgoarch(); 216 if(strncmp(p, thestring, strlen(thestring)) != 0) 217 sysfatal("cannot use %cg with GOARCH=%s", thechar, p); 218 goarch = p; 219 220 linkarchinit(); 221 ctxt = linknew(thelinkarch); 222 ctxt->diag = yyerror; 223 ctxt->bso = &bstdout; 224 Binit(&bstdout, 1, OWRITE); 225 226 localpkg = mkpkg(strlit("")); 227 localpkg->prefix = "\"\""; 228 229 // pseudo-package, for scoping 230 builtinpkg = mkpkg(strlit("go.builtin")); 231 232 // pseudo-package, accessed by import "unsafe" 233 unsafepkg = mkpkg(strlit("unsafe")); 234 unsafepkg->name = "unsafe"; 235 236 // real package, referred to by generated runtime calls 237 runtimepkg = mkpkg(strlit("runtime")); 238 runtimepkg->name = "runtime"; 239 240 // pseudo-packages used in symbol tables 241 gostringpkg = mkpkg(strlit("go.string")); 242 gostringpkg->name = "go.string"; 243 gostringpkg->prefix = "go.string"; // not go%2estring 244 245 itabpkg = mkpkg(strlit("go.itab")); 246 itabpkg->name = "go.itab"; 247 itabpkg->prefix = "go.itab"; // not go%2eitab 248 249 weaktypepkg = mkpkg(strlit("go.weak.type")); 250 weaktypepkg->name = "go.weak.type"; 251 weaktypepkg->prefix = "go.weak.type"; // not go%2eweak%2etype 252 253 typelinkpkg = mkpkg(strlit("go.typelink")); 254 typelinkpkg->name = "go.typelink"; 255 typelinkpkg->prefix = "go.typelink"; // not go%2etypelink 256 257 trackpkg = mkpkg(strlit("go.track")); 258 trackpkg->name = "go.track"; 259 trackpkg->prefix = "go.track"; // not go%2etrack 260 261 typepkg = mkpkg(strlit("type")); 262 typepkg->name = "type"; 263 264 goroot = getgoroot(); 265 goos = getgoos(); 266 267 nacl = strcmp(goos, "nacl") == 0; 268 if(nacl) 269 flag_largemodel = 1; 270 271 setexp(); 272 273 outfile = nil; 274 flagcount("+", "compiling runtime", &compiling_runtime); 275 flagcount("%", "debug non-static initializers", &debug['%']); 276 flagcount("A", "for bootstrapping, allow 'any' type", &debug['A']); 277 flagcount("B", "disable bounds checking", &debug['B']); 278 flagstr("D", "path: set relative path for local imports", &localimport); 279 flagcount("E", "debug symbol export", &debug['E']); 280 flagfn1("I", "dir: add dir to import search path", addidir); 281 flagcount("K", "debug missing line numbers", &debug['K']); 282 flagcount("L", "use full (long) path in error messages", &debug['L']); 283 flagcount("M", "debug move generation", &debug['M']); 284 flagcount("N", "disable optimizations", &debug['N']); 285 flagcount("P", "debug peephole optimizer", &debug['P']); 286 flagcount("R", "debug register optimizer", &debug['R']); 287 flagcount("S", "print assembly listing", &debug['S']); 288 flagfn0("V", "print compiler version", doversion); 289 flagcount("W", "debug parse tree after type checking", &debug['W']); 290 flagcount("complete", "compiling complete package (no C or assembly)", &pure_go); 291 flagstr("d", "list: print debug information about items in list", &debugstr); 292 flagcount("e", "no limit on number of errors reported", &debug['e']); 293 flagcount("f", "debug stack frames", &debug['f']); 294 flagcount("g", "debug code generation", &debug['g']); 295 flagcount("h", "halt on error", &debug['h']); 296 flagcount("i", "debug line number stack", &debug['i']); 297 flagstr("installsuffix", "pkg directory suffix", &flag_installsuffix); 298 flagcount("j", "debug runtime-initialized variables", &debug['j']); 299 flagcount("l", "disable inlining", &debug['l']); 300 flagcount("live", "debug liveness analysis", &debuglive); 301 flagcount("m", "print optimization decisions", &debug['m']); 302 flagcount("nolocalimports", "reject local (relative) imports", &nolocalimports); 303 flagstr("o", "obj: set output file", &outfile); 304 flagstr("p", "path: set expected package import path", &myimportpath); 305 flagcount("pack", "write package file instead of object file", &writearchive); 306 flagcount("r", "debug generated wrappers", &debug['r']); 307 flagcount("race", "enable race detector", &flag_race); 308 flagcount("s", "warn about composite literals that can be simplified", &debug['s']); 309 flagstr("trimpath", "prefix: remove prefix from recorded source file paths", &ctxt->trimpath); 310 flagcount("u", "reject unsafe code", &safemode); 311 flagcount("v", "increase debug verbosity", &debug['v']); 312 flagcount("w", "debug type checking", &debug['w']); 313 flagcount("x", "debug lexer", &debug['x']); 314 flagcount("y", "debug declarations in canned imports (with -d)", &debug['y']); 315 if(thechar == '6') 316 flagcount("largemodel", "generate code that assumes a large memory model", &flag_largemodel); 317 318 flagparse(&argc, &argv, usage); 319 ctxt->debugasm = debug['S']; 320 321 if(argc < 1) 322 usage(); 323 324 if(flag_race) { 325 racepkg = mkpkg(strlit("runtime/race")); 326 racepkg->name = "race"; 327 } 328 329 // parse -d argument 330 if(debugstr) { 331 char *f[100]; 332 int i, j, nf; 333 334 nf = getfields(debugstr, f, nelem(f), 1, ","); 335 for(i=0; i<nf; i++) { 336 for(j=0; debugtab[j].name != nil; j++) { 337 if(strcmp(debugtab[j].name, f[i]) == 0) { 338 *debugtab[j].val = 1; 339 break; 340 } 341 } 342 if(j == nelem(debugtab)) 343 fatal("unknown debug information -d '%s'\n", f[i]); 344 } 345 } 346 347 // enable inlining. for now: 348 // default: inlining on. (debug['l'] == 1) 349 // -l: inlining off (debug['l'] == 0) 350 // -ll, -lll: inlining on again, with extra debugging (debug['l'] > 1) 351 if(debug['l'] <= 1) 352 debug['l'] = 1 - debug['l']; 353 354 if(thechar == '8') { 355 p = getgo386(); 356 if(strcmp(p, "387") == 0) 357 use_sse = 0; 358 else if(strcmp(p, "sse2") == 0) 359 use_sse = 1; 360 else 361 sysfatal("unsupported setting GO386=%s", p); 362 } 363 364 fmtinstallgo(); 365 betypeinit(); 366 if(widthptr == 0) 367 fatal("betypeinit failed"); 368 369 lexinit(); 370 typeinit(); 371 lexinit1(); 372 yytinit(); 373 374 blockgen = 1; 375 dclcontext = PEXTERN; 376 nerrors = 0; 377 lexlineno = 1; 378 379 for(i=0; i<argc; i++) { 380 infile = argv[i]; 381 linehist(infile, 0, 0); 382 383 curio.infile = infile; 384 curio.bin = Bopen(infile, OREAD); 385 if(curio.bin == nil) { 386 print("open %s: %r\n", infile); 387 errorexit(); 388 } 389 curio.peekc = 0; 390 curio.peekc1 = 0; 391 curio.nlsemi = 0; 392 curio.eofnl = 0; 393 curio.last = 0; 394 395 // Skip initial BOM if present. 396 if(Bgetrune(curio.bin) != BOM) 397 Bungetrune(curio.bin); 398 399 block = 1; 400 iota = -1000000; 401 402 yyparse(); 403 if(nsyntaxerrors != 0) 404 errorexit(); 405 406 linehist(nil, 0, 0); 407 if(curio.bin != nil) 408 Bterm(curio.bin); 409 } 410 testdclstack(); 411 mkpackage(localpkg->name); // final import not used checks 412 lexfini(); 413 414 typecheckok = 1; 415 if(debug['f']) 416 frame(1); 417 418 // Process top-level declarations in phases. 419 420 // Phase 1: const, type, and names and types of funcs. 421 // This will gather all the information about types 422 // and methods but doesn't depend on any of it. 423 defercheckwidth(); 424 for(l=xtop; l; l=l->next) 425 if(l->n->op != ODCL && l->n->op != OAS) 426 typecheck(&l->n, Etop); 427 428 // Phase 2: Variable assignments. 429 // To check interface assignments, depends on phase 1. 430 for(l=xtop; l; l=l->next) 431 if(l->n->op == ODCL || l->n->op == OAS) 432 typecheck(&l->n, Etop); 433 resumecheckwidth(); 434 435 // Phase 3: Type check function bodies. 436 for(l=xtop; l; l=l->next) { 437 if(l->n->op == ODCLFUNC || l->n->op == OCLOSURE) { 438 curfn = l->n; 439 saveerrors(); 440 typechecklist(l->n->nbody, Etop); 441 checkreturn(l->n); 442 if(nerrors != 0) 443 l->n->nbody = nil; // type errors; do not compile 444 } 445 } 446 447 curfn = nil; 448 449 if(nsavederrors+nerrors) 450 errorexit(); 451 452 // Phase 4: Inlining 453 if(debug['l'] > 1) { 454 // Typecheck imported function bodies if debug['l'] > 1, 455 // otherwise lazily when used or re-exported. 456 for(l=importlist; l; l=l->next) 457 if (l->n->inl) { 458 saveerrors(); 459 typecheckinl(l->n); 460 } 461 462 if(nsavederrors+nerrors) 463 errorexit(); 464 } 465 466 if(debug['l']) { 467 // Find functions that can be inlined and clone them before walk expands them. 468 for(l=xtop; l; l=l->next) 469 if(l->n->op == ODCLFUNC) 470 caninl(l->n); 471 472 // Expand inlineable calls in all functions 473 for(l=xtop; l; l=l->next) 474 if(l->n->op == ODCLFUNC) 475 inlcalls(l->n); 476 } 477 478 // Phase 5: Escape analysis. 479 if(!debug['N']) 480 escapes(xtop); 481 482 // Escape analysis moved escaped values off stack. 483 // Move large values off stack too. 484 movelarge(xtop); 485 486 // Phase 6: Compile top level functions. 487 for(l=xtop; l; l=l->next) 488 if(l->n->op == ODCLFUNC) 489 funccompile(l->n, 0); 490 491 if(nsavederrors+nerrors == 0) 492 fninit(xtop); 493 494 // Phase 7: Check external declarations. 495 for(l=externdcl; l; l=l->next) 496 if(l->n->op == ONAME) 497 typecheck(&l->n, Erv); 498 499 if(nerrors+nsavederrors) 500 errorexit(); 501 502 dumpobj(); 503 504 if(nerrors+nsavederrors) 505 errorexit(); 506 507 flusherrors(); 508 exits(0); 509 return 0; 510 } 511 512 void 513 saveerrors(void) 514 { 515 nsavederrors += nerrors; 516 nerrors = 0; 517 } 518 519 /* 520 * macro to portably read/write archive header. 521 * 'cmd' is read/write/Bread/Bwrite, etc. 522 */ 523 #define HEADER_IO(cmd, f, h) cmd(f, h.name, sizeof(h.name)) != sizeof(h.name)\ 524 || cmd(f, h.date, sizeof(h.date)) != sizeof(h.date)\ 525 || cmd(f, h.uid, sizeof(h.uid)) != sizeof(h.uid)\ 526 || cmd(f, h.gid, sizeof(h.gid)) != sizeof(h.gid)\ 527 || cmd(f, h.mode, sizeof(h.mode)) != sizeof(h.mode)\ 528 || cmd(f, h.size, sizeof(h.size)) != sizeof(h.size)\ 529 || cmd(f, h.fmag, sizeof(h.fmag)) != sizeof(h.fmag) 530 531 static int 532 arsize(Biobuf *b, char *name) 533 { 534 struct ar_hdr a; 535 536 if (HEADER_IO(Bread, b, a)) 537 return -1; 538 539 if(strncmp(a.name, name, strlen(name)) != 0) 540 return -1; 541 542 return atoi(a.size); 543 } 544 545 static int 546 skiptopkgdef(Biobuf *b) 547 { 548 char *p; 549 int sz; 550 551 /* archive header */ 552 if((p = Brdline(b, '\n')) == nil) 553 return 0; 554 if(Blinelen(b) != 8) 555 return 0; 556 if(memcmp(p, "!<arch>\n", 8) != 0) 557 return 0; 558 /* symbol table may be first; skip it */ 559 sz = arsize(b, "__.GOSYMDEF"); 560 if(sz >= 0) 561 Bseek(b, sz, 1); 562 else 563 Bseek(b, 8, 0); 564 /* package export block is next */ 565 sz = arsize(b, "__.PKGDEF"); 566 if(sz <= 0) 567 return 0; 568 return 1; 569 } 570 571 static void 572 addidir(char* dir) 573 { 574 Idir** pp; 575 576 if(dir == nil) 577 return; 578 579 for(pp = &idirs; *pp != nil; pp = &(*pp)->link) 580 ; 581 *pp = mal(sizeof(Idir)); 582 (*pp)->link = nil; 583 (*pp)->dir = dir; 584 } 585 586 // is this path a local name? begins with ./ or ../ or / 587 static int 588 islocalname(Strlit *name) 589 { 590 if(name->len >= 1 && name->s[0] == '/') 591 return 1; 592 if(ctxt->windows && name->len >= 3 && 593 yy_isalpha(name->s[0]) && name->s[1] == ':' && name->s[2] == '/') 594 return 1; 595 if(name->len >= 2 && strncmp(name->s, "./", 2) == 0) 596 return 1; 597 if(name->len == 1 && strncmp(name->s, ".", 1) == 0) 598 return 1; 599 if(name->len >= 3 && strncmp(name->s, "../", 3) == 0) 600 return 1; 601 if(name->len == 2 && strncmp(name->s, "..", 2) == 0) 602 return 1; 603 return 0; 604 } 605 606 static int 607 findpkg(Strlit *name) 608 { 609 Idir *p; 610 char *q, *suffix, *suffixsep; 611 612 if(islocalname(name)) { 613 if(safemode || nolocalimports) 614 return 0; 615 // try .a before .6. important for building libraries: 616 // if there is an array.6 in the array.a library, 617 // want to find all of array.a, not just array.6. 618 snprint(namebuf, sizeof(namebuf), "%Z.a", name); 619 if(access(namebuf, 0) >= 0) 620 return 1; 621 snprint(namebuf, sizeof(namebuf), "%Z.%c", name, thechar); 622 if(access(namebuf, 0) >= 0) 623 return 1; 624 return 0; 625 } 626 627 // local imports should be canonicalized already. 628 // don't want to see "encoding/../encoding/base64" 629 // as different from "encoding/base64". 630 q = mal(name->len+1); 631 memmove(q, name->s, name->len); 632 q[name->len] = '\0'; 633 cleanname(q); 634 if(strlen(q) != name->len || memcmp(q, name->s, name->len) != 0) { 635 yyerror("non-canonical import path %Z (should be %s)", name, q); 636 return 0; 637 } 638 639 for(p = idirs; p != nil; p = p->link) { 640 snprint(namebuf, sizeof(namebuf), "%s/%Z.a", p->dir, name); 641 if(access(namebuf, 0) >= 0) 642 return 1; 643 snprint(namebuf, sizeof(namebuf), "%s/%Z.%c", p->dir, name, thechar); 644 if(access(namebuf, 0) >= 0) 645 return 1; 646 } 647 if(goroot != nil) { 648 suffix = ""; 649 suffixsep = ""; 650 if(flag_installsuffix != nil) { 651 suffixsep = "_"; 652 suffix = flag_installsuffix; 653 } else if(flag_race) { 654 suffixsep = "_"; 655 suffix = "race"; 656 } 657 snprint(namebuf, sizeof(namebuf), "%s/pkg/%s_%s%s%s/%Z.a", goroot, goos, goarch, suffixsep, suffix, name); 658 if(access(namebuf, 0) >= 0) 659 return 1; 660 snprint(namebuf, sizeof(namebuf), "%s/pkg/%s_%s%s%s/%Z.%c", goroot, goos, goarch, suffixsep, suffix, name, thechar); 661 if(access(namebuf, 0) >= 0) 662 return 1; 663 } 664 return 0; 665 } 666 667 static void 668 fakeimport(void) 669 { 670 importpkg = mkpkg(strlit("fake")); 671 cannedimports("fake.6", "$$\n"); 672 } 673 674 void 675 importfile(Val *f, int line) 676 { 677 Biobuf *imp; 678 char *file, *p, *q, *tag; 679 int32 c; 680 int len; 681 Strlit *path; 682 char *cleanbuf, *prefix; 683 684 USED(line); 685 686 if(f->ctype != CTSTR) { 687 yyerror("import statement not a string"); 688 fakeimport(); 689 return; 690 } 691 692 if(f->u.sval->len == 0) { 693 yyerror("import path is empty"); 694 fakeimport(); 695 return; 696 } 697 698 if(isbadimport(f->u.sval)) { 699 fakeimport(); 700 return; 701 } 702 703 // The package name main is no longer reserved, 704 // but we reserve the import path "main" to identify 705 // the main package, just as we reserve the import 706 // path "math" to identify the standard math package. 707 if(strcmp(f->u.sval->s, "main") == 0) { 708 yyerror("cannot import \"main\""); 709 errorexit(); 710 } 711 712 if(myimportpath != nil && strcmp(f->u.sval->s, myimportpath) == 0) { 713 yyerror("import \"%Z\" while compiling that package (import cycle)", f->u.sval); 714 errorexit(); 715 } 716 717 if(strcmp(f->u.sval->s, "unsafe") == 0) { 718 if(safemode) { 719 yyerror("cannot import package unsafe"); 720 errorexit(); 721 } 722 importpkg = mkpkg(f->u.sval); 723 cannedimports("unsafe.6", unsafeimport); 724 return; 725 } 726 727 path = f->u.sval; 728 if(islocalname(path)) { 729 if(path->s[0] == '/') { 730 yyerror("import path cannot be absolute path"); 731 fakeimport(); 732 return; 733 } 734 prefix = ctxt->pathname; 735 if(localimport != nil) 736 prefix = localimport; 737 cleanbuf = mal(strlen(prefix) + strlen(path->s) + 2); 738 strcpy(cleanbuf, prefix); 739 strcat(cleanbuf, "/"); 740 strcat(cleanbuf, path->s); 741 cleanname(cleanbuf); 742 path = strlit(cleanbuf); 743 744 if(isbadimport(path)) { 745 fakeimport(); 746 return; 747 } 748 } 749 750 if(!findpkg(path)) { 751 yyerror("can't find import: \"%Z\"", f->u.sval); 752 errorexit(); 753 } 754 importpkg = mkpkg(path); 755 756 // If we already saw that package, feed a dummy statement 757 // to the lexer to avoid parsing export data twice. 758 if(importpkg->imported) { 759 file = strdup(namebuf); 760 tag = ""; 761 if(importpkg->safe) { 762 tag = "safe"; 763 } 764 p = smprint("package %s %s\n$$\n", importpkg->name, tag); 765 cannedimports(file, p); 766 return; 767 } 768 importpkg->imported = 1; 769 770 imp = Bopen(namebuf, OREAD); 771 if(imp == nil) { 772 yyerror("can't open import: \"%Z\": %r", f->u.sval); 773 errorexit(); 774 } 775 file = strdup(namebuf); 776 777 len = strlen(namebuf); 778 if(len > 2 && namebuf[len-2] == '.' && namebuf[len-1] == 'a') { 779 if(!skiptopkgdef(imp)) { 780 yyerror("import %s: not a package file", file); 781 errorexit(); 782 } 783 } 784 785 // check object header 786 p = Brdstr(imp, '\n', 1); 787 if(strcmp(p, "empty archive") != 0) { 788 if(strncmp(p, "go object ", 10) != 0) { 789 yyerror("import %s: not a go object file", file); 790 errorexit(); 791 } 792 q = smprint("%s %s %s %s", getgoos(), getgoarch(), getgoversion(), expstring()); 793 if(strcmp(p+10, q) != 0) { 794 yyerror("import %s: object is [%s] expected [%s]", file, p+10, q); 795 errorexit(); 796 } 797 free(q); 798 } 799 800 // assume files move (get installed) 801 // so don't record the full path. 802 linehist(file + len - path->len - 2, -1, 1); // acts as #pragma lib 803 804 /* 805 * position the input right 806 * after $$ and return 807 */ 808 pushedio = curio; 809 curio.bin = imp; 810 curio.peekc = 0; 811 curio.peekc1 = 0; 812 curio.infile = file; 813 curio.nlsemi = 0; 814 typecheckok = 1; 815 816 for(;;) { 817 c = getc(); 818 if(c == EOF) 819 break; 820 if(c != '$') 821 continue; 822 c = getc(); 823 if(c == EOF) 824 break; 825 if(c != '$') 826 continue; 827 return; 828 } 829 yyerror("no import in \"%Z\"", f->u.sval); 830 unimportfile(); 831 } 832 833 void 834 unimportfile(void) 835 { 836 if(curio.bin != nil) { 837 Bterm(curio.bin); 838 curio.bin = nil; 839 } else 840 lexlineno--; // re correct sys.6 line number 841 842 curio = pushedio; 843 pushedio.bin = nil; 844 incannedimport = 0; 845 typecheckok = 0; 846 } 847 848 void 849 cannedimports(char *file, char *cp) 850 { 851 lexlineno++; // if sys.6 is included on line 1, 852 853 pushedio = curio; 854 curio.bin = nil; 855 curio.peekc = 0; 856 curio.peekc1 = 0; 857 curio.infile = file; 858 curio.cp = cp; 859 curio.nlsemi = 0; 860 curio.importsafe = 0; 861 862 typecheckok = 1; 863 incannedimport = 1; 864 } 865 866 static int 867 isfrog(int c) 868 { 869 // complain about possibly invisible control characters 870 if(c < ' ') { 871 return !yy_isspace(c); // exclude good white space 872 } 873 if(0x7f <= c && c <= 0xa0) // DEL, unicode block including unbreakable space. 874 return 1; 875 return 0; 876 } 877 878 typedef struct Loophack Loophack; 879 struct Loophack { 880 int v; 881 Loophack *next; 882 }; 883 884 static int32 885 _yylex(void) 886 { 887 int c, c1, clen, escflag, ncp; 888 vlong v; 889 char *cp, *ep; 890 Rune rune; 891 Sym *s; 892 static Loophack *lstk; 893 Loophack *h; 894 895 prevlineno = lineno; 896 897 l0: 898 c = getc(); 899 if(yy_isspace(c)) { 900 if(c == '\n' && curio.nlsemi) { 901 ungetc(c); 902 DBG("lex: implicit semi\n"); 903 return ';'; 904 } 905 goto l0; 906 } 907 908 lineno = lexlineno; /* start of token */ 909 910 if(c >= Runeself) { 911 /* all multibyte runes are alpha */ 912 cp = lexbuf; 913 ep = lexbuf+sizeof lexbuf; 914 goto talph; 915 } 916 917 if(yy_isalpha(c)) { 918 cp = lexbuf; 919 ep = lexbuf+sizeof lexbuf; 920 goto talph; 921 } 922 923 if(yy_isdigit(c)) 924 goto tnum; 925 926 switch(c) { 927 case EOF: 928 lineno = prevlineno; 929 ungetc(EOF); 930 return -1; 931 932 case '_': 933 cp = lexbuf; 934 ep = lexbuf+sizeof lexbuf; 935 goto talph; 936 937 case '.': 938 c1 = getc(); 939 if(yy_isdigit(c1)) { 940 cp = lexbuf; 941 ep = lexbuf+sizeof lexbuf; 942 *cp++ = c; 943 c = c1; 944 goto casedot; 945 } 946 if(c1 == '.') { 947 c1 = getc(); 948 if(c1 == '.') { 949 c = LDDD; 950 goto lx; 951 } 952 ungetc(c1); 953 c1 = '.'; 954 } 955 break; 956 957 case '"': 958 /* "..." */ 959 strcpy(lexbuf, "\"<string>\""); 960 cp = mal(8); 961 clen = sizeof(int32); 962 ncp = 8; 963 964 for(;;) { 965 if(clen+UTFmax > ncp) { 966 cp = remal(cp, ncp, ncp); 967 ncp += ncp; 968 } 969 if(escchar('"', &escflag, &v)) 970 break; 971 if(v < Runeself || escflag) { 972 cp[clen++] = v; 973 } else { 974 rune = v; 975 c = runelen(rune); 976 runetochar(cp+clen, &rune); 977 clen += c; 978 } 979 } 980 goto strlit; 981 982 case '`': 983 /* `...` */ 984 strcpy(lexbuf, "`<string>`"); 985 cp = mal(8); 986 clen = sizeof(int32); 987 ncp = 8; 988 989 for(;;) { 990 if(clen+UTFmax > ncp) { 991 cp = remal(cp, ncp, ncp); 992 ncp += ncp; 993 } 994 c = getr(); 995 if(c == '\r') 996 continue; 997 if(c == EOF) { 998 yyerror("eof in string"); 999 break; 1000 } 1001 if(c == '`') 1002 break; 1003 rune = c; 1004 clen += runetochar(cp+clen, &rune); 1005 } 1006 1007 strlit: 1008 *(int32*)cp = clen-sizeof(int32); // length 1009 do { 1010 cp[clen++] = 0; 1011 } while(clen & MAXALIGN); 1012 yylval.val.u.sval = (Strlit*)cp; 1013 yylval.val.ctype = CTSTR; 1014 DBG("lex: string literal\n"); 1015 strcpy(litbuf, "string literal"); 1016 return LLITERAL; 1017 1018 case '\'': 1019 /* '.' */ 1020 if(escchar('\'', &escflag, &v)) { 1021 yyerror("empty character literal or unescaped ' in character literal"); 1022 v = '\''; 1023 } 1024 if(!escchar('\'', &escflag, &v)) { 1025 yyerror("missing '"); 1026 ungetc(v); 1027 } 1028 yylval.val.u.xval = mal(sizeof(*yylval.val.u.xval)); 1029 mpmovecfix(yylval.val.u.xval, v); 1030 yylval.val.ctype = CTRUNE; 1031 DBG("lex: codepoint literal\n"); 1032 strcpy(litbuf, "string literal"); 1033 return LLITERAL; 1034 1035 case '/': 1036 c1 = getc(); 1037 if(c1 == '*') { 1038 int nl; 1039 1040 nl = 0; 1041 for(;;) { 1042 c = getr(); 1043 if(c == '\n') 1044 nl = 1; 1045 while(c == '*') { 1046 c = getr(); 1047 if(c == '/') { 1048 if(nl) 1049 ungetc('\n'); 1050 goto l0; 1051 } 1052 if(c == '\n') 1053 nl = 1; 1054 } 1055 if(c == EOF) { 1056 yyerror("eof in comment"); 1057 errorexit(); 1058 } 1059 } 1060 } 1061 if(c1 == '/') { 1062 c = getlinepragma(); 1063 for(;;) { 1064 if(c == '\n' || c == EOF) { 1065 ungetc(c); 1066 goto l0; 1067 } 1068 c = getr(); 1069 } 1070 } 1071 if(c1 == '=') { 1072 c = ODIV; 1073 goto asop; 1074 } 1075 break; 1076 1077 case ':': 1078 c1 = getc(); 1079 if(c1 == '=') { 1080 c = LCOLAS; 1081 yylval.i = lexlineno; 1082 goto lx; 1083 } 1084 break; 1085 1086 case '*': 1087 c1 = getc(); 1088 if(c1 == '=') { 1089 c = OMUL; 1090 goto asop; 1091 } 1092 break; 1093 1094 case '%': 1095 c1 = getc(); 1096 if(c1 == '=') { 1097 c = OMOD; 1098 goto asop; 1099 } 1100 break; 1101 1102 case '+': 1103 c1 = getc(); 1104 if(c1 == '+') { 1105 c = LINC; 1106 goto lx; 1107 } 1108 if(c1 == '=') { 1109 c = OADD; 1110 goto asop; 1111 } 1112 break; 1113 1114 case '-': 1115 c1 = getc(); 1116 if(c1 == '-') { 1117 c = LDEC; 1118 goto lx; 1119 } 1120 if(c1 == '=') { 1121 c = OSUB; 1122 goto asop; 1123 } 1124 break; 1125 1126 case '>': 1127 c1 = getc(); 1128 if(c1 == '>') { 1129 c = LRSH; 1130 c1 = getc(); 1131 if(c1 == '=') { 1132 c = ORSH; 1133 goto asop; 1134 } 1135 break; 1136 } 1137 if(c1 == '=') { 1138 c = LGE; 1139 goto lx; 1140 } 1141 c = LGT; 1142 break; 1143 1144 case '<': 1145 c1 = getc(); 1146 if(c1 == '<') { 1147 c = LLSH; 1148 c1 = getc(); 1149 if(c1 == '=') { 1150 c = OLSH; 1151 goto asop; 1152 } 1153 break; 1154 } 1155 if(c1 == '=') { 1156 c = LLE; 1157 goto lx; 1158 } 1159 if(c1 == '-') { 1160 c = LCOMM; 1161 goto lx; 1162 } 1163 c = LLT; 1164 break; 1165 1166 case '=': 1167 c1 = getc(); 1168 if(c1 == '=') { 1169 c = LEQ; 1170 goto lx; 1171 } 1172 break; 1173 1174 case '!': 1175 c1 = getc(); 1176 if(c1 == '=') { 1177 c = LNE; 1178 goto lx; 1179 } 1180 break; 1181 1182 case '&': 1183 c1 = getc(); 1184 if(c1 == '&') { 1185 c = LANDAND; 1186 goto lx; 1187 } 1188 if(c1 == '^') { 1189 c = LANDNOT; 1190 c1 = getc(); 1191 if(c1 == '=') { 1192 c = OANDNOT; 1193 goto asop; 1194 } 1195 break; 1196 } 1197 if(c1 == '=') { 1198 c = OAND; 1199 goto asop; 1200 } 1201 break; 1202 1203 case '|': 1204 c1 = getc(); 1205 if(c1 == '|') { 1206 c = LOROR; 1207 goto lx; 1208 } 1209 if(c1 == '=') { 1210 c = OOR; 1211 goto asop; 1212 } 1213 break; 1214 1215 case '^': 1216 c1 = getc(); 1217 if(c1 == '=') { 1218 c = OXOR; 1219 goto asop; 1220 } 1221 break; 1222 1223 /* 1224 * clumsy dance: 1225 * to implement rule that disallows 1226 * if T{1}[0] { ... } 1227 * but allows 1228 * if (T{1}[0]) { ... } 1229 * the block bodies for if/for/switch/select 1230 * begin with an LBODY token, not '{'. 1231 * 1232 * when we see the keyword, the next 1233 * non-parenthesized '{' becomes an LBODY. 1234 * loophack is normally 0. 1235 * a keyword makes it go up to 1. 1236 * parens push loophack onto a stack and go back to 0. 1237 * a '{' with loophack == 1 becomes LBODY and disables loophack. 1238 * 1239 * i said it was clumsy. 1240 */ 1241 case '(': 1242 case '[': 1243 if(loophack || lstk != nil) { 1244 h = malloc(sizeof *h); 1245 if(h == nil) { 1246 flusherrors(); 1247 yyerror("out of memory"); 1248 errorexit(); 1249 } 1250 h->v = loophack; 1251 h->next = lstk; 1252 lstk = h; 1253 loophack = 0; 1254 } 1255 goto lx; 1256 case ')': 1257 case ']': 1258 if(lstk != nil) { 1259 h = lstk; 1260 loophack = h->v; 1261 lstk = h->next; 1262 free(h); 1263 } 1264 goto lx; 1265 case '{': 1266 if(loophack == 1) { 1267 DBG("%L lex: LBODY\n", lexlineno); 1268 loophack = 0; 1269 return LBODY; 1270 } 1271 goto lx; 1272 1273 default: 1274 goto lx; 1275 } 1276 ungetc(c1); 1277 1278 lx: 1279 if(c > 0xff) 1280 DBG("%L lex: TOKEN %s\n", lexlineno, lexname(c)); 1281 else 1282 DBG("%L lex: TOKEN '%c'\n", lexlineno, c); 1283 if(isfrog(c)) { 1284 yyerror("illegal character 0x%ux", c); 1285 goto l0; 1286 } 1287 if(importpkg == nil && (c == '#' || c == '$' || c == '?' || c == '@' || c == '\\')) { 1288 yyerror("%s: unexpected %c", "syntax error", c); 1289 goto l0; 1290 } 1291 return c; 1292 1293 asop: 1294 yylval.i = c; // rathole to hold which asop 1295 DBG("lex: TOKEN ASOP %c\n", c); 1296 return LASOP; 1297 1298 talph: 1299 /* 1300 * cp is set to lexbuf and some 1301 * prefix has been stored 1302 */ 1303 for(;;) { 1304 if(cp+10 >= ep) { 1305 yyerror("identifier too long"); 1306 errorexit(); 1307 } 1308 if(c >= Runeself) { 1309 ungetc(c); 1310 rune = getr(); 1311 // 0xb7 ยท is used for internal names 1312 if(!isalpharune(rune) && !isdigitrune(rune) && (importpkg == nil || rune != 0xb7)) 1313 yyerror("invalid identifier character U+%04x", rune); 1314 cp += runetochar(cp, &rune); 1315 } else if(!yy_isalnum(c) && c != '_') 1316 break; 1317 else 1318 *cp++ = c; 1319 c = getc(); 1320 } 1321 *cp = 0; 1322 ungetc(c); 1323 1324 s = lookup(lexbuf); 1325 switch(s->lexical) { 1326 case LIGNORE: 1327 goto l0; 1328 1329 case LFOR: 1330 case LIF: 1331 case LSWITCH: 1332 case LSELECT: 1333 loophack = 1; // see comment about loophack above 1334 break; 1335 } 1336 1337 DBG("lex: %S %s\n", s, lexname(s->lexical)); 1338 yylval.sym = s; 1339 return s->lexical; 1340 1341 tnum: 1342 cp = lexbuf; 1343 ep = lexbuf+sizeof lexbuf; 1344 if(c != '0') { 1345 for(;;) { 1346 if(cp+10 >= ep) { 1347 yyerror("identifier too long"); 1348 errorexit(); 1349 } 1350 *cp++ = c; 1351 c = getc(); 1352 if(yy_isdigit(c)) 1353 continue; 1354 goto dc; 1355 } 1356 } 1357 *cp++ = c; 1358 c = getc(); 1359 if(c == 'x' || c == 'X') { 1360 for(;;) { 1361 if(cp+10 >= ep) { 1362 yyerror("identifier too long"); 1363 errorexit(); 1364 } 1365 *cp++ = c; 1366 c = getc(); 1367 if(yy_isdigit(c)) 1368 continue; 1369 if(c >= 'a' && c <= 'f') 1370 continue; 1371 if(c >= 'A' && c <= 'F') 1372 continue; 1373 if(cp == lexbuf+2) 1374 yyerror("malformed hex constant"); 1375 if(c == 'p') 1376 goto caseep; 1377 goto ncu; 1378 } 1379 } 1380 1381 if(c == 'p') // 0p begins floating point zero 1382 goto caseep; 1383 1384 c1 = 0; 1385 for(;;) { 1386 if(cp+10 >= ep) { 1387 yyerror("identifier too long"); 1388 errorexit(); 1389 } 1390 if(!yy_isdigit(c)) 1391 break; 1392 if(c < '0' || c > '7') 1393 c1 = 1; // not octal 1394 *cp++ = c; 1395 c = getc(); 1396 } 1397 if(c == '.') 1398 goto casedot; 1399 if(c == 'e' || c == 'E') 1400 goto caseep; 1401 if(c == 'i') 1402 goto casei; 1403 if(c1) 1404 yyerror("malformed octal constant"); 1405 goto ncu; 1406 1407 dc: 1408 if(c == '.') 1409 goto casedot; 1410 if(c == 'e' || c == 'E' || c == 'p' || c == 'P') 1411 goto caseep; 1412 if(c == 'i') 1413 goto casei; 1414 1415 ncu: 1416 *cp = 0; 1417 ungetc(c); 1418 1419 yylval.val.u.xval = mal(sizeof(*yylval.val.u.xval)); 1420 mpatofix(yylval.val.u.xval, lexbuf); 1421 if(yylval.val.u.xval->ovf) { 1422 yyerror("overflow in constant"); 1423 mpmovecfix(yylval.val.u.xval, 0); 1424 } 1425 yylval.val.ctype = CTINT; 1426 DBG("lex: integer literal\n"); 1427 strcpy(litbuf, "literal "); 1428 strcat(litbuf, lexbuf); 1429 return LLITERAL; 1430 1431 casedot: 1432 for(;;) { 1433 if(cp+10 >= ep) { 1434 yyerror("identifier too long"); 1435 errorexit(); 1436 } 1437 *cp++ = c; 1438 c = getc(); 1439 if(!yy_isdigit(c)) 1440 break; 1441 } 1442 if(c == 'i') 1443 goto casei; 1444 if(c != 'e' && c != 'E') 1445 goto caseout; 1446 1447 caseep: 1448 *cp++ = c; 1449 c = getc(); 1450 if(c == '+' || c == '-') { 1451 *cp++ = c; 1452 c = getc(); 1453 } 1454 if(!yy_isdigit(c)) 1455 yyerror("malformed fp constant exponent"); 1456 while(yy_isdigit(c)) { 1457 if(cp+10 >= ep) { 1458 yyerror("identifier too long"); 1459 errorexit(); 1460 } 1461 *cp++ = c; 1462 c = getc(); 1463 } 1464 if(c == 'i') 1465 goto casei; 1466 goto caseout; 1467 1468 casei: 1469 // imaginary constant 1470 *cp = 0; 1471 yylval.val.u.cval = mal(sizeof(*yylval.val.u.cval)); 1472 mpmovecflt(&yylval.val.u.cval->real, 0.0); 1473 mpatoflt(&yylval.val.u.cval->imag, lexbuf); 1474 if(yylval.val.u.cval->imag.val.ovf) { 1475 yyerror("overflow in imaginary constant"); 1476 mpmovecflt(&yylval.val.u.cval->real, 0.0); 1477 } 1478 yylval.val.ctype = CTCPLX; 1479 DBG("lex: imaginary literal\n"); 1480 strcpy(litbuf, "literal "); 1481 strcat(litbuf, lexbuf); 1482 return LLITERAL; 1483 1484 caseout: 1485 *cp = 0; 1486 ungetc(c); 1487 1488 yylval.val.u.fval = mal(sizeof(*yylval.val.u.fval)); 1489 mpatoflt(yylval.val.u.fval, lexbuf); 1490 if(yylval.val.u.fval->val.ovf) { 1491 yyerror("overflow in float constant"); 1492 mpmovecflt(yylval.val.u.fval, 0.0); 1493 } 1494 yylval.val.ctype = CTFLT; 1495 DBG("lex: floating literal\n"); 1496 strcpy(litbuf, "literal "); 1497 strcat(litbuf, lexbuf); 1498 return LLITERAL; 1499 } 1500 1501 /* 1502 * read and interpret syntax that looks like 1503 * //line parse.y:15 1504 * as a discontinuity in sequential line numbers. 1505 * the next line of input comes from parse.y:15 1506 */ 1507 static int 1508 getlinepragma(void) 1509 { 1510 int i, c, n; 1511 char *cp, *ep, *linep; 1512 Hist *h; 1513 1514 c = getr(); 1515 if(c == 'g') 1516 goto go; 1517 if(c != 'l') 1518 goto out; 1519 for(i=1; i<5; i++) { 1520 c = getr(); 1521 if(c != "line "[i]) 1522 goto out; 1523 } 1524 1525 cp = lexbuf; 1526 ep = lexbuf+sizeof(lexbuf)-5; 1527 linep = nil; 1528 for(;;) { 1529 c = getr(); 1530 if(c == EOF) 1531 goto out; 1532 if(c == '\n') 1533 break; 1534 if(c == ' ') 1535 continue; 1536 if(c == ':') 1537 linep = cp; 1538 if(cp < ep) 1539 *cp++ = c; 1540 } 1541 *cp = 0; 1542 1543 if(linep == nil || linep >= ep) 1544 goto out; 1545 *linep++ = '\0'; 1546 n = 0; 1547 for(cp=linep; *cp; cp++) { 1548 if(*cp < '0' || *cp > '9') 1549 goto out; 1550 n = n*10 + *cp - '0'; 1551 if(n > 1e8) { 1552 yyerror("line number out of range"); 1553 errorexit(); 1554 } 1555 } 1556 if(n <= 0) 1557 goto out; 1558 1559 // try to avoid allocating file name over and over 1560 for(h=ctxt->hist; h!=nil; h=h->link) { 1561 if(h->name != nil && strcmp(h->name, lexbuf) == 0) { 1562 linehist(h->name, n, 0); 1563 goto out; 1564 } 1565 } 1566 linehist(strdup(lexbuf), n, 0); 1567 goto out; 1568 1569 go: 1570 cp = lexbuf; 1571 ep = lexbuf+sizeof(lexbuf)-5; 1572 *cp++ = 'g'; // already read 1573 for(;;) { 1574 c = getr(); 1575 if(c == EOF || c >= Runeself) 1576 goto out; 1577 if(c == '\n') 1578 break; 1579 if(cp < ep) 1580 *cp++ = c; 1581 } 1582 *cp = 0; 1583 ep = strchr(lexbuf, ' '); 1584 if(ep != nil) 1585 *ep = 0; 1586 1587 if(strcmp(lexbuf, "go:nointerface") == 0 && fieldtrack_enabled) { 1588 nointerface = 1; 1589 goto out; 1590 } 1591 if(strcmp(lexbuf, "go:noescape") == 0) { 1592 noescape = 1; 1593 goto out; 1594 } 1595 1596 out: 1597 return c; 1598 } 1599 1600 int32 1601 yylex(void) 1602 { 1603 int lx; 1604 1605 lx = _yylex(); 1606 1607 if(curio.nlsemi && lx == EOF) { 1608 // Treat EOF as "end of line" for the purposes 1609 // of inserting a semicolon. 1610 lx = ';'; 1611 } 1612 1613 switch(lx) { 1614 case LNAME: 1615 case LLITERAL: 1616 case LBREAK: 1617 case LCONTINUE: 1618 case LFALL: 1619 case LRETURN: 1620 case LINC: 1621 case LDEC: 1622 case ')': 1623 case '}': 1624 case ']': 1625 curio.nlsemi = 1; 1626 break; 1627 default: 1628 curio.nlsemi = 0; 1629 break; 1630 } 1631 1632 // Track last two tokens returned by yylex. 1633 yyprev = yylast; 1634 yylast = lx; 1635 return lx; 1636 } 1637 1638 static int 1639 getc(void) 1640 { 1641 int c, c1, c2; 1642 1643 c = curio.peekc; 1644 if(c != 0) { 1645 curio.peekc = curio.peekc1; 1646 curio.peekc1 = 0; 1647 goto check; 1648 } 1649 1650 if(curio.bin == nil) { 1651 c = *curio.cp & 0xff; 1652 if(c != 0) 1653 curio.cp++; 1654 } else { 1655 loop: 1656 c = BGETC(curio.bin); 1657 if(c == 0xef) { 1658 c1 = BGETC(curio.bin); 1659 c2 = BGETC(curio.bin); 1660 if(c1 == 0xbb && c2 == 0xbf) { 1661 yyerrorl(lexlineno, "Unicode (UTF-8) BOM in middle of file"); 1662 goto loop; 1663 } 1664 Bungetc(curio.bin); 1665 Bungetc(curio.bin); 1666 } 1667 } 1668 1669 check: 1670 switch(c) { 1671 case 0: 1672 if(curio.bin != nil) { 1673 yyerror("illegal NUL byte"); 1674 break; 1675 } 1676 case EOF: 1677 // insert \n at EOF 1678 if(curio.eofnl || curio.last == '\n') 1679 return EOF; 1680 curio.eofnl = 1; 1681 c = '\n'; 1682 case '\n': 1683 if(pushedio.bin == nil) 1684 lexlineno++; 1685 break; 1686 } 1687 curio.last = c; 1688 return c; 1689 } 1690 1691 static void 1692 ungetc(int c) 1693 { 1694 curio.peekc1 = curio.peekc; 1695 curio.peekc = c; 1696 if(c == '\n' && pushedio.bin == nil) 1697 lexlineno--; 1698 } 1699 1700 static int32 1701 getr(void) 1702 { 1703 int c, i; 1704 char str[UTFmax+1]; 1705 Rune rune; 1706 1707 c = getc(); 1708 if(c < Runeself) 1709 return c; 1710 i = 0; 1711 str[i++] = c; 1712 1713 loop: 1714 c = getc(); 1715 str[i++] = c; 1716 if(!fullrune(str, i)) 1717 goto loop; 1718 c = chartorune(&rune, str); 1719 if(rune == Runeerror && c == 1) { 1720 lineno = lexlineno; 1721 yyerror("illegal UTF-8 sequence"); 1722 flusherrors(); 1723 print("\t"); 1724 for(c=0; c<i; c++) 1725 print("%s%.2x", c > 0 ? " " : "", *(uchar*)(str+c)); 1726 print("\n"); 1727 } 1728 return rune; 1729 } 1730 1731 static int 1732 escchar(int e, int *escflg, vlong *val) 1733 { 1734 int i, u, c; 1735 vlong l; 1736 1737 *escflg = 0; 1738 1739 c = getr(); 1740 switch(c) { 1741 case EOF: 1742 yyerror("eof in string"); 1743 return 1; 1744 case '\n': 1745 yyerror("newline in string"); 1746 return 1; 1747 case '\\': 1748 break; 1749 default: 1750 if(c == e) 1751 return 1; 1752 *val = c; 1753 return 0; 1754 } 1755 1756 u = 0; 1757 c = getr(); 1758 switch(c) { 1759 case 'x': 1760 *escflg = 1; // it's a byte 1761 i = 2; 1762 goto hex; 1763 1764 case 'u': 1765 i = 4; 1766 u = 1; 1767 goto hex; 1768 1769 case 'U': 1770 i = 8; 1771 u = 1; 1772 goto hex; 1773 1774 case '0': 1775 case '1': 1776 case '2': 1777 case '3': 1778 case '4': 1779 case '5': 1780 case '6': 1781 case '7': 1782 *escflg = 1; // it's a byte 1783 goto oct; 1784 1785 case 'a': c = '\a'; break; 1786 case 'b': c = '\b'; break; 1787 case 'f': c = '\f'; break; 1788 case 'n': c = '\n'; break; 1789 case 'r': c = '\r'; break; 1790 case 't': c = '\t'; break; 1791 case 'v': c = '\v'; break; 1792 case '\\': c = '\\'; break; 1793 1794 default: 1795 if(c != e) 1796 yyerror("unknown escape sequence: %c", c); 1797 } 1798 *val = c; 1799 return 0; 1800 1801 hex: 1802 l = 0; 1803 for(; i>0; i--) { 1804 c = getc(); 1805 if(c >= '0' && c <= '9') { 1806 l = l*16 + c-'0'; 1807 continue; 1808 } 1809 if(c >= 'a' && c <= 'f') { 1810 l = l*16 + c-'a' + 10; 1811 continue; 1812 } 1813 if(c >= 'A' && c <= 'F') { 1814 l = l*16 + c-'A' + 10; 1815 continue; 1816 } 1817 yyerror("non-hex character in escape sequence: %c", c); 1818 ungetc(c); 1819 break; 1820 } 1821 if(u && (l > Runemax || (0xd800 <= l && l < 0xe000))) { 1822 yyerror("invalid Unicode code point in escape sequence: %#llx", l); 1823 l = Runeerror; 1824 } 1825 *val = l; 1826 return 0; 1827 1828 oct: 1829 l = c - '0'; 1830 for(i=2; i>0; i--) { 1831 c = getc(); 1832 if(c >= '0' && c <= '7') { 1833 l = l*8 + c-'0'; 1834 continue; 1835 } 1836 yyerror("non-octal character in escape sequence: %c", c); 1837 ungetc(c); 1838 } 1839 if(l > 255) 1840 yyerror("octal escape value > 255: %d", l); 1841 1842 *val = l; 1843 return 0; 1844 } 1845 1846 static struct 1847 { 1848 char* name; 1849 int lexical; 1850 int etype; 1851 int op; 1852 } syms[] = 1853 { 1854 /* name lexical etype op 1855 */ 1856 /* basic types */ 1857 "int8", LNAME, TINT8, OXXX, 1858 "int16", LNAME, TINT16, OXXX, 1859 "int32", LNAME, TINT32, OXXX, 1860 "int64", LNAME, TINT64, OXXX, 1861 1862 "uint8", LNAME, TUINT8, OXXX, 1863 "uint16", LNAME, TUINT16, OXXX, 1864 "uint32", LNAME, TUINT32, OXXX, 1865 "uint64", LNAME, TUINT64, OXXX, 1866 1867 "float32", LNAME, TFLOAT32, OXXX, 1868 "float64", LNAME, TFLOAT64, OXXX, 1869 1870 "complex64", LNAME, TCOMPLEX64, OXXX, 1871 "complex128", LNAME, TCOMPLEX128, OXXX, 1872 1873 "bool", LNAME, TBOOL, OXXX, 1874 "string", LNAME, TSTRING, OXXX, 1875 1876 "any", LNAME, TANY, OXXX, 1877 1878 "break", LBREAK, Txxx, OXXX, 1879 "case", LCASE, Txxx, OXXX, 1880 "chan", LCHAN, Txxx, OXXX, 1881 "const", LCONST, Txxx, OXXX, 1882 "continue", LCONTINUE, Txxx, OXXX, 1883 "default", LDEFAULT, Txxx, OXXX, 1884 "else", LELSE, Txxx, OXXX, 1885 "defer", LDEFER, Txxx, OXXX, 1886 "fallthrough", LFALL, Txxx, OXXX, 1887 "for", LFOR, Txxx, OXXX, 1888 "func", LFUNC, Txxx, OXXX, 1889 "go", LGO, Txxx, OXXX, 1890 "goto", LGOTO, Txxx, OXXX, 1891 "if", LIF, Txxx, OXXX, 1892 "import", LIMPORT, Txxx, OXXX, 1893 "interface", LINTERFACE, Txxx, OXXX, 1894 "map", LMAP, Txxx, OXXX, 1895 "package", LPACKAGE, Txxx, OXXX, 1896 "range", LRANGE, Txxx, OXXX, 1897 "return", LRETURN, Txxx, OXXX, 1898 "select", LSELECT, Txxx, OXXX, 1899 "struct", LSTRUCT, Txxx, OXXX, 1900 "switch", LSWITCH, Txxx, OXXX, 1901 "type", LTYPE, Txxx, OXXX, 1902 "var", LVAR, Txxx, OXXX, 1903 1904 "append", LNAME, Txxx, OAPPEND, 1905 "cap", LNAME, Txxx, OCAP, 1906 "close", LNAME, Txxx, OCLOSE, 1907 "complex", LNAME, Txxx, OCOMPLEX, 1908 "copy", LNAME, Txxx, OCOPY, 1909 "delete", LNAME, Txxx, ODELETE, 1910 "imag", LNAME, Txxx, OIMAG, 1911 "len", LNAME, Txxx, OLEN, 1912 "make", LNAME, Txxx, OMAKE, 1913 "new", LNAME, Txxx, ONEW, 1914 "panic", LNAME, Txxx, OPANIC, 1915 "print", LNAME, Txxx, OPRINT, 1916 "println", LNAME, Txxx, OPRINTN, 1917 "real", LNAME, Txxx, OREAL, 1918 "recover", LNAME, Txxx, ORECOVER, 1919 1920 "notwithstanding", LIGNORE, Txxx, OXXX, 1921 "thetruthofthematter", LIGNORE, Txxx, OXXX, 1922 "despiteallobjections", LIGNORE, Txxx, OXXX, 1923 "whereas", LIGNORE, Txxx, OXXX, 1924 "insofaras", LIGNORE, Txxx, OXXX, 1925 }; 1926 1927 static void 1928 lexinit(void) 1929 { 1930 int i, lex; 1931 Sym *s, *s1; 1932 Type *t; 1933 int etype; 1934 Val v; 1935 1936 /* 1937 * initialize basic types array 1938 * initialize known symbols 1939 */ 1940 for(i=0; i<nelem(syms); i++) { 1941 lex = syms[i].lexical; 1942 s = lookup(syms[i].name); 1943 s->lexical = lex; 1944 1945 etype = syms[i].etype; 1946 if(etype != Txxx) { 1947 if(etype < 0 || etype >= nelem(types)) 1948 fatal("lexinit: %s bad etype", s->name); 1949 s1 = pkglookup(syms[i].name, builtinpkg); 1950 t = types[etype]; 1951 if(t == T) { 1952 t = typ(etype); 1953 t->sym = s1; 1954 1955 if(etype != TANY && etype != TSTRING) 1956 dowidth(t); 1957 types[etype] = t; 1958 } 1959 s1->lexical = LNAME; 1960 s1->def = typenod(t); 1961 continue; 1962 } 1963 1964 etype = syms[i].op; 1965 if(etype != OXXX) { 1966 s1 = pkglookup(syms[i].name, builtinpkg); 1967 s1->lexical = LNAME; 1968 s1->def = nod(ONAME, N, N); 1969 s1->def->sym = s1; 1970 s1->def->etype = etype; 1971 s1->def->builtin = 1; 1972 } 1973 } 1974 1975 // logically, the type of a string literal. 1976 // types[TSTRING] is the named type string 1977 // (the type of x in var x string or var x = "hello"). 1978 // this is the ideal form 1979 // (the type of x in const x = "hello"). 1980 idealstring = typ(TSTRING); 1981 idealbool = typ(TBOOL); 1982 1983 s = pkglookup("true", builtinpkg); 1984 s->def = nodbool(1); 1985 s->def->sym = lookup("true"); 1986 s->def->type = idealbool; 1987 1988 s = pkglookup("false", builtinpkg); 1989 s->def = nodbool(0); 1990 s->def->sym = lookup("false"); 1991 s->def->type = idealbool; 1992 1993 s = lookup("_"); 1994 s->block = -100; 1995 s->def = nod(ONAME, N, N); 1996 s->def->sym = s; 1997 types[TBLANK] = typ(TBLANK); 1998 s->def->type = types[TBLANK]; 1999 nblank = s->def; 2000 2001 s = pkglookup("_", builtinpkg); 2002 s->block = -100; 2003 s->def = nod(ONAME, N, N); 2004 s->def->sym = s; 2005 types[TBLANK] = typ(TBLANK); 2006 s->def->type = types[TBLANK]; 2007 2008 types[TNIL] = typ(TNIL); 2009 s = pkglookup("nil", builtinpkg); 2010 v.ctype = CTNIL; 2011 s->def = nodlit(v); 2012 s->def->sym = s; 2013 } 2014 2015 static void 2016 lexinit1(void) 2017 { 2018 Sym *s, *s1; 2019 Type *t, *f, *rcvr, *in, *out; 2020 2021 // t = interface { Error() string } 2022 rcvr = typ(TSTRUCT); 2023 rcvr->type = typ(TFIELD); 2024 rcvr->type->type = ptrto(typ(TSTRUCT)); 2025 rcvr->funarg = 1; 2026 in = typ(TSTRUCT); 2027 in->funarg = 1; 2028 out = typ(TSTRUCT); 2029 out->type = typ(TFIELD); 2030 out->type->type = types[TSTRING]; 2031 out->funarg = 1; 2032 f = typ(TFUNC); 2033 *getthis(f) = rcvr; 2034 *getoutarg(f) = out; 2035 *getinarg(f) = in; 2036 f->thistuple = 1; 2037 f->intuple = 0; 2038 f->outnamed = 0; 2039 f->outtuple = 1; 2040 t = typ(TINTER); 2041 t->type = typ(TFIELD); 2042 t->type->sym = lookup("Error"); 2043 t->type->type = f; 2044 2045 // error type 2046 s = lookup("error"); 2047 s->lexical = LNAME; 2048 s1 = pkglookup("error", builtinpkg); 2049 errortype = t; 2050 errortype->sym = s1; 2051 s1->lexical = LNAME; 2052 s1->def = typenod(errortype); 2053 2054 // byte alias 2055 s = lookup("byte"); 2056 s->lexical = LNAME; 2057 s1 = pkglookup("byte", builtinpkg); 2058 bytetype = typ(TUINT8); 2059 bytetype->sym = s1; 2060 s1->lexical = LNAME; 2061 s1->def = typenod(bytetype); 2062 2063 // rune alias 2064 s = lookup("rune"); 2065 s->lexical = LNAME; 2066 s1 = pkglookup("rune", builtinpkg); 2067 runetype = typ(TINT32); 2068 runetype->sym = s1; 2069 s1->lexical = LNAME; 2070 s1->def = typenod(runetype); 2071 } 2072 2073 static void 2074 lexfini(void) 2075 { 2076 Sym *s; 2077 int lex, etype, i; 2078 Val v; 2079 2080 for(i=0; i<nelem(syms); i++) { 2081 lex = syms[i].lexical; 2082 if(lex != LNAME) 2083 continue; 2084 s = lookup(syms[i].name); 2085 s->lexical = lex; 2086 2087 etype = syms[i].etype; 2088 if(etype != Txxx && (etype != TANY || debug['A']) && s->def == N) { 2089 s->def = typenod(types[etype]); 2090 s->origpkg = builtinpkg; 2091 } 2092 2093 etype = syms[i].op; 2094 if(etype != OXXX && s->def == N) { 2095 s->def = nod(ONAME, N, N); 2096 s->def->sym = s; 2097 s->def->etype = etype; 2098 s->def->builtin = 1; 2099 s->origpkg = builtinpkg; 2100 } 2101 } 2102 2103 // backend-specific builtin types (e.g. int). 2104 for(i=0; typedefs[i].name; i++) { 2105 s = lookup(typedefs[i].name); 2106 if(s->def == N) { 2107 s->def = typenod(types[typedefs[i].etype]); 2108 s->origpkg = builtinpkg; 2109 } 2110 } 2111 2112 // there's only so much table-driven we can handle. 2113 // these are special cases. 2114 s = lookup("byte"); 2115 if(s->def == N) { 2116 s->def = typenod(bytetype); 2117 s->origpkg = builtinpkg; 2118 } 2119 2120 s = lookup("error"); 2121 if(s->def == N) { 2122 s->def = typenod(errortype); 2123 s->origpkg = builtinpkg; 2124 } 2125 2126 s = lookup("rune"); 2127 if(s->def == N) { 2128 s->def = typenod(runetype); 2129 s->origpkg = builtinpkg; 2130 } 2131 2132 s = lookup("nil"); 2133 if(s->def == N) { 2134 v.ctype = CTNIL; 2135 s->def = nodlit(v); 2136 s->def->sym = s; 2137 s->origpkg = builtinpkg; 2138 } 2139 2140 s = lookup("iota"); 2141 if(s->def == N) { 2142 s->def = nod(OIOTA, N, N); 2143 s->def->sym = s; 2144 s->origpkg = builtinpkg; 2145 } 2146 2147 s = lookup("true"); 2148 if(s->def == N) { 2149 s->def = nodbool(1); 2150 s->def->sym = s; 2151 s->origpkg = builtinpkg; 2152 } 2153 2154 s = lookup("false"); 2155 if(s->def == N) { 2156 s->def = nodbool(0); 2157 s->def->sym = s; 2158 s->origpkg = builtinpkg; 2159 } 2160 2161 nodfp = nod(ONAME, N, N); 2162 nodfp->type = types[TINT32]; 2163 nodfp->xoffset = 0; 2164 nodfp->class = PPARAM; 2165 nodfp->sym = lookup(".fp"); 2166 } 2167 2168 struct 2169 { 2170 int lex; 2171 char* name; 2172 } lexn[] = 2173 { 2174 LANDAND, "ANDAND", 2175 LANDNOT, "ANDNOT", 2176 LASOP, "ASOP", 2177 LBREAK, "BREAK", 2178 LCASE, "CASE", 2179 LCHAN, "CHAN", 2180 LCOLAS, "COLAS", 2181 LCOMM, "<-", 2182 LCONST, "CONST", 2183 LCONTINUE, "CONTINUE", 2184 LDDD, "...", 2185 LDEC, "DEC", 2186 LDEFAULT, "DEFAULT", 2187 LDEFER, "DEFER", 2188 LELSE, "ELSE", 2189 LEQ, "EQ", 2190 LFALL, "FALL", 2191 LFOR, "FOR", 2192 LFUNC, "FUNC", 2193 LGE, "GE", 2194 LGO, "GO", 2195 LGOTO, "GOTO", 2196 LGT, "GT", 2197 LIF, "IF", 2198 LIMPORT, "IMPORT", 2199 LINC, "INC", 2200 LINTERFACE, "INTERFACE", 2201 LLE, "LE", 2202 LLITERAL, "LITERAL", 2203 LLSH, "LSH", 2204 LLT, "LT", 2205 LMAP, "MAP", 2206 LNAME, "NAME", 2207 LNE, "NE", 2208 LOROR, "OROR", 2209 LPACKAGE, "PACKAGE", 2210 LRANGE, "RANGE", 2211 LRETURN, "RETURN", 2212 LRSH, "RSH", 2213 LSELECT, "SELECT", 2214 LSTRUCT, "STRUCT", 2215 LSWITCH, "SWITCH", 2216 LTYPE, "TYPE", 2217 LVAR, "VAR", 2218 }; 2219 2220 char* 2221 lexname(int lex) 2222 { 2223 int i; 2224 static char buf[100]; 2225 2226 for(i=0; i<nelem(lexn); i++) 2227 if(lexn[i].lex == lex) 2228 return lexn[i].name; 2229 snprint(buf, sizeof(buf), "LEX-%d", lex); 2230 return buf; 2231 } 2232 2233 struct 2234 { 2235 char *have; 2236 char *want; 2237 } yytfix[] = 2238 { 2239 "$end", "EOF", 2240 "LLITERAL", "literal", 2241 "LASOP", "op=", 2242 "LBREAK", "break", 2243 "LCASE", "case", 2244 "LCHAN", "chan", 2245 "LCOLAS", ":=", 2246 "LCONST", "const", 2247 "LCONTINUE", "continue", 2248 "LDDD", "...", 2249 "LDEFAULT", "default", 2250 "LDEFER", "defer", 2251 "LELSE", "else", 2252 "LFALL", "fallthrough", 2253 "LFOR", "for", 2254 "LFUNC", "func", 2255 "LGO", "go", 2256 "LGOTO", "goto", 2257 "LIF", "if", 2258 "LIMPORT", "import", 2259 "LINTERFACE", "interface", 2260 "LMAP", "map", 2261 "LNAME", "name", 2262 "LPACKAGE", "package", 2263 "LRANGE", "range", 2264 "LRETURN", "return", 2265 "LSELECT", "select", 2266 "LSTRUCT", "struct", 2267 "LSWITCH", "switch", 2268 "LTYPE", "type", 2269 "LVAR", "var", 2270 "LANDAND", "&&", 2271 "LANDNOT", "&^", 2272 "LBODY", "{", 2273 "LCOMM", "<-", 2274 "LDEC", "--", 2275 "LINC", "++", 2276 "LEQ", "==", 2277 "LGE", ">=", 2278 "LGT", ">", 2279 "LLE", "<=", 2280 "LLT", "<", 2281 "LLSH", "<<", 2282 "LRSH", ">>", 2283 "LOROR", "||", 2284 "LNE", "!=", 2285 2286 // spell out to avoid confusion with punctuation in error messages 2287 "';'", "semicolon or newline", 2288 "','", "comma", 2289 }; 2290 2291 static void 2292 yytinit(void) 2293 { 2294 int i, j; 2295 extern char *yytname[]; 2296 char *s, *t; 2297 2298 for(i=0; yytname[i] != nil; i++) { 2299 s = yytname[i]; 2300 2301 if(strcmp(s, "LLITERAL") == 0) { 2302 strcpy(litbuf, "literal"); 2303 yytname[i] = litbuf; 2304 goto loop; 2305 } 2306 2307 // apply yytfix if possible 2308 for(j=0; j<nelem(yytfix); j++) { 2309 if(strcmp(s, yytfix[j].have) == 0) { 2310 yytname[i] = yytfix[j].want; 2311 goto loop; 2312 } 2313 } 2314 2315 // turn 'x' into x. 2316 if(s[0] == '\'') { 2317 t = strdup(s+1); 2318 t[strlen(t)-1] = '\0'; 2319 yytname[i] = t; 2320 } 2321 loop:; 2322 } 2323 } 2324 2325 static void 2326 pkgnotused(int lineno, Strlit *path, char *name) 2327 { 2328 char *elem; 2329 2330 // If the package was imported with a name other than the final 2331 // import path element, show it explicitly in the error message. 2332 // Note that this handles both renamed imports and imports of 2333 // packages containing unconventional package declarations. 2334 // Note that this uses / always, even on Windows, because Go import 2335 // paths always use forward slashes. 2336 elem = strrchr(path->s, '/'); 2337 if(elem != nil) 2338 elem++; 2339 else 2340 elem = path->s; 2341 if(name == nil || strcmp(elem, name) == 0) 2342 yyerrorl(lineno, "imported and not used: \"%Z\"", path); 2343 else 2344 yyerrorl(lineno, "imported and not used: \"%Z\" as %s", path, name); 2345 } 2346 2347 void 2348 mkpackage(char* pkgname) 2349 { 2350 Sym *s; 2351 int32 h; 2352 char *p, *q; 2353 2354 if(localpkg->name == nil) { 2355 if(strcmp(pkgname, "_") == 0) 2356 yyerror("invalid package name _"); 2357 localpkg->name = pkgname; 2358 } else { 2359 if(strcmp(pkgname, localpkg->name) != 0) 2360 yyerror("package %s; expected %s", pkgname, localpkg->name); 2361 for(h=0; h<NHASH; h++) { 2362 for(s = hash[h]; s != S; s = s->link) { 2363 if(s->def == N || s->pkg != localpkg) 2364 continue; 2365 if(s->def->op == OPACK) { 2366 // throw away top-level package name leftover 2367 // from previous file. 2368 // leave s->block set to cause redeclaration 2369 // errors if a conflicting top-level name is 2370 // introduced by a different file. 2371 if(!s->def->used && !nsyntaxerrors) 2372 pkgnotused(s->def->lineno, s->def->pkg->path, s->name); 2373 s->def = N; 2374 continue; 2375 } 2376 if(s->def->sym != s) { 2377 // throw away top-level name left over 2378 // from previous import . "x" 2379 if(s->def->pack != N && !s->def->pack->used && !nsyntaxerrors) { 2380 pkgnotused(s->def->pack->lineno, s->def->pack->pkg->path, nil); 2381 s->def->pack->used = 1; 2382 } 2383 s->def = N; 2384 continue; 2385 } 2386 } 2387 } 2388 } 2389 2390 if(outfile == nil) { 2391 p = strrchr(infile, '/'); 2392 if(ctxt->windows) { 2393 q = strrchr(infile, '\\'); 2394 if(q > p) 2395 p = q; 2396 } 2397 if(p == nil) 2398 p = infile; 2399 else 2400 p = p+1; 2401 snprint(namebuf, sizeof(namebuf), "%s", p); 2402 p = strrchr(namebuf, '.'); 2403 if(p != nil) 2404 *p = 0; 2405 outfile = smprint("%s.%c", namebuf, thechar); 2406 } 2407 }