diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 7577cf5c..ce896a59 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -67,6 +67,7 @@ type Flags uint32 const ( FlagsSuspendible = Flags(0x00000001) + FlagsTypeChecked = Flags(0x00000002) ) type Node struct { @@ -88,7 +89,9 @@ type Node struct { list1 []*Node } -func (n *Node) Kind() Kind { return n.kind } +func (n *Node) Kind() Kind { return n.kind } +func (n *Node) TypeChecked() bool { return n.flags&FlagsTypeChecked != 0 } +func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked } func (n *Node) Assert() *Assert { return (*Assert)(n) } func (n *Node) Assign() *Assign { return (*Assign)(n) } @@ -150,6 +153,9 @@ func (n *Raw) List1() []*Node { return n.list1 } func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l } +// MaxExprDepth is an advisory limit for an Expr's recursion depth. +const MaxExprDepth = 255 + // Expr is an expression, such as "i", "+j" or "k + l[m(n, o)].p": // - FlagsSuspendible is "f(x)" vs "f?(x)" // - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot> @@ -351,6 +357,9 @@ func NewJump(keyword t.ID) *Jump { } } +// MaxTypeExprDepth is an advisory limit for a TypeExpr's recursion depth. +const MaxTypeExprDepth = 63 + // TypeExpr is a type expression, such as "u32", "u32[:8]", "pkg.foo", "ptr T" // or "[8] T": // - ID0: <0|package name|IDPtr|IDOpenBracket> diff --git a/lang/ast/string.go b/lang/ast/string.go index dbe57bc3..18a3dc4b 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -15,10 +15,15 @@ func (n *Expr) String(m *t.IDMap) string { if n.id0 == 0 { return m.ByID(n.id1) } - return string(n.appendString(nil, m, false)) + return string(n.appendString(nil, m, false, 0)) } -func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte { +func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uint32) []byte { + if depth > MaxExprDepth { + return append(buf, "!expr_recursion_depth_too_large!"...) + } + depth++ + if n != nil { switch n.id0.Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { case 0: @@ -27,7 +32,7 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte { buf = append(buf, m.ByID(n.id1)...) case t.IDOpenParen: - buf = n.lhs.Expr().appendString(buf, m, true) + buf = n.lhs.Expr().appendString(buf, m, true, depth) if n.flags&FlagsSuspendible != 0 { buf = append(buf, '?') } @@ -36,44 +41,44 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte { if i != 0 { buf = append(buf, ", "...) } - buf = o.Expr().appendString(buf, m, false) + buf = o.Expr().appendString(buf, m, false, depth) } buf = append(buf, ')') case t.IDOpenBracket: - buf = n.lhs.Expr().appendString(buf, m, true) + buf = n.lhs.Expr().appendString(buf, m, true, depth) buf = append(buf, '[') - buf = n.rhs.Expr().appendString(buf, m, false) + buf = n.rhs.Expr().appendString(buf, m, false, depth) buf = append(buf, ']') case t.IDColon: - buf = n.lhs.Expr().appendString(buf, m, true) + buf = n.lhs.Expr().appendString(buf, m, true, depth) buf = append(buf, '[') - buf = n.mhs.Expr().appendString(buf, m, false) + buf = n.mhs.Expr().appendString(buf, m, false, depth) buf = append(buf, ':') - buf = n.rhs.Expr().appendString(buf, m, false) + buf = n.rhs.Expr().appendString(buf, m, false, depth) buf = append(buf, ']') case t.IDDot: - buf = n.lhs.Expr().appendString(buf, m, true) + buf = n.lhs.Expr().appendString(buf, m, true, depth) buf = append(buf, '.') buf = append(buf, m.ByID(n.id1)...) } case t.FlagsUnaryOp: buf = append(buf, opStrings[0xFF&n.id0.Key()]...) - buf = n.rhs.Expr().appendString(buf, m, true) + buf = n.rhs.Expr().appendString(buf, m, true, depth) case t.FlagsBinaryOp: if parenthesize { buf = append(buf, '(') } - buf = n.lhs.Expr().appendString(buf, m, true) + buf = n.lhs.Expr().appendString(buf, m, true, depth) buf = append(buf, opStrings[0xFF&n.id0.Key()]...) if n.id0 == t.IDXBinaryAs { buf = append(buf, n.rhs.TypeExpr().String(m)...) } else { - buf = n.rhs.Expr().appendString(buf, m, true) + buf = n.rhs.Expr().appendString(buf, m, true, depth) } if parenthesize { buf = append(buf, ')') @@ -88,7 +93,7 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool) []byte { if i != 0 { buf = append(buf, op...) } - buf = o.Expr().appendString(buf, m, true) + buf = o.Expr().appendString(buf, m, true, depth) } if parenthesize { buf = append(buf, ')') @@ -135,22 +140,46 @@ var opStrings = [256]string{ // String returns a string form of n. func (n *TypeExpr) String(m *t.IDMap) string { - if n != nil { - s := "" - switch n.PackageOrDecorator() { - case 0: - s = m.ByID(n.Name()) - case t.IDPtr: - return "ptr " + n.Inner().String(m) - case t.IDOpenBracket: - return "[" + n.ArrayLength().String(m) + "] " + n.Inner().String(m) - default: - s = m.ByID(n.PackageOrDecorator()) + "." + m.ByID(n.Name()) - } - if n.InclMin() == nil && n.ExclMax() == nil { - return s - } - return s + "[" + n.InclMin().String(m) + ":" + n.ExclMax().String(m) + "]" + if n == nil { + return "" } - return "!invalid_type!" + if n.PackageOrDecorator() == 0 && n.InclMin() == nil && n.ExclMax() == nil { + return m.ByID(n.Name()) + } + return string(n.appendString(nil, m, 0)) +} + +func (n *TypeExpr) appendString(buf []byte, m *t.IDMap, depth uint32) []byte { + if depth > MaxTypeExprDepth { + return append(buf, "!type_expr_recursion_depth_too_large!"...) + } + depth++ + if n == nil { + return append(buf, "!invalid_type!"...) + } + + switch n.PackageOrDecorator() { + case 0: + buf = append(buf, m.ByID(n.Name())...) + case t.IDPtr: + buf = append(buf, "ptr "...) + return n.Inner().appendString(buf, m, depth) + case t.IDOpenBracket: + buf = append(buf, '[') + buf = n.ArrayLength().appendString(buf, m, false, 0) + buf = append(buf, "] "...) + return n.Inner().appendString(buf, m, depth) + default: + buf = append(buf, m.ByID(n.PackageOrDecorator())...) + buf = append(buf, '.') + buf = append(buf, m.ByID(n.Name())...) + } + if n.InclMin() != nil || n.ExclMax() != nil { + buf = append(buf, '[') + buf = n.InclMin().appendString(buf, m, false, 0) + buf = append(buf, ':') + buf = n.ExclMax().appendString(buf, m, false, 0) + buf = append(buf, ']') + } + return buf } diff --git a/lang/check/check.go b/lang/check/check.go index 74ca38ee..5b096b10 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -83,6 +83,7 @@ func Check(m *t.IDMap, files ...*a.File) (*Checker, error) { return nil, err } } + f.Node().SetTypeChecked() } } return c, nil @@ -126,10 +127,11 @@ func (c *Checker) checkFields(fields []*a.Node) error { if _, ok := fieldNames[f.Name()]; ok { return fmt.Errorf("check: duplicate field %q", c.idMap.ByID(f.Name())) } - if err := tc.checkTypeExpr(f.XType()); err != nil { + if err := tc.checkTypeExpr(f.XType(), 0); err != nil { return fmt.Errorf("%v in field %q", err, c.idMap.ByID(f.Name())) } fieldNames[f.Name()] = true + f.Node().SetTypeChecked() } return nil } @@ -157,6 +159,7 @@ func (c *Checker) checkStruct(n *a.Node) error { ID: id, Struct: s, } + s.Node().SetTypeChecked() return nil } @@ -169,6 +172,7 @@ func (c *Checker) checkFuncSignature(n *a.Node) error { Line: f.Line(), } } + f.In().Node().SetTypeChecked() if err := c.checkFields(f.Out().Fields()); err != nil { return &Error{ Err: fmt.Errorf("%v in out-params for func %q", err, c.idMap.ByID(f.Name())), @@ -176,6 +180,8 @@ func (c *Checker) checkFuncSignature(n *a.Node) error { Line: f.Line(), } } + f.Out().Node().SetTypeChecked() + qid := f.QID() if other, ok := c.funcs[qid]; ok { return &Error{ @@ -186,9 +192,14 @@ func (c *Checker) checkFuncSignature(n *a.Node) error { OtherLine: other.Func.Line(), } } + + inTyp := a.NewTypeExpr(0, f.In().Name(), nil, nil, nil) + inTyp.Node().SetTypeChecked() + outTyp := a.NewTypeExpr(0, f.Out().Name(), nil, nil, nil) + outTyp.Node().SetTypeChecked() localVars := TypeMap{ - t.IDIn: a.NewTypeExpr(0, f.In().Name(), nil, nil, nil), - t.IDOut: a.NewTypeExpr(0, f.Out().Name(), nil, nil, nil), + t.IDIn: inTyp, + t.IDOut: outTyp, } if qid[0] != 0 { if _, ok := c.structs[qid[0]]; !ok { @@ -198,7 +209,11 @@ func (c *Checker) checkFuncSignature(n *a.Node) error { Line: f.Line(), } } - localVars[t.IDThis] = a.NewTypeExpr(t.IDPtr, 0, nil, nil, a.NewTypeExpr(0, qid[0], nil, nil, nil)) + sTyp := a.NewTypeExpr(0, qid[0], nil, nil, nil) + sTyp.Node().SetTypeChecked() + pTyp := a.NewTypeExpr(t.IDPtr, 0, nil, nil, sTyp) + pTyp.Node().SetTypeChecked() + localVars[t.IDThis] = pTyp } c.funcs[qid] = Func{ QID: qid, @@ -246,6 +261,8 @@ func (c *Checker) checkFuncBody(n *a.Node) error { } } + // TODO: check that variables are never used before they're initialized. + // Assign ConstValue's (if applicable) and MType's to each Expr. for _, m := range f.Body() { if err := tc.checkStatement(m); err != nil { @@ -256,14 +273,20 @@ func (c *Checker) checkFuncBody(n *a.Node) error { } } } + + f.Node().SetTypeChecked() + if err := f.Node().Walk(func(n *a.Node) error { + if !n.TypeChecked() { + return fmt.Errorf("check: internal error: unchecked %s node", n.Kind()) + } if n.Kind() == a.KExpr { e := n.Expr() if typ := e.MType(); typ == nil { - return fmt.Errorf("check: internal error: expression node %q has no (implicit) type", + return fmt.Errorf("check: internal error: expression %q has no (implicit) type", e.String(tc.idMap)) } else if typ == TypeExprIdealNumber && e.ConstValue() == nil { - return fmt.Errorf("check: internal error: expression node %q has ideal number type "+ + return fmt.Errorf("check: internal error: expression %q has ideal number type "+ "but no const value", e.String(tc.idMap)) } } diff --git a/lang/check/type.go b/lang/check/type.go index b9952d04..30dbd32e 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -29,12 +29,9 @@ func (c *typeChecker) checkVars(n *a.Node) error { 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 { + if err := c.checkTypeExpr(v.XType(), 0); err != nil { return err } - if value := v.Value(); value != nil { - // TODO: check that value doesn't mention the variable itself. - } c.f.LocalVars[name] = v.XType() return nil } @@ -53,25 +50,31 @@ func (c *typeChecker) checkStatement(n *a.Node) error { switch n.Kind() { case a.KAssert: - return c.checkAssert(n.Assert()) + if err := c.checkAssert(n.Assert()); err != nil { + return err + } case a.KAssign: - return c.checkAssign(n.Assign()) + if err := c.checkAssign(n.Assign()); err != nil { + return err + } case a.KVar: o := n.Var() + if !o.XType().Node().TypeChecked() { + return fmt.Errorf("check: internal error: unchecked type expression %q", o.XType().String(c.idMap)) + } if value := o.Value(); value != nil { - if err := c.checkExpr(value); err != nil { + if err := c.checkExpr(value, 0); err != nil { return err } // TODO: check that value.Type is assignable to o.TypeExpr(). } - return nil case a.KWhile: o := n.While() cond := o.Condition() - if err := c.checkExpr(cond); err != nil { + if err := c.checkExpr(cond, 0); err != nil { return err } if !cond.MType().Eq(TypeExprBoolean) { @@ -82,18 +85,18 @@ func (c *typeChecker) checkStatement(n *a.Node) error { if err := c.checkAssert(m.Assert()); err != nil { return err } + m.SetTypeChecked() } for _, m := range o.Body() { if err := c.checkStatement(m); err != nil { return err } } - return nil case a.KIf: for o := n.If(); o != nil; o = o.ElseIf() { cond := o.Condition() - if err := c.checkExpr(cond); err != nil { + if err := c.checkExpr(cond, 0); err != nil { return err } if !cond.MType().Eq(TypeExprBoolean) { @@ -111,30 +114,31 @@ func (c *typeChecker) checkStatement(n *a.Node) error { } } } - return nil case a.KReturn: o := n.Return() if value := o.Value(); value != nil { - if err := c.checkExpr(value); err != nil { + if err := c.checkExpr(value, 0); err != nil { return err } // TODO: type-check that value is assignable to the return value. // This needs the context of what func we're in. } - return nil case a.KJump: // TODO: check that we're in a for loop. - return nil + + default: + return fmt.Errorf("check: unrecognized ast.Kind (%d) for checkStatement", n.Kind()) } - return fmt.Errorf("check: unrecognized ast.Kind (%d) for checkStatement", n.Kind()) + n.SetTypeChecked() + return nil } func (c *typeChecker) checkAssert(n *a.Assert) error { cond := n.Condition() - if err := c.checkExpr(cond); err != nil { + if err := c.checkExpr(cond, 0); err != nil { return err } if !cond.MType().Eq(TypeExprBoolean) { @@ -149,10 +153,10 @@ func (c *typeChecker) checkAssert(n *a.Assert) error { func (c *typeChecker) checkAssign(n *a.Assign) error { lhs := n.LHS() rhs := n.RHS() - if err := c.checkExpr(lhs); err != nil { + if err := c.checkExpr(lhs, 0); err != nil { return err } - if err := c.checkExpr(rhs); err != nil { + if err := c.checkExpr(rhs, 0); err != nil { return err } lTyp := lhs.MType() @@ -190,21 +194,37 @@ func (c *typeChecker) checkAssign(n *a.Assign) error { ) } -func (c *typeChecker) checkExpr(n *a.Expr) error { +func (c *typeChecker) checkExpr(n *a.Expr, depth uint32) error { + if depth > a.MaxExprDepth { + return fmt.Errorf("check: expression recursion depth too large") + } + depth++ + switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { case 0: - return c.checkExprOther(n) + if err := c.checkExprOther(n, depth); err != nil { + return err + } case t.FlagsUnaryOp: - return c.checkExprUnaryOp(n) + if err := c.checkExprUnaryOp(n, depth); err != nil { + return err + } case t.FlagsBinaryOp: - return c.checkExprBinaryOp(n) + if err := c.checkExprBinaryOp(n, depth); err != nil { + return err + } case t.FlagsAssociativeOp: - return c.checkExprAssociativeOp(n) + if err := c.checkExprAssociativeOp(n, depth); err != nil { + return err + } + default: + return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExpr", n.ID0().Key()) } - return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExpr", n.ID0().Key()) + n.Node().SetTypeChecked() + return nil } -func (c *typeChecker) checkExprOther(n *a.Expr) error { +func (c *typeChecker) checkExprOther(n *a.Expr, depth uint32) error { switch n.ID0() { case 0: switch id1 := n.ID1(); { @@ -258,14 +278,14 @@ func (c *typeChecker) checkExprOther(n *a.Expr) error { // TODO. case t.IDDot: - return c.checkDot(n) + return c.checkDot(n, depth) } return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprOther", n.ID0().Key()) } -func (c *typeChecker) checkDot(n *a.Expr) error { +func (c *typeChecker) checkDot(n *a.Expr, depth uint32) error { lhs := n.LHS().Expr() - if err := c.checkExpr(lhs); err != nil { + if err := c.checkExpr(lhs, depth); err != nil { return err } lTyp := lhs.MType() @@ -304,9 +324,9 @@ func (c *typeChecker) checkDot(n *a.Expr) error { n.ID1().String(c.idMap), lTyp.Name().String(c.idMap), n.String(c.idMap)) } -func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error { +func (c *typeChecker) checkExprUnaryOp(n *a.Expr, depth uint32) error { rhs := n.RHS().Expr() - if err := c.checkExpr(rhs); err != nil { + if err := c.checkExpr(rhs, depth); err != nil { return err } rTyp := rhs.MType() @@ -340,16 +360,16 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error { return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprUnaryOp", n.ID0().Key()) } -func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error { +func (c *typeChecker) checkExprBinaryOp(n *a.Expr, depth uint32) error { lhs := n.LHS().Expr() - if err := c.checkExpr(lhs); err != nil { + if err := c.checkExpr(lhs, depth); err != nil { return err } lTyp := lhs.MType() op := n.ID0() if op == t.IDXBinaryAs { rhs := n.RHS().TypeExpr() - if err := c.checkTypeExpr(rhs); err != nil { + if err := c.checkTypeExpr(rhs, 0); err != nil { return err } if numeric(lTyp) && rhs.IsNumType() { @@ -360,7 +380,7 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error { lhs.String(c.idMap), lTyp.String(c.idMap), rhs.String(c.idMap)) } rhs := n.RHS().Expr() - if err := c.checkExpr(rhs); err != nil { + if err := c.checkExpr(rhs, depth); err != nil { return err } rTyp := rhs.MType() @@ -480,52 +500,60 @@ func (c *typeChecker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) e return nil } -func (c *typeChecker) checkExprAssociativeOp(n *a.Expr) error { +func (c *typeChecker) checkExprAssociativeOp(n *a.Expr, depth uint32) error { // TODO. return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprAssociativeOp", n.ID0().Key()) } -func (c *typeChecker) checkTypeExpr(n *a.TypeExpr) error { - for ; n != nil; n = n.Inner() { - switch n.PackageOrDecorator() { - case 0: - name := n.Name() - if name.IsNumType() || name == t.IDBool { - for _, bound := range n.Bounds() { - if bound == nil { - continue - } - if err := c.checkExpr(bound); err != nil { - return err - } - if bound.ConstValue() == nil { - return fmt.Errorf("check: %q is not constant", bound.String(c.idMap)) - } - } - return nil - } - if n.InclMin() != nil || n.ExclMax() != nil { - // TODO: reject. - } - // TODO: see if name refers to a struct type. - return fmt.Errorf("check: %q is not a type", c.idMap.ByID(name)) - - case t.IDPtr: - // No-op. - - case t.IDOpenBracket: - aLen := n.ArrayLength() - if err := c.checkExpr(aLen); err != nil { - return err - } - if aLen.ConstValue() == nil { - return fmt.Errorf("check: %q is not constant", aLen.String(c.idMap)) - } - - default: - return fmt.Errorf("check: unrecognized node for checkTypeExpr") - } +func (c *typeChecker) checkTypeExpr(n *a.TypeExpr, depth uint32) error { + if depth > a.MaxTypeExprDepth { + return fmt.Errorf("check: type expression recursion depth too large") } + depth++ + + switch n.PackageOrDecorator() { + case 0: + if name := n.Name(); name.IsNumType() || name == t.IDBool { + for _, bound := range n.Bounds() { + if bound == nil { + continue + } + if err := c.checkExpr(bound, 0); err != nil { + return err + } + if bound.ConstValue() == nil { + return fmt.Errorf("check: %q is not constant", bound.String(c.idMap)) + } + } + break + } + if n.InclMin() != nil || n.ExclMax() != nil { + // TODO: reject. + } + // TODO: see if name refers to a struct type. + return fmt.Errorf("check: %q is not a type", c.idMap.ByID(n.Name())) + + case t.IDPtr: + if err := c.checkTypeExpr(n.Inner(), depth); err != nil { + return err + } + + case t.IDOpenBracket: + aLen := n.ArrayLength() + if err := c.checkExpr(aLen, 0); err != nil { + return err + } + if aLen.ConstValue() == nil { + return fmt.Errorf("check: %q is not constant", aLen.String(c.idMap)) + } + if err := c.checkTypeExpr(n.Inner(), depth); err != nil { + return err + } + + default: + return fmt.Errorf("check: unrecognized node for checkTypeExpr") + } + n.Node().SetTypeChecked() return nil }