Commit 9f26b9b9 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: eliminate iota_

Change-Id: Iad9c1961aedcc754ad2f6010a49f94c5a0a4bfee
Reviewed-on: https://go-review.googlesource.com/32487
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJosh Bleecher Snyder <josharian@gmail.com>
parent 986768de
...@@ -288,7 +288,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node { ...@@ -288,7 +288,7 @@ func variter(vl []*Node, t *Node, el []*Node) []*Node {
// declare constants from grammar // declare constants from grammar
// new_name_list [[type] = expr_list] // new_name_list [[type] = expr_list]
func constiter(vl []*Node, t *Node, cl []*Node) []*Node { func constiter(vl []*Node, t *Node, cl []*Node, iotaVal int64) []*Node {
var lno src.XPos // default is to leave line number alone in listtreecopy var lno src.XPos // default is to leave line number alone in listtreecopy
if len(cl) == 0 { if len(cl) == 0 {
if t != nil { if t != nil {
...@@ -301,31 +301,29 @@ func constiter(vl []*Node, t *Node, cl []*Node) []*Node { ...@@ -301,31 +301,29 @@ func constiter(vl []*Node, t *Node, cl []*Node) []*Node {
lastconst = cl lastconst = cl
lasttype = t lasttype = t
} }
clcopy := listtreecopy(cl, lno)
var vv []*Node var vv []*Node
for _, v := range vl { for i, v := range vl {
if len(clcopy) == 0 { if i >= len(cl) {
yyerror("missing value in const declaration") yyerror("missing value in const declaration")
break break
} }
c := clcopy[0] c := treecopy(cl[i], lno)
clcopy = clcopy[1:]
v.Op = OLITERAL v.Op = OLITERAL
declare(v, dclcontext) declare(v, dclcontext)
v.Name.Param.Ntype = t v.Name.Param.Ntype = t
v.Name.Defn = c v.Name.Defn = c
v.SetIota(iotaVal)
vv = append(vv, nod(ODCLCONST, v, nil)) vv = append(vv, nod(ODCLCONST, v, nil))
} }
if len(clcopy) != 0 { if len(cl) > len(vl) {
yyerror("extra expression in const declaration") yyerror("extra expression in const declaration")
} }
iota_ += 1
return vv return vv
} }
...@@ -401,9 +399,7 @@ func oldname(s *Sym) *Node { ...@@ -401,9 +399,7 @@ func oldname(s *Sym) *Node {
// Maybe a top-level declaration will come along later to // Maybe a top-level declaration will come along later to
// define s. resolve will check s.Def again once all input // define s. resolve will check s.Def again once all input
// source has been processed. // source has been processed.
n = newnoname(s) return newnoname(s)
n.SetIota(iota_) // save current iota value in const declarations
return n
} }
if Curfn != nil && n.Op == ONAME && n.Name.Funcdepth > 0 && n.Name.Funcdepth != funcdepth { if Curfn != nil && n.Op == ONAME && n.Name.Funcdepth > 0 && n.Name.Funcdepth != funcdepth {
......
...@@ -223,8 +223,6 @@ var dclcontext Class // PEXTERN/PAUTO ...@@ -223,8 +223,6 @@ var dclcontext Class // PEXTERN/PAUTO
var statuniqgen int // name generator for static temps var statuniqgen int // name generator for static temps
var iota_ int64
var lastconst []*Node var lastconst []*Node
var lasttype *Node var lasttype *Node
......
...@@ -679,7 +679,6 @@ func findpkg(name string) (file string, ok bool) { ...@@ -679,7 +679,6 @@ func findpkg(name string) (file string, ok bool) {
// but does not make them visible to user code. // but does not make them visible to user code.
func loadsys() { func loadsys() {
block = 1 block = 1
iota_ = -1000000
importpkg = Runtimepkg importpkg = Runtimepkg
typecheckok = true typecheckok = true
......
...@@ -85,7 +85,6 @@ type linkname struct { ...@@ -85,7 +85,6 @@ type linkname struct {
func (p *noder) node() { func (p *noder) node() {
block = 1 block = 1
iota_ = -1000000
imported_unsafe = false imported_unsafe = false
p.lineno(p.file.PkgName) p.lineno(p.file.PkgName)
...@@ -134,22 +133,18 @@ func (p *noder) decls(decls []syntax.Decl) (l []*Node) { ...@@ -134,22 +133,18 @@ func (p *noder) decls(decls []syntax.Decl) (l []*Node) {
case *syntax.ConstDecl: case *syntax.ConstDecl:
// Tricky to handle golang.org/issue/15550 correctly. // Tricky to handle golang.org/issue/15550 correctly.
prevIota := iota_
if decl.Group == nil || decl.Group != lastConstGroup { if decl.Group == nil || decl.Group != lastConstGroup {
iotaVal = 0 iotaVal = 0
lastConstRHS = nil lastConstRHS = nil
} }
iota_ = iotaVal
lastconst = lastConstRHS lastconst = lastConstRHS
l = append(l, p.constDecl(decl)...) l = append(l, p.constDecl(decl, iotaVal)...)
lastConstRHS = lastconst lastConstRHS = lastconst
lastconst = nil lastconst = nil
iota_ = prevIota
iotaVal++ iotaVal++
lastConstGroup = decl.Group lastConstGroup = decl.Group
...@@ -227,7 +222,7 @@ func (p *noder) varDecl(decl *syntax.VarDecl) []*Node { ...@@ -227,7 +222,7 @@ func (p *noder) varDecl(decl *syntax.VarDecl) []*Node {
return variter(names, typ, exprs) return variter(names, typ, exprs)
} }
func (p *noder) constDecl(decl *syntax.ConstDecl) []*Node { func (p *noder) constDecl(decl *syntax.ConstDecl, iotaVal int64) []*Node {
names := p.declNames(decl.NameList) names := p.declNames(decl.NameList)
typ := p.typeExprOrNil(decl.Type) typ := p.typeExprOrNil(decl.Type)
...@@ -236,7 +231,7 @@ func (p *noder) constDecl(decl *syntax.ConstDecl) []*Node { ...@@ -236,7 +231,7 @@ func (p *noder) constDecl(decl *syntax.ConstDecl) []*Node {
exprs = p.exprList(decl.Values) exprs = p.exprList(decl.Values)
} }
return constiter(names, typ, exprs) return constiter(names, typ, exprs, iotaVal)
} }
func (p *noder) typeDecl(decl *syntax.TypeDecl) *Node { func (p *noder) typeDecl(decl *syntax.TypeDecl) *Node {
......
...@@ -476,28 +476,13 @@ func treecopy(n *Node, pos src.XPos) *Node { ...@@ -476,28 +476,13 @@ func treecopy(n *Node, pos src.XPos) *Node {
} }
return &m return &m
case ONONAME:
if n.Sym == lookup("iota") {
// Not sure yet whether this is the real iota,
// but make a copy of the Node* just in case,
// so that all the copies of this const definition
// don't have the same iota value.
m := *n
if pos.IsKnown() {
m.Pos = pos
}
m.SetIota(iota_)
return &m
}
return n
case OPACK: case OPACK:
// OPACK nodes are never valid in const value declarations, // OPACK nodes are never valid in const value declarations,
// but allow them like any other declared symbol to avoid // but allow them like any other declared symbol to avoid
// crashing (golang.org/issue/11361). // crashing (golang.org/issue/11361).
fallthrough fallthrough
case ONAME, OLITERAL, OTYPE: case ONAME, ONONAME, OLITERAL, OTYPE:
return n return n
} }
......
...@@ -43,7 +43,7 @@ type Node struct { ...@@ -43,7 +43,7 @@ type Node struct {
// - ODOT, ODOTPTR, and OINDREGSP use it to indicate offset relative to their base address. // - ODOT, ODOTPTR, and OINDREGSP use it to indicate offset relative to their base address.
// - OSTRUCTKEY uses it to store the named field's offset. // - OSTRUCTKEY uses it to store the named field's offset.
// - OXCASE and OXFALL use it to validate the use of fallthrough. // - OXCASE and OXFALL use it to validate the use of fallthrough.
// - ONONAME uses it to store the current value of iota, see Node.Iota // - Named OLITERALs use it to to store their ambient iota value.
// Possibly still more uses. If you find any, document them. // Possibly still more uses. If you find any, document them.
Xoffset int64 Xoffset int64
......
...@@ -36,8 +36,11 @@ func resolve(n *Node) *Node { ...@@ -36,8 +36,11 @@ func resolve(n *Node) *Node {
if r != nil { if r != nil {
if r.Op != OIOTA { if r.Op != OIOTA {
n = r n = r
} else if n.Iota() >= 0 { } else if len(typecheckdefstack) > 0 {
n = nodintconst(n.Iota()) x := typecheckdefstack[len(typecheckdefstack)-1]
if x.Op == OLITERAL {
n = nodintconst(x.Iota())
}
} }
} }
} }
......
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