-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassType.zu
735 lines (652 loc) · 21.3 KB
/
ClassType.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
#
# The Zimbu compiler written in Zimbu
#
# ClassType class.
#
# A Type used for classes and interfaces.
#
# Copyright 2011 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 "ClassRefType.zu"
IMPORT "ClassScope.zu"
IMPORT "Declaration.zu"
IMPORT "Generate.zu"
IMPORT "MethodType.zu"
IMPORT "Output.zu"
IMPORT "ReferenceType.zu"
IMPORT "SContext.zu"
IMPORT "SymUse.zu"
IMPORT "Template.zu"
IMPORT "Type.zu"
IMPORT "ZuiDeclarationExt.zu"
IMPORT "lib/ILoader.zu"
CLASS ClassType EXTENDS ReferenceType @items=public
# Scope that contains $members and $objectMembers
ClassScope $scope
# Parent class, if any.
ClassType $parent
# Inheritance level, number of classes to the top.
int $parentLevel
# Interfaces the class declared to implement.
# This can also be SomeClass.I, which is an ClassRefType iobject.
list<Declaration> $interfaces
# Known child classes.
list<ClassType> $children
# Classes known to implement this interface.
list<ClassType> $implementers
# Class used for variable that needs to be allocated, can't be on the stack.
Declaration $allocDecl
# When not NIL this class is templated and it lists the templates that were
# found for this class.
list<Template> $templateList
# When not NIL the list of names used in the template: <Tone, Ttwo>
list<string> $templateNames
# When not NIL the list of types used: <int, string>
list<Declaration> $templateTypes
# Unique class ID, for debugging class identity.
int $classId
# Fake $Init() declaration, only used when there is no real $Init().
# $initDecl.defined is TRUE when it is used.
MethodType $initDecl
# Object member declarations with inits. These are also used by children.
list<Zui.Declaration> $memberInits
# Object member declarations with inits. These are NOT used by children.
list<Zui.Declaration> $ownMemberInits
# Keep track of using the interfaceMemberTableTable
Declaration.C $imttDecl
# A lazily created declaration for the interface of this class, used for
# MyClass.I
Declaration $interfaceObjectDecl
# A lazily created declaration for the interface of this class, used for
# MyClass when it has children.
Declaration $interfaceObjectDeclAuto
# Cached value type. Can be an iobject for a class with children.
ClassRefType $valueType
# $valueType was set when $children was not empty
bool $valueTypeHadChildren
# Cached object type. For a class it is always an object, not iobject.
ClassRefType $objectType
# Cached starred type.
ClassRefType $starType
# For generating JS: whether the class has been written.
bool $jsWritten
NEW(Type.Enum type, string name) @replace
IF type == Type.Enum.class
|| type == Type.Enum.interface
|| type == Type.Enum.piece
PARENT.NEW(type, name)
ELSE
LOG.internal("Creating ClassType with type \(type.ToString())")
}
$classId = ++nextClassId
}
# Make a copy of this Type. Every subclass must redefine it.
FUNC $copyType() Type @replace @default
ClassType ret = NEW($ttype, $name)
$copyClassValues(ret)
RETURN ret
}
# Copy the values of this object into |ret|.
PROC $copyClassValues(ClassType ret) @default
$copyReferenceValues(ret)
ret.scope = $scope
ret.parent = $parent
ret.parentLevel = $parentLevel
ret.interfaces = $interfaces
ret.children = $children
ret.implementers = $implementers
ret.templateList = $templateList
ret.templateNames = $templateNames
ret.memberInits = $memberInits
ret.ownMemberInits = $ownMemberInits
IF ret.classId == 0 # should not happen
IF $classId > 0
ret.classId = $classId + 1000
ELSE
ret.classId = ++nextClassId + 1000
}
}
ret.imttDecl = $imttDecl
ret.interfaceObjectDecl = $interfaceObjectDecl
ret.interfaceObjectDeclAuto = $interfaceObjectDeclAuto
}
# Return the name for the object declaration table.
# Return NIL if this is not an object.
FUNC $getTypeName(SContext ctx) string @replace
IF $pName == NIL
RETURN "unknown" # error should be given elsewhere
}
RETURN $pName
}
# Return TRUE for types that use managed memory. This excludes pointers to
# callbacks, these are in static memory. Also exclude references, e.g. an
# "&undef" argument.
FUNC $isManaged() bool @replace
RETURN TRUE
}
# Return TRUE for types that can have an instance: Class, Enum, Bits.
FUNC $needCopyValue() bool @replace
RETURN TRUE
}
# Return the type of an object. When this type is an interface or a class
# with children this returns an iobject.
FUNC $getValueType(SContext ctx) Type @replace
bool hasChildren = $children != NIL && $children.Size() > 0
IF $valueType == NIL || $valueTypeHadChildren != hasChildren
# Use "object" for "Foo.C" and for "Foo" when there are no children.
# Note that children may be added later, remember that in
# $valueTypeHadChildren
IF $ttype == Type.Enum.classDotC
|| ($ttype == Type.Enum.class && !hasChildren)
$valueType = NEW(Type.Enum.object, THIS, "instance of " .. $name)
ELSE
$valueType = NEW(Type.Enum.iobject, THIS, "instance of " .. $name)
}
IF $zuiAttr != NIL
$valueType.zuiAttr = $zuiAttr.copy(TRUE)
$valueType.zuiAttr.setAbstract(FALSE)
}
$valueType.addDependsOn(THIS)
$valueTypeHadChildren = hasChildren
}
IF !hasChildren && $valueType.ttype == Type.Enum.object
# If the type is an object it might become an iobject if children are
# added to the class. Must come back here if that happens.
$scope.addScopeDependency(ctx.scope)
}
RETURN $valueType
}
# Return an object of this class.
FUNC $getObject() ClassRefType
IF $objectType == NIL
IF $ttype == Type.Enum.classDotC || $ttype == Type.Enum.class
$objectType = NEW(Type.Enum.object, THIS, "instance of " .. $name)
ELSE
$objectType = NEW(Type.Enum.iobject, THIS, "instance of " .. $name)
}
IF $zuiAttr != NIL
$objectType.zuiAttr = $zuiAttr.copy(TRUE)
$objectType.zuiAttr.setAbstract(FALSE)
}
$objectType.addDependsOn(THIS)
}
RETURN $objectType
}
# Return a class with the type "classDotC", the class itself: MyClass.C
FUNC $getStarType(Zui.Position pos, SContext ctx) ClassRefType @replace
IF $starType == NIL
IF $ttype != Type.Enum.class
ctx.error("Cannot use * after an interface name", pos)
}
$starType = NEW(Type.Enum.classDotC, THIS, $name .. "*")
IF $zuiAttr != NIL
$starType.zuiAttr = $zuiAttr.copy(TRUE)
}
$starType.addDependsOn(THIS)
}
RETURN $starType
}
# Return a declaration for an object of the interface of this class.
# It is stored, so that the same declaration is returned every time.
# Used for MyClass.I.
FUNC $getInterfaceObjectDecl() Declaration
IF $interfaceObjectDecl == NIL
$interfaceObjectDecl = $makeInterfaceObjectDecl()
}
RETURN $interfaceObjectDecl
}
# Return a declaration for an object of the interface of this class.
# It is stored, so that the same declaration is returned every time.
# Used for MyClass where it means MyClass.I if it has children.
FUNC $getInterfaceObjectDeclAuto() Declaration
IF $interfaceObjectDeclAuto == NIL
ClassRefType iobject = $makeInterfaceObjectDecl()
iobject.isAuto = TRUE
$interfaceObjectDeclAuto = iobject
}
RETURN $interfaceObjectDeclAuto
}
# Create an interface object for $interfaceObjectDecl or
# $interfaceObjectDeclAuto.
FUNC $makeInterfaceObjectDecl() ClassRefType
# Create an iclass ClassRefType, then make an iobject ClassRefType
# that references it.
ClassRefType iclass = NEW(Type.Enum.iclass, THIS, "interface of " .. $name)
ClassRefType iobject = NEW(Type.Enum.iobject, iclass,
"interface of " .. $name)
iobject.zuiDecl = $zuiDecl.copy(FALSE)
iobject.zuiPos = $zuiPos
iobject.addDependsOn(THIS)
RETURN iobject
}
# Return the name of the class instead of just "class".
FUNC $typeName(bool long) string @replace
string id = !long || $classId == 0 ? "" : "/" .. $classId
IF $scope == NIL || $scope.name == NIL
RETURN PARENT.typeName(long) .. id
}
RETURN $scope.name .. id
}
# For a method reference return the argument list.
# Otherwise return NIL
FUNC $getArgList() list<Declaration.C> @replace @default
RETURN NIL
}
# For a method reference return the return type.
# Otherwise return NIL
FUNC $getReturnType() Type @replace @default
RETURN NIL
}
PROC $addInterface(Type itf, Zui.Declaration zuiDecl, SContext ctx)
Declaration decl
VAR declExt = ZuiDeclarationExt.get(zuiDecl)
IF declExt.decl != NIL
# Re-use the declaration created previously.
decl = declExt.decl
ELSE
# Create a Declaration that holds the info about where the interface was
# used. itf.decl can be NIL for an iobject.
IF itf.name == NIL # TODO: cannot happen?
decl = Declaration.NEW("none")
ELSE
decl = Declaration.NEW(itf.name)
}
decl.type = itf
decl.zuiDecl = zuiDecl
declExt.decl = decl
}
IF $interfaces == NIL
$interfaces = NEW()
ELSE
# Only add it when it isn't there yet
FOR s IN $interfaces
IF s.type IS decl.type
ctx.error("Duplicate interface: " .. decl.name, zuiDecl)
RETURN
}
}
}
$interfaces.add(decl)
}
FUNC $findMember(string name, SymUse symUse) Declaration @replace
IF $scope == NIL
RETURN NIL
}
Declaration decl = Declaration.find($scope.declDict, name)
decl = $verifyAccess(decl, symUse)
IF decl == NIL && $parent != NIL
RETURN $parent.findMember(name, symUse)
}
RETURN decl
}
# Remove member |decl|.
PROC $removeMember(Declaration decl)
$scope.removeMember(decl)
}
# Add object member |decl|.
PROC $addObjectMember(Declaration decl)
$scope.addObjectMember(decl)
}
# Return TRUE if there are any members to be initialized, either in this
# class or in parents.
FUNC $hasMemberInits() bool
IF $memberInits != NIL || $ownMemberInits != NIL
RETURN TRUE
}
ClassType parent = $parent
WHILE parent != NIL
IF parent.memberInits != NIL
RETURN TRUE
}
parent = parent.parent
}
RETURN FALSE
}
# Add initialization of an object member, to be taken over by children.
PROC $addMemberInit(Zui.Declaration zuiDecl)
IF $memberInits == NIL
$memberInits = NEW()
}
$addSomeMemberInit(zuiDecl, $memberInits)
}
# Add initialization of an object member, not used by children.
PROC $addOwnMemberInit(Zui.Declaration zuiDecl)
IF $ownMemberInits == NIL
$ownMemberInits = NEW()
}
$addSomeMemberInit(zuiDecl, $ownMemberInits)
}
# Add initialization of an object member to |inits|.
PROC $addSomeMemberInit(Zui.Declaration zuiDecl, list<Zui.Declaration> inits)
FOR decl IN inits
IF decl IS zuiDecl
RETURN # already added
}
}
inits.add(zuiDecl)
}
# Remove object member |decl|.
PROC $removeObjectMember(Declaration decl)
$scope.removeObjectMember(decl)
}
# Search for |name| in the object member dict, checking for usage with
# |symUse|. Only search parents when |options.searchParent| is TRUE.
# Returns the first one that is visible. NIL if none are visible.
FUNC $findObjectMember(string name,
SymUse symUse,
MethodType.FindFuncOptions options,
bool inParent
) Declaration @replace
# Get all declarations for "name" and find the first one that is visible.
list<Declaration> declList = Declaration.findAll($getObjectDeclDict(), name)
Declaration decl
FOR d IN declList
# A declaration with @local cannot be used from a child class.
IF !inParent || !d.hasLocalAttr() || options.ignoreLocal
decl = $verifyAccess(d, symUse)
IF decl != NIL
BREAK
}
}
}
IF decl == NIL && options.searchParent && $parent != NIL
RETURN $parent.findObjectMember(name, symUse, options, TRUE)
}
RETURN decl
}
# Find the first method with name |method| that matches the arguments
# |argList|. Skip |skip.method|.
# The caller should probably check if there is another function
# that matches.
PROC $findMatchingMethods(string method,
bool objectMethod,
list<Declaration.C> argList,
MethodType.Skip skip,
MethodType.FindFuncOptions options,
int &undef,
multiDict<int, Declaration> funcs,
SContext ctx
) @replace
Declaration match
IF $scope != NIL
multiDict<string, Declaration> declList
IF objectMethod
IF $scope.objectMembers != NIL
declList = $scope.objectMembers
}
ELSEIF !options.skipShared
declList = $scope.declDict
}
IF declList != NIL
MethodType.findMatchingFunctionsInList(declList.get(method, NIL),
method, argList, skip, options, undef, funcs, ctx)
IF funcs.Size() > 0
match = funcs.get(funcs.keys()[0])[0]
}
}
}
IF match == NIL
&& $parent != NIL
&& options.searchParent
&& (skip == NIL || !skip.foundSkip)
# object Methods are copied from parent, don't look in parent
&& (!objectMethod || $ttype == Type.Enum.interface)
$parent.findMatchingMethods(method, objectMethod, argList,
skip, options, undef, funcs, ctx)
}
}
# Return the NEW() method that accepts a list of items or a dict with items.
FUNC $findNewFromContainerMethod(Zui.Expression expr,
Type.Enum ttype, string tName,
SContext ctx) MethodType @replace
MethodType found
IF $getObjectDeclDict() != NIL
FOR l IN $getObjectDeclDict().values()
FOR d IN l
IF d.name == "NEW"
list<Declaration.C> argList = d.type.getArgList()
IF argList.Size() == 1 && argList[0].type.ttype == ttype
IF found != NIL
IF ctx.doError()
ctx.error("More than one NEW method that accepts a "
.. tName, expr)
}
RETURN NIL
}
found = d.type
}
}
}
}
}
IF found == NIL && ctx.doError()
ctx.error("No NEW method that accepts a " .. tName, expr)
}
RETURN found
}
# Return TRUE when |child| is this class or a subclass of this class,
# directly or indirectly.
FUNC $hasSubclass(ClassType child) bool
RETURN $subClassDepth(child) >= 0
}
# Return how deep |child| can be found as a child of this class.
# Return -1 if not found.
FUNC $subClassDepth(ClassType child) int
IF child IS THIS
RETURN 0
}
IF $children != NIL
FOR m IN $children
int n = m.subClassDepth(child)
IF n >= 0
RETURN n + 1
}
}
}
RETURN -1
}
# Return the class type.
FUNC $getClassType(SContext ctx) ClassType @replace
RETURN THIS
}
# Get the dictionary with member Declarations.
FUNC $getDeclDict() multiDict<string, Declaration> @replace
IF $scope != NIL
RETURN $scope.declDict
}
RETURN NIL
}
FUNC $hasAnyMembers() bool @replace
RETURN TRUE
}
# Get the list of object member Declarations.
FUNC $getObjectDeclDict() multiDict<string, Declaration> @replace
IF $scope != NIL
RETURN $scope.getObjectDeclDict()
}
RETURN NIL
}
PROC $addChild(Type child, SContext ctx)
IF $children == NIL
$children = NEW()
# If a child is added code that uses this interface needs another pass.
# Objects change from object to iobject type.
$scope.setNeedPassInDependencies(ctx)
ELSE
# Only add it when it isn't there yet, implemented interfaces are added
# again each pass.
FOR s IN $children
IF s IS child
RETURN
}
}
}
$children.add(child)
}
# Return TRUE when this class has any children.
FUNC $hasChildren() bool
RETURN $children != NIL && $children.Size() > 0
}
# Return the index of |child| in the list of children, used as the index
# in the argument type lookup function.
# If |last| is TRUE then return the index plus children count plus one.
FUNC $childIndex(Type child, bool last) int
IF child == NIL
RETURN 0
}
IF child IS THIS && !last
# Index of the parent itself is zero.
RETURN 0
}
int i = $findChild(child, last)
IF i >= 0
# Index of a child is list index, plus one for non-abstract class.
RETURN i + ($isAbstract() ? 0 : 1)
}
IF child IS THIS
# Return children count, plus one for the parent itself.
RETURN -i
}
RETURN -1
}
# Find |child| in children, return its index.
# This works recursively.
# If |last| is TRUE then return the index plus children count plus one.
# Return -(number of children) - 1 when not found.
FUNC $findChild(Type child, bool last) int
int idx
IF $children != NIL
FOR c IN $children
IF c IS child && !last
RETURN idx
}
IF !c.isAbstract()
idx++
}
int ci = c.findChild(child, last)
IF ci >= 0
RETURN idx + ci
}
idx += -(ci + 1)
IF c IS child && last
RETURN idx
}
}
}
RETURN -(idx + 1)
}
PROC $imttUsed(SContext ctx)
IF $imttDecl == NIL
$imttDecl = NEW($name .. "__imtt")
}
ctx.addUsedItem($imttDecl)
# All _imt tables inside it are used for used classes.
$imtUsed(THIS, ctx)
}
PROC $imtUsed(Declaration itf, SContext ctx)
$usingInterface(itf, ctx)
IF $children != NIL
FOR child IN $children
child.imtUsed(itf, ctx)
}
}
}
# Return TRUE when this class or any child has a Finish() method.
FUNC $hasFinishMethod(SContext ctx) bool
IF Generate.getFinishMethod($scope, ctx) != NIL
RETURN TRUE
ELSEIF $children != NIL
FOR child IN $children
IF child.hasFinishMethod(ctx)
RETURN TRUE
}
}
}
RETURN FALSE
}
# Return the types as they are used in a typespec.
# foo<type0, type1>
FUNC $getTypespecType(int idx) Type @replace
IF $templateTypes != NIL && $templateTypes.Size() > idx
RETURN $templateTypes[idx].type
}
RETURN NIL
}
# Return TRUE when this is a class used to store an allocated variable, used
# in a USE &argument.
FUNC $isAllocType(SContext ctx) bool @replace
RETURN $allocDecl != NIL
}
# Return TRUE if this class is I.Iterable or a templated class of it.
FUNC $isIterable() bool
Declaration iterableDecl = ILoader.getIterableDecl()
RETURN THIS IS iterableDecl.type || ($scope.template != NIL
&& $scope.template.templateClass IS iterableDecl.type)
}
# Return TRUE if this class implements I.Iterable or one of the parents
# does.
FUNC $implementsIterable(SContext ctx) bool
IF $isIterable()
RETURN TRUE
}
FOR itf IN $interfaces ?: []
IF itf.type.getClassType(ctx).isIterable()
RETURN TRUE
}
}
IF $parent != NIL
RETURN $parent.implementsIterable(ctx)
}
RETURN FALSE
}
# Return TRUE if this class is I.Iterator or a templated class of it.
FUNC $isIterator() bool
Declaration iteratorDecl = ILoader.getIteratorDecl()
RETURN THIS IS iteratorDecl.type || ($scope.template != NIL
&& $scope.template.templateClass IS iteratorDecl.type)
}
# Return TRUE if this class implements I.Iterator or one of the parents
# does.
FUNC $implementsIterator(SContext ctx) bool
IF $isIterator()
RETURN TRUE
}
FOR itf IN $interfaces ?: []
IF itf.type.getClassType(ctx).isIterator()
RETURN TRUE
}
}
IF $parent != NIL
RETURN $parent.implementsIterator(ctx)
}
RETURN FALSE
}
# Return TRUE if this class is I.KeyIterator or a templated class of it.
FUNC $isKeyIterator() bool
Declaration iteratorDecl = ILoader.getKeyIteratorDecl()
RETURN THIS IS iteratorDecl.type || ($scope.template != NIL
&& $scope.template.templateClass IS iteratorDecl.type)
}
# Return TRUE if this class implements I.KeyIterator or one of the parents
# does.
FUNC $implementsKeyIterator(SContext ctx) bool
IF $isKeyIterator()
RETURN TRUE
}
FOR itf IN $interfaces ?: []
IF itf.type.getClassType(ctx).isKeyIterator()
RETURN TRUE
}
}
IF $parent != NIL
RETURN $parent.implementsKeyIterator(ctx)
}
RETURN FALSE
}
SHARED
int nextClassId
}
}