mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 00:55:11 +01:00
Add pkg to ast Func, Struct and TypeExpr nodes
This commit is contained in:
1 parent
10e4936f08
commit
ed03c5943a
16 files changed
+311
-245
No files matched your search
+22
-10
@@ -170,8 +170,10 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
|
||||
|
||||
func typeBounds(tm *t.Map, typ *a.TypeExpr) (*big.Int, *big.Int, error) {
|
||||
b := [2]*big.Int{}
|
||||
if key := typ.Name().Key(); typ.Decorator() == 0 && key < t.Key(len(numTypeBounds)) {
|
||||
b = numTypeBounds[key]
|
||||
if typ.Decorator() == 0 {
|
||||
if qid := typ.QID(); qid[0] == 0 && qid[1].Key() < t.Key(len(numTypeBounds)) {
|
||||
b = numTypeBounds[qid[1].Key()]
|
||||
}
|
||||
}
|
||||
if b[0] == nil || b[1] == nil {
|
||||
return nil, nil, nil
|
||||
@@ -940,7 +942,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
}
|
||||
lTyp := n.LHS().Expr().MType()
|
||||
return nil, nil, fmt.Errorf("check: no field named %q found in struct type %q for expression %q",
|
||||
n.ID1().Str(q.tm), lTyp.Name().Str(q.tm), n.Str(q.tm))
|
||||
n.ID1().Str(q.tm), lTyp.QID().Str(q.tm), n.Str(q.tm))
|
||||
}
|
||||
|
||||
if _, _, err := q.bcheckExpr(n.LHS().Expr(), depth); err != nil {
|
||||
@@ -1135,8 +1137,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
|
||||
if typ.IsIdeal() {
|
||||
typ = rhs.MType()
|
||||
}
|
||||
b := numTypeBounds[typ.Name().Key()]
|
||||
return b[0], b[1], nil
|
||||
if qid := typ.QID(); qid[0] == 0 {
|
||||
b := numTypeBounds[qid[1].Key()]
|
||||
return b[0], b[1], nil
|
||||
}
|
||||
}
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprBinaryOp", op)
|
||||
}
|
||||
@@ -1175,18 +1179,26 @@ func (q *checker) bcheckTypeExpr(typ *a.TypeExpr) (*big.Int, *big.Int, error) {
|
||||
}
|
||||
|
||||
switch typ.Decorator().Key() {
|
||||
// TODO: case t.KeyOpenParen.
|
||||
case t.KeyPtr, t.KeyOpenBracket, t.KeyColon:
|
||||
return nil, nil, nil
|
||||
}
|
||||
switch typ.Name().Key() {
|
||||
case t.KeyReader1, t.KeyWriter1:
|
||||
return nil, nil, nil
|
||||
|
||||
// TODO: is the special cases for reader1 and writer1 superfluous with the
|
||||
// general purpose code for built-ins below?
|
||||
if qid := typ.QID(); qid[0] == 0 {
|
||||
switch qid[1].Key() {
|
||||
case t.KeyReader1, t.KeyWriter1:
|
||||
return nil, nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
b := [2]*big.Int{}
|
||||
if key := typ.Name().Key(); key < t.Key(len(numTypeBounds)) {
|
||||
b = numTypeBounds[key]
|
||||
if qid := typ.QID(); qid[0] == 0 && qid[1].Key() < t.Key(len(numTypeBounds)) {
|
||||
b = numTypeBounds[qid[1].Key()]
|
||||
}
|
||||
// TODO: should || be && instead (see also func typeBounds)? Is this if
|
||||
// code superfluous?
|
||||
if b[0] == nil || b[1] == nil {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
+44
-41
@@ -66,7 +66,7 @@ type Const struct {
|
||||
}
|
||||
|
||||
type Func struct {
|
||||
QID t.QID // Qualified ID of the func name.
|
||||
QQID t.QQID // Double-qualified ID of the func name.
|
||||
Func *a.Func
|
||||
LocalVars TypeMap
|
||||
}
|
||||
@@ -77,15 +77,15 @@ type Status struct {
|
||||
}
|
||||
|
||||
type Struct struct {
|
||||
ID t.ID // ID of the struct name.
|
||||
QID t.QID // Qualified ID of the struct name.
|
||||
Struct *a.Struct
|
||||
}
|
||||
|
||||
type usee struct {
|
||||
consts map[t.QID]*a.Const
|
||||
funcs map[t.QID]*a.Func
|
||||
funcs map[t.QQID]*a.Func
|
||||
statuses map[t.QID]*a.Status
|
||||
structs map[t.ID]*a.Struct
|
||||
structs map[t.QID]*a.Struct
|
||||
}
|
||||
|
||||
func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte, error)) (*Checker, error) {
|
||||
@@ -117,9 +117,9 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte,
|
||||
reasonMap: rMap,
|
||||
packageID: base38.Max + 1,
|
||||
consts: map[t.QID]Const{},
|
||||
funcs: map[t.QID]Func{},
|
||||
funcs: map[t.QQID]Func{},
|
||||
statuses: map[t.QID]Status{},
|
||||
structs: map[t.ID]Struct{},
|
||||
structs: map[t.QID]Struct{},
|
||||
usees: map[t.ID]*usee{},
|
||||
}
|
||||
|
||||
@@ -177,24 +177,24 @@ type Checker struct {
|
||||
otherPackageID *a.PackageID
|
||||
|
||||
consts map[t.QID]Const
|
||||
funcs map[t.QID]Func
|
||||
funcs map[t.QQID]Func
|
||||
statuses map[t.QID]Status
|
||||
structs map[t.ID]Struct
|
||||
structs map[t.QID]Struct
|
||||
|
||||
// usees are the packages referred to in the `use "foo/bar"` lines. The
|
||||
// keys are the use path base: `bar`, not `"foo/bar"`.
|
||||
usees map[t.ID]*usee
|
||||
|
||||
builtInFuncs map[t.QID]*a.Func
|
||||
builtInSliceFuncs map[t.QID]*a.Func
|
||||
builtInFuncs map[t.QQID]*a.Func
|
||||
builtInSliceFuncs map[t.QQID]*a.Func
|
||||
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.QID]Func { return c.funcs }
|
||||
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.ID]Struct { return c.structs }
|
||||
func (c *Checker) Structs() map[t.QID]Struct { return c.structs }
|
||||
|
||||
func (c *Checker) checkPackageID(node *a.Node) error {
|
||||
n := node.PackageID()
|
||||
@@ -271,9 +271,9 @@ func (c *Checker) checkUse(node *a.Node) error {
|
||||
|
||||
u := &usee{
|
||||
consts: map[t.QID]*a.Const{},
|
||||
funcs: map[t.QID]*a.Func{},
|
||||
funcs: map[t.QQID]*a.Func{},
|
||||
statuses: map[t.QID]*a.Status{},
|
||||
structs: map[t.ID]*a.Struct{},
|
||||
structs: map[t.QID]*a.Struct{},
|
||||
}
|
||||
for _, n := range f.TopLevelDecls() {
|
||||
if err := n.Raw().SetPackage(c.tm, base); err != nil {
|
||||
@@ -285,13 +285,13 @@ func (c *Checker) checkUse(node *a.Node) error {
|
||||
u.consts[n.QID()] = n
|
||||
case a.KFunc:
|
||||
n := n.Func()
|
||||
u.funcs[n.QID()] = n
|
||||
u.funcs[n.QQID()] = n
|
||||
case a.KStatus:
|
||||
n := n.Status()
|
||||
u.statuses[n.QID()] = n
|
||||
case a.KStruct:
|
||||
n := n.Struct()
|
||||
u.structs[n.Name()] = n
|
||||
u.structs[n.QID()] = n
|
||||
}
|
||||
}
|
||||
c.usees[base] = u
|
||||
@@ -393,18 +393,18 @@ func (c *Checker) checkConstElement(n *a.Expr, nMin *big.Int, nMax *big.Int, nLi
|
||||
|
||||
func (c *Checker) checkStructDecl(node *a.Node) error {
|
||||
n := node.Struct()
|
||||
id := n.Name()
|
||||
if other, ok := c.structs[id]; ok {
|
||||
qid := n.QID()
|
||||
if other, ok := c.structs[qid]; ok {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("check: duplicate struct %q", id.Str(c.tm)),
|
||||
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(),
|
||||
}
|
||||
}
|
||||
c.structs[id] = Struct{
|
||||
ID: id,
|
||||
c.structs[qid] = Struct{
|
||||
QID: qid,
|
||||
Struct: n,
|
||||
}
|
||||
c.unsortedStructs = append(c.unsortedStructs, n)
|
||||
@@ -422,7 +422,7 @@ 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().Str(c.tm)),
|
||||
Err: fmt.Errorf("%v in struct %s", err, n.QID().Str(c.tm)),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
}
|
||||
@@ -455,7 +455,7 @@ func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool) error {
|
||||
}
|
||||
if dv := f.DefaultValue(); dv != nil {
|
||||
if f.XType().Decorator() != 0 {
|
||||
return fmt.Errorf("check: cannot set default value for qualified type %q for field %q",
|
||||
return fmt.Errorf("check: cannot set default value for type %q for field %q",
|
||||
f.XType().Str(c.tm), f.Name().Str(c.tm))
|
||||
}
|
||||
if err := q.tcheckExpr(dv, 0); err != nil {
|
||||
@@ -476,7 +476,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
n := node.Func()
|
||||
if err := c.checkFields(n.In().Fields(), false); err != nil {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("%v in in-params for func %q", err, n.Name().Str(c.tm)),
|
||||
Err: fmt.Errorf("%v in in-params for func %s", err, n.QQID().Str(c.tm)),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
}
|
||||
@@ -484,7 +484,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
n.In().Node().SetTypeChecked()
|
||||
if err := c.checkFields(n.Out().Fields(), false); err != nil {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("%v in out-params for func %q", err, n.Name().Str(c.tm)),
|
||||
Err: fmt.Errorf("%v in out-params for func %s", err, n.QQID().Str(c.tm)),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
}
|
||||
@@ -495,10 +495,10 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
// suspendible), that we end with a return statement? Or is that an
|
||||
// implicit "return out"?
|
||||
|
||||
qid := n.QID()
|
||||
if other, ok := c.funcs[qid]; ok {
|
||||
qqid := n.QQID()
|
||||
if other, ok := c.funcs[qqid]; ok {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("check: duplicate function %q", qid.Str(c.tm)),
|
||||
Err: fmt.Errorf("check: duplicate function %s", qqid.Str(c.tm)),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
OtherFilename: other.Func.Filename(),
|
||||
@@ -506,30 +506,32 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
}
|
||||
}
|
||||
|
||||
inTyp := a.NewTypeExpr(0, n.In().Name(), nil, nil, nil)
|
||||
iQID := n.In().QID()
|
||||
inTyp := a.NewTypeExpr(0, iQID[0], iQID[1], nil, nil, nil)
|
||||
inTyp.Node().SetTypeChecked()
|
||||
outTyp := a.NewTypeExpr(0, n.Out().Name(), nil, nil, nil)
|
||||
oQID := n.Out().QID()
|
||||
outTyp := a.NewTypeExpr(0, oQID[0], oQID[1], nil, nil, nil)
|
||||
outTyp.Node().SetTypeChecked()
|
||||
localVars := TypeMap{
|
||||
t.IDIn: inTyp,
|
||||
t.IDOut: outTyp,
|
||||
}
|
||||
if qid[0] != 0 {
|
||||
if _, ok := c.structs[qid[0]]; !ok {
|
||||
if qqid[1] != 0 {
|
||||
if _, ok := c.structs[t.QID{qqid[0], qqid[1]}]; !ok {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("check: no receiver struct defined for function %q", qid.Str(c.tm)),
|
||||
Err: fmt.Errorf("check: no receiver struct defined for function %s", qqid.Str(c.tm)),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
}
|
||||
}
|
||||
sTyp := a.NewTypeExpr(0, qid[0], nil, nil, nil)
|
||||
sTyp := a.NewTypeExpr(0, qqid[0], qqid[1], nil, nil, nil)
|
||||
sTyp.Node().SetTypeChecked()
|
||||
pTyp := a.NewTypeExpr(t.IDPtr, 0, nil, nil, sTyp)
|
||||
pTyp := a.NewTypeExpr(t.IDPtr, 0, 0, nil, nil, sTyp)
|
||||
pTyp.Node().SetTypeChecked()
|
||||
localVars[t.IDThis] = pTyp
|
||||
}
|
||||
c.funcs[qid] = Func{
|
||||
QID: qid,
|
||||
c.funcs[qqid] = Func{
|
||||
QQID: qqid,
|
||||
Func: n,
|
||||
LocalVars: localVars,
|
||||
}
|
||||
@@ -559,7 +561,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
|
||||
c: c,
|
||||
tm: c.tm,
|
||||
reasonMap: c.reasonMap,
|
||||
f: c.funcs[n.QID()],
|
||||
f: c.funcs[n.QQID()],
|
||||
}
|
||||
|
||||
// Fill in the TypeMap with all local variables. Note that they have
|
||||
@@ -630,10 +632,11 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
|
||||
func (c *Checker) checkFieldMethodCollisions(node *a.Node) error {
|
||||
n := node.Struct()
|
||||
for _, o := range n.Fields() {
|
||||
qid := t.QID{n.Name(), o.Field().Name()}
|
||||
if _, ok := c.funcs[qid]; ok {
|
||||
nQID := n.QID()
|
||||
qqid := t.QQID{nQID[0], nQID[1], o.Field().Name()}
|
||||
if _, ok := c.funcs[qqid]; ok {
|
||||
return fmt.Errorf("check: struct %q has both a field and method named %q",
|
||||
qid[0].Str(c.tm), qid[1].Str(c.tm))
|
||||
nQID.Str(c.tm), qqid[2].Str(c.tm))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -127,7 +127,7 @@ func TestCheck(tt *testing.T) {
|
||||
break
|
||||
}
|
||||
|
||||
if got, want := fooBar.QID.Str(tm), "foo.bar"; got != want {
|
||||
if got, want := fooBar.QQID.Str(tm), "foo.bar"; got != want {
|
||||
tt.Fatalf("Funcs[0] name: got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
|
||||
+42
-47
@@ -34,25 +34,25 @@ var (
|
||||
|
||||
// typeExprFoo is an *ast.Node MType (implicit type).
|
||||
var (
|
||||
typeExprGeneric = a.NewTypeExpr(0, t.IDDiamond, nil, nil, nil)
|
||||
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
|
||||
typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil)
|
||||
typeExprGeneric = a.NewTypeExpr(0, 0, t.IDDiamond, nil, nil, nil)
|
||||
typeExprIdeal = a.NewTypeExpr(0, 0, t.IDDoubleZ, nil, nil, nil)
|
||||
typeExprList = a.NewTypeExpr(0, 0, t.IDDollar, nil, nil, nil)
|
||||
|
||||
typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
|
||||
typeExprU16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
|
||||
typeExprU32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
|
||||
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
|
||||
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
||||
typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil)
|
||||
typeExprReader1 = a.NewTypeExpr(0, t.IDReader1, nil, nil, nil)
|
||||
typeExprWriter1 = a.NewTypeExpr(0, t.IDWriter1, nil, nil, nil)
|
||||
typeExprU8 = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil)
|
||||
typeExprU16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil)
|
||||
typeExprU32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil)
|
||||
typeExprU64 = a.NewTypeExpr(0, 0, t.IDU64, nil, nil, nil)
|
||||
typeExprBool = a.NewTypeExpr(0, 0, t.IDBool, nil, nil, nil)
|
||||
typeExprStatus = a.NewTypeExpr(0, 0, t.IDStatus, nil, nil, nil)
|
||||
typeExprReader1 = a.NewTypeExpr(0, 0, t.IDReader1, nil, nil, nil)
|
||||
typeExprWriter1 = a.NewTypeExpr(0, 0, t.IDWriter1, nil, nil, nil)
|
||||
|
||||
typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, nil, nil, typeExprU8)
|
||||
typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, 0, nil, nil, typeExprU8)
|
||||
|
||||
// TODO: delete this.
|
||||
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
|
||||
typeExprPlaceholder16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
|
||||
typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
|
||||
typeExprPlaceholder = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil)
|
||||
typeExprPlaceholder16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil)
|
||||
typeExprPlaceholder32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil)
|
||||
)
|
||||
|
||||
// TypeMap maps from variable names (as token IDs) to types.
|
||||
@@ -69,7 +69,7 @@ var builtInTypeMap = TypeMap{
|
||||
t.IDWriter1: typeExprWriter1,
|
||||
}
|
||||
|
||||
func (c *Checker) builtInFunc(qid t.QID) (*a.Func, error) {
|
||||
func (c *Checker) builtInFunc(qqid t.QQID) (*a.Func, error) {
|
||||
if c.builtInFuncs == nil {
|
||||
err := error(nil)
|
||||
c.builtInFuncs, err = parseBuiltInFuncs(c.tm, builtin.Funcs, false)
|
||||
@@ -77,10 +77,10 @@ func (c *Checker) builtInFunc(qid t.QID) (*a.Func, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c.builtInFuncs[qid], nil
|
||||
return c.builtInFuncs[qqid], nil
|
||||
}
|
||||
|
||||
func (c *Checker) builtInSliceFunc(qid t.QID) (*a.Func, error) {
|
||||
func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) {
|
||||
if c.builtInSliceFuncs == nil {
|
||||
err := error(nil)
|
||||
c.builtInSliceFuncs, err = parseBuiltInFuncs(c.tm, builtin.SliceFuncs, true)
|
||||
@@ -88,11 +88,11 @@ func (c *Checker) builtInSliceFunc(qid t.QID) (*a.Func, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c.builtInSliceFuncs[qid], nil
|
||||
return c.builtInSliceFuncs[qqid], nil
|
||||
}
|
||||
|
||||
func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QID]*a.Func, error) {
|
||||
m := map[t.QID]*a.Func{}
|
||||
func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QQID]*a.Func, error) {
|
||||
m := map[t.QQID]*a.Func{}
|
||||
buf := []byte(nil)
|
||||
opts := parse.Options{
|
||||
AllowBuiltIns: true,
|
||||
@@ -125,7 +125,7 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QID]*a.Func,
|
||||
return nil, fmt.Errorf("check: parsing %q: got %d top level decls, want %d", s, len(tlds), 1)
|
||||
}
|
||||
f := tlds[0].Func()
|
||||
m[f.QID()] = f
|
||||
m[f.QQID()] = f
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -135,37 +135,32 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
|
||||
return nil, fmt.Errorf("check: resolveFunc cannot look up non-func TypeExpr %q", typ.Str(c.tm))
|
||||
}
|
||||
lTyp := typ.Receiver()
|
||||
qid := t.QID{lTyp.Name(), typ.Name()}
|
||||
switch lTyp.Decorator().Key() {
|
||||
case 0:
|
||||
if f, err := c.builtInFunc(qid); err != nil {
|
||||
return nil, err
|
||||
} else if f != nil {
|
||||
return f, nil
|
||||
}
|
||||
if f := c.funcs[qid].Func; f != nil {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
case t.KeyColon:
|
||||
lQID := lTyp.QID()
|
||||
qqid := t.QQID{lQID[0], lQID[1], typ.FuncName()}
|
||||
if lTyp.Decorator().Key() == t.KeyColon {
|
||||
// lTyp is a slice.
|
||||
qid[0] = t.IDDiamond
|
||||
if f, err := c.builtInSliceFunc(qid); err != nil {
|
||||
qqid[0] = 0
|
||||
qqid[1] = t.IDDiamond
|
||||
if f, err := c.builtInSliceFunc(qqid); err != nil {
|
||||
return nil, err
|
||||
} else if f != nil {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
default:
|
||||
// lTyp is from a used package: `use "foo"` followed by `foo.bar`.
|
||||
u := c.usees[lTyp.Decorator()]
|
||||
if u == nil {
|
||||
return nil, fmt.Errorf("check: cannot resolve %q in type %q",
|
||||
lTyp.Decorator().Str(c.tm), lTyp.Str(c.tm))
|
||||
} else {
|
||||
if f, err := c.builtInFunc(qqid); err != nil {
|
||||
return nil, err
|
||||
} else if f != nil {
|
||||
return f, nil
|
||||
}
|
||||
if f := u.funcs[qid]; f != nil {
|
||||
// TODO: f.Receiver().Str(c.tm) should be "deflate.decoder", not
|
||||
// just "decoder".
|
||||
if f := c.funcs[qqid].Func; f != nil {
|
||||
return f, nil
|
||||
}
|
||||
// lTyp is from a used package: `use "foo"` followed by `foo.bar`.
|
||||
u := c.usees[qqid[0]]
|
||||
if u == nil {
|
||||
return nil, fmt.Errorf("check: cannot resolve %q in type %q", qqid[0].Str(c.tm), lTyp.Str(c.tm))
|
||||
}
|
||||
if f := u.funcs[qqid]; f != nil {
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
|
||||
+40
-32
@@ -511,7 +511,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return fmt.Errorf("check: %s is a slice expression but %s has type %s, not an array or slice type",
|
||||
n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm))
|
||||
case t.KeyOpenBracket:
|
||||
n.SetMType(a.NewTypeExpr(t.IDColon, 0, nil, nil, lTyp.Inner()))
|
||||
n.SetMType(a.NewTypeExpr(t.IDColon, 0, 0, nil, nil, lTyp.Inner()))
|
||||
case t.KeyColon:
|
||||
n.SetMType(lTyp)
|
||||
}
|
||||
@@ -566,7 +566,7 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
|
||||
genericType := (*a.TypeExpr)(nil)
|
||||
if f.Receiver().Key() == t.KeyDiamond {
|
||||
if f.Receiver() == (t.QID{0, t.IDDiamond}) {
|
||||
genericType = lhs.MType().Receiver()
|
||||
}
|
||||
|
||||
@@ -682,49 +682,51 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
||||
return err
|
||||
}
|
||||
lTyp := lhs.MType().Pointee()
|
||||
qid := t.QID{lTyp.Name(), n.ID1()}
|
||||
switch lTyp.Decorator().Key() {
|
||||
case 0:
|
||||
// No-op.
|
||||
lQID := lTyp.QID()
|
||||
qqid := t.QQID{lQID[0], lQID[1], n.ID1()}
|
||||
|
||||
case t.KeyColon:
|
||||
if key := lTyp.Decorator().Key(); key == t.KeyColon {
|
||||
// lTyp is a slice.
|
||||
qid[0] = t.IDDiamond
|
||||
if f, err := q.c.builtInSliceFunc(qid); err != nil {
|
||||
qqid[0] = 0
|
||||
qqid[1] = t.IDDiamond
|
||||
if f, err := q.c.builtInSliceFunc(qqid); err != nil {
|
||||
return err
|
||||
} else if f == nil {
|
||||
return fmt.Errorf("check: no slice method %q", n.ID1().Str(q.tm))
|
||||
}
|
||||
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
|
||||
n.SetMType(a.NewTypeExpr(t.IDOpenParen, 0, n.ID1(), lTyp.Node(), nil, nil))
|
||||
return nil
|
||||
} else if key != 0 {
|
||||
return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm))
|
||||
}
|
||||
|
||||
default:
|
||||
if qqid[0] != 0 {
|
||||
// lTyp is from a used package: `use "foo"` followed by `foo.bar`.
|
||||
if u := q.c.usees[lTyp.Decorator()]; u == nil {
|
||||
if u := q.c.usees[qqid[0]]; u == nil {
|
||||
return fmt.Errorf("check: cannot resolve %q in type %q for expression %q",
|
||||
lTyp.Decorator().Str(q.tm), lTyp.Str(q.tm), lhs.Str(q.tm))
|
||||
} else if u.funcs[qid] == nil {
|
||||
qqid[0].Str(q.tm), lTyp.Str(q.tm), lhs.Str(q.tm))
|
||||
} else if u.funcs[qqid] == nil {
|
||||
return fmt.Errorf("check: no method named %q found in type %q for expression %q",
|
||||
n.ID1().Str(q.tm), lTyp.Str(q.tm), n.Str(q.tm))
|
||||
}
|
||||
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
|
||||
n.SetMType(a.NewTypeExpr(t.IDOpenParen, 0, n.ID1(), lTyp.Node(), nil, nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := q.c.builtInFunc(qid)
|
||||
f, err := q.c.builtInFunc(qqid)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if f == nil {
|
||||
f = q.c.funcs[qid].Func
|
||||
f = q.c.funcs[qqid].Func
|
||||
}
|
||||
if f != nil {
|
||||
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
|
||||
n.SetMType(a.NewTypeExpr(t.IDOpenParen, 0, n.ID1(), lTyp.Node(), nil, nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
s := (*a.Struct)(nil)
|
||||
if q.f.Func != nil {
|
||||
switch lTyp.Name().Key() {
|
||||
if q.f.Func != nil && lQID[0] == 0 {
|
||||
switch lQID[1].Key() {
|
||||
case t.KeyIn:
|
||||
s = q.f.Func.In()
|
||||
case t.KeyOut:
|
||||
@@ -732,9 +734,8 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
}
|
||||
if s == nil {
|
||||
name := lTyp.Name()
|
||||
s = q.c.structs[name].Struct
|
||||
if s == nil && builtInTypeMap[name] == nil {
|
||||
s = q.c.structs[lQID].Struct
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -1034,8 +1035,19 @@ func (q *checker) tcheckTypeExpr(typ *a.TypeExpr, depth uint32) error {
|
||||
|
||||
swtch:
|
||||
switch typ.Decorator().Key() {
|
||||
// TODO: also check t.KeyOpenParen.
|
||||
case 0:
|
||||
if typ.Name().IsNumType() {
|
||||
qid := typ.QID()
|
||||
if qid[0] != 0 {
|
||||
if u := q.c.usees[qid[0]]; u == nil {
|
||||
return fmt.Errorf("check: cannot resolve %q in type %q", qid[0].Str(q.tm), typ.Str(q.tm))
|
||||
} else if u.structs[qid] == nil {
|
||||
return fmt.Errorf("check: cannot resolve type %q", typ.Str(q.tm))
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if qid[1].IsNumType() {
|
||||
for _, b := range typ.Bounds() {
|
||||
if b == nil {
|
||||
continue
|
||||
@@ -1052,15 +1064,15 @@ swtch:
|
||||
if typ.Min() != nil || typ.Max() != nil {
|
||||
// TODO: reject. You can only refine numeric types.
|
||||
}
|
||||
if _, ok := builtInTypeMap[typ.Name()]; ok {
|
||||
if _, ok := builtInTypeMap[qid[1]]; ok {
|
||||
break swtch
|
||||
}
|
||||
for _, s := range q.c.structs {
|
||||
if s.ID == typ.Name() {
|
||||
if s.QID == qid {
|
||||
break swtch
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("check: %q is not a type", typ.Name().Str(q.tm))
|
||||
return fmt.Errorf("check: %q is not a type", typ.Str(q.tm))
|
||||
|
||||
case t.KeyOpenBracket:
|
||||
aLen := typ.ArrayLength()
|
||||
@@ -1079,11 +1091,7 @@ swtch:
|
||||
}
|
||||
|
||||
default:
|
||||
if u := q.c.usees[typ.Decorator()]; u == nil {
|
||||
return fmt.Errorf("check: cannot resolve %q in type %q", typ.Decorator().Str(q.tm), typ.Str(q.tm))
|
||||
} else if u.structs[typ.Name()] == nil {
|
||||
return fmt.Errorf("check: cannot resolve type %q", typ.Str(q.tm))
|
||||
}
|
||||
return fmt.Errorf("check: %q is not a type", typ.Str(q.tm))
|
||||
}
|
||||
typ.Node().SetTypeChecked()
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user