| 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 interp |
| 6 | |
| 7 | // Values |
| 8 | // |
| 9 | // All interpreter values are "boxed" in the empty interface, value. |
| 10 | // The range of possible dynamic types within value are: |
| 11 | // |
| 12 | // - bool |
| 13 | // - numbers (all built-in int/float/complex types are distinguished) |
| 14 | // - string |
| 15 | // - map[value]value --- maps for which usesBuiltinMap(keyType) |
| 16 | // *hashmap --- maps for which !usesBuiltinMap(keyType) |
| 17 | // - chan value |
| 18 | // - []value --- slices |
| 19 | // - iface --- interfaces. |
| 20 | // - structure --- structs. Fields are ordered and accessed by numeric indices. |
| 21 | // - array --- arrays. |
| 22 | // - *value --- pointers. Careful: *value is a distinct type from *array etc. |
| 23 | // - *ssa.Function \ |
| 24 | // *ssa.Builtin } --- functions. A nil 'func' is always of type *ssa.Function. |
| 25 | // *closure / |
| 26 | // - tuple --- as returned by Return, Next, "value,ok" modes, etc. |
| 27 | // - iter --- iterators from 'range' over map or string. |
| 28 | // - bad --- a poison pill for locals that have gone out of scope. |
| 29 | // - rtype -- the interpreter's concrete implementation of reflect.Type |
| 30 | // - **deferred -- the address of a frame's defer stack for a Defer._Stack. |
| 31 | // |
| 32 | // Note that nil is not on this list. |
| 33 | // |
| 34 | // Pay close attention to whether or not the dynamic type is a pointer. |
| 35 | // The compiler cannot help you since value is an empty interface. |
| 36 | |
| 37 | import ( |
| 38 | "bytes" |
| 39 | "fmt" |
| 40 | "go/types" |
| 41 | "io" |
| 42 | "reflect" |
| 43 | "strings" |
| 44 | "unsafe" |
| 45 | |
| 46 | "golang.org/x/tools/go/ssa" |
| 47 | "golang.org/x/tools/go/types/typeutil" |
| 48 | ) |
| 49 | |
| 50 | type value any |
| 51 | |
| 52 | type tuple []value |
| 53 | |
| 54 | type array []value |
| 55 | |
| 56 | type iface struct { |
| 57 | t types.Type // never an "untyped" type |
| 58 | v value |
| 59 | } |
| 60 | |
| 61 | type structure []value |
| 62 | |
| 63 | // For map, array, *array, slice, string or channel. |
| 64 | type iter interface { |
| 65 | // next returns a Tuple (key, value, ok). |
| 66 | // key and value are unaliased, e.g. copies of the sequence element. |
| 67 | next() tuple |
| 68 | } |
| 69 | |
| 70 | type closure struct { |
| 71 | Fn *ssa.Function |
| 72 | Env []value |
| 73 | } |
| 74 | |
| 75 | type bad struct{} |
| 76 | |
| 77 | type rtype struct { |
| 78 | t types.Type |
| 79 | } |
| 80 | |
| 81 | // Hash functions and equivalence relation: |
| 82 | |
| 83 | // hashString computes the FNV hash of s. |
| 84 | func hashString(s string) int { |
| 85 | var h uint32 |
| 86 | for i := 0; i < len(s); i++ { |
| 87 | h ^= uint32(s[i]) |
| 88 | h *= 16777619 |
| 89 | } |
| 90 | return int(h) |
| 91 | } |
| 92 | |
| 93 | var hasher = typeutil.MakeHasher() |
| 94 | |
| 95 | // hashType returns a hash for t such that |
| 96 | // types.Identical(x, y) => hashType(x) == hashType(y). |
| 97 | func hashType(t types.Type) int { |
| 98 | return int(hasher.Hash(t)) |
| 99 | } |
| 100 | |
| 101 | // usesBuiltinMap returns true if the built-in hash function and |
| 102 | // equivalence relation for type t are consistent with those of the |
| 103 | // interpreter's representation of type t. Such types are: all basic |
| 104 | // types (bool, numbers, string), pointers and channels. |
| 105 | // |
| 106 | // usesBuiltinMap returns false for types that require a custom map |
| 107 | // implementation: interfaces, arrays and structs. |
| 108 | // |
| 109 | // Panic ensues if t is an invalid map key type: function, map or slice. |
| 110 | func usesBuiltinMap(t types.Type) bool { |
| 111 | switch t := t.(type) { |
| 112 | case *types.Basic, *types.Chan, *types.Pointer: |
| 113 | return true |
| 114 | case *types.Named, *types.Alias: |
| 115 | return usesBuiltinMap(t.Underlying()) |
| 116 | case *types.Interface, *types.Array, *types.Struct: |
| 117 | return false |
| 118 | } |
| 119 | panic(fmt.Sprintf("invalid map key type: %T", t)) |
| 120 | } |
| 121 | |
| 122 | func (x array) eq(t types.Type, _y any) bool { |
| 123 | y := _y.(array) |
| 124 | tElt := t.Underlying().(*types.Array).Elem() |
| 125 | for i, xi := range x { |
| 126 | if !equals(tElt, xi, y[i]) { |
| 127 | return false |
| 128 | } |
| 129 | } |
| 130 | return true |
| 131 | } |
| 132 | |
| 133 | func (x array) hash(t types.Type) int { |
| 134 | h := 0 |
| 135 | tElt := t.Underlying().(*types.Array).Elem() |
| 136 | for _, xi := range x { |
| 137 | h += hash(t, tElt, xi) |
| 138 | } |
| 139 | return h |
| 140 | } |
| 141 | |
| 142 | func (x structure) eq(t types.Type, _y any) bool { |
| 143 | y := _y.(structure) |
| 144 | tStruct := t.Underlying().(*types.Struct) |
| 145 | for i, n := 0, tStruct.NumFields(); i < n; i++ { |
| 146 | if f := tStruct.Field(i); !f.Anonymous() { |
| 147 | if !equals(f.Type(), x[i], y[i]) { |
| 148 | return false |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return true |
| 153 | } |
| 154 | |
| 155 | func (x structure) hash(t types.Type) int { |
| 156 | tStruct := t.Underlying().(*types.Struct) |
| 157 | h := 0 |
| 158 | for i, n := 0, tStruct.NumFields(); i < n; i++ { |
| 159 | if f := tStruct.Field(i); !f.Anonymous() { |
| 160 | h += hash(t, f.Type(), x[i]) |
| 161 | } |
| 162 | } |
| 163 | return h |
| 164 | } |
| 165 | |
| 166 | // nil-tolerant variant of types.Identical. |
| 167 | func sameType(x, y types.Type) bool { |
| 168 | if x == nil { |
| 169 | return y == nil |
| 170 | } |
| 171 | return y != nil && types.Identical(x, y) |
| 172 | } |
| 173 | |
| 174 | func (x iface) eq(t types.Type, _y any) bool { |
| 175 | y := _y.(iface) |
| 176 | return sameType(x.t, y.t) && (x.t == nil || equals(x.t, x.v, y.v)) |
| 177 | } |
| 178 | |
| 179 | func (x iface) hash(outer types.Type) int { |
| 180 | return hashType(x.t)*8581 + hash(outer, x.t, x.v) |
| 181 | } |
| 182 | |
| 183 | func (x rtype) hash(_ types.Type) int { |
| 184 | return hashType(x.t) |
| 185 | } |
| 186 | |
| 187 | func (x rtype) eq(_ types.Type, y any) bool { |
| 188 | return types.Identical(x.t, y.(rtype).t) |
| 189 | } |
| 190 | |
| 191 | // equals returns true iff x and y are equal according to Go's |
| 192 | // linguistic equivalence relation for type t. |
| 193 | // In a well-typed program, the dynamic types of x and y are |
| 194 | // guaranteed equal. |
| 195 | func equals(t types.Type, x, y value) bool { |
| 196 | switch x := x.(type) { |
| 197 | case bool: |
| 198 | return x == y.(bool) |
| 199 | case int: |
| 200 | return x == y.(int) |
| 201 | case int8: |
| 202 | return x == y.(int8) |
| 203 | case int16: |
| 204 | return x == y.(int16) |
| 205 | case int32: |
| 206 | return x == y.(int32) |
| 207 | case int64: |
| 208 | return x == y.(int64) |
| 209 | case uint: |
| 210 | return x == y.(uint) |
| 211 | case uint8: |
| 212 | return x == y.(uint8) |
| 213 | case uint16: |
| 214 | return x == y.(uint16) |
| 215 | case uint32: |
| 216 | return x == y.(uint32) |
| 217 | case uint64: |
| 218 | return x == y.(uint64) |
| 219 | case uintptr: |
| 220 | return x == y.(uintptr) |
| 221 | case float32: |
| 222 | return x == y.(float32) |
| 223 | case float64: |
| 224 | return x == y.(float64) |
| 225 | case complex64: |
| 226 | return x == y.(complex64) |
| 227 | case complex128: |
| 228 | return x == y.(complex128) |
| 229 | case string: |
| 230 | return x == y.(string) |
| 231 | case *value: |
| 232 | return x == y.(*value) |
| 233 | case chan value: |
| 234 | return x == y.(chan value) |
| 235 | case structure: |
| 236 | return x.eq(t, y) |
| 237 | case array: |
| 238 | return x.eq(t, y) |
| 239 | case iface: |
| 240 | return x.eq(t, y) |
| 241 | case rtype: |
| 242 | return x.eq(t, y) |
| 243 | } |
| 244 | |
| 245 | // Since map, func and slice don't support comparison, this |
| 246 | // case is only reachable if one of x or y is literally nil |
| 247 | // (handled in eqnil) or via interface{} values. |
| 248 | panic(fmt.Sprintf("comparing uncomparable type %s", t)) |
| 249 | } |
| 250 | |
| 251 | // Returns an integer hash of x such that equals(x, y) => hash(x) == hash(y). |
| 252 | // The outer type is used only for the "unhashable" panic message. |
| 253 | func hash(outer, t types.Type, x value) int { |
| 254 | switch x := x.(type) { |
| 255 | case bool: |
| 256 | if x { |
| 257 | return 1 |
| 258 | } |
| 259 | return 0 |
| 260 | case int: |
| 261 | return x |
| 262 | case int8: |
| 263 | return int(x) |
| 264 | case int16: |
| 265 | return int(x) |
| 266 | case int32: |
| 267 | return int(x) |
| 268 | case int64: |
| 269 | return int(x) |
| 270 | case uint: |
| 271 | return int(x) |
| 272 | case uint8: |
| 273 | return int(x) |
| 274 | case uint16: |
| 275 | return int(x) |
| 276 | case uint32: |
| 277 | return int(x) |
| 278 | case uint64: |
| 279 | return int(x) |
| 280 | case uintptr: |
| 281 | return int(x) |
| 282 | case float32: |
| 283 | return int(x) |
| 284 | case float64: |
| 285 | return int(x) |
| 286 | case complex64: |
| 287 | return int(real(x)) |
| 288 | case complex128: |
| 289 | return int(real(x)) |
| 290 | case string: |
| 291 | return hashString(x) |
| 292 | case *value: |
| 293 | return int(uintptr(unsafe.Pointer(x))) |
| 294 | case chan value: |
| 295 | return int(uintptr(reflect.ValueOf(x).Pointer())) |
| 296 | case structure: |
| 297 | return x.hash(t) |
| 298 | case array: |
| 299 | return x.hash(t) |
| 300 | case iface: |
| 301 | return x.hash(t) |
| 302 | case rtype: |
| 303 | return x.hash(t) |
| 304 | } |
| 305 | panic(fmt.Sprintf("unhashable type %v", outer)) |
| 306 | } |
| 307 | |
| 308 | // reflect.Value struct values don't have a fixed shape, since the |
| 309 | // payload can be a scalar or an aggregate depending on the instance. |
| 310 | // So store (and load) can't simply use recursion over the shape of the |
| 311 | // rhs value, or the lhs, to copy the value; we need the static type |
| 312 | // information. (We can't make reflect.Value a new basic data type |
| 313 | // because its "structness" is exposed to Go programs.) |
| 314 | |
| 315 | // load returns the value of type T in *addr. |
| 316 | func load(T types.Type, addr *value) value { |
| 317 | switch T := T.Underlying().(type) { |
| 318 | case *types.Struct: |
| 319 | v := (*addr).(structure) |
| 320 | a := make(structure, len(v)) |
| 321 | for i := range a { |
| 322 | a[i] = load(T.Field(i).Type(), &v[i]) |
| 323 | } |
| 324 | return a |
| 325 | case *types.Array: |
| 326 | v := (*addr).(array) |
| 327 | a := make(array, len(v)) |
| 328 | for i := range a { |
| 329 | a[i] = load(T.Elem(), &v[i]) |
| 330 | } |
| 331 | return a |
| 332 | default: |
| 333 | return *addr |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // store stores value v of type T into *addr. |
| 338 | func store(T types.Type, addr *value, v value) { |
| 339 | switch T := T.Underlying().(type) { |
| 340 | case *types.Struct: |
| 341 | lhs := (*addr).(structure) |
| 342 | rhs := v.(structure) |
| 343 | for i := range lhs { |
| 344 | store(T.Field(i).Type(), &lhs[i], rhs[i]) |
| 345 | } |
| 346 | case *types.Array: |
| 347 | lhs := (*addr).(array) |
| 348 | rhs := v.(array) |
| 349 | for i := range lhs { |
| 350 | store(T.Elem(), &lhs[i], rhs[i]) |
| 351 | } |
| 352 | default: |
| 353 | *addr = v |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // Prints in the style of built-in println. |
| 358 | // (More or less; in gc println is actually a compiler intrinsic and |
| 359 | // can distinguish println(1) from println(interface{}(1)).) |
| 360 | func writeValue(buf *bytes.Buffer, v value) { |
| 361 | switch v := v.(type) { |
| 362 | case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, string: |
| 363 | fmt.Fprintf(buf, "%v", v) |
| 364 | |
| 365 | case map[value]value: |
| 366 | buf.WriteString("map[") |
| 367 | sep := "" |
| 368 | for k, e := range v { |
| 369 | buf.WriteString(sep) |
| 370 | sep = " " |
| 371 | writeValue(buf, k) |
| 372 | buf.WriteString(":") |
| 373 | writeValue(buf, e) |
| 374 | } |
| 375 | buf.WriteString("]") |
| 376 | |
| 377 | case *hashmap: |
| 378 | buf.WriteString("map[") |
| 379 | sep := " " |
| 380 | for _, e := range v.entries() { |
| 381 | for e != nil { |
| 382 | buf.WriteString(sep) |
| 383 | sep = " " |
| 384 | writeValue(buf, e.key) |
| 385 | buf.WriteString(":") |
| 386 | writeValue(buf, e.value) |
| 387 | e = e.next |
| 388 | } |
| 389 | } |
| 390 | buf.WriteString("]") |
| 391 | |
| 392 | case chan value: |
| 393 | fmt.Fprintf(buf, "%v", v) // (an address) |
| 394 | |
| 395 | case *value: |
| 396 | if v == nil { |
| 397 | buf.WriteString("<nil>") |
| 398 | } else { |
| 399 | fmt.Fprintf(buf, "%p", v) |
| 400 | } |
| 401 | |
| 402 | case iface: |
| 403 | fmt.Fprintf(buf, "(%s, ", v.t) |
| 404 | writeValue(buf, v.v) |
| 405 | buf.WriteString(")") |
| 406 | |
| 407 | case structure: |
| 408 | buf.WriteString("{") |
| 409 | for i, e := range v { |
| 410 | if i > 0 { |
| 411 | buf.WriteString(" ") |
| 412 | } |
| 413 | writeValue(buf, e) |
| 414 | } |
| 415 | buf.WriteString("}") |
| 416 | |
| 417 | case array: |
| 418 | buf.WriteString("[") |
| 419 | for i, e := range v { |
| 420 | if i > 0 { |
| 421 | buf.WriteString(" ") |
| 422 | } |
| 423 | writeValue(buf, e) |
| 424 | } |
| 425 | buf.WriteString("]") |
| 426 | |
| 427 | case []value: |
| 428 | buf.WriteString("[") |
| 429 | for i, e := range v { |
| 430 | if i > 0 { |
| 431 | buf.WriteString(" ") |
| 432 | } |
| 433 | writeValue(buf, e) |
| 434 | } |
| 435 | buf.WriteString("]") |
| 436 | |
| 437 | case *ssa.Function, *ssa.Builtin, *closure: |
| 438 | fmt.Fprintf(buf, "%p", v) // (an address) |
| 439 | |
| 440 | case rtype: |
| 441 | buf.WriteString(v.t.String()) |
| 442 | |
| 443 | case tuple: |
| 444 | // Unreachable in well-formed Go programs |
| 445 | buf.WriteString("(") |
| 446 | for i, e := range v { |
| 447 | if i > 0 { |
| 448 | buf.WriteString(", ") |
| 449 | } |
| 450 | writeValue(buf, e) |
| 451 | } |
| 452 | buf.WriteString(")") |
| 453 | |
| 454 | default: |
| 455 | fmt.Fprintf(buf, "<%T>", v) |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // Implements printing of Go values in the style of built-in println. |
| 460 | func toString(v value) string { |
| 461 | var b bytes.Buffer |
| 462 | writeValue(&b, v) |
| 463 | return b.String() |
| 464 | } |
| 465 | |
| 466 | // ------------------------------------------------------------------------ |
| 467 | // Iterators |
| 468 | |
| 469 | type stringIter struct { |
| 470 | *strings.Reader |
| 471 | i int |
| 472 | } |
| 473 | |
| 474 | func (it *stringIter) next() tuple { |
| 475 | okv := make(tuple, 3) |
| 476 | ch, n, err := it.ReadRune() |
| 477 | ok := err != io.EOF |
| 478 | okv[0] = ok |
| 479 | if ok { |
| 480 | okv[1] = it.i |
| 481 | okv[2] = ch |
| 482 | } |
| 483 | it.i += n |
| 484 | return okv |
| 485 | } |
| 486 | |
| 487 | type mapIter struct { |
| 488 | iter *reflect.MapIter |
| 489 | ok bool |
| 490 | } |
| 491 | |
| 492 | func (it *mapIter) next() tuple { |
| 493 | it.ok = it.iter.Next() |
| 494 | if !it.ok { |
| 495 | return []value{false, nil, nil} |
| 496 | } |
| 497 | k, v := it.iter.Key().Interface(), it.iter.Value().Interface() |
| 498 | return []value{true, k, v} |
| 499 | } |
| 500 | |
| 501 | type hashmapIter struct { |
| 502 | iter *reflect.MapIter |
| 503 | ok bool |
| 504 | cur *entry |
| 505 | } |
| 506 | |
| 507 | func (it *hashmapIter) next() tuple { |
| 508 | for { |
| 509 | if it.cur != nil { |
| 510 | k, v := it.cur.key, it.cur.value |
| 511 | it.cur = it.cur.next |
| 512 | return []value{true, k, v} |
| 513 | } |
| 514 | it.ok = it.iter.Next() |
| 515 | if !it.ok { |
| 516 | return []value{false, nil, nil} |
| 517 | } |
| 518 | it.cur = it.iter.Value().Interface().(*entry) |
| 519 | } |
| 520 | } |