| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"go/ast" |
| 11 |
"go/parser" |
| 12 |
"go/types" |
| 13 |
|
| 14 |
"golang.org/x/tools/go/ast/astutil" |
| 15 |
"golang.org/x/tools/go/types/typeutil" |
| 16 |
"golang.org/x/tools/gopls/internal/cache" |
| 17 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 20 |
"golang.org/x/tools/internal/analysis/driverutil" |
| 21 |
"golang.org/x/tools/internal/diff" |
| 22 |
"golang.org/x/tools/internal/refactor" |
| 23 |
"golang.org/x/tools/internal/refactor/inline" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
func inlineAllCalls(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, origDecl *ast.FuncDecl, callee *inline.Callee, post func([]byte) []byte, opts *inline.Options) (_ map[protocol.DocumentURI][]byte, inlineErr error) { |
| 51 |
|
| 52 |
var refs []protocol.Location |
| 53 |
{ |
| 54 |
funcRng, err := pgf.Mapper.PosRange(pgf.Tok, origDecl.Name.NamePos, origDecl.Name.NamePos) |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
fh, err := snapshot.ReadFile(ctx, pgf.URI) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
refs, err = References(ctx, snapshot, fh, funcRng, false) |
| 63 |
if err != nil { |
| 64 |
return nil, fmt.Errorf("finding references to rewrite: %v", err) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
var ( |
| 72 |
pkgForRef = make(map[protocol.Location]PackageID) |
| 73 |
pkgs = make(map[PackageID]*cache.Package) |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
badPkg = false |
| 79 |
) |
| 80 |
{ |
| 81 |
needPkgs := make(map[PackageID]struct{}) |
| 82 |
for _, ref := range refs { |
| 83 |
md, err := snapshot.NarrowestMetadataForFile(ctx, ref.URI) |
| 84 |
if err != nil { |
| 85 |
return nil, fmt.Errorf("finding ref metadata: %v", err) |
| 86 |
} |
| 87 |
pkgForRef[ref] = md.ID |
| 88 |
needPkgs[md.ID] = struct{}{} |
| 89 |
} |
| 90 |
var pkgIDs []PackageID |
| 91 |
for id := range needPkgs { |
| 92 |
pkgIDs = append(pkgIDs, id) |
| 93 |
} |
| 94 |
|
| 95 |
refPkgs, err := snapshot.TypeCheck(ctx, pkgIDs...) |
| 96 |
if err != nil { |
| 97 |
return nil, fmt.Errorf("type checking reference packages: %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
for _, p := range refPkgs { |
| 101 |
pkgs[p.Metadata().ID] = p |
| 102 |
if len(p.ParseErrors())+len(p.TypeErrors()) > 0 { |
| 103 |
badPkg = true |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if badPkg { |
| 109 |
defer func() { |
| 110 |
if x := recover(); x != nil { |
| 111 |
inlineErr = fmt.Errorf("inlining failed (%q), likely because inputs were ill-typed", x) |
| 112 |
} |
| 113 |
}() |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
type fileCalls struct { |
| 122 |
pkg *cache.Package |
| 123 |
pgf *parsego.File |
| 124 |
calls []*ast.CallExpr |
| 125 |
} |
| 126 |
|
| 127 |
refsByFile := make(map[protocol.DocumentURI]*fileCalls) |
| 128 |
for _, ref := range refs { |
| 129 |
refpkg := pkgs[pkgForRef[ref]] |
| 130 |
pgf, err := refpkg.File(ref.URI) |
| 131 |
if err != nil { |
| 132 |
return nil, bug.Errorf("finding %s in %s: %v", ref.URI, refpkg.Metadata().ID, err) |
| 133 |
} |
| 134 |
|
| 135 |
start, end, err := pgf.RangePos(ref.Range) |
| 136 |
if err != nil { |
| 137 |
return nil, err |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
var ( |
| 142 |
name *ast.Ident |
| 143 |
call *ast.CallExpr |
| 144 |
) |
| 145 |
path, _ := astutil.PathEnclosingInterval(pgf.File, start, end) |
| 146 |
name, _ = path[0].(*ast.Ident) |
| 147 |
|
| 148 |
|
| 149 |
if _, ok := path[1].(*ast.SelectorExpr); ok { |
| 150 |
call, _ = path[2].(*ast.CallExpr) |
| 151 |
} else { |
| 152 |
call, _ = path[1].(*ast.CallExpr) |
| 153 |
} |
| 154 |
if name == nil || call == nil { |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
return nil, fmt.Errorf("cannot inline: found non-call function reference %v", ref) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
hasTypeErrors := false |
| 166 |
for _, typeErr := range refpkg.TypeErrors() { |
| 167 |
if call.Lparen <= typeErr.Pos && typeErr.Pos <= call.Rparen { |
| 168 |
hasTypeErrors = true |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if hasTypeErrors { |
| 173 |
continue |
| 174 |
} |
| 175 |
|
| 176 |
if typeutil.StaticCallee(refpkg.TypesInfo(), call) == nil { |
| 177 |
continue |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if obj := refpkg.TypesInfo().ObjectOf(name); obj == nil || |
| 182 |
obj.Name() != origDecl.Name.Name || |
| 183 |
obj.Pkg() == nil || |
| 184 |
obj.Pkg().Path() != string(pkg.Metadata().PkgPath) { |
| 185 |
|
| 186 |
return nil, bug.Errorf("cannot inline: corrupted reference %v", ref) |
| 187 |
} |
| 188 |
|
| 189 |
callInfo, ok := refsByFile[ref.URI] |
| 190 |
if !ok { |
| 191 |
callInfo = &fileCalls{ |
| 192 |
pkg: refpkg, |
| 193 |
pgf: pgf, |
| 194 |
} |
| 195 |
refsByFile[ref.URI] = callInfo |
| 196 |
} |
| 197 |
callInfo.calls = append(callInfo.calls, call) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
result := make(map[protocol.DocumentURI][]byte) |
| 207 |
for uri, callInfo := range refsByFile { |
| 208 |
var ( |
| 209 |
calls = callInfo.calls |
| 210 |
fset = callInfo.pkg.FileSet() |
| 211 |
tpkg = callInfo.pkg.Types() |
| 212 |
tinfo = callInfo.pkg.TypesInfo() |
| 213 |
file = callInfo.pgf.File |
| 214 |
content = callInfo.pgf.Src |
| 215 |
) |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
for i := range calls { |
| 222 |
if i > 0 && calls[i-1].End() > calls[i].Pos() { |
| 223 |
return nil, fmt.Errorf("%s: can't inline overlapping call %s", uri, types.ExprString(calls[i-1])) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
currentCall := 0 |
| 228 |
for currentCall < len(calls) { |
| 229 |
caller := &inline.Caller{ |
| 230 |
Fset: fset, |
| 231 |
Types: tpkg, |
| 232 |
Info: tinfo, |
| 233 |
File: file, |
| 234 |
Call: calls[currentCall], |
| 235 |
CountUses: nil, |
| 236 |
} |
| 237 |
res, err := inline.Inline(caller, callee, opts) |
| 238 |
if err != nil { |
| 239 |
return nil, fmt.Errorf("inlining failed: %v", err) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
applyEdits := func(content []byte, edits []refactor.Edit) ([]byte, error) { |
| 247 |
dedits := make([]diff.Edit, len(edits)) |
| 248 |
for i, edit := range edits { |
| 249 |
dedits[i] = diff.Edit{ |
| 250 |
Start: int(edit.Pos - file.FileStart), |
| 251 |
End: int(edit.End - file.FileStart), |
| 252 |
New: string(edit.NewText), |
| 253 |
} |
| 254 |
} |
| 255 |
got, err := diff.ApplyBytes(content, dedits) |
| 256 |
if err != nil { |
| 257 |
return nil, err |
| 258 |
} |
| 259 |
got, err = driverutil.FormatSourceRemoveImports(tpkg, got) |
| 260 |
if err != nil { |
| 261 |
return nil, err |
| 262 |
} |
| 263 |
return []byte(got), nil |
| 264 |
} |
| 265 |
|
| 266 |
content, err = applyEdits(content, res.Edits) |
| 267 |
if err != nil { |
| 268 |
return nil, fmt.Errorf("applying inliner edits failed: %v", err) |
| 269 |
} |
| 270 |
|
| 271 |
if post != nil { |
| 272 |
content = post(content) |
| 273 |
} |
| 274 |
if len(calls) <= 1 { |
| 275 |
|
| 276 |
break |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
file, err = parser.ParseFile(fset, uri.Path(), content, parser.ParseComments|parser.SkipObjectResolution) |
| 290 |
if err != nil { |
| 291 |
return nil, bug.Errorf("inlined file failed to parse: %v", err) |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
logf := func(string, ...any) {} |
| 304 |
if opts != nil { |
| 305 |
logf = opts.Logf |
| 306 |
} |
| 307 |
tpkg, tinfo, err = reTypeCheck(logf, callInfo.pkg, map[protocol.DocumentURI]*ast.File{uri: file}, true) |
| 308 |
if err != nil { |
| 309 |
return nil, bug.Errorf("type checking after inlining failed: %v", err) |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
var calls2 []*ast.CallExpr |
| 314 |
ast.Inspect(file, func(n ast.Node) bool { |
| 315 |
if call, ok := n.(*ast.CallExpr); ok { |
| 316 |
fn := typeutil.StaticCallee(tinfo, call) |
| 317 |
if fn != nil && fn.Pkg().Path() == string(pkg.Metadata().PkgPath) && fn.Name() == origDecl.Name.Name { |
| 318 |
calls2 = append(calls2, call) |
| 319 |
} |
| 320 |
} |
| 321 |
return true |
| 322 |
}) |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
switch { |
| 337 |
case len(calls2) > len(calls): |
| 338 |
return nil, fmt.Errorf("inlining increased calls %d->%d, possible recursive call? content:\n%s", len(calls), len(calls2), content) |
| 339 |
case len(calls2) < len(calls): |
| 340 |
calls = calls2 |
| 341 |
case len(calls2) == len(calls): |
| 342 |
calls = calls2 |
| 343 |
currentCall++ |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
result[callInfo.pgf.URI] = content |
| 348 |
} |
| 349 |
return result, nil |
| 350 |
} |