summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-07-27 18:20:13 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-07-27 18:20:13 +0200
commitef4b755183f6564cc0f35cdf01794626c4e5fe2f (patch)
tree1ea46f6bf9dc4d38067abae043907dcfbadd2ebc
parent4ec91a30c4ab4bdb45f9805168f124205235d19c (diff)
downloadNim-ef4b755183f6564cc0f35cdf01794626c4e5fe2f.tar.gz
allows a destructor to be attached to a tyString/tySequence
-rw-r--r--compiler/ast.nim6
-rw-r--r--compiler/semstmts.nim10
-rw-r--r--lib/core/seqs.nim12
-rw-r--r--lib/core/strs.nim17
4 files changed, 28 insertions, 17 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 3870c82ee..a61ac055e 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -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()
 
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index 781813795..f7d8b6b7b 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -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
diff --git a/lib/core/seqs.nim b/lib/core/seqs.nim
index 4327d2352..4dcf6cbbb 100644
--- a/lib/core/seqs.nim
+++ b/lib/core/seqs.nim
@@ -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
diff --git a/lib/core/strs.nim b/lib/core/strs.nim
index fdf3d6e42..562beaa91 100644
--- a/lib/core/strs.nim
+++ b/lib/core/strs.nim
@@ -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