github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/cmd/compile/internal/gc/subr.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gc 6 7 import ( 8 "bytes" 9 "cmd/internal/obj" 10 "crypto/md5" 11 "encoding/binary" 12 "fmt" 13 "os" 14 "sort" 15 "strings" 16 "unicode" 17 "unicode/utf8" 18 ) 19 20 type Error struct { 21 lineno int 22 seq int 23 msg string 24 } 25 26 var errors []Error 27 28 func errorexit() { 29 Flusherrors() 30 if outfile != "" { 31 os.Remove(outfile) 32 } 33 os.Exit(2) 34 } 35 36 func parserline() int { 37 if parsing && theparser.Lookahead() > 0 { 38 // parser has one symbol lookahead 39 return int(prevlineno) 40 } 41 return int(lineno) 42 } 43 44 func adderrorname(n *Node) { 45 if n.Op != ODOT { 46 return 47 } 48 old := fmt.Sprintf("%v: undefined: %v\n", n.Line(), n.Left) 49 if len(errors) > 0 && int32(errors[len(errors)-1].lineno) == n.Lineno && errors[len(errors)-1].msg == old { 50 errors[len(errors)-1].msg = fmt.Sprintf("%v: undefined: %v in %v\n", n.Line(), n.Left, n) 51 } 52 } 53 54 func adderr(line int, format string, args ...interface{}) { 55 errors = append(errors, Error{ 56 seq: len(errors), 57 lineno: line, 58 msg: fmt.Sprintf("%v: %s\n", Ctxt.Line(line), fmt.Sprintf(format, args...)), 59 }) 60 } 61 62 // errcmp sorts errors by line, then seq, then message. 63 type errcmp []Error 64 65 func (x errcmp) Len() int { return len(x) } 66 func (x errcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 67 func (x errcmp) Less(i, j int) bool { 68 a := &x[i] 69 b := &x[j] 70 if a.lineno != b.lineno { 71 return a.lineno < b.lineno 72 } 73 if a.seq != b.seq { 74 return a.seq < b.seq 75 } 76 return a.msg < b.msg 77 } 78 79 func Flusherrors() { 80 bstdout.Flush() 81 if len(errors) == 0 { 82 return 83 } 84 sort.Sort(errcmp(errors)) 85 for i := 0; i < len(errors); i++ { 86 if i == 0 || errors[i].msg != errors[i-1].msg { 87 fmt.Printf("%s", errors[i].msg) 88 } 89 } 90 errors = errors[:0] 91 } 92 93 func hcrash() { 94 if Debug['h'] != 0 { 95 Flusherrors() 96 if outfile != "" { 97 os.Remove(outfile) 98 } 99 var x *int 100 *x = 0 101 } 102 } 103 104 func yyerrorl(line int, format string, args ...interface{}) { 105 adderr(line, format, args...) 106 107 hcrash() 108 nerrors++ 109 if nsavederrors+nerrors >= 10 && Debug['e'] == 0 { 110 Flusherrors() 111 fmt.Printf("%v: too many errors\n", Ctxt.Line(line)) 112 errorexit() 113 } 114 } 115 116 var yyerror_lastsyntax int 117 118 func Yyerror(format string, args ...interface{}) { 119 msg := fmt.Sprintf(format, args...) 120 if strings.HasPrefix(msg, "syntax error") { 121 nsyntaxerrors++ 122 123 // An unexpected EOF caused a syntax error. Use the previous 124 // line number since getc generated a fake newline character. 125 if curio.eofnl { 126 lexlineno = prevlineno 127 } 128 129 // only one syntax error per line 130 if int32(yyerror_lastsyntax) == lexlineno { 131 return 132 } 133 yyerror_lastsyntax = int(lexlineno) 134 135 // plain "syntax error" gets "near foo" added 136 if msg == "syntax error" { 137 yyerrorl(int(lexlineno), "syntax error near %s", lexbuf.String()) 138 return 139 } 140 141 // The grammar has { and LBRACE but both show up as {. 142 // Rewrite syntax error referring to "{ or {" to say just "{". 143 // The grammar has ? and @ but only for reading imports. 144 // Silence them in ordinary errors. 145 msg = strings.Replace(msg, "{ or {", "{", -1) 146 msg = strings.Replace(msg, " or ?", "", -1) 147 msg = strings.Replace(msg, " or @", "", -1) 148 149 msg = strings.Replace(msg, "LLITERAL", litbuf, -1) 150 151 yyerrorl(int(lexlineno), "%s", msg) 152 return 153 } 154 155 adderr(parserline(), "%s", msg) 156 157 hcrash() 158 nerrors++ 159 if nsavederrors+nerrors >= 10 && Debug['e'] == 0 { 160 Flusherrors() 161 fmt.Printf("%v: too many errors\n", Ctxt.Line(parserline())) 162 errorexit() 163 } 164 } 165 166 func Warn(fmt_ string, args ...interface{}) { 167 adderr(parserline(), fmt_, args...) 168 169 hcrash() 170 } 171 172 func Warnl(line int, fmt_ string, args ...interface{}) { 173 adderr(line, fmt_, args...) 174 if Debug['m'] != 0 { 175 Flusherrors() 176 } 177 } 178 179 func Fatalf(fmt_ string, args ...interface{}) { 180 Flusherrors() 181 182 fmt.Printf("%v: internal compiler error: ", Ctxt.Line(int(lineno))) 183 fmt.Printf(fmt_, args...) 184 fmt.Printf("\n") 185 186 // If this is a released compiler version, ask for a bug report. 187 if strings.HasPrefix(obj.Getgoversion(), "release") { 188 fmt.Printf("\n") 189 fmt.Printf("Please file a bug report including a short program that triggers the error.\n") 190 fmt.Printf("https://golang.org/issue/new\n") 191 } 192 193 hcrash() 194 errorexit() 195 } 196 197 func linehistpragma(file string) { 198 if Debug['i'] != 0 { 199 fmt.Printf("pragma %s at line %v\n", file, Ctxt.Line(int(lexlineno))) 200 } 201 Ctxt.AddImport(file) 202 } 203 204 func linehistpush(file string) { 205 if Debug['i'] != 0 { 206 fmt.Printf("import %s at line %v\n", file, Ctxt.Line(int(lexlineno))) 207 } 208 Ctxt.LineHist.Push(int(lexlineno), file) 209 } 210 211 func linehistpop() { 212 if Debug['i'] != 0 { 213 fmt.Printf("end of import at line %v\n", Ctxt.Line(int(lexlineno))) 214 } 215 Ctxt.LineHist.Pop(int(lexlineno)) 216 } 217 218 func linehistupdate(file string, off int) { 219 if Debug['i'] != 0 { 220 fmt.Printf("line %s at line %v\n", file, Ctxt.Line(int(lexlineno))) 221 } 222 Ctxt.LineHist.Update(int(lexlineno), file, off) 223 } 224 225 func setlineno(n *Node) int32 { 226 lno := lineno 227 if n != nil { 228 switch n.Op { 229 case ONAME, OTYPE, OPACK: 230 break 231 232 case OLITERAL: 233 if n.Sym != nil { 234 break 235 } 236 fallthrough 237 238 default: 239 lineno = n.Lineno 240 if lineno == 0 { 241 if Debug['K'] != 0 { 242 Warn("setlineno: line 0") 243 } 244 lineno = lno 245 } 246 } 247 } 248 249 return lno 250 } 251 252 func Lookup(name string) *Sym { 253 return localpkg.Lookup(name) 254 } 255 256 func Lookupf(format string, a ...interface{}) *Sym { 257 return Lookup(fmt.Sprintf(format, a...)) 258 } 259 260 func LookupBytes(name []byte) *Sym { 261 return localpkg.LookupBytes(name) 262 } 263 264 var initSyms []*Sym 265 266 var nopkg = &Pkg{ 267 Syms: make(map[string]*Sym), 268 } 269 270 func (pkg *Pkg) Lookup(name string) *Sym { 271 if pkg == nil { 272 pkg = nopkg 273 } 274 if s := pkg.Syms[name]; s != nil { 275 return s 276 } 277 278 s := &Sym{ 279 Name: name, 280 Pkg: pkg, 281 Lexical: LNAME, 282 } 283 if name == "init" { 284 initSyms = append(initSyms, s) 285 } 286 pkg.Syms[name] = s 287 return s 288 } 289 290 func (pkg *Pkg) LookupBytes(name []byte) *Sym { 291 if pkg == nil { 292 pkg = nopkg 293 } 294 if s := pkg.Syms[string(name)]; s != nil { 295 return s 296 } 297 str := internString(name) 298 return pkg.Lookup(str) 299 } 300 301 func Pkglookup(name string, pkg *Pkg) *Sym { 302 return pkg.Lookup(name) 303 } 304 305 func restrictlookup(name string, pkg *Pkg) *Sym { 306 if !exportname(name) && pkg != localpkg { 307 Yyerror("cannot refer to unexported name %s.%s", pkg.Name, name) 308 } 309 return Pkglookup(name, pkg) 310 } 311 312 // find all the exported symbols in package opkg 313 // and make them available in the current package 314 func importdot(opkg *Pkg, pack *Node) { 315 var s1 *Sym 316 var pkgerror string 317 318 n := 0 319 for _, s := range opkg.Syms { 320 if s.Def == nil { 321 continue 322 } 323 if !exportname(s.Name) || strings.ContainsRune(s.Name, 0xb7) { // 0xb7 = center dot 324 continue 325 } 326 s1 = Lookup(s.Name) 327 if s1.Def != nil { 328 pkgerror = fmt.Sprintf("during import %q", opkg.Path) 329 redeclare(s1, pkgerror) 330 continue 331 } 332 333 s1.Def = s.Def 334 s1.Block = s.Block 335 if s1.Def.Name == nil { 336 Dump("s1def", s1.Def) 337 Fatalf("missing Name") 338 } 339 s1.Def.Name.Pack = pack 340 s1.Origpkg = opkg 341 n++ 342 } 343 344 if n == 0 { 345 // can't possibly be used - there were no symbols 346 yyerrorl(int(pack.Lineno), "imported and not used: %q", opkg.Path) 347 } 348 } 349 350 func Nod(op int, nleft *Node, nright *Node) *Node { 351 n := new(Node) 352 n.Op = uint8(op) 353 n.Left = nleft 354 n.Right = nright 355 n.Lineno = int32(parserline()) 356 n.Xoffset = BADWIDTH 357 n.Orig = n 358 switch op { 359 case OCLOSURE, ODCLFUNC: 360 n.Func = new(Func) 361 n.Func.FCurfn = Curfn 362 case ONAME: 363 n.Name = new(Name) 364 n.Name.Param = new(Param) 365 case OLABEL, OPACK: 366 n.Name = new(Name) 367 case ODCLFIELD: 368 if nleft != nil { 369 n.Name = nleft.Name 370 } else { 371 n.Name = new(Name) 372 n.Name.Param = new(Param) 373 } 374 } 375 if n.Name != nil { 376 n.Name.Curfn = Curfn 377 } 378 return n 379 } 380 381 func saveorignode(n *Node) { 382 if n.Orig != nil { 383 return 384 } 385 norig := Nod(int(n.Op), nil, nil) 386 *norig = *n 387 n.Orig = norig 388 } 389 390 // ispaddedfield reports whether the given field 391 // is followed by padding. For the case where t is 392 // the last field, total gives the size of the enclosing struct. 393 func ispaddedfield(t *Type, total int64) bool { 394 if t.Etype != TFIELD { 395 Fatalf("ispaddedfield called non-field %v", t) 396 } 397 if t.Down == nil { 398 return t.Width+t.Type.Width != total 399 } 400 return t.Width+t.Type.Width != t.Down.Width 401 } 402 403 func algtype1(t *Type, bad **Type) int { 404 if bad != nil { 405 *bad = nil 406 } 407 if t.Broke { 408 return AMEM 409 } 410 if t.Noalg { 411 return ANOEQ 412 } 413 414 switch t.Etype { 415 // will be defined later. 416 case TANY, TFORW: 417 *bad = t 418 419 return -1 420 421 case TINT8, 422 TUINT8, 423 TINT16, 424 TUINT16, 425 TINT32, 426 TUINT32, 427 TINT64, 428 TUINT64, 429 TINT, 430 TUINT, 431 TUINTPTR, 432 TBOOL, 433 TPTR32, 434 TPTR64, 435 TCHAN, 436 TUNSAFEPTR: 437 return AMEM 438 439 case TFUNC, TMAP: 440 if bad != nil { 441 *bad = t 442 } 443 return ANOEQ 444 445 case TFLOAT32: 446 return AFLOAT32 447 448 case TFLOAT64: 449 return AFLOAT64 450 451 case TCOMPLEX64: 452 return ACPLX64 453 454 case TCOMPLEX128: 455 return ACPLX128 456 457 case TSTRING: 458 return ASTRING 459 460 case TINTER: 461 if isnilinter(t) { 462 return ANILINTER 463 } 464 return AINTER 465 466 case TARRAY: 467 if Isslice(t) { 468 if bad != nil { 469 *bad = t 470 } 471 return ANOEQ 472 } 473 474 a := algtype1(t.Type, bad) 475 if a == ANOEQ || a == AMEM { 476 if a == ANOEQ && bad != nil { 477 *bad = t 478 } 479 return a 480 } 481 482 return -1 // needs special compare 483 484 case TSTRUCT: 485 if t.Type != nil && t.Type.Down == nil && !isblanksym(t.Type.Sym) { 486 // One-field struct is same as that one field alone. 487 return algtype1(t.Type.Type, bad) 488 } 489 490 ret := AMEM 491 var a int 492 for t1 := t.Type; t1 != nil; t1 = t1.Down { 493 // All fields must be comparable. 494 a = algtype1(t1.Type, bad) 495 496 if a == ANOEQ { 497 return ANOEQ 498 } 499 500 // Blank fields, padded fields, fields with non-memory 501 // equality need special compare. 502 if a != AMEM || isblanksym(t1.Sym) || ispaddedfield(t1, t.Width) { 503 ret = -1 504 continue 505 } 506 } 507 508 return ret 509 } 510 511 Fatalf("algtype1: unexpected type %v", t) 512 return 0 513 } 514 515 func algtype(t *Type) int { 516 a := algtype1(t, nil) 517 if a == AMEM || a == ANOEQ { 518 if Isslice(t) { 519 return ASLICE 520 } 521 switch t.Width { 522 case 0: 523 return a + AMEM0 - AMEM 524 525 case 1: 526 return a + AMEM8 - AMEM 527 528 case 2: 529 return a + AMEM16 - AMEM 530 531 case 4: 532 return a + AMEM32 - AMEM 533 534 case 8: 535 return a + AMEM64 - AMEM 536 537 case 16: 538 return a + AMEM128 - AMEM 539 } 540 } 541 542 return a 543 } 544 545 func maptype(key *Type, val *Type) *Type { 546 if key != nil { 547 var bad *Type 548 atype := algtype1(key, &bad) 549 var mtype int 550 if bad == nil { 551 mtype = int(key.Etype) 552 } else { 553 mtype = int(bad.Etype) 554 } 555 switch mtype { 556 default: 557 if atype == ANOEQ { 558 Yyerror("invalid map key type %v", key) 559 } 560 561 // will be resolved later. 562 case TANY: 563 break 564 565 // map[key] used during definition of key. 566 // postpone check until key is fully defined. 567 // if there are multiple uses of map[key] 568 // before key is fully defined, the error 569 // will only be printed for the first one. 570 // good enough. 571 case TFORW: 572 if key.Maplineno == 0 { 573 key.Maplineno = lineno 574 } 575 } 576 } 577 578 t := typ(TMAP) 579 t.Down = key 580 t.Type = val 581 return t 582 } 583 584 func typ(et int) *Type { 585 t := new(Type) 586 t.Etype = uint8(et) 587 t.Width = BADWIDTH 588 t.Lineno = int(lineno) 589 t.Orig = t 590 return t 591 } 592 593 // methcmp sorts by symbol, then by package path for unexported symbols. 594 type methcmp []*Type 595 596 func (x methcmp) Len() int { return len(x) } 597 func (x methcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 598 func (x methcmp) Less(i, j int) bool { 599 a := x[i] 600 b := x[j] 601 if a.Sym == nil && b.Sym == nil { 602 return false 603 } 604 if a.Sym == nil { 605 return true 606 } 607 if b.Sym == nil { 608 return false 609 } 610 if a.Sym.Name != b.Sym.Name { 611 return a.Sym.Name < b.Sym.Name 612 } 613 if !exportname(a.Sym.Name) { 614 if a.Sym.Pkg.Path != b.Sym.Pkg.Path { 615 return a.Sym.Pkg.Path < b.Sym.Pkg.Path 616 } 617 } 618 619 return false 620 } 621 622 func sortinter(t *Type) *Type { 623 if t.Type == nil || t.Type.Down == nil { 624 return t 625 } 626 627 var a []*Type 628 for f := t.Type; f != nil; f = f.Down { 629 a = append(a, f) 630 } 631 sort.Sort(methcmp(a)) 632 633 n := len(a) // n > 0 due to initial conditions. 634 for i := 0; i < n-1; i++ { 635 a[i].Down = a[i+1] 636 } 637 a[n-1].Down = nil 638 639 t.Type = a[0] 640 return t 641 } 642 643 func Nodintconst(v int64) *Node { 644 c := Nod(OLITERAL, nil, nil) 645 c.Addable = true 646 c.SetVal(Val{new(Mpint)}) 647 Mpmovecfix(c.Val().U.(*Mpint), v) 648 c.Type = Types[TIDEAL] 649 ullmancalc(c) 650 return c 651 } 652 653 func nodfltconst(v *Mpflt) *Node { 654 c := Nod(OLITERAL, nil, nil) 655 c.Addable = true 656 c.SetVal(Val{newMpflt()}) 657 mpmovefltflt(c.Val().U.(*Mpflt), v) 658 c.Type = Types[TIDEAL] 659 ullmancalc(c) 660 return c 661 } 662 663 func Nodconst(n *Node, t *Type, v int64) { 664 *n = Node{} 665 n.Op = OLITERAL 666 n.Addable = true 667 ullmancalc(n) 668 n.SetVal(Val{new(Mpint)}) 669 Mpmovecfix(n.Val().U.(*Mpint), v) 670 n.Type = t 671 672 if Isfloat[t.Etype] { 673 Fatalf("nodconst: bad type %v", t) 674 } 675 } 676 677 func nodnil() *Node { 678 c := Nodintconst(0) 679 c.SetVal(Val{new(NilVal)}) 680 c.Type = Types[TNIL] 681 return c 682 } 683 684 func Nodbool(b bool) *Node { 685 c := Nodintconst(0) 686 c.SetVal(Val{b}) 687 c.Type = idealbool 688 return c 689 } 690 691 func aindex(b *Node, t *Type) *Type { 692 bound := int64(-1) // open bound 693 typecheck(&b, Erv) 694 if b != nil { 695 switch consttype(b) { 696 default: 697 Yyerror("array bound must be an integer expression") 698 699 case CTINT, CTRUNE: 700 bound = Mpgetfix(b.Val().U.(*Mpint)) 701 if bound < 0 { 702 Yyerror("array bound must be non negative") 703 } 704 } 705 } 706 707 // fixed array 708 r := typ(TARRAY) 709 710 r.Type = t 711 r.Bound = bound 712 return r 713 } 714 715 // treecopy recursively copies n, with the exception of 716 // ONAME, OLITERAL, OTYPE, and non-iota ONONAME leaves. 717 // Copies of iota ONONAME nodes are assigned the current 718 // value of iota_. If lineno != 0, it sets the line number 719 // of newly allocated nodes to lineno. 720 func treecopy(n *Node, lineno int32) *Node { 721 if n == nil { 722 return nil 723 } 724 725 var m *Node 726 switch n.Op { 727 default: 728 m = Nod(OXXX, nil, nil) 729 *m = *n 730 m.Orig = m 731 m.Left = treecopy(n.Left, lineno) 732 m.Right = treecopy(n.Right, lineno) 733 m.List = listtreecopy(n.List, lineno) 734 if lineno != 0 { 735 m.Lineno = lineno 736 } 737 if m.Name != nil && n.Op != ODCLFIELD { 738 Dump("treecopy", n) 739 Fatalf("treecopy Name") 740 } 741 742 case ONONAME: 743 if n.Sym == Lookup("iota") { 744 // Not sure yet whether this is the real iota, 745 // but make a copy of the Node* just in case, 746 // so that all the copies of this const definition 747 // don't have the same iota value. 748 m = Nod(OXXX, nil, nil) 749 *m = *n 750 if lineno != 0 { 751 m.Lineno = lineno 752 } 753 m.Name = new(Name) 754 *m.Name = *n.Name 755 m.Name.Iota = iota_ 756 break 757 } 758 fallthrough 759 760 case ONAME, OLITERAL, OTYPE: 761 m = n 762 } 763 764 return m 765 } 766 767 func isnil(n *Node) bool { 768 if n == nil { 769 return false 770 } 771 if n.Op != OLITERAL { 772 return false 773 } 774 if n.Val().Ctype() != CTNIL { 775 return false 776 } 777 return true 778 } 779 780 func isptrto(t *Type, et int) bool { 781 if t == nil { 782 return false 783 } 784 if !Isptr[t.Etype] { 785 return false 786 } 787 t = t.Type 788 if t == nil { 789 return false 790 } 791 if int(t.Etype) != et { 792 return false 793 } 794 return true 795 } 796 797 func Istype(t *Type, et int) bool { 798 return t != nil && int(t.Etype) == et 799 } 800 801 func Isfixedarray(t *Type) bool { 802 return t != nil && t.Etype == TARRAY && t.Bound >= 0 803 } 804 805 func Isslice(t *Type) bool { 806 return t != nil && t.Etype == TARRAY && t.Bound < 0 807 } 808 809 func isblank(n *Node) bool { 810 if n == nil { 811 return false 812 } 813 return isblanksym(n.Sym) 814 } 815 816 func isblanksym(s *Sym) bool { 817 return s != nil && s.Name == "_" 818 } 819 820 func Isinter(t *Type) bool { 821 return t != nil && t.Etype == TINTER 822 } 823 824 func isnilinter(t *Type) bool { 825 if !Isinter(t) { 826 return false 827 } 828 if t.Type != nil { 829 return false 830 } 831 return true 832 } 833 834 func isideal(t *Type) bool { 835 if t == nil { 836 return false 837 } 838 if t == idealstring || t == idealbool { 839 return true 840 } 841 switch t.Etype { 842 case TNIL, TIDEAL: 843 return true 844 } 845 846 return false 847 } 848 849 /* 850 * given receiver of type t (t == r or t == *r) 851 * return type to hang methods off (r). 852 */ 853 func methtype(t *Type, mustname int) *Type { 854 if t == nil { 855 return nil 856 } 857 858 // strip away pointer if it's there 859 if Isptr[t.Etype] { 860 if t.Sym != nil { 861 return nil 862 } 863 t = t.Type 864 if t == nil { 865 return nil 866 } 867 } 868 869 // need a type name 870 if t.Sym == nil && (mustname != 0 || t.Etype != TSTRUCT) { 871 return nil 872 } 873 874 // check types 875 if !issimple[t.Etype] { 876 switch t.Etype { 877 default: 878 return nil 879 880 case TSTRUCT, 881 TARRAY, 882 TMAP, 883 TCHAN, 884 TSTRING, 885 TFUNC: 886 break 887 } 888 } 889 890 return t 891 } 892 893 func cplxsubtype(et int) int { 894 switch et { 895 case TCOMPLEX64: 896 return TFLOAT32 897 898 case TCOMPLEX128: 899 return TFLOAT64 900 } 901 902 Fatalf("cplxsubtype: %v\n", Econv(int(et), 0)) 903 return 0 904 } 905 906 func eqnote(a, b *string) bool { 907 return a == b || a != nil && b != nil && *a == *b 908 } 909 910 type TypePairList struct { 911 t1 *Type 912 t2 *Type 913 next *TypePairList 914 } 915 916 func onlist(l *TypePairList, t1 *Type, t2 *Type) bool { 917 for ; l != nil; l = l.next { 918 if (l.t1 == t1 && l.t2 == t2) || (l.t1 == t2 && l.t2 == t1) { 919 return true 920 } 921 } 922 return false 923 } 924 925 // Return 1 if t1 and t2 are identical, following the spec rules. 926 // 927 // Any cyclic type must go through a named type, and if one is 928 // named, it is only identical to the other if they are the same 929 // pointer (t1 == t2), so there's no chance of chasing cycles 930 // ad infinitum, so no need for a depth counter. 931 func Eqtype(t1 *Type, t2 *Type) bool { 932 return eqtype1(t1, t2, nil) 933 } 934 935 func eqtype1(t1 *Type, t2 *Type, assumed_equal *TypePairList) bool { 936 if t1 == t2 { 937 return true 938 } 939 if t1 == nil || t2 == nil || t1.Etype != t2.Etype { 940 return false 941 } 942 if t1.Sym != nil || t2.Sym != nil { 943 // Special case: we keep byte and uint8 separate 944 // for error messages. Treat them as equal. 945 switch t1.Etype { 946 case TUINT8: 947 if (t1 == Types[TUINT8] || t1 == bytetype) && (t2 == Types[TUINT8] || t2 == bytetype) { 948 return true 949 } 950 951 case TINT, TINT32: 952 if (t1 == Types[runetype.Etype] || t1 == runetype) && (t2 == Types[runetype.Etype] || t2 == runetype) { 953 return true 954 } 955 } 956 957 return false 958 } 959 960 if onlist(assumed_equal, t1, t2) { 961 return true 962 } 963 var l TypePairList 964 l.next = assumed_equal 965 l.t1 = t1 966 l.t2 = t2 967 968 switch t1.Etype { 969 case TINTER, TSTRUCT: 970 t1 = t1.Type 971 t2 = t2.Type 972 for ; t1 != nil && t2 != nil; t1, t2 = t1.Down, t2.Down { 973 if t1.Etype != TFIELD || t2.Etype != TFIELD { 974 Fatalf("struct/interface missing field: %v %v", t1, t2) 975 } 976 if t1.Sym != t2.Sym || t1.Embedded != t2.Embedded || !eqtype1(t1.Type, t2.Type, &l) || !eqnote(t1.Note, t2.Note) { 977 return false 978 } 979 } 980 981 if t1 == nil && t2 == nil { 982 return true 983 } 984 return false 985 986 // Loop over structs: receiver, in, out. 987 case TFUNC: 988 t1 = t1.Type 989 t2 = t2.Type 990 for ; t1 != nil && t2 != nil; t1, t2 = t1.Down, t2.Down { 991 if t1.Etype != TSTRUCT || t2.Etype != TSTRUCT { 992 Fatalf("func missing struct: %v %v", t1, t2) 993 } 994 995 // Loop over fields in structs, ignoring argument names. 996 ta := t1.Type 997 tb := t2.Type 998 for ; ta != nil && tb != nil; ta, tb = ta.Down, tb.Down { 999 if ta.Etype != TFIELD || tb.Etype != TFIELD { 1000 Fatalf("func struct missing field: %v %v", ta, tb) 1001 } 1002 if ta.Isddd != tb.Isddd || !eqtype1(ta.Type, tb.Type, &l) { 1003 return false 1004 } 1005 } 1006 1007 if ta != nil || tb != nil { 1008 return false 1009 } 1010 } 1011 1012 if t1 == nil && t2 == nil { 1013 return true 1014 } 1015 return false 1016 1017 case TARRAY: 1018 if t1.Bound != t2.Bound { 1019 return false 1020 } 1021 1022 case TCHAN: 1023 if t1.Chan != t2.Chan { 1024 return false 1025 } 1026 } 1027 1028 if eqtype1(t1.Down, t2.Down, &l) && eqtype1(t1.Type, t2.Type, &l) { 1029 return true 1030 } 1031 return false 1032 } 1033 1034 // Are t1 and t2 equal struct types when field names are ignored? 1035 // For deciding whether the result struct from g can be copied 1036 // directly when compiling f(g()). 1037 func eqtypenoname(t1 *Type, t2 *Type) bool { 1038 if t1 == nil || t2 == nil || t1.Etype != TSTRUCT || t2.Etype != TSTRUCT { 1039 return false 1040 } 1041 1042 t1 = t1.Type 1043 t2 = t2.Type 1044 for { 1045 if !Eqtype(t1, t2) { 1046 return false 1047 } 1048 if t1 == nil { 1049 return true 1050 } 1051 t1 = t1.Down 1052 t2 = t2.Down 1053 } 1054 } 1055 1056 // Is type src assignment compatible to type dst? 1057 // If so, return op code to use in conversion. 1058 // If not, return 0. 1059 func assignop(src *Type, dst *Type, why *string) int { 1060 if why != nil { 1061 *why = "" 1062 } 1063 1064 // TODO(rsc,lvd): This behaves poorly in the presence of inlining. 1065 // https://golang.org/issue/2795 1066 if safemode != 0 && importpkg == nil && src != nil && src.Etype == TUNSAFEPTR { 1067 Yyerror("cannot use unsafe.Pointer") 1068 errorexit() 1069 } 1070 1071 if src == dst { 1072 return OCONVNOP 1073 } 1074 if src == nil || dst == nil || src.Etype == TFORW || dst.Etype == TFORW || src.Orig == nil || dst.Orig == nil { 1075 return 0 1076 } 1077 1078 // 1. src type is identical to dst. 1079 if Eqtype(src, dst) { 1080 return OCONVNOP 1081 } 1082 1083 // 2. src and dst have identical underlying types 1084 // and either src or dst is not a named type or 1085 // both are empty interface types. 1086 // For assignable but different non-empty interface types, 1087 // we want to recompute the itab. 1088 if Eqtype(src.Orig, dst.Orig) && (src.Sym == nil || dst.Sym == nil || isnilinter(src)) { 1089 return OCONVNOP 1090 } 1091 1092 // 3. dst is an interface type and src implements dst. 1093 if dst.Etype == TINTER && src.Etype != TNIL { 1094 var missing *Type 1095 var ptr int 1096 var have *Type 1097 if implements(src, dst, &missing, &have, &ptr) { 1098 return OCONVIFACE 1099 } 1100 1101 // we'll have complained about this method anyway, suppress spurious messages. 1102 if have != nil && have.Sym == missing.Sym && (have.Type.Broke || missing.Type.Broke) { 1103 return OCONVIFACE 1104 } 1105 1106 if why != nil { 1107 if isptrto(src, TINTER) { 1108 *why = fmt.Sprintf(":\n\t%v is pointer to interface, not interface", src) 1109 } else if have != nil && have.Sym == missing.Sym && have.Nointerface { 1110 *why = fmt.Sprintf(":\n\t%v does not implement %v (%v method is marked 'nointerface')", src, dst, missing.Sym) 1111 } else if have != nil && have.Sym == missing.Sym { 1112 *why = fmt.Sprintf(":\n\t%v does not implement %v (wrong type for %v method)\n"+"\t\thave %v%v\n\t\twant %v%v", src, dst, missing.Sym, have.Sym, Tconv(have.Type, obj.FmtShort|obj.FmtByte), missing.Sym, Tconv(missing.Type, obj.FmtShort|obj.FmtByte)) 1113 } else if ptr != 0 { 1114 *why = fmt.Sprintf(":\n\t%v does not implement %v (%v method has pointer receiver)", src, dst, missing.Sym) 1115 } else if have != nil { 1116 *why = fmt.Sprintf(":\n\t%v does not implement %v (missing %v method)\n"+"\t\thave %v%v\n\t\twant %v%v", src, dst, missing.Sym, have.Sym, Tconv(have.Type, obj.FmtShort|obj.FmtByte), missing.Sym, Tconv(missing.Type, obj.FmtShort|obj.FmtByte)) 1117 } else { 1118 *why = fmt.Sprintf(":\n\t%v does not implement %v (missing %v method)", src, dst, missing.Sym) 1119 } 1120 } 1121 1122 return 0 1123 } 1124 1125 if isptrto(dst, TINTER) { 1126 if why != nil { 1127 *why = fmt.Sprintf(":\n\t%v is pointer to interface, not interface", dst) 1128 } 1129 return 0 1130 } 1131 1132 if src.Etype == TINTER && dst.Etype != TBLANK { 1133 var have *Type 1134 var ptr int 1135 var missing *Type 1136 if why != nil && implements(dst, src, &missing, &have, &ptr) { 1137 *why = ": need type assertion" 1138 } 1139 return 0 1140 } 1141 1142 // 4. src is a bidirectional channel value, dst is a channel type, 1143 // src and dst have identical element types, and 1144 // either src or dst is not a named type. 1145 if src.Etype == TCHAN && src.Chan == Cboth && dst.Etype == TCHAN { 1146 if Eqtype(src.Type, dst.Type) && (src.Sym == nil || dst.Sym == nil) { 1147 return OCONVNOP 1148 } 1149 } 1150 1151 // 5. src is the predeclared identifier nil and dst is a nillable type. 1152 if src.Etype == TNIL { 1153 switch dst.Etype { 1154 case TARRAY: 1155 if dst.Bound != -100 { // not slice 1156 break 1157 } 1158 fallthrough 1159 1160 case TPTR32, 1161 TPTR64, 1162 TFUNC, 1163 TMAP, 1164 TCHAN, 1165 TINTER: 1166 return OCONVNOP 1167 } 1168 } 1169 1170 // 6. rule about untyped constants - already converted by defaultlit. 1171 1172 // 7. Any typed value can be assigned to the blank identifier. 1173 if dst.Etype == TBLANK { 1174 return OCONVNOP 1175 } 1176 1177 return 0 1178 } 1179 1180 // Can we convert a value of type src to a value of type dst? 1181 // If so, return op code to use in conversion (maybe OCONVNOP). 1182 // If not, return 0. 1183 func convertop(src *Type, dst *Type, why *string) int { 1184 if why != nil { 1185 *why = "" 1186 } 1187 1188 if src == dst { 1189 return OCONVNOP 1190 } 1191 if src == nil || dst == nil { 1192 return 0 1193 } 1194 1195 // 1. src can be assigned to dst. 1196 op := assignop(src, dst, why) 1197 if op != 0 { 1198 return op 1199 } 1200 1201 // The rules for interfaces are no different in conversions 1202 // than assignments. If interfaces are involved, stop now 1203 // with the good message from assignop. 1204 // Otherwise clear the error. 1205 if src.Etype == TINTER || dst.Etype == TINTER { 1206 return 0 1207 } 1208 if why != nil { 1209 *why = "" 1210 } 1211 1212 // 2. src and dst have identical underlying types. 1213 if Eqtype(src.Orig, dst.Orig) { 1214 return OCONVNOP 1215 } 1216 1217 // 3. src and dst are unnamed pointer types 1218 // and their base types have identical underlying types. 1219 if Isptr[src.Etype] && Isptr[dst.Etype] && src.Sym == nil && dst.Sym == nil { 1220 if Eqtype(src.Type.Orig, dst.Type.Orig) { 1221 return OCONVNOP 1222 } 1223 } 1224 1225 // 4. src and dst are both integer or floating point types. 1226 if (Isint[src.Etype] || Isfloat[src.Etype]) && (Isint[dst.Etype] || Isfloat[dst.Etype]) { 1227 if Simtype[src.Etype] == Simtype[dst.Etype] { 1228 return OCONVNOP 1229 } 1230 return OCONV 1231 } 1232 1233 // 5. src and dst are both complex types. 1234 if Iscomplex[src.Etype] && Iscomplex[dst.Etype] { 1235 if Simtype[src.Etype] == Simtype[dst.Etype] { 1236 return OCONVNOP 1237 } 1238 return OCONV 1239 } 1240 1241 // 6. src is an integer or has type []byte or []rune 1242 // and dst is a string type. 1243 if Isint[src.Etype] && dst.Etype == TSTRING { 1244 return ORUNESTR 1245 } 1246 1247 if Isslice(src) && dst.Etype == TSTRING { 1248 if src.Type.Etype == bytetype.Etype { 1249 return OARRAYBYTESTR 1250 } 1251 if src.Type.Etype == runetype.Etype { 1252 return OARRAYRUNESTR 1253 } 1254 } 1255 1256 // 7. src is a string and dst is []byte or []rune. 1257 // String to slice. 1258 if src.Etype == TSTRING && Isslice(dst) { 1259 if dst.Type.Etype == bytetype.Etype { 1260 return OSTRARRAYBYTE 1261 } 1262 if dst.Type.Etype == runetype.Etype { 1263 return OSTRARRAYRUNE 1264 } 1265 } 1266 1267 // 8. src is a pointer or uintptr and dst is unsafe.Pointer. 1268 if (Isptr[src.Etype] || src.Etype == TUINTPTR) && dst.Etype == TUNSAFEPTR { 1269 return OCONVNOP 1270 } 1271 1272 // 9. src is unsafe.Pointer and dst is a pointer or uintptr. 1273 if src.Etype == TUNSAFEPTR && (Isptr[dst.Etype] || dst.Etype == TUINTPTR) { 1274 return OCONVNOP 1275 } 1276 1277 return 0 1278 } 1279 1280 func assignconv(n *Node, t *Type, context string) *Node { 1281 return assignconvfn(n, t, func() string { return context }) 1282 } 1283 1284 // Convert node n for assignment to type t. 1285 func assignconvfn(n *Node, t *Type, context func() string) *Node { 1286 if n == nil || n.Type == nil || n.Type.Broke { 1287 return n 1288 } 1289 1290 if t.Etype == TBLANK && n.Type.Etype == TNIL { 1291 Yyerror("use of untyped nil") 1292 } 1293 1294 old := n 1295 old.Diag++ // silence errors about n; we'll issue one below 1296 defaultlit(&n, t) 1297 old.Diag-- 1298 if t.Etype == TBLANK { 1299 return n 1300 } 1301 1302 // Convert ideal bool from comparison to plain bool 1303 // if the next step is non-bool (like interface{}). 1304 if n.Type == idealbool && t.Etype != TBOOL { 1305 if n.Op == ONAME || n.Op == OLITERAL { 1306 r := Nod(OCONVNOP, n, nil) 1307 r.Type = Types[TBOOL] 1308 r.Typecheck = 1 1309 r.Implicit = true 1310 n = r 1311 } 1312 } 1313 1314 if Eqtype(n.Type, t) { 1315 return n 1316 } 1317 1318 var why string 1319 op := assignop(n.Type, t, &why) 1320 if op == 0 { 1321 Yyerror("cannot use %v as type %v in %s%s", Nconv(n, obj.FmtLong), t, context(), why) 1322 op = OCONV 1323 } 1324 1325 r := Nod(op, n, nil) 1326 r.Type = t 1327 r.Typecheck = 1 1328 r.Implicit = true 1329 r.Orig = n.Orig 1330 return r 1331 } 1332 1333 // substArgTypes substitutes the given list of types for 1334 // successive occurrences of the "any" placeholder in the 1335 // type syntax expression n.Type. 1336 func substArgTypes(n *Node, types ...*Type) { 1337 for _, t := range types { 1338 dowidth(t) 1339 } 1340 substAny(&n.Type, &types) 1341 if len(types) > 0 { 1342 Fatalf("substArgTypes: too many argument types") 1343 } 1344 } 1345 1346 // substAny walks *tp, replacing instances of "any" with successive 1347 // elements removed from types. 1348 func substAny(tp **Type, types *[]*Type) { 1349 for { 1350 t := *tp 1351 if t == nil { 1352 return 1353 } 1354 if t.Etype == TANY && t.Copyany { 1355 if len(*types) == 0 { 1356 Fatalf("substArgTypes: not enough argument types") 1357 } 1358 *tp = (*types)[0] 1359 *types = (*types)[1:] 1360 } 1361 1362 switch t.Etype { 1363 case TPTR32, TPTR64, TCHAN, TARRAY: 1364 tp = &t.Type 1365 continue 1366 1367 case TMAP: 1368 substAny(&t.Down, types) 1369 tp = &t.Type 1370 continue 1371 1372 case TFUNC: 1373 substAny(&t.Type, types) 1374 substAny(&t.Type.Down.Down, types) 1375 substAny(&t.Type.Down, types) 1376 1377 case TSTRUCT: 1378 for t = t.Type; t != nil; t = t.Down { 1379 substAny(&t.Type, types) 1380 } 1381 } 1382 return 1383 } 1384 } 1385 1386 /* 1387 * Is this a 64-bit type? 1388 */ 1389 func Is64(t *Type) bool { 1390 if t == nil { 1391 return false 1392 } 1393 switch Simtype[t.Etype] { 1394 case TINT64, TUINT64, TPTR64: 1395 return true 1396 } 1397 1398 return false 1399 } 1400 1401 /* 1402 * Is a conversion between t1 and t2 a no-op? 1403 */ 1404 func Noconv(t1 *Type, t2 *Type) bool { 1405 e1 := int(Simtype[t1.Etype]) 1406 e2 := int(Simtype[t2.Etype]) 1407 1408 switch e1 { 1409 case TINT8, TUINT8: 1410 return e2 == TINT8 || e2 == TUINT8 1411 1412 case TINT16, TUINT16: 1413 return e2 == TINT16 || e2 == TUINT16 1414 1415 case TINT32, TUINT32, TPTR32: 1416 return e2 == TINT32 || e2 == TUINT32 || e2 == TPTR32 1417 1418 case TINT64, TUINT64, TPTR64: 1419 return e2 == TINT64 || e2 == TUINT64 || e2 == TPTR64 1420 1421 case TFLOAT32: 1422 return e2 == TFLOAT32 1423 1424 case TFLOAT64: 1425 return e2 == TFLOAT64 1426 } 1427 1428 return false 1429 } 1430 1431 func shallow(t *Type) *Type { 1432 if t == nil { 1433 return nil 1434 } 1435 nt := typ(0) 1436 *nt = *t 1437 if t.Orig == t { 1438 nt.Orig = nt 1439 } 1440 return nt 1441 } 1442 1443 func deep(t *Type) *Type { 1444 if t == nil { 1445 return nil 1446 } 1447 1448 var nt *Type 1449 switch t.Etype { 1450 default: 1451 nt = t // share from here down 1452 1453 case TANY: 1454 nt = shallow(t) 1455 nt.Copyany = true 1456 1457 case TPTR32, TPTR64, TCHAN, TARRAY: 1458 nt = shallow(t) 1459 nt.Type = deep(t.Type) 1460 1461 case TMAP: 1462 nt = shallow(t) 1463 nt.Down = deep(t.Down) 1464 nt.Type = deep(t.Type) 1465 1466 case TFUNC: 1467 nt = shallow(t) 1468 nt.Type = deep(t.Type) 1469 nt.Type.Down = deep(t.Type.Down) 1470 nt.Type.Down.Down = deep(t.Type.Down.Down) 1471 1472 case TSTRUCT: 1473 nt = shallow(t) 1474 nt.Type = shallow(t.Type) 1475 xt := nt.Type 1476 1477 for t = t.Type; t != nil; t = t.Down { 1478 xt.Type = deep(t.Type) 1479 xt.Down = shallow(t.Down) 1480 xt = xt.Down 1481 } 1482 } 1483 1484 return nt 1485 } 1486 1487 func syslook(name string, copy int) *Node { 1488 s := Pkglookup(name, Runtimepkg) 1489 if s == nil || s.Def == nil { 1490 Fatalf("syslook: can't find runtime.%s", name) 1491 } 1492 1493 if copy == 0 { 1494 return s.Def 1495 } 1496 1497 n := Nod(0, nil, nil) 1498 *n = *s.Def 1499 n.Type = deep(s.Def.Type) 1500 1501 return n 1502 } 1503 1504 /* 1505 * compute a hash value for type t. 1506 * if t is a method type, ignore the receiver 1507 * so that the hash can be used in interface checks. 1508 * %T already contains 1509 * all the necessary logic to generate a representation 1510 * of the type that completely describes it. 1511 * using smprint here avoids duplicating that code. 1512 * using md5 here is overkill, but i got tired of 1513 * accidental collisions making the runtime think 1514 * two types are equal when they really aren't. 1515 */ 1516 func typehash(t *Type) uint32 { 1517 var p string 1518 1519 if t.Thistuple != 0 { 1520 // hide method receiver from Tpretty 1521 t.Thistuple = 0 1522 1523 p = Tconv(t, obj.FmtLeft|obj.FmtUnsigned) 1524 t.Thistuple = 1 1525 } else { 1526 p = Tconv(t, obj.FmtLeft|obj.FmtUnsigned) 1527 } 1528 1529 //print("typehash: %s\n", p); 1530 h := md5.Sum([]byte(p)) 1531 return binary.LittleEndian.Uint32(h[:4]) 1532 } 1533 1534 var initPtrtoDone bool 1535 1536 var ( 1537 ptrToUint8 *Type 1538 ptrToAny *Type 1539 ptrToString *Type 1540 ptrToBool *Type 1541 ptrToInt32 *Type 1542 ) 1543 1544 func initPtrto() { 1545 ptrToUint8 = ptrto1(Types[TUINT8]) 1546 ptrToAny = ptrto1(Types[TANY]) 1547 ptrToString = ptrto1(Types[TSTRING]) 1548 ptrToBool = ptrto1(Types[TBOOL]) 1549 ptrToInt32 = ptrto1(Types[TINT32]) 1550 } 1551 1552 func ptrto1(t *Type) *Type { 1553 t1 := typ(Tptr) 1554 t1.Type = t 1555 t1.Width = int64(Widthptr) 1556 t1.Align = uint8(Widthptr) 1557 return t1 1558 } 1559 1560 // Ptrto returns the Type *t. 1561 // The returned struct must not be modified. 1562 func Ptrto(t *Type) *Type { 1563 if Tptr == 0 { 1564 Fatalf("ptrto: no tptr") 1565 } 1566 // Reduce allocations by pre-creating common cases. 1567 if !initPtrtoDone { 1568 initPtrto() 1569 initPtrtoDone = true 1570 } 1571 switch t { 1572 case Types[TUINT8]: 1573 return ptrToUint8 1574 case Types[TINT32]: 1575 return ptrToInt32 1576 case Types[TANY]: 1577 return ptrToAny 1578 case Types[TSTRING]: 1579 return ptrToString 1580 case Types[TBOOL]: 1581 return ptrToBool 1582 } 1583 return ptrto1(t) 1584 } 1585 1586 func frame(context int) { 1587 if context != 0 { 1588 fmt.Printf("--- external frame ---\n") 1589 for _, n := range externdcl { 1590 printframenode(n) 1591 } 1592 return 1593 } 1594 1595 if Curfn != nil { 1596 fmt.Printf("--- %v frame ---\n", Curfn.Func.Nname.Sym) 1597 for l := Curfn.Func.Dcl; l != nil; l = l.Next { 1598 printframenode(l.N) 1599 } 1600 } 1601 } 1602 1603 func printframenode(n *Node) { 1604 w := int64(-1) 1605 if n.Type != nil { 1606 w = n.Type.Width 1607 } 1608 switch n.Op { 1609 case ONAME: 1610 fmt.Printf("%v %v G%d %v width=%d\n", Oconv(int(n.Op), 0), n.Sym, n.Name.Vargen, n.Type, w) 1611 case OTYPE: 1612 fmt.Printf("%v %v width=%d\n", Oconv(int(n.Op), 0), n.Type, w) 1613 } 1614 } 1615 1616 /* 1617 * calculate sethi/ullman number 1618 * roughly how many registers needed to 1619 * compile a node. used to compile the 1620 * hardest side first to minimize registers. 1621 */ 1622 func ullmancalc(n *Node) { 1623 if n == nil { 1624 return 1625 } 1626 1627 var ul int 1628 var ur int 1629 if n.Ninit != nil { 1630 ul = UINF 1631 goto out 1632 } 1633 1634 switch n.Op { 1635 case OREGISTER, OLITERAL, ONAME: 1636 ul = 1 1637 if n.Class == PPARAMREF || (n.Class&PHEAP != 0) { 1638 ul++ 1639 } 1640 goto out 1641 1642 case OCALL, OCALLFUNC, OCALLMETH, OCALLINTER, OASWB: 1643 ul = UINF 1644 goto out 1645 1646 // hard with race detector 1647 case OANDAND, OOROR: 1648 if flag_race != 0 { 1649 ul = UINF 1650 goto out 1651 } 1652 } 1653 1654 ul = 1 1655 if n.Left != nil { 1656 ul = int(n.Left.Ullman) 1657 } 1658 ur = 1 1659 if n.Right != nil { 1660 ur = int(n.Right.Ullman) 1661 } 1662 if ul == ur { 1663 ul += 1 1664 } 1665 if ur > ul { 1666 ul = ur 1667 } 1668 1669 out: 1670 if ul > 200 { 1671 ul = 200 // clamp to uchar with room to grow 1672 } 1673 n.Ullman = uint8(ul) 1674 } 1675 1676 func badtype(o int, tl *Type, tr *Type) { 1677 fmt_ := "" 1678 if tl != nil { 1679 fmt_ += fmt.Sprintf("\n\t%v", tl) 1680 } 1681 if tr != nil { 1682 fmt_ += fmt.Sprintf("\n\t%v", tr) 1683 } 1684 1685 // common mistake: *struct and *interface. 1686 if tl != nil && tr != nil && Isptr[tl.Etype] && Isptr[tr.Etype] { 1687 if tl.Type.Etype == TSTRUCT && tr.Type.Etype == TINTER { 1688 fmt_ += "\n\t(*struct vs *interface)" 1689 } else if tl.Type.Etype == TINTER && tr.Type.Etype == TSTRUCT { 1690 fmt_ += "\n\t(*interface vs *struct)" 1691 } 1692 } 1693 1694 s := fmt_ 1695 Yyerror("illegal types for operand: %v%s", Oconv(int(o), 0), s) 1696 } 1697 1698 /* 1699 * iterator to walk a structure declaration 1700 */ 1701 func Structfirst(s *Iter, nn **Type) *Type { 1702 var t *Type 1703 1704 n := *nn 1705 if n == nil { 1706 goto bad 1707 } 1708 1709 switch n.Etype { 1710 default: 1711 goto bad 1712 1713 case TSTRUCT, TINTER, TFUNC: 1714 break 1715 } 1716 1717 t = n.Type 1718 if t == nil { 1719 return nil 1720 } 1721 1722 if t.Etype != TFIELD { 1723 Fatalf("structfirst: not field %v", t) 1724 } 1725 1726 s.T = t 1727 return t 1728 1729 bad: 1730 Fatalf("structfirst: not struct %v", n) 1731 1732 return nil 1733 } 1734 1735 func structnext(s *Iter) *Type { 1736 n := s.T 1737 t := n.Down 1738 if t == nil { 1739 return nil 1740 } 1741 1742 if t.Etype != TFIELD { 1743 Fatalf("structnext: not struct %v", n) 1744 1745 return nil 1746 } 1747 1748 s.T = t 1749 return t 1750 } 1751 1752 /* 1753 * iterator to this and inargs in a function 1754 */ 1755 func funcfirst(s *Iter, t *Type) *Type { 1756 var fp *Type 1757 1758 if t == nil { 1759 goto bad 1760 } 1761 1762 if t.Etype != TFUNC { 1763 goto bad 1764 } 1765 1766 s.Tfunc = t 1767 s.Done = 0 1768 fp = Structfirst(s, getthis(t)) 1769 if fp == nil { 1770 s.Done = 1 1771 fp = Structfirst(s, getinarg(t)) 1772 } 1773 1774 return fp 1775 1776 bad: 1777 Fatalf("funcfirst: not func %v", t) 1778 return nil 1779 } 1780 1781 func funcnext(s *Iter) *Type { 1782 fp := structnext(s) 1783 if fp == nil && s.Done == 0 { 1784 s.Done = 1 1785 fp = Structfirst(s, getinarg(s.Tfunc)) 1786 } 1787 1788 return fp 1789 } 1790 1791 func getthis(t *Type) **Type { 1792 if t.Etype != TFUNC { 1793 Fatalf("getthis: not a func %v", t) 1794 } 1795 return &t.Type 1796 } 1797 1798 func Getoutarg(t *Type) **Type { 1799 if t.Etype != TFUNC { 1800 Fatalf("getoutarg: not a func %v", t) 1801 } 1802 return &t.Type.Down 1803 } 1804 1805 func getinarg(t *Type) **Type { 1806 if t.Etype != TFUNC { 1807 Fatalf("getinarg: not a func %v", t) 1808 } 1809 return &t.Type.Down.Down 1810 } 1811 1812 func getthisx(t *Type) *Type { 1813 return *getthis(t) 1814 } 1815 1816 func getoutargx(t *Type) *Type { 1817 return *Getoutarg(t) 1818 } 1819 1820 func getinargx(t *Type) *Type { 1821 return *getinarg(t) 1822 } 1823 1824 // Brcom returns !(op). 1825 // For example, Brcom(==) is !=. 1826 func Brcom(a int) int { 1827 switch a { 1828 case OEQ: 1829 return ONE 1830 case ONE: 1831 return OEQ 1832 case OLT: 1833 return OGE 1834 case OGT: 1835 return OLE 1836 case OLE: 1837 return OGT 1838 case OGE: 1839 return OLT 1840 } 1841 Fatalf("brcom: no com for %v\n", Oconv(a, 0)) 1842 return a 1843 } 1844 1845 // Brrev returns reverse(op). 1846 // For example, Brrev(<) is >. 1847 func Brrev(a int) int { 1848 switch a { 1849 case OEQ: 1850 return OEQ 1851 case ONE: 1852 return ONE 1853 case OLT: 1854 return OGT 1855 case OGT: 1856 return OLT 1857 case OLE: 1858 return OGE 1859 case OGE: 1860 return OLE 1861 } 1862 Fatalf("brrev: no rev for %v\n", Oconv(a, 0)) 1863 return a 1864 } 1865 1866 /* 1867 * return side effect-free n, appending side effects to init. 1868 * result is assignable if n is. 1869 */ 1870 func safeexpr(n *Node, init **NodeList) *Node { 1871 if n == nil { 1872 return nil 1873 } 1874 1875 if n.Ninit != nil { 1876 walkstmtlist(n.Ninit) 1877 *init = concat(*init, n.Ninit) 1878 n.Ninit = nil 1879 } 1880 1881 switch n.Op { 1882 case ONAME, OLITERAL: 1883 return n 1884 1885 case ODOT, OLEN, OCAP: 1886 l := safeexpr(n.Left, init) 1887 if l == n.Left { 1888 return n 1889 } 1890 r := Nod(OXXX, nil, nil) 1891 *r = *n 1892 r.Left = l 1893 typecheck(&r, Erv) 1894 walkexpr(&r, init) 1895 return r 1896 1897 case ODOTPTR, OIND: 1898 l := safeexpr(n.Left, init) 1899 if l == n.Left { 1900 return n 1901 } 1902 a := Nod(OXXX, nil, nil) 1903 *a = *n 1904 a.Left = l 1905 walkexpr(&a, init) 1906 return a 1907 1908 case OINDEX, OINDEXMAP: 1909 l := safeexpr(n.Left, init) 1910 r := safeexpr(n.Right, init) 1911 if l == n.Left && r == n.Right { 1912 return n 1913 } 1914 a := Nod(OXXX, nil, nil) 1915 *a = *n 1916 a.Left = l 1917 a.Right = r 1918 walkexpr(&a, init) 1919 return a 1920 } 1921 1922 // make a copy; must not be used as an lvalue 1923 if islvalue(n) { 1924 Fatalf("missing lvalue case in safeexpr: %v", n) 1925 } 1926 return cheapexpr(n, init) 1927 } 1928 1929 func copyexpr(n *Node, t *Type, init **NodeList) *Node { 1930 l := temp(t) 1931 a := Nod(OAS, l, n) 1932 typecheck(&a, Etop) 1933 walkexpr(&a, init) 1934 *init = list(*init, a) 1935 return l 1936 } 1937 1938 /* 1939 * return side-effect free and cheap n, appending side effects to init. 1940 * result may not be assignable. 1941 */ 1942 func cheapexpr(n *Node, init **NodeList) *Node { 1943 switch n.Op { 1944 case ONAME, OLITERAL: 1945 return n 1946 } 1947 1948 return copyexpr(n, n.Type, init) 1949 } 1950 1951 func Setmaxarg(t *Type, extra int32) { 1952 dowidth(t) 1953 w := t.Argwid 1954 if w >= Thearch.MAXWIDTH { 1955 Fatalf("bad argwid %v", t) 1956 } 1957 w += int64(extra) 1958 if w >= Thearch.MAXWIDTH { 1959 Fatalf("bad argwid %d + %v", extra, t) 1960 } 1961 if w > Maxarg { 1962 Maxarg = w 1963 } 1964 } 1965 1966 /* 1967 * unicode-aware case-insensitive strcmp 1968 */ 1969 1970 /* 1971 * code to resolve elided DOTs 1972 * in embedded types 1973 */ 1974 1975 // search depth 0 -- 1976 // return count of fields+methods 1977 // found with a given name 1978 func lookdot0(s *Sym, t *Type, save **Type, ignorecase int) int { 1979 u := t 1980 if Isptr[u.Etype] { 1981 u = u.Type 1982 } 1983 1984 c := 0 1985 if u.Etype == TSTRUCT || u.Etype == TINTER { 1986 for f := u.Type; f != nil; f = f.Down { 1987 if f.Sym == s || (ignorecase != 0 && f.Type.Etype == TFUNC && f.Type.Thistuple > 0 && strings.EqualFold(f.Sym.Name, s.Name)) { 1988 if save != nil { 1989 *save = f 1990 } 1991 c++ 1992 } 1993 } 1994 } 1995 1996 u = methtype(t, 0) 1997 if u != nil { 1998 for f := u.Method; f != nil; f = f.Down { 1999 if f.Embedded == 0 && (f.Sym == s || (ignorecase != 0 && strings.EqualFold(f.Sym.Name, s.Name))) { 2000 if save != nil { 2001 *save = f 2002 } 2003 c++ 2004 } 2005 } 2006 } 2007 2008 return c 2009 } 2010 2011 // search depth d for field/method s -- 2012 // return count of fields+methods 2013 // found at search depth. 2014 // answer is in dotlist array and 2015 // count of number of ways is returned. 2016 func adddot1(s *Sym, t *Type, d int, save **Type, ignorecase int) int { 2017 if t.Trecur != 0 { 2018 return 0 2019 } 2020 t.Trecur = 1 2021 2022 var c int 2023 var u *Type 2024 var a int 2025 if d == 0 { 2026 c = lookdot0(s, t, save, ignorecase) 2027 goto out 2028 } 2029 2030 c = 0 2031 u = t 2032 if Isptr[u.Etype] { 2033 u = u.Type 2034 } 2035 if u.Etype != TSTRUCT && u.Etype != TINTER { 2036 goto out 2037 } 2038 2039 d-- 2040 for f := u.Type; f != nil; f = f.Down { 2041 if f.Embedded == 0 { 2042 continue 2043 } 2044 if f.Sym == nil { 2045 continue 2046 } 2047 a = adddot1(s, f.Type, d, save, ignorecase) 2048 if a != 0 && c == 0 { 2049 dotlist[d].field = f 2050 } 2051 c += a 2052 } 2053 2054 out: 2055 t.Trecur = 0 2056 return c 2057 } 2058 2059 // in T.field 2060 // find missing fields that 2061 // will give shortest unique addressing. 2062 // modify the tree with missing type names. 2063 func adddot(n *Node) *Node { 2064 typecheck(&n.Left, Etype|Erv) 2065 n.Diag |= n.Left.Diag 2066 t := n.Left.Type 2067 if t == nil { 2068 return n 2069 } 2070 2071 if n.Left.Op == OTYPE { 2072 return n 2073 } 2074 2075 if n.Right.Op != ONAME { 2076 return n 2077 } 2078 s := n.Right.Sym 2079 if s == nil { 2080 return n 2081 } 2082 2083 var c int 2084 for d := 0; d < len(dotlist); d++ { 2085 c = adddot1(s, t, d, nil, 0) 2086 if c > 0 { 2087 if c > 1 { 2088 Yyerror("ambiguous selector %v", n) 2089 n.Left = nil 2090 return n 2091 } 2092 2093 // rebuild elided dots 2094 for c := d - 1; c >= 0; c-- { 2095 n.Left = Nod(ODOT, n.Left, newname(dotlist[c].field.Sym)) 2096 n.Left.Implicit = true 2097 } 2098 2099 return n 2100 } 2101 } 2102 2103 return n 2104 } 2105 2106 /* 2107 * code to help generate trampoline 2108 * functions for methods on embedded 2109 * subtypes. 2110 * these are approx the same as 2111 * the corresponding adddot routines 2112 * except that they expect to be called 2113 * with unique tasks and they return 2114 * the actual methods. 2115 */ 2116 type Symlink struct { 2117 field *Type 2118 link *Symlink 2119 good bool 2120 followptr bool 2121 } 2122 2123 var slist *Symlink 2124 2125 func expand0(t *Type, followptr bool) { 2126 u := t 2127 if Isptr[u.Etype] { 2128 followptr = true 2129 u = u.Type 2130 } 2131 2132 if u.Etype == TINTER { 2133 var sl *Symlink 2134 for f := u.Type; f != nil; f = f.Down { 2135 if f.Sym.Flags&SymUniq != 0 { 2136 continue 2137 } 2138 f.Sym.Flags |= SymUniq 2139 sl = new(Symlink) 2140 sl.field = f 2141 sl.link = slist 2142 sl.followptr = followptr 2143 slist = sl 2144 } 2145 2146 return 2147 } 2148 2149 u = methtype(t, 0) 2150 if u != nil { 2151 var sl *Symlink 2152 for f := u.Method; f != nil; f = f.Down { 2153 if f.Sym.Flags&SymUniq != 0 { 2154 continue 2155 } 2156 f.Sym.Flags |= SymUniq 2157 sl = new(Symlink) 2158 sl.field = f 2159 sl.link = slist 2160 sl.followptr = followptr 2161 slist = sl 2162 } 2163 } 2164 } 2165 2166 func expand1(t *Type, d int, followptr bool) { 2167 if t.Trecur != 0 { 2168 return 2169 } 2170 if d == 0 { 2171 return 2172 } 2173 t.Trecur = 1 2174 2175 if d != len(dotlist)-1 { 2176 expand0(t, followptr) 2177 } 2178 2179 u := t 2180 if Isptr[u.Etype] { 2181 followptr = true 2182 u = u.Type 2183 } 2184 2185 if u.Etype != TSTRUCT && u.Etype != TINTER { 2186 goto out 2187 } 2188 2189 for f := u.Type; f != nil; f = f.Down { 2190 if f.Embedded == 0 { 2191 continue 2192 } 2193 if f.Sym == nil { 2194 continue 2195 } 2196 expand1(f.Type, d-1, followptr) 2197 } 2198 2199 out: 2200 t.Trecur = 0 2201 } 2202 2203 func expandmeth(t *Type) { 2204 if t == nil || t.Xmethod != nil { 2205 return 2206 } 2207 2208 // mark top-level method symbols 2209 // so that expand1 doesn't consider them. 2210 var f *Type 2211 for f = t.Method; f != nil; f = f.Down { 2212 f.Sym.Flags |= SymUniq 2213 } 2214 2215 // generate all reachable methods 2216 slist = nil 2217 2218 expand1(t, len(dotlist)-1, false) 2219 2220 // check each method to be uniquely reachable 2221 var c int 2222 var d int 2223 for sl := slist; sl != nil; sl = sl.link { 2224 sl.field.Sym.Flags &^= SymUniq 2225 for d = 0; d < len(dotlist); d++ { 2226 c = adddot1(sl.field.Sym, t, d, &f, 0) 2227 if c == 0 { 2228 continue 2229 } 2230 if c == 1 { 2231 // addot1 may have dug out arbitrary fields, we only want methods. 2232 if f.Type.Etype == TFUNC && f.Type.Thistuple > 0 { 2233 sl.good = true 2234 sl.field = f 2235 } 2236 } 2237 2238 break 2239 } 2240 } 2241 2242 for f = t.Method; f != nil; f = f.Down { 2243 f.Sym.Flags &^= SymUniq 2244 } 2245 2246 t.Xmethod = t.Method 2247 for sl := slist; sl != nil; sl = sl.link { 2248 if sl.good { 2249 // add it to the base type method list 2250 f = typ(TFIELD) 2251 2252 *f = *sl.field 2253 f.Embedded = 1 // needs a trampoline 2254 if sl.followptr { 2255 f.Embedded = 2 2256 } 2257 f.Down = t.Xmethod 2258 t.Xmethod = f 2259 } 2260 } 2261 } 2262 2263 /* 2264 * Given funarg struct list, return list of ODCLFIELD Node fn args. 2265 */ 2266 func structargs(tl **Type, mustname int) *NodeList { 2267 var savet Iter 2268 var a *Node 2269 var n *Node 2270 var buf string 2271 2272 var args *NodeList 2273 gen := 0 2274 for t := Structfirst(&savet, tl); t != nil; t = structnext(&savet) { 2275 n = nil 2276 if mustname != 0 && (t.Sym == nil || t.Sym.Name == "_") { 2277 // invent a name so that we can refer to it in the trampoline 2278 buf = fmt.Sprintf(".anon%d", gen) 2279 gen++ 2280 2281 n = newname(Lookup(buf)) 2282 } else if t.Sym != nil { 2283 n = newname(t.Sym) 2284 } 2285 a = Nod(ODCLFIELD, n, typenod(t.Type)) 2286 a.Isddd = t.Isddd 2287 if n != nil { 2288 n.Isddd = t.Isddd 2289 } 2290 args = list(args, a) 2291 } 2292 2293 return args 2294 } 2295 2296 /* 2297 * Generate a wrapper function to convert from 2298 * a receiver of type T to a receiver of type U. 2299 * That is, 2300 * 2301 * func (t T) M() { 2302 * ... 2303 * } 2304 * 2305 * already exists; this function generates 2306 * 2307 * func (u U) M() { 2308 * u.M() 2309 * } 2310 * 2311 * where the types T and U are such that u.M() is valid 2312 * and calls the T.M method. 2313 * The resulting function is for use in method tables. 2314 * 2315 * rcvr - U 2316 * method - M func (t T)(), a TFIELD type struct 2317 * newnam - the eventual mangled name of this function 2318 */ 2319 2320 var genwrapper_linehistdone int = 0 2321 2322 func genwrapper(rcvr *Type, method *Type, newnam *Sym, iface int) { 2323 if false && Debug['r'] != 0 { 2324 fmt.Printf("genwrapper rcvrtype=%v method=%v newnam=%v\n", rcvr, method, newnam) 2325 } 2326 2327 lexlineno++ 2328 lineno = lexlineno 2329 if genwrapper_linehistdone == 0 { 2330 // All the wrappers can share the same linehist entry. 2331 linehistpush("<autogenerated>") 2332 2333 genwrapper_linehistdone = 1 2334 } 2335 2336 dclcontext = PEXTERN 2337 markdcl() 2338 2339 this := Nod(ODCLFIELD, newname(Lookup(".this")), typenod(rcvr)) 2340 this.Left.Name.Param.Ntype = this.Right 2341 in := structargs(getinarg(method.Type), 1) 2342 out := structargs(Getoutarg(method.Type), 0) 2343 2344 t := Nod(OTFUNC, nil, nil) 2345 l := list1(this) 2346 if iface != 0 && rcvr.Width < Types[Tptr].Width { 2347 // Building method for interface table and receiver 2348 // is smaller than the single pointer-sized word 2349 // that the interface call will pass in. 2350 // Add a dummy padding argument after the 2351 // receiver to make up the difference. 2352 tpad := typ(TARRAY) 2353 2354 tpad.Type = Types[TUINT8] 2355 tpad.Bound = Types[Tptr].Width - rcvr.Width 2356 pad := Nod(ODCLFIELD, newname(Lookup(".pad")), typenod(tpad)) 2357 l = list(l, pad) 2358 } 2359 2360 t.List = concat(l, in) 2361 t.Rlist = out 2362 2363 fn := Nod(ODCLFUNC, nil, nil) 2364 fn.Func.Nname = newname(newnam) 2365 fn.Func.Nname.Name.Defn = fn 2366 fn.Func.Nname.Name.Param.Ntype = t 2367 declare(fn.Func.Nname, PFUNC) 2368 funchdr(fn) 2369 2370 // arg list 2371 var args *NodeList 2372 2373 isddd := false 2374 for l := in; l != nil; l = l.Next { 2375 args = list(args, l.N.Left) 2376 isddd = l.N.Left.Isddd 2377 } 2378 2379 methodrcvr := getthisx(method.Type).Type.Type 2380 2381 // generate nil pointer check for better error 2382 if Isptr[rcvr.Etype] && rcvr.Type == methodrcvr { 2383 // generating wrapper from *T to T. 2384 n := Nod(OIF, nil, nil) 2385 2386 n.Left = Nod(OEQ, this.Left, nodnil()) 2387 2388 // these strings are already in the reflect tables, 2389 // so no space cost to use them here. 2390 var l *NodeList 2391 2392 var v Val 2393 v.U = rcvr.Type.Sym.Pkg.Name // package name 2394 l = list(l, nodlit(v)) 2395 v.U = rcvr.Type.Sym.Name // type name 2396 l = list(l, nodlit(v)) 2397 v.U = method.Sym.Name 2398 l = list(l, nodlit(v)) // method name 2399 call := Nod(OCALL, syslook("panicwrap", 0), nil) 2400 call.List = l 2401 n.Nbody = list1(call) 2402 fn.Nbody = list(fn.Nbody, n) 2403 } 2404 2405 dot := adddot(Nod(OXDOT, this.Left, newname(method.Sym))) 2406 2407 // generate call 2408 if flag_race == 0 && Isptr[rcvr.Etype] && Isptr[methodrcvr.Etype] && method.Embedded != 0 && !isifacemethod(method.Type) { 2409 // generate tail call: adjust pointer receiver and jump to embedded method. 2410 dot = dot.Left // skip final .M 2411 if !Isptr[dotlist[0].field.Type.Etype] { 2412 dot = Nod(OADDR, dot, nil) 2413 } 2414 as := Nod(OAS, this.Left, Nod(OCONVNOP, dot, nil)) 2415 as.Right.Type = rcvr 2416 fn.Nbody = list(fn.Nbody, as) 2417 n := Nod(ORETJMP, nil, nil) 2418 n.Left = newname(methodsym(method.Sym, methodrcvr, 0)) 2419 fn.Nbody = list(fn.Nbody, n) 2420 } else { 2421 fn.Func.Wrapper = true // ignore frame for panic+recover matching 2422 call := Nod(OCALL, dot, nil) 2423 call.List = args 2424 call.Isddd = isddd 2425 if method.Type.Outtuple > 0 { 2426 n := Nod(ORETURN, nil, nil) 2427 n.List = list1(call) 2428 call = n 2429 } 2430 2431 fn.Nbody = list(fn.Nbody, call) 2432 } 2433 2434 if false && Debug['r'] != 0 { 2435 dumplist("genwrapper body", fn.Nbody) 2436 } 2437 2438 funcbody(fn) 2439 Curfn = fn 2440 2441 // wrappers where T is anonymous (struct or interface) can be duplicated. 2442 if rcvr.Etype == TSTRUCT || rcvr.Etype == TINTER || Isptr[rcvr.Etype] && rcvr.Type.Etype == TSTRUCT { 2443 fn.Func.Dupok = true 2444 } 2445 typecheck(&fn, Etop) 2446 typechecklist(fn.Nbody, Etop) 2447 2448 inlcalls(fn) 2449 escAnalyze([]*Node{fn}, false) 2450 2451 Curfn = nil 2452 funccompile(fn) 2453 } 2454 2455 func hashmem(t *Type) *Node { 2456 sym := Pkglookup("memhash", Runtimepkg) 2457 2458 n := newname(sym) 2459 n.Class = PFUNC 2460 tfn := Nod(OTFUNC, nil, nil) 2461 tfn.List = list(tfn.List, Nod(ODCLFIELD, nil, typenod(Ptrto(t)))) 2462 tfn.List = list(tfn.List, Nod(ODCLFIELD, nil, typenod(Types[TUINTPTR]))) 2463 tfn.List = list(tfn.List, Nod(ODCLFIELD, nil, typenod(Types[TUINTPTR]))) 2464 tfn.Rlist = list(tfn.Rlist, Nod(ODCLFIELD, nil, typenod(Types[TUINTPTR]))) 2465 typecheck(&tfn, Etype) 2466 n.Type = tfn.Type 2467 return n 2468 } 2469 2470 func hashfor(t *Type) *Node { 2471 var sym *Sym 2472 2473 a := algtype1(t, nil) 2474 switch a { 2475 case AMEM: 2476 Fatalf("hashfor with AMEM type") 2477 2478 case AINTER: 2479 sym = Pkglookup("interhash", Runtimepkg) 2480 2481 case ANILINTER: 2482 sym = Pkglookup("nilinterhash", Runtimepkg) 2483 2484 case ASTRING: 2485 sym = Pkglookup("strhash", Runtimepkg) 2486 2487 case AFLOAT32: 2488 sym = Pkglookup("f32hash", Runtimepkg) 2489 2490 case AFLOAT64: 2491 sym = Pkglookup("f64hash", Runtimepkg) 2492 2493 case ACPLX64: 2494 sym = Pkglookup("c64hash", Runtimepkg) 2495 2496 case ACPLX128: 2497 sym = Pkglookup("c128hash", Runtimepkg) 2498 2499 default: 2500 sym = typesymprefix(".hash", t) 2501 } 2502 2503 n := newname(sym) 2504 n.Class = PFUNC 2505 tfn := Nod(OTFUNC, nil, nil) 2506 tfn.List = list(tfn.List, Nod(ODCLFIELD, nil, typenod(Ptrto(t)))) 2507 tfn.List = list(tfn.List, Nod(ODCLFIELD, nil, typenod(Types[TUINTPTR]))) 2508 tfn.Rlist = list(tfn.Rlist, Nod(ODCLFIELD, nil, typenod(Types[TUINTPTR]))) 2509 typecheck(&tfn, Etype) 2510 n.Type = tfn.Type 2511 return n 2512 } 2513 2514 /* 2515 * Generate a helper function to compute the hash of a value of type t. 2516 */ 2517 func genhash(sym *Sym, t *Type) { 2518 if Debug['r'] != 0 { 2519 fmt.Printf("genhash %v %v\n", sym, t) 2520 } 2521 2522 lineno = 1 // less confusing than end of input 2523 dclcontext = PEXTERN 2524 markdcl() 2525 2526 // func sym(p *T, h uintptr) uintptr 2527 fn := Nod(ODCLFUNC, nil, nil) 2528 2529 fn.Func.Nname = newname(sym) 2530 fn.Func.Nname.Class = PFUNC 2531 tfn := Nod(OTFUNC, nil, nil) 2532 fn.Func.Nname.Name.Param.Ntype = tfn 2533 2534 n := Nod(ODCLFIELD, newname(Lookup("p")), typenod(Ptrto(t))) 2535 tfn.List = list(tfn.List, n) 2536 np := n.Left 2537 n = Nod(ODCLFIELD, newname(Lookup("h")), typenod(Types[TUINTPTR])) 2538 tfn.List = list(tfn.List, n) 2539 nh := n.Left 2540 n = Nod(ODCLFIELD, nil, typenod(Types[TUINTPTR])) // return value 2541 tfn.Rlist = list(tfn.Rlist, n) 2542 2543 funchdr(fn) 2544 typecheck(&fn.Func.Nname.Name.Param.Ntype, Etype) 2545 2546 // genhash is only called for types that have equality but 2547 // cannot be handled by the standard algorithms, 2548 // so t must be either an array or a struct. 2549 switch t.Etype { 2550 default: 2551 Fatalf("genhash %v", t) 2552 2553 case TARRAY: 2554 if Isslice(t) { 2555 Fatalf("genhash %v", t) 2556 } 2557 2558 // An array of pure memory would be handled by the 2559 // standard algorithm, so the element type must not be 2560 // pure memory. 2561 hashel := hashfor(t.Type) 2562 2563 n := Nod(ORANGE, nil, Nod(OIND, np, nil)) 2564 ni := newname(Lookup("i")) 2565 ni.Type = Types[TINT] 2566 n.List = list1(ni) 2567 n.Colas = true 2568 colasdefn(n.List, n) 2569 ni = n.List.N 2570 2571 // h = hashel(&p[i], h) 2572 call := Nod(OCALL, hashel, nil) 2573 2574 nx := Nod(OINDEX, np, ni) 2575 nx.Bounded = true 2576 na := Nod(OADDR, nx, nil) 2577 na.Etype = 1 // no escape to heap 2578 call.List = list(call.List, na) 2579 call.List = list(call.List, nh) 2580 n.Nbody = list(n.Nbody, Nod(OAS, nh, call)) 2581 2582 fn.Nbody = list(fn.Nbody, n) 2583 2584 // Walk the struct using memhash for runs of AMEM 2585 // and calling specific hash functions for the others. 2586 case TSTRUCT: 2587 var first *Type 2588 2589 offend := int64(0) 2590 var size int64 2591 var call *Node 2592 var nx *Node 2593 var na *Node 2594 var hashel *Node 2595 for t1 := t.Type; ; t1 = t1.Down { 2596 if t1 != nil && algtype1(t1.Type, nil) == AMEM && !isblanksym(t1.Sym) { 2597 offend = t1.Width + t1.Type.Width 2598 if first == nil { 2599 first = t1 2600 } 2601 2602 // If it's a memory field but it's padded, stop here. 2603 if ispaddedfield(t1, t.Width) { 2604 t1 = t1.Down 2605 } else { 2606 continue 2607 } 2608 } 2609 2610 // Run memhash for fields up to this one. 2611 if first != nil { 2612 size = offend - first.Width // first->width is offset 2613 hashel = hashmem(first.Type) 2614 2615 // h = hashel(&p.first, size, h) 2616 call = Nod(OCALL, hashel, nil) 2617 2618 nx = Nod(OXDOT, np, newname(first.Sym)) // TODO: fields from other packages? 2619 na = Nod(OADDR, nx, nil) 2620 na.Etype = 1 // no escape to heap 2621 call.List = list(call.List, na) 2622 call.List = list(call.List, nh) 2623 call.List = list(call.List, Nodintconst(size)) 2624 fn.Nbody = list(fn.Nbody, Nod(OAS, nh, call)) 2625 2626 first = nil 2627 } 2628 2629 if t1 == nil { 2630 break 2631 } 2632 if isblanksym(t1.Sym) { 2633 continue 2634 } 2635 2636 // Run hash for this field. 2637 if algtype1(t1.Type, nil) == AMEM { 2638 hashel = hashmem(t1.Type) 2639 2640 // h = memhash(&p.t1, h, size) 2641 call = Nod(OCALL, hashel, nil) 2642 2643 nx = Nod(OXDOT, np, newname(t1.Sym)) // TODO: fields from other packages? 2644 na = Nod(OADDR, nx, nil) 2645 na.Etype = 1 // no escape to heap 2646 call.List = list(call.List, na) 2647 call.List = list(call.List, nh) 2648 call.List = list(call.List, Nodintconst(t1.Type.Width)) 2649 fn.Nbody = list(fn.Nbody, Nod(OAS, nh, call)) 2650 } else { 2651 hashel = hashfor(t1.Type) 2652 2653 // h = hashel(&p.t1, h) 2654 call = Nod(OCALL, hashel, nil) 2655 2656 nx = Nod(OXDOT, np, newname(t1.Sym)) // TODO: fields from other packages? 2657 na = Nod(OADDR, nx, nil) 2658 na.Etype = 1 // no escape to heap 2659 call.List = list(call.List, na) 2660 call.List = list(call.List, nh) 2661 fn.Nbody = list(fn.Nbody, Nod(OAS, nh, call)) 2662 } 2663 } 2664 } 2665 2666 r := Nod(ORETURN, nil, nil) 2667 r.List = list(r.List, nh) 2668 fn.Nbody = list(fn.Nbody, r) 2669 2670 if Debug['r'] != 0 { 2671 dumplist("genhash body", fn.Nbody) 2672 } 2673 2674 funcbody(fn) 2675 Curfn = fn 2676 fn.Func.Dupok = true 2677 typecheck(&fn, Etop) 2678 typechecklist(fn.Nbody, Etop) 2679 Curfn = nil 2680 2681 // Disable safemode while compiling this code: the code we 2682 // generate internally can refer to unsafe.Pointer. 2683 // In this case it can happen if we need to generate an == 2684 // for a struct containing a reflect.Value, which itself has 2685 // an unexported field of type unsafe.Pointer. 2686 old_safemode := safemode 2687 2688 safemode = 0 2689 funccompile(fn) 2690 safemode = old_safemode 2691 } 2692 2693 // Return node for 2694 // if p.field != q.field { return false } 2695 func eqfield(p *Node, q *Node, field *Node) *Node { 2696 nx := Nod(OXDOT, p, field) 2697 ny := Nod(OXDOT, q, field) 2698 nif := Nod(OIF, nil, nil) 2699 nif.Left = Nod(ONE, nx, ny) 2700 r := Nod(ORETURN, nil, nil) 2701 r.List = list(r.List, Nodbool(false)) 2702 nif.Nbody = list(nif.Nbody, r) 2703 return nif 2704 } 2705 2706 func eqmemfunc(size int64, type_ *Type, needsize *int) *Node { 2707 var fn *Node 2708 2709 switch size { 2710 default: 2711 fn = syslook("memequal", 1) 2712 *needsize = 1 2713 2714 case 1, 2, 4, 8, 16: 2715 buf := fmt.Sprintf("memequal%d", int(size)*8) 2716 fn = syslook(buf, 1) 2717 *needsize = 0 2718 } 2719 2720 substArgTypes(fn, type_, type_) 2721 return fn 2722 } 2723 2724 // Return node for 2725 // if !memequal(&p.field, &q.field [, size]) { return false } 2726 func eqmem(p *Node, q *Node, field *Node, size int64) *Node { 2727 var needsize int 2728 2729 nx := Nod(OADDR, Nod(OXDOT, p, field), nil) 2730 nx.Etype = 1 // does not escape 2731 ny := Nod(OADDR, Nod(OXDOT, q, field), nil) 2732 ny.Etype = 1 // does not escape 2733 typecheck(&nx, Erv) 2734 typecheck(&ny, Erv) 2735 2736 call := Nod(OCALL, eqmemfunc(size, nx.Type.Type, &needsize), nil) 2737 call.List = list(call.List, nx) 2738 call.List = list(call.List, ny) 2739 if needsize != 0 { 2740 call.List = list(call.List, Nodintconst(size)) 2741 } 2742 2743 nif := Nod(OIF, nil, nil) 2744 nif.Left = Nod(ONOT, call, nil) 2745 r := Nod(ORETURN, nil, nil) 2746 r.List = list(r.List, Nodbool(false)) 2747 nif.Nbody = list(nif.Nbody, r) 2748 return nif 2749 } 2750 2751 /* 2752 * Generate a helper function to check equality of two values of type t. 2753 */ 2754 func geneq(sym *Sym, t *Type) { 2755 if Debug['r'] != 0 { 2756 fmt.Printf("geneq %v %v\n", sym, t) 2757 } 2758 2759 lineno = 1 // less confusing than end of input 2760 dclcontext = PEXTERN 2761 markdcl() 2762 2763 // func sym(p, q *T) bool 2764 fn := Nod(ODCLFUNC, nil, nil) 2765 2766 fn.Func.Nname = newname(sym) 2767 fn.Func.Nname.Class = PFUNC 2768 tfn := Nod(OTFUNC, nil, nil) 2769 fn.Func.Nname.Name.Param.Ntype = tfn 2770 2771 n := Nod(ODCLFIELD, newname(Lookup("p")), typenod(Ptrto(t))) 2772 tfn.List = list(tfn.List, n) 2773 np := n.Left 2774 n = Nod(ODCLFIELD, newname(Lookup("q")), typenod(Ptrto(t))) 2775 tfn.List = list(tfn.List, n) 2776 nq := n.Left 2777 n = Nod(ODCLFIELD, nil, typenod(Types[TBOOL])) 2778 tfn.Rlist = list(tfn.Rlist, n) 2779 2780 funchdr(fn) 2781 2782 // geneq is only called for types that have equality but 2783 // cannot be handled by the standard algorithms, 2784 // so t must be either an array or a struct. 2785 switch t.Etype { 2786 default: 2787 Fatalf("geneq %v", t) 2788 2789 case TARRAY: 2790 if Isslice(t) { 2791 Fatalf("geneq %v", t) 2792 } 2793 2794 // An array of pure memory would be handled by the 2795 // standard memequal, so the element type must not be 2796 // pure memory. Even if we unrolled the range loop, 2797 // each iteration would be a function call, so don't bother 2798 // unrolling. 2799 nrange := Nod(ORANGE, nil, Nod(OIND, np, nil)) 2800 2801 ni := newname(Lookup("i")) 2802 ni.Type = Types[TINT] 2803 nrange.List = list1(ni) 2804 nrange.Colas = true 2805 colasdefn(nrange.List, nrange) 2806 ni = nrange.List.N 2807 2808 // if p[i] != q[i] { return false } 2809 nx := Nod(OINDEX, np, ni) 2810 2811 nx.Bounded = true 2812 ny := Nod(OINDEX, nq, ni) 2813 ny.Bounded = true 2814 2815 nif := Nod(OIF, nil, nil) 2816 nif.Left = Nod(ONE, nx, ny) 2817 r := Nod(ORETURN, nil, nil) 2818 r.List = list(r.List, Nodbool(false)) 2819 nif.Nbody = list(nif.Nbody, r) 2820 nrange.Nbody = list(nrange.Nbody, nif) 2821 fn.Nbody = list(fn.Nbody, nrange) 2822 2823 // Walk the struct using memequal for runs of AMEM 2824 // and calling specific equality tests for the others. 2825 // Skip blank-named fields. 2826 case TSTRUCT: 2827 var first *Type 2828 2829 offend := int64(0) 2830 var size int64 2831 for t1 := t.Type; ; t1 = t1.Down { 2832 if t1 != nil && algtype1(t1.Type, nil) == AMEM && !isblanksym(t1.Sym) { 2833 offend = t1.Width + t1.Type.Width 2834 if first == nil { 2835 first = t1 2836 } 2837 2838 // If it's a memory field but it's padded, stop here. 2839 if ispaddedfield(t1, t.Width) { 2840 t1 = t1.Down 2841 } else { 2842 continue 2843 } 2844 } 2845 2846 // Run memequal for fields up to this one. 2847 // TODO(rsc): All the calls to newname are wrong for 2848 // cross-package unexported fields. 2849 if first != nil { 2850 if first.Down == t1 { 2851 fn.Nbody = list(fn.Nbody, eqfield(np, nq, newname(first.Sym))) 2852 } else if first.Down.Down == t1 { 2853 fn.Nbody = list(fn.Nbody, eqfield(np, nq, newname(first.Sym))) 2854 first = first.Down 2855 if !isblanksym(first.Sym) { 2856 fn.Nbody = list(fn.Nbody, eqfield(np, nq, newname(first.Sym))) 2857 } 2858 } else { 2859 // More than two fields: use memequal. 2860 size = offend - first.Width // first->width is offset 2861 fn.Nbody = list(fn.Nbody, eqmem(np, nq, newname(first.Sym), size)) 2862 } 2863 2864 first = nil 2865 } 2866 2867 if t1 == nil { 2868 break 2869 } 2870 if isblanksym(t1.Sym) { 2871 continue 2872 } 2873 2874 // Check this field, which is not just memory. 2875 fn.Nbody = list(fn.Nbody, eqfield(np, nq, newname(t1.Sym))) 2876 } 2877 } 2878 2879 // return true 2880 r := Nod(ORETURN, nil, nil) 2881 2882 r.List = list(r.List, Nodbool(true)) 2883 fn.Nbody = list(fn.Nbody, r) 2884 2885 if Debug['r'] != 0 { 2886 dumplist("geneq body", fn.Nbody) 2887 } 2888 2889 funcbody(fn) 2890 Curfn = fn 2891 fn.Func.Dupok = true 2892 typecheck(&fn, Etop) 2893 typechecklist(fn.Nbody, Etop) 2894 Curfn = nil 2895 2896 // Disable safemode while compiling this code: the code we 2897 // generate internally can refer to unsafe.Pointer. 2898 // In this case it can happen if we need to generate an == 2899 // for a struct containing a reflect.Value, which itself has 2900 // an unexported field of type unsafe.Pointer. 2901 old_safemode := safemode 2902 2903 safemode = 0 2904 funccompile(fn) 2905 safemode = old_safemode 2906 } 2907 2908 func ifacelookdot(s *Sym, t *Type, followptr *bool, ignorecase int) *Type { 2909 *followptr = false 2910 2911 if t == nil { 2912 return nil 2913 } 2914 2915 var m *Type 2916 var i int 2917 var c int 2918 for d := 0; d < len(dotlist); d++ { 2919 c = adddot1(s, t, d, &m, ignorecase) 2920 if c > 1 { 2921 Yyerror("%v.%v is ambiguous", t, s) 2922 return nil 2923 } 2924 2925 if c == 1 { 2926 for i = 0; i < d; i++ { 2927 if Isptr[dotlist[i].field.Type.Etype] { 2928 *followptr = true 2929 break 2930 } 2931 } 2932 2933 if m.Type.Etype != TFUNC || m.Type.Thistuple == 0 { 2934 Yyerror("%v.%v is a field, not a method", t, s) 2935 return nil 2936 } 2937 2938 return m 2939 } 2940 } 2941 2942 return nil 2943 } 2944 2945 func implements(t *Type, iface *Type, m **Type, samename **Type, ptr *int) bool { 2946 t0 := t 2947 if t == nil { 2948 return false 2949 } 2950 2951 // if this is too slow, 2952 // could sort these first 2953 // and then do one loop. 2954 2955 if t.Etype == TINTER { 2956 var tm *Type 2957 for im := iface.Type; im != nil; im = im.Down { 2958 for tm = t.Type; tm != nil; tm = tm.Down { 2959 if tm.Sym == im.Sym { 2960 if Eqtype(tm.Type, im.Type) { 2961 goto found 2962 } 2963 *m = im 2964 *samename = tm 2965 *ptr = 0 2966 return false 2967 } 2968 } 2969 2970 *m = im 2971 *samename = nil 2972 *ptr = 0 2973 return false 2974 found: 2975 } 2976 2977 return true 2978 } 2979 2980 t = methtype(t, 0) 2981 if t != nil { 2982 expandmeth(t) 2983 } 2984 var tm *Type 2985 var imtype *Type 2986 var followptr bool 2987 var rcvr *Type 2988 for im := iface.Type; im != nil; im = im.Down { 2989 if im.Broke { 2990 continue 2991 } 2992 imtype = methodfunc(im.Type, nil) 2993 tm = ifacelookdot(im.Sym, t, &followptr, 0) 2994 if tm == nil || tm.Nointerface || !Eqtype(methodfunc(tm.Type, nil), imtype) { 2995 if tm == nil { 2996 tm = ifacelookdot(im.Sym, t, &followptr, 1) 2997 } 2998 *m = im 2999 *samename = tm 3000 *ptr = 0 3001 return false 3002 } 3003 3004 // if pointer receiver in method, 3005 // the method does not exist for value types. 3006 rcvr = getthisx(tm.Type).Type.Type 3007 3008 if Isptr[rcvr.Etype] && !Isptr[t0.Etype] && !followptr && !isifacemethod(tm.Type) { 3009 if false && Debug['r'] != 0 { 3010 Yyerror("interface pointer mismatch") 3011 } 3012 3013 *m = im 3014 *samename = nil 3015 *ptr = 1 3016 return false 3017 } 3018 } 3019 3020 return true 3021 } 3022 3023 /* 3024 * even simpler simtype; get rid of ptr, bool. 3025 * assuming that the front end has rejected 3026 * all the invalid conversions (like ptr -> bool) 3027 */ 3028 func Simsimtype(t *Type) int { 3029 if t == nil { 3030 return 0 3031 } 3032 3033 et := int(Simtype[t.Etype]) 3034 switch et { 3035 case TPTR32: 3036 et = TUINT32 3037 3038 case TPTR64: 3039 et = TUINT64 3040 3041 case TBOOL: 3042 et = TUINT8 3043 } 3044 3045 return et 3046 } 3047 3048 func listtreecopy(l *NodeList, lineno int32) *NodeList { 3049 var out *NodeList 3050 for ; l != nil; l = l.Next { 3051 out = list(out, treecopy(l.N, lineno)) 3052 } 3053 return out 3054 } 3055 3056 func liststmt(l *NodeList) *Node { 3057 n := Nod(OBLOCK, nil, nil) 3058 n.List = l 3059 if l != nil { 3060 n.Lineno = l.N.Lineno 3061 } 3062 return n 3063 } 3064 3065 /* 3066 * return nelem of list 3067 */ 3068 func structcount(t *Type) int { 3069 var s Iter 3070 3071 v := 0 3072 for t = Structfirst(&s, &t); t != nil; t = structnext(&s) { 3073 v++ 3074 } 3075 return v 3076 } 3077 3078 /* 3079 * return power of 2 of the constant 3080 * operand. -1 if it is not a power of 2. 3081 * 1000+ if it is a -(power of 2) 3082 */ 3083 func powtwo(n *Node) int { 3084 if n == nil || n.Op != OLITERAL || n.Type == nil { 3085 return -1 3086 } 3087 if !Isint[n.Type.Etype] { 3088 return -1 3089 } 3090 3091 v := uint64(Mpgetfix(n.Val().U.(*Mpint))) 3092 b := uint64(1) 3093 for i := 0; i < 64; i++ { 3094 if b == v { 3095 return i 3096 } 3097 b = b << 1 3098 } 3099 3100 if !Issigned[n.Type.Etype] { 3101 return -1 3102 } 3103 3104 v = -v 3105 b = 1 3106 for i := 0; i < 64; i++ { 3107 if b == v { 3108 return i + 1000 3109 } 3110 b = b << 1 3111 } 3112 3113 return -1 3114 } 3115 3116 /* 3117 * return the unsigned type for 3118 * a signed integer type. 3119 * returns T if input is not a 3120 * signed integer type. 3121 */ 3122 func tounsigned(t *Type) *Type { 3123 // this is types[et+1], but not sure 3124 // that this relation is immutable 3125 switch t.Etype { 3126 default: 3127 fmt.Printf("tounsigned: unknown type %v\n", t) 3128 t = nil 3129 3130 case TINT: 3131 t = Types[TUINT] 3132 3133 case TINT8: 3134 t = Types[TUINT8] 3135 3136 case TINT16: 3137 t = Types[TUINT16] 3138 3139 case TINT32: 3140 t = Types[TUINT32] 3141 3142 case TINT64: 3143 t = Types[TUINT64] 3144 } 3145 3146 return t 3147 } 3148 3149 /* 3150 * magic number for signed division 3151 * see hacker's delight chapter 10 3152 */ 3153 func Smagic(m *Magic) { 3154 var mask uint64 3155 3156 m.Bad = 0 3157 switch m.W { 3158 default: 3159 m.Bad = 1 3160 return 3161 3162 case 8: 3163 mask = 0xff 3164 3165 case 16: 3166 mask = 0xffff 3167 3168 case 32: 3169 mask = 0xffffffff 3170 3171 case 64: 3172 mask = 0xffffffffffffffff 3173 } 3174 3175 two31 := mask ^ (mask >> 1) 3176 3177 p := m.W - 1 3178 ad := uint64(m.Sd) 3179 if m.Sd < 0 { 3180 ad = -uint64(m.Sd) 3181 } 3182 3183 // bad denominators 3184 if ad == 0 || ad == 1 || ad == two31 { 3185 m.Bad = 1 3186 return 3187 } 3188 3189 t := two31 3190 ad &= mask 3191 3192 anc := t - 1 - t%ad 3193 anc &= mask 3194 3195 q1 := two31 / anc 3196 r1 := two31 - q1*anc 3197 q1 &= mask 3198 r1 &= mask 3199 3200 q2 := two31 / ad 3201 r2 := two31 - q2*ad 3202 q2 &= mask 3203 r2 &= mask 3204 3205 var delta uint64 3206 for { 3207 p++ 3208 q1 <<= 1 3209 r1 <<= 1 3210 q1 &= mask 3211 r1 &= mask 3212 if r1 >= anc { 3213 q1++ 3214 r1 -= anc 3215 q1 &= mask 3216 r1 &= mask 3217 } 3218 3219 q2 <<= 1 3220 r2 <<= 1 3221 q2 &= mask 3222 r2 &= mask 3223 if r2 >= ad { 3224 q2++ 3225 r2 -= ad 3226 q2 &= mask 3227 r2 &= mask 3228 } 3229 3230 delta = ad - r2 3231 delta &= mask 3232 if q1 < delta || (q1 == delta && r1 == 0) { 3233 continue 3234 } 3235 3236 break 3237 } 3238 3239 m.Sm = int64(q2 + 1) 3240 if uint64(m.Sm)&two31 != 0 { 3241 m.Sm |= ^int64(mask) 3242 } 3243 m.S = p - m.W 3244 } 3245 3246 /* 3247 * magic number for unsigned division 3248 * see hacker's delight chapter 10 3249 */ 3250 func Umagic(m *Magic) { 3251 var mask uint64 3252 3253 m.Bad = 0 3254 m.Ua = 0 3255 3256 switch m.W { 3257 default: 3258 m.Bad = 1 3259 return 3260 3261 case 8: 3262 mask = 0xff 3263 3264 case 16: 3265 mask = 0xffff 3266 3267 case 32: 3268 mask = 0xffffffff 3269 3270 case 64: 3271 mask = 0xffffffffffffffff 3272 } 3273 3274 two31 := mask ^ (mask >> 1) 3275 3276 m.Ud &= mask 3277 if m.Ud == 0 || m.Ud == two31 { 3278 m.Bad = 1 3279 return 3280 } 3281 3282 nc := mask - (-m.Ud&mask)%m.Ud 3283 p := m.W - 1 3284 3285 q1 := two31 / nc 3286 r1 := two31 - q1*nc 3287 q1 &= mask 3288 r1 &= mask 3289 3290 q2 := (two31 - 1) / m.Ud 3291 r2 := (two31 - 1) - q2*m.Ud 3292 q2 &= mask 3293 r2 &= mask 3294 3295 var delta uint64 3296 for { 3297 p++ 3298 if r1 >= nc-r1 { 3299 q1 <<= 1 3300 q1++ 3301 r1 <<= 1 3302 r1 -= nc 3303 } else { 3304 q1 <<= 1 3305 r1 <<= 1 3306 } 3307 3308 q1 &= mask 3309 r1 &= mask 3310 if r2+1 >= m.Ud-r2 { 3311 if q2 >= two31-1 { 3312 m.Ua = 1 3313 } 3314 3315 q2 <<= 1 3316 q2++ 3317 r2 <<= 1 3318 r2++ 3319 r2 -= m.Ud 3320 } else { 3321 if q2 >= two31 { 3322 m.Ua = 1 3323 } 3324 3325 q2 <<= 1 3326 r2 <<= 1 3327 r2++ 3328 } 3329 3330 q2 &= mask 3331 r2 &= mask 3332 3333 delta = m.Ud - 1 - r2 3334 delta &= mask 3335 3336 if p < m.W+m.W { 3337 if q1 < delta || (q1 == delta && r1 == 0) { 3338 continue 3339 } 3340 } 3341 3342 break 3343 } 3344 3345 m.Um = q2 + 1 3346 m.S = p - m.W 3347 } 3348 3349 func ngotype(n *Node) *Sym { 3350 if n.Type != nil { 3351 return typenamesym(n.Type) 3352 } 3353 return nil 3354 } 3355 3356 /* 3357 * Convert raw string to the prefix that will be used in the symbol 3358 * table. All control characters, space, '%' and '"', as well as 3359 * non-7-bit clean bytes turn into %xx. The period needs escaping 3360 * only in the last segment of the path, and it makes for happier 3361 * users if we escape that as little as possible. 3362 * 3363 * If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too. 3364 */ 3365 func pathtoprefix(s string) string { 3366 slash := strings.LastIndex(s, "/") 3367 for i := 0; i < len(s); i++ { 3368 c := s[i] 3369 if c <= ' ' || i >= slash && c == '.' || c == '%' || c == '"' || c >= 0x7F { 3370 var buf bytes.Buffer 3371 for i := 0; i < len(s); i++ { 3372 c := s[i] 3373 if c <= ' ' || i >= slash && c == '.' || c == '%' || c == '"' || c >= 0x7F { 3374 fmt.Fprintf(&buf, "%%%02x", c) 3375 continue 3376 } 3377 buf.WriteByte(c) 3378 } 3379 return buf.String() 3380 } 3381 } 3382 return s 3383 } 3384 3385 var pkgMap = make(map[string]*Pkg) 3386 var pkgs []*Pkg 3387 3388 func mkpkg(path string) *Pkg { 3389 if p := pkgMap[path]; p != nil { 3390 return p 3391 } 3392 3393 p := new(Pkg) 3394 p.Path = path 3395 p.Prefix = pathtoprefix(path) 3396 p.Syms = make(map[string]*Sym) 3397 pkgMap[path] = p 3398 pkgs = append(pkgs, p) 3399 return p 3400 } 3401 3402 func addinit(np **Node, init *NodeList) { 3403 if init == nil { 3404 return 3405 } 3406 3407 n := *np 3408 switch n.Op { 3409 // There may be multiple refs to this node; 3410 // introduce OCONVNOP to hold init list. 3411 case ONAME, OLITERAL: 3412 n = Nod(OCONVNOP, n, nil) 3413 3414 n.Type = n.Left.Type 3415 n.Typecheck = 1 3416 *np = n 3417 } 3418 3419 n.Ninit = concat(init, n.Ninit) 3420 n.Ullman = UINF 3421 } 3422 3423 var reservedimports = []string{ 3424 "go", 3425 "type", 3426 } 3427 3428 func isbadimport(path string) bool { 3429 if strings.Contains(path, "\x00") { 3430 Yyerror("import path contains NUL") 3431 return true 3432 } 3433 3434 for _, ri := range reservedimports { 3435 if path == ri { 3436 Yyerror("import path %q is reserved and cannot be used", path) 3437 return true 3438 } 3439 } 3440 3441 for _, r := range path { 3442 if r == utf8.RuneError { 3443 Yyerror("import path contains invalid UTF-8 sequence: %q", path) 3444 return true 3445 } 3446 3447 if r < 0x20 || r == 0x7f { 3448 Yyerror("import path contains control character: %q", path) 3449 return true 3450 } 3451 3452 if r == '\\' { 3453 Yyerror("import path contains backslash; use slash: %q", path) 3454 return true 3455 } 3456 3457 if unicode.IsSpace(rune(r)) { 3458 Yyerror("import path contains space character: %q", path) 3459 return true 3460 } 3461 3462 if strings.ContainsRune("!\"#$%&'()*,:;<=>?[]^`{|}", r) { 3463 Yyerror("import path contains invalid character '%c': %q", r, path) 3464 return true 3465 } 3466 } 3467 3468 return false 3469 } 3470 3471 func checknil(x *Node, init **NodeList) { 3472 if Isinter(x.Type) { 3473 x = Nod(OITAB, x, nil) 3474 typecheck(&x, Erv) 3475 } 3476 3477 n := Nod(OCHECKNIL, x, nil) 3478 n.Typecheck = 1 3479 *init = list(*init, n) 3480 } 3481 3482 /* 3483 * Can this type be stored directly in an interface word? 3484 * Yes, if the representation is a single pointer. 3485 */ 3486 func isdirectiface(t *Type) bool { 3487 switch t.Etype { 3488 case TPTR32, 3489 TPTR64, 3490 TCHAN, 3491 TMAP, 3492 TFUNC, 3493 TUNSAFEPTR: 3494 return true 3495 3496 // Array of 1 direct iface type can be direct. 3497 case TARRAY: 3498 return t.Bound == 1 && isdirectiface(t.Type) 3499 3500 // Struct with 1 field of direct iface type can be direct. 3501 case TSTRUCT: 3502 return t.Type != nil && t.Type.Down == nil && isdirectiface(t.Type.Type) 3503 } 3504 3505 return false 3506 } 3507 3508 // type2IET returns "T" if t is a concrete type, 3509 // "I" if t is an interface type, and "E" if t is an empty interface type. 3510 // It is used to build calls to the conv* and assert* runtime routines. 3511 func type2IET(t *Type) string { 3512 if isnilinter(t) { 3513 return "E" 3514 } 3515 if Isinter(t) { 3516 return "I" 3517 } 3518 return "T" 3519 }