From eca90561c39d4d8cc587fbcc70baa9d90f3f0707 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 30 Mar 2017 07:38:46 -0700 Subject: [PATCH] cmd/compile: minor init handling cleanup Place comments correctly. Simplify control flow. Reduce variable scope. Passes toolstash-check. Change-Id: Iea47ed3502c15491c2ca6db8149fe0949b8849aa Reviewed-on: https://go-review.googlesource.com/38914 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/cmd/compile/internal/gc/init.go | 48 +++++++++++++---------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/cmd/compile/internal/gc/init.go b/src/cmd/compile/internal/gc/init.go index 0ebbffd83a..7ce8383dbc 100644 --- a/src/cmd/compile/internal/gc/init.go +++ b/src/cmd/compile/internal/gc/init.go @@ -17,35 +17,15 @@ func renameinit() *Sym { return lookupN("init.", renameinit_initgen) } -// hand-craft the following initialization code -// var initdone· uint8 (1) -// func init() { (2) -// if initdone· > 1 { (3) -// return (3a) -// } -// if initdone· == 1 { (4) -// throw() (4a) -// } -// initdone· = 1 (5) -// // over all matching imported symbols -// .init() (6) -// { } (7) -// init.() // if any (8) -// initdone· = 2 (9) -// return (10) -// } +// anyinit reports whether there any interesting init statements. func anyinit(n []*Node) bool { - // are there any interesting init statements for _, ln := range n { switch ln.Op { case ODCLFUNC, ODCLCONST, ODCLTYPE, OEMPTY: - break - case OAS: - if isblank(ln.Left) && candiscard(ln.Right) { - break + if !isblank(ln.Left) || !candiscard(ln.Right) { + return true } - fallthrough default: return true } @@ -57,9 +37,7 @@ func anyinit(n []*Node) bool { } // is there an explicit init function - s := lookup("init.1") - - if s.Def != nil { + if s := lookup("init.1"); s.Def != nil { return true } @@ -74,6 +52,24 @@ func anyinit(n []*Node) bool { return false } +// fninit hand-crafts package initialization code. +// +// var initdone· uint8 (1) +// func init() { (2) +// if initdone· > 1 { (3) +// return (3a) +// } +// if initdone· == 1 { (4) +// throw() (4a) +// } +// initdone· = 1 (5) +// // over all matching imported symbols +// .init() (6) +// { } (7) +// init.() // if any (8) +// initdone· = 2 (9) +// return (10) +// } func fninit(n []*Node) { lineno = autogeneratedPos nf := initfix(n) -- 2.48.1