2010-10-22 11:03:14 -06:00
|
|
|
// Copyright 2010 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
2011-12-02 12:14:04 -07:00
|
|
|
"go/token"
|
2010-10-22 11:03:14 -06:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2010-12-09 11:11:57 -07:00
|
|
|
type simplifier struct{}
|
2010-10-22 11:03:14 -06:00
|
|
|
|
2010-12-09 11:22:01 -07:00
|
|
|
func (s *simplifier) Visit(node ast.Node) ast.Visitor {
|
2010-12-09 11:11:57 -07:00
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.CompositeLit:
|
2010-10-22 11:03:14 -06:00
|
|
|
// array, slice, and map composite literals may be simplified
|
2010-12-09 11:11:57 -07:00
|
|
|
outer := n
|
2010-10-22 11:03:14 -06:00
|
|
|
var eltType ast.Expr
|
|
|
|
switch typ := outer.Type.(type) {
|
|
|
|
case *ast.ArrayType:
|
|
|
|
eltType = typ.Elt
|
|
|
|
case *ast.MapType:
|
|
|
|
eltType = typ.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
if eltType != nil {
|
2011-04-25 11:39:36 -06:00
|
|
|
typ := reflect.ValueOf(eltType)
|
2011-12-02 12:14:04 -07:00
|
|
|
for i, x := range outer.Elts {
|
|
|
|
px := &outer.Elts[i]
|
2010-10-22 11:03:14 -06:00
|
|
|
// look at value of indexed/named elements
|
|
|
|
if t, ok := x.(*ast.KeyValueExpr); ok {
|
|
|
|
x = t.Value
|
2011-12-02 12:14:04 -07:00
|
|
|
px = &t.Value
|
2010-10-22 11:03:14 -06:00
|
|
|
}
|
|
|
|
simplify(x)
|
|
|
|
// if the element is a composite literal and its literal type
|
|
|
|
// matches the outer literal's element type exactly, the inner
|
|
|
|
// literal type may be omitted
|
|
|
|
if inner, ok := x.(*ast.CompositeLit); ok {
|
2011-04-25 11:39:36 -06:00
|
|
|
if match(nil, typ, reflect.ValueOf(inner.Type)) {
|
2010-10-22 11:03:14 -06:00
|
|
|
inner.Type = nil
|
|
|
|
}
|
|
|
|
}
|
2011-12-02 12:14:04 -07:00
|
|
|
// if the outer literal's element type is a pointer type *T
|
|
|
|
// and the element is & of a composite literal of type T,
|
|
|
|
// the inner &T may be omitted.
|
|
|
|
if ptr, ok := eltType.(*ast.StarExpr); ok {
|
|
|
|
if addr, ok := x.(*ast.UnaryExpr); ok && addr.Op == token.AND {
|
|
|
|
if inner, ok := addr.X.(*ast.CompositeLit); ok {
|
|
|
|
if match(nil, reflect.ValueOf(ptr.X), reflect.ValueOf(inner.Type)) {
|
2012-01-13 19:05:47 -07:00
|
|
|
inner.Type = nil // drop T
|
|
|
|
*px = inner // drop &
|
2011-12-02 12:14:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-22 11:03:14 -06:00
|
|
|
}
|
|
|
|
|
2010-12-09 11:11:57 -07:00
|
|
|
// node was simplified - stop walk (there are no subnodes to simplify)
|
2010-10-22 11:03:14 -06:00
|
|
|
return nil
|
|
|
|
}
|
2010-12-09 11:11:57 -07:00
|
|
|
|
|
|
|
case *ast.RangeStmt:
|
|
|
|
// range of the form: for x, _ = range v {...}
|
|
|
|
// can be simplified to: for x = range v {...}
|
|
|
|
if n.Value != nil {
|
|
|
|
if ident, ok := n.Value.(*ast.Ident); ok && ident.Name == "_" {
|
|
|
|
n.Value = nil
|
|
|
|
}
|
|
|
|
}
|
2010-10-22 11:03:14 -06:00
|
|
|
}
|
|
|
|
|
2010-12-09 11:11:57 -07:00
|
|
|
return s
|
2010-10-22 11:03:14 -06:00
|
|
|
}
|
|
|
|
|
2010-12-09 11:22:01 -07:00
|
|
|
func simplify(node ast.Node) {
|
2010-12-09 11:11:57 -07:00
|
|
|
var s simplifier
|
|
|
|
ast.Walk(&s, node)
|
2010-10-22 11:03:14 -06:00
|
|
|
}
|