Add pkg to ast Func, Struct and TypeExpr nodes

This commit is contained in:
Nigel Tao committed 2017-12-29 12:08:17 +11:00
1 parent 10e4936f08
commit ed03c5943a
16 files changed
+311 -245

No files matched your search

+29 -26
View File
@@ -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")
+12 -9
View File
@@ -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++ {
+22 -17
View File
@@ -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;")
+4 -5
View File
@@ -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)
+20 -11
View File
@@ -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
+3 -3
View File
@@ -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)
}
}
}