summary refs log tree commit diff stats
path: root/compiler/procfind.nim
blob: f385c2d94ed76d6539cb1998ae9a5fffd9fcfd34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; fo
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements the searching for procs and iterators.
# This is needed for proper handling of forward declarations.

import 
  ast, astalgo, msgs, semdata, types, trees

proc SearchForProc*(c: PContext, fn: PSym, tos: int): PSym
  # Searchs for the fn in the symbol table. If the parameter lists are exactly
  # the same the sym in the symbol table is returned, else nil.
proc SearchForBorrowProc*(c: PContext, fn: PSym, tos: int): PSym
  # Searchs for the fn in the symbol table. If the parameter lists are suitable
  # for borrowing the sym in the symbol table is returned, else nil.
# implementation

proc equalGenericParams(procA, procB: PNode): bool = 
  var a, b: PSym
  result = procA == procB
  if result: return 
  if (procA == nil) or (procB == nil): return 
  if sonsLen(procA) != sonsLen(procB): return 
  for i in countup(0, sonsLen(procA) - 1): 
    if procA.sons[i].kind != nkSym: 
      InternalError(procA.info, "equalGenericParams")
    if procB.sons[i].kind != nkSym: 
      InternalError(procB.info, "equalGenericParams")
    a = procA.sons[i].sym
    b = procB.sons[i].sym
    if (a.name.id != b.name.id) or not sameTypeOrNil(a.typ, b.typ): return 
    if (a.ast != nil) and (b.ast != nil): 
      if not ExprStructuralEquivalent(a.ast, b.ast): return 
  result = true

proc SearchForProc(c: PContext, fn: PSym, tos: int): PSym = 
  var it: TIdentIter
  result = initIdentIter(it, c.tab.stack[tos], fn.Name)
  while result != nil: 
    if (result.Kind == fn.kind): 
      if equalGenericParams(result.ast.sons[genericParamsPos], 
                            fn.ast.sons[genericParamsPos]): 
        case equalParams(result.typ.n, fn.typ.n)
        of paramsEqual: 
          return 
        of paramsIncompatible: 
          LocalError(fn.info, errNotOverloadable, fn.name.s)
          return 
        of paramsNotEqual: 
          nil
    result = NextIdentIter(it, c.tab.stack[tos])

proc paramsFitBorrow(child, parent: PNode): bool = 
  var length = sonsLen(child)
  result = false
  if length == sonsLen(parent): 
    for i in countup(1, length - 1): 
      var m = child.sons[i].sym
      var n = parent.sons[i].sym
      assert((m.kind == skParam) and (n.kind == skParam))
      if not compareTypes(m.typ, n.typ, dcEqOrDistinctOf): return 
    if not compareTypes(child.sons[0].typ, parent.sons[0].typ,
                        dcEqOrDistinctOf): return
    result = true

proc SearchForBorrowProc(c: PContext, fn: PSym, tos: int): PSym = 
  # Searchs for the fn in the symbol table. If the parameter lists are suitable
  # for borrowing the sym in the symbol table is returned, else nil.
  var it: TIdentIter
  for scope in countdown(tos, 0): 
    result = initIdentIter(it, c.tab.stack[scope], fn.Name)
    while result != nil: 
      # watchout! result must not be the same as fn!
      if (result.Kind == fn.kind) and (result.id != fn.id): 
        if equalGenericParams(result.ast.sons[genericParamsPos], 
                              fn.ast.sons[genericParamsPos]): 
          if paramsFitBorrow(fn.typ.n, result.typ.n): return 
      result = NextIdentIter(it, c.tab.stack[scope])