From 5606702e6d9aa583141c975f45534d0f55d9acc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20M=20G=C3=B3mez?= Date: Sun, 21 May 2023 03:44:43 +0100 Subject: [PATCH] implements: "Allow bycopy to work in params #21874" (#21877) * implements: "Allow bycopy to work in params #21874" * Update compiler/pragmas.nim --------- Co-authored-by: Andreas Rumpf --- compiler/ast.nim | 3 ++- compiler/ccgutils.nim | 4 +++- compiler/pragmas.nim | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 7e92cd140b..da9b3898e9 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -231,7 +231,7 @@ type TNodeKinds* = set[TNodeKind] type - TSymFlag* = enum # 50 flags! + TSymFlag* = enum # 51 flags! sfUsed, # read access of sym (for warnings) or simply used sfExported, # symbol is exported from module sfFromGeneric, # symbol is instantiation of a generic; this is needed @@ -313,6 +313,7 @@ type # This is disallowed but can cause the typechecking to go into # an infinite loop, this flag is used as a sentinel to stop it. sfVirtual # proc is a C++ virtual function + sfByCopy # param is marked as pass bycopy TSymFlags* = set[TSymFlag] diff --git a/compiler/ccgutils.nim b/compiler/ccgutils.nim index d9aa58c675..ea4f3fe18b 100644 --- a/compiler/ccgutils.nim +++ b/compiler/ccgutils.nim @@ -122,8 +122,10 @@ proc ccgIntroducedPtr*(conf: ConfigRef; s: PSym, retType: PType): bool = var pt = skipTypes(s.typ, typedescInst) assert skResult != s.kind + #note precedence: params override types if optByRef in s.options: return true - if tfByRef in pt.flags: return true + elif sfByCopy in s.flags: return false + elif tfByRef in pt.flags: return true elif tfByCopy in pt.flags: return false case pt.kind of tyObject: diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index bc375b1bc9..11305db2a3 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -84,7 +84,7 @@ const wGensym, wInject, wIntDefine, wStrDefine, wBoolDefine, wDefine, wCompilerProc, wCore} - paramPragmas* = {wNoalias, wInject, wGensym, wByRef} + paramPragmas* = {wNoalias, wInject, wGensym, wByRef, wByCopy} letPragmas* = varPragmas procTypePragmas* = {FirstCallConv..LastCallConv, wVarargs, wNoSideEffect, wThread, wRaises, wEffectsOf, wLocks, wTags, wForbids, wGcSafe, @@ -1204,7 +1204,9 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int, incl(sym.typ.flags, tfByRef) of wByCopy: noVal(c, it) - if sym.kind != skType or sym.typ == nil: invalidPragma(c, it) + if sym.kind == skParam: + incl(sym.flags, sfByCopy) + elif sym.kind != skType or sym.typ == nil: invalidPragma(c, it) else: incl(sym.typ.flags, tfByCopy) of wPartial: noVal(c, it)