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