Commit ecead89b authored by Rob Pike's avatar Rob Pike

go/types: remove the renaming import of go/constant

For niceness, when go/exact was moved from x/tools, it
was renamed go/constant.

For simplicity, when go/types was moved from x/tools, its
imports of (now) go/constant were done with a rename:

    import exact "go/constant"

This kept the code just as it was before and avoided the issue
of what to call the internal constant called, um, constant.

But not all was hidden, as the text of some fields of structs and
the like leaked the old name, so things like "exact.Value" appeared
in type definitions and function signatures in the documentation.
This is unacceptable.

Fix the documentation issue by fixing the code. Rename the constant
constant constant_, and remove the renaming import.

This should go into 1.5. It's mostly a mechanical change, is
internal to the package, and fixes the documentation. It contains
no semantic changes except to fix a benchmark that was broken
in the original transition.

Fixes #11949.

Change-Id: Ieb94b6558535b504180b1378f19e8f5a96f92d3c
Reviewed-on: https://go-review.googlesource.com/13051Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 2bd52370
...@@ -28,7 +28,7 @@ import ( ...@@ -28,7 +28,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
) )
...@@ -218,7 +218,7 @@ func (info *Info) ObjectOf(id *ast.Ident) Object { ...@@ -218,7 +218,7 @@ func (info *Info) ObjectOf(id *ast.Ident) Object {
type TypeAndValue struct { type TypeAndValue struct {
mode operandMode mode operandMode
Type Type Type Type
Value exact.Value // == constant.Value Value constant.Value
} }
// TODO(gri) Consider eliminating the IsVoid predicate. Instead, report // TODO(gri) Consider eliminating the IsVoid predicate. Instead, report
...@@ -246,7 +246,7 @@ func (tv TypeAndValue) IsBuiltin() bool { ...@@ -246,7 +246,7 @@ func (tv TypeAndValue) IsBuiltin() bool {
// nil Value. // nil Value.
func (tv TypeAndValue) IsValue() bool { func (tv TypeAndValue) IsValue() bool {
switch tv.mode { switch tv.mode {
case constant, variable, mapindex, value, commaok: case constant_, variable, mapindex, value, commaok:
return true return true
} }
return false return false
......
...@@ -23,7 +23,7 @@ func (check *Checker) assignment(x *operand, T Type) bool { ...@@ -23,7 +23,7 @@ func (check *Checker) assignment(x *operand, T Type) bool {
switch x.mode { switch x.mode {
case invalid: case invalid:
return true // error reported before return true // error reported before
case constant, variable, mapindex, value, commaok: case constant_, variable, mapindex, value, commaok:
// ok // ok
default: default:
unreachable() unreachable()
...@@ -74,7 +74,7 @@ func (check *Checker) initConst(lhs *Const, x *operand) { ...@@ -74,7 +74,7 @@ func (check *Checker) initConst(lhs *Const, x *operand) {
} }
// rhs must be a constant // rhs must be a constant
if x.mode != constant { if x.mode != constant_ {
check.errorf(x.pos(), "%s is not constant", x) check.errorf(x.pos(), "%s is not constant", x)
if lhs.typ == nil { if lhs.typ == nil {
lhs.typ = Typ[Invalid] lhs.typ = Typ[Invalid]
......
...@@ -8,7 +8,7 @@ package types ...@@ -8,7 +8,7 @@ package types
import ( import (
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
) )
...@@ -138,13 +138,13 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -138,13 +138,13 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
// len(x) // len(x)
mode := invalid mode := invalid
var typ Type var typ Type
var val exact.Value var val constant.Value
switch typ = implicitArrayDeref(x.typ.Underlying()); t := typ.(type) { switch typ = implicitArrayDeref(x.typ.Underlying()); t := typ.(type) {
case *Basic: case *Basic:
if isString(t) && id == _Len { if isString(t) && id == _Len {
if x.mode == constant { if x.mode == constant_ {
mode = constant mode = constant_
val = exact.MakeInt64(int64(len(exact.StringVal(x.val)))) val = constant.MakeInt64(int64(len(constant.StringVal(x.val))))
} else { } else {
mode = value mode = value
} }
...@@ -157,8 +157,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -157,8 +157,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
// the expression s does not contain channel receives or // the expression s does not contain channel receives or
// function calls; in this case s is not evaluated." // function calls; in this case s is not evaluated."
if !check.hasCallOrRecv { if !check.hasCallOrRecv {
mode = constant mode = constant_
val = exact.MakeInt64(t.len) val = constant.MakeInt64(t.len)
} }
case *Slice, *Chan: case *Slice, *Chan:
...@@ -178,7 +178,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -178,7 +178,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
x.mode = mode x.mode = mode
x.typ = Typ[Int] x.typ = Typ[Int]
x.val = val x.val = val
if check.Types != nil && mode != constant { if check.Types != nil && mode != constant_ {
check.recordBuiltinType(call.Fun, makeSig(x.typ, typ)) check.recordBuiltinType(call.Fun, makeSig(x.typ, typ))
} }
...@@ -228,8 +228,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -228,8 +228,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
return return
} }
if x.mode == constant && y.mode == constant { if x.mode == constant_ && y.mode == constant_ {
x.val = exact.BinaryOp(x.val, token.ADD, exact.MakeImag(y.val)) x.val = constant.BinaryOp(x.val, token.ADD, constant.MakeImag(y.val))
} else { } else {
x.mode = value x.mode = value
} }
...@@ -242,7 +242,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -242,7 +242,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
case Float64: case Float64:
complexT = Typ[Complex128] complexT = Typ[Complex128]
case UntypedInt, UntypedRune, UntypedFloat: case UntypedInt, UntypedRune, UntypedFloat:
if x.mode == constant { if x.mode == constant_ {
realT = defaultType(realT).(*Basic) realT = defaultType(realT).(*Basic)
complexT = Typ[UntypedComplex] complexT = Typ[UntypedComplex]
} else { } else {
...@@ -257,11 +257,11 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -257,11 +257,11 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
} }
x.typ = complexT x.typ = complexT
if check.Types != nil && x.mode != constant { if check.Types != nil && x.mode != constant_ {
check.recordBuiltinType(call.Fun, makeSig(complexT, realT, realT)) check.recordBuiltinType(call.Fun, makeSig(complexT, realT, realT))
} }
if x.mode != constant { if x.mode != constant_ {
// The arguments have now their final types, which at run- // The arguments have now their final types, which at run-
// time will be materialized. Update the expression trees. // time will be materialized. Update the expression trees.
// If the current types are untyped, the materialized type // If the current types are untyped, the materialized type
...@@ -339,11 +339,11 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -339,11 +339,11 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
check.invalidArg(x.pos(), "%s must be a complex number", x) check.invalidArg(x.pos(), "%s must be a complex number", x)
return return
} }
if x.mode == constant { if x.mode == constant_ {
if id == _Real { if id == _Real {
x.val = exact.Real(x.val) x.val = constant.Real(x.val)
} else { } else {
x.val = exact.Imag(x.val) x.val = constant.Imag(x.val)
} }
} else { } else {
x.mode = value x.mode = value
...@@ -360,7 +360,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -360,7 +360,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
unreachable() unreachable()
} }
if check.Types != nil && x.mode != constant { if check.Types != nil && x.mode != constant_ {
check.recordBuiltinType(call.Fun, makeSig(Typ[k], x.typ)) check.recordBuiltinType(call.Fun, makeSig(Typ[k], x.typ))
} }
x.typ = Typ[k] x.typ = Typ[k]
...@@ -471,8 +471,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -471,8 +471,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
return return
} }
x.mode = constant x.mode = constant_
x.val = exact.MakeInt64(check.conf.alignof(x.typ)) x.val = constant.MakeInt64(check.conf.alignof(x.typ))
x.typ = Typ[Uintptr] x.typ = Typ[Uintptr]
// result is constant - no need to record signature // result is constant - no need to record signature
...@@ -516,8 +516,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -516,8 +516,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
check.recordSelection(selx, FieldVal, base, obj, index, false) check.recordSelection(selx, FieldVal, base, obj, index, false)
offs := check.conf.offsetof(base, index) offs := check.conf.offsetof(base, index)
x.mode = constant x.mode = constant_
x.val = exact.MakeInt64(offs) x.val = constant.MakeInt64(offs)
x.typ = Typ[Uintptr] x.typ = Typ[Uintptr]
// result is constant - no need to record signature // result is constant - no need to record signature
...@@ -528,8 +528,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -528,8 +528,8 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
return return
} }
x.mode = constant x.mode = constant_
x.val = exact.MakeInt64(check.conf.sizeof(x.typ)) x.val = constant.MakeInt64(check.conf.sizeof(x.typ))
x.typ = Typ[Uintptr] x.typ = Typ[Uintptr]
// result is constant - no need to record signature // result is constant - no need to record signature
...@@ -537,15 +537,15 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b ...@@ -537,15 +537,15 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
// assert(pred) causes a typechecker error if pred is false. // assert(pred) causes a typechecker error if pred is false.
// The result of assert is the value of pred if there is no error. // The result of assert is the value of pred if there is no error.
// Note: assert is only available in self-test mode. // Note: assert is only available in self-test mode.
if x.mode != constant || !isBoolean(x.typ) { if x.mode != constant_ || !isBoolean(x.typ) {
check.invalidArg(x.pos(), "%s is not a boolean constant", x) check.invalidArg(x.pos(), "%s is not a boolean constant", x)
return return
} }
if x.val.Kind() != exact.Bool { if x.val.Kind() != constant.Bool {
check.errorf(x.pos(), "internal error: value of %s should be a boolean constant", x) check.errorf(x.pos(), "internal error: value of %s should be a boolean constant", x)
return return
} }
if !exact.BoolVal(x.val) { if !constant.BoolVal(x.val) {
check.errorf(call.Pos(), "%s failed", call) check.errorf(call.Pos(), "%s failed", call)
// compile-time assertion failure - safe to continue // compile-time assertion failure - safe to continue
} }
......
...@@ -46,7 +46,7 @@ func (check *Checker) call(x *operand, e *ast.CallExpr) exprKind { ...@@ -46,7 +46,7 @@ func (check *Checker) call(x *operand, e *ast.CallExpr) exprKind {
} }
x.expr = e x.expr = e
// a non-constant result implies a function call // a non-constant result implies a function call
if x.mode != invalid && x.mode != constant { if x.mode != invalid && x.mode != constant_ {
check.hasCallOrRecv = true check.hasCallOrRecv = true
} }
return predeclaredFuncs[id].kind return predeclaredFuncs[id].kind
...@@ -302,7 +302,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) { ...@@ -302,7 +302,7 @@ func (check *Checker) selector(x *operand, e *ast.SelectorExpr) {
switch exp := exp.(type) { switch exp := exp.(type) {
case *Const: case *Const:
assert(exp.Val() != nil) assert(exp.Val() != nil)
x.mode = constant x.mode = constant_
x.typ = exp.typ x.typ = exp.typ
x.val = exp.val x.val = exp.val
case *TypeName: case *TypeName:
......
...@@ -8,7 +8,7 @@ package types ...@@ -8,7 +8,7 @@ package types
import ( import (
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
) )
...@@ -36,7 +36,7 @@ type exprInfo struct { ...@@ -36,7 +36,7 @@ type exprInfo struct {
isLhs bool // expression is lhs operand of a shift with delayed type-check isLhs bool // expression is lhs operand of a shift with delayed type-check
mode operandMode mode operandMode
typ *Basic typ *Basic
val exact.Value // constant value; or nil (if not a constant) val constant.Value // constant value; or nil (if not a constant)
} }
// funcInfo stores the information required for type-checking a function. // funcInfo stores the information required for type-checking a function.
...@@ -51,7 +51,7 @@ type funcInfo struct { ...@@ -51,7 +51,7 @@ type funcInfo struct {
type context struct { type context struct {
decl *declInfo // package-level declaration whose init expression/function body is checked decl *declInfo // package-level declaration whose init expression/function body is checked
scope *Scope // top-most scope for lookups scope *Scope // top-most scope for lookups
iota exact.Value // value of iota in a constant declaration; nil otherwise iota constant.Value // value of iota in a constant declaration; nil otherwise
sig *Signature // function signature if inside a function; nil otherwise sig *Signature // function signature if inside a function; nil otherwise
hasLabel bool // set if a function makes use of labels (only ~1% of functions); unused outside functions hasLabel bool // set if a function makes use of labels (only ~1% of functions); unused outside functions
hasCallOrRecv bool // set if an expression contains a function call or channel receive operation hasCallOrRecv bool // set if an expression contains a function call or channel receive operation
...@@ -126,7 +126,7 @@ func (check *Checker) assocMethod(tname string, meth *Func) { ...@@ -126,7 +126,7 @@ func (check *Checker) assocMethod(tname string, meth *Func) {
m[tname] = append(m[tname], meth) m[tname] = append(m[tname], meth)
} }
func (check *Checker) rememberUntyped(e ast.Expr, lhs bool, mode operandMode, typ *Basic, val exact.Value) { func (check *Checker) rememberUntyped(e ast.Expr, lhs bool, mode operandMode, typ *Basic, val constant.Value) {
m := check.untyped m := check.untyped
if m == nil { if m == nil {
m = make(map[ast.Expr]exprInfo) m = make(map[ast.Expr]exprInfo)
...@@ -257,14 +257,14 @@ func (check *Checker) recordUntyped() { ...@@ -257,14 +257,14 @@ func (check *Checker) recordUntyped() {
} }
} }
func (check *Checker) recordTypeAndValue(x ast.Expr, mode operandMode, typ Type, val exact.Value) { func (check *Checker) recordTypeAndValue(x ast.Expr, mode operandMode, typ Type, val constant.Value) {
assert(x != nil) assert(x != nil)
assert(typ != nil) assert(typ != nil)
if mode == invalid { if mode == invalid {
return // omit return // omit
} }
assert(typ != nil) assert(typ != nil)
if mode == constant { if mode == constant_ {
assert(val != nil) assert(val != nil)
assert(typ == Typ[Invalid] || isConstType(typ)) assert(typ == Typ[Invalid] || isConstType(typ))
} }
......
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
package types package types
import exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove import "go/constant"
// Conversion type-checks the conversion T(x). // Conversion type-checks the conversion T(x).
// The result is in x. // The result is in x.
func (check *Checker) conversion(x *operand, T Type) { func (check *Checker) conversion(x *operand, T Type) {
constArg := x.mode == constant constArg := x.mode == constant_
var ok bool var ok bool
switch { switch {
...@@ -22,13 +22,13 @@ func (check *Checker) conversion(x *operand, T Type) { ...@@ -22,13 +22,13 @@ func (check *Checker) conversion(x *operand, T Type) {
ok = true ok = true
case isInteger(x.typ) && isString(t): case isInteger(x.typ) && isString(t):
codepoint := int64(-1) codepoint := int64(-1)
if i, ok := exact.Int64Val(x.val); ok { if i, ok := constant.Int64Val(x.val); ok {
codepoint = i codepoint = i
} }
// If codepoint < 0 the absolute value is too large (or unknown) for // If codepoint < 0 the absolute value is too large (or unknown) for
// conversion. This is the same as converting any other out-of-range // conversion. This is the same as converting any other out-of-range
// value - let string(codepoint) do the work. // value - let string(codepoint) do the work.
x.val = exact.MakeString(string(codepoint)) x.val = constant.MakeString(string(codepoint))
ok = true ok = true
} }
case x.convertibleTo(check.conf, T): case x.convertibleTo(check.conf, T):
......
...@@ -6,7 +6,7 @@ package types ...@@ -6,7 +6,7 @@ package types
import ( import (
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
) )
...@@ -105,7 +105,7 @@ func (check *Checker) constDecl(obj *Const, typ, init ast.Expr) { ...@@ -105,7 +105,7 @@ func (check *Checker) constDecl(obj *Const, typ, init ast.Expr) {
defer func() { check.iota = nil }() defer func() { check.iota = nil }()
// provide valid constant value under all circumstances // provide valid constant value under all circumstances
obj.val = exact.MakeUnknown() obj.val = constant.MakeUnknown()
// determine type, if any // determine type, if any
if typ != nil { if typ != nil {
...@@ -335,7 +335,7 @@ func (check *Checker) declStmt(decl ast.Decl) { ...@@ -335,7 +335,7 @@ func (check *Checker) declStmt(decl ast.Decl) {
// declare all constants // declare all constants
lhs := make([]*Const, len(s.Names)) lhs := make([]*Const, len(s.Names))
for i, name := range s.Names { for i, name := range s.Names {
obj := NewConst(name.Pos(), pkg, name.Name, nil, exact.MakeInt64(int64(iota))) obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(iota)))
lhs[i] = obj lhs[i] = obj
var init ast.Expr var init ast.Expr
......
...@@ -9,7 +9,7 @@ package types ...@@ -9,7 +9,7 @@ package types
import ( import (
"fmt" "fmt"
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
"math" "math"
) )
...@@ -116,13 +116,13 @@ func (check *Checker) unary(x *operand, e *ast.UnaryExpr, op token.Token) { ...@@ -116,13 +116,13 @@ func (check *Checker) unary(x *operand, e *ast.UnaryExpr, op token.Token) {
return return
} }
if x.mode == constant { if x.mode == constant_ {
typ := x.typ.Underlying().(*Basic) typ := x.typ.Underlying().(*Basic)
var prec uint var prec uint
if isUnsigned(typ) { if isUnsigned(typ) {
prec = uint(check.conf.sizeof(typ) * 8) prec = uint(check.conf.sizeof(typ) * 8)
} }
x.val = exact.UnaryOp(op, x.val, prec) x.val = constant.UnaryOp(op, x.val, prec)
// Typed constants must be representable in // Typed constants must be representable in
// their type after each constant operation. // their type after each constant operation.
if isTyped(typ) { if isTyped(typ) {
...@@ -151,30 +151,30 @@ func isComparison(op token.Token) bool { ...@@ -151,30 +151,30 @@ func isComparison(op token.Token) bool {
return false return false
} }
func fitsFloat32(x exact.Value) bool { func fitsFloat32(x constant.Value) bool {
f32, _ := exact.Float32Val(x) f32, _ := constant.Float32Val(x)
f := float64(f32) f := float64(f32)
return !math.IsInf(f, 0) return !math.IsInf(f, 0)
} }
func roundFloat32(x exact.Value) exact.Value { func roundFloat32(x constant.Value) constant.Value {
f32, _ := exact.Float32Val(x) f32, _ := constant.Float32Val(x)
f := float64(f32) f := float64(f32)
if !math.IsInf(f, 0) { if !math.IsInf(f, 0) {
return exact.MakeFloat64(f) return constant.MakeFloat64(f)
} }
return nil return nil
} }
func fitsFloat64(x exact.Value) bool { func fitsFloat64(x constant.Value) bool {
f, _ := exact.Float64Val(x) f, _ := constant.Float64Val(x)
return !math.IsInf(f, 0) return !math.IsInf(f, 0)
} }
func roundFloat64(x exact.Value) exact.Value { func roundFloat64(x constant.Value) constant.Value {
f, _ := exact.Float64Val(x) f, _ := constant.Float64Val(x)
if !math.IsInf(f, 0) { if !math.IsInf(f, 0) {
return exact.MakeFloat64(f) return constant.MakeFloat64(f)
} }
return nil return nil
} }
...@@ -186,16 +186,16 @@ func roundFloat64(x exact.Value) exact.Value { ...@@ -186,16 +186,16 @@ func roundFloat64(x exact.Value) exact.Value {
// If rounded != nil, *rounded is set to the rounded value of x for // If rounded != nil, *rounded is set to the rounded value of x for
// representable floating-point values; it is left alone otherwise. // representable floating-point values; it is left alone otherwise.
// It is ok to provide the addressof the first argument for rounded. // It is ok to provide the addressof the first argument for rounded.
func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exact.Value) bool { func representableConst(x constant.Value, conf *Config, as BasicKind, rounded *constant.Value) bool {
switch x.Kind() { switch x.Kind() {
case exact.Unknown: case constant.Unknown:
return true return true
case exact.Bool: case constant.Bool:
return as == Bool || as == UntypedBool return as == Bool || as == UntypedBool
case exact.Int: case constant.Int:
if x, ok := exact.Int64Val(x); ok { if x, ok := constant.Int64Val(x); ok {
switch as { switch as {
case Int: case Int:
var s = uint(conf.sizeof(Typ[as])) * 8 var s = uint(conf.sizeof(Typ[as])) * 8
...@@ -233,13 +233,13 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac ...@@ -233,13 +233,13 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac
} }
} }
n := exact.BitLen(x) n := constant.BitLen(x)
switch as { switch as {
case Uint, Uintptr: case Uint, Uintptr:
var s = uint(conf.sizeof(Typ[as])) * 8 var s = uint(conf.sizeof(Typ[as])) * 8
return exact.Sign(x) >= 0 && n <= int(s) return constant.Sign(x) >= 0 && n <= int(s)
case Uint64: case Uint64:
return exact.Sign(x) >= 0 && n <= 64 return constant.Sign(x) >= 0 && n <= 64
case Float32, Complex64: case Float32, Complex64:
if rounded == nil { if rounded == nil {
return fitsFloat32(x) return fitsFloat32(x)
...@@ -262,7 +262,7 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac ...@@ -262,7 +262,7 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac
return true return true
} }
case exact.Float: case constant.Float:
switch as { switch as {
case Float32, Complex64: case Float32, Complex64:
if rounded == nil { if rounded == nil {
...@@ -286,33 +286,33 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac ...@@ -286,33 +286,33 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac
return true return true
} }
case exact.Complex: case constant.Complex:
switch as { switch as {
case Complex64: case Complex64:
if rounded == nil { if rounded == nil {
return fitsFloat32(exact.Real(x)) && fitsFloat32(exact.Imag(x)) return fitsFloat32(constant.Real(x)) && fitsFloat32(constant.Imag(x))
} }
re := roundFloat32(exact.Real(x)) re := roundFloat32(constant.Real(x))
im := roundFloat32(exact.Imag(x)) im := roundFloat32(constant.Imag(x))
if re != nil && im != nil { if re != nil && im != nil {
*rounded = exact.BinaryOp(re, token.ADD, exact.MakeImag(im)) *rounded = constant.BinaryOp(re, token.ADD, constant.MakeImag(im))
return true return true
} }
case Complex128: case Complex128:
if rounded == nil { if rounded == nil {
return fitsFloat64(exact.Real(x)) && fitsFloat64(exact.Imag(x)) return fitsFloat64(constant.Real(x)) && fitsFloat64(constant.Imag(x))
} }
re := roundFloat64(exact.Real(x)) re := roundFloat64(constant.Real(x))
im := roundFloat64(exact.Imag(x)) im := roundFloat64(constant.Imag(x))
if re != nil && im != nil { if re != nil && im != nil {
*rounded = exact.BinaryOp(re, token.ADD, exact.MakeImag(im)) *rounded = constant.BinaryOp(re, token.ADD, constant.MakeImag(im))
return true return true
} }
case UntypedComplex: case UntypedComplex:
return true return true
} }
case exact.String: case constant.String:
return as == String || as == UntypedString return as == String || as == UntypedString
default: default:
...@@ -324,7 +324,7 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac ...@@ -324,7 +324,7 @@ func representableConst(x exact.Value, conf *Config, as BasicKind, rounded *exac
// representable checks that a constant operand is representable in the given basic type. // representable checks that a constant operand is representable in the given basic type.
func (check *Checker) representable(x *operand, typ *Basic) { func (check *Checker) representable(x *operand, typ *Basic) {
assert(x.mode == constant) assert(x.mode == constant_)
if !representableConst(x.val, check.conf, typ.kind, &x.val) { if !representableConst(x.val, check.conf, typ.kind, &x.val) {
var msg string var msg string
if isNumeric(x.typ) && isNumeric(typ) { if isNumeric(x.typ) && isNumeric(typ) {
...@@ -458,7 +458,7 @@ func (check *Checker) updateExprType(x ast.Expr, typ Type, final bool) { ...@@ -458,7 +458,7 @@ func (check *Checker) updateExprType(x ast.Expr, typ Type, final bool) {
} }
// updateExprVal updates the value of x to val. // updateExprVal updates the value of x to val.
func (check *Checker) updateExprVal(x ast.Expr, val exact.Value) { func (check *Checker) updateExprVal(x ast.Expr, val constant.Value) {
if info, ok := check.untyped[x]; ok { if info, ok := check.untyped[x]; ok {
info.val = val info.val = val
check.untyped[x] = info check.untyped[x] = info
...@@ -492,7 +492,7 @@ func (check *Checker) convertUntyped(x *operand, target Type) { ...@@ -492,7 +492,7 @@ func (check *Checker) convertUntyped(x *operand, target Type) {
// typed target // typed target
switch t := target.Underlying().(type) { switch t := target.Underlying().(type) {
case *Basic: case *Basic:
if x.mode == constant { if x.mode == constant_ {
check.representable(x, t) check.representable(x, t)
if x.mode == invalid { if x.mode == invalid {
return return
...@@ -599,8 +599,8 @@ func (check *Checker) comparison(x, y *operand, op token.Token) { ...@@ -599,8 +599,8 @@ func (check *Checker) comparison(x, y *operand, op token.Token) {
return return
} }
if x.mode == constant && y.mode == constant { if x.mode == constant_ && y.mode == constant_ {
x.val = exact.MakeBool(exact.Compare(x.val, op, y.val)) x.val = constant.MakeBool(constant.Compare(x.val, op, y.val))
// The operands are never materialized; no need to update // The operands are never materialized; no need to update
// their types. // their types.
} else { } else {
...@@ -647,8 +647,8 @@ func (check *Checker) shift(x, y *operand, op token.Token) { ...@@ -647,8 +647,8 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
return return
} }
if x.mode == constant { if x.mode == constant_ {
if y.mode == constant { if y.mode == constant_ {
// rhs must be an integer value // rhs must be an integer value
if !y.isInteger() { if !y.isInteger() {
check.invalidOp(y.pos(), "shift count %s must be unsigned integer", y) check.invalidOp(y.pos(), "shift count %s must be unsigned integer", y)
...@@ -657,7 +657,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) { ...@@ -657,7 +657,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
} }
// rhs must be within reasonable bounds // rhs must be within reasonable bounds
const stupidShift = 1023 - 1 + 52 // so we can express smallestFloat64 const stupidShift = 1023 - 1 + 52 // so we can express smallestFloat64
s, ok := exact.Uint64Val(y.val) s, ok := constant.Uint64Val(y.val)
if !ok || s > stupidShift { if !ok || s > stupidShift {
check.invalidOp(y.pos(), "stupid shift count %s", y) check.invalidOp(y.pos(), "stupid shift count %s", y)
x.mode = invalid x.mode = invalid
...@@ -670,7 +670,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) { ...@@ -670,7 +670,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
if !isInteger(x.typ) { if !isInteger(x.typ) {
x.typ = Typ[UntypedInt] x.typ = Typ[UntypedInt]
} }
x.val = exact.Shift(x.val, op, uint(s)) x.val = constant.Shift(x.val, op, uint(s))
return return
} }
...@@ -695,7 +695,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) { ...@@ -695,7 +695,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
} }
// constant rhs must be >= 0 // constant rhs must be >= 0
if y.mode == constant && exact.Sign(y.val) < 0 { if y.mode == constant_ && constant.Sign(y.val) < 0 {
check.invalidOp(y.pos(), "shift count %s must not be negative", y) check.invalidOp(y.pos(), "shift count %s must not be negative", y)
} }
...@@ -776,19 +776,19 @@ func (check *Checker) binary(x *operand, e *ast.BinaryExpr, lhs, rhs ast.Expr, o ...@@ -776,19 +776,19 @@ func (check *Checker) binary(x *operand, e *ast.BinaryExpr, lhs, rhs ast.Expr, o
return return
} }
if (op == token.QUO || op == token.REM) && (x.mode == constant || isInteger(x.typ)) && y.mode == constant && exact.Sign(y.val) == 0 { if (op == token.QUO || op == token.REM) && (x.mode == constant_ || isInteger(x.typ)) && y.mode == constant_ && constant.Sign(y.val) == 0 {
check.invalidOp(y.pos(), "division by zero") check.invalidOp(y.pos(), "division by zero")
x.mode = invalid x.mode = invalid
return return
} }
if x.mode == constant && y.mode == constant { if x.mode == constant_ && y.mode == constant_ {
typ := x.typ.Underlying().(*Basic) typ := x.typ.Underlying().(*Basic)
// force integer division of integer operands // force integer division of integer operands
if op == token.QUO && isInteger(typ) { if op == token.QUO && isInteger(typ) {
op = token.QUO_ASSIGN op = token.QUO_ASSIGN
} }
x.val = exact.BinaryOp(x.val, op, y.val) x.val = constant.BinaryOp(x.val, op, y.val)
// Typed constants must be representable in // Typed constants must be representable in
// their type after each constant operation. // their type after each constant operation.
if isTyped(typ) { if isTyped(typ) {
...@@ -827,12 +827,12 @@ func (check *Checker) index(index ast.Expr, max int64) (i int64, valid bool) { ...@@ -827,12 +827,12 @@ func (check *Checker) index(index ast.Expr, max int64) (i int64, valid bool) {
} }
// a constant index i must be in bounds // a constant index i must be in bounds
if x.mode == constant { if x.mode == constant_ {
if exact.Sign(x.val) < 0 { if constant.Sign(x.val) < 0 {
check.invalidArg(x.pos(), "index %s must not be negative", &x) check.invalidArg(x.pos(), "index %s must not be negative", &x)
return return
} }
i, valid = exact.Int64Val(x.val) i, valid = constant.Int64Val(x.val)
if !valid || max >= 0 && i >= max { if !valid || max >= 0 && i >= max {
check.errorf(x.pos(), "index %s is out of bounds", &x) check.errorf(x.pos(), "index %s is out of bounds", &x)
return i, false return i, false
...@@ -923,13 +923,13 @@ func (check *Checker) rawExpr(x *operand, e ast.Expr, hint Type) exprKind { ...@@ -923,13 +923,13 @@ func (check *Checker) rawExpr(x *operand, e ast.Expr, hint Type) exprKind {
// convert x into a user-friendly set of values // convert x into a user-friendly set of values
// TODO(gri) this code can be simplified // TODO(gri) this code can be simplified
var typ Type var typ Type
var val exact.Value var val constant.Value
switch x.mode { switch x.mode {
case invalid: case invalid:
typ = Typ[Invalid] typ = Typ[Invalid]
case novalue: case novalue:
typ = (*Tuple)(nil) typ = (*Tuple)(nil)
case constant: case constant_:
typ = x.typ typ = x.typ
val = x.val val = x.val
default: default:
...@@ -1115,7 +1115,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind { ...@@ -1115,7 +1115,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
} }
continue continue
} }
if x.mode == constant { if x.mode == constant_ {
duplicate := false duplicate := false
// if the key is of interface type, the type is also significant when checking for duplicates // if the key is of interface type, the type is also significant when checking for duplicates
if _, ok := utyp.key.Underlying().(*Interface); ok { if _, ok := utyp.key.Underlying().(*Interface); ok {
...@@ -1175,8 +1175,8 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind { ...@@ -1175,8 +1175,8 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
case *Basic: case *Basic:
if isString(typ) { if isString(typ) {
valid = true valid = true
if x.mode == constant { if x.mode == constant_ {
length = int64(len(exact.StringVal(x.val))) length = int64(len(constant.StringVal(x.val)))
} }
// an indexed string always yields a byte value // an indexed string always yields a byte value
// (not a constant) even if the string and the // (not a constant) even if the string and the
...@@ -1250,8 +1250,8 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind { ...@@ -1250,8 +1250,8 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
goto Error goto Error
} }
valid = true valid = true
if x.mode == constant { if x.mode == constant_ {
length = int64(len(exact.StringVal(x.val))) length = int64(len(constant.StringVal(x.val)))
} }
// spec: "For untyped string operands the result // spec: "For untyped string operands the result
// is a non-constant value of type string." // is a non-constant value of type string."
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
) )
...@@ -143,15 +143,15 @@ func (obj *PkgName) Imported() *Package { return obj.imported } ...@@ -143,15 +143,15 @@ func (obj *PkgName) Imported() *Package { return obj.imported }
// A Const represents a declared constant. // A Const represents a declared constant.
type Const struct { type Const struct {
object object
val exact.Value val constant.Value
visited bool // for initialization cycle detection visited bool // for initialization cycle detection
} }
func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val exact.Value) *Const { func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const {
return &Const{object{nil, pos, pkg, name, typ, 0, token.NoPos}, val, false} return &Const{object{nil, pos, pkg, name, typ, 0, token.NoPos}, val, false}
} }
func (obj *Const) Val() exact.Value { return obj.val } func (obj *Const) Val() constant.Value { return obj.val }
// A TypeName represents a declared type. // A TypeName represents a declared type.
type TypeName struct { type TypeName struct {
......
...@@ -9,7 +9,7 @@ package types ...@@ -9,7 +9,7 @@ package types
import ( import (
"bytes" "bytes"
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
) )
...@@ -21,7 +21,7 @@ const ( ...@@ -21,7 +21,7 @@ const (
novalue // operand represents no value (result of a function call w/o result) novalue // operand represents no value (result of a function call w/o result)
builtin // operand is a built-in function builtin // operand is a built-in function
typexpr // operand is a type typexpr // operand is a type
constant // operand is a constant; the operand's typ is a Basic type constant_ // operand is a constant; the operand's typ is a Basic type
variable // operand is an addressable variable variable // operand is an addressable variable
mapindex // operand is a map index expression (acts like a variable on lhs, commaok on rhs of an assignment) mapindex // operand is a map index expression (acts like a variable on lhs, commaok on rhs of an assignment)
value // operand is a computed value value // operand is a computed value
...@@ -33,7 +33,7 @@ var operandModeString = [...]string{ ...@@ -33,7 +33,7 @@ var operandModeString = [...]string{
novalue: "no value", novalue: "no value",
builtin: "built-in", builtin: "built-in",
typexpr: "type", typexpr: "type",
constant: "constant", constant_: "constant",
variable: "variable", variable: "variable",
mapindex: "map index expression", mapindex: "map index expression",
value: "value", value: "value",
...@@ -50,7 +50,7 @@ type operand struct { ...@@ -50,7 +50,7 @@ type operand struct {
mode operandMode mode operandMode
expr ast.Expr expr ast.Expr
typ Type typ Type
val exact.Value val constant.Value
id builtinId id builtinId
} }
...@@ -105,7 +105,7 @@ func operandString(x *operand, qf Qualifier) string { ...@@ -105,7 +105,7 @@ func operandString(x *operand, qf Qualifier) string {
expr = predeclaredFuncs[x.id].name expr = predeclaredFuncs[x.id].name
case typexpr: case typexpr:
expr = TypeString(x.typ, qf) expr = TypeString(x.typ, qf)
case constant: case constant_:
expr = x.val.String() expr = x.val.String()
} }
} }
...@@ -135,7 +135,7 @@ func operandString(x *operand, qf Qualifier) string { ...@@ -135,7 +135,7 @@ func operandString(x *operand, qf Qualifier) string {
buf.WriteString(operandModeString[x.mode]) buf.WriteString(operandModeString[x.mode])
// <val> // <val>
if x.mode == constant { if x.mode == constant_ {
if s := x.val.String(); s != expr { if s := x.val.String(); s != expr {
buf.WriteByte(' ') buf.WriteByte(' ')
buf.WriteString(s) buf.WriteString(s)
...@@ -166,7 +166,7 @@ func (x *operand) String() string { ...@@ -166,7 +166,7 @@ func (x *operand) String() string {
// setConst sets x to the untyped constant for literal lit. // setConst sets x to the untyped constant for literal lit.
func (x *operand) setConst(tok token.Token, lit string) { func (x *operand) setConst(tok token.Token, lit string) {
val := exact.MakeFromLiteral(lit, tok, 0) val := constant.MakeFromLiteral(lit, tok, 0)
if val == nil { if val == nil {
// TODO(gri) Should we make it an unknown constant instead? // TODO(gri) Should we make it an unknown constant instead?
x.mode = invalid x.mode = invalid
...@@ -187,7 +187,7 @@ func (x *operand) setConst(tok token.Token, lit string) { ...@@ -187,7 +187,7 @@ func (x *operand) setConst(tok token.Token, lit string) {
kind = UntypedString kind = UntypedString
} }
x.mode = constant x.mode = constant_
x.typ = Typ[kind] x.typ = Typ[kind]
x.val = val x.val = val
} }
...@@ -260,7 +260,7 @@ func (x *operand) assignableTo(conf *Config, T Type) bool { ...@@ -260,7 +260,7 @@ func (x *operand) assignableTo(conf *Config, T Type) bool {
if isUntyped(Vu) { if isUntyped(Vu) {
switch t := Tu.(type) { switch t := Tu.(type) {
case *Basic: case *Basic:
if x.mode == constant { if x.mode == constant_ {
return representableConst(x.val, conf, t.kind, nil) return representableConst(x.val, conf, t.kind, nil)
} }
// The result of a comparison is an untyped boolean, // The result of a comparison is an untyped boolean,
...@@ -283,5 +283,5 @@ func (x *operand) assignableTo(conf *Config, T Type) bool { ...@@ -283,5 +283,5 @@ func (x *operand) assignableTo(conf *Config, T Type) bool {
func (x *operand) isInteger() bool { func (x *operand) isInteger() bool {
return x.mode == invalid || return x.mode == invalid ||
isInteger(x.typ) || isInteger(x.typ) ||
isUntyped(x.typ) && x.mode == constant && representableConst(x.val, nil, UntypedInt, nil) // no *Config required for UntypedInt isUntyped(x.typ) && x.mode == constant_ && representableConst(x.val, nil, UntypedInt, nil) // no *Config required for UntypedInt
} }
...@@ -7,7 +7,7 @@ package types ...@@ -7,7 +7,7 @@ package types
import ( import (
"fmt" "fmt"
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
pathLib "path" pathLib "path"
"strconv" "strconv"
...@@ -256,7 +256,7 @@ func (check *Checker) collectObjects() { ...@@ -256,7 +256,7 @@ func (check *Checker) collectObjects() {
// declare all constants // declare all constants
for i, name := range s.Names { for i, name := range s.Names {
obj := NewConst(name.Pos(), pkg, name.Name, nil, exact.MakeInt64(int64(iota))) obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(iota)))
var init ast.Expr var init ast.Expr
if i < len(last.Values) { if i < len(last.Values) {
......
...@@ -47,7 +47,7 @@ func TestBenchmark(t *testing.T) { ...@@ -47,7 +47,7 @@ func TestBenchmark(t *testing.T) {
// We're not using testing's benchmarking mechanism directly // We're not using testing's benchmarking mechanism directly
// because we want custom output. // because we want custom output.
for _, p := range []string{"types", "exact", "gcimporter"} { for _, p := range []string{"types", "constant", filepath.Join("internal", "gcimporter")} {
path := filepath.Join("..", p) path := filepath.Join("..", p)
runbench(t, path, false) runbench(t, path, false)
runbench(t, path, true) runbench(t, path, true)
......
...@@ -9,7 +9,7 @@ package types ...@@ -9,7 +9,7 @@ package types
import ( import (
"fmt" "fmt"
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
) )
...@@ -402,9 +402,9 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) { ...@@ -402,9 +402,9 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
} else { } else {
// spec: "A missing switch expression is // spec: "A missing switch expression is
// equivalent to the boolean value true." // equivalent to the boolean value true."
x.mode = constant x.mode = constant_
x.typ = Typ[Bool] x.typ = Typ[Bool]
x.val = exact.MakeBool(true) x.val = constant.MakeBool(true)
x.expr = &ast.Ident{NamePos: s.Body.Lbrace, Name: "true"} x.expr = &ast.Ident{NamePos: s.Body.Lbrace, Name: "true"}
} }
......
...@@ -8,7 +8,7 @@ package types ...@@ -8,7 +8,7 @@ package types
import ( import (
"go/ast" "go/ast"
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
"sort" "sort"
"strconv" "strconv"
...@@ -65,7 +65,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, path []*TypeNa ...@@ -65,7 +65,7 @@ func (check *Checker) ident(x *operand, e *ast.Ident, def *Named, path []*TypeNa
x.val = obj.val x.val = obj.val
} }
assert(x.val != nil) assert(x.val != nil)
x.mode = constant x.mode = constant_
case *TypeName: case *TypeName:
x.mode = typexpr x.mode = typexpr
...@@ -367,7 +367,7 @@ func (check *Checker) typOrNil(e ast.Expr) Type { ...@@ -367,7 +367,7 @@ func (check *Checker) typOrNil(e ast.Expr) Type {
func (check *Checker) arrayLength(e ast.Expr) int64 { func (check *Checker) arrayLength(e ast.Expr) int64 {
var x operand var x operand
check.expr(&x, e) check.expr(&x, e)
if x.mode != constant { if x.mode != constant_ {
if x.mode != invalid { if x.mode != invalid {
check.errorf(x.pos(), "array length %s must be constant", &x) check.errorf(x.pos(), "array length %s must be constant", &x)
} }
...@@ -377,7 +377,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 { ...@@ -377,7 +377,7 @@ func (check *Checker) arrayLength(e ast.Expr) int64 {
check.errorf(x.pos(), "array length %s must be integer", &x) check.errorf(x.pos(), "array length %s must be integer", &x)
return 0 return 0
} }
n, ok := exact.Int64Val(x.val) n, ok := constant.Int64Val(x.val)
if !ok || n < 0 { if !ok || n < 0 {
check.errorf(x.pos(), "invalid array length %s", &x) check.errorf(x.pos(), "invalid array length %s", &x)
return 0 return 0
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
package types package types
import ( import (
exact "go/constant" // Renamed to reduce diffs from x/tools. TODO: remove "go/constant"
"go/token" "go/token"
"strings" "strings"
) )
...@@ -76,11 +76,11 @@ func defPredeclaredTypes() { ...@@ -76,11 +76,11 @@ func defPredeclaredTypes() {
var predeclaredConsts = [...]struct { var predeclaredConsts = [...]struct {
name string name string
kind BasicKind kind BasicKind
val exact.Value val constant.Value
}{ }{
{"true", UntypedBool, exact.MakeBool(true)}, {"true", UntypedBool, constant.MakeBool(true)},
{"false", UntypedBool, exact.MakeBool(false)}, {"false", UntypedBool, constant.MakeBool(false)},
{"iota", UntypedInt, exact.MakeInt64(0)}, {"iota", UntypedInt, constant.MakeInt64(0)},
} }
func defPredeclaredConsts() { func defPredeclaredConsts() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment