pgen: discover types for composite literals.

This commit is contained in:
Greg 2018-08-04 15:41:34 -04:00
parent 0d662564aa
commit 100f65f83a
2 changed files with 14 additions and 4 deletions

View File

@ -24,7 +24,7 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
if !reg.MatchString(id.Name) { return v } if !reg.MatchString(id.Name) { return v }
if len(x.Args) != 2 { if len(x.Args) != 2 {
log.Fatal("Wrong number of arguments in persist_T call.") log.Fatal("Wrong number of arguments in persistT call.")
} }
var tp string var tp string
switch arg := x.Args[1].(type) { switch arg := x.Args[1].(type) {
@ -33,14 +33,16 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
if tp == "float" { if tp == "float" {
tp = "float64" tp = "float64"
} }
case *ast.CompositeLit:
tp = types.TypeString(info.TypeOf(arg),types.RelativeTo(pkg))
case *ast.Ident: case *ast.Ident:
if arg.Obj.Kind != ast.Var { return v } if arg.Obj.Kind != ast.Var { return v }
inner := pkg.Scope().Innermost(arg.Pos()) inner := pkg.Scope().Innermost(arg.Pos())
_,obj := inner.LookupParent(arg.Obj.Name,arg.Pos()) _,obj := inner.LookupParent(arg.Obj.Name,arg.Pos())
tp = types.TypeString(obj.Type(),types.RelativeTo(pkg)) tp = types.TypeString(obj.Type(),types.RelativeTo(pkg))
trimType(&tp) trimType(&tp)
for _, submatches := range impreg.FindAllStringSubmatchIndex(tp, -1) { for _, s := range impreg.FindAllStringSubmatchIndex(tp, -1) {
pkgname := impreg.ExpandString(make([]byte,0), "$1", tp, submatches) pkgname := impreg.ExpandString(make([]byte,0), "$1", tp, s)
needImps[string(pkgname)] = true needImps[string(pkgname)] = true
} }
default: default:
@ -63,6 +65,7 @@ var pkgreg *regexp.Regexp
var impreg *regexp.Regexp var impreg *regexp.Regexp
var imps map[string]string var imps map[string]string
var needImps map[string]bool var needImps map[string]bool
var info *types.Info
func chkpkg(p *ast.Package) { func chkpkg(p *ast.Package) {
fs := make([]*ast.File,0) fs := make([]*ast.File,0)
@ -72,7 +75,12 @@ func chkpkg(p *ast.Package) {
conf := types.Config{Error: func(error) {}, Importer: importer.Default()} conf := types.Config{Error: func(error) {}, Importer: importer.Default()}
var err error var err error
pkg, err = conf.Check("main", fset, fs, nil) info = &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object)}
pkg, err = conf.Check("main", fset, fs, info)
if err != nil { if err != nil {
log.Print("Check:",err) log.Print("Check:",err)
} }

View File

@ -34,6 +34,8 @@ func main() {
y6 := make(map[persist.Var]gtoken.FileSet) y6 := make(map[persist.Var]gtoken.FileSet)
y6p := persistM6("y6",y6) y6p := persistM6("y6",y6)
y6 = y6p.Get() y6 = y6p.Get()
y7p := persistIntArray("y7",[]int{1,2,3})
_ = y7p
z := func(interface{}) { z := func(interface{}) {
_ = persistFloat("y7",1.0) _ = persistFloat("y7",1.0)