-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Attribute.pm6
258 lines (244 loc) Β· 10.2 KB
/
Attribute.pm6
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
my class Attribute { # declared in BOOTSTRAP
# class Attribute is Any
# has str $!name;
# has int $!rw;
# has int $!is_built;
# has int $!is_bound;
# has int $!has_accessor;
# has Mu $!type;
# has Mu $!container_descriptor;
# has Mu $!auto_viv_container;
# has Mu $!build_closure;
# has Mu $!package;
# has int $!inlined;
# has Mu $!dimensions;
# has int $!positional_delegate;
# has int $!associative_delegate;
# has Mu $!why;
# has $!required;
# has Mu $!container_initializer;
# has Attribute $!original;
# has Attribute $!composed;
method compose(Mu $package, :$compiler_services) {
return if $!composed;
my $dcpkg := nqp::decont($package);
nqp::bindattr(self, Attribute, '$!package', $dcpkg);
# Generate accessor method, if we're meant to have one.
if self.has_accessor {
my str $name = nqp::unbox_s(self.name);
my $meth_name := nqp::substr($name, 2);
unless nqp::existskey($package.^method_table, $meth_name)
|| nqp::existskey($package.^submethod_table, $meth_name)
|| (nqp::can($package.HOW, 'has_multi_candidate')
&& $package.^has_multi_candidate($meth_name))
{
my $meth;
my int $attr_type = nqp::objprimspec($!type);
if nqp::can(self,"DEPRECATED")
&& self.DEPRECATED -> $alternative {
my $what = "Method $meth_name (from $package.^name())";
if self.rw {
$meth := nqp::iseq_i($attr_type, 0)
??
method (Mu:D \fles:) is raw {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::getattr(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::iseq_i($attr_type, 1)
??
method (Mu:D \fles:) is raw {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::getattrref_i(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::iseq_i($attr_type, 2)
??
method (Mu:D \fles:) is raw {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::getattrref_n(nqp::decont(fles), $dcpkg, $name)
}
!!
method (Mu:D \fles:) is raw {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::getattrref_s(nqp::decont(fles), $dcpkg, $name)
}
$meth.set_name($meth_name);
}
else { # DEPRECATED ro accessor
$meth := nqp::iseq_i($attr_type, 0)
??
method (Mu:D \fles:) {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::getattr(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::iseq_i($attr_type, 1)
??
method (Mu:D \fles:) {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::p6box_i(
nqp::getattr_i(nqp::decont(fles), $dcpkg, $name)
);
}
!!
nqp::iseq_i($attr_type, 2)
??
method (Mu:D \fles:) {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::p6box_n(
nqp::getattr_n(nqp::decont(fles), $dcpkg, $name)
);
}
!!
method (Mu:D \fles:) {
Rakudo::Deprecations.DEPRECATED($alternative,:$what);
nqp::p6box_s(
nqp::getattr_s(nqp::decont(fles), $dcpkg, $name)
);
}
$meth.set_name($meth_name);
}
}
# Get the compiler to generate us an accessor when possible.
elsif $compiler_services.DEFINITE {
$meth := $compiler_services.generate_accessor($meth_name,
$dcpkg, $name, $!type, self.rw ?? 1 !! 0);
}
# No compiler services available, so do it as a closure.
elsif self.rw {
$meth := nqp::iseq_i($attr_type, 0)
??
method (Mu:D \fles:) is raw {
nqp::getattr(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::iseq_i($attr_type, 1)
??
method (Mu:D \fles:) is raw {
nqp::getattrref_i(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::iseq_i($attr_type, 2)
??
method (Mu:D \fles:) is raw {
nqp::getattrref_n(nqp::decont(fles), $dcpkg, $name)
}
!!
method (Mu:D \fles:) is raw {
nqp::getattrref_s(nqp::decont(fles), $dcpkg, $name)
}
$meth.set_name($meth_name);
}
else { # ro accessor
$meth := nqp::iseq_i($attr_type, 0)
??
method (Mu:D \fles:) {
nqp::getattr(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::iseq_i($attr_type, 1)
??
method (Mu:D \fles:) {
nqp::p6box_i(
nqp::getattr_i(nqp::decont(fles), $dcpkg, $name)
);
}
!!
nqp::iseq_i($attr_type, 2)
??
method (Mu:D \fles:) {
nqp::p6box_n(
nqp::getattr_n(nqp::decont(fles), $dcpkg, $name)
);
}
!!
method (Mu:D \fles:) {
nqp::p6box_s(
nqp::getattr_s(nqp::decont(fles), $dcpkg, $name)
);
}
$meth.set_name($meth_name);
}
$package.^add_method($meth_name, $meth);
}
}
nqp::bindattr_i(self, Attribute, '$!composed', 1);
# Apply any handles trait we may have.
self.apply_handles($package);
}
method apply_handles(Mu $pkg) {
# None by default.
}
method get_value(Mu $obj) is raw {
(my int $t = nqp::objprimspec($!type))
?? nqp::iseq_i($t,1)
?? nqp::getattr_i(nqp::decont($obj),$!package,$!name)
!! nqp::iseq_i($t,2)
?? nqp::getattr_n(nqp::decont($obj),$!package,$!name)
!! nqp::getattr_s(nqp::decont($obj),$!package,$!name) # assume t=3
!! nqp::getattr(nqp::decont($obj),$!package,$!name)
}
method set_value(Mu $obj, Mu \value) is raw {
(my int $t = nqp::objprimspec($!type))
?? nqp::iseq_i($t,1)
?? nqp::bindattr_i(nqp::decont($obj),$!package,$!name,value)
!! nqp::iseq_i($t,2)
?? nqp::bindattr_n(nqp::decont($obj),$!package,$!name,value)
!! nqp::bindattr_s(nqp::decont($obj),$!package,$!name,value) # t=3
!! nqp::bindattr(nqp::decont($obj),$!package,$!name,value)
}
method container() is raw { nqp::ifnull($!auto_viv_container,Nil) }
method readonly() { !self.rw }
method package() { $!package }
method inlined() { $!inlined }
method dimensions() { $!dimensions } # turn list_i into List
multi method Str(Attribute:D:) { self.name }
multi method gist(Attribute:D:) { self.type.^name ~ " " ~ self.name }
method WHY() {
if nqp::isnull($!why) {
nextsame
} else {
$!why.set_docee(self);
$!why
}
}
method set_why($why) {
$!why := $why;
}
}
# does trait
multi sub trait_mod:<does>(Attribute:D $a, Mu:U $role) {
if $role.HOW.archetypes.composable() {
nqp::getattr($a,Attribute,'$!auto_viv_container').VAR
does $role;
}
elsif $role.HOW.archetypes.composalizable() {
nqp::getattr($a,Attribute,'$!auto_viv_container').VAR
does $role.HOW.composalize($role);
}
else {
X::Composition::NotComposable.new(
target-name => 'an attribute',
composer => $role,
).throw;
}
}
multi sub trait_mod:<is>(Attribute:D $a, :$built!) {
if nqp::istype($built,Bool) {
nqp::bindattr_i($a,Attribute,'$!is_built',+$built);
}
elsif nqp::istype($built,Pair) {
if $built.key eq 'bind' {
nqp::bindattr_i($a,Attribute,'$!is_built',1);
nqp::bindattr_i($a,Attribute,'$!is_bound',+$built.value);
}
else {
die "Don't know how to handle 'is built($built.raku())' trait";
}
}
else {
die "Don't know how to handle 'is built($built.raku())' trait";
}
}
# vim: expandtab shiftwidth=4