2008-10-20 16:03:40 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2008-10-14 19:14:01 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// The AST package declares the types used to represent
|
|
|
|
// syntax trees for Go source files.
|
|
|
|
//
|
2009-03-05 18:15:36 -07:00
|
|
|
package ast
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-11-19 17:49:50 -07:00
|
|
|
import (
|
2009-03-03 19:25:07 -07:00
|
|
|
"token";
|
2009-03-11 13:52:11 -06:00
|
|
|
"scanner";
|
2008-11-19 17:49:50 -07:00
|
|
|
)
|
2008-10-14 19:14:01 -06:00
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// TODO rename Position to scanner.Position, possibly factor out
|
|
|
|
type Position scanner.Location
|
|
|
|
|
|
|
|
|
|
|
|
// TODO try to get rid of these
|
2009-01-20 15:40:40 -07:00
|
|
|
type (
|
2009-01-23 10:44:01 -07:00
|
|
|
Block struct;
|
2009-03-17 19:41:35 -06:00
|
|
|
Signature struct;
|
2008-10-14 19:14:01 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-20 18:18:48 -06:00
|
|
|
// Interfaces
|
|
|
|
//
|
|
|
|
// There are 3 main classes of nodes: Expressions and type nodes,
|
|
|
|
// statement nodes, and declaration nodes. The node names usually
|
|
|
|
// match the corresponding Go spec production names to which they
|
|
|
|
// correspond. The node fields correspond to the individual parts
|
|
|
|
// of the respective productions.
|
|
|
|
//
|
|
|
|
// Nodes contain selective position information: a position field
|
|
|
|
// marking the beginning of the corresponding source text segment
|
|
|
|
// if necessary; and specific position information for language
|
|
|
|
// constructs where comments may be found between parts of the
|
|
|
|
// construct (typically any larger, parenthesized subpart). The
|
|
|
|
// position information is needed to properly position comments
|
|
|
|
// when printing the construct.
|
2009-03-12 18:24:03 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// TODO: For comment positioning only the byte position and not
|
|
|
|
// a complete Position field is needed. May be able to trim node
|
|
|
|
// sizes a bit.
|
2009-03-12 18:24:03 -06:00
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
type (
|
|
|
|
ExprVisitor interface;
|
|
|
|
StatVisitor interface;
|
|
|
|
DeclVisitor interface;
|
|
|
|
)
|
2009-01-09 17:28:09 -07:00
|
|
|
|
2008-10-16 13:16:50 -06:00
|
|
|
|
2009-03-17 19:41:35 -06:00
|
|
|
// All expression nodes implement the Expr interface.
|
|
|
|
type Expr interface {
|
|
|
|
// For a (dynamic) node type X, calling Visit with an expression
|
|
|
|
// visitor v invokes the node-specific DoX function of the visitor.
|
|
|
|
//
|
|
|
|
Visit(v ExprVisitor);
|
|
|
|
|
|
|
|
// Pos returns the (beginning) position of the expression.
|
|
|
|
Pos() Position;
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// All statement nodes implement the Stat interface.
|
|
|
|
type Stat interface {
|
|
|
|
// For a (dynamic) node type X, calling Visit with a statement
|
|
|
|
// visitor v invokes the node-specific DoX function of the visitor.
|
|
|
|
//
|
|
|
|
Visit(v StatVisitor);
|
|
|
|
|
|
|
|
// Pos returns the (beginning) position of the statement.
|
|
|
|
Pos() Position;
|
|
|
|
}
|
2008-10-16 13:16:50 -06:00
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// All declaration nodes implement the Decl interface.
|
|
|
|
type Decl interface {
|
|
|
|
// For a (dynamic) node type X, calling Visit with a declaration
|
|
|
|
// visitor v invokes the node-specific DoX function of the visitor.
|
|
|
|
//
|
|
|
|
Visit(v DeclVisitor);
|
|
|
|
|
|
|
|
// Pos returns the (beginning) position of the declaration.
|
|
|
|
Pos() Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Comments
|
|
|
|
|
|
|
|
// A Comment node represents a single //-style or /*-style comment.
|
|
|
|
type Comment struct {
|
|
|
|
Pos_ Position; // beginning position of the comment
|
|
|
|
Text []byte; // the comment text (without '\n' for //-style comments)
|
|
|
|
EndLine int; // the line where the comment ends
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A Comments node represents a sequence of single comments
|
|
|
|
// with no other tokens and no empty lines between.
|
|
|
|
//
|
|
|
|
type Comments []*Comment
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Expressions and types
|
|
|
|
|
2009-03-17 19:41:35 -06:00
|
|
|
// An expression is represented by a tree consisting of one
|
2009-03-20 18:18:48 -06:00
|
|
|
// or more of the following concrete expression nodes.
|
2009-03-17 19:41:35 -06:00
|
|
|
//
|
|
|
|
type (
|
2009-03-20 18:18:48 -06:00
|
|
|
// A BadExpr node is a placeholder for expressions containing
|
|
|
|
// syntax errors for which no correct expression nodes can be
|
|
|
|
// created.
|
2009-03-17 19:41:35 -06:00
|
|
|
//
|
2009-02-03 18:44:01 -07:00
|
|
|
BadExpr struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Pos_ Position; // beginning position of bad expression
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An Ident node represents an identifier.
|
2009-02-03 18:44:01 -07:00
|
|
|
Ident struct {
|
2009-03-17 19:41:35 -06:00
|
|
|
Pos_ Position; // identifier position
|
2009-03-20 18:18:48 -06:00
|
|
|
Lit []byte; // identifier string (e.g. foobar)
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A BasicLit node represents a basic literal.
|
2009-03-17 19:41:35 -06:00
|
|
|
BasicLit struct {
|
|
|
|
Pos_ Position; // literal string position
|
2009-03-20 18:18:48 -06:00
|
|
|
Tok int; // literal token (INT, FLOAT, CHAR, STRING)
|
|
|
|
Lit []byte; // literal string
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A StringLit node represents a sequence of string literals.
|
2009-03-17 19:41:35 -06:00
|
|
|
StringLit struct {
|
|
|
|
Strings []*BasicLit; // sequence of strings
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A FunctionLit node represents a function literal.
|
2009-02-03 18:44:01 -07:00
|
|
|
FunctionLit struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Func Position; // position of "func" keyword
|
2009-03-17 19:41:35 -06:00
|
|
|
Typ *Signature; // function signature
|
|
|
|
Body *Block; // function body
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
2009-03-17 19:41:35 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A CompositeLit node represents a composite literal.
|
2009-03-17 19:41:35 -06:00
|
|
|
CompositeLit struct {
|
|
|
|
Typ Expr; // literal type
|
2009-03-20 18:18:48 -06:00
|
|
|
Lbrace Position; // position of "{"
|
2009-03-17 19:41:35 -06:00
|
|
|
Elts []Expr; // list of composite elements
|
2009-03-20 18:18:48 -06:00
|
|
|
Rbrace Position; // position of "}"
|
2009-03-17 19:41:35 -06:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A ParenExpr node represents a parenthesized expression.
|
|
|
|
ParenExpr struct {
|
|
|
|
Lparen Position; // position of "("
|
2009-03-17 19:41:35 -06:00
|
|
|
X Expr; // parenthesized expression
|
2009-03-20 18:18:48 -06:00
|
|
|
Rparen Position; // position of ")"
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A SelectorExpr node represents a primary expression followed by a selector.
|
|
|
|
SelectorExpr struct {
|
2009-03-17 19:41:35 -06:00
|
|
|
X Expr; // primary expression
|
|
|
|
Sel *Ident; // field selector
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An IndexExpr node represents a primary expression followed by an index.
|
|
|
|
IndexExpr struct {
|
2009-03-17 19:41:35 -06:00
|
|
|
X Expr; // primary expression
|
|
|
|
Index Expr; // index expression
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
2009-03-17 19:41:35 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A SliceExpr node represents a primary expression followed by a slice.
|
|
|
|
SliceExpr struct {
|
2009-03-17 19:41:35 -06:00
|
|
|
X Expr; // primary expression
|
2009-03-20 18:18:48 -06:00
|
|
|
Begin, End Expr; // slice range
|
2009-03-17 19:41:35 -06:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A TypeAssertExpr node represents a primary expression followed by a
|
|
|
|
// type assertion.
|
2009-03-17 19:41:35 -06:00
|
|
|
//
|
2009-03-20 18:18:48 -06:00
|
|
|
TypeAssertExpr struct {
|
2009-03-17 19:41:35 -06:00
|
|
|
X Expr; // primary expression
|
|
|
|
Typ Expr; // asserted type
|
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A CallExpr node represents a primary expression followed by an argument list.
|
|
|
|
CallExpr struct {
|
2009-03-17 19:41:35 -06:00
|
|
|
Fun Expr; // function expression
|
2009-03-20 18:18:48 -06:00
|
|
|
Lparen Position; // position of "("
|
2009-03-17 19:41:35 -06:00
|
|
|
Args []Expr; // function arguments
|
2009-03-20 18:18:48 -06:00
|
|
|
Rparen Position; // positions of ")"
|
2009-03-17 19:41:35 -06:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A StarExpr node represents an expression of the form "*" Expression.
|
|
|
|
// Semantically it could be a unary "*" expression, or a pointer type.
|
|
|
|
StarExpr struct {
|
|
|
|
Star Position; // position of "*"
|
|
|
|
X Expr; // operand
|
|
|
|
};
|
2009-03-17 19:41:35 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A UnaryExpr node represents a unary expression.
|
|
|
|
// Unary "*" expressions are represented via DerefExpr nodes.
|
|
|
|
//
|
2009-03-17 19:41:35 -06:00
|
|
|
UnaryExpr struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Pos_ Position; // token position
|
|
|
|
Tok int; // operator
|
2009-03-17 19:41:35 -06:00
|
|
|
X Expr; // operand
|
2009-02-03 18:44:01 -07:00
|
|
|
};
|
2009-02-27 16:40:17 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A BinaryExpr node represents a binary expression.
|
2009-03-17 19:41:35 -06:00
|
|
|
BinaryExpr struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
X Expr; // left operand
|
|
|
|
Pos_ Position; // token position
|
|
|
|
Tok int; // operator
|
|
|
|
Y Expr; // right operand
|
2009-03-17 19:41:35 -06:00
|
|
|
};
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// The direction of a channel type is indicated by one
|
|
|
|
// of the following constants.
|
|
|
|
//
|
2009-03-20 18:18:48 -06:00
|
|
|
type ChanDir int
|
|
|
|
const (
|
|
|
|
SEND ChanDir = 1 << iota;
|
2009-03-17 19:41:35 -06:00
|
|
|
RECV;
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A type is represented by a tree consisting of one
|
|
|
|
// or more of the following type-specific expression
|
|
|
|
// nodes.
|
|
|
|
//
|
2009-03-17 19:41:35 -06:00
|
|
|
type (
|
2009-03-20 18:18:48 -06:00
|
|
|
// An Ellipsis node stands for the "..." type in a
|
|
|
|
// parameter list or the "..." length in an array type.
|
|
|
|
//
|
2009-02-27 16:40:17 -07:00
|
|
|
Ellipsis struct { // neither a type nor an expression
|
2009-03-20 18:18:48 -06:00
|
|
|
Pos_ Position; // position of "..."
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-03-05 18:15:36 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An ArrayType node represents an array type.
|
|
|
|
ArrayType struct {
|
|
|
|
Lbrack Position; // position of "["
|
|
|
|
Len Expr; // an Ellipsis node for [...]T array types
|
|
|
|
Elt Expr; // element type
|
2009-03-05 18:15:36 -07:00
|
|
|
};
|
2009-02-27 16:40:17 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A SliceType node represents a slice type.
|
|
|
|
SliceType struct {
|
|
|
|
Lbrack Position; // position of "["
|
|
|
|
Elt Expr; // element type
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
// A Field represents a Field declaration list in a struct type,
|
|
|
|
// a method in an interface type, or a parameter declaration in
|
|
|
|
// a signature.
|
2009-02-27 16:40:17 -07:00
|
|
|
Field struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Doc Comments; // associated documentation (struct types only)
|
|
|
|
Names []*Ident; // field/method/parameter names; nil if anonymous field
|
|
|
|
Typ Expr; // field/method/parameter type
|
|
|
|
Tag Expr; // field tag; nil if no tag
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A StructType node represents a struct type.
|
2009-02-27 16:40:17 -07:00
|
|
|
StructType struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Struct, Lbrace Position; // positions of "struct" keyword, "{"
|
|
|
|
Fields []*Field; // list of field declarations; nil if forward declaration
|
|
|
|
Rbrace Position; // position of "}"
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
// Note: pointer types are represented via StarExpr nodes.
|
|
|
|
|
|
|
|
// A signature node represents the parameter and result
|
|
|
|
// sections of a function type only.
|
|
|
|
//
|
2009-02-27 16:40:17 -07:00
|
|
|
Signature struct {
|
|
|
|
Params []*Field;
|
|
|
|
Result []*Field;
|
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A FunctionType node represents a function type.
|
2009-02-27 16:40:17 -07:00
|
|
|
FunctionType struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Func Position; // position of "func" keyword
|
2009-02-27 16:40:17 -07:00
|
|
|
Sig *Signature;
|
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An InterfaceType node represents an interface type.
|
2009-02-27 16:40:17 -07:00
|
|
|
InterfaceType struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Interface, Lbrace Position; // positions of "interface" keyword, "{"
|
|
|
|
Methods []*Field; // list of methods; nil if forward declaration
|
|
|
|
Rbrace Position; // position of "}"
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A MapType node represents a map type.
|
2009-02-27 16:40:17 -07:00
|
|
|
MapType struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Map Position; // position of "map" keyword
|
2009-02-27 16:40:17 -07:00
|
|
|
Key Expr;
|
2009-03-20 18:18:48 -06:00
|
|
|
Value Expr;
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
// A ChannelType node represents a channel type.
|
2009-02-27 16:40:17 -07:00
|
|
|
ChannelType struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Pos_ Position; // position of "chan" keyword or "<-" (whichever comes first)
|
|
|
|
Dir ChanDir;
|
|
|
|
Value Expr; // value type
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-02-03 18:44:01 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Pos() implementations for all expression/type nodes.
|
|
|
|
//
|
|
|
|
func (x *BadExpr) Pos() Position { return x.Pos_; }
|
|
|
|
func (x *Ident) Pos() Position { return x.Pos_; }
|
|
|
|
func (x *BasicLit) Pos() Position { return x.Pos_; }
|
|
|
|
func (x *StringLit) Pos() Position { return x.Strings[0].Pos(); }
|
|
|
|
func (x *FunctionLit) Pos() Position { return x.Func; }
|
|
|
|
func (x *CompositeLit) Pos() Position { return x.Typ.Pos(); }
|
|
|
|
func (x *ParenExpr) Pos() Position { return x.Lparen; }
|
|
|
|
func (x *SelectorExpr) Pos() Position { return x.X.Pos(); }
|
|
|
|
func (x *IndexExpr) Pos() Position { return x.X.Pos(); }
|
|
|
|
func (x *SliceExpr) Pos() Position { return x.X.Pos(); }
|
|
|
|
func (x *TypeAssertExpr) Pos() Position { return x.X.Pos(); }
|
|
|
|
func (x *CallExpr) Pos() Position { return x.Fun.Pos(); }
|
|
|
|
func (x *StarExpr) Pos() Position { return x.Star; }
|
|
|
|
func (x *UnaryExpr) Pos() Position { return x.Pos_; }
|
|
|
|
func (x *BinaryExpr) Pos() Position { return x.X.Pos(); }
|
|
|
|
|
|
|
|
func (x *Ellipsis) Pos() Position { return x.Pos_; }
|
|
|
|
func (x *ArrayType) Pos() Position { return x.Lbrack; }
|
|
|
|
func (x *SliceType) Pos() Position { return x.Lbrack; }
|
|
|
|
func (x *StructType) Pos() Position { return x.Struct; }
|
|
|
|
func (x *FunctionType) Pos() Position { return x.Func; }
|
|
|
|
func (x *InterfaceType) Pos() Position { return x.Interface; }
|
|
|
|
func (x *MapType) Pos() Position { return x.Map; }
|
|
|
|
func (x *ChannelType) Pos() Position { return x.Pos_; }
|
|
|
|
|
|
|
|
|
|
|
|
// All expression/type nodes implement a Visit method which takes
|
|
|
|
// an ExprVisitor as argument. For a given node x of type X, and
|
|
|
|
// an implementation v of an ExprVisitor, calling x.Visit(v) will
|
|
|
|
// result in a call of v.DoX(x) (through a double-dispatch).
|
|
|
|
//
|
2009-02-04 19:28:41 -07:00
|
|
|
type ExprVisitor interface {
|
2009-03-17 19:41:35 -06:00
|
|
|
// Expressions
|
2009-02-03 18:44:01 -07:00
|
|
|
DoBadExpr(x *BadExpr);
|
|
|
|
DoIdent(x *Ident);
|
|
|
|
DoBasicLit(x *BasicLit);
|
2009-03-17 19:41:35 -06:00
|
|
|
DoStringLit(x *StringLit);
|
2009-02-03 18:44:01 -07:00
|
|
|
DoFunctionLit(x *FunctionLit);
|
2009-03-17 19:41:35 -06:00
|
|
|
DoCompositeLit(x *CompositeLit);
|
2009-03-20 18:18:48 -06:00
|
|
|
DoParenExpr(x *ParenExpr);
|
|
|
|
DoSelectorExpr(x *SelectorExpr);
|
|
|
|
DoIndexExpr(x *IndexExpr);
|
|
|
|
DoSliceExpr(x *SliceExpr);
|
|
|
|
DoTypeAssertExpr(x *TypeAssertExpr);
|
|
|
|
DoCallExpr(x *CallExpr);
|
|
|
|
DoStarExpr(x *StarExpr);
|
2009-03-17 19:41:35 -06:00
|
|
|
DoUnaryExpr(x *UnaryExpr);
|
|
|
|
DoBinaryExpr(x *BinaryExpr);
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Type expressions
|
2009-02-27 16:40:17 -07:00
|
|
|
DoEllipsis(x *Ellipsis);
|
|
|
|
DoArrayType(x *ArrayType);
|
2009-03-20 18:18:48 -06:00
|
|
|
DoSliceType(x *SliceType);
|
2009-02-27 16:40:17 -07:00
|
|
|
DoStructType(x *StructType);
|
|
|
|
DoFunctionType(x *FunctionType);
|
|
|
|
DoInterfaceType(x *InterfaceType);
|
|
|
|
DoMapType(x *MapType);
|
|
|
|
DoChannelType(x *ChannelType);
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Visit() implementations for all expression/type nodes.
|
|
|
|
//
|
2009-02-04 19:28:41 -07:00
|
|
|
func (x *BadExpr) Visit(v ExprVisitor) { v.DoBadExpr(x); }
|
|
|
|
func (x *Ident) Visit(v ExprVisitor) { v.DoIdent(x); }
|
|
|
|
func (x *BasicLit) Visit(v ExprVisitor) { v.DoBasicLit(x); }
|
2009-03-17 19:41:35 -06:00
|
|
|
func (x *StringLit) Visit(v ExprVisitor) { v.DoStringLit(x); }
|
2009-02-04 19:28:41 -07:00
|
|
|
func (x *FunctionLit) Visit(v ExprVisitor) { v.DoFunctionLit(x); }
|
2009-03-17 19:41:35 -06:00
|
|
|
func (x *CompositeLit) Visit(v ExprVisitor) { v.DoCompositeLit(x); }
|
2009-03-20 18:18:48 -06:00
|
|
|
func (x *ParenExpr) Visit(v ExprVisitor) { v.DoParenExpr(x); }
|
|
|
|
func (x *SelectorExpr) Visit(v ExprVisitor) { v.DoSelectorExpr(x); }
|
|
|
|
func (x *IndexExpr) Visit(v ExprVisitor) { v.DoIndexExpr(x); }
|
|
|
|
func (x *SliceExpr) Visit(v ExprVisitor) { v.DoSliceExpr(x); }
|
|
|
|
func (x *TypeAssertExpr) Visit(v ExprVisitor) { v.DoTypeAssertExpr(x); }
|
|
|
|
func (x *CallExpr) Visit(v ExprVisitor) { v.DoCallExpr(x); }
|
|
|
|
func (x *StarExpr) Visit(v ExprVisitor) { v.DoStarExpr(x); }
|
2009-03-17 19:41:35 -06:00
|
|
|
func (x *UnaryExpr) Visit(v ExprVisitor) { v.DoUnaryExpr(x); }
|
|
|
|
func (x *BinaryExpr) Visit(v ExprVisitor) { v.DoBinaryExpr(x); }
|
2009-02-03 18:44:01 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
func (x *Ellipsis) Visit(v ExprVisitor) { v.DoEllipsis(x); }
|
|
|
|
func (x *ArrayType) Visit(v ExprVisitor) { v.DoArrayType(x); }
|
2009-03-20 18:18:48 -06:00
|
|
|
func (x *SliceType) Visit(v ExprVisitor) { v.DoSliceType(x); }
|
2009-02-27 16:40:17 -07:00
|
|
|
func (x *StructType) Visit(v ExprVisitor) { v.DoStructType(x); }
|
|
|
|
func (x *FunctionType) Visit(v ExprVisitor) { v.DoFunctionType(x); }
|
|
|
|
func (x *InterfaceType) Visit(v ExprVisitor) { v.DoInterfaceType(x); }
|
|
|
|
func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); }
|
|
|
|
func (x *ChannelType) Visit(v ExprVisitor) { v.DoChannelType(x); }
|
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
|
2009-02-05 12:05:02 -07:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Blocks
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
// A Block represents syntactic constructs of the form:
|
2009-02-05 12:05:02 -07:00
|
|
|
//
|
|
|
|
// "{" StatementList "}"
|
|
|
|
// ":" StatementList
|
2009-03-20 18:18:48 -06:00
|
|
|
//
|
2009-02-05 12:05:02 -07:00
|
|
|
type Block struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Pos_ Position;
|
2009-03-11 13:52:11 -06:00
|
|
|
Tok int;
|
2009-03-20 18:18:48 -06:00
|
|
|
List []Stat;
|
|
|
|
Rparen Position; // position of closing "}" if present
|
2009-02-05 12:05:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A statement is represented by a tree consisting of one
|
|
|
|
// or more of the following concrete statement nodes.
|
|
|
|
//
|
2009-02-04 19:28:41 -07:00
|
|
|
type (
|
2009-03-20 18:18:48 -06:00
|
|
|
// A BadStat node is a placeholder for statements containing
|
|
|
|
// syntax errors for which no correct statement nodes can be
|
|
|
|
// created.
|
|
|
|
//
|
|
|
|
BadStat struct {
|
|
|
|
Pos_ Position; // beginning position of bad statement
|
|
|
|
};
|
2009-02-04 19:28:41 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A DeclStat node represents a declaration in a statement list.
|
|
|
|
DeclStat struct {
|
|
|
|
Decl Decl;
|
2009-02-04 19:28:41 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
// An EmptyStat node represents an empty statement.
|
|
|
|
// The "position" of the empty statement is the position
|
|
|
|
// of the immediately preceeding semicolon.
|
|
|
|
//
|
|
|
|
EmptyStat struct {
|
|
|
|
Semicolon Position; // position of preceeding ";"
|
2009-02-04 19:28:41 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A LabeledStat node represents a labeled statement.
|
2009-03-13 17:59:51 -06:00
|
|
|
LabeledStat struct {
|
2009-02-04 19:28:41 -07:00
|
|
|
Label *Ident;
|
2009-03-13 17:59:51 -06:00
|
|
|
Stat Stat;
|
2009-02-04 19:28:41 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An ExprStat node represents a (stand-alone) expression
|
|
|
|
// in a statement list.
|
|
|
|
//
|
|
|
|
ExprStat struct {
|
|
|
|
X Expr; // expression
|
2009-02-04 19:28:41 -07:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An IncDecStat node represents an increment or decrement statement.
|
|
|
|
IncDecStat struct {
|
|
|
|
X Expr;
|
|
|
|
Tok int; // INC or DEC
|
2009-02-04 19:28:41 -07:00
|
|
|
};
|
2009-02-05 12:05:02 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An AssignmentStat node represents an assignment or
|
|
|
|
// a short variable declaration.
|
2009-03-16 21:29:31 -06:00
|
|
|
AssignmentStat struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Lhs []Expr;
|
|
|
|
Pos_ Position; // token position
|
|
|
|
Tok int; // assignment token, DEFINE
|
|
|
|
Rhs []Expr;
|
2009-03-16 21:29:31 -06:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A GoStat node represents a go statement.
|
|
|
|
GoStat struct {
|
|
|
|
Go Position; // position of "go" keyword
|
|
|
|
Call Expr;
|
2009-03-16 21:29:31 -06:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A DeferStat node represents a defer statement.
|
|
|
|
DeferStat struct {
|
|
|
|
Defer Position; // position of "defer" keyword
|
|
|
|
Call Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A ReturnStat node represents a return statement.
|
|
|
|
ReturnStat struct {
|
|
|
|
Return Position; // position of "return" keyword
|
|
|
|
Results []Expr;
|
2009-03-16 21:29:31 -06:00
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A ControlFlowStat node represents a break, continue, goto,
|
|
|
|
// or fallthrough statement.
|
|
|
|
//
|
|
|
|
ControlFlowStat struct {
|
|
|
|
Pos_ Position; // position of keyword
|
|
|
|
Tok int; // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
|
|
|
|
Label *Ident;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A CompositeStat node represents a braced statement list.
|
2009-02-05 12:05:02 -07:00
|
|
|
CompositeStat struct {
|
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An IfStat node represents an if statement.
|
2009-02-04 19:28:41 -07:00
|
|
|
IfStat struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
If Position; // position of "if" keyword
|
2009-02-04 19:28:41 -07:00
|
|
|
Init Stat;
|
|
|
|
Cond Expr;
|
|
|
|
Body *Block;
|
|
|
|
Else Stat;
|
|
|
|
};
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A CaseClause represents a case of an expression switch statement.
|
2009-02-05 12:05:02 -07:00
|
|
|
CaseClause struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Case Position; // position of "case" or "default" keyword
|
2009-03-16 21:29:31 -06:00
|
|
|
Values []Expr; // nil means default case
|
2009-02-05 12:05:02 -07:00
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A SwitchStat node represents an expression switch statement.
|
2009-02-04 19:28:41 -07:00
|
|
|
SwitchStat struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Switch Position; // position of "switch" keyword
|
2009-02-04 19:28:41 -07:00
|
|
|
Init Stat;
|
|
|
|
Tag Expr;
|
2009-03-20 18:18:48 -06:00
|
|
|
Body *Block; // CaseClauses only
|
|
|
|
};
|
|
|
|
|
|
|
|
// A TypeCaseClause represents a case of a type switch statement.
|
|
|
|
TypeCaseClause struct {
|
|
|
|
Case Position; // position of "case" or "default" keyword
|
|
|
|
Typ Expr; // nil means default case
|
2009-02-04 19:28:41 -07:00
|
|
|
Body *Block;
|
|
|
|
};
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An TypeSwitchStat node represents a type switch statement.
|
|
|
|
TypeSwitchStat struct {
|
|
|
|
Switch Position; // position of "switch" keyword
|
|
|
|
Init Stat;
|
|
|
|
Assign Stat; // x := y.(type)
|
|
|
|
Body *Block; // TypeCaseClauses only
|
|
|
|
};
|
|
|
|
|
|
|
|
// A CommClause node represents a case of a select statement.
|
2009-03-16 21:29:31 -06:00
|
|
|
CommClause struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Case Position; // position of "case" or "default" keyword
|
|
|
|
Tok int; // ASSIGN, DEFINE (valid only if Lhs != nil)
|
2009-03-16 21:29:31 -06:00
|
|
|
Lhs, Rhs Expr; // Rhs == nil means default case
|
|
|
|
Body *Block;
|
|
|
|
};
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// An SelectStat node represents a select statement.
|
2009-02-04 19:28:41 -07:00
|
|
|
SelectStat struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Select Position; // position of "select" keyword
|
|
|
|
Body *Block; // CommClauses only
|
2009-02-04 19:28:41 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
// A ForStat represents a for statement.
|
|
|
|
ForStat struct {
|
|
|
|
For Position; // position of "for" keyword
|
|
|
|
Init Stat;
|
|
|
|
Cond Expr;
|
|
|
|
Post Stat;
|
|
|
|
Body *Block;
|
2009-03-16 21:29:31 -06:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
// A RangeStat represents a for statement with a range clause.
|
|
|
|
RangeStat struct {
|
|
|
|
For Position; // position of "for" keyword
|
|
|
|
Range Stat;
|
|
|
|
Body *Block;
|
2009-02-12 17:06:21 -07:00
|
|
|
};
|
2009-02-04 19:28:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Pos() implementations for all statement nodes.
|
|
|
|
//
|
|
|
|
func (s *BadStat) Pos() Position { return s.Pos_; }
|
|
|
|
func (s *DeclStat) Pos() Position { return s.Decl.Pos(); }
|
|
|
|
func (s *EmptyStat) Pos() Position { return s.Semicolon; }
|
|
|
|
func (s *LabeledStat) Pos() Position { return s.Label.Pos(); }
|
|
|
|
func (s *ExprStat) Pos() Position { return s.X.Pos(); }
|
|
|
|
func (s *IncDecStat) Pos() Position { return s.X.Pos(); }
|
|
|
|
func (s *AssignmentStat) Pos() Position { return s.Lhs[0].Pos(); }
|
|
|
|
func (s *GoStat) Pos() Position { return s.Go; }
|
|
|
|
func (s *DeferStat) Pos() Position { return s.Defer; }
|
|
|
|
func (s *ReturnStat) Pos() Position { return s.Return; }
|
|
|
|
func (s *ControlFlowStat) Pos() Position { return s.Pos_; }
|
|
|
|
func (s *CompositeStat) Pos() Position { return s.Body.Pos_; }
|
|
|
|
func (s *IfStat) Pos() Position { return s.If; }
|
|
|
|
func (s *CaseClause) Pos() Position { return s.Case; }
|
|
|
|
func (s *SwitchStat) Pos() Position { return s.Switch; }
|
|
|
|
func (s *TypeCaseClause) Pos() Position { return s.Case; }
|
|
|
|
func (s *TypeSwitchStat) Pos() Position { return s.Switch; }
|
|
|
|
func (s *CommClause) Pos() Position { return s.Case; }
|
|
|
|
func (s *SelectStat) Pos() Position { return s.Select; }
|
|
|
|
func (s *ForStat) Pos() Position { return s.For; }
|
|
|
|
func (s *RangeStat) Pos() Position { return s.For; }
|
|
|
|
|
|
|
|
|
|
|
|
// All statement nodes implement a Visit method which takes
|
|
|
|
// a StatVisitor as argument. For a given node x of type X, and
|
|
|
|
// an implementation v of a StatVisitor, calling x.Visit(v) will
|
|
|
|
// result in a call of v.DoX(x) (through a double-dispatch).
|
|
|
|
//
|
2009-02-04 19:28:41 -07:00
|
|
|
type StatVisitor interface {
|
|
|
|
DoBadStat(s *BadStat);
|
2009-03-20 18:18:48 -06:00
|
|
|
DoDeclStat(s *DeclStat);
|
|
|
|
DoEmptyStat(s *EmptyStat);
|
2009-03-13 17:59:51 -06:00
|
|
|
DoLabeledStat(s *LabeledStat);
|
2009-03-20 18:18:48 -06:00
|
|
|
DoExprStat(s *ExprStat);
|
2009-03-16 21:29:31 -06:00
|
|
|
DoIncDecStat(s *IncDecStat);
|
2009-03-20 18:18:48 -06:00
|
|
|
DoAssignmentStat(s *AssignmentStat);
|
|
|
|
DoGoStat(s *GoStat);
|
|
|
|
DoDeferStat(s *DeferStat);
|
|
|
|
DoReturnStat(s *ReturnStat);
|
|
|
|
DoControlFlowStat(s *ControlFlowStat);
|
2009-02-05 12:05:02 -07:00
|
|
|
DoCompositeStat(s *CompositeStat);
|
2009-02-04 19:28:41 -07:00
|
|
|
DoIfStat(s *IfStat);
|
2009-02-05 12:05:02 -07:00
|
|
|
DoCaseClause(s *CaseClause);
|
2009-02-04 19:28:41 -07:00
|
|
|
DoSwitchStat(s *SwitchStat);
|
2009-03-20 18:18:48 -06:00
|
|
|
DoTypeCaseClause(s *TypeCaseClause);
|
|
|
|
DoTypeSwitchStat(s *TypeSwitchStat);
|
2009-03-16 21:29:31 -06:00
|
|
|
DoCommClause(s *CommClause);
|
2009-02-04 19:28:41 -07:00
|
|
|
DoSelectStat(s *SelectStat);
|
2009-03-20 18:18:48 -06:00
|
|
|
DoForStat(s *ForStat);
|
|
|
|
DoRangeStat(s *RangeStat);
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Visit() implementations for all statement nodes.
|
|
|
|
//
|
2009-02-04 19:28:41 -07:00
|
|
|
func (s *BadStat) Visit(v StatVisitor) { v.DoBadStat(s); }
|
2009-03-20 18:18:48 -06:00
|
|
|
func (s *DeclStat) Visit(v StatVisitor) { v.DoDeclStat(s); }
|
|
|
|
func (s *EmptyStat) Visit(v StatVisitor) { v.DoEmptyStat(s); }
|
2009-03-13 17:59:51 -06:00
|
|
|
func (s *LabeledStat) Visit(v StatVisitor) { v.DoLabeledStat(s); }
|
2009-03-20 18:18:48 -06:00
|
|
|
func (s *ExprStat) Visit(v StatVisitor) { v.DoExprStat(s); }
|
2009-03-16 21:29:31 -06:00
|
|
|
func (s *IncDecStat) Visit(v StatVisitor) { v.DoIncDecStat(s); }
|
2009-03-20 18:18:48 -06:00
|
|
|
func (s *AssignmentStat) Visit(v StatVisitor) { v.DoAssignmentStat(s); }
|
|
|
|
func (s *GoStat) Visit(v StatVisitor) { v.DoGoStat(s); }
|
|
|
|
func (s *DeferStat) Visit(v StatVisitor) { v.DoDeferStat(s); }
|
|
|
|
func (s *ReturnStat) Visit(v StatVisitor) { v.DoReturnStat(s); }
|
|
|
|
func (s *ControlFlowStat) Visit(v StatVisitor) { v.DoControlFlowStat(s); }
|
2009-02-05 12:05:02 -07:00
|
|
|
func (s *CompositeStat) Visit(v StatVisitor) { v.DoCompositeStat(s); }
|
2009-02-04 19:28:41 -07:00
|
|
|
func (s *IfStat) Visit(v StatVisitor) { v.DoIfStat(s); }
|
2009-02-05 12:05:02 -07:00
|
|
|
func (s *CaseClause) Visit(v StatVisitor) { v.DoCaseClause(s); }
|
2009-02-04 19:28:41 -07:00
|
|
|
func (s *SwitchStat) Visit(v StatVisitor) { v.DoSwitchStat(s); }
|
2009-03-20 18:18:48 -06:00
|
|
|
func (s *TypeCaseClause) Visit(v StatVisitor) { v.DoTypeCaseClause(s); }
|
|
|
|
func (s *TypeSwitchStat) Visit(v StatVisitor) { v.DoTypeSwitchStat(s); }
|
2009-03-16 21:29:31 -06:00
|
|
|
func (s *CommClause) Visit(v StatVisitor) { v.DoCommClause(s); }
|
2009-02-04 19:28:41 -07:00
|
|
|
func (s *SelectStat) Visit(v StatVisitor) { v.DoSelectStat(s); }
|
2009-03-20 18:18:48 -06:00
|
|
|
func (s *ForStat) Visit(v StatVisitor) { v.DoForStat(s); }
|
|
|
|
func (s *RangeStat) Visit(v StatVisitor) { v.DoRangeStat(s); }
|
2009-02-04 19:28:41 -07:00
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// A declaration is represented by one of the following declaration nodes.
|
|
|
|
//
|
|
|
|
type (
|
|
|
|
// A BadDecl node is a placeholder for declarations containing
|
|
|
|
// syntax errors for which no correct declaration nodes can be
|
|
|
|
// created.
|
|
|
|
//
|
2009-02-27 16:40:17 -07:00
|
|
|
BadDecl struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Pos_ Position; // beginning position of bad declaration
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
ImportDecl struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Doc Comments; // associated documentation
|
|
|
|
Import Position; // position of "import" keyword
|
|
|
|
Name *Ident; // local package name or nil
|
|
|
|
Path *StringLit; // package path
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
ConstDecl struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Doc Comments; // associated documentation
|
|
|
|
Const Position; // position of "const" keyword
|
2009-03-13 17:59:51 -06:00
|
|
|
Names []*Ident;
|
2009-03-20 18:18:48 -06:00
|
|
|
Typ Expr; // constant type or nil
|
2009-03-16 21:29:31 -06:00
|
|
|
Values []Expr;
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
TypeDecl struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Doc Comments; // associated documentation
|
|
|
|
Type Position; // position of "type" keyword
|
2009-03-13 17:59:51 -06:00
|
|
|
Name *Ident;
|
2009-02-27 16:40:17 -07:00
|
|
|
Typ Expr;
|
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
VarDecl struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Doc Comments; // associated documentation
|
|
|
|
Var Position; // position of "var" keyword
|
2009-03-13 17:59:51 -06:00
|
|
|
Names []*Ident;
|
2009-03-20 18:18:48 -06:00
|
|
|
Typ Expr; // variable type or nil
|
2009-03-16 21:29:31 -06:00
|
|
|
Values []Expr;
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
FuncDecl struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Doc Comments; // associated documentation
|
|
|
|
Func Position; // position of "func" keyword
|
|
|
|
Recv *Field; // receiver (methods) or nil (functions)
|
|
|
|
Name *Ident; // function/method name
|
|
|
|
Sig *Signature; // parameters and results
|
|
|
|
Body *Block; // function body or nil (forward declaration)
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
DeclList struct {
|
2009-03-20 18:18:48 -06:00
|
|
|
Doc Comments; // associated documentation
|
|
|
|
Pos_ Position; // position of token
|
|
|
|
Tok int; // IMPORT, CONST, VAR, TYPE
|
|
|
|
Lparen Position; // position of '('
|
|
|
|
List []Decl; // the list of parenthesized declarations
|
|
|
|
Rparen Position; // position of ')'
|
2009-02-27 16:40:17 -07:00
|
|
|
};
|
|
|
|
)
|
2008-10-14 19:14:01 -06:00
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Pos() implementations for all declaration nodes.
|
|
|
|
//
|
|
|
|
func (d *BadDecl) Pos() Position { return d.Pos_; }
|
|
|
|
func (d *ImportDecl) Pos() Position { return d.Import; }
|
|
|
|
func (d *ConstDecl) Pos() Position { return d.Const; }
|
|
|
|
func (d *TypeDecl) Pos() Position { return d.Type; }
|
|
|
|
func (d *VarDecl) Pos() Position { return d.Var; }
|
|
|
|
func (d *FuncDecl) Pos() Position { return d.Func; }
|
|
|
|
func (d *DeclList) Pos() Position { return d.Lparen; }
|
|
|
|
|
|
|
|
|
|
|
|
// All declaration nodes implement a Visit method which takes
|
|
|
|
// a DeclVisitor as argument. For a given node x of type X, and
|
|
|
|
// an implementation v of a DeclVisitor, calling x.Visit(v) will
|
|
|
|
// result in a call of v.DoX(x) (through a double-dispatch).
|
|
|
|
//
|
2009-02-27 16:40:17 -07:00
|
|
|
type DeclVisitor interface {
|
|
|
|
DoBadDecl(d *BadDecl);
|
|
|
|
DoImportDecl(d *ImportDecl);
|
|
|
|
DoConstDecl(d *ConstDecl);
|
|
|
|
DoTypeDecl(d *TypeDecl);
|
|
|
|
DoVarDecl(d *VarDecl);
|
|
|
|
DoFuncDecl(d *FuncDecl);
|
|
|
|
DoDeclList(d *DeclList);
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Visit() implementations for all declaration nodes.
|
|
|
|
//
|
2009-02-27 16:40:17 -07:00
|
|
|
func (d *BadDecl) Visit(v DeclVisitor) { v.DoBadDecl(d); }
|
|
|
|
func (d *ImportDecl) Visit(v DeclVisitor) { v.DoImportDecl(d); }
|
|
|
|
func (d *ConstDecl) Visit(v DeclVisitor) { v.DoConstDecl(d); }
|
|
|
|
func (d *TypeDecl) Visit(v DeclVisitor) { v.DoTypeDecl(d); }
|
|
|
|
func (d *VarDecl) Visit(v DeclVisitor) { v.DoVarDecl(d); }
|
|
|
|
func (d *FuncDecl) Visit(v DeclVisitor) { v.DoFuncDecl(d); }
|
|
|
|
func (d *DeclList) Visit(v DeclVisitor) { v.DoDeclList(d); }
|
2008-10-17 17:19:31 -06:00
|
|
|
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-20 18:18:48 -06:00
|
|
|
// Packages
|
|
|
|
|
|
|
|
// A Package node represents the root node of an AST.
|
|
|
|
type Package struct {
|
|
|
|
Doc Comments; // associated documentation
|
|
|
|
Package Position; // position of "package" keyword
|
|
|
|
Name *Ident; // package name
|
|
|
|
Decls []Decl; // top-level declarations
|
|
|
|
Comments []*Comment; // list of unassociated comments
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|