// goVal returns the Go value for val, or nil. func goVal(val constant.Value) interface{} { // val should exist, but be conservative and check if val == nil { return nil } // Match implementation restriction of other compilers. // gc only checks duplicates for integer, floating-point // and string values, so only create Go values for these // types. switch val.Kind() { case constant.Int: if x, ok := constant.Int64Val(val); ok { return x } if x, ok := constant.Uint64Val(val); ok { return x } case constant.Float: if x, ok := constant.Float64Val(val); ok { return x } case constant.String: return constant.StringVal(val) } return nil }
func (check *Checker) shift(x, y *operand, e *ast.BinaryExpr, op token.Token) { untypedx := isUntyped(x.typ) var xval constant.Value if x.mode == constant_ { xval = constant.ToInt(x.val) } if isInteger(x.typ) || untypedx && xval != nil && xval.Kind() == constant.Int { // The lhs is of integer type or an untyped constant representable // as an integer. Nothing to do. } else { // shift has no chance check.invalidOp(x.pos(), "shifted operand %s must be integer", x) x.mode = invalid return } // spec: "The right operand in a shift expression must have unsigned // integer type or be an untyped constant that can be converted to // unsigned integer type." switch { case isUnsigned(y.typ): // nothing to do case isUntyped(y.typ): check.convertUntyped(y, Typ[UntypedInt]) if y.mode == invalid { x.mode = invalid return } default: check.invalidOp(y.pos(), "shift count %s must be unsigned integer", y) x.mode = invalid return } if x.mode == constant_ { if y.mode == constant_ { // rhs must be an integer value yval := constant.ToInt(y.val) if yval.Kind() != constant.Int { check.invalidOp(y.pos(), "shift count %s must be unsigned integer", y) x.mode = invalid return } // rhs must be within reasonable bounds const shiftBound = 1023 - 1 + 52 // so we can express smallestFloat64 s, ok := constant.Uint64Val(yval) if !ok || s > shiftBound { check.invalidOp(y.pos(), "invalid shift count %s", y) x.mode = invalid return } // The lhs is representable as an integer but may not be an integer // (e.g., 2.0, an untyped float) - this can only happen for untyped // non-integer numeric constants. Correct the type so that the shift // result is of integer type. if !isInteger(x.typ) { x.typ = Typ[UntypedInt] } // x is a constant so xval != nil and it must be of Int kind. x.val = constant.Shift(xval, op, uint(s)) // Typed constants must be representable in // their type after each constant operation. if isTyped(x.typ) { if e != nil { x.expr = e // for better error message } check.representable(x, x.typ.Underlying().(*Basic)) } return } // non-constant shift with constant lhs if untypedx { // spec: "If the left operand of a non-constant shift // expression is an untyped constant, the type of the // constant is what it would be if the shift expression // were replaced by its left operand alone.". // // Delay operand checking until we know the final type // by marking the lhs expression as lhs shift operand. // // Usually (in correct programs), the lhs expression // is in the untyped map. However, it is possible to // create incorrect programs where the same expression // is evaluated twice (via a declaration cycle) such // that the lhs expression type is determined in the // first round and thus deleted from the map, and then // not found in the second round (double insertion of // the same expr node still just leads to one entry for // that node, and it can only be deleted once). // Be cautious and check for presence of entry. // Example: var e, f = int(1<<""[f]) // issue 11347 if info, found := check.untyped[x.expr]; found { info.isLhs = true check.untyped[x.expr] = info } // keep x's type x.mode = value return } } // constant rhs must be >= 0 if y.mode == constant_ && constant.Sign(y.val) < 0 { check.invalidOp(y.pos(), "shift count %s must not be negative", y) } // non-constant shift - lhs must be an integer if !isInteger(x.typ) { check.invalidOp(x.pos(), "shifted operand %s must be integer", x) x.mode = invalid return } x.mode = value }