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