lib/Object/Pad.xs

filter: Statementstmt 90.1% 792 / 879 Branchbran 67.9% 288 / 424 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.8% 1080 / 1303 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/Object/mop-field.xsi ↑ Summary lib/Object/mop-class.xsi »
1 /* You may distribute under the terms of either the GNU General Public License
2 * or the Artistic License (the same terms as Perl itself)
3 *
4 * (C) Paul Evans, 2019-2024 -- leonerd@leonerd.org.uk
5 */
6 #define PERL_NO_GET_CONTEXT
7  
8 #include "EXTERN.h"
9 #include "perl.h"
10 #include "XSUB.h"
11  
12 #include "XSParseKeyword.h"
13  
14 #include "XSParseSublike.h"
15  
16 #include "perl-backcompat.c.inc"
17  
18 #ifdef HAVE_DMD_HELPER
19 # define WANT_DMD_API_044
20 # include "DMD_helper.h"
21 #endif
22  
23 #include "perl-additions.c.inc"
24 #include "lexer-additions.c.inc"
25 #include "exec_optree.c.inc"
26 #include "forbid_outofblock_ops.c.inc"
27 #include "optree-additions.c.inc"
28 #include "newMYCONSTSUB.c.inc"
29 #include "newOP_CUSTOM.c.inc"
30  
31 #if HAVE_PERL_VERSION(5, 26, 0)
32 # define HAVE_PARSE_SUBSIGNATURE
33 #endif
34  
35 #if HAVE_PERL_VERSION(5, 28, 0)
36 # define HAVE_UNOP_AUX_PV
37 #endif
38  
39 #include "object_pad.h"
40 #include "class.h"
41 #include "field.h"
42  
43 #define warn_deprecated(...) Perl_ck_warner(aTHX_ packWARN(WARN_DEPRECATED), __VA_ARGS__)
44  
45 typedef void MethodAttributeHandler(pTHX_ MethodMeta *meta, const char *value, void *data);
46  
47 struct MethodAttributeDefinition {
48 char *attrname;
49 /* TODO: int flags */
50 MethodAttributeHandler *apply;
51 void *applydata;
52 };
53  
54 /**********************************
55 * Class and Field Implementation *
56 **********************************/
57  
58 401 void ObjectPad_extend_pad_vars(pTHX_ const ClassMeta *meta)
59 {
60 PADOFFSET padix;
61  
62 401 padix = pad_add_name_pvs("$self", 0, NULL, NULL);
63 401 ▶ if(padix != PADIX_SELF)
Branch: 1 branch
BranchTrueFalseTotal
if(padix != PADIX_SELF) 0 401 401
64 0 croak("ARGH: Expected that padix[$self] = 1");
65  
66 /* Give it a name that isn't valid as a Perl variable so it can't collide */
67 401 padix = pad_add_name_pvs("@(Object::Pad/fields)", 0, NULL, NULL);
68 401 ▶ if(padix != PADIX_FIELDS)
Branch: 1 branch
BranchTrueFalseTotal
if(padix != PADIX_FIELDS) 0 401 401
69 0 croak("ARGH: Expected that padix[@fields] = 2");
70  
71 401 ▶ if(meta->type == METATYPE_ROLE) {
Branch: 1 branch
BranchTrueFalseTotal
if(meta->type == METATYPE_ROLE) { 52 349 401
72 /* Don't give this a padname or Future::AsyncAwait will break it (RT137649) */
73 52 padix = pad_add_name_pvs("", 0, NULL, NULL);
74 52 ▶ if(padix != PADIX_EMBEDDING)
Branch: 1 branch
BranchTrueFalseTotal
if(padix != PADIX_EMBEDDING) 0 52 52
75 0 croak("ARGH: Expected that padix[(embedding)] = 3");
76 }
77 401 }
78  
79 578 static void S_freepadsv(pTHX_ PADOFFSET padix)
80 {
81 578 SvREFCNT_dec(PAD_SVl(padix));
82 578 PAD_SVl(padix) = NULL;
83 578 }
84 #define SAVEFREEPADSV(padix) SAVEDESTRUCTOR_X(S_freepadsv, (void *)(padix))
85  
86 #define bind_field_to_pad(sv, fieldix, private, padix) S_bind_field_to_pad(aTHX_ sv, fieldix, private, padix)
87 209 static void S_bind_field_to_pad(pTHX_ SV *sv, FIELDOFFSET fieldix, U8 private, PADOFFSET padix)
88 {
89 SV *val;
90 209 switch(private) {
91 180 case OPpFIELDPAD_SV:
92 180 val = sv;
93 180 break;
94 18 case OPpFIELDPAD_AV:
95 18 ▶ if(!SvROK(sv) || SvTYPE(val = SvRV(sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(sv) || SvTYPE(val = SvRV(sv)) != SVt_PVAV) 18 0 18
if(!SvROK(sv) || SvTYPE(val = SvRV(sv)) != SVt_PVAV) 0 18 18
96 0 croak("ARGH: expected to find an ARRAY reference at field index %ld", (long int)fieldix);
97 18 break;
98 11 case OPpFIELDPAD_HV:
99 11 ▶ if(!SvROK(sv) || SvTYPE(val = SvRV(sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(sv) || SvTYPE(val = SvRV(sv)) != SVt_PVHV) 11 0 11
if(!SvROK(sv) || SvTYPE(val = SvRV(sv)) != SVt_PVHV) 0 11 11
100 0 croak("ARGH: expected to find a HASH reference at field index %ld", (long int)fieldix);
101 11 break;
102 0 default:
103 0 croak("ARGH: unsure what to do with this field type");
104 }
105  
106 209 SAVESPTR(PAD_SVl(padix));
107 209 PAD_SVl(padix) = SvREFCNT_inc(val);
108 209 SAVEFREEPADSV(padix);
109 209 }
110  
111 #define methstart_common(is_role) S_methstart_common(aTHX_ is_role)
112 374 static void S_methstart_common(pTHX_ bool is_role)
113 {
114 374 bool create = PL_op->op_flags & OPf_MOD;
115 374 bool do_shift = PL_op->op_flags & OPf_STACKED;
116  
117 SV *self;
118 374 ▶ if(do_shift)
Branch: 1 branch
BranchTrueFalseTotal
if(do_shift) 365 9 374
119 365 self = av_shift(GvAV(PL_defgv));
120 else
121 9 self = PAD_SVl(PADIX_SELF);
122  
123 374 ▶ if(!SvROK(self) || !SvOBJECT(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(self) || !SvOBJECT(SvRV(self))) 373 1 374
if(!SvROK(self) || !SvOBJECT(SvRV(self))) 0 373 373
124 1 croak("Cannot invoke method on a non-instance");
125  
126 HV *classstash;
127 FIELDOFFSET offset;
128 373 RoleEmbedding *embedding = NULL;
129  
130 373 ▶ if(is_role) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_role) { 31 342 373
131 /* Embedding info is stored in pad1; PAD_SVl() will look at CvDEPTH. We'll
132 * have to grab it manually */
133 31 PAD *pad1 = PadlistARRAY(CvPADLIST(find_runcv(0)))[1];
134 31 SV *embeddingsv = PadARRAY(pad1)[PADIX_EMBEDDING];
135  
136 /* We should never see NULL */
137 assert(embeddingsv);
138  
139 31 ▶ if(SvTYPE(embeddingsv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(embeddingsv) == SVt_PVHV) { 1 30 31
140 1 HV *applied_classes = (HV *)embeddingsv;
141 1 HV *stash = SvSTASH(SvRV(self));
142  
143 1 ▶ if(stash == CvSTASH(find_runcv(0)))
Branch: 1 branch
BranchTrueFalseTotal
if(stash == CvSTASH(find_runcv(0))) 1 0 1
144 1 croak("Cannot invoke a role method directly");
145  
146 0 AV *isa = mro_get_linear_isa(stash);
147  
148 0 ▶ for(UV idx = 0; idx < av_count(isa); idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for(UV idx = 0; idx < av_count(isa); idx++) { 0 0 0
149 0 HE *he = hv_fetch_ent(applied_classes, AvARRAY(isa)[idx], 0, 0);
150 0 ▶ if(!he)
Branch: 1 branch
BranchTrueFalseTotal
if(!he) 0 0 0
151 0 continue;
152  
153 /* HeVAL is a RoleEmbedding * directly */
154 0 embedding = MUST_ROLEEMBEDDING(HeVAL(he));
155 0 break;
156 }
157  
158 0 ▶ if(!embedding)
Branch: 1 branch
BranchTrueFalseTotal
if(!embedding) 0 0 0
159 0 ▶ croak("Could not find embedding info for %" HEKf " in applied classes map",
Branch: 3 branches
BranchTrueFalseTotal
croak("Could not find embedding info for %" HEKf " in applied classes map", 0 0 0
croak("Could not find embedding info for %" HEKf " in applied classes map", 0 0 0
croak("Could not find embedding info for %" HEKf " in applied classes map", 0 0 0
160 HEKfARG(HvNAME_HEK(stash)));
161 }
162 else {
163 30 embedding = MUST_ROLEEMBEDDING(SvPVX(embeddingsv));
164 }
165  
166 30 ▶ if(embedding == &ObjectPad__embedding_standalone) {
Branch: 1 branch
BranchTrueFalseTotal
if(embedding == &ObjectPad__embedding_standalone) { 2 28 30
167 2 classstash = NULL;
168 2 offset = 0;
169 }
170 else {
171 28 classstash = embedding->classmeta->stash;
172 28 offset = embedding->offset;
173 }
174 }
175 else {
176 342 classstash = CvSTASH(find_runcv(0));
177 342 offset = 0;
178 }
179  
180 372 ▶ if(classstash) {
Branch: 1 branch
BranchTrueFalseTotal
if(classstash) { 370 2 372
181 370 ▶ if(!sv_derived_from_hv(self, classstash))
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_derived_from_hv(self, classstash)) 1 369 370
182 1 croak("Cannot invoke foreign method on non-derived instance");
183 }
184  
185 371 ▶ if(do_shift) {
Branch: 1 branch
BranchTrueFalseTotal
if(do_shift) { 362 9 371
186 362 save_clearsv(&PAD_SVl(PADIX_SELF));
187 362 sv_setsv(PAD_SVl(PADIX_SELF), self);
188 }
189  
190 SV *fieldstore;
191  
192 371 ▶ if(is_role) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_role) { 30 341 371
193 30 ▶ if(embedding == &ObjectPad__embedding_standalone) {
Branch: 1 branch
BranchTrueFalseTotal
if(embedding == &ObjectPad__embedding_standalone) { 2 28 30
194 2 fieldstore = NULL;
195 }
196 else {
197 28 fieldstore = get_obj_fieldstore(self, embedding->classmeta->repr, create);
198 }
199 }
200 else {
201 /* op_private contains the repr type so we can extract backing */
202 341 fieldstore = get_obj_fieldstore(self, PL_op->op_private, create);
203 }
204  
205 371 ▶ if(fieldstore) {
Branch: 1 branch
BranchTrueFalseTotal
if(fieldstore) { 369 2 371
206 369 SAVESPTR(PAD_SVl(PADIX_FIELDS));
207 369 PAD_SVl(PADIX_FIELDS) = SvREFCNT_inc(fieldstore);
208 369 SAVEFREEPADSV(PADIX_FIELDS);
209 }
210  
211 371 UNOP_AUX_item *aux = cUNOP_AUX->op_aux;
212 371 ▶ if(aux) {
Branch: 1 branch
BranchTrueFalseTotal
if(aux) { 159 212 371
213 159 U32 fieldcount = (aux++)->uv;
214 159 U32 max_fieldix = (aux++)->uv;
215 159 ▶ SV **fieldsvs = fieldstore_fields(fieldstore);
Branch: 1 branch
BranchTrueFalseTotal
SV **fieldsvs = fieldstore_fields(fieldstore); 10 149 159
216  
217 159 ▶ if(max_fieldix + offset > fieldstore_maxfield(fieldstore))
Branch: 2 branches
BranchTrueFalseTotal
if(max_fieldix + offset > fieldstore_maxfield(fieldstore)) 10 149 159
if(max_fieldix + offset > fieldstore_maxfield(fieldstore)) 0 159 159
218 0 croak("ARGH: instance does not have a field at index %ld", (long int)max_fieldix);
219  
220 368 ▶ while(fieldcount) {
Branch: 1 branch
BranchTrueFalseTotal
while(fieldcount) { 209 159 368
221 209 PADOFFSET padix = (aux++)->uv;
222 209 UV fieldix = (aux++)->uv + offset;
223  
224 209 U8 private = fieldix >> FIELDIX_TYPE_SHIFT;
225 209 fieldix &= FIELDIX_MASK;
226  
227 209 bind_field_to_pad(fieldsvs[fieldix], fieldix, private, padix);
228  
229 209 fieldcount--;
230 }
231 }
232 371 }
233  
234 static XOP xop_methstart;
235 343 static OP *pp_methstart(pTHX)
236 {
237 343 methstart_common(false);
238 341 return PL_op->op_next;
239 }
240  
241 static XOP xop_rolemethstart;
242 31 static OP *pp_rolemethstart(pTHX)
243 {
244 31 methstart_common(true);
245 30 return PL_op->op_next;
246 }
247  
248 380 OP *ObjectPad_newMETHSTARTOP(pTHX_ U32 flags)
249 {
250 380 ▶ OP *(*ppaddr)(pTHX) = (flags & OPfMETHSTART_ROLE) ? &pp_rolemethstart : &pp_methstart;
Branch: 1 branch
BranchTrueFalseTotal
OP *(*ppaddr)(pTHX) = (flags & OPfMETHSTART_ROLE) ? &pp_rolemethstart : &pp_methstart; 48 332 380
251  
252 380 OP *op = newUNOP_AUX(OP_CUSTOM, flags, NULL, NULL);
253 380 op->op_ppaddr = ppaddr;
254 380 op->op_private = (U8)(flags >> 8);
255 380 ▶ if(flags & OPfMETHSTART_ROLE)
Branch: 1 branch
BranchTrueFalseTotal
if(flags & OPfMETHSTART_ROLE) 48 332 380
256 48 op->op_flags |= OPf_SPECIAL;
257 380 return op;
258 }
259  
260 static XOP xop_commonmethstart;
261 3 static OP *pp_commonmethstart(pTHX)
262 {
263 3 bool do_shift = PL_op->op_flags & OPf_STACKED;
264  
265 SV *self;
266 3 ▶ if(do_shift)
Branch: 1 branch
BranchTrueFalseTotal
if(do_shift) 2 1 3
267 2 self = av_shift(GvAV(PL_defgv));
268 else
269 1 self = PAD_SVl(PADIX_SELF);
270  
271 3 ▶ if(SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(self)) 0 3 3
272 /* TODO: Should handle this somehow */
273 0 croak("Cannot invoke common method on an instance");
274  
275 3 ▶ if(do_shift) {
Branch: 1 branch
BranchTrueFalseTotal
if(do_shift) { 2 1 3
276 2 save_clearsv(&PAD_SVl(PADIX_SELF));
277 2 sv_setsv(PAD_SVl(PADIX_SELF), self);
278 }
279  
280 3 return PL_op->op_next;
281 }
282  
283 4 OP *ObjectPad_newCOMMONMETHSTARTOP(pTHX_ U32 flags)
284 {
285 4 OP *op = newOP_CUSTOM(&pp_commonmethstart, flags);
286 4 op->op_private = (U8)(flags >> 8);
287 4 return op;
288 }
289  
290 /* The metadata on the currently-compiling class */
291 #define compclassmeta S_compclassmeta(aTHX)
292 1290 static ClassMeta *S_compclassmeta(pTHX)
293 {
294 1290 SV **svp = hv_fetchs(GvHV(PL_hintgv), "Object::Pad/compclassmeta", 0);
295 1290 ▶ if(!svp || !*svp || !SvOK(*svp))
Branch: 3 branches
BranchTrueFalseTotal
if(!svp || !*svp || !SvOK(*svp)) 1290 0 1290
if(!svp || !*svp || !SvOK(*svp)) 1290 0 1290
if(!svp || !*svp || !SvOK(*svp)) 0 1290 1290
296 0 return NULL;
297 1290 return MUST_CLASSMETA(SvIV(*svp));
298 }
299  
300 #define have_compclassmeta S_have_compclassmeta(aTHX)
301 291 static bool S_have_compclassmeta(pTHX)
302 {
303 291 SV **svp = hv_fetchs(GvHV(PL_hintgv), "Object::Pad/compclassmeta", 0);
304 291 ▶ if(!svp || !*svp)
Branch: 2 branches
BranchTrueFalseTotal
if(!svp || !*svp) 288 3 291
if(!svp || !*svp) 0 288 288
305 3 return false;
306  
307 288 ▶ if(SvOK(*svp) && SvIV(*svp))
Branch: 2 branches
BranchTrueFalseTotal
if(SvOK(*svp) && SvIV(*svp)) 288 0 288
if(SvOK(*svp) && SvIV(*svp)) 288 0 288
308 288 return true;
309  
310 0 return false;
311 }
312  
313 #define compclassmeta_set(meta) S_compclassmeta_set(aTHX_ meta)
314 212 static void S_compclassmeta_set(pTHX_ ClassMeta *meta)
315 {
316 212 SV *sv = *hv_fetchs(GvHV(PL_hintgv), "Object::Pad/compclassmeta", GV_ADD);
317 212 sv_setiv(sv, PTR2UV(meta));
318 212 }
319  
320 0 ClassMeta *ObjectPad_get_compclassmeta(pTHX)
321 {
322 0 ▶ if(!have_compclassmeta)
Branch: 1 branch
BranchTrueFalseTotal
if(!have_compclassmeta) 0 0 0
323 0 croak("An Object::Pad class is not currently under compilation");
324  
325 0 return compclassmeta;
326 }
327  
328 4 XS_INTERNAL(xsub_mop_class_seal)
329 {
330 4 dXSARGS;
331 4 ClassMeta *meta = MUST_CLASSMETA(XSANY.any_ptr);
332  
333 PERL_UNUSED_ARG(items);
334  
335 4 ▶ if(!PL_parser) {
Branch: 1 branch
BranchTrueFalseTotal
if(!PL_parser) { 4 0 4
336 /* We need to generate just enough of a PL_parser to keep newSTATEOP()
337 * happy, otherwise it will SIGSEGV
338 */
339 4 SAVEVPTR(PL_parser);
340 4 Newxz(PL_parser, 1, yy_parser);
341 4 SAVEFREEPV(PL_parser);
342  
343 4 PL_parser->copline = NOLINE;
344 #if HAVE_PERL_VERSION(5, 20, 0)
345 4 PL_parser->preambling = NOLINE;
346 #endif
347 }
348  
349 4 mop_class_seal(meta);
350 4 }
351  
352 102 static void inplace_trim_whitespace(SV *sv)
353 {
354 102 ▶ if(!SvPOK(sv) || !SvCUR(sv))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvPOK(sv) || !SvCUR(sv)) 24 78 102
if(!SvPOK(sv) || !SvCUR(sv)) 0 24 24
355 78 return;
356  
357 24 char *dst = SvPVX(sv);
358 24 char *src = dst;
359  
360 25 ▶ while(*src && isSPACE(*src))
Branch: 2 branches
BranchTrueFalseTotal
while(*src && isSPACE(*src)) 25 0 25
while(*src && isSPACE(*src)) 1 24 25
361 1 src++;
362  
363 24 ▶ if(src > dst) {
Branch: 1 branch
BranchTrueFalseTotal
if(src > dst) { 1 23 24
364 1 size_t offset = src - dst;
365 1 Move(src, dst, SvCUR(sv) - offset, char);
366 1 SvCUR(sv) -= offset;
367 }
368  
369 24 src = dst + SvCUR(sv) - 1;
370 25 ▶ while(src > dst && isSPACE(*src))
Branch: 2 branches
BranchTrueFalseTotal
while(src > dst && isSPACE(*src)) 25 0 25
while(src > dst && isSPACE(*src)) 1 24 25
371 1 src--;
372  
373 24 SvCUR(sv) = src - dst + 1;
374 24 dst[SvCUR(sv)] = 0;
375 }
376  
377 5 static void S_apply_method_common(pTHX_ MethodMeta *meta, const char *val, void *_data)
378 {
379 5 meta->is_common = true;
380 5 }
381  
382 2 static void S_apply_method_override(pTHX_ MethodMeta *meta, const char *val, void *_data)
383 {
384 2 ▶ if(!meta->name)
Branch: 1 branch
BranchTrueFalseTotal
if(!meta->name) 0 2 2
385 0 croak("Cannot apply :override to anonymous methods");
386  
387 2 GV *gv = gv_fetchmeth_sv(compclassmeta->stash, meta->name, 0, 0);
388 2 ▶ if(gv && GvCV(gv))
Branch: 2 branches
BranchTrueFalseTotal
if(gv && GvCV(gv)) 1 1 2
if(gv && GvCV(gv)) 1 0 1
389 1 return;
390  
391 1 croak("Superclass does not have a method named '%" SVf "'", SVfARG(meta->name));
392 }
393  
394 static struct MethodAttributeDefinition method_attributes[] = {
395 { "common", &S_apply_method_common, NULL },
396 { "override", &S_apply_method_override, NULL },
397 { 0 }
398 };
399  
400 /*******************
401 * Custom Keywords *
402 *******************/
403  
404 static IV next_anonclass_id;
405  
406 209 static int build_classlike(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
407 {
408 209 int argi = 0;
409 209 HV *hints = GvHV(PL_hintgv);
410  
411 209 int imported_version = 0;
412 {
413 SV **svp;
414 418 if(hints &&
415 209 (svp = hv_fetchs(hints, "Object::Pad/imported-version", 0)))
416 206 imported_version = SvNV(*svp) * 1000;
417 }
418  
419 209 bool is_anon = false;
420 209 bool is_lexical = (PL_parser->in_my != 0);
421 209 PL_parser->in_my = 0;
422  
423 209 SV *packagename = args[argi++]->sv;
424 209 ▶ if(!packagename) {
Branch: 1 branch
BranchTrueFalseTotal
if(!packagename) { 1 208 209
425 1 ▶ if(is_lexical)
Branch: 1 branch
BranchTrueFalseTotal
if(is_lexical) 0 1 1
426 0 croak("Lexical class requires a name");
427 1 is_anon = true;
428 1 packagename = newSVpvf("Object::Pad::__ANONCLASS__::%" IVdf,
429 next_anonclass_id++);
430 }
431  
432 209 ▶ if(is_lexical) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_lexical) { 2 207 209
433 /* Lexical class is implemented by overriding the package name to
434 * something anonymous then setting up a const sub named after the
435 * requested name which just returns it
436 */
437 2 ▶ if(!hv_fetchs(hints, "Object::Pad/experimental(lexical_class)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(!hv_fetchs(hints, "Object::Pad/experimental(lexical_class)", 0)) 0 2 2
438 0 Perl_ck_warner(aTHX_ packWARN(WARN_EXPERIMENTAL),
439 "'my class' is experimental and may be changed or removed without notice");
440  
441 2 SV *lexname = packagename;
442 2 ▶ if(strstr(SvPV_nolen(lexname), "::"))
Branch: 1 branch
BranchTrueFalseTotal
if(strstr(SvPV_nolen(lexname), "::")) 0 2 2
443 0 croak("Lexical class name must not be fully-qualified");
444  
445 2 packagename = newSVpvf("%" SVf "::__LEXCLASS__/%" SVf,
446 SVfARG(PL_curstname), lexname);
447  
448 2 int unique_suffix = 0;
449 3 ▶ while(gv_stashsv(packagename, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
while(gv_stashsv(packagename, 0)) { 1 2 3
450 /* Append a uniqueness number on the end of there's more than one */
451 1 sv_setpvf(packagename, "%" SVf "::__LEXCLASS__/%" SVf ".%d",
452 SVfARG(PL_curstname), lexname, ++unique_suffix);
453 }
454  
455 2 newMYCONSTSUB_named_sv(lexname, packagename);
456 }
457  
458 209 enum MetaType type = PTR2UV(hookdata);
459  
460 209 SV *packagever = args[argi++]->sv;
461  
462 209 ClassMeta *meta = mop_create_class(type, packagename);
463  
464 209 int nattrs = args[argi++]->i;
465 209 ▶ if(nattrs) {
Branch: 1 branch
BranchTrueFalseTotal
if(nattrs) { 19 190 209
466 19 ▶ if(hv_fetchs(hints, "Object::Pad/configure(no_class_attrs)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(hv_fetchs(hints, "Object::Pad/configure(no_class_attrs)", 0)) 0 19 19
467 0 croak("Class/role attributes are not permitted");
468  
469 19 SV **svp = hv_fetchs(hints, "Object::Pad/configure(only_class_attrs)", 0);
470 19 ▶ HV *only_class_attrs = svp && SvROK(*svp) ? HV_FROM_REF(*svp) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *only_class_attrs = svp && SvROK(*svp) ? HV_FROM_REF(*svp) : NULL; 0 19 19
HV *only_class_attrs = svp && SvROK(*svp) ? HV_FROM_REF(*svp) : NULL; 0 0 0
471  
472 int i;
473 39 ▶ for(i = 0; i < nattrs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nattrs; i++) { 21 18 39
474 21 SV *attrname = args[argi]->attr.name;
475 21 SV *attrval = args[argi]->attr.value;
476  
477 21 ▶ if(only_class_attrs && !hv_fetch_ent(only_class_attrs, attrname, 0, 0))
Branch: 2 branches
BranchTrueFalseTotal
if(only_class_attrs && !hv_fetch_ent(only_class_attrs, attrname, 0, 0)) 0 21 21
if(only_class_attrs && !hv_fetch_ent(only_class_attrs, attrname, 0, 0)) 0 0 0
478 0 croak("Class/role attribute :%" SVf " is not permitted", SVfARG(attrname));
479  
480 21 inplace_trim_whitespace(attrval);
481  
482 21 mop_class_apply_attribute(meta, SvPVX(attrname), attrval);
483  
484 20 argi++;
485 }
486 }
487  
488 208 ▶ if(hv_fetchs(hints, "Object::Pad/configure(always_strict)", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if(hv_fetchs(hints, "Object::Pad/configure(always_strict)", 0)) { 0 208 208
489 0 mop_class_apply_attribute(meta, "strict", sv_2mortal(newSVpvs("params")));
490 }
491  
492 /* At this point XS::Parse::Keyword has parsed all it can. From here we will
493 * take over to perform the odd "block or statement" behaviour of `class`
494 * keywords
495 */
496  
497 bool is_block;
498  
499 208 ▶ if(lex_consume_unichar('{')) {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_consume_unichar('{')) { 207 1 208
500 207 is_block = true;
501 207 ENTER;
502 }
503 1 ▶ else if(lex_consume_unichar(';')) {
Branch: 1 branch
BranchTrueFalseTotal
else if(lex_consume_unichar(';')) { 1 0 1
504 1 is_block = false;
505 1 ▶ if(is_anon)
Branch: 1 branch
BranchTrueFalseTotal
if(is_anon) 0 1 1
506 0 croak("Anonymous class requires a {BLOCK}");
507 1 ▶ if(is_lexical)
Branch: 1 branch
BranchTrueFalseTotal
if(is_lexical) 0 1 1
508 0 croak("Lexical class requires a {BLOCK}");
509 }
510 else
511 0 croak("Expected a block or ';', found > %s", PL_parser->bufptr);
512  
513 415 if(imported_version < 821 &&
514 207 !hv_fetchs(hints, "Object::Pad/configure(no_implicit_pragmata)", 0)) {
515 207 bool was_explicit_strict =
516 412 (PL_hints & HINT_STRICT_REFS) &&
517 412 ▶ (PL_hints & HINT_STRICT_SUBS) &&
Branch: 2 branches
BranchTrueFalseTotal
(PL_hints & HINT_STRICT_SUBS) && 205 2 207
(PL_hints & HINT_STRICT_SUBS) && 205 0 205
518 205 ▶ (PL_hints & HINT_STRICT_VARS);
Branch: 1 branch
BranchTrueFalseTotal
(PL_hints & HINT_STRICT_VARS); 205 0 205
519  
520 207 bool was_explicit_warnings =
521 207 PL_compiling.cop_warnings != pWARN_STD;
522 /* TODO: might be set to something custom? */
523  
524 207 import_pragma("strict", NULL);
525 207 import_pragma("warnings", NULL);
526 #if HAVE_PERL_VERSION(5, 31, 9)
527 207 import_pragma("-feature", "indirect");
528 #else
529 import_pragma("-indirect", ":fatal");
530 #endif
531 #ifdef HAVE_PARSE_SUBSIGNATURE
532 207 import_pragma("experimental", "signatures");
533 #endif
534  
535 207 ▶ if(imported_version >= 800) {
Branch: 1 branch
BranchTrueFalseTotal
if(imported_version >= 800) { 204 3 207
536 204 ▶ const char *kwname = (type == METATYPE_ROLE) ? "role" : "class";
Branch: 1 branch
BranchTrueFalseTotal
const char *kwname = (type == METATYPE_ROLE) ? "role" : "class"; 31 173 204
537  
538 204 ▶ if(!was_explicit_strict)
Branch: 1 branch
BranchTrueFalseTotal
if(!was_explicit_strict) 1 203 204
539 1 warn("%s keyword enabled 'use strict' but this will be removed in a later version", kwname);
540 204 ▶ if(!was_explicit_warnings)
Branch: 1 branch
BranchTrueFalseTotal
if(!was_explicit_warnings) 1 203 204
541 1 warn("%s keyword enabled 'use warnings' but this will be removed in a later version", kwname);
542 }
543 }
544  
545 /* CARGOCULT from perl/op.c:Perl_package() */
546 {
547 208 SAVEGENERICSV(PL_curstash);
548 208 save_item(PL_curstname);
549  
550 208 PL_curstash = (HV *)SvREFCNT_inc(meta->stash);
551 208 sv_setsv(PL_curstname, packagename);
552  
553 208 PL_hints |= HINT_BLOCK_SCOPE;
554 208 PL_parser->copline = NOLINE;
555 }
556  
557 208 ▶ if(packagever) {
Branch: 1 branch
BranchTrueFalseTotal
if(packagever) { 2 206 208
558 /* stolen from op.c because Perl_package_version isn't exported */
559 2 U32 savehints = PL_hints;
560 2 PL_hints &= ~HINT_STRICT_VARS;
561  
562 2 sv_setsv(GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), packagever);
563  
564 2 PL_hints = savehints;
565 }
566  
567 208 ▶ if(is_block) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_block) { 207 1 208
568 207 I32 save_ix = block_start(TRUE);
569 207 compclassmeta_set(meta);
570 207 mop_class_prepare_parse(meta);
571  
572 207 OP *body = parse_stmtseq(0);
573 196 body = block_end(save_ix, body);
574  
575 196 ▶ if(!lex_consume_unichar('}'))
Branch: 1 branch
BranchTrueFalseTotal
if(!lex_consume_unichar('}')) 0 196 196
576 0 croak("Expected }");
577  
578 196 mop_class_seal(meta);
579  
580 193 LEAVE;
581  
582 193 ▶ if(is_anon) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_anon) { 1 192 193
583 1 *out = newSVOP(OP_CONST, 0, SvREFCNT_inc(packagename));
584 1 return KEYWORD_PLUGIN_EXPR;
585 }
586 else {
587 /* CARGOCULT from perl/perly.y:PACKAGE BAREWORD BAREWORD '{' */
588 /* a block is a loop that happens once */
589 192 *out = op_append_elem(OP_LINESEQ,
590 newWHILEOP(0, 1, NULL, NULL, body, NULL, 0),
591 newSVOP(OP_CONST, 0, &PL_sv_yes));
592 192 return KEYWORD_PLUGIN_STMT;
593 }
594 }
595 else {
596 1 SAVEDESTRUCTOR_X(&ObjectPad_mop_class_seal, meta);
597  
598 1 SAVEHINTS();
599 1 compclassmeta_set(meta);
600 1 mop_class_prepare_parse(meta);
601  
602 1 *out = newSVOP(OP_CONST, 0, &PL_sv_yes);
603 1 return KEYWORD_PLUGIN_STMT;
604 }
605 }
606  
607 static const struct XSParseKeywordPieceType pieces_classlike[] = {
608 XPK_PACKAGENAME_OPT,
609 XPK_VSTRING_OPT,
610 XPK_ATTRIBUTES,
611 {0}
612 };
613  
614 static const struct XSParseKeywordHooks kwhooks_class = {
615 .flags = XPK_FLAG_PERMIT_LEXICAL,
616 .permit_hintkey = "Object::Pad/class",
617 .pieces = pieces_classlike,
618 .build = &build_classlike,
619 };
620 static const struct XSParseKeywordHooks kwhooks_role = {
621 .flags = XPK_FLAG_PERMIT_LEXICAL,
622 .permit_hintkey = "Object::Pad/role",
623 .pieces = pieces_classlike,
624 .build = &build_classlike,
625 };
626  
627 41 static int build_inherit(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
628 {
629 41 int argi = 0;
630  
631 41 SV *supername = args[argi++]->sv;
632 41 SV *superver = args[argi++]->sv;
633 41 OP *argsexpr = args[argi++]->op;
634  
635 41 ClassMeta *meta = compclassmeta;
636  
637 41 ▶ if(meta->begun)
Branch: 1 branch
BranchTrueFalseTotal
if(meta->begun) 0 41 41
638 0 croak("Too late to 'inherit' into a class; this must be the first significant declaration within the class");
639  
640 41 AV *argsav = NULL;
641 41 ▶ if(argsexpr) {
Branch: 1 branch
BranchTrueFalseTotal
if(argsexpr) { 4 37 41
642 4 SAVEFREEOP(argsexpr);
643 4 argsav = exec_optree_list(argsexpr);
644  
645 4 SAVEFREESV(argsav);
646 }
647  
648 41 mop_class_load_and_set_superclass(meta, supername, superver);
649  
650 40 mop_class_begin(meta);
651  
652 40 ▶ if(argsav && av_count(argsav)) {
Branch: 2 branches
BranchTrueFalseTotal
if(argsav && av_count(argsav)) { 4 36 40
if(argsav && av_count(argsav)) { 4 0 4
653 4 HV *hints = GvHV(PL_hintgv);
654 4 ▶ if(!hv_fetchs(hints, "Object::Pad/experimental(inherit_field)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(!hv_fetchs(hints, "Object::Pad/experimental(inherit_field)", 0)) 1 3 4
655 1 Perl_ck_warner(aTHX_ packWARN(WARN_EXPERIMENTAL),
656 "inheriting fields is experimental and may be changed or removed without notice");
657  
658 4 mop_class_inherit_from_superclass(meta, AvARRAY(argsav), av_count(argsav));
659 }
660  
661 40 return KEYWORD_PLUGIN_STMT;
662 }
663  
664 static const struct XSParseKeywordHooks kwhooks_inherit = {
665 .permit_hintkey = "Object::Pad/inherit",
666 .pieces = (const struct XSParseKeywordPieceType []){
667 XPK_PACKAGENAME,
668 XPK_VSTRING_OPT,
669 XPK_LISTEXPR_LISTCTX_OPT,
670 {0}
671 },
672 .build = &build_inherit,
673 };
674  
675 39 static int build_apply(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
676 {
677 39 int argi = 0;
678  
679 39 SV *rolename = args[argi++]->sv;
680 39 SV *rolever = args[argi++]->sv;
681  
682 39 ClassMeta *meta = compclassmeta;
683  
684 39 mop_class_begin(meta);
685  
686 39 mop_class_load_and_add_role(meta, rolename, rolever);
687  
688 39 return KEYWORD_PLUGIN_STMT;
689 }
690  
691 static const struct XSParseKeywordHooks kwhooks_apply = {
692 .permit_hintkey = "Object::Pad/apply",
693 .pieces = (const struct XSParseKeywordPieceType []){
694 XPK_PACKAGENAME,
695 XPK_VSTRING_OPT,
696 /* TODO: Allow more apply-time args later */
697 {0}
698 },
699 .build = &build_apply,
700 };
701  
702 enum {
703 FIELD_INIT_CLASSEXPR,
704 FIELD_INIT_BLOCK,
705 FIELD_INIT_EXPR,
706 FIELD_INIT_DOREXPR,
707 FIELD_INIT_OREXPR,
708 };
709  
710 132 static void check_field(pTHX_ void *hookdata)
711 {
712 132 char *kwname = hookdata;
713  
714 132 ▶ if(!have_compclassmeta)
Branch: 1 branch
BranchTrueFalseTotal
if(!have_compclassmeta) 2 130 132
715 2 croak("Cannot '%s' outside of 'class'", kwname);
716  
717 130 ▶ if(compclassmeta->role_is_invokable)
Branch: 1 branch
BranchTrueFalseTotal
if(compclassmeta->role_is_invokable) 1 129 130
718 1 croak("Cannot add field data to an invokable role");
719  
720 129 ▶ if(!sv_eq(PL_curstname, compclassmeta->name))
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_eq(PL_curstname, compclassmeta->name)) 0 129 129
721 0 croak("Current package name no longer matches current class (%" SVf " vs %" SVf ")",
722 PL_curstname, compclassmeta->name);
723 129 }
724  
725 128 static int build_field(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
726 {
727 128 int argi = 0;
728  
729 128 SV *name = args[argi++]->sv;
730 128 char sigil = SvPV_nolen(name)[0];
731  
732 128 ClassMeta *classmeta = compclassmeta;
733  
734 128 mop_class_begin(classmeta);
735  
736 128 FieldMeta *fieldmeta = mop_class_add_field(classmeta, name);
737 128 SvREFCNT_dec(name);
738  
739 128 int nattrs = args[argi++]->i;
740 128 ▶ if(nattrs) {
Branch: 1 branch
BranchTrueFalseTotal
if(nattrs) { 60 68 128
741 60 ▶ if(hv_fetchs(GvHV(PL_hintgv), "Object::Pad/configure(no_field_attrs)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(hv_fetchs(GvHV(PL_hintgv), "Object::Pad/configure(no_field_attrs)", 0)) 0 60 60
742 0 croak("Field attributes are not permitted");
743  
744 60 SV **svp = hv_fetchs(GvHV(PL_hintgv), "Object::Pad/configure(only_field_attrs)", 0);
745 60 ▶ HV *only_field_attrs = svp && SvROK(*svp) ? HV_FROM_REF(*svp) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
HV *only_field_attrs = svp && SvROK(*svp) ? HV_FROM_REF(*svp) : NULL; 0 60 60
HV *only_field_attrs = svp && SvROK(*svp) ? HV_FROM_REF(*svp) : NULL; 0 0 0
746  
747 60 SV *fieldmetasv = newSV(0);
748 60 sv_setref_uv(fieldmetasv, "Object::Pad::MOP::Field", PTR2UV(fieldmeta));
749 60 SAVEFREESV(fieldmetasv);
750  
751 138 ▶ while(argi < (nattrs+2)) {
Branch: 1 branch
BranchTrueFalseTotal
while(argi < (nattrs+2)) { 81 57 138
752 81 SV *attrname = args[argi]->attr.name;
753 81 SV *attrval = args[argi]->attr.value;
754  
755 81 ▶ if(only_field_attrs && !hv_fetch_ent(only_field_attrs, attrname, 0, 0))
Branch: 2 branches
BranchTrueFalseTotal
if(only_field_attrs && !hv_fetch_ent(only_field_attrs, attrname, 0, 0)) 0 81 81
if(only_field_attrs && !hv_fetch_ent(only_field_attrs, attrname, 0, 0)) 0 0 0
756 0 croak("Field attribute :%" SVf " is not permitted", SVfARG(attrname));
757  
758 81 inplace_trim_whitespace(attrval);
759  
760 81 mop_field_parse_and_apply_attribute(fieldmeta, SvPVX(attrname), attrval);
761  
762 78 ▶ if(attrval)
Branch: 1 branch
BranchTrueFalseTotal
if(attrval) 78 0 78
763 78 SvREFCNT_dec(attrval);
764  
765 78 argi++;
766 }
767 }
768  
769 125 bool is_block = FALSE;
770  
771 /* It would be nice to just yield some OP to represent the has field here
772 * and let normal parsing of normal scalar assignment accept it. But we can't
773 * because scalar assignment tries to peephole far too deply into us and
774 * everything breaks... :/
775 */
776 125 int inittype = args[argi++]->i;
777 125 switch(inittype) {
778 55 case -1:
779 /* no expr */
780 55 break;
781  
782 0 case FIELD_INIT_CLASSEXPR:
783 0 croak("Unreachable");
784  
785 11 case FIELD_INIT_BLOCK:
786 11 is_block = TRUE;
787 /* FALLTHROUGH */
788 70 case FIELD_INIT_EXPR:
789 case FIELD_INIT_DOREXPR:
790 case FIELD_INIT_OREXPR:
791 {
792 70 OP *op = args[argi++]->op;
793 70 U8 want = 0;
794  
795 70 ▶ forbid_outofblock_ops(op,
Branch: 1 branch
BranchTrueFalseTotal
forbid_outofblock_ops(op, 11 59 70
796 is_block ? "a field initialiser block" : "a field initialiser expression");
797  
798 70 switch(sigil) {
799 62 case '$':
800 62 want = G_SCALAR;
801 62 break;
802 8 case '@':
803 case '%':
804 8 want = G_LIST;
805 8 break;
806 }
807  
808 70 fieldmeta->defaultexpr = op_contextualize(op_scope(op), want);
809 70 ▶ if(inittype == FIELD_INIT_DOREXPR)
Branch: 1 branch
BranchTrueFalseTotal
if(inittype == FIELD_INIT_DOREXPR) 1 69 70
810 1 fieldmeta->def_if_undef = true;
811 70 ▶ if(inittype == FIELD_INIT_OREXPR)
Branch: 1 branch
BranchTrueFalseTotal
if(inittype == FIELD_INIT_OREXPR) 1 69 70
812 1 fieldmeta->def_if_false = true;
813 }
814 70 break;
815 }
816  
817 125 mop_field_seal(fieldmeta);
818  
819 125 return KEYWORD_PLUGIN_STMT;
820 }
821  
822 71 static void setup_parse_field(pTHX_ bool is_block)
823 {
824 71 CV *was_compcv = PL_compcv;
825 71 HV *hints = GvHV(PL_hintgv);
826  
827 71 ClassMeta *classmeta = compclassmeta;
828  
829 71 resume_compcv_and_save(&classmeta->initfields_compcv);
830  
831 /* Set up this new block as if the current compiler context were its scope */
832  
833 71 ▶ if(CvOUTSIDE(PL_compcv))
Branch: 1 branch
BranchTrueFalseTotal
if(CvOUTSIDE(PL_compcv)) 71 0 71
834 71 SvREFCNT_dec(CvOUTSIDE(PL_compcv));
835  
836 71 CvOUTSIDE(PL_compcv) = (CV *)SvREFCNT_inc(was_compcv);
837 71 CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
838  
839 71 hv_stores(hints, "Object::Pad/__CLASS__", newSVsv(&PL_sv_yes));
840 71 hv_stores(hints, "Object::Pad/fieldcopline", newSVuv(CopLINE(PL_curcop)));
841  
842 71 ▶ if(!is_block) {
Branch: 1 branch
BranchTrueFalseTotal
if(!is_block) { 60 11 71
843 /* Hide the $self lexical by scrubbing its name */
844 60 PADNAME *pn_self = PadnamelistARRAY(PadlistNAMES(CvPADLIST(PL_compcv)))[PADIX_SELF];
845  
846 60 SAVEI8(PadnamePV(pn_self)[1]);
847 60 PadnamePV(pn_self)[1] = '\0';
848 }
849  
850 71 U32 nfields = av_count(classmeta->fields);
851 71 ▶ if(classmeta->next_field_for_initfields < nfields) {
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->next_field_for_initfields < nfields) { 24 47 71
852 24 add_fields_to_pad(classmeta, classmeta->next_field_for_initfields);
853 24 intro_my();
854 24 classmeta->next_field_for_initfields = nfields;
855 }
856 71 }
857  
858 11 static void setup_parse_field_initblock(pTHX_ void *hookdata)
859 {
860 11 HV *hints = GvHV(PL_hintgv);
861  
862 11 ▶ if(hv_fetchs(hints, "Object::Pad/configure(no_field_block)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(hv_fetchs(hints, "Object::Pad/configure(no_field_block)", 0)) 0 11 11
863 0 croak("Field initialisation block is not permitted");
864  
865 11 ▶ if(!hv_fetchs(hints, "Object::Pad/experimental(init_expr)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(!hv_fetchs(hints, "Object::Pad/experimental(init_expr)", 0)) 1 10 11
866 1 Perl_ck_warner(aTHX_ packWARN(WARN_EXPERIMENTAL),
867 "field initialiser block is experimental and may be changed or removed without notice");
868  
869 11 setup_parse_field(aTHX_ TRUE);
870 11 }
871  
872 60 static void setup_parse_field_initexpr(pTHX_ void *hookdata)
873 {
874 60 setup_parse_field(aTHX_ FALSE);
875 60 }
876  
877 #define XPK_DOREQUALS XPK_LITERAL("//=")
878 #define XPK_OREQUALS XPK_LITERAL("||=")
879  
880 static const struct XSParseKeywordHooks kwhooks_field = {
881 .flags = XPK_FLAG_STMT,
882 .permit_hintkey = "Object::Pad/field",
883  
884 .check = &check_field,
885  
886 .pieces = (const struct XSParseKeywordPieceType []){
887 XPK_LEXVARNAME(XPK_LEXVAR_ANY),
888 XPK_ATTRIBUTES,
889 XPK_TAGGEDCHOICE(
890 XPK_PREFIXED_BLOCK_ENTERLEAVE(XPK_SETUP(&setup_parse_field_initblock)),
891 XPK_TAG(FIELD_INIT_BLOCK),
892 XPK_SEQUENCE(XPK_EQUALS, XPK_PREFIXED_LISTEXPR_ENTERLEAVE(XPK_SETUP(&setup_parse_field_initexpr)), XPK_AUTOSEMI),
893 XPK_TAG(FIELD_INIT_EXPR),
894 XPK_SEQUENCE(XPK_DOREQUALS, XPK_PREFIXED_LISTEXPR_ENTERLEAVE(XPK_SETUP(&setup_parse_field_initexpr)), XPK_AUTOSEMI),
895 XPK_TAG(FIELD_INIT_DOREXPR),
896 XPK_SEQUENCE(XPK_OREQUALS, XPK_PREFIXED_LISTEXPR_ENTERLEAVE(XPK_SETUP(&setup_parse_field_initexpr)), XPK_AUTOSEMI),
897 XPK_TAG(FIELD_INIT_OREXPR)
898 ),
899 {0}
900 },
901 .build = &build_field,
902 };
903 static const struct XSParseKeywordHooks kwhooks_has = {
904 .flags = XPK_FLAG_STMT,
905 .permit_hintkey = "Object::Pad/has",
906  
907 .check = &check_field,
908  
909 .pieces = (const struct XSParseKeywordPieceType []){
910 XPK_FAILURE("'has' is no longer supported; use 'field' instead"),
911 {0}
912 },
913 .build = &build_field,
914 };
915  
916 /* We use the method-like keyword parser to parse phaser blocks as well as
917 * methods. In order to tell what is going on, hookdata will be an integer
918 * set to one of the following
919 */
920  
921 enum PhaserType {
922 PHASER_NONE, /* A normal `method`; i.e. not a phaser */
923 PHASER_BUILD,
924 PHASER_ADJUST,
925 PHASER_ADJUSTPARAMS,
926 PHASER_APPLY,
927 };
928  
929 static const char *phasertypename[] = {
930 [PHASER_BUILD] = "BUILD",
931 [PHASER_ADJUST] = "ADJUST",
932 [PHASER_ADJUSTPARAMS] = "ADJUST",
933 [PHASER_APPLY] = "APPLY",
934 };
935  
936 107 static bool parse_method_permit(pTHX_ void *hookdata)
937 {
938 107 ▶ if(!have_compclassmeta)
Branch: 1 branch
BranchTrueFalseTotal
if(!have_compclassmeta) 1 106 107
939 1 croak("Cannot 'method' outside of 'class'");
940  
941 106 ▶ if(!sv_eq(PL_curstname, compclassmeta->name))
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_eq(PL_curstname, compclassmeta->name)) 0 106 106
942 0 croak("Current package name no longer matches current class (%" SVf " vs %" SVf ")",
943 PL_curstname, compclassmeta->name);
944  
945 106 return true;
946 }
947  
948 149 static void parse_method_pre_subparse(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
949 {
950 149 enum PhaserType type = PTR2UV(hookdata);
951 149 HV *hints = GvHV(PL_hintgv);
952  
953 /* XS::Parse::Sublike doesn't support lexical `method $foo`, but we can hack
954 * it up here
955 */
956 155 ▶ if(type == PHASER_NONE && !ctx->name &&
Branch: 1 branch
BranchTrueFalseTotal
if(type == PHASER_NONE && !ctx->name && 106 43 149
957 6 lex_peek_unichar(0) == '$') {
958 1 ctx->name = lex_scan_lexvar();
959 1 ▶ if(!ctx->name)
Branch: 1 branch
BranchTrueFalseTotal
if(!ctx->name) 0 1 1
960 0 croak("Expected a lexical variable name");
961  
962 1 lex_read_space(0);
963 1 hv_stores(ctx->moddata, "Object::Pad/method_varname", SvREFCNT_inc(ctx->name));
964  
965 /* XPS should set a CV name */
966 1 ctx->actions |= XS_PARSE_SUBLIKE_ACTION_SET_CVNAME;
967 /* XPS should not CVf_ANON, install a named symbol, or emit an anoncode expr */
968 1 ctx->actions &= ~(XS_PARSE_SUBLIKE_ACTION_CVf_ANON|XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL|XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE|XS_PARSE_SUBLIKE_ACTION_RET_EXPR);
969 }
970  
971 149 switch(type) {
972 145 case PHASER_NONE:
973 case PHASER_BUILD:
974 case PHASER_ADJUST:
975 case PHASER_APPLY:
976 145 break;
977  
978 4 case PHASER_ADJUSTPARAMS:
979 if(0)
980 warn("ADJUSTPARAMS is now the same as ADJUST; you should use ADJUST instead");
981 4 break;
982 }
983  
984 149 ▶ if(type != PHASER_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if(type != PHASER_NONE) 43 106 149
985 /* We need to fool start_subparse() into thinking this is a named function
986 * so it emits a real CV and not a protosub
987 */
988 43 ctx->actions &= ~XS_PARSE_SUBLIKE_ACTION_CVf_ANON;
989  
990 149 ClassMeta *meta = compclassmeta;
991  
992 149 mop_class_begin(meta);
993  
994 149 prepare_method_parse(meta);
995  
996 MethodMeta *compmethodmeta;
997 149 Newx(compmethodmeta, 1, MethodMeta);
998  
999 149 *compmethodmeta = (MethodMeta){
1000 LINNET_INIT(LINNET_VAL_METHODMETA)
1001 149 .name = SvREFCNT_inc(ctx->name),
1002 };
1003  
1004 149 hv_stores(ctx->moddata, "Object::Pad/compmethodmeta", newSVuv(PTR2UV(compmethodmeta)));
1005 149 hv_stores(hints, "Object::Pad/__CLASS__", newSVsv(&PL_sv_yes));
1006 149 }
1007  
1008 11 static void parse_method_start_signature(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1009 {
1010 /* reserve argidx=0 for $self */
1011 11 xps_signature_add_param(ctx, (&(struct XPSSignatureParamDetails){
1012 .ver = XSPARSESUBLIKE_ABI_VERSION,
1013  
1014 .sigil = '$',
1015 .padix = PADIX_SELF,
1016 }));
1017 11 }
1018  
1019 /* TODO: It'd be nice to do the rest of the signature op manipulation in a
1020 * finish_signature hook function, but currently XPS does not expose enough of
1021 * the signature ops in a visible way for us to do that.
1022 */
1023  
1024 1 static void parse_classphaser_pre_subparse(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1025 {
1026 1 ClassMeta *meta = compclassmeta;
1027  
1028 1 mop_class_begin(meta);
1029  
1030 1 ctx->actions &= ~XS_PARSE_SUBLIKE_ACTION_CVf_ANON;
1031 1 }
1032  
1033 11 static bool parse_method_filter_attr(pTHX_ struct XSParseSublikeContext *ctx, SV *attr, SV *val, void *hookdata)
1034 {
1035 11 MethodMeta *compmethodmeta = MUST_METHODMETA(SvUV(*hv_fetchs(ctx->moddata, "Object::Pad/compmethodmeta", 0)));
1036  
1037 struct MethodAttributeDefinition *def;
1038 21 ▶ for(def = method_attributes; def->attrname; def++) {
Branch: 1 branch
BranchTrueFalseTotal
for(def = method_attributes; def->attrname; def++) { 17 4 21
1039 17 ▶ if(!strEQ(SvPVX(attr), def->attrname))
Branch: 1 branch
BranchTrueFalseTotal
if(!strEQ(SvPVX(attr), def->attrname)) 10 7 17
1040 10 continue;
1041  
1042 /* TODO: We might want to wrap the CV in some sort of MethodMeta struct
1043 * but for now we'll just pass the XSParseSublikeContext context */
1044 7 ▶ (*def->apply)(aTHX_ compmethodmeta, SvPOK(val) ? SvPVX(val) : NULL, def->applydata);
Branch: 1 branch
BranchTrueFalseTotal
(*def->apply)(aTHX_ compmethodmeta, SvPOK(val) ? SvPVX(val) : NULL, def->applydata); 0 7 7
1045  
1046 6 return true;
1047 }
1048  
1049 /* No error, just let it fall back to usual attribute handling */
1050 4 return false;
1051 }
1052  
1053 8 static bool parse_phaser_filter_attr(pTHX_ struct XSParseSublikeContext *ctx, SV *attr, SV *val, void *hookdata)
1054 {
1055 8 enum PhaserType type = PTR2UV(hookdata);
1056 8 HV *hints = GvHV(PL_hintgv);
1057  
1058 8 ▶ if(hv_fetchs(hints, "Object::Pad/configure(no_adjust_attrs)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(hv_fetchs(hints, "Object::Pad/configure(no_adjust_attrs)", 0)) 0 8 8
1059 0 croak("ADJUST phaser attributes are not permitted");
1060  
1061 8 ▶ if(strEQ(SvPVX(attr), "params")) {
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(SvPVX(attr), "params")) { 8 0 8
1062 8 ▶ if(type != PHASER_ADJUST)
Branch: 1 branch
BranchTrueFalseTotal
if(type != PHASER_ADJUST) 0 8 8
1063 0 croak("Cannot set :params for a phaser other than ADJUST");
1064  
1065 8 hv_stores(ctx->moddata, "Object::Pad/ADJUST:params", newRV_noinc((SV *)newAV()));
1066 8 return true;
1067 }
1068  
1069 0 return false;
1070 }
1071  
1072 148 static void parse_method_post_blockstart(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1073 {
1074 148 enum PhaserType type = PTR2UV(hookdata);
1075  
1076 148 MethodMeta *compmethodmeta = MUST_METHODMETA(SvUV(*hv_fetchs(ctx->moddata, "Object::Pad/compmethodmeta", 0)));
1077  
1078 /* `method` always permits signatures */
1079 #ifdef HAVE_PARSE_SUBSIGNATURE
1080 148 import_pragma("feature", "signatures");
1081 148 import_pragma("-warnings", "experimental::signatures");
1082 #endif
1083  
1084 148 start_method_parse(compclassmeta, compmethodmeta->is_common);
1085  
1086 SV **svp;
1087  
1088 148 ▶ if(type == PHASER_ADJUST && (svp = hv_fetchs(ctx->moddata, "Object::Pad/ADJUST:params", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if(type == PHASER_ADJUST && (svp = hv_fetchs(ctx->moddata, "Object::Pad/ADJUST:params", 0))) { 22 126 148
if(type == PHASER_ADJUST && (svp = hv_fetchs(ctx->moddata, "Object::Pad/ADJUST:params", 0))) { 8 14 22
1089 8 AV *params = AV_FROM_REF(*svp);
1090  
1091 8 prepare_adjust_params(compclassmeta);
1092  
1093 8 parse_adjust_params(compclassmeta, params);
1094 }
1095 147 }
1096  
1097 1 static void parse_classphaser_post_blockstart(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1098 {
1099 /* phasers always permit signatures */
1100 #ifdef HAVE_PARSE_SUBSIGNATURE
1101 1 import_pragma("feature", "signatures");
1102 1 import_pragma("-warnings", "experimental::signatures");
1103 #endif
1104 1 }
1105  
1106 #define walk_optree_warn_for_defargs(o) S_walk_optree_warn_for_defargs(aTHX_ o)
1107 static void S_walk_optree_warn_for_defargs(pTHX_ OP *o);
1108 190 static void S_walk_optree_warn_for_defargs(pTHX_ OP *o)
1109 {
1110 OP *kid;
1111  
1112 190 switch(o->op_type) {
1113 31 case OP_NEXTSTATE:
1114 case OP_DBSTATE:
1115 31 PL_curcop = (COP *)o;
1116 31 break;
1117  
1118 3 case OP_RV2AV:
1119 /* check for @_; also catches $_[0] as part of AELEM etc */
1120 3 ▶ if(o->op_flags & OPf_KIDS &&
Branch: 1 branch
BranchTrueFalseTotal
if(o->op_flags & OPf_KIDS && 3 0 3
1121 3 ▶ (kid = cUNOPo->op_first) &&
Branch: 1 branch
BranchTrueFalseTotal
(kid = cUNOPo->op_first) && 3 0 3
1122 3 ▶ kid->op_type == OP_GV &&
Branch: 1 branch
BranchTrueFalseTotal
kid->op_type == OP_GV && 3 0 3
1123 3 ▶ kGVOP_gv == PL_defgv)
Branch: 1 branch
BranchTrueFalseTotal
kGVOP_gv == PL_defgv) 3 0 3
1124 3 warn_deprecated("Use of @_ is deprecated in ADJUST");
1125 3 break;
1126  
1127 2 case OP_SHIFT:
1128 case OP_POP:
1129 2 ▶ if(o->op_flags & OPf_SPECIAL)
Branch: 1 branch
BranchTrueFalseTotal
if(o->op_flags & OPf_SPECIAL) 1 1 2
1130 1 warn_deprecated("Implicit use of @_ in %s is deprecated in ADJUST", PL_op_name[o->op_type]);
1131 2 break;
1132 }
1133  
1134 190 ▶ if(o->op_flags & OPf_KIDS) {
Branch: 1 branch
BranchTrueFalseTotal
if(o->op_flags & OPf_KIDS) { 70 120 190
1135 239 ▶ for(kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
Branch: 2 branches
BranchTrueFalseTotal
for(kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) 99 70 169
for(kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) 169 70 239
1136 169 walk_optree_warn_for_defargs(kid);
1137 }
1138 190 }
1139  
1140 146 static void parse_method_pre_blockend(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1141 {
1142 146 enum PhaserType type = PTR2UV(hookdata);
1143  
1144 146 MethodMeta *compmethodmeta = MUST_METHODMETA(SvUV(*hv_fetchs(ctx->moddata, "Object::Pad/compmethodmeta", 0)));
1145  
1146 SV **svp;
1147  
1148 146 ▶ if(type == PHASER_ADJUST) {
Branch: 1 branch
BranchTrueFalseTotal
if(type == PHASER_ADJUST) { 21 125 146
1149 21 ENTER;
1150 21 SAVEVPTR(PL_curcop);
1151  
1152 #if HAVE_PERL_VERSION(5, 26, 0)
1153 21 OP *o = ctx->body;
1154  
1155 /* Try to find the first significant op in the tree. There's a few
1156 * standard tricks we can do to attempt to find the OP_ARGCHECK if there
1157 * is one. */
1158 while(1) {
1159 38 redo:
1160 59 ▶ if(!o)
Branch: 1 branch
BranchTrueFalseTotal
if(!o) 0 59 59
1161 0 break;
1162 59 switch(o->op_type) {
1163 0 case OP_NULL:
1164 0 ▶ if(o->op_targ == OP_ARGCHECK) {
Branch: 1 branch
BranchTrueFalseTotal
if(o->op_targ == OP_ARGCHECK) { 0 0 0
1165 0 o = cUNOPo->op_first;
1166 0 goto redo;
1167 }
1168  
1169 0 o = NULL;
1170 0 break;
1171  
1172 19 case OP_NEXTSTATE:
1173 case OP_DBSTATE:
1174 19 PL_curcop = (COP *)o;
1175 19 ▶ o = OpSIBLING(o);
Branch: 1 branch
BranchTrueFalseTotal
o = OpSIBLING(o); 19 0 19
1176 19 goto redo;
1177  
1178 19 case OP_LINESEQ:
1179 19 o = cLISTOPo->op_first;
1180 19 goto redo;
1181 }
1182 21 break;
1183 }
1184  
1185 21 ▶ if(o && o->op_type == OP_ARGCHECK) {
Branch: 2 branches
BranchTrueFalseTotal
if(o && o->op_type == OP_ARGCHECK) { 21 0 21
if(o && o->op_type == OP_ARGCHECK) { 1 20 21
1186 1 warn_deprecated("Use of ADJUST (signature) {BLOCK} is now deprecated");
1187 }
1188 #endif
1189  
1190 21 walk_optree_warn_for_defargs(ctx->body);
1191  
1192 21 LEAVE;
1193 }
1194  
1195 146 ▶ if(type == PHASER_ADJUST && (svp = hv_fetchs(ctx->moddata, "Object::Pad/ADJUST:params", 0))) {
Branch: 2 branches
BranchTrueFalseTotal
if(type == PHASER_ADJUST && (svp = hv_fetchs(ctx->moddata, "Object::Pad/ADJUST:params", 0))) { 21 125 146
if(type == PHASER_ADJUST && (svp = hv_fetchs(ctx->moddata, "Object::Pad/ADJUST:params", 0))) { 7 14 21
1196 7 AV *params = AV_FROM_REF(*svp);
1197  
1198 7 ctx->body = finish_adjust_params(compclassmeta, params, ctx->body);
1199 }
1200  
1201 146 ctx->body = finish_method_parse(compclassmeta, compmethodmeta->is_common, ctx->body);
1202  
1203 146 ▶ if(type != PHASER_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if(type != PHASER_NONE) 42 104 146
1204 /* We need to remove the name now to stop newATTRSUB() from creating this
1205 * as a named symbol table entry
1206 */
1207 42 ctx->actions &= ~XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL;
1208 146 }
1209  
1210 1 static void parse_classphaser_pre_blockend(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1211 {
1212 /* We need to remove the name now to stop newATTRSUB() from creating this
1213 * as a named symbol table entry
1214 */
1215 1 ctx->actions &= ~XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL;
1216 1 }
1217  
1218 146 static void parse_method_post_newcv(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1219 {
1220 146 enum PhaserType type = PTR2UV(hookdata);
1221  
1222 MethodMeta *compmethodmeta;
1223 {
1224 146 SV *tmpsv = *hv_fetchs(ctx->moddata, "Object::Pad/compmethodmeta", 0);
1225 146 compmethodmeta = MUST_METHODMETA(SvUV(tmpsv));
1226 146 sv_setuv(tmpsv, 0);
1227 }
1228  
1229 146 ▶ if(ctx->cv)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx->cv) 143 3 146
1230 143 CvMETHOD_on(ctx->cv);
1231  
1232 146 ▶ if(!ctx->cv) {
Branch: 1 branch
BranchTrueFalseTotal
if(!ctx->cv) { 3 143 146
1233 /* This is a required method declaration for a role */
1234 /* TODO: This was a pretty rubbish way to detect that. We should remember it
1235 * more reliably */
1236  
1237 /* This already checks and complains if meta->type != METATYPE_ROLE */
1238 3 mop_class_add_required_method(compclassmeta, ctx->name);
1239 3 return;
1240 }
1241  
1242 143 switch(type) {
1243 101 case PHASER_NONE:
1244 101 ▶ if(ctx->cv && ctx->name && (ctx->actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL)) {
Branch: 3 branches
BranchTrueFalseTotal
if(ctx->cv && ctx->name && (ctx->actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL)) { 101 0 101
if(ctx->cv && ctx->name && (ctx->actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL)) { 96 5 101
if(ctx->cv && ctx->name && (ctx->actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL)) { 93 3 96
1245 93 MethodMeta *meta = mop_class_add_method(compclassmeta, ctx->name);
1246  
1247 92 meta->is_common = compmethodmeta->is_common;
1248 }
1249 100 break;
1250  
1251 17 case PHASER_BUILD:
1252 17 mop_class_add_BUILD(compclassmeta, ctx->cv); /* steal CV */
1253 17 break;
1254  
1255 25 case PHASER_ADJUST:
1256 case PHASER_ADJUSTPARAMS:
1257 25 mop_class_add_ADJUST(compclassmeta, ctx->cv); /* steal CV */
1258 25 break;
1259  
1260 0 case PHASER_APPLY:
1261 0 croak("ARHG unreachable wrong post_newcv for type=%d", type);
1262 }
1263  
1264 SV **varnamep;
1265 142 ▶ if((varnamep = hv_fetchs(ctx->moddata, "Object::Pad/method_varname", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if((varnamep = hv_fetchs(ctx->moddata, "Object::Pad/method_varname", 0))) { 1 141 142
1266 1 PADOFFSET padix = pad_add_name_sv(*varnamep, 0, NULL, NULL);
1267 1 intro_my();
1268  
1269 1 SV **svp = &PAD_SVl(padix);
1270  
1271 1 ▶ if(*svp)
Branch: 1 branch
BranchTrueFalseTotal
if(*svp) 1 0 1
1272 1 SvREFCNT_dec(*svp);
1273  
1274 1 *svp = newRV_inc((SV *)ctx->cv);
1275 1 SvREADONLY_on(*svp);
1276 }
1277  
1278 142 ▶ if(type != PHASER_NONE)
Branch: 1 branch
BranchTrueFalseTotal
if(type != PHASER_NONE) 42 100 142
1279 /* Do not generate REFGEN/ANONCODE optree, do not yield expression */
1280 42 ctx->actions &= ~(XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE|XS_PARSE_SUBLIKE_ACTION_RET_EXPR);
1281  
1282 142 SvREFCNT_dec(compmethodmeta->name);
1283 142 Safefree(compmethodmeta);
1284 }
1285  
1286 1 static void parse_classphaser_post_newcv(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
1287 {
1288 1 enum PhaserType type = PTR2UV(hookdata);
1289  
1290 1 switch(type) {
1291 1 case PHASER_APPLY:
1292 1 mop_class_add_APPLY(compclassmeta, ctx->cv); /* steal CV */
1293 1 break;
1294  
1295 0 case PHASER_NONE:
1296 case PHASER_BUILD:
1297 case PHASER_ADJUST:
1298 case PHASER_ADJUSTPARAMS:
1299 0 croak("ARHG unreachable wrong post_newcv for type=%d", type);
1300 }
1301  
1302 1 ctx->actions &= ~(XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE|XS_PARSE_SUBLIKE_ACTION_RET_EXPR);
1303 1 }
1304  
1305 static struct XSParseSublikeHooks parse_method_hooks = {
1306 .ver = 7,
1307 .flags = XS_PARSE_SUBLIKE_FLAG_FILTERATTRS |
1308 XS_PARSE_SUBLIKE_COMPAT_FLAG_DYNAMIC_ACTIONS |
1309 XS_PARSE_SUBLIKE_FLAG_BODY_OPTIONAL,
1310 .permit_hintkey = "Object::Pad/method",
1311 .permit = parse_method_permit,
1312 .pre_subparse = parse_method_pre_subparse,
1313 .filter_attr = parse_method_filter_attr,
1314 .post_blockstart = parse_method_post_blockstart,
1315 .pre_blockend = parse_method_pre_blockend,
1316 .post_newcv = parse_method_post_newcv,
1317  
1318 .start_signature = parse_method_start_signature,
1319 };
1320  
1321 static struct XSParseSublikeHooks parse_phaser_hooks = {
1322 .ver = 7,
1323 .flags = XS_PARSE_SUBLIKE_FLAG_FILTERATTRS |
1324 XS_PARSE_SUBLIKE_COMPAT_FLAG_DYNAMIC_ACTIONS,
1325 .skip_parts = XS_PARSE_SUBLIKE_PART_NAME,
1326 /* no permit */
1327 .pre_subparse = parse_method_pre_subparse,
1328 .filter_attr = parse_phaser_filter_attr,
1329 .post_blockstart = parse_method_post_blockstart,
1330 .pre_blockend = parse_method_pre_blockend,
1331 .post_newcv = parse_method_post_newcv,
1332  
1333 .start_signature = parse_method_start_signature,
1334 };
1335  
1336 static struct XSParseSublikeHooks parse_classphaser_hooks = {
1337 /* hooks for phasers that apply to entire classes but not instances */
1338 .flags = XS_PARSE_SUBLIKE_COMPAT_FLAG_DYNAMIC_ACTIONS,
1339 .skip_parts = XS_PARSE_SUBLIKE_PART_NAME,
1340 /* no permit */
1341 .pre_subparse = parse_classphaser_pre_subparse,
1342 .post_blockstart = parse_classphaser_post_blockstart,
1343 .pre_blockend = parse_classphaser_pre_blockend,
1344 .post_newcv = parse_classphaser_post_newcv,
1345 };
1346  
1347 50 static int parse_phaser(pTHX_ OP **out, void *hookdata)
1348 {
1349 50 enum PhaserType type = PTR2UV(hookdata);
1350 50 HV *hints = GvHV(PL_hintgv);
1351  
1352 50 ▶ if(!have_compclassmeta)
Branch: 1 branch
BranchTrueFalseTotal
if(!have_compclassmeta) 0 50 50
1353 0 croak("Cannot '%s' outside of 'class'", phasertypename[PTR2UV(hookdata)]);
1354  
1355 50 lex_read_space(0);
1356  
1357 50 ▶ if(type == PHASER_ADJUST && compclassmeta->composed_adjust) {
Branch: 2 branches
BranchTrueFalseTotal
if(type == PHASER_ADJUST && compclassmeta->composed_adjust) { 28 22 50
if(type == PHASER_ADJUST && compclassmeta->composed_adjust) { 6 22 28
1358 6 ClassMeta *classmeta = compclassmeta;
1359  
1360 6 ENTER;
1361  
1362 6 resume_compcv_and_save(&classmeta->adjust_compcv);
1363  
1364 6 bool do_params = false;
1365  
1366 6 ▶ if(lex_consume_unichar(':')) {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_consume_unichar(':')) { 1 5 6
1367 1 lex_read_space(0);
1368  
1369 1 SV *name = sv_newmortal(), *val = sv_newmortal();
1370 /* A custom copy of lex_scan_attrs() because we only care about one thing */
1371 2 ▶ while(lex_scan_attrval_into(name, val)) {
Branch: 1 branch
BranchTrueFalseTotal
while(lex_scan_attrval_into(name, val)) { 1 1 2
1372 1 lex_read_space(0);
1373  
1374 1 ▶ if(!strEQ(SvPVX(name), "params"))
Branch: 1 branch
BranchTrueFalseTotal
if(!strEQ(SvPVX(name), "params")) 0 1 1
1375 // Normally core perl makes this complaint; we'll have to make do here
1376 0 SvPOK(val) ? croak("Invalid CODE attribute %" SVf "(%" SVf ")", SVfARG(name), SVfARG(val))
1377 0 ▶ : croak("Invalid CODE attribute %" SVf, SVfARG(name));
Branch: 1 branch
BranchTrueFalseTotal
: croak("Invalid CODE attribute %" SVf, SVfARG(name)); 0 0 0
1378  
1379 // ignore the value - even its mere presence
1380 1 do_params = true;
1381  
1382 1 ▶ if(lex_peek_unichar(0) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ':') { 0 1 1
1383 0 lex_read_unichar(0);
1384 0 lex_read_space(0);
1385 }
1386 }
1387 }
1388  
1389 6 U32 nfields = av_count(classmeta->fields);
1390  
1391 6 ▶ if(classmeta->next_field_for_adjust < nfields) {
Branch: 1 branch
BranchTrueFalseTotal
if(classmeta->next_field_for_adjust < nfields) { 3 3 6
1392 3 ENTER;
1393 3 SAVESPTR(PL_comppad);
1394 3 SAVESPTR(PL_comppad_name);
1395 3 SAVESPTR(PL_curpad);
1396  
1397 3 CV *fieldscope = CvOUTSIDE(PL_compcv);
1398  
1399 3 PL_comppad = PadlistARRAY(CvPADLIST(fieldscope))[1];
1400 3 PL_comppad_name = PadlistNAMES(CvPADLIST(fieldscope));
1401 3 PL_curpad = AvARRAY(PL_comppad);
1402  
1403 3 add_fields_to_pad(classmeta, classmeta->next_field_for_adjust);
1404  
1405 3 intro_my();
1406  
1407 3 LEAVE;
1408  
1409 3 classmeta->next_field_for_adjust = nfields;
1410 }
1411  
1412 6 CvOUTSIDE_SEQ(PL_compcv) = PL_cop_seqmax;
1413  
1414 6 ▶ if(do_params) {
Branch: 1 branch
BranchTrueFalseTotal
if(do_params) { 1 5 6
1415 1 parse_adjust_params(classmeta, classmeta->adjust_params);
1416 }
1417  
1418 6 OP *body = parse_block(0);
1419 6 ▶ if(!body || PL_parser->error_count) {
Branch: 2 branches
BranchTrueFalseTotal
if(!body || PL_parser->error_count) { 6 0 6
if(!body || PL_parser->error_count) { 0 6 6
1420 0 croak("syntax error");
1421 }
1422  
1423 6 classmeta->adjust_lines = op_append_list(OP_LINESEQ, classmeta->adjust_lines,
1424 body);
1425  
1426 6 LEAVE;
1427  
1428 6 return KEYWORD_PLUGIN_STMT;
1429 }
1430  
1431 44 switch(type) {
1432 43 case PHASER_NONE:
1433 case PHASER_BUILD:
1434 case PHASER_ADJUST:
1435 case PHASER_ADJUSTPARAMS:
1436 43 return xs_parse_sublike(&parse_phaser_hooks, hookdata, out);
1437  
1438 1 case PHASER_APPLY:
1439 1 ▶ if(!hv_fetchs(hints, "Object::Pad/experimental(apply_phaser)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(!hv_fetchs(hints, "Object::Pad/experimental(apply_phaser)", 0)) 0 1 1
1440 0 Perl_ck_warner(aTHX_ packWARN(WARN_EXPERIMENTAL),
1441 "APPLY phasers are experimental and may be changed or removed without notice");
1442 1 return xs_parse_sublike(&parse_classphaser_hooks, hookdata, out);
1443 }
1444  
1445 0 croak("ARGH unreachable: unhandled phaser type %d", type);
1446 }
1447  
1448 static const struct XSParseKeywordHooks kwhooks_BUILD = {
1449 .permit_hintkey = "Object::Pad/BUILD",
1450 .parse = &parse_phaser,
1451 };
1452  
1453 static const struct XSParseKeywordHooks kwhooks_ADJUST = {
1454 .permit_hintkey = "Object::Pad/ADJUST",
1455 .parse = &parse_phaser,
1456 };
1457  
1458 static const struct XSParseKeywordHooks kwhooks_APPLY = {
1459 .permit_hintkey = "Object::Pad/APPLY",
1460 .parse = &parse_phaser,
1461 };
1462  
1463 4 static void check_uuCLASS(pTHX_ void *hookdata)
1464 {
1465 /* We test this other hints key purely to get a more useful error message
1466 * in cases like class X { say "My class is", __CLASS__; }
1467 */
1468  
1469 SV **svp;
1470 4 ▶ if(!(svp = hv_fetchs(GvHV(PL_hintgv), "Object::Pad/__CLASS__", 0)) ||
Branch: 1 branch
BranchTrueFalseTotal
if(!(svp = hv_fetchs(GvHV(PL_hintgv), "Object::Pad/__CLASS__", 0)) || 3 1 4
1471 3 ▶ !SvTRUE(*svp))
Branch: 1 branch
BranchTrueFalseTotal
!SvTRUE(*svp)) 0 3 3
1472 1 croak("Cannot use __CLASS__ outside of a method, ADJUST phaser or field initialiser");
1473 3 }
1474  
1475 3 static OP *pp_curclass(pTHX)
1476 {
1477 3 dSP;
1478  
1479 3 SV *self = PAD_SVl(PADIX_SELF);
1480  
1481 assert(SvROK(self) && SvOBJECT(SvRV(self)));
1482  
1483 3 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 3 3
1484  
1485 3 PUSHs(sv_newmortal());
1486 #if HAVE_PERL_VERSION(5, 24, 0)
1487 3 sv_ref(*SP, SvRV(self), TRUE);
1488 #else
1489 HV *stash = SvSTASH(SvRV(self));
1490 sv_setpv(*SP, HvNAME(stash));
1491 if(HvNAMEUTF8(stash))
1492 SvUTF8_on(*SP);
1493 #endif
1494  
1495 3 RETURN;
1496 }
1497  
1498 3 static int build_uuCLASS(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
1499 {
1500 3 *out = newOP_CUSTOM(&pp_curclass, 0);
1501  
1502 3 return KEYWORD_PLUGIN_EXPR;
1503 }
1504  
1505 static const struct XSParseKeywordHooks kwhooks_uuCLASS = {
1506 .flags = XPK_FLAG_EXPR,
1507 .permit_hintkey = "Object::Pad/class",
1508  
1509 .check = &check_uuCLASS,
1510  
1511 .pieces = (const struct XSParseKeywordPieceType []){ {0} },
1512 .build = &build_uuCLASS,
1513 };
1514  
1515 2 static void check_requires(pTHX_ void *hookdata)
1516 {
1517 2 ▶ if(!have_compclassmeta)
Branch: 1 branch
BranchTrueFalseTotal
if(!have_compclassmeta) 0 2 2
1518 0 croak("Cannot 'requires' outside of 'role'");
1519  
1520 2 ▶ if(compclassmeta->type == METATYPE_CLASS)
Branch: 1 branch
BranchTrueFalseTotal
if(compclassmeta->type == METATYPE_CLASS) 0 2 2
1521 0 croak("A class may not declare required methods");
1522 2 }
1523  
1524 2 static int build_requires(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
1525 {
1526 2 SV *mname = args[0]->sv;
1527  
1528 2 ClassMeta *meta = compclassmeta;
1529  
1530 2 mop_class_begin(meta);
1531  
1532 2 mop_class_add_required_method(meta, mname);
1533  
1534 2 *out = newOP(OP_NULL, 0);
1535  
1536 2 return KEYWORD_PLUGIN_STMT;
1537 }
1538  
1539 static const struct XSParseKeywordHooks kwhooks_requires = {
1540 .flags = XPK_FLAG_STMT|XPK_FLAG_AUTOSEMI,
1541 .permit_hintkey = "Object::Pad/requires",
1542  
1543 .check = &check_requires,
1544  
1545 .pieces = (const struct XSParseKeywordPieceType []){
1546 XPK_WARNING_DEPRECATED("'requires' is now discouraged; use an empty 'method NAME;' declaration instead"),
1547 XPK_IDENT,
1548 {0}
1549 },
1550 .build = &build_requires,
1551 };
1552  
1553 #ifdef HAVE_DMD_HELPER
1554 static void dump_fieldmeta(pTHX_ DMDContext *ctx, FieldMeta *fieldmeta)
1555 {
1556 DMD_DUMP_STRUCT(ctx, "Object::Pad/FieldMeta", fieldmeta, sizeof(FieldMeta),
1557 7, ((const DMDNamedField []){
1558 {"the name SV", DMD_FIELD_PTR, .ptr = fieldmeta->name},
1559 {"is direct", DMD_FIELD_BOOL, .b = fieldmeta->is_direct},
1560 {"the class", DMD_FIELD_PTR, .ptr = fieldmeta->class},
1561 {"the default value SV", DMD_FIELD_PTR, .ptr = mop_field_get_default_sv(fieldmeta)},
1562 /* TODO: Maybe hunt for constants in the defaultexpr optree fragment? */
1563 {"fieldix", DMD_FIELD_UINT, .n = fieldmeta->fieldix},
1564 {"the :param name SV", DMD_FIELD_PTR, .ptr = fieldmeta->paramname},
1565 {"the hooks AV", DMD_FIELD_PTR, .ptr = fieldmeta->hooks},
1566 })
1567 );
1568 }
1569  
1570 static void dump_methodmeta(pTHX_ DMDContext *ctx, MethodMeta *methodmeta)
1571 {
1572 DMD_DUMP_STRUCT(ctx, "Object::Pad/MethodMeta", methodmeta, sizeof(MethodMeta),
1573 4, ((const DMDNamedField []){
1574 {"the name SV", DMD_FIELD_PTR, .ptr = methodmeta->name},
1575 {"the class", DMD_FIELD_PTR, .ptr = methodmeta->class},
1576 {"the origin role", DMD_FIELD_PTR, .ptr = methodmeta->role},
1577 {"is_common", DMD_FIELD_BOOL, .b = methodmeta->is_common},
1578 })
1579 );
1580 }
1581  
1582 static void dump_parammeta(pTHX_ DMDContext *ctx, ParamMeta *parammeta)
1583 {
1584 switch(parammeta->type) {
1585 case PARAM_FIELD:
1586 DMD_DUMP_STRUCT(ctx, "Object::Pad/ParamMeta.field", parammeta, sizeof(ParamMeta),
1587 4, ((const DMDNamedField []){
1588 {"the name SV", DMD_FIELD_PTR, .ptr = parammeta->name},
1589 {"the class", DMD_FIELD_PTR, .ptr = parammeta->class},
1590 {"the field", DMD_FIELD_PTR, .ptr = parammeta->field.fieldmeta},
1591 {"fieldix", DMD_FIELD_UINT, .n = parammeta->field.fieldix},
1592 })
1593 );
1594 break;
1595  
1596 case PARAM_ADJUST:
1597 DMD_DUMP_STRUCT(ctx, "Object::Pad/ParamMeta.adjust", parammeta, sizeof(ParamMeta),
1598 3, ((const DMDNamedField []){
1599 {"the name SV", DMD_FIELD_PTR, .ptr = parammeta->name},
1600 {"the class", DMD_FIELD_PTR, .ptr = parammeta->class},
1601 {"padix", DMD_FIELD_UINT, .n = parammeta->adjust.padix},
1602 /* No point dumping the defexpr because Devel::MAT can't peek into them */
1603 })
1604 );
1605 break;
1606 }
1607 }
1608  
1609 static void dump_roleembedding(pTHX_ DMDContext *ctx, RoleEmbedding *embedding)
1610 {
1611 DMD_DUMP_STRUCT(ctx, "Object::Pad/RoleEmbedding", embedding, sizeof(RoleEmbedding),
1612 4, ((const DMDNamedField []){
1613 {"the embedding SV", DMD_FIELD_PTR, .ptr = embedding->embeddingsv},
1614 {"the role", DMD_FIELD_PTR, .ptr = embedding->rolemeta},
1615 {"the class", DMD_FIELD_PTR, .ptr = embedding->classmeta},
1616 {"offset", DMD_FIELD_UINT, .n = embedding->offset}
1617 })
1618 );
1619 }
1620  
1621 static void dump_classmeta(pTHX_ DMDContext *ctx, ClassMeta *classmeta)
1622 {
1623 /* We'll handle the two types of classmeta by claiming two different struct
1624 * types
1625 */
1626  
1627 #define N_COMMON_FIELDS 16
1628 #define COMMON_FIELDS \
1629 {"type", DMD_FIELD_U8, .n = classmeta->type}, \
1630 {"repr", DMD_FIELD_U8, .n = classmeta->repr}, \
1631 {"sealed", DMD_FIELD_BOOL, .b = classmeta->sealed}, \
1632 {"start_fieldix", DMD_FIELD_UINT, .n = classmeta->start_fieldix}, \
1633 {"the name SV", DMD_FIELD_PTR, .ptr = classmeta->name}, \
1634 {"the stash SV", DMD_FIELD_PTR, .ptr = classmeta->stash}, \
1635 {"the pending submeta AV", DMD_FIELD_PTR, .ptr = classmeta->pending_submeta}, \
1636 {"the hooks AV", DMD_FIELD_PTR, .ptr = classmeta->hooks}, \
1637 {"the fields AV", DMD_FIELD_PTR, .ptr = classmeta->fields}, \
1638 {"the direct methods AV", DMD_FIELD_PTR, .ptr = classmeta->direct_methods}, \
1639 {"the param map HV", DMD_FIELD_PTR, .ptr = classmeta->parammap}, \
1640 {"the requiremethods AV", DMD_FIELD_PTR, .ptr = classmeta->requiremethods}, \
1641 {"the initfields CV", DMD_FIELD_PTR, .ptr = classmeta->initfields}, \
1642 {"the BUILD phasers AV", DMD_FIELD_PTR, .ptr = classmeta->buildcvs}, \
1643 {"the ADJUST phasers AV", DMD_FIELD_PTR, .ptr = classmeta->adjustcvs}, \
1644 {"the temporary method scope", DMD_FIELD_PTR, .ptr = classmeta->methodscope}
1645  
1646 switch(classmeta->type) {
1647 case METATYPE_CLASS:
1648 DMD_DUMP_STRUCT(ctx, "Object::Pad/ClassMeta.class", classmeta, sizeof(ClassMeta),
1649 N_COMMON_FIELDS+5, ((const DMDNamedField []){
1650 COMMON_FIELDS,
1651 {"the supermeta", DMD_FIELD_PTR, .ptr = classmeta->cls.supermeta},
1652 {"the foreign superclass constructor CV", DMD_FIELD_PTR, .ptr = classmeta->cls.foreign_new},
1653 {"the foreign superclass DOES CV", DMD_FIELD_PTR, .ptr = classmeta->cls.foreign_does},
1654 {"the direct roles AV", DMD_FIELD_PTR, .ptr = classmeta->cls.direct_roles},
1655 {"the embedded roles AV", DMD_FIELD_PTR, .ptr = classmeta->cls.embedded_roles},
1656 })
1657 );
1658 break;
1659  
1660 case METATYPE_ROLE:
1661 DMD_DUMP_STRUCT(ctx, "Object::Pad/ClassMeta.role", classmeta, sizeof(ClassMeta),
1662 N_COMMON_FIELDS+3, ((const DMDNamedField []){
1663 COMMON_FIELDS,
1664 {"the superroles AV", DMD_FIELD_PTR, .ptr = classmeta->role.superroles},
1665 {"the role applied classes HV", DMD_FIELD_PTR, .ptr = classmeta->role.applied_classes},
1666 {"the role APPLY phasers AV", DMD_FIELD_PTR, .ptr = classmeta->role.applycvs},
1667 })
1668 );
1669 break;
1670 }
1671  
1672 #undef COMMON_FIELDS
1673  
1674 I32 i;
1675  
1676 for(i = 0; i < av_count(classmeta->fields); i++) {
1677 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(classmeta->fields)[i]);
1678  
1679 dump_fieldmeta(aTHX_ ctx, fieldmeta);
1680 }
1681  
1682 for(i = 0; i < av_count(classmeta->direct_methods); i++) {
1683 MethodMeta *methodmeta = MUST_METHODMETA(AvARRAY(classmeta->direct_methods)[i]);
1684  
1685 dump_methodmeta(aTHX_ ctx, methodmeta);
1686 }
1687  
1688 HV *parammap;
1689 if((parammap = classmeta->parammap)) {
1690 hv_iterinit(parammap);
1691  
1692 HE *iter;
1693 while((iter = hv_iternext(parammap))) {
1694 ParamMeta *parammeta = MUST_PARAMMETA(HeVAL(iter));
1695  
1696 dump_parammeta(aTHX_ ctx, parammeta);
1697 }
1698 }
1699  
1700 switch(classmeta->type) {
1701 case METATYPE_CLASS:
1702 for(i = 0; i < av_count(classmeta->cls.direct_roles); i++) {
1703 RoleEmbedding *embedding = MUST_ROLEEMBEDDING(AvARRAY(classmeta->cls.direct_roles)[i]);
1704  
1705 dump_roleembedding(aTHX_ ctx, embedding);
1706 }
1707 break;
1708  
1709 case METATYPE_ROLE:
1710 /* No need to dump the values of role.applied_classes because any class
1711 * they're applied to will have done that already */
1712 break;
1713 }
1714 }
1715  
1716 static int dumppackage_class(pTHX_ DMDContext *ctx, const SV *sv)
1717 {
1718 int ret = 0;
1719  
1720 ClassMeta *meta = MUST_CLASSMETA(SvUV((SV *)sv));
1721  
1722 dump_classmeta(aTHX_ ctx, meta);
1723  
1724 ret += DMD_ANNOTATE_SV(sv, (SV *)meta, "the Object::Pad class");
1725  
1726 return ret;
1727 }
1728 #endif
1729  
1730 /*********************
1731 * Custom FieldHooks *
1732 *********************/
1733  
1734 struct CustomFieldHookData
1735 {
1736 SV *apply_cb;
1737 };
1738  
1739 3 static bool fieldhook_custom_apply(pTHX_ FieldMeta *fieldmeta, SV *value, SV **hookdata_ptr, void *_funcdata)
1740 {
1741 3 struct CustomFieldHookData *funcdata = _funcdata;
1742  
1743 SV *cb;
1744 3 ▶ if((cb = funcdata->apply_cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if((cb = funcdata->apply_cb)) { 3 0 3
1745 3 dSP;
1746 3 ENTER;
1747 3 SAVETMPS;
1748  
1749 3 SV *fieldmetasv = sv_newmortal();
1750 3 sv_setref_uv(fieldmetasv, "Object::Pad::MOP::Field", PTR2UV(fieldmeta));
1751  
1752 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
1753 3 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 3 3
1754 3 PUSHs(fieldmetasv);
1755 3 PUSHs(value);
1756 3 PUTBACK;
1757  
1758 3 call_sv(cb, G_SCALAR);
1759  
1760 3 SPAGAIN;
1761 3 SV *ret = POPs;
1762 3 *hookdata_ptr = SvREFCNT_inc(ret);
1763  
1764 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
1765 3 LEAVE;
1766 }
1767  
1768 3 return TRUE;
1769 }
1770  
1771 /* internal function shared by various *.c files */
1772 590 void ObjectPad__need_PLparser(pTHX)
1773 {
1774 590 ▶ if(!PL_parser) {
Branch: 1 branch
BranchTrueFalseTotal
if(!PL_parser) { 8 582 590
1775 /* We need to generate just enough of a PL_parser to keep newSTATEOP()
1776 * happy, otherwise it will SIGSEGV (RT133258)
1777 */
1778 8 SAVEVPTR(PL_parser);
1779 8 Newxz(PL_parser, 1, yy_parser);
1780 8 SAVEFREEPV(PL_parser);
1781  
1782 8 PL_parser->copline = NOLINE;
1783 #if HAVE_PERL_VERSION(5, 20, 0)
1784 8 PL_parser->preambling = NOLINE;
1785 #endif
1786 }
1787 590 }
1788  
1789 /* used by XSUB deconstruct_object */
1790 #define deconstruct_object_class(fieldstore, classmeta, offset) S_deconstruct_object_class(aTHX_ fieldstore, classmeta, offset)
1791 10 static U32 S_deconstruct_object_class(pTHX_ SV *fieldstore, ClassMeta *classmeta, FIELDOFFSET offset)
1792 {
1793 10 dSP;
1794 10 U32 retcount = 0;
1795 10 AV *fields = classmeta->fields;
1796 10 U32 nfields = av_count(fields);
1797  
1798 10 ▶ EXTEND(SP, nfields * 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, nfields * 2); 0 10 10
1799  
1800 10 ▶ SV **fieldsvs = fieldstore_fields(fieldstore);
Branch: 1 branch
BranchTrueFalseTotal
SV **fieldsvs = fieldstore_fields(fieldstore); 4 6 10
1801  
1802 FIELDOFFSET i;
1803 22 ▶ for(i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) { 12 10 22
1804 12 FieldMeta *fieldmeta = MUST_FIELDMETA(AvARRAY(fields)[i]);
1805 12 ▶ if(!fieldmeta->is_direct)
Branch: 1 branch
BranchTrueFalseTotal
if(!fieldmeta->is_direct) 1 11 12
1806 1 continue;
1807  
1808 11 mPUSHs(newSVpvf("%" SVf ".%" SVf,
1809 SVfARG(classmeta->name), SVfARG(fieldmeta->name)));
1810  
1811 11 SV *value = fieldsvs[fieldmeta->fieldix + offset];
1812 22 switch(SvPV_nolen(fieldmeta->name)[0]) {
1813 9 case '$':
1814 9 value = newSVsv(value);
1815 9 break;
1816  
1817 1 case '@':
1818 1 value = newRV_noinc((SV *)newAVav(AV_FROM_REF(value)));
1819 1 break;
1820  
1821 1 case '%':
1822 1 value = newRV_noinc((SV *)newHVhv(HV_FROM_REF(value)));
1823 1 break;
1824 }
1825  
1826 11 mPUSHs(value);
1827  
1828 11 retcount += 2;
1829 }
1830  
1831 10 PUTBACK;
1832  
1833 10 return retcount;
1834 }
1835  
1836 /* used by XSUB ref_field */
1837 #define ref_field_class(want_fieldname, fieldstore, classmeta, offset) S_ref_field_class(aTHX_ want_fieldname, fieldstore, classmeta, offset)
1838 13 static SV *S_ref_field_class(pTHX_ SV *want_fieldname, SV *fieldstore, ClassMeta *classmeta, FIELDOFFSET offset)
1839 {
1840 13 FieldMeta *fieldmeta = mop_class_find_field(classmeta, want_fieldname, 0);
1841  
1842 13 ▶ if(!fieldmeta)
Branch: 1 branch
BranchTrueFalseTotal
if(!fieldmeta) 0 13 13
1843 0 return NULL;
1844  
1845 /* found it */
1846 13 ▶ SV *sv = fieldstore_fields(fieldstore)[fieldmeta->fieldix + offset];
Branch: 1 branch
BranchTrueFalseTotal
SV *sv = fieldstore_fields(fieldstore)[fieldmeta->fieldix + offset]; 3 10 13
1847 13 switch(mop_field_get_sigil(fieldmeta)) {
1848 7 case '$':
1849 7 return newRV_inc(sv);
1850  
1851 6 case '@':
1852 case '%':
1853 6 return newSVsv(sv);
1854 }
1855  
1856 0 return NULL;
1857 }
1858  
1859 /* Handy functions for MOP wrapper methods */
1860 #define MUST_CLASSMETA_FROM_RV(self) S_must_classmeta_from_rv(aTHX_ self)
1861 74 static ClassMeta *S_must_classmeta_from_rv(pTHX_ SV *self)
1862 {
1863 74 ▶ if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Class")))
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Class"))) 74 0 74
if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Class"))) 0 74 74
1864 0 croak("Expected an Object::Pad::MOP::Class instance");
1865  
1866 74 return MUST_CLASSMETA(NUM2PTR(ClassMeta *, SvUV(SvRV(self))));
1867 }
1868  
1869 #define MUST_FIELDMETA_FROM_RV(self) S_must_fieldmeta_from_rv(aTHX_ self)
1870 30 static FieldMeta *S_must_fieldmeta_from_rv(pTHX_ SV *self)
1871 {
1872 30 ▶ if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Field")))
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Field"))) 30 0 30
if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Field"))) 0 30 30
1873 0 croak("Expected an Object::Pad::MOP::Field instance");
1874  
1875 30 return MUST_FIELDMETA(NUM2PTR(FieldMeta *, SvUV(SvRV(self))));
1876 }
1877  
1878 #define MUST_METHODMETA_FROM_RV(self) S_must_methodmeta_from_rv(aTHX_ self)
1879 10 static MethodMeta *S_must_methodmeta_from_rv(pTHX_ SV *self)
1880 {
1881 10 ▶ if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Method")))
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Method"))) 10 0 10
if(!(SvROK(self) && sv_derived_from(self, "Object::Pad::MOP::Method"))) 0 10 10
1882 0 croak("Expected an Object::Pad::MOP::Method instance");
1883  
1884 10 return MUST_METHODMETA(NUM2PTR(MethodMeta *, SvUV(SvRV(self))));
1885 }
1886  
1887 MODULE = Object::Pad PACKAGE = Object::Pad::MOP::Class
1888  
1889 INCLUDE: mop-class.xsi
1890  
1891 MODULE = Object::Pad PACKAGE = Object::Pad::MOP::Method
1892  
1893 INCLUDE: mop-method.xsi
1894  
1895 MODULE = Object::Pad PACKAGE = Object::Pad::MOP::Field
1896  
1897 INCLUDE: mop-field.xsi
1898  
1899 MODULE = Object::Pad PACKAGE = Object::Pad::MOP::FieldAttr
1900  
1901 void
1902 register(class, name, ...)
1903 SV *class
1904 SV *name
1905 CODE:
1906 {
1907 PERL_UNUSED_VAR(class);
1908 1 dKWARG(2);
1909  
1910 {
1911 1 ▶ if(!cophh_exists_pvs(CopHINTHASH_get(PL_curcop), "Object::Pad/experimental(custom_field_attr)", 0))
Branch: 1 branch
BranchTrueFalseTotal
if(!cophh_exists_pvs(CopHINTHASH_get(PL_curcop), "Object::Pad/experimental(custom_field_attr)", 0)) 0 1 1
1912 0 Perl_ck_warner(aTHX_ packWARN(WARN_EXPERIMENTAL),
1913 "Object::Pad::MOP::FieldAttr is experimental and may be changed or removed without notice");
1914 }
1915  
1916 1 struct FieldHookFuncs funcs = {};
1917  
1918 1 struct CustomFieldHookData funcdata = {};
1919  
1920 1 funcs.ver = OBJECTPAD_ABIVERSION;
1921  
1922 1 funcs.apply = &fieldhook_custom_apply;
1923  
1924 static const char *args[] = {
1925 "permit_hintkey",
1926 "apply",
1927 "no_value",
1928 "must_value",
1929 NULL,
1930 };
1931 5 ▶ while(KWARG_NEXT(args)) {
Branch: 1 branch
BranchTrueFalseTotal
while(KWARG_NEXT(args)) { 3 1 4
1932 3 switch(kwarg) {
1933 1 case 0: /* permit_hintkey */
1934 1 funcs.permit_hintkey = SvPV_nolen(kwval);
1935 1 break;
1936  
1937 1 case 1: /* apply */
1938 1 funcdata.apply_cb = kwval;
1939 1 break;
1940  
1941 0 case 2: /* no_value */
1942 0 ▶ if(SvTRUE(kwval))
Branch: 1 branch
BranchTrueFalseTotal
if(SvTRUE(kwval)) 0 0 0
1943 0 funcs.flags |= OBJECTPAD_FLAG_ATTR_NO_VALUE;
1944 0 break;
1945  
1946 1 case 3: /* must_value */
1947 1 ▶ if(SvTRUE(kwval))
Branch: 1 branch
BranchTrueFalseTotal
if(SvTRUE(kwval)) 1 0 1
1948 1 funcs.flags |= OBJECTPAD_FLAG_ATTR_MUST_VALUE;
1949 1 break;
1950 }
1951 }
1952  
1953 1 ▶ if((funcs.flags & OBJECTPAD_FLAG_ATTR_NO_VALUE) &&
Branch: 1 branch
BranchTrueFalseTotal
if((funcs.flags & OBJECTPAD_FLAG_ATTR_NO_VALUE) && 0 1 1
1954 0 ▶ (funcs.flags & OBJECTPAD_FLAG_ATTR_MUST_VALUE))
Branch: 1 branch
BranchTrueFalseTotal
(funcs.flags & OBJECTPAD_FLAG_ATTR_MUST_VALUE)) 0 0 0
1955 0 croak("Cannot register a FieldAttr with both 'no_value' and 'must_value'");
1956  
1957 struct FieldHookFuncs *_funcs;
1958 1 Newxz(_funcs, 1, struct FieldHookFuncs);
1959 1 Copy(&funcs, _funcs, 1, struct FieldHookFuncs);
1960 1 ▶ if(_funcs->permit_hintkey)
Branch: 1 branch
BranchTrueFalseTotal
if(_funcs->permit_hintkey) 1 0 1
1961 1 _funcs->permit_hintkey = savepv(_funcs->permit_hintkey);
1962  
1963 struct CustomFieldHookData *_funcdata;
1964 1 Newxz(_funcdata, 1, struct CustomFieldHookData);
1965 1 Copy(&funcdata, _funcdata, 1, struct CustomFieldHookData);
1966 1 ▶ if(_funcdata->apply_cb)
Branch: 1 branch
BranchTrueFalseTotal
if(_funcdata->apply_cb) 1 0 1
1967 1 _funcdata->apply_cb = newSVsv(_funcdata->apply_cb);
1968  
1969 1 register_field_attribute(savepv(SvPV_nolen(name)), _funcs, _funcdata);
1970 }
1971  
1972 MODULE = Object::Pad PACKAGE = Object::Pad::MetaFunctions
1973  
1974 SV *
1975 metaclass(SV *obj)
1976 CODE:
1977 {
1978 1 ▶ if(!SvROK(obj) || !SvOBJECT(SvRV(obj)))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(obj) || !SvOBJECT(SvRV(obj))) 1 0 1
if(!SvROK(obj) || !SvOBJECT(SvRV(obj))) 0 1 1
1979 0 croak("Expected an object reference to metaclass");
1980  
1981 1 HV *stash = SvSTASH(SvRV(obj));
1982  
1983 1 GV **gvp = (GV **)hv_fetchs(stash, "META", 0);
1984 1 ▶ if(!gvp)
Branch: 1 branch
BranchTrueFalseTotal
if(!gvp) 0 1 1
1985 0 ▶ croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash)));
Branch: 3 branches
BranchTrueFalseTotal
croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash))); 0 0 0
croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash))); 0 0 0
croak("Unable to find ClassMeta for %" HEKf, HEKfARG(HvNAME_HEK(stash))); 0 0 0
1986  
1987 1 RETVAL = newSVsv(GvSV(*gvp));
1988 }
1989 OUTPUT:
1990 RETVAL
1991  
1992 void
1993 deconstruct_object(SV *obj)
1994 PPCODE:
1995 {
1996 4 ▶ if(!SvROK(obj) || !SvOBJECT(SvRV(obj)))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(obj) || !SvOBJECT(SvRV(obj))) 4 0 4
if(!SvROK(obj) || !SvOBJECT(SvRV(obj))) 0 4 4
1997 0 croak("Expected an object reference to deconstruct_object");
1998  
1999 4 ClassMeta *classmeta = mop_get_class_for_stash(SvSTASH(SvRV(obj)));
2000  
2001 4 SV *fieldstore = get_obj_fieldstore(obj, classmeta->repr, true);
2002  
2003 4 U32 retcount = 0;
2004  
2005 4 PUSHs(sv_mortalcopy(classmeta->name));
2006 4 retcount++;
2007  
2008 4 PUTBACK;
2009  
2010 12 ▶ while(classmeta) {
Branch: 1 branch
BranchTrueFalseTotal
while(classmeta) { 8 4 12
2011 8 retcount += deconstruct_object_class(fieldstore, classmeta, 0);
2012  
2013 8 AV *roles = classmeta->cls.direct_roles;
2014 8 U32 nroles = av_count(roles);
2015 10 ▶ for(U32 i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(U32 i = 0; i < nroles; i++) { 2 8 10
2016 2 RoleEmbedding *embedding = MUST_ROLEEMBEDDING(AvARRAY(roles)[i]);
2017  
2018 2 retcount += deconstruct_object_class(fieldstore, embedding->rolemeta, embedding->offset);
2019 }
2020  
2021 8 classmeta = classmeta->cls.supermeta;
2022 }
2023  
2024 4 SPAGAIN;
2025 4 XSRETURN(retcount);
2026 }
2027  
2028 SV *
2029 ref_field(SV *fieldname, SV *obj)
2030 CODE:
2031 {
2032 13 SV *want_classname = NULL, *want_fieldname;
2033  
2034 13 ▶ if(!SvROK(obj) || !SvOBJECT(SvRV(obj)))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvROK(obj) || !SvOBJECT(SvRV(obj))) 13 0 13
if(!SvROK(obj) || !SvOBJECT(SvRV(obj))) 0 13 13
2035 0 croak("Expected an object reference to ref_field");
2036  
2037 13 SvGETMAGIC(fieldname);
2038  
2039 13 char *s = SvPV_nolen(fieldname);
2040 char *dotpos;
2041 13 ▶ if((dotpos = strchr(s, '.'))) {
Branch: 1 branch
BranchTrueFalseTotal
if((dotpos = strchr(s, '.'))) { 7 6 13
2042 7 U32 flags = SvUTF8(fieldname) ? SVf_UTF8 : 0;
2043 7 want_classname = newSVpvn_flags(s, dotpos - s, flags);
2044 7 want_fieldname = newSVpvn_flags(dotpos + 1, strlen(dotpos + 1), flags);
2045 }
2046 else {
2047 6 want_fieldname = SvREFCNT_inc(fieldname);
2048 }
2049  
2050 13 SAVEFREESV(want_classname);
2051 13 SAVEFREESV(want_fieldname);
2052  
2053 13 ClassMeta *classmeta = mop_get_class_for_stash(SvSTASH(SvRV(obj)));
2054  
2055 13 SV *fieldstore = get_obj_fieldstore(obj, classmeta->repr, true);
2056  
2057 16 ▶ while(classmeta) {
Branch: 1 branch
BranchTrueFalseTotal
while(classmeta) { 16 0 16
2058 16 ▶ if(!want_classname || sv_eq(want_classname, classmeta->name)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!want_classname || sv_eq(want_classname, classmeta->name)) { 10 6 16
if(!want_classname || sv_eq(want_classname, classmeta->name)) { 5 5 10
2059 11 RETVAL = ref_field_class(want_fieldname, fieldstore, classmeta, 0);
2060 11 ▶ if(RETVAL)
Branch: 1 branch
BranchTrueFalseTotal
if(RETVAL) 11 0 11
2061 11 goto done;
2062 }
2063  
2064 5 AV *roles = classmeta->cls.direct_roles;
2065 5 U32 nroles = av_count(roles);
2066 7 ▶ for(U32 i = 0; i < nroles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(U32 i = 0; i < nroles; i++) { 4 3 7
2067 4 RoleEmbedding *embedding = MUST_ROLEEMBEDDING(AvARRAY(roles)[i]);
2068  
2069 4 ▶ if(!want_classname || sv_eq(want_classname, embedding->rolemeta->name)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!want_classname || sv_eq(want_classname, embedding->rolemeta->name)) { 4 0 4
if(!want_classname || sv_eq(want_classname, embedding->rolemeta->name)) { 2 2 4
2070 2 RETVAL = ref_field_class(want_fieldname, fieldstore, embedding->rolemeta, embedding->offset);
2071 2 ▶ if(RETVAL)
Branch: 1 branch
BranchTrueFalseTotal
if(RETVAL) 2 0 2
2072 2 goto done;
2073 }
2074 }
2075  
2076 3 classmeta = classmeta->cls.supermeta;
2077 }
2078  
2079 0 ▶ if(want_classname)
Branch: 1 branch
BranchTrueFalseTotal
if(want_classname) 0 0 0
2080 0 croak("Could not find a field called %" SVf " in class %" SVf,
2081 SVfARG(want_fieldname), SVfARG(want_classname));
2082 else
2083 0 croak("Could not find a field called %" SVf " in any class",
2084 SVfARG(want_fieldname));
2085 13 done:
2086 ;
2087 }
2088 OUTPUT:
2089 RETVAL
2090  
2091 BOOT:
2092 61 XopENTRY_set(&xop_methstart, xop_name, "methstart");
2093 61 XopENTRY_set(&xop_methstart, xop_desc, "enter method");
2094 61 XopENTRY_set(&xop_methstart, xop_class, OA_UNOP_AUX);
2095 61 Perl_custom_op_register(aTHX_ &pp_methstart, &xop_methstart);
2096  
2097 61 XopENTRY_set(&xop_rolemethstart, xop_name, "rolemethstart");
2098 61 XopENTRY_set(&xop_rolemethstart, xop_desc, "enter role method");
2099 61 XopENTRY_set(&xop_rolemethstart, xop_class, OA_UNOP_AUX);
2100 61 Perl_custom_op_register(aTHX_ &pp_rolemethstart, &xop_rolemethstart);
2101  
2102 61 XopENTRY_set(&xop_commonmethstart, xop_name, "commonmethstart");
2103 61 XopENTRY_set(&xop_commonmethstart, xop_desc, "enter method :common");
2104 61 XopENTRY_set(&xop_commonmethstart, xop_class, OA_BASEOP);
2105 61 Perl_custom_op_register(aTHX_ &pp_commonmethstart, &xop_commonmethstart);
2106  
2107 61 CvLVALUE_on(get_cv("Object::Pad::MOP::Field::value", 0));
2108 #ifdef HAVE_DMD_HELPER
2109 DMD_SET_PACKAGE_HELPER("Object::Pad::MOP::Class", &dumppackage_class);
2110 #endif
2111  
2112 61 boot_xs_parse_keyword(0.48); /* XPK_FLAG_PERMIT_LEXICAL */
2113  
2114 61 register_xs_parse_keyword("class", &kwhooks_class, (void *)METATYPE_CLASS);
2115 61 register_xs_parse_keyword("role", &kwhooks_role, (void *)METATYPE_ROLE);
2116  
2117 61 register_xs_parse_keyword("inherit", &kwhooks_inherit, NULL);
2118 61 register_xs_parse_keyword("apply", &kwhooks_apply, NULL);
2119  
2120 61 register_xs_parse_keyword("field", &kwhooks_field, "field");
2121 61 register_xs_parse_keyword("has", &kwhooks_has, "has");
2122  
2123 61 register_xs_parse_keyword("BUILD", &kwhooks_BUILD, (void *)PHASER_BUILD);
2124 61 register_xs_parse_keyword("ADJUST", &kwhooks_ADJUST, (void *)PHASER_ADJUST);
2125 61 register_xs_parse_keyword("ADJUSTPARAMS", &kwhooks_ADJUST, (void *)PHASER_ADJUSTPARAMS);
2126 61 register_xs_parse_keyword("APPLY", &kwhooks_APPLY, (void *)PHASER_APPLY);
2127  
2128 61 register_xs_parse_keyword("__CLASS__", &kwhooks_uuCLASS, NULL);
2129  
2130 61 register_xs_parse_keyword("requires", &kwhooks_requires, NULL);
2131  
2132 61 boot_xs_parse_sublike(0.35); /* 'my' prefix scanning bugfix */
2133  
2134 61 register_xs_parse_sublike("method", &parse_method_hooks, (void *)PHASER_NONE);
2135  
2136 61 ObjectPad__boot_classes(aTHX);
2137 61 ObjectPad__boot_fields(aTHX);
« lib/Object/mop-field.xsi ↑ Summary lib/Object/mop-class.xsi »