@@ -70,8 +70,8 @@ proc semConstrField(c: PContext, flags: TExprFlags,
70
70
var initValue = semExprFlagDispatched (c, assignment[1 ], flags)
71
71
if initValue != nil :
72
72
initValue = fitNode (c, field.typ, initValue, assignment.info)
73
- assignment.sons [0 ] = newSymNode (field)
74
- assignment.sons [1 ] = initValue
73
+ assignment[0 ] = newSymNode (field)
74
+ assignment[1 ] = initValue
75
75
assignment.flags.incl nfSem
76
76
return initValue
77
77
@@ -88,7 +88,7 @@ proc allPossibleValues(c: PContext, t: PType): IntSet =
88
88
result = initIntSet ()
89
89
if t.enumHasHoles:
90
90
let t = t.skipTypes (abstractRange)
91
- for field in t.n.sons :
91
+ for field in t.n:
92
92
result .incl (field.sym.position)
93
93
else :
94
94
for i in toInt64 (firstOrd (c.config, t)) .. toInt64 (lastOrd (c.config, t)):
@@ -100,13 +100,13 @@ proc branchVals(c: PContext, caseNode: PNode, caseIdx: int,
100
100
result = initIntSet ()
101
101
processBranchVals (caseNode[caseIdx], incl)
102
102
else :
103
- result = allPossibleValues (c, caseNode.sons [0 ].typ)
103
+ result = allPossibleValues (c, caseNode[0 ].typ)
104
104
for i in 1 .. caseNode.len- 2 :
105
105
processBranchVals (caseNode[i], excl)
106
106
107
107
proc rangeTypVals (rangeTyp: PType ): IntSet =
108
108
assert rangeTyp.kind == tyRange
109
- let (a, b) = (rangeTyp.n.sons [0 ].intVal, rangeTyp.n.sons [1 ].intVal)
109
+ let (a, b) = (rangeTyp.n[0 ].intVal, rangeTyp.n[1 ].intVal)
110
110
result = initIntSet ()
111
111
for it in a .. b:
112
112
result .incl (it.int )
@@ -118,8 +118,8 @@ proc formatUnsafeBranchVals(t: PType, diffVals: IntSet): string =
118
118
if t.kind in {tyEnum, tyBool}:
119
119
var i = 0
120
120
for val in diffVals:
121
- while t.n.sons [i].sym.position < val: inc (i)
122
- strs.add (t.n.sons [i].sym.name.s)
121
+ while t.n[i].sym.position < val: inc (i)
122
+ strs.add (t.n[i].sym.name.s)
123
123
else :
124
124
for val in diffVals:
125
125
strs.add ($ val)
@@ -194,13 +194,11 @@ proc checkForMissingFields(c: PContext, recList, initExpr: PNode, considerDefaul
194
194
localError (c.config, initExpr.info, " fields not initialized: $1." , [missing])
195
195
196
196
proc semConstructFields (c: PContext , recNode: PNode ,
197
- initExpr: PNode , flags: TExprFlags , wantsInit = true ): InitStatus =
198
- result = initUnknown
199
-
197
+ initExpr: PNode , flags: TExprFlags ): InitStatus =
200
198
case recNode.kind
201
199
of nkRecList:
202
200
for field in recNode:
203
- let status = semConstructFields (c, field, initExpr, flags, wantsInit )
201
+ let status = semConstructFields (c, field, initExpr, flags)
204
202
mergeInitStatus (result , status)
205
203
206
204
of nkRecCase:
@@ -209,18 +207,18 @@ proc semConstructFields(c: PContext, recNode: PNode,
209
207
let fields = branch[branch.len - 1 ]
210
208
fieldsPresentInInitExpr (c, fields, initExpr)
211
209
212
- template checkMissingFields (branchNode: PNode , considerDefaults: bool ) =
210
+ proc checkMissingFields (branchNode: PNode , considerDefaults: bool ) =
213
211
if branchNode != nil :
214
212
let fields = branchNode[^ 1 ]
215
213
checkForMissingFields (c, fields, initExpr, considerDefaults)
216
214
217
- let discriminator = recNode.sons [0 ]
215
+ let discriminator = recNode[0 ]
218
216
internalAssert c.config, discriminator.kind == nkSym
219
217
var selectedBranch = - 1
220
218
221
219
for i in 1 ..< recNode.len:
222
220
let innerRecords = recNode[i][^ 1 ]
223
- let status = semConstructFields (c, innerRecords, initExpr, flags, selectedBranch == - 1 )
221
+ let status = semConstructFields (c, innerRecords, initExpr, flags)
224
222
if status notin {initNone, initUnknown}:
225
223
mergeInitStatus (result , status)
226
224
if selectedBranch != - 1 :
@@ -255,7 +253,7 @@ proc semConstructFields(c: PContext, recNode: PNode,
255
253
localError (c.config, discriminatorVal.info, (" possible values " &
256
254
" $2are in conflict with discriminator values for " &
257
255
" selected object branch $1." ) % [$ selectedBranch,
258
- formatUnsafeBranchVals (recNode.sons [0 ].typ, valsDiff)])
256
+ formatUnsafeBranchVals (recNode[0 ].typ, valsDiff)])
259
257
260
258
let branchNode = recNode[selectedBranch]
261
259
let flags = flags* {efAllowDestructor} + {efPreferStatic,
@@ -270,7 +268,7 @@ proc semConstructFields(c: PContext, recNode: PNode,
270
268
if discriminatorVal.kind notin nkLiterals and (
271
269
not isOrdinalType (discriminatorVal.typ, true ) or
272
270
lengthOrd (c.config, discriminatorVal.typ) > MaxSetElements or
273
- lengthOrd (c.config, recNode.sons [0 ].typ) > MaxSetElements ):
271
+ lengthOrd (c.config, recNode[0 ].typ) > MaxSetElements ):
274
272
localError (c.config, discriminatorVal.info,
275
273
" branch initialization with a runtime discriminator only " &
276
274
" supports ordinal types with 2^16 elements or less." )
@@ -348,33 +346,28 @@ proc semConstructFields(c: PContext, recNode: PNode,
348
346
checkMissingFields matchedBranch, true
349
347
else :
350
348
result = initPartial
349
+ discriminatorVal = discriminatorVal.skipHidden
350
+ debug discriminatorVal
351
351
if discriminatorVal.kind == nkIntLit: # or discriminatorVal.typ.kind == tyRange:
352
352
# When the discriminator is a compile-time value, we also know
353
353
# which branch will be selected:
354
354
matchedBranch = recNode.pickCaseBranch discriminatorVal
355
355
if matchedBranch != nil : checkMissingFields matchedBranch, true
356
+ elif discriminatorVal.typ.kind == tyRange:
357
+ let matchedBranches = recNode.pickCaseBranches discriminatorVal.typ.n
358
+ for m in matchedBranches:
359
+ checkMissingFields m, matchedBranches.len == 0
356
360
else :
357
- discriminatorVal = discriminatorVal.skipHidden
358
- if discriminatorVal.typ.kind == tyRange and not ( # TODO : Maybe hint or warn when the type is too big to select the branch
359
- discriminatorVal.kind notin nkLiterals and (
360
- not isOrdinalType (discriminatorVal.typ, true ) or
361
- lengthOrd (c.config, discriminatorVal.typ) > MaxSetElements or
362
- lengthOrd (c.config, recNode.sons[0 ].typ) > MaxSetElements )
363
- ):
364
- let matchedBranches = recNode.pickCaseBranches discriminatorVal.typ.n
365
- for m in matchedBranches:
366
- checkMissingFields m, matchedBranches.len == 0
367
- else :
368
- # All bets are off. If any of the branches has a mandatory
369
- # fields we must produce an error:
370
- for i in 1 ..< recNode.len: checkMissingFields recNode[i], false
361
+ # All bets are off. If any of the branches has a mandatory
362
+ # fields we must produce an error:
363
+ for i in 1 ..< recNode.len: checkMissingFields recNode[i], false
371
364
372
365
of nkSym:
373
366
let field = recNode.sym
374
- var e = semConstrField (c, flags, field, initExpr)
375
- if wantsInit and e == nil : # Try to use default value
376
- e = field.ast
377
- result = if e != nil : initFull else : initNone
367
+ let e = semConstrField (c, flags, field, initExpr)
368
+ result = if e != nil : initFull
369
+ elif field.ast != nil : initUnknown
370
+ else : initNone
378
371
379
372
else :
380
373
internalAssert c.config, false
@@ -388,12 +381,12 @@ proc semConstructType(c: PContext, initExpr: PNode,
388
381
mergeInitStatus (result , status)
389
382
if status in {initPartial, initNone, initUnknown}:
390
383
checkForMissingFields c, t.n, initExpr, true
391
- let base = t.sons [0 ]
384
+ let base = t[0 ]
392
385
if base == nil : break
393
386
t = skipTypes (base, skipPtrs)
394
387
395
388
proc semObjConstr (c: PContext , n: PNode , flags: TExprFlags ): PNode =
396
- var t = semTypeNode (c, n.sons [0 ], nil )
389
+ var t = semTypeNode (c, n[0 ], nil )
397
390
result = newNodeIT (nkObjConstr, n.info, t)
398
391
for child in n: result .add child
399
392
@@ -403,7 +396,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
403
396
404
397
t = skipTypes (t, {tyGenericInst, tyAlias, tySink, tyOwned})
405
398
if t.kind == tyRef:
406
- t = skipTypes (t.sons [0 ], {tyGenericInst, tyAlias, tySink, tyOwned})
399
+ t = skipTypes (t[0 ], {tyGenericInst, tyAlias, tySink, tyOwned})
407
400
if optOwnedRefs in c.config.globalOptions:
408
401
result .typ = makeVarType (c, result .typ, tyOwned)
409
402
# we have to watch out, there are also 'owned proc' types that can be used
0 commit comments