Mercurial > repo
comparison perl-5.22.2/op.c @ 8045:a16537d2fe07
<xfix> tar xf perl-5.22.2.tar.gz # Ah, whatever, I\'m doing it anyway
author | HackBot |
---|---|
date | Sat, 14 May 2016 14:54:38 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
8044:711c038a7dce | 8045:a16537d2fe07 |
---|---|
1 #line 2 "op.c" | |
2 /* op.c | |
3 * | |
4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, | |
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others | |
6 * | |
7 * You may distribute under the terms of either the GNU General Public | |
8 * License or the Artistic License, as specified in the README file. | |
9 * | |
10 */ | |
11 | |
12 /* | |
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was | |
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the | |
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second | |
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed | |
17 * either way, as the saying is, if you follow me.' --the Gaffer | |
18 * | |
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] | |
20 */ | |
21 | |
22 /* This file contains the functions that create, manipulate and optimize | |
23 * the OP structures that hold a compiled perl program. | |
24 * | |
25 * A Perl program is compiled into a tree of OPs. Each op contains | |
26 * structural pointers (eg to its siblings and the next op in the | |
27 * execution sequence), a pointer to the function that would execute the | |
28 * op, plus any data specific to that op. For example, an OP_CONST op | |
29 * points to the pp_const() function and to an SV containing the constant | |
30 * value. When pp_const() is executed, its job is to push that SV onto the | |
31 * stack. | |
32 * | |
33 * OPs are mainly created by the newFOO() functions, which are mainly | |
34 * called from the parser (in perly.y) as the code is parsed. For example | |
35 * the Perl code $a + $b * $c would cause the equivalent of the following | |
36 * to be called (oversimplifying a bit): | |
37 * | |
38 * newBINOP(OP_ADD, flags, | |
39 * newSVREF($a), | |
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c)) | |
41 * ) | |
42 * | |
43 * Note that during the build of miniperl, a temporary copy of this file | |
44 * is made, called opmini.c. | |
45 */ | |
46 | |
47 /* | |
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases: | |
49 | |
50 A bottom-up pass | |
51 A top-down pass | |
52 An execution-order pass | |
53 | |
54 The bottom-up pass is represented by all the "newOP" routines and | |
55 the ck_ routines. The bottom-upness is actually driven by yacc. | |
56 So at the point that a ck_ routine fires, we have no idea what the | |
57 context is, either upward in the syntax tree, or either forward or | |
58 backward in the execution order. (The bottom-up parser builds that | |
59 part of the execution order it knows about, but if you follow the "next" | |
60 links around, you'll find it's actually a closed loop through the | |
61 top level node.) | |
62 | |
63 Whenever the bottom-up parser gets to a node that supplies context to | |
64 its components, it invokes that portion of the top-down pass that applies | |
65 to that part of the subtree (and marks the top node as processed, so | |
66 if a node further up supplies context, it doesn't have to take the | |
67 plunge again). As a particular subcase of this, as the new node is | |
68 built, it takes all the closed execution loops of its subcomponents | |
69 and links them into a new closed loop for the higher level node. But | |
70 it's still not the real execution order. | |
71 | |
72 The actual execution order is not known till we get a grammar reduction | |
73 to a top-level unit like a subroutine or file that will be called by | |
74 "name" rather than via a "next" pointer. At that point, we can call | |
75 into peep() to do that code's portion of the 3rd pass. It has to be | |
76 recursive, but it's recursive on basic blocks, not on tree nodes. | |
77 */ | |
78 | |
79 /* To implement user lexical pragmas, there needs to be a way at run time to | |
80 get the compile time state of %^H for that block. Storing %^H in every | |
81 block (or even COP) would be very expensive, so a different approach is | |
82 taken. The (running) state of %^H is serialised into a tree of HE-like | |
83 structs. Stores into %^H are chained onto the current leaf as a struct | |
84 refcounted_he * with the key and the value. Deletes from %^H are saved | |
85 with a value of PL_sv_placeholder. The state of %^H at any point can be | |
86 turned back into a regular HV by walking back up the tree from that point's | |
87 leaf, ignoring any key you've already seen (placeholder or not), storing | |
88 the rest into the HV structure, then removing the placeholders. Hence | |
89 memory is only used to store the %^H deltas from the enclosing COP, rather | |
90 than the entire %^H on each COP. | |
91 | |
92 To cause actions on %^H to write out the serialisation records, it has | |
93 magic type 'H'. This magic (itself) does nothing, but its presence causes | |
94 the values to gain magic type 'h', which has entries for set and clear. | |
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store | |
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS> | |
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that | |
98 it will be correctly restored when any inner compiling scope is exited. | |
99 */ | |
100 | |
101 #include "EXTERN.h" | |
102 #define PERL_IN_OP_C | |
103 #include "perl.h" | |
104 #include "keywords.h" | |
105 #include "feature.h" | |
106 #include "regcomp.h" | |
107 | |
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o) | |
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o) | |
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o) | |
111 | |
112 /* Used to avoid recursion through the op tree in scalarvoid() and | |
113 op_free() | |
114 */ | |
115 | |
116 #define DEFERRED_OP_STEP 100 | |
117 #define DEFER_OP(o) \ | |
118 STMT_START { \ | |
119 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \ | |
120 defer_stack_alloc += DEFERRED_OP_STEP; \ | |
121 assert(defer_stack_alloc > 0); \ | |
122 Renew(defer_stack, defer_stack_alloc, OP *); \ | |
123 } \ | |
124 defer_stack[++defer_ix] = o; \ | |
125 } STMT_END | |
126 | |
127 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL) | |
128 | |
129 /* remove any leading "empty" ops from the op_next chain whose first | |
130 * node's address is stored in op_p. Store the updated address of the | |
131 * first node in op_p. | |
132 */ | |
133 | |
134 STATIC void | |
135 S_prune_chain_head(OP** op_p) | |
136 { | |
137 while (*op_p | |
138 && ( (*op_p)->op_type == OP_NULL | |
139 || (*op_p)->op_type == OP_SCOPE | |
140 || (*op_p)->op_type == OP_SCALAR | |
141 || (*op_p)->op_type == OP_LINESEQ) | |
142 ) | |
143 *op_p = (*op_p)->op_next; | |
144 } | |
145 | |
146 | |
147 /* See the explanatory comments above struct opslab in op.h. */ | |
148 | |
149 #ifdef PERL_DEBUG_READONLY_OPS | |
150 # define PERL_SLAB_SIZE 128 | |
151 # define PERL_MAX_SLAB_SIZE 4096 | |
152 # include <sys/mman.h> | |
153 #endif | |
154 | |
155 #ifndef PERL_SLAB_SIZE | |
156 # define PERL_SLAB_SIZE 64 | |
157 #endif | |
158 #ifndef PERL_MAX_SLAB_SIZE | |
159 # define PERL_MAX_SLAB_SIZE 2048 | |
160 #endif | |
161 | |
162 /* rounds up to nearest pointer */ | |
163 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *)) | |
164 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o))) | |
165 | |
166 static OPSLAB * | |
167 S_new_slab(pTHX_ size_t sz) | |
168 { | |
169 #ifdef PERL_DEBUG_READONLY_OPS | |
170 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *), | |
171 PROT_READ|PROT_WRITE, | |
172 MAP_ANON|MAP_PRIVATE, -1, 0); | |
173 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n", | |
174 (unsigned long) sz, slab)); | |
175 if (slab == MAP_FAILED) { | |
176 perror("mmap failed"); | |
177 abort(); | |
178 } | |
179 slab->opslab_size = (U16)sz; | |
180 #else | |
181 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *)); | |
182 #endif | |
183 #ifndef WIN32 | |
184 /* The context is unused in non-Windows */ | |
185 PERL_UNUSED_CONTEXT; | |
186 #endif | |
187 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1); | |
188 return slab; | |
189 } | |
190 | |
191 /* requires double parens and aTHX_ */ | |
192 #define DEBUG_S_warn(args) \ | |
193 DEBUG_S( \ | |
194 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \ | |
195 ) | |
196 | |
197 void * | |
198 Perl_Slab_Alloc(pTHX_ size_t sz) | |
199 { | |
200 OPSLAB *slab; | |
201 OPSLAB *slab2; | |
202 OPSLOT *slot; | |
203 OP *o; | |
204 size_t opsz, space; | |
205 | |
206 /* We only allocate ops from the slab during subroutine compilation. | |
207 We find the slab via PL_compcv, hence that must be non-NULL. It could | |
208 also be pointing to a subroutine which is now fully set up (CvROOT() | |
209 pointing to the top of the optree for that sub), or a subroutine | |
210 which isn't using the slab allocator. If our sanity checks aren't met, | |
211 don't use a slab, but allocate the OP directly from the heap. */ | |
212 if (!PL_compcv || CvROOT(PL_compcv) | |
213 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv))) | |
214 { | |
215 o = (OP*)PerlMemShared_calloc(1, sz); | |
216 goto gotit; | |
217 } | |
218 | |
219 /* While the subroutine is under construction, the slabs are accessed via | |
220 CvSTART(), to avoid needing to expand PVCV by one pointer for something | |
221 unneeded at runtime. Once a subroutine is constructed, the slabs are | |
222 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been | |
223 allocated yet. See the commit message for 8be227ab5eaa23f2 for more | |
224 details. */ | |
225 if (!CvSTART(PL_compcv)) { | |
226 CvSTART(PL_compcv) = | |
227 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE)); | |
228 CvSLABBED_on(PL_compcv); | |
229 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */ | |
230 } | |
231 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt; | |
232 | |
233 opsz = SIZE_TO_PSIZE(sz); | |
234 sz = opsz + OPSLOT_HEADER_P; | |
235 | |
236 /* The slabs maintain a free list of OPs. In particular, constant folding | |
237 will free up OPs, so it makes sense to re-use them where possible. A | |
238 freed up slot is used in preference to a new allocation. */ | |
239 if (slab->opslab_freed) { | |
240 OP **too = &slab->opslab_freed; | |
241 o = *too; | |
242 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab)); | |
243 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) { | |
244 DEBUG_S_warn((aTHX_ "Alas! too small")); | |
245 o = *(too = &o->op_next); | |
246 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); } | |
247 } | |
248 if (o) { | |
249 *too = o->op_next; | |
250 Zero(o, opsz, I32 *); | |
251 o->op_slabbed = 1; | |
252 goto gotit; | |
253 } | |
254 } | |
255 | |
256 #define INIT_OPSLOT \ | |
257 slot->opslot_slab = slab; \ | |
258 slot->opslot_next = slab2->opslab_first; \ | |
259 slab2->opslab_first = slot; \ | |
260 o = &slot->opslot_op; \ | |
261 o->op_slabbed = 1 | |
262 | |
263 /* The partially-filled slab is next in the chain. */ | |
264 slab2 = slab->opslab_next ? slab->opslab_next : slab; | |
265 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) { | |
266 /* Remaining space is too small. */ | |
267 | |
268 /* If we can fit a BASEOP, add it to the free chain, so as not | |
269 to waste it. */ | |
270 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) { | |
271 slot = &slab2->opslab_slots; | |
272 INIT_OPSLOT; | |
273 o->op_type = OP_FREED; | |
274 o->op_next = slab->opslab_freed; | |
275 slab->opslab_freed = o; | |
276 } | |
277 | |
278 /* Create a new slab. Make this one twice as big. */ | |
279 slot = slab2->opslab_first; | |
280 while (slot->opslot_next) slot = slot->opslot_next; | |
281 slab2 = S_new_slab(aTHX_ | |
282 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE | |
283 ? PERL_MAX_SLAB_SIZE | |
284 : (DIFF(slab2, slot)+1)*2); | |
285 slab2->opslab_next = slab->opslab_next; | |
286 slab->opslab_next = slab2; | |
287 } | |
288 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz); | |
289 | |
290 /* Create a new op slot */ | |
291 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz); | |
292 assert(slot >= &slab2->opslab_slots); | |
293 if (DIFF(&slab2->opslab_slots, slot) | |
294 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) | |
295 slot = &slab2->opslab_slots; | |
296 INIT_OPSLOT; | |
297 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab)); | |
298 | |
299 gotit: | |
300 #ifdef PERL_OP_PARENT | |
301 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */ | |
302 assert(!o->op_moresib); | |
303 assert(!o->op_sibparent); | |
304 #endif | |
305 | |
306 return (void *)o; | |
307 } | |
308 | |
309 #undef INIT_OPSLOT | |
310 | |
311 #ifdef PERL_DEBUG_READONLY_OPS | |
312 void | |
313 Perl_Slab_to_ro(pTHX_ OPSLAB *slab) | |
314 { | |
315 PERL_ARGS_ASSERT_SLAB_TO_RO; | |
316 | |
317 if (slab->opslab_readonly) return; | |
318 slab->opslab_readonly = 1; | |
319 for (; slab; slab = slab->opslab_next) { | |
320 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n", | |
321 (unsigned long) slab->opslab_size, slab));*/ | |
322 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ)) | |
323 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab, | |
324 (unsigned long)slab->opslab_size, errno); | |
325 } | |
326 } | |
327 | |
328 void | |
329 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab) | |
330 { | |
331 OPSLAB *slab2; | |
332 | |
333 PERL_ARGS_ASSERT_SLAB_TO_RW; | |
334 | |
335 if (!slab->opslab_readonly) return; | |
336 slab2 = slab; | |
337 for (; slab2; slab2 = slab2->opslab_next) { | |
338 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n", | |
339 (unsigned long) size, slab2));*/ | |
340 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *), | |
341 PROT_READ|PROT_WRITE)) { | |
342 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab, | |
343 (unsigned long)slab2->opslab_size, errno); | |
344 } | |
345 } | |
346 slab->opslab_readonly = 0; | |
347 } | |
348 | |
349 #else | |
350 # define Slab_to_rw(op) NOOP | |
351 #endif | |
352 | |
353 /* This cannot possibly be right, but it was copied from the old slab | |
354 allocator, to which it was originally added, without explanation, in | |
355 commit 083fcd5. */ | |
356 #ifdef NETWARE | |
357 # define PerlMemShared PerlMem | |
358 #endif | |
359 | |
360 void | |
361 Perl_Slab_Free(pTHX_ void *op) | |
362 { | |
363 OP * const o = (OP *)op; | |
364 OPSLAB *slab; | |
365 | |
366 PERL_ARGS_ASSERT_SLAB_FREE; | |
367 | |
368 if (!o->op_slabbed) { | |
369 if (!o->op_static) | |
370 PerlMemShared_free(op); | |
371 return; | |
372 } | |
373 | |
374 slab = OpSLAB(o); | |
375 /* If this op is already freed, our refcount will get screwy. */ | |
376 assert(o->op_type != OP_FREED); | |
377 o->op_type = OP_FREED; | |
378 o->op_next = slab->opslab_freed; | |
379 slab->opslab_freed = o; | |
380 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab)); | |
381 OpslabREFCNT_dec_padok(slab); | |
382 } | |
383 | |
384 void | |
385 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab) | |
386 { | |
387 const bool havepad = !!PL_comppad; | |
388 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD; | |
389 if (havepad) { | |
390 ENTER; | |
391 PAD_SAVE_SETNULLPAD(); | |
392 } | |
393 opslab_free(slab); | |
394 if (havepad) LEAVE; | |
395 } | |
396 | |
397 void | |
398 Perl_opslab_free(pTHX_ OPSLAB *slab) | |
399 { | |
400 OPSLAB *slab2; | |
401 PERL_ARGS_ASSERT_OPSLAB_FREE; | |
402 PERL_UNUSED_CONTEXT; | |
403 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab)); | |
404 assert(slab->opslab_refcnt == 1); | |
405 do { | |
406 slab2 = slab->opslab_next; | |
407 #ifdef DEBUGGING | |
408 slab->opslab_refcnt = ~(size_t)0; | |
409 #endif | |
410 #ifdef PERL_DEBUG_READONLY_OPS | |
411 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n", | |
412 (void*)slab)); | |
413 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) { | |
414 perror("munmap failed"); | |
415 abort(); | |
416 } | |
417 #else | |
418 PerlMemShared_free(slab); | |
419 #endif | |
420 slab = slab2; | |
421 } while (slab); | |
422 } | |
423 | |
424 void | |
425 Perl_opslab_force_free(pTHX_ OPSLAB *slab) | |
426 { | |
427 OPSLAB *slab2; | |
428 OPSLOT *slot; | |
429 #ifdef DEBUGGING | |
430 size_t savestack_count = 0; | |
431 #endif | |
432 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE; | |
433 slab2 = slab; | |
434 do { | |
435 for (slot = slab2->opslab_first; | |
436 slot->opslot_next; | |
437 slot = slot->opslot_next) { | |
438 if (slot->opslot_op.op_type != OP_FREED | |
439 && !(slot->opslot_op.op_savefree | |
440 #ifdef DEBUGGING | |
441 && ++savestack_count | |
442 #endif | |
443 ) | |
444 ) { | |
445 assert(slot->opslot_op.op_slabbed); | |
446 op_free(&slot->opslot_op); | |
447 if (slab->opslab_refcnt == 1) goto free; | |
448 } | |
449 } | |
450 } while ((slab2 = slab2->opslab_next)); | |
451 /* > 1 because the CV still holds a reference count. */ | |
452 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */ | |
453 #ifdef DEBUGGING | |
454 assert(savestack_count == slab->opslab_refcnt-1); | |
455 #endif | |
456 /* Remove the CV’s reference count. */ | |
457 slab->opslab_refcnt--; | |
458 return; | |
459 } | |
460 free: | |
461 opslab_free(slab); | |
462 } | |
463 | |
464 #ifdef PERL_DEBUG_READONLY_OPS | |
465 OP * | |
466 Perl_op_refcnt_inc(pTHX_ OP *o) | |
467 { | |
468 if(o) { | |
469 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL; | |
470 if (slab && slab->opslab_readonly) { | |
471 Slab_to_rw(slab); | |
472 ++o->op_targ; | |
473 Slab_to_ro(slab); | |
474 } else { | |
475 ++o->op_targ; | |
476 } | |
477 } | |
478 return o; | |
479 | |
480 } | |
481 | |
482 PADOFFSET | |
483 Perl_op_refcnt_dec(pTHX_ OP *o) | |
484 { | |
485 PADOFFSET result; | |
486 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL; | |
487 | |
488 PERL_ARGS_ASSERT_OP_REFCNT_DEC; | |
489 | |
490 if (slab && slab->opslab_readonly) { | |
491 Slab_to_rw(slab); | |
492 result = --o->op_targ; | |
493 Slab_to_ro(slab); | |
494 } else { | |
495 result = --o->op_targ; | |
496 } | |
497 return result; | |
498 } | |
499 #endif | |
500 /* | |
501 * In the following definition, the ", (OP*)0" is just to make the compiler | |
502 * think the expression is of the right type: croak actually does a Siglongjmp. | |
503 */ | |
504 #define CHECKOP(type,o) \ | |
505 ((PL_op_mask && PL_op_mask[type]) \ | |
506 ? ( op_free((OP*)o), \ | |
507 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \ | |
508 (OP*)0 ) \ | |
509 : PL_check[type](aTHX_ (OP*)o)) | |
510 | |
511 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2) | |
512 | |
513 #define OpTYPE_set(o,type) \ | |
514 STMT_START { \ | |
515 o->op_type = (OPCODE)type; \ | |
516 o->op_ppaddr = PL_ppaddr[type]; \ | |
517 } STMT_END | |
518 | |
519 STATIC OP * | |
520 S_no_fh_allowed(pTHX_ OP *o) | |
521 { | |
522 PERL_ARGS_ASSERT_NO_FH_ALLOWED; | |
523 | |
524 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function", | |
525 OP_DESC(o))); | |
526 return o; | |
527 } | |
528 | |
529 STATIC OP * | |
530 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags) | |
531 { | |
532 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV; | |
533 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags); | |
534 return o; | |
535 } | |
536 | |
537 STATIC OP * | |
538 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags) | |
539 { | |
540 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV; | |
541 | |
542 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags); | |
543 return o; | |
544 } | |
545 | |
546 STATIC void | |
547 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid) | |
548 { | |
549 PERL_ARGS_ASSERT_BAD_TYPE_PV; | |
550 | |
551 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)", | |
552 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0); | |
553 } | |
554 | |
555 /* remove flags var, its unused in all callers, move to to right end since gv | |
556 and kid are always the same */ | |
557 STATIC void | |
558 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t) | |
559 { | |
560 SV * const namesv = cv_name((CV *)gv, NULL, 0); | |
561 PERL_ARGS_ASSERT_BAD_TYPE_GV; | |
562 | |
563 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)", | |
564 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv)); | |
565 } | |
566 | |
567 STATIC void | |
568 S_no_bareword_allowed(pTHX_ OP *o) | |
569 { | |
570 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED; | |
571 | |
572 qerror(Perl_mess(aTHX_ | |
573 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use", | |
574 SVfARG(cSVOPo_sv))); | |
575 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */ | |
576 } | |
577 | |
578 /* "register" allocation */ | |
579 | |
580 PADOFFSET | |
581 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags) | |
582 { | |
583 PADOFFSET off; | |
584 const bool is_our = (PL_parser->in_my == KEY_our); | |
585 | |
586 PERL_ARGS_ASSERT_ALLOCMY; | |
587 | |
588 if (flags & ~SVf_UTF8) | |
589 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf, | |
590 (UV)flags); | |
591 | |
592 /* complain about "my $<special_var>" etc etc */ | |
593 if (len && | |
594 !(is_our || | |
595 isALPHA(name[1]) || | |
596 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) || | |
597 (name[1] == '_' && (*name == '$' || len > 2)))) | |
598 { | |
599 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1])) | |
600 && isASCII(name[1]) | |
601 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) { | |
602 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"", | |
603 name[0], toCTRL(name[1]), (int)(len - 2), name + 2, | |
604 PL_parser->in_my == KEY_state ? "state" : "my")); | |
605 } else { | |
606 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name, | |
607 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8); | |
608 } | |
609 } | |
610 else if (len == 2 && name[1] == '_' && !is_our) | |
611 /* diag_listed_as: Use of my $_ is experimental */ | |
612 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC), | |
613 "Use of %s $_ is experimental", | |
614 PL_parser->in_my == KEY_state | |
615 ? "state" | |
616 : "my"); | |
617 | |
618 /* allocate a spare slot and store the name in that slot */ | |
619 | |
620 off = pad_add_name_pvn(name, len, | |
621 (is_our ? padadd_OUR : | |
622 PL_parser->in_my == KEY_state ? padadd_STATE : 0), | |
623 PL_parser->in_my_stash, | |
624 (is_our | |
625 /* $_ is always in main::, even with our */ | |
626 ? (PL_curstash && !memEQs(name,len,"$_") | |
627 ? PL_curstash | |
628 : PL_defstash) | |
629 : NULL | |
630 ) | |
631 ); | |
632 /* anon sub prototypes contains state vars should always be cloned, | |
633 * otherwise the state var would be shared between anon subs */ | |
634 | |
635 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv)) | |
636 CvCLONE_on(PL_compcv); | |
637 | |
638 return off; | |
639 } | |
640 | |
641 /* | |
642 =head1 Optree Manipulation Functions | |
643 | |
644 =for apidoc alloccopstash | |
645 | |
646 Available only under threaded builds, this function allocates an entry in | |
647 C<PL_stashpad> for the stash passed to it. | |
648 | |
649 =cut | |
650 */ | |
651 | |
652 #ifdef USE_ITHREADS | |
653 PADOFFSET | |
654 Perl_alloccopstash(pTHX_ HV *hv) | |
655 { | |
656 PADOFFSET off = 0, o = 1; | |
657 bool found_slot = FALSE; | |
658 | |
659 PERL_ARGS_ASSERT_ALLOCCOPSTASH; | |
660 | |
661 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix; | |
662 | |
663 for (; o < PL_stashpadmax; ++o) { | |
664 if (PL_stashpad[o] == hv) return PL_stashpadix = o; | |
665 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV) | |
666 found_slot = TRUE, off = o; | |
667 } | |
668 if (!found_slot) { | |
669 Renew(PL_stashpad, PL_stashpadmax + 10, HV *); | |
670 Zero(PL_stashpad + PL_stashpadmax, 10, HV *); | |
671 off = PL_stashpadmax; | |
672 PL_stashpadmax += 10; | |
673 } | |
674 | |
675 PL_stashpad[PL_stashpadix = off] = hv; | |
676 return off; | |
677 } | |
678 #endif | |
679 | |
680 /* free the body of an op without examining its contents. | |
681 * Always use this rather than FreeOp directly */ | |
682 | |
683 static void | |
684 S_op_destroy(pTHX_ OP *o) | |
685 { | |
686 FreeOp(o); | |
687 } | |
688 | |
689 /* Destructor */ | |
690 | |
691 /* | |
692 =for apidoc Am|void|op_free|OP *o | |
693 | |
694 Free an op. Only use this when an op is no longer linked to from any | |
695 optree. | |
696 | |
697 =cut | |
698 */ | |
699 | |
700 void | |
701 Perl_op_free(pTHX_ OP *o) | |
702 { | |
703 dVAR; | |
704 OPCODE type; | |
705 SSize_t defer_ix = -1; | |
706 SSize_t defer_stack_alloc = 0; | |
707 OP **defer_stack = NULL; | |
708 | |
709 do { | |
710 | |
711 /* Though ops may be freed twice, freeing the op after its slab is a | |
712 big no-no. */ | |
713 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0); | |
714 /* During the forced freeing of ops after compilation failure, kidops | |
715 may be freed before their parents. */ | |
716 if (!o || o->op_type == OP_FREED) | |
717 continue; | |
718 | |
719 type = o->op_type; | |
720 | |
721 /* an op should only ever acquire op_private flags that we know about. | |
722 * If this fails, you may need to fix something in regen/op_private. | |
723 * Don't bother testing if: | |
724 * * the op_ppaddr doesn't match the op; someone may have | |
725 * overridden the op and be doing strange things with it; | |
726 * * we've errored, as op flags are often left in an | |
727 * inconsistent state then. Note that an error when | |
728 * compiling the main program leaves PL_parser NULL, so | |
729 * we can't spot faults in the main code, onoly | |
730 * evaled/required code */ | |
731 #ifdef DEBUGGING | |
732 if ( o->op_ppaddr == PL_ppaddr[o->op_type] | |
733 && PL_parser | |
734 && !PL_parser->error_count) | |
735 { | |
736 assert(!(o->op_private & ~PL_op_private_valid[type])); | |
737 } | |
738 #endif | |
739 | |
740 if (o->op_private & OPpREFCOUNTED) { | |
741 switch (type) { | |
742 case OP_LEAVESUB: | |
743 case OP_LEAVESUBLV: | |
744 case OP_LEAVEEVAL: | |
745 case OP_LEAVE: | |
746 case OP_SCOPE: | |
747 case OP_LEAVEWRITE: | |
748 { | |
749 PADOFFSET refcnt; | |
750 OP_REFCNT_LOCK; | |
751 refcnt = OpREFCNT_dec(o); | |
752 OP_REFCNT_UNLOCK; | |
753 if (refcnt) { | |
754 /* Need to find and remove any pattern match ops from the list | |
755 we maintain for reset(). */ | |
756 find_and_forget_pmops(o); | |
757 continue; | |
758 } | |
759 } | |
760 break; | |
761 default: | |
762 break; | |
763 } | |
764 } | |
765 | |
766 /* Call the op_free hook if it has been set. Do it now so that it's called | |
767 * at the right time for refcounted ops, but still before all of the kids | |
768 * are freed. */ | |
769 CALL_OPFREEHOOK(o); | |
770 | |
771 if (o->op_flags & OPf_KIDS) { | |
772 OP *kid, *nextkid; | |
773 for (kid = cUNOPo->op_first; kid; kid = nextkid) { | |
774 nextkid = OpSIBLING(kid); /* Get before next freeing kid */ | |
775 if (!kid || kid->op_type == OP_FREED) | |
776 /* During the forced freeing of ops after | |
777 compilation failure, kidops may be freed before | |
778 their parents. */ | |
779 continue; | |
780 if (!(kid->op_flags & OPf_KIDS)) | |
781 /* If it has no kids, just free it now */ | |
782 op_free(kid); | |
783 else | |
784 DEFER_OP(kid); | |
785 } | |
786 } | |
787 if (type == OP_NULL) | |
788 type = (OPCODE)o->op_targ; | |
789 | |
790 if (o->op_slabbed) | |
791 Slab_to_rw(OpSLAB(o)); | |
792 | |
793 /* COP* is not cleared by op_clear() so that we may track line | |
794 * numbers etc even after null() */ | |
795 if (type == OP_NEXTSTATE || type == OP_DBSTATE) { | |
796 cop_free((COP*)o); | |
797 } | |
798 | |
799 op_clear(o); | |
800 FreeOp(o); | |
801 #ifdef DEBUG_LEAKING_SCALARS | |
802 if (PL_op == o) | |
803 PL_op = NULL; | |
804 #endif | |
805 } while ( (o = POP_DEFERRED_OP()) ); | |
806 | |
807 Safefree(defer_stack); | |
808 } | |
809 | |
810 /* S_op_clear_gv(): free a GV attached to an OP */ | |
811 | |
812 #ifdef USE_ITHREADS | |
813 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp) | |
814 #else | |
815 void S_op_clear_gv(pTHX_ OP *o, SV**svp) | |
816 #endif | |
817 { | |
818 | |
819 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV | |
820 || o->op_type == OP_MULTIDEREF) | |
821 #ifdef USE_ITHREADS | |
822 && PL_curpad | |
823 ? ((GV*)PAD_SVl(*ixp)) : NULL; | |
824 #else | |
825 ? (GV*)(*svp) : NULL; | |
826 #endif | |
827 /* It's possible during global destruction that the GV is freed | |
828 before the optree. Whilst the SvREFCNT_inc is happy to bump from | |
829 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0 | |
830 will trigger an assertion failure, because the entry to sv_clear | |
831 checks that the scalar is not already freed. A check of for | |
832 !SvIS_FREED(gv) turns out to be invalid, because during global | |
833 destruction the reference count can be forced down to zero | |
834 (with SVf_BREAK set). In which case raising to 1 and then | |
835 dropping to 0 triggers cleanup before it should happen. I | |
836 *think* that this might actually be a general, systematic, | |
837 weakness of the whole idea of SVf_BREAK, in that code *is* | |
838 allowed to raise and lower references during global destruction, | |
839 so any *valid* code that happens to do this during global | |
840 destruction might well trigger premature cleanup. */ | |
841 bool still_valid = gv && SvREFCNT(gv); | |
842 | |
843 if (still_valid) | |
844 SvREFCNT_inc_simple_void(gv); | |
845 #ifdef USE_ITHREADS | |
846 if (*ixp > 0) { | |
847 pad_swipe(*ixp, TRUE); | |
848 *ixp = 0; | |
849 } | |
850 #else | |
851 SvREFCNT_dec(*svp); | |
852 *svp = NULL; | |
853 #endif | |
854 if (still_valid) { | |
855 int try_downgrade = SvREFCNT(gv) == 2; | |
856 SvREFCNT_dec_NN(gv); | |
857 if (try_downgrade) | |
858 gv_try_downgrade(gv); | |
859 } | |
860 } | |
861 | |
862 | |
863 void | |
864 Perl_op_clear(pTHX_ OP *o) | |
865 { | |
866 | |
867 dVAR; | |
868 | |
869 PERL_ARGS_ASSERT_OP_CLEAR; | |
870 | |
871 switch (o->op_type) { | |
872 case OP_NULL: /* Was holding old type, if any. */ | |
873 /* FALLTHROUGH */ | |
874 case OP_ENTERTRY: | |
875 case OP_ENTEREVAL: /* Was holding hints. */ | |
876 o->op_targ = 0; | |
877 break; | |
878 default: | |
879 if (!(o->op_flags & OPf_REF) | |
880 || (PL_check[o->op_type] != Perl_ck_ftst)) | |
881 break; | |
882 /* FALLTHROUGH */ | |
883 case OP_GVSV: | |
884 case OP_GV: | |
885 case OP_AELEMFAST: | |
886 #ifdef USE_ITHREADS | |
887 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix)); | |
888 #else | |
889 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv)); | |
890 #endif | |
891 break; | |
892 case OP_METHOD_REDIR: | |
893 case OP_METHOD_REDIR_SUPER: | |
894 #ifdef USE_ITHREADS | |
895 if (cMETHOPx(o)->op_rclass_targ) { | |
896 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1); | |
897 cMETHOPx(o)->op_rclass_targ = 0; | |
898 } | |
899 #else | |
900 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv); | |
901 cMETHOPx(o)->op_rclass_sv = NULL; | |
902 #endif | |
903 case OP_METHOD_NAMED: | |
904 case OP_METHOD_SUPER: | |
905 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv); | |
906 cMETHOPx(o)->op_u.op_meth_sv = NULL; | |
907 #ifdef USE_ITHREADS | |
908 if (o->op_targ) { | |
909 pad_swipe(o->op_targ, 1); | |
910 o->op_targ = 0; | |
911 } | |
912 #endif | |
913 break; | |
914 case OP_CONST: | |
915 case OP_HINTSEVAL: | |
916 SvREFCNT_dec(cSVOPo->op_sv); | |
917 cSVOPo->op_sv = NULL; | |
918 #ifdef USE_ITHREADS | |
919 /** Bug #15654 | |
920 Even if op_clear does a pad_free for the target of the op, | |
921 pad_free doesn't actually remove the sv that exists in the pad; | |
922 instead it lives on. This results in that it could be reused as | |
923 a target later on when the pad was reallocated. | |
924 **/ | |
925 if(o->op_targ) { | |
926 pad_swipe(o->op_targ,1); | |
927 o->op_targ = 0; | |
928 } | |
929 #endif | |
930 break; | |
931 case OP_DUMP: | |
932 case OP_GOTO: | |
933 case OP_NEXT: | |
934 case OP_LAST: | |
935 case OP_REDO: | |
936 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) | |
937 break; | |
938 /* FALLTHROUGH */ | |
939 case OP_TRANS: | |
940 case OP_TRANSR: | |
941 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { | |
942 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR); | |
943 #ifdef USE_ITHREADS | |
944 if (cPADOPo->op_padix > 0) { | |
945 pad_swipe(cPADOPo->op_padix, TRUE); | |
946 cPADOPo->op_padix = 0; | |
947 } | |
948 #else | |
949 SvREFCNT_dec(cSVOPo->op_sv); | |
950 cSVOPo->op_sv = NULL; | |
951 #endif | |
952 } | |
953 else { | |
954 PerlMemShared_free(cPVOPo->op_pv); | |
955 cPVOPo->op_pv = NULL; | |
956 } | |
957 break; | |
958 case OP_SUBST: | |
959 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot); | |
960 goto clear_pmop; | |
961 case OP_PUSHRE: | |
962 #ifdef USE_ITHREADS | |
963 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) { | |
964 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE); | |
965 } | |
966 #else | |
967 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv)); | |
968 #endif | |
969 /* FALLTHROUGH */ | |
970 case OP_MATCH: | |
971 case OP_QR: | |
972 clear_pmop: | |
973 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE)) | |
974 op_free(cPMOPo->op_code_list); | |
975 cPMOPo->op_code_list = NULL; | |
976 forget_pmop(cPMOPo); | |
977 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL; | |
978 /* we use the same protection as the "SAFE" version of the PM_ macros | |
979 * here since sv_clean_all might release some PMOPs | |
980 * after PL_regex_padav has been cleared | |
981 * and the clearing of PL_regex_padav needs to | |
982 * happen before sv_clean_all | |
983 */ | |
984 #ifdef USE_ITHREADS | |
985 if(PL_regex_pad) { /* We could be in destruction */ | |
986 const IV offset = (cPMOPo)->op_pmoffset; | |
987 ReREFCNT_dec(PM_GETRE(cPMOPo)); | |
988 PL_regex_pad[offset] = &PL_sv_undef; | |
989 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset, | |
990 sizeof(offset)); | |
991 } | |
992 #else | |
993 ReREFCNT_dec(PM_GETRE(cPMOPo)); | |
994 PM_SETRE(cPMOPo, NULL); | |
995 #endif | |
996 | |
997 break; | |
998 | |
999 case OP_MULTIDEREF: | |
1000 { | |
1001 UNOP_AUX_item *items = cUNOP_AUXo->op_aux; | |
1002 UV actions = items->uv; | |
1003 bool last = 0; | |
1004 bool is_hash = FALSE; | |
1005 | |
1006 while (!last) { | |
1007 switch (actions & MDEREF_ACTION_MASK) { | |
1008 | |
1009 case MDEREF_reload: | |
1010 actions = (++items)->uv; | |
1011 continue; | |
1012 | |
1013 case MDEREF_HV_padhv_helem: | |
1014 is_hash = TRUE; | |
1015 case MDEREF_AV_padav_aelem: | |
1016 pad_free((++items)->pad_offset); | |
1017 goto do_elem; | |
1018 | |
1019 case MDEREF_HV_gvhv_helem: | |
1020 is_hash = TRUE; | |
1021 case MDEREF_AV_gvav_aelem: | |
1022 #ifdef USE_ITHREADS | |
1023 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); | |
1024 #else | |
1025 S_op_clear_gv(aTHX_ o, &((++items)->sv)); | |
1026 #endif | |
1027 goto do_elem; | |
1028 | |
1029 case MDEREF_HV_gvsv_vivify_rv2hv_helem: | |
1030 is_hash = TRUE; | |
1031 case MDEREF_AV_gvsv_vivify_rv2av_aelem: | |
1032 #ifdef USE_ITHREADS | |
1033 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); | |
1034 #else | |
1035 S_op_clear_gv(aTHX_ o, &((++items)->sv)); | |
1036 #endif | |
1037 goto do_vivify_rv2xv_elem; | |
1038 | |
1039 case MDEREF_HV_padsv_vivify_rv2hv_helem: | |
1040 is_hash = TRUE; | |
1041 case MDEREF_AV_padsv_vivify_rv2av_aelem: | |
1042 pad_free((++items)->pad_offset); | |
1043 goto do_vivify_rv2xv_elem; | |
1044 | |
1045 case MDEREF_HV_pop_rv2hv_helem: | |
1046 case MDEREF_HV_vivify_rv2hv_helem: | |
1047 is_hash = TRUE; | |
1048 do_vivify_rv2xv_elem: | |
1049 case MDEREF_AV_pop_rv2av_aelem: | |
1050 case MDEREF_AV_vivify_rv2av_aelem: | |
1051 do_elem: | |
1052 switch (actions & MDEREF_INDEX_MASK) { | |
1053 case MDEREF_INDEX_none: | |
1054 last = 1; | |
1055 break; | |
1056 case MDEREF_INDEX_const: | |
1057 if (is_hash) { | |
1058 #ifdef USE_ITHREADS | |
1059 /* see RT #15654 */ | |
1060 pad_swipe((++items)->pad_offset, 1); | |
1061 #else | |
1062 SvREFCNT_dec((++items)->sv); | |
1063 #endif | |
1064 } | |
1065 else | |
1066 items++; | |
1067 break; | |
1068 case MDEREF_INDEX_padsv: | |
1069 pad_free((++items)->pad_offset); | |
1070 break; | |
1071 case MDEREF_INDEX_gvsv: | |
1072 #ifdef USE_ITHREADS | |
1073 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); | |
1074 #else | |
1075 S_op_clear_gv(aTHX_ o, &((++items)->sv)); | |
1076 #endif | |
1077 break; | |
1078 } | |
1079 | |
1080 if (actions & MDEREF_FLAG_last) | |
1081 last = 1; | |
1082 is_hash = FALSE; | |
1083 | |
1084 break; | |
1085 | |
1086 default: | |
1087 assert(0); | |
1088 last = 1; | |
1089 break; | |
1090 | |
1091 } /* switch */ | |
1092 | |
1093 actions >>= MDEREF_SHIFT; | |
1094 } /* while */ | |
1095 | |
1096 /* start of malloc is at op_aux[-1], where the length is | |
1097 * stored */ | |
1098 PerlMemShared_free(cUNOP_AUXo->op_aux - 1); | |
1099 } | |
1100 break; | |
1101 } | |
1102 | |
1103 if (o->op_targ > 0) { | |
1104 pad_free(o->op_targ); | |
1105 o->op_targ = 0; | |
1106 } | |
1107 } | |
1108 | |
1109 STATIC void | |
1110 S_cop_free(pTHX_ COP* cop) | |
1111 { | |
1112 PERL_ARGS_ASSERT_COP_FREE; | |
1113 | |
1114 CopFILE_free(cop); | |
1115 if (! specialWARN(cop->cop_warnings)) | |
1116 PerlMemShared_free(cop->cop_warnings); | |
1117 cophh_free(CopHINTHASH_get(cop)); | |
1118 if (PL_curcop == cop) | |
1119 PL_curcop = NULL; | |
1120 } | |
1121 | |
1122 STATIC void | |
1123 S_forget_pmop(pTHX_ PMOP *const o | |
1124 ) | |
1125 { | |
1126 HV * const pmstash = PmopSTASH(o); | |
1127 | |
1128 PERL_ARGS_ASSERT_FORGET_PMOP; | |
1129 | |
1130 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) { | |
1131 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab); | |
1132 if (mg) { | |
1133 PMOP **const array = (PMOP**) mg->mg_ptr; | |
1134 U32 count = mg->mg_len / sizeof(PMOP**); | |
1135 U32 i = count; | |
1136 | |
1137 while (i--) { | |
1138 if (array[i] == o) { | |
1139 /* Found it. Move the entry at the end to overwrite it. */ | |
1140 array[i] = array[--count]; | |
1141 mg->mg_len = count * sizeof(PMOP**); | |
1142 /* Could realloc smaller at this point always, but probably | |
1143 not worth it. Probably worth free()ing if we're the | |
1144 last. */ | |
1145 if(!count) { | |
1146 Safefree(mg->mg_ptr); | |
1147 mg->mg_ptr = NULL; | |
1148 } | |
1149 break; | |
1150 } | |
1151 } | |
1152 } | |
1153 } | |
1154 if (PL_curpm == o) | |
1155 PL_curpm = NULL; | |
1156 } | |
1157 | |
1158 STATIC void | |
1159 S_find_and_forget_pmops(pTHX_ OP *o) | |
1160 { | |
1161 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS; | |
1162 | |
1163 if (o->op_flags & OPf_KIDS) { | |
1164 OP *kid = cUNOPo->op_first; | |
1165 while (kid) { | |
1166 switch (kid->op_type) { | |
1167 case OP_SUBST: | |
1168 case OP_PUSHRE: | |
1169 case OP_MATCH: | |
1170 case OP_QR: | |
1171 forget_pmop((PMOP*)kid); | |
1172 } | |
1173 find_and_forget_pmops(kid); | |
1174 kid = OpSIBLING(kid); | |
1175 } | |
1176 } | |
1177 } | |
1178 | |
1179 /* | |
1180 =for apidoc Am|void|op_null|OP *o | |
1181 | |
1182 Neutralizes an op when it is no longer needed, but is still linked to from | |
1183 other ops. | |
1184 | |
1185 =cut | |
1186 */ | |
1187 | |
1188 void | |
1189 Perl_op_null(pTHX_ OP *o) | |
1190 { | |
1191 dVAR; | |
1192 | |
1193 PERL_ARGS_ASSERT_OP_NULL; | |
1194 | |
1195 if (o->op_type == OP_NULL) | |
1196 return; | |
1197 op_clear(o); | |
1198 o->op_targ = o->op_type; | |
1199 OpTYPE_set(o, OP_NULL); | |
1200 } | |
1201 | |
1202 void | |
1203 Perl_op_refcnt_lock(pTHX) | |
1204 { | |
1205 #ifdef USE_ITHREADS | |
1206 dVAR; | |
1207 #endif | |
1208 PERL_UNUSED_CONTEXT; | |
1209 OP_REFCNT_LOCK; | |
1210 } | |
1211 | |
1212 void | |
1213 Perl_op_refcnt_unlock(pTHX) | |
1214 { | |
1215 #ifdef USE_ITHREADS | |
1216 dVAR; | |
1217 #endif | |
1218 PERL_UNUSED_CONTEXT; | |
1219 OP_REFCNT_UNLOCK; | |
1220 } | |
1221 | |
1222 | |
1223 /* | |
1224 =for apidoc op_sibling_splice | |
1225 | |
1226 A general function for editing the structure of an existing chain of | |
1227 op_sibling nodes. By analogy with the perl-level splice() function, allows | |
1228 you to delete zero or more sequential nodes, replacing them with zero or | |
1229 more different nodes. Performs the necessary op_first/op_last | |
1230 housekeeping on the parent node and op_sibling manipulation on the | |
1231 children. The last deleted node will be marked as as the last node by | |
1232 updating the op_sibling/op_sibparent or op_moresib field as appropriate. | |
1233 | |
1234 Note that op_next is not manipulated, and nodes are not freed; that is the | |
1235 responsibility of the caller. It also won't create a new list op for an | |
1236 empty list etc; use higher-level functions like op_append_elem() for that. | |
1237 | |
1238 parent is the parent node of the sibling chain. It may passed as NULL if | |
1239 the splicing doesn't affect the first or last op in the chain. | |
1240 | |
1241 start is the node preceding the first node to be spliced. Node(s) | |
1242 following it will be deleted, and ops will be inserted after it. If it is | |
1243 NULL, the first node onwards is deleted, and nodes are inserted at the | |
1244 beginning. | |
1245 | |
1246 del_count is the number of nodes to delete. If zero, no nodes are deleted. | |
1247 If -1 or greater than or equal to the number of remaining kids, all | |
1248 remaining kids are deleted. | |
1249 | |
1250 insert is the first of a chain of nodes to be inserted in place of the nodes. | |
1251 If NULL, no nodes are inserted. | |
1252 | |
1253 The head of the chain of deleted ops is returned, or NULL if no ops were | |
1254 deleted. | |
1255 | |
1256 For example: | |
1257 | |
1258 action before after returns | |
1259 ------ ----- ----- ------- | |
1260 | |
1261 P P | |
1262 splice(P, A, 2, X-Y-Z) | | B-C | |
1263 A-B-C-D A-X-Y-Z-D | |
1264 | |
1265 P P | |
1266 splice(P, NULL, 1, X-Y) | | A | |
1267 A-B-C-D X-Y-B-C-D | |
1268 | |
1269 P P | |
1270 splice(P, NULL, 3, NULL) | | A-B-C | |
1271 A-B-C-D D | |
1272 | |
1273 P P | |
1274 splice(P, B, 0, X-Y) | | NULL | |
1275 A-B-C-D A-B-X-Y-C-D | |
1276 | |
1277 | |
1278 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>, | |
1279 see C<OpMORESIB_set>, C<OpLASTSIB_set>, C<OpMAYBESIB_set>. | |
1280 | |
1281 =cut | |
1282 */ | |
1283 | |
1284 OP * | |
1285 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert) | |
1286 { | |
1287 OP *first; | |
1288 OP *rest; | |
1289 OP *last_del = NULL; | |
1290 OP *last_ins = NULL; | |
1291 | |
1292 if (start) | |
1293 first = OpSIBLING(start); | |
1294 else if (!parent) | |
1295 goto no_parent; | |
1296 else | |
1297 first = cLISTOPx(parent)->op_first; | |
1298 | |
1299 assert(del_count >= -1); | |
1300 | |
1301 if (del_count && first) { | |
1302 last_del = first; | |
1303 while (--del_count && OpHAS_SIBLING(last_del)) | |
1304 last_del = OpSIBLING(last_del); | |
1305 rest = OpSIBLING(last_del); | |
1306 OpLASTSIB_set(last_del, NULL); | |
1307 } | |
1308 else | |
1309 rest = first; | |
1310 | |
1311 if (insert) { | |
1312 last_ins = insert; | |
1313 while (OpHAS_SIBLING(last_ins)) | |
1314 last_ins = OpSIBLING(last_ins); | |
1315 OpMAYBESIB_set(last_ins, rest, NULL); | |
1316 } | |
1317 else | |
1318 insert = rest; | |
1319 | |
1320 if (start) { | |
1321 OpMAYBESIB_set(start, insert, NULL); | |
1322 } | |
1323 else { | |
1324 if (!parent) | |
1325 goto no_parent; | |
1326 cLISTOPx(parent)->op_first = insert; | |
1327 if (insert) | |
1328 parent->op_flags |= OPf_KIDS; | |
1329 else | |
1330 parent->op_flags &= ~OPf_KIDS; | |
1331 } | |
1332 | |
1333 if (!rest) { | |
1334 /* update op_last etc */ | |
1335 U32 type; | |
1336 OP *lastop; | |
1337 | |
1338 if (!parent) | |
1339 goto no_parent; | |
1340 | |
1341 /* ought to use OP_CLASS(parent) here, but that can't handle | |
1342 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't | |
1343 * either */ | |
1344 type = parent->op_type; | |
1345 if (type == OP_CUSTOM) { | |
1346 dTHX; | |
1347 type = XopENTRYCUSTOM(parent, xop_class); | |
1348 } | |
1349 else { | |
1350 if (type == OP_NULL) | |
1351 type = parent->op_targ; | |
1352 type = PL_opargs[type] & OA_CLASS_MASK; | |
1353 } | |
1354 | |
1355 lastop = last_ins ? last_ins : start ? start : NULL; | |
1356 if ( type == OA_BINOP | |
1357 || type == OA_LISTOP | |
1358 || type == OA_PMOP | |
1359 || type == OA_LOOP | |
1360 ) | |
1361 cLISTOPx(parent)->op_last = lastop; | |
1362 | |
1363 if (lastop) | |
1364 OpLASTSIB_set(lastop, parent); | |
1365 } | |
1366 return last_del ? first : NULL; | |
1367 | |
1368 no_parent: | |
1369 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent"); | |
1370 } | |
1371 | |
1372 | |
1373 #ifdef PERL_OP_PARENT | |
1374 | |
1375 /* | |
1376 =for apidoc op_parent | |
1377 | |
1378 Returns the parent OP of o, if it has a parent. Returns NULL otherwise. | |
1379 This function is only available on perls built with C<-DPERL_OP_PARENT>. | |
1380 | |
1381 =cut | |
1382 */ | |
1383 | |
1384 OP * | |
1385 Perl_op_parent(OP *o) | |
1386 { | |
1387 PERL_ARGS_ASSERT_OP_PARENT; | |
1388 while (OpHAS_SIBLING(o)) | |
1389 o = OpSIBLING(o); | |
1390 return o->op_sibparent; | |
1391 } | |
1392 | |
1393 #endif | |
1394 | |
1395 | |
1396 /* replace the sibling following start with a new UNOP, which becomes | |
1397 * the parent of the original sibling; e.g. | |
1398 * | |
1399 * op_sibling_newUNOP(P, A, unop-args...) | |
1400 * | |
1401 * P P | |
1402 * | becomes | | |
1403 * A-B-C A-U-C | |
1404 * | | |
1405 * B | |
1406 * | |
1407 * where U is the new UNOP. | |
1408 * | |
1409 * parent and start args are the same as for op_sibling_splice(); | |
1410 * type and flags args are as newUNOP(). | |
1411 * | |
1412 * Returns the new UNOP. | |
1413 */ | |
1414 | |
1415 OP * | |
1416 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags) | |
1417 { | |
1418 OP *kid, *newop; | |
1419 | |
1420 kid = op_sibling_splice(parent, start, 1, NULL); | |
1421 newop = newUNOP(type, flags, kid); | |
1422 op_sibling_splice(parent, start, 0, newop); | |
1423 return newop; | |
1424 } | |
1425 | |
1426 | |
1427 /* lowest-level newLOGOP-style function - just allocates and populates | |
1428 * the struct. Higher-level stuff should be done by S_new_logop() / | |
1429 * newLOGOP(). This function exists mainly to avoid op_first assignment | |
1430 * being spread throughout this file. | |
1431 */ | |
1432 | |
1433 LOGOP * | |
1434 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other) | |
1435 { | |
1436 dVAR; | |
1437 LOGOP *logop; | |
1438 OP *kid = first; | |
1439 NewOp(1101, logop, 1, LOGOP); | |
1440 OpTYPE_set(logop, type); | |
1441 logop->op_first = first; | |
1442 logop->op_other = other; | |
1443 logop->op_flags = OPf_KIDS; | |
1444 while (kid && OpHAS_SIBLING(kid)) | |
1445 kid = OpSIBLING(kid); | |
1446 if (kid) | |
1447 OpLASTSIB_set(kid, (OP*)logop); | |
1448 return logop; | |
1449 } | |
1450 | |
1451 | |
1452 /* Contextualizers */ | |
1453 | |
1454 /* | |
1455 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context | |
1456 | |
1457 Applies a syntactic context to an op tree representing an expression. | |
1458 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>, | |
1459 or C<G_VOID> to specify the context to apply. The modified op tree | |
1460 is returned. | |
1461 | |
1462 =cut | |
1463 */ | |
1464 | |
1465 OP * | |
1466 Perl_op_contextualize(pTHX_ OP *o, I32 context) | |
1467 { | |
1468 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE; | |
1469 switch (context) { | |
1470 case G_SCALAR: return scalar(o); | |
1471 case G_ARRAY: return list(o); | |
1472 case G_VOID: return scalarvoid(o); | |
1473 default: | |
1474 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld", | |
1475 (long) context); | |
1476 } | |
1477 } | |
1478 | |
1479 /* | |
1480 | |
1481 =for apidoc Am|OP*|op_linklist|OP *o | |
1482 This function is the implementation of the L</LINKLIST> macro. It should | |
1483 not be called directly. | |
1484 | |
1485 =cut | |
1486 */ | |
1487 | |
1488 OP * | |
1489 Perl_op_linklist(pTHX_ OP *o) | |
1490 { | |
1491 OP *first; | |
1492 | |
1493 PERL_ARGS_ASSERT_OP_LINKLIST; | |
1494 | |
1495 if (o->op_next) | |
1496 return o->op_next; | |
1497 | |
1498 /* establish postfix order */ | |
1499 first = cUNOPo->op_first; | |
1500 if (first) { | |
1501 OP *kid; | |
1502 o->op_next = LINKLIST(first); | |
1503 kid = first; | |
1504 for (;;) { | |
1505 OP *sibl = OpSIBLING(kid); | |
1506 if (sibl) { | |
1507 kid->op_next = LINKLIST(sibl); | |
1508 kid = sibl; | |
1509 } else { | |
1510 kid->op_next = o; | |
1511 break; | |
1512 } | |
1513 } | |
1514 } | |
1515 else | |
1516 o->op_next = o; | |
1517 | |
1518 return o->op_next; | |
1519 } | |
1520 | |
1521 static OP * | |
1522 S_scalarkids(pTHX_ OP *o) | |
1523 { | |
1524 if (o && o->op_flags & OPf_KIDS) { | |
1525 OP *kid; | |
1526 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) | |
1527 scalar(kid); | |
1528 } | |
1529 return o; | |
1530 } | |
1531 | |
1532 STATIC OP * | |
1533 S_scalarboolean(pTHX_ OP *o) | |
1534 { | |
1535 PERL_ARGS_ASSERT_SCALARBOOLEAN; | |
1536 | |
1537 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST | |
1538 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) { | |
1539 if (ckWARN(WARN_SYNTAX)) { | |
1540 const line_t oldline = CopLINE(PL_curcop); | |
1541 | |
1542 if (PL_parser && PL_parser->copline != NOLINE) { | |
1543 /* This ensures that warnings are reported at the first line | |
1544 of the conditional, not the last. */ | |
1545 CopLINE_set(PL_curcop, PL_parser->copline); | |
1546 } | |
1547 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be =="); | |
1548 CopLINE_set(PL_curcop, oldline); | |
1549 } | |
1550 } | |
1551 return scalar(o); | |
1552 } | |
1553 | |
1554 static SV * | |
1555 S_op_varname(pTHX_ const OP *o) | |
1556 { | |
1557 assert(o); | |
1558 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV || | |
1559 o->op_type == OP_PADHV || o->op_type == OP_RV2HV); | |
1560 { | |
1561 const char funny = o->op_type == OP_PADAV | |
1562 || o->op_type == OP_RV2AV ? '@' : '%'; | |
1563 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) { | |
1564 GV *gv; | |
1565 if (cUNOPo->op_first->op_type != OP_GV | |
1566 || !(gv = cGVOPx_gv(cUNOPo->op_first))) | |
1567 return NULL; | |
1568 return varname(gv, funny, 0, NULL, 0, 1); | |
1569 } | |
1570 return | |
1571 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1); | |
1572 } | |
1573 } | |
1574 | |
1575 static void | |
1576 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv) | |
1577 { /* or not so pretty :-) */ | |
1578 if (o->op_type == OP_CONST) { | |
1579 *retsv = cSVOPo_sv; | |
1580 if (SvPOK(*retsv)) { | |
1581 SV *sv = *retsv; | |
1582 *retsv = sv_newmortal(); | |
1583 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL, | |
1584 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT); | |
1585 } | |
1586 else if (!SvOK(*retsv)) | |
1587 *retpv = "undef"; | |
1588 } | |
1589 else *retpv = "..."; | |
1590 } | |
1591 | |
1592 static void | |
1593 S_scalar_slice_warning(pTHX_ const OP *o) | |
1594 { | |
1595 OP *kid; | |
1596 const char lbrack = | |
1597 o->op_type == OP_HSLICE ? '{' : '['; | |
1598 const char rbrack = | |
1599 o->op_type == OP_HSLICE ? '}' : ']'; | |
1600 SV *name; | |
1601 SV *keysv = NULL; /* just to silence compiler warnings */ | |
1602 const char *key = NULL; | |
1603 | |
1604 if (!(o->op_private & OPpSLICEWARNING)) | |
1605 return; | |
1606 if (PL_parser && PL_parser->error_count) | |
1607 /* This warning can be nonsensical when there is a syntax error. */ | |
1608 return; | |
1609 | |
1610 kid = cLISTOPo->op_first; | |
1611 kid = OpSIBLING(kid); /* get past pushmark */ | |
1612 /* weed out false positives: any ops that can return lists */ | |
1613 switch (kid->op_type) { | |
1614 case OP_BACKTICK: | |
1615 case OP_GLOB: | |
1616 case OP_READLINE: | |
1617 case OP_MATCH: | |
1618 case OP_RV2AV: | |
1619 case OP_EACH: | |
1620 case OP_VALUES: | |
1621 case OP_KEYS: | |
1622 case OP_SPLIT: | |
1623 case OP_LIST: | |
1624 case OP_SORT: | |
1625 case OP_REVERSE: | |
1626 case OP_ENTERSUB: | |
1627 case OP_CALLER: | |
1628 case OP_LSTAT: | |
1629 case OP_STAT: | |
1630 case OP_READDIR: | |
1631 case OP_SYSTEM: | |
1632 case OP_TMS: | |
1633 case OP_LOCALTIME: | |
1634 case OP_GMTIME: | |
1635 case OP_ENTEREVAL: | |
1636 case OP_REACH: | |
1637 case OP_RKEYS: | |
1638 case OP_RVALUES: | |
1639 return; | |
1640 } | |
1641 | |
1642 /* Don't warn if we have a nulled list either. */ | |
1643 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST) | |
1644 return; | |
1645 | |
1646 assert(OpSIBLING(kid)); | |
1647 name = S_op_varname(aTHX_ OpSIBLING(kid)); | |
1648 if (!name) /* XS module fiddling with the op tree */ | |
1649 return; | |
1650 S_op_pretty(aTHX_ kid, &keysv, &key); | |
1651 assert(SvPOK(name)); | |
1652 sv_chop(name,SvPVX(name)+1); | |
1653 if (key) | |
1654 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */ | |
1655 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
1656 "Scalar value @%"SVf"%c%s%c better written as $%"SVf | |
1657 "%c%s%c", | |
1658 SVfARG(name), lbrack, key, rbrack, SVfARG(name), | |
1659 lbrack, key, rbrack); | |
1660 else | |
1661 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */ | |
1662 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
1663 "Scalar value @%"SVf"%c%"SVf"%c better written as $%" | |
1664 SVf"%c%"SVf"%c", | |
1665 SVfARG(name), lbrack, SVfARG(keysv), rbrack, | |
1666 SVfARG(name), lbrack, SVfARG(keysv), rbrack); | |
1667 } | |
1668 | |
1669 OP * | |
1670 Perl_scalar(pTHX_ OP *o) | |
1671 { | |
1672 OP *kid; | |
1673 | |
1674 /* assumes no premature commitment */ | |
1675 if (!o || (PL_parser && PL_parser->error_count) | |
1676 || (o->op_flags & OPf_WANT) | |
1677 || o->op_type == OP_RETURN) | |
1678 { | |
1679 return o; | |
1680 } | |
1681 | |
1682 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; | |
1683 | |
1684 switch (o->op_type) { | |
1685 case OP_REPEAT: | |
1686 scalar(cBINOPo->op_first); | |
1687 if (o->op_private & OPpREPEAT_DOLIST) { | |
1688 kid = cLISTOPx(cUNOPo->op_first)->op_first; | |
1689 assert(kid->op_type == OP_PUSHMARK); | |
1690 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) { | |
1691 op_null(cLISTOPx(cUNOPo->op_first)->op_first); | |
1692 o->op_private &=~ OPpREPEAT_DOLIST; | |
1693 } | |
1694 } | |
1695 break; | |
1696 case OP_OR: | |
1697 case OP_AND: | |
1698 case OP_COND_EXPR: | |
1699 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) | |
1700 scalar(kid); | |
1701 break; | |
1702 /* FALLTHROUGH */ | |
1703 case OP_SPLIT: | |
1704 case OP_MATCH: | |
1705 case OP_QR: | |
1706 case OP_SUBST: | |
1707 case OP_NULL: | |
1708 default: | |
1709 if (o->op_flags & OPf_KIDS) { | |
1710 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) | |
1711 scalar(kid); | |
1712 } | |
1713 break; | |
1714 case OP_LEAVE: | |
1715 case OP_LEAVETRY: | |
1716 kid = cLISTOPo->op_first; | |
1717 scalar(kid); | |
1718 kid = OpSIBLING(kid); | |
1719 do_kids: | |
1720 while (kid) { | |
1721 OP *sib = OpSIBLING(kid); | |
1722 if (sib && kid->op_type != OP_LEAVEWHEN | |
1723 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL | |
1724 || ( sib->op_targ != OP_NEXTSTATE | |
1725 && sib->op_targ != OP_DBSTATE ))) | |
1726 scalarvoid(kid); | |
1727 else | |
1728 scalar(kid); | |
1729 kid = sib; | |
1730 } | |
1731 PL_curcop = &PL_compiling; | |
1732 break; | |
1733 case OP_SCOPE: | |
1734 case OP_LINESEQ: | |
1735 case OP_LIST: | |
1736 kid = cLISTOPo->op_first; | |
1737 goto do_kids; | |
1738 case OP_SORT: | |
1739 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context"); | |
1740 break; | |
1741 case OP_KVHSLICE: | |
1742 case OP_KVASLICE: | |
1743 { | |
1744 /* Warn about scalar context */ | |
1745 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '['; | |
1746 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']'; | |
1747 SV *name; | |
1748 SV *keysv; | |
1749 const char *key = NULL; | |
1750 | |
1751 /* This warning can be nonsensical when there is a syntax error. */ | |
1752 if (PL_parser && PL_parser->error_count) | |
1753 break; | |
1754 | |
1755 if (!ckWARN(WARN_SYNTAX)) break; | |
1756 | |
1757 kid = cLISTOPo->op_first; | |
1758 kid = OpSIBLING(kid); /* get past pushmark */ | |
1759 assert(OpSIBLING(kid)); | |
1760 name = S_op_varname(aTHX_ OpSIBLING(kid)); | |
1761 if (!name) /* XS module fiddling with the op tree */ | |
1762 break; | |
1763 S_op_pretty(aTHX_ kid, &keysv, &key); | |
1764 assert(SvPOK(name)); | |
1765 sv_chop(name,SvPVX(name)+1); | |
1766 if (key) | |
1767 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */ | |
1768 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
1769 "%%%"SVf"%c%s%c in scalar context better written " | |
1770 "as $%"SVf"%c%s%c", | |
1771 SVfARG(name), lbrack, key, rbrack, SVfARG(name), | |
1772 lbrack, key, rbrack); | |
1773 else | |
1774 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */ | |
1775 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
1776 "%%%"SVf"%c%"SVf"%c in scalar context better " | |
1777 "written as $%"SVf"%c%"SVf"%c", | |
1778 SVfARG(name), lbrack, SVfARG(keysv), rbrack, | |
1779 SVfARG(name), lbrack, SVfARG(keysv), rbrack); | |
1780 } | |
1781 } | |
1782 return o; | |
1783 } | |
1784 | |
1785 OP * | |
1786 Perl_scalarvoid(pTHX_ OP *arg) | |
1787 { | |
1788 dVAR; | |
1789 OP *kid; | |
1790 SV* sv; | |
1791 U8 want; | |
1792 SSize_t defer_stack_alloc = 0; | |
1793 SSize_t defer_ix = -1; | |
1794 OP **defer_stack = NULL; | |
1795 OP *o = arg; | |
1796 | |
1797 PERL_ARGS_ASSERT_SCALARVOID; | |
1798 | |
1799 do { | |
1800 SV *useless_sv = NULL; | |
1801 const char* useless = NULL; | |
1802 | |
1803 if (o->op_type == OP_NEXTSTATE | |
1804 || o->op_type == OP_DBSTATE | |
1805 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE | |
1806 || o->op_targ == OP_DBSTATE))) | |
1807 PL_curcop = (COP*)o; /* for warning below */ | |
1808 | |
1809 /* assumes no premature commitment */ | |
1810 want = o->op_flags & OPf_WANT; | |
1811 if ((want && want != OPf_WANT_SCALAR) | |
1812 || (PL_parser && PL_parser->error_count) | |
1813 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN) | |
1814 { | |
1815 continue; | |
1816 } | |
1817 | |
1818 if ((o->op_private & OPpTARGET_MY) | |
1819 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ | |
1820 { | |
1821 /* newASSIGNOP has already applied scalar context, which we | |
1822 leave, as if this op is inside SASSIGN. */ | |
1823 continue; | |
1824 } | |
1825 | |
1826 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; | |
1827 | |
1828 switch (o->op_type) { | |
1829 default: | |
1830 if (!(PL_opargs[o->op_type] & OA_FOLDCONST)) | |
1831 break; | |
1832 /* FALLTHROUGH */ | |
1833 case OP_REPEAT: | |
1834 if (o->op_flags & OPf_STACKED) | |
1835 break; | |
1836 if (o->op_type == OP_REPEAT) | |
1837 scalar(cBINOPo->op_first); | |
1838 goto func_ops; | |
1839 case OP_SUBSTR: | |
1840 if (o->op_private == 4) | |
1841 break; | |
1842 /* FALLTHROUGH */ | |
1843 case OP_WANTARRAY: | |
1844 case OP_GV: | |
1845 case OP_SMARTMATCH: | |
1846 case OP_AV2ARYLEN: | |
1847 case OP_REF: | |
1848 case OP_REFGEN: | |
1849 case OP_SREFGEN: | |
1850 case OP_DEFINED: | |
1851 case OP_HEX: | |
1852 case OP_OCT: | |
1853 case OP_LENGTH: | |
1854 case OP_VEC: | |
1855 case OP_INDEX: | |
1856 case OP_RINDEX: | |
1857 case OP_SPRINTF: | |
1858 case OP_KVASLICE: | |
1859 case OP_KVHSLICE: | |
1860 case OP_UNPACK: | |
1861 case OP_PACK: | |
1862 case OP_JOIN: | |
1863 case OP_LSLICE: | |
1864 case OP_ANONLIST: | |
1865 case OP_ANONHASH: | |
1866 case OP_SORT: | |
1867 case OP_REVERSE: | |
1868 case OP_RANGE: | |
1869 case OP_FLIP: | |
1870 case OP_FLOP: | |
1871 case OP_CALLER: | |
1872 case OP_FILENO: | |
1873 case OP_EOF: | |
1874 case OP_TELL: | |
1875 case OP_GETSOCKNAME: | |
1876 case OP_GETPEERNAME: | |
1877 case OP_READLINK: | |
1878 case OP_TELLDIR: | |
1879 case OP_GETPPID: | |
1880 case OP_GETPGRP: | |
1881 case OP_GETPRIORITY: | |
1882 case OP_TIME: | |
1883 case OP_TMS: | |
1884 case OP_LOCALTIME: | |
1885 case OP_GMTIME: | |
1886 case OP_GHBYNAME: | |
1887 case OP_GHBYADDR: | |
1888 case OP_GHOSTENT: | |
1889 case OP_GNBYNAME: | |
1890 case OP_GNBYADDR: | |
1891 case OP_GNETENT: | |
1892 case OP_GPBYNAME: | |
1893 case OP_GPBYNUMBER: | |
1894 case OP_GPROTOENT: | |
1895 case OP_GSBYNAME: | |
1896 case OP_GSBYPORT: | |
1897 case OP_GSERVENT: | |
1898 case OP_GPWNAM: | |
1899 case OP_GPWUID: | |
1900 case OP_GGRNAM: | |
1901 case OP_GGRGID: | |
1902 case OP_GETLOGIN: | |
1903 case OP_PROTOTYPE: | |
1904 case OP_RUNCV: | |
1905 func_ops: | |
1906 useless = OP_DESC(o); | |
1907 break; | |
1908 | |
1909 case OP_GVSV: | |
1910 case OP_PADSV: | |
1911 case OP_PADAV: | |
1912 case OP_PADHV: | |
1913 case OP_PADANY: | |
1914 case OP_AELEM: | |
1915 case OP_AELEMFAST: | |
1916 case OP_AELEMFAST_LEX: | |
1917 case OP_ASLICE: | |
1918 case OP_HELEM: | |
1919 case OP_HSLICE: | |
1920 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))) | |
1921 /* Otherwise it's "Useless use of grep iterator" */ | |
1922 useless = OP_DESC(o); | |
1923 break; | |
1924 | |
1925 case OP_SPLIT: | |
1926 kid = cLISTOPo->op_first; | |
1927 if (kid && kid->op_type == OP_PUSHRE | |
1928 && !kid->op_targ | |
1929 && !(o->op_flags & OPf_STACKED) | |
1930 #ifdef USE_ITHREADS | |
1931 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff | |
1932 #else | |
1933 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv | |
1934 #endif | |
1935 ) | |
1936 useless = OP_DESC(o); | |
1937 break; | |
1938 | |
1939 case OP_NOT: | |
1940 kid = cUNOPo->op_first; | |
1941 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST && | |
1942 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) { | |
1943 goto func_ops; | |
1944 } | |
1945 useless = "negative pattern binding (!~)"; | |
1946 break; | |
1947 | |
1948 case OP_SUBST: | |
1949 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT) | |
1950 useless = "non-destructive substitution (s///r)"; | |
1951 break; | |
1952 | |
1953 case OP_TRANSR: | |
1954 useless = "non-destructive transliteration (tr///r)"; | |
1955 break; | |
1956 | |
1957 case OP_RV2GV: | |
1958 case OP_RV2SV: | |
1959 case OP_RV2AV: | |
1960 case OP_RV2HV: | |
1961 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) && | |
1962 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE)) | |
1963 useless = "a variable"; | |
1964 break; | |
1965 | |
1966 case OP_CONST: | |
1967 sv = cSVOPo_sv; | |
1968 if (cSVOPo->op_private & OPpCONST_STRICT) | |
1969 no_bareword_allowed(o); | |
1970 else { | |
1971 if (ckWARN(WARN_VOID)) { | |
1972 NV nv; | |
1973 /* don't warn on optimised away booleans, eg | |
1974 * use constant Foo, 5; Foo || print; */ | |
1975 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT) | |
1976 useless = NULL; | |
1977 /* the constants 0 and 1 are permitted as they are | |
1978 conventionally used as dummies in constructs like | |
1979 1 while some_condition_with_side_effects; */ | |
1980 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0)) | |
1981 useless = NULL; | |
1982 else if (SvPOK(sv)) { | |
1983 SV * const dsv = newSVpvs(""); | |
1984 useless_sv | |
1985 = Perl_newSVpvf(aTHX_ | |
1986 "a constant (%s)", | |
1987 pv_pretty(dsv, SvPVX_const(sv), | |
1988 SvCUR(sv), 32, NULL, NULL, | |
1989 PERL_PV_PRETTY_DUMP | |
1990 | PERL_PV_ESCAPE_NOCLEAR | |
1991 | PERL_PV_ESCAPE_UNI_DETECT)); | |
1992 SvREFCNT_dec_NN(dsv); | |
1993 } | |
1994 else if (SvOK(sv)) { | |
1995 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv)); | |
1996 } | |
1997 else | |
1998 useless = "a constant (undef)"; | |
1999 } | |
2000 } | |
2001 op_null(o); /* don't execute or even remember it */ | |
2002 break; | |
2003 | |
2004 case OP_POSTINC: | |
2005 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */ | |
2006 break; | |
2007 | |
2008 case OP_POSTDEC: | |
2009 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */ | |
2010 break; | |
2011 | |
2012 case OP_I_POSTINC: | |
2013 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */ | |
2014 break; | |
2015 | |
2016 case OP_I_POSTDEC: | |
2017 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */ | |
2018 break; | |
2019 | |
2020 case OP_SASSIGN: { | |
2021 OP *rv2gv; | |
2022 UNOP *refgen, *rv2cv; | |
2023 LISTOP *exlist; | |
2024 | |
2025 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2) | |
2026 break; | |
2027 | |
2028 rv2gv = ((BINOP *)o)->op_last; | |
2029 if (!rv2gv || rv2gv->op_type != OP_RV2GV) | |
2030 break; | |
2031 | |
2032 refgen = (UNOP *)((BINOP *)o)->op_first; | |
2033 | |
2034 if (!refgen || (refgen->op_type != OP_REFGEN | |
2035 && refgen->op_type != OP_SREFGEN)) | |
2036 break; | |
2037 | |
2038 exlist = (LISTOP *)refgen->op_first; | |
2039 if (!exlist || exlist->op_type != OP_NULL | |
2040 || exlist->op_targ != OP_LIST) | |
2041 break; | |
2042 | |
2043 if (exlist->op_first->op_type != OP_PUSHMARK | |
2044 && exlist->op_first != exlist->op_last) | |
2045 break; | |
2046 | |
2047 rv2cv = (UNOP*)exlist->op_last; | |
2048 | |
2049 if (rv2cv->op_type != OP_RV2CV) | |
2050 break; | |
2051 | |
2052 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0); | |
2053 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0); | |
2054 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0); | |
2055 | |
2056 o->op_private |= OPpASSIGN_CV_TO_GV; | |
2057 rv2gv->op_private |= OPpDONT_INIT_GV; | |
2058 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT; | |
2059 | |
2060 break; | |
2061 } | |
2062 | |
2063 case OP_AASSIGN: { | |
2064 inplace_aassign(o); | |
2065 break; | |
2066 } | |
2067 | |
2068 case OP_OR: | |
2069 case OP_AND: | |
2070 kid = cLOGOPo->op_first; | |
2071 if (kid->op_type == OP_NOT | |
2072 && (kid->op_flags & OPf_KIDS)) { | |
2073 if (o->op_type == OP_AND) { | |
2074 OpTYPE_set(o, OP_OR); | |
2075 } else { | |
2076 OpTYPE_set(o, OP_AND); | |
2077 } | |
2078 op_null(kid); | |
2079 } | |
2080 /* FALLTHROUGH */ | |
2081 | |
2082 case OP_DOR: | |
2083 case OP_COND_EXPR: | |
2084 case OP_ENTERGIVEN: | |
2085 case OP_ENTERWHEN: | |
2086 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) | |
2087 if (!(kid->op_flags & OPf_KIDS)) | |
2088 scalarvoid(kid); | |
2089 else | |
2090 DEFER_OP(kid); | |
2091 break; | |
2092 | |
2093 case OP_NULL: | |
2094 if (o->op_flags & OPf_STACKED) | |
2095 break; | |
2096 /* FALLTHROUGH */ | |
2097 case OP_NEXTSTATE: | |
2098 case OP_DBSTATE: | |
2099 case OP_ENTERTRY: | |
2100 case OP_ENTER: | |
2101 if (!(o->op_flags & OPf_KIDS)) | |
2102 break; | |
2103 /* FALLTHROUGH */ | |
2104 case OP_SCOPE: | |
2105 case OP_LEAVE: | |
2106 case OP_LEAVETRY: | |
2107 case OP_LEAVELOOP: | |
2108 case OP_LINESEQ: | |
2109 case OP_LEAVEGIVEN: | |
2110 case OP_LEAVEWHEN: | |
2111 kids: | |
2112 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) | |
2113 if (!(kid->op_flags & OPf_KIDS)) | |
2114 scalarvoid(kid); | |
2115 else | |
2116 DEFER_OP(kid); | |
2117 break; | |
2118 case OP_LIST: | |
2119 /* If the first kid after pushmark is something that the padrange | |
2120 optimisation would reject, then null the list and the pushmark. | |
2121 */ | |
2122 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK | |
2123 && ( !(kid = OpSIBLING(kid)) | |
2124 || ( kid->op_type != OP_PADSV | |
2125 && kid->op_type != OP_PADAV | |
2126 && kid->op_type != OP_PADHV) | |
2127 || kid->op_private & ~OPpLVAL_INTRO | |
2128 || !(kid = OpSIBLING(kid)) | |
2129 || ( kid->op_type != OP_PADSV | |
2130 && kid->op_type != OP_PADAV | |
2131 && kid->op_type != OP_PADHV) | |
2132 || kid->op_private & ~OPpLVAL_INTRO) | |
2133 ) { | |
2134 op_null(cUNOPo->op_first); /* NULL the pushmark */ | |
2135 op_null(o); /* NULL the list */ | |
2136 } | |
2137 goto kids; | |
2138 case OP_ENTEREVAL: | |
2139 scalarkids(o); | |
2140 break; | |
2141 case OP_SCALAR: | |
2142 scalar(o); | |
2143 break; | |
2144 } | |
2145 | |
2146 if (useless_sv) { | |
2147 /* mortalise it, in case warnings are fatal. */ | |
2148 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), | |
2149 "Useless use of %"SVf" in void context", | |
2150 SVfARG(sv_2mortal(useless_sv))); | |
2151 } | |
2152 else if (useless) { | |
2153 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), | |
2154 "Useless use of %s in void context", | |
2155 useless); | |
2156 } | |
2157 } while ( (o = POP_DEFERRED_OP()) ); | |
2158 | |
2159 Safefree(defer_stack); | |
2160 | |
2161 return arg; | |
2162 } | |
2163 | |
2164 static OP * | |
2165 S_listkids(pTHX_ OP *o) | |
2166 { | |
2167 if (o && o->op_flags & OPf_KIDS) { | |
2168 OP *kid; | |
2169 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) | |
2170 list(kid); | |
2171 } | |
2172 return o; | |
2173 } | |
2174 | |
2175 OP * | |
2176 Perl_list(pTHX_ OP *o) | |
2177 { | |
2178 OP *kid; | |
2179 | |
2180 /* assumes no premature commitment */ | |
2181 if (!o || (o->op_flags & OPf_WANT) | |
2182 || (PL_parser && PL_parser->error_count) | |
2183 || o->op_type == OP_RETURN) | |
2184 { | |
2185 return o; | |
2186 } | |
2187 | |
2188 if ((o->op_private & OPpTARGET_MY) | |
2189 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ | |
2190 { | |
2191 return o; /* As if inside SASSIGN */ | |
2192 } | |
2193 | |
2194 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; | |
2195 | |
2196 switch (o->op_type) { | |
2197 case OP_FLOP: | |
2198 list(cBINOPo->op_first); | |
2199 break; | |
2200 case OP_REPEAT: | |
2201 if (o->op_private & OPpREPEAT_DOLIST | |
2202 && !(o->op_flags & OPf_STACKED)) | |
2203 { | |
2204 list(cBINOPo->op_first); | |
2205 kid = cBINOPo->op_last; | |
2206 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv) | |
2207 && SvIVX(kSVOP_sv) == 1) | |
2208 { | |
2209 op_null(o); /* repeat */ | |
2210 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */ | |
2211 /* const (rhs): */ | |
2212 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL)); | |
2213 } | |
2214 } | |
2215 break; | |
2216 case OP_OR: | |
2217 case OP_AND: | |
2218 case OP_COND_EXPR: | |
2219 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) | |
2220 list(kid); | |
2221 break; | |
2222 default: | |
2223 case OP_MATCH: | |
2224 case OP_QR: | |
2225 case OP_SUBST: | |
2226 case OP_NULL: | |
2227 if (!(o->op_flags & OPf_KIDS)) | |
2228 break; | |
2229 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { | |
2230 list(cBINOPo->op_first); | |
2231 return gen_constant_list(o); | |
2232 } | |
2233 listkids(o); | |
2234 break; | |
2235 case OP_LIST: | |
2236 listkids(o); | |
2237 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) { | |
2238 op_null(cUNOPo->op_first); /* NULL the pushmark */ | |
2239 op_null(o); /* NULL the list */ | |
2240 } | |
2241 break; | |
2242 case OP_LEAVE: | |
2243 case OP_LEAVETRY: | |
2244 kid = cLISTOPo->op_first; | |
2245 list(kid); | |
2246 kid = OpSIBLING(kid); | |
2247 do_kids: | |
2248 while (kid) { | |
2249 OP *sib = OpSIBLING(kid); | |
2250 if (sib && kid->op_type != OP_LEAVEWHEN) | |
2251 scalarvoid(kid); | |
2252 else | |
2253 list(kid); | |
2254 kid = sib; | |
2255 } | |
2256 PL_curcop = &PL_compiling; | |
2257 break; | |
2258 case OP_SCOPE: | |
2259 case OP_LINESEQ: | |
2260 kid = cLISTOPo->op_first; | |
2261 goto do_kids; | |
2262 } | |
2263 return o; | |
2264 } | |
2265 | |
2266 static OP * | |
2267 S_scalarseq(pTHX_ OP *o) | |
2268 { | |
2269 if (o) { | |
2270 const OPCODE type = o->op_type; | |
2271 | |
2272 if (type == OP_LINESEQ || type == OP_SCOPE || | |
2273 type == OP_LEAVE || type == OP_LEAVETRY) | |
2274 { | |
2275 OP *kid, *sib; | |
2276 for (kid = cLISTOPo->op_first; kid; kid = sib) { | |
2277 if ((sib = OpSIBLING(kid)) | |
2278 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL | |
2279 || ( sib->op_targ != OP_NEXTSTATE | |
2280 && sib->op_targ != OP_DBSTATE ))) | |
2281 { | |
2282 scalarvoid(kid); | |
2283 } | |
2284 } | |
2285 PL_curcop = &PL_compiling; | |
2286 } | |
2287 o->op_flags &= ~OPf_PARENS; | |
2288 if (PL_hints & HINT_BLOCK_SCOPE) | |
2289 o->op_flags |= OPf_PARENS; | |
2290 } | |
2291 else | |
2292 o = newOP(OP_STUB, 0); | |
2293 return o; | |
2294 } | |
2295 | |
2296 STATIC OP * | |
2297 S_modkids(pTHX_ OP *o, I32 type) | |
2298 { | |
2299 if (o && o->op_flags & OPf_KIDS) { | |
2300 OP *kid; | |
2301 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) | |
2302 op_lvalue(kid, type); | |
2303 } | |
2304 return o; | |
2305 } | |
2306 | |
2307 | |
2308 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid | |
2309 * const fields. Also, convert CONST keys to HEK-in-SVs. | |
2310 * rop is the op that retrieves the hash; | |
2311 * key_op is the first key | |
2312 */ | |
2313 | |
2314 void | |
2315 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op) | |
2316 { | |
2317 PADNAME *lexname; | |
2318 GV **fields; | |
2319 bool check_fields; | |
2320 | |
2321 /* find the padsv corresponding to $lex->{} or @{$lex}{} */ | |
2322 if (rop) { | |
2323 if (rop->op_first->op_type == OP_PADSV) | |
2324 /* @$hash{qw(keys here)} */ | |
2325 rop = (UNOP*)rop->op_first; | |
2326 else { | |
2327 /* @{$hash}{qw(keys here)} */ | |
2328 if (rop->op_first->op_type == OP_SCOPE | |
2329 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV) | |
2330 { | |
2331 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last; | |
2332 } | |
2333 else | |
2334 rop = NULL; | |
2335 } | |
2336 } | |
2337 | |
2338 lexname = NULL; /* just to silence compiler warnings */ | |
2339 fields = NULL; /* just to silence compiler warnings */ | |
2340 | |
2341 check_fields = | |
2342 rop | |
2343 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ), | |
2344 SvPAD_TYPED(lexname)) | |
2345 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE)) | |
2346 && isGV(*fields) && GvHV(*fields); | |
2347 | |
2348 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) { | |
2349 SV **svp, *sv; | |
2350 if (key_op->op_type != OP_CONST) | |
2351 continue; | |
2352 svp = cSVOPx_svp(key_op); | |
2353 | |
2354 /* make sure it's not a bareword under strict subs */ | |
2355 if (key_op->op_private & OPpCONST_BARE && | |
2356 key_op->op_private & OPpCONST_STRICT) | |
2357 { | |
2358 no_bareword_allowed((OP*)key_op); | |
2359 } | |
2360 | |
2361 /* Make the CONST have a shared SV */ | |
2362 if ( !SvIsCOW_shared_hash(sv = *svp) | |
2363 && SvTYPE(sv) < SVt_PVMG | |
2364 && SvOK(sv) | |
2365 && !SvROK(sv)) | |
2366 { | |
2367 SSize_t keylen; | |
2368 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen); | |
2369 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0); | |
2370 SvREFCNT_dec_NN(sv); | |
2371 *svp = nsv; | |
2372 } | |
2373 | |
2374 if ( check_fields | |
2375 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) | |
2376 { | |
2377 Perl_croak(aTHX_ "No such class field \"%"SVf"\" " | |
2378 "in variable %"PNf" of type %"HEKf, | |
2379 SVfARG(*svp), PNfARG(lexname), | |
2380 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname)))); | |
2381 } | |
2382 } | |
2383 } | |
2384 | |
2385 | |
2386 /* | |
2387 =for apidoc finalize_optree | |
2388 | |
2389 This function finalizes the optree. Should be called directly after | |
2390 the complete optree is built. It does some additional | |
2391 checking which can't be done in the normal ck_xxx functions and makes | |
2392 the tree thread-safe. | |
2393 | |
2394 =cut | |
2395 */ | |
2396 void | |
2397 Perl_finalize_optree(pTHX_ OP* o) | |
2398 { | |
2399 PERL_ARGS_ASSERT_FINALIZE_OPTREE; | |
2400 | |
2401 ENTER; | |
2402 SAVEVPTR(PL_curcop); | |
2403 | |
2404 finalize_op(o); | |
2405 | |
2406 LEAVE; | |
2407 } | |
2408 | |
2409 #ifdef USE_ITHREADS | |
2410 /* Relocate sv to the pad for thread safety. | |
2411 * Despite being a "constant", the SV is written to, | |
2412 * for reference counts, sv_upgrade() etc. */ | |
2413 PERL_STATIC_INLINE void | |
2414 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp) | |
2415 { | |
2416 PADOFFSET ix; | |
2417 PERL_ARGS_ASSERT_OP_RELOCATE_SV; | |
2418 if (!*svp) return; | |
2419 ix = pad_alloc(OP_CONST, SVf_READONLY); | |
2420 SvREFCNT_dec(PAD_SVl(ix)); | |
2421 PAD_SETSV(ix, *svp); | |
2422 /* XXX I don't know how this isn't readonly already. */ | |
2423 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix)); | |
2424 *svp = NULL; | |
2425 *targp = ix; | |
2426 } | |
2427 #endif | |
2428 | |
2429 | |
2430 STATIC void | |
2431 S_finalize_op(pTHX_ OP* o) | |
2432 { | |
2433 PERL_ARGS_ASSERT_FINALIZE_OP; | |
2434 | |
2435 | |
2436 switch (o->op_type) { | |
2437 case OP_NEXTSTATE: | |
2438 case OP_DBSTATE: | |
2439 PL_curcop = ((COP*)o); /* for warnings */ | |
2440 break; | |
2441 case OP_EXEC: | |
2442 if (OpHAS_SIBLING(o)) { | |
2443 OP *sib = OpSIBLING(o); | |
2444 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE) | |
2445 && ckWARN(WARN_EXEC) | |
2446 && OpHAS_SIBLING(sib)) | |
2447 { | |
2448 const OPCODE type = OpSIBLING(sib)->op_type; | |
2449 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) { | |
2450 const line_t oldline = CopLINE(PL_curcop); | |
2451 CopLINE_set(PL_curcop, CopLINE((COP*)sib)); | |
2452 Perl_warner(aTHX_ packWARN(WARN_EXEC), | |
2453 "Statement unlikely to be reached"); | |
2454 Perl_warner(aTHX_ packWARN(WARN_EXEC), | |
2455 "\t(Maybe you meant system() when you said exec()?)\n"); | |
2456 CopLINE_set(PL_curcop, oldline); | |
2457 } | |
2458 } | |
2459 } | |
2460 break; | |
2461 | |
2462 case OP_GV: | |
2463 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) { | |
2464 GV * const gv = cGVOPo_gv; | |
2465 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) { | |
2466 /* XXX could check prototype here instead of just carping */ | |
2467 SV * const sv = sv_newmortal(); | |
2468 gv_efullname3(sv, gv, NULL); | |
2469 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), | |
2470 "%"SVf"() called too early to check prototype", | |
2471 SVfARG(sv)); | |
2472 } | |
2473 } | |
2474 break; | |
2475 | |
2476 case OP_CONST: | |
2477 if (cSVOPo->op_private & OPpCONST_STRICT) | |
2478 no_bareword_allowed(o); | |
2479 /* FALLTHROUGH */ | |
2480 #ifdef USE_ITHREADS | |
2481 case OP_HINTSEVAL: | |
2482 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ); | |
2483 #endif | |
2484 break; | |
2485 | |
2486 #ifdef USE_ITHREADS | |
2487 /* Relocate all the METHOP's SVs to the pad for thread safety. */ | |
2488 case OP_METHOD_NAMED: | |
2489 case OP_METHOD_SUPER: | |
2490 case OP_METHOD_REDIR: | |
2491 case OP_METHOD_REDIR_SUPER: | |
2492 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ); | |
2493 break; | |
2494 #endif | |
2495 | |
2496 case OP_HELEM: { | |
2497 UNOP *rop; | |
2498 SVOP *key_op; | |
2499 OP *kid; | |
2500 | |
2501 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST) | |
2502 break; | |
2503 | |
2504 rop = (UNOP*)((BINOP*)o)->op_first; | |
2505 | |
2506 goto check_keys; | |
2507 | |
2508 case OP_HSLICE: | |
2509 S_scalar_slice_warning(aTHX_ o); | |
2510 /* FALLTHROUGH */ | |
2511 | |
2512 case OP_KVHSLICE: | |
2513 kid = OpSIBLING(cLISTOPo->op_first); | |
2514 if (/* I bet there's always a pushmark... */ | |
2515 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST) | |
2516 && OP_TYPE_ISNT_NN(kid, OP_CONST)) | |
2517 { | |
2518 break; | |
2519 } | |
2520 | |
2521 key_op = (SVOP*)(kid->op_type == OP_CONST | |
2522 ? kid | |
2523 : OpSIBLING(kLISTOP->op_first)); | |
2524 | |
2525 rop = (UNOP*)((LISTOP*)o)->op_last; | |
2526 | |
2527 check_keys: | |
2528 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV) | |
2529 rop = NULL; | |
2530 S_check_hash_fields_and_hekify(aTHX_ rop, key_op); | |
2531 break; | |
2532 } | |
2533 case OP_ASLICE: | |
2534 S_scalar_slice_warning(aTHX_ o); | |
2535 break; | |
2536 | |
2537 case OP_SUBST: { | |
2538 if (cPMOPo->op_pmreplrootu.op_pmreplroot) | |
2539 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot); | |
2540 break; | |
2541 } | |
2542 default: | |
2543 break; | |
2544 } | |
2545 | |
2546 if (o->op_flags & OPf_KIDS) { | |
2547 OP *kid; | |
2548 | |
2549 #ifdef DEBUGGING | |
2550 /* check that op_last points to the last sibling, and that | |
2551 * the last op_sibling/op_sibparent field points back to the | |
2552 * parent, and that the only ops with KIDS are those which are | |
2553 * entitled to them */ | |
2554 U32 type = o->op_type; | |
2555 U32 family; | |
2556 bool has_last; | |
2557 | |
2558 if (type == OP_NULL) { | |
2559 type = o->op_targ; | |
2560 /* ck_glob creates a null UNOP with ex-type GLOB | |
2561 * (which is a list op. So pretend it wasn't a listop */ | |
2562 if (type == OP_GLOB) | |
2563 type = OP_NULL; | |
2564 } | |
2565 family = PL_opargs[type] & OA_CLASS_MASK; | |
2566 | |
2567 has_last = ( family == OA_BINOP | |
2568 || family == OA_LISTOP | |
2569 || family == OA_PMOP | |
2570 || family == OA_LOOP | |
2571 ); | |
2572 assert( has_last /* has op_first and op_last, or ... | |
2573 ... has (or may have) op_first: */ | |
2574 || family == OA_UNOP | |
2575 || family == OA_UNOP_AUX | |
2576 || family == OA_LOGOP | |
2577 || family == OA_BASEOP_OR_UNOP | |
2578 || family == OA_FILESTATOP | |
2579 || family == OA_LOOPEXOP | |
2580 || family == OA_METHOP | |
2581 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */ | |
2582 || type == OP_SASSIGN | |
2583 || type == OP_CUSTOM | |
2584 || type == OP_NULL /* new_logop does this */ | |
2585 ); | |
2586 | |
2587 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { | |
2588 # ifdef PERL_OP_PARENT | |
2589 if (!OpHAS_SIBLING(kid)) { | |
2590 if (has_last) | |
2591 assert(kid == cLISTOPo->op_last); | |
2592 assert(kid->op_sibparent == o); | |
2593 } | |
2594 # else | |
2595 if (has_last && !OpHAS_SIBLING(kid)) | |
2596 assert(kid == cLISTOPo->op_last); | |
2597 # endif | |
2598 } | |
2599 #endif | |
2600 | |
2601 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) | |
2602 finalize_op(kid); | |
2603 } | |
2604 } | |
2605 | |
2606 /* | |
2607 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type | |
2608 | |
2609 Propagate lvalue ("modifiable") context to an op and its children. | |
2610 I<type> represents the context type, roughly based on the type of op that | |
2611 would do the modifying, although C<local()> is represented by OP_NULL, | |
2612 because it has no op type of its own (it is signalled by a flag on | |
2613 the lvalue op). | |
2614 | |
2615 This function detects things that can't be modified, such as C<$x+1>, and | |
2616 generates errors for them. For example, C<$x+1 = 2> would cause it to be | |
2617 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN. | |
2618 | |
2619 It also flags things that need to behave specially in an lvalue context, | |
2620 such as C<$$x = 5> which might have to vivify a reference in C<$x>. | |
2621 | |
2622 =cut | |
2623 */ | |
2624 | |
2625 static void | |
2626 S_mark_padname_lvalue(pTHX_ PADNAME *pn) | |
2627 { | |
2628 CV *cv = PL_compcv; | |
2629 PadnameLVALUE_on(pn); | |
2630 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) { | |
2631 cv = CvOUTSIDE(cv); | |
2632 /* RT #127786: cv can be NULL due to an eval within the DB package | |
2633 * called from an anon sub - anon subs don't have CvOUTSIDE() set | |
2634 * unless they contain an eval, but calling eval within DB | |
2635 * pretends the eval was done in the caller's scope. | |
2636 */ | |
2637 if (!cv) | |
2638 break; | |
2639 assert(CvPADLIST(cv)); | |
2640 pn = | |
2641 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)]; | |
2642 assert(PadnameLEN(pn)); | |
2643 PadnameLVALUE_on(pn); | |
2644 } | |
2645 } | |
2646 | |
2647 static bool | |
2648 S_vivifies(const OPCODE type) | |
2649 { | |
2650 switch(type) { | |
2651 case OP_RV2AV: case OP_ASLICE: | |
2652 case OP_RV2HV: case OP_KVASLICE: | |
2653 case OP_RV2SV: case OP_HSLICE: | |
2654 case OP_AELEMFAST: case OP_KVHSLICE: | |
2655 case OP_HELEM: | |
2656 case OP_AELEM: | |
2657 return 1; | |
2658 } | |
2659 return 0; | |
2660 } | |
2661 | |
2662 static void | |
2663 S_lvref(pTHX_ OP *o, I32 type) | |
2664 { | |
2665 dVAR; | |
2666 OP *kid; | |
2667 switch (o->op_type) { | |
2668 case OP_COND_EXPR: | |
2669 for (kid = OpSIBLING(cUNOPo->op_first); kid; | |
2670 kid = OpSIBLING(kid)) | |
2671 S_lvref(aTHX_ kid, type); | |
2672 /* FALLTHROUGH */ | |
2673 case OP_PUSHMARK: | |
2674 return; | |
2675 case OP_RV2AV: | |
2676 if (cUNOPo->op_first->op_type != OP_GV) goto badref; | |
2677 o->op_flags |= OPf_STACKED; | |
2678 if (o->op_flags & OPf_PARENS) { | |
2679 if (o->op_private & OPpLVAL_INTRO) { | |
2680 yyerror(Perl_form(aTHX_ "Can't modify reference to " | |
2681 "localized parenthesized array in list assignment")); | |
2682 return; | |
2683 } | |
2684 slurpy: | |
2685 OpTYPE_set(o, OP_LVAVREF); | |
2686 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE; | |
2687 o->op_flags |= OPf_MOD|OPf_REF; | |
2688 return; | |
2689 } | |
2690 o->op_private |= OPpLVREF_AV; | |
2691 goto checkgv; | |
2692 case OP_RV2CV: | |
2693 kid = cUNOPo->op_first; | |
2694 if (kid->op_type == OP_NULL) | |
2695 kid = cUNOPx(OpSIBLING(kUNOP->op_first)) | |
2696 ->op_first; | |
2697 o->op_private = OPpLVREF_CV; | |
2698 if (kid->op_type == OP_GV) | |
2699 o->op_flags |= OPf_STACKED; | |
2700 else if (kid->op_type == OP_PADCV) { | |
2701 o->op_targ = kid->op_targ; | |
2702 kid->op_targ = 0; | |
2703 op_free(cUNOPo->op_first); | |
2704 cUNOPo->op_first = NULL; | |
2705 o->op_flags &=~ OPf_KIDS; | |
2706 } | |
2707 else goto badref; | |
2708 break; | |
2709 case OP_RV2HV: | |
2710 if (o->op_flags & OPf_PARENS) { | |
2711 parenhash: | |
2712 yyerror(Perl_form(aTHX_ "Can't modify reference to " | |
2713 "parenthesized hash in list assignment")); | |
2714 return; | |
2715 } | |
2716 o->op_private |= OPpLVREF_HV; | |
2717 /* FALLTHROUGH */ | |
2718 case OP_RV2SV: | |
2719 checkgv: | |
2720 if (cUNOPo->op_first->op_type != OP_GV) goto badref; | |
2721 o->op_flags |= OPf_STACKED; | |
2722 break; | |
2723 case OP_PADHV: | |
2724 if (o->op_flags & OPf_PARENS) goto parenhash; | |
2725 o->op_private |= OPpLVREF_HV; | |
2726 /* FALLTHROUGH */ | |
2727 case OP_PADSV: | |
2728 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); | |
2729 break; | |
2730 case OP_PADAV: | |
2731 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); | |
2732 if (o->op_flags & OPf_PARENS) goto slurpy; | |
2733 o->op_private |= OPpLVREF_AV; | |
2734 break; | |
2735 case OP_AELEM: | |
2736 case OP_HELEM: | |
2737 o->op_private |= OPpLVREF_ELEM; | |
2738 o->op_flags |= OPf_STACKED; | |
2739 break; | |
2740 case OP_ASLICE: | |
2741 case OP_HSLICE: | |
2742 OpTYPE_set(o, OP_LVREFSLICE); | |
2743 o->op_private &= OPpLVAL_INTRO|OPpLVREF_ELEM; | |
2744 return; | |
2745 case OP_NULL: | |
2746 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ | |
2747 goto badref; | |
2748 else if (!(o->op_flags & OPf_KIDS)) | |
2749 return; | |
2750 if (o->op_targ != OP_LIST) { | |
2751 S_lvref(aTHX_ cBINOPo->op_first, type); | |
2752 return; | |
2753 } | |
2754 /* FALLTHROUGH */ | |
2755 case OP_LIST: | |
2756 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) { | |
2757 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID); | |
2758 S_lvref(aTHX_ kid, type); | |
2759 } | |
2760 return; | |
2761 case OP_STUB: | |
2762 if (o->op_flags & OPf_PARENS) | |
2763 return; | |
2764 /* FALLTHROUGH */ | |
2765 default: | |
2766 badref: | |
2767 /* diag_listed_as: Can't modify reference to %s in %s assignment */ | |
2768 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s", | |
2769 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL | |
2770 ? "do block" | |
2771 : OP_DESC(o), | |
2772 PL_op_desc[type])); | |
2773 } | |
2774 OpTYPE_set(o, OP_LVREF); | |
2775 o->op_private &= | |
2776 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE; | |
2777 if (type == OP_ENTERLOOP) | |
2778 o->op_private |= OPpLVREF_ITER; | |
2779 } | |
2780 | |
2781 OP * | |
2782 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags) | |
2783 { | |
2784 dVAR; | |
2785 OP *kid; | |
2786 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */ | |
2787 int localize = -1; | |
2788 | |
2789 if (!o || (PL_parser && PL_parser->error_count)) | |
2790 return o; | |
2791 | |
2792 if ((o->op_private & OPpTARGET_MY) | |
2793 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ | |
2794 { | |
2795 return o; | |
2796 } | |
2797 | |
2798 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID ); | |
2799 | |
2800 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB; | |
2801 | |
2802 switch (o->op_type) { | |
2803 case OP_UNDEF: | |
2804 PL_modcount++; | |
2805 return o; | |
2806 case OP_STUB: | |
2807 if ((o->op_flags & OPf_PARENS)) | |
2808 break; | |
2809 goto nomod; | |
2810 case OP_ENTERSUB: | |
2811 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) && | |
2812 !(o->op_flags & OPf_STACKED)) { | |
2813 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */ | |
2814 assert(cUNOPo->op_first->op_type == OP_NULL); | |
2815 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ | |
2816 break; | |
2817 } | |
2818 else { /* lvalue subroutine call */ | |
2819 o->op_private |= OPpLVAL_INTRO; | |
2820 PL_modcount = RETURN_UNLIMITED_NUMBER; | |
2821 if (type == OP_GREPSTART || type == OP_ENTERSUB | |
2822 || type == OP_REFGEN || type == OP_LEAVESUBLV) { | |
2823 /* Potential lvalue context: */ | |
2824 o->op_private |= OPpENTERSUB_INARGS; | |
2825 break; | |
2826 } | |
2827 else { /* Compile-time error message: */ | |
2828 OP *kid = cUNOPo->op_first; | |
2829 CV *cv; | |
2830 GV *gv; | |
2831 | |
2832 if (kid->op_type != OP_PUSHMARK) { | |
2833 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST) | |
2834 Perl_croak(aTHX_ | |
2835 "panic: unexpected lvalue entersub " | |
2836 "args: type/targ %ld:%"UVuf, | |
2837 (long)kid->op_type, (UV)kid->op_targ); | |
2838 kid = kLISTOP->op_first; | |
2839 } | |
2840 while (OpHAS_SIBLING(kid)) | |
2841 kid = OpSIBLING(kid); | |
2842 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) { | |
2843 break; /* Postpone until runtime */ | |
2844 } | |
2845 | |
2846 kid = kUNOP->op_first; | |
2847 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV) | |
2848 kid = kUNOP->op_first; | |
2849 if (kid->op_type == OP_NULL) | |
2850 Perl_croak(aTHX_ | |
2851 "Unexpected constant lvalue entersub " | |
2852 "entry via type/targ %ld:%"UVuf, | |
2853 (long)kid->op_type, (UV)kid->op_targ); | |
2854 if (kid->op_type != OP_GV) { | |
2855 break; | |
2856 } | |
2857 | |
2858 gv = kGVOP_gv; | |
2859 cv = isGV(gv) | |
2860 ? GvCV(gv) | |
2861 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV | |
2862 ? MUTABLE_CV(SvRV(gv)) | |
2863 : NULL; | |
2864 if (!cv) | |
2865 break; | |
2866 if (CvLVALUE(cv)) | |
2867 break; | |
2868 } | |
2869 } | |
2870 /* FALLTHROUGH */ | |
2871 default: | |
2872 nomod: | |
2873 if (flags & OP_LVALUE_NO_CROAK) return NULL; | |
2874 /* grep, foreach, subcalls, refgen */ | |
2875 if (type == OP_GREPSTART || type == OP_ENTERSUB | |
2876 || type == OP_REFGEN || type == OP_LEAVESUBLV) | |
2877 break; | |
2878 yyerror(Perl_form(aTHX_ "Can't modify %s in %s", | |
2879 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL) | |
2880 ? "do block" | |
2881 : (o->op_type == OP_ENTERSUB | |
2882 ? "non-lvalue subroutine call" | |
2883 : OP_DESC(o))), | |
2884 type ? PL_op_desc[type] : "local")); | |
2885 return o; | |
2886 | |
2887 case OP_PREINC: | |
2888 case OP_PREDEC: | |
2889 case OP_POW: | |
2890 case OP_MULTIPLY: | |
2891 case OP_DIVIDE: | |
2892 case OP_MODULO: | |
2893 case OP_ADD: | |
2894 case OP_SUBTRACT: | |
2895 case OP_CONCAT: | |
2896 case OP_LEFT_SHIFT: | |
2897 case OP_RIGHT_SHIFT: | |
2898 case OP_BIT_AND: | |
2899 case OP_BIT_XOR: | |
2900 case OP_BIT_OR: | |
2901 case OP_I_MULTIPLY: | |
2902 case OP_I_DIVIDE: | |
2903 case OP_I_MODULO: | |
2904 case OP_I_ADD: | |
2905 case OP_I_SUBTRACT: | |
2906 if (!(o->op_flags & OPf_STACKED)) | |
2907 goto nomod; | |
2908 PL_modcount++; | |
2909 break; | |
2910 | |
2911 case OP_REPEAT: | |
2912 if (o->op_flags & OPf_STACKED) { | |
2913 PL_modcount++; | |
2914 break; | |
2915 } | |
2916 if (!(o->op_private & OPpREPEAT_DOLIST)) | |
2917 goto nomod; | |
2918 else { | |
2919 const I32 mods = PL_modcount; | |
2920 modkids(cBINOPo->op_first, type); | |
2921 if (type != OP_AASSIGN) | |
2922 goto nomod; | |
2923 kid = cBINOPo->op_last; | |
2924 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) { | |
2925 const IV iv = SvIV(kSVOP_sv); | |
2926 if (PL_modcount != RETURN_UNLIMITED_NUMBER) | |
2927 PL_modcount = | |
2928 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv); | |
2929 } | |
2930 else | |
2931 PL_modcount = RETURN_UNLIMITED_NUMBER; | |
2932 } | |
2933 break; | |
2934 | |
2935 case OP_COND_EXPR: | |
2936 localize = 1; | |
2937 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) | |
2938 op_lvalue(kid, type); | |
2939 break; | |
2940 | |
2941 case OP_RV2AV: | |
2942 case OP_RV2HV: | |
2943 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { | |
2944 PL_modcount = RETURN_UNLIMITED_NUMBER; | |
2945 return o; /* Treat \(@foo) like ordinary list. */ | |
2946 } | |
2947 /* FALLTHROUGH */ | |
2948 case OP_RV2GV: | |
2949 if (scalar_mod_type(o, type)) | |
2950 goto nomod; | |
2951 ref(cUNOPo->op_first, o->op_type); | |
2952 /* FALLTHROUGH */ | |
2953 case OP_ASLICE: | |
2954 case OP_HSLICE: | |
2955 localize = 1; | |
2956 /* FALLTHROUGH */ | |
2957 case OP_AASSIGN: | |
2958 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */ | |
2959 if (type == OP_LEAVESUBLV && ( | |
2960 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV) | |
2961 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR | |
2962 )) | |
2963 o->op_private |= OPpMAYBE_LVSUB; | |
2964 /* FALLTHROUGH */ | |
2965 case OP_NEXTSTATE: | |
2966 case OP_DBSTATE: | |
2967 PL_modcount = RETURN_UNLIMITED_NUMBER; | |
2968 break; | |
2969 case OP_KVHSLICE: | |
2970 case OP_KVASLICE: | |
2971 if (type == OP_LEAVESUBLV) | |
2972 o->op_private |= OPpMAYBE_LVSUB; | |
2973 goto nomod; | |
2974 case OP_AV2ARYLEN: | |
2975 PL_hints |= HINT_BLOCK_SCOPE; | |
2976 if (type == OP_LEAVESUBLV) | |
2977 o->op_private |= OPpMAYBE_LVSUB; | |
2978 PL_modcount++; | |
2979 break; | |
2980 case OP_RV2SV: | |
2981 ref(cUNOPo->op_first, o->op_type); | |
2982 localize = 1; | |
2983 /* FALLTHROUGH */ | |
2984 case OP_GV: | |
2985 PL_hints |= HINT_BLOCK_SCOPE; | |
2986 /* FALLTHROUGH */ | |
2987 case OP_SASSIGN: | |
2988 case OP_ANDASSIGN: | |
2989 case OP_ORASSIGN: | |
2990 case OP_DORASSIGN: | |
2991 PL_modcount++; | |
2992 break; | |
2993 | |
2994 case OP_AELEMFAST: | |
2995 case OP_AELEMFAST_LEX: | |
2996 localize = -1; | |
2997 PL_modcount++; | |
2998 break; | |
2999 | |
3000 case OP_PADAV: | |
3001 case OP_PADHV: | |
3002 PL_modcount = RETURN_UNLIMITED_NUMBER; | |
3003 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) | |
3004 return o; /* Treat \(@foo) like ordinary list. */ | |
3005 if (scalar_mod_type(o, type)) | |
3006 goto nomod; | |
3007 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR | |
3008 && type == OP_LEAVESUBLV) | |
3009 o->op_private |= OPpMAYBE_LVSUB; | |
3010 /* FALLTHROUGH */ | |
3011 case OP_PADSV: | |
3012 PL_modcount++; | |
3013 if (!type) /* local() */ | |
3014 Perl_croak(aTHX_ "Can't localize lexical variable %"PNf, | |
3015 PNfARG(PAD_COMPNAME(o->op_targ))); | |
3016 if (!(o->op_private & OPpLVAL_INTRO) | |
3017 || ( type != OP_SASSIGN && type != OP_AASSIGN | |
3018 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) )) | |
3019 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ)); | |
3020 break; | |
3021 | |
3022 case OP_PUSHMARK: | |
3023 localize = 0; | |
3024 break; | |
3025 | |
3026 case OP_KEYS: | |
3027 case OP_RKEYS: | |
3028 if (type != OP_SASSIGN && type != OP_LEAVESUBLV) | |
3029 goto nomod; | |
3030 goto lvalue_func; | |
3031 case OP_SUBSTR: | |
3032 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */ | |
3033 goto nomod; | |
3034 /* FALLTHROUGH */ | |
3035 case OP_POS: | |
3036 case OP_VEC: | |
3037 lvalue_func: | |
3038 if (type == OP_LEAVESUBLV) | |
3039 o->op_private |= OPpMAYBE_LVSUB; | |
3040 if (o->op_flags & OPf_KIDS) | |
3041 op_lvalue(OpSIBLING(cBINOPo->op_first), type); | |
3042 break; | |
3043 | |
3044 case OP_AELEM: | |
3045 case OP_HELEM: | |
3046 ref(cBINOPo->op_first, o->op_type); | |
3047 if (type == OP_ENTERSUB && | |
3048 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) | |
3049 o->op_private |= OPpLVAL_DEFER; | |
3050 if (type == OP_LEAVESUBLV) | |
3051 o->op_private |= OPpMAYBE_LVSUB; | |
3052 localize = 1; | |
3053 PL_modcount++; | |
3054 break; | |
3055 | |
3056 case OP_LEAVE: | |
3057 case OP_LEAVELOOP: | |
3058 o->op_private |= OPpLVALUE; | |
3059 /* FALLTHROUGH */ | |
3060 case OP_SCOPE: | |
3061 case OP_ENTER: | |
3062 case OP_LINESEQ: | |
3063 localize = 0; | |
3064 if (o->op_flags & OPf_KIDS) | |
3065 op_lvalue(cLISTOPo->op_last, type); | |
3066 break; | |
3067 | |
3068 case OP_NULL: | |
3069 localize = 0; | |
3070 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ | |
3071 goto nomod; | |
3072 else if (!(o->op_flags & OPf_KIDS)) | |
3073 break; | |
3074 if (o->op_targ != OP_LIST) { | |
3075 op_lvalue(cBINOPo->op_first, type); | |
3076 break; | |
3077 } | |
3078 /* FALLTHROUGH */ | |
3079 case OP_LIST: | |
3080 localize = 0; | |
3081 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) | |
3082 /* elements might be in void context because the list is | |
3083 in scalar context or because they are attribute sub calls */ | |
3084 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID ) | |
3085 op_lvalue(kid, type); | |
3086 break; | |
3087 | |
3088 case OP_COREARGS: | |
3089 return o; | |
3090 | |
3091 case OP_AND: | |
3092 case OP_OR: | |
3093 if (type == OP_LEAVESUBLV | |
3094 || !S_vivifies(cLOGOPo->op_first->op_type)) | |
3095 op_lvalue(cLOGOPo->op_first, type); | |
3096 if (type == OP_LEAVESUBLV | |
3097 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type)) | |
3098 op_lvalue(OpSIBLING(cLOGOPo->op_first), type); | |
3099 goto nomod; | |
3100 | |
3101 case OP_SREFGEN: | |
3102 if (type != OP_AASSIGN && type != OP_SASSIGN | |
3103 && type != OP_ENTERLOOP) | |
3104 goto nomod; | |
3105 /* Don’t bother applying lvalue context to the ex-list. */ | |
3106 kid = cUNOPx(cUNOPo->op_first)->op_first; | |
3107 assert (!OpHAS_SIBLING(kid)); | |
3108 goto kid_2lvref; | |
3109 case OP_REFGEN: | |
3110 if (type != OP_AASSIGN) goto nomod; | |
3111 kid = cUNOPo->op_first; | |
3112 kid_2lvref: | |
3113 { | |
3114 const U8 ec = PL_parser ? PL_parser->error_count : 0; | |
3115 S_lvref(aTHX_ kid, type); | |
3116 if (!PL_parser || PL_parser->error_count == ec) { | |
3117 if (!FEATURE_REFALIASING_IS_ENABLED) | |
3118 Perl_croak(aTHX_ | |
3119 "Experimental aliasing via reference not enabled"); | |
3120 Perl_ck_warner_d(aTHX_ | |
3121 packWARN(WARN_EXPERIMENTAL__REFALIASING), | |
3122 "Aliasing via reference is experimental"); | |
3123 } | |
3124 } | |
3125 if (o->op_type == OP_REFGEN) | |
3126 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */ | |
3127 op_null(o); | |
3128 return o; | |
3129 | |
3130 case OP_SPLIT: | |
3131 kid = cLISTOPo->op_first; | |
3132 if (kid && kid->op_type == OP_PUSHRE && | |
3133 ( kid->op_targ | |
3134 || o->op_flags & OPf_STACKED | |
3135 #ifdef USE_ITHREADS | |
3136 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff | |
3137 #else | |
3138 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv | |
3139 #endif | |
3140 )) { | |
3141 /* This is actually @array = split. */ | |
3142 PL_modcount = RETURN_UNLIMITED_NUMBER; | |
3143 break; | |
3144 } | |
3145 goto nomod; | |
3146 | |
3147 case OP_SCALAR: | |
3148 op_lvalue(cUNOPo->op_first, OP_ENTERSUB); | |
3149 goto nomod; | |
3150 } | |
3151 | |
3152 /* [20011101.069] File test operators interpret OPf_REF to mean that | |
3153 their argument is a filehandle; thus \stat(".") should not set | |
3154 it. AMS 20011102 */ | |
3155 if (type == OP_REFGEN && | |
3156 PL_check[o->op_type] == Perl_ck_ftst) | |
3157 return o; | |
3158 | |
3159 if (type != OP_LEAVESUBLV) | |
3160 o->op_flags |= OPf_MOD; | |
3161 | |
3162 if (type == OP_AASSIGN || type == OP_SASSIGN) | |
3163 o->op_flags |= OPf_SPECIAL|OPf_REF; | |
3164 else if (!type) { /* local() */ | |
3165 switch (localize) { | |
3166 case 1: | |
3167 o->op_private |= OPpLVAL_INTRO; | |
3168 o->op_flags &= ~OPf_SPECIAL; | |
3169 PL_hints |= HINT_BLOCK_SCOPE; | |
3170 break; | |
3171 case 0: | |
3172 break; | |
3173 case -1: | |
3174 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), | |
3175 "Useless localization of %s", OP_DESC(o)); | |
3176 } | |
3177 } | |
3178 else if (type != OP_GREPSTART && type != OP_ENTERSUB | |
3179 && type != OP_LEAVESUBLV) | |
3180 o->op_flags |= OPf_REF; | |
3181 return o; | |
3182 } | |
3183 | |
3184 STATIC bool | |
3185 S_scalar_mod_type(const OP *o, I32 type) | |
3186 { | |
3187 switch (type) { | |
3188 case OP_POS: | |
3189 case OP_SASSIGN: | |
3190 if (o && o->op_type == OP_RV2GV) | |
3191 return FALSE; | |
3192 /* FALLTHROUGH */ | |
3193 case OP_PREINC: | |
3194 case OP_PREDEC: | |
3195 case OP_POSTINC: | |
3196 case OP_POSTDEC: | |
3197 case OP_I_PREINC: | |
3198 case OP_I_PREDEC: | |
3199 case OP_I_POSTINC: | |
3200 case OP_I_POSTDEC: | |
3201 case OP_POW: | |
3202 case OP_MULTIPLY: | |
3203 case OP_DIVIDE: | |
3204 case OP_MODULO: | |
3205 case OP_REPEAT: | |
3206 case OP_ADD: | |
3207 case OP_SUBTRACT: | |
3208 case OP_I_MULTIPLY: | |
3209 case OP_I_DIVIDE: | |
3210 case OP_I_MODULO: | |
3211 case OP_I_ADD: | |
3212 case OP_I_SUBTRACT: | |
3213 case OP_LEFT_SHIFT: | |
3214 case OP_RIGHT_SHIFT: | |
3215 case OP_BIT_AND: | |
3216 case OP_BIT_XOR: | |
3217 case OP_BIT_OR: | |
3218 case OP_CONCAT: | |
3219 case OP_SUBST: | |
3220 case OP_TRANS: | |
3221 case OP_TRANSR: | |
3222 case OP_READ: | |
3223 case OP_SYSREAD: | |
3224 case OP_RECV: | |
3225 case OP_ANDASSIGN: | |
3226 case OP_ORASSIGN: | |
3227 case OP_DORASSIGN: | |
3228 return TRUE; | |
3229 default: | |
3230 return FALSE; | |
3231 } | |
3232 } | |
3233 | |
3234 STATIC bool | |
3235 S_is_handle_constructor(const OP *o, I32 numargs) | |
3236 { | |
3237 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR; | |
3238 | |
3239 switch (o->op_type) { | |
3240 case OP_PIPE_OP: | |
3241 case OP_SOCKPAIR: | |
3242 if (numargs == 2) | |
3243 return TRUE; | |
3244 /* FALLTHROUGH */ | |
3245 case OP_SYSOPEN: | |
3246 case OP_OPEN: | |
3247 case OP_SELECT: /* XXX c.f. SelectSaver.pm */ | |
3248 case OP_SOCKET: | |
3249 case OP_OPEN_DIR: | |
3250 case OP_ACCEPT: | |
3251 if (numargs == 1) | |
3252 return TRUE; | |
3253 /* FALLTHROUGH */ | |
3254 default: | |
3255 return FALSE; | |
3256 } | |
3257 } | |
3258 | |
3259 static OP * | |
3260 S_refkids(pTHX_ OP *o, I32 type) | |
3261 { | |
3262 if (o && o->op_flags & OPf_KIDS) { | |
3263 OP *kid; | |
3264 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) | |
3265 ref(kid, type); | |
3266 } | |
3267 return o; | |
3268 } | |
3269 | |
3270 OP * | |
3271 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref) | |
3272 { | |
3273 dVAR; | |
3274 OP *kid; | |
3275 | |
3276 PERL_ARGS_ASSERT_DOREF; | |
3277 | |
3278 if (PL_parser && PL_parser->error_count) | |
3279 return o; | |
3280 | |
3281 switch (o->op_type) { | |
3282 case OP_ENTERSUB: | |
3283 if ((type == OP_EXISTS || type == OP_DEFINED) && | |
3284 !(o->op_flags & OPf_STACKED)) { | |
3285 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */ | |
3286 assert(cUNOPo->op_first->op_type == OP_NULL); | |
3287 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */ | |
3288 o->op_flags |= OPf_SPECIAL; | |
3289 } | |
3290 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){ | |
3291 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV | |
3292 : type == OP_RV2HV ? OPpDEREF_HV | |
3293 : OPpDEREF_SV); | |
3294 o->op_flags |= OPf_MOD; | |
3295 } | |
3296 | |
3297 break; | |
3298 | |
3299 case OP_COND_EXPR: | |
3300 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) | |
3301 doref(kid, type, set_op_ref); | |
3302 break; | |
3303 case OP_RV2SV: | |
3304 if (type == OP_DEFINED) | |
3305 o->op_flags |= OPf_SPECIAL; /* don't create GV */ | |
3306 doref(cUNOPo->op_first, o->op_type, set_op_ref); | |
3307 /* FALLTHROUGH */ | |
3308 case OP_PADSV: | |
3309 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { | |
3310 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV | |
3311 : type == OP_RV2HV ? OPpDEREF_HV | |
3312 : OPpDEREF_SV); | |
3313 o->op_flags |= OPf_MOD; | |
3314 } | |
3315 break; | |
3316 | |
3317 case OP_RV2AV: | |
3318 case OP_RV2HV: | |
3319 if (set_op_ref) | |
3320 o->op_flags |= OPf_REF; | |
3321 /* FALLTHROUGH */ | |
3322 case OP_RV2GV: | |
3323 if (type == OP_DEFINED) | |
3324 o->op_flags |= OPf_SPECIAL; /* don't create GV */ | |
3325 doref(cUNOPo->op_first, o->op_type, set_op_ref); | |
3326 break; | |
3327 | |
3328 case OP_PADAV: | |
3329 case OP_PADHV: | |
3330 if (set_op_ref) | |
3331 o->op_flags |= OPf_REF; | |
3332 break; | |
3333 | |
3334 case OP_SCALAR: | |
3335 case OP_NULL: | |
3336 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED) | |
3337 break; | |
3338 doref(cBINOPo->op_first, type, set_op_ref); | |
3339 break; | |
3340 case OP_AELEM: | |
3341 case OP_HELEM: | |
3342 doref(cBINOPo->op_first, o->op_type, set_op_ref); | |
3343 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { | |
3344 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV | |
3345 : type == OP_RV2HV ? OPpDEREF_HV | |
3346 : OPpDEREF_SV); | |
3347 o->op_flags |= OPf_MOD; | |
3348 } | |
3349 break; | |
3350 | |
3351 case OP_SCOPE: | |
3352 case OP_LEAVE: | |
3353 set_op_ref = FALSE; | |
3354 /* FALLTHROUGH */ | |
3355 case OP_ENTER: | |
3356 case OP_LIST: | |
3357 if (!(o->op_flags & OPf_KIDS)) | |
3358 break; | |
3359 doref(cLISTOPo->op_last, type, set_op_ref); | |
3360 break; | |
3361 default: | |
3362 break; | |
3363 } | |
3364 return scalar(o); | |
3365 | |
3366 } | |
3367 | |
3368 STATIC OP * | |
3369 S_dup_attrlist(pTHX_ OP *o) | |
3370 { | |
3371 OP *rop; | |
3372 | |
3373 PERL_ARGS_ASSERT_DUP_ATTRLIST; | |
3374 | |
3375 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids, | |
3376 * where the first kid is OP_PUSHMARK and the remaining ones | |
3377 * are OP_CONST. We need to push the OP_CONST values. | |
3378 */ | |
3379 if (o->op_type == OP_CONST) | |
3380 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv)); | |
3381 else { | |
3382 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS)); | |
3383 rop = NULL; | |
3384 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) { | |
3385 if (o->op_type == OP_CONST) | |
3386 rop = op_append_elem(OP_LIST, rop, | |
3387 newSVOP(OP_CONST, o->op_flags, | |
3388 SvREFCNT_inc_NN(cSVOPo->op_sv))); | |
3389 } | |
3390 } | |
3391 return rop; | |
3392 } | |
3393 | |
3394 STATIC void | |
3395 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs) | |
3396 { | |
3397 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no; | |
3398 | |
3399 PERL_ARGS_ASSERT_APPLY_ATTRS; | |
3400 | |
3401 /* fake up C<use attributes $pkg,$rv,@attrs> */ | |
3402 | |
3403 #define ATTRSMODULE "attributes" | |
3404 #define ATTRSMODULE_PM "attributes.pm" | |
3405 | |
3406 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, | |
3407 newSVpvs(ATTRSMODULE), | |
3408 NULL, | |
3409 op_prepend_elem(OP_LIST, | |
3410 newSVOP(OP_CONST, 0, stashsv), | |
3411 op_prepend_elem(OP_LIST, | |
3412 newSVOP(OP_CONST, 0, | |
3413 newRV(target)), | |
3414 dup_attrlist(attrs)))); | |
3415 } | |
3416 | |
3417 STATIC void | |
3418 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp) | |
3419 { | |
3420 OP *pack, *imop, *arg; | |
3421 SV *meth, *stashsv, **svp; | |
3422 | |
3423 PERL_ARGS_ASSERT_APPLY_ATTRS_MY; | |
3424 | |
3425 if (!attrs) | |
3426 return; | |
3427 | |
3428 assert(target->op_type == OP_PADSV || | |
3429 target->op_type == OP_PADHV || | |
3430 target->op_type == OP_PADAV); | |
3431 | |
3432 /* Ensure that attributes.pm is loaded. */ | |
3433 /* Don't force the C<use> if we don't need it. */ | |
3434 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE); | |
3435 if (svp && *svp != &PL_sv_undef) | |
3436 NOOP; /* already in %INC */ | |
3437 else | |
3438 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, | |
3439 newSVpvs(ATTRSMODULE), NULL); | |
3440 | |
3441 /* Need package name for method call. */ | |
3442 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE)); | |
3443 | |
3444 /* Build up the real arg-list. */ | |
3445 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no; | |
3446 | |
3447 arg = newOP(OP_PADSV, 0); | |
3448 arg->op_targ = target->op_targ; | |
3449 arg = op_prepend_elem(OP_LIST, | |
3450 newSVOP(OP_CONST, 0, stashsv), | |
3451 op_prepend_elem(OP_LIST, | |
3452 newUNOP(OP_REFGEN, 0, | |
3453 arg), | |
3454 dup_attrlist(attrs))); | |
3455 | |
3456 /* Fake up a method call to import */ | |
3457 meth = newSVpvs_share("import"); | |
3458 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID, | |
3459 op_append_elem(OP_LIST, | |
3460 op_prepend_elem(OP_LIST, pack, arg), | |
3461 newMETHOP_named(OP_METHOD_NAMED, 0, meth))); | |
3462 | |
3463 /* Combine the ops. */ | |
3464 *imopsp = op_append_elem(OP_LIST, *imopsp, imop); | |
3465 } | |
3466 | |
3467 /* | |
3468 =notfor apidoc apply_attrs_string | |
3469 | |
3470 Attempts to apply a list of attributes specified by the C<attrstr> and | |
3471 C<len> arguments to the subroutine identified by the C<cv> argument which | |
3472 is expected to be associated with the package identified by the C<stashpv> | |
3473 argument (see L<attributes>). It gets this wrong, though, in that it | |
3474 does not correctly identify the boundaries of the individual attribute | |
3475 specifications within C<attrstr>. This is not really intended for the | |
3476 public API, but has to be listed here for systems such as AIX which | |
3477 need an explicit export list for symbols. (It's called from XS code | |
3478 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it | |
3479 to respect attribute syntax properly would be welcome. | |
3480 | |
3481 =cut | |
3482 */ | |
3483 | |
3484 void | |
3485 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv, | |
3486 const char *attrstr, STRLEN len) | |
3487 { | |
3488 OP *attrs = NULL; | |
3489 | |
3490 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING; | |
3491 | |
3492 if (!len) { | |
3493 len = strlen(attrstr); | |
3494 } | |
3495 | |
3496 while (len) { | |
3497 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ; | |
3498 if (len) { | |
3499 const char * const sstr = attrstr; | |
3500 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ; | |
3501 attrs = op_append_elem(OP_LIST, attrs, | |
3502 newSVOP(OP_CONST, 0, | |
3503 newSVpvn(sstr, attrstr-sstr))); | |
3504 } | |
3505 } | |
3506 | |
3507 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, | |
3508 newSVpvs(ATTRSMODULE), | |
3509 NULL, op_prepend_elem(OP_LIST, | |
3510 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)), | |
3511 op_prepend_elem(OP_LIST, | |
3512 newSVOP(OP_CONST, 0, | |
3513 newRV(MUTABLE_SV(cv))), | |
3514 attrs))); | |
3515 } | |
3516 | |
3517 STATIC void | |
3518 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name) | |
3519 { | |
3520 OP *new_proto = NULL; | |
3521 STRLEN pvlen; | |
3522 char *pv; | |
3523 OP *o; | |
3524 | |
3525 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR; | |
3526 | |
3527 if (!*attrs) | |
3528 return; | |
3529 | |
3530 o = *attrs; | |
3531 if (o->op_type == OP_CONST) { | |
3532 pv = SvPV(cSVOPo_sv, pvlen); | |
3533 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) { | |
3534 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv)); | |
3535 SV ** const tmpo = cSVOPx_svp(o); | |
3536 SvREFCNT_dec(cSVOPo_sv); | |
3537 *tmpo = tmpsv; | |
3538 new_proto = o; | |
3539 *attrs = NULL; | |
3540 } | |
3541 } else if (o->op_type == OP_LIST) { | |
3542 OP * lasto; | |
3543 assert(o->op_flags & OPf_KIDS); | |
3544 lasto = cLISTOPo->op_first; | |
3545 assert(lasto->op_type == OP_PUSHMARK); | |
3546 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) { | |
3547 if (o->op_type == OP_CONST) { | |
3548 pv = SvPV(cSVOPo_sv, pvlen); | |
3549 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) { | |
3550 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv)); | |
3551 SV ** const tmpo = cSVOPx_svp(o); | |
3552 SvREFCNT_dec(cSVOPo_sv); | |
3553 *tmpo = tmpsv; | |
3554 if (new_proto && ckWARN(WARN_MISC)) { | |
3555 STRLEN new_len; | |
3556 const char * newp = SvPV(cSVOPo_sv, new_len); | |
3557 Perl_warner(aTHX_ packWARN(WARN_MISC), | |
3558 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub", | |
3559 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp)); | |
3560 op_free(new_proto); | |
3561 } | |
3562 else if (new_proto) | |
3563 op_free(new_proto); | |
3564 new_proto = o; | |
3565 /* excise new_proto from the list */ | |
3566 op_sibling_splice(*attrs, lasto, 1, NULL); | |
3567 o = lasto; | |
3568 continue; | |
3569 } | |
3570 } | |
3571 lasto = o; | |
3572 } | |
3573 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs | |
3574 would get pulled in with no real need */ | |
3575 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) { | |
3576 op_free(*attrs); | |
3577 *attrs = NULL; | |
3578 } | |
3579 } | |
3580 | |
3581 if (new_proto) { | |
3582 SV *svname; | |
3583 if (isGV(name)) { | |
3584 svname = sv_newmortal(); | |
3585 gv_efullname3(svname, name, NULL); | |
3586 } | |
3587 else if (SvPOK(name) && *SvPVX((SV *)name) == '&') | |
3588 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP); | |
3589 else | |
3590 svname = (SV *)name; | |
3591 if (ckWARN(WARN_ILLEGALPROTO)) | |
3592 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE); | |
3593 if (*proto && ckWARN(WARN_PROTOTYPE)) { | |
3594 STRLEN old_len, new_len; | |
3595 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len); | |
3596 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len); | |
3597 | |
3598 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), | |
3599 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'" | |
3600 " in %"SVf, | |
3601 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp), | |
3602 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp), | |
3603 SVfARG(svname)); | |
3604 } | |
3605 if (*proto) | |
3606 op_free(*proto); | |
3607 *proto = new_proto; | |
3608 } | |
3609 } | |
3610 | |
3611 static void | |
3612 S_cant_declare(pTHX_ OP *o) | |
3613 { | |
3614 if (o->op_type == OP_NULL | |
3615 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS) | |
3616 o = cUNOPo->op_first; | |
3617 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"", | |
3618 o->op_type == OP_NULL | |
3619 && o->op_flags & OPf_SPECIAL | |
3620 ? "do block" | |
3621 : OP_DESC(o), | |
3622 PL_parser->in_my == KEY_our ? "our" : | |
3623 PL_parser->in_my == KEY_state ? "state" : | |
3624 "my")); | |
3625 } | |
3626 | |
3627 STATIC OP * | |
3628 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp) | |
3629 { | |
3630 I32 type; | |
3631 const bool stately = PL_parser && PL_parser->in_my == KEY_state; | |
3632 | |
3633 PERL_ARGS_ASSERT_MY_KID; | |
3634 | |
3635 if (!o || (PL_parser && PL_parser->error_count)) | |
3636 return o; | |
3637 | |
3638 type = o->op_type; | |
3639 | |
3640 if (type == OP_LIST) { | |
3641 OP *kid; | |
3642 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) | |
3643 my_kid(kid, attrs, imopsp); | |
3644 return o; | |
3645 } else if (type == OP_UNDEF || type == OP_STUB) { | |
3646 return o; | |
3647 } else if (type == OP_RV2SV || /* "our" declaration */ | |
3648 type == OP_RV2AV || | |
3649 type == OP_RV2HV) { /* XXX does this let anything illegal in? */ | |
3650 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */ | |
3651 S_cant_declare(aTHX_ o); | |
3652 } else if (attrs) { | |
3653 GV * const gv = cGVOPx_gv(cUNOPo->op_first); | |
3654 assert(PL_parser); | |
3655 PL_parser->in_my = FALSE; | |
3656 PL_parser->in_my_stash = NULL; | |
3657 apply_attrs(GvSTASH(gv), | |
3658 (type == OP_RV2SV ? GvSV(gv) : | |
3659 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) : | |
3660 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)), | |
3661 attrs); | |
3662 } | |
3663 o->op_private |= OPpOUR_INTRO; | |
3664 return o; | |
3665 } | |
3666 else if (type != OP_PADSV && | |
3667 type != OP_PADAV && | |
3668 type != OP_PADHV && | |
3669 type != OP_PUSHMARK) | |
3670 { | |
3671 S_cant_declare(aTHX_ o); | |
3672 return o; | |
3673 } | |
3674 else if (attrs && type != OP_PUSHMARK) { | |
3675 HV *stash; | |
3676 | |
3677 assert(PL_parser); | |
3678 PL_parser->in_my = FALSE; | |
3679 PL_parser->in_my_stash = NULL; | |
3680 | |
3681 /* check for C<my Dog $spot> when deciding package */ | |
3682 stash = PAD_COMPNAME_TYPE(o->op_targ); | |
3683 if (!stash) | |
3684 stash = PL_curstash; | |
3685 apply_attrs_my(stash, o, attrs, imopsp); | |
3686 } | |
3687 o->op_flags |= OPf_MOD; | |
3688 o->op_private |= OPpLVAL_INTRO; | |
3689 if (stately) | |
3690 o->op_private |= OPpPAD_STATE; | |
3691 return o; | |
3692 } | |
3693 | |
3694 OP * | |
3695 Perl_my_attrs(pTHX_ OP *o, OP *attrs) | |
3696 { | |
3697 OP *rops; | |
3698 int maybe_scalar = 0; | |
3699 | |
3700 PERL_ARGS_ASSERT_MY_ATTRS; | |
3701 | |
3702 /* [perl #17376]: this appears to be premature, and results in code such as | |
3703 C< our(%x); > executing in list mode rather than void mode */ | |
3704 #if 0 | |
3705 if (o->op_flags & OPf_PARENS) | |
3706 list(o); | |
3707 else | |
3708 maybe_scalar = 1; | |
3709 #else | |
3710 maybe_scalar = 1; | |
3711 #endif | |
3712 if (attrs) | |
3713 SAVEFREEOP(attrs); | |
3714 rops = NULL; | |
3715 o = my_kid(o, attrs, &rops); | |
3716 if (rops) { | |
3717 if (maybe_scalar && o->op_type == OP_PADSV) { | |
3718 o = scalar(op_append_list(OP_LIST, rops, o)); | |
3719 o->op_private |= OPpLVAL_INTRO; | |
3720 } | |
3721 else { | |
3722 /* The listop in rops might have a pushmark at the beginning, | |
3723 which will mess up list assignment. */ | |
3724 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */ | |
3725 if (rops->op_type == OP_LIST && | |
3726 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK) | |
3727 { | |
3728 OP * const pushmark = lrops->op_first; | |
3729 /* excise pushmark */ | |
3730 op_sibling_splice(rops, NULL, 1, NULL); | |
3731 op_free(pushmark); | |
3732 } | |
3733 o = op_append_list(OP_LIST, o, rops); | |
3734 } | |
3735 } | |
3736 PL_parser->in_my = FALSE; | |
3737 PL_parser->in_my_stash = NULL; | |
3738 return o; | |
3739 } | |
3740 | |
3741 OP * | |
3742 Perl_sawparens(pTHX_ OP *o) | |
3743 { | |
3744 PERL_UNUSED_CONTEXT; | |
3745 if (o) | |
3746 o->op_flags |= OPf_PARENS; | |
3747 return o; | |
3748 } | |
3749 | |
3750 OP * | |
3751 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right) | |
3752 { | |
3753 OP *o; | |
3754 bool ismatchop = 0; | |
3755 const OPCODE ltype = left->op_type; | |
3756 const OPCODE rtype = right->op_type; | |
3757 | |
3758 PERL_ARGS_ASSERT_BIND_MATCH; | |
3759 | |
3760 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV | |
3761 || ltype == OP_PADHV) && ckWARN(WARN_MISC)) | |
3762 { | |
3763 const char * const desc | |
3764 = PL_op_desc[( | |
3765 rtype == OP_SUBST || rtype == OP_TRANS | |
3766 || rtype == OP_TRANSR | |
3767 ) | |
3768 ? (int)rtype : OP_MATCH]; | |
3769 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV; | |
3770 SV * const name = | |
3771 S_op_varname(aTHX_ left); | |
3772 if (name) | |
3773 Perl_warner(aTHX_ packWARN(WARN_MISC), | |
3774 "Applying %s to %"SVf" will act on scalar(%"SVf")", | |
3775 desc, SVfARG(name), SVfARG(name)); | |
3776 else { | |
3777 const char * const sample = (isary | |
3778 ? "@array" : "%hash"); | |
3779 Perl_warner(aTHX_ packWARN(WARN_MISC), | |
3780 "Applying %s to %s will act on scalar(%s)", | |
3781 desc, sample, sample); | |
3782 } | |
3783 } | |
3784 | |
3785 if (rtype == OP_CONST && | |
3786 cSVOPx(right)->op_private & OPpCONST_BARE && | |
3787 cSVOPx(right)->op_private & OPpCONST_STRICT) | |
3788 { | |
3789 no_bareword_allowed(right); | |
3790 } | |
3791 | |
3792 /* !~ doesn't make sense with /r, so error on it for now */ | |
3793 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) && | |
3794 type == OP_NOT) | |
3795 /* diag_listed_as: Using !~ with %s doesn't make sense */ | |
3796 yyerror("Using !~ with s///r doesn't make sense"); | |
3797 if (rtype == OP_TRANSR && type == OP_NOT) | |
3798 /* diag_listed_as: Using !~ with %s doesn't make sense */ | |
3799 yyerror("Using !~ with tr///r doesn't make sense"); | |
3800 | |
3801 ismatchop = (rtype == OP_MATCH || | |
3802 rtype == OP_SUBST || | |
3803 rtype == OP_TRANS || rtype == OP_TRANSR) | |
3804 && !(right->op_flags & OPf_SPECIAL); | |
3805 if (ismatchop && right->op_private & OPpTARGET_MY) { | |
3806 right->op_targ = 0; | |
3807 right->op_private &= ~OPpTARGET_MY; | |
3808 } | |
3809 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) { | |
3810 if (left->op_type == OP_PADSV | |
3811 && !(left->op_private & OPpLVAL_INTRO)) | |
3812 { | |
3813 right->op_targ = left->op_targ; | |
3814 op_free(left); | |
3815 o = right; | |
3816 } | |
3817 else { | |
3818 right->op_flags |= OPf_STACKED; | |
3819 if (rtype != OP_MATCH && rtype != OP_TRANSR && | |
3820 ! (rtype == OP_TRANS && | |
3821 right->op_private & OPpTRANS_IDENTICAL) && | |
3822 ! (rtype == OP_SUBST && | |
3823 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT))) | |
3824 left = op_lvalue(left, rtype); | |
3825 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR) | |
3826 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right); | |
3827 else | |
3828 o = op_prepend_elem(rtype, scalar(left), right); | |
3829 } | |
3830 if (type == OP_NOT) | |
3831 return newUNOP(OP_NOT, 0, scalar(o)); | |
3832 return o; | |
3833 } | |
3834 else | |
3835 return bind_match(type, left, | |
3836 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0)); | |
3837 } | |
3838 | |
3839 OP * | |
3840 Perl_invert(pTHX_ OP *o) | |
3841 { | |
3842 if (!o) | |
3843 return NULL; | |
3844 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o)); | |
3845 } | |
3846 | |
3847 /* | |
3848 =for apidoc Amx|OP *|op_scope|OP *o | |
3849 | |
3850 Wraps up an op tree with some additional ops so that at runtime a dynamic | |
3851 scope will be created. The original ops run in the new dynamic scope, | |
3852 and then, provided that they exit normally, the scope will be unwound. | |
3853 The additional ops used to create and unwind the dynamic scope will | |
3854 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used | |
3855 instead if the ops are simple enough to not need the full dynamic scope | |
3856 structure. | |
3857 | |
3858 =cut | |
3859 */ | |
3860 | |
3861 OP * | |
3862 Perl_op_scope(pTHX_ OP *o) | |
3863 { | |
3864 dVAR; | |
3865 if (o) { | |
3866 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) { | |
3867 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o); | |
3868 OpTYPE_set(o, OP_LEAVE); | |
3869 } | |
3870 else if (o->op_type == OP_LINESEQ) { | |
3871 OP *kid; | |
3872 OpTYPE_set(o, OP_SCOPE); | |
3873 kid = ((LISTOP*)o)->op_first; | |
3874 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { | |
3875 op_null(kid); | |
3876 | |
3877 /* The following deals with things like 'do {1 for 1}' */ | |
3878 kid = OpSIBLING(kid); | |
3879 if (kid && | |
3880 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)) | |
3881 op_null(kid); | |
3882 } | |
3883 } | |
3884 else | |
3885 o = newLISTOP(OP_SCOPE, 0, o, NULL); | |
3886 } | |
3887 return o; | |
3888 } | |
3889 | |
3890 OP * | |
3891 Perl_op_unscope(pTHX_ OP *o) | |
3892 { | |
3893 if (o && o->op_type == OP_LINESEQ) { | |
3894 OP *kid = cLISTOPo->op_first; | |
3895 for(; kid; kid = OpSIBLING(kid)) | |
3896 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) | |
3897 op_null(kid); | |
3898 } | |
3899 return o; | |
3900 } | |
3901 | |
3902 /* | |
3903 =for apidoc Am|int|block_start|int full | |
3904 | |
3905 Handles compile-time scope entry. | |
3906 Arranges for hints to be restored on block | |
3907 exit and also handles pad sequence numbers to make lexical variables scope | |
3908 right. Returns a savestack index for use with C<block_end>. | |
3909 | |
3910 =cut | |
3911 */ | |
3912 | |
3913 int | |
3914 Perl_block_start(pTHX_ int full) | |
3915 { | |
3916 const int retval = PL_savestack_ix; | |
3917 | |
3918 PL_compiling.cop_seq = PL_cop_seqmax; | |
3919 COP_SEQMAX_INC; | |
3920 pad_block_start(full); | |
3921 SAVEHINTS(); | |
3922 PL_hints &= ~HINT_BLOCK_SCOPE; | |
3923 SAVECOMPILEWARNINGS(); | |
3924 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings); | |
3925 SAVEI32(PL_compiling.cop_seq); | |
3926 PL_compiling.cop_seq = 0; | |
3927 | |
3928 CALL_BLOCK_HOOKS(bhk_start, full); | |
3929 | |
3930 return retval; | |
3931 } | |
3932 | |
3933 /* | |
3934 =for apidoc Am|OP *|block_end|I32 floor|OP *seq | |
3935 | |
3936 Handles compile-time scope exit. I<floor> | |
3937 is the savestack index returned by | |
3938 C<block_start>, and I<seq> is the body of the block. Returns the block, | |
3939 possibly modified. | |
3940 | |
3941 =cut | |
3942 */ | |
3943 | |
3944 OP* | |
3945 Perl_block_end(pTHX_ I32 floor, OP *seq) | |
3946 { | |
3947 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE; | |
3948 OP* retval = scalarseq(seq); | |
3949 OP *o; | |
3950 | |
3951 /* XXX Is the null PL_parser check necessary here? */ | |
3952 assert(PL_parser); /* Let’s find out under debugging builds. */ | |
3953 if (PL_parser && PL_parser->parsed_sub) { | |
3954 o = newSTATEOP(0, NULL, NULL); | |
3955 op_null(o); | |
3956 retval = op_append_elem(OP_LINESEQ, retval, o); | |
3957 } | |
3958 | |
3959 CALL_BLOCK_HOOKS(bhk_pre_end, &retval); | |
3960 | |
3961 LEAVE_SCOPE(floor); | |
3962 if (needblockscope) | |
3963 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */ | |
3964 o = pad_leavemy(); | |
3965 | |
3966 if (o) { | |
3967 /* pad_leavemy has created a sequence of introcv ops for all my | |
3968 subs declared in the block. We have to replicate that list with | |
3969 clonecv ops, to deal with this situation: | |
3970 | |
3971 sub { | |
3972 my sub s1; | |
3973 my sub s2; | |
3974 sub s1 { state sub foo { \&s2 } } | |
3975 }->() | |
3976 | |
3977 Originally, I was going to have introcv clone the CV and turn | |
3978 off the stale flag. Since &s1 is declared before &s2, the | |
3979 introcv op for &s1 is executed (on sub entry) before the one for | |
3980 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is | |
3981 cloned, since it is a state sub) closes over &s2 and expects | |
3982 to see it in its outer CV’s pad. If the introcv op clones &s1, | |
3983 then &s2 is still marked stale. Since &s1 is not active, and | |
3984 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia- | |
3985 ble will not stay shared’ warning. Because it is the same stub | |
3986 that will be used when the introcv op for &s2 is executed, clos- | |
3987 ing over it is safe. Hence, we have to turn off the stale flag | |
3988 on all lexical subs in the block before we clone any of them. | |
3989 Hence, having introcv clone the sub cannot work. So we create a | |
3990 list of ops like this: | |
3991 | |
3992 lineseq | |
3993 | | |
3994 +-- introcv | |
3995 | | |
3996 +-- introcv | |
3997 | | |
3998 +-- introcv | |
3999 | | |
4000 . | |
4001 . | |
4002 . | |
4003 | | |
4004 +-- clonecv | |
4005 | | |
4006 +-- clonecv | |
4007 | | |
4008 +-- clonecv | |
4009 | | |
4010 . | |
4011 . | |
4012 . | |
4013 */ | |
4014 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o; | |
4015 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o; | |
4016 for (;; kid = OpSIBLING(kid)) { | |
4017 OP *newkid = newOP(OP_CLONECV, 0); | |
4018 newkid->op_targ = kid->op_targ; | |
4019 o = op_append_elem(OP_LINESEQ, o, newkid); | |
4020 if (kid == last) break; | |
4021 } | |
4022 retval = op_prepend_elem(OP_LINESEQ, o, retval); | |
4023 } | |
4024 | |
4025 CALL_BLOCK_HOOKS(bhk_post_end, &retval); | |
4026 | |
4027 return retval; | |
4028 } | |
4029 | |
4030 /* | |
4031 =head1 Compile-time scope hooks | |
4032 | |
4033 =for apidoc Aox||blockhook_register | |
4034 | |
4035 Register a set of hooks to be called when the Perl lexical scope changes | |
4036 at compile time. See L<perlguts/"Compile-time scope hooks">. | |
4037 | |
4038 =cut | |
4039 */ | |
4040 | |
4041 void | |
4042 Perl_blockhook_register(pTHX_ BHK *hk) | |
4043 { | |
4044 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER; | |
4045 | |
4046 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk))); | |
4047 } | |
4048 | |
4049 void | |
4050 Perl_newPROG(pTHX_ OP *o) | |
4051 { | |
4052 PERL_ARGS_ASSERT_NEWPROG; | |
4053 | |
4054 if (PL_in_eval) { | |
4055 PERL_CONTEXT *cx; | |
4056 I32 i; | |
4057 if (PL_eval_root) | |
4058 return; | |
4059 PL_eval_root = newUNOP(OP_LEAVEEVAL, | |
4060 ((PL_in_eval & EVAL_KEEPERR) | |
4061 ? OPf_SPECIAL : 0), o); | |
4062 | |
4063 cx = &cxstack[cxstack_ix]; | |
4064 assert(CxTYPE(cx) == CXt_EVAL); | |
4065 | |
4066 if ((cx->blk_gimme & G_WANT) == G_VOID) | |
4067 scalarvoid(PL_eval_root); | |
4068 else if ((cx->blk_gimme & G_WANT) == G_ARRAY) | |
4069 list(PL_eval_root); | |
4070 else | |
4071 scalar(PL_eval_root); | |
4072 | |
4073 PL_eval_start = op_linklist(PL_eval_root); | |
4074 PL_eval_root->op_private |= OPpREFCOUNTED; | |
4075 OpREFCNT_set(PL_eval_root, 1); | |
4076 PL_eval_root->op_next = 0; | |
4077 i = PL_savestack_ix; | |
4078 SAVEFREEOP(o); | |
4079 ENTER; | |
4080 CALL_PEEP(PL_eval_start); | |
4081 finalize_optree(PL_eval_root); | |
4082 S_prune_chain_head(&PL_eval_start); | |
4083 LEAVE; | |
4084 PL_savestack_ix = i; | |
4085 } | |
4086 else { | |
4087 if (o->op_type == OP_STUB) { | |
4088 /* This block is entered if nothing is compiled for the main | |
4089 program. This will be the case for an genuinely empty main | |
4090 program, or one which only has BEGIN blocks etc, so already | |
4091 run and freed. | |
4092 | |
4093 Historically (5.000) the guard above was !o. However, commit | |
4094 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as | |
4095 c71fccf11fde0068, changed perly.y so that newPROG() is now | |
4096 called with the output of block_end(), which returns a new | |
4097 OP_STUB for the case of an empty optree. ByteLoader (and | |
4098 maybe other things) also take this path, because they set up | |
4099 PL_main_start and PL_main_root directly, without generating an | |
4100 optree. | |
4101 | |
4102 If the parsing the main program aborts (due to parse errors, | |
4103 or due to BEGIN or similar calling exit), then newPROG() | |
4104 isn't even called, and hence this code path and its cleanups | |
4105 are skipped. This shouldn't make a make a difference: | |
4106 * a non-zero return from perl_parse is a failure, and | |
4107 perl_destruct() should be called immediately. | |
4108 * however, if exit(0) is called during the parse, then | |
4109 perl_parse() returns 0, and perl_run() is called. As | |
4110 PL_main_start will be NULL, perl_run() will return | |
4111 promptly, and the exit code will remain 0. | |
4112 */ | |
4113 | |
4114 PL_comppad_name = 0; | |
4115 PL_compcv = 0; | |
4116 S_op_destroy(aTHX_ o); | |
4117 return; | |
4118 } | |
4119 PL_main_root = op_scope(sawparens(scalarvoid(o))); | |
4120 PL_curcop = &PL_compiling; | |
4121 PL_main_start = LINKLIST(PL_main_root); | |
4122 PL_main_root->op_private |= OPpREFCOUNTED; | |
4123 OpREFCNT_set(PL_main_root, 1); | |
4124 PL_main_root->op_next = 0; | |
4125 CALL_PEEP(PL_main_start); | |
4126 finalize_optree(PL_main_root); | |
4127 S_prune_chain_head(&PL_main_start); | |
4128 cv_forget_slab(PL_compcv); | |
4129 PL_compcv = 0; | |
4130 | |
4131 /* Register with debugger */ | |
4132 if (PERLDB_INTER) { | |
4133 CV * const cv = get_cvs("DB::postponed", 0); | |
4134 if (cv) { | |
4135 dSP; | |
4136 PUSHMARK(SP); | |
4137 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling))); | |
4138 PUTBACK; | |
4139 call_sv(MUTABLE_SV(cv), G_DISCARD); | |
4140 } | |
4141 } | |
4142 } | |
4143 } | |
4144 | |
4145 OP * | |
4146 Perl_localize(pTHX_ OP *o, I32 lex) | |
4147 { | |
4148 PERL_ARGS_ASSERT_LOCALIZE; | |
4149 | |
4150 if (o->op_flags & OPf_PARENS) | |
4151 /* [perl #17376]: this appears to be premature, and results in code such as | |
4152 C< our(%x); > executing in list mode rather than void mode */ | |
4153 #if 0 | |
4154 list(o); | |
4155 #else | |
4156 NOOP; | |
4157 #endif | |
4158 else { | |
4159 if ( PL_parser->bufptr > PL_parser->oldbufptr | |
4160 && PL_parser->bufptr[-1] == ',' | |
4161 && ckWARN(WARN_PARENTHESIS)) | |
4162 { | |
4163 char *s = PL_parser->bufptr; | |
4164 bool sigil = FALSE; | |
4165 | |
4166 /* some heuristics to detect a potential error */ | |
4167 while (*s && (strchr(", \t\n", *s))) | |
4168 s++; | |
4169 | |
4170 while (1) { | |
4171 if (*s && strchr("@$%*", *s) && *++s | |
4172 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) { | |
4173 s++; | |
4174 sigil = TRUE; | |
4175 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) | |
4176 s++; | |
4177 while (*s && (strchr(", \t\n", *s))) | |
4178 s++; | |
4179 } | |
4180 else | |
4181 break; | |
4182 } | |
4183 if (sigil && (*s == ';' || *s == '=')) { | |
4184 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS), | |
4185 "Parentheses missing around \"%s\" list", | |
4186 lex | |
4187 ? (PL_parser->in_my == KEY_our | |
4188 ? "our" | |
4189 : PL_parser->in_my == KEY_state | |
4190 ? "state" | |
4191 : "my") | |
4192 : "local"); | |
4193 } | |
4194 } | |
4195 } | |
4196 if (lex) | |
4197 o = my(o); | |
4198 else | |
4199 o = op_lvalue(o, OP_NULL); /* a bit kludgey */ | |
4200 PL_parser->in_my = FALSE; | |
4201 PL_parser->in_my_stash = NULL; | |
4202 return o; | |
4203 } | |
4204 | |
4205 OP * | |
4206 Perl_jmaybe(pTHX_ OP *o) | |
4207 { | |
4208 PERL_ARGS_ASSERT_JMAYBE; | |
4209 | |
4210 if (o->op_type == OP_LIST) { | |
4211 OP * const o2 | |
4212 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV))); | |
4213 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o)); | |
4214 } | |
4215 return o; | |
4216 } | |
4217 | |
4218 PERL_STATIC_INLINE OP * | |
4219 S_op_std_init(pTHX_ OP *o) | |
4220 { | |
4221 I32 type = o->op_type; | |
4222 | |
4223 PERL_ARGS_ASSERT_OP_STD_INIT; | |
4224 | |
4225 if (PL_opargs[type] & OA_RETSCALAR) | |
4226 scalar(o); | |
4227 if (PL_opargs[type] & OA_TARGET && !o->op_targ) | |
4228 o->op_targ = pad_alloc(type, SVs_PADTMP); | |
4229 | |
4230 return o; | |
4231 } | |
4232 | |
4233 PERL_STATIC_INLINE OP * | |
4234 S_op_integerize(pTHX_ OP *o) | |
4235 { | |
4236 I32 type = o->op_type; | |
4237 | |
4238 PERL_ARGS_ASSERT_OP_INTEGERIZE; | |
4239 | |
4240 /* integerize op. */ | |
4241 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)) | |
4242 { | |
4243 dVAR; | |
4244 o->op_ppaddr = PL_ppaddr[++(o->op_type)]; | |
4245 } | |
4246 | |
4247 if (type == OP_NEGATE) | |
4248 /* XXX might want a ck_negate() for this */ | |
4249 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT; | |
4250 | |
4251 return o; | |
4252 } | |
4253 | |
4254 static OP * | |
4255 S_fold_constants(pTHX_ OP *o) | |
4256 { | |
4257 dVAR; | |
4258 OP * VOL curop; | |
4259 OP *newop; | |
4260 VOL I32 type = o->op_type; | |
4261 bool is_stringify; | |
4262 SV * VOL sv = NULL; | |
4263 int ret = 0; | |
4264 I32 oldscope; | |
4265 OP *old_next; | |
4266 SV * const oldwarnhook = PL_warnhook; | |
4267 SV * const olddiehook = PL_diehook; | |
4268 COP not_compiling; | |
4269 U8 oldwarn = PL_dowarn; | |
4270 dJMPENV; | |
4271 | |
4272 PERL_ARGS_ASSERT_FOLD_CONSTANTS; | |
4273 | |
4274 if (!(PL_opargs[type] & OA_FOLDCONST)) | |
4275 goto nope; | |
4276 | |
4277 switch (type) { | |
4278 case OP_UCFIRST: | |
4279 case OP_LCFIRST: | |
4280 case OP_UC: | |
4281 case OP_LC: | |
4282 case OP_FC: | |
4283 #ifdef USE_LOCALE_CTYPE | |
4284 if (IN_LC_COMPILETIME(LC_CTYPE)) | |
4285 goto nope; | |
4286 #endif | |
4287 break; | |
4288 case OP_SLT: | |
4289 case OP_SGT: | |
4290 case OP_SLE: | |
4291 case OP_SGE: | |
4292 case OP_SCMP: | |
4293 #ifdef USE_LOCALE_COLLATE | |
4294 if (IN_LC_COMPILETIME(LC_COLLATE)) | |
4295 goto nope; | |
4296 #endif | |
4297 break; | |
4298 case OP_SPRINTF: | |
4299 /* XXX what about the numeric ops? */ | |
4300 #ifdef USE_LOCALE_NUMERIC | |
4301 if (IN_LC_COMPILETIME(LC_NUMERIC)) | |
4302 goto nope; | |
4303 #endif | |
4304 break; | |
4305 case OP_PACK: | |
4306 if (!OpHAS_SIBLING(cLISTOPo->op_first) | |
4307 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST) | |
4308 goto nope; | |
4309 { | |
4310 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first)); | |
4311 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope; | |
4312 { | |
4313 const char *s = SvPVX_const(sv); | |
4314 while (s < SvEND(sv)) { | |
4315 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope; | |
4316 s++; | |
4317 } | |
4318 } | |
4319 } | |
4320 break; | |
4321 case OP_REPEAT: | |
4322 if (o->op_private & OPpREPEAT_DOLIST) goto nope; | |
4323 break; | |
4324 case OP_SREFGEN: | |
4325 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST | |
4326 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first))) | |
4327 goto nope; | |
4328 } | |
4329 | |
4330 if (PL_parser && PL_parser->error_count) | |
4331 goto nope; /* Don't try to run w/ errors */ | |
4332 | |
4333 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { | |
4334 const OPCODE type = curop->op_type; | |
4335 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) && | |
4336 type != OP_LIST && | |
4337 type != OP_SCALAR && | |
4338 type != OP_NULL && | |
4339 type != OP_PUSHMARK) | |
4340 { | |
4341 goto nope; | |
4342 } | |
4343 } | |
4344 | |
4345 curop = LINKLIST(o); | |
4346 old_next = o->op_next; | |
4347 o->op_next = 0; | |
4348 PL_op = curop; | |
4349 | |
4350 oldscope = PL_scopestack_ix; | |
4351 create_eval_scope(G_FAKINGEVAL); | |
4352 | |
4353 /* Verify that we don't need to save it: */ | |
4354 assert(PL_curcop == &PL_compiling); | |
4355 StructCopy(&PL_compiling, ¬_compiling, COP); | |
4356 PL_curcop = ¬_compiling; | |
4357 /* The above ensures that we run with all the correct hints of the | |
4358 currently compiling COP, but that IN_PERL_RUNTIME is true. */ | |
4359 assert(IN_PERL_RUNTIME); | |
4360 PL_warnhook = PERL_WARNHOOK_FATAL; | |
4361 PL_diehook = NULL; | |
4362 JMPENV_PUSH(ret); | |
4363 | |
4364 /* Effective $^W=1. */ | |
4365 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) | |
4366 PL_dowarn |= G_WARN_ON; | |
4367 | |
4368 switch (ret) { | |
4369 case 0: | |
4370 CALLRUNOPS(aTHX); | |
4371 sv = *(PL_stack_sp--); | |
4372 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */ | |
4373 pad_swipe(o->op_targ, FALSE); | |
4374 } | |
4375 else if (SvTEMP(sv)) { /* grab mortal temp? */ | |
4376 SvREFCNT_inc_simple_void(sv); | |
4377 SvTEMP_off(sv); | |
4378 } | |
4379 else { assert(SvIMMORTAL(sv)); } | |
4380 break; | |
4381 case 3: | |
4382 /* Something tried to die. Abandon constant folding. */ | |
4383 /* Pretend the error never happened. */ | |
4384 CLEAR_ERRSV(); | |
4385 o->op_next = old_next; | |
4386 break; | |
4387 default: | |
4388 JMPENV_POP; | |
4389 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */ | |
4390 PL_warnhook = oldwarnhook; | |
4391 PL_diehook = olddiehook; | |
4392 /* XXX note that this croak may fail as we've already blown away | |
4393 * the stack - eg any nested evals */ | |
4394 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret); | |
4395 } | |
4396 JMPENV_POP; | |
4397 PL_dowarn = oldwarn; | |
4398 PL_warnhook = oldwarnhook; | |
4399 PL_diehook = olddiehook; | |
4400 PL_curcop = &PL_compiling; | |
4401 | |
4402 if (PL_scopestack_ix > oldscope) | |
4403 delete_eval_scope(); | |
4404 | |
4405 if (ret) | |
4406 goto nope; | |
4407 | |
4408 /* OP_STRINGIFY and constant folding are used to implement qq. | |
4409 Here the constant folding is an implementation detail that we | |
4410 want to hide. If the stringify op is itself already marked | |
4411 folded, however, then it is actually a folded join. */ | |
4412 is_stringify = type == OP_STRINGIFY && !o->op_folded; | |
4413 op_free(o); | |
4414 assert(sv); | |
4415 if (is_stringify) | |
4416 SvPADTMP_off(sv); | |
4417 else if (!SvIMMORTAL(sv)) { | |
4418 SvPADTMP_on(sv); | |
4419 SvREADONLY_on(sv); | |
4420 } | |
4421 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv)); | |
4422 if (!is_stringify) newop->op_folded = 1; | |
4423 return newop; | |
4424 | |
4425 nope: | |
4426 return o; | |
4427 } | |
4428 | |
4429 static OP * | |
4430 S_gen_constant_list(pTHX_ OP *o) | |
4431 { | |
4432 dVAR; | |
4433 OP *curop; | |
4434 const SSize_t oldtmps_floor = PL_tmps_floor; | |
4435 SV **svp; | |
4436 AV *av; | |
4437 | |
4438 list(o); | |
4439 if (PL_parser && PL_parser->error_count) | |
4440 return o; /* Don't attempt to run with errors */ | |
4441 | |
4442 curop = LINKLIST(o); | |
4443 o->op_next = 0; | |
4444 CALL_PEEP(curop); | |
4445 S_prune_chain_head(&curop); | |
4446 PL_op = curop; | |
4447 Perl_pp_pushmark(aTHX); | |
4448 CALLRUNOPS(aTHX); | |
4449 PL_op = curop; | |
4450 assert (!(curop->op_flags & OPf_SPECIAL)); | |
4451 assert(curop->op_type == OP_RANGE); | |
4452 Perl_pp_anonlist(aTHX); | |
4453 PL_tmps_floor = oldtmps_floor; | |
4454 | |
4455 OpTYPE_set(o, OP_RV2AV); | |
4456 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */ | |
4457 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */ | |
4458 o->op_opt = 0; /* needs to be revisited in rpeep() */ | |
4459 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--); | |
4460 | |
4461 /* replace subtree with an OP_CONST */ | |
4462 curop = ((UNOP*)o)->op_first; | |
4463 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av)); | |
4464 op_free(curop); | |
4465 | |
4466 if (AvFILLp(av) != -1) | |
4467 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp) | |
4468 { | |
4469 SvPADTMP_on(*svp); | |
4470 SvREADONLY_on(*svp); | |
4471 } | |
4472 LINKLIST(o); | |
4473 return list(o); | |
4474 } | |
4475 | |
4476 /* | |
4477 =head1 Optree Manipulation Functions | |
4478 */ | |
4479 | |
4480 /* List constructors */ | |
4481 | |
4482 /* | |
4483 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last | |
4484 | |
4485 Append an item to the list of ops contained directly within a list-type | |
4486 op, returning the lengthened list. I<first> is the list-type op, | |
4487 and I<last> is the op to append to the list. I<optype> specifies the | |
4488 intended opcode for the list. If I<first> is not already a list of the | |
4489 right type, it will be upgraded into one. If either I<first> or I<last> | |
4490 is null, the other is returned unchanged. | |
4491 | |
4492 =cut | |
4493 */ | |
4494 | |
4495 OP * | |
4496 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last) | |
4497 { | |
4498 if (!first) | |
4499 return last; | |
4500 | |
4501 if (!last) | |
4502 return first; | |
4503 | |
4504 if (first->op_type != (unsigned)type | |
4505 || (type == OP_LIST && (first->op_flags & OPf_PARENS))) | |
4506 { | |
4507 return newLISTOP(type, 0, first, last); | |
4508 } | |
4509 | |
4510 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last); | |
4511 first->op_flags |= OPf_KIDS; | |
4512 return first; | |
4513 } | |
4514 | |
4515 /* | |
4516 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last | |
4517 | |
4518 Concatenate the lists of ops contained directly within two list-type ops, | |
4519 returning the combined list. I<first> and I<last> are the list-type ops | |
4520 to concatenate. I<optype> specifies the intended opcode for the list. | |
4521 If either I<first> or I<last> is not already a list of the right type, | |
4522 it will be upgraded into one. If either I<first> or I<last> is null, | |
4523 the other is returned unchanged. | |
4524 | |
4525 =cut | |
4526 */ | |
4527 | |
4528 OP * | |
4529 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last) | |
4530 { | |
4531 if (!first) | |
4532 return last; | |
4533 | |
4534 if (!last) | |
4535 return first; | |
4536 | |
4537 if (first->op_type != (unsigned)type) | |
4538 return op_prepend_elem(type, first, last); | |
4539 | |
4540 if (last->op_type != (unsigned)type) | |
4541 return op_append_elem(type, first, last); | |
4542 | |
4543 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first); | |
4544 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last; | |
4545 OpLASTSIB_set(((LISTOP*)first)->op_last, first); | |
4546 first->op_flags |= (last->op_flags & OPf_KIDS); | |
4547 | |
4548 S_op_destroy(aTHX_ last); | |
4549 | |
4550 return first; | |
4551 } | |
4552 | |
4553 /* | |
4554 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last | |
4555 | |
4556 Prepend an item to the list of ops contained directly within a list-type | |
4557 op, returning the lengthened list. I<first> is the op to prepend to the | |
4558 list, and I<last> is the list-type op. I<optype> specifies the intended | |
4559 opcode for the list. If I<last> is not already a list of the right type, | |
4560 it will be upgraded into one. If either I<first> or I<last> is null, | |
4561 the other is returned unchanged. | |
4562 | |
4563 =cut | |
4564 */ | |
4565 | |
4566 OP * | |
4567 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last) | |
4568 { | |
4569 if (!first) | |
4570 return last; | |
4571 | |
4572 if (!last) | |
4573 return first; | |
4574 | |
4575 if (last->op_type == (unsigned)type) { | |
4576 if (type == OP_LIST) { /* already a PUSHMARK there */ | |
4577 /* insert 'first' after pushmark */ | |
4578 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first); | |
4579 if (!(first->op_flags & OPf_PARENS)) | |
4580 last->op_flags &= ~OPf_PARENS; | |
4581 } | |
4582 else | |
4583 op_sibling_splice(last, NULL, 0, first); | |
4584 last->op_flags |= OPf_KIDS; | |
4585 return last; | |
4586 } | |
4587 | |
4588 return newLISTOP(type, 0, first, last); | |
4589 } | |
4590 | |
4591 /* | |
4592 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o | |
4593 | |
4594 Converts I<o> into a list op if it is not one already, and then converts it | |
4595 into the specified I<type>, calling its check function, allocating a target if | |
4596 it needs one, and folding constants. | |
4597 | |
4598 A list-type op is usually constructed one kid at a time via C<newLISTOP>, | |
4599 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to | |
4600 C<op_convert_list> to make it the right type. | |
4601 | |
4602 =cut | |
4603 */ | |
4604 | |
4605 OP * | |
4606 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o) | |
4607 { | |
4608 dVAR; | |
4609 if (type < 0) type = -type, flags |= OPf_SPECIAL; | |
4610 if (!o || o->op_type != OP_LIST) | |
4611 o = force_list(o, 0); | |
4612 else | |
4613 { | |
4614 o->op_flags &= ~OPf_WANT; | |
4615 o->op_private &= ~OPpLVAL_INTRO; | |
4616 } | |
4617 | |
4618 if (!(PL_opargs[type] & OA_MARK)) | |
4619 op_null(cLISTOPo->op_first); | |
4620 else { | |
4621 OP * const kid2 = OpSIBLING(cLISTOPo->op_first); | |
4622 if (kid2 && kid2->op_type == OP_COREARGS) { | |
4623 op_null(cLISTOPo->op_first); | |
4624 kid2->op_private |= OPpCOREARGS_PUSHMARK; | |
4625 } | |
4626 } | |
4627 | |
4628 OpTYPE_set(o, type); | |
4629 o->op_flags |= flags; | |
4630 if (flags & OPf_FOLDED) | |
4631 o->op_folded = 1; | |
4632 | |
4633 o = CHECKOP(type, o); | |
4634 if (o->op_type != (unsigned)type) | |
4635 return o; | |
4636 | |
4637 return fold_constants(op_integerize(op_std_init(o))); | |
4638 } | |
4639 | |
4640 /* Constructors */ | |
4641 | |
4642 | |
4643 /* | |
4644 =head1 Optree construction | |
4645 | |
4646 =for apidoc Am|OP *|newNULLLIST | |
4647 | |
4648 Constructs, checks, and returns a new C<stub> op, which represents an | |
4649 empty list expression. | |
4650 | |
4651 =cut | |
4652 */ | |
4653 | |
4654 OP * | |
4655 Perl_newNULLLIST(pTHX) | |
4656 { | |
4657 return newOP(OP_STUB, 0); | |
4658 } | |
4659 | |
4660 /* promote o and any siblings to be a list if its not already; i.e. | |
4661 * | |
4662 * o - A - B | |
4663 * | |
4664 * becomes | |
4665 * | |
4666 * list | |
4667 * | | |
4668 * pushmark - o - A - B | |
4669 * | |
4670 * If nullit it true, the list op is nulled. | |
4671 */ | |
4672 | |
4673 static OP * | |
4674 S_force_list(pTHX_ OP *o, bool nullit) | |
4675 { | |
4676 if (!o || o->op_type != OP_LIST) { | |
4677 OP *rest = NULL; | |
4678 if (o) { | |
4679 /* manually detach any siblings then add them back later */ | |
4680 rest = OpSIBLING(o); | |
4681 OpLASTSIB_set(o, NULL); | |
4682 } | |
4683 o = newLISTOP(OP_LIST, 0, o, NULL); | |
4684 if (rest) | |
4685 op_sibling_splice(o, cLISTOPo->op_last, 0, rest); | |
4686 } | |
4687 if (nullit) | |
4688 op_null(o); | |
4689 return o; | |
4690 } | |
4691 | |
4692 /* | |
4693 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last | |
4694 | |
4695 Constructs, checks, and returns an op of any list type. I<type> is | |
4696 the opcode. I<flags> gives the eight bits of C<op_flags>, except that | |
4697 C<OPf_KIDS> will be set automatically if required. I<first> and I<last> | |
4698 supply up to two ops to be direct children of the list op; they are | |
4699 consumed by this function and become part of the constructed op tree. | |
4700 | |
4701 For most list operators, the check function expects all the kid ops to be | |
4702 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not | |
4703 appropriate. What you want to do in that case is create an op of type | |
4704 OP_LIST, append more children to it, and then call L</op_convert_list>. | |
4705 See L</op_convert_list> for more information. | |
4706 | |
4707 | |
4708 =cut | |
4709 */ | |
4710 | |
4711 OP * | |
4712 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) | |
4713 { | |
4714 dVAR; | |
4715 LISTOP *listop; | |
4716 | |
4717 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP | |
4718 || type == OP_CUSTOM); | |
4719 | |
4720 NewOp(1101, listop, 1, LISTOP); | |
4721 | |
4722 OpTYPE_set(listop, type); | |
4723 if (first || last) | |
4724 flags |= OPf_KIDS; | |
4725 listop->op_flags = (U8)flags; | |
4726 | |
4727 if (!last && first) | |
4728 last = first; | |
4729 else if (!first && last) | |
4730 first = last; | |
4731 else if (first) | |
4732 OpMORESIB_set(first, last); | |
4733 listop->op_first = first; | |
4734 listop->op_last = last; | |
4735 if (type == OP_LIST) { | |
4736 OP* const pushop = newOP(OP_PUSHMARK, 0); | |
4737 OpMORESIB_set(pushop, first); | |
4738 listop->op_first = pushop; | |
4739 listop->op_flags |= OPf_KIDS; | |
4740 if (!last) | |
4741 listop->op_last = pushop; | |
4742 } | |
4743 if (listop->op_last) | |
4744 OpLASTSIB_set(listop->op_last, (OP*)listop); | |
4745 | |
4746 return CHECKOP(type, listop); | |
4747 } | |
4748 | |
4749 /* | |
4750 =for apidoc Am|OP *|newOP|I32 type|I32 flags | |
4751 | |
4752 Constructs, checks, and returns an op of any base type (any type that | |
4753 has no extra fields). I<type> is the opcode. I<flags> gives the | |
4754 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits | |
4755 of C<op_private>. | |
4756 | |
4757 =cut | |
4758 */ | |
4759 | |
4760 OP * | |
4761 Perl_newOP(pTHX_ I32 type, I32 flags) | |
4762 { | |
4763 dVAR; | |
4764 OP *o; | |
4765 | |
4766 if (type == -OP_ENTEREVAL) { | |
4767 type = OP_ENTEREVAL; | |
4768 flags |= OPpEVAL_BYTES<<8; | |
4769 } | |
4770 | |
4771 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP | |
4772 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP | |
4773 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP | |
4774 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); | |
4775 | |
4776 NewOp(1101, o, 1, OP); | |
4777 OpTYPE_set(o, type); | |
4778 o->op_flags = (U8)flags; | |
4779 | |
4780 o->op_next = o; | |
4781 o->op_private = (U8)(0 | (flags >> 8)); | |
4782 if (PL_opargs[type] & OA_RETSCALAR) | |
4783 scalar(o); | |
4784 if (PL_opargs[type] & OA_TARGET) | |
4785 o->op_targ = pad_alloc(type, SVs_PADTMP); | |
4786 return CHECKOP(type, o); | |
4787 } | |
4788 | |
4789 /* | |
4790 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first | |
4791 | |
4792 Constructs, checks, and returns an op of any unary type. I<type> is | |
4793 the opcode. I<flags> gives the eight bits of C<op_flags>, except that | |
4794 C<OPf_KIDS> will be set automatically if required, and, shifted up eight | |
4795 bits, the eight bits of C<op_private>, except that the bit with value 1 | |
4796 is automatically set. I<first> supplies an optional op to be the direct | |
4797 child of the unary op; it is consumed by this function and become part | |
4798 of the constructed op tree. | |
4799 | |
4800 =cut | |
4801 */ | |
4802 | |
4803 OP * | |
4804 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first) | |
4805 { | |
4806 dVAR; | |
4807 UNOP *unop; | |
4808 | |
4809 if (type == -OP_ENTEREVAL) { | |
4810 type = OP_ENTEREVAL; | |
4811 flags |= OPpEVAL_BYTES<<8; | |
4812 } | |
4813 | |
4814 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP | |
4815 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP | |
4816 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP | |
4817 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP | |
4818 || type == OP_SASSIGN | |
4819 || type == OP_ENTERTRY | |
4820 || type == OP_CUSTOM | |
4821 || type == OP_NULL ); | |
4822 | |
4823 if (!first) | |
4824 first = newOP(OP_STUB, 0); | |
4825 if (PL_opargs[type] & OA_MARK) | |
4826 first = force_list(first, 1); | |
4827 | |
4828 NewOp(1101, unop, 1, UNOP); | |
4829 OpTYPE_set(unop, type); | |
4830 unop->op_first = first; | |
4831 unop->op_flags = (U8)(flags | OPf_KIDS); | |
4832 unop->op_private = (U8)(1 | (flags >> 8)); | |
4833 | |
4834 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */ | |
4835 OpLASTSIB_set(first, (OP*)unop); | |
4836 | |
4837 unop = (UNOP*) CHECKOP(type, unop); | |
4838 if (unop->op_next) | |
4839 return (OP*)unop; | |
4840 | |
4841 return fold_constants(op_integerize(op_std_init((OP *) unop))); | |
4842 } | |
4843 | |
4844 /* | |
4845 =for apidoc newUNOP_AUX | |
4846 | |
4847 Similar to C<newUNOP>, but creates an UNOP_AUX struct instead, with op_aux | |
4848 initialised to aux | |
4849 | |
4850 =cut | |
4851 */ | |
4852 | |
4853 OP * | |
4854 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux) | |
4855 { | |
4856 dVAR; | |
4857 UNOP_AUX *unop; | |
4858 | |
4859 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX | |
4860 || type == OP_CUSTOM); | |
4861 | |
4862 NewOp(1101, unop, 1, UNOP_AUX); | |
4863 unop->op_type = (OPCODE)type; | |
4864 unop->op_ppaddr = PL_ppaddr[type]; | |
4865 unop->op_first = first; | |
4866 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0)); | |
4867 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8)); | |
4868 unop->op_aux = aux; | |
4869 | |
4870 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */ | |
4871 OpLASTSIB_set(first, (OP*)unop); | |
4872 | |
4873 unop = (UNOP_AUX*) CHECKOP(type, unop); | |
4874 | |
4875 return op_std_init((OP *) unop); | |
4876 } | |
4877 | |
4878 /* | |
4879 =for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first | |
4880 | |
4881 Constructs, checks, and returns an op of method type with a method name | |
4882 evaluated at runtime. I<type> is the opcode. I<flags> gives the eight | |
4883 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically, | |
4884 and, shifted up eight bits, the eight bits of C<op_private>, except that | |
4885 the bit with value 1 is automatically set. I<dynamic_meth> supplies an | |
4886 op which evaluates method name; it is consumed by this function and | |
4887 become part of the constructed op tree. | |
4888 Supported optypes: OP_METHOD. | |
4889 | |
4890 =cut | |
4891 */ | |
4892 | |
4893 static OP* | |
4894 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) { | |
4895 dVAR; | |
4896 METHOP *methop; | |
4897 | |
4898 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP | |
4899 || type == OP_CUSTOM); | |
4900 | |
4901 NewOp(1101, methop, 1, METHOP); | |
4902 if (dynamic_meth) { | |
4903 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1); | |
4904 methop->op_flags = (U8)(flags | OPf_KIDS); | |
4905 methop->op_u.op_first = dynamic_meth; | |
4906 methop->op_private = (U8)(1 | (flags >> 8)); | |
4907 | |
4908 if (!OpHAS_SIBLING(dynamic_meth)) | |
4909 OpLASTSIB_set(dynamic_meth, (OP*)methop); | |
4910 } | |
4911 else { | |
4912 assert(const_meth); | |
4913 methop->op_flags = (U8)(flags & ~OPf_KIDS); | |
4914 methop->op_u.op_meth_sv = const_meth; | |
4915 methop->op_private = (U8)(0 | (flags >> 8)); | |
4916 methop->op_next = (OP*)methop; | |
4917 } | |
4918 | |
4919 #ifdef USE_ITHREADS | |
4920 methop->op_rclass_targ = 0; | |
4921 #else | |
4922 methop->op_rclass_sv = NULL; | |
4923 #endif | |
4924 | |
4925 OpTYPE_set(methop, type); | |
4926 return CHECKOP(type, methop); | |
4927 } | |
4928 | |
4929 OP * | |
4930 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) { | |
4931 PERL_ARGS_ASSERT_NEWMETHOP; | |
4932 return newMETHOP_internal(type, flags, dynamic_meth, NULL); | |
4933 } | |
4934 | |
4935 /* | |
4936 =for apidoc Am|OP *|newMETHOP_named|I32 type|I32 flags|SV *const_meth | |
4937 | |
4938 Constructs, checks, and returns an op of method type with a constant | |
4939 method name. I<type> is the opcode. I<flags> gives the eight bits of | |
4940 C<op_flags>, and, shifted up eight bits, the eight bits of | |
4941 C<op_private>. I<const_meth> supplies a constant method name; | |
4942 it must be a shared COW string. | |
4943 Supported optypes: OP_METHOD_NAMED. | |
4944 | |
4945 =cut | |
4946 */ | |
4947 | |
4948 OP * | |
4949 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) { | |
4950 PERL_ARGS_ASSERT_NEWMETHOP_NAMED; | |
4951 return newMETHOP_internal(type, flags, NULL, const_meth); | |
4952 } | |
4953 | |
4954 /* | |
4955 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last | |
4956 | |
4957 Constructs, checks, and returns an op of any binary type. I<type> | |
4958 is the opcode. I<flags> gives the eight bits of C<op_flags>, except | |
4959 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits, | |
4960 the eight bits of C<op_private>, except that the bit with value 1 or | |
4961 2 is automatically set as required. I<first> and I<last> supply up to | |
4962 two ops to be the direct children of the binary op; they are consumed | |
4963 by this function and become part of the constructed op tree. | |
4964 | |
4965 =cut | |
4966 */ | |
4967 | |
4968 OP * | |
4969 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) | |
4970 { | |
4971 dVAR; | |
4972 BINOP *binop; | |
4973 | |
4974 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP | |
4975 || type == OP_SASSIGN || type == OP_NULL || type == OP_CUSTOM); | |
4976 | |
4977 NewOp(1101, binop, 1, BINOP); | |
4978 | |
4979 if (!first) | |
4980 first = newOP(OP_NULL, 0); | |
4981 | |
4982 OpTYPE_set(binop, type); | |
4983 binop->op_first = first; | |
4984 binop->op_flags = (U8)(flags | OPf_KIDS); | |
4985 if (!last) { | |
4986 last = first; | |
4987 binop->op_private = (U8)(1 | (flags >> 8)); | |
4988 } | |
4989 else { | |
4990 binop->op_private = (U8)(2 | (flags >> 8)); | |
4991 OpMORESIB_set(first, last); | |
4992 } | |
4993 | |
4994 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */ | |
4995 OpLASTSIB_set(last, (OP*)binop); | |
4996 | |
4997 binop->op_last = OpSIBLING(binop->op_first); | |
4998 if (binop->op_last) | |
4999 OpLASTSIB_set(binop->op_last, (OP*)binop); | |
5000 | |
5001 binop = (BINOP*)CHECKOP(type, binop); | |
5002 if (binop->op_next || binop->op_type != (OPCODE)type) | |
5003 return (OP*)binop; | |
5004 | |
5005 return fold_constants(op_integerize(op_std_init((OP *)binop))); | |
5006 } | |
5007 | |
5008 static int uvcompare(const void *a, const void *b) | |
5009 __attribute__nonnull__(1) | |
5010 __attribute__nonnull__(2) | |
5011 __attribute__pure__; | |
5012 static int uvcompare(const void *a, const void *b) | |
5013 { | |
5014 if (*((const UV *)a) < (*(const UV *)b)) | |
5015 return -1; | |
5016 if (*((const UV *)a) > (*(const UV *)b)) | |
5017 return 1; | |
5018 if (*((const UV *)a+1) < (*(const UV *)b+1)) | |
5019 return -1; | |
5020 if (*((const UV *)a+1) > (*(const UV *)b+1)) | |
5021 return 1; | |
5022 return 0; | |
5023 } | |
5024 | |
5025 static OP * | |
5026 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) | |
5027 { | |
5028 SV * const tstr = ((SVOP*)expr)->op_sv; | |
5029 SV * const rstr = | |
5030 ((SVOP*)repl)->op_sv; | |
5031 STRLEN tlen; | |
5032 STRLEN rlen; | |
5033 const U8 *t = (U8*)SvPV_const(tstr, tlen); | |
5034 const U8 *r = (U8*)SvPV_const(rstr, rlen); | |
5035 I32 i; | |
5036 I32 j; | |
5037 I32 grows = 0; | |
5038 short *tbl; | |
5039 | |
5040 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT; | |
5041 const I32 squash = o->op_private & OPpTRANS_SQUASH; | |
5042 I32 del = o->op_private & OPpTRANS_DELETE; | |
5043 SV* swash; | |
5044 | |
5045 PERL_ARGS_ASSERT_PMTRANS; | |
5046 | |
5047 PL_hints |= HINT_BLOCK_SCOPE; | |
5048 | |
5049 if (SvUTF8(tstr)) | |
5050 o->op_private |= OPpTRANS_FROM_UTF; | |
5051 | |
5052 if (SvUTF8(rstr)) | |
5053 o->op_private |= OPpTRANS_TO_UTF; | |
5054 | |
5055 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { | |
5056 SV* const listsv = newSVpvs("# comment\n"); | |
5057 SV* transv = NULL; | |
5058 const U8* tend = t + tlen; | |
5059 const U8* rend = r + rlen; | |
5060 STRLEN ulen; | |
5061 UV tfirst = 1; | |
5062 UV tlast = 0; | |
5063 IV tdiff; | |
5064 STRLEN tcount = 0; | |
5065 UV rfirst = 1; | |
5066 UV rlast = 0; | |
5067 IV rdiff; | |
5068 STRLEN rcount = 0; | |
5069 IV diff; | |
5070 I32 none = 0; | |
5071 U32 max = 0; | |
5072 I32 bits; | |
5073 I32 havefinal = 0; | |
5074 U32 final = 0; | |
5075 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF; | |
5076 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF; | |
5077 U8* tsave = NULL; | |
5078 U8* rsave = NULL; | |
5079 const U32 flags = UTF8_ALLOW_DEFAULT; | |
5080 | |
5081 if (!from_utf) { | |
5082 STRLEN len = tlen; | |
5083 t = tsave = bytes_to_utf8(t, &len); | |
5084 tend = t + len; | |
5085 } | |
5086 if (!to_utf && rlen) { | |
5087 STRLEN len = rlen; | |
5088 r = rsave = bytes_to_utf8(r, &len); | |
5089 rend = r + len; | |
5090 } | |
5091 | |
5092 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has | |
5093 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255 | |
5094 * odd. */ | |
5095 | |
5096 if (complement) { | |
5097 U8 tmpbuf[UTF8_MAXBYTES+1]; | |
5098 UV *cp; | |
5099 UV nextmin = 0; | |
5100 Newx(cp, 2*tlen, UV); | |
5101 i = 0; | |
5102 transv = newSVpvs(""); | |
5103 while (t < tend) { | |
5104 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags); | |
5105 t += ulen; | |
5106 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { | |
5107 t++; | |
5108 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags); | |
5109 t += ulen; | |
5110 } | |
5111 else { | |
5112 cp[2*i+1] = cp[2*i]; | |
5113 } | |
5114 i++; | |
5115 } | |
5116 qsort(cp, i, 2*sizeof(UV), uvcompare); | |
5117 for (j = 0; j < i; j++) { | |
5118 UV val = cp[2*j]; | |
5119 diff = val - nextmin; | |
5120 if (diff > 0) { | |
5121 t = uvchr_to_utf8(tmpbuf,nextmin); | |
5122 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); | |
5123 if (diff > 1) { | |
5124 U8 range_mark = ILLEGAL_UTF8_BYTE; | |
5125 t = uvchr_to_utf8(tmpbuf, val - 1); | |
5126 sv_catpvn(transv, (char *)&range_mark, 1); | |
5127 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); | |
5128 } | |
5129 } | |
5130 val = cp[2*j+1]; | |
5131 if (val >= nextmin) | |
5132 nextmin = val + 1; | |
5133 } | |
5134 t = uvchr_to_utf8(tmpbuf,nextmin); | |
5135 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); | |
5136 { | |
5137 U8 range_mark = ILLEGAL_UTF8_BYTE; | |
5138 sv_catpvn(transv, (char *)&range_mark, 1); | |
5139 } | |
5140 t = uvchr_to_utf8(tmpbuf, 0x7fffffff); | |
5141 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); | |
5142 t = (const U8*)SvPVX_const(transv); | |
5143 tlen = SvCUR(transv); | |
5144 tend = t + tlen; | |
5145 Safefree(cp); | |
5146 } | |
5147 else if (!rlen && !del) { | |
5148 r = t; rlen = tlen; rend = tend; | |
5149 } | |
5150 if (!squash) { | |
5151 if ((!rlen && !del) || t == r || | |
5152 (tlen == rlen && memEQ((char *)t, (char *)r, tlen))) | |
5153 { | |
5154 o->op_private |= OPpTRANS_IDENTICAL; | |
5155 } | |
5156 } | |
5157 | |
5158 while (t < tend || tfirst <= tlast) { | |
5159 /* see if we need more "t" chars */ | |
5160 if (tfirst > tlast) { | |
5161 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags); | |
5162 t += ulen; | |
5163 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */ | |
5164 t++; | |
5165 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags); | |
5166 t += ulen; | |
5167 } | |
5168 else | |
5169 tlast = tfirst; | |
5170 } | |
5171 | |
5172 /* now see if we need more "r" chars */ | |
5173 if (rfirst > rlast) { | |
5174 if (r < rend) { | |
5175 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags); | |
5176 r += ulen; | |
5177 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */ | |
5178 r++; | |
5179 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags); | |
5180 r += ulen; | |
5181 } | |
5182 else | |
5183 rlast = rfirst; | |
5184 } | |
5185 else { | |
5186 if (!havefinal++) | |
5187 final = rlast; | |
5188 rfirst = rlast = 0xffffffff; | |
5189 } | |
5190 } | |
5191 | |
5192 /* now see which range will peter our first, if either. */ | |
5193 tdiff = tlast - tfirst; | |
5194 rdiff = rlast - rfirst; | |
5195 tcount += tdiff + 1; | |
5196 rcount += rdiff + 1; | |
5197 | |
5198 if (tdiff <= rdiff) | |
5199 diff = tdiff; | |
5200 else | |
5201 diff = rdiff; | |
5202 | |
5203 if (rfirst == 0xffffffff) { | |
5204 diff = tdiff; /* oops, pretend rdiff is infinite */ | |
5205 if (diff > 0) | |
5206 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n", | |
5207 (long)tfirst, (long)tlast); | |
5208 else | |
5209 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst); | |
5210 } | |
5211 else { | |
5212 if (diff > 0) | |
5213 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n", | |
5214 (long)tfirst, (long)(tfirst + diff), | |
5215 (long)rfirst); | |
5216 else | |
5217 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n", | |
5218 (long)tfirst, (long)rfirst); | |
5219 | |
5220 if (rfirst + diff > max) | |
5221 max = rfirst + diff; | |
5222 if (!grows) | |
5223 grows = (tfirst < rfirst && | |
5224 UNISKIP(tfirst) < UNISKIP(rfirst + diff)); | |
5225 rfirst += diff + 1; | |
5226 } | |
5227 tfirst += diff + 1; | |
5228 } | |
5229 | |
5230 none = ++max; | |
5231 if (del) | |
5232 del = ++max; | |
5233 | |
5234 if (max > 0xffff) | |
5235 bits = 32; | |
5236 else if (max > 0xff) | |
5237 bits = 16; | |
5238 else | |
5239 bits = 8; | |
5240 | |
5241 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none)); | |
5242 #ifdef USE_ITHREADS | |
5243 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY); | |
5244 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix)); | |
5245 PAD_SETSV(cPADOPo->op_padix, swash); | |
5246 SvPADTMP_on(swash); | |
5247 SvREADONLY_on(swash); | |
5248 #else | |
5249 cSVOPo->op_sv = swash; | |
5250 #endif | |
5251 SvREFCNT_dec(listsv); | |
5252 SvREFCNT_dec(transv); | |
5253 | |
5254 if (!del && havefinal && rlen) | |
5255 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5, | |
5256 newSVuv((UV)final), 0); | |
5257 | |
5258 Safefree(tsave); | |
5259 Safefree(rsave); | |
5260 | |
5261 tlen = tcount; | |
5262 rlen = rcount; | |
5263 if (r < rend) | |
5264 rlen++; | |
5265 else if (rlast == 0xffffffff) | |
5266 rlen = 0; | |
5267 | |
5268 goto warnins; | |
5269 } | |
5270 | |
5271 tbl = (short*)PerlMemShared_calloc( | |
5272 (o->op_private & OPpTRANS_COMPLEMENT) && | |
5273 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256, | |
5274 sizeof(short)); | |
5275 cPVOPo->op_pv = (char*)tbl; | |
5276 if (complement) { | |
5277 for (i = 0; i < (I32)tlen; i++) | |
5278 tbl[t[i]] = -1; | |
5279 for (i = 0, j = 0; i < 256; i++) { | |
5280 if (!tbl[i]) { | |
5281 if (j >= (I32)rlen) { | |
5282 if (del) | |
5283 tbl[i] = -2; | |
5284 else if (rlen) | |
5285 tbl[i] = r[j-1]; | |
5286 else | |
5287 tbl[i] = (short)i; | |
5288 } | |
5289 else { | |
5290 if (i < 128 && r[j] >= 128) | |
5291 grows = 1; | |
5292 tbl[i] = r[j++]; | |
5293 } | |
5294 } | |
5295 } | |
5296 if (!del) { | |
5297 if (!rlen) { | |
5298 j = rlen; | |
5299 if (!squash) | |
5300 o->op_private |= OPpTRANS_IDENTICAL; | |
5301 } | |
5302 else if (j >= (I32)rlen) | |
5303 j = rlen - 1; | |
5304 else { | |
5305 tbl = | |
5306 (short *) | |
5307 PerlMemShared_realloc(tbl, | |
5308 (0x101+rlen-j) * sizeof(short)); | |
5309 cPVOPo->op_pv = (char*)tbl; | |
5310 } | |
5311 tbl[0x100] = (short)(rlen - j); | |
5312 for (i=0; i < (I32)rlen - j; i++) | |
5313 tbl[0x101+i] = r[j+i]; | |
5314 } | |
5315 } | |
5316 else { | |
5317 if (!rlen && !del) { | |
5318 r = t; rlen = tlen; | |
5319 if (!squash) | |
5320 o->op_private |= OPpTRANS_IDENTICAL; | |
5321 } | |
5322 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) { | |
5323 o->op_private |= OPpTRANS_IDENTICAL; | |
5324 } | |
5325 for (i = 0; i < 256; i++) | |
5326 tbl[i] = -1; | |
5327 for (i = 0, j = 0; i < (I32)tlen; i++,j++) { | |
5328 if (j >= (I32)rlen) { | |
5329 if (del) { | |
5330 if (tbl[t[i]] == -1) | |
5331 tbl[t[i]] = -2; | |
5332 continue; | |
5333 } | |
5334 --j; | |
5335 } | |
5336 if (tbl[t[i]] == -1) { | |
5337 if (t[i] < 128 && r[j] >= 128) | |
5338 grows = 1; | |
5339 tbl[t[i]] = r[j]; | |
5340 } | |
5341 } | |
5342 } | |
5343 | |
5344 warnins: | |
5345 if(del && rlen == tlen) { | |
5346 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); | |
5347 } else if(rlen > tlen && !complement) { | |
5348 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list"); | |
5349 } | |
5350 | |
5351 if (grows) | |
5352 o->op_private |= OPpTRANS_GROWS; | |
5353 op_free(expr); | |
5354 op_free(repl); | |
5355 | |
5356 return o; | |
5357 } | |
5358 | |
5359 /* | |
5360 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags | |
5361 | |
5362 Constructs, checks, and returns an op of any pattern matching type. | |
5363 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags> | |
5364 and, shifted up eight bits, the eight bits of C<op_private>. | |
5365 | |
5366 =cut | |
5367 */ | |
5368 | |
5369 OP * | |
5370 Perl_newPMOP(pTHX_ I32 type, I32 flags) | |
5371 { | |
5372 dVAR; | |
5373 PMOP *pmop; | |
5374 | |
5375 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP | |
5376 || type == OP_CUSTOM); | |
5377 | |
5378 NewOp(1101, pmop, 1, PMOP); | |
5379 OpTYPE_set(pmop, type); | |
5380 pmop->op_flags = (U8)flags; | |
5381 pmop->op_private = (U8)(0 | (flags >> 8)); | |
5382 if (PL_opargs[type] & OA_RETSCALAR) | |
5383 scalar((OP *)pmop); | |
5384 | |
5385 if (PL_hints & HINT_RE_TAINT) | |
5386 pmop->op_pmflags |= PMf_RETAINT; | |
5387 #ifdef USE_LOCALE_CTYPE | |
5388 if (IN_LC_COMPILETIME(LC_CTYPE)) { | |
5389 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET); | |
5390 } | |
5391 else | |
5392 #endif | |
5393 if (IN_UNI_8_BIT) { | |
5394 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET); | |
5395 } | |
5396 if (PL_hints & HINT_RE_FLAGS) { | |
5397 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_ | |
5398 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0 | |
5399 ); | |
5400 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags); | |
5401 reflags = Perl_refcounted_he_fetch_pvn(aTHX_ | |
5402 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0 | |
5403 ); | |
5404 if (reflags && SvOK(reflags)) { | |
5405 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags)); | |
5406 } | |
5407 } | |
5408 | |
5409 | |
5410 #ifdef USE_ITHREADS | |
5411 assert(SvPOK(PL_regex_pad[0])); | |
5412 if (SvCUR(PL_regex_pad[0])) { | |
5413 /* Pop off the "packed" IV from the end. */ | |
5414 SV *const repointer_list = PL_regex_pad[0]; | |
5415 const char *p = SvEND(repointer_list) - sizeof(IV); | |
5416 const IV offset = *((IV*)p); | |
5417 | |
5418 assert(SvCUR(repointer_list) % sizeof(IV) == 0); | |
5419 | |
5420 SvEND_set(repointer_list, p); | |
5421 | |
5422 pmop->op_pmoffset = offset; | |
5423 /* This slot should be free, so assert this: */ | |
5424 assert(PL_regex_pad[offset] == &PL_sv_undef); | |
5425 } else { | |
5426 SV * const repointer = &PL_sv_undef; | |
5427 av_push(PL_regex_padav, repointer); | |
5428 pmop->op_pmoffset = av_tindex(PL_regex_padav); | |
5429 PL_regex_pad = AvARRAY(PL_regex_padav); | |
5430 } | |
5431 #endif | |
5432 | |
5433 return CHECKOP(type, pmop); | |
5434 } | |
5435 | |
5436 static void | |
5437 S_set_haseval(pTHX) | |
5438 { | |
5439 PADOFFSET i = 1; | |
5440 PL_cv_has_eval = 1; | |
5441 /* Any pad names in scope are potentially lvalues. */ | |
5442 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) { | |
5443 PADNAME *pn = PAD_COMPNAME_SV(i); | |
5444 if (!pn || !PadnameLEN(pn)) | |
5445 continue; | |
5446 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax)) | |
5447 S_mark_padname_lvalue(aTHX_ pn); | |
5448 } | |
5449 } | |
5450 | |
5451 /* Given some sort of match op o, and an expression expr containing a | |
5452 * pattern, either compile expr into a regex and attach it to o (if it's | |
5453 * constant), or convert expr into a runtime regcomp op sequence (if it's | |
5454 * not) | |
5455 * | |
5456 * isreg indicates that the pattern is part of a regex construct, eg | |
5457 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or | |
5458 * split "pattern", which aren't. In the former case, expr will be a list | |
5459 * if the pattern contains more than one term (eg /a$b/). | |
5460 * | |
5461 * When the pattern has been compiled within a new anon CV (for | |
5462 * qr/(?{...})/ ), then floor indicates the savestack level just before | |
5463 * the new sub was created | |
5464 */ | |
5465 | |
5466 OP * | |
5467 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, bool isreg, I32 floor) | |
5468 { | |
5469 PMOP *pm; | |
5470 LOGOP *rcop; | |
5471 I32 repl_has_vars = 0; | |
5472 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR); | |
5473 bool is_compiletime; | |
5474 bool has_code; | |
5475 | |
5476 PERL_ARGS_ASSERT_PMRUNTIME; | |
5477 | |
5478 if (is_trans) { | |
5479 return pmtrans(o, expr, repl); | |
5480 } | |
5481 | |
5482 /* find whether we have any runtime or code elements; | |
5483 * at the same time, temporarily set the op_next of each DO block; | |
5484 * then when we LINKLIST, this will cause the DO blocks to be excluded | |
5485 * from the op_next chain (and from having LINKLIST recursively | |
5486 * applied to them). We fix up the DOs specially later */ | |
5487 | |
5488 is_compiletime = 1; | |
5489 has_code = 0; | |
5490 if (expr->op_type == OP_LIST) { | |
5491 OP *o; | |
5492 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) { | |
5493 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) { | |
5494 has_code = 1; | |
5495 assert(!o->op_next); | |
5496 if (UNLIKELY(!OpHAS_SIBLING(o))) { | |
5497 assert(PL_parser && PL_parser->error_count); | |
5498 /* This can happen with qr/ (?{(^{})/. Just fake up | |
5499 the op we were expecting to see, to avoid crashing | |
5500 elsewhere. */ | |
5501 op_sibling_splice(expr, o, 0, | |
5502 newSVOP(OP_CONST, 0, &PL_sv_no)); | |
5503 } | |
5504 o->op_next = OpSIBLING(o); | |
5505 } | |
5506 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK) | |
5507 is_compiletime = 0; | |
5508 } | |
5509 } | |
5510 else if (expr->op_type != OP_CONST) | |
5511 is_compiletime = 0; | |
5512 | |
5513 LINKLIST(expr); | |
5514 | |
5515 /* fix up DO blocks; treat each one as a separate little sub; | |
5516 * also, mark any arrays as LIST/REF */ | |
5517 | |
5518 if (expr->op_type == OP_LIST) { | |
5519 OP *o; | |
5520 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) { | |
5521 | |
5522 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) { | |
5523 assert( !(o->op_flags & OPf_WANT)); | |
5524 /* push the array rather than its contents. The regex | |
5525 * engine will retrieve and join the elements later */ | |
5526 o->op_flags |= (OPf_WANT_LIST | OPf_REF); | |
5527 continue; | |
5528 } | |
5529 | |
5530 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL))) | |
5531 continue; | |
5532 o->op_next = NULL; /* undo temporary hack from above */ | |
5533 scalar(o); | |
5534 LINKLIST(o); | |
5535 if (cLISTOPo->op_first->op_type == OP_LEAVE) { | |
5536 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first); | |
5537 /* skip ENTER */ | |
5538 assert(leaveop->op_first->op_type == OP_ENTER); | |
5539 assert(OpHAS_SIBLING(leaveop->op_first)); | |
5540 o->op_next = OpSIBLING(leaveop->op_first); | |
5541 /* skip leave */ | |
5542 assert(leaveop->op_flags & OPf_KIDS); | |
5543 assert(leaveop->op_last->op_next == (OP*)leaveop); | |
5544 leaveop->op_next = NULL; /* stop on last op */ | |
5545 op_null((OP*)leaveop); | |
5546 } | |
5547 else { | |
5548 /* skip SCOPE */ | |
5549 OP *scope = cLISTOPo->op_first; | |
5550 assert(scope->op_type == OP_SCOPE); | |
5551 assert(scope->op_flags & OPf_KIDS); | |
5552 scope->op_next = NULL; /* stop on last op */ | |
5553 op_null(scope); | |
5554 } | |
5555 /* have to peep the DOs individually as we've removed it from | |
5556 * the op_next chain */ | |
5557 CALL_PEEP(o); | |
5558 S_prune_chain_head(&(o->op_next)); | |
5559 if (is_compiletime) | |
5560 /* runtime finalizes as part of finalizing whole tree */ | |
5561 finalize_optree(o); | |
5562 } | |
5563 } | |
5564 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) { | |
5565 assert( !(expr->op_flags & OPf_WANT)); | |
5566 /* push the array rather than its contents. The regex | |
5567 * engine will retrieve and join the elements later */ | |
5568 expr->op_flags |= (OPf_WANT_LIST | OPf_REF); | |
5569 } | |
5570 | |
5571 PL_hints |= HINT_BLOCK_SCOPE; | |
5572 pm = (PMOP*)o; | |
5573 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV)); | |
5574 | |
5575 if (is_compiletime) { | |
5576 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME; | |
5577 regexp_engine const *eng = current_re_engine(); | |
5578 | |
5579 if (o->op_flags & OPf_SPECIAL) | |
5580 rx_flags |= RXf_SPLIT; | |
5581 | |
5582 if (!has_code || !eng->op_comp) { | |
5583 /* compile-time simple constant pattern */ | |
5584 | |
5585 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) { | |
5586 /* whoops! we guessed that a qr// had a code block, but we | |
5587 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv | |
5588 * that isn't required now. Note that we have to be pretty | |
5589 * confident that nothing used that CV's pad while the | |
5590 * regex was parsed, except maybe op targets for \Q etc. | |
5591 * If there were any op targets, though, they should have | |
5592 * been stolen by constant folding. | |
5593 */ | |
5594 #ifdef DEBUGGING | |
5595 SSize_t i = 0; | |
5596 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0); | |
5597 while (++i <= AvFILLp(PL_comppad)) { | |
5598 assert(!PL_curpad[i]); | |
5599 } | |
5600 #endif | |
5601 /* But we know that one op is using this CV's slab. */ | |
5602 cv_forget_slab(PL_compcv); | |
5603 LEAVE_SCOPE(floor); | |
5604 pm->op_pmflags &= ~PMf_HAS_CV; | |
5605 } | |
5606 | |
5607 PM_SETRE(pm, | |
5608 eng->op_comp | |
5609 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL, | |
5610 rx_flags, pm->op_pmflags) | |
5611 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL, | |
5612 rx_flags, pm->op_pmflags) | |
5613 ); | |
5614 op_free(expr); | |
5615 } | |
5616 else { | |
5617 /* compile-time pattern that includes literal code blocks */ | |
5618 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL, | |
5619 rx_flags, | |
5620 (pm->op_pmflags | | |
5621 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0)) | |
5622 ); | |
5623 PM_SETRE(pm, re); | |
5624 if (pm->op_pmflags & PMf_HAS_CV) { | |
5625 CV *cv; | |
5626 /* this QR op (and the anon sub we embed it in) is never | |
5627 * actually executed. It's just a placeholder where we can | |
5628 * squirrel away expr in op_code_list without the peephole | |
5629 * optimiser etc processing it for a second time */ | |
5630 OP *qr = newPMOP(OP_QR, 0); | |
5631 ((PMOP*)qr)->op_code_list = expr; | |
5632 | |
5633 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */ | |
5634 SvREFCNT_inc_simple_void(PL_compcv); | |
5635 cv = newATTRSUB(floor, 0, NULL, NULL, qr); | |
5636 ReANY(re)->qr_anoncv = cv; | |
5637 | |
5638 /* attach the anon CV to the pad so that | |
5639 * pad_fixup_inner_anons() can find it */ | |
5640 (void)pad_add_anon(cv, o->op_type); | |
5641 SvREFCNT_inc_simple_void(cv); | |
5642 } | |
5643 else { | |
5644 pm->op_code_list = expr; | |
5645 } | |
5646 } | |
5647 } | |
5648 else { | |
5649 /* runtime pattern: build chain of regcomp etc ops */ | |
5650 bool reglist; | |
5651 PADOFFSET cv_targ = 0; | |
5652 | |
5653 reglist = isreg && expr->op_type == OP_LIST; | |
5654 if (reglist) | |
5655 op_null(expr); | |
5656 | |
5657 if (has_code) { | |
5658 pm->op_code_list = expr; | |
5659 /* don't free op_code_list; its ops are embedded elsewhere too */ | |
5660 pm->op_pmflags |= PMf_CODELIST_PRIVATE; | |
5661 } | |
5662 | |
5663 if (o->op_flags & OPf_SPECIAL) | |
5664 pm->op_pmflags |= PMf_SPLIT; | |
5665 | |
5666 /* the OP_REGCMAYBE is a placeholder in the non-threaded case | |
5667 * to allow its op_next to be pointed past the regcomp and | |
5668 * preceding stacking ops; | |
5669 * OP_REGCRESET is there to reset taint before executing the | |
5670 * stacking ops */ | |
5671 if (pm->op_pmflags & PMf_KEEP || TAINTING_get) | |
5672 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr); | |
5673 | |
5674 if (pm->op_pmflags & PMf_HAS_CV) { | |
5675 /* we have a runtime qr with literal code. This means | |
5676 * that the qr// has been wrapped in a new CV, which | |
5677 * means that runtime consts, vars etc will have been compiled | |
5678 * against a new pad. So... we need to execute those ops | |
5679 * within the environment of the new CV. So wrap them in a call | |
5680 * to a new anon sub. i.e. for | |
5681 * | |
5682 * qr/a$b(?{...})/, | |
5683 * | |
5684 * we build an anon sub that looks like | |
5685 * | |
5686 * sub { "a", $b, '(?{...})' } | |
5687 * | |
5688 * and call it, passing the returned list to regcomp. | |
5689 * Or to put it another way, the list of ops that get executed | |
5690 * are: | |
5691 * | |
5692 * normal PMf_HAS_CV | |
5693 * ------ ------------------- | |
5694 * pushmark (for regcomp) | |
5695 * pushmark (for entersub) | |
5696 * anoncode | |
5697 * srefgen | |
5698 * entersub | |
5699 * regcreset regcreset | |
5700 * pushmark pushmark | |
5701 * const("a") const("a") | |
5702 * gvsv(b) gvsv(b) | |
5703 * const("(?{...})") const("(?{...})") | |
5704 * leavesub | |
5705 * regcomp regcomp | |
5706 */ | |
5707 | |
5708 SvREFCNT_inc_simple_void(PL_compcv); | |
5709 CvLVALUE_on(PL_compcv); | |
5710 /* these lines are just an unrolled newANONATTRSUB */ | |
5711 expr = newSVOP(OP_ANONCODE, 0, | |
5712 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr))); | |
5713 cv_targ = expr->op_targ; | |
5714 expr = newUNOP(OP_REFGEN, 0, expr); | |
5715 | |
5716 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), 1)); | |
5717 } | |
5718 | |
5719 rcop = S_alloc_LOGOP(aTHX_ OP_REGCOMP, scalar(expr), o); | |
5720 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0) | |
5721 | (reglist ? OPf_STACKED : 0); | |
5722 rcop->op_targ = cv_targ; | |
5723 | |
5724 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */ | |
5725 if (PL_hints & HINT_RE_EVAL) | |
5726 S_set_haseval(aTHX); | |
5727 | |
5728 /* establish postfix order */ | |
5729 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) { | |
5730 LINKLIST(expr); | |
5731 rcop->op_next = expr; | |
5732 ((UNOP*)expr)->op_first->op_next = (OP*)rcop; | |
5733 } | |
5734 else { | |
5735 rcop->op_next = LINKLIST(expr); | |
5736 expr->op_next = (OP*)rcop; | |
5737 } | |
5738 | |
5739 op_prepend_elem(o->op_type, scalar((OP*)rcop), o); | |
5740 } | |
5741 | |
5742 if (repl) { | |
5743 OP *curop = repl; | |
5744 bool konst; | |
5745 /* If we are looking at s//.../e with a single statement, get past | |
5746 the implicit do{}. */ | |
5747 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS | |
5748 && cUNOPx(curop)->op_first->op_type == OP_SCOPE | |
5749 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS) | |
5750 { | |
5751 OP *sib; | |
5752 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first; | |
5753 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid)) | |
5754 && !OpHAS_SIBLING(sib)) | |
5755 curop = sib; | |
5756 } | |
5757 if (curop->op_type == OP_CONST) | |
5758 konst = TRUE; | |
5759 else if (( (curop->op_type == OP_RV2SV || | |
5760 curop->op_type == OP_RV2AV || | |
5761 curop->op_type == OP_RV2HV || | |
5762 curop->op_type == OP_RV2GV) | |
5763 && cUNOPx(curop)->op_first | |
5764 && cUNOPx(curop)->op_first->op_type == OP_GV ) | |
5765 || curop->op_type == OP_PADSV | |
5766 || curop->op_type == OP_PADAV | |
5767 || curop->op_type == OP_PADHV | |
5768 || curop->op_type == OP_PADANY) { | |
5769 repl_has_vars = 1; | |
5770 konst = TRUE; | |
5771 } | |
5772 else konst = FALSE; | |
5773 if (konst | |
5774 && !(repl_has_vars | |
5775 && (!PM_GETRE(pm) | |
5776 || !RX_PRELEN(PM_GETRE(pm)) | |
5777 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN))) | |
5778 { | |
5779 pm->op_pmflags |= PMf_CONST; /* const for long enough */ | |
5780 op_prepend_elem(o->op_type, scalar(repl), o); | |
5781 } | |
5782 else { | |
5783 rcop = S_alloc_LOGOP(aTHX_ OP_SUBSTCONT, scalar(repl), o); | |
5784 rcop->op_private = 1; | |
5785 | |
5786 /* establish postfix order */ | |
5787 rcop->op_next = LINKLIST(repl); | |
5788 repl->op_next = (OP*)rcop; | |
5789 | |
5790 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop); | |
5791 assert(!(pm->op_pmflags & PMf_ONCE)); | |
5792 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop); | |
5793 rcop->op_next = 0; | |
5794 } | |
5795 } | |
5796 | |
5797 return (OP*)pm; | |
5798 } | |
5799 | |
5800 /* | |
5801 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv | |
5802 | |
5803 Constructs, checks, and returns an op of any type that involves an | |
5804 embedded SV. I<type> is the opcode. I<flags> gives the eight bits | |
5805 of C<op_flags>. I<sv> gives the SV to embed in the op; this function | |
5806 takes ownership of one reference to it. | |
5807 | |
5808 =cut | |
5809 */ | |
5810 | |
5811 OP * | |
5812 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv) | |
5813 { | |
5814 dVAR; | |
5815 SVOP *svop; | |
5816 | |
5817 PERL_ARGS_ASSERT_NEWSVOP; | |
5818 | |
5819 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP | |
5820 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP | |
5821 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP | |
5822 || type == OP_CUSTOM); | |
5823 | |
5824 NewOp(1101, svop, 1, SVOP); | |
5825 OpTYPE_set(svop, type); | |
5826 svop->op_sv = sv; | |
5827 svop->op_next = (OP*)svop; | |
5828 svop->op_flags = (U8)flags; | |
5829 svop->op_private = (U8)(0 | (flags >> 8)); | |
5830 if (PL_opargs[type] & OA_RETSCALAR) | |
5831 scalar((OP*)svop); | |
5832 if (PL_opargs[type] & OA_TARGET) | |
5833 svop->op_targ = pad_alloc(type, SVs_PADTMP); | |
5834 return CHECKOP(type, svop); | |
5835 } | |
5836 | |
5837 /* | |
5838 =for apidoc Am|OP *|newDEFSVOP| | |
5839 | |
5840 Constructs and returns an op to access C<$_>, either as a lexical | |
5841 variable (if declared as C<my $_>) in the current scope, or the | |
5842 global C<$_>. | |
5843 | |
5844 =cut | |
5845 */ | |
5846 | |
5847 OP * | |
5848 Perl_newDEFSVOP(pTHX) | |
5849 { | |
5850 const PADOFFSET offset = pad_findmy_pvs("$_", 0); | |
5851 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) { | |
5852 return newSVREF(newGVOP(OP_GV, 0, PL_defgv)); | |
5853 } | |
5854 else { | |
5855 OP * const o = newOP(OP_PADSV, 0); | |
5856 o->op_targ = offset; | |
5857 return o; | |
5858 } | |
5859 } | |
5860 | |
5861 #ifdef USE_ITHREADS | |
5862 | |
5863 /* | |
5864 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv | |
5865 | |
5866 Constructs, checks, and returns an op of any type that involves a | |
5867 reference to a pad element. I<type> is the opcode. I<flags> gives the | |
5868 eight bits of C<op_flags>. A pad slot is automatically allocated, and | |
5869 is populated with I<sv>; this function takes ownership of one reference | |
5870 to it. | |
5871 | |
5872 This function only exists if Perl has been compiled to use ithreads. | |
5873 | |
5874 =cut | |
5875 */ | |
5876 | |
5877 OP * | |
5878 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv) | |
5879 { | |
5880 dVAR; | |
5881 PADOP *padop; | |
5882 | |
5883 PERL_ARGS_ASSERT_NEWPADOP; | |
5884 | |
5885 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP | |
5886 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP | |
5887 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP | |
5888 || type == OP_CUSTOM); | |
5889 | |
5890 NewOp(1101, padop, 1, PADOP); | |
5891 OpTYPE_set(padop, type); | |
5892 padop->op_padix = | |
5893 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP); | |
5894 SvREFCNT_dec(PAD_SVl(padop->op_padix)); | |
5895 PAD_SETSV(padop->op_padix, sv); | |
5896 assert(sv); | |
5897 padop->op_next = (OP*)padop; | |
5898 padop->op_flags = (U8)flags; | |
5899 if (PL_opargs[type] & OA_RETSCALAR) | |
5900 scalar((OP*)padop); | |
5901 if (PL_opargs[type] & OA_TARGET) | |
5902 padop->op_targ = pad_alloc(type, SVs_PADTMP); | |
5903 return CHECKOP(type, padop); | |
5904 } | |
5905 | |
5906 #endif /* USE_ITHREADS */ | |
5907 | |
5908 /* | |
5909 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv | |
5910 | |
5911 Constructs, checks, and returns an op of any type that involves an | |
5912 embedded reference to a GV. I<type> is the opcode. I<flags> gives the | |
5913 eight bits of C<op_flags>. I<gv> identifies the GV that the op should | |
5914 reference; calling this function does not transfer ownership of any | |
5915 reference to it. | |
5916 | |
5917 =cut | |
5918 */ | |
5919 | |
5920 OP * | |
5921 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv) | |
5922 { | |
5923 PERL_ARGS_ASSERT_NEWGVOP; | |
5924 | |
5925 #ifdef USE_ITHREADS | |
5926 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv)); | |
5927 #else | |
5928 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv)); | |
5929 #endif | |
5930 } | |
5931 | |
5932 /* | |
5933 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv | |
5934 | |
5935 Constructs, checks, and returns an op of any type that involves an | |
5936 embedded C-level pointer (PV). I<type> is the opcode. I<flags> gives | |
5937 the eight bits of C<op_flags>. I<pv> supplies the C-level pointer, which | |
5938 must have been allocated using C<PerlMemShared_malloc>; the memory will | |
5939 be freed when the op is destroyed. | |
5940 | |
5941 =cut | |
5942 */ | |
5943 | |
5944 OP * | |
5945 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv) | |
5946 { | |
5947 dVAR; | |
5948 const bool utf8 = cBOOL(flags & SVf_UTF8); | |
5949 PVOP *pvop; | |
5950 | |
5951 flags &= ~SVf_UTF8; | |
5952 | |
5953 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP | |
5954 || type == OP_RUNCV || type == OP_CUSTOM | |
5955 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); | |
5956 | |
5957 NewOp(1101, pvop, 1, PVOP); | |
5958 OpTYPE_set(pvop, type); | |
5959 pvop->op_pv = pv; | |
5960 pvop->op_next = (OP*)pvop; | |
5961 pvop->op_flags = (U8)flags; | |
5962 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0; | |
5963 if (PL_opargs[type] & OA_RETSCALAR) | |
5964 scalar((OP*)pvop); | |
5965 if (PL_opargs[type] & OA_TARGET) | |
5966 pvop->op_targ = pad_alloc(type, SVs_PADTMP); | |
5967 return CHECKOP(type, pvop); | |
5968 } | |
5969 | |
5970 void | |
5971 Perl_package(pTHX_ OP *o) | |
5972 { | |
5973 SV *const sv = cSVOPo->op_sv; | |
5974 | |
5975 PERL_ARGS_ASSERT_PACKAGE; | |
5976 | |
5977 SAVEGENERICSV(PL_curstash); | |
5978 save_item(PL_curstname); | |
5979 | |
5980 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD)); | |
5981 | |
5982 sv_setsv(PL_curstname, sv); | |
5983 | |
5984 PL_hints |= HINT_BLOCK_SCOPE; | |
5985 PL_parser->copline = NOLINE; | |
5986 | |
5987 op_free(o); | |
5988 } | |
5989 | |
5990 void | |
5991 Perl_package_version( pTHX_ OP *v ) | |
5992 { | |
5993 U32 savehints = PL_hints; | |
5994 PERL_ARGS_ASSERT_PACKAGE_VERSION; | |
5995 PL_hints &= ~HINT_STRICT_VARS; | |
5996 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv ); | |
5997 PL_hints = savehints; | |
5998 op_free(v); | |
5999 } | |
6000 | |
6001 void | |
6002 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg) | |
6003 { | |
6004 OP *pack; | |
6005 OP *imop; | |
6006 OP *veop; | |
6007 SV *use_version = NULL; | |
6008 | |
6009 PERL_ARGS_ASSERT_UTILIZE; | |
6010 | |
6011 if (idop->op_type != OP_CONST) | |
6012 Perl_croak(aTHX_ "Module name must be constant"); | |
6013 | |
6014 veop = NULL; | |
6015 | |
6016 if (version) { | |
6017 SV * const vesv = ((SVOP*)version)->op_sv; | |
6018 | |
6019 if (!arg && !SvNIOKp(vesv)) { | |
6020 arg = version; | |
6021 } | |
6022 else { | |
6023 OP *pack; | |
6024 SV *meth; | |
6025 | |
6026 if (version->op_type != OP_CONST || !SvNIOKp(vesv)) | |
6027 Perl_croak(aTHX_ "Version number must be a constant number"); | |
6028 | |
6029 /* Make copy of idop so we don't free it twice */ | |
6030 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); | |
6031 | |
6032 /* Fake up a method call to VERSION */ | |
6033 meth = newSVpvs_share("VERSION"); | |
6034 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, | |
6035 op_append_elem(OP_LIST, | |
6036 op_prepend_elem(OP_LIST, pack, version), | |
6037 newMETHOP_named(OP_METHOD_NAMED, 0, meth))); | |
6038 } | |
6039 } | |
6040 | |
6041 /* Fake up an import/unimport */ | |
6042 if (arg && arg->op_type == OP_STUB) { | |
6043 imop = arg; /* no import on explicit () */ | |
6044 } | |
6045 else if (SvNIOKp(((SVOP*)idop)->op_sv)) { | |
6046 imop = NULL; /* use 5.0; */ | |
6047 if (aver) | |
6048 use_version = ((SVOP*)idop)->op_sv; | |
6049 else | |
6050 idop->op_private |= OPpCONST_NOVER; | |
6051 } | |
6052 else { | |
6053 SV *meth; | |
6054 | |
6055 /* Make copy of idop so we don't free it twice */ | |
6056 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); | |
6057 | |
6058 /* Fake up a method call to import/unimport */ | |
6059 meth = aver | |
6060 ? newSVpvs_share("import") : newSVpvs_share("unimport"); | |
6061 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, | |
6062 op_append_elem(OP_LIST, | |
6063 op_prepend_elem(OP_LIST, pack, arg), | |
6064 newMETHOP_named(OP_METHOD_NAMED, 0, meth) | |
6065 )); | |
6066 } | |
6067 | |
6068 /* Fake up the BEGIN {}, which does its thing immediately. */ | |
6069 newATTRSUB(floor, | |
6070 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")), | |
6071 NULL, | |
6072 NULL, | |
6073 op_append_elem(OP_LINESEQ, | |
6074 op_append_elem(OP_LINESEQ, | |
6075 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)), | |
6076 newSTATEOP(0, NULL, veop)), | |
6077 newSTATEOP(0, NULL, imop) )); | |
6078 | |
6079 if (use_version) { | |
6080 /* Enable the | |
6081 * feature bundle that corresponds to the required version. */ | |
6082 use_version = sv_2mortal(new_version(use_version)); | |
6083 S_enable_feature_bundle(aTHX_ use_version); | |
6084 | |
6085 /* If a version >= 5.11.0 is requested, strictures are on by default! */ | |
6086 if (vcmp(use_version, | |
6087 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) { | |
6088 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS)) | |
6089 PL_hints |= HINT_STRICT_REFS; | |
6090 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS)) | |
6091 PL_hints |= HINT_STRICT_SUBS; | |
6092 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS)) | |
6093 PL_hints |= HINT_STRICT_VARS; | |
6094 } | |
6095 /* otherwise they are off */ | |
6096 else { | |
6097 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS)) | |
6098 PL_hints &= ~HINT_STRICT_REFS; | |
6099 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS)) | |
6100 PL_hints &= ~HINT_STRICT_SUBS; | |
6101 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS)) | |
6102 PL_hints &= ~HINT_STRICT_VARS; | |
6103 } | |
6104 } | |
6105 | |
6106 /* The "did you use incorrect case?" warning used to be here. | |
6107 * The problem is that on case-insensitive filesystems one | |
6108 * might get false positives for "use" (and "require"): | |
6109 * "use Strict" or "require CARP" will work. This causes | |
6110 * portability problems for the script: in case-strict | |
6111 * filesystems the script will stop working. | |
6112 * | |
6113 * The "incorrect case" warning checked whether "use Foo" | |
6114 * imported "Foo" to your namespace, but that is wrong, too: | |
6115 * there is no requirement nor promise in the language that | |
6116 * a Foo.pm should or would contain anything in package "Foo". | |
6117 * | |
6118 * There is very little Configure-wise that can be done, either: | |
6119 * the case-sensitivity of the build filesystem of Perl does not | |
6120 * help in guessing the case-sensitivity of the runtime environment. | |
6121 */ | |
6122 | |
6123 PL_hints |= HINT_BLOCK_SCOPE; | |
6124 PL_parser->copline = NOLINE; | |
6125 COP_SEQMAX_INC; /* Purely for B::*'s benefit */ | |
6126 } | |
6127 | |
6128 /* | |
6129 =head1 Embedding Functions | |
6130 | |
6131 =for apidoc load_module | |
6132 | |
6133 Loads the module whose name is pointed to by the string part of name. | |
6134 Note that the actual module name, not its filename, should be given. | |
6135 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of | |
6136 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS | |
6137 (or 0 for no flags). ver, if specified | |
6138 and not NULL, provides version semantics | |
6139 similar to C<use Foo::Bar VERSION>. The optional trailing SV* | |
6140 arguments can be used to specify arguments to the module's import() | |
6141 method, similar to C<use Foo::Bar VERSION LIST>. They must be | |
6142 terminated with a final NULL pointer. Note that this list can only | |
6143 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used. | |
6144 Otherwise at least a single NULL pointer to designate the default | |
6145 import list is required. | |
6146 | |
6147 The reference count for each specified C<SV*> parameter is decremented. | |
6148 | |
6149 =cut */ | |
6150 | |
6151 void | |
6152 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...) | |
6153 { | |
6154 va_list args; | |
6155 | |
6156 PERL_ARGS_ASSERT_LOAD_MODULE; | |
6157 | |
6158 va_start(args, ver); | |
6159 vload_module(flags, name, ver, &args); | |
6160 va_end(args); | |
6161 } | |
6162 | |
6163 #ifdef PERL_IMPLICIT_CONTEXT | |
6164 void | |
6165 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...) | |
6166 { | |
6167 dTHX; | |
6168 va_list args; | |
6169 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT; | |
6170 va_start(args, ver); | |
6171 vload_module(flags, name, ver, &args); | |
6172 va_end(args); | |
6173 } | |
6174 #endif | |
6175 | |
6176 void | |
6177 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args) | |
6178 { | |
6179 OP *veop, *imop; | |
6180 OP * const modname = newSVOP(OP_CONST, 0, name); | |
6181 | |
6182 PERL_ARGS_ASSERT_VLOAD_MODULE; | |
6183 | |
6184 modname->op_private |= OPpCONST_BARE; | |
6185 if (ver) { | |
6186 veop = newSVOP(OP_CONST, 0, ver); | |
6187 } | |
6188 else | |
6189 veop = NULL; | |
6190 if (flags & PERL_LOADMOD_NOIMPORT) { | |
6191 imop = sawparens(newNULLLIST()); | |
6192 } | |
6193 else if (flags & PERL_LOADMOD_IMPORT_OPS) { | |
6194 imop = va_arg(*args, OP*); | |
6195 } | |
6196 else { | |
6197 SV *sv; | |
6198 imop = NULL; | |
6199 sv = va_arg(*args, SV*); | |
6200 while (sv) { | |
6201 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv)); | |
6202 sv = va_arg(*args, SV*); | |
6203 } | |
6204 } | |
6205 | |
6206 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure | |
6207 * that it has a PL_parser to play with while doing that, and also | |
6208 * that it doesn't mess with any existing parser, by creating a tmp | |
6209 * new parser with lex_start(). This won't actually be used for much, | |
6210 * since pp_require() will create another parser for the real work. | |
6211 * The ENTER/LEAVE pair protect callers from any side effects of use. */ | |
6212 | |
6213 ENTER; | |
6214 SAVEVPTR(PL_curcop); | |
6215 lex_start(NULL, NULL, LEX_START_SAME_FILTER); | |
6216 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0), | |
6217 veop, modname, imop); | |
6218 LEAVE; | |
6219 } | |
6220 | |
6221 PERL_STATIC_INLINE OP * | |
6222 S_new_entersubop(pTHX_ GV *gv, OP *arg) | |
6223 { | |
6224 return newUNOP(OP_ENTERSUB, OPf_STACKED, | |
6225 newLISTOP(OP_LIST, 0, arg, | |
6226 newUNOP(OP_RV2CV, 0, | |
6227 newGVOP(OP_GV, 0, gv)))); | |
6228 } | |
6229 | |
6230 OP * | |
6231 Perl_dofile(pTHX_ OP *term, I32 force_builtin) | |
6232 { | |
6233 OP *doop; | |
6234 GV *gv; | |
6235 | |
6236 PERL_ARGS_ASSERT_DOFILE; | |
6237 | |
6238 if (!force_builtin && (gv = gv_override("do", 2))) { | |
6239 doop = S_new_entersubop(aTHX_ gv, term); | |
6240 } | |
6241 else { | |
6242 doop = newUNOP(OP_DOFILE, 0, scalar(term)); | |
6243 } | |
6244 return doop; | |
6245 } | |
6246 | |
6247 /* | |
6248 =head1 Optree construction | |
6249 | |
6250 =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval | |
6251 | |
6252 Constructs, checks, and returns an C<lslice> (list slice) op. I<flags> | |
6253 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will | |
6254 be set automatically, and, shifted up eight bits, the eight bits of | |
6255 C<op_private>, except that the bit with value 1 or 2 is automatically | |
6256 set as required. I<listval> and I<subscript> supply the parameters of | |
6257 the slice; they are consumed by this function and become part of the | |
6258 constructed op tree. | |
6259 | |
6260 =cut | |
6261 */ | |
6262 | |
6263 OP * | |
6264 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval) | |
6265 { | |
6266 return newBINOP(OP_LSLICE, flags, | |
6267 list(force_list(subscript, 1)), | |
6268 list(force_list(listval, 1)) ); | |
6269 } | |
6270 | |
6271 #define ASSIGN_LIST 1 | |
6272 #define ASSIGN_REF 2 | |
6273 | |
6274 STATIC I32 | |
6275 S_assignment_type(pTHX_ const OP *o) | |
6276 { | |
6277 unsigned type; | |
6278 U8 flags; | |
6279 U8 ret; | |
6280 | |
6281 if (!o) | |
6282 return TRUE; | |
6283 | |
6284 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS)) | |
6285 o = cUNOPo->op_first; | |
6286 | |
6287 flags = o->op_flags; | |
6288 type = o->op_type; | |
6289 if (type == OP_COND_EXPR) { | |
6290 OP * const sib = OpSIBLING(cLOGOPo->op_first); | |
6291 const I32 t = assignment_type(sib); | |
6292 const I32 f = assignment_type(OpSIBLING(sib)); | |
6293 | |
6294 if (t == ASSIGN_LIST && f == ASSIGN_LIST) | |
6295 return ASSIGN_LIST; | |
6296 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST)) | |
6297 yyerror("Assignment to both a list and a scalar"); | |
6298 return FALSE; | |
6299 } | |
6300 | |
6301 if (type == OP_SREFGEN) | |
6302 { | |
6303 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first; | |
6304 type = kid->op_type; | |
6305 flags |= kid->op_flags; | |
6306 if (!(flags & OPf_PARENS) | |
6307 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV || | |
6308 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV )) | |
6309 return ASSIGN_REF; | |
6310 ret = ASSIGN_REF; | |
6311 } | |
6312 else ret = 0; | |
6313 | |
6314 if (type == OP_LIST && | |
6315 (flags & OPf_WANT) == OPf_WANT_SCALAR && | |
6316 o->op_private & OPpLVAL_INTRO) | |
6317 return ret; | |
6318 | |
6319 if (type == OP_LIST || flags & OPf_PARENS || | |
6320 type == OP_RV2AV || type == OP_RV2HV || | |
6321 type == OP_ASLICE || type == OP_HSLICE || | |
6322 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN) | |
6323 return TRUE; | |
6324 | |
6325 if (type == OP_PADAV || type == OP_PADHV) | |
6326 return TRUE; | |
6327 | |
6328 if (type == OP_RV2SV) | |
6329 return ret; | |
6330 | |
6331 return ret; | |
6332 } | |
6333 | |
6334 /* | |
6335 Helper function for newASSIGNOP to detect commonality between the | |
6336 lhs and the rhs. (It is actually called very indirectly. newASSIGNOP | |
6337 flags the op and the peephole optimizer calls this helper function | |
6338 if the flag is set.) Marks all variables with PL_generation. If it | |
6339 returns TRUE the assignment must be able to handle common variables. | |
6340 | |
6341 PL_generation sorcery: | |
6342 An assignment like ($a,$b) = ($c,$d) is easier than | |
6343 ($a,$b) = ($c,$a), since there is no need for temporary vars. | |
6344 To detect whether there are common vars, the global var | |
6345 PL_generation is incremented for each assign op we compile. | |
6346 Then, while compiling the assign op, we run through all the | |
6347 variables on both sides of the assignment, setting a spare slot | |
6348 in each of them to PL_generation. If any of them already have | |
6349 that value, we know we've got commonality. Also, if the | |
6350 generation number is already set to PERL_INT_MAX, then | |
6351 the variable is involved in aliasing, so we also have | |
6352 potential commonality in that case. We could use a | |
6353 single bit marker, but then we'd have to make 2 passes, first | |
6354 to clear the flag, then to test and set it. And that | |
6355 wouldn't help with aliasing, either. To find somewhere | |
6356 to store these values, evil chicanery is done with SvUVX(). | |
6357 */ | |
6358 PERL_STATIC_INLINE bool | |
6359 S_aassign_common_vars(pTHX_ OP* o) | |
6360 { | |
6361 OP *curop; | |
6362 for (curop = cUNOPo->op_first; curop; curop = OpSIBLING(curop)) { | |
6363 if (PL_opargs[curop->op_type] & OA_DANGEROUS) { | |
6364 if (curop->op_type == OP_GV || curop->op_type == OP_GVSV | |
6365 || curop->op_type == OP_AELEMFAST) { | |
6366 GV *gv = cGVOPx_gv(curop); | |
6367 if (gv == PL_defgv | |
6368 || (int)GvASSIGN_GENERATION(gv) == PL_generation) | |
6369 return TRUE; | |
6370 GvASSIGN_GENERATION_set(gv, PL_generation); | |
6371 } | |
6372 else if (curop->op_type == OP_PADSV || | |
6373 curop->op_type == OP_PADAV || | |
6374 curop->op_type == OP_PADHV || | |
6375 curop->op_type == OP_AELEMFAST_LEX || | |
6376 curop->op_type == OP_PADANY) | |
6377 { | |
6378 padcheck: | |
6379 if (PAD_COMPNAME_GEN(curop->op_targ) | |
6380 == (STRLEN)PL_generation | |
6381 || PAD_COMPNAME_GEN(curop->op_targ) == PERL_INT_MAX) | |
6382 return TRUE; | |
6383 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation); | |
6384 | |
6385 } | |
6386 else if (curop->op_type == OP_RV2CV) | |
6387 return TRUE; | |
6388 else if (curop->op_type == OP_RV2SV || | |
6389 curop->op_type == OP_RV2AV || | |
6390 curop->op_type == OP_RV2HV || | |
6391 curop->op_type == OP_RV2GV) { | |
6392 if (cUNOPx(curop)->op_first->op_type != OP_GV) /* funny deref? */ | |
6393 return TRUE; | |
6394 } | |
6395 else if (curop->op_type == OP_PUSHRE) { | |
6396 GV *const gv = | |
6397 #ifdef USE_ITHREADS | |
6398 ((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff | |
6399 ? MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff)) | |
6400 : NULL; | |
6401 #else | |
6402 ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv; | |
6403 #endif | |
6404 if (gv) { | |
6405 if (gv == PL_defgv | |
6406 || (int)GvASSIGN_GENERATION(gv) == PL_generation) | |
6407 return TRUE; | |
6408 GvASSIGN_GENERATION_set(gv, PL_generation); | |
6409 } | |
6410 else if (curop->op_targ) | |
6411 goto padcheck; | |
6412 } | |
6413 else if (curop->op_type == OP_PADRANGE) | |
6414 /* Ignore padrange; checking its siblings is sufficient. */ | |
6415 continue; | |
6416 else | |
6417 return TRUE; | |
6418 } | |
6419 else if (PL_opargs[curop->op_type] & OA_TARGLEX | |
6420 && curop->op_private & OPpTARGET_MY) | |
6421 goto padcheck; | |
6422 | |
6423 if (curop->op_flags & OPf_KIDS) { | |
6424 if (aassign_common_vars(curop)) | |
6425 return TRUE; | |
6426 } | |
6427 } | |
6428 return FALSE; | |
6429 } | |
6430 | |
6431 /* This variant only handles lexical aliases. It is called when | |
6432 newASSIGNOP decides that we don’t have any common vars, as lexical ali- | |
6433 ases trump that decision. */ | |
6434 PERL_STATIC_INLINE bool | |
6435 S_aassign_common_vars_aliases_only(pTHX_ OP *o) | |
6436 { | |
6437 OP *curop; | |
6438 for (curop = cUNOPo->op_first; curop; curop = OpSIBLING(curop)) { | |
6439 if ((curop->op_type == OP_PADSV || | |
6440 curop->op_type == OP_PADAV || | |
6441 curop->op_type == OP_PADHV || | |
6442 curop->op_type == OP_AELEMFAST_LEX || | |
6443 curop->op_type == OP_PADANY || | |
6444 ( PL_opargs[curop->op_type] & OA_TARGLEX | |
6445 && curop->op_private & OPpTARGET_MY )) | |
6446 && PAD_COMPNAME_GEN(curop->op_targ) == PERL_INT_MAX) | |
6447 return TRUE; | |
6448 | |
6449 if (curop->op_type == OP_PUSHRE && curop->op_targ | |
6450 && PAD_COMPNAME_GEN(curop->op_targ) == PERL_INT_MAX) | |
6451 return TRUE; | |
6452 | |
6453 if (curop->op_flags & OPf_KIDS) { | |
6454 if (S_aassign_common_vars_aliases_only(aTHX_ curop)) | |
6455 return TRUE; | |
6456 } | |
6457 } | |
6458 return FALSE; | |
6459 } | |
6460 | |
6461 /* | |
6462 =for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right | |
6463 | |
6464 Constructs, checks, and returns an assignment op. I<left> and I<right> | |
6465 supply the parameters of the assignment; they are consumed by this | |
6466 function and become part of the constructed op tree. | |
6467 | |
6468 If I<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then | |
6469 a suitable conditional optree is constructed. If I<optype> is the opcode | |
6470 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that | |
6471 performs the binary operation and assigns the result to the left argument. | |
6472 Either way, if I<optype> is non-zero then I<flags> has no effect. | |
6473 | |
6474 If I<optype> is zero, then a plain scalar or list assignment is | |
6475 constructed. Which type of assignment it is is automatically determined. | |
6476 I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS> | |
6477 will be set automatically, and, shifted up eight bits, the eight bits | |
6478 of C<op_private>, except that the bit with value 1 or 2 is automatically | |
6479 set as required. | |
6480 | |
6481 =cut | |
6482 */ | |
6483 | |
6484 OP * | |
6485 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right) | |
6486 { | |
6487 OP *o; | |
6488 I32 assign_type; | |
6489 | |
6490 if (optype) { | |
6491 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) { | |
6492 return newLOGOP(optype, 0, | |
6493 op_lvalue(scalar(left), optype), | |
6494 newUNOP(OP_SASSIGN, 0, scalar(right))); | |
6495 } | |
6496 else { | |
6497 return newBINOP(optype, OPf_STACKED, | |
6498 op_lvalue(scalar(left), optype), scalar(right)); | |
6499 } | |
6500 } | |
6501 | |
6502 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) { | |
6503 static const char no_list_state[] = "Initialization of state variables" | |
6504 " in list context currently forbidden"; | |
6505 OP *curop; | |
6506 bool maybe_common_vars = TRUE; | |
6507 | |
6508 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE) | |
6509 left->op_private &= ~ OPpSLICEWARNING; | |
6510 | |
6511 PL_modcount = 0; | |
6512 left = op_lvalue(left, OP_AASSIGN); | |
6513 curop = list(force_list(left, 1)); | |
6514 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, 1)), curop); | |
6515 o->op_private = (U8)(0 | (flags >> 8)); | |
6516 | |
6517 if (OP_TYPE_IS_OR_WAS(left, OP_LIST)) | |
6518 { | |
6519 OP* lop = ((LISTOP*)left)->op_first; | |
6520 maybe_common_vars = FALSE; | |
6521 while (lop) { | |
6522 if (lop->op_type == OP_PADSV || | |
6523 lop->op_type == OP_PADAV || | |
6524 lop->op_type == OP_PADHV || | |
6525 lop->op_type == OP_PADANY) { | |
6526 if (!(lop->op_private & OPpLVAL_INTRO)) | |
6527 maybe_common_vars = TRUE; | |
6528 | |
6529 if (lop->op_private & OPpPAD_STATE) { | |
6530 if (left->op_private & OPpLVAL_INTRO) { | |
6531 /* Each variable in state($a, $b, $c) = ... */ | |
6532 } | |
6533 else { | |
6534 /* Each state variable in | |
6535 (state $a, my $b, our $c, $d, undef) = ... */ | |
6536 } | |
6537 yyerror(no_list_state); | |
6538 } else { | |
6539 /* Each my variable in | |
6540 (state $a, my $b, our $c, $d, undef) = ... */ | |
6541 } | |
6542 } else if (lop->op_type == OP_UNDEF || | |
6543 OP_TYPE_IS_OR_WAS(lop, OP_PUSHMARK)) { | |
6544 /* undef may be interesting in | |
6545 (state $a, undef, state $c) */ | |
6546 } else { | |
6547 /* Other ops in the list. */ | |
6548 maybe_common_vars = TRUE; | |
6549 } | |
6550 lop = OpSIBLING(lop); | |
6551 } | |
6552 } | |
6553 else if ((left->op_private & OPpLVAL_INTRO) | |
6554 && ( left->op_type == OP_PADSV | |
6555 || left->op_type == OP_PADAV | |
6556 || left->op_type == OP_PADHV | |
6557 || left->op_type == OP_PADANY)) | |
6558 { | |
6559 if (left->op_type == OP_PADSV) maybe_common_vars = FALSE; | |
6560 if (left->op_private & OPpPAD_STATE) { | |
6561 /* All single variable list context state assignments, hence | |
6562 state ($a) = ... | |
6563 (state $a) = ... | |
6564 state @a = ... | |
6565 state (@a) = ... | |
6566 (state @a) = ... | |
6567 state %a = ... | |
6568 state (%a) = ... | |
6569 (state %a) = ... | |
6570 */ | |
6571 yyerror(no_list_state); | |
6572 } | |
6573 } | |
6574 | |
6575 if (maybe_common_vars) { | |
6576 /* The peephole optimizer will do the full check and pos- | |
6577 sibly turn this off. */ | |
6578 o->op_private |= OPpASSIGN_COMMON; | |
6579 } | |
6580 | |
6581 if (right && right->op_type == OP_SPLIT | |
6582 && !(right->op_flags & OPf_STACKED)) { | |
6583 OP* tmpop = ((LISTOP*)right)->op_first; | |
6584 PMOP * const pm = (PMOP*)tmpop; | |
6585 assert (tmpop && (tmpop->op_type == OP_PUSHRE)); | |
6586 if ( | |
6587 #ifdef USE_ITHREADS | |
6588 !pm->op_pmreplrootu.op_pmtargetoff | |
6589 #else | |
6590 !pm->op_pmreplrootu.op_pmtargetgv | |
6591 #endif | |
6592 && !pm->op_targ | |
6593 ) { | |
6594 if (!(left->op_private & OPpLVAL_INTRO) && | |
6595 ( (left->op_type == OP_RV2AV && | |
6596 (tmpop=((UNOP*)left)->op_first)->op_type==OP_GV) | |
6597 || left->op_type == OP_PADAV ) | |
6598 ) { | |
6599 if (tmpop != (OP *)pm) { | |
6600 #ifdef USE_ITHREADS | |
6601 pm->op_pmreplrootu.op_pmtargetoff | |
6602 = cPADOPx(tmpop)->op_padix; | |
6603 cPADOPx(tmpop)->op_padix = 0; /* steal it */ | |
6604 #else | |
6605 pm->op_pmreplrootu.op_pmtargetgv | |
6606 = MUTABLE_GV(cSVOPx(tmpop)->op_sv); | |
6607 cSVOPx(tmpop)->op_sv = NULL; /* steal it */ | |
6608 #endif | |
6609 right->op_private |= | |
6610 left->op_private & OPpOUR_INTRO; | |
6611 } | |
6612 else { | |
6613 pm->op_targ = left->op_targ; | |
6614 left->op_targ = 0; /* filch it */ | |
6615 } | |
6616 detach_split: | |
6617 tmpop = cUNOPo->op_first; /* to list (nulled) */ | |
6618 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ | |
6619 /* detach rest of siblings from o subtree, | |
6620 * and free subtree */ | |
6621 op_sibling_splice(cUNOPo->op_first, tmpop, -1, NULL); | |
6622 op_free(o); /* blow off assign */ | |
6623 right->op_flags &= ~OPf_WANT; | |
6624 /* "I don't know and I don't care." */ | |
6625 return right; | |
6626 } | |
6627 else if (left->op_type == OP_RV2AV | |
6628 || left->op_type == OP_PADAV) | |
6629 { | |
6630 /* Detach the array. */ | |
6631 #ifdef DEBUGGING | |
6632 OP * const ary = | |
6633 #endif | |
6634 op_sibling_splice(cBINOPo->op_last, | |
6635 cUNOPx(cBINOPo->op_last) | |
6636 ->op_first, 1, NULL); | |
6637 assert(ary == left); | |
6638 /* Attach it to the split. */ | |
6639 op_sibling_splice(right, cLISTOPx(right)->op_last, | |
6640 0, left); | |
6641 right->op_flags |= OPf_STACKED; | |
6642 /* Detach split and expunge aassign as above. */ | |
6643 goto detach_split; | |
6644 } | |
6645 else if (PL_modcount < RETURN_UNLIMITED_NUMBER && | |
6646 ((LISTOP*)right)->op_last->op_type == OP_CONST) | |
6647 { | |
6648 SV ** const svp = | |
6649 &((SVOP*)((LISTOP*)right)->op_last)->op_sv; | |
6650 SV * const sv = *svp; | |
6651 if (SvIOK(sv) && SvIVX(sv) == 0) | |
6652 { | |
6653 if (right->op_private & OPpSPLIT_IMPLIM) { | |
6654 /* our own SV, created in ck_split */ | |
6655 SvREADONLY_off(sv); | |
6656 sv_setiv(sv, PL_modcount+1); | |
6657 } | |
6658 else { | |
6659 /* SV may belong to someone else */ | |
6660 SvREFCNT_dec(sv); | |
6661 *svp = newSViv(PL_modcount+1); | |
6662 } | |
6663 } | |
6664 } | |
6665 } | |
6666 } | |
6667 return o; | |
6668 } | |
6669 if (assign_type == ASSIGN_REF) | |
6670 return newBINOP(OP_REFASSIGN, flags, scalar(right), left); | |
6671 if (!right) | |
6672 right = newOP(OP_UNDEF, 0); | |
6673 if (right->op_type == OP_READLINE) { | |
6674 right->op_flags |= OPf_STACKED; | |
6675 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN), | |
6676 scalar(right)); | |
6677 } | |
6678 else { | |
6679 o = newBINOP(OP_SASSIGN, flags, | |
6680 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) ); | |
6681 } | |
6682 return o; | |
6683 } | |
6684 | |
6685 /* | |
6686 =for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o | |
6687 | |
6688 Constructs a state op (COP). The state op is normally a C<nextstate> op, | |
6689 but will be a C<dbstate> op if debugging is enabled for currently-compiled | |
6690 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>). | |
6691 If I<label> is non-null, it supplies the name of a label to attach to | |
6692 the state op; this function takes ownership of the memory pointed at by | |
6693 I<label>, and will free it. I<flags> gives the eight bits of C<op_flags> | |
6694 for the state op. | |
6695 | |
6696 If I<o> is null, the state op is returned. Otherwise the state op is | |
6697 combined with I<o> into a C<lineseq> list op, which is returned. I<o> | |
6698 is consumed by this function and becomes part of the returned op tree. | |
6699 | |
6700 =cut | |
6701 */ | |
6702 | |
6703 OP * | |
6704 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o) | |
6705 { | |
6706 dVAR; | |
6707 const U32 seq = intro_my(); | |
6708 const U32 utf8 = flags & SVf_UTF8; | |
6709 COP *cop; | |
6710 | |
6711 PL_parser->parsed_sub = 0; | |
6712 | |
6713 flags &= ~SVf_UTF8; | |
6714 | |
6715 NewOp(1101, cop, 1, COP); | |
6716 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) { | |
6717 OpTYPE_set(cop, OP_DBSTATE); | |
6718 } | |
6719 else { | |
6720 OpTYPE_set(cop, OP_NEXTSTATE); | |
6721 } | |
6722 cop->op_flags = (U8)flags; | |
6723 CopHINTS_set(cop, PL_hints); | |
6724 #ifdef VMS | |
6725 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH; | |
6726 #endif | |
6727 cop->op_next = (OP*)cop; | |
6728 | |
6729 cop->cop_seq = seq; | |
6730 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); | |
6731 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop))); | |
6732 if (label) { | |
6733 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8); | |
6734 | |
6735 PL_hints |= HINT_BLOCK_SCOPE; | |
6736 /* It seems that we need to defer freeing this pointer, as other parts | |
6737 of the grammar end up wanting to copy it after this op has been | |
6738 created. */ | |
6739 SAVEFREEPV(label); | |
6740 } | |
6741 | |
6742 if (PL_parser->preambling != NOLINE) { | |
6743 CopLINE_set(cop, PL_parser->preambling); | |
6744 PL_parser->copline = NOLINE; | |
6745 } | |
6746 else if (PL_parser->copline == NOLINE) | |
6747 CopLINE_set(cop, CopLINE(PL_curcop)); | |
6748 else { | |
6749 CopLINE_set(cop, PL_parser->copline); | |
6750 PL_parser->copline = NOLINE; | |
6751 } | |
6752 #ifdef USE_ITHREADS | |
6753 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */ | |
6754 #else | |
6755 CopFILEGV_set(cop, CopFILEGV(PL_curcop)); | |
6756 #endif | |
6757 CopSTASH_set(cop, PL_curstash); | |
6758 | |
6759 if (cop->op_type == OP_DBSTATE) { | |
6760 /* this line can have a breakpoint - store the cop in IV */ | |
6761 AV *av = CopFILEAVx(PL_curcop); | |
6762 if (av) { | |
6763 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE); | |
6764 if (svp && *svp != &PL_sv_undef ) { | |
6765 (void)SvIOK_on(*svp); | |
6766 SvIV_set(*svp, PTR2IV(cop)); | |
6767 } | |
6768 } | |
6769 } | |
6770 | |
6771 if (flags & OPf_SPECIAL) | |
6772 op_null((OP*)cop); | |
6773 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o); | |
6774 } | |
6775 | |
6776 /* | |
6777 =for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other | |
6778 | |
6779 Constructs, checks, and returns a logical (flow control) op. I<type> | |
6780 is the opcode. I<flags> gives the eight bits of C<op_flags>, except | |
6781 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits, | |
6782 the eight bits of C<op_private>, except that the bit with value 1 is | |
6783 automatically set. I<first> supplies the expression controlling the | |
6784 flow, and I<other> supplies the side (alternate) chain of ops; they are | |
6785 consumed by this function and become part of the constructed op tree. | |
6786 | |
6787 =cut | |
6788 */ | |
6789 | |
6790 OP * | |
6791 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other) | |
6792 { | |
6793 PERL_ARGS_ASSERT_NEWLOGOP; | |
6794 | |
6795 return new_logop(type, flags, &first, &other); | |
6796 } | |
6797 | |
6798 STATIC OP * | |
6799 S_search_const(pTHX_ OP *o) | |
6800 { | |
6801 PERL_ARGS_ASSERT_SEARCH_CONST; | |
6802 | |
6803 switch (o->op_type) { | |
6804 case OP_CONST: | |
6805 return o; | |
6806 case OP_NULL: | |
6807 if (o->op_flags & OPf_KIDS) | |
6808 return search_const(cUNOPo->op_first); | |
6809 break; | |
6810 case OP_LEAVE: | |
6811 case OP_SCOPE: | |
6812 case OP_LINESEQ: | |
6813 { | |
6814 OP *kid; | |
6815 if (!(o->op_flags & OPf_KIDS)) | |
6816 return NULL; | |
6817 kid = cLISTOPo->op_first; | |
6818 do { | |
6819 switch (kid->op_type) { | |
6820 case OP_ENTER: | |
6821 case OP_NULL: | |
6822 case OP_NEXTSTATE: | |
6823 kid = OpSIBLING(kid); | |
6824 break; | |
6825 default: | |
6826 if (kid != cLISTOPo->op_last) | |
6827 return NULL; | |
6828 goto last; | |
6829 } | |
6830 } while (kid); | |
6831 if (!kid) | |
6832 kid = cLISTOPo->op_last; | |
6833 last: | |
6834 return search_const(kid); | |
6835 } | |
6836 } | |
6837 | |
6838 return NULL; | |
6839 } | |
6840 | |
6841 STATIC OP * | |
6842 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp) | |
6843 { | |
6844 dVAR; | |
6845 LOGOP *logop; | |
6846 OP *o; | |
6847 OP *first; | |
6848 OP *other; | |
6849 OP *cstop = NULL; | |
6850 int prepend_not = 0; | |
6851 | |
6852 PERL_ARGS_ASSERT_NEW_LOGOP; | |
6853 | |
6854 first = *firstp; | |
6855 other = *otherp; | |
6856 | |
6857 /* [perl #59802]: Warn about things like "return $a or $b", which | |
6858 is parsed as "(return $a) or $b" rather than "return ($a or | |
6859 $b)". NB: This also applies to xor, which is why we do it | |
6860 here. | |
6861 */ | |
6862 switch (first->op_type) { | |
6863 case OP_NEXT: | |
6864 case OP_LAST: | |
6865 case OP_REDO: | |
6866 /* XXX: Perhaps we should emit a stronger warning for these. | |
6867 Even with the high-precedence operator they don't seem to do | |
6868 anything sensible. | |
6869 | |
6870 But until we do, fall through here. | |
6871 */ | |
6872 case OP_RETURN: | |
6873 case OP_EXIT: | |
6874 case OP_DIE: | |
6875 case OP_GOTO: | |
6876 /* XXX: Currently we allow people to "shoot themselves in the | |
6877 foot" by explicitly writing "(return $a) or $b". | |
6878 | |
6879 Warn unless we are looking at the result from folding or if | |
6880 the programmer explicitly grouped the operators like this. | |
6881 The former can occur with e.g. | |
6882 | |
6883 use constant FEATURE => ( $] >= ... ); | |
6884 sub { not FEATURE and return or do_stuff(); } | |
6885 */ | |
6886 if (!first->op_folded && !(first->op_flags & OPf_PARENS)) | |
6887 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), | |
6888 "Possible precedence issue with control flow operator"); | |
6889 /* XXX: Should we optimze this to "return $a;" (i.e. remove | |
6890 the "or $b" part)? | |
6891 */ | |
6892 break; | |
6893 } | |
6894 | |
6895 if (type == OP_XOR) /* Not short circuit, but here by precedence. */ | |
6896 return newBINOP(type, flags, scalar(first), scalar(other)); | |
6897 | |
6898 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP | |
6899 || type == OP_CUSTOM); | |
6900 | |
6901 scalarboolean(first); | |
6902 /* optimize AND and OR ops that have NOTs as children */ | |
6903 if (first->op_type == OP_NOT | |
6904 && (first->op_flags & OPf_KIDS) | |
6905 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */ | |
6906 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */ | |
6907 ) { | |
6908 if (type == OP_AND || type == OP_OR) { | |
6909 if (type == OP_AND) | |
6910 type = OP_OR; | |
6911 else | |
6912 type = OP_AND; | |
6913 op_null(first); | |
6914 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */ | |
6915 op_null(other); | |
6916 prepend_not = 1; /* prepend a NOT op later */ | |
6917 } | |
6918 } | |
6919 } | |
6920 /* search for a constant op that could let us fold the test */ | |
6921 if ((cstop = search_const(first))) { | |
6922 if (cstop->op_private & OPpCONST_STRICT) | |
6923 no_bareword_allowed(cstop); | |
6924 else if ((cstop->op_private & OPpCONST_BARE)) | |
6925 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional"); | |
6926 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) || | |
6927 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) || | |
6928 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) { | |
6929 *firstp = NULL; | |
6930 if (other->op_type == OP_CONST) | |
6931 other->op_private |= OPpCONST_SHORTCIRCUIT; | |
6932 op_free(first); | |
6933 if (other->op_type == OP_LEAVE) | |
6934 other = newUNOP(OP_NULL, OPf_SPECIAL, other); | |
6935 else if (other->op_type == OP_MATCH | |
6936 || other->op_type == OP_SUBST | |
6937 || other->op_type == OP_TRANSR | |
6938 || other->op_type == OP_TRANS) | |
6939 /* Mark the op as being unbindable with =~ */ | |
6940 other->op_flags |= OPf_SPECIAL; | |
6941 | |
6942 other->op_folded = 1; | |
6943 return other; | |
6944 } | |
6945 else { | |
6946 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */ | |
6947 const OP *o2 = other; | |
6948 if ( ! (o2->op_type == OP_LIST | |
6949 && (( o2 = cUNOPx(o2)->op_first)) | |
6950 && o2->op_type == OP_PUSHMARK | |
6951 && (( o2 = OpSIBLING(o2))) ) | |
6952 ) | |
6953 o2 = other; | |
6954 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV | |
6955 || o2->op_type == OP_PADHV) | |
6956 && o2->op_private & OPpLVAL_INTRO | |
6957 && !(o2->op_private & OPpPAD_STATE)) | |
6958 { | |
6959 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), | |
6960 "Deprecated use of my() in false conditional"); | |
6961 } | |
6962 | |
6963 *otherp = NULL; | |
6964 if (cstop->op_type == OP_CONST) | |
6965 cstop->op_private |= OPpCONST_SHORTCIRCUIT; | |
6966 op_free(other); | |
6967 return first; | |
6968 } | |
6969 } | |
6970 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR | |
6971 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */ | |
6972 { | |
6973 const OP * const k1 = ((UNOP*)first)->op_first; | |
6974 const OP * const k2 = OpSIBLING(k1); | |
6975 OPCODE warnop = 0; | |
6976 switch (first->op_type) | |
6977 { | |
6978 case OP_NULL: | |
6979 if (k2 && k2->op_type == OP_READLINE | |
6980 && (k2->op_flags & OPf_STACKED) | |
6981 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) | |
6982 { | |
6983 warnop = k2->op_type; | |
6984 } | |
6985 break; | |
6986 | |
6987 case OP_SASSIGN: | |
6988 if (k1->op_type == OP_READDIR | |
6989 || k1->op_type == OP_GLOB | |
6990 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) | |
6991 || k1->op_type == OP_EACH | |
6992 || k1->op_type == OP_AEACH) | |
6993 { | |
6994 warnop = ((k1->op_type == OP_NULL) | |
6995 ? (OPCODE)k1->op_targ : k1->op_type); | |
6996 } | |
6997 break; | |
6998 } | |
6999 if (warnop) { | |
7000 const line_t oldline = CopLINE(PL_curcop); | |
7001 /* This ensures that warnings are reported at the first line | |
7002 of the construction, not the last. */ | |
7003 CopLINE_set(PL_curcop, PL_parser->copline); | |
7004 Perl_warner(aTHX_ packWARN(WARN_MISC), | |
7005 "Value of %s%s can be \"0\"; test with defined()", | |
7006 PL_op_desc[warnop], | |
7007 ((warnop == OP_READLINE || warnop == OP_GLOB) | |
7008 ? " construct" : "() operator")); | |
7009 CopLINE_set(PL_curcop, oldline); | |
7010 } | |
7011 } | |
7012 | |
7013 if (!other) | |
7014 return first; | |
7015 | |
7016 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN) | |
7017 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */ | |
7018 | |
7019 logop = S_alloc_LOGOP(aTHX_ type, first, LINKLIST(other)); | |
7020 logop->op_flags |= (U8)flags; | |
7021 logop->op_private = (U8)(1 | (flags >> 8)); | |
7022 | |
7023 /* establish postfix order */ | |
7024 logop->op_next = LINKLIST(first); | |
7025 first->op_next = (OP*)logop; | |
7026 assert(!OpHAS_SIBLING(first)); | |
7027 op_sibling_splice((OP*)logop, first, 0, other); | |
7028 | |
7029 CHECKOP(type,logop); | |
7030 | |
7031 o = newUNOP(prepend_not ? OP_NOT : OP_NULL, | |
7032 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0, | |
7033 (OP*)logop); | |
7034 other->op_next = o; | |
7035 | |
7036 return o; | |
7037 } | |
7038 | |
7039 /* | |
7040 =for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop | |
7041 | |
7042 Constructs, checks, and returns a conditional-expression (C<cond_expr>) | |
7043 op. I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS> | |
7044 will be set automatically, and, shifted up eight bits, the eight bits of | |
7045 C<op_private>, except that the bit with value 1 is automatically set. | |
7046 I<first> supplies the expression selecting between the two branches, | |
7047 and I<trueop> and I<falseop> supply the branches; they are consumed by | |
7048 this function and become part of the constructed op tree. | |
7049 | |
7050 =cut | |
7051 */ | |
7052 | |
7053 OP * | |
7054 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop) | |
7055 { | |
7056 dVAR; | |
7057 LOGOP *logop; | |
7058 OP *start; | |
7059 OP *o; | |
7060 OP *cstop; | |
7061 | |
7062 PERL_ARGS_ASSERT_NEWCONDOP; | |
7063 | |
7064 if (!falseop) | |
7065 return newLOGOP(OP_AND, 0, first, trueop); | |
7066 if (!trueop) | |
7067 return newLOGOP(OP_OR, 0, first, falseop); | |
7068 | |
7069 scalarboolean(first); | |
7070 if ((cstop = search_const(first))) { | |
7071 /* Left or right arm of the conditional? */ | |
7072 const bool left = SvTRUE(((SVOP*)cstop)->op_sv); | |
7073 OP *live = left ? trueop : falseop; | |
7074 OP *const dead = left ? falseop : trueop; | |
7075 if (cstop->op_private & OPpCONST_BARE && | |
7076 cstop->op_private & OPpCONST_STRICT) { | |
7077 no_bareword_allowed(cstop); | |
7078 } | |
7079 op_free(first); | |
7080 op_free(dead); | |
7081 if (live->op_type == OP_LEAVE) | |
7082 live = newUNOP(OP_NULL, OPf_SPECIAL, live); | |
7083 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST | |
7084 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR) | |
7085 /* Mark the op as being unbindable with =~ */ | |
7086 live->op_flags |= OPf_SPECIAL; | |
7087 live->op_folded = 1; | |
7088 return live; | |
7089 } | |
7090 logop = S_alloc_LOGOP(aTHX_ OP_COND_EXPR, first, LINKLIST(trueop)); | |
7091 logop->op_flags |= (U8)flags; | |
7092 logop->op_private = (U8)(1 | (flags >> 8)); | |
7093 logop->op_next = LINKLIST(falseop); | |
7094 | |
7095 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */ | |
7096 logop); | |
7097 | |
7098 /* establish postfix order */ | |
7099 start = LINKLIST(first); | |
7100 first->op_next = (OP*)logop; | |
7101 | |
7102 /* make first, trueop, falseop siblings */ | |
7103 op_sibling_splice((OP*)logop, first, 0, trueop); | |
7104 op_sibling_splice((OP*)logop, trueop, 0, falseop); | |
7105 | |
7106 o = newUNOP(OP_NULL, 0, (OP*)logop); | |
7107 | |
7108 trueop->op_next = falseop->op_next = o; | |
7109 | |
7110 o->op_next = start; | |
7111 return o; | |
7112 } | |
7113 | |
7114 /* | |
7115 =for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right | |
7116 | |
7117 Constructs and returns a C<range> op, with subordinate C<flip> and | |
7118 C<flop> ops. I<flags> gives the eight bits of C<op_flags> for the | |
7119 C<flip> op and, shifted up eight bits, the eight bits of C<op_private> | |
7120 for both the C<flip> and C<range> ops, except that the bit with value | |
7121 1 is automatically set. I<left> and I<right> supply the expressions | |
7122 controlling the endpoints of the range; they are consumed by this function | |
7123 and become part of the constructed op tree. | |
7124 | |
7125 =cut | |
7126 */ | |
7127 | |
7128 OP * | |
7129 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right) | |
7130 { | |
7131 LOGOP *range; | |
7132 OP *flip; | |
7133 OP *flop; | |
7134 OP *leftstart; | |
7135 OP *o; | |
7136 | |
7137 PERL_ARGS_ASSERT_NEWRANGE; | |
7138 | |
7139 range = S_alloc_LOGOP(aTHX_ OP_RANGE, left, LINKLIST(right)); | |
7140 range->op_flags = OPf_KIDS; | |
7141 leftstart = LINKLIST(left); | |
7142 range->op_private = (U8)(1 | (flags >> 8)); | |
7143 | |
7144 /* make left and right siblings */ | |
7145 op_sibling_splice((OP*)range, left, 0, right); | |
7146 | |
7147 range->op_next = (OP*)range; | |
7148 flip = newUNOP(OP_FLIP, flags, (OP*)range); | |
7149 flop = newUNOP(OP_FLOP, 0, flip); | |
7150 o = newUNOP(OP_NULL, 0, flop); | |
7151 LINKLIST(flop); | |
7152 range->op_next = leftstart; | |
7153 | |
7154 left->op_next = flip; | |
7155 right->op_next = flop; | |
7156 | |
7157 range->op_targ = | |
7158 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0); | |
7159 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV); | |
7160 flip->op_targ = | |
7161 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);; | |
7162 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV); | |
7163 SvPADTMP_on(PAD_SV(flip->op_targ)); | |
7164 | |
7165 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
7166 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
7167 | |
7168 /* check barewords before they might be optimized aways */ | |
7169 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT) | |
7170 no_bareword_allowed(left); | |
7171 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT) | |
7172 no_bareword_allowed(right); | |
7173 | |
7174 flip->op_next = o; | |
7175 if (!flip->op_private || !flop->op_private) | |
7176 LINKLIST(o); /* blow off optimizer unless constant */ | |
7177 | |
7178 return o; | |
7179 } | |
7180 | |
7181 /* | |
7182 =for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block | |
7183 | |
7184 Constructs, checks, and returns an op tree expressing a loop. This is | |
7185 only a loop in the control flow through the op tree; it does not have | |
7186 the heavyweight loop structure that allows exiting the loop by C<last> | |
7187 and suchlike. I<flags> gives the eight bits of C<op_flags> for the | |
7188 top-level op, except that some bits will be set automatically as required. | |
7189 I<expr> supplies the expression controlling loop iteration, and I<block> | |
7190 supplies the body of the loop; they are consumed by this function and | |
7191 become part of the constructed op tree. I<debuggable> is currently | |
7192 unused and should always be 1. | |
7193 | |
7194 =cut | |
7195 */ | |
7196 | |
7197 OP * | |
7198 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block) | |
7199 { | |
7200 OP* listop; | |
7201 OP* o; | |
7202 const bool once = block && block->op_flags & OPf_SPECIAL && | |
7203 block->op_type == OP_NULL; | |
7204 | |
7205 PERL_UNUSED_ARG(debuggable); | |
7206 | |
7207 if (expr) { | |
7208 if (once && ( | |
7209 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv)) | |
7210 || ( expr->op_type == OP_NOT | |
7211 && cUNOPx(expr)->op_first->op_type == OP_CONST | |
7212 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first)) | |
7213 ) | |
7214 )) | |
7215 /* Return the block now, so that S_new_logop does not try to | |
7216 fold it away. */ | |
7217 return block; /* do {} while 0 does once */ | |
7218 if (expr->op_type == OP_READLINE | |
7219 || expr->op_type == OP_READDIR | |
7220 || expr->op_type == OP_GLOB | |
7221 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH | |
7222 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { | |
7223 expr = newUNOP(OP_DEFINED, 0, | |
7224 newASSIGNOP(0, newDEFSVOP(), 0, expr) ); | |
7225 } else if (expr->op_flags & OPf_KIDS) { | |
7226 const OP * const k1 = ((UNOP*)expr)->op_first; | |
7227 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL; | |
7228 switch (expr->op_type) { | |
7229 case OP_NULL: | |
7230 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR) | |
7231 && (k2->op_flags & OPf_STACKED) | |
7232 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) | |
7233 expr = newUNOP(OP_DEFINED, 0, expr); | |
7234 break; | |
7235 | |
7236 case OP_SASSIGN: | |
7237 if (k1 && (k1->op_type == OP_READDIR | |
7238 || k1->op_type == OP_GLOB | |
7239 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) | |
7240 || k1->op_type == OP_EACH | |
7241 || k1->op_type == OP_AEACH)) | |
7242 expr = newUNOP(OP_DEFINED, 0, expr); | |
7243 break; | |
7244 } | |
7245 } | |
7246 } | |
7247 | |
7248 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar | |
7249 * op, in listop. This is wrong. [perl #27024] */ | |
7250 if (!block) | |
7251 block = newOP(OP_NULL, 0); | |
7252 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0)); | |
7253 o = new_logop(OP_AND, 0, &expr, &listop); | |
7254 | |
7255 if (once) { | |
7256 ASSUME(listop); | |
7257 } | |
7258 | |
7259 if (listop) | |
7260 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o); | |
7261 | |
7262 if (once && o != listop) | |
7263 { | |
7264 assert(cUNOPo->op_first->op_type == OP_AND | |
7265 || cUNOPo->op_first->op_type == OP_OR); | |
7266 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other; | |
7267 } | |
7268 | |
7269 if (o == listop) | |
7270 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */ | |
7271 | |
7272 o->op_flags |= flags; | |
7273 o = op_scope(o); | |
7274 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/ | |
7275 return o; | |
7276 } | |
7277 | |
7278 /* | |
7279 =for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my | |
7280 | |
7281 Constructs, checks, and returns an op tree expressing a C<while> loop. | |
7282 This is a heavyweight loop, with structure that allows exiting the loop | |
7283 by C<last> and suchlike. | |
7284 | |
7285 I<loop> is an optional preconstructed C<enterloop> op to use in the | |
7286 loop; if it is null then a suitable op will be constructed automatically. | |
7287 I<expr> supplies the loop's controlling expression. I<block> supplies the | |
7288 main body of the loop, and I<cont> optionally supplies a C<continue> block | |
7289 that operates as a second half of the body. All of these optree inputs | |
7290 are consumed by this function and become part of the constructed op tree. | |
7291 | |
7292 I<flags> gives the eight bits of C<op_flags> for the C<leaveloop> | |
7293 op and, shifted up eight bits, the eight bits of C<op_private> for | |
7294 the C<leaveloop> op, except that (in both cases) some bits will be set | |
7295 automatically. I<debuggable> is currently unused and should always be 1. | |
7296 I<has_my> can be supplied as true to force the | |
7297 loop body to be enclosed in its own scope. | |
7298 | |
7299 =cut | |
7300 */ | |
7301 | |
7302 OP * | |
7303 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, | |
7304 OP *expr, OP *block, OP *cont, I32 has_my) | |
7305 { | |
7306 dVAR; | |
7307 OP *redo; | |
7308 OP *next = NULL; | |
7309 OP *listop; | |
7310 OP *o; | |
7311 U8 loopflags = 0; | |
7312 | |
7313 PERL_UNUSED_ARG(debuggable); | |
7314 | |
7315 if (expr) { | |
7316 if (expr->op_type == OP_READLINE | |
7317 || expr->op_type == OP_READDIR | |
7318 || expr->op_type == OP_GLOB | |
7319 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH | |
7320 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { | |
7321 expr = newUNOP(OP_DEFINED, 0, | |
7322 newASSIGNOP(0, newDEFSVOP(), 0, expr) ); | |
7323 } else if (expr->op_flags & OPf_KIDS) { | |
7324 const OP * const k1 = ((UNOP*)expr)->op_first; | |
7325 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL; | |
7326 switch (expr->op_type) { | |
7327 case OP_NULL: | |
7328 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR) | |
7329 && (k2->op_flags & OPf_STACKED) | |
7330 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) | |
7331 expr = newUNOP(OP_DEFINED, 0, expr); | |
7332 break; | |
7333 | |
7334 case OP_SASSIGN: | |
7335 if (k1 && (k1->op_type == OP_READDIR | |
7336 || k1->op_type == OP_GLOB | |
7337 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) | |
7338 || k1->op_type == OP_EACH | |
7339 || k1->op_type == OP_AEACH)) | |
7340 expr = newUNOP(OP_DEFINED, 0, expr); | |
7341 break; | |
7342 } | |
7343 } | |
7344 } | |
7345 | |
7346 if (!block) | |
7347 block = newOP(OP_NULL, 0); | |
7348 else if (cont || has_my) { | |
7349 block = op_scope(block); | |
7350 } | |
7351 | |
7352 if (cont) { | |
7353 next = LINKLIST(cont); | |
7354 } | |
7355 if (expr) { | |
7356 OP * const unstack = newOP(OP_UNSTACK, 0); | |
7357 if (!next) | |
7358 next = unstack; | |
7359 cont = op_append_elem(OP_LINESEQ, cont, unstack); | |
7360 } | |
7361 | |
7362 assert(block); | |
7363 listop = op_append_list(OP_LINESEQ, block, cont); | |
7364 assert(listop); | |
7365 redo = LINKLIST(listop); | |
7366 | |
7367 if (expr) { | |
7368 scalar(listop); | |
7369 o = new_logop(OP_AND, 0, &expr, &listop); | |
7370 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) { | |
7371 op_free((OP*)loop); | |
7372 return expr; /* listop already freed by new_logop */ | |
7373 } | |
7374 if (listop) | |
7375 ((LISTOP*)listop)->op_last->op_next = | |
7376 (o == listop ? redo : LINKLIST(o)); | |
7377 } | |
7378 else | |
7379 o = listop; | |
7380 | |
7381 if (!loop) { | |
7382 NewOp(1101,loop,1,LOOP); | |
7383 OpTYPE_set(loop, OP_ENTERLOOP); | |
7384 loop->op_private = 0; | |
7385 loop->op_next = (OP*)loop; | |
7386 } | |
7387 | |
7388 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o); | |
7389 | |
7390 loop->op_redoop = redo; | |
7391 loop->op_lastop = o; | |
7392 o->op_private |= loopflags; | |
7393 | |
7394 if (next) | |
7395 loop->op_nextop = next; | |
7396 else | |
7397 loop->op_nextop = o; | |
7398 | |
7399 o->op_flags |= flags; | |
7400 o->op_private |= (flags >> 8); | |
7401 return o; | |
7402 } | |
7403 | |
7404 /* | |
7405 =for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont | |
7406 | |
7407 Constructs, checks, and returns an op tree expressing a C<foreach> | |
7408 loop (iteration through a list of values). This is a heavyweight loop, | |
7409 with structure that allows exiting the loop by C<last> and suchlike. | |
7410 | |
7411 I<sv> optionally supplies the variable that will be aliased to each | |
7412 item in turn; if null, it defaults to C<$_> (either lexical or global). | |
7413 I<expr> supplies the list of values to iterate over. I<block> supplies | |
7414 the main body of the loop, and I<cont> optionally supplies a C<continue> | |
7415 block that operates as a second half of the body. All of these optree | |
7416 inputs are consumed by this function and become part of the constructed | |
7417 op tree. | |
7418 | |
7419 I<flags> gives the eight bits of C<op_flags> for the C<leaveloop> | |
7420 op and, shifted up eight bits, the eight bits of C<op_private> for | |
7421 the C<leaveloop> op, except that (in both cases) some bits will be set | |
7422 automatically. | |
7423 | |
7424 =cut | |
7425 */ | |
7426 | |
7427 OP * | |
7428 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont) | |
7429 { | |
7430 dVAR; | |
7431 LOOP *loop; | |
7432 OP *wop; | |
7433 PADOFFSET padoff = 0; | |
7434 I32 iterflags = 0; | |
7435 I32 iterpflags = 0; | |
7436 | |
7437 PERL_ARGS_ASSERT_NEWFOROP; | |
7438 | |
7439 if (sv) { | |
7440 if (sv->op_type == OP_RV2SV) { /* symbol table variable */ | |
7441 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */ | |
7442 OpTYPE_set(sv, OP_RV2GV); | |
7443 | |
7444 /* The op_type check is needed to prevent a possible segfault | |
7445 * if the loop variable is undeclared and 'strict vars' is in | |
7446 * effect. This is illegal but is nonetheless parsed, so we | |
7447 * may reach this point with an OP_CONST where we're expecting | |
7448 * an OP_GV. | |
7449 */ | |
7450 if (cUNOPx(sv)->op_first->op_type == OP_GV | |
7451 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv) | |
7452 iterpflags |= OPpITER_DEF; | |
7453 } | |
7454 else if (sv->op_type == OP_PADSV) { /* private variable */ | |
7455 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */ | |
7456 padoff = sv->op_targ; | |
7457 sv->op_targ = 0; | |
7458 op_free(sv); | |
7459 sv = NULL; | |
7460 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX); | |
7461 } | |
7462 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN) | |
7463 NOOP; | |
7464 else | |
7465 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]); | |
7466 if (padoff) { | |
7467 PADNAME * const pn = PAD_COMPNAME(padoff); | |
7468 const char * const name = PadnamePV(pn); | |
7469 | |
7470 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_') | |
7471 iterpflags |= OPpITER_DEF; | |
7472 } | |
7473 } | |
7474 else { | |
7475 const PADOFFSET offset = pad_findmy_pvs("$_", 0); | |
7476 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) { | |
7477 sv = newGVOP(OP_GV, 0, PL_defgv); | |
7478 } | |
7479 else { | |
7480 padoff = offset; | |
7481 } | |
7482 iterpflags |= OPpITER_DEF; | |
7483 } | |
7484 | |
7485 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { | |
7486 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), 1), OP_GREPSTART); | |
7487 iterflags |= OPf_STACKED; | |
7488 } | |
7489 else if (expr->op_type == OP_NULL && | |
7490 (expr->op_flags & OPf_KIDS) && | |
7491 ((BINOP*)expr)->op_first->op_type == OP_FLOP) | |
7492 { | |
7493 /* Basically turn for($x..$y) into the same as for($x,$y), but we | |
7494 * set the STACKED flag to indicate that these values are to be | |
7495 * treated as min/max values by 'pp_enteriter'. | |
7496 */ | |
7497 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first; | |
7498 LOGOP* const range = (LOGOP*) flip->op_first; | |
7499 OP* const left = range->op_first; | |
7500 OP* const right = OpSIBLING(left); | |
7501 LISTOP* listop; | |
7502 | |
7503 range->op_flags &= ~OPf_KIDS; | |
7504 /* detach range's children */ | |
7505 op_sibling_splice((OP*)range, NULL, -1, NULL); | |
7506 | |
7507 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right); | |
7508 listop->op_first->op_next = range->op_next; | |
7509 left->op_next = range->op_other; | |
7510 right->op_next = (OP*)listop; | |
7511 listop->op_next = listop->op_first; | |
7512 | |
7513 op_free(expr); | |
7514 expr = (OP*)(listop); | |
7515 op_null(expr); | |
7516 iterflags |= OPf_STACKED; | |
7517 } | |
7518 else { | |
7519 expr = op_lvalue(force_list(expr, 1), OP_GREPSTART); | |
7520 } | |
7521 | |
7522 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags, | |
7523 op_append_elem(OP_LIST, list(expr), | |
7524 scalar(sv))); | |
7525 assert(!loop->op_next); | |
7526 /* for my $x () sets OPpLVAL_INTRO; | |
7527 * for our $x () sets OPpOUR_INTRO */ | |
7528 loop->op_private = (U8)iterpflags; | |
7529 if (loop->op_slabbed | |
7530 && DIFF(loop, OpSLOT(loop)->opslot_next) | |
7531 < SIZE_TO_PSIZE(sizeof(LOOP))) | |
7532 { | |
7533 LOOP *tmp; | |
7534 NewOp(1234,tmp,1,LOOP); | |
7535 Copy(loop,tmp,1,LISTOP); | |
7536 #ifdef PERL_OP_PARENT | |
7537 assert(loop->op_last->op_sibparent == (OP*)loop); | |
7538 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */ | |
7539 #endif | |
7540 S_op_destroy(aTHX_ (OP*)loop); | |
7541 loop = tmp; | |
7542 } | |
7543 else if (!loop->op_slabbed) | |
7544 { | |
7545 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP)); | |
7546 #ifdef PERL_OP_PARENT | |
7547 OpLASTSIB_set(loop->op_last, (OP*)loop); | |
7548 #endif | |
7549 } | |
7550 loop->op_targ = padoff; | |
7551 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0); | |
7552 return wop; | |
7553 } | |
7554 | |
7555 /* | |
7556 =for apidoc Am|OP *|newLOOPEX|I32 type|OP *label | |
7557 | |
7558 Constructs, checks, and returns a loop-exiting op (such as C<goto> | |
7559 or C<last>). I<type> is the opcode. I<label> supplies the parameter | |
7560 determining the target of the op; it is consumed by this function and | |
7561 becomes part of the constructed op tree. | |
7562 | |
7563 =cut | |
7564 */ | |
7565 | |
7566 OP* | |
7567 Perl_newLOOPEX(pTHX_ I32 type, OP *label) | |
7568 { | |
7569 OP *o = NULL; | |
7570 | |
7571 PERL_ARGS_ASSERT_NEWLOOPEX; | |
7572 | |
7573 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP | |
7574 || type == OP_CUSTOM); | |
7575 | |
7576 if (type != OP_GOTO) { | |
7577 /* "last()" means "last" */ | |
7578 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) { | |
7579 o = newOP(type, OPf_SPECIAL); | |
7580 } | |
7581 } | |
7582 else { | |
7583 /* Check whether it's going to be a goto &function */ | |
7584 if (label->op_type == OP_ENTERSUB | |
7585 && !(label->op_flags & OPf_STACKED)) | |
7586 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN)); | |
7587 } | |
7588 | |
7589 /* Check for a constant argument */ | |
7590 if (label->op_type == OP_CONST) { | |
7591 SV * const sv = ((SVOP *)label)->op_sv; | |
7592 STRLEN l; | |
7593 const char *s = SvPV_const(sv,l); | |
7594 if (l == strlen(s)) { | |
7595 o = newPVOP(type, | |
7596 SvUTF8(((SVOP*)label)->op_sv), | |
7597 savesharedpv( | |
7598 SvPV_nolen_const(((SVOP*)label)->op_sv))); | |
7599 } | |
7600 } | |
7601 | |
7602 /* If we have already created an op, we do not need the label. */ | |
7603 if (o) | |
7604 op_free(label); | |
7605 else o = newUNOP(type, OPf_STACKED, label); | |
7606 | |
7607 PL_hints |= HINT_BLOCK_SCOPE; | |
7608 return o; | |
7609 } | |
7610 | |
7611 /* if the condition is a literal array or hash | |
7612 (or @{ ... } etc), make a reference to it. | |
7613 */ | |
7614 STATIC OP * | |
7615 S_ref_array_or_hash(pTHX_ OP *cond) | |
7616 { | |
7617 if (cond | |
7618 && (cond->op_type == OP_RV2AV | |
7619 || cond->op_type == OP_PADAV | |
7620 || cond->op_type == OP_RV2HV | |
7621 || cond->op_type == OP_PADHV)) | |
7622 | |
7623 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN)); | |
7624 | |
7625 else if(cond | |
7626 && (cond->op_type == OP_ASLICE | |
7627 || cond->op_type == OP_KVASLICE | |
7628 || cond->op_type == OP_HSLICE | |
7629 || cond->op_type == OP_KVHSLICE)) { | |
7630 | |
7631 /* anonlist now needs a list from this op, was previously used in | |
7632 * scalar context */ | |
7633 cond->op_flags |= ~(OPf_WANT_SCALAR | OPf_REF); | |
7634 cond->op_flags |= OPf_WANT_LIST; | |
7635 | |
7636 return newANONLIST(op_lvalue(cond, OP_ANONLIST)); | |
7637 } | |
7638 | |
7639 else | |
7640 return cond; | |
7641 } | |
7642 | |
7643 /* These construct the optree fragments representing given() | |
7644 and when() blocks. | |
7645 | |
7646 entergiven and enterwhen are LOGOPs; the op_other pointer | |
7647 points up to the associated leave op. We need this so we | |
7648 can put it in the context and make break/continue work. | |
7649 (Also, of course, pp_enterwhen will jump straight to | |
7650 op_other if the match fails.) | |
7651 */ | |
7652 | |
7653 STATIC OP * | |
7654 S_newGIVWHENOP(pTHX_ OP *cond, OP *block, | |
7655 I32 enter_opcode, I32 leave_opcode, | |
7656 PADOFFSET entertarg) | |
7657 { | |
7658 dVAR; | |
7659 LOGOP *enterop; | |
7660 OP *o; | |
7661 | |
7662 PERL_ARGS_ASSERT_NEWGIVWHENOP; | |
7663 | |
7664 enterop = S_alloc_LOGOP(aTHX_ enter_opcode, block, NULL); | |
7665 enterop->op_targ = ((entertarg == NOT_IN_PAD) ? 0 : entertarg); | |
7666 enterop->op_private = 0; | |
7667 | |
7668 o = newUNOP(leave_opcode, 0, (OP *) enterop); | |
7669 | |
7670 if (cond) { | |
7671 /* prepend cond if we have one */ | |
7672 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond)); | |
7673 | |
7674 o->op_next = LINKLIST(cond); | |
7675 cond->op_next = (OP *) enterop; | |
7676 } | |
7677 else { | |
7678 /* This is a default {} block */ | |
7679 enterop->op_flags |= OPf_SPECIAL; | |
7680 o ->op_flags |= OPf_SPECIAL; | |
7681 | |
7682 o->op_next = (OP *) enterop; | |
7683 } | |
7684 | |
7685 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since | |
7686 entergiven and enterwhen both | |
7687 use ck_null() */ | |
7688 | |
7689 enterop->op_next = LINKLIST(block); | |
7690 block->op_next = enterop->op_other = o; | |
7691 | |
7692 return o; | |
7693 } | |
7694 | |
7695 /* Does this look like a boolean operation? For these purposes | |
7696 a boolean operation is: | |
7697 - a subroutine call [*] | |
7698 - a logical connective | |
7699 - a comparison operator | |
7700 - a filetest operator, with the exception of -s -M -A -C | |
7701 - defined(), exists() or eof() | |
7702 - /$re/ or $foo =~ /$re/ | |
7703 | |
7704 [*] possibly surprising | |
7705 */ | |
7706 STATIC bool | |
7707 S_looks_like_bool(pTHX_ const OP *o) | |
7708 { | |
7709 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL; | |
7710 | |
7711 switch(o->op_type) { | |
7712 case OP_OR: | |
7713 case OP_DOR: | |
7714 return looks_like_bool(cLOGOPo->op_first); | |
7715 | |
7716 case OP_AND: | |
7717 { | |
7718 OP* sibl = OpSIBLING(cLOGOPo->op_first); | |
7719 ASSUME(sibl); | |
7720 return ( | |
7721 looks_like_bool(cLOGOPo->op_first) | |
7722 && looks_like_bool(sibl)); | |
7723 } | |
7724 | |
7725 case OP_NULL: | |
7726 case OP_SCALAR: | |
7727 return ( | |
7728 o->op_flags & OPf_KIDS | |
7729 && looks_like_bool(cUNOPo->op_first)); | |
7730 | |
7731 case OP_ENTERSUB: | |
7732 | |
7733 case OP_NOT: case OP_XOR: | |
7734 | |
7735 case OP_EQ: case OP_NE: case OP_LT: | |
7736 case OP_GT: case OP_LE: case OP_GE: | |
7737 | |
7738 case OP_I_EQ: case OP_I_NE: case OP_I_LT: | |
7739 case OP_I_GT: case OP_I_LE: case OP_I_GE: | |
7740 | |
7741 case OP_SEQ: case OP_SNE: case OP_SLT: | |
7742 case OP_SGT: case OP_SLE: case OP_SGE: | |
7743 | |
7744 case OP_SMARTMATCH: | |
7745 | |
7746 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC: | |
7747 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC: | |
7748 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED: | |
7749 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR: | |
7750 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR: | |
7751 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID: | |
7752 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY: | |
7753 case OP_FTTEXT: case OP_FTBINARY: | |
7754 | |
7755 case OP_DEFINED: case OP_EXISTS: | |
7756 case OP_MATCH: case OP_EOF: | |
7757 | |
7758 case OP_FLOP: | |
7759 | |
7760 return TRUE; | |
7761 | |
7762 case OP_CONST: | |
7763 /* Detect comparisons that have been optimized away */ | |
7764 if (cSVOPo->op_sv == &PL_sv_yes | |
7765 || cSVOPo->op_sv == &PL_sv_no) | |
7766 | |
7767 return TRUE; | |
7768 else | |
7769 return FALSE; | |
7770 | |
7771 /* FALLTHROUGH */ | |
7772 default: | |
7773 return FALSE; | |
7774 } | |
7775 } | |
7776 | |
7777 /* | |
7778 =for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off | |
7779 | |
7780 Constructs, checks, and returns an op tree expressing a C<given> block. | |
7781 I<cond> supplies the expression that will be locally assigned to a lexical | |
7782 variable, and I<block> supplies the body of the C<given> construct; they | |
7783 are consumed by this function and become part of the constructed op tree. | |
7784 I<defsv_off> is the pad offset of the scalar lexical variable that will | |
7785 be affected. If it is 0, the global $_ will be used. | |
7786 | |
7787 =cut | |
7788 */ | |
7789 | |
7790 OP * | |
7791 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off) | |
7792 { | |
7793 PERL_ARGS_ASSERT_NEWGIVENOP; | |
7794 return newGIVWHENOP( | |
7795 ref_array_or_hash(cond), | |
7796 block, | |
7797 OP_ENTERGIVEN, OP_LEAVEGIVEN, | |
7798 defsv_off); | |
7799 } | |
7800 | |
7801 /* | |
7802 =for apidoc Am|OP *|newWHENOP|OP *cond|OP *block | |
7803 | |
7804 Constructs, checks, and returns an op tree expressing a C<when> block. | |
7805 I<cond> supplies the test expression, and I<block> supplies the block | |
7806 that will be executed if the test evaluates to true; they are consumed | |
7807 by this function and become part of the constructed op tree. I<cond> | |
7808 will be interpreted DWIMically, often as a comparison against C<$_>, | |
7809 and may be null to generate a C<default> block. | |
7810 | |
7811 =cut | |
7812 */ | |
7813 | |
7814 OP * | |
7815 Perl_newWHENOP(pTHX_ OP *cond, OP *block) | |
7816 { | |
7817 const bool cond_llb = (!cond || looks_like_bool(cond)); | |
7818 OP *cond_op; | |
7819 | |
7820 PERL_ARGS_ASSERT_NEWWHENOP; | |
7821 | |
7822 if (cond_llb) | |
7823 cond_op = cond; | |
7824 else { | |
7825 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL, | |
7826 newDEFSVOP(), | |
7827 scalar(ref_array_or_hash(cond))); | |
7828 } | |
7829 | |
7830 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0); | |
7831 } | |
7832 | |
7833 /* must not conflict with SVf_UTF8 */ | |
7834 #define CV_CKPROTO_CURSTASH 0x1 | |
7835 | |
7836 void | |
7837 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p, | |
7838 const STRLEN len, const U32 flags) | |
7839 { | |
7840 SV *name = NULL, *msg; | |
7841 const char * cvp = SvROK(cv) | |
7842 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV | |
7843 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv)) | |
7844 : "" | |
7845 : CvPROTO(cv); | |
7846 STRLEN clen = CvPROTOLEN(cv), plen = len; | |
7847 | |
7848 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS; | |
7849 | |
7850 if (p == NULL && cvp == NULL) | |
7851 return; | |
7852 | |
7853 if (!ckWARN_d(WARN_PROTOTYPE)) | |
7854 return; | |
7855 | |
7856 if (p && cvp) { | |
7857 p = S_strip_spaces(aTHX_ p, &plen); | |
7858 cvp = S_strip_spaces(aTHX_ cvp, &clen); | |
7859 if ((flags & SVf_UTF8) == SvUTF8(cv)) { | |
7860 if (plen == clen && memEQ(cvp, p, plen)) | |
7861 return; | |
7862 } else { | |
7863 if (flags & SVf_UTF8) { | |
7864 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0) | |
7865 return; | |
7866 } | |
7867 else { | |
7868 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0) | |
7869 return; | |
7870 } | |
7871 } | |
7872 } | |
7873 | |
7874 msg = sv_newmortal(); | |
7875 | |
7876 if (gv) | |
7877 { | |
7878 if (isGV(gv)) | |
7879 gv_efullname3(name = sv_newmortal(), gv, NULL); | |
7880 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&') | |
7881 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP); | |
7882 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) { | |
7883 name = sv_2mortal(newSVhek(HvNAME_HEK(PL_curstash))); | |
7884 sv_catpvs(name, "::"); | |
7885 if (SvROK(gv)) { | |
7886 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV); | |
7887 assert (CvNAMED(SvRV_const(gv))); | |
7888 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv)))); | |
7889 } | |
7890 else sv_catsv(name, (SV *)gv); | |
7891 } | |
7892 else name = (SV *)gv; | |
7893 } | |
7894 sv_setpvs(msg, "Prototype mismatch:"); | |
7895 if (name) | |
7896 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, SVfARG(name)); | |
7897 if (cvp) | |
7898 Perl_sv_catpvf(aTHX_ msg, " (%"UTF8f")", | |
7899 UTF8fARG(SvUTF8(cv),clen,cvp) | |
7900 ); | |
7901 else | |
7902 sv_catpvs(msg, ": none"); | |
7903 sv_catpvs(msg, " vs "); | |
7904 if (p) | |
7905 Perl_sv_catpvf(aTHX_ msg, "(%"UTF8f")", UTF8fARG(flags & SVf_UTF8,len,p)); | |
7906 else | |
7907 sv_catpvs(msg, "none"); | |
7908 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, SVfARG(msg)); | |
7909 } | |
7910 | |
7911 static void const_sv_xsub(pTHX_ CV* cv); | |
7912 static void const_av_xsub(pTHX_ CV* cv); | |
7913 | |
7914 /* | |
7915 | |
7916 =head1 Optree Manipulation Functions | |
7917 | |
7918 =for apidoc cv_const_sv | |
7919 | |
7920 If C<cv> is a constant sub eligible for inlining, returns the constant | |
7921 value returned by the sub. Otherwise, returns NULL. | |
7922 | |
7923 Constant subs can be created with C<newCONSTSUB> or as described in | |
7924 L<perlsub/"Constant Functions">. | |
7925 | |
7926 =cut | |
7927 */ | |
7928 SV * | |
7929 Perl_cv_const_sv(const CV *const cv) | |
7930 { | |
7931 SV *sv; | |
7932 if (!cv) | |
7933 return NULL; | |
7934 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM)) | |
7935 return NULL; | |
7936 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL; | |
7937 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL; | |
7938 return sv; | |
7939 } | |
7940 | |
7941 SV * | |
7942 Perl_cv_const_sv_or_av(const CV * const cv) | |
7943 { | |
7944 if (!cv) | |
7945 return NULL; | |
7946 if (SvROK(cv)) return SvRV((SV *)cv); | |
7947 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM); | |
7948 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL; | |
7949 } | |
7950 | |
7951 /* op_const_sv: examine an optree to determine whether it's in-lineable. | |
7952 * Can be called in 2 ways: | |
7953 * | |
7954 * !allow_lex | |
7955 * look for a single OP_CONST with attached value: return the value | |
7956 * | |
7957 * allow_lex && !CvCONST(cv); | |
7958 * | |
7959 * examine the clone prototype, and if contains only a single | |
7960 * OP_CONST, return the value; or if it contains a single PADSV ref- | |
7961 * erencing an outer lexical, turn on CvCONST to indicate the CV is | |
7962 * a candidate for "constizing" at clone time, and return NULL. | |
7963 */ | |
7964 | |
7965 static SV * | |
7966 S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex) | |
7967 { | |
7968 SV *sv = NULL; | |
7969 bool padsv = FALSE; | |
7970 | |
7971 assert(o); | |
7972 assert(cv); | |
7973 | |
7974 for (; o; o = o->op_next) { | |
7975 const OPCODE type = o->op_type; | |
7976 | |
7977 if (type == OP_NEXTSTATE || type == OP_LINESEQ | |
7978 || type == OP_NULL | |
7979 || type == OP_PUSHMARK) | |
7980 continue; | |
7981 if (type == OP_DBSTATE) | |
7982 continue; | |
7983 if (type == OP_LEAVESUB) | |
7984 break; | |
7985 if (sv) | |
7986 return NULL; | |
7987 if (type == OP_CONST && cSVOPo->op_sv) | |
7988 sv = cSVOPo->op_sv; | |
7989 else if (type == OP_UNDEF && !o->op_private) { | |
7990 sv = newSV(0); | |
7991 SAVEFREESV(sv); | |
7992 } | |
7993 else if (allow_lex && type == OP_PADSV) { | |
7994 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEt_OUTER) | |
7995 { | |
7996 sv = &PL_sv_undef; /* an arbitrary non-null value */ | |
7997 padsv = TRUE; | |
7998 } | |
7999 else | |
8000 return NULL; | |
8001 } | |
8002 else { | |
8003 return NULL; | |
8004 } | |
8005 } | |
8006 if (padsv) { | |
8007 CvCONST_on(cv); | |
8008 return NULL; | |
8009 } | |
8010 return sv; | |
8011 } | |
8012 | |
8013 static bool | |
8014 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o, | |
8015 PADNAME * const name, SV ** const const_svp) | |
8016 { | |
8017 assert (cv); | |
8018 assert (o || name); | |
8019 assert (const_svp); | |
8020 if ((!block | |
8021 )) { | |
8022 if (CvFLAGS(PL_compcv)) { | |
8023 /* might have had built-in attrs applied */ | |
8024 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv); | |
8025 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl | |
8026 && ckWARN(WARN_MISC)) | |
8027 { | |
8028 /* protect against fatal warnings leaking compcv */ | |
8029 SAVEFREESV(PL_compcv); | |
8030 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined"); | |
8031 SvREFCNT_inc_simple_void_NN(PL_compcv); | |
8032 } | |
8033 CvFLAGS(cv) |= | |
8034 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS | |
8035 & ~(CVf_LVALUE * pureperl)); | |
8036 } | |
8037 return FALSE; | |
8038 } | |
8039 | |
8040 /* redundant check for speed: */ | |
8041 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) { | |
8042 const line_t oldline = CopLINE(PL_curcop); | |
8043 SV *namesv = o | |
8044 ? cSVOPo->op_sv | |
8045 : sv_2mortal(newSVpvn_utf8( | |
8046 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name) | |
8047 )); | |
8048 if (PL_parser && PL_parser->copline != NOLINE) | |
8049 /* This ensures that warnings are reported at the first | |
8050 line of a redefinition, not the last. */ | |
8051 CopLINE_set(PL_curcop, PL_parser->copline); | |
8052 /* protect against fatal warnings leaking compcv */ | |
8053 SAVEFREESV(PL_compcv); | |
8054 report_redefined_cv(namesv, cv, const_svp); | |
8055 SvREFCNT_inc_simple_void_NN(PL_compcv); | |
8056 CopLINE_set(PL_curcop, oldline); | |
8057 } | |
8058 SAVEFREESV(cv); | |
8059 return TRUE; | |
8060 } | |
8061 | |
8062 CV * | |
8063 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block) | |
8064 { | |
8065 CV **spot; | |
8066 SV **svspot; | |
8067 const char *ps; | |
8068 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */ | |
8069 U32 ps_utf8 = 0; | |
8070 CV *cv = NULL; | |
8071 CV *compcv = PL_compcv; | |
8072 SV *const_sv; | |
8073 PADNAME *name; | |
8074 PADOFFSET pax = o->op_targ; | |
8075 CV *outcv = CvOUTSIDE(PL_compcv); | |
8076 CV *clonee = NULL; | |
8077 HEK *hek = NULL; | |
8078 bool reusable = FALSE; | |
8079 OP *start = NULL; | |
8080 #ifdef PERL_DEBUG_READONLY_OPS | |
8081 OPSLAB *slab = NULL; | |
8082 #endif | |
8083 | |
8084 PERL_ARGS_ASSERT_NEWMYSUB; | |
8085 | |
8086 /* Find the pad slot for storing the new sub. | |
8087 We cannot use PL_comppad, as it is the pad owned by the new sub. We | |
8088 need to look in CvOUTSIDE and find the pad belonging to the enclos- | |
8089 ing sub. And then we need to dig deeper if this is a lexical from | |
8090 outside, as in: | |
8091 my sub foo; sub { sub foo { } } | |
8092 */ | |
8093 redo: | |
8094 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax]; | |
8095 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) { | |
8096 pax = PARENT_PAD_INDEX(name); | |
8097 outcv = CvOUTSIDE(outcv); | |
8098 assert(outcv); | |
8099 goto redo; | |
8100 } | |
8101 svspot = | |
8102 &PadARRAY(PadlistARRAY(CvPADLIST(outcv)) | |
8103 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax]; | |
8104 spot = (CV **)svspot; | |
8105 | |
8106 if (!(PL_parser && PL_parser->error_count)) | |
8107 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name)); | |
8108 | |
8109 if (proto) { | |
8110 assert(proto->op_type == OP_CONST); | |
8111 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len); | |
8112 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv); | |
8113 } | |
8114 else | |
8115 ps = NULL; | |
8116 | |
8117 if (proto) | |
8118 SAVEFREEOP(proto); | |
8119 if (attrs) | |
8120 SAVEFREEOP(attrs); | |
8121 | |
8122 if (PL_parser && PL_parser->error_count) { | |
8123 op_free(block); | |
8124 SvREFCNT_dec(PL_compcv); | |
8125 PL_compcv = 0; | |
8126 goto done; | |
8127 } | |
8128 | |
8129 if (CvDEPTH(outcv) && CvCLONE(compcv)) { | |
8130 cv = *spot; | |
8131 svspot = (SV **)(spot = &clonee); | |
8132 } | |
8133 else if (PadnameIsSTATE(name) || CvDEPTH(outcv)) | |
8134 cv = *spot; | |
8135 else { | |
8136 assert (SvTYPE(*spot) == SVt_PVCV); | |
8137 if (CvNAMED(*spot)) | |
8138 hek = CvNAME_HEK(*spot); | |
8139 else { | |
8140 dVAR; | |
8141 U32 hash; | |
8142 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1); | |
8143 CvNAME_HEK_set(*spot, hek = | |
8144 share_hek( | |
8145 PadnamePV(name)+1, | |
8146 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1), | |
8147 hash | |
8148 ) | |
8149 ); | |
8150 CvLEXICAL_on(*spot); | |
8151 } | |
8152 cv = PadnamePROTOCV(name); | |
8153 svspot = (SV **)(spot = &PadnamePROTOCV(name)); | |
8154 } | |
8155 | |
8156 if (block) { | |
8157 /* This makes sub {}; work as expected. */ | |
8158 if (block->op_type == OP_STUB) { | |
8159 const line_t l = PL_parser->copline; | |
8160 op_free(block); | |
8161 block = newSTATEOP(0, NULL, 0); | |
8162 PL_parser->copline = l; | |
8163 } | |
8164 block = CvLVALUE(compcv) | |
8165 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)) | |
8166 ? newUNOP(OP_LEAVESUBLV, 0, | |
8167 op_lvalue(scalarseq(block), OP_LEAVESUBLV)) | |
8168 : newUNOP(OP_LEAVESUB, 0, scalarseq(block)); | |
8169 start = LINKLIST(block); | |
8170 block->op_next = 0; | |
8171 } | |
8172 | |
8173 if (!block || !ps || *ps || attrs | |
8174 || CvLVALUE(compcv) | |
8175 ) | |
8176 const_sv = NULL; | |
8177 else | |
8178 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE); | |
8179 | |
8180 if (cv) { | |
8181 const bool exists = CvROOT(cv) || CvXSUB(cv); | |
8182 | |
8183 /* if the subroutine doesn't exist and wasn't pre-declared | |
8184 * with a prototype, assume it will be AUTOLOADed, | |
8185 * skipping the prototype check | |
8186 */ | |
8187 if (exists || SvPOK(cv)) | |
8188 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len, | |
8189 ps_utf8); | |
8190 /* already defined? */ | |
8191 if (exists) { | |
8192 if (S_already_defined(aTHX_ cv,block,NULL,name,&const_sv)) | |
8193 cv = NULL; | |
8194 else { | |
8195 if (attrs) goto attrs; | |
8196 /* just a "sub foo;" when &foo is already defined */ | |
8197 SAVEFREESV(compcv); | |
8198 goto done; | |
8199 } | |
8200 } | |
8201 else if (CvDEPTH(outcv) && CvCLONE(compcv)) { | |
8202 cv = NULL; | |
8203 reusable = TRUE; | |
8204 } | |
8205 } | |
8206 if (const_sv) { | |
8207 SvREFCNT_inc_simple_void_NN(const_sv); | |
8208 SvFLAGS(const_sv) |= SVs_PADTMP; | |
8209 if (cv) { | |
8210 assert(!CvROOT(cv) && !CvCONST(cv)); | |
8211 cv_forget_slab(cv); | |
8212 } | |
8213 else { | |
8214 cv = MUTABLE_CV(newSV_type(SVt_PVCV)); | |
8215 CvFILE_set_from_cop(cv, PL_curcop); | |
8216 CvSTASH_set(cv, PL_curstash); | |
8217 *spot = cv; | |
8218 } | |
8219 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */ | |
8220 CvXSUBANY(cv).any_ptr = const_sv; | |
8221 CvXSUB(cv) = const_sv_xsub; | |
8222 CvCONST_on(cv); | |
8223 CvISXSUB_on(cv); | |
8224 PoisonPADLIST(cv); | |
8225 CvFLAGS(cv) |= CvMETHOD(compcv); | |
8226 op_free(block); | |
8227 SvREFCNT_dec(compcv); | |
8228 PL_compcv = NULL; | |
8229 goto setname; | |
8230 } | |
8231 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to | |
8232 determine whether this sub definition is in the same scope as its | |
8233 declaration. If this sub definition is inside an inner named pack- | |
8234 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to | |
8235 the package sub. So check PadnameOUTER(name) too. | |
8236 */ | |
8237 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) { | |
8238 assert(!CvWEAKOUTSIDE(compcv)); | |
8239 SvREFCNT_dec(CvOUTSIDE(compcv)); | |
8240 CvWEAKOUTSIDE_on(compcv); | |
8241 } | |
8242 /* XXX else do we have a circular reference? */ | |
8243 if (cv) { /* must reuse cv in case stub is referenced elsewhere */ | |
8244 /* transfer PL_compcv to cv */ | |
8245 if (block | |
8246 ) { | |
8247 cv_flags_t preserved_flags = | |
8248 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED); | |
8249 PADLIST *const temp_padl = CvPADLIST(cv); | |
8250 CV *const temp_cv = CvOUTSIDE(cv); | |
8251 const cv_flags_t other_flags = | |
8252 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE); | |
8253 OP * const cvstart = CvSTART(cv); | |
8254 | |
8255 SvPOK_off(cv); | |
8256 CvFLAGS(cv) = | |
8257 CvFLAGS(compcv) | preserved_flags; | |
8258 CvOUTSIDE(cv) = CvOUTSIDE(compcv); | |
8259 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv); | |
8260 CvPADLIST_set(cv, CvPADLIST(compcv)); | |
8261 CvOUTSIDE(compcv) = temp_cv; | |
8262 CvPADLIST_set(compcv, temp_padl); | |
8263 CvSTART(cv) = CvSTART(compcv); | |
8264 CvSTART(compcv) = cvstart; | |
8265 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE); | |
8266 CvFLAGS(compcv) |= other_flags; | |
8267 | |
8268 if (CvFILE(cv) && CvDYNFILE(cv)) { | |
8269 Safefree(CvFILE(cv)); | |
8270 } | |
8271 | |
8272 /* inner references to compcv must be fixed up ... */ | |
8273 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv); | |
8274 if (PERLDB_INTER)/* Advice debugger on the new sub. */ | |
8275 ++PL_sub_generation; | |
8276 } | |
8277 else { | |
8278 /* Might have had built-in attributes applied -- propagate them. */ | |
8279 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS); | |
8280 } | |
8281 /* ... before we throw it away */ | |
8282 SvREFCNT_dec(compcv); | |
8283 PL_compcv = compcv = cv; | |
8284 } | |
8285 else { | |
8286 cv = compcv; | |
8287 *spot = cv; | |
8288 } | |
8289 setname: | |
8290 CvLEXICAL_on(cv); | |
8291 if (!CvNAME_HEK(cv)) { | |
8292 if (hek) (void)share_hek_hek(hek); | |
8293 else { | |
8294 dVAR; | |
8295 U32 hash; | |
8296 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1); | |
8297 hek = share_hek(PadnamePV(name)+1, | |
8298 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1), | |
8299 hash); | |
8300 } | |
8301 CvNAME_HEK_set(cv, hek); | |
8302 } | |
8303 if (const_sv) goto clone; | |
8304 | |
8305 CvFILE_set_from_cop(cv, PL_curcop); | |
8306 CvSTASH_set(cv, PL_curstash); | |
8307 | |
8308 if (ps) { | |
8309 sv_setpvn(MUTABLE_SV(cv), ps, ps_len); | |
8310 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv)); | |
8311 } | |
8312 | |
8313 if (!block) | |
8314 goto attrs; | |
8315 | |
8316 /* If we assign an optree to a PVCV, then we've defined a subroutine that | |
8317 the debugger could be able to set a breakpoint in, so signal to | |
8318 pp_entereval that it should not throw away any saved lines at scope | |
8319 exit. */ | |
8320 | |
8321 PL_breakable_sub_gen++; | |
8322 CvROOT(cv) = block; | |
8323 CvROOT(cv)->op_private |= OPpREFCOUNTED; | |
8324 OpREFCNT_set(CvROOT(cv), 1); | |
8325 /* The cv no longer needs to hold a refcount on the slab, as CvROOT | |
8326 itself has a refcount. */ | |
8327 CvSLABBED_off(cv); | |
8328 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv)); | |
8329 #ifdef PERL_DEBUG_READONLY_OPS | |
8330 slab = (OPSLAB *)CvSTART(cv); | |
8331 #endif | |
8332 CvSTART(cv) = start; | |
8333 CALL_PEEP(start); | |
8334 finalize_optree(CvROOT(cv)); | |
8335 S_prune_chain_head(&CvSTART(cv)); | |
8336 | |
8337 /* now that optimizer has done its work, adjust pad values */ | |
8338 | |
8339 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB); | |
8340 | |
8341 attrs: | |
8342 if (attrs) { | |
8343 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */ | |
8344 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs); | |
8345 } | |
8346 | |
8347 if (block) { | |
8348 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) { | |
8349 SV * const tmpstr = sv_newmortal(); | |
8350 GV * const db_postponed = gv_fetchpvs("DB::postponed", | |
8351 GV_ADDMULTI, SVt_PVHV); | |
8352 HV *hv; | |
8353 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld", | |
8354 CopFILE(PL_curcop), | |
8355 (long)PL_subline, | |
8356 (long)CopLINE(PL_curcop)); | |
8357 if (HvNAME_HEK(PL_curstash)) { | |
8358 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash)); | |
8359 sv_catpvs(tmpstr, "::"); | |
8360 } | |
8361 else sv_setpvs(tmpstr, "__ANON__::"); | |
8362 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1, | |
8363 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES); | |
8364 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), | |
8365 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0); | |
8366 hv = GvHVn(db_postponed); | |
8367 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) { | |
8368 CV * const pcv = GvCV(db_postponed); | |
8369 if (pcv) { | |
8370 dSP; | |
8371 PUSHMARK(SP); | |
8372 XPUSHs(tmpstr); | |
8373 PUTBACK; | |
8374 call_sv(MUTABLE_SV(pcv), G_DISCARD); | |
8375 } | |
8376 } | |
8377 } | |
8378 } | |
8379 | |
8380 clone: | |
8381 if (clonee) { | |
8382 assert(CvDEPTH(outcv)); | |
8383 spot = (CV **) | |
8384 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax]; | |
8385 if (reusable) cv_clone_into(clonee, *spot); | |
8386 else *spot = cv_clone(clonee); | |
8387 SvREFCNT_dec_NN(clonee); | |
8388 cv = *spot; | |
8389 } | |
8390 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) { | |
8391 PADOFFSET depth = CvDEPTH(outcv); | |
8392 while (--depth) { | |
8393 SV *oldcv; | |
8394 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax]; | |
8395 oldcv = *svspot; | |
8396 *svspot = SvREFCNT_inc_simple_NN(cv); | |
8397 SvREFCNT_dec(oldcv); | |
8398 } | |
8399 } | |
8400 | |
8401 done: | |
8402 if (PL_parser) | |
8403 PL_parser->copline = NOLINE; | |
8404 LEAVE_SCOPE(floor); | |
8405 #ifdef PERL_DEBUG_READONLY_OPS | |
8406 if (slab) | |
8407 Slab_to_ro(slab); | |
8408 #endif | |
8409 op_free(o); | |
8410 return cv; | |
8411 } | |
8412 | |
8413 /* _x = extended */ | |
8414 CV * | |
8415 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, | |
8416 OP *block, bool o_is_gv) | |
8417 { | |
8418 GV *gv; | |
8419 const char *ps; | |
8420 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */ | |
8421 U32 ps_utf8 = 0; | |
8422 CV *cv = NULL; | |
8423 SV *const_sv; | |
8424 const bool ec = PL_parser && PL_parser->error_count; | |
8425 /* If the subroutine has no body, no attributes, and no builtin attributes | |
8426 then it's just a sub declaration, and we may be able to get away with | |
8427 storing with a placeholder scalar in the symbol table, rather than a | |
8428 full CV. If anything is present then it will take a full CV to | |
8429 store it. */ | |
8430 const I32 gv_fetch_flags | |
8431 = ec ? GV_NOADD_NOINIT : | |
8432 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) | |
8433 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT; | |
8434 STRLEN namlen = 0; | |
8435 const char * const name = | |
8436 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL; | |
8437 bool has_name; | |
8438 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv); | |
8439 bool evanescent = FALSE; | |
8440 OP *start = NULL; | |
8441 #ifdef PERL_DEBUG_READONLY_OPS | |
8442 OPSLAB *slab = NULL; | |
8443 #endif | |
8444 | |
8445 if (o_is_gv) { | |
8446 gv = (GV*)o; | |
8447 o = NULL; | |
8448 has_name = TRUE; | |
8449 } else if (name) { | |
8450 /* Try to optimise and avoid creating a GV. Instead, the CV’s name | |
8451 hek and CvSTASH pointer together can imply the GV. If the name | |
8452 contains a package name, then GvSTASH(CvGV(cv)) may differ from | |
8453 CvSTASH, so forego the optimisation if we find any. | |
8454 Also, we may be called from load_module at run time, so | |
8455 PL_curstash (which sets CvSTASH) may not point to the stash the | |
8456 sub is stored in. */ | |
8457 const I32 flags = | |
8458 ec ? GV_NOADD_NOINIT | |
8459 : PL_curstash != CopSTASH(PL_curcop) | |
8460 || memchr(name, ':', namlen) || memchr(name, '\'', namlen) | |
8461 ? gv_fetch_flags | |
8462 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL; | |
8463 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV); | |
8464 has_name = TRUE; | |
8465 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) { | |
8466 SV * const sv = sv_newmortal(); | |
8467 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]", | |
8468 PL_curstash ? "__ANON__" : "__ANON__::__ANON__", | |
8469 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); | |
8470 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV); | |
8471 has_name = TRUE; | |
8472 } else if (PL_curstash) { | |
8473 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV); | |
8474 has_name = FALSE; | |
8475 } else { | |
8476 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV); | |
8477 has_name = FALSE; | |
8478 } | |
8479 if (!ec) | |
8480 move_proto_attr(&proto, &attrs, | |
8481 isGV(gv) ? gv : (GV *)cSVOPo->op_sv); | |
8482 | |
8483 if (proto) { | |
8484 assert(proto->op_type == OP_CONST); | |
8485 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len); | |
8486 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv); | |
8487 } | |
8488 else | |
8489 ps = NULL; | |
8490 | |
8491 if (o) | |
8492 SAVEFREEOP(o); | |
8493 if (proto) | |
8494 SAVEFREEOP(proto); | |
8495 if (attrs) | |
8496 SAVEFREEOP(attrs); | |
8497 | |
8498 if (ec) { | |
8499 op_free(block); | |
8500 if (name) SvREFCNT_dec(PL_compcv); | |
8501 else cv = PL_compcv; | |
8502 PL_compcv = 0; | |
8503 if (name && block) { | |
8504 const char *s = strrchr(name, ':'); | |
8505 s = s ? s+1 : name; | |
8506 if (strEQ(s, "BEGIN")) { | |
8507 if (PL_in_eval & EVAL_KEEPERR) | |
8508 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted"); | |
8509 else { | |
8510 SV * const errsv = ERRSV; | |
8511 /* force display of errors found but not reported */ | |
8512 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted"); | |
8513 Perl_croak_nocontext("%"SVf, SVfARG(errsv)); | |
8514 } | |
8515 } | |
8516 } | |
8517 goto done; | |
8518 } | |
8519 | |
8520 if (!block && SvTYPE(gv) != SVt_PVGV) { | |
8521 /* If we are not defining a new sub and the existing one is not a | |
8522 full GV + CV... */ | |
8523 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) { | |
8524 /* We are applying attributes to an existing sub, so we need it | |
8525 upgraded if it is a constant. */ | |
8526 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV) | |
8527 gv_init_pvn(gv, PL_curstash, name, namlen, | |
8528 SVf_UTF8 * name_is_utf8); | |
8529 } | |
8530 else { /* Maybe prototype now, and had at maximum | |
8531 a prototype or const/sub ref before. */ | |
8532 if (SvTYPE(gv) > SVt_NULL) { | |
8533 cv_ckproto_len_flags((const CV *)gv, | |
8534 o ? (const GV *)cSVOPo->op_sv : NULL, ps, | |
8535 ps_len, ps_utf8); | |
8536 } | |
8537 if (!SvROK(gv)) { | |
8538 if (ps) { | |
8539 sv_setpvn(MUTABLE_SV(gv), ps, ps_len); | |
8540 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(gv)); | |
8541 } | |
8542 else | |
8543 sv_setiv(MUTABLE_SV(gv), -1); | |
8544 } | |
8545 | |
8546 SvREFCNT_dec(PL_compcv); | |
8547 cv = PL_compcv = NULL; | |
8548 goto done; | |
8549 } | |
8550 } | |
8551 | |
8552 cv = (!name || (isGV(gv) && GvCVGEN(gv))) | |
8553 ? NULL | |
8554 : isGV(gv) | |
8555 ? GvCV(gv) | |
8556 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV | |
8557 ? (CV *)SvRV(gv) | |
8558 : NULL; | |
8559 | |
8560 if (block) { | |
8561 /* This makes sub {}; work as expected. */ | |
8562 if (block->op_type == OP_STUB) { | |
8563 const line_t l = PL_parser->copline; | |
8564 op_free(block); | |
8565 block = newSTATEOP(0, NULL, 0); | |
8566 PL_parser->copline = l; | |
8567 } | |
8568 block = CvLVALUE(PL_compcv) | |
8569 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv) | |
8570 && (!isGV(gv) || !GvASSUMECV(gv))) | |
8571 ? newUNOP(OP_LEAVESUBLV, 0, | |
8572 op_lvalue(scalarseq(block), OP_LEAVESUBLV)) | |
8573 : newUNOP(OP_LEAVESUB, 0, scalarseq(block)); | |
8574 start = LINKLIST(block); | |
8575 block->op_next = 0; | |
8576 } | |
8577 | |
8578 if (!block || !ps || *ps || attrs | |
8579 || CvLVALUE(PL_compcv) | |
8580 ) | |
8581 const_sv = NULL; | |
8582 else | |
8583 const_sv = | |
8584 S_op_const_sv(aTHX_ start, PL_compcv, cBOOL(CvCLONE(PL_compcv))); | |
8585 | |
8586 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) { | |
8587 cv_ckproto_len_flags((const CV *)gv, | |
8588 o ? (const GV *)cSVOPo->op_sv : NULL, ps, | |
8589 ps_len, ps_utf8|CV_CKPROTO_CURSTASH); | |
8590 if (SvROK(gv)) { | |
8591 /* All the other code for sub redefinition warnings expects the | |
8592 clobbered sub to be a CV. Instead of making all those code | |
8593 paths more complex, just inline the RV version here. */ | |
8594 const line_t oldline = CopLINE(PL_curcop); | |
8595 assert(IN_PERL_COMPILETIME); | |
8596 if (PL_parser && PL_parser->copline != NOLINE) | |
8597 /* This ensures that warnings are reported at the first | |
8598 line of a redefinition, not the last. */ | |
8599 CopLINE_set(PL_curcop, PL_parser->copline); | |
8600 /* protect against fatal warnings leaking compcv */ | |
8601 SAVEFREESV(PL_compcv); | |
8602 | |
8603 if (ckWARN(WARN_REDEFINE) | |
8604 || ( ckWARN_d(WARN_REDEFINE) | |
8605 && ( !const_sv || SvRV(gv) == const_sv | |
8606 || sv_cmp(SvRV(gv), const_sv) ))) | |
8607 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), | |
8608 "Constant subroutine %"SVf" redefined", | |
8609 SVfARG(cSVOPo->op_sv)); | |
8610 | |
8611 SvREFCNT_inc_simple_void_NN(PL_compcv); | |
8612 CopLINE_set(PL_curcop, oldline); | |
8613 SvREFCNT_dec(SvRV(gv)); | |
8614 } | |
8615 } | |
8616 | |
8617 if (cv) { | |
8618 const bool exists = CvROOT(cv) || CvXSUB(cv); | |
8619 | |
8620 /* if the subroutine doesn't exist and wasn't pre-declared | |
8621 * with a prototype, assume it will be AUTOLOADed, | |
8622 * skipping the prototype check | |
8623 */ | |
8624 if (exists || SvPOK(cv)) | |
8625 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8); | |
8626 /* already defined (or promised)? */ | |
8627 if (exists || (isGV(gv) && GvASSUMECV(gv))) { | |
8628 if (S_already_defined(aTHX_ cv, block, o, NULL, &const_sv)) | |
8629 cv = NULL; | |
8630 else { | |
8631 if (attrs) goto attrs; | |
8632 /* just a "sub foo;" when &foo is already defined */ | |
8633 SAVEFREESV(PL_compcv); | |
8634 goto done; | |
8635 } | |
8636 } | |
8637 } | |
8638 if (const_sv) { | |
8639 SvREFCNT_inc_simple_void_NN(const_sv); | |
8640 SvFLAGS(const_sv) |= SVs_PADTMP; | |
8641 if (cv) { | |
8642 assert(!CvROOT(cv) && !CvCONST(cv)); | |
8643 cv_forget_slab(cv); | |
8644 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */ | |
8645 CvXSUBANY(cv).any_ptr = const_sv; | |
8646 CvXSUB(cv) = const_sv_xsub; | |
8647 CvCONST_on(cv); | |
8648 CvISXSUB_on(cv); | |
8649 PoisonPADLIST(cv); | |
8650 CvFLAGS(cv) |= CvMETHOD(PL_compcv); | |
8651 } | |
8652 else { | |
8653 if (isGV(gv) || CvMETHOD(PL_compcv)) { | |
8654 if (name && isGV(gv)) | |
8655 GvCV_set(gv, NULL); | |
8656 cv = newCONSTSUB_flags( | |
8657 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0, | |
8658 const_sv | |
8659 ); | |
8660 CvFLAGS(cv) |= CvMETHOD(PL_compcv); | |
8661 } | |
8662 else { | |
8663 if (!SvROK(gv)) { | |
8664 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv); | |
8665 prepare_SV_for_RV((SV *)gv); | |
8666 SvOK_off((SV *)gv); | |
8667 SvROK_on(gv); | |
8668 } | |
8669 SvRV_set(gv, const_sv); | |
8670 } | |
8671 } | |
8672 op_free(block); | |
8673 SvREFCNT_dec(PL_compcv); | |
8674 PL_compcv = NULL; | |
8675 goto done; | |
8676 } | |
8677 if (cv) { /* must reuse cv if autoloaded */ | |
8678 /* transfer PL_compcv to cv */ | |
8679 if (block | |
8680 ) { | |
8681 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS; | |
8682 PADLIST *const temp_av = CvPADLIST(cv); | |
8683 CV *const temp_cv = CvOUTSIDE(cv); | |
8684 const cv_flags_t other_flags = | |
8685 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE); | |
8686 OP * const cvstart = CvSTART(cv); | |
8687 | |
8688 if (isGV(gv)) { | |
8689 CvGV_set(cv,gv); | |
8690 assert(!CvCVGV_RC(cv)); | |
8691 assert(CvGV(cv) == gv); | |
8692 } | |
8693 else { | |
8694 dVAR; | |
8695 U32 hash; | |
8696 PERL_HASH(hash, name, namlen); | |
8697 CvNAME_HEK_set(cv, | |
8698 share_hek(name, | |
8699 name_is_utf8 | |
8700 ? -(SSize_t)namlen | |
8701 : (SSize_t)namlen, | |
8702 hash)); | |
8703 } | |
8704 | |
8705 SvPOK_off(cv); | |
8706 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs | |
8707 | CvNAMED(cv); | |
8708 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv); | |
8709 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv); | |
8710 CvPADLIST_set(cv,CvPADLIST(PL_compcv)); | |
8711 CvOUTSIDE(PL_compcv) = temp_cv; | |
8712 CvPADLIST_set(PL_compcv, temp_av); | |
8713 CvSTART(cv) = CvSTART(PL_compcv); | |
8714 CvSTART(PL_compcv) = cvstart; | |
8715 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE); | |
8716 CvFLAGS(PL_compcv) |= other_flags; | |
8717 | |
8718 if (CvFILE(cv) && CvDYNFILE(cv)) { | |
8719 Safefree(CvFILE(cv)); | |
8720 } | |
8721 CvFILE_set_from_cop(cv, PL_curcop); | |
8722 CvSTASH_set(cv, PL_curstash); | |
8723 | |
8724 /* inner references to PL_compcv must be fixed up ... */ | |
8725 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv); | |
8726 if (PERLDB_INTER)/* Advice debugger on the new sub. */ | |
8727 ++PL_sub_generation; | |
8728 } | |
8729 else { | |
8730 /* Might have had built-in attributes applied -- propagate them. */ | |
8731 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS); | |
8732 } | |
8733 /* ... before we throw it away */ | |
8734 SvREFCNT_dec(PL_compcv); | |
8735 PL_compcv = cv; | |
8736 } | |
8737 else { | |
8738 cv = PL_compcv; | |
8739 if (name && isGV(gv)) { | |
8740 GvCV_set(gv, cv); | |
8741 GvCVGEN(gv) = 0; | |
8742 if (HvENAME_HEK(GvSTASH(gv))) | |
8743 /* sub Foo::bar { (shift)+1 } */ | |
8744 gv_method_changed(gv); | |
8745 } | |
8746 else if (name) { | |
8747 if (!SvROK(gv)) { | |
8748 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv); | |
8749 prepare_SV_for_RV((SV *)gv); | |
8750 SvOK_off((SV *)gv); | |
8751 SvROK_on(gv); | |
8752 } | |
8753 SvRV_set(gv, (SV *)cv); | |
8754 } | |
8755 } | |
8756 if (!CvHASGV(cv)) { | |
8757 if (isGV(gv)) CvGV_set(cv, gv); | |
8758 else { | |
8759 dVAR; | |
8760 U32 hash; | |
8761 PERL_HASH(hash, name, namlen); | |
8762 CvNAME_HEK_set(cv, share_hek(name, | |
8763 name_is_utf8 | |
8764 ? -(SSize_t)namlen | |
8765 : (SSize_t)namlen, | |
8766 hash)); | |
8767 } | |
8768 CvFILE_set_from_cop(cv, PL_curcop); | |
8769 CvSTASH_set(cv, PL_curstash); | |
8770 } | |
8771 | |
8772 if (ps) { | |
8773 sv_setpvn(MUTABLE_SV(cv), ps, ps_len); | |
8774 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv)); | |
8775 } | |
8776 | |
8777 if (!block) | |
8778 goto attrs; | |
8779 | |
8780 /* If we assign an optree to a PVCV, then we've defined a subroutine that | |
8781 the debugger could be able to set a breakpoint in, so signal to | |
8782 pp_entereval that it should not throw away any saved lines at scope | |
8783 exit. */ | |
8784 | |
8785 PL_breakable_sub_gen++; | |
8786 CvROOT(cv) = block; | |
8787 CvROOT(cv)->op_private |= OPpREFCOUNTED; | |
8788 OpREFCNT_set(CvROOT(cv), 1); | |
8789 /* The cv no longer needs to hold a refcount on the slab, as CvROOT | |
8790 itself has a refcount. */ | |
8791 CvSLABBED_off(cv); | |
8792 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv)); | |
8793 #ifdef PERL_DEBUG_READONLY_OPS | |
8794 slab = (OPSLAB *)CvSTART(cv); | |
8795 #endif | |
8796 CvSTART(cv) = start; | |
8797 CALL_PEEP(start); | |
8798 finalize_optree(CvROOT(cv)); | |
8799 S_prune_chain_head(&CvSTART(cv)); | |
8800 | |
8801 /* now that optimizer has done its work, adjust pad values */ | |
8802 | |
8803 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB); | |
8804 | |
8805 attrs: | |
8806 if (attrs) { | |
8807 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */ | |
8808 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv)) | |
8809 ? GvSTASH(CvGV(cv)) | |
8810 : PL_curstash; | |
8811 if (!name) SAVEFREESV(cv); | |
8812 apply_attrs(stash, MUTABLE_SV(cv), attrs); | |
8813 if (!name) SvREFCNT_inc_simple_void_NN(cv); | |
8814 } | |
8815 | |
8816 if (block && has_name) { | |
8817 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) { | |
8818 SV * const tmpstr = cv_name(cv,NULL,0); | |
8819 GV * const db_postponed = gv_fetchpvs("DB::postponed", | |
8820 GV_ADDMULTI, SVt_PVHV); | |
8821 HV *hv; | |
8822 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld", | |
8823 CopFILE(PL_curcop), | |
8824 (long)PL_subline, | |
8825 (long)CopLINE(PL_curcop)); | |
8826 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), | |
8827 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0); | |
8828 hv = GvHVn(db_postponed); | |
8829 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) { | |
8830 CV * const pcv = GvCV(db_postponed); | |
8831 if (pcv) { | |
8832 dSP; | |
8833 PUSHMARK(SP); | |
8834 XPUSHs(tmpstr); | |
8835 PUTBACK; | |
8836 call_sv(MUTABLE_SV(pcv), G_DISCARD); | |
8837 } | |
8838 } | |
8839 } | |
8840 | |
8841 if (name) { | |
8842 if (PL_parser && PL_parser->error_count) | |
8843 clear_special_blocks(name, gv, cv); | |
8844 else | |
8845 evanescent = | |
8846 process_special_blocks(floor, name, gv, cv); | |
8847 } | |
8848 } | |
8849 | |
8850 done: | |
8851 if (PL_parser) | |
8852 PL_parser->copline = NOLINE; | |
8853 LEAVE_SCOPE(floor); | |
8854 if (!evanescent) { | |
8855 #ifdef PERL_DEBUG_READONLY_OPS | |
8856 if (slab) | |
8857 Slab_to_ro(slab); | |
8858 #endif | |
8859 if (cv && name && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv))) | |
8860 pad_add_weakref(cv); | |
8861 } | |
8862 return cv; | |
8863 } | |
8864 | |
8865 STATIC void | |
8866 S_clear_special_blocks(pTHX_ const char *const fullname, | |
8867 GV *const gv, CV *const cv) { | |
8868 const char *colon; | |
8869 const char *name; | |
8870 | |
8871 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS; | |
8872 | |
8873 colon = strrchr(fullname,':'); | |
8874 name = colon ? colon + 1 : fullname; | |
8875 | |
8876 if ((*name == 'B' && strEQ(name, "BEGIN")) | |
8877 || (*name == 'E' && strEQ(name, "END")) | |
8878 || (*name == 'U' && strEQ(name, "UNITCHECK")) | |
8879 || (*name == 'C' && strEQ(name, "CHECK")) | |
8880 || (*name == 'I' && strEQ(name, "INIT"))) { | |
8881 if (!isGV(gv)) { | |
8882 (void)CvGV(cv); | |
8883 assert(isGV(gv)); | |
8884 } | |
8885 GvCV_set(gv, NULL); | |
8886 SvREFCNT_dec_NN(MUTABLE_SV(cv)); | |
8887 } | |
8888 } | |
8889 | |
8890 /* Returns true if the sub has been freed. */ | |
8891 STATIC bool | |
8892 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname, | |
8893 GV *const gv, | |
8894 CV *const cv) | |
8895 { | |
8896 const char *const colon = strrchr(fullname,':'); | |
8897 const char *const name = colon ? colon + 1 : fullname; | |
8898 | |
8899 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS; | |
8900 | |
8901 if (*name == 'B') { | |
8902 if (strEQ(name, "BEGIN")) { | |
8903 const I32 oldscope = PL_scopestack_ix; | |
8904 dSP; | |
8905 (void)CvGV(cv); | |
8906 if (floor) LEAVE_SCOPE(floor); | |
8907 ENTER; | |
8908 PUSHSTACKi(PERLSI_REQUIRE); | |
8909 SAVECOPFILE(&PL_compiling); | |
8910 SAVECOPLINE(&PL_compiling); | |
8911 SAVEVPTR(PL_curcop); | |
8912 | |
8913 DEBUG_x( dump_sub(gv) ); | |
8914 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv)); | |
8915 GvCV_set(gv,0); /* cv has been hijacked */ | |
8916 call_list(oldscope, PL_beginav); | |
8917 | |
8918 POPSTACK; | |
8919 LEAVE; | |
8920 return !PL_savebegin; | |
8921 } | |
8922 else | |
8923 return FALSE; | |
8924 } else { | |
8925 if (*name == 'E') { | |
8926 if strEQ(name, "END") { | |
8927 DEBUG_x( dump_sub(gv) ); | |
8928 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv)); | |
8929 } else | |
8930 return FALSE; | |
8931 } else if (*name == 'U') { | |
8932 if (strEQ(name, "UNITCHECK")) { | |
8933 /* It's never too late to run a unitcheck block */ | |
8934 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv)); | |
8935 } | |
8936 else | |
8937 return FALSE; | |
8938 } else if (*name == 'C') { | |
8939 if (strEQ(name, "CHECK")) { | |
8940 if (PL_main_start) | |
8941 /* diag_listed_as: Too late to run %s block */ | |
8942 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), | |
8943 "Too late to run CHECK block"); | |
8944 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv)); | |
8945 } | |
8946 else | |
8947 return FALSE; | |
8948 } else if (*name == 'I') { | |
8949 if (strEQ(name, "INIT")) { | |
8950 if (PL_main_start) | |
8951 /* diag_listed_as: Too late to run %s block */ | |
8952 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), | |
8953 "Too late to run INIT block"); | |
8954 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv)); | |
8955 } | |
8956 else | |
8957 return FALSE; | |
8958 } else | |
8959 return FALSE; | |
8960 DEBUG_x( dump_sub(gv) ); | |
8961 (void)CvGV(cv); | |
8962 GvCV_set(gv,0); /* cv has been hijacked */ | |
8963 return FALSE; | |
8964 } | |
8965 } | |
8966 | |
8967 /* | |
8968 =for apidoc newCONSTSUB | |
8969 | |
8970 See L</newCONSTSUB_flags>. | |
8971 | |
8972 =cut | |
8973 */ | |
8974 | |
8975 CV * | |
8976 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv) | |
8977 { | |
8978 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv); | |
8979 } | |
8980 | |
8981 /* | |
8982 =for apidoc newCONSTSUB_flags | |
8983 | |
8984 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is | |
8985 eligible for inlining at compile-time. | |
8986 | |
8987 Currently, the only useful value for C<flags> is SVf_UTF8. | |
8988 | |
8989 The newly created subroutine takes ownership of a reference to the passed in | |
8990 SV. | |
8991 | |
8992 Passing NULL for SV creates a constant sub equivalent to C<sub BAR () {}>, | |
8993 which won't be called if used as a destructor, but will suppress the overhead | |
8994 of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at | |
8995 compile time.) | |
8996 | |
8997 =cut | |
8998 */ | |
8999 | |
9000 CV * | |
9001 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len, | |
9002 U32 flags, SV *sv) | |
9003 { | |
9004 CV* cv; | |
9005 const char *const file = CopFILE(PL_curcop); | |
9006 | |
9007 ENTER; | |
9008 | |
9009 if (IN_PERL_RUNTIME) { | |
9010 /* at runtime, it's not safe to manipulate PL_curcop: it may be | |
9011 * an op shared between threads. Use a non-shared COP for our | |
9012 * dirty work */ | |
9013 SAVEVPTR(PL_curcop); | |
9014 SAVECOMPILEWARNINGS(); | |
9015 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); | |
9016 PL_curcop = &PL_compiling; | |
9017 } | |
9018 SAVECOPLINE(PL_curcop); | |
9019 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE); | |
9020 | |
9021 SAVEHINTS(); | |
9022 PL_hints &= ~HINT_BLOCK_SCOPE; | |
9023 | |
9024 if (stash) { | |
9025 SAVEGENERICSV(PL_curstash); | |
9026 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash); | |
9027 } | |
9028 | |
9029 /* Protect sv against leakage caused by fatal warnings. */ | |
9030 if (sv) SAVEFREESV(sv); | |
9031 | |
9032 /* file becomes the CvFILE. For an XS, it's usually static storage, | |
9033 and so doesn't get free()d. (It's expected to be from the C pre- | |
9034 processor __FILE__ directive). But we need a dynamically allocated one, | |
9035 and we need it to get freed. */ | |
9036 cv = newXS_len_flags(name, len, | |
9037 sv && SvTYPE(sv) == SVt_PVAV | |
9038 ? const_av_xsub | |
9039 : const_sv_xsub, | |
9040 file ? file : "", "", | |
9041 &sv, XS_DYNAMIC_FILENAME | flags); | |
9042 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv); | |
9043 CvCONST_on(cv); | |
9044 | |
9045 LEAVE; | |
9046 | |
9047 return cv; | |
9048 } | |
9049 | |
9050 /* | |
9051 =for apidoc U||newXS | |
9052 | |
9053 Used by C<xsubpp> to hook up XSUBs as Perl subs. I<filename> needs to be | |
9054 static storage, as it is used directly as CvFILE(), without a copy being made. | |
9055 | |
9056 =cut | |
9057 */ | |
9058 | |
9059 CV * | |
9060 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename) | |
9061 { | |
9062 PERL_ARGS_ASSERT_NEWXS; | |
9063 return newXS_len_flags( | |
9064 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0 | |
9065 ); | |
9066 } | |
9067 | |
9068 CV * | |
9069 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr, | |
9070 const char *const filename, const char *const proto, | |
9071 U32 flags) | |
9072 { | |
9073 PERL_ARGS_ASSERT_NEWXS_FLAGS; | |
9074 return newXS_len_flags( | |
9075 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags | |
9076 ); | |
9077 } | |
9078 | |
9079 CV * | |
9080 Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr) | |
9081 { | |
9082 PERL_ARGS_ASSERT_NEWXS_DEFFILE; | |
9083 return newXS_len_flags( | |
9084 name, name ? strlen(name) : 0, subaddr, NULL, NULL, NULL, 0 | |
9085 ); | |
9086 } | |
9087 | |
9088 CV * | |
9089 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len, | |
9090 XSUBADDR_t subaddr, const char *const filename, | |
9091 const char *const proto, SV **const_svp, | |
9092 U32 flags) | |
9093 { | |
9094 CV *cv; | |
9095 bool interleave = FALSE; | |
9096 | |
9097 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS; | |
9098 | |
9099 { | |
9100 GV * const gv = gv_fetchpvn( | |
9101 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__", | |
9102 name ? len : PL_curstash ? sizeof("__ANON__") - 1: | |
9103 sizeof("__ANON__::__ANON__") - 1, | |
9104 GV_ADDMULTI | flags, SVt_PVCV); | |
9105 | |
9106 if ((cv = (name ? GvCV(gv) : NULL))) { | |
9107 if (GvCVGEN(gv)) { | |
9108 /* just a cached method */ | |
9109 SvREFCNT_dec(cv); | |
9110 cv = NULL; | |
9111 } | |
9112 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { | |
9113 /* already defined (or promised) */ | |
9114 /* Redundant check that allows us to avoid creating an SV | |
9115 most of the time: */ | |
9116 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) { | |
9117 report_redefined_cv(newSVpvn_flags( | |
9118 name,len,(flags&SVf_UTF8)|SVs_TEMP | |
9119 ), | |
9120 cv, const_svp); | |
9121 } | |
9122 interleave = TRUE; | |
9123 ENTER; | |
9124 SAVEFREESV(cv); | |
9125 cv = NULL; | |
9126 } | |
9127 } | |
9128 | |
9129 if (cv) /* must reuse cv if autoloaded */ | |
9130 cv_undef(cv); | |
9131 else { | |
9132 cv = MUTABLE_CV(newSV_type(SVt_PVCV)); | |
9133 if (name) { | |
9134 GvCV_set(gv,cv); | |
9135 GvCVGEN(gv) = 0; | |
9136 if (HvENAME_HEK(GvSTASH(gv))) | |
9137 gv_method_changed(gv); /* newXS */ | |
9138 } | |
9139 } | |
9140 | |
9141 CvGV_set(cv, gv); | |
9142 if(filename) { | |
9143 (void)gv_fetchfile(filename); | |
9144 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */ | |
9145 if (flags & XS_DYNAMIC_FILENAME) { | |
9146 CvDYNFILE_on(cv); | |
9147 CvFILE(cv) = savepv(filename); | |
9148 } else { | |
9149 /* NOTE: not copied, as it is expected to be an external constant string */ | |
9150 CvFILE(cv) = (char *)filename; | |
9151 } | |
9152 } else { | |
9153 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename); | |
9154 CvFILE(cv) = (char*)PL_xsubfilename; | |
9155 } | |
9156 CvISXSUB_on(cv); | |
9157 CvXSUB(cv) = subaddr; | |
9158 #ifndef PERL_IMPLICIT_CONTEXT | |
9159 CvHSCXT(cv) = &PL_stack_sp; | |
9160 #else | |
9161 PoisonPADLIST(cv); | |
9162 #endif | |
9163 | |
9164 if (name) | |
9165 process_special_blocks(0, name, gv, cv); | |
9166 else | |
9167 CvANON_on(cv); | |
9168 } /* <- not a conditional branch */ | |
9169 | |
9170 | |
9171 sv_setpv(MUTABLE_SV(cv), proto); | |
9172 if (interleave) LEAVE; | |
9173 return cv; | |
9174 } | |
9175 | |
9176 CV * | |
9177 Perl_newSTUB(pTHX_ GV *gv, bool fake) | |
9178 { | |
9179 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV)); | |
9180 GV *cvgv; | |
9181 PERL_ARGS_ASSERT_NEWSTUB; | |
9182 assert(!GvCVu(gv)); | |
9183 GvCV_set(gv, cv); | |
9184 GvCVGEN(gv) = 0; | |
9185 if (!fake && HvENAME_HEK(GvSTASH(gv))) | |
9186 gv_method_changed(gv); | |
9187 if (SvFAKE(gv)) { | |
9188 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV); | |
9189 SvFAKE_off(cvgv); | |
9190 } | |
9191 else cvgv = gv; | |
9192 CvGV_set(cv, cvgv); | |
9193 CvFILE_set_from_cop(cv, PL_curcop); | |
9194 CvSTASH_set(cv, PL_curstash); | |
9195 GvMULTI_on(gv); | |
9196 return cv; | |
9197 } | |
9198 | |
9199 void | |
9200 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block) | |
9201 { | |
9202 CV *cv; | |
9203 | |
9204 GV *gv; | |
9205 | |
9206 if (PL_parser && PL_parser->error_count) { | |
9207 op_free(block); | |
9208 goto finish; | |
9209 } | |
9210 | |
9211 gv = o | |
9212 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM) | |
9213 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM); | |
9214 | |
9215 GvMULTI_on(gv); | |
9216 if ((cv = GvFORM(gv))) { | |
9217 if (ckWARN(WARN_REDEFINE)) { | |
9218 const line_t oldline = CopLINE(PL_curcop); | |
9219 if (PL_parser && PL_parser->copline != NOLINE) | |
9220 CopLINE_set(PL_curcop, PL_parser->copline); | |
9221 if (o) { | |
9222 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), | |
9223 "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv)); | |
9224 } else { | |
9225 /* diag_listed_as: Format %s redefined */ | |
9226 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), | |
9227 "Format STDOUT redefined"); | |
9228 } | |
9229 CopLINE_set(PL_curcop, oldline); | |
9230 } | |
9231 SvREFCNT_dec(cv); | |
9232 } | |
9233 cv = PL_compcv; | |
9234 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv); | |
9235 CvGV_set(cv, gv); | |
9236 CvFILE_set_from_cop(cv, PL_curcop); | |
9237 | |
9238 | |
9239 pad_tidy(padtidy_FORMAT); | |
9240 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block)); | |
9241 CvROOT(cv)->op_private |= OPpREFCOUNTED; | |
9242 OpREFCNT_set(CvROOT(cv), 1); | |
9243 CvSTART(cv) = LINKLIST(CvROOT(cv)); | |
9244 CvROOT(cv)->op_next = 0; | |
9245 CALL_PEEP(CvSTART(cv)); | |
9246 finalize_optree(CvROOT(cv)); | |
9247 S_prune_chain_head(&CvSTART(cv)); | |
9248 cv_forget_slab(cv); | |
9249 | |
9250 finish: | |
9251 op_free(o); | |
9252 if (PL_parser) | |
9253 PL_parser->copline = NOLINE; | |
9254 LEAVE_SCOPE(floor); | |
9255 PL_compiling.cop_seq = 0; | |
9256 } | |
9257 | |
9258 OP * | |
9259 Perl_newANONLIST(pTHX_ OP *o) | |
9260 { | |
9261 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o); | |
9262 } | |
9263 | |
9264 OP * | |
9265 Perl_newANONHASH(pTHX_ OP *o) | |
9266 { | |
9267 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o); | |
9268 } | |
9269 | |
9270 OP * | |
9271 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block) | |
9272 { | |
9273 return newANONATTRSUB(floor, proto, NULL, block); | |
9274 } | |
9275 | |
9276 OP * | |
9277 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block) | |
9278 { | |
9279 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block)); | |
9280 OP * anoncode = | |
9281 newSVOP(OP_ANONCODE, 0, | |
9282 cv); | |
9283 if (CvANONCONST(cv)) | |
9284 anoncode = newUNOP(OP_ANONCONST, 0, | |
9285 op_convert_list(OP_ENTERSUB, | |
9286 OPf_STACKED|OPf_WANT_SCALAR, | |
9287 anoncode)); | |
9288 return newUNOP(OP_REFGEN, 0, anoncode); | |
9289 } | |
9290 | |
9291 OP * | |
9292 Perl_oopsAV(pTHX_ OP *o) | |
9293 { | |
9294 dVAR; | |
9295 | |
9296 PERL_ARGS_ASSERT_OOPSAV; | |
9297 | |
9298 switch (o->op_type) { | |
9299 case OP_PADSV: | |
9300 case OP_PADHV: | |
9301 OpTYPE_set(o, OP_PADAV); | |
9302 return ref(o, OP_RV2AV); | |
9303 | |
9304 case OP_RV2SV: | |
9305 case OP_RV2HV: | |
9306 OpTYPE_set(o, OP_RV2AV); | |
9307 ref(o, OP_RV2AV); | |
9308 break; | |
9309 | |
9310 default: | |
9311 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV"); | |
9312 break; | |
9313 } | |
9314 return o; | |
9315 } | |
9316 | |
9317 OP * | |
9318 Perl_oopsHV(pTHX_ OP *o) | |
9319 { | |
9320 dVAR; | |
9321 | |
9322 PERL_ARGS_ASSERT_OOPSHV; | |
9323 | |
9324 switch (o->op_type) { | |
9325 case OP_PADSV: | |
9326 case OP_PADAV: | |
9327 OpTYPE_set(o, OP_PADHV); | |
9328 return ref(o, OP_RV2HV); | |
9329 | |
9330 case OP_RV2SV: | |
9331 case OP_RV2AV: | |
9332 OpTYPE_set(o, OP_RV2HV); | |
9333 ref(o, OP_RV2HV); | |
9334 break; | |
9335 | |
9336 default: | |
9337 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV"); | |
9338 break; | |
9339 } | |
9340 return o; | |
9341 } | |
9342 | |
9343 OP * | |
9344 Perl_newAVREF(pTHX_ OP *o) | |
9345 { | |
9346 dVAR; | |
9347 | |
9348 PERL_ARGS_ASSERT_NEWAVREF; | |
9349 | |
9350 if (o->op_type == OP_PADANY) { | |
9351 OpTYPE_set(o, OP_PADAV); | |
9352 return o; | |
9353 } | |
9354 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) { | |
9355 Perl_croak(aTHX_ "Can't use an array as a reference"); | |
9356 } | |
9357 return newUNOP(OP_RV2AV, 0, scalar(o)); | |
9358 } | |
9359 | |
9360 OP * | |
9361 Perl_newGVREF(pTHX_ I32 type, OP *o) | |
9362 { | |
9363 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT) | |
9364 return newUNOP(OP_NULL, 0, o); | |
9365 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type); | |
9366 } | |
9367 | |
9368 OP * | |
9369 Perl_newHVREF(pTHX_ OP *o) | |
9370 { | |
9371 dVAR; | |
9372 | |
9373 PERL_ARGS_ASSERT_NEWHVREF; | |
9374 | |
9375 if (o->op_type == OP_PADANY) { | |
9376 OpTYPE_set(o, OP_PADHV); | |
9377 return o; | |
9378 } | |
9379 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) { | |
9380 Perl_croak(aTHX_ "Can't use a hash as a reference"); | |
9381 } | |
9382 return newUNOP(OP_RV2HV, 0, scalar(o)); | |
9383 } | |
9384 | |
9385 OP * | |
9386 Perl_newCVREF(pTHX_ I32 flags, OP *o) | |
9387 { | |
9388 if (o->op_type == OP_PADANY) { | |
9389 dVAR; | |
9390 OpTYPE_set(o, OP_PADCV); | |
9391 } | |
9392 return newUNOP(OP_RV2CV, flags, scalar(o)); | |
9393 } | |
9394 | |
9395 OP * | |
9396 Perl_newSVREF(pTHX_ OP *o) | |
9397 { | |
9398 dVAR; | |
9399 | |
9400 PERL_ARGS_ASSERT_NEWSVREF; | |
9401 | |
9402 if (o->op_type == OP_PADANY) { | |
9403 OpTYPE_set(o, OP_PADSV); | |
9404 scalar(o); | |
9405 return o; | |
9406 } | |
9407 return newUNOP(OP_RV2SV, 0, scalar(o)); | |
9408 } | |
9409 | |
9410 /* Check routines. See the comments at the top of this file for details | |
9411 * on when these are called */ | |
9412 | |
9413 OP * | |
9414 Perl_ck_anoncode(pTHX_ OP *o) | |
9415 { | |
9416 PERL_ARGS_ASSERT_CK_ANONCODE; | |
9417 | |
9418 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type); | |
9419 cSVOPo->op_sv = NULL; | |
9420 return o; | |
9421 } | |
9422 | |
9423 static void | |
9424 S_io_hints(pTHX_ OP *o) | |
9425 { | |
9426 #if O_BINARY != 0 || O_TEXT != 0 | |
9427 HV * const table = | |
9428 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;; | |
9429 if (table) { | |
9430 SV **svp = hv_fetchs(table, "open_IN", FALSE); | |
9431 if (svp && *svp) { | |
9432 STRLEN len = 0; | |
9433 const char *d = SvPV_const(*svp, len); | |
9434 const I32 mode = mode_from_discipline(d, len); | |
9435 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */ | |
9436 # if O_BINARY != 0 | |
9437 if (mode & O_BINARY) | |
9438 o->op_private |= OPpOPEN_IN_RAW; | |
9439 # endif | |
9440 # if O_TEXT != 0 | |
9441 if (mode & O_TEXT) | |
9442 o->op_private |= OPpOPEN_IN_CRLF; | |
9443 # endif | |
9444 } | |
9445 | |
9446 svp = hv_fetchs(table, "open_OUT", FALSE); | |
9447 if (svp && *svp) { | |
9448 STRLEN len = 0; | |
9449 const char *d = SvPV_const(*svp, len); | |
9450 const I32 mode = mode_from_discipline(d, len); | |
9451 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */ | |
9452 # if O_BINARY != 0 | |
9453 if (mode & O_BINARY) | |
9454 o->op_private |= OPpOPEN_OUT_RAW; | |
9455 # endif | |
9456 # if O_TEXT != 0 | |
9457 if (mode & O_TEXT) | |
9458 o->op_private |= OPpOPEN_OUT_CRLF; | |
9459 # endif | |
9460 } | |
9461 } | |
9462 #else | |
9463 PERL_UNUSED_CONTEXT; | |
9464 PERL_UNUSED_ARG(o); | |
9465 #endif | |
9466 } | |
9467 | |
9468 OP * | |
9469 Perl_ck_backtick(pTHX_ OP *o) | |
9470 { | |
9471 GV *gv; | |
9472 OP *newop = NULL; | |
9473 OP *sibl; | |
9474 PERL_ARGS_ASSERT_CK_BACKTICK; | |
9475 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */ | |
9476 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first)) | |
9477 && (gv = gv_override("readpipe",8))) | |
9478 { | |
9479 /* detach rest of siblings from o and its first child */ | |
9480 op_sibling_splice(o, cUNOPo->op_first, -1, NULL); | |
9481 newop = S_new_entersubop(aTHX_ gv, sibl); | |
9482 } | |
9483 else if (!(o->op_flags & OPf_KIDS)) | |
9484 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP()); | |
9485 if (newop) { | |
9486 op_free(o); | |
9487 return newop; | |
9488 } | |
9489 S_io_hints(aTHX_ o); | |
9490 return o; | |
9491 } | |
9492 | |
9493 OP * | |
9494 Perl_ck_bitop(pTHX_ OP *o) | |
9495 { | |
9496 PERL_ARGS_ASSERT_CK_BITOP; | |
9497 | |
9498 o->op_private = (U8)(PL_hints & HINT_INTEGER); | |
9499 | |
9500 if (o->op_type == OP_NBIT_OR || o->op_type == OP_SBIT_OR | |
9501 || o->op_type == OP_NBIT_XOR || o->op_type == OP_SBIT_XOR | |
9502 || o->op_type == OP_NBIT_AND || o->op_type == OP_SBIT_AND | |
9503 || o->op_type == OP_NCOMPLEMENT || o->op_type == OP_SCOMPLEMENT) | |
9504 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__BITWISE), | |
9505 "The bitwise feature is experimental"); | |
9506 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */ | |
9507 && OP_IS_INFIX_BIT(o->op_type)) | |
9508 { | |
9509 const OP * const left = cBINOPo->op_first; | |
9510 const OP * const right = OpSIBLING(left); | |
9511 if ((OP_IS_NUMCOMPARE(left->op_type) && | |
9512 (left->op_flags & OPf_PARENS) == 0) || | |
9513 (OP_IS_NUMCOMPARE(right->op_type) && | |
9514 (right->op_flags & OPf_PARENS) == 0)) | |
9515 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE), | |
9516 "Possible precedence problem on bitwise %s operator", | |
9517 o->op_type == OP_BIT_OR | |
9518 ||o->op_type == OP_NBIT_OR ? "|" | |
9519 : o->op_type == OP_BIT_AND | |
9520 ||o->op_type == OP_NBIT_AND ? "&" | |
9521 : o->op_type == OP_BIT_XOR | |
9522 ||o->op_type == OP_NBIT_XOR ? "^" | |
9523 : o->op_type == OP_SBIT_OR ? "|." | |
9524 : o->op_type == OP_SBIT_AND ? "&." : "^." | |
9525 ); | |
9526 } | |
9527 return o; | |
9528 } | |
9529 | |
9530 PERL_STATIC_INLINE bool | |
9531 is_dollar_bracket(pTHX_ const OP * const o) | |
9532 { | |
9533 const OP *kid; | |
9534 PERL_UNUSED_CONTEXT; | |
9535 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS | |
9536 && (kid = cUNOPx(o)->op_first) | |
9537 && kid->op_type == OP_GV | |
9538 && strEQ(GvNAME(cGVOPx_gv(kid)), "["); | |
9539 } | |
9540 | |
9541 OP * | |
9542 Perl_ck_cmp(pTHX_ OP *o) | |
9543 { | |
9544 PERL_ARGS_ASSERT_CK_CMP; | |
9545 if (ckWARN(WARN_SYNTAX)) { | |
9546 const OP *kid = cUNOPo->op_first; | |
9547 if (kid && | |
9548 ( | |
9549 ( is_dollar_bracket(aTHX_ kid) | |
9550 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST | |
9551 ) | |
9552 || ( kid->op_type == OP_CONST | |
9553 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid) | |
9554 ) | |
9555 ) | |
9556 ) | |
9557 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
9558 "$[ used in %s (did you mean $] ?)", OP_DESC(o)); | |
9559 } | |
9560 return o; | |
9561 } | |
9562 | |
9563 OP * | |
9564 Perl_ck_concat(pTHX_ OP *o) | |
9565 { | |
9566 const OP * const kid = cUNOPo->op_first; | |
9567 | |
9568 PERL_ARGS_ASSERT_CK_CONCAT; | |
9569 PERL_UNUSED_CONTEXT; | |
9570 | |
9571 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) && | |
9572 !(kUNOP->op_first->op_flags & OPf_MOD)) | |
9573 o->op_flags |= OPf_STACKED; | |
9574 return o; | |
9575 } | |
9576 | |
9577 OP * | |
9578 Perl_ck_spair(pTHX_ OP *o) | |
9579 { | |
9580 dVAR; | |
9581 | |
9582 PERL_ARGS_ASSERT_CK_SPAIR; | |
9583 | |
9584 if (o->op_flags & OPf_KIDS) { | |
9585 OP* newop; | |
9586 OP* kid; | |
9587 OP* kidkid; | |
9588 const OPCODE type = o->op_type; | |
9589 o = modkids(ck_fun(o), type); | |
9590 kid = cUNOPo->op_first; | |
9591 kidkid = kUNOP->op_first; | |
9592 newop = OpSIBLING(kidkid); | |
9593 if (newop) { | |
9594 const OPCODE type = newop->op_type; | |
9595 if (OpHAS_SIBLING(newop)) | |
9596 return o; | |
9597 if (o->op_type == OP_REFGEN | |
9598 && ( type == OP_RV2CV | |
9599 || ( !(newop->op_flags & OPf_PARENS) | |
9600 && ( type == OP_RV2AV || type == OP_PADAV | |
9601 || type == OP_RV2HV || type == OP_PADHV)))) | |
9602 NOOP; /* OK (allow srefgen for \@a and \%h) */ | |
9603 else if (OP_GIMME(newop,0) != G_SCALAR) | |
9604 return o; | |
9605 } | |
9606 /* excise first sibling */ | |
9607 op_sibling_splice(kid, NULL, 1, NULL); | |
9608 op_free(kidkid); | |
9609 } | |
9610 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP, | |
9611 * and OP_CHOMP into OP_SCHOMP */ | |
9612 o->op_ppaddr = PL_ppaddr[++o->op_type]; | |
9613 return ck_fun(o); | |
9614 } | |
9615 | |
9616 OP * | |
9617 Perl_ck_delete(pTHX_ OP *o) | |
9618 { | |
9619 PERL_ARGS_ASSERT_CK_DELETE; | |
9620 | |
9621 o = ck_fun(o); | |
9622 o->op_private = 0; | |
9623 if (o->op_flags & OPf_KIDS) { | |
9624 OP * const kid = cUNOPo->op_first; | |
9625 switch (kid->op_type) { | |
9626 case OP_ASLICE: | |
9627 o->op_flags |= OPf_SPECIAL; | |
9628 /* FALLTHROUGH */ | |
9629 case OP_HSLICE: | |
9630 o->op_private |= OPpSLICE; | |
9631 break; | |
9632 case OP_AELEM: | |
9633 o->op_flags |= OPf_SPECIAL; | |
9634 /* FALLTHROUGH */ | |
9635 case OP_HELEM: | |
9636 break; | |
9637 case OP_KVASLICE: | |
9638 Perl_croak(aTHX_ "delete argument is index/value array slice," | |
9639 " use array slice"); | |
9640 case OP_KVHSLICE: | |
9641 Perl_croak(aTHX_ "delete argument is key/value hash slice, use" | |
9642 " hash slice"); | |
9643 default: | |
9644 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY " | |
9645 "element or slice"); | |
9646 } | |
9647 if (kid->op_private & OPpLVAL_INTRO) | |
9648 o->op_private |= OPpLVAL_INTRO; | |
9649 op_null(kid); | |
9650 } | |
9651 return o; | |
9652 } | |
9653 | |
9654 OP * | |
9655 Perl_ck_eof(pTHX_ OP *o) | |
9656 { | |
9657 PERL_ARGS_ASSERT_CK_EOF; | |
9658 | |
9659 if (o->op_flags & OPf_KIDS) { | |
9660 OP *kid; | |
9661 if (cLISTOPo->op_first->op_type == OP_STUB) { | |
9662 OP * const newop | |
9663 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv)); | |
9664 op_free(o); | |
9665 o = newop; | |
9666 } | |
9667 o = ck_fun(o); | |
9668 kid = cLISTOPo->op_first; | |
9669 if (kid->op_type == OP_RV2GV) | |
9670 kid->op_private |= OPpALLOW_FAKE; | |
9671 } | |
9672 return o; | |
9673 } | |
9674 | |
9675 OP * | |
9676 Perl_ck_eval(pTHX_ OP *o) | |
9677 { | |
9678 dVAR; | |
9679 | |
9680 PERL_ARGS_ASSERT_CK_EVAL; | |
9681 | |
9682 PL_hints |= HINT_BLOCK_SCOPE; | |
9683 if (o->op_flags & OPf_KIDS) { | |
9684 SVOP * const kid = (SVOP*)cUNOPo->op_first; | |
9685 assert(kid); | |
9686 | |
9687 if (o->op_type == OP_ENTERTRY) { | |
9688 LOGOP *enter; | |
9689 | |
9690 /* cut whole sibling chain free from o */ | |
9691 op_sibling_splice(o, NULL, -1, NULL); | |
9692 op_free(o); | |
9693 | |
9694 enter = S_alloc_LOGOP(aTHX_ OP_ENTERTRY, NULL, NULL); | |
9695 | |
9696 /* establish postfix order */ | |
9697 enter->op_next = (OP*)enter; | |
9698 | |
9699 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid); | |
9700 OpTYPE_set(o, OP_LEAVETRY); | |
9701 enter->op_other = o; | |
9702 return o; | |
9703 } | |
9704 else { | |
9705 scalar((OP*)kid); | |
9706 S_set_haseval(aTHX); | |
9707 } | |
9708 } | |
9709 else { | |
9710 const U8 priv = o->op_private; | |
9711 op_free(o); | |
9712 /* the newUNOP will recursively call ck_eval(), which will handle | |
9713 * all the stuff at the end of this function, like adding | |
9714 * OP_HINTSEVAL | |
9715 */ | |
9716 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP()); | |
9717 } | |
9718 o->op_targ = (PADOFFSET)PL_hints; | |
9719 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8; | |
9720 if ((PL_hints & HINT_LOCALIZE_HH) != 0 | |
9721 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) { | |
9722 /* Store a copy of %^H that pp_entereval can pick up. */ | |
9723 OP *hhop = newSVOP(OP_HINTSEVAL, 0, | |
9724 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv)))); | |
9725 /* append hhop to only child */ | |
9726 op_sibling_splice(o, cUNOPo->op_first, 0, hhop); | |
9727 | |
9728 o->op_private |= OPpEVAL_HAS_HH; | |
9729 } | |
9730 if (!(o->op_private & OPpEVAL_BYTES) | |
9731 && FEATURE_UNIEVAL_IS_ENABLED) | |
9732 o->op_private |= OPpEVAL_UNICODE; | |
9733 return o; | |
9734 } | |
9735 | |
9736 OP * | |
9737 Perl_ck_exec(pTHX_ OP *o) | |
9738 { | |
9739 PERL_ARGS_ASSERT_CK_EXEC; | |
9740 | |
9741 if (o->op_flags & OPf_STACKED) { | |
9742 OP *kid; | |
9743 o = ck_fun(o); | |
9744 kid = OpSIBLING(cUNOPo->op_first); | |
9745 if (kid->op_type == OP_RV2GV) | |
9746 op_null(kid); | |
9747 } | |
9748 else | |
9749 o = listkids(o); | |
9750 return o; | |
9751 } | |
9752 | |
9753 OP * | |
9754 Perl_ck_exists(pTHX_ OP *o) | |
9755 { | |
9756 PERL_ARGS_ASSERT_CK_EXISTS; | |
9757 | |
9758 o = ck_fun(o); | |
9759 if (o->op_flags & OPf_KIDS) { | |
9760 OP * const kid = cUNOPo->op_first; | |
9761 if (kid->op_type == OP_ENTERSUB) { | |
9762 (void) ref(kid, o->op_type); | |
9763 if (kid->op_type != OP_RV2CV | |
9764 && !(PL_parser && PL_parser->error_count)) | |
9765 Perl_croak(aTHX_ | |
9766 "exists argument is not a subroutine name"); | |
9767 o->op_private |= OPpEXISTS_SUB; | |
9768 } | |
9769 else if (kid->op_type == OP_AELEM) | |
9770 o->op_flags |= OPf_SPECIAL; | |
9771 else if (kid->op_type != OP_HELEM) | |
9772 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY " | |
9773 "element or a subroutine"); | |
9774 op_null(kid); | |
9775 } | |
9776 return o; | |
9777 } | |
9778 | |
9779 OP * | |
9780 Perl_ck_rvconst(pTHX_ OP *o) | |
9781 { | |
9782 dVAR; | |
9783 SVOP * const kid = (SVOP*)cUNOPo->op_first; | |
9784 | |
9785 PERL_ARGS_ASSERT_CK_RVCONST; | |
9786 | |
9787 o->op_private |= (PL_hints & HINT_STRICT_REFS); | |
9788 | |
9789 if (kid->op_type == OP_CONST) { | |
9790 int iscv; | |
9791 GV *gv; | |
9792 SV * const kidsv = kid->op_sv; | |
9793 | |
9794 /* Is it a constant from cv_const_sv()? */ | |
9795 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) { | |
9796 return o; | |
9797 } | |
9798 if (SvTYPE(kidsv) == SVt_PVAV) return o; | |
9799 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) { | |
9800 const char *badthing; | |
9801 switch (o->op_type) { | |
9802 case OP_RV2SV: | |
9803 badthing = "a SCALAR"; | |
9804 break; | |
9805 case OP_RV2AV: | |
9806 badthing = "an ARRAY"; | |
9807 break; | |
9808 case OP_RV2HV: | |
9809 badthing = "a HASH"; | |
9810 break; | |
9811 default: | |
9812 badthing = NULL; | |
9813 break; | |
9814 } | |
9815 if (badthing) | |
9816 Perl_croak(aTHX_ | |
9817 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use", | |
9818 SVfARG(kidsv), badthing); | |
9819 } | |
9820 /* | |
9821 * This is a little tricky. We only want to add the symbol if we | |
9822 * didn't add it in the lexer. Otherwise we get duplicate strict | |
9823 * warnings. But if we didn't add it in the lexer, we must at | |
9824 * least pretend like we wanted to add it even if it existed before, | |
9825 * or we get possible typo warnings. OPpCONST_ENTERED says | |
9826 * whether the lexer already added THIS instance of this symbol. | |
9827 */ | |
9828 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0; | |
9829 gv = gv_fetchsv(kidsv, | |
9830 o->op_type == OP_RV2CV | |
9831 && o->op_private & OPpMAY_RETURN_CONSTANT | |
9832 ? GV_NOEXPAND | |
9833 : iscv | !(kid->op_private & OPpCONST_ENTERED), | |
9834 iscv | |
9835 ? SVt_PVCV | |
9836 : o->op_type == OP_RV2SV | |
9837 ? SVt_PV | |
9838 : o->op_type == OP_RV2AV | |
9839 ? SVt_PVAV | |
9840 : o->op_type == OP_RV2HV | |
9841 ? SVt_PVHV | |
9842 : SVt_PVGV); | |
9843 if (gv) { | |
9844 if (!isGV(gv)) { | |
9845 assert(iscv); | |
9846 assert(SvROK(gv)); | |
9847 if (!(o->op_private & OPpMAY_RETURN_CONSTANT) | |
9848 && SvTYPE(SvRV(gv)) != SVt_PVCV) | |
9849 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV); | |
9850 } | |
9851 OpTYPE_set(kid, OP_GV); | |
9852 SvREFCNT_dec(kid->op_sv); | |
9853 #ifdef USE_ITHREADS | |
9854 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */ | |
9855 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP)); | |
9856 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY); | |
9857 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix)); | |
9858 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv))); | |
9859 #else | |
9860 kid->op_sv = SvREFCNT_inc_simple_NN(gv); | |
9861 #endif | |
9862 kid->op_private = 0; | |
9863 /* FAKE globs in the symbol table cause weird bugs (#77810) */ | |
9864 SvFAKE_off(gv); | |
9865 } | |
9866 } | |
9867 return o; | |
9868 } | |
9869 | |
9870 OP * | |
9871 Perl_ck_ftst(pTHX_ OP *o) | |
9872 { | |
9873 dVAR; | |
9874 const I32 type = o->op_type; | |
9875 | |
9876 PERL_ARGS_ASSERT_CK_FTST; | |
9877 | |
9878 if (o->op_flags & OPf_REF) { | |
9879 NOOP; | |
9880 } | |
9881 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) { | |
9882 SVOP * const kid = (SVOP*)cUNOPo->op_first; | |
9883 const OPCODE kidtype = kid->op_type; | |
9884 | |
9885 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE) | |
9886 && !kid->op_folded) { | |
9887 OP * const newop = newGVOP(type, OPf_REF, | |
9888 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO)); | |
9889 op_free(o); | |
9890 return newop; | |
9891 } | |
9892 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type)) | |
9893 o->op_private |= OPpFT_ACCESS; | |
9894 if (type != OP_STAT && type != OP_LSTAT | |
9895 && PL_check[kidtype] == Perl_ck_ftst | |
9896 && kidtype != OP_STAT && kidtype != OP_LSTAT | |
9897 ) { | |
9898 o->op_private |= OPpFT_STACKED; | |
9899 kid->op_private |= OPpFT_STACKING; | |
9900 if (kidtype == OP_FTTTY && ( | |
9901 !(kid->op_private & OPpFT_STACKED) | |
9902 || kid->op_private & OPpFT_AFTER_t | |
9903 )) | |
9904 o->op_private |= OPpFT_AFTER_t; | |
9905 } | |
9906 } | |
9907 else { | |
9908 op_free(o); | |
9909 if (type == OP_FTTTY) | |
9910 o = newGVOP(type, OPf_REF, PL_stdingv); | |
9911 else | |
9912 o = newUNOP(type, 0, newDEFSVOP()); | |
9913 } | |
9914 return o; | |
9915 } | |
9916 | |
9917 OP * | |
9918 Perl_ck_fun(pTHX_ OP *o) | |
9919 { | |
9920 const int type = o->op_type; | |
9921 I32 oa = PL_opargs[type] >> OASHIFT; | |
9922 | |
9923 PERL_ARGS_ASSERT_CK_FUN; | |
9924 | |
9925 if (o->op_flags & OPf_STACKED) { | |
9926 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL)) | |
9927 oa &= ~OA_OPTIONAL; | |
9928 else | |
9929 return no_fh_allowed(o); | |
9930 } | |
9931 | |
9932 if (o->op_flags & OPf_KIDS) { | |
9933 OP *prev_kid = NULL; | |
9934 OP *kid = cLISTOPo->op_first; | |
9935 I32 numargs = 0; | |
9936 bool seen_optional = FALSE; | |
9937 | |
9938 if (kid->op_type == OP_PUSHMARK || | |
9939 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)) | |
9940 { | |
9941 prev_kid = kid; | |
9942 kid = OpSIBLING(kid); | |
9943 } | |
9944 if (kid && kid->op_type == OP_COREARGS) { | |
9945 bool optional = FALSE; | |
9946 while (oa) { | |
9947 numargs++; | |
9948 if (oa & OA_OPTIONAL) optional = TRUE; | |
9949 oa = oa >> 4; | |
9950 } | |
9951 if (optional) o->op_private |= numargs; | |
9952 return o; | |
9953 } | |
9954 | |
9955 while (oa) { | |
9956 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) { | |
9957 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) { | |
9958 kid = newDEFSVOP(); | |
9959 /* append kid to chain */ | |
9960 op_sibling_splice(o, prev_kid, 0, kid); | |
9961 } | |
9962 seen_optional = TRUE; | |
9963 } | |
9964 if (!kid) break; | |
9965 | |
9966 numargs++; | |
9967 switch (oa & 7) { | |
9968 case OA_SCALAR: | |
9969 /* list seen where single (scalar) arg expected? */ | |
9970 if (numargs == 1 && !(oa >> 4) | |
9971 && kid->op_type == OP_LIST && type != OP_SCALAR) | |
9972 { | |
9973 return too_many_arguments_pv(o,PL_op_desc[type], 0); | |
9974 } | |
9975 if (type != OP_DELETE) scalar(kid); | |
9976 break; | |
9977 case OA_LIST: | |
9978 if (oa < 16) { | |
9979 kid = 0; | |
9980 continue; | |
9981 } | |
9982 else | |
9983 list(kid); | |
9984 break; | |
9985 case OA_AVREF: | |
9986 if ((type == OP_PUSH || type == OP_UNSHIFT) | |
9987 && !OpHAS_SIBLING(kid)) | |
9988 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), | |
9989 "Useless use of %s with no values", | |
9990 PL_op_desc[type]); | |
9991 | |
9992 if (kid->op_type == OP_CONST | |
9993 && ( !SvROK(cSVOPx_sv(kid)) | |
9994 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV ) | |
9995 ) | |
9996 bad_type_pv(numargs, "array", o, kid); | |
9997 /* Defer checks to run-time if we have a scalar arg */ | |
9998 if (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV) | |
9999 op_lvalue(kid, type); | |
10000 else { | |
10001 scalar(kid); | |
10002 /* diag_listed_as: push on reference is experimental */ | |
10003 Perl_ck_warner_d(aTHX_ | |
10004 packWARN(WARN_EXPERIMENTAL__AUTODEREF), | |
10005 "%s on reference is experimental", | |
10006 PL_op_desc[type]); | |
10007 } | |
10008 break; | |
10009 case OA_HVREF: | |
10010 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV) | |
10011 bad_type_pv(numargs, "hash", o, kid); | |
10012 op_lvalue(kid, type); | |
10013 break; | |
10014 case OA_CVREF: | |
10015 { | |
10016 /* replace kid with newop in chain */ | |
10017 OP * const newop = | |
10018 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0); | |
10019 newop->op_next = newop; | |
10020 kid = newop; | |
10021 } | |
10022 break; | |
10023 case OA_FILEREF: | |
10024 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) { | |
10025 if (kid->op_type == OP_CONST && | |
10026 (kid->op_private & OPpCONST_BARE)) | |
10027 { | |
10028 OP * const newop = newGVOP(OP_GV, 0, | |
10029 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO)); | |
10030 /* replace kid with newop in chain */ | |
10031 op_sibling_splice(o, prev_kid, 1, newop); | |
10032 op_free(kid); | |
10033 kid = newop; | |
10034 } | |
10035 else if (kid->op_type == OP_READLINE) { | |
10036 /* neophyte patrol: open(<FH>), close(<FH>) etc. */ | |
10037 bad_type_pv(numargs, "HANDLE", o, kid); | |
10038 } | |
10039 else { | |
10040 I32 flags = OPf_SPECIAL; | |
10041 I32 priv = 0; | |
10042 PADOFFSET targ = 0; | |
10043 | |
10044 /* is this op a FH constructor? */ | |
10045 if (is_handle_constructor(o,numargs)) { | |
10046 const char *name = NULL; | |
10047 STRLEN len = 0; | |
10048 U32 name_utf8 = 0; | |
10049 bool want_dollar = TRUE; | |
10050 | |
10051 flags = 0; | |
10052 /* Set a flag to tell rv2gv to vivify | |
10053 * need to "prove" flag does not mean something | |
10054 * else already - NI-S 1999/05/07 | |
10055 */ | |
10056 priv = OPpDEREF; | |
10057 if (kid->op_type == OP_PADSV) { | |
10058 PADNAME * const pn | |
10059 = PAD_COMPNAME_SV(kid->op_targ); | |
10060 name = PadnamePV (pn); | |
10061 len = PadnameLEN(pn); | |
10062 name_utf8 = PadnameUTF8(pn); | |
10063 } | |
10064 else if (kid->op_type == OP_RV2SV | |
10065 && kUNOP->op_first->op_type == OP_GV) | |
10066 { | |
10067 GV * const gv = cGVOPx_gv(kUNOP->op_first); | |
10068 name = GvNAME(gv); | |
10069 len = GvNAMELEN(gv); | |
10070 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0; | |
10071 } | |
10072 else if (kid->op_type == OP_AELEM | |
10073 || kid->op_type == OP_HELEM) | |
10074 { | |
10075 OP *firstop; | |
10076 OP *op = ((BINOP*)kid)->op_first; | |
10077 name = NULL; | |
10078 if (op) { | |
10079 SV *tmpstr = NULL; | |
10080 const char * const a = | |
10081 kid->op_type == OP_AELEM ? | |
10082 "[]" : "{}"; | |
10083 if (((op->op_type == OP_RV2AV) || | |
10084 (op->op_type == OP_RV2HV)) && | |
10085 (firstop = ((UNOP*)op)->op_first) && | |
10086 (firstop->op_type == OP_GV)) { | |
10087 /* packagevar $a[] or $h{} */ | |
10088 GV * const gv = cGVOPx_gv(firstop); | |
10089 if (gv) | |
10090 tmpstr = | |
10091 Perl_newSVpvf(aTHX_ | |
10092 "%s%c...%c", | |
10093 GvNAME(gv), | |
10094 a[0], a[1]); | |
10095 } | |
10096 else if (op->op_type == OP_PADAV | |
10097 || op->op_type == OP_PADHV) { | |
10098 /* lexicalvar $a[] or $h{} */ | |
10099 const char * const padname = | |
10100 PAD_COMPNAME_PV(op->op_targ); | |
10101 if (padname) | |
10102 tmpstr = | |
10103 Perl_newSVpvf(aTHX_ | |
10104 "%s%c...%c", | |
10105 padname + 1, | |
10106 a[0], a[1]); | |
10107 } | |
10108 if (tmpstr) { | |
10109 name = SvPV_const(tmpstr, len); | |
10110 name_utf8 = SvUTF8(tmpstr); | |
10111 sv_2mortal(tmpstr); | |
10112 } | |
10113 } | |
10114 if (!name) { | |
10115 name = "__ANONIO__"; | |
10116 len = 10; | |
10117 want_dollar = FALSE; | |
10118 } | |
10119 op_lvalue(kid, type); | |
10120 } | |
10121 if (name) { | |
10122 SV *namesv; | |
10123 targ = pad_alloc(OP_RV2GV, SVf_READONLY); | |
10124 namesv = PAD_SVl(targ); | |
10125 if (want_dollar && *name != '$') | |
10126 sv_setpvs(namesv, "$"); | |
10127 else | |
10128 sv_setpvs(namesv, ""); | |
10129 sv_catpvn(namesv, name, len); | |
10130 if ( name_utf8 ) SvUTF8_on(namesv); | |
10131 } | |
10132 } | |
10133 scalar(kid); | |
10134 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid, | |
10135 OP_RV2GV, flags); | |
10136 kid->op_targ = targ; | |
10137 kid->op_private |= priv; | |
10138 } | |
10139 } | |
10140 scalar(kid); | |
10141 break; | |
10142 case OA_SCALARREF: | |
10143 if ((type == OP_UNDEF || type == OP_POS) | |
10144 && numargs == 1 && !(oa >> 4) | |
10145 && kid->op_type == OP_LIST) | |
10146 return too_many_arguments_pv(o,PL_op_desc[type], 0); | |
10147 op_lvalue(scalar(kid), type); | |
10148 break; | |
10149 } | |
10150 oa >>= 4; | |
10151 prev_kid = kid; | |
10152 kid = OpSIBLING(kid); | |
10153 } | |
10154 /* FIXME - should the numargs or-ing move after the too many | |
10155 * arguments check? */ | |
10156 o->op_private |= numargs; | |
10157 if (kid) | |
10158 return too_many_arguments_pv(o,OP_DESC(o), 0); | |
10159 listkids(o); | |
10160 } | |
10161 else if (PL_opargs[type] & OA_DEFGV) { | |
10162 /* Ordering of these two is important to keep f_map.t passing. */ | |
10163 op_free(o); | |
10164 return newUNOP(type, 0, newDEFSVOP()); | |
10165 } | |
10166 | |
10167 if (oa) { | |
10168 while (oa & OA_OPTIONAL) | |
10169 oa >>= 4; | |
10170 if (oa && oa != OA_LIST) | |
10171 return too_few_arguments_pv(o,OP_DESC(o), 0); | |
10172 } | |
10173 return o; | |
10174 } | |
10175 | |
10176 OP * | |
10177 Perl_ck_glob(pTHX_ OP *o) | |
10178 { | |
10179 GV *gv; | |
10180 | |
10181 PERL_ARGS_ASSERT_CK_GLOB; | |
10182 | |
10183 o = ck_fun(o); | |
10184 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first)) | |
10185 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */ | |
10186 | |
10187 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4))) | |
10188 { | |
10189 /* convert | |
10190 * glob | |
10191 * \ null - const(wildcard) | |
10192 * into | |
10193 * null | |
10194 * \ enter | |
10195 * \ list | |
10196 * \ mark - glob - rv2cv | |
10197 * | \ gv(CORE::GLOBAL::glob) | |
10198 * | | |
10199 * \ null - const(wildcard) | |
10200 */ | |
10201 o->op_flags |= OPf_SPECIAL; | |
10202 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP); | |
10203 o = S_new_entersubop(aTHX_ gv, o); | |
10204 o = newUNOP(OP_NULL, 0, o); | |
10205 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */ | |
10206 return o; | |
10207 } | |
10208 else o->op_flags &= ~OPf_SPECIAL; | |
10209 #if !defined(PERL_EXTERNAL_GLOB) | |
10210 if (!PL_globhook) { | |
10211 ENTER; | |
10212 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, | |
10213 newSVpvs("File::Glob"), NULL, NULL, NULL); | |
10214 LEAVE; | |
10215 } | |
10216 #endif /* !PERL_EXTERNAL_GLOB */ | |
10217 gv = (GV *)newSV(0); | |
10218 gv_init(gv, 0, "", 0, 0); | |
10219 gv_IOadd(gv); | |
10220 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv)); | |
10221 SvREFCNT_dec_NN(gv); /* newGVOP increased it */ | |
10222 scalarkids(o); | |
10223 return o; | |
10224 } | |
10225 | |
10226 OP * | |
10227 Perl_ck_grep(pTHX_ OP *o) | |
10228 { | |
10229 LOGOP *gwop; | |
10230 OP *kid; | |
10231 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE; | |
10232 PADOFFSET offset; | |
10233 | |
10234 PERL_ARGS_ASSERT_CK_GREP; | |
10235 | |
10236 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */ | |
10237 | |
10238 if (o->op_flags & OPf_STACKED) { | |
10239 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first; | |
10240 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE) | |
10241 return no_fh_allowed(o); | |
10242 o->op_flags &= ~OPf_STACKED; | |
10243 } | |
10244 kid = OpSIBLING(cLISTOPo->op_first); | |
10245 if (type == OP_MAPWHILE) | |
10246 list(kid); | |
10247 else | |
10248 scalar(kid); | |
10249 o = ck_fun(o); | |
10250 if (PL_parser && PL_parser->error_count) | |
10251 return o; | |
10252 kid = OpSIBLING(cLISTOPo->op_first); | |
10253 if (kid->op_type != OP_NULL) | |
10254 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type); | |
10255 kid = kUNOP->op_first; | |
10256 | |
10257 gwop = S_alloc_LOGOP(aTHX_ type, o, LINKLIST(kid)); | |
10258 kid->op_next = (OP*)gwop; | |
10259 offset = pad_findmy_pvs("$_", 0); | |
10260 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) { | |
10261 o->op_private = gwop->op_private = 0; | |
10262 gwop->op_targ = pad_alloc(type, SVs_PADTMP); | |
10263 } | |
10264 else { | |
10265 o->op_private = gwop->op_private = OPpGREP_LEX; | |
10266 gwop->op_targ = o->op_targ = offset; | |
10267 } | |
10268 | |
10269 kid = OpSIBLING(cLISTOPo->op_first); | |
10270 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid)) | |
10271 op_lvalue(kid, OP_GREPSTART); | |
10272 | |
10273 return (OP*)gwop; | |
10274 } | |
10275 | |
10276 OP * | |
10277 Perl_ck_index(pTHX_ OP *o) | |
10278 { | |
10279 PERL_ARGS_ASSERT_CK_INDEX; | |
10280 | |
10281 if (o->op_flags & OPf_KIDS) { | |
10282 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ | |
10283 if (kid) | |
10284 kid = OpSIBLING(kid); /* get past "big" */ | |
10285 if (kid && kid->op_type == OP_CONST) { | |
10286 const bool save_taint = TAINT_get; | |
10287 SV *sv = kSVOP->op_sv; | |
10288 if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) { | |
10289 sv = newSV(0); | |
10290 sv_copypv(sv, kSVOP->op_sv); | |
10291 SvREFCNT_dec_NN(kSVOP->op_sv); | |
10292 kSVOP->op_sv = sv; | |
10293 } | |
10294 if (SvOK(sv)) fbm_compile(sv, 0); | |
10295 TAINT_set(save_taint); | |
10296 #ifdef NO_TAINT_SUPPORT | |
10297 PERL_UNUSED_VAR(save_taint); | |
10298 #endif | |
10299 } | |
10300 } | |
10301 return ck_fun(o); | |
10302 } | |
10303 | |
10304 OP * | |
10305 Perl_ck_lfun(pTHX_ OP *o) | |
10306 { | |
10307 const OPCODE type = o->op_type; | |
10308 | |
10309 PERL_ARGS_ASSERT_CK_LFUN; | |
10310 | |
10311 return modkids(ck_fun(o), type); | |
10312 } | |
10313 | |
10314 OP * | |
10315 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */ | |
10316 { | |
10317 PERL_ARGS_ASSERT_CK_DEFINED; | |
10318 | |
10319 if ((o->op_flags & OPf_KIDS)) { | |
10320 switch (cUNOPo->op_first->op_type) { | |
10321 case OP_RV2AV: | |
10322 case OP_PADAV: | |
10323 Perl_croak(aTHX_ "Can't use 'defined(@array)'" | |
10324 " (Maybe you should just omit the defined()?)"); | |
10325 break; | |
10326 case OP_RV2HV: | |
10327 case OP_PADHV: | |
10328 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'" | |
10329 " (Maybe you should just omit the defined()?)"); | |
10330 break; | |
10331 default: | |
10332 /* no warning */ | |
10333 break; | |
10334 } | |
10335 } | |
10336 return ck_rfun(o); | |
10337 } | |
10338 | |
10339 OP * | |
10340 Perl_ck_readline(pTHX_ OP *o) | |
10341 { | |
10342 PERL_ARGS_ASSERT_CK_READLINE; | |
10343 | |
10344 if (o->op_flags & OPf_KIDS) { | |
10345 OP *kid = cLISTOPo->op_first; | |
10346 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE; | |
10347 } | |
10348 else { | |
10349 OP * const newop | |
10350 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv)); | |
10351 op_free(o); | |
10352 return newop; | |
10353 } | |
10354 return o; | |
10355 } | |
10356 | |
10357 OP * | |
10358 Perl_ck_rfun(pTHX_ OP *o) | |
10359 { | |
10360 const OPCODE type = o->op_type; | |
10361 | |
10362 PERL_ARGS_ASSERT_CK_RFUN; | |
10363 | |
10364 return refkids(ck_fun(o), type); | |
10365 } | |
10366 | |
10367 OP * | |
10368 Perl_ck_listiob(pTHX_ OP *o) | |
10369 { | |
10370 OP *kid; | |
10371 | |
10372 PERL_ARGS_ASSERT_CK_LISTIOB; | |
10373 | |
10374 kid = cLISTOPo->op_first; | |
10375 if (!kid) { | |
10376 o = force_list(o, 1); | |
10377 kid = cLISTOPo->op_first; | |
10378 } | |
10379 if (kid->op_type == OP_PUSHMARK) | |
10380 kid = OpSIBLING(kid); | |
10381 if (kid && o->op_flags & OPf_STACKED) | |
10382 kid = OpSIBLING(kid); | |
10383 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */ | |
10384 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE | |
10385 && !kid->op_folded) { | |
10386 o->op_flags |= OPf_STACKED; /* make it a filehandle */ | |
10387 scalar(kid); | |
10388 /* replace old const op with new OP_RV2GV parent */ | |
10389 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first, | |
10390 OP_RV2GV, OPf_REF); | |
10391 kid = OpSIBLING(kid); | |
10392 } | |
10393 } | |
10394 | |
10395 if (!kid) | |
10396 op_append_elem(o->op_type, o, newDEFSVOP()); | |
10397 | |
10398 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF); | |
10399 return listkids(o); | |
10400 } | |
10401 | |
10402 OP * | |
10403 Perl_ck_smartmatch(pTHX_ OP *o) | |
10404 { | |
10405 dVAR; | |
10406 PERL_ARGS_ASSERT_CK_SMARTMATCH; | |
10407 if (0 == (o->op_flags & OPf_SPECIAL)) { | |
10408 OP *first = cBINOPo->op_first; | |
10409 OP *second = OpSIBLING(first); | |
10410 | |
10411 /* Implicitly take a reference to an array or hash */ | |
10412 | |
10413 /* remove the original two siblings, then add back the | |
10414 * (possibly different) first and second sibs. | |
10415 */ | |
10416 op_sibling_splice(o, NULL, 1, NULL); | |
10417 op_sibling_splice(o, NULL, 1, NULL); | |
10418 first = ref_array_or_hash(first); | |
10419 second = ref_array_or_hash(second); | |
10420 op_sibling_splice(o, NULL, 0, second); | |
10421 op_sibling_splice(o, NULL, 0, first); | |
10422 | |
10423 /* Implicitly take a reference to a regular expression */ | |
10424 if (first->op_type == OP_MATCH) { | |
10425 OpTYPE_set(first, OP_QR); | |
10426 } | |
10427 if (second->op_type == OP_MATCH) { | |
10428 OpTYPE_set(second, OP_QR); | |
10429 } | |
10430 } | |
10431 | |
10432 return o; | |
10433 } | |
10434 | |
10435 | |
10436 static OP * | |
10437 S_maybe_targlex(pTHX_ OP *o) | |
10438 { | |
10439 OP * const kid = cLISTOPo->op_first; | |
10440 /* has a disposable target? */ | |
10441 if ((PL_opargs[kid->op_type] & OA_TARGLEX) | |
10442 && !(kid->op_flags & OPf_STACKED) | |
10443 /* Cannot steal the second time! */ | |
10444 && !(kid->op_private & OPpTARGET_MY) | |
10445 ) | |
10446 { | |
10447 OP * const kkid = OpSIBLING(kid); | |
10448 | |
10449 /* Can just relocate the target. */ | |
10450 if (kkid && kkid->op_type == OP_PADSV | |
10451 && (!(kkid->op_private & OPpLVAL_INTRO) | |
10452 || kkid->op_private & OPpPAD_STATE)) | |
10453 { | |
10454 kid->op_targ = kkid->op_targ; | |
10455 kkid->op_targ = 0; | |
10456 /* Now we do not need PADSV and SASSIGN. | |
10457 * Detach kid and free the rest. */ | |
10458 op_sibling_splice(o, NULL, 1, NULL); | |
10459 op_free(o); | |
10460 kid->op_private |= OPpTARGET_MY; /* Used for context settings */ | |
10461 return kid; | |
10462 } | |
10463 } | |
10464 return o; | |
10465 } | |
10466 | |
10467 OP * | |
10468 Perl_ck_sassign(pTHX_ OP *o) | |
10469 { | |
10470 dVAR; | |
10471 OP * const kid = cLISTOPo->op_first; | |
10472 | |
10473 PERL_ARGS_ASSERT_CK_SASSIGN; | |
10474 | |
10475 if (OpHAS_SIBLING(kid)) { | |
10476 OP *kkid = OpSIBLING(kid); | |
10477 /* For state variable assignment with attributes, kkid is a list op | |
10478 whose op_last is a padsv. */ | |
10479 if ((kkid->op_type == OP_PADSV || | |
10480 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) && | |
10481 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV | |
10482 ) | |
10483 ) | |
10484 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) | |
10485 == (OPpLVAL_INTRO|OPpPAD_STATE)) { | |
10486 const PADOFFSET target = kkid->op_targ; | |
10487 OP *const other = newOP(OP_PADSV, | |
10488 kkid->op_flags | |
10489 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8)); | |
10490 OP *const first = newOP(OP_NULL, 0); | |
10491 OP *const nullop = | |
10492 newCONDOP(0, first, o, other); | |
10493 /* XXX targlex disabled for now; see ticket #124160 | |
10494 newCONDOP(0, first, S_maybe_targlex(aTHX_ o), other); | |
10495 */ | |
10496 OP *const condop = first->op_next; | |
10497 | |
10498 OpTYPE_set(condop, OP_ONCE); | |
10499 other->op_targ = target; | |
10500 nullop->op_flags |= OPf_WANT_SCALAR; | |
10501 | |
10502 /* Store the initializedness of state vars in a separate | |
10503 pad entry. */ | |
10504 condop->op_targ = | |
10505 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0); | |
10506 /* hijacking PADSTALE for uninitialized state variables */ | |
10507 SvPADSTALE_on(PAD_SVl(condop->op_targ)); | |
10508 | |
10509 return nullop; | |
10510 } | |
10511 } | |
10512 return S_maybe_targlex(aTHX_ o); | |
10513 } | |
10514 | |
10515 OP * | |
10516 Perl_ck_match(pTHX_ OP *o) | |
10517 { | |
10518 PERL_ARGS_ASSERT_CK_MATCH; | |
10519 | |
10520 if (o->op_type != OP_QR && PL_compcv) { | |
10521 const PADOFFSET offset = pad_findmy_pvs("$_", 0); | |
10522 if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS_isOUR(offset))) { | |
10523 o->op_targ = offset; | |
10524 o->op_private |= OPpTARGET_MY; | |
10525 } | |
10526 } | |
10527 if (o->op_type == OP_MATCH || o->op_type == OP_QR) | |
10528 o->op_private |= OPpRUNTIME; | |
10529 return o; | |
10530 } | |
10531 | |
10532 OP * | |
10533 Perl_ck_method(pTHX_ OP *o) | |
10534 { | |
10535 SV *sv, *methsv, *rclass; | |
10536 const char* method; | |
10537 char* compatptr; | |
10538 int utf8; | |
10539 STRLEN len, nsplit = 0, i; | |
10540 OP* new_op; | |
10541 OP * const kid = cUNOPo->op_first; | |
10542 | |
10543 PERL_ARGS_ASSERT_CK_METHOD; | |
10544 if (kid->op_type != OP_CONST) return o; | |
10545 | |
10546 sv = kSVOP->op_sv; | |
10547 | |
10548 /* replace ' with :: */ | |
10549 while ((compatptr = strchr(SvPVX(sv), '\''))) { | |
10550 *compatptr = ':'; | |
10551 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1); | |
10552 } | |
10553 | |
10554 method = SvPVX_const(sv); | |
10555 len = SvCUR(sv); | |
10556 utf8 = SvUTF8(sv) ? -1 : 1; | |
10557 | |
10558 for (i = len - 1; i > 0; --i) if (method[i] == ':') { | |
10559 nsplit = i+1; | |
10560 break; | |
10561 } | |
10562 | |
10563 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0); | |
10564 | |
10565 if (!nsplit) { /* $proto->method() */ | |
10566 op_free(o); | |
10567 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv); | |
10568 } | |
10569 | |
10570 if (nsplit == 7 && memEQ(method, "SUPER::", nsplit)) { /* $proto->SUPER::method() */ | |
10571 op_free(o); | |
10572 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv); | |
10573 } | |
10574 | |
10575 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */ | |
10576 if (nsplit >= 9 && strnEQ(method+nsplit-9, "::SUPER::", 9)) { | |
10577 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0); | |
10578 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv); | |
10579 } else { | |
10580 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0); | |
10581 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv); | |
10582 } | |
10583 #ifdef USE_ITHREADS | |
10584 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ); | |
10585 #else | |
10586 cMETHOPx(new_op)->op_rclass_sv = rclass; | |
10587 #endif | |
10588 op_free(o); | |
10589 return new_op; | |
10590 } | |
10591 | |
10592 OP * | |
10593 Perl_ck_null(pTHX_ OP *o) | |
10594 { | |
10595 PERL_ARGS_ASSERT_CK_NULL; | |
10596 PERL_UNUSED_CONTEXT; | |
10597 return o; | |
10598 } | |
10599 | |
10600 OP * | |
10601 Perl_ck_open(pTHX_ OP *o) | |
10602 { | |
10603 PERL_ARGS_ASSERT_CK_OPEN; | |
10604 | |
10605 S_io_hints(aTHX_ o); | |
10606 { | |
10607 /* In case of three-arg dup open remove strictness | |
10608 * from the last arg if it is a bareword. */ | |
10609 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */ | |
10610 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */ | |
10611 OP *oa; | |
10612 const char *mode; | |
10613 | |
10614 if ((last->op_type == OP_CONST) && /* The bareword. */ | |
10615 (last->op_private & OPpCONST_BARE) && | |
10616 (last->op_private & OPpCONST_STRICT) && | |
10617 (oa = OpSIBLING(first)) && /* The fh. */ | |
10618 (oa = OpSIBLING(oa)) && /* The mode. */ | |
10619 (oa->op_type == OP_CONST) && | |
10620 SvPOK(((SVOP*)oa)->op_sv) && | |
10621 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) && | |
10622 mode[0] == '>' && mode[1] == '&' && /* A dup open. */ | |
10623 (last == OpSIBLING(oa))) /* The bareword. */ | |
10624 last->op_private &= ~OPpCONST_STRICT; | |
10625 } | |
10626 return ck_fun(o); | |
10627 } | |
10628 | |
10629 OP * | |
10630 Perl_ck_prototype(pTHX_ OP *o) | |
10631 { | |
10632 PERL_ARGS_ASSERT_CK_PROTOTYPE; | |
10633 if (!(o->op_flags & OPf_KIDS)) { | |
10634 op_free(o); | |
10635 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP()); | |
10636 } | |
10637 return o; | |
10638 } | |
10639 | |
10640 OP * | |
10641 Perl_ck_refassign(pTHX_ OP *o) | |
10642 { | |
10643 OP * const right = cLISTOPo->op_first; | |
10644 OP * const left = OpSIBLING(right); | |
10645 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first; | |
10646 bool stacked = 0; | |
10647 | |
10648 PERL_ARGS_ASSERT_CK_REFASSIGN; | |
10649 assert (left); | |
10650 assert (left->op_type == OP_SREFGEN); | |
10651 | |
10652 o->op_private = varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE); | |
10653 | |
10654 switch (varop->op_type) { | |
10655 case OP_PADAV: | |
10656 o->op_private |= OPpLVREF_AV; | |
10657 goto settarg; | |
10658 case OP_PADHV: | |
10659 o->op_private |= OPpLVREF_HV; | |
10660 case OP_PADSV: | |
10661 settarg: | |
10662 o->op_targ = varop->op_targ; | |
10663 varop->op_targ = 0; | |
10664 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); | |
10665 break; | |
10666 case OP_RV2AV: | |
10667 o->op_private |= OPpLVREF_AV; | |
10668 goto checkgv; | |
10669 NOT_REACHED; /* NOTREACHED */ | |
10670 case OP_RV2HV: | |
10671 o->op_private |= OPpLVREF_HV; | |
10672 /* FALLTHROUGH */ | |
10673 case OP_RV2SV: | |
10674 checkgv: | |
10675 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad; | |
10676 detach_and_stack: | |
10677 /* Point varop to its GV kid, detached. */ | |
10678 varop = op_sibling_splice(varop, NULL, -1, NULL); | |
10679 stacked = TRUE; | |
10680 break; | |
10681 case OP_RV2CV: { | |
10682 OP * const kidparent = | |
10683 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first); | |
10684 OP * const kid = cUNOPx(kidparent)->op_first; | |
10685 o->op_private |= OPpLVREF_CV; | |
10686 if (kid->op_type == OP_GV) { | |
10687 varop = kidparent; | |
10688 goto detach_and_stack; | |
10689 } | |
10690 if (kid->op_type != OP_PADCV) goto bad; | |
10691 o->op_targ = kid->op_targ; | |
10692 kid->op_targ = 0; | |
10693 break; | |
10694 } | |
10695 case OP_AELEM: | |
10696 case OP_HELEM: | |
10697 o->op_private |= OPpLVREF_ELEM; | |
10698 op_null(varop); | |
10699 stacked = TRUE; | |
10700 /* Detach varop. */ | |
10701 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL); | |
10702 break; | |
10703 default: | |
10704 bad: | |
10705 /* diag_listed_as: Can't modify reference to %s in %s assignment */ | |
10706 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar " | |
10707 "assignment", | |
10708 OP_DESC(varop))); | |
10709 return o; | |
10710 } | |
10711 if (!FEATURE_REFALIASING_IS_ENABLED) | |
10712 Perl_croak(aTHX_ | |
10713 "Experimental aliasing via reference not enabled"); | |
10714 Perl_ck_warner_d(aTHX_ | |
10715 packWARN(WARN_EXPERIMENTAL__REFALIASING), | |
10716 "Aliasing via reference is experimental"); | |
10717 if (stacked) { | |
10718 o->op_flags |= OPf_STACKED; | |
10719 op_sibling_splice(o, right, 1, varop); | |
10720 } | |
10721 else { | |
10722 o->op_flags &=~ OPf_STACKED; | |
10723 op_sibling_splice(o, right, 1, NULL); | |
10724 } | |
10725 op_free(left); | |
10726 return o; | |
10727 } | |
10728 | |
10729 OP * | |
10730 Perl_ck_repeat(pTHX_ OP *o) | |
10731 { | |
10732 PERL_ARGS_ASSERT_CK_REPEAT; | |
10733 | |
10734 if (cBINOPo->op_first->op_flags & OPf_PARENS) { | |
10735 OP* kids; | |
10736 o->op_private |= OPpREPEAT_DOLIST; | |
10737 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */ | |
10738 kids = force_list(kids, 1); /* promote it to a list */ | |
10739 op_sibling_splice(o, NULL, 0, kids); /* and add back */ | |
10740 } | |
10741 else | |
10742 scalar(o); | |
10743 return o; | |
10744 } | |
10745 | |
10746 OP * | |
10747 Perl_ck_require(pTHX_ OP *o) | |
10748 { | |
10749 GV* gv; | |
10750 | |
10751 PERL_ARGS_ASSERT_CK_REQUIRE; | |
10752 | |
10753 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */ | |
10754 SVOP * const kid = (SVOP*)cUNOPo->op_first; | |
10755 HEK *hek; | |
10756 U32 hash; | |
10757 char *s; | |
10758 STRLEN len; | |
10759 if (kid->op_type == OP_CONST) { | |
10760 SV * const sv = kid->op_sv; | |
10761 U32 const was_readonly = SvREADONLY(sv); | |
10762 if (kid->op_private & OPpCONST_BARE) { | |
10763 dVAR; | |
10764 const char *end; | |
10765 | |
10766 if (was_readonly) { | |
10767 SvREADONLY_off(sv); | |
10768 } | |
10769 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0); | |
10770 | |
10771 s = SvPVX(sv); | |
10772 len = SvCUR(sv); | |
10773 end = s + len; | |
10774 for (; s < end; s++) { | |
10775 if (*s == ':' && s[1] == ':') { | |
10776 *s = '/'; | |
10777 Move(s+2, s+1, end - s - 1, char); | |
10778 --end; | |
10779 } | |
10780 } | |
10781 SvEND_set(sv, end); | |
10782 sv_catpvs(sv, ".pm"); | |
10783 PERL_HASH(hash, SvPVX(sv), SvCUR(sv)); | |
10784 hek = share_hek(SvPVX(sv), | |
10785 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1), | |
10786 hash); | |
10787 sv_sethek(sv, hek); | |
10788 unshare_hek(hek); | |
10789 SvFLAGS(sv) |= was_readonly; | |
10790 } | |
10791 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv) | |
10792 && !SvVOK(sv)) { | |
10793 s = SvPV(sv, len); | |
10794 if (SvREFCNT(sv) > 1) { | |
10795 kid->op_sv = newSVpvn_share( | |
10796 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0); | |
10797 SvREFCNT_dec_NN(sv); | |
10798 } | |
10799 else { | |
10800 dVAR; | |
10801 if (was_readonly) SvREADONLY_off(sv); | |
10802 PERL_HASH(hash, s, len); | |
10803 hek = share_hek(s, | |
10804 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, | |
10805 hash); | |
10806 sv_sethek(sv, hek); | |
10807 unshare_hek(hek); | |
10808 SvFLAGS(sv) |= was_readonly; | |
10809 } | |
10810 } | |
10811 } | |
10812 } | |
10813 | |
10814 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */ | |
10815 /* handle override, if any */ | |
10816 && (gv = gv_override("require", 7))) { | |
10817 OP *kid, *newop; | |
10818 if (o->op_flags & OPf_KIDS) { | |
10819 kid = cUNOPo->op_first; | |
10820 op_sibling_splice(o, NULL, -1, NULL); | |
10821 } | |
10822 else { | |
10823 kid = newDEFSVOP(); | |
10824 } | |
10825 op_free(o); | |
10826 newop = S_new_entersubop(aTHX_ gv, kid); | |
10827 return newop; | |
10828 } | |
10829 | |
10830 return ck_fun(o); | |
10831 } | |
10832 | |
10833 OP * | |
10834 Perl_ck_return(pTHX_ OP *o) | |
10835 { | |
10836 OP *kid; | |
10837 | |
10838 PERL_ARGS_ASSERT_CK_RETURN; | |
10839 | |
10840 kid = OpSIBLING(cLISTOPo->op_first); | |
10841 if (CvLVALUE(PL_compcv)) { | |
10842 for (; kid; kid = OpSIBLING(kid)) | |
10843 op_lvalue(kid, OP_LEAVESUBLV); | |
10844 } | |
10845 | |
10846 return o; | |
10847 } | |
10848 | |
10849 OP * | |
10850 Perl_ck_select(pTHX_ OP *o) | |
10851 { | |
10852 dVAR; | |
10853 OP* kid; | |
10854 | |
10855 PERL_ARGS_ASSERT_CK_SELECT; | |
10856 | |
10857 if (o->op_flags & OPf_KIDS) { | |
10858 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ | |
10859 if (kid && OpHAS_SIBLING(kid)) { | |
10860 OpTYPE_set(o, OP_SSELECT); | |
10861 o = ck_fun(o); | |
10862 return fold_constants(op_integerize(op_std_init(o))); | |
10863 } | |
10864 } | |
10865 o = ck_fun(o); | |
10866 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ | |
10867 if (kid && kid->op_type == OP_RV2GV) | |
10868 kid->op_private &= ~HINT_STRICT_REFS; | |
10869 return o; | |
10870 } | |
10871 | |
10872 OP * | |
10873 Perl_ck_shift(pTHX_ OP *o) | |
10874 { | |
10875 const I32 type = o->op_type; | |
10876 | |
10877 PERL_ARGS_ASSERT_CK_SHIFT; | |
10878 | |
10879 if (!(o->op_flags & OPf_KIDS)) { | |
10880 OP *argop; | |
10881 | |
10882 if (!CvUNIQUE(PL_compcv)) { | |
10883 o->op_flags |= OPf_SPECIAL; | |
10884 return o; | |
10885 } | |
10886 | |
10887 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv))); | |
10888 op_free(o); | |
10889 return newUNOP(type, 0, scalar(argop)); | |
10890 } | |
10891 return scalar(ck_fun(o)); | |
10892 } | |
10893 | |
10894 OP * | |
10895 Perl_ck_sort(pTHX_ OP *o) | |
10896 { | |
10897 OP *firstkid; | |
10898 OP *kid; | |
10899 HV * const hinthv = | |
10900 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL; | |
10901 U8 stacked; | |
10902 | |
10903 PERL_ARGS_ASSERT_CK_SORT; | |
10904 | |
10905 if (hinthv) { | |
10906 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE); | |
10907 if (svp) { | |
10908 const I32 sorthints = (I32)SvIV(*svp); | |
10909 if ((sorthints & HINT_SORT_QUICKSORT) != 0) | |
10910 o->op_private |= OPpSORT_QSORT; | |
10911 if ((sorthints & HINT_SORT_STABLE) != 0) | |
10912 o->op_private |= OPpSORT_STABLE; | |
10913 } | |
10914 } | |
10915 | |
10916 if (o->op_flags & OPf_STACKED) | |
10917 simplify_sort(o); | |
10918 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ | |
10919 | |
10920 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */ | |
10921 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */ | |
10922 | |
10923 /* if the first arg is a code block, process it and mark sort as | |
10924 * OPf_SPECIAL */ | |
10925 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) { | |
10926 LINKLIST(kid); | |
10927 if (kid->op_type == OP_LEAVE) | |
10928 op_null(kid); /* wipe out leave */ | |
10929 /* Prevent execution from escaping out of the sort block. */ | |
10930 kid->op_next = 0; | |
10931 | |
10932 /* provide scalar context for comparison function/block */ | |
10933 kid = scalar(firstkid); | |
10934 kid->op_next = kid; | |
10935 o->op_flags |= OPf_SPECIAL; | |
10936 } | |
10937 else if (kid->op_type == OP_CONST | |
10938 && kid->op_private & OPpCONST_BARE) { | |
10939 char tmpbuf[256]; | |
10940 STRLEN len; | |
10941 PADOFFSET off; | |
10942 const char * const name = SvPV(kSVOP_sv, len); | |
10943 *tmpbuf = '&'; | |
10944 assert (len < 256); | |
10945 Copy(name, tmpbuf+1, len, char); | |
10946 off = pad_findmy_pvn(tmpbuf, len+1, SvUTF8(kSVOP_sv)); | |
10947 if (off != NOT_IN_PAD) { | |
10948 if (PAD_COMPNAME_FLAGS_isOUR(off)) { | |
10949 SV * const fq = | |
10950 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off))); | |
10951 sv_catpvs(fq, "::"); | |
10952 sv_catsv(fq, kSVOP_sv); | |
10953 SvREFCNT_dec_NN(kSVOP_sv); | |
10954 kSVOP->op_sv = fq; | |
10955 } | |
10956 else { | |
10957 OP * const padop = newOP(OP_PADCV, 0); | |
10958 padop->op_targ = off; | |
10959 /* replace the const op with the pad op */ | |
10960 op_sibling_splice(firstkid, NULL, 1, padop); | |
10961 op_free(kid); | |
10962 } | |
10963 } | |
10964 } | |
10965 | |
10966 firstkid = OpSIBLING(firstkid); | |
10967 } | |
10968 | |
10969 for (kid = firstkid; kid; kid = OpSIBLING(kid)) { | |
10970 /* provide list context for arguments */ | |
10971 list(kid); | |
10972 if (stacked) | |
10973 op_lvalue(kid, OP_GREPSTART); | |
10974 } | |
10975 | |
10976 return o; | |
10977 } | |
10978 | |
10979 /* for sort { X } ..., where X is one of | |
10980 * $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a | |
10981 * elide the second child of the sort (the one containing X), | |
10982 * and set these flags as appropriate | |
10983 OPpSORT_NUMERIC; | |
10984 OPpSORT_INTEGER; | |
10985 OPpSORT_DESCEND; | |
10986 * Also, check and warn on lexical $a, $b. | |
10987 */ | |
10988 | |
10989 STATIC void | |
10990 S_simplify_sort(pTHX_ OP *o) | |
10991 { | |
10992 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ | |
10993 OP *k; | |
10994 int descending; | |
10995 GV *gv; | |
10996 const char *gvname; | |
10997 bool have_scopeop; | |
10998 | |
10999 PERL_ARGS_ASSERT_SIMPLIFY_SORT; | |
11000 | |
11001 kid = kUNOP->op_first; /* get past null */ | |
11002 if (!(have_scopeop = kid->op_type == OP_SCOPE) | |
11003 && kid->op_type != OP_LEAVE) | |
11004 return; | |
11005 kid = kLISTOP->op_last; /* get past scope */ | |
11006 switch(kid->op_type) { | |
11007 case OP_NCMP: | |
11008 case OP_I_NCMP: | |
11009 case OP_SCMP: | |
11010 if (!have_scopeop) goto padkids; | |
11011 break; | |
11012 default: | |
11013 return; | |
11014 } | |
11015 k = kid; /* remember this node*/ | |
11016 if (kBINOP->op_first->op_type != OP_RV2SV | |
11017 || kBINOP->op_last ->op_type != OP_RV2SV) | |
11018 { | |
11019 /* | |
11020 Warn about my($a) or my($b) in a sort block, *if* $a or $b is | |
11021 then used in a comparison. This catches most, but not | |
11022 all cases. For instance, it catches | |
11023 sort { my($a); $a <=> $b } | |
11024 but not | |
11025 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; } | |
11026 (although why you'd do that is anyone's guess). | |
11027 */ | |
11028 | |
11029 padkids: | |
11030 if (!ckWARN(WARN_SYNTAX)) return; | |
11031 kid = kBINOP->op_first; | |
11032 do { | |
11033 if (kid->op_type == OP_PADSV) { | |
11034 PADNAME * const name = PAD_COMPNAME(kid->op_targ); | |
11035 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$' | |
11036 && ( PadnamePV(name)[1] == 'a' | |
11037 || PadnamePV(name)[1] == 'b' )) | |
11038 /* diag_listed_as: "my %s" used in sort comparison */ | |
11039 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
11040 "\"%s %s\" used in sort comparison", | |
11041 PadnameIsSTATE(name) | |
11042 ? "state" | |
11043 : "my", | |
11044 PadnamePV(name)); | |
11045 } | |
11046 } while ((kid = OpSIBLING(kid))); | |
11047 return; | |
11048 } | |
11049 kid = kBINOP->op_first; /* get past cmp */ | |
11050 if (kUNOP->op_first->op_type != OP_GV) | |
11051 return; | |
11052 kid = kUNOP->op_first; /* get past rv2sv */ | |
11053 gv = kGVOP_gv; | |
11054 if (GvSTASH(gv) != PL_curstash) | |
11055 return; | |
11056 gvname = GvNAME(gv); | |
11057 if (*gvname == 'a' && gvname[1] == '\0') | |
11058 descending = 0; | |
11059 else if (*gvname == 'b' && gvname[1] == '\0') | |
11060 descending = 1; | |
11061 else | |
11062 return; | |
11063 | |
11064 kid = k; /* back to cmp */ | |
11065 /* already checked above that it is rv2sv */ | |
11066 kid = kBINOP->op_last; /* down to 2nd arg */ | |
11067 if (kUNOP->op_first->op_type != OP_GV) | |
11068 return; | |
11069 kid = kUNOP->op_first; /* get past rv2sv */ | |
11070 gv = kGVOP_gv; | |
11071 if (GvSTASH(gv) != PL_curstash) | |
11072 return; | |
11073 gvname = GvNAME(gv); | |
11074 if ( descending | |
11075 ? !(*gvname == 'a' && gvname[1] == '\0') | |
11076 : !(*gvname == 'b' && gvname[1] == '\0')) | |
11077 return; | |
11078 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL); | |
11079 if (descending) | |
11080 o->op_private |= OPpSORT_DESCEND; | |
11081 if (k->op_type == OP_NCMP) | |
11082 o->op_private |= OPpSORT_NUMERIC; | |
11083 if (k->op_type == OP_I_NCMP) | |
11084 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER; | |
11085 kid = OpSIBLING(cLISTOPo->op_first); | |
11086 /* cut out and delete old block (second sibling) */ | |
11087 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL); | |
11088 op_free(kid); | |
11089 } | |
11090 | |
11091 OP * | |
11092 Perl_ck_split(pTHX_ OP *o) | |
11093 { | |
11094 dVAR; | |
11095 OP *kid; | |
11096 | |
11097 PERL_ARGS_ASSERT_CK_SPLIT; | |
11098 | |
11099 if (o->op_flags & OPf_STACKED) | |
11100 return no_fh_allowed(o); | |
11101 | |
11102 kid = cLISTOPo->op_first; | |
11103 if (kid->op_type != OP_NULL) | |
11104 Perl_croak(aTHX_ "panic: ck_split, type=%u", (unsigned) kid->op_type); | |
11105 /* delete leading NULL node, then add a CONST if no other nodes */ | |
11106 op_sibling_splice(o, NULL, 1, | |
11107 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" "))); | |
11108 op_free(kid); | |
11109 kid = cLISTOPo->op_first; | |
11110 | |
11111 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) { | |
11112 /* remove kid, and replace with new optree */ | |
11113 op_sibling_splice(o, NULL, 1, NULL); | |
11114 /* OPf_SPECIAL is used to trigger split " " behavior */ | |
11115 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, NULL, 0, 0); | |
11116 op_sibling_splice(o, NULL, 0, kid); | |
11117 } | |
11118 OpTYPE_set(kid, OP_PUSHRE); | |
11119 /* target implies @ary=..., so wipe it */ | |
11120 kid->op_targ = 0; | |
11121 scalar(kid); | |
11122 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) { | |
11123 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), | |
11124 "Use of /g modifier is meaningless in split"); | |
11125 } | |
11126 | |
11127 if (!OpHAS_SIBLING(kid)) | |
11128 op_append_elem(OP_SPLIT, o, newDEFSVOP()); | |
11129 | |
11130 kid = OpSIBLING(kid); | |
11131 assert(kid); | |
11132 scalar(kid); | |
11133 | |
11134 if (!OpHAS_SIBLING(kid)) | |
11135 { | |
11136 op_append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0))); | |
11137 o->op_private |= OPpSPLIT_IMPLIM; | |
11138 } | |
11139 assert(OpHAS_SIBLING(kid)); | |
11140 | |
11141 kid = OpSIBLING(kid); | |
11142 scalar(kid); | |
11143 | |
11144 if (OpHAS_SIBLING(kid)) | |
11145 return too_many_arguments_pv(o,OP_DESC(o), 0); | |
11146 | |
11147 return o; | |
11148 } | |
11149 | |
11150 OP * | |
11151 Perl_ck_stringify(pTHX_ OP *o) | |
11152 { | |
11153 OP * const kid = OpSIBLING(cUNOPo->op_first); | |
11154 PERL_ARGS_ASSERT_CK_STRINGIFY; | |
11155 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA | |
11156 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST | |
11157 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST) | |
11158 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */ | |
11159 { | |
11160 op_sibling_splice(o, cUNOPo->op_first, -1, NULL); | |
11161 op_free(o); | |
11162 return kid; | |
11163 } | |
11164 return ck_fun(o); | |
11165 } | |
11166 | |
11167 OP * | |
11168 Perl_ck_join(pTHX_ OP *o) | |
11169 { | |
11170 OP * const kid = OpSIBLING(cLISTOPo->op_first); | |
11171 | |
11172 PERL_ARGS_ASSERT_CK_JOIN; | |
11173 | |
11174 if (kid && kid->op_type == OP_MATCH) { | |
11175 if (ckWARN(WARN_SYNTAX)) { | |
11176 const REGEXP *re = PM_GETRE(kPMOP); | |
11177 const SV *msg = re | |
11178 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re), | |
11179 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) ) | |
11180 : newSVpvs_flags( "STRING", SVs_TEMP ); | |
11181 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
11182 "/%"SVf"/ should probably be written as \"%"SVf"\"", | |
11183 SVfARG(msg), SVfARG(msg)); | |
11184 } | |
11185 } | |
11186 if (kid | |
11187 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */ | |
11188 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO)) | |
11189 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV | |
11190 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))))) | |
11191 { | |
11192 const OP * const bairn = OpSIBLING(kid); /* the list */ | |
11193 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */ | |
11194 && OP_GIMME(bairn,0) == G_SCALAR) | |
11195 { | |
11196 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED, | |
11197 op_sibling_splice(o, kid, 1, NULL)); | |
11198 op_free(o); | |
11199 return ret; | |
11200 } | |
11201 } | |
11202 | |
11203 return ck_fun(o); | |
11204 } | |
11205 | |
11206 /* | |
11207 =for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags | |
11208 | |
11209 Examines an op, which is expected to identify a subroutine at runtime, | |
11210 and attempts to determine at compile time which subroutine it identifies. | |
11211 This is normally used during Perl compilation to determine whether | |
11212 a prototype can be applied to a function call. I<cvop> is the op | |
11213 being considered, normally an C<rv2cv> op. A pointer to the identified | |
11214 subroutine is returned, if it could be determined statically, and a null | |
11215 pointer is returned if it was not possible to determine statically. | |
11216 | |
11217 Currently, the subroutine can be identified statically if the RV that the | |
11218 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op. | |
11219 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is | |
11220 suitable if the constant value must be an RV pointing to a CV. Details of | |
11221 this process may change in future versions of Perl. If the C<rv2cv> op | |
11222 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify | |
11223 the subroutine statically: this flag is used to suppress compile-time | |
11224 magic on a subroutine call, forcing it to use default runtime behaviour. | |
11225 | |
11226 If I<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling | |
11227 of a GV reference is modified. If a GV was examined and its CV slot was | |
11228 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set. | |
11229 If the op is not optimised away, and the CV slot is later populated with | |
11230 a subroutine having a prototype, that flag eventually triggers the warning | |
11231 "called too early to check prototype". | |
11232 | |
11233 If I<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead | |
11234 of returning a pointer to the subroutine it returns a pointer to the | |
11235 GV giving the most appropriate name for the subroutine in this context. | |
11236 Normally this is just the C<CvGV> of the subroutine, but for an anonymous | |
11237 (C<CvANON>) subroutine that is referenced through a GV it will be the | |
11238 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned. | |
11239 A null pointer is returned as usual if there is no statically-determinable | |
11240 subroutine. | |
11241 | |
11242 =cut | |
11243 */ | |
11244 | |
11245 /* shared by toke.c:yylex */ | |
11246 CV * | |
11247 Perl_find_lexical_cv(pTHX_ PADOFFSET off) | |
11248 { | |
11249 PADNAME *name = PAD_COMPNAME(off); | |
11250 CV *compcv = PL_compcv; | |
11251 while (PadnameOUTER(name)) { | |
11252 assert(PARENT_PAD_INDEX(name)); | |
11253 compcv = CvOUTSIDE(compcv); | |
11254 name = PadlistNAMESARRAY(CvPADLIST(compcv)) | |
11255 [off = PARENT_PAD_INDEX(name)]; | |
11256 } | |
11257 assert(!PadnameIsOUR(name)); | |
11258 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) { | |
11259 return PadnamePROTOCV(name); | |
11260 } | |
11261 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off]; | |
11262 } | |
11263 | |
11264 CV * | |
11265 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags) | |
11266 { | |
11267 OP *rvop; | |
11268 CV *cv; | |
11269 GV *gv; | |
11270 PERL_ARGS_ASSERT_RV2CV_OP_CV; | |
11271 if (flags & ~RV2CVOPCV_FLAG_MASK) | |
11272 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags); | |
11273 if (cvop->op_type != OP_RV2CV) | |
11274 return NULL; | |
11275 if (cvop->op_private & OPpENTERSUB_AMPER) | |
11276 return NULL; | |
11277 if (!(cvop->op_flags & OPf_KIDS)) | |
11278 return NULL; | |
11279 rvop = cUNOPx(cvop)->op_first; | |
11280 switch (rvop->op_type) { | |
11281 case OP_GV: { | |
11282 gv = cGVOPx_gv(rvop); | |
11283 if (!isGV(gv)) { | |
11284 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) { | |
11285 cv = MUTABLE_CV(SvRV(gv)); | |
11286 gv = NULL; | |
11287 break; | |
11288 } | |
11289 if (flags & RV2CVOPCV_RETURN_STUB) | |
11290 return (CV *)gv; | |
11291 else return NULL; | |
11292 } | |
11293 cv = GvCVu(gv); | |
11294 if (!cv) { | |
11295 if (flags & RV2CVOPCV_MARK_EARLY) | |
11296 rvop->op_private |= OPpEARLY_CV; | |
11297 return NULL; | |
11298 } | |
11299 } break; | |
11300 case OP_CONST: { | |
11301 SV *rv = cSVOPx_sv(rvop); | |
11302 if (!SvROK(rv)) | |
11303 return NULL; | |
11304 cv = (CV*)SvRV(rv); | |
11305 gv = NULL; | |
11306 } break; | |
11307 case OP_PADCV: { | |
11308 cv = find_lexical_cv(rvop->op_targ); | |
11309 gv = NULL; | |
11310 } break; | |
11311 default: { | |
11312 return NULL; | |
11313 } NOT_REACHED; /* NOTREACHED */ | |
11314 } | |
11315 if (SvTYPE((SV*)cv) != SVt_PVCV) | |
11316 return NULL; | |
11317 if (flags & (RV2CVOPCV_RETURN_NAME_GV|RV2CVOPCV_MAYBE_NAME_GV)) { | |
11318 if ((!CvANON(cv) || !gv) && !CvLEXICAL(cv) | |
11319 && ((flags & RV2CVOPCV_RETURN_NAME_GV) || !CvNAMED(cv))) | |
11320 gv = CvGV(cv); | |
11321 return (CV*)gv; | |
11322 } else { | |
11323 return cv; | |
11324 } | |
11325 } | |
11326 | |
11327 /* | |
11328 =for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop | |
11329 | |
11330 Performs the default fixup of the arguments part of an C<entersub> | |
11331 op tree. This consists of applying list context to each of the | |
11332 argument ops. This is the standard treatment used on a call marked | |
11333 with C<&>, or a method call, or a call through a subroutine reference, | |
11334 or any other call where the callee can't be identified at compile time, | |
11335 or a call where the callee has no prototype. | |
11336 | |
11337 =cut | |
11338 */ | |
11339 | |
11340 OP * | |
11341 Perl_ck_entersub_args_list(pTHX_ OP *entersubop) | |
11342 { | |
11343 OP *aop; | |
11344 | |
11345 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST; | |
11346 | |
11347 aop = cUNOPx(entersubop)->op_first; | |
11348 if (!OpHAS_SIBLING(aop)) | |
11349 aop = cUNOPx(aop)->op_first; | |
11350 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) { | |
11351 /* skip the extra attributes->import() call implicitly added in | |
11352 * something like foo(my $x : bar) | |
11353 */ | |
11354 if ( aop->op_type == OP_ENTERSUB | |
11355 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID | |
11356 ) | |
11357 continue; | |
11358 list(aop); | |
11359 op_lvalue(aop, OP_ENTERSUB); | |
11360 } | |
11361 return entersubop; | |
11362 } | |
11363 | |
11364 /* | |
11365 =for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv | |
11366 | |
11367 Performs the fixup of the arguments part of an C<entersub> op tree | |
11368 based on a subroutine prototype. This makes various modifications to | |
11369 the argument ops, from applying context up to inserting C<refgen> ops, | |
11370 and checking the number and syntactic types of arguments, as directed by | |
11371 the prototype. This is the standard treatment used on a subroutine call, | |
11372 not marked with C<&>, where the callee can be identified at compile time | |
11373 and has a prototype. | |
11374 | |
11375 I<protosv> supplies the subroutine prototype to be applied to the call. | |
11376 It may be a normal defined scalar, of which the string value will be used. | |
11377 Alternatively, for convenience, it may be a subroutine object (a C<CV*> | |
11378 that has been cast to C<SV*>) which has a prototype. The prototype | |
11379 supplied, in whichever form, does not need to match the actual callee | |
11380 referenced by the op tree. | |
11381 | |
11382 If the argument ops disagree with the prototype, for example by having | |
11383 an unacceptable number of arguments, a valid op tree is returned anyway. | |
11384 The error is reflected in the parser state, normally resulting in a single | |
11385 exception at the top level of parsing which covers all the compilation | |
11386 errors that occurred. In the error message, the callee is referred to | |
11387 by the name defined by the I<namegv> parameter. | |
11388 | |
11389 =cut | |
11390 */ | |
11391 | |
11392 OP * | |
11393 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv) | |
11394 { | |
11395 STRLEN proto_len; | |
11396 const char *proto, *proto_end; | |
11397 OP *aop, *prev, *cvop, *parent; | |
11398 int optional = 0; | |
11399 I32 arg = 0; | |
11400 I32 contextclass = 0; | |
11401 const char *e = NULL; | |
11402 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO; | |
11403 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv)) | |
11404 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, " | |
11405 "flags=%lx", (unsigned long) SvFLAGS(protosv)); | |
11406 if (SvTYPE(protosv) == SVt_PVCV) | |
11407 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv); | |
11408 else proto = SvPV(protosv, proto_len); | |
11409 proto = S_strip_spaces(aTHX_ proto, &proto_len); | |
11410 proto_end = proto + proto_len; | |
11411 parent = entersubop; | |
11412 aop = cUNOPx(entersubop)->op_first; | |
11413 if (!OpHAS_SIBLING(aop)) { | |
11414 parent = aop; | |
11415 aop = cUNOPx(aop)->op_first; | |
11416 } | |
11417 prev = aop; | |
11418 aop = OpSIBLING(aop); | |
11419 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; | |
11420 while (aop != cvop) { | |
11421 OP* o3 = aop; | |
11422 | |
11423 if (proto >= proto_end) | |
11424 { | |
11425 SV * const namesv = cv_name((CV *)namegv, NULL, 0); | |
11426 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, | |
11427 SVfARG(namesv)), SvUTF8(namesv)); | |
11428 return entersubop; | |
11429 } | |
11430 | |
11431 switch (*proto) { | |
11432 case ';': | |
11433 optional = 1; | |
11434 proto++; | |
11435 continue; | |
11436 case '_': | |
11437 /* _ must be at the end */ | |
11438 if (proto[1] && !strchr(";@%", proto[1])) | |
11439 goto oops; | |
11440 /* FALLTHROUGH */ | |
11441 case '$': | |
11442 proto++; | |
11443 arg++; | |
11444 scalar(aop); | |
11445 break; | |
11446 case '%': | |
11447 case '@': | |
11448 list(aop); | |
11449 arg++; | |
11450 break; | |
11451 case '&': | |
11452 proto++; | |
11453 arg++; | |
11454 if ( o3->op_type != OP_UNDEF | |
11455 && (o3->op_type != OP_SREFGEN | |
11456 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type | |
11457 != OP_ANONCODE | |
11458 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type | |
11459 != OP_RV2CV))) | |
11460 bad_type_gv(arg, namegv, o3, | |
11461 arg == 1 ? "block or sub {}" : "sub {}"); | |
11462 break; | |
11463 case '*': | |
11464 /* '*' allows any scalar type, including bareword */ | |
11465 proto++; | |
11466 arg++; | |
11467 if (o3->op_type == OP_RV2GV) | |
11468 goto wrapref; /* autoconvert GLOB -> GLOBref */ | |
11469 else if (o3->op_type == OP_CONST) | |
11470 o3->op_private &= ~OPpCONST_STRICT; | |
11471 scalar(aop); | |
11472 break; | |
11473 case '+': | |
11474 proto++; | |
11475 arg++; | |
11476 if (o3->op_type == OP_RV2AV || | |
11477 o3->op_type == OP_PADAV || | |
11478 o3->op_type == OP_RV2HV || | |
11479 o3->op_type == OP_PADHV | |
11480 ) { | |
11481 goto wrapref; | |
11482 } | |
11483 scalar(aop); | |
11484 break; | |
11485 case '[': case ']': | |
11486 goto oops; | |
11487 | |
11488 case '\\': | |
11489 proto++; | |
11490 arg++; | |
11491 again: | |
11492 switch (*proto++) { | |
11493 case '[': | |
11494 if (contextclass++ == 0) { | |
11495 e = strchr(proto, ']'); | |
11496 if (!e || e == proto) | |
11497 goto oops; | |
11498 } | |
11499 else | |
11500 goto oops; | |
11501 goto again; | |
11502 | |
11503 case ']': | |
11504 if (contextclass) { | |
11505 const char *p = proto; | |
11506 const char *const end = proto; | |
11507 contextclass = 0; | |
11508 while (*--p != '[') | |
11509 /* \[$] accepts any scalar lvalue */ | |
11510 if (*p == '$' | |
11511 && Perl_op_lvalue_flags(aTHX_ | |
11512 scalar(o3), | |
11513 OP_READ, /* not entersub */ | |
11514 OP_LVALUE_NO_CROAK | |
11515 )) goto wrapref; | |
11516 bad_type_gv(arg, namegv, o3, | |
11517 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p)); | |
11518 } else | |
11519 goto oops; | |
11520 break; | |
11521 case '*': | |
11522 if (o3->op_type == OP_RV2GV) | |
11523 goto wrapref; | |
11524 if (!contextclass) | |
11525 bad_type_gv(arg, namegv, o3, "symbol"); | |
11526 break; | |
11527 case '&': | |
11528 if (o3->op_type == OP_ENTERSUB | |
11529 && !(o3->op_flags & OPf_STACKED)) | |
11530 goto wrapref; | |
11531 if (!contextclass) | |
11532 bad_type_gv(arg, namegv, o3, "subroutine"); | |
11533 break; | |
11534 case '$': | |
11535 if (o3->op_type == OP_RV2SV || | |
11536 o3->op_type == OP_PADSV || | |
11537 o3->op_type == OP_HELEM || | |
11538 o3->op_type == OP_AELEM) | |
11539 goto wrapref; | |
11540 if (!contextclass) { | |
11541 /* \$ accepts any scalar lvalue */ | |
11542 if (Perl_op_lvalue_flags(aTHX_ | |
11543 scalar(o3), | |
11544 OP_READ, /* not entersub */ | |
11545 OP_LVALUE_NO_CROAK | |
11546 )) goto wrapref; | |
11547 bad_type_gv(arg, namegv, o3, "scalar"); | |
11548 } | |
11549 break; | |
11550 case '@': | |
11551 if (o3->op_type == OP_RV2AV || | |
11552 o3->op_type == OP_PADAV) | |
11553 { | |
11554 o3->op_flags &=~ OPf_PARENS; | |
11555 goto wrapref; | |
11556 } | |
11557 if (!contextclass) | |
11558 bad_type_gv(arg, namegv, o3, "array"); | |
11559 break; | |
11560 case '%': | |
11561 if (o3->op_type == OP_RV2HV || | |
11562 o3->op_type == OP_PADHV) | |
11563 { | |
11564 o3->op_flags &=~ OPf_PARENS; | |
11565 goto wrapref; | |
11566 } | |
11567 if (!contextclass) | |
11568 bad_type_gv(arg, namegv, o3, "hash"); | |
11569 break; | |
11570 wrapref: | |
11571 aop = S_op_sibling_newUNOP(aTHX_ parent, prev, | |
11572 OP_REFGEN, 0); | |
11573 if (contextclass && e) { | |
11574 proto = e + 1; | |
11575 contextclass = 0; | |
11576 } | |
11577 break; | |
11578 default: goto oops; | |
11579 } | |
11580 if (contextclass) | |
11581 goto again; | |
11582 break; | |
11583 case ' ': | |
11584 proto++; | |
11585 continue; | |
11586 default: | |
11587 oops: { | |
11588 Perl_croak(aTHX_ "Malformed prototype for %"SVf": %"SVf, | |
11589 SVfARG(cv_name((CV *)namegv, NULL, 0)), | |
11590 SVfARG(protosv)); | |
11591 } | |
11592 } | |
11593 | |
11594 op_lvalue(aop, OP_ENTERSUB); | |
11595 prev = aop; | |
11596 aop = OpSIBLING(aop); | |
11597 } | |
11598 if (aop == cvop && *proto == '_') { | |
11599 /* generate an access to $_ */ | |
11600 op_sibling_splice(parent, prev, 0, newDEFSVOP()); | |
11601 } | |
11602 if (!optional && proto_end > proto && | |
11603 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_')) | |
11604 { | |
11605 SV * const namesv = cv_name((CV *)namegv, NULL, 0); | |
11606 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, | |
11607 SVfARG(namesv)), SvUTF8(namesv)); | |
11608 } | |
11609 return entersubop; | |
11610 } | |
11611 | |
11612 /* | |
11613 =for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv | |
11614 | |
11615 Performs the fixup of the arguments part of an C<entersub> op tree either | |
11616 based on a subroutine prototype or using default list-context processing. | |
11617 This is the standard treatment used on a subroutine call, not marked | |
11618 with C<&>, where the callee can be identified at compile time. | |
11619 | |
11620 I<protosv> supplies the subroutine prototype to be applied to the call, | |
11621 or indicates that there is no prototype. It may be a normal scalar, | |
11622 in which case if it is defined then the string value will be used | |
11623 as a prototype, and if it is undefined then there is no prototype. | |
11624 Alternatively, for convenience, it may be a subroutine object (a C<CV*> | |
11625 that has been cast to C<SV*>), of which the prototype will be used if it | |
11626 has one. The prototype (or lack thereof) supplied, in whichever form, | |
11627 does not need to match the actual callee referenced by the op tree. | |
11628 | |
11629 If the argument ops disagree with the prototype, for example by having | |
11630 an unacceptable number of arguments, a valid op tree is returned anyway. | |
11631 The error is reflected in the parser state, normally resulting in a single | |
11632 exception at the top level of parsing which covers all the compilation | |
11633 errors that occurred. In the error message, the callee is referred to | |
11634 by the name defined by the I<namegv> parameter. | |
11635 | |
11636 =cut | |
11637 */ | |
11638 | |
11639 OP * | |
11640 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop, | |
11641 GV *namegv, SV *protosv) | |
11642 { | |
11643 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST; | |
11644 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv)) | |
11645 return ck_entersub_args_proto(entersubop, namegv, protosv); | |
11646 else | |
11647 return ck_entersub_args_list(entersubop); | |
11648 } | |
11649 | |
11650 OP * | |
11651 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv) | |
11652 { | |
11653 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv); | |
11654 OP *aop = cUNOPx(entersubop)->op_first; | |
11655 | |
11656 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE; | |
11657 | |
11658 if (!opnum) { | |
11659 OP *cvop; | |
11660 if (!OpHAS_SIBLING(aop)) | |
11661 aop = cUNOPx(aop)->op_first; | |
11662 aop = OpSIBLING(aop); | |
11663 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ; | |
11664 if (aop != cvop) | |
11665 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0); | |
11666 | |
11667 op_free(entersubop); | |
11668 switch(GvNAME(namegv)[2]) { | |
11669 case 'F': return newSVOP(OP_CONST, 0, | |
11670 newSVpv(CopFILE(PL_curcop),0)); | |
11671 case 'L': return newSVOP( | |
11672 OP_CONST, 0, | |
11673 Perl_newSVpvf(aTHX_ | |
11674 "%"IVdf, (IV)CopLINE(PL_curcop) | |
11675 ) | |
11676 ); | |
11677 case 'P': return newSVOP(OP_CONST, 0, | |
11678 (PL_curstash | |
11679 ? newSVhek(HvNAME_HEK(PL_curstash)) | |
11680 : &PL_sv_undef | |
11681 ) | |
11682 ); | |
11683 } | |
11684 NOT_REACHED; /* NOTREACHED */ | |
11685 } | |
11686 else { | |
11687 OP *prev, *cvop, *first, *parent; | |
11688 U32 flags = 0; | |
11689 | |
11690 parent = entersubop; | |
11691 if (!OpHAS_SIBLING(aop)) { | |
11692 parent = aop; | |
11693 aop = cUNOPx(aop)->op_first; | |
11694 } | |
11695 | |
11696 first = prev = aop; | |
11697 aop = OpSIBLING(aop); | |
11698 /* find last sibling */ | |
11699 for (cvop = aop; | |
11700 OpHAS_SIBLING(cvop); | |
11701 prev = cvop, cvop = OpSIBLING(cvop)) | |
11702 ; | |
11703 if (!(cvop->op_private & OPpENTERSUB_NOPAREN) | |
11704 /* Usually, OPf_SPECIAL on an op with no args means that it had | |
11705 * parens, but these have their own meaning for that flag: */ | |
11706 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH | |
11707 && opnum != OP_DELETE && opnum != OP_EXISTS) | |
11708 flags |= OPf_SPECIAL; | |
11709 /* excise cvop from end of sibling chain */ | |
11710 op_sibling_splice(parent, prev, 1, NULL); | |
11711 op_free(cvop); | |
11712 if (aop == cvop) aop = NULL; | |
11713 | |
11714 /* detach remaining siblings from the first sibling, then | |
11715 * dispose of original optree */ | |
11716 | |
11717 if (aop) | |
11718 op_sibling_splice(parent, first, -1, NULL); | |
11719 op_free(entersubop); | |
11720 | |
11721 if (opnum == OP_ENTEREVAL | |
11722 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9)) | |
11723 flags |= OPpEVAL_BYTES <<8; | |
11724 | |
11725 switch (PL_opargs[opnum] & OA_CLASS_MASK) { | |
11726 case OA_UNOP: | |
11727 case OA_BASEOP_OR_UNOP: | |
11728 case OA_FILESTATOP: | |
11729 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags); | |
11730 case OA_BASEOP: | |
11731 if (aop) { | |
11732 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0); | |
11733 op_free(aop); | |
11734 } | |
11735 return opnum == OP_RUNCV | |
11736 ? newPVOP(OP_RUNCV,0,NULL) | |
11737 : newOP(opnum,0); | |
11738 default: | |
11739 return op_convert_list(opnum,0,aop); | |
11740 } | |
11741 } | |
11742 NOT_REACHED; /* NOTREACHED */ | |
11743 return entersubop; | |
11744 } | |
11745 | |
11746 /* | |
11747 =for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p | |
11748 | |
11749 Retrieves the function that will be used to fix up a call to I<cv>. | |
11750 Specifically, the function is applied to an C<entersub> op tree for a | |
11751 subroutine call, not marked with C<&>, where the callee can be identified | |
11752 at compile time as I<cv>. | |
11753 | |
11754 The C-level function pointer is returned in I<*ckfun_p>, and an SV | |
11755 argument for it is returned in I<*ckobj_p>. The function is intended | |
11756 to be called in this manner: | |
11757 | |
11758 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p)); | |
11759 | |
11760 In this call, I<entersubop> is a pointer to the C<entersub> op, | |
11761 which may be replaced by the check function, and I<namegv> is a GV | |
11762 supplying the name that should be used by the check function to refer | |
11763 to the callee of the C<entersub> op if it needs to emit any diagnostics. | |
11764 It is permitted to apply the check function in non-standard situations, | |
11765 such as to a call to a different subroutine or to a method call. | |
11766 | |
11767 By default, the function is | |
11768 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>, | |
11769 and the SV parameter is I<cv> itself. This implements standard | |
11770 prototype processing. It can be changed, for a particular subroutine, | |
11771 by L</cv_set_call_checker>. | |
11772 | |
11773 =cut | |
11774 */ | |
11775 | |
11776 static void | |
11777 S_cv_get_call_checker(CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p, | |
11778 U8 *flagsp) | |
11779 { | |
11780 MAGIC *callmg; | |
11781 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL; | |
11782 if (callmg) { | |
11783 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr); | |
11784 *ckobj_p = callmg->mg_obj; | |
11785 if (flagsp) *flagsp = callmg->mg_flags; | |
11786 } else { | |
11787 *ckfun_p = Perl_ck_entersub_args_proto_or_list; | |
11788 *ckobj_p = (SV*)cv; | |
11789 if (flagsp) *flagsp = 0; | |
11790 } | |
11791 } | |
11792 | |
11793 void | |
11794 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p) | |
11795 { | |
11796 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER; | |
11797 PERL_UNUSED_CONTEXT; | |
11798 S_cv_get_call_checker(cv, ckfun_p, ckobj_p, NULL); | |
11799 } | |
11800 | |
11801 /* | |
11802 =for apidoc Am|void|cv_set_call_checker_flags|CV *cv|Perl_call_checker ckfun|SV *ckobj|U32 flags | |
11803 | |
11804 Sets the function that will be used to fix up a call to I<cv>. | |
11805 Specifically, the function is applied to an C<entersub> op tree for a | |
11806 subroutine call, not marked with C<&>, where the callee can be identified | |
11807 at compile time as I<cv>. | |
11808 | |
11809 The C-level function pointer is supplied in I<ckfun>, and an SV argument | |
11810 for it is supplied in I<ckobj>. The function should be defined like this: | |
11811 | |
11812 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj) | |
11813 | |
11814 It is intended to be called in this manner: | |
11815 | |
11816 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj); | |
11817 | |
11818 In this call, I<entersubop> is a pointer to the C<entersub> op, | |
11819 which may be replaced by the check function, and I<namegv> supplies | |
11820 the name that should be used by the check function to refer | |
11821 to the callee of the C<entersub> op if it needs to emit any diagnostics. | |
11822 It is permitted to apply the check function in non-standard situations, | |
11823 such as to a call to a different subroutine or to a method call. | |
11824 | |
11825 I<namegv> may not actually be a GV. For efficiency, perl may pass a | |
11826 CV or other SV instead. Whatever is passed can be used as the first | |
11827 argument to L</cv_name>. You can force perl to pass a GV by including | |
11828 C<CALL_CHECKER_REQUIRE_GV> in the I<flags>. | |
11829 | |
11830 The current setting for a particular CV can be retrieved by | |
11831 L</cv_get_call_checker>. | |
11832 | |
11833 =for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj | |
11834 | |
11835 The original form of L</cv_set_call_checker_flags>, which passes it the | |
11836 C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility. | |
11837 | |
11838 =cut | |
11839 */ | |
11840 | |
11841 void | |
11842 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj) | |
11843 { | |
11844 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER; | |
11845 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV); | |
11846 } | |
11847 | |
11848 void | |
11849 Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun, | |
11850 SV *ckobj, U32 flags) | |
11851 { | |
11852 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS; | |
11853 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) { | |
11854 if (SvMAGICAL((SV*)cv)) | |
11855 mg_free_type((SV*)cv, PERL_MAGIC_checkcall); | |
11856 } else { | |
11857 MAGIC *callmg; | |
11858 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0); | |
11859 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall); | |
11860 assert(callmg); | |
11861 if (callmg->mg_flags & MGf_REFCOUNTED) { | |
11862 SvREFCNT_dec(callmg->mg_obj); | |
11863 callmg->mg_flags &= ~MGf_REFCOUNTED; | |
11864 } | |
11865 callmg->mg_ptr = FPTR2DPTR(char *, ckfun); | |
11866 callmg->mg_obj = ckobj; | |
11867 if (ckobj != (SV*)cv) { | |
11868 SvREFCNT_inc_simple_void_NN(ckobj); | |
11869 callmg->mg_flags |= MGf_REFCOUNTED; | |
11870 } | |
11871 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV) | |
11872 | (U8)(flags & MGf_REQUIRE_GV) | MGf_COPY; | |
11873 } | |
11874 } | |
11875 | |
11876 static void | |
11877 S_entersub_alloc_targ(pTHX_ OP * const o) | |
11878 { | |
11879 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP); | |
11880 o->op_private |= OPpENTERSUB_HASTARG; | |
11881 } | |
11882 | |
11883 OP * | |
11884 Perl_ck_subr(pTHX_ OP *o) | |
11885 { | |
11886 OP *aop, *cvop; | |
11887 CV *cv; | |
11888 GV *namegv; | |
11889 SV **const_class = NULL; | |
11890 | |
11891 PERL_ARGS_ASSERT_CK_SUBR; | |
11892 | |
11893 aop = cUNOPx(o)->op_first; | |
11894 if (!OpHAS_SIBLING(aop)) | |
11895 aop = cUNOPx(aop)->op_first; | |
11896 aop = OpSIBLING(aop); | |
11897 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; | |
11898 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY); | |
11899 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL; | |
11900 | |
11901 o->op_private &= ~1; | |
11902 o->op_private |= (PL_hints & HINT_STRICT_REFS); | |
11903 if (PERLDB_SUB && PL_curstash != PL_debstash) | |
11904 o->op_private |= OPpENTERSUB_DB; | |
11905 switch (cvop->op_type) { | |
11906 case OP_RV2CV: | |
11907 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER); | |
11908 op_null(cvop); | |
11909 break; | |
11910 case OP_METHOD: | |
11911 case OP_METHOD_NAMED: | |
11912 case OP_METHOD_SUPER: | |
11913 case OP_METHOD_REDIR: | |
11914 case OP_METHOD_REDIR_SUPER: | |
11915 if (aop->op_type == OP_CONST) { | |
11916 aop->op_private &= ~OPpCONST_STRICT; | |
11917 const_class = &cSVOPx(aop)->op_sv; | |
11918 } | |
11919 else if (aop->op_type == OP_LIST) { | |
11920 OP * const sib = OpSIBLING(((UNOP*)aop)->op_first); | |
11921 if (sib && sib->op_type == OP_CONST) { | |
11922 sib->op_private &= ~OPpCONST_STRICT; | |
11923 const_class = &cSVOPx(sib)->op_sv; | |
11924 } | |
11925 } | |
11926 /* make class name a shared cow string to speedup method calls */ | |
11927 /* constant string might be replaced with object, f.e. bigint */ | |
11928 if (const_class && SvPOK(*const_class)) { | |
11929 STRLEN len; | |
11930 const char* str = SvPV(*const_class, len); | |
11931 if (len) { | |
11932 SV* const shared = newSVpvn_share( | |
11933 str, SvUTF8(*const_class) | |
11934 ? -(SSize_t)len : (SSize_t)len, | |
11935 0 | |
11936 ); | |
11937 if (SvREADONLY(*const_class)) | |
11938 SvREADONLY_on(shared); | |
11939 SvREFCNT_dec(*const_class); | |
11940 *const_class = shared; | |
11941 } | |
11942 } | |
11943 break; | |
11944 } | |
11945 | |
11946 if (!cv) { | |
11947 S_entersub_alloc_targ(aTHX_ o); | |
11948 return ck_entersub_args_list(o); | |
11949 } else { | |
11950 Perl_call_checker ckfun; | |
11951 SV *ckobj; | |
11952 U8 flags; | |
11953 S_cv_get_call_checker(cv, &ckfun, &ckobj, &flags); | |
11954 if (CvISXSUB(cv) || !CvROOT(cv)) | |
11955 S_entersub_alloc_targ(aTHX_ o); | |
11956 if (!namegv) { | |
11957 /* The original call checker API guarantees that a GV will be | |
11958 be provided with the right name. So, if the old API was | |
11959 used (or the REQUIRE_GV flag was passed), we have to reify | |
11960 the CV’s GV, unless this is an anonymous sub. This is not | |
11961 ideal for lexical subs, as its stringification will include | |
11962 the package. But it is the best we can do. */ | |
11963 if (flags & MGf_REQUIRE_GV) { | |
11964 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv))) | |
11965 namegv = CvGV(cv); | |
11966 } | |
11967 else namegv = MUTABLE_GV(cv); | |
11968 /* After a syntax error in a lexical sub, the cv that | |
11969 rv2cv_op_cv returns may be a nameless stub. */ | |
11970 if (!namegv) return ck_entersub_args_list(o); | |
11971 | |
11972 } | |
11973 return ckfun(aTHX_ o, namegv, ckobj); | |
11974 } | |
11975 } | |
11976 | |
11977 OP * | |
11978 Perl_ck_svconst(pTHX_ OP *o) | |
11979 { | |
11980 SV * const sv = cSVOPo->op_sv; | |
11981 PERL_ARGS_ASSERT_CK_SVCONST; | |
11982 PERL_UNUSED_CONTEXT; | |
11983 #ifdef PERL_OLD_COPY_ON_WRITE | |
11984 if (SvIsCOW(sv)) sv_force_normal(sv); | |
11985 #elif defined(PERL_NEW_COPY_ON_WRITE) | |
11986 /* Since the read-only flag may be used to protect a string buffer, we | |
11987 cannot do copy-on-write with existing read-only scalars that are not | |
11988 already copy-on-write scalars. To allow $_ = "hello" to do COW with | |
11989 that constant, mark the constant as COWable here, if it is not | |
11990 already read-only. */ | |
11991 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) { | |
11992 SvIsCOW_on(sv); | |
11993 CowREFCNT(sv) = 0; | |
11994 # ifdef PERL_DEBUG_READONLY_COW | |
11995 sv_buf_to_ro(sv); | |
11996 # endif | |
11997 } | |
11998 #endif | |
11999 SvREADONLY_on(sv); | |
12000 return o; | |
12001 } | |
12002 | |
12003 OP * | |
12004 Perl_ck_trunc(pTHX_ OP *o) | |
12005 { | |
12006 PERL_ARGS_ASSERT_CK_TRUNC; | |
12007 | |
12008 if (o->op_flags & OPf_KIDS) { | |
12009 SVOP *kid = (SVOP*)cUNOPo->op_first; | |
12010 | |
12011 if (kid->op_type == OP_NULL) | |
12012 kid = (SVOP*)OpSIBLING(kid); | |
12013 if (kid && kid->op_type == OP_CONST && | |
12014 (kid->op_private & OPpCONST_BARE) && | |
12015 !kid->op_folded) | |
12016 { | |
12017 o->op_flags |= OPf_SPECIAL; | |
12018 kid->op_private &= ~OPpCONST_STRICT; | |
12019 } | |
12020 } | |
12021 return ck_fun(o); | |
12022 } | |
12023 | |
12024 OP * | |
12025 Perl_ck_substr(pTHX_ OP *o) | |
12026 { | |
12027 PERL_ARGS_ASSERT_CK_SUBSTR; | |
12028 | |
12029 o = ck_fun(o); | |
12030 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) { | |
12031 OP *kid = cLISTOPo->op_first; | |
12032 | |
12033 if (kid->op_type == OP_NULL) | |
12034 kid = OpSIBLING(kid); | |
12035 if (kid) | |
12036 kid->op_flags |= OPf_MOD; | |
12037 | |
12038 } | |
12039 return o; | |
12040 } | |
12041 | |
12042 OP * | |
12043 Perl_ck_tell(pTHX_ OP *o) | |
12044 { | |
12045 PERL_ARGS_ASSERT_CK_TELL; | |
12046 o = ck_fun(o); | |
12047 if (o->op_flags & OPf_KIDS) { | |
12048 OP *kid = cLISTOPo->op_first; | |
12049 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid); | |
12050 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE; | |
12051 } | |
12052 return o; | |
12053 } | |
12054 | |
12055 OP * | |
12056 Perl_ck_each(pTHX_ OP *o) | |
12057 { | |
12058 dVAR; | |
12059 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL; | |
12060 const unsigned orig_type = o->op_type; | |
12061 const unsigned array_type = orig_type == OP_EACH ? OP_AEACH | |
12062 : orig_type == OP_KEYS ? OP_AKEYS : OP_AVALUES; | |
12063 const unsigned ref_type = orig_type == OP_EACH ? OP_REACH | |
12064 : orig_type == OP_KEYS ? OP_RKEYS : OP_RVALUES; | |
12065 | |
12066 PERL_ARGS_ASSERT_CK_EACH; | |
12067 | |
12068 if (kid) { | |
12069 switch (kid->op_type) { | |
12070 case OP_PADHV: | |
12071 case OP_RV2HV: | |
12072 break; | |
12073 case OP_PADAV: | |
12074 case OP_RV2AV: | |
12075 OpTYPE_set(o, array_type); | |
12076 break; | |
12077 case OP_CONST: | |
12078 if (kid->op_private == OPpCONST_BARE | |
12079 || !SvROK(cSVOPx_sv(kid)) | |
12080 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV | |
12081 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV ) | |
12082 ) | |
12083 /* we let ck_fun handle it */ | |
12084 break; | |
12085 default: | |
12086 OpTYPE_set(o, ref_type); | |
12087 scalar(kid); | |
12088 } | |
12089 } | |
12090 /* if treating as a reference, defer additional checks to runtime */ | |
12091 if (o->op_type == ref_type) { | |
12092 /* diag_listed_as: keys on reference is experimental */ | |
12093 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__AUTODEREF), | |
12094 "%s is experimental", PL_op_desc[ref_type]); | |
12095 return o; | |
12096 } | |
12097 return ck_fun(o); | |
12098 } | |
12099 | |
12100 OP * | |
12101 Perl_ck_length(pTHX_ OP *o) | |
12102 { | |
12103 PERL_ARGS_ASSERT_CK_LENGTH; | |
12104 | |
12105 o = ck_fun(o); | |
12106 | |
12107 if (ckWARN(WARN_SYNTAX)) { | |
12108 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL; | |
12109 | |
12110 if (kid) { | |
12111 SV *name = NULL; | |
12112 const bool hash = kid->op_type == OP_PADHV | |
12113 || kid->op_type == OP_RV2HV; | |
12114 switch (kid->op_type) { | |
12115 case OP_PADHV: | |
12116 case OP_PADAV: | |
12117 case OP_RV2HV: | |
12118 case OP_RV2AV: | |
12119 name = S_op_varname(aTHX_ kid); | |
12120 break; | |
12121 default: | |
12122 return o; | |
12123 } | |
12124 if (name) | |
12125 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
12126 "length() used on %"SVf" (did you mean \"scalar(%s%"SVf | |
12127 ")\"?)", | |
12128 SVfARG(name), hash ? "keys " : "", SVfARG(name) | |
12129 ); | |
12130 else if (hash) | |
12131 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */ | |
12132 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
12133 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)"); | |
12134 else | |
12135 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */ | |
12136 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), | |
12137 "length() used on @array (did you mean \"scalar(@array)\"?)"); | |
12138 } | |
12139 } | |
12140 | |
12141 return o; | |
12142 } | |
12143 | |
12144 /* Check for in place reverse and sort assignments like "@a = reverse @a" | |
12145 and modify the optree to make them work inplace */ | |
12146 | |
12147 STATIC void | |
12148 S_inplace_aassign(pTHX_ OP *o) { | |
12149 | |
12150 OP *modop, *modop_pushmark; | |
12151 OP *oright; | |
12152 OP *oleft, *oleft_pushmark; | |
12153 | |
12154 PERL_ARGS_ASSERT_INPLACE_AASSIGN; | |
12155 | |
12156 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID); | |
12157 | |
12158 assert(cUNOPo->op_first->op_type == OP_NULL); | |
12159 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first; | |
12160 assert(modop_pushmark->op_type == OP_PUSHMARK); | |
12161 modop = OpSIBLING(modop_pushmark); | |
12162 | |
12163 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE) | |
12164 return; | |
12165 | |
12166 /* no other operation except sort/reverse */ | |
12167 if (OpHAS_SIBLING(modop)) | |
12168 return; | |
12169 | |
12170 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK); | |
12171 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return; | |
12172 | |
12173 if (modop->op_flags & OPf_STACKED) { | |
12174 /* skip sort subroutine/block */ | |
12175 assert(oright->op_type == OP_NULL); | |
12176 oright = OpSIBLING(oright); | |
12177 } | |
12178 | |
12179 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL); | |
12180 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first; | |
12181 assert(oleft_pushmark->op_type == OP_PUSHMARK); | |
12182 oleft = OpSIBLING(oleft_pushmark); | |
12183 | |
12184 /* Check the lhs is an array */ | |
12185 if (!oleft || | |
12186 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV) | |
12187 || OpHAS_SIBLING(oleft) | |
12188 || (oleft->op_private & OPpLVAL_INTRO) | |
12189 ) | |
12190 return; | |
12191 | |
12192 /* Only one thing on the rhs */ | |
12193 if (OpHAS_SIBLING(oright)) | |
12194 return; | |
12195 | |
12196 /* check the array is the same on both sides */ | |
12197 if (oleft->op_type == OP_RV2AV) { | |
12198 if (oright->op_type != OP_RV2AV | |
12199 || !cUNOPx(oright)->op_first | |
12200 || cUNOPx(oright)->op_first->op_type != OP_GV | |
12201 || cUNOPx(oleft )->op_first->op_type != OP_GV | |
12202 || cGVOPx_gv(cUNOPx(oleft)->op_first) != | |
12203 cGVOPx_gv(cUNOPx(oright)->op_first) | |
12204 ) | |
12205 return; | |
12206 } | |
12207 else if (oright->op_type != OP_PADAV | |
12208 || oright->op_targ != oleft->op_targ | |
12209 ) | |
12210 return; | |
12211 | |
12212 /* This actually is an inplace assignment */ | |
12213 | |
12214 modop->op_private |= OPpSORT_INPLACE; | |
12215 | |
12216 /* transfer MODishness etc from LHS arg to RHS arg */ | |
12217 oright->op_flags = oleft->op_flags; | |
12218 | |
12219 /* remove the aassign op and the lhs */ | |
12220 op_null(o); | |
12221 op_null(oleft_pushmark); | |
12222 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first) | |
12223 op_null(cUNOPx(oleft)->op_first); | |
12224 op_null(oleft); | |
12225 } | |
12226 | |
12227 | |
12228 | |
12229 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start' | |
12230 * that potentially represent a series of one or more aggregate derefs | |
12231 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert | |
12232 * the whole chain to a single OP_MULTIDEREF op (maybe with a few | |
12233 * additional ops left in too). | |
12234 * | |
12235 * The caller will have already verified that the first few ops in the | |
12236 * chain following 'start' indicate a multideref candidate, and will have | |
12237 * set 'orig_o' to the point further on in the chain where the first index | |
12238 * expression (if any) begins. 'orig_action' specifies what type of | |
12239 * beginning has already been determined by the ops between start..orig_o | |
12240 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc). | |
12241 * | |
12242 * 'hints' contains any hints flags that need adding (currently just | |
12243 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller. | |
12244 */ | |
12245 | |
12246 void | |
12247 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints) | |
12248 { | |
12249 dVAR; | |
12250 int pass; | |
12251 UNOP_AUX_item *arg_buf = NULL; | |
12252 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */ | |
12253 int index_skip = -1; /* don't output index arg on this action */ | |
12254 | |
12255 /* similar to regex compiling, do two passes; the first pass | |
12256 * determines whether the op chain is convertible and calculates the | |
12257 * buffer size; the second pass populates the buffer and makes any | |
12258 * changes necessary to ops (such as moving consts to the pad on | |
12259 * threaded builds). | |
12260 * | |
12261 * NB: for things like Coverity, note that both passes take the same | |
12262 * path through the logic tree (except for 'if (pass)' bits), since | |
12263 * both passes are following the same op_next chain; and in | |
12264 * particular, if it would return early on the second pass, it would | |
12265 * already have returned early on the first pass. | |
12266 */ | |
12267 for (pass = 0; pass < 2; pass++) { | |
12268 OP *o = orig_o; | |
12269 UV action = orig_action; | |
12270 OP *first_elem_op = NULL; /* first seen aelem/helem */ | |
12271 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */ | |
12272 int action_count = 0; /* number of actions seen so far */ | |
12273 int action_ix = 0; /* action_count % (actions per IV) */ | |
12274 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */ | |
12275 bool is_last = FALSE; /* no more derefs to follow */ | |
12276 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */ | |
12277 UNOP_AUX_item *arg = arg_buf; | |
12278 UNOP_AUX_item *action_ptr = arg_buf; | |
12279 | |
12280 if (pass) | |
12281 action_ptr->uv = 0; | |
12282 arg++; | |
12283 | |
12284 switch (action) { | |
12285 case MDEREF_HV_gvsv_vivify_rv2hv_helem: | |
12286 case MDEREF_HV_gvhv_helem: | |
12287 next_is_hash = TRUE; | |
12288 /* FALLTHROUGH */ | |
12289 case MDEREF_AV_gvsv_vivify_rv2av_aelem: | |
12290 case MDEREF_AV_gvav_aelem: | |
12291 if (pass) { | |
12292 #ifdef USE_ITHREADS | |
12293 arg->pad_offset = cPADOPx(start)->op_padix; | |
12294 /* stop it being swiped when nulled */ | |
12295 cPADOPx(start)->op_padix = 0; | |
12296 #else | |
12297 arg->sv = cSVOPx(start)->op_sv; | |
12298 cSVOPx(start)->op_sv = NULL; | |
12299 #endif | |
12300 } | |
12301 arg++; | |
12302 break; | |
12303 | |
12304 case MDEREF_HV_padhv_helem: | |
12305 case MDEREF_HV_padsv_vivify_rv2hv_helem: | |
12306 next_is_hash = TRUE; | |
12307 /* FALLTHROUGH */ | |
12308 case MDEREF_AV_padav_aelem: | |
12309 case MDEREF_AV_padsv_vivify_rv2av_aelem: | |
12310 if (pass) { | |
12311 arg->pad_offset = start->op_targ; | |
12312 /* we skip setting op_targ = 0 for now, since the intact | |
12313 * OP_PADXV is needed by S_check_hash_fields_and_hekify */ | |
12314 reset_start_targ = TRUE; | |
12315 } | |
12316 arg++; | |
12317 break; | |
12318 | |
12319 case MDEREF_HV_pop_rv2hv_helem: | |
12320 next_is_hash = TRUE; | |
12321 /* FALLTHROUGH */ | |
12322 case MDEREF_AV_pop_rv2av_aelem: | |
12323 break; | |
12324 | |
12325 default: | |
12326 NOT_REACHED; /* NOTREACHED */ | |
12327 return; | |
12328 } | |
12329 | |
12330 while (!is_last) { | |
12331 /* look for another (rv2av/hv; get index; | |
12332 * aelem/helem/exists/delele) sequence */ | |
12333 | |
12334 OP *kid; | |
12335 bool is_deref; | |
12336 bool ok; | |
12337 UV index_type = MDEREF_INDEX_none; | |
12338 | |
12339 if (action_count) { | |
12340 /* if this is not the first lookup, consume the rv2av/hv */ | |
12341 | |
12342 /* for N levels of aggregate lookup, we normally expect | |
12343 * that the first N-1 [ah]elem ops will be flagged as | |
12344 * /DEREF (so they autovivifiy if necessary), and the last | |
12345 * lookup op not to be. | |
12346 * For other things (like @{$h{k1}{k2}}) extra scope or | |
12347 * leave ops can appear, so abandon the effort in that | |
12348 * case */ | |
12349 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV) | |
12350 return; | |
12351 | |
12352 /* rv2av or rv2hv sKR/1 */ | |
12353 | |
12354 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS | |
12355 |OPf_REF|OPf_MOD|OPf_SPECIAL))); | |
12356 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF)) | |
12357 return; | |
12358 | |
12359 /* at this point, we wouldn't expect any of these | |
12360 * possible private flags: | |
12361 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO | |
12362 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only) | |
12363 */ | |
12364 ASSUME(!(o->op_private & | |
12365 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING))); | |
12366 | |
12367 hints = (o->op_private & OPpHINT_STRICT_REFS); | |
12368 | |
12369 /* make sure the type of the previous /DEREF matches the | |
12370 * type of the next lookup */ | |
12371 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV)); | |
12372 top_op = o; | |
12373 | |
12374 action = next_is_hash | |
12375 ? MDEREF_HV_vivify_rv2hv_helem | |
12376 : MDEREF_AV_vivify_rv2av_aelem; | |
12377 o = o->op_next; | |
12378 } | |
12379 | |
12380 /* if this is the second pass, and we're at the depth where | |
12381 * previously we encountered a non-simple index expression, | |
12382 * stop processing the index at this point */ | |
12383 if (action_count != index_skip) { | |
12384 | |
12385 /* look for one or more simple ops that return an array | |
12386 * index or hash key */ | |
12387 | |
12388 switch (o->op_type) { | |
12389 case OP_PADSV: | |
12390 /* it may be a lexical var index */ | |
12391 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS | |
12392 |OPf_REF|OPf_MOD|OPf_SPECIAL))); | |
12393 ASSUME(!(o->op_private & | |
12394 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO))); | |
12395 | |
12396 if ( OP_GIMME(o,0) == G_SCALAR | |
12397 && !(o->op_flags & (OPf_REF|OPf_MOD)) | |
12398 && o->op_private == 0) | |
12399 { | |
12400 if (pass) | |
12401 arg->pad_offset = o->op_targ; | |
12402 arg++; | |
12403 index_type = MDEREF_INDEX_padsv; | |
12404 o = o->op_next; | |
12405 } | |
12406 break; | |
12407 | |
12408 case OP_CONST: | |
12409 if (next_is_hash) { | |
12410 /* it's a constant hash index */ | |
12411 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK))) | |
12412 /* "use constant foo => FOO; $h{+foo}" for | |
12413 * some weird FOO, can leave you with constants | |
12414 * that aren't simple strings. It's not worth | |
12415 * the extra hassle for those edge cases */ | |
12416 break; | |
12417 | |
12418 if (pass) { | |
12419 UNOP *rop = NULL; | |
12420 OP * helem_op = o->op_next; | |
12421 | |
12422 ASSUME( helem_op->op_type == OP_HELEM | |
12423 || helem_op->op_type == OP_NULL); | |
12424 if (helem_op->op_type == OP_HELEM) { | |
12425 rop = (UNOP*)(((BINOP*)helem_op)->op_first); | |
12426 if ( helem_op->op_private & OPpLVAL_INTRO | |
12427 || rop->op_type != OP_RV2HV | |
12428 ) | |
12429 rop = NULL; | |
12430 } | |
12431 S_check_hash_fields_and_hekify(aTHX_ rop, cSVOPo); | |
12432 | |
12433 #ifdef USE_ITHREADS | |
12434 /* Relocate sv to the pad for thread safety */ | |
12435 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ); | |
12436 arg->pad_offset = o->op_targ; | |
12437 o->op_targ = 0; | |
12438 #else | |
12439 arg->sv = cSVOPx_sv(o); | |
12440 #endif | |
12441 } | |
12442 } | |
12443 else { | |
12444 /* it's a constant array index */ | |
12445 IV iv; | |
12446 SV *ix_sv = cSVOPo->op_sv; | |
12447 if (!SvIOK(ix_sv)) | |
12448 break; | |
12449 iv = SvIV(ix_sv); | |
12450 | |
12451 if ( action_count == 0 | |
12452 && iv >= -128 | |
12453 && iv <= 127 | |
12454 && ( action == MDEREF_AV_padav_aelem | |
12455 || action == MDEREF_AV_gvav_aelem) | |
12456 ) | |
12457 maybe_aelemfast = TRUE; | |
12458 | |
12459 if (pass) { | |
12460 arg->iv = iv; | |
12461 SvREFCNT_dec_NN(cSVOPo->op_sv); | |
12462 } | |
12463 } | |
12464 if (pass) | |
12465 /* we've taken ownership of the SV */ | |
12466 cSVOPo->op_sv = NULL; | |
12467 arg++; | |
12468 index_type = MDEREF_INDEX_const; | |
12469 o = o->op_next; | |
12470 break; | |
12471 | |
12472 case OP_GV: | |
12473 /* it may be a package var index */ | |
12474 | |
12475 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_SPECIAL))); | |
12476 ASSUME(!(o->op_private & ~(OPpEARLY_CV))); | |
12477 if ( (o->op_flags &~ OPf_SPECIAL) != OPf_WANT_SCALAR | |
12478 || o->op_private != 0 | |
12479 ) | |
12480 break; | |
12481 | |
12482 kid = o->op_next; | |
12483 if (kid->op_type != OP_RV2SV) | |
12484 break; | |
12485 | |
12486 ASSUME(!(kid->op_flags & | |
12487 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF | |
12488 |OPf_SPECIAL|OPf_PARENS))); | |
12489 ASSUME(!(kid->op_private & | |
12490 ~(OPpARG1_MASK | |
12491 |OPpHINT_STRICT_REFS|OPpOUR_INTRO | |
12492 |OPpDEREF|OPpLVAL_INTRO))); | |
12493 if( (kid->op_flags &~ OPf_PARENS) | |
12494 != (OPf_WANT_SCALAR|OPf_KIDS) | |
12495 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS)) | |
12496 ) | |
12497 break; | |
12498 | |
12499 if (pass) { | |
12500 #ifdef USE_ITHREADS | |
12501 arg->pad_offset = cPADOPx(o)->op_padix; | |
12502 /* stop it being swiped when nulled */ | |
12503 cPADOPx(o)->op_padix = 0; | |
12504 #else | |
12505 arg->sv = cSVOPx(o)->op_sv; | |
12506 cSVOPo->op_sv = NULL; | |
12507 #endif | |
12508 } | |
12509 arg++; | |
12510 index_type = MDEREF_INDEX_gvsv; | |
12511 o = kid->op_next; | |
12512 break; | |
12513 | |
12514 } /* switch */ | |
12515 } /* action_count != index_skip */ | |
12516 | |
12517 action |= index_type; | |
12518 | |
12519 | |
12520 /* at this point we have either: | |
12521 * * detected what looks like a simple index expression, | |
12522 * and expect the next op to be an [ah]elem, or | |
12523 * an nulled [ah]elem followed by a delete or exists; | |
12524 * * found a more complex expression, so something other | |
12525 * than the above follows. | |
12526 */ | |
12527 | |
12528 /* possibly an optimised away [ah]elem (where op_next is | |
12529 * exists or delete) */ | |
12530 if (o->op_type == OP_NULL) | |
12531 o = o->op_next; | |
12532 | |
12533 /* at this point we're looking for an OP_AELEM, OP_HELEM, | |
12534 * OP_EXISTS or OP_DELETE */ | |
12535 | |
12536 /* if something like arybase (a.k.a $[ ) is in scope, | |
12537 * abandon optimisation attempt */ | |
12538 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM) | |
12539 && PL_check[o->op_type] != Perl_ck_null) | |
12540 return; | |
12541 | |
12542 if ( o->op_type != OP_AELEM | |
12543 || (o->op_private & | |
12544 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) | |
12545 ) | |
12546 maybe_aelemfast = FALSE; | |
12547 | |
12548 /* look for aelem/helem/exists/delete. If it's not the last elem | |
12549 * lookup, it *must* have OPpDEREF_AV/HV, but not many other | |
12550 * flags; if it's the last, then it mustn't have | |
12551 * OPpDEREF_AV/HV, but may have lots of other flags, like | |
12552 * OPpLVAL_INTRO etc | |
12553 */ | |
12554 | |
12555 if ( index_type == MDEREF_INDEX_none | |
12556 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM | |
12557 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE) | |
12558 ) | |
12559 ok = FALSE; | |
12560 else { | |
12561 /* we have aelem/helem/exists/delete with valid simple index */ | |
12562 | |
12563 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM) | |
12564 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV | |
12565 || (o->op_private & OPpDEREF) == OPpDEREF_HV); | |
12566 | |
12567 if (is_deref) { | |
12568 ASSUME(!(o->op_flags & | |
12569 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS))); | |
12570 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF))); | |
12571 | |
12572 ok = (o->op_flags &~ OPf_PARENS) | |
12573 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD) | |
12574 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK)); | |
12575 } | |
12576 else if (o->op_type == OP_EXISTS) { | |
12577 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS | |
12578 |OPf_REF|OPf_MOD|OPf_SPECIAL))); | |
12579 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB))); | |
12580 ok = !(o->op_private & ~OPpARG1_MASK); | |
12581 } | |
12582 else if (o->op_type == OP_DELETE) { | |
12583 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS | |
12584 |OPf_REF|OPf_MOD|OPf_SPECIAL))); | |
12585 ASSUME(!(o->op_private & | |
12586 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO))); | |
12587 /* don't handle slices or 'local delete'; the latter | |
12588 * is fairly rare, and has a complex runtime */ | |
12589 ok = !(o->op_private & ~OPpARG1_MASK); | |
12590 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM)) | |
12591 /* skip handling run-tome error */ | |
12592 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL)); | |
12593 } | |
12594 else { | |
12595 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM); | |
12596 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD | |
12597 |OPf_PARENS|OPf_REF|OPf_SPECIAL))); | |
12598 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB | |
12599 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO))); | |
12600 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV; | |
12601 } | |
12602 } | |
12603 | |
12604 if (ok) { | |
12605 if (!first_elem_op) | |
12606 first_elem_op = o; | |
12607 top_op = o; | |
12608 if (is_deref) { | |
12609 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV); | |
12610 o = o->op_next; | |
12611 } | |
12612 else { | |
12613 is_last = TRUE; | |
12614 action |= MDEREF_FLAG_last; | |
12615 } | |
12616 } | |
12617 else { | |
12618 /* at this point we have something that started | |
12619 * promisingly enough (with rv2av or whatever), but failed | |
12620 * to find a simple index followed by an | |
12621 * aelem/helem/exists/delete. If this is the first action, | |
12622 * give up; but if we've already seen at least one | |
12623 * aelem/helem, then keep them and add a new action with | |
12624 * MDEREF_INDEX_none, which causes it to do the vivify | |
12625 * from the end of the previous lookup, and do the deref, | |
12626 * but stop at that point. So $a[0][expr] will do one | |
12627 * av_fetch, vivify and deref, then continue executing at | |
12628 * expr */ | |
12629 if (!action_count) | |
12630 return; | |
12631 is_last = TRUE; | |
12632 index_skip = action_count; | |
12633 action |= MDEREF_FLAG_last; | |
12634 } | |
12635 | |
12636 if (pass) | |
12637 action_ptr->uv |= (action << (action_ix * MDEREF_SHIFT)); | |
12638 action_ix++; | |
12639 action_count++; | |
12640 /* if there's no space for the next action, create a new slot | |
12641 * for it *before* we start adding args for that action */ | |
12642 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) { | |
12643 action_ptr = arg; | |
12644 if (pass) | |
12645 arg->uv = 0; | |
12646 arg++; | |
12647 action_ix = 0; | |
12648 } | |
12649 } /* while !is_last */ | |
12650 | |
12651 /* success! */ | |
12652 | |
12653 if (pass) { | |
12654 OP *mderef; | |
12655 OP *p, *q; | |
12656 | |
12657 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf); | |
12658 if (index_skip == -1) { | |
12659 mderef->op_flags = o->op_flags | |
12660 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0)); | |
12661 if (o->op_type == OP_EXISTS) | |
12662 mderef->op_private = OPpMULTIDEREF_EXISTS; | |
12663 else if (o->op_type == OP_DELETE) | |
12664 mderef->op_private = OPpMULTIDEREF_DELETE; | |
12665 else | |
12666 mderef->op_private = o->op_private | |
12667 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO); | |
12668 } | |
12669 /* accumulate strictness from every level (although I don't think | |
12670 * they can actually vary) */ | |
12671 mderef->op_private |= hints; | |
12672 | |
12673 /* integrate the new multideref op into the optree and the | |
12674 * op_next chain. | |
12675 * | |
12676 * In general an op like aelem or helem has two child | |
12677 * sub-trees: the aggregate expression (a_expr) and the | |
12678 * index expression (i_expr): | |
12679 * | |
12680 * aelem | |
12681 * | | |
12682 * a_expr - i_expr | |
12683 * | |
12684 * The a_expr returns an AV or HV, while the i-expr returns an | |
12685 * index. In general a multideref replaces most or all of a | |
12686 * multi-level tree, e.g. | |
12687 * | |
12688 * exists | |
12689 * | | |
12690 * ex-aelem | |
12691 * | | |
12692 * rv2av - i_expr1 | |
12693 * | | |
12694 * helem | |
12695 * | | |
12696 * rv2hv - i_expr2 | |
12697 * | | |
12698 * aelem | |
12699 * | | |
12700 * a_expr - i_expr3 | |
12701 * | |
12702 * With multideref, all the i_exprs will be simple vars or | |
12703 * constants, except that i_expr1 may be arbitrary in the case | |
12704 * of MDEREF_INDEX_none. | |
12705 * | |
12706 * The bottom-most a_expr will be either: | |
12707 * 1) a simple var (so padXv or gv+rv2Xv); | |
12708 * 2) a simple scalar var dereferenced (e.g. $r->[0]): | |
12709 * so a simple var with an extra rv2Xv; | |
12710 * 3) or an arbitrary expression. | |
12711 * | |
12712 * 'start', the first op in the execution chain, will point to | |
12713 * 1),2): the padXv or gv op; | |
12714 * 3): the rv2Xv which forms the last op in the a_expr | |
12715 * execution chain, and the top-most op in the a_expr | |
12716 * subtree. | |
12717 * | |
12718 * For all cases, the 'start' node is no longer required, | |
12719 * but we can't free it since one or more external nodes | |
12720 * may point to it. E.g. consider | |
12721 * $h{foo} = $a ? $b : $c | |
12722 * Here, both the op_next and op_other branches of the | |
12723 * cond_expr point to the gv[*h] of the hash expression, so | |
12724 * we can't free the 'start' op. | |
12725 * | |
12726 * For expr->[...], we need to save the subtree containing the | |
12727 * expression; for the other cases, we just need to save the | |
12728 * start node. | |
12729 * So in all cases, we null the start op and keep it around by | |
12730 * making it the child of the multideref op; for the expr-> | |
12731 * case, the expr will be a subtree of the start node. | |
12732 * | |
12733 * So in the simple 1,2 case the optree above changes to | |
12734 * | |
12735 * ex-exists | |
12736 * | | |
12737 * multideref | |
12738 * | | |
12739 * ex-gv (or ex-padxv) | |
12740 * | |
12741 * with the op_next chain being | |
12742 * | |
12743 * -> ex-gv -> multideref -> op-following-ex-exists -> | |
12744 * | |
12745 * In the 3 case, we have | |
12746 * | |
12747 * ex-exists | |
12748 * | | |
12749 * multideref | |
12750 * | | |
12751 * ex-rv2xv | |
12752 * | | |
12753 * rest-of-a_expr | |
12754 * subtree | |
12755 * | |
12756 * and | |
12757 * | |
12758 * -> rest-of-a_expr subtree -> | |
12759 * ex-rv2xv -> multideref -> op-following-ex-exists -> | |
12760 * | |
12761 * | |
12762 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none, | |
12763 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the | |
12764 * multideref attached as the child, e.g. | |
12765 * | |
12766 * exists | |
12767 * | | |
12768 * ex-aelem | |
12769 * | | |
12770 * ex-rv2av - i_expr1 | |
12771 * | | |
12772 * multideref | |
12773 * | | |
12774 * ex-whatever | |
12775 * | |
12776 */ | |
12777 | |
12778 /* if we free this op, don't free the pad entry */ | |
12779 if (reset_start_targ) | |
12780 start->op_targ = 0; | |
12781 | |
12782 | |
12783 /* Cut the bit we need to save out of the tree and attach to | |
12784 * the multideref op, then free the rest of the tree */ | |
12785 | |
12786 /* find parent of node to be detached (for use by splice) */ | |
12787 p = first_elem_op; | |
12788 if ( orig_action == MDEREF_AV_pop_rv2av_aelem | |
12789 || orig_action == MDEREF_HV_pop_rv2hv_helem) | |
12790 { | |
12791 /* there is an arbitrary expression preceding us, e.g. | |
12792 * expr->[..]? so we need to save the 'expr' subtree */ | |
12793 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE) | |
12794 p = cUNOPx(p)->op_first; | |
12795 ASSUME( start->op_type == OP_RV2AV | |
12796 || start->op_type == OP_RV2HV); | |
12797 } | |
12798 else { | |
12799 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem | |
12800 * above for exists/delete. */ | |
12801 while ( (p->op_flags & OPf_KIDS) | |
12802 && cUNOPx(p)->op_first != start | |
12803 ) | |
12804 p = cUNOPx(p)->op_first; | |
12805 } | |
12806 ASSUME(cUNOPx(p)->op_first == start); | |
12807 | |
12808 /* detach from main tree, and re-attach under the multideref */ | |
12809 op_sibling_splice(mderef, NULL, 0, | |
12810 op_sibling_splice(p, NULL, 1, NULL)); | |
12811 op_null(start); | |
12812 | |
12813 start->op_next = mderef; | |
12814 | |
12815 mderef->op_next = index_skip == -1 ? o->op_next : o; | |
12816 | |
12817 /* excise and free the original tree, and replace with | |
12818 * the multideref op */ | |
12819 p = op_sibling_splice(top_op, NULL, -1, mderef); | |
12820 while (p) { | |
12821 q = OpSIBLING(p); | |
12822 op_free(p); | |
12823 p = q; | |
12824 } | |
12825 op_null(top_op); | |
12826 } | |
12827 else { | |
12828 Size_t size = arg - arg_buf; | |
12829 | |
12830 if (maybe_aelemfast && action_count == 1) | |
12831 return; | |
12832 | |
12833 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc( | |
12834 sizeof(UNOP_AUX_item) * (size + 1)); | |
12835 /* for dumping etc: store the length in a hidden first slot; | |
12836 * we set the op_aux pointer to the second slot */ | |
12837 arg_buf->uv = size; | |
12838 arg_buf++; | |
12839 } | |
12840 } /* for (pass = ...) */ | |
12841 } | |
12842 | |
12843 | |
12844 | |
12845 /* mechanism for deferring recursion in rpeep() */ | |
12846 | |
12847 #define MAX_DEFERRED 4 | |
12848 | |
12849 #define DEFER(o) \ | |
12850 STMT_START { \ | |
12851 if (defer_ix == (MAX_DEFERRED-1)) { \ | |
12852 OP **defer = defer_queue[defer_base]; \ | |
12853 CALL_RPEEP(*defer); \ | |
12854 S_prune_chain_head(defer); \ | |
12855 defer_base = (defer_base + 1) % MAX_DEFERRED; \ | |
12856 defer_ix--; \ | |
12857 } \ | |
12858 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \ | |
12859 } STMT_END | |
12860 | |
12861 #define IS_AND_OP(o) (o->op_type == OP_AND) | |
12862 #define IS_OR_OP(o) (o->op_type == OP_OR) | |
12863 | |
12864 | |
12865 /* A peephole optimizer. We visit the ops in the order they're to execute. | |
12866 * See the comments at the top of this file for more details about when | |
12867 * peep() is called */ | |
12868 | |
12869 void | |
12870 Perl_rpeep(pTHX_ OP *o) | |
12871 { | |
12872 dVAR; | |
12873 OP* oldop = NULL; | |
12874 OP* oldoldop = NULL; | |
12875 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */ | |
12876 int defer_base = 0; | |
12877 int defer_ix = -1; | |
12878 OP *fop; | |
12879 OP *sop; | |
12880 | |
12881 if (!o || o->op_opt) | |
12882 return; | |
12883 ENTER; | |
12884 SAVEOP(); | |
12885 SAVEVPTR(PL_curcop); | |
12886 for (;; o = o->op_next) { | |
12887 if (o && o->op_opt) | |
12888 o = NULL; | |
12889 if (!o) { | |
12890 while (defer_ix >= 0) { | |
12891 OP **defer = | |
12892 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED]; | |
12893 CALL_RPEEP(*defer); | |
12894 S_prune_chain_head(defer); | |
12895 } | |
12896 break; | |
12897 } | |
12898 | |
12899 redo: | |
12900 /* By default, this op has now been optimised. A couple of cases below | |
12901 clear this again. */ | |
12902 o->op_opt = 1; | |
12903 PL_op = o; | |
12904 | |
12905 /* look for a series of 1 or more aggregate derefs, e.g. | |
12906 * $a[1]{foo}[$i]{$k} | |
12907 * and replace with a single OP_MULTIDEREF op. | |
12908 * Each index must be either a const, or a simple variable, | |
12909 * | |
12910 * First, look for likely combinations of starting ops, | |
12911 * corresponding to (global and lexical variants of) | |
12912 * $a[...] $h{...} | |
12913 * $r->[...] $r->{...} | |
12914 * (preceding expression)->[...] | |
12915 * (preceding expression)->{...} | |
12916 * and if so, call maybe_multideref() to do a full inspection | |
12917 * of the op chain and if appropriate, replace with an | |
12918 * OP_MULTIDEREF | |
12919 */ | |
12920 { | |
12921 UV action; | |
12922 OP *o2 = o; | |
12923 U8 hints = 0; | |
12924 | |
12925 switch (o2->op_type) { | |
12926 case OP_GV: | |
12927 /* $pkg[..] : gv[*pkg] | |
12928 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */ | |
12929 | |
12930 /* Fail if there are new op flag combinations that we're | |
12931 * not aware of, rather than: | |
12932 * * silently failing to optimise, or | |
12933 * * silently optimising the flag away. | |
12934 * If this ASSUME starts failing, examine what new flag | |
12935 * has been added to the op, and decide whether the | |
12936 * optimisation should still occur with that flag, then | |
12937 * update the code accordingly. This applies to all the | |
12938 * other ASSUMEs in the block of code too. | |
12939 */ | |
12940 ASSUME(!(o2->op_flags & | |
12941 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL))); | |
12942 ASSUME(!(o2->op_private & ~OPpEARLY_CV)); | |
12943 | |
12944 o2 = o2->op_next; | |
12945 | |
12946 if (o2->op_type == OP_RV2AV) { | |
12947 action = MDEREF_AV_gvav_aelem; | |
12948 goto do_deref; | |
12949 } | |
12950 | |
12951 if (o2->op_type == OP_RV2HV) { | |
12952 action = MDEREF_HV_gvhv_helem; | |
12953 goto do_deref; | |
12954 } | |
12955 | |
12956 if (o2->op_type != OP_RV2SV) | |
12957 break; | |
12958 | |
12959 /* at this point we've seen gv,rv2sv, so the only valid | |
12960 * construct left is $pkg->[] or $pkg->{} */ | |
12961 | |
12962 ASSUME(!(o2->op_flags & OPf_STACKED)); | |
12963 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) | |
12964 != (OPf_WANT_SCALAR|OPf_MOD)) | |
12965 break; | |
12966 | |
12967 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS | |
12968 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO))); | |
12969 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO)) | |
12970 break; | |
12971 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV | |
12972 && (o2->op_private & OPpDEREF) != OPpDEREF_HV) | |
12973 break; | |
12974 | |
12975 o2 = o2->op_next; | |
12976 if (o2->op_type == OP_RV2AV) { | |
12977 action = MDEREF_AV_gvsv_vivify_rv2av_aelem; | |
12978 goto do_deref; | |
12979 } | |
12980 if (o2->op_type == OP_RV2HV) { | |
12981 action = MDEREF_HV_gvsv_vivify_rv2hv_helem; | |
12982 goto do_deref; | |
12983 } | |
12984 break; | |
12985 | |
12986 case OP_PADSV: | |
12987 /* $lex->[...]: padsv[$lex] sM/DREFAV */ | |
12988 | |
12989 ASSUME(!(o2->op_flags & | |
12990 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL))); | |
12991 if ((o2->op_flags & | |
12992 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) | |
12993 != (OPf_WANT_SCALAR|OPf_MOD)) | |
12994 break; | |
12995 | |
12996 ASSUME(!(o2->op_private & | |
12997 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO))); | |
12998 /* skip if state or intro, or not a deref */ | |
12999 if ( o2->op_private != OPpDEREF_AV | |
13000 && o2->op_private != OPpDEREF_HV) | |
13001 break; | |
13002 | |
13003 o2 = o2->op_next; | |
13004 if (o2->op_type == OP_RV2AV) { | |
13005 action = MDEREF_AV_padsv_vivify_rv2av_aelem; | |
13006 goto do_deref; | |
13007 } | |
13008 if (o2->op_type == OP_RV2HV) { | |
13009 action = MDEREF_HV_padsv_vivify_rv2hv_helem; | |
13010 goto do_deref; | |
13011 } | |
13012 break; | |
13013 | |
13014 case OP_PADAV: | |
13015 case OP_PADHV: | |
13016 /* $lex[..]: padav[@lex:1,2] sR * | |
13017 * or $lex{..}: padhv[%lex:1,2] sR */ | |
13018 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS| | |
13019 OPf_REF|OPf_SPECIAL))); | |
13020 if ((o2->op_flags & | |
13021 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) | |
13022 != (OPf_WANT_SCALAR|OPf_REF)) | |
13023 break; | |
13024 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF)) | |
13025 break; | |
13026 /* OPf_PARENS isn't currently used in this case; | |
13027 * if that changes, let us know! */ | |
13028 ASSUME(!(o2->op_flags & OPf_PARENS)); | |
13029 | |
13030 /* at this point, we wouldn't expect any of the remaining | |
13031 * possible private flags: | |
13032 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL, | |
13033 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB | |
13034 * | |
13035 * OPpSLICEWARNING shouldn't affect runtime | |
13036 */ | |
13037 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING))); | |
13038 | |
13039 action = o2->op_type == OP_PADAV | |
13040 ? MDEREF_AV_padav_aelem | |
13041 : MDEREF_HV_padhv_helem; | |
13042 o2 = o2->op_next; | |
13043 S_maybe_multideref(aTHX_ o, o2, action, 0); | |
13044 break; | |
13045 | |
13046 | |
13047 case OP_RV2AV: | |
13048 case OP_RV2HV: | |
13049 action = o2->op_type == OP_RV2AV | |
13050 ? MDEREF_AV_pop_rv2av_aelem | |
13051 : MDEREF_HV_pop_rv2hv_helem; | |
13052 /* FALLTHROUGH */ | |
13053 do_deref: | |
13054 /* (expr)->[...]: rv2av sKR/1; | |
13055 * (expr)->{...}: rv2hv sKR/1; */ | |
13056 | |
13057 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV); | |
13058 | |
13059 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS | |
13060 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL))); | |
13061 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF)) | |
13062 break; | |
13063 | |
13064 /* at this point, we wouldn't expect any of these | |
13065 * possible private flags: | |
13066 * OPpMAYBE_LVSUB, OPpLVAL_INTRO | |
13067 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only) | |
13068 */ | |
13069 ASSUME(!(o2->op_private & | |
13070 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING | |
13071 |OPpOUR_INTRO))); | |
13072 hints |= (o2->op_private & OPpHINT_STRICT_REFS); | |
13073 | |
13074 o2 = o2->op_next; | |
13075 | |
13076 S_maybe_multideref(aTHX_ o, o2, action, hints); | |
13077 break; | |
13078 | |
13079 default: | |
13080 break; | |
13081 } | |
13082 } | |
13083 | |
13084 | |
13085 switch (o->op_type) { | |
13086 case OP_DBSTATE: | |
13087 PL_curcop = ((COP*)o); /* for warnings */ | |
13088 break; | |
13089 case OP_NEXTSTATE: | |
13090 PL_curcop = ((COP*)o); /* for warnings */ | |
13091 | |
13092 /* Optimise a "return ..." at the end of a sub to just be "...". | |
13093 * This saves 2 ops. Before: | |
13094 * 1 <;> nextstate(main 1 -e:1) v ->2 | |
13095 * 4 <@> return K ->5 | |
13096 * 2 <0> pushmark s ->3 | |
13097 * - <1> ex-rv2sv sK/1 ->4 | |
13098 * 3 <#> gvsv[*cat] s ->4 | |
13099 * | |
13100 * After: | |
13101 * - <@> return K ->- | |
13102 * - <0> pushmark s ->2 | |
13103 * - <1> ex-rv2sv sK/1 ->- | |
13104 * 2 <$> gvsv(*cat) s ->3 | |
13105 */ | |
13106 { | |
13107 OP *next = o->op_next; | |
13108 OP *sibling = OpSIBLING(o); | |
13109 if ( OP_TYPE_IS(next, OP_PUSHMARK) | |
13110 && OP_TYPE_IS(sibling, OP_RETURN) | |
13111 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ) | |
13112 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB) | |
13113 ||OP_TYPE_IS(sibling->op_next->op_next, | |
13114 OP_LEAVESUBLV)) | |
13115 && cUNOPx(sibling)->op_first == next | |
13116 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next | |
13117 && next->op_next | |
13118 ) { | |
13119 /* Look through the PUSHMARK's siblings for one that | |
13120 * points to the RETURN */ | |
13121 OP *top = OpSIBLING(next); | |
13122 while (top && top->op_next) { | |
13123 if (top->op_next == sibling) { | |
13124 top->op_next = sibling->op_next; | |
13125 o->op_next = next->op_next; | |
13126 break; | |
13127 } | |
13128 top = OpSIBLING(top); | |
13129 } | |
13130 } | |
13131 } | |
13132 | |
13133 /* Optimise 'my $x; my $y;' into 'my ($x, $y);' | |
13134 * | |
13135 * This latter form is then suitable for conversion into padrange | |
13136 * later on. Convert: | |
13137 * | |
13138 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3 | |
13139 * | |
13140 * into: | |
13141 * | |
13142 * nextstate1 -> listop -> nextstate3 | |
13143 * / \ | |
13144 * pushmark -> padop1 -> padop2 | |
13145 */ | |
13146 if (o->op_next && ( | |
13147 o->op_next->op_type == OP_PADSV | |
13148 || o->op_next->op_type == OP_PADAV | |
13149 || o->op_next->op_type == OP_PADHV | |
13150 ) | |
13151 && !(o->op_next->op_private & ~OPpLVAL_INTRO) | |
13152 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE | |
13153 && o->op_next->op_next->op_next && ( | |
13154 o->op_next->op_next->op_next->op_type == OP_PADSV | |
13155 || o->op_next->op_next->op_next->op_type == OP_PADAV | |
13156 || o->op_next->op_next->op_next->op_type == OP_PADHV | |
13157 ) | |
13158 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO) | |
13159 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE | |
13160 && (!CopLABEL((COP*)o)) /* Don't mess with labels */ | |
13161 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */ | |
13162 ) { | |
13163 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm; | |
13164 | |
13165 pad1 = o->op_next; | |
13166 ns2 = pad1->op_next; | |
13167 pad2 = ns2->op_next; | |
13168 ns3 = pad2->op_next; | |
13169 | |
13170 /* we assume here that the op_next chain is the same as | |
13171 * the op_sibling chain */ | |
13172 assert(OpSIBLING(o) == pad1); | |
13173 assert(OpSIBLING(pad1) == ns2); | |
13174 assert(OpSIBLING(ns2) == pad2); | |
13175 assert(OpSIBLING(pad2) == ns3); | |
13176 | |
13177 /* excise and delete ns2 */ | |
13178 op_sibling_splice(NULL, pad1, 1, NULL); | |
13179 op_free(ns2); | |
13180 | |
13181 /* excise pad1 and pad2 */ | |
13182 op_sibling_splice(NULL, o, 2, NULL); | |
13183 | |
13184 /* create new listop, with children consisting of: | |
13185 * a new pushmark, pad1, pad2. */ | |
13186 newop = newLISTOP(OP_LIST, 0, pad1, pad2); | |
13187 newop->op_flags |= OPf_PARENS; | |
13188 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID; | |
13189 | |
13190 /* insert newop between o and ns3 */ | |
13191 op_sibling_splice(NULL, o, 0, newop); | |
13192 | |
13193 /*fixup op_next chain */ | |
13194 newpm = cUNOPx(newop)->op_first; /* pushmark */ | |
13195 o ->op_next = newpm; | |
13196 newpm->op_next = pad1; | |
13197 pad1 ->op_next = pad2; | |
13198 pad2 ->op_next = newop; /* listop */ | |
13199 newop->op_next = ns3; | |
13200 | |
13201 /* Ensure pushmark has this flag if padops do */ | |
13202 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) { | |
13203 newpm->op_flags |= OPf_MOD; | |
13204 } | |
13205 | |
13206 break; | |
13207 } | |
13208 | |
13209 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen | |
13210 to carry two labels. For now, take the easier option, and skip | |
13211 this optimisation if the first NEXTSTATE has a label. */ | |
13212 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) { | |
13213 OP *nextop = o->op_next; | |
13214 while (nextop && nextop->op_type == OP_NULL) | |
13215 nextop = nextop->op_next; | |
13216 | |
13217 if (nextop && (nextop->op_type == OP_NEXTSTATE)) { | |
13218 op_null(o); | |
13219 if (oldop) | |
13220 oldop->op_next = nextop; | |
13221 /* Skip (old)oldop assignment since the current oldop's | |
13222 op_next already points to the next op. */ | |
13223 continue; | |
13224 } | |
13225 } | |
13226 break; | |
13227 | |
13228 case OP_CONCAT: | |
13229 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) { | |
13230 if (o->op_next->op_private & OPpTARGET_MY) { | |
13231 if (o->op_flags & OPf_STACKED) /* chained concats */ | |
13232 break; /* ignore_optimization */ | |
13233 else { | |
13234 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */ | |
13235 o->op_targ = o->op_next->op_targ; | |
13236 o->op_next->op_targ = 0; | |
13237 o->op_private |= OPpTARGET_MY; | |
13238 } | |
13239 } | |
13240 op_null(o->op_next); | |
13241 } | |
13242 break; | |
13243 case OP_STUB: | |
13244 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) { | |
13245 break; /* Scalar stub must produce undef. List stub is noop */ | |
13246 } | |
13247 goto nothin; | |
13248 case OP_NULL: | |
13249 if (o->op_targ == OP_NEXTSTATE | |
13250 || o->op_targ == OP_DBSTATE) | |
13251 { | |
13252 PL_curcop = ((COP*)o); | |
13253 } | |
13254 /* XXX: We avoid setting op_seq here to prevent later calls | |
13255 to rpeep() from mistakenly concluding that optimisation | |
13256 has already occurred. This doesn't fix the real problem, | |
13257 though (See 20010220.007). AMS 20010719 */ | |
13258 /* op_seq functionality is now replaced by op_opt */ | |
13259 o->op_opt = 0; | |
13260 /* FALLTHROUGH */ | |
13261 case OP_SCALAR: | |
13262 case OP_LINESEQ: | |
13263 case OP_SCOPE: | |
13264 nothin: | |
13265 if (oldop) { | |
13266 oldop->op_next = o->op_next; | |
13267 o->op_opt = 0; | |
13268 continue; | |
13269 } | |
13270 break; | |
13271 | |
13272 case OP_PUSHMARK: | |
13273 | |
13274 /* Given | |
13275 5 repeat/DOLIST | |
13276 3 ex-list | |
13277 1 pushmark | |
13278 2 scalar or const | |
13279 4 const[0] | |
13280 convert repeat into a stub with no kids. | |
13281 */ | |
13282 if (o->op_next->op_type == OP_CONST | |
13283 || ( o->op_next->op_type == OP_PADSV | |
13284 && !(o->op_next->op_private & OPpLVAL_INTRO)) | |
13285 || ( o->op_next->op_type == OP_GV | |
13286 && o->op_next->op_next->op_type == OP_RV2SV | |
13287 && !(o->op_next->op_next->op_private | |
13288 & (OPpLVAL_INTRO|OPpOUR_INTRO)))) | |
13289 { | |
13290 const OP *kid = o->op_next->op_next; | |
13291 if (o->op_next->op_type == OP_GV) | |
13292 kid = kid->op_next; | |
13293 /* kid is now the ex-list. */ | |
13294 if (kid->op_type == OP_NULL | |
13295 && (kid = kid->op_next)->op_type == OP_CONST | |
13296 /* kid is now the repeat count. */ | |
13297 && kid->op_next->op_type == OP_REPEAT | |
13298 && kid->op_next->op_private & OPpREPEAT_DOLIST | |
13299 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST | |
13300 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0) | |
13301 { | |
13302 o = kid->op_next; /* repeat */ | |
13303 assert(oldop); | |
13304 oldop->op_next = o; | |
13305 op_free(cBINOPo->op_first); | |
13306 op_free(cBINOPo->op_last ); | |
13307 o->op_flags &=~ OPf_KIDS; | |
13308 /* stub is a baseop; repeat is a binop */ | |
13309 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP)); | |
13310 OpTYPE_set(o, OP_STUB); | |
13311 o->op_private = 0; | |
13312 break; | |
13313 } | |
13314 } | |
13315 | |
13316 /* Convert a series of PAD ops for my vars plus support into a | |
13317 * single padrange op. Basically | |
13318 * | |
13319 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest | |
13320 * | |
13321 * becomes, depending on circumstances, one of | |
13322 * | |
13323 * padrange ----------------------------------> (list) -> rest | |
13324 * padrange --------------------------------------------> rest | |
13325 * | |
13326 * where all the pad indexes are sequential and of the same type | |
13327 * (INTRO or not). | |
13328 * We convert the pushmark into a padrange op, then skip | |
13329 * any other pad ops, and possibly some trailing ops. | |
13330 * Note that we don't null() the skipped ops, to make it | |
13331 * easier for Deparse to undo this optimisation (and none of | |
13332 * the skipped ops are holding any resourses). It also makes | |
13333 * it easier for find_uninit_var(), as it can just ignore | |
13334 * padrange, and examine the original pad ops. | |
13335 */ | |
13336 { | |
13337 OP *p; | |
13338 OP *followop = NULL; /* the op that will follow the padrange op */ | |
13339 U8 count = 0; | |
13340 U8 intro = 0; | |
13341 PADOFFSET base = 0; /* init only to stop compiler whining */ | |
13342 bool gvoid = 0; /* init only to stop compiler whining */ | |
13343 bool defav = 0; /* seen (...) = @_ */ | |
13344 bool reuse = 0; /* reuse an existing padrange op */ | |
13345 | |
13346 /* look for a pushmark -> gv[_] -> rv2av */ | |
13347 | |
13348 { | |
13349 OP *rv2av, *q; | |
13350 p = o->op_next; | |
13351 if ( p->op_type == OP_GV | |
13352 && cGVOPx_gv(p) == PL_defgv | |
13353 && (rv2av = p->op_next) | |
13354 && rv2av->op_type == OP_RV2AV | |
13355 && !(rv2av->op_flags & OPf_REF) | |
13356 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB)) | |
13357 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST) | |
13358 ) { | |
13359 q = rv2av->op_next; | |
13360 if (q->op_type == OP_NULL) | |
13361 q = q->op_next; | |
13362 if (q->op_type == OP_PUSHMARK) { | |
13363 defav = 1; | |
13364 p = q; | |
13365 } | |
13366 } | |
13367 } | |
13368 if (!defav) { | |
13369 p = o; | |
13370 } | |
13371 | |
13372 /* scan for PAD ops */ | |
13373 | |
13374 for (p = p->op_next; p; p = p->op_next) { | |
13375 if (p->op_type == OP_NULL) | |
13376 continue; | |
13377 | |
13378 if (( p->op_type != OP_PADSV | |
13379 && p->op_type != OP_PADAV | |
13380 && p->op_type != OP_PADHV | |
13381 ) | |
13382 /* any private flag other than INTRO? e.g. STATE */ | |
13383 || (p->op_private & ~OPpLVAL_INTRO) | |
13384 ) | |
13385 break; | |
13386 | |
13387 /* let $a[N] potentially be optimised into AELEMFAST_LEX | |
13388 * instead */ | |
13389 if ( p->op_type == OP_PADAV | |
13390 && p->op_next | |
13391 && p->op_next->op_type == OP_CONST | |
13392 && p->op_next->op_next | |
13393 && p->op_next->op_next->op_type == OP_AELEM | |
13394 ) | |
13395 break; | |
13396 | |
13397 /* for 1st padop, note what type it is and the range | |
13398 * start; for the others, check that it's the same type | |
13399 * and that the targs are contiguous */ | |
13400 if (count == 0) { | |
13401 intro = (p->op_private & OPpLVAL_INTRO); | |
13402 base = p->op_targ; | |
13403 gvoid = OP_GIMME(p,0) == G_VOID; | |
13404 } | |
13405 else { | |
13406 if ((p->op_private & OPpLVAL_INTRO) != intro) | |
13407 break; | |
13408 /* Note that you'd normally expect targs to be | |
13409 * contiguous in my($a,$b,$c), but that's not the case | |
13410 * when external modules start doing things, e.g. | |
13411 i* Function::Parameters */ | |
13412 if (p->op_targ != base + count) | |
13413 break; | |
13414 assert(p->op_targ == base + count); | |
13415 /* Either all the padops or none of the padops should | |
13416 be in void context. Since we only do the optimisa- | |
13417 tion for av/hv when the aggregate itself is pushed | |
13418 on to the stack (one item), there is no need to dis- | |
13419 tinguish list from scalar context. */ | |
13420 if (gvoid != (OP_GIMME(p,0) == G_VOID)) | |
13421 break; | |
13422 } | |
13423 | |
13424 /* for AV, HV, only when we're not flattening */ | |
13425 if ( p->op_type != OP_PADSV | |
13426 && !gvoid | |
13427 && !(p->op_flags & OPf_REF) | |
13428 ) | |
13429 break; | |
13430 | |
13431 if (count >= OPpPADRANGE_COUNTMASK) | |
13432 break; | |
13433 | |
13434 /* there's a biggest base we can fit into a | |
13435 * SAVEt_CLEARPADRANGE in pp_padrange */ | |
13436 if (intro && base > | |
13437 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))) | |
13438 break; | |
13439 | |
13440 /* Success! We've got another valid pad op to optimise away */ | |
13441 count++; | |
13442 followop = p->op_next; | |
13443 } | |
13444 | |
13445 if (count < 1 || (count == 1 && !defav)) | |
13446 break; | |
13447 | |
13448 /* pp_padrange in specifically compile-time void context | |
13449 * skips pushing a mark and lexicals; in all other contexts | |
13450 * (including unknown till runtime) it pushes a mark and the | |
13451 * lexicals. We must be very careful then, that the ops we | |
13452 * optimise away would have exactly the same effect as the | |
13453 * padrange. | |
13454 * In particular in void context, we can only optimise to | |
13455 * a padrange if see see the complete sequence | |
13456 * pushmark, pad*v, ...., list | |
13457 * which has the net effect of of leaving the markstack as it | |
13458 * was. Not pushing on to the stack (whereas padsv does touch | |
13459 * the stack) makes no difference in void context. | |
13460 */ | |
13461 assert(followop); | |
13462 if (gvoid) { | |
13463 if (followop->op_type == OP_LIST | |
13464 && OP_GIMME(followop,0) == G_VOID | |
13465 ) | |
13466 { | |
13467 followop = followop->op_next; /* skip OP_LIST */ | |
13468 | |
13469 /* consolidate two successive my(...);'s */ | |
13470 | |
13471 if ( oldoldop | |
13472 && oldoldop->op_type == OP_PADRANGE | |
13473 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID | |
13474 && (oldoldop->op_private & OPpLVAL_INTRO) == intro | |
13475 && !(oldoldop->op_flags & OPf_SPECIAL) | |
13476 ) { | |
13477 U8 old_count; | |
13478 assert(oldoldop->op_next == oldop); | |
13479 assert( oldop->op_type == OP_NEXTSTATE | |
13480 || oldop->op_type == OP_DBSTATE); | |
13481 assert(oldop->op_next == o); | |
13482 | |
13483 old_count | |
13484 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK); | |
13485 | |
13486 /* Do not assume pad offsets for $c and $d are con- | |
13487 tiguous in | |
13488 my ($a,$b,$c); | |
13489 my ($d,$e,$f); | |
13490 */ | |
13491 if ( oldoldop->op_targ + old_count == base | |
13492 && old_count < OPpPADRANGE_COUNTMASK - count) { | |
13493 base = oldoldop->op_targ; | |
13494 count += old_count; | |
13495 reuse = 1; | |
13496 } | |
13497 } | |
13498 | |
13499 /* if there's any immediately following singleton | |
13500 * my var's; then swallow them and the associated | |
13501 * nextstates; i.e. | |
13502 * my ($a,$b); my $c; my $d; | |
13503 * is treated as | |
13504 * my ($a,$b,$c,$d); | |
13505 */ | |
13506 | |
13507 while ( ((p = followop->op_next)) | |
13508 && ( p->op_type == OP_PADSV | |
13509 || p->op_type == OP_PADAV | |
13510 || p->op_type == OP_PADHV) | |
13511 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID | |
13512 && (p->op_private & OPpLVAL_INTRO) == intro | |
13513 && !(p->op_private & ~OPpLVAL_INTRO) | |
13514 && p->op_next | |
13515 && ( p->op_next->op_type == OP_NEXTSTATE | |
13516 || p->op_next->op_type == OP_DBSTATE) | |
13517 && count < OPpPADRANGE_COUNTMASK | |
13518 && base + count == p->op_targ | |
13519 ) { | |
13520 count++; | |
13521 followop = p->op_next; | |
13522 } | |
13523 } | |
13524 else | |
13525 break; | |
13526 } | |
13527 | |
13528 if (reuse) { | |
13529 assert(oldoldop->op_type == OP_PADRANGE); | |
13530 oldoldop->op_next = followop; | |
13531 oldoldop->op_private = (intro | count); | |
13532 o = oldoldop; | |
13533 oldop = NULL; | |
13534 oldoldop = NULL; | |
13535 } | |
13536 else { | |
13537 /* Convert the pushmark into a padrange. | |
13538 * To make Deparse easier, we guarantee that a padrange was | |
13539 * *always* formerly a pushmark */ | |
13540 assert(o->op_type == OP_PUSHMARK); | |
13541 o->op_next = followop; | |
13542 OpTYPE_set(o, OP_PADRANGE); | |
13543 o->op_targ = base; | |
13544 /* bit 7: INTRO; bit 6..0: count */ | |
13545 o->op_private = (intro | count); | |
13546 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL)) | |
13547 | gvoid * OPf_WANT_VOID | |
13548 | (defav ? OPf_SPECIAL : 0)); | |
13549 } | |
13550 break; | |
13551 } | |
13552 | |
13553 case OP_PADAV: | |
13554 case OP_PADSV: | |
13555 case OP_PADHV: | |
13556 /* Skip over state($x) in void context. */ | |
13557 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO) | |
13558 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID) | |
13559 { | |
13560 oldop->op_next = o->op_next; | |
13561 goto redo_nextstate; | |
13562 } | |
13563 if (o->op_type != OP_PADAV) | |
13564 break; | |
13565 /* FALLTHROUGH */ | |
13566 case OP_GV: | |
13567 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) { | |
13568 OP* const pop = (o->op_type == OP_PADAV) ? | |
13569 o->op_next : o->op_next->op_next; | |
13570 IV i; | |
13571 if (pop && pop->op_type == OP_CONST && | |
13572 ((PL_op = pop->op_next)) && | |
13573 pop->op_next->op_type == OP_AELEM && | |
13574 !(pop->op_next->op_private & | |
13575 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) && | |
13576 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127) | |
13577 { | |
13578 GV *gv; | |
13579 if (cSVOPx(pop)->op_private & OPpCONST_STRICT) | |
13580 no_bareword_allowed(pop); | |
13581 if (o->op_type == OP_GV) | |
13582 op_null(o->op_next); | |
13583 op_null(pop->op_next); | |
13584 op_null(pop); | |
13585 o->op_flags |= pop->op_next->op_flags & OPf_MOD; | |
13586 o->op_next = pop->op_next->op_next; | |
13587 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST]; | |
13588 o->op_private = (U8)i; | |
13589 if (o->op_type == OP_GV) { | |
13590 gv = cGVOPo_gv; | |
13591 GvAVn(gv); | |
13592 o->op_type = OP_AELEMFAST; | |
13593 } | |
13594 else | |
13595 o->op_type = OP_AELEMFAST_LEX; | |
13596 } | |
13597 if (o->op_type != OP_GV) | |
13598 break; | |
13599 } | |
13600 | |
13601 /* Remove $foo from the op_next chain in void context. */ | |
13602 if (oldop | |
13603 && ( o->op_next->op_type == OP_RV2SV | |
13604 || o->op_next->op_type == OP_RV2AV | |
13605 || o->op_next->op_type == OP_RV2HV ) | |
13606 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID | |
13607 && !(o->op_next->op_private & OPpLVAL_INTRO)) | |
13608 { | |
13609 oldop->op_next = o->op_next->op_next; | |
13610 /* Reprocess the previous op if it is a nextstate, to | |
13611 allow double-nextstate optimisation. */ | |
13612 redo_nextstate: | |
13613 if (oldop->op_type == OP_NEXTSTATE) { | |
13614 oldop->op_opt = 0; | |
13615 o = oldop; | |
13616 oldop = oldoldop; | |
13617 oldoldop = NULL; | |
13618 goto redo; | |
13619 } | |
13620 o = oldop; | |
13621 } | |
13622 else if (o->op_next->op_type == OP_RV2SV) { | |
13623 if (!(o->op_next->op_private & OPpDEREF)) { | |
13624 op_null(o->op_next); | |
13625 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO | |
13626 | OPpOUR_INTRO); | |
13627 o->op_next = o->op_next->op_next; | |
13628 OpTYPE_set(o, OP_GVSV); | |
13629 } | |
13630 } | |
13631 else if (o->op_next->op_type == OP_READLINE | |
13632 && o->op_next->op_next->op_type == OP_CONCAT | |
13633 && (o->op_next->op_next->op_flags & OPf_STACKED)) | |
13634 { | |
13635 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */ | |
13636 OpTYPE_set(o, OP_RCATLINE); | |
13637 o->op_flags |= OPf_STACKED; | |
13638 op_null(o->op_next->op_next); | |
13639 op_null(o->op_next); | |
13640 } | |
13641 | |
13642 break; | |
13643 | |
13644 #define HV_OR_SCALARHV(op) \ | |
13645 ( (op)->op_type == OP_PADHV || (op)->op_type == OP_RV2HV \ | |
13646 ? (op) \ | |
13647 : (op)->op_type == OP_SCALAR && (op)->op_flags & OPf_KIDS \ | |
13648 && ( cUNOPx(op)->op_first->op_type == OP_PADHV \ | |
13649 || cUNOPx(op)->op_first->op_type == OP_RV2HV) \ | |
13650 ? cUNOPx(op)->op_first \ | |
13651 : NULL) | |
13652 | |
13653 case OP_NOT: | |
13654 if ((fop = HV_OR_SCALARHV(cUNOP->op_first))) | |
13655 fop->op_private |= OPpTRUEBOOL; | |
13656 break; | |
13657 | |
13658 case OP_AND: | |
13659 case OP_OR: | |
13660 case OP_DOR: | |
13661 fop = cLOGOP->op_first; | |
13662 sop = OpSIBLING(fop); | |
13663 while (cLOGOP->op_other->op_type == OP_NULL) | |
13664 cLOGOP->op_other = cLOGOP->op_other->op_next; | |
13665 while (o->op_next && ( o->op_type == o->op_next->op_type | |
13666 || o->op_next->op_type == OP_NULL)) | |
13667 o->op_next = o->op_next->op_next; | |
13668 | |
13669 /* if we're an OR and our next is a AND in void context, we'll | |
13670 follow it's op_other on short circuit, same for reverse. | |
13671 We can't do this with OP_DOR since if it's true, its return | |
13672 value is the underlying value which must be evaluated | |
13673 by the next op */ | |
13674 if (o->op_next && | |
13675 ( | |
13676 (IS_AND_OP(o) && IS_OR_OP(o->op_next)) | |
13677 || (IS_OR_OP(o) && IS_AND_OP(o->op_next)) | |
13678 ) | |
13679 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID | |
13680 ) { | |
13681 o->op_next = ((LOGOP*)o->op_next)->op_other; | |
13682 } | |
13683 DEFER(cLOGOP->op_other); | |
13684 | |
13685 o->op_opt = 1; | |
13686 fop = HV_OR_SCALARHV(fop); | |
13687 if (sop) sop = HV_OR_SCALARHV(sop); | |
13688 if (fop || sop | |
13689 ){ | |
13690 OP * nop = o; | |
13691 OP * lop = o; | |
13692 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) { | |
13693 while (nop && nop->op_next) { | |
13694 switch (nop->op_next->op_type) { | |
13695 case OP_NOT: | |
13696 case OP_AND: | |
13697 case OP_OR: | |
13698 case OP_DOR: | |
13699 lop = nop = nop->op_next; | |
13700 break; | |
13701 case OP_NULL: | |
13702 nop = nop->op_next; | |
13703 break; | |
13704 default: | |
13705 nop = NULL; | |
13706 break; | |
13707 } | |
13708 } | |
13709 } | |
13710 if (fop) { | |
13711 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID | |
13712 || o->op_type == OP_AND ) | |
13713 fop->op_private |= OPpTRUEBOOL; | |
13714 else if (!(lop->op_flags & OPf_WANT)) | |
13715 fop->op_private |= OPpMAYBE_TRUEBOOL; | |
13716 } | |
13717 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID | |
13718 && sop) | |
13719 sop->op_private |= OPpTRUEBOOL; | |
13720 } | |
13721 | |
13722 | |
13723 break; | |
13724 | |
13725 case OP_COND_EXPR: | |
13726 if ((fop = HV_OR_SCALARHV(cLOGOP->op_first))) | |
13727 fop->op_private |= OPpTRUEBOOL; | |
13728 #undef HV_OR_SCALARHV | |
13729 /* GERONIMO! */ /* FALLTHROUGH */ | |
13730 | |
13731 case OP_MAPWHILE: | |
13732 case OP_GREPWHILE: | |
13733 case OP_ANDASSIGN: | |
13734 case OP_ORASSIGN: | |
13735 case OP_DORASSIGN: | |
13736 case OP_RANGE: | |
13737 case OP_ONCE: | |
13738 while (cLOGOP->op_other->op_type == OP_NULL) | |
13739 cLOGOP->op_other = cLOGOP->op_other->op_next; | |
13740 DEFER(cLOGOP->op_other); | |
13741 break; | |
13742 | |
13743 case OP_ENTERLOOP: | |
13744 case OP_ENTERITER: | |
13745 while (cLOOP->op_redoop->op_type == OP_NULL) | |
13746 cLOOP->op_redoop = cLOOP->op_redoop->op_next; | |
13747 while (cLOOP->op_nextop->op_type == OP_NULL) | |
13748 cLOOP->op_nextop = cLOOP->op_nextop->op_next; | |
13749 while (cLOOP->op_lastop->op_type == OP_NULL) | |
13750 cLOOP->op_lastop = cLOOP->op_lastop->op_next; | |
13751 /* a while(1) loop doesn't have an op_next that escapes the | |
13752 * loop, so we have to explicitly follow the op_lastop to | |
13753 * process the rest of the code */ | |
13754 DEFER(cLOOP->op_lastop); | |
13755 break; | |
13756 | |
13757 case OP_ENTERTRY: | |
13758 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY); | |
13759 DEFER(cLOGOPo->op_other); | |
13760 break; | |
13761 | |
13762 case OP_SUBST: | |
13763 assert(!(cPMOP->op_pmflags & PMf_ONCE)); | |
13764 while (cPMOP->op_pmstashstartu.op_pmreplstart && | |
13765 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL) | |
13766 cPMOP->op_pmstashstartu.op_pmreplstart | |
13767 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next; | |
13768 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart); | |
13769 break; | |
13770 | |
13771 case OP_SORT: { | |
13772 OP *oright; | |
13773 | |
13774 if (o->op_flags & OPf_SPECIAL) { | |
13775 /* first arg is a code block */ | |
13776 OP * const nullop = OpSIBLING(cLISTOP->op_first); | |
13777 OP * kid = cUNOPx(nullop)->op_first; | |
13778 | |
13779 assert(nullop->op_type == OP_NULL); | |
13780 assert(kid->op_type == OP_SCOPE | |
13781 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE)); | |
13782 /* since OP_SORT doesn't have a handy op_other-style | |
13783 * field that can point directly to the start of the code | |
13784 * block, store it in the otherwise-unused op_next field | |
13785 * of the top-level OP_NULL. This will be quicker at | |
13786 * run-time, and it will also allow us to remove leading | |
13787 * OP_NULLs by just messing with op_nexts without | |
13788 * altering the basic op_first/op_sibling layout. */ | |
13789 kid = kLISTOP->op_first; | |
13790 assert( | |
13791 (kid->op_type == OP_NULL | |
13792 && ( kid->op_targ == OP_NEXTSTATE | |
13793 || kid->op_targ == OP_DBSTATE )) | |
13794 || kid->op_type == OP_STUB | |
13795 || kid->op_type == OP_ENTER); | |
13796 nullop->op_next = kLISTOP->op_next; | |
13797 DEFER(nullop->op_next); | |
13798 } | |
13799 | |
13800 /* check that RHS of sort is a single plain array */ | |
13801 oright = cUNOPo->op_first; | |
13802 if (!oright || oright->op_type != OP_PUSHMARK) | |
13803 break; | |
13804 | |
13805 if (o->op_private & OPpSORT_INPLACE) | |
13806 break; | |
13807 | |
13808 /* reverse sort ... can be optimised. */ | |
13809 if (!OpHAS_SIBLING(cUNOPo)) { | |
13810 /* Nothing follows us on the list. */ | |
13811 OP * const reverse = o->op_next; | |
13812 | |
13813 if (reverse->op_type == OP_REVERSE && | |
13814 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) { | |
13815 OP * const pushmark = cUNOPx(reverse)->op_first; | |
13816 if (pushmark && (pushmark->op_type == OP_PUSHMARK) | |
13817 && (OpSIBLING(cUNOPx(pushmark)) == o)) { | |
13818 /* reverse -> pushmark -> sort */ | |
13819 o->op_private |= OPpSORT_REVERSE; | |
13820 op_null(reverse); | |
13821 pushmark->op_next = oright->op_next; | |
13822 op_null(oright); | |
13823 } | |
13824 } | |
13825 } | |
13826 | |
13827 break; | |
13828 } | |
13829 | |
13830 case OP_REVERSE: { | |
13831 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av; | |
13832 OP *gvop = NULL; | |
13833 LISTOP *enter, *exlist; | |
13834 | |
13835 if (o->op_private & OPpSORT_INPLACE) | |
13836 break; | |
13837 | |
13838 enter = (LISTOP *) o->op_next; | |
13839 if (!enter) | |
13840 break; | |
13841 if (enter->op_type == OP_NULL) { | |
13842 enter = (LISTOP *) enter->op_next; | |
13843 if (!enter) | |
13844 break; | |
13845 } | |
13846 /* for $a (...) will have OP_GV then OP_RV2GV here. | |
13847 for (...) just has an OP_GV. */ | |
13848 if (enter->op_type == OP_GV) { | |
13849 gvop = (OP *) enter; | |
13850 enter = (LISTOP *) enter->op_next; | |
13851 if (!enter) | |
13852 break; | |
13853 if (enter->op_type == OP_RV2GV) { | |
13854 enter = (LISTOP *) enter->op_next; | |
13855 if (!enter) | |
13856 break; | |
13857 } | |
13858 } | |
13859 | |
13860 if (enter->op_type != OP_ENTERITER) | |
13861 break; | |
13862 | |
13863 iter = enter->op_next; | |
13864 if (!iter || iter->op_type != OP_ITER) | |
13865 break; | |
13866 | |
13867 expushmark = enter->op_first; | |
13868 if (!expushmark || expushmark->op_type != OP_NULL | |
13869 || expushmark->op_targ != OP_PUSHMARK) | |
13870 break; | |
13871 | |
13872 exlist = (LISTOP *) OpSIBLING(expushmark); | |
13873 if (!exlist || exlist->op_type != OP_NULL | |
13874 || exlist->op_targ != OP_LIST) | |
13875 break; | |
13876 | |
13877 if (exlist->op_last != o) { | |
13878 /* Mmm. Was expecting to point back to this op. */ | |
13879 break; | |
13880 } | |
13881 theirmark = exlist->op_first; | |
13882 if (!theirmark || theirmark->op_type != OP_PUSHMARK) | |
13883 break; | |
13884 | |
13885 if (OpSIBLING(theirmark) != o) { | |
13886 /* There's something between the mark and the reverse, eg | |
13887 for (1, reverse (...)) | |
13888 so no go. */ | |
13889 break; | |
13890 } | |
13891 | |
13892 ourmark = ((LISTOP *)o)->op_first; | |
13893 if (!ourmark || ourmark->op_type != OP_PUSHMARK) | |
13894 break; | |
13895 | |
13896 ourlast = ((LISTOP *)o)->op_last; | |
13897 if (!ourlast || ourlast->op_next != o) | |
13898 break; | |
13899 | |
13900 rv2av = OpSIBLING(ourmark); | |
13901 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av) | |
13902 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) { | |
13903 /* We're just reversing a single array. */ | |
13904 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF; | |
13905 enter->op_flags |= OPf_STACKED; | |
13906 } | |
13907 | |
13908 /* We don't have control over who points to theirmark, so sacrifice | |
13909 ours. */ | |
13910 theirmark->op_next = ourmark->op_next; | |
13911 theirmark->op_flags = ourmark->op_flags; | |
13912 ourlast->op_next = gvop ? gvop : (OP *) enter; | |
13913 op_null(ourmark); | |
13914 op_null(o); | |
13915 enter->op_private |= OPpITER_REVERSED; | |
13916 iter->op_private |= OPpITER_REVERSED; | |
13917 | |
13918 break; | |
13919 } | |
13920 | |
13921 case OP_QR: | |
13922 case OP_MATCH: | |
13923 if (!(cPMOP->op_pmflags & PMf_ONCE)) { | |
13924 assert (!cPMOP->op_pmstashstartu.op_pmreplstart); | |
13925 } | |
13926 break; | |
13927 | |
13928 case OP_RUNCV: | |
13929 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv) | |
13930 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb))) | |
13931 { | |
13932 SV *sv; | |
13933 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef; | |
13934 else { | |
13935 sv = newRV((SV *)PL_compcv); | |
13936 sv_rvweaken(sv); | |
13937 SvREADONLY_on(sv); | |
13938 } | |
13939 OpTYPE_set(o, OP_CONST); | |
13940 o->op_flags |= OPf_SPECIAL; | |
13941 cSVOPo->op_sv = sv; | |
13942 } | |
13943 break; | |
13944 | |
13945 case OP_SASSIGN: | |
13946 if (OP_GIMME(o,0) == G_VOID | |
13947 || ( o->op_next->op_type == OP_LINESEQ | |
13948 && ( o->op_next->op_next->op_type == OP_LEAVESUB | |
13949 || ( o->op_next->op_next->op_type == OP_RETURN | |
13950 && !CvLVALUE(PL_compcv))))) | |
13951 { | |
13952 OP *right = cBINOP->op_first; | |
13953 if (right) { | |
13954 /* sassign | |
13955 * RIGHT | |
13956 * substr | |
13957 * pushmark | |
13958 * arg1 | |
13959 * arg2 | |
13960 * ... | |
13961 * becomes | |
13962 * | |
13963 * ex-sassign | |
13964 * substr | |
13965 * pushmark | |
13966 * RIGHT | |
13967 * arg1 | |
13968 * arg2 | |
13969 * ... | |
13970 */ | |
13971 OP *left = OpSIBLING(right); | |
13972 if (left->op_type == OP_SUBSTR | |
13973 && (left->op_private & 7) < 4) { | |
13974 op_null(o); | |
13975 /* cut out right */ | |
13976 op_sibling_splice(o, NULL, 1, NULL); | |
13977 /* and insert it as second child of OP_SUBSTR */ | |
13978 op_sibling_splice(left, cBINOPx(left)->op_first, 0, | |
13979 right); | |
13980 left->op_private |= OPpSUBSTR_REPL_FIRST; | |
13981 left->op_flags = | |
13982 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; | |
13983 } | |
13984 } | |
13985 } | |
13986 break; | |
13987 | |
13988 case OP_AASSIGN: | |
13989 /* We do the common-vars check here, rather than in newASSIGNOP | |
13990 (as formerly), so that all lexical vars that get aliased are | |
13991 marked as such before we do the check. */ | |
13992 /* There can’t be common vars if the lhs is a stub. */ | |
13993 if (OpSIBLING(cLISTOPx(cBINOPo->op_last)->op_first) | |
13994 == cLISTOPx(cBINOPo->op_last)->op_last | |
13995 && cLISTOPx(cBINOPo->op_last)->op_last->op_type == OP_STUB) | |
13996 { | |
13997 o->op_private &=~ OPpASSIGN_COMMON; | |
13998 break; | |
13999 } | |
14000 if (o->op_private & OPpASSIGN_COMMON) { | |
14001 /* See the comment before S_aassign_common_vars concerning | |
14002 PL_generation sorcery. */ | |
14003 PL_generation++; | |
14004 if (!aassign_common_vars(o)) | |
14005 o->op_private &=~ OPpASSIGN_COMMON; | |
14006 } | |
14007 else if (S_aassign_common_vars_aliases_only(aTHX_ o)) | |
14008 o->op_private |= OPpASSIGN_COMMON; | |
14009 break; | |
14010 | |
14011 case OP_CUSTOM: { | |
14012 Perl_cpeep_t cpeep = | |
14013 XopENTRYCUSTOM(o, xop_peep); | |
14014 if (cpeep) | |
14015 cpeep(aTHX_ o, oldop); | |
14016 break; | |
14017 } | |
14018 | |
14019 } | |
14020 /* did we just null the current op? If so, re-process it to handle | |
14021 * eliding "empty" ops from the chain */ | |
14022 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) { | |
14023 o->op_opt = 0; | |
14024 o = oldop; | |
14025 } | |
14026 else { | |
14027 oldoldop = oldop; | |
14028 oldop = o; | |
14029 } | |
14030 } | |
14031 LEAVE; | |
14032 } | |
14033 | |
14034 void | |
14035 Perl_peep(pTHX_ OP *o) | |
14036 { | |
14037 CALL_RPEEP(o); | |
14038 } | |
14039 | |
14040 /* | |
14041 =head1 Custom Operators | |
14042 | |
14043 =for apidoc Ao||custom_op_xop | |
14044 Return the XOP structure for a given custom op. This macro should be | |
14045 considered internal to OP_NAME and the other access macros: use them instead. | |
14046 This macro does call a function. Prior | |
14047 to 5.19.6, this was implemented as a | |
14048 function. | |
14049 | |
14050 =cut | |
14051 */ | |
14052 | |
14053 XOPRETANY | |
14054 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field) | |
14055 { | |
14056 SV *keysv; | |
14057 HE *he = NULL; | |
14058 XOP *xop; | |
14059 | |
14060 static const XOP xop_null = { 0, 0, 0, 0, 0 }; | |
14061 | |
14062 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD; | |
14063 assert(o->op_type == OP_CUSTOM); | |
14064 | |
14065 /* This is wrong. It assumes a function pointer can be cast to IV, | |
14066 * which isn't guaranteed, but this is what the old custom OP code | |
14067 * did. In principle it should be safer to Copy the bytes of the | |
14068 * pointer into a PV: since the new interface is hidden behind | |
14069 * functions, this can be changed later if necessary. */ | |
14070 /* Change custom_op_xop if this ever happens */ | |
14071 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr))); | |
14072 | |
14073 if (PL_custom_ops) | |
14074 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0); | |
14075 | |
14076 /* assume noone will have just registered a desc */ | |
14077 if (!he && PL_custom_op_names && | |
14078 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0)) | |
14079 ) { | |
14080 const char *pv; | |
14081 STRLEN l; | |
14082 | |
14083 /* XXX does all this need to be shared mem? */ | |
14084 Newxz(xop, 1, XOP); | |
14085 pv = SvPV(HeVAL(he), l); | |
14086 XopENTRY_set(xop, xop_name, savepvn(pv, l)); | |
14087 if (PL_custom_op_descs && | |
14088 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0)) | |
14089 ) { | |
14090 pv = SvPV(HeVAL(he), l); | |
14091 XopENTRY_set(xop, xop_desc, savepvn(pv, l)); | |
14092 } | |
14093 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop); | |
14094 } | |
14095 else { | |
14096 if (!he) | |
14097 xop = (XOP *)&xop_null; | |
14098 else | |
14099 xop = INT2PTR(XOP *, SvIV(HeVAL(he))); | |
14100 } | |
14101 { | |
14102 XOPRETANY any; | |
14103 if(field == XOPe_xop_ptr) { | |
14104 any.xop_ptr = xop; | |
14105 } else { | |
14106 const U32 flags = XopFLAGS(xop); | |
14107 if(flags & field) { | |
14108 switch(field) { | |
14109 case XOPe_xop_name: | |
14110 any.xop_name = xop->xop_name; | |
14111 break; | |
14112 case XOPe_xop_desc: | |
14113 any.xop_desc = xop->xop_desc; | |
14114 break; | |
14115 case XOPe_xop_class: | |
14116 any.xop_class = xop->xop_class; | |
14117 break; | |
14118 case XOPe_xop_peep: | |
14119 any.xop_peep = xop->xop_peep; | |
14120 break; | |
14121 default: | |
14122 NOT_REACHED; /* NOTREACHED */ | |
14123 break; | |
14124 } | |
14125 } else { | |
14126 switch(field) { | |
14127 case XOPe_xop_name: | |
14128 any.xop_name = XOPd_xop_name; | |
14129 break; | |
14130 case XOPe_xop_desc: | |
14131 any.xop_desc = XOPd_xop_desc; | |
14132 break; | |
14133 case XOPe_xop_class: | |
14134 any.xop_class = XOPd_xop_class; | |
14135 break; | |
14136 case XOPe_xop_peep: | |
14137 any.xop_peep = XOPd_xop_peep; | |
14138 break; | |
14139 default: | |
14140 NOT_REACHED; /* NOTREACHED */ | |
14141 break; | |
14142 } | |
14143 } | |
14144 } | |
14145 /* Some gcc releases emit a warning for this function: | |
14146 * op.c: In function 'Perl_custom_op_get_field': | |
14147 * op.c:...: warning: 'any.xop_name' may be used uninitialized in this function [-Wmaybe-uninitialized] | |
14148 * Whether this is true, is currently unknown. */ | |
14149 return any; | |
14150 } | |
14151 } | |
14152 | |
14153 /* | |
14154 =for apidoc Ao||custom_op_register | |
14155 Register a custom op. See L<perlguts/"Custom Operators">. | |
14156 | |
14157 =cut | |
14158 */ | |
14159 | |
14160 void | |
14161 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop) | |
14162 { | |
14163 SV *keysv; | |
14164 | |
14165 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER; | |
14166 | |
14167 /* see the comment in custom_op_xop */ | |
14168 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr))); | |
14169 | |
14170 if (!PL_custom_ops) | |
14171 PL_custom_ops = newHV(); | |
14172 | |
14173 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0)) | |
14174 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name); | |
14175 } | |
14176 | |
14177 /* | |
14178 | |
14179 =for apidoc core_prototype | |
14180 | |
14181 This function assigns the prototype of the named core function to C<sv>, or | |
14182 to a new mortal SV if C<sv> is NULL. It returns the modified C<sv>, or | |
14183 NULL if the core function has no prototype. C<code> is a code as returned | |
14184 by C<keyword()>. It must not be equal to 0. | |
14185 | |
14186 =cut | |
14187 */ | |
14188 | |
14189 SV * | |
14190 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code, | |
14191 int * const opnum) | |
14192 { | |
14193 int i = 0, n = 0, seen_question = 0, defgv = 0; | |
14194 I32 oa; | |
14195 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2) | |
14196 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */ | |
14197 bool nullret = FALSE; | |
14198 | |
14199 PERL_ARGS_ASSERT_CORE_PROTOTYPE; | |
14200 | |
14201 assert (code); | |
14202 | |
14203 if (!sv) sv = sv_newmortal(); | |
14204 | |
14205 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv | |
14206 | |
14207 switch (code < 0 ? -code : code) { | |
14208 case KEY_and : case KEY_chop: case KEY_chomp: | |
14209 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec : | |
14210 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto : | |
14211 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le : | |
14212 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next : | |
14213 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr : | |
14214 case KEY_redo : case KEY_require: case KEY_return: case KEY_say : | |
14215 case KEY_select: case KEY_sort : case KEY_split : case KEY_system: | |
14216 case KEY_x : case KEY_xor : | |
14217 if (!opnum) return NULL; nullret = TRUE; goto findopnum; | |
14218 case KEY_glob: retsetpvs("_;", OP_GLOB); | |
14219 case KEY_keys: retsetpvs("+", OP_KEYS); | |
14220 case KEY_values: retsetpvs("+", OP_VALUES); | |
14221 case KEY_each: retsetpvs("+", OP_EACH); | |
14222 case KEY_push: retsetpvs("+@", OP_PUSH); | |
14223 case KEY_unshift: retsetpvs("+@", OP_UNSHIFT); | |
14224 case KEY_pop: retsetpvs(";+", OP_POP); | |
14225 case KEY_shift: retsetpvs(";+", OP_SHIFT); | |
14226 case KEY_pos: retsetpvs(";\\[$*]", OP_POS); | |
14227 case KEY_splice: | |
14228 retsetpvs("+;$$@", OP_SPLICE); | |
14229 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__: | |
14230 retsetpvs("", 0); | |
14231 case KEY_evalbytes: | |
14232 name = "entereval"; break; | |
14233 case KEY_readpipe: | |
14234 name = "backtick"; | |
14235 } | |
14236 | |
14237 #undef retsetpvs | |
14238 | |
14239 findopnum: | |
14240 while (i < MAXO) { /* The slow way. */ | |
14241 if (strEQ(name, PL_op_name[i]) | |
14242 || strEQ(name, PL_op_desc[i])) | |
14243 { | |
14244 if (nullret) { assert(opnum); *opnum = i; return NULL; } | |
14245 goto found; | |
14246 } | |
14247 i++; | |
14248 } | |
14249 return NULL; | |
14250 found: | |
14251 defgv = PL_opargs[i] & OA_DEFGV; | |
14252 oa = PL_opargs[i] >> OASHIFT; | |
14253 while (oa) { | |
14254 if (oa & OA_OPTIONAL && !seen_question && ( | |
14255 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF | |
14256 )) { | |
14257 seen_question = 1; | |
14258 str[n++] = ';'; | |
14259 } | |
14260 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF | |
14261 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF | |
14262 /* But globs are already references (kinda) */ | |
14263 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF | |
14264 ) { | |
14265 str[n++] = '\\'; | |
14266 } | |
14267 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF | |
14268 && !scalar_mod_type(NULL, i)) { | |
14269 str[n++] = '['; | |
14270 str[n++] = '$'; | |
14271 str[n++] = '@'; | |
14272 str[n++] = '%'; | |
14273 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&'; | |
14274 str[n++] = '*'; | |
14275 str[n++] = ']'; | |
14276 } | |
14277 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)]; | |
14278 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') { | |
14279 str[n-1] = '_'; defgv = 0; | |
14280 } | |
14281 oa = oa >> 4; | |
14282 } | |
14283 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';'; | |
14284 str[n++] = '\0'; | |
14285 sv_setpvn(sv, str, n - 1); | |
14286 if (opnum) *opnum = i; | |
14287 return sv; | |
14288 } | |
14289 | |
14290 OP * | |
14291 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code, | |
14292 const int opnum) | |
14293 { | |
14294 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv); | |
14295 OP *o; | |
14296 | |
14297 PERL_ARGS_ASSERT_CORESUB_OP; | |
14298 | |
14299 switch(opnum) { | |
14300 case 0: | |
14301 return op_append_elem(OP_LINESEQ, | |
14302 argop, | |
14303 newSLICEOP(0, | |
14304 newSVOP(OP_CONST, 0, newSViv(-code % 3)), | |
14305 newOP(OP_CALLER,0) | |
14306 ) | |
14307 ); | |
14308 case OP_SELECT: /* which represents OP_SSELECT as well */ | |
14309 if (code) | |
14310 return newCONDOP( | |
14311 0, | |
14312 newBINOP(OP_GT, 0, | |
14313 newAVREF(newGVOP(OP_GV, 0, PL_defgv)), | |
14314 newSVOP(OP_CONST, 0, newSVuv(1)) | |
14315 ), | |
14316 coresub_op(newSVuv((UV)OP_SSELECT), 0, | |
14317 OP_SSELECT), | |
14318 coresub_op(coreargssv, 0, OP_SELECT) | |
14319 ); | |
14320 /* FALLTHROUGH */ | |
14321 default: | |
14322 switch (PL_opargs[opnum] & OA_CLASS_MASK) { | |
14323 case OA_BASEOP: | |
14324 return op_append_elem( | |
14325 OP_LINESEQ, argop, | |
14326 newOP(opnum, | |
14327 opnum == OP_WANTARRAY || opnum == OP_RUNCV | |
14328 ? OPpOFFBYONE << 8 : 0) | |
14329 ); | |
14330 case OA_BASEOP_OR_UNOP: | |
14331 if (opnum == OP_ENTEREVAL) { | |
14332 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop); | |
14333 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES; | |
14334 } | |
14335 else o = newUNOP(opnum,0,argop); | |
14336 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE; | |
14337 else { | |
14338 onearg: | |
14339 if (is_handle_constructor(o, 1)) | |
14340 argop->op_private |= OPpCOREARGS_DEREF1; | |
14341 if (scalar_mod_type(NULL, opnum)) | |
14342 argop->op_private |= OPpCOREARGS_SCALARMOD; | |
14343 } | |
14344 return o; | |
14345 default: | |
14346 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop); | |
14347 if (is_handle_constructor(o, 2)) | |
14348 argop->op_private |= OPpCOREARGS_DEREF2; | |
14349 if (opnum == OP_SUBSTR) { | |
14350 o->op_private |= OPpMAYBE_LVSUB; | |
14351 return o; | |
14352 } | |
14353 else goto onearg; | |
14354 } | |
14355 } | |
14356 } | |
14357 | |
14358 void | |
14359 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv, | |
14360 SV * const *new_const_svp) | |
14361 { | |
14362 const char *hvname; | |
14363 bool is_const = !!CvCONST(old_cv); | |
14364 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL; | |
14365 | |
14366 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV; | |
14367 | |
14368 if (is_const && new_const_svp && old_const_sv == *new_const_svp) | |
14369 return; | |
14370 /* They are 2 constant subroutines generated from | |
14371 the same constant. This probably means that | |
14372 they are really the "same" proxy subroutine | |
14373 instantiated in 2 places. Most likely this is | |
14374 when a constant is exported twice. Don't warn. | |
14375 */ | |
14376 if ( | |
14377 (ckWARN(WARN_REDEFINE) | |
14378 && !( | |
14379 CvGV(old_cv) && GvSTASH(CvGV(old_cv)) | |
14380 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7 | |
14381 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))), | |
14382 strEQ(hvname, "autouse")) | |
14383 ) | |
14384 ) | |
14385 || (is_const | |
14386 && ckWARN_d(WARN_REDEFINE) | |
14387 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp)) | |
14388 ) | |
14389 ) | |
14390 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), | |
14391 is_const | |
14392 ? "Constant subroutine %"SVf" redefined" | |
14393 : "Subroutine %"SVf" redefined", | |
14394 SVfARG(name)); | |
14395 } | |
14396 | |
14397 /* | |
14398 =head1 Hook manipulation | |
14399 | |
14400 These functions provide convenient and thread-safe means of manipulating | |
14401 hook variables. | |
14402 | |
14403 =cut | |
14404 */ | |
14405 | |
14406 /* | |
14407 =for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p | |
14408 | |
14409 Puts a C function into the chain of check functions for a specified op | |
14410 type. This is the preferred way to manipulate the L</PL_check> array. | |
14411 I<opcode> specifies which type of op is to be affected. I<new_checker> | |
14412 is a pointer to the C function that is to be added to that opcode's | |
14413 check chain, and I<old_checker_p> points to the storage location where a | |
14414 pointer to the next function in the chain will be stored. The value of | |
14415 I<new_pointer> is written into the L</PL_check> array, while the value | |
14416 previously stored there is written to I<*old_checker_p>. | |
14417 | |
14418 The function should be defined like this: | |
14419 | |
14420 static OP *new_checker(pTHX_ OP *op) { ... } | |
14421 | |
14422 It is intended to be called in this manner: | |
14423 | |
14424 new_checker(aTHX_ op) | |
14425 | |
14426 I<old_checker_p> should be defined like this: | |
14427 | |
14428 static Perl_check_t old_checker_p; | |
14429 | |
14430 L</PL_check> is global to an entire process, and a module wishing to | |
14431 hook op checking may find itself invoked more than once per process, | |
14432 typically in different threads. To handle that situation, this function | |
14433 is idempotent. The location I<*old_checker_p> must initially (once | |
14434 per process) contain a null pointer. A C variable of static duration | |
14435 (declared at file scope, typically also marked C<static> to give | |
14436 it internal linkage) will be implicitly initialised appropriately, | |
14437 if it does not have an explicit initialiser. This function will only | |
14438 actually modify the check chain if it finds I<*old_checker_p> to be null. | |
14439 This function is also thread safe on the small scale. It uses appropriate | |
14440 locking to avoid race conditions in accessing L</PL_check>. | |
14441 | |
14442 When this function is called, the function referenced by I<new_checker> | |
14443 must be ready to be called, except for I<*old_checker_p> being unfilled. | |
14444 In a threading situation, I<new_checker> may be called immediately, | |
14445 even before this function has returned. I<*old_checker_p> will always | |
14446 be appropriately set before I<new_checker> is called. If I<new_checker> | |
14447 decides not to do anything special with an op that it is given (which | |
14448 is the usual case for most uses of op check hooking), it must chain the | |
14449 check function referenced by I<*old_checker_p>. | |
14450 | |
14451 If you want to influence compilation of calls to a specific subroutine, | |
14452 then use L</cv_set_call_checker> rather than hooking checking of all | |
14453 C<entersub> ops. | |
14454 | |
14455 =cut | |
14456 */ | |
14457 | |
14458 void | |
14459 Perl_wrap_op_checker(pTHX_ Optype opcode, | |
14460 Perl_check_t new_checker, Perl_check_t *old_checker_p) | |
14461 { | |
14462 dVAR; | |
14463 | |
14464 PERL_UNUSED_CONTEXT; | |
14465 PERL_ARGS_ASSERT_WRAP_OP_CHECKER; | |
14466 if (*old_checker_p) return; | |
14467 OP_CHECK_MUTEX_LOCK; | |
14468 if (!*old_checker_p) { | |
14469 *old_checker_p = PL_check[opcode]; | |
14470 PL_check[opcode] = new_checker; | |
14471 } | |
14472 OP_CHECK_MUTEX_UNLOCK; | |
14473 } | |
14474 | |
14475 #include "XSUB.h" | |
14476 | |
14477 /* Efficient sub that returns a constant scalar value. */ | |
14478 static void | |
14479 const_sv_xsub(pTHX_ CV* cv) | |
14480 { | |
14481 dXSARGS; | |
14482 SV *const sv = MUTABLE_SV(XSANY.any_ptr); | |
14483 PERL_UNUSED_ARG(items); | |
14484 if (!sv) { | |
14485 XSRETURN(0); | |
14486 } | |
14487 EXTEND(sp, 1); | |
14488 ST(0) = sv; | |
14489 XSRETURN(1); | |
14490 } | |
14491 | |
14492 static void | |
14493 const_av_xsub(pTHX_ CV* cv) | |
14494 { | |
14495 dXSARGS; | |
14496 AV * const av = MUTABLE_AV(XSANY.any_ptr); | |
14497 SP -= items; | |
14498 assert(av); | |
14499 #ifndef DEBUGGING | |
14500 if (!av) { | |
14501 XSRETURN(0); | |
14502 } | |
14503 #endif | |
14504 if (SvRMAGICAL(av)) | |
14505 Perl_croak(aTHX_ "Magical list constants are not supported"); | |
14506 if (GIMME_V != G_ARRAY) { | |
14507 EXTEND(SP, 1); | |
14508 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1)); | |
14509 XSRETURN(1); | |
14510 } | |
14511 EXTEND(SP, AvFILLp(av)+1); | |
14512 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *); | |
14513 XSRETURN(AvFILLp(av)+1); | |
14514 } | |
14515 | |
14516 /* | |
14517 * ex: set ts=8 sts=4 sw=4 et: | |
14518 */ |