Skip to content

Commit f24eac4

Browse files
committed
cmd/compile: improving the documentation of various fields and functions
This is only changes to comments, so should be fine to go into 1.17. Change-Id: I01e28dc76b03fb3ca846d976f8ac84bc2acb2ea2 Reviewed-on: https://go-review.googlesource.com/c/go/+/318009 Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 3980c4d commit f24eac4

File tree

7 files changed

+42
-20
lines changed

7 files changed

+42
-20
lines changed

src/cmd/compile/internal/inline/inl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ const (
5353
inlineBigFunctionMaxCost = 20 // Max cost of inlinee when inlining into a "big" function.
5454
)
5555

56+
// InlinePackage finds functions that can be inlined and clones them before walk expands them.
5657
func InlinePackage() {
57-
// Find functions that can be inlined and clone them before walk expands them.
5858
ir.VisitFuncsBottomUp(typecheck.Target.Decls, func(list []*ir.Func, recursive bool) {
5959
numfns := numNonClosures(list)
6060
for _, n := range list {
@@ -74,8 +74,8 @@ func InlinePackage() {
7474
}
7575

7676
// CanInline determines whether fn is inlineable.
77-
// If so, CanInline saves fn->nbody in fn->inl and substitutes it with a copy.
78-
// fn and ->nbody will already have been typechecked.
77+
// If so, CanInline saves copies of fn.Body and fn.Dcl in fn.Inl.
78+
// fn and fn.Body will already have been typechecked.
7979
func CanInline(fn *ir.Func) {
8080
if fn.Nname == nil {
8181
base.Fatalf("CanInline no nname %+v", fn)
@@ -520,7 +520,7 @@ func inlcopy(n ir.Node) ir.Node {
520520
return edit(n)
521521
}
522522

523-
// Inlcalls/nodelist/node walks fn's statements and expressions and substitutes any
523+
// InlineCalls/inlnode walks fn's statements and expressions and substitutes any
524524
// calls made to inlineable functions. This is the external entry point.
525525
func InlineCalls(fn *ir.Func) {
526526
savefn := ir.CurFunc

src/cmd/compile/internal/ir/func.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ func (f *Func) LinksymABI(abi obj.ABI) *obj.LSym { return f.Nname.LinksymABI(abi
160160
type Inline struct {
161161
Cost int32 // heuristic cost of inlining this function
162162

163-
// Copies of Func.Dcl and Nbody for use during inlining.
163+
// Copies of Func.Dcl and Func.Body for use during inlining. Copies are
164+
// needed because the function's dcl/body may be changed by later compiler
165+
// transformations. These fields are also populated when a function from
166+
// another package is imported.
164167
Dcl []*Name
165168
Body []Node
166169
}

src/cmd/compile/internal/typecheck/func.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ func PartialCallType(n *ir.SelectorExpr) *types.Type {
105105
// typechecking an inline body, as opposed to the body of a real function.
106106
var inTypeCheckInl bool
107107

108-
// Lazy typechecking of imported bodies. For local functions, CanInline will set ->typecheck
109-
// because they're a copy of an already checked body.
108+
// ImportedBody returns immediately if the inlining information for fn is
109+
// populated. Otherwise, fn must be an imported function. If so, ImportedBody
110+
// loads in the dcls and body for fn, and typechecks as needed.
110111
func ImportedBody(fn *ir.Func) {
111112
if fn.Inl.Body != nil {
112113
return
@@ -180,7 +181,7 @@ func fnpkg(fn *ir.Name) *types.Pkg {
180181
return fn.Sym().Pkg
181182
}
182183

183-
// closurename generates a new unique name for a closure within
184+
// ClosureName generates a new unique name for a closure within
184185
// outerfunc.
185186
func ClosureName(outerfunc *ir.Func) *types.Sym {
186187
outer := "glob."

src/cmd/compile/internal/typecheck/iimport.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ var (
4242
inlineImporter = map[*types.Sym]iimporterAndOffset{}
4343
)
4444

45+
// expandDecl returns immediately if n is already a Name node. Otherwise, n should
46+
// be an Ident node, and expandDecl reads in the definition of the specified
47+
// identifier from the appropriate package.
4548
func expandDecl(n ir.Node) ir.Node {
4649
if n, ok := n.(*ir.Name); ok {
4750
return n
@@ -61,6 +64,8 @@ func expandDecl(n ir.Node) ir.Node {
6164
return r.doDecl(n.Sym())
6265
}
6366

67+
// ImportBody reads in the dcls and body of an imported function (which should not
68+
// yet have been read in).
6469
func ImportBody(fn *ir.Func) {
6570
if fn.Inl.Body != nil {
6671
base.Fatalf("%v already has inline body", fn)

src/cmd/compile/internal/typecheck/typecheck.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func FuncBody(n *ir.Func) {
6666

6767
var importlist []*ir.Func
6868

69+
// AllImportedBodies reads in the bodies of all imported functions and typechecks
70+
// them, if needed.
6971
func AllImportedBodies() {
7072
for _, n := range importlist {
7173
if n.Inl != nil {

src/cmd/compile/internal/types/sym.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ type Sym struct {
3232
Pkg *Pkg
3333
Name string // object name
3434

35-
// saved and restored by Pushdcl/Popdcl
36-
Def Object // definition: ONAME OTYPE OPACK or OLITERAL
35+
// Def, Block, and Lastlineno are saved and restored by Pushdcl/Popdcl.
36+
37+
// The unique ONAME, OTYPE, OPACK, or OLITERAL node that this symbol is
38+
// bound to within the current scope. (Most parts of the compiler should
39+
// prefer passing the Node directly, rather than relying on this field.)
40+
Def Object
3741
Block int32 // blocknumber to catch redeclaration
3842
Lastlineno src.XPos // last declaration for diagnostic
3943

src/cmd/compile/internal/types/type.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"sync"
1212
)
1313

14-
// IRNode represents an ir.Node, but without needing to import cmd/compile/internal/ir,
14+
// Object represents an ir.Node, but without needing to import cmd/compile/internal/ir,
1515
// which would cause an import cycle. The uses in other packages must type assert
16-
// values of type IRNode to ir.Node or a more specific type.
16+
// values of type Object to ir.Node or a more specific type.
1717
type Object interface {
1818
Pos() src.XPos
1919
Sym() *Sym
@@ -157,12 +157,15 @@ type Type struct {
157157
// Width is the width of this Type in bytes.
158158
Width int64 // valid if Align > 0
159159

160-
methods Fields
160+
// list of base methods (excluding embedding)
161+
methods Fields
162+
// list of all methods (including embedding)
161163
allMethods Fields
162164

163165
// canonical OTYPE node for a named type (should be an ir.Name node with same sym)
164-
nod Object
165-
underlying *Type // original type (type literal or predefined type)
166+
nod Object
167+
// the underlying type (type literal or predeclared type) for a defined type
168+
underlying *Type
166169

167170
// Cache of composite types, with this type being the element type.
168171
cache struct {
@@ -423,8 +426,11 @@ type Slice struct {
423426
Elem *Type // element type
424427
}
425428

426-
// A Field represents a field in a struct or a method in an interface or
427-
// associated with a named type.
429+
// A Field is a (Sym, Type) pairing along with some other information, and,
430+
// depending on the context, is used to represent:
431+
// - a field in a struct
432+
// - a method in an interface or associated with a named type
433+
// - a function parameter
428434
type Field struct {
429435
flags bitset8
430436

@@ -1656,9 +1662,10 @@ var (
16561662
)
16571663

16581664
// NewNamed returns a new named type for the given type name. obj should be an
1659-
// ir.Name. The new type is incomplete, and the underlying type should be set
1660-
// later via SetUnderlying(). References to the type are maintained until the type
1661-
// is filled in, so those references can be updated when the type is complete.
1665+
// ir.Name. The new type is incomplete (marked as TFORW kind), and the underlying
1666+
// type should be set later via SetUnderlying(). References to the type are
1667+
// maintained until the type is filled in, so those references can be updated when
1668+
// the type is complete.
16621669
func NewNamed(obj Object) *Type {
16631670
t := New(TFORW)
16641671
t.sym = obj.Sym()

0 commit comments

Comments
 (0)