lib/Devel/CallParser.xs

filter: Statementstmt 86.7% 137 / 158 Branchbran 61.2% 71 / 116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.9% 208 / 274 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Devel/CallParser.pm »
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "callchecker0.h"
5 #include "XSUB.h"
6  
7 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
8 #define PERL_DECIMAL_VERSION \
9 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
10 #ifndef PERL_VERSION_GE
11 # define PERL_VERSION_GE(r,v,s) \
12 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
13 #endif /* !PERL_VERSION_GE */
14  
15 /* the pad's name list: a PADNAMELIST from 5.21.7, an AV before */
16 #if PERL_VERSION_GE(5,21,7)
17 # define Q_PADNAMES_MAX(pnl) PadnamelistMAX(pnl)
18 # define Q_PADNAMES_TRIM(pnl, n) STMT_START { \
19 if(PadnamelistMAX(pnl) > (n)) PadnamelistMAX(pnl) = (n); \
20 } STMT_END
21 #else /* < 5.21.7 */
22 # define Q_PADNAMES_MAX(pnl) av_len(pnl)
23 # define Q_PADNAMES_TRIM(pnl, n) STMT_START { \
24 if(av_len(pnl) > (n)) av_fill(pnl, n); \
25 } STMT_END
26 #endif /* < 5.21.7 */
27  
28 #ifndef op_append_elem
29 # define op_append_elem(t, f, l) THX_op_append_elem(aTHX_ t, f, l)
30 static OP *THX_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
31 {
32 if(!first) return last;
33 if(!last) return first;
34 if(first->op_type != (unsigned)type ||
35 (type == OP_LIST && (first->op_flags & OPf_PARENS)))
36 return newLISTOP(type, 0, first, last);
37 if(first->op_flags & OPf_KIDS) {
38 cLISTOPx(first)->op_last->op_sibling = last;
39 } else {
40 first->op_flags |= OPf_KIDS;
41 cLISTOPx(first)->op_first = last;
42 }
43 cLISTOPx(first)->op_last = last;
44 return first;
45 }
46 #endif /* !op_append_elem */
47  
48 #ifndef qerror
49 /* Perl_qerror is exported (EXp) on all platforms but its declaration
50 * is hidden behind PERL_CORE/PERL_EXT header guards. Declare it
51 * ourselves so we can use it. */
52 EXTERN_C void Perl_qerror(pTHX_ SV *err);
53 # define qerror(m) Perl_qerror(aTHX_ m)
54 #endif /* !qerror */
55  
56 #define QPFX C8K61oRQKxigiqmUlVdk_
57 #define QPFXS STRINGIFY(QPFX)
58 #define QCONCAT0(a,b) a##b
59 #define QCONCAT1(a,b) QCONCAT0(a,b)
60 #define QPFXD(name) QCONCAT1(QPFX, name)
61  
62 #if defined(WIN32) && PERL_VERSION_GE(5,13,6)
63 # define MY_BASE_CALLCONV EXTERN_C
64 # define MY_BASE_CALLCONV_S "EXTERN_C"
65 #else /* !(WIN32 && >= 5.13.6) */
66 # define MY_BASE_CALLCONV PERL_CALLCONV
67 # define MY_BASE_CALLCONV_S "PERL_CALLCONV"
68 #endif /* !(WIN32 && >= 5.13.6) */
69  
70 #define MY_EXPORT_CALLCONV MY_BASE_CALLCONV
71  
72 #if defined(WIN32) || defined(__CYGWIN__)
73 # define MY_IMPORT_CALLCONV_S MY_BASE_CALLCONV_S" __declspec(dllimport)"
74 #else
75 # define MY_IMPORT_CALLCONV_S MY_BASE_CALLCONV_S
76 #endif
77  
78 static MGVTBL mgvtbl_parsecall;
79  
80 typedef OP *(*Perl_call_parser)(pTHX_ GV *, SV *, U32 *);
81  
82 #define CALLPARSER_PARENS 0x00000001
83 #define CALLPARSER_STATEMENT 0x00000002
84  
85 #ifdef parse_fullexpr
86 # define Q_PARSER_AVAILABLE 1
87 #endif /* parse_fullexpr */
88  
89 #if Q_PARSER_AVAILABLE
90  
91 # define Perl_parse_args_parenthesised QPFXD(pac0)
92 # define parse_args_parenthesised(fp) Perl_parse_args_parenthesised(aTHX_ fp)
93 179 MY_EXPORT_CALLCONV OP *QPFXD(pac0)(pTHX_ U32 *flags_p)
94 {
95 OP *argsop;
96 179 lex_read_space(0);
97 179 ▶ if(lex_peek_unichar(0) != '('/*)*/) {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '('/*)*/) { 29 150 179
98 29 qerror(mess("syntax error"));
99 29 return NULL;
100 }
101 150 lex_read_unichar(0);
102 150 argsop = parse_fullexpr(PARSE_OPTIONAL);
103 150 lex_read_space(0);
104 150 ▶ if(lex_peek_unichar(0) != /*(*/')') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != /*(*/')') { 0 150 150
105 0 qerror(mess("syntax error"));
106 0 return argsop;
107 }
108 150 lex_read_unichar(0);
109 150 *flags_p |= CALLPARSER_PARENS;
110 150 return argsop;
111 }
112  
113 # define Perl_parse_args_nullary QPFXD(paz0)
114 # define parse_args_nullary(fp) Perl_parse_args_nullary(aTHX_ fp)
115 63 MY_EXPORT_CALLCONV OP *QPFXD(paz0)(pTHX_ U32 *flags_p)
116 {
117 63 lex_read_space(0);
118 63 ▶ if(lex_peek_unichar(0) == '('/*)*/)
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == '('/*)*/) 28 35 63
119 28 return parse_args_parenthesised(flags_p);
120 35 return NULL;
121 }
122  
123 # define Perl_parse_args_unary QPFXD(pau0)
124 # define parse_args_unary(fp) Perl_parse_args_unary(aTHX_ fp)
125 95 MY_EXPORT_CALLCONV OP *QPFXD(pau0)(pTHX_ U32 *flags_p)
126 {
127 95 lex_read_space(0);
128 95 ▶ if(lex_peek_unichar(0) == '('/*)*/)
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == '('/*)*/) 36 59 95
129 36 return parse_args_parenthesised(flags_p);
130 59 return parse_arithexpr(PARSE_OPTIONAL);
131 }
132  
133 # define Perl_parse_args_list QPFXD(pal0)
134 # define parse_args_list(fp) Perl_parse_args_list(aTHX_ fp)
135 79 MY_EXPORT_CALLCONV OP *QPFXD(pal0)(pTHX_ U32 *flags_p)
136 {
137 79 lex_read_space(0);
138 79 ▶ if(lex_peek_unichar(0) == '('/*)*/)
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == '('/*)*/) 32 47 79
139 32 return parse_args_parenthesised(flags_p);
140 47 return parse_listexpr(PARSE_OPTIONAL);
141 }
142  
143 # define Perl_parse_args_block_list QPFXD(pab0)
144 # define parse_args_block_list(fp) Perl_parse_args_block_list(aTHX_ fp)
145 63 MY_EXPORT_CALLCONV OP *QPFXD(pab0)(pTHX_ U32 *flags_p)
146 {
147 OP *blkop, *argsop;
148 I32 c;
149 63 lex_read_space(0);
150 63 c = lex_peek_unichar(0);
151 63 ▶ if(c == '('/*)*/) return parse_args_parenthesised(flags_p);
Branch: 1 branch
BranchTrueFalseTotal
if(c == '('/*)*/) return parse_args_parenthesised(flags_p); 28 35 63
152 35 ▶ if(c == '{'/*}*/) {
Branch: 1 branch
BranchTrueFalseTotal
if(c == '{'/*}*/) { 8 27 35
153 8 I32 floor = start_subparse(0, CVf_ANON);
154 8 SAVEFREESV(PL_compcv);
155 8 blkop = parse_block(0);
156 8 ▶ SvREFCNT_inc_simple_void((SV*)PL_compcv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void((SV*)PL_compcv); 8 0 8
157 8 blkop = newANONATTRSUB(floor, NULL, NULL, blkop);
158 } else {
159 27 blkop = NULL;
160 }
161 35 argsop = parse_listexpr(PARSE_OPTIONAL);
162 32 return op_prepend_elem(OP_LIST, blkop, argsop);
163 }
164  
165 # define Perl_parse_args_proto QPFXD(pap0)
166 # define parse_args_proto(gv, sv, fp) Perl_parse_args_proto(aTHX_ gv, sv, fp)
167 80 MY_EXPORT_CALLCONV OP *QPFXD(pap0)(pTHX_ GV *namegv, SV *protosv, U32 *flags_p)
168 {
169 STRLEN proto_len;
170 char const *proto;
171 PERL_UNUSED_ARG(namegv);
172 80 ▶ if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv)) 24 56 80
if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv)) 16 64 80
173 16 croak("panic: parse_args_proto with no proto");
174 /*
175 * There are variations between Perl versions in the syntactic
176 * interpretation of prototypes, which this code in principle
177 * needs to track. However, from the introduction of the parser
178 * API functions required by this code (5.13.8) to the date
179 * of this note (5.14.0-RC0) there have been no such changes.
180 * With luck there may be no more before this function migrates
181 * into the core.
182 */
183 64 proto = SvPV(protosv, proto_len);
184 64 ▶ if(!proto_len) return parse_args_nullary(flags_p);
Branch: 1 branch
BranchTrueFalseTotal
if(!proto_len) return parse_args_nullary(flags_p); 8 56 64
185 72 ▶ while(*proto == ';') proto++;
Branch: 1 branch
BranchTrueFalseTotal
while(*proto == ';') proto++; 16 56 72
186 56 ▶ if(proto[0] == '&') return parse_args_block_list(flags_p);
Branch: 1 branch
BranchTrueFalseTotal
if(proto[0] == '&') return parse_args_block_list(flags_p); 8 48 56
187 48 ▶ if(((proto[0] == '$' || proto[0] == '_' ||
Branch: 2 branches
BranchTrueFalseTotal
if(((proto[0] == '$' || proto[0] == '_' || 8 40 48
if(((proto[0] == '$' || proto[0] == '_' || 8 0 8
188 8 ▶ proto[0] == '*' || proto[0] == '+') &&
Branch: 2 branches
BranchTrueFalseTotal
proto[0] == '*' || proto[0] == '+') && 8 0 8
proto[0] == '*' || proto[0] == '+') && 0 8 8
189 40 ▶ !proto[1]) ||
Branch: 1 branch
BranchTrueFalseTotal
!proto[1]) || 0 40 40
190 8 ▶ (proto[0] == '\\' && proto[1] && !proto[2]))
Branch: 3 branches
BranchTrueFalseTotal
(proto[0] == '\\' && proto[1] && !proto[2])) 0 8 8
(proto[0] == '\\' && proto[1] && !proto[2])) 0 0 0
(proto[0] == '\\' && proto[1] && !proto[2])) 0 0 0
191 40 return parse_args_unary(flags_p);
192 8 ▶ if(proto[0] == '\\' && proto[1] == '['/*]*/) {
Branch: 2 branches
BranchTrueFalseTotal
if(proto[0] == '\\' && proto[1] == '['/*]*/) { 0 8 8
if(proto[0] == '\\' && proto[1] == '['/*]*/) { 0 0 0
193 0 proto += 2;
194 0 ▶ while(*proto && *proto != /*[*/']') proto++;
Branch: 2 branches
BranchTrueFalseTotal
while(*proto && *proto != /*[*/']') proto++; 0 0 0
while(*proto && *proto != /*[*/']') proto++; 0 0 0
195 0 ▶ if(proto[0] == /*[*/']' && !proto[1])
Branch: 2 branches
BranchTrueFalseTotal
if(proto[0] == /*[*/']' && !proto[1]) 0 0 0
if(proto[0] == /*[*/']' && !proto[1]) 0 0 0
196 0 return parse_args_unary(flags_p);
197 }
198 8 return parse_args_list(flags_p);
199 }
200  
201 # define Perl_parse_args_proto_or_list QPFXD(pan0)
202 # define parse_args_proto_or_list(gv, sv, fp) \
203 Perl_parse_args_proto_or_list(aTHX_ gv, sv, fp)
204 32 MY_EXPORT_CALLCONV OP *QPFXD(pan0)(pTHX_ GV *namegv, SV *protosv, U32 *flags_p)
205 {
206 32 ▶ if(SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
Branch: 2 branches
BranchTrueFalseTotal
if(SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv)) 16 16 32
if(SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv)) 16 16 32
207 16 return parse_args_proto(namegv, protosv, flags_p);
208 else
209 16 return parse_args_list(flags_p);
210 }
211  
212 #endif /* Q_PARSER_AVAILABLE */
213  
214 #ifndef mg_findext
215 # define mg_findext(sv, type, vtbl) THX_mg_findext(aTHX_ sv, type, vtbl)
216 static MAGIC *THX_mg_findext(pTHX_ SV *sv, int type, MGVTBL const *vtbl)
217 {
218 MAGIC *mg;
219 if(sv)
220 for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
221 if(mg->mg_type == type && mg->mg_virtual == vtbl)
222 return mg;
223 return NULL;
224 }
225 #endif /* !mg_findext */
226  
227 #ifndef sv_unmagicext
228 # define sv_unmagicext(sv, type, vtbl) THX_sv_unmagicext(aTHX_ sv, type, vtbl)
229 static int THX_sv_unmagicext(pTHX_ SV *sv, int type, MGVTBL const *vtbl)
230 {
231 MAGIC *mg, **mgp;
232 if((vtbl && vtbl->svt_free) || type == PERL_MAGIC_regex_global)
233 /* exceeded intended usage of this reserve implementation */
234 return 0;
235 if(SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv)) return 0;
236 mgp = NULL;
237 for(mg = mgp ? *mgp : SvMAGIC(sv); mg; mg = mgp ? *mgp : SvMAGIC(sv)) {
238 if(mg->mg_type == type && mg->mg_virtual == vtbl) {
239 if(mgp)
240 *mgp = mg->mg_moremagic;
241 else
242 SvMAGIC_set(sv, mg->mg_moremagic);
243 if(mg->mg_flags & MGf_REFCOUNTED)
244 SvREFCNT_dec(mg->mg_obj);
245 Safefree(mg);
246 } else {
247 mgp = &mg->mg_moremagic;
248 }
249 }
250 SvMAGICAL_off(sv);
251 mg_magical(sv);
252 return 0;
253 }
254 #endif /* !sv_unmagicext */
255  
256 2090 MY_EXPORT_CALLCONV void QPFXD(gcp0)(pTHX_ CV *cv,
257 Perl_call_parser *psfun_p, SV **psobj_p)
258 {
259 4180 MAGIC *callmg = SvMAGICAL((SV*)cv) ?
260 2090 ▶ mg_findext((SV*)cv, PERL_MAGIC_ext, &mgvtbl_parsecall) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
mg_findext((SV*)cv, PERL_MAGIC_ext, &mgvtbl_parsecall) : NULL; 424 1666 2090
261 2090 ▶ if(callmg) {
Branch: 1 branch
BranchTrueFalseTotal
if(callmg) { 397 1693 2090
262 397 *psfun_p = DPTR2FPTR(Perl_call_parser, callmg->mg_ptr);
263 397 *psobj_p = callmg->mg_obj;
264 } else {
265 1693 *psfun_p = DPTR2FPTR(Perl_call_parser, NULL);
266 1693 *psobj_p = NULL;
267 }
268 2090 }
269  
270 48 MY_EXPORT_CALLCONV void QPFXD(scp0)(pTHX_ CV *cv,
271 Perl_call_parser psfun, SV *psobj)
272 {
273 48 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if( 2 46 48
274 2 ▶ (!psfun && !psobj)
Branch: 1 branch
BranchTrueFalseTotal
(!psfun && !psobj) 0 2 2
275 #if Q_PARSER_AVAILABLE
276 46 ▶ || (psfun == Perl_parse_args_proto_or_list && psobj == (SV*)cv)
Branch: 2 branches
BranchTrueFalseTotal
|| (psfun == Perl_parse_args_proto_or_list && psobj == (SV*)cv) 7 39 46
|| (psfun == Perl_parse_args_proto_or_list && psobj == (SV*)cv) 2 5 7
277 #endif /* Q_PARSER_AVAILABLE */
278 ) {
279 4 ▶ if(SvMAGICAL((SV*)cv))
Branch: 1 branch
BranchTrueFalseTotal
if(SvMAGICAL((SV*)cv)) 4 0 4
280 4 sv_unmagicext((SV*)cv, PERL_MAGIC_ext,
281 &mgvtbl_parsecall);
282 } else {
283 MAGIC *callmg =
284 44 mg_findext((SV*)cv, PERL_MAGIC_ext, &mgvtbl_parsecall);
285 44 ▶ if(!callmg)
Branch: 1 branch
BranchTrueFalseTotal
if(!callmg) 33 11 44
286 33 callmg = sv_magicext((SV*)cv, &PL_sv_undef,
287 PERL_MAGIC_ext, &mgvtbl_parsecall, NULL, 0);
288 44 ▶ if(callmg->mg_flags & MGf_REFCOUNTED) {
Branch: 1 branch
BranchTrueFalseTotal
if(callmg->mg_flags & MGf_REFCOUNTED) { 44 0 44
289 44 SvREFCNT_dec(callmg->mg_obj);
290 44 callmg->mg_flags &= ~MGf_REFCOUNTED;
291 }
292 44 callmg->mg_ptr = FPTR2DPTR(char *, psfun);
293 44 callmg->mg_obj = psobj;
294 44 ▶ if(psobj != (SV*)cv) {
Branch: 1 branch
BranchTrueFalseTotal
if(psobj != (SV*)cv) { 44 0 44
295 44 SvREFCNT_inc(psobj);
296 44 callmg->mg_flags |= MGf_REFCOUNTED;
297 }
298 }
299 48 }
300  
301 #if Q_PARSER_AVAILABLE
302  
303 10 MY_EXPORT_CALLCONV void QPFXD(gcp1)(pTHX_ CV *cv,
304 Perl_call_parser *psfun_p, SV **psobj_p)
305 {
306 10 QPFXD(gcp0)(aTHX_ cv, psfun_p, psobj_p);
307 10 ▶ if(!*psfun_p && !*psobj_p) {
Branch: 2 branches
BranchTrueFalseTotal
if(!*psfun_p && !*psobj_p) { 6 4 10
if(!*psfun_p && !*psobj_p) { 6 0 6
308 6 *psfun_p = Perl_parse_args_proto_or_list;
309 6 *psobj_p = (SV*)cv;
310 }
311 10 }
312  
313 41 MY_EXPORT_CALLCONV void QPFXD(scp1)(pTHX_ CV *cv,
314 Perl_call_parser psfun, SV *psobj)
315 {
316 41 ▶ if(!psobj) croak("null object for cv_set_call_parser");
Branch: 1 branch
BranchTrueFalseTotal
if(!psobj) croak("null object for cv_set_call_parser"); 0 41 41
317 41 QPFXD(scp0)(aTHX_ cv, psfun, psobj);
318 41 }
319  
320 #endif /* Q_PARSER_AVAILABLE */
321  
322 /*
323 * Find a CV by name, checking lexical hints first (for Lexical::Sub
324 * and similar modules), then falling back to package lookup.
325 * This is more reliable than rv2cv_op_cv() on threaded/debugging perls.
326 *
327 * Lexical::Sub (via Lexical::Var) stores CVs with the key format:
328 * "Lexical::Var/&subname"
329 */
330 50562 static CV *THX_find_lexical_cv(pTHX_ char *name, STRLEN len)
331 {
332 /* Check %^H hints hash - where Lexical::Sub stores its subs */
333 50562 ▶ if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) { 1075 49487 50562
if ((PL_hints & HINT_LOCALIZE_HH) && GvHV(PL_hintgv)) { 1075 0 1075
334 SV *keysv;
335 SV **svp;
336 /* Construct the Lexical::Var key format: "Lexical::Var/&name" */
337 1075 keysv = newSVpvs("Lexical::Var/&");
338 1075 sv_catpvn(keysv, name, len);
339 1075 svp = hv_fetch(GvHV(PL_hintgv), SvPVX(keysv), (I32)SvCUR(keysv), 0);
340 1075 SvREFCNT_dec(keysv);
341 1075 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 0 1075 1075
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 0 0 0
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 0 0 0
342 0 return (CV*)SvRV(*svp);
343 }
344 }
345 50562 return NULL;
346 }
347  
348 static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
349 50562 static int my_keyword_plugin(pTHX_
350 char *keyword_ptr, STRLEN keyword_len, OP **op_ptr)
351 {
352 OP *nmop, *cvop, *argsop;
353 CV *cv;
354 GV *namegv;
355 Perl_call_parser psfun;
356 SV *psobj;
357 U32 parser_flags;
358 /*
359 * First, check for lexically-scoped CVs directly in %^H.
360 * This handles Lexical::Sub and similar modules reliably
361 * across all Perl configurations (threaded, debugging, etc.).
362 */
363 50562 cv = THX_find_lexical_cv(aTHX_ keyword_ptr, keyword_len);
364 50562 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 0 50562 50562
365 0 QPFXD(gcp0)(aTHX_ cv, &psfun, &psobj);
366 0 ▶ if (psfun || psobj) {
Branch: 2 branches
BranchTrueFalseTotal
if (psfun || psobj) { 0 0 0
if (psfun || psobj) { 0 0 0
367 /* Found lexical CV with call parser - use it */
368 0 nmop = newSVOP(OP_CONST, 0,
369 newSVpvn(keyword_ptr, keyword_len));
370 0 nmop->op_private = OPpCONST_BARE;
371 0 cvop = newUNOP(OP_RV2CV, 0,
372 newSVOP(OP_CONST, 0, newRV_inc((SV*)cv)));
373 0 namegv = (GV*)newSVpvn(keyword_ptr, keyword_len);
374 0 parser_flags = 0;
375 0 argsop = psfun(aTHX_ namegv, psobj, &parser_flags);
376 0 SvREFCNT_dec((SV*)namegv);
377 0 ▶ if(!(parser_flags & CALLPARSER_PARENS))
Branch: 1 branch
BranchTrueFalseTotal
if(!(parser_flags & CALLPARSER_PARENS)) 0 0 0
378 0 cvop->op_private |= OPpENTERSUB_NOPAREN;
379 0 *op_ptr = newUNOP(OP_ENTERSUB, OPf_STACKED,
380 op_append_elem(OP_LIST, argsop, cvop));
381 0 return (parser_flags & CALLPARSER_STATEMENT) ?
382 0 ▶ KEYWORD_PLUGIN_STMT : KEYWORD_PLUGIN_EXPR;
Branch: 1 branch
BranchTrueFalseTotal
KEYWORD_PLUGIN_STMT : KEYWORD_PLUGIN_EXPR; 0 0 0
383 }
384 }
385 /*
386 * Fall back to original op-based lookup for package subs.
387 *
388 * Creation of the rv2cv op below (or more precisely its gv op
389 * child created during checking) uses a pad slot under threads.
390 * Normally this is fine, but early versions of the padrange
391 * mechanism make assumptions about pad slots being contiguous
392 * that this breaks. On the affected perl versions, therefore,
393 * we watch for the pad slot being consumed, and restore the
394 * pad's fill pointer if we throw the op away (upon declining
395 * to handle the keyword).
396 *
397 * The core bug was supposedly fixed in Perl 5.19.4, but actually
398 * that version exhibits a different bug also apparently related
399 * to padrange. Restoring the pad's fill pointer works around
400 * this bug too. So for now this workaround is used with no
401 * upper bound on the Perl version.
402 *
403 * The pad's name list must be restored with it. Allocating the
404 * constant slot for the gv op stores a placeholder name at that
405 * index, which can extend the name list; freeing the op replaces
406 * the placeholder but leaves the list's fill where it was. A
407 * block opened right after the declined keyword then takes the
408 * inflated fill as its floor, the first "my" inside the block
409 * lands on the floor slot, and pad_leavemy never closes it: the
410 * lexical stays visible after the block, masking the outer one
411 * of the same name (a "masks earlier declaration in same scope"
412 * warning where the two are in different scopes).
413 */
414 #define MUST_RESTORE_PAD_FILL PERL_VERSION_GE(5,17,6)
415 #if MUST_RESTORE_PAD_FILL
416 50562 I32 padfill = av_len(PL_comppad);
417 50562 SSize_t namefill = Q_PADNAMES_MAX(PL_comppad_name);
418 #endif /* MUST_RESTORE_PAD_FILL */
419 /*
420 * If Devel::Declare happens to be loaded, it triggers magic
421 * upon building of an rv2cv op, assuming that it's being built
422 * by the lexer. Since we're about to build such an op here,
423 * replicating what the lexer will normally do shortly after,
424 * there's a risk that Devel::Declare could fire here, ultimately
425 * firing twice for a single appearance of a name it's interested
426 * in. To suppress Devel::Declare, therefore, we temporarily
427 * set PL_parser to null. The same goes for Data::Alias and
428 * some other modules that use similar techniques.
429 *
430 * Unfortunately Devel::Declare prior to 0.006004 still does some
431 * work at the wrong time if PL_parser is null, and Data::Alias
432 * prior to 1.13 crashes if PL_parser is null. So this module
433 * is not compatible with earlier versions of those modules,
434 * and can't be made compatible.
435 */
436 50562 ENTER;
437 50562 SAVEVPTR(PL_parser);
438 50562 PL_parser = NULL;
439 50562 nmop = newSVOP(OP_CONST, 0, newSVpvn(keyword_ptr, keyword_len));
440 50562 nmop->op_private = OPpCONST_BARE;
441 50562 cvop = newCVREF(0, nmop);
442 50562 LEAVE;
443 50562 ▶ if(!(cv = rv2cv_op_cv(cvop, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if(!(cv = rv2cv_op_cv(cvop, 0))) { 48492 2070 50562
444 48492 decline:
445 50173 op_free(cvop);
446 #if MUST_RESTORE_PAD_FILL
447 50173 av_fill(PL_comppad, padfill);
448 50173 ▶ Q_PADNAMES_TRIM(PL_comppad_name, namefill);
Branch: 1 branch
BranchTrueFalseTotal
Q_PADNAMES_TRIM(PL_comppad_name, namefill); 0 50173 50173
449 #endif /* MUST_RESTORE_PAD_FILL */
450 50173 return next_keyword_plugin(aTHX_
451 keyword_ptr, keyword_len, op_ptr);
452 }
453 2070 QPFXD(gcp0)(aTHX_ cv, &psfun, &psobj);
454 2070 ▶ if(!psfun && !psobj) goto decline;
Branch: 2 branches
BranchTrueFalseTotal
if(!psfun && !psobj) goto decline; 1681 389 2070
if(!psfun && !psobj) goto decline; 1681 0 1681
455 389 namegv = (GV*)rv2cv_op_cv(cvop,
456 RV2CVOPCV_MARK_EARLY|RV2CVOPCV_RETURN_NAME_GV);
457 389 parser_flags = 0;
458 389 argsop = psfun(aTHX_ namegv, psobj, &parser_flags);
459 358 ▶ if(!(parser_flags & CALLPARSER_PARENS))
Branch: 1 branch
BranchTrueFalseTotal
if(!(parser_flags & CALLPARSER_PARENS)) 206 152 358
460 206 cvop->op_private |= OPpENTERSUB_NOPAREN;
461 358 *op_ptr = newUNOP(OP_ENTERSUB, OPf_STACKED,
462 op_append_elem(OP_LIST, argsop, cvop));
463 358 return (parser_flags & CALLPARSER_STATEMENT) ?
464 358 ▶ KEYWORD_PLUGIN_STMT : KEYWORD_PLUGIN_EXPR;
Branch: 1 branch
BranchTrueFalseTotal
KEYWORD_PLUGIN_STMT : KEYWORD_PLUGIN_EXPR; 8 350 358
465 }
466  
467 #define fmt_header(n, content) THX_fmt_header(aTHX_ n, content)
468 6 static SV *THX_fmt_header(pTHX_ char n, char const *content)
469 {
470 6 return newSVpvf(
471 "/* DO NOT EDIT -- generated "
472 "by Devel::CallParser version "XS_VERSION" */\n"
473 "#ifndef "QPFXS"INCLUDED_callparser%c\n"
474 "#define "QPFXS"INCLUDED_callparser%c 1\n"
475 "#ifndef PERL_VERSION\n"
476 " #error you must include perl.h before callparser%c.h\n"
477 "#elif !(PERL_REVISION == "STRINGIFY(PERL_REVISION)
478 " && PERL_VERSION == "STRINGIFY(PERL_VERSION)
479 #if PERL_VERSION & 1
480 " && PERL_SUBVERSION == "STRINGIFY(PERL_SUBVERSION)
481 #endif /* PERL_VERSION & 1 */
482 ")\n"
483 " #error this callparser%c.h is for Perl "
484 STRINGIFY(PERL_REVISION)"."STRINGIFY(PERL_VERSION)
485 #if PERL_VERSION & 1
486 "."STRINGIFY(PERL_SUBVERSION)
487 #endif /* PERL_VERSION & 1 */
488 " only\n"
489 "#endif /* Perl version mismatch */\n"
490 "%s"
491 "#endif /* !"QPFXS"INCLUDED_callparser%c */\n",
492 n, n, n, n, content, n);
493 }
494  
495 #define DEFFN(RETTYPE, PUBNAME, PRIVNAME, ARGTYPES, ARGNAMES) \
496 MY_IMPORT_CALLCONV_S" "RETTYPE" "QPFXS PRIVNAME"(pTHX_ "ARGTYPES");\n" \
497 "#define Perl_"PUBNAME" "QPFXS PRIVNAME"\n" \
498 "#define "PUBNAME"("ARGNAMES") Perl_"PUBNAME"(aTHX_ "ARGNAMES")\n"
499  
500 #define DEFCALLBACK \
501 "typedef OP *(*Perl_call_parser)(pTHX_ GV *, SV *, U32 *);\n" \
502 "#define CALLPARSER_PARENS 0x00000001\n" \
503 "#define CALLPARSER_STATEMENT 0x00000002\n"
504  
505 MODULE = Devel::CallParser PACKAGE = Devel::CallParser
506  
507 PROTOTYPES: DISABLE
508  
509 BOOT:
510 11 next_keyword_plugin = PL_keyword_plugin;
511 11 PL_keyword_plugin = my_keyword_plugin;
512  
513 SV *
514 callparser0_h()
515 CODE:
516 3 RETVAL = fmt_header('0',
517 DEFCALLBACK
518 DEFFN("void", "cv_get_call_parser", "gcp0",
519 "CV *, Perl_call_parser *, SV **", "cv, fp, op")
520 DEFFN("void", "cv_set_call_parser", "scp0",
521 "CV *, Perl_call_parser, SV *", "cv, f, o")
522 );
523 OUTPUT:
524 RETVAL
525  
526 SV *
527 callparser1_h()
528 CODE:
529 #if Q_PARSER_AVAILABLE
530 3 RETVAL = fmt_header('1',
531 DEFFN("OP *", "parse_args_parenthesised", "pac0", "U32 *", "fp")
532 DEFFN("OP *", "parse_args_nullary", "paz0", "U32 *", "fp")
533 DEFFN("OP *", "parse_args_unary", "pau0", "U32 *", "fp")
534 DEFFN("OP *", "parse_args_list", "pal0", "U32 *", "fp")
535 DEFFN("OP *", "parse_args_block_list", "pab0", "U32 *", "fp")
536 DEFFN("OP *", "parse_args_proto", "pap0",
537 "GV *, SV *, U32 *", "gv, sv, fp")
538 DEFFN("OP *", "parse_args_proto_or_list", "pan0",
539 "GV *, SV *, U32 *", "gv, sv, fp")
540 DEFCALLBACK
541 DEFFN("void", "cv_get_call_parser", "gcp1",
542 "CV *, Perl_call_parser *, SV **", "cv, fp, op")
543 DEFFN("void", "cv_set_call_parser", "scp1",
544 "CV *, Perl_call_parser, SV *", "cv, f, o")
545 );
546 #else /* !Q_PARSER_AVAILABLE */
547 croak("callparser1.h not available on this version of Perl");
548 #endif /* !Q_PARSER_AVAILABLE */
549 OUTPUT:
550 RETVAL
↑ Summary blib/lib/Devel/CallParser.pm »