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);
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;
4262 SV * const oldwarnhook = PL_warnhook;
4263 SV * const olddiehook = PL_diehook;
4265 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 old_cxix = cxstack_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 we croaked, depending on how we croaked the eval scope
4400 * may or may not have already been popped */
4401 if (cxstack_ix > old_cxix) {
4402 assert(cxstack_ix == old_cxix + 1);
4403 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
4404 delete_eval_scope();
4409 /* OP_STRINGIFY and constant folding are used to implement qq.
4410 Here the constant folding is an implementation detail that we
4411 want to hide. If the stringify op is itself already marked
4412 folded, however, then it is actually a folded join. */
4413 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4418 else if (!SvIMMORTAL(sv)) {
4422 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4423 if (!is_stringify) newop->op_folded = 1;
4431 S_gen_constant_list(pTHX_ OP *o)
4435 const SSize_t oldtmps_floor = PL_tmps_floor;
4440 if (PL_parser && PL_parser->error_count)
4441 return o; /* Don't attempt to run with errors */
4443 curop = LINKLIST(o);
4446 S_prune_chain_head(&curop);
4448 Perl_pp_pushmark(aTHX);
4451 assert (!(curop->op_flags & OPf_SPECIAL));
4452 assert(curop->op_type == OP_RANGE);
4453 Perl_pp_anonlist(aTHX);
4454 PL_tmps_floor = oldtmps_floor;
4456 OpTYPE_set(o, OP_RV2AV);
4457 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4458 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4459 o->op_opt = 0; /* needs to be revisited in rpeep() */
4460 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4462 /* replace subtree with an OP_CONST */
4463 curop = ((UNOP*)o)->op_first;
4464 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4467 if (AvFILLp(av) != -1)
4468 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4471 SvREADONLY_on(*svp);
4478 =head1 Optree Manipulation Functions
4481 /* List constructors */
4484 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4486 Append an item to the list of ops contained directly within a list-type
4487 op, returning the lengthened list. C<first> is the list-type op,
4488 and C<last> is the op to append to the list. C<optype> specifies the
4489 intended opcode for the list. If C<first> is not already a list of the
4490 right type, it will be upgraded into one. If either C<first> or C<last>
4491 is null, the other is returned unchanged.
4497 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4505 if (first->op_type != (unsigned)type
4506 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4508 return newLISTOP(type, 0, first, last);
4511 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4512 first->op_flags |= OPf_KIDS;
4517 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4519 Concatenate the lists of ops contained directly within two list-type ops,
4520 returning the combined list. C<first> and C<last> are the list-type ops
4521 to concatenate. C<optype> specifies the intended opcode for the list.
4522 If either C<first> or C<last> is not already a list of the right type,
4523 it will be upgraded into one. If either C<first> or C<last> is null,
4524 the other is returned unchanged.
4530 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4538 if (first->op_type != (unsigned)type)
4539 return op_prepend_elem(type, first, last);
4541 if (last->op_type != (unsigned)type)
4542 return op_append_elem(type, first, last);
4544 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4545 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4546 OpLASTSIB_set(((LISTOP*)first)->op_last, first);
4547 first->op_flags |= (last->op_flags & OPf_KIDS);
4549 S_op_destroy(aTHX_ last);
4555 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4557 Prepend an item to the list of ops contained directly within a list-type
4558 op, returning the lengthened list. C<first> is the op to prepend to the
4559 list, and C<last> is the list-type op. C<optype> specifies the intended
4560 opcode for the list. If C<last> is not already a list of the right type,
4561 it will be upgraded into one. If either C<first> or C<last> is null,
4562 the other is returned unchanged.
4568 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4576 if (last->op_type == (unsigned)type) {
4577 if (type == OP_LIST) { /* already a PUSHMARK there */
4578 /* insert 'first' after pushmark */
4579 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4580 if (!(first->op_flags & OPf_PARENS))
4581 last->op_flags &= ~OPf_PARENS;
4584 op_sibling_splice(last, NULL, 0, first);
4585 last->op_flags |= OPf_KIDS;
4589 return newLISTOP(type, 0, first, last);
4593 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4595 Converts C<o> into a list op if it is not one already, and then converts it
4596 into the specified C<type>, calling its check function, allocating a target if
4597 it needs one, and folding constants.
4599 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4600 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4601 C<op_convert_list> to make it the right type.
4607 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4610 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4611 if (!o || o->op_type != OP_LIST)
4612 o = force_list(o, 0);
4615 o->op_flags &= ~OPf_WANT;
4616 o->op_private &= ~OPpLVAL_INTRO;
4619 if (!(PL_opargs[type] & OA_MARK))
4620 op_null(cLISTOPo->op_first);
4622 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
4623 if (kid2 && kid2->op_type == OP_COREARGS) {
4624 op_null(cLISTOPo->op_first);
4625 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4629 OpTYPE_set(o, type);
4630 o->op_flags |= flags;
4631 if (flags & OPf_FOLDED)
4634 o = CHECKOP(type, o);
4635 if (o->op_type != (unsigned)type)
4638 return fold_constants(op_integerize(op_std_init(o)));
4645 =head1 Optree construction
4647 =for apidoc Am|OP *|newNULLLIST
4649 Constructs, checks, and returns a new C<stub> op, which represents an
4650 empty list expression.
4656 Perl_newNULLLIST(pTHX)
4658 return newOP(OP_STUB, 0);
4661 /* promote o and any siblings to be a list if its not already; i.e.
4669 * pushmark - o - A - B
4671 * If nullit it true, the list op is nulled.
4675 S_force_list(pTHX_ OP *o, bool nullit)
4677 if (!o || o->op_type != OP_LIST) {
4680 /* manually detach any siblings then add them back later */
4681 rest = OpSIBLING(o);
4682 OpLASTSIB_set(o, NULL);
4684 o = newLISTOP(OP_LIST, 0, o, NULL);
4686 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4694 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4696 Constructs, checks, and returns an op of any list type. C<type> is
4697 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4698 C<OPf_KIDS> will be set automatically if required. C<first> and C<last>
4699 supply up to two ops to be direct children of the list op; they are
4700 consumed by this function and become part of the constructed op tree.
4702 For most list operators, the check function expects all the kid ops to be
4703 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
4704 appropriate. What you want to do in that case is create an op of type
4705 C<OP_LIST>, append more children to it, and then call L</op_convert_list>.
4706 See L</op_convert_list> for more information.
4713 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4718 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
4719 || type == OP_CUSTOM);
4721 NewOp(1101, listop, 1, LISTOP);
4723 OpTYPE_set(listop, type);
4726 listop->op_flags = (U8)flags;
4730 else if (!first && last)
4733 OpMORESIB_set(first, last);
4734 listop->op_first = first;
4735 listop->op_last = last;
4736 if (type == OP_LIST) {
4737 OP* const pushop = newOP(OP_PUSHMARK, 0);
4738 OpMORESIB_set(pushop, first);
4739 listop->op_first = pushop;
4740 listop->op_flags |= OPf_KIDS;
4742 listop->op_last = pushop;
4744 if (listop->op_last)
4745 OpLASTSIB_set(listop->op_last, (OP*)listop);
4747 return CHECKOP(type, listop);
4751 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4753 Constructs, checks, and returns an op of any base type (any type that
4754 has no extra fields). C<type> is the opcode. C<flags> gives the
4755 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4762 Perl_newOP(pTHX_ I32 type, I32 flags)
4767 if (type == -OP_ENTEREVAL) {
4768 type = OP_ENTEREVAL;
4769 flags |= OPpEVAL_BYTES<<8;
4772 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4773 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4774 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4775 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4777 NewOp(1101, o, 1, OP);
4778 OpTYPE_set(o, type);
4779 o->op_flags = (U8)flags;
4782 o->op_private = (U8)(0 | (flags >> 8));
4783 if (PL_opargs[type] & OA_RETSCALAR)
4785 if (PL_opargs[type] & OA_TARGET)
4786 o->op_targ = pad_alloc(type, SVs_PADTMP);
4787 return CHECKOP(type, o);
4791 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4793 Constructs, checks, and returns an op of any unary type. C<type> is
4794 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4795 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4796 bits, the eight bits of C<op_private>, except that the bit with value 1
4797 is automatically set. C<first> supplies an optional op to be the direct
4798 child of the unary op; it is consumed by this function and become part
4799 of the constructed op tree.
4805 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4810 if (type == -OP_ENTEREVAL) {
4811 type = OP_ENTEREVAL;
4812 flags |= OPpEVAL_BYTES<<8;
4815 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4816 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4817 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4818 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4819 || type == OP_SASSIGN
4820 || type == OP_ENTERTRY
4821 || type == OP_CUSTOM
4822 || type == OP_NULL );
4825 first = newOP(OP_STUB, 0);
4826 if (PL_opargs[type] & OA_MARK)
4827 first = force_list(first, 1);
4829 NewOp(1101, unop, 1, UNOP);
4830 OpTYPE_set(unop, type);
4831 unop->op_first = first;
4832 unop->op_flags = (U8)(flags | OPf_KIDS);
4833 unop->op_private = (U8)(1 | (flags >> 8));
4835 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
4836 OpLASTSIB_set(first, (OP*)unop);
4838 unop = (UNOP*) CHECKOP(type, unop);
4842 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4846 =for apidoc newUNOP_AUX