-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsemexprs.nim
3943 lines (3518 loc) · 138 KB
/
semexprs.nim
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
#
# The Nim Compiler
# (c) Copyright 2013 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## this module does the semantic checking for expressions
## included from sem.nim
proc semTemplateExpr(c: PContext, n: PNode, s: PSym,
flags: TExprFlags = {}): PNode =
rememberExpansion(c, n.info, s)
let info = getCallLineInfo(n)
markUsed(c, info, s)
# Note: This is n.info on purpose. It prevents template from creating an info
# context when called from an another template
pushInfoContext(c.config, n.info, s)
result = evalTemplate(n, s, getCurrOwner(c), c.config, c.cache,
c.templInstCounter, c.idgen, efFromHlo in flags)
if efNoSemCheck notin flags: result = semAfterMacroCall(c, n, result, s, flags)
popInfoContext(c.config)
# XXX: A more elaborate line info rewrite might be needed
result.info = info
proc semFieldAccess(c: PContext, n: PNode, flags: TExprFlags = {}): PNode
template rejectEmptyNode(n: PNode) =
# No matter what a nkEmpty node is not what we want here
if n.kind == nkEmpty:
semReportIllformedAst(c.config, n, "Unexpected empty node")
proc semOperand(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
# same as 'semExprWithType' but doesn't check for proc vars
rejectEmptyNode(n)
let exprFlags = flags + efOperand
result =
case n.kind
of nkStmtList, nkStmtListExpr:
if nfSem in n.flags:
n
else:
semStmtList(c, n, exprFlags, collapse = false)
else:
semExpr(c, n, exprFlags)
result.flags.incl nfSem # `semStmtList` doesn't add it
if result.typ != nil:
# XXX tyGenericInst here?
if result.typ.kind == tyProc and hasUnresolvedParams(result):
result = c.config.newError(n, PAstDiag(kind: adSemProcHasNoConcreteType))
elif result.typ.kind in {tyVar, tyLent}:
result = newDeref(result)
elif {efWantStmt, efAllowStmt} * flags != {}:
result.typ = newTypeS(tyVoid, c)
else:
result = c.config.newError(n, PAstDiag(kind: adSemExpressionHasNoType))
proc semExprCheck(c: PContext, n: PNode, flags: TExprFlags): PNode =
addInNimDebugUtils(c.config, "semExprCheck", n, result, flags)
rejectEmptyNode(n)
result = semExpr(c, n, flags+{efWantValue})
let
isEmpty = result.kind == nkEmpty
isError = result.kind == nkError
isTypeError = result.typ != nil and result.typ.kind == tyError
if isError:
discard # no need to do anything
elif isEmpty or isTypeError:
# bug #12741, redundant error messages are the lesser evil here:
result = c.config.newError(n, PAstDiag(kind: adSemExpressionHasNoType))
proc semExprWithType(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
addInNimDebugUtils(c.config, "semExprWithType", n, result, flags)
result = semExprCheck(c, n, flags)
if result.typ == nil and efInTypeof in flags:
result.typ = c.voidType
elif result.typ == nil or result.typ == c.enforceVoidContext:
result = c.config.newError(n, PAstDiag(kind: adSemExpressionHasNoType))
elif result.typ.kind == tyError:
# associates the type error to the current owner
result.typ = errorType(c)
elif result.typ.kind in {tyVar, tyLent}:
result = newDeref(result)
proc semExprNoDeref(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
result = semExprCheck(c, n, flags)
if result.typ == nil:
result = c.config.newError(n, PAstDiag(kind: adSemExpressionHasNoType))
proc inlineConst(c: PContext, n: PNode, s: PSym): PNode {.inline.} =
if s.ast.isNil:
result = c.config.newError(n, PAstDiag(kind: adSemConstantOfTypeHasNoValue,
constSym: s))
else:
result = copyTree(s.ast)
result.typ = s.typ
result.info = n.info
type
TConvStatus = enum
convOK,
convNotNeedeed,
convNotLegal,
convNotInRange
proc checkConversionBetweenObjects(castDest, src: PType; pointers: int): TConvStatus =
let diff = inheritanceDiff(castDest, src)
return if diff == high(int) or (pointers > 1 and diff != 0):
convNotLegal
else:
convOK
const
IntegralTypes = {tyBool, tyEnum, tyChar, tyInt..tyUInt64}
proc checkConvertible(c: PContext, targetTyp: PType, src: PNode): TConvStatus =
let srcTyp = src.typ.skipTypes({tyStatic})
result = convOK
if sameType(targetTyp, srcTyp) and targetTyp.sym == srcTyp.sym:
# don't annoy conversions that may be needed on another processor:
if targetTyp.kind notin IntegralTypes+{tyRange}:
result = convNotNeedeed
return
var d = skipTypes(targetTyp, abstractVar)
var s = srcTyp
if s.kind in tyUserTypeClasses and s.isResolvedUserTypeClass:
s = s.lastSon
s = skipTypes(s, abstractVar-{tyTypeDesc})
var pointers = 0
while (d != nil) and (d.kind in {tyPtr, tyRef}):
if d.kind != s.kind:
break
else:
d = d.lastSon
s = s.lastSon
inc pointers
let targetBaseTyp = skipTypes(targetTyp, abstractVarRange)
let srcBaseTyp = skipTypes(srcTyp, abstractVarRange-{tyTypeDesc})
if d == nil:
result = convNotLegal
elif d.skipTypes(abstractInst).kind == tyObject and s.skipTypes(abstractInst).kind == tyObject:
result = checkConversionBetweenObjects(d.skipTypes(abstractInst), s.skipTypes(abstractInst), pointers)
elif (targetBaseTyp.kind in IntegralTypes) and
(srcBaseTyp.kind in IntegralTypes):
if targetTyp.kind == tyEnum and srcBaseTyp.kind == tyEnum:
localReport(c.config, src.info, SemReport(
kind: rsemSuspiciousEnumConv,
ast: src,
typeMismatch: @[
typeMismatch(formal = targetTyp, actual = srcBaseTyp)]))
# `elif` would be incorrect here
if targetTyp.kind == tyBool:
discard "convOk"
elif targetTyp.isOrdinalType:
if src.kind in nkIntLiterals and
src.getInt notin firstOrd(c.config, targetTyp)..lastOrd(c.config, targetTyp):
result = convNotInRange
elif src.kind in nkFloatLiterals:
if not src.floatVal.inInt128Range:
result = convNotInRange
elif src.floatVal.toInt128 notin firstOrd(c.config, targetTyp)..lastOrd(c.config, targetTyp):
result = convNotInRange
elif targetBaseTyp.kind in tyFloat..tyFloat64:
if src.kind in nkFloatLiterals and
not floatRangeCheck(src.floatVal, targetTyp):
result = convNotInRange
elif src.kind in nkIntLiterals and
not floatRangeCheck(src.intVal.float, targetTyp):
result = convNotInRange
elif targetBaseTyp.enumHasHoles:
if src.kind in nkIntLiterals and
toInt64(src.getInt).int notin getIntSetOfType(c, targetBaseTyp):
result = convNotInRange
else:
# we use d, s here to speed up that operation a bit:
case cmpTypes(c, d, s)
of isNone, isGeneric:
if not compareTypes(
targetTyp.skipTypes(abstractVar),
srcTyp,
dcEqIgnoreDistinct
):
result = convNotLegal
else:
discard
proc isCastable(c: PContext; dst, src: PType): bool =
## Checks whether the source type can be cast to the destination type.
## Casting is very unrestrictive; casts are allowed as long as
## castDest.size >= src.size, and typeAllowed(dst, skParam)
#const
# castableTypeKinds = {tyInt, tyPtr, tyRef, tyCstring, tyString,
# tySequence, tyPointer, tyNil, tyOpenArray,
# tyProc, tySet, tyEnum, tyBool, tyChar}
let src = src.skipTypes(tyUserTypeClasses)
if tyError in {src.kind, dst.kind}:
return true # error correction for suggest, check, etc.
if skipTypes(dst, abstractInst-{tyOpenArray}).kind == tyOpenArray:
return false
if skipTypes(src, abstractInst-{tyTypeDesc}).kind == tyTypeDesc:
return false
if skipTypes(dst, abstractInst).kind == tyBuiltInTypeClass:
return false
if skipTypes(src, abstractInst).kind == tyOpenArray:
# always castable from for backwards compatibility
return true
let conf = c.config
if conf.selectedGC in {gcArc, gcOrc}:
let d = skipTypes(dst, abstractInst)
let s = skipTypes(src, abstractInst)
if d.kind == tyRef and s.kind == tyRef and s[0].isFinal != d[0].isFinal:
return false
elif d.kind in IntegralTypes and s.kind in {tyString, tySequence}:
return false
var dstSize, srcSize: BiggestInt
dstSize = computeSize(conf, dst)
srcSize = computeSize(conf, src)
if dstSize == szUnknownSize or srcSize == szUnknownSize:
# The Nim compiler can't detect if it's legal or not.
# Just assume the programmer knows what they're is doing.
return true
if dstSize < 0:
result = false
elif srcSize < 0:
result = false
elif typeAllowed(dst, skParam, c) != nil:
result = false
elif dst.kind == tyProc and dst.callConv == ccClosure:
result = src.kind == tyProc and src.callConv == ccClosure
else:
result = (dstSize >= srcSize) or
(skipTypes(dst, abstractInst).kind in IntegralTypes) or
(skipTypes(src, abstractInst-{tyTypeDesc}).kind in IntegralTypes)
if result and src.kind == tyNil:
result = dst.size <= conf.target.ptrSize
proc isSymChoice(n: PNode): bool {.inline.} =
result = n.kind in nkSymChoices
proc maybeLiftType(t: var PType, c: PContext, info: TLineInfo) =
# XXX: liftParamType started to perform addDecl
# we could do that instead in semTypeNode by snooping for added
# gnrc. params, then it won't be necessary to open a new scope here
openScope(c)
var lifted = liftParamType(c, skType, newNodeI(nkArgList, info),
t, ":anon", info)
closeScope(c)
if lifted != nil: t = lifted
proc semConv(c: PContext, n: PNode): PNode =
addInNimDebugUtils(c.config, "semConv", n, result)
if n.len != 2:
result = c.config.newError(n,
PAstDiag(kind: adSemTypeConversionArgumentMismatch,
convArgsRecvd: n.len - 1))
return
result = newNodeI(nkConv, n.info)
var targetType = semTypeNode(c, n[0], nil)
case targetType.kind
of tyTypeDesc:
c.config.internalAssert targetType.len > 0
if targetType.base.kind == tyNone:
return semTypeOf(c, n)
else:
targetType = targetType.base
of tyStatic:
if targetType.base.kind == tyNone:
let evaluated = semStaticExpr(c, n[1])
# the meaning depends on the type of the operand
if evaluated.typ.kind == tyTypeDesc:
# a type construction, e.g.: ``static(int)``
result = newTreeI(nkStaticTy, n.info, evaluated)
result.typ = c.makeTypeDesc:
let typ = newTypeS(tyStatic, c)
typ.rawAddSon(evaluated.typ.base)
typ.flags.incl tfHasStatic
typ
return
else:
# an expression forcefully evaluated at compile-time,
# e.g.: ``static(x)``
return evaluated
else:
# a coercion to a static type, e.g.: ``static[int](x)``. It's
# semantically equivalent to ``static(int(x))``
result.add newNodeIT(nkType, n[0].info, c.makeTypeDesc targetType.base)
result.add n[1]
result = newTreeI(nkStaticExpr, n.info, result)
return semExprWithType(c, result, {})
else: discard
maybeLiftType(targetType, c, n[0].info)
var hasError = false
block:
let s = qualifiedLookUp(c, n[0], {})
if s.isError:
result.add s.ast
hasError = true
else:
result.add copyTree(n[0])
# special case to make MyObject(x = 3) produce a nicer error message:
if n[1].kind == nkExprEqExpr and
targetType.skipTypes(abstractPtrs).kind == tyObject:
result = c.config.newError(n,
PAstDiag(kind: adSemUnexpectedEqInObjectConstructor,
eqInfo: n[1].info))
return
template handleError(c: PContext, result: PNode, hasError: bool): PNode =
if hasError and result.kind != nkError:
c.config.wrapError(result)
else:
result
var op = semExprWithType(c, n[1])
if targetType.kind != tyGenericParam and targetType.isMetaType:
let final = inferWithMetatype(c, targetType, op, true)
# XXX: this makes little sense -- the source and target type of the
# resulting conversion are the same
result.add final
result.typ = final.typ
return handleError(c, result, hasError)
result.typ = targetType
# XXX op is overwritten later on, this is likely added too early
# here or needs to be overwritten too then.
result.add op
if targetType.kind == tyGenericParam:
result.typ = makeTypeFromExpr(c, copyTree(result))
return handleError(c, result, hasError)
if not isSymChoice(op):
let status = checkConvertible(c, result.typ, op)
case status
of convOK:
# handle SomeProcType(SomeGenericProc)
if op.kind == nkSym and op.sym.isGenericRoutine:
result[1] = fitNode(c, result.typ, result[1], result.info)
elif op.kind in {nkPar, nkTupleConstr} and targetType.kind == tyTuple:
op = fitNode(c, targetType, op, result.info)
of convNotNeedeed:
localReport(c.config, n.info, reportTyp(
rsemConvFromXtoItselfNotNeeded, result.typ, ast = result))
of convNotLegal:
result = fitNode(c, result.typ, result[1], result.info)
if result == nil:
result = c.config.newError(n, PAstDiag(
kind: adSemIllegalConversion,
typeMismatch: @[typeMismatch(formal = result.typ, actual = op.typ)]))
of convNotInRange:
result = c.config.newError(n, PAstDiag(kind: adSemCannotBeConvertedTo,
inputVal: op,
targetTyp: result.typ))
else:
for it in op:
let status = checkConvertible(c, result.typ, it)
if status in {convOK, convNotNeedeed}:
markUsed(c, n.info, it.sym)
markIndirect(c, it.sym)
return
if hasError:
handleError(c, result, hasError)
else:
it
let s = errorUseQualifier(c, op, op[0].sym)
if s.isError:
result[1] = s.ast
result = c.config.wrapError(result)
else:
# `errorUseQualifier` won't return an error sym if it's unambiguous, but
# we already know that it can't be used for conversion via the loop above
result = c.config.newError(n, PAstDiag(kind: adSemCannotBeConvertedTo,
inputVal: op,
targetTyp: result.typ))
proc semCast(c: PContext, n: PNode): ElaborateAst =
## Semantically analyze a casting ("cast[type](param)")
checkSonsLen(n, 2, c.config)
let
typeExpr = semTypeNode2(c, n[0], nil)
targetType = typeExpr.typ
castedExpr = semExprWithType(c, n[1])
result.initWith(n)
result.typ = targetType
result[0] = typeExpr
result[1] = castedExpr
if tfHasMeta in targetType.flags:
result.diag = PAstDiag(kind: adSemCannotCastToNonConcrete,
wrongType: targetType)
elif not isCastable(c, targetType, castedExpr.typ):
result.diag = PAstDiag(
kind: adSemCannotCastTypes,
typeMismatch: @[typeMismatch(formal = targetType,
actual = castedExpr.typ)])
proc semLowHigh(c: PContext, n: PNode, m: TMagic): PNode =
if n.len != 2:
result =
case m
of mLow, mHigh:
c.config.newError(n, PAstDiag(kind: adSemMagicExpectTypeOrValue,
magic: m))
else:
unreachable()
else:
n[1] = semExprWithType(c, n[1])
var typ = skipTypes(n[1].typ, abstractVarRange + {tyTypeDesc, tyUserTypeClassInst})
case typ.kind
of tySequence, tyString, tyCstring, tyOpenArray, tyVarargs:
n.typ = getSysType(c.graph, n.info, tyInt)
of tyArray:
n.typ = typ[0] # indextype
if n.typ.kind == tyRange and emptyRange(n.typ.n[0], n.typ.n[1]): #Invalid range
n.typ = getSysType(c.graph, n.info, tyInt)
of tyInt..tyInt64, tyChar, tyBool, tyEnum, tyUInt..tyUInt64, tyFloat..tyFloat64:
n.typ = n[1].typ.skipTypes({tyTypeDesc})
of tyGenericParam:
# prepare this for resolving in semtypinst:
# we must use copyTree here in order to avoid creating a cycle
# that could easily turn into an infinite recursion in semtypinst
n.typ = makeTypeFromExpr(c, n.copyTree)
else:
result =
case m
of mLow, mHigh:
c.config.newError(n, PAstDiag(kind: adSemLowHighInvalidArgument,
invalidTyp: typ,
highLow: m))
else:
unreachable()
return
result = n
proc fixupStaticType(c: PContext, n: PNode) =
# This proc can be applied to evaluated expressions to assign
# them a static type.
#
# XXX: with implicit static, this should not be necessary,
# because the output type of operations such as `semConstExpr`
# should be a static type (as well as the type of any other
# expression that can be implicitly evaluated). For now, we
# apply this measure only in code that is enlightened to work
# with static types.
if n.typ.kind != tyStatic:
n.typ = newTypeWithSons(getCurrOwner(c), tyStatic, @[n.typ], c.idgen)
n.typ.n = n # XXX: cycles like the one here look dangerous.
# Consider using `n.copyTree`
proc isOpImpl(c: PContext, n: PNode, flags: TExprFlags): PNode =
## implements `is`, for `x is Y` where x is an expression and `Y` is a type
## or an expression whose type is compared with `x`'s type.
c.config.internalAssert:
n.len == 3 and n[1].typ != nil and
n[2].kind in nkStrLiterals + nkType
var
res = false
t1 = n[1].typ
t2 = n[2].typ
if t1.kind == tyTypeDesc and t2.kind != tyTypeDesc:
t1 = t1.base
if n[2].kind in nkStrLiterals:
case n[2].strVal.normalize
of "closure":
let t = skipTypes(t1, abstractRange)
res = t.kind == tyProc and
t.callConv == ccClosure
of "iterator":
let t = skipTypes(t1, abstractRange)
res = t.kind == tyProc and
t.callConv == ccClosure and
tfIterator in t.flags
else:
res = false
else:
if t1.skipTypes({tyGenericInst, tyAlias, tySink, tyDistinct}).kind != tyGenericBody:
maybeLiftType(t2, c, n.info)
else:
#[
for this case:
type Foo = object[T]
Foo is Foo
]#
discard
var m = newCandidate(c, t2)
if efExplain in flags:
m.error.diagnosticsEnabled = true
res = typeRel(m, t2, t1) >= isSubtype # isNone
if m.error.diagnosticsEnabled and (m.error.diag.diags.len > 0 or
m.error.diag.tempDiagFailCount > 0):
localReport(c.config, n.info, SemReport(
ast: n,
kind: rsemDiagnostics,
diag: m.error.diag
))
# `res = sameType(t1, t2)` would be wrong, e.g. for `int is (int|float)`
result = newIntNode(nkIntLit, ord(res))
result.typ = n.typ
result.info = n.info
proc semIs(c: PContext, n: PNode, flags: TExprFlags): PNode =
addInNimDebugUtils(c.config, "semIs", n, result, flags)
if n.len != 3:
# the check is necessary because `is` can be analyzed pre-sigmatch
return c.config.newError(n, PAstDiag(kind: adSemIsOperatorTakes2Args))
let boolType = getSysType(c.graph, n.info, tyBool)
var
liftLhs = true
lhs = semExprWithType(c, n[1], flags + {efWantIterator})
rhs: PNode
case n[2].kind
of nkStrLiterals:
rhs = semExpr(c, n[2])
of nkError:
discard # below we'll wrap the result in an error
else:
let t2 = semTypeNode(c, n[2], nil)
rhs = newNodeIT(nkType, n[2].info, t2)
if t2.kind == tyStatic:
let evaluated = tryConstExpr(c, lhs)
if evaluated != nil:
c.fixupStaticType(evaluated)
lhs = evaluated
else:
result = newIntNode(nkIntLit, 0)
result.typ = boolType
result.info = n.info
return
elif t2.kind == tyTypeDesc and
(t2.base.kind == tyNone or tfExplicit in t2.flags):
# When the right-hand side is an explicit type, we must
# not allow regular values to be matched against the type:
liftLhs = false
result = shallowCopy(n)
result.typ = boolType
result[0] = n[0]
result[1] = lhs
result[2] = rhs
if result[1].isError or result[2].isError:
result = wrapError(c.config, result)
else:
if lhs.typ.kind != tyTypeDesc and liftLhs:
result[1] = makeTypeSymNode(c, lhs.typ, n[1].info)
result = isOpImpl(c, result, flags)
proc semOpAux(c: PContext, n: PNode): bool =
## Returns whether n contains errors
## This does not not wrap child errors in n
## the caller has to handle that
for i in 1..<n.len:
var a = n[i]
if a.kind == nkExprEqExpr and a.len == 2:
let
info = a[0].info
ident = legacyConsiderQuotedIdent(c, a[0], a)
a[0] = newIdentNode(ident, info)
a[1] = semExprWithType(c, a[1])
if a[1].isError:
result = true
a.typ = a[1].typ
else:
n[i] = semExprWithType(c, a)
if n[i].isError:
result = true
proc overloadedCallOpr(c: PContext, n: PNode): PNode =
## search for an overloaded call operator (`()`), if it exists create the
## call tree, else return `nil`.
var par = getIdent(c.cache, "()")
var amb = false
if searchInScopes(c, par, amb) == nil:
result = nil
else:
result = newNodeI(nkCall, n.info)
result.add newIdentNode(par, n.info)
for i in 0..<n.len:
result.add n[i]
proc changeType(c: PContext, n: PNode, newType: PType, check: bool): PNode =
result = n # xxx: typically avoid this but this doesn't look like a semantic
# analysis proc resulting in a production but more an
# operation/helper mutating an existing production
var hasError = false
case n.kind
of nkCurly, nkBracket:
for i, kid in n.pairs:
result[i] = changeType(c, kid, elemType(newType), check)
if result[i].isError:
hasError = true
of nkPar, nkTupleConstr:
let tup = newType.skipTypes({tyGenericInst, tyAlias, tySink, tyDistinct})
case tup.kind
of tyTuple:
if n.len > 0 and n[0].kind == nkExprColonExpr:
# named tuple
for i, colonExpr in n.pairs:
let
key = colonExpr[0]
val = colonExpr[1]
case key.kind
of nkSym:
let elemTyp =
if tup.n.isNil:
tup[i]
else:
let f = getSymFromList(tup.n, key.sym.name)
if f.isNil:
result = newError(c.config, n,
PAstDiag(kind: adSemUnknownIdentifier,
unknownSym: key.sym))
return # hard error
f.typ
result[i][1] = changeType(c, val, elemTyp, check)
if result[i].isError:
hasError = true
else:
result = newError(c.config, key,
PAstDiag(kind: adSemInvalidTupleConstructorKey,
invalidKey: n))
return # hard error
else:
# positional args
for i, elem in n.pairs:
result[i] = changeType(c, elem, tup[i], check)
if result[i].isError:
hasError = true
of tyObject:
result = n # xxx: feels like `changeType` the proc is a hack
return # original bug: https://github.com/nim-lang/Nim/issues/2602
else:
result = newError(c.config, n,
PAstDiag(kind: adSemNoTupleTypeForConstructor))
return # hard error
of nkIntLiterals:
if check and n.kind != nkUInt64Lit and not sameType(n.typ, newType):
let val = n.intVal
if val < firstOrd(c.config, newType) or val > lastOrd(c.config, newType):
result = newError(c.config, n,
PAstDiag(kind: adSemCannotBeConvertedTo,
inputVal: n,
targetTyp: newType))
of nkFloatLiterals:
if check and not floatRangeCheck(n.floatVal, newType):
result = newError(c.config, n,
PAstDiag(kind: adSemCannotBeConvertedTo,
inputVal: n,
targetTyp: newType))
of nkError:
return # return an error
else:
discard
n.typ = newType # `n` is either the wrongNode in an error or same as `result`
if hasError and result.kind != nkError:
result = c.config.wrapError(result)
proc semArrayElementIndex(c: PContext, n: PNode, formalIdx: PType
): tuple[n: PNode, val: Int128] =
## Analyses the array element expression `n`, producing either the
## semantically analysed index expression plus its value, an error, or an
## ``nkEmpty`` if no index is specified.
##
## `formalIdx` specifies the type that the index value must fit. If
## `formalIdx` is nil, the index specified by `n` is always allowed as long
## as it's of a valid ordinal-like type.
addInNimDebugUtils(c.config, "semArrayElementIndex", n, result.n)
case n.kind
of nkExprColonExpr:
checkSonsLen(n, 2, c.config)
let idx = semRealConstExpr(c, n[0])
if idx.kind == nkError:
(idx, Zero)
elif formalIdx != nil:
# we already have a formal type, make sure the provided index fits
let r = fitNode(c, formalIdx, idx, idx.info)
if r.kind == nkError: (r, Zero)
else: (r, getOrdValue(r))
elif isOrdinalType(idx.typ):
# no formal type is set yet and we got a valid ordinal
(idx, getOrdValue(idx))
else:
# a constant expression, but not a value usable as an ordinal
(c.config.newError(idx, PAstDiag(kind: adSemExpectedOrdinalArrayIdx,
nonOrdInput: idx,
indexExpr: n)),
Zero)
else:
(c.graph.emptyNode, Zero)
proc semArrayConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
result = newNodeI(nkBracket, n.info)
result.typ = newTypeS(tyArray, c)
template createRange(first, last: Int128, typ: PType): PType =
makeRangeType(c, toInt64(first), toInt64(last), n.info, typ)
if n.len == 0:
# an empty array constructor
let indexType = getSysType(c.graph, n.info, tyInt)
rawAddSon(result.typ, createRange(Zero, toInt128(-1), indexType))
rawAddSon(result.typ, newTypeS(tyEmpty, c)) # needs an empty basetype!
else:
# analyse the first element separately -- the other elements depend on its
# type
let
(first, firstIndex) = semArrayElementIndex(c, n[0], nil)
indexType =
# if no type is provided or an error occurred, default to ``int`` for
# the index type
case first.kind
of nkError, nkEmpty: getSysType(c.graph, n.info, tyInt)
else: first.typ
# before doing anything else, check if the size of the array doesn't exceed
# the maximum value representable by the index type
let limit = lastOrd(c.config, indexType)
if firstIndex + toInt128(n.len - 1) > limit:
result = c.config.newError(n,
PAstDiag(
kind: adSemIndexOutOfBounds,
ordRange: createRange(firstIndex, limit, indexType),
outOfBoundsIdx: toInt(firstIndex) + n.len - 1))
return
var
typ = PType(nil) ## the common type of all elements
lastIndex = firstIndex - 1 ## tracks the index of the previous element
result.sons.setLen(n.len)
for i, it in n.pairs:
# first, analyse the index expression (if one exist)
var (idx, val) =
if i == 0: (first, firstIndex)
else: semArrayElementIndex(c, it, indexType)
if idx.kind notin {nkError, nkEmpty} and val != lastIndex + 1:
# the specified index value doesn't match with the expected one
idx = c.config.newError(idx,
PAstDiag(kind: adSemInvalidOrderInArrayConstructor))
# always analyze the expression, even when the index expression is
# erroneous
var e = semExprWithType(c, it.skipColon, {})
e = exprNotGenericRoutine(c, e)
if typ.isNil:
# must be the first item; initialize the common type:
typ = e.typ
elif {e.typ.kind, typ.kind} == {tyObject} and typ != e.typ:
# XXX: the check is meant to disallow implicit up- or down-conversion
# of object types in an array constructor, but it is incorrect:
# ``sink``, aliases, and generic instance types are not skipped
# and the comparision doesn't use ``sameType``.
# The ``lang_types/array/tarray.nim`` test depends on this
# behaviour, and whether the semantics are really what is wanted
# is also not clear, so the incorrect behaviour is kept for now
e = typeMismatch(c.config, e.info, typ, e.typ, e)
else:
# in the case that the types are not compatible, no error is produced
# yet
typ = commonType(c, typ, e.typ)
if it.kind == nkExprColonExpr:
result[i] = newTreeI(nkExprColonExpr, it.info, [idx, e])
else:
result[i] = e
inc lastIndex
# watch out for ``sink T``!
# XXX: things would be easier if ``sink T`` only exists for the operands
# of a ``tyProc``
typ = typ.skipTypes({tySink})
# finish the array type:
rawAddSon(result.typ, createRange(firstIndex, lastIndex, indexType))
addSonSkipIntLit(result.typ, typ, c.idgen)
var hasError = false
# fit all elements to be of the derived common type
for it in result.sons.mitems:
if it.kind == nkExprColonExpr:
it[1] = fitNode(c, typ, it[1], it[1].info)
hasError = hasError or nkError in {it[0].kind, it[1].kind}
else:
it = fitNode(c, typ, it, it.info)
hasError = hasError or it.kind == nkError
if hasError:
result = c.config.wrapError(result)
proc fitArgTypesPostMatch(c: PContext, n: PNode): PNode =
## Takes the production AST of a call and performs the post-match type
## fitting on the argument nodes. Errors are passed through.
addInNimDebugUtils(c.config, "fitArgTypesPostMatch", n, result)
result = n # `n` is already a production
case n.kind
of nkCallKinds:
# fit all argument expressions
var hasError = false
for i in 1..<n.len:
result[i] = fitNodePostMatch(c, n[i])
hasError = hasError or result[i].isError
if hasError:
result = c.config.wrapError(result)
of nkError:
discard # already set above
else:
unreachable(n.kind)
proc isAssignable(c: PContext, n: PNode; isUnsafeAddr=false): TAssignableResult =
result = parampatterns.isAssignable(c.p.owner, n, isUnsafeAddr)
proc hasUnresolvedArgs(c: PContext, n: PNode): bool =
# Checks whether an expression depends on generic parameters that
# don't have bound values yet. E.g. this could happen in situations
# such as:
# type Slot[T] = array[T.size, byte]
# proc foo[T](x: default(T))
#
# Both static parameter and type parameters can be unresolved.
case n.kind
of nkSym:
return isUnresolvedSym(n.sym)
of nkIdent, nkAccQuoted:
let (ident, err) = considerQuotedIdent(c, n)
if err != nil:
localReport(c.config, err)
var amb = false
let sym = searchInScopes(c, ident, amb)
if sym != nil:
return isUnresolvedSym(sym)
else:
return false
of nkError:
return false
else:
for i in 0..<n.safeLen:
if hasUnresolvedArgs(c, n[i]): return true
return false
proc newHiddenAddrTaken(c: PContext, n: PNode): PNode =
## Wraps the expression `n` in an ``nkHiddenAddr`` and assigns a ``var`` type
## derived from the source type to the resulting expression
result = newTreeIT(nkHiddenAddr, n.info, makeVarType(c, n.typ)): n
func isVarParam(t: PType): bool =
# watch out: ``typeDesc[var int]`` is **not** a 'var' parameter
t.skipTypes(abstractInst - {tyTypeDesc}).kind == tyVar
proc analyseIfAddressTaken(n: PNode) =
## Analyses if taking the address of lvalue expression `n` counts as an
## "address taken" and if it does, marks the symbol of the underlying
## location (if it's associated with a symbol) with the ``sfAddrTaken``
## flag.
## XXX: the exact meaning of "address taken" is rather fuzzy at the moment,
## and the whole thing is likely not a good idea in the first place.
## For locations, only ``guards.nim`` makes use of ``sfAddrTaken``.
## There, the presense of the flag is used to decide whether a fact
## cannot change through an undetectable indirect mutation.
##
## XXX: including the flag here has the additional issue of letting AST that
## might turn out erroneous (e.g. ``(var i = 0; discard addr(i); error)``)
## modify a symbol. Performing this analysis in ``sempass2`` would be the
## better approach
##
var
n {.cursor.} = n
hadBracket = false
while true:
case n.kind
of nkConv:
# skip lvalue conversion. We know that all conversions we encounter here
# are lvalue conversions because the input `n` is an lvalue. In addition
# we explicitly only consider ``nkConv`` (and not ``nkHiddenSubConv``,
# etc.), as it's the only conversion operator used for lvalue conversion
# (ignoring things like ``openArray`` conversion, but we're not
# interested in those)
n = n[1]
of nkBracketExpr:
# skip a single array-like access operation
# XXX: the previous implementation did the same, but the "why" is not
# clear. The underlying issue is that the meaning of ``sfAddrTaken``
# for locals and globals is not really specified
if hadBracket:
break
else:
n = n[0]
hadBracket = true
of nkStmtListExpr:
n = n.lastSon
else:
break
if n.kind == nkSym:
incl(n.sym.flags, sfAddrTaken)
proc passToVarParameter(c: PContext, n: PNode): PNode =
## Returns `n` wrapped in an ``nkHiddenAddr`` node and marks the symbol of
## the underlying location, if one exists, with ``sfAddrTaken``.
if n.typ.kind != tyVar:
analyseIfAddressTaken(n)
newHiddenAddrTaken(c, n)
else:
# only allowed when trying a concept
c.config.internalAssert(c.matchedConcept != nil, n.info)
n
proc analyseIfAddressTakenInCall(n: PNode) =
## Performs the "is address taken" analysis for all immediate arguments of
## the call `n`. The input AST is, except for the symbols that get marked
## with ``sfAddrTaken``, not mutated
for i in 1..<n.len:
if n[i].kind == nkHiddenAddr:
analyseIfAddressTaken(n[i][0])
proc fixVarArgumentsAndAnalyse(c: PContext, n: PNode): PNode =
## Introduces an ``nkHiddenAddr`` node for each immediate argument part of
## the call expression `n` that is passed to a 'var' parameter. Only the
## immediate. The fixup has to be applied *after* overload is done and the
## arguments are otherwise sem-checked already.
##
## Also performs the "is address taken" analysis for each argument passed to
## a ``var`` parameter.
##
## Note that not all ``var`` parameters are considered, certain magics are
## ignored during this fixup
addInNimDebugUtils(c.config, "fixVarArgumentsAndAnalyse", n, result)
if n.isError:
return n
func skipDeref(n: PNode): PNode =
if n.kind == nkHiddenDeref: n[0]
else: n
result = n
# get the real type of the callee
# it may be a proc var with a generic alias type, so we skip over them
let
t = n[0].typ.skipTypes({tyGenericInst, tyAlias, tySink})
magic = getMagic(n)
var hasError = false
if magic == mNewSeq:
# XXX: this check doesn't really fit here. ``magicsAfterOverloadResolution``
# would be a better place for it
# bug #5113: disallow newSeq(result) where result is a 'var T':
let arg = skipDeref(n[1])