...

Source file src/go/constant/value.go

Documentation: go/constant

     1  // Copyright 2013 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 constant implements Values representing untyped
     6  // Go constants and their corresponding operations.
     7  //
     8  // A special Unknown value may be used when a value
     9  // is unknown due to an error. Operations on unknown
    10  // values produce unknown values unless specified
    11  // otherwise.
    12  package constant
    13  
    14  import (
    15  	"fmt"
    16  	"go/token"
    17  	"math"
    18  	"math/big"
    19  	"math/bits"
    20  	"strconv"
    21  	"strings"
    22  	"sync"
    23  	"unicode/utf8"
    24  )
    25  
    26  //go:generate stringer -type Kind
    27  
    28  // Kind specifies the kind of value represented by a Value.
    29  type Kind int
    30  
    31  const (
    32  	// unknown values
    33  	Unknown Kind = iota
    34  
    35  	// non-numeric values
    36  	Bool
    37  	String
    38  
    39  	// numeric values
    40  	Int
    41  	Float
    42  	Complex
    43  )
    44  
    45  // A Value represents the value of a Go constant.
    46  type Value interface {
    47  	// Kind returns the value kind.
    48  	Kind() Kind
    49  
    50  	// String returns a short, quoted (human-readable) form of the value.
    51  	// For numeric values, the result may be an approximation;
    52  	// for String values the result may be a shortened string.
    53  	// Use ExactString for a string representing a value exactly.
    54  	String() string
    55  
    56  	// ExactString returns an exact, quoted (human-readable) form of the value.
    57  	// If the Value is of Kind String, use StringVal to obtain the unquoted string.
    58  	ExactString() string
    59  
    60  	// Prevent external implementations.
    61  	implementsValue()
    62  }
    63  
    64  // ----------------------------------------------------------------------------
    65  // Implementations
    66  
    67  // Maximum supported mantissa precision.
    68  // The spec requires at least 256 bits; typical implementations use 512 bits.
    69  const prec = 512
    70  
    71  // TODO(gri) Consider storing "error" information in an unknownVal so clients
    72  // can provide better error messages. For instance, if a number is
    73  // too large (incl. infinity), that could be recorded in unknownVal.
    74  // See also #20583 and #42695 for use cases.
    75  
    76  // Representation of values:
    77  //
    78  // Values of Int and Float Kind have two different representations each: int64Val
    79  // and intVal, and ratVal and floatVal. When possible, the "smaller", respectively
    80  // more precise (for Floats) representation is chosen. However, once a Float value
    81  // is represented as a floatVal, any subsequent results remain floatVals (unless
    82  // explicitly converted); i.e., no attempt is made to convert a floatVal back into
    83  // a ratVal. The reasoning is that all representations but floatVal are mathematically
    84  // exact, but once that precision is lost (by moving to floatVal), moving back to
    85  // a different representation implies a precision that's not actually there.
    86  
    87  type (
    88  	unknownVal struct{}
    89  	boolVal    bool
    90  	stringVal  struct {
    91  		// Lazy value: either a string (l,r==nil) or an addition (l,r!=nil).
    92  		mu   sync.Mutex
    93  		s    string
    94  		l, r *stringVal
    95  	}
    96  	int64Val   int64                    // Int values representable as an int64
    97  	intVal     struct{ val *big.Int }   // Int values not representable as an int64
    98  	ratVal     struct{ val *big.Rat }   // Float values representable as a fraction
    99  	floatVal   struct{ val *big.Float } // Float values not representable as a fraction
   100  	complexVal struct{ re, im Value }
   101  )
   102  
   103  func (unknownVal) Kind() Kind { return Unknown }
   104  func (boolVal) Kind() Kind    { return Bool }
   105  func (*stringVal) Kind() Kind { return String }
   106  func (int64Val) Kind() Kind   { return Int }
   107  func (intVal) Kind() Kind     { return Int }
   108  func (ratVal) Kind() Kind     { return Float }
   109  func (floatVal) Kind() Kind   { return Float }
   110  func (complexVal) Kind() Kind { return Complex }
   111  
   112  func (unknownVal) String() string { return "unknown" }
   113  func (x boolVal) String() string  { return strconv.FormatBool(bool(x)) }
   114  
   115  // String returns a possibly shortened quoted form of the String value.
   116  func (x *stringVal) String() string {
   117  	const maxLen = 72 // a reasonable length
   118  	s := strconv.Quote(x.string())
   119  	if utf8.RuneCountInString(s) > maxLen {
   120  		// The string without the enclosing quotes is greater than maxLen-2 runes
   121  		// long. Remove the last 3 runes (including the closing '"') by keeping
   122  		// only the first maxLen-3 runes; then add "...".
   123  		i := 0
   124  		for n := 0; n < maxLen-3; n++ {
   125  			_, size := utf8.DecodeRuneInString(s[i:])
   126  			i += size
   127  		}
   128  		s = s[:i] + "..."
   129  	}
   130  	return s
   131  }
   132  
   133  // string constructs and returns the actual string literal value.
   134  // If x represents an addition, then it rewrites x to be a single
   135  // string, to speed future calls. This lazy construction avoids
   136  // building different string values for all subpieces of a large
   137  // concatenation. See golang.org/issue/23348.
   138  func (x *stringVal) string() string {
   139  	x.mu.Lock()
   140  	if x.l != nil {
   141  		x.s = strings.Join(reverse(x.appendReverse(nil)), "")
   142  		x.l = nil
   143  		x.r = nil
   144  	}
   145  	s := x.s
   146  	x.mu.Unlock()
   147  
   148  	return s
   149  }
   150  
   151  // reverse reverses x in place and returns it.
   152  func reverse(x []string) []string {
   153  	n := len(x)
   154  	for i := 0; i+i < n; i++ {
   155  		x[i], x[n-1-i] = x[n-1-i], x[i]
   156  	}
   157  	return x
   158  }
   159  
   160  // appendReverse appends to list all of x's subpieces, but in reverse,
   161  // and returns the result. Appending the reversal allows processing
   162  // the right side in a recursive call and the left side in a loop.
   163  // Because a chain like a + b + c + d + e is actually represented
   164  // as ((((a + b) + c) + d) + e), the left-side loop avoids deep recursion.
   165  // x must be locked.
   166  func (x *stringVal) appendReverse(list []string) []string {
   167  	y := x
   168  	for y.r != nil {
   169  		y.r.mu.Lock()
   170  		list = y.r.appendReverse(list)
   171  		y.r.mu.Unlock()
   172  
   173  		l := y.l
   174  		if y != x {
   175  			y.mu.Unlock()
   176  		}
   177  		l.mu.Lock()
   178  		y = l
   179  	}
   180  	s := y.s
   181  	if y != x {
   182  		y.mu.Unlock()
   183  	}
   184  	return append(list, s)
   185  }
   186  
   187  func (x int64Val) String() string { return strconv.FormatInt(int64(x), 10) }
   188  func (x intVal) String() string   { return x.val.String() }
   189  func (x ratVal) String() string   { return rtof(x).String() }
   190  
   191  // String returns a decimal approximation of the Float value.
   192  func (x floatVal) String() string {
   193  	f := x.val
   194  
   195  	// Don't try to convert infinities (will not terminate).
   196  	if f.IsInf() {
   197  		return f.String()
   198  	}
   199  
   200  	// Use exact fmt formatting if in float64 range (common case):
   201  	// proceed if f doesn't underflow to 0 or overflow to inf.
   202  	if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) {
   203  		return fmt.Sprintf("%.6g", x)
   204  	}
   205  
   206  	// Out of float64 range. Do approximate manual to decimal
   207  	// conversion to avoid precise but possibly slow Float
   208  	// formatting.
   209  	// f = mant * 2**exp
   210  	var mant big.Float
   211  	exp := f.MantExp(&mant) // 0.5 <= |mant| < 1.0
   212  
   213  	// approximate float64 mantissa m and decimal exponent d
   214  	// f ~ m * 10**d
   215  	m, _ := mant.Float64()                     // 0.5 <= |m| < 1.0
   216  	d := float64(exp) * (math.Ln2 / math.Ln10) // log_10(2)
   217  
   218  	// adjust m for truncated (integer) decimal exponent e
   219  	e := int64(d)
   220  	m *= math.Pow(10, d-float64(e))
   221  
   222  	// ensure 1 <= |m| < 10
   223  	switch am := math.Abs(m); {
   224  	case am < 1-0.5e-6:
   225  		// The %.6g format below rounds m to 5 digits after the
   226  		// decimal point. Make sure that m*10 < 10 even after
   227  		// rounding up: m*10 + 0.5e-5 < 10 => m < 1 - 0.5e6.
   228  		m *= 10
   229  		e--
   230  	case am >= 10:
   231  		m /= 10
   232  		e++
   233  	}
   234  
   235  	return fmt.Sprintf("%.6ge%+d", m, e)
   236  }
   237  
   238  func (x complexVal) String() string { return fmt.Sprintf("(%s + %si)", x.re, x.im) }
   239  
   240  func (x unknownVal) ExactString() string { return x.String() }
   241  func (x boolVal) ExactString() string    { return x.String() }
   242  func (x *stringVal) ExactString() string { return strconv.Quote(x.string()) }
   243  func (x int64Val) ExactString() string   { return x.String() }
   244  func (x intVal) ExactString() string     { return x.String() }
   245  
   246  func (x ratVal) ExactString() string {
   247  	r := x.val
   248  	if r.IsInt() {
   249  		return r.Num().String()
   250  	}
   251  	return r.String()
   252  }
   253  
   254  func (x floatVal) ExactString() string { return x.val.Text('p', 0) }
   255  
   256  func (x complexVal) ExactString() string {
   257  	return fmt.Sprintf("(%s + %si)", x.re.ExactString(), x.im.ExactString())
   258  }
   259  
   260  func (unknownVal) implementsValue() {}
   261  func (boolVal) implementsValue()    {}
   262  func (*stringVal) implementsValue() {}
   263  func (int64Val) implementsValue()   {}
   264  func (ratVal) implementsValue()     {}
   265  func (intVal) implementsValue()     {}
   266  func (floatVal) implementsValue()   {}
   267  func (complexVal) implementsValue() {}
   268  
   269  func newInt() *big.Int     { return new(big.Int) }
   270  func newRat() *big.Rat     { return new(big.Rat) }
   271  func newFloat() *big.Float { return new(big.Float).SetPrec(prec) }
   272  
   273  func i64toi(x int64Val) intVal   { return intVal{newInt().SetInt64(int64(x))} }
   274  func i64tor(x int64Val) ratVal   { return ratVal{newRat().SetInt64(int64(x))} }
   275  func i64tof(x int64Val) floatVal { return floatVal{newFloat().SetInt64(int64(x))} }
   276  func itor(x intVal) ratVal       { return ratVal{newRat().SetInt(x.val)} }
   277  func itof(x intVal) floatVal     { return floatVal{newFloat().SetInt(x.val)} }
   278  func rtof(x ratVal) floatVal     { return floatVal{newFloat().SetRat(x.val)} }
   279  func vtoc(x Value) complexVal    { return complexVal{x, int64Val(0)} }
   280  
   281  func makeInt(x *big.Int) Value {
   282  	if x.IsInt64() {
   283  		return int64Val(x.Int64())
   284  	}
   285  	return intVal{x}
   286  }
   287  
   288  func makeRat(x *big.Rat) Value {
   289  	a := x.Num()
   290  	b := x.Denom()
   291  	if smallInt(a) && smallInt(b) {
   292  		// ok to remain fraction
   293  		return ratVal{x}
   294  	}
   295  	// components too large => switch to float
   296  	return floatVal{newFloat().SetRat(x)}
   297  }
   298  
   299  var floatVal0 = floatVal{newFloat()}
   300  
   301  func makeFloat(x *big.Float) Value {
   302  	// convert -0
   303  	if x.Sign() == 0 {
   304  		return floatVal0
   305  	}
   306  	if x.IsInf() {
   307  		return unknownVal{}
   308  	}
   309  	// No attempt is made to "go back" to ratVal, even if possible,
   310  	// to avoid providing the illusion of a mathematically exact
   311  	// representation.
   312  	return floatVal{x}
   313  }
   314  
   315  func makeComplex(re, im Value) Value {
   316  	if re.Kind() == Unknown || im.Kind() == Unknown {
   317  		return unknownVal{}
   318  	}
   319  	return complexVal{re, im}
   320  }
   321  
   322  func makeFloatFromLiteral(lit string) Value {
   323  	if f, ok := newFloat().SetString(lit); ok {
   324  		if smallFloat(f) {
   325  			// ok to use rationals
   326  			if f.Sign() == 0 {
   327  				// Issue 20228: If the float underflowed to zero, parse just "0".
   328  				// Otherwise, lit might contain a value with a large negative exponent,
   329  				// such as -6e-1886451601. As a float, that will underflow to 0,
   330  				// but it'll take forever to parse as a Rat.
   331  				lit = "0"
   332  			}
   333  			if r, ok := newRat().SetString(lit); ok {
   334  				return ratVal{r}
   335  			}
   336  		}
   337  		// otherwise use floats
   338  		return makeFloat(f)
   339  	}
   340  	return nil
   341  }
   342  
   343  // Permit fractions with component sizes up to maxExp
   344  // before switching to using floating-point numbers.
   345  const maxExp = 4 << 10
   346  
   347  // smallInt reports whether x would lead to "reasonably"-sized fraction
   348  // if converted to a *big.Rat.
   349  func smallInt(x *big.Int) bool {
   350  	return x.BitLen() < maxExp
   351  }
   352  
   353  // smallFloat64 reports whether x would lead to "reasonably"-sized fraction
   354  // if converted to a *big.Rat.
   355  func smallFloat64(x float64) bool {
   356  	if math.IsInf(x, 0) {
   357  		return false
   358  	}
   359  	_, e := math.Frexp(x)
   360  	return -maxExp < e && e < maxExp
   361  }
   362  
   363  // smallFloat reports whether x would lead to "reasonably"-sized fraction
   364  // if converted to a *big.Rat.
   365  func smallFloat(x *big.Float) bool {
   366  	if x.IsInf() {
   367  		return false
   368  	}
   369  	e := x.MantExp(nil)
   370  	return -maxExp < e && e < maxExp
   371  }
   372  
   373  // ----------------------------------------------------------------------------
   374  // Factories
   375  
   376  // MakeUnknown returns the Unknown value.
   377  func MakeUnknown() Value { return unknownVal{} }
   378  
   379  // MakeBool returns the Bool value for b.
   380  func MakeBool(b bool) Value { return boolVal(b) }
   381  
   382  // MakeString returns the String value for s.
   383  func MakeString(s string) Value { return &stringVal{s: s} }
   384  
   385  // MakeInt64 returns the Int value for x.
   386  func MakeInt64(x int64) Value { return int64Val(x) }
   387  
   388  // MakeUint64 returns the Int value for x.
   389  func MakeUint64(x uint64) Value {
   390  	if x < 1<<63 {
   391  		return int64Val(int64(x))
   392  	}
   393  	return intVal{newInt().SetUint64(x)}
   394  }
   395  
   396  // MakeFloat64 returns the Float value for x.
   397  // If x is -0.0, the result is 0.0.
   398  // If x is not finite, the result is an Unknown.
   399  func MakeFloat64(x float64) Value {
   400  	if math.IsInf(x, 0) || math.IsNaN(x) {
   401  		return unknownVal{}
   402  	}
   403  	if smallFloat64(x) {
   404  		return ratVal{newRat().SetFloat64(x + 0)} // convert -0 to 0
   405  	}
   406  	return floatVal{newFloat().SetFloat64(x + 0)}
   407  }
   408  
   409  // MakeFromLiteral returns the corresponding integer, floating-point,
   410  // imaginary, character, or string value for a Go literal string. The
   411  // tok value must be one of token.INT, token.FLOAT, token.IMAG,
   412  // token.CHAR, or token.STRING. The final argument must be zero.
   413  // If the literal string syntax is invalid, the result is an Unknown.
   414  func MakeFromLiteral(lit string, tok token.Token, zero uint) Value {
   415  	if zero != 0 {
   416  		panic("MakeFromLiteral called with non-zero last argument")
   417  	}
   418  
   419  	switch tok {
   420  	case token.INT:
   421  		if x, err := strconv.ParseInt(lit, 0, 64); err == nil {
   422  			return int64Val(x)
   423  		}
   424  		if x, ok := newInt().SetString(lit, 0); ok {
   425  			return intVal{x}
   426  		}
   427  
   428  	case token.FLOAT:
   429  		if x := makeFloatFromLiteral(lit); x != nil {
   430  			return x
   431  		}
   432  
   433  	case token.IMAG:
   434  		if n := len(lit); n > 0 && lit[n-1] == 'i' {
   435  			if im := makeFloatFromLiteral(lit[:n-1]); im != nil {
   436  				return makeComplex(int64Val(0), im)
   437  			}
   438  		}
   439  
   440  	case token.CHAR:
   441  		if n := len(lit); n >= 2 {
   442  			if code, _, _, err := strconv.UnquoteChar(lit[1:n-1], '\''); err == nil {
   443  				return MakeInt64(int64(code))
   444  			}
   445  		}
   446  
   447  	case token.STRING:
   448  		if s, err := strconv.Unquote(lit); err == nil {
   449  			return MakeString(s)
   450  		}
   451  
   452  	default:
   453  		panic(fmt.Sprintf("%v is not a valid token", tok))
   454  	}
   455  
   456  	return unknownVal{}
   457  }
   458  
   459  // ----------------------------------------------------------------------------
   460  // Accessors
   461  //
   462  // For unknown arguments the result is the zero value for the respective
   463  // accessor type, except for Sign, where the result is 1.
   464  
   465  // BoolVal returns the Go boolean value of x, which must be a Bool or an Unknown.
   466  // If x is Unknown, the result is false.
   467  func BoolVal(x Value) bool {
   468  	switch x := x.(type) {
   469  	case boolVal:
   470  		return bool(x)
   471  	case unknownVal:
   472  		return false
   473  	default:
   474  		panic(fmt.Sprintf("%v not a Bool", x))
   475  	}
   476  }
   477  
   478  // StringVal returns the Go string value of x, which must be a String or an Unknown.
   479  // If x is Unknown, the result is "".
   480  func StringVal(x Value) string {
   481  	switch x := x.(type) {
   482  	case *stringVal:
   483  		return x.string()
   484  	case unknownVal:
   485  		return ""
   486  	default:
   487  		panic(fmt.Sprintf("%v not a String", x))
   488  	}
   489  }
   490  
   491  // Int64Val returns the Go int64 value of x and whether the result is exact;
   492  // x must be an Int or an Unknown. If the result is not exact, its value is undefined.
   493  // If x is Unknown, the result is (0, false).
   494  func Int64Val(x Value) (int64, bool) {
   495  	switch x := x.(type) {
   496  	case int64Val:
   497  		return int64(x), true
   498  	case intVal:
   499  		return x.val.Int64(), false // not an int64Val and thus not exact
   500  	case unknownVal:
   501  		return 0, false
   502  	default:
   503  		panic(fmt.Sprintf("%v not an Int", x))
   504  	}
   505  }
   506  
   507  // Uint64Val returns the Go uint64 value of x and whether the result is exact;
   508  // x must be an Int or an Unknown. If the result is not exact, its value is undefined.
   509  // If x is Unknown, the result is (0, false).
   510  func Uint64Val(x Value) (uint64, bool) {
   511  	switch x := x.(type) {
   512  	case int64Val:
   513  		return uint64(x), x >= 0
   514  	case intVal:
   515  		return x.val.Uint64(), x.val.IsUint64()
   516  	case unknownVal:
   517  		return 0, false
   518  	default:
   519  		panic(fmt.Sprintf("%v not an Int", x))
   520  	}
   521  }
   522  
   523  // Float32Val is like Float64Val but for float32 instead of float64.
   524  func Float32Val(x Value) (float32, bool) {
   525  	switch x := x.(type) {
   526  	case int64Val:
   527  		f := float32(x)
   528  		return f, int64Val(f) == x
   529  	case intVal:
   530  		f, acc := newFloat().SetInt(x.val).Float32()
   531  		return f, acc == big.Exact
   532  	case ratVal:
   533  		return x.val.Float32()
   534  	case floatVal:
   535  		f, acc := x.val.Float32()
   536  		return f, acc == big.Exact
   537  	case unknownVal:
   538  		return 0, false
   539  	default:
   540  		panic(fmt.Sprintf("%v not a Float", x))
   541  	}
   542  }
   543  
   544  // Float64Val returns the nearest Go float64 value of x and whether the result is exact;
   545  // x must be numeric or an Unknown, but not Complex. For values too small (too close to 0)
   546  // to represent as float64, Float64Val silently underflows to 0. The result sign always
   547  // matches the sign of x, even for 0.
   548  // If x is Unknown, the result is (0, false).
   549  func Float64Val(x Value) (float64, bool) {
   550  	switch x := x.(type) {
   551  	case int64Val:
   552  		f := float64(int64(x))
   553  		return f, int64Val(f) == x
   554  	case intVal:
   555  		f, acc := newFloat().SetInt(x.val).Float64()
   556  		return f, acc == big.Exact
   557  	case ratVal:
   558  		return x.val.Float64()
   559  	case floatVal:
   560  		f, acc := x.val.Float64()
   561  		return f, acc == big.Exact
   562  	case unknownVal:
   563  		return 0, false
   564  	default:
   565  		panic(fmt.Sprintf("%v not a Float", x))
   566  	}
   567  }
   568  
   569  // Val returns the underlying value for a given constant. Since it returns an
   570  // interface, it is up to the caller to type assert the result to the expected
   571  // type. The possible dynamic return types are:
   572  //
   573  //	x Kind             type of result
   574  //	-----------------------------------------
   575  //	Bool               bool
   576  //	String             string
   577  //	Int                int64 or *big.Int
   578  //	Float              *big.Float or *big.Rat
   579  //	everything else    nil
   580  func Val(x Value) any {
   581  	switch x := x.(type) {
   582  	case boolVal:
   583  		return bool(x)
   584  	case *stringVal:
   585  		return x.string()
   586  	case int64Val:
   587  		return int64(x)
   588  	case intVal:
   589  		return x.val
   590  	case ratVal:
   591  		return x.val
   592  	case floatVal:
   593  		return x.val
   594  	default:
   595  		return nil
   596  	}
   597  }
   598  
   599  // Make returns the Value for x.
   600  //
   601  //	type of x        result Kind
   602  //	----------------------------
   603  //	bool             Bool
   604  //	string           String
   605  //	int64            Int
   606  //	*big.Int         Int
   607  //	*big.Float       Float
   608  //	*big.Rat         Float
   609  //	anything else    Unknown
   610  func Make(x any) Value {
   611  	switch x := x.(type) {
   612  	case bool:
   613  		return boolVal(x)
   614  	case string:
   615  		return &stringVal{s: x}
   616  	case int64:
   617  		return int64Val(x)
   618  	case *big.Int:
   619  		return makeInt(x)
   620  	case *big.Rat:
   621  		return makeRat(x)
   622  	case *big.Float:
   623  		return makeFloat(x)
   624  	default:
   625  		return unknownVal{}
   626  	}
   627  }
   628  
   629  // BitLen returns the number of bits required to represent
   630  // the absolute value x in binary representation; x must be an Int or an Unknown.
   631  // If x is Unknown, the result is 0.
   632  func BitLen(x Value) int {
   633  	switch x := x.(type) {
   634  	case int64Val:
   635  		u := uint64(x)
   636  		if x < 0 {
   637  			u = uint64(-x)
   638  		}
   639  		return 64 - bits.LeadingZeros64(u)
   640  	case intVal:
   641  		return x.val.BitLen()
   642  	case unknownVal:
   643  		return 0
   644  	default:
   645  		panic(fmt.Sprintf("%v not an Int", x))
   646  	}
   647  }
   648  
   649  // Sign returns -1, 0, or 1 depending on whether x < 0, x == 0, or x > 0;
   650  // x must be numeric or Unknown. For complex values x, the sign is 0 if x == 0,
   651  // otherwise it is != 0. If x is Unknown, the result is 1.
   652  func Sign(x Value) int {
   653  	switch x := x.(type) {
   654  	case int64Val:
   655  		switch {
   656  		case x < 0:
   657  			return -1
   658  		case x > 0:
   659  			return 1
   660  		}
   661  		return 0
   662  	case intVal:
   663  		return x.val.Sign()
   664  	case ratVal:
   665  		return x.val.Sign()
   666  	case floatVal:
   667  		return x.val.Sign()
   668  	case complexVal:
   669  		return Sign(x.re) | Sign(x.im)
   670  	case unknownVal:
   671  		return 1 // avoid spurious division by zero errors
   672  	default:
   673  		panic(fmt.Sprintf("%v not numeric", x))
   674  	}
   675  }
   676  
   677  // ----------------------------------------------------------------------------
   678  // Support for assembling/disassembling numeric values
   679  
   680  const (
   681  	// Compute the size of a Word in bytes.
   682  	_m       = ^big.Word(0)
   683  	_log     = _m>>8&1 + _m>>16&1 + _m>>32&1
   684  	wordSize = 1 << _log
   685  )
   686  
   687  // Bytes returns the bytes for the absolute value of x in little-
   688  // endian binary representation; x must be an Int.
   689  func Bytes(x Value) []byte {
   690  	var t intVal
   691  	switch x := x.(type) {
   692  	case int64Val:
   693  		t = i64toi(x)
   694  	case intVal:
   695  		t = x
   696  	default:
   697  		panic(fmt.Sprintf("%v not an Int", x))
   698  	}
   699  
   700  	words := t.val.Bits()
   701  	bytes := make([]byte, len(words)*wordSize)
   702  
   703  	i := 0
   704  	for _, w := range words {
   705  		for j := 0; j < wordSize; j++ {
   706  			bytes[i] = byte(w)
   707  			w >>= 8
   708  			i++
   709  		}
   710  	}
   711  	// remove leading 0's
   712  	for i > 0 && bytes[i-1] == 0 {
   713  		i--
   714  	}
   715  
   716  	return bytes[:i]
   717  }
   718  
   719  // MakeFromBytes returns the Int value given the bytes of its little-endian
   720  // binary representation. An empty byte slice argument represents 0.
   721  func MakeFromBytes(bytes []byte) Value {
   722  	words := make([]big.Word, (len(bytes)+(wordSize-1))/wordSize)
   723  
   724  	i := 0
   725  	var w big.Word
   726  	var s uint
   727  	for _, b := range bytes {
   728  		w |= big.Word(b) << s
   729  		if s += 8; s == wordSize*8 {
   730  			words[i] = w
   731  			i++
   732  			w = 0
   733  			s = 0
   734  		}
   735  	}
   736  	// store last word
   737  	if i < len(words) {
   738  		words[i] = w
   739  		i++
   740  	}
   741  	// remove leading 0's
   742  	for i > 0 && words[i-1] == 0 {
   743  		i--
   744  	}
   745  
   746  	return makeInt(newInt().SetBits(words[:i]))
   747  }
   748  
   749  // Num returns the numerator of x; x must be Int, Float, or Unknown.
   750  // If x is Unknown, or if it is too large or small to represent as a
   751  // fraction, the result is Unknown. Otherwise the result is an Int
   752  // with the same sign as x.
   753  func Num(x Value) Value {
   754  	switch x := x.(type) {
   755  	case int64Val, intVal:
   756  		return x
   757  	case ratVal:
   758  		return makeInt(x.val.Num())
   759  	case floatVal:
   760  		if smallFloat(x.val) {
   761  			r, _ := x.val.Rat(nil)
   762  			return makeInt(r.Num())
   763  		}
   764  	case unknownVal:
   765  		break
   766  	default:
   767  		panic(fmt.Sprintf("%v not Int or Float", x))
   768  	}
   769  	return unknownVal{}
   770  }
   771  
   772  // Denom returns the denominator of x; x must be Int, Float, or Unknown.
   773  // If x is Unknown, or if it is too large or small to represent as a
   774  // fraction, the result is Unknown. Otherwise the result is an Int >= 1.
   775  func Denom(x Value) Value {
   776  	switch x := x.(type) {
   777  	case int64Val, intVal:
   778  		return int64Val(1)
   779  	case ratVal:
   780  		return makeInt(x.val.Denom())
   781  	case floatVal:
   782  		if smallFloat(x.val) {
   783  			r, _ := x.val.Rat(nil)
   784  			return makeInt(r.Denom())
   785  		}
   786  	case unknownVal:
   787  		break
   788  	default:
   789  		panic(fmt.Sprintf("%v not Int or Float", x))
   790  	}
   791  	return unknownVal{}
   792  }
   793  
   794  // MakeImag returns the Complex value x*i;
   795  // x must be Int, Float, or Unknown.
   796  // If x is Unknown, the result is Unknown.
   797  func MakeImag(x Value) Value {
   798  	switch x.(type) {
   799  	case unknownVal:
   800  		return x
   801  	case int64Val, intVal, ratVal, floatVal:
   802  		return makeComplex(int64Val(0), x)
   803  	default:
   804  		panic(fmt.Sprintf("%v not Int or Float", x))
   805  	}
   806  }
   807  
   808  // Real returns the real part of x, which must be a numeric or unknown value.
   809  // If x is Unknown, the result is Unknown.
   810  func Real(x Value) Value {
   811  	switch x := x.(type) {
   812  	case unknownVal, int64Val, intVal, ratVal, floatVal:
   813  		return x
   814  	case complexVal:
   815  		return x.re
   816  	default:
   817  		panic(fmt.Sprintf("%v not numeric", x))
   818  	}
   819  }
   820  
   821  // Imag returns the imaginary part of x, which must be a numeric or unknown value.
   822  // If x is Unknown, the result is Unknown.
   823  func Imag(x Value) Value {
   824  	switch x := x.(type) {
   825  	case unknownVal:
   826  		return x
   827  	case int64Val, intVal, ratVal, floatVal:
   828  		return int64Val(0)
   829  	case complexVal:
   830  		return x.im
   831  	default:
   832  		panic(fmt.Sprintf("%v not numeric", x))
   833  	}
   834  }
   835  
   836  // ----------------------------------------------------------------------------
   837  // Numeric conversions
   838  
   839  // ToInt converts x to an Int value if x is representable as an Int.
   840  // Otherwise it returns an Unknown.
   841  func ToInt(x Value) Value {
   842  	switch x := x.(type) {
   843  	case int64Val, intVal:
   844  		return x
   845  
   846  	case ratVal:
   847  		if x.val.IsInt() {
   848  			return makeInt(x.val.Num())
   849  		}
   850  
   851  	case floatVal:
   852  		// avoid creation of huge integers
   853  		// (Existing tests require permitting exponents of at least 1024;
   854  		// allow any value that would also be permissible as a fraction.)
   855  		if smallFloat(x.val) {
   856  			i := newInt()
   857  			if _, acc := x.val.Int(i); acc == big.Exact {
   858  				return makeInt(i)
   859  			}
   860  
   861  			// If we can get an integer by rounding up or down,
   862  			// assume x is not an integer because of rounding
   863  			// errors in prior computations.
   864  
   865  			const delta = 4 // a small number of bits > 0
   866  			var t big.Float
   867  			t.SetPrec(prec - delta)
   868  
   869  			// try rounding down a little
   870  			t.SetMode(big.ToZero)
   871  			t.Set(x.val)
   872  			if _, acc := t.Int(i); acc == big.Exact {
   873  				return makeInt(i)
   874  			}
   875  
   876  			// try rounding up a little
   877  			t.SetMode(big.AwayFromZero)
   878  			t.Set(x.val)
   879  			if _, acc := t.Int(i); acc == big.Exact {
   880  				return makeInt(i)
   881  			}
   882  		}
   883  
   884  	case complexVal:
   885  		if re := ToFloat(x); re.Kind() == Float {
   886  			return ToInt(re)
   887  		}
   888  	}
   889  
   890  	return unknownVal{}
   891  }
   892  
   893  // ToFloat converts x to a Float value if x is representable as a Float.
   894  // Otherwise it returns an Unknown.
   895  func ToFloat(x Value) Value {
   896  	switch x := x.(type) {
   897  	case int64Val:
   898  		return i64tor(x) // x is always a small int
   899  	case intVal:
   900  		if smallInt(x.val) {
   901  			return itor(x)
   902  		}
   903  		return itof(x)
   904  	case ratVal, floatVal:
   905  		return x
   906  	case complexVal:
   907  		if Sign(x.im) == 0 {
   908  			return ToFloat(x.re)
   909  		}
   910  	}
   911  	return unknownVal{}
   912  }
   913  
   914  // ToComplex converts x to a Complex value if x is representable as a Complex.
   915  // Otherwise it returns an Unknown.
   916  func ToComplex(x Value) Value {
   917  	switch x := x.(type) {
   918  	case int64Val, intVal, ratVal, floatVal:
   919  		return vtoc(x)
   920  	case complexVal:
   921  		return x
   922  	}
   923  	return unknownVal{}
   924  }
   925  
   926  // ----------------------------------------------------------------------------
   927  // Operations
   928  
   929  // is32bit reports whether x can be represented using 32 bits.
   930  func is32bit(x int64) bool {
   931  	const s = 32
   932  	return -1<<(s-1) <= x && x <= 1<<(s-1)-1
   933  }
   934  
   935  // is63bit reports whether x can be represented using 63 bits.
   936  func is63bit(x int64) bool {
   937  	const s = 63
   938  	return -1<<(s-1) <= x && x <= 1<<(s-1)-1
   939  }
   940  
   941  // UnaryOp returns the result of the unary expression op y.
   942  // The operation must be defined for the operand.
   943  // If prec > 0 it specifies the ^ (xor) result size in bits.
   944  // If y is Unknown, the result is Unknown.
   945  func UnaryOp(op token.Token, y Value, prec uint) Value {
   946  	switch op {
   947  	case token.ADD:
   948  		switch y.(type) {
   949  		case unknownVal, int64Val, intVal, ratVal, floatVal, complexVal:
   950  			return y
   951  		}
   952  
   953  	case token.SUB:
   954  		switch y := y.(type) {
   955  		case unknownVal:
   956  			return y
   957  		case int64Val:
   958  			if z := -y; z != y {
   959  				return z // no overflow
   960  			}
   961  			return makeInt(newInt().Neg(big.NewInt(int64(y))))
   962  		case intVal:
   963  			return makeInt(newInt().Neg(y.val))
   964  		case ratVal:
   965  			return makeRat(newRat().Neg(y.val))
   966  		case floatVal:
   967  			return makeFloat(newFloat().Neg(y.val))
   968  		case complexVal:
   969  			re := UnaryOp(token.SUB, y.re, 0)
   970  			im := UnaryOp(token.SUB, y.im, 0)
   971  			return makeComplex(re, im)
   972  		}
   973  
   974  	case token.XOR:
   975  		z := newInt()
   976  		switch y := y.(type) {
   977  		case unknownVal:
   978  			return y
   979  		case int64Val:
   980  			z.Not(big.NewInt(int64(y)))
   981  		case intVal:
   982  			z.Not(y.val)
   983  		default:
   984  			goto Error
   985  		}
   986  		// For unsigned types, the result will be negative and
   987  		// thus "too large": We must limit the result precision
   988  		// to the type's precision.
   989  		if prec > 0 {
   990  			z.AndNot(z, newInt().Lsh(big.NewInt(-1), prec)) // z &^= (-1)<<prec
   991  		}
   992  		return makeInt(z)
   993  
   994  	case token.NOT:
   995  		switch y := y.(type) {
   996  		case unknownVal:
   997  			return y
   998  		case boolVal:
   999  			return !y
  1000  		}
  1001  	}
  1002  
  1003  Error:
  1004  	panic(fmt.Sprintf("invalid unary operation %s%v", op, y))
  1005  }
  1006  
  1007  func ord(x Value) int {
  1008  	switch x.(type) {
  1009  	default:
  1010  		// force invalid value into "x position" in match
  1011  		// (don't panic here so that callers can provide a better error message)
  1012  		return -1
  1013  	case unknownVal:
  1014  		return 0
  1015  	case boolVal, *stringVal:
  1016  		return 1
  1017  	case int64Val:
  1018  		return 2
  1019  	case intVal:
  1020  		return 3
  1021  	case ratVal:
  1022  		return 4
  1023  	case floatVal:
  1024  		return 5
  1025  	case complexVal:
  1026  		return 6
  1027  	}
  1028  }
  1029  
  1030  // match returns the matching representation (same type) with the
  1031  // smallest complexity for two values x and y. If one of them is
  1032  // numeric, both of them must be numeric. If one of them is Unknown
  1033  // or invalid (say, nil) both results are that value.
  1034  func match(x, y Value) (_, _ Value) {
  1035  	switch ox, oy := ord(x), ord(y); {
  1036  	case ox < oy:
  1037  		x, y = match0(x, y)
  1038  	case ox > oy:
  1039  		y, x = match0(y, x)
  1040  	}
  1041  	return x, y
  1042  }
  1043  
  1044  // match0 must only be called by match.
  1045  // Invariant: ord(x) < ord(y)
  1046  func match0(x, y Value) (_, _ Value) {
  1047  	// Prefer to return the original x and y arguments when possible,
  1048  	// to avoid unnecessary heap allocations.
  1049  
  1050  	switch y.(type) {
  1051  	case intVal:
  1052  		switch x1 := x.(type) {
  1053  		case int64Val:
  1054  			return i64toi(x1), y
  1055  		}
  1056  	case ratVal:
  1057  		switch x1 := x.(type) {
  1058  		case int64Val:
  1059  			return i64tor(x1), y
  1060  		case intVal:
  1061  			return itor(x1), y
  1062  		}
  1063  	case floatVal:
  1064  		switch x1 := x.(type) {
  1065  		case int64Val:
  1066  			return i64tof(x1), y
  1067  		case intVal:
  1068  			return itof(x1), y
  1069  		case ratVal:
  1070  			return rtof(x1), y
  1071  		}
  1072  	case complexVal:
  1073  		return vtoc(x), y
  1074  	}
  1075  
  1076  	// force unknown and invalid values into "x position" in callers of match
  1077  	// (don't panic here so that callers can provide a better error message)
  1078  	return x, x
  1079  }
  1080  
  1081  // BinaryOp returns the result of the binary expression x op y.
  1082  // The operation must be defined for the operands. If one of the
  1083  // operands is Unknown, the result is Unknown.
  1084  // BinaryOp doesn't handle comparisons or shifts; use Compare
  1085  // or Shift instead.
  1086  //
  1087  // To force integer division of Int operands, use op == token.QUO_ASSIGN
  1088  // instead of token.QUO; the result is guaranteed to be Int in this case.
  1089  // Division by zero leads to a run-time panic.
  1090  func BinaryOp(x_ Value, op token.Token, y_ Value) Value {
  1091  	x, y := match(x_, y_)
  1092  
  1093  	switch x := x.(type) {
  1094  	case unknownVal:
  1095  		return x
  1096  
  1097  	case boolVal:
  1098  		y := y.(boolVal)
  1099  		switch op {
  1100  		case token.LAND:
  1101  			return x && y
  1102  		case token.LOR:
  1103  			return x || y
  1104  		}
  1105  
  1106  	case int64Val:
  1107  		a := int64(x)
  1108  		b := int64(y.(int64Val))
  1109  		var c int64
  1110  		switch op {
  1111  		case token.ADD:
  1112  			if !is63bit(a) || !is63bit(b) {
  1113  				return makeInt(newInt().Add(big.NewInt(a), big.NewInt(b)))
  1114  			}
  1115  			c = a + b
  1116  		case token.SUB:
  1117  			if !is63bit(a) || !is63bit(b) {
  1118  				return makeInt(newInt().Sub(big.NewInt(a), big.NewInt(b)))
  1119  			}
  1120  			c = a - b
  1121  		case token.MUL:
  1122  			if !is32bit(a) || !is32bit(b) {
  1123  				return makeInt(newInt().Mul(big.NewInt(a), big.NewInt(b)))
  1124  			}
  1125  			c = a * b
  1126  		case token.QUO:
  1127  			return makeRat(big.NewRat(a, b))
  1128  		case token.QUO_ASSIGN: // force integer division
  1129  			c = a / b
  1130  		case token.REM:
  1131  			c = a % b
  1132  		case token.AND:
  1133  			c = a & b
  1134  		case token.OR:
  1135  			c = a | b
  1136  		case token.XOR:
  1137  			c = a ^ b
  1138  		case token.AND_NOT:
  1139  			c = a &^ b
  1140  		default:
  1141  			goto Error
  1142  		}
  1143  		return int64Val(c)
  1144  
  1145  	case intVal:
  1146  		a := x.val
  1147  		b := y.(intVal).val
  1148  		c := newInt()
  1149  		switch op {
  1150  		case token.ADD:
  1151  			c.Add(a, b)
  1152  		case token.SUB:
  1153  			c.Sub(a, b)
  1154  		case token.MUL:
  1155  			c.Mul(a, b)
  1156  		case token.QUO:
  1157  			return makeRat(newRat().SetFrac(a, b))
  1158  		case token.QUO_ASSIGN: // force integer division
  1159  			c.Quo(a, b)
  1160  		case token.REM:
  1161  			c.Rem(a, b)
  1162  		case token.AND:
  1163  			c.And(a, b)
  1164  		case token.OR:
  1165  			c.Or(a, b)
  1166  		case token.XOR:
  1167  			c.Xor(a, b)
  1168  		case token.AND_NOT:
  1169  			c.AndNot(a, b)
  1170  		default:
  1171  			goto Error
  1172  		}
  1173  		return makeInt(c)
  1174  
  1175  	case ratVal:
  1176  		a := x.val
  1177  		b := y.(ratVal).val
  1178  		c := newRat()
  1179  		switch op {
  1180  		case token.ADD:
  1181  			c.Add(a, b)
  1182  		case token.SUB:
  1183  			c.Sub(a, b)
  1184  		case token.MUL:
  1185  			c.Mul(a, b)
  1186  		case token.QUO:
  1187  			c.Quo(a, b)
  1188  		default:
  1189  			goto Error
  1190  		}
  1191  		return makeRat(c)
  1192  
  1193  	case floatVal:
  1194  		a := x.val
  1195  		b := y.(floatVal).val
  1196  		c := newFloat()
  1197  		switch op {
  1198  		case token.ADD:
  1199  			c.Add(a, b)
  1200  		case token.SUB:
  1201  			c.Sub(a, b)
  1202  		case token.MUL:
  1203  			c.Mul(a, b)
  1204  		case token.QUO:
  1205  			c.Quo(a, b)
  1206  		default:
  1207  			goto Error
  1208  		}
  1209  		return makeFloat(c)
  1210  
  1211  	case complexVal:
  1212  		y := y.(complexVal)
  1213  		a, b := x.re, x.im
  1214  		c, d := y.re, y.im
  1215  		var re, im Value
  1216  		switch op {
  1217  		case token.ADD:
  1218  			// (a+c) + i(b+d)
  1219  			re = add(a, c)
  1220  			im = add(b, d)
  1221  		case token.SUB:
  1222  			// (a-c) + i(b-d)
  1223  			re = sub(a, c)
  1224  			im = sub(b, d)
  1225  		case token.MUL:
  1226  			// (ac-bd) + i(bc+ad)
  1227  			ac := mul(a, c)
  1228  			bd := mul(b, d)
  1229  			bc := mul(b, c)
  1230  			ad := mul(a, d)
  1231  			re = sub(ac, bd)
  1232  			im = add(bc, ad)
  1233  		case token.QUO:
  1234  			// (ac+bd)/s + i(bc-ad)/s, with s = cc + dd
  1235  			ac := mul(a, c)
  1236  			bd := mul(b, d)
  1237  			bc := mul(b, c)
  1238  			ad := mul(a, d)
  1239  			cc := mul(c, c)
  1240  			dd := mul(d, d)
  1241  			s := add(cc, dd)
  1242  			re = add(ac, bd)
  1243  			re = quo(re, s)
  1244  			im = sub(bc, ad)
  1245  			im = quo(im, s)
  1246  		default:
  1247  			goto Error
  1248  		}
  1249  		return makeComplex(re, im)
  1250  
  1251  	case *stringVal:
  1252  		if op == token.ADD {
  1253  			return &stringVal{l: x, r: y.(*stringVal)}
  1254  		}
  1255  	}
  1256  
  1257  Error:
  1258  	panic(fmt.Sprintf("invalid binary operation %v %s %v", x_, op, y_))
  1259  }
  1260  
  1261  func add(x, y Value) Value { return BinaryOp(x, token.ADD, y) }
  1262  func sub(x, y Value) Value { return BinaryOp(x, token.SUB, y) }
  1263  func mul(x, y Value) Value { return BinaryOp(x, token.MUL, y) }
  1264  func quo(x, y Value) Value { return BinaryOp(x, token.QUO, y) }
  1265  
  1266  // Shift returns the result of the shift expression x op s
  1267  // with op == token.SHL or token.SHR (<< or >>). x must be
  1268  // an Int or an Unknown. If x is Unknown, the result is x.
  1269  func Shift(x Value, op token.Token, s uint) Value {
  1270  	switch x := x.(type) {
  1271  	case unknownVal:
  1272  		return x
  1273  
  1274  	case int64Val:
  1275  		if s == 0 {
  1276  			return x
  1277  		}
  1278  		switch op {
  1279  		case token.SHL:
  1280  			z := i64toi(x).val
  1281  			return makeInt(z.Lsh(z, s))
  1282  		case token.SHR:
  1283  			return x >> s
  1284  		}
  1285  
  1286  	case intVal:
  1287  		if s == 0 {
  1288  			return x
  1289  		}
  1290  		z := newInt()
  1291  		switch op {
  1292  		case token.SHL:
  1293  			return makeInt(z.Lsh(x.val, s))
  1294  		case token.SHR:
  1295  			return makeInt(z.Rsh(x.val, s))
  1296  		}
  1297  	}
  1298  
  1299  	panic(fmt.Sprintf("invalid shift %v %s %d", x, op, s))
  1300  }
  1301  
  1302  func cmpZero(x int, op token.Token) bool {
  1303  	switch op {
  1304  	case token.EQL:
  1305  		return x == 0
  1306  	case token.NEQ:
  1307  		return x != 0
  1308  	case token.LSS:
  1309  		return x < 0
  1310  	case token.LEQ:
  1311  		return x <= 0
  1312  	case token.GTR:
  1313  		return x > 0
  1314  	case token.GEQ:
  1315  		return x >= 0
  1316  	}
  1317  	panic(fmt.Sprintf("invalid comparison %v %s 0", x, op))
  1318  }
  1319  
  1320  // Compare returns the result of the comparison x op y.
  1321  // The comparison must be defined for the operands.
  1322  // If one of the operands is Unknown, the result is
  1323  // false.
  1324  func Compare(x_ Value, op token.Token, y_ Value) bool {
  1325  	x, y := match(x_, y_)
  1326  
  1327  	switch x := x.(type) {
  1328  	case unknownVal:
  1329  		return false
  1330  
  1331  	case boolVal:
  1332  		y := y.(boolVal)
  1333  		switch op {
  1334  		case token.EQL:
  1335  			return x == y
  1336  		case token.NEQ:
  1337  			return x != y
  1338  		}
  1339  
  1340  	case int64Val:
  1341  		y := y.(int64Val)
  1342  		switch op {
  1343  		case token.EQL:
  1344  			return x == y
  1345  		case token.NEQ:
  1346  			return x != y
  1347  		case token.LSS:
  1348  			return x < y
  1349  		case token.LEQ:
  1350  			return x <= y
  1351  		case token.GTR:
  1352  			return x > y
  1353  		case token.GEQ:
  1354  			return x >= y
  1355  		}
  1356  
  1357  	case intVal:
  1358  		return cmpZero(x.val.Cmp(y.(intVal).val), op)
  1359  
  1360  	case ratVal:
  1361  		return cmpZero(x.val.Cmp(y.(ratVal).val), op)
  1362  
  1363  	case floatVal:
  1364  		return cmpZero(x.val.Cmp(y.(floatVal).val), op)
  1365  
  1366  	case complexVal:
  1367  		y := y.(complexVal)
  1368  		re := Compare(x.re, token.EQL, y.re)
  1369  		im := Compare(x.im, token.EQL, y.im)
  1370  		switch op {
  1371  		case token.EQL:
  1372  			return re && im
  1373  		case token.NEQ:
  1374  			return !re || !im
  1375  		}
  1376  
  1377  	case *stringVal:
  1378  		xs := x.string()
  1379  		ys := y.(*stringVal).string()
  1380  		switch op {
  1381  		case token.EQL:
  1382  			return xs == ys
  1383  		case token.NEQ:
  1384  			return xs != ys
  1385  		case token.LSS:
  1386  			return xs < ys
  1387  		case token.LEQ:
  1388  			return xs <= ys
  1389  		case token.GTR:
  1390  			return xs > ys
  1391  		case token.GEQ:
  1392  			return xs >= ys
  1393  		}
  1394  	}
  1395  
  1396  	panic(fmt.Sprintf("invalid comparison %v %s %v", x_, op, y_))
  1397  }
  1398  

View as plain text