github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/cmd/compile/internal/gc/swt.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 "cmd/compile/internal/types" 9 "fmt" 10 "sort" 11 ) 12 13 const ( 14 // expression switch 15 switchKindExpr = iota // switch a {...} or switch 5 {...} 16 switchKindTrue // switch true {...} or switch {...} 17 switchKindFalse // switch false {...} 18 ) 19 20 const ( 21 binarySearchMin = 4 // minimum number of cases for binary search 22 integerRangeMin = 2 // minimum size of integer ranges 23 ) 24 25 // An exprSwitch walks an expression switch. 26 type exprSwitch struct { 27 exprname *Node // node for the expression being switched on 28 kind int // kind of switch statement (switchKind*) 29 } 30 31 // A typeSwitch walks a type switch. 32 type typeSwitch struct { 33 hashname *Node // node for the hash of the type of the variable being switched on 34 facename *Node // node for the concrete type of the variable being switched on 35 okname *Node // boolean node used for comma-ok type assertions 36 } 37 38 // A caseClause is a single case clause in a switch statement. 39 type caseClause struct { 40 node *Node // points at case statement 41 ordinal int // position in switch 42 hash uint32 // hash of a type switch 43 // isconst indicates whether this case clause is a constant, 44 // for the purposes of the switch code generation. 45 // For expression switches, that's generally literals (case 5:, not case x:). 46 // For type switches, that's concrete types (case time.Time:), not interfaces (case io.Reader:). 47 isconst bool 48 } 49 50 // caseClauses are all the case clauses in a switch statement. 51 type caseClauses struct { 52 list []caseClause // general cases 53 defjmp *Node // OGOTO for default case or OBREAK if no default case present 54 niljmp *Node // OGOTO for nil type case in a type switch 55 } 56 57 // typecheckswitch typechecks a switch statement. 58 func typecheckswitch(n *Node) { 59 typecheckslice(n.Ninit.Slice(), Etop) 60 61 var nilonly string 62 var top int 63 var t *types.Type 64 65 if n.Left != nil && n.Left.Op == OTYPESW { 66 // type switch 67 top = Etype 68 n.Left.Right = typecheck(n.Left.Right, Erv) 69 t = n.Left.Right.Type 70 if t != nil && !t.IsInterface() { 71 yyerrorl(n.Pos, "cannot type switch on non-interface value %L", n.Left.Right) 72 } 73 if v := n.Left.Left; v != nil && !v.isBlank() && n.List.Len() == 0 { 74 // We don't actually declare the type switch's guarded 75 // declaration itself. So if there are no cases, we 76 // won't notice that it went unused. 77 yyerrorl(v.Pos, "%v declared and not used", v.Sym) 78 } 79 } else { 80 // expression switch 81 top = Erv 82 if n.Left != nil { 83 n.Left = typecheck(n.Left, Erv) 84 n.Left = defaultlit(n.Left, nil) 85 t = n.Left.Type 86 } else { 87 t = types.Types[TBOOL] 88 } 89 if t != nil { 90 switch { 91 case !okforeq[t.Etype]: 92 yyerrorl(n.Pos, "cannot switch on %L", n.Left) 93 case t.IsSlice(): 94 nilonly = "slice" 95 case t.IsArray() && !IsComparable(t): 96 yyerrorl(n.Pos, "cannot switch on %L", n.Left) 97 case t.IsStruct(): 98 if f := IncomparableField(t); f != nil { 99 yyerrorl(n.Pos, "cannot switch on %L (struct containing %v cannot be compared)", n.Left, f.Type) 100 } 101 case t.Etype == TFUNC: 102 nilonly = "func" 103 case t.IsMap(): 104 nilonly = "map" 105 } 106 } 107 } 108 109 n.Type = t 110 111 var def, niltype *Node 112 for _, ncase := range n.List.Slice() { 113 if ncase.List.Len() == 0 { 114 // default 115 if def != nil { 116 setlineno(ncase) 117 yyerrorl(ncase.Pos, "multiple defaults in switch (first at %v)", def.Line()) 118 } else { 119 def = ncase 120 } 121 } else { 122 ls := ncase.List.Slice() 123 for i1, n1 := range ls { 124 setlineno(n1) 125 ls[i1] = typecheck(ls[i1], Erv|Etype) 126 n1 = ls[i1] 127 if n1.Type == nil || t == nil { 128 continue 129 } 130 131 setlineno(ncase) 132 switch top { 133 // expression switch 134 case Erv: 135 ls[i1] = defaultlit(ls[i1], t) 136 n1 = ls[i1] 137 switch { 138 case n1.Op == OTYPE: 139 yyerrorl(ncase.Pos, "type %v is not an expression", n1.Type) 140 case n1.Type != nil && assignop(n1.Type, t, nil) == 0 && assignop(t, n1.Type, nil) == 0: 141 if n.Left != nil { 142 yyerrorl(ncase.Pos, "invalid case %v in switch on %v (mismatched types %v and %v)", n1, n.Left, n1.Type, t) 143 } else { 144 yyerrorl(ncase.Pos, "invalid case %v in switch (mismatched types %v and bool)", n1, n1.Type) 145 } 146 case nilonly != "" && !n1.isNil(): 147 yyerrorl(ncase.Pos, "invalid case %v in switch (can only compare %s %v to nil)", n1, nilonly, n.Left) 148 case t.IsInterface() && !n1.Type.IsInterface() && !IsComparable(n1.Type): 149 yyerrorl(ncase.Pos, "invalid case %L in switch (incomparable type)", n1) 150 } 151 152 // type switch 153 case Etype: 154 var missing, have *types.Field 155 var ptr int 156 switch { 157 case n1.Op == OLITERAL && n1.Type.IsKind(TNIL): 158 // case nil: 159 if niltype != nil { 160 yyerrorl(ncase.Pos, "multiple nil cases in type switch (first at %v)", niltype.Line()) 161 } else { 162 niltype = ncase 163 } 164 case n1.Op != OTYPE && n1.Type != nil: // should this be ||? 165 yyerrorl(ncase.Pos, "%L is not a type", n1) 166 // reset to original type 167 n1 = n.Left.Right 168 ls[i1] = n1 169 case !n1.Type.IsInterface() && t.IsInterface() && !implements(n1.Type, t, &missing, &have, &ptr): 170 if have != nil && !missing.Broke() && !have.Broke() { 171 yyerrorl(ncase.Pos, "impossible type switch case: %L cannot have dynamic type %v"+ 172 " (wrong type for %v method)\n\thave %v%S\n\twant %v%S", n.Left.Right, n1.Type, missing.Sym, have.Sym, have.Type, missing.Sym, missing.Type) 173 } else if !missing.Broke() { 174 if ptr != 0 { 175 yyerrorl(ncase.Pos, "impossible type switch case: %L cannot have dynamic type %v"+ 176 " (%v method has pointer receiver)", n.Left.Right, n1.Type, missing.Sym) 177 } else { 178 yyerrorl(ncase.Pos, "impossible type switch case: %L cannot have dynamic type %v"+ 179 " (missing %v method)", n.Left.Right, n1.Type, missing.Sym) 180 } 181 } 182 } 183 } 184 } 185 } 186 187 if n.Type == nil || n.Type.IsUntyped() { 188 // if the value we're switching on has no type or is untyped, 189 // we've already printed an error and don't need to continue 190 // typechecking the body 191 return 192 } 193 194 if top == Etype { 195 ll := ncase.List 196 if ncase.Rlist.Len() != 0 { 197 nvar := ncase.Rlist.First() 198 if ll.Len() == 1 && ll.First().Type != nil && !ll.First().Type.IsKind(TNIL) { 199 // single entry type switch 200 nvar.Type = ll.First().Type 201 } else { 202 // multiple entry type switch or default 203 nvar.Type = n.Type 204 } 205 206 nvar = typecheck(nvar, Erv|Easgn) 207 ncase.Rlist.SetFirst(nvar) 208 } 209 } 210 211 typecheckslice(ncase.Nbody.Slice(), Etop) 212 } 213 switch top { 214 // expression switch 215 case Erv: 216 checkDupExprCases(n.Left, n.List.Slice()) 217 } 218 } 219 220 // walkswitch walks a switch statement. 221 func walkswitch(sw *Node) { 222 // convert switch {...} to switch true {...} 223 if sw.Left == nil { 224 sw.Left = nodbool(true) 225 sw.Left = typecheck(sw.Left, Erv) 226 sw.Left = defaultlit(sw.Left, nil) 227 } 228 229 if sw.Left.Op == OTYPESW { 230 var s typeSwitch 231 s.walk(sw) 232 } else { 233 var s exprSwitch 234 s.walk(sw) 235 } 236 } 237 238 // walk generates an AST implementing sw. 239 // sw is an expression switch. 240 // The AST is generally of the form of a linear 241 // search using if..goto, although binary search 242 // is used with long runs of constants. 243 func (s *exprSwitch) walk(sw *Node) { 244 // Guard against double walk, see #25776. 245 if sw.List.Len() == 0 && sw.Nbody.Len() > 0 { 246 Fatalf("second walk of switch") 247 } 248 249 casebody(sw, nil) 250 251 cond := sw.Left 252 sw.Left = nil 253 254 s.kind = switchKindExpr 255 if Isconst(cond, CTBOOL) { 256 s.kind = switchKindTrue 257 if !cond.Val().U.(bool) { 258 s.kind = switchKindFalse 259 } 260 } 261 262 // Given "switch string(byteslice)", 263 // with all cases being constants (or the default case), 264 // use a zero-cost alias of the byte slice. 265 // In theory, we could be more aggressive, 266 // allowing any side-effect-free expressions in cases, 267 // but it's a bit tricky because some of that information 268 // is unavailable due to the introduction of temporaries during order. 269 // Restricting to constants is simple and probably powerful enough. 270 // Do this before calling walkexpr on cond, 271 // because walkexpr will lower the string 272 // conversion into a runtime call. 273 // See issue 24937 for more discussion. 274 if cond.Op == OARRAYBYTESTR { 275 ok := true 276 for _, cas := range sw.List.Slice() { 277 if cas.Op != OCASE { 278 Fatalf("switch string(byteslice) bad op: %v", cas.Op) 279 } 280 if cas.Left != nil && !Isconst(cas.Left, CTSTR) { 281 ok = false 282 break 283 } 284 } 285 if ok { 286 cond.Op = OARRAYBYTESTRTMP 287 } 288 } 289 290 cond = walkexpr(cond, &sw.Ninit) 291 t := sw.Type 292 if t == nil { 293 return 294 } 295 296 // convert the switch into OIF statements 297 var cas []*Node 298 if s.kind == switchKindTrue || s.kind == switchKindFalse { 299 s.exprname = nodbool(s.kind == switchKindTrue) 300 } else if consttype(cond) > 0 { 301 // leave constants to enable dead code elimination (issue 9608) 302 s.exprname = cond 303 } else { 304 s.exprname = temp(cond.Type) 305 cas = []*Node{nod(OAS, s.exprname, cond)} 306 typecheckslice(cas, Etop) 307 } 308 309 // Enumerate the cases and prepare the default case. 310 clauses := s.genCaseClauses(sw.List.Slice()) 311 sw.List.Set(nil) 312 cc := clauses.list 313 314 // handle the cases in order 315 for len(cc) > 0 { 316 run := 1 317 if okforcmp[t.Etype] && cc[0].isconst { 318 // do binary search on runs of constants 319 for ; run < len(cc) && cc[run].isconst; run++ { 320 } 321 // sort and compile constants 322 sort.Sort(caseClauseByConstVal(cc[:run])) 323 } 324 325 a := s.walkCases(cc[:run]) 326 cas = append(cas, a) 327 cc = cc[run:] 328 } 329 330 // handle default case 331 if nerrors == 0 { 332 cas = append(cas, clauses.defjmp) 333 sw.Nbody.Prepend(cas...) 334 walkstmtlist(sw.Nbody.Slice()) 335 } 336 } 337 338 // walkCases generates an AST implementing the cases in cc. 339 func (s *exprSwitch) walkCases(cc []caseClause) *Node { 340 if len(cc) < binarySearchMin { 341 // linear search 342 var cas []*Node 343 for _, c := range cc { 344 n := c.node 345 lno := setlineno(n) 346 347 a := nod(OIF, nil, nil) 348 if rng := n.List.Slice(); rng != nil { 349 // Integer range. 350 // exprname is a temp or a constant, 351 // so it is safe to evaluate twice. 352 // In most cases, this conjunction will be 353 // rewritten by walkinrange into a single comparison. 354 low := nod(OGE, s.exprname, rng[0]) 355 high := nod(OLE, s.exprname, rng[1]) 356 a.Left = nod(OANDAND, low, high) 357 } else if (s.kind != switchKindTrue && s.kind != switchKindFalse) || assignop(n.Left.Type, s.exprname.Type, nil) == OCONVIFACE || assignop(s.exprname.Type, n.Left.Type, nil) == OCONVIFACE { 358 a.Left = nod(OEQ, s.exprname, n.Left) // if name == val 359 } else if s.kind == switchKindTrue { 360 a.Left = n.Left // if val 361 } else { 362 // s.kind == switchKindFalse 363 a.Left = nod(ONOT, n.Left, nil) // if !val 364 } 365 a.Left = typecheck(a.Left, Erv) 366 a.Left = defaultlit(a.Left, nil) 367 a.Nbody.Set1(n.Right) // goto l 368 369 cas = append(cas, a) 370 lineno = lno 371 } 372 return liststmt(cas) 373 } 374 375 // find the middle and recur 376 half := len(cc) / 2 377 a := nod(OIF, nil, nil) 378 n := cc[half-1].node 379 var mid *Node 380 if rng := n.List.Slice(); rng != nil { 381 mid = rng[1] // high end of range 382 } else { 383 mid = n.Left 384 } 385 le := nod(OLE, s.exprname, mid) 386 if Isconst(mid, CTSTR) { 387 // Search by length and then by value; see caseClauseByConstVal. 388 lenlt := nod(OLT, nod(OLEN, s.exprname, nil), nod(OLEN, mid, nil)) 389 leneq := nod(OEQ, nod(OLEN, s.exprname, nil), nod(OLEN, mid, nil)) 390 a.Left = nod(OOROR, lenlt, nod(OANDAND, leneq, le)) 391 } else { 392 a.Left = le 393 } 394 a.Left = typecheck(a.Left, Erv) 395 a.Left = defaultlit(a.Left, nil) 396 a.Nbody.Set1(s.walkCases(cc[:half])) 397 a.Rlist.Set1(s.walkCases(cc[half:])) 398 return a 399 } 400 401 // casebody builds separate lists of statements and cases. 402 // It makes labels between cases and statements 403 // and deals with fallthrough, break, and unreachable statements. 404 func casebody(sw *Node, typeswvar *Node) { 405 if sw.List.Len() == 0 { 406 return 407 } 408 409 lno := setlineno(sw) 410 411 var cas []*Node // cases 412 var stat []*Node // statements 413 var def *Node // defaults 414 br := nod(OBREAK, nil, nil) 415 416 for _, n := range sw.List.Slice() { 417 setlineno(n) 418 if n.Op != OXCASE { 419 Fatalf("casebody %v", n.Op) 420 } 421 n.Op = OCASE 422 needvar := n.List.Len() != 1 || n.List.First().Op == OLITERAL 423 424 jmp := nod(OGOTO, autolabel(".s"), nil) 425 switch n.List.Len() { 426 case 0: 427 // default 428 if def != nil { 429 yyerrorl(n.Pos, "more than one default case") 430 } 431 // reuse original default case 432 n.Right = jmp 433 def = n 434 case 1: 435 // one case -- reuse OCASE node 436 n.Left = n.List.First() 437 n.Right = jmp 438 n.List.Set(nil) 439 cas = append(cas, n) 440 default: 441 // Expand multi-valued cases and detect ranges of integer cases. 442 if typeswvar != nil || sw.Left.Type.IsInterface() || !n.List.First().Type.IsInteger() || n.List.Len() < integerRangeMin { 443 // Can't use integer ranges. Expand each case into a separate node. 444 for _, n1 := range n.List.Slice() { 445 cas = append(cas, nod(OCASE, n1, jmp)) 446 } 447 break 448 } 449 // Find integer ranges within runs of constants. 450 s := n.List.Slice() 451 j := 0 452 for j < len(s) { 453 // Find a run of constants. 454 var run int 455 for run = j; run < len(s) && Isconst(s[run], CTINT); run++ { 456 } 457 if run-j >= integerRangeMin { 458 // Search for integer ranges in s[j:run]. 459 // Typechecking is done, so all values are already in an appropriate range. 460 search := s[j:run] 461 sort.Sort(constIntNodesByVal(search)) 462 for beg, end := 0, 1; end <= len(search); end++ { 463 if end < len(search) && search[end].Int64() == search[end-1].Int64()+1 { 464 continue 465 } 466 if end-beg >= integerRangeMin { 467 // Record range in List. 468 c := nod(OCASE, nil, jmp) 469 c.List.Set2(search[beg], search[end-1]) 470 cas = append(cas, c) 471 } else { 472 // Not large enough for range; record separately. 473 for _, n := range search[beg:end] { 474 cas = append(cas, nod(OCASE, n, jmp)) 475 } 476 } 477 beg = end 478 } 479 j = run 480 } 481 // Advance to next constant, adding individual non-constant 482 // or as-yet-unhandled constant cases as we go. 483 for ; j < len(s) && (j < run || !Isconst(s[j], CTINT)); j++ { 484 cas = append(cas, nod(OCASE, s[j], jmp)) 485 } 486 } 487 } 488 489 stat = append(stat, nod(OLABEL, jmp.Left, nil)) 490 if typeswvar != nil && needvar && n.Rlist.Len() != 0 { 491 l := []*Node{ 492 nod(ODCL, n.Rlist.First(), nil), 493 nod(OAS, n.Rlist.First(), typeswvar), 494 } 495 typecheckslice(l, Etop) 496 stat = append(stat, l...) 497 } 498 stat = append(stat, n.Nbody.Slice()...) 499 500 // Search backwards for the index of the fallthrough 501 // statement. Do not assume it'll be in the last 502 // position, since in some cases (e.g. when the statement 503 // list contains autotmp_ variables), one or more OVARKILL 504 // nodes will be at the end of the list. 505 fallIndex := len(stat) - 1 506 for stat[fallIndex].Op == OVARKILL { 507 fallIndex-- 508 } 509 last := stat[fallIndex] 510 if last.Op != OFALL { 511 stat = append(stat, br) 512 } 513 } 514 515 stat = append(stat, br) 516 if def != nil { 517 cas = append(cas, def) 518 } 519 520 sw.List.Set(cas) 521 sw.Nbody.Set(stat) 522 lineno = lno 523 } 524 525 // genCaseClauses generates the caseClauses value for clauses. 526 func (s *exprSwitch) genCaseClauses(clauses []*Node) caseClauses { 527 var cc caseClauses 528 for _, n := range clauses { 529 if n.Left == nil && n.List.Len() == 0 { 530 // default case 531 if cc.defjmp != nil { 532 Fatalf("duplicate default case not detected during typechecking") 533 } 534 cc.defjmp = n.Right 535 continue 536 } 537 c := caseClause{node: n, ordinal: len(cc.list)} 538 if n.List.Len() > 0 { 539 c.isconst = true 540 } 541 switch consttype(n.Left) { 542 case CTFLT, CTINT, CTRUNE, CTSTR: 543 c.isconst = true 544 } 545 cc.list = append(cc.list, c) 546 } 547 548 if cc.defjmp == nil { 549 cc.defjmp = nod(OBREAK, nil, nil) 550 } 551 return cc 552 } 553 554 // genCaseClauses generates the caseClauses value for clauses. 555 func (s *typeSwitch) genCaseClauses(clauses []*Node) caseClauses { 556 var cc caseClauses 557 for _, n := range clauses { 558 switch { 559 case n.Left == nil: 560 // default case 561 if cc.defjmp != nil { 562 Fatalf("duplicate default case not detected during typechecking") 563 } 564 cc.defjmp = n.Right 565 continue 566 case n.Left.Op == OLITERAL: 567 // nil case in type switch 568 if cc.niljmp != nil { 569 Fatalf("duplicate nil case not detected during typechecking") 570 } 571 cc.niljmp = n.Right 572 continue 573 } 574 575 // general case 576 c := caseClause{ 577 node: n, 578 ordinal: len(cc.list), 579 isconst: !n.Left.Type.IsInterface(), 580 hash: typehash(n.Left.Type), 581 } 582 cc.list = append(cc.list, c) 583 } 584 585 if cc.defjmp == nil { 586 cc.defjmp = nod(OBREAK, nil, nil) 587 } 588 589 // diagnose duplicate cases 590 s.checkDupCases(cc.list) 591 return cc 592 } 593 594 func (s *typeSwitch) checkDupCases(cc []caseClause) { 595 if len(cc) < 2 { 596 return 597 } 598 // We store seen types in a map keyed by type hash. 599 // It is possible, but very unlikely, for multiple distinct types to have the same hash. 600 seen := make(map[uint32][]*Node) 601 // To avoid many small allocations of length 1 slices, 602 // also set up a single large slice to slice into. 603 nn := make([]*Node, 0, len(cc)) 604 Outer: 605 for _, c := range cc { 606 prev, ok := seen[c.hash] 607 if !ok { 608 // First entry for this hash. 609 nn = append(nn, c.node) 610 seen[c.hash] = nn[len(nn)-1 : len(nn) : len(nn)] 611 continue 612 } 613 for _, n := range prev { 614 if types.Identical(n.Left.Type, c.node.Left.Type) { 615 yyerrorl(c.node.Pos, "duplicate case %v in type switch\n\tprevious case at %v", c.node.Left.Type, n.Line()) 616 // avoid double-reporting errors 617 continue Outer 618 } 619 } 620 seen[c.hash] = append(seen[c.hash], c.node) 621 } 622 } 623 624 func checkDupExprCases(exprname *Node, clauses []*Node) { 625 // boolean (naked) switch, nothing to do. 626 if exprname == nil { 627 return 628 } 629 // The common case is that s's expression is not an interface. 630 // In that case, all constant clauses have the same type, 631 // so checking for duplicates can be done solely by value. 632 if !exprname.Type.IsInterface() { 633 seen := make(map[interface{}]*Node) 634 for _, ncase := range clauses { 635 for _, n := range ncase.List.Slice() { 636 // Can't check for duplicates that aren't constants, per the spec. Issue 15896. 637 // Don't check for duplicate bools. Although the spec allows it, 638 // (1) the compiler hasn't checked it in the past, so compatibility mandates it, and 639 // (2) it would disallow useful things like 640 // case GOARCH == "arm" && GOARM == "5": 641 // case GOARCH == "arm": 642 // which would both evaluate to false for non-ARM compiles. 643 if ct := consttype(n); ct == 0 || ct == CTBOOL { 644 continue 645 } 646 647 val := n.Val().Interface() 648 prev, dup := seen[val] 649 if !dup { 650 seen[val] = n 651 continue 652 } 653 yyerrorl(ncase.Pos, "duplicate case %s in switch\n\tprevious case at %v", 654 nodeAndVal(n), prev.Line()) 655 } 656 } 657 return 658 } 659 660 // s's expression is an interface. This is fairly rare, so 661 // keep this simple. Case expressions are only duplicates if 662 // they have the same value and identical types. 663 // 664 // In general, we have to use eqtype to test type identity, 665 // because == gives false negatives for anonymous types and 666 // the byte/uint8 and rune/int32 builtin type aliases. 667 // However, this is not a problem here, because constant 668 // expressions are always untyped or have a named type, and we 669 // explicitly handle the builtin type aliases below. 670 // 671 // This approach may need to be revisited though if we fix 672 // #21866 by treating all type aliases like byte/uint8 and 673 // rune/int32. 674 type typeVal struct { 675 typ *types.Type 676 val interface{} 677 } 678 seen := make(map[typeVal]*Node) 679 for _, ncase := range clauses { 680 for _, n := range ncase.List.Slice() { 681 if ct := consttype(n); ct == 0 || ct == CTBOOL { 682 continue 683 } 684 tv := typeVal{ 685 typ: n.Type, 686 val: n.Val().Interface(), 687 } 688 switch tv.typ { 689 case types.Bytetype: 690 tv.typ = types.Types[TUINT8] 691 case types.Runetype: 692 tv.typ = types.Types[TINT32] 693 } 694 prev, dup := seen[tv] 695 if !dup { 696 seen[tv] = n 697 continue 698 } 699 yyerrorl(ncase.Pos, "duplicate case %s in switch\n\tprevious case at %v", 700 nodeAndVal(n), prev.Line()) 701 } 702 } 703 } 704 705 func nodeAndVal(n *Node) string { 706 show := n.String() 707 val := n.Val().Interface() 708 if s := fmt.Sprintf("%#v", val); show != s { 709 show += " (value " + s + ")" 710 } 711 return show 712 } 713 714 // walk generates an AST that implements sw, 715 // where sw is a type switch. 716 // The AST is generally of the form of a linear 717 // search using if..goto, although binary search 718 // is used with long runs of concrete types. 719 func (s *typeSwitch) walk(sw *Node) { 720 cond := sw.Left 721 sw.Left = nil 722 723 if cond == nil { 724 sw.List.Set(nil) 725 return 726 } 727 if cond.Right == nil { 728 yyerrorl(sw.Pos, "type switch must have an assignment") 729 return 730 } 731 732 cond.Right = walkexpr(cond.Right, &sw.Ninit) 733 if !cond.Right.Type.IsInterface() { 734 yyerrorl(sw.Pos, "type switch must be on an interface") 735 return 736 } 737 738 var cas []*Node 739 740 // predeclare temporary variables and the boolean var 741 s.facename = temp(cond.Right.Type) 742 743 a := nod(OAS, s.facename, cond.Right) 744 a = typecheck(a, Etop) 745 cas = append(cas, a) 746 747 s.okname = temp(types.Types[TBOOL]) 748 s.okname = typecheck(s.okname, Erv) 749 750 s.hashname = temp(types.Types[TUINT32]) 751 s.hashname = typecheck(s.hashname, Erv) 752 753 // set up labels and jumps 754 casebody(sw, s.facename) 755 756 clauses := s.genCaseClauses(sw.List.Slice()) 757 sw.List.Set(nil) 758 def := clauses.defjmp 759 760 // For empty interfaces, do: 761 // if e._type == nil { 762 // do nil case if it exists, otherwise default 763 // } 764 // h := e._type.hash 765 // Use a similar strategy for non-empty interfaces. 766 767 // Get interface descriptor word. 768 // For empty interfaces this will be the type. 769 // For non-empty interfaces this will be the itab. 770 itab := nod(OITAB, s.facename, nil) 771 772 // Check for nil first. 773 i := nod(OIF, nil, nil) 774 i.Left = nod(OEQ, itab, nodnil()) 775 if clauses.niljmp != nil { 776 // Do explicit nil case right here. 777 i.Nbody.Set1(clauses.niljmp) 778 } else { 779 // Jump to default case. 780 lbl := autolabel(".s") 781 i.Nbody.Set1(nod(OGOTO, lbl, nil)) 782 // Wrap default case with label. 783 blk := nod(OBLOCK, nil, nil) 784 blk.List.Set2(nod(OLABEL, lbl, nil), def) 785 def = blk 786 } 787 i.Left = typecheck(i.Left, Erv) 788 i.Left = defaultlit(i.Left, nil) 789 cas = append(cas, i) 790 791 // Load hash from type or itab. 792 h := nodSym(ODOTPTR, itab, nil) 793 h.Type = types.Types[TUINT32] 794 h.SetTypecheck(1) 795 if cond.Right.Type.IsEmptyInterface() { 796 h.Xoffset = int64(2 * Widthptr) // offset of hash in runtime._type 797 } else { 798 h.Xoffset = int64(2 * Widthptr) // offset of hash in runtime.itab 799 } 800 h.SetBounded(true) // guaranteed not to fault 801 a = nod(OAS, s.hashname, h) 802 a = typecheck(a, Etop) 803 cas = append(cas, a) 804 805 cc := clauses.list 806 807 // insert type equality check into each case block 808 for _, c := range cc { 809 c.node.Right = s.typeone(c.node) 810 } 811 812 // generate list of if statements, binary search for constant sequences 813 for len(cc) > 0 { 814 if !cc[0].isconst { 815 n := cc[0].node 816 cas = append(cas, n.Right) 817 cc = cc[1:] 818 continue 819 } 820 821 // identify run of constants 822 var run int 823 for run = 1; run < len(cc) && cc[run].isconst; run++ { 824 } 825 826 // sort by hash 827 sort.Sort(caseClauseByType(cc[:run])) 828 829 // for debugging: linear search 830 if false { 831 for i := 0; i < run; i++ { 832 n := cc[i].node 833 cas = append(cas, n.Right) 834 } 835 continue 836 } 837 838 // combine adjacent cases with the same hash 839 var batch []caseClause 840 for i, j := 0, 0; i < run; i = j { 841 hash := []*Node{cc[i].node.Right} 842 for j = i + 1; j < run && cc[i].hash == cc[j].hash; j++ { 843 hash = append(hash, cc[j].node.Right) 844 } 845 cc[i].node.Right = liststmt(hash) 846 batch = append(batch, cc[i]) 847 } 848 849 // binary search among cases to narrow by hash 850 cas = append(cas, s.walkCases(batch)) 851 cc = cc[run:] 852 } 853 854 // handle default case 855 if nerrors == 0 { 856 cas = append(cas, def) 857 sw.Nbody.Prepend(cas...) 858 sw.List.Set(nil) 859 walkstmtlist(sw.Nbody.Slice()) 860 } 861 } 862 863 // typeone generates an AST that jumps to the 864 // case body if the variable is of type t. 865 func (s *typeSwitch) typeone(t *Node) *Node { 866 var name *Node 867 var init Nodes 868 if t.Rlist.Len() == 0 { 869 name = nblank 870 nblank = typecheck(nblank, Erv|Easgn) 871 } else { 872 name = t.Rlist.First() 873 init.Append(nod(ODCL, name, nil)) 874 a := nod(OAS, name, nil) 875 a = typecheck(a, Etop) 876 init.Append(a) 877 } 878 879 a := nod(OAS2, nil, nil) 880 a.List.Set2(name, s.okname) // name, ok = 881 b := nod(ODOTTYPE, s.facename, nil) 882 b.Type = t.Left.Type // interface.(type) 883 a.Rlist.Set1(b) 884 a = typecheck(a, Etop) 885 a = walkexpr(a, &init) 886 init.Append(a) 887 888 c := nod(OIF, nil, nil) 889 c.Left = s.okname 890 c.Nbody.Set1(t.Right) // if ok { goto l } 891 892 init.Append(c) 893 return init.asblock() 894 } 895 896 // walkCases generates an AST implementing the cases in cc. 897 func (s *typeSwitch) walkCases(cc []caseClause) *Node { 898 if len(cc) < binarySearchMin { 899 var cas []*Node 900 for _, c := range cc { 901 n := c.node 902 if !c.isconst { 903 Fatalf("typeSwitch walkCases") 904 } 905 a := nod(OIF, nil, nil) 906 a.Left = nod(OEQ, s.hashname, nodintconst(int64(c.hash))) 907 a.Left = typecheck(a.Left, Erv) 908 a.Left = defaultlit(a.Left, nil) 909 a.Nbody.Set1(n.Right) 910 cas = append(cas, a) 911 } 912 return liststmt(cas) 913 } 914 915 // find the middle and recur 916 half := len(cc) / 2 917 a := nod(OIF, nil, nil) 918 a.Left = nod(OLE, s.hashname, nodintconst(int64(cc[half-1].hash))) 919 a.Left = typecheck(a.Left, Erv) 920 a.Left = defaultlit(a.Left, nil) 921 a.Nbody.Set1(s.walkCases(cc[:half])) 922 a.Rlist.Set1(s.walkCases(cc[half:])) 923 return a 924 } 925 926 // caseClauseByConstVal sorts clauses by constant value to enable binary search. 927 type caseClauseByConstVal []caseClause 928 929 func (x caseClauseByConstVal) Len() int { return len(x) } 930 func (x caseClauseByConstVal) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 931 func (x caseClauseByConstVal) Less(i, j int) bool { 932 // n1 and n2 might be individual constants or integer ranges. 933 // We have checked for duplicates already, 934 // so ranges can be safely represented by any value in the range. 935 n1 := x[i].node 936 var v1 interface{} 937 if s := n1.List.Slice(); s != nil { 938 v1 = s[0].Val().U 939 } else { 940 v1 = n1.Left.Val().U 941 } 942 943 n2 := x[j].node 944 var v2 interface{} 945 if s := n2.List.Slice(); s != nil { 946 v2 = s[0].Val().U 947 } else { 948 v2 = n2.Left.Val().U 949 } 950 951 switch v1 := v1.(type) { 952 case *Mpflt: 953 return v1.Cmp(v2.(*Mpflt)) < 0 954 case *Mpint: 955 return v1.Cmp(v2.(*Mpint)) < 0 956 case string: 957 // Sort strings by length and then by value. 958 // It is much cheaper to compare lengths than values, 959 // and all we need here is consistency. 960 // We respect this sorting in exprSwitch.walkCases. 961 a := v1 962 b := v2.(string) 963 if len(a) != len(b) { 964 return len(a) < len(b) 965 } 966 return a < b 967 } 968 969 Fatalf("caseClauseByConstVal passed bad clauses %v < %v", x[i].node.Left, x[j].node.Left) 970 return false 971 } 972 973 type caseClauseByType []caseClause 974 975 func (x caseClauseByType) Len() int { return len(x) } 976 func (x caseClauseByType) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 977 func (x caseClauseByType) Less(i, j int) bool { 978 c1, c2 := x[i], x[j] 979 // sort by hash code, then ordinal (for the rare case of hash collisions) 980 if c1.hash != c2.hash { 981 return c1.hash < c2.hash 982 } 983 return c1.ordinal < c2.ordinal 984 } 985 986 type constIntNodesByVal []*Node 987 988 func (x constIntNodesByVal) Len() int { return len(x) } 989 func (x constIntNodesByVal) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 990 func (x constIntNodesByVal) Less(i, j int) bool { 991 return x[i].Val().U.(*Mpint).Cmp(x[j].Val().U.(*Mpint)) < 0 992 }