diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 58e0db5d..f82c6164 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -934,7 +934,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, case t.KeyDot: // TODO: delete this hack that only matches "in". if n.LHS().Expr().Ident().Key() == t.KeyIn { - for _, o := range q.f.Func.In().Fields() { + for _, o := range q.astFunc.In().Fields() { o := o.Field() if o.Name() == n.Ident() { return q.bcheckTypeExpr(o.XType()) diff --git a/lang/check/check.go b/lang/check/check.go index f52c1968..e92bd6d3 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -60,27 +60,6 @@ func (e *Error) Error() string { return string(b) } -type Const struct { - QID t.QID // Qualified ID of the const name. - Const *a.Const -} - -type Func struct { - QQID t.QQID // Double-qualified ID of the func name. - Func *a.Func - LocalVars TypeMap -} - -type Status struct { - QID t.QID // Qualified ID of the status message. - Status *a.Status -} - -type Struct struct { - QID t.QID // Qualified ID of the struct name. - Struct *a.Struct -} - type usee struct { consts map[t.QID]*a.Const funcs map[t.QQID]*a.Func @@ -116,10 +95,11 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte, resolveUse: resolveUse, reasonMap: rMap, packageID: base38.Max + 1, - consts: map[t.QID]Const{}, - funcs: map[t.QQID]Func{}, - statuses: map[t.QID]Status{}, - structs: map[t.QID]Struct{}, + consts: map[t.QID]*a.Const{}, + funcs: map[t.QQID]*a.Func{}, + localVars: map[t.QQID]typeMap{}, + statuses: map[t.QID]*a.Status{}, + structs: map[t.QID]*a.Struct{}, usees: map[t.ID]*usee{}, } @@ -176,10 +156,11 @@ type Checker struct { packageID uint32 otherPackageID *a.PackageID - consts map[t.QID]Const - funcs map[t.QQID]Func - statuses map[t.QID]Status - structs map[t.QID]Struct + consts map[t.QID]*a.Const + funcs map[t.QQID]*a.Func + localVars map[t.QQID]typeMap + statuses map[t.QID]*a.Status + structs map[t.QID]*a.Struct // usees are the packages referred to in the `use "foo/bar"` lines. The // keys are the use path base: `bar`, not `"foo/bar"`. @@ -190,11 +171,7 @@ type Checker struct { unsortedStructs []*a.Struct } -func (c *Checker) PackageID() uint32 { return c.packageID } -func (c *Checker) Consts() map[t.QID]Const { return c.consts } -func (c *Checker) Funcs() map[t.QQID]Func { return c.funcs } -func (c *Checker) Statuses() map[t.QID]Status { return c.statuses } -func (c *Checker) Structs() map[t.QID]Struct { return c.structs } +func (c *Checker) PackageID() uint32 { return c.packageID } func (c *Checker) checkPackageID(node *a.Node) error { n := node.PackageID() @@ -306,14 +283,11 @@ func (c *Checker) checkStatus(node *a.Node) error { Err: fmt.Errorf("check: duplicate status %s", qid.Str(c.tm)), Filename: n.Filename(), Line: n.Line(), - OtherFilename: other.Status.Filename(), - OtherLine: other.Status.Line(), + OtherFilename: other.Filename(), + OtherLine: other.Line(), } } - c.statuses[qid] = Status{ - QID: qid, - Status: n, - } + c.statuses[qid] = n n.Node().SetTypeChecked() return nil } @@ -326,14 +300,11 @@ func (c *Checker) checkConst(node *a.Node) error { Err: fmt.Errorf("check: duplicate const %s", qid.Str(c.tm)), Filename: n.Filename(), Line: n.Line(), - OtherFilename: other.Const.Filename(), - OtherLine: other.Const.Line(), + OtherFilename: other.Filename(), + OtherLine: other.Line(), } } - c.consts[qid] = Const{ - QID: qid, - Const: n, - } + c.consts[qid] = n q := &checker{ c: c, @@ -399,14 +370,11 @@ func (c *Checker) checkStructDecl(node *a.Node) error { Err: fmt.Errorf("check: duplicate struct %s", qid.Str(c.tm)), Filename: n.Filename(), Line: n.Line(), - OtherFilename: other.Struct.Filename(), - OtherLine: other.Struct.Line(), + OtherFilename: other.Filename(), + OtherLine: other.Line(), } } - c.structs[qid] = Struct{ - QID: qid, - Struct: n, - } + c.structs[qid] = n c.unsortedStructs = append(c.unsortedStructs, n) return nil } @@ -501,8 +469,8 @@ func (c *Checker) checkFuncSignature(node *a.Node) error { Err: fmt.Errorf("check: duplicate function %s", qqid.Str(c.tm)), Filename: n.Filename(), Line: n.Line(), - OtherFilename: other.Func.Filename(), - OtherLine: other.Func.Line(), + OtherFilename: other.Filename(), + OtherLine: other.Line(), } } @@ -512,7 +480,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error { oQID := n.Out().QID() outTyp := a.NewTypeExpr(0, oQID[0], oQID[1], nil, nil, nil) outTyp.Node().SetTypeChecked() - localVars := TypeMap{ + localVars := typeMap{ t.IDIn: inTyp, t.IDOut: outTyp, } @@ -530,11 +498,8 @@ func (c *Checker) checkFuncSignature(node *a.Node) error { pTyp.Node().SetTypeChecked() localVars[t.IDThis] = pTyp } - c.funcs[qqid] = Func{ - QQID: qqid, - Func: n, - LocalVars: localVars, - } + c.funcs[qqid] = n + c.localVars[qqid] = localVars return nil } @@ -561,7 +526,8 @@ func (c *Checker) checkFuncBody(node *a.Node) error { c: c, tm: c.tm, reasonMap: c.reasonMap, - f: c.funcs[n.QQID()], + astFunc: c.funcs[n.QQID()], + localVars: c.localVars[n.QQID()], } // Fill in the TypeMap with all local variables. Note that they have @@ -646,7 +612,8 @@ type checker struct { c *Checker tm *t.Map reasonMap reasonMap - f Func + astFunc *a.Func + localVars typeMap errFilename string errLine uint32 diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 1924c28a..908d720e 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -55,10 +55,10 @@ var ( typeExprPlaceholder32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil) ) -// TypeMap maps from variable names (as token IDs) to types. -type TypeMap map[t.ID]*a.TypeExpr +// typeMap maps from variable names (as token IDs) to types. +type typeMap map[t.ID]*a.TypeExpr -var builtInTypeMap = TypeMap{ +var builtInTypeMap = typeMap{ t.IDU8: typeExprU8, t.IDU16: typeExprU16, t.IDU32: typeExprU32, @@ -152,7 +152,7 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { } else if f != nil { return f, nil } - if f := c.funcs[qqid].Func; f != nil { + if f := c.funcs[qqid]; f != nil { return f, nil } // lTyp is from a used package: `use "foo"` followed by `foo.bar`. diff --git a/lang/check/type.go b/lang/check/type.go index 638f7fbe..e4bb27d3 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -50,13 +50,13 @@ func (q *checker) tcheckVars(block []*a.Node) error { case a.KVar: o := o.Var() name := o.Name() - if _, ok := q.f.LocalVars[name]; ok { + if _, ok := q.localVars[name]; ok { return fmt.Errorf("check: duplicate var %q", name.Str(q.tm)) } if err := q.tcheckTypeExpr(o.XType(), 0); err != nil { return err } - q.f.LocalVars[name] = o.XType() + q.localVars[name] = o.XType() case a.KWhile: if err := q.tcheckVars(o.While().Body()); err != nil { @@ -388,8 +388,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return nil } else if id1.IsIdent() { - if q.f.LocalVars != nil { - if typ, ok := q.f.LocalVars[id1]; ok { + if q.localVars != nil { + if typ, ok := q.localVars[id1]; ok { n.SetMType(typ) return nil } @@ -398,7 +398,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { // TODO: check somewhere that a global ident (i.e. a const) is // not directly in the LHS of an assignment. n.SetGlobalIdent() - n.SetMType(c.Const.XType()) + n.SetMType(c.XType()) return nil } // TODO: look for other (global) names: consts, funcs, statuses, @@ -524,7 +524,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { nominalKeyword := n.Operator() declaredKeyword := t.ID(0) if s, ok := q.c.statuses[n.StatusQID()]; ok { - declaredKeyword = s.Status.Keyword() + declaredKeyword = s.Keyword() } else { msg, _ := t.Unescape(n.Ident().Str(q.tm)) z, ok := builtin.StatusMap[msg] @@ -717,7 +717,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { if err != nil { return err } else if f == nil { - f = q.c.funcs[qqid].Func + f = q.c.funcs[qqid] } if f != nil { n.SetMType(a.NewTypeExpr(t.IDOpenParen, 0, n.Ident(), lTyp.Node(), nil, nil)) @@ -725,16 +725,16 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { } s := (*a.Struct)(nil) - if q.f.Func != nil && lQID[0] == 0 { + if q.astFunc != nil && lQID[0] == 0 { switch lQID[1].Key() { case t.KeyIn: - s = q.f.Func.In() + s = q.astFunc.In() case t.KeyOut: - s = q.f.Func.Out() + s = q.astFunc.Out() } } if s == nil { - s = q.c.structs[lQID].Struct + s = q.c.structs[lQID] if s == nil && builtInTypeMap[lQID[1]] == nil { return fmt.Errorf("check: no struct type %q found for expression %q", lTyp.Str(q.tm), lhs.Str(q.tm)) } @@ -1068,7 +1068,7 @@ swtch: break swtch } for _, s := range q.c.structs { - if s.QID == qid { + if s.QID() == qid { break swtch } }