github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/cmd/gc/lex.c (about)

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