// truncate float literal fv to 32-bit or 64-bit precision
// according to type; return truncated value.
func truncfltlit(v constant.Value, t *types.Type) constant.Value {
- if t.IsUntyped() || overflow(v, t) {
- // If there was overflow, simply continuing would set the
- // value to Inf which in turn would lead to spurious follow-on
- // errors. Avoid this by returning the existing value.
+ if t.IsUntyped() {
return v
}
// precision, according to type; return truncated value. In case of
// overflow, calls Errorf but does not truncate the input value.
func trunccmplxlit(v constant.Value, t *types.Type) constant.Value {
- if t.IsUntyped() || overflow(v, t) {
- // If there was overflow, simply continuing would set the
- // value to Inf which in turn would lead to spurious follow-on
- // errors. Avoid this by returning the existing value.
+ if t.IsUntyped() {
return v
}
switch {
case t.IsInteger():
v = toint(v)
- overflow(v, t)
return v
case t.IsFloat():
v = toflt(v)
func toflt(v constant.Value) constant.Value {
if v.Kind() == constant.Complex {
- if constant.Sign(constant.Imag(v)) != 0 {
- base.Errorf("constant %v truncated to real", v)
- }
v = constant.Real(v)
}
func toint(v constant.Value) constant.Value {
if v.Kind() == constant.Complex {
- if constant.Sign(constant.Imag(v)) != 0 {
- base.Errorf("constant %v truncated to integer", v)
- }
v = constant.Real(v)
}
return constant.MakeInt64(1)
}
-// overflow reports whether constant value v is too large
-// to represent with type t, and emits an error message if so.
-func overflow(v constant.Value, t *types.Type) bool {
- // v has already been converted
- // to appropriate form for t.
- if t.IsUntyped() {
- return false
- }
- if v.Kind() == constant.Int && constant.BitLen(v) > ir.ConstPrec {
- base.Errorf("integer too large")
- return true
- }
- if ir.ConstOverflow(v, t) {
- base.Errorf("constant %v overflows %v", types.FmtConst(v, false), t)
- return true
- }
- return false
-}
-
func tostr(v constant.Value) constant.Value {
if v.Kind() == constant.Int {
r := unicode.ReplacementChar