Skip to content

Commit

Permalink
add test, adapt compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Dec 23, 2023
1 parent a15c2f0 commit 1ca52be
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
18 changes: 11 additions & 7 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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
Expand Down
22 changes: 13 additions & 9 deletions compiler/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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..<it.len-2:
if it[j].kind == nkSym:
let x = freshVar(c, it[j].sym)
idNodeTablePut(c.transCon.mapping, it[j].sym, x)
let name = skipPostfix(it[j])
if name.kind == nkSym:
let x = freshVar(c, name.sym)
idNodeTablePut(c.transCon.mapping, name.sym, x)
defs[j] = x
else:
defs[j] = transform(c, it[j])
defs[j] = transform(c, name)
assert(it[^2].kind == nkEmpty)
defs[^2] = newNodeI(nkEmpty, it.info)
defs[^1] = transform(c, it[^1])
Expand Down Expand Up @@ -1011,13 +1012,16 @@ proc transform(c: PTransf, n: PNode): PNode =
result = n
of nkBracketExpr: result = transformArrayAccess(c, n)
of procDefs:
var s = n[namePos].sym
let name = skipPostfix(n[0])
let s = name.sym
if n.typ != nil and s.typ.callConv == ccClosure:
result = transformSym(c, n[namePos])
let t = transformSym(c, name)
# use the same node as before if still a symbol:
if result.kind == nkSym: result = n
else:
result = n
if t.kind != nkSym:
return t
result = newTransNode(n)
result[0] = name
for i in 1 ..< n.len: result[i] = n[i]
of nkMacroDef:
# XXX no proper closure support yet:
when false:
Expand Down
33 changes: 33 additions & 0 deletions tests/macros/tastrepr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ var
b = 2
type
A* = object
var
c* = 1
d* {.used.} = 1
e* = 2
let f* = 1
const
g* = 1
proc h*() =
discard
template i*() =
discard
var data = @[(1, "one"), (2, "two")]
for (i, d) in pairs(data):
Expand All @@ -24,6 +37,18 @@ for i, (x, y) in pairs(data):
var (a, b) = (1, 2)
type
A* = object
var
c* = 1
(d* {.used.}, e*) = (1, 2)
let f* = 1
const
g* = 1
proc h*() =
discard
template i*() =
discard
'''
"""

Expand All @@ -49,3 +74,11 @@ echoTypedAndUntypedRepr:
discard
var (a,b) = (1,2)
type A* = object # issue #22933
var
c* = 1
(d* {.used.}, e*) = (1, 2)
let f* = 1
const g* = 1
proc h*() = discard
template i*() = discard

0 comments on commit 1ca52be

Please sign in to comment.