added 'reset' magic proc

This commit is contained in:
Araq
2011-03-25 01:49:42 +01:00
parent 032599c156
commit bd4f0b94ae
9 changed files with 73 additions and 12 deletions

View File

@@ -88,8 +88,7 @@ proc new*[T](a: var ref T, finalizer: proc (x: ref T)) {.
## the object! This means that for each object of type `T` the finalizer
## will be called!
proc reset*(obj: var T) {.magic: "Reset", noSideEffect.}
proc reset*(obj: ref T) {.noSideEffect.} = reset(obj^)
proc reset*[T](obj: var T) {.magic: "Reset", noSideEffect.}
## resets an object `obj` to its initial (binary zero) value. This needs to
## be called before any possible `object branch transition`:idx:.

View File

@@ -331,8 +331,9 @@ type
mPlusSet, mMinusSet, mSymDiffSet, mConStrStr, mConArrArr, mConArrT,
mConTArr, mConTT, mSlice, mAppendStrCh, mAppendStrStr, mAppendSeqElem,
mInRange, mInSet, mRepr, mExit, mSetLengthStr, mSetLengthSeq, mAssert,
mSwap, mIsNil, mArrToSeq, mCopyStr, mCopyStrLast, mNewString, mArray,
mOpenArray, mRange, mSet, mSeq, mOrdinal, mInt, mInt8, mInt16, mInt32,
mSwap, mIsNil, mArrToSeq, mCopyStr, mCopyStrLast, mNewString, mReset,
mArray, mOpenArray, mRange, mSet, mSeq,
mOrdinal, mInt, mInt8, mInt16, mInt32,
mInt64, mFloat, mFloat32, mFloat64, mBool, mChar, mString, mCstring,
mPointer, mEmptySet, mIntSetBaseType, mNil, mExpr, mStmt, mTypeDesc,
mIsMainModule, mCompileDate, mCompileTime, mNimrodVersion, mNimrodMajor,

View File

@@ -931,6 +931,12 @@ proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
dest.r = ropef("$1->data[$1->Sup.len-1]", [rdLoc(a)])
genAssignment(p, dest, b, {needToCopy, afDestIsNil})
proc genReset(p: BProc, n: PNode) =
var a: TLoc
InitLocExpr(p, n.sons[1], a)
appcg(p, cpsStmts, "#genericReset((void*)$1, $2);$n",
[addrLoc(a), genTypeInfo(p.module, skipTypes(a.t, abstractVarRange))])
proc genNew(p: BProc, e: PNode) =
var
a, b: TLoc
@@ -1449,6 +1455,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
mInSet:
genSetOp(p, e, d, op)
of mNewString, mCopyStr, mCopyStrLast, mExit: genCall(p, e, d)
of mReset: genReset(p, e)
of mEcho: genEcho(p, e)
of mArrToSeq: genArrToSeq(p, e, d)
of mNLen..mNError:

View File

@@ -98,6 +98,9 @@ proc isSpecial(n: PNode): bool {.inline.} =
# XXX this does not work yet! Better to compile too much than to compile to
# few programs
proc myreset(n: PNode) {.inline.} =
when defined(system.reset): reset(n^)
proc evalIf(c: PEvalContext, n: PNode): PNode =
var i = 0
var length = sonsLen(n)
@@ -361,7 +364,7 @@ proc evalAsgn(c: PEvalContext, n: PNode): PNode =
var x = result
result = evalAux(c, n.sons[1], {})
if isSpecial(result): return
when defined(system.reset): reset(x)
myreset(x)
x.kind = result.kind
x.typ = result.typ
case x.kind
@@ -479,17 +482,13 @@ proc evalNew(c: PEvalContext, n: PNode): PNode =
var a = result
var t = skipTypes(n.sons[1].typ, abstractVar)
if a.kind == nkEmpty: InternalError(n.info, "first parameter is empty")
# changing the node kind is ugly and suggests deep problems:
myreset(a)
a.kind = nkRefTy
a.info = n.info
a.typ = t
a.sons = nil
addSon(a, getNullValue(t.sons[0], n.info))
result = emptyNode
when false:
var t = skipTypes(n.sons[1].typ, abstractVar)
result = newNodeIT(nkRefTy, n.info, t)
addSon(result, getNullValue(t.sons[0], n.info))
proc evalDeref(c: PEvalContext, n: PNode, flags: TEvalFlags): PNode =
result = evalAux(c, n.sons[0], {efLValue})
@@ -646,7 +645,7 @@ proc evalNewSeq(c: PEvalContext, n: PNode): PNode =
var b = result
var t = skipTypes(n.sons[1].typ, abstractVar)
if a.kind == nkEmpty: InternalError(n.info, "first parameter is empty")
# changing the node kind is ugly and suggests deep problems:
myreset(a)
a.kind = nkBracket
a.info = n.info
a.typ = t

View File

@@ -392,7 +392,7 @@ proc analyseIfAddressTakenInCall(c: PContext, n: PNode) =
const
FakeVarParams = {mNew, mNewFinalize, mInc, ast.mDec, mIncl, mExcl,
mSetLengthStr, mSetLengthSeq, mAppendStrCh, mAppendStrStr, mSwap,
mAppendSeqElem, mNewSeq}
mAppendSeqElem, mNewSeq, mReset}
checkMinSonsLen(n, 1)
var t = n.sons[0].typ
if (n.sons[0].kind == nkSym) and (n.sons[0].sym.magic in FakeVarParams):

View File

@@ -0,0 +1,10 @@
##
## can_inherit_generic Nimrod Module
##
## Created by Eric Doughty-Papassideris on 2011-02-16.
## Copyright (c) 2011 FWA. All rights reserved.
type
TGen[T] = object
TSpef[T] = object of TGen[T]

View File

@@ -0,0 +1,11 @@
##
## can_specialise_generic Nimrod Module
##
## Created by Eric Doughty-Papassideris on 2011-02-16.
## Copyright (c) 2011 FWA. All rights reserved.
type
TGen[T] = object
TSpef = object of TGen[string]

View File

@@ -0,0 +1,15 @@
##
## specialised_is_equivalent Nimrod Module
##
## Created by Eric Doughty-Papassideris on 2011-02-16.
## Copyright (c) 2011 FWA. All rights reserved.
type
TGen[T] = tuple[a: T]
TSpef = tuple[a: string]
var
a: TGen[string]
b: TSpef
a = b

View File

@@ -0,0 +1,19 @@
discard """
output: "after"
"""
import
macros, strutils
macro test_macro*(n: stmt): stmt =
result = newNimNode(nnkStmtList)
var ass : PNimrodNode = newNimNode(nnkAsgn)
add(ass, newIdentNode("str"))
add(ass, newStrLitNode("after"))
add(result, ass)
when isMainModule:
var str: string = "before"
test_macro(str):
var i : integer = 123
echo str