src/parse_subsignature_ex.c

filter: Statementstmt 86.8% 483 / 556 Branchbran 71.0% 270 / 380 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.4% 753 / 936 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/XS/Parse/Sublike.xs ↑ Summary hax/croak_from_caller.c.inc »
1 /* vi: set ft=c : */
2  
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6  
7 #define HAVE_PERL_VERSION(R, V, S) \
8 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
9  
10 #include "XSParseSublike.h"
11  
12 /* Skip this entire file on perls older than OP_ARGCHECK */
13 #if HAVE_PERL_VERSION(5, 26, 0)
14  
15 #define PERL_EXT
16 /* We need to be able to see FEATURE_*_IS_ENABLED */
17 #include "feature.h"
18 /* Also need KEY_sigvar */
19 #include "keywords.h"
20  
21 #include "parse_subsignature_ex.h"
22  
23 #include "lexer-additions.c.inc"
24  
25 #include "croak_from_caller.c.inc"
26 #include "make_argcheck_aux.c.inc"
27 #include "newSV_with_free.c.inc"
28  
29 #ifdef XOPf_xop_dump
30 # define HAVE_XOP_DUMP
31 #endif
32  
33 #ifndef av_count
34 # define av_count(av) (1 + AvFILL(av))
35 #endif
36  
37 #define newSVpvx(ptr) S_newSVpvx(aTHX_ ptr)
38 33 static SV *S_newSVpvx(pTHX_ void *ptr)
39 {
40 33 SV *sv = newSV(0);
41 33 sv_upgrade(sv, SVt_PV);
42 33 SvPVX(sv) = ptr;
43 33 return sv;
44 }
45  
46 /*
47 * Need to grab some things that aren't quite core perl API
48 */
49  
50 /* yyerror() is a long function and hard to emulate or copy-paste for our
51 * purposes; we'll reïmplement a smaller version of it
52 */
53  
54 #define LEX_IGNORE_UTF8_HINTS 0x00000002
55  
56 #define PL_linestr (PL_parser->linestr)
57  
58 #ifdef USE_UTF8_SCRIPTS
59 # define UTF cBOOL(!IN_BYTES)
60 #else
61 # define UTF cBOOL((PL_linestr && DO_UTF8(PL_linestr)) || ( !(PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS) && (PL_hints & HINT_UTF8)))
62 #endif
63  
64 #define yyerror(s) S_yyerror(aTHX_ s)
65 0 void S_yyerror(pTHX_ const char *s)
66 {
67 0 SV *message = sv_2mortal(newSVpvs_flags("", 0));
68  
69 0 char *context = PL_parser->oldbufptr;
70 0 STRLEN contlen = PL_parser->bufptr - PL_parser->oldbufptr;
71  
72 0 ▶ sv_catpvf(message, "%s at %s line %" IVdf,
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(message, "%s at %s line %" IVdf, 0 0 0
73 s, OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
74  
75 0 ▶ if(context)
Branch: 1 branch
BranchTrueFalseTotal
if(context) 0 0 0
76 0 ▶ sv_catpvf(message, ", near \"%" UTF8f "\"",
Branch: 5 branches
BranchTrueFalseTotal
sv_catpvf(message, ", near \"%" UTF8f "\"", 0 0 0
sv_catpvf(message, ", near \"%" UTF8f "\"", 0 0 0
sv_catpvf(message, ", near \"%" UTF8f "\"", 0 0 0
sv_catpvf(message, ", near \"%" UTF8f "\"", 0 0 0
sv_catpvf(message, ", near \"%" UTF8f "\"", 0 0 0
77 UTF8fARG(UTF, contlen, context));
78  
79 0 sv_catpvf(message, "\n");
80  
81 0 PL_parser->error_count++;
82 0 warn_sv(message);
83 0 }
84  
85 /* Stolen from op.c */
86 #ifndef OpTYPE_set
87 # define OpTYPE_set(op, type) \
88 STMT_START { \
89 op->op_type = (OPCODE)type; \
90 op->op_ppaddr = PL_ppaddr[type]; \
91 } STMT_END
92 #endif
93  
94 #define alloc_LOGOP(a,b,c) S_alloc_LOGOP(aTHX_ a,b,c)
95 15 static LOGOP *S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
96 {
97 dVAR;
98 LOGOP *logop;
99 15 OP *kid = first;
100 15 NewOp(1101, logop, 1, LOGOP);
101 15 OpTYPE_set(logop, type);
102 15 logop->op_first = first;
103 15 logop->op_other = other;
104 15 ▶ if (first)
Branch: 1 branch
BranchTrueFalseTotal
if (first) 15 0 15
105 15 logop->op_flags = OPf_KIDS;
106 15 ▶ while (kid && OpHAS_SIBLING(kid))
Branch: 2 branches
BranchTrueFalseTotal
while (kid && OpHAS_SIBLING(kid)) 15 0 15
while (kid && OpHAS_SIBLING(kid)) 0 15 15
107 0 ▶ kid = OpSIBLING(kid);
Branch: 1 branch
BranchTrueFalseTotal
kid = OpSIBLING(kid); 0 0 0
108 15 ▶ if (kid)
Branch: 1 branch
BranchTrueFalseTotal
if (kid) 15 0 15
109 15 OpLASTSIB_set(kid, (OP*)logop);
110 15 return logop;
111 }
112  
113 /* copypaste from core's pp.c */
114 static SV *
115 6 S_find_runcv_name(pTHX)
116 {
117 CV *cv;
118 GV *gv;
119 SV *sv;
120  
121 6 cv = find_runcv(0);
122 6 ▶ if (!cv)
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) 0 6 6
123 0 return &PL_sv_no;
124  
125 6 gv = CvGV(cv);
126 6 ▶ if (!gv)
Branch: 1 branch
BranchTrueFalseTotal
if (!gv) 0 6 6
127 0 return &PL_sv_no;
128  
129 6 sv = sv_newmortal();
130 6 gv_fullname4(sv, gv, NULL, TRUE);
131 6 return sv;
132 }
133  
134 /*****************************
135 * Named arguments extension *
136 *****************************
137  
138 Signature handling of named arguments proceeds initially as with regular perl,
139 with the addition of one big op that handles all the named arguments at once.
140  
141 The generated optree will have additional steps after the OP_ARGCHECK +
142 OP_ARGELEM ops of positional parameters. Any CV with named parameters will
143 have a single OP_CUSTOM/pp_argelems_named that stands in place of any
144 OP_ARGELEM that would have been used for a final slurpy element, if present.
145 This stores details of all the named arguments in an array in its ->op_aux,
146 and processes all of the named arguments and the slurpy element all at once.
147 Following this will be a small optree per optional named parameter, consisting
148 of an OP_CUSTOM/pp_namedargexists, OP_CUSTOM/pp_namedargassign and the
149 defaulting expression.
150  
151 Temporarily during processing we make use of the SvPADSTALE flag on every pad
152 variable used to store a named parameter, to remember that no value has yet
153 been assigned into it. This is how we can detect required but missing named
154 parameters once argument processing is finished, and how the optional
155 parameters can have default expressions assigned into them.
156  
157 */
158  
159 enum {
160 OPp_NAMEDARGDEFELEM_IF_UNDEF = 1,
161 OPp_NAMEDARGDEFELEM_IF_FALSE = 2,
162 };
163  
164 static XOP xop_namedargexists;
165 16 static OP *pp_namedargexists(pTHX)
166 {
167 16 dSP;
168 16 dTARGET;
169  
170 16 bool ok = false;
171 16 switch(PL_op->op_private & 3) {
172 12 case 0:
173 12 ▶ ok = TARG && !SvPADSTALE(TARG);
Branch: 2 branches
BranchTrueFalseTotal
ok = TARG && !SvPADSTALE(TARG); 12 0 12
ok = TARG && !SvPADSTALE(TARG); 6 6 12
174 12 break;
175  
176 2 case OPp_NAMEDARGDEFELEM_IF_UNDEF:
177 2 ▶ ok = TARG && SvOK(TARG);
Branch: 2 branches
BranchTrueFalseTotal
ok = TARG && SvOK(TARG); 2 0 2
ok = TARG && SvOK(TARG); 1 1 2
178 2 break;
179  
180 2 case OPp_NAMEDARGDEFELEM_IF_FALSE:
181 2 ▶ ok = TARG && SvTRUE(TARG);
Branch: 2 branches
BranchTrueFalseTotal
ok = TARG && SvTRUE(TARG); 2 0 2
ok = TARG && SvTRUE(TARG); 1 1 2
182 2 break;
183 }
184  
185 16 ▶ if(!ok)
Branch: 1 branch
BranchTrueFalseTotal
if(!ok) 8 8 16
186 8 return cLOGOP->op_other;
187  
188 8 RETURN;
189 }
190  
191 #define check_refalias_arg(priv, sv) S_check_refalias_arg(aTHX_ priv, sv)
192 15 static bool S_check_refalias_arg(pTHX_ U8 priv, SV *sv)
193 {
194 15 ▶ if(!sv || !SvROK(sv))
Branch: 2 branches
BranchTrueFalseTotal
if(!sv || !SvROK(sv)) 15 0 15
if(!sv || !SvROK(sv)) 0 15 15
195 0 return false;
196  
197 15 SV *rv = SvRV(sv);
198  
199 15 switch(priv & OPpARGELEM_MASK) {
200 1 case OPpARGELEM_SV:
201 1 ▶ if(SvTYPE(rv) > SVt_PVMG)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) > SVt_PVMG) 0 1 1
202 0 return false;
203 1 break;
204  
205 11 case OPpARGELEM_AV:
206 11 ▶ if(SvTYPE(rv) != SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) != SVt_PVAV) 1 10 11
207 1 return false;
208 10 break;
209  
210 3 case OPpARGELEM_HV:
211 3 ▶ if(SvTYPE(rv) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) != SVt_PVHV) 1 2 3
212 1 return false;
213 2 break;
214 }
215  
216 13 return true;
217 }
218  
219 struct ArgElemsNamedParam {
220 U32 flags;
221 PADOFFSET padix;
222 U32 namehash;
223 Size_t namelen;
224 const char *namepv;
225 };
226 enum {
227 /* These flags are also stored in op_private of some ops so they have to
228 * fit in U8
229 */
230 NAMEDPARAMf_REQUIRED = (1<<0),
231 NAMEDPARAMf_UTF8 = (1<<1),
232 NAMEDPARAMf_REFALIAS = (1<<2),
233  
234 NAMEDPARAMf_REFSCALAR = (1<<3),
235 NAMEDPARAMf_REFARRAY = (2<<3),
236 NAMEDPARAMf_REFHASH = (3<<3),
237 };
238  
239 #define do_namedarg_assign(flags, padix, name, val) S_do_namedarg_assign(aTHX_ flags, padix, name, val)
240 42 static void S_do_namedarg_assign(pTHX_ U8 flags, PADOFFSET padix, SV *name, SV *val)
241 {
242 42 SV **padentry = &PAD_SVl(padix);
243  
244 /* This has to do all the work normally done by pp_argelem */
245 assert(TAINTING_get || !TAINT_get);
246 42 ▶ if(UNLIKELY(TAINT_get) && !SvTAINTED(val))
Branch: 3 branches
BranchTrueFalseTotal
if(UNLIKELY(TAINT_get) && !SvTAINTED(val)) 0 42 42
if(UNLIKELY(TAINT_get) && !SvTAINTED(val)) 0 0 0
if(UNLIKELY(TAINT_get) && !SvTAINTED(val)) 0 0 0
247 0 TAINT_NOT;
248  
249 42 ▶ if(flags & NAMEDPARAMf_REFALIAS) {
Branch: 1 branch
BranchTrueFalseTotal
if(flags & NAMEDPARAMf_REFALIAS) { 5 37 42
250 5 const char *exp_reftype = NULL;
251 5 U8 priv = 0;
252 5 switch(flags & NAMEDPARAMf_REFHASH) {
253 1 case NAMEDPARAMf_REFSCALAR: priv = OPpARGELEM_SV; exp_reftype = "SCALAR"; break;
254 3 case NAMEDPARAMf_REFARRAY: priv = OPpARGELEM_AV; exp_reftype = "ARRAY"; break;
255 1 case NAMEDPARAMf_REFHASH: priv = OPpARGELEM_HV; exp_reftype = "HASH"; break;
256 }
257 5 ▶ if(!check_refalias_arg(priv, val)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!check_refalias_arg(priv, val)) { 0 5 5
258 0 ▶ if(!name)
Branch: 1 branch
BranchTrueFalseTotal
if(!name) 0 0 0
259 // TODO: Look up the param name from the padix... somehow?
260 0 name = newSVpvs_flags("???", SVs_TEMP);
261 0 croak_from_caller("Expected named argument '%" SVf "' to %" SVf " to be a reference to %s",
262 SVfARG(name), SVfARG(S_find_runcv_name(aTHX)), exp_reftype);
263 }
264  
265 5 SvREFCNT_dec(*padentry);
266 5 *padentry = SvREFCNT_inc(SvRV(val));
267 }
268 else
269 37 ▶ SvSetMagicSV(*padentry, val);
Branch: 2 branches
BranchTrueFalseTotal
SvSetMagicSV(*padentry, val); 37 0 37
SvSetMagicSV(*padentry, val); 0 37 37
270  
271 42 SvPADSTALE_off(*padentry);
272 42 }
273  
274 static XOP xop_namedargassign;
275 8 static OP *pp_namedargassign(pTHX)
276 {
277 8 dSP;
278 8 SV *val = POPs;
279  
280 8 do_namedarg_assign(PL_op->op_private, PL_op->op_targ, NULL, val);
281  
282 8 RETURN;
283 }
284  
285 11 static int cmp_argelemsnamedparam(const void *_a, const void *_b)
286 {
287 11 const struct ArgElemsNamedParam *a = _a, *b = _b;
288 11 ▶ if(a->namehash < b->namehash)
Branch: 1 branch
BranchTrueFalseTotal
if(a->namehash < b->namehash) 5 6 11
289 5 return -1;
290 6 ▶ if(a->namehash > b->namehash)
Branch: 1 branch
BranchTrueFalseTotal
if(a->namehash > b->namehash) 6 0 6
291 6 return 1;
292 0 return 0;
293 }
294  
295 struct ArgElemsNamedAux {
296 UV start_argix;
297 Size_t n_params;
298 struct ArgElemsNamedParam params[0];
299 };
300  
301 static XOP xop_argelems_named;
302 32 static OP *pp_argelems_named(pTHX)
303 {
304 32 struct ArgElemsNamedAux *aux = (struct ArgElemsNamedAux *)cUNOP_AUX->op_aux;
305 32 AV *defav = GvAV(PL_defgv);
306  
307 32 HV *slurpy_hv = NULL;
308 32 AV *slurpy_av = NULL;
309 32 bool slurpy_ignore = false;
310  
311 32 ▶ if(PL_op->op_targ) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_op->op_targ) { 7 25 32
312 /* We have a slurpy of some kind */
313 7 save_clearsv(&PAD_SVl(PL_op->op_targ));
314 }
315  
316 32 ▶ if(PL_op->op_private & OPpARGELEM_HV) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_op->op_private & OPpARGELEM_HV) { 6 26 32
317 6 ▶ if(PL_op->op_targ) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_op->op_targ) { 5 1 6
318 5 slurpy_hv = (HV *)PAD_SVl(PL_op->op_targ);
319 assert(SvTYPE(slurpy_hv) == SVt_PVHV);
320 assert(HvKEYS(slurpy_hv) == 0);
321 }
322 else {
323 1 slurpy_ignore = true;
324 }
325 }
326 26 ▶ else if(PL_op->op_private & OPpARGELEM_AV) {
Branch: 1 branch
BranchTrueFalseTotal
else if(PL_op->op_private & OPpARGELEM_AV) { 3 23 26
327 3 ▶ if(PL_op->op_targ) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_op->op_targ) { 2 1 3
328 2 slurpy_av = (AV *)PAD_SVl(PL_op->op_targ);
329 assert(SvTYPE(slurpy_av) == SVt_PVAV);
330 assert(av_count(slurpy_av) == 0);
331 }
332 else {
333 1 slurpy_ignore = true;
334 }
335 }
336  
337 32 UV argix = aux->start_argix;
338 32 UV argc = av_count(defav);
339  
340 U32 parami;
341 32 UV n_params = aux->n_params;
342  
343 /* Before we process the incoming args we need to prepare *all* the param
344 * variable pad slots.
345 */
346 76 ▶ for(parami = 0; parami < n_params; parami++) {
Branch: 1 branch
BranchTrueFalseTotal
for(parami = 0; parami < n_params; parami++) { 44 32 76
347 44 struct ArgElemsNamedParam *param = &aux->params[parami];
348  
349 44 SV **padentry = &PAD_SVl(param->padix);
350 assert(padentry);
351 44 save_clearsv(padentry);
352  
353 /* A slight abuse of the PADSTALE flag so we can detect which parameters
354 * not been assigned to afterwards
355 */
356 44 SvPADSTALE_on(*padentry);
357 }
358  
359 32 SV *unrecognised_keynames = NULL;
360 32 UV n_unrecognised = 0;
361  
362 74 ▶ while(argix < argc) {
Branch: 1 branch
BranchTrueFalseTotal
while(argix < argc) { 42 32 74
363 /* TODO: do we need av_fetch or can we cheat around it? */
364 42 SV *name = *av_fetch(defav, argix, 0);
365 42 argix++;
366 42 ▶ SV *val = argix < argc ? *av_fetch(defav, argix, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *val = argix < argc ? *av_fetch(defav, argix, 0) : &PL_sv_undef; 41 1 42
367 42 argix++;
368  
369 STRLEN namelen;
370 42 const char *namepv = SvPV(name, namelen);
371  
372 U32 namehash;
373 42 ▶ PERL_HASH(namehash, namepv, namelen);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(namehash, namepv, namelen); 42 0 42
374  
375 /* In theory we would get better performance at runtime by binary
376 * searching for a good starting index. In practice only actually starts
377 * saving measurable time once we start to get to literally hundreds of
378 * named parameters. This simple linear search is actually very quick per
379 * rejected element.
380 * If your perl function wants to declare hundreds of different named
381 * parameters you probably want to rethink your strategy. ;)
382 */
383 42 struct ArgElemsNamedParam *param = NULL;
384 58 ▶ for(parami = 0; parami < n_params; parami++) {
Branch: 1 branch
BranchTrueFalseTotal
for(parami = 0; parami < n_params; parami++) { 56 2 58
385 56 struct ArgElemsNamedParam *p = &aux->params[parami];
386  
387 /* Since the params are stored in hash key order, if we are already
388 * past it then we know we are done
389 */
390 56 ▶ if(p->namehash > namehash)
Branch: 1 branch
BranchTrueFalseTotal
if(p->namehash > namehash) 6 50 56
391 6 break;
392 50 ▶ if(p->namehash != namehash)
Branch: 1 branch
BranchTrueFalseTotal
if(p->namehash != namehash) 16 34 50
393 16 continue;
394  
395 /* TODO: This will be wrong for UTF-8 comparisons */
396 34 ▶ if(namelen != p->namelen)
Branch: 1 branch
BranchTrueFalseTotal
if(namelen != p->namelen) 0 34 34
397 0 continue;
398 34 ▶ if(!strnEQ(namepv, p->namepv, namelen))
Branch: 1 branch
BranchTrueFalseTotal
if(!strnEQ(namepv, p->namepv, namelen)) 0 34 34
399 0 continue;
400  
401 34 param = p;
402 34 break;
403 }
404  
405 42 ▶ if(param) {
Branch: 1 branch
BranchTrueFalseTotal
if(param) { 34 8 42
406 34 do_namedarg_assign(param->flags, param->padix, name, val);
407 }
408 8 ▶ else if(slurpy_hv) {
Branch: 1 branch
BranchTrueFalseTotal
else if(slurpy_hv) { 1 7 8
409 1 hv_store_ent(slurpy_hv, name, newSVsv(val), 0);
410 }
411 7 ▶ else if(slurpy_av) {
Branch: 1 branch
BranchTrueFalseTotal
else if(slurpy_av) { 4 3 7
412 4 av_push(slurpy_av, newSVsv(name));
413 4 ▶ if(argix <= argc)
Branch: 1 branch
BranchTrueFalseTotal
if(argix <= argc) 3 1 4
414 3 av_push(slurpy_av, newSVsv(val));
415 }
416 3 ▶ else if(!slurpy_ignore) {
Branch: 1 branch
BranchTrueFalseTotal
else if(!slurpy_ignore) { 1 2 3
417 1 ▶ if(!unrecognised_keynames) {
Branch: 1 branch
BranchTrueFalseTotal
if(!unrecognised_keynames) { 1 0 1
418 1 unrecognised_keynames = newSVpvn("", 0);
419 1 SAVEFREESV(unrecognised_keynames);
420 }
421  
422 1 ▶ if(SvCUR(unrecognised_keynames))
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(unrecognised_keynames)) 0 1 1
423 0 sv_catpvs(unrecognised_keynames, ", ");
424 1 sv_catpvf(unrecognised_keynames, "'%" SVf "'", SVfARG(name));
425 1 n_unrecognised++;
426 }
427 }
428  
429 32 ▶ if(n_unrecognised) {
Branch: 1 branch
BranchTrueFalseTotal
if(n_unrecognised) { 1 31 32
430 1 ▶ croak_from_caller("Unrecognised %s %" SVf " for subroutine %" SVf,
Branch: 1 branch
BranchTrueFalseTotal
croak_from_caller("Unrecognised %s %" SVf " for subroutine %" SVf, 0 1 1
431 n_unrecognised > 1 ? "arguments" : "argument",
432 SVfARG(unrecognised_keynames), SVfARG(S_find_runcv_name(aTHX)));
433 }
434  
435 31 SV *missing_keynames = NULL;
436 31 UV n_missing = 0;
437  
438 74 ▶ for(parami = 0; parami < n_params; parami++) {
Branch: 1 branch
BranchTrueFalseTotal
for(parami = 0; parami < n_params; parami++) { 43 31 74
439 43 struct ArgElemsNamedParam *param = &aux->params[parami];
440 43 SV *targ = PAD_SVl(param->padix);
441  
442 43 ▶ if(!SvPADSTALE(targ))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPADSTALE(targ)) 33 10 43
443 33 continue;
444 10 ▶ if(!(param->flags & NAMEDPARAMf_REQUIRED))
Branch: 1 branch
BranchTrueFalseTotal
if(!(param->flags & NAMEDPARAMf_REQUIRED)) 6 4 10
445 6 continue;
446  
447 4 ▶ if(!missing_keynames) {
Branch: 1 branch
BranchTrueFalseTotal
if(!missing_keynames) { 3 1 4
448 3 missing_keynames = newSVpvn("", 0);
449 3 SAVEFREESV(missing_keynames);
450 }
451  
452 4 ▶ if(SvCUR(missing_keynames))
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(missing_keynames)) 1 3 4
453 1 sv_catpvs(missing_keynames, ", ");
454 4 sv_catpvf(missing_keynames, "'%s'", param->namepv);
455 4 n_missing++;
456 }
457  
458 31 ▶ if(n_missing) {
Branch: 1 branch
BranchTrueFalseTotal
if(n_missing) { 3 28 31
459 3 ▶ croak_from_caller("Missing %s %" SVf " for subroutine %" SVf,
Branch: 1 branch
BranchTrueFalseTotal
croak_from_caller("Missing %s %" SVf " for subroutine %" SVf, 1 2 3
460 n_missing > 1 ? "arguments" : "argument",
461 SVfARG(missing_keynames), SVfARG(S_find_runcv_name(aTHX)));
462 }
463  
464 28 return NORMAL;
465 }
466  
467 #ifdef HAVE_XOP_DUMP
468 0 static void opdump_argelems_named(pTHX_ const OP *o, struct Perl_OpDumpContext *ctx)
469 {
470 0 struct ArgElemsNamedAux *aux = (struct ArgElemsNamedAux *)cUNOP_AUXo->op_aux;
471  
472 0 opdump_printf(ctx, "START_ARGIX = %" UVuf "\n", aux->start_argix);
473 0 opdump_printf(ctx, "PARAMS = (%" UVuf ")\n", aux->n_params);
474  
475 U32 parami;
476 0 ▶ for(parami = 0; parami < aux->n_params; parami++) {
Branch: 1 branch
BranchTrueFalseTotal
for(parami = 0; parami < aux->n_params; parami++) { 0 0 0
477 0 struct ArgElemsNamedParam *param = &aux->params[parami];
478  
479 0 opdump_printf(ctx, " [%d] = {.name=\"%s\", .namehash=%u .padix=%u, .flags=(",
480 parami,
481 param->namepv,
482 param->namehash,
483 (unsigned int)param->padix);
484  
485 0 bool need_comma = false;
486 0 ▶ if(param->flags & NAMEDPARAMf_UTF8)
Branch: 1 branch
BranchTrueFalseTotal
if(param->flags & NAMEDPARAMf_UTF8) 0 0 0
487 0 ▶ opdump_printf(ctx, "%sUTF8", need_comma?",":""), need_comma = true;
Branch: 1 branch
BranchTrueFalseTotal
opdump_printf(ctx, "%sUTF8", need_comma?",":""), need_comma = true; 0 0 0
488 0 ▶ if(param->flags & NAMEDPARAMf_REQUIRED)
Branch: 1 branch
BranchTrueFalseTotal
if(param->flags & NAMEDPARAMf_REQUIRED) 0 0 0
489 0 ▶ opdump_printf(ctx, "%sREQUIRED", need_comma?",":""), need_comma = true;
Branch: 1 branch
BranchTrueFalseTotal
opdump_printf(ctx, "%sREQUIRED", need_comma?",":""), need_comma = true; 0 0 0
490 0 ▶ if(param->flags & NAMEDPARAMf_REFALIAS)
Branch: 1 branch
BranchTrueFalseTotal
if(param->flags & NAMEDPARAMf_REFALIAS) 0 0 0
491 0 ▶ opdump_printf(ctx, "%sREFALIAS", need_comma?",":""), need_comma = true;
Branch: 1 branch
BranchTrueFalseTotal
opdump_printf(ctx, "%sREFALIAS", need_comma?",":""), need_comma = true; 0 0 0
492  
493 0 opdump_printf(ctx, ")}\n");
494 }
495 0 }
496 #endif
497  
498 static XOP xop_refargelem;
499 10 static OP *pp_refargelem(pTHX)
500 {
501 10 dSP;
502 10 U8 priv = PL_op->op_private;
503 10 IV argix = PTR2IV(cUNOP_AUX->op_aux);
504  
505 SV *sv;
506 10 ▶ if(PL_op->op_flags & OPf_STACKED)
Branch: 1 branch
BranchTrueFalseTotal
if(PL_op->op_flags & OPf_STACKED) 3 7 10
507 3 sv = POPs;
508 else {
509 7 SV **svp = av_fetch(GvAV(PL_defgv), argix, FALSE);
510 7 ▶ sv = svp ? *svp : NULL;
Branch: 1 branch
BranchTrueFalseTotal
sv = svp ? *svp : NULL; 7 0 7
511 }
512  
513 10 PUTBACK;
514  
515 10 ▶ if(!check_refalias_arg(priv, sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!check_refalias_arg(priv, sv)) { 2 8 10
516 2 const char *exp_reftype = NULL;
517 2 switch(priv & OPpARGELEM_MASK) {
518 0 case OPpARGELEM_SV: exp_reftype = "SCALAR"; break;
519 1 case OPpARGELEM_AV: exp_reftype = "ARRAY"; break;
520 1 case OPpARGELEM_HV: exp_reftype = "HASH"; break;
521 }
522 2 croak_from_caller("Expected argument %" IVdf " to %" SVf " to be a reference to %s",
523 argix + 1, SVfARG(S_find_runcv_name(aTHX)), exp_reftype);
524 }
525  
526 /* Perform refaliasing into the pad */
527 8 SV **padentry = &(PAD_SVl(PL_op->op_targ));
528 8 save_clearsv(padentry);
529 8 SvREFCNT_dec(*padentry);
530 8 *padentry = SvREFCNT_inc(SvRV(sv));
531  
532 8 return PL_op->op_next;
533 }
534  
535 /* Parameter attribute extensions */
536 typedef struct SignatureAttributeRegistration SignatureAttributeRegistration;
537  
538 struct SignatureAttributeRegistration {
539 SignatureAttributeRegistration *next;
540  
541 const char *name;
542 STRLEN permit_hintkeylen;
543  
544 const struct XPSSignatureAttributeFuncs *funcs;
545 void *funcdata;
546 };
547  
548 static SignatureAttributeRegistration *sigattrs = NULL;
549  
550 #define find_registered_attribute(name) S_find_registered_attribute(aTHX_ name)
551 8 static SignatureAttributeRegistration *S_find_registered_attribute(pTHX_ const char *name)
552 {
553 8 HV *hints = GvHV(PL_hintgv);
554  
555 SignatureAttributeRegistration *reg;
556 8 ▶ for(reg = sigattrs; reg; reg = reg->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(reg = sigattrs; reg; reg = reg->next) { 8 0 8
557 8 ▶ if(!strEQ(name, reg->name))
Branch: 1 branch
BranchTrueFalseTotal
if(!strEQ(name, reg->name)) 0 8 8
558 0 continue;
559  
560 8 ▶ if(reg->funcs->permit_hintkey &&
Branch: 2 branches
BranchTrueFalseTotal
if(reg->funcs->permit_hintkey && 8 0 8
if(reg->funcs->permit_hintkey && 8 0 8
561 8 ▶ (!hints || !hv_fetch(hints, reg->funcs->permit_hintkey, reg->permit_hintkeylen, 0)))
Branch: 1 branch
BranchTrueFalseTotal
(!hints || !hv_fetch(hints, reg->funcs->permit_hintkey, reg->permit_hintkeylen, 0))) 0 8 8
562 0 continue;
563  
564 8 return reg;
565 }
566  
567 0 croak("Unrecognised signature parameter attribute :%s", name);
568 }
569  
570 struct PendingSignatureFunc {
571 const struct XPSSignatureAttributeFuncs *funcs;
572 void *funcdata;
573 void *attrdata;
574 };
575  
576 #define PENDING_FROM_SV(sv) ((struct PendingSignatureFunc *)SvPVX(sv))
577  
578 8 static void pending_free(pTHX_ SV *sv)
579 {
580 8 struct PendingSignatureFunc *p = PENDING_FROM_SV(sv);
581  
582 8 ▶ if(p->funcs->free)
Branch: 1 branch
BranchTrueFalseTotal
if(p->funcs->free) 0 8 8
583 0 (*p->funcs->free)(aTHX_ p->attrdata, p->funcdata);
584 8 }
585  
586 #define NEW_SV_PENDING() newSV_with_free(sizeof(struct PendingSignatureFunc), &pending_free)
587  
588 struct NamedParamDetails {
589 PADOFFSET padix;
590 U8 flags;
591 char sigil;
592 };
593 struct SignatureParsingContext {
594 OP *positional_elems; /* OP_LINESEQ of every positional element, in order */
595 OP *named_elem_defops; /* OP_LINESEQ of those named elements that have defaulting expressions */
596 HV *named_details; /* SV ptrs to NamedParamDetails of every named parameter */
597 OP *slurpy_elem;
598  
599 /* Counters that replace what PL_parser->sig_* used to be */
600 IV n_elems;
601 IV n_optelems;
602 char slurpy_sigil;
603 };
604  
605 42 static void free_parsing_ctx(pTHX_ void *_sigctx)
606 {
607 42 struct SignatureParsingContext *sigctx = _sigctx;
608 /* TODO the rest */
609 42 ▶ if(sigctx->named_details)
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->named_details) 23 19 42
610 23 SvREFCNT_dec((SV *)sigctx->named_details);
611 42 }
612  
613 #define sigctx_add_param(sigctx, paramctx) S_sigctx_add_param(aTHX_ sigctx, paramctx)
614 70 static void S_sigctx_add_param(pTHX_ struct SignatureParsingContext *sigctx, struct XPSSignatureParamContext *paramctx)
615 {
616 70 ▶ if(paramctx->is_named) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->is_named) { 33 37 70
617 /* A named scalar */
618  
619 33 ▶ if(paramctx->namelen) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->namelen) { 33 0 33
620 33 ▶ if(!sigctx->named_details)
Branch: 1 branch
BranchTrueFalseTotal
if(!sigctx->named_details) 23 10 33
621 23 sigctx->named_details = newHV();
622  
623 struct NamedParamDetails *details;
624 33 Newx(details, 1, struct NamedParamDetails);
625 33 *details = (struct NamedParamDetails){
626 33 .padix = paramctx->padix,
627 33 .flags = (!paramctx->defop) ? NAMEDPARAMf_REQUIRED : 0,
628 33 .sigil = paramctx->sigil,
629 };
630  
631 33 ▶ if(paramctx->is_refalias) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->is_refalias) { 4 29 33
632 4 details->flags |= NAMEDPARAMf_REFALIAS;
633 4 switch(paramctx->sigil) {
634 1 case '$': details->flags |= NAMEDPARAMf_REFSCALAR; break;
635 2 case '@': details->flags |= NAMEDPARAMf_REFARRAY; break;
636 1 case '%': details->flags |= NAMEDPARAMf_REFHASH; break;
637 }
638 }
639  
640 33 hv_store(sigctx->named_details, paramctx->namepv, paramctx->namelen, newSVpvx(details), 0);
641 }
642  
643 33 sigctx->named_elem_defops = op_append_elem(OP_LINESEQ, sigctx->named_elem_defops,
644 paramctx->op);
645  
646 /* Introduce the named parameter variable so later expressions can see it.
647 * This is done implicitly by newSTATEOP() for positional ones, but we
648 * must do it manually here.
649 */
650 33 intro_my();
651 }
652 37 ▶ else if(paramctx->is_refalias) {
Branch: 1 branch
BranchTrueFalseTotal
else if(paramctx->is_refalias) { 5 32 37
653 /* A positional reference alias. */
654  
655 /* Acts as a positional for argument consuming purposes */
656 5 ▶ if(paramctx->op)
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->op) 5 0 5
657 5 sigctx->positional_elems = op_append_list(OP_LINESEQ, sigctx->positional_elems,
658 newSTATEOP(0, NULL, paramctx->op));
659  
660 5 sigctx->n_elems++;
661 5 ▶ if(paramctx->defop)
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->defop) 2 3 5
662 2 sigctx->n_optelems++;
663 }
664 32 ▶ else if(paramctx->sigil == '$') {
Branch: 1 branch
BranchTrueFalseTotal
else if(paramctx->sigil == '$') { 26 6 32
665 /* A positional scalar */
666  
667 /* This call to newSTATEOP() must come AFTER parsing the defaulting
668 * expression because it involves an implicit intro_my() and so we must
669 * not introduce the new parameter variable beforehand (RT155630)
670 */
671 26 ▶ if(paramctx->op)
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->op) 23 3 26
672 23 sigctx->positional_elems = op_append_list(OP_LINESEQ, sigctx->positional_elems,
673 newSTATEOP(0, NULL, paramctx->op));
674  
675 26 sigctx->n_elems++;
676 26 ▶ if(paramctx->defop)
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->defop) 4 22 26
677 4 sigctx->n_optelems++;
678 }
679 else {
680 /* The final slurpy */
681 assert(paramctx->sigil == '@' || paramctx->sigil == '%');
682  
683 6 ▶ if(paramctx->varop)
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx->varop) 4 2 6
684 4 sigctx->slurpy_elem = newSTATEOP(0, NULL, paramctx->varop);
685  
686 6 sigctx->slurpy_sigil = paramctx->sigil;
687 }
688 70 }
689  
690 2 void XPS_signature_add_param(pTHX_ struct XSParseSublikeContext *ctx, struct XPSSignatureParamDetails *details)
691 {
692 /* We know that ctx is really a struct XPSContextWithPointer */
693 2 struct SignatureParsingContext *sigctx = ((struct XPSContextWithPointer *)ctx)->sigctx;
694  
695 /* Was added in version 7 and so far remains unchanged */
696 2 ▶ if(details->ver < 7 || details->ver > XSPARSESUBLIKE_ABI_VERSION)
Branch: 2 branches
BranchTrueFalseTotal
if(details->ver < 7 || details->ver > XSPARSESUBLIKE_ABI_VERSION) 2 0 2
if(details->ver < 7 || details->ver > XSPARSESUBLIKE_ABI_VERSION) 0 2 2
697 0 croak("ABI version mismatch in .ver of XPSSignatureParamDetails structure passed to xps_signature_add_param()");
698  
699 2 struct XPSSignatureParamContext paramctx = {
700 .is_named = false,
701 .is_refalias = false,
702 2 .sigil = details->sigil,
703 2 .padix = details->padix,
704 .varop = NULL, /* wil be set below */
705 .defop = NULL,
706 };
707  
708 2 char padname_sigil = PadnamePV(PadnamelistARRAY(PL_comppad_name)[details->padix])[0];
709 assert(padname_sigil == details->sigil);
710 PERL_UNUSED_VAR(padname_sigil);
711  
712 2 paramctx.varop = newUNOP_AUX(OP_ARGELEM, 0, NULL, INT2PTR(UNOP_AUX_item *, (sigctx->n_elems)));
713 2 switch(details->sigil) {
714 1 case '$': paramctx.varop->op_private |= OPpARGELEM_SV; break;
715 1 case '@': paramctx.varop->op_private |= OPpARGELEM_AV; break;
716 0 case '%': paramctx.varop->op_private |= OPpARGELEM_HV; break;
717 }
718 2 paramctx.varop->op_targ = details->padix;
719  
720 2 ▶ if(details->sigil == '$')
Branch: 1 branch
BranchTrueFalseTotal
if(details->sigil == '$') 1 1 2
721 1 paramctx.op = paramctx.varop;
722  
723 2 sigctx_add_param(sigctx, &paramctx);
724 2 }
725  
726 2 IV XPS_signature_query(pTHX_ struct XSParseSublikeContext *ctx, int q)
727 {
728 /* We know that ctx is really a struct XPSContextWithPointer */
729 2 struct SignatureParsingContext *sigctx = ((struct XPSContextWithPointer *)ctx)->sigctx;
730  
731 2 switch(q) {
732 2 case 0: return sigctx->n_elems;
733 0 case 1: return sigctx->n_optelems;
734 0 case 2: return sigctx->slurpy_sigil;
735 0 default:
736 0 croak("ARGH unreachable");
737 }
738 }
739  
740 #define parse_sigelem(sigctx, flags) S_parse_sigelem(aTHX_ sigctx, flags)
741 69 static void S_parse_sigelem(pTHX_ struct SignatureParsingContext *sigctx, U32 flags)
742 {
743 69 bool permit_attributes = flags & PARSE_SUBSIGNATURE_PARAM_ATTRIBUTES;
744  
745 69 yy_parser *parser = PL_parser;
746  
747 69 int c = lex_peek_unichar(0);
748 int private;
749 69 struct XPSSignatureParamContext paramctx = { 0 };
750  
751 69 AV *pending = NULL;
752  
753 69 ▶ if((flags & PARSE_SUBSIGNATURE_NAMED_PARAMS) && c == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if((flags & PARSE_SUBSIGNATURE_NAMED_PARAMS) && c == ':') { 56 13 69
if((flags & PARSE_SUBSIGNATURE_NAMED_PARAMS) && c == ':') { 33 23 56
754 33 lex_read_unichar(0);
755 33 lex_read_space(0);
756  
757 33 paramctx.is_named = true;
758 33 c = lex_peek_unichar(0);
759 }
760  
761 69 ▶ if((flags & PARSE_SUBSIGNATURE_REFALIAS) && c == '\\') {
Branch: 2 branches
BranchTrueFalseTotal
if((flags & PARSE_SUBSIGNATURE_REFALIAS) && c == '\\') { 22 47 69
if((flags & PARSE_SUBSIGNATURE_REFALIAS) && c == '\\') { 9 13 22
762 9 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__REFALIASING),
763 "refaliases are experimental");
764  
765 9 lex_read_unichar(0);
766 9 lex_read_space(0);
767  
768 9 paramctx.is_refalias = true;
769 9 c = lex_peek_unichar(0);
770 }
771  
772 /* Be slightly helpful to folks who write \:$foo */
773 69 ▶ if((flags & PARSE_SUBSIGNATURE_NAMED_PARAMS) && c == ':')
Branch: 2 branches
BranchTrueFalseTotal
if((flags & PARSE_SUBSIGNATURE_NAMED_PARAMS) && c == ':') 56 13 69
if((flags & PARSE_SUBSIGNATURE_NAMED_PARAMS) && c == ':') 0 56 56
774 0 croak("Named refalias parameters should be written :\\VAR, not \\:VAR");
775  
776 69 paramctx.sigil = c;
777 69 switch(paramctx.sigil) {
778 56 case '$': private = OPpARGELEM_SV; break;
779 8 case '@': private = OPpARGELEM_AV; break;
780 5 case '%': private = OPpARGELEM_HV; break;
781 0 case ':':
782 0 croak("Named signature elements are not permitted");
783 0 default:
784 0 croak("Expected a signature element at <%s>\n", parser->bufptr);
785 }
786  
787 69 char *lexname = parser->bufptr;
788  
789 /* Consume sigil */
790 69 lex_read_unichar(0);
791  
792 69 STRLEN lexname_len = 0;
793  
794 69 ▶ if(isIDFIRST_uni(lex_peek_unichar(0))) {
Branch: 4 branches
BranchTrueFalseTotal
if(isIDFIRST_uni(lex_peek_unichar(0))) { 69 0 69
if(isIDFIRST_uni(lex_peek_unichar(0))) { 69 0 69
if(isIDFIRST_uni(lex_peek_unichar(0))) { 63 6 69
if(isIDFIRST_uni(lex_peek_unichar(0))) { 63 6 69
795 63 lex_read_unichar(0);
796 119 ▶ while(isALNUM_uni(lex_peek_unichar(0)))
Branch: 4 branches
BranchTrueFalseTotal
while(isALNUM_uni(lex_peek_unichar(0))) 119 0 119
while(isALNUM_uni(lex_peek_unichar(0))) 119 0 119
while(isALNUM_uni(lex_peek_unichar(0))) 56 63 119
while(isALNUM_uni(lex_peek_unichar(0))) 56 63 119
797 56 lex_read_unichar(0);
798  
799 63 ENTER;
800 63 SAVEI16(PL_parser->in_my);
801 63 PL_parser->in_my = KEY_sigvar;
802  
803 63 lexname_len = PL_parser->bufptr - lexname;
804 63 paramctx.padix = pad_add_name_pvn(lexname, lexname_len, 0, NULL, NULL);
805  
806 63 ▶ if(paramctx.is_named) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx.is_named) { 33 30 63
807 33 paramctx.namepv = lexname + 1;
808 33 paramctx.namelen = lexname_len - 1;
809  
810 /* named params don't get an individual varop */
811 }
812 else {
813 30 ▶ if(paramctx.is_refalias) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx.is_refalias) { 5 25 30
814 5 paramctx.varop = newUNOP_AUX(OP_CUSTOM, 0, NULL, INT2PTR(UNOP_AUX_item *, (sigctx->n_elems)));
815 5 paramctx.varop->op_ppaddr = &pp_refargelem;
816 }
817 else
818 25 paramctx.varop = newUNOP_AUX(OP_ARGELEM, 0, NULL, INT2PTR(UNOP_AUX_item *, (sigctx->n_elems)));
819 30 paramctx.varop->op_private |= private;
820 30 paramctx.varop->op_targ = paramctx.padix;
821 }
822  
823 63 LEAVE;
824 }
825  
826 69 lex_read_space(0);
827  
828 69 ▶ if(lex_peek_unichar(0) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ':') { 8 61 69
829 8 ▶ if(!permit_attributes)
Branch: 1 branch
BranchTrueFalseTotal
if(!permit_attributes) 0 8 8
830 0 croak("Attributes on signature parameters are not permitted");
831  
832 8 lex_read_unichar(0);
833 8 lex_read_space(0);
834  
835 8 SV *attrname = sv_newmortal(), *attrval = sv_newmortal();
836  
837 16 ▶ while(lex_scan_attrval_into(attrname, attrval)) {
Branch: 1 branch
BranchTrueFalseTotal
while(lex_scan_attrval_into(attrname, attrval)) { 8 8 16
838 8 lex_read_space(0);
839  
840 8 SignatureAttributeRegistration *reg = find_registered_attribute(SvPV_nolen(attrname));
841  
842 8 void *attrdata = NULL;
843 8 ▶ if(reg->funcs->apply)
Branch: 1 branch
BranchTrueFalseTotal
if(reg->funcs->apply) 8 0 8
844 8 (*reg->funcs->apply)(aTHX_ &paramctx, attrval, &attrdata, reg->funcdata);
845  
846 8 ▶ if(attrdata || reg->funcs->post_defop) {
Branch: 2 branches
BranchTrueFalseTotal
if(attrdata || reg->funcs->post_defop) { 8 0 8
if(attrdata || reg->funcs->post_defop) { 8 0 8
847 8 ▶ if(!pending) {
Branch: 1 branch
BranchTrueFalseTotal
if(!pending) { 8 0 8
848 8 pending = newAV();
849 8 SAVEFREESV(pending);
850 }
851  
852 SV *psv;
853 8 av_push(pending, psv = NEW_SV_PENDING());
854  
855 8 PENDING_FROM_SV(psv)->funcs = reg->funcs;
856 8 PENDING_FROM_SV(psv)->funcdata = reg->funcdata;
857 8 PENDING_FROM_SV(psv)->attrdata = attrdata;
858 }
859  
860 8 ▶ if(lex_peek_unichar(0) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ':') { 0 8 8
861 0 lex_read_unichar(0);
862 0 lex_read_space(0);
863 }
864 }
865 }
866  
867 132 ▶ if(paramctx.sigil == '$' || paramctx.is_refalias) {
Branch: 2 branches
BranchTrueFalseTotal
if(paramctx.sigil == '$' || paramctx.is_refalias) { 13 56 69
if(paramctx.sigil == '$' || paramctx.is_refalias) { 8 5 13
868 64 ▶ if(paramctx.is_named) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx.is_named) { 31 33 64
869 }
870 else {
871 31 ▶ if(sigctx->slurpy_sigil)
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->slurpy_sigil) 0 31 31
872 0 yyerror("Slurpy parameters not last");
873 }
874  
875 64 bool default_if_undef = false, default_if_false = false;
876 64 ▶ if(lex_consume("=") ||
Branch: 1 branch
BranchTrueFalseTotal
if(lex_consume("=") || 49 15 64
877 49 ▶ (default_if_undef = lex_consume("//=")) ||
Branch: 1 branch
BranchTrueFalseTotal
(default_if_undef = lex_consume("//=")) || 47 2 49
878 65 ▶ (default_if_false = lex_consume("||="))) {
Branch: 1 branch
BranchTrueFalseTotal
(default_if_false = lex_consume("||="))) { 2 45 47
879 19 OP *defexpr = parse_termexpr(PARSE_OPTIONAL);
880 19 ▶ if(PL_parser->error_count)
Branch: 1 branch
BranchTrueFalseTotal
if(PL_parser->error_count) 0 19 19
881 0 croak("Expected a defaulting expression for optional parameter");
882 19 ▶ if(!paramctx.is_named && !paramctx.varop) {
Branch: 2 branches
BranchTrueFalseTotal
if(!paramctx.is_named && !paramctx.varop) { 10 9 19
if(!paramctx.is_named && !paramctx.varop) { 4 6 10
883 /* We permit `= undef` and the blank `=` but nothing else */
884 4 ▶ if(defexpr && defexpr->op_type != OP_UNDEF)
Branch: 2 branches
BranchTrueFalseTotal
if(defexpr && defexpr->op_type != OP_UNDEF) 2 2 4
if(defexpr && defexpr->op_type != OP_UNDEF) 1 1 2
885 1 croak("Unnamed positional parameters cannot have defaulting expressions");
886 }
887  
888 18 ▶ if(paramctx.is_named) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx.is_named) { 9 9 18
889 9 OP *assignop = newUNOP(OP_CUSTOM, 0, defexpr);
890 9 assignop->op_ppaddr = &pp_namedargassign;
891 9 assignop->op_targ = paramctx.padix;
892 9 ▶ if(paramctx.is_refalias) {
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx.is_refalias) { 1 8 9
893 1 assignop->op_private |= NAMEDPARAMf_REFALIAS;
894 1 switch(paramctx.sigil) {
895 0 case '$': assignop->op_private |= NAMEDPARAMf_REFSCALAR; break;
896 1 case '@': assignop->op_private |= NAMEDPARAMf_REFARRAY; break;
897 0 case '%': assignop->op_private |= NAMEDPARAMf_REFHASH; break;
898 }
899 }
900  
901 9 ▶ OP *existsop = (OP *)alloc_LOGOP(OP_CUSTOM, assignop, LINKLIST(assignop));
Branch: 1 branch
BranchTrueFalseTotal
OP *existsop = (OP *)alloc_LOGOP(OP_CUSTOM, assignop, LINKLIST(assignop)); 0 9 9
902 9 existsop->op_ppaddr = &pp_namedargexists;
903 9 existsop->op_targ = paramctx.padix;
904 9 ▶ existsop->op_private =
Branch: 2 branches
BranchTrueFalseTotal
existsop->op_private = 1 8 9
existsop->op_private = 1 7 8
905 default_if_undef ? OPp_NAMEDARGDEFELEM_IF_UNDEF :
906 default_if_false ? OPp_NAMEDARGDEFELEM_IF_FALSE :
907 0;
908  
909 9 OP *defop = newUNOP(OP_NULL, 0, existsop);
910  
911 9 ▶ LINKLIST(defop);
Branch: 1 branch
BranchTrueFalseTotal
LINKLIST(defop); 0 9 9
912  
913 9 defop->op_next = existsop; /* start of this fragment */
914 9 assignop->op_next = defop; /* after assign, stop this fragment */
915  
916 9 paramctx.op = defop;
917 9 paramctx.defop = defop;
918 }
919 9 ▶ else if(paramctx.varop) {
Branch: 1 branch
BranchTrueFalseTotal
else if(paramctx.varop) { 6 3 9
920 6 U8 private = 0;
921 #ifdef OPpARG_IF_UNDEF
922 6 ▶ if(default_if_undef) private |= OPpARG_IF_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if(default_if_undef) private |= OPpARG_IF_UNDEF; 1 5 6
923 6 ▶ if(default_if_false) private |= OPpARG_IF_FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if(default_if_false) private |= OPpARG_IF_FALSE; 1 5 6
924 #else
925 if(default_if_undef || default_if_false)
926 /* TODO: This would be possible with a custom op but we'd basically
927 * have to copy the behaviour of pp_argdefelem in that case
928 */
929 yyerror("This Perl version cannot handle if_undef/if_false defaulting expressions on positional parameters");
930 #endif
931  
932 6 ▶ if(!defexpr)
Branch: 1 branch
BranchTrueFalseTotal
if(!defexpr) 1 5 6
933 1 defexpr = newOP(OP_UNDEF, OPf_WANT_SCALAR);
934  
935 6 ▶ OP *defop = (OP *)alloc_LOGOP(OP_ARGDEFELEM, defexpr, LINKLIST(defexpr));
Branch: 1 branch
BranchTrueFalseTotal
OP *defop = (OP *)alloc_LOGOP(OP_ARGDEFELEM, defexpr, LINKLIST(defexpr)); 4 2 6
936 6 defop->op_targ = (PADOFFSET)sigctx->n_elems;
937 6 defop->op_private = private;
938  
939 6 paramctx.varop->op_flags |= OPf_STACKED;
940 6 op_sibling_splice(paramctx.varop, NULL, 0, defop);
941 6 defop = op_contextualize(defop, G_SCALAR);
942  
943 6 ▶ LINKLIST(paramctx.varop);
Branch: 1 branch
BranchTrueFalseTotal
LINKLIST(paramctx.varop); 0 6 6
944  
945 6 paramctx.varop->op_next = defop;
946 6 defexpr->op_next = paramctx.varop;
947  
948 6 paramctx.op = paramctx.varop;
949 6 paramctx.defop = defop;
950 }
951 /* else this is `= undef` on anonymous param; nothing to do */
952 }
953 else {
954 45 ▶ if(sigctx->n_optelems)
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->n_optelems) 0 45 45
955 0 yyerror("Mandatory parameter follows optional parameter");
956  
957 45 ▶ if(!paramctx.is_named)
Branch: 1 branch
BranchTrueFalseTotal
if(!paramctx.is_named) 21 24 45
958 21 paramctx.op = paramctx.varop;
959 }
960 }
961 else {
962 5 ▶ if(paramctx.is_named)
Branch: 1 branch
BranchTrueFalseTotal
if(paramctx.is_named) 0 5 5
963 0 yyerror("Slurpy parameters may not be named");
964 5 ▶ if(sigctx->slurpy_sigil)
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->slurpy_sigil) 0 5 5
965 0 yyerror("Multiple slurpy parameters not allowed");
966  
967 5 ▶ if(lex_peek_unichar(0) == '=')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == '=') 0 5 5
968 0 yyerror("A slurpy parameter may not have a default value");
969 }
970  
971 68 ▶ if(pending) {
Branch: 1 branch
BranchTrueFalseTotal
if(pending) { 8 60 68
972 16 ▶ for(int i = 0; i <= AvFILL(pending); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 0; i <= AvFILL(pending); i++) { 8 8 16
973 8 struct PendingSignatureFunc *p = PENDING_FROM_SV(AvARRAY(pending)[i]);
974  
975 8 ▶ if(p->funcs->post_defop)
Branch: 1 branch
BranchTrueFalseTotal
if(p->funcs->post_defop) 8 0 8
976 8 (*p->funcs->post_defop)(aTHX_ &paramctx, p->attrdata, p->funcdata);
977 }
978 }
979  
980 /* Only after we've run the post_defop hooks can we actually consume the
981 * result in paramctx.op
982 */
983 68 sigctx_add_param(sigctx, &paramctx);
984 68 }
985  
986 42 OP *XPS_parse_subsignature_ex(pTHX_ int flags,
987 struct XPSContextWithPointer *ctx,
988 struct HooksAndData hooksanddata[],
989 size_t nhooks)
990 {
991 /* Mostly reconstructed logic from perl 5.28.0's toke.c and perly.y
992 */
993 42 yy_parser *parser = PL_parser;
994 42 struct SignatureParsingContext sigctx_ = { 0 };
995 42 struct SignatureParsingContext *const sigctx = &sigctx_;
996  
997 42 ▶ if(ctx)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx) 42 0 42
998 42 ctx->sigctx = sigctx;
999  
1000 assert((flags & ~(PARSE_SUBSIGNATURE_NAMED_PARAMS|PARSE_SUBSIGNATURE_PARAM_ATTRIBUTES|PARSE_SUBSIGNATURE_REFALIAS)) == 0);
1001  
1002 42 ENTER;
1003 42 SAVEDESTRUCTOR_X(&free_parsing_ctx, sigctx);
1004  
1005 IV hooki;
1006 const struct XSParseSublikeHooks *hooks;
1007 void *hookdata;
1008  
1009 88 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 46 42 88
FOREACH_HOOKS_FORWARD { 0 46 46
FOREACH_HOOKS_FORWARD { 46 42 88
1010 46 ▶ if(hooks->ver >= 7 && hooks->start_signature)
Branch: 2 branches
BranchTrueFalseTotal
if(hooks->ver >= 7 && hooks->start_signature) 46 0 46
if(hooks->ver >= 7 && hooks->start_signature) 4 42 46
1011 4 (*hooks->start_signature)(aTHX_ &(ctx->ctx), hookdata);
1012 }
1013  
1014 71 ▶ while(lex_peek_unichar(0) != ')') {
Branch: 1 branch
BranchTrueFalseTotal
while(lex_peek_unichar(0) != ')') { 69 2 71
1015 69 lex_read_space(0);
1016 69 parse_sigelem(sigctx, flags);
1017  
1018 68 ▶ if(PL_parser->error_count) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_parser->error_count) { 0 68 68
1019 0 LEAVE;
1020 0 return NULL;
1021 }
1022  
1023 68 lex_read_space(0);
1024 68 switch(lex_peek_unichar(0)) {
1025 39 case ')': goto endofelems;
1026 29 case ',': break;
1027 0 default:
1028 0 fprintf(stderr, "ARGH unsure how to proceed parse_subsignature at <%s>\n",
1029 parser->bufptr);
1030 0 croak("ARGH");
1031 break;
1032 }
1033  
1034 29 lex_read_unichar(0);
1035 29 lex_read_space(0);
1036 }
1037 2 endofelems:
1038  
1039 41 ▶ if (!FEATURE_SIGNATURES_IS_ENABLED)
Branch: 5 branches
BranchTrueFalseTotal
if (!FEATURE_SIGNATURES_IS_ENABLED) 41 0 41
if (!FEATURE_SIGNATURES_IS_ENABLED) 41 0 41
if (!FEATURE_SIGNATURES_IS_ENABLED) 41 0 41
if (!FEATURE_SIGNATURES_IS_ENABLED) 41 0 41
if (!FEATURE_SIGNATURES_IS_ENABLED) 0 41 41
1040 0 croak("Experimental subroutine signatures not enabled");
1041  
1042 #if !HAVE_PERL_VERSION(5, 37, 0)
1043 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__SIGNATURES),
1044 "The signatures feature is experimental");
1045 #endif
1046  
1047 86 ▶ FOREACH_HOOKS_REVERSE {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_REVERSE { 45 41 86
FOREACH_HOOKS_REVERSE { 0 45 45
FOREACH_HOOKS_REVERSE { 45 41 86
1048 45 ▶ if(hooks->ver >= 7 && hooks->finish_signature)
Branch: 2 branches
BranchTrueFalseTotal
if(hooks->ver >= 7 && hooks->finish_signature) 45 0 45
if(hooks->ver >= 7 && hooks->finish_signature) 4 41 45
1049 4 (*hooks->finish_signature)(aTHX_ &(ctx->ctx), hookdata);
1050 }
1051  
1052 41 char slurpy_sigil = sigctx->slurpy_sigil;
1053 41 ▶ if(!slurpy_sigil && sigctx->named_details)
Branch: 2 branches
BranchTrueFalseTotal
if(!slurpy_sigil && sigctx->named_details) 35 6 41
if(!slurpy_sigil && sigctx->named_details) 18 17 35
1054 18 slurpy_sigil = '%';
1055  
1056 41 UNOP_AUX_item *aux = make_argcheck_aux(
1057 sigctx->n_elems,
1058 sigctx->n_optelems,
1059 slurpy_sigil);
1060  
1061 41 OP *checkop = newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux);
1062  
1063 41 OP *ops = op_prepend_elem(OP_LINESEQ, newSTATEOP(0, NULL, NULL),
1064 op_prepend_elem(OP_LINESEQ, checkop, sigctx->positional_elems));
1065  
1066 41 ▶ if(sigctx->named_details) {
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->named_details) { 23 18 41
1067 23 ▶ UV n_params = HvKEYS(sigctx->named_details);
Branch: 1 branch
BranchTrueFalseTotal
UV n_params = HvKEYS(sigctx->named_details); 0 23 23
1068  
1069 23 struct ArgElemsNamedAux *aux = safemalloc(
1070 23 sizeof(struct ArgElemsNamedAux) + n_params * sizeof(struct ArgElemsNamedParam)
1071 );
1072  
1073 23 aux->start_argix = sigctx->n_elems;
1074 23 aux->n_params = n_params;
1075  
1076 23 struct ArgElemsNamedParam *param = &aux->params[0];
1077  
1078 23 hv_iterinit(sigctx->named_details);
1079 HE *iter;
1080 56 ▶ while((iter = hv_iternext(sigctx->named_details))) {
Branch: 1 branch
BranchTrueFalseTotal
while((iter = hv_iternext(sigctx->named_details))) { 33 23 56
1081 STRLEN namelen;
1082 33 ▶ const char *namepv = HePV(iter, namelen);
Branch: 1 branch
BranchTrueFalseTotal
const char *namepv = HePV(iter, namelen); 0 33 33
1083 33 struct NamedParamDetails *details = (struct NamedParamDetails *)SvPVX(HeVAL(iter));
1084  
1085 66 *param = (struct ArgElemsNamedParam){
1086 66 .flags = details->flags |
1087 33 ▶ (HeUTF8(iter) ? NAMEDPARAMf_UTF8 : 0),
Branch: 2 branches
BranchTrueFalseTotal
(HeUTF8(iter) ? NAMEDPARAMf_UTF8 : 0), 0 33 33
(HeUTF8(iter) ? NAMEDPARAMf_UTF8 : 0), 0 33 33
1088 33 .padix = details->padix,
1089 33 .namehash = HeHASH(iter),
1090 33 .namepv = savepvn(namepv, namelen),
1091 .namelen = namelen,
1092 };
1093 33 param++;
1094 }
1095  
1096 23 ▶ if(aux->n_params > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(aux->n_params > 1) { 9 14 23
1097 /* Sort the params by hash value */
1098 9 qsort(&aux->params, aux->n_params, sizeof(aux->params[0]),
1099 &cmp_argelemsnamedparam);
1100 }
1101  
1102 23 OP *argelems_named_op = newUNOP_AUX(OP_CUSTOM, 0, NULL, (UNOP_AUX_item *)aux);
1103 23 argelems_named_op->op_ppaddr = &pp_argelems_named;
1104 23 ▶ if(sigctx->slurpy_sigil) {
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->slurpy_sigil) { 5 18 23
1105 5 ▶ if(sigctx->slurpy_elem && sigctx->slurpy_elem->op_type == OP_LINESEQ) {
Branch: 2 branches
BranchTrueFalseTotal
if(sigctx->slurpy_elem && sigctx->slurpy_elem->op_type == OP_LINESEQ) { 3 2 5
if(sigctx->slurpy_elem && sigctx->slurpy_elem->op_type == OP_LINESEQ) { 3 0 3
1106 /* A real named slurpy variable */
1107 3 ▶ OP *o = OpSIBLING(cLISTOPx(sigctx->slurpy_elem)->op_first);
Branch: 1 branch
BranchTrueFalseTotal
OP *o = OpSIBLING(cLISTOPx(sigctx->slurpy_elem)->op_first); 3 0 3
1108 assert(o);
1109 assert(o->op_type == OP_ARGELEM);
1110  
1111 /* Steal the slurpy's targ and private flags */
1112 3 argelems_named_op->op_targ = o->op_targ;
1113 3 argelems_named_op->op_private |= o->op_private & OPpARGELEM_MASK;
1114 }
1115 else {
1116 /* The slurpy is unnamed. Don't steal its targ but still set the
1117 * private flags
1118 */
1119 2 argelems_named_op->op_targ = 0;
1120 3 ▶ argelems_named_op->op_private = (sigctx->slurpy_sigil == '%') ? OPpARGELEM_HV :
Branch: 1 branch
BranchTrueFalseTotal
argelems_named_op->op_private = (sigctx->slurpy_sigil == '%') ? OPpARGELEM_HV : 1 1 2
1121 1 ▶ (sigctx->slurpy_sigil == '@') ? OPpARGELEM_AV :
Branch: 1 branch
BranchTrueFalseTotal
(sigctx->slurpy_sigil == '@') ? OPpARGELEM_AV : 1 0 1
1122 0;
1123 }
1124  
1125 5 ▶ if(sigctx->slurpy_elem) {
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->slurpy_elem) { 3 2 5
1126 3 op_free(sigctx->slurpy_elem);
1127 3 sigctx->slurpy_elem = NULL;
1128 }
1129 }
1130  
1131 23 ops = op_append_list(OP_LINESEQ, ops,
1132 newSTATEOP(0, NULL, NULL));
1133 23 ops = op_append_list(OP_LINESEQ, ops,
1134 argelems_named_op);
1135  
1136 23 ▶ if(sigctx->named_elem_defops)
Branch: 1 branch
BranchTrueFalseTotal
if(sigctx->named_elem_defops) 9 14 23
1137 /* TODO: append each elem individually */
1138 9 ops = op_append_list(OP_LINESEQ, ops,
1139 sigctx->named_elem_defops);
1140 }
1141 18 ▶ else if(sigctx->slurpy_elem) {
Branch: 1 branch
BranchTrueFalseTotal
else if(sigctx->slurpy_elem) { 1 17 18
1142 1 ops = op_append_list(OP_LINESEQ, ops, sigctx->slurpy_elem);
1143 }
1144  
1145 /* a nextstate at the end handles context correctly for an empty
1146 * sub body */
1147 41 ops = op_append_elem(OP_LINESEQ, ops, newSTATEOP(0, NULL, NULL));
1148  
1149 41 LEAVE;
1150  
1151 41 return ops;
1152 }
1153  
1154 9 void XPS_register_subsignature_attribute(pTHX_ const char *name, const struct XPSSignatureAttributeFuncs *funcs, void *funcdata)
1155 {
1156 SignatureAttributeRegistration *reg;
1157 9 Newx(reg, 1, struct SignatureAttributeRegistration);
1158  
1159 9 *reg = (struct SignatureAttributeRegistration){
1160 .name = name,
1161 .funcs = funcs,
1162 .funcdata = funcdata,
1163 };
1164  
1165 9 ▶ if(funcs->permit_hintkey)
Branch: 1 branch
BranchTrueFalseTotal
if(funcs->permit_hintkey) 9 0 9
1166 9 reg->permit_hintkeylen = strlen(funcs->permit_hintkey);
1167  
1168 9 reg->next = sigattrs;
1169 9 sigattrs = reg;
1170 9 }
1171  
1172 26 void XPS_boot_parse_subsignature_ex(pTHX)
1173 {
1174 26 XopENTRY_set(&xop_namedargexists, xop_name, "namedargexists");
1175 26 XopENTRY_set(&xop_namedargexists, xop_desc, "named argument element exists test");
1176 26 XopENTRY_set(&xop_namedargexists, xop_class, OA_LOGOP);
1177 26 Perl_custom_op_register(aTHX_ &pp_namedargexists, &xop_namedargexists);
1178  
1179 26 XopENTRY_set(&xop_namedargassign, xop_name, "namedargassign");
1180 26 XopENTRY_set(&xop_namedargassign, xop_desc, "named argument element assignment");
1181 26 XopENTRY_set(&xop_namedargassign, xop_class, OA_UNOP);
1182 26 Perl_custom_op_register(aTHX_ &pp_namedargassign, &xop_namedargassign);
1183  
1184 26 XopENTRY_set(&xop_argelems_named, xop_name, "argelems_named");
1185 26 XopENTRY_set(&xop_argelems_named, xop_desc, "named parameter elements");
1186 26 XopENTRY_set(&xop_argelems_named, xop_class, OA_UNOP_AUX);
1187 #ifdef HAVE_XOP_DUMP
1188 26 XopENTRY_set(&xop_argelems_named, xop_dump, &opdump_argelems_named);
1189 #endif
1190 26 Perl_custom_op_register(aTHX_ &pp_argelems_named, &xop_argelems_named);
1191  
1192 26 XopENTRY_set(&xop_refargelem, xop_name, "refargelem");
1193 26 XopENTRY_set(&xop_refargelem, xop_desc, "refalias argument element");
1194 26 XopENTRY_set(&xop_refargelem, xop_class, OA_UNOP_AUX);
1195 26 Perl_custom_op_register(aTHX_ &pp_refargelem, &xop_refargelem);
1196 26 }
1197  
1198 #else /* !HAVE_PERL_VERSION(5, 26, 0) */
1199  
1200 void XPS_signature_add_param(pTHX_ struct XSParseSublikeContext *ctx, struct XPSSignatureParamDetails *details)
1201 {
1202 }
1203  
1204 IV XPS_signature_query(pTHX_ struct XSParseSublikeContext *ctx, int q)
1205 {
1206 return 0;
1207 }
1208  
1209 void XPS_register_subsignature_attribute(pTHX_ const char *name, const struct XPSSignatureAttributeFuncs *funcs, void *funcdata)
1210 {
1211 croak("Custom subroutine signature attributes are not supported on this verison of Perl");
1212 }
1213  
1214 void XPS_boot_parse_subsignature_ex(pTHX)
1215 {
1216 }
1217 #endif
« lib/XS/Parse/Sublike.xs ↑ Summary hax/croak_from_caller.c.inc »