package inline

import "golang.org/x/tools/internal/refactor/inline"

Package inline implements inlining of Go function calls.

The client provides information about the caller and callee, including the source text, syntax tree, and type information, and the inliner returns the modified source file for the caller, or an error if the inlining operation is invalid (for example because the function body refers to names that are inaccessible to the caller).

Although this interface demands more information from the client than might seem necessary, it enables smoother integration with existing batch and interactive tools that have their own ways of managing the processes of reading, parsing, and type-checking packages. In particular, this package does not assume that the caller and callee belong to the same token.FileSet or types.Importer realms.

In general, inlining consists of modifying a function or method call expression f(a1, ..., an) so that the name of the function f is replaced ("literalized") by a literal copy of the function declaration, with free identifiers suitably modified to use the locally appropriate identifiers or perhaps constant argument values.

Inlining must not change the semantics of the call. Semantics preservation is crucial for clients such as codebase maintenance tools that automatically inline all calls to designated functions on a large scale. Such tools must not introduce subtle behavior changes. (Fully inlining a call is dynamically observable using reflection over the call stack, but this exception to the rule is explicitly allowed.)

In some special cases it is possible to entirely replace ("reduce") the call by a copy of the function's body in which parameters have been replaced by arguments, but this is surprisingly tricky for a number of reasons, some of which are listed here for illustration:

More complex callee functions are inlinable with more elaborate and invasive changes to the statements surrounding the call expression.

TODO(adonovan): future work:

Index

Functions

func Inline

func Inline(caller *Caller, callee_ *Callee) ([]byte, error)

Inline inlines the called function (callee) into the function call (caller) and returns the updated, formatted content of the caller source file.

Types

type Callee

type Callee struct {
	// contains filtered or unexported fields
}

A Callee holds information about an inlinable function. Gob-serializable.

func AnalyzeCallee

func AnalyzeCallee(fset *token.FileSet, pkg *types.Package, info *types.Info, decl *ast.FuncDecl, content []byte) (*Callee, error)

AnalyzeCallee analyzes a function that is a candidate for inlining and returns a Callee that describes it. The Callee object, which is serializable, can be passed to one or more subsequent calls to Inline, each with a different Caller.

This design allows separate analysis of callers and callees in the golang.org/x/tools/go/analysis framework: the inlining information about a callee can be recorded as a "fact".

func (*Callee) GobDecode

func (callee *Callee) GobDecode(data []byte) error

func (*Callee) GobEncode

func (callee *Callee) GobEncode() ([]byte, error)

func (*Callee) String

func (callee *Callee) String() string

type Caller

type Caller struct {
	Fset    *token.FileSet
	Types   *types.Package
	Info    *types.Info
	File    *ast.File
	Call    *ast.CallExpr
	Content []byte
}

A Caller describes the function call and its enclosing context.

The client is responsible for populating this struct and passing it to Inline.

Source Files

callee.go inline.go

Directories

PathSynopsis
internal/refactor/inline/analyzer
Version
v0.13.0
Published
Sep 5, 2023
Platform
darwin/amd64
Imports
16 packages
Last checked
4 minutes ago

Tools for package owners.