allows a destructor to be attached to a tyString/tySequence

This commit is contained in:
Andreas Rumpf
2018-07-27 18:20:13 +02:00
parent 4ec91a30c4
commit ef4b755183
4 changed files with 28 additions and 17 deletions

View File

@@ -1088,9 +1088,9 @@ proc newSym*(symKind: TSymKind, name: PIdent, owner: PSym,
result.id = getID()
when debugIds:
registerId(result)
#if result.id == 93289:
#if result.id == 77131:
# writeStacktrace()
# MessageOut(name.s & " has id: " & toString(result.id))
# echo name.s
proc isMetaType*(t: PType): bool =
return t.kind in tyMetaTypes or
@@ -1272,7 +1272,7 @@ proc newType*(kind: TTypeKind, owner: PSym): PType =
when debugIds:
registerId(result)
when false:
if result.id == 205734:
if result.id == 76426:
echo "KNID ", kind
writeStackTrace()

View File

@@ -1017,8 +1017,8 @@ proc checkForMetaFields(c: PContext; n: PNode) =
case t.kind
of tySequence, tySet, tyArray, tyOpenArray, tyVar, tyLent, tyPtr, tyRef,
tyProc, tyGenericInvocation, tyGenericInst, tyAlias, tySink:
let start = int ord(t.kind in {tyGenericInvocation, tyGenericInst})
for i in start ..< t.sons.len:
let start = ord(t.kind in {tyGenericInvocation, tyGenericInst})
for i in start ..< t.len:
checkMeta(t.sons[i])
else:
checkMeta(t)
@@ -1337,7 +1337,7 @@ proc semOverride(c: PContext, s: PSym, n: PNode) =
if obj.kind in {tyGenericBody, tyGenericInst}: obj = obj.lastSon
elif obj.kind == tyGenericInvocation: obj = obj.sons[0]
else: break
if obj.kind in {tyObject, tyDistinct}:
if obj.kind in {tyObject, tyDistinct, tySequence, tyString}:
if obj.destructor.isNil:
obj.destructor = s
else:
@@ -1359,7 +1359,7 @@ proc semOverride(c: PContext, s: PSym, n: PNode) =
if t.kind == tyGenericBody: t = t.lastSon
elif t.kind == tyGenericInvocation: t = t.sons[0]
else: break
if t.kind in {tyObject, tyDistinct, tyEnum}:
if t.kind in {tyObject, tyDistinct, tyEnum, tySequence, tyString}:
if t.deepCopy.isNil: t.deepCopy = s
else:
localError(c.config, n.info, errGenerated,
@@ -1388,7 +1388,7 @@ proc semOverride(c: PContext, s: PSym, n: PNode) =
elif objB.kind in {tyGenericInvocation, tyGenericInst}:
objB = objB.sons[0]
else: break
if obj.kind in {tyObject, tyDistinct} and sameType(obj, objB):
if obj.kind in {tyObject, tyDistinct, tySequence, tyString} and sameType(obj, objB):
let opr = if s.name.s == "=": addr(obj.assignment) else: addr(obj.sink)
if opr[].isNil:
opr[] = s

View File

@@ -34,7 +34,8 @@ when false:
proc `=trace`[T](s: NimSeqV2[T]) =
for i in 0 ..< s.len: `=trace`(s.data[i])
proc `=destroy`[T](x: var NimSeqV2[T]) =
proc `=destroy`[T](s: var seq[T]) =
var x = cast[ptr NimSeqV2[T]](addr s)
var p = x.p
if p != nil:
when not supportsCopyMem(T):
@@ -43,7 +44,10 @@ proc `=destroy`[T](x: var NimSeqV2[T]) =
x.p = nil
x.len = 0
proc `=`[T](a: var NimSeqV2[T]; b: NimSeqV2[T]) =
proc `=`[T](x: var seq[T]; y: seq[T]) =
var a = cast[ptr NimSeqV2[T]](addr x)
var b = cast[ptr NimSeqV2[T]](unsafeAddr y)
if a.p == b.p: return
`=destroy`(a)
a.len = b.len
@@ -56,7 +60,9 @@ proc `=`[T](a: var NimSeqV2[T]; b: NimSeqV2[T]) =
for i in 0..<a.len:
a.p.data[i] = b.p.data[i]
proc `=sink`[T](a: var NimSeqV2[T]; b: NimSeqV2[T]) =
proc `=sink`[T](x: var seq[T]; y: seq[T]) =
var a = cast[ptr NimSeqV2[T]](addr x)
var b = cast[ptr NimSeqV2[T]](unsafeAddr y)
if a.p != nil and a.p != b.p:
`=destroy`(a)
a.len = b.len

View File

@@ -45,22 +45,27 @@ template frees(s) =
if not isLiteral(s):
s.p.region.dealloc(s.p.region, s.p, contentSize(s.p.cap))
proc `=destroy`(s: var NimStringV2) =
frees(s)
s.len = 0
s.p = nil
proc `=destroy`(s: var string) =
var a = cast[ptr NimStringV2[T]](addr s)
frees(a)
a.len = 0
a.p = nil
template lose(a) =
frees(a)
proc `=sink`(a: var NimStringV2, b: NimStringV2) =
proc `=sink`(x: var string, y: string) =
var a = cast[ptr NimStringV2](addr x)
var b = cast[ptr NimStringV2](unsafeAddr y)
# we hope this is optimized away for not yet alive objects:
if unlikely(a.p == b.p): return
lose(a)
a.len = b.len
a.p = b.p
proc `=`(a: var NimStringV2; b: NimStringV2) =
proc `=`(x: var string, y: string) =
var a = cast[ptr NimStringV2](addr x)
var b = cast[ptr NimStringV2](unsafeAddr y)
if unlikely(a.p == b.p): return
lose(a)
a.len = b.len