This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix range op under aborted constant folding
[perl5.git] / op.c
... / ...
CommitLineData
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 * Note that during the build of miniperl, a temporary copy of this file
26 * is made, called opmini.c.
27 *
28 * A Perl program is compiled into a tree of OP nodes. Each op contains:
29 * * structural OP pointers to its children and siblings (op_sibling,
30 * op_first etc) that define the tree structure;
31 * * execution order OP pointers (op_next, plus sometimes op_other,
32 * op_lastop etc) that define the execution sequence plus variants;
33 * * a pointer to the C "pp" function that would execute the op;
34 * * any data specific to that op.
35 * For example, an OP_CONST op points to the pp_const() function and to an
36 * SV containing the constant value. When pp_const() is executed, its job
37 * is to push that SV onto the stack.
38 *
39 * OPs are mainly created by the newFOO() functions, which are mainly
40 * called from the parser (in perly.y) as the code is parsed. For example
41 * the Perl code $a + $b * $c would cause the equivalent of the following
42 * to be called (oversimplifying a bit):
43 *
44 * newBINOP(OP_ADD, flags,
45 * newSVREF($a),
46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
47 * )
48 *
49 * As the parser reduces low-level rules, it creates little op subtrees;
50 * as higher-level rules are resolved, these subtrees get joined together
51 * as branches on a bigger subtree, until eventually a top-level rule like
52 * a subroutine definition is reduced, at which point there is one large
53 * parse tree left.
54 *
55 * The execution order pointers (op_next) are generated as the subtrees
56 * are joined together. Consider this sub-expression: A*B + C/D: at the
57 * point when it's just been parsed, the op tree looks like:
58 *
59 * [+]
60 * |
61 * [*]------[/]
62 * | |
63 * A---B C---D
64 *
65 * with the intended execution order being:
66 *
67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
68 *
69 * At this point all the nodes' op_next pointers will have been set,
70 * except that:
71 * * we don't know what the [NEXT] node will be yet;
72 * * we don't know what the [PREV] node will be yet, but when it gets
73 * created and needs its op_next set, it needs to be set to point to
74 * A, which is non-obvious.
75 * To handle both those cases, we temporarily set the top node's
76 * op_next to point to the first node to be executed in this subtree (A in
77 * this case). This means that initially a subtree's op_next chain,
78 * starting from the top node, will visit each node in execution sequence
79 * then point back at the top node.
80 * When we embed this subtree in a larger tree, its top op_next is used
81 * to get the start node, then is set to point to its new neighbour.
82 * For example the two separate [*],A,B and [/],C,D subtrees would
83 * initially have had:
84 * [*] => A; A => B; B => [*]
85 * and
86 * [/] => C; C => D; D => [/]
87 * When these two subtrees were joined together to make the [+] subtree,
88 * [+]'s op_next was set to [*]'s op_next, i.e. A; then [*]'s op_next was
89 * set to point to [/]'s op_next, i.e. C.
90 *
91 * This op_next linking is done by the LINKLIST() macro and its underlying
92 * op_linklist() function. Given a top-level op, if its op_next is
93 * non-null, it's already been linked, so leave it. Otherwise link it with
94 * its children as described above, possibly recursively if any of the
95 * children have a null op_next.
96 *
97 * In summary: given a subtree, its top-level node's op_next will either
98 * be:
99 * NULL: the subtree hasn't been LINKLIST()ed yet;
100 * fake: points to the start op for this subtree;
101 * real: once the subtree has been embedded into a larger tree
102 */
103
104/*
105
106Here's an older description from Larry.
107
108Perl's compiler is essentially a 3-pass compiler with interleaved phases:
109
110 A bottom-up pass
111 A top-down pass
112 An execution-order pass
113
114The bottom-up pass is represented by all the "newOP" routines and
115the ck_ routines. The bottom-upness is actually driven by yacc.
116So at the point that a ck_ routine fires, we have no idea what the
117context is, either upward in the syntax tree, or either forward or
118backward in the execution order. (The bottom-up parser builds that
119part of the execution order it knows about, but if you follow the "next"
120links around, you'll find it's actually a closed loop through the
121top level node.)
122
123Whenever the bottom-up parser gets to a node that supplies context to
124its components, it invokes that portion of the top-down pass that applies
125to that part of the subtree (and marks the top node as processed, so
126if a node further up supplies context, it doesn't have to take the
127plunge again). As a particular subcase of this, as the new node is
128built, it takes all the closed execution loops of its subcomponents
129and links them into a new closed loop for the higher level node. But
130it's still not the real execution order.
131
132The actual execution order is not known till we get a grammar reduction
133to a top-level unit like a subroutine or file that will be called by
134"name" rather than via a "next" pointer. At that point, we can call
135into peep() to do that code's portion of the 3rd pass. It has to be
136recursive, but it's recursive on basic blocks, not on tree nodes.
137*/
138
139/* To implement user lexical pragmas, there needs to be a way at run time to
140 get the compile time state of %^H for that block. Storing %^H in every
141 block (or even COP) would be very expensive, so a different approach is
142 taken. The (running) state of %^H is serialised into a tree of HE-like
143 structs. Stores into %^H are chained onto the current leaf as a struct
144 refcounted_he * with the key and the value. Deletes from %^H are saved
145 with a value of PL_sv_placeholder. The state of %^H at any point can be
146 turned back into a regular HV by walking back up the tree from that point's
147 leaf, ignoring any key you've already seen (placeholder or not), storing
148 the rest into the HV structure, then removing the placeholders. Hence
149 memory is only used to store the %^H deltas from the enclosing COP, rather
150 than the entire %^H on each COP.
151
152 To cause actions on %^H to write out the serialisation records, it has
153 magic type 'H'. This magic (itself) does nothing, but its presence causes
154 the values to gain magic type 'h', which has entries for set and clear.
155 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
156 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
157 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
158 it will be correctly restored when any inner compiling scope is exited.
159*/
160
161#include "EXTERN.h"
162#define PERL_IN_OP_C
163#include "perl.h"
164#include "keywords.h"
165#include "feature.h"
166#include "regcomp.h"
167
168#define CALL_PEEP(o) PL_peepp(aTHX_ o)
169#define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
170#define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
171
172static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
173
174/* Used to avoid recursion through the op tree in scalarvoid() and
175 op_free()
176*/
177
178#define DEFERRED_OP_STEP 100
179#define DEFER_OP(o) \
180 STMT_START { \
181 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \
182 defer_stack_alloc += DEFERRED_OP_STEP; \
183 assert(defer_stack_alloc > 0); \
184 Renew(defer_stack, defer_stack_alloc, OP *); \
185 } \
186 defer_stack[++defer_ix] = o; \
187 } STMT_END
188
189#define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
190
191/* remove any leading "empty" ops from the op_next chain whose first
192 * node's address is stored in op_p. Store the updated address of the
193 * first node in op_p.
194 */
195
196STATIC void
197S_prune_chain_head(OP** op_p)
198{
199 while (*op_p
200 && ( (*op_p)->op_type == OP_NULL
201 || (*op_p)->op_type == OP_SCOPE
202 || (*op_p)->op_type == OP_SCALAR
203 || (*op_p)->op_type == OP_LINESEQ)
204 )
205 *op_p = (*op_p)->op_next;
206}
207
208
209/* See the explanatory comments above struct opslab in op.h. */
210
211#ifdef PERL_DEBUG_READONLY_OPS
212# define PERL_SLAB_SIZE 128
213# define PERL_MAX_SLAB_SIZE 4096
214# include <sys/mman.h>
215#endif
216
217#ifndef PERL_SLAB_SIZE
218# define PERL_SLAB_SIZE 64
219#endif
220#ifndef PERL_MAX_SLAB_SIZE
221# define PERL_MAX_SLAB_SIZE 2048
222#endif
223
224/* rounds up to nearest pointer */
225#define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
226#define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
227
228static OPSLAB *
229S_new_slab(pTHX_ size_t sz)
230{
231#ifdef PERL_DEBUG_READONLY_OPS
232 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
233 PROT_READ|PROT_WRITE,
234 MAP_ANON|MAP_PRIVATE, -1, 0);
235 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
236 (unsigned long) sz, slab));
237 if (slab == MAP_FAILED) {
238 perror("mmap failed");
239 abort();
240 }
241 slab->opslab_size = (U16)sz;
242#else
243 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
244#endif
245#ifndef WIN32
246 /* The context is unused in non-Windows */
247 PERL_UNUSED_CONTEXT;
248#endif
249 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
250 return slab;
251}
252
253/* requires double parens and aTHX_ */
254#define DEBUG_S_warn(args) \
255 DEBUG_S( \
256 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
257 )
258
259void *
260Perl_Slab_Alloc(pTHX_ size_t sz)
261{
262 OPSLAB *slab;
263 OPSLAB *slab2;
264 OPSLOT *slot;
265 OP *o;
266 size_t opsz, space;
267
268 /* We only allocate ops from the slab during subroutine compilation.
269 We find the slab via PL_compcv, hence that must be non-NULL. It could
270 also be pointing to a subroutine which is now fully set up (CvROOT()
271 pointing to the top of the optree for that sub), or a subroutine
272 which isn't using the slab allocator. If our sanity checks aren't met,
273 don't use a slab, but allocate the OP directly from the heap. */
274 if (!PL_compcv || CvROOT(PL_compcv)
275 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
276 {
277 o = (OP*)PerlMemShared_calloc(1, sz);
278 goto gotit;
279 }
280
281 /* While the subroutine is under construction, the slabs are accessed via
282 CvSTART(), to avoid needing to expand PVCV by one pointer for something
283 unneeded at runtime. Once a subroutine is constructed, the slabs are
284 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
285 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
286 details. */
287 if (!CvSTART(PL_compcv)) {
288 CvSTART(PL_compcv) =
289 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
290 CvSLABBED_on(PL_compcv);
291 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
292 }
293 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
294
295 opsz = SIZE_TO_PSIZE(sz);
296 sz = opsz + OPSLOT_HEADER_P;
297
298 /* The slabs maintain a free list of OPs. In particular, constant folding
299 will free up OPs, so it makes sense to re-use them where possible. A
300 freed up slot is used in preference to a new allocation. */
301 if (slab->opslab_freed) {
302 OP **too = &slab->opslab_freed;
303 o = *too;
304 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
305 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
306 DEBUG_S_warn((aTHX_ "Alas! too small"));
307 o = *(too = &o->op_next);
308 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
309 }
310 if (o) {
311 *too = o->op_next;
312 Zero(o, opsz, I32 *);
313 o->op_slabbed = 1;
314 goto gotit;
315 }
316 }
317
318#define INIT_OPSLOT \
319 slot->opslot_slab = slab; \
320 slot->opslot_next = slab2->opslab_first; \
321 slab2->opslab_first = slot; \
322 o = &slot->opslot_op; \
323 o->op_slabbed = 1
324
325 /* The partially-filled slab is next in the chain. */
326 slab2 = slab->opslab_next ? slab->opslab_next : slab;
327 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
328 /* Remaining space is too small. */
329
330 /* If we can fit a BASEOP, add it to the free chain, so as not
331 to waste it. */
332 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
333 slot = &slab2->opslab_slots;
334 INIT_OPSLOT;
335 o->op_type = OP_FREED;
336 o->op_next = slab->opslab_freed;
337 slab->opslab_freed = o;
338 }
339
340 /* Create a new slab. Make this one twice as big. */
341 slot = slab2->opslab_first;
342 while (slot->opslot_next) slot = slot->opslot_next;
343 slab2 = S_new_slab(aTHX_
344 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
345 ? PERL_MAX_SLAB_SIZE
346 : (DIFF(slab2, slot)+1)*2);
347 slab2->opslab_next = slab->opslab_next;
348 slab->opslab_next = slab2;
349 }
350 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
351
352 /* Create a new op slot */
353 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
354 assert(slot >= &slab2->opslab_slots);
355 if (DIFF(&slab2->opslab_slots, slot)
356 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
357 slot = &slab2->opslab_slots;
358 INIT_OPSLOT;
359 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
360
361 gotit:
362#ifdef PERL_OP_PARENT
363 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
364 assert(!o->op_moresib);
365 assert(!o->op_sibparent);
366#endif
367
368 return (void *)o;
369}
370
371#undef INIT_OPSLOT
372
373#ifdef PERL_DEBUG_READONLY_OPS
374void
375Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
376{
377 PERL_ARGS_ASSERT_SLAB_TO_RO;
378
379 if (slab->opslab_readonly) return;
380 slab->opslab_readonly = 1;
381 for (; slab; slab = slab->opslab_next) {
382 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
383 (unsigned long) slab->opslab_size, slab));*/
384 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
385 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
386 (unsigned long)slab->opslab_size, errno);
387 }
388}
389
390void
391Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
392{
393 OPSLAB *slab2;
394
395 PERL_ARGS_ASSERT_SLAB_TO_RW;
396
397 if (!slab->opslab_readonly) return;
398 slab2 = slab;
399 for (; slab2; slab2 = slab2->opslab_next) {
400 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
401 (unsigned long) size, slab2));*/
402 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
403 PROT_READ|PROT_WRITE)) {
404 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
405 (unsigned long)slab2->opslab_size, errno);
406 }
407 }
408 slab->opslab_readonly = 0;
409}
410
411#else
412# define Slab_to_rw(op) NOOP
413#endif
414
415/* This cannot possibly be right, but it was copied from the old slab
416 allocator, to which it was originally added, without explanation, in
417 commit 083fcd5. */
418#ifdef NETWARE
419# define PerlMemShared PerlMem
420#endif
421
422void
423Perl_Slab_Free(pTHX_ void *op)
424{
425 OP * const o = (OP *)op;
426 OPSLAB *slab;
427
428 PERL_ARGS_ASSERT_SLAB_FREE;
429
430 if (!o->op_slabbed) {
431 if (!o->op_static)
432 PerlMemShared_free(op);
433 return;
434 }
435
436 slab = OpSLAB(o);
437 /* If this op is already freed, our refcount will get screwy. */
438 assert(o->op_type != OP_FREED);
439 o->op_type = OP_FREED;
440 o->op_next = slab->opslab_freed;
441 slab->opslab_freed = o;
442 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
443 OpslabREFCNT_dec_padok(slab);
444}
445
446void
447Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
448{
449 const bool havepad = !!PL_comppad;
450 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
451 if (havepad) {
452 ENTER;
453 PAD_SAVE_SETNULLPAD();
454 }
455 opslab_free(slab);
456 if (havepad) LEAVE;
457}
458
459void
460Perl_opslab_free(pTHX_ OPSLAB *slab)
461{
462 OPSLAB *slab2;
463 PERL_ARGS_ASSERT_OPSLAB_FREE;
464 PERL_UNUSED_CONTEXT;
465 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
466 assert(slab->opslab_refcnt == 1);
467 do {
468 slab2 = slab->opslab_next;
469#ifdef DEBUGGING
470 slab->opslab_refcnt = ~(size_t)0;
471#endif
472#ifdef PERL_DEBUG_READONLY_OPS
473 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
474 (void*)slab));
475 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
476 perror("munmap failed");
477 abort();
478 }
479#else
480 PerlMemShared_free(slab);
481#endif
482 slab = slab2;
483 } while (slab);
484}
485
486void
487Perl_opslab_force_free(pTHX_ OPSLAB *slab)
488{
489 OPSLAB *slab2;
490 OPSLOT *slot;
491#ifdef DEBUGGING
492 size_t savestack_count = 0;
493#endif
494 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
495 slab2 = slab;
496 do {
497 for (slot = slab2->opslab_first;
498 slot->opslot_next;
499 slot = slot->opslot_next) {
500 if (slot->opslot_op.op_type != OP_FREED
501 && !(slot->opslot_op.op_savefree
502#ifdef DEBUGGING
503 && ++savestack_count
504#endif
505 )
506 ) {
507 assert(slot->opslot_op.op_slabbed);
508 op_free(&slot->opslot_op);
509 if (slab->opslab_refcnt == 1) goto free;
510 }
511 }
512 } while ((slab2 = slab2->opslab_next));
513 /* > 1 because the CV still holds a reference count. */
514 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
515#ifdef DEBUGGING
516 assert(savestack_count == slab->opslab_refcnt-1);
517#endif
518 /* Remove the CV’s reference count. */
519 slab->opslab_refcnt--;
520 return;
521 }
522 free:
523 opslab_free(slab);
524}
525
526#ifdef PERL_DEBUG_READONLY_OPS
527OP *
528Perl_op_refcnt_inc(pTHX_ OP *o)
529{
530 if(o) {
531 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
532 if (slab && slab->opslab_readonly) {
533 Slab_to_rw(slab);
534 ++o->op_targ;
535 Slab_to_ro(slab);
536 } else {
537 ++o->op_targ;
538 }
539 }
540 return o;
541
542}
543
544PADOFFSET
545Perl_op_refcnt_dec(pTHX_ OP *o)
546{
547 PADOFFSET result;
548 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
549
550 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
551
552 if (slab && slab->opslab_readonly) {
553 Slab_to_rw(slab);
554 result = --o->op_targ;
555 Slab_to_ro(slab);
556 } else {
557 result = --o->op_targ;
558 }
559 return result;
560}
561#endif
562/*
563 * In the following definition, the ", (OP*)0" is just to make the compiler
564 * think the expression is of the right type: croak actually does a Siglongjmp.
565 */
566#define CHECKOP(type,o) \
567 ((PL_op_mask && PL_op_mask[type]) \
568 ? ( op_free((OP*)o), \
569 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
570 (OP*)0 ) \
571 : PL_check[type](aTHX_ (OP*)o))
572
573#define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
574
575#define OpTYPE_set(o,type) \
576 STMT_START { \
577 o->op_type = (OPCODE)type; \
578 o->op_ppaddr = PL_ppaddr[type]; \
579 } STMT_END
580
581STATIC OP *
582S_no_fh_allowed(pTHX_ OP *o)
583{
584 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
585
586 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
587 OP_DESC(o)));
588 return o;
589}
590
591STATIC OP *
592S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
593{
594 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
595 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
596 return o;
597}
598
599STATIC OP *
600S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
601{
602 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
603
604 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
605 return o;
606}
607
608STATIC void
609S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
610{
611 PERL_ARGS_ASSERT_BAD_TYPE_PV;
612
613 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
614 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
615}
616
617/* remove flags var, its unused in all callers, move to to right end since gv
618 and kid are always the same */
619STATIC void
620S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
621{
622 SV * const namesv = cv_name((CV *)gv, NULL, 0);
623 PERL_ARGS_ASSERT_BAD_TYPE_GV;
624
625 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
626 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
627}
628
629STATIC void
630S_no_bareword_allowed(pTHX_ OP *o)
631{
632 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
633
634 qerror(Perl_mess(aTHX_
635 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
636 SVfARG(cSVOPo_sv)));
637 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
638}
639
640/* "register" allocation */
641
642PADOFFSET
643Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
644{
645 PADOFFSET off;
646 const bool is_our = (PL_parser->in_my == KEY_our);
647
648 PERL_ARGS_ASSERT_ALLOCMY;
649
650 if (flags & ~SVf_UTF8)
651 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
652 (UV)flags);
653
654 /* complain about "my $<special_var>" etc etc */
655 if ( len
656 && !( is_our
657 || isALPHA(name[1])
658 || ( (flags & SVf_UTF8)
659 && isIDFIRST_utf8_safe((U8 *)name+1, name + len))
660 || (name[1] == '_' && len > 2)))
661 {
662 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
663 && isASCII(name[1])
664 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
665 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
666 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
667 PL_parser->in_my == KEY_state ? "state" : "my"));
668 } else {
669 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
670 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
671 }
672 }
673
674 /* allocate a spare slot and store the name in that slot */
675
676 off = pad_add_name_pvn(name, len,
677 (is_our ? padadd_OUR :
678 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
679 PL_parser->in_my_stash,
680 (is_our
681 /* $_ is always in main::, even with our */
682 ? (PL_curstash && !memEQs(name,len,"$_")
683 ? PL_curstash
684 : PL_defstash)
685 : NULL
686 )
687 );
688 /* anon sub prototypes contains state vars should always be cloned,
689 * otherwise the state var would be shared between anon subs */
690
691 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
692 CvCLONE_on(PL_compcv);
693
694 return off;
695}
696
697/*
698=head1 Optree Manipulation Functions
699
700=for apidoc alloccopstash
701
702Available only under threaded builds, this function allocates an entry in
703C<PL_stashpad> for the stash passed to it.
704
705=cut
706*/
707
708#ifdef USE_ITHREADS
709PADOFFSET
710Perl_alloccopstash(pTHX_ HV *hv)
711{
712 PADOFFSET off = 0, o = 1;
713 bool found_slot = FALSE;
714
715 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
716
717 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
718
719 for (; o < PL_stashpadmax; ++o) {
720 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
721 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
722 found_slot = TRUE, off = o;
723 }
724 if (!found_slot) {
725 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
726 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
727 off = PL_stashpadmax;
728 PL_stashpadmax += 10;
729 }
730
731 PL_stashpad[PL_stashpadix = off] = hv;
732 return off;
733}
734#endif
735
736/* free the body of an op without examining its contents.
737 * Always use this rather than FreeOp directly */
738
739static void
740S_op_destroy(pTHX_ OP *o)
741{
742 FreeOp(o);
743}
744
745/* Destructor */
746
747/*
748=for apidoc Am|void|op_free|OP *o
749
750Free an op. Only use this when an op is no longer linked to from any
751optree.
752
753=cut
754*/
755
756void
757Perl_op_free(pTHX_ OP *o)
758{
759 dVAR;
760 OPCODE type;
761 SSize_t defer_ix = -1;
762 SSize_t defer_stack_alloc = 0;
763 OP **defer_stack = NULL;
764
765 do {
766
767 /* Though ops may be freed twice, freeing the op after its slab is a
768 big no-no. */
769 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
770 /* During the forced freeing of ops after compilation failure, kidops
771 may be freed before their parents. */
772 if (!o || o->op_type == OP_FREED)
773 continue;
774
775 type = o->op_type;
776
777 /* an op should only ever acquire op_private flags that we know about.
778 * If this fails, you may need to fix something in regen/op_private.
779 * Don't bother testing if:
780 * * the op_ppaddr doesn't match the op; someone may have
781 * overridden the op and be doing strange things with it;
782 * * we've errored, as op flags are often left in an
783 * inconsistent state then. Note that an error when
784 * compiling the main program leaves PL_parser NULL, so
785 * we can't spot faults in the main code, only
786 * evaled/required code */
787#ifdef DEBUGGING
788 if ( o->op_ppaddr == PL_ppaddr[o->op_type]
789 && PL_parser
790 && !PL_parser->error_count)
791 {
792 assert(!(o->op_private & ~PL_op_private_valid[type]));
793 }
794#endif
795
796 if (o->op_private & OPpREFCOUNTED) {
797 switch (type) {
798 case OP_LEAVESUB:
799 case OP_LEAVESUBLV:
800 case OP_LEAVEEVAL:
801 case OP_LEAVE:
802 case OP_SCOPE:
803 case OP_LEAVEWRITE:
804 {
805 PADOFFSET refcnt;
806 OP_REFCNT_LOCK;
807 refcnt = OpREFCNT_dec(o);
808 OP_REFCNT_UNLOCK;
809 if (refcnt) {
810 /* Need to find and remove any pattern match ops from the list
811 we maintain for reset(). */
812 find_and_forget_pmops(o);
813 continue;
814 }
815 }
816 break;
817 default:
818 break;
819 }
820 }
821
822 /* Call the op_free hook if it has been set. Do it now so that it's called
823 * at the right time for refcounted ops, but still before all of the kids
824 * are freed. */
825 CALL_OPFREEHOOK(o);
826
827 if (o->op_flags & OPf_KIDS) {
828 OP *kid, *nextkid;
829 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
830 nextkid = OpSIBLING(kid); /* Get before next freeing kid */
831 if (!kid || kid->op_type == OP_FREED)
832 /* During the forced freeing of ops after
833 compilation failure, kidops may be freed before
834 their parents. */
835 continue;
836 if (!(kid->op_flags & OPf_KIDS))
837 /* If it has no kids, just free it now */
838 op_free(kid);
839 else
840 DEFER_OP(kid);
841 }
842 }
843 if (type == OP_NULL)
844 type = (OPCODE)o->op_targ;
845
846 if (o->op_slabbed)
847 Slab_to_rw(OpSLAB(o));
848
849 /* COP* is not cleared by op_clear() so that we may track line
850 * numbers etc even after null() */
851 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
852 cop_free((COP*)o);
853 }
854
855 op_clear(o);
856 FreeOp(o);
857 if (PL_op == o)
858 PL_op = NULL;
859 } while ( (o = POP_DEFERRED_OP()) );
860
861 Safefree(defer_stack);
862}
863
864/* S_op_clear_gv(): free a GV attached to an OP */
865
866STATIC
867#ifdef USE_ITHREADS
868void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
869#else
870void S_op_clear_gv(pTHX_ OP *o, SV**svp)
871#endif
872{
873
874 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
875 || o->op_type == OP_MULTIDEREF)
876#ifdef USE_ITHREADS
877 && PL_curpad
878 ? ((GV*)PAD_SVl(*ixp)) : NULL;
879#else
880 ? (GV*)(*svp) : NULL;
881#endif
882 /* It's possible during global destruction that the GV is freed
883 before the optree. Whilst the SvREFCNT_inc is happy to bump from
884 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
885 will trigger an assertion failure, because the entry to sv_clear
886 checks that the scalar is not already freed. A check of for
887 !SvIS_FREED(gv) turns out to be invalid, because during global
888 destruction the reference count can be forced down to zero
889 (with SVf_BREAK set). In which case raising to 1 and then
890 dropping to 0 triggers cleanup before it should happen. I
891 *think* that this might actually be a general, systematic,
892 weakness of the whole idea of SVf_BREAK, in that code *is*
893 allowed to raise and lower references during global destruction,
894 so any *valid* code that happens to do this during global
895 destruction might well trigger premature cleanup. */
896 bool still_valid = gv && SvREFCNT(gv);
897
898 if (still_valid)
899 SvREFCNT_inc_simple_void(gv);
900#ifdef USE_ITHREADS
901 if (*ixp > 0) {
902 pad_swipe(*ixp, TRUE);
903 *ixp = 0;
904 }
905#else
906 SvREFCNT_dec(*svp);
907 *svp = NULL;
908#endif
909 if (still_valid) {
910 int try_downgrade = SvREFCNT(gv) == 2;
911 SvREFCNT_dec_NN(gv);
912 if (try_downgrade)
913 gv_try_downgrade(gv);
914 }
915}
916
917
918void
919Perl_op_clear(pTHX_ OP *o)
920{
921
922 dVAR;
923
924 PERL_ARGS_ASSERT_OP_CLEAR;
925
926 switch (o->op_type) {
927 case OP_NULL: /* Was holding old type, if any. */
928 /* FALLTHROUGH */
929 case OP_ENTERTRY:
930 case OP_ENTEREVAL: /* Was holding hints. */
931 case OP_ARGDEFELEM: /* Was holding signature index. */
932 o->op_targ = 0;
933 break;
934 default:
935 if (!(o->op_flags & OPf_REF)
936 || (PL_check[o->op_type] != Perl_ck_ftst))
937 break;
938 /* FALLTHROUGH */
939 case OP_GVSV:
940 case OP_GV:
941 case OP_AELEMFAST:
942#ifdef USE_ITHREADS
943 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
944#else
945 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
946#endif
947 break;
948 case OP_METHOD_REDIR:
949 case OP_METHOD_REDIR_SUPER:
950#ifdef USE_ITHREADS
951 if (cMETHOPx(o)->op_rclass_targ) {
952 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
953 cMETHOPx(o)->op_rclass_targ = 0;
954 }
955#else
956 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
957 cMETHOPx(o)->op_rclass_sv = NULL;
958#endif
959 case OP_METHOD_NAMED:
960 case OP_METHOD_SUPER:
961 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
962 cMETHOPx(o)->op_u.op_meth_sv = NULL;
963#ifdef USE_ITHREADS
964 if (o->op_targ) {
965 pad_swipe(o->op_targ, 1);
966 o->op_targ = 0;
967 }
968#endif
969 break;
970 case OP_CONST:
971 case OP_HINTSEVAL:
972 SvREFCNT_dec(cSVOPo->op_sv);
973 cSVOPo->op_sv = NULL;
974#ifdef USE_ITHREADS
975 /** Bug #15654
976 Even if op_clear does a pad_free for the target of the op,
977 pad_free doesn't actually remove the sv that exists in the pad;
978 instead it lives on. This results in that it could be reused as
979 a target later on when the pad was reallocated.
980 **/
981 if(o->op_targ) {
982 pad_swipe(o->op_targ,1);
983 o->op_targ = 0;
984 }
985#endif
986 break;
987 case OP_DUMP:
988 case OP_GOTO:
989 case OP_NEXT:
990 case OP_LAST:
991 case OP_REDO:
992 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
993 break;
994 /* FALLTHROUGH */
995 case OP_TRANS:
996 case OP_TRANSR:
997 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR)
998 && (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)))
999 {
1000#ifdef USE_ITHREADS
1001 if (cPADOPo->op_padix > 0) {
1002 pad_swipe(cPADOPo->op_padix, TRUE);
1003 cPADOPo->op_padix = 0;
1004 }
1005#else
1006 SvREFCNT_dec(cSVOPo->op_sv);
1007 cSVOPo->op_sv = NULL;
1008#endif
1009 }
1010 else {
1011 PerlMemShared_free(cPVOPo->op_pv);
1012 cPVOPo->op_pv = NULL;
1013 }
1014 break;
1015 case OP_SUBST:
1016 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1017 goto clear_pmop;
1018
1019 case OP_SPLIT:
1020 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1021 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1022 {
1023 if (o->op_private & OPpSPLIT_LEX)
1024 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1025 else
1026#ifdef USE_ITHREADS
1027 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1028#else
1029 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1030#endif
1031 }
1032 /* FALLTHROUGH */
1033 case OP_MATCH:
1034 case OP_QR:
1035 clear_pmop:
1036 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1037 op_free(cPMOPo->op_code_list);
1038 cPMOPo->op_code_list = NULL;
1039 forget_pmop(cPMOPo);
1040 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1041 /* we use the same protection as the "SAFE" version of the PM_ macros
1042 * here since sv_clean_all might release some PMOPs
1043 * after PL_regex_padav has been cleared
1044 * and the clearing of PL_regex_padav needs to
1045 * happen before sv_clean_all
1046 */
1047#ifdef USE_ITHREADS
1048 if(PL_regex_pad) { /* We could be in destruction */
1049 const IV offset = (cPMOPo)->op_pmoffset;
1050 ReREFCNT_dec(PM_GETRE(cPMOPo));
1051 PL_regex_pad[offset] = &PL_sv_undef;
1052 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1053 sizeof(offset));
1054 }
1055#else
1056 ReREFCNT_dec(PM_GETRE(cPMOPo));
1057 PM_SETRE(cPMOPo, NULL);
1058#endif
1059
1060 break;
1061
1062 case OP_ARGCHECK:
1063 PerlMemShared_free(cUNOP_AUXo->op_aux);
1064 break;
1065
1066 case OP_MULTIDEREF:
1067 {
1068 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1069 UV actions = items->uv;
1070 bool last = 0;
1071 bool is_hash = FALSE;
1072
1073 while (!last) {
1074 switch (actions & MDEREF_ACTION_MASK) {
1075
1076 case MDEREF_reload:
1077 actions = (++items)->uv;
1078 continue;
1079
1080 case MDEREF_HV_padhv_helem:
1081 is_hash = TRUE;
1082 case MDEREF_AV_padav_aelem:
1083 pad_free((++items)->pad_offset);
1084 goto do_elem;
1085
1086 case MDEREF_HV_gvhv_helem:
1087 is_hash = TRUE;
1088 case MDEREF_AV_gvav_aelem:
1089#ifdef USE_ITHREADS
1090 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1091#else
1092 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1093#endif
1094 goto do_elem;
1095
1096 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1097 is_hash = TRUE;
1098 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1099#ifdef USE_ITHREADS
1100 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1101#else
1102 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1103#endif
1104 goto do_vivify_rv2xv_elem;
1105
1106 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1107 is_hash = TRUE;
1108 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1109 pad_free((++items)->pad_offset);
1110 goto do_vivify_rv2xv_elem;
1111
1112 case MDEREF_HV_pop_rv2hv_helem:
1113 case MDEREF_HV_vivify_rv2hv_helem:
1114 is_hash = TRUE;
1115 do_vivify_rv2xv_elem:
1116 case MDEREF_AV_pop_rv2av_aelem:
1117 case MDEREF_AV_vivify_rv2av_aelem:
1118 do_elem:
1119 switch (actions & MDEREF_INDEX_MASK) {
1120 case MDEREF_INDEX_none:
1121 last = 1;
1122 break;
1123 case MDEREF_INDEX_const:
1124 if (is_hash) {
1125#ifdef USE_ITHREADS
1126 /* see RT #15654 */
1127 pad_swipe((++items)->pad_offset, 1);
1128#else
1129 SvREFCNT_dec((++items)->sv);
1130#endif
1131 }
1132 else
1133 items++;
1134 break;
1135 case MDEREF_INDEX_padsv:
1136 pad_free((++items)->pad_offset);
1137 break;
1138 case MDEREF_INDEX_gvsv:
1139#ifdef USE_ITHREADS
1140 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1141#else
1142 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1143#endif
1144 break;
1145 }
1146
1147 if (actions & MDEREF_FLAG_last)
1148 last = 1;
1149 is_hash = FALSE;
1150
1151 break;
1152
1153 default:
1154 assert(0);
1155 last = 1;
1156 break;
1157
1158 } /* switch */
1159
1160 actions >>= MDEREF_SHIFT;
1161 } /* while */
1162
1163 /* start of malloc is at op_aux[-1], where the length is
1164 * stored */
1165 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1166 }
1167 break;
1168 }
1169
1170 if (o->op_targ > 0) {
1171 pad_free(o->op_targ);
1172 o->op_targ = 0;
1173 }
1174}
1175
1176STATIC void
1177S_cop_free(pTHX_ COP* cop)
1178{
1179 PERL_ARGS_ASSERT_COP_FREE;
1180
1181 CopFILE_free(cop);
1182 if (! specialWARN(cop->cop_warnings))
1183 PerlMemShared_free(cop->cop_warnings);
1184 cophh_free(CopHINTHASH_get(cop));
1185 if (PL_curcop == cop)
1186 PL_curcop = NULL;
1187}
1188
1189STATIC void
1190S_forget_pmop(pTHX_ PMOP *const o
1191 )
1192{
1193 HV * const pmstash = PmopSTASH(o);
1194
1195 PERL_ARGS_ASSERT_FORGET_PMOP;
1196
1197 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1198 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1199 if (mg) {
1200 PMOP **const array = (PMOP**) mg->mg_ptr;
1201 U32 count = mg->mg_len / sizeof(PMOP**);
1202 U32 i = count;
1203
1204 while (i--) {
1205 if (array[i] == o) {
1206 /* Found it. Move the entry at the end to overwrite it. */
1207 array[i] = array[--count];
1208 mg->mg_len = count * sizeof(PMOP**);
1209 /* Could realloc smaller at this point always, but probably
1210 not worth it. Probably worth free()ing if we're the
1211 last. */
1212 if(!count) {
1213 Safefree(mg->mg_ptr);
1214 mg->mg_ptr = NULL;
1215 }
1216 break;
1217 }
1218 }
1219 }
1220 }
1221 if (PL_curpm == o)
1222 PL_curpm = NULL;
1223}
1224
1225STATIC void
1226S_find_and_forget_pmops(pTHX_ OP *o)
1227{
1228 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1229
1230 if (o->op_flags & OPf_KIDS) {
1231 OP *kid = cUNOPo->op_first;
1232 while (kid) {
1233 switch (kid->op_type) {
1234 case OP_SUBST:
1235 case OP_SPLIT:
1236 case OP_MATCH:
1237 case OP_QR:
1238 forget_pmop((PMOP*)kid);
1239 }
1240 find_and_forget_pmops(kid);
1241 kid = OpSIBLING(kid);
1242 }
1243 }
1244}
1245
1246/*
1247=for apidoc Am|void|op_null|OP *o
1248
1249Neutralizes an op when it is no longer needed, but is still linked to from
1250other ops.
1251
1252=cut
1253*/
1254
1255void
1256Perl_op_null(pTHX_ OP *o)
1257{
1258 dVAR;
1259
1260 PERL_ARGS_ASSERT_OP_NULL;
1261
1262 if (o->op_type == OP_NULL)
1263 return;
1264 op_clear(o);
1265 o->op_targ = o->op_type;
1266 OpTYPE_set(o, OP_NULL);
1267}
1268
1269void
1270Perl_op_refcnt_lock(pTHX)
1271 PERL_TSA_ACQUIRE(PL_op_mutex)
1272{
1273#ifdef USE_ITHREADS
1274 dVAR;
1275#endif
1276 PERL_UNUSED_CONTEXT;
1277 OP_REFCNT_LOCK;
1278}
1279
1280void
1281Perl_op_refcnt_unlock(pTHX)
1282 PERL_TSA_RELEASE(PL_op_mutex)
1283{
1284#ifdef USE_ITHREADS
1285 dVAR;
1286#endif
1287 PERL_UNUSED_CONTEXT;
1288 OP_REFCNT_UNLOCK;
1289}
1290
1291
1292/*
1293=for apidoc op_sibling_splice
1294
1295A general function for editing the structure of an existing chain of
1296op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1297you to delete zero or more sequential nodes, replacing them with zero or
1298more different nodes. Performs the necessary op_first/op_last
1299housekeeping on the parent node and op_sibling manipulation on the
1300children. The last deleted node will be marked as as the last node by
1301updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1302
1303Note that op_next is not manipulated, and nodes are not freed; that is the
1304responsibility of the caller. It also won't create a new list op for an
1305empty list etc; use higher-level functions like op_append_elem() for that.
1306
1307C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1308the splicing doesn't affect the first or last op in the chain.
1309
1310C<start> is the node preceding the first node to be spliced. Node(s)
1311following it will be deleted, and ops will be inserted after it. If it is
1312C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1313beginning.
1314
1315C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1316If -1 or greater than or equal to the number of remaining kids, all
1317remaining kids are deleted.
1318
1319C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1320If C<NULL>, no nodes are inserted.
1321
1322The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1323deleted.
1324
1325For example:
1326
1327 action before after returns
1328 ------ ----- ----- -------
1329
1330 P P
1331 splice(P, A, 2, X-Y-Z) | | B-C
1332 A-B-C-D A-X-Y-Z-D
1333
1334 P P
1335 splice(P, NULL, 1, X-Y) | | A
1336 A-B-C-D X-Y-B-C-D
1337
1338 P P
1339 splice(P, NULL, 3, NULL) | | A-B-C
1340 A-B-C-D D
1341
1342 P P
1343 splice(P, B, 0, X-Y) | | NULL
1344 A-B-C-D A-B-X-Y-C-D
1345
1346
1347For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1348see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1349
1350=cut
1351*/
1352
1353OP *
1354Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1355{
1356 OP *first;
1357 OP *rest;
1358 OP *last_del = NULL;
1359 OP *last_ins = NULL;
1360
1361 if (start)
1362 first = OpSIBLING(start);
1363 else if (!parent)
1364 goto no_parent;
1365 else
1366 first = cLISTOPx(parent)->op_first;
1367
1368 assert(del_count >= -1);
1369
1370 if (del_count && first) {
1371 last_del = first;
1372 while (--del_count && OpHAS_SIBLING(last_del))
1373 last_del = OpSIBLING(last_del);
1374 rest = OpSIBLING(last_del);
1375 OpLASTSIB_set(last_del, NULL);
1376 }
1377 else
1378 rest = first;
1379
1380 if (insert) {
1381 last_ins = insert;
1382 while (OpHAS_SIBLING(last_ins))
1383 last_ins = OpSIBLING(last_ins);
1384 OpMAYBESIB_set(last_ins, rest, NULL);
1385 }
1386 else
1387 insert = rest;
1388
1389 if (start) {
1390 OpMAYBESIB_set(start, insert, NULL);
1391 }
1392 else {
1393 if (!parent)
1394 goto no_parent;
1395 cLISTOPx(parent)->op_first = insert;
1396 if (insert)
1397 parent->op_flags |= OPf_KIDS;
1398 else
1399 parent->op_flags &= ~OPf_KIDS;
1400 }
1401
1402 if (!rest) {
1403 /* update op_last etc */
1404 U32 type;
1405 OP *lastop;
1406
1407 if (!parent)
1408 goto no_parent;
1409
1410 /* ought to use OP_CLASS(parent) here, but that can't handle
1411 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1412 * either */
1413 type = parent->op_type;
1414 if (type == OP_CUSTOM) {
1415 dTHX;
1416 type = XopENTRYCUSTOM(parent, xop_class);
1417 }
1418 else {
1419 if (type == OP_NULL)
1420 type = parent->op_targ;
1421 type = PL_opargs[type] & OA_CLASS_MASK;
1422 }
1423
1424 lastop = last_ins ? last_ins : start ? start : NULL;
1425 if ( type == OA_BINOP
1426 || type == OA_LISTOP
1427 || type == OA_PMOP
1428 || type == OA_LOOP
1429 )
1430 cLISTOPx(parent)->op_last = lastop;
1431
1432 if (lastop)
1433 OpLASTSIB_set(lastop, parent);
1434 }
1435 return last_del ? first : NULL;
1436
1437 no_parent:
1438 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1439}
1440
1441
1442#ifdef PERL_OP_PARENT
1443
1444/*
1445=for apidoc op_parent
1446
1447Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1448This function is only available on perls built with C<-DPERL_OP_PARENT>.
1449
1450=cut
1451*/
1452
1453OP *
1454Perl_op_parent(OP *o)
1455{
1456 PERL_ARGS_ASSERT_OP_PARENT;
1457 while (OpHAS_SIBLING(o))
1458 o = OpSIBLING(o);
1459 return o->op_sibparent;
1460}
1461
1462#endif
1463
1464
1465/* replace the sibling following start with a new UNOP, which becomes
1466 * the parent of the original sibling; e.g.
1467 *
1468 * op_sibling_newUNOP(P, A, unop-args...)
1469 *
1470 * P P
1471 * | becomes |
1472 * A-B-C A-U-C
1473 * |
1474 * B
1475 *
1476 * where U is the new UNOP.
1477 *
1478 * parent and start args are the same as for op_sibling_splice();
1479 * type and flags args are as newUNOP().
1480 *
1481 * Returns the new UNOP.
1482 */
1483
1484STATIC OP *
1485S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1486{
1487 OP *kid, *newop;
1488
1489 kid = op_sibling_splice(parent, start, 1, NULL);
1490 newop = newUNOP(type, flags, kid);
1491 op_sibling_splice(parent, start, 0, newop);
1492 return newop;
1493}
1494
1495
1496/* lowest-level newLOGOP-style function - just allocates and populates
1497 * the struct. Higher-level stuff should be done by S_new_logop() /
1498 * newLOGOP(). This function exists mainly to avoid op_first assignment
1499 * being spread throughout this file.
1500 */
1501
1502LOGOP *
1503Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1504{
1505 dVAR;
1506 LOGOP *logop;
1507 OP *kid = first;
1508 NewOp(1101, logop, 1, LOGOP);
1509 OpTYPE_set(logop, type);
1510 logop->op_first = first;
1511 logop->op_other = other;
1512 logop->op_flags = OPf_KIDS;
1513 while (kid && OpHAS_SIBLING(kid))
1514 kid = OpSIBLING(kid);
1515 if (kid)
1516 OpLASTSIB_set(kid, (OP*)logop);
1517 return logop;
1518}
1519
1520
1521/* Contextualizers */
1522
1523/*
1524=for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1525
1526Applies a syntactic context to an op tree representing an expression.
1527C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1528or C<G_VOID> to specify the context to apply. The modified op tree
1529is returned.
1530
1531=cut
1532*/
1533
1534OP *
1535Perl_op_contextualize(pTHX_ OP *o, I32 context)
1536{
1537 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1538 switch (context) {
1539 case G_SCALAR: return scalar(o);
1540 case G_ARRAY: return list(o);
1541 case G_VOID: return scalarvoid(o);
1542 default:
1543 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1544 (long) context);
1545 }
1546}
1547
1548/*
1549
1550=for apidoc Am|OP*|op_linklist|OP *o
1551This function is the implementation of the L</LINKLIST> macro. It should
1552not be called directly.
1553
1554=cut
1555*/
1556
1557OP *
1558Perl_op_linklist(pTHX_ OP *o)
1559{
1560 OP *first;
1561
1562 PERL_ARGS_ASSERT_OP_LINKLIST;
1563
1564 if (o->op_next)
1565 return o->op_next;
1566
1567 /* establish postfix order */
1568 first = cUNOPo->op_first;
1569 if (first) {
1570 OP *kid;
1571 o->op_next = LINKLIST(first);
1572 kid = first;
1573 for (;;) {
1574 OP *sibl = OpSIBLING(kid);
1575 if (sibl) {
1576 kid->op_next = LINKLIST(sibl);
1577 kid = sibl;
1578 } else {
1579 kid->op_next = o;
1580 break;
1581 }
1582 }
1583 }
1584 else
1585 o->op_next = o;
1586
1587 return o->op_next;
1588}
1589
1590static OP *
1591S_scalarkids(pTHX_ OP *o)
1592{
1593 if (o && o->op_flags & OPf_KIDS) {
1594 OP *kid;
1595 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1596 scalar(kid);
1597 }
1598 return o;
1599}
1600
1601STATIC OP *
1602S_scalarboolean(pTHX_ OP *o)
1603{
1604 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1605
1606 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1607 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1608 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1609 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1610 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1611 if (ckWARN(WARN_SYNTAX)) {
1612 const line_t oldline = CopLINE(PL_curcop);
1613
1614 if (PL_parser && PL_parser->copline != NOLINE) {
1615 /* This ensures that warnings are reported at the first line
1616 of the conditional, not the last. */
1617 CopLINE_set(PL_curcop, PL_parser->copline);
1618 }
1619 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1620 CopLINE_set(PL_curcop, oldline);
1621 }
1622 }
1623 return scalar(o);
1624}
1625
1626static SV *
1627S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1628{
1629 assert(o);
1630 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1631 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1632 {
1633 const char funny = o->op_type == OP_PADAV
1634 || o->op_type == OP_RV2AV ? '@' : '%';
1635 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1636 GV *gv;
1637 if (cUNOPo->op_first->op_type != OP_GV
1638 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1639 return NULL;
1640 return varname(gv, funny, 0, NULL, 0, subscript_type);
1641 }
1642 return
1643 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1644 }
1645}
1646
1647static SV *
1648S_op_varname(pTHX_ const OP *o)
1649{
1650 return S_op_varname_subscript(aTHX_ o, 1);
1651}
1652
1653static void
1654S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1655{ /* or not so pretty :-) */
1656 if (o->op_type == OP_CONST) {
1657 *retsv = cSVOPo_sv;
1658 if (SvPOK(*retsv)) {
1659 SV *sv = *retsv;
1660 *retsv = sv_newmortal();
1661 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1662 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1663 }
1664 else if (!SvOK(*retsv))
1665 *retpv = "undef";
1666 }
1667 else *retpv = "...";
1668}
1669
1670static void
1671S_scalar_slice_warning(pTHX_ const OP *o)
1672{
1673 OP *kid;
1674 const bool h = o->op_type == OP_HSLICE
1675 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
1676 const char lbrack =
1677 h ? '{' : '[';
1678 const char rbrack =
1679 h ? '}' : ']';
1680 SV *name;
1681 SV *keysv = NULL; /* just to silence compiler warnings */
1682 const char *key = NULL;
1683
1684 if (!(o->op_private & OPpSLICEWARNING))
1685 return;
1686 if (PL_parser && PL_parser->error_count)
1687 /* This warning can be nonsensical when there is a syntax error. */
1688 return;
1689
1690 kid = cLISTOPo->op_first;
1691 kid = OpSIBLING(kid); /* get past pushmark */
1692 /* weed out false positives: any ops that can return lists */
1693 switch (kid->op_type) {
1694 case OP_BACKTICK:
1695 case OP_GLOB:
1696 case OP_READLINE:
1697 case OP_MATCH:
1698 case OP_RV2AV:
1699 case OP_EACH:
1700 case OP_VALUES:
1701 case OP_KEYS:
1702 case OP_SPLIT:
1703 case OP_LIST:
1704 case OP_SORT:
1705 case OP_REVERSE:
1706 case OP_ENTERSUB:
1707 case OP_CALLER:
1708 case OP_LSTAT:
1709 case OP_STAT:
1710 case OP_READDIR:
1711 case OP_SYSTEM:
1712 case OP_TMS:
1713 case OP_LOCALTIME:
1714 case OP_GMTIME:
1715 case OP_ENTEREVAL:
1716 return;
1717 }
1718
1719 /* Don't warn if we have a nulled list either. */
1720 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1721 return;
1722
1723 assert(OpSIBLING(kid));
1724 name = S_op_varname(aTHX_ OpSIBLING(kid));
1725 if (!name) /* XS module fiddling with the op tree */
1726 return;
1727 S_op_pretty(aTHX_ kid, &keysv, &key);
1728 assert(SvPOK(name));
1729 sv_chop(name,SvPVX(name)+1);
1730 if (key)
1731 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1732 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1733 "Scalar value @%" SVf "%c%s%c better written as $%" SVf
1734 "%c%s%c",
1735 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1736 lbrack, key, rbrack);
1737 else
1738 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1739 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1740 "Scalar value @%" SVf "%c%" SVf "%c better written as $%"
1741 SVf "%c%" SVf "%c",
1742 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1743 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1744}
1745
1746OP *
1747Perl_scalar(pTHX_ OP *o)
1748{
1749 OP *kid;
1750
1751 /* assumes no premature commitment */
1752 if (!o || (PL_parser && PL_parser->error_count)
1753 || (o->op_flags & OPf_WANT)
1754 || o->op_type == OP_RETURN)
1755 {
1756 return o;
1757 }
1758
1759 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1760
1761 switch (o->op_type) {
1762 case OP_REPEAT:
1763 scalar(cBINOPo->op_first);
1764 if (o->op_private & OPpREPEAT_DOLIST) {
1765 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1766 assert(kid->op_type == OP_PUSHMARK);
1767 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1768 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1769 o->op_private &=~ OPpREPEAT_DOLIST;
1770 }
1771 }
1772 break;
1773 case OP_OR:
1774 case OP_AND:
1775 case OP_COND_EXPR:
1776 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
1777 scalar(kid);
1778 break;
1779 /* FALLTHROUGH */
1780 case OP_SPLIT:
1781 case OP_MATCH:
1782 case OP_QR:
1783 case OP_SUBST:
1784 case OP_NULL:
1785 default:
1786 if (o->op_flags & OPf_KIDS) {
1787 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
1788 scalar(kid);
1789 }
1790 break;
1791 case OP_LEAVE:
1792 case OP_LEAVETRY:
1793 kid = cLISTOPo->op_first;
1794 scalar(kid);
1795 kid = OpSIBLING(kid);
1796 do_kids:
1797 while (kid) {
1798 OP *sib = OpSIBLING(kid);
1799 if (sib && kid->op_type != OP_LEAVEWHEN
1800 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
1801 || ( sib->op_targ != OP_NEXTSTATE
1802 && sib->op_targ != OP_DBSTATE )))
1803 scalarvoid(kid);
1804 else
1805 scalar(kid);
1806 kid = sib;
1807 }
1808 PL_curcop = &PL_compiling;
1809 break;
1810 case OP_SCOPE:
1811 case OP_LINESEQ:
1812 case OP_LIST:
1813 kid = cLISTOPo->op_first;
1814 goto do_kids;
1815 case OP_SORT:
1816 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1817 break;
1818 case OP_KVHSLICE:
1819 case OP_KVASLICE:
1820 {
1821 /* Warn about scalar context */
1822 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1823 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1824 SV *name;
1825 SV *keysv;
1826 const char *key = NULL;
1827
1828 /* This warning can be nonsensical when there is a syntax error. */
1829 if (PL_parser && PL_parser->error_count)
1830 break;
1831
1832 if (!ckWARN(WARN_SYNTAX)) break;
1833
1834 kid = cLISTOPo->op_first;
1835 kid = OpSIBLING(kid); /* get past pushmark */
1836 assert(OpSIBLING(kid));
1837 name = S_op_varname(aTHX_ OpSIBLING(kid));
1838 if (!name) /* XS module fiddling with the op tree */
1839 break;
1840 S_op_pretty(aTHX_ kid, &keysv, &key);
1841 assert(SvPOK(name));
1842 sv_chop(name,SvPVX(name)+1);
1843 if (key)
1844 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1845 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1846 "%%%" SVf "%c%s%c in scalar context better written "
1847 "as $%" SVf "%c%s%c",
1848 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1849 lbrack, key, rbrack);
1850 else
1851 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1852 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1853 "%%%" SVf "%c%" SVf "%c in scalar context better "
1854 "written as $%" SVf "%c%" SVf "%c",
1855 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1856 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1857 }
1858 }
1859 return o;
1860}
1861
1862OP *
1863Perl_scalarvoid(pTHX_ OP *arg)
1864{
1865 dVAR;
1866 OP *kid;
1867 SV* sv;
1868 U8 want;
1869 SSize_t defer_stack_alloc = 0;
1870 SSize_t defer_ix = -1;
1871 OP **defer_stack = NULL;
1872 OP *o = arg;
1873
1874 PERL_ARGS_ASSERT_SCALARVOID;
1875
1876 do {
1877 SV *useless_sv = NULL;
1878 const char* useless = NULL;
1879
1880 if (o->op_type == OP_NEXTSTATE
1881 || o->op_type == OP_DBSTATE
1882 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1883 || o->op_targ == OP_DBSTATE)))
1884 PL_curcop = (COP*)o; /* for warning below */
1885
1886 /* assumes no premature commitment */
1887 want = o->op_flags & OPf_WANT;
1888 if ((want && want != OPf_WANT_SCALAR)
1889 || (PL_parser && PL_parser->error_count)
1890 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1891 {
1892 continue;
1893 }
1894
1895 if ((o->op_private & OPpTARGET_MY)
1896 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1897 {
1898 /* newASSIGNOP has already applied scalar context, which we
1899 leave, as if this op is inside SASSIGN. */
1900 continue;
1901 }
1902
1903 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1904
1905 switch (o->op_type) {
1906 default:
1907 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1908 break;
1909 /* FALLTHROUGH */
1910 case OP_REPEAT:
1911 if (o->op_flags & OPf_STACKED)
1912 break;
1913 if (o->op_type == OP_REPEAT)
1914 scalar(cBINOPo->op_first);
1915 goto func_ops;
1916 case OP_SUBSTR:
1917 if (o->op_private == 4)
1918 break;
1919 /* FALLTHROUGH */
1920 case OP_WANTARRAY:
1921 case OP_GV:
1922 case OP_SMARTMATCH:
1923 case OP_AV2ARYLEN:
1924 case OP_REF:
1925 case OP_REFGEN:
1926 case OP_SREFGEN:
1927 case OP_DEFINED:
1928 case OP_HEX:
1929 case OP_OCT:
1930 case OP_LENGTH:
1931 case OP_VEC:
1932 case OP_INDEX:
1933 case OP_RINDEX:
1934 case OP_SPRINTF:
1935 case OP_KVASLICE:
1936 case OP_KVHSLICE:
1937 case OP_UNPACK:
1938 case OP_PACK:
1939 case OP_JOIN:
1940 case OP_LSLICE:
1941 case OP_ANONLIST:
1942 case OP_ANONHASH:
1943 case OP_SORT:
1944 case OP_REVERSE:
1945 case OP_RANGE:
1946 case OP_FLIP:
1947 case OP_FLOP:
1948 case OP_CALLER:
1949 case OP_FILENO:
1950 case OP_EOF:
1951 case OP_TELL:
1952 case OP_GETSOCKNAME:
1953 case OP_GETPEERNAME:
1954 case OP_READLINK:
1955 case OP_TELLDIR:
1956 case OP_GETPPID:
1957 case OP_GETPGRP:
1958 case OP_GETPRIORITY:
1959 case OP_TIME:
1960 case OP_TMS:
1961 case OP_LOCALTIME:
1962 case OP_GMTIME:
1963 case OP_GHBYNAME:
1964 case OP_GHBYADDR:
1965 case OP_GHOSTENT:
1966 case OP_GNBYNAME:
1967 case OP_GNBYADDR:
1968 case OP_GNETENT:
1969 case OP_GPBYNAME:
1970 case OP_GPBYNUMBER:
1971 case OP_GPROTOENT:
1972 case OP_GSBYNAME:
1973 case OP_GSBYPORT:
1974 case OP_GSERVENT:
1975 case OP_GPWNAM:
1976 case OP_GPWUID:
1977 case OP_GGRNAM:
1978 case OP_GGRGID:
1979 case OP_GETLOGIN:
1980 case OP_PROTOTYPE:
1981 case OP_RUNCV:
1982 func_ops:
1983 useless = OP_DESC(o);
1984 break;
1985
1986 case OP_GVSV:
1987 case OP_PADSV:
1988 case OP_PADAV:
1989 case OP_PADHV:
1990 case OP_PADANY:
1991 case OP_AELEM:
1992 case OP_AELEMFAST:
1993 case OP_AELEMFAST_LEX:
1994 case OP_ASLICE:
1995 case OP_HELEM:
1996 case OP_HSLICE:
1997 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1998 /* Otherwise it's "Useless use of grep iterator" */
1999 useless = OP_DESC(o);
2000 break;
2001
2002 case OP_SPLIT:
2003 if (!(o->op_private & OPpSPLIT_ASSIGN))
2004 useless = OP_DESC(o);
2005 break;
2006
2007 case OP_NOT:
2008 kid = cUNOPo->op_first;
2009 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2010 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2011 goto func_ops;
2012 }
2013 useless = "negative pattern binding (!~)";
2014 break;
2015
2016 case OP_SUBST:
2017 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2018 useless = "non-destructive substitution (s///r)";
2019 break;
2020
2021 case OP_TRANSR:
2022 useless = "non-destructive transliteration (tr///r)";
2023 break;
2024
2025 case OP_RV2GV:
2026 case OP_RV2SV:
2027 case OP_RV2AV:
2028 case OP_RV2HV:
2029 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2030 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2031 useless = "a variable";
2032 break;
2033
2034 case OP_CONST:
2035 sv = cSVOPo_sv;
2036 if (cSVOPo->op_private & OPpCONST_STRICT)
2037 no_bareword_allowed(o);
2038 else {
2039 if (ckWARN(WARN_VOID)) {
2040 NV nv;
2041 /* don't warn on optimised away booleans, eg
2042 * use constant Foo, 5; Foo || print; */
2043 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2044 useless = NULL;
2045 /* the constants 0 and 1 are permitted as they are
2046 conventionally used as dummies in constructs like
2047 1 while some_condition_with_side_effects; */
2048 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2049 useless = NULL;
2050 else if (SvPOK(sv)) {
2051 SV * const dsv = newSVpvs("");
2052 useless_sv
2053 = Perl_newSVpvf(aTHX_
2054 "a constant (%s)",
2055 pv_pretty(dsv, SvPVX_const(sv),
2056 SvCUR(sv), 32, NULL, NULL,
2057 PERL_PV_PRETTY_DUMP
2058 | PERL_PV_ESCAPE_NOCLEAR
2059 | PERL_PV_ESCAPE_UNI_DETECT));
2060 SvREFCNT_dec_NN(dsv);
2061 }
2062 else if (SvOK(sv)) {
2063 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2064 }
2065 else
2066 useless = "a constant (undef)";
2067 }
2068 }
2069 op_null(o); /* don't execute or even remember it */
2070 break;
2071
2072 case OP_POSTINC:
2073 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2074 break;
2075
2076 case OP_POSTDEC:
2077 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2078 break;
2079
2080 case OP_I_POSTINC:
2081 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2082 break;
2083
2084 case OP_I_POSTDEC:
2085 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2086 break;
2087
2088 case OP_SASSIGN: {
2089 OP *rv2gv;
2090 UNOP *refgen, *rv2cv;
2091 LISTOP *exlist;
2092
2093 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2094 break;
2095
2096 rv2gv = ((BINOP *)o)->op_last;
2097 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2098 break;
2099
2100 refgen = (UNOP *)((BINOP *)o)->op_first;
2101
2102 if (!refgen || (refgen->op_type != OP_REFGEN
2103 && refgen->op_type != OP_SREFGEN))
2104 break;
2105
2106 exlist = (LISTOP *)refgen->op_first;
2107 if (!exlist || exlist->op_type != OP_NULL
2108 || exlist->op_targ != OP_LIST)
2109 break;
2110
2111 if (exlist->op_first->op_type != OP_PUSHMARK
2112 && exlist->op_first != exlist->op_last)
2113 break;
2114
2115 rv2cv = (UNOP*)exlist->op_last;
2116
2117 if (rv2cv->op_type != OP_RV2CV)
2118 break;
2119
2120 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2121 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2122 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2123
2124 o->op_private |= OPpASSIGN_CV_TO_GV;
2125 rv2gv->op_private |= OPpDONT_INIT_GV;
2126 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2127
2128 break;
2129 }
2130
2131 case OP_AASSIGN: {
2132 inplace_aassign(o);
2133 break;
2134 }
2135
2136 case OP_OR:
2137 case OP_AND:
2138 kid = cLOGOPo->op_first;
2139 if (kid->op_type == OP_NOT
2140 && (kid->op_flags & OPf_KIDS)) {
2141 if (o->op_type == OP_AND) {
2142 OpTYPE_set(o, OP_OR);
2143 } else {
2144 OpTYPE_set(o, OP_AND);
2145 }
2146 op_null(kid);
2147 }
2148 /* FALLTHROUGH */
2149
2150 case OP_DOR:
2151 case OP_COND_EXPR:
2152 case OP_ENTERGIVEN:
2153 case OP_ENTERWHEN:
2154 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2155 if (!(kid->op_flags & OPf_KIDS))
2156 scalarvoid(kid);
2157 else
2158 DEFER_OP(kid);
2159 break;
2160
2161 case OP_NULL:
2162 if (o->op_flags & OPf_STACKED)
2163 break;
2164 /* FALLTHROUGH */
2165 case OP_NEXTSTATE:
2166 case OP_DBSTATE:
2167 case OP_ENTERTRY:
2168 case OP_ENTER:
2169 if (!(o->op_flags & OPf_KIDS))
2170 break;
2171 /* FALLTHROUGH */
2172 case OP_SCOPE:
2173 case OP_LEAVE:
2174 case OP_LEAVETRY:
2175 case OP_LEAVELOOP:
2176 case OP_LINESEQ:
2177 case OP_LEAVEGIVEN:
2178 case OP_LEAVEWHEN:
2179 kids:
2180 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2181 if (!(kid->op_flags & OPf_KIDS))
2182 scalarvoid(kid);
2183 else
2184 DEFER_OP(kid);
2185 break;
2186 case OP_LIST:
2187 /* If the first kid after pushmark is something that the padrange
2188 optimisation would reject, then null the list and the pushmark.
2189 */
2190 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2191 && ( !(kid = OpSIBLING(kid))
2192 || ( kid->op_type != OP_PADSV
2193 && kid->op_type != OP_PADAV
2194 && kid->op_type != OP_PADHV)
2195 || kid->op_private & ~OPpLVAL_INTRO
2196 || !(kid = OpSIBLING(kid))
2197 || ( kid->op_type != OP_PADSV
2198 && kid->op_type != OP_PADAV
2199 && kid->op_type != OP_PADHV)
2200 || kid->op_private & ~OPpLVAL_INTRO)
2201 ) {
2202 op_null(cUNOPo->op_first); /* NULL the pushmark */
2203 op_null(o); /* NULL the list */
2204 }
2205 goto kids;
2206 case OP_ENTEREVAL:
2207 scalarkids(o);
2208 break;
2209 case OP_SCALAR:
2210 scalar(o);
2211 break;
2212 }
2213
2214 if (useless_sv) {
2215 /* mortalise it, in case warnings are fatal. */
2216 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2217 "Useless use of %" SVf " in void context",
2218 SVfARG(sv_2mortal(useless_sv)));
2219 }
2220 else if (useless) {
2221 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2222 "Useless use of %s in void context",
2223 useless);
2224 }
2225 } while ( (o = POP_DEFERRED_OP()) );
2226
2227 Safefree(defer_stack);
2228
2229 return arg;
2230}
2231
2232static OP *
2233S_listkids(pTHX_ OP *o)
2234{
2235 if (o && o->op_flags & OPf_KIDS) {
2236 OP *kid;
2237 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2238 list(kid);
2239 }
2240 return o;
2241}
2242
2243OP *
2244Perl_list(pTHX_ OP *o)
2245{
2246 OP *kid;
2247
2248 /* assumes no premature commitment */
2249 if (!o || (o->op_flags & OPf_WANT)
2250 || (PL_parser && PL_parser->error_count)
2251 || o->op_type == OP_RETURN)
2252 {
2253 return o;
2254 }
2255
2256 if ((o->op_private & OPpTARGET_MY)
2257 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2258 {
2259 return o; /* As if inside SASSIGN */
2260 }
2261
2262 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2263
2264 switch (o->op_type) {
2265 case OP_FLOP:
2266 list(cBINOPo->op_first);
2267 break;
2268 case OP_REPEAT:
2269 if (o->op_private & OPpREPEAT_DOLIST
2270 && !(o->op_flags & OPf_STACKED))
2271 {
2272 list(cBINOPo->op_first);
2273 kid = cBINOPo->op_last;
2274 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2275 && SvIVX(kSVOP_sv) == 1)
2276 {
2277 op_null(o); /* repeat */
2278 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2279 /* const (rhs): */
2280 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2281 }
2282 }
2283 break;
2284 case OP_OR:
2285 case OP_AND:
2286 case OP_COND_EXPR:
2287 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2288 list(kid);
2289 break;
2290 default:
2291 case OP_MATCH:
2292 case OP_QR:
2293 case OP_SUBST:
2294 case OP_NULL:
2295 if (!(o->op_flags & OPf_KIDS))
2296 break;
2297 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2298 list(cBINOPo->op_first);
2299 return gen_constant_list(o);
2300 }
2301 listkids(o);
2302 break;
2303 case OP_LIST:
2304 listkids(o);
2305 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2306 op_null(cUNOPo->op_first); /* NULL the pushmark */
2307 op_null(o); /* NULL the list */
2308 }
2309 break;
2310 case OP_LEAVE:
2311 case OP_LEAVETRY:
2312 kid = cLISTOPo->op_first;
2313 list(kid);
2314 kid = OpSIBLING(kid);
2315 do_kids:
2316 while (kid) {
2317 OP *sib = OpSIBLING(kid);
2318 if (sib && kid->op_type != OP_LEAVEWHEN)
2319 scalarvoid(kid);
2320 else
2321 list(kid);
2322 kid = sib;
2323 }
2324 PL_curcop = &PL_compiling;
2325 break;
2326 case OP_SCOPE:
2327 case OP_LINESEQ:
2328 kid = cLISTOPo->op_first;
2329 goto do_kids;
2330 }
2331 return o;
2332}
2333
2334static OP *
2335S_scalarseq(pTHX_ OP *o)
2336{
2337 if (o) {
2338 const OPCODE type = o->op_type;
2339
2340 if (type == OP_LINESEQ || type == OP_SCOPE ||
2341 type == OP_LEAVE || type == OP_LEAVETRY)
2342 {
2343 OP *kid, *sib;
2344 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2345 if ((sib = OpSIBLING(kid))
2346 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2347 || ( sib->op_targ != OP_NEXTSTATE
2348 && sib->op_targ != OP_DBSTATE )))
2349 {
2350 scalarvoid(kid);
2351 }
2352 }
2353 PL_curcop = &PL_compiling;
2354 }
2355 o->op_flags &= ~OPf_PARENS;
2356 if (PL_hints & HINT_BLOCK_SCOPE)
2357 o->op_flags |= OPf_PARENS;
2358 }
2359 else
2360 o = newOP(OP_STUB, 0);
2361 return o;
2362}
2363
2364STATIC OP *
2365S_modkids(pTHX_ OP *o, I32 type)
2366{
2367 if (o && o->op_flags & OPf_KIDS) {
2368 OP *kid;
2369 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2370 op_lvalue(kid, type);
2371 }
2372 return o;
2373}
2374
2375
2376/* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2377 * const fields. Also, convert CONST keys to HEK-in-SVs.
2378 * rop is the op that retrieves the hash;
2379 * key_op is the first key
2380 */
2381
2382STATIC void
2383S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op)
2384{
2385 PADNAME *lexname;
2386 GV **fields;
2387 bool check_fields;
2388
2389 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2390 if (rop) {
2391 if (rop->op_first->op_type == OP_PADSV)
2392 /* @$hash{qw(keys here)} */
2393 rop = (UNOP*)rop->op_first;
2394 else {
2395 /* @{$hash}{qw(keys here)} */
2396 if (rop->op_first->op_type == OP_SCOPE
2397 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2398 {
2399 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2400 }
2401 else
2402 rop = NULL;
2403 }
2404 }
2405
2406 lexname = NULL; /* just to silence compiler warnings */
2407 fields = NULL; /* just to silence compiler warnings */
2408
2409 check_fields =
2410 rop
2411 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2412 SvPAD_TYPED(lexname))
2413 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2414 && isGV(*fields) && GvHV(*fields);
2415
2416 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2417 SV **svp, *sv;
2418 if (key_op->op_type != OP_CONST)
2419 continue;
2420 svp = cSVOPx_svp(key_op);
2421
2422 /* make sure it's not a bareword under strict subs */
2423 if (key_op->op_private & OPpCONST_BARE &&
2424 key_op->op_private & OPpCONST_STRICT)
2425 {
2426 no_bareword_allowed((OP*)key_op);
2427 }
2428
2429 /* Make the CONST have a shared SV */
2430 if ( !SvIsCOW_shared_hash(sv = *svp)
2431 && SvTYPE(sv) < SVt_PVMG
2432 && SvOK(sv)
2433 && !SvROK(sv))
2434 {
2435 SSize_t keylen;
2436 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2437 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2438 SvREFCNT_dec_NN(sv);
2439 *svp = nsv;
2440 }
2441
2442 if ( check_fields
2443 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2444 {
2445 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2446 "in variable %" PNf " of type %" HEKf,
2447 SVfARG(*svp), PNfARG(lexname),
2448 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2449 }
2450 }
2451}
2452
2453
2454/* do all the final processing on an optree (e.g. running the peephole
2455 * optimiser on it), then attach it to cv (if cv is non-null)
2456 */
2457
2458static void
2459S_process_optree(pTHX_ CV *cv, OP *optree, OP* start)
2460{
2461 OP **startp;
2462
2463 /* XXX for some reason, evals, require and main optrees are
2464 * never attached to their CV; instead they just hang off
2465 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start
2466 * and get manually freed when appropriate */
2467 if (cv)
2468 startp = &CvSTART(cv);
2469 else
2470 startp = PL_in_eval? &PL_eval_start : &PL_main_start;
2471
2472 *startp = start;
2473 optree->op_private |= OPpREFCOUNTED;
2474 OpREFCNT_set(optree, 1);
2475 CALL_PEEP(*startp);
2476 finalize_optree(optree);
2477 S_prune_chain_head(startp);
2478
2479 if (cv) {
2480 /* now that optimizer has done its work, adjust pad values */
2481 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT
2482 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
2483 }
2484}
2485
2486
2487/*
2488=for apidoc finalize_optree
2489
2490This function finalizes the optree. Should be called directly after
2491the complete optree is built. It does some additional
2492checking which can't be done in the normal C<ck_>xxx functions and makes
2493the tree thread-safe.
2494
2495=cut
2496*/
2497void
2498Perl_finalize_optree(pTHX_ OP* o)
2499{
2500 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2501
2502 ENTER;
2503 SAVEVPTR(PL_curcop);
2504
2505 finalize_op(o);
2506
2507 LEAVE;
2508}
2509
2510#ifdef USE_ITHREADS
2511/* Relocate sv to the pad for thread safety.
2512 * Despite being a "constant", the SV is written to,
2513 * for reference counts, sv_upgrade() etc. */
2514PERL_STATIC_INLINE void
2515S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2516{
2517 PADOFFSET ix;
2518 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2519 if (!*svp) return;
2520 ix = pad_alloc(OP_CONST, SVf_READONLY);
2521 SvREFCNT_dec(PAD_SVl(ix));
2522 PAD_SETSV(ix, *svp);
2523 /* XXX I don't know how this isn't readonly already. */
2524 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2525 *svp = NULL;
2526 *targp = ix;
2527}
2528#endif
2529
2530
2531STATIC void
2532S_finalize_op(pTHX_ OP* o)
2533{
2534 PERL_ARGS_ASSERT_FINALIZE_OP;
2535
2536 assert(o->op_type != OP_FREED);
2537
2538 switch (o->op_type) {
2539 case OP_NEXTSTATE:
2540 case OP_DBSTATE:
2541 PL_curcop = ((COP*)o); /* for warnings */
2542 break;
2543 case OP_EXEC:
2544 if (OpHAS_SIBLING(o)) {
2545 OP *sib = OpSIBLING(o);
2546 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2547 && ckWARN(WARN_EXEC)
2548 && OpHAS_SIBLING(sib))
2549 {
2550 const OPCODE type = OpSIBLING(sib)->op_type;
2551 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2552 const line_t oldline = CopLINE(PL_curcop);
2553 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2554 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2555 "Statement unlikely to be reached");
2556 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2557 "\t(Maybe you meant system() when you said exec()?)\n");
2558 CopLINE_set(PL_curcop, oldline);
2559 }
2560 }
2561 }
2562 break;
2563
2564 case OP_GV:
2565 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2566 GV * const gv = cGVOPo_gv;
2567 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2568 /* XXX could check prototype here instead of just carping */
2569 SV * const sv = sv_newmortal();
2570 gv_efullname3(sv, gv, NULL);
2571 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2572 "%" SVf "() called too early to check prototype",
2573 SVfARG(sv));
2574 }
2575 }
2576 break;
2577
2578 case OP_CONST:
2579 if (cSVOPo->op_private & OPpCONST_STRICT)
2580 no_bareword_allowed(o);
2581 /* FALLTHROUGH */
2582#ifdef USE_ITHREADS
2583 case OP_HINTSEVAL:
2584 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2585#endif
2586 break;
2587
2588#ifdef USE_ITHREADS
2589 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2590 case OP_METHOD_NAMED:
2591 case OP_METHOD_SUPER:
2592 case OP_METHOD_REDIR:
2593 case OP_METHOD_REDIR_SUPER:
2594 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2595 break;
2596#endif
2597
2598 case OP_HELEM: {
2599 UNOP *rop;
2600 SVOP *key_op;
2601 OP *kid;
2602
2603 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2604 break;
2605
2606 rop = (UNOP*)((BINOP*)o)->op_first;
2607
2608 goto check_keys;
2609
2610 case OP_HSLICE:
2611 S_scalar_slice_warning(aTHX_ o);
2612 /* FALLTHROUGH */
2613
2614 case OP_KVHSLICE:
2615 kid = OpSIBLING(cLISTOPo->op_first);
2616 if (/* I bet there's always a pushmark... */
2617 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2618 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2619 {
2620 break;
2621 }
2622
2623 key_op = (SVOP*)(kid->op_type == OP_CONST
2624 ? kid
2625 : OpSIBLING(kLISTOP->op_first));
2626
2627 rop = (UNOP*)((LISTOP*)o)->op_last;
2628
2629 check_keys:
2630 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2631 rop = NULL;
2632 S_check_hash_fields_and_hekify(aTHX_ rop, key_op);
2633 break;
2634 }
2635 case OP_NULL:
2636 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
2637 break;
2638 /* FALLTHROUGH */
2639 case OP_ASLICE:
2640 S_scalar_slice_warning(aTHX_ o);
2641 break;
2642
2643 case OP_SUBST: {
2644 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2645 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2646 break;
2647 }
2648 default:
2649 break;
2650 }
2651
2652 if (o->op_flags & OPf_KIDS) {
2653 OP *kid;
2654
2655#ifdef DEBUGGING
2656 /* check that op_last points to the last sibling, and that
2657 * the last op_sibling/op_sibparent field points back to the
2658 * parent, and that the only ops with KIDS are those which are
2659 * entitled to them */
2660 U32 type = o->op_type;
2661 U32 family;
2662 bool has_last;
2663
2664 if (type == OP_NULL) {
2665 type = o->op_targ;
2666 /* ck_glob creates a null UNOP with ex-type GLOB
2667 * (which is a list op. So pretend it wasn't a listop */
2668 if (type == OP_GLOB)
2669 type = OP_NULL;
2670 }
2671 family = PL_opargs[type] & OA_CLASS_MASK;
2672
2673 has_last = ( family == OA_BINOP
2674 || family == OA_LISTOP
2675 || family == OA_PMOP
2676 || family == OA_LOOP
2677 );
2678 assert( has_last /* has op_first and op_last, or ...
2679 ... has (or may have) op_first: */
2680 || family == OA_UNOP
2681 || family == OA_UNOP_AUX
2682 || family == OA_LOGOP
2683 || family == OA_BASEOP_OR_UNOP
2684 || family == OA_FILESTATOP
2685 || family == OA_LOOPEXOP
2686 || family == OA_METHOP
2687 || type == OP_CUSTOM
2688 || type == OP_NULL /* new_logop does this */
2689 );
2690
2691 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2692# ifdef PERL_OP_PARENT
2693 if (!OpHAS_SIBLING(kid)) {
2694 if (has_last)
2695 assert(kid == cLISTOPo->op_last);
2696 assert(kid->op_sibparent == o);
2697 }
2698# else
2699 if (has_last && !OpHAS_SIBLING(kid))
2700 assert(kid == cLISTOPo->op_last);
2701# endif
2702 }
2703#endif
2704
2705 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
2706 finalize_op(kid);
2707 }
2708}
2709
2710/*
2711=for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2712
2713Propagate lvalue ("modifiable") context to an op and its children.
2714C<type> represents the context type, roughly based on the type of op that
2715would do the modifying, although C<local()> is represented by C<OP_NULL>,
2716because it has no op type of its own (it is signalled by a flag on
2717the lvalue op).
2718
2719This function detects things that can't be modified, such as C<$x+1>, and
2720generates errors for them. For example, C<$x+1 = 2> would cause it to be
2721called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
2722
2723It also flags things that need to behave specially in an lvalue context,
2724such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2725
2726=cut
2727*/
2728
2729static void
2730S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2731{
2732 CV *cv = PL_compcv;
2733 PadnameLVALUE_on(pn);
2734 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2735 cv = CvOUTSIDE(cv);
2736 /* RT #127786: cv can be NULL due to an eval within the DB package
2737 * called from an anon sub - anon subs don't have CvOUTSIDE() set
2738 * unless they contain an eval, but calling eval within DB
2739 * pretends the eval was done in the caller's scope.
2740 */
2741 if (!cv)
2742 break;
2743 assert(CvPADLIST(cv));
2744 pn =
2745 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2746 assert(PadnameLEN(pn));
2747 PadnameLVALUE_on(pn);
2748 }
2749}
2750
2751static bool
2752S_vivifies(const OPCODE type)
2753{
2754 switch(type) {
2755 case OP_RV2AV: case OP_ASLICE:
2756 case OP_RV2HV: case OP_KVASLICE:
2757 case OP_RV2SV: case OP_HSLICE:
2758 case OP_AELEMFAST: case OP_KVHSLICE:
2759 case OP_HELEM:
2760 case OP_AELEM:
2761 return 1;
2762 }
2763 return 0;
2764}
2765
2766static void
2767S_lvref(pTHX_ OP *o, I32 type)
2768{
2769 dVAR;
2770 OP *kid;
2771 switch (o->op_type) {
2772 case OP_COND_EXPR:
2773 for (kid = OpSIBLING(cUNOPo->op_first); kid;
2774 kid = OpSIBLING(kid))
2775 S_lvref(aTHX_ kid, type);
2776 /* FALLTHROUGH */
2777 case OP_PUSHMARK:
2778 return;
2779 case OP_RV2AV:
2780 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2781 o->op_flags |= OPf_STACKED;
2782 if (o->op_flags & OPf_PARENS) {
2783 if (o->op_private & OPpLVAL_INTRO) {
2784 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2785 "localized parenthesized array in list assignment"));
2786 return;
2787 }
2788 slurpy:
2789 OpTYPE_set(o, OP_LVAVREF);
2790 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2791 o->op_flags |= OPf_MOD|OPf_REF;
2792 return;
2793 }
2794 o->op_private |= OPpLVREF_AV;
2795 goto checkgv;
2796 case OP_RV2CV:
2797 kid = cUNOPo->op_first;
2798 if (kid->op_type == OP_NULL)
2799 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
2800 ->op_first;
2801 o->op_private = OPpLVREF_CV;
2802 if (kid->op_type == OP_GV)
2803 o->op_flags |= OPf_STACKED;
2804 else if (kid->op_type == OP_PADCV) {
2805 o->op_targ = kid->op_targ;
2806 kid->op_targ = 0;
2807 op_free(cUNOPo->op_first);
2808 cUNOPo->op_first = NULL;
2809 o->op_flags &=~ OPf_KIDS;
2810 }
2811 else goto badref;
2812 break;
2813 case OP_RV2HV:
2814 if (o->op_flags & OPf_PARENS) {
2815 parenhash:
2816 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2817 "parenthesized hash in list assignment"));
2818 return;
2819 }
2820 o->op_private |= OPpLVREF_HV;
2821 /* FALLTHROUGH */
2822 case OP_RV2SV:
2823 checkgv:
2824 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2825 o->op_flags |= OPf_STACKED;
2826 break;
2827 case OP_PADHV:
2828 if (o->op_flags & OPf_PARENS) goto parenhash;
2829 o->op_private |= OPpLVREF_HV;
2830 /* FALLTHROUGH */
2831 case OP_PADSV:
2832 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2833 break;
2834 case OP_PADAV:
2835 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2836 if (o->op_flags & OPf_PARENS) goto slurpy;
2837 o->op_private |= OPpLVREF_AV;
2838 break;
2839 case OP_AELEM:
2840 case OP_HELEM:
2841 o->op_private |= OPpLVREF_ELEM;
2842 o->op_flags |= OPf_STACKED;
2843 break;
2844 case OP_ASLICE:
2845 case OP_HSLICE:
2846 OpTYPE_set(o, OP_LVREFSLICE);
2847 o->op_private &= OPpLVAL_INTRO;
2848 return;
2849 case OP_NULL:
2850 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2851 goto badref;
2852 else if (!(o->op_flags & OPf_KIDS))
2853 return;
2854 if (o->op_targ != OP_LIST) {
2855 S_lvref(aTHX_ cBINOPo->op_first, type);
2856 return;
2857 }
2858 /* FALLTHROUGH */
2859 case OP_LIST:
2860 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
2861 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2862 S_lvref(aTHX_ kid, type);
2863 }
2864 return;
2865 case OP_STUB:
2866 if (o->op_flags & OPf_PARENS)
2867 return;
2868 /* FALLTHROUGH */
2869 default:
2870 badref:
2871 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2872 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2873 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2874 ? "do block"
2875 : OP_DESC(o),
2876 PL_op_desc[type]));
2877 return;
2878 }
2879 OpTYPE_set(o, OP_LVREF);
2880 o->op_private &=
2881 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2882 if (type == OP_ENTERLOOP)
2883 o->op_private |= OPpLVREF_ITER;
2884}
2885
2886PERL_STATIC_INLINE bool
2887S_potential_mod_type(I32 type)
2888{
2889 /* Types that only potentially result in modification. */
2890 return type == OP_GREPSTART || type == OP_ENTERSUB
2891 || type == OP_REFGEN || type == OP_LEAVESUBLV;
2892}
2893
2894OP *
2895Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2896{
2897 dVAR;
2898 OP *kid;
2899 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2900 int localize = -1;
2901
2902 if (!o || (PL_parser && PL_parser->error_count))
2903 return o;
2904
2905 if ((o->op_private & OPpTARGET_MY)
2906 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2907 {
2908 return o;
2909 }
2910
2911 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2912
2913 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2914
2915 switch (o->op_type) {
2916 case OP_UNDEF:
2917 PL_modcount++;
2918 return o;
2919 case OP_STUB:
2920 if ((o->op_flags & OPf_PARENS))
2921 break;
2922 goto nomod;
2923 case OP_ENTERSUB:
2924 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2925 !(o->op_flags & OPf_STACKED)) {
2926 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
2927 assert(cUNOPo->op_first->op_type == OP_NULL);
2928 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2929 break;
2930 }
2931 else { /* lvalue subroutine call */
2932 o->op_private |= OPpLVAL_INTRO;
2933 PL_modcount = RETURN_UNLIMITED_NUMBER;
2934 if (S_potential_mod_type(type)) {
2935 o->op_private |= OPpENTERSUB_INARGS;
2936 break;
2937 }
2938 else { /* Compile-time error message: */
2939 OP *kid = cUNOPo->op_first;
2940 CV *cv;
2941 GV *gv;
2942 SV *namesv;
2943
2944 if (kid->op_type != OP_PUSHMARK) {
2945 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2946 Perl_croak(aTHX_
2947 "panic: unexpected lvalue entersub "
2948 "args: type/targ %ld:%" UVuf,
2949 (long)kid->op_type, (UV)kid->op_targ);
2950 kid = kLISTOP->op_first;
2951 }
2952 while (OpHAS_SIBLING(kid))
2953 kid = OpSIBLING(kid);
2954 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2955 break; /* Postpone until runtime */
2956 }
2957
2958 kid = kUNOP->op_first;
2959 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2960 kid = kUNOP->op_first;
2961 if (kid->op_type == OP_NULL)
2962 Perl_croak(aTHX_
2963 "Unexpected constant lvalue entersub "
2964 "entry via type/targ %ld:%" UVuf,
2965 (long)kid->op_type, (UV)kid->op_targ);
2966 if (kid->op_type != OP_GV) {
2967 break;
2968 }
2969
2970 gv = kGVOP_gv;
2971 cv = isGV(gv)
2972 ? GvCV(gv)
2973 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2974 ? MUTABLE_CV(SvRV(gv))
2975 : NULL;
2976 if (!cv)
2977 break;
2978 if (CvLVALUE(cv))
2979 break;
2980 if (flags & OP_LVALUE_NO_CROAK)
2981 return NULL;
2982
2983 namesv = cv_name(cv, NULL, 0);
2984 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
2985 "subroutine call of &%" SVf " in %s",
2986 SVfARG(namesv), PL_op_desc[type]),
2987 SvUTF8(namesv));
2988 return o;
2989 }
2990 }
2991 /* FALLTHROUGH */
2992 default:
2993 nomod:
2994 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2995 /* grep, foreach, subcalls, refgen */
2996 if (S_potential_mod_type(type))
2997 break;
2998 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2999 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
3000 ? "do block"
3001 : OP_DESC(o)),
3002 type ? PL_op_desc[type] : "local"));
3003 return o;
3004
3005 case OP_PREINC:
3006 case OP_PREDEC:
3007 case OP_POW:
3008 case OP_MULTIPLY:
3009 case OP_DIVIDE:
3010 case OP_MODULO:
3011 case OP_ADD:
3012 case OP_SUBTRACT:
3013 case OP_CONCAT:
3014 case OP_LEFT_SHIFT:
3015 case OP_RIGHT_SHIFT:
3016 case OP_BIT_AND:
3017 case OP_BIT_XOR:
3018 case OP_BIT_OR:
3019 case OP_I_MULTIPLY:
3020 case OP_I_DIVIDE:
3021 case OP_I_MODULO:
3022 case OP_I_ADD:
3023 case OP_I_SUBTRACT:
3024 if (!(o->op_flags & OPf_STACKED))
3025 goto nomod;
3026 PL_modcount++;
3027 break;
3028
3029 case OP_REPEAT:
3030 if (o->op_flags & OPf_STACKED) {
3031 PL_modcount++;
3032 break;
3033 }
3034 if (!(o->op_private & OPpREPEAT_DOLIST))
3035 goto nomod;
3036 else {
3037 const I32 mods = PL_modcount;
3038 modkids(cBINOPo->op_first, type);
3039 if (type != OP_AASSIGN)
3040 goto nomod;
3041 kid = cBINOPo->op_last;
3042 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
3043 const IV iv = SvIV(kSVOP_sv);
3044 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
3045 PL_modcount =
3046 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
3047 }
3048 else
3049 PL_modcount = RETURN_UNLIMITED_NUMBER;
3050 }
3051 break;
3052
3053 case OP_COND_EXPR:
3054 localize = 1;
3055 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3056 op_lvalue(kid, type);
3057 break;
3058
3059 case OP_RV2AV:
3060 case OP_RV2HV:
3061 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3062 PL_modcount = RETURN_UNLIMITED_NUMBER;
3063 return o; /* Treat \(@foo) like ordinary list. */
3064 }
3065 /* FALLTHROUGH */
3066 case OP_RV2GV:
3067 if (scalar_mod_type(o, type))
3068 goto nomod;
3069 ref(cUNOPo->op_first, o->op_type);
3070 /* FALLTHROUGH */
3071 case OP_ASLICE:
3072 case OP_HSLICE:
3073 localize = 1;
3074 /* FALLTHROUGH */
3075 case OP_AASSIGN:
3076 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
3077 if (type == OP_LEAVESUBLV && (
3078 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
3079 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3080 ))
3081 o->op_private |= OPpMAYBE_LVSUB;
3082 /* FALLTHROUGH */
3083 case OP_NEXTSTATE:
3084 case OP_DBSTATE:
3085 PL_modcount = RETURN_UNLIMITED_NUMBER;
3086 break;
3087 case OP_KVHSLICE:
3088 case OP_KVASLICE:
3089 case OP_AKEYS:
3090 if (type == OP_LEAVESUBLV)
3091 o->op_private |= OPpMAYBE_LVSUB;
3092 goto nomod;
3093 case OP_AVHVSWITCH:
3094 if (type == OP_LEAVESUBLV
3095 && (o->op_private & 3) + OP_EACH == OP_KEYS)
3096 o->op_private |= OPpMAYBE_LVSUB;
3097 goto nomod;
3098 case OP_AV2ARYLEN:
3099 PL_hints |= HINT_BLOCK_SCOPE;
3100 if (type == OP_LEAVESUBLV)
3101 o->op_private |= OPpMAYBE_LVSUB;
3102 PL_modcount++;
3103 break;
3104 case OP_RV2SV:
3105 ref(cUNOPo->op_first, o->op_type);
3106 localize = 1;
3107 /* FALLTHROUGH */
3108 case OP_GV:
3109 PL_hints |= HINT_BLOCK_SCOPE;
3110 /* FALLTHROUGH */
3111 case OP_SASSIGN:
3112 case OP_ANDASSIGN:
3113 case OP_ORASSIGN:
3114 case OP_DORASSIGN:
3115 PL_modcount++;
3116 break;
3117
3118 case OP_AELEMFAST:
3119 case OP_AELEMFAST_LEX:
3120 localize = -1;
3121 PL_modcount++;
3122 break;
3123
3124 case OP_PADAV:
3125 case OP_PADHV:
3126 PL_modcount = RETURN_UNLIMITED_NUMBER;
3127 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
3128 return o; /* Treat \(@foo) like ordinary list. */
3129 if (scalar_mod_type(o, type))
3130 goto nomod;
3131 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3132 && type == OP_LEAVESUBLV)
3133 o->op_private |= OPpMAYBE_LVSUB;
3134 /* FALLTHROUGH */
3135 case OP_PADSV:
3136 PL_modcount++;
3137 if (!type) /* local() */
3138 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
3139 PNfARG(PAD_COMPNAME(o->op_targ)));
3140 if (!(o->op_private & OPpLVAL_INTRO)
3141 || ( type != OP_SASSIGN && type != OP_AASSIGN
3142 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
3143 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
3144 break;
3145
3146 case OP_PUSHMARK:
3147 localize = 0;
3148 break;
3149
3150 case OP_KEYS:
3151 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
3152 goto nomod;
3153 goto lvalue_func;
3154 case OP_SUBSTR:
3155 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
3156 goto nomod;
3157 /* FALLTHROUGH */
3158 case OP_POS:
3159 case OP_VEC:
3160 lvalue_func:
3161 if (type == OP_LEAVESUBLV)
3162 o->op_private |= OPpMAYBE_LVSUB;
3163 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
3164 /* substr and vec */
3165 /* If this op is in merely potential (non-fatal) modifiable
3166 context, then apply OP_ENTERSUB context to
3167 the kid op (to avoid croaking). Other-
3168 wise pass this op’s own type so the correct op is mentioned
3169 in error messages. */
3170 op_lvalue(OpSIBLING(cBINOPo->op_first),
3171 S_potential_mod_type(type)
3172 ? (I32)OP_ENTERSUB
3173 : o->op_type);
3174 }
3175 break;
3176
3177 case OP_AELEM:
3178 case OP_HELEM:
3179 ref(cBINOPo->op_first, o->op_type);
3180 if (type == OP_ENTERSUB &&
3181 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3182 o->op_private |= OPpLVAL_DEFER;
3183 if (type == OP_LEAVESUBLV)
3184 o->op_private |= OPpMAYBE_LVSUB;
3185 localize = 1;
3186 PL_modcount++;
3187 break;
3188
3189 case OP_LEAVE:
3190 case OP_LEAVELOOP:
3191 o->op_private |= OPpLVALUE;
3192 /* FALLTHROUGH */
3193 case OP_SCOPE:
3194 case OP_ENTER:
3195 case OP_LINESEQ:
3196 localize = 0;
3197 if (o->op_flags & OPf_KIDS)
3198 op_lvalue(cLISTOPo->op_last, type);
3199 break;
3200
3201 case OP_NULL:
3202 localize = 0;
3203 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3204 goto nomod;
3205 else if (!(o->op_flags & OPf_KIDS))
3206 break;
3207
3208 if (o->op_targ != OP_LIST) {
3209 OP *sib = OpSIBLING(cLISTOPo->op_first);
3210 /* OP_TRANS and OP_TRANSR with argument have a weird optree
3211 * that looks like
3212 *
3213 * null
3214 * arg
3215 * trans
3216 *
3217 * compared with things like OP_MATCH which have the argument
3218 * as a child:
3219 *
3220 * match
3221 * arg
3222 *
3223 * so handle specially to correctly get "Can't modify" croaks etc
3224 */
3225
3226 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
3227 {
3228 /* this should trigger a "Can't modify transliteration" err */
3229 op_lvalue(sib, type);
3230 }
3231 op_lvalue(cBINOPo->op_first, type);
3232 break;
3233 }
3234 /* FALLTHROUGH */
3235 case OP_LIST:
3236 localize = 0;
3237 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3238 /* elements might be in void context because the list is
3239 in scalar context or because they are attribute sub calls */
3240 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
3241 op_lvalue(kid, type);
3242 break;
3243
3244 case OP_COREARGS:
3245 return o;
3246
3247 case OP_AND:
3248 case OP_OR:
3249 if (type == OP_LEAVESUBLV
3250 || !S_vivifies(cLOGOPo->op_first->op_type))
3251 op_lvalue(cLOGOPo->op_first, type);
3252 if (type == OP_LEAVESUBLV
3253 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3254 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
3255 goto nomod;
3256
3257 case OP_SREFGEN:
3258 if (type == OP_NULL) { /* local */
3259 local_refgen:
3260 if (!FEATURE_MYREF_IS_ENABLED)
3261 Perl_croak(aTHX_ "The experimental declared_refs "
3262 "feature is not enabled");
3263 Perl_ck_warner_d(aTHX_
3264 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
3265 "Declaring references is experimental");
3266 op_lvalue(cUNOPo->op_first, OP_NULL);
3267 return o;
3268 }
3269 if (type != OP_AASSIGN && type != OP_SASSIGN
3270 && type != OP_ENTERLOOP)
3271 goto nomod;
3272 /* Don’t bother applying lvalue context to the ex-list. */
3273 kid = cUNOPx(cUNOPo->op_first)->op_first;
3274 assert (!OpHAS_SIBLING(kid));
3275 goto kid_2lvref;
3276 case OP_REFGEN:
3277 if (type == OP_NULL) /* local */
3278 goto local_refgen;
3279 if (type != OP_AASSIGN) goto nomod;
3280 kid = cUNOPo->op_first;
3281 kid_2lvref:
3282 {
3283 const U8 ec = PL_parser ? PL_parser->error_count : 0;
3284 S_lvref(aTHX_ kid, type);
3285 if (!PL_parser || PL_parser->error_count == ec) {
3286 if (!FEATURE_REFALIASING_IS_ENABLED)
3287 Perl_croak(aTHX_
3288 "Experimental aliasing via reference not enabled");
3289 Perl_ck_warner_d(aTHX_
3290 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3291 "Aliasing via reference is experimental");
3292 }
3293 }
3294 if (o->op_type == OP_REFGEN)
3295 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3296 op_null(o);
3297 return o;
3298
3299 case OP_SPLIT:
3300 if ((o->op_private & OPpSPLIT_ASSIGN)) {
3301 /* This is actually @array = split. */
3302 PL_modcount = RETURN_UNLIMITED_NUMBER;
3303 break;
3304 }
3305 goto nomod;
3306
3307 case OP_SCALAR:
3308 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3309 goto nomod;
3310 }
3311
3312 /* [20011101.069 (#7861)] File test operators interpret OPf_REF to mean that
3313 their argument is a filehandle; thus \stat(".") should not set
3314 it. AMS 20011102 */
3315 if (type == OP_REFGEN &&
3316 PL_check[o->op_type] == Perl_ck_ftst)
3317 return o;
3318
3319 if (type != OP_LEAVESUBLV)
3320 o->op_flags |= OPf_MOD;
3321
3322 if (type == OP_AASSIGN || type == OP_SASSIGN)
3323 o->op_flags |= OPf_SPECIAL
3324 |(o->op_type == OP_ENTERSUB ? 0 : OPf_REF);
3325 else if (!type) { /* local() */
3326 switch (localize) {
3327 case 1:
3328 o->op_private |= OPpLVAL_INTRO;
3329 o->op_flags &= ~OPf_SPECIAL;
3330 PL_hints |= HINT_BLOCK_SCOPE;
3331 break;
3332 case 0:
3333 break;
3334 case -1:
3335 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3336 "Useless localization of %s", OP_DESC(o));
3337 }
3338 }
3339 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3340 && type != OP_LEAVESUBLV && o->op_type != OP_ENTERSUB)
3341 o->op_flags |= OPf_REF;
3342 return o;
3343}
3344
3345STATIC bool
3346S_scalar_mod_type(const OP *o, I32 type)
3347{
3348 switch (type) {
3349 case OP_POS:
3350 case OP_SASSIGN:
3351 if (o && o->op_type == OP_RV2GV)
3352 return FALSE;
3353 /* FALLTHROUGH */
3354 case OP_PREINC:
3355 case OP_PREDEC:
3356 case OP_POSTINC:
3357 case OP_POSTDEC:
3358 case OP_I_PREINC:
3359 case OP_I_PREDEC:
3360 case OP_I_POSTINC:
3361 case OP_I_POSTDEC:
3362 case OP_POW:
3363 case OP_MULTIPLY:
3364 case OP_DIVIDE:
3365 case OP_MODULO:
3366 case OP_REPEAT:
3367 case OP_ADD:
3368 case OP_SUBTRACT:
3369 case OP_I_MULTIPLY:
3370 case OP_I_DIVIDE:
3371 case OP_I_MODULO:
3372 case OP_I_ADD:
3373 case OP_I_SUBTRACT:
3374 case OP_LEFT_SHIFT:
3375 case OP_RIGHT_SHIFT:
3376 case OP_BIT_AND:
3377 case OP_BIT_XOR:
3378 case OP_BIT_OR:
3379 case OP_NBIT_AND:
3380 case OP_NBIT_XOR:
3381 case OP_NBIT_OR:
3382 case OP_SBIT_AND:
3383 case OP_SBIT_XOR:
3384 case OP_SBIT_OR:
3385 case OP_CONCAT:
3386 case OP_SUBST:
3387 case OP_TRANS:
3388 case OP_TRANSR:
3389 case OP_READ:
3390 case OP_SYSREAD:
3391 case OP_RECV:
3392 case OP_ANDASSIGN:
3393 case OP_ORASSIGN:
3394 case OP_DORASSIGN:
3395 case OP_VEC:
3396 case OP_SUBSTR:
3397 return TRUE;
3398 default:
3399 return FALSE;
3400 }
3401}
3402
3403STATIC bool
3404S_is_handle_constructor(const OP *o, I32 numargs)
3405{
3406 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3407
3408 switch (o->op_type) {
3409 case OP_PIPE_OP:
3410 case OP_SOCKPAIR:
3411 if (numargs == 2)
3412 return TRUE;
3413 /* FALLTHROUGH */
3414 case OP_SYSOPEN:
3415 case OP_OPEN:
3416 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3417 case OP_SOCKET:
3418 case OP_OPEN_DIR:
3419 case OP_ACCEPT:
3420 if (numargs == 1)
3421 return TRUE;
3422 /* FALLTHROUGH */
3423 default:
3424 return FALSE;
3425 }
3426}
3427
3428static OP *
3429S_refkids(pTHX_ OP *o, I32 type)
3430{
3431 if (o && o->op_flags & OPf_KIDS) {
3432 OP *kid;
3433 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3434 ref(kid, type);
3435 }
3436 return o;
3437}
3438
3439OP *
3440Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3441{
3442 dVAR;
3443 OP *kid;
3444
3445 PERL_ARGS_ASSERT_DOREF;
3446
3447 if (PL_parser && PL_parser->error_count)
3448 return o;
3449
3450 switch (o->op_type) {
3451 case OP_ENTERSUB:
3452 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3453 !(o->op_flags & OPf_STACKED)) {
3454 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
3455 assert(cUNOPo->op_first->op_type == OP_NULL);
3456 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
3457 o->op_flags |= OPf_SPECIAL;
3458 }
3459 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3460 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3461 : type == OP_RV2HV ? OPpDEREF_HV
3462 : OPpDEREF_SV);
3463 o->op_flags |= OPf_MOD;
3464 }
3465
3466 break;
3467
3468 case OP_COND_EXPR:
3469 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3470 doref(kid, type, set_op_ref);
3471 break;
3472 case OP_RV2SV:
3473 if (type == OP_DEFINED)
3474 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3475 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3476 /* FALLTHROUGH */
3477 case OP_PADSV:
3478 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3479 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3480 : type == OP_RV2HV ? OPpDEREF_HV
3481 : OPpDEREF_SV);
3482 o->op_flags |= OPf_MOD;
3483 }
3484 break;
3485
3486 case OP_RV2AV:
3487 case OP_RV2HV:
3488 if (set_op_ref)
3489 o->op_flags |= OPf_REF;
3490 /* FALLTHROUGH */
3491 case OP_RV2GV:
3492 if (type == OP_DEFINED)
3493 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3494 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3495 break;
3496
3497 case OP_PADAV:
3498 case OP_PADHV:
3499 if (set_op_ref)
3500 o->op_flags |= OPf_REF;
3501 break;
3502
3503 case OP_SCALAR:
3504 case OP_NULL:
3505 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3506 break;
3507 doref(cBINOPo->op_first, type, set_op_ref);
3508 break;
3509 case OP_AELEM:
3510 case OP_HELEM:
3511 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3512 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3513 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3514 : type == OP_RV2HV ? OPpDEREF_HV
3515 : OPpDEREF_SV);
3516 o->op_flags |= OPf_MOD;
3517 }
3518 break;
3519
3520 case OP_SCOPE:
3521 case OP_LEAVE:
3522 set_op_ref = FALSE;
3523 /* FALLTHROUGH */
3524 case OP_ENTER:
3525 case OP_LIST:
3526 if (!(o->op_flags & OPf_KIDS))
3527 break;
3528 doref(cLISTOPo->op_last, type, set_op_ref);
3529 break;
3530 default:
3531 break;
3532 }
3533 return scalar(o);
3534
3535}
3536
3537STATIC OP *
3538S_dup_attrlist(pTHX_ OP *o)
3539{
3540 OP *rop;
3541
3542 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3543
3544 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3545 * where the first kid is OP_PUSHMARK and the remaining ones
3546 * are OP_CONST. We need to push the OP_CONST values.
3547 */
3548 if (o->op_type == OP_CONST)
3549 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3550 else {
3551 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3552 rop = NULL;
3553 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
3554 if (o->op_type == OP_CONST)
3555 rop = op_append_elem(OP_LIST, rop,
3556 newSVOP(OP_CONST, o->op_flags,
3557 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3558 }
3559 }
3560 return rop;
3561}
3562
3563STATIC void
3564S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3565{
3566 PERL_ARGS_ASSERT_APPLY_ATTRS;
3567 {
3568 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
3569
3570 /* fake up C<use attributes $pkg,$rv,@attrs> */
3571
3572#define ATTRSMODULE "attributes"
3573#define ATTRSMODULE_PM "attributes.pm"
3574
3575 Perl_load_module(
3576 aTHX_ PERL_LOADMOD_IMPORT_OPS,
3577 newSVpvs(ATTRSMODULE),
3578 NULL,
3579 op_prepend_elem(OP_LIST,
3580 newSVOP(OP_CONST, 0, stashsv),
3581 op_prepend_elem(OP_LIST,
3582 newSVOP(OP_CONST, 0,
3583 newRV(target)),
3584 dup_attrlist(attrs))));
3585 }
3586}
3587
3588STATIC void
3589S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3590{
3591 OP *pack, *imop, *arg;
3592 SV *meth, *stashsv, **svp;
3593
3594 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3595
3596 if (!attrs)
3597 return;
3598
3599 assert(target->op_type == OP_PADSV ||
3600 target->op_type == OP_PADHV ||
3601 target->op_type == OP_PADAV);
3602
3603 /* Ensure that attributes.pm is loaded. */
3604 /* Don't force the C<use> if we don't need it. */
3605 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3606 if (svp && *svp != &PL_sv_undef)
3607 NOOP; /* already in %INC */
3608 else
3609 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3610 newSVpvs(ATTRSMODULE), NULL);
3611
3612 /* Need package name for method call. */
3613 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3614
3615 /* Build up the real arg-list. */
3616 stashsv = newSVhek(HvNAME_HEK(stash));
3617
3618 arg = newOP(OP_PADSV, 0);
3619 arg->op_targ = target->op_targ;
3620 arg = op_prepend_elem(OP_LIST,
3621 newSVOP(OP_CONST, 0, stashsv),
3622 op_prepend_elem(OP_LIST,
3623 newUNOP(OP_REFGEN, 0,
3624 arg),
3625 dup_attrlist(attrs)));
3626
3627 /* Fake up a method call to import */
3628 meth = newSVpvs_share("import");
3629 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3630 op_append_elem(OP_LIST,
3631 op_prepend_elem(OP_LIST, pack, arg),
3632 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3633
3634 /* Combine the ops. */
3635 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3636}
3637
3638/*
3639=notfor apidoc apply_attrs_string
3640
3641Attempts to apply a list of attributes specified by the C<attrstr> and
3642C<len> arguments to the subroutine identified by the C<cv> argument which
3643is expected to be associated with the package identified by the C<stashpv>
3644argument (see L<attributes>). It gets this wrong, though, in that it
3645does not correctly identify the boundaries of the individual attribute
3646specifications within C<attrstr>. This is not really intended for the
3647public API, but has to be listed here for systems such as AIX which
3648need an explicit export list for symbols. (It's called from XS code
3649in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3650to respect attribute syntax properly would be welcome.
3651
3652=cut
3653*/
3654
3655void
3656Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3657 const char *attrstr, STRLEN len)
3658{
3659 OP *attrs = NULL;
3660
3661 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3662
3663 if (!len) {
3664 len = strlen(attrstr);
3665 }
3666
3667 while (len) {
3668 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3669 if (len) {
3670 const char * const sstr = attrstr;
3671 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3672 attrs = op_append_elem(OP_LIST, attrs,
3673 newSVOP(OP_CONST, 0,
3674 newSVpvn(sstr, attrstr-sstr)));
3675 }
3676 }
3677
3678 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3679 newSVpvs(ATTRSMODULE),
3680 NULL, op_prepend_elem(OP_LIST,
3681 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3682 op_prepend_elem(OP_LIST,
3683 newSVOP(OP_CONST, 0,
3684 newRV(MUTABLE_SV(cv))),
3685 attrs)));
3686}
3687
3688STATIC void
3689S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3690{
3691 OP *new_proto = NULL;
3692 STRLEN pvlen;
3693 char *pv;
3694 OP *o;
3695
3696 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3697
3698 if (!*attrs)
3699 return;
3700
3701 o = *attrs;
3702 if (o->op_type == OP_CONST) {
3703 pv = SvPV(cSVOPo_sv, pvlen);
3704 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3705 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3706 SV ** const tmpo = cSVOPx_svp(o);
3707 SvREFCNT_dec(cSVOPo_sv);
3708 *tmpo = tmpsv;
3709 new_proto = o;
3710 *attrs = NULL;
3711 }
3712 } else if (o->op_type == OP_LIST) {
3713 OP * lasto;
3714 assert(o->op_flags & OPf_KIDS);
3715 lasto = cLISTOPo->op_first;
3716 assert(lasto->op_type == OP_PUSHMARK);
3717 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
3718 if (o->op_type == OP_CONST) {
3719 pv = SvPV(cSVOPo_sv, pvlen);
3720 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3721 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3722 SV ** const tmpo = cSVOPx_svp(o);
3723 SvREFCNT_dec(cSVOPo_sv);
3724 *tmpo = tmpsv;
3725 if (new_proto && ckWARN(WARN_MISC)) {
3726 STRLEN new_len;
3727 const char * newp = SvPV(cSVOPo_sv, new_len);
3728 Perl_warner(aTHX_ packWARN(WARN_MISC),
3729 "Attribute prototype(%" UTF8f ") discards earlier prototype attribute in same sub",
3730 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3731 op_free(new_proto);
3732 }
3733 else if (new_proto)
3734 op_free(new_proto);
3735 new_proto = o;
3736 /* excise new_proto from the list */
3737 op_sibling_splice(*attrs, lasto, 1, NULL);
3738 o = lasto;
3739 continue;
3740 }
3741 }
3742 lasto = o;
3743 }
3744 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3745 would get pulled in with no real need */
3746 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3747 op_free(*attrs);
3748 *attrs = NULL;
3749 }
3750 }
3751
3752 if (new_proto) {
3753 SV *svname;
3754 if (isGV(name)) {
3755 svname = sv_newmortal();
3756 gv_efullname3(svname, name, NULL);
3757 }
3758 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3759 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3760 else
3761 svname = (SV *)name;
3762 if (ckWARN(WARN_ILLEGALPROTO))
3763 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3764 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3765 STRLEN old_len, new_len;
3766 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3767 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3768
3769 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3770 "Prototype '%" UTF8f "' overridden by attribute 'prototype(%" UTF8f ")'"
3771 " in %" SVf,
3772 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3773 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3774 SVfARG(svname));
3775 }
3776 if (*proto)
3777 op_free(*proto);
3778 *proto = new_proto;
3779 }
3780}
3781
3782static void
3783S_cant_declare(pTHX_ OP *o)
3784{
3785 if (o->op_type == OP_NULL
3786 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3787 o = cUNOPo->op_first;
3788 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3789 o->op_type == OP_NULL
3790 && o->op_flags & OPf_SPECIAL
3791 ? "do block"
3792 : OP_DESC(o),
3793 PL_parser->in_my == KEY_our ? "our" :
3794 PL_parser->in_my == KEY_state ? "state" :
3795 "my"));
3796}
3797
3798STATIC OP *
3799S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3800{
3801 I32 type;
3802 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3803
3804 PERL_ARGS_ASSERT_MY_KID;
3805
3806 if (!o || (PL_parser && PL_parser->error_count))
3807 return o;
3808
3809 type = o->op_type;
3810
3811 if (OP_TYPE_IS_OR_WAS(o, OP_LIST)) {
3812 OP *kid;
3813 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3814 my_kid(kid, attrs, imopsp);
3815 return o;
3816 } else if (type == OP_UNDEF || type == OP_STUB) {
3817 return o;
3818 } else if (type == OP_RV2SV || /* "our" declaration */
3819 type == OP_RV2AV ||
3820 type == OP_RV2HV) {
3821 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3822 S_cant_declare(aTHX_ o);
3823 } else if (attrs) {
3824 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3825 assert(PL_parser);
3826 PL_parser->in_my = FALSE;
3827 PL_parser->in_my_stash = NULL;
3828 apply_attrs(GvSTASH(gv),
3829 (type == OP_RV2SV ? GvSV(gv) :
3830 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3831 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3832 attrs);
3833 }
3834 o->op_private |= OPpOUR_INTRO;
3835 return o;
3836 }
3837 else if (type == OP_REFGEN || type == OP_SREFGEN) {
3838 if (!FEATURE_MYREF_IS_ENABLED)
3839 Perl_croak(aTHX_ "The experimental declared_refs "
3840 "feature is not enabled");
3841 Perl_ck_warner_d(aTHX_
3842 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
3843 "Declaring references is experimental");
3844 /* Kid is a nulled OP_LIST, handled above. */
3845 my_kid(cUNOPo->op_first, attrs, imopsp);
3846 return o;
3847 }
3848 else if (type != OP_PADSV &&
3849 type != OP_PADAV &&
3850 type != OP_PADHV &&
3851 type != OP_PUSHMARK)
3852 {
3853 S_cant_declare(aTHX_ o);
3854 return o;
3855 }
3856 else if (attrs && type != OP_PUSHMARK) {
3857 HV *stash;
3858
3859 assert(PL_parser);
3860 PL_parser->in_my = FALSE;
3861 PL_parser->in_my_stash = NULL;
3862
3863 /* check for C<my Dog $spot> when deciding package */
3864 stash = PAD_COMPNAME_TYPE(o->op_targ);
3865 if (!stash)
3866 stash = PL_curstash;
3867 apply_attrs_my(stash, o, attrs, imopsp);
3868 }
3869 o->op_flags |= OPf_MOD;
3870 o->op_private |= OPpLVAL_INTRO;
3871 if (stately)
3872 o->op_private |= OPpPAD_STATE;
3873 return o;
3874}
3875
3876OP *
3877Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3878{
3879 OP *rops;
3880 int maybe_scalar = 0;
3881
3882 PERL_ARGS_ASSERT_MY_ATTRS;
3883
3884/* [perl #17376]: this appears to be premature, and results in code such as
3885 C< our(%x); > executing in list mode rather than void mode */
3886#if 0
3887 if (o->op_flags & OPf_PARENS)
3888 list(o);
3889 else
3890 maybe_scalar = 1;
3891#else
3892 maybe_scalar = 1;
3893#endif
3894 if (attrs)
3895 SAVEFREEOP(attrs);
3896 rops = NULL;
3897 o = my_kid(o, attrs, &rops);
3898 if (rops) {
3899 if (maybe_scalar && o->op_type == OP_PADSV) {
3900 o = scalar(op_append_list(OP_LIST, rops, o));
3901 o->op_private |= OPpLVAL_INTRO;
3902 }
3903 else {
3904 /* The listop in rops might have a pushmark at the beginning,
3905 which will mess up list assignment. */
3906 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3907 if (rops->op_type == OP_LIST &&
3908 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3909 {
3910 OP * const pushmark = lrops->op_first;
3911 /* excise pushmark */
3912 op_sibling_splice(rops, NULL, 1, NULL);
3913 op_free(pushmark);
3914 }
3915 o = op_append_list(OP_LIST, o, rops);
3916 }
3917 }
3918 PL_parser->in_my = FALSE;
3919 PL_parser->in_my_stash = NULL;
3920 return o;
3921}
3922
3923OP *
3924Perl_sawparens(pTHX_ OP *o)
3925{
3926 PERL_UNUSED_CONTEXT;
3927 if (o)
3928 o->op_flags |= OPf_PARENS;
3929 return o;
3930}
3931
3932OP *
3933Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3934{
3935 OP *o;
3936 bool ismatchop = 0;
3937 const OPCODE ltype = left->op_type;
3938 const OPCODE rtype = right->op_type;
3939
3940 PERL_ARGS_ASSERT_BIND_MATCH;
3941
3942 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3943 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3944 {
3945 const char * const desc
3946 = PL_op_desc[(
3947 rtype == OP_SUBST || rtype == OP_TRANS
3948 || rtype == OP_TRANSR
3949 )
3950 ? (int)rtype : OP_MATCH];
3951 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3952 SV * const name =
3953 S_op_varname(aTHX_ left);
3954 if (name)
3955 Perl_warner(aTHX_ packWARN(WARN_MISC),
3956 "Applying %s to %" SVf " will act on scalar(%" SVf ")",
3957 desc, SVfARG(name), SVfARG(name));
3958 else {
3959 const char * const sample = (isary
3960 ? "@array" : "%hash");
3961 Perl_warner(aTHX_ packWARN(WARN_MISC),
3962 "Applying %s to %s will act on scalar(%s)",
3963 desc, sample, sample);
3964 }
3965 }
3966
3967 if (rtype == OP_CONST &&
3968 cSVOPx(right)->op_private & OPpCONST_BARE &&
3969 cSVOPx(right)->op_private & OPpCONST_STRICT)
3970 {
3971 no_bareword_allowed(right);
3972 }
3973
3974 /* !~ doesn't make sense with /r, so error on it for now */
3975 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3976 type == OP_NOT)
3977 /* diag_listed_as: Using !~ with %s doesn't make sense */
3978 yyerror("Using !~ with s///r doesn't make sense");
3979 if (rtype == OP_TRANSR && type == OP_NOT)
3980 /* diag_listed_as: Using !~ with %s doesn't make sense */
3981 yyerror("Using !~ with tr///r doesn't make sense");
3982
3983 ismatchop = (rtype == OP_MATCH ||
3984 rtype == OP_SUBST ||
3985 rtype == OP_TRANS || rtype == OP_TRANSR)
3986 && !(right->op_flags & OPf_SPECIAL);
3987 if (ismatchop && right->op_private & OPpTARGET_MY) {
3988 right->op_targ = 0;
3989 right->op_private &= ~OPpTARGET_MY;
3990 }
3991 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
3992 if (left->op_type == OP_PADSV
3993 && !(left->op_private & OPpLVAL_INTRO))
3994 {
3995 right->op_targ = left->op_targ;
3996 op_free(left);
3997 o = right;
3998 }
3999 else {
4000 right->op_flags |= OPf_STACKED;
4001 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
4002 ! (rtype == OP_TRANS &&
4003 right->op_private & OPpTRANS_IDENTICAL) &&
4004 ! (rtype == OP_SUBST &&
4005 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
4006 left = op_lvalue(left, rtype);
4007 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
4008 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
4009 else
4010 o = op_prepend_elem(rtype, scalar(left), right);
4011 }
4012 if (type == OP_NOT)
4013 return newUNOP(OP_NOT, 0, scalar(o));
4014 return o;
4015 }
4016 else
4017 return bind_match(type, left,
4018 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
4019}
4020
4021OP *
4022Perl_invert(pTHX_ OP *o)
4023{
4024 if (!o)
4025 return NULL;
4026 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
4027}
4028
4029/*
4030=for apidoc Amx|OP *|op_scope|OP *o
4031
4032Wraps up an op tree with some additional ops so that at runtime a dynamic
4033scope will be created. The original ops run in the new dynamic scope,
4034and then, provided that they exit normally, the scope will be unwound.
4035The additional ops used to create and unwind the dynamic scope will
4036normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
4037instead if the ops are simple enough to not need the full dynamic scope
4038structure.
4039
4040=cut
4041*/
4042
4043OP *
4044Perl_op_scope(pTHX_ OP *o)
4045{
4046 dVAR;
4047 if (o) {
4048 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
4049 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
4050 OpTYPE_set(o, OP_LEAVE);
4051 }
4052 else if (o->op_type == OP_LINESEQ) {
4053 OP *kid;
4054 OpTYPE_set(o, OP_SCOPE);
4055 kid = ((LISTOP*)o)->op_first;
4056 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
4057 op_null(kid);
4058
4059 /* The following deals with things like 'do {1 for 1}' */
4060 kid = OpSIBLING(kid);
4061 if (kid &&
4062 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
4063 op_null(kid);
4064 }
4065 }
4066 else
4067 o = newLISTOP(OP_SCOPE, 0, o, NULL);
4068 }
4069 return o;
4070}
4071
4072OP *
4073Perl_op_unscope(pTHX_ OP *o)
4074{
4075 if (o && o->op_type == OP_LINESEQ) {
4076 OP *kid = cLISTOPo->op_first;
4077 for(; kid; kid = OpSIBLING(kid))
4078 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
4079 op_null(kid);
4080 }
4081 return o;
4082}
4083
4084/*
4085=for apidoc Am|int|block_start|int full
4086
4087Handles compile-time scope entry.
4088Arranges for hints to be restored on block
4089exit and also handles pad sequence numbers to make lexical variables scope
4090right. Returns a savestack index for use with C<block_end>.
4091
4092=cut
4093*/
4094
4095int
4096Perl_block_start(pTHX_ int full)
4097{
4098 const int retval = PL_savestack_ix;
4099
4100 PL_compiling.cop_seq = PL_cop_seqmax;
4101 COP_SEQMAX_INC;
4102 pad_block_start(full);
4103 SAVEHINTS();
4104 PL_hints &= ~HINT_BLOCK_SCOPE;
4105 SAVECOMPILEWARNINGS();
4106 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
4107 SAVEI32(PL_compiling.cop_seq);
4108 PL_compiling.cop_seq = 0;
4109
4110 CALL_BLOCK_HOOKS(bhk_start, full);
4111
4112 return retval;
4113}
4114
4115/*
4116=for apidoc Am|OP *|block_end|I32 floor|OP *seq
4117
4118Handles compile-time scope exit. C<floor>
4119is the savestack index returned by
4120C<block_start>, and C<seq> is the body of the block. Returns the block,
4121possibly modified.
4122
4123=cut
4124*/
4125
4126OP*
4127Perl_block_end(pTHX_ I32 floor, OP *seq)
4128{
4129 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
4130 OP* retval = scalarseq(seq);
4131 OP *o;
4132
4133 /* XXX Is the null PL_parser check necessary here? */
4134 assert(PL_parser); /* Let’s find out under debugging builds. */
4135 if (PL_parser && PL_parser->parsed_sub) {
4136 o = newSTATEOP(0, NULL, NULL);
4137 op_null(o);
4138 retval = op_append_elem(OP_LINESEQ, retval, o);
4139 }
4140
4141 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
4142
4143 LEAVE_SCOPE(floor);
4144 if (needblockscope)
4145 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
4146 o = pad_leavemy();
4147
4148 if (o) {
4149 /* pad_leavemy has created a sequence of introcv ops for all my
4150 subs declared in the block. We have to replicate that list with
4151 clonecv ops, to deal with this situation:
4152
4153 sub {
4154 my sub s1;
4155 my sub s2;
4156 sub s1 { state sub foo { \&s2 } }
4157 }->()
4158
4159 Originally, I was going to have introcv clone the CV and turn
4160 off the stale flag. Since &s1 is declared before &s2, the
4161 introcv op for &s1 is executed (on sub entry) before the one for
4162 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
4163 cloned, since it is a state sub) closes over &s2 and expects
4164 to see it in its outer CV’s pad. If the introcv op clones &s1,
4165 then &s2 is still marked stale. Since &s1 is not active, and
4166 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
4167 ble will not stay shared’ warning. Because it is the same stub
4168 that will be used when the introcv op for &s2 is executed, clos-
4169 ing over it is safe. Hence, we have to turn off the stale flag
4170 on all lexical subs in the block before we clone any of them.
4171 Hence, having introcv clone the sub cannot work. So we create a
4172 list of ops like this:
4173
4174 lineseq
4175 |
4176 +-- introcv
4177 |
4178 +-- introcv
4179 |
4180 +-- introcv
4181 |
4182 .
4183 .
4184 .
4185 |
4186 +-- clonecv
4187 |
4188 +-- clonecv
4189 |
4190 +-- clonecv
4191 |
4192 .
4193 .
4194 .
4195 */
4196 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
4197 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
4198 for (;; kid = OpSIBLING(kid)) {
4199 OP *newkid = newOP(OP_CLONECV, 0);
4200 newkid->op_targ = kid->op_targ;
4201 o = op_append_elem(OP_LINESEQ, o, newkid);
4202 if (kid == last) break;
4203 }
4204 retval = op_prepend_elem(OP_LINESEQ, o, retval);
4205 }
4206
4207 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
4208
4209 return retval;
4210}
4211
4212/*
4213=head1 Compile-time scope hooks
4214
4215=for apidoc Aox||blockhook_register
4216
4217Register a set of hooks to be called when the Perl lexical scope changes
4218at compile time. See L<perlguts/"Compile-time scope hooks">.
4219
4220=cut
4221*/
4222
4223void
4224Perl_blockhook_register(pTHX_ BHK *hk)
4225{
4226 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
4227
4228 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
4229}
4230
4231void
4232Perl_newPROG(pTHX_ OP *o)
4233{
4234 OP *start;
4235
4236 PERL_ARGS_ASSERT_NEWPROG;
4237
4238 if (PL_in_eval) {
4239 PERL_CONTEXT *cx;
4240 I32 i;
4241 if (PL_eval_root)
4242 return;
4243 PL_eval_root = newUNOP(OP_LEAVEEVAL,
4244 ((PL_in_eval & EVAL_KEEPERR)
4245 ? OPf_SPECIAL : 0), o);
4246
4247 cx = CX_CUR();
4248 assert(CxTYPE(cx) == CXt_EVAL);
4249
4250 if ((cx->blk_gimme & G_WANT) == G_VOID)
4251 scalarvoid(PL_eval_root);
4252 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
4253 list(PL_eval_root);
4254 else
4255 scalar(PL_eval_root);
4256
4257 start = op_linklist(PL_eval_root);
4258 PL_eval_root->op_next = 0;
4259 i = PL_savestack_ix;
4260 SAVEFREEOP(o);
4261 ENTER;
4262 S_process_optree(aTHX_ NULL, PL_eval_root, start);
4263 LEAVE;
4264 PL_savestack_ix = i;
4265 }
4266 else {
4267 if (o->op_type == OP_STUB) {
4268 /* This block is entered if nothing is compiled for the main
4269 program. This will be the case for an genuinely empty main
4270 program, or one which only has BEGIN blocks etc, so already
4271 run and freed.
4272
4273 Historically (5.000) the guard above was !o. However, commit
4274 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
4275 c71fccf11fde0068, changed perly.y so that newPROG() is now
4276 called with the output of block_end(), which returns a new
4277 OP_STUB for the case of an empty optree. ByteLoader (and
4278 maybe other things) also take this path, because they set up
4279 PL_main_start and PL_main_root directly, without generating an
4280 optree.
4281
4282 If the parsing the main program aborts (due to parse errors,
4283 or due to BEGIN or similar calling exit), then newPROG()
4284 isn't even called, and hence this code path and its cleanups
4285 are skipped. This shouldn't make a make a difference:
4286 * a non-zero return from perl_parse is a failure, and
4287 perl_destruct() should be called immediately.
4288 * however, if exit(0) is called during the parse, then
4289 perl_parse() returns 0, and perl_run() is called. As
4290 PL_main_start will be NULL, perl_run() will return
4291 promptly, and the exit code will remain 0.
4292 */
4293
4294 PL_comppad_name = 0;
4295 PL_compcv = 0;
4296 S_op_destroy(aTHX_ o);
4297 return;
4298 }
4299 PL_main_root = op_scope(sawparens(scalarvoid(o)));
4300 PL_curcop = &PL_compiling;
4301 start = LINKLIST(PL_main_root);
4302 PL_main_root->op_next = 0;
4303 S_process_optree(aTHX_ NULL, PL_main_root, start);
4304 cv_forget_slab(PL_compcv);
4305 PL_compcv = 0;
4306
4307 /* Register with debugger */
4308 if (PERLDB_INTER) {
4309 CV * const cv = get_cvs("DB::postponed", 0);
4310 if (cv) {
4311 dSP;
4312 PUSHMARK(SP);
4313 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
4314 PUTBACK;
4315 call_sv(MUTABLE_SV(cv), G_DISCARD);
4316 }
4317 }
4318 }
4319}
4320
4321OP *
4322Perl_localize(pTHX_ OP *o, I32 lex)
4323{
4324 PERL_ARGS_ASSERT_LOCALIZE;
4325
4326 if (o->op_flags & OPf_PARENS)
4327/* [perl #17376]: this appears to be premature, and results in code such as
4328 C< our(%x); > executing in list mode rather than void mode */
4329#if 0
4330 list(o);
4331#else
4332 NOOP;
4333#endif
4334 else {
4335 if ( PL_parser->bufptr > PL_parser->oldbufptr
4336 && PL_parser->bufptr[-1] == ','
4337 && ckWARN(WARN_PARENTHESIS))
4338 {
4339 char *s = PL_parser->bufptr;
4340 bool sigil = FALSE;
4341
4342 /* some heuristics to detect a potential error */
4343 while (*s && (strchr(", \t\n", *s)))
4344 s++;
4345
4346 while (1) {
4347 if (*s && (strchr("@$%", *s) || (!lex && *s == '*'))
4348 && *++s
4349 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
4350 s++;
4351 sigil = TRUE;
4352 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
4353 s++;
4354 while (*s && (strchr(", \t\n", *s)))
4355 s++;
4356 }
4357 else
4358 break;
4359 }
4360 if (sigil && (*s == ';' || *s == '=')) {
4361 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
4362 "Parentheses missing around \"%s\" list",
4363 lex
4364 ? (PL_parser->in_my == KEY_our
4365 ? "our"
4366 : PL_parser->in_my == KEY_state
4367 ? "state"
4368 : "my")
4369 : "local");
4370 }
4371 }
4372 }
4373 if (lex)
4374 o = my(o);
4375 else
4376 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4377 PL_parser->in_my = FALSE;
4378 PL_parser->in_my_stash = NULL;
4379 return o;
4380}
4381
4382OP *
4383Perl_jmaybe(pTHX_ OP *o)
4384{
4385 PERL_ARGS_ASSERT_JMAYBE;
4386
4387 if (o->op_type == OP_LIST) {
4388 OP * const o2
4389 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4390 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4391 }
4392 return o;
4393}
4394
4395PERL_STATIC_INLINE OP *
4396S_op_std_init(pTHX_ OP *o)
4397{
4398 I32 type = o->op_type;
4399
4400 PERL_ARGS_ASSERT_OP_STD_INIT;
4401
4402 if (PL_opargs[type] & OA_RETSCALAR)
4403 scalar(o);
4404 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4405 o->op_targ = pad_alloc(type, SVs_PADTMP);
4406
4407 return o;
4408}
4409
4410PERL_STATIC_INLINE OP *
4411S_op_integerize(pTHX_ OP *o)
4412{
4413 I32 type = o->op_type;
4414
4415 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4416
4417 /* integerize op. */
4418 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4419 {
4420 dVAR;
4421 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4422 }
4423
4424 if (type == OP_NEGATE)
4425 /* XXX might want a ck_negate() for this */
4426 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4427
4428 return o;
4429}
4430
4431static OP *
4432S_fold_constants(pTHX_ OP *const o)
4433{
4434 dVAR;
4435 OP * VOL curop;
4436 OP *newop;
4437 VOL I32 type = o->op_type;
4438 bool is_stringify;
4439 SV * VOL sv = NULL;
4440 int ret = 0;
4441 OP *old_next;
4442 SV * const oldwarnhook = PL_warnhook;
4443 SV * const olddiehook = PL_diehook;
4444 COP not_compiling;
4445 U8 oldwarn = PL_dowarn;
4446 I32 old_cxix;
4447 dJMPENV;
4448
4449 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4450
4451 if (!(PL_opargs[type] & OA_FOLDCONST))
4452 goto nope;
4453
4454 switch (type) {
4455 case OP_UCFIRST:
4456 case OP_LCFIRST:
4457 case OP_UC:
4458 case OP_LC:
4459 case OP_FC:
4460#ifdef USE_LOCALE_CTYPE
4461 if (IN_LC_COMPILETIME(LC_CTYPE))
4462 goto nope;
4463#endif
4464 break;
4465 case OP_SLT:
4466 case OP_SGT:
4467 case OP_SLE:
4468 case OP_SGE:
4469 case OP_SCMP:
4470#ifdef USE_LOCALE_COLLATE
4471 if (IN_LC_COMPILETIME(LC_COLLATE))
4472 goto nope;
4473#endif
4474 break;
4475 case OP_SPRINTF:
4476 /* XXX what about the numeric ops? */
4477#ifdef USE_LOCALE_NUMERIC
4478 if (IN_LC_COMPILETIME(LC_NUMERIC))
4479 goto nope;
4480#endif
4481 break;
4482 case OP_PACK:
4483 if (!OpHAS_SIBLING(cLISTOPo->op_first)
4484 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4485 goto nope;
4486 {
4487 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
4488 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4489 {
4490 const char *s = SvPVX_const(sv);
4491 while (s < SvEND(sv)) {
4492 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4493 s++;
4494 }
4495 }
4496 }
4497 break;
4498 case OP_REPEAT:
4499 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4500 break;
4501 case OP_SREFGEN:
4502 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4503 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4504 goto nope;
4505 }
4506
4507 if (PL_parser && PL_parser->error_count)
4508 goto nope; /* Don't try to run w/ errors */
4509
4510 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4511 switch (curop->op_type) {
4512 case OP_CONST:
4513 if ( (curop->op_private & OPpCONST_BARE)
4514 && (curop->op_private & OPpCONST_STRICT)) {
4515 no_bareword_allowed(curop);
4516 goto nope;
4517 }
4518 /* FALLTHROUGH */
4519 case OP_LIST:
4520 case OP_SCALAR:
4521 case OP_NULL:
4522 case OP_PUSHMARK:
4523 /* Foldable; move to next op in list */
4524 break;
4525
4526 default:
4527 /* No other op types are considered foldable */
4528 goto nope;
4529 }
4530 }
4531
4532 curop = LINKLIST(o);
4533 old_next = o->op_next;
4534 o->op_next = 0;
4535 PL_op = curop;
4536
4537 old_cxix = cxstack_ix;
4538 create_eval_scope(NULL, G_FAKINGEVAL);
4539
4540 /* Verify that we don't need to save it: */
4541 assert(PL_curcop == &PL_compiling);
4542 StructCopy(&PL_compiling, &not_compiling, COP);
4543 PL_curcop = &not_compiling;
4544 /* The above ensures that we run with all the correct hints of the
4545 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4546 assert(IN_PERL_RUNTIME);
4547 PL_warnhook = PERL_WARNHOOK_FATAL;
4548 PL_diehook = NULL;
4549 JMPENV_PUSH(ret);
4550
4551 /* Effective $^W=1. */
4552 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4553 PL_dowarn |= G_WARN_ON;
4554
4555 switch (ret) {
4556 case 0:
4557 CALLRUNOPS(aTHX);
4558 sv = *(PL_stack_sp--);
4559 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4560 pad_swipe(o->op_targ, FALSE);
4561 }
4562 else if (SvTEMP(sv)) { /* grab mortal temp? */
4563 SvREFCNT_inc_simple_void(sv);
4564 SvTEMP_off(sv);
4565 }
4566 else { assert(SvIMMORTAL(sv)); }
4567 break;
4568 case 3:
4569 /* Something tried to die. Abandon constant folding. */
4570 /* Pretend the error never happened. */
4571 CLEAR_ERRSV();
4572 o->op_next = old_next;
4573 break;
4574 default:
4575 JMPENV_POP;
4576 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4577 PL_warnhook = oldwarnhook;
4578 PL_diehook = olddiehook;
4579 /* XXX note that this croak may fail as we've already blown away
4580 * the stack - eg any nested evals */
4581 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4582 }
4583 JMPENV_POP;
4584 PL_dowarn = oldwarn;
4585 PL_warnhook = oldwarnhook;
4586 PL_diehook = olddiehook;
4587 PL_curcop = &PL_compiling;
4588
4589 /* if we croaked, depending on how we croaked the eval scope
4590 * may or may not have already been popped */
4591 if (cxstack_ix > old_cxix) {
4592 assert(cxstack_ix == old_cxix + 1);
4593 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
4594 delete_eval_scope();
4595 }
4596 if (ret)
4597 goto nope;
4598
4599 /* OP_STRINGIFY and constant folding are used to implement qq.
4600 Here the constant folding is an implementation detail that we
4601 want to hide. If the stringify op is itself already marked
4602 folded, however, then it is actually a folded join. */
4603 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4604 op_free(o);
4605 assert(sv);
4606 if (is_stringify)
4607 SvPADTMP_off(sv);
4608 else if (!SvIMMORTAL(sv)) {
4609 SvPADTMP_on(sv);
4610 SvREADONLY_on(sv);
4611 }
4612 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4613 if (!is_stringify) newop->op_folded = 1;
4614 return newop;
4615
4616 nope:
4617 return o;
4618}
4619
4620static OP *
4621S_gen_constant_list(pTHX_ OP *o)
4622{
4623 dVAR;
4624 OP *curop, *old_next;
4625 SV * const oldwarnhook = PL_warnhook;
4626 SV * const olddiehook = PL_diehook;
4627 COP *old_curcop;
4628 U8 oldwarn = PL_dowarn;
4629 SV **svp;
4630 AV *av;
4631 I32 old_cxix;
4632 COP not_compiling;
4633 int ret = 0;
4634 dJMPENV;
4635 bool op_was_null;
4636
4637 list(o);
4638 if (PL_parser && PL_parser->error_count)
4639 return o; /* Don't attempt to run with errors */
4640
4641 curop = LINKLIST(o);
4642 old_next = o->op_next;
4643 o->op_next = 0;
4644 op_was_null = o->op_type == OP_NULL;
4645 if (op_was_null)
4646 o->op_type = OP_CUSTOM;
4647 CALL_PEEP(curop);
4648 if (op_was_null)
4649 o->op_type = OP_NULL;
4650 S_prune_chain_head(&curop);
4651 PL_op = curop;
4652
4653 old_cxix = cxstack_ix;
4654 create_eval_scope(NULL, G_FAKINGEVAL);
4655
4656 old_curcop = PL_curcop;
4657 StructCopy(old_curcop, &not_compiling, COP);
4658 PL_curcop = &not_compiling;
4659 /* The above ensures that we run with all the correct hints of the
4660 current COP, but that IN_PERL_RUNTIME is true. */
4661 assert(IN_PERL_RUNTIME);
4662 PL_warnhook = PERL_WARNHOOK_FATAL;
4663 PL_diehook = NULL;
4664 JMPENV_PUSH(ret);
4665
4666 /* Effective $^W=1. */
4667 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4668 PL_dowarn |= G_WARN_ON;
4669
4670 switch (ret) {
4671 case 0:
4672 Perl_pp_pushmark(aTHX);
4673 CALLRUNOPS(aTHX);
4674 PL_op = curop;
4675 assert (!(curop->op_flags & OPf_SPECIAL));
4676 assert(curop->op_type == OP_RANGE);
4677 Perl_pp_anonlist(aTHX);
4678 break;
4679 case 3:
4680 CLEAR_ERRSV();
4681 o->op_next = old_next;
4682 break;
4683 default:
4684 JMPENV_POP;
4685 PL_warnhook = oldwarnhook;
4686 PL_diehook = olddiehook;
4687 Perl_croak(aTHX_ "panic: gen_constant_list JMPENV_PUSH returned %d",
4688 ret);
4689 }
4690
4691 JMPENV_POP;
4692 PL_dowarn = oldwarn;
4693 PL_warnhook = oldwarnhook;
4694 PL_diehook = olddiehook;
4695 PL_curcop = old_curcop;
4696
4697 if (cxstack_ix > old_cxix) {
4698 assert(cxstack_ix == old_cxix + 1);
4699 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
4700 delete_eval_scope();
4701 }
4702 if (ret)
4703 return o;
4704
4705 OpTYPE_set(o, OP_RV2AV);
4706 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4707 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4708 o->op_opt = 0; /* needs to be revisited in rpeep() */
4709 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4710
4711 /* replace subtree with an OP_CONST */
4712 curop = ((UNOP*)o)->op_first;
4713 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4714 op_free(curop);
4715
4716 if (AvFILLp(av) != -1)
4717 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4718 {
4719 SvPADTMP_on(*svp);
4720 SvREADONLY_on(*svp);
4721 }
4722 LINKLIST(o);
4723 return list(o);
4724}
4725
4726/*
4727=head1 Optree Manipulation Functions
4728*/
4729
4730/* List constructors */
4731
4732/*
4733=for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4734
4735Append an item to the list of ops contained directly within a list-type
4736op, returning the lengthened list. C<first> is the list-type op,
4737and C<last> is the op to append to the list. C<optype> specifies the
4738intended opcode for the list. If C<first> is not already a list of the
4739right type, it will be upgraded into one. If either C<first> or C<last>
4740is null, the other is returned unchanged.
4741
4742=cut
4743*/
4744
4745OP *
4746Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4747{
4748 if (!first)
4749 return last;
4750
4751 if (!last)
4752 return first;
4753
4754 if (first->op_type != (unsigned)type
4755 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4756 {
4757 return newLISTOP(type, 0, first, last);
4758 }
4759
4760 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4761 first->op_flags |= OPf_KIDS;
4762 return first;
4763}
4764
4765/*
4766=for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4767
4768Concatenate the lists of ops contained directly within two list-type ops,
4769returning the combined list. C<first> and C<last> are the list-type ops
4770to concatenate. C<optype> specifies the intended opcode for the list.
4771If either C<first> or C<last> is not already a list of the right type,
4772it will be upgraded into one. If either C<first> or C<last> is null,
4773the other is returned unchanged.
4774
4775=cut
4776*/
4777
4778OP *
4779Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4780{
4781 if (!first)
4782 return last;
4783
4784 if (!last)
4785 return first;
4786
4787 if (first->op_type != (unsigned)type)
4788 return op_prepend_elem(type, first, last);
4789
4790 if (last->op_type != (unsigned)type)
4791 return op_append_elem(type, first, last);
4792
4793 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4794 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4795 OpLASTSIB_set(((LISTOP*)first)->op_last, first);
4796 first->op_flags |= (last->op_flags & OPf_KIDS);
4797
4798 S_op_destroy(aTHX_ last);
4799
4800 return first;
4801}
4802
4803/*
4804=for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4805
4806Prepend an item to the list of ops contained directly within a list-type
4807op, returning the lengthened list. C<first> is the op to prepend to the
4808list, and C<last> is the list-type op. C<optype> specifies the intended
4809opcode for the list. If C<last> is not already a list of the right type,
4810it will be upgraded into one. If either C<first> or C<last> is null,
4811the other is returned unchanged.
4812
4813=cut
4814*/
4815
4816OP *
4817Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4818{
4819 if (!first)
4820 return last;
4821
4822 if (!last)
4823 return first;
4824
4825 if (last->op_type == (unsigned)type) {
4826 if (type == OP_LIST) { /* already a PUSHMARK there */
4827 /* insert 'first' after pushmark */
4828 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4829 if (!(first->op_flags & OPf_PARENS))
4830 last->op_flags &= ~OPf_PARENS;
4831 }
4832 else
4833 op_sibling_splice(last, NULL, 0, first);
4834 last->op_flags |= OPf_KIDS;
4835 return last;
4836 }
4837
4838 return newLISTOP(type, 0, first, last);
4839}
4840
4841/*
4842=for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4843
4844Converts C<o> into a list op if it is not one already, and then converts it
4845into the specified C<type>, calling its check function, allocating a target if
4846it needs one, and folding constants.
4847
4848A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4849C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4850C<op_convert_list> to make it the right type.
4851
4852=cut
4853*/
4854
4855OP *
4856Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4857{
4858 dVAR;
4859 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4860 if (!o || o->op_type != OP_LIST)
4861 o = force_list(o, 0);
4862 else
4863 {
4864 o->op_flags &= ~OPf_WANT;
4865 o->op_private &= ~OPpLVAL_INTRO;
4866 }
4867
4868 if (!(PL_opargs[type] & OA_MARK))
4869 op_null(cLISTOPo->op_first);
4870 else {
4871 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
4872 if (kid2 && kid2->op_type == OP_COREARGS) {
4873 op_null(cLISTOPo->op_first);
4874 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4875 }
4876 }
4877
4878 if (type != OP_SPLIT)
4879 /* At this point o is a LISTOP, but OP_SPLIT is a PMOP; let
4880 * ck_split() create a real PMOP and leave the op's type as listop
4881 * for now. Otherwise op_free() etc will crash.
4882 */
4883 OpTYPE_set(o, type);
4884
4885 o->op_flags |= flags;
4886 if (flags & OPf_FOLDED)
4887 o->op_folded = 1;
4888
4889 o = CHECKOP(type, o);
4890 if (o->op_type != (unsigned)type)
4891 return o;
4892
4893 return fold_constants(op_integerize(op_std_init(o)));
4894}
4895
4896/* Constructors */
4897
4898
4899/*
4900=head1 Optree construction
4901
4902=for apidoc Am|OP *|newNULLLIST
4903
4904Constructs, checks, and returns a new C<stub> op, which represents an
4905empty list expression.
4906
4907=cut
4908*/
4909
4910OP *
4911Perl_newNULLLIST(pTHX)
4912{
4913 return newOP(OP_STUB, 0);
4914}
4915
4916/* promote o and any siblings to be a list if its not already; i.e.
4917 *
4918 * o - A - B
4919 *
4920 * becomes
4921 *
4922 * list
4923 * |
4924 * pushmark - o - A - B
4925 *
4926 * If nullit it true, the list op is nulled.
4927 */
4928
4929static OP *
4930S_force_list(pTHX_ OP *o, bool nullit)
4931{
4932 if (!o || o->op_type != OP_LIST) {
4933 OP *rest = NULL;
4934 if (o) {
4935 /* manually detach any siblings then add them back later */
4936 rest = OpSIBLING(o);
4937 OpLASTSIB_set(o, NULL);
4938 }
4939 o = newLISTOP(OP_LIST, 0, o, NULL);
4940 if (rest)
4941 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4942 }
4943 if (nullit)
4944 op_null(o);
4945 return o;
4946}
4947
4948/*
4949=for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4950
4951Constructs, checks, and returns an op of any list type. C<type> is
4952the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4953C<OPf_KIDS> will be set automatically if required. C<first> and C<last>
4954supply up to two ops to be direct children of the list op; they are
4955consumed by this function and become part of the constructed op tree.
4956
4957For most list operators, the check function expects all the kid ops to be
4958present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
4959appropriate. What you want to do in that case is create an op of type
4960C<OP_LIST>, append more children to it, and then call L</op_convert_list>.
4961See L</op_convert_list> for more information.
4962
4963
4964=cut
4965*/
4966
4967OP *
4968Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4969{
4970 dVAR;
4971 LISTOP *listop;
4972
4973 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
4974 || type == OP_CUSTOM);
4975
4976 NewOp(1101, listop, 1, LISTOP);
4977
4978 OpTYPE_set(listop, type);
4979 if (first || last)
4980 flags |= OPf_KIDS;
4981 listop->op_flags = (U8)flags;
4982
4983 if (!last && first)
4984 last = first;
4985 else if (!first && last)
4986 first = last;
4987 else if (first)
4988 OpMORESIB_set(first, last);
4989 listop->op_first = first;
4990 listop->op_last = last;
4991 if (type == OP_LIST) {
4992 OP* const pushop = newOP(OP_PUSHMARK, 0);
4993 OpMORESIB_set(pushop, first);
4994 listop->op_first = pushop;
4995 listop->op_flags |= OPf_KIDS;
4996 if (!last)
4997 listop->op_last = pushop;
4998 }
4999 if (listop->op_last)
5000 OpLASTSIB_set(listop->op_last, (OP*)listop);
5001
5002 return CHECKOP(type, listop);
5003}
5004
5005/*
5006=for apidoc Am|OP *|newOP|I32 type|I32 flags
5007
5008Constructs, checks, and returns an op of any base type (any type that
5009has no extra fields). C<type> is the opcode. C<flags> gives the
5010eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
5011of C<op_private>.
5012
5013=cut
5014*/
5015
5016OP *
5017Perl_newOP(pTHX_ I32 type, I32 flags)
5018{
5019 dVAR;
5020 OP *o;
5021
5022 if (type == -OP_ENTEREVAL) {
5023 type = OP_ENTEREVAL;
5024 flags |= OPpEVAL_BYTES<<8;
5025 }
5026
5027 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
5028 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
5029 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
5030 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
5031
5032 NewOp(1101, o, 1, OP);
5033 OpTYPE_set(o, type);
5034 o->op_flags = (U8)flags;
5035
5036 o->op_next = o;
5037 o->op_private = (U8)(0 | (flags >> 8));
5038 if (PL_opargs[type] & OA_RETSCALAR)
5039 scalar(o);
5040 if (PL_opargs[type] & OA_TARGET)
5041 o->op_targ = pad_alloc(type, SVs_PADTMP);
5042 return CHECKOP(type, o);
5043}
5044
5045/*
5046=for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
5047
5048Constructs, checks, and returns an op of any unary type. C<type> is
5049the opcode. C<flags> gives the eight bits of C<op_flags>, except that
5050C<OPf_KIDS> will be set automatically if required, and, shifted up eight
5051bits, the eight bits of C<op_private>, except that the bit with value 1
5052is automatically set. C<first> supplies an optional op to be the direct
5053child of the unary op; it is consumed by this function and become part
5054of the constructed op tree.
5055
5056=cut
5057*/
5058
5059OP *
5060Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
5061{
5062 dVAR;
5063 UNOP *unop;
5064
5065 if (type == -OP_ENTEREVAL) {
5066 type = OP_ENTEREVAL;
5067 flags |= OPpEVAL_BYTES<<8;
5068 }
5069
5070 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
5071 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
5072 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
5073 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
5074 || type == OP_SASSIGN
5075 || type == OP_ENTERTRY
5076 || type == OP_CUSTOM
5077 || type == OP_NULL );
5078
5079 if (!first)
5080 first = newOP(OP_STUB, 0);
5081 if (PL_opargs[type] & OA_MARK)
5082 first = force_list(first, 1);
5083
5084 NewOp(1101, unop, 1, UNOP);
5085 OpTYPE_set(unop, type);
5086 unop->op_first = first;
5087 unop->op_flags = (U8)(flags | OPf_KIDS);
5088 unop->op_private = (U8)(1 | (flags >> 8));
5089
5090 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
5091 OpLASTSIB_set(first, (OP*)unop);
5092
5093 unop = (UNOP*) CHECKOP(type, unop);
5094 if (unop->op_next)
5095 return (OP*)unop;
5096
5097 return fold_constants(op_integerize(op_std_init((OP *) unop)));
5098}
5099
5100/*
5101=for apidoc newUNOP_AUX
5102
5103Similar to C<newUNOP>, but creates an C<UNOP_AUX> struct instead, with C<op_aux>
5104initialised to C<aux>
5105
5106=cut
5107*/
5108
5109OP *
5110Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux)
5111{
5112 dVAR;
5113 UNOP_AUX *unop;
5114
5115 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX
5116 || type == OP_CUSTOM);
5117
5118 NewOp(1101, unop, 1, UNOP_AUX);
5119 unop->op_type = (OPCODE)type;
5120 unop->op_ppaddr = PL_ppaddr[type];
5121 unop->op_first = first;
5122 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0));
5123 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8));
5124 unop->op_aux = aux;
5125
5126 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */
5127 OpLASTSIB_set(first, (OP*)unop);
5128
5129 unop = (UNOP_AUX*) CHECKOP(type, unop);
5130
5131 return op_std_init((OP *) unop);
5132}
5133
5134/*
5135=for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first
5136
5137Constructs, checks, and returns an op of method type with a method name
5138evaluated at runtime. C<type> is the opcode. C<flags> gives the eight
5139bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically,
5140and, shifted up eight bits, the eight bits of C<op_private>, except that
5141the bit with value 1 is automatically set. C<dynamic_meth> supplies an
5142op which evaluates method name; it is consumed by this function and
5143become part of the constructed op tree.
5144Supported optypes: C<OP_METHOD>.
5145
5146=cut
5147*/
5148
5149static OP*
5150S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) {
5151 dVAR;
5152 METHOP *methop;
5153
5154 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP
5155 || type == OP_CUSTOM);
5156
5157 NewOp(1101, methop, 1, METHOP);
5158 if (dynamic_meth) {
5159 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1);
5160 methop->op_flags = (U8)(flags | OPf_KIDS);
5161 methop->op_u.op_first = dynamic_meth;
5162 methop->op_private = (U8)(1 | (flags >> 8));
5163
5164 if (!OpHAS_SIBLING(dynamic_meth))
5165 OpLASTSIB_set(dynamic_meth, (OP*)methop);
5166 }
5167 else {
5168 assert(const_meth);
5169 methop->op_flags = (U8)(flags & ~OPf_KIDS);
5170 methop->op_u.op_meth_sv = const_meth;
5171 methop->op_private = (U8)(0 | (flags >> 8));
5172 methop->op_next = (OP*)methop;
5173 }
5174
5175#ifdef USE_ITHREADS
5176 methop->op_rclass_targ = 0;
5177#else
5178 methop->op_rclass_sv = NULL;
5179#endif
5180
5181 OpTYPE_set(methop, type);
5182 return CHECKOP(type, methop);
5183}
5184
5185OP *
5186Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) {
5187 PERL_ARGS_ASSERT_NEWMETHOP;
5188 return newMETHOP_internal(type, flags, dynamic_meth, NULL);
5189}
5190
5191/*
5192=for apidoc Am|OP *|newMETHOP_named|I32 type|I32 flags|SV *const_meth
5193
5194Constructs, checks, and returns an op of method type with a constant
5195method name. C<type> is the opcode. C<flags> gives the eight bits of
5196C<op_flags>, and, shifted up eight bits, the eight bits of
5197C<op_private>. C<const_meth> supplies a constant method name;
5198it must be a shared COW string.
5199Supported optypes: C<OP_METHOD_NAMED>.
5200
5201=cut
5202*/
5203
5204OP *
5205Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) {
5206 PERL_ARGS_ASSERT_NEWMETHOP_NAMED;
5207 return newMETHOP_internal(type, flags, NULL, const_meth);
5208}
5209
5210/*
5211=for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
5212
5213Constructs, checks, and returns an op of any binary type. C<type>
5214is the opcode. C<flags> gives the eight bits of C<op_flags>, except
5215that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
5216the eight bits of C<op_private>, except that the bit with value 1 or
52172 is automatically set as required. C<first> and C<last> supply up to
5218two ops to be the direct children of the binary op; they are consumed
5219by this function and become part of the constructed op tree.
5220
5221=cut
5222*/
5223
5224OP *
5225Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
5226{
5227 dVAR;
5228 BINOP *binop;
5229
5230 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
5231 || type == OP_NULL || type == OP_CUSTOM);
5232
5233 NewOp(1101, binop, 1, BINOP);
5234
5235 if (!first)
5236 first = newOP(OP_NULL, 0);
5237
5238 OpTYPE_set(binop, type);
5239 binop->op_first = first;
5240 binop->op_flags = (U8)(flags | OPf_KIDS);
5241 if (!last) {
5242 last = first;
5243 binop->op_private = (U8)(1 | (flags >> 8));
5244 }
5245 else {
5246 binop->op_private = (U8)(2 | (flags >> 8));
5247 OpMORESIB_set(first, last);
5248 }
5249
5250 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */
5251 OpLASTSIB_set(last, (OP*)binop);
5252
5253 binop->op_last = OpSIBLING(binop->op_first);
5254 if (binop->op_last)
5255 OpLASTSIB_set(binop->op_last, (OP*)binop);
5256
5257 binop = (BINOP*)CHECKOP(type, binop);
5258 if (binop->op_next || binop->op_type != (OPCODE)type)
5259 return (OP*)binop;
5260
5261 return fold_constants(op_integerize(op_std_init((OP *)binop)));
5262}
5263
5264static int uvcompare(const void *a, const void *b)
5265 __attribute__nonnull__(1)
5266 __attribute__nonnull__(2)
5267 __attribute__pure__;
5268static int uvcompare(const void *a, const void *b)
5269{
5270 if (*((const UV *)a) < (*(const UV *)b))
5271 return -1;
5272 if (*((const UV *)a) > (*(const UV *)b))
5273 return 1;
5274 if (*((const UV *)a+1) < (*(const UV *)b+1))
5275 return -1;
5276 if (*((const UV *)a+1) > (*(const UV *)b+1))
5277 return 1;
5278 return 0;
5279}
5280
5281static OP *
5282S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
5283{
5284 SV * const tstr = ((SVOP*)expr)->op_sv;
5285 SV * const rstr =
5286 ((SVOP*)repl)->op_sv;
5287 STRLEN tlen;
5288 STRLEN rlen;
5289 const U8 *t = (U8*)SvPV_const(tstr, tlen);
5290 const U8 *r = (U8*)SvPV_const(rstr, rlen);
5291 I32 i;
5292 I32 j;
5293 I32 grows = 0;
5294 short *tbl;
5295
5296 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
5297 const I32 squash = o->op_private & OPpTRANS_SQUASH;
5298 I32 del = o->op_private & OPpTRANS_DELETE;
5299 SV* swash;
5300
5301 PERL_ARGS_ASSERT_PMTRANS;
5302
5303 PL_hints |= HINT_BLOCK_SCOPE;
5304
5305 if (SvUTF8(tstr))
5306 o->op_private |= OPpTRANS_FROM_UTF;
5307
5308 if (SvUTF8(rstr))
5309 o->op_private |= OPpTRANS_TO_UTF;
5310
5311 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
5312 SV* const listsv = newSVpvs("# comment\n");
5313 SV* transv = NULL;
5314 const U8* tend = t + tlen;
5315 const U8* rend = r + rlen;
5316 STRLEN ulen;
5317 UV tfirst = 1;
5318 UV tlast = 0;
5319 IV tdiff;
5320 STRLEN tcount = 0;
5321 UV rfirst = 1;
5322 UV rlast = 0;
5323 IV rdiff;
5324 STRLEN rcount = 0;
5325 IV diff;
5326 I32 none = 0;
5327 U32 max = 0;
5328 I32 bits;
5329 I32 havefinal = 0;
5330 U32 final = 0;
5331 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
5332 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
5333 U8* tsave = NULL;
5334 U8* rsave = NULL;
5335 const U32 flags = UTF8_ALLOW_DEFAULT;
5336
5337 if (!from_utf) {
5338 STRLEN len = tlen;
5339 t = tsave = bytes_to_utf8(t, &len);
5340 tend = t + len;
5341 }
5342 if (!to_utf && rlen) {
5343 STRLEN len = rlen;
5344 r = rsave = bytes_to_utf8(r, &len);
5345 rend = r + len;
5346 }
5347
5348/* There is a snag with this code on EBCDIC: scan_const() in toke.c has
5349 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
5350 * odd. */
5351
5352 if (complement) {
5353 U8 tmpbuf[UTF8_MAXBYTES+1];
5354 UV *cp;
5355 UV nextmin = 0;
5356 Newx(cp, 2*tlen, UV);
5357 i = 0;
5358 transv = newSVpvs("");
5359 while (t < tend) {
5360 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
5361 t += ulen;
5362 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
5363 t++;
5364 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
5365 t += ulen;
5366 }
5367 else {
5368 cp[2*i+1] = cp[2*i];
5369 }
5370 i++;
5371 }
5372 qsort(cp, i, 2*sizeof(UV), uvcompare);
5373 for (j = 0; j < i; j++) {
5374 UV val = cp[2*j];
5375 diff = val - nextmin;
5376 if (diff > 0) {
5377 t = uvchr_to_utf8(tmpbuf,nextmin);
5378 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5379 if (diff > 1) {
5380 U8 range_mark = ILLEGAL_UTF8_BYTE;
5381 t = uvchr_to_utf8(tmpbuf, val - 1);
5382 sv_catpvn(transv, (char *)&range_mark, 1);
5383 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5384 }
5385 }
5386 val = cp[2*j+1];
5387 if (val >= nextmin)
5388 nextmin = val + 1;
5389 }
5390 t = uvchr_to_utf8(tmpbuf,nextmin);
5391 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5392 {
5393 U8 range_mark = ILLEGAL_UTF8_BYTE;
5394 sv_catpvn(transv, (char *)&range_mark, 1);
5395 }
5396 t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
5397 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5398 t = (const U8*)SvPVX_const(transv);
5399 tlen = SvCUR(transv);
5400 tend = t + tlen;
5401 Safefree(cp);
5402 }
5403 else if (!rlen && !del) {
5404 r = t; rlen = tlen; rend = tend;
5405 }
5406 if (!squash) {
5407 if ((!rlen && !del) || t == r ||
5408 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
5409 {
5410 o->op_private |= OPpTRANS_IDENTICAL;
5411 }
5412 }
5413
5414 while (t < tend || tfirst <= tlast) {
5415 /* see if we need more "t" chars */
5416 if (tfirst > tlast) {
5417 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
5418 t += ulen;
5419 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
5420 t++;
5421 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
5422 t += ulen;
5423 }
5424 else
5425 tlast = tfirst;
5426 }
5427
5428 /* now see if we need more "r" chars */
5429 if (rfirst > rlast) {
5430 if (r < rend) {
5431 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
5432 r += ulen;
5433 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
5434 r++;
5435 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
5436 r += ulen;
5437 }
5438 else
5439 rlast = rfirst;
5440 }
5441 else {
5442 if (!havefinal++)
5443 final = rlast;
5444 rfirst = rlast = 0xffffffff;
5445 }
5446 }
5447
5448 /* now see which range will peter out first, if either. */
5449 tdiff = tlast - tfirst;
5450 rdiff = rlast - rfirst;
5451 tcount += tdiff + 1;
5452 rcount += rdiff + 1;
5453
5454 if (tdiff <= rdiff)
5455 diff = tdiff;
5456 else
5457 diff = rdiff;
5458
5459 if (rfirst == 0xffffffff) {
5460 diff = tdiff; /* oops, pretend rdiff is infinite */
5461 if (diff > 0)
5462 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
5463 (long)tfirst, (long)tlast);
5464 else
5465 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
5466 }
5467 else {
5468 if (diff > 0)
5469 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
5470 (long)tfirst, (long)(tfirst + diff),
5471 (long)rfirst);
5472 else
5473 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
5474 (long)tfirst, (long)rfirst);
5475
5476 if (rfirst + diff > max)
5477 max = rfirst + diff;
5478 if (!grows)
5479 grows = (tfirst < rfirst &&
5480 UVCHR_SKIP(tfirst) < UVCHR_SKIP(rfirst + diff));
5481 rfirst += diff + 1;
5482 }
5483 tfirst += diff + 1;
5484 }
5485
5486 none = ++max;
5487 if (del)
5488 del = ++max;
5489
5490 if (max > 0xffff)
5491 bits = 32;
5492 else if (max > 0xff)
5493 bits = 16;
5494 else
5495 bits = 8;
5496
5497 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
5498#ifdef USE_ITHREADS
5499 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
5500 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
5501 PAD_SETSV(cPADOPo->op_padix, swash);
5502 SvPADTMP_on(swash);
5503 SvREADONLY_on(swash);
5504#else
5505 cSVOPo->op_sv = swash;
5506#endif
5507 SvREFCNT_dec(listsv);
5508 SvREFCNT_dec(transv);
5509
5510 if (!del && havefinal && rlen)
5511 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
5512 newSVuv((UV)final), 0);
5513
5514 Safefree(tsave);
5515 Safefree(rsave);
5516
5517 tlen = tcount;
5518 rlen = rcount;
5519 if (r < rend)
5520 rlen++;
5521 else if (rlast == 0xffffffff)
5522 rlen = 0;
5523
5524 goto warnins;
5525 }
5526
5527 tbl = (short*)PerlMemShared_calloc(
5528 (o->op_private & OPpTRANS_COMPLEMENT) &&
5529 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
5530 sizeof(short));
5531 cPVOPo->op_pv = (char*)tbl;
5532 if (complement) {
5533 for (i = 0; i < (I32)tlen; i++)
5534 tbl[t[i]] = -1;
5535 for (i = 0, j = 0; i < 256; i++) {
5536 if (!tbl[i]) {
5537 if (j >= (I32)rlen) {
5538 if (del)
5539 tbl[i] = -2;
5540 else if (rlen)
5541 tbl[i] = r[j-1];
5542 else
5543 tbl[i] = (short)i;
5544 }
5545 else {
5546 if (UVCHR_IS_INVARIANT(i) && ! UVCHR_IS_INVARIANT(r[j]))
5547 grows = 1;
5548 tbl[i] = r[j++];
5549 }
5550 }
5551 }
5552 if (!del) {
5553 if (!rlen) {
5554 j = rlen;
5555 if (!squash)
5556 o->op_private |= OPpTRANS_IDENTICAL;
5557 }
5558 else if (j >= (I32)rlen)
5559 j = rlen - 1;
5560 else {
5561 tbl =
5562 (short *)
5563 PerlMemShared_realloc(tbl,
5564 (0x101+rlen-j) * sizeof(short));
5565 cPVOPo->op_pv = (char*)tbl;
5566 }
5567 tbl[0x100] = (short)(rlen - j);
5568 for (i=0; i < (I32)rlen - j; i++)
5569 tbl[0x101+i] = r[j+i];
5570 }
5571 }
5572 else {
5573 if (!rlen && !del) {
5574 r = t; rlen = tlen;
5575 if (!squash)
5576 o->op_private |= OPpTRANS_IDENTICAL;
5577 }
5578 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
5579 o->op_private |= OPpTRANS_IDENTICAL;
5580 }
5581 for (i = 0; i < 256; i++)
5582 tbl[i] = -1;
5583 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
5584 if (j >= (I32)rlen) {
5585 if (del) {
5586 if (tbl[t[i]] == -1)
5587 tbl[t[i]] = -2;
5588 continue;
5589 }
5590 --j;
5591 }
5592 if (tbl[t[i]] == -1) {
5593 if ( UVCHR_IS_INVARIANT(t[i])
5594 && ! UVCHR_IS_INVARIANT(r[j]))
5595 grows = 1;
5596 tbl[t[i]] = r[j];
5597 }
5598 }
5599 }
5600
5601 warnins:
5602 if(del && rlen == tlen) {
5603 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
5604 } else if(rlen > tlen && !complement) {
5605 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
5606 }
5607
5608 if (grows)
5609 o->op_private |= OPpTRANS_GROWS;
5610 op_free(expr);
5611 op_free(repl);
5612
5613 return o;
5614}
5615
5616/*
5617=for apidoc Am|OP *|newPMOP|I32 type|I32 flags
5618
5619Constructs, checks, and returns an op of any pattern matching type.
5620C<type> is the opcode. C<flags> gives the eight bits of C<op_flags>
5621and, shifted up eight bits, the eight bits of C<op_private>.
5622
5623=cut
5624*/
5625
5626OP *
5627Perl_newPMOP(pTHX_ I32 type, I32 flags)
5628{
5629 dVAR;
5630 PMOP *pmop;
5631
5632 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP
5633 || type == OP_CUSTOM);
5634
5635 NewOp(1101, pmop, 1, PMOP);
5636 OpTYPE_set(pmop, type);
5637 pmop->op_flags = (U8)flags;
5638 pmop->op_private = (U8)(0 | (flags >> 8));
5639 if (PL_opargs[type] & OA_RETSCALAR)
5640 scalar((OP *)pmop);
5641
5642 if (PL_hints & HINT_RE_TAINT)
5643 pmop->op_pmflags |= PMf_RETAINT;
5644#ifdef USE_LOCALE_CTYPE
5645 if (IN_LC_COMPILETIME(LC_CTYPE)) {
5646 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
5647 }
5648 else
5649#endif
5650 if (IN_UNI_8_BIT) {
5651 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
5652 }
5653 if (PL_hints & HINT_RE_FLAGS) {
5654 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
5655 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
5656 );
5657 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
5658 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
5659 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
5660 );
5661 if (reflags && SvOK(reflags)) {
5662 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
5663 }
5664 }
5665
5666
5667#ifdef USE_ITHREADS
5668 assert(SvPOK(PL_regex_pad[0]));
5669 if (SvCUR(PL_regex_pad[0])) {
5670 /* Pop off the "packed" IV from the end. */
5671 SV *const repointer_list = PL_regex_pad[0];
5672 const char *p = SvEND(repointer_list) - sizeof(IV);
5673 const IV offset = *((IV*)p);
5674
5675 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
5676
5677 SvEND_set(repointer_list, p);
5678
5679 pmop->op_pmoffset = offset;
5680 /* This slot should be free, so assert this: */
5681 assert(PL_regex_pad[offset] == &PL_sv_undef);
5682 } else {
5683 SV * const repointer = &PL_sv_undef;
5684 av_push(PL_regex_padav, repointer);
5685 pmop->op_pmoffset = av_tindex(PL_regex_padav);
5686 PL_regex_pad = AvARRAY(PL_regex_padav);
5687 }
5688#endif
5689
5690 return CHECKOP(type, pmop);
5691}
5692
5693static void
5694S_set_haseval(pTHX)
5695{
5696 PADOFFSET i = 1;
5697 PL_cv_has_eval = 1;
5698 /* Any pad names in scope are potentially lvalues. */
5699 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) {
5700 PADNAME *pn = PAD_COMPNAME_SV(i);
5701 if (!pn || !PadnameLEN(pn))
5702 continue;
5703 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax))
5704 S_mark_padname_lvalue(aTHX_ pn);
5705 }
5706}
5707
5708/* Given some sort of match op o, and an expression expr containing a
5709 * pattern, either compile expr into a regex and attach it to o (if it's
5710 * constant), or convert expr into a runtime regcomp op sequence (if it's
5711 * not)
5712 *
5713 * Flags currently has 2 bits of meaning:
5714 * 1: isreg indicates that the pattern is part of a regex construct, eg
5715 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
5716 * split "pattern", which aren't. In the former case, expr will be a list
5717 * if the pattern contains more than one term (eg /a$b/).
5718 * 2: The pattern is for a split.
5719 *
5720 * When the pattern has been compiled within a new anon CV (for
5721 * qr/(?{...})/ ), then floor indicates the savestack level just before
5722 * the new sub was created
5723 */
5724
5725OP *
5726Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, UV flags, I32 floor)
5727{
5728 PMOP *pm;
5729 LOGOP *rcop;
5730 I32 repl_has_vars = 0;
5731 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
5732 bool is_compiletime;
5733 bool has_code;
5734 bool isreg = cBOOL(flags & 1);
5735 bool is_split = cBOOL(flags & 2);
5736
5737 PERL_ARGS_ASSERT_PMRUNTIME;
5738
5739 if (is_trans) {
5740 return pmtrans(o, expr, repl);
5741 }
5742
5743 /* find whether we have any runtime or code elements;
5744 * at the same time, temporarily set the op_next of each DO block;
5745 * then when we LINKLIST, this will cause the DO blocks to be excluded
5746 * from the op_next chain (and from having LINKLIST recursively
5747 * applied to them). We fix up the DOs specially later */
5748
5749 is_compiletime = 1;
5750 has_code = 0;
5751 if (expr->op_type == OP_LIST) {
5752 OP *o;
5753 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) {
5754 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
5755 has_code = 1;
5756 assert(!o->op_next);
5757 if (UNLIKELY(!OpHAS_SIBLING(o))) {
5758 assert(PL_parser && PL_parser->error_count);
5759 /* This can happen with qr/ (?{(^{})/. Just fake up
5760 the op we were expecting to see, to avoid crashing
5761 elsewhere. */
5762 op_sibling_splice(expr, o, 0,
5763 newSVOP(OP_CONST, 0, &PL_sv_no));
5764 }
5765 o->op_next = OpSIBLING(o);
5766 }
5767 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
5768 is_compiletime = 0;
5769 }
5770 }
5771 else if (expr->op_type != OP_CONST)
5772 is_compiletime = 0;
5773
5774 LINKLIST(expr);
5775
5776 /* fix up DO blocks; treat each one as a separate little sub;
5777 * also, mark any arrays as LIST/REF */
5778
5779 if (expr->op_type == OP_LIST) {
5780 OP *o;
5781 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) {
5782
5783 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) {
5784 assert( !(o->op_flags & OPf_WANT));
5785 /* push the array rather than its contents. The regex
5786 * engine will retrieve and join the elements later */
5787 o->op_flags |= (OPf_WANT_LIST | OPf_REF);
5788 continue;
5789 }
5790
5791 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
5792 continue;
5793 o->op_next = NULL; /* undo temporary hack from above */
5794 scalar(o);
5795 LINKLIST(o);
5796 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
5797 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first);
5798 /* skip ENTER */
5799 assert(leaveop->op_first->op_type == OP_ENTER);
5800 assert(OpHAS_SIBLING(leaveop->op_first));
5801 o->op_next = OpSIBLING(leaveop->op_first);
5802 /* skip leave */
5803 assert(leaveop->op_flags & OPf_KIDS);
5804 assert(leaveop->op_last->op_next == (OP*)leaveop);
5805 leaveop->op_next = NULL; /* stop on last op */
5806 op_null((OP*)leaveop);
5807 }
5808 else {
5809 /* skip SCOPE */
5810 OP *scope = cLISTOPo->op_first;
5811 assert(scope->op_type == OP_SCOPE);
5812 assert(scope->op_flags & OPf_KIDS);
5813 scope->op_next = NULL; /* stop on last op */
5814 op_null(scope);
5815 }
5816 /* have to peep the DOs individually as we've removed it from
5817 * the op_next chain */
5818 CALL_PEEP(o);
5819 S_prune_chain_head(&(o->op_next));
5820 if (is_compiletime)
5821 /* runtime finalizes as part of finalizing whole tree */
5822 finalize_optree(o);
5823 }
5824 }
5825 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
5826 assert( !(expr->op_flags & OPf_WANT));
5827 /* push the array rather than its contents. The regex
5828 * engine will retrieve and join the elements later */
5829 expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
5830 }
5831
5832 PL_hints |= HINT_BLOCK_SCOPE;
5833 pm = (PMOP*)o;
5834 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
5835
5836 if (is_compiletime) {
5837 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
5838 regexp_engine const *eng = current_re_engine();
5839
5840 if (is_split) {
5841 /* make engine handle split ' ' specially */
5842 pm->op_pmflags |= PMf_SPLIT;
5843 rx_flags |= RXf_SPLIT;
5844 }
5845
5846 if (!has_code || !eng->op_comp) {
5847 /* compile-time simple constant pattern */
5848
5849 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
5850 /* whoops! we guessed that a qr// had a code block, but we
5851 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
5852 * that isn't required now. Note that we have to be pretty
5853 * confident that nothing used that CV's pad while the
5854 * regex was parsed, except maybe op targets for \Q etc.
5855 * If there were any op targets, though, they should have
5856 * been stolen by constant folding.
5857 */
5858#ifdef DEBUGGING
5859 SSize_t i = 0;
5860 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0);
5861 while (++i <= AvFILLp(PL_comppad)) {
5862# ifdef USE_PAD_RESET
5863 /* under USE_PAD_RESET, pad swipe replaces a swiped
5864 * folded constant with a fresh padtmp */
5865 assert(!PL_curpad[i] || SvPADTMP(PL_curpad[i]));
5866# else
5867 assert(!PL_curpad[i]);
5868# endif
5869 }
5870#endif
5871 /* But we know that one op is using this CV's slab. */
5872 cv_forget_slab(PL_compcv);
5873 LEAVE_SCOPE(floor);
5874 pm->op_pmflags &= ~PMf_HAS_CV;
5875 }
5876
5877 PM_SETRE(pm,
5878 eng->op_comp
5879 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5880 rx_flags, pm->op_pmflags)
5881 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5882 rx_flags, pm->op_pmflags)
5883 );
5884 op_free(expr);
5885 }
5886 else {
5887 /* compile-time pattern that includes literal code blocks */
5888 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5889 rx_flags,
5890 (pm->op_pmflags |
5891 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
5892 );
5893 PM_SETRE(pm, re);
5894 if (pm->op_pmflags & PMf_HAS_CV) {
5895 CV *cv;
5896 /* this QR op (and the anon sub we embed it in) is never
5897 * actually executed. It's just a placeholder where we can
5898 * squirrel away expr in op_code_list without the peephole
5899 * optimiser etc processing it for a second time */
5900 OP *qr = newPMOP(OP_QR, 0);
5901 ((PMOP*)qr)->op_code_list = expr;
5902
5903 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
5904 SvREFCNT_inc_simple_void(PL_compcv);
5905 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
5906 ReANY(re)->qr_anoncv = cv;
5907
5908 /* attach the anon CV to the pad so that
5909 * pad_fixup_inner_anons() can find it */
5910 (void)pad_add_anon(cv, o->op_type);
5911 SvREFCNT_inc_simple_void(cv);
5912 }
5913 else {
5914 pm->op_code_list = expr;
5915 }
5916 }
5917 }
5918 else {
5919 /* runtime pattern: build chain of regcomp etc ops */
5920 bool reglist;
5921 PADOFFSET cv_targ = 0;
5922
5923 reglist = isreg && expr->op_type == OP_LIST;
5924 if (reglist)
5925 op_null(expr);
5926
5927 if (has_code) {
5928 pm->op_code_list = expr;
5929 /* don't free op_code_list; its ops are embedded elsewhere too */
5930 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
5931 }
5932
5933 if (is_split)
5934 /* make engine handle split ' ' specially */
5935 pm->op_pmflags |= PMf_SPLIT;
5936
5937 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
5938 * to allow its op_next to be pointed past the regcomp and
5939 * preceding stacking ops;
5940 * OP_REGCRESET is there to reset taint before executing the
5941 * stacking ops */
5942 if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
5943 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
5944
5945 if (pm->op_pmflags & PMf_HAS_CV) {
5946 /* we have a runtime qr with literal code. This means
5947 * that the qr// has been wrapped in a new CV, which
5948 * means that runtime consts, vars etc will have been compiled
5949 * against a new pad. So... we need to execute those ops
5950 * within the environment of the new CV. So wrap them in a call
5951 * to a new anon sub. i.e. for
5952 *
5953 * qr/a$b(?{...})/,
5954 *
5955 * we build an anon sub that looks like
5956 *
5957 * sub { "a", $b, '(?{...})' }
5958 *
5959 * and call it, passing the returned list to regcomp.
5960 * Or to put it another way, the list of ops that get executed
5961 * are:
5962 *
5963 * normal PMf_HAS_CV
5964 * ------ -------------------
5965 * pushmark (for regcomp)
5966 * pushmark (for entersub)
5967 * anoncode
5968 * srefgen
5969 * entersub
5970 * regcreset regcreset
5971 * pushmark pushmark
5972 * const("a") const("a")
5973 * gvsv(b) gvsv(b)
5974 * const("(?{...})") const("(?{...})")
5975 * leavesub
5976 * regcomp regcomp
5977 */
5978
5979 SvREFCNT_inc_simple_void(PL_compcv);
5980 CvLVALUE_on(PL_compcv);
5981 /* these lines are just an unrolled newANONATTRSUB */
5982 expr = newSVOP(OP_ANONCODE, 0,
5983 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
5984 cv_targ = expr->op_targ;
5985 expr = newUNOP(OP_REFGEN, 0, expr);
5986
5987 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), 1));
5988 }
5989
5990 rcop = alloc_LOGOP(OP_REGCOMP, scalar(expr), o);
5991 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
5992 | (reglist ? OPf_STACKED : 0);
5993 rcop->op_targ = cv_targ;
5994
5995 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
5996 if (PL_hints & HINT_RE_EVAL)
5997 S_set_haseval(aTHX);
5998
5999 /* establish postfix order */
6000 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
6001 LINKLIST(expr);
6002 rcop->op_next = expr;
6003 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
6004 }
6005 else {
6006 rcop->op_next = LINKLIST(expr);
6007 expr->op_next = (OP*)rcop;
6008 }
6009
6010 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
6011 }
6012
6013 if (repl) {
6014 OP *curop = repl;
6015 bool konst;
6016 /* If we are looking at s//.../e with a single statement, get past
6017 the implicit do{}. */
6018 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
6019 && cUNOPx(curop)->op_first->op_type == OP_SCOPE
6020 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS)
6021 {
6022 OP *sib;
6023 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
6024 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid))
6025 && !OpHAS_SIBLING(sib))
6026 curop = sib;
6027 }
6028 if (curop->op_type == OP_CONST)
6029 konst = TRUE;
6030 else if (( (curop->op_type == OP_RV2SV ||
6031 curop->op_type == OP_RV2AV ||
6032 curop->op_type == OP_RV2HV ||
6033 curop->op_type == OP_RV2GV)
6034 && cUNOPx(curop)->op_first
6035 && cUNOPx(curop)->op_first->op_type == OP_GV )
6036 || curop->op_type == OP_PADSV
6037 || curop->op_type == OP_PADAV
6038 || curop->op_type == OP_PADHV
6039 || curop->op_type == OP_PADANY) {
6040 repl_has_vars = 1;
6041 konst = TRUE;
6042 }
6043 else konst = FALSE;
6044 if (konst
6045 && !(repl_has_vars
6046 && (!PM_GETRE(pm)
6047 || !RX_PRELEN(PM_GETRE(pm))
6048 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
6049 {
6050 pm->op_pmflags |= PMf_CONST; /* const for long enough */
6051 op_prepend_elem(o->op_type, scalar(repl), o);
6052 }
6053 else {
6054 rcop = alloc_LOGOP(OP_SUBSTCONT, scalar(repl), o);
6055 rcop->op_private = 1;
6056
6057 /* establish postfix order */
6058 rcop->op_next = LINKLIST(repl);
6059 repl->op_next = (OP*)rcop;
6060
6061 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
6062 assert(!(pm->op_pmflags & PMf_ONCE));
6063 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
6064 rcop->op_next = 0;
6065 }
6066 }
6067
6068 return (OP*)pm;
6069}
6070
6071/*
6072=for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
6073
6074Constructs, checks, and returns an op of any type that involves an
6075embedded SV. C<type> is the opcode. C<flags> gives the eight bits
6076of C<op_flags>. C<sv> gives the SV to embed in the op; this function
6077takes ownership of one reference to it.
6078
6079=cut
6080*/
6081
6082OP *
6083Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
6084{
6085 dVAR;
6086 SVOP *svop;
6087
6088 PERL_ARGS_ASSERT_NEWSVOP;
6089
6090 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
6091 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
6092 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
6093 || type == OP_CUSTOM);
6094
6095 NewOp(1101, svop, 1, SVOP);
6096 OpTYPE_set(svop, type);
6097 svop->op_sv = sv;
6098 svop->op_next = (OP*)svop;
6099 svop->op_flags = (U8)flags;
6100 svop->op_private = (U8)(0 | (flags >> 8));
6101 if (PL_opargs[type] & OA_RETSCALAR)
6102 scalar((OP*)svop);
6103 if (PL_opargs[type] & OA_TARGET)
6104 svop->op_targ = pad_alloc(type, SVs_PADTMP);
6105 return CHECKOP(type, svop);
6106}
6107
6108/*
6109=for apidoc Am|OP *|newDEFSVOP|
6110
6111Constructs and returns an op to access C<$_>.
6112
6113=cut
6114*/
6115
6116OP *
6117Perl_newDEFSVOP(pTHX)
6118{
6119 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
6120}
6121
6122#ifdef USE_ITHREADS
6123
6124/*
6125=for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
6126
6127Constructs, checks, and returns an op of any type that involves a
6128reference to a pad element. C<type> is the opcode. C<flags> gives the
6129eight bits of C<op_flags>. A pad slot is automatically allocated, and
6130is populated with C<sv>; this function takes ownership of one reference
6131to it.
6132
6133This function only exists if Perl has been compiled to use ithreads.
6134
6135=cut
6136*/
6137
6138OP *
6139Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
6140{
6141 dVAR;
6142 PADOP *padop;
6143
6144 PERL_ARGS_ASSERT_NEWPADOP;
6145
6146 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
6147 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
6148 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
6149 || type == OP_CUSTOM);
6150
6151 NewOp(1101, padop, 1, PADOP);
6152 OpTYPE_set(padop, type);
6153 padop->op_padix =
6154 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP);
6155 SvREFCNT_dec(PAD_SVl(padop->op_padix));
6156 PAD_SETSV(padop->op_padix, sv);
6157 assert(sv);
6158 padop->op_next = (OP*)padop;
6159 padop->op_flags = (U8)flags;
6160 if (PL_opargs[type] & OA_RETSCALAR)
6161 scalar((OP*)padop);
6162 if (PL_opargs[type] & OA_TARGET)
6163 padop->op_targ = pad_alloc(type, SVs_PADTMP);
6164 return CHECKOP(type, padop);
6165}
6166
6167#endif /* USE_ITHREADS */
6168
6169/*
6170=for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
6171
6172Constructs, checks, and returns an op of any type that involves an
6173embedded reference to a GV. C<type> is the opcode. C<flags> gives the
6174eight bits of C<op_flags>. C<gv> identifies the GV that the op should
6175reference; calling this function does not transfer ownership of any
6176reference to it.
6177
6178=cut
6179*/
6180
6181OP *
6182Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
6183{
6184 PERL_ARGS_ASSERT_NEWGVOP;
6185
6186#ifdef USE_ITHREADS
6187 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
6188#else
6189 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
6190#endif
6191}
6192
6193/*
6194=for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
6195
6196Constructs, checks, and returns an op of any type that involves an
6197embedded C-level pointer (PV). C<type> is the opcode. C<flags> gives
6198the eight bits of C<op_flags>. C<pv> supplies the C-level pointer, which
6199must have been allocated using C<PerlMemShared_malloc>; the memory will
6200be freed when the op is destroyed.
6201
6202=cut
6203*/
6204
6205OP *
6206Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
6207{
6208 dVAR;
6209 const bool utf8 = cBOOL(flags & SVf_UTF8);
6210 PVOP *pvop;
6211
6212 flags &= ~SVf_UTF8;
6213
6214 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
6215 || type == OP_RUNCV || type == OP_CUSTOM
6216 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
6217
6218 NewOp(1101, pvop, 1, PVOP);
6219 OpTYPE_set(pvop, type);
6220 pvop->op_pv = pv;
6221 pvop->op_next = (OP*)pvop;
6222 pvop->op_flags = (U8)flags;
6223 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
6224 if (PL_opargs[type] & OA_RETSCALAR)
6225 scalar((OP*)pvop);
6226 if (PL_opargs[type] & OA_TARGET)
6227 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
6228 return CHECKOP(type, pvop);
6229}
6230
6231void
6232Perl_package(pTHX_ OP *o)
6233{
6234 SV *const sv = cSVOPo->op_sv;
6235
6236 PERL_ARGS_ASSERT_PACKAGE;
6237
6238 SAVEGENERICSV(PL_curstash);
6239 save_item(PL_curstname);
6240
6241 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
6242
6243 sv_setsv(PL_curstname, sv);
6244
6245 PL_hints |= HINT_BLOCK_SCOPE;
6246 PL_parser->copline = NOLINE;
6247
6248 op_free(o);
6249}
6250
6251void
6252Perl_package_version( pTHX_ OP *v )
6253{
6254 U32 savehints = PL_hints;
6255 PERL_ARGS_ASSERT_PACKAGE_VERSION;
6256 PL_hints &= ~HINT_STRICT_VARS;
6257 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
6258 PL_hints = savehints;
6259 op_free(v);
6260}
6261
6262void
6263Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
6264{
6265 OP *pack;
6266 OP *imop;
6267 OP *veop;
6268 SV *use_version = NULL;
6269
6270 PERL_ARGS_ASSERT_UTILIZE;
6271
6272 if (idop->op_type != OP_CONST)
6273 Perl_croak(aTHX_ "Module name must be constant");
6274
6275 veop = NULL;
6276
6277 if (version) {
6278 SV * const vesv = ((SVOP*)version)->op_sv;
6279
6280 if (!arg && !SvNIOKp(vesv)) {
6281 arg = version;
6282 }
6283 else {
6284 OP *pack;
6285 SV *meth;
6286
6287 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
6288 Perl_croak(aTHX_ "Version number must be a constant number");
6289
6290 /* Make copy of idop so we don't free it twice */
6291 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
6292
6293 /* Fake up a method call to VERSION */
6294 meth = newSVpvs_share("VERSION");
6295 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
6296 op_append_elem(OP_LIST,
6297 op_prepend_elem(OP_LIST, pack, version),
6298 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
6299 }
6300 }
6301
6302 /* Fake up an import/unimport */
6303 if (arg && arg->op_type == OP_STUB) {
6304 imop = arg; /* no import on explicit () */
6305 }
6306 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
6307 imop = NULL; /* use 5.0; */
6308 if (aver)
6309 use_version = ((SVOP*)idop)->op_sv;
6310 else
6311 idop->op_private |= OPpCONST_NOVER;
6312 }
6313 else {
6314 SV *meth;
6315
6316 /* Make copy of idop so we don't free it twice */
6317 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
6318
6319 /* Fake up a method call to import/unimport */
6320 meth = aver
6321 ? newSVpvs_share("import") : newSVpvs_share("unimport");
6322 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
6323 op_append_elem(OP_LIST,
6324 op_prepend_elem(OP_LIST, pack, arg),
6325 newMETHOP_named(OP_METHOD_NAMED, 0, meth)
6326 ));
6327 }
6328
6329 /* Fake up the BEGIN {}, which does its thing immediately. */
6330 newATTRSUB(floor,
6331 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
6332 NULL,
6333 NULL,
6334 op_append_elem(OP_LINESEQ,
6335 op_append_elem(OP_LINESEQ,
6336 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
6337 newSTATEOP(0, NULL, veop)),
6338 newSTATEOP(0, NULL, imop) ));
6339
6340 if (use_version) {
6341 /* Enable the
6342 * feature bundle that corresponds to the required version. */
6343 use_version = sv_2mortal(new_version(use_version));
6344 S_enable_feature_bundle(aTHX_ use_version);
6345
6346 /* If a version >= 5.11.0 is requested, strictures are on by default! */
6347 if (vcmp(use_version,
6348 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
6349 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
6350 PL_hints |= HINT_STRICT_REFS;
6351 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
6352 PL_hints |= HINT_STRICT_SUBS;
6353 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
6354 PL_hints |= HINT_STRICT_VARS;
6355 }
6356 /* otherwise they are off */
6357 else {
6358 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
6359 PL_hints &= ~HINT_STRICT_REFS;
6360 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
6361 PL_hints &= ~HINT_STRICT_SUBS;
6362 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
6363 PL_hints &= ~HINT_STRICT_VARS;
6364 }
6365 }
6366
6367 /* The "did you use incorrect case?" warning used to be here.
6368 * The problem is that on case-insensitive filesystems one
6369 * might get false positives for "use" (and "require"):
6370 * "use Strict" or "require CARP" will work. This causes
6371 * portability problems for the script: in case-strict
6372 * filesystems the script will stop working.
6373 *
6374 * The "incorrect case" warning checked whether "use Foo"
6375 * imported "Foo" to your namespace, but that is wrong, too:
6376 * there is no requirement nor promise in the language that
6377 * a Foo.pm should or would contain anything in package "Foo".
6378 *
6379 * There is very little Configure-wise that can be done, either:
6380 * the case-sensitivity of the build filesystem of Perl does not
6381 * help in guessing the case-sensitivity of the runtime environment.
6382 */
6383
6384 PL_hints |= HINT_BLOCK_SCOPE;
6385 PL_parser->copline = NOLINE;
6386 COP_SEQMAX_INC; /* Purely for B::*'s benefit */
6387}
6388
6389/*
6390=head1 Embedding Functions
6391
6392=for apidoc load_module
6393
6394Loads the module whose name is pointed to by the string part of C<name>.
6395Note that the actual module name, not its filename, should be given.
6396Eg, "Foo::Bar" instead of "Foo/Bar.pm". ver, if specified and not NULL,
6397provides version semantics similar to C<use Foo::Bar VERSION>. The optional
6398trailing arguments can be used to specify arguments to the module's C<import()>
6399method, similar to C<use Foo::Bar VERSION LIST>; their precise handling depends
6400on the flags. The flags argument is a bitwise-ORed collection of any of
6401C<PERL_LOADMOD_DENY>, C<PERL_LOADMOD_NOIMPORT>, or C<PERL_LOADMOD_IMPORT_OPS>
6402(or 0 for no flags).
6403
6404If C<PERL_LOADMOD_NOIMPORT> is set, the module is loaded as if with an empty
6405import list, as in C<use Foo::Bar ()>; this is the only circumstance in which
6406the trailing optional arguments may be omitted entirely. Otherwise, if
6407C<PERL_LOADMOD_IMPORT_OPS> is set, the trailing arguments must consist of
6408exactly one C<OP*>, containing the op tree that produces the relevant import
6409arguments. Otherwise, the trailing arguments must all be C<SV*> values that
6410will be used as import arguments; and the list must be terminated with C<(SV*)
6411NULL>. If neither C<PERL_LOADMOD_NOIMPORT> nor C<PERL_LOADMOD_IMPORT_OPS> is
6412set, the trailing C<NULL> pointer is needed even if no import arguments are
6413desired. The reference count for each specified C<SV*> argument is
6414decremented. In addition, the C<name> argument is modified.
6415
6416If C<PERL_LOADMOD_DENY> is set, the module is loaded as if with C<no> rather
6417than C<use>.
6418
6419=cut */
6420
6421void
6422Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
6423{
6424 va_list args;
6425
6426 PERL_ARGS_ASSERT_LOAD_MODULE;
6427
6428 va_start(args, ver);
6429 vload_module(flags, name, ver, &args);
6430 va_end(args);
6431}
6432
6433#ifdef PERL_IMPLICIT_CONTEXT
6434void
6435Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
6436{
6437 dTHX;
6438 va_list args;
6439 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
6440 va_start(args, ver);
6441 vload_module(flags, name, ver, &args);
6442 va_end(args);
6443}
6444#endif
6445
6446void
6447Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
6448{
6449 OP *veop, *imop;
6450 OP * const modname = newSVOP(OP_CONST, 0, name);
6451
6452 PERL_ARGS_ASSERT_VLOAD_MODULE;
6453
6454 modname->op_private |= OPpCONST_BARE;
6455 if (ver) {
6456 veop = newSVOP(OP_CONST, 0, ver);
6457 }
6458 else
6459 veop = NULL;
6460 if (flags & PERL_LOADMOD_NOIMPORT) {
6461 imop = sawparens(newNULLLIST());
6462 }
6463 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
6464 imop = va_arg(*args, OP*);
6465 }
6466 else {
6467 SV *sv;
6468 imop = NULL;
6469 sv = va_arg(*args, SV*);
6470 while (sv) {
6471 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
6472 sv = va_arg(*args, SV*);
6473 }
6474 }
6475
6476 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
6477 * that it has a PL_parser to play with while doing that, and also
6478 * that it doesn't mess with any existing parser, by creating a tmp
6479 * new parser with lex_start(). This won't actually be used for much,
6480 * since pp_require() will create another parser for the real work.
6481 * The ENTER/LEAVE pair protect callers from any side effects of use. */
6482
6483 ENTER;
6484 SAVEVPTR(PL_curcop);
6485 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
6486 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
6487 veop, modname, imop);
6488 LEAVE;
6489}
6490
6491PERL_STATIC_INLINE OP *
6492S_new_entersubop(pTHX_ GV *gv, OP *arg)
6493{
6494 return newUNOP(OP_ENTERSUB, OPf_STACKED,
6495 newLISTOP(OP_LIST, 0, arg,
6496 newUNOP(OP_RV2CV, 0,
6497 newGVOP(OP_GV, 0, gv))));
6498}
6499
6500OP *
6501Perl_dofile(pTHX_ OP *term, I32 force_builtin)
6502{
6503 OP *doop;
6504 GV *gv;
6505
6506 PERL_ARGS_ASSERT_DOFILE;
6507
6508 if (!force_builtin && (gv = gv_override("do", 2))) {
6509 doop = S_new_entersubop(aTHX_ gv, term);
6510 }
6511 else {
6512 doop = newUNOP(OP_DOFILE, 0, scalar(term));
6513 }
6514 return doop;
6515}
6516
6517/*
6518=head1 Optree construction
6519
6520=for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval
6521
6522Constructs, checks, and returns an C<lslice> (list slice) op. C<flags>
6523gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
6524be set automatically, and, shifted up eight bits, the eight bits of
6525C<op_private>, except that the bit with value 1 or 2 is automatically
6526set as required. C<listval> and C<subscript> supply the parameters of
6527the slice; they are consumed by this function and become part of the
6528constructed op tree.
6529
6530=cut
6531*/
6532
6533OP *
6534Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
6535{
6536 return newBINOP(OP_LSLICE, flags,
6537 list(force_list(subscript, 1)),
6538 list(force_list(listval, 1)) );
6539}
6540
6541#define ASSIGN_LIST 1
6542#define ASSIGN_REF 2
6543
6544STATIC I32
6545S_assignment_type(pTHX_ const OP *o)
6546{
6547 unsigned type;
6548 U8 flags;
6549 U8 ret;
6550
6551 if (!o)
6552 return TRUE;
6553
6554 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
6555 o = cUNOPo->op_first;
6556
6557 flags = o->op_flags;
6558 type = o->op_type;
6559 if (type == OP_COND_EXPR) {
6560 OP * const sib = OpSIBLING(cLOGOPo->op_first);
6561 const I32 t = assignment_type(sib);
6562 const I32 f = assignment_type(OpSIBLING(sib));
6563
6564 if (t == ASSIGN_LIST && f == ASSIGN_LIST)
6565 return ASSIGN_LIST;
6566 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST))
6567 yyerror("Assignment to both a list and a scalar");
6568 return FALSE;
6569 }
6570
6571 if (type == OP_SREFGEN)
6572 {
6573 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first;
6574 type = kid->op_type;
6575 flags |= kid->op_flags;
6576 if (!(flags & OPf_PARENS)
6577 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV ||
6578 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV ))
6579 return ASSIGN_REF;
6580 ret = ASSIGN_REF;
6581 }
6582 else ret = 0;
6583
6584 if (type == OP_LIST &&
6585 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
6586 o->op_private & OPpLVAL_INTRO)
6587 return ret;
6588
6589 if (type == OP_LIST || flags & OPf_PARENS ||
6590 type == OP_RV2AV || type == OP_RV2HV ||
6591 type == OP_ASLICE || type == OP_HSLICE ||
6592 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN)
6593 return TRUE;
6594
6595 if (type == OP_PADAV || type == OP_PADHV)
6596 return TRUE;
6597
6598 if (type == OP_RV2SV)
6599 return ret;
6600
6601 return ret;
6602}
6603
6604
6605/*
6606=for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right
6607
6608Constructs, checks, and returns an assignment op. C<left> and C<right>
6609supply the parameters of the assignment; they are consumed by this
6610function and become part of the constructed op tree.
6611
6612If C<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
6613a suitable conditional optree is constructed. If C<optype> is the opcode
6614of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
6615performs the binary operation and assigns the result to the left argument.
6616Either way, if C<optype> is non-zero then C<flags> has no effect.
6617
6618If C<optype> is zero, then a plain scalar or list assignment is
6619constructed. Which type of assignment it is is automatically determined.
6620C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
6621will be set automatically, and, shifted up eight bits, the eight bits
6622of C<op_private>, except that the bit with value 1 or 2 is automatically
6623set as required.
6624
6625=cut
6626*/
6627
6628OP *
6629Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
6630{
6631 OP *o;
6632 I32 assign_type;
6633
6634 if (optype) {
6635 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
6636 right = scalar(right);
6637 return newLOGOP(optype, 0,
6638 op_lvalue(scalar(left), optype),
6639 newBINOP(OP_SASSIGN, OPpASSIGN_BACKWARDS<<8, right, right));
6640 }
6641 else {
6642 return newBINOP(optype, OPf_STACKED,
6643 op_lvalue(scalar(left), optype), scalar(right));
6644 }
6645 }
6646
6647 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) {
6648 static const char no_list_state[] = "Initialization of state variables"
6649 " in list context currently forbidden";
6650 OP *curop;
6651
6652 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE)
6653 left->op_private &= ~ OPpSLICEWARNING;
6654
6655 PL_modcount = 0;
6656 left = op_lvalue(left, OP_AASSIGN);
6657 curop = list(force_list(left, 1));
6658 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, 1)), curop);
6659 o->op_private = (U8)(0 | (flags >> 8));
6660
6661 if (OP_TYPE_IS_OR_WAS(left, OP_LIST))
6662 {
6663 OP* lop = ((LISTOP*)left)->op_first;
6664 while (lop) {
6665 if ((lop->op_type == OP_PADSV ||
6666 lop->op_type == OP_PADAV ||
6667 lop->op_type == OP_PADHV ||
6668 lop->op_type == OP_PADANY)
6669 && (lop->op_private & OPpPAD_STATE)
6670 )
6671 yyerror(no_list_state);
6672 lop = OpSIBLING(lop);
6673 }
6674 }
6675 else if ( (left->op_private & OPpLVAL_INTRO)
6676 && (left->op_private & OPpPAD_STATE)
6677 && ( left->op_type == OP_PADSV
6678 || left->op_type == OP_PADAV
6679 || left->op_type == OP_PADHV
6680 || left->op_type == OP_PADANY)
6681 ) {
6682 /* All single variable list context state assignments, hence
6683 state ($a) = ...
6684 (state $a) = ...
6685 state @a = ...
6686 state (@a) = ...
6687 (state @a) = ...
6688 state %a = ...
6689 state (%a) = ...
6690 (state %a) = ...
6691 */
6692 yyerror(no_list_state);
6693 }
6694
6695 /* optimise @a = split(...) into:
6696 * @{expr}: split(..., @{expr}) (where @a is not flattened)
6697 * @a, my @a, local @a: split(...) (where @a is attached to
6698 * the split op itself)
6699 */
6700
6701 if ( right
6702 && right->op_type == OP_SPLIT
6703 /* don't do twice, e.g. @b = (@a = split) */
6704 && !(right->op_private & OPpSPLIT_ASSIGN))
6705 {
6706 OP *gvop = NULL;
6707
6708 if ( ( left->op_type == OP_RV2AV
6709 && (gvop=((UNOP*)left)->op_first)->op_type==OP_GV)
6710 || left->op_type == OP_PADAV)
6711 {
6712 /* @pkg or @lex or local @pkg' or 'my @lex' */
6713 OP *tmpop;
6714 if (gvop) {
6715#ifdef USE_ITHREADS
6716 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff
6717 = cPADOPx(gvop)->op_padix;
6718 cPADOPx(gvop)->op_padix = 0; /* steal it */
6719#else
6720 ((PMOP*)right)->op_pmreplrootu.op_pmtargetgv
6721 = MUTABLE_GV(cSVOPx(gvop)->op_sv);
6722 cSVOPx(gvop)->op_sv = NULL; /* steal it */
6723#endif
6724 right->op_private |=
6725 left->op_private & OPpOUR_INTRO;
6726 }
6727 else {
6728 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff = left->op_targ;
6729 left->op_targ = 0; /* steal it */
6730 right->op_private |= OPpSPLIT_LEX;
6731 }
6732 right->op_private |= left->op_private & OPpLVAL_INTRO;
6733
6734 detach_split:
6735 tmpop = cUNOPo->op_first; /* to list (nulled) */
6736 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
6737 assert(OpSIBLING(tmpop) == right);
6738 assert(!OpHAS_SIBLING(right));
6739 /* detach the split subtreee from the o tree,
6740 * then free the residual o tree */
6741 op_sibling_splice(cUNOPo->op_first, tmpop, 1, NULL);
6742 op_free(o); /* blow off assign */
6743 right->op_private |= OPpSPLIT_ASSIGN;
6744 right->op_flags &= ~OPf_WANT;
6745 /* "I don't know and I don't care." */
6746 return right;
6747 }
6748 else if (left->op_type == OP_RV2AV) {
6749 /* @{expr} */
6750
6751 OP *pushop = cUNOPx(cBINOPo->op_last)->op_first;
6752 assert(OpSIBLING(pushop) == left);
6753 /* Detach the array ... */
6754 op_sibling_splice(cBINOPo->op_last, pushop, 1, NULL);
6755 /* ... and attach it to the split. */
6756 op_sibling_splice(right, cLISTOPx(right)->op_last,
6757 0, left);
6758 right->op_flags |= OPf_STACKED;
6759 /* Detach split and expunge aassign as above. */
6760 goto detach_split;
6761 }
6762 else if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
6763 ((LISTOP*)right)->op_last->op_type == OP_CONST)
6764 {
6765 /* convert split(...,0) to split(..., PL_modcount+1) */
6766 SV ** const svp =
6767 &((SVOP*)((LISTOP*)right)->op_last)->op_sv;
6768 SV * const sv = *svp;
6769 if (SvIOK(sv) && SvIVX(sv) == 0)
6770 {
6771 if (right->op_private & OPpSPLIT_IMPLIM) {
6772 /* our own SV, created in ck_split */
6773 SvREADONLY_off(sv);
6774 sv_setiv(sv, PL_modcount+1);
6775 }
6776 else {
6777 /* SV may belong to someone else */
6778 SvREFCNT_dec(sv);
6779 *svp = newSViv(PL_modcount+1);
6780 }
6781 }
6782 }
6783 }
6784 return o;
6785 }
6786 if (assign_type == ASSIGN_REF)
6787 return newBINOP(OP_REFASSIGN, flags, scalar(right), left);
6788 if (!right)
6789 right = newOP(OP_UNDEF, 0);
6790 if (right->op_type == OP_READLINE) {
6791 right->op_flags |= OPf_STACKED;
6792 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
6793 scalar(right));
6794 }
6795 else {
6796 o = newBINOP(OP_SASSIGN, flags,
6797 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
6798 }
6799 return o;
6800}
6801
6802/*
6803=for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o
6804
6805Constructs a state op (COP). The state op is normally a C<nextstate> op,
6806but will be a C<dbstate> op if debugging is enabled for currently-compiled
6807code. The state op is populated from C<PL_curcop> (or C<PL_compiling>).
6808If C<label> is non-null, it supplies the name of a label to attach to
6809the state op; this function takes ownership of the memory pointed at by
6810C<label>, and will free it. C<flags> gives the eight bits of C<op_flags>
6811for the state op.
6812
6813If C<o> is null, the state op is returned. Otherwise the state op is
6814combined with C<o> into a C<lineseq> list op, which is returned. C<o>
6815is consumed by this function and becomes part of the returned op tree.
6816
6817=cut
6818*/
6819
6820OP *
6821Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
6822{
6823 dVAR;
6824 const U32 seq = intro_my();
6825 const U32 utf8 = flags & SVf_UTF8;
6826 COP *cop;
6827
6828 PL_parser->parsed_sub = 0;
6829
6830 flags &= ~SVf_UTF8;
6831
6832 NewOp(1101, cop, 1, COP);
6833 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
6834 OpTYPE_set(cop, OP_DBSTATE);
6835 }
6836 else {
6837 OpTYPE_set(cop, OP_NEXTSTATE);
6838 }
6839 cop->op_flags = (U8)flags;
6840 CopHINTS_set(cop, PL_hints);
6841#ifdef VMS
6842 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH;
6843#endif
6844 cop->op_next = (OP*)cop;
6845
6846 cop->cop_seq = seq;
6847 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
6848 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
6849 if (label) {
6850 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
6851
6852 PL_hints |= HINT_BLOCK_SCOPE;
6853 /* It seems that we need to defer freeing this pointer, as other parts
6854 of the grammar end up wanting to copy it after this op has been
6855 created. */
6856 SAVEFREEPV(label);
6857 }
6858
6859 if (PL_parser->preambling != NOLINE) {
6860 CopLINE_set(cop, PL_parser->preambling);
6861 PL_parser->copline = NOLINE;
6862 }
6863 else if (PL_parser->copline == NOLINE)
6864 CopLINE_set(cop, CopLINE(PL_curcop));
6865 else {
6866 CopLINE_set(cop, PL_parser->copline);
6867 PL_parser->copline = NOLINE;
6868 }
6869#ifdef USE_ITHREADS
6870 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
6871#else
6872 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
6873#endif
6874 CopSTASH_set(cop, PL_curstash);
6875
6876 if (cop->op_type == OP_DBSTATE) {
6877 /* this line can have a breakpoint - store the cop in IV */
6878 AV *av = CopFILEAVx(PL_curcop);
6879 if (av) {
6880 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
6881 if (svp && *svp != &PL_sv_undef ) {
6882 (void)SvIOK_on(*svp);
6883 SvIV_set(*svp, PTR2IV(cop));
6884 }
6885 }
6886 }
6887
6888 if (flags & OPf_SPECIAL)
6889 op_null((OP*)cop);
6890 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
6891}
6892
6893/*
6894=for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other
6895
6896Constructs, checks, and returns a logical (flow control) op. C<type>
6897is the opcode. C<flags> gives the eight bits of C<op_flags>, except
6898that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
6899the eight bits of C<op_private>, except that the bit with value 1 is
6900automatically set. C<first> supplies the expression controlling the
6901flow, and C<other> supplies the side (alternate) chain of ops; they are
6902consumed by this function and become part of the constructed op tree.
6903
6904=cut
6905*/
6906
6907OP *
6908Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
6909{
6910 PERL_ARGS_ASSERT_NEWLOGOP;
6911
6912 return new_logop(type, flags, &first, &other);
6913}
6914
6915STATIC OP *
6916S_search_const(pTHX_ OP *o)
6917{
6918 PERL_ARGS_ASSERT_SEARCH_CONST;
6919
6920 switch (o->op_type) {
6921 case OP_CONST:
6922 return o;
6923 case OP_NULL:
6924 if (o->op_flags & OPf_KIDS)
6925 return search_const(cUNOPo->op_first);
6926 break;
6927 case OP_LEAVE:
6928 case OP_SCOPE:
6929 case OP_LINESEQ:
6930 {
6931 OP *kid;
6932 if (!(o->op_flags & OPf_KIDS))
6933 return NULL;
6934 kid = cLISTOPo->op_first;
6935 do {
6936 switch (kid->op_type) {
6937 case OP_ENTER:
6938 case OP_NULL:
6939 case OP_NEXTSTATE:
6940 kid = OpSIBLING(kid);
6941 break;
6942 default:
6943 if (kid != cLISTOPo->op_last)
6944 return NULL;
6945 goto last;
6946 }
6947 } while (kid);
6948 if (!kid)
6949 kid = cLISTOPo->op_last;
6950 last:
6951 return search_const(kid);
6952 }
6953 }
6954
6955 return NULL;
6956}
6957
6958STATIC OP *
6959S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
6960{
6961 dVAR;
6962 LOGOP *logop;
6963 OP *o;
6964 OP *first;
6965 OP *other;
6966 OP *cstop = NULL;
6967 int prepend_not = 0;
6968
6969 PERL_ARGS_ASSERT_NEW_LOGOP;
6970
6971 first = *firstp;
6972 other = *otherp;
6973
6974 /* [perl #59802]: Warn about things like "return $a or $b", which
6975 is parsed as "(return $a) or $b" rather than "return ($a or
6976 $b)". NB: This also applies to xor, which is why we do it
6977 here.
6978 */
6979 switch (first->op_type) {
6980 case OP_NEXT:
6981 case OP_LAST:
6982 case OP_REDO:
6983 /* XXX: Perhaps we should emit a stronger warning for these.
6984 Even with the high-precedence operator they don't seem to do
6985 anything sensible.
6986
6987 But until we do, fall through here.
6988 */
6989 case OP_RETURN:
6990 case OP_EXIT:
6991 case OP_DIE:
6992 case OP_GOTO:
6993 /* XXX: Currently we allow people to "shoot themselves in the
6994 foot" by explicitly writing "(return $a) or $b".
6995
6996 Warn unless we are looking at the result from folding or if
6997 the programmer explicitly grouped the operators like this.
6998 The former can occur with e.g.
6999
7000 use constant FEATURE => ( $] >= ... );
7001 sub { not FEATURE and return or do_stuff(); }
7002 */
7003 if (!first->op_folded && !(first->op_flags & OPf_PARENS))
7004 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
7005 "Possible precedence issue with control flow operator");
7006 /* XXX: Should we optimze this to "return $a;" (i.e. remove
7007 the "or $b" part)?
7008 */
7009 break;
7010 }
7011
7012 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
7013 return newBINOP(type, flags, scalar(first), scalar(other));
7014
7015 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP
7016 || type == OP_CUSTOM);
7017
7018 scalarboolean(first);
7019
7020 /* search for a constant op that could let us fold the test */
7021 if ((cstop = search_const(first))) {
7022 if (cstop->op_private & OPpCONST_STRICT)
7023 no_bareword_allowed(cstop);
7024 else if ((cstop->op_private & OPpCONST_BARE))
7025 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
7026 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) ||
7027 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
7028 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
7029 /* Elide the (constant) lhs, since it can't affect the outcome */
7030 *firstp = NULL;
7031 if (other->op_type == OP_CONST)
7032 other->op_private |= OPpCONST_SHORTCIRCUIT;
7033 op_free(first);
7034 if (other->op_type == OP_LEAVE)
7035 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
7036 else if (other->op_type == OP_MATCH
7037 || other->op_type == OP_SUBST
7038 || other->op_type == OP_TRANSR
7039 || other->op_type == OP_TRANS)
7040 /* Mark the op as being unbindable with =~ */
7041 other->op_flags |= OPf_SPECIAL;
7042
7043 other->op_folded = 1;
7044 return other;
7045 }
7046 else {
7047 /* Elide the rhs, since the outcome is entirely determined by
7048 * the (constant) lhs */
7049
7050 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
7051 const OP *o2 = other;
7052 if ( ! (o2->op_type == OP_LIST
7053 && (( o2 = cUNOPx(o2)->op_first))
7054 && o2->op_type == OP_PUSHMARK
7055 && (( o2 = OpSIBLING(o2))) )
7056 )
7057 o2 = other;
7058 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
7059 || o2->op_type == OP_PADHV)
7060 && o2->op_private & OPpLVAL_INTRO
7061 && !(o2->op_private & OPpPAD_STATE))
7062 {
7063 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7064 "Deprecated use of my() in false conditional. "
7065 "This will be a fatal error in Perl 5.30");
7066 }
7067
7068 *otherp = NULL;
7069 if (cstop->op_type == OP_CONST)
7070 cstop->op_private |= OPpCONST_SHORTCIRCUIT;
7071 op_free(other);
7072 return first;
7073 }
7074 }
7075 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
7076 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
7077 {
7078 const OP * const k1 = ((UNOP*)first)->op_first;
7079 const OP * const k2 = OpSIBLING(k1);
7080 OPCODE warnop = 0;
7081 switch (first->op_type)
7082 {
7083 case OP_NULL:
7084 if (k2 && k2->op_type == OP_READLINE
7085 && (k2->op_flags & OPf_STACKED)
7086 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
7087 {
7088 warnop = k2->op_type;
7089 }
7090 break;
7091
7092 case OP_SASSIGN:
7093 if (k1->op_type == OP_READDIR
7094 || k1->op_type == OP_GLOB
7095 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
7096 || k1->op_type == OP_EACH
7097 || k1->op_type == OP_AEACH)
7098 {
7099 warnop = ((k1->op_type == OP_NULL)
7100 ? (OPCODE)k1->op_targ : k1->op_type);
7101 }
7102 break;
7103 }
7104 if (warnop) {
7105 const line_t oldline = CopLINE(PL_curcop);
7106 /* This ensures that warnings are reported at the first line
7107 of the construction, not the last. */
7108 CopLINE_set(PL_curcop, PL_parser->copline);
7109 Perl_warner(aTHX_ packWARN(WARN_MISC),
7110 "Value of %s%s can be \"0\"; test with defined()",
7111 PL_op_desc[warnop],
7112 ((warnop == OP_READLINE || warnop == OP_GLOB)
7113 ? " construct" : "() operator"));
7114 CopLINE_set(PL_curcop, oldline);
7115 }
7116 }
7117
7118 /* optimize AND and OR ops that have NOTs as children */
7119 if (first->op_type == OP_NOT
7120 && (first->op_flags & OPf_KIDS)
7121 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
7122 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
7123 ) {
7124 if (type == OP_AND || type == OP_OR) {
7125 if (type == OP_AND)
7126 type = OP_OR;
7127 else
7128 type = OP_AND;
7129 op_null(first);
7130 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
7131 op_null(other);
7132 prepend_not = 1; /* prepend a NOT op later */
7133 }
7134 }
7135 }
7136
7137 logop = alloc_LOGOP(type, first, LINKLIST(other));
7138 logop->op_flags |= (U8)flags;
7139 logop->op_private = (U8)(1 | (flags >> 8));
7140
7141 /* establish postfix order */
7142 logop->op_next = LINKLIST(first);
7143 first->op_next = (OP*)logop;
7144 assert(!OpHAS_SIBLING(first));
7145 op_sibling_splice((OP*)logop, first, 0, other);
7146
7147 CHECKOP(type,logop);
7148
7149 o = newUNOP(prepend_not ? OP_NOT : OP_NULL,
7150 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0,
7151 (OP*)logop);
7152 other->op_next = o;
7153
7154 return o;
7155}
7156
7157/*
7158=for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop
7159
7160Constructs, checks, and returns a conditional-expression (C<cond_expr>)
7161op. C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
7162will be set automatically, and, shifted up eight bits, the eight bits of
7163C<op_private>, except that the bit with value 1 is automatically set.
7164C<first> supplies the expression selecting between the two branches,
7165and C<trueop> and C<falseop> supply the branches; they are consumed by
7166this function and become part of the constructed op tree.
7167
7168=cut
7169*/
7170
7171OP *
7172Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
7173{
7174 dVAR;
7175 LOGOP *logop;
7176 OP *start;
7177 OP *o;
7178 OP *cstop;
7179
7180 PERL_ARGS_ASSERT_NEWCONDOP;
7181
7182 if (!falseop)
7183 return newLOGOP(OP_AND, 0, first, trueop);
7184 if (!trueop)
7185 return newLOGOP(OP_OR, 0, first, falseop);
7186
7187 scalarboolean(first);
7188 if ((cstop = search_const(first))) {
7189 /* Left or right arm of the conditional? */
7190 const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
7191 OP *live = left ? trueop : falseop;
7192 OP *const dead = left ? falseop : trueop;
7193 if (cstop->op_private & OPpCONST_BARE &&
7194 cstop->op_private & OPpCONST_STRICT) {
7195 no_bareword_allowed(cstop);
7196 }
7197 op_free(first);
7198 op_free(dead);
7199 if (live->op_type == OP_LEAVE)
7200 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
7201 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
7202 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
7203 /* Mark the op as being unbindable with =~ */
7204 live->op_flags |= OPf_SPECIAL;
7205 live->op_folded = 1;
7206 return live;
7207 }
7208 logop = alloc_LOGOP(OP_COND_EXPR, first, LINKLIST(trueop));
7209 logop->op_flags |= (U8)flags;
7210 logop->op_private = (U8)(1 | (flags >> 8));
7211 logop->op_next = LINKLIST(falseop);
7212
7213 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
7214 logop);
7215
7216 /* establish postfix order */
7217 start = LINKLIST(first);
7218 first->op_next = (OP*)logop;
7219
7220 /* make first, trueop, falseop siblings */
7221 op_sibling_splice((OP*)logop, first, 0, trueop);
7222 op_sibling_splice((OP*)logop, trueop, 0, falseop);
7223
7224 o = newUNOP(OP_NULL, 0, (OP*)logop);
7225
7226 trueop->op_next = falseop->op_next = o;
7227
7228 o->op_next = start;
7229 return o;
7230}
7231
7232/*
7233=for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right
7234
7235Constructs and returns a C<range> op, with subordinate C<flip> and
7236C<flop> ops. C<flags> gives the eight bits of C<op_flags> for the
7237C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
7238for both the C<flip> and C<range> ops, except that the bit with value
72391 is automatically set. C<left> and C<right> supply the expressions
7240controlling the endpoints of the range; they are consumed by this function
7241and become part of the constructed op tree.
7242
7243=cut
7244*/
7245
7246OP *
7247Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
7248{
7249 LOGOP *range;
7250 OP *flip;
7251 OP *flop;
7252 OP *leftstart;
7253 OP *o;
7254
7255 PERL_ARGS_ASSERT_NEWRANGE;
7256
7257 range = alloc_LOGOP(OP_RANGE, left, LINKLIST(right));
7258 range->op_flags = OPf_KIDS;
7259 leftstart = LINKLIST(left);
7260 range->op_private = (U8)(1 | (flags >> 8));
7261
7262 /* make left and right siblings */
7263 op_sibling_splice((OP*)range, left, 0, right);
7264
7265 range->op_next = (OP*)range;
7266 flip = newUNOP(OP_FLIP, flags, (OP*)range);
7267 flop = newUNOP(OP_FLOP, 0, flip);
7268 o = newUNOP(OP_NULL, 0, flop);
7269 LINKLIST(flop);
7270 range->op_next = leftstart;
7271
7272 left->op_next = flip;
7273 right->op_next = flop;
7274
7275 range->op_targ =
7276 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);
7277 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
7278 flip->op_targ =
7279 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);;
7280 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
7281 SvPADTMP_on(PAD_SV(flip->op_targ));
7282
7283 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
7284 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
7285
7286 /* check barewords before they might be optimized aways */
7287 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
7288 no_bareword_allowed(left);
7289 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
7290 no_bareword_allowed(right);
7291
7292 flip->op_next = o;
7293 if (!flip->op_private || !flop->op_private)
7294 LINKLIST(o); /* blow off optimizer unless constant */
7295
7296 return o;
7297}
7298
7299/*
7300=for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block
7301
7302Constructs, checks, and returns an op tree expressing a loop. This is
7303only a loop in the control flow through the op tree; it does not have
7304the heavyweight loop structure that allows exiting the loop by C<last>
7305and suchlike. C<flags> gives the eight bits of C<op_flags> for the
7306top-level op, except that some bits will be set automatically as required.
7307C<expr> supplies the expression controlling loop iteration, and C<block>
7308supplies the body of the loop; they are consumed by this function and
7309become part of the constructed op tree. C<debuggable> is currently
7310unused and should always be 1.
7311
7312=cut
7313*/
7314
7315OP *
7316Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
7317{
7318 OP* listop;
7319 OP* o;
7320 const bool once = block && block->op_flags & OPf_SPECIAL &&
7321 block->op_type == OP_NULL;
7322
7323 PERL_UNUSED_ARG(debuggable);
7324
7325 if (expr) {
7326 if (once && (
7327 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
7328 || ( expr->op_type == OP_NOT
7329 && cUNOPx(expr)->op_first->op_type == OP_CONST
7330 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first))
7331 )
7332 ))
7333 /* Return the block now, so that S_new_logop does not try to
7334 fold it away. */
7335 return block; /* do {} while 0 does once */
7336 if (expr->op_type == OP_READLINE
7337 || expr->op_type == OP_READDIR
7338 || expr->op_type == OP_GLOB
7339 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
7340 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
7341 expr = newUNOP(OP_DEFINED, 0,
7342 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
7343 } else if (expr->op_flags & OPf_KIDS) {
7344 const OP * const k1 = ((UNOP*)expr)->op_first;
7345 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL;
7346 switch (expr->op_type) {
7347 case OP_NULL:
7348 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
7349 && (k2->op_flags & OPf_STACKED)
7350 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
7351 expr = newUNOP(OP_DEFINED, 0, expr);
7352 break;
7353
7354 case OP_SASSIGN:
7355 if (k1 && (k1->op_type == OP_READDIR
7356 || k1->op_type == OP_GLOB
7357 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
7358 || k1->op_type == OP_EACH
7359 || k1->op_type == OP_AEACH))
7360 expr = newUNOP(OP_DEFINED, 0, expr);
7361 break;
7362 }
7363 }
7364 }
7365
7366 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
7367 * op, in listop. This is wrong. [perl #27024] */
7368 if (!block)
7369 block = newOP(OP_NULL, 0);
7370 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
7371 o = new_logop(OP_AND, 0, &expr, &listop);
7372
7373 if (once) {
7374 ASSUME(listop);
7375 }
7376
7377 if (listop)
7378 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
7379
7380 if (once && o != listop)
7381 {
7382 assert(cUNOPo->op_first->op_type == OP_AND
7383 || cUNOPo->op_first->op_type == OP_OR);
7384 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
7385 }
7386
7387 if (o == listop)
7388 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
7389
7390 o->op_flags |= flags;
7391 o = op_scope(o);
7392 o->op_flags |= OPf_SPECIAL; /* suppress cx_popblock() curpm restoration*/
7393 return o;
7394}
7395
7396/*
7397=for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my
7398
7399Constructs, checks, and returns an op tree expressing a C<while> loop.
7400This is a heavyweight loop, with structure that allows exiting the loop
7401by C<last> and suchlike.
7402
7403C<loop> is an optional preconstructed C<enterloop> op to use in the
7404loop; if it is null then a suitable op will be constructed automatically.
7405C<expr> supplies the loop's controlling expression. C<block> supplies the
7406main body of the loop, and C<cont> optionally supplies a C<continue> block
7407that operates as a second half of the body. All of these optree inputs
7408are consumed by this function and become part of the constructed op tree.
7409
7410C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
7411op and, shifted up eight bits, the eight bits of C<op_private> for
7412the C<leaveloop> op, except that (in both cases) some bits will be set
7413automatically. C<debuggable> is currently unused and should always be 1.
7414C<has_my> can be supplied as true to force the
7415loop body to be enclosed in its own scope.
7416
7417=cut
7418*/
7419
7420OP *
7421Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
7422 OP *expr, OP *block, OP *cont, I32 has_my)
7423{
7424 dVAR;
7425 OP *redo;
7426 OP *next = NULL;
7427 OP *listop;
7428 OP *o;
7429 U8 loopflags = 0;
7430
7431 PERL_UNUSED_ARG(debuggable);
7432
7433 if (expr) {
7434 if (expr->op_type == OP_READLINE
7435 || expr->op_type == OP_READDIR
7436 || expr->op_type == OP_GLOB
7437 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
7438 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
7439 expr = newUNOP(OP_DEFINED, 0,
7440 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
7441 } else if (expr->op_flags & OPf_KIDS) {
7442 const OP * const k1 = ((UNOP*)expr)->op_first;
7443 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL;
7444 switch (expr->op_type) {
7445 case OP_NULL:
7446 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
7447 && (k2->op_flags & OPf_STACKED)
7448 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
7449 expr = newUNOP(OP_DEFINED, 0, expr);
7450 break;
7451
7452 case OP_SASSIGN:
7453 if (k1 && (k1->op_type == OP_READDIR
7454 || k1->op_type == OP_GLOB
7455 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
7456 || k1->op_type == OP_EACH
7457 || k1->op_type == OP_AEACH))
7458 expr = newUNOP(OP_DEFINED, 0, expr);
7459 break;
7460 }
7461 }
7462 }
7463
7464 if (!block)
7465 block = newOP(OP_NULL, 0);
7466 else if (cont || has_my) {
7467 block = op_scope(block);
7468 }
7469
7470 if (cont) {
7471 next = LINKLIST(cont);
7472 }
7473 if (expr) {
7474 OP * const unstack = newOP(OP_UNSTACK, 0);
7475 if (!next)
7476 next = unstack;
7477 cont = op_append_elem(OP_LINESEQ, cont, unstack);
7478 }
7479
7480 assert(block);
7481 listop = op_append_list(OP_LINESEQ, block, cont);
7482 assert(listop);
7483 redo = LINKLIST(listop);
7484
7485 if (expr) {
7486 scalar(listop);
7487 o = new_logop(OP_AND, 0, &expr, &listop);
7488 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
7489 op_free((OP*)loop);
7490 return expr; /* listop already freed by new_logop */
7491 }
7492 if (listop)
7493 ((LISTOP*)listop)->op_last->op_next =
7494 (o == listop ? redo : LINKLIST(o));
7495 }
7496 else
7497 o = listop;
7498
7499 if (!loop) {
7500 NewOp(1101,loop,1,LOOP);
7501 OpTYPE_set(loop, OP_ENTERLOOP);
7502 loop->op_private = 0;
7503 loop->op_next = (OP*)loop;
7504 }
7505
7506 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
7507
7508 loop->op_redoop = redo;
7509 loop->op_lastop = o;
7510 o->op_private |= loopflags;
7511
7512 if (next)
7513 loop->op_nextop = next;
7514 else
7515 loop->op_nextop = o;
7516
7517 o->op_flags |= flags;
7518 o->op_private |= (flags >> 8);
7519 return o;
7520}
7521
7522/*
7523=for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont
7524
7525Constructs, checks, and returns an op tree expressing a C<foreach>
7526loop (iteration through a list of values). This is a heavyweight loop,
7527with structure that allows exiting the loop by C<last> and suchlike.
7528
7529C<sv> optionally supplies the variable that will be aliased to each
7530item in turn; if null, it defaults to C<$_>.
7531C<expr> supplies the list of values to iterate over. C<block> supplies
7532the main body of the loop, and C<cont> optionally supplies a C<continue>
7533block that operates as a second half of the body. All of these optree
7534inputs are consumed by this function and become part of the constructed
7535op tree.
7536
7537C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
7538op and, shifted up eight bits, the eight bits of C<op_private> for
7539the C<leaveloop> op, except that (in both cases) some bits will be set
7540automatically.
7541
7542=cut
7543*/
7544
7545OP *
7546Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
7547{
7548 dVAR;
7549 LOOP *loop;
7550 OP *wop;
7551 PADOFFSET padoff = 0;
7552 I32 iterflags = 0;
7553 I32 iterpflags = 0;
7554
7555 PERL_ARGS_ASSERT_NEWFOROP;
7556
7557 if (sv) {
7558 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
7559 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
7560 OpTYPE_set(sv, OP_RV2GV);
7561
7562 /* The op_type check is needed to prevent a possible segfault
7563 * if the loop variable is undeclared and 'strict vars' is in
7564 * effect. This is illegal but is nonetheless parsed, so we
7565 * may reach this point with an OP_CONST where we're expecting
7566 * an OP_GV.
7567 */
7568 if (cUNOPx(sv)->op_first->op_type == OP_GV
7569 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
7570 iterpflags |= OPpITER_DEF;
7571 }
7572 else if (sv->op_type == OP_PADSV) { /* private variable */
7573 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
7574 padoff = sv->op_targ;
7575 sv->op_targ = 0;
7576 op_free(sv);
7577 sv = NULL;
7578 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX);
7579 }
7580 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN)
7581 NOOP;
7582 else
7583 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
7584 if (padoff) {
7585 PADNAME * const pn = PAD_COMPNAME(padoff);
7586 const char * const name = PadnamePV(pn);
7587
7588 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_')
7589 iterpflags |= OPpITER_DEF;
7590 }
7591 }
7592 else {
7593 sv = newGVOP(OP_GV, 0, PL_defgv);
7594 iterpflags |= OPpITER_DEF;
7595 }
7596
7597 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
7598 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), 1), OP_GREPSTART);
7599 iterflags |= OPf_STACKED;
7600 }
7601 else if (expr->op_type == OP_NULL &&
7602 (expr->op_flags & OPf_KIDS) &&
7603 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
7604 {
7605 /* Basically turn for($x..$y) into the same as for($x,$y), but we
7606 * set the STACKED flag to indicate that these values are to be
7607 * treated as min/max values by 'pp_enteriter'.
7608 */
7609 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
7610 LOGOP* const range = (LOGOP*) flip->op_first;
7611 OP* const left = range->op_first;
7612 OP* const right = OpSIBLING(left);
7613 LISTOP* listop;
7614
7615 range->op_flags &= ~OPf_KIDS;
7616 /* detach range's children */
7617 op_sibling_splice((OP*)range, NULL, -1, NULL);
7618
7619 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
7620 listop->op_first->op_next = range->op_next;
7621 left->op_next = range->op_other;
7622 right->op_next = (OP*)listop;
7623 listop->op_next = listop->op_first;
7624
7625 op_free(expr);
7626 expr = (OP*)(listop);
7627 op_null(expr);
7628 iterflags |= OPf_STACKED;
7629 }
7630 else {
7631 expr = op_lvalue(force_list(expr, 1), OP_GREPSTART);
7632 }
7633
7634 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags,
7635 op_append_elem(OP_LIST, list(expr),
7636 scalar(sv)));
7637 assert(!loop->op_next);
7638 /* for my $x () sets OPpLVAL_INTRO;
7639 * for our $x () sets OPpOUR_INTRO */
7640 loop->op_private = (U8)iterpflags;
7641 if (loop->op_slabbed
7642 && DIFF(loop, OpSLOT(loop)->opslot_next)
7643 < SIZE_TO_PSIZE(sizeof(LOOP)))
7644 {
7645 LOOP *tmp;
7646 NewOp(1234,tmp,1,LOOP);
7647 Copy(loop,tmp,1,LISTOP);
7648#ifdef PERL_OP_PARENT
7649 assert(loop->op_last->op_sibparent == (OP*)loop);
7650 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */
7651#endif
7652 S_op_destroy(aTHX_ (OP*)loop);
7653 loop = tmp;
7654 }
7655 else if (!loop->op_slabbed)
7656 {
7657 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
7658#ifdef PERL_OP_PARENT
7659 OpLASTSIB_set(loop->op_last, (OP*)loop);
7660#endif
7661 }
7662 loop->op_targ = padoff;
7663 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0);
7664 return wop;
7665}
7666
7667/*
7668=for apidoc Am|OP *|newLOOPEX|I32 type|OP *label
7669
7670Constructs, checks, and returns a loop-exiting op (such as C<goto>
7671or C<last>). C<type> is the opcode. C<label> supplies the parameter
7672determining the target of the op; it is consumed by this function and
7673becomes part of the constructed op tree.
7674
7675=cut
7676*/
7677
7678OP*
7679Perl_newLOOPEX(pTHX_ I32 type, OP *label)
7680{
7681 OP *o = NULL;
7682
7683 PERL_ARGS_ASSERT_NEWLOOPEX;
7684
7685 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
7686 || type == OP_CUSTOM);
7687
7688 if (type != OP_GOTO) {
7689 /* "last()" means "last" */
7690 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) {
7691 o = newOP(type, OPf_SPECIAL);
7692 }
7693 }
7694 else {
7695 /* Check whether it's going to be a goto &function */
7696 if (label->op_type == OP_ENTERSUB
7697 && !(label->op_flags & OPf_STACKED))
7698 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
7699 }
7700
7701 /* Check for a constant argument */
7702 if (label->op_type == OP_CONST) {
7703 SV * const sv = ((SVOP *)label)->op_sv;
7704 STRLEN l;
7705 const char *s = SvPV_const(sv,l);
7706 if (l == strlen(s)) {
7707 o = newPVOP(type,
7708 SvUTF8(((SVOP*)label)->op_sv),
7709 savesharedpv(
7710 SvPV_nolen_const(((SVOP*)label)->op_sv)));
7711 }
7712 }
7713
7714 /* If we have already created an op, we do not need the label. */
7715 if (o)
7716 op_free(label);
7717 else o = newUNOP(type, OPf_STACKED, label);
7718
7719 PL_hints |= HINT_BLOCK_SCOPE;
7720 return o;
7721}
7722
7723/* if the condition is a literal array or hash
7724 (or @{ ... } etc), make a reference to it.
7725 */
7726STATIC OP *
7727S_ref_array_or_hash(pTHX_ OP *cond)
7728{
7729 if (cond
7730 && (cond->op_type == OP_RV2AV
7731 || cond->op_type == OP_PADAV
7732 || cond->op_type == OP_RV2HV
7733 || cond->op_type == OP_PADHV))
7734
7735 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
7736
7737 else if(cond
7738 && (cond->op_type == OP_ASLICE
7739 || cond->op_type == OP_KVASLICE
7740 || cond->op_type == OP_HSLICE
7741 || cond->op_type == OP_KVHSLICE)) {
7742
7743 /* anonlist now needs a list from this op, was previously used in
7744 * scalar context */
7745 cond->op_flags &= ~(OPf_WANT_SCALAR | OPf_REF);
7746 cond->op_flags |= OPf_WANT_LIST;
7747
7748 return newANONLIST(op_lvalue(cond, OP_ANONLIST));
7749 }
7750
7751 else
7752 return cond;
7753}
7754
7755/* These construct the optree fragments representing given()
7756 and when() blocks.
7757
7758 entergiven and enterwhen are LOGOPs; the op_other pointer
7759 points up to the associated leave op. We need this so we
7760 can put it in the context and make break/continue work.
7761 (Also, of course, pp_enterwhen will jump straight to
7762 op_other if the match fails.)
7763 */
7764
7765STATIC OP *
7766S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
7767 I32 enter_opcode, I32 leave_opcode,
7768 PADOFFSET entertarg)
7769{
7770 dVAR;
7771 LOGOP *enterop;
7772 OP *o;
7773
7774 PERL_ARGS_ASSERT_NEWGIVWHENOP;
7775 PERL_UNUSED_ARG(entertarg); /* used to indicate targ of lexical $_ */
7776
7777 enterop = alloc_LOGOP(enter_opcode, block, NULL);
7778 enterop->op_targ = 0;
7779 enterop->op_private = 0;
7780
7781 o = newUNOP(leave_opcode, 0, (OP *) enterop);
7782
7783 if (cond) {
7784 /* prepend cond if we have one */
7785 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond));
7786
7787 o->op_next = LINKLIST(cond);
7788 cond->op_next = (OP *) enterop;
7789 }
7790 else {
7791 /* This is a default {} block */
7792 enterop->op_flags |= OPf_SPECIAL;
7793 o ->op_flags |= OPf_SPECIAL;
7794
7795 o->op_next = (OP *) enterop;
7796 }
7797
7798 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
7799 entergiven and enterwhen both
7800 use ck_null() */
7801
7802 enterop->op_next = LINKLIST(block);
7803 block->op_next = enterop->op_other = o;
7804
7805 return o;
7806}
7807
7808/* Does this look like a boolean operation? For these purposes
7809 a boolean operation is:
7810 - a subroutine call [*]
7811 - a logical connective
7812 - a comparison operator
7813 - a filetest operator, with the exception of -s -M -A -C
7814 - defined(), exists() or eof()
7815 - /$re/ or $foo =~ /$re/
7816
7817 [*] possibly surprising
7818 */
7819STATIC bool
7820S_looks_like_bool(pTHX_ const OP *o)
7821{
7822 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
7823
7824 switch(o->op_type) {
7825 case OP_OR:
7826 case OP_DOR:
7827 return looks_like_bool(cLOGOPo->op_first);
7828
7829 case OP_AND:
7830 {
7831 OP* sibl = OpSIBLING(cLOGOPo->op_first);
7832 ASSUME(sibl);
7833 return (
7834 looks_like_bool(cLOGOPo->op_first)
7835 && looks_like_bool(sibl));
7836 }
7837
7838 case OP_NULL:
7839 case OP_SCALAR:
7840 return (
7841 o->op_flags & OPf_KIDS
7842 && looks_like_bool(cUNOPo->op_first));
7843
7844 case OP_ENTERSUB:
7845
7846 case OP_NOT: case OP_XOR:
7847
7848 case OP_EQ: case OP_NE: case OP_LT:
7849 case OP_GT: case OP_LE: case OP_GE:
7850
7851 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
7852 case OP_I_GT: case OP_I_LE: case OP_I_GE:
7853
7854 case OP_SEQ: case OP_SNE: case OP_SLT:
7855 case OP_SGT: case OP_SLE: case OP_SGE:
7856
7857 case OP_SMARTMATCH:
7858
7859 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
7860 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
7861 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
7862 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
7863 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
7864 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
7865 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
7866 case OP_FTTEXT: case OP_FTBINARY:
7867
7868 case OP_DEFINED: case OP_EXISTS:
7869 case OP_MATCH: case OP_EOF:
7870
7871 case OP_FLOP:
7872
7873 return TRUE;
7874
7875 case OP_CONST:
7876 /* Detect comparisons that have been optimized away */
7877 if (cSVOPo->op_sv == &PL_sv_yes
7878 || cSVOPo->op_sv == &PL_sv_no)
7879
7880 return TRUE;
7881 else
7882 return FALSE;
7883
7884 /* FALLTHROUGH */
7885 default:
7886 return FALSE;
7887 }
7888}
7889
7890/*
7891=for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off
7892
7893Constructs, checks, and returns an op tree expressing a C<given> block.
7894C<cond> supplies the expression that will be locally assigned to a lexical
7895variable, and C<block> supplies the body of the C<given> construct; they
7896are consumed by this function and become part of the constructed op tree.
7897C<defsv_off> must be zero (it used to identity the pad slot of lexical $_).
7898
7899=cut
7900*/
7901
7902OP *
7903Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
7904{
7905 PERL_ARGS_ASSERT_NEWGIVENOP;
7906 PERL_UNUSED_ARG(defsv_off);
7907
7908 assert(!defsv_off);
7909 return newGIVWHENOP(
7910 ref_array_or_hash(cond),
7911 block,
7912 OP_ENTERGIVEN, OP_LEAVEGIVEN,
7913 0);
7914}
7915
7916/*
7917=for apidoc Am|OP *|newWHENOP|OP *cond|OP *block
7918
7919Constructs, checks, and returns an op tree expressing a C<when> block.
7920C<cond> supplies the test expression, and C<block> supplies the block
7921that will be executed if the test evaluates to true; they are consumed
7922by this function and become part of the constructed op tree. C<cond>
7923will be interpreted DWIMically, often as a comparison against C<$_>,
7924and may be null to generate a C<default> block.
7925
7926=cut
7927*/
7928
7929OP *
7930Perl_newWHENOP(pTHX_ OP *cond, OP *block)
7931{
7932 const bool cond_llb = (!cond || looks_like_bool(cond));
7933 OP *cond_op;
7934
7935 PERL_ARGS_ASSERT_NEWWHENOP;
7936
7937 if (cond_llb)
7938 cond_op = cond;
7939 else {
7940 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
7941 newDEFSVOP(),
7942 scalar(ref_array_or_hash(cond)));
7943 }
7944
7945 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
7946}
7947
7948/* must not conflict with SVf_UTF8 */
7949#define CV_CKPROTO_CURSTASH 0x1
7950
7951void
7952Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
7953 const STRLEN len, const U32 flags)
7954{
7955 SV *name = NULL, *msg;
7956 const char * cvp = SvROK(cv)
7957 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV
7958 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv))
7959 : ""
7960 : CvPROTO(cv);
7961 STRLEN clen = CvPROTOLEN(cv), plen = len;
7962
7963 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
7964
7965 if (p == NULL && cvp == NULL)
7966 return;
7967
7968 if (!ckWARN_d(WARN_PROTOTYPE))
7969 return;
7970
7971 if (p && cvp) {
7972 p = S_strip_spaces(aTHX_ p, &plen);
7973 cvp = S_strip_spaces(aTHX_ cvp, &clen);
7974 if ((flags & SVf_UTF8) == SvUTF8(cv)) {
7975 if (plen == clen && memEQ(cvp, p, plen))
7976 return;
7977 } else {
7978 if (flags & SVf_UTF8) {
7979 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0)
7980 return;
7981 }
7982 else {
7983 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0)
7984 return;
7985 }
7986 }
7987 }
7988
7989 msg = sv_newmortal();
7990
7991 if (gv)
7992 {
7993 if (isGV(gv))
7994 gv_efullname3(name = sv_newmortal(), gv, NULL);
7995 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&')
7996 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP);
7997 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) {
7998 name = sv_2mortal(newSVhek(HvNAME_HEK(PL_curstash)));
7999 sv_catpvs(name, "::");
8000 if (SvROK(gv)) {
8001 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV);
8002 assert (CvNAMED(SvRV_const(gv)));
8003 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv))));
8004 }
8005 else sv_catsv(name, (SV *)gv);
8006 }
8007 else name = (SV *)gv;
8008 }
8009 sv_setpvs(msg, "Prototype mismatch:");
8010 if (name)
8011 Perl_sv_catpvf(aTHX_ msg, " sub %" SVf, SVfARG(name));
8012 if (cvp)
8013 Perl_sv_catpvf(aTHX_ msg, " (%" UTF8f ")",
8014 UTF8fARG(SvUTF8(cv),clen,cvp)
8015 );
8016 else
8017 sv_catpvs(msg, ": none");
8018 sv_catpvs(msg, " vs ");
8019 if (p)
8020 Perl_sv_catpvf(aTHX_ msg, "(%" UTF8f ")", UTF8fARG(flags & SVf_UTF8,len,p));
8021 else
8022 sv_catpvs(msg, "none");
8023 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%" SVf, SVfARG(msg));
8024}
8025
8026static void const_sv_xsub(pTHX_ CV* cv);
8027static void const_av_xsub(pTHX_ CV* cv);
8028
8029/*
8030
8031=head1 Optree Manipulation Functions
8032
8033=for apidoc cv_const_sv
8034
8035If C<cv> is a constant sub eligible for inlining, returns the constant
8036value returned by the sub. Otherwise, returns C<NULL>.
8037
8038Constant subs can be created with C<newCONSTSUB> or as described in
8039L<perlsub/"Constant Functions">.
8040
8041=cut
8042*/
8043SV *
8044Perl_cv_const_sv(const CV *const cv)
8045{
8046 SV *sv;
8047 if (!cv)
8048 return NULL;
8049 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
8050 return NULL;
8051 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
8052 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL;
8053 return sv;
8054}
8055
8056SV *
8057Perl_cv_const_sv_or_av(const CV * const cv)
8058{
8059 if (!cv)
8060 return NULL;
8061 if (SvROK(cv)) return SvRV((SV *)cv);
8062 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
8063 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
8064}
8065
8066/* op_const_sv: examine an optree to determine whether it's in-lineable.
8067 * Can be called in 2 ways:
8068 *
8069 * !allow_lex
8070 * look for a single OP_CONST with attached value: return the value
8071 *
8072 * allow_lex && !CvCONST(cv);
8073 *
8074 * examine the clone prototype, and if contains only a single
8075 * OP_CONST, return the value; or if it contains a single PADSV ref-
8076 * erencing an outer lexical, turn on CvCONST to indicate the CV is
8077 * a candidate for "constizing" at clone time, and return NULL.
8078 */
8079
8080static SV *
8081S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex)
8082{
8083 SV *sv = NULL;
8084 bool padsv = FALSE;
8085
8086 assert(o);
8087 assert(cv);
8088
8089 for (; o; o = o->op_next) {
8090 const OPCODE type = o->op_type;
8091
8092 if (type == OP_NEXTSTATE || type == OP_LINESEQ
8093 || type == OP_NULL
8094 || type == OP_PUSHMARK)
8095 continue;
8096 if (type == OP_DBSTATE)
8097 continue;
8098 if (type == OP_LEAVESUB)
8099 break;
8100 if (sv)
8101 return NULL;
8102 if (type == OP_CONST && cSVOPo->op_sv)
8103 sv = cSVOPo->op_sv;
8104 else if (type == OP_UNDEF && !o->op_private) {
8105 sv = newSV(0);
8106 SAVEFREESV(sv);
8107 }
8108 else if (allow_lex && type == OP_PADSV) {
8109 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEt_OUTER)
8110 {
8111 sv = &PL_sv_undef; /* an arbitrary non-null value */
8112 padsv = TRUE;
8113 }
8114 else
8115 return NULL;
8116 }
8117 else {
8118 return NULL;
8119 }
8120 }
8121 if (padsv) {
8122 CvCONST_on(cv);
8123 return NULL;
8124 }
8125 return sv;
8126}
8127
8128static void
8129S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
8130 PADNAME * const name, SV ** const const_svp)
8131{
8132 assert (cv);
8133 assert (o || name);
8134 assert (const_svp);
8135 if (!block) {
8136 if (CvFLAGS(PL_compcv)) {
8137 /* might have had built-in attrs applied */
8138 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
8139 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
8140 && ckWARN(WARN_MISC))
8141 {
8142 /* protect against fatal warnings leaking compcv */
8143 SAVEFREESV(PL_compcv);
8144 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
8145 SvREFCNT_inc_simple_void_NN(PL_compcv);
8146 }
8147 CvFLAGS(cv) |=
8148 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
8149 & ~(CVf_LVALUE * pureperl));
8150 }
8151 return;
8152 }
8153
8154 /* redundant check for speed: */
8155 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
8156 const line_t oldline = CopLINE(PL_curcop);
8157 SV *namesv = o
8158 ? cSVOPo->op_sv
8159 : sv_2mortal(newSVpvn_utf8(
8160 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name)
8161 ));
8162 if (PL_parser && PL_parser->copline != NOLINE)
8163 /* This ensures that warnings are reported at the first
8164 line of a redefinition, not the last. */
8165 CopLINE_set(PL_curcop, PL_parser->copline);
8166 /* protect against fatal warnings leaking compcv */
8167 SAVEFREESV(PL_compcv);
8168 report_redefined_cv(namesv, cv, const_svp);
8169 SvREFCNT_inc_simple_void_NN(PL_compcv);
8170 CopLINE_set(PL_curcop, oldline);
8171 }
8172 SAVEFREESV(cv);
8173 return;
8174}
8175
8176CV *
8177Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
8178{
8179 CV **spot;
8180 SV **svspot;
8181 const char *ps;
8182 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
8183 U32 ps_utf8 = 0;
8184 CV *cv = NULL;
8185 CV *compcv = PL_compcv;
8186 SV *const_sv;
8187 PADNAME *name;
8188 PADOFFSET pax = o->op_targ;
8189 CV *outcv = CvOUTSIDE(PL_compcv);
8190 CV *clonee = NULL;
8191 HEK *hek = NULL;
8192 bool reusable = FALSE;
8193 OP *start = NULL;
8194#ifdef PERL_DEBUG_READONLY_OPS
8195 OPSLAB *slab = NULL;
8196#endif
8197
8198 PERL_ARGS_ASSERT_NEWMYSUB;
8199
8200 /* Find the pad slot for storing the new sub.
8201 We cannot use PL_comppad, as it is the pad owned by the new sub. We
8202 need to look in CvOUTSIDE and find the pad belonging to the enclos-
8203 ing sub. And then we need to dig deeper if this is a lexical from
8204 outside, as in:
8205 my sub foo; sub { sub foo { } }
8206 */
8207 redo:
8208 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
8209 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
8210 pax = PARENT_PAD_INDEX(name);
8211 outcv = CvOUTSIDE(outcv);
8212 assert(outcv);
8213 goto redo;
8214 }
8215 svspot =
8216 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
8217 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
8218 spot = (CV **)svspot;
8219
8220 if (!(PL_parser && PL_parser->error_count))
8221 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name));
8222
8223 if (proto) {
8224 assert(proto->op_type == OP_CONST);
8225 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
8226 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
8227 }
8228 else
8229 ps = NULL;
8230
8231 if (proto)
8232 SAVEFREEOP(proto);
8233 if (attrs)
8234 SAVEFREEOP(attrs);
8235
8236 if (PL_parser && PL_parser->error_count) {
8237 op_free(block);
8238 SvREFCNT_dec(PL_compcv);
8239 PL_compcv = 0;
8240 goto done;
8241 }
8242
8243 if (CvDEPTH(outcv) && CvCLONE(compcv)) {
8244 cv = *spot;
8245 svspot = (SV **)(spot = &clonee);
8246 }
8247 else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
8248 cv = *spot;
8249 else {
8250 assert (SvTYPE(*spot) == SVt_PVCV);
8251 if (CvNAMED(*spot))
8252 hek = CvNAME_HEK(*spot);
8253 else {
8254 dVAR;
8255 U32 hash;
8256 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
8257 CvNAME_HEK_set(*spot, hek =
8258 share_hek(
8259 PadnamePV(name)+1,
8260 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
8261 hash
8262 )
8263 );
8264 CvLEXICAL_on(*spot);
8265 }
8266 cv = PadnamePROTOCV(name);
8267 svspot = (SV **)(spot = &PadnamePROTOCV(name));
8268 }
8269
8270 if (block) {
8271 /* This makes sub {}; work as expected. */
8272 if (block->op_type == OP_STUB) {
8273 const line_t l = PL_parser->copline;
8274 op_free(block);
8275 block = newSTATEOP(0, NULL, 0);
8276 PL_parser->copline = l;
8277 }
8278 block = CvLVALUE(compcv)
8279 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv))
8280 ? newUNOP(OP_LEAVESUBLV, 0,
8281 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
8282 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
8283 start = LINKLIST(block);
8284 block->op_next = 0;
8285 if (ps && !*ps && !attrs && !CvLVALUE(compcv))
8286 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE);
8287 else
8288 const_sv = NULL;
8289 }
8290 else
8291 const_sv = NULL;
8292
8293 if (cv) {
8294 const bool exists = CvROOT(cv) || CvXSUB(cv);
8295
8296 /* if the subroutine doesn't exist and wasn't pre-declared
8297 * with a prototype, assume it will be AUTOLOADed,
8298 * skipping the prototype check
8299 */
8300 if (exists || SvPOK(cv))
8301 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len,
8302 ps_utf8);
8303 /* already defined? */
8304 if (exists) {
8305 S_already_defined(aTHX_ cv, block, NULL, name, &const_sv);
8306 if (block)
8307 cv = NULL;
8308 else {
8309 if (attrs)
8310 goto attrs;
8311 /* just a "sub foo;" when &foo is already defined */
8312 SAVEFREESV(compcv);
8313 goto done;
8314 }
8315 }
8316 else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
8317 cv = NULL;
8318 reusable = TRUE;
8319 }
8320 }
8321
8322 if (const_sv) {
8323 SvREFCNT_inc_simple_void_NN(const_sv);
8324 SvFLAGS(const_sv) |= SVs_PADTMP;
8325 if (cv) {
8326 assert(!CvROOT(cv) && !CvCONST(cv));
8327 cv_forget_slab(cv);
8328 }
8329 else {
8330 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8331 CvFILE_set_from_cop(cv, PL_curcop);
8332 CvSTASH_set(cv, PL_curstash);
8333 *spot = cv;
8334 }
8335 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
8336 CvXSUBANY(cv).any_ptr = const_sv;
8337 CvXSUB(cv) = const_sv_xsub;
8338 CvCONST_on(cv);
8339 CvISXSUB_on(cv);
8340 PoisonPADLIST(cv);
8341 CvFLAGS(cv) |= CvMETHOD(compcv);
8342 op_free(block);
8343 SvREFCNT_dec(compcv);
8344 PL_compcv = NULL;
8345 goto setname;
8346 }
8347
8348 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
8349 determine whether this sub definition is in the same scope as its
8350 declaration. If this sub definition is inside an inner named pack-
8351 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
8352 the package sub. So check PadnameOUTER(name) too.
8353 */
8354 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) {
8355 assert(!CvWEAKOUTSIDE(compcv));
8356 SvREFCNT_dec(CvOUTSIDE(compcv));
8357 CvWEAKOUTSIDE_on(compcv);
8358 }
8359 /* XXX else do we have a circular reference? */
8360
8361 if (cv) { /* must reuse cv in case stub is referenced elsewhere */
8362 /* transfer PL_compcv to cv */
8363 if (block) {
8364 cv_flags_t preserved_flags =
8365 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
8366 PADLIST *const temp_padl = CvPADLIST(cv);
8367 CV *const temp_cv = CvOUTSIDE(cv);
8368 const cv_flags_t other_flags =
8369 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
8370 OP * const cvstart = CvSTART(cv);
8371
8372 SvPOK_off(cv);
8373 CvFLAGS(cv) =
8374 CvFLAGS(compcv) | preserved_flags;
8375 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
8376 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
8377 CvPADLIST_set(cv, CvPADLIST(compcv));
8378 CvOUTSIDE(compcv) = temp_cv;
8379 CvPADLIST_set(compcv, temp_padl);
8380 CvSTART(cv) = CvSTART(compcv);
8381 CvSTART(compcv) = cvstart;
8382 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
8383 CvFLAGS(compcv) |= other_flags;
8384
8385 if (CvFILE(cv) && CvDYNFILE(cv)) {
8386 Safefree(CvFILE(cv));
8387 }
8388
8389 /* inner references to compcv must be fixed up ... */
8390 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
8391 if (PERLDB_INTER)/* Advice debugger on the new sub. */
8392 ++PL_sub_generation;
8393 }
8394 else {
8395 /* Might have had built-in attributes applied -- propagate them. */
8396 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
8397 }
8398 /* ... before we throw it away */
8399 SvREFCNT_dec(compcv);
8400 PL_compcv = compcv = cv;
8401 }
8402 else {
8403 cv = compcv;
8404 *spot = cv;
8405 }
8406
8407 setname:
8408 CvLEXICAL_on(cv);
8409 if (!CvNAME_HEK(cv)) {
8410 if (hek) (void)share_hek_hek(hek);
8411 else {
8412 dVAR;
8413 U32 hash;
8414 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
8415 hek = share_hek(PadnamePV(name)+1,
8416 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
8417 hash);
8418 }
8419 CvNAME_HEK_set(cv, hek);
8420 }
8421
8422 if (const_sv)
8423 goto clone;
8424
8425 CvFILE_set_from_cop(cv, PL_curcop);
8426 CvSTASH_set(cv, PL_curstash);
8427
8428 if (ps) {
8429 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
8430 if (ps_utf8)
8431 SvUTF8_on(MUTABLE_SV(cv));
8432 }
8433
8434 if (block) {
8435 /* If we assign an optree to a PVCV, then we've defined a
8436 * subroutine that the debugger could be able to set a breakpoint
8437 * in, so signal to pp_entereval that it should not throw away any
8438 * saved lines at scope exit. */
8439
8440 PL_breakable_sub_gen++;
8441 CvROOT(cv) = block;
8442 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8443 itself has a refcount. */
8444 CvSLABBED_off(cv);
8445 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8446#ifdef PERL_DEBUG_READONLY_OPS
8447 slab = (OPSLAB *)CvSTART(cv);
8448#endif
8449 S_process_optree(aTHX_ cv, block, start);
8450 }
8451
8452 attrs:
8453 if (attrs) {
8454 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8455 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
8456 }
8457
8458 if (block) {
8459 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8460 SV * const tmpstr = sv_newmortal();
8461 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8462 GV_ADDMULTI, SVt_PVHV);
8463 HV *hv;
8464 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8465 CopFILE(PL_curcop),
8466 (long)PL_subline,
8467 (long)CopLINE(PL_curcop));
8468 if (HvNAME_HEK(PL_curstash)) {
8469 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
8470 sv_catpvs(tmpstr, "::");
8471 }
8472 else
8473 sv_setpvs(tmpstr, "__ANON__::");
8474
8475 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
8476 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
8477 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8478 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8479 hv = GvHVn(db_postponed);
8480 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8481 CV * const pcv = GvCV(db_postponed);
8482 if (pcv) {
8483 dSP;
8484 PUSHMARK(SP);
8485 XPUSHs(tmpstr);
8486 PUTBACK;
8487 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8488 }
8489 }
8490 }
8491 }
8492
8493 clone:
8494 if (clonee) {
8495 assert(CvDEPTH(outcv));
8496 spot = (CV **)
8497 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
8498 if (reusable)
8499 cv_clone_into(clonee, *spot);
8500 else *spot = cv_clone(clonee);
8501 SvREFCNT_dec_NN(clonee);
8502 cv = *spot;
8503 }
8504
8505 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
8506 PADOFFSET depth = CvDEPTH(outcv);
8507 while (--depth) {
8508 SV *oldcv;
8509 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
8510 oldcv = *svspot;
8511 *svspot = SvREFCNT_inc_simple_NN(cv);
8512 SvREFCNT_dec(oldcv);
8513 }
8514 }
8515
8516 done:
8517 if (PL_parser)
8518 PL_parser->copline = NOLINE;
8519 LEAVE_SCOPE(floor);
8520#ifdef PERL_DEBUG_READONLY_OPS
8521 if (slab)
8522 Slab_to_ro(slab);
8523#endif
8524 op_free(o);
8525 return cv;
8526}
8527
8528
8529/* _x = extended */
8530CV *
8531Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
8532 OP *block, bool o_is_gv)
8533{
8534 GV *gv;
8535 const char *ps;
8536 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
8537 U32 ps_utf8 = 0;
8538 CV *cv = NULL; /* the previous CV with this name, if any */
8539 SV *const_sv;
8540 const bool ec = PL_parser && PL_parser->error_count;
8541 /* If the subroutine has no body, no attributes, and no builtin attributes
8542 then it's just a sub declaration, and we may be able to get away with
8543 storing with a placeholder scalar in the symbol table, rather than a
8544 full CV. If anything is present then it will take a full CV to
8545 store it. */
8546 const I32 gv_fetch_flags
8547 = ec ? GV_NOADD_NOINIT :
8548 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
8549 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
8550 STRLEN namlen = 0;
8551 const char * const name =
8552 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
8553 bool has_name;
8554 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
8555 bool evanescent = FALSE;
8556 OP *start = NULL;
8557#ifdef PERL_DEBUG_READONLY_OPS
8558 OPSLAB *slab = NULL;
8559#endif
8560
8561 if (o_is_gv) {
8562 gv = (GV*)o;
8563 o = NULL;
8564 has_name = TRUE;
8565 } else if (name) {
8566 /* Try to optimise and avoid creating a GV. Instead, the CV’s name
8567 hek and CvSTASH pointer together can imply the GV. If the name
8568 contains a package name, then GvSTASH(CvGV(cv)) may differ from
8569 CvSTASH, so forego the optimisation if we find any.
8570 Also, we may be called from load_module at run time, so
8571 PL_curstash (which sets CvSTASH) may not point to the stash the
8572 sub is stored in. */
8573 const I32 flags =
8574 ec ? GV_NOADD_NOINIT
8575 : PL_curstash != CopSTASH(PL_curcop)
8576 || memchr(name, ':', namlen) || memchr(name, '\'', namlen)
8577 ? gv_fetch_flags
8578 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL;
8579 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV);
8580 has_name = TRUE;
8581 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
8582 SV * const sv = sv_newmortal();
8583 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%" IVdf "]",
8584 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
8585 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
8586 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
8587 has_name = TRUE;
8588 } else if (PL_curstash) {
8589 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
8590 has_name = FALSE;
8591 } else {
8592 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
8593 has_name = FALSE;
8594 }
8595
8596 if (!ec) {
8597 if (isGV(gv)) {
8598 move_proto_attr(&proto, &attrs, gv);
8599 } else {
8600 assert(cSVOPo);
8601 move_proto_attr(&proto, &attrs, (GV *)cSVOPo->op_sv);
8602 }
8603 }
8604
8605 if (proto) {
8606 assert(proto->op_type == OP_CONST);
8607 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
8608 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
8609 }
8610 else
8611 ps = NULL;
8612
8613 if (o)
8614 SAVEFREEOP(o);
8615 if (proto)
8616 SAVEFREEOP(proto);
8617 if (attrs)
8618 SAVEFREEOP(attrs);
8619
8620 if (ec) {
8621 op_free(block);
8622
8623 if (name)
8624 SvREFCNT_dec(PL_compcv);
8625 else
8626 cv = PL_compcv;
8627
8628 PL_compcv = 0;
8629 if (name && block) {
8630 const char *s = strrchr(name, ':');
8631 s = s ? s+1 : name;
8632 if (strEQ(s, "BEGIN")) {
8633 if (PL_in_eval & EVAL_KEEPERR)
8634 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
8635 else {
8636 SV * const errsv = ERRSV;
8637 /* force display of errors found but not reported */
8638 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
8639 Perl_croak_nocontext("%" SVf, SVfARG(errsv));
8640 }
8641 }
8642 }
8643 goto done;
8644 }
8645
8646 if (!block && SvTYPE(gv) != SVt_PVGV) {
8647 /* If we are not defining a new sub and the existing one is not a
8648 full GV + CV... */
8649 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) {
8650 /* We are applying attributes to an existing sub, so we need it
8651 upgraded if it is a constant. */
8652 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)
8653 gv_init_pvn(gv, PL_curstash, name, namlen,
8654 SVf_UTF8 * name_is_utf8);
8655 }
8656 else { /* Maybe prototype now, and had at maximum
8657 a prototype or const/sub ref before. */
8658 if (SvTYPE(gv) > SVt_NULL) {
8659 cv_ckproto_len_flags((const CV *)gv,
8660 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
8661 ps_len, ps_utf8);
8662 }
8663
8664 if (!SvROK(gv)) {
8665 if (ps) {
8666 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
8667 if (ps_utf8)
8668 SvUTF8_on(MUTABLE_SV(gv));
8669 }
8670 else
8671 sv_setiv(MUTABLE_SV(gv), -1);
8672 }
8673
8674 SvREFCNT_dec(PL_compcv);
8675 cv = PL_compcv = NULL;
8676 goto done;
8677 }
8678 }
8679
8680 cv = (!name || (isGV(gv) && GvCVGEN(gv)))
8681 ? NULL
8682 : isGV(gv)
8683 ? GvCV(gv)
8684 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
8685 ? (CV *)SvRV(gv)
8686 : NULL;
8687
8688 if (block) {
8689 assert(PL_parser);
8690 /* This makes sub {}; work as expected. */
8691 if (block->op_type == OP_STUB) {
8692 const line_t l = PL_parser->copline;
8693 op_free(block);
8694 block = newSTATEOP(0, NULL, 0);
8695 PL_parser->copline = l;
8696 }
8697 block = CvLVALUE(PL_compcv)
8698 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)
8699 && (!isGV(gv) || !GvASSUMECV(gv)))
8700 ? newUNOP(OP_LEAVESUBLV, 0,
8701 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
8702 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
8703 start = LINKLIST(block);
8704 block->op_next = 0;
8705 if (ps && !*ps && !attrs && !CvLVALUE(PL_compcv))
8706 const_sv =
8707 S_op_const_sv(aTHX_ start, PL_compcv,
8708 cBOOL(CvCLONE(PL_compcv)));
8709 else
8710 const_sv = NULL;
8711 }
8712 else
8713 const_sv = NULL;
8714
8715 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) {
8716 cv_ckproto_len_flags((const CV *)gv,
8717 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
8718 ps_len, ps_utf8|CV_CKPROTO_CURSTASH);
8719 if (SvROK(gv)) {
8720 /* All the other code for sub redefinition warnings expects the
8721 clobbered sub to be a CV. Instead of making all those code
8722 paths more complex, just inline the RV version here. */
8723 const line_t oldline = CopLINE(PL_curcop);
8724 assert(IN_PERL_COMPILETIME);
8725 if (PL_parser && PL_parser->copline != NOLINE)
8726 /* This ensures that warnings are reported at the first
8727 line of a redefinition, not the last. */
8728 CopLINE_set(PL_curcop, PL_parser->copline);
8729 /* protect against fatal warnings leaking compcv */
8730 SAVEFREESV(PL_compcv);
8731
8732 if (ckWARN(WARN_REDEFINE)
8733 || ( ckWARN_d(WARN_REDEFINE)
8734 && ( !const_sv || SvRV(gv) == const_sv
8735 || sv_cmp(SvRV(gv), const_sv) ))) {
8736 assert(cSVOPo);
8737 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8738 "Constant subroutine %" SVf " redefined",
8739 SVfARG(cSVOPo->op_sv));
8740 }
8741
8742 SvREFCNT_inc_simple_void_NN(PL_compcv);
8743 CopLINE_set(PL_curcop, oldline);
8744 SvREFCNT_dec(SvRV(gv));
8745 }
8746 }
8747
8748 if (cv) {
8749 const bool exists = CvROOT(cv) || CvXSUB(cv);
8750
8751 /* if the subroutine doesn't exist and wasn't pre-declared
8752 * with a prototype, assume it will be AUTOLOADed,
8753 * skipping the prototype check
8754 */
8755 if (exists || SvPOK(cv))
8756 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
8757 /* already defined (or promised)? */
8758 if (exists || (isGV(gv) && GvASSUMECV(gv))) {
8759 S_already_defined(aTHX_ cv, block, o, NULL, &const_sv);
8760 if (block)
8761 cv = NULL;
8762 else {
8763 if (attrs)
8764 goto attrs;
8765 /* just a "sub foo;" when &foo is already defined */
8766 SAVEFREESV(PL_compcv);
8767 goto done;
8768 }
8769 }
8770 }
8771
8772 if (const_sv) {
8773 SvREFCNT_inc_simple_void_NN(const_sv);
8774 SvFLAGS(const_sv) |= SVs_PADTMP;
8775 if (cv) {
8776 assert(!CvROOT(cv) && !CvCONST(cv));
8777 cv_forget_slab(cv);
8778 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
8779 CvXSUBANY(cv).any_ptr = const_sv;
8780 CvXSUB(cv) = const_sv_xsub;
8781 CvCONST_on(cv);
8782 CvISXSUB_on(cv);
8783 PoisonPADLIST(cv);
8784 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
8785 }
8786 else {
8787 if (isGV(gv) || CvMETHOD(PL_compcv)) {
8788 if (name && isGV(gv))
8789 GvCV_set(gv, NULL);
8790 cv = newCONSTSUB_flags(
8791 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
8792 const_sv
8793 );
8794 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
8795 }
8796 else {
8797 if (!SvROK(gv)) {
8798 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
8799 prepare_SV_for_RV((SV *)gv);
8800 SvOK_off((SV *)gv);
8801 SvROK_on(gv);
8802 }
8803 SvRV_set(gv, const_sv);
8804 }
8805 }
8806 op_free(block);
8807 SvREFCNT_dec(PL_compcv);
8808 PL_compcv = NULL;
8809 goto done;
8810 }
8811
8812 /* don't copy new BEGIN CV to old BEGIN CV - RT #129099 */
8813 if (name && cv && *name == 'B' && strEQ(name, "BEGIN"))
8814 cv = NULL;
8815
8816 if (cv) { /* must reuse cv if autoloaded */
8817 /* transfer PL_compcv to cv */
8818 if (block) {
8819 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
8820 PADLIST *const temp_av = CvPADLIST(cv);
8821 CV *const temp_cv = CvOUTSIDE(cv);
8822 const cv_flags_t other_flags =
8823 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
8824 OP * const cvstart = CvSTART(cv);
8825
8826 if (isGV(gv)) {
8827 CvGV_set(cv,gv);
8828 assert(!CvCVGV_RC(cv));
8829 assert(CvGV(cv) == gv);
8830 }
8831 else {
8832 dVAR;
8833 U32 hash;
8834 PERL_HASH(hash, name, namlen);
8835 CvNAME_HEK_set(cv,
8836 share_hek(name,
8837 name_is_utf8
8838 ? -(SSize_t)namlen
8839 : (SSize_t)namlen,
8840 hash));
8841 }
8842
8843 SvPOK_off(cv);
8844 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs
8845 | CvNAMED(cv);
8846 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
8847 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
8848 CvPADLIST_set(cv,CvPADLIST(PL_compcv));
8849 CvOUTSIDE(PL_compcv) = temp_cv;
8850 CvPADLIST_set(PL_compcv, temp_av);
8851 CvSTART(cv) = CvSTART(PL_compcv);
8852 CvSTART(PL_compcv) = cvstart;
8853 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
8854 CvFLAGS(PL_compcv) |= other_flags;
8855
8856 if (CvFILE(cv) && CvDYNFILE(cv)) {
8857 Safefree(CvFILE(cv));
8858 }
8859 CvFILE_set_from_cop(cv, PL_curcop);
8860 CvSTASH_set(cv, PL_curstash);
8861
8862 /* inner references to PL_compcv must be fixed up ... */
8863 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
8864 if (PERLDB_INTER)/* Advice debugger on the new sub. */
8865 ++PL_sub_generation;
8866 }
8867 else {
8868 /* Might have had built-in attributes applied -- propagate them. */
8869 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
8870 }
8871 /* ... before we throw it away */
8872 SvREFCNT_dec(PL_compcv);
8873 PL_compcv = cv;
8874 }
8875 else {
8876 cv = PL_compcv;
8877 if (name && isGV(gv)) {
8878 GvCV_set(gv, cv);
8879 GvCVGEN(gv) = 0;
8880 if (HvENAME_HEK(GvSTASH(gv)))
8881 /* sub Foo::bar { (shift)+1 } */
8882 gv_method_changed(gv);
8883 }
8884 else if (name) {
8885 if (!SvROK(gv)) {
8886 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
8887 prepare_SV_for_RV((SV *)gv);
8888 SvOK_off((SV *)gv);
8889 SvROK_on(gv);
8890 }
8891 SvRV_set(gv, (SV *)cv);
8892 }
8893 }
8894
8895 if (!CvHASGV(cv)) {
8896 if (isGV(gv))
8897 CvGV_set(cv, gv);
8898 else {
8899 dVAR;
8900 U32 hash;
8901 PERL_HASH(hash, name, namlen);
8902 CvNAME_HEK_set(cv, share_hek(name,
8903 name_is_utf8
8904 ? -(SSize_t)namlen
8905 : (SSize_t)namlen,
8906 hash));
8907 }
8908 CvFILE_set_from_cop(cv, PL_curcop);
8909 CvSTASH_set(cv, PL_curstash);
8910 }
8911
8912 if (ps) {
8913 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
8914 if ( ps_utf8 )
8915 SvUTF8_on(MUTABLE_SV(cv));
8916 }
8917
8918 if (block) {
8919 /* If we assign an optree to a PVCV, then we've defined a
8920 * subroutine that the debugger could be able to set a breakpoint
8921 * in, so signal to pp_entereval that it should not throw away any
8922 * saved lines at scope exit. */
8923
8924 PL_breakable_sub_gen++;
8925 CvROOT(cv) = block;
8926 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8927 itself has a refcount. */
8928 CvSLABBED_off(cv);
8929 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8930#ifdef PERL_DEBUG_READONLY_OPS
8931 slab = (OPSLAB *)CvSTART(cv);
8932#endif
8933 S_process_optree(aTHX_ cv, block, start);
8934 }
8935
8936 attrs:
8937 if (attrs) {
8938 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8939 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv))
8940 ? GvSTASH(CvGV(cv))
8941 : PL_curstash;
8942 if (!name)
8943 SAVEFREESV(cv);
8944 apply_attrs(stash, MUTABLE_SV(cv), attrs);
8945 if (!name)
8946 SvREFCNT_inc_simple_void_NN(cv);
8947 }
8948
8949 if (block && has_name) {
8950 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8951 SV * const tmpstr = cv_name(cv,NULL,0);
8952 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8953 GV_ADDMULTI, SVt_PVHV);
8954 HV *hv;
8955 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8956 CopFILE(PL_curcop),
8957 (long)PL_subline,
8958 (long)CopLINE(PL_curcop));
8959 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8960 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8961 hv = GvHVn(db_postponed);
8962 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8963 CV * const pcv = GvCV(db_postponed);
8964 if (pcv) {
8965 dSP;
8966 PUSHMARK(SP);
8967 XPUSHs(tmpstr);
8968 PUTBACK;
8969 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8970 }
8971 }
8972 }
8973
8974 if (name) {
8975 if (PL_parser && PL_parser->error_count)
8976 clear_special_blocks(name, gv, cv);
8977 else
8978 evanescent =
8979 process_special_blocks(floor, name, gv, cv);
8980 }
8981 }
8982
8983 done:
8984 if (PL_parser)
8985 PL_parser->copline = NOLINE;
8986 LEAVE_SCOPE(floor);
8987
8988 if (!evanescent) {
8989#ifdef PERL_DEBUG_READONLY_OPS
8990 if (slab)
8991 Slab_to_ro(slab);
8992#endif
8993 if (cv && name && block && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv)))
8994 pad_add_weakref(cv);
8995 }
8996 return cv;
8997}
8998
8999STATIC void
9000S_clear_special_blocks(pTHX_ const char *const fullname,
9001 GV *const gv, CV *const cv) {
9002 const char *colon;
9003 const char *name;
9004
9005 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS;
9006
9007 colon = strrchr(fullname,':');
9008 name = colon ? colon + 1 : fullname;
9009
9010 if ((*name == 'B' && strEQ(name, "BEGIN"))
9011 || (*name == 'E' && strEQ(name, "END"))
9012 || (*name == 'U' && strEQ(name, "UNITCHECK"))
9013 || (*name == 'C' && strEQ(name, "CHECK"))
9014 || (*name == 'I' && strEQ(name, "INIT"))) {
9015 if (!isGV(gv)) {
9016 (void)CvGV(cv);
9017 assert(isGV(gv));
9018 }
9019 GvCV_set(gv, NULL);
9020 SvREFCNT_dec_NN(MUTABLE_SV(cv));
9021 }
9022}
9023
9024/* Returns true if the sub has been freed. */
9025STATIC bool
9026S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
9027 GV *const gv,
9028 CV *const cv)
9029{
9030 const char *const colon = strrchr(fullname,':');
9031 const char *const name = colon ? colon + 1 : fullname;
9032
9033 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
9034
9035 if (*name == 'B') {
9036 if (strEQ(name, "BEGIN")) {
9037 const I32 oldscope = PL_scopestack_ix;
9038 dSP;
9039 (void)CvGV(cv);
9040 if (floor) LEAVE_SCOPE(floor);
9041 ENTER;
9042 PUSHSTACKi(PERLSI_REQUIRE);
9043 SAVECOPFILE(&PL_compiling);
9044 SAVECOPLINE(&PL_compiling);
9045 SAVEVPTR(PL_curcop);
9046
9047 DEBUG_x( dump_sub(gv) );
9048 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
9049 GvCV_set(gv,0); /* cv has been hijacked */
9050 call_list(oldscope, PL_beginav);
9051
9052 POPSTACK;
9053 LEAVE;
9054 return !PL_savebegin;
9055 }
9056 else
9057 return FALSE;
9058 } else {
9059 if (*name == 'E') {
9060 if strEQ(name, "END") {
9061 DEBUG_x( dump_sub(gv) );
9062 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
9063 } else
9064 return FALSE;
9065 } else if (*name == 'U') {
9066 if (strEQ(name, "UNITCHECK")) {
9067 /* It's never too late to run a unitcheck block */
9068 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
9069 }
9070 else
9071 return FALSE;
9072 } else if (*name == 'C') {
9073 if (strEQ(name, "CHECK")) {
9074 if (PL_main_start)
9075 /* diag_listed_as: Too late to run %s block */
9076 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
9077 "Too late to run CHECK block");
9078 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
9079 }
9080 else
9081 return FALSE;
9082 } else if (*name == 'I') {
9083 if (strEQ(name, "INIT")) {
9084 if (PL_main_start)
9085 /* diag_listed_as: Too late to run %s block */
9086 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
9087 "Too late to run INIT block");
9088 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
9089 }
9090 else
9091 return FALSE;
9092 } else
9093 return FALSE;
9094 DEBUG_x( dump_sub(gv) );
9095 (void)CvGV(cv);
9096 GvCV_set(gv,0); /* cv has been hijacked */
9097 return FALSE;
9098 }
9099}
9100
9101/*
9102=for apidoc newCONSTSUB
9103
9104See L</newCONSTSUB_flags>.
9105
9106=cut
9107*/
9108
9109CV *
9110Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
9111{
9112 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
9113}
9114
9115/*
9116=for apidoc newCONSTSUB_flags
9117
9118Creates a constant sub equivalent to Perl S<C<sub FOO () { 123 }>> which is
9119eligible for inlining at compile-time.
9120
9121Currently, the only useful value for C<flags> is C<SVf_UTF8>.
9122
9123The newly created subroutine takes ownership of a reference to the passed in
9124SV.
9125
9126Passing C<NULL> for SV creates a constant sub equivalent to S<C<sub BAR () {}>>,
9127which won't be called if used as a destructor, but will suppress the overhead
9128of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at
9129compile time.)
9130
9131=cut
9132*/
9133
9134CV *
9135Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
9136 U32 flags, SV *sv)
9137{
9138 CV* cv;
9139 const char *const file = CopFILE(PL_curcop);
9140
9141 ENTER;
9142
9143 if (IN_PERL_RUNTIME) {
9144 /* at runtime, it's not safe to manipulate PL_curcop: it may be
9145 * an op shared between threads. Use a non-shared COP for our
9146 * dirty work */
9147 SAVEVPTR(PL_curcop);
9148 SAVECOMPILEWARNINGS();
9149 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
9150 PL_curcop = &PL_compiling;
9151 }
9152 SAVECOPLINE(PL_curcop);
9153 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
9154
9155 SAVEHINTS();
9156 PL_hints &= ~HINT_BLOCK_SCOPE;
9157
9158 if (stash) {
9159 SAVEGENERICSV(PL_curstash);
9160 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
9161 }
9162
9163 /* Protect sv against leakage caused by fatal warnings. */
9164 if (sv) SAVEFREESV(sv);
9165
9166 /* file becomes the CvFILE. For an XS, it's usually static storage,
9167 and so doesn't get free()d. (It's expected to be from the C pre-
9168 processor __FILE__ directive). But we need a dynamically allocated one,
9169 and we need it to get freed. */
9170 cv = newXS_len_flags(name, len,
9171 sv && SvTYPE(sv) == SVt_PVAV
9172 ? const_av_xsub
9173 : const_sv_xsub,
9174 file ? file : "", "",
9175 &sv, XS_DYNAMIC_FILENAME | flags);
9176 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
9177 CvCONST_on(cv);
9178
9179 LEAVE;
9180
9181 return cv;
9182}
9183
9184/*
9185=for apidoc U||newXS
9186
9187Used by C<xsubpp> to hook up XSUBs as Perl subs. C<filename> needs to be
9188static storage, as it is used directly as CvFILE(), without a copy being made.
9189
9190=cut
9191*/
9192
9193CV *
9194Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
9195{
9196 PERL_ARGS_ASSERT_NEWXS;
9197 return newXS_len_flags(
9198 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
9199 );
9200}
9201
9202CV *
9203Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
9204 const char *const filename, const char *const proto,
9205 U32 flags)
9206{
9207 PERL_ARGS_ASSERT_NEWXS_FLAGS;
9208 return newXS_len_flags(
9209 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
9210 );
9211}
9212
9213CV *
9214Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr)
9215{
9216 PERL_ARGS_ASSERT_NEWXS_DEFFILE;
9217 return newXS_len_flags(
9218 name, strlen(name), subaddr, NULL, NULL, NULL, 0
9219 );
9220}
9221
9222CV *
9223Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
9224 XSUBADDR_t subaddr, const char *const filename,
9225 const char *const proto, SV **const_svp,
9226 U32 flags)
9227{
9228 CV *cv;
9229 bool interleave = FALSE;
9230
9231 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
9232
9233 {
9234 GV * const gv = gv_fetchpvn(
9235 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
9236 name ? len : PL_curstash ? sizeof("__ANON__") - 1:
9237 sizeof("__ANON__::__ANON__") - 1,
9238 GV_ADDMULTI | flags, SVt_PVCV);
9239
9240 if ((cv = (name ? GvCV(gv) : NULL))) {
9241 if (GvCVGEN(gv)) {
9242 /* just a cached method */
9243 SvREFCNT_dec(cv);
9244 cv = NULL;
9245 }
9246 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
9247 /* already defined (or promised) */
9248 /* Redundant check that allows us to avoid creating an SV
9249 most of the time: */
9250 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
9251 report_redefined_cv(newSVpvn_flags(
9252 name,len,(flags&SVf_UTF8)|SVs_TEMP
9253 ),
9254 cv, const_svp);
9255 }
9256 interleave = TRUE;
9257 ENTER;
9258 SAVEFREESV(cv);
9259 cv = NULL;
9260 }
9261 }
9262
9263 if (cv) /* must reuse cv if autoloaded */
9264 cv_undef(cv);
9265 else {
9266 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
9267 if (name) {
9268 GvCV_set(gv,cv);
9269 GvCVGEN(gv) = 0;
9270 if (HvENAME_HEK(GvSTASH(gv)))
9271 gv_method_changed(gv); /* newXS */
9272 }
9273 }
9274
9275 CvGV_set(cv, gv);
9276 if(filename) {
9277 /* XSUBs can't be perl lang/perl5db.pl debugged
9278 if (PERLDB_LINE_OR_SAVESRC)
9279 (void)gv_fetchfile(filename); */
9280 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
9281 if (flags & XS_DYNAMIC_FILENAME) {
9282 CvDYNFILE_on(cv);
9283 CvFILE(cv) = savepv(filename);
9284 } else {
9285 /* NOTE: not copied, as it is expected to be an external constant string */
9286 CvFILE(cv) = (char *)filename;
9287 }
9288 } else {
9289 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename);
9290 CvFILE(cv) = (char*)PL_xsubfilename;
9291 }
9292 CvISXSUB_on(cv);
9293 CvXSUB(cv) = subaddr;
9294#ifndef PERL_IMPLICIT_CONTEXT
9295 CvHSCXT(cv) = &PL_stack_sp;
9296#else
9297 PoisonPADLIST(cv);
9298#endif
9299
9300 if (name)
9301 process_special_blocks(0, name, gv, cv);
9302 else
9303 CvANON_on(cv);
9304 } /* <- not a conditional branch */
9305
9306
9307 sv_setpv(MUTABLE_SV(cv), proto);
9308 if (interleave) LEAVE;
9309 return cv;
9310}
9311
9312CV *
9313Perl_newSTUB(pTHX_ GV *gv, bool fake)
9314{
9315 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
9316 GV *cvgv;
9317 PERL_ARGS_ASSERT_NEWSTUB;
9318 assert(!GvCVu(gv));
9319 GvCV_set(gv, cv);
9320 GvCVGEN(gv) = 0;
9321 if (!fake && GvSTASH(gv) && HvENAME_HEK(GvSTASH(gv)))
9322 gv_method_changed(gv);
9323 if (SvFAKE(gv)) {
9324 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
9325 SvFAKE_off(cvgv);
9326 }
9327 else cvgv = gv;
9328 CvGV_set(cv, cvgv);
9329 CvFILE_set_from_cop(cv, PL_curcop);
9330 CvSTASH_set(cv, PL_curstash);
9331 GvMULTI_on(gv);
9332 return cv;
9333}
9334
9335void
9336Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
9337{
9338 CV *cv;
9339 GV *gv;
9340 OP *root;
9341 OP *start;
9342
9343 if (PL_parser && PL_parser->error_count) {
9344 op_free(block);
9345 goto finish;
9346 }
9347
9348 gv = o
9349 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
9350 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
9351
9352 GvMULTI_on(gv);
9353 if ((cv = GvFORM(gv))) {
9354 if (ckWARN(WARN_REDEFINE)) {
9355 const line_t oldline = CopLINE(PL_curcop);
9356 if (PL_parser && PL_parser->copline != NOLINE)
9357 CopLINE_set(PL_curcop, PL_parser->copline);
9358 if (o) {
9359 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
9360 "Format %" SVf " redefined", SVfARG(cSVOPo->op_sv));
9361 } else {
9362 /* diag_listed_as: Format %s redefined */
9363 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
9364 "Format STDOUT redefined");
9365 }
9366 CopLINE_set(PL_curcop, oldline);
9367 }
9368 SvREFCNT_dec(cv);
9369 }
9370 cv = PL_compcv;
9371 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
9372 CvGV_set(cv, gv);
9373 CvFILE_set_from_cop(cv, PL_curcop);
9374
9375
9376 root = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
9377 CvROOT(cv) = root;
9378 start = LINKLIST(root);
9379 root->op_next = 0;
9380 S_process_optree(aTHX_ cv, root, start);
9381 cv_forget_slab(cv);
9382
9383 finish:
9384 op_free(o);
9385 if (PL_parser)
9386 PL_parser->copline = NOLINE;
9387 LEAVE_SCOPE(floor);
9388 PL_compiling.cop_seq = 0;
9389}
9390
9391OP *
9392Perl_newANONLIST(pTHX_ OP *o)
9393{
9394 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o);
9395}
9396
9397OP *
9398Perl_newANONHASH(pTHX_ OP *o)
9399{
9400 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o);
9401}
9402
9403OP *
9404Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
9405{
9406 return newANONATTRSUB(floor, proto, NULL, block);
9407}
9408
9409OP *
9410Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
9411{
9412 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block));
9413 OP * anoncode =
9414 newSVOP(OP_ANONCODE, 0,
9415 cv);
9416 if (CvANONCONST(cv))
9417 anoncode = newUNOP(OP_ANONCONST, 0,
9418 op_convert_list(OP_ENTERSUB,
9419 OPf_STACKED|OPf_WANT_SCALAR,
9420 anoncode));
9421 return newUNOP(OP_REFGEN, 0, anoncode);
9422}
9423
9424OP *
9425Perl_oopsAV(pTHX_ OP *o)
9426{
9427 dVAR;
9428
9429 PERL_ARGS_ASSERT_OOPSAV;
9430
9431 switch (o->op_type) {
9432 case OP_PADSV:
9433 case OP_PADHV:
9434 OpTYPE_set(o, OP_PADAV);
9435 return ref(o, OP_RV2AV);
9436
9437 case OP_RV2SV:
9438 case OP_RV2HV:
9439 OpTYPE_set(o, OP_RV2AV);
9440 ref(o, OP_RV2AV);
9441 break;
9442
9443 default:
9444 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
9445 break;
9446 }
9447 return o;
9448}
9449
9450OP *
9451Perl_oopsHV(pTHX_ OP *o)
9452{
9453 dVAR;
9454
9455 PERL_ARGS_ASSERT_OOPSHV;
9456
9457 switch (o->op_type) {
9458 case OP_PADSV:
9459 case OP_PADAV:
9460 OpTYPE_set(o, OP_PADHV);
9461 return ref(o, OP_RV2HV);
9462
9463 case OP_RV2SV:
9464 case OP_RV2AV:
9465 OpTYPE_set(o, OP_RV2HV);
9466 ref(o, OP_RV2HV);
9467 break;
9468
9469 default:
9470 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
9471 break;
9472 }
9473 return o;
9474}
9475
9476OP *
9477Perl_newAVREF(pTHX_ OP *o)
9478{
9479 dVAR;
9480
9481 PERL_ARGS_ASSERT_NEWAVREF;
9482
9483 if (o->op_type == OP_PADANY) {
9484 OpTYPE_set(o, OP_PADAV);
9485 return o;
9486 }
9487 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
9488 Perl_croak(aTHX_ "Can't use an array as a reference");
9489 }
9490 return newUNOP(OP_RV2AV, 0, scalar(o));
9491}
9492
9493OP *
9494Perl_newGVREF(pTHX_ I32 type, OP *o)
9495{
9496 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
9497 return newUNOP(OP_NULL, 0, o);
9498 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
9499}
9500
9501OP *
9502Perl_newHVREF(pTHX_ OP *o)
9503{
9504 dVAR;
9505
9506 PERL_ARGS_ASSERT_NEWHVREF;
9507
9508 if (o->op_type == OP_PADANY) {
9509 OpTYPE_set(o, OP_PADHV);
9510 return o;
9511 }
9512 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
9513 Perl_croak(aTHX_ "Can't use a hash as a reference");
9514 }
9515 return newUNOP(OP_RV2HV, 0, scalar(o));
9516}
9517
9518OP *
9519Perl_newCVREF(pTHX_ I32 flags, OP *o)
9520{
9521 if (o->op_type == OP_PADANY) {
9522 dVAR;
9523 OpTYPE_set(o, OP_PADCV);
9524 }
9525 return newUNOP(OP_RV2CV, flags, scalar(o));
9526}
9527
9528OP *
9529Perl_newSVREF(pTHX_ OP *o)
9530{
9531 dVAR;
9532
9533 PERL_ARGS_ASSERT_NEWSVREF;
9534
9535 if (o->op_type == OP_PADANY) {
9536 OpTYPE_set(o, OP_PADSV);
9537 scalar(o);
9538 return o;
9539 }
9540 return newUNOP(OP_RV2SV, 0, scalar(o));
9541}
9542
9543/* Check routines. See the comments at the top of this file for details
9544 * on when these are called */
9545
9546OP *
9547Perl_ck_anoncode(pTHX_ OP *o)
9548{
9549 PERL_ARGS_ASSERT_CK_ANONCODE;
9550
9551 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
9552 cSVOPo->op_sv = NULL;
9553 return o;
9554}
9555
9556static void
9557S_io_hints(pTHX_ OP *o)
9558{
9559#if O_BINARY != 0 || O_TEXT != 0
9560 HV * const table =
9561 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
9562 if (table) {
9563 SV **svp = hv_fetchs(table, "open_IN", FALSE);
9564 if (svp && *svp) {
9565 STRLEN len = 0;
9566 const char *d = SvPV_const(*svp, len);
9567 const I32 mode = mode_from_discipline(d, len);
9568 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
9569# if O_BINARY != 0
9570 if (mode & O_BINARY)
9571 o->op_private |= OPpOPEN_IN_RAW;
9572# endif
9573# if O_TEXT != 0
9574 if (mode & O_TEXT)
9575 o->op_private |= OPpOPEN_IN_CRLF;
9576# endif
9577 }
9578
9579 svp = hv_fetchs(table, "open_OUT", FALSE);
9580 if (svp && *svp) {
9581 STRLEN len = 0;
9582 const char *d = SvPV_const(*svp, len);
9583 const I32 mode = mode_from_discipline(d, len);
9584 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
9585# if O_BINARY != 0
9586 if (mode & O_BINARY)
9587 o->op_private |= OPpOPEN_OUT_RAW;
9588# endif
9589# if O_TEXT != 0
9590 if (mode & O_TEXT)
9591 o->op_private |= OPpOPEN_OUT_CRLF;
9592# endif
9593 }
9594 }
9595#else
9596 PERL_UNUSED_CONTEXT;
9597 PERL_UNUSED_ARG(o);
9598#endif
9599}
9600
9601OP *
9602Perl_ck_backtick(pTHX_ OP *o)
9603{
9604 GV *gv;
9605 OP *newop = NULL;
9606 OP *sibl;
9607 PERL_ARGS_ASSERT_CK_BACKTICK;
9608 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
9609 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first))
9610 && (gv = gv_override("readpipe",8)))
9611 {
9612 /* detach rest of siblings from o and its first child */
9613 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
9614 newop = S_new_entersubop(aTHX_ gv, sibl);
9615 }
9616 else if (!(o->op_flags & OPf_KIDS))
9617 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
9618 if (newop) {
9619 op_free(o);
9620 return newop;
9621 }
9622 S_io_hints(aTHX_ o);
9623 return o;
9624}
9625
9626OP *
9627Perl_ck_bitop(pTHX_ OP *o)
9628{
9629 PERL_ARGS_ASSERT_CK_BITOP;
9630
9631 o->op_private = (U8)(PL_hints & HINT_INTEGER);
9632
9633 if (o->op_type == OP_NBIT_OR || o->op_type == OP_SBIT_OR
9634 || o->op_type == OP_NBIT_XOR || o->op_type == OP_SBIT_XOR
9635 || o->op_type == OP_NBIT_AND || o->op_type == OP_SBIT_AND
9636 || o->op_type == OP_NCOMPLEMENT || o->op_type == OP_SCOMPLEMENT)
9637 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__BITWISE),
9638 "The bitwise feature is experimental");
9639 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
9640 && OP_IS_INFIX_BIT(o->op_type))
9641 {
9642 const OP * const left = cBINOPo->op_first;
9643 const OP * const right = OpSIBLING(left);
9644 if ((OP_IS_NUMCOMPARE(left->op_type) &&
9645 (left->op_flags & OPf_PARENS) == 0) ||
9646 (OP_IS_NUMCOMPARE(right->op_type) &&
9647 (right->op_flags & OPf_PARENS) == 0))
9648 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
9649 "Possible precedence problem on bitwise %s operator",
9650 o->op_type == OP_BIT_OR
9651 ||o->op_type == OP_NBIT_OR ? "|"
9652 : o->op_type == OP_BIT_AND
9653 ||o->op_type == OP_NBIT_AND ? "&"
9654 : o->op_type == OP_BIT_XOR
9655 ||o->op_type == OP_NBIT_XOR ? "^"
9656 : o->op_type == OP_SBIT_OR ? "|."
9657 : o->op_type == OP_SBIT_AND ? "&." : "^."
9658 );
9659 }
9660 return o;
9661}
9662
9663PERL_STATIC_INLINE bool
9664is_dollar_bracket(pTHX_ const OP * const o)
9665{
9666 const OP *kid;
9667 PERL_UNUSED_CONTEXT;
9668 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
9669 && (kid = cUNOPx(o)->op_first)
9670 && kid->op_type == OP_GV
9671 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
9672}
9673
9674OP *
9675Perl_ck_cmp(pTHX_ OP *o)
9676{
9677 PERL_ARGS_ASSERT_CK_CMP;
9678 if (ckWARN(WARN_SYNTAX)) {
9679 const OP *kid = cUNOPo->op_first;
9680 if (kid &&
9681 (
9682 ( is_dollar_bracket(aTHX_ kid)
9683 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST
9684 )
9685 || ( kid->op_type == OP_CONST
9686 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid)
9687 )
9688 )
9689 )
9690 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9691 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
9692 }
9693 return o;
9694}
9695
9696OP *
9697Perl_ck_concat(pTHX_ OP *o)
9698{
9699 const OP * const kid = cUNOPo->op_first;
9700
9701 PERL_ARGS_ASSERT_CK_CONCAT;
9702 PERL_UNUSED_CONTEXT;
9703
9704 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
9705 !(kUNOP->op_first->op_flags & OPf_MOD))
9706 o->op_flags |= OPf_STACKED;
9707 return o;
9708}
9709
9710OP *
9711Perl_ck_spair(pTHX_ OP *o)
9712{
9713 dVAR;
9714
9715 PERL_ARGS_ASSERT_CK_SPAIR;
9716
9717 if (o->op_flags & OPf_KIDS) {
9718 OP* newop;
9719 OP* kid;
9720 OP* kidkid;
9721 const OPCODE type = o->op_type;
9722 o = modkids(ck_fun(o), type);
9723 kid = cUNOPo->op_first;
9724 kidkid = kUNOP->op_first;
9725 newop = OpSIBLING(kidkid);
9726 if (newop) {
9727 const OPCODE type = newop->op_type;
9728 if (OpHAS_SIBLING(newop))
9729 return o;
9730 if (o->op_type == OP_REFGEN
9731 && ( type == OP_RV2CV
9732 || ( !(newop->op_flags & OPf_PARENS)
9733 && ( type == OP_RV2AV || type == OP_PADAV
9734 || type == OP_RV2HV || type == OP_PADHV))))
9735 NOOP; /* OK (allow srefgen for \@a and \%h) */
9736 else if (OP_GIMME(newop,0) != G_SCALAR)
9737 return o;
9738 }
9739 /* excise first sibling */
9740 op_sibling_splice(kid, NULL, 1, NULL);
9741 op_free(kidkid);
9742 }
9743 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
9744 * and OP_CHOMP into OP_SCHOMP */
9745 o->op_ppaddr = PL_ppaddr[++o->op_type];
9746 return ck_fun(o);
9747}
9748
9749OP *
9750Perl_ck_delete(pTHX_ OP *o)
9751{
9752 PERL_ARGS_ASSERT_CK_DELETE;
9753
9754 o = ck_fun(o);
9755 o->op_private = 0;
9756 if (o->op_flags & OPf_KIDS) {
9757 OP * const kid = cUNOPo->op_first;
9758 switch (kid->op_type) {
9759 case OP_ASLICE:
9760 o->op_flags |= OPf_SPECIAL;
9761 /* FALLTHROUGH */
9762 case OP_HSLICE:
9763 o->op_private |= OPpSLICE;
9764 break;
9765 case OP_AELEM:
9766 o->op_flags |= OPf_SPECIAL;
9767 /* FALLTHROUGH */
9768 case OP_HELEM:
9769 break;
9770 case OP_KVASLICE:
9771 Perl_croak(aTHX_ "delete argument is index/value array slice,"
9772 " use array slice");
9773 case OP_KVHSLICE:
9774 Perl_croak(aTHX_ "delete argument is key/value hash slice, use"
9775 " hash slice");
9776 default:
9777 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
9778 "element or slice");
9779 }
9780 if (kid->op_private & OPpLVAL_INTRO)
9781 o->op_private |= OPpLVAL_INTRO;
9782 op_null(kid);
9783 }
9784 return o;
9785}
9786
9787OP *
9788Perl_ck_eof(pTHX_ OP *o)
9789{
9790 PERL_ARGS_ASSERT_CK_EOF;
9791
9792 if (o->op_flags & OPf_KIDS) {
9793 OP *kid;
9794 if (cLISTOPo->op_first->op_type == OP_STUB) {
9795 OP * const newop
9796 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
9797 op_free(o);
9798 o = newop;
9799 }
9800 o = ck_fun(o);
9801 kid = cLISTOPo->op_first;
9802 if (kid->op_type == OP_RV2GV)
9803 kid->op_private |= OPpALLOW_FAKE;
9804 }
9805 return o;
9806}
9807
9808OP *
9809Perl_ck_eval(pTHX_ OP *o)
9810{
9811 dVAR;
9812
9813 PERL_ARGS_ASSERT_CK_EVAL;
9814
9815 PL_hints |= HINT_BLOCK_SCOPE;
9816 if (o->op_flags & OPf_KIDS) {
9817 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9818 assert(kid);
9819
9820 if (o->op_type == OP_ENTERTRY) {
9821 LOGOP *enter;
9822
9823 /* cut whole sibling chain free from o */
9824 op_sibling_splice(o, NULL, -1, NULL);
9825 op_free(o);
9826
9827 enter = alloc_LOGOP(OP_ENTERTRY, NULL, NULL);
9828
9829 /* establish postfix order */
9830 enter->op_next = (OP*)enter;
9831
9832 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
9833 OpTYPE_set(o, OP_LEAVETRY);
9834 enter->op_other = o;
9835 return o;
9836 }
9837 else {
9838 scalar((OP*)kid);
9839 S_set_haseval(aTHX);
9840 }
9841 }
9842 else {
9843 const U8 priv = o->op_private;
9844 op_free(o);
9845 /* the newUNOP will recursively call ck_eval(), which will handle
9846 * all the stuff at the end of this function, like adding
9847 * OP_HINTSEVAL
9848 */
9849 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
9850 }
9851 o->op_targ = (PADOFFSET)PL_hints;
9852 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
9853 if ((PL_hints & HINT_LOCALIZE_HH) != 0
9854 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
9855 /* Store a copy of %^H that pp_entereval can pick up. */
9856 OP *hhop = newSVOP(OP_HINTSEVAL, 0,
9857 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv))));
9858 /* append hhop to only child */
9859 op_sibling_splice(o, cUNOPo->op_first, 0, hhop);
9860
9861 o->op_private |= OPpEVAL_HAS_HH;
9862 }
9863 if (!(o->op_private & OPpEVAL_BYTES)
9864 && FEATURE_UNIEVAL_IS_ENABLED)
9865 o->op_private |= OPpEVAL_UNICODE;
9866 return o;
9867}
9868
9869OP *
9870Perl_ck_exec(pTHX_ OP *o)
9871{
9872 PERL_ARGS_ASSERT_CK_EXEC;
9873
9874 if (o->op_flags & OPf_STACKED) {
9875 OP *kid;
9876 o = ck_fun(o);
9877 kid = OpSIBLING(cUNOPo->op_first);
9878 if (kid->op_type == OP_RV2GV)
9879 op_null(kid);
9880 }
9881 else
9882 o = listkids(o);
9883 return o;
9884}
9885
9886OP *
9887Perl_ck_exists(pTHX_ OP *o)
9888{
9889 PERL_ARGS_ASSERT_CK_EXISTS;
9890
9891 o = ck_fun(o);
9892 if (o->op_flags & OPf_KIDS) {
9893 OP * const kid = cUNOPo->op_first;
9894 if (kid->op_type == OP_ENTERSUB) {
9895 (void) ref(kid, o->op_type);
9896 if (kid->op_type != OP_RV2CV
9897 && !(PL_parser && PL_parser->error_count))
9898 Perl_croak(aTHX_
9899 "exists argument is not a subroutine name");
9900 o->op_private |= OPpEXISTS_SUB;
9901 }
9902 else if (kid->op_type == OP_AELEM)
9903 o->op_flags |= OPf_SPECIAL;
9904 else if (kid->op_type != OP_HELEM)
9905 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
9906 "element or a subroutine");
9907 op_null(kid);
9908 }
9909 return o;
9910}
9911
9912OP *
9913Perl_ck_rvconst(pTHX_ OP *o)
9914{
9915 dVAR;
9916 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9917
9918 PERL_ARGS_ASSERT_CK_RVCONST;
9919
9920 o->op_private |= (PL_hints & HINT_STRICT_REFS);
9921
9922 if (kid->op_type == OP_CONST) {
9923 int iscv;
9924 GV *gv;
9925 SV * const kidsv = kid->op_sv;
9926
9927 /* Is it a constant from cv_const_sv()? */
9928 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) {
9929 return o;
9930 }
9931 if (SvTYPE(kidsv) == SVt_PVAV) return o;
9932 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
9933 const char *badthing;
9934 switch (o->op_type) {
9935 case OP_RV2SV:
9936 badthing = "a SCALAR";
9937 break;
9938 case OP_RV2AV:
9939 badthing = "an ARRAY";
9940 break;
9941 case OP_RV2HV:
9942 badthing = "a HASH";
9943 break;
9944 default:
9945 badthing = NULL;
9946 break;
9947 }
9948 if (badthing)
9949 Perl_croak(aTHX_
9950 "Can't use bareword (\"%" SVf "\") as %s ref while \"strict refs\" in use",
9951 SVfARG(kidsv), badthing);
9952 }
9953 /*
9954 * This is a little tricky. We only want to add the symbol if we
9955 * didn't add it in the lexer. Otherwise we get duplicate strict
9956 * warnings. But if we didn't add it in the lexer, we must at
9957 * least pretend like we wanted to add it even if it existed before,
9958 * or we get possible typo warnings. OPpCONST_ENTERED says
9959 * whether the lexer already added THIS instance of this symbol.
9960 */
9961 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0;
9962 gv = gv_fetchsv(kidsv,
9963 o->op_type == OP_RV2CV
9964 && o->op_private & OPpMAY_RETURN_CONSTANT
9965 ? GV_NOEXPAND
9966 : iscv | !(kid->op_private & OPpCONST_ENTERED),
9967 iscv
9968 ? SVt_PVCV
9969 : o->op_type == OP_RV2SV
9970 ? SVt_PV
9971 : o->op_type == OP_RV2AV
9972 ? SVt_PVAV
9973 : o->op_type == OP_RV2HV
9974 ? SVt_PVHV
9975 : SVt_PVGV);
9976 if (gv) {
9977 if (!isGV(gv)) {
9978 assert(iscv);
9979 assert(SvROK(gv));
9980 if (!(o->op_private & OPpMAY_RETURN_CONSTANT)
9981 && SvTYPE(SvRV(gv)) != SVt_PVCV)
9982 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV);
9983 }
9984 OpTYPE_set(kid, OP_GV);
9985 SvREFCNT_dec(kid->op_sv);
9986#ifdef USE_ITHREADS
9987 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
9988 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP));
9989 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY);
9990 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
9991 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
9992#else
9993 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
9994#endif
9995 kid->op_private = 0;
9996 /* FAKE globs in the symbol table cause weird bugs (#77810) */
9997 SvFAKE_off(gv);
9998 }
9999 }
10000 return o;
10001}
10002
10003OP *
10004Perl_ck_ftst(pTHX_ OP *o)
10005{
10006 dVAR;
10007 const I32 type = o->op_type;
10008
10009 PERL_ARGS_ASSERT_CK_FTST;
10010
10011 if (o->op_flags & OPf_REF) {
10012 NOOP;
10013 }
10014 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
10015 SVOP * const kid = (SVOP*)cUNOPo->op_first;
10016 const OPCODE kidtype = kid->op_type;
10017
10018 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
10019 && !kid->op_folded) {
10020 OP * const newop = newGVOP(type, OPf_REF,
10021 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
10022 op_free(o);
10023 return newop;
10024 }
10025
10026 if ((kidtype == OP_RV2AV || kidtype == OP_PADAV) && ckWARN(WARN_SYNTAX)) {
10027 SV *name = S_op_varname_subscript(aTHX_ (OP*)kid, 2);
10028 if (name) {
10029 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
10030 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s (did you want stat %" SVf "?)",
10031 array_passed_to_stat, name);
10032 }
10033 else {
10034 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
10035 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s", array_passed_to_stat);
10036 }
10037 }
10038 scalar((OP *) kid);
10039 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
10040 o->op_private |= OPpFT_ACCESS;
10041 if (type != OP_STAT && type != OP_LSTAT
10042 && PL_check[kidtype] == Perl_ck_ftst
10043 && kidtype != OP_STAT && kidtype != OP_LSTAT
10044 ) {
10045 o->op_private |= OPpFT_STACKED;
10046 kid->op_private |= OPpFT_STACKING;
10047 if (kidtype == OP_FTTTY && (
10048 !(kid->op_private & OPpFT_STACKED)
10049 || kid->op_private & OPpFT_AFTER_t
10050 ))
10051 o->op_private |= OPpFT_AFTER_t;
10052 }
10053 }
10054 else {
10055 op_free(o);
10056 if (type == OP_FTTTY)
10057 o = newGVOP(type, OPf_REF, PL_stdingv);
10058 else
10059 o = newUNOP(type, 0, newDEFSVOP());
10060 }
10061 return o;
10062}
10063
10064OP *
10065Perl_ck_fun(pTHX_ OP *o)
10066{
10067 const int type = o->op_type;
10068 I32 oa = PL_opargs[type] >> OASHIFT;
10069
10070 PERL_ARGS_ASSERT_CK_FUN;
10071
10072 if (o->op_flags & OPf_STACKED) {
10073 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
10074 oa &= ~OA_OPTIONAL;
10075 else
10076 return no_fh_allowed(o);
10077 }
10078
10079 if (o->op_flags & OPf_KIDS) {
10080 OP *prev_kid = NULL;
10081 OP *kid = cLISTOPo->op_first;
10082 I32 numargs = 0;
10083 bool seen_optional = FALSE;
10084
10085 if (kid->op_type == OP_PUSHMARK ||
10086 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
10087 {
10088 prev_kid = kid;
10089 kid = OpSIBLING(kid);
10090 }
10091 if (kid && kid->op_type == OP_COREARGS) {
10092 bool optional = FALSE;
10093 while (oa) {
10094 numargs++;
10095 if (oa & OA_OPTIONAL) optional = TRUE;
10096 oa = oa >> 4;
10097 }
10098 if (optional) o->op_private |= numargs;
10099 return o;
10100 }
10101
10102 while (oa) {
10103 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
10104 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) {
10105 kid = newDEFSVOP();
10106 /* append kid to chain */
10107 op_sibling_splice(o, prev_kid, 0, kid);
10108 }
10109 seen_optional = TRUE;
10110 }
10111 if (!kid) break;
10112
10113 numargs++;
10114 switch (oa & 7) {
10115 case OA_SCALAR:
10116 /* list seen where single (scalar) arg expected? */
10117 if (numargs == 1 && !(oa >> 4)
10118 && kid->op_type == OP_LIST && type != OP_SCALAR)
10119 {
10120 return too_many_arguments_pv(o,PL_op_desc[type], 0);
10121 }
10122 if (type != OP_DELETE) scalar(kid);
10123 break;
10124 case OA_LIST:
10125 if (oa < 16) {
10126 kid = 0;
10127 continue;
10128 }
10129 else
10130 list(kid);
10131 break;
10132 case OA_AVREF:
10133 if ((type == OP_PUSH || type == OP_UNSHIFT)
10134 && !OpHAS_SIBLING(kid))
10135 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
10136 "Useless use of %s with no values",
10137 PL_op_desc[type]);
10138
10139 if (kid->op_type == OP_CONST
10140 && ( !SvROK(cSVOPx_sv(kid))
10141 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
10142 )
10143 bad_type_pv(numargs, "array", o, kid);
10144 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) {
10145 yyerror_pv(Perl_form(aTHX_ "Experimental %s on scalar is now forbidden",
10146 PL_op_desc[type]), 0);
10147 }
10148 else {
10149 op_lvalue(kid, type);
10150 }
10151 break;
10152 case OA_HVREF:
10153 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
10154 bad_type_pv(numargs, "hash", o, kid);
10155 op_lvalue(kid, type);
10156 break;
10157 case OA_CVREF:
10158 {
10159 /* replace kid with newop in chain */
10160 OP * const newop =
10161 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0);
10162 newop->op_next = newop;
10163 kid = newop;
10164 }
10165 break;
10166 case OA_FILEREF:
10167 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
10168 if (kid->op_type == OP_CONST &&
10169 (kid->op_private & OPpCONST_BARE))
10170 {
10171 OP * const newop = newGVOP(OP_GV, 0,
10172 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
10173 /* replace kid with newop in chain */
10174 op_sibling_splice(o, prev_kid, 1, newop);
10175 op_free(kid);
10176 kid = newop;
10177 }
10178 else if (kid->op_type == OP_READLINE) {
10179 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
10180 bad_type_pv(numargs, "HANDLE", o, kid);
10181 }
10182 else {
10183 I32 flags = OPf_SPECIAL;
10184 I32 priv = 0;
10185 PADOFFSET targ = 0;
10186
10187 /* is this op a FH constructor? */
10188 if (is_handle_constructor(o,numargs)) {
10189 const char *name = NULL;
10190 STRLEN len = 0;
10191 U32 name_utf8 = 0;
10192 bool want_dollar = TRUE;
10193
10194 flags = 0;
10195 /* Set a flag to tell rv2gv to vivify
10196 * need to "prove" flag does not mean something
10197 * else already - NI-S 1999/05/07
10198 */
10199 priv = OPpDEREF;
10200 if (kid->op_type == OP_PADSV) {
10201 PADNAME * const pn
10202 = PAD_COMPNAME_SV(kid->op_targ);
10203 name = PadnamePV (pn);
10204 len = PadnameLEN(pn);
10205 name_utf8 = PadnameUTF8(pn);
10206 }
10207 else if (kid->op_type == OP_RV2SV
10208 && kUNOP->op_first->op_type == OP_GV)
10209 {
10210 GV * const gv = cGVOPx_gv(kUNOP->op_first);
10211 name = GvNAME(gv);
10212 len = GvNAMELEN(gv);
10213 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
10214 }
10215 else if (kid->op_type == OP_AELEM
10216 || kid->op_type == OP_HELEM)
10217 {
10218 OP *firstop;
10219 OP *op = ((BINOP*)kid)->op_first;
10220 name = NULL;
10221 if (op) {
10222 SV *tmpstr = NULL;
10223 const char * const a =
10224 kid->op_type == OP_AELEM ?
10225 "[]" : "{}";
10226 if (((op->op_type == OP_RV2AV) ||
10227 (op->op_type == OP_RV2HV)) &&
10228 (firstop = ((UNOP*)op)->op_first) &&
10229 (firstop->op_type == OP_GV)) {
10230 /* packagevar $a[] or $h{} */
10231 GV * const gv = cGVOPx_gv(firstop);
10232 if (gv)
10233 tmpstr =
10234 Perl_newSVpvf(aTHX_
10235 "%s%c...%c",
10236 GvNAME(gv),
10237 a[0], a[1]);
10238 }
10239 else if (op->op_type == OP_PADAV
10240 || op->op_type == OP_PADHV) {
10241 /* lexicalvar $a[] or $h{} */
10242 const char * const padname =
10243 PAD_COMPNAME_PV(op->op_targ);
10244 if (padname)
10245 tmpstr =
10246 Perl_newSVpvf(aTHX_
10247 "%s%c...%c",
10248 padname + 1,
10249 a[0], a[1]);
10250 }
10251 if (tmpstr) {
10252 name = SvPV_const(tmpstr, len);
10253 name_utf8 = SvUTF8(tmpstr);
10254 sv_2mortal(tmpstr);
10255 }
10256 }
10257 if (!name) {
10258 name = "__ANONIO__";
10259 len = 10;
10260 want_dollar = FALSE;
10261 }
10262 op_lvalue(kid, type);
10263 }
10264 if (name) {
10265 SV *namesv;
10266 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
10267 namesv = PAD_SVl(targ);
10268 if (want_dollar && *name != '$')
10269 sv_setpvs(namesv, "$");
10270 else
10271 SvPVCLEAR(namesv);
10272 sv_catpvn(namesv, name, len);
10273 if ( name_utf8 ) SvUTF8_on(namesv);
10274 }
10275 }
10276 scalar(kid);
10277 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid,
10278 OP_RV2GV, flags);
10279 kid->op_targ = targ;
10280 kid->op_private |= priv;
10281 }
10282 }
10283 scalar(kid);
10284 break;
10285 case OA_SCALARREF:
10286 if ((type == OP_UNDEF || type == OP_POS)
10287 && numargs == 1 && !(oa >> 4)
10288 && kid->op_type == OP_LIST)
10289 return too_many_arguments_pv(o,PL_op_desc[type], 0);
10290 op_lvalue(scalar(kid), type);
10291 break;
10292 }
10293 oa >>= 4;
10294 prev_kid = kid;
10295 kid = OpSIBLING(kid);
10296 }
10297 /* FIXME - should the numargs or-ing move after the too many
10298 * arguments check? */
10299 o->op_private |= numargs;
10300 if (kid)
10301 return too_many_arguments_pv(o,OP_DESC(o), 0);
10302 listkids(o);
10303 }
10304 else if (PL_opargs[type] & OA_DEFGV) {
10305 /* Ordering of these two is important to keep f_map.t passing. */
10306 op_free(o);
10307 return newUNOP(type, 0, newDEFSVOP());
10308 }
10309
10310 if (oa) {
10311 while (oa & OA_OPTIONAL)
10312 oa >>= 4;
10313 if (oa && oa != OA_LIST)
10314 return too_few_arguments_pv(o,OP_DESC(o), 0);
10315 }
10316 return o;
10317}
10318
10319OP *
10320Perl_ck_glob(pTHX_ OP *o)
10321{
10322 GV *gv;
10323
10324 PERL_ARGS_ASSERT_CK_GLOB;
10325
10326 o = ck_fun(o);
10327 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first))
10328 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
10329
10330 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
10331 {
10332 /* convert
10333 * glob
10334 * \ null - const(wildcard)
10335 * into
10336 * null
10337 * \ enter
10338 * \ list
10339 * \ mark - glob - rv2cv
10340 * | \ gv(CORE::GLOBAL::glob)
10341 * |
10342 * \ null - const(wildcard)
10343 */
10344 o->op_flags |= OPf_SPECIAL;
10345 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
10346 o = S_new_entersubop(aTHX_ gv, o);
10347 o = newUNOP(OP_NULL, 0, o);
10348 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
10349 return o;
10350 }
10351 else o->op_flags &= ~OPf_SPECIAL;
10352#if !defined(PERL_EXTERNAL_GLOB)
10353 if (!PL_globhook) {
10354 ENTER;
10355 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
10356 newSVpvs("File::Glob"), NULL, NULL, NULL);
10357 LEAVE;
10358 }
10359#endif /* !PERL_EXTERNAL_GLOB */
10360 gv = (GV *)newSV(0);
10361 gv_init(gv, 0, "", 0, 0);
10362 gv_IOadd(gv);
10363 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
10364 SvREFCNT_dec_NN(gv); /* newGVOP increased it */
10365 scalarkids(o);
10366 return o;
10367}
10368
10369OP *
10370Perl_ck_grep(pTHX_ OP *o)
10371{
10372 LOGOP *gwop;
10373 OP *kid;
10374 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
10375
10376 PERL_ARGS_ASSERT_CK_GREP;
10377
10378 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
10379
10380 if (o->op_flags & OPf_STACKED) {
10381 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first;
10382 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
10383 return no_fh_allowed(o);
10384 o->op_flags &= ~OPf_STACKED;
10385 }
10386 kid = OpSIBLING(cLISTOPo->op_first);
10387 if (type == OP_MAPWHILE)
10388 list(kid);
10389 else
10390 scalar(kid);
10391 o = ck_fun(o);
10392 if (PL_parser && PL_parser->error_count)
10393 return o;
10394 kid = OpSIBLING(cLISTOPo->op_first);
10395 if (kid->op_type != OP_NULL)
10396 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
10397 kid = kUNOP->op_first;
10398
10399 gwop = alloc_LOGOP(type, o, LINKLIST(kid));
10400 kid->op_next = (OP*)gwop;
10401 o->op_private = gwop->op_private = 0;
10402 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
10403
10404 kid = OpSIBLING(cLISTOPo->op_first);
10405 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid))
10406 op_lvalue(kid, OP_GREPSTART);
10407
10408 return (OP*)gwop;
10409}
10410
10411OP *
10412Perl_ck_index(pTHX_ OP *o)
10413{
10414 PERL_ARGS_ASSERT_CK_INDEX;
10415
10416 if (o->op_flags & OPf_KIDS) {
10417 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10418 if (kid)
10419 kid = OpSIBLING(kid); /* get past "big" */
10420 if (kid && kid->op_type == OP_CONST) {
10421 const bool save_taint = TAINT_get;
10422 SV *sv = kSVOP->op_sv;
10423 if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) {
10424 sv = newSV(0);
10425 sv_copypv(sv, kSVOP->op_sv);
10426 SvREFCNT_dec_NN(kSVOP->op_sv);
10427 kSVOP->op_sv = sv;
10428 }
10429 if (SvOK(sv)) fbm_compile(sv, 0);
10430 TAINT_set(save_taint);
10431#ifdef NO_TAINT_SUPPORT
10432 PERL_UNUSED_VAR(save_taint);
10433#endif
10434 }
10435 }
10436 return ck_fun(o);
10437}
10438
10439OP *
10440Perl_ck_lfun(pTHX_ OP *o)
10441{
10442 const OPCODE type = o->op_type;
10443
10444 PERL_ARGS_ASSERT_CK_LFUN;
10445
10446 return modkids(ck_fun(o), type);
10447}
10448
10449OP *
10450Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
10451{
10452 PERL_ARGS_ASSERT_CK_DEFINED;
10453
10454 if ((o->op_flags & OPf_KIDS)) {
10455 switch (cUNOPo->op_first->op_type) {
10456 case OP_RV2AV:
10457 case OP_PADAV:
10458 Perl_croak(aTHX_ "Can't use 'defined(@array)'"
10459 " (Maybe you should just omit the defined()?)");
10460 NOT_REACHED; /* NOTREACHED */
10461 break;
10462 case OP_RV2HV:
10463 case OP_PADHV:
10464 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'"
10465 " (Maybe you should just omit the defined()?)");
10466 NOT_REACHED; /* NOTREACHED */
10467 break;
10468 default:
10469 /* no warning */
10470 break;
10471 }
10472 }
10473 return ck_rfun(o);
10474}
10475
10476OP *
10477Perl_ck_readline(pTHX_ OP *o)
10478{
10479 PERL_ARGS_ASSERT_CK_READLINE;
10480
10481 if (o->op_flags & OPf_KIDS) {
10482 OP *kid = cLISTOPo->op_first;
10483 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
10484 }
10485 else {
10486 OP * const newop
10487 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
10488 op_free(o);
10489 return newop;
10490 }
10491 return o;
10492}
10493
10494OP *
10495Perl_ck_rfun(pTHX_ OP *o)
10496{
10497 const OPCODE type = o->op_type;
10498
10499 PERL_ARGS_ASSERT_CK_RFUN;
10500
10501 return refkids(ck_fun(o), type);
10502}
10503
10504OP *
10505Perl_ck_listiob(pTHX_ OP *o)
10506{
10507 OP *kid;
10508
10509 PERL_ARGS_ASSERT_CK_LISTIOB;
10510
10511 kid = cLISTOPo->op_first;
10512 if (!kid) {
10513 o = force_list(o, 1);
10514 kid = cLISTOPo->op_first;
10515 }
10516 if (kid->op_type == OP_PUSHMARK)
10517 kid = OpSIBLING(kid);
10518 if (kid && o->op_flags & OPf_STACKED)
10519 kid = OpSIBLING(kid);
10520 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */
10521 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
10522 && !kid->op_folded) {
10523 o->op_flags |= OPf_STACKED; /* make it a filehandle */
10524 scalar(kid);
10525 /* replace old const op with new OP_RV2GV parent */
10526 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first,
10527 OP_RV2GV, OPf_REF);
10528 kid = OpSIBLING(kid);
10529 }
10530 }
10531
10532 if (!kid)
10533 op_append_elem(o->op_type, o, newDEFSVOP());
10534
10535 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
10536 return listkids(o);
10537}
10538
10539OP *
10540Perl_ck_smartmatch(pTHX_ OP *o)
10541{
10542 dVAR;
10543 PERL_ARGS_ASSERT_CK_SMARTMATCH;
10544 if (0 == (o->op_flags & OPf_SPECIAL)) {
10545 OP *first = cBINOPo->op_first;
10546 OP *second = OpSIBLING(first);
10547
10548 /* Implicitly take a reference to an array or hash */
10549
10550 /* remove the original two siblings, then add back the
10551 * (possibly different) first and second sibs.
10552 */
10553 op_sibling_splice(o, NULL, 1, NULL);
10554 op_sibling_splice(o, NULL, 1, NULL);
10555 first = ref_array_or_hash(first);
10556 second = ref_array_or_hash(second);
10557 op_sibling_splice(o, NULL, 0, second);
10558 op_sibling_splice(o, NULL, 0, first);
10559
10560 /* Implicitly take a reference to a regular expression */
10561 if (first->op_type == OP_MATCH) {
10562 OpTYPE_set(first, OP_QR);
10563 }
10564 if (second->op_type == OP_MATCH) {
10565 OpTYPE_set(second, OP_QR);
10566 }
10567 }
10568
10569 return o;
10570}
10571
10572
10573static OP *
10574S_maybe_targlex(pTHX_ OP *o)
10575{
10576 OP * const kid = cLISTOPo->op_first;
10577 /* has a disposable target? */
10578 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
10579 && !(kid->op_flags & OPf_STACKED)
10580 /* Cannot steal the second time! */
10581 && !(kid->op_private & OPpTARGET_MY)
10582 )
10583 {
10584 OP * const kkid = OpSIBLING(kid);
10585
10586 /* Can just relocate the target. */
10587 if (kkid && kkid->op_type == OP_PADSV
10588 && (!(kkid->op_private & OPpLVAL_INTRO)
10589 || kkid->op_private & OPpPAD_STATE))
10590 {
10591 kid->op_targ = kkid->op_targ;
10592 kkid->op_targ = 0;
10593 /* Now we do not need PADSV and SASSIGN.
10594 * Detach kid and free the rest. */
10595 op_sibling_splice(o, NULL, 1, NULL);
10596 op_free(o);
10597 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
10598 return kid;
10599 }
10600 }
10601 return o;
10602}
10603
10604OP *
10605Perl_ck_sassign(pTHX_ OP *o)
10606{
10607 dVAR;
10608 OP * const kid = cBINOPo->op_first;
10609
10610 PERL_ARGS_ASSERT_CK_SASSIGN;
10611
10612 if (OpHAS_SIBLING(kid)) {
10613 OP *kkid = OpSIBLING(kid);
10614 /* For state variable assignment with attributes, kkid is a list op
10615 whose op_last is a padsv. */
10616 if ((kkid->op_type == OP_PADSV ||
10617 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
10618 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
10619 )
10620 )
10621 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
10622 == (OPpLVAL_INTRO|OPpPAD_STATE)) {
10623 const PADOFFSET target = kkid->op_targ;
10624 OP *const other = newOP(OP_PADSV,
10625 kkid->op_flags
10626 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
10627 OP *const first = newOP(OP_NULL, 0);
10628 OP *const nullop =
10629 newCONDOP(0, first, o, other);
10630 /* XXX targlex disabled for now; see ticket #124160
10631 newCONDOP(0, first, S_maybe_targlex(aTHX_ o), other);
10632 */
10633 OP *const condop = first->op_next;
10634
10635 OpTYPE_set(condop, OP_ONCE);
10636 other->op_targ = target;
10637 nullop->op_flags |= OPf_WANT_SCALAR;
10638
10639 /* Store the initializedness of state vars in a separate
10640 pad entry. */
10641 condop->op_targ =
10642 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0);
10643 /* hijacking PADSTALE for uninitialized state variables */
10644 SvPADSTALE_on(PAD_SVl(condop->op_targ));
10645
10646 return nullop;
10647 }
10648 }
10649 return S_maybe_targlex(aTHX_ o);
10650}
10651
10652OP *
10653Perl_ck_match(pTHX_ OP *o)
10654{
10655 PERL_UNUSED_CONTEXT;
10656 PERL_ARGS_ASSERT_CK_MATCH;
10657
10658 return o;
10659}
10660
10661OP *
10662Perl_ck_method(pTHX_ OP *o)
10663{
10664 SV *sv, *methsv, *rclass;
10665 const char* method;
10666 char* compatptr;
10667 int utf8;
10668 STRLEN len, nsplit = 0, i;
10669 OP* new_op;
10670 OP * const kid = cUNOPo->op_first;
10671
10672 PERL_ARGS_ASSERT_CK_METHOD;
10673 if (kid->op_type != OP_CONST) return o;
10674
10675 sv = kSVOP->op_sv;
10676
10677 /* replace ' with :: */
10678 while ((compatptr = strchr(SvPVX(sv), '\''))) {
10679 *compatptr = ':';
10680 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1);
10681 }
10682
10683 method = SvPVX_const(sv);
10684 len = SvCUR(sv);
10685 utf8 = SvUTF8(sv) ? -1 : 1;
10686
10687 for (i = len - 1; i > 0; --i) if (method[i] == ':') {
10688 nsplit = i+1;
10689 break;
10690 }
10691
10692 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0);
10693
10694 if (!nsplit) { /* $proto->method() */
10695 op_free(o);
10696 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv);
10697 }
10698
10699 if (nsplit == 7 && memEQ(method, "SUPER::", nsplit)) { /* $proto->SUPER::method() */
10700 op_free(o);
10701 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv);
10702 }
10703
10704 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */
10705 if (nsplit >= 9 && strnEQ(method+nsplit-9, "::SUPER::", 9)) {
10706 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0);
10707 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv);
10708 } else {
10709 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0);
10710 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv);
10711 }
10712#ifdef USE_ITHREADS
10713 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ);
10714#else
10715 cMETHOPx(new_op)->op_rclass_sv = rclass;
10716#endif
10717 op_free(o);
10718 return new_op;
10719}
10720
10721OP *
10722Perl_ck_null(pTHX_ OP *o)
10723{
10724 PERL_ARGS_ASSERT_CK_NULL;
10725 PERL_UNUSED_CONTEXT;
10726 return o;
10727}
10728
10729OP *
10730Perl_ck_open(pTHX_ OP *o)
10731{
10732 PERL_ARGS_ASSERT_CK_OPEN;
10733
10734 S_io_hints(aTHX_ o);
10735 {
10736 /* In case of three-arg dup open remove strictness
10737 * from the last arg if it is a bareword. */
10738 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
10739 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
10740 OP *oa;
10741 const char *mode;
10742
10743 if ((last->op_type == OP_CONST) && /* The bareword. */
10744 (last->op_private & OPpCONST_BARE) &&
10745 (last->op_private & OPpCONST_STRICT) &&
10746 (oa = OpSIBLING(first)) && /* The fh. */
10747 (oa = OpSIBLING(oa)) && /* The mode. */
10748 (oa->op_type == OP_CONST) &&
10749 SvPOK(((SVOP*)oa)->op_sv) &&
10750 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
10751 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
10752 (last == OpSIBLING(oa))) /* The bareword. */
10753 last->op_private &= ~OPpCONST_STRICT;
10754 }
10755 return ck_fun(o);
10756}
10757
10758OP *
10759Perl_ck_prototype(pTHX_ OP *o)
10760{
10761 PERL_ARGS_ASSERT_CK_PROTOTYPE;
10762 if (!(o->op_flags & OPf_KIDS)) {
10763 op_free(o);
10764 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP());
10765 }
10766 return o;
10767}
10768
10769OP *
10770Perl_ck_refassign(pTHX_ OP *o)
10771{
10772 OP * const right = cLISTOPo->op_first;
10773 OP * const left = OpSIBLING(right);
10774 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first;
10775 bool stacked = 0;
10776
10777 PERL_ARGS_ASSERT_CK_REFASSIGN;
10778 assert (left);
10779 assert (left->op_type == OP_SREFGEN);
10780
10781 o->op_private = 0;
10782 /* we use OPpPAD_STATE in refassign to mean either of those things,
10783 * and the code assumes the two flags occupy the same bit position
10784 * in the various ops below */
10785 assert(OPpPAD_STATE == OPpOUR_INTRO);
10786
10787 switch (varop->op_type) {
10788 case OP_PADAV:
10789 o->op_private |= OPpLVREF_AV;
10790 goto settarg;
10791 case OP_PADHV:
10792 o->op_private |= OPpLVREF_HV;
10793 /* FALLTHROUGH */
10794 case OP_PADSV:
10795 settarg:
10796 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
10797 o->op_targ = varop->op_targ;
10798 varop->op_targ = 0;
10799 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
10800 break;
10801
10802 case OP_RV2AV:
10803 o->op_private |= OPpLVREF_AV;
10804 goto checkgv;
10805 NOT_REACHED; /* NOTREACHED */
10806 case OP_RV2HV:
10807 o->op_private |= OPpLVREF_HV;
10808 /* FALLTHROUGH */
10809 case OP_RV2SV:
10810 checkgv:
10811 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO));
10812 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad;
10813 detach_and_stack:
10814 /* Point varop to its GV kid, detached. */
10815 varop = op_sibling_splice(varop, NULL, -1, NULL);
10816 stacked = TRUE;
10817 break;
10818 case OP_RV2CV: {
10819 OP * const kidparent =
10820 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first);
10821 OP * const kid = cUNOPx(kidparent)->op_first;
10822 o->op_private |= OPpLVREF_CV;
10823 if (kid->op_type == OP_GV) {
10824 varop = kidparent;
10825 goto detach_and_stack;
10826 }
10827 if (kid->op_type != OP_PADCV) goto bad;
10828 o->op_targ = kid->op_targ;
10829 kid->op_targ = 0;
10830 break;
10831 }
10832 case OP_AELEM:
10833 case OP_HELEM:
10834 o->op_private |= (varop->op_private & OPpLVAL_INTRO);
10835 o->op_private |= OPpLVREF_ELEM;
10836 op_null(varop);
10837 stacked = TRUE;
10838 /* Detach varop. */
10839 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL);
10840 break;
10841 default:
10842 bad:
10843 /* diag_listed_as: Can't modify reference to %s in %s assignment */
10844 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar "
10845 "assignment",
10846 OP_DESC(varop)));
10847 return o;
10848 }
10849 if (!FEATURE_REFALIASING_IS_ENABLED)
10850 Perl_croak(aTHX_
10851 "Experimental aliasing via reference not enabled");
10852 Perl_ck_warner_d(aTHX_
10853 packWARN(WARN_EXPERIMENTAL__REFALIASING),
10854 "Aliasing via reference is experimental");
10855 if (stacked) {
10856 o->op_flags |= OPf_STACKED;
10857 op_sibling_splice(o, right, 1, varop);
10858 }
10859 else {
10860 o->op_flags &=~ OPf_STACKED;
10861 op_sibling_splice(o, right, 1, NULL);
10862 }
10863 op_free(left);
10864 return o;
10865}
10866
10867OP *
10868Perl_ck_repeat(pTHX_ OP *o)
10869{
10870 PERL_ARGS_ASSERT_CK_REPEAT;
10871
10872 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
10873 OP* kids;
10874 o->op_private |= OPpREPEAT_DOLIST;
10875 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */
10876 kids = force_list(kids, 1); /* promote it to a list */
10877 op_sibling_splice(o, NULL, 0, kids); /* and add back */
10878 }
10879 else
10880 scalar(o);
10881 return o;
10882}
10883
10884OP *
10885Perl_ck_require(pTHX_ OP *o)
10886{
10887 GV* gv;
10888
10889 PERL_ARGS_ASSERT_CK_REQUIRE;
10890
10891 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
10892 SVOP * const kid = (SVOP*)cUNOPo->op_first;
10893 HEK *hek;
10894 U32 hash;
10895 char *s;
10896 STRLEN len;
10897 if (kid->op_type == OP_CONST) {
10898 SV * const sv = kid->op_sv;
10899 U32 const was_readonly = SvREADONLY(sv);
10900 if (kid->op_private & OPpCONST_BARE) {
10901 dVAR;
10902 const char *end;
10903
10904 if (was_readonly) {
10905 SvREADONLY_off(sv);
10906 }
10907 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
10908
10909 s = SvPVX(sv);
10910 len = SvCUR(sv);
10911 end = s + len;
10912 /* treat ::foo::bar as foo::bar */
10913 if (len >= 2 && s[0] == ':' && s[1] == ':')
10914 DIE(aTHX_ "Bareword in require must not start with a double-colon: \"%s\"\n", s);
10915 if (s == end)
10916 DIE(aTHX_ "Bareword in require maps to empty filename");
10917
10918 for (; s < end; s++) {
10919 if (*s == ':' && s[1] == ':') {
10920 *s = '/';
10921 Move(s+2, s+1, end - s - 1, char);
10922 --end;
10923 }
10924 }
10925 SvEND_set(sv, end);
10926 sv_catpvs(sv, ".pm");
10927 PERL_HASH(hash, SvPVX(sv), SvCUR(sv));
10928 hek = share_hek(SvPVX(sv),
10929 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1),
10930 hash);
10931 sv_sethek(sv, hek);
10932 unshare_hek(hek);
10933 SvFLAGS(sv) |= was_readonly;
10934 }
10935 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv)
10936 && !SvVOK(sv)) {
10937 s = SvPV(sv, len);
10938 if (SvREFCNT(sv) > 1) {
10939 kid->op_sv = newSVpvn_share(
10940 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0);
10941 SvREFCNT_dec_NN(sv);
10942 }
10943 else {
10944 dVAR;
10945 if (was_readonly) SvREADONLY_off(sv);
10946 PERL_HASH(hash, s, len);
10947 hek = share_hek(s,
10948 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len,
10949 hash);
10950 sv_sethek(sv, hek);
10951 unshare_hek(hek);
10952 SvFLAGS(sv) |= was_readonly;
10953 }
10954 }
10955 }
10956 }
10957
10958 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
10959 /* handle override, if any */
10960 && (gv = gv_override("require", 7))) {
10961 OP *kid, *newop;
10962 if (o->op_flags & OPf_KIDS) {
10963 kid = cUNOPo->op_first;
10964 op_sibling_splice(o, NULL, -1, NULL);
10965 }
10966 else {
10967 kid = newDEFSVOP();
10968 }
10969 op_free(o);
10970 newop = S_new_entersubop(aTHX_ gv, kid);
10971 return newop;
10972 }
10973
10974 return ck_fun(o);
10975}
10976
10977OP *
10978Perl_ck_return(pTHX_ OP *o)
10979{
10980 OP *kid;
10981
10982 PERL_ARGS_ASSERT_CK_RETURN;
10983
10984 kid = OpSIBLING(cLISTOPo->op_first);
10985 if (CvLVALUE(PL_compcv)) {
10986 for (; kid; kid = OpSIBLING(kid))
10987 op_lvalue(kid, OP_LEAVESUBLV);
10988 }
10989
10990 return o;
10991}
10992
10993OP *
10994Perl_ck_select(pTHX_ OP *o)
10995{
10996 dVAR;
10997 OP* kid;
10998
10999 PERL_ARGS_ASSERT_CK_SELECT;
11000
11001 if (o->op_flags & OPf_KIDS) {
11002 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
11003 if (kid && OpHAS_SIBLING(kid)) {
11004 OpTYPE_set(o, OP_SSELECT);
11005 o = ck_fun(o);
11006 return fold_constants(op_integerize(op_std_init(o)));
11007 }
11008 }
11009 o = ck_fun(o);
11010 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
11011 if (kid && kid->op_type == OP_RV2GV)
11012 kid->op_private &= ~HINT_STRICT_REFS;
11013 return o;
11014}
11015
11016OP *
11017Perl_ck_shift(pTHX_ OP *o)
11018{
11019 const I32 type = o->op_type;
11020
11021 PERL_ARGS_ASSERT_CK_SHIFT;
11022
11023 if (!(o->op_flags & OPf_KIDS)) {
11024 OP *argop;
11025
11026 if (!CvUNIQUE(PL_compcv)) {
11027 o->op_flags |= OPf_SPECIAL;
11028 return o;
11029 }
11030
11031 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
11032 op_free(o);
11033 return newUNOP(type, 0, scalar(argop));
11034 }
11035 return scalar(ck_fun(o));
11036}
11037
11038OP *
11039Perl_ck_sort(pTHX_ OP *o)
11040{
11041 OP *firstkid;
11042 OP *kid;
11043 HV * const hinthv =
11044 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;
11045 U8 stacked;
11046
11047 PERL_ARGS_ASSERT_CK_SORT;
11048
11049 if (hinthv) {
11050 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
11051 if (svp) {
11052 const I32 sorthints = (I32)SvIV(*svp);
11053 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
11054 o->op_private |= OPpSORT_QSORT;
11055 if ((sorthints & HINT_SORT_STABLE) != 0)
11056 o->op_private |= OPpSORT_STABLE;
11057 }
11058 }
11059
11060 if (o->op_flags & OPf_STACKED)
11061 simplify_sort(o);
11062 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
11063
11064 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */
11065 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
11066
11067 /* if the first arg is a code block, process it and mark sort as
11068 * OPf_SPECIAL */
11069 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
11070 LINKLIST(kid);
11071 if (kid->op_type == OP_LEAVE)
11072 op_null(kid); /* wipe out leave */
11073 /* Prevent execution from escaping out of the sort block. */
11074 kid->op_next = 0;
11075
11076 /* provide scalar context for comparison function/block */
11077 kid = scalar(firstkid);
11078 kid->op_next = kid;
11079 o->op_flags |= OPf_SPECIAL;
11080 }
11081 else if (kid->op_type == OP_CONST
11082 && kid->op_private & OPpCONST_BARE) {
11083 char tmpbuf[256];
11084 STRLEN len;
11085 PADOFFSET off;
11086 const char * const name = SvPV(kSVOP_sv, len);
11087 *tmpbuf = '&';
11088 assert (len < 256);
11089 Copy(name, tmpbuf+1, len, char);
11090 off = pad_findmy_pvn(tmpbuf, len+1, SvUTF8(kSVOP_sv));
11091 if (off != NOT_IN_PAD) {
11092 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
11093 SV * const fq =
11094 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off)));
11095 sv_catpvs(fq, "::");
11096 sv_catsv(fq, kSVOP_sv);
11097 SvREFCNT_dec_NN(kSVOP_sv);
11098 kSVOP->op_sv = fq;
11099 }
11100 else {
11101 OP * const padop = newOP(OP_PADCV, 0);
11102 padop->op_targ = off;
11103 /* replace the const op with the pad op */
11104 op_sibling_splice(firstkid, NULL, 1, padop);
11105 op_free(kid);
11106 }
11107 }
11108 }
11109
11110 firstkid = OpSIBLING(firstkid);
11111 }
11112
11113 for (kid = firstkid; kid; kid = OpSIBLING(kid)) {
11114 /* provide list context for arguments */
11115 list(kid);
11116 if (stacked)
11117 op_lvalue(kid, OP_GREPSTART);
11118 }
11119
11120 return o;
11121}
11122
11123/* for sort { X } ..., where X is one of
11124 * $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a
11125 * elide the second child of the sort (the one containing X),
11126 * and set these flags as appropriate
11127 OPpSORT_NUMERIC;
11128 OPpSORT_INTEGER;
11129 OPpSORT_DESCEND;
11130 * Also, check and warn on lexical $a, $b.
11131 */
11132
11133STATIC void
11134S_simplify_sort(pTHX_ OP *o)
11135{
11136 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
11137 OP *k;
11138 int descending;
11139 GV *gv;
11140 const char *gvname;
11141 bool have_scopeop;
11142
11143 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
11144
11145 kid = kUNOP->op_first; /* get past null */
11146 if (!(have_scopeop = kid->op_type == OP_SCOPE)
11147 && kid->op_type != OP_LEAVE)
11148 return;
11149 kid = kLISTOP->op_last; /* get past scope */
11150 switch(kid->op_type) {
11151 case OP_NCMP:
11152 case OP_I_NCMP:
11153 case OP_SCMP:
11154 if (!have_scopeop) goto padkids;
11155 break;
11156 default:
11157 return;
11158 }
11159 k = kid; /* remember this node*/
11160 if (kBINOP->op_first->op_type != OP_RV2SV
11161 || kBINOP->op_last ->op_type != OP_RV2SV)
11162 {
11163 /*
11164 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
11165 then used in a comparison. This catches most, but not
11166 all cases. For instance, it catches
11167 sort { my($a); $a <=> $b }
11168 but not
11169 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
11170 (although why you'd do that is anyone's guess).
11171 */
11172
11173 padkids:
11174 if (!ckWARN(WARN_SYNTAX)) return;
11175 kid = kBINOP->op_first;
11176 do {
11177 if (kid->op_type == OP_PADSV) {
11178 PADNAME * const name = PAD_COMPNAME(kid->op_targ);
11179 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$'
11180 && ( PadnamePV(name)[1] == 'a'
11181 || PadnamePV(name)[1] == 'b' ))
11182 /* diag_listed_as: "my %s" used in sort comparison */
11183 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11184 "\"%s %s\" used in sort comparison",
11185 PadnameIsSTATE(name)
11186 ? "state"
11187 : "my",
11188 PadnamePV(name));
11189 }
11190 } while ((kid = OpSIBLING(kid)));
11191 return;
11192 }
11193 kid = kBINOP->op_first; /* get past cmp */
11194 if (kUNOP->op_first->op_type != OP_GV)
11195 return;
11196 kid = kUNOP->op_first; /* get past rv2sv */
11197 gv = kGVOP_gv;
11198 if (GvSTASH(gv) != PL_curstash)
11199 return;
11200 gvname = GvNAME(gv);
11201 if (*gvname == 'a' && gvname[1] == '\0')
11202 descending = 0;
11203 else if (*gvname == 'b' && gvname[1] == '\0')
11204 descending = 1;
11205 else
11206 return;
11207
11208 kid = k; /* back to cmp */
11209 /* already checked above that it is rv2sv */
11210 kid = kBINOP->op_last; /* down to 2nd arg */
11211 if (kUNOP->op_first->op_type != OP_GV)
11212 return;
11213 kid = kUNOP->op_first; /* get past rv2sv */
11214 gv = kGVOP_gv;
11215 if (GvSTASH(gv) != PL_curstash)
11216 return;
11217 gvname = GvNAME(gv);
11218 if ( descending
11219 ? !(*gvname == 'a' && gvname[1] == '\0')
11220 : !(*gvname == 'b' && gvname[1] == '\0'))
11221 return;
11222 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
11223 if (descending)
11224 o->op_private |= OPpSORT_DESCEND;
11225 if (k->op_type == OP_NCMP)
11226 o->op_private |= OPpSORT_NUMERIC;
11227 if (k->op_type == OP_I_NCMP)
11228 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
11229 kid = OpSIBLING(cLISTOPo->op_first);
11230 /* cut out and delete old block (second sibling) */
11231 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL);
11232 op_free(kid);
11233}
11234
11235OP *
11236Perl_ck_split(pTHX_ OP *o)
11237{
11238 dVAR;
11239 OP *kid;
11240 OP *sibs;
11241
11242 PERL_ARGS_ASSERT_CK_SPLIT;
11243
11244 assert(o->op_type == OP_LIST);
11245
11246 if (o->op_flags & OPf_STACKED)
11247 return no_fh_allowed(o);
11248
11249 kid = cLISTOPo->op_first;
11250 /* delete leading NULL node, then add a CONST if no other nodes */
11251 assert(kid->op_type == OP_NULL);
11252 op_sibling_splice(o, NULL, 1,
11253 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" ")));
11254 op_free(kid);
11255 kid = cLISTOPo->op_first;
11256
11257 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
11258 /* remove match expression, and replace with new optree with
11259 * a match op at its head */
11260 op_sibling_splice(o, NULL, 1, NULL);
11261 /* pmruntime will handle split " " behavior with flag==2 */
11262 kid = pmruntime(newPMOP(OP_MATCH, 0), kid, NULL, 2, 0);
11263 op_sibling_splice(o, NULL, 0, kid);
11264 }
11265
11266 assert(kid->op_type == OP_MATCH || kid->op_type == OP_SPLIT);
11267
11268 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
11269 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
11270 "Use of /g modifier is meaningless in split");
11271 }
11272
11273 /* eliminate the split op, and move the match op (plus any children)
11274 * into its place, then convert the match op into a split op. i.e.
11275 *
11276 * SPLIT MATCH SPLIT(ex-MATCH)
11277 * | | |
11278 * MATCH - A - B - C => R - A - B - C => R - A - B - C
11279 * | | |
11280 * R X - Y X - Y
11281 * |
11282 * X - Y
11283 *
11284 * (R, if it exists, will be a regcomp op)
11285 */
11286
11287 op_sibling_splice(o, NULL, 1, NULL); /* detach match op from o */
11288 sibs = op_sibling_splice(o, NULL, -1, NULL); /* detach any other sibs */
11289 op_sibling_splice(kid, cLISTOPx(kid)->op_last, 0, sibs); /* and reattach */
11290 OpTYPE_set(kid, OP_SPLIT);
11291 kid->op_flags = (o->op_flags | (kid->op_flags & OPf_KIDS));
11292 kid->op_private = o->op_private;
11293 op_free(o);
11294 o = kid;
11295 kid = sibs; /* kid is now the string arg of the split */
11296
11297 if (!kid) {
11298 kid = newDEFSVOP();
11299 op_append_elem(OP_SPLIT, o, kid);
11300 }
11301 scalar(kid);
11302
11303 kid = OpSIBLING(kid);
11304 if (!kid) {
11305 kid = newSVOP(OP_CONST, 0, newSViv(0));
11306 op_append_elem(OP_SPLIT, o, kid);
11307 o->op_private |= OPpSPLIT_IMPLIM;
11308 }
11309 scalar(kid);
11310
11311 if (OpHAS_SIBLING(kid))
11312 return too_many_arguments_pv(o,OP_DESC(o), 0);
11313
11314 return o;
11315}
11316
11317OP *
11318Perl_ck_stringify(pTHX_ OP *o)
11319{
11320 OP * const kid = OpSIBLING(cUNOPo->op_first);
11321 PERL_ARGS_ASSERT_CK_STRINGIFY;
11322 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA
11323 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST
11324 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST)
11325 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */
11326 {
11327 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
11328 op_free(o);
11329 return kid;
11330 }
11331 return ck_fun(o);
11332}
11333
11334OP *
11335Perl_ck_join(pTHX_ OP *o)
11336{
11337 OP * const kid = OpSIBLING(cLISTOPo->op_first);
11338
11339 PERL_ARGS_ASSERT_CK_JOIN;
11340
11341 if (kid && kid->op_type == OP_MATCH) {
11342 if (ckWARN(WARN_SYNTAX)) {
11343 const REGEXP *re = PM_GETRE(kPMOP);
11344 const SV *msg = re
11345 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
11346 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
11347 : newSVpvs_flags( "STRING", SVs_TEMP );
11348 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11349 "/%" SVf "/ should probably be written as \"%" SVf "\"",
11350 SVfARG(msg), SVfARG(msg));
11351 }
11352 }
11353 if (kid
11354 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */
11355 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO))
11356 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV
11357 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))))
11358 {
11359 const OP * const bairn = OpSIBLING(kid); /* the list */
11360 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */
11361 && OP_GIMME(bairn,0) == G_SCALAR)
11362 {
11363 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED,
11364 op_sibling_splice(o, kid, 1, NULL));
11365 op_free(o);
11366 return ret;
11367 }
11368 }
11369
11370 return ck_fun(o);
11371}
11372
11373/*
11374=for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags
11375
11376Examines an op, which is expected to identify a subroutine at runtime,
11377and attempts to determine at compile time which subroutine it identifies.
11378This is normally used during Perl compilation to determine whether
11379a prototype can be applied to a function call. C<cvop> is the op
11380being considered, normally an C<rv2cv> op. A pointer to the identified
11381subroutine is returned, if it could be determined statically, and a null
11382pointer is returned if it was not possible to determine statically.
11383
11384Currently, the subroutine can be identified statically if the RV that the
11385C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
11386A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
11387suitable if the constant value must be an RV pointing to a CV. Details of
11388this process may change in future versions of Perl. If the C<rv2cv> op
11389has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
11390the subroutine statically: this flag is used to suppress compile-time
11391magic on a subroutine call, forcing it to use default runtime behaviour.
11392
11393If C<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
11394of a GV reference is modified. If a GV was examined and its CV slot was
11395found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
11396If the op is not optimised away, and the CV slot is later populated with
11397a subroutine having a prototype, that flag eventually triggers the warning
11398"called too early to check prototype".
11399
11400If C<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
11401of returning a pointer to the subroutine it returns a pointer to the
11402GV giving the most appropriate name for the subroutine in this context.
11403Normally this is just the C<CvGV> of the subroutine, but for an anonymous
11404(C<CvANON>) subroutine that is referenced through a GV it will be the
11405referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
11406A null pointer is returned as usual if there is no statically-determinable
11407subroutine.
11408
11409=cut
11410*/
11411
11412/* shared by toke.c:yylex */
11413CV *
11414Perl_find_lexical_cv(pTHX_ PADOFFSET off)
11415{
11416 PADNAME *name = PAD_COMPNAME(off);
11417 CV *compcv = PL_compcv;
11418 while (PadnameOUTER(name)) {
11419 assert(PARENT_PAD_INDEX(name));
11420 compcv = CvOUTSIDE(compcv);
11421 name = PadlistNAMESARRAY(CvPADLIST(compcv))
11422 [off = PARENT_PAD_INDEX(name)];
11423 }
11424 assert(!PadnameIsOUR(name));
11425 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) {
11426 return PadnamePROTOCV(name);
11427 }
11428 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
11429}
11430
11431CV *
11432Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
11433{
11434 OP *rvop;
11435 CV *cv;
11436 GV *gv;
11437 PERL_ARGS_ASSERT_RV2CV_OP_CV;
11438 if (flags & ~RV2CVOPCV_FLAG_MASK)
11439 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
11440 if (cvop->op_type != OP_RV2CV)
11441 return NULL;
11442 if (cvop->op_private & OPpENTERSUB_AMPER)
11443 return NULL;
11444 if (!(cvop->op_flags & OPf_KIDS))
11445 return NULL;
11446 rvop = cUNOPx(cvop)->op_first;
11447 switch (rvop->op_type) {
11448 case OP_GV: {
11449 gv = cGVOPx_gv(rvop);
11450 if (!isGV(gv)) {
11451 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) {
11452 cv = MUTABLE_CV(SvRV(gv));
11453 gv = NULL;
11454 break;
11455 }
11456 if (flags & RV2CVOPCV_RETURN_STUB)
11457 return (CV *)gv;
11458 else return NULL;
11459 }
11460 cv = GvCVu(gv);
11461 if (!cv) {
11462 if (flags & RV2CVOPCV_MARK_EARLY)
11463 rvop->op_private |= OPpEARLY_CV;
11464 return NULL;
11465 }
11466 } break;
11467 case OP_CONST: {
11468 SV *rv = cSVOPx_sv(rvop);
11469 if (!SvROK(rv))
11470 return NULL;
11471 cv = (CV*)SvRV(rv);
11472 gv = NULL;
11473 } break;
11474 case OP_PADCV: {
11475 cv = find_lexical_cv(rvop->op_targ);
11476 gv = NULL;
11477 } break;
11478 default: {
11479 return NULL;
11480 } NOT_REACHED; /* NOTREACHED */
11481 }
11482 if (SvTYPE((SV*)cv) != SVt_PVCV)
11483 return NULL;
11484 if (flags & (RV2CVOPCV_RETURN_NAME_GV|RV2CVOPCV_MAYBE_NAME_GV)) {
11485 if ((!CvANON(cv) || !gv) && !CvLEXICAL(cv)
11486 && ((flags & RV2CVOPCV_RETURN_NAME_GV) || !CvNAMED(cv)))
11487 gv = CvGV(cv);
11488 return (CV*)gv;
11489 } else {
11490 return cv;
11491 }
11492}
11493
11494/*
11495=for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop
11496
11497Performs the default fixup of the arguments part of an C<entersub>
11498op tree. This consists of applying list context to each of the
11499argument ops. This is the standard treatment used on a call marked
11500with C<&>, or a method call, or a call through a subroutine reference,
11501or any other call where the callee can't be identified at compile time,
11502or a call where the callee has no prototype.
11503
11504=cut
11505*/
11506
11507OP *
11508Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
11509{
11510 OP *aop;
11511
11512 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
11513
11514 aop = cUNOPx(entersubop)->op_first;
11515 if (!OpHAS_SIBLING(aop))
11516 aop = cUNOPx(aop)->op_first;
11517 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) {
11518 /* skip the extra attributes->import() call implicitly added in
11519 * something like foo(my $x : bar)
11520 */
11521 if ( aop->op_type == OP_ENTERSUB
11522 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID
11523 )
11524 continue;
11525 list(aop);
11526 op_lvalue(aop, OP_ENTERSUB);
11527 }
11528 return entersubop;
11529}
11530
11531/*
11532=for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv
11533
11534Performs the fixup of the arguments part of an C<entersub> op tree
11535based on a subroutine prototype. This makes various modifications to
11536the argument ops, from applying context up to inserting C<refgen> ops,
11537and checking the number and syntactic types of arguments, as directed by
11538the prototype. This is the standard treatment used on a subroutine call,
11539not marked with C<&>, where the callee can be identified at compile time
11540and has a prototype.
11541
11542C<protosv> supplies the subroutine prototype to be applied to the call.
11543It may be a normal defined scalar, of which the string value will be used.
11544Alternatively, for convenience, it may be a subroutine object (a C<CV*>
11545that has been cast to C<SV*>) which has a prototype. The prototype
11546supplied, in whichever form, does not need to match the actual callee
11547referenced by the op tree.
11548
11549If the argument ops disagree with the prototype, for example by having
11550an unacceptable number of arguments, a valid op tree is returned anyway.
11551The error is reflected in the parser state, normally resulting in a single
11552exception at the top level of parsing which covers all the compilation
11553errors that occurred. In the error message, the callee is referred to
11554by the name defined by the C<namegv> parameter.
11555
11556=cut
11557*/
11558
11559OP *
11560Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
11561{
11562 STRLEN proto_len;
11563 const char *proto, *proto_end;
11564 OP *aop, *prev, *cvop, *parent;
11565 int optional = 0;
11566 I32 arg = 0;
11567 I32 contextclass = 0;
11568 const char *e = NULL;
11569 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
11570 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
11571 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
11572 "flags=%lx", (unsigned long) SvFLAGS(protosv));
11573 if (SvTYPE(protosv) == SVt_PVCV)
11574 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
11575 else proto = SvPV(protosv, proto_len);
11576 proto = S_strip_spaces(aTHX_ proto, &proto_len);
11577 proto_end = proto + proto_len;
11578 parent = entersubop;
11579 aop = cUNOPx(entersubop)->op_first;
11580 if (!OpHAS_SIBLING(aop)) {
11581 parent = aop;
11582 aop = cUNOPx(aop)->op_first;
11583 }
11584 prev = aop;
11585 aop = OpSIBLING(aop);
11586 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11587 while (aop != cvop) {
11588 OP* o3 = aop;
11589
11590 if (proto >= proto_end)
11591 {
11592 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
11593 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
11594 SVfARG(namesv)), SvUTF8(namesv));
11595 return entersubop;
11596 }
11597
11598 switch (*proto) {
11599 case ';':
11600 optional = 1;
11601 proto++;
11602 continue;
11603 case '_':
11604 /* _ must be at the end */
11605 if (proto[1] && !strchr(";@%", proto[1]))
11606 goto oops;
11607 /* FALLTHROUGH */
11608 case '$':
11609 proto++;
11610 arg++;
11611 scalar(aop);
11612 break;
11613 case '%':
11614 case '@':
11615 list(aop);
11616 arg++;
11617 break;
11618 case '&':
11619 proto++;
11620 arg++;
11621 if ( o3->op_type != OP_UNDEF
11622 && (o3->op_type != OP_SREFGEN
11623 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
11624 != OP_ANONCODE
11625 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
11626 != OP_RV2CV)))
11627 bad_type_gv(arg, namegv, o3,
11628 arg == 1 ? "block or sub {}" : "sub {}");
11629 break;
11630 case '*':
11631 /* '*' allows any scalar type, including bareword */
11632 proto++;
11633 arg++;
11634 if (o3->op_type == OP_RV2GV)
11635 goto wrapref; /* autoconvert GLOB -> GLOBref */
11636 else if (o3->op_type == OP_CONST)
11637 o3->op_private &= ~OPpCONST_STRICT;
11638 scalar(aop);
11639 break;
11640 case '+':
11641 proto++;
11642 arg++;
11643 if (o3->op_type == OP_RV2AV ||
11644 o3->op_type == OP_PADAV ||
11645 o3->op_type == OP_RV2HV ||
11646 o3->op_type == OP_PADHV
11647 ) {
11648 goto wrapref;
11649 }
11650 scalar(aop);
11651 break;
11652 case '[': case ']':
11653 goto oops;
11654
11655 case '\\':
11656 proto++;
11657 arg++;
11658 again:
11659 switch (*proto++) {
11660 case '[':
11661 if (contextclass++ == 0) {
11662 e = strchr(proto, ']');
11663 if (!e || e == proto)
11664 goto oops;
11665 }
11666 else
11667 goto oops;
11668 goto again;
11669
11670 case ']':
11671 if (contextclass) {
11672 const char *p = proto;
11673 const char *const end = proto;
11674 contextclass = 0;
11675 while (*--p != '[')
11676 /* \[$] accepts any scalar lvalue */
11677 if (*p == '$'
11678 && Perl_op_lvalue_flags(aTHX_
11679 scalar(o3),
11680 OP_READ, /* not entersub */
11681 OP_LVALUE_NO_CROAK
11682 )) goto wrapref;
11683 bad_type_gv(arg, namegv, o3,
11684 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p));
11685 } else
11686 goto oops;
11687 break;
11688 case '*':
11689 if (o3->op_type == OP_RV2GV)
11690 goto wrapref;
11691 if (!contextclass)
11692 bad_type_gv(arg, namegv, o3, "symbol");
11693 break;
11694 case '&':
11695 if (o3->op_type == OP_ENTERSUB
11696 && !(o3->op_flags & OPf_STACKED))
11697 goto wrapref;
11698 if (!contextclass)
11699 bad_type_gv(arg, namegv, o3, "subroutine");
11700 break;
11701 case '$':
11702 if (o3->op_type == OP_RV2SV ||
11703 o3->op_type == OP_PADSV ||
11704 o3->op_type == OP_HELEM ||
11705 o3->op_type == OP_AELEM)
11706 goto wrapref;
11707 if (!contextclass) {
11708 /* \$ accepts any scalar lvalue */
11709 if (Perl_op_lvalue_flags(aTHX_
11710 scalar(o3),
11711 OP_READ, /* not entersub */
11712 OP_LVALUE_NO_CROAK
11713 )) goto wrapref;
11714 bad_type_gv(arg, namegv, o3, "scalar");
11715 }
11716 break;
11717 case '@':
11718 if (o3->op_type == OP_RV2AV ||
11719 o3->op_type == OP_PADAV)
11720 {
11721 o3->op_flags &=~ OPf_PARENS;
11722 goto wrapref;
11723 }
11724 if (!contextclass)
11725 bad_type_gv(arg, namegv, o3, "array");
11726 break;
11727 case '%':
11728 if (o3->op_type == OP_RV2HV ||
11729 o3->op_type == OP_PADHV)
11730 {
11731 o3->op_flags &=~ OPf_PARENS;
11732 goto wrapref;
11733 }
11734 if (!contextclass)
11735 bad_type_gv(arg, namegv, o3, "hash");
11736 break;
11737 wrapref:
11738 aop = S_op_sibling_newUNOP(aTHX_ parent, prev,
11739 OP_REFGEN, 0);
11740 if (contextclass && e) {
11741 proto = e + 1;
11742 contextclass = 0;
11743 }
11744 break;
11745 default: goto oops;
11746 }
11747 if (contextclass)
11748 goto again;
11749 break;
11750 case ' ':
11751 proto++;
11752 continue;
11753 default:
11754 oops: {
11755 Perl_croak(aTHX_ "Malformed prototype for %" SVf ": %" SVf,
11756 SVfARG(cv_name((CV *)namegv, NULL, 0)),
11757 SVfARG(protosv));
11758 }
11759 }
11760
11761 op_lvalue(aop, OP_ENTERSUB);
11762 prev = aop;
11763 aop = OpSIBLING(aop);
11764 }
11765 if (aop == cvop && *proto == '_') {
11766 /* generate an access to $_ */
11767 op_sibling_splice(parent, prev, 0, newDEFSVOP());
11768 }
11769 if (!optional && proto_end > proto &&
11770 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
11771 {
11772 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
11773 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %" SVf,
11774 SVfARG(namesv)), SvUTF8(namesv));
11775 }
11776 return entersubop;
11777}
11778
11779/*
11780=for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv
11781
11782Performs the fixup of the arguments part of an C<entersub> op tree either
11783based on a subroutine prototype or using default list-context processing.
11784This is the standard treatment used on a subroutine call, not marked
11785with C<&>, where the callee can be identified at compile time.
11786
11787C<protosv> supplies the subroutine prototype to be applied to the call,
11788or indicates that there is no prototype. It may be a normal scalar,
11789in which case if it is defined then the string value will be used
11790as a prototype, and if it is undefined then there is no prototype.
11791Alternatively, for convenience, it may be a subroutine object (a C<CV*>
11792that has been cast to C<SV*>), of which the prototype will be used if it
11793has one. The prototype (or lack thereof) supplied, in whichever form,
11794does not need to match the actual callee referenced by the op tree.
11795
11796If the argument ops disagree with the prototype, for example by having
11797an unacceptable number of arguments, a valid op tree is returned anyway.
11798The error is reflected in the parser state, normally resulting in a single
11799exception at the top level of parsing which covers all the compilation
11800errors that occurred. In the error message, the callee is referred to
11801by the name defined by the C<namegv> parameter.
11802
11803=cut
11804*/
11805
11806OP *
11807Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
11808 GV *namegv, SV *protosv)
11809{
11810 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
11811 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
11812 return ck_entersub_args_proto(entersubop, namegv, protosv);
11813 else
11814 return ck_entersub_args_list(entersubop);
11815}
11816
11817OP *
11818Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
11819{
11820 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv);
11821 OP *aop = cUNOPx(entersubop)->op_first;
11822
11823 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
11824
11825 if (!opnum) {
11826 OP *cvop;
11827 if (!OpHAS_SIBLING(aop))
11828 aop = cUNOPx(aop)->op_first;
11829 aop = OpSIBLING(aop);
11830 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11831 if (aop != cvop)
11832 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0);
11833
11834 op_free(entersubop);
11835 switch(GvNAME(namegv)[2]) {
11836 case 'F': return newSVOP(OP_CONST, 0,
11837 newSVpv(CopFILE(PL_curcop),0));
11838 case 'L': return newSVOP(
11839 OP_CONST, 0,
11840 Perl_newSVpvf(aTHX_
11841 "%" IVdf, (IV)CopLINE(PL_curcop)
11842 )
11843 );
11844 case 'P': return newSVOP(OP_CONST, 0,
11845 (PL_curstash
11846 ? newSVhek(HvNAME_HEK(PL_curstash))
11847 : &PL_sv_undef
11848 )
11849 );
11850 }
11851 NOT_REACHED; /* NOTREACHED */
11852 }
11853 else {
11854 OP *prev, *cvop, *first, *parent;
11855 U32 flags = 0;
11856
11857 parent = entersubop;
11858 if (!OpHAS_SIBLING(aop)) {
11859 parent = aop;
11860 aop = cUNOPx(aop)->op_first;
11861 }
11862
11863 first = prev = aop;
11864 aop = OpSIBLING(aop);
11865 /* find last sibling */
11866 for (cvop = aop;
11867 OpHAS_SIBLING(cvop);
11868 prev = cvop, cvop = OpSIBLING(cvop))
11869 ;
11870 if (!(cvop->op_private & OPpENTERSUB_NOPAREN)
11871 /* Usually, OPf_SPECIAL on an op with no args means that it had
11872 * parens, but these have their own meaning for that flag: */
11873 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH
11874 && opnum != OP_DELETE && opnum != OP_EXISTS)
11875 flags |= OPf_SPECIAL;
11876 /* excise cvop from end of sibling chain */
11877 op_sibling_splice(parent, prev, 1, NULL);
11878 op_free(cvop);
11879 if (aop == cvop) aop = NULL;
11880
11881 /* detach remaining siblings from the first sibling, then
11882 * dispose of original optree */
11883
11884 if (aop)
11885 op_sibling_splice(parent, first, -1, NULL);
11886 op_free(entersubop);
11887
11888 if (opnum == OP_ENTEREVAL
11889 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9))
11890 flags |= OPpEVAL_BYTES <<8;
11891
11892 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
11893 case OA_UNOP:
11894 case OA_BASEOP_OR_UNOP:
11895 case OA_FILESTATOP:
11896 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags);
11897 case OA_BASEOP:
11898 if (aop) {
11899 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0);
11900 op_free(aop);
11901 }
11902 return opnum == OP_RUNCV
11903 ? newPVOP(OP_RUNCV,0,NULL)
11904 : newOP(opnum,0);
11905 default:
11906 return op_convert_list(opnum,0,aop);
11907 }
11908 }
11909 NOT_REACHED; /* NOTREACHED */
11910 return entersubop;
11911}
11912
11913/*
11914=for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p
11915
11916Retrieves the function that will be used to fix up a call to C<cv>.
11917Specifically, the function is applied to an C<entersub> op tree for a
11918subroutine call, not marked with C<&>, where the callee can be identified
11919at compile time as C<cv>.
11920
11921The C-level function pointer is returned in C<*ckfun_p>, and an SV
11922argument for it is returned in C<*ckobj_p>. The function is intended
11923to be called in this manner:
11924
11925 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
11926
11927In this call, C<entersubop> is a pointer to the C<entersub> op,
11928which may be replaced by the check function, and C<namegv> is a GV
11929supplying the name that should be used by the check function to refer
11930to the callee of the C<entersub> op if it needs to emit any diagnostics.
11931It is permitted to apply the check function in non-standard situations,
11932such as to a call to a different subroutine or to a method call.
11933
11934By default, the function is
11935L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
11936and the SV parameter is C<cv> itself. This implements standard
11937prototype processing. It can be changed, for a particular subroutine,
11938by L</cv_set_call_checker>.
11939
11940=cut
11941*/
11942
11943static void
11944S_cv_get_call_checker(CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p,
11945 U8 *flagsp)
11946{
11947 MAGIC *callmg;
11948 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
11949 if (callmg) {
11950 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
11951 *ckobj_p = callmg->mg_obj;
11952 if (flagsp) *flagsp = callmg->mg_flags;
11953 } else {
11954 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
11955 *ckobj_p = (SV*)cv;
11956 if (flagsp) *flagsp = 0;
11957 }
11958}
11959
11960void
11961Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
11962{
11963 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
11964 PERL_UNUSED_CONTEXT;
11965 S_cv_get_call_checker(cv, ckfun_p, ckobj_p, NULL);
11966}
11967
11968/*
11969=for apidoc Am|void|cv_set_call_checker_flags|CV *cv|Perl_call_checker ckfun|SV *ckobj|U32 flags
11970
11971Sets the function that will be used to fix up a call to C<cv>.
11972Specifically, the function is applied to an C<entersub> op tree for a
11973subroutine call, not marked with C<&>, where the callee can be identified
11974at compile time as C<cv>.
11975
11976The C-level function pointer is supplied in C<ckfun>, and an SV argument
11977for it is supplied in C<ckobj>. The function should be defined like this:
11978
11979 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
11980
11981It is intended to be called in this manner:
11982
11983 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
11984
11985In this call, C<entersubop> is a pointer to the C<entersub> op,
11986which may be replaced by the check function, and C<namegv> supplies
11987the name that should be used by the check function to refer
11988to the callee of the C<entersub> op if it needs to emit any diagnostics.
11989It is permitted to apply the check function in non-standard situations,
11990such as to a call to a different subroutine or to a method call.
11991
11992C<namegv> may not actually be a GV. For efficiency, perl may pass a
11993CV or other SV instead. Whatever is passed can be used as the first
11994argument to L</cv_name>. You can force perl to pass a GV by including
11995C<CALL_CHECKER_REQUIRE_GV> in the C<flags>.
11996
11997The current setting for a particular CV can be retrieved by
11998L</cv_get_call_checker>.
11999
12000=for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj
12001
12002The original form of L</cv_set_call_checker_flags>, which passes it the
12003C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility.
12004
12005=cut
12006*/
12007
12008void
12009Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
12010{
12011 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
12012 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV);
12013}
12014
12015void
12016Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun,
12017 SV *ckobj, U32 flags)
12018{
12019 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS;
12020 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
12021 if (SvMAGICAL((SV*)cv))
12022 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
12023 } else {
12024 MAGIC *callmg;
12025 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
12026 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
12027 assert(callmg);
12028 if (callmg->mg_flags & MGf_REFCOUNTED) {
12029 SvREFCNT_dec(callmg->mg_obj);
12030 callmg->mg_flags &= ~MGf_REFCOUNTED;
12031 }
12032 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
12033 callmg->mg_obj = ckobj;
12034 if (ckobj != (SV*)cv) {
12035 SvREFCNT_inc_simple_void_NN(ckobj);
12036 callmg->mg_flags |= MGf_REFCOUNTED;
12037 }
12038 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV)
12039 | (U8)(flags & MGf_REQUIRE_GV) | MGf_COPY;
12040 }
12041}
12042
12043static void
12044S_entersub_alloc_targ(pTHX_ OP * const o)
12045{
12046 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP);
12047 o->op_private |= OPpENTERSUB_HASTARG;
12048}
12049
12050OP *
12051Perl_ck_subr(pTHX_ OP *o)
12052{
12053 OP *aop, *cvop;
12054 CV *cv;
12055 GV *namegv;
12056 SV **const_class = NULL;
12057
12058 PERL_ARGS_ASSERT_CK_SUBR;
12059
12060 aop = cUNOPx(o)->op_first;
12061 if (!OpHAS_SIBLING(aop))
12062 aop = cUNOPx(aop)->op_first;
12063 aop = OpSIBLING(aop);
12064 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
12065 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
12066 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL;
12067
12068 o->op_private &= ~1;
12069 o->op_private |= (PL_hints & HINT_STRICT_REFS);
12070 if (PERLDB_SUB && PL_curstash != PL_debstash)
12071 o->op_private |= OPpENTERSUB_DB;
12072 switch (cvop->op_type) {
12073 case OP_RV2CV:
12074 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
12075 op_null(cvop);
12076 break;
12077 case OP_METHOD:
12078 case OP_METHOD_NAMED:
12079 case OP_METHOD_SUPER:
12080 case OP_METHOD_REDIR:
12081 case OP_METHOD_REDIR_SUPER:
12082 o->op_flags |= OPf_REF;
12083 if (aop->op_type == OP_CONST) {
12084 aop->op_private &= ~OPpCONST_STRICT;
12085 const_class = &cSVOPx(aop)->op_sv;
12086 }
12087 else if (aop->op_type == OP_LIST) {
12088 OP * const sib = OpSIBLING(((UNOP*)aop)->op_first);
12089 if (sib && sib->op_type == OP_CONST) {
12090 sib->op_private &= ~OPpCONST_STRICT;
12091 const_class = &cSVOPx(sib)->op_sv;
12092 }
12093 }
12094 /* make class name a shared cow string to speedup method calls */
12095 /* constant string might be replaced with object, f.e. bigint */
12096 if (const_class && SvPOK(*const_class)) {
12097 STRLEN len;
12098 const char* str = SvPV(*const_class, len);
12099 if (len) {
12100 SV* const shared = newSVpvn_share(
12101 str, SvUTF8(*const_class)
12102 ? -(SSize_t)len : (SSize_t)len,
12103 0
12104 );
12105 if (SvREADONLY(*const_class))
12106 SvREADONLY_on(shared);
12107 SvREFCNT_dec(*const_class);
12108 *const_class = shared;
12109 }
12110 }
12111 break;
12112 }
12113
12114 if (!cv) {
12115 S_entersub_alloc_targ(aTHX_ o);
12116 return ck_entersub_args_list(o);
12117 } else {
12118 Perl_call_checker ckfun;
12119 SV *ckobj;
12120 U8 flags;
12121 S_cv_get_call_checker(cv, &ckfun, &ckobj, &flags);
12122 if (CvISXSUB(cv) || !CvROOT(cv))
12123 S_entersub_alloc_targ(aTHX_ o);
12124 if (!namegv) {
12125 /* The original call checker API guarantees that a GV will be
12126 be provided with the right name. So, if the old API was
12127 used (or the REQUIRE_GV flag was passed), we have to reify
12128 the CV’s GV, unless this is an anonymous sub. This is not
12129 ideal for lexical subs, as its stringification will include
12130 the package. But it is the best we can do. */
12131 if (flags & MGf_REQUIRE_GV) {
12132 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv)))
12133 namegv = CvGV(cv);
12134 }
12135 else namegv = MUTABLE_GV(cv);
12136 /* After a syntax error in a lexical sub, the cv that
12137 rv2cv_op_cv returns may be a nameless stub. */
12138 if (!namegv) return ck_entersub_args_list(o);
12139
12140 }
12141 return ckfun(aTHX_ o, namegv, ckobj);
12142 }
12143}
12144
12145OP *
12146Perl_ck_svconst(pTHX_ OP *o)
12147{
12148 SV * const sv = cSVOPo->op_sv;
12149 PERL_ARGS_ASSERT_CK_SVCONST;
12150 PERL_UNUSED_CONTEXT;
12151#ifdef PERL_COPY_ON_WRITE
12152 /* Since the read-only flag may be used to protect a string buffer, we
12153 cannot do copy-on-write with existing read-only scalars that are not
12154 already copy-on-write scalars. To allow $_ = "hello" to do COW with
12155 that constant, mark the constant as COWable here, if it is not
12156 already read-only. */
12157 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
12158 SvIsCOW_on(sv);
12159 CowREFCNT(sv) = 0;
12160# ifdef PERL_DEBUG_READONLY_COW
12161 sv_buf_to_ro(sv);
12162# endif
12163 }
12164#endif
12165 SvREADONLY_on(sv);
12166 return o;
12167}
12168
12169OP *
12170Perl_ck_trunc(pTHX_ OP *o)
12171{
12172 PERL_ARGS_ASSERT_CK_TRUNC;
12173
12174 if (o->op_flags & OPf_KIDS) {
12175 SVOP *kid = (SVOP*)cUNOPo->op_first;
12176
12177 if (kid->op_type == OP_NULL)
12178 kid = (SVOP*)OpSIBLING(kid);
12179 if (kid && kid->op_type == OP_CONST &&
12180 (kid->op_private & OPpCONST_BARE) &&
12181 !kid->op_folded)
12182 {
12183 o->op_flags |= OPf_SPECIAL;
12184 kid->op_private &= ~OPpCONST_STRICT;
12185 }
12186 }
12187 return ck_fun(o);
12188}
12189
12190OP *
12191Perl_ck_substr(pTHX_ OP *o)
12192{
12193 PERL_ARGS_ASSERT_CK_SUBSTR;
12194
12195 o = ck_fun(o);
12196 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
12197 OP *kid = cLISTOPo->op_first;
12198
12199 if (kid->op_type == OP_NULL)
12200 kid = OpSIBLING(kid);
12201 if (kid)
12202 kid->op_flags |= OPf_MOD;
12203
12204 }
12205 return o;
12206}
12207
12208OP *
12209Perl_ck_tell(pTHX_ OP *o)
12210{
12211 PERL_ARGS_ASSERT_CK_TELL;
12212 o = ck_fun(o);
12213 if (o->op_flags & OPf_KIDS) {
12214 OP *kid = cLISTOPo->op_first;
12215 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid);
12216 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
12217 }
12218 return o;
12219}
12220
12221OP *
12222Perl_ck_each(pTHX_ OP *o)
12223{
12224 dVAR;
12225 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
12226 const unsigned orig_type = o->op_type;
12227
12228 PERL_ARGS_ASSERT_CK_EACH;
12229
12230 if (kid) {
12231 switch (kid->op_type) {
12232 case OP_PADHV:
12233 case OP_RV2HV:
12234 break;
12235 case OP_PADAV:
12236 case OP_RV2AV:
12237 OpTYPE_set(o, orig_type == OP_EACH ? OP_AEACH
12238 : orig_type == OP_KEYS ? OP_AKEYS
12239 : OP_AVALUES);
12240 break;
12241 case OP_CONST:
12242 if (kid->op_private == OPpCONST_BARE
12243 || !SvROK(cSVOPx_sv(kid))
12244 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
12245 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
12246 )
12247 goto bad;
12248 /* FALLTHROUGH */
12249 default:
12250 qerror(Perl_mess(aTHX_
12251 "Experimental %s on scalar is now forbidden",
12252 PL_op_desc[orig_type]));
12253 bad:
12254 bad_type_pv(1, "hash or array", o, kid);
12255 return o;
12256 }
12257 }
12258 return ck_fun(o);
12259}
12260
12261OP *
12262Perl_ck_length(pTHX_ OP *o)
12263{
12264 PERL_ARGS_ASSERT_CK_LENGTH;
12265
12266 o = ck_fun(o);
12267
12268 if (ckWARN(WARN_SYNTAX)) {
12269 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
12270
12271 if (kid) {
12272 SV *name = NULL;
12273 const bool hash = kid->op_type == OP_PADHV
12274 || kid->op_type == OP_RV2HV;
12275 switch (kid->op_type) {
12276 case OP_PADHV:
12277 case OP_PADAV:
12278 case OP_RV2HV:
12279 case OP_RV2AV:
12280 name = S_op_varname(aTHX_ kid);
12281 break;
12282 default:
12283 return o;
12284 }
12285 if (name)
12286 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
12287 "length() used on %" SVf " (did you mean \"scalar(%s%" SVf
12288 ")\"?)",
12289 SVfARG(name), hash ? "keys " : "", SVfARG(name)
12290 );
12291 else if (hash)
12292 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
12293 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
12294 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
12295 else
12296 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
12297 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
12298 "length() used on @array (did you mean \"scalar(@array)\"?)");
12299 }
12300 }
12301
12302 return o;
12303}
12304
12305
12306
12307/*
12308 ---------------------------------------------------------
12309
12310 Common vars in list assignment
12311
12312 There now follows some enums and static functions for detecting
12313 common variables in list assignments. Here is a little essay I wrote
12314 for myself when trying to get my head around this. DAPM.
12315
12316 ----
12317
12318 First some random observations:
12319
12320 * If a lexical var is an alias of something else, e.g.
12321 for my $x ($lex, $pkg, $a[0]) {...}
12322 then the act of aliasing will increase the reference count of the SV
12323
12324 * If a package var is an alias of something else, it may still have a
12325 reference count of 1, depending on how the alias was created, e.g.
12326 in *a = *b, $a may have a refcount of 1 since the GP is shared
12327 with a single GvSV pointer to the SV. So If it's an alias of another
12328 package var, then RC may be 1; if it's an alias of another scalar, e.g.
12329 a lexical var or an array element, then it will have RC > 1.
12330
12331 * There are many ways to create a package alias; ultimately, XS code
12332 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so
12333 run-time tracing mechanisms are unlikely to be able to catch all cases.
12334
12335 * When the LHS is all my declarations, the same vars can't appear directly
12336 on the RHS, but they can indirectly via closures, aliasing and lvalue
12337 subs. But those techniques all involve an increase in the lexical
12338 scalar's ref count.
12339
12340 * When the LHS is all lexical vars (but not necessarily my declarations),
12341 it is possible for the same lexicals to appear directly on the RHS, and
12342 without an increased ref count, since the stack isn't refcounted.
12343 This case can be detected at compile time by scanning for common lex
12344 vars with PL_generation.
12345
12346 * lvalue subs defeat common var detection, but they do at least
12347 return vars with a temporary ref count increment. Also, you can't
12348 tell at compile time whether a sub call is lvalue.
12349
12350
12351 So...
12352
12353 A: There are a few circumstances where there definitely can't be any
12354 commonality:
12355
12356 LHS empty: () = (...);
12357 RHS empty: (....) = ();
12358 RHS contains only constants or other 'can't possibly be shared'
12359 elements (e.g. ops that return PADTMPs): (...) = (1,2, length)
12360 i.e. they only contain ops not marked as dangerous, whose children
12361 are also not dangerous;
12362 LHS ditto;
12363 LHS contains a single scalar element: e.g. ($x) = (....); because
12364 after $x has been modified, it won't be used again on the RHS;
12365 RHS contains a single element with no aggregate on LHS: e.g.
12366 ($a,$b,$c) = ($x); again, once $a has been modified, its value
12367 won't be used again.
12368
12369 B: If LHS are all 'my' lexical var declarations (or safe ops, which
12370 we can ignore):
12371
12372 my ($a, $b, @c) = ...;
12373
12374 Due to closure and goto tricks, these vars may already have content.
12375 For the same reason, an element on the RHS may be a lexical or package
12376 alias of one of the vars on the left, or share common elements, for
12377 example:
12378
12379 my ($x,$y) = f(); # $x and $y on both sides
12380 sub f : lvalue { ($x,$y) = (1,2); $y, $x }
12381
12382 and
12383
12384 my $ra = f();
12385 my @a = @$ra; # elements of @a on both sides
12386 sub f { @a = 1..4; \@a }
12387
12388
12389 First, just consider scalar vars on LHS:
12390
12391 RHS is safe only if (A), or in addition,
12392 * contains only lexical *scalar* vars, where neither side's
12393 lexicals have been flagged as aliases
12394
12395 If RHS is not safe, then it's always legal to check LHS vars for
12396 RC==1, since the only RHS aliases will always be associated
12397 with an RC bump.
12398
12399 Note that in particular, RHS is not safe if:
12400
12401 * it contains package scalar vars; e.g.:
12402
12403 f();
12404 my ($x, $y) = (2, $x_alias);
12405 sub f { $x = 1; *x_alias = \$x; }
12406
12407 * It contains other general elements, such as flattened or
12408 * spliced or single array or hash elements, e.g.
12409
12410 f();
12411 my ($x,$y) = @a; # or $a[0] or @a{@b} etc
12412
12413 sub f {
12414 ($x, $y) = (1,2);
12415 use feature 'refaliasing';
12416 \($a[0], $a[1]) = \($y,$x);
12417 }
12418
12419 It doesn't matter if the array/hash is lexical or package.
12420
12421 * it contains a function call that happens to be an lvalue
12422 sub which returns one or more of the above, e.g.
12423
12424 f();
12425 my ($x,$y) = f();
12426
12427 sub f : lvalue {
12428 ($x, $y) = (1,2);
12429 *x1 = \$x;
12430 $y, $x1;
12431 }
12432
12433 (so a sub call on the RHS should be treated the same
12434 as having a package var on the RHS).
12435
12436 * any other "dangerous" thing, such an op or built-in that
12437 returns one of the above, e.g. pp_preinc
12438
12439
12440 If RHS is not safe, what we can do however is at compile time flag
12441 that the LHS are all my declarations, and at run time check whether
12442 all the LHS have RC == 1, and if so skip the full scan.
12443
12444 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...;
12445
12446 Here the issue is whether there can be elements of @a on the RHS
12447 which will get prematurely freed when @a is cleared prior to
12448 assignment. This is only a problem if the aliasing mechanism
12449 is one which doesn't increase the refcount - only if RC == 1
12450 will the RHS element be prematurely freed.
12451
12452 Because the array/hash is being INTROed, it or its elements
12453 can't directly appear on the RHS:
12454
12455 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE
12456
12457 but can indirectly, e.g.:
12458
12459 my $r = f();
12460 my (@a) = @$r;
12461 sub f { @a = 1..3; \@a }
12462
12463 So if the RHS isn't safe as defined by (A), we must always
12464 mortalise and bump the ref count of any remaining RHS elements
12465 when assigning to a non-empty LHS aggregate.
12466
12467 Lexical scalars on the RHS aren't safe if they've been involved in
12468 aliasing, e.g.
12469
12470 use feature 'refaliasing';
12471
12472 f();
12473 \(my $lex) = \$pkg;
12474 my @a = ($lex,3); # equivalent to ($a[0],3)
12475
12476 sub f {
12477 @a = (1,2);
12478 \$pkg = \$a[0];
12479 }
12480
12481 Similarly with lexical arrays and hashes on the RHS:
12482
12483 f();
12484 my @b;
12485 my @a = (@b);
12486
12487 sub f {
12488 @a = (1,2);
12489 \$b[0] = \$a[1];
12490 \$b[1] = \$a[0];
12491 }
12492
12493
12494
12495 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g.
12496 my $a; ($a, my $b) = (....);
12497
12498 The difference between (B) and (C) is that it is now physically
12499 possible for the LHS vars to appear on the RHS too, where they
12500 are not reference counted; but in this case, the compile-time
12501 PL_generation sweep will detect such common vars.
12502
12503 So the rules for (C) differ from (B) in that if common vars are
12504 detected, the runtime "test RC==1" optimisation can no longer be used,
12505 and a full mark and sweep is required
12506
12507 D: As (C), but in addition the LHS may contain package vars.
12508
12509 Since package vars can be aliased without a corresponding refcount
12510 increase, all bets are off. It's only safe if (A). E.g.
12511
12512 my ($x, $y) = (1,2);
12513
12514 for $x_alias ($x) {
12515 ($x_alias, $y) = (3, $x); # whoops
12516 }
12517
12518 Ditto for LHS aggregate package vars.
12519
12520 E: Any other dangerous ops on LHS, e.g.
12521 (f(), $a[0], @$r) = (...);
12522
12523 this is similar to (E) in that all bets are off. In addition, it's
12524 impossible to determine at compile time whether the LHS
12525 contains a scalar or an aggregate, e.g.
12526
12527 sub f : lvalue { @a }
12528 (f()) = 1..3;
12529
12530* ---------------------------------------------------------
12531*/
12532
12533
12534/* A set of bit flags returned by S_aassign_scan(). Each flag indicates
12535 * that at least one of the things flagged was seen.
12536 */
12537
12538enum {
12539 AAS_MY_SCALAR = 0x001, /* my $scalar */
12540 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */
12541 AAS_LEX_SCALAR = 0x004, /* $lexical */
12542 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */
12543 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */
12544 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */
12545 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */
12546 AAS_DANGEROUS = 0x080, /* an op (other than the above)
12547 that's flagged OA_DANGEROUS */
12548 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's
12549 not in any of the categories above */
12550 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */
12551};
12552
12553
12554
12555/* helper function for S_aassign_scan().
12556 * check a PAD-related op for commonality and/or set its generation number.
12557 * Returns a boolean indicating whether its shared */
12558
12559static bool
12560S_aassign_padcheck(pTHX_ OP* o, bool rhs)
12561{
12562 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX)
12563 /* lexical used in aliasing */
12564 return TRUE;
12565
12566 if (rhs)
12567 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation);
12568 else
12569 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation);
12570
12571 return FALSE;
12572}
12573
12574
12575/*
12576 Helper function for OPpASSIGN_COMMON* detection in rpeep().
12577 It scans the left or right hand subtree of the aassign op, and returns a
12578 set of flags indicating what sorts of things it found there.
12579 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we
12580 set PL_generation on lexical vars; if the latter, we see if
12581 PL_generation matches.
12582 'top' indicates whether we're recursing or at the top level.
12583 'scalars_p' is a pointer to a counter of the number of scalar SVs seen.
12584 This fn will increment it by the number seen. It's not intended to
12585 be an accurate count (especially as many ops can push a variable
12586 number of SVs onto the stack); rather it's used as to test whether there
12587 can be at most 1 SV pushed; so it's only meanings are "0, 1, many".
12588*/
12589
12590static int
12591S_aassign_scan(pTHX_ OP* o, bool rhs, bool top, int *scalars_p)
12592{
12593 int flags = 0;
12594 bool kid_top = FALSE;
12595
12596 /* first, look for a solitary @_ on the RHS */
12597 if ( rhs
12598 && top
12599 && (o->op_flags & OPf_KIDS)
12600 && OP_TYPE_IS_OR_WAS(o, OP_LIST)
12601 ) {
12602 OP *kid = cUNOPo->op_first;
12603 if ( ( kid->op_type == OP_PUSHMARK
12604 || kid->op_type == OP_PADRANGE) /* ex-pushmark */
12605 && ((kid = OpSIBLING(kid)))
12606 && !OpHAS_SIBLING(kid)
12607 && kid->op_type == OP_RV2AV
12608 && !(kid->op_flags & OPf_REF)
12609 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
12610 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST)
12611 && ((kid = cUNOPx(kid)->op_first))
12612 && kid->op_type == OP_GV
12613 && cGVOPx_gv(kid) == PL_defgv
12614 )
12615 flags |= AAS_DEFAV;
12616 }
12617
12618 switch (o->op_type) {
12619 case OP_GVSV:
12620 (*scalars_p)++;
12621 return AAS_PKG_SCALAR;
12622
12623 case OP_PADAV:
12624 case OP_PADHV:
12625 (*scalars_p) += 2;
12626 /* if !top, could be e.g. @a[0,1] */
12627 if (top && (o->op_flags & OPf_REF))
12628 return (o->op_private & OPpLVAL_INTRO)
12629 ? AAS_MY_AGG : AAS_LEX_AGG;
12630 return AAS_DANGEROUS;
12631
12632 case OP_PADSV:
12633 {
12634 int comm = S_aassign_padcheck(aTHX_ o, rhs)
12635 ? AAS_LEX_SCALAR_COMM : 0;
12636 (*scalars_p)++;
12637 return (o->op_private & OPpLVAL_INTRO)
12638 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm);
12639 }
12640
12641 case OP_RV2AV:
12642 case OP_RV2HV:
12643 (*scalars_p) += 2;
12644 if (cUNOPx(o)->op_first->op_type != OP_GV)
12645 return AAS_DANGEROUS; /* @{expr}, %{expr} */
12646 /* @pkg, %pkg */
12647 /* if !top, could be e.g. @a[0,1] */
12648 if (top && (o->op_flags & OPf_REF))
12649 return AAS_PKG_AGG;
12650 return AAS_DANGEROUS;
12651
12652 case OP_RV2SV:
12653 (*scalars_p)++;
12654 if (cUNOPx(o)->op_first->op_type != OP_GV) {
12655 (*scalars_p) += 2;
12656 return AAS_DANGEROUS; /* ${expr} */
12657 }
12658 return AAS_PKG_SCALAR; /* $pkg */
12659
12660 case OP_SPLIT:
12661 if (o->op_private & OPpSPLIT_ASSIGN) {
12662 /* the assign in @a = split() has been optimised away
12663 * and the @a attached directly to the split op
12664 * Treat the array as appearing on the RHS, i.e.
12665 * ... = (@a = split)
12666 * is treated like
12667 * ... = @a;
12668 */
12669
12670 if (o->op_flags & OPf_STACKED)
12671 /* @{expr} = split() - the array expression is tacked
12672 * on as an extra child to split - process kid */
12673 return S_aassign_scan(aTHX_ cLISTOPo->op_last, rhs,
12674 top, scalars_p);
12675
12676 /* ... else array is directly attached to split op */
12677 (*scalars_p) += 2;
12678 if (PL_op->op_private & OPpSPLIT_LEX)
12679 return (o->op_private & OPpLVAL_INTRO)
12680 ? AAS_MY_AGG : AAS_LEX_AGG;
12681 else
12682 return AAS_PKG_AGG;
12683 }
12684 (*scalars_p)++;
12685 /* other args of split can't be returned */
12686 return AAS_SAFE_SCALAR;
12687
12688 case OP_UNDEF:
12689 /* undef counts as a scalar on the RHS:
12690 * (undef, $x) = ...; # only 1 scalar on LHS: always safe
12691 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe
12692 */
12693 if (rhs)
12694 (*scalars_p)++;
12695 flags = AAS_SAFE_SCALAR;
12696 break;
12697
12698 case OP_PUSHMARK:
12699 case OP_STUB:
12700 /* these are all no-ops; they don't push a potentially common SV
12701 * onto the stack, so they are neither AAS_DANGEROUS nor
12702 * AAS_SAFE_SCALAR */
12703 return 0;
12704
12705 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */
12706 break;
12707
12708 case OP_NULL:
12709 case OP_LIST:
12710 /* these do nothing but may have children; but their children
12711 * should also be treated as top-level */
12712 kid_top = top;
12713 break;
12714
12715 default:
12716 if (PL_opargs[o->op_type] & OA_DANGEROUS) {
12717 (*scalars_p) += 2;
12718 flags = AAS_DANGEROUS;
12719 break;
12720 }
12721
12722 if ( (PL_opargs[o->op_type] & OA_TARGLEX)
12723 && (o->op_private & OPpTARGET_MY))
12724 {
12725 (*scalars_p)++;
12726 return S_aassign_padcheck(aTHX_ o, rhs)
12727 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR;
12728 }
12729
12730 /* if its an unrecognised, non-dangerous op, assume that it
12731 * it the cause of at least one safe scalar */
12732 (*scalars_p)++;
12733 flags = AAS_SAFE_SCALAR;
12734 break;
12735 }
12736
12737 /* XXX this assumes that all other ops are "transparent" - i.e. that
12738 * they can return some of their children. While this true for e.g.
12739 * sort and grep, it's not true for e.g. map. We really need a
12740 * 'transparent' flag added to regen/opcodes
12741 */
12742 if (o->op_flags & OPf_KIDS) {
12743 OP *kid;
12744 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
12745 flags |= S_aassign_scan(aTHX_ kid, rhs, kid_top, scalars_p);
12746 }
12747 return flags;
12748}
12749
12750
12751/* Check for in place reverse and sort assignments like "@a = reverse @a"
12752 and modify the optree to make them work inplace */
12753
12754STATIC void
12755S_inplace_aassign(pTHX_ OP *o) {
12756
12757 OP *modop, *modop_pushmark;
12758 OP *oright;
12759 OP *oleft, *oleft_pushmark;
12760
12761 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
12762
12763 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
12764
12765 assert(cUNOPo->op_first->op_type == OP_NULL);
12766 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
12767 assert(modop_pushmark->op_type == OP_PUSHMARK);
12768 modop = OpSIBLING(modop_pushmark);
12769
12770 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
12771 return;
12772
12773 /* no other operation except sort/reverse */
12774 if (OpHAS_SIBLING(modop))
12775 return;
12776
12777 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
12778 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return;
12779
12780 if (modop->op_flags & OPf_STACKED) {
12781 /* skip sort subroutine/block */
12782 assert(oright->op_type == OP_NULL);
12783 oright = OpSIBLING(oright);
12784 }
12785
12786 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL);
12787 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first;
12788 assert(oleft_pushmark->op_type == OP_PUSHMARK);
12789 oleft = OpSIBLING(oleft_pushmark);
12790
12791 /* Check the lhs is an array */
12792 if (!oleft ||
12793 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
12794 || OpHAS_SIBLING(oleft)
12795 || (oleft->op_private & OPpLVAL_INTRO)
12796 )
12797 return;
12798
12799 /* Only one thing on the rhs */
12800 if (OpHAS_SIBLING(oright))
12801 return;
12802
12803 /* check the array is the same on both sides */
12804 if (oleft->op_type == OP_RV2AV) {
12805 if (oright->op_type != OP_RV2AV
12806 || !cUNOPx(oright)->op_first
12807 || cUNOPx(oright)->op_first->op_type != OP_GV
12808 || cUNOPx(oleft )->op_first->op_type != OP_GV
12809 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
12810 cGVOPx_gv(cUNOPx(oright)->op_first)
12811 )
12812 return;
12813 }
12814 else if (oright->op_type != OP_PADAV
12815 || oright->op_targ != oleft->op_targ
12816 )
12817 return;
12818
12819 /* This actually is an inplace assignment */
12820
12821 modop->op_private |= OPpSORT_INPLACE;
12822
12823 /* transfer MODishness etc from LHS arg to RHS arg */
12824 oright->op_flags = oleft->op_flags;
12825
12826 /* remove the aassign op and the lhs */
12827 op_null(o);
12828 op_null(oleft_pushmark);
12829 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
12830 op_null(cUNOPx(oleft)->op_first);
12831 op_null(oleft);
12832}
12833
12834
12835
12836/* S_maybe_multideref(): given an op_next chain of ops beginning at 'start'
12837 * that potentially represent a series of one or more aggregate derefs
12838 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert
12839 * the whole chain to a single OP_MULTIDEREF op (maybe with a few
12840 * additional ops left in too).
12841 *
12842 * The caller will have already verified that the first few ops in the
12843 * chain following 'start' indicate a multideref candidate, and will have
12844 * set 'orig_o' to the point further on in the chain where the first index
12845 * expression (if any) begins. 'orig_action' specifies what type of
12846 * beginning has already been determined by the ops between start..orig_o
12847 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc).
12848 *
12849 * 'hints' contains any hints flags that need adding (currently just
12850 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller.
12851 */
12852
12853STATIC void
12854S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints)
12855{
12856 dVAR;
12857 int pass;
12858 UNOP_AUX_item *arg_buf = NULL;
12859 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */
12860 int index_skip = -1; /* don't output index arg on this action */
12861
12862 /* similar to regex compiling, do two passes; the first pass
12863 * determines whether the op chain is convertible and calculates the
12864 * buffer size; the second pass populates the buffer and makes any
12865 * changes necessary to ops (such as moving consts to the pad on
12866 * threaded builds).
12867 *
12868 * NB: for things like Coverity, note that both passes take the same
12869 * path through the logic tree (except for 'if (pass)' bits), since
12870 * both passes are following the same op_next chain; and in
12871 * particular, if it would return early on the second pass, it would
12872 * already have returned early on the first pass.
12873 */
12874 for (pass = 0; pass < 2; pass++) {
12875 OP *o = orig_o;
12876 UV action = orig_action;
12877 OP *first_elem_op = NULL; /* first seen aelem/helem */
12878 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */
12879 int action_count = 0; /* number of actions seen so far */
12880 int action_ix = 0; /* action_count % (actions per IV) */
12881 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */
12882 bool is_last = FALSE; /* no more derefs to follow */
12883 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */
12884 UNOP_AUX_item *arg = arg_buf;
12885 UNOP_AUX_item *action_ptr = arg_buf;
12886
12887 if (pass)
12888 action_ptr->uv = 0;
12889 arg++;
12890
12891 switch (action) {
12892 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
12893 case MDEREF_HV_gvhv_helem:
12894 next_is_hash = TRUE;
12895 /* FALLTHROUGH */
12896 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
12897 case MDEREF_AV_gvav_aelem:
12898 if (pass) {
12899#ifdef USE_ITHREADS
12900 arg->pad_offset = cPADOPx(start)->op_padix;
12901 /* stop it being swiped when nulled */
12902 cPADOPx(start)->op_padix = 0;
12903#else
12904 arg->sv = cSVOPx(start)->op_sv;
12905 cSVOPx(start)->op_sv = NULL;
12906#endif
12907 }
12908 arg++;
12909 break;
12910
12911 case MDEREF_HV_padhv_helem:
12912 case MDEREF_HV_padsv_vivify_rv2hv_helem:
12913 next_is_hash = TRUE;
12914 /* FALLTHROUGH */
12915 case MDEREF_AV_padav_aelem:
12916 case MDEREF_AV_padsv_vivify_rv2av_aelem:
12917 if (pass) {
12918 arg->pad_offset = start->op_targ;
12919 /* we skip setting op_targ = 0 for now, since the intact
12920 * OP_PADXV is needed by S_check_hash_fields_and_hekify */
12921 reset_start_targ = TRUE;
12922 }
12923 arg++;
12924 break;
12925
12926 case MDEREF_HV_pop_rv2hv_helem:
12927 next_is_hash = TRUE;
12928 /* FALLTHROUGH */
12929 case MDEREF_AV_pop_rv2av_aelem:
12930 break;
12931
12932 default:
12933 NOT_REACHED; /* NOTREACHED */
12934 return;
12935 }
12936
12937 while (!is_last) {
12938 /* look for another (rv2av/hv; get index;
12939 * aelem/helem/exists/delele) sequence */
12940
12941 OP *kid;
12942 bool is_deref;
12943 bool ok;
12944 UV index_type = MDEREF_INDEX_none;
12945
12946 if (action_count) {
12947 /* if this is not the first lookup, consume the rv2av/hv */
12948
12949 /* for N levels of aggregate lookup, we normally expect
12950 * that the first N-1 [ah]elem ops will be flagged as
12951 * /DEREF (so they autovivifiy if necessary), and the last
12952 * lookup op not to be.
12953 * For other things (like @{$h{k1}{k2}}) extra scope or
12954 * leave ops can appear, so abandon the effort in that
12955 * case */
12956 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
12957 return;
12958
12959 /* rv2av or rv2hv sKR/1 */
12960
12961 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
12962 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
12963 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
12964 return;
12965
12966 /* at this point, we wouldn't expect any of these
12967 * possible private flags:
12968 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO
12969 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only)
12970 */
12971 ASSUME(!(o->op_private &
12972 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING)));
12973
12974 hints = (o->op_private & OPpHINT_STRICT_REFS);
12975
12976 /* make sure the type of the previous /DEREF matches the
12977 * type of the next lookup */
12978 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV));
12979 top_op = o;
12980
12981 action = next_is_hash
12982 ? MDEREF_HV_vivify_rv2hv_helem
12983 : MDEREF_AV_vivify_rv2av_aelem;
12984 o = o->op_next;
12985 }
12986
12987 /* if this is the second pass, and we're at the depth where
12988 * previously we encountered a non-simple index expression,
12989 * stop processing the index at this point */
12990 if (action_count != index_skip) {
12991
12992 /* look for one or more simple ops that return an array
12993 * index or hash key */
12994
12995 switch (o->op_type) {
12996 case OP_PADSV:
12997 /* it may be a lexical var index */
12998 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS
12999 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
13000 ASSUME(!(o->op_private &
13001 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
13002
13003 if ( OP_GIMME(o,0) == G_SCALAR
13004 && !(o->op_flags & (OPf_REF|OPf_MOD))
13005 && o->op_private == 0)
13006 {
13007 if (pass)
13008 arg->pad_offset = o->op_targ;
13009 arg++;
13010 index_type = MDEREF_INDEX_padsv;
13011 o = o->op_next;
13012 }
13013 break;
13014
13015 case OP_CONST:
13016 if (next_is_hash) {
13017 /* it's a constant hash index */
13018 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK)))
13019 /* "use constant foo => FOO; $h{+foo}" for
13020 * some weird FOO, can leave you with constants
13021 * that aren't simple strings. It's not worth
13022 * the extra hassle for those edge cases */
13023 break;
13024
13025 if (pass) {
13026 UNOP *rop = NULL;
13027 OP * helem_op = o->op_next;
13028
13029 ASSUME( helem_op->op_type == OP_HELEM
13030 || helem_op->op_type == OP_NULL);
13031 if (helem_op->op_type == OP_HELEM) {
13032 rop = (UNOP*)(((BINOP*)helem_op)->op_first);
13033 if ( helem_op->op_private & OPpLVAL_INTRO
13034 || rop->op_type != OP_RV2HV
13035 )
13036 rop = NULL;
13037 }
13038 S_check_hash_fields_and_hekify(aTHX_ rop, cSVOPo);
13039
13040#ifdef USE_ITHREADS
13041 /* Relocate sv to the pad for thread safety */
13042 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
13043 arg->pad_offset = o->op_targ;
13044 o->op_targ = 0;
13045#else
13046 arg->sv = cSVOPx_sv(o);
13047#endif
13048 }
13049 }
13050 else {
13051 /* it's a constant array index */
13052 IV iv;
13053 SV *ix_sv = cSVOPo->op_sv;
13054 if (!SvIOK(ix_sv))
13055 break;
13056 iv = SvIV(ix_sv);
13057
13058 if ( action_count == 0
13059 && iv >= -128
13060 && iv <= 127
13061 && ( action == MDEREF_AV_padav_aelem
13062 || action == MDEREF_AV_gvav_aelem)
13063 )
13064 maybe_aelemfast = TRUE;
13065
13066 if (pass) {
13067 arg->iv = iv;
13068 SvREFCNT_dec_NN(cSVOPo->op_sv);
13069 }
13070 }
13071 if (pass)
13072 /* we've taken ownership of the SV */
13073 cSVOPo->op_sv = NULL;
13074 arg++;
13075 index_type = MDEREF_INDEX_const;
13076 o = o->op_next;
13077 break;
13078
13079 case OP_GV:
13080 /* it may be a package var index */
13081
13082 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_SPECIAL)));
13083 ASSUME(!(o->op_private & ~(OPpEARLY_CV)));
13084 if ( (o->op_flags &~ OPf_SPECIAL) != OPf_WANT_SCALAR
13085 || o->op_private != 0
13086 )
13087 break;
13088
13089 kid = o->op_next;
13090 if (kid->op_type != OP_RV2SV)
13091 break;
13092
13093 ASSUME(!(kid->op_flags &
13094 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF
13095 |OPf_SPECIAL|OPf_PARENS)));
13096 ASSUME(!(kid->op_private &
13097 ~(OPpARG1_MASK
13098 |OPpHINT_STRICT_REFS|OPpOUR_INTRO
13099 |OPpDEREF|OPpLVAL_INTRO)));
13100 if( (kid->op_flags &~ OPf_PARENS)
13101 != (OPf_WANT_SCALAR|OPf_KIDS)
13102 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS))
13103 )
13104 break;
13105
13106 if (pass) {
13107#ifdef USE_ITHREADS
13108 arg->pad_offset = cPADOPx(o)->op_padix;
13109 /* stop it being swiped when nulled */
13110 cPADOPx(o)->op_padix = 0;
13111#else
13112 arg->sv = cSVOPx(o)->op_sv;
13113 cSVOPo->op_sv = NULL;
13114#endif
13115 }
13116 arg++;
13117 index_type = MDEREF_INDEX_gvsv;
13118 o = kid->op_next;
13119 break;
13120
13121 } /* switch */
13122 } /* action_count != index_skip */
13123
13124 action |= index_type;
13125
13126
13127 /* at this point we have either:
13128 * * detected what looks like a simple index expression,
13129 * and expect the next op to be an [ah]elem, or
13130 * an nulled [ah]elem followed by a delete or exists;
13131 * * found a more complex expression, so something other
13132 * than the above follows.
13133 */
13134
13135 /* possibly an optimised away [ah]elem (where op_next is
13136 * exists or delete) */
13137 if (o->op_type == OP_NULL)
13138 o = o->op_next;
13139
13140 /* at this point we're looking for an OP_AELEM, OP_HELEM,
13141 * OP_EXISTS or OP_DELETE */
13142
13143 /* if something like arybase (a.k.a $[ ) is in scope,
13144 * abandon optimisation attempt */
13145 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
13146 && PL_check[o->op_type] != Perl_ck_null)
13147 return;
13148 /* similarly for customised exists and delete */
13149 if ( (o->op_type == OP_EXISTS)
13150 && PL_check[o->op_type] != Perl_ck_exists)
13151 return;
13152 if ( (o->op_type == OP_DELETE)
13153 && PL_check[o->op_type] != Perl_ck_delete)
13154 return;
13155
13156 if ( o->op_type != OP_AELEM
13157 || (o->op_private &
13158 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB))
13159 )
13160 maybe_aelemfast = FALSE;
13161
13162 /* look for aelem/helem/exists/delete. If it's not the last elem
13163 * lookup, it *must* have OPpDEREF_AV/HV, but not many other
13164 * flags; if it's the last, then it mustn't have
13165 * OPpDEREF_AV/HV, but may have lots of other flags, like
13166 * OPpLVAL_INTRO etc
13167 */
13168
13169 if ( index_type == MDEREF_INDEX_none
13170 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM
13171 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE)
13172 )
13173 ok = FALSE;
13174 else {
13175 /* we have aelem/helem/exists/delete with valid simple index */
13176
13177 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
13178 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV
13179 || (o->op_private & OPpDEREF) == OPpDEREF_HV);
13180
13181 if (is_deref) {
13182 ASSUME(!(o->op_flags &
13183 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS)));
13184 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF)));
13185
13186 ok = (o->op_flags &~ OPf_PARENS)
13187 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD)
13188 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK));
13189 }
13190 else if (o->op_type == OP_EXISTS) {
13191 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
13192 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
13193 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB)));
13194 ok = !(o->op_private & ~OPpARG1_MASK);
13195 }
13196 else if (o->op_type == OP_DELETE) {
13197 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
13198 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
13199 ASSUME(!(o->op_private &
13200 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO)));
13201 /* don't handle slices or 'local delete'; the latter
13202 * is fairly rare, and has a complex runtime */
13203 ok = !(o->op_private & ~OPpARG1_MASK);
13204 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM))
13205 /* skip handling run-tome error */
13206 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL));
13207 }
13208 else {
13209 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM);
13210 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD
13211 |OPf_PARENS|OPf_REF|OPf_SPECIAL)));
13212 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB
13213 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO)));
13214 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV;
13215 }
13216 }
13217
13218 if (ok) {
13219 if (!first_elem_op)
13220 first_elem_op = o;
13221 top_op = o;
13222 if (is_deref) {
13223 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV);
13224 o = o->op_next;
13225 }
13226 else {
13227 is_last = TRUE;
13228 action |= MDEREF_FLAG_last;
13229 }
13230 }
13231 else {
13232 /* at this point we have something that started
13233 * promisingly enough (with rv2av or whatever), but failed
13234 * to find a simple index followed by an
13235 * aelem/helem/exists/delete. If this is the first action,
13236 * give up; but if we've already seen at least one
13237 * aelem/helem, then keep them and add a new action with
13238 * MDEREF_INDEX_none, which causes it to do the vivify
13239 * from the end of the previous lookup, and do the deref,
13240 * but stop at that point. So $a[0][expr] will do one
13241 * av_fetch, vivify and deref, then continue executing at
13242 * expr */
13243 if (!action_count)
13244 return;
13245 is_last = TRUE;
13246 index_skip = action_count;
13247 action |= MDEREF_FLAG_last;
13248 if (index_type != MDEREF_INDEX_none)
13249 arg--;
13250 }
13251
13252 if (pass)
13253 action_ptr->uv |= (action << (action_ix * MDEREF_SHIFT));
13254 action_ix++;
13255 action_count++;
13256 /* if there's no space for the next action, create a new slot
13257 * for it *before* we start adding args for that action */
13258 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) {
13259 action_ptr = arg;
13260 if (pass)
13261 arg->uv = 0;
13262 arg++;
13263 action_ix = 0;
13264 }
13265 } /* while !is_last */
13266
13267 /* success! */
13268
13269 if (pass) {
13270 OP *mderef;
13271 OP *p, *q;
13272
13273 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf);
13274 if (index_skip == -1) {
13275 mderef->op_flags = o->op_flags
13276 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0));
13277 if (o->op_type == OP_EXISTS)
13278 mderef->op_private = OPpMULTIDEREF_EXISTS;
13279 else if (o->op_type == OP_DELETE)
13280 mderef->op_private = OPpMULTIDEREF_DELETE;
13281 else
13282 mderef->op_private = o->op_private
13283 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO);
13284 }
13285 /* accumulate strictness from every level (although I don't think
13286 * they can actually vary) */
13287 mderef->op_private |= hints;
13288
13289 /* integrate the new multideref op into the optree and the
13290 * op_next chain.
13291 *
13292 * In general an op like aelem or helem has two child
13293 * sub-trees: the aggregate expression (a_expr) and the
13294 * index expression (i_expr):
13295 *
13296 * aelem
13297 * |
13298 * a_expr - i_expr
13299 *
13300 * The a_expr returns an AV or HV, while the i-expr returns an
13301 * index. In general a multideref replaces most or all of a
13302 * multi-level tree, e.g.
13303 *
13304 * exists
13305 * |
13306 * ex-aelem
13307 * |
13308 * rv2av - i_expr1
13309 * |
13310 * helem
13311 * |
13312 * rv2hv - i_expr2
13313 * |
13314 * aelem
13315 * |
13316 * a_expr - i_expr3
13317 *
13318 * With multideref, all the i_exprs will be simple vars or
13319 * constants, except that i_expr1 may be arbitrary in the case
13320 * of MDEREF_INDEX_none.
13321 *
13322 * The bottom-most a_expr will be either:
13323 * 1) a simple var (so padXv or gv+rv2Xv);
13324 * 2) a simple scalar var dereferenced (e.g. $r->[0]):
13325 * so a simple var with an extra rv2Xv;
13326 * 3) or an arbitrary expression.
13327 *
13328 * 'start', the first op in the execution chain, will point to
13329 * 1),2): the padXv or gv op;
13330 * 3): the rv2Xv which forms the last op in the a_expr
13331 * execution chain, and the top-most op in the a_expr
13332 * subtree.
13333 *
13334 * For all cases, the 'start' node is no longer required,
13335 * but we can't free it since one or more external nodes
13336 * may point to it. E.g. consider
13337 * $h{foo} = $a ? $b : $c
13338 * Here, both the op_next and op_other branches of the
13339 * cond_expr point to the gv[*h] of the hash expression, so
13340 * we can't free the 'start' op.
13341 *
13342 * For expr->[...], we need to save the subtree containing the
13343 * expression; for the other cases, we just need to save the
13344 * start node.
13345 * So in all cases, we null the start op and keep it around by
13346 * making it the child of the multideref op; for the expr->
13347 * case, the expr will be a subtree of the start node.
13348 *
13349 * So in the simple 1,2 case the optree above changes to
13350 *
13351 * ex-exists
13352 * |
13353 * multideref
13354 * |
13355 * ex-gv (or ex-padxv)
13356 *
13357 * with the op_next chain being
13358 *
13359 * -> ex-gv -> multideref -> op-following-ex-exists ->
13360 *
13361 * In the 3 case, we have
13362 *
13363 * ex-exists
13364 * |
13365 * multideref
13366 * |
13367 * ex-rv2xv
13368 * |
13369 * rest-of-a_expr
13370 * subtree
13371 *
13372 * and
13373 *
13374 * -> rest-of-a_expr subtree ->
13375 * ex-rv2xv -> multideref -> op-following-ex-exists ->
13376 *
13377 *
13378 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none,
13379 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the
13380 * multideref attached as the child, e.g.
13381 *
13382 * exists
13383 * |
13384 * ex-aelem
13385 * |
13386 * ex-rv2av - i_expr1
13387 * |
13388 * multideref
13389 * |
13390 * ex-whatever
13391 *
13392 */
13393
13394 /* if we free this op, don't free the pad entry */
13395 if (reset_start_targ)
13396 start->op_targ = 0;
13397
13398
13399 /* Cut the bit we need to save out of the tree and attach to
13400 * the multideref op, then free the rest of the tree */
13401
13402 /* find parent of node to be detached (for use by splice) */
13403 p = first_elem_op;
13404 if ( orig_action == MDEREF_AV_pop_rv2av_aelem
13405 || orig_action == MDEREF_HV_pop_rv2hv_helem)
13406 {
13407 /* there is an arbitrary expression preceding us, e.g.
13408 * expr->[..]? so we need to save the 'expr' subtree */
13409 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE)
13410 p = cUNOPx(p)->op_first;
13411 ASSUME( start->op_type == OP_RV2AV
13412 || start->op_type == OP_RV2HV);
13413 }
13414 else {
13415 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem
13416 * above for exists/delete. */
13417 while ( (p->op_flags & OPf_KIDS)
13418 && cUNOPx(p)->op_first != start
13419 )
13420 p = cUNOPx(p)->op_first;
13421 }
13422 ASSUME(cUNOPx(p)->op_first == start);
13423
13424 /* detach from main tree, and re-attach under the multideref */
13425 op_sibling_splice(mderef, NULL, 0,
13426 op_sibling_splice(p, NULL, 1, NULL));
13427 op_null(start);
13428
13429 start->op_next = mderef;
13430
13431 mderef->op_next = index_skip == -1 ? o->op_next : o;
13432
13433 /* excise and free the original tree, and replace with
13434 * the multideref op */
13435 p = op_sibling_splice(top_op, NULL, -1, mderef);
13436 while (p) {
13437 q = OpSIBLING(p);
13438 op_free(p);
13439 p = q;
13440 }
13441 op_null(top_op);
13442 }
13443 else {
13444 Size_t size = arg - arg_buf;
13445
13446 if (maybe_aelemfast && action_count == 1)
13447 return;
13448
13449 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc(
13450 sizeof(UNOP_AUX_item) * (size + 1));
13451 /* for dumping etc: store the length in a hidden first slot;
13452 * we set the op_aux pointer to the second slot */
13453 arg_buf->uv = size;
13454 arg_buf++;
13455 }
13456 } /* for (pass = ...) */
13457}
13458
13459/* See if the ops following o are such that o will always be executed in
13460 * boolean context: that is, the SV which o pushes onto the stack will
13461 * only ever be used by later ops with SvTRUE(sv) or similar.
13462 * If so, set a suitable private flag on o. Normally this will be
13463 * bool_flag; but if it's only possible to determine booleaness at run
13464 * time (e.g. sub f { ....; (%h || $y) }), then set maybe_flag instead.
13465 */
13466
13467static void
13468S_check_for_bool_cxt(pTHX_ OP*o, U8 bool_flag, U8 maybe_flag)
13469{
13470 OP *lop;
13471
13472 assert((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR);
13473
13474 lop = o->op_next;
13475
13476 while (lop) {
13477 switch (lop->op_type) {
13478 case OP_NULL:
13479 case OP_SCALAR:
13480 break;
13481
13482 /* these two consume the stack argument in the scalar case,
13483 * and treat it as a boolean in the non linenumber case */
13484 case OP_FLIP:
13485 case OP_FLOP:
13486 if ( ((lop->op_flags & OPf_WANT) == OPf_WANT_LIST)
13487 || (lop->op_private & OPpFLIP_LINENUM))
13488 {
13489 lop = NULL;
13490 break;
13491 }
13492 /* FALLTHROUGH */
13493 /* these never leave the original value on the stack */
13494 case OP_NOT:
13495 case OP_XOR:
13496 case OP_COND_EXPR:
13497 case OP_GREPWHILE:
13498 o->op_private |= bool_flag;
13499 lop = NULL;
13500 break;
13501
13502 /* OR DOR and AND evaluate their arg as a boolean, but then may
13503 * leave the original scalar value on the stack when following the
13504 * op_next route. If not in void context, we need to ensure
13505 * that whatever follows consumes the arg only in boolean context
13506 * too.
13507 */
13508 case OP_OR:
13509 case OP_DOR:
13510 case OP_AND:
13511 if ((lop->op_flags & OPf_WANT) == OPf_WANT_VOID) {
13512 o->op_private |= bool_flag;
13513 lop = NULL;
13514 }
13515 else if (!(lop->op_flags & OPf_WANT)) {
13516 /* unknown context - decide at runtime */
13517 o->op_private |= maybe_flag;
13518 lop = NULL;
13519 }
13520 break;
13521
13522 default:
13523 lop = NULL;
13524 break;
13525 }
13526
13527 if (lop)
13528 lop = lop->op_next;
13529 }
13530}
13531
13532
13533
13534/* mechanism for deferring recursion in rpeep() */
13535
13536#define MAX_DEFERRED 4
13537
13538#define DEFER(o) \
13539 STMT_START { \
13540 if (defer_ix == (MAX_DEFERRED-1)) { \
13541 OP **defer = defer_queue[defer_base]; \
13542 CALL_RPEEP(*defer); \
13543 S_prune_chain_head(defer); \
13544 defer_base = (defer_base + 1) % MAX_DEFERRED; \
13545 defer_ix--; \
13546 } \
13547 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
13548 } STMT_END
13549
13550#define IS_AND_OP(o) (o->op_type == OP_AND)
13551#define IS_OR_OP(o) (o->op_type == OP_OR)
13552
13553
13554/* A peephole optimizer. We visit the ops in the order they're to execute.
13555 * See the comments at the top of this file for more details about when
13556 * peep() is called */
13557
13558void
13559Perl_rpeep(pTHX_ OP *o)
13560{
13561 dVAR;
13562 OP* oldop = NULL;
13563 OP* oldoldop = NULL;
13564 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
13565 int defer_base = 0;
13566 int defer_ix = -1;
13567
13568 if (!o || o->op_opt)
13569 return;
13570
13571 assert(o->op_type != OP_FREED);
13572
13573 ENTER;
13574 SAVEOP();
13575 SAVEVPTR(PL_curcop);
13576 for (;; o = o->op_next) {
13577 if (o && o->op_opt)
13578 o = NULL;
13579 if (!o) {
13580 while (defer_ix >= 0) {
13581 OP **defer =
13582 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
13583 CALL_RPEEP(*defer);
13584 S_prune_chain_head(defer);
13585 }
13586 break;
13587 }
13588
13589 redo:
13590
13591 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */
13592 assert(!oldoldop || oldoldop->op_next == oldop);
13593 assert(!oldop || oldop->op_next == o);
13594
13595 /* By default, this op has now been optimised. A couple of cases below
13596 clear this again. */
13597 o->op_opt = 1;
13598 PL_op = o;
13599
13600 /* look for a series of 1 or more aggregate derefs, e.g.
13601 * $a[1]{foo}[$i]{$k}
13602 * and replace with a single OP_MULTIDEREF op.
13603 * Each index must be either a const, or a simple variable,
13604 *
13605 * First, look for likely combinations of starting ops,
13606 * corresponding to (global and lexical variants of)
13607 * $a[...] $h{...}
13608 * $r->[...] $r->{...}
13609 * (preceding expression)->[...]
13610 * (preceding expression)->{...}
13611 * and if so, call maybe_multideref() to do a full inspection
13612 * of the op chain and if appropriate, replace with an
13613 * OP_MULTIDEREF
13614 */
13615 {
13616 UV action;
13617 OP *o2 = o;
13618 U8 hints = 0;
13619
13620 switch (o2->op_type) {
13621 case OP_GV:
13622 /* $pkg[..] : gv[*pkg]
13623 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */
13624
13625 /* Fail if there are new op flag combinations that we're
13626 * not aware of, rather than:
13627 * * silently failing to optimise, or
13628 * * silently optimising the flag away.
13629 * If this ASSUME starts failing, examine what new flag
13630 * has been added to the op, and decide whether the
13631 * optimisation should still occur with that flag, then
13632 * update the code accordingly. This applies to all the
13633 * other ASSUMEs in the block of code too.
13634 */
13635 ASSUME(!(o2->op_flags &
13636 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL)));
13637 ASSUME(!(o2->op_private & ~OPpEARLY_CV));
13638
13639 o2 = o2->op_next;
13640
13641 if (o2->op_type == OP_RV2AV) {
13642 action = MDEREF_AV_gvav_aelem;
13643 goto do_deref;
13644 }
13645
13646 if (o2->op_type == OP_RV2HV) {
13647 action = MDEREF_HV_gvhv_helem;
13648 goto do_deref;
13649 }
13650
13651 if (o2->op_type != OP_RV2SV)
13652 break;
13653
13654 /* at this point we've seen gv,rv2sv, so the only valid
13655 * construct left is $pkg->[] or $pkg->{} */
13656
13657 ASSUME(!(o2->op_flags & OPf_STACKED));
13658 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13659 != (OPf_WANT_SCALAR|OPf_MOD))
13660 break;
13661
13662 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS
13663 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO)));
13664 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO))
13665 break;
13666 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV
13667 && (o2->op_private & OPpDEREF) != OPpDEREF_HV)
13668 break;
13669
13670 o2 = o2->op_next;
13671 if (o2->op_type == OP_RV2AV) {
13672 action = MDEREF_AV_gvsv_vivify_rv2av_aelem;
13673 goto do_deref;
13674 }
13675 if (o2->op_type == OP_RV2HV) {
13676 action = MDEREF_HV_gvsv_vivify_rv2hv_helem;
13677 goto do_deref;
13678 }
13679 break;
13680
13681 case OP_PADSV:
13682 /* $lex->[...]: padsv[$lex] sM/DREFAV */
13683
13684 ASSUME(!(o2->op_flags &
13685 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL)));
13686 if ((o2->op_flags &
13687 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13688 != (OPf_WANT_SCALAR|OPf_MOD))
13689 break;
13690
13691 ASSUME(!(o2->op_private &
13692 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
13693 /* skip if state or intro, or not a deref */
13694 if ( o2->op_private != OPpDEREF_AV
13695 && o2->op_private != OPpDEREF_HV)
13696 break;
13697
13698 o2 = o2->op_next;
13699 if (o2->op_type == OP_RV2AV) {
13700 action = MDEREF_AV_padsv_vivify_rv2av_aelem;
13701 goto do_deref;
13702 }
13703 if (o2->op_type == OP_RV2HV) {
13704 action = MDEREF_HV_padsv_vivify_rv2hv_helem;
13705 goto do_deref;
13706 }
13707 break;
13708
13709 case OP_PADAV:
13710 case OP_PADHV:
13711 /* $lex[..]: padav[@lex:1,2] sR *
13712 * or $lex{..}: padhv[%lex:1,2] sR */
13713 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS|
13714 OPf_REF|OPf_SPECIAL)));
13715 if ((o2->op_flags &
13716 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13717 != (OPf_WANT_SCALAR|OPf_REF))
13718 break;
13719 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF))
13720 break;
13721 /* OPf_PARENS isn't currently used in this case;
13722 * if that changes, let us know! */
13723 ASSUME(!(o2->op_flags & OPf_PARENS));
13724
13725 /* at this point, we wouldn't expect any of the remaining
13726 * possible private flags:
13727 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL,
13728 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB
13729 *
13730 * OPpSLICEWARNING shouldn't affect runtime
13731 */
13732 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING)));
13733
13734 action = o2->op_type == OP_PADAV
13735 ? MDEREF_AV_padav_aelem
13736 : MDEREF_HV_padhv_helem;
13737 o2 = o2->op_next;
13738 S_maybe_multideref(aTHX_ o, o2, action, 0);
13739 break;
13740
13741
13742 case OP_RV2AV:
13743 case OP_RV2HV:
13744 action = o2->op_type == OP_RV2AV
13745 ? MDEREF_AV_pop_rv2av_aelem
13746 : MDEREF_HV_pop_rv2hv_helem;
13747 /* FALLTHROUGH */
13748 do_deref:
13749 /* (expr)->[...]: rv2av sKR/1;
13750 * (expr)->{...}: rv2hv sKR/1; */
13751
13752 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV);
13753
13754 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
13755 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL)));
13756 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
13757 break;
13758
13759 /* at this point, we wouldn't expect any of these
13760 * possible private flags:
13761 * OPpMAYBE_LVSUB, OPpLVAL_INTRO
13762 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only)
13763 */
13764 ASSUME(!(o2->op_private &
13765 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING
13766 |OPpOUR_INTRO)));
13767 hints |= (o2->op_private & OPpHINT_STRICT_REFS);
13768
13769 o2 = o2->op_next;
13770
13771 S_maybe_multideref(aTHX_ o, o2, action, hints);
13772 break;
13773
13774 default:
13775 break;
13776 }
13777 }
13778
13779
13780 switch (o->op_type) {
13781 case OP_DBSTATE:
13782 PL_curcop = ((COP*)o); /* for warnings */
13783 break;
13784 case OP_NEXTSTATE:
13785 PL_curcop = ((COP*)o); /* for warnings */
13786
13787 /* Optimise a "return ..." at the end of a sub to just be "...".
13788 * This saves 2 ops. Before:
13789 * 1 <;> nextstate(main 1 -e:1) v ->2
13790 * 4 <@> return K ->5
13791 * 2 <0> pushmark s ->3
13792 * - <1> ex-rv2sv sK/1 ->4
13793 * 3 <#> gvsv[*cat] s ->4
13794 *
13795 * After:
13796 * - <@> return K ->-
13797 * - <0> pushmark s ->2
13798 * - <1> ex-rv2sv sK/1 ->-
13799 * 2 <$> gvsv(*cat) s ->3
13800 */
13801 {
13802 OP *next = o->op_next;
13803 OP *sibling = OpSIBLING(o);
13804 if ( OP_TYPE_IS(next, OP_PUSHMARK)
13805 && OP_TYPE_IS(sibling, OP_RETURN)
13806 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
13807 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
13808 ||OP_TYPE_IS(sibling->op_next->op_next,
13809 OP_LEAVESUBLV))
13810 && cUNOPx(sibling)->op_first == next
13811 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next
13812 && next->op_next
13813 ) {
13814 /* Look through the PUSHMARK's siblings for one that
13815 * points to the RETURN */
13816 OP *top = OpSIBLING(next);
13817 while (top && top->op_next) {
13818 if (top->op_next == sibling) {
13819 top->op_next = sibling->op_next;
13820 o->op_next = next->op_next;
13821 break;
13822 }
13823 top = OpSIBLING(top);
13824 }
13825 }
13826 }
13827
13828 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
13829 *
13830 * This latter form is then suitable for conversion into padrange
13831 * later on. Convert:
13832 *
13833 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
13834 *
13835 * into:
13836 *
13837 * nextstate1 -> listop -> nextstate3
13838 * / \
13839 * pushmark -> padop1 -> padop2
13840 */
13841 if (o->op_next && (
13842 o->op_next->op_type == OP_PADSV
13843 || o->op_next->op_type == OP_PADAV
13844 || o->op_next->op_type == OP_PADHV
13845 )
13846 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
13847 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
13848 && o->op_next->op_next->op_next && (
13849 o->op_next->op_next->op_next->op_type == OP_PADSV
13850 || o->op_next->op_next->op_next->op_type == OP_PADAV
13851 || o->op_next->op_next->op_next->op_type == OP_PADHV
13852 )
13853 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
13854 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
13855 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
13856 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
13857 ) {
13858 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm;
13859
13860 pad1 = o->op_next;
13861 ns2 = pad1->op_next;
13862 pad2 = ns2->op_next;
13863 ns3 = pad2->op_next;
13864
13865 /* we assume here that the op_next chain is the same as
13866 * the op_sibling chain */
13867 assert(OpSIBLING(o) == pad1);
13868 assert(OpSIBLING(pad1) == ns2);
13869 assert(OpSIBLING(ns2) == pad2);
13870 assert(OpSIBLING(pad2) == ns3);
13871
13872 /* excise and delete ns2 */
13873 op_sibling_splice(NULL, pad1, 1, NULL);
13874 op_free(ns2);
13875
13876 /* excise pad1 and pad2 */
13877 op_sibling_splice(NULL, o, 2, NULL);
13878
13879 /* create new listop, with children consisting of:
13880 * a new pushmark, pad1, pad2. */
13881 newop = newLISTOP(OP_LIST, 0, pad1, pad2);
13882 newop->op_flags |= OPf_PARENS;
13883 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
13884
13885 /* insert newop between o and ns3 */
13886 op_sibling_splice(NULL, o, 0, newop);
13887
13888 /*fixup op_next chain */
13889 newpm = cUNOPx(newop)->op_first; /* pushmark */
13890 o ->op_next = newpm;
13891 newpm->op_next = pad1;
13892 pad1 ->op_next = pad2;
13893 pad2 ->op_next = newop; /* listop */
13894 newop->op_next = ns3;
13895
13896 /* Ensure pushmark has this flag if padops do */
13897 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) {
13898 newpm->op_flags |= OPf_MOD;
13899 }
13900
13901 break;
13902 }
13903
13904 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
13905 to carry two labels. For now, take the easier option, and skip
13906 this optimisation if the first NEXTSTATE has a label. */
13907 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
13908 OP *nextop = o->op_next;
13909 while (nextop && nextop->op_type == OP_NULL)
13910 nextop = nextop->op_next;
13911
13912 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
13913 op_null(o);
13914 if (oldop)
13915 oldop->op_next = nextop;
13916 o = nextop;
13917 /* Skip (old)oldop assignment since the current oldop's
13918 op_next already points to the next op. */
13919 goto redo;
13920 }
13921 }
13922 break;
13923
13924 case OP_CONCAT:
13925 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
13926 if (o->op_next->op_private & OPpTARGET_MY) {
13927 if (o->op_flags & OPf_STACKED) /* chained concats */
13928 break; /* ignore_optimization */
13929 else {
13930 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
13931 o->op_targ = o->op_next->op_targ;
13932 o->op_next->op_targ = 0;
13933 o->op_private |= OPpTARGET_MY;
13934 }
13935 }
13936 op_null(o->op_next);
13937 }
13938 break;
13939 case OP_STUB:
13940 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
13941 break; /* Scalar stub must produce undef. List stub is noop */
13942 }
13943 goto nothin;
13944 case OP_NULL:
13945 if (o->op_targ == OP_NEXTSTATE
13946 || o->op_targ == OP_DBSTATE)
13947 {
13948 PL_curcop = ((COP*)o);
13949 }
13950 /* XXX: We avoid setting op_seq here to prevent later calls
13951 to rpeep() from mistakenly concluding that optimisation
13952 has already occurred. This doesn't fix the real problem,
13953 though (See 20010220.007 (#5874)). AMS 20010719 */
13954 /* op_seq functionality is now replaced by op_opt */
13955 o->op_opt = 0;
13956 /* FALLTHROUGH */
13957 case OP_SCALAR:
13958 case OP_LINESEQ:
13959 case OP_SCOPE:
13960 nothin:
13961 if (oldop) {
13962 oldop->op_next = o->op_next;
13963 o->op_opt = 0;
13964 continue;
13965 }
13966 break;
13967
13968 case OP_PUSHMARK:
13969
13970 /* Given
13971 5 repeat/DOLIST
13972 3 ex-list
13973 1 pushmark
13974 2 scalar or const
13975 4 const[0]
13976 convert repeat into a stub with no kids.
13977 */
13978 if (o->op_next->op_type == OP_CONST
13979 || ( o->op_next->op_type == OP_PADSV
13980 && !(o->op_next->op_private & OPpLVAL_INTRO))
13981 || ( o->op_next->op_type == OP_GV
13982 && o->op_next->op_next->op_type == OP_RV2SV
13983 && !(o->op_next->op_next->op_private
13984 & (OPpLVAL_INTRO|OPpOUR_INTRO))))
13985 {
13986 const OP *kid = o->op_next->op_next;
13987 if (o->op_next->op_type == OP_GV)
13988 kid = kid->op_next;
13989 /* kid is now the ex-list. */
13990 if (kid->op_type == OP_NULL
13991 && (kid = kid->op_next)->op_type == OP_CONST
13992 /* kid is now the repeat count. */
13993 && kid->op_next->op_type == OP_REPEAT
13994 && kid->op_next->op_private & OPpREPEAT_DOLIST
13995 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST
13996 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0
13997 && oldop)
13998 {
13999 o = kid->op_next; /* repeat */
14000 oldop->op_next = o;
14001 op_free(cBINOPo->op_first);
14002 op_free(cBINOPo->op_last );
14003 o->op_flags &=~ OPf_KIDS;
14004 /* stub is a baseop; repeat is a binop */
14005 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP));
14006 OpTYPE_set(o, OP_STUB);
14007 o->op_private = 0;
14008 break;
14009 }
14010 }
14011
14012 /* Convert a series of PAD ops for my vars plus support into a
14013 * single padrange op. Basically
14014 *
14015 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
14016 *
14017 * becomes, depending on circumstances, one of
14018 *
14019 * padrange ----------------------------------> (list) -> rest
14020 * padrange --------------------------------------------> rest
14021 *
14022 * where all the pad indexes are sequential and of the same type
14023 * (INTRO or not).
14024 * We convert the pushmark into a padrange op, then skip
14025 * any other pad ops, and possibly some trailing ops.
14026 * Note that we don't null() the skipped ops, to make it
14027 * easier for Deparse to undo this optimisation (and none of
14028 * the skipped ops are holding any resourses). It also makes
14029 * it easier for find_uninit_var(), as it can just ignore
14030 * padrange, and examine the original pad ops.
14031 */
14032 {
14033 OP *p;
14034 OP *followop = NULL; /* the op that will follow the padrange op */
14035 U8 count = 0;
14036 U8 intro = 0;
14037 PADOFFSET base = 0; /* init only to stop compiler whining */
14038 bool gvoid = 0; /* init only to stop compiler whining */
14039 bool defav = 0; /* seen (...) = @_ */
14040 bool reuse = 0; /* reuse an existing padrange op */
14041
14042 /* look for a pushmark -> gv[_] -> rv2av */
14043
14044 {
14045 OP *rv2av, *q;
14046 p = o->op_next;
14047 if ( p->op_type == OP_GV
14048 && cGVOPx_gv(p) == PL_defgv
14049 && (rv2av = p->op_next)
14050 && rv2av->op_type == OP_RV2AV
14051 && !(rv2av->op_flags & OPf_REF)
14052 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
14053 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
14054 ) {
14055 q = rv2av->op_next;
14056 if (q->op_type == OP_NULL)
14057 q = q->op_next;
14058 if (q->op_type == OP_PUSHMARK) {
14059 defav = 1;
14060 p = q;
14061 }
14062 }
14063 }
14064 if (!defav) {
14065 p = o;
14066 }
14067
14068 /* scan for PAD ops */
14069
14070 for (p = p->op_next; p; p = p->op_next) {
14071 if (p->op_type == OP_NULL)
14072 continue;
14073
14074 if (( p->op_type != OP_PADSV
14075 && p->op_type != OP_PADAV
14076 && p->op_type != OP_PADHV
14077 )
14078 /* any private flag other than INTRO? e.g. STATE */
14079 || (p->op_private & ~OPpLVAL_INTRO)
14080 )
14081 break;
14082
14083 /* let $a[N] potentially be optimised into AELEMFAST_LEX
14084 * instead */
14085 if ( p->op_type == OP_PADAV
14086 && p->op_next
14087 && p->op_next->op_type == OP_CONST
14088 && p->op_next->op_next
14089 && p->op_next->op_next->op_type == OP_AELEM
14090 )
14091 break;
14092
14093 /* for 1st padop, note what type it is and the range
14094 * start; for the others, check that it's the same type
14095 * and that the targs are contiguous */
14096 if (count == 0) {
14097 intro = (p->op_private & OPpLVAL_INTRO);
14098 base = p->op_targ;
14099 gvoid = OP_GIMME(p,0) == G_VOID;
14100 }
14101 else {
14102 if ((p->op_private & OPpLVAL_INTRO) != intro)
14103 break;
14104 /* Note that you'd normally expect targs to be
14105 * contiguous in my($a,$b,$c), but that's not the case
14106 * when external modules start doing things, e.g.
14107 * Function::Parameters */
14108 if (p->op_targ != base + count)
14109 break;
14110 assert(p->op_targ == base + count);
14111 /* Either all the padops or none of the padops should
14112 be in void context. Since we only do the optimisa-
14113 tion for av/hv when the aggregate itself is pushed
14114 on to the stack (one item), there is no need to dis-
14115 tinguish list from scalar context. */
14116 if (gvoid != (OP_GIMME(p,0) == G_VOID))
14117 break;
14118 }
14119
14120 /* for AV, HV, only when we're not flattening */
14121 if ( p->op_type != OP_PADSV
14122 && !gvoid
14123 && !(p->op_flags & OPf_REF)
14124 )
14125 break;
14126
14127 if (count >= OPpPADRANGE_COUNTMASK)
14128 break;
14129
14130 /* there's a biggest base we can fit into a
14131 * SAVEt_CLEARPADRANGE in pp_padrange.
14132 * (The sizeof() stuff will be constant-folded, and is
14133 * intended to avoid getting "comparison is always false"
14134 * compiler warnings. See the comments above
14135 * MEM_WRAP_CHECK for more explanation on why we do this
14136 * in a weird way to avoid compiler warnings.)
14137 */
14138 if ( intro
14139 && (8*sizeof(base) >
14140 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT
14141 ? (Size_t)base
14142 : (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
14143 ) >
14144 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
14145 )
14146 break;
14147
14148 /* Success! We've got another valid pad op to optimise away */
14149 count++;
14150 followop = p->op_next;
14151 }
14152
14153 if (count < 1 || (count == 1 && !defav))
14154 break;
14155
14156 /* pp_padrange in specifically compile-time void context
14157 * skips pushing a mark and lexicals; in all other contexts
14158 * (including unknown till runtime) it pushes a mark and the
14159 * lexicals. We must be very careful then, that the ops we
14160 * optimise away would have exactly the same effect as the
14161 * padrange.
14162 * In particular in void context, we can only optimise to
14163 * a padrange if we see the complete sequence
14164 * pushmark, pad*v, ...., list
14165 * which has the net effect of leaving the markstack as it
14166 * was. Not pushing onto the stack (whereas padsv does touch
14167 * the stack) makes no difference in void context.
14168 */
14169 assert(followop);
14170 if (gvoid) {
14171 if (followop->op_type == OP_LIST
14172 && OP_GIMME(followop,0) == G_VOID
14173 )
14174 {
14175 followop = followop->op_next; /* skip OP_LIST */
14176
14177 /* consolidate two successive my(...);'s */
14178
14179 if ( oldoldop
14180 && oldoldop->op_type == OP_PADRANGE
14181 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
14182 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
14183 && !(oldoldop->op_flags & OPf_SPECIAL)
14184 ) {
14185 U8 old_count;
14186 assert(oldoldop->op_next == oldop);
14187 assert( oldop->op_type == OP_NEXTSTATE
14188 || oldop->op_type == OP_DBSTATE);
14189 assert(oldop->op_next == o);
14190
14191 old_count
14192 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
14193
14194 /* Do not assume pad offsets for $c and $d are con-
14195 tiguous in
14196 my ($a,$b,$c);
14197 my ($d,$e,$f);
14198 */
14199 if ( oldoldop->op_targ + old_count == base
14200 && old_count < OPpPADRANGE_COUNTMASK - count) {
14201 base = oldoldop->op_targ;
14202 count += old_count;
14203 reuse = 1;
14204 }
14205 }
14206
14207 /* if there's any immediately following singleton
14208 * my var's; then swallow them and the associated
14209 * nextstates; i.e.
14210 * my ($a,$b); my $c; my $d;
14211 * is treated as
14212 * my ($a,$b,$c,$d);
14213 */
14214
14215 while ( ((p = followop->op_next))
14216 && ( p->op_type == OP_PADSV
14217 || p->op_type == OP_PADAV
14218 || p->op_type == OP_PADHV)
14219 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
14220 && (p->op_private & OPpLVAL_INTRO) == intro
14221 && !(p->op_private & ~OPpLVAL_INTRO)
14222 && p->op_next
14223 && ( p->op_next->op_type == OP_NEXTSTATE
14224 || p->op_next->op_type == OP_DBSTATE)
14225 && count < OPpPADRANGE_COUNTMASK
14226 && base + count == p->op_targ
14227 ) {
14228 count++;
14229 followop = p->op_next;
14230 }
14231 }
14232 else
14233 break;
14234 }
14235
14236 if (reuse) {
14237 assert(oldoldop->op_type == OP_PADRANGE);
14238 oldoldop->op_next = followop;
14239 oldoldop->op_private = (intro | count);
14240 o = oldoldop;
14241 oldop = NULL;
14242 oldoldop = NULL;
14243 }
14244 else {
14245 /* Convert the pushmark into a padrange.
14246 * To make Deparse easier, we guarantee that a padrange was
14247 * *always* formerly a pushmark */
14248 assert(o->op_type == OP_PUSHMARK);
14249 o->op_next = followop;
14250 OpTYPE_set(o, OP_PADRANGE);
14251 o->op_targ = base;
14252 /* bit 7: INTRO; bit 6..0: count */
14253 o->op_private = (intro | count);
14254 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
14255 | gvoid * OPf_WANT_VOID
14256 | (defav ? OPf_SPECIAL : 0));
14257 }
14258 break;
14259 }
14260
14261 case OP_RV2HV:
14262 case OP_PADHV:
14263 /* see if %h is used in boolean context */
14264 if ((o->op_flags & OPf_WANT) == OPf_WANT_SCALAR)
14265 S_check_for_bool_cxt(aTHX_ o, OPpTRUEBOOL, OPpMAYBE_TRUEBOOL);
14266 if (o->op_type != OP_PADHV)
14267 break;
14268 /* FALLTHROUGH */
14269 case OP_PADAV:
14270 case OP_PADSV:
14271 /* Skip over state($x) in void context. */
14272 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO)
14273 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID)
14274 {
14275 oldop->op_next = o->op_next;
14276 goto redo_nextstate;
14277 }
14278 if (o->op_type != OP_PADAV)
14279 break;
14280 /* FALLTHROUGH */
14281 case OP_GV:
14282 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
14283 OP* const pop = (o->op_type == OP_PADAV) ?
14284 o->op_next : o->op_next->op_next;
14285 IV i;
14286 if (pop && pop->op_type == OP_CONST &&
14287 ((PL_op = pop->op_next)) &&
14288 pop->op_next->op_type == OP_AELEM &&
14289 !(pop->op_next->op_private &
14290 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
14291 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
14292 {
14293 GV *gv;
14294 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
14295 no_bareword_allowed(pop);
14296 if (o->op_type == OP_GV)
14297 op_null(o->op_next);
14298 op_null(pop->op_next);
14299 op_null(pop);
14300 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
14301 o->op_next = pop->op_next->op_next;
14302 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
14303 o->op_private = (U8)i;
14304 if (o->op_type == OP_GV) {
14305 gv = cGVOPo_gv;
14306 GvAVn(gv);
14307 o->op_type = OP_AELEMFAST;
14308 }
14309 else
14310 o->op_type = OP_AELEMFAST_LEX;
14311 }
14312 if (o->op_type != OP_GV)
14313 break;
14314 }
14315
14316 /* Remove $foo from the op_next chain in void context. */
14317 if (oldop
14318 && ( o->op_next->op_type == OP_RV2SV
14319 || o->op_next->op_type == OP_RV2AV
14320 || o->op_next->op_type == OP_RV2HV )
14321 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
14322 && !(o->op_next->op_private & OPpLVAL_INTRO))
14323 {
14324 oldop->op_next = o->op_next->op_next;
14325 /* Reprocess the previous op if it is a nextstate, to
14326 allow double-nextstate optimisation. */
14327 redo_nextstate:
14328 if (oldop->op_type == OP_NEXTSTATE) {
14329 oldop->op_opt = 0;
14330 o = oldop;
14331 oldop = oldoldop;
14332 oldoldop = NULL;
14333 goto redo;
14334 }
14335 o = oldop->op_next;
14336 goto redo;
14337 }
14338 else if (o->op_next->op_type == OP_RV2SV) {
14339 if (!(o->op_next->op_private & OPpDEREF)) {
14340 op_null(o->op_next);
14341 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
14342 | OPpOUR_INTRO);
14343 o->op_next = o->op_next->op_next;
14344 OpTYPE_set(o, OP_GVSV);
14345 }
14346 }
14347 else if (o->op_next->op_type == OP_READLINE
14348 && o->op_next->op_next->op_type == OP_CONCAT
14349 && (o->op_next->op_next->op_flags & OPf_STACKED))
14350 {
14351 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
14352 OpTYPE_set(o, OP_RCATLINE);
14353 o->op_flags |= OPf_STACKED;
14354 op_null(o->op_next->op_next);
14355 op_null(o->op_next);
14356 }
14357
14358 break;
14359
14360 case OP_NOT:
14361 break;
14362
14363 case OP_AND:
14364 case OP_OR:
14365 case OP_DOR:
14366 while (cLOGOP->op_other->op_type == OP_NULL)
14367 cLOGOP->op_other = cLOGOP->op_other->op_next;
14368 while (o->op_next && ( o->op_type == o->op_next->op_type
14369 || o->op_next->op_type == OP_NULL))
14370 o->op_next = o->op_next->op_next;
14371
14372 /* If we're an OR and our next is an AND in void context, we'll
14373 follow its op_other on short circuit, same for reverse.
14374 We can't do this with OP_DOR since if it's true, its return
14375 value is the underlying value which must be evaluated
14376 by the next op. */
14377 if (o->op_next &&
14378 (
14379 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
14380 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
14381 )
14382 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
14383 ) {
14384 o->op_next = ((LOGOP*)o->op_next)->op_other;
14385 }
14386 DEFER(cLOGOP->op_other);
14387 o->op_opt = 1;
14388 break;
14389
14390 case OP_COND_EXPR:
14391 case OP_MAPWHILE:
14392 case OP_GREPWHILE:
14393 case OP_ANDASSIGN:
14394 case OP_ORASSIGN:
14395 case OP_DORASSIGN:
14396 case OP_RANGE:
14397 case OP_ONCE:
14398 case OP_ARGDEFELEM:
14399 while (cLOGOP->op_other->op_type == OP_NULL)
14400 cLOGOP->op_other = cLOGOP->op_other->op_next;
14401 DEFER(cLOGOP->op_other);
14402 break;
14403
14404 case OP_ENTERLOOP:
14405 case OP_ENTERITER:
14406 while (cLOOP->op_redoop->op_type == OP_NULL)
14407 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
14408 while (cLOOP->op_nextop->op_type == OP_NULL)
14409 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
14410 while (cLOOP->op_lastop->op_type == OP_NULL)
14411 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
14412 /* a while(1) loop doesn't have an op_next that escapes the
14413 * loop, so we have to explicitly follow the op_lastop to
14414 * process the rest of the code */
14415 DEFER(cLOOP->op_lastop);
14416 break;
14417
14418 case OP_ENTERTRY:
14419 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
14420 DEFER(cLOGOPo->op_other);
14421 break;
14422
14423 case OP_SUBST:
14424 assert(!(cPMOP->op_pmflags & PMf_ONCE));
14425 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
14426 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
14427 cPMOP->op_pmstashstartu.op_pmreplstart
14428 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
14429 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
14430 break;
14431
14432 case OP_SORT: {
14433 OP *oright;
14434
14435 if (o->op_flags & OPf_SPECIAL) {
14436 /* first arg is a code block */
14437 OP * const nullop = OpSIBLING(cLISTOP->op_first);
14438 OP * kid = cUNOPx(nullop)->op_first;
14439
14440 assert(nullop->op_type == OP_NULL);
14441 assert(kid->op_type == OP_SCOPE
14442 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
14443 /* since OP_SORT doesn't have a handy op_other-style
14444 * field that can point directly to the start of the code
14445 * block, store it in the otherwise-unused op_next field
14446 * of the top-level OP_NULL. This will be quicker at
14447 * run-time, and it will also allow us to remove leading
14448 * OP_NULLs by just messing with op_nexts without
14449 * altering the basic op_first/op_sibling layout. */
14450 kid = kLISTOP->op_first;
14451 assert(
14452 (kid->op_type == OP_NULL
14453 && ( kid->op_targ == OP_NEXTSTATE
14454 || kid->op_targ == OP_DBSTATE ))
14455 || kid->op_type == OP_STUB
14456 || kid->op_type == OP_ENTER
14457 || (PL_parser && PL_parser->error_count));
14458 nullop->op_next = kid->op_next;
14459 DEFER(nullop->op_next);
14460 }
14461
14462 /* check that RHS of sort is a single plain array */
14463 oright = cUNOPo->op_first;
14464 if (!oright || oright->op_type != OP_PUSHMARK)
14465 break;
14466
14467 if (o->op_private & OPpSORT_INPLACE)
14468 break;
14469
14470 /* reverse sort ... can be optimised. */
14471 if (!OpHAS_SIBLING(cUNOPo)) {
14472 /* Nothing follows us on the list. */
14473 OP * const reverse = o->op_next;
14474
14475 if (reverse->op_type == OP_REVERSE &&
14476 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
14477 OP * const pushmark = cUNOPx(reverse)->op_first;
14478 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
14479 && (OpSIBLING(cUNOPx(pushmark)) == o)) {
14480 /* reverse -> pushmark -> sort */
14481 o->op_private |= OPpSORT_REVERSE;
14482 op_null(reverse);
14483 pushmark->op_next = oright->op_next;
14484 op_null(oright);
14485 }
14486 }
14487 }
14488
14489 break;
14490 }
14491
14492 case OP_REVERSE: {
14493 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
14494 OP *gvop = NULL;
14495 LISTOP *enter, *exlist;
14496
14497 if (o->op_private & OPpSORT_INPLACE)
14498 break;
14499
14500 enter = (LISTOP *) o->op_next;
14501 if (!enter)
14502 break;
14503 if (enter->op_type == OP_NULL) {
14504 enter = (LISTOP *) enter->op_next;
14505 if (!enter)
14506 break;
14507 }
14508 /* for $a (...) will have OP_GV then OP_RV2GV here.
14509 for (...) just has an OP_GV. */
14510 if (enter->op_type == OP_GV) {
14511 gvop = (OP *) enter;
14512 enter = (LISTOP *) enter->op_next;
14513 if (!enter)
14514 break;
14515 if (enter->op_type == OP_RV2GV) {
14516 enter = (LISTOP *) enter->op_next;
14517 if (!enter)
14518 break;
14519 }
14520 }
14521
14522 if (enter->op_type != OP_ENTERITER)
14523 break;
14524
14525 iter = enter->op_next;
14526 if (!iter || iter->op_type != OP_ITER)
14527 break;
14528
14529 expushmark = enter->op_first;
14530 if (!expushmark || expushmark->op_type != OP_NULL
14531 || expushmark->op_targ != OP_PUSHMARK)
14532 break;
14533
14534 exlist = (LISTOP *) OpSIBLING(expushmark);
14535 if (!exlist || exlist->op_type != OP_NULL
14536 || exlist->op_targ != OP_LIST)
14537 break;
14538
14539 if (exlist->op_last != o) {
14540 /* Mmm. Was expecting to point back to this op. */
14541 break;
14542 }
14543 theirmark = exlist->op_first;
14544 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
14545 break;
14546
14547 if (OpSIBLING(theirmark) != o) {
14548 /* There's something between the mark and the reverse, eg
14549 for (1, reverse (...))
14550 so no go. */
14551 break;
14552 }
14553
14554 ourmark = ((LISTOP *)o)->op_first;
14555 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
14556 break;
14557
14558 ourlast = ((LISTOP *)o)->op_last;
14559 if (!ourlast || ourlast->op_next != o)
14560 break;
14561
14562 rv2av = OpSIBLING(ourmark);
14563 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av)
14564 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
14565 /* We're just reversing a single array. */
14566 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
14567 enter->op_flags |= OPf_STACKED;
14568 }
14569
14570 /* We don't have control over who points to theirmark, so sacrifice
14571 ours. */
14572 theirmark->op_next = ourmark->op_next;
14573 theirmark->op_flags = ourmark->op_flags;
14574 ourlast->op_next = gvop ? gvop : (OP *) enter;
14575 op_null(ourmark);
14576 op_null(o);
14577 enter->op_private |= OPpITER_REVERSED;
14578 iter->op_private |= OPpITER_REVERSED;
14579
14580 oldoldop = NULL;
14581 oldop = ourlast;
14582 o = oldop->op_next;
14583 goto redo;
14584 NOT_REACHED; /* NOTREACHED */
14585 break;
14586 }
14587
14588 case OP_QR:
14589 case OP_MATCH:
14590 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
14591 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
14592 }
14593 break;
14594
14595 case OP_RUNCV:
14596 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)
14597 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb)))
14598 {
14599 SV *sv;
14600 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
14601 else {
14602 sv = newRV((SV *)PL_compcv);
14603 sv_rvweaken(sv);
14604 SvREADONLY_on(sv);
14605 }
14606 OpTYPE_set(o, OP_CONST);
14607 o->op_flags |= OPf_SPECIAL;
14608 cSVOPo->op_sv = sv;
14609 }
14610 break;
14611
14612 case OP_SASSIGN:
14613 if (OP_GIMME(o,0) == G_VOID
14614 || ( o->op_next->op_type == OP_LINESEQ
14615 && ( o->op_next->op_next->op_type == OP_LEAVESUB
14616 || ( o->op_next->op_next->op_type == OP_RETURN
14617 && !CvLVALUE(PL_compcv)))))
14618 {
14619 OP *right = cBINOP->op_first;
14620 if (right) {
14621 /* sassign
14622 * RIGHT
14623 * substr
14624 * pushmark
14625 * arg1
14626 * arg2
14627 * ...
14628 * becomes
14629 *
14630 * ex-sassign
14631 * substr
14632 * pushmark
14633 * RIGHT
14634 * arg1
14635 * arg2
14636 * ...
14637 */
14638 OP *left = OpSIBLING(right);
14639 if (left->op_type == OP_SUBSTR
14640 && (left->op_private & 7) < 4) {
14641 op_null(o);
14642 /* cut out right */
14643 op_sibling_splice(o, NULL, 1, NULL);
14644 /* and insert it as second child of OP_SUBSTR */
14645 op_sibling_splice(left, cBINOPx(left)->op_first, 0,
14646 right);
14647 left->op_private |= OPpSUBSTR_REPL_FIRST;
14648 left->op_flags =
14649 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
14650 }
14651 }
14652 }
14653 break;
14654
14655 case OP_AASSIGN: {
14656 int l, r, lr, lscalars, rscalars;
14657
14658 /* handle common vars detection, e.g. ($a,$b) = ($b,$a).
14659 Note that we do this now rather than in newASSIGNOP(),
14660 since only by now are aliased lexicals flagged as such
14661
14662 See the essay "Common vars in list assignment" above for
14663 the full details of the rationale behind all the conditions
14664 below.
14665
14666 PL_generation sorcery:
14667 To detect whether there are common vars, the global var
14668 PL_generation is incremented for each assign op we scan.
14669 Then we run through all the lexical variables on the LHS,
14670 of the assignment, setting a spare slot in each of them to
14671 PL_generation. Then we scan the RHS, and if any lexicals
14672 already have that value, we know we've got commonality.
14673 Also, if the generation number is already set to
14674 PERL_INT_MAX, then the variable is involved in aliasing, so
14675 we also have potential commonality in that case.
14676 */
14677
14678 PL_generation++;
14679 /* scan LHS */
14680 lscalars = 0;
14681 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, 1, &lscalars);
14682 /* scan RHS */
14683 rscalars = 0;
14684 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, 1, &rscalars);
14685 lr = (l|r);
14686
14687
14688 /* After looking for things which are *always* safe, this main
14689 * if/else chain selects primarily based on the type of the
14690 * LHS, gradually working its way down from the more dangerous
14691 * to the more restrictive and thus safer cases */
14692
14693 if ( !l /* () = ....; */
14694 || !r /* .... = (); */
14695 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */
14696 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */
14697 || (lscalars < 2) /* ($x, undef) = ... */
14698 ) {
14699 NOOP; /* always safe */
14700 }
14701 else if (l & AAS_DANGEROUS) {
14702 /* always dangerous */
14703 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14704 o->op_private |= OPpASSIGN_COMMON_AGG;
14705 }
14706 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) {
14707 /* package vars are always dangerous - too many
14708 * aliasing possibilities */
14709 if (l & AAS_PKG_SCALAR)
14710 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14711 if (l & AAS_PKG_AGG)
14712 o->op_private |= OPpASSIGN_COMMON_AGG;
14713 }
14714 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG
14715 |AAS_LEX_SCALAR|AAS_LEX_AGG))
14716 {
14717 /* LHS contains only lexicals and safe ops */
14718
14719 if (l & (AAS_MY_AGG|AAS_LEX_AGG))
14720 o->op_private |= OPpASSIGN_COMMON_AGG;
14721
14722 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) {
14723 if (lr & AAS_LEX_SCALAR_COMM)
14724 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14725 else if ( !(l & AAS_LEX_SCALAR)
14726 && (r & AAS_DEFAV))
14727 {
14728 /* falsely mark
14729 * my (...) = @_
14730 * as scalar-safe for performance reasons.
14731 * (it will still have been marked _AGG if necessary */
14732 NOOP;
14733 }
14734 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS))
14735 /* if there are only lexicals on the LHS and no
14736 * common ones on the RHS, then we assume that the
14737 * only way those lexicals could also get
14738 * on the RHS is via some sort of dereffing or
14739 * closure, e.g.
14740 * $r = \$lex;
14741 * ($lex, $x) = (1, $$r)
14742 * and in this case we assume the var must have
14743 * a bumped ref count. So if its ref count is 1,
14744 * it must only be on the LHS.
14745 */
14746 o->op_private |= OPpASSIGN_COMMON_RC1;
14747 }
14748 }
14749
14750 /* ... = ($x)
14751 * may have to handle aggregate on LHS, but we can't
14752 * have common scalars. */
14753 if (rscalars < 2)
14754 o->op_private &=
14755 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1);
14756
14757 break;
14758 }
14759
14760 case OP_CUSTOM: {
14761 Perl_cpeep_t cpeep =
14762 XopENTRYCUSTOM(o, xop_peep);
14763 if (cpeep)
14764 cpeep(aTHX_ o, oldop);
14765 break;
14766 }
14767
14768 }
14769 /* did we just null the current op? If so, re-process it to handle
14770 * eliding "empty" ops from the chain */
14771 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
14772 o->op_opt = 0;
14773 o = oldop;
14774 }
14775 else {
14776 oldoldop = oldop;
14777 oldop = o;
14778 }
14779 }
14780 LEAVE;
14781}
14782
14783void
14784Perl_peep(pTHX_ OP *o)
14785{
14786 CALL_RPEEP(o);
14787}
14788
14789/*
14790=head1 Custom Operators
14791
14792=for apidoc Ao||custom_op_xop
14793Return the XOP structure for a given custom op. This macro should be
14794considered internal to C<OP_NAME> and the other access macros: use them instead.
14795This macro does call a function. Prior
14796to 5.19.6, this was implemented as a
14797function.
14798
14799=cut
14800*/
14801
14802XOPRETANY
14803Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
14804{
14805 SV *keysv;
14806 HE *he = NULL;
14807 XOP *xop;
14808
14809 static const XOP xop_null = { 0, 0, 0, 0, 0 };
14810
14811 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
14812 assert(o->op_type == OP_CUSTOM);
14813
14814 /* This is wrong. It assumes a function pointer can be cast to IV,
14815 * which isn't guaranteed, but this is what the old custom OP code
14816 * did. In principle it should be safer to Copy the bytes of the
14817 * pointer into a PV: since the new interface is hidden behind
14818 * functions, this can be changed later if necessary. */
14819 /* Change custom_op_xop if this ever happens */
14820 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
14821
14822 if (PL_custom_ops)
14823 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
14824
14825 /* assume noone will have just registered a desc */
14826 if (!he && PL_custom_op_names &&
14827 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
14828 ) {
14829 const char *pv;
14830 STRLEN l;
14831
14832 /* XXX does all this need to be shared mem? */
14833 Newxz(xop, 1, XOP);
14834 pv = SvPV(HeVAL(he), l);
14835 XopENTRY_set(xop, xop_name, savepvn(pv, l));
14836 if (PL_custom_op_descs &&
14837 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
14838 ) {
14839 pv = SvPV(HeVAL(he), l);
14840 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
14841 }
14842 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
14843 }
14844 else {
14845 if (!he)
14846 xop = (XOP *)&xop_null;
14847 else
14848 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
14849 }
14850 {
14851 XOPRETANY any;
14852 if(field == XOPe_xop_ptr) {
14853 any.xop_ptr = xop;
14854 } else {
14855 const U32 flags = XopFLAGS(xop);
14856 if(flags & field) {
14857 switch(field) {
14858 case XOPe_xop_name:
14859 any.xop_name = xop->xop_name;
14860 break;
14861 case XOPe_xop_desc:
14862 any.xop_desc = xop->xop_desc;
14863 break;
14864 case XOPe_xop_class:
14865 any.xop_class = xop->xop_class;
14866 break;
14867 case XOPe_xop_peep:
14868 any.xop_peep = xop->xop_peep;
14869 break;
14870 default:
14871 NOT_REACHED; /* NOTREACHED */
14872 break;
14873 }
14874 } else {
14875 switch(field) {
14876 case XOPe_xop_name:
14877 any.xop_name = XOPd_xop_name;
14878 break;
14879 case XOPe_xop_desc:
14880 any.xop_desc = XOPd_xop_desc;
14881 break;
14882 case XOPe_xop_class:
14883 any.xop_class = XOPd_xop_class;
14884 break;
14885 case XOPe_xop_peep:
14886 any.xop_peep = XOPd_xop_peep;
14887 break;
14888 default:
14889 NOT_REACHED; /* NOTREACHED */
14890 break;
14891 }
14892 }
14893 }
14894 /* On some platforms (HP-UX, IA64) gcc emits a warning for this function:
14895 * op.c: In function 'Perl_custom_op_get_field':
14896 * op.c:...: warning: 'any.xop_name' may be used uninitialized in this function [-Wmaybe-uninitialized]
14897 * This is because on those platforms (with -DEBUGGING) NOT_REACHED
14898 * expands to assert(0), which expands to ((0) ? (void)0 :
14899 * __assert(...)), and gcc doesn't know that __assert can never return. */
14900 return any;
14901 }
14902}
14903
14904/*
14905=for apidoc Ao||custom_op_register
14906Register a custom op. See L<perlguts/"Custom Operators">.
14907
14908=cut
14909*/
14910
14911void
14912Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
14913{
14914 SV *keysv;
14915
14916 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
14917
14918 /* see the comment in custom_op_xop */
14919 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
14920
14921 if (!PL_custom_ops)
14922 PL_custom_ops = newHV();
14923
14924 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
14925 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
14926}
14927
14928/*
14929
14930=for apidoc core_prototype
14931
14932This function assigns the prototype of the named core function to C<sv>, or
14933to a new mortal SV if C<sv> is C<NULL>. It returns the modified C<sv>, or
14934C<NULL> if the core function has no prototype. C<code> is a code as returned
14935by C<keyword()>. It must not be equal to 0.
14936
14937=cut
14938*/
14939
14940SV *
14941Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
14942 int * const opnum)
14943{
14944 int i = 0, n = 0, seen_question = 0, defgv = 0;
14945 I32 oa;
14946#define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
14947 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
14948 bool nullret = FALSE;
14949
14950 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
14951
14952 assert (code);
14953
14954 if (!sv) sv = sv_newmortal();
14955
14956#define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
14957
14958 switch (code < 0 ? -code : code) {
14959 case KEY_and : case KEY_chop: case KEY_chomp:
14960 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
14961 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
14962 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
14963 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
14964 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
14965 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
14966 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
14967 case KEY_x : case KEY_xor :
14968 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
14969 case KEY_glob: retsetpvs("_;", OP_GLOB);
14970 case KEY_keys: retsetpvs("\\[%@]", OP_KEYS);
14971 case KEY_values: retsetpvs("\\[%@]", OP_VALUES);
14972 case KEY_each: retsetpvs("\\[%@]", OP_EACH);
14973 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
14974 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
14975 retsetpvs("", 0);
14976 case KEY_evalbytes:
14977 name = "entereval"; break;
14978 case KEY_readpipe:
14979 name = "backtick";
14980 }
14981
14982#undef retsetpvs
14983
14984 findopnum:
14985 while (i < MAXO) { /* The slow way. */
14986 if (strEQ(name, PL_op_name[i])
14987 || strEQ(name, PL_op_desc[i]))
14988 {
14989 if (nullret) { assert(opnum); *opnum = i; return NULL; }
14990 goto found;
14991 }
14992 i++;
14993 }
14994 return NULL;
14995 found:
14996 defgv = PL_opargs[i] & OA_DEFGV;
14997 oa = PL_opargs[i] >> OASHIFT;
14998 while (oa) {
14999 if (oa & OA_OPTIONAL && !seen_question && (
15000 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
15001 )) {
15002 seen_question = 1;
15003 str[n++] = ';';
15004 }
15005 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
15006 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
15007 /* But globs are already references (kinda) */
15008 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
15009 ) {
15010 str[n++] = '\\';
15011 }
15012 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
15013 && !scalar_mod_type(NULL, i)) {
15014 str[n++] = '[';
15015 str[n++] = '$';
15016 str[n++] = '@';
15017 str[n++] = '%';
15018 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
15019 str[n++] = '*';
15020 str[n++] = ']';
15021 }
15022 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
15023 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
15024 str[n-1] = '_'; defgv = 0;
15025 }
15026 oa = oa >> 4;
15027 }
15028 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
15029 str[n++] = '\0';
15030 sv_setpvn(sv, str, n - 1);
15031 if (opnum) *opnum = i;
15032 return sv;
15033}
15034
15035OP *
15036Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
15037 const int opnum)
15038{
15039 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv);
15040 OP *o;
15041
15042 PERL_ARGS_ASSERT_CORESUB_OP;
15043
15044 switch(opnum) {
15045 case 0:
15046 return op_append_elem(OP_LINESEQ,
15047 argop,
15048 newSLICEOP(0,
15049 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
15050 newOP(OP_CALLER,0)
15051 )
15052 );
15053 case OP_EACH:
15054 case OP_KEYS:
15055 case OP_VALUES:
15056 o = newUNOP(OP_AVHVSWITCH,0,argop);
15057 o->op_private = opnum-OP_EACH;
15058 return o;
15059 case OP_SELECT: /* which represents OP_SSELECT as well */
15060 if (code)
15061 return newCONDOP(
15062 0,
15063 newBINOP(OP_GT, 0,
15064 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
15065 newSVOP(OP_CONST, 0, newSVuv(1))
15066 ),
15067 coresub_op(newSVuv((UV)OP_SSELECT), 0,
15068 OP_SSELECT),
15069 coresub_op(coreargssv, 0, OP_SELECT)
15070 );
15071 /* FALLTHROUGH */
15072 default:
15073 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
15074 case OA_BASEOP:
15075 return op_append_elem(
15076 OP_LINESEQ, argop,
15077 newOP(opnum,
15078 opnum == OP_WANTARRAY || opnum == OP_RUNCV
15079 ? OPpOFFBYONE << 8 : 0)
15080 );
15081 case OA_BASEOP_OR_UNOP:
15082 if (opnum == OP_ENTEREVAL) {
15083 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
15084 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
15085 }
15086 else o = newUNOP(opnum,0,argop);
15087 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
15088 else {
15089 onearg:
15090 if (is_handle_constructor(o, 1))
15091 argop->op_private |= OPpCOREARGS_DEREF1;
15092 if (scalar_mod_type(NULL, opnum))
15093 argop->op_private |= OPpCOREARGS_SCALARMOD;
15094 }
15095 return o;
15096 default:
15097 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
15098 if (is_handle_constructor(o, 2))
15099 argop->op_private |= OPpCOREARGS_DEREF2;
15100 if (opnum == OP_SUBSTR) {
15101 o->op_private |= OPpMAYBE_LVSUB;
15102 return o;
15103 }
15104 else goto onearg;
15105 }
15106 }
15107}
15108
15109void
15110Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
15111 SV * const *new_const_svp)
15112{
15113 const char *hvname;
15114 bool is_const = !!CvCONST(old_cv);
15115 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
15116
15117 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
15118
15119 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
15120 return;
15121 /* They are 2 constant subroutines generated from
15122 the same constant. This probably means that
15123 they are really the "same" proxy subroutine
15124 instantiated in 2 places. Most likely this is
15125 when a constant is exported twice. Don't warn.
15126 */
15127 if (
15128 (ckWARN(WARN_REDEFINE)
15129 && !(
15130 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
15131 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
15132 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
15133 strEQ(hvname, "autouse"))
15134 )
15135 )
15136 || (is_const
15137 && ckWARN_d(WARN_REDEFINE)
15138 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
15139 )
15140 )
15141 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
15142 is_const
15143 ? "Constant subroutine %" SVf " redefined"
15144 : "Subroutine %" SVf " redefined",
15145 SVfARG(name));
15146}
15147
15148/*
15149=head1 Hook manipulation
15150
15151These functions provide convenient and thread-safe means of manipulating
15152hook variables.
15153
15154=cut
15155*/
15156
15157/*
15158=for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p
15159
15160Puts a C function into the chain of check functions for a specified op
15161type. This is the preferred way to manipulate the L</PL_check> array.
15162C<opcode> specifies which type of op is to be affected. C<new_checker>
15163is a pointer to the C function that is to be added to that opcode's
15164check chain, and C<old_checker_p> points to the storage location where a
15165pointer to the next function in the chain will be stored. The value of
15166C<new_pointer> is written into the L</PL_check> array, while the value
15167previously stored there is written to C<*old_checker_p>.
15168
15169The function should be defined like this:
15170
15171 static OP *new_checker(pTHX_ OP *op) { ... }
15172
15173It is intended to be called in this manner:
15174
15175 new_checker(aTHX_ op)
15176
15177C<old_checker_p> should be defined like this:
15178
15179 static Perl_check_t old_checker_p;
15180
15181L</PL_check> is global to an entire process, and a module wishing to
15182hook op checking may find itself invoked more than once per process,
15183typically in different threads. To handle that situation, this function
15184is idempotent. The location C<*old_checker_p> must initially (once
15185per process) contain a null pointer. A C variable of static duration
15186(declared at file scope, typically also marked C<static> to give
15187it internal linkage) will be implicitly initialised appropriately,
15188if it does not have an explicit initialiser. This function will only
15189actually modify the check chain if it finds C<*old_checker_p> to be null.
15190This function is also thread safe on the small scale. It uses appropriate
15191locking to avoid race conditions in accessing L</PL_check>.
15192
15193When this function is called, the function referenced by C<new_checker>
15194must be ready to be called, except for C<*old_checker_p> being unfilled.
15195In a threading situation, C<new_checker> may be called immediately,
15196even before this function has returned. C<*old_checker_p> will always
15197be appropriately set before C<new_checker> is called. If C<new_checker>
15198decides not to do anything special with an op that it is given (which
15199is the usual case for most uses of op check hooking), it must chain the
15200check function referenced by C<*old_checker_p>.
15201
15202If you want to influence compilation of calls to a specific subroutine,
15203then use L</cv_set_call_checker> rather than hooking checking of all
15204C<entersub> ops.
15205
15206=cut
15207*/
15208
15209void
15210Perl_wrap_op_checker(pTHX_ Optype opcode,
15211 Perl_check_t new_checker, Perl_check_t *old_checker_p)
15212{
15213 dVAR;
15214
15215 PERL_UNUSED_CONTEXT;
15216 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
15217 if (*old_checker_p) return;
15218 OP_CHECK_MUTEX_LOCK;
15219 if (!*old_checker_p) {
15220 *old_checker_p = PL_check[opcode];
15221 PL_check[opcode] = new_checker;
15222 }
15223 OP_CHECK_MUTEX_UNLOCK;
15224}
15225
15226#include "XSUB.h"
15227
15228/* Efficient sub that returns a constant scalar value. */
15229static void
15230const_sv_xsub(pTHX_ CV* cv)
15231{
15232 dXSARGS;
15233 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
15234 PERL_UNUSED_ARG(items);
15235 if (!sv) {
15236 XSRETURN(0);
15237 }
15238 EXTEND(sp, 1);
15239 ST(0) = sv;
15240 XSRETURN(1);
15241}
15242
15243static void
15244const_av_xsub(pTHX_ CV* cv)
15245{
15246 dXSARGS;
15247 AV * const av = MUTABLE_AV(XSANY.any_ptr);
15248 SP -= items;
15249 assert(av);
15250#ifndef DEBUGGING
15251 if (!av) {
15252 XSRETURN(0);
15253 }
15254#endif
15255 if (SvRMAGICAL(av))
15256 Perl_croak(aTHX_ "Magical list constants are not supported");
15257 if (GIMME_V != G_ARRAY) {
15258 EXTEND(SP, 1);
15259 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
15260 XSRETURN(1);
15261 }
15262 EXTEND(SP, AvFILLp(av)+1);
15263 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
15264 XSRETURN(AvFILLp(av)+1);
15265}
15266
15267
15268/*
15269 * ex: set ts=8 sts=4 sw=4 et:
15270 */