Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sem: replace addr calls with nkAddr #1465

Merged
merged 4 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions compiler/ast/astalgo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,8 @@ proc iiTablePut(t: var TIITable, key, val: int) =
iiTableRawInsert(t.data, key, val)
inc(t.counter)

proc isAddrNode*(n: PNode): bool =
case n.kind
of nkAddr, nkHiddenAddr: true
of nkCallKinds:
if n[0].kind == nkSym and n[0].sym.magic == mAddr: true
else: false
else: false
func isAddrNode*(n: PNode): bool =
n.kind in {nkAddr, nkHiddenAddr}

proc listSymbolNames*(symbols: openArray[PSym]): string =
for sym in symbols:
Expand Down
4 changes: 1 addition & 3 deletions compiler/sem/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2814,9 +2814,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
of mAddr:
markUsed(c, n.info, s)
checkSonsLen(n, 2, c.config)
result[0] = newSymNode(s, n[0].info)
result[1] = semAddrArg(c, n[1])
result.typ = makePtrType(c, result[1].typ)
result = semAddrCall(c, n)
of mTypeOf:
markUsed(c, n.info, s)
result = semTypeOf(c, n)
Expand Down
15 changes: 11 additions & 4 deletions compiler/sem/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ proc semAddrArg(c: PContext; n: PNode): PNode =
else:
result = newError(c.config, n, PAstDiag(kind: adSemExprHasNoAddress))

proc semAddrCall(c: PContext, n: PNode): PNode =
## Analyzes a well-formed call of the ``system.addr`` procedure (`n`),
## returning either an error or an ``nkAddr`` expression.
result = newTreeI(nkAddr, n.info, semAddrArg(c, n[1]))
if result[0].kind == nkError:
result = c.config.wrapError(result)
else:
result.typ = makePtrType(c, result[0].typ)

proc semTypeOf(c: PContext; n: PNode): PNode =
addInNimDebugUtils(c.config, "semTypeOf", n, result)
var m = BiggestInt 1 # typeOfIter
Expand Down Expand Up @@ -421,11 +430,9 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,

case n[0].sym.magic
of mAddr:
# XXX: wasn't this magic already processed in ``semMagic``?
# 'addr' was overloaded, hence ``semMagic`` not handling the magic already
checkSonsLen(n, 2, c.config)
result = n
result[1] = semAddrArg(c, n[1])
result.typ = makePtrType(c, result[1].typ)
result = semAddrCall(c, n)
of mTypeOf:
result = semTypeOf(c, n)
of mSizeOf:
Expand Down
2 changes: 0 additions & 2 deletions compiler/sem/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,6 @@ proc allowCStringConv(n: PNode): bool =
of nkStrLiterals: result = true
of nkSym: result = n.sym.kind in {skConst, skParam}
of nkAddr: result = isCharArrayPtr(n.typ, true)
of nkCallKinds:
result = isCharArrayPtr(n.typ, n[0].kind == nkSym and n[0].sym.magic == mAddr)
else: result = isCharArrayPtr(n.typ, false)

proc reportErrors(c: ConfigRef, n: PNode) =
Expand Down
3 changes: 0 additions & 3 deletions compiler/sem/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,6 @@
inc(j)
result.add(a)
if result.len == 2: result = result[1]
elif magic == mAddr:
result = newTreeIT(nkAddr, n.info, n.typ): n[1]
result = transformAddr(c, result)
elif magic == mTypeOf:
result = n
elif magic == mRunnableExamples:
Expand Down Expand Up @@ -1308,7 +1305,7 @@
result = transformSons(c, n)
of nkIdentDefs, nkConstDef:
result = shallowCopy(n)
result[0] = transformDefSym(c, n[0])

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build release binaries (macOS (M1))

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build release binaries (macOS)

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build binaries with most recent GCC version

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build release binaries (Linux)

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build release binaries (Windows)

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build docs and release artifacts (macOS (M1))

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build docs and release artifacts (macOS)

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build docs and release artifacts (Linux)

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]

Check warning on line 1308 in compiler/sem/transf.nim

View workflow job for this annotation

GitHub Actions / Build docs and release artifacts (Windows)

workaround for sem not sanitizing AST; transformDefSym is deprecated [Deprecated]
# Skip the second son since it only contains an unsemanticized copy of the
# variable type used by docgen
let last = n.len-1
Expand Down
11 changes: 11 additions & 0 deletions tests/effects/teffects11.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
discard """
description: '''
Taking the address of a location storing a procedural value does not
incur the procedure's effects.
'''
action: compile
"""

proc p() {.raises: [].} =
var a: proc() {.raises: ValueError.}
discard addr(a)
4 changes: 0 additions & 4 deletions tests/lang_callable/macros/tmacrostmt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ proc fn6() =

#------------------------------------
# bug #10807
proc fn_unsafeaddr(x: int): int =
cast[int](unsafeAddr(x))

static:
let fn1s = "proc fn1(x, y: int): int =\n result = 2 * (x + y)\n"
Expand All @@ -124,15 +122,13 @@ static:
let fn4s = "proc fn4(x: int): int =\n if x mod 2 == 0:\n return x + 2\n else:\n return 0\n"
let fn5s = "proc fn5(a, b: float): float =\n result = -a * a / (b * b)\n"
let fn6s = "proc fn6() =\n var a = @[1.0, 2.0]\n let z = a{0, 1}\n a{2} = 5.0\n"
let fnAddr = "proc fn_unsafeaddr(x: int): int =\n result = cast[int](unsafeAddr(x))\n"

doAssert fn1.repr_to_string == fn1s
doAssert fn2.repr_to_string == fn2s
doAssert fn3.repr_to_string == fn3s
doAssert fn4.repr_to_string == fn4s
doAssert fn5.repr_to_string == fn5s
doAssert fn6.repr_to_string == fn6s
doAssert fn_unsafeaddr.repr_to_string == fnAddr

#------------------------------------
# bug #8763
Expand Down
Loading