diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 142d2189..818e9dd4 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -45,8 +45,10 @@ func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32 } if cv := n.ConstValue(); cv != nil { - if !n.MType().IsBool() { + if typ := n.MType(); typ.IsNumTypeOrIdeal() { b.writes(cv.String()) + } else if typ.IsNullptr() { + b.writes("NULL") } else if cv.Cmp(zero) == 0 { b.writes("false") } else if cv.Cmp(one) == 0 { diff --git a/doc/changelog.md b/doc/changelog.md index 92c86a1b..66e123a3 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -6,6 +6,7 @@ - Renamed Puffs to Wuffs. - Ship as a "single file C library" - Added a `skipgendeps` flag. +- Added a `nullptr` literal. - Added an `io_bind` keyword. - Added a `use` keyword. - Added a `yield` keyword. diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 3e4e2078..f7dfb82a 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -701,6 +701,10 @@ func (n *TypeExpr) IsIOType() bool { return n.id0 == 0 && n.id1 == t.IDBase && (n.id2 == t.IDIOReader || n.id2 == t.IDIOWriter) } +func (n *TypeExpr) IsNullptr() bool { + return n.id0 == 0 && n.id1 == t.IDBase && n.id2 == t.IDQNullptr +} + func (n *TypeExpr) IsNumType() bool { return n.id0 == 0 && n.id1 == t.IDBase && n.id2.IsNumType() } @@ -717,6 +721,10 @@ func (n *TypeExpr) IsArrayType() bool { return n.id0 == t.IDArray } +func (n *TypeExpr) IsPointerType() bool { + return n.id0 == t.IDNptr || n.id0 == t.IDPtr +} + func (n *TypeExpr) IsSliceType() bool { return n.id0 == t.IDSlice } diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 92346952..31ba4e5d 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -38,6 +38,7 @@ var ( typeExprGeneric2 = a.NewTypeExpr(0, t.IDBase, t.IDDagger2, nil, nil, nil) typeExprIdeal = a.NewTypeExpr(0, t.IDBase, t.IDQIdeal, nil, nil, nil) typeExprList = a.NewTypeExpr(0, t.IDBase, t.IDDollar, nil, nil, nil) + typeExprNullptr = a.NewTypeExpr(0, t.IDBase, t.IDQNullptr, nil, nil, nil) typeExprPlaceholder = a.NewTypeExpr(0, t.IDBase, t.IDQPlaceholder, nil, nil, nil) typeExprTypeExpr = a.NewTypeExpr(0, t.IDBase, t.IDQTypeExpr, nil, nil, nil) diff --git a/lang/check/type.go b/lang/check/type.go index da5622db..a2a557f7 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -404,6 +404,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.SetConstValue(one) n.SetMType(typeExprBool) return nil + + case t.IDNullptr: + n.SetConstValue(zero) + n.SetMType(typeExprNullptr) + return nil } case t.IDOpenParen, t.IDTry: @@ -735,6 +740,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { } rTyp := rhs.MType() + pointerComparison := false switch op { case t.IDXBinaryAnd, t.IDXBinaryOr: if !lTyp.IsBool() { @@ -745,24 +751,37 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error { return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type", op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) } - case t.IDXBinaryNotEq, t.IDXBinaryEqEq: - if lTyp.Eq(typeExprStatus) && rTyp.Eq(typeExprStatus) { - break - } - fallthrough default: + bad := (*a.Expr)(nil) if !lTyp.IsNumTypeOrIdeal() { - return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", - op.AmbiguousForm().Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) + bad = lhs + } else if !rTyp.IsNumTypeOrIdeal() { + bad = rhs } - if !rTyp.IsNumTypeOrIdeal() { + if op == t.IDXBinaryNotEq || op == t.IDXBinaryEqEq { + if lTyp.Eq(typeExprStatus) && rTyp.Eq(typeExprStatus) { + break + } + lNullptr := lTyp.Eq(typeExprNullptr) + rNullptr := rTyp.Eq(typeExprNullptr) + if (lNullptr && rNullptr) || + (lNullptr && rTyp.IsPointerType()) || + (rNullptr && lTyp.IsPointerType()) { + pointerComparison = true + break + } + } + if bad != nil { return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type", - op.AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) + op.AmbiguousForm().Str(q.tm), bad.Str(q.tm), bad.MType().Str(q.tm)) } } switch op { default: + if pointerComparison { + break + } if !lTyp.EqIgnoringRefinements(rTyp) && !lTyp.IsIdeal() && !rTyp.IsIdeal() { return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types", op.AmbiguousForm().Str(q.tm), diff --git a/lang/token/list.go b/lang/token/list.go index 4494babb..2922437e 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -371,22 +371,24 @@ const ( const ( minBuiltInLiteral = 0xB0 - minBuiltInNumLiteral = 0xB2 + minBuiltInNumLiteral = 0xB3 maxBuiltInNumLiteral = 0xBF maxBuiltInLiteral = 0xBF - IDFalse = ID(0xB0) - IDTrue = ID(0xB1) - ID0 = ID(0xB2) - ID1 = ID(0xB3) - ID2 = ID(0xB4) - ID4 = ID(0xB5) - ID8 = ID(0xB6) - ID16 = ID(0xB7) - ID32 = ID(0xB8) - ID64 = ID(0xB9) - ID128 = ID(0xBA) - ID256 = ID(0xBB) + IDFalse = ID(0xB0) + IDTrue = ID(0xB1) + IDNullptr = ID(0xB2) + + ID0 = ID(0xB3) + ID1 = ID(0xB4) + ID2 = ID(0xB5) + ID4 = ID(0xB6) + ID8 = ID(0xB7) + ID16 = ID(0xB8) + ID32 = ID(0xB9) + ID64 = ID(0xBA) + ID128 = ID(0xBB) + ID256 = ID(0xBC) ) const ( @@ -421,6 +423,7 @@ const ( IDDagger1 = ID(0x11A) IDDagger2 = ID(0x11B) + IDQNullptr = ID(0x11C) IDQPlaceholder = ID(0x11D) IDQTypeExpr = ID(0x11E) @@ -648,18 +651,20 @@ var builtInsByID = [nBuiltInIDs]string{ IDSlice: "slice", IDTable: "table", - IDFalse: "false", - IDTrue: "true", - ID0: "0", - ID1: "1", - ID2: "2", - ID4: "4", - ID8: "8", - ID16: "16", - ID32: "32", - ID64: "64", - ID128: "128", - ID256: "256", + IDFalse: "false", + IDTrue: "true", + IDNullptr: "nullptr", + + ID0: "0", + ID1: "1", + ID2: "2", + ID4: "4", + ID8: "8", + ID16: "16", + ID32: "32", + ID64: "64", + ID128: "128", + ID256: "256", // -------- 0x100 block. @@ -692,6 +697,10 @@ var builtInsByID = [nBuiltInIDs]string{ IDDagger1: "†", // U+2020 DAGGER IDDagger2: "‡", // U+2021 DOUBLE DAGGER + // IDQNullptr is used by the type checker to build an artificial MType for + // the nullptr literal. + IDQNullptr: "«Nullptr»", + // IDQPlaceholder is used by the type checker to build an artificial MType // for AST nodes that aren't expression nodes or type expression nodes, // such as struct definition nodes and statement nodes. Its presence means diff --git a/release/c/unsupported-snapshot.h b/release/c/unsupported-snapshot.h index 49ba155f..0dec1f12 100644 --- a/release/c/unsupported-snapshot.h +++ b/release/c/unsupported-snapshot.h @@ -6573,9 +6573,11 @@ wuffs_gif__decoder__decode_config(wuffs_gif__decoder* self, (self->private_impl.f_frame_rect_x1 == self->private_impl.f_width) && (self->private_impl.f_frame_rect_y1 == self->private_impl.f_height) && self->private_impl.f_has_full_palette); - wuffs_base__image_config__initialize( - a_dst, 570984584, 0, self->private_impl.f_width, - self->private_impl.f_height, v_num_loops, v_ffio); + if (a_dst != NULL) { + wuffs_base__image_config__initialize( + a_dst, 570984584, 0, self->private_impl.f_width, + self->private_impl.f_height, v_num_loops, v_ffio); + } self->private_impl.f_call_sequence = 1; goto ok; diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 0089dfeb..10bf8e7c 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -126,12 +126,14 @@ pub func decoder.decode_config?(dst ptr base.image_config, src base.io_reader)() (this.frame_rect_y1 == this.height) and (this.has_full_palette) - // TODO: rename initialize to set? - // - // TODO: a Wuffs (not just C) name for the - // WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant. - in.dst.initialize!(pixfmt:0x22088888, pixsub:0, - width:this.width, height:this.height, num_loops:num_loops, first_frame_is_opaque:ffio) + if in.dst != nullptr { + // TODO: rename initialize to set? + // + // TODO: a Wuffs (not just C) name for the + // WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL_INDEXED magic pixfmt constant. + in.dst.initialize!(pixfmt:0x22088888, pixsub:0, + width:this.width, height:this.height, num_loops:num_loops, first_frame_is_opaque:ffio) + } this.call_sequence = 1 }