CTxxx Ctype = iota
CTINT
- CTRUNE
CTFLT
CTCPLX
CTSTR
type Val struct {
// U contains one of:
// bool bool when Ctype() == CTBOOL
- // *Mpint int when Ctype() == CTINT, rune when Ctype() == CTRUNE
+ // *Mpint int when Ctype() == CTINT
// *Mpflt float when Ctype() == CTFLT
// *Mpcplx pair of floats when Ctype() == CTCPLX
// string string when Ctype() == CTSTR
}
func (v Val) Ctype() Ctype {
- switch x := v.U.(type) {
+ switch v.U.(type) {
default:
Fatalf("unexpected Ctype for %T", v.U)
panic("unreachable")
case bool:
return CTBOOL
case *Mpint:
- if x.Rune {
- return CTRUNE
- }
return CTINT
case *Mpflt:
return CTFLT
return v
}
- case CTINT, CTRUNE:
+ case CTINT:
if explicit && t.IsString() {
return tostr(v)
}
func toint(v Val) Val {
switch u := v.U.(type) {
case *Mpint:
- if u.Rune {
- i := new(Mpint)
- i.Set(u)
- v.U = i
- }
case *Mpflt:
i := new(Mpint)
}
func Isconst(n *Node, ct Ctype) bool {
- t := consttype(n)
-
- // If the caller is asking for CTINT, allow CTRUNE too.
- // Makes life easier for back ends.
- return t == ct || (ct == CTINT && t == CTRUNE)
+ return consttype(n) == ct
}
// evconst rewrites constant expressions into OLITERAL nodes.
return x != y
}
- case CTINT, CTRUNE:
+ case CTINT:
x, y := x.U.(*Mpint), y.U.(*Mpint)
return cmpZero(x.Cmp(y), op)
return Val{U: x || y}
}
- case CTINT, CTRUNE:
+ case CTINT:
x, y := x.U.(*Mpint), y.U.(*Mpint)
u := new(Mpint)
- u.Rune = x.Rune || y.Rune
u.Set(x)
switch op {
case OADD:
switch op {
case OPLUS:
switch x.Ctype() {
- case CTINT, CTRUNE, CTFLT, CTCPLX:
+ case CTINT, CTFLT, CTCPLX:
return x
}
case ONEG:
switch x.Ctype() {
- case CTINT, CTRUNE:
+ case CTINT:
x := x.U.(*Mpint)
u := new(Mpint)
- u.Rune = x.Rune
u.Set(x)
u.Neg()
return Val{U: u}
case OBITNOT:
switch x.Ctype() {
- case CTINT, CTRUNE:
+ case CTINT:
x := x.U.(*Mpint)
u := new(Mpint)
- u.Rune = x.Rune
if t.IsSigned() || t.IsUntyped() {
// Signed values change sign.
u.SetInt64(-1)
}
func shiftOp(x Val, op Op, y Val) Val {
- if x.Ctype() != CTRUNE {
- x = toint(x)
- }
+ x = toint(x)
y = toint(y)
u := new(Mpint)
u.Set(x.U.(*Mpint))
- u.Rune = x.U.(*Mpint).Rune
switch op {
case OLSH:
u.Lsh(y.U.(*Mpint))
}
vt := idealType(v.Ctype())
- return t == vt
+ return t == vt || (t == types.UntypedRune && vt == types.UntypedInt)
}
func setboolconst(n *Node, v bool) {
return types.UntypedBool
case CTINT:
return types.UntypedInt
- case CTRUNE:
- return types.UntypedRune
case CTFLT:
return types.UntypedFloat
case CTCPLX:
return l, r
}
-func ctype(t *types.Type) Ctype {
- switch t {
- case types.UntypedBool:
- return CTBOOL
- case types.UntypedString:
- return CTSTR
- case types.UntypedInt:
- return CTINT
- case types.UntypedRune:
- return CTRUNE
- case types.UntypedFloat:
- return CTFLT
- case types.UntypedComplex:
- return CTCPLX
+func mixUntyped(t1, t2 *types.Type) *types.Type {
+ if t1 == t2 {
+ return t1
+ }
+
+ rank := func(t *types.Type) int {
+ switch t {
+ case types.UntypedInt:
+ return 0
+ case types.UntypedRune:
+ return 1
+ case types.UntypedFloat:
+ return 2
+ case types.UntypedComplex:
+ return 3
+ }
+ Fatalf("bad type %v", t)
+ panic("unreachable")
}
- Fatalf("bad type %v", t)
- panic("unreachable")
-}
-func mixUntyped(t1, t2 *types.Type) *types.Type {
- t := t1
- if ctype(t2) > ctype(t1) {
- t = t2
+ if rank(t2) > rank(t1) {
+ return t2
}
- return t
+ return t1
}
func defaultType(t *types.Type) *types.Type {
func (v Val) vconv(s fmt.State, flag FmtFlag) {
switch u := v.U.(type) {
case *Mpint:
- if !u.Rune {
- if flag&FmtSharp != 0 {
- fmt.Fprint(s, u.String())
- return
- }
- fmt.Fprint(s, u.GoString())
+ if flag&FmtSharp != 0 {
+ fmt.Fprint(s, u.String())
return
}
-
- switch x := u.Int64(); {
- case ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'':
- fmt.Fprintf(s, "'%c'", int(x))
-
- case 0 <= x && x < 1<<16:
- fmt.Fprintf(s, "'\\u%04x'", uint(int(x)))
-
- case 0 <= x && x <= utf8.MaxRune:
- fmt.Fprintf(s, "'\\U%08x'", uint64(x))
-
- default:
- fmt.Fprintf(s, "('\\x00' + %v)", u)
- }
+ fmt.Fprint(s, u.GoString())
+ return
case *Mpflt:
if flag&FmtSharp != 0 {
}
}
+ needUnparen := false
if n.Type != nil && !n.Type.IsUntyped() {
// Need parens when type begins with what might
// be misinterpreted as a unary operator: * or <-.
if n.Type.IsPtr() || (n.Type.IsChan() && n.Type.ChanDir() == types.Crecv) {
- mode.Fprintf(s, "(%v)(%v)", n.Type, n.Val())
- return
+ mode.Fprintf(s, "(%v)(", n.Type)
} else {
- mode.Fprintf(s, "%v(%v)", n.Type, n.Val())
- return
+ mode.Fprintf(s, "%v(", n.Type)
}
+ needUnparen = true
}
- mode.Fprintf(s, "%v", n.Val())
+ if n.Type == types.UntypedRune {
+ u := n.Val().U.(*Mpint)
+ switch x := u.Int64(); {
+ case ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'':
+ fmt.Fprintf(s, "'%c'", int(x))
+
+ case 0 <= x && x < 1<<16:
+ fmt.Fprintf(s, "'\\u%04x'", uint(int(x)))
+
+ case 0 <= x && x <= utf8.MaxRune:
+ fmt.Fprintf(s, "'\\U%08x'", uint64(x))
+
+ default:
+ fmt.Fprintf(s, "('\\x00' + %v)", u)
+ }
+ } else {
+ mode.Fprintf(s, "%v", n.Val())
+ }
+
+ if needUnparen {
+ mode.Fprintf(s, ")")
+ }
// Special case: name used as local variable in export.
// _ becomes ~b%d internally; print as _ for export
v.U = p.string()
case CTINT:
x := new(Mpint)
- x.Rune = typ == types.UntypedRune
p.mpint(&x.Val, typ)
v.U = x
case CTFLT:
// Mpint represents an integer constant.
type Mpint struct {
- Val big.Int
- Ovf bool // set if Val overflowed compiler limit (sticky)
- Rune bool // set if syntax indicates default type rune
+ Val big.Int
+ Ovf bool // set if Val overflowed compiler limit (sticky)
}
func (a *Mpint) SetOverflow() {
return p.mkname(expr)
case *syntax.BasicLit:
n := nodlit(p.basicLit(expr))
+ if expr.Kind == syntax.RuneLit {
+ n.Type = types.UntypedRune
+ }
n.SetDiag(expr.Bad) // avoid follow-on errors if there was a syntax error
return n
case *syntax.CompositeLit:
case syntax.RuneLit:
x := new(Mpint)
- x.Rune = true
if !lit.Bad {
u, _ := strconv.Unquote(s)
var r rune
// Do range checks for constants before defaultlit
// to avoid redundant "constant NNN overflows int" errors.
switch consttype(n) {
- case CTINT, CTRUNE, CTFLT, CTCPLX:
+ case CTINT, CTFLT, CTCPLX:
v := toint(n.Val()).U.(*Mpint)
if v.CmpInt64(0) < 0 {
yyerror("negative %s argument in make(%v)", arg, t)
calls := []*Node{mkcall("printlock", nil, init)}
for i, n := range nn.List.Slice() {
if n.Op == OLITERAL {
- switch n.Val().Ctype() {
- case CTRUNE:
+ if n.Type == types.UntypedRune {
n = defaultlit(n, types.Runetype)
+ }
+ switch n.Val().Ctype() {
case CTINT:
n = defaultlit(n, types.Types[TINT64])