lib/XS/Parse/Sublike.xs

filter: Statementstmt 84.7% 312 / 368 Branchbran 75.7% 253 / 334 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.4% 565 / 702 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« t/flags.xs ↑ Summary src/parse_subsignature_ex.c »
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  
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10  
11 /* We need to be able to see FEATURE_*_IS_ENABLED */
12 #define PERL_EXT
13 #include "feature.h"
14  
15 #include "XSParseSublike.h"
16  
17 #define HAVE_PERL_VERSION(R, V, S) \
18 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
19  
20 #if HAVE_PERL_VERSION(5, 37, 10)
21 /* feature 'class' first became available in 5.37.9 but it wasn't until
22 * 5.37.10 that we could pass CVf_IsMETHOD to start_subparse()
23 */
24 # define HAVE_FEATURE_CLASS
25 #endif
26  
27 #if HAVE_PERL_VERSION(5, 18, 0)
28 # define HAVE_LEXICAL_SUB
29 #endif
30  
31 /* We always need this included to get the struct and function definitions
32 * visible, even though we won't be calling it
33 */
34 #include "parse_subsignature_ex.h"
35  
36 #if HAVE_PERL_VERSION(5, 26, 0)
37 # include "make_argcheck_aux.c.inc"
38  
39 # if !HAVE_PERL_VERSION(5, 31, 3)
40 # define parse_subsignature(flags) parse_subsignature_ex(0, NULL, NULL, 0) /* ignore core flags as there are none */
41 # endif
42  
43 # define HAVE_PARSE_SUBSIGNATURE
44 #endif
45  
46 #if !HAVE_PERL_VERSION(5, 22, 0)
47 # include "block_start.c.inc"
48 # include "block_end.c.inc"
49 #endif
50  
51 #ifndef wrap_keyword_plugin
52 # include "wrap_keyword_plugin.c.inc"
53 #endif
54  
55 #include "lexer-additions.c.inc"
56  
57 #define QUOTED_PVNf "\"%.*s\"%s"
58 #define QUOTED_PVNfARG(pv,len) ((len) <= 255 ? (int)(len) : 255), (pv), ((len) <= 255 ? "" : "...")
59  
60 /* Non-documented internal flags we use for our own purposes */
61 enum {
62 XS_PARSE_SUBLIKE_ACTION_CVf_IsMETHOD = (1<<31), /* do we set CVf_IsMETHOD? */
63 };
64  
65 89 static int parse(pTHX_
66 struct HooksAndData hooksanddata[],
67 size_t nhooks,
68 OP **op_ptr)
69 {
70 /* We need to reserve extra space in here for the sigctx pointer. To
71 * simplify much code here lets just pretend `ctx` is the actual context
72 * struct stored within
73 */
74 89 struct XPSContextWithPointer ctx_with_ptr = { 0 };
75 #define ctx (ctx_with_ptr.ctx)
76  
77 IV hooki;
78 const struct XSParseSublikeHooks *hooks;
79 void *hookdata;
80  
81 89 U8 require_parts = 0, skip_parts = 0;
82 89 bool have_dynamic_actions = FALSE;
83  
84 89 ENTER_with_name("parse_sublike");
85 /* From here onwards any `return` must be prefixed by LEAVE_with_name() */
86 89 U32 was_scopestack_ix = PL_scopestack_ix;
87  
88 89 ctx.moddata = newHV();
89 89 SAVEFREESV(ctx.moddata);
90  
91 187 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 98 89 187
FOREACH_HOOKS_FORWARD { 4 94 98
FOREACH_HOOKS_FORWARD { 98 89 187
92 98 require_parts |= hooks->require_parts;
93 98 skip_parts |= hooks->skip_parts;
94 98 ▶ if(!(hooks->flags & XS_PARSE_SUBLIKE_FLAG_BODY_OPTIONAL))
Branch: 1 branch
BranchTrueFalseTotal
if(!(hooks->flags & XS_PARSE_SUBLIKE_FLAG_BODY_OPTIONAL)) 86 12 98
95 86 require_parts |= XS_PARSE_SUBLIKE_PART_BODY;
96 98 ▶ if(hooks->flags & XS_PARSE_SUBLIKE_COMPAT_FLAG_DYNAMIC_ACTIONS)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->flags & XS_PARSE_SUBLIKE_COMPAT_FLAG_DYNAMIC_ACTIONS) 3 95 98
97 3 have_dynamic_actions = TRUE;
98 }
99  
100 89 ▶ if(!(skip_parts & XS_PARSE_SUBLIKE_PART_NAME)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(skip_parts & XS_PARSE_SUBLIKE_PART_NAME)) { 88 1 89
101 88 ctx.name = lex_scan_packagename();
102 88 lex_read_space(0);
103 }
104 89 ▶ if((require_parts & XS_PARSE_SUBLIKE_PART_NAME) && !ctx.name)
Branch: 2 branches
BranchTrueFalseTotal
if((require_parts & XS_PARSE_SUBLIKE_PART_NAME) && !ctx.name) 4 85 89
if((require_parts & XS_PARSE_SUBLIKE_PART_NAME) && !ctx.name) 1 3 4
105 1 croak("Expected name for sub-like construction");
106  
107 167 ▶ if(ctx.name && strstr(SvPV_nolen(ctx.name), "::")) {
Branch: 2 branches
BranchTrueFalseTotal
if(ctx.name && strstr(SvPV_nolen(ctx.name), "::")) { 79 9 88
if(ctx.name && strstr(SvPV_nolen(ctx.name), "::")) { 2 77 79
108 3 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 2 1 3
FOREACH_HOOKS_FORWARD { 0 2 2
FOREACH_HOOKS_FORWARD { 2 1 3
109 2 ▶ if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_ALLOW_PKGNAME)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_ALLOW_PKGNAME) 1 1 2
110 1 continue;
111  
112 1 croak("Declaring this sub-like function in another package is not permitted");
113 }
114 }
115  
116 /* Initial idea of actions are determined by whether we have a name */
117 174 ctx.actions = ctx.name
118 ? /* named */ XS_PARSE_SUBLIKE_ACTION_SET_CVNAME|XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL
119 87 ▶ : /* anon */ XS_PARSE_SUBLIKE_ACTION_CVf_ANON|XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE|XS_PARSE_SUBLIKE_ACTION_RET_EXPR;
Branch: 1 branch
BranchTrueFalseTotal
: /* anon */ XS_PARSE_SUBLIKE_ACTION_CVf_ANON|XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE|XS_PARSE_SUBLIKE_ACTION_RET_EXPR; 78 9 87
120  
121 183 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 96 87 183
FOREACH_HOOKS_FORWARD { 4 92 96
FOREACH_HOOKS_FORWARD { 96 87 183
122 96 ▶ if(hooks->pre_subparse)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->pre_subparse) 24 72 96
123 24 (*hooks->pre_subparse)(aTHX_ &ctx, hookdata);
124 }
125  
126 #ifdef DEBUGGING
127 if(PL_scopestack_ix != was_scopestack_ix)
128 croak("ARGH: pre_subparse broke the scopestack (was %d, now %d)\n",
129 was_scopestack_ix, PL_scopestack_ix);
130 #endif
131  
132 87 ▶ if(!have_dynamic_actions) {
Branch: 1 branch
BranchTrueFalseTotal
if(!have_dynamic_actions) { 84 3 87
133 84 ▶ if(ctx.name)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.name) 75 9 84
134 75 ctx.actions &= ~XS_PARSE_SUBLIKE_ACTION_CVf_ANON;
135 else
136 9 ctx.actions |= XS_PARSE_SUBLIKE_ACTION_CVf_ANON;
137 }
138  
139 87 int subparse_flags = 0;
140 87 ▶ if(ctx.actions & XS_PARSE_SUBLIKE_ACTION_CVf_ANON)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.actions & XS_PARSE_SUBLIKE_ACTION_CVf_ANON) 9 78 87
141 9 subparse_flags |= CVf_ANON;
142 #ifdef HAVE_FEATURE_CLASS
143 87 ▶ if(ctx.actions & XS_PARSE_SUBLIKE_ACTION_CVf_IsMETHOD)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.actions & XS_PARSE_SUBLIKE_ACTION_CVf_IsMETHOD) 5 82 87
144 5 subparse_flags |= CVf_IsMETHOD;
145 #endif
146  
147 /* TODO: We should find a way to put this in the main ctx structure, but we
148 * can't easily change that without breaking ABI compat.
149 */
150 87 PADOFFSET lexname_padix = 0;
151  
152 87 ▶ if(ctx.name && (ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL)) {
Branch: 2 branches
BranchTrueFalseTotal
if(ctx.name && (ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL)) { 78 9 87
if(ctx.name && (ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL)) { 2 76 78
153 2 SV *ampname = newSVpvf("&%" SVf, SVfARG(ctx.name));
154 2 SAVEFREESV(ampname);
155 2 lexname_padix = pad_add_name_sv(ampname, 0, NULL, NULL);
156 }
157  
158 87 I32 floor_ix = start_subparse(FALSE, subparse_flags);
159 87 SAVEFREESV(PL_compcv);
160  
161 #ifdef HAVE_LEXICAL_SUB
162 87 ▶ if(ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL) 2 85 87
163 /* Lexical subs always have CVf_CLONE */
164 2 CvCLONE_on(PL_compcv);
165 #endif
166  
167 87 ▶ if(!(skip_parts & XS_PARSE_SUBLIKE_PART_ATTRS) && (lex_peek_unichar(0) == ':')) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(skip_parts & XS_PARSE_SUBLIKE_PART_ATTRS) && (lex_peek_unichar(0) == ':')) { 86 1 87
if(!(skip_parts & XS_PARSE_SUBLIKE_PART_ATTRS) && (lex_peek_unichar(0) == ':')) { 5 81 86
168 5 lex_read_unichar(0);
169 5 lex_read_space(0);
170  
171 5 ctx.attrs = newLISTOP(OP_LIST, 0, NULL, NULL);
172  
173 5 while(1) {
174 10 SV *attr = newSV(0);
175 10 SV *val = newSV(0);
176 10 ▶ if(!lex_scan_attrval_into(attr, val))
Branch: 1 branch
BranchTrueFalseTotal
if(!lex_scan_attrval_into(attr, val)) 5 5 10
177 5 break;
178 5 lex_read_space(0);
179 5 ▶ if(lex_peek_unichar(0) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ':') { 0 5 5
180 0 lex_read_unichar(0);
181 0 lex_read_space(0);
182 }
183  
184 5 bool handled = FALSE;
185  
186 10 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 5 5 10
FOREACH_HOOKS_FORWARD { 0 5 5
FOREACH_HOOKS_FORWARD { 5 5 10
187 5 ▶ if(hooks->filter_attr)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->filter_attr) 1 4 5
188 1 handled |= (*hooks->filter_attr)(aTHX_ &ctx, attr, val, hookdata);
189 }
190  
191 5 ▶ if(handled) {
Branch: 1 branch
BranchTrueFalseTotal
if(handled) { 1 4 5
192 1 SvREFCNT_dec(attr);
193 1 SvREFCNT_dec(val);
194 1 continue;
195 }
196  
197 4 ▶ if(strEQ(SvPVX(attr), "lvalue")) {
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(SvPVX(attr), "lvalue")) { 0 4 4
198 0 CvLVALUE_on(PL_compcv);
199 0 continue;
200 }
201  
202 4 ▶ if(SvPOK(val))
Branch: 1 branch
BranchTrueFalseTotal
if(SvPOK(val)) 2 2 4
203 2 sv_catpvf(attr, "(%" SVf ")", val);
204 4 SvREFCNT_dec(val);
205  
206 4 ctx.attrs = op_append_elem(OP_LIST, ctx.attrs, newSVOP(OP_CONST, 0, attr));
207 }
208 }
209  
210 87 PL_hints |= HINT_LOCALIZE_HH;
211 87 I32 save_ix = block_start(TRUE);
212  
213 183 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 96 87 183
FOREACH_HOOKS_FORWARD { 4 92 96
FOREACH_HOOKS_FORWARD { 96 87 183
214 96 ▶ if(hooks->post_blockstart)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->post_blockstart) 14 82 96
215 14 (*hooks->post_blockstart)(aTHX_ &ctx, hookdata);
216 }
217  
218 #ifdef DEBUGGING
219 if(PL_scopestack_ix != was_scopestack_ix)
220 croak("ARGH: post_blockstart broke the scopestack (was %d, now %d)\n",
221 was_scopestack_ix, PL_scopestack_ix);
222 #endif
223  
224 #if HAVE_PERL_VERSION(5, 45, 3)
225 if(CvIsMETHOD(PL_compcv))
226 class_method_parse_post_blockstart(PL_compcv);
227 #endif
228  
229 #ifdef HAVE_PARSE_SUBSIGNATURE
230 87 OP *sigop = NULL;
231 87 ▶ if(!(skip_parts & XS_PARSE_SUBLIKE_PART_SIGNATURE) && (lex_peek_unichar(0) == '(')) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(skip_parts & XS_PARSE_SUBLIKE_PART_SIGNATURE) && (lex_peek_unichar(0) == '(')) { 86 1 87
if(!(skip_parts & XS_PARSE_SUBLIKE_PART_SIGNATURE) && (lex_peek_unichar(0) == '(')) { 55 31 86
232 55 lex_read_unichar(0);
233 55 lex_read_space(0);
234  
235 55 ▶ if(require_parts & XS_PARSE_SUBLIKE_PART_SIGNATURE) {
Branch: 1 branch
BranchTrueFalseTotal
if(require_parts & XS_PARSE_SUBLIKE_PART_SIGNATURE) { 4 51 55
236 #if HAVE_PERL_VERSION(5, 41, 8)
237 4 SAVEFEATUREBITS();
238 4 PL_compiling.cop_features.bits[FEATURE_SIGNATURES_INDEX] |= FEATURE_SIGNATURES_BIT;
239 #elif HAVE_PERL_VERSION(5, 32, 0)
240 SAVEI32(PL_compiling.cop_features);
241 PL_compiling.cop_features |= FEATURE_SIGNATURES_BIT;
242 #else
243 /* So far this is only used by the "method" keyword hack for perl 5.38
244 * onwards so this doesn't technically matter. Yet...
245 */
246 croak("TODO: import_pragma(\"feature\", \"signatures\")");
247 #endif
248 }
249  
250 55 U32 flags = 0;
251 55 bool have_sighooks = false;
252 114 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 59 55 114
FOREACH_HOOKS_FORWARD { 0 59 59
FOREACH_HOOKS_FORWARD { 59 55 114
253 59 ▶ if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_SIGNATURE_NAMED_PARAMS)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_SIGNATURE_NAMED_PARAMS) 32 27 59
254 32 flags |= PARSE_SUBSIGNATURE_NAMED_PARAMS;
255 59 ▶ if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_SIGNATURE_PARAM_ATTRIBUTES)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_SIGNATURE_PARAM_ATTRIBUTES) 15 44 59
256 15 flags |= PARSE_SUBSIGNATURE_PARAM_ATTRIBUTES;
257 59 ▶ if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_SIGNATURE_REFALIAS)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->flags & XS_PARSE_SUBLIKE_FLAG_SIGNATURE_REFALIAS) 16 43 59
258 16 flags |= PARSE_SUBSIGNATURE_REFALIAS;
259 59 ▶ if(hooks->ver >= 7 && (hooks->start_signature || hooks->finish_signature))
Branch: 3 branches
BranchTrueFalseTotal
if(hooks->ver >= 7 && (hooks->start_signature || hooks->finish_signature)) 59 0 59
if(hooks->ver >= 7 && (hooks->start_signature || hooks->finish_signature)) 55 4 59
if(hooks->ver >= 7 && (hooks->start_signature || hooks->finish_signature)) 0 55 55
260 4 have_sighooks = true;
261 }
262  
263 55 ▶ if(flags || have_sighooks)
Branch: 2 branches
BranchTrueFalseTotal
if(flags || have_sighooks) 17 38 55
if(flags || have_sighooks) 4 13 17
264 42 sigop = parse_subsignature_ex(flags, &ctx_with_ptr, hooksanddata, nhooks);
265 else {
266 #if HAVE_PERL_VERSION(5, 31, 3)
267 /* core's parse_subsignature doesn't seem able to handle empty sigs
268 * RT132284
269 * https://github.com/Perl/perl5/issues/17689
270 */
271 13 ▶ if(lex_peek_unichar(0) == ')') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ')') { 3 10 13
272 /* Inject an empty OP_ARGCHECK much as core would do if it encountered
273 * an empty signature */
274 3 UNOP_AUX_item *aux = make_argcheck_aux(0, 0, 0);
275  
276 3 sigop = op_prepend_elem(OP_LINESEQ, newSTATEOP(0, NULL, NULL),
277 newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux));
278  
279 /* a nextstate at the end handles context correctly for an empty
280 * sub body */
281 3 sigop = op_append_elem(OP_LINESEQ, sigop, newSTATEOP(0, NULL, NULL));
282  
283 #if HAVE_PERL_VERSION(5,31,5)
284 /* wrap the list of arg ops in a NULL aux op. This serves two
285 * purposes. First, it makes the arg list a separate subtree
286 * from the body of the sub, and secondly the null op may in
287 * future be upgraded to an OP_SIGNATURE when implemented. For
288 * now leave it as ex-argcheck
289 */
290 3 sigop = newUNOP_AUX(OP_ARGCHECK, 0, sigop, NULL);
291 3 op_null(sigop);
292 #endif
293 }
294 else
295 #endif
296 10 sigop = parse_subsignature(0);
297  
298 13 ▶ if(PL_parser->error_count) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_parser->error_count) { 0 13 13
299 assert(PL_scopestack_ix == was_scopestack_ix);
300 0 LEAVE_with_name("parse_sublike");
301 0 return 0;
302 }
303 }
304  
305 54 ▶ if(lex_peek_unichar(0) != ')')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != ')') 0 54 54
306 0 croak("Expected ')'");
307 54 lex_read_unichar(0);
308 54 lex_read_space(0);
309 }
310 #endif
311  
312 86 ▶ if(lex_peek_unichar(0) == '{') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == '{') { 82 4 86
313 /* TODO: technically possible to have skip body flag */
314 82 ctx.body = parse_block(0);
315 82 SvREFCNT_inc(PL_compcv);
316 }
317 4 ▶ else if(require_parts & XS_PARSE_SUBLIKE_PART_BODY)
Branch: 1 branch
BranchTrueFalseTotal
else if(require_parts & XS_PARSE_SUBLIKE_PART_BODY) 3 1 4
318 3 croak("Expected '{' for block body");
319 1 ▶ else if(lex_peek_unichar(0) == ';') {
Branch: 1 branch
BranchTrueFalseTotal
else if(lex_peek_unichar(0) == ';') { 0 1 1
320 /* nothing to be done */
321 }
322 else
323 0 croak("Expected '{' for block body or ';'");
324  
325 #ifdef HAVE_PARSE_SUBSIGNATURE
326 83 ▶ if(ctx.body && sigop) {
Branch: 2 branches
BranchTrueFalseTotal
if(ctx.body && sigop) { 82 1 83
if(ctx.body && sigop) { 54 28 82
327 /* parse_block() returns an empy block as a stub op.
328 * no need to keep that if we we have a signature.
329 */
330 54 ▶ if (ctx.body->op_type == OP_STUB) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.body->op_type == OP_STUB) { 13 41 54
331 13 op_free(ctx.body);
332 13 ctx.body = NULL;
333 }
334 54 ctx.body = op_append_list(OP_LINESEQ, sigop, ctx.body);
335 }
336 #endif
337  
338 83 ▶ if(PL_parser->error_count) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_parser->error_count) { 0 83 83
339 /* parse_block() still sometimes returns a valid body even if a parse
340 * error happens.
341 * We need to destroy this partial body before returning a valid(ish)
342 * state to the keyword hook mechanism, so it will find the error count
343 * correctly
344 * See https://rt.cpan.org/Ticket/Display.html?id=130417
345 */
346 0 op_free(ctx.body);
347  
348 /* REALLY??! Do I really have to do this??
349 * See also:
350 * https://www.nntp.perl.org/group/perl.perl5.porters/2021/06/msg260642.html
351 */
352 0 ▶ while(PL_scopestack_ix > was_scopestack_ix)
Branch: 1 branch
BranchTrueFalseTotal
while(PL_scopestack_ix > was_scopestack_ix) 0 0 0
353 0 LEAVE;
354  
355 0 *op_ptr = newOP(OP_NULL, 0);
356 0 ▶ if(ctx.name) {
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.name) { 0 0 0
357 0 SvREFCNT_dec(ctx.name);
358 assert(PL_scopestack_ix == was_scopestack_ix);
359 0 LEAVE_with_name("parse_sublike");
360 0 return KEYWORD_PLUGIN_STMT;
361 }
362 else {
363 assert(PL_scopestack_ix == was_scopestack_ix);
364 0 LEAVE_with_name("parse_sublike");
365 0 return KEYWORD_PLUGIN_EXPR;
366 }
367 }
368  
369 175 ▶ FOREACH_HOOKS_REVERSE {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_REVERSE { 92 83 175
FOREACH_HOOKS_REVERSE { 4 88 92
FOREACH_HOOKS_REVERSE { 92 83 175
370 92 ▶ if(hooks->pre_blockend)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->pre_blockend) 16 76 92
371 16 (*hooks->pre_blockend)(aTHX_ &ctx, hookdata);
372 }
373  
374 #ifdef DEBUGGING
375 if(PL_scopestack_ix != was_scopestack_ix)
376 croak("ARGH: pre_blockend broke the scopestack (was %d, now %d)\n",
377 was_scopestack_ix, PL_scopestack_ix);
378 #endif
379  
380 83 ▶ if(ctx.body) {
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.body) { 82 1 83
381 82 ctx.body = block_end(save_ix, ctx.body);
382  
383 82 ▶ if(!have_dynamic_actions) {
Branch: 1 branch
BranchTrueFalseTotal
if(!have_dynamic_actions) { 79 3 82
384 79 ▶ if(ctx.name) {
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.name) { 71 8 79
385 71 ctx.actions |= XS_PARSE_SUBLIKE_ACTION_SET_CVNAME;
386 71 ▶ if(!(ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL))
Branch: 1 branch
BranchTrueFalseTotal
if(!(ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL)) 69 2 71
387 69 ctx.actions |= XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL;
388 }
389 else
390 8 ctx.actions &= ~(XS_PARSE_SUBLIKE_ACTION_SET_CVNAME|XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL);
391 }
392  
393 /* If we want both SET_CVNAME and INSTALL_SYMBOL actions we might as well
394 * let newATTRSUB() do it. If we only wanted one we need to be more subtle
395 */
396 82 bool action_set_cvname = ctx.actions & XS_PARSE_SUBLIKE_ACTION_SET_CVNAME;
397 82 bool action_install_symbol = ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL;
398 82 bool action_install_lexical = ctx.actions & XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL;
399 82 ▶ if(action_install_symbol && action_install_lexical)
Branch: 2 branches
BranchTrueFalseTotal
if(action_install_symbol && action_install_lexical) 71 11 82
if(action_install_symbol && action_install_lexical) 0 71 71
400 0 croak("Cannot both ACTION_INSTALL_SYMBOL and ACTION_INSTALL_LEXICAL");
401  
402 82 OP *nameop = NULL;
403 82 ▶ if(ctx.name && action_set_cvname && action_install_symbol)
Branch: 3 branches
BranchTrueFalseTotal
if(ctx.name && action_set_cvname && action_install_symbol) 74 8 82
if(ctx.name && action_set_cvname && action_install_symbol) 74 0 74
if(ctx.name && action_set_cvname && action_install_symbol) 71 3 74
404 71 nameop = newSVOP(OP_CONST, 0, SvREFCNT_inc(ctx.name));
405  
406 82 ▶ if(!nameop && action_install_symbol)
Branch: 2 branches
BranchTrueFalseTotal
if(!nameop && action_install_symbol) 11 71 82
if(!nameop && action_install_symbol) 0 11 11
407 0 warn("Setting XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL without _ACTION_SET_CVNAME is nonsensical");
408  
409 82 ▶ if(action_install_lexical) {
Branch: 1 branch
BranchTrueFalseTotal
if(action_install_lexical) { 2 80 82
410 #ifdef HAVE_LEXICAL_SUB
411 assert(lexname_padix);
412 2 nameop = newOP(OP_PADANY, 0);
413 2 nameop->op_targ = lexname_padix;
414  
415 2 ctx.cv = newMYSUB(floor_ix, nameop, NULL, ctx.attrs, ctx.body);
416 #else
417 PERL_UNUSED_VAR(lexname_padix);
418 croak("XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL is not supported on this version of Perl");
419 #endif
420 }
421 else
422 80 ctx.cv = newATTRSUB(floor_ix, nameop, NULL, ctx.attrs, ctx.body);
423  
424 82 ▶ if(!nameop && action_set_cvname) {
Branch: 2 branches
BranchTrueFalseTotal
if(!nameop && action_set_cvname) { 9 73 82
if(!nameop && action_set_cvname) { 1 8 9
425 #if HAVE_PERL_VERSION(5,22,0)
426 STRLEN namelen;
427 1 const char *name = SvPV_const(ctx.name, namelen);
428 U32 hash;
429 1 ▶ PERL_HASH(hash, name, namelen);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(hash, name, namelen); 1 0 1
430  
431 /* Core's CvNAME_HEK_set macro uses unshare_hek() which isn't exposed. But we
432 * likely don't need it here */
433 #ifndef unshare_hek
434 # define unshare_hek(h) (void)0
435 #endif
436 assert(!CvNAME_HEK(ctx.cv));
437  
438 1 ▶ CvNAME_HEK_set(ctx.cv,
Branch: 2 branches
BranchTrueFalseTotal
CvNAME_HEK_set(ctx.cv, 0 1 1
CvNAME_HEK_set(ctx.cv, 1 0 1
439 share_hek(name, SvUTF8(ctx.name) ? -namelen : namelen, hash));
440 #endif
441 }
442  
443 82 ctx.attrs = NULL;
444 82 ctx.body = NULL;
445 }
446  
447 175 ▶ FOREACH_HOOKS_FORWARD {
Branch: 3 branches
BranchTrueFalseTotal
FOREACH_HOOKS_FORWARD { 92 83 175
FOREACH_HOOKS_FORWARD { 4 88 92
FOREACH_HOOKS_FORWARD { 92 83 175
448 92 ▶ if(hooks->post_newcv)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->post_newcv) 15 77 92
449 15 (*hooks->post_newcv)(aTHX_ &ctx, hookdata);
450 }
451  
452 assert(PL_scopestack_ix == was_scopestack_ix);
453 83 LEAVE_with_name("parse_sublike");
454  
455 83 ▶ if(!have_dynamic_actions) {
Branch: 1 branch
BranchTrueFalseTotal
if(!have_dynamic_actions) { 80 3 83
456 80 ▶ if(!ctx.name)
Branch: 1 branch
BranchTrueFalseTotal
if(!ctx.name) 8 72 80
457 8 ctx.actions |= XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE;
458 else
459 72 ctx.actions &= ~XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE;
460 }
461  
462 83 ▶ if(!(ctx.actions & XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(ctx.actions & XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE)) { 73 10 83
463 73 *op_ptr = newOP(OP_NULL, 0);
464  
465 73 SvREFCNT_dec(ctx.name);
466 }
467 else {
468 10 *op_ptr = newUNOP(OP_REFGEN, 0,
469 newSVOP(OP_ANONCODE, 0, (SV *)ctx.cv));
470 }
471  
472 83 ▶ if(!have_dynamic_actions) {
Branch: 1 branch
BranchTrueFalseTotal
if(!have_dynamic_actions) { 80 3 83
473 80 ▶ if(!ctx.name)
Branch: 1 branch
BranchTrueFalseTotal
if(!ctx.name) 8 72 80
474 8 ctx.actions |= XS_PARSE_SUBLIKE_ACTION_RET_EXPR;
475 else
476 72 ctx.actions &= ~XS_PARSE_SUBLIKE_ACTION_RET_EXPR;
477 }
478  
479 83 ▶ return (ctx.actions & XS_PARSE_SUBLIKE_ACTION_RET_EXPR) ? KEYWORD_PLUGIN_EXPR : KEYWORD_PLUGIN_STMT;
Branch: 1 branch
BranchTrueFalseTotal
return (ctx.actions & XS_PARSE_SUBLIKE_ACTION_RET_EXPR) ? KEYWORD_PLUGIN_EXPR : KEYWORD_PLUGIN_STMT; 10 73 83
480 #undef ctx
481 }
482  
483 0 static int IMPL_xs_parse_sublike_v6(pTHX_ const struct XSParseSublikeHooks *hooks, void *hookdata, OP **op_ptr)
484 {
485 0 struct HooksAndData hd = { .hooks = hooks, .data = hookdata };
486 0 return parse(aTHX_ &hd, 1, op_ptr);
487 }
488  
489 struct Registration;
490 struct Registration {
491 struct Registration *next;
492 const char *kw;
493 STRLEN kwlen;
494 union {
495 const struct XSParseSublikeHooks *hooks;
496 };
497 void *hookdata;
498  
499 STRLEN permit_hintkey_len;
500 };
501  
502 #define REGISTRATIONS_LOCK OP_CHECK_MUTEX_LOCK
503 #define REGISTRATIONS_UNLOCK OP_CHECK_MUTEX_UNLOCK
504  
505 static struct Registration *registrations;
506  
507 149 static void register_sublike(pTHX_ const char *kw, const struct XSParseSublikeHooks *hooks, void *hookdata, int ver)
508 {
509 149 ▶ if(ver < 4)
Branch: 1 branch
BranchTrueFalseTotal
if(ver < 4) 0 149 149
510 0 croak("Mismatch in sublike keyword registration ABI version field: module wants %u; we require >= 4\n",
511 ver);
512 149 ▶ if(ver > XSPARSESUBLIKE_ABI_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if(ver > XSPARSESUBLIKE_ABI_VERSION) 0 149 149
513 0 croak("Mismatch in sublike keyword registration ABI version field: module wants %u; we support <= %d\n",
514 ver, XSPARSESUBLIKE_ABI_VERSION);
515  
516 struct Registration *reg;
517 149 Newx(reg, 1, struct Registration);
518  
519 149 reg->kw = savepv(kw);
520 149 reg->kwlen = strlen(kw);
521 149 reg->hooks = hooks;
522 149 reg->hookdata = hookdata;
523  
524 149 ▶ if(reg->hooks->permit_hintkey)
Branch: 1 branch
BranchTrueFalseTotal
if(reg->hooks->permit_hintkey) 113 36 149
525 113 reg->permit_hintkey_len = strlen(reg->hooks->permit_hintkey);
526 else
527 36 reg->permit_hintkey_len = 0;
528  
529 149 ▶ if(!reg->hooks->permit && !reg->hooks->permit_hintkey)
Branch: 2 branches
BranchTrueFalseTotal
if(!reg->hooks->permit && !reg->hooks->permit_hintkey) 113 36 149
if(!reg->hooks->permit && !reg->hooks->permit_hintkey) 0 113 113
530 0 croak("Third-party sublike keywords require a permit callback or hinthash key");
531  
532 REGISTRATIONS_LOCK;
533 {
534 149 reg->next = registrations;
535 149 registrations = reg;
536 }
537 REGISTRATIONS_UNLOCK;
538 149 }
539  
540 71 static void IMPL_register_xs_parse_sublike_v6(pTHX_ const char *kw, const struct XSParseSublikeHooks *hooks, void *hookdata)
541 {
542 71 int ver = hooks->ver;
543 71 ▶ if(!ver)
Branch: 1 branch
BranchTrueFalseTotal
if(!ver) 0 71 71
544 /* Caller forgot to set .ver but for source-level compat we'll presume they
545 * wanted version 6, the first ABI version that added the .ver field
546 */
547 0 ver = 6;
548  
549 71 register_sublike(aTHX_ kw, hooks, hookdata, ver);
550 71 }
551  
552 107744 static const struct Registration *find_permitted(pTHX_ const char *kw, STRLEN kwlen)
553 {
554 const struct Registration *reg;
555  
556 107744 HV *hints = GvHV(PL_hintgv);
557  
558 738339 ▶ for(reg = registrations; reg; reg = reg->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(reg = registrations; reg; reg = reg->next) { 630686 107653 738339
559 630686 ▶ if(reg->kwlen != kwlen || !strnEQ(reg->kw, kw, kwlen))
Branch: 2 branches
BranchTrueFalseTotal
if(reg->kwlen != kwlen || !strnEQ(reg->kw, kw, kwlen)) 78649 552037 630686
if(reg->kwlen != kwlen || !strnEQ(reg->kw, kw, kwlen)) 68063 10586 78649
560 620100 continue;
561  
562 10586 ▶ if(reg->hooks->permit_hintkey &&
Branch: 2 branches
BranchTrueFalseTotal
if(reg->hooks->permit_hintkey && 10560 26 10586
if(reg->hooks->permit_hintkey && 10560 0 10560
563 10560 ▶ (!hints || !hv_fetch(hints, reg->hooks->permit_hintkey, reg->permit_hintkey_len, 0)))
Branch: 1 branch
BranchTrueFalseTotal
(!hints || !hv_fetch(hints, reg->hooks->permit_hintkey, reg->permit_hintkey_len, 0))) 10494 66 10560
564 10494 continue;
565  
566 92 ▶ if(reg->hooks->permit &&
Branch: 1 branch
BranchTrueFalseTotal
if(reg->hooks->permit && 26 66 92
567 26 ▶ !(*reg->hooks->permit)(aTHX_ reg->hookdata))
Branch: 1 branch
BranchTrueFalseTotal
!(*reg->hooks->permit)(aTHX_ reg->hookdata)) 1 25 26
568 1 continue;
569  
570 91 return reg;
571 }
572  
573 107653 return NULL;
574 }
575  
576 1 static int IMPL_xs_parse_sublike_any_v6(pTHX_ const struct XSParseSublikeHooks *hooksA, void *hookdataA, OP **op_ptr)
577 {
578 1 SV *kwsv = lex_scan_ident();
579 1 ▶ if(!kwsv || !SvCUR(kwsv))
Branch: 2 branches
BranchTrueFalseTotal
if(!kwsv || !SvCUR(kwsv)) 1 0 1
if(!kwsv || !SvCUR(kwsv)) 0 1 1
580 0 croak("Expected a keyword to introduce a sub or sub-like construction");
581  
582 1 const char *kw = SvPV_nolen(kwsv);
583 1 STRLEN kwlen = SvCUR(kwsv);
584  
585 1 lex_read_space(0);
586  
587 1 const struct Registration *reg = NULL;
588 /* We permit 'sub' as a NULL set of hooks; anything else should be a registered keyword */
589 1 ▶ if(kwlen != 3 || !strEQ(kw, "sub")) {
Branch: 2 branches
BranchTrueFalseTotal
if(kwlen != 3 || !strEQ(kw, "sub")) { 0 1 1
if(kwlen != 3 || !strEQ(kw, "sub")) { 0 0 0
590 1 reg = find_permitted(aTHX_ kw, kwlen);
591 1 ▶ if(!reg)
Branch: 1 branch
BranchTrueFalseTotal
if(!reg) 0 1 1
592 0 ▶ croak("Expected a keyword to introduce a sub or sub-like construction, found " QUOTED_PVNf,
Branch: 2 branches
BranchTrueFalseTotal
croak("Expected a keyword to introduce a sub or sub-like construction, found " QUOTED_PVNf, 0 0 0
croak("Expected a keyword to introduce a sub or sub-like construction, found " QUOTED_PVNf, 0 0 0
593 QUOTED_PVNfARG(kw, kwlen));
594 }
595  
596 1 SvREFCNT_dec(kwsv);
597  
598 1 struct HooksAndData hd[] = {
599 { .hooks = hooksA, .data = hookdataA },
600 { 0 }
601 };
602  
603 1 ▶ if(reg) {
Branch: 1 branch
BranchTrueFalseTotal
if(reg) { 1 0 1
604 1 hd[1].hooks = reg->hooks;
605 1 hd[1].data = reg->hookdata;
606 }
607  
608 1 ▶ return parse(aTHX_ hd, 1 + !!reg, op_ptr);
Branch: 1 branch
BranchTrueFalseTotal
return parse(aTHX_ hd, 1 + !!reg, op_ptr); 1 0 1
609 }
610  
611 9 static void IMPL_register_xps_signature_attribute(pTHX_ const char *name, const struct XPSSignatureAttributeFuncs *funcs, void *funcdata)
612 {
613 9 ▶ if(funcs->ver < 5)
Branch: 1 branch
BranchTrueFalseTotal
if(funcs->ver < 5) 0 9 9
614 0 croak("Mismatch in signature param attribute ABI version field: module wants %u; we require >= 5\n",
615 funcs->ver);
616 9 ▶ if(funcs->ver > XSPARSESUBLIKE_ABI_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if(funcs->ver > XSPARSESUBLIKE_ABI_VERSION) 0 9 9
617 0 croak("Mismatch in signature param attribute ABI version field: module wants %u; we support <= %d\n",
618 funcs->ver, XSPARSESUBLIKE_ABI_VERSION);
619  
620 9 ▶ if(!name || !(name[0] >= 'A' && name[0] <= 'Z'))
Branch: 3 branches
BranchTrueFalseTotal
if(!name || !(name[0] >= 'A' && name[0] <= 'Z')) 9 0 9
if(!name || !(name[0] >= 'A' && name[0] <= 'Z')) 9 0 9
if(!name || !(name[0] >= 'A' && name[0] <= 'Z')) 0 9 9
621 0 croak("Signature param attribute names must begin with a capital letter");
622  
623 9 ▶ if(!funcs->permit_hintkey)
Branch: 1 branch
BranchTrueFalseTotal
if(!funcs->permit_hintkey) 0 9 9
624 0 croak("Signature param attributes require a permit hinthash key");
625  
626 9 register_subsignature_attribute(name, funcs, funcdata);
627 9 }
628  
629 #ifdef HAVE_FEATURE_CLASS
630 5 static bool permit_core_method(pTHX_ void *hookdata)
631 {
632 5 ▶ return FEATURE_CLASS_IS_ENABLED;
Branch: 3 branches
BranchTrueFalseTotal
return FEATURE_CLASS_IS_ENABLED; 5 0 5
return FEATURE_CLASS_IS_ENABLED; 5 0 5
return FEATURE_CLASS_IS_ENABLED; 5 0 5
633 }
634  
635 5 static void pre_subparse_core_method(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
636 {
637 5 ctx->actions |= XS_PARSE_SUBLIKE_ACTION_CVf_IsMETHOD;
638 5 }
639  
640 static const struct XSParseSublikeHooks hooks_core_method = {
641 .ver = XSPARSESUBLIKE_ABI_VERSION,
642 .permit = &permit_core_method,
643 .pre_subparse = &pre_subparse_core_method,
644 .require_parts = XS_PARSE_SUBLIKE_PART_SIGNATURE, /* enable signatures feature */
645 };
646 #endif
647  
648 #ifdef HAVE_LEXICAL_SUB
649 2 static void pre_subparse_lexical_sub(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata)
650 {
651 2 ctx->actions &= ~XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL;
652 2 ctx->actions |= XS_PARSE_SUBLIKE_ACTION_INSTALL_LEXICAL;
653 2 }
654  
655 static const struct XSParseSublikeHooks hooks_lexical_sub = {
656 .ver = XSPARSESUBLIKE_ABI_VERSION,
657 /* no permit needed */
658 .pre_subparse = &pre_subparse_lexical_sub,
659 };
660 #endif
661  
662 /* Sublike::Extended */
663  
664 static struct XSParseSublikeHooks hooks_extended = {
665 .ver = XSPARSESUBLIKE_ABI_VERSION,
666 .permit_hintkey = "Sublike::Extended/extended",
667 .flags = XS_PARSE_SUBLIKE_FLAG_PREFIX|
668 XS_PARSE_SUBLIKE_FLAG_BODY_OPTIONAL|
669 XS_PARSE_SUBLIKE_FLAG_SIGNATURE_NAMED_PARAMS|
670 XS_PARSE_SUBLIKE_FLAG_SIGNATURE_PARAM_ATTRIBUTES|
671 XS_PARSE_SUBLIKE_FLAG_SIGNATURE_REFALIAS,
672  
673 /* No hooks */
674 };
675  
676 static struct XSParseSublikeHooks hooks_extended_sub = {
677 .ver = XSPARSESUBLIKE_ABI_VERSION,
678 .permit_hintkey = "Sublike::Extended/extended-sub",
679 .flags = XS_PARSE_SUBLIKE_FLAG_BODY_OPTIONAL|
680 XS_PARSE_SUBLIKE_FLAG_SIGNATURE_NAMED_PARAMS|
681 XS_PARSE_SUBLIKE_FLAG_SIGNATURE_PARAM_ATTRIBUTES|
682 XS_PARSE_SUBLIKE_FLAG_SIGNATURE_REFALIAS,
683  
684 /* No hooks */
685 };
686  
687 /* keyword plugin */
688  
689 static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
690  
691 127631 static int my_keyword_plugin(pTHX_ char *kw, STRLEN kwlen, OP **op_ptr)
692 {
693 127631 char *orig_kw = kw;
694 127631 STRLEN orig_kwlen = kwlen;
695  
696 #ifdef HAVE_LEXICAL_SUB
697 127631 char *was_parser_bufptr = PL_parser->bufptr;
698  
699 127631 bool is_lexical_sub = false;
700  
701 127631 ▶ if(kwlen == 2 && strEQ(kw, "my")) {
Branch: 2 branches
BranchTrueFalseTotal
if(kwlen == 2 && strEQ(kw, "my")) { 42226 85405 127631
if(kwlen == 2 && strEQ(kw, "my")) { 19898 22328 42226
702 19898 lex_read_space(0);
703  
704 19898 I32 c = lex_peek_unichar(0);
705 19898 ▶ if(!isIDFIRST_uni(c))
Branch: 4 branches
BranchTrueFalseTotal
if(!isIDFIRST_uni(c)) 19898 0 19898
if(!isIDFIRST_uni(c)) 19898 0 19898
if(!isIDFIRST_uni(c)) 19895 3 19898
if(!isIDFIRST_uni(c)) 19895 3 19898
706 19895 goto next_keyword;
707  
708 3 kw = PL_parser->bufptr;
709  
710 3 lex_read_unichar(0);
711 12 ▶ while((c = lex_peek_unichar(0)) && isALNUM_uni(c))
Branch: 6 branches
BranchTrueFalseTotal
while((c = lex_peek_unichar(0)) && isALNUM_uni(c)) 12 0 12
while((c = lex_peek_unichar(0)) && isALNUM_uni(c)) 12 0 12
while((c = lex_peek_unichar(0)) && isALNUM_uni(c)) 12 0 12
while((c = lex_peek_unichar(0)) && isALNUM_uni(c)) 9 3 12
while((c = lex_peek_unichar(0)) && isALNUM_uni(c)) 9 3 12
while((c = lex_peek_unichar(0)) && isALNUM_uni(c)) 0 0 0
712 9 lex_read_unichar(0);
713  
714 3 kwlen = PL_parser->bufptr - kw;
715  
716 3 is_lexical_sub = true;
717 }
718 #endif
719  
720 107736 const struct Registration *reg = find_permitted(aTHX_ kw, kwlen);
721  
722 107736 ▶ if(!reg) {
Branch: 1 branch
BranchTrueFalseTotal
if(!reg) { 107648 88 107736
723 #ifdef HAVE_LEXICAL_SUB
724 107648 ▶ if(PL_parser->bufptr > was_parser_bufptr)
Branch: 1 branch
BranchTrueFalseTotal
if(PL_parser->bufptr > was_parser_bufptr) 107647 1 107648
725 1 PL_parser->bufptr = was_parser_bufptr;
726 107647 next_keyword:
727 #endif
728 127543 return (*next_keyword_plugin)(aTHX_ orig_kw, orig_kwlen, op_ptr);
729 }
730  
731 88 lex_read_space(0);
732  
733 /* We'll abuse the SvPVX storage of an SV to keep an array of HooksAndData
734 * structures
735 */
736 88 SV *hdlsv = newSV(4 * sizeof(struct HooksAndData));
737 88 SAVEFREESV(hdlsv);
738 88 struct HooksAndData *hd = (struct HooksAndData *)SvPVX(hdlsv);
739 88 size_t nhooks = 0;
740  
741 #ifdef HAVE_LEXICAL_SUB
742 88 ▶ if(is_lexical_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_lexical_sub) { 2 86 88
743 2 hd[nhooks].hooks = &hooks_lexical_sub;
744 2 hd[nhooks].data = NULL;
745 2 nhooks++;
746 }
747 #endif
748  
749 88 struct XSParseSublikeHooks *hooks = (struct XSParseSublikeHooks *)reg->hooks;
750  
751 88 hd[nhooks].hooks = hooks;
752 88 hd[nhooks].data = reg->hookdata;
753 88 nhooks++;
754  
755 90 ▶ while(hooks->flags & XS_PARSE_SUBLIKE_FLAG_PREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
while(hooks->flags & XS_PARSE_SUBLIKE_FLAG_PREFIX) { 7 83 90
756 /* After a prefixing keyword, expect another one */
757 7 SV *kwsv = lex_scan_ident();
758 7 SAVEFREESV(kwsv);
759  
760 7 ▶ if(!kwsv || !SvCUR(kwsv))
Branch: 2 branches
BranchTrueFalseTotal
if(!kwsv || !SvCUR(kwsv)) 7 0 7
if(!kwsv || !SvCUR(kwsv)) 0 7 7
761 0 croak("Expected a keyword to introduce a sub or sub-like construction");
762  
763 7 kw = SvPV_nolen(kwsv);
764 7 kwlen = SvCUR(kwsv);
765  
766 7 lex_read_space(0);
767  
768 7 reg = find_permitted(aTHX_ kw, kwlen);
769  
770 /* We permit 'sub' as a NULL set of hooks; anything else should be a registered keyword */
771 7 ▶ if(!reg && kwlen == 3 && strEQ(kw, "sub"))
Branch: 3 branches
BranchTrueFalseTotal
if(!reg && kwlen == 3 && strEQ(kw, "sub")) 5 2 7
if(!reg && kwlen == 3 && strEQ(kw, "sub")) 5 0 5
if(!reg && kwlen == 3 && strEQ(kw, "sub")) 5 0 5
772 5 break;
773 2 ▶ if(!reg)
Branch: 1 branch
BranchTrueFalseTotal
if(!reg) 0 2 2
774 0 ▶ croak("Expected a keyword to introduce a sub or sub-like construction, found " QUOTED_PVNf,
Branch: 2 branches
BranchTrueFalseTotal
croak("Expected a keyword to introduce a sub or sub-like construction, found " QUOTED_PVNf, 0 0 0
croak("Expected a keyword to introduce a sub or sub-like construction, found " QUOTED_PVNf, 0 0 0
775 QUOTED_PVNfARG(kw, kwlen));
776  
777 2 hooks = (struct XSParseSublikeHooks *)reg->hooks;
778  
779 2 ▶ if(SvLEN(hdlsv) < (nhooks + 1) * sizeof(struct HooksAndData)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvLEN(hdlsv) < (nhooks + 1) * sizeof(struct HooksAndData)) { 0 2 2
780 0 ▶ SvGROW(hdlsv, SvLEN(hdlsv) * 2);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(hdlsv, SvLEN(hdlsv) * 2); 0 0 0
SvGROW(hdlsv, SvLEN(hdlsv) * 2); 0 0 0
781 0 hd = (struct HooksAndData *)SvPVX(hdlsv);
782 }
783 2 hd[nhooks].hooks = hooks;
784 2 hd[nhooks].data = reg->hookdata;
785 2 nhooks++;
786 }
787  
788 /* See if Sublike::Extended wants to claim this one. If it wanted 'sub' it
789 * has already claimed that above */
790 88 ▶ if(kwlen != 3 || !strEQ(kw, "sub")) {
Branch: 2 branches
BranchTrueFalseTotal
if(kwlen != 3 || !strEQ(kw, "sub")) { 6 82 88
if(kwlen != 3 || !strEQ(kw, "sub")) { 0 6 6
791 82 HV *hints = GvHV(PL_hintgv);
792 82 SV *keysv = sv_2mortal(newSVpvf("Sublike::Extended/extended-%.*s", (int)kwlen, kw));
793 82 ▶ if(hints && hv_exists_ent(hints, keysv, 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if(hints && hv_exists_ent(hints, keysv, 0)) { 82 0 82
if(hints && hv_exists_ent(hints, keysv, 0)) { 4 78 82
794 4 ▶ if(SvLEN(hdlsv) < (nhooks + 1) * sizeof(struct HooksAndData)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvLEN(hdlsv) < (nhooks + 1) * sizeof(struct HooksAndData)) { 0 4 4
795 0 ▶ SvGROW(hdlsv, SvLEN(hdlsv) * 2);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(hdlsv, SvLEN(hdlsv) * 2); 0 0 0
SvGROW(hdlsv, SvLEN(hdlsv) * 2); 0 0 0
796 0 hd = (struct HooksAndData *)SvPVX(hdlsv);
797 }
798 /* This hook has the prefix flag set, but it doesn't matter because
799 * we've finished processing those already
800 */
801 4 hd[nhooks].hooks = &hooks_extended;
802 4 hd[nhooks].data = NULL;
803 4 nhooks++;
804 }
805 }
806  
807 88 return parse(aTHX_ hd, nhooks, op_ptr);
808 }
809  
810 /* API v3 back-compat */
811  
812 0 static int IMPL_xs_parse_sublike_v3(pTHX_ const void *hooks, void *hookdata, OP **op_ptr)
813 {
814 0 croak("XS::Parse::Sublike ABI v3 is no longer supported; the caller should be rebuilt to use v4");
815 }
816  
817 0 static void IMPL_register_xs_parse_sublike_v3(pTHX_ const char *kw, const void *hooks, void *hookdata)
818 {
819 0 croak("XS::Parse::Sublike ABI v3 is no longer supported; the caller should be rebuilt to use v4");
820 }
821  
822 0 static int IMPL_xs_parse_sublike_any_v3(pTHX_ const void *hooksA, void *hookdataA, OP **op_ptr)
823 {
824 0 croak("XS::Parse::Sublike ABI v3 is no longer supported; the caller should be rebuilt to use v4");
825 }
826  
827 /* API v4 back-compat */
828  
829 struct XSParseSublikeHooks_v4 {
830 U16 flags;
831 U8 require_parts;
832 U8 skip_parts;
833 const char *permit_hintkey;
834 bool (*permit)(pTHX_ void *hookdata);
835 void (*pre_subparse) (pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
836 void (*post_blockstart)(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
837 void (*pre_blockend) (pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
838 void (*post_newcv) (pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
839 bool (*filter_attr) (pTHX_ struct XSParseSublikeContext *ctx, SV *attr, SV *val, void *hookdata);
840 };
841  
842 #define STRUCT_XSPARSESUBLIKEHOOKS_FROM_v4(hooks_v4) \
843 (struct XSParseSublikeHooks){ \
844 .ver = 4, \
845 .flags = hooks_v4->flags, \
846 .require_parts = hooks_v4->require_parts, \
847 .skip_parts = hooks_v4->skip_parts, \
848 .permit_hintkey = hooks_v4->permit_hintkey, \
849 .permit = hooks_v4->permit, \
850 .pre_subparse = hooks_v4->pre_subparse, \
851 .filter_attr = (hooks_v4->flags & XS_PARSE_SUBLIKE_FLAG_FILTERATTRS) \
852 ? hooks_v4->filter_attr \
853 : NULL, \
854 .post_blockstart = hooks_v4->post_blockstart, \
855 .pre_blockend = hooks_v4->pre_blockend, \
856 .post_newcv = hooks_v4->post_newcv, \
857 }
858  
859 0 static int IMPL_xs_parse_sublike_v4(pTHX_ const struct XSParseSublikeHooks_v4 *hooks_v4, void *hookdata, OP **op_ptr)
860 {
861 0 return IMPL_xs_parse_sublike_v6(aTHX_
862 0 ▶ &STRUCT_XSPARSESUBLIKEHOOKS_FROM_v4(hooks_v4),
Branch: 1 branch
BranchTrueFalseTotal
&STRUCT_XSPARSESUBLIKEHOOKS_FROM_v4(hooks_v4), 0 0 0
863 hookdata,
864 op_ptr);
865 }
866  
867 0 static void IMPL_register_xs_parse_sublike_v4(pTHX_ const char *kw, const struct XSParseSublikeHooks_v4 *hooks_v4, void *hookdata)
868 {
869 struct XSParseSublikeHooks *hooks;
870 0 Newx(hooks, 1, struct XSParseSublikeHooks);
871 0 ▶ *hooks = STRUCT_XSPARSESUBLIKEHOOKS_FROM_v4(hooks_v4);
Branch: 1 branch
BranchTrueFalseTotal
*hooks = STRUCT_XSPARSESUBLIKEHOOKS_FROM_v4(hooks_v4); 0 0 0
872  
873 0 register_sublike(aTHX_ kw, hooks, hookdata, 4);
874 0 }
875  
876 0 static int IMPL_xs_parse_sublike_any_v4(pTHX_ const struct XSParseSublikeHooks_v4 *hooksA_v4, void *hookdataA, OP **op_ptr)
877 {
878 0 return IMPL_xs_parse_sublike_any_v6(aTHX_
879 0 ▶ &STRUCT_XSPARSESUBLIKEHOOKS_FROM_v4(hooksA_v4),
Branch: 1 branch
BranchTrueFalseTotal
&STRUCT_XSPARSESUBLIKEHOOKS_FROM_v4(hooksA_v4), 0 0 0
880 hookdataA,
881 op_ptr);
882 }
883  
884 MODULE = XS::Parse::Sublike PACKAGE = XS::Parse::Sublike
885  
886 BOOT:
887 /* Legacy lookup mechanism using perl symbol table */
888 26 sv_setiv(get_sv("XS::Parse::Sublike::ABIVERSION", GV_ADDMULTI), 4);
889 26 sv_setuv(get_sv("XS::Parse::Sublike::PARSE", GV_ADDMULTI), PTR2UV(&IMPL_xs_parse_sublike_v3));
890 26 sv_setuv(get_sv("XS::Parse::Sublike::REGISTER", GV_ADDMULTI), PTR2UV(&IMPL_register_xs_parse_sublike_v3));
891 26 sv_setuv(get_sv("XS::Parse::Sublike::PARSEANY", GV_ADDMULTI), PTR2UV(&IMPL_xs_parse_sublike_any_v3));
892  
893 /* Newer mechanism */
894 26 sv_setiv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/ABIVERSION_MIN", 1), 4);
895 26 sv_setiv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/ABIVERSION_MAX", 1), XSPARSESUBLIKE_ABI_VERSION);
896  
897 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/parse()@4", 1), PTR2UV(&IMPL_xs_parse_sublike_v4));
898 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/parse()@6", 1), PTR2UV(&IMPL_xs_parse_sublike_v6));
899 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/register()@6", 1), PTR2UV(&IMPL_register_xs_parse_sublike_v6));
900 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/register()@4", 1), PTR2UV(&IMPL_register_xs_parse_sublike_v4));
901 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/parseany()@4", 1), PTR2UV(&IMPL_xs_parse_sublike_any_v4));
902 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/parseany()@6", 1), PTR2UV(&IMPL_xs_parse_sublike_any_v6));
903 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/signature_add_param()@7", 1), PTR2UV(&XPS_signature_add_param));
904 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/signature_query()@8", 1), PTR2UV(&XPS_signature_query));
905  
906 26 sv_setuv(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/register_sigattr()@5", 1), PTR2UV(&IMPL_register_xps_signature_attribute));
907 #ifdef HAVE_FEATURE_CLASS
908 26 register_sublike(aTHX_ "method", &hooks_core_method, NULL, 4);
909 #endif
910  
911 26 wrap_keyword_plugin(&my_keyword_plugin, &next_keyword_plugin);
912  
913 26 register_sublike(aTHX_ "extended", &hooks_extended, NULL, 4);
914 26 register_sublike(aTHX_ "sub", &hooks_extended_sub, NULL, 4);
915  
916 26 boot_parse_subsignature_ex();
« t/flags.xs ↑ Summary src/parse_subsignature_ex.c »