ast: add TypeExpr.IsSmallInteger method

This commit is contained in:
Nigel Tao committed 2023-12-14 10:22:38 +11:00
1 parent c4d2fa13da
commit 0164364106
3 files changed
+7 -12

No files matched your search

+1 -7
View File
@@ -286,13 +286,7 @@ func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, depth uint32) error {
} }
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, depth uint32) error { func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, depth uint32) error {
opName, lhsCast, overallCast := "", false, false opName, lhsCast, overallCast := "", false, n.MType().IsSmallInteger()
if typ := n.MType(); typ.IsNumType() {
if u := typ.QID()[1]; u == t.IDU8 || u == t.IDU16 {
overallCast = true
}
}
op := n.Operator() op := n.Operator()
switch op { switch op {
+1 -5
View File
@@ -171,22 +171,18 @@ func (g *gen) writeStatementAssign1(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
} }
if op.IsAssign() { if op.IsAssign() && lTyp.IsSmallInteger() {
switch op { switch op {
case t.IDAmpEq, t.IDPipeEq, t.IDHatEq, t.IDEq, t.IDEqQuestion: case t.IDAmpEq, t.IDPipeEq, t.IDHatEq, t.IDEq, t.IDEqQuestion:
// No-op. // No-op.
default: default:
if lTyp.IsNumType() {
if u := lTyp.QID()[1]; u == t.IDU8 || u == t.IDU16 {
disableWconversion = true disableWconversion = true
} }
} }
} }
} }
} }
}
}
// "x += 1" triggers -Wconversion, if x is smaller than an int (i.e. a // "x += 1" triggers -Wconversion, if x is smaller than an int (i.e. a
// uint8_t or a uint16_t). This is arguably a clang/gcc bug, but in any // uint8_t or a uint16_t). This is arguably a clang/gcc bug, but in any
// case, we work around it in Wuffs. // case, we work around it in Wuffs.
+5
View File
@@ -1007,6 +1007,11 @@ func (n *TypeExpr) IsContainerOfSpecificNumType(container t.ID, numType t.ID) bo
return (n.id0 == container) && n.Inner().IsNumType() && (n.Inner().id2 == numType) return (n.id0 == container) && n.Inner().IsNumType() && (n.Inner().id2 == numType)
} }
func (n *TypeExpr) IsSmallInteger() bool {
return n.id0 == 0 && n.id1 == t.IDBase &&
(n.id2 == t.IDU8 || n.id2 == t.IDU16)
}
func (n *TypeExpr) IsUnsignedInteger() bool { func (n *TypeExpr) IsUnsignedInteger() bool {
return n.id0 == 0 && n.id1 == t.IDBase && return n.id0 == 0 && n.id1 == t.IDBase &&
(n.id2 == t.IDU8 || n.id2 == t.IDU16 || n.id2 == t.IDU32 || n.id2 == t.IDU64) (n.id2 == t.IDU8 || n.id2 == t.IDU16 || n.id2 == t.IDU32 || n.id2 == t.IDU64)