Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Clyybber committed Jul 24, 2020
1 parent c9356f5 commit a5f46f3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ type
nkBreakState, # special break statement for easier code generation
nkFuncDef, # a func
nkTupleConstr # a tuple constructor
nkEarlySemArg # When an argument was symmed early, but we might need the untyped AST later

TNodeKinds* = set[TNodeKind]

Expand Down
6 changes: 6 additions & 0 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ proc tryDeref(n: PNode): PNode =

proc semOverloadedCall(c: PContext, n, nOrig: PNode,
filter: TSymKinds, flags: TExprFlags): PNode {.nosinks.} =
var earlySym: PNode
if n.len > 1 and n[1].kind == nkEarlySemArg:
earlySym = n[1]
n[1] = n[1][0]
var errors: CandidateErrors = @[] # if efExplain in flags: @[] else: nil
var r = resolveOverloads(c, n, nOrig, filter, flags, errors, efExplain in flags)
if r.state == csMatch:
Expand All @@ -562,6 +566,8 @@ proc semOverloadedCall(c: PContext, n, nOrig: PNode,
message(c.config, n.info, hintUserRaw,
"Non-matching candidates for " & renderTree(n) & "\n" &
candidates)
if earlySym != nil:
n[1] = earlySym
result = semResolvedCall(c, r, n, flags)
elif implicitDeref in c.features and canDeref(n):
# try to deref the first argument and then try overloading resolution again:
Expand Down
16 changes: 14 additions & 2 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ proc semExprCheck(c: PContext, n: PNode, flags: TExprFlags): PNode =
result = errorNode(c, n)

proc semExprWithType(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
let n = if n.kind == nkEarlySemArg:
n[0]
else:
n
result = semExprCheck(c, n, flags)
if result.typ == nil or result.typ == c.enforceVoidContext:
localError(c.config, n.info, errExprXHasNoType %
Expand Down Expand Up @@ -852,7 +856,13 @@ proc semOverloadedCallAnalyseEffects(c: PContext, n: PNode, nOrig: PNode,
return
let callee = result[0].sym
case callee.kind
of skMacro, skTemplate: discard
of skMacro, skTemplate:
discard
let typ = callee.typ.n
#The first argument might be early gensymmed because of the method call syntax
if typ.len > 1 and typ[1].typ != nil and typ[1].typ.kind == tyUntyped and n.len > 1 and n[1].kind == nkEarlySemArg:
n[1] = n[1][1] # Restore untyped AST
#TODO: Warn the user about this extra symming
else:
if callee.kind == skIterator and callee.id == c.p.owner.id:
localError(c.config, n.info, errRecursiveDependencyIteratorX % callee.name.s)
Expand Down Expand Up @@ -1443,7 +1453,9 @@ proc semFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
if result == nil:
result = dotTransformation(c, n)
if result.kind == nkDotCall:
result = dotTransformation(c, oldN) #TODO: Warn if we do this
#TODO: What about the IdentCache?
result = dotTransformation(c, oldN)
result[1] = newNode(nkEarlySemArg, result[1].info, @[result[1], oldN])

proc buildOverloadedSubscripts(n: PNode, ident: PIdent): PNode =
result = newNodeI(nkCall, n.info)
Expand Down
6 changes: 2 additions & 4 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,8 @@ template applyIt*(varSeq, op: untyped) =
nums.applyIt(it * 3)
assert nums[0] + nums[3] == 15

for i in low(varSeq) .. high(varSeq):
let it {.inject.} = varSeq[i]
varSeq[i] = op

for it {.inject.} in mitems(varSeq):
it = op

template newSeqWith*(len: int, init: untyped): untyped =
## Creates a new sequence of length `len`, calling `init` to initialize
Expand Down
21 changes: 16 additions & 5 deletions tests/macros/tmacros_issues.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ false
true
@[i0, i1, i2, i3, i4]
@[tmp, tmp, tmp, tmp, tmp]
hey
hey
compTime
compTime
now method:
hey
hey
compTime
compTime
compTime
now method command:
compTime
compTime
compTime
'''
output: '''
Expand All @@ -40,6 +45,12 @@ test
foo1
foo2
foo3
0
1
3
4
6
7
'''
"""

Expand Down Expand Up @@ -309,4 +320,4 @@ doubleEval(echoes)
static: echo "now method:"
(echoes).doubleEval()
static: echo "now method command:"
(echoes).doubleEval #TODO: This is still broken
(echoes).doubleEval

0 comments on commit a5f46f3

Please sign in to comment.