From ed03c5943ae8903968f670336d1b70a854104ba5 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 29 Dec 2017 12:08:17 +1100 Subject: [PATCH] Add pkg to ast Func, Struct and TypeExpr nodes --- cmd/wuffs-c/internal/cgen/cgen.go | 55 ++++++++-------- cmd/wuffs-c/internal/cgen/expr.go | 21 +++--- cmd/wuffs-c/internal/cgen/func.go | 39 ++++++----- cmd/wuffs-c/internal/cgen/statement.go | 9 ++- cmd/wuffs-c/internal/cgen/var.go | 31 +++++---- cmd/wuffs/gen.go | 6 +- lang/ast/ast.go | 60 ++++++++++------- lang/ast/sort.go | 15 ++--- lang/ast/string.go | 10 ++- lang/check/bounds.go | 32 ++++++--- lang/check/check.go | 85 ++++++++++++------------ lang/check/check_test.go | 2 +- lang/check/resolve.go | 89 ++++++++++++-------------- lang/check/type.go | 72 ++++++++++++--------- lang/parse/parse.go | 6 +- lang/token/list.go | 24 ++++++- 16 files changed, 311 insertions(+), 245 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index 4f07db70..52c22433 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -165,21 +165,21 @@ type gen struct { files []*a.File statusList []status - statusMap map[t.ID]status + statusMap map[t.QID]status structList []*a.Struct - structMap map[t.ID]*a.Struct + structMap map[t.QID]*a.Struct usesList []string usesMap map[string]struct{} currFunk funk - funks map[t.QID]funk + funks map[t.QQID]funk wuffsRoot string } func (g *gen) generate() ([]byte, error) { b := new(buffer) - g.statusMap = map[t.ID]status{} + g.statusMap = map[t.QID]status{} if err := g.forEachStatus(b, bothPubPri, (*gen).gatherStatuses); err != nil { return nil, err } @@ -198,12 +198,12 @@ func (g *gen) generate() ([]byte, error) { if !ok { return nil, fmt.Errorf("cyclical struct definitions") } - g.structMap = map[t.ID]*a.Struct{} + g.structMap = map[t.QID]*a.Struct{} for _, n := range g.structList { - g.structMap[n.Name()] = n + g.structMap[n.QID()] = n } - g.funks = map[t.QID]funk{} + g.funks = map[t.QQID]funk{} if err := g.forEachFunc(nil, bothPubPri, (*gen).gatherFuncImpl); err != nil { return nil, err } @@ -455,15 +455,17 @@ func (g *gen) cName(name string) string { func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) { if typ.Decorator() == 0 { - switch typ.Name().Key() { - case t.KeyU8: - return 1, nil - case t.KeyU16: - return 2, nil - case t.KeyU32: - return 4, nil - case t.KeyU64: - return 8, nil + if qid := typ.QID(); qid[0] == 0 { + switch qid[1].Key() { + case t.KeyU8: + return 1, nil + case t.KeyU16: + return 2, nil + case t.KeyU32: + return 4, nil + case t.KeyU64: + return 8, nil + } } } return 0, fmt.Errorf("unknown sizeof for %q", typ.Str(g.tm)) @@ -485,7 +487,7 @@ func (g *gen) gatherStatuses(b *buffer, n *a.Status) error { keyword: n.Keyword(), } g.statusList = append(g.statusList, s) - g.statusMap[n.QID()[1]] = s + g.statusMap[n.QID()] = s return nil } @@ -530,7 +532,7 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error { // set "self->private_impl.status = WUFFS_ETC__ERROR_BAD_WUFFS_VERSION;" // regardless of the sizeof(*self) struct reserved by the caller and even // if the caller and callee were built with different versions. - structName := n.Name().Str(g.tm) + structName := n.QID().Str(g.tm) b.writes("typedef struct {\n") b.writes("// Do not access the private_impl's fields directly. There is no API/ABI\n") b.writes("// compatibility or safety guarantee if you do so. Instead, use the\n") @@ -562,10 +564,10 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error { continue } o := tld.Func() - if o.Receiver() != n.Name() || !o.Suspendible() { + if o.Receiver() != n.QID() || !o.Suspendible() { continue } - k := g.funks[o.QID()] + k := g.funks[o.QQID()] if k.coroSuspPoint == 0 && !k.usesScratch { continue } @@ -581,7 +583,7 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error { if k.usesScratch { b.writes("uint64_t scratch;\n") } - b.printf("} %s%s[%d];\n", cPrefix, o.Name().Str(g.tm), maxDepth) + b.printf("} %s%s[%d];\n", cPrefix, o.FuncName().Str(g.tm), maxDepth) } } } @@ -645,7 +647,7 @@ func (g *gen) writeUse(b *buffer, n *a.Use) error { } func (g *gen) writeInitializerSignature(b *buffer, n *a.Struct, public bool) error { - structName := n.Name().Str(g.tm) + structName := n.QID().Str(g.tm) if public { b.printf("// %s%s__initialize is an initializer function.\n", g.pkgPrefix, structName) b.printf("//\n") @@ -707,17 +709,18 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error { } prefix := g.pkgPrefix - if otherPkgID := x.Decorator(); otherPkgID != 0 { + qid := x.QID() + if qid[0] != 0 { // See gen.writeCTypeName for a related TODO with otherPkg. - otherPkg := g.tm.ByID(otherPkgID) + otherPkg := g.tm.ByID(qid[0]) prefix = "wuffs_" + otherPkg + "__" - } else if g.structMap[x.Name()] == nil { + } else if g.structMap[qid] == nil { // Skip field types like u32 and bool. continue } b.printf("%s%s__initialize(&self->private_impl.%s%s, WUFFS_VERSION, WUFFS_BASE__ALREADY_ZEROED);\n", - prefix, x.Name().Str(g.tm), fPrefix, f.Name().Str(g.tm)) + prefix, qid[1].Str(g.tm), fPrefix, f.Name().Str(g.tm)) } b.writes("}\n\n") diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 8efd8463..4caca35d 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -440,7 +440,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil case t.KeyError, t.KeyStatus, t.KeySuspension: - status := g.statusMap[n.ID1()] + status := g.statusMap[t.QID{0, n.ID1()}] if status.name == "" { msg, _ := t.Unescape(n.ID1().Str(g.tm)) z := builtin.StatusMap[msg] @@ -523,7 +523,7 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var // arrays of slices, slices of pointers, etc. if n.IsSliceType() { o := n.Inner() - if o.Decorator() == 0 && o.Name().Key() == t.KeyU8 && !o.IsRefined() { + if o.Decorator() == 0 && o.QID() == (t.QID{0, t.IDU8}) && !o.IsRefined() { b.writes("wuffs_base__slice_u8") b.writeb(' ') b.writes(varNamePrefix) @@ -555,16 +555,19 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var } fallback := true - if key := innermost.Name().Key(); key < t.Key(len(cTypeNames)) { - if s := cTypeNames[key]; s != "" { - b.writes(s) - fallback = false + if qid := innermost.QID(); qid[0] == 0 { + if key := qid[1].Key(); key < t.Key(len(cTypeNames)) { + if s := cTypeNames[key]; s != "" { + b.writes(s) + fallback = false + } } } if fallback { prefix := g.pkgPrefix - if otherPkgID := innermost.Decorator(); otherPkgID != 0 { - otherPkg := g.tm.ByID(otherPkgID) + qid := innermost.QID() + if qid[0] != 0 { + otherPkg := g.tm.ByID(qid[0]) // TODO: map the "deflate" in "deflate.decoder" to the "deflate" in // `use "std/deflate"`, and use the latter "deflate". // @@ -578,7 +581,7 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var // See gen.writeInitializerImpl for a similar use of otherPkg. prefix = "wuffs_" + otherPkg + "__" } - b.printf("%s%s", prefix, innermost.Name().Str(g.tm)) + b.printf("%s%s", prefix, qid[1].Str(g.tm)) } for i := 0; i < numPointers; i++ { diff --git a/cmd/wuffs-c/internal/cgen/func.go b/cmd/wuffs-c/internal/cgen/func.go index 7901f03a..9da8f073 100644 --- a/cmd/wuffs-c/internal/cgen/func.go +++ b/cmd/wuffs-c/internal/cgen/func.go @@ -58,10 +58,13 @@ func (k *funk) jumpTarget(n a.Loop) (uint32, error) { } func (g *gen) funcCName(n *a.Func) string { - if r := n.Receiver(); r != 0 { - return g.pkgPrefix + r.Str(g.tm) + "__" + n.Name().Str(g.tm) + if r := n.Receiver(); !r.IsZero() { + // TODO: this isn't right if r[0] != 0, i.e. the receiver is from a + // used package. There might be similar cases elsewhere in this + // package. + return g.pkgPrefix + r.Str(g.tm) + "__" + n.FuncName().Str(g.tm) } - return g.pkgPrefix + n.Name().Str(g.tm) + return g.pkgPrefix + n.FuncName().Str(g.tm) } func (g *gen) writeFuncSignature(b *buffer, n *a.Func) error { @@ -87,7 +90,7 @@ func (g *gen) writeFuncSignature(b *buffer, n *a.Func) error { b.writeb('(') comma := false - if r := n.Receiver(); r != 0 { + if r := n.Receiver(); !r.IsZero() { b.printf("%s%s *self", g.pkgPrefix, r.Str(g.tm)) comma = true } @@ -115,7 +118,7 @@ func (g *gen) writeFuncPrototype(b *buffer, n *a.Func) error { } func (g *gen) writeFuncImpl(b *buffer, n *a.Func) error { - k := g.funks[n.QID()] + k := g.funks[n.QQID()] if err := g.writeFuncSignature(b, n); err != nil { return err @@ -161,13 +164,13 @@ func (g *gen) gatherFuncImpl(_ *buffer, n *a.Func) error { if g.currFunk.tempW != g.currFunk.tempR { return fmt.Errorf("internal error: temporary variable count out of sync") } - g.funks[n.QID()] = g.currFunk + g.funks[n.QQID()] = g.currFunk return nil } func (g *gen) writeFuncImplHeader(b *buffer) error { // Check the previous status and the "self" arg. - if g.currFunk.public && g.currFunk.astFunc.Receiver() != 0 { + if g.currFunk.public && !g.currFunk.astFunc.Receiver().IsZero() { b.writes("if (!self) {") if g.currFunk.suspendible { b.printf("return %sERROR_BAD_RECEIVER;", g.PKGPREFIX) @@ -224,7 +227,7 @@ func (g *gen) writeFuncImplBodyResume(b *buffer) error { if g.currFunk.suspendible { // TODO: don't hard-code [0], and allow recursive coroutines. b.printf("uint32_t coro_susp_point = self->private_impl.%s%s[0].coro_susp_point;\n", - cPrefix, g.currFunk.astFunc.Name().Str(g.tm)) + cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm)) b.printf("if (coro_susp_point) {\n") if err := g.writeResumeSuspend(b, g.currFunk.astFunc.Body(), false); err != nil { return err @@ -256,14 +259,14 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error { b.writes("\ngoto ok;\n") // Avoid the "unused label" warning. b.writes("ok:\n") b.printf("self->private_impl.%s%s[0].coro_susp_point = 0;\n", - cPrefix, g.currFunk.astFunc.Name().Str(g.tm)) + cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm)) b.writes("goto exit; }\n\n") // Close the coroutine switch. b.writes("goto suspend;\n") // Avoid the "unused label" warning. b.writes("suspend:\n") b.printf("self->private_impl.%s%s[0].coro_susp_point = coro_susp_point;\n", - cPrefix, g.currFunk.astFunc.Name().Str(g.tm)) + cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm)) if err := g.writeResumeSuspend(b, g.currFunk.astFunc.Body(), true); err != nil { return err } @@ -330,12 +333,14 @@ func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error { } } } - if key := oTyp.Name().Key(); key < t.Key(len(numTypeBounds)) { - ntb := numTypeBounds[key] - for i := 0; i < 2; i++ { - if bounds[i] != nil && ntb[i] != nil && bounds[i].Cmp(ntb[i]) == 0 { - bounds[i] = nil - continue + if qid := oTyp.QID(); qid[0] == 0 { + if key := qid[1].Key(); key < t.Key(len(numTypeBounds)) { + ntb := numTypeBounds[key] + for i := 0; i < 2; i++ { + if bounds[i] != nil && ntb[i] != nil && bounds[i].Cmp(ntb[i]) == 0 { + bounds[i] = nil + continue + } } } } @@ -365,7 +370,7 @@ func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error { b.writes(") {") if g.currFunk.suspendible { b.printf("status = %sERROR_BAD_ARGUMENT; goto exit;", g.PKGPREFIX) - } else if n.Receiver() != 0 { + } else if !n.Receiver().IsZero() { b.printf("self->private_impl.status = %sERROR_BAD_ARGUMENT; return;", g.PKGPREFIX) } else { b.printf("return;") diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index c976898c..75d2acd7 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -275,8 +275,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { return err } } - switch n.XType().Decorator().Key() { - case t.KeyOpenBracket: + if n.XType().Decorator().Key() == t.KeyOpenBracket { if n.Value() != nil { // TODO: something like: // cv := n.XType().ArrayLength().ConstValue() @@ -289,7 +288,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { name := n.Name().Str(g.tm) b.printf("memset(%s%s, 0, sizeof(%s%s));\n", vPrefix, name, vPrefix, name) - default: + } else { b.printf("%s%s = ", vPrefix, n.Name().Str(g.tm)) if v := n.Value(); v != nil { if err := g.writeExpr(b, v, replaceCallSuspendibles, parenthesesMandatory, 0); err != nil { @@ -487,7 +486,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { g.currFunk.usesScratch = true // TODO: don't hard-code [0], and allow recursive coroutines. scratchName := fmt.Sprintf("self->private_impl.%s%s[0].scratch", - cPrefix, g.currFunk.astFunc.Name().Str(g.tm)) + cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm)) b.printf("%s = ", scratchName) x := n.Args()[0].Arg().Value() @@ -715,7 +714,7 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia g.currFunk.usesScratch = true // TODO: don't hard-code [0], and allow recursive coroutines. scratchName := fmt.Sprintf("self->private_impl.%s%s[0].scratch", - cPrefix, g.currFunk.astFunc.Name().Str(g.tm)) + cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm)) b.printf("if (WUFFS_BASE__LIKELY(%srend_src - %srptr_src >= %d)) {", bPrefix, bPrefix, size/8) b.printf("%s%d = wuffs_base__load_u%d%s(%srptr_src);\n", tPrefix, temp1, size, endianness, bPrefix) diff --git a/cmd/wuffs-c/internal/cgen/var.go b/cmd/wuffs-c/internal/cgen/var.go index cd624e6e..993226b4 100644 --- a/cmd/wuffs-c/internal/cgen/var.go +++ b/cmd/wuffs-c/internal/cgen/var.go @@ -63,8 +63,11 @@ func (g *gen) findDerivedVars() { if oTyp.Decorator() != 0 { continue } - if key := oTyp.Name().Key(); key != t.KeyReader1 && key != t.KeyWriter1 { + switch oTyp.QID() { + default: continue + case t.QID{0, t.IDReader1}, t.QID{0, t.IDWriter1}: + // No-op. } if !g.needDerivedVar(o.Name()) { continue @@ -90,8 +93,8 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header return nil } nameStr := name.Str(g.tm) - switch typ.Name().Key() { - case t.KeyReader1: + switch typ.QID() { + case t.QID{0, t.IDReader1}: if header { b.printf("uint8_t* %srptr_%s = NULL;", bPrefix, nameStr) b.printf("uint8_t* %srstart_%s = NULL;", bPrefix, nameStr) @@ -119,7 +122,7 @@ func (g *gen) writeLoadDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, header b.printf("}\n") - case t.KeyWriter1: + case t.QID{0, t.IDWriter1}: if header { b.printf("uint8_t* %swptr_%s = NULL;", bPrefix, nameStr) b.printf("uint8_t* %swstart_%s = NULL;", bPrefix, nameStr) @@ -167,8 +170,8 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer return nil } nameStr := name.Str(g.tm) - switch typ.Name().Key() { - case t.KeyReader1: + switch typ.QID() { + case t.QID{0, t.IDReader1}: b.printf("if (%s%s.buf) {", aPrefix, nameStr) b.printf("size_t n = %srptr_%s - (%s%s.buf->ptr + %s%s.buf->ri);", @@ -189,7 +192,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr, footer b.printf("}\n") - case t.KeyWriter1: + case t.QID{0, t.IDWriter1}: b.printf("if (%s%s.buf) {", aPrefix, nameStr) b.printf("size_t n = %swptr_%s - (%s%s.buf->ptr + %s%s.buf->wi);", @@ -304,8 +307,10 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool) error { switch typ.Decorator().Key() { case 0: - if key := typ.Name().Key(); key < t.Key(len(cTypeNames)) { - rhs = cTypeNames[key] + if qid := typ.QID(); qid[0] == 0 { + if key := qid[1].Key(); key < t.Key(len(cTypeNames)) { + rhs = cTypeNames[key] + } } case t.KeyColon: // TODO: don't assume that the slice is a slice of u8. @@ -319,7 +324,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool) error { } else { lhs := local // TODO: don't hard-code [0], and allow recursive coroutines. - rhs := fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.Name().Str(g.tm), lhs) + rhs := fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm), lhs) if suspend { lhs, rhs = rhs, lhs } @@ -333,7 +338,11 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, suspend bool) error { if inner.Decorator() != 0 { break } - switch inner.Name().Key() { + qid := inner.QID() + if qid[0] != 0 { + break + } + switch qid[1].Key() { case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64: b.printf("memcpy(%s, %s, sizeof(%s));\n", lhs, rhs, local) return nil diff --git a/cmd/wuffs/gen.go b/cmd/wuffs/gen.go index 70f1ecbb..888f4cf4 100644 --- a/cmd/wuffs/gen.go +++ b/cmd/wuffs/gen.go @@ -267,11 +267,11 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error } else if n.Impure() { effect = "!" } - if n.Receiver() == 0 { + if n.Receiver().IsZero() { return fmt.Errorf("TODO: genWuffs for a free-standing function") } // TODO: look at n.Asserts(). - fmt.Fprintf(out, "pub func %s.%s%s(", n.Receiver().Str(&h.tm), n.Name().Str(&h.tm), effect) + fmt.Fprintf(out, "pub func %s.%s%s(", n.Receiver().Str(&h.tm), n.FuncName().Str(&h.tm), effect) for i, param := range [2]*a.Struct{n.In(), n.Out()} { if i > 0 { fmt.Fprintf(out, ")(") @@ -304,7 +304,7 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error if n.Suspendible() { effect = "?" } - fmt.Fprintf(out, "pub struct %s%s()\n", n.Name().Str(&h.tm), effect) + fmt.Fprintf(out, "pub struct %s%s()\n", n.QID().Str(&h.tm), effect) } } } diff --git a/lang/ast/ast.go b/lang/ast/ast.go index ff29fa9e..a3e002ed 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -143,15 +143,15 @@ type Node struct { // Expr operator literal/ident . Expr // Field . . name Field // File . . . File - // Func receiver . name Func + // Func funcName receiverPkg receiverName Func // If . . . If // Iterate . label . Iterate // Jump keyword label . Jump // PackageID . literal . PackageID // Ret keyword . . Ret // Status keyword pkg literal Status - // Struct . . name Struct - // TypeExpr decorator . name TypeExpr + // Struct . pkg name Struct + // TypeExpr decorator pkg name TypeExpr // Use . literal . Use // Var operator . name Var // While . label . While @@ -238,11 +238,21 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l } func (n *Raw) SetPackage(tm *t.Map, pkg t.ID) error { return n.Node().Walk(func(o *Node) error { switch o.Kind() { - case KConst, KStatus: + case KConst, KFunc, KStatus, KStruct, KTypeExpr: if o.id1 != 0 { return fmt.Errorf(`invalid SetPackage(%q) call: old package: got %q, want ""`, pkg.Str(tm), o.id1.Str(tm)) } + + // TODO: Add signed integer types, as per the builtin.Types var? + // + // Or, don't hard code these, and instead require built-in types to + // have qualified names, such as "builtin.u8" or "base.reader1"? + switch o.id2.Key() { + case t.KeyU8, t.KeyU16, t.KeyU32, t.KeyU64, t.KeyBool, t.KeyStatus, t.KeyReader1, t.KeyWriter1: + return nil + } + o.id1 = pkg } return nil @@ -574,7 +584,8 @@ const MaxTypeExprDepth = 63 // TypeExpr is a type expression, such as "u32", "u32[..8]", "pkg.foo", "ptr // T", "[8] T" or "[] T": -// - ID0: <0|package name|IDPtr|IDOpenBracket|IDColon> +// - ID0: <0|IDPtr|IDOpenBracket|IDColon|IDOpenParen> +// - ID1: <0|pkg> // - ID2: <0|type name> // - LHS: // - MHS: @@ -586,23 +597,25 @@ const MaxTypeExprDepth = 63 // // An IDColon ID0 means "[] RHS". RHS is the inner type. // -// An IDOpenParen ID0 means "LHS.ID1", a function or method type. LHS is the +// An IDOpenParen ID0 means "LHS.ID2", a function or method type. LHS is the // receiver type, which may be nil. If non-nil, it will be a pointee type: "T" // instead of "ptr T", "ptr ptr T", etc. // // TODO: method effects: "foo" vs "foo!" vs "foo?". // -// Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" or -// "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be -// refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For -// example, the LHS for "u32[..4095]" is nil. +// A zero ID0 means a (possibly package-qualified) type like "pkg.foo" or +// "foo". ID1 is the "pkg" or zero, ID2 is the "foo". // -// TODO: struct types, list types. +// Numeric types can be refined as "foo[LHS..MHS]". LHS and MHS are Expr's, +// possibly nil. For example, the LHS for "u32[..4095]" is nil. +// +// TODO: struct types, list types, nptr vs ptr. type TypeExpr Node func (n *TypeExpr) Node() *Node { return (*Node)(n) } func (n *TypeExpr) Decorator() t.ID { return n.id0 } -func (n *TypeExpr) Name() t.ID { return n.id2 } +func (n *TypeExpr) QID() t.QID { return t.QID{n.id1, n.id2} } +func (n *TypeExpr) FuncName() t.ID { return n.id2 } func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() } func (n *TypeExpr) Receiver() *TypeExpr { return n.lhs.TypeExpr() } func (n *TypeExpr) Bounds() [2]*Expr { return [2]*Expr{n.lhs.Expr(), n.mhs.Expr()} } @@ -676,10 +689,11 @@ func (n *TypeExpr) Unrefined() *TypeExpr { return &o } -func NewTypeExpr(decorator t.ID, name t.ID, alenRecvMin *Node, max *Expr, inner *TypeExpr) *TypeExpr { +func NewTypeExpr(decorator t.ID, pkg t.ID, name t.ID, alenRecvMin *Node, max *Expr, inner *TypeExpr) *TypeExpr { return &TypeExpr{ kind: KTypeExpr, id0: decorator, + id1: pkg, id2: name, lhs: alenRecvMin, mhs: max.Node(), @@ -694,8 +708,9 @@ const MaxBodyDepth = 255 // - FlagsImpure is "ID1" vs "ID1!" // - FlagsSuspendible is "ID1" vs "ID1?", it implies FlagsImpure // - FlagsPublic is "pub" vs "pri" -// - ID0: <0|receiver> -// - ID2: name +// - ID0: funcName +// - ID1: <0|receiverPkg> (set by calling SetPackage) +// - ID2: <0|receiverName> // - LHS: in-parameters // - RHS: out-parameters // - List1: asserts @@ -720,22 +735,22 @@ func (n *Func) Suspendible() bool { return n.flags&FlagsSuspendible != 0 } func (n *Func) Public() bool { return n.flags&FlagsPublic != 0 } func (n *Func) Filename() string { return n.filename } func (n *Func) Line() uint32 { return n.line } -func (n *Func) QID() t.QID { return t.QID{n.id0, n.id2} } -func (n *Func) Receiver() t.ID { return n.id0 } -func (n *Func) Name() t.ID { return n.id2 } +func (n *Func) QQID() t.QQID { return t.QQID{n.id1, n.id2, n.id0} } +func (n *Func) Receiver() t.QID { return t.QID{n.id1, n.id2} } +func (n *Func) FuncName() t.ID { return n.id0 } func (n *Func) In() *Struct { return n.lhs.Struct() } func (n *Func) Out() *Struct { return n.rhs.Struct() } func (n *Func) Asserts() []*Node { return n.list1 } func (n *Func) Body() []*Node { return n.list2 } -func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID, in *Struct, out *Struct, asserts []*Node, body []*Node) *Func { +func NewFunc(flags Flags, filename string, line uint32, receiverName t.ID, funcName t.ID, in *Struct, out *Struct, asserts []*Node, body []*Node) *Func { return &Func{ kind: KFunc, flags: flags, filename: filename, line: line, - id0: receiver, - id2: name, + id0: funcName, + id2: receiverName, lhs: in.Node(), rhs: out.Node(), list1: asserts, @@ -799,6 +814,7 @@ func NewConst(flags Flags, filename string, line uint32, name t.ID, xType *TypeE // Struct is "struct ID1(List0)": // - FlagsSuspendible is "ID1" vs "ID1?" // - FlagsPublic is "pub" vs "pri" +// - ID1: <0|pkg> (set by calling SetPackage) // - ID2: name // - List0: fields type Struct Node @@ -808,7 +824,7 @@ func (n *Struct) Suspendible() bool { return n.flags&FlagsSuspendible != 0 } func (n *Struct) Public() bool { return n.flags&FlagsPublic != 0 } func (n *Struct) Filename() string { return n.filename } func (n *Struct) Line() uint32 { return n.line } -func (n *Struct) Name() t.ID { return n.id2 } +func (n *Struct) QID() t.QID { return t.QID{n.id1, n.id2} } func (n *Struct) Fields() []*Node { return n.list0 } func NewStruct(flags Flags, filename string, line uint32, name t.ID, fields []*Node) *Struct { diff --git a/lang/ast/sort.go b/lang/ast/sort.go index cf3aa22c..78020256 100644 --- a/lang/ast/sort.go +++ b/lang/ast/sort.go @@ -22,14 +22,14 @@ 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{} + byQID := map[t.QID]*Struct{} for _, n := range ns { - byName[n.Name()] = n + byQID[n.QID()] = n } marks := map[*Struct]uint8{} for _, n := range ns { if _, ok := marks[n]; !ok { - sorted, ok = tssVisit(sorted, n, byName, marks) + sorted, ok = tssVisit(sorted, n, byQID, marks) if !ok { return nil, false } @@ -38,7 +38,7 @@ func TopologicalSortStructs(ns []*Struct) (sorted []*Struct, ok bool) { return sorted, true } -func tssVisit(dst []*Struct, n *Struct, byName map[t.ID]*Struct, marks map[*Struct]uint8) ([]*Struct, bool) { +func tssVisit(dst []*Struct, n *Struct, byQID map[t.QID]*Struct, marks map[*Struct]uint8) ([]*Struct, bool) { const ( unmarked = 0 temporary = 1 @@ -54,12 +54,9 @@ func tssVisit(dst []*Struct, n *Struct, byName map[t.ID]*Struct, marks map[*Stru for _, f := range n.Fields() { x := f.Field().XType().Innermost() - if x.Decorator() != 0 { - continue - } - if o := byName[x.Name()]; o != nil { + if o := byQID[x.QID()]; o != nil { var ok bool - dst, ok = tssVisit(dst, o, byName, marks) + dst, ok = tssVisit(dst, o, byQID, marks) if !ok { return nil, false } diff --git a/lang/ast/string.go b/lang/ast/string.go index 3776c8da..60114169 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -176,7 +176,7 @@ func (n *TypeExpr) Str(tm *t.Map) string { return "" } if n.Decorator() == 0 && n.Min() == nil && n.Max() == nil { - return tm.ByID(n.Name()) + return n.QID().Str(tm) } return string(n.appendStr(nil, tm, 0)) } @@ -192,7 +192,7 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte { switch n.Decorator().Key() { case 0: - buf = append(buf, tm.ByID(n.Name())...) + buf = append(buf, n.QID().Str(tm)...) case t.KeyPtr: buf = append(buf, "ptr "...) return n.Inner().appendStr(buf, tm, depth) @@ -208,11 +208,9 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte { buf = append(buf, "func "...) buf = n.Receiver().appendStr(buf, tm, depth) buf = append(buf, '.') - return append(buf, tm.ByID(n.Name())...) + return append(buf, n.FuncName().Str(tm)...) default: - buf = append(buf, tm.ByID(n.Decorator())...) - buf = append(buf, '.') - buf = append(buf, tm.ByID(n.Name())...) + return append(buf, "!invalid_type!"...) } if n.Min() != nil || n.Max() != nil { buf = append(buf, '[') diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 53615bf8..1ff92166 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -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 } diff --git a/lang/check/check.go b/lang/check/check.go index 52e88375..4a10c2ef 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -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 diff --git a/lang/check/check_test.go b/lang/check/check_test.go index 95e11d39..59627340 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -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) } diff --git a/lang/check/resolve.go b/lang/check/resolve.go index b31f243c..2b72b6e7 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -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 } } diff --git a/lang/check/type.go b/lang/check/type.go index 81983250..faff7818 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -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 diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 418acbe2..5cba3ac8 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -384,7 +384,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { if err != nil { return nil, err } - return a.NewTypeExpr(t.IDPtr, 0, nil, nil, rhs), nil + return a.NewTypeExpr(t.IDPtr, 0, 0, nil, nil, rhs), nil } if p.peek1().Key() == t.KeyOpenBracket { @@ -407,7 +407,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { if err != nil { return nil, err } - return a.NewTypeExpr(decorator, 0, lhs.Node(), nil, rhs), nil + return a.NewTypeExpr(decorator, 0, 0, lhs.Node(), nil, rhs), nil } pkg, name, err := p.parseQualifiedIdent() @@ -423,7 +423,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) { } } - return a.NewTypeExpr(pkg, name, lhs.Node(), mhs, nil), nil + return a.NewTypeExpr(0, pkg, name, lhs.Node(), mhs, nil), nil } // parseBracket parses "[i:j]", "[i:]", "[:j]" and "[:]". A double dot replaces diff --git a/lang/token/list.go b/lang/token/list.go index f31d7adb..f04ede29 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -99,12 +99,30 @@ func (x ID) IsXAssociativeOp() bool { return x.Key().isXOp() && x.IsAssociativeO // "bar"'s. QID[0] may be 0 for a plain "bar". type QID [2]ID +func (x QID) IsZero() bool { return x == QID{} } + // Str returns a string form of x. func (x QID) Str(m *Map) string { - if x[0] == 0 { - return m.ByID(x[1]) + if x[0] != 0 { + return m.ByID(x[0]) + "." + m.ByID(x[1]) } - return m.ByID(x[0]) + "." + m.ByID(x[1]) + return m.ByID(x[1]) +} + +// QQID is a double-qualified ID, such as "receiverPkg.receiverName.funcName". +type QQID [3]ID + +func (x QQID) IsZero() bool { return x == QQID{} } + +// Str returns a string form of x. +func (x QQID) Str(m *Map) string { + if x[0] != 0 { + return m.ByID(x[0]) + "." + m.ByID(x[1]) + "." + m.ByID(x[2]) + } + if x[1] != 0 { + return m.ByID(x[1]) + "." + m.ByID(x[2]) + } + return m.ByID(x[2]) } // Token combines an ID and the line number it was seen.