/
Attribute.pm
165 lines (154 loc) Β· 5.79 KB
/
Attribute.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
my class Attribute { # declared in BOOTSTRAP
# class Attribute is Any
# has str $!name;
# has int $!rw;
# 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 int $!positional_delegate;
# has int $!associative_delegate;
# has Mu $!why;
# has $!required;
# has Mu $!container_initializer;
method compose(Mu $package, :$compiler_services) {
# 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 $package.^declares_method($meth_name) {
my $dcpkg := nqp::decont($package);
my $meth;
my int $attr_type = nqp::objprimspec($!type);
# Get the compiler to generate us an accessor when possible.
if $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::p6bool(nqp::iseq_i($attr_type, 0))
??
method (Mu:D \fles:) is raw {
nqp::getattr(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::p6bool(nqp::iseq_i($attr_type, 1))
??
method (Mu:D \fles:) is raw {
nqp::getattrref_i(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::p6bool(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::p6bool(nqp::iseq_i($attr_type, 0))
??
method (Mu:D \fles:) {
nqp::getattr(nqp::decont(fles), $dcpkg, $name)
}
!!
nqp::p6bool(nqp::iseq_i($attr_type, 1))
??
method (Mu:D \fles:) {
nqp::p6box_i(
nqp::getattr_i(nqp::decont(fles), $dcpkg, $name)
);
}
!!
nqp::p6bool(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);
}
}
# Apply any handles trait we may have.
self.apply_handles($package);
}
method apply_handles(Mu $pkg) {
# None by default.
}
method get_value(Mu $obj) {
nqp::if(
nqp::iseq_i((my int $t = nqp::objprimspec($!type)),0),
nqp::getattr(nqp::decont($obj),$!package,$!name),
nqp::if(
nqp::iseq_i($t,1),
nqp::p6box_i(nqp::getattr_i(nqp::decont($obj),$!package,$!name)),
nqp::if(
nqp::iseq_i($t,2),
nqp::p6box_n(nqp::getattr_n(nqp::decont($obj),
$!package,$!name)),
nqp::if(
nqp::iseq_i($t,3),
nqp::p6box_s(nqp::getattr_s(nqp::decont($obj),
$!package,$!name))
)
)
)
)
}
method set_value(Mu $obj, Mu \value) {
nqp::if(
nqp::iseq_i((my int $t = nqp::objprimspec($!type)),0),
nqp::bindattr(nqp::decont($obj),$!package,$!name,value),
nqp::if(
nqp::iseq_i($t,1),
nqp::p6box_i(nqp::bindattr_i(nqp::decont($obj),
$!package,$!name,value)),
nqp::if(
nqp::iseq_i($t,2),
nqp::p6box_n(nqp::bindattr_n(nqp::decont($obj),
$!package,$!name,value)),
nqp::if(
nqp::iseq_i($t,3),
nqp::p6box_s(nqp::bindattr_s(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 }
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;
}
}
# vim: ft=perl6 expandtab sw=4