diff --git a/changelog.md b/changelog.md index d5d1be95605a4..cb2aa1b6785ed 100644 --- a/changelog.md +++ b/changelog.md @@ -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 @@ -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. diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index cdb47d2a45ca9..30f14b524b917 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -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..