From 95391533b3159af5194694aee21b85901d198e6d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 28 Sep 2017 11:41:01 +1000 Subject: [PATCH] Add a TypeExpr.HasPointers method. --- cmd/puffs-c/internal/cgen/expr.go | 9 --------- cmd/puffs-c/internal/cgen/var.go | 18 ++---------------- lang/ast/ast.go | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 9bfd46a4..be36cbd8 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -384,15 +384,6 @@ func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, var return nil } -var builtInPointerTypes = [...]bool{ - t.KeyBuf1: true, - t.KeyBuf1Mark: true, - // TODO: uncomment, after figuring out how limit's should really work. - // t.KeyReader1: true, - t.KeyWriter1: true, - t.KeyBuf2: true, -} - var cTypeNames = [...]string{ t.KeyI8: "int8_t", t.KeyI16: "int16_t", diff --git a/cmd/puffs-c/internal/cgen/var.go b/cmd/puffs-c/internal/cgen/var.go index aa6cd19e..cf26e3ab 100644 --- a/cmd/puffs-c/internal/cgen/var.go +++ b/cmd/puffs-c/internal/cgen/var.go @@ -232,7 +232,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, n *a.Var, prefix string, suspend bo local := fmt.Sprintf("%s%s", prefix, n.Name().String(g.tm)) // TODO: explicitly handle prefix == lPrefix? - if typ := n.XType(); prefix == vPrefix && typeHasPointers(typ) { + if typ := n.XType(); prefix == vPrefix && typ.HasPointers() { if suspend { return nil } @@ -297,7 +297,7 @@ func (g *gen) writeVars(b *buffer, block []*a.Node, skipPointerTypes bool) error if v := n.Value(); v != nil && v.ID0().Key() == t.KeyLimit { b.printf("uint64_t %s%v;\n", lPrefix, n.Name().String(g.tm)) } - if skipPointerTypes && typeHasPointers(n.XType()) { + if skipPointerTypes && n.XType().HasPointers() { return nil } if err := g.writeCTypeName(b, n.XType(), vPrefix, n.Name().String(g.tm)); err != nil { @@ -307,17 +307,3 @@ func (g *gen) writeVars(b *buffer, block []*a.Node, skipPointerTypes bool) error return nil }) } - -func typeHasPointers(typ *a.TypeExpr) bool { - for ; typ != nil; typ = typ.Inner() { - switch typ.Decorator().Key() { - case 0: - if key := typ.Name().Key(); key < t.Key(len(builtInPointerTypes)) && builtInPointerTypes[key] { - return true - } - case t.KeyPtr, t.KeyColon: - return true - } - } - return false -} diff --git a/lang/ast/ast.go b/lang/ast/ast.go index cce7b534..fb718f34 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -513,6 +513,22 @@ func (n *TypeExpr) IsRefined() bool { return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil) } +func (n *TypeExpr) HasPointers() bool { + for ; n != nil; n = n.Inner() { + switch n.id0.Key() { + case 0: + // TODO: add t.KeyReader1, after figuring out how limit's should really work. + switch n.id1.Key() { + case t.KeyBuf1, t.KeyBuf1Mark, t.KeyWriter1, t.KeyBuf2: + return true + } + case t.KeyPtr, t.KeyNptr, t.KeyColon: + return true + } + } + return false +} + func (n *TypeExpr) Unrefined() *TypeExpr { if !n.IsRefined() { return n