-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResolve.zu
3764 lines (3374 loc) · 132 KB
/
Resolve.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
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 Zimbu compiler written in Zimbu
#
# Resolve class and module: Methods are invoked from Generate for each node
# where symbols need to be resolved.
#
# Copyright 2009 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 "ArrayStuff.zu"
IMPORT "BitsType.zu"
IMPORT "BitsValueType.zu"
IMPORT "CallbackType.zu"
IMPORT "ClassType.zu"
IMPORT "ContainerType.zu"
IMPORT "Conversion.zu"
IMPORT "Declaration.zu"
IMPORT "DeclStore.zu"
IMPORT "DictStuff.zu"
IMPORT "EnumType.zu"
IMPORT "EnumValueType.zu"
IMPORT "ExprEval.zu"
IMPORT "ExprArg.zu"
IMPORT "ForLoopInfo.zu"
IMPORT "Generate.zu"
IMPORT "ListStuff.zu"
IMPORT "MethodScope.zu"
IMPORT "MethodRefType.zu"
IMPORT "MethodType.zu"
IMPORT "ModuleScope.zu"
IMPORT "ModuleType.zu"
IMPORT "MultipleType.zu"
IMPORT "NoAllocType.zu"
IMPORT "Output.zu"
IMPORT "SContext.zu"
IMPORT "Scope.zu"
IMPORT "SymUse.zu"
IMPORT "TryScope.zu"
IMPORT "Type.zu"
IMPORT "TupleType.zu"
IMPORT "UsedFile.zu"
IMPORT "ValueType.zu"
IMPORT "WriteCommon.zu"
IMPORT "ZimbuFile.zu"
IMPORT "ZuiDeclarationExt.zu"
IMPORT "ZuiExpressionExt.zu"
IMPORT "ZuiMethodCallExt.zu"
IMPORT "ZuiStatementExt.zu"
IMPORT "ZuiTryStatementExt.zu"
IMPORT "genC/WriteC.zu"
CLASS Resolve EXTENDS WriteCommon @items=public # TODO: restrict visibility
NEW()
$virtualFuncMap = NEW()
}
# Return the name to be used for "THIS". When |insideNew| for a NEW()
# method, otherwise for any other method.
FUNC $thisName(bool insideNew) string
# return arbitrary value
RETURN "this"
}
# Write statement and line end, usually ";\n".
PROC $statementLineEnd(Output out)
}
# Write code for the end of a scope.
# |willJump| is true when there is a jump next, no need to jump out of the
# scope here.
# |statements| can be NIL.
PROC $writeScopeEnd(bool writeLabel, bool willJump,
list<Zui.Statement> statements, Zui.Position pos, SContext ctx)
}
# Write code to jump to the end of the scope.
PROC $writeJumpToLabel(SContext ctx)
}
# Return TRUE if an abstract method method is not to be generated.
FUNC $skipAbstractMethod() bool
RETURN FALSE # in case writing JS code later
}
# Return TRUE if a method from the parent is to be written in a child class.
FUNC $doWriteParentMethod(Declaration decl) bool
RETURN TRUE # in case writing C code later
}
# Generate the interface member lookup table, when needed.
PROC $interfaceMemberTable(Declaration decl, Declaration itf, SContext ctx)
}
# Write interface member lookup tables for a class.
PROC $writeIMTTables(Declaration decl, set<string> imtDone, SContext ctx)
}
# Write object declaration table for a class.
PROC $writeToTable(Declaration decl, SContext ctx)
# TODO: Only used when object.ToString() is used.
ClassType thisClass = decl.type.getClassType(ctx)
int undef
findToStringMethod(decl, Declaration.itemToString,
thisClass, decl.zuiPos, &undef, ctx)
}
# End of writing declaration table for a class.
PROC $writeIMTend(SContext ctx)
}
# Return the code-specific entry in |zimbuFile|.
FUNC $getCS(ZimbuFile zimbuFile) ZimbuFile.CodeSpecific
LOG.internal("Resolve.getCS() should not be called")
RETURN NIL
}
# Produce head of the Main() function.
PROC $mainHead(MethodType method, SContext ctx)
}
# Produce the start of the body of the Main() function.
PROC $mainMiddle(SContext ctx)
}
# Produce end of the Main() function.
PROC $mainEnd(SContext ctx)
}
# Write any collected virtual methods.
PROC $writeVirtual(SContext ctx)
}
# Allocate a new object.
PROC $writeAlloc(string typeName, Declaration finishMethod,
Output out, SContext ctx)
ctx.addUsedItem(Declaration.alloc)
}
# Write a call to $Init()
PROC $writeObjectInit(Declaration initDecl, ClassType classType,
Declaration dest, SContext ctx)
}
# Write a call to clear a not allocated item.
PROC $writeNoAllocClear(string typeName, Declaration initMethod,
Declaration finishMethod, string destName, SContext ctx)
}
# Write a call to clear a not allocated object.
PROC $writeNoAllocClear(string typeName, Declaration initMethod,
Declaration finishMethod, Declaration dest, SContext ctx)
}
# Allocate a new string from an array.
PROC $writeNewString(Zui.MethodCall call, int &undef, SContext ctx)
IF Generate.checkArgCount(call, 1, 3, ctx) == OK
list<Zui.Expression> args = call.getArgumentList()
Generate.genExpr(args[0], ctx, Type.anArray)
undef += ZuiExpressionExt.get(args[0]).undefined
IF args.Size() >= 2
Generate.genExpr(args[1], ctx, Type.anInt)
undef += ZuiExpressionExt.get(args[1]).undefined
}
IF args.Size() == 3
Generate.genExpr(args[2], ctx, Type.anInt)
undef += ZuiExpressionExt.get(args[2]).undefined
}
}
}
# Allocate a new array for |type|.
PROC $writeArrayAlloc(ContainerType type, string noAllocName,
Zui.MethodCall call, int &undef, SContext ctx)
ctx.addUsedItem(Declaration.array)
ctx.addUsedItem(Declaration.newArray)
list<Zui.Expression> args = call.getArgumentList()
IF args.Size() == 1
Generate.genExpr(args[0], ctx, Type.anInt)
undef += ZuiExpressionExt.get(args[0]).undefined
ELSEIF args.Size() != 0
ctx.error("Expected one argument", call.getPos())
}
}
# Allocate a new list for |type|.
PROC $writeListAlloc(ContainerType type, string noAllocName,
Zui.MethodCall call, int &undef, SContext ctx)
ctx.addUsedItem(Declaration.newList)
list<Zui.Expression> args = call.getArgumentList()
IF args.Size() == 2
Generate.genExpr(args[0], ctx, Type.anInt)
Generate.genExpr(args[1], ctx, type.itemType)
undef += ZuiExpressionExt.get(args[0]).undefined
+ ZuiExpressionExt.get(args[1]).undefined
ELSEIF args.Size() != 0
ctx.error("Expected zero or two arguments", call.getPos())
}
}
# Write the code to declare a callback type.
PROC $writeCallbackDecl(CallbackType type, Zui.Position pos, SContext ctx)
}
# Allocate a new callback for |type|.
PROC $writeCallbackAlloc(CallbackType type, string noAllocName,
Generate.CallbackInfo cbInfo,
Zui.MethodCall call, int &undef, SContext ctx)
list<Zui.Expression> args = call.getArgumentList()
int argOffset = cbInfo != NIL ? 0 : 1
IF args.Size() != type.arguments.Size() + argOffset
IF ZuiMethodCallExt.get(call).undefined == 0
ctx.error("Expected " .. (type.arguments.Size() + argOffset)
.. " arguments, found " .. args.Size(), call.getPos())
}
undef += 10
ELSE
Zui.Expression methodExpr = cbInfo != NIL ? call.getName() : args[0]
VAR methodExprExt = ZuiExpressionExt.get(methodExpr)
methodExprExt.undefined = 0
IF cbInfo == NIL
Generate.genExpr(methodExpr, ctx, type.calledMethodType.getMethodRef())
ELSE
bool isIobject
IF methodExpr.getType() == Zui.ExprType.eMEMBER
Zui.Expression left = methodExpr.getLeft()
Type leftType = genExpr(left, ctx)
IF leftType != NIL && leftType.getTtype() == Type.Enum.iobject
isIobject = TRUE
# $iobjectFuncUse(cbInfo.methodType, leftType, left, ctx)
SymUse symUse = NEW(methodExpr.getPos(), ctx)
Generate.markMethodsUsed(cbInfo.methodType,
leftType.getClassType(ctx), cbInfo.methodType.name,
symUse, ctx)
}
}
IF !isIobject
$namelessFuncUse(cbInfo.methodType, ctx)
}
}
undef += methodExprExt.undefined
FOR i IN argOffset UNTIL args.Size()
Generate.genExpr(args[i], ctx, type.arguments[i - 1].type)
undef += ZuiExpressionExt.get(args[i]).undefined
}
ctx.addUsedItem(type.calledMethodType)
}
}
# Allocate a new closure for |type|.
PROC $writeClosureAlloc(CallbackType type, MethodType method, bool typeCast,
int &undef, SContext ctx)
$namelessFuncUse(type.calledMethodType, ctx)
FOR l IN [method.useArguments, method.autoArguments]
FOR arg IN l
IF arg.type ISA MethodRefType
# PROC foo(USE funcName): generate callback decl for funcName
Generate.generateMethodUse(undef, arg.type.getMethod(),
arg.type.getMethod(), ctx)
ctx.addUsedItem(Declaration.funcRef)
}
}
}
}
# Write the code to declare a tuple type.
PROC $writeTupleDecl(TupleType tupleType, Zui.Position pos, SContext ctx)
FOR decl IN tupleType.types
int undef
getArgumentType(decl.type, pos, &undef, ctx)
}
Type.aDyn.addDependsOn(Declaration.itemToString)
IF tupleType.toStringDecl != NIL
tupleType.toStringDecl.addDependsOn(Declaration.itemToString)
}
}
PROC $writeTupleAlloc(TupleType type, string noAllocName,
Zui.MethodCall call, int &undef, SContext ctx)
list<Zui.Expression> args = call.getArgumentList()
IF args.Size() == 0
# NEW() without setting values
ELSEIF args.Size() != type.types.Size()
ctx.error("Expected " .. (type.types.Size() + 1)
.. " arguments, found " .. args.Size(), call.getPos())
undef += 10
ELSE
FOR i IN 0 UNTIL args.Size()
Generate.genExpr(args[i], ctx, type.types[i].type)
undef += ZuiExpressionExt.get(args[i]).undefined
}
}
}
PROC $callTupleToString(Zui.MethodCall call,
Type type, Zui.Expression expr, SContext ctx)
genExpr(expr, ctx, type)
type.getEffType().<TupleType>.usingToString(ctx)
}
PROC $callTypeToString(Zui.Expression expr, SContext ctx)
genExpr(expr, ctx, Type.aType)
ctx.addUsedItem(Declaration.typeToString)
}
PROC $callTypeName(Zui.Expression expr, SContext ctx)
genExpr(expr, ctx, Type.aType)
ctx.addUsedItem(Declaration.typeNameFunc)
}
# Write field of an iobject for assignment.
PROC $writeDerefLhs(Output out)
}
# Write the statement for a NEW() method that allocates the object, or, when
# |init| is not NIL, calls $Init().
PROC $writeNewThis(MethodType method, Declaration initMethod,
Declaration finishMethod, Zui.Position pos, SContext ctx)
ctx.scope.wantBacktrace = TRUE
ctx.addUsedItem(Declaration.alloc)
IF finishMethod != NIL
ctx.addUsedItem(Declaration.hasFinish)
}
}
# Write the first argument of a NEW() method call.
# When |useThis| is TRUE "THIS".
# When |className| is not NIL clear the |dest| not allocated variable. Then
# |initMethod| is the Init() method to use, can be NIL.
# Otherwise "NULL".
PROC $writeNewArg(bool useThis, string className, Declaration dest,
Declaration initMethod, Declaration finishMethod,
bool hasArg, SContext ctx)
}
# Write the return statement for a NEW() method.
PROC $writeNewReturn(Zui.Position pos, SContext ctx)
}
# Write the name of |decl| with type |type|, surrounding it with what is
# required to access it.
# When |read| is TRUE the symbol is read from, not assigned to or called.
PROC $writeSymName(Declaration decl, Type type, bool read, SContext ctx)
ctx.addUsedItem(decl)
}
# Write the name of the variable |decl|.
PROC $writeVarName(Declaration decl, SContext ctx)
}
# Generate using a nameless function as an expression, the Tcb used.
PROC $namelessFuncUse(Declaration decl, SContext ctx)
ctx.addUsedItem(decl)
}
# Using a nameless function as an expression, the Tcb is used.
PROC $namelessFuncReference(Declaration decl, SContext ctx)
ctx.addUsedItem(decl)
}
# Generate the type cast for a proc_ref or func_ref.
PROC $refCast(Type type, Zui.Position pos, SContext ctx)
}
# Generate a type cast for a reference.
PROC $refCast(SContext ctx)
}
# Generate an object initializer.
PROC $objectInit(Zui.Expression initExpr, Declaration dest, SContext ctx)
Type destType = dest?.type
VAR initExprExt = ZuiExpressionExt.get(initExpr)
ClassType classType = destType?.getClassType(ctx)
IF classType == NIL
initExprExt.undefined = 10
ELSE
initExprExt.undefined = Generate.generateEmptyNewCall(initExpr.getPos(),
destType, ctx)
initExprExt.resultType = destType
SymUse symUse = NEW(initExpr.getPos(), ctx)
ctx.addUsedItem(destType)
FOR init IN initExpr.getInitItemList()
string name = init.getName()
Zui.Expression expr = init.getValue()
Declaration decl = classType.findObjectMember(name, symUse,
searchParent, TRUE)
IF decl == NIL
initExprExt.undefined += 2
ELSE
Generate.genExprDoConv(expr, ctx, decl.type)
initExprExt.undefined += ZuiExpressionExt.get(expr).undefined
ctx.addUsedItem(decl)
}
}
}
}
# Generate the return type of a method from the declaration.
# Adds undefined count to decl.undefined.
# Return a symbol for the type.
FUNC $methodReturnType(Zui.Declaration decl, bool isNew, bool isInit,
SContext ctx) Type
Zui.MethodType method = decl.getType().getMethodDecl()
Type retType
IF isNew || isInit
retType = ctx.scope.classType
ELSEIF method.hasReturnType() # func
list<Declaration> types
IF method.sizeReturnType() > 1
types = NEW()
}
FOR t IN method.getReturnTypeList()
Zui.Expression expr = t.getName()
IF expr.getType() == Zui.ExprType.eTHIS
# Returning THIS, the object itself.
retType = ctx.scope.classType
ELSE
retType = Generate.generateDeclType(expr, ctx,
isDecl + dotI + markUsed, NEW(decl.getPos(), ctx))
ZuiDeclarationExt.get(decl).undefined +=
ZuiExpressionExt.get(expr).undefined
}
IF types != NIL
IF retType == NIL
# When one of the types is unknown the whole type should be
# assumed unknown, so that where the method is called is resolved
# again.
types = NIL
ELSE
types.add(retType.getValueType(ctx))
}
}
}
IF types != NIL
retType = MultipleType.NEW(types, "ret")
}
}
RETURN retType
}
# Write the stack frame offset table.
PROC $methodLeader(MethodType method, Output out, SContext ctx)
}
# Write the head of the method: function name, "(" and optionally "THIS".
PROC $methodStart(Declaration decl, bool hasArguments, SContext ctx)
}
# Write the init at the very start of a function.
# RetType is the returned type for a FUNC.
PROC $methodBodyStart(MethodType method, bool isNew, bool isInit,
Zui.Position pos, SContext ctx)
IF ctx.scope.isClassScope() && ctx.scope.thisName != NIL && !isNew
ctx.addUsedItem(Declaration.throwThisNil)
}
IF ctx.scope.isClassScope() && ctx.scope.thisName != NIL
# Add "THIS" to the stack frame.
# Use the "THIS" name for C, that's the only place where it is used.
Generate.putRefInMethodScope(WriteC.cThisName, ctx.scope.classType, ctx)
}
# The return variable in not on the stack frame, it is used when it's not
# safe to run the GC.
# An argument may be a reference.
IF ctx.scope.declDict != NIL
Generate.checkRefScope(ctx.scope.declDict, ctx)
}
IF ctx.scope.<MethodScope>.objectMembers != NIL
Generate.checkRefScope(ctx.scope.<MethodScope>.objectMembers, ctx)
}
}
# Write the start of a method call.
PROC $writeMethodCall(Declaration funcDecl, bool moreArgs, SContext ctx)
}
# object.Type()
PROC $callObjectType(Zui.MethodCall call, ClassType class, Type object,
Zui.Expression expr, SContext ctx)
Generate.generateVarname(expr, ctx, object)
ZuiMethodCallExt.get(call).undefined += ZuiExpressionExt.get(expr).undefined
}
# object.ToString()
PROC $callObjectToString(Zui.MethodCall call, ClassType class, Type object,
Zui.Expression expr, SContext ctx)
Generate.generateVarname(expr, ctx, object)
ZuiMethodCallExt.get(call).undefined += ZuiExpressionExt.get(expr).undefined
}
# Add a DEFER'ed function to the defer list
PROC $addDefer(Zui.MethodCall call, Generate.CallbackInfo cbInfo,
SContext ctx)
ctx.scope.methodScope.hasDefer = TRUE
ctx.addUsedItem(cbInfo.methodType)
int undef
$writeCallbackAlloc(cbInfo.callback, NIL, cbInfo, call, &undef, ctx)
ZuiMethodCallExt.get(call).undefined += undef
}
# Call to a function reference.
FUNC $functionRefCall(Zui.MethodCall call, Declaration decl,
string funcName, Type destType, SContext ctx
) Type
ctx.addUsedItem(decl)
IF decl.type.getMethod() == NIL
RETURN NIL
}
RETURN decl.type.getMethod().returnType
}
# Call to a method reference.
FUNC $methodRefCall(Zui.MethodCall call,
Type mtype,
Type object,
Zui.Expression objExpr,
string pName,
Type destType,
SContext ctx) Type
Generate.generateVarname(objExpr, ctx, object)
ZuiMethodCallExt.get(call).undefined +=
ZuiExpressionExt.get(objExpr).undefined
MethodType mt = mtype.getMethod()
string methodName = call.getName().getRight().getId().getName()
Generate.generateArgumentsCheck(call, methodName, ctx, mt, destType)
ctx.out.write(")")
# TODO: handle callback with extra arguments
IF mt != NIL && mt.returnType != NIL
RETURN mt.returnType
}
RETURN NIL
}
# "object.method(args)"
# Keep in sync with WriteC.objectCall()!
FUNC $objectCall(Zui.MethodCall call,
bool i_object_arg,
list<Declaration.C> arglist,
Declaration mdecl,
Type objectType,
Zui.Expression objExpr,
string pName,
Type destType,
SContext ctx) Type
Zui.Expression nameExpr = call.getName() # method
string methodName = nameExpr.getRight().getId().getName()
MethodType methodType
IF mdecl.type ISA CallbackType
# The method is actually a callback:
methodType = mdecl.type.<CallbackType>.methodType
ELSE
methodType = mdecl.type
}
Type retType = methodType.returnType
# Default is to use the argument type list of the method.
# But when generating a virtual function we use the types of the arguments
# passed in.
list<Declaration.C> useArglist = methodType.getArgList()
VAR callExt = ZuiMethodCallExt.get(call)
IF !(ctx.scope.isClassScope() && ctx.scope.classType.isAbstract())
Type class = objectType.getClassType(ctx)
IF class == NIL
callExt.undefined += 10
RETURN retType
}
string funcName = class.pName
IF objectType.ttype == Type.Enum.iobject
funcName ..= "_I"
}
funcName ..= "__M" .. methodName .. "_I"
# The "left" always needs to be evaluated.
Generate.generateVarnameParent(objExpr, ctx, objectType)
callExt.undefined += ZuiExpressionExt.get(objExpr).undefined
# For C we need to call the right function and pass "THIS".
# But we don't write anything for an abtract class.
IF objectType.ttype == Type.Enum.object && !i_object_arg
# object.method(arg) -> method_name(object, arg)
ctx.addUsedItem(mdecl)
ELSEIF objectType.ttype == Type.Enum.iobject && !i_object_arg
# object.method(arg)
# -> ((ret (*)(args))(object->table[method_idx]))
# (object->ptr, arg)
int udef
$usingIobjectMethod(mdecl, arglist, objectType, &udef,
methodName, nameExpr.getPos(), funcName, ctx)
callExt.undefined += udef
ELSE
# One of the arguments is of i_object type.
# object.method(arg) -> method_func(object, arg)
# Define a function that does the work:
# RetType method_func(objectType *object, argType arg) {
# int idx = object->type * NTYPES + arg->type;
# if (some_table[idx]) /* arg used as i_object */
# return func_table[idx](object, arg)
# return func_table[idx](object, arg->ptr)
# }
# TODO: doesn't work for PARENT.method()
list<Declaration.C> newArglist = NEW()
IF arglist != NIL
FOR i IN 0 UNTIL arglist.Size()
Declaration.C d = arglist[i]
IF d != NIL
IF d.type.ttype == Type.Enum.unknown
|| d.type.ttype == Type.Enum.nilval
|| (d.type ISA ContainerType
&& d.type.<ContainerType>.itemType == NIL)
|| d.type.isValueType()
# The argument is NEW(), NIL or [], use the argument type
# of the found method.
# For value types also use the argument type of the found
# method. E.g., for a "9" (natval) may use an "int" type.
d = useArglist[i]
ELSE
# The method argument type is not passed below, but will be
# used inside the Virtual function, evaluate it to mark items
# as used.
ctx.gen.writeArgExpr(call.getArgument(i),
ctx, useArglist[i].type)
}
}
newArglist.add(d)
}
}
funcName ..= objectArgName(arglist, methodType, ctx)
int udef
Declaration.C funcDecl = $generateVirtualFunc(funcName, objectType,
methodName, nameExpr.getPos(), &udef, newArglist, ctx)
callExt.undefined += udef
IF funcDecl != NIL
retType = funcDecl.type
ctx.addUsedItem(funcDecl)
}
# Must generate the arguments for the virtual function, same types as
# the arguments that are passed in (thus no iobject -> object
# conversion).
useArglist = newArglist
}
}
Generate.generateArgumentsCheck(call, methodName, ctx, useArglist,
methodType, destType)
RETURN retType
}
# Mark a method on an iobject as being used.
PROC $usingIobjectMethod(Declaration mdecl,
list<Declaration.C> arglist,
Type objectType,
int &udef,
string methodName,
Zui.Position pos,
string baseFuncName,
SContext ctx)
ctx.addUsedItem(mdecl)
# Need to mark all methods that implement this interface as used.
# Make a call to generateVirtualFunc() for that.
string funcName = baseFuncName .. objectArgName(arglist, mdecl.type, ctx)
Declaration.C funcDecl = $generateVirtualFunc(funcName, objectType,
methodName, pos, udef, arglist, ctx)
# If this scope is generated then all methods that implement the call
# must be generated.
IF funcDecl != NIL
ctx.addUsedItem(funcDecl)
}
# If another implementation or method is added to the interface we
# must come back here.
objectType.getClassType(ctx).scope.addScopeDependency(ctx.scope)
}
# Remember which functions have been generated, don't do one twice.
# Key is a concatenation of class, argument, etc.
dict<string, Declaration.C> $virtualFuncMap
# Generate a function that takes an object or i_object and a list of
# arguments and figures out the method to be invoked.
# |nameExpr| is the method name.
# Returns the Declaration to keep track of the virtual method.
# NIL if there is something wrong.
FUNC $generateVirtualFunc(string funcKey,
Type varType,
string methodName,
Zui.Position pos,
int &undef,
list<Declaration.C> callArglist,
SContext ctx
) Declaration
IF $virtualFuncMap.has(funcKey)
RETURN $virtualFuncMap.get(funcKey)
}
Type retType
Declaration.C funcDecl = NEW(funcKey)
# Make a list with list of possible symbols for THIS and each argument.
# altList[0] for A0 THIS
# altList[1] for A1 arg 1
# altList[1] for A2 arg 2
# etc.
list<list<Declaration>> altList = getDeclAltList(
varType, methodName, pos, undef, callArglist, ctx)
IF altList == NIL
RETURN NIL
}
list<int> indexes = NEW()
FOR l IN altList
indexes.add(0)
}
# Find out for what arguments with more than one possible type, the type
# makes us pick another method. If the type doesn't matter, just use the
# first one. This avoids adding a switch where all alternatives are
# equal.
FOR tryIdx IN 0 UNTIL altList.Size()
IF altList[tryIdx].Size() > 1
# For every possible combination of types for arguments other than
# tryIdx see if the picked method changes if we use a different type
# for tryIdx.
bool pickedAnother = FALSE
indexes.map({ i => 0 })
WHILE !pickedAnother
# Gather the arguments for this combination.
indexes[tryIdx] = 0
list<Declaration.C> argtry = NEW()
FOR idx IN 1 UNTIL altList.Size()
Declaration.C decl = NEW("")
decl.type = altList[idx][indexes[idx]].type
argtry.add(decl)
}
# Try every type of altList[tryIdx], see if the picked method
# changes
Type pickedMethod
FOR tryIdxVal IN 0 UNTIL altList[tryIdx].Size()
indexes[tryIdx] = tryIdxVal
IF tryIdx > 0 && tryIdxVal > 0
# Change the argument at tryIdx to tryIdxVal
argtry[tryIdx - 1].type = altList[tryIdx][tryIdxVal].type
}
Declaration object = altList[0][indexes[0]]
Declaration mdecl
mdecl = Generate.findMethodArglist(object.type, methodName,
TRUE, argtry, NIL, ctx, TRUE,
searchParent + allowInvisible,
ctx.doError(), pos, "", undef)
IF mdecl == NIL
# can't find method, error given elsewhere
pickedAnother = TRUE
BREAK
}
IF pickedMethod == NIL
pickedMethod = mdecl.type
ELSEIF mdecl.type ISNOT pickedMethod
pickedAnother = TRUE
BREAK
}
}
IF pickedAnother
BREAK
}
# Next combination of arguments other than tryIdx.
bool didinc
FOR i IN 0 UNTIL indexes.Size()
IF i != tryIdx && altList[i].Size() > 1
IF indexes[i] + 1 < altList[i].Size()
++indexes[i]
didinc = TRUE
BREAK
}
indexes[i] = 0
}
}
IF !didinc
BREAK
}
}
IF !pickedAnother
# type of this argument doesn't matter, set its size to one.
altList[tryIdx].remove(1, -1)
}
}
}
# If there is only one matching function we can skip all the conditions
# and just write that one.
# TODO: can skip a lot of stuff now that we figure out haveTwoMethods
# early.
bool haveTwoMethods
FOR l IN altList
IF l.Size() > 1
haveTwoMethods = TRUE
ctx.addUsedItem(Declaration.throwCstringBadValue)
BREAK
}
}
# For every permutation find a matching function.
# E.g. A0 is an i_object with 2 possible classes, A2 is an i_object with
# three possible classes
# switch (A0->type) {
# case 0:
# switch (A2->type) {
# case 0:
# return func_obj0_obj0(A0->ptr, A1, A2->ptr);
# case 1:
# return func_obj0_itf(A0->ptr, A1, A2);
# case 2:
# return func_obj0_itf(A0->ptr, A1, A2);
# }
# case 1:
# switch (A2->type) {
# case 0:
# return func_obj1_obj0(A0->ptr, A1, A2->ptr);
# case 1:
# return func_obj1_obj1(A0->ptr, A1, A2->ptr);
# case 2:
# return func_obj1_obj2(A0->ptr, A1, A2->ptr);
# }
# }
#
# Every possible class must have a matching method, either with that class
# or with an interface.
indexes.map({ i => 0 })
Type foundRetType
# depth indicates what to do when a matching func is found:
# 0: write "switch" for A0
# 1: write "case" for A0
# 2: write "switch" for A1
# 3: write "case" for A1
# etc.
int depth
WHILE TRUE
list<Declaration.C> argtry = NEW()
FOR idx IN 1 UNTIL altList.Size()
Declaration.C decl = NEW("")
decl.type = altList[idx][indexes[idx]].type
argtry.add(decl)
}
Declaration object = altList[0][indexes[0]]
Declaration mdecl
mdecl = Generate.findMethodArglist(object.type, methodName, TRUE,
argtry, NIL, ctx, TRUE, searchParent + allowInvisible,
FALSE, pos, "", undef)
Type mtype = mdecl?.type
IF mtype == NIL
++undef
ELSE
IF mtype.ttype == Type.Enum.func && mtype.<MethodType>.returnType != NIL
# TODO: check the return types of all methods are equal.
retType = mtype.<MethodType>.returnType
IF foundRetType != NIL
&& !Type.matchingTypes(foundRetType, retType, ctx)
++undef
}
foundRetType = retType
}
depth = 2 * altList.Size()
# The method is really only used if the class is also used.
funcDecl.addDependsOnCond(mdecl, object.type.getClassType(ctx))
}
# Advance to the next class for the argument.
int idx = altList.Size()
WHILE idx > 0
idx--
IF depth > idx * 2
depth--
}
IF altList[idx].Size() > 1
indexes[idx]++
IF indexes[idx] < altList[idx].Size()
BREAK
}
indexes[idx] = 0
IF depth > idx * 2
depth--
}
ELSE
depth--
}
}
IF depth <= 0
BREAK
}
}
# Most likely newPosString() is invoked, mark it as used.
newPosString(pos, ctx)
funcDecl.type = retType
$virtualFuncMap[funcKey] = funcDecl
RETURN funcDecl
}
# Write code for "object.member.(expr)(arg)".
# |method| has the arguments.
# |method.getName()| is "object.member.(expr)"
# |method.getName().getLeft()| is "object.member"
# |method.getName().getRight()| is "expr"
FUNC $memberExpr(Zui.MethodCall method, Generate.CallbackInfo cbInfo,
SContext ctx, Type destType) Type
Zui.Expression nameExpr = method.getName()
IF cbInfo != NIL
ctx.error("Not supported for DEFER (memberExpr)", nameExpr)
}
VAR methodExt = ZuiMethodCallExt.get(method)
# Evaluate "(expr)"
ctx.scope.wantBacktrace = TRUE
Type type = genExpr(nameExpr.getRight(), ctx)
methodExt.typeObj = type
# Evaluate "object.member"
Type classType
IF type != NIL && type ISA MethodType
classType = type.<MethodType>.classType
}
genExpr(nameExpr.getLeft(), ctx, classType)
# Evaluate "(arg)"
MethodType methodType = destType ISA MethodType ? destType : NIL
Generate.generateArgumentsCheck(method, "{expr}", ctx,
methodType?.getArgList(), methodType, destType)
methodExt.undefined = ZuiExpressionExt.get(nameExpr.getLeft()).undefined
+ ZuiExpressionExt.get(nameExpr.getRight()).undefined
IF methodExt.typeObj != NIL
RETURN methodExt.typeObj.getReturnType()
}
++methodExt.undefined
RETURN NIL
}
# Write a function argument "argName" with type declaration.
# |first| is true for the first argument.
PROC $argWithType(bool first, Type type, Zui.Position pos,
string argName, SContext ctx)
}
# Write a varargs argument for |args[startIndex]| .. |args[args.Size() - 1]|
# |decl| has the type of the method varargs.
# Return the number of undefined symbols.
FUNC $writeVarargs(Zui.MethodCall call, Type type, TupleType tupleType,
list<Zui.Expression> args, int startIndex, SContext ctx) int
ctx.addUsedItem(Declaration.array)
ctx.addUsedItem(Declaration.newArray)
int undef
FOR idx IN startIndex UNTIL args.Size()
Zui.Expression expr = args[idx]
IF expr.getType() == Zui.ExprType.eASSIGN
# for "name = value" generate "value".
expr = expr.getRight()
}
ctx.gen.writeArgExpr(expr, ctx, type)
undef += ZuiExpressionExt.get(expr).undefined
}
RETURN undef
}
# Return TRUE when forward declarations are to be written.
FUNC $doWriteDecl() bool
RETURN FALSE
}
# expr.left[expr.right]
FUNC $subscript(Zui.Expression expr, SContext ctx, Type destType) Type
Zui.Expression left = expr.getLeft()
Zui.Expression right = expr.getRight()
VAR exprExt = ZuiExpressionExt.get(expr)
VAR leftExt = ZuiExpressionExt.get(left)
VAR rightExt = ZuiExpressionExt.get(right)
Type ret