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