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

+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: