diff --git a/var.go b/var.go index acee64b..1c52cbe 100644 --- a/var.go +++ b/var.go @@ -42,26 +42,38 @@ func (v *Variable) lateBind() error { switch c := v.caller.Fun.(type) { case *ast.Ident: name := nameFromIdent(c) - bl, err := getBuiltin().FindFunction(name) + fn, err := getBuiltin().FindFunction(name) if err == nil { - v.Type = bl.Type - } else { - var t Type - fn, err := v.pkg.FindFunction(name) - if err == nil { - if len(fn.Type.Results) <= v.index { - return fmt.Errorf("%d result is available but want the %d", len(fn.Type.Results), v.index) - } - t = fn.Type.Results[v.index].Type - } else { - t, err = checkTypeCast(v.pkg, getBuiltin(), v.caller.Args, name) - if err != nil { - return err + // make and new are exceptions, + if fn.Name == "make" { + v.Type = newType(v.pkg, v.file, v.caller.Args[0]) + } + + if fn.Name == "new" { + tt := newType(v.pkg, v.file, v.caller.Args[0]) + v.Type = &StarType{ + Target: tt, } } + break + } - v.Type = t + fn, err = v.pkg.FindFunction(name) + var t Type + if err == nil { + if len(fn.Type.Results) <= v.index { + return fmt.Errorf("%d result is available but want the %d", len(fn.Type.Results), v.index) + } + t = fn.Type.Results[v.index].Type + } else { + t, err = checkTypeCast(v.pkg, getBuiltin(), v.caller.Args, name) + if err != nil { + return err + } } + + v.Type = t + case *ast.SelectorExpr: var pkg string switch c.X.(type) { diff --git a/var_test.go b/var_test.go index 497ab01..a676556 100644 --- a/var_test.go +++ b/var_test.go @@ -23,6 +23,9 @@ var i5 = []int{1,2,3,4,5} var i6 = 10i +var i7 = make([]int, 10) + +var i8 = new(int) ` func TestVariable(t *testing.T) { @@ -43,7 +46,6 @@ func TestVariable(t *testing.T) { i, err := p.FindVariable("i1") So(err, ShouldBeNil) So(i.Name, ShouldEqual, "i1") - So(i.Name, ShouldEqual, "i1") So(i.Type.(*IdentType).Ident, ShouldEqual, "int") }) @@ -51,7 +53,6 @@ func TestVariable(t *testing.T) { i, err := p.FindVariable("i2") So(err, ShouldBeNil) So(i.Name, ShouldEqual, "i2") - So(i.Name, ShouldEqual, "i2") So(i.Type.(*IdentType).Ident, ShouldEqual, "float64") }) @@ -59,7 +60,6 @@ func TestVariable(t *testing.T) { i, err := p.FindVariable("i3") So(err, ShouldBeNil) So(i.Name, ShouldEqual, "i3") - So(i.Name, ShouldEqual, "i3") So(i.Type.(*IdentType).Ident, ShouldEqual, "string") }) @@ -67,7 +67,6 @@ func TestVariable(t *testing.T) { i, err := p.FindVariable("i4") So(err, ShouldBeNil) So(i.Name, ShouldEqual, "i4") - So(i.Name, ShouldEqual, "i4") So(i.Type.(*IdentType).Ident, ShouldEqual, "char") }) @@ -75,7 +74,6 @@ func TestVariable(t *testing.T) { i, err := p.FindVariable("i5") So(err, ShouldBeNil) So(i.Name, ShouldEqual, "i5") - So(i.Name, ShouldEqual, "i5") So(i.Type.(*ArrayType).Type.(*IdentType).Ident, ShouldEqual, "int") }) @@ -83,8 +81,24 @@ func TestVariable(t *testing.T) { i, err := p.FindVariable("i6") So(err, ShouldBeNil) So(i.Name, ShouldEqual, "i6") - So(i.Name, ShouldEqual, "i6") So(i.Type.(*IdentType).Ident, ShouldEqual, "complex64") }) + + Convey("call builtin func", func() { + So(p.Bind(), ShouldBeNil) + Convey("make func i7", func() { + i, err := p.FindVariable("i7") + So(err, ShouldBeNil) + So(i.Name, ShouldEqual, "i7") + So(i.Type.(*ArrayType).Type.(*IdentType).Ident, ShouldEqual, "int") + }) + + Convey("new func i8", func() { + i, err := p.FindVariable("i8") + So(err, ShouldBeNil) + So(i.Name, ShouldEqual, "i8") + So(i.Type.(*StarType).Target.(*IdentType).Ident, ShouldEqual, "int") + }) + }) }) }