Skip to content

Commit

Permalink
all works
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Mar 22, 2020
1 parent 2c27bc3 commit 2e69fdc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 57 deletions.
1 change: 0 additions & 1 deletion compiler/lookups.nim
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ proc qualifiedLookUp*(c: PContext, n: PNode, flags: set[TLookupFlag]): PSym =
result = searchInScopes(c, ident, allExceptModule).skipAlias(n, c.config)
if result == nil and checkPureEnumFields in flags:
result = strTableGet(c.pureEnumFields, ident)
# if result == nil and checkUndeclared in flags:
if result == nil and checkUndeclared in flags and checkOverloadResolve notin flags:
fixSpelling(n, ident, searchInScopes)
errorUndeclaredIdentifier(c, n.info, ident.s)
Expand Down
3 changes: 1 addition & 2 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ proc resolveOverloads(c: PContext, n, orig: PNode,
pickBest(callOp)

if overloadsState == csEmpty and result.state == csEmpty:
if {efNoUndeclared, efOverloadResolve} * flags == {}:
# for tests/pragmas/tcustom_pragma.nim
if {efNoUndeclared, efOverloadResolve} * flags == {}: # for tests/pragmas/tcustom_pragma.nim
localError(c.config, n.info, getMsgDiagnostic(c, flags, n, f))
return
elif result.state != csMatch:
Expand Down
3 changes: 1 addition & 2 deletions compiler/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ type
# Use this if undeclared identifiers should not raise an error during
# overload resolution.
efOverloadResolve,
# for `mOverloadResolve` evaluation
# return resolved `foo` in `foo(args)`
# for `mOverloadResolve` evaluation, resolves `foo` in `foo(args)`

TExprFlags* = set[TExprFlag]

Expand Down
55 changes: 7 additions & 48 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# this module does the semantic checking for expressions
# included from sem.nim

import std/wrapnils

const
errExprXHasNoType = "expression '$1' has no type (or is ambiguous)"
errXExpectsTypeOrValue = "'$1' expects a type or value"
Expand Down Expand Up @@ -62,11 +60,8 @@ proc semOperand(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
result.typ = errorType(c)

proc semExprWithType(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
echo0b (flags, c.config$n.info)
rejectEmptyNode(n)
echo0b ()
result = semExpr(c, n, flags+{efWantValue})
echo0b ()
if result == nil: return errorNode(c, n)
if result.kind == nkEmpty:
# do not produce another redundant error message:
Expand Down Expand Up @@ -913,20 +908,8 @@ proc semIndirectOp(c: PContext, n: PNode, flags: TExprFlags): PNode =
else:
n[0] = n0
else:
echo0b (flags, n[0].renderTree, n.renderTree, n.kind, n[0].kind)
var flags2 = {efInCall}
if efOverloadResolve in flags and efOperand notin flags: flags2.incl efOverloadResolve
n[0] = semExpr(c, n[0], flags2)
# n[0] = semExpr(c, n[0], {efInCall} + flags * {efOverloadResolve})
# n[0] = semExpr(c, n[0], {efInCall})
if efOverloadResolve in flags:
echo0b (flags, n[0] == nil, )
if n[0] != nil:
echo0b n[0].kind
if n[0] == nil and efOverloadResolve in flags:
echo0b "errorNode"
return errorNode(c, n)
# return nil
n[0] = semExpr(c, n[0], {efInCall} + flags * {efOverloadResolve})
if n[0] == nil and efOverloadResolve in flags: return errorNode(c, n)
let t = n[0].typ
if t != nil and t.kind in {tyVar, tyLent}:
n[0] = newDeref(n[0])
Expand Down Expand Up @@ -1292,9 +1275,7 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
if exactEquals(c.config.m.trackPos, n[1].info): suggestExprNoCheck(c, n)

var flags2 = {checkAmbiguity, checkUndeclared, checkModule}
if efOverloadResolve in flags:
flags2.incl checkOverloadResolve
# flags2.excl checkUndeclared # PRTEMP
if efOverloadResolve in flags: flags2.incl checkOverloadResolve
var s = qualifiedLookUp(c, n, flags2)
if efOverloadResolve in flags and n.kind == nkDotExpr:
var m = qualifiedLookUp(c, n[0], (flags2*{checkUndeclared})+{checkModule})
Expand Down Expand Up @@ -2134,37 +2115,20 @@ proc semCompiles(c: PContext, n: PNode, flags: TExprFlags): PNode =

proc semOverloadResolve(c: PContext, n: PNode, flags: TExprFlags, isTopLevel: bool): PNode =
var n = n
echo0b (c.config$n.info, n.kind, flags, isTopLevel)
if isTopLevel:
if n.len != 2:
localError(c.config, n.info, "semOverloadResolve: got" & $n.len)
return
n = n[1]
let nKind = n.kind
if n.kind notin {nkIdent,nkDotExpr,nkAccQuoted} + nkCallKinds - {nkHiddenCallConv}:
localError(c.config, n.info, "expected routine, got " & $n.kind)
return

# PRTEMP: remove efOperand stuff
# let flags = flags + {efWantIterator, efOverloadResolve}
return errorNode(c, n)
if n.kind == nkDotExpr:
# let flags = flags + {efWantIterator, efOverloadResolve} - {efOperand}
# result = semExpr(c, n[0], flags)
# result = semExpr(c, n[0], flags)
# so that this doesn't compile: `overloadExists(nonexistant().foo)`
n[0] = semExpr(c, n[0], flags)
let flags = flags + {efWantIterator, efOverloadResolve} - {efOperand}
let flags = flags + {efWantIterator, efOverloadResolve}
result = semExpr(c, n, flags)

echo0b (result == nil, ?.result.kind, nKind, flags)
# if result != nil and result.kind == nkEmpty and nKind == nkDotExpr:
when false:
if (result == nil or result.kind == nkEmpty) and nKind == nkDotExpr:
# localError()
# localError(c.config, n.info, "invalid expression for OverloadResolve: " & $n.renderTree)
localError(c.config, n.info, "invalid expression for OverloadResolve")
return result # PRTEMP
if result == nil or result.kind == nkEmpty:
# doAssert isTopLevel # PRTEMP
if isTopLevel:
result = newNodeIT(nkNilLit, n.info, getSysType(c.graph, n.info, tyNil))
elif result.kind == nkClosedSymChoice:
Expand Down Expand Up @@ -2642,12 +2606,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
{checkUndeclared, checkModule, checkPureEnumFields}
else:
{checkUndeclared, checkModule, checkAmbiguity, checkPureEnumFields}
# if efOverloadResolve in flags and efInCall notin flags:
if efOverloadResolve in flags and efInCall in flags:
# PRTEMP efOverloadResolve checkOverloadResolve
# checks.excl checkUndeclared
checks.incl checkOverloadResolve
echo0b (checks, flags)
if efOverloadResolve in flags: checks.incl checkOverloadResolve
var s = qualifiedLookUp(c, n, checks)
if efOverloadResolve in flags and s == nil: return nil
if c.matchedConcept == nil: semCaptureSym(s, c.p.owner)
Expand Down
2 changes: 1 addition & 1 deletion tests/magics/mresolves.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/macros

macro overloadExistsImpl(x: typed): bool =
macro overloadExistsImpl(x: typed): bool =
newLit(x != nil)

template overloadExists*(a: untyped): bool =
Expand Down
17 changes: 14 additions & 3 deletions tests/magics/tresolve_overloads.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,23 @@ proc main()=
doAssert not compiles(nonexistant.bar1)
doAssert not compiles overloadExists(nonexistant.bar1)
doAssert not compiles overloadExists(nonexistant().mfoo1)

doAssert not compiles overloadExists(nonexistant1().nonexistant2)
doAssert not compiles overloadExists(nonexistant().bar2)
doAssert not compiles overloadExists(nonexistant().bar1)

# doAssert not overloadExists(nonexistant) # PRTEMP
doAssert declared(mresolve_overloads)
doAssert declared(fun7)
doAssert declared(foo)
doAssert declared(`bar5`)
doAssert declared(bar5)
doAssert declared(`system`)
doAssert not declared(nonexistant)
doAssert not overloadExists(nonexistant)
doAssert not overloadExists(`nonexistant`)
doAssert overloadExists(system)
doAssert overloadExists(`system`)
doAssert overloadExists(`bar5`)
doAssert overloadExists(bar5)

block: # resolveSymbol
doAssert resolveSymbol(fun8(1))(3) == fun8(3)
Expand Down Expand Up @@ -196,7 +207,7 @@ proc main2()=
doAssert compiles resolveSymbol(system.compiles)
inspect resolveSymbol(system.compiles)
doAssert resolveSymbol(system.nonexistant) == nil
# doAssert resolveSymbol(nonexistant) == nil
doAssert resolveSymbol(nonexistant) == nil

block:
template bar1(): untyped = 12
Expand Down

0 comments on commit 2e69fdc

Please sign in to comment.