more arc features (#13098)

* config update
* ARC now supports 'repr' and 'new' with finalizers is supported
This commit is contained in:
Andreas Rumpf 2020-01-10 23:17:33 +01:00 • committed by GitHub
commit 767592a3c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 252 additions and 13 deletions

View file

@ -383,6 +383,35 @@ proc semUnown(c: PContext; n: PNode): PNode =
# little hack for injectdestructors.nim (see bug #11350):
#result[0].typ = nil
proc turnFinalizerIntoDestructor(c: PContext; orig: PSym): PSym =
# We need to do 2 things: Replace n.typ which is a 'ref T' by a 'var T' type.
# Replace nkDerefExpr by nkHiddenDeref
# nkDeref is for 'ref T': x[].field
# nkHiddenDeref is for 'var T': x<hidden deref [] here>.field
proc transform(n: PNode; old, fresh: PType; oldParam, newParam: PSym): PNode =
result = shallowCopy(n)
if sameTypeOrNil(n.typ, old):
result.typ = fresh
if n.kind == nkSym and n.sym == oldParam:
result.sym = newParam
for i in 0 ..< safeLen(n):
result[i] = transform(n[i], old, fresh, oldParam, newParam)
#if n.kind == nkDerefExpr and sameType(n[0].typ, old):
# result =
result = copySym(orig)
result.flags.incl sfFromGeneric
let origParamType = orig.typ[1]
let newParamType = makeVarType(result, origParamType.skipTypes(abstractPtrs))
let oldParam = orig.typ.n[1].sym
let newParam = newSym(skParam, oldParam.name, result, result.info)
newParam.typ = newParamType
# proc body:
result.ast = transform(orig.ast, origParamType, newParamType, oldParam, newParam)
# proc signature:
result.typ = newProcType(result.info, result)
result.typ.addParam newParam
proc magicsAfterOverloadResolution(c: PContext, n: PNode,
flags: TExprFlags): PNode =
## This is the preferred code point to implement magics.
@ -447,6 +476,8 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
# Make sure the finalizer procedure refers to a procedure
if n[^1].kind == nkSym and n[^1].sym.kind notin {skProc, skFunc}:
localError(c.config, n.info, "finalizer must be a direct reference to a proc")
elif optTinyRtti in c.config.globalOptions:
bindTypeHook(c, turnFinalizerIntoDestructor(c, n[^1].sym), n, attachedDestructor)
result = n
of mDestroy:
result = n