Commit f96099db authored by Robert Griesemer's avatar Robert Griesemer

Cleanups:

- remove visitor pattern interface (not used)
- added non-exported "branding" methods to enforce
  node hierarchy

R=rsc
DELTA=174  (13 added, 92 deleted, 69 changed)
OCL=33838
CL=33963
parent ea62b344
...@@ -31,44 +31,32 @@ import ( ...@@ -31,44 +31,32 @@ import (
// That position information is needed to properly position comments // That position information is needed to properly position comments
// when printing the construct. // when printing the construct.
// TODO: For comment positioning only the byte position and not
// a complete token.Position field is needed. May be able to trim // All node types implement the Node interface.
// node sizes a bit. type Node interface {
// Pos returns the (beginning) position of the node.
Pos() token.Position;
}
// All expression nodes implement the Expr interface. // All expression nodes implement the Expr interface.
type Expr interface { type Expr interface {
// For a (dynamic) node type X, calling Visit with an expression Node;
// visitor v invokes the node-specific DoX function of the visitor. exprNode();
//
Visit(v ExprVisitor);
// Pos returns the (beginning) position of the expression.
Pos() token.Position;
} }
// All statement nodes implement the Stmt interface. // All statement nodes implement the Stmt interface.
type Stmt interface { type Stmt interface {
// For a (dynamic) node type X, calling Visit with a statement Node;
// visitor v invokes the node-specific DoX function of the visitor. stmtNode();
//
Visit(v StmtVisitor);
// Pos returns the (beginning) position of the statement.
Pos() token.Position;
} }
// All declaration nodes implement the Decl interface. // All declaration nodes implement the Decl interface.
type Decl interface { type Decl interface {
// For a (dynamic) node type X, calling Visit with a declaration Node;
// visitor v invokes the node-specific DoX function of the visitor. declNode();
//
Visit(v DeclVisitor);
// Pos returns the (beginning) position of the declaration.
Pos() token.Position;
} }
...@@ -329,71 +317,34 @@ func (x *BinaryExpr) Pos() token.Position { return x.X.Pos(); } ...@@ -329,71 +317,34 @@ func (x *BinaryExpr) Pos() token.Position { return x.X.Pos(); }
func (x *KeyValueExpr) Pos() token.Position { return x.Key.Pos(); } func (x *KeyValueExpr) Pos() token.Position { return x.Key.Pos(); }
// All expression/type nodes implement a Visit method which takes // exprNode() ensures that only expression/type nodes can be
// an ExprVisitor as argument. For a given node x of type X, and // assigned to an ExprNode.
// an implementation v of an ExprVisitor, calling x.Visit(v) will func (x *BadExpr) exprNode() {}
// result in a call of v.DoX(x) (through a double-dispatch). func (x *Ident) exprNode() {}
// func (x *Ellipsis) exprNode() {}
type ExprVisitor interface { func (x *IntLit) exprNode() {}
// Expressions func (x *FloatLit) exprNode() {}
DoBadExpr(x *BadExpr); func (x *CharLit) exprNode() {}
DoIdent(x *Ident); func (x *StringLit) exprNode() {}
DoIntLit(x *IntLit); func (x *StringList) exprNode() {}
DoFloatLit(x *FloatLit); func (x *FuncLit) exprNode() {}
DoCharLit(x *CharLit); func (x *CompositeLit) exprNode() {}
DoStringLit(x *StringLit); func (x *ParenExpr) exprNode() {}
DoStringList(x *StringList); func (x *SelectorExpr) exprNode() {}
DoFuncLit(x *FuncLit); func (x *IndexExpr) exprNode() {}
DoCompositeLit(x *CompositeLit); func (x *TypeAssertExpr) exprNode() {}
DoParenExpr(x *ParenExpr); func (x *CallExpr) exprNode() {}
DoSelectorExpr(x *SelectorExpr); func (x *StarExpr) exprNode() {}
DoIndexExpr(x *IndexExpr); func (x *UnaryExpr) exprNode() {}
DoTypeAssertExpr(x *TypeAssertExpr); func (x *BinaryExpr) exprNode() {}
DoCallExpr(x *CallExpr); func (x *KeyValueExpr) exprNode() {}
DoStarExpr(x *StarExpr);
DoUnaryExpr(x *UnaryExpr); func (x *ArrayType) exprNode() {}
DoBinaryExpr(x *BinaryExpr); func (x *StructType) exprNode() {}
DoKeyValueExpr(x *KeyValueExpr); func (x *FuncType) exprNode() {}
func (x *InterfaceType) exprNode() {}
// Type expressions func (x *MapType) exprNode() {}
DoEllipsis(x *Ellipsis); func (x *ChanType) exprNode() {}
DoArrayType(x *ArrayType);
DoStructType(x *StructType);
DoFuncType(x *FuncType);
DoInterfaceType(x *InterfaceType);
DoMapType(x *MapType);
DoChanType(x *ChanType);
}
// Visit() implementations for all expression/type nodes.
//
func (x *BadExpr) Visit(v ExprVisitor) { v.DoBadExpr(x); }
func (x *Ident) Visit(v ExprVisitor) { v.DoIdent(x); }
func (x *Ellipsis) Visit(v ExprVisitor) { v.DoEllipsis(x); }
func (x *IntLit) Visit(v ExprVisitor) { v.DoIntLit(x); }
func (x *FloatLit) Visit(v ExprVisitor) { v.DoFloatLit(x); }
func (x *CharLit) Visit(v ExprVisitor) { v.DoCharLit(x); }
func (x *StringLit) Visit(v ExprVisitor) { v.DoStringLit(x); }
func (x *StringList) Visit(v ExprVisitor) { v.DoStringList(x); }
func (x *FuncLit) Visit(v ExprVisitor) { v.DoFuncLit(x); }
func (x *CompositeLit) Visit(v ExprVisitor) { v.DoCompositeLit(x); }
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 *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); }
func (x *UnaryExpr) Visit(v ExprVisitor) { v.DoUnaryExpr(x); }
func (x *BinaryExpr) Visit(v ExprVisitor) { v.DoBinaryExpr(x); }
func (x *KeyValueExpr) Visit(v ExprVisitor) { v.DoKeyValueExpr(x); }
func (x *ArrayType) Visit(v ExprVisitor) { v.DoArrayType(x); }
func (x *StructType) Visit(v ExprVisitor) { v.DoStructType(x); }
func (x *FuncType) Visit(v ExprVisitor) { v.DoFuncType(x); }
func (x *InterfaceType) Visit(v ExprVisitor) { v.DoInterfaceType(x); }
func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); }
func (x *ChanType) Visit(v ExprVisitor) { v.DoChanType(x); }
// IsExported returns whether name is an exported Go symbol // IsExported returns whether name is an exported Go symbol
...@@ -591,59 +542,30 @@ func (s *IncDecStmt) Pos() token.Position { return s.X.Pos(); } ...@@ -591,59 +542,30 @@ func (s *IncDecStmt) Pos() token.Position { return s.X.Pos(); }
func (s *AssignStmt) Pos() token.Position { return s.Lhs[0].Pos(); } func (s *AssignStmt) Pos() token.Position { return s.Lhs[0].Pos(); }
// All statement nodes implement a Visit method which takes // stmtNode() ensures that only statement nodes can be
// a StmtVisitor as argument. For a given node x of type X, and // assigned to a StmtNode.
// an implementation v of a StmtVisitor, calling x.Visit(v) will
// result in a call of v.DoX(x) (through a double-dispatch).
// //
type StmtVisitor interface { func (s *BadStmt) stmtNode() {}
DoBadStmt(s *BadStmt); func (s *DeclStmt) stmtNode() {}
DoDeclStmt(s *DeclStmt); func (s *EmptyStmt) stmtNode() {}
DoEmptyStmt(s *EmptyStmt); func (s *LabeledStmt) stmtNode() {}
DoLabeledStmt(s *LabeledStmt); func (s *ExprStmt) stmtNode() {}
DoExprStmt(s *ExprStmt); func (s *IncDecStmt) stmtNode() {}
DoIncDecStmt(s *IncDecStmt); func (s *AssignStmt) stmtNode() {}
DoAssignStmt(s *AssignStmt); func (s *GoStmt) stmtNode() {}
DoGoStmt(s *GoStmt); func (s *DeferStmt) stmtNode() {}
DoDeferStmt(s *DeferStmt); func (s *ReturnStmt) stmtNode() {}
DoReturnStmt(s *ReturnStmt); func (s *BranchStmt) stmtNode() {}
DoBranchStmt(s *BranchStmt); func (s *BlockStmt) stmtNode() {}
DoBlockStmt(s *BlockStmt); func (s *IfStmt) stmtNode() {}
DoIfStmt(s *IfStmt); func (s *CaseClause) stmtNode() {}
DoCaseClause(s *CaseClause); func (s *SwitchStmt) stmtNode() {}
DoSwitchStmt(s *SwitchStmt); func (s *TypeCaseClause) stmtNode() {}
DoTypeCaseClause(s *TypeCaseClause); func (s *TypeSwitchStmt) stmtNode() {}
DoTypeSwitchStmt(s *TypeSwitchStmt); func (s *CommClause) stmtNode() {}
DoCommClause(s *CommClause); func (s *SelectStmt) stmtNode() {}
DoSelectStmt(s *SelectStmt); func (s *ForStmt) stmtNode() {}
DoForStmt(s *ForStmt); func (s *RangeStmt) stmtNode() {}
DoRangeStmt(s *RangeStmt);
}
// Visit() implementations for all statement nodes.
//
func (s *BadStmt) Visit(v StmtVisitor) { v.DoBadStmt(s); }
func (s *DeclStmt) Visit(v StmtVisitor) { v.DoDeclStmt(s); }
func (s *EmptyStmt) Visit(v StmtVisitor) { v.DoEmptyStmt(s); }
func (s *LabeledStmt) Visit(v StmtVisitor) { v.DoLabeledStmt(s); }
func (s *ExprStmt) Visit(v StmtVisitor) { v.DoExprStmt(s); }
func (s *IncDecStmt) Visit(v StmtVisitor) { v.DoIncDecStmt(s); }
func (s *AssignStmt) Visit(v StmtVisitor) { v.DoAssignStmt(s); }
func (s *GoStmt) Visit(v StmtVisitor) { v.DoGoStmt(s); }
func (s *DeferStmt) Visit(v StmtVisitor) { v.DoDeferStmt(s); }
func (s *ReturnStmt) Visit(v StmtVisitor) { v.DoReturnStmt(s); }
func (s *BranchStmt) Visit(v StmtVisitor) { v.DoBranchStmt(s); }
func (s *BlockStmt) Visit(v StmtVisitor) { v.DoBlockStmt(s); }
func (s *IfStmt) Visit(v StmtVisitor) { v.DoIfStmt(s); }
func (s *CaseClause) Visit(v StmtVisitor) { v.DoCaseClause(s); }
func (s *SwitchStmt) Visit(v StmtVisitor) { v.DoSwitchStmt(s); }
func (s *TypeCaseClause) Visit(v StmtVisitor) { v.DoTypeCaseClause(s); }
func (s *TypeSwitchStmt) Visit(v StmtVisitor) { v.DoTypeSwitchStmt(s); }
func (s *CommClause) Visit(v StmtVisitor) { v.DoCommClause(s); }
func (s *SelectStmt) Visit(v StmtVisitor) { v.DoSelectStmt(s); }
func (s *ForStmt) Visit(v StmtVisitor) { v.DoForStmt(s); }
func (s *RangeStmt) Visit(v StmtVisitor) { v.DoRangeStmt(s); }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -654,7 +576,9 @@ func (s *RangeStmt) Visit(v StmtVisitor) { v.DoRangeStmt(s); } ...@@ -654,7 +576,9 @@ func (s *RangeStmt) Visit(v StmtVisitor) { v.DoRangeStmt(s); }
// //
type ( type (
// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec. // The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
Spec interface {}; Spec interface {
specNode();
};
// An ImportSpec node represents a single package import. // An ImportSpec node represents a single package import.
ImportSpec struct { ImportSpec struct {
...@@ -684,6 +608,14 @@ type ( ...@@ -684,6 +608,14 @@ type (
) )
// specNode() ensures that only spec nodes can be
// assigned to a Spec.
//
func (s *ImportSpec) specNode() {}
func (s *ValueSpec) specNode() {}
func (s *TypeSpec) specNode() {}
// A declaration is represented by one of the following declaration nodes. // A declaration is represented by one of the following declaration nodes.
// //
type ( type (
...@@ -730,23 +662,12 @@ type ( ...@@ -730,23 +662,12 @@ type (
func (d *FuncDecl) Pos() token.Position { return d.Type.Pos(); } func (d *FuncDecl) Pos() token.Position { return d.Type.Pos(); }
// All declaration nodes implement a Visit method which takes // declNode() ensures that only declaration nodes can be
// a DeclVisitor as argument. For a given node x of type X, and // assigned to a DeclNode.
// an implementation v of a DeclVisitor, calling x.Visit(v) will
// result in a call of v.DoX(x) (through a double-dispatch).
//
type DeclVisitor interface {
DoBadDecl(d *BadDecl);
DoGenDecl(d *GenDecl);
DoFuncDecl(d *FuncDecl);
}
// Visit() implementations for all declaration nodes.
// //
func (d *BadDecl) Visit(v DeclVisitor) { v.DoBadDecl(d); } func (d *BadDecl) declNode() {}
func (d *GenDecl) Visit(v DeclVisitor) { v.DoGenDecl(d); } func (d *GenDecl) declNode() {}
func (d *FuncDecl) Visit(v DeclVisitor) { v.DoFuncDecl(d); } func (d *FuncDecl) declNode() {}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
......
...@@ -1793,7 +1793,7 @@ func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction, getSemi ...@@ -1793,7 +1793,7 @@ func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction, getSemi
// convert vector // convert vector
specs := make([]ast.Spec, list.Len()); specs := make([]ast.Spec, list.Len());
for i := 0; i < list.Len(); i++ { for i := 0; i < list.Len(); i++ {
specs[i] = list.At(i); specs[i] = list.At(i).(ast.Spec);
} }
return &ast.GenDecl{doc, pos, keyword, lparen, specs, rparen}, gotSemi; return &ast.GenDecl{doc, pos, keyword, lparen, specs, rparen}, gotSemi;
......
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