From 1ca52be38b461e7c8db29deb05dfe4f878487752 Mon Sep 17 00:00:00 2001 From: metagn <10591326+metagn@users.noreply.github.com> Date: Sat, 23 Dec 2023 18:43:31 +0300 Subject: [PATCH] add test, adapt compiler --- compiler/ast.nim | 18 +++++++++++------- compiler/cgen.nim | 2 +- compiler/semstmts.nim | 13 +++++++------ compiler/transf.nim | 22 +++++++++++++--------- tests/macros/tastrepr.nim | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 732763f0fe61b..dd4ec85289911 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -1289,13 +1289,17 @@ proc extractPragma*(s: PSym): PNode = result = nil assert result == nil or result.kind == nkPragma -proc skipPragmaExpr*(n: PNode): PNode = - ## if pragma expr, give the node the pragmas are applied to, - ## otherwise give node itself - if n.kind == nkPragmaExpr: - result = n[0] - else: - result = n +proc skipPostfix*(n: PNode): PNode {.inline.} = + ## if postfix, give the operand, otherwise give node itself + result = n + if result.kind == nkPostfix: result = result[1] + +proc skipPragmaExpr*(n: PNode): PNode {.inline.} = + ## if pragma expr, take the node the pragmas are applied to, + ## otherwise take node itself; then skip postfix + result = n + if result.kind == nkPragmaExpr: result = result[0] + result = skipPostfix(result) proc setInfoRecursive*(n: PNode, info: TLineInfo) = ## set line info recursively diff --git a/compiler/cgen.nim b/compiler/cgen.nim index b18ddb63cb213..e820ebf9d70c0 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -720,7 +720,7 @@ proc assignParam(p: BProc, s: PSym, retType: PType) = scopeMangledParam(p, s) proc fillProcLoc(m: BModule; n: PNode) = - let sym = n.sym + let sym = skipPostfix(n).sym if sym.loc.k == locNone: fillBackendName(m, sym) fillLoc(sym.loc, locProc, n, OnStack) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 6020486bb6caf..19ee7b7fcb856 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -2372,8 +2372,9 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, n[genericParamsPos] = proto.ast[genericParamsPos] n[paramsPos] = proto.ast[paramsPos] n[pragmasPos] = proto.ast[pragmasPos] - if n[namePos].kind != nkSym: internalError(c.config, n.info, "semProcAux") - n[namePos].sym = proto + let name = skipPostfix(n[namePos]) + if name.kind != nkSym: internalError(c.config, n.info, "semProcAux") + name.sym = proto if importantComments(c.config) and proto.ast.comment.len > 0: n.comment = proto.ast.comment proto.ast = n # needed for code generation @@ -2492,7 +2493,7 @@ proc semIterator(c: PContext, n: PNode): PNode = # nkIteratorDef aynmore, return. The iterator then might have been # sem'checked already. (Or not, if the macro skips it.) if result.kind != n.kind: return - var s = result[namePos].sym + let s = skipPostfix(result[namePos]).sym var t = s.typ if t.returnType == nil and s.typ.callConv != ccClosure: localError(c.config, n.info, "iterator needs a return type") @@ -2526,7 +2527,7 @@ proc semMethod(c: PContext, n: PNode): PNode = # nkIteratorDef aynmore, return. The iterator then might have been # sem'checked already. (Or not, if the macro skips it.) if result.kind != nkMethodDef: return - var s = result[namePos].sym + let s = skipPostfix(result[namePos]).sym # we need to fix the 'auto' return type for the dispatcher here (see tautonotgeneric # test case): let disp = getDispatcher(s) @@ -2547,7 +2548,7 @@ proc semConverterDef(c: PContext, n: PNode): PNode = # nkIteratorDef aynmore, return. The iterator then might have been # sem'checked already. (Or not, if the macro skips it.) if result.kind != nkConverterDef: return - var s = result[namePos].sym + let s = skipPostfix(result[namePos]).sym var t = s.typ if t.returnType == nil: localError(c.config, n.info, errXNeedsReturnType % "converter") if t.len != 2: localError(c.config, n.info, "a converter takes exactly one argument") @@ -2561,7 +2562,7 @@ proc semMacroDef(c: PContext, n: PNode): PNode = # nkIteratorDef aynmore, return. The iterator then might have been # sem'checked already. (Or not, if the macro skips it.) if result.kind != nkMacroDef: return - var s = result[namePos].sym + let s = skipPostfix(result[namePos]).sym var t = s.typ var allUntyped = true var nullary = true diff --git a/compiler/transf.nim b/compiler/transf.nim index 9a5702b6e3c0f..661b63da468a8 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -214,12 +214,13 @@ proc transformVarSection(c: PTransf, v: PNode): PNode = internalError(c.graph.config, it.info, "transformVarSection: not nkVarTuple") var defs = newTransNode(it.kind, it.info, it.len) for j in 0..