Commit fc6bcdee authored by Todd Neal's avatar Todd Neal Committed by Robert Griesemer

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: default avatarJosh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 3dd4f74e
......@@ -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:
......
......@@ -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:
......
......@@ -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))
......
......@@ -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))
......
......@@ -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
}
......
......@@ -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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment