summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2013-12-30 14:30:36 +0200
committerZahary Karadjov <zahary@gmail.com>2013-12-30 16:06:32 +0200
commit7e24cf26dec34a85d7551946f546f32e30b27f42 (patch)
tree78c9f6d164fc2fc3c57a5cb5a9ef6377101a94a0 /compiler
parent88873f7965d04cc2bea56f2b957e3b506442c582 (diff)
downloadNim-7e24cf26dec34a85d7551946f546f32e30b27f42.tar.gz
handle recursive types during the instantiation of meta types; propagate tfHasMeta more carefully
Diffstat (limited to 'compiler')
-rw-r--r--compiler/ast.nim8
-rw-r--r--compiler/seminst.nim1
-rw-r--r--compiler/semtypinst.nim98
-rw-r--r--compiler/types.nim5
4 files changed, 86 insertions, 26 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 22b17f673..8699d1715 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -404,8 +404,8 @@ type
     tfHasMeta,        # type contains "wildcard" sub-types such as generic params
                       # or other type classes
     tfHasGCedMem,     # type contains GC'ed memory
-    tfGenericTypeParam
     tfHasStatic
+    tfGenericTypeParam
 
   TTypeFlags* = set[TTypeFlag]
 
@@ -1229,6 +1229,10 @@ proc newSons(father: PNode, length: int) =
   else:
     setLen(father.sons, length)
 
