1
0
mirror of https://github.com/golang/go synced 2024-11-22 15:14:53 -07:00

cmd/compile: allow inlining of functions that declare a const

Consider functions with an ODCLCONST for inlining and modify exprfmt to
ignore those nodes when exporting. Don't add symbols to the export list
if there is no definition.  This occurs when OLITERAL symbols are looked
up via Pkglookup for non-exported symbols.

Fixes #7655

Change-Id: I1de827850f4c69e58107447314fe7433e378e069
Reviewed-on: https://go-review.googlesource.com/20773
Run-TryBot: Todd Neal <todd@tneal.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Todd Neal 2016-03-16 18:44:17 -05:00 committed by Robert Griesemer
parent 3dd4f74e06
commit fc6bcdee79
6 changed files with 17 additions and 6 deletions

View File

@ -984,7 +984,7 @@ func (p *exporter) node(n *Node) {
case OBREAK, OCONTINUE, OGOTO, OFALL, OXFALL:
p.nodesOrNil(n.Left, nil)
case OEMPTY:
case OEMPTY, ODCLCONST:
// nothing to do
case OLABEL:

View File

@ -751,7 +751,7 @@ func (p *importer) node() *Node {
case OBREAK, OCONTINUE, OGOTO, OFALL, OXFALL:
n.Left, _ = p.nodesOrNil()
case OEMPTY:
case OEMPTY, ODCLCONST:
// nothing to do
case OLABEL:

View File

@ -176,7 +176,7 @@ func reexportdep(n *Node) {
fallthrough
case OTYPE:
if n.Sym != nil && !exportedsym(n.Sym) {
if n.Sym != nil && n.Sym.Def != nil && !exportedsym(n.Sym) {
if Debug['E'] != 0 {
fmt.Printf("reexport literal/type %v\n", n.Sym)
}
@ -331,7 +331,7 @@ func dumpexporttype(t *Type) {
if Debug['l'] < 2 {
typecheckinl(f.Type.Nname)
}
exportf("\tfunc %v %v %v { %v }\n", Tconv(f.Type.Recvs(), FmtSharp), Sconv(f.Sym, FmtShort|FmtByte|FmtSharp), Tconv(f.Type, FmtShort|FmtSharp), Hconv(f.Type.Nname.Func.Inl, FmtSharp))
exportf("\tfunc %v %v %v { %v }\n", Tconv(f.Type.Recvs(), FmtSharp), Sconv(f.Sym, FmtShort|FmtByte|FmtSharp), Tconv(f.Type, FmtShort|FmtSharp), Hconv(f.Type.Nname.Func.Inl, FmtSharp|FmtBody))
reexportdeplist(f.Type.Nname.Func.Inl)
} else {
exportf("\tfunc %v %v %v\n", Tconv(f.Type.Recvs(), FmtSharp), Sconv(f.Sym, FmtShort|FmtByte|FmtSharp), Tconv(f.Type, FmtShort|FmtSharp))

View File

@ -1413,6 +1413,13 @@ func exprfmt(n *Node, prec int) string {
f += fmt.Sprintf(" %v ", Oconv(Op(n.Etype), FmtSharp))
f += exprfmt(n.Right, nprec+1)
return f
case ODCLCONST:
// if exporting, DCLCONST should just be removed as its usage
// has already been replaced with literals
if fmtbody {
return ""
}
}
return fmt.Sprintf("<node %v>", Oconv(n.Op, 0))

View File

@ -217,8 +217,7 @@ func ishairy(n *Node, budget *int) bool {
OSWITCH,
OPROC,
ODEFER,
ODCLTYPE, // can't print yet
ODCLCONST, // can't print yet
ODCLTYPE, // can't print yet
ORETJMP:
return true
}

View File

@ -31,3 +31,8 @@ func g(x int) int {
func h(x int) int { // ERROR "can inline h"
return x + 2
}
func i(x int) int { // ERROR "can inline i"
const y = 2
return x + y
}