summary refs log tree commit diff stats
path: root/compiler/semstmts.nim
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-03-23 03:15:45 -0700
committerGitHub <noreply@github.com>2020-03-23 11:15:45 +0100
commit913bc95964458d3eb2fc4d8a108031e6b9398da5 (patch)
treef66dc374d3ff53ff444e4975cfd40ec14916291c /compiler/semstmts.nim
parentfa06203e90d9bb9211d0b6b9726fc9f2c5dc80ad (diff)
downloadNim-913bc95964458d3eb2fc4d8a108031e6b9398da5.tar.gz
new syntax for lvalue references: `var b {.byaddr.} = expr` (#13508)
* new syntax for lvalue references: `var b {.byaddr.} = expr`
* on type mismatch, `???(0, 0)` not shown anymore
* * compiler now lowers `var a: {.foo.}: MyType = expr` to foo(a, MyType, expr)
* new pragmas.byaddr defined in pure library code exploiting this lowering
* skip `template foo() {.pragma.}`
Diffstat (limited to 'compiler/semstmts.nim')
-rw-r--r--compiler/semstmts.nim46
1 files changed, 46 insertions, 0 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index cdb47d2a4..30f14b524 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..<n.len: