-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAnnotator.zu
763 lines (686 loc) · 24.7 KB
/
Annotator.zu
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
#
# The Zimbu compiler written in Zimbu
#
# Annotator module: Goes through the code and adds annotations.
#
# Copyright 2013 Bram Moolenaar All Rights Reserved.
# Licensed under the Apache License, Version 2.0. See the LICENSE file or
# obtain a copy at: http://www.apache.org/licenses/LICENSE-2.0
#
IMPORT.PROTO "zui.proto"
IMPORT "ClassType.zu"
IMPORT "CodeProperties.zu"
IMPORT "Conversion.zu"
IMPORT "Declaration.zu"
IMPORT "FileScope.zu"
IMPORT "Generate.zu"
IMPORT "MethodScope.zu"
IMPORT "MethodType.zu"
IMPORT "ModuleScope.zu"
IMPORT "NoAllocType.zu"
IMPORT "Output.zu"
IMPORT "PieceScope.zu"
IMPORT "Resolve.zu"
IMPORT "SContext.zu"
IMPORT "Scope.zu"
IMPORT "TopScope.zu"
IMPORT "Type.zu"
IMPORT "TupleType.zu"
IMPORT "UsedFile.zu"
IMPORT "ZimbuFile.zu"
IMPORT "ZuiDeclarationExt.zu"
IMPORT "ZuiExpressionExt.zu"
IMPORT "ZuiMethodCallExt.zu"
IMPORT "ZuiMethodTypeExt.zu"
IMPORT "ZuiStatementExt.zu"
IMPORT "ZuiForStatementExt.zu"
IMPORT "genC/WriteC.zu"
MODULE Annotator
Output.Group %outs = NEW()
# Go through the code, recursing into imported files.
# Figure out properties of statements and expressions.
# The properties are stored with the statement/expression, to be used when
# generating code.
# Also reserves temporary variables where needed.
PROC annotate(UsedFile usedFile, TopScope topScope, WriteC gen) @public
# Annotate the main file.
getScopeProps(topScope, usedFile.scope(), gen)
# Annotate the imported files.
FOR zimbuFile IN topScope.usedImports
getScopeProps(topScope, zimbuFile.fileScope, gen)
}
# Annotate the builtins modules.
FOR builtin IN topScope.usedBuiltins
IF builtin != NIL && builtin.usedFile != NIL
getScopeProps(topScope, builtin.usedFile.scope(), gen)
}
}
}
PROC getScopeProps(TopScope topScope, FileScope scope, WriteC gen)
LOG.info("Annotating \(scope.zimbuFile.filename)")
SContext ctx = NEW(topScope, scope, gen, %outs)
getProps(scope.statements, ctx)
}
PROC getProps(list<Zui.Statement> statements, SContext ctx)
Zui.Statement saveStmt = ctx.stmt
FOR stmt IN statements
ctx.stmt = stmt
ZuiStatementExt.get(stmt).props = getProps(stmt, ctx)
releaseTemps(stmt)
}
ctx.stmt = saveStmt
}
# Release the temp variables.
PROC releaseTemps(Zui.Statement stmt)
FOR decl IN ZuiStatementExt.get(stmt).tempDecl
decl.temp = FALSE
}
}
# Get properties for |stmt|. Only methodCall matters.
# Inspect expressions and nested statements.
FUNC getProps(Zui.Statement stmt, SContext ctx) CodeProperties
CodeProperties props
SWITCH stmt.getType()
CASE Zui.StatementType.eALIAS_DECL
CASE Zui.StatementType.eTYPE_DECL
CASE Zui.StatementType.eCONTINUE
CASE Zui.StatementType.eBREAK
CASE Zui.StatementType.eCBLOCK
CASE Zui.StatementType.eBITS_DECL
CASE Zui.StatementType.eENUM_DECL
BREAK # no expression or nested statement
CASE Zui.StatementType.eASSIGN
Zui.Assignment ass = stmt.getAssign()
Zui.Expression lhs = ass.getLhs()
Zui.Expression rhs = ass.getRhs()
IF lhs.getType() == Zui.ExprType.eLIST
addTemp(rhs, ZuiExpressionExt.get(rhs).resultType?.getTtype()
== Type.Enum.multiple, ctx)
IF rhs.getType() == Zui.ExprType.eLIST
# Not going to create an actual list, only handle each item
# separately.
FOR itemExpr IN rhs.getListItemList()
IF getProps(itemExpr, ctx).methodCall
props.methodCall = TRUE
}
}
ELSE
props = getProps(rhs, ctx)
}
FOR e IN lhs.getListItemList()
props = props | getProps(e, ctx)
}
ELSE
props = getProps(lhs, ctx) | getProps(rhs, ctx)
IF ass.hasType() && ass.getType() == Zui.ExprType.eCONCAT
props.allocMem = TRUE
ELSEIF rhs.getType() == Zui.ExprType.eNIL
# Assigning NIL to a not allocated object may call Finish().
# Otherwise setting the allocMem flag won't hurt.
props.allocMem = TRUE
ELSE
Type.Enum rightTtype =
ZuiExpressionExt.get(rhs).resultType?.getTtype()
IF (rightTtype == Type.Enum.object
|| rightTtype == Type.Enum.iobject)
&& rhs.getType() != Zui.ExprType.eNIL
# Might require conversion using Zao() or allocating Pos.
props.allocMem = TRUE
}
}
}
CASE Zui.StatementType.eCALL
CASE Zui.StatementType.eNEWCALL
props = getProps(stmt.getMethodCall(), ctx)
CASE Zui.StatementType.eVAR_DECL
Zui.Declaration zuiDecl = stmt.getDeclaration()
IF zuiDecl.hasInit()
Zui.Expression init = zuiDecl.getInit()
props = getProps(init, ctx)
Type.Enum initTtype =
ZuiExpressionExt.get(init).resultType?.getTtype()
IF initTtype == Type.Enum.object || initTtype == Type.Enum.iobject
# Might require conversion using Zao() or allocating Pos.
props.allocMem = TRUE
}
}
CASE Zui.StatementType.eINC
CASE Zui.StatementType.eDEC
CASE Zui.StatementType.eRETURN
CASE Zui.StatementType.eEXIT
CASE Zui.StatementType.eTHROW
CASE Zui.StatementType.eDEFER
CASE Zui.StatementType.eGENERATEERROR
IF stmt.hasExpr()
props = getProps(stmt.getExpr(), ctx)
}
CASE Zui.StatementType.eMODULE_DECL
Zui.Declaration zuiDecl = stmt.getDeclaration()
Zui.ModuleType module = zuiDecl.getType().getModuleDecl()
getProps(module.getStatementList(), ctx)
CASE Zui.StatementType.eCLASS_DECL
Zui.Declaration zuiDecl = stmt.getDeclaration()
Zui.ClassType class = zuiDecl.getType().getClassDecl()
# A templated class itself is not generated.
IF class.hasTemplateTypeName()
ClassType classType = ZuiStatementExt.get(stmt).typeObj
FOR template IN classType.templateList
Zui.ClassType tclass = template.stmt.getDeclaration()
.getType().getClassDecl()
getProps(tclass, ctx)
}
ELSE
getProps(class, ctx)
}
CASE Zui.StatementType.eINCLUDE
FOR incStmt IN stmt.getInclude().getStatementList()
IF incStmt.getType() == Zui.StatementType.eVAR_DECL
# For every delcaration a copy of the included PIECE or CLASS is
# made and needs to be annotated.
Zui.Declaration zuiDecl = incStmt.getDeclaration()
VAR declExt = ZuiDeclarationExt.get(zuiDecl)
PieceScope pieceScope = declExt.pieceScope
IF pieceScope != NIL
getProps(pieceScope.statements, ctx)
IF pieceScope.sharedBlock != NIL
getProps(pieceScope.sharedBlock.getStatementList(), ctx)
}
}
}
}
CASE Zui.StatementType.eMETHOD_DECL
Zui.Declaration zuiDecl = stmt.getDeclaration()
Zui.MethodType method = zuiDecl.getType().getMethodDecl()
IF method.hasTemplateTypeName()
MethodType methodType = ZuiDeclarationExt.get(zuiDecl).decl?.type
FOR template IN methodType?.templateList
Zui.MethodType tmethod = template.zuiDecl
.getType().getMethodDecl()
props = getProps(tmethod, ctx)
}
ELSE
props = getProps(method, ctx)
}
CASE Zui.StatementType.eBLOCK
CASE Zui.StatementType.eCASE
CASE Zui.StatementType.eDEFAULT
CASE Zui.StatementType.eTRYELSE
IF stmt.hasBlock()
getProps(stmt.getBlock().getStatementList(), ctx)
}
CASE Zui.StatementType.eGENERATEIF
CASE Zui.StatementType.eIF
CASE Zui.StatementType.eIFNIL
CASE Zui.StatementType.eWHILE
CASE Zui.StatementType.eDO
CASE Zui.StatementType.eUNTIL
CASE Zui.StatementType.eSWITCH
Zui.Condition cond = stmt.getCondition()
IF cond.hasCond()
props = getProps(cond.getCond(), ctx)
}
releaseTemps(stmt)
IF cond.hasBlock() && cond.getBlock().hasStatement()
getProps(cond.getBlock().getStatementList(), ctx)
}
IF cond.hasElseif()
FOR c IN cond.getElseifList()
props = props | getProps(c.getCond(), ctx)
releaseTemps(stmt)
IF c.getBlock().hasStatement()
getProps(c.getBlock().getStatementList(), ctx)
}
}
}
IF cond.hasElse()
getProps(cond.getElse().getStatementList(), ctx)
}
CASE Zui.StatementType.eFOR
Zui.ForStatement for = stmt.getFor()
FOR iter IN for.getIterList()
props = props | getProps(iter, ctx)
}
IF for.hasTo()
props = props | getProps(for.getTo(), ctx)
}
IF for.hasUntil()
props = props | getProps(for.getUntil(), ctx)
}
releaseTemps(stmt)
IF for.hasBody()
getProps(for.getBody().getStatementList(), ctx)
}
IF ZuiForStatementExt.get(for).info.multiExpr != NIL
# Need a temp variable to store the result of a KeyIterator.
addTemp(ZuiForStatementExt.get(for).info.multiExpr, TRUE, ctx)
}
CASE Zui.StatementType.eTRY
Zui.TryStatement try = stmt.getTry()
IF try.hasBody()
getProps(try.getBody().getStatementList(), ctx)
}
IF try.hasCatch()
FOR catch IN try.getCatchList()
IF catch.hasBody()
getProps(catch.getBody().getStatementList(), ctx)
}
}
}
IF try.hasFinally()
getProps(try.getFinally().getStatementList(), ctx)
}
BREAK
DEFAULT
ctx.error("INTERNAL: Annotator.getProps(): Statement type not handled: "
.. stmt.getType().ToString(), ctx.zcPos(stmt.getPos()))
}
RETURN props
}
# Return TRUE if |expr| has a conversion that allocates memory.
FUNC exprConversionAlloc(Zui.Expression expr) bool
STATIC set<Conversion> %allocatingConversions = [
Conversion.object2iobject,
Conversion.iobject2object,
Conversion.string2varString,
Conversion.varString2string,
Conversion.int2string,
Conversion.nat2string,
Conversion.float2string,
Conversion.bool2dyn,
Conversion.status2dyn,
Conversion.int2dyn,
Conversion.int82dyn,
Conversion.int162dyn,
Conversion.int322dyn,
Conversion.nat2dyn,
Conversion.byte2dyn,
Conversion.nat162dyn,
Conversion.nat322dyn,
Conversion.float2dyn,
Conversion.enum2dyn,
Conversion.bits2dyn,
Conversion.tuple2dyn,
Conversion.type2dyn,
Conversion.object2dyn,
Conversion.iobject2dyn,
Conversion.string2dyn,
Conversion.byteString2dyn,
Conversion.array2dyn,
Conversion.list2dyn,
Conversion.dict2dyn,
Conversion.dyn2bool,
Conversion.dyn2status,
Conversion.dyn2int,
Conversion.dyn2int8,
Conversion.dyn2int16,
Conversion.dyn2int32,
Conversion.dyn2nat,
Conversion.dyn2byte,
Conversion.dyn2nat16,
Conversion.dyn2nat32,
Conversion.dyn2float,
Conversion.dyn2enum,
Conversion.dyn2tuple,
Conversion.dyn2type,
Conversion.dyn2object,
Conversion.dyn2iobject,
Conversion.dyn2string,
Conversion.dyn2byteString,
Conversion.dyn2array,
Conversion.dyn2list,
Conversion.dyn2dict,
]
RETURN %allocatingConversions.has(ZuiExpressionExt.get(expr).conversion)
}
FUNC getProps(Zui.MethodType method, SContext ctx) CodeProperties
VAR methodExt = ZuiMethodTypeExt.get(method)
IF method.hasBody() && methodExt.scope != NIL
Scope saveScope = ctx.scope
ctx.scope = methodExt.scope
getProps(method.getBody().getStatementList(), ctx)
ctx.scope = saveScope
}
RETURN 0
}
FUNC getProps(Zui.ClassType class, SContext ctx) CodeProperties
Scope saveScope = ctx.scope
ctx.scope = NIL
getProps(class.getMemberList(), ctx)
IF class.hasShared()
getProps(class.getShared().getStatementList(), ctx)
}
ctx.scope = saveScope
RETURN 0
}
# Method call, either as a statement or part of an expression.
FUNC getProps(Zui.MethodCall call, SContext ctx) CodeProperties
IF call.hasName()
getProps(call.getName(), ctx)
}
list<CodeProperties> %propList = NEW()
int methodCall
FOR arg IN call.getArgumentList()
CodeProperties props = getProps(arg, ctx)
IF props.methodCall
++methodCall
}
%propList.add(props)
}
IF methodCall > 0 && call.sizeArgument() >= 2
# A temp var is needed when the argument is in allocated memory and
# another argument has a method call. The GC needs to be able to find
# that allocated memory when it's run in that method call.
int i
FOR arg IN call.getArgumentList()
CodeProperties props = %propList[i]
IF props.inAllocMem && (methodCall > 1 || !props.methodCall)
addTemp(arg, ctx)
}
++i
}
}
ZuiMethodCallExt ext = ZuiMethodCallExt.get(call)
IF ext.firstVararg > 0
# Need a temp var for the varargs argument. The type is a tuple on the
# stack.
VAR arg = call.newVararg()
arg.setType(Zui.ExprType.eID)
arg.setPos(call.getPos())
# Use the argument type of the called method.
IF ext.method != NIL
Type tupleType = ext.method.varargsTuple
addTemp(arg, NoAllocType.NEW(tupleType, "vararg"), TRUE, ctx)
}
}
CodeProperties ret
ret.methodCall = TRUE
RETURN ret
}
FUNC getProps(Zui.Expression expr, SContext ctx) CodeProperties @public
CodeProperties props
CodeProperties propsLeft
CodeProperties propsRight
VAR exprExt = ZuiExpressionExt.get(expr)
SWITCH expr.getType()
CASE Zui.ExprType.eTYPESPEC
CASE Zui.ExprType.eCCODE
# No expression below.
BREAK
CASE Zui.ExprType.eNOT
CASE Zui.ExprType.eMINUS
CASE Zui.ExprType.eTILDE
CASE Zui.ExprType.ePRE_INC
CASE Zui.ExprType.ePRE_DEC
CASE Zui.ExprType.ePOST_INC
CASE Zui.ExprType.ePOST_DEC
CASE Zui.ExprType.ePARENS
# Unary operator. Never use a temp value.
props = getProps(expr.getRight(), ctx)
CASE Zui.ExprType.eEQUAL
CASE Zui.ExprType.eNOTEQUAL
CASE Zui.ExprType.eIS
CASE Zui.ExprType.eISNOT
CASE Zui.ExprType.eISA
CASE Zui.ExprType.eISNOTA
CASE Zui.ExprType.eGREATER
CASE Zui.ExprType.eGREATER_EQUAL
CASE Zui.ExprType.eLESS
CASE Zui.ExprType.eLESS_EQUAL
CASE Zui.ExprType.eMATCH
CASE Zui.ExprType.eNOMATCH
CASE Zui.ExprType.eCONCAT
CASE Zui.ExprType.eSUBSCRIPT
# Binary operator where left and right may need a temp value.
propsLeft = getProps(expr.getLeft(), ctx)
propsRight = getProps(expr.getRight(), ctx)
IF propsLeft.methodCall && propsRight.inAllocMem
# func() == expr -> func() == (t = expr)
addTemp(expr.getRight(), ctx)
}
IF propsRight.methodCall && propsLeft.inAllocMem
# expr == func() -> (t = expr) == func()
addTemp(expr.getLeft(), ctx)
}
props.methodCall = propsLeft.methodCall || propsRight.methodCall
props.inAllocMem = exprExt.resultType?.isManaged()
props.allocMem = propsLeft.allocMem || propsRight.allocMem
# String concatenation uses a function, unless it's a constant.
IF expr.getType() == Zui.ExprType.eCONCAT
IF Generate.resultType(expr, ctx) == Type.Enum.stringval
# Constant is not allocated.
props = 0
ELSE
props.methodCall = TRUE
}
}
# Subscript may throw an exception, thus allocate memory.
IF expr.getType() == Zui.ExprType.eSUBSCRIPT
props.allocMem = TRUE
}
CASE Zui.ExprType.eMEMBER
CASE Zui.ExprType.eBIT_AND
CASE Zui.ExprType.eBIT_OR
CASE Zui.ExprType.eBIT_XOR
CASE Zui.ExprType.eMULTIPLY
CASE Zui.ExprType.eDIVIDE
CASE Zui.ExprType.eREMAINDER
CASE Zui.ExprType.eSHIFT_RIGHT
CASE Zui.ExprType.eSHIFT_LEFT
CASE Zui.ExprType.eSUBTRACT
CASE Zui.ExprType.eADD
CASE Zui.ExprType.eIFNIL
CASE Zui.ExprType.eAND
CASE Zui.ExprType.eOR
CASE Zui.ExprType.eEXPRMETHOD
# Binary operator, no temp values.
propsLeft = getProps(expr.getLeft(), ctx)
propsRight = getProps(expr.getRight(), ctx)
props.methodCall = propsLeft.methodCall || propsRight.methodCall
props.inAllocMem = exprExt.resultType?.isManaged()
props.allocMem = propsLeft.allocMem || propsRight.allocMem
CASE Zui.ExprType.eTERNARY
# cond ? expr : expr, no temp values
props = getProps(expr.getCond(), ctx)
propsLeft = getProps(expr.getLeft(), ctx)
propsRight = getProps(expr.getRight(), ctx)
props.methodCall = props.methodCall || propsLeft.methodCall
|| propsRight.methodCall
props.inAllocMem = exprExt.resultType?.isManaged()
props.allocMem = propsLeft.allocMem || propsRight.allocMem
CASE Zui.ExprType.eCALL
props = getProps(expr.getMethodCall(), ctx)
props.inAllocMem = exprExt.resultType?.isManaged()
CASE Zui.ExprType.eNEWCALL
props = getProps(expr.getMethodCall(), ctx)
props.allocMem = TRUE
props.inAllocMem = TRUE
CASE Zui.ExprType.eTYPECAST
props = getProps(expr.getLeft(), ctx)
CASE Zui.ExprType.eID
CASE Zui.ExprType.eREF
props.inAllocMem = exprExt.resultType?.isManaged()
CASE Zui.ExprType.eSTRING
# String contstants are in static memory.
BREAK
CASE Zui.ExprType.eLIST
# When there are items, need a temp variable to store the pointer to
# the list.
IF expr.sizeListItem() > 0
# Need a temp variable to hold the list pointer. Unless it's
# resulting in a tuple.
IF exprExt.resultType?.ttype != Type.Enum.tuple
addTemp(expr, ctx)
}
# Go over the list values.
FOR itemExpr IN expr.getListItemList()
propsLeft = getProps(itemExpr, ctx)
IF propsLeft.methodCall
props.methodCall = TRUE
}
}
}
props.allocMem = TRUE
props.inAllocMem = TRUE
CASE Zui.ExprType.eDICT
# When there are items, need a temp variable to store the pointer to
# the list.
IF expr.sizeDictItem() > 0
# Need a temp variable to hold the list pointer.
addTemp(expr, ctx)
# Go over the dict keys and values.
FOR exprPair IN expr.getDictItemList()
CodeProperties keyProps = getProps(exprPair.getKey(), ctx)
CodeProperties valueProps = getProps(exprPair.getValue(), ctx)
IF keyProps.methodCall || valueProps.methodCall
props.methodCall = TRUE
}
}
}
props.allocMem = TRUE
props.inAllocMem = TRUE
CASE Zui.ExprType.eINIT
# Using a temp var for the new object.
addTemp(expr, ctx)
FOR kv IN expr.getInitItemList()
propsLeft = getProps(kv.getValue(), ctx)
IF propsLeft.methodCall
props.methodCall = TRUE
}
}
props.allocMem = TRUE
props.inAllocMem = TRUE
CASE Zui.ExprType.eBITSASSIGN
CASE Zui.ExprType.eTRUE
CASE Zui.ExprType.eFALSE
CASE Zui.ExprType.eNAT
CASE Zui.ExprType.eINT
CASE Zui.ExprType.eFLOAT
CASE Zui.ExprType.eOK
CASE Zui.ExprType.eFAIL
CASE Zui.ExprType.eNIL
CASE Zui.ExprType.eINF
CASE Zui.ExprType.eNINF
CASE Zui.ExprType.eNAN
CASE Zui.ExprType.ePARENT
CASE Zui.ExprType.eTHIS
# props allocMem and methodCall both FALSE
BREAK
CASE Zui.ExprType.eMETHOD
CASE Zui.ExprType.eLAMBDA
IF exprExt.decl != NIL
MethodType method = exprExt.decl.type.getMethod()
IF method != NIL
IF method.hasUseArguments()
# A method with USE arguments will allocate a Tcb. Without USE
# arguments it is a constant.
props.allocMem = TRUE
}
# Annotate the statements inside the method.
Scope saveScope = ctx.scope
ctx.scope = method.scope
getProps(method.scope?.statements, ctx)
ctx.scope = saveScope
}
}
CASE Zui.ExprType.eASSIGN
# "name = expr" only depends on "expr"
props = getProps(expr.getRight(), ctx)
DEFAULT
ctx.error("Expression type not handled: " .. expr.getType().ToString(),
ctx.zcPos(expr.getPos()))
}
IF exprExt.conversion == Conversion.object2iobject
# Need an object to store the result.
# TODO: not needed for an assignment to a variable.
addTemp(expr, TRUE, ctx)
ELSEIF exprConversionAlloc(expr)
# Calling Zao() allocates memory. String conversions too.
props.allocMem = TRUE
}
exprExt.props = props
RETURN props
}
# Use a temp variable for the result of |expr|.
PROC addTemp(Zui.Expression expr, SContext ctx)
addTemp(expr, FALSE, ctx)
}
PROC addTemp(Zui.Expression expr, bool force, SContext ctx)
addTemp(expr, ZuiExpressionExt.get(expr).resultType, force, ctx)
}
PROC addTemp(Zui.Expression expr, Type resultType, bool force, SContext ctx)
# If the expression is the name of a variable or a constant there is no
# need for a temp variable.
# TODO: also for global variables in a module or class.
IF !force && (expr.getType() == Zui.ExprType.eID
|| expr.getType() == Zui.ExprType.eTHIS
|| expr.getType() == Zui.ExprType.eNIL)
RETURN
}
# Return if we can't use a temp var or use another var.
# And return if there already is a temp var.
VAR exprExt = ZuiExpressionExt.get(expr)
IF ctx.stmt == NIL || exprExt.toplevelVar != NIL || exprExt.tempDecl != NIL
RETURN
}
# A declaration "Foo %var = initExpr" will be initialized in the global
# scope or the module init function, not in the current method scope.
bool isShared = ctx.stmt.getType() == Zui.StatementType.eVAR_DECL
&& (ctx.stmt.getDeclaration().getPercent()
|| ctx.stmt.getDeclaration().getShared())
# When inside a method assign the result to a temp variable in that method.
# When in the module or file scope use a temp variable in the
# initialization method.
MethodScope ms
IF !isShared
ms = ctx.scope?.methodScope
}
IF ms == NIL && ctx.scope != NIL
&& (ctx.scope ISA ModuleScope || ctx.scope ISA FileScope || isShared)
ZimbuFile top = ctx.scope.topZimbuFile()
IF top.initScope == NIL
top.initScope = NEW(NIL, NIL)
}
ms = top.initScope
}
IF ms != NIL
Type type = resultType
IF expr.getType() == Zui.ExprType.eLIST
&& (type?.getClassType(ctx) != NIL
|| type?.getTtype() == Type.Enum.dyn)
# When the target is an object and the expression is a list the temp
# var will hold the list pointer. "multiDict<> md = [values]"
# When the target is a dyn the temp var holds a list pointer.
type = Type.aList
}
IF type != NIL && (force || type.isManaged()
|| type.getTtype() == Type.Enum.multiple)
string typeName = type.getTypeName(ctx)
Declaration.C decl
FOR d IN ms.tempVars
IF d.type.getTypeName(ctx) == typeName && !d.temp
decl = d
BREAK
}
}
IF decl == NIL
decl = NEW("temp")
decl.pName = "t" .. ms.tempVars.Size()
decl.zuiPos = expr.getPos()
decl.type = type
IF ms.tempVars == NIL
ms.tempVars = NEW()
}
ms.tempVars.add(decl)
Generate.mayPutDeclInMethodScope(decl, ctx)
}
decl.temp = TRUE # mark it as being used now
exprExt.tempDecl = decl
VAR ctxStmtExt = ZuiStatementExt.get(ctx.stmt)
IF ctxStmtExt.tempDecl == NIL
ctxStmtExt.tempDecl = NEW()
}
ctxStmtExt.tempDecl.add(decl)
}
}
}
}