Check "this.x", "in.y" and "out.z".

This commit is contained in:
Nigel Tao committed 2017-05-13 17:40:09 +10:00
1 parent 27b33b77df
commit dd06f67f7a
3 files changed
+82 -23

No files matched your search

+15 -4
View File
@@ -149,10 +149,21 @@ func (c *Checker) checkFuncSignature(n *a.Node) error {
return fmt.Errorf("check: duplicate function %q at %s:%d and %s:%d",
qid.String(c.idMap), other.Func.Filename(), other.Func.Line(), f.Filename(), f.Line())
}
localVars := TypeMap{
t.IDIn: a.NewTypeExpr(0, f.In().Name(), nil, nil, nil),
t.IDOut: a.NewTypeExpr(0, f.Out().Name(), nil, nil, nil),
}
if qid[0] != 0 {
if _, ok := c.structs[qid[0]]; !ok {
return fmt.Errorf("check: no receiver struct defined for function %q at %s:%d",
qid.String(c.idMap), f.Filename(), f.Line())
}
localVars[t.IDThis] = a.NewTypeExpr(t.IDPtr, 0, nil, nil, a.NewTypeExpr(0, qid[0], nil, nil, nil))
}
c.funcs[qid] = Func{
QID: qid,
Func: f,
LocalVars: TypeMap{},
LocalVars: localVars,
}
return nil
}
@@ -160,9 +171,9 @@ func (c *Checker) checkFuncSignature(n *a.Node) error {
func (c *Checker) checkFuncBody(n *a.Node) error {
f := n.Func()
tc := &typeChecker{
c: c,
idMap: c.idMap,
typeMap: c.funcs[f.QID()].LocalVars,
c: c,
idMap: c.idMap,
f: c.funcs[f.QID()],
}
// Fill in the TypeMap with all local variables. Note that they have
+18 -10
View File
@@ -46,7 +46,11 @@ func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, src string) err
func TestCheck(t *testing.T) {
const filename = "test.puffs"
src := strings.TrimSpace(`
func foo()() {
struct foo(
i i32,
)
func foo.bar()() {
var x u8
var y i32 = +2
var z u64[:123]
@@ -56,14 +60,15 @@ func TestCheck(t *testing.T) {
x = 0
x = 1 + x
y = -y
y = this.i
b = not true
y = x as i32
var i i32
var j i32[0:8]
var p i32
var q i32[0:8]
while i == j {
while p == q {
}
}
`) + "\n"
@@ -93,18 +98,18 @@ func TestCheck(t *testing.T) {
if len(funcs) != 1 {
t.Fatalf("Funcs: got %d elements, want 1", len(funcs))
}
foo := Func{}
fooBar := Func{}
for _, f := range funcs {
foo = f
fooBar = f
break
}
if got, want := idMap.ByID(foo.QID[1]), "foo"; got != want {
if got, want := fooBar.QID.String(idMap), "foo.bar"; got != want {
t.Fatalf("Funcs[0] name: got %q, want %q", got, want)
}
got := [][2]string(nil)
for id, typ := range foo.LocalVars {
for id, typ := range fooBar.LocalVars {
got = append(got, [2]string{
idMap.ByID(id),
typ.String(idMap),
@@ -117,8 +122,11 @@ func TestCheck(t *testing.T) {
want := [][2]string{
{"a", "[4] u8"},
{"b", "bool"},
{"i", "i32"},
{"j", "i32[0:8]"},
{"in", "in"},
{"out", "out"},
{"p", "i32"},
{"q", "i32[0:8]"},
{"this", "ptr foo"},
{"x", "u8"},
{"y", "i32"},
{"z", "u64[:123]"},
+49 -9
View File
@@ -12,9 +12,9 @@ import (
)
type typeChecker struct {
c *Checker
idMap *t.IDMap
typeMap TypeMap
c *Checker
idMap *t.IDMap
f Func
errFilename string
errLine uint32
@@ -26,7 +26,7 @@ func (c *typeChecker) checkVars(n *a.Node) error {
if n.Kind() == a.KVar {
v := n.Var()
name := v.Name()
if _, ok := c.typeMap[name]; ok {
if _, ok := c.f.LocalVars[name]; ok {
return fmt.Errorf("check: duplicate var %q", c.idMap.ByID(name))
}
if err := c.checkTypeExpr(v.XType()); err != nil {
@@ -35,7 +35,7 @@ func (c *typeChecker) checkVars(n *a.Node) error {
if value := v.Value(); value != nil {
// TODO: check that value doesn't mention the variable itself.
}
c.typeMap[name] = v.XType()
c.f.LocalVars[name] = v.XType()
return nil
}
for _, l := range n.Raw().SubLists() {
@@ -215,8 +215,8 @@ func (c *typeChecker) checkExprOther(n *a.Expr) error {
return nil
case id1.IsIdent():
if c.typeMap != nil {
if typ, ok := c.typeMap[id1]; ok {
if c.f.LocalVars != nil {
if typ, ok := c.f.LocalVars[id1]; ok {
n.SetMType(typ)
return nil
}
@@ -254,12 +254,52 @@ func (c *typeChecker) checkExprOther(n *a.Expr) error {
// TODO.
case t.IDDot:
// n is a selector.
// TODO.
return c.checkDot(n)
}
return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprOther", n.ID0().Key())
}
func (c *typeChecker) checkDot(n *a.Expr) error {
lhs := n.LHS().Expr()
if err := c.checkExpr(lhs); err != nil {
return err
}
lTyp := lhs.MType()
for ; lTyp.PackageOrDecorator() == t.IDPtr; lTyp = lTyp.Inner() {
}
if lTyp.PackageOrDecorator() != 0 {
// TODO.
return fmt.Errorf("check: unsupported package-or-decorator for checkDot")
}
s := (*a.Struct)(nil)
if c.f.Func != nil {
switch name := lTyp.Name(); name {
case t.IDIn:
s = c.f.Func.In()
case t.IDOut:
s = c.f.Func.Out()
default:
s = c.c.structs[name].Struct
}
}
if s == nil {
return fmt.Errorf("check: no struct type %q found for expression %q",
lTyp.Name().String(c.idMap), lhs.String(c.idMap))
}
for _, field := range s.Fields() {
f := field.Field()
if f.Name() == n.ID1() {
n.SetMType(f.XType())
return nil
}
}
return fmt.Errorf("check: no field named %q found in struct type %q for expression %q",
n.ID1().String(c.idMap), lTyp.Name().String(c.idMap), n.String(c.idMap))
}
func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error {
rhs := n.RHS().Expr()
if err := c.checkExpr(rhs); err != nil {