| 1 | // Copyright 2023 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 typerefs extracts symbol-level reachability information |
| 6 | // from the syntax of a Go package. |
| 7 | // |
| 8 | // # Background |
| 9 | // |
| 10 | // The goal of this analysis is to determine, for each package P, a nearly |
| 11 | // minimal set of packages that could affect the type checking of P. This set |
| 12 | // may contain false positives, but the smaller this set the better we can |
| 13 | // invalidate and prune packages in gopls. |
| 14 | // |
| 15 | // More precisely, for each package P we define the set of "reachable" packages |
| 16 | // from P as the set of packages that may affect the (deep) export data of the |
| 17 | // direct dependencies of P. By this definition, the complement of this set |
| 18 | // cannot affect any information derived from type checking P, such as |
| 19 | // diagnostics, cross references, or method sets. Therefore we need not |
| 20 | // invalidate any results for P when a package in the complement of this set |
| 21 | // changes. |
| 22 | // |
| 23 | // # Computing references |
| 24 | // |
| 25 | // For a given declaration D, references are computed based on identifiers or |
| 26 | // dotted identifiers referenced in the declaration of D, that may affect |
| 27 | // the type of D. However, these references reflect only local knowledge of the |
| 28 | // package and its dependency metadata, and do not depend on any analysis of |
| 29 | // the dependencies themselves. This allows the reference information for |
| 30 | // a package to be cached independent of all others. |
| 31 | // |
| 32 | // Specifically, if a referring identifier I appears in the declaration, we |
| 33 | // record an edge from D to each object possibly referenced by I. We search for |
| 34 | // references within type syntax, but do not actually type-check, so we can't |
| 35 | // reliably determine whether an expression is a type or a term, or whether a |
| 36 | // function is a builtin or generic. For example, the type of x in var x = |
| 37 | // p.F(W) only depends on W if p.F is a builtin or generic function, which we |
| 38 | // cannot know without type-checking package p. So we may over-approximate in |
| 39 | // this way. |
| 40 | // |
| 41 | // - If I is declared in the current package, record a reference to its |
| 42 | // declaration. |
| 43 | // - Otherwise, if there are any dot imports in the current |
| 44 | // file and I is exported, record a (possibly dangling) edge to |
| 45 | // the corresponding declaration in each dot-imported package. |
| 46 | // |
| 47 | // If a dotted identifier q.I appears in the declaration, we |
| 48 | // perform a similar operation: |
| 49 | // |
| 50 | // - If q is declared in the current package, we record a reference to that |
| 51 | // object. It may be a var or const that has a field or method I. |
| 52 | // - Otherwise, if q is a valid import name based on imports in the current file |
| 53 | // and the provided metadata for dependency package names, record a |
| 54 | // reference to the object I in that package. |
| 55 | // - Additionally, handle the case where Q is exported, and Q.I may refer to |
| 56 | // a field or method in a dot-imported package. |
| 57 | // |
| 58 | // That is essentially the entire algorithm, though there is some subtlety to |
| 59 | // visiting the set of identifiers or dotted identifiers that may affect the |
| 60 | // declaration type. See the visitDeclOrSpec function for the details of this |
| 61 | // analysis. Notably, we also skip identifiers that refer to type parameters in |
| 62 | // generic declarations. |
| 63 | // |
| 64 | // # Graph optimizations |
| 65 | // |
| 66 | // The references extracted from the syntax are used to construct |
| 67 | // edges between nodes representing declarations. Edges are of two |
| 68 | // kinds: internal references, from one package-level declaration to |
| 69 | // another; and external references, from a symbol in this package to |
| 70 | // a symbol imported from a direct dependency. |
| 71 | // |
| 72 | // Once the symbol reference graph is constructed, we find its |
| 73 | // strongly connected components (SCCs) using Tarjan's algorithm. |
| 74 | // As we coalesce the nodes of each SCC we compute the union of |
| 75 | // external references reached by each package-level declaration. |
| 76 | // The final result is the mapping from each exported package-level |
| 77 | // declaration to the set of external (imported) declarations that it |
| 78 | // reaches. |
| 79 | // |
| 80 | // Because it is common for many package members to have the same |
| 81 | // reachability, the result takes the form of a set of equivalence |
| 82 | // classes, each mapping a set of package-level declarations to a set |
| 83 | // of external symbols. We use a hash table to canonicalize sets so that |
| 84 | // repeated occurrences of the same set (which are common) are only |
| 85 | // represented once in memory or in the file system. |
| 86 | // For example, all declarations that ultimately reference only |
| 87 | // {fmt.Println,strings.Join} would be classed as equivalent. |
| 88 | // |
| 89 | // This approach was inspired by the Hash-Value Numbering (HVN) |
| 90 | // optimization described by Hardekopf and Lin. See |
| 91 | // golang.org/x/tools/go/pointer/hvn.go for an implementation. (Like |
| 92 | // pointer analysis, this problem is fundamentally one of graph |
| 93 | // reachability.) The HVN algorithm takes the compression a step |
| 94 | // further by preserving the topology of the SCC DAG, in which edges |
| 95 | // represent "is a superset of" constraints. Redundant edges that |
| 96 | // don't increase the solution can be deleted. We could apply the same |
| 97 | // technique here to further reduce the worst-case size of the result, |
| 98 | // but the current implementation seems adequate. |
| 99 | // |
| 100 | // # API |
| 101 | // |
| 102 | // The main entry point for this analysis is the [Encode] function, |
| 103 | // which implements the analysis described above for one package, and |
| 104 | // encodes the result as a binary message. |
| 105 | // |
| 106 | // The [Decode] function decodes the message into a usable form: a set |
| 107 | // of equivalence classes. The decoder uses a shared [PackageIndex] to |
| 108 | // enable more compact representations of sets of packages |
| 109 | // ([PackageSet]) during the global reacahability computation. |
| 110 | // |
| 111 | // The [BuildPackageGraph] constructor implements a whole-graph analysis similar |
| 112 | // to that which will be implemented by gopls, but for various reasons the |
| 113 | // logic for this analysis will eventually live in the |
| 114 | // [golang.org/x/tools/gopls/internal/cache] package. Nevertheless, |
| 115 | // BuildPackageGraph and its test serve to verify the syntactic analysis, and |
| 116 | // may serve as a proving ground for new optimizations of the whole-graph analysis. |
| 117 | // |
| 118 | // # Export data is insufficient |
| 119 | // |
| 120 | // At first it may seem that the simplest way to implement this analysis would |
| 121 | // be to consider the types.Packages of the dependencies of P, for example |
| 122 | // during export. After all, it makes sense that the type checked packages |
| 123 | // themselves could describe their dependencies. However, this does not work as |
| 124 | // type information does not describe certain syntactic relationships. |
| 125 | // |
| 126 | // For example, the following scenarios cause type information to miss |
| 127 | // syntactic relationships: |
| 128 | // |
| 129 | // Named type forwarding: |
| 130 | // |
| 131 | // package a; type A b.B |
| 132 | // package b; type B int |
| 133 | // |
| 134 | // Aliases: |
| 135 | // |
| 136 | // package a; func A(f b.B) |
| 137 | // package b; type B = func() |
| 138 | // |
| 139 | // Initializers: |
| 140 | // |
| 141 | // package a; var A = b.B() |
| 142 | // package b; func B() string { return "hi" } |
| 143 | // |
| 144 | // Use of the unsafe package: |
| 145 | // |
| 146 | // package a; type A [unsafe.Sizeof(B{})]int |
| 147 | // package b; type B struct { f1, f2, f3 int } |
| 148 | // |
| 149 | // In all of these examples, types do not contain information about the edge |
| 150 | // between the a.A and b.B declarations. |
| 151 | package typerefs |