github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/cmd/compile/internal/gc/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 // Binary package import. 6 // See bexport.go for the export data format and how 7 // to make a format change. 8 9 package gc 10 11 import ( 12 "bufio" 13 "encoding/binary" 14 "fmt" 15 "math/big" 16 "strconv" 17 "strings" 18 ) 19 20 // The overall structure of Import is symmetric to Export: For each 21 // export method in bexport.go there is a matching and symmetric method 22 // in bimport.go. Changing the export format requires making symmetric 23 // changes to bimport.go and bexport.go. 24 25 type importer struct { 26 in *bufio.Reader 27 buf []byte // reused for reading strings 28 version int // export format version 29 30 // object lists, in order of deserialization 31 strList []string 32 pkgList []*Pkg 33 typList []*Type 34 funcList []*Node // nil entry means already declared 35 trackAllTypes bool 36 37 // for delayed type verification 38 cmpList []struct{ pt, t *Type } 39 40 // position encoding 41 posInfoFormat bool 42 prevFile string 43 prevLine int 44 45 // debugging support 46 debugFormat bool 47 read int // bytes read 48 } 49 50 // Import populates importpkg from the serialized package data. 51 func Import(in *bufio.Reader) { 52 p := importer{ 53 in: in, 54 version: -1, // unknown version 55 strList: []string{""}, // empty string is mapped to 0 56 } 57 58 // read version info 59 var versionstr string 60 if b := p.rawByte(); b == 'c' || b == 'd' { 61 // Go1.7 encoding; first byte encodes low-level 62 // encoding format (compact vs debug). 63 // For backward-compatibility only (avoid problems with 64 // old installed packages). Newly compiled packages use 65 // the extensible format string. 66 // TODO(gri) Remove this support eventually; after Go1.8. 67 if b == 'd' { 68 p.debugFormat = true 69 } 70 p.trackAllTypes = p.rawByte() == 'a' 71 p.posInfoFormat = p.bool() 72 versionstr = p.string() 73 if versionstr == "v1" { 74 p.version = 0 75 } 76 } else { 77 // Go1.8 extensible encoding 78 // read version string and extract version number (ignore anything after the version number) 79 versionstr = p.rawStringln(b) 80 if s := strings.SplitN(versionstr, " ", 3); len(s) >= 2 && s[0] == "version" { 81 if v, err := strconv.Atoi(s[1]); err == nil && v > 0 { 82 p.version = v 83 } 84 } 85 } 86 87 // read version specific flags - extend as necessary 88 switch p.version { 89 // case 4: 90 // ... 91 // fallthrough 92 case 3, 2, 1: 93 p.debugFormat = p.rawStringln(p.rawByte()) == "debug" 94 p.trackAllTypes = p.bool() 95 p.posInfoFormat = p.bool() 96 case 0: 97 // Go1.7 encoding format - nothing to do here 98 default: 99 formatErrorf("unknown export format version %d (%q)", p.version, versionstr) 100 } 101 102 // --- generic export data --- 103 104 // populate typList with predeclared "known" types 105 p.typList = append(p.typList, predeclared()...) 106 107 // read package data 108 p.pkg() 109 110 // defer some type-checking until all types are read in completely 111 tcok := typecheckok 112 typecheckok = true 113 defercheckwidth() 114 115 // read objects 116 117 // phase 1 118 objcount := 0 119 for { 120 tag := p.tagOrIndex() 121 if tag == endTag { 122 break 123 } 124 p.obj(tag) 125 objcount++ 126 } 127 128 // self-verification 129 if count := p.int(); count != objcount { 130 formatErrorf("got %d objects; want %d", objcount, count) 131 } 132 133 // --- compiler-specific export data --- 134 135 // read compiler-specific flags 136 137 // phase 2 138 objcount = 0 139 for { 140 tag := p.tagOrIndex() 141 if tag == endTag { 142 break 143 } 144 p.obj(tag) 145 objcount++ 146 } 147 148 // self-verification 149 if count := p.int(); count != objcount { 150 formatErrorf("got %d objects; want %d", objcount, count) 151 } 152 153 // read inlineable functions bodies 154 if dclcontext != PEXTERN { 155 formatErrorf("unexpected context %d", dclcontext) 156 } 157 158 objcount = 0 159 for i0 := -1; ; { 160 i := p.int() // index of function with inlineable body 161 if i < 0 { 162 break 163 } 164 165 // don't process the same function twice 166 if i <= i0 { 167 formatErrorf("index not increasing: %d <= %d", i, i0) 168 } 169 i0 = i 170 171 if funcdepth != 0 { 172 formatErrorf("unexpected Funcdepth %d", funcdepth) 173 } 174 175 // Note: In the original code, funchdr and funcbody are called for 176 // all functions (that were not yet imported). Now, we are calling 177 // them only for functions with inlineable bodies. funchdr does 178 // parameter renaming which doesn't matter if we don't have a body. 179 180 if f := p.funcList[i]; f != nil { 181 // function not yet imported - read body and set it 182 funchdr(f) 183 body := p.stmtList() 184 if body == nil { 185 // Make sure empty body is not interpreted as 186 // no inlineable body (see also parser.fnbody) 187 // (not doing so can cause significant performance 188 // degradation due to unnecessary calls to empty 189 // functions). 190 body = []*Node{nod(OEMPTY, nil, nil)} 191 } 192 f.Func.Inl.Set(body) 193 funcbody(f) 194 } else { 195 // function already imported - read body but discard declarations 196 dclcontext = PDISCARD // throw away any declarations 197 p.stmtList() 198 dclcontext = PEXTERN 199 } 200 201 objcount++ 202 } 203 204 // self-verification 205 if count := p.int(); count != objcount { 206 formatErrorf("got %d functions; want %d", objcount, count) 207 } 208 209 if dclcontext != PEXTERN { 210 formatErrorf("unexpected context %d", dclcontext) 211 } 212 213 p.verifyTypes() 214 215 // --- end of export data --- 216 217 typecheckok = tcok 218 resumecheckwidth() 219 220 testdclstack() // debugging only 221 } 222 223 func formatErrorf(format string, args ...interface{}) { 224 if debugFormat { 225 Fatalf(format, args...) 226 } 227 228 yyerror("cannot import %q due to version skew - reinstall package (%s)", 229 importpkg.Path, fmt.Sprintf(format, args...)) 230 errorexit() 231 } 232 233 func (p *importer) verifyTypes() { 234 for _, pair := range p.cmpList { 235 pt := pair.pt 236 t := pair.t 237 if !eqtype(pt.Orig, t) { 238 formatErrorf("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, importpkg.Path) 239 } 240 } 241 } 242 243 // numImport tracks how often a package with a given name is imported. 244 // It is used to provide a better error message (by using the package 245 // path to disambiguate) if a package that appears multiple times with 246 // the same name appears in an error message. 247 var numImport = make(map[string]int) 248 249 func (p *importer) pkg() *Pkg { 250 // if the package was seen before, i is its index (>= 0) 251 i := p.tagOrIndex() 252 if i >= 0 { 253 return p.pkgList[i] 254 } 255 256 // otherwise, i is the package tag (< 0) 257 if i != packageTag { 258 formatErrorf("expected package tag, found tag = %d", i) 259 } 260 261 // read package data 262 name := p.string() 263 path := p.string() 264 265 // we should never see an empty package name 266 if name == "" { 267 formatErrorf("empty package name for path %q", path) 268 } 269 270 // we should never see a bad import path 271 if isbadimport(path) { 272 formatErrorf("bad package path %q for package %s", path, name) 273 } 274 275 // an empty path denotes the package we are currently importing; 276 // it must be the first package we see 277 if (path == "") != (len(p.pkgList) == 0) { 278 formatErrorf("package path %q for pkg index %d", path, len(p.pkgList)) 279 } 280 281 // add package to pkgList 282 pkg := importpkg 283 if path != "" { 284 pkg = mkpkg(path) 285 } 286 if pkg.Name == "" { 287 pkg.Name = name 288 numImport[name]++ 289 } else if pkg.Name != name { 290 yyerror("conflicting package names %s and %s for path %q", pkg.Name, name, path) 291 } 292 if myimportpath != "" && path == myimportpath { 293 yyerror("import %q: package depends on %q (import cycle)", importpkg.Path, path) 294 errorexit() 295 } 296 p.pkgList = append(p.pkgList, pkg) 297 298 return pkg 299 } 300 301 func idealType(typ *Type) *Type { 302 if typ.IsUntyped() { 303 // canonicalize ideal types 304 typ = Types[TIDEAL] 305 } 306 return typ 307 } 308 309 func (p *importer) obj(tag int) { 310 switch tag { 311 case constTag: 312 p.pos() 313 sym := p.qualifiedName() 314 typ := p.typ() 315 val := p.value(typ) 316 importconst(sym, idealType(typ), nodlit(val)) 317 318 case typeTag: 319 p.typ() 320 321 case varTag: 322 p.pos() 323 sym := p.qualifiedName() 324 typ := p.typ() 325 importvar(sym, typ) 326 327 case funcTag: 328 p.pos() 329 sym := p.qualifiedName() 330 params := p.paramList() 331 result := p.paramList() 332 333 sig := functypefield(nil, params, result) 334 importsym(sym, ONAME) 335 if sym.Def != nil && sym.Def.Op == ONAME { 336 // function was imported before (via another import) 337 if !eqtype(sig, sym.Def.Type) { 338 formatErrorf("inconsistent definition for func %v during import\n\t%v\n\t%v", sym, sym.Def.Type, sig) 339 } 340 p.funcList = append(p.funcList, nil) 341 break 342 } 343 344 n := newfuncname(sym) 345 n.Type = sig 346 declare(n, PFUNC) 347 p.funcList = append(p.funcList, n) 348 importlist = append(importlist, n) 349 350 if Debug['E'] > 0 { 351 fmt.Printf("import [%q] func %v \n", importpkg.Path, n) 352 if Debug['m'] > 2 && n.Func.Inl.Len() != 0 { 353 fmt.Printf("inl body: %v\n", n.Func.Inl) 354 } 355 } 356 357 case aliasTag: 358 p.pos() 359 alias := importpkg.Lookup(p.string()) 360 orig := p.qualifiedName() 361 362 // Although the protocol allows the alias to precede the original, 363 // this never happens in files produced by gc. 364 alias.Flags |= SymAlias 365 alias.Def = orig.Def 366 importsym(alias, orig.Def.Op) 367 368 default: 369 formatErrorf("unexpected object (tag = %d)", tag) 370 } 371 } 372 373 func (p *importer) pos() { 374 if !p.posInfoFormat { 375 return 376 } 377 378 file := p.prevFile 379 line := p.prevLine 380 if delta := p.int(); delta != 0 { 381 // line changed 382 line += delta 383 } else if n := p.int(); n >= 0 { 384 // file changed 385 file = p.prevFile[:n] + p.string() 386 p.prevFile = file 387 line = p.int() 388 } 389 p.prevLine = line 390 391 // TODO(gri) register new position 392 } 393 394 func (p *importer) newtyp(etype EType) *Type { 395 t := typ(etype) 396 if p.trackAllTypes { 397 p.typList = append(p.typList, t) 398 } 399 return t 400 } 401 402 // importtype declares that pt, an imported named type, has underlying type t. 403 func (p *importer) importtype(pt, t *Type) { 404 if pt.Etype == TFORW { 405 n := pt.nod 406 copytype(pt.nod, t) 407 pt.nod = n // unzero nod 408 pt.Sym.Importdef = importpkg 409 pt.Sym.Lastlineno = lineno 410 declare(n, PEXTERN) 411 checkwidth(pt) 412 } else { 413 // pt.Orig and t must be identical. 414 if p.trackAllTypes { 415 // If we track all types, t may not be fully set up yet. 416 // Collect the types and verify identity later. 417 p.cmpList = append(p.cmpList, struct{ pt, t *Type }{pt, t}) 418 } else if !eqtype(pt.Orig, t) { 419 yyerror("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, importpkg.Path) 420 } 421 } 422 423 if Debug['E'] != 0 { 424 fmt.Printf("import type %v %L\n", pt, t) 425 } 426 } 427 428 func (p *importer) typ() *Type { 429 // if the type was seen before, i is its index (>= 0) 430 i := p.tagOrIndex() 431 if i >= 0 { 432 return p.typList[i] 433 } 434 435 // otherwise, i is the type tag (< 0) 436 var t *Type 437 switch i { 438 case namedTag: 439 p.pos() 440 tsym := p.qualifiedName() 441 442 t = pkgtype(tsym) 443 p.typList = append(p.typList, t) 444 445 // read underlying type 446 t0 := p.typ() 447 p.importtype(t, t0) 448 449 // interfaces don't have associated methods 450 if t0.IsInterface() { 451 break 452 } 453 454 // set correct import context (since p.typ() may be called 455 // while importing the body of an inlined function) 456 savedContext := dclcontext 457 dclcontext = PEXTERN 458 459 // read associated methods 460 for i := p.int(); i > 0; i-- { 461 p.pos() 462 sym := p.fieldSym() 463 464 // during import unexported method names should be in the type's package 465 if !exportname(sym.Name) && sym.Pkg != tsym.Pkg { 466 Fatalf("imported method name %+v in wrong package %s\n", sym, tsym.Pkg.Name) 467 } 468 469 recv := p.paramList() // TODO(gri) do we need a full param list for the receiver? 470 params := p.paramList() 471 result := p.paramList() 472 nointerface := p.bool() 473 474 base := recv[0].Type 475 star := false 476 if base.IsPtr() { 477 base = base.Elem() 478 star = true 479 } 480 481 n := methodname0(sym, star, base.Sym) 482 n.Type = functypefield(recv[0], params, result) 483 checkwidth(n.Type) 484 addmethod(sym, n.Type, false, nointerface) 485 p.funcList = append(p.funcList, n) 486 importlist = append(importlist, n) 487 488 // (comment from parser.go) 489 // inl.C's inlnode in on a dotmeth node expects to find the inlineable body as 490 // (dotmeth's type).Nname.Inl, and dotmeth's type has been pulled 491 // out by typecheck's lookdot as this $$.ttype. So by providing 492 // this back link here we avoid special casing there. 493 n.Type.SetNname(n) 494 495 if Debug['E'] > 0 { 496 fmt.Printf("import [%q] meth %v \n", importpkg.Path, n) 497 if Debug['m'] > 2 && n.Func.Inl.Len() != 0 { 498 fmt.Printf("inl body: %v\n", n.Func.Inl) 499 } 500 } 501 } 502 503 dclcontext = savedContext 504 505 case arrayTag: 506 t = p.newtyp(TARRAY) 507 bound := p.int64() 508 elem := p.typ() 509 t.Extra = &ArrayType{Elem: elem, Bound: bound} 510 511 case sliceTag: 512 t = p.newtyp(TSLICE) 513 elem := p.typ() 514 t.Extra = SliceType{Elem: elem} 515 516 case dddTag: 517 t = p.newtyp(TDDDFIELD) 518 t.Extra = DDDFieldType{T: p.typ()} 519 520 case structTag: 521 t = p.newtyp(TSTRUCT) 522 t.SetFields(p.fieldList()) 523 checkwidth(t) 524 525 case pointerTag: 526 t = p.newtyp(Tptr) 527 t.Extra = PtrType{Elem: p.typ()} 528 529 case signatureTag: 530 t = p.newtyp(TFUNC) 531 params := p.paramList() 532 result := p.paramList() 533 functypefield0(t, nil, params, result) 534 535 case interfaceTag: 536 t = p.newtyp(TINTER) 537 if p.int() != 0 { 538 formatErrorf("unexpected embedded interface") 539 } 540 t.SetFields(p.methodList()) 541 checkwidth(t) 542 543 case mapTag: 544 t = p.newtyp(TMAP) 545 mt := t.MapType() 546 mt.Key = p.typ() 547 mt.Val = p.typ() 548 549 case chanTag: 550 t = p.newtyp(TCHAN) 551 ct := t.ChanType() 552 ct.Dir = ChanDir(p.int()) 553 ct.Elem = p.typ() 554 555 default: 556 formatErrorf("unexpected type (tag = %d)", i) 557 } 558 559 if t == nil { 560 formatErrorf("nil type (type tag = %d)", i) 561 } 562 563 return t 564 } 565 566 func (p *importer) qualifiedName() *Sym { 567 name := p.string() 568 pkg := p.pkg() 569 return pkg.Lookup(name) 570 } 571 572 func (p *importer) fieldList() (fields []*Field) { 573 if n := p.int(); n > 0 { 574 fields = make([]*Field, n) 575 for i := range fields { 576 fields[i] = p.field() 577 } 578 } 579 return 580 } 581 582 func (p *importer) field() *Field { 583 p.pos() 584 sym := p.fieldName() 585 typ := p.typ() 586 note := p.string() 587 588 f := newField() 589 if sym.Name == "" { 590 // anonymous field - typ must be T or *T and T must be a type name 591 s := typ.Sym 592 if s == nil && typ.IsPtr() { 593 s = typ.Elem().Sym // deref 594 } 595 sym = sym.Pkg.Lookup(s.Name) 596 f.Embedded = 1 597 } 598 599 f.Sym = sym 600 f.Nname = newname(sym) 601 f.Type = typ 602 f.Note = note 603 604 return f 605 } 606 607 func (p *importer) methodList() (methods []*Field) { 608 if n := p.int(); n > 0 { 609 methods = make([]*Field, n) 610 for i := range methods { 611 methods[i] = p.method() 612 } 613 } 614 return 615 } 616 617 func (p *importer) method() *Field { 618 p.pos() 619 sym := p.fieldName() 620 params := p.paramList() 621 result := p.paramList() 622 623 f := newField() 624 f.Sym = sym 625 f.Nname = newname(sym) 626 f.Type = functypefield(fakethisfield(), params, result) 627 return f 628 } 629 630 func (p *importer) fieldName() *Sym { 631 name := p.string() 632 if p.version == 0 && name == "_" { 633 // version 0 didn't export a package for _ fields 634 // but used the builtin package instead 635 return builtinpkg.Lookup(name) 636 } 637 pkg := localpkg 638 if name != "" && !exportname(name) { 639 if name == "?" { 640 name = "" 641 } 642 pkg = p.pkg() 643 } 644 return pkg.Lookup(name) 645 } 646 647 func (p *importer) paramList() []*Field { 648 i := p.int() 649 if i == 0 { 650 return nil 651 } 652 // negative length indicates unnamed parameters 653 named := true 654 if i < 0 { 655 i = -i 656 named = false 657 } 658 // i > 0 659 fs := make([]*Field, i) 660 for i := range fs { 661 fs[i] = p.param(named) 662 } 663 return fs 664 } 665 666 func (p *importer) param(named bool) *Field { 667 f := newField() 668 f.Type = p.typ() 669 if f.Type.Etype == TDDDFIELD { 670 // TDDDFIELD indicates wrapped ... slice type 671 f.Type = typSlice(f.Type.DDDField()) 672 f.Isddd = true 673 } 674 675 if named { 676 name := p.string() 677 if name == "" { 678 formatErrorf("expected named parameter") 679 } 680 // TODO(gri) Supply function/method package rather than 681 // encoding the package for each parameter repeatedly. 682 pkg := localpkg 683 if name != "_" { 684 pkg = p.pkg() 685 } 686 f.Sym = pkg.Lookup(name) 687 f.Nname = newname(f.Sym) 688 } 689 690 // TODO(gri) This is compiler-specific (escape info). 691 // Move into compiler-specific section eventually? 692 f.Note = p.string() 693 694 return f 695 } 696 697 func (p *importer) value(typ *Type) (x Val) { 698 switch tag := p.tagOrIndex(); tag { 699 case falseTag: 700 x.U = false 701 702 case trueTag: 703 x.U = true 704 705 case int64Tag: 706 u := new(Mpint) 707 u.SetInt64(p.int64()) 708 u.Rune = typ == idealrune 709 x.U = u 710 711 case floatTag: 712 f := newMpflt() 713 p.float(f) 714 if typ == idealint || typ.IsInteger() { 715 // uncommon case: large int encoded as float 716 u := new(Mpint) 717 u.SetFloat(f) 718 x.U = u 719 break 720 } 721 x.U = f 722 723 case complexTag: 724 u := new(Mpcplx) 725 p.float(&u.Real) 726 p.float(&u.Imag) 727 x.U = u 728 729 case stringTag: 730 x.U = p.string() 731 732 case unknownTag: 733 formatErrorf("unknown constant (importing package with errors)") 734 735 case nilTag: 736 x.U = new(NilVal) 737 738 default: 739 formatErrorf("unexpected value tag %d", tag) 740 } 741 742 // verify ideal type 743 if typ.IsUntyped() && untype(x.Ctype()) != typ { 744 formatErrorf("value %v and type %v don't match", x, typ) 745 } 746 747 return 748 } 749 750 func (p *importer) float(x *Mpflt) { 751 sign := p.int() 752 if sign == 0 { 753 x.SetFloat64(0) 754 return 755 } 756 757 exp := p.int() 758 mant := new(big.Int).SetBytes([]byte(p.string())) 759 760 m := x.Val.SetInt(mant) 761 m.SetMantExp(m, exp-mant.BitLen()) 762 if sign < 0 { 763 m.Neg(m) 764 } 765 } 766 767 // ---------------------------------------------------------------------------- 768 // Inlined function bodies 769 770 // Approach: Read nodes and use them to create/declare the same data structures 771 // as done originally by the (hidden) parser by closely following the parser's 772 // original code. In other words, "parsing" the import data (which happens to 773 // be encoded in binary rather textual form) is the best way at the moment to 774 // re-establish the syntax tree's invariants. At some future point we might be 775 // able to avoid this round-about way and create the rewritten nodes directly, 776 // possibly avoiding a lot of duplicate work (name resolution, type checking). 777 // 778 // Refined nodes (e.g., ODOTPTR as a refinement of OXDOT) are exported as their 779 // unrefined nodes (since this is what the importer uses). The respective case 780 // entries are unreachable in the importer. 781 782 func (p *importer) stmtList() []*Node { 783 var list []*Node 784 for { 785 n := p.node() 786 if n == nil { 787 break 788 } 789 // OBLOCK nodes may be created when importing ODCL nodes - unpack them 790 if n.Op == OBLOCK { 791 list = append(list, n.List.Slice()...) 792 } else { 793 list = append(list, n) 794 } 795 } 796 return list 797 } 798 799 func (p *importer) exprList() []*Node { 800 var list []*Node 801 for { 802 n := p.expr() 803 if n == nil { 804 break 805 } 806 list = append(list, n) 807 } 808 return list 809 } 810 811 func (p *importer) elemList() []*Node { 812 c := p.int() 813 list := make([]*Node, c) 814 for i := range list { 815 s := p.fieldSym() 816 list[i] = nodSym(OSTRUCTKEY, p.expr(), s) 817 } 818 return list 819 } 820 821 func (p *importer) expr() *Node { 822 n := p.node() 823 if n != nil && n.Op == OBLOCK { 824 Fatalf("unexpected block node: %v", n) 825 } 826 return n 827 } 828 829 // TODO(gri) split into expr and stmt 830 func (p *importer) node() *Node { 831 switch op := p.op(); op { 832 // expressions 833 // case OPAREN: 834 // unreachable - unpacked by exporter 835 836 // case ODDDARG: 837 // unimplemented 838 839 case OLITERAL: 840 typ := p.typ() 841 n := nodlit(p.value(typ)) 842 if !typ.IsUntyped() { 843 // Type-checking simplifies unsafe.Pointer(uintptr(c)) 844 // to unsafe.Pointer(c) which then cannot type-checked 845 // again. Re-introduce explicit uintptr(c) conversion. 846 // (issue 16317). 847 if typ.IsUnsafePtr() { 848 conv := nod(OCALL, typenod(Types[TUINTPTR]), nil) 849 conv.List.Set1(n) 850 n = conv 851 } 852 conv := nod(OCALL, typenod(typ), nil) 853 conv.List.Set1(n) 854 n = conv 855 } 856 return n 857 858 case ONAME: 859 return mkname(p.sym()) 860 861 // case OPACK, ONONAME: 862 // unreachable - should have been resolved by typechecking 863 864 case OTYPE: 865 if p.bool() { 866 return mkname(p.sym()) 867 } 868 return typenod(p.typ()) 869 870 // case OTARRAY, OTMAP, OTCHAN, OTSTRUCT, OTINTER, OTFUNC: 871 // unreachable - should have been resolved by typechecking 872 873 // case OCLOSURE: 874 // unimplemented 875 876 case OPTRLIT: 877 n := p.expr() 878 if !p.bool() /* !implicit, i.e. '&' operator */ { 879 if n.Op == OCOMPLIT { 880 // Special case for &T{...}: turn into (*T){...}. 881 n.Right = nod(OIND, n.Right, nil) 882 n.Right.Implicit = true 883 } else { 884 n = nod(OADDR, n, nil) 885 } 886 } 887 return n 888 889 case OSTRUCTLIT: 890 n := nod(OCOMPLIT, nil, typenod(p.typ())) 891 n.List.Set(p.elemList()) // special handling of field names 892 return n 893 894 // case OARRAYLIT, OSLICELIT, OMAPLIT: 895 // unreachable - mapped to case OCOMPLIT below by exporter 896 897 case OCOMPLIT: 898 n := nod(OCOMPLIT, nil, typenod(p.typ())) 899 n.List.Set(p.exprList()) 900 return n 901 902 case OKEY: 903 left, right := p.exprsOrNil() 904 return nod(OKEY, left, right) 905 906 // case OSTRUCTKEY: 907 // unreachable - handled in case OSTRUCTLIT by elemList 908 909 // case OCALLPART: 910 // unimplemented 911 912 // case OXDOT, ODOT, ODOTPTR, ODOTINTER, ODOTMETH: 913 // unreachable - mapped to case OXDOT below by exporter 914 915 case OXDOT: 916 // see parser.new_dotname 917 return nodSym(OXDOT, p.expr(), p.fieldSym()) 918 919 // case ODOTTYPE, ODOTTYPE2: 920 // unreachable - mapped to case ODOTTYPE below by exporter 921 922 case ODOTTYPE: 923 n := nod(ODOTTYPE, p.expr(), nil) 924 if p.bool() { 925 n.Right = p.expr() 926 } else { 927 n.Right = typenod(p.typ()) 928 } 929 return n 930 931 // case OINDEX, OINDEXMAP, OSLICE, OSLICESTR, OSLICEARR, OSLICE3, OSLICE3ARR: 932 // unreachable - mapped to cases below by exporter 933 934 case OINDEX: 935 return nod(op, p.expr(), p.expr()) 936 937 case OSLICE, OSLICE3: 938 n := nod(op, p.expr(), nil) 939 low, high := p.exprsOrNil() 940 var max *Node 941 if n.Op.IsSlice3() { 942 max = p.expr() 943 } 944 n.SetSliceBounds(low, high, max) 945 return n 946 947 // case OCONV, OCONVIFACE, OCONVNOP, OARRAYBYTESTR, OARRAYRUNESTR, OSTRARRAYBYTE, OSTRARRAYRUNE, ORUNESTR: 948 // unreachable - mapped to OCONV case below by exporter 949 950 case OCONV: 951 n := nod(OCALL, typenod(p.typ()), nil) 952 n.List.Set(p.exprList()) 953 return n 954 955 case OCOPY, OCOMPLEX, OREAL, OIMAG, OAPPEND, OCAP, OCLOSE, ODELETE, OLEN, OMAKE, ONEW, OPANIC, ORECOVER, OPRINT, OPRINTN: 956 n := builtinCall(op) 957 n.List.Set(p.exprList()) 958 if op == OAPPEND { 959 n.Isddd = p.bool() 960 } 961 return n 962 963 // case OCALL, OCALLFUNC, OCALLMETH, OCALLINTER, OGETG: 964 // unreachable - mapped to OCALL case below by exporter 965 966 case OCALL: 967 n := nod(OCALL, p.expr(), nil) 968 n.List.Set(p.exprList()) 969 n.Isddd = p.bool() 970 return n 971 972 case OMAKEMAP, OMAKECHAN, OMAKESLICE: 973 n := builtinCall(OMAKE) 974 n.List.Append(typenod(p.typ())) 975 n.List.Append(p.exprList()...) 976 return n 977 978 // unary expressions 979 case OPLUS, OMINUS, OADDR, OCOM, OIND, ONOT, ORECV: 980 return nod(op, p.expr(), nil) 981 982 // binary expressions 983 case OADD, OAND, OANDAND, OANDNOT, ODIV, OEQ, OGE, OGT, OLE, OLT, 984 OLSH, OMOD, OMUL, ONE, OOR, OOROR, ORSH, OSEND, OSUB, OXOR: 985 return nod(op, p.expr(), p.expr()) 986 987 case OADDSTR: 988 list := p.exprList() 989 x := list[0] 990 for _, y := range list[1:] { 991 x = nod(OADD, x, y) 992 } 993 return x 994 995 // case OCMPSTR, OCMPIFACE: 996 // unreachable - mapped to std comparison operators by exporter 997 998 case ODCLCONST: 999 // TODO(gri) these should not be exported in the first place 1000 return nod(OEMPTY, nil, nil) 1001 1002 // -------------------------------------------------------------------- 1003 // statements 1004 case ODCL: 1005 if p.version < 2 { 1006 // versions 0 and 1 exported a bool here but it 1007 // was always false - simply ignore in this case 1008 p.bool() 1009 } 1010 lhs := dclname(p.sym()) 1011 typ := typenod(p.typ()) 1012 return liststmt(variter([]*Node{lhs}, typ, nil)) // TODO(gri) avoid list creation 1013 1014 // case ODCLFIELD: 1015 // unimplemented 1016 1017 // case OAS, OASWB: 1018 // unreachable - mapped to OAS case below by exporter 1019 1020 case OAS: 1021 return nod(OAS, p.expr(), p.expr()) 1022 1023 case OASOP: 1024 n := nod(OASOP, nil, nil) 1025 n.Etype = EType(p.int()) 1026 n.Left = p.expr() 1027 if !p.bool() { 1028 n.Right = nodintconst(1) 1029 n.Implicit = true 1030 } else { 1031 n.Right = p.expr() 1032 } 1033 return n 1034 1035 // case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV: 1036 // unreachable - mapped to OAS2 case below by exporter 1037 1038 case OAS2: 1039 n := nod(OAS2, nil, nil) 1040 n.List.Set(p.exprList()) 1041 n.Rlist.Set(p.exprList()) 1042 return n 1043 1044 case ORETURN: 1045 n := nod(ORETURN, nil, nil) 1046 n.List.Set(p.exprList()) 1047 return n 1048 1049 // case ORETJMP: 1050 // unreachable - generated by compiler for trampolin routines (not exported) 1051 1052 case OPROC, ODEFER: 1053 return nod(op, p.expr(), nil) 1054 1055 case OIF: 1056 markdcl() 1057 n := nod(OIF, nil, nil) 1058 n.Ninit.Set(p.stmtList()) 1059 n.Left = p.expr() 1060 n.Nbody.Set(p.stmtList()) 1061 n.Rlist.Set(p.stmtList()) 1062 popdcl() 1063 return n 1064 1065 case OFOR: 1066 markdcl() 1067 n := nod(OFOR, nil, nil) 1068 n.Ninit.Set(p.stmtList()) 1069 n.Left, n.Right = p.exprsOrNil() 1070 n.Nbody.Set(p.stmtList()) 1071 popdcl() 1072 return n 1073 1074 case ORANGE: 1075 markdcl() 1076 n := nod(ORANGE, nil, nil) 1077 n.List.Set(p.stmtList()) 1078 n.Right = p.expr() 1079 n.Nbody.Set(p.stmtList()) 1080 popdcl() 1081 return n 1082 1083 case OSELECT, OSWITCH: 1084 markdcl() 1085 n := nod(op, nil, nil) 1086 n.Ninit.Set(p.stmtList()) 1087 n.Left, _ = p.exprsOrNil() 1088 n.List.Set(p.stmtList()) 1089 popdcl() 1090 return n 1091 1092 // case OCASE, OXCASE: 1093 // unreachable - mapped to OXCASE case below by exporter 1094 1095 case OXCASE: 1096 markdcl() 1097 n := nod(OXCASE, nil, nil) 1098 n.Xoffset = int64(block) 1099 n.List.Set(p.exprList()) 1100 // TODO(gri) eventually we must declare variables for type switch 1101 // statements (type switch statements are not yet exported) 1102 n.Nbody.Set(p.stmtList()) 1103 popdcl() 1104 return n 1105 1106 // case OFALL: 1107 // unreachable - mapped to OXFALL case below by exporter 1108 1109 case OXFALL: 1110 n := nod(OXFALL, nil, nil) 1111 n.Xoffset = int64(block) 1112 return n 1113 1114 case OBREAK, OCONTINUE: 1115 left, _ := p.exprsOrNil() 1116 if left != nil { 1117 left = newname(left.Sym) 1118 } 1119 return nod(op, left, nil) 1120 1121 // case OEMPTY: 1122 // unreachable - not emitted by exporter 1123 1124 case OGOTO, OLABEL: 1125 n := nod(op, newname(p.expr().Sym), nil) 1126 n.Sym = dclstack // context, for goto restrictions 1127 return n 1128 1129 case OEND: 1130 return nil 1131 1132 default: 1133 Fatalf("cannot import %v (%d) node\n"+ 1134 "==> please file an issue and assign to gri@\n", op, int(op)) 1135 panic("unreachable") // satisfy compiler 1136 } 1137 } 1138 1139 func builtinCall(op Op) *Node { 1140 return nod(OCALL, mkname(builtinpkg.Lookup(goopnames[op])), nil) 1141 } 1142 1143 func (p *importer) exprsOrNil() (a, b *Node) { 1144 ab := p.int() 1145 if ab&1 != 0 { 1146 a = p.expr() 1147 } 1148 if ab&2 != 0 { 1149 b = p.expr() 1150 } 1151 return 1152 } 1153 1154 func (p *importer) fieldSym() *Sym { 1155 name := p.string() 1156 pkg := localpkg 1157 if !exportname(name) { 1158 pkg = p.pkg() 1159 } 1160 return pkg.Lookup(name) 1161 } 1162 1163 func (p *importer) sym() *Sym { 1164 name := p.string() 1165 pkg := localpkg 1166 if name != "_" { 1167 pkg = p.pkg() 1168 } 1169 return pkg.Lookup(name) 1170 } 1171 1172 func (p *importer) bool() bool { 1173 return p.int() != 0 1174 } 1175 1176 func (p *importer) op() Op { 1177 return Op(p.int()) 1178 } 1179 1180 // ---------------------------------------------------------------------------- 1181 // Low-level decoders 1182 1183 func (p *importer) tagOrIndex() int { 1184 if p.debugFormat { 1185 p.marker('t') 1186 } 1187 1188 return int(p.rawInt64()) 1189 } 1190 1191 func (p *importer) int() int { 1192 x := p.int64() 1193 if int64(int(x)) != x { 1194 formatErrorf("exported integer too large") 1195 } 1196 return int(x) 1197 } 1198 1199 func (p *importer) int64() int64 { 1200 if p.debugFormat { 1201 p.marker('i') 1202 } 1203 1204 return p.rawInt64() 1205 } 1206 1207 func (p *importer) string() string { 1208 if p.debugFormat { 1209 p.marker('s') 1210 } 1211 // if the string was seen before, i is its index (>= 0) 1212 // (the empty string is at index 0) 1213 i := p.rawInt64() 1214 if i >= 0 { 1215 return p.strList[i] 1216 } 1217 // otherwise, i is the negative string length (< 0) 1218 if n := int(-i); n <= cap(p.buf) { 1219 p.buf = p.buf[:n] 1220 } else { 1221 p.buf = make([]byte, n) 1222 } 1223 for i := range p.buf { 1224 p.buf[i] = p.rawByte() 1225 } 1226 s := string(p.buf) 1227 p.strList = append(p.strList, s) 1228 return s 1229 } 1230 1231 func (p *importer) marker(want byte) { 1232 if got := p.rawByte(); got != want { 1233 formatErrorf("incorrect marker: got %c; want %c (pos = %d)", got, want, p.read) 1234 } 1235 1236 pos := p.read 1237 if n := int(p.rawInt64()); n != pos { 1238 formatErrorf("incorrect position: got %d; want %d", n, pos) 1239 } 1240 } 1241 1242 // rawInt64 should only be used by low-level decoders. 1243 func (p *importer) rawInt64() int64 { 1244 i, err := binary.ReadVarint(p) 1245 if err != nil { 1246 formatErrorf("read error: %v", err) 1247 } 1248 return i 1249 } 1250 1251 // rawStringln should only be used to read the initial version string. 1252 func (p *importer) rawStringln(b byte) string { 1253 p.buf = p.buf[:0] 1254 for b != '\n' { 1255 p.buf = append(p.buf, b) 1256 b = p.rawByte() 1257 } 1258 return string(p.buf) 1259 } 1260 1261 // needed for binary.ReadVarint in rawInt64 1262 func (p *importer) ReadByte() (byte, error) { 1263 return p.rawByte(), nil 1264 } 1265 1266 // rawByte is the bottleneck interface for reading from p.in. 1267 // It unescapes '|' 'S' to '$' and '|' '|' to '|'. 1268 // rawByte should only be used by low-level decoders. 1269 func (p *importer) rawByte() byte { 1270 c, err := p.in.ReadByte() 1271 p.read++ 1272 if err != nil { 1273 formatErrorf("read error: %v", err) 1274 } 1275 if c == '|' { 1276 c, err = p.in.ReadByte() 1277 p.read++ 1278 if err != nil { 1279 formatErrorf("read error: %v", err) 1280 } 1281 switch c { 1282 case 'S': 1283 c = '$' 1284 case '|': 1285 // nothing to do 1286 default: 1287 formatErrorf("unexpected escape sequence in export data") 1288 } 1289 } 1290 return c 1291 }