Handle structs that contain structs.

This commit is contained in:
Nigel Tao committed 2017-06-15 15:19:41 +10:00
1 parent b3c12e05cf
commit 86c6b79e1a
9 files changed
+263 -96

No files matched your search

+6
View File
@@ -469,6 +469,12 @@ func (n *TypeExpr) Min() *Expr { return n.lhs.Expr() }
func (n *TypeExpr) Max() *Expr { return n.mhs.Expr() }
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() }
func (n *TypeExpr) Innermost() *TypeExpr {
for ; n != nil && n.Inner() != nil; n = n.Inner() {
}
return n
}
func (n *TypeExpr) IsBool() bool {
return n.id0 == 0 && n.id1.Key() == t.KeyBool
}
+57
View File
@@ -0,0 +1,57 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package ast
import (
t "github.com/google/puffs/lang/token"
)
func TopologicalSortStructs(ns []*Struct) (sorted []*Struct, ok bool) {
// Algorithm is a depth-first search as per
// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
sorted = make([]*Struct, 0, len(ns))
byName := map[t.ID]*Struct{}
for _, n := range ns {
byName[n.Name()] = n
}
marks := map[*Struct]uint8{}
for _, n := range ns {
if _, ok := marks[n]; !ok {
sorted, ok = tssVisit(sorted, n, byName, marks)
if !ok {
return nil, false
}
}
}
return sorted, true
}
func tssVisit(dst []*Struct, n *Struct, byName map[t.ID]*Struct, marks map[*Struct]uint8) ([]*Struct, bool) {
const (
unmarked = 0
temporary = 1
permanent = 2
)
if marks[n] == temporary {
return nil, false
}
marks[n] = temporary
for _, f := range n.Fields() {
x := f.Field().XType().Innermost()
if x.Decorator() != 0 {
continue
}
if o := byName[x.Name()]; o != nil {
var ok bool
dst, ok = tssVisit(dst, o, byName, marks)
if !ok {
return nil, false
}
}
}
marks[n] = permanent
return append(dst, n), true
}
+2 -3
View File
@@ -127,9 +127,8 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
}
func bcheckField(tm *t.Map, n *a.Field) error {
x := n.XType()
for ; x.Inner() != nil; x = x.Inner() {
}
// TODO: check if x is a numeric type? What if n.XType() is "ptr T"?
x := n.XType().Innermost()
dv, nMin, nMax := zero, zero, zero
if o := n.DefaultValue(); o != nil {
dv = o.ConstValue()
+36 -12
View File
@@ -117,6 +117,12 @@ func Check(tm *t.Map, flags Flags, files ...*a.File) (*Checker, error) {
for _, phase := range phases {
for _, f := range files {
if phase.kind == a.KInvalid {
if err := phase.check(c, nil); err != nil {
return nil, err
}
continue
}
for _, n := range f.TopLevelDecls() {
if n.Kind() != phase.kind {
continue
@@ -137,7 +143,9 @@ var phases = [...]struct {
}{
{a.KUse, (*Checker).checkUse},
{a.KStatus, (*Checker).checkStatus},
{a.KStruct, (*Checker).checkStruct},
{a.KStruct, (*Checker).checkStructDecl},
{a.KInvalid, (*Checker).checkStructCycles},
{a.KStruct, (*Checker).checkStructFields},
{a.KFunc, (*Checker).checkFuncSignature},
{a.KFunc, (*Checker).checkFuncContract},
{a.KFunc, (*Checker).checkFuncBody},
@@ -147,9 +155,12 @@ type Checker struct {
flags Flags
tm *t.Map
reasonMap reasonMap
funcs map[t.QID]Func
statuses map[t.ID]Status
structs map[t.ID]Struct
funcs map[t.QID]Func
statuses map[t.ID]Status
structs map[t.ID]Struct
unsortedStructs []*a.Struct
}
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
@@ -229,15 +240,8 @@ func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool) error {
return nil
}
func (c *Checker) checkStruct(node *a.Node) error {
func (c *Checker) checkStructDecl(node *a.Node) error {
n := node.Struct()
if err := c.checkFields(n.Fields(), true); err != nil {
return &Error{
Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
}
id := n.Name()
if other, ok := c.structs[id]; ok {
return &Error{
@@ -252,6 +256,26 @@ func (c *Checker) checkStruct(node *a.Node) error {
ID: id,
Struct: n,
}
c.unsortedStructs = append(c.unsortedStructs, n)
return nil
}
func (c *Checker) checkStructCycles(_ *a.Node) error {
if _, ok := a.TopologicalSortStructs(c.unsortedStructs); !ok {
return fmt.Errorf("check: cyclical struct definitions")
}
return nil
}
func (c *Checker) checkStructFields(node *a.Node) error {
n := node.Struct()
if err := c.checkFields(n.Fields(), true); err != nil {
return &Error{
Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
}
n.Node().SetTypeChecked()
return nil
}
+7 -2
View File
@@ -667,6 +667,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
}
depth++
swtch:
switch n.Decorator().Key() {
case 0:
if n.Name().IsNumType() {
@@ -684,12 +685,16 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
break
}
if n.Min() != nil || n.Max() != nil {
// TODO: reject.
// TODO: reject. You can only refine numeric types.
}
if name := n.Name().Key(); name == t.KeyBool || name == t.KeyBuf1 {
break
}
// TODO: see if name refers to a struct type.
for _, s := range q.c.structs {
if s.ID == n.Name() {
break swtch
}
}
return fmt.Errorf("check: %q is not a type", n.Name().String(q.tm))
case t.KeyPtr: