| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package typerefs |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"go/ast" |
| 10 |
"go/token" |
| 11 |
"sort" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 15 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 16 |
"golang.org/x/tools/gopls/internal/util/frob" |
| 17 |
"golang.org/x/tools/internal/astutil" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func Encode(files []*parsego.File, imports map[metadata.ImportPath]*metadata.Package) []byte { |
| 28 |
return index(files, imports) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func Decode(pkgIndex *PackageIndex, data []byte) []Class { |
| 42 |
return decode(pkgIndex, data) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
type Class struct { |
| 54 |
Decls []string |
| 55 |
Refs []Symbol |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
type Symbol struct { |
| 61 |
Package IndexID |
| 62 |
Name string |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
type IndexID int |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
type symbolSet map[symbol]bool |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
type symbol struct { |
| 80 |
pkg metadata.PackageID |
| 81 |
name string |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
type declNode struct { |
| 90 |
name string |
| 91 |
rep *declNode |
| 92 |
|
| 93 |
|
| 94 |
intRefs map[*declNode]bool |
| 95 |
extRefs symbolSet |
| 96 |
extRefsClass int |
| 97 |
|
| 98 |
|
| 99 |
index, lowlink int32 |
| 100 |
scc int32 |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
type state struct { |
| 111 |
|
| 112 |
class []symbolSet |
| 113 |
classIndex map[string]int |
| 114 |
|
| 115 |
|
| 116 |
index int32 |
| 117 |
stack []*declNode |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
func (st *state) getClassIndex(set symbolSet) int { |
| 123 |
key := classKey(set) |
| 124 |
i, ok := st.classIndex[key] |
| 125 |
if !ok { |
| 126 |
i = len(st.class) |
| 127 |
st.classIndex[key] = i |
| 128 |
st.class = append(st.class, set) |
| 129 |
} |
| 130 |
return i |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
func (set symbolSet) appendSorted(syms []symbol) []symbol { |
| 137 |
for sym := range set { |
| 138 |
syms = append(syms, sym) |
| 139 |
} |
| 140 |
sort.Slice(syms, func(i, j int) bool { |
| 141 |
x, y := syms[i], syms[j] |
| 142 |
if x.pkg != y.pkg { |
| 143 |
return x.pkg < y.pkg |
| 144 |
} |
| 145 |
return x.name < y.name |
| 146 |
}) |
| 147 |
return syms |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func classKey(set symbolSet) string { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
var s strings.Builder |
| 158 |
for _, sym := range set.appendSorted(make([]symbol, 0, len(set))) { |
| 159 |
fmt.Fprintf(&s, "%s:%s;", sym.pkg, sym.name) |
| 160 |
} |
| 161 |
return s.String() |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
func index(pgfs []*parsego.File, imports map[metadata.ImportPath]*metadata.Package) []byte { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
decls := make(map[string]*declNode) |
| 171 |
addDecl := func(id *ast.Ident) { |
| 172 |
if name := id.Name; name != "_" && decls[name] == nil { |
| 173 |
node := &declNode{name: name, extRefsClass: -1} |
| 174 |
node.rep = node |
| 175 |
decls[name] = node |
| 176 |
} |
| 177 |
} |
| 178 |
for _, pgf := range pgfs { |
| 179 |
for _, d := range pgf.File.Decls { |
| 180 |
switch d := d.(type) { |
| 181 |
case *ast.GenDecl: |
| 182 |
switch d.Tok { |
| 183 |
case token.TYPE: |
| 184 |
for _, spec := range d.Specs { |
| 185 |
addDecl(spec.(*ast.TypeSpec).Name) |
| 186 |
} |
| 187 |
|
| 188 |
case token.VAR, token.CONST: |
| 189 |
for _, spec := range d.Specs { |
| 190 |
for _, ident := range spec.(*ast.ValueSpec).Names { |
| 191 |
addDecl(ident) |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
case *ast.FuncDecl: |
| 197 |
|
| 198 |
if d.Recv.NumFields() == 0 { |
| 199 |
addDecl(d.Name) |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
st := &state{classIndex: make(map[string]int)} |
| 207 |
for _, pgf := range pgfs { |
| 208 |
visitFile(pgf.File, imports, decls) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
st.index = 1 |
| 214 |
for _, decl := range decls { |
| 215 |
if decl.index == 0 { |
| 216 |
st.visit(decl) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
classNames := make(map[int][]string) |
| 232 |
for name, decl := range decls { |
| 233 |
if !ast.IsExported(name) { |
| 234 |
continue |
| 235 |
} |
| 236 |
|
| 237 |
decl = decl.find() |
| 238 |
|
| 239 |
|
| 240 |
if len(decl.extRefs) == 0 { |
| 241 |
continue |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
class := decl.extRefsClass |
| 246 |
if class < 0 { |
| 247 |
class = st.getClassIndex(decl.extRefs) |
| 248 |
decl.extRefsClass = class |
| 249 |
} |
| 250 |
classNames[class] = append(classNames[class], name) |
| 251 |
} |
| 252 |
|
| 253 |
return encode(classNames, st.class) |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
func visitFile(file *ast.File, imports map[metadata.ImportPath]*metadata.Package, decls map[string]*declNode) { |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
fileImports := make(map[string][]metadata.PackageID) |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
importEdge := func(decl *declNode, pkgname, name string) { |
| 268 |
if token.IsExported(name) { |
| 269 |
for _, depID := range fileImports[pkgname] { |
| 270 |
if decl.extRefs == nil { |
| 271 |
decl.extRefs = make(symbolSet) |
| 272 |
} |
| 273 |
decl.extRefs[symbol{depID, name}] = true |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
visit := func(fromId *ast.Ident, node ast.Node, tparams map[string]bool) { |
| 281 |
if fromId.Name == "_" { |
| 282 |
return |
| 283 |
} |
| 284 |
from := decls[fromId.Name] |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if from == nil { |
| 289 |
return |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
visitDeclOrSpec(node, func(name, sel string) { |
| 294 |
|
| 295 |
if tparams[name] { |
| 296 |
return |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if to, ok := decls[name]; ok { |
| 308 |
if from.intRefs == nil { |
| 309 |
from.intRefs = make(map[*declNode]bool) |
| 310 |
} |
| 311 |
from.intRefs[to] = true |
| 312 |
|
| 313 |
} else { |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
importEdge(from, ".", name) |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if sel != "" { |
| 327 |
importEdge(from, name, sel) |
| 328 |
} |
| 329 |
}) |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
for _, d := range file.Decls { |
| 335 |
switch d := d.(type) { |
| 336 |
case *ast.GenDecl: |
| 337 |
switch d.Tok { |
| 338 |
case token.IMPORT: |
| 339 |
|
| 340 |
for _, spec := range d.Specs { |
| 341 |
spec := spec.(*ast.ImportSpec) |
| 342 |
path := metadata.UnquoteImportPath(spec) |
| 343 |
if path == "" { |
| 344 |
continue |
| 345 |
} |
| 346 |
dep := imports[path] |
| 347 |
if dep == nil { |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
continue |
| 353 |
} |
| 354 |
name := string(dep.Name) |
| 355 |
if spec.Name != nil { |
| 356 |
if spec.Name.Name == "_" { |
| 357 |
continue |
| 358 |
} |
| 359 |
name = spec.Name.Name |
| 360 |
} |
| 361 |
fileImports[name] = append(fileImports[name], dep.ID) |
| 362 |
} |
| 363 |
|
| 364 |
case token.TYPE: |
| 365 |
for _, spec := range d.Specs { |
| 366 |
spec := spec.(*ast.TypeSpec) |
| 367 |
tparams := tparamsMap(spec.TypeParams) |
| 368 |
visit(spec.Name, spec, tparams) |
| 369 |
} |
| 370 |
|
| 371 |
case token.VAR, token.CONST: |
| 372 |
for _, spec := range d.Specs { |
| 373 |
spec := spec.(*ast.ValueSpec) |
| 374 |
for _, name := range spec.Names { |
| 375 |
visit(name, spec, nil) |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
case *ast.FuncDecl: |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
if d.Recv.NumFields() > 0 { |
| 386 |
|
| 387 |
_, id, typeParams := astutil.UnpackRecv(d.Recv.List[0].Type) |
| 388 |
if id != nil { |
| 389 |
var tparams map[string]bool |
| 390 |
if len(typeParams) > 0 { |
| 391 |
tparams = make(map[string]bool) |
| 392 |
for _, tparam := range typeParams { |
| 393 |
if tparam.Name != "_" { |
| 394 |
tparams[tparam.Name] = true |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
visit(id, d, tparams) |
| 399 |
} |
| 400 |
} else { |
| 401 |
|
| 402 |
tparams := tparamsMap(d.Type.TypeParams) |
| 403 |
visit(d.Name, d, tparams) |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
func tparamsMap(tparams *ast.FieldList) map[string]bool { |
| 413 |
if tparams == nil || len(tparams.List) == 0 { |
| 414 |
return nil |
| 415 |
} |
| 416 |
m := make(map[string]bool) |
| 417 |
for _, f := range tparams.List { |
| 418 |
for _, name := range f.Names { |
| 419 |
if name.Name != "_" { |
| 420 |
m[name.Name] = true |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
return m |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
type refVisitor = func(name, sel string) |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
func visitDeclOrSpec(node ast.Node, f refVisitor) { |
| 437 |
|
| 438 |
switch n := node.(type) { |
| 439 |
|
| 440 |
|
| 441 |
case *ast.ValueSpec: |
| 442 |
|
| 443 |
|
| 444 |
if n.Type != nil { |
| 445 |
visitExpr(n.Type, f) |
| 446 |
} else { |
| 447 |
visitExprList(n.Values, f) |
| 448 |
} |
| 449 |
|
| 450 |
case *ast.TypeSpec: |
| 451 |
|
| 452 |
if tparams := n.TypeParams; tparams != nil { |
| 453 |
visitFieldList(tparams, f) |
| 454 |
} |
| 455 |
visitExpr(n.Type, f) |
| 456 |
|
| 457 |
case *ast.BadDecl: |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
case *ast.FuncDecl: |
| 463 |
|
| 464 |
|
| 465 |
visitExpr(n.Type, f) |
| 466 |
|
| 467 |
default: |
| 468 |
panic(fmt.Sprintf("unexpected node type %T", node)) |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
func visitExpr(expr ast.Expr, f refVisitor) { |
| 478 |
switch n := expr.(type) { |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
case *ast.Ident: |
| 483 |
f(n.Name, "") |
| 484 |
|
| 485 |
case *ast.BasicLit: |
| 486 |
|
| 487 |
|
| 488 |
case *ast.SelectorExpr: |
| 489 |
if ident, ok := n.X.(*ast.Ident); ok { |
| 490 |
f(ident.Name, n.Sel.Name) |
| 491 |
} else { |
| 492 |
visitExpr(n.X, f) |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
} |
| 497 |
|
| 498 |
case *ast.CallExpr: |
| 499 |
visitExpr(n.Fun, f) |
| 500 |
visitExprList(n.Args, f) |
| 501 |
|
| 502 |
|
| 503 |
case *ast.Ellipsis: |
| 504 |
if n.Elt != nil { |
| 505 |
visitExpr(n.Elt, f) |
| 506 |
} |
| 507 |
|
| 508 |
case *ast.FuncLit: |
| 509 |
visitExpr(n.Type, f) |
| 510 |
|
| 511 |
|
| 512 |
case *ast.CompositeLit: |
| 513 |
if n.Type != nil { |
| 514 |
visitExpr(n.Type, f) |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
case *ast.ParenExpr: |
| 519 |
visitExpr(n.X, f) |
| 520 |
|
| 521 |
case *ast.IndexExpr: |
| 522 |
visitExpr(n.X, f) |
| 523 |
visitExpr(n.Index, f) |
| 524 |
|
| 525 |
case *ast.IndexListExpr: |
| 526 |
visitExpr(n.X, f) |
| 527 |
for _, index := range n.Indices { |
| 528 |
visitExpr(index, f) |
| 529 |
} |
| 530 |
|
| 531 |
case *ast.SliceExpr: |
| 532 |
visitExpr(n.X, f) |
| 533 |
|
| 534 |
|
| 535 |
case *ast.TypeAssertExpr: |
| 536 |
|
| 537 |
|
| 538 |
if n.Type != nil { |
| 539 |
visitExpr(n.Type, f) |
| 540 |
} |
| 541 |
|
| 542 |
case *ast.StarExpr: |
| 543 |
visitExpr(n.X, f) |
| 544 |
|
| 545 |
case *ast.UnaryExpr: |
| 546 |
visitExpr(n.X, f) |
| 547 |
|
| 548 |
case *ast.BinaryExpr: |
| 549 |
visitExpr(n.X, f) |
| 550 |
visitExpr(n.Y, f) |
| 551 |
|
| 552 |
case *ast.KeyValueExpr: |
| 553 |
panic("unreachable") |
| 554 |
|
| 555 |
case *ast.ArrayType: |
| 556 |
if n.Len != nil { |
| 557 |
visitExpr(n.Len, f) |
| 558 |
} |
| 559 |
visitExpr(n.Elt, f) |
| 560 |
|
| 561 |
case *ast.StructType: |
| 562 |
visitFieldList(n.Fields, f) |
| 563 |
|
| 564 |
case *ast.FuncType: |
| 565 |
if tparams := n.TypeParams; tparams != nil { |
| 566 |
visitFieldList(tparams, f) |
| 567 |
} |
| 568 |
if n.Params != nil { |
| 569 |
visitFieldList(n.Params, f) |
| 570 |
} |
| 571 |
if n.Results != nil { |
| 572 |
visitFieldList(n.Results, f) |
| 573 |
} |
| 574 |
|
| 575 |
case *ast.InterfaceType: |
| 576 |
visitFieldList(n.Methods, f) |
| 577 |
|
| 578 |
case *ast.MapType: |
| 579 |
visitExpr(n.Key, f) |
| 580 |
visitExpr(n.Value, f) |
| 581 |
|
| 582 |
case *ast.ChanType: |
| 583 |
visitExpr(n.Value, f) |
| 584 |
|
| 585 |
case *ast.BadExpr: |
| 586 |
|
| 587 |
|
| 588 |
default: |
| 589 |
panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n)) |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
func visitExprList(list []ast.Expr, f refVisitor) { |
| 594 |
for _, x := range list { |
| 595 |
visitExpr(x, f) |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func visitFieldList(n *ast.FieldList, f refVisitor) { |
| 600 |
for _, field := range n.List { |
| 601 |
visitExpr(field.Type, f) |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
func (st *state) visit(x *declNode) { |
| 611 |
checkCanonical(x) |
| 612 |
x.index = st.index |
| 613 |
x.lowlink = st.index |
| 614 |
st.index++ |
| 615 |
|
| 616 |
st.stack = append(st.stack, x) |
| 617 |
assert(x.scc == 0, "node revisited") |
| 618 |
x.scc = -1 |
| 619 |
|
| 620 |
for y := range x.intRefs { |
| 621 |
|
| 622 |
|
| 623 |
y := y.find() |
| 624 |
|
| 625 |
if x == y { |
| 626 |
continue |
| 627 |
} |
| 628 |
|
| 629 |
switch { |
| 630 |
case y.scc > 0: |
| 631 |
|
| 632 |
|
| 633 |
case y.scc < 0: |
| 634 |
|
| 635 |
if y.index < x.lowlink { |
| 636 |
x.lowlink = y.index |
| 637 |
} |
| 638 |
|
| 639 |
default: |
| 640 |
|
| 641 |
st.visit(y) |
| 642 |
|
| 643 |
|
| 644 |
x = x.find() |
| 645 |
|
| 646 |
if y.lowlink < x.lowlink { |
| 647 |
x.lowlink = y.lowlink |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
checkCanonical(x) |
| 652 |
|
| 653 |
|
| 654 |
if x.lowlink == x.index { |
| 655 |
|
| 656 |
for { |
| 657 |
|
| 658 |
i := len(st.stack) - 1 |
| 659 |
y := st.stack[i] |
| 660 |
st.stack = st.stack[:i] |
| 661 |
|
| 662 |
checkCanonical(x) |
| 663 |
checkCanonical(y) |
| 664 |
|
| 665 |
if x == y { |
| 666 |
break |
| 667 |
} |
| 668 |
coalesce(x, y) |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
for y := range x.intRefs { |
| 674 |
y := y.find() |
| 675 |
if y == x { |
| 676 |
continue |
| 677 |
} |
| 678 |
assert(y.scc == 1, "edge to non-scc node") |
| 679 |
for z := range y.extRefs { |
| 680 |
if x.extRefs == nil { |
| 681 |
x.extRefs = make(symbolSet) |
| 682 |
} |
| 683 |
x.extRefs[z] = true |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
x.scc = 1 |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
func coalesce(x, y *declNode) { |
| 694 |
|
| 695 |
y.rep = x |
| 696 |
|
| 697 |
|
| 698 |
for z := range y.intRefs { |
| 699 |
x.intRefs[z] = true |
| 700 |
} |
| 701 |
y.intRefs = nil |
| 702 |
|
| 703 |
|
| 704 |
for z := range y.extRefs { |
| 705 |
if x.extRefs == nil { |
| 706 |
x.extRefs = make(symbolSet) |
| 707 |
} |
| 708 |
x.extRefs[z] = true |
| 709 |
} |
| 710 |
y.extRefs = nil |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
func (decl *declNode) find() *declNode { |
| 716 |
rep := decl.rep |
| 717 |
if rep != decl { |
| 718 |
rep = rep.find() |
| 719 |
decl.rep = rep |
| 720 |
} |
| 721 |
return rep |
| 722 |
} |
| 723 |
|
| 724 |
const debugSCC = false |
| 725 |
|
| 726 |
func checkCanonical(x *declNode) { |
| 727 |
if debugSCC { |
| 728 |
assert(x == x.find(), "not canonical") |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
func assert(cond bool, msg string) { |
| 733 |
if debugSCC && !cond { |
| 734 |
panic(msg) |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
var classesCodec = frob.CodecFor[gobClasses]() |
| 742 |
|
| 743 |
type gobClasses struct { |
| 744 |
Strings []string |
| 745 |
Classes []gobClass |
| 746 |
} |
| 747 |
|
| 748 |
type gobClass struct { |
| 749 |
Decls []int32 |
| 750 |
Refs []int32 |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
func encode(classNames map[int][]string, classes []symbolSet) []byte { |
| 760 |
payload := gobClasses{ |
| 761 |
Classes: make([]gobClass, 0, len(classNames)), |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
strings := make(map[string]int32) |
| 766 |
stringIndex := func(s string) int32 { |
| 767 |
i, ok := strings[s] |
| 768 |
if !ok { |
| 769 |
i = int32(len(payload.Strings)) |
| 770 |
strings[s] = i |
| 771 |
payload.Strings = append(payload.Strings, s) |
| 772 |
} |
| 773 |
return i |
| 774 |
} |
| 775 |
|
| 776 |
var refs []symbol |
| 777 |
for class, names := range classNames { |
| 778 |
set := classes[class] |
| 779 |
|
| 780 |
|
| 781 |
sort.Strings(names) |
| 782 |
gobDecls := make([]int32, len(names)) |
| 783 |
for i, name := range names { |
| 784 |
gobDecls[i] = stringIndex(name) |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
gobRefs := make([]int32, 0, 2*len(set)) |
| 789 |
for _, sym := range set.appendSorted(refs[:0]) { |
| 790 |
gobRefs = append(gobRefs, |
| 791 |
stringIndex(string(sym.pkg)), |
| 792 |
stringIndex(sym.name)) |
| 793 |
} |
| 794 |
payload.Classes = append(payload.Classes, gobClass{ |
| 795 |
Decls: gobDecls, |
| 796 |
Refs: gobRefs, |
| 797 |
}) |
| 798 |
} |
| 799 |
|
| 800 |
return classesCodec.Encode(payload) |
| 801 |
} |
| 802 |
|
| 803 |
func decode(pkgIndex *PackageIndex, data []byte) []Class { |
| 804 |
payload := classesCodec.Decode(data) |
| 805 |
|
| 806 |
classes := make([]Class, len(payload.Classes)) |
| 807 |
for i, gobClass := range payload.Classes { |
| 808 |
decls := make([]string, len(gobClass.Decls)) |
| 809 |
for i, decl := range gobClass.Decls { |
| 810 |
decls[i] = payload.Strings[decl] |
| 811 |
} |
| 812 |
refs := make([]Symbol, len(gobClass.Refs)/2) |
| 813 |
for i := range refs { |
| 814 |
pkgID := pkgIndex.IndexID(metadata.PackageID(payload.Strings[gobClass.Refs[2*i]])) |
| 815 |
name := payload.Strings[gobClass.Refs[2*i+1]] |
| 816 |
refs[i] = Symbol{Package: pkgID, Name: name} |
| 817 |
} |
| 818 |
classes[i] = Class{ |
| 819 |
Decls: decls, |
| 820 |
Refs: refs, |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
sort.Slice(classes, func(i, j int) bool { |
| 827 |
return classes[i].Decls[0] < classes[j].Decls[0] |
| 828 |
}) |
| 829 |
|
| 830 |
return classes |
| 831 |
} |