Commit 5267ac27 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile/internal/syntax: establish principled position information

Until now, the parser set the position for each Node to the position of
the first token belonging to that node. For compatibility with the now
defunct gc parser, in many places that position information was modified
when the gcCompat flag was set (which it was, by default). Furthermore,
in some places, position information was not set at all.

This change removes the gcCompat flag and all associated code, and sets
position information for all nodes in a more principled way, as proposed
by mdempsky (see #16943 for details). Specifically, the position of a
node may not be at the very beginning of the respective production. For
instance for an Operation `a + b`, the position associated with the node
is the position of the `+`. Thus, for `a + b + c` we now get different
positions for the two additions.

This change does not pass toolstash -cmp because position information
recorded in export data and pcline tables is different. There are no
other functional changes.

Added test suite testing the position of all nodes.

Fixes #16943.

Change-Id: I3fc02bf096bc3b3d7d2fa655dfd4714a1a0eb90c
Reviewed-on: https://go-review.googlesource.com/37017
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent 6910756f
...@@ -10,9 +10,16 @@ import "cmd/internal/src" ...@@ -10,9 +10,16 @@ import "cmd/internal/src"
// Nodes // Nodes
type Node interface { type Node interface {
// Pos() returns the position associated with the node as follows:
// 1) The position of a node representing a terminal syntax production
// (Name, BasicLit, etc.) is the position of the respective production
// in the source.
// 2) The position of a node representing a non-terminal production
// (IndexExpr, IfStmt, etc.) is the position of a token uniquely
// associated with that production; usually the left-most one
// ('[' for IndexExpr, 'if' for IfStmt, etc.)
Pos() src.Pos Pos() src.Pos
aNode() aNode()
init(p *parser)
} }
type node struct { type node struct {
...@@ -27,11 +34,6 @@ func (n *node) Pos() src.Pos { ...@@ -27,11 +34,6 @@ func (n *node) Pos() src.Pos {
func (*node) aNode() {} func (*node) aNode() {}
// TODO(gri) we may be able to get rid of init here and in Node
func (n *node) init(p *parser) {
n.pos = p.pos()
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Files // Files
......
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syntax
import (
"fmt"
"strings"
"testing"
)
// A test is a source code snippet of a particular node type.
// In the snippet, a '@' indicates the position recorded by
// the parser when creating the respective node.
type test struct {
nodetyp string
snippet string
}
var decls = []test{
// The position of declarations is always the
// position of the first token of an individual
// declaration, independent of grouping.
{"ImportDecl", `import @"math"`},
{"ImportDecl", `import @mymath "math"`},
{"ImportDecl", `import @. "math"`},
{"ImportDecl", `import (@"math")`},
{"ImportDecl", `import (@mymath "math")`},
{"ImportDecl", `import (@. "math")`},
{"ConstDecl", `const @x`},
{"ConstDecl", `const @x = 0`},
{"ConstDecl", `const @x, y, z = 0, 1, 2`},
{"ConstDecl", `const (@x)`},
{"ConstDecl", `const (@x = 0)`},
{"ConstDecl", `const (@x, y, z = 0, 1, 2)`},
{"TypeDecl", `type @T int`},
{"TypeDecl", `type @T = int`},
{"TypeDecl", `type (@T int)`},
{"TypeDecl", `type (@T = int)`},
{"VarDecl", `var @x int`},
{"VarDecl", `var @x, y, z int`},
{"VarDecl", `var @x int = 0`},
{"VarDecl", `var @x, y, z int = 1, 2, 3`},
{"VarDecl", `var @x = 0`},
{"VarDecl", `var @x, y, z = 1, 2, 3`},
{"VarDecl", `var (@x int)`},
{"VarDecl", `var (@x, y, z int)`},
{"VarDecl", `var (@x int = 0)`},
{"VarDecl", `var (@x, y, z int = 1, 2, 3)`},
{"VarDecl", `var (@x = 0)`},
{"VarDecl", `var (@x, y, z = 1, 2, 3)`},
{"FuncDecl", `func @f() {}`},
{"FuncDecl", `func @(T) f() {}`},
{"FuncDecl", `func @(x T) f() {}`},
}
var exprs = []test{
// The position of an expression is the position
// of the left-most token that identifies the
// kind of expression.
{"Name", `@x`},
{"BasicLit", `@0`},
{"BasicLit", `@0x123`},
{"BasicLit", `@3.1415`},
{"BasicLit", `@.2718`},
{"BasicLit", `@1i`},
{"BasicLit", `@'a'`},
{"BasicLit", `@"abc"`},
{"BasicLit", "@`abc`"},
{"CompositeLit", `@{}`},
{"CompositeLit", `T@{}`},
{"CompositeLit", `struct{x, y int}@{}`},
{"KeyValueExpr", `"foo"@: true`},
{"KeyValueExpr", `"a"@: b`},
{"FuncLit", `@func (){}`},
{"ParenExpr", `@(x)`},
{"SelectorExpr", `a@.b`},
{"IndexExpr", `a@[i]`},
{"SliceExpr", `a@[:]`},
{"SliceExpr", `a@[i:]`},
{"SliceExpr", `a@[:j]`},
{"SliceExpr", `a@[i:j]`},
{"SliceExpr", `a@[i:j:k]`},
{"AssertExpr", `x@.(T)`},
{"Operation", `@*b`},
{"Operation", `@+b`},
{"Operation", `@-b`},
{"Operation", `@!b`},
{"Operation", `@^b`},
{"Operation", `@&b`},
{"Operation", `@<-b`},
{"Operation", `a @|| b`},
{"Operation", `a @&& b`},
{"Operation", `a @== b`},
{"Operation", `a @+ b`},
{"Operation", `a @* b`},
{"CallExpr", `f@()`},
{"CallExpr", `f@(x, y, z)`},
{"CallExpr", `obj.f@(1, 2, 3)`},
{"CallExpr", `func(x int) int { return x + 1 }@(y)`},
// ListExpr: tested via multi-value const/var declarations
}
var types = []test{
{"Operation", `@*T`},
{"Operation", `@*struct{}`},
{"ArrayType", `@[10]T`},
{"ArrayType", `@[...]T`},
{"SliceType", `@[]T`},
{"DotsType", `@...T`},
{"StructType", `@struct{}`},
{"InterfaceType", `@interface{}`},
{"FuncType", `func@()`},
{"MapType", `@map[T]T`},
{"ChanType", `@chan T`},
{"ChanType", `@chan<- T`},
{"ChanType", `@<-chan T`},
}
var fields = []test{
{"Field", `@T`},
{"Field", `@(T)`},
{"Field", `@x T`},
{"Field", `@x *(T)`},
{"Field", `@x, y, z T`},
{"Field", `@x, y, z (*T)`},
}
var stmts = []test{
{"EmptyStmt", `@;`},
{"LabeledStmt", `L@:`},
{"LabeledStmt", `L@: ;`},
{"LabeledStmt", `L@: f()`},
{"BlockStmt", `@{}`},
// The position of an ExprStmt is the position of the expression.
{"ExprStmt", `@<-ch`},
{"ExprStmt", `f@()`},
{"ExprStmt", `append@(s, 1, 2, 3)`},
{"SendStmt", `ch @<- x`},
{"DeclStmt", `@const x = 0`},
{"DeclStmt", `@const (x = 0)`},
{"DeclStmt", `@type T int`},
{"DeclStmt", `@type T = int`},
{"DeclStmt", `@type (T1 = int; T2 = float32)`},
{"DeclStmt", `@var x = 0`},
{"DeclStmt", `@var x, y, z int`},
{"DeclStmt", `@var (a, b = 1, 2)`},
{"AssignStmt", `x @= y`},
{"AssignStmt", `a, b, x @= 1, 2, 3`},
{"AssignStmt", `x @+= y`},
{"AssignStmt", `x @:= y`},
{"AssignStmt", `x, ok @:= f()`},
{"AssignStmt", `x@++`},
{"AssignStmt", `a[i]@--`},
{"BranchStmt", `@break`},
{"BranchStmt", `@break L`},
{"BranchStmt", `@continue`},
{"BranchStmt", `@continue L`},
{"BranchStmt", `@fallthrough`},
{"BranchStmt", `@goto L`},
{"CallStmt", `@defer f()`},
{"CallStmt", `@go f()`},
{"ReturnStmt", `@return`},
{"ReturnStmt", `@return x`},
{"ReturnStmt", `@return a, b, c`},
{"IfStmt", `@if cond {}`},
{"ForStmt", `@for {}`},
{"SwitchStmt", `@switch {}`},
{"SelectStmt", `@select {}`},
}
var ranges = []test{
{"RangeClause", `for @range s {}`},
{"RangeClause", `for _, i = @range s {}`},
{"RangeClause", `for x, i = @range s {}`},
{"RangeClause", `for _, i := @range s {}`},
{"RangeClause", `for x, i := @range s {}`},
}
var guards = []test{
{"TypeSwitchGuard", `switch x@.(type) {}`},
{"TypeSwitchGuard", `switch x := x@.(type) {}`},
{"TypeSwitchGuard", `switch a = b; x@.(type) {}`},
{"TypeSwitchGuard", `switch a := b; x := x@.(type) {}`},
}
var cases = []test{
{"CaseClause", ` switch { @case x: }`},
{"CaseClause", ` switch { @case x, y, z: }`},
{"CaseClause", ` switch { @case x == 1, y == 2: }`},
{"CaseClause", ` switch { @default: }`},
}
var comms = []test{
{"CommClause", `select { @case <-ch: }`},
{"CommClause", `select { @case x <- ch: }`},
{"CommClause", `select { @case x = <-ch: }`},
{"CommClause", `select { @case x := <-ch: }`},
{"CommClause", `select { @case x, ok = <-ch: }`},
{"CommClause", `select { @case x, ok := <-ch: }`},
{"CommClause", `select { @default: }`},
}
func TestPos(t *testing.T) {
// TODO(gri) Once we have a general tree walker, we can use that to find
// the first occurence of the respective node and we don't need to hand-
// extract the node for each specific kind of construct.
testPos(t, decls, "package p; ", "",
func(f *File) Node { return f.DeclList[0] },
)
// embed expressions in a composite literal so we can test key:value and naked composite literals
testPos(t, exprs, "package p; var _ = T{ ", " }",
func(f *File) Node { return f.DeclList[0].(*VarDecl).Values.(*CompositeLit).ElemList[0] },
)
// embed types in a function signature so we can test ... types
testPos(t, types, "package p; func f(", ")",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Type.ParamList[0].Type },
)
testPos(t, fields, "package p; func f(", ")",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Type.ParamList[0] },
)
testPos(t, stmts, "package p; func _() { ", " } ",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body[0] },
)
testPos(t, ranges, "package p; func _() { ", " } ",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body[0].(*ForStmt).Init.(*RangeClause) },
)
testPos(t, guards, "package p; func _() { ", " } ",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body[0].(*SwitchStmt).Tag.(*TypeSwitchGuard) },
)
testPos(t, cases, "package p; func _() { ", " } ",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body[0].(*SwitchStmt).Body[0] },
)
testPos(t, comms, "package p; func _() { ", " } ",
func(f *File) Node { return f.DeclList[0].(*FuncDecl).Body[0].(*SelectStmt).Body[0] },
)
}
func testPos(t *testing.T, list []test, prefix, suffix string, extract func(*File) Node) {
for _, test := range list {
// complete source, compute @ position, and strip @ from source
src, index := stripAt(prefix + test.snippet + suffix)
if index < 0 {
t.Errorf("missing @: %s", src)
continue
}
// build syntaxt tree
file, err := ParseBytes(nil, []byte(src), nil, nil, 0)
if err != nil {
t.Errorf("parse error: %s: %v", src, err)
continue
}
// extract desired node
node := extract(file)
if typ := typeOf(node); typ != test.nodetyp {
t.Errorf("type error: %s: type = %s, want %s", src, typ, test.nodetyp)
continue
}
// verify node position with expected position as indicated by @
if col := int(node.Pos().Col()); col != index {
t.Errorf("pos error: %s: col = %d, want %d", src, col, index)
continue
}
}
}
func stripAt(s string) (string, int) {
if i := strings.Index(s, "@"); i >= 0 {
return s[:i] + s[i+1:], i
}
return s, -1
}
func typeOf(n Node) string {
const prefix = "*syntax."
k := fmt.Sprintf("%T", n)
if strings.HasPrefix(k, prefix) {
return k[len(prefix):]
}
return k
}
...@@ -15,11 +15,6 @@ import ( ...@@ -15,11 +15,6 @@ import (
const debug = false const debug = false
const trace = false const trace = false
// The old gc parser assigned line numbers very inconsistently depending
// on when it happened to construct AST nodes. To make transitioning to the
// new AST easier, we try to mimick the behavior as much as possible.
const gcCompat = true
type parser struct { type parser struct {
base *src.PosBase base *src.PosBase
errh ErrorHandler errh ErrorHandler
...@@ -248,7 +243,7 @@ func (p *parser) file() *File { ...@@ -248,7 +243,7 @@ func (p *parser) file() *File {
} }
f := new(File) f := new(File)
f.init(p) f.pos = p.pos()
// PackageClause // PackageClause
if !p.got(_Package) { if !p.got(_Package) {
...@@ -346,14 +341,14 @@ func (p *parser) importDecl(group *Group) Decl { ...@@ -346,14 +341,14 @@ func (p *parser) importDecl(group *Group) Decl {
} }
d := new(ImportDecl) d := new(ImportDecl)
d.init(p) d.pos = p.pos()
switch p.tok { switch p.tok {
case _Name: case _Name:
d.LocalPkgName = p.name() d.LocalPkgName = p.name()
case _Dot: case _Dot:
n := new(Name) n := new(Name)
n.init(p) n.pos = p.pos()
n.Value = "." n.Value = "."
d.LocalPkgName = n d.LocalPkgName = n
p.next() p.next()
...@@ -376,7 +371,7 @@ func (p *parser) constDecl(group *Group) Decl { ...@@ -376,7 +371,7 @@ func (p *parser) constDecl(group *Group) Decl {
} }
d := new(ConstDecl) d := new(ConstDecl)
d.init(p) d.pos = p.pos()
d.NameList = p.nameList(p.name()) d.NameList = p.nameList(p.name())
if p.tok != _EOF && p.tok != _Semi && p.tok != _Rparen { if p.tok != _EOF && p.tok != _Semi && p.tok != _Rparen {
...@@ -397,7 +392,7 @@ func (p *parser) typeDecl(group *Group) Decl { ...@@ -397,7 +392,7 @@ func (p *parser) typeDecl(group *Group) Decl {
} }
d := new(TypeDecl) d := new(TypeDecl)
d.init(p) d.pos = p.pos()
d.Name = p.name() d.Name = p.name()
d.Alias = p.got(_Assign) d.Alias = p.got(_Assign)
...@@ -419,7 +414,7 @@ func (p *parser) varDecl(group *Group) Decl { ...@@ -419,7 +414,7 @@ func (p *parser) varDecl(group *Group) Decl {
} }
d := new(VarDecl) d := new(VarDecl)
d.init(p) d.pos = p.pos()
d.NameList = p.nameList(p.name()) d.NameList = p.nameList(p.name())
if p.got(_Assign) { if p.got(_Assign) {
...@@ -431,9 +426,6 @@ func (p *parser) varDecl(group *Group) Decl { ...@@ -431,9 +426,6 @@ func (p *parser) varDecl(group *Group) Decl {
} }
} }
d.Group = group d.Group = group
if gcCompat {
d.init(p)
}
return d return d
} }
...@@ -449,7 +441,7 @@ func (p *parser) funcDecl() *FuncDecl { ...@@ -449,7 +441,7 @@ func (p *parser) funcDecl() *FuncDecl {
} }
f := new(FuncDecl) f := new(FuncDecl)
f.init(p) f.pos = p.pos()
badRecv := false badRecv := false
if p.tok == _Lparen { if p.tok == _Lparen {
...@@ -488,9 +480,6 @@ func (p *parser) funcDecl() *FuncDecl { ...@@ -488,9 +480,6 @@ func (p *parser) funcDecl() *FuncDecl {
f.Name = p.name() f.Name = p.name()
f.Type = p.funcType() f.Type = p.funcType()
if gcCompat {
f.node = f.Type.node
}
f.Body = p.funcBody() f.Body = p.funcBody()
f.Pragma = p.pragma f.Pragma = p.pragma
...@@ -525,15 +514,12 @@ func (p *parser) binaryExpr(prec int) Expr { ...@@ -525,15 +514,12 @@ func (p *parser) binaryExpr(prec int) Expr {
x := p.unaryExpr() x := p.unaryExpr()
for (p.tok == _Operator || p.tok == _Star) && p.prec > prec { for (p.tok == _Operator || p.tok == _Star) && p.prec > prec {
t := new(Operation) t := new(Operation)
t.init(p) t.pos = p.pos()
t.Op = p.op t.Op = p.op
t.X = x t.X = x
tprec := p.prec tprec := p.prec
p.next() p.next()
t.Y = p.binaryExpr(tprec) t.Y = p.binaryExpr(tprec)
if gcCompat {
t.init(p)
}
x = t x = t
} }
return x return x
...@@ -550,20 +536,17 @@ func (p *parser) unaryExpr() Expr { ...@@ -550,20 +536,17 @@ func (p *parser) unaryExpr() Expr {
switch p.op { switch p.op {
case Mul, Add, Sub, Not, Xor: case Mul, Add, Sub, Not, Xor:
x := new(Operation) x := new(Operation)
x.init(p) x.pos = p.pos()
x.Op = p.op x.Op = p.op
p.next() p.next()
x.X = p.unaryExpr() x.X = p.unaryExpr()
if gcCompat {
x.init(p)
}
return x return x
case And: case And:
p.next()
x := new(Operation) x := new(Operation)
x.init(p) x.pos = p.pos()
x.Op = And x.Op = And
p.next()
// unaryExpr may have returned a parenthesized composite literal // unaryExpr may have returned a parenthesized composite literal
// (see comment in operand) - remove parentheses if any // (see comment in operand) - remove parentheses if any
x.X = unparen(p.unaryExpr()) x.X = unparen(p.unaryExpr())
...@@ -572,6 +555,7 @@ func (p *parser) unaryExpr() Expr { ...@@ -572,6 +555,7 @@ func (p *parser) unaryExpr() Expr {
case _Arrow: case _Arrow:
// receive op (<-x) or receive-only channel (<-chan E) // receive op (<-x) or receive-only channel (<-chan E)
pos := p.pos()
p.next() p.next()
// If the next token is _Chan we still don't know if it is // If the next token is _Chan we still don't know if it is
...@@ -620,7 +604,11 @@ func (p *parser) unaryExpr() Expr { ...@@ -620,7 +604,11 @@ func (p *parser) unaryExpr() Expr {
} }
// x is not a channel type => we have a receive op // x is not a channel type => we have a receive op
return &Operation{Op: Recv, X: x} o := new(Operation)
o.pos = pos
o.Op = Recv
o.X = x
return o
} }
// TODO(mdempsky): We need parens here so we can report an // TODO(mdempsky): We need parens here so we can report an
...@@ -636,7 +624,7 @@ func (p *parser) callStmt() *CallStmt { ...@@ -636,7 +624,7 @@ func (p *parser) callStmt() *CallStmt {
} }
s := new(CallStmt) s := new(CallStmt)
s.init(p) s.pos = p.pos()
s.Tok = p.tok // _Defer or _Go s.Tok = p.tok // _Defer or _Go
p.next() p.next()
...@@ -672,6 +660,7 @@ func (p *parser) operand(keep_parens bool) Expr { ...@@ -672,6 +660,7 @@ func (p *parser) operand(keep_parens bool) Expr {
return p.oliteral() return p.oliteral()
case _Lparen: case _Lparen:
pos := p.pos()
p.next() p.next()
p.xnest++ p.xnest++
x := p.expr() // expr_or_type x := p.expr() // expr_or_type
...@@ -700,21 +689,27 @@ func (p *parser) operand(keep_parens bool) Expr { ...@@ -700,21 +689,27 @@ func (p *parser) operand(keep_parens bool) Expr {
// in a go/defer statement. In that case, operand is called // in a go/defer statement. In that case, operand is called
// with keep_parens set. // with keep_parens set.
if keep_parens { if keep_parens {
x = &ParenExpr{X: x} px := new(ParenExpr)
px.pos = pos
px.X = x
x = px
} }
return x return x
case _Func: case _Func:
pos := p.pos()
p.next() p.next()
t := p.funcType() t := p.funcType()
if p.tok == _Lbrace { if p.tok == _Lbrace {
p.fnest++ p.fnest++
p.xnest++ p.xnest++
f := new(FuncLit) f := new(FuncLit)
f.init(p) f.pos = pos
f.Type = t f.Type = t
f.Body = p.funcBody() f.Body = p.funcBody()
f.EndLine = p.line f.EndLine = p.line
p.xnest-- p.xnest--
p.fnest-- p.fnest--
return f return f
...@@ -767,6 +762,7 @@ func (p *parser) pexpr(keep_parens bool) Expr { ...@@ -767,6 +762,7 @@ func (p *parser) pexpr(keep_parens bool) Expr {
loop: loop:
for { for {
pos := p.pos()
switch p.tok { switch p.tok {
case _Dot: case _Dot:
p.next() p.next()
...@@ -774,7 +770,7 @@ loop: ...@@ -774,7 +770,7 @@ loop:
case _Name: case _Name:
// pexpr '.' sym // pexpr '.' sym
t := new(SelectorExpr) t := new(SelectorExpr)
t.init(p) t.pos = pos
t.X = x t.X = x
t.Sel = p.name() t.Sel = p.name()
x = t x = t
...@@ -783,12 +779,12 @@ loop: ...@@ -783,12 +779,12 @@ loop:
p.next() p.next()
if p.got(_Type) { if p.got(_Type) {
t := new(TypeSwitchGuard) t := new(TypeSwitchGuard)
t.init(p) t.pos = pos
t.X = x t.X = x
x = t x = t
} else { } else {
t := new(AssertExpr) t := new(AssertExpr)
t.init(p) t.pos = pos
t.X = x t.X = x
t.Type = p.expr() t.Type = p.expr()
x = t x = t
...@@ -799,9 +795,6 @@ loop: ...@@ -799,9 +795,6 @@ loop:
p.syntax_error("expecting name or (") p.syntax_error("expecting name or (")
p.advance(_Semi, _Rparen) p.advance(_Semi, _Rparen)
} }
if gcCompat && x != nil {
x.init(p)
}
case _Lbrack: case _Lbrack:
p.next() p.next()
...@@ -813,7 +806,7 @@ loop: ...@@ -813,7 +806,7 @@ loop:
if p.got(_Rbrack) { if p.got(_Rbrack) {
// x[i] // x[i]
t := new(IndexExpr) t := new(IndexExpr)
t.init(p) t.pos = pos
t.X = x t.X = x
t.Index = i t.Index = i
x = t x = t
...@@ -824,7 +817,7 @@ loop: ...@@ -824,7 +817,7 @@ loop:
// x[i:... // x[i:...
t := new(SliceExpr) t := new(SliceExpr)
t.init(p) t.pos = pos
t.X = x t.X = x
t.Index[0] = i t.Index[0] = i
p.want(_Colon) p.want(_Colon)
...@@ -909,7 +902,7 @@ func (p *parser) complitexpr() *CompositeLit { ...@@ -909,7 +902,7 @@ func (p *parser) complitexpr() *CompositeLit {
} }
x := new(CompositeLit) x := new(CompositeLit)
x.init(p) x.pos = p.pos()
p.want(_Lbrace) p.want(_Lbrace)
p.xnest++ p.xnest++
...@@ -917,15 +910,13 @@ func (p *parser) complitexpr() *CompositeLit { ...@@ -917,15 +910,13 @@ func (p *parser) complitexpr() *CompositeLit {
for p.tok != _EOF && p.tok != _Rbrace { for p.tok != _EOF && p.tok != _Rbrace {
// value // value
e := p.bare_complitexpr() e := p.bare_complitexpr()
if p.got(_Colon) { if p.tok == _Colon {
// key ':' value // key ':' value
l := new(KeyValueExpr) l := new(KeyValueExpr)
l.init(p) l.pos = p.pos()
p.next()
l.Key = e l.Key = e
l.Value = p.bare_complitexpr() l.Value = p.bare_complitexpr()
if gcCompat {
l.init(p)
}
e = l e = l
x.NKeys++ x.NKeys++
} }
...@@ -954,13 +945,17 @@ func (p *parser) type_() Expr { ...@@ -954,13 +945,17 @@ func (p *parser) type_() Expr {
return typ return typ
} }
p.syntax_error("") p.syntax_error("expecting type")
p.advance() p.advance()
return nil return nil
} }
func indirect(typ Expr) Expr { func indirect(pos src.Pos, typ Expr) Expr {
return &Operation{Op: Mul, X: typ} o := new(Operation)
o.pos = pos
o.Op = Mul
o.X = typ
return o
} }
// tryType is like type_ but it returns nil if there was no type // tryType is like type_ but it returns nil if there was no type
...@@ -975,18 +970,19 @@ func (p *parser) tryType() Expr { ...@@ -975,18 +970,19 @@ func (p *parser) tryType() Expr {
defer p.trace("tryType")() defer p.trace("tryType")()
} }
pos := p.pos()
switch p.tok { switch p.tok {
case _Star: case _Star:
// ptrtype // ptrtype
p.next() p.next()
return indirect(p.type_()) return indirect(pos, p.type_())
case _Arrow: case _Arrow:
// recvchantype // recvchantype
p.next() p.next()
p.want(_Chan) p.want(_Chan)
t := new(ChanType) t := new(ChanType)
t.init(p) t.pos = pos
t.Dir = RecvOnly t.Dir = RecvOnly
t.Elem = p.chanElem() t.Elem = p.chanElem()
return t return t
...@@ -1005,14 +1001,14 @@ func (p *parser) tryType() Expr { ...@@ -1005,14 +1001,14 @@ func (p *parser) tryType() Expr {
// []T // []T
p.xnest-- p.xnest--
t := new(SliceType) t := new(SliceType)
t.init(p) t.pos = pos
t.Elem = p.type_() t.Elem = p.type_()
return t return t
} }
// [n]T // [n]T
t := new(ArrayType) t := new(ArrayType)
t.init(p) t.pos = pos
if !p.got(_DotDotDot) { if !p.got(_DotDotDot) {
t.Len = p.expr() t.Len = p.expr()
} }
...@@ -1026,7 +1022,7 @@ func (p *parser) tryType() Expr { ...@@ -1026,7 +1022,7 @@ func (p *parser) tryType() Expr {
// _Chan _Comm ntype // _Chan _Comm ntype
p.next() p.next()
t := new(ChanType) t := new(ChanType)
t.init(p) t.pos = pos
if p.got(_Arrow) { if p.got(_Arrow) {
t.Dir = SendOnly t.Dir = SendOnly
} }
...@@ -1038,7 +1034,7 @@ func (p *parser) tryType() Expr { ...@@ -1038,7 +1034,7 @@ func (p *parser) tryType() Expr {
p.next() p.next()
p.want(_Lbrack) p.want(_Lbrack)
t := new(MapType) t := new(MapType)
t.init(p) t.pos = pos
t.Key = p.type_() t.Key = p.type_()
p.want(_Rbrack) p.want(_Rbrack)
t.Value = p.type_() t.Value = p.type_()
...@@ -1069,12 +1065,10 @@ func (p *parser) funcType() *FuncType { ...@@ -1069,12 +1065,10 @@ func (p *parser) funcType() *FuncType {
} }
typ := new(FuncType) typ := new(FuncType)
typ.init(p) typ.pos = p.pos()
typ.ParamList = p.paramList() typ.ParamList = p.paramList()
typ.ResultList = p.funcResult() typ.ResultList = p.funcResult()
if gcCompat {
typ.init(p)
}
return typ return typ
} }
...@@ -1097,9 +1091,10 @@ func (p *parser) dotname(name *Name) Expr { ...@@ -1097,9 +1091,10 @@ func (p *parser) dotname(name *Name) Expr {
defer p.trace("dotname")() defer p.trace("dotname")()
} }
if p.got(_Dot) { if p.tok == _Dot {
s := new(SelectorExpr) s := new(SelectorExpr)
s.init(p) s.pos = p.pos()
p.next()
s.X = name s.X = name
s.Sel = p.name() s.Sel = p.name()
return s return s
...@@ -1114,7 +1109,7 @@ func (p *parser) structType() *StructType { ...@@ -1114,7 +1109,7 @@ func (p *parser) structType() *StructType {
} }
typ := new(StructType) typ := new(StructType)
typ.init(p) typ.pos = p.pos()
p.want(_Struct) p.want(_Struct)
p.want(_Lbrace) p.want(_Lbrace)
...@@ -1136,7 +1131,7 @@ func (p *parser) interfaceType() *InterfaceType { ...@@ -1136,7 +1131,7 @@ func (p *parser) interfaceType() *InterfaceType {
} }
typ := new(InterfaceType) typ := new(InterfaceType)
typ.init(p) typ.pos = p.pos()
p.want(_Interface) p.want(_Interface)
p.want(_Lbrace) p.want(_Lbrace)
...@@ -1183,9 +1178,10 @@ func (p *parser) funcResult() []*Field { ...@@ -1183,9 +1178,10 @@ func (p *parser) funcResult() []*Field {
return p.paramList() return p.paramList()
} }
pos := p.pos()
if result := p.tryType(); result != nil { if result := p.tryType(); result != nil {
f := new(Field) f := new(Field)
f.init(p) f.pos = pos
f.Type = result f.Type = result
return []*Field{f} return []*Field{f}
} }
...@@ -1193,7 +1189,7 @@ func (p *parser) funcResult() []*Field { ...@@ -1193,7 +1189,7 @@ func (p *parser) funcResult() []*Field {
return nil return nil
} }
func (p *parser) addField(styp *StructType, name *Name, typ Expr, tag *BasicLit) { func (p *parser) addField(styp *StructType, pos src.Pos, name *Name, typ Expr, tag *BasicLit) {
if tag != nil { if tag != nil {
for i := len(styp.FieldList) - len(styp.TagList); i > 0; i-- { for i := len(styp.FieldList) - len(styp.TagList); i > 0; i-- {
styp.TagList = append(styp.TagList, nil) styp.TagList = append(styp.TagList, nil)
...@@ -1202,15 +1198,11 @@ func (p *parser) addField(styp *StructType, name *Name, typ Expr, tag *BasicLit) ...@@ -1202,15 +1198,11 @@ func (p *parser) addField(styp *StructType, name *Name, typ Expr, tag *BasicLit)
} }
f := new(Field) f := new(Field)
f.init(p) f.pos = pos
f.Name = name f.Name = name
f.Type = typ f.Type = typ
styp.FieldList = append(styp.FieldList, f) styp.FieldList = append(styp.FieldList, f)
if gcCompat && name != nil {
f.node = name.node
}
if debug && tag != nil && len(styp.FieldList) != len(styp.TagList) { if debug && tag != nil && len(styp.FieldList) != len(styp.TagList) {
panic("inconsistent struct field list") panic("inconsistent struct field list")
} }
...@@ -1224,15 +1216,15 @@ func (p *parser) fieldDecl(styp *StructType) { ...@@ -1224,15 +1216,15 @@ func (p *parser) fieldDecl(styp *StructType) {
defer p.trace("fieldDecl")() defer p.trace("fieldDecl")()
} }
var name *Name pos := p.pos()
switch p.tok { switch p.tok {
case _Name: case _Name:
name = p.name() name := p.name()
if p.tok == _Dot || p.tok == _Literal || p.tok == _Semi || p.tok == _Rbrace { if p.tok == _Dot || p.tok == _Literal || p.tok == _Semi || p.tok == _Rbrace {
// embed oliteral // embed oliteral
typ := p.qualifiedName(name) typ := p.qualifiedName(name)
tag := p.oliteral() tag := p.oliteral()
p.addField(styp, nil, typ, tag) p.addField(styp, pos, nil, typ, tag)
return return
} }
...@@ -1242,18 +1234,19 @@ func (p *parser) fieldDecl(styp *StructType) { ...@@ -1242,18 +1234,19 @@ func (p *parser) fieldDecl(styp *StructType) {
tag := p.oliteral() tag := p.oliteral()
for _, name := range names { for _, name := range names {
p.addField(styp, name, typ, tag) p.addField(styp, name.Pos(), name, typ, tag)
} }
case _Lparen: case _Lparen:
p.next() p.next()
if p.tok == _Star { if p.tok == _Star {
// '(' '*' embed ')' oliteral // '(' '*' embed ')' oliteral
pos := p.pos()
p.next() p.next()
typ := indirect(p.qualifiedName(nil)) typ := indirect(pos, p.qualifiedName(nil))
p.want(_Rparen) p.want(_Rparen)
tag := p.oliteral() tag := p.oliteral()
p.addField(styp, nil, typ, tag) p.addField(styp, pos, nil, typ, tag)
p.syntax_error("cannot parenthesize embedded type") p.syntax_error("cannot parenthesize embedded type")
} else { } else {
...@@ -1261,7 +1254,7 @@ func (p *parser) fieldDecl(styp *StructType) { ...@@ -1261,7 +1254,7 @@ func (p *parser) fieldDecl(styp *StructType) {
typ := p.qualifiedName(nil) typ := p.qualifiedName(nil)
p.want(_Rparen) p.want(_Rparen)
tag := p.oliteral() tag := p.oliteral()
p.addField(styp, nil, typ, tag) p.addField(styp, pos, nil, typ, tag)
p.syntax_error("cannot parenthesize embedded type") p.syntax_error("cannot parenthesize embedded type")
} }
...@@ -1269,17 +1262,17 @@ func (p *parser) fieldDecl(styp *StructType) { ...@@ -1269,17 +1262,17 @@ func (p *parser) fieldDecl(styp *StructType) {
p.next() p.next()
if p.got(_Lparen) { if p.got(_Lparen) {
// '*' '(' embed ')' oliteral // '*' '(' embed ')' oliteral
typ := indirect(p.qualifiedName(nil)) typ := indirect(pos, p.qualifiedName(nil))
p.want(_Rparen) p.want(_Rparen)
tag := p.oliteral() tag := p.oliteral()
p.addField(styp, nil, typ, tag) p.addField(styp, pos, nil, typ, tag)
p.syntax_error("cannot parenthesize embedded type") p.syntax_error("cannot parenthesize embedded type")
} else { } else {
// '*' embed oliteral // '*' embed oliteral
typ := indirect(p.qualifiedName(nil)) typ := indirect(pos, p.qualifiedName(nil))
tag := p.oliteral() tag := p.oliteral()
p.addField(styp, nil, typ, tag) p.addField(styp, pos, nil, typ, tag)
} }
default: default:
...@@ -1291,7 +1284,7 @@ func (p *parser) fieldDecl(styp *StructType) { ...@@ -1291,7 +1284,7 @@ func (p *parser) fieldDecl(styp *StructType) {
func (p *parser) oliteral() *BasicLit { func (p *parser) oliteral() *BasicLit {
if p.tok == _Literal { if p.tok == _Literal {
b := new(BasicLit) b := new(BasicLit)
b.init(p) b.pos = p.pos()
b.Value = p.lit b.Value = p.lit
b.Kind = p.kind b.Kind = p.kind
p.next() p.next()
...@@ -1324,7 +1317,7 @@ func (p *parser) methodDecl() *Field { ...@@ -1324,7 +1317,7 @@ func (p *parser) methodDecl() *Field {
} }
f := new(Field) f := new(Field)
f.init(p) f.pos = name.Pos()
if p.tok != _Lparen { if p.tok != _Lparen {
// packname // packname
f.Type = p.qualifiedName(name) f.Type = p.qualifiedName(name)
...@@ -1336,16 +1329,16 @@ func (p *parser) methodDecl() *Field { ...@@ -1336,16 +1329,16 @@ func (p *parser) methodDecl() *Field {
return f return f
case _Lparen: case _Lparen:
p.next() p.syntax_error("cannot parenthesize embedded type")
f := new(Field) f := new(Field)
f.init(p) f.pos = p.pos()
p.next()
f.Type = p.qualifiedName(nil) f.Type = p.qualifiedName(nil)
p.want(_Rparen) p.want(_Rparen)
p.syntax_error("cannot parenthesize embedded type")
return f return f
default: default:
p.syntax_error("") p.syntax_error("expecting method or interface name")
p.advance(_Semi, _Rbrace) p.advance(_Semi, _Rbrace)
return nil return nil
} }
...@@ -1358,7 +1351,7 @@ func (p *parser) paramDecl() *Field { ...@@ -1358,7 +1351,7 @@ func (p *parser) paramDecl() *Field {
} }
f := new(Field) f := new(Field)
f.init(p) f.pos = p.pos()
switch p.tok { switch p.tok {
case _Name: case _Name:
...@@ -1403,7 +1396,7 @@ func (p *parser) dotsType() *DotsType { ...@@ -1403,7 +1396,7 @@ func (p *parser) dotsType() *DotsType {
} }
t := new(DotsType) t := new(DotsType)
t.init(p) t.pos = p.pos()
p.want(_DotDotDot) p.want(_DotDotDot)
t.Elem = p.tryType() t.Elem = p.tryType()
...@@ -1486,7 +1479,7 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt { ...@@ -1486,7 +1479,7 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt {
defer p.trace("simpleStmt")() defer p.trace("simpleStmt")()
} }
if rangeOk && p.got(_Range) { if rangeOk && p.tok == _Range {
// _Range expr // _Range expr
if debug && lhs != nil { if debug && lhs != nil {
panic("invalid call of simpleStmt") panic("invalid call of simpleStmt")
...@@ -1500,55 +1493,60 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt { ...@@ -1500,55 +1493,60 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt {
if _, ok := lhs.(*ListExpr); !ok && p.tok != _Assign && p.tok != _Define { if _, ok := lhs.(*ListExpr); !ok && p.tok != _Assign && p.tok != _Define {
// expr // expr
pos := p.pos()
switch p.tok { switch p.tok {
case _AssignOp: case _AssignOp:
// lhs op= rhs // lhs op= rhs
op := p.op op := p.op
p.next() p.next()
return p.newAssignStmt(op, lhs, p.expr()) return p.newAssignStmt(pos, op, lhs, p.expr())
case _IncOp: case _IncOp:
// lhs++ or lhs-- // lhs++ or lhs--
op := p.op op := p.op
p.next() p.next()
return p.newAssignStmt(op, lhs, ImplicitOne) return p.newAssignStmt(pos, op, lhs, ImplicitOne)
case _Arrow: case _Arrow:
// lhs <- rhs // lhs <- rhs
p.next()
s := new(SendStmt) s := new(SendStmt)
s.init(p) s.pos = pos
p.next()
s.Chan = lhs s.Chan = lhs
s.Value = p.expr() s.Value = p.expr()
if gcCompat {
s.init(p)
}
return s return s
default: default:
// expr // expr
return &ExprStmt{X: lhs} s := new(ExprStmt)
if lhs != nil { // be cautious (test/syntax/semi4.go)
s.pos = lhs.Pos()
} else {
s.pos = p.pos()
}
s.X = lhs
return s
} }
} }
// expr_list // expr_list
pos := p.pos()
switch p.tok { switch p.tok {
case _Assign: case _Assign:
p.next() p.next()
if rangeOk && p.got(_Range) { if rangeOk && p.tok == _Range {
// expr_list '=' _Range expr // expr_list '=' _Range expr
return p.rangeClause(lhs, false) return p.rangeClause(lhs, false)
} }
// expr_list '=' expr_list // expr_list '=' expr_list
return p.newAssignStmt(0, lhs, p.exprList()) return p.newAssignStmt(pos, 0, lhs, p.exprList())
case _Define: case _Define:
pos := p.pos()
p.next() p.next()
if rangeOk && p.got(_Range) { if rangeOk && p.tok == _Range {
// expr_list ':=' range expr // expr_list ':=' range expr
return p.rangeClause(lhs, true) return p.rangeClause(lhs, true)
} }
...@@ -1566,10 +1564,13 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt { ...@@ -1566,10 +1564,13 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt {
// TODO(mdempsky): Have Expr types implement Stringer? // TODO(mdempsky): Have Expr types implement Stringer?
p.error(fmt.Sprintf("invalid variable name %s in type switch", lhs)) p.error(fmt.Sprintf("invalid variable name %s in type switch", lhs))
} }
return &ExprStmt{X: x} s := new(ExprStmt)
s.pos = x.Pos()
s.X = x
return s
} }
as := p.newAssignStmt(Def, lhs, rhs) as := p.newAssignStmt(pos, Def, lhs, rhs)
as.pos = pos // TODO(gri) pass this into newAssignStmt as.pos = pos // TODO(gri) pass this into newAssignStmt
return as return as
...@@ -1582,19 +1583,17 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt { ...@@ -1582,19 +1583,17 @@ func (p *parser) simpleStmt(lhs Expr, rangeOk bool) SimpleStmt {
func (p *parser) rangeClause(lhs Expr, def bool) *RangeClause { func (p *parser) rangeClause(lhs Expr, def bool) *RangeClause {
r := new(RangeClause) r := new(RangeClause)
r.init(p) r.pos = p.pos()
p.next() // consume _Range
r.Lhs = lhs r.Lhs = lhs
r.Def = def r.Def = def
r.X = p.expr() r.X = p.expr()
if gcCompat {
r.init(p)
}
return r return r
} }
func (p *parser) newAssignStmt(op Operator, lhs, rhs Expr) *AssignStmt { func (p *parser) newAssignStmt(pos src.Pos, op Operator, lhs, rhs Expr) *AssignStmt {
a := new(AssignStmt) a := new(AssignStmt)
a.init(p) a.pos = pos
a.Op = op a.Op = op
a.Lhs = lhs a.Lhs = lhs
a.Rhs = rhs a.Rhs = rhs
...@@ -1607,7 +1606,7 @@ func (p *parser) labeledStmt(label *Name) Stmt { ...@@ -1607,7 +1606,7 @@ func (p *parser) labeledStmt(label *Name) Stmt {
} }
s := new(LabeledStmt) s := new(LabeledStmt)
s.init(p) s.pos = p.pos()
s.Label = label s.Label = label
p.want(_Colon) p.want(_Colon)
...@@ -1631,7 +1630,7 @@ func (p *parser) blockStmt() *BlockStmt { ...@@ -1631,7 +1630,7 @@ func (p *parser) blockStmt() *BlockStmt {
} }
s := new(BlockStmt) s := new(BlockStmt)
s.init(p) s.pos = p.pos()
p.want(_Lbrace) p.want(_Lbrace)
s.Body = p.stmtList() s.Body = p.stmtList()
p.want(_Rbrace) p.want(_Rbrace)
...@@ -1645,7 +1644,7 @@ func (p *parser) declStmt(f func(*Group) Decl) *DeclStmt { ...@@ -1645,7 +1644,7 @@ func (p *parser) declStmt(f func(*Group) Decl) *DeclStmt {
} }
s := new(DeclStmt) s := new(DeclStmt)
s.init(p) s.pos = p.pos()
p.next() // _Const, _Type, or _Var p.next() // _Const, _Type, or _Var
s.DeclList = p.appendGroup(nil, f) s.DeclList = p.appendGroup(nil, f)
...@@ -1659,13 +1658,9 @@ func (p *parser) forStmt() Stmt { ...@@ -1659,13 +1658,9 @@ func (p *parser) forStmt() Stmt {
} }
s := new(ForStmt) s := new(ForStmt)
s.init(p) s.pos = p.pos()
p.want(_For)
s.Init, s.Cond, s.Post = p.header(_For) s.Init, s.Cond, s.Post = p.header(_For)
if gcCompat {
s.init(p)
}
s.Body = p.stmtBody("for clause") s.Body = p.stmtBody("for clause")
return s return s
...@@ -1689,7 +1684,7 @@ func (p *parser) stmtBody(context string) []Stmt { ...@@ -1689,7 +1684,7 @@ func (p *parser) stmtBody(context string) []Stmt {
} }
func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleStmt) { func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleStmt) {
// TODO(gri) move caller's p.want(keyword) here, once we removed gcCompat p.want(keyword)
if p.tok == _Lbrace { if p.tok == _Lbrace {
if keyword == _If { if keyword == _If {
...@@ -1765,15 +1760,9 @@ func (p *parser) ifStmt() *IfStmt { ...@@ -1765,15 +1760,9 @@ func (p *parser) ifStmt() *IfStmt {
} }
s := new(IfStmt) s := new(IfStmt)
s.init(p) s.pos = p.pos()
p.want(_If)
s.Init, s.Cond, _ = p.header(_If) s.Init, s.Cond, _ = p.header(_If)
if gcCompat {
s.init(p)
}
s.Then = p.stmtBody("if clause") s.Then = p.stmtBody("if clause")
if p.got(_Else) { if p.got(_Else) {
...@@ -1796,9 +1785,8 @@ func (p *parser) switchStmt() *SwitchStmt { ...@@ -1796,9 +1785,8 @@ func (p *parser) switchStmt() *SwitchStmt {
defer p.trace("switchStmt")() defer p.trace("switchStmt")()
} }
p.want(_Switch)
s := new(SwitchStmt) s := new(SwitchStmt)
s.init(p) s.pos = p.pos()
s.Init, s.Tag, _ = p.header(_Switch) s.Init, s.Tag, _ = p.header(_Switch)
...@@ -1819,10 +1807,10 @@ func (p *parser) selectStmt() *SelectStmt { ...@@ -1819,10 +1807,10 @@ func (p *parser) selectStmt() *SelectStmt {
defer p.trace("selectStmt")() defer p.trace("selectStmt")()
} }
p.want(_Select)
s := new(SelectStmt) s := new(SelectStmt)
s.init(p) s.pos = p.pos()
p.want(_Select)
if !p.got(_Lbrace) { if !p.got(_Lbrace) {
p.syntax_error("missing { after select clause") p.syntax_error("missing { after select clause")
p.advance(_Case, _Default, _Rbrace) p.advance(_Case, _Default, _Rbrace)
...@@ -1841,7 +1829,7 @@ func (p *parser) caseClause() *CaseClause { ...@@ -1841,7 +1829,7 @@ func (p *parser) caseClause() *CaseClause {
} }
c := new(CaseClause) c := new(CaseClause)
c.init(p) c.pos = p.pos()
switch p.tok { switch p.tok {
case _Case: case _Case:
...@@ -1868,7 +1856,7 @@ func (p *parser) commClause() *CommClause { ...@@ -1868,7 +1856,7 @@ func (p *parser) commClause() *CommClause {
} }
c := new(CommClause) c := new(CommClause)
c.init(p) c.pos = p.pos()
switch p.tok { switch p.tok {
case _Case: case _Case:
...@@ -1895,9 +1883,6 @@ func (p *parser) commClause() *CommClause { ...@@ -1895,9 +1883,6 @@ func (p *parser) commClause() *CommClause {
p.advance(_Case, _Default, _Rbrace) p.advance(_Case, _Default, _Rbrace)
} }
if gcCompat {
c.init(p)
}
p.want(_Colon) p.want(_Colon)
c.Body = p.stmtList() c.Body = p.stmtList()
...@@ -1966,22 +1951,17 @@ func (p *parser) stmt() Stmt { ...@@ -1966,22 +1951,17 @@ func (p *parser) stmt() Stmt {
return p.ifStmt() return p.ifStmt()
case _Fallthrough: case _Fallthrough:
p.next()
s := new(BranchStmt) s := new(BranchStmt)
s.init(p) s.pos = p.pos()
p.next()
s.Tok = _Fallthrough s.Tok = _Fallthrough
return s return s
// // will be converted to OFALL
// stmt := nod(OXFALL, nil, nil)
// stmt.Xoffset = int64(block)
// return stmt
case _Break, _Continue: case _Break, _Continue:
tok := p.tok
p.next()
s := new(BranchStmt) s := new(BranchStmt)
s.init(p) s.pos = p.pos()
s.Tok = tok s.Tok = p.tok
p.next()
if p.tok == _Name { if p.tok == _Name {
s.Label = p.name() s.Label = p.name()
} }
...@@ -1991,31 +1971,25 @@ func (p *parser) stmt() Stmt { ...@@ -1991,31 +1971,25 @@ func (p *parser) stmt() Stmt {
return p.callStmt() return p.callStmt()
case _Goto: case _Goto:
p.next()
s := new(BranchStmt) s := new(BranchStmt)
s.init(p) s.pos = p.pos()
s.Tok = _Goto s.Tok = _Goto
p.next()
s.Label = p.name() s.Label = p.name()
return s return s
// stmt := nod(OGOTO, p.new_name(p.name()), nil)
// stmt.Sym = dclstack // context, for goto restrictions
// return stmt
case _Return: case _Return:
p.next()
s := new(ReturnStmt) s := new(ReturnStmt)
s.init(p) s.pos = p.pos()
p.next()
if p.tok != _Semi && p.tok != _Rbrace { if p.tok != _Semi && p.tok != _Rbrace {
s.Results = p.exprList() s.Results = p.exprList()
} }
if gcCompat {
s.init(p)
}
return s return s
case _Semi: case _Semi:
s := new(EmptyStmt) s := new(EmptyStmt)
s.init(p) s.pos = p.pos()
return s return s
} }
...@@ -2056,7 +2030,7 @@ func (p *parser) call(fun Expr) *CallExpr { ...@@ -2056,7 +2030,7 @@ func (p *parser) call(fun Expr) *CallExpr {
// call or conversion // call or conversion
// convtype '(' expr ocomma ')' // convtype '(' expr ocomma ')'
c := new(CallExpr) c := new(CallExpr)
c.init(p) c.pos = p.pos()
c.Fun = fun c.Fun = fun
p.want(_Lparen) p.want(_Lparen)
...@@ -2071,9 +2045,6 @@ func (p *parser) call(fun Expr) *CallExpr { ...@@ -2071,9 +2045,6 @@ func (p *parser) call(fun Expr) *CallExpr {
} }
p.xnest-- p.xnest--
if gcCompat {
c.init(p)
}
p.want(_Rparen) p.want(_Rparen)
return c return c
...@@ -2086,7 +2057,7 @@ func (p *parser) name() *Name { ...@@ -2086,7 +2057,7 @@ func (p *parser) name() *Name {
// no tracing to avoid overly verbose output // no tracing to avoid overly verbose output
n := new(Name) n := new(Name)
n.init(p) n.pos = p.pos()
if p.tok == _Name { if p.tok == _Name {
n.Value = p.lit n.Value = p.lit
...@@ -2132,7 +2103,7 @@ func (p *parser) qualifiedName(name *Name) Expr { ...@@ -2132,7 +2103,7 @@ func (p *parser) qualifiedName(name *Name) Expr {
name = p.name() name = p.name()
default: default:
name = new(Name) name = new(Name)
name.init(p) name.pos = p.pos()
p.syntax_error("expecting name") p.syntax_error("expecting name")
p.advance(_Dot, _Semi, _Rbrace) p.advance(_Dot, _Semi, _Rbrace)
} }
......
...@@ -55,9 +55,9 @@ func ClosureCallArgs4() { ...@@ -55,9 +55,9 @@ func ClosureCallArgs4() {
func ClosureCallArgs5() { func ClosureCallArgs5() {
x := 0 // ERROR "moved to heap: x" x := 0 // ERROR "moved to heap: x"
sink = func(p *int) *int { // ERROR "leaking param: p to result ~r1" "func literal does not escape" sink = func(p *int) *int { // ERROR "leaking param: p to result ~r1" "func literal does not escape" "\(func literal\)\(&x\) escapes to heap"
return p return p
}(&x) // ERROR "&x escapes to heap" "\(func literal\)\(&x\) escapes to heap" }(&x) // ERROR "&x escapes to heap"
} }
func ClosureCallArgs6() { func ClosureCallArgs6() {
...@@ -140,10 +140,10 @@ func ClosureCallArgs14() { ...@@ -140,10 +140,10 @@ func ClosureCallArgs14() {
func ClosureCallArgs15() { func ClosureCallArgs15() {
x := 0 // ERROR "moved to heap: x" x := 0 // ERROR "moved to heap: x"
p := &x // ERROR "moved to heap: p" "&x escapes to heap" p := &x // ERROR "moved to heap: p" "&x escapes to heap"
sink = func(p **int) *int { // ERROR "leaking param: p to result ~r1 level=1" "func literal does not escape" sink = func(p **int) *int { // ERROR "leaking param: p to result ~r1 level=1" "func literal does not escape" "\(func literal\)\(&p\) escapes to heap"
return *p return *p
// BAD: p should not escape here // BAD: p should not escape here
}(&p) // ERROR "&p escapes to heap" "\(func literal\)\(&p\) escapes to heap" }(&p) // ERROR "&p escapes to heap"
} }
func ClosureLeak1(s string) string { // ERROR "ClosureLeak1 s does not escape" func ClosureLeak1(s string) string { // ERROR "ClosureLeak1 s does not escape"
......
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