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 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
38 * newBINOP(OP_ADD, flags,
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
52 An execution-order pass
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines. The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order. (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again). As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node. But
70 it's still not the real execution order.
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer. At that point, we can call
75 into peep() to do that code's portion of the 3rd pass. It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80 get the compile time state of %^H for that block. Storing %^H in every
81 block (or even COP) would be very expensive, so a different approach is
82 taken. The (running) state of %^H is serialised into a tree of HE-like
83 structs. Stores into %^H are chained onto the current leaf as a struct
84 refcounted_he * with the key and the value. Deletes from %^H are saved
85 with a value of PL_sv_placeholder. The state of %^H at any point can be
86 turned back into a regular HV by walking back up the tree from that point's
87 leaf, ignoring any key you've already seen (placeholder or not), storing
88 the rest into the HV structure, then removing the placeholders. Hence
89 memory is only used to store the %^H deltas from the enclosing COP, rather
90 than the entire %^H on each COP.
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98 it will be correctly restored when any inner compiling scope is exited.
104 #include "keywords.h"
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
112 /* Used to avoid recursion through the op tree in scalarvoid() and
116 #define DEFERRED_OP_STEP 100
117 #define DEFER_OP(o) \
119 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \
120 defer_stack_alloc += DEFERRED_OP_STEP; \
121 assert(defer_stack_alloc > 0); \
122 Renew(defer_stack, defer_stack_alloc, OP *); \
124 defer_stack[++defer_ix] = o; \
127 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
129 /* remove any leading "empty" ops from the op_next chain whose first
130 * node's address is stored in op_p. Store the updated address of the
131 * first node in op_p.
135 S_prune_chain_head(OP** op_p)
138 && ( (*op_p)->op_type == OP_NULL
139 || (*op_p)->op_type == OP_SCOPE
140 || (*op_p)->op_type == OP_SCALAR
141 || (*op_p)->op_type == OP_LINESEQ)
143 *op_p = (*op_p)->op_next;
147 /* See the explanatory comments above struct opslab in op.h. */
149 #ifdef PERL_DEBUG_READONLY_OPS
150 # define PERL_SLAB_SIZE 128
151 # define PERL_MAX_SLAB_SIZE 4096
152 # include <sys/mman.h>
155 #ifndef PERL_SLAB_SIZE
156 # define PERL_SLAB_SIZE 64
158 #ifndef PERL_MAX_SLAB_SIZE
159 # define PERL_MAX_SLAB_SIZE 2048
162 /* rounds up to nearest pointer */
163 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
164 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
167 S_new_slab(pTHX_ size_t sz)
169 #ifdef PERL_DEBUG_READONLY_OPS
170 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
171 PROT_READ|PROT_WRITE,
172 MAP_ANON|MAP_PRIVATE, -1, 0);
173 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
174 (unsigned long) sz, slab));
175 if (slab == MAP_FAILED) {
176 perror("mmap failed");
179 slab->opslab_size = (U16)sz;
181 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
184 /* The context is unused in non-Windows */
187 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
191 /* requires double parens and aTHX_ */
192 #define DEBUG_S_warn(args) \
194 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
198 Perl_Slab_Alloc(pTHX_ size_t sz)
206 /* We only allocate ops from the slab during subroutine compilation.
207 We find the slab via PL_compcv, hence that must be non-NULL. It could
208 also be pointing to a subroutine which is now fully set up (CvROOT()
209 pointing to the top of the optree for that sub), or a subroutine
210 which isn't using the slab allocator. If our sanity checks aren't met,
211 don't use a slab, but allocate the OP directly from the heap. */
212 if (!PL_compcv || CvROOT(PL_compcv)
213 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
215 o = (OP*)PerlMemShared_calloc(1, sz);
219 /* While the subroutine is under construction, the slabs are accessed via
220 CvSTART(), to avoid needing to expand PVCV by one pointer for something
221 unneeded at runtime. Once a subroutine is constructed, the slabs are
222 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
223 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
225 if (!CvSTART(PL_compcv)) {
227 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
228 CvSLABBED_on(PL_compcv);
229 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
231 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
233 opsz = SIZE_TO_PSIZE(sz);
234 sz = opsz + OPSLOT_HEADER_P;
236 /* The slabs maintain a free list of OPs. In particular, constant folding
237 will free up OPs, so it makes sense to re-use them where possible. A
238 freed up slot is used in preference to a new allocation. */
239 if (slab->opslab_freed) {
240 OP **too = &slab->opslab_freed;
242 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
243 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
244 DEBUG_S_warn((aTHX_ "Alas! too small"));
245 o = *(too = &o->op_next);
246 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
250 Zero(o, opsz, I32 *);
256 #define INIT_OPSLOT \
257 slot->opslot_slab = slab; \
258 slot->opslot_next = slab2->opslab_first; \
259 slab2->opslab_first = slot; \
260 o = &slot->opslot_op; \
263 /* The partially-filled slab is next in the chain. */
264 slab2 = slab->opslab_next ? slab->opslab_next : slab;
265 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
266 /* Remaining space is too small. */
268 /* If we can fit a BASEOP, add it to the free chain, so as not
270 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
271 slot = &slab2->opslab_slots;
273 o->op_type = OP_FREED;
274 o->op_next = slab->opslab_freed;
275 slab->opslab_freed = o;
278 /* Create a new slab. Make this one twice as big. */
279 slot = slab2->opslab_first;
280 while (slot->opslot_next) slot = slot->opslot_next;
281 slab2 = S_new_slab(aTHX_
282 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
284 : (DIFF(slab2, slot)+1)*2);
285 slab2->opslab_next = slab->opslab_next;
286 slab->opslab_next = slab2;
288 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
290 /* Create a new op slot */
291 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
292 assert(slot >= &slab2->opslab_slots);
293 if (DIFF(&slab2->opslab_slots, slot)
294 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
295 slot = &slab2->opslab_slots;
297 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
300 #ifdef PERL_OP_PARENT
301 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
302 assert(!o->op_moresib);
303 assert(!o->op_sibparent);
311 #ifdef PERL_DEBUG_READONLY_OPS
313 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
315 PERL_ARGS_ASSERT_SLAB_TO_RO;
317 if (slab->opslab_readonly) return;
318 slab->opslab_readonly = 1;
319 for (; slab; slab = slab->opslab_next) {
320 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
321 (unsigned long) slab->opslab_size, slab));*/
322 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
323 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
324 (unsigned long)slab->opslab_size, errno);
329 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
333 PERL_ARGS_ASSERT_SLAB_TO_RW;
335 if (!slab->opslab_readonly) return;
337 for (; slab2; slab2 = slab2->opslab_next) {
338 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
339 (unsigned long) size, slab2));*/
340 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
341 PROT_READ|PROT_WRITE)) {
342 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
343 (unsigned long)slab2->opslab_size, errno);
346 slab->opslab_readonly = 0;
350 # define Slab_to_rw(op) NOOP
353 /* This cannot possibly be right, but it was copied from the old slab
354 allocator, to which it was originally added, without explanation, in
357 # define PerlMemShared PerlMem
361 Perl_Slab_Free(pTHX_ void *op)
363 OP * const o = (OP *)op;
366 PERL_ARGS_ASSERT_SLAB_FREE;
368 if (!o->op_slabbed) {
370 PerlMemShared_free(op);
375 /* If this op is already freed, our refcount will get screwy. */
376 assert(o->op_type != OP_FREED);
377 o->op_type = OP_FREED;
378 o->op_next = slab->opslab_freed;
379 slab->opslab_freed = o;
380 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
381 OpslabREFCNT_dec_padok(slab);
385 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
387 const bool havepad = !!PL_comppad;
388 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
391 PAD_SAVE_SETNULLPAD();
398 Perl_opslab_free(pTHX_ OPSLAB *slab)
401 PERL_ARGS_ASSERT_OPSLAB_FREE;
403 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
404 assert(slab->opslab_refcnt == 1);
406 slab2 = slab->opslab_next;
408 slab->opslab_refcnt = ~(size_t)0;
410 #ifdef PERL_DEBUG_READONLY_OPS
411 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
413 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
414 perror("munmap failed");
418 PerlMemShared_free(slab);
425 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
430 size_t savestack_count = 0;
432 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
435 for (slot = slab2->opslab_first;
437 slot = slot->opslot_next) {
438 if (slot->opslot_op.op_type != OP_FREED
439 && !(slot->opslot_op.op_savefree
445 assert(slot->opslot_op.op_slabbed);
446 op_free(&slot->opslot_op);
447 if (slab->opslab_refcnt == 1) goto free;
450 } while ((slab2 = slab2->opslab_next));
451 /* > 1 because the CV still holds a reference count. */
452 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
454 assert(savestack_count == slab->opslab_refcnt-1);
456 /* Remove the CV’s reference count. */
457 slab->opslab_refcnt--;
464 #ifdef PERL_DEBUG_READONLY_OPS
466 Perl_op_refcnt_inc(pTHX_ OP *o)
469 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
470 if (slab && slab->opslab_readonly) {
483 Perl_op_refcnt_dec(pTHX_ OP *o)
486 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
488 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
490 if (slab && slab->opslab_readonly) {
492 result = --o->op_targ;
495 result = --o->op_targ;
501 * In the following definition, the ", (OP*)0" is just to make the compiler
502 * think the expression is of the right type: croak actually does a Siglongjmp.
504 #define CHECKOP(type,o) \
505 ((PL_op_mask && PL_op_mask[type]) \
506 ? ( op_free((OP*)o), \
507 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
509 : PL_check[type](aTHX_ (OP*)o))
511 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
513 #define OpTYPE_set(o,type) \
515 o->op_type = (OPCODE)type; \
516 o->op_ppaddr = PL_ppaddr[type]; \
520 S_no_fh_allowed(pTHX_ OP *o)
522 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
524 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
530 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
532 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
533 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
538 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
540 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
542 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
547 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
549 PERL_ARGS_ASSERT_BAD_TYPE_PV;
551 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
552 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
555 /* remove flags var, its unused in all callers, move to to right end since gv
556 and kid are always the same */
558 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
560 SV * const namesv = cv_name((CV *)gv, NULL, 0);
561 PERL_ARGS_ASSERT_BAD_TYPE_GV;
563 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
564 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
568 S_no_bareword_allowed(pTHX_ OP *o)
570 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
572 qerror(Perl_mess(aTHX_
573 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
575 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
578 /* "register" allocation */
581 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
584 const bool is_our = (PL_parser->in_my == KEY_our);
586 PERL_ARGS_ASSERT_ALLOCMY;
588 if (flags & ~SVf_UTF8)
589 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
592 /* complain about "my $<special_var>" etc etc */
596 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
597 (name[1] == '_' && len > 2)))
599 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
601 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
602 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
603 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
604 PL_parser->in_my == KEY_state ? "state" : "my"));
606 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
607 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
611 /* allocate a spare slot and store the name in that slot */
613 off = pad_add_name_pvn(name, len,
614 (is_our ? padadd_OUR :
615 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
616 PL_parser->in_my_stash,
618 /* $_ is always in main::, even with our */
619 ? (PL_curstash && !memEQs(name,len,"$_")
625 /* anon sub prototypes contains state vars should always be cloned,
626 * otherwise the state var would be shared between anon subs */
628 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
629 CvCLONE_on(PL_compcv);
635 =head1 Optree Manipulation Functions
637 =for apidoc alloccopstash
639 Available only under threaded builds, this function allocates an entry in
640 C<PL_stashpad> for the stash passed to it.
647 Perl_alloccopstash(pTHX_ HV *hv)
649 PADOFFSET off = 0, o = 1;
650 bool found_slot = FALSE;
652 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
654 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
656 for (; o < PL_stashpadmax; ++o) {
657 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
658 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
659 found_slot = TRUE, off = o;
662 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
663 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
664 off = PL_stashpadmax;
665 PL_stashpadmax += 10;
668 PL_stashpad[PL_stashpadix = off] = hv;
673 /* free the body of an op without examining its contents.
674 * Always use this rather than FreeOp directly */
677 S_op_destroy(pTHX_ OP *o)
685 =for apidoc Am|void|op_free|OP *o
687 Free an op. Only use this when an op is no longer linked to from any
694 Perl_op_free(pTHX_ OP *o)
698 SSize_t defer_ix = -1;
699 SSize_t defer_stack_alloc = 0;
700 OP **defer_stack = NULL;
704 /* Though ops may be freed twice, freeing the op after its slab is a
706 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
707 /* During the forced freeing of ops after compilation failure, kidops
708 may be freed before their parents. */
709 if (!o || o->op_type == OP_FREED)
714 /* an op should only ever acquire op_private flags that we know about.
715 * If this fails, you may need to fix something in regen/op_private.
716 * Don't bother testing if:
717 * * the op_ppaddr doesn't match the op; someone may have
718 * overridden the op and be doing strange things with it;
719 * * we've errored, as op flags are often left in an
720 * inconsistent state then. Note that an error when
721 * compiling the main program leaves PL_parser NULL, so
722 * we can't spot faults in the main code, only
723 * evaled/required code */
725 if ( o->op_ppaddr == PL_ppaddr[o->op_type]
727 && !PL_parser->error_count)
729 assert(!(o->op_private & ~PL_op_private_valid[type]));
733 if (o->op_private & OPpREFCOUNTED) {
744 refcnt = OpREFCNT_dec(o);
747 /* Need to find and remove any pattern match ops from the list
748 we maintain for reset(). */
749 find_and_forget_pmops(o);
759 /* Call the op_free hook if it has been set. Do it now so that it's called
760 * at the right time for refcounted ops, but still before all of the kids
764 if (o->op_flags & OPf_KIDS) {
766 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
767 nextkid = OpSIBLING(kid); /* Get before next freeing kid */
768 if (!kid || kid->op_type == OP_FREED)
769 /* During the forced freeing of ops after
770 compilation failure, kidops may be freed before
773 if (!(kid->op_flags & OPf_KIDS))
774 /* If it has no kids, just free it now */
781 type = (OPCODE)o->op_targ;
784 Slab_to_rw(OpSLAB(o));
786 /* COP* is not cleared by op_clear() so that we may track line
787 * numbers etc even after null() */
788 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
794 #ifdef DEBUG_LEAKING_SCALARS
798 } while ( (o = POP_DEFERRED_OP()) );
800 Safefree(defer_stack);
803 /* S_op_clear_gv(): free a GV attached to an OP */
807 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
809 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
813 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
814 || o->op_type == OP_MULTIDEREF)
817 ? ((GV*)PAD_SVl(*ixp)) : NULL;
819 ? (GV*)(*svp) : NULL;
821 /* It's possible during global destruction that the GV is freed
822 before the optree. Whilst the SvREFCNT_inc is happy to bump from
823 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
824 will trigger an assertion failure, because the entry to sv_clear
825 checks that the scalar is not already freed. A check of for
826 !SvIS_FREED(gv) turns out to be invalid, because during global
827 destruction the reference count can be forced down to zero
828 (with SVf_BREAK set). In which case raising to 1 and then
829 dropping to 0 triggers cleanup before it should happen. I
830 *think* that this might actually be a general, systematic,
831 weakness of the whole idea of SVf_BREAK, in that code *is*
832 allowed to raise and lower references during global destruction,
833 so any *valid* code that happens to do this during global
834 destruction might well trigger premature cleanup. */
835 bool still_valid = gv && SvREFCNT(gv);
838 SvREFCNT_inc_simple_void(gv);
841 pad_swipe(*ixp, TRUE);
849 int try_downgrade = SvREFCNT(gv) == 2;
852 gv_try_downgrade(gv);
858 Perl_op_clear(pTHX_ OP *o)
863 PERL_ARGS_ASSERT_OP_CLEAR;
865 switch (o->op_type) {
866 case OP_NULL: /* Was holding old type, if any. */
869 case OP_ENTEREVAL: /* Was holding hints. */
873 if (!(o->op_flags & OPf_REF)
874 || (PL_check[o->op_type] != Perl_ck_ftst))
881 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
883 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
886 case OP_METHOD_REDIR:
887 case OP_METHOD_REDIR_SUPER:
889 if (cMETHOPx(o)->op_rclass_targ) {
890 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
891 cMETHOPx(o)->op_rclass_targ = 0;
894 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
895 cMETHOPx(o)->op_rclass_sv = NULL;
897 case OP_METHOD_NAMED:
898 case OP_METHOD_SUPER:
899 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
900 cMETHOPx(o)->op_u.op_meth_sv = NULL;
903 pad_swipe(o->op_targ, 1);
910 SvREFCNT_dec(cSVOPo->op_sv);
911 cSVOPo->op_sv = NULL;
914 Even if op_clear does a pad_free for the target of the op,
915 pad_free doesn't actually remove the sv that exists in the pad;
916 instead it lives on. This results in that it could be reused as
917 a target later on when the pad was reallocated.
920 pad_swipe(o->op_targ,1);
930 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
935 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
936 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
938 if (cPADOPo->op_padix > 0) {
939 pad_swipe(cPADOPo->op_padix, TRUE);
940 cPADOPo->op_padix = 0;
943 SvREFCNT_dec(cSVOPo->op_sv);
944 cSVOPo->op_sv = NULL;
948 PerlMemShared_free(cPVOPo->op_pv);
949 cPVOPo->op_pv = NULL;
953 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
957 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
958 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
961 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
967 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
968 op_free(cPMOPo->op_code_list);
969 cPMOPo->op_code_list = NULL;
971 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
972 /* we use the same protection as the "SAFE" version of the PM_ macros
973 * here since sv_clean_all might release some PMOPs
974 * after PL_regex_padav has been cleared
975 * and the clearing of PL_regex_padav needs to
976 * happen before sv_clean_all
979 if(PL_regex_pad) { /* We could be in destruction */
980 const IV offset = (cPMOPo)->op_pmoffset;
981 ReREFCNT_dec(PM_GETRE(cPMOPo));
982 PL_regex_pad[offset] = &PL_sv_undef;
983 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
987 ReREFCNT_dec(PM_GETRE(cPMOPo));
988 PM_SETRE(cPMOPo, NULL);
995 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
996 UV actions = items->uv;
998 bool is_hash = FALSE;
1001 switch (actions & MDEREF_ACTION_MASK) {
1004 actions = (++items)->uv;
1007 case MDEREF_HV_padhv_helem:
1009 case MDEREF_AV_padav_aelem:
1010 pad_free((++items)->pad_offset);
1013 case MDEREF_HV_gvhv_helem:
1015 case MDEREF_AV_gvav_aelem:
1017 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1019 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1023 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1025 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1027 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1029 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1031 goto do_vivify_rv2xv_elem;
1033 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1035 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1036 pad_free((++items)->pad_offset);
1037 goto do_vivify_rv2xv_elem;
1039 case MDEREF_HV_pop_rv2hv_helem:
1040 case MDEREF_HV_vivify_rv2hv_helem:
1042 do_vivify_rv2xv_elem:
1043 case MDEREF_AV_pop_rv2av_aelem:
1044 case MDEREF_AV_vivify_rv2av_aelem:
1046 switch (actions & MDEREF_INDEX_MASK) {
1047 case MDEREF_INDEX_none:
1050 case MDEREF_INDEX_const:
1054 pad_swipe((++items)->pad_offset, 1);
1056 SvREFCNT_dec((++items)->sv);
1062 case MDEREF_INDEX_padsv:
1063 pad_free((++items)->pad_offset);
1065 case MDEREF_INDEX_gvsv:
1067 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1069 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1074 if (actions & MDEREF_FLAG_last)
1087 actions >>= MDEREF_SHIFT;
1090 /* start of malloc is at op_aux[-1], where the length is
1092 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1097 if (o->op_targ > 0) {
1098 pad_free(o->op_targ);
1104 S_cop_free(pTHX_ COP* cop)
1106 PERL_ARGS_ASSERT_COP_FREE;
1109 if (! specialWARN(cop->cop_warnings))
1110 PerlMemShared_free(cop->cop_warnings);
1111 cophh_free(CopHINTHASH_get(cop));
1112 if (PL_curcop == cop)
1117 S_forget_pmop(pTHX_ PMOP *const o
1120 HV * const pmstash = PmopSTASH(o);
1122 PERL_ARGS_ASSERT_FORGET_PMOP;
1124 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1125 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1127 PMOP **const array = (PMOP**) mg->mg_ptr;
1128 U32 count = mg->mg_len / sizeof(PMOP**);
1132 if (array[i] == o) {
1133 /* Found it. Move the entry at the end to overwrite it. */
1134 array[i] = array[--count];
1135 mg->mg_len = count * sizeof(PMOP**);
1136 /* Could realloc smaller at this point always, but probably
1137 not worth it. Probably worth free()ing if we're the
1140 Safefree(mg->mg_ptr);
1153 S_find_and_forget_pmops(pTHX_ OP *o)
1155 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1157 if (o->op_flags & OPf_KIDS) {
1158 OP *kid = cUNOPo->op_first;
1160 switch (kid->op_type) {
1165 forget_pmop((PMOP*)kid);
1167 find_and_forget_pmops(kid);
1168 kid = OpSIBLING(kid);
1174 =for apidoc Am|void|op_null|OP *o
1176 Neutralizes an op when it is no longer needed, but is still linked to from
1183 Perl_op_null(pTHX_ OP *o)
1187 PERL_ARGS_ASSERT_OP_NULL;
1189 if (o->op_type == OP_NULL)
1192 o->op_targ = o->op_type;
1193 OpTYPE_set(o, OP_NULL);
1197 Perl_op_refcnt_lock(pTHX)
1198 PERL_TSA_ACQUIRE(PL_op_mutex)
1203 PERL_UNUSED_CONTEXT;
1208 Perl_op_refcnt_unlock(pTHX)
1209 PERL_TSA_RELEASE(PL_op_mutex)
1214 PERL_UNUSED_CONTEXT;
1220 =for apidoc op_sibling_splice
1222 A general function for editing the structure of an existing chain of
1223 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1224 you to delete zero or more sequential nodes, replacing them with zero or
1225 more different nodes. Performs the necessary op_first/op_last
1226 housekeeping on the parent node and op_sibling manipulation on the
1227 children. The last deleted node will be marked as as the last node by
1228 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1230 Note that op_next is not manipulated, and nodes are not freed; that is the
1231 responsibility of the caller. It also won't create a new list op for an
1232 empty list etc; use higher-level functions like op_append_elem() for that.
1234 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1235 the splicing doesn't affect the first or last op in the chain.
1237 C<start> is the node preceding the first node to be spliced. Node(s)
1238 following it will be deleted, and ops will be inserted after it. If it is
1239 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1242 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1243 If -1 or greater than or equal to the number of remaining kids, all
1244 remaining kids are deleted.
1246 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1247 If C<NULL>, no nodes are inserted.
1249 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1254 action before after returns
1255 ------ ----- ----- -------
1258 splice(P, A, 2, X-Y-Z) | | B-C
1262 splice(P, NULL, 1, X-Y) | | A
1266 splice(P, NULL, 3, NULL) | | A-B-C
1270 splice(P, B, 0, X-Y) | | NULL
1274 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1275 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1281 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1285 OP *last_del = NULL;
1286 OP *last_ins = NULL;
1289 first = OpSIBLING(start);
1293 first = cLISTOPx(parent)->op_first;
1295 assert(del_count >= -1);
1297 if (del_count && first) {
1299 while (--del_count && OpHAS_SIBLING(last_del))
1300 last_del = OpSIBLING(last_del);
1301 rest = OpSIBLING(last_del);
1302 OpLASTSIB_set(last_del, NULL);
1309 while (OpHAS_SIBLING(last_ins))
1310 last_ins = OpSIBLING(last_ins);
1311 OpMAYBESIB_set(last_ins, rest, NULL);
1317 OpMAYBESIB_set(start, insert, NULL);
1322 cLISTOPx(parent)->op_first = insert;
1324 parent->op_flags |= OPf_KIDS;
1326 parent->op_flags &= ~OPf_KIDS;
1330 /* update op_last etc */
1337 /* ought to use OP_CLASS(parent) here, but that can't handle
1338 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1340 type = parent->op_type;
1341 if (type == OP_CUSTOM) {
1343 type = XopENTRYCUSTOM(parent, xop_class);
1346 if (type == OP_NULL)
1347 type = parent->op_targ;
1348 type = PL_opargs[type] & OA_CLASS_MASK;
1351 lastop = last_ins ? last_ins : start ? start : NULL;
1352 if ( type == OA_BINOP
1353 || type == OA_LISTOP
1357 cLISTOPx(parent)->op_last = lastop;
1360 OpLASTSIB_set(lastop, parent);
1362 return last_del ? first : NULL;
1365 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1369 #ifdef PERL_OP_PARENT
1372 =for apidoc op_parent
1374 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1375 This function is only available on perls built with C<-DPERL_OP_PARENT>.
1381 Perl_op_parent(OP *o)
1383 PERL_ARGS_ASSERT_OP_PARENT;
1384 while (OpHAS_SIBLING(o))
1386 return o->op_sibparent;
1392 /* replace the sibling following start with a new UNOP, which becomes
1393 * the parent of the original sibling; e.g.
1395 * op_sibling_newUNOP(P, A, unop-args...)
1403 * where U is the new UNOP.
1405 * parent and start args are the same as for op_sibling_splice();
1406 * type and flags args are as newUNOP().
1408 * Returns the new UNOP.
1412 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1416 kid = op_sibling_splice(parent, start, 1, NULL);
1417 newop = newUNOP(type, flags, kid);
1418 op_sibling_splice(parent, start, 0, newop);
1423 /* lowest-level newLOGOP-style function - just allocates and populates
1424 * the struct. Higher-level stuff should be done by S_new_logop() /
1425 * newLOGOP(). This function exists mainly to avoid op_first assignment
1426 * being spread throughout this file.
1430 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1435 NewOp(1101, logop, 1, LOGOP);
1436 OpTYPE_set(logop, type);
1437 logop->op_first = first;
1438 logop->op_other = other;
1439 logop->op_flags = OPf_KIDS;
1440 while (kid && OpHAS_SIBLING(kid))
1441 kid = OpSIBLING(kid);
1443 OpLASTSIB_set(kid, (OP*)logop);
1448 /* Contextualizers */
1451 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1453 Applies a syntactic context to an op tree representing an expression.
1454 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1455 or C<G_VOID> to specify the context to apply. The modified op tree
1462 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1464 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1466 case G_SCALAR: return scalar(o);
1467 case G_ARRAY: return list(o);
1468 case G_VOID: return scalarvoid(o);
1470 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1477 =for apidoc Am|OP*|op_linklist|OP *o
1478 This function is the implementation of the L</LINKLIST> macro. It should
1479 not be called directly.
1485 Perl_op_linklist(pTHX_ OP *o)
1489 PERL_ARGS_ASSERT_OP_LINKLIST;
1494 /* establish postfix order */
1495 first = cUNOPo->op_first;
1498 o->op_next = LINKLIST(first);
1501 OP *sibl = OpSIBLING(kid);
1503 kid->op_next = LINKLIST(sibl);
1518 S_scalarkids(pTHX_ OP *o)
1520 if (o && o->op_flags & OPf_KIDS) {
1522 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1529 S_scalarboolean(pTHX_ OP *o)
1531 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1533 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1534 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1535 if (ckWARN(WARN_SYNTAX)) {
1536 const line_t oldline = CopLINE(PL_curcop);
1538 if (PL_parser && PL_parser->copline != NOLINE) {
1539 /* This ensures that warnings are reported at the first line
1540 of the conditional, not the last. */
1541 CopLINE_set(PL_curcop, PL_parser->copline);
1543 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1544 CopLINE_set(PL_curcop, oldline);
1551 S_op_varname(pTHX_ const OP *o)
1554 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1555 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1557 const char funny = o->op_type == OP_PADAV
1558 || o->op_type == OP_RV2AV ? '@' : '%';
1559 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1561 if (cUNOPo->op_first->op_type != OP_GV
1562 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1564 return varname(gv, funny, 0, NULL, 0, 1);
1567 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1572 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1573 { /* or not so pretty :-) */
1574 if (o->op_type == OP_CONST) {
1576 if (SvPOK(*retsv)) {
1578 *retsv = sv_newmortal();
1579 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1580 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1582 else if (!SvOK(*retsv))
1585 else *retpv = "...";
1589 S_scalar_slice_warning(pTHX_ const OP *o)
1593 o->op_type == OP_HSLICE ? '{' : '[';
1595 o->op_type == OP_HSLICE ? '}' : ']';
1597 SV *keysv = NULL; /* just to silence compiler warnings */
1598 const char *key = NULL;
1600 if (!(o->op_private & OPpSLICEWARNING))
1602 if (PL_parser && PL_parser->error_count)
1603 /* This warning can be nonsensical when there is a syntax error. */
1606 kid = cLISTOPo->op_first;
1607 kid = OpSIBLING(kid); /* get past pushmark */
1608 /* weed out false positives: any ops that can return lists */
1609 switch (kid->op_type) {
1635 /* Don't warn if we have a nulled list either. */
1636 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1639 assert(OpSIBLING(kid));
1640 name = S_op_varname(aTHX_ OpSIBLING(kid));
1641 if (!name) /* XS module fiddling with the op tree */
1643 S_op_pretty(aTHX_ kid, &keysv, &key);
1644 assert(SvPOK(name));
1645 sv_chop(name,SvPVX(name)+1);
1647 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1648 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1649 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1651 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1652 lbrack, key, rbrack);
1654 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1655 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1656 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1658 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1659 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1663 Perl_scalar(pTHX_ OP *o)
1667 /* assumes no premature commitment */
1668 if (!o || (PL_parser && PL_parser->error_count)
1669 || (o->op_flags & OPf_WANT)
1670 || o->op_type == OP_RETURN)
1675 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1677 switch (o->op_type) {
1679 scalar(cBINOPo->op_first);
1680 if (o->op_private & OPpREPEAT_DOLIST) {
1681 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1682 assert(kid->op_type == OP_PUSHMARK);
1683 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1684 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1685 o->op_private &=~ OPpREPEAT_DOLIST;
1692 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
1702 if (o->op_flags & OPf_KIDS) {
1703 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
1709 kid = cLISTOPo->op_first;
1711 kid = OpSIBLING(kid);
1714 OP *sib = OpSIBLING(kid);
1715 if (sib && kid->op_type != OP_LEAVEWHEN
1716 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
1717 || ( sib->op_targ != OP_NEXTSTATE
1718 && sib->op_targ != OP_DBSTATE )))
1724 PL_curcop = &PL_compiling;
1729 kid = cLISTOPo->op_first;
1732 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1737 /* Warn about scalar context */
1738 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1739 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1742 const char *key = NULL;
1744 /* This warning can be nonsensical when there is a syntax error. */
1745 if (PL_parser && PL_parser->error_count)
1748 if (!ckWARN(WARN_SYNTAX)) break;
1750 kid = cLISTOPo->op_first;
1751 kid = OpSIBLING(kid); /* get past pushmark */
1752 assert(OpSIBLING(kid));
1753 name = S_op_varname(aTHX_ OpSIBLING(kid));
1754 if (!name) /* XS module fiddling with the op tree */
1756 S_op_pretty(aTHX_ kid, &keysv, &key);
1757 assert(SvPOK(name));
1758 sv_chop(name,SvPVX(name)+1);
1760 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1761 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1762 "%%%"SVf"%c%s%c in scalar context better written "
1764 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1765 lbrack, key, rbrack);
1767 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1768 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1769 "%%%"SVf"%c%"SVf"%c in scalar context better "
1770 "written as $%"SVf"%c%"SVf"%c",
1771 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1772 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1779 Perl_scalarvoid(pTHX_ OP *arg)
1785 SSize_t defer_stack_alloc = 0;
1786 SSize_t defer_ix = -1;
1787 OP **defer_stack = NULL;
1790 PERL_ARGS_ASSERT_SCALARVOID;
1793 SV *useless_sv = NULL;
1794 const char* useless = NULL;
1796 if (o->op_type == OP_NEXTSTATE
1797 || o->op_type == OP_DBSTATE
1798 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1799 || o->op_targ == OP_DBSTATE)))
1800 PL_curcop = (COP*)o; /* for warning below */
1802 /* assumes no premature commitment */
1803 want = o->op_flags & OPf_WANT;
1804 if ((want && want != OPf_WANT_SCALAR)
1805 || (PL_parser && PL_parser->error_count)
1806 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1811 if ((o->op_private & OPpTARGET_MY)
1812 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1814 /* newASSIGNOP has already applied scalar context, which we
1815 leave, as if this op is inside SASSIGN. */
1819 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1821 switch (o->op_type) {
1823 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1827 if (o->op_flags & OPf_STACKED)
1829 if (o->op_type == OP_REPEAT)
1830 scalar(cBINOPo->op_first);
1833 if (o->op_private == 4)
1868 case OP_GETSOCKNAME:
1869 case OP_GETPEERNAME:
1874 case OP_GETPRIORITY:
1899 useless = OP_DESC(o);
1909 case OP_AELEMFAST_LEX:
1913 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1914 /* Otherwise it's "Useless use of grep iterator" */
1915 useless = OP_DESC(o);
1919 kid = cLISTOPo->op_first;
1920 if (kid && kid->op_type == OP_PUSHRE
1922 && !(o->op_flags & OPf_STACKED)
1924 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
1926 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
1929 useless = OP_DESC(o);
1933 kid = cUNOPo->op_first;
1934 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1935 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1938 useless = "negative pattern binding (!~)";
1942 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1943 useless = "non-destructive substitution (s///r)";
1947 useless = "non-destructive transliteration (tr///r)";
1954 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1955 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
1956 useless = "a variable";
1961 if (cSVOPo->op_private & OPpCONST_STRICT)
1962 no_bareword_allowed(o);
1964 if (ckWARN(WARN_VOID)) {
1966 /* don't warn on optimised away booleans, eg
1967 * use constant Foo, 5; Foo || print; */
1968 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1970 /* the constants 0 and 1 are permitted as they are
1971 conventionally used as dummies in constructs like
1972 1 while some_condition_with_side_effects; */
1973 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
1975 else if (SvPOK(sv)) {
1976 SV * const dsv = newSVpvs("");
1978 = Perl_newSVpvf(aTHX_
1980 pv_pretty(dsv, SvPVX_const(sv),
1981 SvCUR(sv), 32, NULL, NULL,
1983 | PERL_PV_ESCAPE_NOCLEAR
1984 | PERL_PV_ESCAPE_UNI_DETECT));
1985 SvREFCNT_dec_NN(dsv);
1987 else if (SvOK(sv)) {
1988 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
1991 useless = "a constant (undef)";
1994 op_null(o); /* don't execute or even remember it */
1998 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2002 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2006 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2010 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2015 UNOP *refgen, *rv2cv;
2018 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2021 rv2gv = ((BINOP *)o)->op_last;
2022 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2025 refgen = (UNOP *)((BINOP *)o)->op_first;
2027 if (!refgen || (refgen->op_type != OP_REFGEN
2028 && refgen->op_type != OP_SREFGEN))
2031 exlist = (LISTOP *)refgen->op_first;
2032 if (!exlist || exlist->op_type != OP_NULL
2033 || exlist->op_targ != OP_LIST)
2036 if (exlist->op_first->op_type != OP_PUSHMARK
2037 && exlist->op_first != exlist->op_last)
2040 rv2cv = (UNOP*)exlist->op_last;
2042 if (rv2cv->op_type != OP_RV2CV)
2045 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2046 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2047 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2049 o->op_private |= OPpASSIGN_CV_TO_GV;
2050 rv2gv->op_private |= OPpDONT_INIT_GV;
2051 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2063 kid = cLOGOPo->op_first;
2064 if (kid->op_type == OP_NOT
2065 && (kid->op_flags & OPf_KIDS)) {
2066 if (o->op_type == OP_AND) {
2067 OpTYPE_set(o, OP_OR);
2069 OpTYPE_set(o, OP_AND);
2079 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2080 if (!(kid->op_flags & OPf_KIDS))
2087 if (o->op_flags & OPf_STACKED)
2094 if (!(o->op_flags & OPf_KIDS))
2105 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2106 if (!(kid->op_flags & OPf_KIDS))
2112 /* If the first kid after pushmark is something that the padrange
2113 optimisation would reject, then null the list and the pushmark.
2115 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2116 && ( !(kid = OpSIBLING(kid))
2117 || ( kid->op_type != OP_PADSV
2118 && kid->op_type != OP_PADAV
2119 && kid->op_type != OP_PADHV)
2120 || kid->op_private & ~OPpLVAL_INTRO
2121 || !(kid = OpSIBLING(kid))
2122 || ( kid->op_type != OP_PADSV
2123 && kid->op_type != OP_PADAV
2124 && kid->op_type != OP_PADHV)
2125 || kid->op_private & ~OPpLVAL_INTRO)
2127 op_null(cUNOPo->op_first); /* NULL the pushmark */
2128 op_null(o); /* NULL the list */
2140 /* mortalise it, in case warnings are fatal. */
2141 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2142 "Useless use of %"SVf" in void context",
2143 SVfARG(sv_2mortal(useless_sv)));
2146 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2147 "Useless use of %s in void context",
2150 } while ( (o = POP_DEFERRED_OP()) );
2152 Safefree(defer_stack);
2158 S_listkids(pTHX_ OP *o)
2160 if (o && o->op_flags & OPf_KIDS) {
2162 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2169 Perl_list(pTHX_ OP *o)
2173 /* assumes no premature commitment */
2174 if (!o || (o->op_flags & OPf_WANT)
2175 || (PL_parser && PL_parser->error_count)
2176 || o->op_type == OP_RETURN)
2181 if ((o->op_private & OPpTARGET_MY)
2182 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2184 return o; /* As if inside SASSIGN */
2187 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2189 switch (o->op_type) {
2191 list(cBINOPo->op_first);
2194 if (o->op_private & OPpREPEAT_DOLIST
2195 && !(o->op_flags & OPf_STACKED))
2197 list(cBINOPo->op_first);
2198 kid = cBINOPo->op_last;
2199 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2200 && SvIVX(kSVOP_sv) == 1)
2202 op_null(o); /* repeat */
2203 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2205 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2212 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2220 if (!(o->op_flags & OPf_KIDS))
2222 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2223 list(cBINOPo->op_first);
2224 return gen_constant_list(o);
2230 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2231 op_null(cUNOPo->op_first); /* NULL the pushmark */
2232 op_null(o); /* NULL the list */
2237 kid = cLISTOPo->op_first;
2239 kid = OpSIBLING(kid);
2242 OP *sib = OpSIBLING(kid);
2243 if (sib && kid->op_type != OP_LEAVEWHEN)
2249 PL_curcop = &PL_compiling;
2253 kid = cLISTOPo->op_first;
2260 S_scalarseq(pTHX_ OP *o)
2263 const OPCODE type = o->op_type;
2265 if (type == OP_LINESEQ || type == OP_SCOPE ||
2266 type == OP_LEAVE || type == OP_LEAVETRY)
2269 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2270 if ((sib = OpSIBLING(kid))
2271 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2272 || ( sib->op_targ != OP_NEXTSTATE
2273 && sib->op_targ != OP_DBSTATE )))
2278 PL_curcop = &PL_compiling;
2280 o->op_flags &= ~OPf_PARENS;
2281 if (PL_hints & HINT_BLOCK_SCOPE)
2282 o->op_flags |= OPf_PARENS;
2285 o = newOP(OP_STUB, 0);
2290 S_modkids(pTHX_ OP *o, I32 type)
2292 if (o && o->op_flags & OPf_KIDS) {
2294 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2295 op_lvalue(kid, type);
2301 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2302 * const fields. Also, convert CONST keys to HEK-in-SVs.
2303 * rop is the op that retrieves the hash;
2304 * key_op is the first key
2308 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op)
2314 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2316 if (rop->op_first->op_type == OP_PADSV)
2317 /* @$hash{qw(keys here)} */
2318 rop = (UNOP*)rop->op_first;
2320 /* @{$hash}{qw(keys here)} */
2321 if (rop->op_first->op_type == OP_SCOPE
2322 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2324 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2331 lexname = NULL; /* just to silence compiler warnings */
2332 fields = NULL; /* just to silence compiler warnings */
2336 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2337 SvPAD_TYPED(lexname))
2338 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2339 && isGV(*fields) && GvHV(*fields);
2341 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2343 if (key_op->op_type != OP_CONST)
2345 svp = cSVOPx_svp(key_op);
2347 /* make sure it's not a bareword under strict subs */
2348 if (key_op->op_private & OPpCONST_BARE &&
2349 key_op->op_private & OPpCONST_STRICT)
2351 no_bareword_allowed((OP*)key_op);
2354 /* Make the CONST have a shared SV */
2355 if ( !SvIsCOW_shared_hash(sv = *svp)
2356 && SvTYPE(sv) < SVt_PVMG
2361 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2362 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2363 SvREFCNT_dec_NN(sv);
2368 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2370 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2371 "in variable %"PNf" of type %"HEKf,
2372 SVfARG(*svp), PNfARG(lexname),
2373 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2380 =for apidoc finalize_optree
2382 This function finalizes the optree. Should be called directly after
2383 the complete optree is built. It does some additional
2384 checking which can't be done in the normal C<ck_>xxx functions and makes
2385 the tree thread-safe.
2390 Perl_finalize_optree(pTHX_ OP* o)
2392 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2395 SAVEVPTR(PL_curcop);
2403 /* Relocate sv to the pad for thread safety.
2404 * Despite being a "constant", the SV is written to,
2405 * for reference counts, sv_upgrade() etc. */
2406 PERL_STATIC_INLINE void
2407 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2410 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2412 ix = pad_alloc(OP_CONST, SVf_READONLY);
2413 SvREFCNT_dec(PAD_SVl(ix));
2414 PAD_SETSV(ix, *svp);
2415 /* XXX I don't know how this isn't readonly already. */
2416 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2424 S_finalize_op(pTHX_ OP* o)
2426 PERL_ARGS_ASSERT_FINALIZE_OP;
2429 switch (o->op_type) {
2432 PL_curcop = ((COP*)o); /* for warnings */
2435 if (OpHAS_SIBLING(o)) {
2436 OP *sib = OpSIBLING(o);
2437 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2438 && ckWARN(WARN_EXEC)
2439 && OpHAS_SIBLING(sib))
2441 const OPCODE type = OpSIBLING(sib)->op_type;
2442 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2443 const line_t oldline = CopLINE(PL_curcop);
2444 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2445 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2446 "Statement unlikely to be reached");
2447 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2448 "\t(Maybe you meant system() when you said exec()?)\n");
2449 CopLINE_set(PL_curcop, oldline);
2456 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2457 GV * const gv = cGVOPo_gv;
2458 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2459 /* XXX could check prototype here instead of just carping */
2460 SV * const sv = sv_newmortal();
2461 gv_efullname3(sv, gv, NULL);
2462 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2463 "%"SVf"() called too early to check prototype",
2470 if (cSVOPo->op_private & OPpCONST_STRICT)
2471 no_bareword_allowed(o);
2475 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2480 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2481 case OP_METHOD_NAMED:
2482 case OP_METHOD_SUPER:
2483 case OP_METHOD_REDIR:
2484 case OP_METHOD_REDIR_SUPER:
2485 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2494 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2497 rop = (UNOP*)((BINOP*)o)->op_first;
2502 S_scalar_slice_warning(aTHX_ o);
2506 kid = OpSIBLING(cLISTOPo->op_first);
2507 if (/* I bet there's always a pushmark... */
2508 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2509 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2514 key_op = (SVOP*)(kid->op_type == OP_CONST
2516 : OpSIBLING(kLISTOP->op_first));
2518 rop = (UNOP*)((LISTOP*)o)->op_last;
2521 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2523 S_check_hash_fields_and_hekify(aTHX_ rop, key_op);
2527 S_scalar_slice_warning(aTHX_ o);
2531 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2532 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2539 if (o->op_flags & OPf_KIDS) {
2543 /* check that op_last points to the last sibling, and that
2544 * the last op_sibling/op_sibparent field points back to the
2545 * parent, and that the only ops with KIDS are those which are
2546 * entitled to them */
2547 U32 type = o->op_type;
2551 if (type == OP_NULL) {
2553 /* ck_glob creates a null UNOP with ex-type GLOB
2554 * (which is a list op. So pretend it wasn't a listop */
2555 if (type == OP_GLOB)
2558 family = PL_opargs[type] & OA_CLASS_MASK;
2560 has_last = ( family == OA_BINOP
2561 || family == OA_LISTOP
2562 || family == OA_PMOP
2563 || family == OA_LOOP
2565 assert( has_last /* has op_first and op_last, or ...
2566 ... has (or may have) op_first: */
2567 || family == OA_UNOP
2568 || family == OA_UNOP_AUX
2569 || family == OA_LOGOP
2570 || family == OA_BASEOP_OR_UNOP
2571 || family == OA_FILESTATOP
2572 || family == OA_LOOPEXOP
2573 || family == OA_METHOP
2574 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */
2575 || type == OP_SASSIGN
2576 || type == OP_CUSTOM
2577 || type == OP_NULL /* new_logop does this */
2580 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2581 # ifdef PERL_OP_PARENT
2582 if (!OpHAS_SIBLING(kid)) {
2584 assert(kid == cLISTOPo->op_last);
2585 assert(kid->op_sibparent == o);
2588 if (has_last && !OpHAS_SIBLING(kid))
2589 assert(kid == cLISTOPo->op_last);
2594 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
2600 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2602 Propagate lvalue ("modifiable") context to an op and its children.
2603 C<type> represents the context type, roughly based on the type of op that
2604 would do the modifying, although C<local()> is represented by C<OP_NULL>,
2605 because it has no op type of its own (it is signalled by a flag on
2608 This function detects things that can't be modified, such as C<$x+1>, and
2609 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2610 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
2612 It also flags things that need to behave specially in an lvalue context,
2613 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2619 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2622 PadnameLVALUE_on(pn);
2623 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2626 assert(CvPADLIST(cv));
2628 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2629 assert(PadnameLEN(pn));
2630 PadnameLVALUE_on(pn);
2635 S_vivifies(const OPCODE type)
2638 case OP_RV2AV: case OP_ASLICE:
2639 case OP_RV2HV: case OP_KVASLICE:
2640 case OP_RV2SV: case OP_HSLICE:
2641 case OP_AELEMFAST: case OP_KVHSLICE:
2650 S_lvref(pTHX_ OP *o, I32 type)
2654 switch (o->op_type) {
2656 for (kid = OpSIBLING(cUNOPo->op_first); kid;
2657 kid = OpSIBLING(kid))
2658 S_lvref(aTHX_ kid, type);
2663 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2664 o->op_flags |= OPf_STACKED;
2665 if (o->op_flags & OPf_PARENS) {
2666 if (o->op_private & OPpLVAL_INTRO) {
2667 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2668 "localized parenthesized array in list assignment"));
2672 OpTYPE_set(o, OP_LVAVREF);
2673 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2674 o->op_flags |= OPf_MOD|OPf_REF;
2677 o->op_private |= OPpLVREF_AV;
2680 kid = cUNOPo->op_first;
2681 if (kid->op_type == OP_NULL)
2682 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
2684 o->op_private = OPpLVREF_CV;
2685 if (kid->op_type == OP_GV)
2686 o->op_flags |= OPf_STACKED;
2687 else if (kid->op_type == OP_PADCV) {
2688 o->op_targ = kid->op_targ;
2690 op_free(cUNOPo->op_first);
2691 cUNOPo->op_first = NULL;
2692 o->op_flags &=~ OPf_KIDS;
2697 if (o->op_flags & OPf_PARENS) {
2699 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2700 "parenthesized hash in list assignment"));
2703 o->op_private |= OPpLVREF_HV;
2707 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2708 o->op_flags |= OPf_STACKED;
2711 if (o->op_flags & OPf_PARENS) goto parenhash;
2712 o->op_private |= OPpLVREF_HV;
2715 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2718 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2719 if (o->op_flags & OPf_PARENS) goto slurpy;
2720 o->op_private |= OPpLVREF_AV;
2724 o->op_private |= OPpLVREF_ELEM;
2725 o->op_flags |= OPf_STACKED;
2729 OpTYPE_set(o, OP_LVREFSLICE);
2730 o->op_private &= OPpLVAL_INTRO|OPpLVREF_ELEM;
2733 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2735 else if (!(o->op_flags & OPf_KIDS))
2737 if (o->op_targ != OP_LIST) {
2738 S_lvref(aTHX_ cBINOPo->op_first, type);
2743 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
2744 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2745 S_lvref(aTHX_ kid, type);
2749 if (o->op_flags & OPf_PARENS)
2754 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2755 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2756 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2761 OpTYPE_set(o, OP_LVREF);
2763 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2764 if (type == OP_ENTERLOOP)
2765 o->op_private |= OPpLVREF_ITER;
2769 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2773 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2776 if (!o || (PL_parser && PL_parser->error_count))
2779 if ((o->op_private & OPpTARGET_MY)
2780 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2785 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2787 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2789 switch (o->op_type) {
2794 if ((o->op_flags & OPf_PARENS))
2798 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2799 !(o->op_flags & OPf_STACKED)) {
2800 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
2801 assert(cUNOPo->op_first->op_type == OP_NULL);
2802 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2805 else { /* lvalue subroutine call */
2806 o->op_private |= OPpLVAL_INTRO;
2807 PL_modcount = RETURN_UNLIMITED_NUMBER;
2808 if (type == OP_GREPSTART || type == OP_ENTERSUB
2809 || type == OP_REFGEN || type == OP_LEAVESUBLV) {
2810 /* Potential lvalue context: */
2811 o->op_private |= OPpENTERSUB_INARGS;
2814 else { /* Compile-time error message: */
2815 OP *kid = cUNOPo->op_first;
2820 if (kid->op_type != OP_PUSHMARK) {
2821 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2823 "panic: unexpected lvalue entersub "
2824 "args: type/targ %ld:%"UVuf,
2825 (long)kid->op_type, (UV)kid->op_targ);
2826 kid = kLISTOP->op_first;
2828 while (OpHAS_SIBLING(kid))
2829 kid = OpSIBLING(kid);
2830 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2831 break; /* Postpone until runtime */
2834 kid = kUNOP->op_first;
2835 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2836 kid = kUNOP->op_first;
2837 if (kid->op_type == OP_NULL)
2839 "Unexpected constant lvalue entersub "
2840 "entry via type/targ %ld:%"UVuf,
2841 (long)kid->op_type, (UV)kid->op_targ);
2842 if (kid->op_type != OP_GV) {
2849 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2850 ? MUTABLE_CV(SvRV(gv))
2856 if (flags & OP_LVALUE_NO_CROAK)
2859 namesv = cv_name(cv, NULL, 0);
2860 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
2861 "subroutine call of &%"SVf" in %s",
2862 SVfARG(namesv), PL_op_desc[type]),
2870 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2871 /* grep, foreach, subcalls, refgen */
2872 if (type == OP_GREPSTART || type == OP_ENTERSUB
2873 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2875 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2876 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2879 type ? PL_op_desc[type] : "local"));
2892 case OP_RIGHT_SHIFT:
2901 if (!(o->op_flags & OPf_STACKED))
2907 if (o->op_flags & OPf_STACKED) {
2911 if (!(o->op_private & OPpREPEAT_DOLIST))
2914 const I32 mods = PL_modcount;
2915 modkids(cBINOPo->op_first, type);
2916 if (type != OP_AASSIGN)
2918 kid = cBINOPo->op_last;
2919 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
2920 const IV iv = SvIV(kSVOP_sv);
2921 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
2923 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
2926 PL_modcount = RETURN_UNLIMITED_NUMBER;
2932 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2933 op_lvalue(kid, type);
2938 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2939 PL_modcount = RETURN_UNLIMITED_NUMBER;
2940 return o; /* Treat \(@foo) like ordinary list. */
2944 if (scalar_mod_type(o, type))
2946 ref(cUNOPo->op_first, o->op_type);
2953 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2954 if (type == OP_LEAVESUBLV && (
2955 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2956 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2958 o->op_private |= OPpMAYBE_LVSUB;
2962 PL_modcount = RETURN_UNLIMITED_NUMBER;
2966 if (type == OP_LEAVESUBLV)
2967 o->op_private |= OPpMAYBE_LVSUB;
2970 PL_hints |= HINT_BLOCK_SCOPE;
2971 if (type == OP_LEAVESUBLV)
2972 o->op_private |= OPpMAYBE_LVSUB;
2976 ref(cUNOPo->op_first, o->op_type);
2980 PL_hints |= HINT_BLOCK_SCOPE;
2990 case OP_AELEMFAST_LEX:
2997 PL_modcount = RETURN_UNLIMITED_NUMBER;
2998 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2999 return o; /* Treat \(@foo) like ordinary list. */
3000 if (scalar_mod_type(o, type))
3002 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3003 && type == OP_LEAVESUBLV)
3004 o->op_private |= OPpMAYBE_LVSUB;
3008 if (!type) /* local() */
3009 Perl_croak(aTHX_ "Can't localize lexical variable %"PNf,
3010 PNfARG(PAD_COMPNAME(o->op_targ)));
3011 if (!(o->op_private & OPpLVAL_INTRO)
3012 || ( type != OP_SASSIGN && type != OP_AASSIGN
3013 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
3014 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
3022 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
3026 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
3032 if (type == OP_LEAVESUBLV)
3033 o->op_private |= OPpMAYBE_LVSUB;
3034 if (o->op_flags & OPf_KIDS)
3035 op_lvalue(OpSIBLING(cBINOPo->op_first), type);
3040 ref(cBINOPo->op_first, o->op_type);
3041 if (type == OP_ENTERSUB &&
3042 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3043 o->op_private |= OPpLVAL_DEFER;
3044 if (type == OP_LEAVESUBLV)
3045 o->op_private |= OPpMAYBE_LVSUB;
3052 o->op_private |= OPpLVALUE;
3058 if (o->op_flags & OPf_KIDS)
3059 op_lvalue(cLISTOPo->op_last, type);
3064 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3066 else if (!(o->op_flags & OPf_KIDS))
3068 if (o->op_targ != OP_LIST) {
3069 op_lvalue(cBINOPo->op_first, type);
3075 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3076 /* elements might be in void context because the list is
3077 in scalar context or because they are attribute sub calls */
3078 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
3079 op_lvalue(kid, type);
3087 if (type == OP_LEAVESUBLV
3088 || !S_vivifies(cLOGOPo->op_first->op_type))
3089 op_lvalue(cLOGOPo->op_first, type);
3090 if (type == OP_LEAVESUBLV
3091 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3092 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
3096 if (type != OP_AASSIGN && type != OP_SASSIGN
3097 && type != OP_ENTERLOOP)
3099 /* Don’t bother applying lvalue context to the ex-list. */
3100 kid = cUNOPx(cUNOPo->op_first)->op_first;
3101 assert (!OpHAS_SIBLING(kid));
3104 if (type != OP_AASSIGN) goto nomod;
3105 kid = cUNOPo->op_first;
3108 const U8 ec = PL_parser ? PL_parser->error_count : 0;
3109 S_lvref(aTHX_ kid, type);
3110 if (!PL_parser || PL_parser->error_count == ec) {
3111 if (!FEATURE_REFALIASING_IS_ENABLED)
3113 "Experimental aliasing via reference not enabled");
3114 Perl_ck_warner_d(aTHX_
3115 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3116 "Aliasing via reference is experimental");
3119 if (o->op_type == OP_REFGEN)
3120 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3125 kid = cLISTOPo->op_first;
3126 if (kid && kid->op_type == OP_PUSHRE &&
3128 || o->op_flags & OPf_STACKED
3130 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
3132 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
3135 /* This is actually @array = split. */
3136 PL_modcount = RETURN_UNLIMITED_NUMBER;
3142 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3146 /* [20011101.069] File test operators interpret OPf_REF to mean that
3147 their argument is a filehandle; thus \stat(".") should not set
3149 if (type == OP_REFGEN &&
3150 PL_check[o->op_type] == Perl_ck_ftst)
3153 if (type != OP_LEAVESUBLV)
3154 o->op_flags |= OPf_MOD;
3156 if (type == OP_AASSIGN || type == OP_SASSIGN)
3157 o->op_flags |= OPf_SPECIAL|OPf_REF;
3158 else if (!type) { /* local() */
3161 o->op_private |= OPpLVAL_INTRO;
3162 o->op_flags &= ~OPf_SPECIAL;
3163 PL_hints |= HINT_BLOCK_SCOPE;
3168 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3169 "Useless localization of %s", OP_DESC(o));
3172 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3173 && type != OP_LEAVESUBLV)
3174 o->op_flags |= OPf_REF;
3179 S_scalar_mod_type(const OP *o, I32 type)
3184 if (o && o->op_type == OP_RV2GV)
3208 case OP_RIGHT_SHIFT:
3229 S_is_handle_constructor(const OP *o, I32 numargs)
3231 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3233 switch (o->op_type) {
3241 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3254 S_refkids(pTHX_ OP *o, I32 type)
3256 if (o && o->op_flags & OPf_KIDS) {
3258 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3265 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3270 PERL_ARGS_ASSERT_DOREF;
3272 if (PL_parser && PL_parser->error_count)
3275 switch (o->op_type) {
3277 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3278 !(o->op_flags & OPf_STACKED)) {
3279 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
3280 assert(cUNOPo->op_first->op_type == OP_NULL);
3281 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
3282 o->op_flags |= OPf_SPECIAL;
3284 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3285 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3286 : type == OP_RV2HV ? OPpDEREF_HV
3288 o->op_flags |= OPf_MOD;
3294 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3295 doref(kid, type, set_op_ref);
3298 if (type == OP_DEFINED)
3299 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3300 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3303 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3304 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3305 : type == OP_RV2HV ? OPpDEREF_HV
3307 o->op_flags |= OPf_MOD;
3314 o->op_flags |= OPf_REF;
3317 if (type == OP_DEFINED)
3318 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3319 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3325 o->op_flags |= OPf_REF;
3330 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3332 doref(cBINOPo->op_first, type, set_op_ref);
3336 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3337 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3338 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3339 : type == OP_RV2HV ? OPpDEREF_HV
3341 o->op_flags |= OPf_MOD;
3351 if (!(o->op_flags & OPf_KIDS))
3353 doref(cLISTOPo->op_last, type, set_op_ref);
3363 S_dup_attrlist(pTHX_ OP *o)
3367 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3369 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3370 * where the first kid is OP_PUSHMARK and the remaining ones
3371 * are OP_CONST. We need to push the OP_CONST values.
3373 if (o->op_type == OP_CONST)
3374 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3376 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3378 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
3379 if (o->op_type == OP_CONST)
3380 rop = op_append_elem(OP_LIST, rop,
3381 newSVOP(OP_CONST, o->op_flags,
3382 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3389 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3391 PERL_ARGS_ASSERT_APPLY_ATTRS;
3393 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
3395 /* fake up C<use attributes $pkg,$rv,@attrs> */
3397 #define ATTRSMODULE "attributes"
3398 #define ATTRSMODULE_PM "attributes.pm"
3401 aTHX_ PERL_LOADMOD_IMPORT_OPS,
3402 newSVpvs(ATTRSMODULE),
3404 op_prepend_elem(OP_LIST,
3405 newSVOP(OP_CONST, 0, stashsv),
3406 op_prepend_elem(OP_LIST,
3407 newSVOP(OP_CONST, 0,
3409 dup_attrlist(attrs))));
3414 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3416 OP *pack, *imop, *arg;
3417 SV *meth, *stashsv, **svp;
3419 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3424 assert(target->op_type == OP_PADSV ||
3425 target->op_type == OP_PADHV ||
3426 target->op_type == OP_PADAV);
3428 /* Ensure that attributes.pm is loaded. */
3429 /* Don't force the C<use> if we don't need it. */
3430 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3431 if (svp && *svp != &PL_sv_undef)
3432 NOOP; /* already in %INC */
3434 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3435 newSVpvs(ATTRSMODULE), NULL);
3437 /* Need package name for method call. */
3438 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3440 /* Build up the real arg-list. */
3441 stashsv = newSVhek(HvNAME_HEK(stash));
3443 arg = newOP(OP_PADSV, 0);
3444 arg->op_targ = target->op_targ;
3445 arg = op_prepend_elem(OP_LIST,
3446 newSVOP(OP_CONST, 0, stashsv),
3447 op_prepend_elem(OP_LIST,
3448 newUNOP(OP_REFGEN, 0,
3450 dup_attrlist(attrs)));
3452 /* Fake up a method call to import */
3453 meth = newSVpvs_share("import");
3454 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3455 op_append_elem(OP_LIST,
3456 op_prepend_elem(OP_LIST, pack, arg),
3457 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3459 /* Combine the ops. */
3460 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3464 =notfor apidoc apply_attrs_string
3466 Attempts to apply a list of attributes specified by the C<attrstr> and
3467 C<len> arguments to the subroutine identified by the C<cv> argument which
3468 is expected to be associated with the package identified by the C<stashpv>
3469 argument (see L<attributes>). It gets this wrong, though, in that it
3470 does not correctly identify the boundaries of the individual attribute
3471 specifications within C<attrstr>. This is not really intended for the
3472 public API, but has to be listed here for systems such as AIX which
3473 need an explicit export list for symbols. (It's called from XS code
3474 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3475 to respect attribute syntax properly would be welcome.
3481 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3482 const char *attrstr, STRLEN len)
3486 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3489 len = strlen(attrstr);
3493 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3495 const char * const sstr = attrstr;
3496 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3497 attrs = op_append_elem(OP_LIST, attrs,
3498 newSVOP(OP_CONST, 0,
3499 newSVpvn(sstr, attrstr-sstr)));
3503 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3504 newSVpvs(ATTRSMODULE),
3505 NULL, op_prepend_elem(OP_LIST,
3506 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3507 op_prepend_elem(OP_LIST,
3508 newSVOP(OP_CONST, 0,
3509 newRV(MUTABLE_SV(cv))),
3514 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3516 OP *new_proto = NULL;
3521 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3527 if (o->op_type == OP_CONST) {
3528 pv = SvPV(cSVOPo_sv, pvlen);
3529 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3530 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3531 SV ** const tmpo = cSVOPx_svp(o);
3532 SvREFCNT_dec(cSVOPo_sv);
3537 } else if (o->op_type == OP_LIST) {
3539 assert(o->op_flags & OPf_KIDS);
3540 lasto = cLISTOPo->op_first;
3541 assert(lasto->op_type == OP_PUSHMARK);
3542 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
3543 if (o->op_type == OP_CONST) {
3544 pv = SvPV(cSVOPo_sv, pvlen);
3545 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3546 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3547 SV ** const tmpo = cSVOPx_svp(o);
3548 SvREFCNT_dec(cSVOPo_sv);
3550 if (new_proto && ckWARN(WARN_MISC)) {
3552 const char * newp = SvPV(cSVOPo_sv, new_len);
3553 Perl_warner(aTHX_ packWARN(WARN_MISC),
3554 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
3555 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3561 /* excise new_proto from the list */
3562 op_sibling_splice(*attrs, lasto, 1, NULL);
3569 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3570 would get pulled in with no real need */
3571 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3580 svname = sv_newmortal();
3581 gv_efullname3(svname, name, NULL);
3583 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3584 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3586 svname = (SV *)name;
3587 if (ckWARN(WARN_ILLEGALPROTO))
3588 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3589 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3590 STRLEN old_len, new_len;
3591 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3592 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3594 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3595 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
3597 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3598 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3608 S_cant_declare(pTHX_ OP *o)
3610 if (o->op_type == OP_NULL
3611 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3612 o = cUNOPo->op_first;
3613 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3614 o->op_type == OP_NULL
3615 && o->op_flags & OPf_SPECIAL
3618 PL_parser->in_my == KEY_our ? "our" :
3619 PL_parser->in_my == KEY_state ? "state" :
3624 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3627 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3629 PERL_ARGS_ASSERT_MY_KID;
3631 if (!o || (PL_parser && PL_parser->error_count))
3636 if (type == OP_LIST) {
3638 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3639 my_kid(kid, attrs, imopsp);
3641 } else if (type == OP_UNDEF || type == OP_STUB) {
3643 } else if (type == OP_RV2SV || /* "our" declaration */
3645 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
3646 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3647 S_cant_declare(aTHX_ o);
3649 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3651 PL_parser->in_my = FALSE;
3652 PL_parser->in_my_stash = NULL;
3653 apply_attrs(GvSTASH(gv),
3654 (type == OP_RV2SV ? GvSV(gv) :
3655 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3656 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3659 o->op_private |= OPpOUR_INTRO;
3662 else if (type != OP_PADSV &&
3665 type != OP_PUSHMARK)
3667 S_cant_declare(aTHX_ o);
3670 else if (attrs && type != OP_PUSHMARK) {
3674 PL_parser->in_my = FALSE;
3675 PL_parser->in_my_stash = NULL;
3677 /* check for C<my Dog $spot> when deciding package */
3678 stash = PAD_COMPNAME_TYPE(o->op_targ);
3680 stash = PL_curstash;
3681 apply_attrs_my(stash, o, attrs, imopsp);
3683 o->op_flags |= OPf_MOD;
3684 o->op_private |= OPpLVAL_INTRO;
3686 o->op_private |= OPpPAD_STATE;
3691 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3694 int maybe_scalar = 0;
3696 PERL_ARGS_ASSERT_MY_ATTRS;
3698 /* [perl #17376]: this appears to be premature, and results in code such as
3699 C< our(%x); > executing in list mode rather than void mode */
3701 if (o->op_flags & OPf_PARENS)
3711 o = my_kid(o, attrs, &rops);
3713 if (maybe_scalar && o->op_type == OP_PADSV) {
3714 o = scalar(op_append_list(OP_LIST, rops, o));
3715 o->op_private |= OPpLVAL_INTRO;
3718 /* The listop in rops might have a pushmark at the beginning,
3719 which will mess up list assignment. */
3720 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3721 if (rops->op_type == OP_LIST &&
3722 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3724 OP * const pushmark = lrops->op_first;
3725 /* excise pushmark */
3726 op_sibling_splice(rops, NULL, 1, NULL);
3729 o = op_append_list(OP_LIST, o, rops);
3732 PL_parser->in_my = FALSE;
3733 PL_parser->in_my_stash = NULL;
3738 Perl_sawparens(pTHX_ OP *o)
3740 PERL_UNUSED_CONTEXT;
3742 o->op_flags |= OPf_PARENS;
3747 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3751 const OPCODE ltype = left->op_type;
3752 const OPCODE rtype = right->op_type;
3754 PERL_ARGS_ASSERT_BIND_MATCH;
3756 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3757 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3759 const char * const desc
3761 rtype == OP_SUBST || rtype == OP_TRANS
3762 || rtype == OP_TRANSR
3764 ? (int)rtype : OP_MATCH];
3765 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3767 S_op_varname(aTHX_ left);
3769 Perl_warner(aTHX_ packWARN(WARN_MISC),
3770 "Applying %s to %"SVf" will act on scalar(%"SVf")",
3771 desc, SVfARG(name), SVfARG(name));
3773 const char * const sample = (isary
3774 ? "@array" : "%hash");
3775 Perl_warner(aTHX_ packWARN(WARN_MISC),
3776 "Applying %s to %s will act on scalar(%s)",
3777 desc, sample, sample);
3781 if (rtype == OP_CONST &&
3782 cSVOPx(right)->op_private & OPpCONST_BARE &&
3783 cSVOPx(right)->op_private & OPpCONST_STRICT)
3785 no_bareword_allowed(right);
3788 /* !~ doesn't make sense with /r, so error on it for now */
3789 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3791 /* diag_listed_as: Using !~ with %s doesn't make sense */
3792 yyerror("Using !~ with s///r doesn't make sense");
3793 if (rtype == OP_TRANSR && type == OP_NOT)
3794 /* diag_listed_as: Using !~ with %s doesn't make sense */
3795 yyerror("Using !~ with tr///r doesn't make sense");
3797 ismatchop = (rtype == OP_MATCH ||
3798 rtype == OP_SUBST ||
3799 rtype == OP_TRANS || rtype == OP_TRANSR)
3800 && !(right->op_flags & OPf_SPECIAL);
3801 if (ismatchop && right->op_private & OPpTARGET_MY) {
3803 right->op_private &= ~OPpTARGET_MY;
3805 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
3806 if (left->op_type == OP_PADSV
3807 && !(left->op_private & OPpLVAL_INTRO))
3809 right->op_targ = left->op_targ;
3814 right->op_flags |= OPf_STACKED;
3815 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3816 ! (rtype == OP_TRANS &&
3817 right->op_private & OPpTRANS_IDENTICAL) &&
3818 ! (rtype == OP_SUBST &&
3819 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3820 left = op_lvalue(left, rtype);
3821 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3822 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
3824 o = op_prepend_elem(rtype, scalar(left), right);
3827 return newUNOP(OP_NOT, 0, scalar(o));
3831 return bind_match(type, left,
3832 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
3836 Perl_invert(pTHX_ OP *o)
3840 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3844 =for apidoc Amx|OP *|op_scope|OP *o
3846 Wraps up an op tree with some additional ops so that at runtime a dynamic
3847 scope will be created. The original ops run in the new dynamic scope,
3848 and then, provided that they exit normally, the scope will be unwound.
3849 The additional ops used to create and unwind the dynamic scope will
3850 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3851 instead if the ops are simple enough to not need the full dynamic scope
3858 Perl_op_scope(pTHX_ OP *o)
3862 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3863 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3864 OpTYPE_set(o, OP_LEAVE);
3866 else if (o->op_type == OP_LINESEQ) {
3868 OpTYPE_set(o, OP_SCOPE);
3869 kid = ((LISTOP*)o)->op_first;
3870 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3873 /* The following deals with things like 'do {1 for 1}' */
3874 kid = OpSIBLING(kid);
3876 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3881 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3887 Perl_op_unscope(pTHX_ OP *o)
3889 if (o && o->op_type == OP_LINESEQ) {
3890 OP *kid = cLISTOPo->op_first;
3891 for(; kid; kid = OpSIBLING(kid))
3892 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3899 =for apidoc Am|int|block_start|int full
3901 Handles compile-time scope entry.
3902 Arranges for hints to be restored on block
3903 exit and also handles pad sequence numbers to make lexical variables scope
3904 right. Returns a savestack index for use with C<block_end>.
3910 Perl_block_start(pTHX_ int full)
3912 const int retval = PL_savestack_ix;
3914 PL_compiling.cop_seq = PL_cop_seqmax;
3916 pad_block_start(full);
3918 PL_hints &= ~HINT_BLOCK_SCOPE;
3919 SAVECOMPILEWARNINGS();
3920 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3921 SAVEI32(PL_compiling.cop_seq);
3922 PL_compiling.cop_seq = 0;
3924 CALL_BLOCK_HOOKS(bhk_start, full);
3930 =for apidoc Am|OP *|block_end|I32 floor|OP *seq
3932 Handles compile-time scope exit. C<floor>
3933 is the savestack index returned by
3934 C<block_start>, and C<seq> is the body of the block. Returns the block,
3941 Perl_block_end(pTHX_ I32 floor, OP *seq)
3943 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3944 OP* retval = scalarseq(seq);
3947 /* XXX Is the null PL_parser check necessary here? */
3948 assert(PL_parser); /* Let’s find out under debugging builds. */
3949 if (PL_parser && PL_parser->parsed_sub) {
3950 o = newSTATEOP(0, NULL, NULL);
3952 retval = op_append_elem(OP_LINESEQ, retval, o);
3955 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3959 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3963 /* pad_leavemy has created a sequence of introcv ops for all my
3964 subs declared in the block. We have to replicate that list with
3965 clonecv ops, to deal with this situation:
3970 sub s1 { state sub foo { \&s2 } }
3973 Originally, I was going to have introcv clone the CV and turn
3974 off the stale flag. Since &s1 is declared before &s2, the
3975 introcv op for &s1 is executed (on sub entry) before the one for
3976 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3977 cloned, since it is a state sub) closes over &s2 and expects
3978 to see it in its outer CV’s pad. If the introcv op clones &s1,
3979 then &s2 is still marked stale. Since &s1 is not active, and
3980 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3981 ble will not stay shared’ warning. Because it is the same stub
3982 that will be used when the introcv op for &s2 is executed, clos-
3983 ing over it is safe. Hence, we have to turn off the stale flag
3984 on all lexical subs in the block before we clone any of them.
3985 Hence, having introcv clone the sub cannot work. So we create a
3986 list of ops like this:
4010 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
4011 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
4012 for (;; kid = OpSIBLING(kid)) {
4013 OP *newkid = newOP(OP_CLONECV, 0);
4014 newkid->op_targ = kid->op_targ;
4015 o = op_append_elem(OP_LINESEQ, o, newkid);
4016 if (kid == last) break;
4018 retval = op_prepend_elem(OP_LINESEQ, o, retval);
4021 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
4027 =head1 Compile-time scope hooks
4029 =for apidoc Aox||blockhook_register
4031 Register a set of hooks to be called when the Perl lexical scope changes
4032 at compile time. See L<perlguts/"Compile-time scope hooks">.
4038 Perl_blockhook_register(pTHX_ BHK *hk)
4040 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
4042 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
4046 Perl_newPROG(pTHX_ OP *o)
4048 PERL_ARGS_ASSERT_NEWPROG;
4055 PL_eval_root = newUNOP(OP_LEAVEEVAL,
4056 ((PL_in_eval & EVAL_KEEPERR)
4057 ? OPf_SPECIAL : 0), o);
4059 cx = &cxstack[cxstack_ix];
4060 assert(CxTYPE(cx) == CXt_EVAL);
4062 if ((cx->blk_gimme & G_WANT) == G_VOID)
4063 scalarvoid(PL_eval_root);
4064 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
4067 scalar(PL_eval_root);
4069 PL_eval_start = op_linklist(PL_eval_root);
4070 PL_eval_root->op_private |= OPpREFCOUNTED;
4071 OpREFCNT_set(PL_eval_root, 1);
4072 PL_eval_root->op_next = 0;
4073 i = PL_savestack_ix;
4076 CALL_PEEP(PL_eval_start);
4077 finalize_optree(PL_eval_root);
4078 S_prune_chain_head(&PL_eval_start);
4080 PL_savestack_ix = i;
4083 if (o->op_type == OP_STUB) {
4084 /* This block is entered if nothing is compiled for the main
4085 program. This will be the case for an genuinely empty main
4086 program, or one which only has BEGIN blocks etc, so already
4089 Historically (5.000) the guard above was !o. However, commit
4090 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
4091 c71fccf11fde0068, changed perly.y so that newPROG() is now
4092 called with the output of block_end(), which returns a new
4093 OP_STUB for the case of an empty optree. ByteLoader (and
4094 maybe other things) also take this path, because they set up
4095 PL_main_start and PL_main_root directly, without generating an
4098 If the parsing the main program aborts (due to parse errors,
4099 or due to BEGIN or similar calling exit), then newPROG()
4100 isn't even called, and hence this code path and its cleanups
4101 are skipped. This shouldn't make a make a difference:
4102 * a non-zero return from perl_parse is a failure, and
4103 perl_destruct() should be called immediately.
4104 * however, if exit(0) is called during the parse, then
4105 perl_parse() returns 0, and perl_run() is called. As
4106 PL_main_start will be NULL, perl_run() will return
4107 promptly, and the exit code will remain 0.
4110 PL_comppad_name = 0;
4112 S_op_destroy(aTHX_ o);
4115 PL_main_root = op_scope(sawparens(scalarvoid(o)));
4116 PL_curcop = &PL_compiling;
4117 PL_main_start = LINKLIST(PL_main_root);
4118 PL_main_root->op_private |= OPpREFCOUNTED;
4119 OpREFCNT_set(PL_main_root, 1);
4120 PL_main_root->op_next = 0;
4121 CALL_PEEP(PL_main_start);
4122 finalize_optree(PL_main_root);
4123 S_prune_chain_head(&PL_main_start);
4124 cv_forget_slab(PL_compcv);
4127 /* Register with debugger */
4129 CV * const cv = get_cvs("DB::postponed", 0);
4133 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
4135 call_sv(MUTABLE_SV(cv), G_DISCARD);
4142 Perl_localize(pTHX_ OP *o, I32 lex)
4144 PERL_ARGS_ASSERT_LOCALIZE;
4146 if (o->op_flags & OPf_PARENS)
4147 /* [perl #17376]: this appears to be premature, and results in code such as
4148 C< our(%x); > executing in list mode rather than void mode */
4155 if ( PL_parser->bufptr > PL_parser->oldbufptr
4156 && PL_parser->bufptr[-1] == ','
4157 && ckWARN(WARN_PARENTHESIS))
4159 char *s = PL_parser->bufptr;
4162 /* some heuristics to detect a potential error */
4163 while (*s && (strchr(", \t\n", *s)))
4167 if (*s && (strchr("@$%", *s) || (!lex && *s == '*'))
4169 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
4172 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
4174 while (*s && (strchr(", \t\n", *s)))
4180 if (sigil && (*s == ';' || *s == '=')) {
4181 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
4182 "Parentheses missing around \"%s\" list",
4184 ? (PL_parser->in_my == KEY_our
4186 : PL_parser->in_my == KEY_state
4196 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4197 PL_parser->in_my = FALSE;
4198 PL_parser->in_my_stash = NULL;
4203 Perl_jmaybe(pTHX_ OP *o)
4205 PERL_ARGS_ASSERT_JMAYBE;
4207 if (o->op_type == OP_LIST) {
4209 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4210 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4215 PERL_STATIC_INLINE OP *
4216 S_op_std_init(pTHX_ OP *o)
4218 I32 type = o->op_type;
4220 PERL_ARGS_ASSERT_OP_STD_INIT;
4222 if (PL_opargs[type] & OA_RETSCALAR)
4224 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4225 o->op_targ = pad_alloc(type, SVs_PADTMP);
4230 PERL_STATIC_INLINE OP *
4231 S_op_integerize(pTHX_ OP *o)
4233 I32 type = o->op_type;
4235 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4237 /* integerize op. */
4238 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4241 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4244 if (type == OP_NEGATE)
4245 /* XXX might want a ck_negate() for this */
4246 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4252 S_fold_constants(pTHX_ OP *o)
4257 VOL I32 type = o->op_type;
4263 SV * const oldwarnhook = PL_warnhook;
4264 SV * const olddiehook = PL_diehook;
4266 U8 oldwarn = PL_dowarn;
4269 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4271 if (!(PL_opargs[type] & OA_FOLDCONST))
4280 #ifdef USE_LOCALE_CTYPE
4281 if (IN_LC_COMPILETIME(LC_CTYPE))
4290 #ifdef USE_LOCALE_COLLATE
4291 if (IN_LC_COMPILETIME(LC_COLLATE))
4296 /* XXX what about the numeric ops? */
4297 #ifdef USE_LOCALE_NUMERIC
4298 if (IN_LC_COMPILETIME(LC_NUMERIC))
4303 if (!OpHAS_SIBLING(cLISTOPo->op_first)
4304 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4307 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
4308 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4310 const char *s = SvPVX_const(sv);
4311 while (s < SvEND(sv)) {
4312 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4319 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4322 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4323 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4327 if (PL_parser && PL_parser->error_count)
4328 goto nope; /* Don't try to run w/ errors */
4330 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4331 const OPCODE type = curop->op_type;
4332 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
4334 type != OP_SCALAR &&
4336 type != OP_PUSHMARK)
4342 curop = LINKLIST(o);
4343 old_next = o->op_next;
4347 oldscope = PL_scopestack_ix;
4348 create_eval_scope(G_FAKINGEVAL);
4350 /* Verify that we don't need to save it: */
4351 assert(PL_curcop == &PL_compiling);
4352 StructCopy(&PL_compiling, ¬_compiling, COP);
4353 PL_curcop = ¬_compiling;
4354 /* The above ensures that we run with all the correct hints of the
4355 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4356 assert(IN_PERL_RUNTIME);
4357 PL_warnhook = PERL_WARNHOOK_FATAL;
4361 /* Effective $^W=1. */
4362 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4363 PL_dowarn |= G_WARN_ON;
4368 sv = *(PL_stack_sp--);
4369 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4370 pad_swipe(o->op_targ, FALSE);
4372 else if (SvTEMP(sv)) { /* grab mortal temp? */
4373 SvREFCNT_inc_simple_void(sv);
4376 else { assert(SvIMMORTAL(sv)); }
4379 /* Something tried to die. Abandon constant folding. */
4380 /* Pretend the error never happened. */
4382 o->op_next = old_next;
4386 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4387 PL_warnhook = oldwarnhook;
4388 PL_diehook = olddiehook;
4389 /* XXX note that this croak may fail as we've already blown away
4390 * the stack - eg any nested evals */
4391 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4394 PL_dowarn = oldwarn;
4395 PL_warnhook = oldwarnhook;
4396 PL_diehook = olddiehook;
4397 PL_curcop = &PL_compiling;
4399 if (PL_scopestack_ix > oldscope)
4400 delete_eval_scope();
4405 /* OP_STRINGIFY and constant folding are used to implement qq.
4406 Here the constant folding is an implementation detail that we
4407 want to hide. If the stringify op is itself already marked
4408 folded, however, then it is actually a folded join. */
4409 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4414 else if (!SvIMMORTAL(sv)) {
4418 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4419 if (!is_stringify) newop->op_folded = 1;
4427 S_gen_constant_list(pTHX_ OP *o)
4431 const SSize_t oldtmps_floor = PL_tmps_floor;
4436 if (PL_parser && PL_parser->error_count)
4437 return o; /* Don't attempt to run with errors */
4439 curop = LINKLIST(o);
4442 S_prune_chain_head(&curop);
4444 Perl_pp_pushmark(aTHX);
4447 assert (!(curop->op_flags & OPf_SPECIAL));
4448 assert(curop->op_type == OP_RANGE);
4449 Perl_pp_anonlist(aTHX);
4450 PL_tmps_floor = oldtmps_floor;
4452 OpTYPE_set(o, OP_RV2AV);
4453 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4454 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4455 o->op_opt = 0; /* needs to be revisited in rpeep() */
4456 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4458 /* replace subtree with an OP_CONST */
4459 curop = ((UNOP*)o)->op_first;
4460 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4463 if (AvFILLp(av) != -1)
4464 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4467 SvREADONLY_on(*svp);
4474 =head1 Optree Manipulation Functions
4477 /* List constructors */
4480 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4482 Append an item to the list of ops contained directly within a list-type
4483 op, returning the lengthened list. C<first> is the list-type op,
4484 and C<last> is the op to append to the list. C<optype> specifies the
4485 intended opcode for the list. If C<first> is not already a list of the
4486 right type, it will be upgraded into one. If either C<first> or C<last>
4487 is null, the other is returned unchanged.
4493 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4501 if (first->op_type != (unsigned)type
4502 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4504 return newLISTOP(type, 0, first, last);
4507 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4508 first->op_flags |= OPf_KIDS;
4513 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4515 Concatenate the lists of ops contained directly within two list-type ops,
4516 returning the combined list. C<first> and C<last> are the list-type ops
4517 to concatenate. C<optype> specifies the intended opcode for the list.
4518 If either C<first> or C<last> is not already a list of the right type,
4519 it will be upgraded into one. If either C<first> or C<last> is null,
4520 the other is returned unchanged.
4526 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4534 if (first->op_type != (unsigned)type)
4535 return op_prepend_elem(type, first, last);
4537 if (last->op_type != (unsigned)type)
4538 return op_append_elem(type, first, last);
4540 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4541 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4542 OpLASTSIB_set(((LISTOP*)first)->op_last, first);
4543 first->op_flags |= (last->op_flags & OPf_KIDS);
4545 S_op_destroy(aTHX_ last);
4551 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4553 Prepend an item to the list of ops contained directly within a list-type
4554 op, returning the lengthened list. C<first> is the op to prepend to the
4555 list, and C<last> is the list-type op. C<optype> specifies the intended
4556 opcode for the list. If C<last> is not already a list of the right type,
4557 it will be upgraded into one. If either C<first> or C<last> is null,
4558 the other is returned unchanged.
4564 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4572 if (last->op_type == (unsigned)type) {
4573 if (type == OP_LIST) { /* already a PUSHMARK there */
4574 /* insert 'first' after pushmark */
4575 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4576 if (!(first->op_flags & OPf_PARENS))
4577 last->op_flags &= ~OPf_PARENS;
4580 op_sibling_splice(last, NULL, 0, first);
4581 last->op_flags |= OPf_KIDS;
4585 return newLISTOP(type, 0, first, last);
4589 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4591 Converts C<o> into a list op if it is not one already, and then converts it
4592 into the specified C<type>, calling its check function, allocating a target if
4593 it needs one, and folding constants.
4595 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4596 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4597 C<op_convert_list> to make it the right type.
4603 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4606 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4607 if (!o || o->op_type != OP_LIST)
4608 o = force_list(o, 0);
4611 o->op_flags &= ~OPf_WANT;
4612 o->op_private &= ~OPpLVAL_INTRO;
4615 if (!(PL_opargs[type] & OA_MARK))
4616 op_null(cLISTOPo->op_first);
4618 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
4619 if (kid2 && kid2->op_type == OP_COREARGS) {
4620 op_null(cLISTOPo->op_first);
4621 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4625 OpTYPE_set(o, type);
4626 o->op_flags |= flags;
4627 if (flags & OPf_FOLDED)
4630 o = CHECKOP(type, o);
4631 if (o->op_type != (unsigned)type)
4634 return fold_constants(op_integerize(op_std_init(o)));
4641 =head1 Optree construction
4643 =for apidoc Am|OP *|newNULLLIST
4645 Constructs, checks, and returns a new C<stub> op, which represents an
4646 empty list expression.
4652 Perl_newNULLLIST(pTHX)
4654 return newOP(OP_STUB, 0);
4657 /* promote o and any siblings to be a list if its not already; i.e.
4665 * pushmark - o - A - B
4667 * If nullit it true, the list op is nulled.
4671 S_force_list(pTHX_ OP *o, bool nullit)
4673 if (!o || o->op_type != OP_LIST) {
4676 /* manually detach any siblings then add them back later */
4677 rest = OpSIBLING(o);
4678 OpLASTSIB_set(o, NULL);
4680 o = newLISTOP(OP_LIST, 0, o, NULL);
4682 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4690 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4692 Constructs, checks, and returns an op of any list type. C<type> is
4693 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4694 C<OPf_KIDS> will be set automatically if required. C<first> and C<last>
4695 supply up to two ops to be direct children of the list op; they are
4696 consumed by this function and become part of the constructed op tree.
4698 For most list operators, the check function expects all the kid ops to be
4699 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
4700 appropriate. What you want to do in that case is create an op of type
4701 C<OP_LIST>, append more children to it, and then call L</op_convert_list>.
4702 See L</op_convert_list> for more information.
4709 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4714 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
4715 || type == OP_CUSTOM);
4717 NewOp(1101, listop, 1, LISTOP);
4719 OpTYPE_set(listop, type);
4722 listop->op_flags = (U8)flags;
4726 else if (!first && last)
4729 OpMORESIB_set(first, last);
4730 listop->op_first = first;
4731 listop->op_last = last;
4732 if (type == OP_LIST) {
4733 OP* const pushop = newOP(OP_PUSHMARK, 0);
4734 OpMORESIB_set(pushop, first);
4735 listop->op_first = pushop;
4736 listop->op_flags |= OPf_KIDS;
4738 listop->op_last = pushop;
4740 if (listop->op_last)
4741 OpLASTSIB_set(listop->op_last, (OP*)listop);
4743 return CHECKOP(type, listop);
4747 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4749 Constructs, checks, and returns an op of any base type (any type that
4750 has no extra fields). C<type> is the opcode. C<flags> gives the
4751 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4758 Perl_newOP(pTHX_ I32 type, I32 flags)
4763 if (type == -OP_ENTEREVAL) {
4764 type = OP_ENTEREVAL;
4765 flags |= OPpEVAL_BYTES<<8;
4768 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4769 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4770 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4771 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4773 NewOp(1101, o, 1, OP);
4774 OpTYPE_set(o, type);
4775 o->op_flags = (U8)flags;
4778 o->op_private = (U8)(0 | (flags >> 8));
4779 if (PL_opargs[type] & OA_RETSCALAR)
4781 if (PL_opargs[type] & OA_TARGET)
4782 o->op_targ = pad_alloc(type, SVs_PADTMP);
4783 return CHECKOP(type, o);
4787 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4789 Constructs, checks, and returns an op of any unary type. C<type> is
4790 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4791 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4792 bits, the eight bits of C<op_private>, except that the bit with value 1
4793 is automatically set. C<first> supplies an optional op to be the direct
4794 child of the unary op; it is consumed by this function and become part
4795 of the constructed op tree.
4801 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4806 if (type == -OP_ENTEREVAL) {
4807 type = OP_ENTEREVAL;
4808 flags |= OPpEVAL_BYTES<<8;
4811 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4812 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4813 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4814 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4815 || type == OP_SASSIGN
4816 || type == OP_ENTERTRY
4817 || type == OP_CUSTOM
4818 || type == OP_NULL );
4821 first = newOP(OP_STUB, 0);
4822 if (PL_opargs[type] & OA_MARK)
4823 first = force_list(first, 1);
4825 NewOp(1101, unop, 1, UNOP);
4826 OpTYPE_set(unop, type);
4827 unop->op_first = first;
4828 unop->op_flags = (U8)(flags | OPf_KIDS);
4829 unop->op_private = (U8)(1 | (flags >> 8));
4831 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
4832 OpLASTSIB_set(first, (OP*)unop);
4834 unop = (UNOP*) CHECKOP(type, unop);
4838 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4842 =for apidoc newUNOP_AUX
4844 Similar to C<newUNOP>, but creates an C<UNOP_AUX> struct instead, with C<op_aux>
4845 initialised to C<aux>
4851 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux)
4856 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX
4857 || type == OP_CUSTOM);
4859 NewOp(1101, unop, 1, UNOP_AUX);
4860 unop->op_type = (OPCODE)type;
4861 unop->op_ppaddr = PL_ppaddr[type];
4862 unop->op_first = first;
4863 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0));
4864 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8));
4867 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */
4868 OpLASTSIB_set(first, (OP*)unop);
4870 unop = (UNOP_AUX*) CHECKOP(type, unop);
4872 return op_std_init((OP *) unop);
4876 =for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first
4878 Constructs, checks, and returns an op of method type with a method name
4879 evaluated at runtime. C<type> is the opcode. C<flags> gives the eight
4880 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically,
4881 and, shifted up eight bits, the eight bits of C<op_private>, except that
4882 the bit with value 1 is automatically set. C<dynamic_meth> supplies an
4883 op which evaluates method name; it is consumed by this function and
4884 become part of the constructed op tree.
4885 Supported optypes: C<OP_METHOD>.
4891 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) {
4895 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP
4896 || type == OP_CUSTOM);
4898 NewOp(1101, methop, 1, METHOP);
4900 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1);
4901 methop->op_flags = (U8)(flags | OPf_KIDS);
4902 methop->op_u.op_first = dynamic_meth;
4903 methop->op_private = (U8)(1 | (flags >> 8));
4905 if (!OpHAS_SIBLING(dynamic_meth))
4906 OpLASTSIB_set(dynamic_meth, (OP*)methop);
4910 methop->op_flags = (U8)(flags & ~OPf_KIDS);
4911 methop->op_u.op_meth_sv = const_meth;
4912 methop->op_private = (U8)(0 | (flags >> 8));
4913 methop->op_next = (OP*)methop;
4917 methop->op_rclass_targ = 0;
4919 methop->op_rclass_sv = NULL;
4922 OpTYPE_set(methop, type);
4923 return CHECKOP(type, methop);
4927 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) {
4928 PERL_ARGS_ASSERT_NEWMETHOP;
4929 return newMETHOP_internal(type, flags, dynamic_meth, NULL);
4933 =for apidoc Am|OP *|newMETHOP_named|I32 type|I32 flags|SV *const_meth
4935 Constructs, checks, and returns an op of method type with a constant
4936 method name. C<type> is the opcode. C<flags> gives the eight bits of
4937 C<op_flags>, and, shifted up eight bits, the eight bits of
4938 C<op_private>. C<const_meth> supplies a constant method name;
4939 it must be a shared COW string.
4940 Supported optypes: C<OP_METHOD_NAMED>.
4946 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) {
4947 PERL_ARGS_ASSERT_NEWMETHOP_NAMED;
4948 return newMETHOP_internal(type, flags, NULL, const_meth);
4952 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
4954 Constructs, checks, and returns an op of any binary type. C<type>
4955 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
4956 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
4957 the eight bits of C<op_private>, except that the bit with value 1 or
4958 2 is automatically set as required. C<first> and C<last> supply up to
4959 two ops to be the direct children of the binary op; they are consumed
4960 by this function and become part of the constructed op tree.
4966 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4971 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4972 || type == OP_SASSIGN || type == OP_NULL || type == OP_CUSTOM);
4974 NewOp(1101, binop, 1, BINOP);
4977 first = newOP(OP_NULL, 0);
4979 OpTYPE_set(binop, type);
4980 binop->op_first = first;
4981 binop->op_flags = (U8)(flags | OPf_KIDS);
4984 binop->op_private = (U8)(1 | (flags >> 8));
4987 binop->op_private = (U8)(2 | (flags >> 8));
4988 OpMORESIB_set(first, last);
4991 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */
4992 OpLASTSIB_set(last, (OP*)binop);
4994 binop->op_last = OpSIBLING(binop->op_first);
4996 OpLASTSIB_set(binop->op_last, (OP*)binop);
4998 binop = (BINOP*)CHECKOP(type, binop);
4999 if (binop->op_next || binop->op_type != (OPCODE)type)
5002 return fold_constants(op_integerize(op_std_init((OP *)binop)));
5005 static int uvcompare(const void *a, const void *b)
5006 __attribute__nonnull__(1)
5007 __attribute__nonnull__(2)
5008 __attribute__pure__;
5009 static int uvcompare(const void *a, const void *b)
5011 if (*((const UV *)a) < (*(const UV *)b))
5013 if (*((const UV *)a) > (*(const UV *)b))
5015 if (*((const UV *)a+1) < (*(const UV *)b+1))
5017 if (*((const UV *)a+1) > (*(const UV *)b+1))
5023 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
5025 SV * const tstr = ((SVOP*)expr)->op_sv;
5027 ((SVOP*)repl)->op_sv;
5030 const U8 *t = (U8*)SvPV_const(tstr, tlen);
5031 const U8 *r = (U8*)SvPV_const(rstr, rlen);
5037 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
5038 const I32 squash = o->op_private & OPpTRANS_SQUASH;
5039 I32 del = o->op_private & OPpTRANS_DELETE;
5042 PERL_ARGS_ASSERT_PMTRANS;
5044 PL_hints |= HINT_BLOCK_SCOPE;
5047 o->op_private |= OPpTRANS_FROM_UTF;
5050 o->op_private |= OPpTRANS_TO_UTF;
5052 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
5053 SV* const listsv = newSVpvs("# comment\n");
5055 const U8* tend = t + tlen;
5056 const U8* rend = r + rlen;
5072 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
5073 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
5076 const U32 flags = UTF8_ALLOW_DEFAULT;
5080 t = tsave = bytes_to_utf8(t, &len);
5083 if (!to_utf && rlen) {
5085 r = rsave = bytes_to_utf8(r, &len);
5089 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has
5090 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
5094 U8 tmpbuf[UTF8_MAXBYTES+1];
5097 Newx(cp, 2*tlen, UV);
5099 transv = newSVpvs("");
5101 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
5103 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
5105 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
5109 cp[2*i+1] = cp[2*i];
5113 qsort(cp, i, 2*sizeof(UV), uvcompare);
5114 for (j = 0; j < i; j++) {
5116 diff = val - nextmin;
5118 t = uvchr_to_utf8(tmpbuf,nextmin);
5119 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5121 U8 range_mark = ILLEGAL_UTF8_BYTE;
5122 t = uvchr_to_utf8(tmpbuf, val - 1);
5123 sv_catpvn(transv, (char *)&range_mark, 1);
5124 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5131 t = uvchr_to_utf8(tmpbuf,nextmin);
5132 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5134 U8 range_mark = ILLEGAL_UTF8_BYTE;
5135 sv_catpvn(transv, (char *)&range_mark, 1);
5137 t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
5138 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
5139 t = (const U8*)SvPVX_const(transv);
5140 tlen = SvCUR(transv);
5144 else if (!rlen && !del) {
5145 r = t; rlen = tlen; rend = tend;
5148 if ((!rlen && !del) || t == r ||
5149 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
5151 o->op_private |= OPpTRANS_IDENTICAL;
5155 while (t < tend || tfirst <= tlast) {
5156 /* see if we need more "t" chars */
5157 if (tfirst > tlast) {
5158 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
5160 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
5162 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
5169 /* now see if we need more "r" chars */
5170 if (rfirst > rlast) {
5172 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
5174 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
5176 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
5185 rfirst = rlast = 0xffffffff;
5189 /* now see which range will peter out first, if either. */
5190 tdiff = tlast - tfirst;
5191 rdiff = rlast - rfirst;
5192 tcount += tdiff + 1;
5193 rcount += rdiff + 1;
5200 if (rfirst == 0xffffffff) {
5201 diff = tdiff; /* oops, pretend rdiff is infinite */
5203 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
5204 (long)tfirst, (long)tlast);
5206 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
5210 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
5211 (long)tfirst, (long)(tfirst + diff),
5214 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
5215 (long)tfirst, (long)rfirst);
5217 if (rfirst + diff > max)
5218 max = rfirst + diff;
5220 grows = (tfirst < rfirst &&
5221 UVCHR_SKIP(tfirst) < UVCHR_SKIP(rfirst + diff));
5233 else if (max > 0xff)
5238 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
5240 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
5241 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
5242 PAD_SETSV(cPADOPo->op_padix, swash);
5244 SvREADONLY_on(swash);
5246 cSVOPo->op_sv = swash;
5248 SvREFCNT_dec(listsv);
5249 SvREFCNT_dec(transv);
5251 if (!del && havefinal && rlen)
5252 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
5253 newSVuv((UV)final), 0);
5262 else if (rlast == 0xffffffff)
5268 tbl = (short*)PerlMemShared_calloc(
5269 (o->op_private & OPpTRANS_COMPLEMENT) &&
5270 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
5272 cPVOPo->op_pv = (char*)tbl;
5274 for (i = 0; i < (I32)tlen; i++)
5276 for (i = 0, j = 0; i < 256; i++) {
5278 if (j >= (I32)rlen) {
5287 if (i < 128 && r[j] >= 128)
5297 o->op_private |= OPpTRANS_IDENTICAL;
5299 else if (j >= (I32)rlen)
5304 PerlMemShared_realloc(tbl,
5305 (0x101+rlen-j) * sizeof(short));
5306 cPVOPo->op_pv = (char*)tbl;
5308 tbl[0x100] = (short)(rlen - j);
5309 for (i=0; i < (I32)rlen - j; i++)
5310 tbl[0x101+i] = r[j+i];
5314 if (!rlen && !del) {
5317 o->op_private |= OPpTRANS_IDENTICAL;
5319 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
5320 o->op_private |= OPpTRANS_IDENTICAL;
5322 for (i = 0; i < 256; i++)
5324 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
5325 if (j >= (I32)rlen) {
5327 if (tbl[t[i]] == -1)
5333 if (tbl[t[i]] == -1) {
5334 if (t[i] < 128 && r[j] >= 128)
5342 if(del && rlen == tlen) {
5343 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
5344 } else if(rlen > tlen && !complement) {
5345 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
5349 o->op_private |= OPpTRANS_GROWS;
5357 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
5359 Constructs, checks, and returns an op of any pattern matching type.
5360 C<type> is the opcode. C<flags> gives the eight bits of C<op_flags>
5361 and, shifted up eight bits, the eight bits of C<op_private>.
5367 Perl_newPMOP(pTHX_ I32 type, I32 flags)
5372 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP
5373 || type == OP_CUSTOM);
5375 NewOp(1101, pmop, 1, PMOP);
5376 OpTYPE_set(pmop, type);
5377 pmop->op_flags = (U8)flags;
5378 pmop->op_private = (U8)(0 | (flags >> 8));
5379 if (PL_opargs[type] & OA_RETSCALAR)
5382 if (PL_hints & HINT_RE_TAINT)
5383 pmop->op_pmflags |= PMf_RETAINT;
5384 #ifdef USE_LOCALE_CTYPE
5385 if (IN_LC_COMPILETIME(LC_CTYPE)) {
5386 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
5391 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
5393 if (PL_hints & HINT_RE_FLAGS) {
5394 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
5395 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
5397 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
5398 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
5399 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
5401 if (reflags && SvOK(reflags)) {
5402 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
5408 assert(SvPOK(PL_regex_pad[0]));
5409 if (SvCUR(PL_regex_pad[0])) {
5410 /* Pop off the "packed" IV from the end. */
5411 SV *const repointer_list = PL_regex_pad[0];
5412 const char *p = SvEND(repointer_list) - sizeof(IV);
5413 const IV offset = *((IV*)p);
5415 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
5417 SvEND_set(repointer_list, p);
5419 pmop->op_pmoffset = offset;
5420 /* This slot should be free, so assert this: */
5421 assert(PL_regex_pad[offset] == &PL_sv_undef);
5423 SV * const repointer = &PL_sv_undef;
5424 av_push(PL_regex_padav, repointer);
5425 pmop->op_pmoffset = av_tindex(PL_regex_padav);
5426 PL_regex_pad = AvARRAY(PL_regex_padav);
5430 return CHECKOP(type, pmop);
5438 /* Any pad names in scope are potentially lvalues. */
5439 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) {
5440 PADNAME *pn = PAD_COMPNAME_SV(i);
5441 if (!pn || !PadnameLEN(pn))
5443 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax))
5444 S_mark_padname_lvalue(aTHX_ pn);
5448 /* Given some sort of match op o, and an expression expr containing a
5449 * pattern, either compile expr into a regex and attach it to o (if it's
5450 * constant), or convert expr into a runtime regcomp op sequence (if it's
5453 * isreg indicates that the pattern is part of a regex construct, eg
5454 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
5455 * split "pattern", which aren't. In the former case, expr will be a list
5456 * if the pattern contains more than one term (eg /a$b/).
5458 * When the pattern has been compiled within a new anon CV (for
5459 * qr/(?{...})/ ), then floor indicates the savestack level just before
5460 * the new sub was created
5464 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, bool isreg, I32 floor)
5468 I32 repl_has_vars = 0;
5469 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
5470 bool is_compiletime;
5473 PERL_ARGS_ASSERT_PMRUNTIME;
5476 return pmtrans(o, expr, repl);
5479 /* find whether we have any runtime or code elements;
5480 * at the same time, temporarily set the op_next of each DO block;
5481 * then when we LINKLIST, this will cause the DO blocks to be excluded
5482 * from the op_next chain (and from having LINKLIST recursively
5483 * applied to them). We fix up the DOs specially later */
5487 if (expr->op_type == OP_LIST) {
5489 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) {
5490 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
5492 assert(!o->op_next);
5493 if (UNLIKELY(!OpHAS_SIBLING(o))) {
5494 assert(PL_parser && PL_parser->error_count);
5495 /* This can happen with qr/ (?{(^{})/. Just fake up
5496 the op we were expecting to see, to avoid crashing
5498 op_sibling_splice(expr, o, 0,
5499 newSVOP(OP_CONST, 0, &PL_sv_no));
5501 o->op_next = OpSIBLING(o);
5503 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
5507 else if (expr->op_type != OP_CONST)
5512 /* fix up DO blocks; treat each one as a separate little sub;
5513 * also, mark any arrays as LIST/REF */
5515 if (expr->op_type == OP_LIST) {
5517 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) {
5519 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) {
5520 assert( !(o->op_flags & OPf_WANT));
5521 /* push the array rather than its contents. The regex
5522 * engine will retrieve and join the elements later */
5523 o->op_flags |= (OPf_WANT_LIST | OPf_REF);
5527 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
5529 o->op_next = NULL; /* undo temporary hack from above */
5532 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
5533 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first);
5535 assert(leaveop->op_first->op_type == OP_ENTER);
5536 assert(OpHAS_SIBLING(leaveop->op_first));
5537 o->op_next = OpSIBLING(leaveop->op_first);
5539 assert(leaveop->op_flags & OPf_KIDS);
5540 assert(leaveop->op_last->op_next == (OP*)leaveop);
5541 leaveop->op_next = NULL; /* stop on last op */
5542 op_null((OP*)leaveop);
5546 OP *scope = cLISTOPo->op_first;
5547 assert(scope->op_type == OP_SCOPE);
5548 assert(scope->op_flags & OPf_KIDS);
5549 scope->op_next = NULL; /* stop on last op */
5552 /* have to peep the DOs individually as we've removed it from
5553 * the op_next chain */
5555 S_prune_chain_head(&(o->op_next));
5557 /* runtime finalizes as part of finalizing whole tree */
5561 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
5562 assert( !(expr->op_flags & OPf_WANT));
5563 /* push the array rather than its contents. The regex
5564 * engine will retrieve and join the elements later */
5565 expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
5568 PL_hints |= HINT_BLOCK_SCOPE;
5570 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
5572 if (is_compiletime) {
5573 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
5574 regexp_engine const *eng = current_re_engine();
5576 if (o->op_flags & OPf_SPECIAL)
5577 rx_flags |= RXf_SPLIT;
5579 if (!has_code || !eng->op_comp) {
5580 /* compile-time simple constant pattern */
5582 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
5583 /* whoops! we guessed that a qr// had a code block, but we
5584 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
5585 * that isn't required now. Note that we have to be pretty
5586 * confident that nothing used that CV's pad while the
5587 * regex was parsed, except maybe op targets for \Q etc.
5588 * If there were any op targets, though, they should have
5589 * been stolen by constant folding.
5593 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0);
5594 while (++i <= AvFILLp(PL_comppad)) {
5595 assert(!PL_curpad[i]);
5598 /* But we know that one op is using this CV's slab. */
5599 cv_forget_slab(PL_compcv);
5601 pm->op_pmflags &= ~PMf_HAS_CV;
5606 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5607 rx_flags, pm->op_pmflags)
5608 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5609 rx_flags, pm->op_pmflags)
5614 /* compile-time pattern that includes literal code blocks */
5615 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
5618 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
5621 if (pm->op_pmflags & PMf_HAS_CV) {
5623 /* this QR op (and the anon sub we embed it in) is never
5624 * actually executed. It's just a placeholder where we can
5625 * squirrel away expr in op_code_list without the peephole
5626 * optimiser etc processing it for a second time */
5627 OP *qr = newPMOP(OP_QR, 0);
5628 ((PMOP*)qr)->op_code_list = expr;
5630 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
5631 SvREFCNT_inc_simple_void(PL_compcv);
5632 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
5633 ReANY(re)->qr_anoncv = cv;
5635 /* attach the anon CV to the pad so that
5636 * pad_fixup_inner_anons() can find it */
5637 (void)pad_add_anon(cv, o->op_type);
5638 SvREFCNT_inc_simple_void(cv);
5641 pm->op_code_list = expr;
5646 /* runtime pattern: build chain of regcomp etc ops */
5648 PADOFFSET cv_targ = 0;
5650 reglist = isreg && expr->op_type == OP_LIST;
5655 pm->op_code_list = expr;
5656 /* don't free op_code_list; its ops are embedded elsewhere too */
5657 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
5660 if (o->op_flags & OPf_SPECIAL)
5661 pm->op_pmflags |= PMf_SPLIT;
5663 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
5664 * to allow its op_next to be pointed past the regcomp and
5665 * preceding stacking ops;
5666 * OP_REGCRESET is there to reset taint before executing the
5668 if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
5669 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
5671 if (pm->op_pmflags & PMf_HAS_CV) {
5672 /* we have a runtime qr with literal code. This means
5673 * that the qr// has been wrapped in a new CV, which
5674 * means that runtime consts, vars etc will have been compiled
5675 * against a new pad. So... we need to execute those ops
5676 * within the environment of the new CV. So wrap them in a call
5677 * to a new anon sub. i.e. for
5681 * we build an anon sub that looks like
5683 * sub { "a", $b, '(?{...})' }
5685 * and call it, passing the returned list to regcomp.
5686 * Or to put it another way, the list of ops that get executed
5690 * ------ -------------------
5691 * pushmark (for regcomp)
5692 * pushmark (for entersub)
5696 * regcreset regcreset
5698 * const("a") const("a")
5700 * const("(?{...})") const("(?{...})")
5705 SvREFCNT_inc_simple_void(PL_compcv);
5706 CvLVALUE_on(PL_compcv);
5707 /* these lines are just an unrolled newANONATTRSUB */
5708 expr = newSVOP(OP_ANONCODE, 0,
5709 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
5710 cv_targ = expr->op_targ;
5711 expr = newUNOP(OP_REFGEN, 0, expr);
5713 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), 1));
5716 rcop = S_alloc_LOGOP(aTHX_ OP_REGCOMP, scalar(expr), o);
5717 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
5718 | (reglist ? OPf_STACKED : 0);
5719 rcop->op_targ = cv_targ;
5721 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
5722 if (PL_hints & HINT_RE_EVAL)
5723 S_set_haseval(aTHX);
5725 /* establish postfix order */
5726 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
5728 rcop->op_next = expr;
5729 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
5732 rcop->op_next = LINKLIST(expr);
5733 expr->op_next = (OP*)rcop;
5736 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
5742 /* If we are looking at s//.../e with a single statement, get past
5743 the implicit do{}. */
5744 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
5745 && cUNOPx(curop)->op_first->op_type == OP_SCOPE
5746 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS)
5749 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
5750 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid))
5751 && !OpHAS_SIBLING(sib))
5754 if (curop->op_type == OP_CONST)
5756 else if (( (curop->op_type == OP_RV2SV ||
5757 curop->op_type == OP_RV2AV ||
5758 curop->op_type == OP_RV2HV ||
5759 curop->op_type == OP_RV2GV)
5760 && cUNOPx(curop)->op_first
5761 && cUNOPx(curop)->op_first->op_type == OP_GV )
5762 || curop->op_type == OP_PADSV
5763 || curop->op_type == OP_PADAV
5764 || curop->op_type == OP_PADHV
5765 || curop->op_type == OP_PADANY) {
5773 || !RX_PRELEN(PM_GETRE(pm))
5774 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
5776 pm->op_pmflags |= PMf_CONST; /* const for long enough */
5777 op_prepend_elem(o->op_type, scalar(repl), o);
5780 rcop = S_alloc_LOGOP(aTHX_ OP_SUBSTCONT, scalar(repl), o);
5781 rcop->op_private = 1;
5783 /* establish postfix order */
5784 rcop->op_next = LINKLIST(repl);
5785 repl->op_next = (OP*)rcop;
5787 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
5788 assert(!(pm->op_pmflags & PMf_ONCE));
5789 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
5798 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
5800 Constructs, checks, and returns an op of any type that involves an
5801 embedded SV. C<type> is the opcode. C<flags> gives the eight bits
5802 of C<op_flags>. C<sv> gives the SV to embed in the op; this function
5803 takes ownership of one reference to it.
5809 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
5814 PERL_ARGS_ASSERT_NEWSVOP;
5816 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
5817 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5818 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
5819 || type == OP_CUSTOM);
5821 NewOp(1101, svop, 1, SVOP);
5822 OpTYPE_set(svop, type);
5824 svop->op_next = (OP*)svop;
5825 svop->op_flags = (U8)flags;
5826 svop->op_private = (U8)(0 | (flags >> 8));
5827 if (PL_opargs[type] & OA_RETSCALAR)
5829 if (PL_opargs[type] & OA_TARGET)
5830 svop->op_targ = pad_alloc(type, SVs_PADTMP);
5831 return CHECKOP(type, svop);
5835 =for apidoc Am|OP *|newDEFSVOP|
5837 Constructs and returns an op to access C<$_>.
5843 Perl_newDEFSVOP(pTHX)
5845 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
5851 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
5853 Constructs, checks, and returns an op of any type that involves a
5854 reference to a pad element. C<type> is the opcode. C<flags> gives the
5855 eight bits of C<op_flags>. A pad slot is automatically allocated, and
5856 is populated with C<sv>; this function takes ownership of one reference
5859 This function only exists if Perl has been compiled to use ithreads.
5865 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
5870 PERL_ARGS_ASSERT_NEWPADOP;
5872 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
5873 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5874 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
5875 || type == OP_CUSTOM);
5877 NewOp(1101, padop, 1, PADOP);
5878 OpTYPE_set(padop, type);
5880 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP);
5881 SvREFCNT_dec(PAD_SVl(padop->op_padix));
5882 PAD_SETSV(padop->op_padix, sv);
5884 padop->op_next = (OP*)padop;
5885 padop->op_flags = (U8)flags;
5886 if (PL_opargs[type] & OA_RETSCALAR)
5888 if (PL_opargs[type] & OA_TARGET)
5889 padop->op_targ = pad_alloc(type, SVs_PADTMP);
5890 return CHECKOP(type, padop);
5893 #endif /* USE_ITHREADS */
5896 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
5898 Constructs, checks, and returns an op of any type that involves an
5899 embedded reference to a GV. C<type> is the opcode. C<flags> gives the
5900 eight bits of C<op_flags>. C<gv> identifies the GV that the op should
5901 reference; calling this function does not transfer ownership of any
5908 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
5910 PERL_ARGS_ASSERT_NEWGVOP;
5913 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
5915 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
5920 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
5922 Constructs, checks, and returns an op of any type that involves an
5923 embedded C-level pointer (PV). C<type> is the opcode. C<flags> gives
5924 the eight bits of C<op_flags>. C<pv> supplies the C-level pointer, which
5925 must have been allocated using C<PerlMemShared_malloc>; the memory will
5926 be freed when the op is destroyed.
5932 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
5935 const bool utf8 = cBOOL(flags & SVf_UTF8);
5940 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5941 || type == OP_RUNCV || type == OP_CUSTOM
5942 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
5944 NewOp(1101, pvop, 1, PVOP);
5945 OpTYPE_set(pvop, type);
5947 pvop->op_next = (OP*)pvop;
5948 pvop->op_flags = (U8)flags;
5949 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
5950 if (PL_opargs[type] & OA_RETSCALAR)
5952 if (PL_opargs[type] & OA_TARGET)
5953 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
5954 return CHECKOP(type, pvop);
5958 Perl_package(pTHX_ OP *o)
5960 SV *const sv = cSVOPo->op_sv;
5962 PERL_ARGS_ASSERT_PACKAGE;
5964 SAVEGENERICSV(PL_curstash);
5965 save_item(PL_curstname);
5967 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
5969 sv_setsv(PL_curstname, sv);
5971 PL_hints |= HINT_BLOCK_SCOPE;
5972 PL_parser->copline = NOLINE;
5978 Perl_package_version( pTHX_ OP *v )
5980 U32 savehints = PL_hints;
5981 PERL_ARGS_ASSERT_PACKAGE_VERSION;
5982 PL_hints &= ~HINT_STRICT_VARS;
5983 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
5984 PL_hints = savehints;
5989 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
5994 SV *use_version = NULL;
5996 PERL_ARGS_ASSERT_UTILIZE;
5998 if (idop->op_type != OP_CONST)
5999 Perl_croak(aTHX_ "Module name must be constant");
6004 SV * const vesv = ((SVOP*)version)->op_sv;
6006 if (!arg && !SvNIOKp(vesv)) {
6013 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
6014 Perl_croak(aTHX_ "Version number must be a constant number");
6016 /* Make copy of idop so we don't free it twice */
6017 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
6019 /* Fake up a method call to VERSION */
6020 meth = newSVpvs_share("VERSION");
6021 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
6022 op_append_elem(OP_LIST,
6023 op_prepend_elem(OP_LIST, pack, version),
6024 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
6028 /* Fake up an import/unimport */
6029 if (arg && arg->op_type == OP_STUB) {
6030 imop = arg; /* no import on explicit () */
6032 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
6033 imop = NULL; /* use 5.0; */
6035 use_version = ((SVOP*)idop)->op_sv;
6037 idop->op_private |= OPpCONST_NOVER;
6042 /* Make copy of idop so we don't free it twice */
6043 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
6045 /* Fake up a method call to import/unimport */
6047 ? newSVpvs_share("import") : newSVpvs_share("unimport");
6048 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
6049 op_append_elem(OP_LIST,
6050 op_prepend_elem(OP_LIST, pack, arg),
6051 newMETHOP_named(OP_METHOD_NAMED, 0, meth)
6055 /* Fake up the BEGIN {}, which does its thing immediately. */
6057 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
6060 op_append_elem(OP_LINESEQ,
6061 op_append_elem(OP_LINESEQ,
6062 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
6063 newSTATEOP(0, NULL, veop)),
6064 newSTATEOP(0, NULL, imop) ));
6068 * feature bundle that corresponds to the required version. */
6069 use_version = sv_2mortal(new_version(use_version));
6070 S_enable_feature_bundle(aTHX_ use_version);
6072 /* If a version >= 5.11.0 is requested, strictures are on by default! */
6073 if (vcmp(use_version,
6074 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
6075 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
6076 PL_hints |= HINT_STRICT_REFS;
6077 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
6078 PL_hints |= HINT_STRICT_SUBS;
6079 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
6080 PL_hints |= HINT_STRICT_VARS;
6082 /* otherwise they are off */
6084 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
6085 PL_hints &= ~HINT_STRICT_REFS;
6086 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
6087 PL_hints &= ~HINT_STRICT_SUBS;
6088 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
6089 PL_hints &= ~HINT_STRICT_VARS;
6093 /* The "did you use incorrect case?" warning used to be here.
6094 * The problem is that on case-insensitive filesystems one
6095 * might get false positives for "use" (and "require"):
6096 * "use Strict" or "require CARP" will work. This causes
6097 * portability problems for the script: in case-strict
6098 * filesystems the script will stop working.
6100 * The "incorrect case" warning checked whether "use Foo"
6101 * imported "Foo" to your namespace, but that is wrong, too:
6102 * there is no requirement nor promise in the language that
6103 * a Foo.pm should or would contain anything in package "Foo".
6105 * There is very little Configure-wise that can be done, either:
6106 * the case-sensitivity of the build filesystem of Perl does not
6107 * help in guessing the case-sensitivity of the runtime environment.
6110 PL_hints |= HINT_BLOCK_SCOPE;
6111 PL_parser->copline = NOLINE;
6112 COP_SEQMAX_INC; /* Purely for B::*'s benefit */
6116 =head1 Embedding Functions
6118 =for apidoc load_module
6120 Loads the module whose name is pointed to by the string part of name.
6121 Note that the actual module name, not its filename, should be given.
6122 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
6123 C<PERL_LOADMOD_DENY>, C<PERL_LOADMOD_NOIMPORT>, or C<PERL_LOADMOD_IMPORT_OPS>
6124 (or 0 for no flags). ver, if specified
6125 and not NULL, provides version semantics
6126 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
6127 arguments can be used to specify arguments to the module's C<import()>
6128 method, similar to C<use Foo::Bar VERSION LIST>. They must be
6129 terminated with a final C<NULL> pointer. Note that this list can only
6130 be omitted when the C<PERL_LOADMOD_NOIMPORT> flag has been used.
6131 Otherwise at least a single C<NULL> pointer to designate the default
6132 import list is required.
6134 The reference count for each specified C<SV*> parameter is decremented.
6139 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
6143 PERL_ARGS_ASSERT_LOAD_MODULE;
6145 va_start(args, ver);
6146 vload_module(flags, name, ver, &args);
6150 #ifdef PERL_IMPLICIT_CONTEXT
6152 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
6156 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
6157 va_start(args, ver);
6158 vload_module(flags, name, ver, &args);
6164 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
6167 OP * const modname = newSVOP(OP_CONST, 0, name);
6169 PERL_ARGS_ASSERT_VLOAD_MODULE;
6171 modname->op_private |= OPpCONST_BARE;
6173 veop = newSVOP(OP_CONST, 0, ver);
6177 if (flags & PERL_LOADMOD_NOIMPORT) {
6178 imop = sawparens(newNULLLIST());
6180 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
6181 imop = va_arg(*args, OP*);
6186 sv = va_arg(*args, SV*);
6188 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
6189 sv = va_arg(*args, SV*);
6193 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
6194 * that it has a PL_parser to play with while doing that, and also
6195 * that it doesn't mess with any existing parser, by creating a tmp
6196 * new parser with lex_start(). This won't actually be used for much,
6197 * since pp_require() will create another parser for the real work.
6198 * The ENTER/LEAVE pair protect callers from any side effects of use. */
6201 SAVEVPTR(PL_curcop);
6202 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
6203 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
6204 veop, modname, imop);
6208 PERL_STATIC_INLINE OP *
6209 S_new_entersubop(pTHX_ GV *gv, OP *arg)
6211 return newUNOP(OP_ENTERSUB, OPf_STACKED,
6212 newLISTOP(OP_LIST, 0, arg,
6213 newUNOP(OP_RV2CV, 0,
6214 newGVOP(OP_GV, 0, gv))));
6218 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
6223 PERL_ARGS_ASSERT_DOFILE;
6225 if (!force_builtin && (gv = gv_override("do", 2))) {
6226 doop = S_new_entersubop(aTHX_ gv, term);
6229 doop = newUNOP(OP_DOFILE, 0, scalar(term));
6235 =head1 Optree construction
6237 =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval
6239 Constructs, checks, and returns an C<lslice> (list slice) op. C<flags>
6240 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
6241 be set automatically, and, shifted up eight bits, the eight bits of
6242 C<op_private>, except that the bit with value 1 or 2 is automatically
6243 set as required. C<listval> and C<subscript> supply the parameters of
6244 the slice; they are consumed by this function and become part of the
6245 constructed op tree.
6251 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
6253 return newBINOP(OP_LSLICE, flags,
6254 list(force_list(subscript, 1)),
6255 list(force_list(listval, 1)) );
6258 #define ASSIGN_LIST 1
6259 #define ASSIGN_REF 2
6262 S_assignment_type(pTHX_ const OP *o)
6271 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
6272 o = cUNOPo->op_first;
6274 flags = o->op_flags;
6276 if (type == OP_COND_EXPR) {
6277 OP * const sib = OpSIBLING(cLOGOPo->op_first);
6278 const I32 t = assignment_type(sib);
6279 const I32 f = assignment_type(OpSIBLING(sib));
6281 if (t == ASSIGN_LIST && f == ASSIGN_LIST)
6283 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST))
6284 yyerror("Assignment to both a list and a scalar");
6288 if (type == OP_SREFGEN)
6290 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first;
6291 type = kid->op_type;
6292 flags |= kid->op_flags;
6293 if (!(flags & OPf_PARENS)
6294 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV ||
6295 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV ))
6301 if (type == OP_LIST &&
6302 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
6303 o->op_private & OPpLVAL_INTRO)
6306 if (type == OP_LIST || flags & OPf_PARENS ||
6307 type == OP_RV2AV || type == OP_RV2HV ||
6308 type == OP_ASLICE || type == OP_HSLICE ||
6309 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN)
6312 if (type == OP_PADAV || type == OP_PADHV)
6315 if (type == OP_RV2SV)
6323 =for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right
6325 Constructs, checks, and returns an assignment op. C<left> and C<right>
6326 supply the parameters of the assignment; they are consumed by this
6327 function and become part of the constructed op tree.
6329 If C<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
6330 a suitable conditional optree is constructed. If C<optype> is the opcode
6331 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
6332 performs the binary operation and assigns the result to the left argument.
6333 Either way, if C<optype> is non-zero then C<flags> has no effect.
6335 If C<optype> is zero, then a plain scalar or list assignment is
6336 constructed. Which type of assignment it is is automatically determined.
6337 C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
6338 will be set automatically, and, shifted up eight bits, the eight bits
6339 of C<op_private>, except that the bit with value 1 or 2 is automatically
6346 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
6352 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
6353 return newLOGOP(optype, 0,
6354 op_lvalue(scalar(left), optype),
6355 newUNOP(OP_SASSIGN, 0, scalar(right)));
6358 return newBINOP(optype, OPf_STACKED,
6359 op_lvalue(scalar(left), optype), scalar(right));
6363 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) {
6364 static const char no_list_state[] = "Initialization of state variables"
6365 " in list context currently forbidden";
6368 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE)
6369 left->op_private &= ~ OPpSLICEWARNING;
6372 left = op_lvalue(left, OP_AASSIGN);
6373 curop = list(force_list(left, 1));
6374 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, 1)), curop);
6375 o->op_private = (U8)(0 | (flags >> 8));
6377 if (OP_TYPE_IS_OR_WAS(left, OP_LIST))
6379 OP* lop = ((LISTOP*)left)->op_first;
6381 if ((lop->op_type == OP_PADSV ||
6382 lop->op_type == OP_PADAV ||
6383 lop->op_type == OP_PADHV ||
6384 lop->op_type == OP_PADANY)
6385 && (lop->op_private & OPpPAD_STATE)
6387 yyerror(no_list_state);
6388 lop = OpSIBLING(lop);
6391 else if ( (left->op_private & OPpLVAL_INTRO)
6392 && (left->op_private & OPpPAD_STATE)
6393 && ( left->op_type == OP_PADSV
6394 || left->op_type == OP_PADAV
6395 || left->op_type == OP_PADHV
6396 || left->op_type == OP_PADANY)
6398 /* All single variable list context state assignments, hence
6408 yyerror(no_list_state);
6411 if (right && right->op_type == OP_SPLIT
6412 && !(right->op_flags & OPf_STACKED)) {
6413 OP* tmpop = ((LISTOP*)right)->op_first;
6414 PMOP * const pm = (PMOP*)tmpop;
6415 assert (tmpop && (tmpop->op_type == OP_PUSHRE));
6418 !pm->op_pmreplrootu.op_pmtargetoff
6420 !pm->op_pmreplrootu.op_pmtargetgv
6424 if (!(left->op_private & OPpLVAL_INTRO) &&
6425 ( (left->op_type == OP_RV2AV &&
6426 (tmpop=((UNOP*)left)->op_first)->op_type==OP_GV)
6427 || left->op_type == OP_PADAV )
6429 if (tmpop != (OP *)pm) {
6431 pm->op_pmreplrootu.op_pmtargetoff
6432 = cPADOPx(tmpop)->op_padix;
6433 cPADOPx(tmpop)->op_padix = 0; /* steal it */
6435 pm->op_pmreplrootu.op_pmtargetgv
6436 = MUTABLE_GV(cSVOPx(tmpop)->op_sv);
6437 cSVOPx(tmpop)->op_sv = NULL; /* steal it */
6439 right->op_private |=
6440 left->op_private & OPpOUR_INTRO;
6443 pm->op_targ = left->op_targ;
6444 left->op_targ = 0; /* filch it */
6447 tmpop = cUNOPo->op_first; /* to list (nulled) */
6448 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
6449 /* detach rest of siblings from o subtree,
6450 * and free subtree */
6451 op_sibling_splice(cUNOPo->op_first, tmpop, -1, NULL);
6452 op_free(o); /* blow off assign */
6453 right->op_flags &= ~OPf_WANT;
6454 /* "I don't know and I don't care." */
6457 else if (left->op_type == OP_RV2AV
6458 || left->op_type == OP_PADAV)
6460 /* Detach the array. */
6464 op_sibling_splice(cBINOPo->op_last,
6465 cUNOPx(cBINOPo->op_last)
6466 ->op_first, 1, NULL);
6467 assert(ary == left);
6468 /* Attach it to the split. */
6469 op_sibling_splice(right, cLISTOPx(right)->op_last,
6471 right->op_flags |= OPf_STACKED;
6472 /* Detach split and expunge aassign as above. */
6475 else if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
6476 ((LISTOP*)right)->op_last->op_type == OP_CONST)
6479 &((SVOP*)((LISTOP*)right)->op_last)->op_sv;
6480 SV * const sv = *svp;
6481 if (SvIOK(sv) && SvIVX(sv) == 0)
6483 if (right->op_private & OPpSPLIT_IMPLIM) {
6484 /* our own SV, created in ck_split */
6486 sv_setiv(sv, PL_modcount+1);
6489 /* SV may belong to someone else */
6491 *svp = newSViv(PL_modcount+1);
6499 if (assign_type == ASSIGN_REF)
6500 return newBINOP(OP_REFASSIGN, flags, scalar(right), left);
6502 right = newOP(OP_UNDEF, 0);
6503 if (right->op_type == OP_READLINE) {
6504 right->op_flags |= OPf_STACKED;
6505 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
6509 o = newBINOP(OP_SASSIGN, flags,
6510 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
6516 =for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o
6518 Constructs a state op (COP). The state op is normally a C<nextstate> op,
6519 but will be a C<dbstate> op if debugging is enabled for currently-compiled
6520 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>).
6521 If C<label> is non-null, it supplies the name of a label to attach to
6522 the state op; this function takes ownership of the memory pointed at by
6523 C<label>, and will free it. C<flags> gives the eight bits of C<op_flags>
6526 If C<o> is null, the state op is returned. Otherwise the state op is
6527 combined with C<o> into a C<lineseq> list op, which is returned. C<o>
6528 is consumed by this function and becomes part of the returned op tree.
6534 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
6537 const U32 seq = intro_my();
6538 const U32 utf8 = flags & SVf_UTF8;
6541 PL_parser->parsed_sub = 0;
6545 NewOp(1101, cop, 1, COP);
6546 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
6547 OpTYPE_set(cop, OP_DBSTATE);
6550 OpTYPE_set(cop, OP_NEXTSTATE);
6552 cop->op_flags = (U8)flags;
6553 CopHINTS_set(cop, PL_hints);
6555 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH;
6557 cop->op_next = (OP*)cop;
6560 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
6561 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
6563 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
6565 PL_hints |= HINT_BLOCK_SCOPE;
6566 /* It seems that we need to defer freeing this pointer, as other parts
6567 of the grammar end up wanting to copy it after this op has been
6572 if (PL_parser->preambling != NOLINE) {
6573 CopLINE_set(cop, PL_parser->preambling);
6574 PL_parser->copline = NOLINE;
6576 else if (PL_parser->copline == NOLINE)
6577 CopLINE_set(cop, CopLINE(PL_curcop));
6579 CopLINE_set(cop, PL_parser->copline);
6580 PL_parser->copline = NOLINE;
6583 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
6585 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
6587 CopSTASH_set(cop, PL_curstash);
6589 if (cop->op_type == OP_DBSTATE) {
6590 /* this line can have a breakpoint - store the cop in IV */
6591 AV *av = CopFILEAVx(PL_curcop);
6593 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
6594 if (svp && *svp != &PL_sv_undef ) {
6595 (void)SvIOK_on(*svp);
6596 SvIV_set(*svp, PTR2IV(cop));
6601 if (flags & OPf_SPECIAL)
6603 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
6607 =for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other
6609 Constructs, checks, and returns a logical (flow control) op. C<type>
6610 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
6611 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
6612 the eight bits of C<op_private>, except that the bit with value 1 is
6613 automatically set. C<first> supplies the expression controlling the
6614 flow, and C<other> supplies the side (alternate) chain of ops; they are
6615 consumed by this function and become part of the constructed op tree.
6621 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
6623 PERL_ARGS_ASSERT_NEWLOGOP;
6625 return new_logop(type, flags, &first, &other);
6629 S_search_const(pTHX_ OP *o)
6631 PERL_ARGS_ASSERT_SEARCH_CONST;
6633 switch (o->op_type) {
6637 if (o->op_flags & OPf_KIDS)
6638 return search_const(cUNOPo->op_first);
6645 if (!(o->op_flags & OPf_KIDS))
6647 kid = cLISTOPo->op_first;
6649 switch (kid->op_type) {
6653 kid = OpSIBLING(kid);
6656 if (kid != cLISTOPo->op_last)
6662 kid = cLISTOPo->op_last;
6664 return search_const(kid);
6672 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
6680 int prepend_not = 0;
6682 PERL_ARGS_ASSERT_NEW_LOGOP;
6687 /* [perl #59802]: Warn about things like "return $a or $b", which
6688 is parsed as "(return $a) or $b" rather than "return ($a or
6689 $b)". NB: This also applies to xor, which is why we do it
6692 switch (first->op_type) {
6696 /* XXX: Perhaps we should emit a stronger warning for these.
6697 Even with the high-precedence operator they don't seem to do
6700 But until we do, fall through here.
6706 /* XXX: Currently we allow people to "shoot themselves in the
6707 foot" by explicitly writing "(return $a) or $b".
6709 Warn unless we are looking at the result from folding or if
6710 the programmer explicitly grouped the operators like this.
6711 The former can occur with e.g.
6713 use constant FEATURE => ( $] >= ... );
6714 sub { not FEATURE and return or do_stuff(); }
6716 if (!first->op_folded && !(first->op_flags & OPf_PARENS))
6717 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
6718 "Possible precedence issue with control flow operator");
6719 /* XXX: Should we optimze this to "return $a;" (i.e. remove
6725 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
6726 return newBINOP(type, flags, scalar(first), scalar(other));
6728 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP
6729 || type == OP_CUSTOM);
6731 scalarboolean(first);
6732 /* optimize AND and OR ops that have NOTs as children */
6733 if (first->op_type == OP_NOT
6734 && (first->op_flags & OPf_KIDS)
6735 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
6736 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
6738 if (type == OP_AND || type == OP_OR) {
6744 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
6746 prepend_not = 1; /* prepend a NOT op later */
6750 /* search for a constant op that could let us fold the test */
6751 if ((cstop = search_const(first))) {
6752 if (cstop->op_private & OPpCONST_STRICT)
6753 no_bareword_allowed(cstop);
6754 else if ((cstop->op_private & OPpCONST_BARE))
6755 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
6756 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) ||
6757 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
6758 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
6760 if (other->op_type == OP_CONST)
6761 other->op_private |= OPpCONST_SHORTCIRCUIT;
6763 if (other->op_type == OP_LEAVE)
6764 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
6765 else if (other->op_type == OP_MATCH
6766 || other->op_type == OP_SUBST
6767 || other->op_type == OP_TRANSR
6768 || other->op_type == OP_TRANS)
6769 /* Mark the op as being unbindable with =~ */
6770 other->op_flags |= OPf_SPECIAL;
6772 other->op_folded = 1;
6776 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
6777 const OP *o2 = other;
6778 if ( ! (o2->op_type == OP_LIST
6779 && (( o2 = cUNOPx(o2)->op_first))
6780 && o2->op_type == OP_PUSHMARK
6781 && (( o2 = OpSIBLING(o2))) )
6784 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
6785 || o2->op_type == OP_PADHV)
6786 && o2->op_private & OPpLVAL_INTRO
6787 && !(o2->op_private & OPpPAD_STATE))
6789 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
6790 "Deprecated use of my() in false conditional");
6794 if (cstop->op_type == OP_CONST)
6795 cstop->op_private |= OPpCONST_SHORTCIRCUIT;
6800 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
6801 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
6803 const OP * const k1 = ((UNOP*)first)->op_first;
6804 const OP * const k2 = OpSIBLING(k1);
6806 switch (first->op_type)
6809 if (k2 && k2->op_type == OP_READLINE
6810 && (k2->op_flags & OPf_STACKED)
6811 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6813 warnop = k2->op_type;
6818 if (k1->op_type == OP_READDIR
6819 || k1->op_type == OP_GLOB
6820 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6821 || k1->op_type == OP_EACH
6822 || k1->op_type == OP_AEACH)
6824 warnop = ((k1->op_type == OP_NULL)
6825 ? (OPCODE)k1->op_targ : k1->op_type);
6830 const line_t oldline = CopLINE(PL_curcop);
6831 /* This ensures that warnings are reported at the first line
6832 of the construction, not the last. */
6833 CopLINE_set(PL_curcop, PL_parser->copline);
6834 Perl_warner(aTHX_ packWARN(WARN_MISC),
6835 "Value of %s%s can be \"0\"; test with defined()",
6837 ((warnop == OP_READLINE || warnop == OP_GLOB)
6838 ? " construct" : "() operator"));
6839 CopLINE_set(PL_curcop, oldline);
6846 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
6847 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
6849 logop = S_alloc_LOGOP(aTHX_ type, first, LINKLIST(other));
6850 logop->op_flags |= (U8)flags;
6851 logop->op_private = (U8)(1 | (flags >> 8));
6853 /* establish postfix order */
6854 logop->op_next = LINKLIST(first);
6855 first->op_next = (OP*)logop;
6856 assert(!OpHAS_SIBLING(first));
6857 op_sibling_splice((OP*)logop, first, 0, other);
6859 CHECKOP(type,logop);
6861 o = newUNOP(prepend_not ? OP_NOT : OP_NULL,
6862 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0,
6870 =for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop
6872 Constructs, checks, and returns a conditional-expression (C<cond_expr>)
6873 op. C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
6874 will be set automatically, and, shifted up eight bits, the eight bits of
6875 C<op_private>, except that the bit with value 1 is automatically set.
6876 C<first> supplies the expression selecting between the two branches,
6877 and C<trueop> and C<falseop> supply the branches; they are consumed by
6878 this function and become part of the constructed op tree.
6884 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
6892 PERL_ARGS_ASSERT_NEWCONDOP;
6895 return newLOGOP(OP_AND, 0, first, trueop);
6897 return newLOGOP(OP_OR, 0, first, falseop);
6899 scalarboolean(first);
6900 if ((cstop = search_const(first))) {
6901 /* Left or right arm of the conditional? */
6902 const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
6903 OP *live = left ? trueop : falseop;
6904 OP *const dead = left ? falseop : trueop;
6905 if (cstop->op_private & OPpCONST_BARE &&
6906 cstop->op_private & OPpCONST_STRICT) {
6907 no_bareword_allowed(cstop);
6911 if (live->op_type == OP_LEAVE)
6912 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
6913 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
6914 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
6915 /* Mark the op as being unbindable with =~ */
6916 live->op_flags |= OPf_SPECIAL;
6917 live->op_folded = 1;
6920 logop = S_alloc_LOGOP(aTHX_ OP_COND_EXPR, first, LINKLIST(trueop));
6921 logop->op_flags |= (U8)flags;
6922 logop->op_private = (U8)(1 | (flags >> 8));
6923 logop->op_next = LINKLIST(falseop);
6925 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
6928 /* establish postfix order */
6929 start = LINKLIST(first);
6930 first->op_next = (OP*)logop;
6932 /* make first, trueop, falseop siblings */
6933 op_sibling_splice((OP*)logop, first, 0, trueop);
6934 op_sibling_splice((OP*)logop, trueop, 0, falseop);
6936 o = newUNOP(OP_NULL, 0, (OP*)logop);
6938 trueop->op_next = falseop->op_next = o;
6945 =for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right
6947 Constructs and returns a C<range> op, with subordinate C<flip> and
6948 C<flop> ops. C<flags> gives the eight bits of C<op_flags> for the
6949 C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
6950 for both the C<flip> and C<range> ops, except that the bit with value
6951 1 is automatically set. C<left> and C<right> supply the expressions
6952 controlling the endpoints of the range; they are consumed by this function
6953 and become part of the constructed op tree.
6959 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
6967 PERL_ARGS_ASSERT_NEWRANGE;
6969 range = S_alloc_LOGOP(aTHX_ OP_RANGE, left, LINKLIST(right));
6970 range->op_flags = OPf_KIDS;
6971 leftstart = LINKLIST(left);
6972 range->op_private = (U8)(1 | (flags >> 8));
6974 /* make left and right siblings */
6975 op_sibling_splice((OP*)range, left, 0, right);
6977 range->op_next = (OP*)range;
6978 flip = newUNOP(OP_FLIP, flags, (OP*)range);
6979 flop = newUNOP(OP_FLOP, 0, flip);
6980 o = newUNOP(OP_NULL, 0, flop);
6982 range->op_next = leftstart;
6984 left->op_next = flip;
6985 right->op_next = flop;
6988 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);
6989 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
6991 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);;
6992 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
6993 SvPADTMP_on(PAD_SV(flip->op_targ));
6995 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6996 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6998 /* check barewords before they might be optimized aways */
6999 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
7000 no_bareword_allowed(left);
7001 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
7002 no_bareword_allowed(right);
7005 if (!flip->op_private || !flop->op_private)
7006 LINKLIST(o); /* blow off optimizer unless constant */
7012 =for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block
7014 Constructs, checks, and returns an op tree expressing a loop. This is
7015 only a loop in the control flow through the op tree; it does not have
7016 the heavyweight loop structure that allows exiting the loop by C<last>
7017 and suchlike. C<flags> gives the eight bits of C<op_flags> for the
7018 top-level op, except that some bits will be set automatically as required.
7019 C<expr> supplies the expression controlling loop iteration, and C<block>
7020 supplies the body of the loop; they are consumed by this function and
7021 become part of the constructed op tree. C<debuggable> is currently
7022 unused and should always be 1.
7028 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
7032 const bool once = block && block->op_flags & OPf_SPECIAL &&
7033 block->op_type == OP_NULL;
7035 PERL_UNUSED_ARG(debuggable);
7039 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
7040 || ( expr->op_type == OP_NOT
7041 && cUNOPx(expr)->op_first->op_type == OP_CONST
7042 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first))
7045 /* Return the block now, so that S_new_logop does not try to
7047 return block; /* do {} while 0 does once */
7048 if (expr->op_type == OP_READLINE
7049 || expr->op_type == OP_READDIR
7050 || expr->op_type == OP_GLOB
7051 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
7052 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
7053 expr = newUNOP(OP_DEFINED, 0,
7054 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
7055 } else if (expr->op_flags & OPf_KIDS) {
7056 const OP * const k1 = ((UNOP*)expr)->op_first;
7057 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL;
7058 switch (expr->op_type) {
7060 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
7061 && (k2->op_flags & OPf_STACKED)
7062 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
7063 expr = newUNOP(OP_DEFINED, 0, expr);
7067 if (k1 && (k1->op_type == OP_READDIR
7068 || k1->op_type == OP_GLOB
7069 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
7070 || k1->op_type == OP_EACH
7071 || k1->op_type == OP_AEACH))
7072 expr = newUNOP(OP_DEFINED, 0, expr);
7078 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
7079 * op, in listop. This is wrong. [perl #27024] */
7081 block = newOP(OP_NULL, 0);
7082 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
7083 o = new_logop(OP_AND, 0, &expr, &listop);
7090 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
7092 if (once && o != listop)
7094 assert(cUNOPo->op_first->op_type == OP_AND
7095 || cUNOPo->op_first->op_type == OP_OR);
7096 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
7100 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
7102 o->op_flags |= flags;
7104 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
7109 =for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my
7111 Constructs, checks, and returns an op tree expressing a C<while> loop.
7112 This is a heavyweight loop, with structure that allows exiting the loop
7113 by C<last> and suchlike.
7115 C<loop> is an optional preconstructed C<enterloop> op to use in the
7116 loop; if it is null then a suitable op will be constructed automatically.
7117 C<expr> supplies the loop's controlling expression. C<block> supplies the
7118 main body of the loop, and C<cont> optionally supplies a C<continue> block
7119 that operates as a second half of the body. All of these optree inputs
7120 are consumed by this function and become part of the constructed op tree.
7122 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
7123 op and, shifted up eight bits, the eight bits of C<op_private> for
7124 the C<leaveloop> op, except that (in both cases) some bits will be set
7125 automatically. C<debuggable> is currently unused and should always be 1.
7126 C<has_my> can be supplied as true to force the
7127 loop body to be enclosed in its own scope.
7133 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
7134 OP *expr, OP *block, OP *cont, I32 has_my)
7143 PERL_UNUSED_ARG(debuggable);
7146 if (expr->op_type == OP_READLINE
7147 || expr->op_type == OP_READDIR
7148 || expr->op_type == OP_GLOB
7149 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
7150 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
7151 expr = newUNOP(OP_DEFINED, 0,
7152 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
7153 } else if (expr->op_flags & OPf_KIDS) {
7154 const OP * const k1 = ((UNOP*)expr)->op_first;
7155 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL;
7156 switch (expr->op_type) {
7158 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
7159 && (k2->op_flags & OPf_STACKED)
7160 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
7161 expr = newUNOP(OP_DEFINED, 0, expr);
7165 if (k1 && (k1->op_type == OP_READDIR
7166 || k1->op_type == OP_GLOB
7167 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
7168 || k1->op_type == OP_EACH
7169 || k1->op_type == OP_AEACH))
7170 expr = newUNOP(OP_DEFINED, 0, expr);
7177 block = newOP(OP_NULL, 0);
7178 else if (cont || has_my) {
7179 block = op_scope(block);
7183 next = LINKLIST(cont);
7186 OP * const unstack = newOP(OP_UNSTACK, 0);
7189 cont = op_append_elem(OP_LINESEQ, cont, unstack);
7193 listop = op_append_list(OP_LINESEQ, block, cont);
7195 redo = LINKLIST(listop);
7199 o = new_logop(OP_AND, 0, &expr, &listop);
7200 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
7202 return expr; /* listop already freed by new_logop */
7205 ((LISTOP*)listop)->op_last->op_next =
7206 (o == listop ? redo : LINKLIST(o));
7212 NewOp(1101,loop,1,LOOP);
7213 OpTYPE_set(loop, OP_ENTERLOOP);
7214 loop->op_private = 0;
7215 loop->op_next = (OP*)loop;
7218 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
7220 loop->op_redoop = redo;
7221 loop->op_lastop = o;
7222 o->op_private |= loopflags;
7225 loop->op_nextop = next;
7227 loop->op_nextop = o;
7229 o->op_flags |= flags;
7230 o->op_private |= (flags >> 8);
7235 =for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont
7237 Constructs, checks, and returns an op tree expressing a C<foreach>
7238 loop (iteration through a list of values). This is a heavyweight loop,
7239 with structure that allows exiting the loop by C<last> and suchlike.
7241 C<sv> optionally supplies the variable that will be aliased to each
7242 item in turn; if null, it defaults to C<$_>.
7243 C<expr> supplies the list of values to iterate over. C<block> supplies
7244 the main body of the loop, and C<cont> optionally supplies a C<continue>
7245 block that operates as a second half of the body. All of these optree
7246 inputs are consumed by this function and become part of the constructed
7249 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
7250 op and, shifted up eight bits, the eight bits of C<op_private> for
7251 the C<leaveloop> op, except that (in both cases) some bits will be set
7258 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
7263 PADOFFSET padoff = 0;
7267 PERL_ARGS_ASSERT_NEWFOROP;
7270 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
7271 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
7272 OpTYPE_set(sv, OP_RV2GV);
7274 /* The op_type check is needed to prevent a possible segfault
7275 * if the loop variable is undeclared and 'strict vars' is in
7276 * effect. This is illegal but is nonetheless parsed, so we
7277 * may reach this point with an OP_CONST where we're expecting
7280 if (cUNOPx(sv)->op_first->op_type == OP_GV
7281 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
7282 iterpflags |= OPpITER_DEF;
7284 else if (sv->op_type == OP_PADSV) { /* private variable */
7285 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
7286 padoff = sv->op_targ;
7290 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX);
7292 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN)
7295 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
7297 PADNAME * const pn = PAD_COMPNAME(padoff);
7298 const char * const name = PadnamePV(pn);
7300 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_')
7301 iterpflags |= OPpITER_DEF;
7305 sv = newGVOP(OP_GV, 0, PL_defgv);
7306 iterpflags |= OPpITER_DEF;
7309 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
7310 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), 1), OP_GREPSTART);
7311 iterflags |= OPf_STACKED;
7313 else if (expr->op_type == OP_NULL &&
7314 (expr->op_flags & OPf_KIDS) &&
7315 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
7317 /* Basically turn for($x..$y) into the same as for($x,$y), but we
7318 * set the STACKED flag to indicate that these values are to be
7319 * treated as min/max values by 'pp_enteriter'.
7321 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
7322 LOGOP* const range = (LOGOP*) flip->op_first;
7323 OP* const left = range->op_first;
7324 OP* const right = OpSIBLING(left);
7327 range->op_flags &= ~OPf_KIDS;
7328 /* detach range's children */
7329 op_sibling_splice((OP*)range, NULL, -1, NULL);
7331 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
7332 listop->op_first->op_next = range->op_next;
7333 left->op_next = range->op_other;
7334 right->op_next = (OP*)listop;
7335 listop->op_next = listop->op_first;
7338 expr = (OP*)(listop);
7340 iterflags |= OPf_STACKED;
7343 expr = op_lvalue(force_list(expr, 1), OP_GREPSTART);
7346 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags,
7347 op_append_elem(OP_LIST, list(expr),
7349 assert(!loop->op_next);
7350 /* for my $x () sets OPpLVAL_INTRO;
7351 * for our $x () sets OPpOUR_INTRO */
7352 loop->op_private = (U8)iterpflags;
7353 if (loop->op_slabbed
7354 && DIFF(loop, OpSLOT(loop)->opslot_next)
7355 < SIZE_TO_PSIZE(sizeof(LOOP)))
7358 NewOp(1234,tmp,1,LOOP);
7359 Copy(loop,tmp,1,LISTOP);
7360 #ifdef PERL_OP_PARENT
7361 assert(loop->op_last->op_sibparent == (OP*)loop);
7362 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */
7364 S_op_destroy(aTHX_ (OP*)loop);
7367 else if (!loop->op_slabbed)
7369 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
7370 #ifdef PERL_OP_PARENT
7371 OpLASTSIB_set(loop->op_last, (OP*)loop);
7374 loop->op_targ = padoff;
7375 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0);
7380 =for apidoc Am|OP *|newLOOPEX|I32 type|OP *label
7382 Constructs, checks, and returns a loop-exiting op (such as C<goto>
7383 or C<last>). C<type> is the opcode. C<label> supplies the parameter
7384 determining the target of the op; it is consumed by this function and
7385 becomes part of the constructed op tree.
7391 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
7395 PERL_ARGS_ASSERT_NEWLOOPEX;
7397 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
7398 || type == OP_CUSTOM);
7400 if (type != OP_GOTO) {
7401 /* "last()" means "last" */
7402 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) {
7403 o = newOP(type, OPf_SPECIAL);
7407 /* Check whether it's going to be a goto &function */
7408 if (label->op_type == OP_ENTERSUB
7409 && !(label->op_flags & OPf_STACKED))
7410 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
7413 /* Check for a constant argument */
7414 if (label->op_type == OP_CONST) {
7415 SV * const sv = ((SVOP *)label)->op_sv;
7417 const char *s = SvPV_const(sv,l);
7418 if (l == strlen(s)) {
7420 SvUTF8(((SVOP*)label)->op_sv),
7422 SvPV_nolen_const(((SVOP*)label)->op_sv)));
7426 /* If we have already created an op, we do not need the label. */
7429 else o = newUNOP(type, OPf_STACKED, label);
7431 PL_hints |= HINT_BLOCK_SCOPE;
7435 /* if the condition is a literal array or hash
7436 (or @{ ... } etc), make a reference to it.
7439 S_ref_array_or_hash(pTHX_ OP *cond)
7442 && (cond->op_type == OP_RV2AV
7443 || cond->op_type == OP_PADAV
7444 || cond->op_type == OP_RV2HV
7445 || cond->op_type == OP_PADHV))
7447 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
7450 && (cond->op_type == OP_ASLICE
7451 || cond->op_type == OP_KVASLICE
7452 || cond->op_type == OP_HSLICE
7453 || cond->op_type == OP_KVHSLICE)) {
7455 /* anonlist now needs a list from this op, was previously used in
7457 cond->op_flags &= ~(OPf_WANT_SCALAR | OPf_REF);
7458 cond->op_flags |= OPf_WANT_LIST;
7460 return newANONLIST(op_lvalue(cond, OP_ANONLIST));
7467 /* These construct the optree fragments representing given()
7470 entergiven and enterwhen are LOGOPs; the op_other pointer
7471 points up to the associated leave op. We need this so we
7472 can put it in the context and make break/continue work.
7473 (Also, of course, pp_enterwhen will jump straight to
7474 op_other if the match fails.)
7478 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
7479 I32 enter_opcode, I32 leave_opcode,
7480 PADOFFSET entertarg)
7486 PERL_ARGS_ASSERT_NEWGIVWHENOP;
7487 PERL_UNUSED_ARG(entertarg); /* used to indicate targ of lexical $_ */
7489 enterop = S_alloc_LOGOP(aTHX_ enter_opcode, block, NULL);
7490 enterop->op_targ = 0;
7491 enterop->op_private = 0;
7493 o = newUNOP(leave_opcode, 0, (OP *) enterop);
7496 /* prepend cond if we have one */
7497 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond));
7499 o->op_next = LINKLIST(cond);
7500 cond->op_next = (OP *) enterop;
7503 /* This is a default {} block */
7504 enterop->op_flags |= OPf_SPECIAL;
7505 o ->op_flags |= OPf_SPECIAL;
7507 o->op_next = (OP *) enterop;
7510 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
7511 entergiven and enterwhen both
7514 enterop->op_next = LINKLIST(block);
7515 block->op_next = enterop->op_other = o;
7520 /* Does this look like a boolean operation? For these purposes
7521 a boolean operation is:
7522 - a subroutine call [*]
7523 - a logical connective
7524 - a comparison operator
7525 - a filetest operator, with the exception of -s -M -A -C
7526 - defined(), exists() or eof()
7527 - /$re/ or $foo =~ /$re/
7529 [*] possibly surprising
7532 S_looks_like_bool(pTHX_ const OP *o)
7534 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
7536 switch(o->op_type) {
7539 return looks_like_bool(cLOGOPo->op_first);
7543 OP* sibl = OpSIBLING(cLOGOPo->op_first);
7546 looks_like_bool(cLOGOPo->op_first)
7547 && looks_like_bool(sibl));
7553 o->op_flags & OPf_KIDS
7554 && looks_like_bool(cUNOPo->op_first));
7558 case OP_NOT: case OP_XOR:
7560 case OP_EQ: case OP_NE: case OP_LT:
7561 case OP_GT: case OP_LE: case OP_GE:
7563 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
7564 case OP_I_GT: case OP_I_LE: case OP_I_GE:
7566 case OP_SEQ: case OP_SNE: case OP_SLT:
7567 case OP_SGT: case OP_SLE: case OP_SGE:
7571 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
7572 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
7573 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
7574 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
7575 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
7576 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
7577 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
7578 case OP_FTTEXT: case OP_FTBINARY:
7580 case OP_DEFINED: case OP_EXISTS:
7581 case OP_MATCH: case OP_EOF:
7588 /* Detect comparisons that have been optimized away */
7589 if (cSVOPo->op_sv == &PL_sv_yes
7590 || cSVOPo->op_sv == &PL_sv_no)
7603 =for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off
7605 Constructs, checks, and returns an op tree expressing a C<given> block.
7606 C<cond> supplies the expression that will be locally assigned to a lexical
7607 variable, and C<block> supplies the body of the C<given> construct; they
7608 are consumed by this function and become part of the constructed op tree.
7609 C<defsv_off> must be zero (it used to identity the pad slot of lexical $_).
7615 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
7617 PERL_ARGS_ASSERT_NEWGIVENOP;
7618 PERL_UNUSED_ARG(defsv_off);
7621 return newGIVWHENOP(
7622 ref_array_or_hash(cond),
7624 OP_ENTERGIVEN, OP_LEAVEGIVEN,
7629 =for apidoc Am|OP *|newWHENOP|OP *cond|OP *block
7631 Constructs, checks, and returns an op tree expressing a C<when> block.
7632 C<cond> supplies the test expression, and C<block> supplies the block
7633 that will be executed if the test evaluates to true; they are consumed
7634 by this function and become part of the constructed op tree. C<cond>
7635 will be interpreted DWIMically, often as a comparison against C<$_>,
7636 and may be null to generate a C<default> block.
7642 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
7644 const bool cond_llb = (!cond || looks_like_bool(cond));
7647 PERL_ARGS_ASSERT_NEWWHENOP;
7652 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
7654 scalar(ref_array_or_hash(cond)));
7657 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
7660 /* must not conflict with SVf_UTF8 */
7661 #define CV_CKPROTO_CURSTASH 0x1
7664 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
7665 const STRLEN len, const U32 flags)
7667 SV *name = NULL, *msg;
7668 const char * cvp = SvROK(cv)
7669 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV
7670 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv))
7673 STRLEN clen = CvPROTOLEN(cv), plen = len;
7675 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
7677 if (p == NULL && cvp == NULL)
7680 if (!ckWARN_d(WARN_PROTOTYPE))
7684 p = S_strip_spaces(aTHX_ p, &plen);
7685 cvp = S_strip_spaces(aTHX_ cvp, &clen);
7686 if ((flags & SVf_UTF8) == SvUTF8(cv)) {
7687 if (plen == clen && memEQ(cvp, p, plen))
7690 if (flags & SVf_UTF8) {
7691 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0)
7695 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0)
7701 msg = sv_newmortal();
7706 gv_efullname3(name = sv_newmortal(), gv, NULL);
7707 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&')
7708 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP);
7709 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) {
7710 name = sv_2mortal(newSVhek(HvNAME_HEK(PL_curstash)));
7711 sv_catpvs(name, "::");
7713 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV);
7714 assert (CvNAMED(SvRV_const(gv)));
7715 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv))));
7717 else sv_catsv(name, (SV *)gv);
7719 else name = (SV *)gv;
7721 sv_setpvs(msg, "Prototype mismatch:");
7723 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, SVfARG(name));
7725 Perl_sv_catpvf(aTHX_ msg, " (%"UTF8f")",
7726 UTF8fARG(SvUTF8(cv),clen,cvp)
7729 sv_catpvs(msg, ": none");
7730 sv_catpvs(msg, " vs ");
7732 Perl_sv_catpvf(aTHX_ msg, "(%"UTF8f")", UTF8fARG(flags & SVf_UTF8,len,p));
7734 sv_catpvs(msg, "none");
7735 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, SVfARG(msg));
7738 static void const_sv_xsub(pTHX_ CV* cv);
7739 static void const_av_xsub(pTHX_ CV* cv);
7743 =head1 Optree Manipulation Functions
7745 =for apidoc cv_const_sv
7747 If C<cv> is a constant sub eligible for inlining, returns the constant
7748 value returned by the sub. Otherwise, returns C<NULL>.
7750 Constant subs can be created with C<newCONSTSUB> or as described in
7751 L<perlsub/"Constant Functions">.
7756 Perl_cv_const_sv(const CV *const cv)
7761 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
7763 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7764 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL;
7769 Perl_cv_const_sv_or_av(const CV * const cv)
7773 if (SvROK(cv)) return SvRV((SV *)cv);
7774 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
7775 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7778 /* op_const_sv: examine an optree to determine whether it's in-lineable.
7779 * Can be called in 2 ways:
7782 * look for a single OP_CONST with attached value: return the value
7784 * allow_lex && !CvCONST(cv);
7786 * examine the clone prototype, and if contains only a single
7787 * OP_CONST, return the value; or if it contains a single PADSV ref-
7788 * erencing an outer lexical, turn on CvCONST to indicate the CV is
7789 * a candidate for "constizing" at clone time, and return NULL.
7793 S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex)
7801 for (; o; o = o->op_next) {
7802 const OPCODE type = o->op_type;
7804 if (type == OP_NEXTSTATE || type == OP_LINESEQ
7806 || type == OP_PUSHMARK)
7808 if (type == OP_DBSTATE)
7810 if (type == OP_LEAVESUB)
7814 if (type == OP_CONST && cSVOPo->op_sv)
7816 else if (type == OP_UNDEF && !o->op_private) {
7820 else if (allow_lex && type == OP_PADSV) {
7821 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEt_OUTER)
7823 sv = &PL_sv_undef; /* an arbitrary non-null value */
7841 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
7842 PADNAME * const name, SV ** const const_svp)
7849 if (CvFLAGS(PL_compcv)) {
7850 /* might have had built-in attrs applied */
7851 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
7852 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
7853 && ckWARN(WARN_MISC))
7855 /* protect against fatal warnings leaking compcv */
7856 SAVEFREESV(PL_compcv);
7857 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
7858 SvREFCNT_inc_simple_void_NN(PL_compcv);
7861 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
7862 & ~(CVf_LVALUE * pureperl));
7867 /* redundant check for speed: */
7868 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
7869 const line_t oldline = CopLINE(PL_curcop);
7872 : sv_2mortal(newSVpvn_utf8(
7873 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name)
7875 if (PL_parser && PL_parser->copline != NOLINE)
7876 /* This ensures that warnings are reported at the first
7877 line of a redefinition, not the last. */
7878 CopLINE_set(PL_curcop, PL_parser->copline);
7879 /* protect against fatal warnings leaking compcv */
7880 SAVEFREESV(PL_compcv);
7881 report_redefined_cv(namesv, cv, const_svp);
7882 SvREFCNT_inc_simple_void_NN(PL_compcv);
7883 CopLINE_set(PL_curcop, oldline);
7890 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
7895 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
7898 CV *compcv = PL_compcv;
7901 PADOFFSET pax = o->op_targ;
7902 CV *outcv = CvOUTSIDE(PL_compcv);
7905 bool reusable = FALSE;
7907 #ifdef PERL_DEBUG_READONLY_OPS
7908 OPSLAB *slab = NULL;
7911 PERL_ARGS_ASSERT_NEWMYSUB;
7913 /* Find the pad slot for storing the new sub.
7914 We cannot use PL_comppad, as it is the pad owned by the new sub. We
7915 need to look in CvOUTSIDE and find the pad belonging to the enclos-
7916 ing sub. And then we need to dig deeper if this is a lexical from
7918 my sub foo; sub { sub foo { } }
7921 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
7922 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
7923 pax = PARENT_PAD_INDEX(name);
7924 outcv = CvOUTSIDE(outcv);
7929 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
7930 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
7931 spot = (CV **)svspot;
7933 if (!(PL_parser && PL_parser->error_count))
7934 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name));
7937 assert(proto->op_type == OP_CONST);
7938 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
7939 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
7949 if (PL_parser && PL_parser->error_count) {
7951 SvREFCNT_dec(PL_compcv);
7956 if (CvDEPTH(outcv) && CvCLONE(compcv)) {
7958 svspot = (SV **)(spot = &clonee);
7960 else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
7963 assert (SvTYPE(*spot) == SVt_PVCV);
7965 hek = CvNAME_HEK(*spot);
7969 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
7970 CvNAME_HEK_set(*spot, hek =
7973 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
7977 CvLEXICAL_on(*spot);
7979 cv = PadnamePROTOCV(name);
7980 svspot = (SV **)(spot = &PadnamePROTOCV(name));
7984 /* This makes sub {}; work as expected. */
7985 if (block->op_type == OP_STUB) {
7986 const line_t l = PL_parser->copline;
7988 block = newSTATEOP(0, NULL, 0);
7989 PL_parser->copline = l;
7991 block = CvLVALUE(compcv)
7992 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv))
7993 ? newUNOP(OP_LEAVESUBLV, 0,
7994 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
7995 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
7996 start = LINKLIST(block);
7998 if (ps && !*ps && !attrs && !CvLVALUE(compcv))
7999 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE);
8007 const bool exists = CvROOT(cv) || CvXSUB(cv);
8009 /* if the subroutine doesn't exist and wasn't pre-declared
8010 * with a prototype, assume it will be AUTOLOADed,
8011 * skipping the prototype check
8013 if (exists || SvPOK(cv))
8014 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len,
8016 /* already defined? */
8018 if (S_already_defined(aTHX_ cv,block,NULL,name,&const_sv))
8021 if (attrs) goto attrs;
8022 /* just a "sub foo;" when &foo is already defined */
8027 else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
8033 SvREFCNT_inc_simple_void_NN(const_sv);
8034 SvFLAGS(const_sv) |= SVs_PADTMP;
8036 assert(!CvROOT(cv) && !CvCONST(cv));
8040 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8041 CvFILE_set_from_cop(cv, PL_curcop);
8042 CvSTASH_set(cv, PL_curstash);
8045 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */
8046 CvXSUBANY(cv).any_ptr = const_sv;
8047 CvXSUB(cv) = const_sv_xsub;
8051 CvFLAGS(cv) |= CvMETHOD(compcv);
8053 SvREFCNT_dec(compcv);
8057 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
8058 determine whether this sub definition is in the same scope as its
8059 declaration. If this sub definition is inside an inner named pack-
8060 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
8061 the package sub. So check PadnameOUTER(name) too.
8063 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) {
8064 assert(!CvWEAKOUTSIDE(compcv));
8065 SvREFCNT_dec(CvOUTSIDE(compcv));
8066 CvWEAKOUTSIDE_on(compcv);
8068 /* XXX else do we have a circular reference? */
8069 if (cv) { /* must reuse cv in case stub is referenced elsewhere */
8070 /* transfer PL_compcv to cv */
8073 cv_flags_t preserved_flags =
8074 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
8075 PADLIST *const temp_padl = CvPADLIST(cv);
8076 CV *const temp_cv = CvOUTSIDE(cv);
8077 const cv_flags_t other_flags =
8078 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
8079 OP * const cvstart = CvSTART(cv);
8083 CvFLAGS(compcv) | preserved_flags;
8084 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
8085 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
8086 CvPADLIST_set(cv, CvPADLIST(compcv));
8087 CvOUTSIDE(compcv) = temp_cv;
8088 CvPADLIST_set(compcv, temp_padl);
8089 CvSTART(cv) = CvSTART(compcv);
8090 CvSTART(compcv) = cvstart;
8091 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
8092 CvFLAGS(compcv) |= other_flags;
8094 if (CvFILE(cv) && CvDYNFILE(cv)) {
8095 Safefree(CvFILE(cv));
8098 /* inner references to compcv must be fixed up ... */
8099 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
8100 if (PERLDB_INTER)/* Advice debugger on the new sub. */
8101 ++PL_sub_generation;
8104 /* Might have had built-in attributes applied -- propagate them. */
8105 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
8107 /* ... before we throw it away */
8108 SvREFCNT_dec(compcv);
8109 PL_compcv = compcv = cv;
8117 if (!CvNAME_HEK(cv)) {
8118 if (hek) (void)share_hek_hek(hek);
8122 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
8123 hek = share_hek(PadnamePV(name)+1,
8124 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
8127 CvNAME_HEK_set(cv, hek);
8129 if (const_sv) goto clone;
8131 CvFILE_set_from_cop(cv, PL_curcop);
8132 CvSTASH_set(cv, PL_curstash);
8135 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
8136 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
8142 /* If we assign an optree to a PVCV, then we've defined a subroutine that
8143 the debugger could be able to set a breakpoint in, so signal to
8144 pp_entereval that it should not throw away any saved lines at scope
8147 PL_breakable_sub_gen++;
8149 CvROOT(cv)->op_private |= OPpREFCOUNTED;
8150 OpREFCNT_set(CvROOT(cv), 1);
8151 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8152 itself has a refcount. */
8154 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8155 #ifdef PERL_DEBUG_READONLY_OPS
8156 slab = (OPSLAB *)CvSTART(cv);
8158 CvSTART(cv) = start;
8160 finalize_optree(CvROOT(cv));
8161 S_prune_chain_head(&CvSTART(cv));
8163 /* now that optimizer has done its work, adjust pad values */
8165 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
8169 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8170 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
8174 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8175 SV * const tmpstr = sv_newmortal();
8176 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8177 GV_ADDMULTI, SVt_PVHV);
8179 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8182 (long)CopLINE(PL_curcop));
8183 if (HvNAME_HEK(PL_curstash)) {
8184 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
8185 sv_catpvs(tmpstr, "::");
8187 else sv_setpvs(tmpstr, "__ANON__::");
8188 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
8189 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
8190 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8191 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8192 hv = GvHVn(db_postponed);
8193 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8194 CV * const pcv = GvCV(db_postponed);
8200 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8208 assert(CvDEPTH(outcv));
8210 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
8211 if (reusable) cv_clone_into(clonee, *spot);
8212 else *spot = cv_clone(clonee);
8213 SvREFCNT_dec_NN(clonee);
8216 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
8217 PADOFFSET depth = CvDEPTH(outcv);
8220 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
8222 *svspot = SvREFCNT_inc_simple_NN(cv);
8223 SvREFCNT_dec(oldcv);
8229 PL_parser->copline = NOLINE;
8231 #ifdef PERL_DEBUG_READONLY_OPS
8241 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
8242 OP *block, bool o_is_gv)
8246 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
8250 const bool ec = PL_parser && PL_parser->error_count;
8251 /* If the subroutine has no body, no attributes, and no builtin attributes
8252 then it's just a sub declaration, and we may be able to get away with
8253 storing with a placeholder scalar in the symbol table, rather than a
8254 full CV. If anything is present then it will take a full CV to
8256 const I32 gv_fetch_flags
8257 = ec ? GV_NOADD_NOINIT :
8258 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
8259 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
8261 const char * const name =
8262 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
8264 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
8265 bool evanescent = FALSE;
8267 #ifdef PERL_DEBUG_READONLY_OPS
8268 OPSLAB *slab = NULL;
8276 /* Try to optimise and avoid creating a GV. Instead, the CV’s name
8277 hek and CvSTASH pointer together can imply the GV. If the name
8278 contains a package name, then GvSTASH(CvGV(cv)) may differ from
8279 CvSTASH, so forego the optimisation if we find any.
8280 Also, we may be called from load_module at run time, so
8281 PL_curstash (which sets CvSTASH) may not point to the stash the
8282 sub is stored in. */
8284 ec ? GV_NOADD_NOINIT
8285 : PL_curstash != CopSTASH(PL_curcop)
8286 || memchr(name, ':', namlen) || memchr(name, '\'', namlen)
8288 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL;
8289 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV);
8291 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
8292 SV * const sv = sv_newmortal();
8293 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
8294 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
8295 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
8296 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
8298 } else if (PL_curstash) {
8299 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
8302 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
8307 move_proto_attr(&proto, &attrs, gv);
8310 move_proto_attr(&proto, &attrs, (GV *)cSVOPo->op_sv);
8315 assert(proto->op_type == OP_CONST);
8316 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
8317 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
8331 if (name) SvREFCNT_dec(PL_compcv);
8332 else cv = PL_compcv;
8334 if (name && block) {
8335 const char *s = strrchr(name, ':');
8337 if (strEQ(s, "BEGIN")) {
8338 if (PL_in_eval & EVAL_KEEPERR)
8339 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
8341 SV * const errsv = ERRSV;
8342 /* force display of errors found but not reported */
8343 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
8344 Perl_croak_nocontext("%"SVf, SVfARG(errsv));
8351 if (!block && SvTYPE(gv) != SVt_PVGV) {
8352 /* If we are not defining a new sub and the existing one is not a
8354 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) {
8355 /* We are applying attributes to an existing sub, so we need it
8356 upgraded if it is a constant. */
8357 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)
8358 gv_init_pvn(gv, PL_curstash, name, namlen,
8359 SVf_UTF8 * name_is_utf8);
8361 else { /* Maybe prototype now, and had at maximum
8362 a prototype or const/sub ref before. */
8363 if (SvTYPE(gv) > SVt_NULL) {
8364 cv_ckproto_len_flags((const CV *)gv,
8365 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
8370 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
8371 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(gv));
8374 sv_setiv(MUTABLE_SV(gv), -1);
8377 SvREFCNT_dec(PL_compcv);
8378 cv = PL_compcv = NULL;
8383 cv = (!name || (isGV(gv) && GvCVGEN(gv)))
8387 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
8392 /* This makes sub {}; work as expected. */
8393 if (block->op_type == OP_STUB) {
8394 const line_t l = PL_parser->copline;
8396 block = newSTATEOP(0, NULL, 0);
8397 PL_parser->copline = l;
8399 block = CvLVALUE(PL_compcv)
8400 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)
8401 && (!isGV(gv) || !GvASSUMECV(gv)))
8402 ? newUNOP(OP_LEAVESUBLV, 0,
8403 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
8404 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
8405 start = LINKLIST(block);
8407 if (ps && !*ps && !attrs && !CvLVALUE(PL_compcv))
8409 S_op_const_sv(aTHX_ start, PL_compcv,
8410 cBOOL(CvCLONE(PL_compcv)));
8417 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) {
8418 cv_ckproto_len_flags((const CV *)gv,
8419 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
8420 ps_len, ps_utf8|CV_CKPROTO_CURSTASH);
8422 /* All the other code for sub redefinition warnings expects the
8423 clobbered sub to be a CV. Instead of making all those code
8424 paths more complex, just inline the RV version here. */
8425 const line_t oldline = CopLINE(PL_curcop);
8426 assert(IN_PERL_COMPILETIME);
8427 if (PL_parser && PL_parser->copline != NOLINE)
8428 /* This ensures that warnings are reported at the first
8429 line of a redefinition, not the last. */
8430 CopLINE_set(PL_curcop, PL_parser->copline);
8431 /* protect against fatal warnings leaking compcv */
8432 SAVEFREESV(PL_compcv);
8434 if (ckWARN(WARN_REDEFINE)
8435 || ( ckWARN_d(WARN_REDEFINE)
8436 && ( !const_sv || SvRV(gv) == const_sv
8437 || sv_cmp(SvRV(gv), const_sv) )))
8438 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8439 "Constant subroutine %"SVf" redefined",
8440 SVfARG(cSVOPo->op_sv));
8442 SvREFCNT_inc_simple_void_NN(PL_compcv);
8443 CopLINE_set(PL_curcop, oldline);
8444 SvREFCNT_dec(SvRV(gv));
8449 const bool exists = CvROOT(cv) || CvXSUB(cv);
8451 /* if the subroutine doesn't exist and wasn't pre-declared
8452 * with a prototype, assume it will be AUTOLOADed,
8453 * skipping the prototype check
8455 if (exists || SvPOK(cv))
8456 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
8457 /* already defined (or promised)? */
8458 if (exists || (isGV(gv) && GvASSUMECV(gv))) {
8459 if (S_already_defined(aTHX_ cv, block, o, NULL, &const_sv))
8462 if (attrs) goto attrs;
8463 /* just a "sub foo;" when &foo is already defined */
8464 SAVEFREESV(PL_compcv);
8470 SvREFCNT_inc_simple_void_NN(const_sv);
8471 SvFLAGS(const_sv) |= SVs_PADTMP;
8473 assert(!CvROOT(cv) && !CvCONST(cv));
8475 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */
8476 CvXSUBANY(cv).any_ptr = const_sv;
8477 CvXSUB(cv) = const_sv_xsub;
8481 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
8484 if (isGV(gv) || CvMETHOD(PL_compcv)) {
8485 if (name && isGV(gv))
8487 cv = newCONSTSUB_flags(
8488 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
8491 CvFLAGS(cv) |= CvMETHOD(PL_compcv);
8495 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
8496 prepare_SV_for_RV((SV *)gv);
8500 SvRV_set(gv, const_sv);
8504 SvREFCNT_dec(PL_compcv);
8508 if (cv) { /* must reuse cv if autoloaded */
8509 /* transfer PL_compcv to cv */
8512 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
8513 PADLIST *const temp_av = CvPADLIST(cv);
8514 CV *const temp_cv = CvOUTSIDE(cv);
8515 const cv_flags_t other_flags =
8516 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
8517 OP * const cvstart = CvSTART(cv);
8521 assert(!CvCVGV_RC(cv));
8522 assert(CvGV(cv) == gv);
8527 PERL_HASH(hash, name, namlen);
8537 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs
8539 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
8540 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
8541 CvPADLIST_set(cv,CvPADLIST(PL_compcv));
8542 CvOUTSIDE(PL_compcv) = temp_cv;
8543 CvPADLIST_set(PL_compcv, temp_av);
8544 CvSTART(cv) = CvSTART(PL_compcv);
8545 CvSTART(PL_compcv) = cvstart;
8546 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
8547 CvFLAGS(PL_compcv) |= other_flags;
8549 if (CvFILE(cv) && CvDYNFILE(cv)) {
8550 Safefree(CvFILE(cv));
8552 CvFILE_set_from_cop(cv, PL_curcop);
8553 CvSTASH_set(cv, PL_curstash);
8555 /* inner references to PL_compcv must be fixed up ... */
8556 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
8557 if (PERLDB_INTER)/* Advice debugger on the new sub. */
8558 ++PL_sub_generation;
8561 /* Might have had built-in attributes applied -- propagate them. */
8562 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
8564 /* ... before we throw it away */
8565 SvREFCNT_dec(PL_compcv);
8570 if (name && isGV(gv)) {
8573 if (HvENAME_HEK(GvSTASH(gv)))
8574 /* sub Foo::bar { (shift)+1 } */
8575 gv_method_changed(gv);
8579 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
8580 prepare_SV_for_RV((SV *)gv);
8584 SvRV_set(gv, (SV *)cv);
8588 if (isGV(gv)) CvGV_set(cv, gv);
8592 PERL_HASH(hash, name, namlen);
8593 CvNAME_HEK_set(cv, share_hek(name,
8599 CvFILE_set_from_cop(cv, PL_curcop);
8600 CvSTASH_set(cv, PL_curstash);
8604 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
8605 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
8611 /* If we assign an optree to a PVCV, then we've defined a subroutine that
8612 the debugger could be able to set a breakpoint in, so signal to
8613 pp_entereval that it should not throw away any saved lines at scope
8616 PL_breakable_sub_gen++;
8618 CvROOT(cv)->op_private |= OPpREFCOUNTED;
8619 OpREFCNT_set(CvROOT(cv), 1);
8620 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8621 itself has a refcount. */
8623 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8624 #ifdef PERL_DEBUG_READONLY_OPS
8625 slab = (OPSLAB *)CvSTART(cv);
8627 CvSTART(cv) = start;
8629 finalize_optree(CvROOT(cv));
8630 S_prune_chain_head(&CvSTART(cv));
8632 /* now that optimizer has done its work, adjust pad values */
8634 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
8638 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8639 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv))
8642 if (!name) SAVEFREESV(cv);
8643 apply_attrs(stash, MUTABLE_SV(cv), attrs);
8644 if (!name) SvREFCNT_inc_simple_void_NN(cv);
8647 if (block && has_name) {
8648 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8649 SV * const tmpstr = cv_name(cv,NULL,0);
8650 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8651 GV_ADDMULTI, SVt_PVHV);
8653 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8656 (long)CopLINE(PL_curcop));
8657 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8658 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8659 hv = GvHVn(db_postponed);
8660 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8661 CV * const pcv = GvCV(db_postponed);
8667 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8673 if (PL_parser && PL_parser->error_count)
8674 clear_special_blocks(name, gv, cv);
8677 process_special_blocks(floor, name, gv, cv);
8683 PL_parser->copline = NOLINE;
8686 #ifdef PERL_DEBUG_READONLY_OPS
8690 if (cv && name && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv)))
8691 pad_add_weakref(cv);
8697 S_clear_special_blocks(pTHX_ const char *const fullname,
8698 GV *const gv, CV *const cv) {
8702 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS;
8704 colon = strrchr(fullname,':');
8705 name = colon ? colon + 1 : fullname;
8707 if ((*name == 'B' && strEQ(name, "BEGIN"))
8708 || (*name == 'E' && strEQ(name, "END"))
8709 || (*name == 'U' && strEQ(name, "UNITCHECK"))
8710 || (*name == 'C' && strEQ(name, "CHECK"))
8711 || (*name == 'I' && strEQ(name, "INIT"))) {
8717 SvREFCNT_dec_NN(MUTABLE_SV(cv));
8721 /* Returns true if the sub has been freed. */
8723 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
8727 const char *const colon = strrchr(fullname,':');
8728 const char *const name = colon ? colon + 1 : fullname;
8730 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
8733 if (strEQ(name, "BEGIN")) {
8734 const I32 oldscope = PL_scopestack_ix;
8737 if (floor) LEAVE_SCOPE(floor);
8739 PUSHSTACKi(PERLSI_REQUIRE);
8740 SAVECOPFILE(&PL_compiling);
8741 SAVECOPLINE(&PL_compiling);
8742 SAVEVPTR(PL_curcop);
8744 DEBUG_x( dump_sub(gv) );
8745 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
8746 GvCV_set(gv,0); /* cv has been hijacked */
8747 call_list(oldscope, PL_beginav);
8751 return !PL_savebegin;
8757 if strEQ(name, "END") {
8758 DEBUG_x( dump_sub(gv) );
8759 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
8762 } else if (*name == 'U') {
8763 if (strEQ(name, "UNITCHECK")) {
8764 /* It's never too late to run a unitcheck block */
8765 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
8769 } else if (*name == 'C') {
8770 if (strEQ(name, "CHECK")) {
8772 /* diag_listed_as: Too late to run %s block */
8773 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8774 "Too late to run CHECK block");
8775 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
8779 } else if (*name == 'I') {
8780 if (strEQ(name, "INIT")) {
8782 /* diag_listed_as: Too late to run %s block */
8783 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8784 "Too late to run INIT block");
8785 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
8791 DEBUG_x( dump_sub(gv) );
8793 GvCV_set(gv,0); /* cv has been hijacked */
8799 =for apidoc newCONSTSUB
8801 See L</newCONSTSUB_flags>.
8807 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
8809 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
8813 =for apidoc newCONSTSUB_flags
8815 Creates a constant sub equivalent to Perl S<C<sub FOO () { 123 }>> which is
8816 eligible for inlining at compile-time.
8818 Currently, the only useful value for C<flags> is C<SVf_UTF8>.
8820 The newly created subroutine takes ownership of a reference to the passed in
8823 Passing C<NULL> for SV creates a constant sub equivalent to S<C<sub BAR () {}>>,
8824 which won't be called if used as a destructor, but will suppress the overhead
8825 of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at
8832 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
8836 const char *const file = CopFILE(PL_curcop);
8840 if (IN_PERL_RUNTIME) {
8841 /* at runtime, it's not safe to manipulate PL_curcop: it may be
8842 * an op shared between threads. Use a non-shared COP for our
8844 SAVEVPTR(PL_curcop);
8845 SAVECOMPILEWARNINGS();
8846 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
8847 PL_curcop = &PL_compiling;
8849 SAVECOPLINE(PL_curcop);
8850 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
8853 PL_hints &= ~HINT_BLOCK_SCOPE;
8856 SAVEGENERICSV(PL_curstash);
8857 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
8860 /* Protect sv against leakage caused by fatal warnings. */
8861 if (sv) SAVEFREESV(sv);
8863 /* file becomes the CvFILE. For an XS, it's usually static storage,
8864 and so doesn't get free()d. (It's expected to be from the C pre-
8865 processor __FILE__ directive). But we need a dynamically allocated one,
8866 and we need it to get freed. */
8867 cv = newXS_len_flags(name, len,
8868 sv && SvTYPE(sv) == SVt_PVAV
8871 file ? file : "", "",
8872 &sv, XS_DYNAMIC_FILENAME | flags);
8873 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
8882 =for apidoc U||newXS
8884 Used by C<xsubpp> to hook up XSUBs as Perl subs. C<filename> needs to be
8885 static storage, as it is used directly as CvFILE(), without a copy being made.
8891 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
8893 PERL_ARGS_ASSERT_NEWXS;
8894 return newXS_len_flags(
8895 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
8900 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
8901 const char *const filename, const char *const proto,
8904 PERL_ARGS_ASSERT_NEWXS_FLAGS;
8905 return newXS_len_flags(
8906 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
8911 Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr)
8913 PERL_ARGS_ASSERT_NEWXS_DEFFILE;
8914 return newXS_len_flags(
8915 name, strlen(name), subaddr, NULL, NULL, NULL, 0
8920 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
8921 XSUBADDR_t subaddr, const char *const filename,
8922 const char *const proto, SV **const_svp,
8926 bool interleave = FALSE;
8928 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
8931 GV * const gv = gv_fetchpvn(
8932 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
8933 name ? len : PL_curstash ? sizeof("__ANON__") - 1:
8934 sizeof("__ANON__::__ANON__") - 1,
8935 GV_ADDMULTI | flags, SVt_PVCV);
8937 if ((cv = (name ? GvCV(gv) : NULL))) {
8939 /* just a cached method */
8943 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
8944 /* already defined (or promised) */
8945 /* Redundant check that allows us to avoid creating an SV
8946 most of the time: */
8947 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
8948 report_redefined_cv(newSVpvn_flags(
8949 name,len,(flags&SVf_UTF8)|SVs_TEMP
8960 if (cv) /* must reuse cv if autoloaded */
8963 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8967 if (HvENAME_HEK(GvSTASH(gv)))
8968 gv_method_changed(gv); /* newXS */
8974 /* XSUBs can't be perl lang/perl5db.pl debugged
8975 if (PERLDB_LINE_OR_SAVESRC)
8976 (void)gv_fetchfile(filename); */
8977 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
8978 if (flags & XS_DYNAMIC_FILENAME) {
8980 CvFILE(cv) = savepv(filename);
8982 /* NOTE: not copied, as it is expected to be an external constant string */
8983 CvFILE(cv) = (char *)filename;
8986 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename);
8987 CvFILE(cv) = (char*)PL_xsubfilename;
8990 CvXSUB(cv) = subaddr;
8991 #ifndef PERL_IMPLICIT_CONTEXT
8992 CvHSCXT(cv) = &PL_stack_sp;
8998 process_special_blocks(0, name, gv, cv);
9001 } /* <- not a conditional branch */
9004 sv_setpv(MUTABLE_SV(cv), proto);
9005 if (interleave) LEAVE;
9010 Perl_newSTUB(pTHX_ GV *gv, bool fake)
9012 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
9014 PERL_ARGS_ASSERT_NEWSTUB;
9018 if (!fake && HvENAME_HEK(GvSTASH(gv)))
9019 gv_method_changed(gv);
9021 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
9026 CvFILE_set_from_cop(cv, PL_curcop);
9027 CvSTASH_set(cv, PL_curstash);
9033 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
9039 if (PL_parser && PL_parser->error_count) {
9045 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
9046 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
9049 if ((cv = GvFORM(gv))) {
9050 if (ckWARN(WARN_REDEFINE)) {
9051 const line_t oldline = CopLINE(PL_curcop);
9052 if (PL_parser && PL_parser->copline != NOLINE)
9053 CopLINE_set(PL_curcop, PL_parser->copline);
9055 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
9056 "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv));
9058 /* diag_listed_as: Format %s redefined */
9059 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
9060 "Format STDOUT redefined");
9062 CopLINE_set(PL_curcop, oldline);
9067 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
9069 CvFILE_set_from_cop(cv, PL_curcop);
9072 pad_tidy(padtidy_FORMAT);
9073 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
9074 CvROOT(cv)->op_private |= OPpREFCOUNTED;
9075 OpREFCNT_set(CvROOT(cv), 1);
9076 CvSTART(cv) = LINKLIST(CvROOT(cv));
9077 CvROOT(cv)->op_next = 0;
9078 CALL_PEEP(CvSTART(cv));
9079 finalize_optree(CvROOT(cv));
9080 S_prune_chain_head(&CvSTART(cv));
9086 PL_parser->copline = NOLINE;
9088 PL_compiling.cop_seq = 0;
9092 Perl_newANONLIST(pTHX_ OP *o)
9094 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o);
9098 Perl_newANONHASH(pTHX_ OP *o)
9100 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o);
9104 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
9106 return newANONATTRSUB(floor, proto, NULL, block);
9110 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
9112 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block));
9114 newSVOP(OP_ANONCODE, 0,
9116 if (CvANONCONST(cv))
9117 anoncode = newUNOP(OP_ANONCONST, 0,
9118 op_convert_list(OP_ENTERSUB,
9119 OPf_STACKED|OPf_WANT_SCALAR,
9121 return newUNOP(OP_REFGEN, 0, anoncode);
9125 Perl_oopsAV(pTHX_ OP *o)
9129 PERL_ARGS_ASSERT_OOPSAV;
9131 switch (o->op_type) {
9134 OpTYPE_set(o, OP_PADAV);
9135 return ref(o, OP_RV2AV);
9139 OpTYPE_set(o, OP_RV2AV);
9144 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
9151 Perl_oopsHV(pTHX_ OP *o)
9155 PERL_ARGS_ASSERT_OOPSHV;
9157 switch (o->op_type) {
9160 OpTYPE_set(o, OP_PADHV);
9161 return ref(o, OP_RV2HV);
9165 OpTYPE_set(o, OP_RV2HV);
9170 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
9177 Perl_newAVREF(pTHX_ OP *o)
9181 PERL_ARGS_ASSERT_NEWAVREF;
9183 if (o->op_type == OP_PADANY) {
9184 OpTYPE_set(o, OP_PADAV);
9187 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
9188 Perl_croak(aTHX_ "Can't use an array as a reference");
9190 return newUNOP(OP_RV2AV, 0, scalar(o));
9194 Perl_newGVREF(pTHX_ I32 type, OP *o)
9196 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
9197 return newUNOP(OP_NULL, 0, o);
9198 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
9202 Perl_newHVREF(pTHX_ OP *o)
9206 PERL_ARGS_ASSERT_NEWHVREF;
9208 if (o->op_type == OP_PADANY) {
9209 OpTYPE_set(o, OP_PADHV);
9212 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
9213 Perl_croak(aTHX_ "Can't use a hash as a reference");
9215 return newUNOP(OP_RV2HV, 0, scalar(o));
9219 Perl_newCVREF(pTHX_ I32 flags, OP *o)
9221 if (o->op_type == OP_PADANY) {
9223 OpTYPE_set(o, OP_PADCV);
9225 return newUNOP(OP_RV2CV, flags, scalar(o));
9229 Perl_newSVREF(pTHX_ OP *o)
9233 PERL_ARGS_ASSERT_NEWSVREF;
9235 if (o->op_type == OP_PADANY) {
9236 OpTYPE_set(o, OP_PADSV);
9240 return newUNOP(OP_RV2SV, 0, scalar(o));
9243 /* Check routines. See the comments at the top of this file for details
9244 * on when these are called */
9247 Perl_ck_anoncode(pTHX_ OP *o)
9249 PERL_ARGS_ASSERT_CK_ANONCODE;
9251 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
9252 cSVOPo->op_sv = NULL;
9257 S_io_hints(pTHX_ OP *o)
9259 #if O_BINARY != 0 || O_TEXT != 0
9261 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
9263 SV **svp = hv_fetchs(table, "open_IN", FALSE);
9266 const char *d = SvPV_const(*svp, len);
9267 const I32 mode = mode_from_discipline(d, len);
9268 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
9270 if (mode & O_BINARY)
9271 o->op_private |= OPpOPEN_IN_RAW;
9275 o->op_private |= OPpOPEN_IN_CRLF;
9279 svp = hv_fetchs(table, "open_OUT", FALSE);
9282 const char *d = SvPV_const(*svp, len);
9283 const I32 mode = mode_from_discipline(d, len);
9284 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
9286 if (mode & O_BINARY)
9287 o->op_private |= OPpOPEN_OUT_RAW;
9291 o->op_private |= OPpOPEN_OUT_CRLF;
9296 PERL_UNUSED_CONTEXT;
9302 Perl_ck_backtick(pTHX_ OP *o)
9307 PERL_ARGS_ASSERT_CK_BACKTICK;
9308 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
9309 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first))
9310 && (gv = gv_override("readpipe",8)))
9312 /* detach rest of siblings from o and its first child */
9313 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
9314 newop = S_new_entersubop(aTHX_ gv, sibl);
9316 else if (!(o->op_flags & OPf_KIDS))
9317 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
9322 S_io_hints(aTHX_ o);
9327 Perl_ck_bitop(pTHX_ OP *o)
9329 PERL_ARGS_ASSERT_CK_BITOP;
9331 o->op_private = (U8)(PL_hints & HINT_INTEGER);
9333 if (o->op_type == OP_NBIT_OR || o->op_type == OP_SBIT_OR
9334 || o->op_type == OP_NBIT_XOR || o->op_type == OP_SBIT_XOR
9335 || o->op_type == OP_NBIT_AND || o->op_type == OP_SBIT_AND
9336 || o->op_type == OP_NCOMPLEMENT || o->op_type == OP_SCOMPLEMENT)
9337 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__BITWISE),
9338 "The bitwise feature is experimental");
9339 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
9340 && OP_IS_INFIX_BIT(o->op_type))
9342 const OP * const left = cBINOPo->op_first;
9343 const OP * const right = OpSIBLING(left);
9344 if ((OP_IS_NUMCOMPARE(left->op_type) &&
9345 (left->op_flags & OPf_PARENS) == 0) ||
9346 (OP_IS_NUMCOMPARE(right->op_type) &&
9347 (right->op_flags & OPf_PARENS) == 0))
9348 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
9349 "Possible precedence problem on bitwise %s operator",
9350 o->op_type == OP_BIT_OR
9351 ||o->op_type == OP_NBIT_OR ? "|"
9352 : o->op_type == OP_BIT_AND
9353 ||o->op_type == OP_NBIT_AND ? "&"
9354 : o->op_type == OP_BIT_XOR
9355 ||o->op_type == OP_NBIT_XOR ? "^"
9356 : o->op_type == OP_SBIT_OR ? "|."
9357 : o->op_type == OP_SBIT_AND ? "&." : "^."
9363 PERL_STATIC_INLINE bool
9364 is_dollar_bracket(pTHX_ const OP * const o)
9367 PERL_UNUSED_CONTEXT;
9368 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
9369 && (kid = cUNOPx(o)->op_first)
9370 && kid->op_type == OP_GV
9371 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
9375 Perl_ck_cmp(pTHX_ OP *o)
9377 PERL_ARGS_ASSERT_CK_CMP;
9378 if (ckWARN(WARN_SYNTAX)) {
9379 const OP *kid = cUNOPo->op_first;
9382 ( is_dollar_bracket(aTHX_ kid)
9383 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST
9385 || ( kid->op_type == OP_CONST
9386 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid)
9390 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9391 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
9397 Perl_ck_concat(pTHX_ OP *o)
9399 const OP * const kid = cUNOPo->op_first;
9401 PERL_ARGS_ASSERT_CK_CONCAT;
9402 PERL_UNUSED_CONTEXT;
9404 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
9405 !(kUNOP->op_first->op_flags & OPf_MOD))
9406 o->op_flags |= OPf_STACKED;
9411 Perl_ck_spair(pTHX_ OP *o)
9415 PERL_ARGS_ASSERT_CK_SPAIR;
9417 if (o->op_flags & OPf_KIDS) {
9421 const OPCODE type = o->op_type;
9422 o = modkids(ck_fun(o), type);
9423 kid = cUNOPo->op_first;
9424 kidkid = kUNOP->op_first;
9425 newop = OpSIBLING(kidkid);
9427 const OPCODE type = newop->op_type;
9428 if (OpHAS_SIBLING(newop))
9430 if (o->op_type == OP_REFGEN
9431 && ( type == OP_RV2CV
9432 || ( !(newop->op_flags & OPf_PARENS)
9433 && ( type == OP_RV2AV || type == OP_PADAV
9434 || type == OP_RV2HV || type == OP_PADHV))))
9435 NOOP; /* OK (allow srefgen for \@a and \%h) */
9436 else if (OP_GIMME(newop,0) != G_SCALAR)
9439 /* excise first sibling */
9440 op_sibling_splice(kid, NULL, 1, NULL);
9443 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
9444 * and OP_CHOMP into OP_SCHOMP */
9445 o->op_ppaddr = PL_ppaddr[++o->op_type];
9450 Perl_ck_delete(pTHX_ OP *o)
9452 PERL_ARGS_ASSERT_CK_DELETE;
9456 if (o->op_flags & OPf_KIDS) {
9457 OP * const kid = cUNOPo->op_first;
9458 switch (kid->op_type) {
9460 o->op_flags |= OPf_SPECIAL;
9463 o->op_private |= OPpSLICE;
9466 o->op_flags |= OPf_SPECIAL;
9471 Perl_croak(aTHX_ "delete argument is index/value array slice,"
9472 " use array slice");
9474 Perl_croak(aTHX_ "delete argument is key/value hash slice, use"
9477 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
9478 "element or slice");
9480 if (kid->op_private & OPpLVAL_INTRO)
9481 o->op_private |= OPpLVAL_INTRO;
9488 Perl_ck_eof(pTHX_ OP *o)
9490 PERL_ARGS_ASSERT_CK_EOF;
9492 if (o->op_flags & OPf_KIDS) {
9494 if (cLISTOPo->op_first->op_type == OP_STUB) {
9496 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
9501 kid = cLISTOPo->op_first;
9502 if (kid->op_type == OP_RV2GV)
9503 kid->op_private |= OPpALLOW_FAKE;
9509 Perl_ck_eval(pTHX_ OP *o)
9513 PERL_ARGS_ASSERT_CK_EVAL;
9515 PL_hints |= HINT_BLOCK_SCOPE;
9516 if (o->op_flags & OPf_KIDS) {
9517 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9520 if (o->op_type == OP_ENTERTRY) {
9523 /* cut whole sibling chain free from o */
9524 op_sibling_splice(o, NULL, -1, NULL);
9527 enter = S_alloc_LOGOP(aTHX_ OP_ENTERTRY, NULL, NULL);
9529 /* establish postfix order */
9530 enter->op_next = (OP*)enter;
9532 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
9533 OpTYPE_set(o, OP_LEAVETRY);
9534 enter->op_other = o;
9539 S_set_haseval(aTHX);
9543 const U8 priv = o->op_private;
9545 /* the newUNOP will recursively call ck_eval(), which will handle
9546 * all the stuff at the end of this function, like adding
9549 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
9551 o->op_targ = (PADOFFSET)PL_hints;
9552 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
9553 if ((PL_hints & HINT_LOCALIZE_HH) != 0
9554 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
9555 /* Store a copy of %^H that pp_entereval can pick up. */
9556 OP *hhop = newSVOP(OP_HINTSEVAL, 0,
9557 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv))));
9558 /* append hhop to only child */
9559 op_sibling_splice(o, cUNOPo->op_first, 0, hhop);
9561 o->op_private |= OPpEVAL_HAS_HH;
9563 if (!(o->op_private & OPpEVAL_BYTES)
9564 && FEATURE_UNIEVAL_IS_ENABLED)
9565 o->op_private |= OPpEVAL_UNICODE;
9570 Perl_ck_exec(pTHX_ OP *o)
9572 PERL_ARGS_ASSERT_CK_EXEC;
9574 if (o->op_flags & OPf_STACKED) {
9577 kid = OpSIBLING(cUNOPo->op_first);
9578 if (kid->op_type == OP_RV2GV)
9587 Perl_ck_exists(pTHX_ OP *o)
9589 PERL_ARGS_ASSERT_CK_EXISTS;
9592 if (o->op_flags & OPf_KIDS) {
9593 OP * const kid = cUNOPo->op_first;
9594 if (kid->op_type == OP_ENTERSUB) {
9595 (void) ref(kid, o->op_type);
9596 if (kid->op_type != OP_RV2CV
9597 && !(PL_parser && PL_parser->error_count))
9599 "exists argument is not a subroutine name");
9600 o->op_private |= OPpEXISTS_SUB;
9602 else if (kid->op_type == OP_AELEM)
9603 o->op_flags |= OPf_SPECIAL;
9604 else if (kid->op_type != OP_HELEM)
9605 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
9606 "element or a subroutine");
9613 Perl_ck_rvconst(pTHX_ OP *o)
9616 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9618 PERL_ARGS_ASSERT_CK_RVCONST;
9620 o->op_private |= (PL_hints & HINT_STRICT_REFS);
9622 if (kid->op_type == OP_CONST) {
9625 SV * const kidsv = kid->op_sv;
9627 /* Is it a constant from cv_const_sv()? */
9628 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) {
9631 if (SvTYPE(kidsv) == SVt_PVAV) return o;
9632 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
9633 const char *badthing;
9634 switch (o->op_type) {
9636 badthing = "a SCALAR";
9639 badthing = "an ARRAY";
9642 badthing = "a HASH";
9650 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
9651 SVfARG(kidsv), badthing);
9654 * This is a little tricky. We only want to add the symbol if we
9655 * didn't add it in the lexer. Otherwise we get duplicate strict
9656 * warnings. But if we didn't add it in the lexer, we must at
9657 * least pretend like we wanted to add it even if it existed before,
9658 * or we get possible typo warnings. OPpCONST_ENTERED says
9659 * whether the lexer already added THIS instance of this symbol.
9661 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0;
9662 gv = gv_fetchsv(kidsv,
9663 o->op_type == OP_RV2CV
9664 && o->op_private & OPpMAY_RETURN_CONSTANT
9666 : iscv | !(kid->op_private & OPpCONST_ENTERED),
9669 : o->op_type == OP_RV2SV
9671 : o->op_type == OP_RV2AV
9673 : o->op_type == OP_RV2HV
9680 if (!(o->op_private & OPpMAY_RETURN_CONSTANT)
9681 && SvTYPE(SvRV(gv)) != SVt_PVCV)
9682 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV);
9684 OpTYPE_set(kid, OP_GV);
9685 SvREFCNT_dec(kid->op_sv);
9687 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
9688 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP));
9689 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY);
9690 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
9691 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
9693 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
9695 kid->op_private = 0;
9696 /* FAKE globs in the symbol table cause weird bugs (#77810) */
9704 Perl_ck_ftst(pTHX_ OP *o)
9707 const I32 type = o->op_type;
9709 PERL_ARGS_ASSERT_CK_FTST;
9711 if (o->op_flags & OPf_REF) {
9714 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
9715 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9716 const OPCODE kidtype = kid->op_type;
9718 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
9719 && !kid->op_folded) {
9720 OP * const newop = newGVOP(type, OPf_REF,
9721 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
9726 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
9727 o->op_private |= OPpFT_ACCESS;
9728 if (type != OP_STAT && type != OP_LSTAT
9729 && PL_check[kidtype] == Perl_ck_ftst
9730 && kidtype != OP_STAT && kidtype != OP_LSTAT
9732 o->op_private |= OPpFT_STACKED;
9733 kid->op_private |= OPpFT_STACKING;
9734 if (kidtype == OP_FTTTY && (
9735 !(kid->op_private & OPpFT_STACKED)
9736 || kid->op_private & OPpFT_AFTER_t
9738 o->op_private |= OPpFT_AFTER_t;
9743 if (type == OP_FTTTY)
9744 o = newGVOP(type, OPf_REF, PL_stdingv);
9746 o = newUNOP(type, 0, newDEFSVOP());
9752 Perl_ck_fun(pTHX_ OP *o)
9754 const int type = o->op_type;
9755 I32 oa = PL_opargs[type] >> OASHIFT;
9757 PERL_ARGS_ASSERT_CK_FUN;
9759 if (o->op_flags & OPf_STACKED) {
9760 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
9763 return no_fh_allowed(o);
9766 if (o->op_flags & OPf_KIDS) {
9767 OP *prev_kid = NULL;
9768 OP *kid = cLISTOPo->op_first;
9770 bool seen_optional = FALSE;
9772 if (kid->op_type == OP_PUSHMARK ||
9773 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
9776 kid = OpSIBLING(kid);
9778 if (kid && kid->op_type == OP_COREARGS) {
9779 bool optional = FALSE;
9782 if (oa & OA_OPTIONAL) optional = TRUE;
9785 if (optional) o->op_private |= numargs;
9790 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
9791 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) {
9793 /* append kid to chain */
9794 op_sibling_splice(o, prev_kid, 0, kid);
9796 seen_optional = TRUE;
9803 /* list seen where single (scalar) arg expected? */
9804 if (numargs == 1 && !(oa >> 4)
9805 && kid->op_type == OP_LIST && type != OP_SCALAR)
9807 return too_many_arguments_pv(o,PL_op_desc[type], 0);
9809 if (type != OP_DELETE) scalar(kid);
9820 if ((type == OP_PUSH || type == OP_UNSHIFT)
9821 && !OpHAS_SIBLING(kid))
9822 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
9823 "Useless use of %s with no values",
9826 if (kid->op_type == OP_CONST
9827 && ( !SvROK(cSVOPx_sv(kid))
9828 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
9830 bad_type_pv(numargs, "array", o, kid);
9831 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) {
9832 yyerror_pv(Perl_form(aTHX_ "Experimental %s on scalar is now forbidden",
9833 PL_op_desc[type]), 0);
9836 op_lvalue(kid, type);
9840 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
9841 bad_type_pv(numargs, "hash", o, kid);
9842 op_lvalue(kid, type);
9846 /* replace kid with newop in chain */
9848 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0);
9849 newop->op_next = newop;
9854 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
9855 if (kid->op_type == OP_CONST &&
9856 (kid->op_private & OPpCONST_BARE))
9858 OP * const newop = newGVOP(OP_GV, 0,
9859 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
9860 /* replace kid with newop in chain */
9861 op_sibling_splice(o, prev_kid, 1, newop);
9865 else if (kid->op_type == OP_READLINE) {
9866 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
9867 bad_type_pv(numargs, "HANDLE", o, kid);
9870 I32 flags = OPf_SPECIAL;
9874 /* is this op a FH constructor? */
9875 if (is_handle_constructor(o,numargs)) {
9876 const char *name = NULL;
9879 bool want_dollar = TRUE;
9882 /* Set a flag to tell rv2gv to vivify
9883 * need to "prove" flag does not mean something
9884 * else already - NI-S 1999/05/07
9887 if (kid->op_type == OP_PADSV) {
9889 = PAD_COMPNAME_SV(kid->op_targ);
9890 name = PadnamePV (pn);
9891 len = PadnameLEN(pn);
9892 name_utf8 = PadnameUTF8(pn);
9894 else if (kid->op_type == OP_RV2SV
9895 && kUNOP->op_first->op_type == OP_GV)
9897 GV * const gv = cGVOPx_gv(kUNOP->op_first);
9899 len = GvNAMELEN(gv);
9900 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
9902 else if (kid->op_type == OP_AELEM
9903 || kid->op_type == OP_HELEM)
9906 OP *op = ((BINOP*)kid)->op_first;
9910 const char * const a =
9911 kid->op_type == OP_AELEM ?
9913 if (((op->op_type == OP_RV2AV) ||
9914 (op->op_type == OP_RV2HV)) &&
9915 (firstop = ((UNOP*)op)->op_first) &&
9916 (firstop->op_type == OP_GV)) {
9917 /* packagevar $a[] or $h{} */
9918 GV * const gv = cGVOPx_gv(firstop);
9926 else if (op->op_type == OP_PADAV
9927 || op->op_type == OP_PADHV) {
9928 /* lexicalvar $a[] or $h{} */
9929 const char * const padname =
9930 PAD_COMPNAME_PV(op->op_targ);
9939 name = SvPV_const(tmpstr, len);
9940 name_utf8 = SvUTF8(tmpstr);
9945 name = "__ANONIO__";
9947 want_dollar = FALSE;
9949 op_lvalue(kid, type);
9953 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
9954 namesv = PAD_SVl(targ);
9955 if (want_dollar && *name != '$')
9956 sv_setpvs(namesv, "$");
9958 sv_setpvs(namesv, "");
9959 sv_catpvn(namesv, name, len);
9960 if ( name_utf8 ) SvUTF8_on(namesv);
9964 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid,
9966 kid->op_targ = targ;
9967 kid->op_private |= priv;
9973 if ((type == OP_UNDEF || type == OP_POS)
9974 && numargs == 1 && !(oa >> 4)
9975 && kid->op_type == OP_LIST)
9976 return too_many_arguments_pv(o,PL_op_desc[type], 0);
9977 op_lvalue(scalar(kid), type);
9982 kid = OpSIBLING(kid);
9984 /* FIXME - should the numargs or-ing move after the too many
9985 * arguments check? */
9986 o->op_private |= numargs;
9988 return too_many_arguments_pv(o,OP_DESC(o), 0);
9991 else if (PL_opargs[type] & OA_DEFGV) {
9992 /* Ordering of these two is important to keep f_map.t passing. */
9994 return newUNOP(type, 0, newDEFSVOP());
9998 while (oa & OA_OPTIONAL)
10000 if (oa && oa != OA_LIST)
10001 return too_few_arguments_pv(o,OP_DESC(o), 0);
10007 Perl_ck_glob(pTHX_ OP *o)
10011 PERL_ARGS_ASSERT_CK_GLOB;
10014 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first))
10015 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
10017 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
10021 * \ null - const(wildcard)
10026 * \ mark - glob - rv2cv
10027 * | \ gv(CORE::GLOBAL::glob)
10029 * \ null - const(wildcard)
10031 o->op_flags |= OPf_SPECIAL;
10032 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
10033 o = S_new_entersubop(aTHX_ gv, o);
10034 o = newUNOP(OP_NULL, 0, o);
10035 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
10038 else o->op_flags &= ~OPf_SPECIAL;
10039 #if !defined(PERL_EXTERNAL_GLOB)
10040 if (!PL_globhook) {
10042 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
10043 newSVpvs("File::Glob"), NULL, NULL, NULL);
10046 #endif /* !PERL_EXTERNAL_GLOB */
10047 gv = (GV *)newSV(0);
10048 gv_init(gv, 0, "", 0, 0);
10050 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
10051 SvREFCNT_dec_NN(gv); /* newGVOP increased it */
10057 Perl_ck_grep(pTHX_ OP *o)
10061 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
10063 PERL_ARGS_ASSERT_CK_GREP;
10065 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
10067 if (o->op_flags & OPf_STACKED) {
10068 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first;
10069 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
10070 return no_fh_allowed(o);
10071 o->op_flags &= ~OPf_STACKED;
10073 kid = OpSIBLING(cLISTOPo->op_first);
10074 if (type == OP_MAPWHILE)
10079 if (PL_parser && PL_parser->error_count)
10081 kid = OpSIBLING(cLISTOPo->op_first);
10082 if (kid->op_type != OP_NULL)
10083 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
10084 kid = kUNOP->op_first;
10086 gwop = S_alloc_LOGOP(aTHX_ type, o, LINKLIST(kid));
10087 kid->op_next = (OP*)gwop;
10088 o->op_private = gwop->op_private = 0;
10089 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
10091 kid = OpSIBLING(cLISTOPo->op_first);
10092 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid))
10093 op_lvalue(kid, OP_GREPSTART);
10099 Perl_ck_index(pTHX_ OP *o)
10101 PERL_ARGS_ASSERT_CK_INDEX;
10103 if (o->op_flags & OPf_KIDS) {
10104 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10106 kid = OpSIBLING(kid); /* get past "big" */
10107 if (kid && kid->op_type == OP_CONST) {
10108 const bool save_taint = TAINT_get;
10109 SV *sv = kSVOP->op_sv;
10110 if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) {
10112 sv_copypv(sv, kSVOP->op_sv);
10113 SvREFCNT_dec_NN(kSVOP->op_sv);
10116 if (SvOK(sv)) fbm_compile(sv, 0);
10117 TAINT_set(save_taint);
10118 #ifdef NO_TAINT_SUPPORT
10119 PERL_UNUSED_VAR(save_taint);
10127 Perl_ck_lfun(pTHX_ OP *o)
10129 const OPCODE type = o->op_type;
10131 PERL_ARGS_ASSERT_CK_LFUN;
10133 return modkids(ck_fun(o), type);
10137 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
10139 PERL_ARGS_ASSERT_CK_DEFINED;
10141 if ((o->op_flags & OPf_KIDS)) {
10142 switch (cUNOPo->op_first->op_type) {
10145 Perl_croak(aTHX_ "Can't use 'defined(@array)'"
10146 " (Maybe you should just omit the defined()?)");
10150 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'"
10151 " (Maybe you should just omit the defined()?)");
10162 Perl_ck_readline(pTHX_ OP *o)
10164 PERL_ARGS_ASSERT_CK_READLINE;
10166 if (o->op_flags & OPf_KIDS) {
10167 OP *kid = cLISTOPo->op_first;
10168 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
10172 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
10180 Perl_ck_rfun(pTHX_ OP *o)
10182 const OPCODE type = o->op_type;
10184 PERL_ARGS_ASSERT_CK_RFUN;
10186 return refkids(ck_fun(o), type);
10190 Perl_ck_listiob(pTHX_ OP *o)
10194 PERL_ARGS_ASSERT_CK_LISTIOB;
10196 kid = cLISTOPo->op_first;
10198 o = force_list(o, 1);
10199 kid = cLISTOPo->op_first;
10201 if (kid->op_type == OP_PUSHMARK)
10202 kid = OpSIBLING(kid);
10203 if (kid && o->op_flags & OPf_STACKED)
10204 kid = OpSIBLING(kid);
10205 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */
10206 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
10207 && !kid->op_folded) {
10208 o->op_flags |= OPf_STACKED; /* make it a filehandle */
10210 /* replace old const op with new OP_RV2GV parent */
10211 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first,
10212 OP_RV2GV, OPf_REF);
10213 kid = OpSIBLING(kid);
10218 op_append_elem(o->op_type, o, newDEFSVOP());
10220 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
10221 return listkids(o);
10225 Perl_ck_smartmatch(pTHX_ OP *o)
10228 PERL_ARGS_ASSERT_CK_SMARTMATCH;
10229 if (0 == (o->op_flags & OPf_SPECIAL)) {
10230 OP *first = cBINOPo->op_first;
10231 OP *second = OpSIBLING(first);
10233 /* Implicitly take a reference to an array or hash */
10235 /* remove the original two siblings, then add back the
10236 * (possibly different) first and second sibs.
10238 op_sibling_splice(o, NULL, 1, NULL);
10239 op_sibling_splice(o, NULL, 1, NULL);
10240 first = ref_array_or_hash(first);
10241 second = ref_array_or_hash(second);
10242 op_sibling_splice(o, NULL, 0, second);
10243 op_sibling_splice(o, NULL, 0, first);
10245 /* Implicitly take a reference to a regular expression */
10246 if (first->op_type == OP_MATCH) {
10247 OpTYPE_set(first, OP_QR);
10249 if (second->op_type == OP_MATCH) {
10250 OpTYPE_set(second, OP_QR);
10259 S_maybe_targlex(pTHX_ OP *o)
10261 OP * const kid = cLISTOPo->op_first;
10262 /* has a disposable target? */
10263 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
10264 && !(kid->op_flags & OPf_STACKED)
10265 /* Cannot steal the second time! */
10266 && !(kid->op_private & OPpTARGET_MY)
10269 OP * const kkid = OpSIBLING(kid);
10271 /* Can just relocate the target. */
10272 if (kkid && kkid->op_type == OP_PADSV
10273 && (!(kkid->op_private & OPpLVAL_INTRO)
10274 || kkid->op_private & OPpPAD_STATE))
10276 kid->op_targ = kkid->op_targ;
10278 /* Now we do not need PADSV and SASSIGN.
10279 * Detach kid and free the rest. */
10280 op_sibling_splice(o, NULL, 1, NULL);
10282 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
10290 Perl_ck_sassign(pTHX_ OP *o)
10293 OP * const kid = cLISTOPo->op_first;
10295 PERL_ARGS_ASSERT_CK_SASSIGN;
10297 if (OpHAS_SIBLING(kid)) {
10298 OP *kkid = OpSIBLING(kid);
10299 /* For state variable assignment with attributes, kkid is a list op
10300 whose op_last is a padsv. */
10301 if ((kkid->op_type == OP_PADSV ||
10302 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
10303 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
10306 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
10307 == (OPpLVAL_INTRO|OPpPAD_STATE)) {
10308 const PADOFFSET target = kkid->op_targ;
10309 OP *const other = newOP(OP_PADSV,
10311 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
10312 OP *const first = newOP(OP_NULL, 0);
10314 newCONDOP(0, first, o, other);
10315 /* XXX targlex disabled for now; see ticket #124160
10316 newCONDOP(0, first, S_maybe_targlex(aTHX_ o), other);
10318 OP *const condop = first->op_next;
10320 OpTYPE_set(condop, OP_ONCE);
10321 other->op_targ = target;
10322 nullop->op_flags |= OPf_WANT_SCALAR;
10324 /* Store the initializedness of state vars in a separate
10327 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0);
10328 /* hijacking PADSTALE for uninitialized state variables */
10329 SvPADSTALE_on(PAD_SVl(condop->op_targ));
10334 return S_maybe_targlex(aTHX_ o);
10338 Perl_ck_match(pTHX_ OP *o)
10340 PERL_UNUSED_CONTEXT;
10341 PERL_ARGS_ASSERT_CK_MATCH;
10343 if (o->op_type == OP_MATCH || o->op_type == OP_QR)
10344 o->op_private |= OPpRUNTIME;
10349 Perl_ck_method(pTHX_ OP *o)
10351 SV *sv, *methsv, *rclass;
10352 const char* method;
10355 STRLEN len, nsplit = 0, i;
10357 OP * const kid = cUNOPo->op_first;
10359 PERL_ARGS_ASSERT_CK_METHOD;
10360 if (kid->op_type != OP_CONST) return o;
10364 /* replace ' with :: */
10365 while ((compatptr = strchr(SvPVX(sv), '\''))) {
10367 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1);
10370 method = SvPVX_const(sv);
10372 utf8 = SvUTF8(sv) ? -1 : 1;
10374 for (i = len - 1; i > 0; --i) if (method[i] == ':') {
10379 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0);
10381 if (!nsplit) { /* $proto->method() */
10383 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv);
10386 if (nsplit == 7 && memEQ(method, "SUPER::", nsplit)) { /* $proto->SUPER::method() */
10388 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv);
10391 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */
10392 if (nsplit >= 9 && strnEQ(method+nsplit-9, "::SUPER::", 9)) {
10393 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0);
10394 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv);
10396 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0);
10397 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv);
10399 #ifdef USE_ITHREADS
10400 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ);
10402 cMETHOPx(new_op)->op_rclass_sv = rclass;
10409 Perl_ck_null(pTHX_ OP *o)
10411 PERL_ARGS_ASSERT_CK_NULL;
10412 PERL_UNUSED_CONTEXT;
10417 Perl_ck_open(pTHX_ OP *o)
10419 PERL_ARGS_ASSERT_CK_OPEN;
10421 S_io_hints(aTHX_ o);
10423 /* In case of three-arg dup open remove strictness
10424 * from the last arg if it is a bareword. */
10425 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
10426 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
10430 if ((last->op_type == OP_CONST) && /* The bareword. */
10431 (last->op_private & OPpCONST_BARE) &&
10432 (last->op_private & OPpCONST_STRICT) &&
10433 (oa = OpSIBLING(first)) && /* The fh. */
10434 (oa = OpSIBLING(oa)) && /* The mode. */
10435 (oa->op_type == OP_CONST) &&
10436 SvPOK(((SVOP*)oa)->op_sv) &&
10437 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
10438 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
10439 (last == OpSIBLING(oa))) /* The bareword. */
10440 last->op_private &= ~OPpCONST_STRICT;
10446 Perl_ck_prototype(pTHX_ OP *o)
10448 PERL_ARGS_ASSERT_CK_PROTOTYPE;
10449 if (!(o->op_flags & OPf_KIDS)) {
10451 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP());
10457 Perl_ck_refassign(pTHX_ OP *o)
10459 OP * const right = cLISTOPo->op_first;
10460 OP * const left = OpSIBLING(right);
10461 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first;
10464 PERL_ARGS_ASSERT_CK_REFASSIGN;
10466 assert (left->op_type == OP_SREFGEN);
10469 /* we use OPpPAD_STATE in refassign to mean either of those things,
10470 * and the code assumes the two flags occupy the same bit position
10471 * in the various ops below */
10472 assert(OPpPAD_STATE == OPpOUR_INTRO);
10474 switch (varop->op_type) {
10476 o->op_private |= OPpLVREF_AV;
10479 o->op_private |= OPpLVREF_HV;
10483 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
10484 o->op_targ = varop->op_targ;
10485 varop->op_targ = 0;
10486 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
10490 o->op_private |= OPpLVREF_AV;
10492 NOT_REACHED; /* NOTREACHED */
10494 o->op_private |= OPpLVREF_HV;
10498 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO));
10499 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad;
10501 /* Point varop to its GV kid, detached. */
10502 varop = op_sibling_splice(varop, NULL, -1, NULL);
10506 OP * const kidparent =
10507 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first);
10508 OP * const kid = cUNOPx(kidparent)->op_first;
10509 o->op_private |= OPpLVREF_CV;
10510 if (kid->op_type == OP_GV) {
10512 goto detach_and_stack;
10514 if (kid->op_type != OP_PADCV) goto bad;
10515 o->op_targ = kid->op_targ;
10521 o->op_private |= (varop->op_private & OPpLVAL_INTRO);
10522 o->op_private |= OPpLVREF_ELEM;
10525 /* Detach varop. */
10526 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL);
10530 /* diag_listed_as: Can't modify reference to %s in %s assignment */
10531 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar "
10536 if (!FEATURE_REFALIASING_IS_ENABLED)
10538 "Experimental aliasing via reference not enabled");
10539 Perl_ck_warner_d(aTHX_
10540 packWARN(WARN_EXPERIMENTAL__REFALIASING),
10541 "Aliasing via reference is experimental");
10543 o->op_flags |= OPf_STACKED;
10544 op_sibling_splice(o, right, 1, varop);
10547 o->op_flags &=~ OPf_STACKED;
10548 op_sibling_splice(o, right, 1, NULL);
10555 Perl_ck_repeat(pTHX_ OP *o)
10557 PERL_ARGS_ASSERT_CK_REPEAT;
10559 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
10561 o->op_private |= OPpREPEAT_DOLIST;
10562 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */
10563 kids = force_list(kids, 1); /* promote it to a list */
10564 op_sibling_splice(o, NULL, 0, kids); /* and add back */
10572 Perl_ck_require(pTHX_ OP *o)
10576 PERL_ARGS_ASSERT_CK_REQUIRE;
10578 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
10579 SVOP * const kid = (SVOP*)cUNOPo->op_first;
10584 if (kid->op_type == OP_CONST) {
10585 SV * const sv = kid->op_sv;
10586 U32 const was_readonly = SvREADONLY(sv);
10587 if (kid->op_private & OPpCONST_BARE) {
10591 if (was_readonly) {
10592 SvREADONLY_off(sv);
10594 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
10599 for (; s < end; s++) {
10600 if (*s == ':' && s[1] == ':') {
10602 Move(s+2, s+1, end - s - 1, char);
10606 SvEND_set(sv, end);
10607 sv_catpvs(sv, ".pm");
10608 PERL_HASH(hash, SvPVX(sv), SvCUR(sv));
10609 hek = share_hek(SvPVX(sv),
10610 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1),
10612 sv_sethek(sv, hek);
10614 SvFLAGS(sv) |= was_readonly;
10616 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv)
10619 if (SvREFCNT(sv) > 1) {
10620 kid->op_sv = newSVpvn_share(
10621 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0);
10622 SvREFCNT_dec_NN(sv);
10626 if (was_readonly) SvREADONLY_off(sv);
10627 PERL_HASH(hash, s, len);
10629 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len,
10631 sv_sethek(sv, hek);
10633 SvFLAGS(sv) |= was_readonly;
10639 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
10640 /* handle override, if any */
10641 && (gv = gv_override("require", 7))) {
10643 if (o->op_flags & OPf_KIDS) {
10644 kid = cUNOPo->op_first;
10645 op_sibling_splice(o, NULL, -1, NULL);
10648 kid = newDEFSVOP();
10651 newop = S_new_entersubop(aTHX_ gv, kid);
10659 Perl_ck_return(pTHX_ OP *o)
10663 PERL_ARGS_ASSERT_CK_RETURN;
10665 kid = OpSIBLING(cLISTOPo->op_first);
10666 if (CvLVALUE(PL_compcv)) {
10667 for (; kid; kid = OpSIBLING(kid))
10668 op_lvalue(kid, OP_LEAVESUBLV);
10675 Perl_ck_select(pTHX_ OP *o)
10680 PERL_ARGS_ASSERT_CK_SELECT;
10682 if (o->op_flags & OPf_KIDS) {
10683 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10684 if (kid && OpHAS_SIBLING(kid)) {
10685 OpTYPE_set(o, OP_SSELECT);
10687 return fold_constants(op_integerize(op_std_init(o)));
10691 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10692 if (kid && kid->op_type == OP_RV2GV)
10693 kid->op_private &= ~HINT_STRICT_REFS;
10698 Perl_ck_shift(pTHX_ OP *o)
10700 const I32 type = o->op_type;
10702 PERL_ARGS_ASSERT_CK_SHIFT;
10704 if (!(o->op_flags & OPf_KIDS)) {
10707 if (!CvUNIQUE(PL_compcv)) {
10708 o->op_flags |= OPf_SPECIAL;
10712 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
10714 return newUNOP(type, 0, scalar(argop));
10716 return scalar(ck_fun(o));
10720 Perl_ck_sort(pTHX_ OP *o)
10724 HV * const hinthv =
10725 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;
10728 PERL_ARGS_ASSERT_CK_SORT;
10731 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
10733 const I32 sorthints = (I32)SvIV(*svp);
10734 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
10735 o->op_private |= OPpSORT_QSORT;
10736 if ((sorthints & HINT_SORT_STABLE) != 0)
10737 o->op_private |= OPpSORT_STABLE;
10741 if (o->op_flags & OPf_STACKED)
10743 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10745 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */
10746 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
10748 /* if the first arg is a code block, process it and mark sort as
10750 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
10752 if (kid->op_type == OP_LEAVE)
10753 op_null(kid); /* wipe out leave */
10754 /* Prevent execution from escaping out of the sort block. */
10757 /* provide scalar context for comparison function/block */
10758 kid = scalar(firstkid);
10759 kid->op_next = kid;
10760 o->op_flags |= OPf_SPECIAL;
10762 else if (kid->op_type == OP_CONST
10763 && kid->op_private & OPpCONST_BARE) {
10767 const char * const name = SvPV(kSVOP_sv, len);
10769 assert (len < 256);
10770 Copy(name, tmpbuf+1, len, char);
10771 off = pad_findmy_pvn(tmpbuf, len+1, SvUTF8(kSVOP_sv));
10772 if (off != NOT_IN_PAD) {
10773 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
10775 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off)));
10776 sv_catpvs(fq, "::");
10777 sv_catsv(fq, kSVOP_sv);
10778 SvREFCNT_dec_NN(kSVOP_sv);
10782 OP * const padop = newOP(OP_PADCV, 0);
10783 padop->op_targ = off;
10784 /* replace the const op with the pad op */
10785 op_sibling_splice(firstkid, NULL, 1, padop);
10791 firstkid = OpSIBLING(firstkid);
10794 for (kid = firstkid; kid; kid = OpSIBLING(kid)) {
10795 /* provide list context for arguments */
10798 op_lvalue(kid, OP_GREPSTART);
10804 /* for sort { X } ..., where X is one of
10805 * $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a
10806 * elide the second child of the sort (the one containing X),
10807 * and set these flags as appropriate
10811 * Also, check and warn on lexical $a, $b.
10815 S_simplify_sort(pTHX_ OP *o)
10817 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
10821 const char *gvname;
10824 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
10826 kid = kUNOP->op_first; /* get past null */
10827 if (!(have_scopeop = kid->op_type == OP_SCOPE)
10828 && kid->op_type != OP_LEAVE)
10830 kid = kLISTOP->op_last; /* get past scope */
10831 switch(kid->op_type) {
10835 if (!have_scopeop) goto padkids;
10840 k = kid; /* remember this node*/
10841 if (kBINOP->op_first->op_type != OP_RV2SV
10842 || kBINOP->op_last ->op_type != OP_RV2SV)
10845 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
10846 then used in a comparison. This catches most, but not
10847 all cases. For instance, it catches
10848 sort { my($a); $a <=> $b }
10850 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
10851 (although why you'd do that is anyone's guess).
10855 if (!ckWARN(WARN_SYNTAX)) return;
10856 kid = kBINOP->op_first;
10858 if (kid->op_type == OP_PADSV) {
10859 PADNAME * const name = PAD_COMPNAME(kid->op_targ);
10860 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$'
10861 && ( PadnamePV(name)[1] == 'a'
10862 || PadnamePV(name)[1] == 'b' ))
10863 /* diag_listed_as: "my %s" used in sort comparison */
10864 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10865 "\"%s %s\" used in sort comparison",
10866 PadnameIsSTATE(name)
10871 } while ((kid = OpSIBLING(kid)));
10874 kid = kBINOP->op_first; /* get past cmp */
10875 if (kUNOP->op_first->op_type != OP_GV)
10877 kid = kUNOP->op_first; /* get past rv2sv */
10879 if (GvSTASH(gv) != PL_curstash)
10881 gvname = GvNAME(gv);
10882 if (*gvname == 'a' && gvname[1] == '\0')
10884 else if (*gvname == 'b' && gvname[1] == '\0')
10889 kid = k; /* back to cmp */
10890 /* already checked above that it is rv2sv */
10891 kid = kBINOP->op_last; /* down to 2nd arg */
10892 if (kUNOP->op_first->op_type != OP_GV)
10894 kid = kUNOP->op_first; /* get past rv2sv */
10896 if (GvSTASH(gv) != PL_curstash)
10898 gvname = GvNAME(gv);
10900 ? !(*gvname == 'a' && gvname[1] == '\0')
10901 : !(*gvname == 'b' && gvname[1] == '\0'))
10903 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
10905 o->op_private |= OPpSORT_DESCEND;
10906 if (k->op_type == OP_NCMP)
10907 o->op_private |= OPpSORT_NUMERIC;
10908 if (k->op_type == OP_I_NCMP)
10909 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
10910 kid = OpSIBLING(cLISTOPo->op_first);
10911 /* cut out and delete old block (second sibling) */
10912 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL);
10917 Perl_ck_split(pTHX_ OP *o)
10922 PERL_ARGS_ASSERT_CK_SPLIT;
10924 if (o->op_flags & OPf_STACKED)
10925 return no_fh_allowed(o);
10927 kid = cLISTOPo->op_first;
10928 if (kid->op_type != OP_NULL)
10929 Perl_croak(aTHX_ "panic: ck_split, type=%u", (unsigned) kid->op_type);
10930 /* delete leading NULL node, then add a CONST if no other nodes */
10931 op_sibling_splice(o, NULL, 1,
10932 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" ")));
10934 kid = cLISTOPo->op_first;
10936 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
10937 /* remove kid, and replace with new optree */
10938 op_sibling_splice(o, NULL, 1, NULL);
10939 /* OPf_SPECIAL is used to trigger split " " behavior */
10940 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, NULL, 0, 0);
10941 op_sibling_splice(o, NULL, 0, kid);
10943 OpTYPE_set(kid, OP_PUSHRE);
10944 /* target implies @ary=..., so wipe it */
10947 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
10948 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
10949 "Use of /g modifier is meaningless in split");
10952 if (!OpHAS_SIBLING(kid))
10953 op_append_elem(OP_SPLIT, o, newDEFSVOP());
10955 kid = OpSIBLING(kid);
10959 if (!OpHAS_SIBLING(kid))
10961 op_append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
10962 o->op_private |= OPpSPLIT_IMPLIM;
10964 assert(OpHAS_SIBLING(kid));
10966 kid = OpSIBLING(kid);
10969 if (OpHAS_SIBLING(kid))
10970 return too_many_arguments_pv(o,OP_DESC(o), 0);
10976 Perl_ck_stringify(pTHX_ OP *o)
10978 OP * const kid = OpSIBLING(cUNOPo->op_first);
10979 PERL_ARGS_ASSERT_CK_STRINGIFY;
10980 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA
10981 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST
10982 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST)
10983 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */
10985 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
10993 Perl_ck_join(pTHX_ OP *o)
10995 OP * const kid = OpSIBLING(cLISTOPo->op_first);
10997 PERL_ARGS_ASSERT_CK_JOIN;
10999 if (kid && kid->op_type == OP_MATCH) {
11000 if (ckWARN(WARN_SYNTAX)) {
11001 const REGEXP *re = PM_GETRE(kPMOP);
11003 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
11004 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
11005 : newSVpvs_flags( "STRING", SVs_TEMP );
11006 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11007 "/%"SVf"/ should probably be written as \"%"SVf"\"",
11008 SVfARG(msg), SVfARG(msg));
11012 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */
11013 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO))
11014 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV
11015 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))))
11017 const OP * const bairn = OpSIBLING(kid); /* the list */
11018 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */
11019 && OP_GIMME(bairn,0) == G_SCALAR)
11021 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED,
11022 op_sibling_splice(o, kid, 1, NULL));
11032 =for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags
11034 Examines an op, which is expected to identify a subroutine at runtime,
11035 and attempts to determine at compile time which subroutine it identifies.
11036 This is normally used during Perl compilation to determine whether
11037 a prototype can be applied to a function call. C<cvop> is the op
11038 being considered, normally an C<rv2cv> op. A pointer to the identified
11039 subroutine is returned, if it could be determined statically, and a null
11040 pointer is returned if it was not possible to determine statically.
11042 Currently, the subroutine can be identified statically if the RV that the
11043 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
11044 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
11045 suitable if the constant value must be an RV pointing to a CV. Details of
11046 this process may change in future versions of Perl. If the C<rv2cv> op
11047 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
11048 the subroutine statically: this flag is used to suppress compile-time
11049 magic on a subroutine call, forcing it to use default runtime behaviour.
11051 If C<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
11052 of a GV reference is modified. If a GV was examined and its CV slot was
11053 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
11054 If the op is not optimised away, and the CV slot is later populated with
11055 a subroutine having a prototype, that flag eventually triggers the warning
11056 "called too early to check prototype".
11058 If C<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
11059 of returning a pointer to the subroutine it returns a pointer to the
11060 GV giving the most appropriate name for the subroutine in this context.
11061 Normally this is just the C<CvGV> of the subroutine, but for an anonymous
11062 (C<CvANON>) subroutine that is referenced through a GV it will be the
11063 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
11064 A null pointer is returned as usual if there is no statically-determinable
11070 /* shared by toke.c:yylex */
11072 Perl_find_lexical_cv(pTHX_ PADOFFSET off)
11074 PADNAME *name = PAD_COMPNAME(off);
11075 CV *compcv = PL_compcv;
11076 while (PadnameOUTER(name)) {
11077 assert(PARENT_PAD_INDEX(name));
11078 compcv = CvOUTSIDE(compcv);
11079 name = PadlistNAMESARRAY(CvPADLIST(compcv))
11080 [off = PARENT_PAD_INDEX(name)];
11082 assert(!PadnameIsOUR(name));
11083 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) {
11084 return PadnamePROTOCV(name);
11086 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
11090 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
11095 PERL_ARGS_ASSERT_RV2CV_OP_CV;
11096 if (flags & ~RV2CVOPCV_FLAG_MASK)
11097 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
11098 if (cvop->op_type != OP_RV2CV)
11100 if (cvop->op_private & OPpENTERSUB_AMPER)
11102 if (!(cvop->op_flags & OPf_KIDS))
11104 rvop = cUNOPx(cvop)->op_first;
11105 switch (rvop->op_type) {
11107 gv = cGVOPx_gv(rvop);
11109 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) {
11110 cv = MUTABLE_CV(SvRV(gv));
11114 if (flags & RV2CVOPCV_RETURN_STUB)
11120 if (flags & RV2CVOPCV_MARK_EARLY)
11121 rvop->op_private |= OPpEARLY_CV;
11126 SV *rv = cSVOPx_sv(rvop);
11129 cv = (CV*)SvRV(rv);
11133 cv = find_lexical_cv(rvop->op_targ);
11138 } NOT_REACHED; /* NOTREACHED */
11140 if (SvTYPE((SV*)cv) != SVt_PVCV)
11142 if (flags & (RV2CVOPCV_RETURN_NAME_GV|RV2CVOPCV_MAYBE_NAME_GV)) {
11143 if ((!CvANON(cv) || !gv) && !CvLEXICAL(cv)
11144 && ((flags & RV2CVOPCV_RETURN_NAME_GV) || !CvNAMED(cv)))
11153 =for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop
11155 Performs the default fixup of the arguments part of an C<entersub>
11156 op tree. This consists of applying list context to each of the
11157 argument ops. This is the standard treatment used on a call marked
11158 with C<&>, or a method call, or a call through a subroutine reference,
11159 or any other call where the callee can't be identified at compile time,
11160 or a call where the callee has no prototype.
11166 Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
11170 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
11172 aop = cUNOPx(entersubop)->op_first;
11173 if (!OpHAS_SIBLING(aop))
11174 aop = cUNOPx(aop)->op_first;
11175 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) {
11176 /* skip the extra attributes->import() call implicitly added in
11177 * something like foo(my $x : bar)
11179 if ( aop->op_type == OP_ENTERSUB
11180 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID
11184 op_lvalue(aop, OP_ENTERSUB);
11190 =for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv
11192 Performs the fixup of the arguments part of an C<entersub> op tree
11193 based on a subroutine prototype. This makes various modifications to
11194 the argument ops, from applying context up to inserting C<refgen> ops,
11195 and checking the number and syntactic types of arguments, as directed by
11196 the prototype. This is the standard treatment used on a subroutine call,
11197 not marked with C<&>, where the callee can be identified at compile time
11198 and has a prototype.
11200 C<protosv> supplies the subroutine prototype to be applied to the call.
11201 It may be a normal defined scalar, of which the string value will be used.
11202 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
11203 that has been cast to C<SV*>) which has a prototype. The prototype
11204 supplied, in whichever form, does not need to match the actual callee
11205 referenced by the op tree.
11207 If the argument ops disagree with the prototype, for example by having
11208 an unacceptable number of arguments, a valid op tree is returned anyway.
11209 The error is reflected in the parser state, normally resulting in a single
11210 exception at the top level of parsing which covers all the compilation
11211 errors that occurred. In the error message, the callee is referred to
11212 by the name defined by the C<namegv> parameter.
11218 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
11221 const char *proto, *proto_end;
11222 OP *aop, *prev, *cvop, *parent;
11225 I32 contextclass = 0;
11226 const char *e = NULL;
11227 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
11228 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
11229 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
11230 "flags=%lx", (unsigned long) SvFLAGS(protosv));
11231 if (SvTYPE(protosv) == SVt_PVCV)
11232 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
11233 else proto = SvPV(protosv, proto_len);
11234 proto = S_strip_spaces(aTHX_ proto, &proto_len);
11235 proto_end = proto + proto_len;
11236 parent = entersubop;
11237 aop = cUNOPx(entersubop)->op_first;
11238 if (!OpHAS_SIBLING(aop)) {
11240 aop = cUNOPx(aop)->op_first;
11243 aop = OpSIBLING(aop);
11244 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11245 while (aop != cvop) {
11248 if (proto >= proto_end)
11250 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
11251 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf,
11252 SVfARG(namesv)), SvUTF8(namesv));
11262 /* _ must be at the end */
11263 if (proto[1] && !strchr(";@%", proto[1]))
11279 if ( o3->op_type != OP_UNDEF
11280 && (o3->op_type != OP_SREFGEN
11281 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
11283 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
11285 bad_type_gv(arg, namegv, o3,
11286 arg == 1 ? "block or sub {}" : "sub {}");
11289 /* '*' allows any scalar type, including bareword */
11292 if (o3->op_type == OP_RV2GV)
11293 goto wrapref; /* autoconvert GLOB -> GLOBref */
11294 else if (o3->op_type == OP_CONST)
11295 o3->op_private &= ~OPpCONST_STRICT;
11301 if (o3->op_type == OP_RV2AV ||
11302 o3->op_type == OP_PADAV ||
11303 o3->op_type == OP_RV2HV ||
11304 o3->op_type == OP_PADHV
11310 case '[': case ']':
11317 switch (*proto++) {
11319 if (contextclass++ == 0) {
11320 e = strchr(proto, ']');
11321 if (!e || e == proto)
11329 if (contextclass) {
11330 const char *p = proto;
11331 const char *const end = proto;
11333 while (*--p != '[')
11334 /* \[$] accepts any scalar lvalue */
11336 && Perl_op_lvalue_flags(aTHX_
11338 OP_READ, /* not entersub */
11341 bad_type_gv(arg, namegv, o3,
11342 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p));
11347 if (o3->op_type == OP_RV2GV)
11350 bad_type_gv(arg, namegv, o3, "symbol");
11353 if (o3->op_type == OP_ENTERSUB
11354 && !(o3->op_flags & OPf_STACKED))
11357 bad_type_gv(arg, namegv, o3, "subroutine");
11360 if (o3->op_type == OP_RV2SV ||
11361 o3->op_type == OP_PADSV ||
11362 o3->op_type == OP_HELEM ||
11363 o3->op_type == OP_AELEM)
11365 if (!contextclass) {
11366 /* \$ accepts any scalar lvalue */
11367 if (Perl_op_lvalue_flags(aTHX_
11369 OP_READ, /* not entersub */
11372 bad_type_gv(arg, namegv, o3, "scalar");
11376 if (o3->op_type == OP_RV2AV ||
11377 o3->op_type == OP_PADAV)
11379 o3->op_flags &=~ OPf_PARENS;
11383 bad_type_gv(arg, namegv, o3, "array");
11386 if (o3->op_type == OP_RV2HV ||
11387 o3->op_type == OP_PADHV)
11389 o3->op_flags &=~ OPf_PARENS;
11393 bad_type_gv(arg, namegv, o3, "hash");
11396 aop = S_op_sibling_newUNOP(aTHX_ parent, prev,
11398 if (contextclass && e) {
11403 default: goto oops;
11413 Perl_croak(aTHX_ "Malformed prototype for %"SVf": %"SVf,
11414 SVfARG(cv_name((CV *)namegv, NULL, 0)),
11419 op_lvalue(aop, OP_ENTERSUB);
11421 aop = OpSIBLING(aop);
11423 if (aop == cvop && *proto == '_') {
11424 /* generate an access to $_ */
11425 op_sibling_splice(parent, prev, 0, newDEFSVOP());
11427 if (!optional && proto_end > proto &&
11428 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
11430 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
11431 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf,
11432 SVfARG(namesv)), SvUTF8(namesv));
11438 =for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv
11440 Performs the fixup of the arguments part of an C<entersub> op tree either
11441 based on a subroutine prototype or using default list-context processing.
11442 This is the standard treatment used on a subroutine call, not marked
11443 with C<&>, where the callee can be identified at compile time.
11445 C<protosv> supplies the subroutine prototype to be applied to the call,
11446 or indicates that there is no prototype. It may be a normal scalar,
11447 in which case if it is defined then the string value will be used
11448 as a prototype, and if it is undefined then there is no prototype.
11449 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
11450 that has been cast to C<SV*>), of which the prototype will be used if it
11451 has one. The prototype (or lack thereof) supplied, in whichever form,
11452 does not need to match the actual callee referenced by the op tree.
11454 If the argument ops disagree with the prototype, for example by having
11455 an unacceptable number of arguments, a valid op tree is returned anyway.
11456 The error is reflected in the parser state, normally resulting in a single
11457 exception at the top level of parsing which covers all the compilation
11458 errors that occurred. In the error message, the callee is referred to
11459 by the name defined by the C<namegv> parameter.
11465 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
11466 GV *namegv, SV *protosv)
11468 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
11469 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
11470 return ck_entersub_args_proto(entersubop, namegv, protosv);
11472 return ck_entersub_args_list(entersubop);
11476 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
11478 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv);
11479 OP *aop = cUNOPx(entersubop)->op_first;
11481 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
11485 if (!OpHAS_SIBLING(aop))
11486 aop = cUNOPx(aop)->op_first;
11487 aop = OpSIBLING(aop);
11488 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11490 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0);
11492 op_free(entersubop);
11493 switch(GvNAME(namegv)[2]) {
11494 case 'F': return newSVOP(OP_CONST, 0,
11495 newSVpv(CopFILE(PL_curcop),0));
11496 case 'L': return newSVOP(
11498 Perl_newSVpvf(aTHX_
11499 "%"IVdf, (IV)CopLINE(PL_curcop)
11502 case 'P': return newSVOP(OP_CONST, 0,
11504 ? newSVhek(HvNAME_HEK(PL_curstash))
11509 NOT_REACHED; /* NOTREACHED */
11512 OP *prev, *cvop, *first, *parent;
11515 parent = entersubop;
11516 if (!OpHAS_SIBLING(aop)) {
11518 aop = cUNOPx(aop)->op_first;
11521 first = prev = aop;
11522 aop = OpSIBLING(aop);
11523 /* find last sibling */
11525 OpHAS_SIBLING(cvop);
11526 prev = cvop, cvop = OpSIBLING(cvop))
11528 if (!(cvop->op_private & OPpENTERSUB_NOPAREN)
11529 /* Usually, OPf_SPECIAL on an op with no args means that it had
11530 * parens, but these have their own meaning for that flag: */
11531 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH
11532 && opnum != OP_DELETE && opnum != OP_EXISTS)
11533 flags |= OPf_SPECIAL;
11534 /* excise cvop from end of sibling chain */
11535 op_sibling_splice(parent, prev, 1, NULL);
11537 if (aop == cvop) aop = NULL;
11539 /* detach remaining siblings from the first sibling, then
11540 * dispose of original optree */
11543 op_sibling_splice(parent, first, -1, NULL);
11544 op_free(entersubop);
11546 if (opnum == OP_ENTEREVAL
11547 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9))
11548 flags |= OPpEVAL_BYTES <<8;
11550 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
11552 case OA_BASEOP_OR_UNOP:
11553 case OA_FILESTATOP:
11554 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags);
11557 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0);
11560 return opnum == OP_RUNCV
11561 ? newPVOP(OP_RUNCV,0,NULL)
11564 return op_convert_list(opnum,0,aop);
11567 NOT_REACHED; /* NOTREACHED */
11572 =for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p
11574 Retrieves the function that will be used to fix up a call to C<cv>.
11575 Specifically, the function is applied to an C<entersub> op tree for a
11576 subroutine call, not marked with C<&>, where the callee can be identified
11577 at compile time as C<cv>.
11579 The C-level function pointer is returned in C<*ckfun_p>, and an SV
11580 argument for it is returned in C<*ckobj_p>. The function is intended
11581 to be called in this manner:
11583 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
11585 In this call, C<entersubop> is a pointer to the C<entersub> op,
11586 which may be replaced by the check function, and C<namegv> is a GV
11587 supplying the name that should be used by the check function to refer
11588 to the callee of the C<entersub> op if it needs to emit any diagnostics.
11589 It is permitted to apply the check function in non-standard situations,
11590 such as to a call to a different subroutine or to a method call.
11592 By default, the function is
11593 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
11594 and the SV parameter is C<cv> itself. This implements standard
11595 prototype processing. It can be changed, for a particular subroutine,
11596 by L</cv_set_call_checker>.
11602 S_cv_get_call_checker(CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p,
11606 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
11608 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
11609 *ckobj_p = callmg->mg_obj;
11610 if (flagsp) *flagsp = callmg->mg_flags;
11612 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
11613 *ckobj_p = (SV*)cv;
11614 if (flagsp) *flagsp = 0;
11619 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
11621 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
11622 PERL_UNUSED_CONTEXT;
11623 S_cv_get_call_checker(cv, ckfun_p, ckobj_p, NULL);
11627 =for apidoc Am|void|cv_set_call_checker_flags|CV *cv|Perl_call_checker ckfun|SV *ckobj|U32 flags
11629 Sets the function that will be used to fix up a call to C<cv>.
11630 Specifically, the function is applied to an C<entersub> op tree for a
11631 subroutine call, not marked with C<&>, where the callee can be identified
11632 at compile time as C<cv>.
11634 The C-level function pointer is supplied in C<ckfun>, and an SV argument
11635 for it is supplied in C<ckobj>. The function should be defined like this:
11637 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
11639 It is intended to be called in this manner:
11641 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
11643 In this call, C<entersubop> is a pointer to the C<entersub> op,
11644 which may be replaced by the check function, and C<namegv> supplies
11645 the name that should be used by the check function to refer
11646 to the callee of the C<entersub> op if it needs to emit any diagnostics.
11647 It is permitted to apply the check function in non-standard situations,
11648 such as to a call to a different subroutine or to a method call.
11650 C<namegv> may not actually be a GV. For efficiency, perl may pass a
11651 CV or other SV instead. Whatever is passed can be used as the first
11652 argument to L</cv_name>. You can force perl to pass a GV by including
11653 C<CALL_CHECKER_REQUIRE_GV> in the C<flags>.
11655 The current setting for a particular CV can be retrieved by
11656 L</cv_get_call_checker>.
11658 =for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj
11660 The original form of L</cv_set_call_checker_flags>, which passes it the
11661 C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility.
11667 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
11669 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
11670 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV);
11674 Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun,
11675 SV *ckobj, U32 flags)
11677 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS;
11678 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
11679 if (SvMAGICAL((SV*)cv))
11680 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
11683 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
11684 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
11686 if (callmg->mg_flags & MGf_REFCOUNTED) {
11687 SvREFCNT_dec(callmg->mg_obj);
11688 callmg->mg_flags &= ~MGf_REFCOUNTED;
11690 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
11691 callmg->mg_obj = ckobj;
11692 if (ckobj != (SV*)cv) {
11693 SvREFCNT_inc_simple_void_NN(ckobj);
11694 callmg->mg_flags |= MGf_REFCOUNTED;
11696 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV)
11697 | (U8)(flags & MGf_REQUIRE_GV) | MGf_COPY;
11702 S_entersub_alloc_targ(pTHX_ OP * const o)
11704 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP);
11705 o->op_private |= OPpENTERSUB_HASTARG;
11709 Perl_ck_subr(pTHX_ OP *o)
11714 SV **const_class = NULL;
11716 PERL_ARGS_ASSERT_CK_SUBR;
11718 aop = cUNOPx(o)->op_first;
11719 if (!OpHAS_SIBLING(aop))
11720 aop = cUNOPx(aop)->op_first;
11721 aop = OpSIBLING(aop);
11722 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
11723 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
11724 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL;
11726 o->op_private &= ~1;
11727 o->op_private |= (PL_hints & HINT_STRICT_REFS);
11728 if (PERLDB_SUB && PL_curstash != PL_debstash)
11729 o->op_private |= OPpENTERSUB_DB;
11730 switch (cvop->op_type) {
11732 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
11736 case OP_METHOD_NAMED:
11737 case OP_METHOD_SUPER:
11738 case OP_METHOD_REDIR:
11739 case OP_METHOD_REDIR_SUPER:
11740 if (aop->op_type == OP_CONST) {
11741 aop->op_private &= ~OPpCONST_STRICT;
11742 const_class = &cSVOPx(aop)->op_sv;
11744 else if (aop->op_type == OP_LIST) {
11745 OP * const sib = OpSIBLING(((UNOP*)aop)->op_first);
11746 if (sib && sib->op_type == OP_CONST) {
11747 sib->op_private &= ~OPpCONST_STRICT;
11748 const_class = &cSVOPx(sib)->op_sv;
11751 /* make class name a shared cow string to speedup method calls */
11752 /* constant string might be replaced with object, f.e. bigint */
11753 if (const_class && SvPOK(*const_class)) {
11755 const char* str = SvPV(*const_class, len);
11757 SV* const shared = newSVpvn_share(
11758 str, SvUTF8(*const_class)
11759 ? -(SSize_t)len : (SSize_t)len,
11762 if (SvREADONLY(*const_class))
11763 SvREADONLY_on(shared);
11764 SvREFCNT_dec(*const_class);
11765 *const_class = shared;
11772 S_entersub_alloc_targ(aTHX_ o);
11773 return ck_entersub_args_list(o);
11775 Perl_call_checker ckfun;
11778 S_cv_get_call_checker(cv, &ckfun, &ckobj, &flags);
11779 if (CvISXSUB(cv) || !CvROOT(cv))
11780 S_entersub_alloc_targ(aTHX_ o);
11782 /* The original call checker API guarantees that a GV will be
11783 be provided with the right name. So, if the old API was
11784 used (or the REQUIRE_GV flag was passed), we have to reify
11785 the CV’s GV, unless this is an anonymous sub. This is not
11786 ideal for lexical subs, as its stringification will include
11787 the package. But it is the best we can do. */
11788 if (flags & MGf_REQUIRE_GV) {
11789 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv)))
11792 else namegv = MUTABLE_GV(cv);
11793 /* After a syntax error in a lexical sub, the cv that
11794 rv2cv_op_cv returns may be a nameless stub. */
11795 if (!namegv) return ck_entersub_args_list(o);
11798 return ckfun(aTHX_ o, namegv, ckobj);
11803 Perl_ck_svconst(pTHX_ OP *o)
11805 SV * const sv = cSVOPo->op_sv;
11806 PERL_ARGS_ASSERT_CK_SVCONST;
11807 PERL_UNUSED_CONTEXT;
11808 #ifdef PERL_COPY_ON_WRITE
11809 /* Since the read-only flag may be used to protect a string buffer, we
11810 cannot do copy-on-write with existing read-only scalars that are not
11811 already copy-on-write scalars. To allow $_ = "hello" to do COW with
11812 that constant, mark the constant as COWable here, if it is not
11813 already read-only. */
11814 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
11817 # ifdef PERL_DEBUG_READONLY_COW
11827 Perl_ck_trunc(pTHX_ OP *o)
11829 PERL_ARGS_ASSERT_CK_TRUNC;
11831 if (o->op_flags & OPf_KIDS) {
11832 SVOP *kid = (SVOP*)cUNOPo->op_first;
11834 if (kid->op_type == OP_NULL)
11835 kid = (SVOP*)OpSIBLING(kid);
11836 if (kid && kid->op_type == OP_CONST &&
11837 (kid->op_private & OPpCONST_BARE) &&
11840 o->op_flags |= OPf_SPECIAL;
11841 kid->op_private &= ~OPpCONST_STRICT;
11848 Perl_ck_substr(pTHX_ OP *o)
11850 PERL_ARGS_ASSERT_CK_SUBSTR;
11853 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
11854 OP *kid = cLISTOPo->op_first;
11856 if (kid->op_type == OP_NULL)
11857 kid = OpSIBLING(kid);
11859 kid->op_flags |= OPf_MOD;
11866 Perl_ck_tell(pTHX_ OP *o)
11868 PERL_ARGS_ASSERT_CK_TELL;
11870 if (o->op_flags & OPf_KIDS) {
11871 OP *kid = cLISTOPo->op_first;
11872 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid);
11873 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
11879 Perl_ck_each(pTHX_ OP *o)
11882 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
11883 const unsigned orig_type = o->op_type;
11885 PERL_ARGS_ASSERT_CK_EACH;
11888 switch (kid->op_type) {
11894 OpTYPE_set(o, orig_type == OP_EACH ? OP_AEACH
11895 : orig_type == OP_KEYS ? OP_AKEYS
11899 if (kid->op_private == OPpCONST_BARE
11900 || !SvROK(cSVOPx_sv(kid))
11901 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
11902 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
11904 /* we let ck_fun handle it */
11907 Perl_croak_nocontext(
11908 "Experimental %s on scalar is now forbidden",
11909 PL_op_desc[orig_type]);
11917 Perl_ck_length(pTHX_ OP *o)
11919 PERL_ARGS_ASSERT_CK_LENGTH;
11923 if (ckWARN(WARN_SYNTAX)) {
11924 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
11928 const bool hash = kid->op_type == OP_PADHV
11929 || kid->op_type == OP_RV2HV;
11930 switch (kid->op_type) {
11935 name = S_op_varname(aTHX_ kid);
11941 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11942 "length() used on %"SVf" (did you mean \"scalar(%s%"SVf
11944 SVfARG(name), hash ? "keys " : "", SVfARG(name)
11947 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11948 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11949 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
11951 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11952 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11953 "length() used on @array (did you mean \"scalar(@array)\"?)");
11963 ---------------------------------------------------------
11965 Common vars in list assignment
11967 There now follows some enums and static functions for detecting
11968 common variables in list assignments. Here is a little essay I wrote
11969 for myself when trying to get my head around this. DAPM.
11973 First some random observations:
11975 * If a lexical var is an alias of something else, e.g.
11976 for my $x ($lex, $pkg, $a[0]) {...}
11977 then the act of aliasing will increase the reference count of the SV
11979 * If a package var is an alias of something else, it may still have a
11980 reference count of 1, depending on how the alias was created, e.g.
11981 in *a = *b, $a may have a refcount of 1 since the GP is shared
11982 with a single GvSV pointer to the SV. So If it's an alias of another
11983 package var, then RC may be 1; if it's an alias of another scalar, e.g.
11984 a lexical var or an array element, then it will have RC > 1.
11986 * There are many ways to create a package alias; ultimately, XS code
11987 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so
11988 run-time tracing mechanisms are unlikely to be able to catch all cases.
11990 * When the LHS is all my declarations, the same vars can't appear directly
11991 on the RHS, but they can indirectly via closures, aliasing and lvalue
11992 subs. But those techniques all involve an increase in the lexical
11993 scalar's ref count.
11995 * When the LHS is all lexical vars (but not necessarily my declarations),
11996 it is possible for the same lexicals to appear directly on the RHS, and
11997 without an increased ref count, since the stack isn't refcounted.
11998 This case can be detected at compile time by scanning for common lex
11999 vars with PL_generation.
12001 * lvalue subs defeat common var detection, but they do at least
12002 return vars with a temporary ref count increment. Also, you can't
12003 tell at compile time whether a sub call is lvalue.
12008 A: There are a few circumstances where there definitely can't be any
12011 LHS empty: () = (...);
12012 RHS empty: (....) = ();
12013 RHS contains only constants or other 'can't possibly be shared'
12014 elements (e.g. ops that return PADTMPs): (...) = (1,2, length)
12015 i.e. they only contain ops not marked as dangerous, whose children
12016 are also not dangerous;
12018 LHS contains a single scalar element: e.g. ($x) = (....); because
12019 after $x has been modified, it won't be used again on the RHS;
12020 RHS contains a single element with no aggregate on LHS: e.g.
12021 ($a,$b,$c) = ($x); again, once $a has been modified, its value
12022 won't be used again.
12024 B: If LHS are all 'my' lexical var declarations (or safe ops, which
12027 my ($a, $b, @c) = ...;
12029 Due to closure and goto tricks, these vars may already have content.
12030 For the same reason, an element on the RHS may be a lexical or package
12031 alias of one of the vars on the left, or share common elements, for
12034 my ($x,$y) = f(); # $x and $y on both sides
12035 sub f : lvalue { ($x,$y) = (1,2); $y, $x }
12040 my @a = @$ra; # elements of @a on both sides
12041 sub f { @a = 1..4; \@a }
12044 First, just consider scalar vars on LHS:
12046 RHS is safe only if (A), or in addition,
12047 * contains only lexical *scalar* vars, where neither side's
12048 lexicals have been flagged as aliases
12050 If RHS is not safe, then it's always legal to check LHS vars for
12051 RC==1, since the only RHS aliases will always be associated
12054 Note that in particular, RHS is not safe if:
12056 * it contains package scalar vars; e.g.:
12059 my ($x, $y) = (2, $x_alias);
12060 sub f { $x = 1; *x_alias = \$x; }
12062 * It contains other general elements, such as flattened or
12063 * spliced or single array or hash elements, e.g.
12066 my ($x,$y) = @a; # or $a[0] or @a{@b} etc
12070 use feature 'refaliasing';
12071 \($a[0], $a[1]) = \($y,$x);
12074 It doesn't matter if the array/hash is lexical or package.
12076 * it contains a function call that happens to be an lvalue
12077 sub which returns one or more of the above, e.g.
12088 (so a sub call on the RHS should be treated the same
12089 as having a package var on the RHS).
12091 * any other "dangerous" thing, such an op or built-in that
12092 returns one of the above, e.g. pp_preinc
12095 If RHS is not safe, what we can do however is at compile time flag
12096 that the LHS are all my declarations, and at run time check whether
12097 all the LHS have RC == 1, and if so skip the full scan.
12099 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...;
12101 Here the issue is whether there can be elements of @a on the RHS
12102 which will get prematurely freed when @a is cleared prior to
12103 assignment. This is only a problem if the aliasing mechanism
12104 is one which doesn't increase the refcount - only if RC == 1
12105 will the RHS element be prematurely freed.
12107 Because the array/hash is being INTROed, it or its elements
12108 can't directly appear on the RHS:
12110 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE
12112 but can indirectly, e.g.:
12116 sub f { @a = 1..3; \@a }
12118 So if the RHS isn't safe as defined by (A), we must always
12119 mortalise and bump the ref count of any remaining RHS elements
12120 when assigning to a non-empty LHS aggregate.
12122 Lexical scalars on the RHS aren't safe if they've been involved in
12125 use feature 'refaliasing';
12128 \(my $lex) = \$pkg;
12129 my @a = ($lex,3); # equivalent to ($a[0],3)
12136 Similarly with lexical arrays and hashes on the RHS:
12150 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g.
12151 my $a; ($a, my $b) = (....);
12153 The difference between (B) and (C) is that it is now physically
12154 possible for the LHS vars to appear on the RHS too, where they
12155 are not reference counted; but in this case, the compile-time
12156 PL_generation sweep will detect such common vars.
12158 So the rules for (C) differ from (B) in that if common vars are
12159 detected, the runtime "test RC==1" optimisation can no longer be used,
12160 and a full mark and sweep is required
12162 D: As (C), but in addition the LHS may contain package vars.
12164 Since package vars can be aliased without a corresponding refcount
12165 increase, all bets are off. It's only safe if (A). E.g.
12167 my ($x, $y) = (1,2);
12169 for $x_alias ($x) {
12170 ($x_alias, $y) = (3, $x); # whoops
12173 Ditto for LHS aggregate package vars.
12175 E: Any other dangerous ops on LHS, e.g.
12176 (f(), $a[0], @$r) = (...);
12178 this is similar to (E) in that all bets are off. In addition, it's
12179 impossible to determine at compile time whether the LHS
12180 contains a scalar or an aggregate, e.g.
12182 sub f : lvalue { @a }
12185 * ---------------------------------------------------------
12189 /* A set of bit flags returned by S_aassign_scan(). Each flag indicates
12190 * that at least one of the things flagged was seen.
12194 AAS_MY_SCALAR = 0x001, /* my $scalar */
12195 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */
12196 AAS_LEX_SCALAR = 0x004, /* $lexical */
12197 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */
12198 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */
12199 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */
12200 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */
12201 AAS_DANGEROUS = 0x080, /* an op (other than the above)
12202 that's flagged OA_DANGEROUS */
12203 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's
12204 not in any of the categories above */
12205 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */
12210 /* helper function for S_aassign_scan().
12211 * check a PAD-related op for commonality and/or set its generation number.
12212 * Returns a boolean indicating whether its shared */
12215 S_aassign_padcheck(pTHX_ OP* o, bool rhs)
12217 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX)
12218 /* lexical used in aliasing */
12222 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation);
12224 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation);
12231 Helper function for OPpASSIGN_COMMON* detection in rpeep().
12232 It scans the left or right hand subtree of the aassign op, and returns a
12233 set of flags indicating what sorts of things it found there.
12234 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we
12235 set PL_generation on lexical vars; if the latter, we see if
12236 PL_generation matches.
12237 'top' indicates whether we're recursing or at the top level.
12238 'scalars_p' is a pointer to a counter of the number of scalar SVs seen.
12239 This fn will increment it by the number seen. It's not intended to
12240 be an accurate count (especially as many ops can push a variable
12241 number of SVs onto the stack); rather it's used as to test whether there
12242 can be at most 1 SV pushed; so it's only meanings are "0, 1, many".
12246 S_aassign_scan(pTHX_ OP* o, bool rhs, bool top, int *scalars_p)
12249 bool kid_top = FALSE;
12251 /* first, look for a solitary @_ on the RHS */
12254 && (o->op_flags & OPf_KIDS)
12255 && OP_TYPE_IS_OR_WAS(o, OP_LIST)
12257 OP *kid = cUNOPo->op_first;
12258 if ( ( kid->op_type == OP_PUSHMARK
12259 || kid->op_type == OP_PADRANGE) /* ex-pushmark */
12260 && ((kid = OpSIBLING(kid)))
12261 && !OpHAS_SIBLING(kid)
12262 && kid->op_type == OP_RV2AV
12263 && !(kid->op_flags & OPf_REF)
12264 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
12265 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST)
12266 && ((kid = cUNOPx(kid)->op_first))
12267 && kid->op_type == OP_GV
12268 && cGVOPx_gv(kid) == PL_defgv
12270 flags |= AAS_DEFAV;
12273 switch (o->op_type) {
12276 return AAS_PKG_SCALAR;
12281 if (top && (o->op_flags & OPf_REF))
12282 return (o->op_private & OPpLVAL_INTRO)
12283 ? AAS_MY_AGG : AAS_LEX_AGG;
12284 return AAS_DANGEROUS;
12288 int comm = S_aassign_padcheck(aTHX_ o, rhs)
12289 ? AAS_LEX_SCALAR_COMM : 0;
12291 return (o->op_private & OPpLVAL_INTRO)
12292 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm);
12298 if (cUNOPx(o)->op_first->op_type != OP_GV)
12299 return AAS_DANGEROUS; /* @{expr}, %{expr} */
12301 if (top && (o->op_flags & OPf_REF))
12302 return AAS_PKG_AGG;
12303 return AAS_DANGEROUS;
12307 if (cUNOPx(o)->op_first->op_type != OP_GV) {
12309 return AAS_DANGEROUS; /* ${expr} */
12311 return AAS_PKG_SCALAR; /* $pkg */
12314 if (cLISTOPo->op_first->op_type == OP_PUSHRE) {
12315 /* "@foo = split... " optimises away the aassign and stores its
12316 * destination array in the OP_PUSHRE that precedes it.
12317 * A flattened array is always dangerous.
12320 return AAS_DANGEROUS;
12325 /* undef counts as a scalar on the RHS:
12326 * (undef, $x) = ...; # only 1 scalar on LHS: always safe
12327 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe
12331 flags = AAS_SAFE_SCALAR;
12336 /* these are all no-ops; they don't push a potentially common SV
12337 * onto the stack, so they are neither AAS_DANGEROUS nor
12338 * AAS_SAFE_SCALAR */
12341 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */
12346 /* these do nothing but may have children; but their children
12347 * should also be treated as top-level */
12352 if (PL_opargs[o->op_type] & OA_DANGEROUS) {
12354 flags = AAS_DANGEROUS;
12358 if ( (PL_opargs[o->op_type] & OA_TARGLEX)
12359 && (o->op_private & OPpTARGET_MY))
12362 return S_aassign_padcheck(aTHX_ o, rhs)
12363 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR;
12366 /* if its an unrecognised, non-dangerous op, assume that it
12367 * it the cause of at least one safe scalar */
12369 flags = AAS_SAFE_SCALAR;
12373 if (o->op_flags & OPf_KIDS) {
12375 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
12376 flags |= S_aassign_scan(aTHX_ kid, rhs, kid_top, scalars_p);
12382 /* Check for in place reverse and sort assignments like "@a = reverse @a"
12383 and modify the optree to make them work inplace */
12386 S_inplace_aassign(pTHX_ OP *o) {
12388 OP *modop, *modop_pushmark;
12390 OP *oleft, *oleft_pushmark;
12392 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
12394 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
12396 assert(cUNOPo->op_first->op_type == OP_NULL);
12397 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
12398 assert(modop_pushmark->op_type == OP_PUSHMARK);
12399 modop = OpSIBLING(modop_pushmark);
12401 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
12404 /* no other operation except sort/reverse */
12405 if (OpHAS_SIBLING(modop))
12408 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
12409 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return;
12411 if (modop->op_flags & OPf_STACKED) {
12412 /* skip sort subroutine/block */
12413 assert(oright->op_type == OP_NULL);
12414 oright = OpSIBLING(oright);
12417 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL);
12418 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first;
12419 assert(oleft_pushmark->op_type == OP_PUSHMARK);
12420 oleft = OpSIBLING(oleft_pushmark);
12422 /* Check the lhs is an array */
12424 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
12425 || OpHAS_SIBLING(oleft)
12426 || (oleft->op_private & OPpLVAL_INTRO)
12430 /* Only one thing on the rhs */
12431 if (OpHAS_SIBLING(oright))
12434 /* check the array is the same on both sides */
12435 if (oleft->op_type == OP_RV2AV) {
12436 if (oright->op_type != OP_RV2AV
12437 || !cUNOPx(oright)->op_first
12438 || cUNOPx(oright)->op_first->op_type != OP_GV
12439 || cUNOPx(oleft )->op_first->op_type != OP_GV
12440 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
12441 cGVOPx_gv(cUNOPx(oright)->op_first)
12445 else if (oright->op_type != OP_PADAV
12446 || oright->op_targ != oleft->op_targ
12450 /* This actually is an inplace assignment */
12452 modop->op_private |= OPpSORT_INPLACE;
12454 /* transfer MODishness etc from LHS arg to RHS arg */
12455 oright->op_flags = oleft->op_flags;
12457 /* remove the aassign op and the lhs */
12459 op_null(oleft_pushmark);
12460 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
12461 op_null(cUNOPx(oleft)->op_first);
12467 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start'
12468 * that potentially represent a series of one or more aggregate derefs
12469 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert
12470 * the whole chain to a single OP_MULTIDEREF op (maybe with a few
12471 * additional ops left in too).
12473 * The caller will have already verified that the first few ops in the
12474 * chain following 'start' indicate a multideref candidate, and will have
12475 * set 'orig_o' to the point further on in the chain where the first index
12476 * expression (if any) begins. 'orig_action' specifies what type of
12477 * beginning has already been determined by the ops between start..orig_o
12478 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc).
12480 * 'hints' contains any hints flags that need adding (currently just
12481 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller.
12485 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints)
12489 UNOP_AUX_item *arg_buf = NULL;
12490 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */
12491 int index_skip = -1; /* don't output index arg on this action */
12493 /* similar to regex compiling, do two passes; the first pass
12494 * determines whether the op chain is convertible and calculates the
12495 * buffer size; the second pass populates the buffer and makes any
12496 * changes necessary to ops (such as moving consts to the pad on
12497 * threaded builds).
12499 * NB: for things like Coverity, note that both passes take the same
12500 * path through the logic tree (except for 'if (pass)' bits), since
12501 * both passes are following the same op_next chain; and in
12502 * particular, if it would return early on the second pass, it would
12503 * already have returned early on the first pass.
12505 for (pass = 0; pass < 2; pass++) {
12507 UV action = orig_action;
12508 OP *first_elem_op = NULL; /* first seen aelem/helem */
12509 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */
12510 int action_count = 0; /* number of actions seen so far */
12511 int action_ix = 0; /* action_count % (actions per IV) */
12512 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */
12513 bool is_last = FALSE; /* no more derefs to follow */
12514 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */
12515 UNOP_AUX_item *arg = arg_buf;
12516 UNOP_AUX_item *action_ptr = arg_buf;
12519 action_ptr->uv = 0;
12523 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
12524 case MDEREF_HV_gvhv_helem:
12525 next_is_hash = TRUE;
12527 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
12528 case MDEREF_AV_gvav_aelem:
12530 #ifdef USE_ITHREADS
12531 arg->pad_offset = cPADOPx(start)->op_padix;
12532 /* stop it being swiped when nulled */
12533 cPADOPx(start)->op_padix = 0;
12535 arg->sv = cSVOPx(start)->op_sv;
12536 cSVOPx(start)->op_sv = NULL;
12542 case MDEREF_HV_padhv_helem:
12543 case MDEREF_HV_padsv_vivify_rv2hv_helem:
12544 next_is_hash = TRUE;
12546 case MDEREF_AV_padav_aelem:
12547 case MDEREF_AV_padsv_vivify_rv2av_aelem:
12549 arg->pad_offset = start->op_targ;
12550 /* we skip setting op_targ = 0 for now, since the intact
12551 * OP_PADXV is needed by S_check_hash_fields_and_hekify */
12552 reset_start_targ = TRUE;
12557 case MDEREF_HV_pop_rv2hv_helem:
12558 next_is_hash = TRUE;
12560 case MDEREF_AV_pop_rv2av_aelem:
12564 NOT_REACHED; /* NOTREACHED */
12569 /* look for another (rv2av/hv; get index;
12570 * aelem/helem/exists/delele) sequence */
12575 UV index_type = MDEREF_INDEX_none;
12577 if (action_count) {
12578 /* if this is not the first lookup, consume the rv2av/hv */
12580 /* for N levels of aggregate lookup, we normally expect
12581 * that the first N-1 [ah]elem ops will be flagged as
12582 * /DEREF (so they autovivifiy if necessary), and the last
12583 * lookup op not to be.
12584 * For other things (like @{$h{k1}{k2}}) extra scope or
12585 * leave ops can appear, so abandon the effort in that
12587 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
12590 /* rv2av or rv2hv sKR/1 */
12592 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
12593 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
12594 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
12597 /* at this point, we wouldn't expect any of these
12598 * possible private flags:
12599 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO
12600 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only)
12602 ASSUME(!(o->op_private &
12603 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING)));
12605 hints = (o->op_private & OPpHINT_STRICT_REFS);
12607 /* make sure the type of the previous /DEREF matches the
12608 * type of the next lookup */
12609 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV));
12612 action = next_is_hash
12613 ? MDEREF_HV_vivify_rv2hv_helem
12614 : MDEREF_AV_vivify_rv2av_aelem;
12618 /* if this is the second pass, and we're at the depth where
12619 * previously we encountered a non-simple index expression,
12620 * stop processing the index at this point */
12621 if (action_count != index_skip) {
12623 /* look for one or more simple ops that return an array
12624 * index or hash key */
12626 switch (o->op_type) {
12628 /* it may be a lexical var index */
12629 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS
12630 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
12631 ASSUME(!(o->op_private &
12632 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
12634 if ( OP_GIMME(o,0) == G_SCALAR
12635 && !(o->op_flags & (OPf_REF|OPf_MOD))
12636 && o->op_private == 0)
12639 arg->pad_offset = o->op_targ;
12641 index_type = MDEREF_INDEX_padsv;
12647 if (next_is_hash) {
12648 /* it's a constant hash index */
12649 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK)))
12650 /* "use constant foo => FOO; $h{+foo}" for
12651 * some weird FOO, can leave you with constants
12652 * that aren't simple strings. It's not worth
12653 * the extra hassle for those edge cases */
12658 OP * helem_op = o->op_next;
12660 ASSUME( helem_op->op_type == OP_HELEM
12661 || helem_op->op_type == OP_NULL);
12662 if (helem_op->op_type == OP_HELEM) {
12663 rop = (UNOP*)(((BINOP*)helem_op)->op_first);
12664 if ( helem_op->op_private & OPpLVAL_INTRO
12665 || rop->op_type != OP_RV2HV
12669 S_check_hash_fields_and_hekify(aTHX_ rop, cSVOPo);
12671 #ifdef USE_ITHREADS
12672 /* Relocate sv to the pad for thread safety */
12673 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
12674 arg->pad_offset = o->op_targ;
12677 arg->sv = cSVOPx_sv(o);
12682 /* it's a constant array index */
12684 SV *ix_sv = cSVOPo->op_sv;
12689 if ( action_count == 0
12692 && ( action == MDEREF_AV_padav_aelem
12693 || action == MDEREF_AV_gvav_aelem)
12695 maybe_aelemfast = TRUE;
12699 SvREFCNT_dec_NN(cSVOPo->op_sv);
12703 /* we've taken ownership of the SV */
12704 cSVOPo->op_sv = NULL;
12706 index_type = MDEREF_INDEX_const;
12711 /* it may be a package var index */
12713 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_SPECIAL)));
12714 ASSUME(!(o->op_private & ~(OPpEARLY_CV)));
12715 if ( (o->op_flags &~ OPf_SPECIAL) != OPf_WANT_SCALAR
12716 || o->op_private != 0
12721 if (kid->op_type != OP_RV2SV)
12724 ASSUME(!(kid->op_flags &
12725 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF
12726 |OPf_SPECIAL|OPf_PARENS)));
12727 ASSUME(!(kid->op_private &
12729 |OPpHINT_STRICT_REFS|OPpOUR_INTRO
12730 |OPpDEREF|OPpLVAL_INTRO)));
12731 if( (kid->op_flags &~ OPf_PARENS)
12732 != (OPf_WANT_SCALAR|OPf_KIDS)
12733 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS))
12738 #ifdef USE_ITHREADS
12739 arg->pad_offset = cPADOPx(o)->op_padix;
12740 /* stop it being swiped when nulled */
12741 cPADOPx(o)->op_padix = 0;
12743 arg->sv = cSVOPx(o)->op_sv;
12744 cSVOPo->op_sv = NULL;
12748 index_type = MDEREF_INDEX_gvsv;
12753 } /* action_count != index_skip */
12755 action |= index_type;
12758 /* at this point we have either:
12759 * * detected what looks like a simple index expression,
12760 * and expect the next op to be an [ah]elem, or
12761 * an nulled [ah]elem followed by a delete or exists;
12762 * * found a more complex expression, so something other
12763 * than the above follows.
12766 /* possibly an optimised away [ah]elem (where op_next is
12767 * exists or delete) */
12768 if (o->op_type == OP_NULL)
12771 /* at this point we're looking for an OP_AELEM, OP_HELEM,
12772 * OP_EXISTS or OP_DELETE */
12774 /* if something like arybase (a.k.a $[ ) is in scope,
12775 * abandon optimisation attempt */
12776 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
12777 && PL_check[o->op_type] != Perl_ck_null)
12780 if ( o->op_type != OP_AELEM
12781 || (o->op_private &
12782 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB))
12784 maybe_aelemfast = FALSE;
12786 /* look for aelem/helem/exists/delete. If it's not the last elem
12787 * lookup, it *must* have OPpDEREF_AV/HV, but not many other
12788 * flags; if it's the last, then it mustn't have
12789 * OPpDEREF_AV/HV, but may have lots of other flags, like
12790 * OPpLVAL_INTRO etc
12793 if ( index_type == MDEREF_INDEX_none
12794 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM
12795 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE)
12799 /* we have aelem/helem/exists/delete with valid simple index */
12801 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM)
12802 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV
12803 || (o->op_private & OPpDEREF) == OPpDEREF_HV);
12806 ASSUME(!(o->op_flags &
12807 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS)));
12808 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF)));
12810 ok = (o->op_flags &~ OPf_PARENS)
12811 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD)
12812 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK));
12814 else if (o->op_type == OP_EXISTS) {
12815 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
12816 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
12817 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB)));
12818 ok = !(o->op_private & ~OPpARG1_MASK);
12820 else if (o->op_type == OP_DELETE) {
12821 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
12822 |OPf_REF|OPf_MOD|OPf_SPECIAL)));
12823 ASSUME(!(o->op_private &
12824 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO)));
12825 /* don't handle slices or 'local delete'; the latter
12826 * is fairly rare, and has a complex runtime */
12827 ok = !(o->op_private & ~OPpARG1_MASK);
12828 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM))
12829 /* skip handling run-tome error */
12830 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL));
12833 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM);
12834 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD
12835 |OPf_PARENS|OPf_REF|OPf_SPECIAL)));
12836 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB
12837 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO)));
12838 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV;
12843 if (!first_elem_op)
12847 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV);
12852 action |= MDEREF_FLAG_last;
12856 /* at this point we have something that started
12857 * promisingly enough (with rv2av or whatever), but failed
12858 * to find a simple index followed by an
12859 * aelem/helem/exists/delete. If this is the first action,
12860 * give up; but if we've already seen at least one
12861 * aelem/helem, then keep them and add a new action with
12862 * MDEREF_INDEX_none, which causes it to do the vivify
12863 * from the end of the previous lookup, and do the deref,
12864 * but stop at that point. So $a[0][expr] will do one
12865 * av_fetch, vivify and deref, then continue executing at
12870 index_skip = action_count;
12871 action |= MDEREF_FLAG_last;
12875 action_ptr->uv |= (action << (action_ix * MDEREF_SHIFT));
12878 /* if there's no space for the next action, create a new slot
12879 * for it *before* we start adding args for that action */
12880 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) {
12887 } /* while !is_last */
12895 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf);
12896 if (index_skip == -1) {
12897 mderef->op_flags = o->op_flags
12898 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0));
12899 if (o->op_type == OP_EXISTS)
12900 mderef->op_private = OPpMULTIDEREF_EXISTS;
12901 else if (o->op_type == OP_DELETE)
12902 mderef->op_private = OPpMULTIDEREF_DELETE;
12904 mderef->op_private = o->op_private
12905 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO);
12907 /* accumulate strictness from every level (although I don't think
12908 * they can actually vary) */
12909 mderef->op_private |= hints;
12911 /* integrate the new multideref op into the optree and the
12914 * In general an op like aelem or helem has two child
12915 * sub-trees: the aggregate expression (a_expr) and the
12916 * index expression (i_expr):
12922 * The a_expr returns an AV or HV, while the i-expr returns an
12923 * index. In general a multideref replaces most or all of a
12924 * multi-level tree, e.g.
12940 * With multideref, all the i_exprs will be simple vars or
12941 * constants, except that i_expr1 may be arbitrary in the case
12942 * of MDEREF_INDEX_none.
12944 * The bottom-most a_expr will be either:
12945 * 1) a simple var (so padXv or gv+rv2Xv);
12946 * 2) a simple scalar var dereferenced (e.g. $r->[0]):
12947 * so a simple var with an extra rv2Xv;
12948 * 3) or an arbitrary expression.
12950 * 'start', the first op in the execution chain, will point to
12951 * 1),2): the padXv or gv op;
12952 * 3): the rv2Xv which forms the last op in the a_expr
12953 * execution chain, and the top-most op in the a_expr
12956 * For all cases, the 'start' node is no longer required,
12957 * but we can't free it since one or more external nodes
12958 * may point to it. E.g. consider
12959 * $h{foo} = $a ? $b : $c
12960 * Here, both the op_next and op_other branches of the
12961 * cond_expr point to the gv[*h] of the hash expression, so
12962 * we can't free the 'start' op.
12964 * For expr->[...], we need to save the subtree containing the
12965 * expression; for the other cases, we just need to save the
12967 * So in all cases, we null the start op and keep it around by
12968 * making it the child of the multideref op; for the expr->
12969 * case, the expr will be a subtree of the start node.
12971 * So in the simple 1,2 case the optree above changes to
12977 * ex-gv (or ex-padxv)
12979 * with the op_next chain being
12981 * -> ex-gv -> multideref -> op-following-ex-exists ->
12983 * In the 3 case, we have
12996 * -> rest-of-a_expr subtree ->
12997 * ex-rv2xv -> multideref -> op-following-ex-exists ->
13000 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none,
13001 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the
13002 * multideref attached as the child, e.g.
13008 * ex-rv2av - i_expr1
13016 /* if we free this op, don't free the pad entry */
13017 if (reset_start_targ)
13018 start->op_targ = 0;
13021 /* Cut the bit we need to save out of the tree and attach to
13022 * the multideref op, then free the rest of the tree */
13024 /* find parent of node to be detached (for use by splice) */
13026 if ( orig_action == MDEREF_AV_pop_rv2av_aelem
13027 || orig_action == MDEREF_HV_pop_rv2hv_helem)
13029 /* there is an arbitrary expression preceding us, e.g.
13030 * expr->[..]? so we need to save the 'expr' subtree */
13031 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE)
13032 p = cUNOPx(p)->op_first;
13033 ASSUME( start->op_type == OP_RV2AV
13034 || start->op_type == OP_RV2HV);
13037 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem
13038 * above for exists/delete. */
13039 while ( (p->op_flags & OPf_KIDS)
13040 && cUNOPx(p)->op_first != start
13042 p = cUNOPx(p)->op_first;
13044 ASSUME(cUNOPx(p)->op_first == start);
13046 /* detach from main tree, and re-attach under the multideref */
13047 op_sibling_splice(mderef, NULL, 0,
13048 op_sibling_splice(p, NULL, 1, NULL));
13051 start->op_next = mderef;
13053 mderef->op_next = index_skip == -1 ? o->op_next : o;
13055 /* excise and free the original tree, and replace with
13056 * the multideref op */
13057 p = op_sibling_splice(top_op, NULL, -1, mderef);
13066 Size_t size = arg - arg_buf;
13068 if (maybe_aelemfast && action_count == 1)
13071 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc(
13072 sizeof(UNOP_AUX_item) * (size + 1));
13073 /* for dumping etc: store the length in a hidden first slot;
13074 * we set the op_aux pointer to the second slot */
13075 arg_buf->uv = size;
13078 } /* for (pass = ...) */
13083 /* mechanism for deferring recursion in rpeep() */
13085 #define MAX_DEFERRED 4
13089 if (defer_ix == (MAX_DEFERRED-1)) { \
13090 OP **defer = defer_queue[defer_base]; \
13091 CALL_RPEEP(*defer); \
13092 S_prune_chain_head(defer); \
13093 defer_base = (defer_base + 1) % MAX_DEFERRED; \
13096 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
13099 #define IS_AND_OP(o) (o->op_type == OP_AND)
13100 #define IS_OR_OP(o) (o->op_type == OP_OR)
13103 /* A peephole optimizer. We visit the ops in the order they're to execute.
13104 * See the comments at the top of this file for more details about when
13105 * peep() is called */
13108 Perl_rpeep(pTHX_ OP *o)
13112 OP* oldoldop = NULL;
13113 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
13114 int defer_base = 0;
13119 if (!o || o->op_opt)
13123 SAVEVPTR(PL_curcop);
13124 for (;; o = o->op_next) {
13125 if (o && o->op_opt)
13128 while (defer_ix >= 0) {
13130 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
13131 CALL_RPEEP(*defer);
13132 S_prune_chain_head(defer);
13139 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */
13140 assert(!oldoldop || oldoldop->op_next == oldop);
13141 assert(!oldop || oldop->op_next == o);
13143 /* By default, this op has now been optimised. A couple of cases below
13144 clear this again. */
13148 /* look for a series of 1 or more aggregate derefs, e.g.
13149 * $a[1]{foo}[$i]{$k}
13150 * and replace with a single OP_MULTIDEREF op.
13151 * Each index must be either a const, or a simple variable,
13153 * First, look for likely combinations of starting ops,
13154 * corresponding to (global and lexical variants of)
13156 * $r->[...] $r->{...}
13157 * (preceding expression)->[...]
13158 * (preceding expression)->{...}
13159 * and if so, call maybe_multideref() to do a full inspection
13160 * of the op chain and if appropriate, replace with an
13168 switch (o2->op_type) {
13170 /* $pkg[..] : gv[*pkg]
13171 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */
13173 /* Fail if there are new op flag combinations that we're
13174 * not aware of, rather than:
13175 * * silently failing to optimise, or
13176 * * silently optimising the flag away.
13177 * If this ASSUME starts failing, examine what new flag
13178 * has been added to the op, and decide whether the
13179 * optimisation should still occur with that flag, then
13180 * update the code accordingly. This applies to all the
13181 * other ASSUMEs in the block of code too.
13183 ASSUME(!(o2->op_flags &
13184 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL)));
13185 ASSUME(!(o2->op_private & ~OPpEARLY_CV));
13189 if (o2->op_type == OP_RV2AV) {
13190 action = MDEREF_AV_gvav_aelem;
13194 if (o2->op_type == OP_RV2HV) {
13195 action = MDEREF_HV_gvhv_helem;
13199 if (o2->op_type != OP_RV2SV)
13202 /* at this point we've seen gv,rv2sv, so the only valid
13203 * construct left is $pkg->[] or $pkg->{} */
13205 ASSUME(!(o2->op_flags & OPf_STACKED));
13206 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13207 != (OPf_WANT_SCALAR|OPf_MOD))
13210 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS
13211 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO)));
13212 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO))
13214 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV
13215 && (o2->op_private & OPpDEREF) != OPpDEREF_HV)
13219 if (o2->op_type == OP_RV2AV) {
13220 action = MDEREF_AV_gvsv_vivify_rv2av_aelem;
13223 if (o2->op_type == OP_RV2HV) {
13224 action = MDEREF_HV_gvsv_vivify_rv2hv_helem;
13230 /* $lex->[...]: padsv[$lex] sM/DREFAV */
13232 ASSUME(!(o2->op_flags &
13233 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL)));
13234 if ((o2->op_flags &
13235 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13236 != (OPf_WANT_SCALAR|OPf_MOD))
13239 ASSUME(!(o2->op_private &
13240 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO)));
13241 /* skip if state or intro, or not a deref */
13242 if ( o2->op_private != OPpDEREF_AV
13243 && o2->op_private != OPpDEREF_HV)
13247 if (o2->op_type == OP_RV2AV) {
13248 action = MDEREF_AV_padsv_vivify_rv2av_aelem;
13251 if (o2->op_type == OP_RV2HV) {
13252 action = MDEREF_HV_padsv_vivify_rv2hv_helem;
13259 /* $lex[..]: padav[@lex:1,2] sR *
13260 * or $lex{..}: padhv[%lex:1,2] sR */
13261 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS|
13262 OPf_REF|OPf_SPECIAL)));
13263 if ((o2->op_flags &
13264 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL))
13265 != (OPf_WANT_SCALAR|OPf_REF))
13267 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF))
13269 /* OPf_PARENS isn't currently used in this case;
13270 * if that changes, let us know! */
13271 ASSUME(!(o2->op_flags & OPf_PARENS));
13273 /* at this point, we wouldn't expect any of the remaining
13274 * possible private flags:
13275 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL,
13276 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB
13278 * OPpSLICEWARNING shouldn't affect runtime
13280 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING)));
13282 action = o2->op_type == OP_PADAV
13283 ? MDEREF_AV_padav_aelem
13284 : MDEREF_HV_padhv_helem;
13286 S_maybe_multideref(aTHX_ o, o2, action, 0);
13292 action = o2->op_type == OP_RV2AV
13293 ? MDEREF_AV_pop_rv2av_aelem
13294 : MDEREF_HV_pop_rv2hv_helem;
13297 /* (expr)->[...]: rv2av sKR/1;
13298 * (expr)->{...}: rv2hv sKR/1; */
13300 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV);
13302 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS
13303 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL)));
13304 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF))
13307 /* at this point, we wouldn't expect any of these
13308 * possible private flags:
13309 * OPpMAYBE_LVSUB, OPpLVAL_INTRO
13310 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only)
13312 ASSUME(!(o2->op_private &
13313 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING
13315 hints |= (o2->op_private & OPpHINT_STRICT_REFS);
13319 S_maybe_multideref(aTHX_ o, o2, action, hints);
13328 switch (o->op_type) {
13330 PL_curcop = ((COP*)o); /* for warnings */
13333 PL_curcop = ((COP*)o); /* for warnings */
13335 /* Optimise a "return ..." at the end of a sub to just be "...".
13336 * This saves 2 ops. Before:
13337 * 1 <;> nextstate(main 1 -e:1) v ->2
13338 * 4 <@> return K ->5
13339 * 2 <0> pushmark s ->3
13340 * - <1> ex-rv2sv sK/1 ->4
13341 * 3 <#> gvsv[*cat] s ->4
13344 * - <@> return K ->-
13345 * - <0> pushmark s ->2
13346 * - <1> ex-rv2sv sK/1 ->-
13347 * 2 <$> gvsv(*cat) s ->3
13350 OP *next = o->op_next;
13351 OP *sibling = OpSIBLING(o);
13352 if ( OP_TYPE_IS(next, OP_PUSHMARK)
13353 && OP_TYPE_IS(sibling, OP_RETURN)
13354 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
13355 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
13356 ||OP_TYPE_IS(sibling->op_next->op_next,
13358 && cUNOPx(sibling)->op_first == next
13359 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next
13362 /* Look through the PUSHMARK's siblings for one that
13363 * points to the RETURN */
13364 OP *top = OpSIBLING(next);
13365 while (top && top->op_next) {
13366 if (top->op_next == sibling) {
13367 top->op_next = sibling->op_next;
13368 o->op_next = next->op_next;
13371 top = OpSIBLING(top);
13376 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
13378 * This latter form is then suitable for conversion into padrange
13379 * later on. Convert:
13381 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
13385 * nextstate1 -> listop -> nextstate3
13387 * pushmark -> padop1 -> padop2
13389 if (o->op_next && (
13390 o->op_next->op_type == OP_PADSV
13391 || o->op_next->op_type == OP_PADAV
13392 || o->op_next->op_type == OP_PADHV
13394 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
13395 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
13396 && o->op_next->op_next->op_next && (
13397 o->op_next->op_next->op_next->op_type == OP_PADSV
13398 || o->op_next->op_next->op_next->op_type == OP_PADAV
13399 || o->op_next->op_next->op_next->op_type == OP_PADHV
13401 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
13402 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
13403 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
13404 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
13406 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm;
13409 ns2 = pad1->op_next;
13410 pad2 = ns2->op_next;
13411 ns3 = pad2->op_next;
13413 /* we assume here that the op_next chain is the same as
13414 * the op_sibling chain */
13415 assert(OpSIBLING(o) == pad1);
13416 assert(OpSIBLING(pad1) == ns2);
13417 assert(OpSIBLING(ns2) == pad2);
13418 assert(OpSIBLING(pad2) == ns3);
13420 /* excise and delete ns2 */
13421 op_sibling_splice(NULL, pad1, 1, NULL);
13424 /* excise pad1 and pad2 */
13425 op_sibling_splice(NULL, o, 2, NULL);
13427 /* create new listop, with children consisting of:
13428 * a new pushmark, pad1, pad2. */
13429 newop = newLISTOP(OP_LIST, 0, pad1, pad2);
13430 newop->op_flags |= OPf_PARENS;
13431 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
13433 /* insert newop between o and ns3 */
13434 op_sibling_splice(NULL, o, 0, newop);
13436 /*fixup op_next chain */
13437 newpm = cUNOPx(newop)->op_first; /* pushmark */
13438 o ->op_next = newpm;
13439 newpm->op_next = pad1;
13440 pad1 ->op_next = pad2;
13441 pad2 ->op_next = newop; /* listop */
13442 newop->op_next = ns3;
13444 /* Ensure pushmark has this flag if padops do */
13445 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) {
13446 newpm->op_flags |= OPf_MOD;
13452 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
13453 to carry two labels. For now, take the easier option, and skip
13454 this optimisation if the first NEXTSTATE has a label. */
13455 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
13456 OP *nextop = o->op_next;
13457 while (nextop && nextop->op_type == OP_NULL)
13458 nextop = nextop->op_next;
13460 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
13463 oldop->op_next = nextop;
13465 /* Skip (old)oldop assignment since the current oldop's
13466 op_next already points to the next op. */
13473 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
13474 if (o->op_next->op_private & OPpTARGET_MY) {
13475 if (o->op_flags & OPf_STACKED) /* chained concats */
13476 break; /* ignore_optimization */
13478 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
13479 o->op_targ = o->op_next->op_targ;
13480 o->op_next->op_targ = 0;
13481 o->op_private |= OPpTARGET_MY;
13484 op_null(o->op_next);
13488 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
13489 break; /* Scalar stub must produce undef. List stub is noop */
13493 if (o->op_targ == OP_NEXTSTATE
13494 || o->op_targ == OP_DBSTATE)
13496 PL_curcop = ((COP*)o);
13498 /* XXX: We avoid setting op_seq here to prevent later calls
13499 to rpeep() from mistakenly concluding that optimisation
13500 has already occurred. This doesn't fix the real problem,
13501 though (See 20010220.007). AMS 20010719 */
13502 /* op_seq functionality is now replaced by op_opt */
13510 oldop->op_next = o->op_next;
13524 convert repeat into a stub with no kids.
13526 if (o->op_next->op_type == OP_CONST
13527 || ( o->op_next->op_type == OP_PADSV
13528 && !(o->op_next->op_private & OPpLVAL_INTRO))
13529 || ( o->op_next->op_type == OP_GV
13530 && o->op_next->op_next->op_type == OP_RV2SV
13531 && !(o->op_next->op_next->op_private
13532 & (OPpLVAL_INTRO|OPpOUR_INTRO))))
13534 const OP *kid = o->op_next->op_next;
13535 if (o->op_next->op_type == OP_GV)
13536 kid = kid->op_next;
13537 /* kid is now the ex-list. */
13538 if (kid->op_type == OP_NULL
13539 && (kid = kid->op_next)->op_type == OP_CONST
13540 /* kid is now the repeat count. */
13541 && kid->op_next->op_type == OP_REPEAT
13542 && kid->op_next->op_private & OPpREPEAT_DOLIST
13543 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST
13544 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0)
13546 o = kid->op_next; /* repeat */
13548 oldop->op_next = o;
13549 op_free(cBINOPo->op_first);
13550 op_free(cBINOPo->op_last );
13551 o->op_flags &=~ OPf_KIDS;
13552 /* stub is a baseop; repeat is a binop */
13553 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP));
13554 OpTYPE_set(o, OP_STUB);
13560 /* Convert a series of PAD ops for my vars plus support into a
13561 * single padrange op. Basically
13563 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
13565 * becomes, depending on circumstances, one of
13567 * padrange ----------------------------------> (list) -> rest
13568 * padrange --------------------------------------------> rest
13570 * where all the pad indexes are sequential and of the same type
13572 * We convert the pushmark into a padrange op, then skip
13573 * any other pad ops, and possibly some trailing ops.
13574 * Note that we don't null() the skipped ops, to make it
13575 * easier for Deparse to undo this optimisation (and none of
13576 * the skipped ops are holding any resourses). It also makes
13577 * it easier for find_uninit_var(), as it can just ignore
13578 * padrange, and examine the original pad ops.
13582 OP *followop = NULL; /* the op that will follow the padrange op */
13585 PADOFFSET base = 0; /* init only to stop compiler whining */
13586 bool gvoid = 0; /* init only to stop compiler whining */
13587 bool defav = 0; /* seen (...) = @_ */
13588 bool reuse = 0; /* reuse an existing padrange op */
13590 /* look for a pushmark -> gv[_] -> rv2av */
13595 if ( p->op_type == OP_GV
13596 && cGVOPx_gv(p) == PL_defgv
13597 && (rv2av = p->op_next)
13598 && rv2av->op_type == OP_RV2AV
13599 && !(rv2av->op_flags & OPf_REF)
13600 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
13601 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
13603 q = rv2av->op_next;
13604 if (q->op_type == OP_NULL)
13606 if (q->op_type == OP_PUSHMARK) {
13616 /* scan for PAD ops */
13618 for (p = p->op_next; p; p = p->op_next) {
13619 if (p->op_type == OP_NULL)
13622 if (( p->op_type != OP_PADSV
13623 && p->op_type != OP_PADAV
13624 && p->op_type != OP_PADHV
13626 /* any private flag other than INTRO? e.g. STATE */
13627 || (p->op_private & ~OPpLVAL_INTRO)
13631 /* let $a[N] potentially be optimised into AELEMFAST_LEX
13633 if ( p->op_type == OP_PADAV
13635 && p->op_next->op_type == OP_CONST
13636 && p->op_next->op_next
13637 && p->op_next->op_next->op_type == OP_AELEM
13641 /* for 1st padop, note what type it is and the range
13642 * start; for the others, check that it's the same type
13643 * and that the targs are contiguous */
13645 intro = (p->op_private & OPpLVAL_INTRO);
13647 gvoid = OP_GIMME(p,0) == G_VOID;
13650 if ((p->op_private & OPpLVAL_INTRO) != intro)
13652 /* Note that you'd normally expect targs to be
13653 * contiguous in my($a,$b,$c), but that's not the case
13654 * when external modules start doing things, e.g.
13655 i* Function::Parameters */
13656 if (p->op_targ != base + count)
13658 assert(p->op_targ == base + count);
13659 /* Either all the padops or none of the padops should
13660 be in void context. Since we only do the optimisa-
13661 tion for av/hv when the aggregate itself is pushed
13662 on to the stack (one item), there is no need to dis-
13663 tinguish list from scalar context. */
13664 if (gvoid != (OP_GIMME(p,0) == G_VOID))
13668 /* for AV, HV, only when we're not flattening */
13669 if ( p->op_type != OP_PADSV
13671 && !(p->op_flags & OPf_REF)
13675 if (count >= OPpPADRANGE_COUNTMASK)
13678 /* there's a biggest base we can fit into a
13679 * SAVEt_CLEARPADRANGE in pp_padrange.
13680 * (The sizeof() stuff will be constant-folded, and is
13681 * intended to avoid getting "comparison is always false"
13682 * compiler warnings)
13685 && (8*sizeof(base) >
13686 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT
13688 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT))
13692 /* Success! We've got another valid pad op to optimise away */
13694 followop = p->op_next;
13697 if (count < 1 || (count == 1 && !defav))
13700 /* pp_padrange in specifically compile-time void context
13701 * skips pushing a mark and lexicals; in all other contexts
13702 * (including unknown till runtime) it pushes a mark and the
13703 * lexicals. We must be very careful then, that the ops we
13704 * optimise away would have exactly the same effect as the
13706 * In particular in void context, we can only optimise to
13707 * a padrange if see see the complete sequence
13708 * pushmark, pad*v, ...., list
13709 * which has the net effect of of leaving the markstack as it
13710 * was. Not pushing on to the stack (whereas padsv does touch
13711 * the stack) makes no difference in void context.
13715 if (followop->op_type == OP_LIST
13716 && OP_GIMME(followop,0) == G_VOID
13719 followop = followop->op_next; /* skip OP_LIST */
13721 /* consolidate two successive my(...);'s */
13724 && oldoldop->op_type == OP_PADRANGE
13725 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
13726 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
13727 && !(oldoldop->op_flags & OPf_SPECIAL)
13730 assert(oldoldop->op_next == oldop);
13731 assert( oldop->op_type == OP_NEXTSTATE
13732 || oldop->op_type == OP_DBSTATE);
13733 assert(oldop->op_next == o);
13736 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
13738 /* Do not assume pad offsets for $c and $d are con-
13743 if ( oldoldop->op_targ + old_count == base
13744 && old_count < OPpPADRANGE_COUNTMASK - count) {
13745 base = oldoldop->op_targ;
13746 count += old_count;
13751 /* if there's any immediately following singleton
13752 * my var's; then swallow them and the associated
13754 * my ($a,$b); my $c; my $d;
13756 * my ($a,$b,$c,$d);
13759 while ( ((p = followop->op_next))
13760 && ( p->op_type == OP_PADSV
13761 || p->op_type == OP_PADAV
13762 || p->op_type == OP_PADHV)
13763 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
13764 && (p->op_private & OPpLVAL_INTRO) == intro
13765 && !(p->op_private & ~OPpLVAL_INTRO)
13767 && ( p->op_next->op_type == OP_NEXTSTATE
13768 || p->op_next->op_type == OP_DBSTATE)
13769 && count < OPpPADRANGE_COUNTMASK
13770 && base + count == p->op_targ
13773 followop = p->op_next;
13781 assert(oldoldop->op_type == OP_PADRANGE);
13782 oldoldop->op_next = followop;
13783 oldoldop->op_private = (intro | count);
13789 /* Convert the pushmark into a padrange.
13790 * To make Deparse easier, we guarantee that a padrange was
13791 * *always* formerly a pushmark */
13792 assert(o->op_type == OP_PUSHMARK);
13793 o->op_next = followop;
13794 OpTYPE_set(o, OP_PADRANGE);
13796 /* bit 7: INTRO; bit 6..0: count */
13797 o->op_private = (intro | count);
13798 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
13799 | gvoid * OPf_WANT_VOID
13800 | (defav ? OPf_SPECIAL : 0));
13808 /* Skip over state($x) in void context. */
13809 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO)
13810 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID)
13812 oldop->op_next = o->op_next;
13813 goto redo_nextstate;
13815 if (o->op_type != OP_PADAV)
13819 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
13820 OP* const pop = (o->op_type == OP_PADAV) ?
13821 o->op_next : o->op_next->op_next;
13823 if (pop && pop->op_type == OP_CONST &&
13824 ((PL_op = pop->op_next)) &&
13825 pop->op_next->op_type == OP_AELEM &&
13826 !(pop->op_next->op_private &
13827 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
13828 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
13831 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
13832 no_bareword_allowed(pop);
13833 if (o->op_type == OP_GV)
13834 op_null(o->op_next);
13835 op_null(pop->op_next);
13837 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
13838 o->op_next = pop->op_next->op_next;
13839 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
13840 o->op_private = (U8)i;
13841 if (o->op_type == OP_GV) {
13844 o->op_type = OP_AELEMFAST;
13847 o->op_type = OP_AELEMFAST_LEX;
13849 if (o->op_type != OP_GV)
13853 /* Remove $foo from the op_next chain in void context. */
13855 && ( o->op_next->op_type == OP_RV2SV
13856 || o->op_next->op_type == OP_RV2AV
13857 || o->op_next->op_type == OP_RV2HV )
13858 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
13859 && !(o->op_next->op_private & OPpLVAL_INTRO))
13861 oldop->op_next = o->op_next->op_next;
13862 /* Reprocess the previous op if it is a nextstate, to
13863 allow double-nextstate optimisation. */
13865 if (oldop->op_type == OP_NEXTSTATE) {
13872 o = oldop->op_next;
13875 else if (o->op_next->op_type == OP_RV2SV) {
13876 if (!(o->op_next->op_private & OPpDEREF)) {
13877 op_null(o->op_next);
13878 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
13880 o->op_next = o->op_next->op_next;
13881 OpTYPE_set(o, OP_GVSV);
13884 else if (o->op_next->op_type == OP_READLINE
13885 && o->op_next->op_next->op_type == OP_CONCAT
13886 && (o->op_next->op_next->op_flags & OPf_STACKED))
13888 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
13889 OpTYPE_set(o, OP_RCATLINE);
13890 o->op_flags |= OPf_STACKED;
13891 op_null(o->op_next->op_next);
13892 op_null(o->op_next);
13897 #define HV_OR_SCALARHV(op) \
13898 ( (op)->op_type == OP_PADHV || (op)->op_type == OP_RV2HV \
13900 : (op)->op_type == OP_SCALAR && (op)->op_flags & OPf_KIDS \
13901 && ( cUNOPx(op)->op_first->op_type == OP_PADHV \
13902 || cUNOPx(op)->op_first->op_type == OP_RV2HV) \
13903 ? cUNOPx(op)->op_first \
13907 if ((fop = HV_OR_SCALARHV(cUNOP->op_first)))
13908 fop->op_private |= OPpTRUEBOOL;
13914 fop = cLOGOP->op_first;
13915 sop = OpSIBLING(fop);
13916 while (cLOGOP->op_other->op_type == OP_NULL)
13917 cLOGOP->op_other = cLOGOP->op_other->op_next;
13918 while (o->op_next && ( o->op_type == o->op_next->op_type
13919 || o->op_next->op_type == OP_NULL))
13920 o->op_next = o->op_next->op_next;
13922 /* if we're an OR and our next is a AND in void context, we'll
13923 follow it's op_other on short circuit, same for reverse.
13924 We can't do this with OP_DOR since if it's true, its return
13925 value is the underlying value which must be evaluated
13929 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
13930 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
13932 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
13934 o->op_next = ((LOGOP*)o->op_next)->op_other;
13936 DEFER(cLOGOP->op_other);
13939 fop = HV_OR_SCALARHV(fop);
13940 if (sop) sop = HV_OR_SCALARHV(sop);
13945 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) {
13946 while (nop && nop->op_next) {
13947 switch (nop->op_next->op_type) {
13952 lop = nop = nop->op_next;
13955 nop = nop->op_next;
13964 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
13965 || o->op_type == OP_AND )
13966 fop->op_private |= OPpTRUEBOOL;
13967 else if (!(lop->op_flags & OPf_WANT))
13968 fop->op_private |= OPpMAYBE_TRUEBOOL;
13970 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
13972 sop->op_private |= OPpTRUEBOOL;
13979 if ((fop = HV_OR_SCALARHV(cLOGOP->op_first)))
13980 fop->op_private |= OPpTRUEBOOL;
13981 #undef HV_OR_SCALARHV
13982 /* GERONIMO! */ /* FALLTHROUGH */
13991 while (cLOGOP->op_other->op_type == OP_NULL)
13992 cLOGOP->op_other = cLOGOP->op_other->op_next;
13993 DEFER(cLOGOP->op_other);
13998 while (cLOOP->op_redoop->op_type == OP_NULL)
13999 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
14000 while (cLOOP->op_nextop->op_type == OP_NULL)
14001 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
14002 while (cLOOP->op_lastop->op_type == OP_NULL)
14003 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
14004 /* a while(1) loop doesn't have an op_next that escapes the
14005 * loop, so we have to explicitly follow the op_lastop to
14006 * process the rest of the code */
14007 DEFER(cLOOP->op_lastop);
14011 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
14012 DEFER(cLOGOPo->op_other);
14016 assert(!(cPMOP->op_pmflags & PMf_ONCE));
14017 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
14018 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
14019 cPMOP->op_pmstashstartu.op_pmreplstart
14020 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
14021 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
14027 if (o->op_flags & OPf_SPECIAL) {
14028 /* first arg is a code block */
14029 OP * const nullop = OpSIBLING(cLISTOP->op_first);
14030 OP * kid = cUNOPx(nullop)->op_first;
14032 assert(nullop->op_type == OP_NULL);
14033 assert(kid->op_type == OP_SCOPE
14034 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
14035 /* since OP_SORT doesn't have a handy op_other-style
14036 * field that can point directly to the start of the code
14037 * block, store it in the otherwise-unused op_next field
14038 * of the top-level OP_NULL. This will be quicker at
14039 * run-time, and it will also allow us to remove leading
14040 * OP_NULLs by just messing with op_nexts without
14041 * altering the basic op_first/op_sibling layout. */
14042 kid = kLISTOP->op_first;
14044 (kid->op_type == OP_NULL
14045 && ( kid->op_targ == OP_NEXTSTATE
14046 || kid->op_targ == OP_DBSTATE ))
14047 || kid->op_type == OP_STUB
14048 || kid->op_type == OP_ENTER);
14049 nullop->op_next = kLISTOP->op_next;
14050 DEFER(nullop->op_next);
14053 /* check that RHS of sort is a single plain array */
14054 oright = cUNOPo->op_first;
14055 if (!oright || oright->op_type != OP_PUSHMARK)
14058 if (o->op_private & OPpSORT_INPLACE)
14061 /* reverse sort ... can be optimised. */
14062 if (!OpHAS_SIBLING(cUNOPo)) {
14063 /* Nothing follows us on the list. */
14064 OP * const reverse = o->op_next;
14066 if (reverse->op_type == OP_REVERSE &&
14067 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
14068 OP * const pushmark = cUNOPx(reverse)->op_first;
14069 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
14070 && (OpSIBLING(cUNOPx(pushmark)) == o)) {
14071 /* reverse -> pushmark -> sort */
14072 o->op_private |= OPpSORT_REVERSE;
14074 pushmark->op_next = oright->op_next;
14084 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
14086 LISTOP *enter, *exlist;
14088 if (o->op_private & OPpSORT_INPLACE)
14091 enter = (LISTOP *) o->op_next;
14094 if (enter->op_type == OP_NULL) {
14095 enter = (LISTOP *) enter->op_next;
14099 /* for $a (...) will have OP_GV then OP_RV2GV here.
14100 for (...) just has an OP_GV. */
14101 if (enter->op_type == OP_GV) {
14102 gvop = (OP *) enter;
14103 enter = (LISTOP *) enter->op_next;
14106 if (enter->op_type == OP_RV2GV) {
14107 enter = (LISTOP *) enter->op_next;
14113 if (enter->op_type != OP_ENTERITER)
14116 iter = enter->op_next;
14117 if (!iter || iter->op_type != OP_ITER)
14120 expushmark = enter->op_first;
14121 if (!expushmark || expushmark->op_type != OP_NULL
14122 || expushmark->op_targ != OP_PUSHMARK)
14125 exlist = (LISTOP *) OpSIBLING(expushmark);
14126 if (!exlist || exlist->op_type != OP_NULL
14127 || exlist->op_targ != OP_LIST)
14130 if (exlist->op_last != o) {
14131 /* Mmm. Was expecting to point back to this op. */
14134 theirmark = exlist->op_first;
14135 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
14138 if (OpSIBLING(theirmark) != o) {
14139 /* There's something between the mark and the reverse, eg
14140 for (1, reverse (...))
14145 ourmark = ((LISTOP *)o)->op_first;
14146 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
14149 ourlast = ((LISTOP *)o)->op_last;
14150 if (!ourlast || ourlast->op_next != o)
14153 rv2av = OpSIBLING(ourmark);
14154 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av)
14155 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
14156 /* We're just reversing a single array. */
14157 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
14158 enter->op_flags |= OPf_STACKED;
14161 /* We don't have control over who points to theirmark, so sacrifice
14163 theirmark->op_next = ourmark->op_next;
14164 theirmark->op_flags = ourmark->op_flags;
14165 ourlast->op_next = gvop ? gvop : (OP *) enter;
14168 enter->op_private |= OPpITER_REVERSED;
14169 iter->op_private |= OPpITER_REVERSED;
14173 o = oldop->op_next;
14181 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
14182 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
14187 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)
14188 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb)))
14191 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
14193 sv = newRV((SV *)PL_compcv);
14197 OpTYPE_set(o, OP_CONST);
14198 o->op_flags |= OPf_SPECIAL;
14199 cSVOPo->op_sv = sv;
14204 if (OP_GIMME(o,0) == G_VOID
14205 || ( o->op_next->op_type == OP_LINESEQ
14206 && ( o->op_next->op_next->op_type == OP_LEAVESUB
14207 || ( o->op_next->op_next->op_type == OP_RETURN
14208 && !CvLVALUE(PL_compcv)))))
14210 OP *right = cBINOP->op_first;
14229 OP *left = OpSIBLING(right);
14230 if (left->op_type == OP_SUBSTR
14231 && (left->op_private & 7) < 4) {
14233 /* cut out right */
14234 op_sibling_splice(o, NULL, 1, NULL);
14235 /* and insert it as second child of OP_SUBSTR */
14236 op_sibling_splice(left, cBINOPx(left)->op_first, 0,
14238 left->op_private |= OPpSUBSTR_REPL_FIRST;
14240 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
14247 int l, r, lr, lscalars, rscalars;
14249 /* handle common vars detection, e.g. ($a,$b) = ($b,$a).
14250 Note that we do this now rather than in newASSIGNOP(),
14251 since only by now are aliased lexicals flagged as such
14253 See the essay "Common vars in list assignment" above for
14254 the full details of the rationale behind all the conditions
14257 PL_generation sorcery:
14258 To detect whether there are common vars, the global var
14259 PL_generation is incremented for each assign op we scan.
14260 Then we run through all the lexical variables on the LHS,
14261 of the assignment, setting a spare slot in each of them to
14262 PL_generation. Then we scan the RHS, and if any lexicals
14263 already have that value, we know we've got commonality.
14264 Also, if the generation number is already set to
14265 PERL_INT_MAX, then the variable is involved in aliasing, so
14266 we also have potential commonality in that case.
14272 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, 1, &lscalars);
14275 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, 1, &rscalars);
14279 /* After looking for things which are *always* safe, this main
14280 * if/else chain selects primarily based on the type of the
14281 * LHS, gradually working its way down from the more dangerous
14282 * to the more restrictive and thus safer cases */
14284 if ( !l /* () = ....; */
14285 || !r /* .... = (); */
14286 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */
14287 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */
14288 || (lscalars < 2) /* ($x, undef) = ... */
14290 NOOP; /* always safe */
14292 else if (l & AAS_DANGEROUS) {
14293 /* always dangerous */
14294 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14295 o->op_private |= OPpASSIGN_COMMON_AGG;
14297 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) {
14298 /* package vars are always dangerous - too many
14299 * aliasing possibilities */
14300 if (l & AAS_PKG_SCALAR)
14301 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14302 if (l & AAS_PKG_AGG)
14303 o->op_private |= OPpASSIGN_COMMON_AGG;
14305 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG
14306 |AAS_LEX_SCALAR|AAS_LEX_AGG))
14308 /* LHS contains only lexicals and safe ops */
14310 if (l & (AAS_MY_AGG|AAS_LEX_AGG))
14311 o->op_private |= OPpASSIGN_COMMON_AGG;
14313 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) {
14314 if (lr & AAS_LEX_SCALAR_COMM)
14315 o->op_private |= OPpASSIGN_COMMON_SCALAR;
14316 else if ( !(l & AAS_LEX_SCALAR)
14317 && (r & AAS_DEFAV))
14321 * as scalar-safe for performance reasons.
14322 * (it will still have been marked _AGG if necessary */
14325 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS))
14326 o->op_private |= OPpASSIGN_COMMON_RC1;
14331 * may have to handle aggregate on LHS, but we can't
14332 * have common scalars. */
14335 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1);
14341 Perl_cpeep_t cpeep =
14342 XopENTRYCUSTOM(o, xop_peep);
14344 cpeep(aTHX_ o, oldop);
14349 /* did we just null the current op? If so, re-process it to handle
14350 * eliding "empty" ops from the chain */
14351 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
14364 Perl_peep(pTHX_ OP *o)
14370 =head1 Custom Operators
14372 =for apidoc Ao||custom_op_xop
14373 Return the XOP structure for a given custom op. This macro should be
14374 considered internal to C<OP_NAME> and the other access macros: use them instead.
14375 This macro does call a function. Prior
14376 to 5.19.6, this was implemented as a
14383 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
14389 static const XOP xop_null = { 0, 0, 0, 0, 0 };
14391 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
14392 assert(o->op_type == OP_CUSTOM);
14394 /* This is wrong. It assumes a function pointer can be cast to IV,
14395 * which isn't guaranteed, but this is what the old custom OP code
14396 * did. In principle it should be safer to Copy the bytes of the
14397 * pointer into a PV: since the new interface is hidden behind
14398 * functions, this can be changed later if necessary. */
14399 /* Change custom_op_xop if this ever happens */
14400 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
14403 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
14405 /* assume noone will have just registered a desc */
14406 if (!he && PL_custom_op_names &&
14407 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
14412 /* XXX does all this need to be shared mem? */
14413 Newxz(xop, 1, XOP);
14414 pv = SvPV(HeVAL(he), l);
14415 XopENTRY_set(xop, xop_name, savepvn(pv, l));
14416 if (PL_custom_op_descs &&
14417 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
14419 pv = SvPV(HeVAL(he), l);
14420 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
14422 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
14426 xop = (XOP *)&xop_null;
14428 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
14432 if(field == XOPe_xop_ptr) {
14435 const U32 flags = XopFLAGS(xop);
14436 if(flags & field) {
14438 case XOPe_xop_name:
14439 any.xop_name = xop->xop_name;
14441 case XOPe_xop_desc:
14442 any.xop_desc = xop->xop_desc;
14444 case XOPe_xop_class:
14445 any.xop_class = xop->xop_class;
14447 case XOPe_xop_peep:
14448 any.xop_peep = xop->xop_peep;
14451 NOT_REACHED; /* NOTREACHED */
14456 case XOPe_xop_name:
14457 any.xop_name = XOPd_xop_name;
14459 case XOPe_xop_desc:
14460 any.xop_desc = XOPd_xop_desc;
14462 case XOPe_xop_class:
14463 any.xop_class = XOPd_xop_class;
14465 case XOPe_xop_peep:
14466 any.xop_peep = XOPd_xop_peep;
14469 NOT_REACHED; /* NOTREACHED */
14474 /* On some platforms (HP-UX, IA64) gcc emits a warning for this function:
14475 * op.c: In function 'Perl_custom_op_get_field':
14476 * op.c:...: warning: 'any.xop_name' may be used uninitialized in this function [-Wmaybe-uninitialized]
14477 * This is because on those platforms (with -DEBUGGING) NOT_REACHED
14478 * expands to assert(0), which expands to ((0) ? (void)0 :
14479 * __assert(...)), and gcc doesn't know that __assert can never return. */
14485 =for apidoc Ao||custom_op_register
14486 Register a custom op. See L<perlguts/"Custom Operators">.
14492 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
14496 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
14498 /* see the comment in custom_op_xop */
14499 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
14501 if (!PL_custom_ops)
14502 PL_custom_ops = newHV();
14504 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
14505 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
14510 =for apidoc core_prototype
14512 This function assigns the prototype of the named core function to C<sv>, or
14513 to a new mortal SV if C<sv> is C<NULL>. It returns the modified C<sv>, or
14514 C<NULL> if the core function has no prototype. C<code> is a code as returned
14515 by C<keyword()>. It must not be equal to 0.
14521 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
14524 int i = 0, n = 0, seen_question = 0, defgv = 0;
14526 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
14527 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
14528 bool nullret = FALSE;
14530 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
14534 if (!sv) sv = sv_newmortal();
14536 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
14538 switch (code < 0 ? -code : code) {
14539 case KEY_and : case KEY_chop: case KEY_chomp:
14540 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
14541 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
14542 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
14543 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
14544 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
14545 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
14546 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
14547 case KEY_x : case KEY_xor :
14548 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
14549 case KEY_glob: retsetpvs("_;", OP_GLOB);
14550 case KEY_keys: retsetpvs("\\[%@]", OP_KEYS);
14551 case KEY_values: retsetpvs("\\[%@]", OP_VALUES);
14552 case KEY_each: retsetpvs("\\[%@]", OP_EACH);
14553 case KEY_push: retsetpvs("\\@@", OP_PUSH);
14554 case KEY_unshift: retsetpvs("\\@@", OP_UNSHIFT);
14555 case KEY_pop: retsetpvs(";\\@", OP_POP);
14556 case KEY_shift: retsetpvs(";\\@", OP_SHIFT);
14557 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
14559 retsetpvs("\\@;$$@", OP_SPLICE);
14560 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
14562 case KEY_evalbytes:
14563 name = "entereval"; break;
14571 while (i < MAXO) { /* The slow way. */
14572 if (strEQ(name, PL_op_name[i])
14573 || strEQ(name, PL_op_desc[i]))
14575 if (nullret) { assert(opnum); *opnum = i; return NULL; }
14582 defgv = PL_opargs[i] & OA_DEFGV;
14583 oa = PL_opargs[i] >> OASHIFT;
14585 if (oa & OA_OPTIONAL && !seen_question && (
14586 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
14591 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
14592 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
14593 /* But globs are already references (kinda) */
14594 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
14598 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
14599 && !scalar_mod_type(NULL, i)) {
14604 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
14608 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
14609 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
14610 str[n-1] = '_'; defgv = 0;
14614 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
14616 sv_setpvn(sv, str, n - 1);
14617 if (opnum) *opnum = i;
14622 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
14625 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv);
14628 PERL_ARGS_ASSERT_CORESUB_OP;
14632 return op_append_elem(OP_LINESEQ,
14635 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
14639 case OP_SELECT: /* which represents OP_SSELECT as well */
14644 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
14645 newSVOP(OP_CONST, 0, newSVuv(1))
14647 coresub_op(newSVuv((UV)OP_SSELECT), 0,
14649 coresub_op(coreargssv, 0, OP_SELECT)
14653 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
14655 return op_append_elem(
14658 opnum == OP_WANTARRAY || opnum == OP_RUNCV
14659 ? OPpOFFBYONE << 8 : 0)
14661 case OA_BASEOP_OR_UNOP:
14662 if (opnum == OP_ENTEREVAL) {
14663 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
14664 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
14666 else o = newUNOP(opnum,0,argop);
14667 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
14670 if (is_handle_constructor(o, 1))
14671 argop->op_private |= OPpCOREARGS_DEREF1;
14672 if (scalar_mod_type(NULL, opnum))
14673 argop->op_private |= OPpCOREARGS_SCALARMOD;
14677 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
14678 if (is_handle_constructor(o, 2))
14679 argop->op_private |= OPpCOREARGS_DEREF2;
14680 if (opnum == OP_SUBSTR) {
14681 o->op_private |= OPpMAYBE_LVSUB;
14690 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
14691 SV * const *new_const_svp)
14693 const char *hvname;
14694 bool is_const = !!CvCONST(old_cv);
14695 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
14697 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
14699 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
14701 /* They are 2 constant subroutines generated from
14702 the same constant. This probably means that
14703 they are really the "same" proxy subroutine
14704 instantiated in 2 places. Most likely this is
14705 when a constant is exported twice. Don't warn.
14708 (ckWARN(WARN_REDEFINE)
14710 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
14711 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
14712 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
14713 strEQ(hvname, "autouse"))
14717 && ckWARN_d(WARN_REDEFINE)
14718 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
14721 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
14723 ? "Constant subroutine %"SVf" redefined"
14724 : "Subroutine %"SVf" redefined",
14729 =head1 Hook manipulation
14731 These functions provide convenient and thread-safe means of manipulating
14738 =for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p
14740 Puts a C function into the chain of check functions for a specified op
14741 type. This is the preferred way to manipulate the L</PL_check> array.
14742 C<opcode> specifies which type of op is to be affected. C<new_checker>
14743 is a pointer to the C function that is to be added to that opcode's
14744 check chain, and C<old_checker_p> points to the storage location where a
14745 pointer to the next function in the chain will be stored. The value of
14746 C<new_pointer> is written into the L</PL_check> array, while the value
14747 previously stored there is written to C<*old_checker_p>.
14749 The function should be defined like this:
14751 static OP *new_checker(pTHX_ OP *op) { ... }
14753 It is intended to be called in this manner:
14755 new_checker(aTHX_ op)
14757 C<old_checker_p> should be defined like this:
14759 static Perl_check_t old_checker_p;
14761 L</PL_check> is global to an entire process, and a module wishing to
14762 hook op checking may find itself invoked more than once per process,
14763 typically in different threads. To handle that situation, this function
14764 is idempotent. The location C<*old_checker_p> must initially (once
14765 per process) contain a null pointer. A C variable of static duration
14766 (declared at file scope, typically also marked C<static> to give
14767 it internal linkage) will be implicitly initialised appropriately,
14768 if it does not have an explicit initialiser. This function will only
14769 actually modify the check chain if it finds C<*old_checker_p> to be null.
14770 This function is also thread safe on the small scale. It uses appropriate
14771 locking to avoid race conditions in accessing L</PL_check>.
14773 When this function is called, the function referenced by C<new_checker>
14774 must be ready to be called, except for C<*old_checker_p> being unfilled.
14775 In a threading situation, C<new_checker> may be called immediately,
14776 even before this function has returned. C<*old_checker_p> will always
14777 be appropriately set before C<new_checker> is called. If C<new_checker>
14778 decides not to do anything special with an op that it is given (which
14779 is the usual case for most uses of op check hooking), it must chain the
14780 check function referenced by C<*old_checker_p>.
14782 If you want to influence compilation of calls to a specific subroutine,
14783 then use L</cv_set_call_checker> rather than hooking checking of all
14790 Perl_wrap_op_checker(pTHX_ Optype opcode,
14791 Perl_check_t new_checker, Perl_check_t *old_checker_p)
14795 PERL_UNUSED_CONTEXT;
14796 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
14797 if (*old_checker_p) return;
14798 OP_CHECK_MUTEX_LOCK;
14799 if (!*old_checker_p) {
14800 *old_checker_p = PL_check[opcode];
14801 PL_check[opcode] = new_checker;
14803 OP_CHECK_MUTEX_UNLOCK;
14808 /* Efficient sub that returns a constant scalar value. */
14810 const_sv_xsub(pTHX_ CV* cv)
14813 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
14814 PERL_UNUSED_ARG(items);
14824 const_av_xsub(pTHX_ CV* cv)
14827 AV * const av = MUTABLE_AV(XSANY.any_ptr);
14835 if (SvRMAGICAL(av))
14836 Perl_croak(aTHX_ "Magical list constants are not supported");
14837 if (GIMME_V != G_ARRAY) {
14839 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
14842 EXTEND(SP, AvFILLp(av)+1);
14843 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
14844 XSRETURN(AvFILLp(av)+1);
14848 * ex: set ts=8 sts=4 sw=4 et: