github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/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  	if(strcmp(lexbuf, "go:nowritebarrier") == 0) {
  1659  		if(!compiling_runtime)
  1660  			yyerror("//go:nowritebarrier only allowed in runtime");
  1661  		nowritebarrier = 1;
  1662  		goto out;
  1663  	}
  1664  	
  1665  out:
  1666  	return c;
  1667  }
  1668  
  1669  static char*
  1670  getimpsym(char **pp)
  1671  {
  1672  	char *p, *start;
  1673  	
  1674  	more(pp); // skip spaces
  1675  
  1676  	p = *pp;
  1677  	if(*p == '\0' || *p == '"')
  1678  		return nil;
  1679  	
  1680  	start = p;
  1681  	while(*p != '\0' && !yy_isspace(*p) && *p != '"')
  1682  		p++;
  1683  	if(*p != '\0')
  1684  		*p++ = '\0';
  1685  	
  1686  	*pp = p;
  1687  	return start;
  1688  }
  1689  
  1690  static char*
  1691  getquoted(char **pp)
  1692  {
  1693  	char *p, *start;
  1694  	
  1695  	more(pp); // skip spaces
  1696  	
  1697  	p = *pp;
  1698  	if(*p != '"')
  1699  		return nil;
  1700  	p++;
  1701  	
  1702  	start = p;
  1703  	while(*p != '"') {
  1704  		if(*p == '\0')
  1705  			return nil;
  1706  		p++;
  1707  	}
  1708  	*p++ = '\0';
  1709  	*pp = p;
  1710  	return start;
  1711  }
  1712  
  1713  // Copied nearly verbatim from the C compiler's #pragma parser.
  1714  // TODO: Rewrite more cleanly once the compiler is written in Go.
  1715  static void
  1716  pragcgo(char *text)
  1717  {
  1718  	char *local, *remote, *p, *q, *verb;
  1719  	
  1720  	for(q=text; *q != '\0' && *q != ' '; q++)
  1721  		;
  1722  	if(*q == ' ')
  1723  		*q++ = '\0';
  1724  	
  1725  	verb = text+3; // skip "go:"
  1726  
  1727  	if(strcmp(verb, "cgo_dynamic_linker") == 0 || strcmp(verb, "dynlinker") == 0) {
  1728  		p = getquoted(&q);
  1729  		if(p == nil)
  1730  			goto err1;
  1731  		fmtprint(&pragcgobuf, "cgo_dynamic_linker %q\n", p);
  1732  		goto out;
  1733  	
  1734  	err1:
  1735  		yyerror("usage: //go:cgo_dynamic_linker \"path\"");
  1736  		goto out;
  1737  	}	
  1738  
  1739  	if(strcmp(verb, "dynexport") == 0)
  1740  		verb = "cgo_export_dynamic";
  1741  	if(strcmp(verb, "cgo_export_static") == 0 || strcmp(verb, "cgo_export_dynamic") == 0) {
  1742  		local = getimpsym(&q);
  1743  		if(local == nil)
  1744  			goto err2;
  1745  		if(!more(&q)) {
  1746  			fmtprint(&pragcgobuf, "%s %q\n", verb, local);
  1747  			goto out;
  1748  		}
  1749  		remote = getimpsym(&q);
  1750  		if(remote == nil)
  1751  			goto err2;
  1752  		fmtprint(&pragcgobuf, "%s %q %q\n", verb, local, remote);
  1753  		goto out;
  1754  	
  1755  	err2:
  1756  		yyerror("usage: //go:%s local [remote]", verb);
  1757  		goto out;
  1758  	}
  1759  	
  1760  	if(strcmp(verb, "cgo_import_dynamic") == 0 || strcmp(verb, "dynimport") == 0) {
  1761  		local = getimpsym(&q);
  1762  		if(local == nil)
  1763  			goto err3;
  1764  		if(!more(&q)) {
  1765  			fmtprint(&pragcgobuf, "cgo_import_dynamic %q\n", local);
  1766  			goto out;
  1767  		}
  1768  		remote = getimpsym(&q);
  1769  		if(remote == nil)
  1770  			goto err3;
  1771  		if(!more(&q)) {
  1772  			fmtprint(&pragcgobuf, "cgo_import_dynamic %q %q\n", local, remote);
  1773  			goto out;
  1774  		}
  1775  		p = getquoted(&q);
  1776  		if(p == nil)	
  1777  			goto err3;
  1778  		fmtprint(&pragcgobuf, "cgo_import_dynamic %q %q %q\n", local, remote, p);
  1779  		goto out;
  1780  	
  1781  	err3:
  1782  		yyerror("usage: //go:cgo_import_dynamic local [remote [\"library\"]]");
  1783  		goto out;
  1784  	}
  1785  	
  1786  	if(strcmp(verb, "cgo_import_static") == 0) {
  1787  		local = getimpsym(&q);
  1788  		if(local == nil || more(&q))
  1789  			goto err4;
  1790  		fmtprint(&pragcgobuf, "cgo_import_static %q\n", local);
  1791  		goto out;
  1792  		
  1793  	err4:
  1794  		yyerror("usage: //go:cgo_import_static local");
  1795  		goto out;
  1796  	}
  1797  	
  1798  	if(strcmp(verb, "cgo_ldflag") == 0) {
  1799  		p = getquoted(&q);
  1800  		if(p == nil)
  1801  			goto err5;
  1802  		fmtprint(&pragcgobuf, "cgo_ldflag %q\n", p);
  1803  		goto out;
  1804  
  1805  	err5:
  1806  		yyerror("usage: //go:cgo_ldflag \"arg\"");
  1807  		goto out;
  1808  	}
  1809  
  1810  out:;
  1811  }
  1812  
  1813  int32
  1814  yylex(void)
  1815  {
  1816  	int lx;
  1817  	
  1818  	lx = _yylex();
  1819  	
  1820  	if(curio.nlsemi && lx == EOF) {
  1821  		// Treat EOF as "end of line" for the purposes
  1822  		// of inserting a semicolon.
  1823  		lx = ';';
  1824  	}
  1825  
  1826  	switch(lx) {
  1827  	case LNAME:
  1828  	case LLITERAL:
  1829  	case LBREAK:
  1830  	case LCONTINUE:
  1831  	case LFALL:
  1832  	case LRETURN:
  1833  	case LINC:
  1834  	case LDEC:
  1835  	case ')':
  1836  	case '}':
  1837  	case ']':
  1838  		curio.nlsemi = 1;
  1839  		break;
  1840  	default:
  1841  		curio.nlsemi = 0;
  1842  		break;
  1843  	}
  1844  
  1845  	// Track last two tokens returned by yylex.
  1846  	yyprev = yylast;
  1847  	yylast = lx;
  1848  	return lx;
  1849  }
  1850  
  1851  static int
  1852  getc(void)
  1853  {
  1854  	int c, c1, c2;
  1855  
  1856  	c = curio.peekc;
  1857  	if(c != 0) {
  1858  		curio.peekc = curio.peekc1;
  1859  		curio.peekc1 = 0;
  1860  		goto check;
  1861  	}
  1862  	
  1863  	if(curio.bin == nil) {
  1864  		c = *curio.cp & 0xff;
  1865  		if(c != 0)
  1866  			curio.cp++;
  1867  	} else {
  1868  	loop:
  1869  		c = BGETC(curio.bin);
  1870  		if(c == 0xef) {
  1871  			c1 = BGETC(curio.bin);
  1872  			c2 = BGETC(curio.bin);
  1873  			if(c1 == 0xbb && c2 == 0xbf) {
  1874  				yyerrorl(lexlineno, "Unicode (UTF-8) BOM in middle of file");
  1875  				goto loop;
  1876  			}
  1877  			Bungetc(curio.bin);
  1878  			Bungetc(curio.bin);
  1879  		}
  1880  	}
  1881  
  1882  check:
  1883  	switch(c) {
  1884  	case 0:
  1885  		if(curio.bin != nil) {
  1886  			yyerror("illegal NUL byte");
  1887  			break;
  1888  		}
  1889  	case EOF:
  1890  		// insert \n at EOF
  1891  		if(curio.eofnl || curio.last == '\n')
  1892  			return EOF;
  1893  		curio.eofnl = 1;
  1894  		c = '\n';
  1895  	case '\n':
  1896  		if(pushedio.bin == nil)
  1897  			lexlineno++;
  1898  		break;
  1899  	}
  1900  	curio.last = c;
  1901  	return c;
  1902  }
  1903  
  1904  static void
  1905  ungetc(int c)
  1906  {
  1907  	curio.peekc1 = curio.peekc;
  1908  	curio.peekc = c;
  1909  	if(c == '\n' && pushedio.bin == nil)
  1910  		lexlineno--;
  1911  }
  1912  
  1913  static int32
  1914  getr(void)
  1915  {
  1916  	int c, i;
  1917  	char str[UTFmax+1];
  1918  	Rune rune;
  1919  
  1920  	c = getc();
  1921  	if(c < Runeself)
  1922  		return c;
  1923  	i = 0;
  1924  	str[i++] = c;
  1925  
  1926  loop:
  1927  	c = getc();
  1928  	str[i++] = c;
  1929  	if(!fullrune(str, i))
  1930  		goto loop;
  1931  	c = chartorune(&rune, str);
  1932  	if(rune == Runeerror && c == 1) {
  1933  		lineno = lexlineno;
  1934  		yyerror("illegal UTF-8 sequence");
  1935  		flusherrors();
  1936  		print("\t");
  1937  		for(c=0; c<i; c++)
  1938  			print("%s%.2x", c > 0 ? " " : "", *(uchar*)(str+c));
  1939  		print("\n");
  1940  	}
  1941  	return rune;
  1942  }
  1943  
  1944  static int
  1945  escchar(int e, int *escflg, vlong *val)
  1946  {
  1947  	int i, u, c;
  1948  	vlong l;
  1949  
  1950  	*escflg = 0;
  1951  
  1952  	c = getr();
  1953  	switch(c) {
  1954  	case EOF:
  1955  		yyerror("eof in string");
  1956  		return 1;
  1957  	case '\n':
  1958  		yyerror("newline in string");
  1959  		return 1;
  1960  	case '\\':
  1961  		break;
  1962  	default:
  1963  		if(c == e)
  1964  			return 1;
  1965  		*val = c;
  1966  		return 0;
  1967  	}
  1968  
  1969  	u = 0;
  1970  	c = getr();
  1971  	switch(c) {
  1972  	case 'x':
  1973  		*escflg = 1;	// it's a byte
  1974  		i = 2;
  1975  		goto hex;
  1976  
  1977  	case 'u':
  1978  		i = 4;
  1979  		u = 1;
  1980  		goto hex;
  1981  
  1982  	case 'U':
  1983  		i = 8;
  1984  		u = 1;
  1985  		goto hex;
  1986  
  1987  	case '0':
  1988  	case '1':
  1989  	case '2':
  1990  	case '3':
  1991  	case '4':
  1992  	case '5':
  1993  	case '6':
  1994  	case '7':
  1995  		*escflg = 1;	// it's a byte
  1996  		goto oct;
  1997  
  1998  	case 'a': c = '\a'; break;
  1999  	case 'b': c = '\b'; break;
  2000  	case 'f': c = '\f'; break;
  2001  	case 'n': c = '\n'; break;
  2002  	case 'r': c = '\r'; break;
  2003  	case 't': c = '\t'; break;
  2004  	case 'v': c = '\v'; break;
  2005  	case '\\': c = '\\'; break;
  2006  
  2007  	default:
  2008  		if(c != e)
  2009  			yyerror("unknown escape sequence: %c", c);
  2010  	}
  2011  	*val = c;
  2012  	return 0;
  2013  
  2014  hex:
  2015  	l = 0;
  2016  	for(; i>0; i--) {
  2017  		c = getc();
  2018  		if(c >= '0' && c <= '9') {
  2019  			l = l*16 + c-'0';
  2020  			continue;
  2021  		}
  2022  		if(c >= 'a' && c <= 'f') {
  2023  			l = l*16 + c-'a' + 10;
  2024  			continue;
  2025  		}
  2026  		if(c >= 'A' && c <= 'F') {
  2027  			l = l*16 + c-'A' + 10;
  2028  			continue;
  2029  		}
  2030  		yyerror("non-hex character in escape sequence: %c", c);
  2031  		ungetc(c);
  2032  		break;
  2033  	}
  2034  	if(u && (l > Runemax || (0xd800 <= l && l < 0xe000))) {
  2035  		yyerror("invalid Unicode code point in escape sequence: %#llx", l);
  2036  		l = Runeerror;
  2037  	}
  2038  	*val = l;
  2039  	return 0;
  2040  
  2041  oct:
  2042  	l = c - '0';
  2043  	for(i=2; i>0; i--) {
  2044  		c = getc();
  2045  		if(c >= '0' && c <= '7') {
  2046  			l = l*8 + c-'0';
  2047  			continue;
  2048  		}
  2049  		yyerror("non-octal character in escape sequence: %c", c);
  2050  		ungetc(c);
  2051  	}
  2052  	if(l > 255)
  2053  		yyerror("octal escape value > 255: %d", l);
  2054  
  2055  	*val = l;
  2056  	return 0;
  2057  }
  2058  
  2059  static	struct
  2060  {
  2061  	char*	name;
  2062  	int	lexical;
  2063  	int	etype;
  2064  	int	op;
  2065  } syms[] =
  2066  {
  2067  /*	name		lexical		etype		op
  2068   */
  2069  /* basic types */
  2070  	{"int8",		LNAME,		TINT8,		OXXX},
  2071  	{"int16",	LNAME,		TINT16,		OXXX},
  2072  	{"int32",	LNAME,		TINT32,		OXXX},
  2073  	{"int64",	LNAME,		TINT64,		OXXX},
  2074  
  2075  	{"uint8",	LNAME,		TUINT8,		OXXX},
  2076  	{"uint16",	LNAME,		TUINT16,	OXXX},
  2077  	{"uint32",	LNAME,		TUINT32,	OXXX},
  2078  	{"uint64",	LNAME,		TUINT64,	OXXX},
  2079  
  2080  	{"float32",	LNAME,		TFLOAT32,	OXXX},
  2081  	{"float64",	LNAME,		TFLOAT64,	OXXX},
  2082  
  2083  	{"complex64",	LNAME,		TCOMPLEX64,	OXXX},
  2084  	{"complex128",	LNAME,		TCOMPLEX128,	OXXX},
  2085  
  2086  	{"bool",		LNAME,		TBOOL,		OXXX},
  2087  	{"string",	LNAME,		TSTRING,	OXXX},
  2088  
  2089  	{"any",		LNAME,		TANY,		OXXX},
  2090  
  2091  	{"break",	LBREAK,		Txxx,		OXXX},
  2092  	{"case",		LCASE,		Txxx,		OXXX},
  2093  	{"chan",		LCHAN,		Txxx,		OXXX},
  2094  	{"const",	LCONST,		Txxx,		OXXX},
  2095  	{"continue",	LCONTINUE,	Txxx,		OXXX},
  2096  	{"default",	LDEFAULT,	Txxx,		OXXX},
  2097  	{"else",		LELSE,		Txxx,		OXXX},
  2098  	{"defer",	LDEFER,		Txxx,		OXXX},
  2099  	{"fallthrough",	LFALL,		Txxx,		OXXX},
  2100  	{"for",		LFOR,		Txxx,		OXXX},
  2101  	{"func",		LFUNC,		Txxx,		OXXX},
  2102  	{"go",		LGO,		Txxx,		OXXX},
  2103  	{"goto",		LGOTO,		Txxx,		OXXX},
  2104  	{"if",		LIF,		Txxx,		OXXX},
  2105  	{"import",	LIMPORT,	Txxx,		OXXX},
  2106  	{"interface",	LINTERFACE,	Txxx,		OXXX},
  2107  	{"map",		LMAP,		Txxx,		OXXX},
  2108  	{"package",	LPACKAGE,	Txxx,		OXXX},
  2109  	{"range",	LRANGE,		Txxx,		OXXX},
  2110  	{"return",	LRETURN,	Txxx,		OXXX},
  2111  	{"select",	LSELECT,	Txxx,		OXXX},
  2112  	{"struct",	LSTRUCT,	Txxx,		OXXX},
  2113  	{"switch",	LSWITCH,	Txxx,		OXXX},
  2114  	{"type",		LTYPE,		Txxx,		OXXX},
  2115  	{"var",		LVAR,		Txxx,		OXXX},
  2116  
  2117  	{"append",	LNAME,		Txxx,		OAPPEND},
  2118  	{"cap",		LNAME,		Txxx,		OCAP},
  2119  	{"close",	LNAME,		Txxx,		OCLOSE},
  2120  	{"complex",	LNAME,		Txxx,		OCOMPLEX},
  2121  	{"copy",		LNAME,		Txxx,		OCOPY},
  2122  	{"delete",	LNAME,		Txxx,		ODELETE},
  2123  	{"imag",		LNAME,		Txxx,		OIMAG},
  2124  	{"len",		LNAME,		Txxx,		OLEN},
  2125  	{"make",		LNAME,		Txxx,		OMAKE},
  2126  	{"new",		LNAME,		Txxx,		ONEW},
  2127  	{"panic",	LNAME,		Txxx,		OPANIC},
  2128  	{"print",	LNAME,		Txxx,		OPRINT},
  2129  	{"println",	LNAME,		Txxx,		OPRINTN},
  2130  	{"real",		LNAME,		Txxx,		OREAL},
  2131  	{"recover",	LNAME,		Txxx,		ORECOVER},
  2132  
  2133  	{"notwithstanding",		LIGNORE,	Txxx,		OXXX},
  2134  	{"thetruthofthematter",		LIGNORE,	Txxx,		OXXX},
  2135  	{"despiteallobjections",		LIGNORE,	Txxx,		OXXX},
  2136  	{"whereas",			LIGNORE,	Txxx,		OXXX},
  2137  	{"insofaras",			LIGNORE,	Txxx,		OXXX},
  2138  };
  2139  
  2140  static void
  2141  lexinit(void)
  2142  {
  2143  	int i, lex;
  2144  	Sym *s, *s1;
  2145  	Type *t;
  2146  	int etype;
  2147  	Val v;
  2148  
  2149  	/*
  2150  	 * initialize basic types array
  2151  	 * initialize known symbols
  2152  	 */
  2153  	for(i=0; i<nelem(syms); i++) {
  2154  		lex = syms[i].lexical;
  2155  		s = lookup(syms[i].name);
  2156  		s->lexical = lex;
  2157  
  2158  		etype = syms[i].etype;
  2159  		if(etype != Txxx) {
  2160  			if(etype < 0 || etype >= nelem(types))
  2161  				fatal("lexinit: %s bad etype", s->name);
  2162  			s1 = pkglookup(syms[i].name, builtinpkg);
  2163  			t = types[etype];
  2164  			if(t == T) {
  2165  				t = typ(etype);
  2166  				t->sym = s1;
  2167  
  2168  				if(etype != TANY && etype != TSTRING)
  2169  					dowidth(t);
  2170  				types[etype] = t;
  2171  			}
  2172  			s1->lexical = LNAME;
  2173  			s1->def = typenod(t);
  2174  			continue;
  2175  		}
  2176  
  2177  		etype = syms[i].op;
  2178  		if(etype != OXXX) {
  2179  			s1 = pkglookup(syms[i].name, builtinpkg);
  2180  			s1->lexical = LNAME;
  2181  			s1->def = nod(ONAME, N, N);
  2182  			s1->def->sym = s1;
  2183  			s1->def->etype = etype;
  2184  			s1->def->builtin = 1;
  2185  		}
  2186  	}
  2187  
  2188  	// logically, the type of a string literal.
  2189  	// types[TSTRING] is the named type string
  2190  	// (the type of x in var x string or var x = "hello").
  2191  	// this is the ideal form
  2192  	// (the type of x in const x = "hello").
  2193  	idealstring = typ(TSTRING);
  2194  	idealbool = typ(TBOOL);
  2195  
  2196  	s = pkglookup("true", builtinpkg);
  2197  	s->def = nodbool(1);
  2198  	s->def->sym = lookup("true");
  2199  	s->def->type = idealbool;
  2200  
  2201  	s = pkglookup("false", builtinpkg);
  2202  	s->def = nodbool(0);
  2203  	s->def->sym = lookup("false");
  2204  	s->def->type = idealbool;
  2205  
  2206  	s = lookup("_");
  2207  	s->block = -100;
  2208  	s->def = nod(ONAME, N, N);
  2209  	s->def->sym = s;
  2210  	types[TBLANK] = typ(TBLANK);
  2211  	s->def->type = types[TBLANK];
  2212  	nblank = s->def;
  2213  
  2214  	s = pkglookup("_", builtinpkg);
  2215  	s->block = -100;
  2216  	s->def = nod(ONAME, N, N);
  2217  	s->def->sym = s;
  2218  	types[TBLANK] = typ(TBLANK);
  2219  	s->def->type = types[TBLANK];
  2220  
  2221  	types[TNIL] = typ(TNIL);
  2222  	s = pkglookup("nil", builtinpkg);
  2223  	v.ctype = CTNIL;
  2224  	s->def = nodlit(v);
  2225  	s->def->sym = s;
  2226  }
  2227  
  2228  static void
  2229  lexinit1(void)
  2230  {
  2231  	Sym *s, *s1;
  2232  	Type *t, *f, *rcvr, *in, *out;
  2233  
  2234  	// t = interface { Error() string }
  2235  	rcvr = typ(TSTRUCT);
  2236  	rcvr->type = typ(TFIELD);
  2237  	rcvr->type->type = ptrto(typ(TSTRUCT));
  2238  	rcvr->funarg = 1;
  2239  	in = typ(TSTRUCT);
  2240  	in->funarg = 1;
  2241  	out = typ(TSTRUCT);
  2242  	out->type = typ(TFIELD);
  2243  	out->type->type = types[TSTRING];
  2244  	out->funarg = 1;
  2245  	f = typ(TFUNC);
  2246  	*getthis(f) = rcvr;
  2247  	*getoutarg(f) = out;
  2248  	*getinarg(f) = in;
  2249  	f->thistuple = 1;
  2250  	f->intuple = 0;
  2251  	f->outnamed = 0;
  2252  	f->outtuple = 1;
  2253  	t = typ(TINTER);
  2254  	t->type = typ(TFIELD);
  2255  	t->type->sym = lookup("Error");
  2256  	t->type->type = f;
  2257  
  2258  	// error type
  2259  	s = lookup("error");
  2260  	s->lexical = LNAME;
  2261  	s1 = pkglookup("error", builtinpkg);
  2262  	errortype = t;
  2263  	errortype->sym = s1;
  2264  	s1->lexical = LNAME;
  2265  	s1->def = typenod(errortype);
  2266  
  2267  	// byte alias
  2268  	s = lookup("byte");
  2269  	s->lexical = LNAME;
  2270  	s1 = pkglookup("byte", builtinpkg);
  2271  	bytetype = typ(TUINT8);
  2272  	bytetype->sym = s1;
  2273  	s1->lexical = LNAME;
  2274  	s1->def = typenod(bytetype);
  2275  
  2276  	// rune alias
  2277  	s = lookup("rune");
  2278  	s->lexical = LNAME;
  2279  	s1 = pkglookup("rune", builtinpkg);
  2280  	runetype = typ(TINT32);
  2281  	runetype->sym = s1;
  2282  	s1->lexical = LNAME;
  2283  	s1->def = typenod(runetype);
  2284  }
  2285  
  2286  static void
  2287  lexfini(void)
  2288  {
  2289  	Sym *s;
  2290  	int lex, etype, i;
  2291  	Val v;
  2292  
  2293  	for(i=0; i<nelem(syms); i++) {
  2294  		lex = syms[i].lexical;
  2295  		if(lex != LNAME)
  2296  			continue;
  2297  		s = lookup(syms[i].name);
  2298  		s->lexical = lex;
  2299  
  2300  		etype = syms[i].etype;
  2301  		if(etype != Txxx && (etype != TANY || debug['A']) && s->def == N) {
  2302  			s->def = typenod(types[etype]);
  2303  			s->origpkg = builtinpkg;
  2304  		}
  2305  
  2306  		etype = syms[i].op;
  2307  		if(etype != OXXX && s->def == N) {
  2308  			s->def = nod(ONAME, N, N);
  2309  			s->def->sym = s;
  2310  			s->def->etype = etype;
  2311  			s->def->builtin = 1;
  2312  			s->origpkg = builtinpkg;
  2313  		}
  2314  	}
  2315  
  2316  	// backend-specific builtin types (e.g. int).
  2317  	for(i=0; typedefs[i].name; i++) {
  2318  		s = lookup(typedefs[i].name);
  2319  		if(s->def == N) {
  2320  			s->def = typenod(types[typedefs[i].etype]);
  2321  			s->origpkg = builtinpkg;
  2322  		}
  2323  	}
  2324  
  2325  	// there's only so much table-driven we can handle.
  2326  	// these are special cases.
  2327  	s = lookup("byte");
  2328  	if(s->def == N) {
  2329  		s->def = typenod(bytetype);
  2330  		s->origpkg = builtinpkg;
  2331  	}
  2332  
  2333  	s = lookup("error");
  2334  	if(s->def == N) {
  2335  		s->def = typenod(errortype);
  2336  		s->origpkg = builtinpkg;
  2337  	}
  2338  
  2339  	s = lookup("rune");
  2340  	if(s->def == N) {
  2341  		s->def = typenod(runetype);
  2342  		s->origpkg = builtinpkg;
  2343  	}
  2344  
  2345  	s = lookup("nil");
  2346  	if(s->def == N) {
  2347  		v.ctype = CTNIL;
  2348  		s->def = nodlit(v);
  2349  		s->def->sym = s;
  2350  		s->origpkg = builtinpkg;
  2351  	}
  2352  
  2353  	s = lookup("iota");
  2354  	if(s->def == N) {
  2355  		s->def = nod(OIOTA, N, N);
  2356  		s->def->sym = s;
  2357  		s->origpkg = builtinpkg;
  2358  	}
  2359  
  2360  	s = lookup("true");
  2361  	if(s->def == N) {
  2362  		s->def = nodbool(1);
  2363  		s->def->sym = s;
  2364  		s->origpkg = builtinpkg;
  2365  	}
  2366  
  2367  	s = lookup("false");
  2368  	if(s->def == N) {
  2369  		s->def = nodbool(0);
  2370  		s->def->sym = s;
  2371  		s->origpkg = builtinpkg;
  2372  	}
  2373  
  2374  	nodfp = nod(ONAME, N, N);
  2375  	nodfp->type = types[TINT32];
  2376  	nodfp->xoffset = 0;
  2377  	nodfp->class = PPARAM;
  2378  	nodfp->sym = lookup(".fp");
  2379  }
  2380  
  2381  struct
  2382  {
  2383  	int	lex;
  2384  	char*	name;
  2385  } lexn[] =
  2386  {
  2387  	{LANDAND,	"ANDAND"},
  2388  	{LANDNOT,	"ANDNOT"},
  2389  	{LASOP,		"ASOP"},
  2390  	{LBREAK,		"BREAK"},
  2391  	{LCASE,		"CASE"},
  2392  	{LCHAN,		"CHAN"},
  2393  	{LCOLAS,		"COLAS"},
  2394  	{LCOMM,		"<-"},
  2395  	{LCONST,		"CONST"},
  2396  	{LCONTINUE,	"CONTINUE"},
  2397  	{LDDD,		"..."},
  2398  	{LDEC,		"DEC"},
  2399  	{LDEFAULT,	"DEFAULT"},
  2400  	{LDEFER,		"DEFER"},
  2401  	{LELSE,		"ELSE"},
  2402  	{LEQ,		"EQ"},
  2403  	{LFALL,		"FALL"},
  2404  	{LFOR,		"FOR"},
  2405  	{LFUNC,		"FUNC"},
  2406  	{LGE,		"GE"},
  2407  	{LGO,		"GO"},
  2408  	{LGOTO,		"GOTO"},
  2409  	{LGT,		"GT"},
  2410  	{LIF,		"IF"},
  2411  	{LIMPORT,	"IMPORT"},
  2412  	{LINC,		"INC"},
  2413  	{LINTERFACE,	"INTERFACE"},
  2414  	{LLE,		"LE"},
  2415  	{LLITERAL,	"LITERAL"},
  2416  	{LLSH,		"LSH"},
  2417  	{LLT,		"LT"},
  2418  	{LMAP,		"MAP"},
  2419  	{LNAME,		"NAME"},
  2420  	{LNE,		"NE"},
  2421  	{LOROR,		"OROR"},
  2422  	{LPACKAGE,	"PACKAGE"},
  2423  	{LRANGE,		"RANGE"},
  2424  	{LRETURN,	"RETURN"},
  2425  	{LRSH,		"RSH"},
  2426  	{LSELECT,	"SELECT"},
  2427  	{LSTRUCT,	"STRUCT"},
  2428  	{LSWITCH,	"SWITCH"},
  2429  	{LTYPE,		"TYPE"},
  2430  	{LVAR,		"VAR"},
  2431  };
  2432  
  2433  char*
  2434  lexname(int lex)
  2435  {
  2436  	int i;
  2437  	static char buf[100];
  2438  
  2439  	for(i=0; i<nelem(lexn); i++)
  2440  		if(lexn[i].lex == lex)
  2441  			return lexn[i].name;
  2442  	snprint(buf, sizeof(buf), "LEX-%d", lex);
  2443  	return buf;
  2444  }
  2445  
  2446  struct
  2447  {
  2448  	char *have;
  2449  	char *want;
  2450  } yytfix[] =
  2451  {
  2452  	{"$end",	"EOF"},
  2453  	{"LLITERAL",	"literal"},
  2454  	{"LASOP",	"op="},
  2455  	{"LBREAK",	"break"},
  2456  	{"LCASE",	"case"},
  2457  	{"LCHAN",	"chan"},
  2458  	{"LCOLAS",	":="},
  2459  	{"LCONST",	"const"},
  2460  	{"LCONTINUE",	"continue"},
  2461  	{"LDDD",	"..."},
  2462  	{"LDEFAULT",	"default"},
  2463  	{"LDEFER",	"defer"},
  2464  	{"LELSE",	"else"},
  2465  	{"LFALL",	"fallthrough"},
  2466  	{"LFOR",	"for"},
  2467  	{"LFUNC",	"func"},
  2468  	{"LGO",	"go"},
  2469  	{"LGOTO",	"goto"},
  2470  	{"LIF",	"if"},
  2471  	{"LIMPORT",	"import"},
  2472  	{"LINTERFACE",	"interface"},
  2473  	{"LMAP",	"map"},
  2474  	{"LNAME",	"name"},
  2475  	{"LPACKAGE",	"package"},
  2476  	{"LRANGE",	"range"},
  2477  	{"LRETURN",	"return"},
  2478  	{"LSELECT",	"select"},
  2479  	{"LSTRUCT",	"struct"},
  2480  	{"LSWITCH",	"switch"},
  2481  	{"LTYPE",	"type"},
  2482  	{"LVAR",	"var"},
  2483  	{"LANDAND",	"&&"},
  2484  	{"LANDNOT",	"&^"},
  2485  	{"LBODY",	"{"},
  2486  	{"LCOMM",	"<-"},
  2487  	{"LDEC",	"--"},
  2488  	{"LINC",	"++"},
  2489  	{"LEQ",	"=="},
  2490  	{"LGE",	">="},
  2491  	{"LGT",	">"},
  2492  	{"LLE",	"<="},
  2493  	{"LLT",	"<"},
  2494  	{"LLSH",	"<<"},
  2495  	{"LRSH",	">>"},
  2496  	{"LOROR",	"||"},
  2497  	{"LNE",	"!="},
  2498  	
  2499  	// spell out to avoid confusion with punctuation in error messages
  2500  	{"';'",	"semicolon or newline"},
  2501  	{"','",	"comma"},
  2502  };
  2503  
  2504  static void
  2505  yytinit(void)
  2506  {
  2507  	int i, j;
  2508  	extern char *yytname[];
  2509  	char *s, *t;
  2510  
  2511  	for(i=0; yytname[i] != nil; i++) {
  2512  		s = yytname[i];
  2513  		
  2514  		if(strcmp(s, "LLITERAL") == 0) {
  2515  			strcpy(litbuf, "literal");
  2516  			yytname[i] = litbuf;
  2517  			goto loop;
  2518  		}
  2519  		
  2520  		// apply yytfix if possible
  2521  		for(j=0; j<nelem(yytfix); j++) {
  2522  			if(strcmp(s, yytfix[j].have) == 0) {
  2523  				yytname[i] = yytfix[j].want;
  2524  				goto loop;
  2525  			}
  2526  		}
  2527  
  2528  		// turn 'x' into x.
  2529  		if(s[0] == '\'') {
  2530  			t = strdup(s+1);
  2531  			t[strlen(t)-1] = '\0';
  2532  			yytname[i] = t;
  2533  		}
  2534  	loop:;
  2535  	}		
  2536  }
  2537  
  2538  static void
  2539  pkgnotused(int lineno, Strlit *path, char *name)
  2540  {
  2541  	char *elem;
  2542  	
  2543  	// If the package was imported with a name other than the final
  2544  	// import path element, show it explicitly in the error message.
  2545  	// Note that this handles both renamed imports and imports of
  2546  	// packages containing unconventional package declarations.
  2547  	// Note that this uses / always, even on Windows, because Go import
  2548  	// paths always use forward slashes.
  2549  	elem = strrchr(path->s, '/');
  2550  	if(elem != nil)
  2551  		elem++;
  2552  	else
  2553  		elem = path->s;
  2554  	if(name == nil || strcmp(elem, name) == 0)
  2555  		yyerrorl(lineno, "imported and not used: \"%Z\"", path);
  2556  	else
  2557  		yyerrorl(lineno, "imported and not used: \"%Z\" as %s", path, name);
  2558  }
  2559  
  2560  void
  2561  mkpackage(char* pkgname)
  2562  {
  2563  	Sym *s;
  2564  	int32 h;
  2565  	char *p, *q;
  2566  
  2567  	if(localpkg->name == nil) {
  2568  		if(strcmp(pkgname, "_") == 0)
  2569  			yyerror("invalid package name _");
  2570  		localpkg->name = pkgname;
  2571  	} else {
  2572  		if(strcmp(pkgname, localpkg->name) != 0)
  2573  			yyerror("package %s; expected %s", pkgname, localpkg->name);
  2574  		for(h=0; h<NHASH; h++) {
  2575  			for(s = hash[h]; s != S; s = s->link) {
  2576  				if(s->def == N || s->pkg != localpkg)
  2577  					continue;
  2578  				if(s->def->op == OPACK) {
  2579  					// throw away top-level package name leftover
  2580  					// from previous file.
  2581  					// leave s->block set to cause redeclaration
  2582  					// errors if a conflicting top-level name is
  2583  					// introduced by a different file.
  2584  					if(!s->def->used && !nsyntaxerrors)
  2585  						pkgnotused(s->def->lineno, s->def->pkg->path, s->name);
  2586  					s->def = N;
  2587  					continue;
  2588  				}
  2589  				if(s->def->sym != s) {
  2590  					// throw away top-level name left over
  2591  					// from previous import . "x"
  2592  					if(s->def->pack != N && !s->def->pack->used && !nsyntaxerrors) {
  2593  						pkgnotused(s->def->pack->lineno, s->def->pack->pkg->path, nil);
  2594  						s->def->pack->used = 1;
  2595  					}
  2596  					s->def = N;
  2597  					continue;
  2598  				}
  2599  			}
  2600  		}
  2601  	}
  2602  
  2603  	if(outfile == nil) {
  2604  		p = strrchr(infile, '/');
  2605  		if(ctxt->windows) {
  2606  			q = strrchr(infile, '\\');
  2607  			if(q > p)
  2608  				p = q;
  2609  		}
  2610  		if(p == nil)
  2611  			p = infile;
  2612  		else
  2613  			p = p+1;
  2614  		snprint(namebuf, sizeof(namebuf), "%s", p);
  2615  		p = strrchr(namebuf, '.');
  2616  		if(p != nil)
  2617  			*p = 0;
  2618  		outfile = smprint("%s.%c", namebuf, thechar);
  2619  	}
  2620  }