| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package fingerprint |
| 11 |
|
| 12 |
import ( |
| 13 |
"fmt" |
| 14 |
"go/types" |
| 15 |
"reflect" |
| 16 |
"strconv" |
| 17 |
"strings" |
| 18 |
"text/scanner" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func Encode(t types.Type) (_ string, tricky bool) { return fingerprint(t) } |
| 41 |
|
| 42 |
|
| 43 |
type Tree struct{ tree sexpr } |
| 44 |
|
| 45 |
|
| 46 |
func (tree Tree) String() string { |
| 47 |
var out strings.Builder |
| 48 |
writeSexpr(&out, tree.tree) |
| 49 |
return out.String() |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
func Parse(fp string) Tree { |
| 57 |
return Tree{parseFingerprint(fp)} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
func Matches(x, y Tree) bool { |
| 64 |
return unify(x.tree, y.tree) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
e |
| 78 |
|
| 79 |
) |
| 80 |
) |
| 81 |
) |
| 82 |
) |
| 83 |
c? |
| 84 |
τ) |
| 85 |
|
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
pe |
| 90 |
|
| 91 |
e |
| 92 |
|
| 93 |
func fingerprint(t types.Type) (string, bool) { |
| 94 |
var buf strings.Builder |
| 95 |
tricky := false |
| 96 |
var print func(t types.Type) |
| 97 |
print = func(t types.Type) { |
| 98 |
switch t := t.(type) { |
| 99 |
case *types.Alias: |
| 100 |
print(types.Unalias(t)) |
| 101 |
|
| 102 |
case *types.Named: |
| 103 |
targs := t.TypeArgs() |
| 104 |
if targs != nil { |
| 105 |
buf.WriteString("(inst ") |
| 106 |
} |
| 107 |
tname := t.Obj() |
| 108 |
if tname.Pkg() != nil { |
| 109 |
fmt.Fprintf(&buf, "(qual %q %s)", tname.Pkg().Path(), tname.Name()) |
| 110 |
} else if tname.Name() != "error" && tname.Name() != "comparable" { |
| 111 |
panic(tname) |
| 112 |
} else { |
| 113 |
buf.WriteString(tname.Name()) |
| 114 |
} |
| 115 |
if targs != nil { |
| 116 |
for t0 := range targs.Types() { |
| 117 |
buf.WriteByte(' ') |
| 118 |
print(t0) |
| 119 |
} |
| 120 |
buf.WriteString(")") |
| 121 |
} |
| 122 |
|
| 123 |
case *types.Array: |
| 124 |
fmt.Fprintf(&buf, "(array %d ", t.Len()) |
| 125 |
print(t.Elem()) |
| 126 |
buf.WriteByte(')') |
| 127 |
|
| 128 |
case *types.Slice: |
| 129 |
buf.WriteString("(slice ") |
| 130 |
print(t.Elem()) |
| 131 |
buf.WriteByte(')') |
| 132 |
|
| 133 |
case *types.Pointer: |
| 134 |
buf.WriteString("(ptr ") |
| 135 |
print(t.Elem()) |
| 136 |
buf.WriteByte(')') |
| 137 |
|
| 138 |
case *types.Map: |
| 139 |
buf.WriteString("(map ") |
| 140 |
print(t.Key()) |
| 141 |
buf.WriteByte(' ') |
| 142 |
print(t.Elem()) |
| 143 |
buf.WriteByte(')') |
| 144 |
|
| 145 |
case *types.Chan: |
| 146 |
fmt.Fprintf(&buf, "(chan %d ", t.Dir()) |
| 147 |
print(t.Elem()) |
| 148 |
buf.WriteByte(')') |
| 149 |
|
| 150 |
case *types.Tuple: |
| 151 |
buf.WriteString("(tuple") |
| 152 |
for v := range t.Variables() { |
| 153 |
buf.WriteByte(' ') |
| 154 |
print(v.Type()) |
| 155 |
} |
| 156 |
buf.WriteByte(')') |
| 157 |
|
| 158 |
case *types.Basic: |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
switch t.Kind() { |
| 164 |
case types.Byte: |
| 165 |
buf.WriteString("byte") |
| 166 |
case types.Rune: |
| 167 |
buf.WriteString("rune") |
| 168 |
case types.UnsafePointer: |
| 169 |
buf.WriteString(`(qual "unsafe" Pointer)`) |
| 170 |
default: |
| 171 |
if t.Info()&types.IsUntyped != 0 { |
| 172 |
panic("fingerprint of untyped type") |
| 173 |
} |
| 174 |
buf.WriteString(t.String()) |
| 175 |
} |
| 176 |
|
| 177 |
case *types.Signature: |
| 178 |
buf.WriteString("(func ") |
| 179 |
print(t.Params()) |
| 180 |
if t.Variadic() { |
| 181 |
buf.WriteString(" v") |
| 182 |
} |
| 183 |
buf.WriteByte(' ') |
| 184 |
print(t.Results()) |
| 185 |
buf.WriteByte(')') |
| 186 |
|
| 187 |
case *types.Struct: |
| 188 |
|
| 189 |
|
| 190 |
buf.WriteString("(struct") |
| 191 |
for i := range t.NumFields() { |
| 192 |
f := t.Field(i) |
| 193 |
name := f.Name() |
| 194 |
if !f.Exported() { |
| 195 |
name = fmt.Sprintf("(qual %q %s)", f.Pkg().Path(), name) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
fmt.Fprintf(&buf, " %s %t %q ", name, f.Embedded(), t.Tag(i)) |
| 202 |
print(f.Type()) |
| 203 |
} |
| 204 |
buf.WriteByte(')') |
| 205 |
|
| 206 |
case *types.Interface: |
| 207 |
if t.NumMethods() == 0 { |
| 208 |
buf.WriteString("any") |
| 209 |
} else { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
tricky = true |
| 219 |
buf.WriteString("(interface)") |
| 220 |
} |
| 221 |
|
| 222 |
case *types.TypeParam: |
| 223 |
|
| 224 |
|
| 225 |
tricky = true |
| 226 |
fmt.Fprintf(&buf, "(%s %d)", symTypeparam, t.Index()) |
| 227 |
|
| 228 |
default: |
| 229 |
panic(t) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
print(t) |
| 234 |
|
| 235 |
return buf.String(), tricky |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
type ( |
| 240 |
sexpr any |
| 241 |
symbol string |
| 242 |
cons struct{ car, cdr sexpr } |
| 243 |
) |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
func parseFingerprint(fp string) sexpr { |
| 250 |
var scan scanner.Scanner |
| 251 |
scan.Error = func(scan *scanner.Scanner, msg string) { panic(msg) } |
| 252 |
scan.Init(strings.NewReader(fp)) |
| 253 |
|
| 254 |
|
| 255 |
var tok rune |
| 256 |
next := func() { tok = scan.Scan() } |
| 257 |
|
| 258 |
next() |
| 259 |
|
| 260 |
|
| 261 |
var parse func() sexpr |
| 262 |
parse = func() sexpr { |
| 263 |
if tok == '(' { |
| 264 |
next() |
| 265 |
var head sexpr |
| 266 |
tailcdr := &head |
| 267 |
for tok != ')' { |
| 268 |
cell := &cons{car: parse()} |
| 269 |
*tailcdr = cell |
| 270 |
tailcdr = &cell.cdr |
| 271 |
} |
| 272 |
next() |
| 273 |
return head |
| 274 |
} |
| 275 |
|
| 276 |
s := scan.TokenText() |
| 277 |
switch tok { |
| 278 |
case scanner.Ident: |
| 279 |
next() |
| 280 |
return symbol(s) |
| 281 |
|
| 282 |
case scanner.Int: |
| 283 |
next() |
| 284 |
i, err := strconv.Atoi(s) |
| 285 |
if err != nil { |
| 286 |
panic(err) |
| 287 |
} |
| 288 |
return i |
| 289 |
|
| 290 |
case scanner.String: |
| 291 |
next() |
| 292 |
s, err := strconv.Unquote(s) |
| 293 |
if err != nil { |
| 294 |
panic(err) |
| 295 |
} |
| 296 |
return s |
| 297 |
|
| 298 |
default: |
| 299 |
panic(tok) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return parse() |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
func writeSexpr(out *strings.Builder, x sexpr) { |
| 309 |
switch x := x.(type) { |
| 310 |
case nil: |
| 311 |
out.WriteString("()") |
| 312 |
case string: |
| 313 |
fmt.Fprintf(out, "%q", x) |
| 314 |
case int: |
| 315 |
fmt.Fprintf(out, "%d", x) |
| 316 |
case symbol: |
| 317 |
out.WriteString(string(x)) |
| 318 |
case *cons: |
| 319 |
out.WriteString("(") |
| 320 |
for { |
| 321 |
writeSexpr(out, x.car) |
| 322 |
if x.cdr == nil { |
| 323 |
break |
| 324 |
} else if cdr, ok := x.cdr.(*cons); ok { |
| 325 |
x = cdr |
| 326 |
out.WriteByte(' ') |
| 327 |
} else { |
| 328 |
|
| 329 |
|
| 330 |
out.WriteString(" . ") |
| 331 |
print(x.cdr) |
| 332 |
break |
| 333 |
} |
| 334 |
} |
| 335 |
out.WriteString(")") |
| 336 |
default: |
| 337 |
panic(x) |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
func unify(x, y sexpr) bool { |
| 345 |
|
| 346 |
|
| 347 |
var maxTypeParam func(x sexpr) int |
| 348 |
maxTypeParam = func(x sexpr) int { |
| 349 |
if i := typeParamIndex(x); i >= 0 { |
| 350 |
return i |
| 351 |
} |
| 352 |
if c, ok := x.(*cons); ok { |
| 353 |
return max(maxTypeParam(c.car), maxTypeParam(c.cdr)) |
| 354 |
} |
| 355 |
return -1 |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
xBindings := make([]*sexpr, maxTypeParam(x)+1) |
| 362 |
for i := range len(xBindings) { |
| 363 |
xBindings[i] = new(sexpr) |
| 364 |
} |
| 365 |
yBindings := make([]*sexpr, maxTypeParam(y)+1) |
| 366 |
for i := range len(yBindings) { |
| 367 |
yBindings[i] = new(sexpr) |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
bind := func(b *sexpr, s sexpr, bindings []*sexpr) bool { |
| 372 |
|
| 373 |
var occurs func(s sexpr) bool |
| 374 |
occurs = func(s sexpr) bool { |
| 375 |
if j := typeParamIndex(s); j >= 0 { |
| 376 |
return b == bindings[j] |
| 377 |
} |
| 378 |
if c, ok := s.(*cons); ok { |
| 379 |
return occurs(c.car) || occurs(c.cdr) |
| 380 |
} |
| 381 |
return false |
| 382 |
} |
| 383 |
|
| 384 |
if occurs(s) { |
| 385 |
return false |
| 386 |
} |
| 387 |
*b = s |
| 388 |
return true |
| 389 |
} |
| 390 |
|
| 391 |
var uni func(x, y sexpr) bool |
| 392 |
uni = func(x, y sexpr) bool { |
| 393 |
var bx, by *sexpr |
| 394 |
ix := typeParamIndex(x) |
| 395 |
if ix >= 0 { |
| 396 |
bx = xBindings[ix] |
| 397 |
} |
| 398 |
iy := typeParamIndex(y) |
| 399 |
if iy >= 0 { |
| 400 |
by = yBindings[iy] |
| 401 |
} |
| 402 |
|
| 403 |
if bx != nil || by != nil { |
| 404 |
|
| 405 |
if bx != nil && by != nil && *bx == nil && *by == nil { |
| 406 |
xBindings[ix] = yBindings[iy] |
| 407 |
return true |
| 408 |
} |
| 409 |
|
| 410 |
if bx != nil && *bx != nil { |
| 411 |
x = *bx |
| 412 |
} |
| 413 |
if by != nil && *by != nil { |
| 414 |
y = *by |
| 415 |
} |
| 416 |
|
| 417 |
if bx != nil && *bx == nil { |
| 418 |
return bind(bx, y, yBindings) |
| 419 |
} |
| 420 |
|
| 421 |
if by != nil && *by == nil { |
| 422 |
return bind(by, x, xBindings) |
| 423 |
} |
| 424 |
|
| 425 |
return uni(x, y) |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
if reflect.TypeOf(x) != reflect.TypeOf(y) { |
| 430 |
return false |
| 431 |
} |
| 432 |
switch x := x.(type) { |
| 433 |
case nil, string, int, symbol: |
| 434 |
return x == y |
| 435 |
case *cons: |
| 436 |
y := y.(*cons) |
| 437 |
if !uni(x.car, y.car) { |
| 438 |
return false |
| 439 |
} |
| 440 |
if x.cdr == nil { |
| 441 |
return y.cdr == nil |
| 442 |
} |
| 443 |
if y.cdr == nil { |
| 444 |
return false |
| 445 |
} |
| 446 |
return uni(x.cdr, y.cdr) |
| 447 |
default: |
| 448 |
panic(fmt.Sprintf("unify %T %T", x, y)) |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return uni(x, y) |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
func typeParamIndex(x sexpr) int { |
| 458 |
if x, ok := x.(*cons); ok { |
| 459 |
if sym, ok := x.car.(symbol); ok && sym == symTypeparam { |
| 460 |
return x.cdr.(*cons).car.(int) |
| 461 |
} |
| 462 |
} |
| 463 |
return -1 |
| 464 |
} |
| 465 |
|
| 466 |
const symTypeparam = "typeparam" |