diff --git a/cmd/wuffs-c/internal/cgen/func.go b/cmd/wuffs-c/internal/cgen/func.go index d1503530..9b96eb0c 100644 --- a/cmd/wuffs-c/internal/cgen/func.go +++ b/cmd/wuffs-c/internal/cgen/func.go @@ -87,7 +87,7 @@ func (g *gen) writeFuncSignature(b *buffer, n *a.Func, cpp uint32) error { } // TODO: write n's return values. - if n.Effect().Optional() { + if n.Effect().Coroutine() { b.writes("wuffs_base__status ") } else if out := n.Out(); out == nil { b.writes("wuffs_base__empty_struct ") @@ -178,7 +178,7 @@ func (g *gen) gatherFuncImpl(_ *buffer, n *a.Func) error { astFunc: n, cName: g.funcCName(n), - returnsStatus: n.Effect().Optional() || + returnsStatus: n.Effect().Coroutine() || ((n.Out() != nil) && n.Out().IsStatus()), } @@ -258,7 +258,7 @@ func (g *gen) writeFuncImplPrologue(b *buffer) error { } } - if g.currFunk.astFunc.Effect().Optional() || + if g.currFunk.astFunc.Effect().Coroutine() || (g.currFunk.returnsStatus && (len(g.currFunk.derivedVars) > 0)) { // TODO: rename the "status" variable to "ret"? b.printf("wuffs_base__status status = NULL;\n") @@ -334,7 +334,7 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error { } func (g *gen) writeFuncImplEpilogue(b *buffer) error { - if g.currFunk.astFunc.Effect().Optional() || + if g.currFunk.astFunc.Effect().Coroutine() || (g.currFunk.returnsStatus && (len(g.currFunk.derivedVars) > 0)) { b.writes("goto exit;exit:") // The goto avoids the "unused label" warning. @@ -350,7 +350,7 @@ func (g *gen) writeFuncImplEpilogue(b *buffer) error { b.writes("\n") } - if g.currFunk.astFunc.Effect().Optional() || + if g.currFunk.astFunc.Effect().Coroutine() || (g.currFunk.returnsStatus && (len(g.currFunk.derivedVars) > 0)) { if g.currFunk.astFunc.Public() { @@ -424,7 +424,7 @@ func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error { } b.writes(") {") b.writes("self->private_impl.magic = WUFFS_BASE__DISABLED;\n") - if g.currFunk.astFunc.Effect().Optional() { + if g.currFunk.astFunc.Effect().Coroutine() { b.writes("return wuffs_base__error__bad_argument;\n\n") } else { // TODO: don't assume that the return type is empty. diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 55002ee2..afa72c89 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -103,7 +103,7 @@ func (g *gen) writeStatementAssign0(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr return false, err } - doWork, hack := (lhs == nil) || rhs.Effect().Optional(), false + doWork, hack := (lhs == nil) || rhs.Effect().Coroutine(), false if !doWork && (rhs.Operator() == t.IDOpenParen) && (len(g.currFunk.derivedVars) > 0) { // TODO: tighten this heuristic for filtering out all but user-defined // method receivers. @@ -146,7 +146,7 @@ func (g *gen) writeStatementAssign0(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr return false, err } b.writes("status = ") - } else if rhs.Effect().Optional() { + } else if rhs.Effect().Coroutine() { b.writes("status = ") } @@ -161,12 +161,8 @@ func (g *gen) writeStatementAssign0(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr } } - if op != t.IDEqQuestion && rhs.Effect().Optional() { - target := "exit" - if rhs.Effect().Coroutine() { - target = "suspend" - } - b.printf("if (status) { goto %s; }\n", target) + if op != t.IDEqQuestion && rhs.Effect().Coroutine() { + b.writes("if (status) { goto suspend; }\n") } } @@ -397,7 +393,7 @@ func (g *gen) writeStatementJump(b *buffer, n *a.Jump, depth uint32) error { func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error { retExpr := n.Value() - if g.currFunk.astFunc.Effect().Optional() || + if g.currFunk.astFunc.Effect().Coroutine() || (g.currFunk.returnsStatus && (len(g.currFunk.derivedVars) > 0)) { isOK := false diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 4618b673..21a976cc 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -110,8 +110,7 @@ type Effect uint8 const ( EffectImpure = Effect(0x01) - EffectOptional = Effect(0x02) // TODO: rename to Statusful or Throws? - EffectCoroutine = Effect(0x04) + EffectCoroutine = Effect(0x02) effectMask = Effect(0xFF) ) @@ -120,7 +119,6 @@ func (e Effect) AsFlags() Flags { return Flags(e) } func (e Effect) Pure() bool { return e == 0 } func (e Effect) Impure() bool { return e&EffectImpure != 0 } -func (e Effect) Optional() bool { return e&EffectOptional != 0 } func (e Effect) Coroutine() bool { return e&EffectCoroutine != 0 } func (e Effect) WeakerThan(o Effect) bool { return e < o } @@ -131,9 +129,7 @@ func (e Effect) String() string { return "" case EffectImpure: return "!" - case EffectImpure | EffectOptional: - return "!?" - case EffectImpure | EffectOptional | EffectCoroutine: + case EffectImpure | EffectCoroutine: return "!??" } return "‽INVALID_EFFECT‽" diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 54d89016..fd0beb9b 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -298,7 +298,7 @@ func (q *checker) bcheckStatement(n *a.Node) error { case a.KRet: n := n.AsRet() lTyp := q.astFunc.Out() - if q.astFunc.Effect().Optional() { + if q.astFunc.Effect().Coroutine() { lTyp = typeExprStatus } else if lTyp == nil { return fmt.Errorf("TODO: allow returning nothing") diff --git a/lang/check/check.go b/lang/check/check.go index 53a216cf..f82c7499 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -433,7 +433,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error { } setPlaceholderMBoundsMType(n.In().AsNode()) if out := n.Out(); out != nil { - if n.Effect().Optional() && n.Receiver()[0] != t.IDBase { + if n.Effect().Coroutine() && n.Receiver()[0] != t.IDBase { return &Error{ Err: fmt.Errorf("func %s has ? effect but non-empty return type", n.QQID().Str(c.tm)), Filename: n.Filename(), diff --git a/lang/check/type.go b/lang/check/type.go index 3b4dfc1f..f76ac6af 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -170,7 +170,7 @@ func (q *checker) tcheckStatement(n *a.Node) error { case a.KRet: n := n.AsRet() lTyp := q.astFunc.Out() - if q.astFunc.Effect().Optional() { + if q.astFunc.Effect().Coroutine() { lTyp = typeExprStatus } else if lTyp == nil { return fmt.Errorf("TODO: allow returning nothing") @@ -545,7 +545,7 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error { oTyp := f.Out() if oTyp == nil { - if n.Effect().Optional() { + if n.Effect().Coroutine() { n.SetMType(typeExprStatus) } else { n.SetMType(typeExprEmptyStruct) diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 27b62a00..a4cbe6e9 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -683,7 +683,7 @@ func (p *parser) parseAssignNode() (*a.Node, error) { } if op == t.IDEqQuestion { - if (rhs.Operator() != t.IDOpenParen) || (!rhs.Effect().Optional()) { + if (rhs.Operator() != t.IDOpenParen) || (!rhs.Effect().Coroutine()) { return nil, fmt.Errorf(`parse: expected ?-function call after "=?", got %q at %s:%d`, rhs.Str(p.tm), p.filename, p.line()) } @@ -1170,8 +1170,8 @@ func (p *parser) parseEffect() a.Effect { } p.src = p.src[1:] if p.peek1() != t.IDQuestion { - return a.EffectImpure | a.EffectOptional + panic("TODO: delete") } p.src = p.src[1:] - return a.EffectImpure | a.EffectOptional | a.EffectCoroutine + return a.EffectImpure | a.EffectCoroutine }