-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
guts.pir
1568 lines (1303 loc) Β· 40.7 KB
/
guts.pir
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
## $Id$
=head1 NAME
src/builtins/guts.pir - subs that are part of the internals, not for users
=head1 SUBS
=over 4
=item !EXPORT(symbols, from :named('from') [, to :named('to')] )
Export symbols in namespace C<from> to the namespace given by C<to>.
If C<to> isn't given, then exports into the HLL global namespace.
This function differs somewhat from Parrot's C<Exporter> PMC in that
it understands how to properly merge C<MultiSub> PMCs.
=cut
.namespace []
.sub '!EXPORT'
.param string symbols
.param pmc from :named('from')
.param pmc to :named('to') :optional
.param int has_to :opt_flag
.param int to_p6_multi :named('to_p6_multi') :optional
if has_to goto have_to
to = get_hll_namespace
have_to:
.local pmc list
list = split ',', symbols
list_loop:
unless list goto list_end
.local string symbol
.local pmc value
symbol = shift list
value = from[symbol]
$I0 = isa value, 'MultiSub'
unless $I0 goto store_value
if to_p6_multi != 1 goto no_convert
$P0 = value[0]
'!TOPERL6MULTISUB'($P0)
value = from[symbol]
no_convert:
$P0 = to[symbol]
if null $P0 goto store_value
$I0 = isa $P0, 'MultiSub'
unless $I0 goto err_type_conflict
$I0 = elements $P0
splice $P0, value, $I0, 0
goto list_loop
store_value:
to[symbol] = value
goto list_loop
list_end:
.return ()
err_type_conflict:
$S0 = concat "Unable to add Multisub '", symbol
$S0 .= "' to existing value"
die $S0
.end
=item !CALLMETHOD('method', obj)
Invoke a method on a possibly foreign object. If the object
supports the requested method, we use it, otherwise we assume
the object is foreign and try using the corresponding method
from C<Any>.
=cut
.namespace []
.sub '!CALLMETHOD'
.param string method
.param pmc obj
$I0 = isa obj, 'Perl6Scalar'
if $I0 goto any_method
$I0 = can obj, method
unless $I0 goto any_method
.tailcall obj.method()
any_method:
.local pmc anyobj
anyobj = get_global '$!ANY'
unless null anyobj goto any_method_1
anyobj = new ['Any']
set_global '$!ANY', anyobj
any_method_1:
$P0 = find_method anyobj, method
.tailcall obj.$P0()
.end
=item !dispatch_method_indirect
Does an indirect method dispatch.
=cut
.sub '!dispatch_method_indirect'
.param pmc obj
.param pmc methodish
.param pmc pos_args :slurpy
.param pmc name_args :slurpy :named
$P0 = get_hll_global 'Callable'
$I0 = $P0.'ACCEPTS'(methodish)
unless $I0 goto candidate_list
.tailcall methodish(obj, pos_args :flat, name_args :flat :named)
candidate_list:
$P0 = root_new ['parrot';'P6Invocation'], methodish
.tailcall $P0(obj, pos_args :flat, name_args :flat :named)
.end
=item !dispatch_dispatcher_parallel
Does a parallel method dispatch over an existing dispatcher. Just invokes the normal
dispatcher for each thingy we're dispatching over.
=cut
.sub '!dispatch_dispatcher_parallel'
.param pmc invocanty
.param string dispatcher
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
.local pmc it, result, disp
disp = find_name dispatcher
result = new ['Perl6Array']
invocanty = invocanty.'list'()
it = iter invocanty
it_loop:
unless it goto it_loop_done
$P0 = shift it
$P0 = disp($P0, pos_args :flat, named_args :flat :named)
$P0 = $P0.'Scalar'()
result.'push'($P0)
goto it_loop
it_loop_done:
.return (result)
.end
=item !dispatch_method_parallel
Does a parallel method dispatch. Invokes the method for each thing in the
array of invocants.
=cut
.sub '!dispatch_method_parallel'
.param pmc invocanty
.param string name
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
.local pmc it, result
result = new ['Perl6Array']
invocanty = invocanty.'list'()
it = iter invocanty
it_loop:
unless it goto it_loop_done
$P0 = shift it
$P0 = $P0.name(pos_args :flat, named_args :flat :named)
$P0 = $P0.'Scalar'()
result.'push'($P0)
goto it_loop
it_loop_done:
.return (result)
.end
=item !VAR
Helper function for implementing the VAR and .VAR macros.
=cut
.sub '!VAR'
.param pmc variable
$I0 = isa variable, 'Perl6Scalar'
unless $I0 goto nothing
$P0 = root_new ['parrot';'MutableVAR'], variable
.return ($P0)
nothing:
.return (variable)
.end
=item !SAMETYPE_EXACT
Takes two types and returns true if they match exactly (not accounting for any
subtyping relations, etc).
=cut
.sub '!SAMETYPE_EXACT'
.param pmc t1
.param pmc t2
# If they have equal address, obviously the same.
.local pmc t1meta, t2meta
t1meta = t1.'HOW'()
t2meta = t2.'HOW'()
eq_addr t1meta, t2meta, same
# If they are junctions, compare inside them recursively.
$I0 = isa t1, 'Junction'
unless $I0 goto not_junc
$I1 = isa t2, 'Junction'
unless $I0 == $I1 goto not_junc
.local pmc j1, j2
.local int max, i
j1 = t1.'eigenstates'()
j2 = t1.'eigenstates'()
max = elements j1
i = 0
junc_loop:
if i >= max goto junc_loop_end
$P0 = j1[i]
$P1 = j2[i]
$I0 = '!SAMETYPE_EXACT'($P0, $P1)
unless $I0 goto not_same
inc i
goto junc_loop
junc_loop_end:
not_junc:
not_same:
.return(0)
same:
.return (1)
.end
=item !CREATE_SUBSET_TYPE
Creates a subset type. Basically, we make an anonymous subclass of the
original type, attach the refinement and override ACCEPTS. We also chase up
to find a real, non-subtype and stash that away for fast access later.
=cut
.sub '!CREATE_SUBSET_TYPE'
.param pmc refinee
.param pmc refinement
.local pmc p6meta
p6meta = get_hll_global ['Perl6Object'], '$!P6META'
# Check if the refinee is a refinement type itself; if so, get the real
# base type we're refining.
.local pmc real_type, real_type_pc
real_type = getprop 'subtype_realtype', refinee
unless null $P0 goto got_real_type
real_type = refinee
got_real_type:
# Create subclass. If it's a role, pun it.
.local pmc parrot_class, type_obj, subset
type_obj = refinee
$I0 = isa type_obj, 'Perl6Role'
unless $I0 goto ambig_role_done
type_obj = type_obj.'!select'()
ambig_role_done:
$I0 = isa type_obj, 'P6role'
unless $I0 goto role_done
type_obj = type_obj.'!pun'()
role_done:
parrot_class = p6meta.'get_parrotclass'(type_obj)
subset = subclass parrot_class
# Override accepts.
.local pmc parrotclass
.const 'Sub' $P0 = "!SUBTYPE_ACCEPTS"
subset.'add_method'('ACCEPTS', $P0)
.const 'Sub' $P1 = "!SUBTYPE_PROTOOVERRIDES"
subset.'add_method'('PROTOOVERRIDES', $P1)
# It's an abstraction.
$P0 = get_hll_global 'Abstraction'
$P0 = $P0.'!select'()
subset.'add_role'($P0)
# Register it, creating a proto-object.
subset = p6meta.'register'(subset)
# Mark it a subtype and stash away real type, refinee and refinement.
$I0 = isa real_type, 'Perl6Role'
unless $I0 goto real_type_done
real_type = real_type.'!select'()
real_type_done:
setprop subset, 'subtype_realtype', real_type
setprop subset, 'subtype_refinement', refinement
setprop subset, 'subtype_refinee', refinee
.return (subset)
.end
.sub "!SUBTYPE_ACCEPTS" :anon :method
.param pmc topic
# Get refinement and check against that.
.local pmc refinement
refinement = getprop 'subtype_refinement', self
$P0 = refinement(topic)
unless $P0 goto false
# Recurse up the tree.
.local pmc refinee
refinee = getprop 'subtype_refinee', self
$P0 = refinee.'ACCEPTS'(topic)
unless $P0 goto false
true:
$P0 = get_hll_global ['Bool'], 'True'
.return ($P0)
false:
$P0 = get_hll_global ['Bool'], 'False'
.return ($P0)
.end
.sub '!SUBTYPE_PROTOOVERRIDES' :anon :method
.return ('new', 'ACCEPTS')
.end
=item !TOPERL6MULTISUB
At the moment, we don't have the abilility to have Parrot use our own MultiSub
type, nor are we ready to (because built-ins need to get Perl 6 signatures
first). So for now we just transform multis in user code like this.
=cut
.sub '!TOPERL6MULTISUB'
.param pmc sub
# Look up what's currently installed in the namespace for this sub; if it
# is already a Perl6MultiSub, leave it.
.local pmc namespace, current_thing
.local string name
namespace = sub.'get_namespace'()
name = sub
current_thing = namespace[name]
if null current_thing goto error
$S0 = typeof current_thing
if $S0 == 'MultiSub' goto not_perl6_multisub
.return()
# It's not a Perl6MultiSub, create one and put contents into it.
not_perl6_multisub:
.local pmc p6multi, sub_iter
p6multi = root_new ['parrot';'Perl6MultiSub']
sub_iter = iter current_thing
iter_loop:
unless sub_iter goto iter_loop_end
$P0 = shift sub_iter
push p6multi, $P0
goto iter_loop
iter_loop_end:
# Nor replace the current thing with the new data structure.
copy current_thing, p6multi
.return()
error:
'die'('Sub lookup failed')
.end
=item !clone_multi_for_lexical
=cut
.sub '!clone_multi_for_lexical'
.param pmc existing
if null existing goto fresh
unless existing goto fresh
$P0 = existing.'clone'()
.return ($P0)
fresh:
$P0 = root_new ['parrot';'Perl6MultiSub']
.return ($P0)
.end
=item !UNIT_START
=cut
.sub '!UNIT_START'
.param pmc unitmain
.param pmc args
args = 'list'(args)
if args goto start_main
.tailcall unitmain()
start_main:
## We're running as main program
## Remove program argument (0) and put it in $*PROGRAM_NAME, then set up
## @ARGS global.
$P0 = shift args
set_hll_global '$PROGRAM_NAME', $P0
args = args.'Array'()
set_hll_global '@ARGS', args
## run unitmain
.local pmc result, MAIN
result = unitmain()
## if there's a MAIN sub in unitmain's namespace, run it also
$P0 = unitmain.'get_namespace'()
MAIN = $P0['MAIN']
if null MAIN goto done
args = get_hll_global '@ARGS'
result = MAIN(args :flat)
done:
.return (result)
.end
=item !capture
Combine slurpy positional and slurpy named args into a list.
Note that original order may be lost -- that's the nature
of captures.
=cut
.sub '!capture'
.param pmc args :slurpy
.param pmc options :slurpy :named
unless options goto done
.local pmc it
it = iter options
iter_loop:
unless it goto done
$S0 = shift it
$P0 = options[$S0]
$P0 = 'infix:=>'($S0, $P0)
push args, $P0
goto iter_loop
done:
.tailcall args.'list'()
.end
=item !ADDTOROLE
Adds a given role initializing multi-variant to a Role object, creating it
and putting it in the namespace if it doesn't already exist.
=cut
.sub '!ADDTOROLE'
.param pmc variant
# Get short name of role.
.local pmc ns
.local string short_name
ns = variant.'get_namespace'()
ns = ns.'get_name'()
short_name = pop ns
$P0 = box short_name
setprop variant, "$!shortname", $P0
$I0 = index short_name, '['
if $I0 == -1 goto have_short_name
short_name = substr short_name, 0, $I0
have_short_name:
# See if we have a Role object already.
.local pmc role_obj
role_obj = get_root_global ns, short_name
if null role_obj goto need_role_obj
$I0 = isa role_obj, 'NameSpace'
unless $I0 goto have_role_obj
need_role_obj:
role_obj = new ['Perl6Role']
transform_to_p6opaque role_obj
set_root_global ns, short_name, role_obj
$P0 = box short_name
setattribute role_obj, "$!shortname", $P0
have_role_obj:
# Add this variant.
role_obj.'!add_variant'(variant)
.end
=item !meta_create(type, name, also)
Create a metaclass object for C<type> with the given C<name>.
This simply creates a handle on which we can hang methods, attributes,
traits, etc. -- the class itself isn't created until the class
is composed (see C<!meta_compose> below).
=cut
.sub '!meta_create'
.param string type
.param string name
.param int also
.local pmc nsarray
$P0 = get_hll_global [ 'Perl6';'Compiler' ], 'parse_name'
$P1 = null
nsarray = $P0($P1, name)
if type == 'package' goto package
if type == 'module' goto package
if type == 'class' goto class
if type == 'grammar' goto class
if type == 'role' goto role
'die'("Unsupported package declarator ", type)
package:
$P0 = get_hll_namespace nsarray
.return ($P0)
class:
.local pmc parrotclass, metaclass, ns
ns = get_hll_namespace nsarray
if also goto is_also
parrotclass = newclass ns
$P0 = box type
setprop parrotclass, 'pkgtype', $P0
'!set_resolves_list'(parrotclass)
metaclass = new ['ClassHOW']
setattribute metaclass, 'parrotclass', parrotclass
.return (metaclass)
is_also:
parrotclass = get_class ns
metaclass = getprop 'metaclass', parrotclass
.return (metaclass)
role:
# This is a little fun. We only want to create the Parrot role and suck
# in the methods once per role definition. We do this and it is attached to
# the namespace. Then we attach this "master role" to a new one we create
# per invocation, so the methods can be newclosure'd and added into it in
# the body.
.local pmc info, parrotrole
ns = get_hll_namespace nsarray
parrotrole = get_class ns
unless null parrotrole goto have_role
info = root_new ['parrot';'Hash']
$P0 = nsarray[-1]
info['name'] = $P0
info['namespace'] = nsarray
parrotrole = root_new ['parrot';'P6role'], info
have_role:
# Copy list of roles done by the original role into this specific
# one.
.local pmc specific_role, tmp, it
specific_role = root_new ['parrot';'P6role']
setprop specific_role, '$!orig_role', parrotrole
tmp = parrotrole.'roles'()
it = iter tmp
roles_loop:
unless it goto roles_loop_end
tmp = shift it
specific_role.'add_role'(tmp)
goto roles_loop
roles_loop_end:
# Now create a meta-object (RoleHOW) to package this all up in.
.local pmc metaclass
metaclass = new ['RoleHOW']
setprop specific_role, 'metaclass', metaclass
setattribute metaclass, 'parrotclass', specific_role
setattribute metaclass, 'protoobject', specific_role
setattribute metaclass, 'shortname', $P0
$P1 = box name
setattribute metaclass, 'longname', $P1
.return (metaclass)
.end
=item !meta_compose(Class metaclass)
Compose the class. This includes resolving any inconsistencies
and creating the protoobjects.
=cut
.sub '!meta_compose'
.param pmc metaclass
.local pmc p6meta
p6meta = get_hll_global ['Perl6Object'], '$!P6META'
# If it's a RoleHOW or otherwise just not a ClassHOW, nothing to do.
$I0 = isa metaclass, 'RoleHOW'
if $I0 goto no_pkgtype
$I0 = isa metaclass, 'ClassHOW'
unless $I0 goto no_pkgtype
# Extract the parrotclass form the metaclass.
.local pmc parrotclass
parrotclass = getattribute metaclass, 'parrotclass'
# Parrot handles composing methods into roles, but we need to handle the
# attribute composition ourselves.
.local pmc roles, roles_it
roles = getprop '@!roles', parrotclass
if null roles goto roles_it_loop_end
roles = '!get_flattened_roles_list'(roles)
roles_it = iter roles
roles_it_loop:
unless roles_it goto roles_it_loop_end
$P0 = shift roles_it
$I0 = does parrotclass, $P0
if $I0 goto roles_it_loop
parrotclass.'add_role'($P0)
'!compose_role_attributes'(parrotclass, $P0)
goto roles_it_loop
roles_it_loop_end:
# We may need to set up invoke vtable if postcircumfix:<( )>
# is implemented.
'!setup_invoke_vtable'(metaclass)
# Create proto-object with default parent being Any or Grammar, unless
# there already is a parent.
.local pmc proto
$P0 = parrotclass.'parents'()
$I0 = elements $P0
if $I0 goto register_parent_set
$S0 = 'Any'
$P0 = getprop 'pkgtype', parrotclass
if null $P0 goto no_pkgtype
if $P0 != 'grammar' goto register
$S0 = 'Grammar'
register:
proto = p6meta.'register'(parrotclass, 'parent'=>$S0, 'how'=>metaclass)
goto have_proto
register_parent_set:
proto = p6meta.'register'(parrotclass, 'how'=>metaclass)
have_proto:
transform_to_p6opaque proto
.return (proto)
no_pkgtype:
.return (metaclass)
.end
=item !setup_invoke_vtable
If we override postcircumfix:<( )> then also add a
vtable override for invoke.
=cut
.sub '!setup_invoke_vtable'
.param pmc metaclass
.local pmc parrotclass
parrotclass = getattribute metaclass, 'parrotclass'
$P0 = parrotclass.'methods'()
$P0 = $P0['postcircumfix:( )']
if null $P0 goto no_invoke
.const 'Sub' $P1 = '!invoke_vtable_override_helper'
parrotclass.'add_vtable_override'('invoke', $P1)
no_invoke:
.end
.sub '' :subid('!invoke_vtable_override_helper')
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named
$P0 = getinterp
$P0 = $P0['sub']
$P0 = getprop '$!self', $P0
.tailcall $P0.'postcircumfix:( )'(pos_args :flat, named_args :flat :named)
.end
=item !get_flattened_roles_list
Flattens out the list of roles.
=cut
.sub '!get_flattened_roles_list'
.param pmc unflat_list
.local pmc flat_list, it, cur_role, nested_roles, nested_it
flat_list = root_new ['parrot';'ResizablePMCArray']
it = iter unflat_list
it_loop:
unless it goto it_loop_end
cur_role = shift it
$I0 = isa cur_role, 'Role'
unless $I0 goto error_not_a_role
push flat_list, cur_role
nested_roles = getprop '@!roles', cur_role
if null nested_roles goto it_loop
nested_roles = '!get_flattened_roles_list'(nested_roles)
nested_it = iter nested_roles
nested_it_loop:
unless nested_it goto it_loop
$P0 = shift nested_it
push flat_list, $P0
goto nested_it_loop
it_loop_end:
.return (flat_list)
error_not_a_role:
'die'('Can not compose a non-role.')
.end
=item !meta_attribute(metaclass, name, itypename [, 'type'=>type] )
Add attribute C<name> to C<metaclass> with the given C<itypename>
and C<type>.
=cut
.sub '!meta_attribute'
.param pmc metaclass
.param string name
.param string itypename :optional
.param int has_itypename :opt_flag
.param pmc attr :slurpy :named
# twigil handling (for has &!foo, we just get name as !foo)
.local int offset
.local string twigil
offset = 1
$S0 = substr name, 0, 1
if $S0 != '!' goto offset_done
offset = 0
offset_done:
twigil = substr name, offset, 1
if twigil == '.' goto twigil_public
if twigil == '!' goto twigil_done
substr name, offset, 0, '!'
goto twigil_done
twigil_public:
substr name, offset, 1, '!'
twigil_done:
# In the future, we'll want to have just called metaclass.add_attribute(...)
# here and let it handle all of this, but we ain't quite ready for that yet.
$I0 = isa metaclass, 'P6metaclass'
unless $I0 goto got_parrot_class
metaclass = getattribute metaclass, 'parrotclass'
got_parrot_class:
$P0 = metaclass.'attributes'()
$I0 = exists $P0[name]
if $I0 goto attr_exists
addattribute metaclass, name
$P1 = getprop '@!attribute_list', metaclass
unless null $P1 goto have_attrlist
$P1 = root_new ['parrot';'ResizableStringArray']
setprop metaclass, '@!attribute_list', $P1
have_attrlist:
push $P1, name
$P0 = metaclass.'attributes'()
attr_exists:
.local pmc attrhash, it
attrhash = $P0[name]
# Set any itype for the attribute.
unless has_itypename goto itype_done
.local pmc itype
if itypename == 'Perl6Scalar' goto itype_pmc
itype = get_class itypename
goto have_itype
itype_pmc:
$P0 = get_root_namespace ['parrot';'Perl6Scalar']
itype = get_class $P0
have_itype:
attrhash['itype'] = itype
itype_done:
# and set any other attributes that came in via the slurpy hash
it = iter attr
attr_loop:
unless it goto attr_done
$S0 = shift it
$P0 = attr[$S0]
attrhash[$S0] = $P0
goto attr_loop
attr_done:
# Anything to do with handles?
$P0 = attr['handles']
if null $P0 goto handles_done
# For the handles trait verb, we may have got a name or a list of names.
# If so, just generate methods with those names. Otherwise, need to store
# them as a property on the metaclass, so the dispatcher can smart-match
# against them later. Also, the % syntax is spec'd as reserved, so we give
# an error on that for now.
.const 'Sub' handles = '!handles'
.local pmc handles_it
$S0 = substr name, 0, 1
if $S0 == '%' goto reserved_syntax_error
$I0 = isa $P0, 'Str'
if $I0 goto simple_handles
$I0 = isa $P0, 'List'
if $I0 goto simple_handles
$I0 = isa $P0, 'Perl6Pair'
if $I0 goto simple_handles
.local pmc class_handles_list, handles_hash
class_handles_list = getprop '@!handles_dispatchers', metaclass
unless null class_handles_list goto have_class_handles_list
class_handles_list = root_new ['parrot';'ResizablePMCArray']
setprop metaclass, '@!handles_dispatchers', class_handles_list
have_class_handles_list:
handles_hash = root_new ['parrot';'Hash']
handles_hash['attrname'] = name
handles_hash['match_against'] = $P0
push class_handles_list, handles_hash
goto handles_done
simple_handles:
$P0 = 'list'($P0)
handles_it = iter $P0
handles_loop:
.local string visible_name
.local pmc orig_name
unless handles_it goto handles_done
$P0 = clone handles
$P1 = box name
setprop $P0, 'attrname', $P1
$P1 = shift handles_it
$I0 = isa $P1, 'Perl6Pair'
if $I0 goto handles_pair
visible_name = $P1
orig_name = $P1
goto naming_done
handles_pair:
visible_name = $P1.'key'()
orig_name = $P1.'value'()
naming_done:
setprop $P0, 'methodname', orig_name
metaclass.'add_method'(visible_name, $P0)
goto handles_loop
handles_done:
.return ()
reserved_syntax_error:
'die'("The use of a %hash with the handles trait verb is reserved")
.end
.sub '!handles' :method
.param pmc args :slurpy
.param pmc options :slurpy :named
.local pmc method, attribute
.local string attrname
$P0 = getinterp
method = $P0['sub']
$P1 = getprop 'attrname', method
attrname = $P1
attribute = getattribute self, attrname
$P1 = getprop 'methodname', method
$S1 = $P1
$S0 = substr attrname, 0, 1
if $S0 != '@' goto single_dispatch
.local pmc it
it = iter attribute
it_loop:
unless it goto it_loop_end
$P0 = shift it
$I0 = $P0.'can'($S1)
unless $I0 goto it_loop
.tailcall $P0.$S1(args :flat, options :flat :named)
it_loop_end:
'die'("You used handles on attribute ", attrname, ", but nothing in the array can do method ", $S1)
single_dispatch:
.tailcall attribute.$S1(args :flat, options :flat :named)
.end
=item !set_resolves_list(class)
Gets all the methods that the class has and adds them to the resolves list.
=cut
.sub '!set_resolves_list'
.param pmc class
.local pmc meths, it, res_list
meths = class.'methods'()
it = iter meths
res_list = root_new ['parrot';'ResizableStringArray']
it_loop:
unless it goto it_loop_end
$S0 = shift it
$P0 = meths[$S0]
$I0 = isa $P0, 'MultiSub'
if $I0 goto it_loop
push res_list, $S0
goto it_loop
it_loop_end:
class.'resolve_method'(res_list)
.end
=item !compose_role_attributes(class, role)
Helper method to compose the attributes of a role into a class.
=cut
.sub '!compose_role_attributes'
.param pmc class
.param pmc role
# Need to get hold of attribute order list for the class.
.local pmc attr_order_list
attr_order_list = getprop '@!attribute_list', class
unless null attr_order_list goto have_attr_order_list
attr_order_list = root_new ['parrot';'ResizableStringArray']
setprop class, '@!attribute_list', attr_order_list
have_attr_order_list:
.local pmc role_attrs, class_attrs, ra_iter, fixup_list
.local string cur_attr
role_attrs = inspect role, "attributes"
class_attrs = class."attributes"()
fixup_list = root_new ['parrot';'ResizableStringArray']
ra_iter = iter role_attrs
ra_iter_loop:
unless ra_iter goto ra_iter_loop_end
cur_attr = shift ra_iter
# Check that this attribute doesn't conflict with one already in the class.
$I0 = exists class_attrs[cur_attr]
unless $I0 goto no_conflict
# We have a name conflict. Let's compare the types. If they match, then we
# can merge the attributes.
.local pmc class_attr_type, role_attr_type
$P0 = class_attrs[cur_attr]
if null $P0 goto conflict
class_attr_type = $P0['type']
if null class_attr_type goto conflict
$P0 = role_attrs[cur_attr]
if null $P0 goto conflict
role_attr_type = $P0['type']
if null role_attr_type goto conflict
$I0 = '!SAMETYPE_EXACT'(class_attr_type, role_attr_type)
if $I0 goto merge
conflict:
$S0 = "Conflict of attribute '"
$S0 = concat cur_attr
$S0 = concat "' in composition of role '"
$S1 = role
$S0 = concat $S1
$S0 = concat "'"
'die'($S0)
no_conflict:
addattribute class, cur_attr
push fixup_list, cur_attr
push attr_order_list, cur_attr
merge:
goto ra_iter_loop
ra_iter_loop_end:
# Now we need, for any merged in attributes, to copy property data.
.local pmc fixup_iter, class_props, role_props, props_iter
class_attrs = class."attributes"()
fixup_iter = iter fixup_list
fixup_iter_loop:
unless fixup_iter goto fixup_iter_loop_end
cur_attr = shift fixup_iter
role_props = role_attrs[cur_attr]
class_props = class_attrs[cur_attr]
props_iter = iter role_props
props_iter_loop:
unless props_iter goto props_iter_loop_end
$S0 = shift props_iter
$P0 = role_props[$S0]
class_props[$S0] = $P0
goto props_iter_loop
props_iter_loop_end:
goto fixup_iter_loop
fixup_iter_loop_end:
.end
=item !add_metaclass_method
=cut
.sub '!add_metaclass_method'
.param pmc metaclass
.param pmc name
.param pmc method