aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2016-05-06 23:17:31 -0700
committerMatthew Dempsky <mdempsky@google.com>2016-08-16 14:32:02 -0700
commit4f989a487d8812666f7f2dd3573122a1a6faa0ce (patch)
treec849e3d169762a54ef0ac37247c1cbbe4eb622e2
parentd5bb1db3ec256ea65713baeaa3b2cb1b0b151792 (diff)
downloadgo-4f989a487d8812666f7f2dd3573122a1a6faa0ce.tar.gz
go-4f989a487d8812666f7f2dd3573122a1a6faa0ce.zip
cmd/compile/internal/syntax: various tweaks
Export token constants needed by users (Break, Continue, Defer, Fallthrough, Go, and Goto). Fix parsing of Continue statements: previously they would be parsed as a BranchStmt with Break as the token. Change aNode, aDecl, aStmt, aExpr, etc.'s tag methods to take a pointer receiver to prevent accidents like trying to type assert from syntax.Expr to syntax.ListExpr instead of *syntax.ListExpr. Relocate pointer fields to the end of AST nodes so the GC can short-circuit scanning objects. Change-Id: Ib7505e75726816e260b9b29a2f726f76bf1a38b4
-rw-r--r--src/cmd/compile/internal/syntax/nodes.go98
-rw-r--r--src/cmd/compile/internal/syntax/parser.go7
-rw-r--r--src/cmd/compile/internal/syntax/tokens.go10
3 files changed, 63 insertions, 52 deletions
diff --git a/src/cmd/compile/internal/syntax/nodes.go b/src/cmd/compile/internal/syntax/nodes.go
index 25eee38428..f190e8aec9 100644
--- a/src/cmd/compile/internal/syntax/nodes.go
+++ b/src/cmd/compile/internal/syntax/nodes.go
@@ -12,12 +12,12 @@ type Node interface {
}
type node struct {
+ doc *Comment // nil means no comment(s) attached
pos uint32
line uint32
- doc *Comment // nil means no comment(s) attached
}
-func (node) aNode() {}
+func (*node) aNode() {}
func (n *node) init(p *parser) {
n.pos = uint32(p.pos)
@@ -28,10 +28,10 @@ func (n *node) init(p *parser) {
// Files
type File struct {
- node
PkgName *Name
DeclList []Decl
Lines int
+ node
}
// ----------------------------------------------------------------------------
@@ -44,48 +44,48 @@ type (
}
ImportDecl struct {
- decl
LocalPkgName *Name // including "."; nil means no rename present
Path *BasicLit
Group *Group // nil means not part of a group
+ decl
}
ConstDecl struct {
- decl
NameList []*Name
Type Expr // nil means no type
Values Expr // nil means no values
Group *Group // nil means not part of a group
+ decl
}
TypeDecl struct {
- decl
Name *Name
Type Expr
Group *Group // nil means not part of a group
+ decl
}
VarDecl struct {
- decl
NameList []*Name
Type Expr // nil means no type
Values Expr // nil means no values
Group *Group // nil means not part of a group
+ decl
}
FuncDecl struct {
- decl
Attr map[string]bool // go:attr map
Recv *Field // nil means regular function
Name *Name
Type *FuncType
Body []Stmt // nil means no body (forward declaration)
+ decl
}
)
type decl struct{ node }
-func (decl) aDecl() {}
+func (*decl) aDecl() {}
// All declarations belonging to the same group point to the same Group node.
type Group struct {
@@ -103,159 +103,159 @@ type (
// Value
Name struct {
- expr
Value string
+ expr
}
// Value
BasicLit struct {
- expr
Value string
+ expr
}
// Type { ElemList[0], ElemList[1], ... }
CompositeLit struct {
- expr
Type Expr // nil means no literal type
ElemList []Expr
NKeys int // number of elements with keys
+ expr
}
// Key: Value
KeyValueExpr struct {
- expr
Key, Value Expr
+ expr
}
// func Type { Body }
FuncLit struct {
- expr
Type *FuncType
Body []Stmt
+ expr
}
// (X)
ParenExpr struct {
- expr
X Expr
+ expr
}
// X.Sel
SelectorExpr struct {
- expr
X Expr
Sel *Name
+ expr
}
// X[Index]
IndexExpr struct {
- expr
X Expr
Index Expr
+ expr
}
// X[Index[0] : Index[1] : Index[2]]
SliceExpr struct {
- expr
X Expr
Index [3]Expr
+ expr
}
// X.(Type)
AssertExpr struct {
- expr
X Expr
// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
Type Expr // nil means x.(type) (for use in type switch)
+ expr
}
Operation struct {
- expr
Op Operator
X, Y Expr // Y == nil means unary expression
+ expr
}
// Fun(ArgList[0], ArgList[1], ...)
CallExpr struct {
- expr
Fun Expr
ArgList []Expr
HasDots bool // last argument is followed by ...
+ expr
}
// ElemList[0], ElemList[1], ...
ListExpr struct {
- expr
ElemList []Expr
+ expr
}
// [Len]Elem
ArrayType struct {
- expr
// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
Len Expr // nil means Len is ...
Elem Expr
+ expr
}
// []Elem
SliceType struct {
- expr
Elem Expr
+ expr
}
// ...Elem
DotsType struct {
- expr
Elem Expr
+ expr
}
// struct { FieldList[0] TagList[0]; FieldList[1] TagList[1]; ... }
StructType struct {
- expr
FieldList []*Field
TagList []*BasicLit // i >= len(TagList) || TagList[i] == nil means no tag for field i
+ expr
}
// Name Type
// Type
Field struct {
- node
Name *Name // nil means anonymous field/parameter (structs/parameters), or embedded interface (interfaces)
Type Expr // field names declared in a list share the same Type (identical pointers)
+ node
}
// interface { MethodList[0]; MethodList[1]; ... }
InterfaceType struct {
- expr
MethodList []*Field
+ expr
}
FuncType struct {
- expr
ParamList []*Field
ResultList []*Field
+ expr
}
// map[Key]Value
MapType struct {
- expr
Key Expr
Value Expr
+ expr
}
// chan Elem
// <-chan Elem
// chan<- Elem
ChanType struct {
- expr
Dir ChanDir // 0 means no direction
Elem Expr
+ expr
}
)
type expr struct{ node }
-func (expr) aExpr() {}
+func (*expr) aExpr() {}
type ChanDir uint
@@ -284,108 +284,108 @@ type (
}
LabeledStmt struct {
- stmt
Label *Name
Stmt Stmt
+ stmt
}
BlockStmt struct {
- stmt
Body []Stmt
+ stmt
}
ExprStmt struct {
- simpleStmt
X Expr
+ simpleStmt
}
SendStmt struct {
- simpleStmt
Chan, Value Expr // Chan <- Value
+ simpleStmt
}
DeclStmt struct {
- stmt
DeclList []Decl
+ stmt
}
AssignStmt struct {
- simpleStmt
Op Operator // 0 means no operation
Lhs, Rhs Expr
+ simpleStmt
}
BranchStmt struct {
- stmt
Tok token // TODO(gri) token values are not yet exported
Label *Name
+ stmt
}
CallStmt struct {
- stmt
Tok token // _Go, or _Defer -- TODO(gri) token values are not yet exported
Call *CallExpr
+ stmt
}
ReturnStmt struct {
- stmt
Results Expr // nil means no (explicit) results
+ stmt
}
IfStmt struct {
- stmt
Init SimpleStmt
Cond Expr
Then []Stmt
Else []Stmt
+ stmt
}
ForStmt struct {
- stmt
Init SimpleStmt // incl. *RangeClause
Cond Expr
Post SimpleStmt
Body []Stmt
+ stmt
}
SwitchStmt struct {
- stmt
Init SimpleStmt
Tag Expr
Body []*CaseClause
+ stmt
}
SelectStmt struct {
- stmt
Body []*CommClause
+ stmt
}
)
type (
RangeClause struct {
- simpleStmt
Lhs Expr // nil means no Lhs = or Lhs :=
Def bool // means :=
X Expr // range X
+ simpleStmt
}
TypeSwitchGuard struct {
- expr
// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
Lhs *Name // nil means no Lhs :=
X Expr // X.(type)
+ expr
}
CaseClause struct {
- node
Cases Expr // nil means default clause
Body []Stmt
+ node
}
CommClause struct {
- node
Comm SimpleStmt // send or receive stmt; nil means default clause
Body []Stmt
+ node
}
)
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go
index 60f050b10d..d9cc96ed54 100644
--- a/src/cmd/compile/internal/syntax/parser.go
+++ b/src/cmd/compile/internal/syntax/parser.go
@@ -517,7 +517,7 @@ func (p *parser) unaryExpr() Expr {
}
// x is not a channel type => we have a receive op
- return &Operation{expr{}, Recv, x, nil}
+ return &Operation{Op: Recv, X: x}
}
return p.pexpr(false)
@@ -850,7 +850,7 @@ func (p *parser) type_() Expr {
}
func indirect(typ Expr) Expr {
- return &Operation{expr{}, Mul, typ, nil}
+ return &Operation{Op: Mul, X: typ}
}
// tryType is like type_ but it returns nil if there was no type
@@ -1843,10 +1843,11 @@ func (p *parser) stmt() Stmt {
// return stmt
case _Break, _Continue:
+ tok := p.tok
p.next()
s := new(BranchStmt)
s.init(p)
- s.Tok = _Break
+ s.Tok = tok
if p.tok == _Name {
s.Label = p.name()
}
diff --git a/src/cmd/compile/internal/syntax/tokens.go b/src/cmd/compile/internal/syntax/tokens.go
index 9243f1e14f..ed7a643a97 100644
--- a/src/cmd/compile/internal/syntax/tokens.go
+++ b/src/cmd/compile/internal/syntax/tokens.go
@@ -68,6 +68,16 @@ const (
tokenCount
)
+const (
+ Go = _Go
+ Defer = _Defer
+
+ Break = _Break
+ Continue = _Continue
+ Fallthrough = _Fallthrough
+ Goto = _Goto
+)
+
var tokstrings = [...]string{
// source control
_EOF: "EOF",