Skip to content

Commit 4f63e0a

Browse files
committed
cmd/compile: update comments only for Node types and some functions
Improve the comments in syntax.go on Node structs and constants. Also, updated a few function header comments. Change-Id: I3e6e4a3c5678fc0b4e18844507b3460303ce1240 Reviewed-on: https://go-review.googlesource.com/c/go/+/269538 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Trust: Dan Scales <danscales@google.com>
1 parent 86954d5 commit 4f63e0a

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

src/cmd/compile/internal/gc/closure.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (p *noder) funcLit(expr *syntax.FuncLit) *Node {
7171
return clo
7272
}
7373

74+
// typecheckclosure typechecks an OCLOSURE node. It also creates the named
75+
// function associated with the closure.
76+
// TODO: This creation of the named function should probably really be done in a
77+
// separate pass from type-checking.
7478
func typecheckclosure(clo *Node, top int) {
7579
xfunc := clo.Func.Closure
7680
// Set current associated iota value, so iota can be used inside

src/cmd/compile/internal/gc/dcl.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ func symfield(s *types.Sym, typ *types.Type) *Node {
257257

258258
// oldname returns the Node that declares symbol s in the current scope.
259259
// If no such Node currently exists, an ONONAME Node is returned instead.
260+
// Automatically creates a new closure variable if the referenced symbol was
261+
// declared in a different (containing) function.
260262
func oldname(s *types.Sym) *Node {
261263
n := asNode(s.Def)
262264
if n == nil {

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ func typecheckinl(fn *Node) {
9494
typecheckslice(fn.Func.Inl.Body, ctxStmt)
9595
Curfn = savefn
9696

97-
// During typechecking, declarations are added to
98-
// Curfn.Func.Dcl. Move them to Inl.Dcl for consistency with
99-
// how local functions behave. (Append because typecheckinl
100-
// may be called multiple times.)
97+
// During expandInline (which imports fn.Func.Inl.Body),
98+
// declarations are added to fn.Func.Dcl by funcHdr(). Move them
99+
// to fn.Func.Inl.Dcl for consistency with how local functions
100+
// behave. (Append because typecheckinl may be called multiple
101+
// times.)
101102
fn.Func.Inl.Dcl = append(fn.Func.Inl.Dcl, fn.Func.Dcl...)
102103
fn.Func.Dcl = nil
103104

@@ -448,9 +449,9 @@ func (v *hairyVisitor) visit(n *Node) bool {
448449
v.visitList(n.Ninit) || v.visitList(n.Nbody)
449450
}
450451

451-
// Inlcopy and inlcopylist recursively copy the body of a function.
452-
// Any name-like node of non-local class is marked for re-export by adding it to
453-
// the exportlist.
452+
// inlcopylist (together with inlcopy) recursively copies a list of nodes, except
453+
// that it keeps the same ONAME, OTYPE, and OLITERAL nodes. It is used for copying
454+
// the body and dcls of an inlineable function.
454455
func inlcopylist(ll []*Node) []*Node {
455456
s := make([]*Node, 0, len(ll))
456457
for _, n := range ll {
@@ -889,10 +890,10 @@ func inlParam(t *types.Field, as *Node, inlvars map[*Node]*Node) *Node {
889890

890891
var inlgen int
891892

892-
// If n is a call, and fn is a function with an inlinable body,
893-
// return an OINLCALL.
894-
// On return ninit has the parameter assignments, the nbody is the
895-
// inlined function body and list, rlist contain the input, output
893+
// If n is a call node (OCALLFUNC or OCALLMETH), and fn is an ONAME node for a
894+
// function with an inlinable body, return an OINLCALL node that can replace n.
895+
// The returned node's Ninit has the parameter assignments, the Nbody is the
896+
// inlined function body, and (List, Rlist) contain the (input, output)
896897
// parameters.
897898
// The result of mkinlcall MUST be assigned back to n, e.g.
898899
// n.Left = mkinlcall(n.Left, fn, isddd)

src/cmd/compile/internal/gc/syntax.go

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,22 @@ func (n *Node) CanBeAnSSASym() {
344344

345345
// Name holds Node fields used only by named nodes (ONAME, OTYPE, OPACK, OLABEL, some OLITERAL).
346346
type Name struct {
347-
Pack *Node // real package for import . names
348-
Pkg *types.Pkg // pkg for OPACK nodes
349-
Defn *Node // initializing assignment
350-
Curfn *Node // function for local variables
351-
Param *Param // additional fields for ONAME, OTYPE
352-
Decldepth int32 // declaration loop depth, increased for every loop or label
353-
Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one.
354-
flags bitset16
347+
Pack *Node // real package for import . names
348+
Pkg *types.Pkg // pkg for OPACK nodes
349+
// For a local variable (not param) or extern, the initializing assignment (OAS or OAS2).
350+
// For a closure var, the ONAME node of the outer captured variable
351+
Defn *Node
352+
// The ODCLFUNC node (for a static function/method or a closure) in which
353+
// local variable or param is declared.
354+
Curfn *Node
355+
Param *Param // additional fields for ONAME, OTYPE
356+
Decldepth int32 // declaration loop depth, increased for every loop or label
357+
// Unique number for ONAME nodes within a function. Function outputs
358+
// (results) are numbered starting at one, followed by function inputs
359+
// (parameters), and then local variables. Vargen is used to distinguish
360+
// local variables/params with the same name.
361+
Vargen int32
362+
flags bitset16
355363
}
356364

357365
const (
@@ -608,10 +616,16 @@ func (p *Param) SetEmbedFiles(list []string) {
608616
// Func holds Node fields used only with function-like nodes.
609617
type Func struct {
610618
Shortname *types.Sym
611-
Enter Nodes // for example, allocate and initialize memory for escaping parameters
612-
Exit Nodes
613-
Cvars Nodes // closure params
614-
Dcl []*Node // autodcl for this func/closure
619+
// Extra entry code for the function. For example, allocate and initialize
620+
// memory for escaping parameters. However, just for OCLOSURE, Enter is a
621+
// list of ONAME nodes of captured variables
622+
Enter Nodes
623+
Exit Nodes
624+
// ONAME nodes for closure params, each should have closurevar set
625+
Cvars Nodes
626+
// ONAME nodes for all params/locals for this func/closure, does NOT
627+
// include closurevars until transformclosure runs.
628+
Dcl []*Node
615629

616630
// Parents records the parent scope of each scope within a
617631
// function. The root scope (0) has no parent, so the i'th
@@ -630,7 +644,7 @@ type Func struct {
630644
DebugInfo *ssa.FuncDebug
631645
Ntype *Node // signature
632646
Top int // top context (ctxCallee, etc)
633-
Closure *Node // OCLOSURE <-> ODCLFUNC
647+
Closure *Node // OCLOSURE <-> ODCLFUNC (see header comment above)
634648
Nname *Node // The ONAME node associated with an ODCLFUNC (both have same Type)
635649
lsym *obj.LSym
636650

@@ -680,6 +694,8 @@ const (
680694
funcWrapper // is method wrapper
681695
funcNeedctxt // function uses context register (has closure variables)
682696
funcReflectMethod // function calls reflect.Type.Method or MethodByName
697+
// true if closure inside a function; false if a simple function or a
698+
// closure in a global variable initialization
683699
funcIsHiddenClosure
684700
funcHasDefer // contains a defer statement
685701
funcNilCheckDisabled // disable nil checks when compiling this function
@@ -731,8 +747,10 @@ const (
731747
OXXX Op = iota
732748

733749
// names
734-
ONAME // var or func name
735-
ONONAME // unnamed arg or return value: f(int, string) (int, error) { etc }
750+
ONAME // var or func name
751+
// Unnamed arg or return value: f(int, string) (int, error) { etc }
752+
// Also used for a qualified package identifier that hasn't been resolved yet.
753+
ONONAME
736754
OTYPE // type name
737755
OPACK // import
738756
OLITERAL // literal
@@ -752,14 +770,18 @@ const (
752770
OSTR2BYTES // Type(Left) (Type is []byte, Left is a string)
753771
OSTR2BYTESTMP // Type(Left) (Type is []byte, Left is a string, ephemeral)
754772
OSTR2RUNES // Type(Left) (Type is []rune, Left is a string)
755-
OAS // Left = Right or (if Colas=true) Left := Right
756-
OAS2 // List = Rlist (x, y, z = a, b, c)
757-
OAS2DOTTYPE // List = Right (x, ok = I.(int))
758-
OAS2FUNC // List = Right (x, y = f())
759-
OAS2MAPR // List = Right (x, ok = m["foo"])
760-
OAS2RECV // List = Right (x, ok = <-c)
761-
OASOP // Left Etype= Right (x += y)
762-
OCALL // Left(List) (function call, method call or type conversion)
773+
// Left = Right or (if Colas=true) Left := Right
774+
// If Colas, then Ninit includes a DCL node for Left.
775+
OAS
776+
// List = Rlist (x, y, z = a, b, c) or (if Colas=true) List := Rlist
777+
// If Colas, then Ninit includes DCL nodes for List
778+
OAS2
779+
OAS2DOTTYPE // List = Right (x, ok = I.(int))
780+
OAS2FUNC // List = Right (x, y = f())
781+
OAS2MAPR // List = Right (x, ok = m["foo"])
782+
OAS2RECV // List = Right (x, ok = <-c)
783+
OASOP // Left Etype= Right (x += y)
784+
OCALL // Left(List) (function call, method call or type conversion)
763785

764786
// OCALLFUNC, OCALLMETH, and OCALLINTER have the same structure.
765787
// Prior to walk, they are: Left(List), where List is all regular arguments.

0 commit comments

Comments
 (0)