]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: document function syntax representation
authorMatthew Dempsky <mdempsky@google.com>
Wed, 25 Oct 2017 20:52:26 +0000 (13:52 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Wed, 25 Oct 2017 22:29:57 +0000 (22:29 +0000)
Not perfect, but it's a start.

Change-Id: I3283385223a39ea73567b0130290bfe4de69d018
Reviewed-on: https://go-review.googlesource.com/73552
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/gc/syntax.go

index 9a07f0ec12ea62a9953e2bfd3ed5733e1d8713f6..c315338c357e3c5101d28692a8444fb84f7cd4dd 100644 (file)
@@ -351,6 +351,48 @@ type Param struct {
        Alias  bool // node is alias for Ntype (only used when type-checking ODCLTYPE)
 }
 
+// Functions
+//
+// A simple function declaration is represented as an ODCLFUNC node f
+// and an ONAME node n. They're linked to one another through
+// f.Func.Nname == n and n.Name.Defn == f. When functions are
+// referenced by name in an expression, the function's ONAME node is
+// used directly.
+//
+// Function names have n.Class() == PFUNC. This distinguishes them
+// from variables of function type.
+//
+// Confusingly, n.Func and f.Func both exist, but commonly point to
+// different Funcs. (Exception: an OCALLPART's Func does point to its
+// ODCLFUNC's Func.)
+//
+// A method declaration is represented like functions, except n.Sym
+// will be the qualified method name (e.g., "T.m") and
+// f.Func.Shortname is the bare method name (e.g., "m").
+//
+// Method expressions are represented as ONAME/PFUNC nodes like
+// function names, but their Left and Right fields still point to the
+// type and method, respectively. They can be distinguished from
+// normal functions with isMethodExpression. Also, unlike function
+// name nodes, method expression nodes exist for each method
+// expression. The declaration ONAME can be accessed with
+// x.Type.Nname(), where x is the method expression ONAME node.
+//
+// Method values are represented by ODOTMETH/ODOTINTER when called
+// immediately, and OCALLPART otherwise. They are like method
+// expressions, except that for ODOTMETH/ODOTINTER the method name is
+// stored in Sym instead of Right.
+//
+// Closures are represented by OCLOSURE node c. They link back and
+// forth with the ODCLFUNC via Func.Closure; that is, c.Func.Closure
+// == f and f.Func.Closure == c.
+//
+// Function bodies are stored in f.Nbody, and inline function bodies
+// are stored in n.Func.Inl. Pragmas are stored in f.Func.Pragma.
+//
+// Imported functions skip the ODCLFUNC, so n.Name.Defn is nil. They
+// also use Dcl instead of Inldcl.
+
 // Func holds Node fields used only with function-like nodes.
 type Func struct {
        Shortname *types.Sym