forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.cr
3393 lines (2813 loc) · 88.7 KB
/
types.cr
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
require "./syntax/ast"
module Crystal
# Abstract base class of all types
abstract class Type
include Annotatable
# Returns the program where this type belongs.
getter program
def initialize(@program : Program)
end
# Returns any doc comments associated to this type.
def doc : String?
nil
end
# Returns all locations where this type is declared
def locations : Array(Location)?
nil
end
# An opaque id of every type. 0 for Nil, non zero for others, so we can
# sort types by opaque_id and have Nil in the beginning.
def opaque_id
self.is_a?(NilType) ? 0_u64 : object_id
end
# The namespace this type belongs to. Every type belongs to
# a namespace, and, when not explicit, the namespace is the `Program` itself.
def namespace : ModuleType
program
end
# Yields each namespace this type belongs to, excluding the `Program` itself.
def each_namespace(& : ModuleType ->) : Nil
ns = self.namespace
until ns == program
yield ns
ns = ns.namespace
end
end
# Returns `true` if this type is abstract.
def abstract?
false
end
# Returns `true` if this type is abstract and it has no
# concrete subclasses. This can happen if this type has abstract
# subclasses, or non-abstract generic subclasses without instantiations.
def abstract_leaf?
type = self.devirtualize
case type
when GenericType
instances = type.instantiated_types
(type.abstract? && instances.empty?) || instances.all?(&.abstract_leaf?)
when GenericClassInstanceType
type.abstract? && type.subclasses.all?(&.abstract_leaf?)
when ClassType
type.abstract? && type.subclasses.all?(&.abstract_leaf?)
else
false
end
end
# Returns `true` if this type is a struct.
def struct?
false
end
# Returns `true` if this is an extern C struct or union (`extern_union?` tells which one)
def extern?
false
end
# Returns `true` if this is an extern C union (`extern?` will be `true` too)
def extern_union?
false
end
# Returns `true` if this type has the `@[Packed]` annotation on it
# (only applicable for C structs)
def packed?
false
end
# Returns `true` if this type inherits from `Reference` or if this
# is a union type where all types are reference types or nil.
# In this case this type can be represented with a single pointer.
def reference_like?
case self
when NilType, NilableType, NilableReferenceUnionType, ReferenceUnionType
true
when NonGenericClassType
!self.struct?
when GenericClassInstanceType
!self.struct?
when VirtualType
!self.struct?
else
false
end
end
# Returns the methods defined in this type, indexed by their name.
# This does not include methods defined in ancestors.
def defs : Hash(String, Array(DefWithMetadata))?
nil
end
# Returns all macros defines in this type, indexed by their name.
# This does not include methods defined in ancestors.
def macros : Hash(String, Array(Macro))?
nil
end
# Returns this type's metaclass, which holds class methods for this type.
getter metaclass : Type do
metaclass = MetaclassType.new(program, self)
initialize_metaclass(metaclass)
metaclass
end
# Initializes a metaclass.
# Some subtypes (classes) add an `allocate` method so a class can be instantiated.
protected def initialize_metaclass(metaclass)
# Nothing
end
# Returns `true` if this type can be assigned to an instance or class
# variable, or used in a generic type argument.
#
# As of now, abstract base type such as Object, Reference, Value,
# Int, and unbound generic types such as `Array(T)`, can't be stored.
def can_be_stored?
true
end
# Returns direct subclasses of this type.
def subclasses : Array(Type)
[] of Type
end
# Returns all subclasses of this type, including subclasses of
# subclasses recursively.
def all_subclasses
subclasses = [] of Type
append_subclasses(self, subclasses)
subclasses
end
private def append_subclasses(type, subclasses)
type.subclasses.each do |subclass|
subclasses << subclass
append_subclasses subclass, subclasses
end
end
# Returns `true` if this type has no subclasses.
def leaf?
subclasses.size == 0
end
def class?
false
end
def module?
false
end
def metaclass?
case self
when MetaclassType,
GenericClassInstanceMetaclassType, GenericModuleInstanceMetaclassType,
VirtualMetaclassType
true
else
false
end
end
def pointer?
self.is_a?(PointerInstanceType)
end
def nil_type?
self.is_a?(NilType)
end
def nilable?
self.is_a?(NilType) || (self.is_a?(UnionType) && self.union_types.any?(&.nil_type?))
end
def bool_type?
self.is_a?(BoolType)
end
def no_return?
self.is_a?(NoReturnType)
end
def virtual?
self.is_a?(VirtualType)
end
def virtual_metaclass?
self.is_a?(VirtualMetaclassType)
end
def proc?
self.is_a?(ProcInstanceType)
end
def void?
self.is_a?(VoidType)
end
def virtual_type
self
end
def virtual_type!
self
end
def instance_type
self
end
def generic_type
raise "BUG: #{self} doesn't implement generic_type"
end
def includes_type?(type)
self == type
end
def remove_typedef
self
end
# Returns the type that owns class vars for a type.
#
# This method returns self, but subclasses might override.
# For example, a metaclass' class_var_owner is the instance type.
def class_var_owner
self
end
def has_in_type_vars?(type)
false
end
# Should `new` be looked up in ancestors?
#
# This is `true` if this type doesn't define any
# `initialize` methods.
def lookup_new_in_ancestors?
false
end
# Returns the non-virtual type of a given type
# (returns self if self is already non-virtual)
def devirtualize
case self
when VirtualType
self.base_type
when VirtualMetaclassType
self.base_type.metaclass
else
self
end
end
def implements?(other_type : Type)
return true if self == other_type
other_type = other_type.remove_alias
case other_type
when UnionType
other_type.union_types.any? do |union_type|
implements?(union_type)
end
when VirtualType
implements?(other_type.base_type)
when VirtualMetaclassType
implements?(other_type.base_type.metaclass)
else
parents.try &.any? &.implements?(other_type)
end
end
def covariant?(other_type : Type)
implements?(other_type)
end
def filter_by(other_type)
restrict other_type, MatchContext.new(self, self, strict: true)
end
def filter_by_responds_to(name)
nil
end
def add_instance_var_initializer(name, value, meta_vars)
raise "BUG: #{self} doesn't implement add_instance_var_initializer"
end
def declare_instance_var(name, type : Type, annotations = nil)
var = MetaTypeVar.new(name)
var.owner = self
var.type = type
var.annotations = annotations
var.bind_to var
var.freeze_type = type
instance_vars[name] = var
end
# Determines if `self` can access *type* assuming it's a `protected` access.
# If `allow_same_namespace` is true (the default), `protected` also means
# the types are in the same namespace. Otherwise, it means they are just
# in the same type hierarchy.
def has_protected_access_to?(type, allow_same_namespace = true)
owner = self.devirtualize
type = type.devirtualize
# Allow two different generic instantiations
# of the same type to have protected access
type = type.generic_type.as(Type) if type.is_a?(GenericInstanceType)
owner = owner.generic_type.as(Type) if owner.is_a?(GenericInstanceType)
owner.implements?(type) ||
type.implements?(owner) ||
(allow_same_namespace && owner.same_namespace?(type))
end
# Returns true if `self` and *other* are in the same namespace.
def same_namespace?(other)
top_namespace(self) == top_namespace(other) ||
parents.try &.any? { |parent| parent.same_namespace?(other) }
end
private def top_namespace(type)
type = type.generic_type if type.is_a?(GenericInstanceType)
namespace = case type
when NamedType
type.namespace
when GenericClassInstanceType
type.namespace
else
nil
end
case namespace
when Program
type
when GenericInstanceType
top_namespace(namespace.generic_type)
when NamedType
top_namespace(namespace)
else
type
end
end
def types
raise "BUG: #{self} has no types"
end
def types?
nil
end
def parents
nil
end
def ancestors
ancestors = [] of Type
collect_ancestors(ancestors)
ancestors
end
protected def collect_ancestors(ancestors)
parents.try &.each do |parent|
ancestors << parent
parent.collect_ancestors(ancestors)
end
end
# Returns this type's superclass, or `nil` if it doesn't have one
def superclass : Type?
nil
end
def lookup_defs(name : String, lookup_ancestors_for_new : Bool = false)
all_defs = [] of Def
lookup_defs(name, all_defs, lookup_ancestors_for_new)
all_defs
end
def lookup_defs(name : String, all_defs : Array(Def), lookup_ancestors_for_new : Bool? = false)
self.defs.try &.[name]?.try &.each do |item|
all_defs << item.def unless all_defs.find(&.same?(item.def))
end
is_new = name == "new"
is_new_or_initialize = is_new || name == "initialize"
return if is_new_or_initialize && !all_defs.empty?
if !is_new_or_initialize || (lookup_ancestors_for_new || self.lookup_new_in_ancestors?)
if is_new
# For a `new` method we need to do this in case a `new` is defined
# in a module type
my_parents = instance_type.parents.try &.map(&.metaclass)
else
my_parents = parents
end
my_parents.try &.each do |parent|
old_size = all_defs.size
parent.lookup_defs(name, all_defs, lookup_ancestors_for_new)
# Don't lookup new or initialize in parents once we found some defs
break if is_new_or_initialize && all_defs.size > old_size
end
end
end
def lookup_defs_without_parents(name : String)
all_defs = [] of Def
lookup_defs_without_parents(name, all_defs)
all_defs
end
def lookup_defs_without_parents(name : String, all_defs : Array(Def))
self.defs.try &.[name]?.try &.each do |item|
all_defs << item.def unless all_defs.find(&.same?(item.def))
end
end
def lookup_defs_with_modules(name)
if (list = self.defs.try &.[name]?) && !list.empty?
return list.map(&.def)
end
parents.try &.each do |parent|
next unless parent.module?
parent_defs = parent.lookup_defs_with_modules(name)
return parent_defs unless parent_defs.empty?
end
[] of Def
end
def lookup_first_def(name, block)
block = !!block
defs.try &.[name]?.try &.find(&.yields.==(block)).try &.def
end
def has_def?(name)
has_def_without_parents?(name) || parents.try(&.any?(&.has_def?(name)))
end
def has_def_without_parents?(name)
defs.try(&.has_key?(name))
end
record DefInMacroLookup
# Looks up a macro with the given name and matching the given args
# and named_args. Returns:
# - a `Macro`, if found
# - `nil`, if not found
# - `DefInMacroLookup` if not found and a Def was found instead
#
# In the case of `DefInMacroLookup`, it means that macros shouldn't
# be looked up in implicit enclosing scopes such as Object
# or the Program.
def lookup_macro(name, args : Array, named_args)
# Macros are always stored in a type's metaclass
macros_scope = self.metaclass? ? self : self.metaclass
if macros = macros_scope.macros.try &.[name]?
match = macros.find &.matches?(args, named_args)
return match if match
end
# First check if there are defs at this scope with that name.
# If so, make that a priority in the lookup and don't consider
# macro matches.
if has_def_without_parents?(name)
return DefInMacroLookup.new
end
# We need to go through the instance type because of module
# inclusion and inheritance.
instance_type.parents.try &.each do |parent|
# Make sure to start the search in the metaclass if we are a metaclass
parent = parent.metaclass if self.metaclass?
parent_macro = parent.lookup_macro(name, args, named_args)
return parent_macro if parent_macro
end
nil
end
# Looks up macros with the given name. Returns:
# - an Array of Macro if found
# - `nil` if not found
# - `DefInMacroLookup` if not found and some Defs were found instead
def lookup_macros(name)
# Macros are always stored in a type's metaclass
macros_scope = self.metaclass? ? self : self.metaclass
if macros = macros_scope.macros.try &.[name]?
return macros
end
if has_def_without_parents?(name)
return DefInMacroLookup.new
end
# We need to go through the instance type because of module
# inclusion and inheritance.
instance_type.parents.try &.each do |parent|
# Make sure to start the search in the metaclass if we are a metaclass
parent = parent.metaclass if self.metaclass?
parent_macros = parent.lookup_macros(name)
return parent_macros if parent_macros
end
nil
end
def add_including_type(mod)
raise "BUG: #{self} doesn't implement add_including_type"
end
def including_types
raise "BUG: #{self} doesn't implement including_types"
end
# Returns `true` if this type can have instance vars.
# Primitive types, and types like Reference and Object,
# can't have instance vars.
def allows_instance_vars?
case self
when program.object, program.value, program.struct,
program.number, program.int, program.float,
PrimitiveType, program.reference
false
else
true
end
end
def instance_vars
raise "BUG: #{self} doesn't implement instance_vars"
end
def class_vars
raise "BUG: #{self} doesn't implement class_vars"
end
def all_instance_vars
if superclass = self.superclass
superclass.all_instance_vars.merge(instance_vars)
else
instance_vars
end
end
def all_class_vars
all_class_vars = {} of String => MetaTypeVar
all_class_vars.merge!(class_vars)
parents.try &.each do |parent|
all_class_vars.merge!(parent.all_class_vars)
end
all_class_vars
end
def index_of_instance_var(name)
if superclass = self.superclass
index = superclass.index_of_instance_var(name)
if index
index
else
index = instance_vars.index { |k, v| k == name }
if index
superclass.all_instance_vars_count + index
else
nil
end
end
else
instance_vars.index { |k, v| k == name }
end
end
def lookup_instance_var(name)
lookup_instance_var?(name).not_nil!
end
def lookup_instance_var?(name)
superclass.try(&.lookup_instance_var?(name)) ||
instance_vars[name]?
end
def lookup_class_var?(name)
nil
end
def lookup_class_var(name)
raise "BUG: #{self} doesn't implement lookup_class_var"
end
def has_instance_var_initializer?(name)
false
end
def all_instance_vars_count
(superclass.try(&.all_instance_vars_count) || 0) + instance_vars.size
end
def add_subclass(subclass)
raise "BUG: #{self} doesn't implement add_subclass"
end
# Replace type parameters in this type with the type parameters
# of the given *instance* type.
def replace_type_parameters(instance) : Type
self
end
def depth
0
end
def type_desc
to_s
end
def remove_alias
self
end
def remove_alias_if_simple
self
end
def remove_indirection
self
end
def remove_literal
self
end
def generic_nest
0
end
def double_variadic?
false
end
def splat_index
nil
end
def type_vars
raise "BUG: #{self} doesn't implement type_vars"
end
def unbound?
false
end
def private?
false
end
def private=(set_private)
end
# Returns true if *name* if an unbound type variable in this (generic) type.
def type_var?(name)
false
end
# Returns the type that has to be used in sizeof and instance_sizeof computations
def sizeof_type
if struct?
# In the case of an abstract struct we want to consider the union type
# of all subtypes (if it's not abstract it's concrete and this will return self)
virtual_type.remove_indirection
else
devirtualize
end
end
def get_instance_var_initializer(name)
nil
end
# Checks whether an exception needs to be raised because of a restriction
# failure. Only overwritten by literal types (NumberLiteralType and
# SymbolLiteralType) when they produce an ambiguous call.
def check_restriction_exception
nil
end
# Yields self and returns true if the block returns a truthy value.
# UnionType overrides it and yields all types in turn and returns
# true if for each of them the block returns true.
def all?
(yield self) ? true : false
end
def to_s(*, generic_args : Bool = true)
String.build do |io|
to_s_with_options io, generic_args: generic_args
end
end
def inspect(io : IO) : Nil
to_s(io)
end
def to_s(io : IO) : Nil
to_s_with_options(io)
end
abstract def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen : Bool = false) : Nil
def pretty_print(pp)
pp.text to_s
end
end
# A type that has a name and can be inside a namespace.
# For example, given `class Foo::Bar`, `Foo` is the namespace and `Bar` is the name.
#
# There are other types that have a name but it can be deduced from other(s) type(s),
# so they don't inherit NamedType: a union type, a metaclass, etc.
abstract class NamedType < Type
getter namespace : ModuleType
getter name : String
getter locations : Array(Location)?
property doc : String?
property? private : Bool = false
def initialize(program, @namespace, @name)
super(program)
end
# Adds a location to this type.
def add_location(location : Location)
locations = @locations ||= [] of Location
locations << location
end
getter(types) { {} of String => NamedType }
def types?
@types
end
def append_full_name(io : IO, codegen : Bool = false) : Nil
case namespace
when Program
# Skip
when FileModule
# For codegen we need the filename to distinguish it from other
# types, but in macros we can't use that because it won't parse
if codegen
namespace.to_s_with_options(io, generic_args: false, codegen: codegen)
io << "::"
end
else
namespace.to_s_with_options(io, generic_args: false, codegen: codegen)
io << "::"
end
io << @name
end
def full_name
String.build { |io| append_full_name(io) }
end
def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen : Bool = false) : Nil
append_full_name(io, codegen: codegen)
end
end
# A Def with some metadata to speed up matching it against
# a call signature, or against other defs:
# - max_size: the maximum number of arguments that can be passed to the method
# - min_size: the minimum number of arguments that can be passed to the method
# - yields: whether the method has a block
record DefWithMetadata,
min_size : Int32,
max_size : Int32,
yields : Bool,
def : Def do
def self.new(a_def : Def)
min_size, max_size = a_def.min_max_args_sizes
new min_size, max_size, !!a_def.yields, a_def
end
end
# A macro hook (:inherited, :included, :extended)
record Hook, kind : Symbol, macro : Macro
# The key by which instantiated methods are cached.
#
# For example, given:
#
# ```
# def foo(x, y) # def object id: 1234
# x + y
# end
#
# foo(1, 2.5)
# ```
#
# When `foo(1)` is analyzed the argument types are [Int32, Float64],
# and so we instantiate the method with those types (in this case there's
# no block type nor named argument types). We remember this instantiation
# with a key that includes the def's object id, argument types, etc., so
# when a call with the same target Def, argument types, etc., is found
# we don't need to re-analyze it.
record DefInstanceKey,
def_object_id : UInt64,
arg_types : Array(Type),
block_type : Type?,
named_args : Array(NamedArgumentType)?
# A type that holds Def instantiations (defs where the argument types
# are fixed). Some types don't support having def instances, for example
# union types.
module DefInstanceContainer
getter(def_instances) { {} of DefInstanceKey => Def }
def add_def_instance(key, typed_def)
def_instances[key] = typed_def
end
def lookup_def_instance(key)
def_instances[key]?
end
end
# Base type for all module-like types (modules, classes, structs, enums).
abstract class ModuleType < NamedType
getter defs : Hash(String, Array(DefWithMetadata))?
getter macros : Hash(String, Array(Macro))?
getter hooks : Array(Hook)?
getter(parents) { [] of Type }
def add_def(a_def)
a_def.owner = self
item = DefWithMetadata.new(a_def)
defs = (@defs ||= {} of String => Array(DefWithMetadata))
list = defs[a_def.name] ||= [] of DefWithMetadata
list.each_with_index do |ex_item, i|
if item.restriction_of?(ex_item, self)
if ex_item.restriction_of?(item, self)
# The two defs have the same signature so item overrides ex_item.
list[i] = item
a_def.previous = ex_item
a_def.doc ||= ex_item.def.doc
ex_item.def.next = a_def
return ex_item.def
else
# item has a new signature, stricter than ex_item.
list.insert(i, item)
return nil
end
end
end
# item has a new signature, less strict than the existing defs with same name.
list << item
nil
end
def add_macro(a_macro)
a_macro.owner = self
case a_macro.name
when "inherited"
return add_hook :inherited, a_macro
when "included"
return add_hook :included, a_macro
when "extended"
return add_hook :extended, a_macro
when "method_added"
return add_hook :method_added, a_macro, args_size: 1
when "method_missing"
if a_macro.args.size != 1
raise TypeException.new "macro 'method_missing' expects 1 argument (call)"
end
else
# normal macro
end
macros = (@macros ||= {} of String => Array(Macro))
array = (macros[a_macro.name] ||= [] of Macro)
index = array.index { |existing_macro| a_macro.overrides?(existing_macro) }
if index
# a_macro has the same signature of an existing macro, we override it.
a_macro.doc ||= array[index].doc
array[index] = a_macro
else
# a_macro has a new signature, add it with the others.
array << a_macro
end
end
def add_hook(kind, a_macro, args_size = 0)
if a_macro.args.size != args_size
case args_size
when 0
raise TypeException.new "macro '#{kind}' must not have arguments"
when 1
raise TypeException.new "macro '#{kind}' must have a argument"
else
raise TypeException.new "macro '#{kind}' must have #{args_size} arguments"
end
end
hooks = @hooks ||= [] of Hook
hooks << Hook.new(kind, a_macro)
end
def filter_by_responds_to(name)
has_def?(name) ? self : nil
end
def include(mod)
if mod == self
raise TypeException.new "cyclic include detected"
elsif mod.ancestors.includes?(self)
raise TypeException.new "cyclic include detected"
else
unless parents.includes?(mod)
parents.insert 0, mod
mod.add_including_type(self)
end
end
end
def type_desc
"module"
end
end
# A type that can have class variables.
module ClassVarContainer
getter(class_vars) { {} of String => MetaTypeVar }
def class_vars?
@class_vars
end
def lookup_class_var(name)
lookup_class_var?(name).not_nil!
end
def lookup_class_var?(name)
class_var = @class_vars.try &.[name]?
return class_var if class_var
ancestors.each do |ancestor|
next unless ancestor.is_a?(ClassVarContainer)
class_var = ancestor.lookup_class_var?(name)
if class_var
var = MetaTypeVar.new(name, class_var.type)
var.owner = self
var.thread_local = class_var.thread_local?
var.initializer = class_var.initializer
var.bind_to(class_var)
self.class_vars[name] = var
return var
end
end
nil
end