mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
Now that the generic graph structure methods - Left, Right, and so on -
have been removed from the Node interface, each implementation's uses
can be replaced with direct field access, using more specific names,
and the methods themselves can be deleted.
Passes buildall w/ toolstash -cmp.
[git-generate]
cd src/cmd/compile/internal/ir
rf '
mv Func.iota Func.Iota_
mv Name.fn Name.Func_
'
cd ../gc
rf '
ex . ../ir {
import "cmd/compile/internal/ir"
import "cmd/compile/internal/types"
var ns ir.Nodes
var b bool
var i64 int64
var n ir.Node
var op ir.Op
var sym *types.Sym
var class ir.Class
var decl *ir.Decl
decl.Left() -> decl.X
decl.SetLeft(n) -> decl.X = n
var asl *ir.AssignListStmt
asl.List() -> asl.Lhs
asl.PtrList() -> &asl.Lhs
asl.SetList(ns) -> asl.Lhs = ns
asl.Rlist() -> asl.Rhs
asl.PtrRlist() -> &asl.Rhs
asl.SetRlist(ns) -> asl.Rhs = ns
asl.Colas() -> asl.Def
asl.SetColas(b) -> asl.Def = b
var as *ir.AssignStmt
as.Left() -> as.X
as.SetLeft(n) -> as.X = n
as.Right() -> as.Y
as.SetRight(n) -> as.Y = n
as.Colas() -> as.Def
as.SetColas(b) -> as.Def = b
var ao *ir.AssignOpStmt
ao.Left() -> ao.X
ao.SetLeft(n) -> ao.X = n
ao.Right() -> ao.Y
ao.SetRight(n) -> ao.Y = n
ao.SubOp() -> ao.AsOp
ao.SetSubOp(op) -> ao.AsOp = op
ao.Implicit() -> ao.IncDec
ao.SetImplicit(b) -> ao.IncDec = b
var bl *ir.BlockStmt
bl.List() -> bl.List_
bl.PtrList() -> &bl.List_
bl.SetList(ns) -> bl.List_ = ns
var br *ir.BranchStmt
br.Sym() -> br.Label
br.SetSym(sym) -> br.Label = sym
var cas *ir.CaseStmt
cas.List() -> cas.List_
cas.PtrList() -> &cas.List_
cas.SetList(ns) -> cas.List_ = ns
cas.Body() -> cas.Body_
cas.PtrBody() -> &cas.Body_
cas.SetBody(ns) -> cas.Body_ = ns
cas.Rlist() -> cas.Vars
cas.PtrRlist() -> &cas.Vars
cas.SetRlist(ns) -> cas.Vars = ns
cas.Left() -> cas.Comm
cas.SetLeft(n) -> cas.Comm = n
var fr *ir.ForStmt
fr.Sym() -> fr.Label
fr.SetSym(sym) -> fr.Label = sym
fr.Left() -> fr.Cond
fr.SetLeft(n) -> fr.Cond = n
fr.Right() -> fr.Post
fr.SetRight(n) -> fr.Post = n
fr.Body() -> fr.Body_
fr.PtrBody() -> &fr.Body_
fr.SetBody(ns) -> fr.Body_ = ns
fr.List() -> fr.Late
fr.PtrList() -> &fr.Late
fr.SetList(ns) -> fr.Late = ns
fr.HasBreak() -> fr.HasBreak_
fr.SetHasBreak(b) -> fr.HasBreak_ = b
var gs *ir.GoDeferStmt
gs.Left() -> gs.Call
gs.SetLeft(n) -> gs.Call = n
var ifs *ir.IfStmt
ifs.Left() -> ifs.Cond
ifs.SetLeft(n) -> ifs.Cond = n
ifs.Body() -> ifs.Body_
ifs.PtrBody() -> &ifs.Body_
ifs.SetBody(ns) -> ifs.Body_ = ns
ifs.Rlist() -> ifs.Else
ifs.PtrRlist() -> &ifs.Else
ifs.SetRlist(ns) -> ifs.Else = ns
ifs.Likely() -> ifs.Likely_
ifs.SetLikely(b) -> ifs.Likely_ = b
var im *ir.InlineMarkStmt
im.Offset() -> im.Index
im.SetOffset(i64) -> im.Index = i64
var lab *ir.LabelStmt
lab.Sym() -> lab.Label
lab.SetSym(sym) -> lab.Label = sym
var rng *ir.RangeStmt
rng.Sym() -> rng.Label
rng.SetSym(sym) -> rng.Label = sym
rng.Right() -> rng.X
rng.SetRight(n) -> rng.X = n
rng.Body() -> rng.Body_
rng.PtrBody() -> &rng.Body_
rng.SetBody(ns) -> rng.Body_ = ns
rng.List() -> rng.Vars
rng.PtrList() -> &rng.Vars
rng.SetList(ns) -> rng.Vars = ns
rng.HasBreak() -> rng.HasBreak_
rng.SetHasBreak(b) -> rng.HasBreak_ = b
rng.Colas() -> rng.Def
rng.SetColas(b) -> rng.Def = b
var ret *ir.ReturnStmt
ret.List() -> ret.Results
ret.PtrList() -> &ret.Results
ret.SetList(ns) -> ret.Results = ns
var sel *ir.SelectStmt
sel.List() -> sel.Cases
sel.PtrList() -> &sel.Cases
sel.SetList(ns) -> sel.Cases = ns
sel.Sym() -> sel.Label
sel.SetSym(sym) -> sel.Label = sym
sel.HasBreak() -> sel.HasBreak_
sel.SetHasBreak(b) -> sel.HasBreak_ = b
sel.Body() -> sel.Compiled
sel.PtrBody() -> &sel.Compiled
sel.SetBody(ns) -> sel.Compiled = ns
var send *ir.SendStmt
send.Left() -> send.Chan
send.SetLeft(n) -> send.Chan = n
send.Right() -> send.Value
send.SetRight(n) -> send.Value = n
var sw *ir.SwitchStmt
sw.Left() -> sw.Tag
sw.SetLeft(n) -> sw.Tag = n
sw.List() -> sw.Cases
sw.PtrList() -> &sw.Cases
sw.SetList(ns) -> sw.Cases = ns
sw.Body() -> sw.Compiled
sw.PtrBody() -> &sw.Compiled
sw.SetBody(ns) -> sw.Compiled = ns
sw.Sym() -> sw.Label
sw.SetSym(sym) -> sw.Label = sym
sw.HasBreak() -> sw.HasBreak_
sw.SetHasBreak(b) -> sw.HasBreak_ = b
var tg *ir.TypeSwitchGuard
tg.Left() -> tg.Tag
tg.SetLeft(nil) -> tg.Tag = nil
tg.SetLeft(n) -> tg.Tag = n.(*ir.Ident)
tg.Right() -> tg.X
tg.SetRight(n) -> tg.X = n
var adds *ir.AddStringExpr
adds.List() -> adds.List_
adds.PtrList() -> &adds.List_
adds.SetList(ns) -> adds.List_ = ns
var addr *ir.AddrExpr
addr.Left() -> addr.X
addr.SetLeft(n) -> addr.X = n
addr.Right() -> addr.Alloc
addr.SetRight(n) -> addr.Alloc = n
var bin *ir.BinaryExpr
bin.Left() -> bin.X
bin.SetLeft(n) -> bin.X = n
bin.Right() -> bin.Y
bin.SetRight(n) -> bin.Y = n
var log *ir.LogicalExpr
log.Left() -> log.X
log.SetLeft(n) -> log.X = n
log.Right() -> log.Y
log.SetRight(n) -> log.Y = n
var call *ir.CallExpr
call.Left() -> call.X
call.SetLeft(n) -> call.X = n
call.List() -> call.Args
call.PtrList() -> &call.Args
call.SetList(ns) -> call.Args = ns
call.Rlist() -> call.Rargs
call.PtrRlist() -> &call.Rargs
call.SetRlist(ns) -> call.Rargs = ns
call.IsDDD() -> call.DDD
call.SetIsDDD(b) -> call.DDD = b
call.NoInline() -> call.NoInline_
call.SetNoInline(b) -> call.NoInline_ = b
call.Body() -> call.Body_
call.PtrBody() -> &call.Body_
call.SetBody(ns) -> call.Body_ = ns
var cp *ir.CallPartExpr
cp.Func() -> cp.Func_
cp.Left() -> cp.X
cp.SetLeft(n) -> cp.X = n
cp.Sym() -> cp.Method.Sym
var clo *ir.ClosureExpr
clo.Func() -> clo.Func_
var cr *ir.ClosureReadExpr
cr.Offset() -> cr.Offset_
var cl *ir.CompLitExpr
cl.Right() -> cl.Ntype
cl.SetRight(nil) -> cl.Ntype = nil
cl.SetRight(n) -> cl.Ntype = ir.Node(n).(ir.Ntype)
cl.List() -> cl.List_
cl.PtrList() -> &cl.List_
cl.SetList(ns) -> cl.List_ = ns
var conv *ir.ConvExpr
conv.Left() -> conv.X
conv.SetLeft(n) -> conv.X = n
var ix *ir.IndexExpr
ix.Left() -> ix.X
ix.SetLeft(n) -> ix.X = n
ix.Right() -> ix.Index
ix.SetRight(n) -> ix.Index = n
ix.IndexMapLValue() -> ix.Assigned
ix.SetIndexMapLValue(b) -> ix.Assigned = b
var kv *ir.KeyExpr
kv.Left() -> kv.Key
kv.SetLeft(n) -> kv.Key = n
kv.Right() -> kv.Value
kv.SetRight(n) -> kv.Value = n
var sk *ir.StructKeyExpr
sk.Sym() -> sk.Field
sk.SetSym(sym) -> sk.Field = sym
sk.Left() -> sk.Value
sk.SetLeft(n) -> sk.Value = n
sk.Offset() -> sk.Offset_
sk.SetOffset(i64) -> sk.Offset_ = i64
var ic *ir.InlinedCallExpr
ic.Body() -> ic.Body_
ic.PtrBody() -> &ic.Body_
ic.SetBody(ns) -> ic.Body_ = ns
ic.Rlist() -> ic.ReturnVars
ic.PtrRlist() -> &ic.ReturnVars
ic.SetRlist(ns) -> ic.ReturnVars = ns
var mak *ir.MakeExpr
mak.Left() -> mak.Len
mak.SetLeft(n) -> mak.Len = n
mak.Right() -> mak.Cap
mak.SetRight(n) -> mak.Cap = n
var par *ir.ParenExpr
par.Left() -> par.X
par.SetLeft(n) -> par.X = n
var res *ir.ResultExpr
res.Offset() -> res.Offset_
res.SetOffset(i64) -> res.Offset_ = i64
var dot *ir.SelectorExpr
dot.Left() -> dot.X
dot.SetLeft(n) -> dot.X = n
dot.Sym() -> dot.Sel
dot.SetSym(sym) -> dot.Sel = sym
dot.Offset() -> dot.Offset_
dot.SetOffset(i64) -> dot.Offset_ = i64
var sl *ir.SliceExpr
sl.Left() -> sl.X
sl.SetLeft(n) -> sl.X = n
sl.List() -> sl.List_
sl.PtrList() -> &sl.List_
sl.SetList(ns) -> sl.List_ = ns
var sh *ir.SliceHeaderExpr
sh.Left() -> sh.Ptr
sh.SetLeft(n) -> sh.Ptr = n
sh.List() -> sh.LenCap_
sh.PtrList() -> &sh.LenCap_
sh.SetList(ns) -> sh.LenCap_ = ns
var st *ir.StarExpr
st.Left() -> st.X
st.SetLeft(n) -> st.X = n
var ta *ir.TypeAssertExpr
ta.Left() -> ta.X
ta.SetLeft(n) -> ta.X = n
ta.Right() -> ta.Ntype
ta.SetRight(n) -> ta.Ntype = n
ta.List() -> ta.Itab
ta.PtrList() -> &ta.Itab
ta.SetList(ns) -> ta.Itab = ns
var u *ir.UnaryExpr
u.Left() -> u.X
u.SetLeft(n) -> u.X = n
var fn *ir.Func
fn.Body() -> fn.Body_
fn.PtrBody() -> &fn.Body_
fn.SetBody(ns) -> fn.Body_ = ns
fn.Iota() -> fn.Iota_
fn.SetIota(i64) -> fn.Iota_ = i64
fn.Func() -> fn
var nam *ir.Name
nam.SubOp() -> nam.BuiltinOp
nam.SetSubOp(op) -> nam.BuiltinOp = op
nam.Class() -> nam.Class_
nam.SetClass(class) -> nam.Class_ = class
nam.Func() -> nam.Func_
nam.Offset() -> nam.Offset_
nam.SetOffset(i64) -> nam.Offset_ = i64
}
ex . ../ir {
import "cmd/compile/internal/ir"
var n ir.Nodes
(&n).Append -> n.Append
(&n).AppendNodes -> n.AppendNodes
(&n).MoveNodes -> n.MoveNodes
(&n).Prepend -> n.Prepend
(&n).Set -> n.Set
(&n).Set1 -> n.Set1
(&n).Set2 -> n.Set2
(&n).Set3 -> n.Set3
var ntype ir.Ntype
ir.Node(ntype).(ir.Ntype) -> ntype
}
'
cd ../ir
rf '
rm \
Decl.Left Decl.SetLeft \
AssignListStmt.List AssignListStmt.PtrList AssignListStmt.SetList \
AssignListStmt.Rlist AssignListStmt.PtrRlist AssignListStmt.SetRlist \
AssignListStmt.Colas AssignListStmt.SetColas \
AssignStmt.Left AssignStmt.SetLeft \
AssignStmt.Right AssignStmt.SetRight \
AssignStmt.Colas AssignStmt.SetColas \
AssignOpStmt.Left AssignOpStmt.SetLeft \
AssignOpStmt.Right AssignOpStmt.SetRight \
AssignOpStmt.SubOp AssignOpStmt.SetSubOp \
AssignOpStmt.Implicit AssignOpStmt.SetImplicit \
BlockStmt.List BlockStmt.PtrList BlockStmt.SetList \
BranchStmt.SetSym \
CaseStmt.List CaseStmt.PtrList CaseStmt.SetList \
CaseStmt.Body CaseStmt.PtrBody CaseStmt.SetBody \
CaseStmt.Rlist CaseStmt.PtrRlist CaseStmt.SetRlist \
CaseStmt.Left CaseStmt.SetLeft \
ForStmt.Left ForStmt.SetLeft \
ForStmt.Right ForStmt.SetRight \
ForStmt.Body ForStmt.PtrBody ForStmt.SetBody \
ForStmt.List ForStmt.PtrList ForStmt.SetList \
ForStmt.HasBreak ForStmt.SetHasBreak \
ForStmt.Sym ForStmt.SetSym \
GoDeferStmt.Left GoDeferStmt.SetLeft \
IfStmt.Left IfStmt.SetLeft \
IfStmt.Body IfStmt.PtrBody IfStmt.SetBody \
IfStmt.Rlist IfStmt.PtrRlist IfStmt.SetRlist \
IfStmt.Likely IfStmt.SetLikely \
LabelStmt.SetSym \
RangeStmt.Right RangeStmt.SetRight \
RangeStmt.Body RangeStmt.PtrBody RangeStmt.SetBody \
RangeStmt.List RangeStmt.PtrList RangeStmt.SetList \
RangeStmt.HasBreak RangeStmt.SetHasBreak \
RangeStmt.Colas RangeStmt.SetColas \
RangeStmt.Sym RangeStmt.SetSym \
ReturnStmt.List ReturnStmt.PtrList ReturnStmt.SetList \
SelectStmt.List SelectStmt.PtrList SelectStmt.SetList \
SelectStmt.HasBreak SelectStmt.SetHasBreak \
SelectStmt.Body SelectStmt.PtrBody SelectStmt.SetBody \
SelectStmt.Sym SelectStmt.SetSym \
SendStmt.Left SendStmt.SetLeft \
SendStmt.Right SendStmt.SetRight \
SwitchStmt.Left SwitchStmt.SetLeft \
SwitchStmt.List SwitchStmt.PtrList SwitchStmt.SetList \
SwitchStmt.Body SwitchStmt.PtrBody SwitchStmt.SetBody \
SwitchStmt.HasBreak SwitchStmt.SetHasBreak \
SwitchStmt.Sym SwitchStmt.SetSym \
TypeSwitchGuard.Left TypeSwitchGuard.SetLeft \
TypeSwitchGuard.Right TypeSwitchGuard.SetRight \
AddStringExpr.List AddStringExpr.PtrList AddStringExpr.SetList \
AddrExpr.Left AddrExpr.SetLeft \
AddrExpr.Right AddrExpr.SetRight \
BinaryExpr.Left BinaryExpr.SetLeft \
BinaryExpr.Right BinaryExpr.SetRight \
LogicalExpr.Left LogicalExpr.SetLeft \
LogicalExpr.Right LogicalExpr.SetRight \
CallExpr.Left CallExpr.SetLeft \
CallExpr.List CallExpr.PtrList CallExpr.SetList \
CallExpr.Rlist CallExpr.PtrRlist CallExpr.SetRlist \
CallExpr.NoInline CallExpr.SetNoInline \
CallExpr.Body CallExpr.PtrBody CallExpr.SetBody \
CallExpr.IsDDD CallExpr.SetIsDDD \
CallPartExpr.Left CallPartExpr.SetLeft \
ClosureReadExpr.Offset \
ClosureReadExpr.Type \ # provided by miniExpr already
CompLitExpr.Right CompLitExpr.SetRight \
CompLitExpr.List CompLitExpr.PtrList CompLitExpr.SetList \
ConvExpr.Left ConvExpr.SetLeft \
IndexExpr.Left IndexExpr.SetLeft \
IndexExpr.Right IndexExpr.SetRight \
IndexExpr.IndexMapLValue IndexExpr.SetIndexMapLValue \
KeyExpr.Left KeyExpr.SetLeft \
KeyExpr.Right KeyExpr.SetRight \
StructKeyExpr.Left StructKeyExpr.SetLeft \
StructKeyExpr.Offset StructKeyExpr.SetOffset \
StructKeyExpr.SetSym \
InlinedCallExpr.Body InlinedCallExpr.PtrBody InlinedCallExpr.SetBody \
InlinedCallExpr.Rlist InlinedCallExpr.PtrRlist InlinedCallExpr.SetRlist \
MakeExpr.Left MakeExpr.SetLeft \
MakeExpr.Right MakeExpr.SetRight \
MethodExpr.Left MethodExpr.SetLeft \
MethodExpr.Right MethodExpr.SetRight \
MethodExpr.Offset MethodExpr.SetOffset \
MethodExpr.Class MethodExpr.SetClass \
ParenExpr.Left ParenExpr.SetLeft \
ResultExpr.Offset ResultExpr.SetOffset \
ReturnStmt.IsDDD \
SelectorExpr.Left SelectorExpr.SetLeft \
SelectorExpr.Offset SelectorExpr.SetOffset \
SelectorExpr.SetSym \
SliceExpr.Left SliceExpr.SetLeft \
SliceExpr.List SliceExpr.PtrList SliceExpr.SetList \
SliceHeaderExpr.Left SliceHeaderExpr.SetLeft \
SliceHeaderExpr.List SliceHeaderExpr.PtrList SliceHeaderExpr.SetList \
StarExpr.Left StarExpr.SetLeft \
TypeAssertExpr.Left TypeAssertExpr.SetLeft \
TypeAssertExpr.Right TypeAssertExpr.SetRight \
TypeAssertExpr.List TypeAssertExpr.PtrList TypeAssertExpr.SetList \
UnaryExpr.Left UnaryExpr.SetLeft \
Func.Body Func.PtrBody Func.SetBody \
Func.Iota Func.SetIota \
CallPartExpr.Func ClosureExpr.Func Func.Func Name.Func \
mv BlockStmt.List_ BlockStmt.List
mv CaseStmt.List_ CaseStmt.List
mv CaseStmt.Body_ CaseStmt.Body
mv ForStmt.Body_ ForStmt.Body
mv ForStmt.HasBreak_ ForStmt.HasBreak
mv Func.Iota_ Func.Iota
mv IfStmt.Body_ IfStmt.Body
mv IfStmt.Likely_ IfStmt.Likely
mv RangeStmt.Body_ RangeStmt.Body
mv RangeStmt.HasBreak_ RangeStmt.HasBreak
mv SelectStmt.HasBreak_ SelectStmt.HasBreak
mv SwitchStmt.HasBreak_ SwitchStmt.HasBreak
mv AddStringExpr.List_ AddStringExpr.List
mv CallExpr.NoInline_ CallExpr.NoInline
mv CallExpr.Body_ CallExpr.Body # TODO what is this?
mv CallExpr.DDD CallExpr.IsDDD
mv ClosureReadExpr.Offset_ ClosureReadExpr.Offset
mv CompLitExpr.List_ CompLitExpr.List
mv StructKeyExpr.Offset_ StructKeyExpr.Offset
mv InlinedCallExpr.Body_ InlinedCallExpr.Body
mv ResultExpr.Offset_ ResultExpr.Offset
mv SelectorExpr.Offset_ SelectorExpr.Offset
mv SliceExpr.List_ SliceExpr.List
mv SliceHeaderExpr.LenCap_ SliceHeaderExpr.LenCap
mv Func.Body_ Func.Body
mv CallPartExpr.Func_ CallPartExpr.Func
mv ClosureExpr.Func_ ClosureExpr.Func
mv Name.Func_ Name.Func
'
Change-Id: Ia2ee59649674f83eb123e63fda7a7781cf91cc56
Reviewed-on: https://go-review.googlesource.com/c/go/+/277935
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
763 lines
20 KiB
Go
763 lines
20 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gc
|
|
|
|
import (
|
|
"cmd/compile/internal/base"
|
|
"cmd/compile/internal/ir"
|
|
"cmd/compile/internal/types"
|
|
"cmd/internal/src"
|
|
"go/constant"
|
|
"go/token"
|
|
"sort"
|
|
)
|
|
|
|
// typecheckswitch typechecks a switch statement.
|
|
func typecheckswitch(n *ir.SwitchStmt) {
|
|
typecheckslice(n.Init().Slice(), ctxStmt)
|
|
if n.Tag != nil && n.Tag.Op() == ir.OTYPESW {
|
|
typecheckTypeSwitch(n)
|
|
} else {
|
|
typecheckExprSwitch(n)
|
|
}
|
|
}
|
|
|
|
func typecheckTypeSwitch(n *ir.SwitchStmt) {
|
|
guard := n.Tag.(*ir.TypeSwitchGuard)
|
|
guard.X = typecheck(guard.X, ctxExpr)
|
|
t := guard.X.Type()
|
|
if t != nil && !t.IsInterface() {
|
|
base.ErrorfAt(n.Pos(), "cannot type switch on non-interface value %L", guard.X)
|
|
t = nil
|
|
}
|
|
|
|
// We don't actually declare the type switch's guarded
|
|
// declaration itself. So if there are no cases, we won't
|
|
// notice that it went unused.
|
|
if v := guard.Tag; v != nil && !ir.IsBlank(v) && n.Cases.Len() == 0 {
|
|
base.ErrorfAt(v.Pos(), "%v declared but not used", v.Sym())
|
|
}
|
|
|
|
var defCase, nilCase ir.Node
|
|
var ts typeSet
|
|
for _, ncase := range n.Cases.Slice() {
|
|
ncase := ncase.(*ir.CaseStmt)
|
|
ls := ncase.List.Slice()
|
|
if len(ls) == 0 { // default:
|
|
if defCase != nil {
|
|
base.ErrorfAt(ncase.Pos(), "multiple defaults in switch (first at %v)", ir.Line(defCase))
|
|
} else {
|
|
defCase = ncase
|
|
}
|
|
}
|
|
|
|
for i := range ls {
|
|
ls[i] = typecheck(ls[i], ctxExpr|ctxType)
|
|
n1 := ls[i]
|
|
if t == nil || n1.Type() == nil {
|
|
continue
|
|
}
|
|
|
|
var missing, have *types.Field
|
|
var ptr int
|
|
if ir.IsNil(n1) { // case nil:
|
|
if nilCase != nil {
|
|
base.ErrorfAt(ncase.Pos(), "multiple nil cases in type switch (first at %v)", ir.Line(nilCase))
|
|
} else {
|
|
nilCase = ncase
|
|
}
|
|
continue
|
|
}
|
|
if n1.Op() != ir.OTYPE {
|
|
base.ErrorfAt(ncase.Pos(), "%L is not a type", n1)
|
|
continue
|
|
}
|
|
if !n1.Type().IsInterface() && !implements(n1.Type(), t, &missing, &have, &ptr) && !missing.Broke() {
|
|
if have != nil && !have.Broke() {
|
|
base.ErrorfAt(ncase.Pos(), "impossible type switch case: %L cannot have dynamic type %v"+
|
|
" (wrong type for %v method)\n\thave %v%S\n\twant %v%S", guard.X, n1.Type(), missing.Sym, have.Sym, have.Type, missing.Sym, missing.Type)
|
|
} else if ptr != 0 {
|
|
base.ErrorfAt(ncase.Pos(), "impossible type switch case: %L cannot have dynamic type %v"+
|
|
" (%v method has pointer receiver)", guard.X, n1.Type(), missing.Sym)
|
|
} else {
|
|
base.ErrorfAt(ncase.Pos(), "impossible type switch case: %L cannot have dynamic type %v"+
|
|
" (missing %v method)", guard.X, n1.Type(), missing.Sym)
|
|
}
|
|
continue
|
|
}
|
|
|
|
ts.add(ncase.Pos(), n1.Type())
|
|
}
|
|
|
|
if ncase.Vars.Len() != 0 {
|
|
// Assign the clause variable's type.
|
|
vt := t
|
|
if len(ls) == 1 {
|
|
if ls[0].Op() == ir.OTYPE {
|
|
vt = ls[0].Type()
|
|
} else if !ir.IsNil(ls[0]) {
|
|
// Invalid single-type case;
|
|
// mark variable as broken.
|
|
vt = nil
|
|
}
|
|
}
|
|
|
|
nvar := ncase.Vars.First()
|
|
nvar.SetType(vt)
|
|
if vt != nil {
|
|
nvar = typecheck(nvar, ctxExpr|ctxAssign)
|
|
} else {
|
|
// Clause variable is broken; prevent typechecking.
|
|
nvar.SetTypecheck(1)
|
|
nvar.SetWalkdef(1)
|
|
}
|
|
ncase.Vars.SetFirst(nvar)
|
|
}
|
|
|
|
typecheckslice(ncase.Body.Slice(), ctxStmt)
|
|
}
|
|
}
|
|
|
|
type typeSet struct {
|
|
m map[string][]typeSetEntry
|
|
}
|
|
|
|
type typeSetEntry struct {
|
|
pos src.XPos
|
|
typ *types.Type
|
|
}
|
|
|
|
func (s *typeSet) add(pos src.XPos, typ *types.Type) {
|
|
if s.m == nil {
|
|
s.m = make(map[string][]typeSetEntry)
|
|
}
|
|
|
|
// LongString does not uniquely identify types, so we need to
|
|
// disambiguate collisions with types.Identical.
|
|
// TODO(mdempsky): Add a method that *is* unique.
|
|
ls := typ.LongString()
|
|
prevs := s.m[ls]
|
|
for _, prev := range prevs {
|
|
if types.Identical(typ, prev.typ) {
|
|
base.ErrorfAt(pos, "duplicate case %v in type switch\n\tprevious case at %s", typ, base.FmtPos(prev.pos))
|
|
return
|
|
}
|
|
}
|
|
s.m[ls] = append(prevs, typeSetEntry{pos, typ})
|
|
}
|
|
|
|
func typecheckExprSwitch(n *ir.SwitchStmt) {
|
|
t := types.Types[types.TBOOL]
|
|
if n.Tag != nil {
|
|
n.Tag = typecheck(n.Tag, ctxExpr)
|
|
n.Tag = defaultlit(n.Tag, nil)
|
|
t = n.Tag.Type()
|
|
}
|
|
|
|
var nilonly string
|
|
if t != nil {
|
|
switch {
|
|
case t.IsMap():
|
|
nilonly = "map"
|
|
case t.Kind() == types.TFUNC:
|
|
nilonly = "func"
|
|
case t.IsSlice():
|
|
nilonly = "slice"
|
|
|
|
case !IsComparable(t):
|
|
if t.IsStruct() {
|
|
base.ErrorfAt(n.Pos(), "cannot switch on %L (struct containing %v cannot be compared)", n.Tag, IncomparableField(t).Type)
|
|
} else {
|
|
base.ErrorfAt(n.Pos(), "cannot switch on %L", n.Tag)
|
|
}
|
|
t = nil
|
|
}
|
|
}
|
|
|
|
var defCase ir.Node
|
|
var cs constSet
|
|
for _, ncase := range n.Cases.Slice() {
|
|
ncase := ncase.(*ir.CaseStmt)
|
|
ls := ncase.List.Slice()
|
|
if len(ls) == 0 { // default:
|
|
if defCase != nil {
|
|
base.ErrorfAt(ncase.Pos(), "multiple defaults in switch (first at %v)", ir.Line(defCase))
|
|
} else {
|
|
defCase = ncase
|
|
}
|
|
}
|
|
|
|
for i := range ls {
|
|
setlineno(ncase)
|
|
ls[i] = typecheck(ls[i], ctxExpr)
|
|
ls[i] = defaultlit(ls[i], t)
|
|
n1 := ls[i]
|
|
if t == nil || n1.Type() == nil {
|
|
continue
|
|
}
|
|
|
|
if nilonly != "" && !ir.IsNil(n1) {
|
|
base.ErrorfAt(ncase.Pos(), "invalid case %v in switch (can only compare %s %v to nil)", n1, nilonly, n.Tag)
|
|
} else if t.IsInterface() && !n1.Type().IsInterface() && !IsComparable(n1.Type()) {
|
|
base.ErrorfAt(ncase.Pos(), "invalid case %L in switch (incomparable type)", n1)
|
|
} else {
|
|
op1, _ := assignop(n1.Type(), t)
|
|
op2, _ := assignop(t, n1.Type())
|
|
if op1 == ir.OXXX && op2 == ir.OXXX {
|
|
if n.Tag != nil {
|
|
base.ErrorfAt(ncase.Pos(), "invalid case %v in switch on %v (mismatched types %v and %v)", n1, n.Tag, n1.Type(), t)
|
|
} else {
|
|
base.ErrorfAt(ncase.Pos(), "invalid case %v in switch (mismatched types %v and bool)", n1, n1.Type())
|
|
}
|
|
}
|
|
}
|
|
|
|
// Don't check for duplicate bools. Although the spec allows it,
|
|
// (1) the compiler hasn't checked it in the past, so compatibility mandates it, and
|
|
// (2) it would disallow useful things like
|
|
// case GOARCH == "arm" && GOARM == "5":
|
|
// case GOARCH == "arm":
|
|
// which would both evaluate to false for non-ARM compiles.
|
|
if !n1.Type().IsBoolean() {
|
|
cs.add(ncase.Pos(), n1, "case", "switch")
|
|
}
|
|
}
|
|
|
|
typecheckslice(ncase.Body.Slice(), ctxStmt)
|
|
}
|
|
}
|
|
|
|
// walkswitch walks a switch statement.
|
|
func walkswitch(sw *ir.SwitchStmt) {
|
|
// Guard against double walk, see #25776.
|
|
if sw.Cases.Len() == 0 && sw.Compiled.Len() > 0 {
|
|
return // Was fatal, but eliminating every possible source of double-walking is hard
|
|
}
|
|
|
|
if sw.Tag != nil && sw.Tag.Op() == ir.OTYPESW {
|
|
walkTypeSwitch(sw)
|
|
} else {
|
|
walkExprSwitch(sw)
|
|
}
|
|
}
|
|
|
|
// walkExprSwitch generates an AST implementing sw. sw is an
|
|
// expression switch.
|
|
func walkExprSwitch(sw *ir.SwitchStmt) {
|
|
lno := setlineno(sw)
|
|
|
|
cond := sw.Tag
|
|
sw.Tag = nil
|
|
|
|
// convert switch {...} to switch true {...}
|
|
if cond == nil {
|
|
cond = nodbool(true)
|
|
cond = typecheck(cond, ctxExpr)
|
|
cond = defaultlit(cond, nil)
|
|
}
|
|
|
|
// Given "switch string(byteslice)",
|
|
// with all cases being side-effect free,
|
|
// use a zero-cost alias of the byte slice.
|
|
// Do this before calling walkexpr on cond,
|
|
// because walkexpr will lower the string
|
|
// conversion into a runtime call.
|
|
// See issue 24937 for more discussion.
|
|
if cond.Op() == ir.OBYTES2STR && allCaseExprsAreSideEffectFree(sw) {
|
|
cond := cond.(*ir.ConvExpr)
|
|
cond.SetOp(ir.OBYTES2STRTMP)
|
|
}
|
|
|
|
cond = walkexpr(cond, sw.PtrInit())
|
|
if cond.Op() != ir.OLITERAL && cond.Op() != ir.ONIL {
|
|
cond = copyexpr(cond, cond.Type(), &sw.Compiled)
|
|
}
|
|
|
|
base.Pos = lno
|
|
|
|
s := exprSwitch{
|
|
exprname: cond,
|
|
}
|
|
|
|
var defaultGoto ir.Node
|
|
var body ir.Nodes
|
|
for _, ncase := range sw.Cases.Slice() {
|
|
ncase := ncase.(*ir.CaseStmt)
|
|
label := autolabel(".s")
|
|
jmp := ir.NewBranchStmt(ncase.Pos(), ir.OGOTO, label)
|
|
|
|
// Process case dispatch.
|
|
if ncase.List.Len() == 0 {
|
|
if defaultGoto != nil {
|
|
base.Fatalf("duplicate default case not detected during typechecking")
|
|
}
|
|
defaultGoto = jmp
|
|
}
|
|
|
|
for _, n1 := range ncase.List.Slice() {
|
|
s.Add(ncase.Pos(), n1, jmp)
|
|
}
|
|
|
|
// Process body.
|
|
body.Append(ir.NewLabelStmt(ncase.Pos(), label))
|
|
body.Append(ncase.Body.Slice()...)
|
|
if fall, pos := endsInFallthrough(ncase.Body.Slice()); !fall {
|
|
br := ir.NewBranchStmt(base.Pos, ir.OBREAK, nil)
|
|
br.SetPos(pos)
|
|
body.Append(br)
|
|
}
|
|
}
|
|
sw.Cases.Set(nil)
|
|
|
|
if defaultGoto == nil {
|
|
br := ir.NewBranchStmt(base.Pos, ir.OBREAK, nil)
|
|
br.SetPos(br.Pos().WithNotStmt())
|
|
defaultGoto = br
|
|
}
|
|
|
|
s.Emit(&sw.Compiled)
|
|
sw.Compiled.Append(defaultGoto)
|
|
sw.Compiled.AppendNodes(&body)
|
|
walkstmtlist(sw.Compiled.Slice())
|
|
}
|
|
|
|
// An exprSwitch walks an expression switch.
|
|
type exprSwitch struct {
|
|
exprname ir.Node // value being switched on
|
|
|
|
done ir.Nodes
|
|
clauses []exprClause
|
|
}
|
|
|
|
type exprClause struct {
|
|
pos src.XPos
|
|
lo, hi ir.Node
|
|
jmp ir.Node
|
|
}
|
|
|
|
func (s *exprSwitch) Add(pos src.XPos, expr, jmp ir.Node) {
|
|
c := exprClause{pos: pos, lo: expr, hi: expr, jmp: jmp}
|
|
if okforcmp[s.exprname.Type().Kind()] && expr.Op() == ir.OLITERAL {
|
|
s.clauses = append(s.clauses, c)
|
|
return
|
|
}
|
|
|
|
s.flush()
|
|
s.clauses = append(s.clauses, c)
|
|
s.flush()
|
|
}
|
|
|
|
func (s *exprSwitch) Emit(out *ir.Nodes) {
|
|
s.flush()
|
|
out.AppendNodes(&s.done)
|
|
}
|
|
|
|
func (s *exprSwitch) flush() {
|
|
cc := s.clauses
|
|
s.clauses = nil
|
|
if len(cc) == 0 {
|
|
return
|
|
}
|
|
|
|
// Caution: If len(cc) == 1, then cc[0] might not an OLITERAL.
|
|
// The code below is structured to implicitly handle this case
|
|
// (e.g., sort.Slice doesn't need to invoke the less function
|
|
// when there's only a single slice element).
|
|
|
|
if s.exprname.Type().IsString() && len(cc) >= 2 {
|
|
// Sort strings by length and then by value. It is
|
|
// much cheaper to compare lengths than values, and
|
|
// all we need here is consistency. We respect this
|
|
// sorting below.
|
|
sort.Slice(cc, func(i, j int) bool {
|
|
si := ir.StringVal(cc[i].lo)
|
|
sj := ir.StringVal(cc[j].lo)
|
|
if len(si) != len(sj) {
|
|
return len(si) < len(sj)
|
|
}
|
|
return si < sj
|
|
})
|
|
|
|
// runLen returns the string length associated with a
|
|
// particular run of exprClauses.
|
|
runLen := func(run []exprClause) int64 { return int64(len(ir.StringVal(run[0].lo))) }
|
|
|
|
// Collapse runs of consecutive strings with the same length.
|
|
var runs [][]exprClause
|
|
start := 0
|
|
for i := 1; i < len(cc); i++ {
|
|
if runLen(cc[start:]) != runLen(cc[i:]) {
|
|
runs = append(runs, cc[start:i])
|
|
start = i
|
|
}
|
|
}
|
|
runs = append(runs, cc[start:])
|
|
|
|
// Perform two-level binary search.
|
|
binarySearch(len(runs), &s.done,
|
|
func(i int) ir.Node {
|
|
return ir.NewBinaryExpr(base.Pos, ir.OLE, ir.NewUnaryExpr(base.Pos, ir.OLEN, s.exprname), nodintconst(runLen(runs[i-1])))
|
|
},
|
|
func(i int, nif *ir.IfStmt) {
|
|
run := runs[i]
|
|
nif.Cond = ir.NewBinaryExpr(base.Pos, ir.OEQ, ir.NewUnaryExpr(base.Pos, ir.OLEN, s.exprname), nodintconst(runLen(run)))
|
|
s.search(run, &nif.Body)
|
|
},
|
|
)
|
|
return
|
|
}
|
|
|
|
sort.Slice(cc, func(i, j int) bool {
|
|
return constant.Compare(cc[i].lo.Val(), token.LSS, cc[j].lo.Val())
|
|
})
|
|
|
|
// Merge consecutive integer cases.
|
|
if s.exprname.Type().IsInteger() {
|
|
merged := cc[:1]
|
|
for _, c := range cc[1:] {
|
|
last := &merged[len(merged)-1]
|
|
if last.jmp == c.jmp && ir.Int64Val(last.hi)+1 == ir.Int64Val(c.lo) {
|
|
last.hi = c.lo
|
|
} else {
|
|
merged = append(merged, c)
|
|
}
|
|
}
|
|
cc = merged
|
|
}
|
|
|
|
s.search(cc, &s.done)
|
|
}
|
|
|
|
func (s *exprSwitch) search(cc []exprClause, out *ir.Nodes) {
|
|
binarySearch(len(cc), out,
|
|
func(i int) ir.Node {
|
|
return ir.NewBinaryExpr(base.Pos, ir.OLE, s.exprname, cc[i-1].hi)
|
|
},
|
|
func(i int, nif *ir.IfStmt) {
|
|
c := &cc[i]
|
|
nif.Cond = c.test(s.exprname)
|
|
nif.Body.Set1(c.jmp)
|
|
},
|
|
)
|
|
}
|
|
|
|
func (c *exprClause) test(exprname ir.Node) ir.Node {
|
|
// Integer range.
|
|
if c.hi != c.lo {
|
|
low := ir.NewBinaryExpr(c.pos, ir.OGE, exprname, c.lo)
|
|
high := ir.NewBinaryExpr(c.pos, ir.OLE, exprname, c.hi)
|
|
return ir.NewLogicalExpr(c.pos, ir.OANDAND, low, high)
|
|
}
|
|
|
|
// Optimize "switch true { ...}" and "switch false { ... }".
|
|
if ir.IsConst(exprname, constant.Bool) && !c.lo.Type().IsInterface() {
|
|
if ir.BoolVal(exprname) {
|
|
return c.lo
|
|
} else {
|
|
return ir.NewUnaryExpr(c.pos, ir.ONOT, c.lo)
|
|
}
|
|
}
|
|
|
|
return ir.NewBinaryExpr(c.pos, ir.OEQ, exprname, c.lo)
|
|
}
|
|
|
|
func allCaseExprsAreSideEffectFree(sw *ir.SwitchStmt) bool {
|
|
// In theory, we could be more aggressive, allowing any
|
|
// side-effect-free expressions in cases, but it's a bit
|
|
// tricky because some of that information is unavailable due
|
|
// to the introduction of temporaries during order.
|
|
// Restricting to constants is simple and probably powerful
|
|
// enough.
|
|
|
|
for _, ncase := range sw.Cases.Slice() {
|
|
ncase := ncase.(*ir.CaseStmt)
|
|
for _, v := range ncase.List.Slice() {
|
|
if v.Op() != ir.OLITERAL {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// endsInFallthrough reports whether stmts ends with a "fallthrough" statement.
|
|
func endsInFallthrough(stmts []ir.Node) (bool, src.XPos) {
|
|
// Search backwards for the index of the fallthrough
|
|
// statement. Do not assume it'll be in the last
|
|
// position, since in some cases (e.g. when the statement
|
|
// list contains autotmp_ variables), one or more OVARKILL
|
|
// nodes will be at the end of the list.
|
|
|
|
i := len(stmts) - 1
|
|
for i >= 0 && stmts[i].Op() == ir.OVARKILL {
|
|
i--
|
|
}
|
|
if i < 0 {
|
|
return false, src.NoXPos
|
|
}
|
|
return stmts[i].Op() == ir.OFALL, stmts[i].Pos()
|
|
}
|
|
|
|
// walkTypeSwitch generates an AST that implements sw, where sw is a
|
|
// type switch.
|
|
func walkTypeSwitch(sw *ir.SwitchStmt) {
|
|
var s typeSwitch
|
|
s.facename = sw.Tag.(*ir.TypeSwitchGuard).X
|
|
sw.Tag = nil
|
|
|
|
s.facename = walkexpr(s.facename, sw.PtrInit())
|
|
s.facename = copyexpr(s.facename, s.facename.Type(), &sw.Compiled)
|
|
s.okname = temp(types.Types[types.TBOOL])
|
|
|
|
// Get interface descriptor word.
|
|
// For empty interfaces this will be the type.
|
|
// For non-empty interfaces this will be the itab.
|
|
itab := ir.NewUnaryExpr(base.Pos, ir.OITAB, s.facename)
|
|
|
|
// For empty interfaces, do:
|
|
// if e._type == nil {
|
|
// do nil case if it exists, otherwise default
|
|
// }
|
|
// h := e._type.hash
|
|
// Use a similar strategy for non-empty interfaces.
|
|
ifNil := ir.NewIfStmt(base.Pos, nil, nil, nil)
|
|
ifNil.Cond = ir.NewBinaryExpr(base.Pos, ir.OEQ, itab, nodnil())
|
|
base.Pos = base.Pos.WithNotStmt() // disable statement marks after the first check.
|
|
ifNil.Cond = typecheck(ifNil.Cond, ctxExpr)
|
|
ifNil.Cond = defaultlit(ifNil.Cond, nil)
|
|
// ifNil.Nbody assigned at end.
|
|
sw.Compiled.Append(ifNil)
|
|
|
|
// Load hash from type or itab.
|
|
dotHash := ir.NewSelectorExpr(base.Pos, ir.ODOTPTR, itab, nil)
|
|
dotHash.SetType(types.Types[types.TUINT32])
|
|
dotHash.SetTypecheck(1)
|
|
if s.facename.Type().IsEmptyInterface() {
|
|
dotHash.Offset = int64(2 * Widthptr) // offset of hash in runtime._type
|
|
} else {
|
|
dotHash.Offset = int64(2 * Widthptr) // offset of hash in runtime.itab
|
|
}
|
|
dotHash.SetBounded(true) // guaranteed not to fault
|
|
s.hashname = copyexpr(dotHash, dotHash.Type(), &sw.Compiled)
|
|
|
|
br := ir.NewBranchStmt(base.Pos, ir.OBREAK, nil)
|
|
var defaultGoto, nilGoto ir.Node
|
|
var body ir.Nodes
|
|
for _, ncase := range sw.Cases.Slice() {
|
|
ncase := ncase.(*ir.CaseStmt)
|
|
var caseVar ir.Node
|
|
if ncase.Vars.Len() != 0 {
|
|
caseVar = ncase.Vars.First()
|
|
}
|
|
|
|
// For single-type cases with an interface type,
|
|
// we initialize the case variable as part of the type assertion.
|
|
// In other cases, we initialize it in the body.
|
|
var singleType *types.Type
|
|
if ncase.List.Len() == 1 && ncase.List.First().Op() == ir.OTYPE {
|
|
singleType = ncase.List.First().Type()
|
|
}
|
|
caseVarInitialized := false
|
|
|
|
label := autolabel(".s")
|
|
jmp := ir.NewBranchStmt(ncase.Pos(), ir.OGOTO, label)
|
|
|
|
if ncase.List.Len() == 0 { // default:
|
|
if defaultGoto != nil {
|
|
base.Fatalf("duplicate default case not detected during typechecking")
|
|
}
|
|
defaultGoto = jmp
|
|
}
|
|
|
|
for _, n1 := range ncase.List.Slice() {
|
|
if ir.IsNil(n1) { // case nil:
|
|
if nilGoto != nil {
|
|
base.Fatalf("duplicate nil case not detected during typechecking")
|
|
}
|
|
nilGoto = jmp
|
|
continue
|
|
}
|
|
|
|
if singleType != nil && singleType.IsInterface() {
|
|
s.Add(ncase.Pos(), n1.Type(), caseVar, jmp)
|
|
caseVarInitialized = true
|
|
} else {
|
|
s.Add(ncase.Pos(), n1.Type(), nil, jmp)
|
|
}
|
|
}
|
|
|
|
body.Append(ir.NewLabelStmt(ncase.Pos(), label))
|
|
if caseVar != nil && !caseVarInitialized {
|
|
val := s.facename
|
|
if singleType != nil {
|
|
// We have a single concrete type. Extract the data.
|
|
if singleType.IsInterface() {
|
|
base.Fatalf("singleType interface should have been handled in Add")
|
|
}
|
|
val = ifaceData(ncase.Pos(), s.facename, singleType)
|
|
}
|
|
l := []ir.Node{
|
|
ir.NewDecl(ncase.Pos(), ir.ODCL, caseVar),
|
|
ir.NewAssignStmt(ncase.Pos(), caseVar, val),
|
|
}
|
|
typecheckslice(l, ctxStmt)
|
|
body.Append(l...)
|
|
}
|
|
body.Append(ncase.Body.Slice()...)
|
|
body.Append(br)
|
|
}
|
|
sw.Cases.Set(nil)
|
|
|
|
if defaultGoto == nil {
|
|
defaultGoto = br
|
|
}
|
|
if nilGoto == nil {
|
|
nilGoto = defaultGoto
|
|
}
|
|
ifNil.Body.Set1(nilGoto)
|
|
|
|
s.Emit(&sw.Compiled)
|
|
sw.Compiled.Append(defaultGoto)
|
|
sw.Compiled.AppendNodes(&body)
|
|
|
|
walkstmtlist(sw.Compiled.Slice())
|
|
}
|
|
|
|
// A typeSwitch walks a type switch.
|
|
type typeSwitch struct {
|
|
// Temporary variables (i.e., ONAMEs) used by type switch dispatch logic:
|
|
facename ir.Node // value being type-switched on
|
|
hashname ir.Node // type hash of the value being type-switched on
|
|
okname ir.Node // boolean used for comma-ok type assertions
|
|
|
|
done ir.Nodes
|
|
clauses []typeClause
|
|
}
|
|
|
|
type typeClause struct {
|
|
hash uint32
|
|
body ir.Nodes
|
|
}
|
|
|
|
func (s *typeSwitch) Add(pos src.XPos, typ *types.Type, caseVar, jmp ir.Node) {
|
|
var body ir.Nodes
|
|
if caseVar != nil {
|
|
l := []ir.Node{
|
|
ir.NewDecl(pos, ir.ODCL, caseVar),
|
|
ir.NewAssignStmt(pos, caseVar, nil),
|
|
}
|
|
typecheckslice(l, ctxStmt)
|
|
body.Append(l...)
|
|
} else {
|
|
caseVar = ir.BlankNode
|
|
}
|
|
|
|
// cv, ok = iface.(type)
|
|
as := ir.NewAssignListStmt(pos, ir.OAS2, nil, nil)
|
|
as.Lhs.Set2(caseVar, s.okname) // cv, ok =
|
|
dot := ir.NewTypeAssertExpr(pos, s.facename, nil)
|
|
dot.SetType(typ) // iface.(type)
|
|
as.Rhs.Set1(dot)
|
|
appendWalkStmt(&body, as)
|
|
|
|
// if ok { goto label }
|
|
nif := ir.NewIfStmt(pos, nil, nil, nil)
|
|
nif.Cond = s.okname
|
|
nif.Body.Set1(jmp)
|
|
body.Append(nif)
|
|
|
|
if !typ.IsInterface() {
|
|
s.clauses = append(s.clauses, typeClause{
|
|
hash: typehash(typ),
|
|
body: body,
|
|
})
|
|
return
|
|
}
|
|
|
|
s.flush()
|
|
s.done.AppendNodes(&body)
|
|
}
|
|
|
|
func (s *typeSwitch) Emit(out *ir.Nodes) {
|
|
s.flush()
|
|
out.AppendNodes(&s.done)
|
|
}
|
|
|
|
func (s *typeSwitch) flush() {
|
|
cc := s.clauses
|
|
s.clauses = nil
|
|
if len(cc) == 0 {
|
|
return
|
|
}
|
|
|
|
sort.Slice(cc, func(i, j int) bool { return cc[i].hash < cc[j].hash })
|
|
|
|
// Combine adjacent cases with the same hash.
|
|
merged := cc[:1]
|
|
for _, c := range cc[1:] {
|
|
last := &merged[len(merged)-1]
|
|
if last.hash == c.hash {
|
|
last.body.AppendNodes(&c.body)
|
|
} else {
|
|
merged = append(merged, c)
|
|
}
|
|
}
|
|
cc = merged
|
|
|
|
binarySearch(len(cc), &s.done,
|
|
func(i int) ir.Node {
|
|
return ir.NewBinaryExpr(base.Pos, ir.OLE, s.hashname, nodintconst(int64(cc[i-1].hash)))
|
|
},
|
|
func(i int, nif *ir.IfStmt) {
|
|
// TODO(mdempsky): Omit hash equality check if
|
|
// there's only one type.
|
|
c := cc[i]
|
|
nif.Cond = ir.NewBinaryExpr(base.Pos, ir.OEQ, s.hashname, nodintconst(int64(c.hash)))
|
|
nif.Body.AppendNodes(&c.body)
|
|
},
|
|
)
|
|
}
|
|
|
|
// binarySearch constructs a binary search tree for handling n cases,
|
|
// and appends it to out. It's used for efficiently implementing
|
|
// switch statements.
|
|
//
|
|
// less(i) should return a boolean expression. If it evaluates true,
|
|
// then cases before i will be tested; otherwise, cases i and later.
|
|
//
|
|
// leaf(i, nif) should setup nif (an OIF node) to test case i. In
|
|
// particular, it should set nif.Left and nif.Nbody.
|
|
func binarySearch(n int, out *ir.Nodes, less func(i int) ir.Node, leaf func(i int, nif *ir.IfStmt)) {
|
|
const binarySearchMin = 4 // minimum number of cases for binary search
|
|
|
|
var do func(lo, hi int, out *ir.Nodes)
|
|
do = func(lo, hi int, out *ir.Nodes) {
|
|
n := hi - lo
|
|
if n < binarySearchMin {
|
|
for i := lo; i < hi; i++ {
|
|
nif := ir.NewIfStmt(base.Pos, nil, nil, nil)
|
|
leaf(i, nif)
|
|
base.Pos = base.Pos.WithNotStmt()
|
|
nif.Cond = typecheck(nif.Cond, ctxExpr)
|
|
nif.Cond = defaultlit(nif.Cond, nil)
|
|
out.Append(nif)
|
|
out = &nif.Else
|
|
}
|
|
return
|
|
}
|
|
|
|
half := lo + n/2
|
|
nif := ir.NewIfStmt(base.Pos, nil, nil, nil)
|
|
nif.Cond = less(half)
|
|
base.Pos = base.Pos.WithNotStmt()
|
|
nif.Cond = typecheck(nif.Cond, ctxExpr)
|
|
nif.Cond = defaultlit(nif.Cond, nil)
|
|
do(lo, half, &nif.Body)
|
|
do(half, hi, &nif.Else)
|
|
out.Append(nif)
|
|
}
|
|
|
|
do(0, n, out)
|
|
}
|