Skip to content

new syntax for lvalue references: var b {.byaddr.} = expr #13508

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

Merged
merged 8 commits into from
Mar 23, 2020
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
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ echo f
- `std/oswalkdir` was buggy, it's now deprecated and reuses `std/os` procs
- `net.newContext` now performs SSL Certificate checking on Linux and OSX.
Define `nimDisableCertificateValidation` to disable it globally.

- new syntax for lvalue references: `var b {.byaddr.} = expr` enabled by `import pragmas`

## Language additions

Expand All @@ -156,6 +156,9 @@ echo f
- `=sink` type bound operator is now optional. Compiler can now use combination
of `=destroy` and `copyMem` to move objects efficiently.

- `var a {.foo.}: MyType = expr` now lowers to `foo(a, MyType, expr)` for non builtin pragmas,
enabling things like lvalue references, see `pragmas.byaddr`

## Language changes

- Unsigned integer operators have been fixed to allow promotion of the first operand.
Expand Down
46 changes: 46 additions & 0 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,53 @@ proc setVarType(c: PContext; v: PSym, typ: PType) =
"; new type is: " & typeToString(typ, preferDesc))
v.typ = typ

proc semLowerLetVarCustomPragma(c: PContext, a: PNode, n: PNode): PNode =
var b = a[0]
if b.kind == nkPragmaExpr:
if b[1].len != 1:
# we could in future support pragmas w args eg: `var foo {.bar:"goo".} = expr`
return nil
let nodePragma = b[1][0]
# see: `singlePragma`
if nodePragma.kind notin {nkIdent, nkAccQuoted}:
return nil
let ident = considerQuotedIdent(c, nodePragma)
var userPragma = strTableGet(c.userPragmas, ident)
if userPragma != nil: return nil

let w = nodePragma.whichPragma
if n.kind == nkVarSection and w in varPragmas or
n.kind == nkLetSection and w in letPragmas or
n.kind == nkConstSection and w in constPragmas:
return nil

let sym = searchInScopes(c, ident)
if sfCustomPragma in sym.flags: return nil # skip `template myAttr() {.pragma.}`
let lhs = b[0]
let clash = strTableGet(c.currentScope.symbols, lhs.ident)
if clash != nil:
# refs https://github.com/nim-lang/Nim/issues/8275
wrongRedefinition(c, lhs.info, lhs.ident.s, clash.info)

result = newTree(nkCall)
doAssert nodePragma.kind in {nkIdent, nkAccQuoted}, $nodePragma.kind
result.add nodePragma
result.add lhs
if a[1].kind != nkEmpty:
result.add a[1]
else:
result.add newNodeIT(nkNilLit, a.info, c.graph.sysTypes[tyNil])
result.add a[2]
result.info = a.info
let ret = newNodeI(nkStmtList, a.info)
ret.add result
result = semExprNoType(c, ret)

proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
if n.len == 1:
result = semLowerLetVarCustomPragma(c, n[0], n)
if result!=nil: return result

var b: PNode
result = copyNode(n)
for i in 0..<n.len:
Expand Down
19 changes: 19 additions & 0 deletions lib/std/pragmas.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# see `semLowerLetVarCustomPragma` for compiler support that enables these
# lowerings

template byaddr*(lhs, typ, expr) =
## Allows a syntax for lvalue reference, exact analog to
## `auto& a = expr;` in C++
runnableExamples:
var s = @[10,11,12]
var a {.byaddr.} = s[0]
a+=100
doAssert s == @[110,11,12]
doAssert a is int
var b {.byaddr.}: int = s[0]
doAssert a.addr == b.addr
when typ is type(nil):
let tmp = addr(expr)
else:
let tmp: ptr typ = addr(expr)
template lhs: untyped = tmp[]
70 changes: 70 additions & 0 deletions tests/stdlib/tpragmas.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import std/pragmas

block:
var s = @[10,11,12]
var a {.byaddr.} = s[0]
a+=100
doAssert s == @[110,11,12]
doAssert a is int
var b {.byaddr.}: int = s[0]
doAssert a.addr == b.addr

doAssert not compiles(block:
# redeclaration not allowed
var foo = 0
var foo {.byaddr.} = s[0])

doAssert not compiles(block:
# ditto
var foo {.byaddr.} = s[0]
var foo {.byaddr.} = s[0])

block:
var b {.byaddr.} = s[1] # redeclaration ok in sub scope
b = 123

doAssert s == @[110,123,12]

b = b * 10
doAssert s == @[1100,123,12]

doAssert not compiles(block:
var b2 {.byaddr.}: float = s[2])

doAssert compiles(block:
var b2 {.byaddr.}: int = s[2])

## We can define custom pragmas in user code
template byUnsafeAddr(lhs, typ, expr) =
when typ is type(nil):
let tmp = unsafeAddr(expr)
else:
let tmp: ptr typ = unsafeAddr(expr)
template lhs: untyped = tmp[]

block:
let s = @["foo", "bar"]
let a {.byUnsafeAddr.} = s[0]
doAssert a == "foo"
doAssert a[0].unsafeAddr == s[0][0].unsafeAddr

block: # nkAccQuoted
# shows using a keyword, which requires nkAccQuoted
template `cast`(lhs, typ, expr) =
when typ is type(nil):
let tmp = unsafeAddr(expr)
else:
let tmp: ptr typ = unsafeAddr(expr)
template lhs: untyped = tmp[]

block:
let s = @["foo", "bar"]
let a {.`byUnsafeAddr`.} = s[0]
doAssert a == "foo"
doAssert a[0].unsafeAddr == s[0][0].unsafeAddr

block:
let s = @["foo", "bar"]
let a {.`cast`.} = s[0]
doAssert a == "foo"
doAssert a[0].unsafeAddr == s[0][0].unsafeAddr