github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/go/internal/gccgoimporter/parser.go (about)

     1  // Copyright 2013 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  package gccgoimporter
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"go/constant"
    12  	"go/token"
    13  	"go/types"
    14  	"io"
    15  	"strconv"
    16  	"strings"
    17  	"text/scanner"
    18  	"unicode/utf8"
    19  )
    20  
    21  type parser struct {
    22  	scanner  *scanner.Scanner
    23  	version  string                    // format version
    24  	tok      rune                      // current token
    25  	lit      string                    // literal string; only valid for Ident, Int, String tokens
    26  	pkgpath  string                    // package path of imported package
    27  	pkgname  string                    // name of imported package
    28  	pkg      *types.Package            // reference to imported package
    29  	imports  map[string]*types.Package // package path -> package object
    30  	typeList []types.Type              // type number -> type
    31  	typeData []string                  // unparsed type data (v3 and later)
    32  	fixups   []fixupRecord             // fixups to apply at end of parsing
    33  	initdata InitData                  // package init priority data
    34  }
    35  
    36  // When reading export data it's possible to encounter a defined type
    37  // N1 with an underlying defined type N2 while we are still reading in
    38  // that defined type N2; see issues #29006 and #29198 for instances
    39  // of this. Example:
    40  //
    41  //   type N1 N2
    42  //   type N2 struct {
    43  //      ...
    44  //      p *N1
    45  //   }
    46  //
    47  // To handle such cases, the parser generates a fixup record (below) and
    48  // delays setting of N1's underlying type until parsing is complete, at
    49  // which point fixups are applied.
    50  
    51  type fixupRecord struct {
    52  	toUpdate *types.Named // type to modify when fixup is processed
    53  	target   types.Type   // type that was incomplete when fixup was created
    54  }
    55  
    56  func (p *parser) init(filename string, src io.Reader, imports map[string]*types.Package) {
    57  	p.scanner = new(scanner.Scanner)
    58  	p.initScanner(filename, src)
    59  	p.imports = imports
    60  	p.typeList = make([]types.Type, 1 /* type numbers start at 1 */, 16)
    61  }
    62  
    63  func (p *parser) initScanner(filename string, src io.Reader) {
    64  	p.scanner.Init(src)
    65  	p.scanner.Error = func(_ *scanner.Scanner, msg string) { p.error(msg) }
    66  	p.scanner.Mode = scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings
    67  	p.scanner.Whitespace = 1<<'\t' | 1<<' '
    68  	p.scanner.Filename = filename // for good error messages
    69  	p.next()
    70  }
    71  
    72  type importError struct {
    73  	pos scanner.Position
    74  	err error
    75  }
    76  
    77  func (e importError) Error() string {
    78  	return fmt.Sprintf("import error %s (byte offset = %d): %s", e.pos, e.pos.Offset, e.err)
    79  }
    80  
    81  func (p *parser) error(err interface{}) {
    82  	if s, ok := err.(string); ok {
    83  		err = errors.New(s)
    84  	}
    85  	// panic with a runtime.Error if err is not an error
    86  	panic(importError{p.scanner.Pos(), err.(error)})
    87  }
    88  
    89  func (p *parser) errorf(format string, args ...interface{}) {
    90  	p.error(fmt.Errorf(format, args...))
    91  }
    92  
    93  func (p *parser) expect(tok rune) string {
    94  	lit := p.lit
    95  	if p.tok != tok {
    96  		p.errorf("expected %s, got %s (%s)", scanner.TokenString(tok), scanner.TokenString(p.tok), lit)
    97  	}
    98  	p.next()
    99  	return lit
   100  }
   101  
   102  func (p *parser) expectEOL() {
   103  	if p.version == "v1" || p.version == "v2" {
   104  		p.expect(';')
   105  	}
   106  	p.expect('\n')
   107  }
   108  
   109  func (p *parser) expectKeyword(keyword string) {
   110  	lit := p.expect(scanner.Ident)
   111  	if lit != keyword {
   112  		p.errorf("expected keyword %s, got %q", keyword, lit)
   113  	}
   114  }
   115  
   116  func (p *parser) parseString() string {
   117  	str, err := strconv.Unquote(p.expect(scanner.String))
   118  	if err != nil {
   119  		p.error(err)
   120  	}
   121  	return str
   122  }
   123  
   124  // unquotedString     = { unquotedStringChar } .
   125  // unquotedStringChar = <neither a whitespace nor a ';' char> .
   126  func (p *parser) parseUnquotedString() string {
   127  	if p.tok == scanner.EOF {
   128  		p.error("unexpected EOF")
   129  	}
   130  	var buf bytes.Buffer
   131  	buf.WriteString(p.scanner.TokenText())
   132  	// This loop needs to examine each character before deciding whether to consume it. If we see a semicolon,
   133  	// we need to let it be consumed by p.next().
   134  	for ch := p.scanner.Peek(); ch != '\n' && ch != ';' && ch != scanner.EOF && p.scanner.Whitespace&(1<<uint(ch)) == 0; ch = p.scanner.Peek() {
   135  		buf.WriteRune(ch)
   136  		p.scanner.Next()
   137  	}
   138  	p.next()
   139  	return buf.String()
   140  }
   141  
   142  func (p *parser) next() {
   143  	p.tok = p.scanner.Scan()
   144  	switch p.tok {
   145  	case scanner.Ident, scanner.Int, scanner.Float, scanner.String, '·':
   146  		p.lit = p.scanner.TokenText()
   147  	default:
   148  		p.lit = ""
   149  	}
   150  }
   151  
   152  func (p *parser) parseQualifiedName() (path, name string) {
   153  	return p.parseQualifiedNameStr(p.parseString())
   154  }
   155  
   156  func (p *parser) parseUnquotedQualifiedName() (path, name string) {
   157  	return p.parseQualifiedNameStr(p.parseUnquotedString())
   158  }
   159  
   160  // qualifiedName = [ ["."] unquotedString "." ] unquotedString .
   161  //
   162  // The above production uses greedy matching.
   163  func (p *parser) parseQualifiedNameStr(unquotedName string) (pkgpath, name string) {
   164  	parts := strings.Split(unquotedName, ".")
   165  	if parts[0] == "" {
   166  		parts = parts[1:]
   167  	}
   168  
   169  	switch len(parts) {
   170  	case 0:
   171  		p.errorf("malformed qualified name: %q", unquotedName)
   172  	case 1:
   173  		// unqualified name
   174  		pkgpath = p.pkgpath
   175  		name = parts[0]
   176  	default:
   177  		// qualified name, which may contain periods
   178  		pkgpath = strings.Join(parts[0:len(parts)-1], ".")
   179  		name = parts[len(parts)-1]
   180  	}
   181  
   182  	return
   183  }
   184  
   185  // getPkg returns the package for a given path. If the package is
   186  // not found but we have a package name, create the package and
   187  // add it to the p.imports map.
   188  //
   189  func (p *parser) getPkg(pkgpath, name string) *types.Package {
   190  	// package unsafe is not in the imports map - handle explicitly
   191  	if pkgpath == "unsafe" {
   192  		return types.Unsafe
   193  	}
   194  	pkg := p.imports[pkgpath]
   195  	if pkg == nil && name != "" {
   196  		pkg = types.NewPackage(pkgpath, name)
   197  		p.imports[pkgpath] = pkg
   198  	}
   199  	return pkg
   200  }
   201  
   202  // parseExportedName is like parseQualifiedName, but
   203  // the package path is resolved to an imported *types.Package.
   204  //
   205  // ExportedName = string [string] .
   206  func (p *parser) parseExportedName() (pkg *types.Package, name string) {
   207  	path, name := p.parseQualifiedName()
   208  	var pkgname string
   209  	if p.tok == scanner.String {
   210  		pkgname = p.parseString()
   211  	}
   212  	pkg = p.getPkg(path, pkgname)
   213  	if pkg == nil {
   214  		p.errorf("package %s (path = %q) not found", name, path)
   215  	}
   216  	return
   217  }
   218  
   219  // Name = QualifiedName | "?" .
   220  func (p *parser) parseName() string {
   221  	if p.tok == '?' {
   222  		// Anonymous.
   223  		p.next()
   224  		return ""
   225  	}
   226  	// The package path is redundant for us. Don't try to parse it.
   227  	_, name := p.parseUnquotedQualifiedName()
   228  	return name
   229  }
   230  
   231  func deref(typ types.Type) types.Type {
   232  	if p, _ := typ.(*types.Pointer); p != nil {
   233  		typ = p.Elem()
   234  	}
   235  	return typ
   236  }
   237  
   238  // Field = Name Type [string] .
   239  func (p *parser) parseField(pkg *types.Package) (field *types.Var, tag string) {
   240  	name := p.parseName()
   241  	typ := p.parseType(pkg)
   242  	anon := false
   243  	if name == "" {
   244  		anon = true
   245  		switch typ := deref(typ).(type) {
   246  		case *types.Basic:
   247  			name = typ.Name()
   248  		case *types.Named:
   249  			name = typ.Obj().Name()
   250  		default:
   251  			p.error("anonymous field expected")
   252  		}
   253  	}
   254  	field = types.NewField(token.NoPos, pkg, name, typ, anon)
   255  	if p.tok == scanner.String {
   256  		tag = p.parseString()
   257  	}
   258  	return
   259  }
   260  
   261  // Param = Name ["..."] Type .
   262  func (p *parser) parseParam(pkg *types.Package) (param *types.Var, isVariadic bool) {
   263  	name := p.parseName()
   264  	if p.tok == '<' && p.scanner.Peek() == 'e' {
   265  		// EscInfo = "<esc:" int ">" . (optional and ignored)
   266  		p.next()
   267  		p.expectKeyword("esc")
   268  		p.expect(':')
   269  		p.expect(scanner.Int)
   270  		p.expect('>')
   271  	}
   272  	if p.tok == '.' {
   273  		p.next()
   274  		p.expect('.')
   275  		p.expect('.')
   276  		isVariadic = true
   277  	}
   278  	typ := p.parseType(pkg)
   279  	if isVariadic {
   280  		typ = types.NewSlice(typ)
   281  	}
   282  	param = types.NewParam(token.NoPos, pkg, name, typ)
   283  	return
   284  }
   285  
   286  // Var = Name Type .
   287  func (p *parser) parseVar(pkg *types.Package) *types.Var {
   288  	name := p.parseName()
   289  	return types.NewVar(token.NoPos, pkg, name, p.parseType(pkg))
   290  }
   291  
   292  // Conversion = "convert" "(" Type "," ConstValue ")" .
   293  func (p *parser) parseConversion(pkg *types.Package) (val constant.Value, typ types.Type) {
   294  	p.expectKeyword("convert")
   295  	p.expect('(')
   296  	typ = p.parseType(pkg)
   297  	p.expect(',')
   298  	val, _ = p.parseConstValue(pkg)
   299  	p.expect(')')
   300  	return
   301  }
   302  
   303  // ConstValue     = string | "false" | "true" | ["-"] (int ["'"] | FloatOrComplex) | Conversion .
   304  // FloatOrComplex = float ["i" | ("+"|"-") float "i"] .
   305  func (p *parser) parseConstValue(pkg *types.Package) (val constant.Value, typ types.Type) {
   306  	// v3 changed to $false, $true, $convert, to avoid confusion
   307  	// with variable names in inline function bodies.
   308  	if p.tok == '$' {
   309  		p.next()
   310  		if p.tok != scanner.Ident {
   311  			p.errorf("expected identifer after '$', got %s (%q)", scanner.TokenString(p.tok), p.lit)
   312  		}
   313  	}
   314  
   315  	switch p.tok {
   316  	case scanner.String:
   317  		str := p.parseString()
   318  		val = constant.MakeString(str)
   319  		typ = types.Typ[types.UntypedString]
   320  		return
   321  
   322  	case scanner.Ident:
   323  		b := false
   324  		switch p.lit {
   325  		case "false":
   326  		case "true":
   327  			b = true
   328  
   329  		case "convert":
   330  			return p.parseConversion(pkg)
   331  
   332  		default:
   333  			p.errorf("expected const value, got %s (%q)", scanner.TokenString(p.tok), p.lit)
   334  		}
   335  
   336  		p.next()
   337  		val = constant.MakeBool(b)
   338  		typ = types.Typ[types.UntypedBool]
   339  		return
   340  	}
   341  
   342  	sign := ""
   343  	if p.tok == '-' {
   344  		p.next()
   345  		sign = "-"
   346  	}
   347  
   348  	switch p.tok {
   349  	case scanner.Int:
   350  		val = constant.MakeFromLiteral(sign+p.lit, token.INT, 0)
   351  		if val == nil {
   352  			p.error("could not parse integer literal")
   353  		}
   354  
   355  		p.next()
   356  		if p.tok == '\'' {
   357  			p.next()
   358  			typ = types.Typ[types.UntypedRune]
   359  		} else {
   360  			typ = types.Typ[types.UntypedInt]
   361  		}
   362  
   363  	case scanner.Float:
   364  		re := sign + p.lit
   365  		p.next()
   366  
   367  		var im string
   368  		switch p.tok {
   369  		case '+':
   370  			p.next()
   371  			im = p.expect(scanner.Float)
   372  
   373  		case '-':
   374  			p.next()
   375  			im = "-" + p.expect(scanner.Float)
   376  
   377  		case scanner.Ident:
   378  			// re is in fact the imaginary component. Expect "i" below.
   379  			im = re
   380  			re = "0"
   381  
   382  		default:
   383  			val = constant.MakeFromLiteral(re, token.FLOAT, 0)
   384  			if val == nil {
   385  				p.error("could not parse float literal")
   386  			}
   387  			typ = types.Typ[types.UntypedFloat]
   388  			return
   389  		}
   390  
   391  		p.expectKeyword("i")
   392  		reval := constant.MakeFromLiteral(re, token.FLOAT, 0)
   393  		if reval == nil {
   394  			p.error("could not parse real component of complex literal")
   395  		}
   396  		imval := constant.MakeFromLiteral(im+"i", token.IMAG, 0)
   397  		if imval == nil {
   398  			p.error("could not parse imag component of complex literal")
   399  		}
   400  		val = constant.BinaryOp(reval, token.ADD, imval)
   401  		typ = types.Typ[types.UntypedComplex]
   402  
   403  	default:
   404  		p.errorf("expected const value, got %s (%q)", scanner.TokenString(p.tok), p.lit)
   405  	}
   406  
   407  	return
   408  }
   409  
   410  // Const = Name [Type] "=" ConstValue .
   411  func (p *parser) parseConst(pkg *types.Package) *types.Const {
   412  	name := p.parseName()
   413  	var typ types.Type
   414  	if p.tok == '<' {
   415  		typ = p.parseType(pkg)
   416  	}
   417  	p.expect('=')
   418  	val, vtyp := p.parseConstValue(pkg)
   419  	if typ == nil {
   420  		typ = vtyp
   421  	}
   422  	return types.NewConst(token.NoPos, pkg, name, typ, val)
   423  }
   424  
   425  // reserved is a singleton type used to fill type map slots that have
   426  // been reserved (i.e., for which a type number has been parsed) but
   427  // which don't have their actual type yet. When the type map is updated,
   428  // the actual type must replace a reserved entry (or we have an internal
   429  // error). Used for self-verification only - not required for correctness.
   430  var reserved = new(struct{ types.Type })
   431  
   432  // reserve reserves the type map entry n for future use.
   433  func (p *parser) reserve(n int) {
   434  	// Notes:
   435  	// - for pre-V3 export data, the type numbers we see are
   436  	//   guaranteed to be in increasing order, so we append a
   437  	//   reserved entry onto the list.
   438  	// - for V3+ export data, type numbers can appear in
   439  	//   any order, however the 'types' section tells us the
   440  	//   total number of types, hence typeList is pre-allocated.
   441  	if len(p.typeData) == 0 {
   442  		if n != len(p.typeList) {
   443  			p.errorf("invalid type number %d (out of sync)", n)
   444  		}
   445  		p.typeList = append(p.typeList, reserved)
   446  	} else {
   447  		if p.typeList[n] != nil {
   448  			p.errorf("previously visited type number %d", n)
   449  		}
   450  		p.typeList[n] = reserved
   451  	}
   452  }
   453  
   454  // update sets the type map entries for the given type numbers nlist to t.
   455  func (p *parser) update(t types.Type, nlist []int) {
   456  	if len(nlist) != 0 {
   457  		if t == reserved {
   458  			p.errorf("internal error: update(%v) invoked on reserved", nlist)
   459  		}
   460  		if t == nil {
   461  			p.errorf("internal error: update(%v) invoked on nil", nlist)
   462  		}
   463  	}
   464  	for _, n := range nlist {
   465  		if p.typeList[n] == t {
   466  			continue
   467  		}
   468  		if p.typeList[n] != reserved {
   469  			p.errorf("internal error: update(%v): %d not reserved", nlist, n)
   470  		}
   471  		p.typeList[n] = t
   472  	}
   473  }
   474  
   475  // NamedType = TypeName [ "=" ] Type { Method } .
   476  // TypeName  = ExportedName .
   477  // Method    = "func" "(" Param ")" Name ParamList ResultList [InlineBody] ";" .
   478  func (p *parser) parseNamedType(nlist []int) types.Type {
   479  	pkg, name := p.parseExportedName()
   480  	scope := pkg.Scope()
   481  	obj := scope.Lookup(name)
   482  	if obj != nil && obj.Type() == nil {
   483  		p.errorf("%v has nil type", obj)
   484  	}
   485  
   486  	// type alias
   487  	if p.tok == '=' {
   488  		p.next()
   489  		if obj != nil {
   490  			// use the previously imported (canonical) type
   491  			t := obj.Type()
   492  			p.update(t, nlist)
   493  			p.parseType(pkg) // discard
   494  			return t
   495  		}
   496  		t := p.parseType(pkg, nlist...)
   497  		obj = types.NewTypeName(token.NoPos, pkg, name, t)
   498  		scope.Insert(obj)
   499  		return t
   500  	}
   501  
   502  	// defined type
   503  	if obj == nil {
   504  		// A named type may be referred to before the underlying type
   505  		// is known - set it up.
   506  		tname := types.NewTypeName(token.NoPos, pkg, name, nil)
   507  		types.NewNamed(tname, nil, nil)
   508  		scope.Insert(tname)
   509  		obj = tname
   510  	}
   511  
   512  	// use the previously imported (canonical), or newly created type
   513  	t := obj.Type()
   514  	p.update(t, nlist)
   515  
   516  	nt, ok := t.(*types.Named)
   517  	if !ok {
   518  		// This can happen for unsafe.Pointer, which is a TypeName holding a Basic type.
   519  		pt := p.parseType(pkg)
   520  		if pt != t {
   521  			p.error("unexpected underlying type for non-named TypeName")
   522  		}
   523  		return t
   524  	}
   525  
   526  	underlying := p.parseType(pkg)
   527  	if nt.Underlying() == nil {
   528  		if underlying.Underlying() == nil {
   529  			fix := fixupRecord{toUpdate: nt, target: underlying}
   530  			p.fixups = append(p.fixups, fix)
   531  		} else {
   532  			nt.SetUnderlying(underlying.Underlying())
   533  		}
   534  	}
   535  
   536  	if p.tok == '\n' {
   537  		p.next()
   538  		// collect associated methods
   539  		for p.tok == scanner.Ident {
   540  			p.expectKeyword("func")
   541  			if p.tok == '/' {
   542  				// Skip a /*nointerface*/ comment.
   543  				p.expect('/')
   544  				p.expect('*')
   545  				p.expect(scanner.Ident)
   546  				p.expect('*')
   547  				p.expect('/')
   548  			}
   549  			p.expect('(')
   550  			receiver, _ := p.parseParam(pkg)
   551  			p.expect(')')
   552  			name := p.parseName()
   553  			params, isVariadic := p.parseParamList(pkg)
   554  			results := p.parseResultList(pkg)
   555  			p.skipInlineBody()
   556  			p.expectEOL()
   557  
   558  			sig := types.NewSignature(receiver, params, results, isVariadic)
   559  			nt.AddMethod(types.NewFunc(token.NoPos, pkg, name, sig))
   560  		}
   561  	}
   562  
   563  	return nt
   564  }
   565  
   566  func (p *parser) parseInt64() int64 {
   567  	lit := p.expect(scanner.Int)
   568  	n, err := strconv.ParseInt(lit, 10, 64)
   569  	if err != nil {
   570  		p.error(err)
   571  	}
   572  	return n
   573  }
   574  
   575  func (p *parser) parseInt() int {
   576  	lit := p.expect(scanner.Int)
   577  	n, err := strconv.ParseInt(lit, 10, 0 /* int */)
   578  	if err != nil {
   579  		p.error(err)
   580  	}
   581  	return int(n)
   582  }
   583  
   584  // ArrayOrSliceType = "[" [ int ] "]" Type .
   585  func (p *parser) parseArrayOrSliceType(pkg *types.Package, nlist []int) types.Type {
   586  	p.expect('[')
   587  	if p.tok == ']' {
   588  		p.next()
   589  
   590  		t := new(types.Slice)
   591  		p.update(t, nlist)
   592  
   593  		*t = *types.NewSlice(p.parseType(pkg))
   594  		return t
   595  	}
   596  
   597  	t := new(types.Array)
   598  	p.update(t, nlist)
   599  
   600  	len := p.parseInt64()
   601  	p.expect(']')
   602  
   603  	*t = *types.NewArray(p.parseType(pkg), len)
   604  	return t
   605  }
   606  
   607  // MapType = "map" "[" Type "]" Type .
   608  func (p *parser) parseMapType(pkg *types.Package, nlist []int) types.Type {
   609  	p.expectKeyword("map")
   610  
   611  	t := new(types.Map)
   612  	p.update(t, nlist)
   613  
   614  	p.expect('[')
   615  	key := p.parseType(pkg)
   616  	p.expect(']')
   617  	elem := p.parseType(pkg)
   618  
   619  	*t = *types.NewMap(key, elem)
   620  	return t
   621  }
   622  
   623  // ChanType = "chan" ["<-" | "-<"] Type .
   624  func (p *parser) parseChanType(pkg *types.Package, nlist []int) types.Type {
   625  	p.expectKeyword("chan")
   626  
   627  	t := new(types.Chan)
   628  	p.update(t, nlist)
   629  
   630  	dir := types.SendRecv
   631  	switch p.tok {
   632  	case '-':
   633  		p.next()
   634  		p.expect('<')
   635  		dir = types.SendOnly
   636  
   637  	case '<':
   638  		// don't consume '<' if it belongs to Type
   639  		if p.scanner.Peek() == '-' {
   640  			p.next()
   641  			p.expect('-')
   642  			dir = types.RecvOnly
   643  		}
   644  	}
   645  
   646  	*t = *types.NewChan(dir, p.parseType(pkg))
   647  	return t
   648  }
   649  
   650  // StructType = "struct" "{" { Field } "}" .
   651  func (p *parser) parseStructType(pkg *types.Package, nlist []int) types.Type {
   652  	p.expectKeyword("struct")
   653  
   654  	t := new(types.Struct)
   655  	p.update(t, nlist)
   656  
   657  	var fields []*types.Var
   658  	var tags []string
   659  
   660  	p.expect('{')
   661  	for p.tok != '}' && p.tok != scanner.EOF {
   662  		field, tag := p.parseField(pkg)
   663  		p.expect(';')
   664  		fields = append(fields, field)
   665  		tags = append(tags, tag)
   666  	}
   667  	p.expect('}')
   668  
   669  	*t = *types.NewStruct(fields, tags)
   670  	return t
   671  }
   672  
   673  // ParamList = "(" [ { Parameter "," } Parameter ] ")" .
   674  func (p *parser) parseParamList(pkg *types.Package) (*types.Tuple, bool) {
   675  	var list []*types.Var
   676  	isVariadic := false
   677  
   678  	p.expect('(')
   679  	for p.tok != ')' && p.tok != scanner.EOF {
   680  		if len(list) > 0 {
   681  			p.expect(',')
   682  		}
   683  		par, variadic := p.parseParam(pkg)
   684  		list = append(list, par)
   685  		if variadic {
   686  			if isVariadic {
   687  				p.error("... not on final argument")
   688  			}
   689  			isVariadic = true
   690  		}
   691  	}
   692  	p.expect(')')
   693  
   694  	return types.NewTuple(list...), isVariadic
   695  }
   696  
   697  // ResultList = Type | ParamList .
   698  func (p *parser) parseResultList(pkg *types.Package) *types.Tuple {
   699  	switch p.tok {
   700  	case '<':
   701  		p.next()
   702  		if p.tok == scanner.Ident && p.lit == "inl" {
   703  			return nil
   704  		}
   705  		return types.NewTuple(types.NewParam(token.NoPos, pkg, "", p.parseTypeAfterAngle(pkg)))
   706  
   707  	case '(':
   708  		params, _ := p.parseParamList(pkg)
   709  		return params
   710  
   711  	default:
   712  		return nil
   713  	}
   714  }
   715  
   716  // FunctionType = ParamList ResultList .
   717  func (p *parser) parseFunctionType(pkg *types.Package, nlist []int) *types.Signature {
   718  	t := new(types.Signature)
   719  	p.update(t, nlist)
   720  
   721  	params, isVariadic := p.parseParamList(pkg)
   722  	results := p.parseResultList(pkg)
   723  
   724  	*t = *types.NewSignature(nil, params, results, isVariadic)
   725  	return t
   726  }
   727  
   728  // Func = Name FunctionType [InlineBody] .
   729  func (p *parser) parseFunc(pkg *types.Package) *types.Func {
   730  	name := p.parseName()
   731  	if strings.ContainsRune(name, '$') {
   732  		// This is a Type$equal or Type$hash function, which we don't want to parse,
   733  		// except for the types.
   734  		p.discardDirectiveWhileParsingTypes(pkg)
   735  		return nil
   736  	}
   737  	f := types.NewFunc(token.NoPos, pkg, name, p.parseFunctionType(pkg, nil))
   738  	p.skipInlineBody()
   739  	return f
   740  }
   741  
   742  // InterfaceType = "interface" "{" { ("?" Type | Func) ";" } "}" .
   743  func (p *parser) parseInterfaceType(pkg *types.Package, nlist []int) types.Type {
   744  	p.expectKeyword("interface")
   745  
   746  	t := new(types.Interface)
   747  	p.update(t, nlist)
   748  
   749  	var methods []*types.Func
   750  	var embeddeds []types.Type
   751  
   752  	p.expect('{')
   753  	for p.tok != '}' && p.tok != scanner.EOF {
   754  		if p.tok == '?' {
   755  			p.next()
   756  			embeddeds = append(embeddeds, p.parseType(pkg))
   757  		} else {
   758  			method := p.parseFunc(pkg)
   759  			methods = append(methods, method)
   760  		}
   761  		p.expect(';')
   762  	}
   763  	p.expect('}')
   764  
   765  	*t = *types.NewInterfaceType(methods, embeddeds)
   766  	return t
   767  }
   768  
   769  // PointerType = "*" ("any" | Type) .
   770  func (p *parser) parsePointerType(pkg *types.Package, nlist []int) types.Type {
   771  	p.expect('*')
   772  	if p.tok == scanner.Ident {
   773  		p.expectKeyword("any")
   774  		t := types.Typ[types.UnsafePointer]
   775  		p.update(t, nlist)
   776  		return t
   777  	}
   778  
   779  	t := new(types.Pointer)
   780  	p.update(t, nlist)
   781  
   782  	*t = *types.NewPointer(p.parseType(pkg))
   783  
   784  	return t
   785  }
   786  
   787  // TypeSpec = NamedType | MapType | ChanType | StructType | InterfaceType | PointerType | ArrayOrSliceType | FunctionType .
   788  func (p *parser) parseTypeSpec(pkg *types.Package, nlist []int) types.Type {
   789  	switch p.tok {
   790  	case scanner.String:
   791  		return p.parseNamedType(nlist)
   792  
   793  	case scanner.Ident:
   794  		switch p.lit {
   795  		case "map":
   796  			return p.parseMapType(pkg, nlist)
   797  
   798  		case "chan":
   799  			return p.parseChanType(pkg, nlist)
   800  
   801  		case "struct":
   802  			return p.parseStructType(pkg, nlist)
   803  
   804  		case "interface":
   805  			return p.parseInterfaceType(pkg, nlist)
   806  		}
   807  
   808  	case '*':
   809  		return p.parsePointerType(pkg, nlist)
   810  
   811  	case '[':
   812  		return p.parseArrayOrSliceType(pkg, nlist)
   813  
   814  	case '(':
   815  		return p.parseFunctionType(pkg, nlist)
   816  	}
   817  
   818  	p.errorf("expected type name or literal, got %s", scanner.TokenString(p.tok))
   819  	return nil
   820  }
   821  
   822  const (
   823  	// From gofrontend/go/export.h
   824  	// Note that these values are negative in the gofrontend and have been made positive
   825  	// in the gccgoimporter.
   826  	gccgoBuiltinINT8       = 1
   827  	gccgoBuiltinINT16      = 2
   828  	gccgoBuiltinINT32      = 3
   829  	gccgoBuiltinINT64      = 4
   830  	gccgoBuiltinUINT8      = 5
   831  	gccgoBuiltinUINT16     = 6
   832  	gccgoBuiltinUINT32     = 7
   833  	gccgoBuiltinUINT64     = 8
   834  	gccgoBuiltinFLOAT32    = 9
   835  	gccgoBuiltinFLOAT64    = 10
   836  	gccgoBuiltinINT        = 11
   837  	gccgoBuiltinUINT       = 12
   838  	gccgoBuiltinUINTPTR    = 13
   839  	gccgoBuiltinBOOL       = 15
   840  	gccgoBuiltinSTRING     = 16
   841  	gccgoBuiltinCOMPLEX64  = 17
   842  	gccgoBuiltinCOMPLEX128 = 18
   843  	gccgoBuiltinERROR      = 19
   844  	gccgoBuiltinBYTE       = 20
   845  	gccgoBuiltinRUNE       = 21
   846  )
   847  
   848  func lookupBuiltinType(typ int) types.Type {
   849  	return [...]types.Type{
   850  		gccgoBuiltinINT8:       types.Typ[types.Int8],
   851  		gccgoBuiltinINT16:      types.Typ[types.Int16],
   852  		gccgoBuiltinINT32:      types.Typ[types.Int32],
   853  		gccgoBuiltinINT64:      types.Typ[types.Int64],
   854  		gccgoBuiltinUINT8:      types.Typ[types.Uint8],
   855  		gccgoBuiltinUINT16:     types.Typ[types.Uint16],
   856  		gccgoBuiltinUINT32:     types.Typ[types.Uint32],
   857  		gccgoBuiltinUINT64:     types.Typ[types.Uint64],
   858  		gccgoBuiltinFLOAT32:    types.Typ[types.Float32],
   859  		gccgoBuiltinFLOAT64:    types.Typ[types.Float64],
   860  		gccgoBuiltinINT:        types.Typ[types.Int],
   861  		gccgoBuiltinUINT:       types.Typ[types.Uint],
   862  		gccgoBuiltinUINTPTR:    types.Typ[types.Uintptr],
   863  		gccgoBuiltinBOOL:       types.Typ[types.Bool],
   864  		gccgoBuiltinSTRING:     types.Typ[types.String],
   865  		gccgoBuiltinCOMPLEX64:  types.Typ[types.Complex64],
   866  		gccgoBuiltinCOMPLEX128: types.Typ[types.Complex128],
   867  		gccgoBuiltinERROR:      types.Universe.Lookup("error").Type(),
   868  		gccgoBuiltinBYTE:       types.Universe.Lookup("byte").Type(),
   869  		gccgoBuiltinRUNE:       types.Universe.Lookup("rune").Type(),
   870  	}[typ]
   871  }
   872  
   873  // Type = "<" "type" ( "-" int | int [ TypeSpec ] ) ">" .
   874  //
   875  // parseType updates the type map to t for all type numbers n.
   876  //
   877  func (p *parser) parseType(pkg *types.Package, n ...int) types.Type {
   878  	p.expect('<')
   879  	return p.parseTypeAfterAngle(pkg, n...)
   880  }
   881  
   882  // (*parser).Type after reading the "<".
   883  func (p *parser) parseTypeAfterAngle(pkg *types.Package, n ...int) (t types.Type) {
   884  	p.expectKeyword("type")
   885  
   886  	switch p.tok {
   887  	case scanner.Int:
   888  		n1 := p.parseInt()
   889  		if p.tok == '>' {
   890  			if len(p.typeData) > 0 && p.typeList[n1] == nil {
   891  				p.parseSavedType(pkg, n1, n)
   892  			}
   893  			t = p.typeList[n1]
   894  			if len(p.typeData) == 0 && t == reserved {
   895  				p.errorf("invalid type cycle, type %d not yet defined (nlist=%v)", n1, n)
   896  			}
   897  			p.update(t, n)
   898  		} else {
   899  			p.reserve(n1)
   900  			t = p.parseTypeSpec(pkg, append(n, n1))
   901  		}
   902  
   903  	case '-':
   904  		p.next()
   905  		n1 := p.parseInt()
   906  		t = lookupBuiltinType(n1)
   907  		p.update(t, n)
   908  
   909  	default:
   910  		p.errorf("expected type number, got %s (%q)", scanner.TokenString(p.tok), p.lit)
   911  		return nil
   912  	}
   913  
   914  	if t == nil || t == reserved {
   915  		p.errorf("internal error: bad return from parseType(%v)", n)
   916  	}
   917  
   918  	p.expect('>')
   919  	return
   920  }
   921  
   922  // InlineBody = "<inl:NN>" .{NN}
   923  // Reports whether a body was skipped.
   924  func (p *parser) skipInlineBody() {
   925  	// We may or may not have seen the '<' already, depending on
   926  	// whether the function had a result type or not.
   927  	if p.tok == '<' {
   928  		p.next()
   929  		p.expectKeyword("inl")
   930  	} else if p.tok != scanner.Ident || p.lit != "inl" {
   931  		return
   932  	} else {
   933  		p.next()
   934  	}
   935  
   936  	p.expect(':')
   937  	want := p.parseInt()
   938  	p.expect('>')
   939  
   940  	defer func(w uint64) {
   941  		p.scanner.Whitespace = w
   942  	}(p.scanner.Whitespace)
   943  	p.scanner.Whitespace = 0
   944  
   945  	got := 0
   946  	for got < want {
   947  		r := p.scanner.Next()
   948  		if r == scanner.EOF {
   949  			p.error("unexpected EOF")
   950  		}
   951  		got += utf8.RuneLen(r)
   952  	}
   953  }
   954  
   955  // Types = "types" maxp1 exportedp1 (offset length)* .
   956  func (p *parser) parseTypes(pkg *types.Package) {
   957  	maxp1 := p.parseInt()
   958  	exportedp1 := p.parseInt()
   959  	p.typeList = make([]types.Type, maxp1, maxp1)
   960  
   961  	type typeOffset struct {
   962  		offset int
   963  		length int
   964  	}
   965  	var typeOffsets []typeOffset
   966  
   967  	total := 0
   968  	for i := 1; i < maxp1; i++ {
   969  		len := p.parseInt()
   970  		typeOffsets = append(typeOffsets, typeOffset{total, len})
   971  		total += len
   972  	}
   973  
   974  	defer func(w uint64) {
   975  		p.scanner.Whitespace = w
   976  	}(p.scanner.Whitespace)
   977  	p.scanner.Whitespace = 0
   978  
   979  	// We should now have p.tok pointing to the final newline.
   980  	// The next runes from the scanner should be the type data.
   981  
   982  	var sb strings.Builder
   983  	for sb.Len() < total {
   984  		r := p.scanner.Next()
   985  		if r == scanner.EOF {
   986  			p.error("unexpected EOF")
   987  		}
   988  		sb.WriteRune(r)
   989  	}
   990  	allTypeData := sb.String()
   991  
   992  	p.typeData = []string{""} // type 0, unused
   993  	for _, to := range typeOffsets {
   994  		p.typeData = append(p.typeData, allTypeData[to.offset:to.offset+to.length])
   995  	}
   996  
   997  	for i := 1; i < int(exportedp1); i++ {
   998  		p.parseSavedType(pkg, i, []int{})
   999  	}
  1000  }
  1001  
  1002  // parseSavedType parses one saved type definition.
  1003  func (p *parser) parseSavedType(pkg *types.Package, i int, nlist []int) {
  1004  	defer func(s *scanner.Scanner, tok rune, lit string) {
  1005  		p.scanner = s
  1006  		p.tok = tok
  1007  		p.lit = lit
  1008  	}(p.scanner, p.tok, p.lit)
  1009  
  1010  	p.scanner = new(scanner.Scanner)
  1011  	p.initScanner(p.scanner.Filename, strings.NewReader(p.typeData[i]))
  1012  	p.expectKeyword("type")
  1013  	id := p.parseInt()
  1014  	if id != i {
  1015  		p.errorf("type ID mismatch: got %d, want %d", id, i)
  1016  	}
  1017  	if p.typeList[i] == reserved {
  1018  		p.errorf("internal error: %d already reserved in parseSavedType", i)
  1019  	}
  1020  	if p.typeList[i] == nil {
  1021  		p.reserve(i)
  1022  		p.parseTypeSpec(pkg, append(nlist, i))
  1023  	}
  1024  	if p.typeList[i] == nil || p.typeList[i] == reserved {
  1025  		p.errorf("internal error: parseSavedType(%d,%v) reserved/nil", i, nlist)
  1026  	}
  1027  }
  1028  
  1029  // PackageInit = unquotedString unquotedString int .
  1030  func (p *parser) parsePackageInit() PackageInit {
  1031  	name := p.parseUnquotedString()
  1032  	initfunc := p.parseUnquotedString()
  1033  	priority := -1
  1034  	if p.version == "v1" {
  1035  		priority = p.parseInt()
  1036  	}
  1037  	return PackageInit{Name: name, InitFunc: initfunc, Priority: priority}
  1038  }
  1039  
  1040  // Throw away tokens until we see a ';'. If we see a '<', attempt to parse as a type.
  1041  func (p *parser) discardDirectiveWhileParsingTypes(pkg *types.Package) {
  1042  	for {
  1043  		switch p.tok {
  1044  		case '\n', ';':
  1045  			return
  1046  		case '<':
  1047  			p.parseType(pkg)
  1048  		case scanner.EOF:
  1049  			p.error("unexpected EOF")
  1050  		default:
  1051  			p.next()
  1052  		}
  1053  	}
  1054  }
  1055  
  1056  // Create the package if we have parsed both the package path and package name.
  1057  func (p *parser) maybeCreatePackage() {
  1058  	if p.pkgname != "" && p.pkgpath != "" {
  1059  		p.pkg = p.getPkg(p.pkgpath, p.pkgname)
  1060  	}
  1061  }
  1062  
  1063  // InitDataDirective = ( "v1" | "v2" | "v3" ) ";" |
  1064  //                     "priority" int ";" |
  1065  //                     "init" { PackageInit } ";" |
  1066  //                     "checksum" unquotedString ";" .
  1067  func (p *parser) parseInitDataDirective() {
  1068  	if p.tok != scanner.Ident {
  1069  		// unexpected token kind; panic
  1070  		p.expect(scanner.Ident)
  1071  	}
  1072  
  1073  	switch p.lit {
  1074  	case "v1", "v2", "v3":
  1075  		p.version = p.lit
  1076  		p.next()
  1077  		p.expect(';')
  1078  		p.expect('\n')
  1079  
  1080  	case "priority":
  1081  		p.next()
  1082  		p.initdata.Priority = p.parseInt()
  1083  		p.expectEOL()
  1084  
  1085  	case "init":
  1086  		p.next()
  1087  		for p.tok != '\n' && p.tok != ';' && p.tok != scanner.EOF {
  1088  			p.initdata.Inits = append(p.initdata.Inits, p.parsePackageInit())
  1089  		}
  1090  		p.expectEOL()
  1091  
  1092  	case "init_graph":
  1093  		p.next()
  1094  		// The graph data is thrown away for now.
  1095  		for p.tok != '\n' && p.tok != ';' && p.tok != scanner.EOF {
  1096  			p.parseInt64()
  1097  			p.parseInt64()
  1098  		}
  1099  		p.expectEOL()
  1100  
  1101  	case "checksum":
  1102  		// Don't let the scanner try to parse the checksum as a number.
  1103  		defer func(mode uint) {
  1104  			p.scanner.Mode = mode
  1105  		}(p.scanner.Mode)
  1106  		p.scanner.Mode &^= scanner.ScanInts | scanner.ScanFloats
  1107  		p.next()
  1108  		p.parseUnquotedString()
  1109  		p.expectEOL()
  1110  
  1111  	default:
  1112  		p.errorf("unexpected identifier: %q", p.lit)
  1113  	}
  1114  }
  1115  
  1116  // Directive = InitDataDirective |
  1117  //             "package" unquotedString [ unquotedString ] [ unquotedString ] ";" |
  1118  //             "pkgpath" unquotedString ";" |
  1119  //             "prefix" unquotedString ";" |
  1120  //             "import" unquotedString unquotedString string ";" |
  1121  //             "indirectimport" unquotedString unquotedstring ";" |
  1122  //             "func" Func ";" |
  1123  //             "type" Type ";" |
  1124  //             "var" Var ";" |
  1125  //             "const" Const ";" .
  1126  func (p *parser) parseDirective() {
  1127  	if p.tok != scanner.Ident {
  1128  		// unexpected token kind; panic
  1129  		p.expect(scanner.Ident)
  1130  	}
  1131  
  1132  	switch p.lit {
  1133  	case "v1", "v2", "v3", "priority", "init", "init_graph", "checksum":
  1134  		p.parseInitDataDirective()
  1135  
  1136  	case "package":
  1137  		p.next()
  1138  		p.pkgname = p.parseUnquotedString()
  1139  		p.maybeCreatePackage()
  1140  		if p.version != "v1" && p.tok != '\n' && p.tok != ';' {
  1141  			p.parseUnquotedString()
  1142  			p.parseUnquotedString()
  1143  		}
  1144  		p.expectEOL()
  1145  
  1146  	case "pkgpath":
  1147  		p.next()
  1148  		p.pkgpath = p.parseUnquotedString()
  1149  		p.maybeCreatePackage()
  1150  		p.expectEOL()
  1151  
  1152  	case "prefix":
  1153  		p.next()
  1154  		p.pkgpath = p.parseUnquotedString()
  1155  		p.expectEOL()
  1156  
  1157  	case "import":
  1158  		p.next()
  1159  		pkgname := p.parseUnquotedString()
  1160  		pkgpath := p.parseUnquotedString()
  1161  		p.getPkg(pkgpath, pkgname)
  1162  		p.parseString()
  1163  		p.expectEOL()
  1164  
  1165  	case "indirectimport":
  1166  		p.next()
  1167  		pkgname := p.parseUnquotedString()
  1168  		pkgpath := p.parseUnquotedString()
  1169  		p.getPkg(pkgpath, pkgname)
  1170  		p.expectEOL()
  1171  
  1172  	case "types":
  1173  		p.next()
  1174  		p.parseTypes(p.pkg)
  1175  		p.expectEOL()
  1176  
  1177  	case "func":
  1178  		p.next()
  1179  		fun := p.parseFunc(p.pkg)
  1180  		if fun != nil {
  1181  			p.pkg.Scope().Insert(fun)
  1182  		}
  1183  		p.expectEOL()
  1184  
  1185  	case "type":
  1186  		p.next()
  1187  		p.parseType(p.pkg)
  1188  		p.expectEOL()
  1189  
  1190  	case "var":
  1191  		p.next()
  1192  		v := p.parseVar(p.pkg)
  1193  		p.pkg.Scope().Insert(v)
  1194  		p.expectEOL()
  1195  
  1196  	case "const":
  1197  		p.next()
  1198  		c := p.parseConst(p.pkg)
  1199  		p.pkg.Scope().Insert(c)
  1200  		p.expectEOL()
  1201  
  1202  	default:
  1203  		p.errorf("unexpected identifier: %q", p.lit)
  1204  	}
  1205  }
  1206  
  1207  // Package = { Directive } .
  1208  func (p *parser) parsePackage() *types.Package {
  1209  	for p.tok != scanner.EOF {
  1210  		p.parseDirective()
  1211  	}
  1212  	for _, f := range p.fixups {
  1213  		if f.target.Underlying() == nil {
  1214  			p.errorf("internal error: fixup can't be applied, loop required")
  1215  		}
  1216  		f.toUpdate.SetUnderlying(f.target.Underlying())
  1217  	}
  1218  	p.fixups = nil
  1219  	for _, typ := range p.typeList {
  1220  		if it, ok := typ.(*types.Interface); ok {
  1221  			it.Complete()
  1222  		}
  1223  	}
  1224  	p.pkg.MarkComplete()
  1225  	return p.pkg
  1226  }