mirror of https://github.com/golang/go.git
[dev.regabi] cmd/compile: refactor type/value assertions
Small refactoring to make subsequent CLs clearer. Passes toolstash-check. Change-Id: I1a6ae599f491220d44aaabae0b7bed4aff46ee92 Reviewed-on: https://go-review.googlesource.com/c/go/+/272651 Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
88a9e2f9ad
commit
6dae48fb0b
|
|
@ -275,8 +275,8 @@ func convlit1(n *Node, t *types.Type, explicit bool, context func() string) *Nod
|
||||||
if v.U == nil {
|
if v.U == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
n.SetVal(v)
|
|
||||||
n.Type = t
|
n.Type = t
|
||||||
|
n.SetVal(v)
|
||||||
return n
|
return n
|
||||||
|
|
||||||
case OPLUS, ONEG, OBITNOT, ONOT, OREAL, OIMAG:
|
case OPLUS, ONEG, OBITNOT, ONOT, OREAL, OIMAG:
|
||||||
|
|
@ -979,9 +979,6 @@ func setconst(n *Node, v Val) {
|
||||||
Xoffset: BADWIDTH,
|
Xoffset: BADWIDTH,
|
||||||
}
|
}
|
||||||
n.SetVal(v)
|
n.SetVal(v)
|
||||||
if vt := idealType(v.Ctype()); n.Type.IsUntyped() && n.Type != vt {
|
|
||||||
Fatalf("untyped type mismatch, have: %v, want: %v", n.Type, vt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check range.
|
// Check range.
|
||||||
lno := setlineno(n)
|
lno := setlineno(n)
|
||||||
|
|
@ -1000,6 +997,22 @@ func setconst(n *Node, v Val) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertRepresents(t *types.Type, v Val) {
|
||||||
|
if !represents(t, v) {
|
||||||
|
Fatalf("%v does not represent %v", t, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func represents(t *types.Type, v Val) bool {
|
||||||
|
if !t.IsUntyped() {
|
||||||
|
// TODO(mdempsky): Stricter handling of typed types.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
vt := idealType(v.Ctype())
|
||||||
|
return t == vt
|
||||||
|
}
|
||||||
|
|
||||||
func setboolconst(n *Node, v bool) {
|
func setboolconst(n *Node, v bool) {
|
||||||
setconst(n, Val{U: v})
|
setconst(n, Val{U: v})
|
||||||
}
|
}
|
||||||
|
|
@ -1013,8 +1026,8 @@ func setintconst(n *Node, v int64) {
|
||||||
// nodlit returns a new untyped constant with value v.
|
// nodlit returns a new untyped constant with value v.
|
||||||
func nodlit(v Val) *Node {
|
func nodlit(v Val) *Node {
|
||||||
n := nod(OLITERAL, nil, nil)
|
n := nod(OLITERAL, nil, nil)
|
||||||
n.SetVal(v)
|
|
||||||
n.Type = idealType(v.Ctype())
|
n.Type = idealType(v.Ctype())
|
||||||
|
n.SetVal(v)
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -777,9 +777,7 @@ func constTypeOf(typ *types.Type) Ctype {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *exportWriter) value(typ *types.Type, v Val) {
|
func (w *exportWriter) value(typ *types.Type, v Val) {
|
||||||
if vt := idealType(v.Ctype()); typ.IsUntyped() && typ != vt {
|
assertRepresents(typ, v)
|
||||||
Fatalf("exporter: untyped type mismatch, have: %v, want: %v", typ, vt)
|
|
||||||
}
|
|
||||||
w.typ(typ)
|
w.typ(typ)
|
||||||
|
|
||||||
// Each type has only one admissible constant representation,
|
// Each type has only one admissible constant representation,
|
||||||
|
|
|
||||||
|
|
@ -251,6 +251,9 @@ func (n *Node) SetVal(v Val) {
|
||||||
Dump("have Opt", n)
|
Dump("have Opt", n)
|
||||||
Fatalf("have Opt")
|
Fatalf("have Opt")
|
||||||
}
|
}
|
||||||
|
if n.Op == OLITERAL {
|
||||||
|
assertRepresents(n.Type, v)
|
||||||
|
}
|
||||||
n.SetHasVal(true)
|
n.SetHasVal(true)
|
||||||
n.E = v.U
|
n.E = v.U
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3624,8 +3624,8 @@ func typecheckdef(n *Node) {
|
||||||
e = convlit(e, t)
|
e = convlit(e, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
n.SetVal(e.Val())
|
|
||||||
n.Type = e.Type
|
n.Type = e.Type
|
||||||
|
n.SetVal(e.Val())
|
||||||
|
|
||||||
case ONAME:
|
case ONAME:
|
||||||
if n.Name.Param.Ntype != nil {
|
if n.Name.Param.Ntype != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue