summary refs log tree commit diff stats
path: root/compiler
Commit message (Collapse)AuthorAgeFilesLines
...
* fixes #18921 [backport] (#18930)Andreas Rumpf2021-09-301-9/+11
|
* [backport] use old style hints in .cfg files (#18917)Miran2021-09-292-3/+3
| | | refs #18085
* envPairs works in vm, nims (#18615)Timothee Cour2021-09-291-3/+11
| | | | | * envPairs works in vm, nims * fixup
* test ord (#18909)flywind2021-09-281-4/+0
|
* fix a typo (canonilization => canonicalization) (#18914)flywind2021-09-281-1/+1
|
* fixes 'lent T' inside object constructor [backport] (#18911)Andreas Rumpf2021-09-273-3/+4
| | | | | * fixes 'lent T' inside object constructor [backport] * progress
* this ensures libp2p continues to compile [backport] (#18908)Andreas Rumpf2021-09-272-5/+16
|
* fix wrong name (rnimsyn => renderer; pnimsyn => parser; scanner => lexer) ↵flywind2021-09-254-6/+6
| | | | | | | (#18895) * fix wrong module name * rephrase more word
* closes #18690; make view types stricter [backport] (#18891)Andreas Rumpf2021-09-241-4/+2
| | | | | * closes #18690 * don't allow capturing of view types [backport]
* [backport] arc: improve compile time of (nested) loops (#18890)Clyybber2021-09-241-3/+8
|
* ported Nim to proprietary CrossOS [backport] (#18889)Andreas Rumpf2021-09-243-3/+10
|
* correct effect tracking for .borrowed procs [backport] (#18882)Andreas Rumpf2021-09-236-10/+18
| | | | | | | | | | | * correct effect tracking for .borrowed procs [backport] * progress * fix error message in a test * correctly fix it Co-authored-by: narimiran <narimiran@disroot.org>
* closes #16132 [backport] (#18880)Andreas Rumpf2021-09-221-5/+8
| | | | | * closes #16132 [backport] * fixes #16132 [backport]
* fixes #18856 [backport] (#18879)Andreas Rumpf2021-09-221-1/+1
|
* fixes #18863 [backport] (#18871)Andreas Rumpf2021-09-202-4/+5
|
* fixes #18847 [backport] (#18870)Andreas Rumpf2021-09-201-2/+3
|
* semtypinst: don't wrap type nodes from expressions in static[T] (#18860)alaviss2021-09-171-1/+1
|
* Fixed borrowing dot from aliases (#18854)Jason Beetham2021-09-161-2/+2
|
* fix #10128 (#18852)flywind2021-09-141-3/+3
|
* Dotborrow now works with generic distincts (#18848)Jason Beetham2021-09-142-3/+4
|
* Fixed #18838 (#18841) [backport]Jason Beetham2021-09-131-1/+5
|
* Generic pointer procs now error if no types supplied (#18832)Jason Beetham2021-09-112-2/+2
| | | | | | | * more precise logic for pointer procs * added test for generic pointer procs * Fixed generic getting bracket expr if erroring
* Fixed type inference for 'set` and 'tuple' (#18827)Jason Beetham2021-09-111-1/+2
| | | | | | | * improved built in typeclass inference * Smarter logic to fit node * Forgot the untyped check
* Fix subranges of distinct types (#18816) [backport]Jason Beetham2021-09-071-1/+1
|
* Fixes implicit and explicit generics in procedures (#18808)Jason Beetham2021-09-061-5/+23
| | | | | | | | | | | * Fixes implicit and explicit generics * moved block logic into 'maybeInstantiateGeneric' * Added more tests * Update compiler/semexprs.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fixes #12642 (#18811)Andreas Rumpf2021-09-063-2/+10
| | | | | | | | | | | | | | | | | | | * fixes #12642 * update important packages; refs #18804 * fixes #18805; refs #18806 * fixes a regression * Update testament/categories.nim Co-authored-by: flywind <xzsflywind@gmail.com> * progress * progress Co-authored-by: flywind <xzsflywind@gmail.com>
* '[]' can now be used for iterators (#18814)Jason Beetham2021-09-061-1/+1
|
* fixes #14165, fixes #18739, fix the second example of #6269 (#18812)Andreas Rumpf2021-09-061-4/+7
|
* Fix recursive generic typed defs (#18809)Jason Beetham2021-09-061-1/+1
|
* formal grammar updates [backport:1.2] (#18803)Andreas Rumpf2021-09-041-27/+23
|
* unicode operator bugfixes (#18802)Andreas Rumpf2021-09-041-46/+60
|
* fixes #16246 (#18800)Andreas Rumpf2021-09-043-2/+11
|
* fix for js strict mode (#18799)Sven Keller2021-09-041-1/+1
| | | | | | | | * Assignments, which would accidentally create global variables, instead throw an error in strict mode * Assignment to a getter-only property Co-authored-by: Sven Keller <s.keller@cortona.de>
* we need something better than warningAsError for effect handling viol… ↵Andreas Rumpf2021-09-041-2/+2
| | | | | | (#18796) * we need something better than warningAsError for effect handling violations
* implements https://github.com/nim-lang/RFCs/issues/407 (#18793)Andreas Rumpf2021-09-037-20/+59
|
* implemented Unicode operators (#18789)Andreas Rumpf2021-09-032-7/+76
| | | | | | | | | | | | | | | * implemented Unicode operators; refs https://github.com/nim-lang/RFCs/issues/388 * bugfix * better test * arguably more elegant implementation * Update changelog.md Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
* remove unnecessary if statement (#18792)flywind2021-09-031-6/+6
|
* fixes #18769 (#18790)Andreas Rumpf2021-09-031-1/+7
|
* fixes #18786 (#18788)Andreas Rumpf2021-09-031-6/+5
|
* fixes #16325 [backport:1.4] (#18784)Andreas Rumpf2021-09-021-2/+1
|
* strict effects (#18777)Andreas Rumpf2021-09-0216-54/+164
| | | | | | | | | | | | | | | | | * fixes #17369 * megatest is green for --cpu:arm64 * docgen output includes more tags/raises * implemented 'effectsOf' * algorithm.nim: uses new effectsOf annotation * closes #18376 * closes #17475 * closes #13905 * allow effectsOf: [a, b] * added a test case * parameters that are not ours cannot be declared as .effectsOf * documentation * manual: added the 'sort' example * bootstrap with the new better options
* fix #9778 (pairs iterator calling a helper proc with tuple return type will ↵flywind2021-09-021-11/+44
| | | | | | | | | | | cut the iterator yield into half) (#18767) * test someSymFromImportTable * Update compiler/lookups.nim * test nkTupleConstr * use isConstExpr * add tests * add comments and todo * use todo
* [minor]break loops after a candidate is added to seqs (#18770)flywind2021-08-311-7/+9
| | | | | | | | | * [minor]break loops when added * Update compiler/lookups.nim Co-authored-by: Clyybber <darkmine956@gmail.com> Co-authored-by: Clyybber <darkmine956@gmail.com>
* move PNode.comment to a side channel, reducing memory usage during ↵Timothee Cour2021-08-293-27/+67
| | | | | | | | | | | | | | | | | compilation by a factor 1.25x (#18760) * move PNode.comment so a side channel, reducing memory usage * fix a bug * fixup * use sfHasComment to speedup comment lookups * fix for IC * Update compiler/parser.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* followup #18759: make compiler build cleanly with 0 warnings/hints (#18764)Timothee Cour2021-08-282-4/+4
|
* more minor bootstrapping cleanups (#18759)Miran2021-08-283-5/+0
|
* [minor] break loops if it is ambiguous (#18745)flywind2021-08-271-6/+7
| | | | | | | | | * [minor] break loops if it is ambiguous * Update compiler/lookups.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
* fix RFC #341: dot-like operators are now parsed with same precedence as `.` ↵Timothee Cour2021-08-252-5/+31
| | | | | | | | | | | | | | | | | | | | | (#18711) * fix RFC #341: dot-like operators are now parsed with same precedence as `.` * fixup * [skip ci] address comment in changelog * address comment * update grammmar * add manual entry * fixup * -d:nimPreviewDotLikeOps * address comment to unblock PR: move nimPreviewDotLikeOps out of config/config.nims
* [minor]use `addInt` and `addFloat` (#18733)flywind2021-08-241-2/+2
| | | | | | | * unify int and uint * back * minor
* get rid of the warnings during bootstrapping (#18741)Miran2021-08-244-5/+5
| | | | | * fix bootstrapping hints and warnings * revert removals in ccgtypes
>
5ee904643 ^
5f2cdcd4f ^

860cbd310 ^

b234b082b ^
839953c3e ^
ca1f3f36b ^
839953c3e ^


38d299dfc ^
4104ee121 ^

840e13deb ^







631a8ab57 ^


840e13deb ^

8a8267e2b ^
840e13deb ^

38d299dfc ^
840e13deb ^
631a8ab57 ^




b234b082b ^
49667798e ^
7e747d11c ^
38d299dfc ^
feb0d21b2 ^
eae1aaa37 ^
58f79e7c3 ^
38d299dfc ^
feb0d21b2 ^
e879da579 ^
eae1aaa37 ^
38d299dfc ^
3b43cff0f ^
eae1aaa37 ^
860cbd310 ^
7e747d11c ^



3b43cff0f ^
38d299dfc ^
3b43cff0f ^

38d299dfc ^
eae1aaa37 ^


38d299dfc ^

eae1aaa37 ^

38d299dfc ^

3b43cff0f ^
eae1aaa37 ^
3b43cff0f ^
860cbd310 ^
3b43cff0f ^




38d299dfc ^
3b43cff0f ^
38d299dfc ^
3b43cff0f ^
38d299dfc ^
3b43cff0f ^
ca4b971bc ^






bafc94a49 ^
f70d967d2 ^

38d299dfc ^
3b43cff0f ^
eae1aaa37 ^
38d299dfc ^
860cbd310 ^
3b43cff0f ^
b234b082b ^




d2cbf7f24 ^







b234b082b ^
38d299dfc ^
72af7e682 ^

38d299dfc ^
c94647aec ^
b234b082b ^


72af7e682 ^
20b011de1 ^
72af7e682 ^

38d299dfc ^
72af7e682 ^







04b91b0d3 ^

49667798e ^


42ae2064d ^
c94647aec ^
72af7e682 ^
71a23fbbb ^








06a8d37f9 ^




c94647aec ^
71a23fbbb ^
72af7e682 ^










c94647aec ^
e3037a2f3 ^
38d299dfc ^
ca4b971bc ^
38d299dfc ^
ca4b971bc ^


041d15392 ^












de4b0346b ^
041d15392 ^






























7e747d11c ^
041d15392 ^










38d299dfc ^
041d15392 ^
c94647aec ^
041d15392 ^




796498525 ^
c94647aec ^
041d15392 ^

e3037a2f3 ^
38d299dfc ^
e3037a2f3 ^

061a9183f ^
e3037a2f3 ^
38d299dfc ^
e3037a2f3 ^




















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422


                            
                                         






                                                                  
                                                                 
                       
            
                                          
 
 



                                   
                                                                

                                                                 
                                               
 







                                                       
 
 




                      
                 
                   
              
                        
 
                                                                                     
                                          








                                                   
                                                               



                                                   
                                            


                                                                  
                                          



                    
                                                                                       












                                                                            
                                                          
                                         








                              
                                                      
 
                                                                                       



               
             
                         
                       
                                  
                


                                                  
                                         
                                         
                                       

                      





                                                                             
                                          
                                    
         
                                       
                                                    
                                     


                                
                                     


                                                                       
                     

                                                                   
                      
                         



                                                                
                                
                                       
                       

                     

                                                                               
                    
                                              
                                  


                                                        
                                  

                        







                                                                                                                              


                                                                        

                                             
                                                                                         

                                                    
                                                                
                                       




                                                                               
         
               
                                 
                                   
                                        
                     
                      
                                       
                                              
                
                     
                             
             
                     
                                         



                                    
                
                                                     

                
                                                                      


                                 

                                 

                     

                                 
            
                     
                                                              
                                      




                                 
                                          
                
                                   
         
                                                       
                         






                                                                                                    
                                            

                     
                                                                     
       
                     
                                                     
                                                               
 




                                                                    







                                                                                 
 
                                                                                         

                   
                                         
                              


                                                                           
 
                                                  

                   
                                   







                                       

                             


                                                                             
                   
                              
 








                                     




                                                                   
                              
 










                                       
                              
 
                                                  
                                             
                              


                           












                                                                                       
                                                                






























                                                                            
                                  










                                                             
                                             
                     
                              




                                                                           
                                               
                                

                                        
                                             
                                                                              

                                                             
                                                                                
                  
                               




















                                                                             
#
#
#           The Nim Compiler
#        (c) Copyright 2017 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Computes hash values for routine (proc, method etc) signatures.

import ast, tables, ropes, modulegraphs, options, msgs, pathutils
from hashes import Hash
import types
import ../dist/checksums/src/checksums/md5


when defined(nimPreviewSlimSystem):
  import std/assertions


proc `&=`(c: var MD5Context, s: string) = md5Update(c, s, s.len)
proc `&=`(c: var MD5Context, ch: char) =
  # XXX suspicious code here; relies on ch being zero terminated?
  md5Update(c, cast[cstring](unsafeAddr ch), 1)

proc `&=`(c: var MD5Context, i: BiggestInt) =
  md5Update(c, cast[cstring](unsafeAddr i), sizeof(i))
proc `&=`(c: var MD5Context, f: BiggestFloat) =
  md5Update(c, cast[cstring](unsafeAddr f), sizeof(f))
proc `&=`(c: var MD5Context, s: SigHash) =
  md5Update(c, cast[cstring](unsafeAddr s), sizeof(s))
template lowlevel(v) =
  md5Update(c, cast[cstring](unsafeAddr(v)), sizeof(v))


type
  ConsiderFlag* = enum
    CoProc
    CoType
    CoOwnerSig
    CoIgnoreRange
    CoConsiderOwned
    CoDistinct
    CoHashTypeInsideNode

proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]; conf: ConfigRef)
proc hashSym(c: var MD5Context, s: PSym) =
  if sfAnon in s.flags or s.kind == skGenericParam:
    c &= ":anon"
  else:
    var it = s
    while it != nil:
      c &= it.name.s
      c &= "."
      it = it.owner

proc hashTypeSym(c: var MD5Context, s: PSym; conf: ConfigRef) =
  if sfAnon in s.flags or s.kind == skGenericParam:
    c &= ":anon"
  else:
    var it = s
    c &= customPath(conf.toFullPath(s.info))
    while it != nil:
      if sfFromGeneric in it.flags and it.kind in routineKinds and
          it.typ != nil:
        hashType c, it.typ, {CoProc}, conf
      c &= it.name.s
      c &= "."
      it = it.owner

proc hashTree(c: var MD5Context, n: PNode; flags: set[ConsiderFlag]; conf: ConfigRef) =
  if n == nil:
    c &= "\255"
    return
  let k = n.kind
  c &= char(k)
  # we really must not hash line information. 'n.typ' is debatable but
  # shouldn't be necessary for now and avoids potential infinite recursions.
  case n.kind
  of nkEmpty, nkNilLit, nkType: discard
  of nkIdent:
    c &= n.ident.s
  of nkSym:
    hashSym(c, n.sym)
    if CoHashTypeInsideNode in flags and n.sym.typ != nil:
      hashType(c, n.sym.typ, flags, conf)
  of nkCharLit..nkUInt64Lit:
    let v = n.intVal
    lowlevel v
  of nkFloatLit..nkFloat64Lit:
    let v = n.floatVal
    lowlevel v
  of nkStrLit..nkTripleStrLit:
    c &= n.strVal
  else:
    for i in 0..<n.len: hashTree(c, n[i], flags, conf)

proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]; conf: ConfigRef) =
  if t == nil:
    c &= "\254"
    return

  case t.kind
  of tyGenericInvocation:
    for i in 0..<t.len:
      c.hashType t[i], flags, conf
  of tyDistinct:
    if CoDistinct in flags:
      if t.sym != nil: c.hashSym(t.sym)
      if t.sym == nil or tfFromGeneric in t.flags:
        c.hashType t.lastSon, flags, conf
    elif CoType in flags or t.sym == nil:
      c.hashType t.lastSon, flags, conf
    else:
      c.hashSym(t.sym)
  of tyGenericInst:
    if sfInfixCall in t.base.sym.flags:
      # This is an imported C++ generic type.
      # We cannot trust the `lastSon` to hold a properly populated and unique
      # value for each instantiation, so we hash the generic parameters here:
      let normalizedType = t.skipGenericAlias
      for i in 0..<normalizedType.len - 1:
        c.hashType t[i], flags, conf
    else:
      c.hashType t.lastSon, flags, conf
  of tyAlias, tySink, tyUserTypeClasses, tyInferred:
    c.hashType t.lastSon, flags, conf
  of tyOwned:
    if CoConsiderOwned in flags:
      c &= char(t.kind)
    c.hashType t.lastSon, flags, conf
  of tyBool, tyChar, tyInt..tyUInt64:
    # no canonicalization for integral types, so that e.g. ``pid_t`` is
    # produced instead of ``NI``:
    c &= char(t.kind)
    if t.sym != nil and {sfImportc, sfExportc} * t.sym.flags != {}:
      c.hashSym(t.sym)
  of tyObject, tyEnum:
    if t.typeInst != nil:
      # prevent against infinite recursions here, see bug #8883:
      let inst = t.typeInst
      t.typeInst = nil
      assert inst.kind == tyGenericInst
      for i in 0..<inst.len - 1:
        c.hashType inst[i], flags, conf
      t.typeInst = inst
      return
    c &= char(t.kind)
    # Every cyclic type in Nim need to be constructed via some 't.sym', so this
    # is actually safe without an infinite recursion check:
    if t.sym != nil:
      if {sfCompilerProc} * t.sym.flags != {}:
        doAssert t.sym.loc.r != ""
        # The user has set a specific name for this type
        c &= t.sym.loc.r
      elif CoOwnerSig in flags:
        c.hashTypeSym(t.sym, conf)
      else:
        c.hashSym(t.sym)

      var symWithFlags: PSym
      template hasFlag(sym): bool =
        let ret = {sfAnon, sfGenSym} * sym.flags != {}
        if ret: symWithFlags = sym
        ret
      if hasFlag(t.sym) or (t.kind == tyObject and t.owner.kind == skType and t.owner.typ.kind == tyRef and hasFlag(t.owner)):
        # for `PFoo:ObjectType`, arising from `type PFoo = ref object`
        # Generated object names can be identical, so we need to
        # disambiguate furthermore by hashing the field types and names.
        if t.n.len > 0:
          let oldFlags = symWithFlags.flags
          # Hack to prevent endless recursion
          # xxx instead, use a hash table to indicate we've already visited a type, which
          # would also be more efficient.
          symWithFlags.flags.excl {sfAnon, sfGenSym}
          hashTree(c, t.n, flags + {CoHashTypeInsideNode}, conf)
          symWithFlags.flags = oldFlags
        else:
          # The object has no fields: we _must_ add something here in order to
          # make the hash different from the one we produce by hashing only the
          # type name.
          c &= ".empty"
    else:
      c &= t.id
    if t.len > 0 and t[0] != nil:
      hashType c, t[0], flags, conf
  of tyRef, tyPtr, tyGenericBody, tyVar:
    c &= char(t.kind)
    if t.sons.len > 0:
      c.hashType t.lastSon, flags, conf
    if tfVarIsPtr in t.flags: c &= ".varisptr"
  of tyFromExpr:
    c &= char(t.kind)
    c.hashTree(t.n, {}, conf)
  of tyTuple:
    c &= char(t.kind)
    if t.n != nil and CoType notin flags:
      assert(t.n.len == t.len)
      for i in 0..<t.n.len:
        assert(t.n[i].kind == nkSym)
        c &= t.n[i].sym.name.s
        c &= ':'
        c.hashType(t[i], flags+{CoIgnoreRange}, conf)
        c &= ','
    else:
      for i in 0..<t.len: c.hashType t[i], flags+{CoIgnoreRange}, conf
  of tyRange:
    if CoIgnoreRange notin flags:
      c &= char(t.kind)
      c.hashTree(t.n, {}, conf)
    c.hashType(t[0], flags, conf)
  of tyStatic:
    c &= char(t.kind)
    c.hashTree(t.n, {}, conf)
    c.hashType(t[0], flags, conf)
  of tyProc:
    c &= char(t.kind)
    c &= (if tfIterator in t.flags: "iterator " else: "proc ")
    if CoProc in flags and t.n != nil:
      let params = t.n
      for i in 1..<params.len:
        let param = params[i].sym
        c &= param.name.s
        c &= ':'
        c.hashType(param.typ, flags, conf)
        c &= ','
      c.hashType(t[0], flags, conf)
    else:
      for i in 0..<t.len: c.hashType(t[i], flags, conf)
    c &= char(t.callConv)
    # purity of functions doesn't have to affect the mangling (which is in fact
    # problematic for HCR - someone could have cached a pointer to another
    # function which changes its purity and suddenly the cached pointer is danglign)
    # IMHO anything that doesn't affect the overload resolution shouldn't be part of the mangling...
    # if CoType notin flags:
    #   if tfNoSideEffect in t.flags: c &= ".noSideEffect"
    #   if tfThread in t.flags: c &= ".thread"
    if tfVarargs in t.flags: c &= ".varargs"
  of tyArray:
    c &= char(t.kind)
    for i in 0..<t.len: c.hashType(t[i], flags-{CoIgnoreRange}, conf)
  else:
    c &= char(t.kind)
    for i in 0..<t.len: c.hashType(t[i], flags, conf)
  if tfNotNil in t.flags and CoType notin flags: c &= "not nil"

when defined(debugSigHashes):
  import db_sqlite

  let db = open(connection="sighashes.db", user="araq", password="",
                database="sighashes")
  db.exec(sql"DROP TABLE IF EXISTS sighashes")
  db.exec sql"""CREATE TABLE sighashes(
    id integer primary key,
    hash varchar(5000) not null,
    type varchar(5000) not null,
    unique (hash, type))"""
  #  select hash, type from sighashes where hash in
  # (select hash from sighashes group by hash having count(*) > 1) order by hash;

proc hashType*(t: PType; conf: ConfigRef; flags: set[ConsiderFlag] = {CoType}): SigHash =
  var c: MD5Context
  md5Init c
  hashType c, t, flags+{CoOwnerSig}, conf
  md5Final c, result.MD5Digest
  when defined(debugSigHashes):
    db.exec(sql"INSERT OR IGNORE INTO sighashes(type, hash) VALUES (?, ?)",
            typeToString(t), $result)

proc hashProc(s: PSym; conf: ConfigRef): SigHash =
  var c: MD5Context
  md5Init c
  hashType c, s.typ, {CoProc}, conf

  var m = s
  while m.kind != skModule: m = m.owner
  let p = m.owner
  assert p.kind == skPackage
  c &= p.name.s
  c &= "."
  c &= m.name.s
  if sfDispatcher in s.flags:
    c &= ".dispatcher"
  # so that createThread[void]() (aka generic specialization) gets a unique
  # hash, we also hash the line information. This is pretty bad, but the best
  # solution for now:
  #c &= s.info.line
  md5Final c, result.MD5Digest

proc hashNonProc*(s: PSym): SigHash =
  var c: MD5Context
  md5Init c
  hashSym(c, s)
  var it = s
  while it != nil:
    c &= it.name.s
    c &= "."
    it = it.owner
  # for bug #5135 we also take the position into account, but only
  # for parameters, because who knows what else position dependency
  # might cause:
  if s.kind == skParam:
    c &= s.position
  md5Final c, result.MD5Digest

proc hashOwner*(s: PSym): SigHash =
  var c: MD5Context
  md5Init c
  var m = s
  while m.kind != skModule: m = m.owner
  let p = m.owner
  assert p.kind == skPackage
  c &= p.name.s
  c &= "."
  c &= m.name.s

  md5Final c, result.MD5Digest

proc sigHash*(s: PSym; conf: ConfigRef): SigHash =
  if s.kind in routineKinds and s.typ != nil:
    result = hashProc(s, conf)
  else:
    result = hashNonProc(s)

proc symBodyDigest*(graph: ModuleGraph, sym: PSym): SigHash

proc hashBodyTree(graph: ModuleGraph, c: var MD5Context, n: PNode)

proc hashVarSymBody(graph: ModuleGraph, c: var MD5Context, s: PSym) =
  assert: s.kind in {skParam, skResult, skVar, skLet, skConst, skForVar}
  if sfGlobal notin s.flags:
    c &= char(s.kind)
    c &= s.name.s
  else:
    c &= hashNonProc(s)
    # this one works for let and const but not for var. True variables can change value
    # later on. it is user resposibility to hash his global state if required
    if s.ast != nil and s.ast.kind in {nkIdentDefs, nkConstDef}:
      hashBodyTree(graph, c, s.ast[^1])
    else:
      hashBodyTree(graph, c, s.ast)

proc hashBodyTree(graph: ModuleGraph, c: var MD5Context, n: PNode) =
  # hash Nim tree recursing into simply
  if n == nil:
    c &= "nil"
    return
  c &= char(n.kind)
  case n.kind
  of nkEmpty, nkNilLit, nkType: discard
  of nkIdent:
    c &= n.ident.s
  of nkSym:
    if n.sym.kind in skProcKinds:
      c &= symBodyDigest(graph, n.sym)
    elif n.sym.kind in {skParam, skResult, skVar, skLet, skConst, skForVar}:
      hashVarSymBody(graph, c, n.sym)
    else:
      c &= hashNonProc(n.sym)
  of nkProcDef, nkFuncDef, nkTemplateDef, nkMacroDef:
    discard # we track usage of proc symbols not their definition
  of nkCharLit..nkUInt64Lit:
    c &= n.intVal
  of nkFloatLit..nkFloat64Lit:
    c &= n.floatVal
  of nkStrLit..nkTripleStrLit:
    c &= n.strVal
  else:
    for i in 0..<n.len:
      hashBodyTree(graph, c, n[i])

proc symBodyDigest*(graph: ModuleGraph, sym: PSym): SigHash =
  ## compute unique digest of the proc/func/method symbols
  ## recursing into invoked symbols as well
  assert(sym.kind in skProcKinds, $sym.kind)

  graph.symBodyHashes.withValue(sym.id, value):
    return value[]

  var c: MD5Context
  md5Init(c)
  c.hashType(sym.typ, {CoProc}, graph.config)
  c &= char(sym.kind)
  c.md5Final(result.MD5Digest)
  graph.symBodyHashes[sym.id] = result # protect from recursion in the body

  if sym.ast != nil:
    md5Init(c)
    c.md5Update(cast[cstring](result.addr), sizeof(result))
    hashBodyTree(graph, c, getBody(graph, sym))
    c.md5Final(result.MD5Digest)
    graph.symBodyHashes[sym.id] = result

proc idOrSig*(s: PSym, currentModule: string,
              sigCollisions: var CountTable[SigHash]; conf: ConfigRef): Rope =
  if s.kind in routineKinds and s.typ != nil:
    # signatures for exported routines are reliable enough to
    # produce a unique name and this means produced C++ is more stable regarding
    # Nim changes:
    let sig = hashProc(s, conf)
    result = rope($sig)
    #let m = if s.typ.callConv != ccInline: findPendingModule(m, s) else: m
    let counter = sigCollisions.getOrDefault(sig)
    #if sigs == "_jckmNePK3i2MFnWwZlp6Lg" and s.name.s == "contains":
    #  echo "counter ", counter, " ", s.id
    if counter != 0:
      result.add "_" & rope(counter+1)
    # this minor hack is necessary to make tests/collections/thashes compile.
    # The inlined hash function's original module is ambiguous so we end up
    # generating duplicate names otherwise:
    if s.typ.callConv == ccInline:
      result.add rope(currentModule)
    sigCollisions.inc(sig)
  else:
    let sig = hashNonProc(s)
    result = rope($sig)
    let counter = sigCollisions.getOrDefault(sig)
    if counter != 0:
      result.add "_" & rope(counter+1)
    sigCollisions.inc(sig)