/
traits.pm
433 lines (407 loc) Β· 13.4 KB
/
traits.pm
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
# for errors
my class X::Inheritance::Unsupported { ... }
my class X::Inheritance::UnknownParent { ... }
my class X::Export::NameClash { ... }
my class X::Composition::NotComposable { ... }
my class X::Import::MissingSymbols { ... }
my class X::Redeclaration { ... }
my class X::Inheritance::SelfInherit { ... }
my class X::Comp::Trait::Unknown { ... }
proto trait_mod:<is>(|) { * }
multi trait_mod:<is>(Mu:U $child, Mu:U $parent) {
if $parent.HOW.archetypes.inheritable() {
$child.HOW.add_parent($child, $parent);
}
elsif $parent.HOW.archetypes.inheritalizable() {
$child.HOW.add_parent($child, $parent.HOW.inheritalize($parent))
}
else {
X::Inheritance::Unsupported.new(
:child-typename($child.HOW.name($child)),
:$parent,
).throw;
}
}
multi trait_mod:<is>(Mu:U $child, :$DEPRECATED!) {
# add COMPOSE phaser for this child, which will add an ENTER phaser to an
# existing "new" method, or create a "new" method with a call to DEPRECATED
# and a nextsame.
}
multi trait_mod:<is>(Mu:U $type, :$rw!) {
$type.HOW.set_rw($type);
}
multi trait_mod:<is>(Mu:U $type, :$nativesize!) {
$type.HOW.set_nativesize($type, $nativesize);
}
multi trait_mod:<is>(Mu:U $type, :$unsigned!) {
$type.HOW.set_unsigned($type, $unsigned);
}
multi trait_mod:<is>(Mu:U $type, :$hidden!) {
$type.HOW.set_hidden($type);
}
multi trait_mod:<is>(Mu:U $type, Mu :$array_type!) {
$type.HOW.set_array_type($type, $array_type);
}
multi trait_mod:<is>(Mu:U $type, *%fail) {
if %fail.keys[0] !eq $type.HOW.name($type) {
X::Inheritance::UnknownParent.new(
:child($type.HOW.name($type)),
:parent(%fail.keys[0]),
:suggestions([])
).throw;
} else {
X::Inheritance::SelfInherit.new(
:name(%fail.keys[0])
).throw;
}
}
multi trait_mod:<is>(Attribute:D $attr, |c ) {
X::Comp::Trait::Unknown.new(
file => $?FILE,
line => $?LINE,
type => 'is',
subtype => c.hash.keys[0],
declaring => 'n attribute',
highexpect => <rw readonly box_target>,
).throw;
}
multi trait_mod:<is>(Attribute:D $attr, :$rw!) {
$attr.set_rw();
warn "useless use of 'is rw' on $attr.name()" unless $attr.has_accessor;
}
multi trait_mod:<is>(Attribute:D $attr, :$readonly!) {
$attr.set_readonly();
warn "useless use of 'is readonly' on $attr.name()" unless $attr.has_accessor;
}
multi trait_mod:<is>(Attribute:D $attr, :$box_target!) {
$attr.set_box_target();
}
multi trait_mod:<is>(Attribute:D $attr, :$DEPRECATED!) {
# need to add a COMPOSE phaser to the class, that will add an ENTER phaser
# to the (possibly auto-generated) accessor method.
}
multi trait_mod:<is>(Routine:D $r, |c ) {
X::Comp::Trait::Unknown.new(
file => $?FILE,
line => $?LINE,
type => 'is',
subtype => c.hash.keys[0],
declaring => ' ' ~ lc( $r.^name ),
highexpect => ('rw parcel hidden_from_backtrace',
'pure default DEPRECATED inlinable',
'prec equiv tighter looser assoc' ),
).throw;
}
multi trait_mod:<is>(Routine:D $r, :$rw!) {
$r.set_rw();
}
multi trait_mod:<is>(Routine:D $r, :$parcel!) {
$r.set_rw(); # for now, until we have real parcel handling
}
multi trait_mod:<is>(Routine:D $r, :$default!) {
$r does role { method default() { True } }
}
multi trait_mod:<is>(Routine:D $r, :$DEPRECATED!) {
my $new := $DEPRECATED ~~ Bool
?? "something else"
!! $DEPRECATED;
$r.add_phaser( 'ENTER', -> { DEPRECATED($new) } );
}
multi trait_mod:<is>(Routine:D $r, Mu :$inlinable!) {
$r.set_inline_info(nqp::decont($inlinable));
}
multi trait_mod:<is>(Routine:D $r, :$onlystar!) {
$r.set_onlystar();
}
multi trait_mod:<is>(Routine:D $r, :prec(%spec)!) {
my role Precedence {
has %.prec;
}
if nqp::istype($r, Precedence) {
for %spec {
$r.prec.{.key} := .value;
}
}
else {
$r.HOW.mixin($r, Precedence);
nqp::bindattr(nqp::decont($r), $r.WHAT, '%!prec', %spec);
}
0;
}
multi trait_mod:<is>(Routine $r, :&equiv!) {
nqp::can(&equiv, 'prec')
?? trait_mod:<is>($r, :prec(&equiv.prec))
!! die "Routine given to equiv does not appear to be an operator";
}
multi trait_mod:<is>(Routine $r, :&tighter!) {
die "Routine given to tighter does not appear to be an operator"
unless nqp::can(&tighter, 'prec');
if !nqp::can($r, 'prec') || ($r.prec<prec> // "") !~~ /<[@:]>/ {
trait_mod:<is>($r, :prec(&tighter.prec))
}
$r.prec<prec> := $r.prec<prec>.subst(/\=/, '@=');
}
multi trait_mod:<is>(Routine $r, :&looser!) {
die "Routine given to looser does not appear to be an operator"
unless nqp::can(&looser, 'prec');
if !nqp::can($r, 'prec') || ($r.prec<prec> // "") !~~ /<[@:]>/ {
trait_mod:<is>($r, :prec(&looser.prec))
}
$r.prec<prec> := $r.prec<prec>.subst(/\=/, ':=');
}
multi trait_mod:<is>(Routine $r, :$assoc!) {
trait_mod:<is>($r, :prec({ :$assoc }))
}
# Since trait_mod:<is> to set onlystar isn't there at the
# point we wrote its proto, we do it manually here.
BEGIN &trait_mod:<is>.set_onlystar();
multi trait_mod:<is>(Parameter:D $param, |c ) {
X::Comp::Trait::Unknown.new(
file => $?FILE,
line => $?LINE,
type => 'is',
subtype => c.hash.keys[0],
declaring => ' parameter',
highexpect => <rw readonly copy required parcel>,
).throw;
}
multi trait_mod:<is>(Parameter:D $param, :$readonly!) {
# This is the default.
}
multi trait_mod:<is>(Parameter:D $param, :$rw!) {
$param.set_rw();
}
multi trait_mod:<is>(Parameter:D $param, :$copy!) {
$param.set_copy();
}
multi trait_mod:<is>(Parameter:D $param, :$required!) {
$param.set_required();
}
multi trait_mod:<is>(Parameter:D $param, :$parcel!) {
$param.set_parcel();
}
# Declare these, as setting mainline doesn't get them automatically (as the
# Mu/Any/Scalar are not loaded).
my $!;
my $/;
my $_;
sub EXPORT_SYMBOL(\exp_name, @tags, Mu \sym) {
my @export_packages = $*EXPORT;
for nqp::hllize(@*PACKAGES) {
unless .WHO.exists_key('EXPORT') {
.WHO<EXPORT> := Metamodel::PackageHOW.new_type(:name('EXPORT'));
.WHO<EXPORT>.^compose;
}
@export_packages.push: .WHO<EXPORT>;
}
for @export_packages -> $p {
for @tags -> $tag {
my $install_in;
if $p.WHO.exists_key($tag) {
$install_in := $p.WHO.{$tag};
}
else {
$install_in := Metamodel::PackageHOW.new_type(:name($tag));
$install_in.HOW.compose($install_in);
$p.WHO{$tag} := $install_in;
}
if $install_in.WHO.exists_key(exp_name) {
unless ($install_in.WHO){exp_name} =:= sym {
X::Export::NameClash.new(symbol => exp_name).throw;
}
}
$install_in.WHO{exp_name} := sym;
}
}
0;
}
multi trait_mod:<is>(Routine:D \r, :$export!) {
my $to_export := r.multi ?? r.dispatcher !! r;
my $exp_name := '&' ~ r.name;
my @tags = 'ALL', ($export ~~ Pair ?? $export.key() !!
$export ~~ Positional ?? @($export)>>.key !!
'DEFAULT');
EXPORT_SYMBOL($exp_name, @tags, $to_export);
}
multi trait_mod:<is>(Mu:U \type, :$export!) {
my $exp_name := type.HOW.name(type);
my @tags = 'ALL', ($export ~~ Pair ?? $export.key !!
$export ~~ Positional ?? @($export)>>.key !!
'DEFAULT');
EXPORT_SYMBOL($exp_name, @tags, type);
}
# for constants
multi trait_mod:<is>(Mu \sym, :$export!, :$SYMBOL!) {
my @tags = 'ALL', ($export ~~ Pair ?? $export.key !!
$export ~~ Positional ?? @($export)>>.key !!
'DEFAULT');
EXPORT_SYMBOL($SYMBOL, @tags, sym);
}
# this should be identical Mu:D, :docs, otherwise the fallback Routine:D, |c
# will catch it and declare "docs" to be an unknown trait
multi trait_mod:<is>(Routine:D $docee, :$docs!) {
$docee does role {
has $!WHY;
method WHY { $!WHY }
method set_docs($d) { $!WHY = $d }
}
$docee.set_docs($docs);
$docs.set_docee($docee);
}
multi trait_mod:<is>(Mu:D $docee, :$docs!) {
$docee does role {
has $!WHY;
method WHY { $!WHY }
method set_docs($d) { $!WHY = $d }
}
$docee.set_docs($docs);
$docs.set_docee($docee);
}
multi trait_mod:<is>(Mu:U $docee, :$docs!) {
$docee.HOW.set_docs($docs);
$docs.set_docee($docee);
}
proto trait_mod:<does>(|) { * }
multi trait_mod:<does>(Mu:U $doee, Mu:U $role) {
if $role.HOW.archetypes.composable() {
$doee.HOW.add_role($doee, $role)
}
elsif $role.HOW.archetypes.composalizable() {
$doee.HOW.add_role($doee, $role.HOW.composalize($role))
}
else {
X::Composition::NotComposable.new(
target-name => $doee.HOW.name($doee),
composer => $role,
).throw;
}
}
proto trait_mod:<of>(|) { * }
multi trait_mod:<of>(Mu:U $target, Mu:U $type) {
# XXX Ensure we can do this, die if not.
$target.HOW.set_of($target, $type);
}
multi trait_mod:<of>(Routine:D $target, Mu:U $type) {
my $sig := $target.signature;
X::Redeclaration.new(what => 'return type for', symbol => $target,
postfix => " (previous return type was {$sig.returns.^name})").throw
if $sig.has_returns;
$sig.set_returns($type)
}
multi trait_mod:<is>(Routine:D $r, :$hidden_from_backtrace!) {
$r.HOW.mixin($r, role {
method is_hidden_from_backtrace { True }
});
}
multi trait_mod:<is>(Routine:D $r, :$pure!) {
$r.HOW.mixin($r, role {
method IS_PURE { True }
});
}
proto trait_mod:<returns>(|) { * }
multi trait_mod:<returns>(Routine:D $target, Mu:U $type) {
my $sig := $target.signature;
X::Redeclaration.new(what => 'return type for', symbol => $target,
postfix => " (previous return type was {$sig.returns.^name})").throw
if $sig.has_returns;
$sig.set_returns($type)
}
proto trait_mod:<as>(|) { * }
multi trait_mod:<as>(Parameter:D $param, $type) {
$param.set_coercion($type);
}
my class Pair { ... }
proto trait_mod:<handles>(|) { * }
multi trait_mod:<handles>(Attribute:D $target, $thunk) {
$target does role {
has $.handles;
method set_handles($expr) {
$!handles := $expr;
}
method add_delegator_method($attr: $pkg, $meth_name, $call_name) {
my $meth := method (|c) is rw {
$attr.get_value(self)."$call_name"(|c)
};
$meth.set_name($meth_name);
$pkg.HOW.add_method($pkg, $meth_name, $meth);
}
method apply_handles($attr: Mu $pkg) {
sub applier($expr) {
if $expr.defined() {
if $expr ~~ Str {
self.add_delegator_method($pkg, $expr, $expr);
}
elsif $expr ~~ Pair {
self.add_delegator_method($pkg, $expr.key, $expr.value);
}
elsif $expr ~~ Positional {
for $expr.list {
applier($_);
}
0;
}
else {
$pkg.HOW.add_fallback($pkg,
-> $obj, $name {
?($name ~~ $expr)
},
-> $obj, $name {
-> $self, |c {
$attr.get_value($self)."$name"(|c)
}
});
}
}
else {
$pkg.HOW.add_fallback($pkg,
-> $obj, $name {
?$expr.can($name)
},
-> $obj, $name {
-> $self, |c {
$attr.get_value($self)."$name"(|c)
}
});
}
}
applier($!handles);
}
};
$target.set_handles($thunk());
}
multi sub trait_mod:<handles>(Method:D $m, &thunk) {
my $pkg := $m.signature.params[0].type;
my $call_name := $m.name;
for thunk() -> $meth_name {
my $meth := method (|c) is rw {
self."$call_name"()."$meth_name"(|c);
}
$meth.set_name($meth_name);
$pkg.HOW.add_method($pkg, $meth_name, $meth);
}
0;
}
proto trait_mod:<will>(|) { * }
multi trait_mod:<will>(Attribute $attr, Block :$build!) {
$attr.set_build($build)
}
proto trait_mod:<trusts>(|) { * }
multi trait_mod:<trusts>(Mu:U $truster, Mu:U $trustee) {
$truster.HOW.add_trustee($truster, $trustee);
}
proto trait_mod:<hides>(|) { * }
multi trait_mod:<hides>(Mu:U $child, Mu:U $parent) {
if $parent.HOW.archetypes.inheritable() {
$child.HOW.add_parent($child, $parent, :hides);
}
elsif $parent.HOW.archetypes.inheritalizable() {
$child.HOW.add_parent($child, $parent.HOW.inheritalize($parent), :hides)
}
else {
X::Inheritance::Unsupported.new(
:child-typename($child.HOW.name($child)),
:$parent,
).throw;
}
}
# vim: ft=perl6 expandtab sw=4