From 5da7e5a1a8fb12a9a131434e45af1ebfbb44f4d2 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 8 Jul 2013 18:02:50 -0400 Subject: [PATCH] go.tools/ssa: give consistent positions for CanonicalPos(Literal). (There are only seven ast.Expr types that can be constant.) R=gri CC=golang-dev https://golang.org/cl/10766044 --- ssa/source.go | 19 ++++++++++--------- ssa/ssa.go | 3 ++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ssa/source.go b/ssa/source.go index 7f09b92d..09daf616 100644 --- a/ssa/source.go +++ b/ssa/source.go @@ -169,21 +169,24 @@ func CanonicalPos(n ast.Node) token.Pos { return CanonicalPos(n.X) case *ast.CallExpr: - // f(x): *Call, *Go, *Defer. - // T(x): *ChangeType, *Convert, *MakeInterface, *ChangeInterface. + // f(x): *Call, *Go, *Defer, *Literal (e.g. len) + // T(x): *ChangeType, *Convert, *MakeInterface, *ChangeInterface, *Literal. // make(): *MakeMap, *MakeChan, *MakeSlice. // new(): *Alloc. // panic(): *Panic. return n.Lparen + case *ast.BasicLit: + return n.ValuePos // *Literal + case *ast.Ident: - return n.NamePos // *Parameter, *Alloc, *Capture + return n.NamePos // *Parameter, *Alloc, *Capture, *Literal case *ast.TypeAssertExpr: return n.Lparen // *ChangeInterface or *TypeAssertExpr case *ast.SelectorExpr: - return n.Sel.NamePos // *MakeClosure, *Field or *FieldAddr + return n.Sel.NamePos // *MakeClosure, *Field, *FieldAddr, *Literal case *ast.FuncLit: return n.Type.Func // *Function or *MakeClosure @@ -192,10 +195,10 @@ func CanonicalPos(n ast.Node) token.Pos { return n.Lbrace // *Alloc or *Slice case *ast.BinaryExpr: - return n.OpPos // *Phi or *BinOp + return n.OpPos // *Phi, *BinOp or *Literal case *ast.UnaryExpr: - return n.OpPos // *Phi or *UnOp + return n.OpPos // *Phi, *UnOp, or *Literal case *ast.IndexExpr: return n.Lbrack // *Index or *IndexAddr @@ -222,7 +225,5 @@ func CanonicalPos(n ast.Node) token.Pos { return n.Colon // *MapUpdate } - // Almost anything can be a constant expression (*Literal). - // TODO(adonovan): actually that's not so; audit and restrict. - return n.Pos() + return token.NoPos } diff --git a/ssa/ssa.go b/ssa/ssa.go index 37693363..0ffd166f 100644 --- a/ssa/ssa.go +++ b/ssa/ssa.go @@ -378,7 +378,8 @@ type Parameter struct { // constants. // // Pos() returns the canonical position (see CanonicalPos) of the -// originating constant expression, if explicit in the source. +// originating constant expression (ast.Ident or ast.BasicLit, or +// ast.{Call,Selector,Unary,Binary}Expr), if explicit in the source. // // Example printed form: // 42:int