github.com/FenixAra/go@v0.0.0-20170127160404-96ea0918e670/src/go/internal/gcimporter/bimport.go (about)

     1  // Copyright 2015 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 gcimporter
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  	"go/constant"
    11  	"go/token"
    12  	"go/types"
    13  	"sort"
    14  	"strconv"
    15  	"strings"
    16  	"sync"
    17  	"unicode"
    18  	"unicode/utf8"
    19  )
    20  
    21  type importer struct {
    22  	imports map[string]*types.Package
    23  	data    []byte
    24  	path    string
    25  	buf     []byte // for reading strings
    26  	version int    // export format version
    27  
    28  	// object lists
    29  	strList       []string         // in order of appearance
    30  	pkgList       []*types.Package // in order of appearance
    31  	typList       []types.Type     // in order of appearance
    32  	trackAllTypes bool
    33  
    34  	// position encoding
    35  	posInfoFormat bool
    36  	prevFile      string
    37  	prevLine      int
    38  	fset          *token.FileSet
    39  	files         map[string]*token.File
    40  
    41  	// debugging support
    42  	debugFormat bool
    43  	read        int // bytes read
    44  }
    45  
    46  // BImportData imports a package from the serialized package data
    47  // and returns the number of bytes consumed and a reference to the package.
    48  // If the export data version is not recognized or the format is otherwise
    49  // compromised, an error is returned.
    50  func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []byte, path string) (_ int, _ *types.Package, err error) {
    51  	// catch panics and return them as errors
    52  	defer func() {
    53  		if e := recover(); e != nil {
    54  			// The package (filename) causing the problem is added to this
    55  			// error by a wrapper in the caller (Import in gcimporter.go).
    56  			err = fmt.Errorf("cannot import, possibly version skew (%v) - reinstall package", e)
    57  		}
    58  	}()
    59  
    60  	p := importer{
    61  		imports: imports,
    62  		data:    data,
    63  		path:    path,
    64  		version: -1,           // unknown version
    65  		strList: []string{""}, // empty string is mapped to 0
    66  		fset:    fset,
    67  		files:   make(map[string]*token.File),
    68  	}
    69  
    70  	// read version info
    71  	var versionstr string
    72  	if b := p.rawByte(); b == 'c' || b == 'd' {
    73  		// Go1.7 encoding; first byte encodes low-level
    74  		// encoding format (compact vs debug).
    75  		// For backward-compatibility only (avoid problems with
    76  		// old installed packages). Newly compiled packages use
    77  		// the extensible format string.
    78  		// TODO(gri) Remove this support eventually; after Go1.8.
    79  		if b == 'd' {
    80  			p.debugFormat = true
    81  		}
    82  		p.trackAllTypes = p.rawByte() == 'a'
    83  		p.posInfoFormat = p.int() != 0
    84  		versionstr = p.string()
    85  		if versionstr == "v1" {
    86  			p.version = 0
    87  		}
    88  	} else {
    89  		// Go1.8 extensible encoding
    90  		// read version string and extract version number (ignore anything after the version number)
    91  		versionstr = p.rawStringln(b)
    92  		if s := strings.SplitN(versionstr, " ", 3); len(s) >= 2 && s[0] == "version" {
    93  			if v, err := strconv.Atoi(s[1]); err == nil && v > 0 {
    94  				p.version = v
    95  			}
    96  		}
    97  	}
    98  
    99  	// read version specific flags - extend as necessary
   100  	switch p.version {
   101  	// case 4:
   102  	// 	...
   103  	//	fallthrough
   104  	case 3, 2, 1:
   105  		p.debugFormat = p.rawStringln(p.rawByte()) == "debug"
   106  		p.trackAllTypes = p.int() != 0
   107  		p.posInfoFormat = p.int() != 0
   108  	case 0:
   109  		// Go1.7 encoding format - nothing to do here
   110  	default:
   111  		errorf("unknown export format version %d (%q)", p.version, versionstr)
   112  	}
   113  
   114  	// --- generic export data ---
   115  
   116  	// populate typList with predeclared "known" types
   117  	p.typList = append(p.typList, predeclared...)
   118  
   119  	// read package data
   120  	pkg := p.pkg()
   121  
   122  	// read objects of phase 1 only (see cmd/compiler/internal/gc/bexport.go)
   123  	objcount := 0
   124  	for {
   125  		tag := p.tagOrIndex()
   126  		if tag == endTag {
   127  			break
   128  		}
   129  		p.obj(tag)
   130  		objcount++
   131  	}
   132  
   133  	// self-verification
   134  	if count := p.int(); count != objcount {
   135  		errorf("got %d objects; want %d", objcount, count)
   136  	}
   137  
   138  	// ignore compiler-specific import data
   139  
   140  	// complete interfaces
   141  	for _, typ := range p.typList {
   142  		// If we only record named types (!p.trackAllTypes),
   143  		// we must check the underlying types here. If we
   144  		// track all types, the Underlying() method call is
   145  		// not needed.
   146  		// TODO(gri) Remove if p.trackAllTypes is gone.
   147  		if it, ok := typ.Underlying().(*types.Interface); ok {
   148  			it.Complete()
   149  		}
   150  	}
   151  
   152  	// record all referenced packages as imports
   153  	list := append(([]*types.Package)(nil), p.pkgList[1:]...)
   154  	sort.Sort(byPath(list))
   155  	pkg.SetImports(list)
   156  
   157  	// package was imported completely and without errors
   158  	pkg.MarkComplete()
   159  
   160  	return p.read, pkg, nil
   161  }
   162  
   163  func errorf(format string, args ...interface{}) {
   164  	panic(fmt.Sprintf(format, args...))
   165  }
   166  
   167  func (p *importer) pkg() *types.Package {
   168  	// if the package was seen before, i is its index (>= 0)
   169  	i := p.tagOrIndex()
   170  	if i >= 0 {
   171  		return p.pkgList[i]
   172  	}
   173  
   174  	// otherwise, i is the package tag (< 0)
   175  	if i != packageTag {
   176  		errorf("unexpected package tag %d", i)
   177  	}
   178  
   179  	// read package data
   180  	name := p.string()
   181  	path := p.string()
   182  
   183  	// we should never see an empty package name
   184  	if name == "" {
   185  		errorf("empty package name in import")
   186  	}
   187  
   188  	// an empty path denotes the package we are currently importing;
   189  	// it must be the first package we see
   190  	if (path == "") != (len(p.pkgList) == 0) {
   191  		errorf("package path %q for pkg index %d", path, len(p.pkgList))
   192  	}
   193  
   194  	// if the package was imported before, use that one; otherwise create a new one
   195  	if path == "" {
   196  		path = p.path
   197  	}
   198  	pkg := p.imports[path]
   199  	if pkg == nil {
   200  		pkg = types.NewPackage(path, name)
   201  		p.imports[path] = pkg
   202  	} else if pkg.Name() != name {
   203  		errorf("conflicting names %s and %s for package %q", pkg.Name(), name, path)
   204  	}
   205  	p.pkgList = append(p.pkgList, pkg)
   206  
   207  	return pkg
   208  }
   209  
   210  // objTag returns the tag value for each object kind.
   211  // obj must not be a *types.Alias.
   212  func objTag(obj types.Object) int {
   213  	switch obj.(type) {
   214  	case *types.Const:
   215  		return constTag
   216  	case *types.TypeName:
   217  		return typeTag
   218  	case *types.Var:
   219  		return varTag
   220  	case *types.Func:
   221  		return funcTag
   222  	// Aliases are not exported multiple times, thus we should not see them here.
   223  	default:
   224  		errorf("unexpected object: %v (%T)", obj, obj) // panics
   225  		panic("unreachable")
   226  	}
   227  }
   228  
   229  func sameObj(a, b types.Object) bool {
   230  	// Because unnamed types are not canonicalized, we cannot simply compare types for
   231  	// (pointer) identity.
   232  	// Ideally we'd check equality of constant values as well, but this is good enough.
   233  	return objTag(a) == objTag(b) && types.Identical(a.Type(), b.Type())
   234  }
   235  
   236  func (p *importer) declare(obj types.Object) {
   237  	pkg := obj.Pkg()
   238  	if alt := pkg.Scope().Insert(obj); alt != nil {
   239  		// This can only trigger if we import a (non-type) object a second time.
   240  		// Excluding aliases, this cannot happen because 1) we only import a package
   241  		// once; and b) we ignore compiler-specific export data which may contain
   242  		// functions whose inlined function bodies refer to other functions that
   243  		// were already imported.
   244  		// However, aliases require reexporting the original object, so we need
   245  		// to allow it (see also the comment in cmd/compile/internal/gc/bimport.go,
   246  		// method importer.obj, switch case importing functions).
   247  		// Note that the original itself cannot be an alias.
   248  		if !sameObj(obj, alt) {
   249  			errorf("inconsistent import:\n\t%v\npreviously imported as:\n\t%v\n", obj, alt)
   250  		}
   251  	}
   252  }
   253  
   254  func (p *importer) obj(tag int) {
   255  	switch tag {
   256  	case constTag:
   257  		pos := p.pos()
   258  		pkg, name := p.qualifiedName()
   259  		typ := p.typ(nil)
   260  		val := p.value()
   261  		p.declare(types.NewConst(pos, pkg, name, typ, val))
   262  
   263  	case typeTag:
   264  		p.typ(nil)
   265  
   266  	case varTag:
   267  		pos := p.pos()
   268  		pkg, name := p.qualifiedName()
   269  		typ := p.typ(nil)
   270  		p.declare(types.NewVar(pos, pkg, name, typ))
   271  
   272  	case funcTag:
   273  		pos := p.pos()
   274  		pkg, name := p.qualifiedName()
   275  		params, isddd := p.paramList()
   276  		result, _ := p.paramList()
   277  		sig := types.NewSignature(nil, params, result, isddd)
   278  		p.declare(types.NewFunc(pos, pkg, name, sig))
   279  
   280  	case aliasTag:
   281  		pos := p.pos()
   282  		name := p.string()
   283  		var orig types.Object
   284  		if pkg, name := p.qualifiedName(); pkg != nil {
   285  			orig = pkg.Scope().Lookup(name)
   286  		}
   287  		// Alias-related code. Keep for now.
   288  		_ = pos
   289  		_ = name
   290  		_ = orig
   291  		// p.declare(types.NewAlias(pos, p.pkgList[0], name, orig))
   292  
   293  	default:
   294  		errorf("unexpected object tag %d", tag)
   295  	}
   296  }
   297  
   298  func (p *importer) pos() token.Pos {
   299  	if !p.posInfoFormat {
   300  		return token.NoPos
   301  	}
   302  
   303  	file := p.prevFile
   304  	line := p.prevLine
   305  	if delta := p.int(); delta != 0 {
   306  		// line changed
   307  		line += delta
   308  	} else if n := p.int(); n >= 0 {
   309  		// file changed
   310  		file = p.prevFile[:n] + p.string()
   311  		p.prevFile = file
   312  		line = p.int()
   313  	}
   314  	p.prevLine = line
   315  
   316  	// Synthesize a token.Pos
   317  
   318  	// Since we don't know the set of needed file positions, we
   319  	// reserve maxlines positions per file.
   320  	const maxlines = 64 * 1024
   321  	f := p.files[file]
   322  	if f == nil {
   323  		f = p.fset.AddFile(file, -1, maxlines)
   324  		p.files[file] = f
   325  		// Allocate the fake linebreak indices on first use.
   326  		// TODO(adonovan): opt: save ~512KB using a more complex scheme?
   327  		fakeLinesOnce.Do(func() {
   328  			fakeLines = make([]int, maxlines)
   329  			for i := range fakeLines {
   330  				fakeLines[i] = i
   331  			}
   332  		})
   333  		f.SetLines(fakeLines)
   334  	}
   335  
   336  	if line > maxlines {
   337  		line = 1
   338  	}
   339  
   340  	// Treat the file as if it contained only newlines
   341  	// and column=1: use the line number as the offset.
   342  	return f.Pos(line - 1)
   343  }
   344  
   345  var (
   346  	fakeLines     []int
   347  	fakeLinesOnce sync.Once
   348  )
   349  
   350  func (p *importer) qualifiedName() (pkg *types.Package, name string) {
   351  	name = p.string()
   352  	if name != "" {
   353  		pkg = p.pkg()
   354  	}
   355  	return
   356  }
   357  
   358  func (p *importer) record(t types.Type) {
   359  	p.typList = append(p.typList, t)
   360  }
   361  
   362  // A dddSlice is a types.Type representing ...T parameters.
   363  // It only appears for parameter types and does not escape
   364  // the importer.
   365  type dddSlice struct {
   366  	elem types.Type
   367  }
   368  
   369  func (t *dddSlice) Underlying() types.Type { return t }
   370  func (t *dddSlice) String() string         { return "..." + t.elem.String() }
   371  
   372  // parent is the package which declared the type; parent == nil means
   373  // the package currently imported. The parent package is needed for
   374  // exported struct fields and interface methods which don't contain
   375  // explicit package information in the export data.
   376  func (p *importer) typ(parent *types.Package) types.Type {
   377  	// if the type was seen before, i is its index (>= 0)
   378  	i := p.tagOrIndex()
   379  	if i >= 0 {
   380  		return p.typList[i]
   381  	}
   382  
   383  	// otherwise, i is the type tag (< 0)
   384  	switch i {
   385  	case namedTag:
   386  		// read type object
   387  		pos := p.pos()
   388  		parent, name := p.qualifiedName()
   389  		scope := parent.Scope()
   390  		obj := scope.Lookup(name)
   391  
   392  		// if the object doesn't exist yet, create and insert it
   393  		if obj == nil {
   394  			obj = types.NewTypeName(pos, parent, name, nil)
   395  			scope.Insert(obj)
   396  		}
   397  
   398  		if _, ok := obj.(*types.TypeName); !ok {
   399  			errorf("pkg = %s, name = %s => %s", parent, name, obj)
   400  		}
   401  
   402  		// associate new named type with obj if it doesn't exist yet
   403  		t0 := types.NewNamed(obj.(*types.TypeName), nil, nil)
   404  
   405  		// but record the existing type, if any
   406  		t := obj.Type().(*types.Named)
   407  		p.record(t)
   408  
   409  		// read underlying type
   410  		t0.SetUnderlying(p.typ(parent))
   411  
   412  		// interfaces don't have associated methods
   413  		if types.IsInterface(t0) {
   414  			return t
   415  		}
   416  
   417  		// read associated methods
   418  		for i := p.int(); i > 0; i-- {
   419  			// TODO(gri) replace this with something closer to fieldName
   420  			pos := p.pos()
   421  			name := p.string()
   422  			if !exported(name) {
   423  				p.pkg()
   424  			}
   425  
   426  			recv, _ := p.paramList() // TODO(gri) do we need a full param list for the receiver?
   427  			params, isddd := p.paramList()
   428  			result, _ := p.paramList()
   429  			p.int() // go:nointerface pragma - discarded
   430  
   431  			sig := types.NewSignature(recv.At(0), params, result, isddd)
   432  			t0.AddMethod(types.NewFunc(pos, parent, name, sig))
   433  		}
   434  
   435  		return t
   436  
   437  	case arrayTag:
   438  		t := new(types.Array)
   439  		if p.trackAllTypes {
   440  			p.record(t)
   441  		}
   442  
   443  		n := p.int64()
   444  		*t = *types.NewArray(p.typ(parent), n)
   445  		return t
   446  
   447  	case sliceTag:
   448  		t := new(types.Slice)
   449  		if p.trackAllTypes {
   450  			p.record(t)
   451  		}
   452  
   453  		*t = *types.NewSlice(p.typ(parent))
   454  		return t
   455  
   456  	case dddTag:
   457  		t := new(dddSlice)
   458  		if p.trackAllTypes {
   459  			p.record(t)
   460  		}
   461  
   462  		t.elem = p.typ(parent)
   463  		return t
   464  
   465  	case structTag:
   466  		t := new(types.Struct)
   467  		if p.trackAllTypes {
   468  			p.record(t)
   469  		}
   470  
   471  		*t = *types.NewStruct(p.fieldList(parent))
   472  		return t
   473  
   474  	case pointerTag:
   475  		t := new(types.Pointer)
   476  		if p.trackAllTypes {
   477  			p.record(t)
   478  		}
   479  
   480  		*t = *types.NewPointer(p.typ(parent))
   481  		return t
   482  
   483  	case signatureTag:
   484  		t := new(types.Signature)
   485  		if p.trackAllTypes {
   486  			p.record(t)
   487  		}
   488  
   489  		params, isddd := p.paramList()
   490  		result, _ := p.paramList()
   491  		*t = *types.NewSignature(nil, params, result, isddd)
   492  		return t
   493  
   494  	case interfaceTag:
   495  		// Create a dummy entry in the type list. This is safe because we
   496  		// cannot expect the interface type to appear in a cycle, as any
   497  		// such cycle must contain a named type which would have been
   498  		// first defined earlier.
   499  		n := len(p.typList)
   500  		if p.trackAllTypes {
   501  			p.record(nil)
   502  		}
   503  
   504  		// no embedded interfaces with gc compiler
   505  		if p.int() != 0 {
   506  			errorf("unexpected embedded interface")
   507  		}
   508  
   509  		t := types.NewInterface(p.methodList(parent), nil)
   510  		if p.trackAllTypes {
   511  			p.typList[n] = t
   512  		}
   513  		return t
   514  
   515  	case mapTag:
   516  		t := new(types.Map)
   517  		if p.trackAllTypes {
   518  			p.record(t)
   519  		}
   520  
   521  		key := p.typ(parent)
   522  		val := p.typ(parent)
   523  		*t = *types.NewMap(key, val)
   524  		return t
   525  
   526  	case chanTag:
   527  		t := new(types.Chan)
   528  		if p.trackAllTypes {
   529  			p.record(t)
   530  		}
   531  
   532  		var dir types.ChanDir
   533  		// tag values must match the constants in cmd/compile/internal/gc/go.go
   534  		switch d := p.int(); d {
   535  		case 1 /* Crecv */ :
   536  			dir = types.RecvOnly
   537  		case 2 /* Csend */ :
   538  			dir = types.SendOnly
   539  		case 3 /* Cboth */ :
   540  			dir = types.SendRecv
   541  		default:
   542  			errorf("unexpected channel dir %d", d)
   543  		}
   544  		val := p.typ(parent)
   545  		*t = *types.NewChan(dir, val)
   546  		return t
   547  
   548  	default:
   549  		errorf("unexpected type tag %d", i) // panics
   550  		panic("unreachable")
   551  	}
   552  }
   553  
   554  func (p *importer) fieldList(parent *types.Package) (fields []*types.Var, tags []string) {
   555  	if n := p.int(); n > 0 {
   556  		fields = make([]*types.Var, n)
   557  		tags = make([]string, n)
   558  		for i := range fields {
   559  			fields[i] = p.field(parent)
   560  			tags[i] = p.string()
   561  		}
   562  	}
   563  	return
   564  }
   565  
   566  func (p *importer) field(parent *types.Package) *types.Var {
   567  	pos := p.pos()
   568  	pkg, name := p.fieldName(parent)
   569  	typ := p.typ(parent)
   570  
   571  	anonymous := false
   572  	if name == "" {
   573  		// anonymous field - typ must be T or *T and T must be a type name
   574  		switch typ := deref(typ).(type) {
   575  		case *types.Basic: // basic types are named types
   576  			pkg = nil // // objects defined in Universe scope have no package
   577  			name = typ.Name()
   578  		case *types.Named:
   579  			name = typ.Obj().Name()
   580  		default:
   581  			errorf("anonymous field expected")
   582  		}
   583  		anonymous = true
   584  	}
   585  
   586  	return types.NewField(pos, pkg, name, typ, anonymous)
   587  }
   588  
   589  func (p *importer) methodList(parent *types.Package) (methods []*types.Func) {
   590  	if n := p.int(); n > 0 {
   591  		methods = make([]*types.Func, n)
   592  		for i := range methods {
   593  			methods[i] = p.method(parent)
   594  		}
   595  	}
   596  	return
   597  }
   598  
   599  func (p *importer) method(parent *types.Package) *types.Func {
   600  	pos := p.pos()
   601  	pkg, name := p.fieldName(parent)
   602  	params, isddd := p.paramList()
   603  	result, _ := p.paramList()
   604  	sig := types.NewSignature(nil, params, result, isddd)
   605  	return types.NewFunc(pos, pkg, name, sig)
   606  }
   607  
   608  func (p *importer) fieldName(parent *types.Package) (*types.Package, string) {
   609  	name := p.string()
   610  	pkg := parent
   611  	if pkg == nil {
   612  		// use the imported package instead
   613  		pkg = p.pkgList[0]
   614  	}
   615  	if p.version == 0 && name == "_" {
   616  		// version 0 didn't export a package for _ fields
   617  		return pkg, name
   618  	}
   619  	if name != "" && !exported(name) {
   620  		if name == "?" {
   621  			name = ""
   622  		}
   623  		pkg = p.pkg()
   624  	}
   625  	return pkg, name
   626  }
   627  
   628  func (p *importer) paramList() (*types.Tuple, bool) {
   629  	n := p.int()
   630  	if n == 0 {
   631  		return nil, false
   632  	}
   633  	// negative length indicates unnamed parameters
   634  	named := true
   635  	if n < 0 {
   636  		n = -n
   637  		named = false
   638  	}
   639  	// n > 0
   640  	params := make([]*types.Var, n)
   641  	isddd := false
   642  	for i := range params {
   643  		params[i], isddd = p.param(named)
   644  	}
   645  	return types.NewTuple(params...), isddd
   646  }
   647  
   648  func (p *importer) param(named bool) (*types.Var, bool) {
   649  	t := p.typ(nil)
   650  	td, isddd := t.(*dddSlice)
   651  	if isddd {
   652  		t = types.NewSlice(td.elem)
   653  	}
   654  
   655  	var pkg *types.Package
   656  	var name string
   657  	if named {
   658  		name = p.string()
   659  		if name == "" {
   660  			errorf("expected named parameter")
   661  		}
   662  		if name != "_" {
   663  			pkg = p.pkg()
   664  		}
   665  		if i := strings.Index(name, "ยท"); i > 0 {
   666  			name = name[:i] // cut off gc-specific parameter numbering
   667  		}
   668  	}
   669  
   670  	// read and discard compiler-specific info
   671  	p.string()
   672  
   673  	return types.NewVar(token.NoPos, pkg, name, t), isddd
   674  }
   675  
   676  func exported(name string) bool {
   677  	ch, _ := utf8.DecodeRuneInString(name)
   678  	return unicode.IsUpper(ch)
   679  }
   680  
   681  func (p *importer) value() constant.Value {
   682  	switch tag := p.tagOrIndex(); tag {
   683  	case falseTag:
   684  		return constant.MakeBool(false)
   685  	case trueTag:
   686  		return constant.MakeBool(true)
   687  	case int64Tag:
   688  		return constant.MakeInt64(p.int64())
   689  	case floatTag:
   690  		return p.float()
   691  	case complexTag:
   692  		re := p.float()
   693  		im := p.float()
   694  		return constant.BinaryOp(re, token.ADD, constant.MakeImag(im))
   695  	case stringTag:
   696  		return constant.MakeString(p.string())
   697  	case unknownTag:
   698  		return constant.MakeUnknown()
   699  	default:
   700  		errorf("unexpected value tag %d", tag) // panics
   701  		panic("unreachable")
   702  	}
   703  }
   704  
   705  func (p *importer) float() constant.Value {
   706  	sign := p.int()
   707  	if sign == 0 {
   708  		return constant.MakeInt64(0)
   709  	}
   710  
   711  	exp := p.int()
   712  	mant := []byte(p.string()) // big endian
   713  
   714  	// remove leading 0's if any
   715  	for len(mant) > 0 && mant[0] == 0 {
   716  		mant = mant[1:]
   717  	}
   718  
   719  	// convert to little endian
   720  	// TODO(gri) go/constant should have a more direct conversion function
   721  	//           (e.g., once it supports a big.Float based implementation)
   722  	for i, j := 0, len(mant)-1; i < j; i, j = i+1, j-1 {
   723  		mant[i], mant[j] = mant[j], mant[i]
   724  	}
   725  
   726  	// adjust exponent (constant.MakeFromBytes creates an integer value,
   727  	// but mant represents the mantissa bits such that 0.5 <= mant < 1.0)
   728  	exp -= len(mant) << 3
   729  	if len(mant) > 0 {
   730  		for msd := mant[len(mant)-1]; msd&0x80 == 0; msd <<= 1 {
   731  			exp++
   732  		}
   733  	}
   734  
   735  	x := constant.MakeFromBytes(mant)
   736  	switch {
   737  	case exp < 0:
   738  		d := constant.Shift(constant.MakeInt64(1), token.SHL, uint(-exp))
   739  		x = constant.BinaryOp(x, token.QUO, d)
   740  	case exp > 0:
   741  		x = constant.Shift(x, token.SHL, uint(exp))
   742  	}
   743  
   744  	if sign < 0 {
   745  		x = constant.UnaryOp(token.SUB, x, 0)
   746  	}
   747  	return x
   748  }
   749  
   750  // ----------------------------------------------------------------------------
   751  // Low-level decoders
   752  
   753  func (p *importer) tagOrIndex() int {
   754  	if p.debugFormat {
   755  		p.marker('t')
   756  	}
   757  
   758  	return int(p.rawInt64())
   759  }
   760  
   761  func (p *importer) int() int {
   762  	x := p.int64()
   763  	if int64(int(x)) != x {
   764  		errorf("exported integer too large")
   765  	}
   766  	return int(x)
   767  }
   768  
   769  func (p *importer) int64() int64 {
   770  	if p.debugFormat {
   771  		p.marker('i')
   772  	}
   773  
   774  	return p.rawInt64()
   775  }
   776  
   777  func (p *importer) string() string {
   778  	if p.debugFormat {
   779  		p.marker('s')
   780  	}
   781  	// if the string was seen before, i is its index (>= 0)
   782  	// (the empty string is at index 0)
   783  	i := p.rawInt64()
   784  	if i >= 0 {
   785  		return p.strList[i]
   786  	}
   787  	// otherwise, i is the negative string length (< 0)
   788  	if n := int(-i); n <= cap(p.buf) {
   789  		p.buf = p.buf[:n]
   790  	} else {
   791  		p.buf = make([]byte, n)
   792  	}
   793  	for i := range p.buf {
   794  		p.buf[i] = p.rawByte()
   795  	}
   796  	s := string(p.buf)
   797  	p.strList = append(p.strList, s)
   798  	return s
   799  }
   800  
   801  func (p *importer) marker(want byte) {
   802  	if got := p.rawByte(); got != want {
   803  		errorf("incorrect marker: got %c; want %c (pos = %d)", got, want, p.read)
   804  	}
   805  
   806  	pos := p.read
   807  	if n := int(p.rawInt64()); n != pos {
   808  		errorf("incorrect position: got %d; want %d", n, pos)
   809  	}
   810  }
   811  
   812  // rawInt64 should only be used by low-level decoders.
   813  func (p *importer) rawInt64() int64 {
   814  	i, err := binary.ReadVarint(p)
   815  	if err != nil {
   816  		errorf("read error: %v", err)
   817  	}
   818  	return i
   819  }
   820  
   821  // rawStringln should only be used to read the initial version string.
   822  func (p *importer) rawStringln(b byte) string {
   823  	p.buf = p.buf[:0]
   824  	for b != '\n' {
   825  		p.buf = append(p.buf, b)
   826  		b = p.rawByte()
   827  	}
   828  	return string(p.buf)
   829  }
   830  
   831  // needed for binary.ReadVarint in rawInt64
   832  func (p *importer) ReadByte() (byte, error) {
   833  	return p.rawByte(), nil
   834  }
   835  
   836  // byte is the bottleneck interface for reading p.data.
   837  // It unescapes '|' 'S' to '$' and '|' '|' to '|'.
   838  // rawByte should only be used by low-level decoders.
   839  func (p *importer) rawByte() byte {
   840  	b := p.data[0]
   841  	r := 1
   842  	if b == '|' {
   843  		b = p.data[1]
   844  		r = 2
   845  		switch b {
   846  		case 'S':
   847  			b = '$'
   848  		case '|':
   849  			// nothing to do
   850  		default:
   851  			errorf("unexpected escape sequence in export data")
   852  		}
   853  	}
   854  	p.data = p.data[r:]
   855  	p.read += r
   856  	return b
   857  
   858  }
   859  
   860  // ----------------------------------------------------------------------------
   861  // Export format
   862  
   863  // Tags. Must be < 0.
   864  const (
   865  	// Objects
   866  	packageTag = -(iota + 1)
   867  	constTag
   868  	typeTag
   869  	varTag
   870  	funcTag
   871  	endTag
   872  
   873  	// Types
   874  	namedTag
   875  	arrayTag
   876  	sliceTag
   877  	dddTag
   878  	structTag
   879  	pointerTag
   880  	signatureTag
   881  	interfaceTag
   882  	mapTag
   883  	chanTag
   884  
   885  	// Values
   886  	falseTag
   887  	trueTag
   888  	int64Tag
   889  	floatTag
   890  	fractionTag // not used by gc
   891  	complexTag
   892  	stringTag
   893  	nilTag     // only used by gc (appears in exported inlined function bodies)
   894  	unknownTag // not used by gc (only appears in packages with errors)
   895  
   896  	// Aliases
   897  	aliasTag
   898  )
   899  
   900  var predeclared = []types.Type{
   901  	// basic types
   902  	types.Typ[types.Bool],
   903  	types.Typ[types.Int],
   904  	types.Typ[types.Int8],
   905  	types.Typ[types.Int16],
   906  	types.Typ[types.Int32],
   907  	types.Typ[types.Int64],
   908  	types.Typ[types.Uint],
   909  	types.Typ[types.Uint8],
   910  	types.Typ[types.Uint16],
   911  	types.Typ[types.Uint32],
   912  	types.Typ[types.Uint64],
   913  	types.Typ[types.Uintptr],
   914  	types.Typ[types.Float32],
   915  	types.Typ[types.Float64],
   916  	types.Typ[types.Complex64],
   917  	types.Typ[types.Complex128],
   918  	types.Typ[types.String],
   919  
   920  	// aliases
   921  	types.Universe.Lookup("byte").Type(),
   922  	types.Universe.Lookup("rune").Type(),
   923  
   924  	// error
   925  	types.Universe.Lookup("error").Type(),
   926  
   927  	// untyped types
   928  	types.Typ[types.UntypedBool],
   929  	types.Typ[types.UntypedInt],
   930  	types.Typ[types.UntypedRune],
   931  	types.Typ[types.UntypedFloat],
   932  	types.Typ[types.UntypedComplex],
   933  	types.Typ[types.UntypedString],
   934  	types.Typ[types.UntypedNil],
   935  
   936  	// package unsafe
   937  	types.Typ[types.UnsafePointer],
   938  
   939  	// invalid type
   940  	types.Typ[types.Invalid], // only appears in packages with errors
   941  
   942  	// used internally by gc; never used by this package or in .a files
   943  	anyType{},
   944  }
   945  
   946  type anyType struct{}
   947  
   948  func (t anyType) Underlying() types.Type { return t }
   949  func (t anyType) String() string         { return "any" }