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
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.
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
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * Note that during the build of miniperl, a temporary copy of this file
26 * is made, called opmini.c.
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.
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):
44 * newBINOP(OP_ADD, flags,
46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
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
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:
65 * with the intended execution order being:
67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
69 * At this point all the nodes' op_next pointers will have been set,
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
84 * [*] => A; A => B; B => [*]
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.
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.
97 * In summary: given a subtree, its top-level node's op_next will either
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
106 Here's an older description from Larry.
108 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
112 An execution-order pass
114 The bottom-up pass is represented by all the "newOP" routines and
115 the ck_ routines. The bottom-upness is actually driven by yacc.
116 So at the point that a ck_ routine fires, we have no idea what the
117 context is, either upward in the syntax tree, or either forward or
118 backward in the execution order. (The bottom-up parser builds that
119 part of the execution order it knows about, but if you follow the "next"
120 links around, you'll find it's actually a closed loop through the
123 Whenever the bottom-up parser gets to a node that supplies context to
124 its components, it invokes that portion of the top-down pass that applies
125 to that part of the subtree (and marks the top node as processed, so
126 if a node further up supplies context, it doesn't have to take the
127 plunge again). As a particular subcase of this, as the new node is
128 built, it takes all the closed execution loops of its subcomponents
129 and links them into a new closed loop for the higher level node. But
130 it's still not the real execution order.
132 The actual execution order is not known till we get a grammar reduction
133 to 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
135 into peep() to do that code's portion of the 3rd pass. It has to be
136 recursive, but it's recursive on basic blocks, not on tree nodes.
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.
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.
164 #include "keywords.h"
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)
172 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
174 /* Used to avoid recursion through the op tree in scalarvoid() and
178 #define DEFERRED_OP_STEP 100
179 #define DEFER_OP(o) \
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 *); \
186 defer_stack[++defer_ix] = o; \
189 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
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.
197 S_prune_chain_head(OP** 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)
205 *op_p = (*op_p)->op_next;
209 /* See the explanatory comments above struct opslab in op.h. */
211 #ifdef PERL_DEBUG_READONLY_OPS
212 # define PERL_SLAB_SIZE 128
213 # define PERL_MAX_SLAB_SIZE 4096
214 # include <sys/mman.h>
217 #ifndef PERL_SLAB_SIZE
218 # define PERL_SLAB_SIZE 64
220 #ifndef PERL_MAX_SLAB_SIZE
221 # define PERL_MAX_SLAB_SIZE 2048
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)))
229 S_new_slab(pTHX_ size_t sz)
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");
241 slab->opslab_size = (U16)sz;
243 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
246 /* The context is unused in non-Windows */
249 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
253 /* requires double parens and aTHX_ */
254 #define DEBUG_S_warn(args) \
256 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
260 Perl_Slab_Alloc(pTHX_ size_t sz)
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)))
277 o = (OP*)PerlMemShared_calloc(1, sz);
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
287 if (!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 */
293 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
295 opsz = SIZE_TO_PSIZE(sz);
296 sz = opsz + OPSLOT_HEADER_P;
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;
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)); }
312 Zero(o, opsz, I32 *);
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; \
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. */
330 /* If we can fit a BASEOP, add it to the free chain, so as not
332 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
333 slot = &slab2->opslab_slots;
335 o->op_type = OP_FREED;
336 o->op_next = slab->opslab_freed;
337 slab->opslab_freed = o;
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
346 : (DIFF(slab2, slot)+1)*2);
347 slab2->opslab_next = slab->opslab_next;
348 slab->opslab_next = slab2;
350 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
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;
359 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
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);
373 #ifdef PERL_DEBUG_READONLY_OPS
375 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
377 PERL_ARGS_ASSERT_SLAB_TO_RO;
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);
391 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
395 PERL_ARGS_ASSERT_SLAB_TO_RW;
397 if (!slab->opslab_readonly) return;
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);
408 slab->opslab_readonly = 0;
412 # define Slab_to_rw(op) NOOP
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
419 # define PerlMemShared PerlMem
423 Perl_Slab_Free(pTHX_ void *op)
425 OP * const o = (OP *)op;
428 PERL_ARGS_ASSERT_SLAB_FREE;
430 if (!o->op_slabbed) {
432 PerlMemShared_free(op);
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);
447 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
449 const bool havepad = !!PL_comppad;
450 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
453 PAD_SAVE_SETNULLPAD();
460 Perl_opslab_free(pTHX_ OPSLAB *slab)
463 PERL_ARGS_ASSERT_OPSLAB_FREE;
465 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
466 assert(slab->opslab_refcnt == 1);
468 slab2 = slab->opslab_next;
470 slab->opslab_refcnt = ~(size_t)0;
472 #ifdef PERL_DEBUG_READONLY_OPS
473 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
475 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
476 perror("munmap failed");
480 PerlMemShared_free(slab);
487 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
492 size_t savestack_count = 0;
494 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
497 for (slot = slab2->opslab_first;
499 slot = slot->opslot_next) {
500 if (slot->opslot_op.op_type != OP_FREED
501 && !(slot->opslot_op.op_savefree
507 assert(slot->opslot_op.op_slabbed);
508 op_free(&slot->opslot_op);
509 if (slab->opslab_refcnt == 1) goto free;
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 */
516 assert(savestack_count == slab->opslab_refcnt-1);
518 /* Remove the CV’s reference count. */
519 slab->opslab_refcnt--;
526 #ifdef PERL_DEBUG_READONLY_OPS
528 Perl_op_refcnt_inc(pTHX_ OP *o)
531 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
532 if (slab && slab->opslab_readonly) {
545 Perl_op_refcnt_dec(pTHX_ OP *o)
548 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
550 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
552 if (slab && slab->opslab_readonly) {
554 result = --o->op_targ;
557 result = --o->op_targ;
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.
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]), \
571 : PL_check[type](aTHX_ (OP*)o))
573 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
575 #define OpTYPE_set(o,type) \
577 o->op_type = (OPCODE)type; \
578 o->op_ppaddr = PL_ppaddr[type]; \
582 S_no_fh_allowed(pTHX_ OP *o)
584 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
586 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
592 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
594 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
595 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
600 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
602 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
604 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
609 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
611 PERL_ARGS_ASSERT_BAD_TYPE_PV;
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);
617 /* remove flags var, its unused in all callers, move to to right end since gv
618 and kid are always the same */
620 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
622 SV * const namesv = cv_name((CV *)gv, NULL, 0);
623 PERL_ARGS_ASSERT_BAD_TYPE_GV;
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));
630 S_no_bareword_allowed(pTHX_ OP *o)
632 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
634 qerror(Perl_mess(aTHX_
635 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
637 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
640 /* "register" allocation */
643 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
646 const bool is_our = (PL_parser->in_my == KEY_our);
648 PERL_ARGS_ASSERT_ALLOCMY;
650 if (flags & ~SVf_UTF8)
651 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
654 /* complain about "my $<special_var>" etc etc */
658 || ( (flags & SVf_UTF8)
659 && isIDFIRST_utf8_safe((U8 *)name+1, name + len))
660 || (name[1] == '_' && len > 2)))
662 if (!(flags & SVf_UTF8 && UTF8_IS_START(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"));
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);
674 /* allocate a spare slot and store the name in that slot */
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,
681 /* $_ is always in main::, even with our */
682 ? (PL_curstash && !memEQs(name,len,"$_")
688 /* anon sub prototypes contains state vars should always be cloned,
689 * otherwise the state var would be shared between anon subs */
691 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
692 CvCLONE_on(PL_compcv);
698 =head1 Optree Manipulation Functions
700 =for apidoc alloccopstash
702 Available only under threaded builds, this function allocates an entry in
703 C<PL_stashpad> for the stash passed to it.
710 Perl_alloccopstash(pTHX_ HV *hv)
712 PADOFFSET off = 0, o = 1;
713 bool found_slot = FALSE;
715 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
717 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
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;
725 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
726 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
727 off = PL_stashpadmax;
728 PL_stashpadmax += 10;
731 PL_stashpad[PL_stashpadix = off] = hv;
736 /* free the body of an op without examining its contents.
737 * Always use this rather than FreeOp directly */
740 S_op_destroy(pTHX_ OP *o)
748 =for apidoc Am|void|op_free|OP *o
750 Free an op. Only use this when an op is no longer linked to from any
757 Perl_op_free(pTHX_ OP *o)
761 SSize_t defer_ix = -1;
762 SSize_t defer_stack_alloc = 0;
763 OP **defer_stack = NULL;
767 /* Though ops may be freed twice, freeing the op after its slab is a
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)
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 */
788 if ( o->op_ppaddr == PL_ppaddr[o->op_type]
790 && !PL_parser->error_count)
792 assert(!(o->op_private & ~PL_op_private_valid[type]));
796 if (o->op_private & OPpREFCOUNTED) {
807 refcnt = OpREFCNT_dec(o);
810 /* Need to find and remove any pattern match ops from the list
811 we maintain for reset(). */
812 find_and_forget_pmops(o);
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
827 if (o->op_flags & OPf_KIDS) {
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
836 if (!(kid->op_flags & OPf_KIDS))
837 /* If it has no kids, just free it now */
844 type = (OPCODE)o->op_targ;
847 Slab_to_rw(OpSLAB(o));
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) {
859 } while ( (o = POP_DEFERRED_OP()) );
861 Safefree(defer_stack);
864 /* S_op_clear_gv(): free a GV attached to an OP */
868 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
870 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
874 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
875 || o->op_type == OP_MULTIDEREF)
878 ? ((GV*)PAD_SVl(*ixp)) : NULL;
880 ? (GV*)(*svp) : NULL;
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);
899 SvREFCNT_inc_simple_void(gv);
902 pad_swipe(*ixp, TRUE);
910 int try_downgrade = SvREFCNT(gv) == 2;
913 gv_try_downgrade(gv);
919 Perl_op_clear(pTHX_ OP *o)
924 PERL_ARGS_ASSERT_OP_CLEAR;
926 switch (o->op_type) {
927 case OP_NULL: /* Was holding old type, if any. */
930 case OP_ENTEREVAL: /* Was holding hints. */
931 case OP_ARGDEFELEM: /* Was holding signature index. */
935 if (!(o->op_flags & OPf_REF)
936 || (PL_check[o->op_type] != Perl_ck_ftst))
943 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
945 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
948 case OP_METHOD_REDIR:
949 case OP_METHOD_REDIR_SUPER:
951 if (cMETHOPx(o)->op_rclass_targ) {
952 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
953 cMETHOPx(o)->op_rclass_targ = 0;
956 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
957 cMETHOPx(o)->op_rclass_sv = NULL;
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;
965 pad_swipe(o->op_targ, 1);
972 SvREFCNT_dec(cSVOPo->op_sv);
973 cSVOPo->op_sv = NULL;
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.
982 pad_swipe(o->op_targ,1);
992 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
997 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
998 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
1000 if (cPADOPo->op_padix > 0) {
1001 pad_swipe(cPADOPo->op_padix, TRUE);
1002 cPADOPo->op_padix = 0;
1005 SvREFCNT_dec(cSVOPo->op_sv);
1006 cSVOPo->op_sv = NULL;
1010 PerlMemShared_free(cPVOPo->op_pv);
1011 cPVOPo->op_pv = NULL;
1015 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1019 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1020 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1022 if (o->op_private & OPpSPLIT_LEX)
1023 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1026 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1028 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1035 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1036 op_free(cPMOPo->op_code_list);
1037 cPMOPo->op_code_list = NULL;
1038 forget_pmop(cPMOPo);
1039 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1040 /* we use the same protection as the "SAFE" version of the PM_ macros
1041 * here since sv_clean_all might release some PMOPs
1042 * after PL_regex_padav has been cleared
1043 * and the clearing of PL_regex_padav needs to
1044 * happen before sv_clean_all
1047 if(PL_regex_pad) { /* We could be in destruction */
1048 const IV offset = (cPMOPo)->op_pmoffset;
1049 ReREFCNT_dec(PM_GETRE(cPMOPo));
1050 PL_regex_pad[offset] = &PL_sv_undef;
1051 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1055 ReREFCNT_dec(PM_GETRE(cPMOPo));
1056 PM_SETRE(cPMOPo, NULL);
1062 PerlMemShared_free(cUNOP_AUXo->op_aux);
1067 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1068 UV actions = items->uv;
1070 bool is_hash = FALSE;
1073 switch (actions & MDEREF_ACTION_MASK) {
1076 actions = (++items)->uv;
1079 case MDEREF_HV_padhv_helem:
1081 case MDEREF_AV_padav_aelem:
1082 pad_free((++items)->pad_offset);
1085 case MDEREF_HV_gvhv_helem:
1087 case MDEREF_AV_gvav_aelem:
1089 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1091 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1095 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1097 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1099 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1101 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1103 goto do_vivify_rv2xv_elem;
1105 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1107 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1108 pad_free((++items)->pad_offset);
1109 goto do_vivify_rv2xv_elem;
1111 case MDEREF_HV_pop_rv2hv_helem:
1112 case MDEREF_HV_vivify_rv2hv_helem:
1114 do_vivify_rv2xv_elem:
1115 case MDEREF_AV_pop_rv2av_aelem:
1116 case MDEREF_AV_vivify_rv2av_aelem:
1118 switch (actions & MDEREF_INDEX_MASK) {
1119 case MDEREF_INDEX_none:
1122 case MDEREF_INDEX_const:
1126 pad_swipe((++items)->pad_offset, 1);
1128 SvREFCNT_dec((++items)->sv);
1134 case MDEREF_INDEX_padsv:
1135 pad_free((++items)->pad_offset);
1137 case MDEREF_INDEX_gvsv:
1139 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1141 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1146 if (actions & MDEREF_FLAG_last)
1159 actions >>= MDEREF_SHIFT;
1162 /* start of malloc is at op_aux[-1], where the length is
1164 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1169 if (o->op_targ > 0) {
1170 pad_free(o->op_targ);
1176 S_cop_free(pTHX_ COP* cop)
1178 PERL_ARGS_ASSERT_COP_FREE;
1181 if (! specialWARN(cop->cop_warnings))
1182 PerlMemShared_free(cop->cop_warnings);
1183 cophh_free(CopHINTHASH_get(cop));
1184 if (PL_curcop == cop)
1189 S_forget_pmop(pTHX_ PMOP *const o
1192 HV * const pmstash = PmopSTASH(o);
1194 PERL_ARGS_ASSERT_FORGET_PMOP;
1196 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1197 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1199 PMOP **const array = (PMOP**) mg->mg_ptr;
1200 U32 count = mg->mg_len / sizeof(PMOP**);
1204 if (array[i] == o) {
1205 /* Found it. Move the entry at the end to overwrite it. */
1206 array[i] = array[--count];
1207 mg->mg_len = count * sizeof(PMOP**);
1208 /* Could realloc smaller at this point always, but probably
1209 not worth it. Probably worth free()ing if we're the
1212 Safefree(mg->mg_ptr);
1225 S_find_and_forget_pmops(pTHX_ OP *o)
1227 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1229 if (o->op_flags & OPf_KIDS) {
1230 OP *kid = cUNOPo->op_first;
1232 switch (kid->op_type) {
1237 forget_pmop((PMOP*)kid);
1239 find_and_forget_pmops(kid);
1240 kid = OpSIBLING(kid);
1246 =for apidoc Am|void|op_null|OP *o
1248 Neutralizes an op when it is no longer needed, but is still linked to from
1255 Perl_op_null(pTHX_ OP *o)
1259 PERL_ARGS_ASSERT_OP_NULL;
1261 if (o->op_type == OP_NULL)
1264 o->op_targ = o->op_type;
1265 OpTYPE_set(o, OP_NULL);
1269 Perl_op_refcnt_lock(pTHX)
1270 PERL_TSA_ACQUIRE(PL_op_mutex)
1275 PERL_UNUSED_CONTEXT;
1280 Perl_op_refcnt_unlock(pTHX)
1281 PERL_TSA_RELEASE(PL_op_mutex)
1286 PERL_UNUSED_CONTEXT;
1292 =for apidoc op_sibling_splice
1294 A general function for editing the structure of an existing chain of
1295 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1296 you to delete zero or more sequential nodes, replacing them with zero or
1297 more different nodes. Performs the necessary op_first/op_last
1298 housekeeping on the parent node and op_sibling manipulation on the
1299 children. The last deleted node will be marked as as the last node by
1300 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1302 Note that op_next is not manipulated, and nodes are not freed; that is the
1303 responsibility of the caller. It also won't create a new list op for an
1304 empty list etc; use higher-level functions like op_append_elem() for that.
1306 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1307 the splicing doesn't affect the first or last op in the chain.
1309 C<start> is the node preceding the first node to be spliced. Node(s)
1310 following it will be deleted, and ops will be inserted after it. If it is
1311 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1314 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1315 If -1 or greater than or equal to the number of remaining kids, all
1316 remaining kids are deleted.
1318 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1319 If C<NULL>, no nodes are inserted.
1321 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1326 action before after returns
1327 ------ ----- ----- -------
1330 splice(P, A, 2, X-Y-Z) | | B-C
1334 splice(P, NULL, 1, X-Y) | | A
1338 splice(P, NULL, 3, NULL) | | A-B-C
1342 splice(P, B, 0, X-Y) | | NULL
1346 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1347 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1353 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1357 OP *last_del = NULL;
1358 OP *last_ins = NULL;
1361 first = OpSIBLING(start);
1365 first = cLISTOPx(parent)->op_first;
1367 assert(del_count >= -1);
1369 if (del_count && first) {
1371 while (--del_count && OpHAS_SIBLING(last_del))
1372 last_del = OpSIBLING(last_del);
1373 rest = OpSIBLING(last_del);
1374 OpLASTSIB_set(last_del, NULL);
1381 while (OpHAS_SIBLING(last_ins))
1382 last_ins = OpSIBLING(last_ins);
1383 OpMAYBESIB_set(last_ins, rest, NULL);
1389 OpMAYBESIB_set(start, insert, NULL);
1394 cLISTOPx(parent)->op_first = insert;
1396 parent->op_flags |= OPf_KIDS;
1398 parent->op_flags &= ~OPf_KIDS;
1402 /* update op_last etc */
1409 /* ought to use OP_CLASS(parent) here, but that can't handle
1410 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1412 type = parent->op_type;
1413 if (type == OP_CUSTOM) {
1415 type = XopENTRYCUSTOM(parent, xop_class);
1418 if (type == OP_NULL)
1419 type = parent->op_targ;
1420 type = PL_opargs[type] & OA_CLASS_MASK;
1423 lastop = last_ins ? last_ins : start ? start : NULL;
1424 if ( type == OA_BINOP
1425 || type == OA_LISTOP
1429 cLISTOPx(parent)->op_last = lastop;
1432 OpLASTSIB_set(lastop, parent);
1434 return last_del ? first : NULL;
1437 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1441 #ifdef PERL_OP_PARENT
1444 =for apidoc op_parent
1446 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1447 This function is only available on perls built with C<-DPERL_OP_PARENT>.
1453 Perl_op_parent(OP *o)
1455 PERL_ARGS_ASSERT_OP_PARENT;
1456 while (OpHAS_SIBLING(o))
1458 return o->op_sibparent;
1464 /* replace the sibling following start with a new UNOP, which becomes
1465 * the parent of the original sibling; e.g.
1467 * op_sibling_newUNOP(P, A, unop-args...)
1475 * where U is the new UNOP.
1477 * parent and start args are the same as for op_sibling_splice();
1478 * type and flags args are as newUNOP().
1480 * Returns the new UNOP.
1484 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1488 kid = op_sibling_splice(parent, start, 1, NULL);
1489 newop = newUNOP(type, flags, kid);
1490 op_sibling_splice(parent, start, 0, newop);
1495 /* lowest-level newLOGOP-style function - just allocates and populates
1496 * the struct. Higher-level stuff should be done by S_new_logop() /
1497 * newLOGOP(). This function exists mainly to avoid op_first assignment
1498 * being spread throughout this file.
1502 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1507 NewOp(1101, logop, 1, LOGOP);
1508 OpTYPE_set(logop, type);
1509 logop->op_first = first;
1510 logop->op_other = other;
1511 logop->op_flags = OPf_KIDS;
1512 while (kid && OpHAS_SIBLING(kid))
1513 kid = OpSIBLING(kid);
1515 OpLASTSIB_set(kid, (OP*)logop);
1520 /* Contextualizers */
1523 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1525 Applies a syntactic context to an op tree representing an expression.
1526 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1527 or C<G_VOID> to specify the context to apply. The modified op tree
1534 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1536 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1538 case G_SCALAR: return scalar(o);
1539 case G_ARRAY: return list(o);
1540 case G_VOID: return scalarvoid(o);
1542 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1549 =for apidoc Am|OP*|op_linklist|OP *o
1550 This function is the implementation of the L</LINKLIST> macro. It should
1551 not be called directly.
1557 Perl_op_linklist(pTHX_ OP *o)
1561 PERL_ARGS_ASSERT_OP_LINKLIST;
1566 /* establish postfix order */
1567 first = cUNOPo->op_first;
1570 o->op_next = LINKLIST(first);
1573 OP *sibl = OpSIBLING(kid);
1575 kid->op_next = LINKLIST(sibl);
1590 S_scalarkids(pTHX_ OP *o)
1592 if (o && o->op_flags & OPf_KIDS) {
1594 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1601 S_scalarboolean(pTHX_ OP *o)
1603 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1605 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1606 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1607 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1608 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1609 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1610 if (ckWARN(WARN_SYNTAX)) {
1611 const line_t oldline = CopLINE(PL_curcop);
1613 if (PL_parser && PL_parser->copline != NOLINE) {
1614 /* This ensures that warnings are reported at the first line
1615 of the conditional, not the last. */
1616 CopLINE_set(PL_curcop, PL_parser->copline);
1618 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1619 CopLINE_set(PL_curcop, oldline);
1626 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1629 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1630 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1632 const char funny = o->op_type == OP_PADAV
1633 || o->op_type == OP_RV2AV ? '@' : '%';
1634 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1636 if (cUNOPo->op_first->op_type != OP_GV
1637 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1639 return varname(gv, funny, 0, NULL, 0, subscript_type);
1642 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1647 S_op_varname(pTHX_ const OP *o)
1649 return S_op_varname_subscript(aTHX_ o, 1);
1653 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1654 { /* or not so pretty :-) */
1655 if (o->op_type == OP_CONST) {
1657 if (SvPOK(*retsv)) {
1659 *retsv = sv_newmortal();
1660 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1661 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1663 else if (!SvOK(*retsv))
1666 else *retpv = "...";
1670 S_scalar_slice_warning(pTHX_ const OP *o)
1674 o->op_type == OP_HSLICE ? '{' : '[';
1676 o->op_type == OP_HSLICE ? '}' : ']';
1678 SV *keysv = NULL; /* just to silence compiler warnings */
1679 const char *key = NULL;
1681 if (!(o->op_private & OPpSLICEWARNING))
1683 if (PL_parser && PL_parser->error_count)
1684 /* This warning can be nonsensical when there is a syntax error. */
1687 kid = cLISTOPo->op_first;
1688 kid = OpSIBLING(kid); /* get past pushmark */
1689 /* weed out false positives: any ops that can return lists */
1690 switch (kid->op_type) {
1716 /* Don't warn if we have a nulled list either. */
1717 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1720 assert(OpSIBLING(kid));
1721 name = S_op_varname(aTHX_ OpSIBLING(kid));
1722 if (!name) /* XS module fiddling with the op tree */
1724 S_op_pretty(aTHX_ kid, &keysv, &key);
1725 assert(SvPOK(name));
1726 sv_chop(name,SvPVX(name)+1);
1728 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1729 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1730 "Scalar value @%" SVf "%c%s%c better written as $%" SVf
1732 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1733 lbrack, key, rbrack);
1735 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1736 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1737 "Scalar value @%" SVf "%c%" SVf "%c better written as $%"
1739 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1740 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1744 Perl_scalar(pTHX_ OP *o)
1748 /* assumes no premature commitment */
1749 if (!o || (PL_parser && PL_parser->error_count)
1750 || (o->op_flags & OPf_WANT)
1751 || o->op_type == OP_RETURN)
1756 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1758 switch (o->op_type) {
1760 scalar(cBINOPo->op_first);
1761 if (o->op_private & OPpREPEAT_DOLIST) {
1762 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1763 assert(kid->op_type == OP_PUSHMARK);
1764 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1765 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1766 o->op_private &=~ OPpREPEAT_DOLIST;
1773 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
1783 if (o->op_flags & OPf_KIDS) {
1784 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
1790 kid = cLISTOPo->op_first;
1792 kid = OpSIBLING(kid);
1795 OP *sib = OpSIBLING(kid);
1796 if (sib && kid->op_type != OP_LEAVEWHEN
1797 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
1798 || ( sib->op_targ != OP_NEXTSTATE
1799 && sib->op_targ != OP_DBSTATE )))
1805 PL_curcop = &PL_compiling;
1810 kid = cLISTOPo->op_first;
1813 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1818 /* Warn about scalar context */
1819 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1820 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1823 const char *key = NULL;
1825 /* This warning can be nonsensical when there is a syntax error. */
1826 if (PL_parser && PL_parser->error_count)
1829 if (!ckWARN(WARN_SYNTAX)) break;
1831 kid = cLISTOPo->op_first;
1832 kid = OpSIBLING(kid); /* get past pushmark */
1833 assert(OpSIBLING(kid));
1834 name = S_op_varname(aTHX_ OpSIBLING(kid));
1835 if (!name) /* XS module fiddling with the op tree */
1837 S_op_pretty(aTHX_ kid, &keysv, &key);
1838 assert(SvPOK(name));
1839 sv_chop(name,SvPVX(name)+1);
1841 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1842 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1843 "%%%" SVf "%c%s%c in scalar context better written "
1844 "as $%" SVf "%c%s%c",
1845 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1846 lbrack, key, rbrack);
1848 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1849 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1850 "%%%" SVf "%c%" SVf "%c in scalar context better "
1851 "written as $%" SVf "%c%" SVf "%c",
1852 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1853 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1860 Perl_scalarvoid(pTHX_ OP *arg)
1866 SSize_t defer_stack_alloc = 0;
1867 SSize_t defer_ix = -1;
1868 OP **defer_stack = NULL;
1871 PERL_ARGS_ASSERT_SCALARVOID;
1874 SV *useless_sv = NULL;
1875 const char* useless = NULL;
1877 if (o->op_type == OP_NEXTSTATE
1878 || o->op_type == OP_DBSTATE
1879 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1880 || o->op_targ == OP_DBSTATE)))
1881 PL_curcop = (COP*)o; /* for warning below */
1883 /* assumes no premature commitment */
1884 want = o->op_flags & OPf_WANT;
1885 if ((want && want != OPf_WANT_SCALAR)
1886 || (PL_parser && PL_parser->error_count)
1887 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1892 if ((o->op_private & OPpTARGET_MY)
1893 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1895 /* newASSIGNOP has already applied scalar context, which we
1896 leave, as if this op is inside SASSIGN. */
1900 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1902 switch (o->op_type) {
1904 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1908 if (o->op_flags & OPf_STACKED)
1910 if (o->op_type == OP_REPEAT)
1911 scalar(cBINOPo->op_first);
1914 if (o->op_private == 4)
1949 case OP_GETSOCKNAME:
1950 case OP_GETPEERNAME:
1955 case OP_GETPRIORITY:
1980 useless = OP_DESC(o);
1990 case OP_AELEMFAST_LEX:
1994 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1995 /* Otherwise it's "Useless use of grep iterator" */
1996 useless = OP_DESC(o);
2000 if (!(o->op_private & OPpSPLIT_ASSIGN))
2001 useless = OP_DESC(o);
2005 kid = cUNOPo->op_first;
2006 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2007 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2010 useless = "negative pattern binding (!~)";
2014 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2015 useless = "non-destructive substitution (s///r)";
2019 useless = "non-destructive transliteration (tr///r)";
2026 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2027 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2028 useless = "a variable";
2033 if (cSVOPo->op_private & OPpCONST_STRICT)
2034 no_bareword_allowed(o);
2036 if (ckWARN(WARN_VOID)) {
2038 /* don't warn on optimised away booleans, eg
2039 * use constant Foo, 5; Foo || print; */
2040 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2042 /* the constants 0 and 1 are permitted as they are
2043 conventionally used as dummies in constructs like
2044 1 while some_condition_with_side_effects; */
2045 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2047 else if (SvPOK(sv)) {
2048 SV * const dsv = newSVpvs("");
2050 = Perl_newSVpvf(aTHX_
2052 pv_pretty(dsv, SvPVX_const(sv),
2053 SvCUR(sv), 32, NULL, NULL,
2055 | PERL_PV_ESCAPE_NOCLEAR
2056 | PERL_PV_ESCAPE_UNI_DETECT));
2057 SvREFCNT_dec_NN(dsv);
2059 else if (SvOK(sv)) {
2060 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2063 useless = "a constant (undef)";
2066 op_null(o); /* don't execute or even remember it */
2070 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2074 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2078 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2082 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2087 UNOP *refgen, *rv2cv;
2090 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2093 rv2gv = ((BINOP *)o)->op_last;
2094 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2097 refgen = (UNOP *)((BINOP *)o)->op_first;
2099 if (!refgen || (refgen->op_type != OP_REFGEN
2100 && refgen->op_type != OP_SREFGEN))
2103 exlist = (LISTOP *)refgen->op_first;
2104 if (!exlist || exlist->op_type != OP_NULL
2105 || exlist->op_targ != OP_LIST)
2108 if (exlist->op_first->op_type != OP_PUSHMARK
2109 && exlist->op_first != exlist->op_last)
2112 rv2cv = (UNOP*)exlist->op_last;
2114 if (rv2cv->op_type != OP_RV2CV)
2117 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2118 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2119 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2121 o->op_private |= OPpASSIGN_CV_TO_GV;
2122 rv2gv->op_private |= OPpDONT_INIT_GV;
2123 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2135 kid = cLOGOPo->op_first;
2136 if (kid->op_type == OP_NOT
2137 && (kid->op_flags & OPf_KIDS)) {
2138 if (o->op_type == OP_AND) {
2139 OpTYPE_set(o, OP_OR);
2141 OpTYPE_set(o, OP_AND);
2151 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2152 if (!(kid->op_flags & OPf_KIDS))
2159 if (o->op_flags & OPf_STACKED)
2166 if (!(o->op_flags & OPf_KIDS))
2177 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2178 if (!(kid->op_flags & OPf_KIDS))
2184 /* If the first kid after pushmark is something that the padrange
2185 optimisation would reject, then null the list and the pushmark.
2187 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2188 && ( !(kid = OpSIBLING(kid))
2189 || ( kid->op_type != OP_PADSV
2190 && kid->op_type != OP_PADAV
2191 && kid->op_type != OP_PADHV)
2192 || kid->op_private & ~OPpLVAL_INTRO
2193 || !(kid = OpSIBLING(kid))
2194 || ( kid->op_type != OP_PADSV
2195 && kid->op_type != OP_PADAV
2196 && kid->op_type != OP_PADHV)
2197 || kid->op_private & ~OPpLVAL_INTRO)
2199 op_null(cUNOPo->op_first); /* NULL the pushmark */
2200 op_null(o); /* NULL the list */
2212 /* mortalise it, in case warnings are fatal. */
2213 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2214 "Useless use of %" SVf " in void context",
2215 SVfARG(sv_2mortal(useless_sv)));
2218 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2219 "Useless use of %s in void context",
2222 } while ( (o = POP_DEFERRED_OP()) );
2224 Safefree(defer_stack);
2230 S_listkids(pTHX_ OP *o)
2232 if (o && o->op_flags & OPf_KIDS) {
2234 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2241 Perl_list(pTHX_ OP *o)
2245 /* assumes no premature commitment */
2246 if (!o || (o->op_flags & OPf_WANT)
2247 || (PL_parser && PL_parser->error_count)
2248 || o->op_type == OP_RETURN)
2253 if ((o->op_private & OPpTARGET_MY)
2254 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2256 return o; /* As if inside SASSIGN */
2259 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2261 switch (o->op_type) {
2263 list(cBINOPo->op_first);
2266 if (o->op_private & OPpREPEAT_DOLIST
2267 && !(o->op_flags & OPf_STACKED))
2269 list(cBINOPo->op_first);
2270 kid = cBINOPo->op_last;
2271 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2272 && SvIVX(kSVOP_sv) == 1)
2274 op_null(o); /* repeat */
2275 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2277 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2284 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2292 if (!(o->op_flags & OPf_KIDS))
2294 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2295 list(cBINOPo->op_first);
2296 return gen_constant_list(o);
2302 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2303 op_null(cUNOPo->op_first); /* NULL the pushmark */
2304 op_null(o); /* NULL the list */
2309 kid = cLISTOPo->op_first;
2311 kid = OpSIBLING(kid);
2314 OP *sib = OpSIBLING(kid);
2315 if (sib && kid->op_type != OP_LEAVEWHEN)
2321 PL_curcop = &PL_compiling;
2325 kid = cLISTOPo->op_first;
2332 S_scalarseq(pTHX_ OP *o)
2335 const OPCODE type = o->op_type;
2337 if (type == OP_LINESEQ || type == OP_SCOPE ||
2338 type == OP_LEAVE || type == OP_LEAVETRY)
2341 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2342 if ((sib = OpSIBLING(kid))
2343 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2344 || ( sib->op_targ != OP_NEXTSTATE
2345 && sib->op_targ != OP_DBSTATE )))
2350 PL_curcop = &PL_compiling;
2352 o->op_flags &= ~OPf_PARENS;
2353 if (PL_hints & HINT_BLOCK_SCOPE)
2354 o->op_flags |= OPf_PARENS;
2357 o = newOP(OP_STUB, 0);
2362 S_modkids(pTHX_ OP *o, I32 type)
2364 if (o && o->op_flags & OPf_KIDS) {
2366 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2367 op_lvalue(kid, type);
2373 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2374 * const fields. Also, convert CONST keys to HEK-in-SVs.
2375 * rop is the op that retrieves the hash;
2376 * key_op is the first key
2380 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op)
2386 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2388 if (rop->op_first->op_type == OP_PADSV)
2389 /* @$hash{qw(keys here)} */
2390 rop = (UNOP*)rop->op_first;
2392 /* @{$hash}{qw(keys here)} */
2393 if (rop->op_first->op_type == OP_SCOPE
2394 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2396 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2403 lexname = NULL; /* just to silence compiler warnings */
2404 fields = NULL; /* just to silence compiler warnings */
2408 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2409 SvPAD_TYPED(lexname))
2410 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2411 && isGV(*fields) && GvHV(*fields);
2413 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2415 if (key_op->op_type != OP_CONST)
2417 svp = cSVOPx_svp(key_op);
2419 /* make sure it's not a bareword under strict subs */
2420 if (key_op->op_private & OPpCONST_BARE &&
2421 key_op->op_private & OPpCONST_STRICT)
2423 no_bareword_allowed((OP*)key_op);
2426 /* Make the CONST have a shared SV */
2427 if ( !SvIsCOW_shared_hash(sv = *svp)
2428 && SvTYPE(sv) < SVt_PVMG
2433 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2434 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2435 SvREFCNT_dec_NN(sv);
2440 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2442 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2443 "in variable %" PNf " of type %" HEKf,
2444 SVfARG(*svp), PNfARG(lexname),
2445 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2452 =for apidoc finalize_optree
2454 This function finalizes the optree. Should be called directly after
2455 the complete optree is built. It does some additional
2456 checking which can't be done in the normal C<ck_>xxx functions and makes
2457 the tree thread-safe.
2462 Perl_finalize_optree(pTHX_ OP* o)
2464 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2467 SAVEVPTR(PL_curcop);
2475 /* Relocate sv to the pad for thread safety.
2476 * Despite being a "constant", the SV is written to,
2477 * for reference counts, sv_upgrade() etc. */
2478 PERL_STATIC_INLINE void
2479 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2482 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2484 ix = pad_alloc(OP_CONST, SVf_READONLY);
2485 SvREFCNT_dec(PAD_SVl(ix));
2486 PAD_SETSV(ix, *svp);
2487 /* XXX I don't know how this isn't readonly already. */
2488 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2496 S_finalize_op(pTHX_ OP* o)
2498 PERL_ARGS_ASSERT_FINALIZE_OP;
2500 assert(o->op_type != OP_FREED);
2502 switch (o->op_type) {
2505 PL_curcop = ((COP*)o); /* for warnings */
2508 if (OpHAS_SIBLING(o)) {
2509 OP *sib = OpSIBLING(o);
2510 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2511 && ckWARN(WARN_EXEC)
2512 && OpHAS_SIBLING(sib))
2514 const OPCODE type = OpSIBLING(sib)->op_type;
2515 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2516 const line_t oldline = CopLINE(PL_curcop);
2517 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2518 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2519 "Statement unlikely to be reached");
2520 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2521 "\t(Maybe you meant system() when you said exec()?)\n");
2522 CopLINE_set(PL_curcop, oldline);
2529 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2530 GV * const gv = cGVOPo_gv;
2531 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2532 /* XXX could check prototype here instead of just carping */
2533 SV * const sv = sv_newmortal();
2534 gv_efullname3(sv, gv, NULL);
2535 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2536 "%" SVf "() called too early to check prototype",
2543 if (cSVOPo->op_private & OPpCONST_STRICT)
2544 no_bareword_allowed(o);
2548 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2553 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2554 case OP_METHOD_NAMED:
2555 case OP_METHOD_SUPER:
2556 case OP_METHOD_REDIR:
2557 case OP_METHOD_REDIR_SUPER:
2558 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2567 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2570 rop = (UNOP*)((BINOP*)o)->op_first;
2575 S_scalar_slice_warning(aTHX_ o);
2579 kid = OpSIBLING(cLISTOPo->op_first);
2580 if (/* I bet there's always a pushmark... */
2581 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2582 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2587 key_op = (SVOP*)(kid->op_type == OP_CONST
2589 : OpSIBLING(kLISTOP->op_first));
2591 rop = (UNOP*)((LISTOP*)o)->op_last;
2594 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2596 S_check_hash_fields_and_hekify(aTHX_ rop, key_op);
2600 S_scalar_slice_warning(aTHX_ o);
2604 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2605 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2612 if (o->op_flags & OPf_KIDS) {
2616 /* check that op_last points to the last sibling, and that
2617 * the last op_sibling/op_sibparent field points back to the
2618 * parent, and that the only ops with KIDS are those which are
2619 * entitled to them */
2620 U32 type = o->op_type;
2624 if (type == OP_NULL) {
2626 /* ck_glob creates a null UNOP with ex-type GLOB
2627 * (which is a list op. So pretend it wasn't a listop */
2628 if (type == OP_GLOB)
2631 family = PL_opargs[type] & OA_CLASS_MASK;
2633 has_last = ( family == OA_BINOP
2634 || family == OA_LISTOP
2635 || family == OA_PMOP
2636 || family == OA_LOOP
2638 assert( has_last /* has op_first and op_last, or ...
2639 ... has (or may have) op_first: */
2640 || family == OA_UNOP
2641 || family == OA_UNOP_AUX
2642 || family == OA_LOGOP
2643 || family == OA_BASEOP_OR_UNOP
2644 || family == OA_FILESTATOP
2645 || family == OA_LOOPEXOP
2646 || family == OA_METHOP
2647 || type == OP_CUSTOM
2648 || type == OP_NULL /* new_logop does this */
2651 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2652 # ifdef PERL_OP_PARENT
2653 if (!OpHAS_SIBLING(kid)) {
2655 assert(kid == cLISTOPo->op_last);
2656 assert(kid->op_sibparent == o);
2659 if (has_last && !OpHAS_SIBLING(kid))
2660 assert(kid == cLISTOPo->op_last);
2665 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
2671 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2673 Propagate lvalue ("modifiable") context to an op and its children.
2674 C<type> represents the context type, roughly based on the type of op that
2675 would do the modifying, although C<local()> is represented by C<OP_NULL>,
2676 because it has no op type of its own (it is signalled by a flag on
2679 This function detects things that can't be modified, such as C<$x+1>, and
2680 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2681 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
2683 It also flags things that need to behave specially in an lvalue context,
2684 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2690 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2693 PadnameLVALUE_on(pn);
2694 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2696 /* RT #127786: cv can be NULL due to an eval within the DB package
2697 * called from an anon sub - anon subs don't have CvOUTSIDE() set
2698 * unless they contain an eval, but calling eval within DB
2699 * pretends the eval was done in the caller's scope.
2703 assert(CvPADLIST(cv));
2705 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2706 assert(PadnameLEN(pn));
2707 PadnameLVALUE_on(pn);
2712 S_vivifies(const OPCODE type)
2715 case OP_RV2AV: case OP_ASLICE:
2716 case OP_RV2HV: case OP_KVASLICE:
2717 case OP_RV2SV: case OP_HSLICE:
2718 case OP_AELEMFAST: case OP_KVHSLICE:
2727 S_lvref(pTHX_ OP *o, I32 type)
2731 switch (o->op_type) {
2733 for (kid = OpSIBLING(cUNOPo->op_first); kid;
2734 kid = OpSIBLING(kid))
2735 S_lvref(aTHX_ kid, type);
2740 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2741 o->op_flags |= OPf_STACKED;
2742 if (o->op_flags & OPf_PARENS) {
2743 if (o->op_private & OPpLVAL_INTRO) {
2744 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2745 "localized parenthesized array in list assignment"));
2749 OpTYPE_set(o, OP_LVAVREF);
2750 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2751 o->op_flags |= OPf_MOD|OPf_REF;
2754 o->op_private |= OPpLVREF_AV;
2757 kid = cUNOPo->op_first;
2758 if (kid->op_type == OP_NULL)
2759 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
2761 o->op_private = OPpLVREF_CV;
2762 if (kid->op_type == OP_GV)
2763 o->op_flags |= OPf_STACKED;
2764 else if (kid->op_type == OP_PADCV) {
2765 o->op_targ = kid->op_targ;
2767 op_free(cUNOPo->op_first);
2768 cUNOPo->op_first = NULL;
2769 o->op_flags &=~ OPf_KIDS;
2774 if (o->op_flags & OPf_PARENS) {
2776 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2777 "parenthesized hash in list assignment"));
2780 o->op_private |= OPpLVREF_HV;
2784 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2785 o->op_flags |= OPf_STACKED;
2788 if (o->op_flags & OPf_PARENS) goto parenhash;
2789 o->op_private |= OPpLVREF_HV;
2792 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2795 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2796 if (o->op_flags & OPf_PARENS) goto slurpy;
2797 o->op_private |= OPpLVREF_AV;
2801 o->op_private |= OPpLVREF_ELEM;
2802 o->op_flags |= OPf_STACKED;
2806 OpTYPE_set(o, OP_LVREFSLICE);
2807 o->op_private &= OPpLVAL_INTRO;
2810 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2812 else if (!(o->op_flags & OPf_KIDS))
2814 if (o->op_targ != OP_LIST) {
2815 S_lvref(aTHX_ cBINOPo->op_first, type);
2820 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
2821 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2822 S_lvref(aTHX_ kid, type);
2826 if (o->op_flags & OPf_PARENS)
2831 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2832 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2833 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2839 OpTYPE_set(o, OP_LVREF);
2841 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2842 if (type == OP_ENTERLOOP)
2843 o->op_private |= OPpLVREF_ITER;
2846 PERL_STATIC_INLINE bool
2847 S_potential_mod_type(I32 type)
2849 /* Types that only potentially result in modification. */
2850 return type == OP_GREPSTART || type == OP_ENTERSUB
2851 || type == OP_REFGEN || type == OP_LEAVESUBLV;
2855 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2859 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2862 if (!o || (PL_parser && PL_parser->error_count))
2865 if ((o->op_private & OPpTARGET_MY)
2866 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2871 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2873 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2875 switch (o->op_type) {
2880 if ((o->op_flags & OPf_PARENS))
2884 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2885 !(o->op_flags & OPf_STACKED)) {
2886 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
2887 assert(cUNOPo->op_first->op_type == OP_NULL);
2888 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2891 else { /* lvalue subroutine call */
2892 o->op_private |= OPpLVAL_INTRO;
2893 PL_modcount = RETURN_UNLIMITED_NUMBER;
2894 if (S_potential_mod_type(type)) {
2895 o->op_private |= OPpENTERSUB_INARGS;
2898 else { /* Compile-time error message: */
2899 OP *kid = cUNOPo->op_first;
2904 if (kid->op_type != OP_PUSHMARK) {
2905 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2907 "panic: unexpected lvalue entersub "
2908 "args: type/targ %ld:%" UVuf,
2909 (long)kid->op_type, (UV)kid->op_targ);
2910 kid = kLISTOP->op_first;
2912 while (OpHAS_SIBLING(kid))
2913 kid = OpSIBLING(kid);
2914 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2915 break; /* Postpone until runtime */
2918 kid = kUNOP->op_first;
2919 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2920 kid = kUNOP->op_first;
2921 if (kid->op_type == OP_NULL)
2923 "Unexpected constant lvalue entersub "
2924 "entry via type/targ %ld:%" UVuf,
2925 (long)kid->op_type, (UV)kid->op_targ);
2926 if (kid->op_type != OP_GV) {
2933 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2934 ? MUTABLE_CV(SvRV(gv))
2940 if (flags & OP_LVALUE_NO_CROAK)
2943 namesv = cv_name(cv, NULL, 0);
2944 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
2945 "subroutine call of &%" SVf " in %s",
2946 SVfARG(namesv), PL_op_desc[type]),
2954 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2955 /* grep, foreach, subcalls, refgen */
2956 if (S_potential_mod_type(type))
2958 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2959 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2962 type ? PL_op_desc[type] : "local"));
2975 case OP_RIGHT_SHIFT:
2984 if (!(o->op_flags & OPf_STACKED))
2990 if (o->op_flags & OPf_STACKED) {
2994 if (!(o->op_private & OPpREPEAT_DOLIST))
2997 const I32 mods = PL_modcount;
2998 modkids(cBINOPo->op_first, type);
2999 if (type != OP_AASSIGN)
3001 kid = cBINOPo->op_last;
3002 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
3003 const IV iv = SvIV(kSVOP_sv);
3004 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
3006 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
3009 PL_modcount = RETURN_UNLIMITED_NUMBER;
3015 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3016 op_lvalue(kid, type);
3021 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3022 PL_modcount = RETURN_UNLIMITED_NUMBER;
3023 return o; /* Treat \(@foo) like ordinary list. */
3027 if (scalar_mod_type(o, type))
3029 ref(cUNOPo->op_first, o->op_type);
3036 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
3037 if (type == OP_LEAVESUBLV && (
3038 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
3039 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3041 o->op_private |= OPpMAYBE_LVSUB;
3045 PL_modcount = RETURN_UNLIMITED_NUMBER;
3050 if (type == OP_LEAVESUBLV)
3051 o->op_private |= OPpMAYBE_LVSUB;
3054 if (type == OP_LEAVESUBLV
3055 && (o->op_private & 3) + OP_EACH == OP_KEYS)
3056 o->op_private |= OPpMAYBE_LVSUB;
3059 PL_hints |= HINT_BLOCK_SCOPE;
3060 if (type == OP_LEAVESUBLV)
3061 o->op_private |= OPpMAYBE_LVSUB;
3065 ref(cUNOPo->op_first, o->op_type);
3069 PL_hints |= HINT_BLOCK_SCOPE;
3079 case OP_AELEMFAST_LEX:
3086 PL_modcount = RETURN_UNLIMITED_NUMBER;
3087 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
3088 return o; /* Treat \(@foo) like ordinary list. */
3089 if (scalar_mod_type(o, type))
3091 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3092 && type == OP_LEAVESUBLV)
3093 o->op_private |= OPpMAYBE_LVSUB;
3097 if (!type) /* local() */
3098 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
3099 PNfARG(PAD_COMPNAME(o->op_targ)));
3100 if (!(o->op_private & OPpLVAL_INTRO)
3101 || ( type != OP_SASSIGN && type != OP_AASSIGN
3102 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
3103 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
3111 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
3115 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
3121 if (type == OP_LEAVESUBLV)
3122 o->op_private |= OPpMAYBE_LVSUB;
3123 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
3124 /* substr and vec */
3125 /* If this op is in merely potential (non-fatal) modifiable
3126 context, then apply OP_ENTERSUB context to
3127 the kid op (to avoid croaking). Other-
3128 wise pass this op’s own type so the correct op is mentioned
3129 in error messages. */
3130 op_lvalue(OpSIBLING(cBINOPo->op_first),
3131 S_potential_mod_type(type)
3139 ref(cBINOPo->op_first, o->op_type);
3140 if (type == OP_ENTERSUB &&
3141 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3142 o->op_private |= OPpLVAL_DEFER;
3143 if (type == OP_LEAVESUBLV)
3144 o->op_private |= OPpMAYBE_LVSUB;
3151 o->op_private |= OPpLVALUE;
3157 if (o->op_flags & OPf_KIDS)
3158 op_lvalue(cLISTOPo->op_last, type);
3163 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3165 else if (!(o->op_flags & OPf_KIDS))
3167 if (o->op_targ != OP_LIST) {
3168 op_lvalue(cBINOPo->op_first, type);
3174 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3175 /* elements might be in void context because the list is
3176 in scalar context or because they are attribute sub calls */
3177 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
3178 op_lvalue(kid, type);
3186 if (type == OP_LEAVESUBLV
3187 || !S_vivifies(cLOGOPo->op_first->op_type))
3188 op_lvalue(cLOGOPo->op_first, type);
3189 if (type == OP_LEAVESUBLV
3190 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3191 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
3195 if (type == OP_NULL) { /* local */
3197 if (!FEATURE_MYREF_IS_ENABLED)
3198 Perl_croak(aTHX_ "The experimental declared_refs "
3199 "feature is not enabled");
3200 Perl_ck_warner_d(aTHX_
3201 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
3202 "Declaring references is experimental");
3203 op_lvalue(cUNOPo->op_first, OP_NULL);
3206 if (type != OP_AASSIGN && type != OP_SASSIGN
3207 && type != OP_ENTERLOOP)
3209 /* Don’t bother applying lvalue context to the ex-list. */
3210 kid = cUNOPx(cUNOPo->op_first)->op_first;
3211 assert (!OpHAS_SIBLING(kid));
3214 if (type == OP_NULL) /* local */
3216 if (type != OP_AASSIGN) goto nomod;
3217 kid = cUNOPo->op_first;
3220 const U8 ec = PL_parser ? PL_parser->error_count : 0;
3221 S_lvref(aTHX_ kid, type);
3222 if (!PL_parser || PL_parser->error_count == ec) {
3223 if (!FEATURE_REFALIASING_IS_ENABLED)
3225 "Experimental aliasing via reference not enabled");
3226 Perl_ck_warner_d(aTHX_
3227 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3228 "Aliasing via reference is experimental");
3231 if (o->op_type == OP_REFGEN)
3232 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3237 if ((o->op_private & OPpSPLIT_ASSIGN)) {
3238 /* This is actually @array = split. */
3239 PL_modcount = RETURN_UNLIMITED_NUMBER;
3245 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3249 /* [20011101.069 (#7861)] File test operators interpret OPf_REF to mean that
3250 their argument is a filehandle; thus \stat(".") should not set
3252 if (type == OP_REFGEN &&
3253 PL_check[o->op_type] == Perl_ck_ftst)
3256 if (type != OP_LEAVESUBLV)
3257 o->op_flags |= OPf_MOD;
3259 if (type == OP_AASSIGN || type == OP_SASSIGN)
3260 o->op_flags |= OPf_SPECIAL
3261 |(o->op_type == OP_ENTERSUB ? 0 : OPf_REF);
3262 else if (!type) { /* local() */
3265 o->op_private |= OPpLVAL_INTRO;
3266 o->op_flags &= ~OPf_SPECIAL;
3267 PL_hints |= HINT_BLOCK_SCOPE;
3272 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3273 "Useless localization of %s", OP_DESC(o));
3276 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3277 && type != OP_LEAVESUBLV && o->op_type != OP_ENTERSUB)
3278 o->op_flags |= OPf_REF;
3283 S_scalar_mod_type(const OP *o, I32 type)
3288 if (o && o->op_type == OP_RV2GV)
3312 case OP_RIGHT_SHIFT:
3341 S_is_handle_constructor(const OP *o, I32 numargs)
3343 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3345 switch (o->op_type) {
3353 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3366 S_refkids(pTHX_ OP *o, I32 type)
3368 if (o && o->op_flags & OPf_KIDS) {
3370 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3377 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3382 PERL_ARGS_ASSERT_DOREF;
3384 if (PL_parser && PL_parser->error_count)
3387 switch (o->op_type) {
3389 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3390 !(o->op_flags & OPf_STACKED)) {
3391 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
3392 assert(cUNOPo->op_first->op_type == OP_NULL);
3393 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
3394 o->op_flags |= OPf_SPECIAL;
3396 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3397 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3398 : type == OP_RV2HV ? OPpDEREF_HV
3400 o->op_flags |= OPf_MOD;
3406 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3407 doref(kid, type, set_op_ref);
3410 if (type == OP_DEFINED)
3411 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3412 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3415 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3416 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3417 : type == OP_RV2HV ? OPpDEREF_HV
3419 o->op_flags |= OPf_MOD;
3426 o->op_flags |= OPf_REF;
3429 if (type == OP_DEFINED)
3430 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3431 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3437 o->op_flags |= OPf_REF;
3442 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3444 doref(cBINOPo->op_first, type, set_op_ref);
3448 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3449 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3450 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3451 : type == OP_RV2HV ? OPpDEREF_HV
3453 o->op_flags |= OPf_MOD;
3463 if (!(o->op_flags & OPf_KIDS))
3465 doref(cLISTOPo->op_last, type, set_op_ref);
3475 S_dup_attrlist(pTHX_ OP *o)
3479 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3481 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3482 * where the first kid is OP_PUSHMARK and the remaining ones
3483 * are OP_CONST. We need to push the OP_CONST values.
3485 if (o->op_type == OP_CONST)
3486 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3488 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3490 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
3491 if (o->op_type == OP_CONST)
3492 rop = op_append_elem(OP_LIST, rop,
3493 newSVOP(OP_CONST, o->op_flags,
3494 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3501 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3503 PERL_ARGS_ASSERT_APPLY_ATTRS;
3505 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
3507 /* fake up C<use attributes $pkg,$rv,@attrs> */
3509 #define ATTRSMODULE "attributes"
3510 #define ATTRSMODULE_PM "attributes.pm"
3513 aTHX_ PERL_LOADMOD_IMPORT_OPS,
3514 newSVpvs(ATTRSMODULE),
3516 op_prepend_elem(OP_LIST,
3517 newSVOP(OP_CONST, 0, stashsv),
3518 op_prepend_elem(OP_LIST,
3519 newSVOP(OP_CONST, 0,
3521 dup_attrlist(attrs))));
3526 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3528 OP *pack, *imop, *arg;
3529 SV *meth, *stashsv, **svp;
3531 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3536 assert(target->op_type == OP_PADSV ||
3537 target->op_type == OP_PADHV ||
3538 target->op_type == OP_PADAV);
3540 /* Ensure that attributes.pm is loaded. */
3541 /* Don't force the C<use> if we don't need it. */
3542 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3543 if (svp && *svp != &PL_sv_undef)
3544 NOOP; /* already in %INC */
3546 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3547 newSVpvs(ATTRSMODULE), NULL);
3549 /* Need package name for method call. */
3550 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3552 /* Build up the real arg-list. */
3553 stashsv = newSVhek(HvNAME_HEK(stash));
3555 arg = newOP(OP_PADSV, 0);
3556 arg->op_targ = target->op_targ;
3557 arg = op_prepend_elem(OP_LIST,
3558 newSVOP(OP_CONST, 0, stashsv),
3559 op_prepend_elem(OP_LIST,
3560 newUNOP(OP_REFGEN, 0,
3562 dup_attrlist(attrs)));
3564 /* Fake up a method call to import */
3565 meth = newSVpvs_share("import");
3566 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3567 op_append_elem(OP_LIST,
3568 op_prepend_elem(OP_LIST, pack, arg),
3569 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3571 /* Combine the ops. */
3572 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3576 =notfor apidoc apply_attrs_string
3578 Attempts to apply a list of attributes specified by the C<attrstr> and
3579 C<len> arguments to the subroutine identified by the C<cv> argument which
3580 is expected to be associated with the package identified by the C<stashpv>
3581 argument (see L<attributes>). It gets this wrong, though, in that it
3582 does not correctly identify the boundaries of the individual attribute
3583 specifications within C<attrstr>. This is not really intended for the
3584 public API, but has to be listed here for systems such as AIX which
3585 need an explicit export list for symbols. (It's called from XS code
3586 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3587 to respect attribute syntax properly would be welcome.
3593 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3594 const char *attrstr, STRLEN len)
3598 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3601 len = strlen(attrstr);
3605 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3607 const char * const sstr = attrstr;
3608 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3609 attrs = op_append_elem(OP_LIST, attrs,
3610 newSVOP(OP_CONST, 0,
3611 newSVpvn(sstr, attrstr-sstr)));
3615 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3616 newSVpvs(ATTRSMODULE),
3617 NULL, op_prepend_elem(OP_LIST,
3618 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3619 op_prepend_elem(OP_LIST,
3620 newSVOP(OP_CONST, 0,
3621 newRV(MUTABLE_SV(cv))),
3626 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3628 OP *new_proto = NULL;
3633 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3639 if (o->op_type == OP_CONST) {
3640 pv = SvPV(cSVOPo_sv, pvlen);
3641 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3642 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3643 SV ** const tmpo = cSVOPx_svp(o);
3644 SvREFCNT_dec(cSVOPo_sv);
3649 } else if (o->op_type == OP_LIST) {
3651 assert(o->op_flags & OPf_KIDS);
3652 lasto = cLISTOPo->op_first;
3653 assert(lasto->op_type == OP_PUSHMARK);
3654 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
3655 if (o->op_type == OP_CONST) {
3656 pv = SvPV(cSVOPo_sv, pvlen);
3657 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3658 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3659 SV ** const tmpo = cSVOPx_svp(o);
3660 SvREFCNT_dec(cSVOPo_sv);
3662 if (new_proto && ckWARN(WARN_MISC)) {
3664 const char * newp = SvPV(cSVOPo_sv, new_len);
3665 Perl_warner(aTHX_ packWARN(WARN_MISC),
3666 "Attribute prototype(%" UTF8f ") discards earlier prototype attribute in same sub",
3667 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3673 /* excise new_proto from the list */
3674 op_sibling_splice(*attrs, lasto, 1, NULL);
3681 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3682 would get pulled in with no real need */
3683 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3692 svname = sv_newmortal();
3693 gv_efullname3(svname, name, NULL);
3695 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3696 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3698 svname = (SV *)name;
3699 if (ckWARN(WARN_ILLEGALPROTO))
3700 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3701 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3702 STRLEN old_len, new_len;
3703 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3704 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3706 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3707 "Prototype '%" UTF8f "' overridden by attribute 'prototype(%" UTF8f ")'"
3709 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3710 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3720 S_cant_declare(pTHX_ OP *o)
3722 if (o->op_type == OP_NULL
3723 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3724 o = cUNOPo->op_first;
3725 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3726 o->op_type == OP_NULL
3727 && o->op_flags & OPf_SPECIAL
3730 PL_parser->in_my == KEY_our ? "our" :
3731 PL_parser->in_my == KEY_state ? "state" :
3736 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3739 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3741 PERL_ARGS_ASSERT_MY_KID;
3743 if (!o || (PL_parser && PL_parser->error_count))
3748 if (OP_TYPE_IS_OR_WAS(o, OP_LIST)) {
3750 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3751 my_kid(kid, attrs, imopsp);
3753 } else if (type == OP_UNDEF || type == OP_STUB) {
3755 } else if (type == OP_RV2SV || /* "our" declaration */
3758 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3759 S_cant_declare(aTHX_ o);
3761 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3763 PL_parser->in_my = FALSE;
3764 PL_parser->in_my_stash = NULL;
3765 apply_attrs(GvSTASH(gv),
3766 (type == OP_RV2SV ? GvSV(gv) :
3767 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3768 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3771 o->op_private |= OPpOUR_INTRO;
3774 else if (type == OP_REFGEN || type == OP_SREFGEN) {
3775 if (!FEATURE_MYREF_IS_ENABLED)
3776 Perl_croak(aTHX_ "The experimental declared_refs "
3777 "feature is not enabled");
3778 Perl_ck_warner_d(aTHX_
3779 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
3780 "Declaring references is experimental");
3781 /* Kid is a nulled OP_LIST, handled above. */
3782 my_kid(cUNOPo->op_first, attrs, imopsp);
3785 else if (type != OP_PADSV &&
3788 type != OP_PUSHMARK)
3790 S_cant_declare(aTHX_ o);
3793 else if (attrs && type != OP_PUSHMARK) {
3797 PL_parser->in_my = FALSE;
3798 PL_parser->in_my_stash = NULL;
3800 /* check for C<my Dog $spot> when deciding package */
3801 stash = PAD_COMPNAME_TYPE(o->op_targ);
3803 stash = PL_curstash;
3804 apply_attrs_my(stash, o, attrs, imopsp);
3806 o->op_flags |= OPf_MOD;
3807 o->op_private |= OPpLVAL_INTRO;
3809 o->op_private |= OPpPAD_STATE;
3814 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3817 int maybe_scalar = 0;
3819 PERL_ARGS_ASSERT_MY_ATTRS;
3821 /* [perl #17376]: this appears to be premature, and results in code such as
3822 C< our(%x); > executing in list mode rather than void mode */
3824 if (o->op_flags & OPf_PARENS)
3834 o = my_kid(o, attrs, &rops);
3836 if (maybe_scalar && o->op_type == OP_PADSV) {
3837 o = scalar(op_append_list(OP_LIST, rops, o));
3838 o->op_private |= OPpLVAL_INTRO;
3841 /* The listop in rops might have a pushmark at the beginning,
3842 which will mess up list assignment. */
3843 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3844 if (rops->op_type == OP_LIST &&
3845 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3847 OP * const pushmark = lrops->op_first;
3848 /* excise pushmark */
3849 op_sibling_splice(rops, NULL, 1, NULL);
3852 o = op_append_list(OP_LIST, o, rops);
3855 PL_parser->in_my = FALSE;
3856 PL_parser->in_my_stash = NULL;
3861 Perl_sawparens(pTHX_ OP *o)
3863 PERL_UNUSED_CONTEXT;
3865 o->op_flags |= OPf_PARENS;
3870 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3874 const OPCODE ltype = left->op_type;
3875 const OPCODE rtype = right->op_type;
3877 PERL_ARGS_ASSERT_BIND_MATCH;
3879 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3880 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3882 const char * const desc
3884 rtype == OP_SUBST || rtype == OP_TRANS
3885 || rtype == OP_TRANSR
3887 ? (int)rtype : OP_MATCH];
3888 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3890 S_op_varname(aTHX_ left);
3892 Perl_warner(aTHX_ packWARN(WARN_MISC),
3893 "Applying %s to %" SVf " will act on scalar(%" SVf ")",
3894 desc, SVfARG(name), SVfARG(name));
3896 const char * const sample = (isary
3897 ? "@array" : "%hash");
3898 Perl_warner(aTHX_ packWARN(WARN_MISC),
3899 "Applying %s to %s will act on scalar(%s)",
3900 desc, sample, sample);
3904 if (rtype == OP_CONST &&
3905 cSVOPx(right)->op_private & OPpCONST_BARE &&
3906 cSVOPx(right)->op_private & OPpCONST_STRICT)
3908 no_bareword_allowed(right);
3911 /* !~ doesn't make sense with /r, so error on it for now */
3912 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3914 /* diag_listed_as: Using !~ with %s doesn't make sense */
3915 yyerror("Using !~ with s///r doesn't make sense");
3916 if (rtype == OP_TRANSR && type == OP_NOT)
3917 /* diag_listed_as: Using !~ with %s doesn't make sense */
3918 yyerror("Using !~ with tr///r doesn't make sense");
3920 ismatchop = (rtype == OP_MATCH ||
3921 rtype == OP_SUBST ||
3922 rtype == OP_TRANS || rtype == OP_TRANSR)
3923 && !(right->op_flags & OPf_SPECIAL);
3924 if (ismatchop && right->op_private & OPpTARGET_MY) {
3926 right->op_private &= ~OPpTARGET_MY;
3928 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
3929 if (left->op_type == OP_PADSV
3930 && !(left->op_private & OPpLVAL_INTRO))
3932 right->op_targ = left->op_targ;
3937 right->op_flags |= OPf_STACKED;
3938 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3939 ! (rtype == OP_TRANS &&
3940 right->op_private & OPpTRANS_IDENTICAL) &&
3941 ! (rtype == OP_SUBST &&
3942 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3943 left = op_lvalue(left, rtype);
3944 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3945 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
3947 o = op_prepend_elem(rtype, scalar(left), right);
3950 return newUNOP(OP_NOT, 0, scalar(o));
3954 return bind_match(type, left,
3955 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
3959 Perl_invert(pTHX_ OP *o)
3963 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3967 =for apidoc Amx|OP *|op_scope|OP *o
3969 Wraps up an op tree with some additional ops so that at runtime a dynamic
3970 scope will be created. The original ops run in the new dynamic scope,
3971 and then, provided that they exit normally, the scope will be unwound.
3972 The additional ops used to create and unwind the dynamic scope will
3973 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3974 instead if the ops are simple enough to not need the full dynamic scope
3981 Perl_op_scope(pTHX_ OP *o)
3985 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3986 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3987 OpTYPE_set(o, OP_LEAVE);
3989 else if (o->op_type == OP_LINESEQ) {
3991 OpTYPE_set(o, OP_SCOPE);
3992 kid = ((LISTOP*)o)->op_first;
3993 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3996 /* The following deals with things like 'do {1 for 1}' */
3997 kid = OpSIBLING(kid);
3999 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
4004 o = newLISTOP(OP_SCOPE, 0, o, NULL);
4010 Perl_op_unscope(pTHX_ OP *o)
4012 if (o && o->op_type == OP_LINESEQ) {
4013 OP *kid = cLISTOPo->op_first;
4014 for(; kid; kid = OpSIBLING(kid))
4015 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
4022 =for apidoc Am|int|block_start|int full
4024 Handles compile-time scope entry.
4025 Arranges for hints to be restored on block
4026 exit and also handles pad sequence numbers to make lexical variables scope
4027 right. Returns a savestack index for use with C<block_end>.
4033 Perl_block_start(pTHX_ int full)
4035 const int retval = PL_savestack_ix;
4037 PL_compiling.cop_seq = PL_cop_seqmax;
4039 pad_block_start(full);
4041 PL_hints &= ~HINT_BLOCK_SCOPE;
4042 SAVECOMPILEWARNINGS();
4043 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
4044 SAVEI32(PL_compiling.cop_seq);
4045 PL_compiling.cop_seq = 0;
4047 CALL_BLOCK_HOOKS(bhk_start, full);
4053 =for apidoc Am|OP *|block_end|I32 floor|OP *seq
4055 Handles compile-time scope exit. C<floor>
4056 is the savestack index returned by
4057 C<block_start>, and C<seq> is the body of the block. Returns the block,
4064 Perl_block_end(pTHX_ I32 floor, OP *seq)
4066 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
4067 OP* retval = scalarseq(seq);
4070 /* XXX Is the null PL_parser check necessary here? */
4071 assert(PL_parser); /* Let’s find out under debugging builds. */
4072 if (PL_parser && PL_parser->parsed_sub) {
4073 o = newSTATEOP(0, NULL, NULL);
4075 retval = op_append_elem(OP_LINESEQ, retval, o);
4078 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
4082 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
4086 /* pad_leavemy has created a sequence of introcv ops for all my
4087 subs declared in the block. We have to replicate that list with
4088 clonecv ops, to deal with this situation:
4093 sub s1 { state sub foo { \&s2 } }
4096 Originally, I was going to have introcv clone the CV and turn
4097 off the stale flag. Since &s1 is declared before &s2, the
4098 introcv op for &s1 is executed (on sub entry) before the one for
4099 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
4100 cloned, since it is a state sub) closes over &s2 and expects
4101 to see it in its outer CV’s pad. If the introcv op clones &s1,
4102 then &s2 is still marked stale. Since &s1 is not active, and
4103 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
4104 ble will not stay shared’ warning. Because it is the same stub
4105 that will be used when the introcv op for &s2 is executed, clos-
4106 ing over it is safe. Hence, we have to turn off the stale flag
4107 on all lexical subs in the block before we clone any of them.
4108 Hence, having introcv clone the sub cannot work. So we create a
4109 list of ops like this:
4133 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
4134 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
4135 for (;; kid = OpSIBLING(kid)) {
4136 OP *newkid = newOP(OP_CLONECV, 0);
4137 newkid->op_targ = kid->op_targ;
4138 o = op_append_elem(OP_LINESEQ, o, newkid);
4139 if (kid == last) break;
4141 retval = op_prepend_elem(OP_LINESEQ, o, retval);
4144 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
4150 =head1 Compile-time scope hooks
4152 =for apidoc Aox||blockhook_register
4154 Register a set of hooks to be called when the Perl lexical scope changes
4155 at compile time. See L<perlguts/"Compile-time scope hooks">.
4161 Perl_blockhook_register(pTHX_ BHK *hk)
4163 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
4165 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
4169 Perl_newPROG(pTHX_ OP *o)
4171 PERL_ARGS_ASSERT_NEWPROG;
4178 PL_eval_root = newUNOP(OP_LEAVEEVAL,
4179 ((PL_in_eval & EVAL_KEEPERR)
4180 ? OPf_SPECIAL : 0), o);
4183 assert(CxTYPE(cx) == CXt_EVAL);
4185 if ((cx->blk_gimme & G_WANT) == G_VOID)
4186 scalarvoid(PL_eval_root);
4187 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
4190 scalar(PL_eval_root);
4192 PL_eval_start = op_linklist(PL_eval_root);
4193 PL_eval_root->op_private |= OPpREFCOUNTED;
4194 OpREFCNT_set(PL_eval_root, 1);
4195 PL_eval_root->op_next = 0;
4196 i = PL_savestack_ix;
4199 CALL_PEEP(PL_eval_start);
4200 finalize_optree(PL_eval_root);
4201 S_prune_chain_head(&PL_eval_start);
4203 PL_savestack_ix = i;
4206 if (o->op_type == OP_STUB) {
4207 /* This block is entered if nothing is compiled for the main
4208 program. This will be the case for an genuinely empty main
4209 program, or one which only has BEGIN blocks etc, so already
4212 Historically (5.000) the guard above was !o. However, commit
4213 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
4214 c71fccf11fde0068, changed perly.y so that newPROG() is now
4215 called with the output of block_end(), which returns a new
4216 OP_STUB for the case of an empty optree. ByteLoader (and
4217 maybe other things) also take this path, because they set up
4218 PL_main_start and PL_main_root directly, without generating an
4221 If the parsing the main program aborts (due to parse errors,
4222 or due to BEGIN or similar calling exit), then newPROG()
4223 isn't even called, and hence this code path and its cleanups
4224 are skipped. This shouldn't make a make a difference:
4225 * a non-zero return from perl_parse is a failure, and
4226 perl_destruct() should be called immediately.
4227 * however, if exit(0) is called during the parse, then
4228 perl_parse() returns 0, and perl_run() is called. As
4229 PL_main_start will be NULL, perl_run() will return
4230 promptly, and the exit code will remain 0.
4233 PL_comppad_name = 0;
4235 S_op_destroy(aTHX_ o);
4238 PL_main_root = op_scope(sawparens(scalarvoid(o)));
4239 PL_curcop = &PL_compiling;
4240 PL_main_start = LINKLIST(PL_main_root);
4241 PL_main_root->op_private |= OPpREFCOUNTED;
4242 OpREFCNT_set(PL_main_root, 1);
4243 PL_main_root->op_next = 0;
4244 CALL_PEEP(PL_main_start);
4245 finalize_optree(PL_main_root);
4246 S_prune_chain_head(&PL_main_start);
4247 cv_forget_slab(PL_compcv);
4250 /* Register with debugger */
4252 CV * const cv = get_cvs("DB::postponed", 0);
4256 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
4258 call_sv(MUTABLE_SV(cv), G_DISCARD);
4265 Perl_localize(pTHX_ OP *o, I32 lex)
4267 PERL_ARGS_ASSERT_LOCALIZE;
4269 if (o->op_flags & OPf_PARENS)
4270 /* [perl #17376]: this appears to be premature, and results in code such as
4271 C< our(%x); > executing in list mode rather than void mode */
4278 if ( PL_parser->bufptr > PL_parser->oldbufptr
4279 && PL_parser->bufptr[-1] == ','
4280 && ckWARN(WARN_PARENTHESIS))
4282 char *s = PL_parser->bufptr;
4285 /* some heuristics to detect a potential error */
4286 while (*s && (strchr(", \t\n", *s)))
4290 if (*s && (strchr("@$%", *s) || (!lex && *s == '*'))
4292 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
4295 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
4297 while (*s && (strchr(", \t\n", *s)))
4303 if (sigil && (*s == ';' || *s == '=')) {
4304 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
4305 "Parentheses missing around \"%s\" list",
4307 ? (PL_parser->in_my == KEY_our
4309 : PL_parser->in_my == KEY_state
4319 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4320 PL_parser->in_my = FALSE;
4321 PL_parser->in_my_stash = NULL;
4326 Perl_jmaybe(pTHX_ OP *o)
4328 PERL_ARGS_ASSERT_JMAYBE;
4330 if (o->op_type == OP_LIST) {
4332 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4333 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4338 PERL_STATIC_INLINE OP *
4339 S_op_std_init(pTHX_ OP *o)
4341 I32 type = o->op_type;
4343 PERL_ARGS_ASSERT_OP_STD_INIT;
4345 if (PL_opargs[type] & OA_RETSCALAR)
4347 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4348 o->op_targ = pad_alloc(type, SVs_PADTMP);
4353 PERL_STATIC_INLINE OP *
4354 S_op_integerize(pTHX_ OP *o)
4356 I32 type = o->op_type;
4358 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4360 /* integerize op. */
4361 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4364 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4367 if (type == OP_NEGATE)
4368 /* XXX might want a ck_negate() for this */
4369 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4375 S_fold_constants(pTHX_ OP *const o)
4380 VOL I32 type = o->op_type;
4385 SV * const oldwarnhook = PL_warnhook;
4386 SV * const olddiehook = PL_diehook;
4388 U8 oldwarn = PL_dowarn;
4392 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4394 if (!(PL_opargs[type] & OA_FOLDCONST))
4403 #ifdef USE_LOCALE_CTYPE
4404 if (IN_LC_COMPILETIME(LC_CTYPE))
4413 #ifdef USE_LOCALE_COLLATE
4414 if (IN_LC_COMPILETIME(LC_COLLATE))
4419 /* XXX what about the numeric ops? */
4420 #ifdef USE_LOCALE_NUMERIC
4421 if (IN_LC_COMPILETIME(LC_NUMERIC))
4426 if (!OpHAS_SIBLING(cLISTOPo->op_first)
4427 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4430 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
4431 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4433 const char *s = SvPVX_const(sv);
4434 while (s < SvEND(sv)) {
4435 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4442 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4445 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4446 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4450 if (PL_parser && PL_parser->error_count)
4451 goto nope; /* Don't try to run w/ errors */
4453 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4454 switch (curop->op_type) {
4456 if ( (curop->op_private & OPpCONST_BARE)
4457 && (curop->op_private & OPpCONST_STRICT)) {
4458 no_bareword_allowed(curop);
4466 /* Foldable; move to next op in list */
4470 /* No other op types are considered foldable */
4475 curop = LINKLIST(o);
4476 old_next = o->op_next;
4480 old_cxix = cxstack_ix;
4481 create_eval_scope(NULL, G_FAKINGEVAL);
4483 /* Verify that we don't need to save it: */
4484 assert(PL_curcop == &PL_compiling);
4485 StructCopy(&PL_compiling, ¬_compiling, COP);
4486 PL_curcop = ¬_compiling;
4487 /* The above ensures that we run with all the correct hints of the
4488 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4489 assert(IN_PERL_RUNTIME);
4490 PL_warnhook = PERL_WARNHOOK_FATAL;
4494 /* Effective $^W=1. */
4495 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4496 PL_dowarn |= G_WARN_ON;
4501 sv = *(PL_stack_sp--);
4502 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4503 pad_swipe(o->op_targ, FALSE);
4505 else if (SvTEMP(sv)) { /* grab mortal temp? */
4506 SvREFCNT_inc_simple_void(sv);
4509 else { assert(SvIMMORTAL(sv)); }
4512 /* Something tried to die. Abandon constant folding. */
4513 /* Pretend the error never happened. */
4515 o->op_next = old_next;
4519 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4520 PL_warnhook = oldwarnhook;
4521 PL_diehook = olddiehook;
4522 /* XXX note that this croak may fail as we've already blown away
4523 * the stack - eg any nested evals */
4524 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4527 PL_dowarn = oldwarn;
4528 PL_warnhook = oldwarnhook;
4529 PL_diehook = olddiehook;
4530 PL_curcop = &PL_compiling;
4532 /* if we croaked, depending on how we croaked the eval scope
4533 * may or may not have already been popped */
4534 if (cxstack_ix > old_cxix) {
4535 assert(cxstack_ix == old_cxix + 1);
4536 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
4537 delete_eval_scope();
4542 /* OP_STRINGIFY and constant folding are used to implement qq.
4543 Here the constant folding is an implementation detail that we
4544 want to hide. If the stringify op is itself already marked
4545 folded, however, then it is actually a folded join. */
4546 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4551 else if (!SvIMMORTAL(sv)) {
4555 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4556 if (!is_stringify) newop->op_folded = 1;
4564 S_gen_constant_list(pTHX_ OP *o)
4568 const SSize_t oldtmps_floor = PL_tmps_floor;
4573 if (PL_parser && PL_parser->error_count)
4574 return o; /* Don't attempt to run with errors */
4576 curop = LINKLIST(o);
4579 S_prune_chain_head(&curop);
4581 Perl_pp_pushmark(aTHX);
4584 assert (!(curop->op_flags & OPf_SPECIAL));
4585 assert(curop->op_type == OP_RANGE);
4586 Perl_pp_anonlist(aTHX);
4587 PL_tmps_floor = oldtmps_floor;
4589 OpTYPE_set(o, OP_RV2AV);
4590 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4591 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4592 o->op_opt = 0; /* needs to be revisited in rpeep() */
4593 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4595 /* replace subtree with an OP_CONST */
4596 curop = ((UNOP*)o)->op_first;
4597 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4600 if (AvFILLp(av) != -1)
4601 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4604 SvREADONLY_on(*svp);
4611 =head1 Optree Manipulation Functions
4614 /* List constructors */
4617 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4619 Append an item to the list of ops contained directly within a list-type
4620 op, returning the lengthened list. C<first> is the list-type op,
4621 and C<last> is the op to append to the list. C<optype> specifies the
4622 intended opcode for the list. If C<first> is not already a list of the
4623 right type, it will be upgraded into one. If either C<first> or C<last>
4624 is null, the other is returned unchanged.
4630 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4638 if (first->op_type != (unsigned)type
4639 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4641 return newLISTOP(type, 0, first, last);
4644 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4645 first->op_flags |= OPf_KIDS;
4650 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4652 Concatenate the lists of ops contained directly within two list-type ops,
4653 returning the combined list. C<first> and C<last> are the list-type ops
4654 to concatenate. C<optype> specifies the intended opcode for the list.
4655 If either C<first> or C<last> is not already a list of the right type,
4656 it will be upgraded into one. If either C<first> or C<last> is null,
4657 the other is returned unchanged.
4663 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4671 if (first->op_type != (unsigned)type)
4672 return op_prepend_elem(type, first, last);
4674 if (last->op_type != (unsigned)type)
4675 return op_append_elem(type, first, last);
4677 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4678 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4679 OpLASTSIB_set(((LISTOP*)first)->op_last, first);
4680 first->op_flags |= (last->op_flags & OPf_KIDS);
4682 S_op_destroy(aTHX_ last);
4688 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4690 Prepend an item to the list of ops contained directly within a list-type
4691 op, returning the lengthened list. C<first> is the op to prepend to the
4692 list, and C<last> is the list-type op. C<optype> specifies the intended
4693 opcode for the list. If C<last> is not already a list of the right type,
4694 it will be upgraded into one. If either C<first> or C<last> is null,
4695 the other is returned unchanged.
4701 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4709 if (last->op_type == (unsigned)type) {
4710 if (type == OP_LIST) { /* already a PUSHMARK there */
4711 /* insert 'first' after pushmark */
4712 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4713 if (!(first->op_flags & OPf_PARENS))
4714 last->op_flags &= ~OPf_PARENS;
4717 op_sibling_splice(last, NULL, 0, first);
4718 last->op_flags |= OPf_KIDS;
4722 return newLISTOP(type, 0, first, last);
4726 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4728 Converts C<o> into a list op if it is not one already, and then converts it
4729 into the specified C<type>, calling its check function, allocating a target if
4730 it needs one, and folding constants.
4732 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4733 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4734 C<op_convert_list> to make it the right type.
4740 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4743 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4744 if (!o || o->op_type != OP_LIST)
4745 o = force_list(o, 0);
4748 o->op_flags &= ~OPf_WANT;
4749 o->op_private &= ~OPpLVAL_INTRO;
4752 if (!(PL_opargs[type] & OA_MARK))
4753 op_null(cLISTOPo->op_first);
4755 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
4756 if (kid2 && kid2->op_type == OP_COREARGS) {
4757 op_null(cLISTOPo->op_first);
4758 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4762 if (type != OP_SPLIT)
4763 /* At this point o is a LISTOP, but OP_SPLIT is a PMOP; let
4764 * ck_split() create a real PMOP and leave the op's type as listop
4765 * for now. Otherwise op_free() etc will crash.
4767 OpTYPE_set(o, type);
4769 o->op_flags |= flags;
4770 if (flags & OPf_FOLDED)
4773 o = CHECKOP(type, o);
4774 if (o->op_type != (unsigned)type)
4777 return fold_constants(op_integerize(op_std_init(o)));
4784 =head1 Optree construction
4786 =for apidoc Am|OP *|newNULLLIST
4788 Constructs, checks, and returns a new C<stub> op, which represents an
4789 empty list expression.
4795 Perl_newNULLLIST(pTHX)
4797 return newOP(OP_STUB, 0);
4800 /* promote o and any siblings to be a list if its not already; i.e.
4808 * pushmark - o - A - B
4810 * If nullit it true, the list op is nulled.
4814 S_force_list(pTHX_ OP *o, bool nullit)
4816 if (!o || o->op_type != OP_LIST) {
4819 /* manually detach any siblings then add them back later */
4820 rest = OpSIBLING(o);
4821 OpLASTSIB_set(o, NULL);
4823 o = newLISTOP(OP_LIST, 0, o, NULL);
4825 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4833 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4835 Constructs, checks, and returns an op of any list type. C<type> is
4836 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4837 C<OPf_KIDS> will be set automatically if required. C<first> and C<last>
4838 supply up to two ops to be direct children of the list op; they are
4839 consumed by this function and become part of the constructed op tree.
4841 For most list operators, the check function expects all the kid ops to be
4842 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
4843 appropriate. What you want to do in that case is create an op of type
4844 C<OP_LIST>, append more children to it, and then call L</op_convert_list>.
4845 See L</op_convert_list> for more information.
4852 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4857 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
4858 || type == OP_CUSTOM);
4860 NewOp(1101, listop, 1, LISTOP);
4862 OpTYPE_set(listop, type);
4865 listop->op_flags = (U8)flags;
4869 else if (!first && last)
4872 OpMORESIB_set(first, last);
4873 listop->op_first = first;
4874 listop->op_last = last;
4875 if (type == OP_LIST) {
4876 OP* const pushop = newOP(OP_PUSHMARK, 0);
4877 OpMORESIB_set(pushop, first);
4878 listop->op_first = pushop;
4879 listop->op_flags |= OPf_KIDS;
4881 listop->op_last = pushop;
4883 if (listop->op_last)
4884 OpLASTSIB_set(listop->op_last, (OP*)listop);
4886 return CHECKOP(type, listop);
4890 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4892 Constructs, checks, and returns an op of any base type (any type that
4893 has no extra fields). C<type> is the opcode. C<flags> gives the
4894 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4901 Perl_newOP(pTHX_ I32 type, I32 flags)
4906 if (type == -OP_ENTEREVAL) {
4907 type = OP_ENTEREVAL;
4908 flags |= OPpEVAL_BYTES<<8;
4911 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4912 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4913 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4914 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4916 NewOp(1101, o, 1, OP);
4917 OpTYPE_set(o, type);
4918 o->op_flags = (U8)flags;
4921 o->op_private = (U8)(0 | (flags >> 8));
4922 if (PL_opargs[type] & OA_RETSCALAR)
4924 if (PL_opargs[type] & OA_TARGET)
4925 o->op_targ = pad_alloc(type, SVs_PADTMP);
4926 return CHECKOP(type, o);
4930 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4932 Constructs, checks, and returns an op of any unary type. C<type> is
4933 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4934 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4935 bits, the eight bits of C<op_private>, except that the bit with value 1
4936 is automatically set. C<first> supplies an optional op to be the direct
4937 child of the unary op; it is consumed by this function and become part
4938 of the constructed op tree.
4944 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4949 if (type == -OP_ENTEREVAL) {
4950 type = OP_ENTEREVAL;
4951 flags |= OPpEVAL_BYTES<<8;
4954 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4955 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4956 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4957 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4958 || type == OP_SASSIGN
4959 || type == OP_ENTERTRY
4960 || type == OP_CUSTOM
4961 || type == OP_NULL );
4964 first = newOP(OP_STUB, 0);
4965 if (PL_opargs[type] & OA_MARK)
4966 first = force_list(first, 1);
4968 NewOp(1101, unop, 1, UNOP);
4969 OpTYPE_set(unop, type);
4970 unop->op_first = first;
4971 unop->op_flags = (U8)(flags | OPf_KIDS);
4972 unop->op_private = (U8)(1 | (flags >> 8));
4974 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
4975 OpLASTSIB_set(first, (OP*)unop);
4977 unop = (UNOP*) CHECKOP(type, unop);
4981 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4985 =for apidoc newUNOP_AUX
4987 Similar to C<newUNOP>, but creates an C<UNOP_AUX> struct instead, with C<op_aux>
4988 initialised to C<aux>
4994 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux)
4999 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX
5000 || type == OP_CUSTOM);
5002 NewOp(1101, unop, 1, UNOP_AUX);
5003 unop->op_type = (OPCODE)type;
5004 unop->op_ppaddr = PL_ppaddr[type];
5005 unop->op_first = first;
5006 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0));
5007 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8));
5010 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */
5011 OpLASTSIB_set(first, (OP*)unop);
5013 unop = (UNOP_AUX*) CHECKOP(type, unop);
5015 return op_std_init((OP *) unop);
5019 =for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first
5021 Constructs, checks, and returns an op of method type with a method name
5022 evaluated at runtime. C<type> is the opcode. C<flags> gives the eight
5023 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically,
5024 and, shifted up eight bits, the eight bits of C<op_private>, except that
5025 the bit with value 1 is automatically set. C<dynamic_meth> supplies an
5026 op which evaluates method name; it is consumed by this function and
5027 become part of the constructed op tree.
5028 Supported optypes: C<OP_METHOD>.
5034 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) {
5038 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP
5039 || type == OP_CUSTOM);
5041 NewOp(1101, methop, 1, METHOP);
5043 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1);
5044 methop->op_flags = (U8)(flags | OPf_KIDS);
5045 methop->op_u.op_first = dynamic_meth;
5046 methop->op_private = (U8)(1 | (flags >> 8));
5048 if (!OpHAS_SIBLING(dynamic_meth))
5049 OpLASTSIB_set(dynamic_meth, (OP*)methop);
5053 methop->op_flags = (U8)(flags & ~OPf_KIDS);
5054 methop->op_u.op_meth_sv = const_meth;
5055 methop->op_private = (U8)(0 | (flags >> 8));
5056 methop->op_next = (OP*)methop;
5060 methop->op_rclass_targ = 0;
5062 methop->op_rclass_sv = NULL;
5065 OpTYPE_set(methop, type);
5066 return CHECKOP(type, methop);
5070 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) {
5071 PERL_ARGS_ASSERT_NEWMETHOP;
5072 return newMETHOP_internal(type, flags, dynamic_meth, NULL);
5076 =for apidoc Am|OP *|newMETHOP_named|I32 type|I32 flags|SV *const_meth
5078 Constructs, checks, and returns an op of method type with a constant
5079 method name. C<type> is the opcode. C<flags> gives the eight bits of
5080 C<op_flags>, and, shifted up eight bits, the eight bits of
5081 C<op_private>. C<const_meth> supplies a constant method name;
5082 it must be a shared COW string.
5083 Supported optypes: C<OP_METHOD_NAMED>.
5089 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) {
5090 PERL_ARGS_ASSERT_NEWMETHOP_NAMED;
5091 return newMETHOP_internal(type, flags, NULL, const_meth);
5095 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
5097 Constructs, checks, and returns an op of any binary type. C<type>
5098 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
5099 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
5100 the eight bits of C<op_private>, except that the bit with value 1 or
5101 2 is automatically set as required. C<first> and C<last> supply up to
5102 two ops to be the direct children of the binary op; they are consumed
5103 by this function and become part of the constructed op tree.
5109 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
5114 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
5115 || type == OP_NULL || type == OP_CUSTOM);
5117 NewOp(1101, binop, 1, BINOP);
5120 first = newOP(OP_NULL, 0);
5122 OpTYPE_set(binop, type);
5123 binop->op_first = first;
5124 binop->op_flags = (U8)(flags | OPf_KIDS);
5127 binop->op_private = (U8)(1 | (flags >> 8));
5130 binop->op_private = (U8)(2 | (flags >> 8));
5131 OpMORESIB_set(first, last);
5134 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */
5135 OpLASTSIB_set(last, (OP*)binop);
5137 binop->op_last = OpSIBLING(binop->op_first);
5139 OpLASTSIB_set(binop->op_last, (OP*)binop);
5141 binop = (BINOP*)CHECKOP(type, binop);
5142 if (binop->op_next || binop->op_type != (OPCODE)type)
5145 return fold_constants(op_integerize(op_std_init((OP *)binop)));
5148 static int uvcompare(const void *a, const void *b)
5149 __attribute__nonnull__(1)
5150 __attribute__nonnull__(2)
5151 __attribute__pure__;
5152 static int uvcompare(const void *a, const void *b)
5154 if (*((const UV *)a) < (*(const UV *)b))
5156 if (*((const UV *)a) > (*(const UV *)b))
5158 if (*((const UV *)a+1) < (*(const UV *)b+1))
5160 if (*((const UV *)a+1) > (*(const UV *)b+1))
5166 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
5168 SV * const tstr = ((SVOP*)expr)->op_sv;
5170 ((SVOP*)repl)->op_sv;
5173 const U8 *t = (U8*)SvPV_const(tstr, tlen);
5174 const U8 *r = (U8*)SvPV_const(rstr, rlen);
5180 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
5181 const I32 squash = o->op_private & OPpTRANS_SQUASH;
5182 I32 del = o->op_private & OPpTRANS_DELETE;
5185 PERL_ARGS_ASSERT_PMTRANS;
5187 PL_hints |= HINT_BLOCK_SCOPE;
5190 o->op_private |= OPpTRANS_FROM_UTF;
5193 o->op_private |= OPpTRANS_TO_UTF;
5195 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
5196 SV* const listsv = newSVpvs("# comment\n");
5198 const U8* tend = t + tlen;
5199 const U8* rend = r + rlen;
5215 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
5216 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
5219 const U32 flags = UTF8_ALLOW_DEFAULT;
5223 t = tsave = bytes_to_utf8(t, &len);
5226 if (!to_utf && rlen) {
5228 r = rsave = bytes_to_utf8(r, &len);
5232 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has
5233 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
5237 U8 tmpbuf[UTF8_MAXBYTES+1];
5240 Newx(cp, 2*tlen, UV);
5242 transv = newSVpvs("");
5244 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
5246 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
5248 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
5252 cp[2*i+1] = cp[2*i];
5256 qsort(cp, i, 2*sizeof(UV), uvcompare);
5257 for (j = 0; j < i; j++) {
5259 diff = val - nextmin;
5261 t = uvchr_to_utf8(tmpbuf,nextmin);
5262 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5264 U8 range_mark = ILLEGAL_UTF8_BYTE;
5265 t = uvchr_to_utf8(tmpbuf, val - 1);
5266 sv_catpvn(transv, (char *)&range_mark, 1);
5267 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5274 t = uvchr_to_utf8(tmpbuf,nextmin);
5275 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5277 U8 range_mark = ILLEGAL_UTF8_BYTE;
5278 sv_catpvn(transv, (char *)&range_mark, 1);
5280 t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
5281 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5282 t = (const U8*)SvPVX_const(transv);
5283 tlen = SvCUR(transv);
5287 else if (!rlen && !del) {
5288 r = t; rlen = tlen; rend = tend;
5291 if ((!rlen && !del) || t == r ||
5292 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
5294 o->op_private |= OPpTRANS_IDENTICAL;
5298 while (t < tend || tfirst <= tlast) {
5299 /* see if we need more "t" chars */
5300 if (tfirst > tlast) {
5301 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
5303 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
5305 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
5312 /* now see if we need more "r" chars */
5313 if (rfirst > rlast) {
5315 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
5317 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
5319 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
5328 rfirst = rlast = 0xffffffff;
5332 /* now see which range will peter out first, if either. */
5333 tdiff = tlast - tfirst;
5334 rdiff = rlast - rfirst;
5335 tcount += tdiff + 1;
5336 rcount += rdiff + 1;
5343 if (rfirst == 0xffffffff) {
5344 diff = tdiff; /* oops, pretend rdiff is infinite */
5346 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
5347 (long)tfirst, (long)tlast);
5349 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
5353 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
5354 (long)tfirst, (long)(tfirst + diff),
5357 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
5358 (long)tfirst, (long)rfirst);
5360 if (rfirst + diff > max)
5361 max = rfirst + diff;
5363 grows = (tfirst < rfirst &&
5364 UVCHR_SKIP(tfirst) < UVCHR_SKIP(rfirst + diff));
5376 else if (max > 0xff)
5381 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
5383 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
5384 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
5385 PAD_SETSV(cPADOPo->op_padix, swash);
5387 SvREADONLY_on(swash);
5389 cSVOPo->op_sv = swash;
5391 SvREFCNT_dec(listsv);
5392 SvREFCNT_dec(transv);
5394 if (!del && havefinal && rlen)
5395 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
5396 newSVuv((UV)final), 0);
5405 else if (rlast == 0xffffffff)
5411 tbl = (short*)PerlMemShared_calloc(
5412 (o->op_private & OPpTRANS_COMPLEMENT) &&
5413 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
5415 cPVOPo->op_pv = (char*)tbl;
5417 for (i = 0; i < (I32)tlen; i++)
5419 for (i = 0, j = 0; i < 256; i++) {
5421 if (j >= (I32)rlen) {
5430 if (UVCHR_IS_INVARIANT(i) && ! UVCHR_IS_INVARIANT(r[j]))
5440 o->op_private |= OPpTRANS_IDENTICAL;
5442 else if (j >= (I32)rlen)
5447 PerlMemShared_realloc(tbl,
5448 (0x101+rlen-j) * sizeof(short));
5449 cPVOPo->op_pv = (char*)tbl;
5451 tbl[0x100] = (short)(rlen - j);
5452 for (i=0; i < (I32)rlen - j; i++)
5453 tbl[0x101+i] = r[j+i];
5457 if (!rlen && !del) {
5460 o->op_private |= OPpTRANS_IDENTICAL;
5462 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
5463 o->op_private |= OPpTRANS_IDENTICAL;
5465 for (i = 0; i < 256; i++)
5467 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
5468 if (j >= (I32)rlen) {
5470 if (tbl[t[i]] == -1)
5476 if (tbl[t[i]] == -1) {
5477 if ( UVCHR_IS_INVARIANT(t[i])
5478 && ! UVCHR_IS_INVARIANT(r[j]))
5486 if(del && rlen == tlen) {
5487 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
5488 } else if(rlen > tlen && !complement) {
5489 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
5493 o->op_private |= OPpTRANS_GROWS;
5501 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
5503 Constructs, checks, and returns an op of any pattern matching type.
5504 C<type> is the opcode. C<flags> gives the eight bits of C<op_flags>
5505 and, shifted up eight bits, the eight bits of C<op_private>.
5511 Perl_newPMOP(pTHX_ I32 type, I32 flags)
5516 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP
5517 || type == OP_CUSTOM);
5519 NewOp(1101, pmop, 1, PMOP);
5520 OpTYPE_set(pmop, type);
5521 pmop->op_flags = (U8)flags;
5522 pmop->op_private = (U8)(0 | (flags >> 8));
5523 if (PL_opargs[type] & OA_RETSCALAR)
5526 if (PL_hints & HINT_RE_TAINT)
5527 pmop->op_pmflags |= PMf_RETAINT;
5528 #ifdef USE_LOCALE_CTYPE
5529 if (IN_LC_COMPILETIME(LC_CTYPE)) {
5530 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
5535 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
5537 if (PL_hints & HINT_RE_FLAGS) {
5538 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
5539 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
5541 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
5542 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
5543 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
5545 if (reflags && SvOK(reflags)) {
5546 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
5552 assert(SvPOK(PL_regex_pad[0]));
5553 if (SvCUR(PL_regex_pad[0])) {
5554 /* Pop off the "packed" IV from the end. */
5555 SV *const repointer_list = PL_regex_pad[0];
5556 const char *p = SvEND(repointer_list) - sizeof(IV);
5557 const IV offset = *((IV*)p);
5559 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
5561 SvEND_set(repointer_list, p);
5563 pmop->op_pmoffset = offset;
5564 /* This slot should be free, so assert this: */
5565 assert(PL_regex_pad[offset] == &PL_sv_undef);
5567 SV * const repointer = &PL_sv_undef;
5568 av_push(PL_regex_padav, repointer);
5569 pmop->op_pmoffset = av_tindex(PL_regex_padav);
5570 PL_regex_pad = AvARRAY(PL_regex_padav);
5574 return CHECKOP(type, pmop);
5582 /* Any pad names in scope are potentially lvalues. */
5583 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) {
5584 PADNAME *pn = PAD_COMPNAME_SV(i);
5585 if (!pn || !PadnameLEN(pn))
5587 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax))
5588 S_mark_padname_lvalue(aTHX_ pn);
5592 /* Given some sort of match op o, and an expression expr containing a
5593 * pattern, either compile expr into a regex and attach it to o (if it's
5594 * constant), or convert expr into a runtime regcomp op sequence (if it's
5597 * Flags currently has 2 bits of meaning:
5598 * 1: isreg indicates that the pattern is part of a regex construct, eg
5599 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
5600 * split "pattern", which aren't. In the former case, expr will be a list
5601 * if the pattern contains more than one term (eg /a$b/).
5602 * 2: The pattern is for a split.
5604 * When the pattern has been compiled within a new anon CV (for
5605 * qr/(?{...})/ ), then floor indicates the savestack level just before
5606 * the new sub was created
5610 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, UV flags, I32 floor)
5614 I32 repl_has_vars = 0;
5615 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
5616 bool is_compiletime;
5618 bool isreg = cBOOL(flags & 1);
5619 bool is_split = cBOOL(flags & 2);
5621 PERL_ARGS_ASSERT_PMRUNTIME;
5624 return pmtrans(o, expr, repl);
5627 /* find whether we have any runtime or code elements;
5628 * at the same time, temporarily set the op_next of each DO block;
5629 * then when we LINKLIST, this will cause the DO blocks to be excluded
5630 * from the op_next chain (and from having LINKLIST recursively
5631 * applied to them). We fix up the DOs specially later */
5635 if (expr->op_type == OP_LIST) {
5637 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) {
5638 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
5640 assert(!o->op_next);
5641 if (UNLIKELY(!OpHAS_SIBLING(o))) {
5642 assert(PL_parser && PL_parser->error_count);
5643 /* This can happen with qr/ (?{(^{})/. Just fake up
5644 the op we were expecting to see, to avoid crashing
5646 op_sibling_splice(expr, o, 0,
5647 newSVOP(OP_CONST, 0, &PL_sv_no));
5649 o->op_next = OpSIBLING(o);
5651 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
5655 else if (expr->op_type != OP_CONST)
5660 /* fix up DO blocks; treat each one as a separate little sub;
5661 * also, mark any arrays as LIST/REF */
5663 if (expr->op_type == OP_LIST) {
5665 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) {
5667 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) {
5668 assert( !(o->op_flags & OPf_WANT));
5669 /* push the array rather than its contents. The regex
5670 * engine will retrieve and join the elements later */
5671 o->op_flags |= (OPf_WANT_LIST | OPf_REF);
5675 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
5677 o->op_next = NULL; /* undo temporary hack from above */
5680 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
5681 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first);
5683 assert(leaveop->op_first->op_type == OP_ENTER);
5684 assert(OpHAS_SIBLING(leaveop->op_first));
5685 o->op_next = OpSIBLING(leaveop->op_first);
5687 assert(leaveop->op_flags & OPf_KIDS);
5688 assert(leaveop->op_last->op_next == (OP*)leaveop);
5689 leaveop->op_next = NULL; /* stop on last op */
5690 op_null((OP*)leaveop);
5694 OP *scope = cLISTOPo->op_first;
5695 assert(scope->op_type == OP_SCOPE);
5696 assert(scope->op_flags & OPf_KIDS);
5697 scope->op_next = NULL; /* stop on last op */
5700 /* have to peep the DOs individually as we've removed it from
5701 * the op_next chain */
5703 S_prune_chain_head(&(o->op_next));
5705 /* runtime finalizes as part of finalizing whole tree */
5709 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
5710 assert( !(expr->op_flags & OPf_WANT));
5711 /* push the array rather than its contents. The regex
5712 * engine will retrieve and join the elements later */
5713 expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
5716 PL_hints |= HINT_BLOCK_SCOPE;
5718 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
5720 if (is_compiletime) {
5721 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
5722 regexp_engine const *eng = current_re_engine();
5725 /* make engine handle split ' ' specially */
5726 pm->op_pmflags |= PMf_SPLIT;
5727 rx_flags |= RXf_SPLIT;
5730 if (!has_code || !eng->op_comp) {
5731 /* compile-time simple constant pattern */
5733 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
5734 /* whoops! we guessed that a qr// had a code block, but we
5735 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
5736 * that isn't required now. Note that we have to be pretty
5737 * confident that nothing used that CV's pad while the
5738 * regex was parsed, except maybe op targets for \Q etc.
5739 * If there were any op targets, though, they should have
5740 * been stolen by constant folding.
5744 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0);
5745 while (++i <= AvFILLp(PL_comppad)) {
5746 # ifdef USE_PAD_RESET
5747 /* under USE_PAD_RESET, pad swipe replaces a swiped
5748 * folded constant with a fresh padtmp */
5749 assert(!PL_curpad[i] || SvPADTMP(PL_curpad[i]));
5751 assert(!PL_curpad[i]);
5755 /* But we know that one op is using this CV's slab. */
5756 cv_forget_slab(PL_compcv);
5758 pm->op_pmflags &= ~PMf_HAS_CV;
5763 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5764 rx_flags, pm->op_pmflags)
5765 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5766 rx_flags, pm->op_pmflags)
5771 /* compile-time pattern that includes literal code blocks */
5772 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5775 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
5778 if (pm->op_pmflags & PMf_HAS_CV) {
5780 /* this QR op (and the anon sub we embed it in) is never
5781 * actually executed. It's just a placeholder where we can
5782 * squirrel away expr in op_code_list without the peephole
5783 * optimiser etc processing it for a second time */
5784 OP *qr = newPMOP(OP_QR, 0);
5785 ((PMOP*)qr)->op_code_list = expr;
5787 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
5788 SvREFCNT_inc_simple_void(PL_compcv);
5789 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
5790 ReANY(re)->qr_anoncv = cv;
5792 /* attach the anon CV to the pad so that
5793 * pad_fixup_inner_anons() can find it */
5794 (void)pad_add_anon(cv, o->op_type);
5795 SvREFCNT_inc_simple_void(cv);
5798 pm->op_code_list = expr;
5803 /* runtime pattern: build chain of regcomp etc ops */
5805 PADOFFSET cv_targ = 0;
5807 reglist = isreg && expr->op_type == OP_LIST;
5812 pm->op_code_list = expr;
5813 /* don't free op_code_list; its ops are embedded elsewhere too */
5814 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
5818 /* make engine handle split ' ' specially */
5819 pm->op_pmflags |= PMf_SPLIT;
5821 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
5822 * to allow its op_next to be pointed past the regcomp and
5823 * preceding stacking ops;
5824 * OP_REGCRESET is there to reset taint before executing the
5826 if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
5827 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
5829 if (pm->op_pmflags & PMf_HAS_CV) {
5830 /* we have a runtime qr with literal code. This means
5831 * that the qr// has been wrapped in a new CV, which
5832 * means that runtime consts, vars etc will have been compiled
5833 * against a new pad. So... we need to execute those ops
5834 * within the environment of the new CV. So wrap them in a call
5835 * to a new anon sub. i.e. for
5839 * we build an anon sub that looks like
5841 * sub { "a", $b, '(?{...})' }
5843 * and call it, passing the returned list to regcomp.
5844 * Or to put it another way, the list of ops that get executed
5848 * ------ -------------------
5849 * pushmark (for regcomp)
5850 * pushmark (for entersub)
5854 * regcreset regcreset
5856 * const("a") const("a")
5858 * const("(?{...})") const("(?{...})")
5863 SvREFCNT_inc_simple_void(PL_compcv);
5864 CvLVALUE_on(PL_compcv);
5865 /* these lines are just an unrolled newANONATTRSUB */
5866 expr = newSVOP(OP_ANONCODE, 0,
5867 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
5868 cv_targ = expr->op_targ;
5869 expr = newUNOP(OP_REFGEN, 0, expr);
5871 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), 1));
5874 rcop = alloc_LOGOP(OP_REGCOMP, scalar(expr), o);
5875 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
5876 | (reglist ? OPf_STACKED : 0);
5877 rcop->op_targ = cv_targ;
5879 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
5880 if (PL_hints & HINT_RE_EVAL)
5881 S_set_haseval(aTHX);
5883 /* establish postfix order */
5884 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
5886 rcop->op_next = expr;
5887 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
5890 rcop->op_next = LINKLIST(expr);
5891 expr->op_next = (OP*)rcop;
5894 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
5900 /* If we are looking at s//.../e with a single statement, get past
5901 the implicit do{}. */
5902 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
5903 && cUNOPx(curop)->op_first->op_type == OP_SCOPE
5904 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS)
5907 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
5908 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid))
5909 && !OpHAS_SIBLING(sib))
5912 if (curop->op_type == OP_CONST)
5914 else if (( (curop->op_type == OP_RV2SV ||
5915 curop->op_type == OP_RV2AV ||
5916 curop->op_type == OP_RV2HV ||
5917 curop->op_type == OP_RV2GV)
5918 && cUNOPx(curop)->op_first
5919 && cUNOPx(curop)->op_first->op_type == OP_GV )
5920 || curop->op_type == OP_PADSV
5921 || curop->op_type == OP_PADAV
5922 || curop->op_type == OP_PADHV
5923 || curop->op_type == OP_PADANY) {
5931 || !RX_PRELEN(PM_GETRE(pm))
5932 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
5934 pm->op_pmflags |= PMf_CONST; /* const for long enough */
5935 op_prepend_elem(o->op_type, scalar(repl), o);
5938 rcop = alloc_LOGOP(OP_SUBSTCONT, scalar(repl), o);
5939 rcop->op_private = 1;
5941 /* establish postfix order */
5942 rcop->op_next = LINKLIST(repl);
5943 repl->op_next = (OP*)rcop;
5945 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
5946 assert(!(pm->op_pmflags & PMf_ONCE));
5947 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
5956 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
5958 Constructs, checks, and returns an op of any type that involves an
5959 embedded SV. C<type> is the opcode. C<flags> gives the eight bits
5960 of C<op_flags>. C<sv> gives the SV to embed in the op; this function
5961 takes ownership of one reference to it.
5967 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
5972 PERL_ARGS_ASSERT_NEWSVOP;
5974 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
5975 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5976 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
5977 || type == OP_CUSTOM);
5979 NewOp(1101, svop, 1, SVOP);
5980 OpTYPE_set(svop, type);
5982 svop->op_next = (OP*)svop;
5983 svop->op_flags = (U8)flags;
5984 svop->op_private = (U8)(0 | (flags >> 8));
5985 if (PL_opargs[type] & OA_RETSCALAR)
5987 if (PL_opargs[type] & OA_TARGET)
5988 svop->op_targ = pad_alloc(type, SVs_PADTMP);
5989 return CHECKOP(type, svop);
5993 =for apidoc Am|OP *|newDEFSVOP|
5995 Constructs and returns an op to access C<$_>.
6001 Perl_newDEFSVOP(pTHX)
6003 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
6009 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
6011 Constructs, checks, and returns an op of any type that involves a
6012 reference to a pad element. C<type> is the opcode. C<flags> gives the
6013 eight bits of C<op_flags>. A pad slot is automatically allocated, and
6014 is populated with C<sv>; this function takes ownership of one reference
6017 This function only exists if Perl has been compiled to use ithreads.
6023 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
6028 PERL_ARGS_ASSERT_NEWPADOP;
6030 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
6031 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
6032 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
6033 || type == OP_CUSTOM);
6035 NewOp(1101, padop, 1, PADOP);
6036 OpTYPE_set(padop, type);
6038 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP);
6039 SvREFCNT_dec(PAD_SVl(padop->op_padix));
6040 PAD_SETSV(padop->op_padix, sv);
6042 padop->op_next = (OP*)padop;
6043 padop->op_flags = (U8)flags;
6044 if (PL_opargs[type] & OA_RETSCALAR)
6046 if (PL_opargs[type] & OA_TARGET)
6047 padop->op_targ = pad_alloc(type, SVs_PADTMP);
6048 return CHECKOP(type, padop);
6051 #endif /* USE_ITHREADS */
6054 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
6056 Constructs, checks, and returns an op of any type that involves an
6057 embedded reference to a GV. C<type> is the opcode. C<flags> gives the
6058 eight bits of C<op_flags>. C<gv> identifies the GV that the op should
6059 reference; calling this function does not transfer ownership of any
6066 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
6068 PERL_ARGS_ASSERT_NEWGVOP;
6071 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
6073 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
6078 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
6080 Constructs, checks, and returns an op of any type that involves an
6081 embedded C-level pointer (PV). C<type> is the opcode. C<flags> gives
6082 the eight bits of C<op_flags>. C<pv> supplies the C-level pointer, which
6083 must have been allocated using C<PerlMemShared_malloc>; the memory will
6084 be freed when the op is destroyed.
6090 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
6093 const bool utf8 = cBOOL(flags & SVf_UTF8);
6098 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
6099 || type == OP_RUNCV || type == OP_CUSTOM
6100 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
6102 NewOp(1101, pvop, 1, PVOP);
6103 OpTYPE_set(pvop, type);
6105 pvop->op_next = (OP*)pvop;
6106 pvop->op_flags = (U8)flags;
6107 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
6108 if (PL_opargs[type] & OA_RETSCALAR)
6110 if (PL_opargs[type] & OA_TARGET)
6111 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
6112 return CHECKOP(type, pvop);
6116 Perl_package(pTHX_ OP *o)
6118 SV *const sv = cSVOPo->op_sv;
6120 PERL_ARGS_ASSERT_PACKAGE;
6122 SAVEGENERICSV(PL_curstash);
6123 save_item(PL_curstname);
6125 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
6127 sv_setsv(PL_curstname, sv);
6129 PL_hints |= HINT_BLOCK_SCOPE;
6130 PL_parser->copline = NOLINE;
6136 Perl_package_version( pTHX_ OP *v )
6138 U32 savehints = PL_hints;
6139 PERL_ARGS_ASSERT_PACKAGE_VERSION;
6140 PL_hints &= ~HINT_STRICT_VARS;
6141 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
6142 PL_hints = savehints;
6147 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
6152 SV *use_version = NULL;
6154 PERL_ARGS_ASSERT_UTILIZE;
6156 if (idop->op_type != OP_CONST)
6157 Perl_croak(aTHX_ "Module name must be constant");
6162 SV * const vesv = ((SVOP*)version)->op_sv;
6164 if (!arg && !SvNIOKp(vesv)) {
6171 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
6172 Perl_croak(aTHX_ "Version number must be a constant number");
6174 /* Make copy of idop so we don't free it twice */
6175 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
6177 /* Fake up a method call to VERSION */
6178 meth = newSVpvs_share("VERSION");
6179 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
6180 op_append_elem(OP_LIST,
6181 op_prepend_elem(OP_LIST, pack, version),
6182 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
6186 /* Fake up an import/unimport */
6187 if (arg && arg->op_type == OP_STUB) {
6188 imop = arg; /* no import on explicit () */
6190 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
6191 imop = NULL; /* use 5.0; */
6193 use_version = ((SVOP*)idop)->op_sv;
6195 idop->op_private |= OPpCONST_NOVER;
6200 /* Make copy of idop so we don't free it twice */
6201 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
6203 /* Fake up a method call to import/unimport */
6205 ? newSVpvs_share("import") : newSVpvs_share("unimport");
6206 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
6207 op_append_elem(OP_LIST,
6208 op_prepend_elem(OP_LIST, pack, arg),
6209 newMETHOP_named(OP_METHOD_NAMED, 0, meth)
6213 /* Fake up the BEGIN {}, which does its thing immediately. */
6215 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
6218 op_append_elem(OP_LINESEQ,
6219 op_append_elem(OP_LINESEQ,
6220 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
6221 newSTATEOP(0, NULL, veop)),
6222 newSTATEOP(0, NULL, imop) ));
6226 * feature bundle that corresponds to the required version. */
6227 use_version = sv_2mortal(new_version(use_version));
6228 S_enable_feature_bundle(aTHX_ use_version);
6230 /* If a version >= 5.11.0 is requested, strictures are on by default! */
6231 if (vcmp(use_version,
6232 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
6233 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
6234 PL_hints |= HINT_STRICT_REFS;
6235 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
6236 PL_hints |= HINT_STRICT_SUBS;
6237 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
6238 PL_hints |= HINT_STRICT_VARS;
6240 /* otherwise they are off */
6242 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
6243 PL_hints &= ~HINT_STRICT_REFS;
6244 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
6245 PL_hints &= ~HINT_STRICT_SUBS;
6246 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
6247 PL_hints &= ~HINT_STRICT_VARS;
6251 /* The "did you use incorrect case?" warning used to be here.
6252 * The problem is that on case-insensitive filesystems one
6253 * might get false positives for "use" (and "require"):
6254 * "use Strict" or "require CARP" will work. This causes
6255 * portability problems for the script: in case-strict
6256 * filesystems the script will stop working.
6258 * The "incorrect case" warning checked whether "use Foo"
6259 * imported "Foo" to your namespace, but that is wrong, too:
6260 * there is no requirement nor promise in the language that
6261 * a Foo.pm should or would contain anything in package "Foo".
6263 * There is very little Configure-wise that can be done, either:
6264 * the case-sensitivity of the build filesystem of Perl does not
6265 * help in guessing the case-sensitivity of the runtime environment.
6268 PL_hints |= HINT_BLOCK_SCOPE;
6269 PL_parser->copline = NOLINE;
6270 COP_SEQMAX_INC; /* Purely for B::*'s benefit */
6274 =head1 Embedding Functions
6276 =for apidoc load_module
6278 Loads the module whose name is pointed to by the string part of C<name>.
6279 Note that the actual module name, not its filename, should be given.
6280 Eg, "Foo::Bar" instead of "Foo/Bar.pm". ver, if specified and not NULL,
6281 provides version semantics similar to C<use Foo::Bar VERSION>. The optional
6282 trailing arguments can be used to specify arguments to the module's C<import()>
6283 method, similar to C<use Foo::Bar VERSION LIST>; their precise handling depends
6284 on the flags. The flags argument is a bitwise-ORed collection of any of
6285 C<PERL_LOADMOD_DENY>, C<PERL_LOADMOD_NOIMPORT>, or C<PERL_LOADMOD_IMPORT_OPS>
6286 (or 0 for no flags).
6288 If C<PERL_LOADMOD_NOIMPORT> is set, the module is loaded as if with an empty
6289 import list, as in C<use Foo::Bar ()>; this is the only circumstance in which
6290 the trailing optional arguments may be omitted entirely. Otherwise, if
6291 C<PERL_LOADMOD_IMPORT_OPS> is set, the trailing arguments must consist of
6292 exactly one C<OP*>, containing the op tree that produces the relevant import
6293 arguments. Otherwise, the trailing arguments must all be C<SV*> values that
6294 will be used as import arguments; and the list must be terminated with C<(SV*)
6295 NULL>. If neither C<PERL_LOADMOD_NOIMPORT> nor C<PERL_LOADMOD_IMPORT_OPS> is
6296 set, the trailing C<NULL> pointer is needed even if no import arguments are
6297 desired. The reference count for each specified C<SV*> argument is
6298 decremented. In addition, the C<name> argument is modified.
6300 If C<PERL_LOADMOD_DENY> is set, the module is loaded as if with C<no> rather
6306 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
6310 PERL_ARGS_ASSERT_LOAD_MODULE;
6312 va_start(args, ver);
6313 vload_module(flags, name, ver, &args);
6317 #ifdef PERL_IMPLICIT_CONTEXT
6319 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
6323 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
6324 va_start(args, ver);
6325 vload_module(flags, name, ver, &args);
6331 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
6334 OP * const modname = newSVOP(OP_CONST, 0, name);
6336 PERL_ARGS_ASSERT_VLOAD_MODULE;
6338 modname->op_private |= OPpCONST_BARE;
6340 veop = newSVOP(OP_CONST, 0, ver);
6344 if (flags & PERL_LOADMOD_NOIMPORT) {
6345 imop = sawparens(newNULLLIST());
6347 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
6348 imop = va_arg(*args, OP*);
6353 sv = va_arg(*args, SV*);
6355 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
6356 sv = va_arg(*args, SV*);
6360 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
6361 * that it has a PL_parser to play with while doing that, and also
6362 * that it doesn't mess with any existing parser, by creating a tmp
6363 * new parser with lex_start(). This won't actually be used for much,
6364 * since pp_require() will create another parser for the real work.
6365 * The ENTER/LEAVE pair protect callers from any side effects of use. */
6368 SAVEVPTR(PL_curcop);
6369 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
6370 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
6371 veop, modname, imop);
6375 PERL_STATIC_INLINE OP *
6376 S_new_entersubop(pTHX_ GV *gv, OP *arg)
6378 return newUNOP(OP_ENTERSUB, OPf_STACKED,
6379 newLISTOP(OP_LIST, 0, arg,
6380 newUNOP(OP_RV2CV, 0,
6381 newGVOP(OP_GV, 0, gv))));
6385 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
6390 PERL_ARGS_ASSERT_DOFILE;
6392 if (!force_builtin && (gv = gv_override("do", 2))) {
6393 doop = S_new_entersubop(aTHX_ gv, term);
6396 doop = newUNOP(OP_DOFILE, 0, scalar(term));
6402 =head1 Optree construction
6404 =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval
6406 Constructs, checks, and returns an C<lslice> (list slice) op. C<flags>
6407 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
6408 be set automatically, and, shifted up eight bits, the eight bits of
6409 C<op_private>, except that the bit with value 1 or 2 is automatically
6410 set as required. C<listval> and C<subscript> supply the parameters of
6411 the slice; they are consumed by this function and become part of the
6412 constructed op tree.
6418 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
6420 return newBINOP(OP_LSLICE, flags,
6421 list(force_list(subscript, 1)),
6422 list(force_list(listval, 1)) );
6425 #define ASSIGN_LIST 1
6426 #define ASSIGN_REF 2
6429 S_assignment_type(pTHX_ const OP *o)
6438 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
6439 o = cUNOPo->op_first;
6441 flags = o->op_flags;
6443 if (type == OP_COND_EXPR) {
6444 OP * const sib = OpSIBLING(cLOGOPo->op_first);
6445 const I32 t = assignment_type(sib);
6446 const I32 f = assignment_type(OpSIBLING(sib));
6448 if (t == ASSIGN_LIST && f == ASSIGN_LIST)
6450 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST))
6451 yyerror("Assignment to both a list and a scalar");
6455 if (type == OP_SREFGEN)
6457 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first;
6458 type = kid->op_type;
6459 flags |= kid->op_flags;
6460 if (!(flags & OPf_PARENS)
6461 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV ||
6462 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV ))
6468 if (type == OP_LIST &&
6469 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
6470 o->op_private & OPpLVAL_INTRO)
6473 if (type == OP_LIST || flags & OPf_PARENS ||
6474 type == OP_RV2AV || type == OP_RV2HV ||
6475 type == OP_ASLICE || type == OP_HSLICE ||
6476 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN)
6479 if (type == OP_PADAV || type == OP_PADHV)
6482 if (type == OP_RV2SV)
6490 =for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right
6492 Constructs, checks, and returns an assignment op. C<left> and C<right>
6493 supply the parameters of the assignment; they are consumed by this
6494 function and become part of the constructed op tree.
6496 If C<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
6497 a suitable conditional optree is constructed. If C<optype> is the opcode
6498 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
6499 performs the binary operation and assigns the result to the left argument.
6500 Either way, if C<optype> is non-zero then C<flags> has no effect.
6502 If C<optype> is zero, then a plain scalar or list assignment is
6503 constructed. Which type of assignment it is is automatically determined.
6504 C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
6505 will be set automatically, and, shifted up eight bits, the eight bits
6506 of C<op_private>, except that the bit with value 1 or 2 is automatically
6513 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
6519 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
6520 right = scalar(right);
6521 return newLOGOP(optype, 0,
6522 op_lvalue(scalar(left), optype),
6523 newBINOP(OP_SASSIGN, OPpASSIGN_BACKWARDS<<8, right, right));
6526 return newBINOP(optype, OPf_STACKED,
6527 op_lvalue(scalar(left), optype), scalar(right));
6531 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) {
6532 static const char no_list_state[] = "Initialization of state variables"
6533 " in list context currently forbidden";
6536 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE)
6537 left->op_private &= ~ OPpSLICEWARNING;
6540 left = op_lvalue(left, OP_AASSIGN);
6541 curop = list(force_list(left, 1));
6542 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, 1)), curop);
6543 o->op_private = (U8)(0 | (flags >> 8));
6545 if (OP_TYPE_IS_OR_WAS(left, OP_LIST))
6547 OP* lop = ((LISTOP*)left)->op_first;
6549 if ((lop->op_type == OP_PADSV ||
6550 lop->op_type == OP_PADAV ||
6551 lop->op_type == OP_PADHV ||
6552 lop->op_type == OP_PADANY)
6553 && (lop->op_private & OPpPAD_STATE)
6555 yyerror(no_list_state);
6556 lop = OpSIBLING(lop);
6559 else if ( (left->op_private & OPpLVAL_INTRO)
6560 && (left->op_private & OPpPAD_STATE)
6561 && ( left->op_type == OP_PADSV
6562 || left->op_type == OP_PADAV
6563 || left->op_type == OP_PADHV
6564 || left->op_type == OP_PADANY)
6566 /* All single variable list context state assignments, hence
6576 yyerror(no_list_state);
6579 /* optimise @a = split(...) into:
6580 * @{expr}: split(..., @{expr}) (where @a is not flattened)
6581 * @a, my @a, local @a: split(...) (where @a is attached to
6582 * the split op itself)
6586 && right->op_type == OP_SPLIT
6587 /* don't do twice, e.g. @b = (@a = split) */
6588 && !(right->op_private & OPpSPLIT_ASSIGN))
6592 if ( ( left->op_type == OP_RV2AV
6593 && (gvop=((UNOP*)left)->op_first)->op_type==OP_GV)
6594 || left->op_type == OP_PADAV)
6596 /* @pkg or @lex or local @pkg' or 'my @lex' */
6600 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff
6601 = cPADOPx(gvop)->op_padix;
6602 cPADOPx(gvop)->op_padix = 0; /* steal it */
6604 ((PMOP*)right)->op_pmreplrootu.op_pmtargetgv
6605 = MUTABLE_GV(cSVOPx(gvop)->op_sv);
6606 cSVOPx(gvop)->op_sv = NULL; /* steal it */
6608 right->op_private |=
6609 left->op_private & OPpOUR_INTRO;
6612 ((PMOP*)right)->op_pmreplrootu.op_pmtargetoff = left->op_targ;
6613 left->op_targ = 0; /* steal it */
6614 right->op_private |= OPpSPLIT_LEX;
6616 right->op_private |= left->op_private & OPpLVAL_INTRO;
6619 tmpop = cUNOPo->op_first; /* to list (nulled) */
6620 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
6621 assert(OpSIBLING(tmpop) == right);
6622 assert(!OpHAS_SIBLING(right));
6623 /* detach the split subtreee from the o tree,
6624 * then free the residual o tree */
6625 op_sibling_splice(cUNOPo->op_first, tmpop, 1, NULL);
6626 op_free(o); /* blow off assign */
6627 right->op_private |= OPpSPLIT_ASSIGN;
6628 right->op_flags &= ~OPf_WANT;
6629 /* "I don't know and I don't care." */
6632 else if (left->op_type == OP_RV2AV) {
6635 OP *pushop = cUNOPx(cBINOPo->op_last)->op_first;
6636 assert(OpSIBLING(pushop) == left);
6637 /* Detach the array ... */
6638 op_sibling_splice(cBINOPo->op_last, pushop, 1, NULL);
6639 /* ... and attach it to the split. */
6640 op_sibling_splice(right, cLISTOPx(right)->op_last,
6642 right->op_flags |= OPf_STACKED;
6643 /* Detach split and expunge aassign as above. */
6646 else if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
6647 ((LISTOP*)right)->op_last->op_type == OP_CONST)
6649 /* convert split(...,0) to split(..., PL_modcount+1) */
6651 &((SVOP*)((LISTOP*)right)->op_last)->op_sv;
6652 SV * const sv = *svp;
6653 if (SvIOK(sv) && SvIVX(sv) == 0)
6655 if (right->op_private & OPpSPLIT_IMPLIM) {
6656 /* our own SV, created in ck_split */
6658 sv_setiv(sv, PL_modcount+1);
6661 /* SV may belong to someone else */
6663 *svp = newSViv(PL_modcount+1);
6670 if (assign_type == ASSIGN_REF)
6671 return newBINOP(OP_REFASSIGN, flags, scalar(right), left);
6673 right = newOP(OP_UNDEF, 0);
6674 if (right->op_type == OP_READLINE) {
6675 right->op_flags |= OPf_STACKED;
6676 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
6680 o = newBINOP(OP_SASSIGN, flags,
6681 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
6687 =for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o
6689 Constructs a state op (COP). The state op is normally a C<nextstate> op,
6690 but will be a C<dbstate> op if debugging is enabled for currently-compiled
6691 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>).
6692 If C<label> is non-null, it supplies the name of a label to attach to
6693 the state op; this function takes ownership of the memory pointed at by
6694 C<label>, and will free it. C<flags> gives the eight bits of C<op_flags>
6697 If C<o> is null, the state op is returned. Otherwise the state op is
6698 combined with C<o> into a C<lineseq> list op, which is returned. C<o>
6699 is consumed by this function and becomes part of the returned op tree.
6705 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
6708 const U32 seq = intro_my();
6709 const U32 utf8 = flags & SVf_UTF8;
6712 PL_parser->parsed_sub = 0;
6716 NewOp(1101, cop, 1, COP);
6717 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
6718 OpTYPE_set(cop, OP_DBSTATE);
6721 OpTYPE_set(cop, OP_NEXTSTATE);
6723 cop->op_flags = (U8)flags;
6724 CopHINTS_set(cop, PL_hints);
6726 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH;
6728 cop->op_next = (OP*)cop;
6731 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
6732 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
6734 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
6736 PL_hints |= HINT_BLOCK_SCOPE;
6737 /* It seems that we need to defer freeing this pointer, as other parts
6738 of the grammar end up wanting to copy it after this op has been
6743 if (PL_parser->preambling != NOLINE) {
6744 CopLINE_set(cop, PL_parser->preambling);
6745 PL_parser->copline = NOLINE;
6747 else if (PL_parser->copline == NOLINE)
6748 CopLINE_set(cop, CopLINE(PL_curcop));
6750 CopLINE_set(cop, PL_parser->copline);
6751 PL_parser->copline = NOLINE;
6754 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
6756 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
6758 CopSTASH_set(cop, PL_curstash);
6760 if (cop->op_type == OP_DBSTATE) {
6761 /* this line can have a breakpoint - store the cop in IV */
6762 AV *av = CopFILEAVx(PL_curcop);
6764 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
6765 if (svp && *svp != &PL_sv_undef ) {
6766 (void)SvIOK_on(*svp);
6767 SvIV_set(*svp, PTR2IV(cop));
6772 if (flags & OPf_SPECIAL)
6774 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
6778 =for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other
6780 Constructs, checks, and returns a logical (flow control) op. C<type>
6781 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
6782 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
6783 the eight bits of C<op_private>, except that the bit with value 1 is
6784 automatically set. C<first> supplies the expression controlling the
6785 flow, and C<other> supplies the side (alternate) chain of ops; they are
6786 consumed by this function and become part of the constructed op tree.
6792 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
6794 PERL_ARGS_ASSERT_NEWLOGOP;
6796 return new_logop(type, flags, &first, &other);
6800 S_search_const(pTHX_ OP *o)
6802 PERL_ARGS_ASSERT_SEARCH_CONST;
6804 switch (o->op_type) {
6808 if (o->op_flags & OPf_KIDS)
6809 return search_const(cUNOPo->op_first);
6816 if (!(o->op_flags & OPf_KIDS))
6818 kid = cLISTOPo->op_first;
6820 switch (kid->op_type) {
6824 kid = OpSIBLING(kid);
6827 if (kid != cLISTOPo->op_last)
6833 kid = cLISTOPo->op_last;
6835 return search_const(kid);
6843 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
6851 int prepend_not = 0;
6853 PERL_ARGS_ASSERT_NEW_LOGOP;
6858 /* [perl #59802]: Warn about things like "return $a or $b", which
6859 is parsed as "(return $a) or $b" rather than "return ($a or
6860 $b)". NB: This also applies to xor, which is why we do it
6863 switch (first->op_type) {
6867 /* XXX: Perhaps we should emit a stronger warning for these.
6868 Even with the high-precedence operator they don't seem to do
6871 But until we do, fall through here.
6877 /* XXX: Currently we allow people to "shoot themselves in the
6878 foot" by explicitly writing "(return $a) or $b".
6880 Warn unless we are looking at the result from folding or if
6881 the programmer explicitly grouped the operators like this.
6882 The former can occur with e.g.
6884 use constant FEATURE => ( $] >= ... );
6885 sub { not FEATURE and return or do_stuff(); }
6887 if (!first->op_folded && !(first->op_flags & OPf_PARENS))
6888 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
6889 "Possible precedence issue with control flow operator");
6890 /* XXX: Should we optimze this to "return $a;" (i.e. remove
6896 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
6897 return newBINOP(type, flags, scalar(first), scalar(other));
6899 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP
6900 || type == OP_CUSTOM);
6902 scalarboolean(first);
6904 /* search for a constant op that could let us fold the test */
6905 if ((cstop = search_const(first))) {
6906 if (cstop->op_private & OPpCONST_STRICT)
6907 no_bareword_allowed(cstop);
6908 else if ((cstop->op_private & OPpCONST_BARE))
6909 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
6910 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) ||
6911 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
6912 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
6913 /* Elide the (constant) lhs, since it can't affect the outcome */
6915 if (other->op_type == OP_CONST)
6916 other->op_private |= OPpCONST_SHORTCIRCUIT;
6918 if (other->op_type == OP_LEAVE)
6919 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
6920 else if (other->op_type == OP_MATCH
6921 || other->op_type == OP_SUBST
6922 || other->op_type == OP_TRANSR
6923 || other->op_type == OP_TRANS)
6924 /* Mark the op as being unbindable with =~ */
6925 other->op_flags |= OPf_SPECIAL;
6927 other->op_folded = 1;
6931 /* Elide the rhs, since the outcome is entirely determined by
6932 * the (constant) lhs */
6934 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
6935 const OP *o2 = other;
6936 if ( ! (o2->op_type == OP_LIST
6937 && (( o2 = cUNOPx(o2)->op_first))
6938 && o2->op_type == OP_PUSHMARK
6939 && (( o2 = OpSIBLING(o2))) )
6942 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
6943 || o2->op_type == OP_PADHV)
6944 && o2->op_private & OPpLVAL_INTRO
6945 && !(o2->op_private & OPpPAD_STATE))
6947 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
6948 "Deprecated use of my() in false conditional");
6952 if (cstop->op_type == OP_CONST)
6953 cstop->op_private |= OPpCONST_SHORTCIRCUIT;
6958 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
6959 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
6961 const OP * const k1 = ((UNOP*)first)->op_first;
6962 const OP * const k2 = OpSIBLING(k1);
6964 switch (first->op_type)
6967 if (k2 && k2->op_type == OP_READLINE
6968 && (k2->op_flags & OPf_STACKED)
6969 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6971 warnop = k2->op_type;
6976 if (k1->op_type == OP_READDIR
6977 || k1->op_type == OP_GLOB
6978 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6979 || k1->op_type == OP_EACH
6980 || k1->op_type == OP_AEACH)
6982 warnop = ((k1->op_type == OP_NULL)
6983 ? (OPCODE)k1->op_targ : k1->op_type);
6988 const line_t oldline = CopLINE(PL_curcop);
6989 /* This ensures that warnings are reported at the first line
6990 of the construction, not the last. */
6991 CopLINE_set(PL_curcop, PL_parser->copline);
6992 Perl_warner(aTHX_ packWARN(WARN_MISC),
6993 "Value of %s%s can be \"0\"; test with defined()",
6995 ((warnop == OP_READLINE || warnop == OP_GLOB)
6996 ? " construct" : "() operator"));
6997 CopLINE_set(PL_curcop, oldline);
7001 /* optimize AND and OR ops that have NOTs as children */
7002 if (first->op_type == OP_NOT
7003 && (first->op_flags & OPf_KIDS)
7004 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
7005 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
7007 if (type == OP_AND || type == OP_OR) {
7013 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
7015 prepend_not = 1; /* prepend a NOT op later */
7020 logop = alloc_LOGOP(type, first, LINKLIST(other));
7021 logop->op_flags |= (U8)flags;
7022 logop->op_private = (U8)(1 | (flags >> 8));
7024 /* establish postfix order */
7025 logop->op_next = LINKLIST(first);
7026 first->op_next = (OP*)logop;
7027 assert(!OpHAS_SIBLING(first));
7028 op_sibling_splice((OP*)logop, first, 0, other);
7030 CHECKOP(type,logop);
7032 o = newUNOP(prepend_not ? OP_NOT : OP_NULL,
7033 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0,
7041 =for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop
7043 Constructs, checks, and returns a conditional-expression (C<cond_expr>)
7044 op. C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
7045 will be set automatically, and, shifted up eight bits, the eight bits of
7046 C<op_private>, except that the bit with value 1 is automatically set.
7047 C<first> supplies the expression selecting between the two branches,
7048 and C<trueop> and C<falseop> supply the branches; they are consumed by
7049 this function and become part of the constructed op tree.
7055 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
7063 PERL_ARGS_ASSERT_NEWCONDOP;
7066 return newLOGOP(OP_AND, 0, first, trueop);
7068 return newLOGOP(OP_OR, 0, first, falseop);
7070 scalarboolean(first);
7071 if ((cstop = search_const(first))) {
7072 /* Left or right arm of the conditional? */
7073 const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
7074 OP *live = left ? trueop : falseop;
7075 OP *const dead = left ? falseop : trueop;
7076 if (cstop->op_private & OPpCONST_BARE &&
7077 cstop->op_private & OPpCONST_STRICT) {
7078 no_bareword_allowed(cstop);
7082 if (live->op_type == OP_LEAVE)
7083 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
7084 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
7085 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
7086 /* Mark the op as being unbindable with =~ */
7087 live->op_flags |= OPf_SPECIAL;
7088 live->op_folded = 1;
7091 logop = alloc_LOGOP(OP_COND_EXPR, first, LINKLIST(trueop));
7092 logop->op_flags |= (U8)flags;
7093 logop->op_private = (U8)(1 | (flags >> 8));
7094 logop->op_next = LINKLIST(falseop);
7096 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
7099 /* establish postfix order */
7100 start = LINKLIST(first);
7101 first->op_next = (OP*)logop;
7103 /* make first, trueop, falseop siblings */
7104 op_sibling_splice((OP*)logop, first, 0, trueop);
7105 op_sibling_splice((OP*)logop, trueop, 0, falseop);
7107 o = newUNOP(OP_NULL, 0, (OP*)logop);
7109 trueop->op_next = falseop->op_next = o;
7116 =for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right
7118 Constructs and returns a C<range> op, with subordinate C<flip> and
7119 C<flop> ops. C<flags> gives the eight bits of C<op_flags> for the
7120 C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
7121 for both the C<flip> and C<range> ops, except that the bit with value
7122 1 is automatically set. C<left> and C<right> supply the expressions
7123 controlling the endpoints of the range; they are consumed by this function
7124 and become part of the constructed op tree.
7130 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
7138 PERL_ARGS_ASSERT_NEWRANGE;
7140 range = alloc_LOGOP(OP_RANGE, left, LINKLIST(right));
7141 range->op_flags = OPf_KIDS;
7142 leftstart = LINKLIST(left);
7143 range->op_private = (U8)(1 | (flags >> 8));
7145 /* make left and right siblings */
7146 op_sibling_splice((OP*)range, left, 0, right);
7148 range->op_next = (OP*)range;
7149 flip = newUNOP(OP_FLIP, flags, (OP*)range);
7150 flop = newUNOP(OP_FLOP, 0, flip);
7151 o = newUNOP(OP_NULL, 0, flop);
7153 range->op_next = leftstart;
7155 left->op_next = flip;
7156 right->op_next = flop;
7159 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);
7160 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
7162 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);;
7163 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
7164 SvPADTMP_on(PAD_SV(flip->op_targ));
7166 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
7167 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
7169 /* check barewords before they might be optimized aways */
7170 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
7171 no_bareword_allowed(left);
7172 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
7173 no_bareword_allowed(right);
7176 if (!flip->op_private || !flop->op_private)
7177 LINKLIST(o); /* blow off optimizer unless constant */
7183 =for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block
7185 Constructs, checks, and returns an op tree expressing a loop. This is
7186 only a loop in the control flow through the op tree; it does not have
7187 the heavyweight loop structure that allows exiting the loop by C<last>
7188 and suchlike. C<flags> gives the eight bits of C<op_flags> for the
7189 top-level op, except that some bits will be set automatically as required.
7190 C<expr> supplies the expression controlling loop iteration, and C<block>
7191 supplies the body of the loop; they are consumed by this function and
7192 become part of the constructed op tree. C<debuggable> is currently
7193 unused and should always be 1.
7199 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
7203 const bool once = block && block->op_flags & OPf_SPECIAL &&
7204 block->op_type == OP_NULL;
7206 PERL_UNUSED_ARG(debuggable);
7210 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
7211 || ( expr->op_type == OP_NOT
7212 && cUNOPx(expr)->op_first->op_type == OP_CONST
7213 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first))
7216 /* Return the block now, so that S_new_logop does not try to
7218 return block; /* do {} while 0 does once */
7219 if (expr->op_type == OP_READLINE
7220 || expr->op_type == OP_READDIR
7221 || expr->op_type == OP_GLOB
7222 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
7223 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
7224 expr = newUNOP(OP_DEFINED, 0,
7225 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
7226 } else if (expr->op_flags & OPf_KIDS) {
7227 const OP * const k1 = ((UNOP*)expr)->op_first;
7228 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL;
7229 switch (expr->op_type) {
7231 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
7232 && (k2->op_flags & OPf_STACKED)
7233 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
7234 expr = newUNOP(OP_DEFINED, 0, expr);
7238 if (k1 && (k1->op_type == OP_READDIR
7239 || k1->op_type == OP_GLOB
7240 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
7241 || k1->op_type == OP_EACH
7242 || k1->op_type == OP_AEACH))
7243 expr = newUNOP(OP_DEFINED, 0, expr);
7249 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
7250 * op, in listop. This is wrong. [perl #27024] */
7252 block = newOP(OP_NULL, 0);
7253 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
7254 o = new_logop(OP_AND, 0, &expr, &listop);
7261 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
7263 if (once && o != listop)
7265 assert(cUNOPo->op_first->op_type == OP_AND
7266 || cUNOPo->op_first->op_type == OP_OR);
7267 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
7271 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
7273 o->op_flags |= flags;
7275 o->op_flags |= OPf_SPECIAL; /* suppress cx_popblock() curpm restoration*/
7280 =for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my
7282 Constructs, checks, and returns an op tree expressing a C<while> loop.
7283 This is a heavyweight loop, with structure that allows exiting the loop
7284 by C<last> and suchlike.
7286 C<loop> is an optional preconstructed C<enterloop> op to use in the
7287 loop; if it is null then a suitable op will be constructed automatically.
7288 C<expr> supplies the loop's controlling expression. C<block> supplies the
7289 main body of the loop, and C<cont> optionally supplies a C<continue> block
7290 that operates as a second half of the body. All of these optree inputs
7291 are consumed by this function and become part of the constructed op tree.
7293 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
7294 op and, shifted up eight bits, the eight bits of C<op_private> for
7295 the C<leaveloop> op, except that (in both cases) some bits will be set
7296 automatically. C<debuggable> is currently unused and should always be 1.
7297 C<has_my> can be supplied as true to force the
7298 loop body to be enclosed in its own scope.
7304 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
7305 OP *expr, OP *block, OP *cont, I32 has_my)
7314 PERL_UNUSED_ARG(debuggable);
7317 if (expr->op_type == OP_READLINE
7318 || expr->op_type == OP_READDIR
7319 || expr->op_type == OP_GLOB
7320 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
7321 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
7322 expr = newUNOP(OP_DEFINED, 0,
7323 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
7324 } else if (expr->op_flags & OPf_KIDS) {
7325 const OP * const k1 = ((UNOP*)expr)->op_first;
7326 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL;
7327 switch (expr->op_type) {
7329 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
7330 && (k2->op_flags & OPf_STACKED)
7331 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
7332 expr = newUNOP(OP_DEFINED, 0, expr);
7336 if (k1 && (k1->op_type == OP_READDIR
7337 || k1->op_type == OP_GLOB
7338 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
7339 || k1->op_type == OP_EACH
7340 || k1->op_type == OP_AEACH))
7341 expr = newUNOP(OP_DEFINED, 0, expr);
7348 block = newOP(OP_NULL, 0);
7349 else if (cont || has_my) {
7350 block = op_scope(block);
7354 next = LINKLIST(cont);
7357 OP * const unstack = newOP(OP_UNSTACK, 0);
7360 cont = op_append_elem(OP_LINESEQ, cont, unstack);
7364 listop = op_append_list(OP_LINESEQ, block, cont);
7366 redo = LINKLIST(listop);
7370 o = new_logop(OP_AND, 0, &expr, &listop);
7371 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
7373 return expr; /* listop already freed by new_logop */
7376 ((LISTOP*)listop)->op_last->op_next =
7377 (o == listop ? redo : LINKLIST(o));
7383 NewOp(1101,loop,1,LOOP);
7384 OpTYPE_set(loop, OP_ENTERLOOP);
7385 loop->op_private = 0;
7386 loop->op_next = (OP*)loop;
7389 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
7391 loop->op_redoop = redo;
7392 loop->op_lastop = o;
7393 o->op_private |= loopflags;
7396 loop->op_nextop = next;
7398 loop->op_nextop = o;
7400 o->op_flags |= flags;
7401 o->op_private |= (flags >> 8);
7406 =for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont
7408 Constructs, checks, and returns an op tree expressing a C<foreach>
7409 loop (iteration through a list of values). This is a heavyweight loop,
7410 with structure that allows exiting the loop by C<last> and suchlike.
7412 C<sv> optionally supplies the variable that will be aliased to each
7413 item in turn; if null, it defaults to C<$_>.
7414 C<expr> supplies the list of values to iterate over. C<block> supplies
7415 the main body of the loop, and C<cont> optionally supplies a C<continue>
7416 block that operates as a second half of the body. All of these optree
7417 inputs are consumed by this function and become part of the constructed
7420 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
7421 op and, shifted up eight bits, the eight bits of C<op_private> for
7422 the C<leaveloop> op, except that (in both cases) some bits will be set
7429 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
7434 PADOFFSET padoff = 0;
7438 PERL_ARGS_ASSERT_NEWFOROP;
7441 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
7442 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
7443 OpTYPE_set(sv, OP_RV2GV);
7445 /* The op_type check is needed to prevent a possible segfault
7446 * if the loop variable is undeclared and 'strict vars' is in
7447 * effect. This is illegal but is nonetheless parsed, so we
7448 * may reach this point with an OP_CONST where we're expecting
7451 if (cUNOPx(sv)->op_first->op_type == OP_GV
7452 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
7453 iterpflags |= OPpITER_DEF;
7455 else if (sv->op_type == OP_PADSV) { /* private variable */
7456 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
7457 padoff = sv->op_targ;
7461 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX);
7463 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN)
7466 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
7468 PADNAME * const pn = PAD_COMPNAME(padoff);
7469 const char * const name = PadnamePV(pn);
7471 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_')
7472 iterpflags |= OPpITER_DEF;
7476 sv = newGVOP(OP_GV, 0, PL_defgv);
7477 iterpflags |= OPpITER_DEF;
7480 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
7481 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), 1), OP_GREPSTART);
7482 iterflags |= OPf_STACKED;
7484 else if (expr->op_type == OP_NULL &&
7485 (expr->op_flags & OPf_KIDS) &&
7486 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
7488 /* Basically turn for($x..$y) into the same as for($x,$y), but we
7489 * set the STACKED flag to indicate that these values are to be
7490 * treated as min/max values by 'pp_enteriter'.
7492 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
7493 LOGOP* const range = (LOGOP*) flip->op_first;
7494 OP* const left = range->op_first;
7495 OP* const right = OpSIBLING(left);
7498 range->op_flags &= ~OPf_KIDS;
7499 /* detach range's children */
7500 op_sibling_splice((OP*)range, NULL, -1, NULL);
7502 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
7503 listop->op_first->op_next = range->op_next;
7504 left->op_next = range->op_other;
7505 right->op_next = (OP*)listop;
7506 listop->op_next = listop->op_first;
7509 expr = (OP*)(listop);
7511 iterflags |= OPf_STACKED;
7514 expr = op_lvalue(force_list(expr, 1), OP_GREPSTART);
7517 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags,
7518 op_append_elem(OP_LIST, list(expr),
7520 assert(!loop->op_next);
7521 /* for my $x () sets OPpLVAL_INTRO;
7522 * for our $x () sets OPpOUR_INTRO */
7523 loop->op_private = (U8)iterpflags;
7524 if (loop->op_slabbed
7525 && DIFF(loop, OpSLOT(loop)->opslot_next)
7526 < SIZE_TO_PSIZE(sizeof(LOOP)))
7529 NewOp(1234,tmp,1,LOOP);
7530 Copy(loop,tmp,1,LISTOP);
7531 #ifdef PERL_OP_PARENT
7532 assert(loop->op_last->op_sibparent == (OP*)loop);
7533 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */
7535 S_op_destroy(aTHX_ (OP*)loop);
7538 else if (!loop->op_slabbed)
7540 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
7541 #ifdef PERL_OP_PARENT
7542 OpLASTSIB_set(loop->op_last, (OP*)loop);
7545 loop->op_targ = padoff;
7546 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0);
7551 =for apidoc Am|OP *|newLOOPEX|I32 type|OP *label
7553 Constructs, checks, and returns a loop-exiting op (such as C<goto>
7554 or C<last>). C<type> is the opcode. C<label> supplies the parameter
7555 determining the target of the op; it is consumed by this function and
7556 becomes part of the constructed op tree.
7562 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
7566 PERL_ARGS_ASSERT_NEWLOOPEX;
7568 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
7569 || type == OP_CUSTOM);
7571 if (type != OP_GOTO) {
7572 /* "last()" means "last" */
7573 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) {
7574 o = newOP(type, OPf_SPECIAL);
7578 /* Check whether it's going to be a goto &function */
7579 if (label->op_type == OP_ENTERSUB
7580 && !(label->op_flags & OPf_STACKED))
7581 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
7584 /* Check for a constant argument */
7585 if (label->op_type == OP_CONST) {
7586 SV * const sv = ((SVOP *)label)->op_sv;
7588 const char *s = SvPV_const(sv,l);
7589 if (l == strlen(s)) {
7591 SvUTF8(((SVOP*)label)->op_sv),
7593 SvPV_nolen_const(((SVOP*)label)->op_sv)));
7597 /* If we have already created an op, we do not need the label. */
7600 else o = newUNOP(type, OPf_STACKED, label);
7602 PL_hints |= HINT_BLOCK_SCOPE;
7606 /* if the condition is a literal array or hash
7607 (or @{ ... } etc), make a reference to it.
7610 S_ref_array_or_hash(pTHX_ OP *cond)
7613 && (cond->op_type == OP_RV2AV
7614 || cond->op_type == OP_PADAV
7615 || cond->op_type == OP_RV2HV
7616 || cond->op_type == OP_PADHV))
7618 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
7621 && (cond->op_type == OP_ASLICE
7622 || cond->op_type == OP_KVASLICE
7623 || cond->op_type == OP_HSLICE
7624 || cond->op_type == OP_KVHSLICE)) {
7626 /* anonlist now needs a list from this op, was previously used in
7628 cond->op_flags &= ~(OPf_WANT_SCALAR | OPf_REF);
7629 cond->op_flags |= OPf_WANT_LIST;
7631 return newANONLIST(op_lvalue(cond, OP_ANONLIST));
7638 /* These construct the optree fragments representing given()
7641 entergiven and enterwhen are LOGOPs; the op_other pointer
7642 points up to the associated leave op. We need this so we
7643 can put it in the context and make break/continue work.
7644 (Also, of course, pp_enterwhen will jump straight to
7645 op_other if the match fails.)
7649 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
7650 I32 enter_opcode, I32 leave_opcode,
7651 PADOFFSET entertarg)
7657 PERL_ARGS_ASSERT_NEWGIVWHENOP;
7658 PERL_UNUSED_ARG(entertarg); /* used to indicate targ of lexical $_ */
7660 enterop = alloc_LOGOP(enter_opcode, block, NULL);
7661 enterop->op_targ = 0;
7662 enterop->op_private = 0;
7664 o = newUNOP(leave_opcode, 0, (OP *) enterop);
7667 /* prepend cond if we have one */
7668 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond));
7670 o->op_next = LINKLIST(cond);
7671 cond->op_next = (OP *) enterop;
7674 /* This is a default {} block */
7675 enterop->op_flags |= OPf_SPECIAL;
7676 o ->op_flags |= OPf_SPECIAL;
7678 o->op_next = (OP *) enterop;
7681 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
7682 entergiven and enterwhen both
7685 enterop->op_next = LINKLIST(block);
7686 block->op_next = enterop->op_other = o;
7691 /* Does this look like a boolean operation? For these purposes
7692 a boolean operation is:
7693 - a subroutine call [*]
7694 - a logical connective
7695 - a comparison operator
7696 - a filetest operator, with the exception of -s -M -A -C
7697 - defined(), exists() or eof()
7698 - /$re/ or $foo =~ /$re/
7700 [*] possibly surprising
7703 S_looks_like_bool(pTHX_ const OP *o)
7705 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
7707 switch(o->op_type) {
7710 return looks_like_bool(cLOGOPo->op_first);
7714 OP* sibl = OpSIBLING(cLOGOPo->op_first);
7717 looks_like_bool(cLOGOPo->op_first)
7718 && looks_like_bool(sibl));
7724 o->op_flags & OPf_KIDS
7725 && looks_like_bool(cUNOPo->op_first));
7729 case OP_NOT: case OP_XOR:
7731 case OP_EQ: case OP_NE: case OP_LT:
7732 case OP_GT: case OP_LE: case OP_GE:
7734 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
7735 case OP_I_GT: case OP_I_LE: case OP_I_GE:
7737 case OP_SEQ: case OP_SNE: case OP_SLT:
7738 case OP_SGT: case OP_SLE: case OP_SGE:
7742 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
7743 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
7744 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
7745 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
7746 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
7747 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
7748 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
7749 case OP_FTTEXT: case OP_FTBINARY:
7751 case OP_DEFINED: case OP_EXISTS:
7752 case OP_MATCH: case OP_EOF:
7759 /* Detect comparisons that have been optimized away */
7760 if (cSVOPo->op_sv == &PL_sv_yes
7761 || cSVOPo->op_sv == &PL_sv_no)
7774 =for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off
7776 Constructs, checks, and returns an op tree expressing a C<given> block.
7777 C<cond> supplies the expression that will be locally assigned to a lexical
7778 variable, and C<block> supplies the body of the C<given> construct; they
7779 are consumed by this function and become part of the constructed op tree.
7780 C<defsv_off> must be zero (it used to identity the pad slot of lexical $_).
7786 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
7788 PERL_ARGS_ASSERT_NEWGIVENOP;
7789 PERL_UNUSED_ARG(defsv_off);
7792 return newGIVWHENOP(
7793 ref_array_or_hash(cond),
7795 OP_ENTERGIVEN, OP_LEAVEGIVEN,
7800 =for apidoc Am|OP *|newWHENOP|OP *cond|OP *block
7802 Constructs, checks, and returns an op tree expressing a C<when> block.
7803 C<cond> supplies the test expression, and C<block> supplies the block
7804 that will be executed if the test evaluates to true; they are consumed
7805 by this function and become part of the constructed op tree. C<cond>
7806 will be interpreted DWIMically, often as a comparison against C<$_>,
7807 and may be null to generate a C<default> block.
7813 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
7815 const bool cond_llb = (!cond || looks_like_bool(cond));
7818 PERL_ARGS_ASSERT_NEWWHENOP;
7823 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
7825 scalar(ref_array_or_hash(cond)));
7828 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
7831 /* must not conflict with SVf_UTF8 */
7832 #define CV_CKPROTO_CURSTASH 0x1
7835 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
7836 const STRLEN len, const U32 flags)
7838 SV *name = NULL, *msg;
7839 const char * cvp = SvROK(cv)
7840 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV
7841 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv))
7844 STRLEN clen = CvPROTOLEN(cv), plen = len;
7846 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
7848 if (p == NULL && cvp == NULL)
7851 if (!ckWARN_d(WARN_PROTOTYPE))
7855 p = S_strip_spaces(aTHX_ p, &plen);
7856 cvp = S_strip_spaces(aTHX_ cvp, &clen);
7857 if ((flags & SVf_UTF8) == SvUTF8(cv)) {
7858 if (plen == clen && memEQ(cvp, p, plen))
7861 if (flags & SVf_UTF8) {
7862 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0)
7866 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0)
7872 msg = sv_newmortal();
7877 gv_efullname3(name = sv_newmortal(), gv, NULL);
7878 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&')
7879 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP);
7880 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) {
7881 name = sv_2mortal(newSVhek(HvNAME_HEK(PL_curstash)));
7882 sv_catpvs(name, "::");
7884 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV);
7885 assert (CvNAMED(SvRV_const(gv)));
7886 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv))));
7888 else sv_catsv(name, (SV *)gv);
7890 else name = (SV *)gv;
7892 sv_setpvs(msg, "Prototype mismatch:");
7894 Perl_sv_catpvf(aTHX_ msg, " sub %" SVf, SVfARG(name));
7896 Perl_sv_catpvf(aTHX_ msg, " (%" UTF8f ")",
7897 UTF8fARG(SvUTF8(cv),clen,cvp)
7900 sv_catpvs(msg, ": none");
7901 sv_catpvs(msg, " vs ");
7903 Perl_sv_catpvf(aTHX_ msg, "(%" UTF8f ")", UTF8fARG(flags & SVf_UTF8,len,p));
7905 sv_catpvs(msg, "none");
7906 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%" SVf, SVfARG(msg));
7909 static void const_sv_xsub(pTHX_ CV* cv);
7910 static void const_av_xsub(pTHX_ CV* cv);
7914 =head1 Optree Manipulation Functions
7916 =for apidoc cv_const_sv
7918 If C<cv> is a constant sub eligible for inlining, returns the constant
7919 value returned by the sub. Otherwise, returns C<NULL>.
7921 Constant subs can be created with C<newCONSTSUB> or as described in
7922 L<perlsub/"Constant Functions">.
7927 Perl_cv_const_sv(const CV *const cv)
7932 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
7934 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7935 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL;
7940 Perl_cv_const_sv_or_av(const CV * const cv)
7944 if (SvROK(cv)) return SvRV((SV *)cv);
7945 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
7946 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7949 /* op_const_sv: examine an optree to determine whether it's in-lineable.
7950 * Can be called in 2 ways:
7953 * look for a single OP_CONST with attached value: return the value
7955 * allow_lex && !CvCONST(cv);
7957 * examine the clone prototype, and if contains only a single
7958 * OP_CONST, return the value; or if it contains a single PADSV ref-
7959 * erencing an outer lexical, turn on CvCONST to indicate the CV is
7960 * a candidate for "constizing" at clone time, and return NULL.
7964 S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex)
7972 for (; o; o = o->op_next) {
7973 const OPCODE type = o->op_type;
7975 if (type == OP_NEXTSTATE || type == OP_LINESEQ
7977 || type == OP_PUSHMARK)
7979 if (type == OP_DBSTATE)
7981 if (type == OP_LEAVESUB)
7985 if (type == OP_CONST && cSVOPo->op_sv)
7987 else if (type == OP_UNDEF && !o->op_private) {
7991 else if (allow_lex && type == OP_PADSV) {
7992 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEt_OUTER)
7994 sv = &PL_sv_undef; /* an arbitrary non-null value */
8012 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
8013 PADNAME * const name, SV ** const const_svp)
8019 if (CvFLAGS(PL_compcv)) {
8020 /* might have had built-in attrs applied */
8021 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
8022 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
8023 && ckWARN(WARN_MISC))
8025 /* protect against fatal warnings leaking compcv */
8026 SAVEFREESV(PL_compcv);
8027 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
8028 SvREFCNT_inc_simple_void_NN(PL_compcv);
8031 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
8032 & ~(CVf_LVALUE * pureperl));
8037 /* redundant check for speed: */
8038 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
8039 const line_t oldline = CopLINE(PL_curcop);
8042 : sv_2mortal(newSVpvn_utf8(
8043 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name)
8045 if (PL_parser && PL_parser->copline != NOLINE)
8046 /* This ensures that warnings are reported at the first
8047 line of a redefinition, not the last. */
8048 CopLINE_set(PL_curcop, PL_parser->copline);
8049 /* protect against fatal warnings leaking compcv */
8050 SAVEFREESV(PL_compcv);
8051 report_redefined_cv(namesv, cv, const_svp);
8052 SvREFCNT_inc_simple_void_NN(PL_compcv);
8053 CopLINE_set(PL_curcop, oldline);
8060 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
8065 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
8068 CV *compcv = PL_compcv;
8071 PADOFFSET pax = o->op_targ;
8072 CV *outcv = CvOUTSIDE(PL_compcv);
8075 bool reusable = FALSE;
8077 #ifdef PERL_DEBUG_READONLY_OPS
8078 OPSLAB *slab = NULL;
8081 PERL_ARGS_ASSERT_NEWMYSUB;
8083 /* Find the pad slot for storing the new sub.
8084 We cannot use PL_comppad, as it is the pad owned by the new sub. We
8085 need to look in CvOUTSIDE and find the pad belonging to the enclos-
8086 ing sub. And then we need to dig deeper if this is a lexical from
8088 my sub foo; sub { sub foo { } }
8091 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
8092 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
8093 pax = PARENT_PAD_INDEX(name);
8094 outcv = CvOUTSIDE(outcv);
8099 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
8100 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
8101 spot = (CV **)svspot;
8103 if (!(PL_parser && PL_parser->error_count))
8104 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name));
8107 assert(proto->op_type == OP_CONST);
8108 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
8109 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
8119 if (PL_parser && PL_parser->error_count) {
8121 SvREFCNT_dec(PL_compcv);
8126 if (CvDEPTH(outcv) && CvCLONE(compcv)) {
8128 svspot = (SV **)(spot = &clonee);
8130 else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
8133 assert (SvTYPE(*spot) == SVt_PVCV);
8135 hek = CvNAME_HEK(*spot);
8139 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
8140 CvNAME_HEK_set(*spot, hek =
8143 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
8147 CvLEXICAL_on(*spot);
8149 cv = PadnamePROTOCV(name);
8150 svspot = (SV **)(spot = &PadnamePROTOCV(name));
8154 /* This makes sub {}; work as expected. */
8155 if (block->op_type == OP_STUB) {
8156 const line_t l = PL_parser->copline;
8158 block = newSTATEOP(0, NULL, 0);
8159 PL_parser->copline = l;
8161 block = CvLVALUE(compcv)
8162 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv))
8163 ? newUNOP(OP_LEAVESUBLV, 0,
8164 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
8165 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
8166 start = LINKLIST(block);
8168 if (ps && !*ps && !attrs && !CvLVALUE(compcv))
8169 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE);
8177 const bool exists = CvROOT(cv) || CvXSUB(cv);
8179 /* if the subroutine doesn't exist and wasn't pre-declared
8180 * with a prototype, assume it will be AUTOLOADed,
8181 * skipping the prototype check
8183 if (exists || SvPOK(cv))
8184 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len,
8186 /* already defined? */
8188 S_already_defined(aTHX_ cv, block, NULL, name, &const_sv);
8194 /* just a "sub foo;" when &foo is already defined */
8199 else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
8206 SvREFCNT_inc_simple_void_NN(const_sv);
8207 SvFLAGS(const_sv) |= SVs_PADTMP;
8209 assert(!CvROOT(cv) && !CvCONST(cv));
8213 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8214 CvFILE_set_from_cop(cv, PL_curcop);
8215 CvSTASH_set(cv, PL_curstash);
8218 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
8219 CvXSUBANY(cv).any_ptr = const_sv;
8220 CvXSUB(cv) = const_sv_xsub;
8224 CvFLAGS(cv) |= CvMETHOD(compcv);
8226 SvREFCNT_dec(compcv);
8231 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
8232 determine whether this sub definition is in the same scope as its
8233 declaration. If this sub definition is inside an inner named pack-
8234 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
8235 the package sub. So check PadnameOUTER(name) too.
8237 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) {
8238 assert(!CvWEAKOUTSIDE(compcv));
8239 SvREFCNT_dec(CvOUTSIDE(compcv));
8240 CvWEAKOUTSIDE_on(compcv);
8242 /* XXX else do we have a circular reference? */
8244 if (cv) { /* must reuse cv in case stub is referenced elsewhere */
8245 /* transfer PL_compcv to cv */
8247 cv_flags_t preserved_flags =
8248 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
8249 PADLIST *const temp_padl = CvPADLIST(cv);
8250 CV *const temp_cv = CvOUTSIDE(cv);
8251 const cv_flags_t other_flags =
8252 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
8253 OP * const cvstart = CvSTART(cv);
8257 CvFLAGS(compcv) | preserved_flags;
8258 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
8259 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
8260 CvPADLIST_set(cv, CvPADLIST(compcv));
8261 CvOUTSIDE(compcv) = temp_cv;
8262 CvPADLIST_set(compcv, temp_padl);
8263 CvSTART(cv) = CvSTART(compcv);
8264 CvSTART(compcv) = cvstart;
8265 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
8266 CvFLAGS(compcv) |= other_flags;
8268 if (CvFILE(cv) && CvDYNFILE(cv)) {
8269 Safefree(CvFILE(cv));
8272 /* inner references to compcv must be fixed up ... */
8273 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
8274 if (PERLDB_INTER)/* Advice debugger on the new sub. */
8275 ++PL_sub_generation;
8278 /* Might have had built-in attributes applied -- propagate them. */
8279 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
8281 /* ... before we throw it away */
8282 SvREFCNT_dec(compcv);
8283 PL_compcv = compcv = cv;
8292 if (!CvNAME_HEK(cv)) {
8293 if (hek) (void)share_hek_hek(hek);
8297 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
8298 hek = share_hek(PadnamePV(name)+1,
8299 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
8302 CvNAME_HEK_set(cv, hek);
8308 CvFILE_set_from_cop(cv, PL_curcop);
8309 CvSTASH_set(cv, PL_curstash);
8312 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
8314 SvUTF8_on(MUTABLE_SV(cv));
8318 /* If we assign an optree to a PVCV, then we've defined a
8319 * subroutine that the debugger could be able to set a breakpoint
8320 * in, so signal to pp_entereval that it should not throw away any
8321 * saved lines at scope exit. */
8323 PL_breakable_sub_gen++;
8325 CvROOT(cv)->op_private |= OPpREFCOUNTED;
8326 OpREFCNT_set(CvROOT(cv), 1);
8327 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8328 itself has a refcount. */
8330 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8331 #ifdef PERL_DEBUG_READONLY_OPS
8332 slab = (OPSLAB *)CvSTART(cv);
8334 CvSTART(cv) = start;
8336 finalize_optree(CvROOT(cv));
8337 S_prune_chain_head(&CvSTART(cv));
8339 /* now that optimizer has done its work, adjust pad values */
8341 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
8346 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8347 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
8351 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8352 SV * const tmpstr = sv_newmortal();
8353 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8354 GV_ADDMULTI, SVt_PVHV);
8356 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8359 (long)CopLINE(PL_curcop));
8360 if (HvNAME_HEK(PL_curstash)) {
8361 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
8362 sv_catpvs(tmpstr, "::");
8365 sv_setpvs(tmpstr, "__ANON__::");
8367 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
8368 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
8369 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8370 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8371 hv = GvHVn(db_postponed);
8372 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8373 CV * const pcv = GvCV(db_postponed);
8379 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8387 assert(CvDEPTH(outcv));
8389 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
8391 cv_clone_into(clonee, *spot);
8392 else *spot = cv_clone(clonee);
8393 SvREFCNT_dec_NN(clonee);
8397 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
8398 PADOFFSET depth = CvDEPTH(outcv);
8401 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
8403 *svspot = SvREFCNT_inc_simple_NN(cv);
8404 SvREFCNT_dec(oldcv);
8410 PL_parser->copline = NOLINE;
8412 #ifdef PERL_DEBUG_READONLY_OPS
8423 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
8424 OP *block, bool o_is_gv)
8428 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
8430 CV *cv = NULL; /* the previous CV with this name, if any */
8432 const bool ec = PL_parser && PL_parser->error_count;
8433 /* If the subroutine has no body, no attributes, and no builtin attributes
8434 then it's just a sub declaration, and we may be able to get away with
8435 storing with a placeholder scalar in the symbol table, rather than a
8436 full CV. If anything is present then it will take a full CV to
8438 const I32 gv_fetch_flags
8439 = ec ? GV_NOADD_NOINIT :
8440 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
8441 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
8443 const char * const name =
8444 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
8446 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
8447 bool evanescent = FALSE;
8449 #ifdef PERL_DEBUG_READONLY_OPS
8450 OPSLAB *slab = NULL;
8458 /* Try to optimise and avoid creating a GV. Instead, the CV’s name
8459 hek and CvSTASH pointer together can imply the GV. If the name
8460 contains a package name, then GvSTASH(CvGV(cv)) may differ from
8461 CvSTASH, so forego the optimisation if we find any.
8462 Also, we may be called from load_module at run time, so
8463 PL_curstash (which sets CvSTASH) may not point to the stash the
8464 sub is stored in. */
8466 ec ? GV_NOADD_NOINIT
8467 : PL_curstash != CopSTASH(PL_curcop)
8468 || memchr(name, ':', namlen) || memchr(name, '\'', namlen)
8470 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL;
8471 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV);
8473 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
8474 SV * const sv = sv_newmortal();
8475 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%" IVdf "]",
8476 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
8477 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
8478 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
8480 } else if (PL_curstash) {
8481 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
8484 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
8490 move_proto_attr(&proto, &attrs, gv);
8493 move_proto_attr(&proto, &attrs, (GV *)cSVOPo->op_sv);
8498 assert(proto->op_type == OP_CONST);
8499 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
8500 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
8516 SvREFCNT_dec(PL_compcv);
8521 if (name && block) {
8522 const char *s = strrchr(name, ':');
8524 if (strEQ(s, "BEGIN")) {
8525 if (PL_in_eval & EVAL_KEEPERR)
8526 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
8528 SV * const errsv = ERRSV;
8529 /* force display of errors found but not reported */
8530 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
8531 Perl_croak_nocontext("%" SVf, SVfARG(errsv));
8538 if (!block && SvTYPE(gv) != SVt_PVGV) {
8539 /* If we are not defining a new sub and the existing one is not a
8541 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) {
8542 /* We are applying attributes to an existing sub, so we need it
8543 upgraded if it is a constant. */
8544 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)
8545 gv_init_pvn(gv, PL_curstash, name, namlen,
8546 SVf_UTF8 * name_is_utf8);
8548 else { /* Maybe prototype now, and had at maximum
8549 a prototype or const/sub ref before. */
8550 if (SvTYPE(gv) > SVt_NULL) {
8551 cv_ckproto_len_flags((const CV *)gv,
8552 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
8558 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
8560 SvUTF8_on(MUTABLE_SV(gv));
8563 sv_setiv(MUTABLE_SV(gv), -1);
8566 SvREFCNT_dec(PL_compcv);
8567 cv = PL_compcv = NULL;
8572 cv = (!name || (isGV(gv) && GvCVGEN(gv)))
8576 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
8582 /* This makes sub {}; work as expected. */
8583 if (block->op_type == OP_STUB) {
8584 const line_t l = PL_parser->copline;
8586 block = newSTATEOP(0, NULL, 0);
8587 PL_parser->copline = l;
8589 block = CvLVALUE(PL_compcv)
8590 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)
8591 && (!isGV(gv) || !GvASSUMECV(gv)))
8592 ? newUNOP(OP_LEAVESUBLV, 0,
8593 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
8594 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
8595 start = LINKLIST(block);
8597 if (ps && !*ps && !attrs && !CvLVALUE(PL_compcv))
8599 S_op_const_sv(aTHX_ start, PL_compcv,
8600 cBOOL(CvCLONE(PL_compcv)));
8607 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) {
8608 cv_ckproto_len_flags((const CV *)gv,
8609 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
8610 ps_len, ps_utf8|CV_CKPROTO_CURSTASH);
8612 /* All the other code for sub redefinition warnings expects the
8613 clobbered sub to be a CV. Instead of making all those code
8614 paths more complex, just inline the RV version here. */
8615 const line_t oldline = CopLINE(PL_curcop);
8616 assert(IN_PERL_COMPILETIME);
8617 if (PL_parser && PL_parser->copline != NOLINE)
8618 /* This ensures that warnings are reported at the first
8619 line of a redefinition, not the last. */
8620 CopLINE_set(PL_curcop, PL_parser->copline);
8621 /* protect against fatal warnings leaking compcv */
8622 SAVEFREESV(PL_compcv);
8624 if (ckWARN(WARN_REDEFINE)
8625 || ( ckWARN_d(WARN_REDEFINE)
8626 && ( !const_sv || SvRV(gv) == const_sv
8627 || sv_cmp(SvRV(gv), const_sv) ))) {
8629 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8630 "Constant subroutine %" SVf " redefined",
8631 SVfARG(cSVOPo->op_sv));
8634 SvREFCNT_inc_simple_void_NN(PL_compcv);
8635 CopLINE_set(PL_curcop, oldline);
8636 SvREFCNT_dec(SvRV(gv));
8641 const bool exists = CvROOT(cv) || CvXSUB(cv);
8643 /* if the subroutine doesn't exist and wasn't pre-declared
8644 * with a prototype, assume it will be AUTOLOADed,
8645 * skipping the prototype check
8647 if (exists || SvPOK(cv))
8648 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
8649 /* already defined (or promised)? */
8650 if (exists || (isGV(gv) && GvASSUMECV(gv))) {
8651 S_already_defined(aTHX_ cv, block, o, NULL, &const_sv);
8657 /* just a "sub foo;" when &foo is already defined */
8658 SAVEFREESV(PL_compcv);
8665 SvREFCNT_inc_simple_void_NN(const_sv);
8666 SvFLAGS(const_sv) |= SVs_PADTMP;
8668 assert(!CvROOT(cv) && !CvCONST(cv));
8670 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
8671 CvXSUBANY(cv).any_ptr = const_sv;
8672 CvXSUB(cv) = const_sv_xsub;
8676 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
8679 if (isGV(gv) || CvMETHOD(PL_compcv)) {
8680 if (name && isGV(gv))
8682 cv = newCONSTSUB_flags(
8683 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
8686 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
8690 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
8691 prepare_SV_for_RV((SV *)gv);
8695 SvRV_set(gv, const_sv);
8699 SvREFCNT_dec(PL_compcv);
8704 /* don't copy new BEGIN CV to old BEGIN CV - RT #129099 */
8705 if (name && cv && *name == 'B' && strEQ(name, "BEGIN"))
8708 if (cv) { /* must reuse cv if autoloaded */
8709 /* transfer PL_compcv to cv */
8711 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
8712 PADLIST *const temp_av = CvPADLIST(cv);
8713 CV *const temp_cv = CvOUTSIDE(cv);
8714 const cv_flags_t other_flags =
8715 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
8716 OP * const cvstart = CvSTART(cv);
8720 assert(!CvCVGV_RC(cv));
8721 assert(CvGV(cv) == gv);
8726 PERL_HASH(hash, name, namlen);
8736 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs
8738 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
8739 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
8740 CvPADLIST_set(cv,CvPADLIST(PL_compcv));
8741 CvOUTSIDE(PL_compcv) = temp_cv;
8742 CvPADLIST_set(PL_compcv, temp_av);
8743 CvSTART(cv) = CvSTART(PL_compcv);
8744 CvSTART(PL_compcv) = cvstart;
8745 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
8746 CvFLAGS(PL_compcv) |= other_flags;
8748 if (CvFILE(cv) && CvDYNFILE(cv)) {
8749 Safefree(CvFILE(cv));
8751 CvFILE_set_from_cop(cv, PL_curcop);
8752 CvSTASH_set(cv, PL_curstash);
8754 /* inner references to PL_compcv must be fixed up ... */
8755 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
8756 if (PERLDB_INTER)/* Advice debugger on the new sub. */
8757 ++PL_sub_generation;
8760 /* Might have had built-in attributes applied -- propagate them. */
8761 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
8763 /* ... before we throw it away */
8764 SvREFCNT_dec(PL_compcv);
8769 if (name && isGV(gv)) {
8772 if (HvENAME_HEK(GvSTASH(gv)))
8773 /* sub Foo::bar { (shift)+1 } */
8774 gv_method_changed(gv);
8778 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
8779 prepare_SV_for_RV((SV *)gv);
8783 SvRV_set(gv, (SV *)cv);
8793 PERL_HASH(hash, name, namlen);
8794 CvNAME_HEK_set(cv, share_hek(name,
8800 CvFILE_set_from_cop(cv, PL_curcop);
8801 CvSTASH_set(cv, PL_curstash);
8805 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
8807 SvUTF8_on(MUTABLE_SV(cv));
8811 /* If we assign an optree to a PVCV, then we've defined a
8812 * subroutine that the debugger could be able to set a breakpoint
8813 * in, so signal to pp_entereval that it should not throw away any
8814 * saved lines at scope exit. */
8816 PL_breakable_sub_gen++;
8818 CvROOT(cv)->op_private |= OPpREFCOUNTED;
8819 OpREFCNT_set(CvROOT(cv), 1);
8820 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8821 itself has a refcount. */
8823 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8824 #ifdef PERL_DEBUG_READONLY_OPS
8825 slab = (OPSLAB *)CvSTART(cv);
8827 CvSTART(cv) = start;
8829 finalize_optree(CvROOT(cv));
8830 S_prune_chain_head(&CvSTART(cv));
8832 /* now that optimizer has done its work, adjust pad values */
8834 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
8839 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8840 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv))
8845 apply_attrs(stash, MUTABLE_SV(cv), attrs);
8847 SvREFCNT_inc_simple_void_NN(cv);
8850 if (block && has_name) {
8851 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8852 SV * const tmpstr = cv_name(cv,NULL,0);
8853 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8854 GV_ADDMULTI, SVt_PVHV);
8856 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8859 (long)CopLINE(PL_curcop));
8860 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8861 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8862 hv = GvHVn(db_postponed);
8863 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8864 CV * const pcv = GvCV(db_postponed);
8870 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8876 if (PL_parser && PL_parser->error_count)
8877 clear_special_blocks(name, gv, cv);
8880 process_special_blocks(floor, name, gv, cv);
8886 PL_parser->copline = NOLINE;
8890 #ifdef PERL_DEBUG_READONLY_OPS
8894 if (cv && name && block && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv)))
8895 pad_add_weakref(cv);
8901 S_clear_special_blocks(pTHX_ const char *const fullname,
8902 GV *const gv, CV *const cv) {
8906 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS;
8908 colon = strrchr(fullname,':');
8909 name = colon ? colon + 1 : fullname;
8911 if ((*name == 'B' && strEQ(name, "BEGIN"))
8912 || (*name == 'E' && strEQ(name, "END"))
8913 || (*name == 'U' && strEQ(name, "UNITCHECK"))
8914 || (*name == 'C' && strEQ(name, "CHECK"))
8915 || (*name == 'I' && strEQ(name, "INIT"))) {
8921 SvREFCNT_dec_NN(MUTABLE_SV(cv));
8925 /* Returns true if the sub has been freed. */
8927 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
8931 const char *const colon = strrchr(fullname,':');
8932 const char *const name = colon ? colon + 1 : fullname;
8934 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
8937 if (strEQ(name, "BEGIN")) {
8938 const I32 oldscope = PL_scopestack_ix;
8941 if (floor) LEAVE_SCOPE(floor);
8943 PUSHSTACKi(PERLSI_REQUIRE);
8944 SAVECOPFILE(&PL_compiling);
8945 SAVECOPLINE(&PL_compiling);
8946 SAVEVPTR(PL_curcop);
8948 DEBUG_x( dump_sub(gv) );
8949 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
8950 GvCV_set(gv,0); /* cv has been hijacked */
8951 call_list(oldscope, PL_beginav);
8955 return !PL_savebegin;
8961 if strEQ(name, "END") {
8962 DEBUG_x( dump_sub(gv) );
8963 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
8966 } else if (*name == 'U') {
8967 if (strEQ(name, "UNITCHECK")) {
8968 /* It's never too late to run a unitcheck block */
8969 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
8973 } else if (*name == 'C') {
8974 if (strEQ(name, "CHECK")) {
8976 /* diag_listed_as: Too late to run %s block */
8977 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8978 "Too late to run CHECK block");
8979 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
8983 } else if (*name == 'I') {
8984 if (strEQ(name, "INIT")) {
8986 /* diag_listed_as: Too late to run %s block */
8987 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8988 "Too late to run INIT block");
8989 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
8995 DEBUG_x( dump_sub(gv) );
8997 GvCV_set(gv,0); /* cv has been hijacked */
9003 =for apidoc newCONSTSUB
9005 See L</newCONSTSUB_flags>.
9011 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
9013 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
9017 =for apidoc newCONSTSUB_flags
9019 Creates a constant sub equivalent to Perl S<C<sub FOO () { 123 }>> which is
9020 eligible for inlining at compile-time.
9022 Currently, the only useful value for C<flags> is C<SVf_UTF8>.
9024 The newly created subroutine takes ownership of a reference to the passed in
9027 Passing C<NULL> for SV creates a constant sub equivalent to S<C<sub BAR () {}>>,
9028 which won't be called if used as a destructor, but will suppress the overhead
9029 of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at
9036 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
9040 const char *const file = CopFILE(PL_curcop);
9044 if (IN_PERL_RUNTIME) {
9045 /* at runtime, it's not safe to manipulate PL_curcop: it may be
9046 * an op shared between threads. Use a non-shared COP for our
9048 SAVEVPTR(PL_curcop);
9049 SAVECOMPILEWARNINGS();
9050 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
9051 PL_curcop = &PL_compiling;
9053 SAVECOPLINE(PL_curcop);
9054 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
9057 PL_hints &= ~HINT_BLOCK_SCOPE;
9060 SAVEGENERICSV(PL_curstash);
9061 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
9064 /* Protect sv against leakage caused by fatal warnings. */
9065 if (sv) SAVEFREESV(sv);
9067 /* file becomes the CvFILE. For an XS, it's usually static storage,
9068 and so doesn't get free()d. (It's expected to be from the C pre-
9069 processor __FILE__ directive). But we need a dynamically allocated one,
9070 and we need it to get freed. */
9071 cv = newXS_len_flags(name, len,
9072 sv && SvTYPE(sv) == SVt_PVAV
9075 file ? file : "", "",
9076 &sv, XS_DYNAMIC_FILENAME | flags);
9077 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
9086 =for apidoc U||newXS
9088 Used by C<xsubpp> to hook up XSUBs as Perl subs. C<filename> needs to be
9089 static storage, as it is used directly as CvFILE(), without a copy being made.
9095 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
9097 PERL_ARGS_ASSERT_NEWXS;
9098 return newXS_len_flags(
9099 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
9104 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
9105 const char *const filename, const char *const proto,
9108 PERL_ARGS_ASSERT_NEWXS_FLAGS;
9109 return newXS_len_flags(
9110 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
9115 Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr)
9117 PERL_ARGS_ASSERT_NEWXS_DEFFILE;
9118 return newXS_len_flags(
9119 name, strlen(name), subaddr, NULL, NULL, NULL, 0
9124 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
9125 XSUBADDR_t subaddr, const char *const filename,
9126 const char *const proto, SV **const_svp,
9130 bool interleave = FALSE;
9132 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
9135 GV * const gv = gv_fetchpvn(
9136 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
9137 name ? len : PL_curstash ? sizeof("__ANON__") - 1:
9138 sizeof("__ANON__::__ANON__") - 1,
9139 GV_ADDMULTI | flags, SVt_PVCV);
9141 if ((cv = (name ? GvCV(gv) : NULL))) {
9143 /* just a cached method */
9147 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
9148 /* already defined (or promised) */
9149 /* Redundant check that allows us to avoid creating an SV
9150 most of the time: */
9151 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
9152 report_redefined_cv(newSVpvn_flags(
9153 name,len,(flags&SVf_UTF8)|SVs_TEMP
9164 if (cv) /* must reuse cv if autoloaded */
9167 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
9171 if (HvENAME_HEK(GvSTASH(gv)))
9172 gv_method_changed(gv); /* newXS */
9178 /* XSUBs can't be perl lang/perl5db.pl debugged
9179 if (PERLDB_LINE_OR_SAVESRC)
9180 (void)gv_fetchfile(filename); */
9181 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
9182 if (flags & XS_DYNAMIC_FILENAME) {
9184 CvFILE(cv) = savepv(filename);
9186 /* NOTE: not copied, as it is expected to be an external constant string */
9187 CvFILE(cv) = (char *)filename;
9190 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename);
9191 CvFILE(cv) = (char*)PL_xsubfilename;
9194 CvXSUB(cv) = subaddr;
9195 #ifndef PERL_IMPLICIT_CONTEXT
9196 CvHSCXT(cv) = &PL_stack_sp;
9202 process_special_blocks(0, name, gv, cv);
9205 } /* <- not a conditional branch */
9208 sv_setpv(MUTABLE_SV(cv), proto);
9209 if (interleave) LEAVE;
9214 Perl_newSTUB(pTHX_ GV *gv, bool fake)
9216 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
9218 PERL_ARGS_ASSERT_NEWSTUB;
9222 if (!fake && GvSTASH(gv) && HvENAME_HEK(GvSTASH(gv)))
9223 gv_method_changed(gv);
9225 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
9230 CvFILE_set_from_cop(cv, PL_curcop);
9231 CvSTASH_set(cv, PL_curstash);
9237 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
9243 if (PL_parser && PL_parser->error_count) {
9249 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
9250 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
9253 if ((cv = GvFORM(gv))) {
9254 if (ckWARN(WARN_REDEFINE)) {
9255 const line_t oldline = CopLINE(PL_curcop);
9256 if (PL_parser && PL_parser->copline != NOLINE)
9257 CopLINE_set(PL_curcop, PL_parser->copline);
9259 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
9260 "Format %" SVf " redefined", SVfARG(cSVOPo->op_sv));
9262 /* diag_listed_as: Format %s redefined */
9263 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
9264 "Format STDOUT redefined");
9266 CopLINE_set(PL_curcop, oldline);
9271 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
9273 CvFILE_set_from_cop(cv, PL_curcop);
9276 pad_tidy(padtidy_FORMAT);
9277 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
9278 CvROOT(cv)->op_private |= OPpREFCOUNTED;
9279 OpREFCNT_set(CvROOT(cv), 1);
9280 CvSTART(cv) = LINKLIST(CvROOT(cv));
9281 CvROOT(cv)->op_next = 0;
9282 CALL_PEEP(CvSTART(cv));
9283 finalize_optree(CvROOT(cv));
9284 S_prune_chain_head(&CvSTART(cv));
9290 PL_parser->copline = NOLINE;
9292 PL_compiling.cop_seq = 0;
9296 Perl_newANONLIST(pTHX_ OP *o)
9298 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o);
9302 Perl_newANONHASH(pTHX_ OP *o)
9304 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o);
9308 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
9310 return newANONATTRSUB(floor, proto, NULL, block);
9314 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
9316 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block));
9318 newSVOP(OP_ANONCODE, 0,
9320 if (CvANONCONST(cv))
9321 anoncode = newUNOP(OP_ANONCONST, 0,
9322 op_convert_list(OP_ENTERSUB,
9323 OPf_STACKED|OPf_WANT_SCALAR,
9325 return newUNOP(OP_REFGEN, 0, anoncode);
9329 Perl_oopsAV(pTHX_ OP *o)
9333 PERL_ARGS_ASSERT_OOPSAV;
9335 switch (o->op_type) {
9338 OpTYPE_set(o, OP_PADAV);
9339 return ref(o, OP_RV2AV);
9343 OpTYPE_set(o, OP_RV2AV);
9348 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
9355 Perl_oopsHV(pTHX_ OP *o)
9359 PERL_ARGS_ASSERT_OOPSHV;
9361 switch (o->op_type) {
9364 OpTYPE_set(o, OP_PADHV);
9365 return ref(o, OP_RV2HV);
9369 OpTYPE_set(o, OP_RV2HV);
9374 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
9381 Perl_newAVREF(pTHX_ OP *o)
9385 PERL_ARGS_ASSERT_NEWAVREF;
9387 if (o->op_type == OP_PADANY) {
9388 OpTYPE_set(o, OP_PADAV);
9391 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
9392 Perl_croak(aTHX_ "Can't use an array as a reference");
9394 return newUNOP(OP_RV2AV, 0, scalar(o));
9398 Perl_newGVREF(pTHX_ I32 type, OP *o)
9400 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
9401 return newUNOP(OP_NULL, 0, o);
9402 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
9406 Perl_newHVREF(pTHX_ OP *o)
9410 PERL_ARGS_ASSERT_NEWHVREF;
9412 if (o->op_type == OP_PADANY) {
9413 OpTYPE_set(o, OP_PADHV);
9416 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
9417 Perl_croak(aTHX_ "Can't use a hash as a reference");
9419 return newUNOP(OP_RV2HV, 0, scalar(o));
9423 Perl_newCVREF(pTHX_ I32 flags, OP *o)
9425 if (o->op_type == OP_PADANY) {
9427 OpTYPE_set(o, OP_PADCV);
9429 return newUNOP(OP_RV2CV, flags, scalar(o));
9433 Perl_newSVREF(pTHX_ OP *o)
9437 PERL_ARGS_ASSERT_NEWSVREF;
9439 if (o->op_type == OP_PADANY) {
9440 OpTYPE_set(o, OP_PADSV);
9444 return newUNOP(OP_RV2SV, 0, scalar(o));
9447 /* Check routines. See the comments at the top of this file for details
9448 * on when these are called */
9451 Perl_ck_anoncode(pTHX_ OP *o)
9453 PERL_ARGS_ASSERT_CK_ANONCODE;
9455 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
9456 cSVOPo->op_sv = NULL;
9461 S_io_hints(pTHX_ OP *o)
9463 #if O_BINARY != 0 || O_TEXT != 0
9465 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
9467 SV **svp = hv_fetchs(table, "open_IN", FALSE);
9470 const char *d = SvPV_const(*svp, len);
9471 const I32 mode = mode_from_discipline(d, len);
9472 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
9474 if (mode & O_BINARY)
9475 o->op_private |= OPpOPEN_IN_RAW;
9479 o->op_private |= OPpOPEN_IN_CRLF;
9483 svp = hv_fetchs(table, "open_OUT", FALSE);
9486 const char *d = SvPV_const(*svp, len);
9487 const I32 mode = mode_from_discipline(d, len);
9488 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
9490 if (mode & O_BINARY)
9491 o->op_private |= OPpOPEN_OUT_RAW;
9495 o->op_private |= OPpOPEN_OUT_CRLF;
9500 PERL_UNUSED_CONTEXT;
9506 Perl_ck_backtick(pTHX_ OP *o)
9511 PERL_ARGS_ASSERT_CK_BACKTICK;
9512 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
9513 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first))
9514 && (gv = gv_override("readpipe",8)))
9516 /* detach rest of siblings from o and its first child */
9517 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
9518 newop = S_new_entersubop(aTHX_ gv, sibl);
9520 else if (!(o->op_flags & OPf_KIDS))
9521 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
9526 S_io_hints(aTHX_ o);
9531 Perl_ck_bitop(pTHX_ OP *o)
9533 PERL_ARGS_ASSERT_CK_BITOP;
9535 o->op_private = (U8)(PL_hints & HINT_INTEGER);
9537 if (o->op_type == OP_NBIT_OR || o->op_type == OP_SBIT_OR
9538 || o->op_type == OP_NBIT_XOR || o->op_type == OP_SBIT_XOR
9539 || o->op_type == OP_NBIT_AND || o->op_type == OP_SBIT_AND
9540 || o->op_type == OP_NCOMPLEMENT || o->op_type == OP_SCOMPLEMENT)
9541 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__BITWISE),
9542 "The bitwise feature is experimental");
9543 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
9544 && OP_IS_INFIX_BIT(o->op_type))
9546 const OP * const left = cBINOPo->op_first;
9547 const OP * const right = OpSIBLING(left);
9548 if ((OP_IS_NUMCOMPARE(left->op_type) &&
9549 (left->op_flags & OPf_PARENS) == 0) ||
9550 (OP_IS_NUMCOMPARE(right->op_type) &&
9551 (right->op_flags & OPf_PARENS) == 0))
9552 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
9553 "Possible precedence problem on bitwise %s operator",
9554 o->op_type == OP_BIT_OR
9555 ||o->op_type == OP_NBIT_OR ? "|"
9556 : o->op_type == OP_BIT_AND
9557 ||o->op_type == OP_NBIT_AND ? "&"
9558 : o->op_type == OP_BIT_XOR
9559 ||o->op_type == OP_NBIT_XOR ? "^"
9560 : o->op_type == OP_SBIT_OR ? "|."
9561 : o->op_type == OP_SBIT_AND ? "&." : "^."
9567 PERL_STATIC_INLINE bool
9568 is_dollar_bracket(pTHX_ const OP * const o)
9571 PERL_UNUSED_CONTEXT;
9572 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
9573 && (kid = cUNOPx(o)->op_first)
9574 && kid->op_type == OP_GV
9575 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
9579 Perl_ck_cmp(pTHX_ OP *o)
9581 PERL_ARGS_ASSERT_CK_CMP;
9582 if (ckWARN(WARN_SYNTAX)) {
9583 const OP *kid = cUNOPo->op_first;
9586 ( is_dollar_bracket(aTHX_ kid)
9587 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST
9589 || ( kid->op_type == OP_CONST
9590 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid)
9594 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9595 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
9601 Perl_ck_concat(pTHX_ OP *o)
9603 const OP * const kid = cUNOPo->op_first;
9605 PERL_ARGS_ASSERT_CK_CONCAT;
9606 PERL_UNUSED_CONTEXT;
9608 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
9609 !(kUNOP->op_first->op_flags & OPf_MOD))
9610 o->op_flags |= OPf_STACKED;
9615 Perl_ck_spair(pTHX_ OP *o)
9619 PERL_ARGS_ASSERT_CK_SPAIR;
9621 if (o->op_flags & OPf_KIDS) {
9625 const OPCODE type = o->op_type;
9626 o = modkids(ck_fun(o), type);
9627 kid = cUNOPo->op_first;
9628 kidkid = kUNOP->op_first;
9629 newop = OpSIBLING(kidkid);
9631 const OPCODE type = newop->op_type;
9632 if (OpHAS_SIBLING(newop))
9634 if (o->op_type == OP_REFGEN
9635 && ( type == OP_RV2CV
9636 || ( !(newop->op_flags & OPf_PARENS)
9637 && ( type == OP_RV2AV || type == OP_PADAV
9638 || type == OP_RV2HV || type == OP_PADHV))))
9639 NOOP; /* OK (allow srefgen for \@a and \%h) */
9640 else if (OP_GIMME(newop,0) != G_SCALAR)
9643 /* excise first sibling */
9644 op_sibling_splice(kid, NULL, 1, NULL);
9647 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
9648 * and OP_CHOMP into OP_SCHOMP */
9649 o->op_ppaddr = PL_ppaddr[++o->op_type];
9654 Perl_ck_delete(pTHX_ OP *o)
9656 PERL_ARGS_ASSERT_CK_DELETE;
9660 if (o->op_flags & OPf_KIDS) {
9661 OP * const kid = cUNOPo->op_first;
9662 switch (kid->op_type) {
9664 o->op_flags |= OPf_SPECIAL;
9667 o->op_private |= OPpSLICE;
9670 o->op_flags |= OPf_SPECIAL;
9675 Perl_croak(aTHX_ "delete argument is index/value array slice,"
9676 " use array slice");
9678 Perl_croak(aTHX_ "delete argument is key/value hash slice, use"
9681 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
9682 "element or slice");
9684 if (kid->op_private & OPpLVAL_INTRO)
9685 o->op_private |= OPpLVAL_INTRO;
9692 Perl_ck_eof(pTHX_ OP *o)
9694 PERL_ARGS_ASSERT_CK_EOF;
9696 if (o->op_flags & OPf_KIDS) {
9698 if (cLISTOPo->op_first->op_type == OP_STUB) {
9700 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
9705 kid = cLISTOPo->op_first;
9706 if (kid->op_type == OP_RV2GV)
9707 kid->op_private |= OPpALLOW_FAKE;
9713 Perl_ck_eval(pTHX_ OP *o)
9717 PERL_ARGS_ASSERT_CK_EVAL;
9719 PL_hints |= HINT_BLOCK_SCOPE;
9720 if (o->op_flags & OPf_KIDS) {
9721 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9724 if (o->op_type == OP_ENTERTRY) {
9727 /* cut whole sibling chain free from o */
9728 op_sibling_splice(o, NULL, -1, NULL);
9731 enter = alloc_LOGOP(OP_ENTERTRY, NULL, NULL);
9733 /* establish postfix order */
9734 enter->op_next = (OP*)enter;
9736 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
9737 OpTYPE_set(o, OP_LEAVETRY);
9738 enter->op_other = o;
9743 S_set_haseval(aTHX);
9747 const U8 priv = o->op_private;
9749 /* the newUNOP will recursively call ck_eval(), which will handle
9750 * all the stuff at the end of this function, like adding
9753 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
9755 o->op_targ = (PADOFFSET)PL_hints;
9756 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
9757 if ((PL_hints & HINT_LOCALIZE_HH) != 0
9758 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
9759 /* Store a copy of %^H that pp_entereval can pick up. */
9760 OP *hhop = newSVOP(OP_HINTSEVAL, 0,
9761 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv))));
9762 /* append hhop to only child */
9763 op_sibling_splice(o, cUNOPo->op_first, 0, hhop);
9765 o->op_private |= OPpEVAL_HAS_HH;
9767 if (!(o->op_private & OPpEVAL_BYTES)
9768 && FEATURE_UNIEVAL_IS_ENABLED)
9769 o->op_private |= OPpEVAL_UNICODE;
9774 Perl_ck_exec(pTHX_ OP *o)
9776 PERL_ARGS_ASSERT_CK_EXEC;
9778 if (o->op_flags & OPf_STACKED) {
9781 kid = OpSIBLING(cUNOPo->op_first);
9782 if (kid->op_type == OP_RV2GV)
9791 Perl_ck_exists(pTHX_ OP *o)
9793 PERL_ARGS_ASSERT_CK_EXISTS;
9796 if (o->op_flags & OPf_KIDS) {
9797 OP * const kid = cUNOPo->op_first;
9798 if (kid->op_type == OP_ENTERSUB) {
9799 (void) ref(kid, o->op_type);
9800 if (kid->op_type != OP_RV2CV
9801 && !(PL_parser && PL_parser->error_count))
9803 "exists argument is not a subroutine name");
9804 o->op_private |= OPpEXISTS_SUB;
9806 else if (kid->op_type == OP_AELEM)
9807 o->op_flags |= OPf_SPECIAL;
9808 else if (kid->op_type != OP_HELEM)
9809 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
9810 "element or a subroutine");
9817 Perl_ck_rvconst(pTHX_ OP *o)
9820 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9822 PERL_ARGS_ASSERT_CK_RVCONST;
9824 o->op_private |= (PL_hints & HINT_STRICT_REFS);
9826 if (kid->op_type == OP_CONST) {
9829 SV * const kidsv = kid->op_sv;
9831 /* Is it a constant from cv_const_sv()? */
9832 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) {
9835 if (SvTYPE(kidsv) == SVt_PVAV) return o;
9836 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
9837 const char *badthing;
9838 switch (o->op_type) {
9840 badthing = "a SCALAR";
9843 badthing = "an ARRAY";
9846 badthing = "a HASH";
9854 "Can't use bareword (\"%" SVf "\") as %s ref while \"strict refs\" in use",
9855 SVfARG(kidsv), badthing);
9858 * This is a little tricky. We only want to add the symbol if we
9859 * didn't add it in the lexer. Otherwise we get duplicate strict
9860 * warnings. But if we didn't add it in the lexer, we must at
9861 * least pretend like we wanted to add it even if it existed before,
9862 * or we get possible typo warnings. OPpCONST_ENTERED says
9863 * whether the lexer already added THIS instance of this symbol.
9865 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0;
9866 gv = gv_fetchsv(kidsv,
9867 o->op_type == OP_RV2CV
9868 && o->op_private & OPpMAY_RETURN_CONSTANT
9870 : iscv | !(kid->op_private & OPpCONST_ENTERED),
9873 : o->op_type == OP_RV2SV
9875 : o->op_type == OP_RV2AV
9877 : o->op_type == OP_RV2HV
9884 if (!(o->op_private & OPpMAY_RETURN_CONSTANT)
9885 && SvTYPE(SvRV(gv)) != SVt_PVCV)
9886 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV);
9888 OpTYPE_set(kid, OP_GV);
9889 SvREFCNT_dec(kid->op_sv);
9891 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
9892 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP));
9893 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY);
9894 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
9895 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
9897 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
9899 kid->op_private = 0;
9900 /* FAKE globs in the symbol table cause weird bugs (#77810) */
9908 Perl_ck_ftst(pTHX_ OP *o)
9911 const I32 type = o->op_type;
9913 PERL_ARGS_ASSERT_CK_FTST;
9915 if (o->op_flags & OPf_REF) {
9918 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
9919 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9920 const OPCODE kidtype = kid->op_type;
9922 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
9923 && !kid->op_folded) {
9924 OP * const newop = newGVOP(type, OPf_REF,
9925 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
9930 if ((kidtype == OP_RV2AV || kidtype == OP_PADAV) && ckWARN(WARN_SYNTAX)) {
9931 SV *name = S_op_varname_subscript(aTHX_ (OP*)kid, 2);
9933 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
9934 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s (did you want stat %" SVf "?)",
9935 array_passed_to_stat, name);
9938 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
9939 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s", array_passed_to_stat);
9943 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
9944 o->op_private |= OPpFT_ACCESS;
9945 if (type != OP_STAT && type != OP_LSTAT
9946 && PL_check[kidtype] == Perl_ck_ftst
9947 && kidtype != OP_STAT && kidtype != OP_LSTAT
9949 o->op_private |= OPpFT_STACKED;
9950 kid->op_private |= OPpFT_STACKING;
9951 if (kidtype == OP_FTTTY && (
9952 !(kid->op_private & OPpFT_STACKED)
9953 || kid->op_private & OPpFT_AFTER_t
9955 o->op_private |= OPpFT_AFTER_t;
9960 if (type == OP_FTTTY)
9961 o = newGVOP(type, OPf_REF, PL_stdingv);
9963 o = newUNOP(type, 0, newDEFSVOP());
9969 Perl_ck_fun(pTHX_ OP *o)
9971 const int type = o->op_type;
9972 I32 oa = PL_opargs[type] >> OASHIFT;
9974 PERL_ARGS_ASSERT_CK_FUN;
9976 if (o->op_flags & OPf_STACKED) {
9977 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
9980 return no_fh_allowed(o);
9983 if (o->op_flags & OPf_KIDS) {
9984 OP *prev_kid = NULL;
9985 OP *kid = cLISTOPo->op_first;
9987 bool seen_optional = FALSE;
9989 if (kid->op_type == OP_PUSHMARK ||
9990 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
9993 kid = OpSIBLING(kid);
9995 if (kid && kid->op_type == OP_COREARGS) {
9996 bool optional = FALSE;
9999 if (oa & OA_OPTIONAL) optional = TRUE;
10002 if (optional) o->op_private |= numargs;
10007 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
10008 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) {
10009 kid = newDEFSVOP();
10010 /* append kid to chain */
10011 op_sibling_splice(o, prev_kid, 0, kid);
10013 seen_optional = TRUE;
10020 /* list seen where single (scalar) arg expected? */
10021 if (numargs == 1 && !(oa >> 4)
10022 && kid->op_type == OP_LIST && type != OP_SCALAR)
10024 return too_many_arguments_pv(o,PL_op_desc[type], 0);
10026 if (type != OP_DELETE) scalar(kid);
10037 if ((type == OP_PUSH || type == OP_UNSHIFT)
10038 && !OpHAS_SIBLING(kid))
10039 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
10040 "Useless use of %s with no values",
10043 if (kid->op_type == OP_CONST
10044 && ( !SvROK(cSVOPx_sv(kid))
10045 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
10047 bad_type_pv(numargs, "array", o, kid);
10048 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) {
10049 yyerror_pv(Perl_form(aTHX_ "Experimental %s on scalar is now forbidden",
10050 PL_op_desc[type]), 0);
10053 op_lvalue(kid, type);
10057 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
10058 bad_type_pv(numargs, "hash", o, kid);
10059 op_lvalue(kid, type);
10063 /* replace kid with newop in chain */
10065 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0);
10066 newop->op_next = newop;
10071 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
10072 if (kid->op_type == OP_CONST &&
10073 (kid->op_private & OPpCONST_BARE))
10075 OP * const newop = newGVOP(OP_GV, 0,
10076 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
10077 /* replace kid with newop in chain */
10078 op_sibling_splice(o, prev_kid, 1, newop);
10082 else if (kid->op_type == OP_READLINE) {
10083 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
10084 bad_type_pv(numargs, "HANDLE", o, kid);
10087 I32 flags = OPf_SPECIAL;
10089 PADOFFSET targ = 0;
10091 /* is this op a FH constructor? */
10092 if (is_handle_constructor(o,numargs)) {
10093 const char *name = NULL;
10096 bool want_dollar = TRUE;
10099 /* Set a flag to tell rv2gv to vivify
10100 * need to "prove" flag does not mean something
10101 * else already - NI-S 1999/05/07
10104 if (kid->op_type == OP_PADSV) {
10106 = PAD_COMPNAME_SV(kid->op_targ);
10107 name = PadnamePV (pn);
10108 len = PadnameLEN(pn);
10109 name_utf8 = PadnameUTF8(pn);
10111 else if (kid->op_type == OP_RV2SV
10112 && kUNOP->op_first->op_type == OP_GV)
10114 GV * const gv = cGVOPx_gv(kUNOP->op_first);
10116 len = GvNAMELEN(gv);
10117 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
10119 else if (kid->op_type == OP_AELEM
10120 || kid->op_type == OP_HELEM)
10123 OP *op = ((BINOP*)kid)->op_first;
10127 const char * const a =
10128 kid->op_type == OP_AELEM ?
10130 if (((op->op_type == OP_RV2AV) ||
10131 (op->op_type == OP_RV2HV)) &&
10132 (firstop = ((UNOP*)op)->op_first) &&
10133 (firstop->op_type == OP_GV)) {
10134 /* packagevar $a[] or $h{} */
10135 GV * const gv = cGVOPx_gv(firstop);
10138 Perl_newSVpvf(aTHX_
10143 else if (op->op_type == OP_PADAV
10144 || op->op_type == OP_PADHV) {
10145 /* lexicalvar $a[] or $h{} */
10146 const char * const padname =
10147 PAD_COMPNAME_PV(op->op_targ);
10150 Perl_newSVpvf(aTHX_
10156 name = SvPV_const(tmpstr, len);
10157 name_utf8 = SvUTF8(tmpstr);
10158 sv_2mortal(tmpstr);
10162 name = "__ANONIO__";
10164 want_dollar = FALSE;
10166 op_lvalue(kid, type);
10170 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
10171 namesv = PAD_SVl(targ);
10172 if (want_dollar && *name != '$')
10173 sv_setpvs(namesv, "$");
10176 sv_catpvn(namesv, name, len);
10177 if ( name_utf8 ) SvUTF8_on(namesv);
10181 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid,
10183 kid->op_targ = targ;
10184 kid->op_private |= priv;
10190 if ((type == OP_UNDEF || type == OP_POS)
10191 && numargs == 1 && !(oa >> 4)
10192 && kid->op_type == OP_LIST)
10193 return too_many_arguments_pv(o,PL_op_desc[type], 0);
10194 op_lvalue(scalar(kid), type);
10199 kid = OpSIBLING(kid);
10201 /* FIXME - should the numargs or-ing move after the too many
10202 * arguments check? */
10203 o->op_private |= numargs;
10205 return too_many_arguments_pv(o,OP_DESC(o), 0);
10208 else if (PL_opargs[type] & OA_DEFGV) {
10209 /* Ordering of these two is important to keep f_map.t passing. */
10211 return newUNOP(type, 0, newDEFSVOP());
10215 while (oa & OA_OPTIONAL)
10217 if (oa && oa != OA_LIST)
10218 return too_few_arguments_pv(o,OP_DESC(o), 0);
10224 Perl_ck_glob(pTHX_ OP *o)
10228 PERL_ARGS_ASSERT_CK_GLOB;
10231 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first))
10232 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
10234 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
10238 * \ null - const(wildcard)
10243 * \ mark - glob - rv2cv
10244 * | \ gv(CORE::GLOBAL::glob)
10246 * \ null - const(wildcard)
10248 o->op_flags |= OPf_SPECIAL;
10249 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
10250 o = S_new_entersubop(aTHX_ gv, o);
10251 o = newUNOP(OP_NULL, 0, o);
10252 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
10255 else o->op_flags &= ~OPf_SPECIAL;
10256 #if !defined(PERL_EXTERNAL_GLOB)
10257 if (!PL_globhook) {
10259 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
10260 newSVpvs("File::Glob"), NULL, NULL, NULL);
10263 #endif /* !PERL_EXTERNAL_GLOB */
10264 gv = (GV *)newSV(0);
10265 gv_init(gv, 0, "", 0, 0);
10267 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
10268 SvREFCNT_dec_NN(gv); /* newGVOP increased it */
10274 Perl_ck_grep(pTHX_ OP *o)
10278 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
10280 PERL_ARGS_ASSERT_CK_GREP;
10282 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
10284 if (o->op_flags & OPf_STACKED) {
10285 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first;
10286 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
10287 return no_fh_allowed(o);
10288 o->op_flags &= ~OPf_STACKED;
10290 kid = OpSIBLING(cLISTOPo->op_first);
10291 if (type == OP_MAPWHILE)
10296 if (PL_parser && PL_parser->error_count)
10298 kid = OpSIBLING(cLISTOPo->op_first);
10299 if (kid->op_type != OP_NULL)
10300 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
10301 kid = kUNOP->op_first;
10303 gwop = alloc_LOGOP(type, o, LINKLIST(kid));
10304 kid->op_next = (OP*)gwop;
10305 o->op_private = gwop->op_private = 0;
10306 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
10308 kid = OpSIBLING(cLISTOPo->op_first);
10309 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid))
10310 op_lvalue(kid, OP_GREPSTART);
10316 Perl_ck_index(pTHX_ OP *o)
10318 PERL_ARGS_ASSERT_CK_INDEX;
10320 if (o->op_flags & OPf_KIDS) {
10321 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10323 kid = OpSIBLING(kid); /* get past "big" */
10324 if (kid && kid->op_type == OP_CONST) {
10325 const bool save_taint = TAINT_get;
10326 SV *sv = kSVOP->op_sv;
10327 if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) {
10329 sv_copypv(sv, kSVOP->op_sv);
10330 SvREFCNT_dec_NN(kSVOP->op_sv);
10333 if (SvOK(sv)) fbm_compile(sv, 0);
10334 TAINT_set(save_taint);
10335 #ifdef NO_TAINT_SUPPORT
10336 PERL_UNUSED_VAR(save_taint);
10344 Perl_ck_lfun(pTHX_ OP *o)
10346 const OPCODE type = o->op_type;
10348 PERL_ARGS_ASSERT_CK_LFUN;
10350 return modkids(ck_fun(o), type);
10354 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
10356 PERL_ARGS_ASSERT_CK_DEFINED;
10358 if ((o->op_flags & OPf_KIDS)) {
10359 switch (cUNOPo->op_first->op_type) {
10362 Perl_croak(aTHX_ "Can't use 'defined(@array)'"
10363 " (Maybe you should just omit the defined()?)");
10364 NOT_REACHED; /* NOTREACHED */
10368 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'"
10369 " (Maybe you should just omit the defined()?)");
10370 NOT_REACHED; /* NOTREACHED */
10381 Perl_ck_readline(pTHX_ OP *o)
10383 PERL_ARGS_ASSERT_CK_READLINE;
10385 if (o->op_flags & OPf_KIDS) {
10386 OP *kid = cLISTOPo->op_first;
10387 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
10391 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
10399 Perl_ck_rfun(pTHX_ OP *o)
10401 const OPCODE type = o->op_type;
10403 PERL_ARGS_ASSERT_CK_RFUN;
10405 return refkids(ck_fun(o), type);
10409 Perl_ck_listiob(pTHX_ OP *o)
10413 PERL_ARGS_ASSERT_CK_LISTIOB;
10415 kid = cLISTOPo->op_first;
10417 o = force_list(o, 1);
10418 kid = cLISTOPo->op_first;
10420 if (kid->op_type == OP_PUSHMARK)
10421 kid = OpSIBLING(kid);
10422 if (kid && o->op_flags & OPf_STACKED)
10423 kid = OpSIBLING(kid);
10424 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */
10425 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
10426 && !kid->op_folded) {
10427 o->op_flags |= OPf_STACKED; /* make it a filehandle */
10429 /* replace old const op with new OP_RV2GV parent */
10430 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first,
10431 OP_RV2GV, OPf_REF);
10432 kid = OpSIBLING(kid);
10437 op_append_elem(o->op_type, o, newDEFSVOP());
10439 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
10440 return listkids(o);
10444 Perl_ck_smartmatch(pTHX_ OP *o)
10447 PERL_ARGS_ASSERT_CK_SMARTMATCH;
10448 if (0 == (o->op_flags & OPf_SPECIAL)) {
10449 OP *first = cBINOPo->op_first;
10450 OP *second = OpSIBLING(first);
10452 /* Implicitly take a reference to an array or hash */
10454 /* remove the original two siblings, then add back the
10455 * (possibly different) first and second sibs.
10457 op_sibling_splice(o, NULL, 1, NULL);
10458 op_sibling_splice(o, NULL, 1, NULL);
10459 first = ref_array_or_hash(first);
10460 second = ref_array_or_hash(second);
10461 op_sibling_splice(o, NULL, 0, second);
10462 op_sibling_splice(o, NULL, 0, first);
10464 /* Implicitly take a reference to a regular expression */
10465 if (first->op_type == OP_MATCH) {
10466 OpTYPE_set(first, OP_QR);
10468 if (second->op_type == OP_MATCH) {
10469 OpTYPE_set(second, OP_QR);
10478 S_maybe_targlex(pTHX_ OP *o)
10480 OP * const kid = cLISTOPo->op_first;
10481 /* has a disposable target? */
10482 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
10483 && !(kid->op_flags & OPf_STACKED)
10484 /* Cannot steal the second time! */
10485 && !(kid->op_private & OPpTARGET_MY)
10488 OP * const kkid = OpSIBLING(kid);
10490 /* Can just relocate the target. */
10491 if (kkid && kkid->op_type == OP_PADSV
10492 && (!(kkid->op_private & OPpLVAL_INTRO)
10493 || kkid->op_private & OPpPAD_STATE))
10495 kid->op_targ = kkid->op_targ;
10497 /* Now we do not need PADSV and SASSIGN.
10498 * Detach kid and free the rest. */
10499 op_sibling_splice(o, NULL, 1, NULL);
10501 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
10509 Perl_ck_sassign(pTHX_ OP *o)
10512 OP * const kid = cBINOPo->op_first;
10514 PERL_ARGS_ASSERT_CK_SASSIGN;
10516 if (OpHAS_SIBLING(kid)) {
10517 OP *kkid = OpSIBLING(kid);
10518 /* For state variable assignment with attributes, kkid is a list op
10519 whose op_last is a padsv. */
10520 if ((kkid->op_type == OP_PADSV ||
10521 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
10522 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
10525 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
10526 == (OPpLVAL_INTRO|OPpPAD_STATE)) {
10527 const PADOFFSET target = kkid->op_targ;
10528 OP *const other = newOP(OP_PADSV,
10530 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
10531 OP *const first = newOP(OP_NULL, 0);
10533 newCONDOP(0, first, o, other);
10534 /* XXX targlex disabled for now; see ticket #124160
10535 newCONDOP(0, first, S_maybe_targlex(aTHX_ o), other);
10537 OP *const condop = first->op_next;
10539 OpTYPE_set(condop, OP_ONCE);
10540 other->op_targ = target;
10541 nullop->op_flags |= OPf_WANT_SCALAR;
10543 /* Store the initializedness of state vars in a separate
10546 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0);
10547 /* hijacking PADSTALE for uninitialized state variables */
10548 SvPADSTALE_on(PAD_SVl(condop->op_targ));
10553 return S_maybe_targlex(aTHX_ o);
10557 Perl_ck_match(pTHX_ OP *o)
10559 PERL_UNUSED_CONTEXT;
10560 PERL_ARGS_ASSERT_CK_MATCH;
10566 Perl_ck_method(pTHX_ OP *o)
10568 SV *sv, *methsv, *rclass;
10569 const char* method;
10572 STRLEN len, nsplit = 0, i;
10574 OP * const kid = cUNOPo->op_first;
10576 PERL_ARGS_ASSERT_CK_METHOD;
10577 if (kid->op_type != OP_CONST) return o;
10581 /* replace ' with :: */
10582 while ((compatptr = strchr(SvPVX(sv), '\''))) {
10584 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1);
10587 method = SvPVX_const(sv);
10589 utf8 = SvUTF8(sv) ? -1 : 1;
10591 for (i = len - 1; i > 0; --i) if (method[i] == ':') {
10596 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0);
10598 if (!nsplit) { /* $proto->method() */
10600 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv);
10603 if (nsplit == 7 && memEQ(method, "SUPER::", nsplit)) { /* $proto->SUPER::method() */
10605 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv);
10608 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */
10609 if (nsplit >= 9 && strnEQ(method+nsplit-9, "::SUPER::", 9)) {
10610 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0);
10611 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv);
10613 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0);
10614 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv);
10616 #ifdef USE_ITHREADS
10617 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ);
10619 cMETHOPx(new_op)->op_rclass_sv = rclass;
10626 Perl_ck_null(pTHX_ OP *o)
10628 PERL_ARGS_ASSERT_CK_NULL;
10629 PERL_UNUSED_CONTEXT;
10634 Perl_ck_open(pTHX_ OP *o)
10636 PERL_ARGS_ASSERT_CK_OPEN;
10638 S_io_hints(aTHX_ o);
10640 /* In case of three-arg dup open remove strictness
10641 * from the last arg if it is a bareword. */
10642 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
10643 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
10647 if ((last->op_type == OP_CONST) && /* The bareword. */
10648 (last->op_private & OPpCONST_BARE) &&
10649 (last->op_private & OPpCONST_STRICT) &&
10650 (oa = OpSIBLING(first)) && /* The fh. */
10651 (oa = OpSIBLING(oa)) && /* The mode. */
10652 (oa->op_type == OP_CONST) &&
10653 SvPOK(((SVOP*)oa)->op_sv) &&
10654 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
10655 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
10656 (last == OpSIBLING(oa))) /* The bareword. */
10657 last->op_private &= ~OPpCONST_STRICT;
10663 Perl_ck_prototype(pTHX_ OP *o)
10665 PERL_ARGS_ASSERT_CK_PROTOTYPE;
10666 if (!(o->op_flags & OPf_KIDS)) {
10668 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP());
10674 Perl_ck_refassign(pTHX_ OP *o)
10676 OP * const right = cLISTOPo->op_first;
10677 OP * const left = OpSIBLING(right);
10678 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first;
10681 PERL_ARGS_ASSERT_CK_REFASSIGN;
10683 assert (left->op_type == OP_SREFGEN);
10686 /* we use OPpPAD_STATE in refassign to mean either of those things,
10687 * and the code assumes the two flags occupy the same bit position
10688 * in the various ops below */
10689 assert(OPpPAD_STATE == OPpOUR_INTRO);
10691 switch (varop->op_type) {
10693 o->op_private |= OPpLVREF_AV;
10696 o->op_private |= OPpLVREF_HV;
10700 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
10701 o->op_targ = varop->op_targ;
10702 varop->op_targ = 0;
10703 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
10707 o->op_private |= OPpLVREF_AV;
10709 NOT_REACHED; /* NOTREACHED */
10711 o->op_private |= OPpLVREF_HV;
10715 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO));
10716 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad;
10718 /* Point varop to its GV kid, detached. */
10719 varop = op_sibling_splice(varop, NULL, -1, NULL);
10723 OP * const kidparent =
10724 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first);
10725 OP * const kid = cUNOPx(kidparent)->op_first;
10726 o->op_private |= OPpLVREF_CV;
10727 if (kid->op_type == OP_GV) {
10729 goto detach_and_stack;
10731 if (kid->op_type != OP_PADCV) goto bad;
10732 o->op_targ = kid->op_targ;
10738 o->op_private |= (varop->op_private & OPpLVAL_INTRO);
10739 o->op_private |= OPpLVREF_ELEM;
10742 /* Detach varop. */
10743 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL);
10747 /* diag_listed_as: Can't modify reference to %s in %s assignment */
10748 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar "
10753 if (!FEATURE_REFALIASING_IS_ENABLED)
10755 "Experimental aliasing via reference not enabled");
10756 Perl_ck_warner_d(aTHX_
10757 packWARN(WARN_EXPERIMENTAL__REFALIASING),
10758 "Aliasing via reference is experimental");
10760 o->op_flags |= OPf_STACKED;
10761 op_sibling_splice(o, right, 1, varop);
10764 o->op_flags &=~ OPf_STACKED;
10765 op_sibling_splice(o, right, 1, NULL);
10772 Perl_ck_repeat(pTHX_ OP *o)
10774 PERL_ARGS_ASSERT_CK_REPEAT;
10776 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
10778 o->op_private |= OPpREPEAT_DOLIST;
10779 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */
10780 kids = force_list(kids, 1); /* promote it to a list */
10781 op_sibling_splice(o, NULL, 0, kids); /* and add back */
10789 Perl_ck_require(pTHX_ OP *o)
10793 PERL_ARGS_ASSERT_CK_REQUIRE;
10795 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
10796 SVOP * const kid = (SVOP*)cUNOPo->op_first;
10801 if (kid->op_type == OP_CONST) {
10802 SV * const sv = kid->op_sv;
10803 U32 const was_readonly = SvREADONLY(sv);
10804 if (kid->op_private & OPpCONST_BARE) {
10808 if (was_readonly) {
10809 SvREADONLY_off(sv);
10811 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
10816 /* treat ::foo::bar as foo::bar */
10817 if (len >= 2 && s[0] == ':' && s[1] == ':')
10818 DIE(aTHX_ "Bareword in require must not start with a double-colon: \"%s\"\n", s);
10820 DIE(aTHX_ "Bareword in require maps to empty filename");
10822 for (; s < end; s++) {
10823 if (*s == ':' && s[1] == ':') {
10825 Move(s+2, s+1, end - s - 1, char);
10829 SvEND_set(sv, end);
10830 sv_catpvs(sv, ".pm");
10831 PERL_HASH(hash, SvPVX(sv), SvCUR(sv));
10832 hek = share_hek(SvPVX(sv),
10833 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1),
10835 sv_sethek(sv, hek);
10837 SvFLAGS(sv) |= was_readonly;
10839 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv)
10842 if (SvREFCNT(sv) > 1) {
10843 kid->op_sv = newSVpvn_share(
10844 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0);
10845 SvREFCNT_dec_NN(sv);
10849 if (was_readonly) SvREADONLY_off(sv);
10850 PERL_HASH(hash, s, len);
10852 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len,
10854 sv_sethek(sv, hek);
10856 SvFLAGS(sv) |= was_readonly;
10862 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
10863 /* handle override, if any */
10864 && (gv = gv_override("require", 7))) {
10866 if (o->op_flags & OPf_KIDS) {
10867 kid = cUNOPo->op_first;
10868 op_sibling_splice(o, NULL, -1, NULL);
10871 kid = newDEFSVOP();
10874 newop = S_new_entersubop(aTHX_ gv, kid);
10882 Perl_ck_return(pTHX_ OP *o)
10886 PERL_ARGS_ASSERT_CK_RETURN;
10888 kid = OpSIBLING(cLISTOPo->op_first);
10889 if (CvLVALUE(PL_compcv)) {
10890 for (; kid; kid = OpSIBLING(kid))
10891 op_lvalue(kid, OP_LEAVESUBLV);
10898 Perl_ck_select(pTHX_ OP *o)
10903 PERL_ARGS_ASSERT_CK_SELECT;
10905 if (o->op_flags & OPf_KIDS) {
10906 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10907 if (kid && OpHAS_SIBLING(kid)) {
10908 OpTYPE_set(o, OP_SSELECT);
10910 return fold_constants(op_integerize(op_std_init(o)));
10914 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10915 if (kid && kid->op_type == OP_RV2GV)
10916 kid->op_private &= ~HINT_STRICT_REFS;
10921 Perl_ck_shift(pTHX_ OP *o)
10923 const I32 type = o->op_type;
10925 PERL_ARGS_ASSERT_CK_SHIFT;
10927 if (!(o->op_flags & OPf_KIDS)) {
10930 if (!CvUNIQUE(PL_compcv)) {
10931 o->op_flags |= OPf_SPECIAL;
10935 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
10937 return newUNOP(type, 0, scalar(argop));
10939 return scalar(ck_fun(o));
10943 Perl_ck_sort(pTHX_ OP *o)
10947 HV * const hinthv =
10948 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;
10951 PERL_ARGS_ASSERT_CK_SORT;
10954 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
10956 const I32 sorthints = (I32)SvIV(*svp);
10957 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
10958 o->op_private |= OPpSORT_QSORT;
10959 if ((sorthints & HINT_SORT_STABLE) != 0)
10960 o->op_private |= OPpSORT_STABLE;
10964 if (o->op_flags & OPf_STACKED)
10966 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10968 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */
10969 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
10971 /* if the first arg is a code block, process it and mark sort as
10973 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
10975 if (kid->op_type == OP_LEAVE)
10976 op_null(kid); /* wipe out leave */
10977 /* Prevent execution from escaping out of the sort block. */
10980 /* provide scalar context for comparison function/block */
10981 kid = scalar(firstkid);
10982 kid->op_next = kid;
10983 o->op_flags |= OPf_SPECIAL;
10985 else if (kid->op_type == OP_CONST
10986 && kid->op_private & OPpCONST_BARE) {
10990 const char * const name = SvPV(kSVOP_sv, len);
10992 assert (len < 256);
10993 Copy(name, tmpbuf+1, len, char);
10994 off = pad_findmy_pvn(tmpbuf, len+1, SvUTF8(kSVOP_sv));
10995 if (off != NOT_IN_PAD) {
10996 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
10998 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off)));
10999 sv_catpvs(fq, "::");
11000 sv_catsv(fq, kSVOP_sv);
11001 SvREFCNT_dec_NN(kSVOP_sv);
11005 OP * const padop = newOP(OP_PADCV, 0);
11006 padop->op_targ = off;
11007 /* replace the const op with the pad op */
11008 op_sibling_splice(firstkid, NULL, 1, padop);
11014 firstkid = OpSIBLING(firstkid);
11017 for (kid = firstkid; kid; kid = OpSIBLING(kid)) {
11018 /* provide list context for arguments */
11021 op_lvalue(kid, OP_GREPSTART);
11027 /* for sort { X } ..., where X is one of
11028 * $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a
11029 * elide the second child of the sort (the one containing X),
11030 * and set these flags as appropriate
11034 * Also, check and warn on lexical $a, $b.
11038 S_simplify_sort(pTHX_ OP *o)
11040 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
11044 const char *gvname;
11047 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
11049 kid = kUNOP->op_first; /* get past null */
11050 if (!(have_scopeop = kid->op_type == OP_SCOPE)
11051 && kid->op_type != OP_LEAVE)
11053 kid = kLISTOP->op_last; /* get past scope */
11054 switch(kid->op_type) {
11058 if (!have_scopeop) goto padkids;
11063 k = kid; /* remember this node*/
11064 if (kBINOP->op_first->op_type != OP_RV2SV
11065 || kBINOP->op_last ->op_type != OP_RV2SV)
11068 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
11069 then used in a comparison. This catches most, but not
11070 all cases. For instance, it catches
11071 sort { my($a); $a <=> $b }
11073 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
11074 (although why you'd do that is anyone's guess).
11078 if (!ckWARN(WARN_SYNTAX)) return;
11079 kid = kBINOP->op_first;
11081 if (kid->op_type == OP_PADSV) {
11082 PADNAME * const name = PAD_COMPNAME(kid->op_targ);
11083 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$'
11084 && ( PadnamePV(name)[1] == 'a'
11085 || PadnamePV(name)[1] == 'b' ))
11086 /* diag_listed_as: "my %s" used in sort comparison */
11087 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11088 "\"%s %s\" used in sort comparison",
11089 PadnameIsSTATE(name)
11094 } while ((kid = OpSIBLING(kid)));
11097 kid = kBINOP->op_first; /* get past cmp */
11098 if (kUNOP->op_first->op_type != OP_GV)
11100 kid = kUNOP->op_first; /* get past rv2sv */
11102 if (GvSTASH(gv) != PL_curstash)
11104 gvname = GvNAME(gv);
11105 if (*gvname == 'a' && gvname[1] == '\0')
11107 else if (*gvname == 'b' && gvname[1] == '\0')
11112 kid = k; /* back to cmp */
11113 /* already checked above that it is rv2sv */
11114 kid = kBINOP->op_last; /* down to 2nd arg */
11115 if (kUNOP->op_first->op_type != OP_GV)
11117 kid = kUNOP->op_first; /* get past rv2sv */
11119 if (GvSTASH(gv) != PL_curstash)
11121 gvname = GvNAME(gv);
11123 ? !(*gvname == 'a' && gvname[1] == '\0')
11124 : !(*gvname == 'b' && gvname[1] == '\0'))
11126 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
11128 o->op_private |= OPpSORT_DESCEND;
11129 if (k->op_type == OP_NCMP)
11130 o->op_private |= OPpSORT_NUMERIC;
11131 if (k->op_type == OP_I_NCMP)
11132 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
11133 kid = OpSIBLING(cLISTOPo->op_first);
11134 /* cut out and delete old block (second sibling) */
11135 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL);
11140 Perl_ck_split(pTHX_ OP *o)
11146 PERL_ARGS_ASSERT_CK_SPLIT;
11148 assert(o->op_type == OP_LIST);
11150 if (o->op_flags & OPf_STACKED)
11151 return no_fh_allowed(o);
11153 kid = cLISTOPo->op_first;
11154 /* delete leading NULL node, then add a CONST if no other nodes */
11155 assert(kid->op_type == OP_NULL);
11156 op_sibling_splice(o, NULL, 1,
11157 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" ")));
11159 kid = cLISTOPo->op_first;
11161 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
11162 /* remove match expression, and replace with new optree with
11163 * a match op at its head */
11164 op_sibling_splice(o, NULL, 1, NULL);
11165 /* pmruntime will handle split " " behavior with flag==2 */
11166 kid = pmruntime(newPMOP(OP_MATCH, 0), kid, NULL, 2, 0);
11167 op_sibling_splice(o, NULL, 0, kid);
11170 assert(kid->op_type == OP_MATCH || kid->op_type == OP_SPLIT);
11172 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
11173 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
11174 "Use of /g modifier is meaningless in split");
11177 /* eliminate the split op, and move the match op (plus any children)
11178 * into its place, then convert the match op into a split op. i.e.
11180 * SPLIT MATCH SPLIT(ex-MATCH)
11182 * MATCH - A - B - C => R - A - B - C => R - A - B - C
11188 * (R, if it exists, will be a regcomp op)
11191 op_sibling_splice(o, NULL, 1, NULL); /* detach match op from o */
11192 sibs = op_sibling_splice(o, NULL, -1, NULL); /* detach any other sibs */
11193 op_sibling_splice(kid, cLISTOPx(kid)->op_last, 0, sibs); /* and reattach */
11194 OpTYPE_set(kid, OP_SPLIT);
11195 kid->op_flags = (o->op_flags | (kid->op_flags & OPf_KIDS));
11196 kid->op_private = o->op_private;
11199 kid = sibs; /* kid is now the string arg of the split */
11202 kid = newDEFSVOP();
11203 op_append_elem(OP_SPLIT, o, kid);
11207 kid = OpSIBLING(kid);
11209 kid = newSVOP(OP_CONST, 0, newSViv(0));
11210 op_append_elem(OP_SPLIT, o, kid);
11211 o->op_private |= OPpSPLIT_IMPLIM;
11215 if (OpHAS_SIBLING(kid))
11216 return too_many_arguments_pv(o,OP_DESC(o), 0);
11222 Perl_ck_stringify(pTHX_ OP *o)
11224 OP * const kid = OpSIBLING(cUNOPo->op_first);
11225 PERL_ARGS_ASSERT_CK_STRINGIFY;
11226 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA
11227 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST
11228 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST)
11229 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */
11231 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
11239 Perl_ck_join(pTHX_ OP *o)
11241 OP * const kid = OpSIBLING(cLISTOPo->op_first);
11243 PERL_ARGS_ASSERT_CK_JOIN;
11245 if (kid && kid->op_type == OP_MATCH) {
11246 if (ckWARN(WARN_SYNTAX)) {
11247 const REGEXP *re = PM_GETRE(kPMOP);
11249 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
11250 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
11251 : newSVpvs_flags( "STRING", SVs_TEMP );
11252 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11253 "/%" SVf "/ should probably be written as \"%" SVf "\"",
11254 SVfARG(msg), SVfARG(msg));
11258 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */
11259 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO))
11260 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV
11261 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))))
11263 const OP * const bairn = OpSIBLING(kid); /* the list */
11264 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */
11265 && OP_GIMME(bairn,0) == G_SCALAR)
11267 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED,
11268 op_sibling_splice(o, kid, 1, NULL));
11278 =for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags
11280 Examines an op, which is expected to identify a subroutine at runtime,
11281 and attempts to determine at compile time which subroutine it identifies.
11282 This is normally used during Perl compilation to determine whether
11283 a prototype can be applied to a function call. C<cvop> is the op
11284 being considered, normally an C<rv2cv> op. A pointer to the identified
11285 subroutine is returned, if it could be determined statically, and a null
11286 pointer is returned if it was not possible to determine statically.
11288 Currently, the subroutine can be identified statically if the RV that the
11289 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
11290 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
11291 suitable if the constant value must be an RV pointing to a CV. Details of
11292 this process may change in future versions of Perl. If the C<rv2cv> op
11293 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
11294 the subroutine statically: this flag is used to suppress compile-time
11295 magic on a subroutine call, forcing it to use default runtime behaviour.
11297 If C<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
11298 of a GV reference is modified. If a GV was examined and its CV slot was
11299 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
11300 If the op is not optimised away, and the CV slot is later populated with
11301 a subroutine having a prototype, that flag eventually triggers the warning
11302 "called too early to check prototype".
11304 If C<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
11305 of returning a pointer to the subroutine it returns a pointer to the
11306 GV giving the most appropriate name for the subroutine in this context.
11307 Normally this is just the C<CvGV> of the subroutine, but for an anonymous
11308 (C<CvANON>) subroutine that is referenced through a GV it will be the
11309 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
11310 A null pointer is returned as usual if there is no statically-determinable
11316 /* shared by toke.c:yylex */
11318 Perl_find_lexical_cv(pTHX_ PADOFFSET off)
11320 PADNAME *name = PAD_COMPNAME(off);
11321 CV *compcv = PL_compcv;
11322 while (PadnameOUTER(name)) {
11323 assert(PARENT_PAD_INDEX(name));
11324 compcv = CvOUTSIDE(compcv);
11325 name = PadlistNAMESARRAY(CvPADLIST(compcv))
11326 [off = PARENT_PAD_INDEX(name)];
11328 assert(!PadnameIsOUR(name));
11329 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) {
11330 return PadnamePROTOCV(name);
11332 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
11336 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
11341 PERL_ARGS_ASSERT_RV2CV_OP_CV;
11342 if (flags & ~RV2CVOPCV_FLAG_MASK)
11343 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
11344 if (cvop->op_type != OP_RV2CV)
11346 if (cvop->op_private & OPpENTERSUB_AMPER)
11348 if (!(cvop->op_flags & OPf_KIDS))
11350 rvop = cUNOPx(cvop)->op_first;
11351 switch (rvop->op_type) {
11353 gv = cGVOPx_gv(rvop);
11355 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) {
11356 cv = MUTABLE_CV(SvRV(gv));
11360 if (flags & RV2CVOPCV_RETURN_STUB)
11366 if (flags & RV2CVOPCV_MARK_EARLY)
11367 rvop->op_private |= OPpEARLY_CV;
11372 SV *rv = cSVOPx_sv(rvop);
11375 cv = (CV*)SvRV(rv);
11379 cv = find_lexical_cv(rvop->op_targ);
11384 } NOT_REACHED; /* NOTREACHED */
11386 if (SvTYPE((SV*)cv) != SVt_PVCV)
11388 if (flags & (RV2CVOPCV_RETURN_NAME_GV|RV2CVOPCV_MAYBE_NAME_GV)) {
11389 if ((!CvANON(cv) || !gv) && !CvLEXICAL(cv)
11390 && ((flags & RV2CVOPCV_RETURN_NAME_GV) || !CvNAMED(cv)))
11399 =for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop
11401 Performs the default fixup of the arguments part of an C<entersub>
11402 op tree. This consists of applying list context to each of the
11403 argument ops. This is the standard treatment used on a call marked
11404 with C<&>, or a method call, or a call through a subroutine reference,
11405 or any other call where the callee can't be identified at compile time,
11406 or a call where the callee has no prototype.
11412 Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
11416 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
11418 aop = cUNOPx(entersubop)->op_first;
11419 if (!OpHAS_SIBLING(aop))
11420 aop = cUNOPx(aop)->op_first;
11421 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) {
11422 /* skip the extra attributes->import() call implicitly added in
11423 * something like foo(my $x : bar)
11425 if ( aop->op_type == OP_ENTERSUB
11426 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID
11430 op_lvalue(aop, OP_ENTERSUB);
11436 =for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv
11438 Performs the fixup of the arguments part of an C<entersub> op tree
11439 based on a subroutine prototype. This makes various modifications to
11440 the argument ops, from applying context up to inserting C<refgen> ops,
11441 and checking the number and syntactic types of arguments, as directed by
11442 the prototype. This is the standard treatment used on a subroutine call,
11443 not marked with C<&>, where the callee can be identified at compile time
11444 and has a prototype.
11446 C<protosv> supplies the subroutine prototype to be applied to the call.
11447 It may be a normal defined scalar, of which the string value will be used.
11448 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
11449 that has been cast to C<SV*>) which has a prototype. The prototype
11450 supplied, in whichever form, does not need to match the actual callee
11451 referenced by the op tree.
11453 If the argument ops disagree with the prototype, for example by having
11454 an unacceptable number of arguments, a valid op tree is returned anyway.
11455 The error is reflected in the parser state, normally resulting in a single
11456 exception at the top level of parsing which covers all the compilation
11457 errors that occurred. In the error message, the callee is referred to
11458 by the name defined by the C<namegv> parameter.
11464 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
11467 const char *proto, *proto_end;
11468 OP *aop, *prev, *cvop, *parent;
11471 I32 contextclass = 0;
11472 const char *e = NULL;
11473 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
11474 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
11475 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
11476 "flags=%lx", (unsigned long) SvFLAGS(protosv));
11477 if (SvTYPE(protosv) == SVt_PVCV)
11478 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
11479 else proto = SvPV(protosv, proto_len);
11480 proto = S_strip_spaces(aTHX_ proto, &proto_len);
11481 proto_end = proto + proto_len;
11482 parent = entersubop;
11483 aop = cUNOPx(entersubop)->op_first;
11484 if (!OpHAS_SIBLING(aop)) {
11486 aop = cUNOPx(aop)->op_first;
11489 aop = OpSIBLING(aop);
11490 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11491 while (aop != cvop) {
11494 if (proto >= proto_end)
11496 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
11497 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
11498 SVfARG(namesv)), SvUTF8(namesv));
11508 /* _ must be at the end */
11509 if (proto[1] && !strchr(";@%", proto[1]))
11525 if ( o3->op_type != OP_UNDEF
11526 && (o3->op_type != OP_SREFGEN
11527 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
11529 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
11531 bad_type_gv(arg, namegv, o3,
11532 arg == 1 ? "block or sub {}" : "sub {}");
11535 /* '*' allows any scalar type, including bareword */
11538 if (o3->op_type == OP_RV2GV)
11539 goto wrapref; /* autoconvert GLOB -> GLOBref */
11540 else if (o3->op_type == OP_CONST)
11541 o3->op_private &= ~OPpCONST_STRICT;
11547 if (o3->op_type == OP_RV2AV ||
11548 o3->op_type == OP_PADAV ||
11549 o3->op_type == OP_RV2HV ||
11550 o3->op_type == OP_PADHV
11556 case '[': case ']':
11563 switch (*proto++) {
11565 if (contextclass++ == 0) {
11566 e = strchr(proto, ']');
11567 if (!e || e == proto)
11575 if (contextclass) {
11576 const char *p = proto;
11577 const char *const end = proto;
11579 while (*--p != '[')
11580 /* \[$] accepts any scalar lvalue */
11582 && Perl_op_lvalue_flags(aTHX_
11584 OP_READ, /* not entersub */
11587 bad_type_gv(arg, namegv, o3,
11588 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p));
11593 if (o3->op_type == OP_RV2GV)
11596 bad_type_gv(arg, namegv, o3, "symbol");
11599 if (o3->op_type == OP_ENTERSUB
11600 && !(o3->op_flags & OPf_STACKED))
11603 bad_type_gv(arg, namegv, o3, "subroutine");
11606 if (o3->op_type == OP_RV2SV ||
11607 o3->op_type == OP_PADSV ||
11608 o3->op_type == OP_HELEM ||
11609 o3->op_type == OP_AELEM)
11611 if (!contextclass) {
11612 /* \$ accepts any scalar lvalue */
11613 if (Perl_op_lvalue_flags(aTHX_
11615 OP_READ, /* not entersub */
11618 bad_type_gv(arg, namegv, o3, "scalar");
11622 if (o3->op_type == OP_RV2AV ||
11623 o3->op_type == OP_PADAV)
11625 o3->op_flags &=~ OPf_PARENS;
11629 bad_type_gv(arg, namegv, o3, "array");
11632 if (o3->op_type == OP_RV2HV ||
11633 o3->op_type == OP_PADHV)
11635 o3->op_flags &=~ OPf_PARENS;
11639 bad_type_gv(arg, namegv, o3, "hash");
11642 aop = S_op_sibling_newUNOP(aTHX_ parent, prev,
11644 if (contextclass && e) {
11649 default: goto oops;
11659 Perl_croak(aTHX_ "Malformed prototype for %" SVf ": %" SVf,
11660 SVfARG(cv_name((CV *)namegv, NULL, 0)),
11665 op_lvalue(aop, OP_ENTERSUB);
11667 aop = OpSIBLING(aop);
11669 if (aop == cvop && *proto == '_') {
11670 /* generate an access to $_ */
11671 op_sibling_splice(parent, prev, 0, newDEFSVOP());
11673 if (!optional && proto_end > proto &&
11674 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
11676 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
11677 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %" SVf,
11678 SVfARG(namesv)), SvUTF8(namesv));
11684 =for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv
11686 Performs the fixup of the arguments part of an C<entersub> op tree either
11687 based on a subroutine prototype or using default list-context processing.
11688 This is the standard treatment used on a subroutine call, not marked
11689 with C<&>, where the callee can be identified at compile time.
11691 C<protosv> supplies the subroutine prototype to be applied to the call,
11692 or indicates that there is no prototype. It may be a normal scalar,
11693 in which case if it is defined then the string value will be used
11694 as a prototype, and if it is undefined then there is no prototype.
11695 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
11696 that has been cast to C<SV*>), of which the prototype will be used if it
11697 has one. The prototype (or lack thereof) supplied, in whichever form,
11698 does not need to match the actual callee referenced by the op tree.
11700 If the argument ops disagree with the prototype, for example by having
11701 an unacceptable number of arguments, a valid op tree is returned anyway.
11702 The error is reflected in the parser state, normally resulting in a single
11703 exception at the top level of parsing which covers all the compilation
11704 errors that occurred. In the error message, the callee is referred to
11705 by the name defined by the C<namegv> parameter.
11711 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
11712 GV *namegv, SV *protosv)
11714 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
11715 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
11716 return ck_entersub_args_proto(entersubop, namegv, protosv);
11718 return ck_entersub_args_list(entersubop);
11722 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
11724 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv);
11725 OP *aop = cUNOPx(entersubop)->op_first;
11727 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
11731 if (!OpHAS_SIBLING(aop))
11732 aop = cUNOPx(aop)->op_first;
11733 aop = OpSIBLING(aop);
11734 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11736 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0);
11738 op_free(entersubop);
11739 switch(GvNAME(namegv)[2]) {
11740 case 'F': return newSVOP(OP_CONST, 0,
11741 newSVpv(CopFILE(PL_curcop),0));
11742 case 'L': return newSVOP(
11744 Perl_newSVpvf(aTHX_
11745 "%" IVdf, (IV)CopLINE(PL_curcop)
11748 case 'P': return newSVOP(OP_CONST, 0,
11750 ? newSVhek(HvNAME_HEK(PL_curstash))
11755 NOT_REACHED; /* NOTREACHED */
11758 OP *prev, *cvop, *first, *parent;
11761 parent = entersubop;
11762 if (!OpHAS_SIBLING(aop)) {
11764 aop = cUNOPx(aop)->op_first;
11767 first = prev = aop;
11768 aop = OpSIBLING(aop);
11769 /* find last sibling */
11771 OpHAS_SIBLING(cvop);
11772 prev = cvop, cvop = OpSIBLING(cvop))
11774 if (!(cvop->op_private & OPpENTERSUB_NOPAREN)
11775 /* Usually, OPf_SPECIAL on an op with no args means that it had
11776 * parens, but these have their own meaning for that flag: */
11777 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH
11778 && opnum != OP_DELETE && opnum != OP_EXISTS)
11779 flags |= OPf_SPECIAL;
11780 /* excise cvop from end of sibling chain */
11781 op_sibling_splice(parent, prev, 1, NULL);
11783 if (aop == cvop) aop = NULL;
11785 /* detach remaining siblings from the first sibling, then
11786 * dispose of original optree */
11789 op_sibling_splice(parent, first, -1, NULL);
11790 op_free(entersubop);
11792 if (opnum == OP_ENTEREVAL
11793 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9))
11794 flags |= OPpEVAL_BYTES <<8;
11796 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
11798 case OA_BASEOP_OR_UNOP:
11799 case OA_FILESTATOP:
11800 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags);
11803 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0);
11806 return opnum == OP_RUNCV
11807 ? newPVOP(OP_RUNCV,0,NULL)
11810 return op_convert_list(opnum,0,aop);
11813 NOT_REACHED; /* NOTREACHED */
11818 =for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p
11820 Retrieves the function that will be used to fix up a call to C<cv>.
11821 Specifically, the function is applied to an C<entersub> op tree for a
11822 subroutine call, not marked with C<&>, where the callee can be identified
11823 at compile time as C<cv>.
11825 The C-level function pointer is returned in C<*ckfun_p>, and an SV
11826 argument for it is returned in C<*ckobj_p>. The function is intended
11827 to be called in this manner:
11829 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
11831 In this call, C<entersubop> is a pointer to the C<entersub> op,
11832 which may be replaced by the check function, and C<namegv> is a GV
11833 supplying the name that should be used by the check function to refer
11834 to the callee of the C<entersub> op if it needs to emit any diagnostics.
11835 It is permitted to apply the check function in non-standard situations,
11836 such as to a call to a different subroutine or to a method call.
11838 By default, the function is
11839 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
11840 and the SV parameter is C<cv> itself. This implements standard
11841 prototype processing. It can be changed, for a particular subroutine,
11842 by L</cv_set_call_checker>.
11848 S_cv_get_call_checker(CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p,
11852 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
11854 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
11855 *ckobj_p = callmg->mg_obj;
11856 if (flagsp) *flagsp = callmg->mg_flags;
11858 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
11859 *ckobj_p = (SV*)cv;
11860 if (flagsp) *flagsp = 0;
11865 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
11867 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
11868 PERL_UNUSED_CONTEXT;
11869 S_cv_get_call_checker(cv, ckfun_p, ckobj_p, NULL);
11873 =for apidoc Am|void|cv_set_call_checker_flags|CV *cv|Perl_call_checker ckfun|SV *ckobj|U32 flags
11875 Sets the function that will be used to fix up a call to C<cv>.
11876 Specifically, the function is applied to an C<entersub> op tree for a
11877 subroutine call, not marked with C<&>, where the callee can be identified
11878 at compile time as C<cv>.
11880 The C-level function pointer is supplied in C<ckfun>, and an SV argument
11881 for it is supplied in C<ckobj>. The function should be defined like this:
11883 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
11885 It is intended to be called in this manner:
11887 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
11889 In this call, C<entersubop> is a pointer to the C<entersub> op,
11890 which may be replaced by the check function, and C<namegv> supplies
11891 the name that should be used by the check function to refer
11892 to the callee of the C<entersub> op if it needs to emit any diagnostics.
11893 It is permitted to apply the check function in non-standard situations,
11894 such as to a call to a different subroutine or to a method call.
11896 C<namegv> may not actually be a GV. For efficiency, perl may pass a
11897 CV or other SV instead. Whatever is passed can be used as the first
11898 argument to L</cv_name>. You can force perl to pass a GV by including
11899 C<CALL_CHECKER_REQUIRE_GV> in the C<flags>.
11901 The current setting for a particular CV can be retrieved by
11902 L</cv_get_call_checker>.
11904 =for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj
11906 The original form of L</cv_set_call_checker_flags>, which passes it the
11907 C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility.
11913 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
11915 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
11916 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV);
11920 Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun,
11921 SV *ckobj, U32 flags)
11923 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS;
11924 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
11925 if (SvMAGICAL((SV*)cv))
11926 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
11929 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
11930 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
11932 if (callmg->mg_flags & MGf_REFCOUNTED) {
11933 SvREFCNT_dec(callmg->mg_obj);
11934 callmg->mg_flags &= ~MGf_REFCOUNTED;
11936 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
11937 callmg->mg_obj = ckobj;
11938 if (ckobj != (SV*)cv) {
11939 SvREFCNT_inc_simple_void_NN(ckobj);
11940 callmg->mg_flags |= MGf_REFCOUNTED;
11942 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV)
11943 | (U8)(flags & MGf_REQUIRE_GV) | MGf_COPY;
11948 S_entersub_alloc_targ(pTHX_ OP * const o)
11950 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP);
11951 o->op_private |= OPpENTERSUB_HASTARG;
11955 Perl_ck_subr(pTHX_ OP *o)
11960 SV **const_class = NULL;
11962 PERL_ARGS_ASSERT_CK_SUBR;
11964 aop = cUNOPx(o)->op_first;
11965 if (!OpHAS_SIBLING(aop))
11966 aop = cUNOPx(aop)->op_first;
11967 aop = OpSIBLING(aop);
11968 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11969 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
11970 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL;
11972 o->op_private &= ~1;
11973 o->op_private |= (PL_hints & HINT_STRICT_REFS);
11974 if (PERLDB_SUB && PL_curstash != PL_debstash)
11975 o->op_private |= OPpENTERSUB_DB;
11976 switch (cvop->op_type) {
11978 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
11982 case OP_METHOD_NAMED:
11983 case OP_METHOD_SUPER:
11984 case OP_METHOD_REDIR:
11985 case OP_METHOD_REDIR_SUPER:
11986 o->op_flags |= OPf_REF;
11987 if (aop->op_type == OP_CONST) {
11988 aop->op_private &= ~OPpCONST_STRICT;
11989 const_class = &cSVOPx(aop)->op_sv;
11991 else if (aop->op_type == OP_LIST) {
11992 OP * const sib = OpSIBLING(((UNOP*)aop)->op_first);
11993 if (sib && sib->op_type == OP_CONST) {
11994 sib->op_private &= ~OPpCONST_STRICT;
11995 const_class = &cSVOPx(sib)->op_sv;
11998 /* make class name a shared cow string to speedup method calls */
11999 /* constant string might be replaced with object, f.e. bigint */
12000 if (const_class && SvPOK(*const_class)) {
12002 const char* str = SvPV(*const_class, len);
12004 SV* const shared = newSVpvn_share(
12005 str, SvUTF8(*const_class)
12006 ? -(SSize_t)len : (SSize_t)len,
12009 if (SvREADONLY(*const_class))
12010 SvREADONLY_on(shared);
12011 SvREFCNT_dec(*const_class);
12012 *const_class = shared;
12019 S_entersub_alloc_targ(aTHX_ o);
12020 return ck_entersub_args_list(o);
12022 Perl_call_checker ckfun;
12025 S_cv_get_call_checker(cv, &ckfun, &ckobj, &flags);
12026 if (CvISXSUB(cv) || !CvROOT(cv))
12027 S_entersub_alloc_targ(aTHX_ o);
12029 /* The original call checker API guarantees that a GV will be
12030 be provided with the right name. So, if the old API was
12031 used (or the REQUIRE_GV flag was passed), we have to reify
12032 the CV’s GV, unless this is an anonymous sub. This is not
12033 ideal for lexical subs, as its stringification will include
12034 the package. But it is the best we can do. */
12035 if (flags & MGf_REQUIRE_GV) {
12036 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv)))
12039 else namegv = MUTABLE_GV(cv);
12040 /* After a syntax error in a lexical sub, the cv that
12041 rv2cv_op_cv returns may be a nameless stub. */
12042 if (!namegv) return ck_entersub_args_list(o);
12045 return ckfun(aTHX_ o, namegv, ckobj);
12050 Perl_ck_svconst(pTHX_ OP *o)
12052 SV * const sv = cSVOPo->op_sv;
12053 PERL_ARGS_ASSERT_CK_SVCONST;
12054 PERL_UNUSED_CONTEXT;
12055 #ifdef PERL_COPY_ON_WRITE
12056 /* Since the read-only flag may be used to protect a string buffer, we
12057 cannot do copy-on-write with existing read-only scalars that are not
12058 already copy-on-write scalars. To allow $_ = "hello" to do COW with
12059 that constant, mark the constant as COWable here, if it is not
12060 already read-only. */
12061 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
12064 # ifdef PERL_DEBUG_READONLY_COW
12074 Perl_ck_trunc(pTHX_ OP *o)
12076 PERL_ARGS_ASSERT_CK_TRUNC;
12078 if (o->op_flags & OPf_KIDS) {
12079 SVOP *kid = (SVOP*)cUNOPo->op_first;
12081 if (kid->op_type == OP_NULL)
12082 kid = (SVOP*)OpSIBLING(kid);
12083 if (kid && kid->op_type == OP_CONST &&
12084 (kid->op_private & OPpCONST_BARE) &&
12087 o->op_flags |= OPf_SPECIAL;
12088 kid->op_private &= ~OPpCONST_STRICT;
12095 Perl_ck_substr(pTHX_ OP *o)
12097 PERL_ARGS_ASSERT_CK_SUBSTR;
12100 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
12101 OP *kid = cLISTOPo->op_first;
12103 if (kid->op_type == OP_NULL)
12104 kid = OpSIBLING(kid);
12106 kid->op_flags |= OPf_MOD;
12113 Perl_ck_tell(pTHX_ OP *o)
12115 PERL_ARGS_ASSERT_CK_TELL;
12117 if (o->op_flags & OPf_KIDS) {
12118 OP *kid = cLISTOPo->op_first;
12119 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid);
12120 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
12126 Perl_ck_each(pTHX_ OP *o)
12129 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
12130 const unsigned orig_type = o->op_type;
12132 PERL_ARGS_ASSERT_CK_EACH;
12135 switch (kid->op_type) {
12141 OpTYPE_set(o, orig_type == OP_EACH ? OP_AEACH
12142 : orig_type == OP_KEYS ? OP_AKEYS
12146 if (kid->op_private == OPpCONST_BARE
12147 || !SvROK(cSVOPx_sv(kid))
12148 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
12149 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
12154 qerror(Perl_mess(aTHX_
12155 "Experimental %s on scalar is now forbidden",
12156 PL_op_desc[orig_type]));
12158 bad_type_pv(1, "hash or array", o, kid);
12166 Perl_ck_length(pTHX_ OP *o)
12168 PERL_ARGS_ASSERT_CK_LENGTH;
12172 if (ckWARN(WARN_SYNTAX)) {
12173 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
12177 const bool hash = kid->op_type == OP_PADHV
12178 || kid->op_type == OP_RV2HV;
12179 switch (kid->op_type) {
12184 name = S_op_varname(aTHX_ kid);
12190 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
12191 "length() used on %" SVf " (did you mean \"scalar(%s%" SVf
12193 SVfARG(name), hash ? "keys " : "", SVfARG(name)
12196 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
12197 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
12198 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
12200 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
12201 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
12202 "length() used on @array (did you mean \"scalar(@array)\"?)");
12212 ---------------------------------------------------------
12214 Common vars in list assignment
12216 There now follows some enums and static functions for detecting
12217 common variables in list assignments. Here is a little essay I wrote
12218 for myself when trying to get my head around this. DAPM.
12222 First some random observations:
12224 * If a lexical var is an alias of something else, e.g.
12225 for my $x ($lex, $pkg, $a[0]) {...}
12226 then the act of aliasing will increase the reference count of the SV
12228 * If a package var is an alias of something else, it may still have a
12229 reference count of 1, depending on how the alias was created, e.g.
12230 in *a = *b, $a may have a refcount of 1 since the GP is shared
12231 with a single GvSV pointer to the SV. So If it's an alias of another
12232 package var, then RC may be 1; if it's an alias of another scalar, e.g.
12233 a lexical var or an array element, then it will have RC > 1.
12235 * There are many ways to create a package alias; ultimately, XS code
12236 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so
12237 run-time tracing mechanisms are unlikely to be able to catch all cases.
12239 * When the LHS is all my declarations, the same vars can't appear directly
12240 on the RHS, but they can indirectly via closures, aliasing and lvalue
12241 subs. But those techniques all involve an increase in the lexical
12242 scalar's ref count.
12244 * When the LHS is all lexical vars (but not necessarily my declarations),
12245 it is possible for the same lexicals to appear directly on the RHS, and
12246 without an increased ref count, since the stack isn't refcounted.
12247 This case can be detected at compile time by scanning for common lex
12248 vars with PL_generation.
12250 * lvalue subs defeat common var detection, but they do at least
12251 return vars with a temporary ref count increment. Also, you can't
12252 tell at compile time whether a sub call is lvalue.
12257 A: There are a few circumstances where there definitely can't be any
12260 LHS empty: () = (...);
12261 RHS empty: (....) = ();
12262 RHS contains only constants or other 'can't possibly be shared'
12263 elements (e.g. ops that return PADTMPs): (...) = (1,2, length)
12264 i.e. they only contain ops not marked as dangerous, whose children
12265 are also not dangerous;
12267 LHS contains a single scalar element: e.g. ($x) = (....); because
12268 after $x has been modified, it won't be used again on the RHS;
12269 RHS contains a single element with no aggregate on LHS: e.g.
12270 ($a,$b,$c) = ($x); again, once $a has been modified, its value
12271 won't be used again.
12273 B: If LHS are all 'my' lexical var declarations (or safe ops, which
12276 my ($a, $b, @c) = ...;
12278 Due to closure and goto tricks, these vars may already have content.
12279 For the same reason, an element on the RHS may be a lexical or package
12280 alias of one of the vars on the left, or share common elements, for
12283 my ($x,$y) = f(); # $x and $y on both sides
12284 sub f : lvalue { ($x,$y) = (1,2); $y, $x }
12289 my @a = @$ra; # elements of @a on both sides
12290 sub f { @a = 1..4; \@a }
12293 First, just consider scalar vars on LHS:
12295 RHS is safe only if (A), or in addition,
12296 * contains only lexical *scalar* vars, where neither side's
12297 lexicals have been flagged as aliases
12299 If RHS is not safe, then it's always legal to check LHS vars for
12300 RC==1, since the only RHS aliases will always be associated
12303 Note that in particular, RHS is not safe if:
12305 * it contains package scalar vars; e.g.:
12308 my ($x, $y) = (2, $x_alias);
12309 sub f { $x = 1; *x_alias = \$x; }
12311 * It contains other general elements, such as flattened or
12312 * spliced or single array or hash elements, e.g.
12315 my ($x,$y) = @a; # or $a[0] or @a{@b} etc
12319 use feature 'refaliasing';
12320 \($a[0], $a[1]) = \($y,$x);
12323 It doesn't matter if the array/hash is lexical or package.
12325 * it contains a function call that happens to be an lvalue
12326 sub which returns one or more of the above, e.g.
12337 (so a sub call on the RHS should be treated the same
12338 as having a package var on the RHS).
12340 * any other "dangerous" thing, such an op or built-in that
12341 returns one of the above, e.g. pp_preinc
12344 If RHS is not safe, what we can do however is at compile time flag
12345 that the LHS are all my declarations, and at run time check whether
12346 all the LHS have RC == 1, and if so skip the full scan.
12348 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...;
12350 Here the issue is whether there can be elements of @a on the RHS
12351 which will get prematurely freed when @a is cleared prior to
12352 assignment. This is only a problem if the aliasing mechanism
12353 is one which doesn't increase the refcount - only if RC == 1
12354 will the RHS element be prematurely freed.
12356 Because the array/hash is being INTROed, it or its elements
12357 can't directly appear on the RHS:
12359 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE
12361 but can indirectly, e.g.:
12365 sub f { @a = 1..3; \@a }
12367 So if the RHS isn't safe as defined by (A), we must always
12368 mortalise and bump the ref count of any remaining RHS elements
12369 when assigning to a non-empty LHS aggregate.
12371 Lexical scalars on the RHS aren't safe if they've been involved in
12374 use feature 'refaliasing';
12377 \(my $lex) = \$pkg;
12378 my @a = ($lex,3); # equivalent to ($a[0],3)
12385 Similarly with lexical arrays and hashes on the RHS:
12399 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g.
12400 my $a; ($a, my $b) = (....);
12402 The difference between (B) and (C) is that it is now physically
12403 possible for the LHS vars to appear on the RHS too, where they
12404 are not reference counted; but in this case, the compile-time
12405 PL_generation sweep will detect such common vars.
12407 So the rules for (C) differ from (B) in that if common vars are
12408 detected, the runtime "test RC==1" optimisation can no longer be used,
12409 and a full mark and sweep is required
12411 D: As (C), but in addition the LHS may contain package vars.
12413 Since package vars can be aliased without a corresponding refcount
12414 increase, all bets are off. It's only safe if (A). E.g.
12416 my ($x, $y) = (1,2);
12418 for $x_alias ($x) {
12419 ($x_alias, $y) = (3, $x); # whoops
12422 Ditto for LHS aggregate package vars.
12424 E: Any other dangerous ops on LHS, e.g.
12425 (f(), $a[0], @$r) = (...);
12427 this is similar to (E) in that all bets are off. In addition, it's
12428 impossible to determine at compile time whether the LHS
12429 contains a scalar or an aggregate, e.g.
12431 sub f : lvalue { @a }
12434 * ---------------------------------------------------------
12438 /* A set of bit flags returned by S_aassign_scan(). Each flag indicates
12439 * that at least one of the things flagged was seen.
12443 AAS_MY_SCALAR = 0x001, /* my $scalar */
12444 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */
12445 AAS_LEX_SCALAR = 0x004, /* $lexical */
12446 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */
12447 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */
12448 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */
12449 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */
12450 AAS_DANGEROUS = 0x080, /* an op (other than the above)
12451 that's flagged OA_DANGEROUS */
12452 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's
12453 not in any of the categories above */
12454 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */
12459 /* helper function for S_aassign_scan().
12460 * check a PAD-related op for commonality and/or set its generation number.
12461 * Returns a boolean indicating whether its shared */
12464 S_aassign_padcheck(pTHX_ OP* o, bool rhs)
12466 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX)
12467 /* lexical used in aliasing */
12471 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation);
12473 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation);
12480 Helper function for OPpASSIGN_COMMON* detection in rpeep().
12481 It scans the left or right hand subtree of the aassign op, and returns a
12482 set of flags indicating what sorts of things it found there.
12483 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we
12484 set PL_generation on lexical vars; if the latter, we see if
12485 PL_generation matches.
12486 'top' indicates whether we're recursing or at the top level.
12487 'scalars_p' is a pointer to a counter of the number of scalar SVs seen.
12488 This fn will increment it by the number seen. It's not intended to
12489 be an accurate count (especially as many ops can push a variable
12490 number of SVs onto the stack); rather it's used as to test whether there
12491 can be at most 1 SV pushed; so it's only meanings are "0, 1, many".
12495 S_aassign_scan(pTHX_ OP* o, bool rhs, bool top, int *scalars_p)
12498 bool kid_top = FALSE;
12500 /* first, look for a solitary @_ on the RHS */
12503 && (o->op_flags & OPf_KIDS)
12504 && OP_TYPE_IS_OR_WAS(o, OP_LIST)
12506 OP *kid = cUNOPo->op_first;
12507 if ( ( kid->op_type == OP_PUSHMARK
12508 || kid->op_type == OP_PADRANGE) /* ex-pushmark */
12509 && ((kid = OpSIBLING(kid)))
12510 && !OpHAS_SIBLING(kid)
12511 && kid->op_type == OP_RV2AV
12512 && !(kid->op_flags & OPf_REF)
12513 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
12514 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST)
12515 && ((kid = cUNOPx(kid)->op_first))
12516 && kid->op_type == OP_GV
12517 && cGVOPx_gv(kid) == PL_defgv
12519 flags |= AAS_DEFAV;
12522 switch (o->op_type) {
12525 return AAS_PKG_SCALAR;
12530 /* if !top, could be e.g. @a[0,1] */
12531 if (top && (o->op_flags & OPf_REF))
12532 return (o->op_private & OPpLVAL_INTRO)
12533 ? AAS_MY_AGG : AAS_LEX_AGG;
12534 return AAS_DANGEROUS;
12538 int comm = S_aassign_padcheck(aTHX_ o, rhs)
12539 ? AAS_LEX_SCALAR_COMM : 0;
12541 return (o->op_private & OPpLVAL_INTRO)
12542 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm);
12548 if (cUNOPx(o)->op_first->op_type != OP_GV)
12549 return AAS_DANGEROUS; /* @{expr}, %{expr} */
12551 /* if !top, could be e.g. @a[0,1] */
12552 if (top && (o->op_flags & OPf_REF))
12553 return AAS_PKG_AGG;
12554 return AAS_DANGEROUS;
12558 if (cUNOPx(o)->op_first->op_type != OP_GV) {
12560 return AAS_DANGEROUS; /* ${expr} */
12562 return AAS_PKG_SCALAR; /* $pkg */
12565 if (o->op_private & OPpSPLIT_ASSIGN) {
12566 /* the assign in @a = split() has been optimised away
12567 * and the @a attached directly to the split op
12568 * Treat the array as appearing on the RHS, i.e.
12569 * ... = (@a = split)
12574 if (o->op_flags & OPf_STACKED)
12575 /* @{expr} = split() - the array expression is tacked
12576 * on as an extra child to split - process kid */
12577 return S_aassign_scan(aTHX_ cLISTOPo->op_last, rhs,
12580 /* ... else array is directly attached to split op */
12582 if (PL_op->op_private & OPpSPLIT_LEX)
12583 return (o->op_private & OPpLVAL_INTRO)
12584 ? AAS_MY_AGG : AAS_LEX_AGG;
12586 return AAS_PKG_AGG;
12589 /* other args of split can't be returned */
12590 return AAS_SAFE_SCALAR;
12593 /* undef counts as a scalar on the RHS:
12594 * (undef, $x) = ...; # only 1 scalar on LHS: always safe
12595 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe
12599 flags = AAS_SAFE_SCALAR;
12604 /* these are all no-ops; they don't push a potentially common SV
12605 * onto the stack, so they are neither AAS_DANGEROUS nor
12606 * AAS_SAFE_SCALAR */
12609 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */
12614 /* these do nothing but may have children; but their children
12615 * should also be treated as top-level */
12620 if (PL_opargs[o->op_type] & OA_DANGEROUS) {
12622 flags = AAS_DANGEROUS;
12626 if ( (PL_opargs[o->op_type] & OA_TARGLEX)
12627 && (o->op_private & OPpTARGET_MY))
12630 return S_aassign_padcheck(aTHX_ o, rhs)
12631 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR;
12634 /* if its an unrecognised, non-dangerous op, assume that it
12635 * it the cause of at least one safe scalar */
12637 flags = AAS_SAFE_SCALAR;
12641 /* XXX this assumes that all other ops are "transparent" - i.e. that
12642 * they can return some of their children. While this true for e.g.
12643 * sort and grep, it's not true for e.g. map. We really need a
12644 * 'transparent' flag added to regen/opcodes
12646 if (o->op_flags & OPf_KIDS) {
12648 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
12649 flags |= S_aassign_scan(aTHX_ kid, rhs, kid_top, scalars_p);
12655 /* Check for in place reverse and sort assignments like "@a = reverse @a"
12656 and modify the optree to make them work inplace */
12659 S_inplace_aassign(pTHX_ OP *o) {
12661 OP *modop, *modop_pushmark;
12663 OP *oleft, *oleft_pushmark;
12665 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
12667 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
12669 assert(cUNOPo->op_first->op_type == OP_NULL);
12670 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
12671 assert(modop_pushmark->op_type == OP_PUSHMARK);
12672 modop = OpSIBLING(modop_pushmark);
12674 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
12677 /* no other operation except sort/reverse */
12678 if (OpHAS_SIBLING(modop))
12681 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
12682 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return;
12684 if (modop->op_flags & OPf_STACKED) {
12685 /* skip sort subroutine/block */
12686 assert(oright->op_type == OP_NULL);
12687 oright = OpSIBLING(oright);
12690 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL);
12691 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first;
12692 assert(oleft_pushmark->op_type == OP_PUSHMARK);
12693 oleft = OpSIBLING(oleft_pushmark);
12695 /* Check the lhs is an array */
12697 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
12698 || OpHAS_SIBLING(oleft)
12699 || (oleft->op_private & OPpLVAL_INTRO)
12703 /* Only one thing on the rhs */
12704 if (OpHAS_SIBLING(oright))
12707 /* check the array is the same on both sides */
12708 if (oleft->op_type == OP_RV2AV) {
12709 if (oright->op_type != OP_RV2AV
12710 || !cUNOPx(oright)->op_first
12711 || cUNOPx(oright)->op_first->op_type != OP_GV
12712 || cUNOPx(oleft )->op_first->op_type != OP_GV
12713 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
12714 cGVOPx_gv(cUNOPx(oright)->op_first)
12718 else if (oright->op_type != OP_PADAV
12719 || oright->op_targ != oleft->op_targ
12723 /* This actually is an inplace assignment */
12725 modop->op_private |= OPpSORT_INPLACE;
12727 /* transfer MODishness etc from LHS arg to RHS arg */
12728 oright->op_flags = oleft->op_flags;
12730 /* remove the aassign op and the lhs */
12732 op_null(oleft_pushmark);
12733 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
12734 op_null(cUNOPx(oleft)->op_first);
12740 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start'
12741 * that potentially represent a series of one or more aggregate derefs
12742 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert
12743 * the whole chain to a single OP_MULTIDEREF op (maybe with a few
12744 * additional ops left in too).
12746 * The caller will have already verified that the first few ops in the
12747 * chain following 'start' indicate a multideref candidate, and will have
12748 * set 'orig_o' to the point further on in the chain where the first index
12749 * expression (if any) begins. 'orig_action' specifies what type of
12750 * beginning has already been determined by the ops between start..orig_o
12751 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc).
12753 * 'hints' contains any hints flags that need adding (currently just
12754 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller.
12758 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints)
12762 UNOP_AUX_item *arg_buf = NULL;
12763 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */
12764 int index_skip = -1; /* don't output index arg on this action */
12766 /* similar to regex compiling, do two passes; the first pass
12767 * determines whether the op chain is convertible and calculates the
12768 * buffer size; the second pass populates the buffer and makes any
12769 * changes necessary to ops (such as moving consts to the pad on
12770 * threaded builds).
12772 * NB: for things like Coverity, note that both passes take the same
12773 * path through the logic tree (except for 'if (pass)' bits), since
12774 * both passes are following the same op_next chain; and in
12775 * particular, if it would return early on the second pass, it would
12776 * already have returned early on the first pass.
12778 for (pass = 0; pass < 2; pass++) {
12780 UV action = orig_action;
12781 OP *first_elem_op = NULL; /* first seen aelem/helem */
12782 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */
12783 int action_count = 0; /* number of actions seen so far */
12784 int action_ix = 0; /* action_count % (actions per IV) */
12785 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */
12786 bool is_last = FALSE; /* no more derefs to follow */
12787 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */
12788 UNOP_AUX_item *arg = arg_buf;
12789 UNOP_AUX_item *action_ptr = arg_buf;
12792 action_ptr->uv = 0;
12796 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
12797 case MDEREF_HV_gvhv_helem:
12798 next_is_hash = TRUE;
12800 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
12801 case MDEREF_AV_gvav_aelem:
12803 #ifdef USE_ITHREADS
12804 arg->pad_offset = cPADOPx(start)->op_padix;
12805 /* stop it being swiped when nulled */
12806 cPADOPx(start)->op_padix = 0;
12808 arg->sv = cSVOPx(start)->op_sv;
12809 cSVOPx(start)->op_sv = NULL;
12815 case MDEREF_HV_padhv_helem:
12816 case MDEREF_HV_padsv_vivify_rv2hv_helem:
12817 next_is_hash = TRUE;
12819 case MDEREF_AV_padav_aelem:
12820 case MDEREF_AV_padsv_vivify_rv2av_aelem:
12822 arg->pad_offset = start->op_targ;
12823 /* we skip setting op_targ = 0 for now, since the intact
12824 * OP_PADXV is needed by S_check_hash_fields_and_hekify */
12825 reset_start_targ = TRUE;
12830 case MDEREF_HV_pop_rv2hv_helem:
12831 next_is_hash = TRUE;
12833 case MDEREF_AV_pop_rv2av_aelem:
12837 NOT_REACHED; /* NOTREACHED */
12842 /* look for another (rv2av/hv; get index;
12843 * aelem/helem/exists/delele) sequence */
12848 UV index_type = MDEREF_INDEX_none;
12850 if (action_count) {
12851 /* if this is not the first lookup, consume the rv2av/hv */
12853 /* for N levels of aggregate lookup, we normally expect
12854 * that the first N-1 [ah]elem ops will be flagged as
12855 * /DEREF (so they autovivifiy if necessary), and the last
12856 * lookup op not to be.
12857 * For other things (like @{$h{k1}{k2}}) extra scope or
12858 * leave ops can appear, so abandon the effort in that
12860 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
12863 /* rv2av or rv2hv sKR/1 */
12865 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
12866 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
12867 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
12870 /* at this point, we wouldn't expect any of these
12871 * possible private flags:
12872 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO
12873 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only)
12875 ASSUME(!(o->op_private &
12876 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING)));
12878 hints = (o->op_private & OPpHINT_STRICT_REFS);
12880 /* make sure the type of the previous /DEREF matches the
12881 * type of the next lookup */
12882 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV));
12885 action = next_is_hash
12886 ? MDEREF_HV_vivify_rv2hv_helem
12887 : MDEREF_AV_vivify_rv2av_aelem;
12891 /* if this is the second pass, and we're at the depth where
12892 * previously we encountered a non-simple index expression,
12893 * stop processing the index at this point */
12894 if (action_count != index_skip) {
12896 /* look for one or more simple ops that return an array
12897 * index or hash key */
12899 switch (o->op_type) {
12901 /* it may be a lexical var index */
12902 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS
12903 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
12904 ASSUME(!(o->op_private &
12905 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
12907 if ( OP_GIMME(o,0) == G_SCALAR
12908 && !(o->op_flags & (OPf_REF|OPf_MOD))
12909 && o->op_private == 0)
12912 arg->pad_offset = o->op_targ;
12914 index_type = MDEREF_INDEX_padsv;
12920 if (next_is_hash) {
12921 /* it's a constant hash index */
12922 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK)))
12923 /* "use constant foo => FOO; $h{+foo}" for
12924 * some weird FOO, can leave you with constants
12925 * that aren't simple strings. It's not worth
12926 * the extra hassle for those edge cases */
12931 OP * helem_op = o->op_next;
12933 ASSUME( helem_op->op_type == OP_HELEM
12934 || helem_op->op_type == OP_NULL);
12935 if (helem_op->op_type == OP_HELEM) {
12936 rop = (UNOP*)(((BINOP*)helem_op)->op_first);
12937 if ( helem_op->op_private & OPpLVAL_INTRO
12938 || rop->op_type != OP_RV2HV
12942 S_check_hash_fields_and_hekify(aTHX_ rop, cSVOPo);
12944 #ifdef USE_ITHREADS
12945 /* Relocate sv to the pad for thread safety */
12946 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
12947 arg->pad_offset = o->op_targ;
12950 arg->sv = cSVOPx_sv(o);
12955 /* it's a constant array index */
12957 SV *ix_sv = cSVOPo->op_sv;
12962 if ( action_count == 0
12965 && ( action == MDEREF_AV_padav_aelem
12966 || action == MDEREF_AV_gvav_aelem)
12968 maybe_aelemfast = TRUE;
12972 SvREFCNT_dec_NN(cSVOPo->op_sv);
12976 /* we've taken ownership of the SV */
12977 cSVOPo->op_sv = NULL;
12979 index_type = MDEREF_INDEX_const;
12984 /* it may be a package var index */
12986 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_SPECIAL)));
12987 ASSUME(!(o->op_private & ~(OPpEARLY_CV)));
12988 if ( (o->op_flags &~ OPf_SPECIAL) != OPf_WANT_SCALAR
12989 || o->op_private != 0
12994 if (kid->op_type != OP_RV2SV)
12997 ASSUME(!(kid->op_flags &
12998 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF
12999 |OPf_SPECIAL|OPf_PARENS)));
13000 ASSUME(!(kid->op_private &
13002 |OPpHINT_STRICT_REFS|OPpOUR_INTRO
13003 |OPpDEREF|OPpLVAL_INTRO)));
13004 if( (kid->op_flags &~ OPf_PARENS)
13005 != (OPf_WANT_SCALAR|OPf_KIDS)
13006 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS))
13011 #ifdef USE_ITHREADS
13012 arg->pad_offset = cPADOPx(o)->op_padix;
13013 /* stop it being swiped when nulled */
13014 cPADOPx(o)->op_padix = 0;
13016 arg->sv = cSVOPx(o)->op_sv;
13017 cSVOPo->op_sv = NULL;
13021 index_type = MDEREF_INDEX_gvsv;
13026 } /* action_count != index_skip */
13028 action |= index_type;
13031 /* at this point we have either:
13032 * * detected what looks like a simple index expression,
13033 * and expect the next op to be an [ah]elem, or
13034 * an nulled [ah]elem followed by a delete or exists;
13035 * * found a more complex expression, so something other
13036 * than the above follows.
13039 /* possibly an optimised away [ah]elem (where op_next is
13040 * exists or delete) */
13041 if (o->op_type == OP_NULL)
13044 /* at this point we're looking for an OP_AELEM, OP_HELEM,
13045 * OP_EXISTS or OP_DELETE */
13047 /* if something like arybase (a.k.a $[ ) is in scope,
13048 * abandon optimisation attempt */
13049 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
13050 && PL_check[o->op_type] != Perl_ck_null)
13052 /* similarly for customised exists and delete */
13053 if ( (o->op_type == OP_EXISTS)
13054 && PL_check[o->op_type] != Perl_ck_exists)
13056 if ( (o->op_type == OP_DELETE)
13057 && PL_check[o->op_type] != Perl_ck_delete)
13060 if ( o->op_type != OP_AELEM
13061 || (o->op_private &
13062 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB))
13064 maybe_aelemfast = FALSE;
13066 /* look for aelem/helem/exists/delete. If it's not the last elem
13067 * lookup, it *must* have OPpDEREF_AV/HV, but not many other
13068 * flags; if it's the last, then it mustn't have
13069 * OPpDEREF_AV/HV, but may have lots of other flags, like
13070 * OPpLVAL_INTRO etc
13073 if ( index_type == MDEREF_INDEX_none
13074 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM
13075 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE)
13079 /* we have aelem/helem/exists/delete with valid simple index */
13081 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
13082 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV
13083 || (o->op_private & OPpDEREF) == OPpDEREF_HV);
13086 ASSUME(!(o->op_flags &
13087 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS)));
13088 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF)));
13090 ok = (o->op_flags &~ OPf_PARENS)
13091 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD)
13092 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK));
13094 else if (o->op_type == OP_EXISTS) {
13095 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
13096 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
13097 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB)));
13098 ok = !(o->op_private & ~OPpARG1_MASK);
13100 else if (o->op_type == OP_DELETE) {
13101 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
13102 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
13103 ASSUME(!(o->op_private &
13104 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO)));
13105 /* don't handle slices or 'local delete'; the latter
13106 * is fairly rare, and has a complex runtime */
13107 ok = !(o->op_private & ~OPpARG1_MASK);
13108 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM))
13109 /* skip handling run-tome error */
13110 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL));
13113 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM);
13114 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD
13115 |OPf_PARENS|OPf_REF|OPf_SPECIAL)));
13116 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB
13117 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO)));
13118 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV;
13123 if (!first_elem_op)
13127 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV);
13132 action |= MDEREF_FLAG_last;
13136 /* at this point we have something that started
13137 * promisingly enough (with rv2av or whatever), but failed
13138 * to find a simple index followed by an
13139 * aelem/helem/exists/delete. If this is the first action,
13140 * give up; but if we've already seen at least one
13141 * aelem/helem, then keep them and add a new action with
13142 * MDEREF_INDEX_none, which causes it to do the vivify
13143 * from the end of the previous lookup, and do the deref,
13144 * but stop at that point. So $a[0][expr] will do one
13145 * av_fetch, vivify and deref, then continue executing at
13150 index_skip = action_count;
13151 action |= MDEREF_FLAG_last;
13152 if (index_type != MDEREF_INDEX_none)
13157 action_ptr->uv |= (action << (action_ix * MDEREF_SHIFT));
13160 /* if there's no space for the next action, create a new slot
13161 * for it *before* we start adding args for that action */
13162 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) {
13169 } /* while !is_last */
13177 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf);
13178 if (index_skip == -1) {
13179 mderef->op_flags = o->op_flags
13180 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0));
13181 if (o->op_type == OP_EXISTS)
13182 mderef->op_private = OPpMULTIDEREF_EXISTS;
13183 else if (o->op_type == OP_DELETE)
13184 mderef->op_private = OPpMULTIDEREF_DELETE;
13186 mderef->op_private = o->op_private
13187 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO);
13189 /* accumulate strictness from every level (although I don't think
13190 * they can actually vary) */
13191 mderef->op_private |= hints;
13193 /* integrate the new multideref op into the optree and the
13196 * In general an op like aelem or helem has two child
13197 * sub-trees: the aggregate expression (a_expr) and the
13198 * index expression (i_expr):
13204 * The a_expr returns an AV or HV, while the i-expr returns an
13205 * index. In general a multideref replaces most or all of a
13206 * multi-level tree, e.g.
13222 * With multideref, all the i_exprs will be simple vars or
13223 * constants, except that i_expr1 may be arbitrary in the case
13224 * of MDEREF_INDEX_none.
13226 * The bottom-most a_expr will be either:
13227 * 1) a simple var (so padXv or gv+rv2Xv);
13228 * 2) a simple scalar var dereferenced (e.g. $r->[0]):
13229 * so a simple var with an extra rv2Xv;
13230 * 3) or an arbitrary expression.
13232 * 'start', the first op in the execution chain, will point to
13233 * 1),2): the padXv or gv op;
13234 * 3): the rv2Xv which forms the last op in the a_expr
13235 * execution chain, and the top-most op in the a_expr
13238 * For all cases, the 'start' node is no longer required,
13239 * but we can't free it since one or more external nodes
13240 * may point to it. E.g. consider
13241 * $h{foo} = $a ? $b : $c
13242 * Here, both the op_next and op_other branches of the
13243 * cond_expr point to the gv[*h] of the hash expression, so
13244 * we can't free the 'start' op.
13246 * For expr->[...], we need to save the subtree containing the
13247 * expression; for the other cases, we just need to save the
13249 * So in all cases, we null the start op and keep it around by
13250 * making it the child of the multideref op; for the expr->
13251 * case, the expr will be a subtree of the start node.
13253 * So in the simple 1,2 case the optree above changes to
13259 * ex-gv (or ex-padxv)
13261 * with the op_next chain being
13263 * -> ex-gv -> multideref -> op-following-ex-exists ->
13265 * In the 3 case, we have
13278 * -> rest-of-a_expr subtree ->
13279 * ex-rv2xv -> multideref -> op-following-ex-exists ->
13282 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none,
13283 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the
13284 * multideref attached as the child, e.g.
13290 * ex-rv2av - i_expr1
13298 /* if we free this op, don't free the pad entry */
13299 if (reset_start_targ)
13300 start->op_targ = 0;
13303 /* Cut the bit we need to save out of the tree and attach to
13304 * the multideref op, then free the rest of the tree */
13306 /* find parent of node to be detached (for use by splice) */
13308 if ( orig_action == MDEREF_AV_pop_rv2av_aelem
13309 || orig_action == MDEREF_HV_pop_rv2hv_helem)
13311 /* there is an arbitrary expression preceding us, e.g.
13312 * expr->[..]? so we need to save the 'expr' subtree */
13313 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE)
13314 p = cUNOPx(p)->op_first;
13315 ASSUME( start->op_type == OP_RV2AV
13316 || start->op_type == OP_RV2HV);
13319 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem
13320 * above for exists/delete. */
13321 while ( (p->op_flags & OPf_KIDS)
13322 && cUNOPx(p)->op_first != start
13324 p = cUNOPx(p)->op_first;
13326 ASSUME(cUNOPx(p)->op_first == start);
13328 /* detach from main tree, and re-attach under the multideref */
13329 op_sibling_splice(mderef, NULL, 0,
13330 op_sibling_splice(p, NULL, 1, NULL));
13333 start->op_next = mderef;
13335 mderef->op_next = index_skip == -1 ? o->op_next : o;
13337 /* excise and free the original tree, and replace with
13338 * the multideref op */
13339 p = op_sibling_splice(top_op, NULL, -1, mderef);
13348 Size_t size = arg - arg_buf;
13350 if (maybe_aelemfast && action_count == 1)
13353 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc(
13354 sizeof(UNOP_AUX_item) * (size + 1));
13355 /* for dumping etc: store the length in a hidden first slot;
13356 * we set the op_aux pointer to the second slot */
13357 arg_buf->uv = size;
13360 } /* for (pass = ...) */
13365 /* mechanism for deferring recursion in rpeep() */
13367 #define MAX_DEFERRED 4
13371 if (defer_ix == (MAX_DEFERRED-1)) { \
13372 OP **defer = defer_queue[defer_base]; \
13373 CALL_RPEEP(*defer); \
13374 S_prune_chain_head(defer); \
13375 defer_base = (defer_base + 1) % MAX_DEFERRED; \
13378 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
13381 #define IS_AND_OP(o) (o->op_type == OP_AND)
13382 #define IS_OR_OP(o) (o->op_type == OP_OR)
13385 /* A peephole optimizer. We visit the ops in the order they're to execute.
13386 * See the comments at the top of this file for more details about when
13387 * peep() is called */
13390 Perl_rpeep(pTHX_ OP *o)
13394 OP* oldoldop = NULL;
13395 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
13396 int defer_base = 0;
13401 if (!o || o->op_opt)
13404 assert(o->op_type != OP_FREED);
13408 SAVEVPTR(PL_curcop);
13409 for (;; o = o->op_next) {
13410 if (o && o->op_opt)
13413 while (defer_ix >= 0) {
13415 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
13416 CALL_RPEEP(*defer);
13417 S_prune_chain_head(defer);
13424 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */
13425 assert(!oldoldop || oldoldop->op_next == oldop);
13426 assert(!oldop || oldop->op_next == o);
13428 /* By default, this op has now been optimised. A couple of cases below
13429 clear this again. */
13433 /* look for a series of 1 or more aggregate derefs, e.g.
13434 * $a[1]{foo}[$i]{$k}
13435 * and replace with a single OP_MULTIDEREF op.
13436 * Each index must be either a const, or a simple variable,
13438 * First, look for likely combinations of starting ops,
13439 * corresponding to (global and lexical variants of)
13441 * $r->[...] $r->{...}
13442 * (preceding expression)->[...]
13443 * (preceding expression)->{...}
13444 * and if so, call maybe_multideref() to do a full inspection
13445 * of the op chain and if appropriate, replace with an
13453 switch (o2->op_type) {
13455 /* $pkg[..] : gv[*pkg]
13456 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */
13458 /* Fail if there are new op flag combinations that we're
13459 * not aware of, rather than:
13460 * * silently failing to optimise, or
13461 * * silently optimising the flag away.
13462 * If this ASSUME starts failing, examine what new flag
13463 * has been added to the op, and decide whether the
13464 * optimisation should still occur with that flag, then
13465 * update the code accordingly. This applies to all the
13466 * other ASSUMEs in the block of code too.
13468 ASSUME(!(o2->op_flags &
13469 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL)));
13470 ASSUME(!(o2->op_private & ~OPpEARLY_CV));
13474 if (o2->op_type == OP_RV2AV) {
13475 action = MDEREF_AV_gvav_aelem;
13479 if (o2->op_type == OP_RV2HV) {
13480 action = MDEREF_HV_gvhv_helem;
13484 if (o2->op_type != OP_RV2SV)
13487 /* at this point we've seen gv,rv2sv, so the only valid
13488 * construct left is $pkg->[] or $pkg->{} */
13490 ASSUME(!(o2->op_flags & OPf_STACKED));
13491 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13492 != (OPf_WANT_SCALAR|OPf_MOD))
13495 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS
13496 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO)));
13497 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO))
13499 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV
13500 && (o2->op_private & OPpDEREF) != OPpDEREF_HV)
13504 if (o2->op_type == OP_RV2AV) {
13505 action = MDEREF_AV_gvsv_vivify_rv2av_aelem;
13508 if (o2->op_type == OP_RV2HV) {
13509 action = MDEREF_HV_gvsv_vivify_rv2hv_helem;
13515 /* $lex->[...]: padsv[$lex] sM/DREFAV */
13517 ASSUME(!(o2->op_flags &
13518 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL)));
13519 if ((o2->op_flags &
13520 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13521 != (OPf_WANT_SCALAR|OPf_MOD))
13524 ASSUME(!(o2->op_private &
13525 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
13526 /* skip if state or intro, or not a deref */
13527 if ( o2->op_private != OPpDEREF_AV
13528 && o2->op_private != OPpDEREF_HV)
13532 if (o2->op_type == OP_RV2AV) {
13533 action = MDEREF_AV_padsv_vivify_rv2av_aelem;
13536 if (o2->op_type == OP_RV2HV) {
13537 action = MDEREF_HV_padsv_vivify_rv2hv_helem;
13544 /* $lex[..]: padav[@lex:1,2] sR *
13545 * or $lex{..}: padhv[%lex:1,2] sR */
13546 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS|
13547 OPf_REF|OPf_SPECIAL)));
13548 if ((o2->op_flags &
13549 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13550 != (OPf_WANT_SCALAR|OPf_REF))
13552 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF))
13554 /* OPf_PARENS isn't currently used in this case;
13555 * if that changes, let us know! */
13556 ASSUME(!(o2->op_flags & OPf_PARENS));
13558 /* at this point, we wouldn't expect any of the remaining
13559 * possible private flags:
13560 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL,
13561 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB
13563 * OPpSLICEWARNING shouldn't affect runtime
13565 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING)));
13567 action = o2->op_type == OP_PADAV
13568 ? MDEREF_AV_padav_aelem
13569 : MDEREF_HV_padhv_helem;
13571 S_maybe_multideref(aTHX_ o, o2, action, 0);
13577 action = o2->op_type == OP_RV2AV
13578 ? MDEREF_AV_pop_rv2av_aelem
13579 : MDEREF_HV_pop_rv2hv_helem;
13582 /* (expr)->[...]: rv2av sKR/1;
13583 * (expr)->{...}: rv2hv sKR/1; */
13585 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV);
13587 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
13588 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL)));
13589 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
13592 /* at this point, we wouldn't expect any of these
13593 * possible private flags:
13594 * OPpMAYBE_LVSUB, OPpLVAL_INTRO
13595 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only)
13597 ASSUME(!(o2->op_private &
13598 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING
13600 hints |= (o2->op_private & OPpHINT_STRICT_REFS);
13604 S_maybe_multideref(aTHX_ o, o2, action, hints);
13613 switch (o->op_type) {
13615 PL_curcop = ((COP*)o); /* for warnings */
13618 PL_curcop = ((COP*)o); /* for warnings */
13620 /* Optimise a "return ..." at the end of a sub to just be "...".
13621 * This saves 2 ops. Before:
13622 * 1 <;> nextstate(main 1 -e:1) v ->2
13623 * 4 <@> return K ->5
13624 * 2 <0> pushmark s ->3
13625 * - <1> ex-rv2sv sK/1 ->4
13626 * 3 <#> gvsv[*cat] s ->4
13629 * - <@> return K ->-
13630 * - <0> pushmark s ->2
13631 * - <1> ex-rv2sv sK/1 ->-
13632 * 2 <$> gvsv(*cat) s ->3
13635 OP *next = o->op_next;
13636 OP *sibling = OpSIBLING(o);
13637 if ( OP_TYPE_IS(next, OP_PUSHMARK)
13638 && OP_TYPE_IS(sibling, OP_RETURN)
13639 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
13640 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
13641 ||OP_TYPE_IS(sibling->op_next->op_next,
13643 && cUNOPx(sibling)->op_first == next
13644 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next
13647 /* Look through the PUSHMARK's siblings for one that
13648 * points to the RETURN */
13649 OP *top = OpSIBLING(next);
13650 while (top && top->op_next) {
13651 if (top->op_next == sibling) {
13652 top->op_next = sibling->op_next;
13653 o->op_next = next->op_next;
13656 top = OpSIBLING(top);
13661 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
13663 * This latter form is then suitable for conversion into padrange
13664 * later on. Convert:
13666 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
13670 * nextstate1 -> listop -> nextstate3
13672 * pushmark -> padop1 -> padop2
13674 if (o->op_next && (
13675 o->op_next->op_type == OP_PADSV
13676 || o->op_next->op_type == OP_PADAV
13677 || o->op_next->op_type == OP_PADHV
13679 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
13680 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
13681 && o->op_next->op_next->op_next && (
13682 o->op_next->op_next->op_next->op_type == OP_PADSV
13683 || o->op_next->op_next->op_next->op_type == OP_PADAV
13684 || o->op_next->op_next->op_next->op_type == OP_PADHV
13686 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
13687 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
13688 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
13689 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
13691 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm;
13694 ns2 = pad1->op_next;
13695 pad2 = ns2->op_next;
13696 ns3 = pad2->op_next;
13698 /* we assume here that the op_next chain is the same as
13699 * the op_sibling chain */
13700 assert(OpSIBLING(o) == pad1);
13701 assert(OpSIBLING(pad1) == ns2);
13702 assert(OpSIBLING(ns2) == pad2);
13703 assert(OpSIBLING(pad2) == ns3);
13705 /* excise and delete ns2 */
13706 op_sibling_splice(NULL, pad1, 1, NULL);
13709 /* excise pad1 and pad2 */
13710 op_sibling_splice(NULL, o, 2, NULL);
13712 /* create new listop, with children consisting of:
13713 * a new pushmark, pad1, pad2. */
13714 newop = newLISTOP(OP_LIST, 0, pad1, pad2);
13715 newop->op_flags |= OPf_PARENS;
13716 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
13718 /* insert newop between o and ns3 */
13719 op_sibling_splice(NULL, o, 0, newop);
13721 /*fixup op_next chain */
13722 newpm = cUNOPx(newop)->op_first; /* pushmark */
13723 o ->op_next = newpm;
13724 newpm->op_next = pad1;
13725 pad1 ->op_next = pad2;
13726 pad2 ->op_next = newop; /* listop */
13727 newop->op_next = ns3;
13729 /* Ensure pushmark has this flag if padops do */
13730 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) {
13731 newpm->op_flags |= OPf_MOD;
13737 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
13738 to carry two labels. For now, take the easier option, and skip
13739 this optimisation if the first NEXTSTATE has a label. */
13740 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
13741 OP *nextop = o->op_next;
13742 while (nextop && nextop->op_type == OP_NULL)
13743 nextop = nextop->op_next;
13745 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
13748 oldop->op_next = nextop;
13750 /* Skip (old)oldop assignment since the current oldop's
13751 op_next already points to the next op. */
13758 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
13759 if (o->op_next->op_private & OPpTARGET_MY) {
13760 if (o->op_flags & OPf_STACKED) /* chained concats */
13761 break; /* ignore_optimization */
13763 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
13764 o->op_targ = o->op_next->op_targ;
13765 o->op_next->op_targ = 0;
13766 o->op_private |= OPpTARGET_MY;
13769 op_null(o->op_next);
13773 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
13774 break; /* Scalar stub must produce undef. List stub is noop */
13778 if (o->op_targ == OP_NEXTSTATE
13779 || o->op_targ == OP_DBSTATE)
13781 PL_curcop = ((COP*)o);
13783 /* XXX: We avoid setting op_seq here to prevent later calls
13784 to rpeep() from mistakenly concluding that optimisation
13785 has already occurred. This doesn't fix the real problem,
13786 though (See 20010220.007 (#5874)). AMS 20010719 */
13787 /* op_seq functionality is now replaced by op_opt */
13795 oldop->op_next = o->op_next;
13809 convert repeat into a stub with no kids.
13811 if (o->op_next->op_type == OP_CONST
13812 || ( o->op_next->op_type == OP_PADSV
13813 && !(o->op_next->op_private & OPpLVAL_INTRO))
13814 || ( o->op_next->op_type == OP_GV
13815 && o->op_next->op_next->op_type == OP_RV2SV
13816 && !(o->op_next->op_next->op_private
13817 & (OPpLVAL_INTRO|OPpOUR_INTRO))))
13819 const OP *kid = o->op_next->op_next;
13820 if (o->op_next->op_type == OP_GV)
13821 kid = kid->op_next;
13822 /* kid is now the ex-list. */
13823 if (kid->op_type == OP_NULL
13824 && (kid = kid->op_next)->op_type == OP_CONST
13825 /* kid is now the repeat count. */
13826 && kid->op_next->op_type == OP_REPEAT
13827 && kid->op_next->op_private & OPpREPEAT_DOLIST
13828 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST
13829 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0
13832 o = kid->op_next; /* repeat */
13833 oldop->op_next = o;
13834 op_free(cBINOPo->op_first);
13835 op_free(cBINOPo->op_last );
13836 o->op_flags &=~ OPf_KIDS;
13837 /* stub is a baseop; repeat is a binop */
13838 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP));
13839 OpTYPE_set(o, OP_STUB);
13845 /* Convert a series of PAD ops for my vars plus support into a
13846 * single padrange op. Basically
13848 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
13850 * becomes, depending on circumstances, one of
13852 * padrange ----------------------------------> (list) -> rest
13853 * padrange --------------------------------------------> rest
13855 * where all the pad indexes are sequential and of the same type
13857 * We convert the pushmark into a padrange op, then skip
13858 * any other pad ops, and possibly some trailing ops.
13859 * Note that we don't null() the skipped ops, to make it
13860 * easier for Deparse to undo this optimisation (and none of
13861 * the skipped ops are holding any resourses). It also makes
13862 * it easier for find_uninit_var(), as it can just ignore
13863 * padrange, and examine the original pad ops.
13867 OP *followop = NULL; /* the op that will follow the padrange op */
13870 PADOFFSET base = 0; /* init only to stop compiler whining */
13871 bool gvoid = 0; /* init only to stop compiler whining */
13872 bool defav = 0; /* seen (...) = @_ */
13873 bool reuse = 0; /* reuse an existing padrange op */
13875 /* look for a pushmark -> gv[_] -> rv2av */
13880 if ( p->op_type == OP_GV
13881 && cGVOPx_gv(p) == PL_defgv
13882 && (rv2av = p->op_next)
13883 && rv2av->op_type == OP_RV2AV
13884 && !(rv2av->op_flags & OPf_REF)
13885 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
13886 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
13888 q = rv2av->op_next;
13889 if (q->op_type == OP_NULL)
13891 if (q->op_type == OP_PUSHMARK) {
13901 /* scan for PAD ops */
13903 for (p = p->op_next; p; p = p->op_next) {
13904 if (p->op_type == OP_NULL)
13907 if (( p->op_type != OP_PADSV
13908 && p->op_type != OP_PADAV
13909 && p->op_type != OP_PADHV
13911 /* any private flag other than INTRO? e.g. STATE */
13912 || (p->op_private & ~OPpLVAL_INTRO)
13916 /* let $a[N] potentially be optimised into AELEMFAST_LEX
13918 if ( p->op_type == OP_PADAV
13920 && p->op_next->op_type == OP_CONST
13921 && p->op_next->op_next
13922 && p->op_next->op_next->op_type == OP_AELEM
13926 /* for 1st padop, note what type it is and the range
13927 * start; for the others, check that it's the same type
13928 * and that the targs are contiguous */
13930 intro = (p->op_private & OPpLVAL_INTRO);
13932 gvoid = OP_GIMME(p,0) == G_VOID;
13935 if ((p->op_private & OPpLVAL_INTRO) != intro)
13937 /* Note that you'd normally expect targs to be
13938 * contiguous in my($a,$b,$c), but that's not the case
13939 * when external modules start doing things, e.g.
13940 * Function::Parameters */
13941 if (p->op_targ != base + count)
13943 assert(p->op_targ == base + count);
13944 /* Either all the padops or none of the padops should
13945 be in void context. Since we only do the optimisa-
13946 tion for av/hv when the aggregate itself is pushed
13947 on to the stack (one item), there is no need to dis-
13948 tinguish list from scalar context. */
13949 if (gvoid != (OP_GIMME(p,0) == G_VOID))
13953 /* for AV, HV, only when we're not flattening */
13954 if ( p->op_type != OP_PADSV
13956 && !(p->op_flags & OPf_REF)
13960 if (count >= OPpPADRANGE_COUNTMASK)
13963 /* there's a biggest base we can fit into a
13964 * SAVEt_CLEARPADRANGE in pp_padrange.
13965 * (The sizeof() stuff will be constant-folded, and is
13966 * intended to avoid getting "comparison is always false"
13967 * compiler warnings. See the comments above
13968 * MEM_WRAP_CHECK for more explanation on why we do this
13969 * in a weird way to avoid compiler warnings.)
13972 && (8*sizeof(base) >
13973 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT
13975 : (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
13977 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
13981 /* Success! We've got another valid pad op to optimise away */
13983 followop = p->op_next;
13986 if (count < 1 || (count == 1 && !defav))
13989 /* pp_padrange in specifically compile-time void context
13990 * skips pushing a mark and lexicals; in all other contexts
13991 * (including unknown till runtime) it pushes a mark and the
13992 * lexicals. We must be very careful then, that the ops we
13993 * optimise away would have exactly the same effect as the
13995 * In particular in void context, we can only optimise to
13996 * a padrange if we see the complete sequence
13997 * pushmark, pad*v, ...., list
13998 * which has the net effect of leaving the markstack as it
13999 * was. Not pushing onto the stack (whereas padsv does touch
14000 * the stack) makes no difference in void context.
14004 if (followop->op_type == OP_LIST
14005 && OP_GIMME(followop,0) == G_VOID
14008 followop = followop->op_next; /* skip OP_LIST */
14010 /* consolidate two successive my(...);'s */
14013 && oldoldop->op_type == OP_PADRANGE
14014 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
14015 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
14016 && !(oldoldop->op_flags & OPf_SPECIAL)
14019 assert(oldoldop->op_next == oldop);
14020 assert( oldop->op_type == OP_NEXTSTATE
14021 || oldop->op_type == OP_DBSTATE);
14022 assert(oldop->op_next == o);
14025 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
14027 /* Do not assume pad offsets for $c and $d are con-
14032 if ( oldoldop->op_targ + old_count == base
14033 && old_count < OPpPADRANGE_COUNTMASK - count) {
14034 base = oldoldop->op_targ;
14035 count += old_count;
14040 /* if there's any immediately following singleton
14041 * my var's; then swallow them and the associated
14043 * my ($a,$b); my $c; my $d;
14045 * my ($a,$b,$c,$d);
14048 while ( ((p = followop->op_next))
14049 && ( p->op_type == OP_PADSV
14050 || p->op_type == OP_PADAV
14051 || p->op_type == OP_PADHV)
14052 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
14053 && (p->op_private & OPpLVAL_INTRO) == intro
14054 && !(p->op_private & ~OPpLVAL_INTRO)
14056 && ( p->op_next->op_type == OP_NEXTSTATE
14057 || p->op_next->op_type == OP_DBSTATE)
14058 && count < OPpPADRANGE_COUNTMASK
14059 && base + count == p->op_targ
14062 followop = p->op_next;
14070 assert(oldoldop->op_type == OP_PADRANGE);
14071 oldoldop->op_next = followop;
14072 oldoldop->op_private = (intro | count);
14078 /* Convert the pushmark into a padrange.
14079 * To make Deparse easier, we guarantee that a padrange was
14080 * *always* formerly a pushmark */
14081 assert(o->op_type == OP_PUSHMARK);
14082 o->op_next = followop;
14083 OpTYPE_set(o, OP_PADRANGE);
14085 /* bit 7: INTRO; bit 6..0: count */
14086 o->op_private = (intro | count);
14087 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
14088 | gvoid * OPf_WANT_VOID
14089 | (defav ? OPf_SPECIAL : 0));
14097 /* Skip over state($x) in void context. */
14098 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO)
14099 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID)
14101 oldop->op_next = o->op_next;
14102 goto redo_nextstate;
14104 if (o->op_type != OP_PADAV)
14108 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
14109 OP* const pop = (o->op_type == OP_PADAV) ?
14110 o->op_next : o->op_next->op_next;
14112 if (pop && pop->op_type == OP_CONST &&
14113 ((PL_op = pop->op_next)) &&
14114 pop->op_next->op_type == OP_AELEM &&
14115 !(pop->op_next->op_private &
14116 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
14117 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
14120 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
14121 no_bareword_allowed(pop);
14122 if (o->op_type == OP_GV)
14123 op_null(o->op_next);
14124 op_null(pop->op_next);
14126 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
14127 o->op_next = pop->op_next->op_next;
14128 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
14129 o->op_private = (U8)i;
14130 if (o->op_type == OP_GV) {
14133 o->op_type = OP_AELEMFAST;
14136 o->op_type = OP_AELEMFAST_LEX;
14138 if (o->op_type != OP_GV)
14142 /* Remove $foo from the op_next chain in void context. */
14144 && ( o->op_next->op_type == OP_RV2SV
14145 || o->op_next->op_type == OP_RV2AV
14146 || o->op_next->op_type == OP_RV2HV )
14147 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
14148 && !(o->op_next->op_private & OPpLVAL_INTRO))
14150 oldop->op_next = o->op_next->op_next;
14151 /* Reprocess the previous op if it is a nextstate, to
14152 allow double-nextstate optimisation. */
14154 if (oldop->op_type == OP_NEXTSTATE) {
14161 o = oldop->op_next;
14164 else if (o->op_next->op_type == OP_RV2SV) {
14165 if (!(o->op_next->op_private & OPpDEREF)) {
14166 op_null(o->op_next);
14167 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
14169 o->op_next = o->op_next->op_next;
14170 OpTYPE_set(o, OP_GVSV);
14173 else if (o->op_next->op_type == OP_READLINE
14174 && o->op_next->op_next->op_type == OP_CONCAT
14175 && (o->op_next->op_next->op_flags & OPf_STACKED))
14177 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
14178 OpTYPE_set(o, OP_RCATLINE);
14179 o->op_flags |= OPf_STACKED;
14180 op_null(o->op_next->op_next);
14181 op_null(o->op_next);
14186 #define HV_OR_SCALARHV(op) \
14187 ( (op)->op_type == OP_PADHV || (op)->op_type == OP_RV2HV \
14189 : (op)->op_type == OP_SCALAR && (op)->op_flags & OPf_KIDS \
14190 && ( cUNOPx(op)->op_first->op_type == OP_PADHV \
14191 || cUNOPx(op)->op_first->op_type == OP_RV2HV) \
14192 ? cUNOPx(op)->op_first \
14196 if ((fop = HV_OR_SCALARHV(cUNOP->op_first)))
14197 fop->op_private |= OPpTRUEBOOL;
14203 fop = cLOGOP->op_first;
14204 sop = OpSIBLING(fop);
14205 while (cLOGOP->op_other->op_type == OP_NULL)
14206 cLOGOP->op_other = cLOGOP->op_other->op_next;
14207 while (o->op_next && ( o->op_type == o->op_next->op_type
14208 || o->op_next->op_type == OP_NULL))
14209 o->op_next = o->op_next->op_next;
14211 /* If we're an OR and our next is an AND in void context, we'll
14212 follow its op_other on short circuit, same for reverse.
14213 We can't do this with OP_DOR since if it's true, its return
14214 value is the underlying value which must be evaluated
14218 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
14219 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
14221 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
14223 o->op_next = ((LOGOP*)o->op_next)->op_other;
14225 DEFER(cLOGOP->op_other);
14228 fop = HV_OR_SCALARHV(fop);
14229 if (sop) sop = HV_OR_SCALARHV(sop);
14234 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) {
14235 while (nop && nop->op_next) {
14236 switch (nop->op_next->op_type) {
14241 lop = nop = nop->op_next;
14244 nop = nop->op_next;
14253 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
14254 || o->op_type == OP_AND )
14255 fop->op_private |= OPpTRUEBOOL;
14256 else if (!(lop->op_flags & OPf_WANT))
14257 fop->op_private |= OPpMAYBE_TRUEBOOL;
14259 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
14261 sop->op_private |= OPpTRUEBOOL;
14268 if ((fop = HV_OR_SCALARHV(cLOGOP->op_first)))
14269 fop->op_private |= OPpTRUEBOOL;
14270 #undef HV_OR_SCALARHV
14271 /* GERONIMO! */ /* FALLTHROUGH */
14280 case OP_ARGDEFELEM:
14281 while (cLOGOP->op_other->op_type == OP_NULL)
14282 cLOGOP->op_other = cLOGOP->op_other->op_next;
14283 DEFER(cLOGOP->op_other);
14288 while (cLOOP->op_redoop->op_type == OP_NULL)
14289 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
14290 while (cLOOP->op_nextop->op_type == OP_NULL)
14291 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
14292 while (cLOOP->op_lastop->op_type == OP_NULL)
14293 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
14294 /* a while(1) loop doesn't have an op_next that escapes the
14295 * loop, so we have to explicitly follow the op_lastop to
14296 * process the rest of the code */
14297 DEFER(cLOOP->op_lastop);
14301 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
14302 DEFER(cLOGOPo->op_other);
14306 assert(!(cPMOP->op_pmflags & PMf_ONCE));
14307 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
14308 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
14309 cPMOP->op_pmstashstartu.op_pmreplstart
14310 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
14311 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
14317 if (o->op_flags & OPf_SPECIAL) {
14318 /* first arg is a code block */
14319 OP * const nullop = OpSIBLING(cLISTOP->op_first);
14320 OP * kid = cUNOPx(nullop)->op_first;
14322 assert(nullop->op_type == OP_NULL);
14323 assert(kid->op_type == OP_SCOPE
14324 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
14325 /* since OP_SORT doesn't have a handy op_other-style
14326 * field that can point directly to the start of the code
14327 * block, store it in the otherwise-unused op_next field
14328 * of the top-level OP_NULL. This will be quicker at
14329 * run-time, and it will also allow us to remove leading
14330 * OP_NULLs by just messing with op_nexts without
14331 * altering the basic op_first/op_sibling layout. */
14332 kid = kLISTOP->op_first;
14334 (kid->op_type == OP_NULL
14335 && ( kid->op_targ == OP_NEXTSTATE
14336 || kid->op_targ == OP_DBSTATE ))
14337 || kid->op_type == OP_STUB
14338 || kid->op_type == OP_ENTER);
14339 nullop->op_next = kLISTOP->op_next;
14340 DEFER(nullop->op_next);
14343 /* check that RHS of sort is a single plain array */
14344 oright = cUNOPo->op_first;
14345 if (!oright || oright->op_type != OP_PUSHMARK)
14348 if (o->op_private & OPpSORT_INPLACE)
14351 /* reverse sort ... can be optimised. */
14352 if (!OpHAS_SIBLING(cUNOPo)) {
14353 /* Nothing follows us on the list. */
14354 OP * const reverse = o->op_next;
14356 if (reverse->op_type == OP_REVERSE &&
14357 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
14358 OP * const pushmark = cUNOPx(reverse)->op_first;
14359 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
14360 && (OpSIBLING(cUNOPx(pushmark)) == o)) {
14361 /* reverse -> pushmark -> sort */
14362 o->op_private |= OPpSORT_REVERSE;
14364 pushmark->op_next = oright->op_next;
14374 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
14376 LISTOP *enter, *exlist;
14378 if (o->op_private & OPpSORT_INPLACE)
14381 enter = (LISTOP *) o->op_next;
14384 if (enter->op_type == OP_NULL) {
14385 enter = (LISTOP *) enter->op_next;
14389 /* for $a (...) will have OP_GV then OP_RV2GV here.
14390 for (...) just has an OP_GV. */
14391 if (enter->op_type == OP_GV) {
14392 gvop = (OP *) enter;
14393 enter = (LISTOP *) enter->op_next;
14396 if (enter->op_type == OP_RV2GV) {
14397 enter = (LISTOP *) enter->op_next;
14403 if (enter->op_type != OP_ENTERITER)
14406 iter = enter->op_next;
14407 if (!iter || iter->op_type != OP_ITER)
14410 expushmark = enter->op_first;
14411 if (!expushmark || expushmark->op_type != OP_NULL
14412 || expushmark->op_targ != OP_PUSHMARK)
14415 exlist = (LISTOP *) OpSIBLING(expushmark);
14416 if (!exlist || exlist->op_type != OP_NULL
14417 || exlist->op_targ != OP_LIST)
14420 if (exlist->op_last != o) {
14421 /* Mmm. Was expecting to point back to this op. */
14424 theirmark = exlist->op_first;
14425 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
14428 if (OpSIBLING(theirmark) != o) {
14429 /* There's something between the mark and the reverse, eg
14430 for (1, reverse (...))
14435 ourmark = ((LISTOP *)o)->op_first;
14436 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
14439 ourlast = ((LISTOP *)o)->op_last;
14440 if (!ourlast || ourlast->op_next != o)
14443 rv2av = OpSIBLING(ourmark);
14444 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av)
14445 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
14446 /* We're just reversing a single array. */
14447 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
14448 enter->op_flags |= OPf_STACKED;
14451 /* We don't have control over who points to theirmark, so sacrifice
14453 theirmark->op_next = ourmark->op_next;
14454 theirmark->op_flags = ourmark->op_flags;
14455 ourlast->op_next = gvop ? gvop : (OP *) enter;
14458 enter->op_private |= OPpITER_REVERSED;
14459 iter->op_private |= OPpITER_REVERSED;
14463 o = oldop->op_next;
14465 NOT_REACHED; /* NOTREACHED */
14471 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
14472 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
14477 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)
14478 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb)))
14481 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
14483 sv = newRV((SV *)PL_compcv);
14487 OpTYPE_set(o, OP_CONST);
14488 o->op_flags |= OPf_SPECIAL;
14489 cSVOPo->op_sv = sv;
14494 if (OP_GIMME(o,0) == G_VOID
14495 || ( o->op_next->op_type == OP_LINESEQ
14496 && ( o->op_next->op_next->op_type == OP_LEAVESUB
14497 || ( o->op_next->op_next->op_type == OP_RETURN
14498 && !CvLVALUE(PL_compcv)))))
14500 OP *right = cBINOP->op_first;
14519 OP *left = OpSIBLING(right);
14520 if (left->op_type == OP_SUBSTR
14521 && (left->op_private & 7) < 4) {
14523 /* cut out right */
14524 op_sibling_splice(o, NULL, 1, NULL);
14525 /* and insert it as second child of OP_SUBSTR */
14526 op_sibling_splice(left, cBINOPx(left)->op_first, 0,
14528 left->op_private |= OPpSUBSTR_REPL_FIRST;
14530 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
14537 int l, r, lr, lscalars, rscalars;
14539 /* handle common vars detection, e.g. ($a,$b) = ($b,$a).
14540 Note that we do this now rather than in newASSIGNOP(),
14541 since only by now are aliased lexicals flagged as such
14543 See the essay "Common vars in list assignment" above for
14544 the full details of the rationale behind all the conditions
14547 PL_generation sorcery:
14548 To detect whether there are common vars, the global var
14549 PL_generation is incremented for each assign op we scan.
14550 Then we run through all the lexical variables on the LHS,
14551 of the assignment, setting a spare slot in each of them to
14552 PL_generation. Then we scan the RHS, and if any lexicals
14553 already have that value, we know we've got commonality.
14554 Also, if the generation number is already set to
14555 PERL_INT_MAX, then the variable is involved in aliasing, so
14556 we also have potential commonality in that case.
14562 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, 1, &lscalars);
14565 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, 1, &rscalars);
14569 /* After looking for things which are *always* safe, this main
14570 * if/else chain selects primarily based on the type of the
14571 * LHS, gradually working its way down from the more dangerous
14572 * to the more restrictive and thus safer cases */
14574 if ( !l /* () = ....; */
14575 || !r /* .... = (); */
14576 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */
14577 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */
14578 || (lscalars < 2) /* ($x, undef) = ... */
14580 NOOP; /* always safe */
14582 else if (l & AAS_DANGEROUS) {
14583 /* always dangerous */
14584 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14585 o->op_private |= OPpASSIGN_COMMON_AGG;
14587 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) {
14588 /* package vars are always dangerous - too many
14589 * aliasing possibilities */
14590 if (l & AAS_PKG_SCALAR)
14591 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14592 if (l & AAS_PKG_AGG)
14593 o->op_private |= OPpASSIGN_COMMON_AGG;
14595 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG
14596 |AAS_LEX_SCALAR|AAS_LEX_AGG))
14598 /* LHS contains only lexicals and safe ops */
14600 if (l & (AAS_MY_AGG|AAS_LEX_AGG))
14601 o->op_private |= OPpASSIGN_COMMON_AGG;
14603 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) {
14604 if (lr & AAS_LEX_SCALAR_COMM)
14605 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14606 else if ( !(l & AAS_LEX_SCALAR)
14607 && (r & AAS_DEFAV))
14611 * as scalar-safe for performance reasons.
14612 * (it will still have been marked _AGG if necessary */
14615 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS))
14616 /* if there are only lexicals on the LHS and no
14617 * common ones on the RHS, then we assume that the
14618 * only way those lexicals could also get
14619 * on the RHS is via some sort of dereffing or
14622 * ($lex, $x) = (1, $$r)
14623 * and in this case we assume the var must have
14624 * a bumped ref count. So if its ref count is 1,
14625 * it must only be on the LHS.
14627 o->op_private |= OPpASSIGN_COMMON_RC1;
14632 * may have to handle aggregate on LHS, but we can't
14633 * have common scalars. */
14636 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1);
14642 Perl_cpeep_t cpeep =
14643 XopENTRYCUSTOM(o, xop_peep);
14645 cpeep(aTHX_ o, oldop);
14650 /* did we just null the current op? If so, re-process it to handle
14651 * eliding "empty" ops from the chain */
14652 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
14665 Perl_peep(pTHX_ OP *o)
14671 =head1 Custom Operators
14673 =for apidoc Ao||custom_op_xop
14674 Return the XOP structure for a given custom op. This macro should be
14675 considered internal to C<OP_NAME> and the other access macros: use them instead.
14676 This macro does call a function. Prior
14677 to 5.19.6, this was implemented as a
14684 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
14690 static const XOP xop_null = { 0, 0, 0, 0, 0 };
14692 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
14693 assert(o->op_type == OP_CUSTOM);
14695 /* This is wrong. It assumes a function pointer can be cast to IV,
14696 * which isn't guaranteed, but this is what the old custom OP code
14697 * did. In principle it should be safer to Copy the bytes of the
14698 * pointer into a PV: since the new interface is hidden behind
14699 * functions, this can be changed later if necessary. */
14700 /* Change custom_op_xop if this ever happens */
14701 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
14704 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
14706 /* assume noone will have just registered a desc */
14707 if (!he && PL_custom_op_names &&
14708 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
14713 /* XXX does all this need to be shared mem? */
14714 Newxz(xop, 1, XOP);
14715 pv = SvPV(HeVAL(he), l);
14716 XopENTRY_set(xop, xop_name, savepvn(pv, l));
14717 if (PL_custom_op_descs &&
14718 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
14720 pv = SvPV(HeVAL(he), l);
14721 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
14723 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
14727 xop = (XOP *)&xop_null;
14729 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
14733 if(field == XOPe_xop_ptr) {
14736 const U32 flags = XopFLAGS(xop);
14737 if(flags & field) {
14739 case XOPe_xop_name:
14740 any.xop_name = xop->xop_name;
14742 case XOPe_xop_desc:
14743 any.xop_desc = xop->xop_desc;
14745 case XOPe_xop_class:
14746 any.xop_class = xop->xop_class;
14748 case XOPe_xop_peep:
14749 any.xop_peep = xop->xop_peep;
14752 NOT_REACHED; /* NOTREACHED */
14757 case XOPe_xop_name:
14758 any.xop_name = XOPd_xop_name;
14760 case XOPe_xop_desc:
14761 any.xop_desc = XOPd_xop_desc;
14763 case XOPe_xop_class:
14764 any.xop_class = XOPd_xop_class;
14766 case XOPe_xop_peep:
14767 any.xop_peep = XOPd_xop_peep;
14770 NOT_REACHED; /* NOTREACHED */
14775 /* On some platforms (HP-UX, IA64) gcc emits a warning for this function:
14776 * op.c: In function 'Perl_custom_op_get_field':
14777 * op.c:...: warning: 'any.xop_name' may be used uninitialized in this function [-Wmaybe-uninitialized]
14778 * This is because on those platforms (with -DEBUGGING) NOT_REACHED
14779 * expands to assert(0), which expands to ((0) ? (void)0 :
14780 * __assert(...)), and gcc doesn't know that __assert can never return. */
14786 =for apidoc Ao||custom_op_register
14787 Register a custom op. See L<perlguts/"Custom Operators">.
14793 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
14797 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
14799 /* see the comment in custom_op_xop */
14800 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
14802 if (!PL_custom_ops)
14803 PL_custom_ops = newHV();
14805 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
14806 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
14811 =for apidoc core_prototype
14813 This function assigns the prototype of the named core function to C<sv>, or
14814 to a new mortal SV if C<sv> is C<NULL>. It returns the modified C<sv>, or
14815 C<NULL> if the core function has no prototype. C<code> is a code as returned
14816 by C<keyword()>. It must not be equal to 0.
14822 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
14825 int i = 0, n = 0, seen_question = 0, defgv = 0;
14827 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
14828 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
14829 bool nullret = FALSE;
14831 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
14835 if (!sv) sv = sv_newmortal();
14837 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
14839 switch (code < 0 ? -code : code) {
14840 case KEY_and : case KEY_chop: case KEY_chomp:
14841 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
14842 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
14843 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
14844 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
14845 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
14846 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
14847 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
14848 case KEY_x : case KEY_xor :
14849 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
14850 case KEY_glob: retsetpvs("_;", OP_GLOB);
14851 case KEY_keys: retsetpvs("\\[%@]", OP_KEYS);
14852 case KEY_values: retsetpvs("\\[%@]", OP_VALUES);
14853 case KEY_each: retsetpvs("\\[%@]", OP_EACH);
14854 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
14855 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
14857 case KEY_evalbytes:
14858 name = "entereval"; break;
14866 while (i < MAXO) { /* The slow way. */
14867 if (strEQ(name, PL_op_name[i])
14868 || strEQ(name, PL_op_desc[i]))
14870 if (nullret) { assert(opnum); *opnum = i; return NULL; }
14877 defgv = PL_opargs[i] & OA_DEFGV;
14878 oa = PL_opargs[i] >> OASHIFT;
14880 if (oa & OA_OPTIONAL && !seen_question && (
14881 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
14886 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
14887 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
14888 /* But globs are already references (kinda) */
14889 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
14893 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
14894 && !scalar_mod_type(NULL, i)) {
14899 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
14903 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
14904 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
14905 str[n-1] = '_'; defgv = 0;
14909 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
14911 sv_setpvn(sv, str, n - 1);
14912 if (opnum) *opnum = i;
14917 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
14920 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv);
14923 PERL_ARGS_ASSERT_CORESUB_OP;
14927 return op_append_elem(OP_LINESEQ,
14930 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
14937 o = newUNOP(OP_AVHVSWITCH,0,argop);
14938 o->op_private = opnum-OP_EACH;
14940 case OP_SELECT: /* which represents OP_SSELECT as well */
14945 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
14946 newSVOP(OP_CONST, 0, newSVuv(1))
14948 coresub_op(newSVuv((UV)OP_SSELECT), 0,
14950 coresub_op(coreargssv, 0, OP_SELECT)
14954 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
14956 return op_append_elem(
14959 opnum == OP_WANTARRAY || opnum == OP_RUNCV
14960 ? OPpOFFBYONE << 8 : 0)
14962 case OA_BASEOP_OR_UNOP:
14963 if (opnum == OP_ENTEREVAL) {
14964 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
14965 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
14967 else o = newUNOP(opnum,0,argop);
14968 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
14971 if (is_handle_constructor(o, 1))
14972 argop->op_private |= OPpCOREARGS_DEREF1;
14973 if (scalar_mod_type(NULL, opnum))
14974 argop->op_private |= OPpCOREARGS_SCALARMOD;
14978 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
14979 if (is_handle_constructor(o, 2))
14980 argop->op_private |= OPpCOREARGS_DEREF2;
14981 if (opnum == OP_SUBSTR) {
14982 o->op_private |= OPpMAYBE_LVSUB;
14991 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
14992 SV * const *new_const_svp)
14994 const char *hvname;
14995 bool is_const = !!CvCONST(old_cv);
14996 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
14998 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
15000 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
15002 /* They are 2 constant subroutines generated from
15003 the same constant. This probably means that
15004 they are really the "same" proxy subroutine
15005 instantiated in 2 places. Most likely this is
15006 when a constant is exported twice. Don't warn.
15009 (ckWARN(WARN_REDEFINE)
15011 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
15012 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
15013 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
15014 strEQ(hvname, "autouse"))
15018 && ckWARN_d(WARN_REDEFINE)
15019 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
15022 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
15024 ? "Constant subroutine %" SVf " redefined"
15025 : "Subroutine %" SVf " redefined",
15030 =head1 Hook manipulation
15032 These functions provide convenient and thread-safe means of manipulating
15039 =for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p
15041 Puts a C function into the chain of check functions for a specified op
15042 type. This is the preferred way to manipulate the L</PL_check> array.
15043 C<opcode> specifies which type of op is to be affected. C<new_checker>
15044 is a pointer to the C function that is to be added to that opcode's
15045 check chain, and C<old_checker_p> points to the storage location where a
15046 pointer to the next function in the chain will be stored. The value of
15047 C<new_pointer> is written into the L</PL_check> array, while the value
15048 previously stored there is written to C<*old_checker_p>.
15050 The function should be defined like this:
15052 static OP *new_checker(pTHX_ OP *op) { ... }
15054 It is intended to be called in this manner:
15056 new_checker(aTHX_ op)
15058 C<old_checker_p> should be defined like this:
15060 static Perl_check_t old_checker_p;
15062 L</PL_check> is global to an entire process, and a module wishing to
15063 hook op checking may find itself invoked more than once per process,
15064 typically in different threads. To handle that situation, this function
15065 is idempotent. The location C<*old_checker_p> must initially (once
15066 per process) contain a null pointer. A C variable of static duration
15067 (declared at file scope, typically also marked C<static> to give
15068 it internal linkage) will be implicitly initialised appropriately,
15069 if it does not have an explicit initialiser. This function will only
15070 actually modify the check chain if it finds C<*old_checker_p> to be null.
15071 This function is also thread safe on the small scale. It uses appropriate
15072 locking to avoid race conditions in accessing L</PL_check>.
15074 When this function is called, the function referenced by C<new_checker>
15075 must be ready to be called, except for C<*old_checker_p> being unfilled.
15076 In a threading situation, C<new_checker> may be called immediately,
15077 even before this function has returned. C<*old_checker_p> will always
15078 be appropriately set before C<new_checker> is called. If C<new_checker>
15079 decides not to do anything special with an op that it is given (which
15080 is the usual case for most uses of op check hooking), it must chain the
15081 check function referenced by C<*old_checker_p>.
15083 If you want to influence compilation of calls to a specific subroutine,
15084 then use L</cv_set_call_checker> rather than hooking checking of all
15091 Perl_wrap_op_checker(pTHX_ Optype opcode,
15092 Perl_check_t new_checker, Perl_check_t *old_checker_p)
15096 PERL_UNUSED_CONTEXT;
15097 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
15098 if (*old_checker_p) return;
15099 OP_CHECK_MUTEX_LOCK;
15100 if (!*old_checker_p) {
15101 *old_checker_p = PL_check[opcode];
15102 PL_check[opcode] = new_checker;
15104 OP_CHECK_MUTEX_UNLOCK;
15109 /* Efficient sub that returns a constant scalar value. */
15111 const_sv_xsub(pTHX_ CV* cv)
15114 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
15115 PERL_UNUSED_ARG(items);
15125 const_av_xsub(pTHX_ CV* cv)
15128 AV * const av = MUTABLE_AV(XSANY.any_ptr);
15136 if (SvRMAGICAL(av))
15137 Perl_croak(aTHX_ "Magical list constants are not supported");
15138 if (GIMME_V != G_ARRAY) {
15140 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
15143 EXTEND(SP, AvFILLp(av)+1);
15144 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
15145 XSRETURN(AvFILLp(av)+1);
15150 * ex: set ts=8 sts=4 sw=4 et: