procfind.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module implements the searching for procs and iterators.
  10. # This is needed for proper handling of forward declarations.
  11. import
  12. ast, astalgo, msgs, semdata, types, trees, strutils, lookups
  13. proc equalGenericParams(procA, procB: PNode): bool =
  14. if procA.len != procB.len: return false
  15. for i in 0..<procA.len:
  16. if procA[i].kind != nkSym:
  17. return false
  18. if procB[i].kind != nkSym:
  19. return false
  20. let a = procA[i].sym
  21. let b = procB[i].sym
  22. if a.name.id != b.name.id or
  23. not sameTypeOrNil(a.typ, b.typ, {ExactTypeDescValues}): return
  24. if a.ast != nil and b.ast != nil:
  25. if not exprStructuralEquivalent(a.ast, b.ast): return
  26. result = true
  27. proc searchForProcAux(c: PContext, scope: PScope, fn: PSym): PSym =
  28. const flags = {ExactGenericParams, ExactTypeDescValues,
  29. ExactConstraints, IgnoreCC}
  30. var it: TIdentIter
  31. result = initIdentIter(it, scope.symbols, fn.name)
  32. while result != nil:
  33. if result.kind == fn.kind: #and sameType(result.typ, fn.typ, flags):
  34. case equalParams(result.typ.n, fn.typ.n)
  35. of paramsEqual:
  36. if (sfExported notin result.flags) and (sfExported in fn.flags):
  37. let message = ("public implementation '$1' has non-public " &
  38. "forward declaration at $2") %
  39. [getProcHeader(c.config, result, getDeclarationPath = false), c.config$result.info]
  40. localError(c.config, fn.info, message)
  41. return
  42. of paramsIncompatible:
  43. localError(c.config, fn.info, "overloaded '$1' leads to ambiguous calls" % fn.name.s)
  44. return
  45. of paramsNotEqual:
  46. discard
  47. result = nextIdentIter(it, scope.symbols)
  48. proc searchForProc*(c: PContext, scope: PScope, fn: PSym): tuple[proto: PSym, comesFromShadowScope: bool] =
  49. var scope = scope
  50. result.proto = searchForProcAux(c, scope, fn)
  51. while result.proto == nil and scope.isShadowScope:
  52. scope = scope.parent
  53. result.proto = searchForProcAux(c, scope, fn)
  54. result.comesFromShadowScope = true
  55. when false:
  56. proc paramsFitBorrow(child, parent: PNode): bool =
  57. result = false
  58. if child.len == parent.len:
  59. for i in 1..<child.len:
  60. var m = child[i].sym
  61. var n = parent[i].sym
  62. assert((m.kind == skParam) and (n.kind == skParam))
  63. if not compareTypes(m.typ, n.typ, dcEqOrDistinctOf): return
  64. if not compareTypes(child[0].typ, parent[0].typ,
  65. dcEqOrDistinctOf): return
  66. result = true
  67. proc searchForBorrowProc*(c: PContext, startScope: PScope, fn: PSym): PSym =
  68. # Searches for the fn in the symbol table. If the parameter lists are suitable
  69. # for borrowing the sym in the symbol table is returned, else nil.
  70. var it: TIdentIter
  71. for scope in walkScopes(startScope):
  72. result = initIdentIter(it, scope.symbols, fn.Name)
  73. while result != nil:
  74. # watchout! result must not be the same as fn!
  75. if (result.Kind == fn.kind) and (result.id != fn.id):
  76. if equalGenericParams(result.ast[genericParamsPos],
  77. fn.ast[genericParamsPos]):
  78. if paramsFitBorrow(fn.typ.n, result.typ.n): return
  79. result = NextIdentIter(it, scope.symbols)