mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 16:47:28 +01:00
lang: add read-only type decorators
This commit is contained in:
1 parent
6e8349129c
commit
f3caf09d77
14 files changed
+132
-77
No files matched your search
+14
-9
@@ -784,7 +784,8 @@ const MaxTypeExprDepth = 63
|
||||
|
||||
// TypeExpr is a type expression, such as "base.u32", "base.u32[..= 8]", "foo",
|
||||
// "pkg.bar", "ptr T", "array[8] T", "slice T" or "table T":
|
||||
// - ID0: <0|IDArray|IDFunc|IDNptr|IDPtr|IDSlice|IDTable>
|
||||
// - ID0: <0|IDArray|IDFunc|IDNptr|IDPtr|IDRoarray|IDRoslice|IDRotable|
|
||||
// .... IDSlice|IDTable>
|
||||
// - ID1: <0|pkg>
|
||||
// - ID2: <0|type name>
|
||||
// - LHS: <nil|Expr>
|
||||
@@ -889,8 +890,16 @@ func (n *TypeExpr) IsStatus() bool {
|
||||
return n.id0 == 0 && n.id1 == t.IDBase && n.id2 == t.IDStatus
|
||||
}
|
||||
|
||||
func (n *TypeExpr) IsArrayType() bool {
|
||||
return n.id0 == t.IDArray
|
||||
func (n *TypeExpr) IsEitherArrayType() bool {
|
||||
return (n.id0 == t.IDArray) || (n.id0 == t.IDRoarray)
|
||||
}
|
||||
|
||||
func (n *TypeExpr) IsEitherSliceType() bool {
|
||||
return (n.id0 == t.IDSlice) || (n.id0 == t.IDRoslice)
|
||||
}
|
||||
|
||||
func (n *TypeExpr) IsEitherTableType() bool {
|
||||
return (n.id0 == t.IDTable) || (n.id0 == t.IDRotable)
|
||||
}
|
||||
|
||||
func (n *TypeExpr) IsFuncType() bool {
|
||||
@@ -901,12 +910,8 @@ func (n *TypeExpr) IsPointerType() bool {
|
||||
return n.id0 == t.IDNptr || n.id0 == t.IDPtr
|
||||
}
|
||||
|
||||
func (n *TypeExpr) IsSliceType() bool {
|
||||
return n.id0 == t.IDSlice
|
||||
}
|
||||
|
||||
func (n *TypeExpr) IsTableType() bool {
|
||||
return n.id0 == t.IDTable
|
||||
func (n *TypeExpr) IsReadOnly() bool {
|
||||
return (n.id0 == t.IDRoarray) || (n.id0 == t.IDRoslice) || (n.id0 == t.IDRotable)
|
||||
}
|
||||
|
||||
func (n *TypeExpr) IsUnsignedInteger() bool {
|
||||
|
||||
+24
-5
@@ -84,16 +84,23 @@ func (n *Expr) Mentions(o *Expr) bool {
|
||||
|
||||
// Eq returns whether n and o are equal.
|
||||
func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
||||
return n.eq(o, false)
|
||||
return n.eq(o, false, false)
|
||||
}
|
||||
|
||||
// EqIgnoringRefinements returns whether n and o are equal, ignoring the
|
||||
// "[i:j]" in "base.u32[i:j]".
|
||||
func (n *TypeExpr) EqIgnoringRefinements(o *TypeExpr) bool {
|
||||
return n.eq(o, true)
|
||||
return n.eq(o, true, false)
|
||||
}
|
||||
|
||||
func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool) bool {
|
||||
// EqIgnoringRefinementsLHSReadOnly returns whether n and o are equal, ignoring
|
||||
// the "[i:j]" in "base.u32[i:j]" and allowing n (the Left Hand Side of an
|
||||
// assignment) to be read-only when o (the Right Hand Side) is read-write.
|
||||
func (n *TypeExpr) EqIgnoringRefinementsLHSReadOnly(o *TypeExpr) bool {
|
||||
return n.eq(o, true, true)
|
||||
}
|
||||
|
||||
func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool, lhsReadOnly bool) bool {
|
||||
for {
|
||||
if n == o {
|
||||
return true
|
||||
@@ -101,10 +108,22 @@ func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool) bool {
|
||||
if n == nil || o == nil {
|
||||
return false
|
||||
}
|
||||
if n.id0 != o.id0 || n.id1 != o.id1 || n.id2 != o.id2 {
|
||||
if n.id0 != o.id0 {
|
||||
if !lhsReadOnly {
|
||||
return false
|
||||
}
|
||||
switch {
|
||||
default:
|
||||
return false
|
||||
case (n.id0 == t.IDRoarray) && (o.id0 == t.IDArray):
|
||||
case (n.id0 == t.IDRoslice) && (o.id0 == t.IDSlice):
|
||||
case (n.id0 == t.IDRotable) && (o.id0 == t.IDTable):
|
||||
}
|
||||
}
|
||||
if n.id1 != o.id1 || n.id2 != o.id2 {
|
||||
return false
|
||||
}
|
||||
if n.IsArrayType() || !ignoreRefinements {
|
||||
if !ignoreRefinements || !n.IsNumType() {
|
||||
if !n.lhs.AsExpr().Eq(o.lhs.AsExpr()) || !n.mhs.AsExpr().Eq(o.mhs.AsExpr()) {
|
||||
return false
|
||||
}
|
||||
|
||||
+13
-4
@@ -208,7 +208,7 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte {
|
||||
return append(buf, "!invalid_type!"...)
|
||||
}
|
||||
|
||||
switch n.Decorator() {
|
||||
switch dec := n.Decorator(); dec {
|
||||
case 0:
|
||||
buf = append(buf, n.QID().Str(tm)...)
|
||||
case t.IDNptr:
|
||||
@@ -217,15 +217,24 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte {
|
||||
case t.IDPtr:
|
||||
buf = append(buf, "ptr "...)
|
||||
return n.Inner().appendStr(buf, tm, depth)
|
||||
case t.IDArray:
|
||||
case t.IDArray, t.IDRoarray:
|
||||
if dec == t.IDRoarray {
|
||||
buf = append(buf, "ro"...)
|
||||
}
|
||||
buf = append(buf, "array["...)
|
||||
buf = n.ArrayLength().appendStr(buf, tm, false, 0)
|
||||
buf = append(buf, "] "...)
|
||||
return n.Inner().appendStr(buf, tm, depth)
|
||||
case t.IDSlice:
|
||||
case t.IDRoslice, t.IDSlice:
|
||||
if dec == t.IDRoslice {
|
||||
buf = append(buf, "ro"...)
|
||||
}
|
||||
buf = append(buf, "slice "...)
|
||||
return n.Inner().appendStr(buf, tm, depth)
|
||||
case t.IDTable:
|
||||
case t.IDRotable, t.IDTable:
|
||||
if dec == t.IDRotable {
|
||||
buf = append(buf, "ro"...)
|
||||
}
|
||||
buf = append(buf, "table "...)
|
||||
return n.Inner().appendStr(buf, tm, depth)
|
||||
case t.IDFunc:
|
||||
|
||||
Reference in new issue
Block a user