+proc skipTypes*(t: PType, kinds: TTypeKinds): PType =
+  result = t
+  while result.kind in kinds: result = lastSon(result)
+
 proc propagateToOwner*(owner, elem: PType) =
   const HaveTheirOwnEmpty = {tySequence, tySet}
   owner.flags = owner.flags + (elem.flags * {tfHasShared, tfHasMeta,
@@ -1245,7 +1249,7 @@ proc propagateToOwner*(owner, elem: PType) =
     
   if tfShared in elem.flags:
     owner.flags.incl tfHasShared
- 
+
   if elem.kind in tyMetaTypes:
     owner.flags.incl tfHasMeta
 
diff --git a/compiler/seminst.nim b/compiler/seminst.nim
index f7f836644..cec3567e2 100644
--- a/compiler/seminst.nim
+++ b/compiler/seminst.nim
@@ -173,6 +173,7 @@ proc instGenericContainer(c: PContext, info: TLineInfo, header: PType,
     var cl: TReplTypeVars
     initIdTable(cl.symMap)
     initIdTable(cl.typeMap)
+    initIdTable(cl.localCache)
     cl.info = info
     cl.c = c
     cl.allowMetaTypes = allowMetaTypes
diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim
index 04d257707..0c185b09a 100644
--- a/compiler/semtypinst.nim
+++ b/compiler/semtypinst.nim
@@ -11,6 +11,9 @@
 
 import ast, astalgo, msgs, types, magicsys, semdata, renderer
 
+const
+  tfInstClearedFlags = {tfHasMeta}
+
 proc checkPartialConstructedType(info: TLineInfo, t: PType) =
   if tfAcyclic in t.flags and skipTypes(t, abstractInst).kind != tyObject:
     localError(info, errInvalidPragmaX, "acyclic")
@@ -67,14 +70,30 @@ type
     c*: PContext
     typeMap*: TIdTable        # map PType to PType
     symMap*: TIdTable         # map PSym to PSym
+    localCache*: TIdTable     # local cache for remembering alraedy replaced
+                              # types during instantiation of meta types
+                              # (they are not stored in the global cache)
     info*: TLineInfo
     allowMetaTypes*: bool     # allow types such as seq[Number]
                               # i.e. the result contains unresolved generics
 
-proc replaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType
+proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType
 proc replaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym
 proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode): PNode
 
+template checkMetaInvariants(cl: TReplTypeVars, t: PType) =
+  when false:
+    if t != nil and tfHasMeta in t.flags and
+       cl.allowMetaTypes == false:
+      echo "UNEXPECTED META ", t.id, " ", instantiationInfo(-1)
+      debug t
+      writeStackTrace()
+      quit 1
+
+proc replaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType =
+  result = replaceTypeVarsTAux(cl, t)
+  checkMetaInvariants(cl, result)
+
 proc prepareNode(cl: var TReplTypeVars, n: PNode): PNode =
   result = copyNode(n)
   result.typ = replaceTypeVarsT(cl, n.typ)
@@ -87,7 +106,9 @@ proc prepareNode(cl: var TReplTypeVars, n: PNode): PNode =
 proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode): PNode =
   if n == nil: return
   result = copyNode(n)
-  result.typ = replaceTypeVarsT(cl, n.typ)
+  if n.typ != nil:
+    result.typ = replaceTypeVarsT(cl, n.typ)
+    checkMetaInvariants(cl, result.typ)
   case n.kind
   of nkNone..pred(nkSym), succ(nkSym)..nkNilLit:
     discard
@@ -140,7 +161,12 @@ proc lookupTypeVar(cl: TReplTypeVars, t: PType): PType =
     result = errorType(cl.c)
   elif result.kind == tyGenericParam and not cl.allowMetaTypes:
     internalError(cl.info, "substitution with generic parameter")
- 
+
+proc instCopyType(t: PType): PType =
+  result = copyType(t, t.owner, false)
+  result.flags.incl tfFromGeneric
+  result.flags.excl tfInstClearedFlags
+
 proc handleGenericInvokation(cl: var TReplTypeVars, t: PType): PType = 
   # tyGenericInvokation[A, tyGenericInvokation[A, B]]
   # is difficult to handle: 
@@ -148,14 +174,17 @@ proc handleGenericInvokation(cl: var TReplTypeVars, t: PType): PType =
   if body.kind != tyGenericBody: internalError(cl.info, "no generic body")
   var header: PType = nil
   # search for some instantiation here:
-  result = searchInstTypes(t)
+  if cl.allowMetaTypes:
+    result = PType(idTableGet(cl.localCache, t))
+  else:
+    result = searchInstTypes(t)
   if result != nil: return
   for i in countup(1, sonsLen(t) - 1):
     var x = t.sons[i]
     if x.kind == tyGenericParam:
       x = lookupTypeVar(cl, x)
       if x != nil:
-        if header == nil: header = copyType(t, t.owner, false)
+        if header == nil: header = instCopyType(t)
         header.sons[i] = x
         propagateToOwner(header, x)
   
@@ -164,14 +193,18 @@ proc handleGenericInvokation(cl: var TReplTypeVars, t: PType): PType =
     result = searchInstTypes(header)
     if result != nil: return
   else:
-    header = copyType(t, t.owner, false)
+    header = instCopyType(t)
+  
+  result = newType(tyGenericInst, t.sons[0].owner)
+  # be careful not to propagate unnecessary flags here (don't use rawAddSon)
+  result.sons = @[header.sons[0]]
   # ugh need another pass for deeply recursive generic types (e.g. PActor)
   # we need to add the candidate here, before it's fully instantiated for
   # recursive instantions:
-  result = newType(tyGenericInst, t.sons[0].owner)
-  result.rawAddSon(header.sons[0])
   if not cl.allowMetaTypes:
     cacheTypeInst(result)
+  else:
+    idTablePut(cl.localCache, t, result)
 
   for i in countup(1, sonsLen(t) - 1):
     var x = replaceTypeVarsT(cl, t.sons[i])
@@ -180,13 +213,13 @@ proc handleGenericInvokation(cl: var TReplTypeVars, t: PType): PType =
     propagateToOwner(header, x)
     idTablePut(cl.typeMap, body.sons[i-1], x)
   
-  for i in countup(1, sonsLen(t) - 1): 
+  for i in countup(1, sonsLen(t) - 1):
     # if one of the params is not concrete, we cannot do anything
     # but we already raised an error!
     rawAddSon(result, header.sons[i])
- 
+
   var newbody = replaceTypeVarsT(cl, lastSon(body))
-  newbody.flags = newbody.flags + t.flags + body.flags
+  newbody.flags = newbody.flags + (t.flags + body.flags - tfInstClearedFlags)
   result.flags = result.flags + newbody.flags
   newbody.callConv = body.callConv
   # This type may be a generic alias and we want to resolve it here.
@@ -215,9 +248,22 @@ proc normalizeProcType(t: PType) =
       setLen t.n.sons, pos
       return
 
-proc replaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType = 
+proc propagateFieldFlags(t: PType, n: PNode) =
+  # This is meant for objects and tuples
+  # The type must be fully instantiated!
+  internalAssert n.kind != nkRecWhen
+  case n.kind
+  of nkSym:
+    propagateToOwner(t, n.sym.typ)
+  of nkRecList, nkRecCase, nkOfBranch, nkElse:
+    for son in n.sons:
+      propagateFieldFlags(t, son)
+  else: discard
+
+proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType =
   result = t
   if t == nil: return
+
   if t.kind == tyStatic and t.sym != nil and t.sym.kind == skGenericParam:
     let s = lookupTypeVar(cl, t)
     return if s != nil: s else: t
@@ -243,9 +289,10 @@ proc replaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType =
       result = lookup
       if tfUnresolved in t.flags: result = result.base
   of tyGenericInst:
-    result = copyType(t, t.owner, true)
+    result = instCopyType(t)
     for i in 1 .. <result.sonsLen:
       result.sons[i] = ReplaceTypeVarsT(cl, result.sons[i])
+    propagateToOwner(result, result.lastSon)
   else:
     if t.kind == tyArray:
       let idxt = t.sons[0]
@@ -253,22 +300,35 @@ proc replaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType =
          idxt.sym != nil and idxt.sym.kind == skGenericParam:
         let value = lookupTypeVar(cl, idxt).n
         t.sons[0] = makeRangeType(cl.c, 0, value.intVal - 1, value.info)
+    
     if containsGenericType(t):
-      result = copyType(t, t.owner, false)
-      incl(result.flags, tfFromGeneric)
+      result = instCopyType(t)
       result.size = -1 # needs to be recomputed
+      
       for i in countup(0, sonsLen(result) - 1):
-        result.sons[i] = replaceTypeVarsT(cl, result.sons[i])
+        if result.sons[i] != nil:
+          result.sons[i] = replaceTypeVarsT(cl, result.sons[i])
+          propagateToOwner(result, result.sons[i])
+
       result.n = replaceTypeVarsN(cl, result.n)
-      if result.kind in GenericTypes:
-        localError(cl.info, errCannotInstantiateX, typeToString(t, preferName))
-      if result.kind == tyProc: normalizeProcType(result)
+      
+      # XXX: This is not really needed?
+      # if result.kind in GenericTypes:
+      #   localError(cl.info, errCannotInstantiateX, typeToString(t, preferName))
+  
+      case result.kind
+      of tyObject, tyTuple:
+        propagateFieldFlags(result, result.n)
+      of tyProc:
+        normalizeProcType(result)
+      else: discard
 
 proc generateTypeInstance*(p: PContext, pt: TIdTable, info: TLineInfo,
                            t: PType): PType =
   var cl: TReplTypeVars
   initIdTable(cl.symMap)
   copyIdTable(cl.typeMap, pt)
+  initIdTable(cl.localCache)
   cl.info = info
   cl.c = p
   pushInfoContext(info)
diff --git a/compiler/types.nim b/compiler/types.nim
index 7403e29f9..a24adb17e 100644
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -64,7 +64,6 @@ const
   typedescPtrs* = abstractPtrs + {tyTypeDesc}
   typedescInst* = abstractInst + {tyTypeDesc}
 
-proc skipTypes*(t: PType, kinds: TTypeKinds): PType
 proc containsObject*(t: PType): bool
 proc containsGarbageCollectedRef*(typ: PType): bool
 proc containsHiddenPointer*(typ: PType): bool
@@ -148,10 +147,6 @@ proc skipGeneric(t: PType): PType =
   result = t
   while result.kind == tyGenericInst: result = lastSon(result)
       
-proc skipTypes(t: PType, kinds: TTypeKinds): PType = 
-  result = t
-  while result.kind in kinds: result = lastSon(result)
-  
 proc isOrdinalType(t: PType): bool =
   assert(t != nil)
   # caution: uint, uint64 are no ordinal types!
ang/Nim/blame/lib/system/threads.nim?h=devel&id=d0bfc3665fd0131dad516d2fcd7cfe73c3a6f122'>^
346443d1b ^
2ca90a20a ^
3260702a6 ^



2ca90a20a ^
3260702a6 ^
f9d8d6ce0 ^
3260702a6 ^
2ca90a20a ^
3260702a6 ^
af84f754b ^
2ca90a20a ^
af84f754b ^
2ca90a20a ^
af84f754b ^
2ca90a20a ^
af84f754b ^
3dd1ecbae ^





2ca90a20a ^
3dd1ecbae ^


af84f754b ^
2ca90a20a ^
af84f754b ^
2ca90a20a ^
af84f754b ^
2ca90a20a ^
af84f754b ^
f9d8d6ce0 ^
70fe773bb ^


3260702a6 ^
55ab6cc2b ^


3260702a6 ^
70fe773bb ^


3260702a6 ^

2ca90a20a ^
3260702a6 ^
2ca90a20a ^
3260702a6 ^
f9d8d6ce0 ^
2ca90a20a ^
3260702a6 ^


2ca90a20a ^

3260702a6 ^
2ca90a20a ^
70fe773bb ^
2ca90a20a ^
70fe773bb ^
3260702a6 ^
2ca90a20a ^
f9d8d6ce0 ^

70fe773bb ^
2ca90a20a ^
70fe773bb ^
3260702a6 ^
2ca90a20a ^
70fe773bb ^
3260702a6 ^
3260702a6 ^
2ca90a20a ^
3260702a6 ^
2ca90a20a ^
3260702a6 ^
2ca90a20a ^
70fe773bb ^
f9d8d6ce0 ^
3260702a6 ^
70fe773bb ^
2ca90a20a ^
70fe773bb ^
3260702a6 ^
2ca90a20a ^
70fe773bb ^
f9d8d6ce0 ^
2ca90a20a ^
3260702a6 ^
2ca90a20a ^
3260702a6 ^
2ca90a20a ^
3260702a6 ^

99bcc233c ^
2ca90a20a ^
99bcc233c ^
70fe773bb ^








99bcc233c ^

5e5e4abfe ^
4fa80956b ^


99bcc233c ^
346443d1b ^
3260702a6 ^
99bcc233c ^


3260702a6 ^
2ca90a20a ^
99bcc233c ^
2ca90a20a ^


99bcc233c ^
2ca90a20a ^
99bcc233c ^

5b96eaa95 ^





2ca90a20a ^
3260702a6 ^
af84f754b ^

2ca90a20a ^
af84f754b ^
2ca90a20a ^
99bcc233c ^
3260702a6 ^
84c473a89 ^
70ce8695e ^




84c473a89 ^
8446d51a6 ^
99bcc233c ^
70ce8695e ^





99bcc233c ^
346443d1b ^
99bcc233c ^

f9d8d6ce0 ^
99bcc233c ^
24ed9d560 ^
3260702a6 ^


99bcc233c ^
af84f754b ^
20c2b0082 ^
f9d8d6ce0 ^
99bcc233c ^
2ca90a20a ^
99bcc233c ^

f9d8d6ce0 ^
50e96ad93 ^
5b96eaa95 ^

f9d8d6ce0 ^

5b96eaa95 ^
346443d1b ^
5b96eaa95 ^

f9d8d6ce0 ^
728328eec ^
5b96eaa95 ^

346443d1b ^
f9d8d6ce0 ^
5b96eaa95 ^

346443d1b ^
5b96eaa95 ^






346443d1b ^
f9d8d6ce0 ^
5b96eaa95 ^

fab8cee13 ^
f9d8d6ce0 ^
dbf9117c5 ^
f9d8d6ce0 ^
3260702a6 ^
8178cd4fa ^
3260702a6 ^

2ca90a20a ^

d560e84fc ^
2ca90a20a ^
fd62116f6 ^
e6d17e627 ^
fd62116f6 ^
e6d17e627 ^
4839800c2 ^
2ca90a20a ^
5b96eaa95 ^
2ca90a20a ^
24ed9d560 ^
50e96ad93 ^
24ed9d560 ^
42e6130b2 ^
639b5e006 ^










































346443d1b ^
8446d51a6 ^
639b5e006 ^

5b96eaa95 ^




f9d8d6ce0 ^
7ad5cab17 ^
639b5e006 ^
f9d8d6ce0 ^
3260702a6 ^

f9d8d6ce0 ^
346443d1b ^
033938a53 ^
3260702a6 ^
f9d8d6ce0 ^
346443d1b ^
3260702a6 ^

f9d8d6ce0 ^
7ad5cab17 ^
4839800c2 ^
7ad5cab17 ^
30823c1ce ^
f9d8d6ce0 ^
30823c1ce ^
346443d1b ^
3260702a6 ^
f9d8d6ce0 ^
30823c1ce ^
2ca90a20a ^
728328eec ^
3260702a6 ^
30823c1ce ^
2ca90a20a ^
30823c1ce ^

2ca90a20a ^
30823c1ce ^


2ca90a20a ^
30823c1ce ^
3260702a6 ^

990dc2d71 ^

2ca90a20a ^
990dc2d71 ^





dd806cafa ^
4839800c2 ^
3260702a6 ^
30823c1ce ^
2ca90a20a ^
f9d8d6ce0 ^
30823c1ce ^






2565ff8dd ^
346443d1b ^
2565ff8dd ^

30823c1ce ^
70fe773bb ^






30823c1ce ^
f9d8d6ce0 ^

30823c1ce ^






2ca90a20a ^
2565ff8dd ^

4839800c2 ^
30823c1ce ^
2565ff8dd ^
70fe773bb ^








03550f14f ^


2ca90a20a ^
5b96eaa95 ^


02e8e9c3e ^
2ca90a20a ^
02e8e9c3e ^
2ca90a20a ^
42e6130b2 ^
99bcc233c ^
2565ff8dd ^
2ca90a20a ^
2565ff8dd ^
99bcc233c ^
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474