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 /* lastsib == 1, op_sibling == 0 implies a solitary unattached op */
302 assert(!o->op_sibling);
309 #ifdef PERL_DEBUG_READONLY_OPS
311 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
313 PERL_ARGS_ASSERT_SLAB_TO_RO;
315 if (slab->opslab_readonly) return;
316 slab->opslab_readonly = 1;
317 for (; slab; slab = slab->opslab_next) {
318 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
319 (unsigned long) slab->opslab_size, slab));*/
320 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
321 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
322 (unsigned long)slab->opslab_size, errno);
327 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
331 PERL_ARGS_ASSERT_SLAB_TO_RW;
333 if (!slab->opslab_readonly) return;
335 for (; slab2; slab2 = slab2->opslab_next) {
336 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
337 (unsigned long) size, slab2));*/
338 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
339 PROT_READ|PROT_WRITE)) {
340 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
341 (unsigned long)slab2->opslab_size, errno);
344 slab->opslab_readonly = 0;
348 # define Slab_to_rw(op) NOOP
351 /* This cannot possibly be right, but it was copied from the old slab
352 allocator, to which it was originally added, without explanation, in
355 # define PerlMemShared PerlMem
359 Perl_Slab_Free(pTHX_ void *op)
361 OP * const o = (OP *)op;
364 PERL_ARGS_ASSERT_SLAB_FREE;
366 if (!o->op_slabbed) {
368 PerlMemShared_free(op);
373 /* If this op is already freed, our refcount will get screwy. */
374 assert(o->op_type != OP_FREED);
375 o->op_type = OP_FREED;
376 o->op_next = slab->opslab_freed;
377 slab->opslab_freed = o;
378 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
379 OpslabREFCNT_dec_padok(slab);
383 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
385 const bool havepad = !!PL_comppad;
386 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
389 PAD_SAVE_SETNULLPAD();
396 Perl_opslab_free(pTHX_ OPSLAB *slab)
399 PERL_ARGS_ASSERT_OPSLAB_FREE;
401 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
402 assert(slab->opslab_refcnt == 1);
403 for (; slab; slab = slab2) {
404 slab2 = slab->opslab_next;
406 slab->opslab_refcnt = ~(size_t)0;
408 #ifdef PERL_DEBUG_READONLY_OPS
409 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
411 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
412 perror("munmap failed");
416 PerlMemShared_free(slab);
422 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
427 size_t savestack_count = 0;
429 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
432 for (slot = slab2->opslab_first;
434 slot = slot->opslot_next) {
435 if (slot->opslot_op.op_type != OP_FREED
436 && !(slot->opslot_op.op_savefree
442 assert(slot->opslot_op.op_slabbed);
443 op_free(&slot->opslot_op);
444 if (slab->opslab_refcnt == 1) goto free;
447 } while ((slab2 = slab2->opslab_next));
448 /* > 1 because the CV still holds a reference count. */
449 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
451 assert(savestack_count == slab->opslab_refcnt-1);
453 /* Remove the CV’s reference count. */
454 slab->opslab_refcnt--;
461 #ifdef PERL_DEBUG_READONLY_OPS
463 Perl_op_refcnt_inc(pTHX_ OP *o)
466 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
467 if (slab && slab->opslab_readonly) {
480 Perl_op_refcnt_dec(pTHX_ OP *o)
483 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
485 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
487 if (slab && slab->opslab_readonly) {
489 result = --o->op_targ;
492 result = --o->op_targ;
498 * In the following definition, the ", (OP*)0" is just to make the compiler
499 * think the expression is of the right type: croak actually does a Siglongjmp.
501 #define CHECKOP(type,o) \
502 ((PL_op_mask && PL_op_mask[type]) \
503 ? ( op_free((OP*)o), \
504 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
506 : PL_check[type](aTHX_ (OP*)o))
508 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
510 #define CHANGE_TYPE(o,type) \
512 o->op_type = (OPCODE)type; \
513 o->op_ppaddr = PL_ppaddr[type]; \
517 S_no_fh_allowed(pTHX_ OP *o)
519 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
521 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
527 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
529 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
530 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
535 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
537 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
539 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
544 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
546 PERL_ARGS_ASSERT_BAD_TYPE_PV;
548 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
549 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
552 /* remove flags var, its unused in all callers, move to to right end since gv
553 and kid are always the same */
555 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
557 SV * const namesv = cv_name((CV *)gv, NULL, 0);
558 PERL_ARGS_ASSERT_BAD_TYPE_GV;
560 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
561 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
565 S_no_bareword_allowed(pTHX_ OP *o)
567 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
569 qerror(Perl_mess(aTHX_
570 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
572 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
575 /* "register" allocation */
578 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
581 const bool is_our = (PL_parser->in_my == KEY_our);
583 PERL_ARGS_ASSERT_ALLOCMY;
585 if (flags & ~SVf_UTF8)
586 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
589 /* complain about "my $<special_var>" etc etc */
593 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
594 (name[1] == '_' && (*name == '$' || len > 2))))
596 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
598 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
599 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
600 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
601 PL_parser->in_my == KEY_state ? "state" : "my"));
603 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
604 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
607 else if (len == 2 && name[1] == '_' && !is_our)
608 /* diag_listed_as: Use of my $_ is experimental */
609 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
610 "Use of %s $_ is experimental",
611 PL_parser->in_my == KEY_state
615 /* allocate a spare slot and store the name in that slot */
617 off = pad_add_name_pvn(name, len,
618 (is_our ? padadd_OUR :
619 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
620 PL_parser->in_my_stash,
622 /* $_ is always in main::, even with our */
623 ? (PL_curstash && !memEQs(name,len,"$_")
629 /* anon sub prototypes contains state vars should always be cloned,
630 * otherwise the state var would be shared between anon subs */
632 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
633 CvCLONE_on(PL_compcv);
639 =head1 Optree Manipulation Functions
641 =for apidoc alloccopstash
643 Available only under threaded builds, this function allocates an entry in
644 C<PL_stashpad> for the stash passed to it.
651 Perl_alloccopstash(pTHX_ HV *hv)
653 PADOFFSET off = 0, o = 1;
654 bool found_slot = FALSE;
656 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
658 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
660 for (; o < PL_stashpadmax; ++o) {
661 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
662 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
663 found_slot = TRUE, off = o;
666 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
667 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
668 off = PL_stashpadmax;
669 PL_stashpadmax += 10;
672 PL_stashpad[PL_stashpadix = off] = hv;
677 /* free the body of an op without examining its contents.
678 * Always use this rather than FreeOp directly */
681 S_op_destroy(pTHX_ OP *o)
689 =for apidoc Am|void|op_free|OP *o
691 Free an op. Only use this when an op is no longer linked to from any
698 Perl_op_free(pTHX_ OP *o)
702 SSize_t defer_ix = -1;
703 SSize_t defer_stack_alloc = 0;
704 OP **defer_stack = NULL;
708 /* Though ops may be freed twice, freeing the op after its slab is a
710 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
711 /* During the forced freeing of ops after compilation failure, kidops
712 may be freed before their parents. */
713 if (!o || o->op_type == OP_FREED)
718 /* an op should only ever acquire op_private flags that we know about.
719 * If this fails, you may need to fix something in regen/op_private */
720 if (o->op_ppaddr == PL_ppaddr[o->op_type]) {
721 assert(!(o->op_private & ~PL_op_private_valid[type]));
724 if (o->op_private & OPpREFCOUNTED) {
735 refcnt = OpREFCNT_dec(o);
738 /* Need to find and remove any pattern match ops from the list
739 we maintain for reset(). */
740 find_and_forget_pmops(o);
750 /* Call the op_free hook if it has been set. Do it now so that it's called
751 * at the right time for refcounted ops, but still before all of the kids
755 if (o->op_flags & OPf_KIDS) {
757 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
758 nextkid = OpSIBLING(kid); /* Get before next freeing kid */
759 if (!kid || kid->op_type == OP_FREED)
760 /* During the forced freeing of ops after
761 compilation failure, kidops may be freed before
764 if (!(kid->op_flags & OPf_KIDS))
765 /* If it has no kids, just free it now */
772 type = (OPCODE)o->op_targ;
775 Slab_to_rw(OpSLAB(o));
777 /* COP* is not cleared by op_clear() so that we may track line
778 * numbers etc even after null() */
779 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
785 #ifdef DEBUG_LEAKING_SCALARS
789 } while ( (o = POP_DEFERRED_OP()) );
791 Safefree(defer_stack);
794 /* S_op_clear_gv(): free a GV attached to an OP */
797 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
799 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
803 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
804 || o->op_type == OP_MULTIDEREF)
807 ? ((GV*)PAD_SVl(*ixp)) : NULL;
809 ? (GV*)(*svp) : NULL;
811 /* It's possible during global destruction that the GV is freed
812 before the optree. Whilst the SvREFCNT_inc is happy to bump from
813 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
814 will trigger an assertion failure, because the entry to sv_clear
815 checks that the scalar is not already freed. A check of for
816 !SvIS_FREED(gv) turns out to be invalid, because during global
817 destruction the reference count can be forced down to zero
818 (with SVf_BREAK set). In which case raising to 1 and then
819 dropping to 0 triggers cleanup before it should happen. I
820 *think* that this might actually be a general, systematic,
821 weakness of the whole idea of SVf_BREAK, in that code *is*
822 allowed to raise and lower references during global destruction,
823 so any *valid* code that happens to do this during global
824 destruction might well trigger premature cleanup. */
825 bool still_valid = gv && SvREFCNT(gv);
828 SvREFCNT_inc_simple_void(gv);
831 pad_swipe(*ixp, TRUE);
839 int try_downgrade = SvREFCNT(gv) == 2;
842 gv_try_downgrade(gv);
848 Perl_op_clear(pTHX_ OP *o)
853 PERL_ARGS_ASSERT_OP_CLEAR;
855 switch (o->op_type) {
856 case OP_NULL: /* Was holding old type, if any. */
859 case OP_ENTEREVAL: /* Was holding hints. */
863 if (!(o->op_flags & OPf_REF)
864 || (PL_check[o->op_type] != Perl_ck_ftst))
871 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
873 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
876 case OP_METHOD_REDIR:
877 case OP_METHOD_REDIR_SUPER:
879 if (cMETHOPx(o)->op_rclass_targ) {
880 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
881 cMETHOPx(o)->op_rclass_targ = 0;
884 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
885 cMETHOPx(o)->op_rclass_sv = NULL;
887 case OP_METHOD_NAMED:
888 case OP_METHOD_SUPER:
889 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
890 cMETHOPx(o)->op_u.op_meth_sv = NULL;
893 pad_swipe(o->op_targ, 1);
900 SvREFCNT_dec(cSVOPo->op_sv);
901 cSVOPo->op_sv = NULL;
904 Even if op_clear does a pad_free for the target of the op,
905 pad_free doesn't actually remove the sv that exists in the pad;
906 instead it lives on. This results in that it could be reused as
907 a target later on when the pad was reallocated.
910 pad_swipe(o->op_targ,1);
920 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
925 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
926 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
928 if (cPADOPo->op_padix > 0) {
929 pad_swipe(cPADOPo->op_padix, TRUE);
930 cPADOPo->op_padix = 0;
933 SvREFCNT_dec(cSVOPo->op_sv);
934 cSVOPo->op_sv = NULL;
938 PerlMemShared_free(cPVOPo->op_pv);
939 cPVOPo->op_pv = NULL;
943 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
947 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
948 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
951 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
957 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
958 op_free(cPMOPo->op_code_list);
959 cPMOPo->op_code_list = NULL;
961 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
962 /* we use the same protection as the "SAFE" version of the PM_ macros
963 * here since sv_clean_all might release some PMOPs
964 * after PL_regex_padav has been cleared
965 * and the clearing of PL_regex_padav needs to
966 * happen before sv_clean_all
969 if(PL_regex_pad) { /* We could be in destruction */
970 const IV offset = (cPMOPo)->op_pmoffset;
971 ReREFCNT_dec(PM_GETRE(cPMOPo));
972 PL_regex_pad[offset] = &PL_sv_undef;
973 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
977 ReREFCNT_dec(PM_GETRE(cPMOPo));
978 PM_SETRE(cPMOPo, NULL);
985 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
986 UV actions = items->uv;
988 bool is_hash = FALSE;
991 switch (actions & MDEREF_ACTION_MASK) {
994 actions = (++items)->uv;
997 case MDEREF_HV_padhv_helem:
999 case MDEREF_AV_padav_aelem:
1000 pad_free((++items)->pad_offset);
1003 case MDEREF_HV_gvhv_helem:
1005 case MDEREF_AV_gvav_aelem:
1007 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1009 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1013 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1015 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1017 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1019 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1021 goto do_vivify_rv2xv_elem;
1023 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1025 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1026 pad_free((++items)->pad_offset);
1027 goto do_vivify_rv2xv_elem;
1029 case MDEREF_HV_pop_rv2hv_helem:
1030 case MDEREF_HV_vivify_rv2hv_helem:
1032 do_vivify_rv2xv_elem:
1033 case MDEREF_AV_pop_rv2av_aelem:
1034 case MDEREF_AV_vivify_rv2av_aelem:
1036 switch (actions & MDEREF_INDEX_MASK) {
1037 case MDEREF_INDEX_none:
1040 case MDEREF_INDEX_const:
1044 pad_swipe((++items)->pad_offset, 1);
1046 SvREFCNT_dec((++items)->sv);
1052 case MDEREF_INDEX_padsv:
1053 pad_free((++items)->pad_offset);
1055 case MDEREF_INDEX_gvsv:
1057 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1059 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1064 if (actions & MDEREF_FLAG_last)
1077 actions >>= MDEREF_SHIFT;
1080 /* start of malloc is at op_aux[-1], where the length is
1082 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1087 if (o->op_targ > 0) {
1088 pad_free(o->op_targ);
1094 S_cop_free(pTHX_ COP* cop)
1096 PERL_ARGS_ASSERT_COP_FREE;
1099 if (! specialWARN(cop->cop_warnings))
1100 PerlMemShared_free(cop->cop_warnings);
1101 cophh_free(CopHINTHASH_get(cop));
1102 if (PL_curcop == cop)
1107 S_forget_pmop(pTHX_ PMOP *const o
1110 HV * const pmstash = PmopSTASH(o);
1112 PERL_ARGS_ASSERT_FORGET_PMOP;
1114 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1115 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1117 PMOP **const array = (PMOP**) mg->mg_ptr;
1118 U32 count = mg->mg_len / sizeof(PMOP**);
1122 if (array[i] == o) {
1123 /* Found it. Move the entry at the end to overwrite it. */
1124 array[i] = array[--count];
1125 mg->mg_len = count * sizeof(PMOP**);
1126 /* Could realloc smaller at this point always, but probably
1127 not worth it. Probably worth free()ing if we're the
1130 Safefree(mg->mg_ptr);
1143 S_find_and_forget_pmops(pTHX_ OP *o)
1145 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1147 if (o->op_flags & OPf_KIDS) {
1148 OP *kid = cUNOPo->op_first;
1150 switch (kid->op_type) {
1155 forget_pmop((PMOP*)kid);
1157 find_and_forget_pmops(kid);
1158 kid = OpSIBLING(kid);
1164 =for apidoc Am|void|op_null|OP *o
1166 Neutralizes an op when it is no longer needed, but is still linked to from
1173 Perl_op_null(pTHX_ OP *o)
1177 PERL_ARGS_ASSERT_OP_NULL;
1179 if (o->op_type == OP_NULL)
1182 o->op_targ = o->op_type;
1183 CHANGE_TYPE(o, OP_NULL);
1187 Perl_op_refcnt_lock(pTHX)
1192 PERL_UNUSED_CONTEXT;
1197 Perl_op_refcnt_unlock(pTHX)
1202 PERL_UNUSED_CONTEXT;
1208 =for apidoc op_sibling_splice
1210 A general function for editing the structure of an existing chain of
1211 op_sibling nodes. By analogy with the perl-level splice() function, allows
1212 you to delete zero or more sequential nodes, replacing them with zero or
1213 more different nodes. Performs the necessary op_first/op_last
1214 housekeeping on the parent node and op_sibling manipulation on the
1215 children. The last deleted node will be marked as as the last node by
1216 updating the op_sibling or op_lastsib field as appropriate.
1218 Note that op_next is not manipulated, and nodes are not freed; that is the
1219 responsibility of the caller. It also won't create a new list op for an
1220 empty list etc; use higher-level functions like op_append_elem() for that.
1222 parent is the parent node of the sibling chain.
1224 start is the node preceding the first node to be spliced. Node(s)
1225 following it will be deleted, and ops will be inserted after it. If it is
1226 NULL, the first node onwards is deleted, and nodes are inserted at the
1229 del_count is the number of nodes to delete. If zero, no nodes are deleted.
1230 If -1 or greater than or equal to the number of remaining kids, all
1231 remaining kids are deleted.
1233 insert is the first of a chain of nodes to be inserted in place of the nodes.
1234 If NULL, no nodes are inserted.
1236 The head of the chain of deleted ops is returned, or NULL if no ops were
1241 action before after returns
1242 ------ ----- ----- -------
1245 splice(P, A, 2, X-Y-Z) | | B-C
1249 splice(P, NULL, 1, X-Y) | | A
1253 splice(P, NULL, 3, NULL) | | A-B-C
1257 splice(P, B, 0, X-Y) | | NULL
1264 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1266 OP *first = start ? OpSIBLING(start) : cLISTOPx(parent)->op_first;
1268 OP *last_del = NULL;
1269 OP *last_ins = NULL;
1271 PERL_ARGS_ASSERT_OP_SIBLING_SPLICE;
1273 assert(del_count >= -1);
1275 if (del_count && first) {
1277 while (--del_count && OpHAS_SIBLING(last_del))
1278 last_del = OpSIBLING(last_del);
1279 rest = OpSIBLING(last_del);
1280 OpSIBLING_set(last_del, NULL);
1281 last_del->op_lastsib = 1;
1288 while (OpHAS_SIBLING(last_ins))
1289 last_ins = OpSIBLING(last_ins);
1290 OpSIBLING_set(last_ins, rest);
1291 last_ins->op_lastsib = rest ? 0 : 1;
1297 OpSIBLING_set(start, insert);
1298 start->op_lastsib = insert ? 0 : 1;
1301 cLISTOPx(parent)->op_first = insert;
1303 parent->op_flags |= OPf_KIDS;
1305 parent->op_flags &= ~OPf_KIDS;
1309 /* update op_last etc */
1310 U32 type = parent->op_type;
1313 if (type == OP_NULL)
1314 type = parent->op_targ;
1315 type = PL_opargs[type] & OA_CLASS_MASK;
1317 lastop = last_ins ? last_ins : start ? start : NULL;
1318 if ( type == OA_BINOP
1319 || type == OA_LISTOP
1323 cLISTOPx(parent)->op_last = lastop;
1326 lastop->op_lastsib = 1;
1327 #ifdef PERL_OP_PARENT
1328 lastop->op_sibling = parent;
1332 return last_del ? first : NULL;
1336 =for apidoc op_parent
1338 returns the parent OP of o, if it has a parent. Returns NULL otherwise.
1339 (Currently perl must be built with C<-DPERL_OP_PARENT> for this feature to
1346 Perl_op_parent(OP *o)
1348 PERL_ARGS_ASSERT_OP_PARENT;
1349 #ifdef PERL_OP_PARENT
1350 while (OpHAS_SIBLING(o))
1352 return o->op_sibling;
1360 /* replace the sibling following start with a new UNOP, which becomes
1361 * the parent of the original sibling; e.g.
1363 * op_sibling_newUNOP(P, A, unop-args...)
1371 * where U is the new UNOP.
1373 * parent and start args are the same as for op_sibling_splice();
1374 * type and flags args are as newUNOP().
1376 * Returns the new UNOP.
1380 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1384 kid = op_sibling_splice(parent, start, 1, NULL);
1385 newop = newUNOP(type, flags, kid);
1386 op_sibling_splice(parent, start, 0, newop);
1391 /* lowest-level newLOGOP-style function - just allocates and populates
1392 * the struct. Higher-level stuff should be done by S_new_logop() /
1393 * newLOGOP(). This function exists mainly to avoid op_first assignment
1394 * being spread throughout this file.
1398 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1403 NewOp(1101, logop, 1, LOGOP);
1404 CHANGE_TYPE(logop, type);
1405 logop->op_first = first;
1406 logop->op_other = other;
1407 logop->op_flags = OPf_KIDS;
1408 while (kid && OpHAS_SIBLING(kid))
1409 kid = OpSIBLING(kid);
1411 kid->op_lastsib = 1;
1412 #ifdef PERL_OP_PARENT
1413 kid->op_sibling = (OP*)logop;
1420 /* Contextualizers */
1423 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1425 Applies a syntactic context to an op tree representing an expression.
1426 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1427 or C<G_VOID> to specify the context to apply. The modified op tree
1434 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1436 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1438 case G_SCALAR: return scalar(o);
1439 case G_ARRAY: return list(o);
1440 case G_VOID: return scalarvoid(o);
1442 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1449 =for apidoc Am|OP*|op_linklist|OP *o
1450 This function is the implementation of the L</LINKLIST> macro. It should
1451 not be called directly.
1457 Perl_op_linklist(pTHX_ OP *o)
1461 PERL_ARGS_ASSERT_OP_LINKLIST;
1466 /* establish postfix order */
1467 first = cUNOPo->op_first;
1470 o->op_next = LINKLIST(first);
1473 OP *sibl = OpSIBLING(kid);
1475 kid->op_next = LINKLIST(sibl);
1490 S_scalarkids(pTHX_ OP *o)
1492 if (o && o->op_flags & OPf_KIDS) {
1494 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1501 S_scalarboolean(pTHX_ OP *o)
1503 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1505 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1506 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1507 if (ckWARN(WARN_SYNTAX)) {
1508 const line_t oldline = CopLINE(PL_curcop);
1510 if (PL_parser && PL_parser->copline != NOLINE) {
1511 /* This ensures that warnings are reported at the first line
1512 of the conditional, not the last. */
1513 CopLINE_set(PL_curcop, PL_parser->copline);
1515 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1516 CopLINE_set(PL_curcop, oldline);
1523 S_op_varname(pTHX_ const OP *o)
1526 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1527 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1529 const char funny = o->op_type == OP_PADAV
1530 || o->op_type == OP_RV2AV ? '@' : '%';
1531 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1533 if (cUNOPo->op_first->op_type != OP_GV
1534 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1536 return varname(gv, funny, 0, NULL, 0, 1);
1539 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1544 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1545 { /* or not so pretty :-) */
1546 if (o->op_type == OP_CONST) {
1548 if (SvPOK(*retsv)) {
1550 *retsv = sv_newmortal();
1551 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1552 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1554 else if (!SvOK(*retsv))
1557 else *retpv = "...";
1561 S_scalar_slice_warning(pTHX_ const OP *o)
1565 o->op_type == OP_HSLICE ? '{' : '[';
1567 o->op_type == OP_HSLICE ? '}' : ']';
1569 SV *keysv = NULL; /* just to silence compiler warnings */
1570 const char *key = NULL;
1572 if (!(o->op_private & OPpSLICEWARNING))
1574 if (PL_parser && PL_parser->error_count)
1575 /* This warning can be nonsensical when there is a syntax error. */
1578 kid = cLISTOPo->op_first;
1579 kid = OpSIBLING(kid); /* get past pushmark */
1580 /* weed out false positives: any ops that can return lists */
1581 switch (kid->op_type) {
1610 /* Don't warn if we have a nulled list either. */
1611 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1614 assert(OpSIBLING(kid));
1615 name = S_op_varname(aTHX_ OpSIBLING(kid));
1616 if (!name) /* XS module fiddling with the op tree */
1618 S_op_pretty(aTHX_ kid, &keysv, &key);
1619 assert(SvPOK(name));
1620 sv_chop(name,SvPVX(name)+1);
1622 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1623 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1624 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1626 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1627 lbrack, key, rbrack);
1629 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1630 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1631 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1633 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1634 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1638 Perl_scalar(pTHX_ OP *o)
1642 /* assumes no premature commitment */
1643 if (!o || (PL_parser && PL_parser->error_count)
1644 || (o->op_flags & OPf_WANT)
1645 || o->op_type == OP_RETURN)
1650 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1652 switch (o->op_type) {
1654 scalar(cBINOPo->op_first);
1655 if (o->op_private & OPpREPEAT_DOLIST) {
1656 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1657 assert(kid->op_type == OP_PUSHMARK);
1658 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1659 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1660 o->op_private &=~ OPpREPEAT_DOLIST;
1667 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
1677 if (o->op_flags & OPf_KIDS) {
1678 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
1684 kid = cLISTOPo->op_first;
1686 kid = OpSIBLING(kid);
1689 OP *sib = OpSIBLING(kid);
1690 if (sib && kid->op_type != OP_LEAVEWHEN
1691 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
1692 || ( sib->op_targ != OP_NEXTSTATE
1693 && sib->op_targ != OP_DBSTATE )))
1699 PL_curcop = &PL_compiling;
1704 kid = cLISTOPo->op_first;
1707 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1712 /* Warn about scalar context */
1713 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1714 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1717 const char *key = NULL;
1719 /* This warning can be nonsensical when there is a syntax error. */
1720 if (PL_parser && PL_parser->error_count)
1723 if (!ckWARN(WARN_SYNTAX)) break;
1725 kid = cLISTOPo->op_first;
1726 kid = OpSIBLING(kid); /* get past pushmark */
1727 assert(OpSIBLING(kid));
1728 name = S_op_varname(aTHX_ OpSIBLING(kid));
1729 if (!name) /* XS module fiddling with the op tree */
1731 S_op_pretty(aTHX_ kid, &keysv, &key);
1732 assert(SvPOK(name));
1733 sv_chop(name,SvPVX(name)+1);
1735 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1736 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1737 "%%%"SVf"%c%s%c in scalar context better written "
1739 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1740 lbrack, key, rbrack);
1742 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1743 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1744 "%%%"SVf"%c%"SVf"%c in scalar context better "
1745 "written as $%"SVf"%c%"SVf"%c",
1746 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1747 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1754 Perl_scalarvoid(pTHX_ OP *arg)
1760 SSize_t defer_stack_alloc = 0;
1761 SSize_t defer_ix = -1;
1762 OP **defer_stack = NULL;
1765 PERL_ARGS_ASSERT_SCALARVOID;
1768 SV *useless_sv = NULL;
1769 const char* useless = NULL;
1771 if (o->op_type == OP_NEXTSTATE
1772 || o->op_type == OP_DBSTATE
1773 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1774 || o->op_targ == OP_DBSTATE)))
1775 PL_curcop = (COP*)o; /* for warning below */
1777 /* assumes no premature commitment */
1778 want = o->op_flags & OPf_WANT;
1779 if ((want && want != OPf_WANT_SCALAR)
1780 || (PL_parser && PL_parser->error_count)
1781 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1786 if ((o->op_private & OPpTARGET_MY)
1787 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1789 /* newASSIGNOP has already applied scalar context, which we
1790 leave, as if this op is inside SASSIGN. */
1794 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1796 switch (o->op_type) {
1798 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1802 if (o->op_flags & OPf_STACKED)
1804 if (o->op_type == OP_REPEAT)
1805 scalar(cBINOPo->op_first);
1808 if (o->op_private == 4)
1843 case OP_GETSOCKNAME:
1844 case OP_GETPEERNAME:
1849 case OP_GETPRIORITY:
1874 useless = OP_DESC(o);
1884 case OP_AELEMFAST_LEX:
1888 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1889 /* Otherwise it's "Useless use of grep iterator" */
1890 useless = OP_DESC(o);
1894 kid = cLISTOPo->op_first;
1895 if (kid && kid->op_type == OP_PUSHRE
1897 && !(o->op_flags & OPf_STACKED)
1899 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
1901 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
1904 useless = OP_DESC(o);
1908 kid = cUNOPo->op_first;
1909 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1910 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1913 useless = "negative pattern binding (!~)";
1917 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1918 useless = "non-destructive substitution (s///r)";
1922 useless = "non-destructive transliteration (tr///r)";
1929 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1930 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
1931 useless = "a variable";
1936 if (cSVOPo->op_private & OPpCONST_STRICT)
1937 no_bareword_allowed(o);
1939 if (ckWARN(WARN_VOID)) {
1941 /* don't warn on optimised away booleans, eg
1942 * use constant Foo, 5; Foo || print; */
1943 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1945 /* the constants 0 and 1 are permitted as they are
1946 conventionally used as dummies in constructs like
1947 1 while some_condition_with_side_effects; */
1948 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
1950 else if (SvPOK(sv)) {
1951 SV * const dsv = newSVpvs("");
1953 = Perl_newSVpvf(aTHX_
1955 pv_pretty(dsv, SvPVX_const(sv),
1956 SvCUR(sv), 32, NULL, NULL,
1958 | PERL_PV_ESCAPE_NOCLEAR
1959 | PERL_PV_ESCAPE_UNI_DETECT));
1960 SvREFCNT_dec_NN(dsv);
1962 else if (SvOK(sv)) {
1963 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
1966 useless = "a constant (undef)";
1969 op_null(o); /* don't execute or even remember it */
1973 CHANGE_TYPE(o, OP_PREINC); /* pre-increment is faster */
1977 CHANGE_TYPE(o, OP_PREDEC); /* pre-decrement is faster */
1981 CHANGE_TYPE(o, OP_I_PREINC); /* pre-increment is faster */
1985 CHANGE_TYPE(o, OP_I_PREDEC); /* pre-decrement is faster */
1990 UNOP *refgen, *rv2cv;
1993 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1996 rv2gv = ((BINOP *)o)->op_last;
1997 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2000 refgen = (UNOP *)((BINOP *)o)->op_first;
2002 if (!refgen || (refgen->op_type != OP_REFGEN
2003 && refgen->op_type != OP_SREFGEN))
2006 exlist = (LISTOP *)refgen->op_first;
2007 if (!exlist || exlist->op_type != OP_NULL
2008 || exlist->op_targ != OP_LIST)
2011 if (exlist->op_first->op_type != OP_PUSHMARK
2012 && exlist->op_first != exlist->op_last)
2015 rv2cv = (UNOP*)exlist->op_last;
2017 if (rv2cv->op_type != OP_RV2CV)
2020 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2021 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2022 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2024 o->op_private |= OPpASSIGN_CV_TO_GV;
2025 rv2gv->op_private |= OPpDONT_INIT_GV;
2026 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2038 kid = cLOGOPo->op_first;
2039 if (kid->op_type == OP_NOT
2040 && (kid->op_flags & OPf_KIDS)) {
2041 if (o->op_type == OP_AND) {
2042 CHANGE_TYPE(o, OP_OR);
2044 CHANGE_TYPE(o, OP_AND);
2054 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2055 if (!(kid->op_flags & OPf_KIDS))
2062 if (o->op_flags & OPf_STACKED)
2069 if (!(o->op_flags & OPf_KIDS))
2080 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2081 if (!(kid->op_flags & OPf_KIDS))
2087 /* If the first kid after pushmark is something that the padrange
2088 optimisation would reject, then null the list and the pushmark.
2090 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2091 && ( !(kid = OpSIBLING(kid))
2092 || ( kid->op_type != OP_PADSV
2093 && kid->op_type != OP_PADAV
2094 && kid->op_type != OP_PADHV)
2095 || kid->op_private & ~OPpLVAL_INTRO
2096 || !(kid = OpSIBLING(kid))
2097 || ( kid->op_type != OP_PADSV
2098 && kid->op_type != OP_PADAV
2099 && kid->op_type != OP_PADHV)
2100 || kid->op_private & ~OPpLVAL_INTRO)
2102 op_null(cUNOPo->op_first); /* NULL the pushmark */
2103 op_null(o); /* NULL the list */
2115 /* mortalise it, in case warnings are fatal. */
2116 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2117 "Useless use of %"SVf" in void context",
2118 SVfARG(sv_2mortal(useless_sv)));
2121 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2122 "Useless use of %s in void context",
2125 } while ( (o = POP_DEFERRED_OP()) );
2127 Safefree(defer_stack);
2133 S_listkids(pTHX_ OP *o)
2135 if (o && o->op_flags & OPf_KIDS) {
2137 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2144 Perl_list(pTHX_ OP *o)
2148 /* assumes no premature commitment */
2149 if (!o || (o->op_flags & OPf_WANT)
2150 || (PL_parser && PL_parser->error_count)
2151 || o->op_type == OP_RETURN)
2156 if ((o->op_private & OPpTARGET_MY)
2157 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2159 return o; /* As if inside SASSIGN */
2162 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2164 switch (o->op_type) {
2166 list(cBINOPo->op_first);
2169 if (o->op_private & OPpREPEAT_DOLIST
2170 && !(o->op_flags & OPf_STACKED))
2172 list(cBINOPo->op_first);
2173 kid = cBINOPo->op_last;
2174 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2175 && SvIVX(kSVOP_sv) == 1)
2177 op_null(o); /* repeat */
2178 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2180 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2187 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2195 if (!(o->op_flags & OPf_KIDS))
2197 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2198 list(cBINOPo->op_first);
2199 return gen_constant_list(o);
2205 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2206 op_null(cUNOPo->op_first); /* NULL the pushmark */
2207 op_null(o); /* NULL the list */
2212 kid = cLISTOPo->op_first;
2214 kid = OpSIBLING(kid);
2217 OP *sib = OpSIBLING(kid);
2218 if (sib && kid->op_type != OP_LEAVEWHEN)
2224 PL_curcop = &PL_compiling;
2228 kid = cLISTOPo->op_first;
2235 S_scalarseq(pTHX_ OP *o)
2238 const OPCODE type = o->op_type;
2240 if (type == OP_LINESEQ || type == OP_SCOPE ||
2241 type == OP_LEAVE || type == OP_LEAVETRY)
2244 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2245 if ((sib = OpSIBLING(kid))
2246 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2247 || ( sib->op_targ != OP_NEXTSTATE
2248 && sib->op_targ != OP_DBSTATE )))
2253 PL_curcop = &PL_compiling;
2255 o->op_flags &= ~OPf_PARENS;
2256 if (PL_hints & HINT_BLOCK_SCOPE)
2257 o->op_flags |= OPf_PARENS;
2260 o = newOP(OP_STUB, 0);
2265 S_modkids(pTHX_ OP *o, I32 type)
2267 if (o && o->op_flags & OPf_KIDS) {
2269 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2270 op_lvalue(kid, type);
2276 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2277 * const fields. Also, convert CONST keys to HEK-in-SVs.
2278 * rop is the op that retrieves the hash;
2279 * key_op is the first key
2283 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op)
2289 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2291 if (rop->op_first->op_type == OP_PADSV)
2292 /* @$hash{qw(keys here)} */
2293 rop = (UNOP*)rop->op_first;
2295 /* @{$hash}{qw(keys here)} */
2296 if (rop->op_first->op_type == OP_SCOPE
2297 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2299 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2306 lexname = NULL; /* just to silence compiler warnings */
2307 fields = NULL; /* just to silence compiler warnings */
2311 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2312 SvPAD_TYPED(lexname))
2313 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2314 && isGV(*fields) && GvHV(*fields);
2316 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2318 if (key_op->op_type != OP_CONST)
2320 svp = cSVOPx_svp(key_op);
2322 /* Make the CONST have a shared SV */
2323 if ( !SvIsCOW_shared_hash(sv = *svp)
2324 && SvTYPE(sv) < SVt_PVMG
2329 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2330 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2331 SvREFCNT_dec_NN(sv);
2336 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2338 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2339 "in variable %"PNf" of type %"HEKf,
2340 SVfARG(*svp), PNfARG(lexname),
2341 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2348 =for apidoc finalize_optree
2350 This function finalizes the optree. Should be called directly after
2351 the complete optree is built. It does some additional
2352 checking which can't be done in the normal ck_xxx functions and makes
2353 the tree thread-safe.
2358 Perl_finalize_optree(pTHX_ OP* o)
2360 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2363 SAVEVPTR(PL_curcop);
2371 /* Relocate sv to the pad for thread safety.
2372 * Despite being a "constant", the SV is written to,
2373 * for reference counts, sv_upgrade() etc. */
2374 PERL_STATIC_INLINE void
2375 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2378 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2380 ix = pad_alloc(OP_CONST, SVf_READONLY);
2381 SvREFCNT_dec(PAD_SVl(ix));
2382 PAD_SETSV(ix, *svp);
2383 /* XXX I don't know how this isn't readonly already. */
2384 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2392 S_finalize_op(pTHX_ OP* o)
2394 PERL_ARGS_ASSERT_FINALIZE_OP;
2397 switch (o->op_type) {
2400 PL_curcop = ((COP*)o); /* for warnings */
2403 if (OpHAS_SIBLING(o)) {
2404 OP *sib = OpSIBLING(o);
2405 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2406 && ckWARN(WARN_EXEC)
2407 && OpHAS_SIBLING(sib))
2409 const OPCODE type = OpSIBLING(sib)->op_type;
2410 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2411 const line_t oldline = CopLINE(PL_curcop);
2412 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2413 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2414 "Statement unlikely to be reached");
2415 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2416 "\t(Maybe you meant system() when you said exec()?)\n");
2417 CopLINE_set(PL_curcop, oldline);
2424 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2425 GV * const gv = cGVOPo_gv;
2426 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2427 /* XXX could check prototype here instead of just carping */
2428 SV * const sv = sv_newmortal();
2429 gv_efullname3(sv, gv, NULL);
2430 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2431 "%"SVf"() called too early to check prototype",
2438 if (cSVOPo->op_private & OPpCONST_STRICT)
2439 no_bareword_allowed(o);
2443 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2448 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2449 case OP_METHOD_NAMED:
2450 case OP_METHOD_SUPER:
2451 case OP_METHOD_REDIR:
2452 case OP_METHOD_REDIR_SUPER:
2453 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2462 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2465 rop = (UNOP*)((BINOP*)o)->op_first;
2470 S_scalar_slice_warning(aTHX_ o);
2474 kid = OpSIBLING(cLISTOPo->op_first);
2475 if (/* I bet there's always a pushmark... */
2476 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2477 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2482 key_op = (SVOP*)(kid->op_type == OP_CONST
2484 : OpSIBLING(kLISTOP->op_first));
2486 rop = (UNOP*)((LISTOP*)o)->op_last;
2489 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2491 S_check_hash_fields_and_hekify(aTHX_ rop, key_op);
2495 S_scalar_slice_warning(aTHX_ o);
2499 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2500 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2507 if (o->op_flags & OPf_KIDS) {
2511 /* check that op_last points to the last sibling, and that
2512 * the last op_sibling field points back to the parent, and
2513 * that the only ops with KIDS are those which are entitled to
2515 U32 type = o->op_type;
2519 if (type == OP_NULL) {
2521 /* ck_glob creates a null UNOP with ex-type GLOB
2522 * (which is a list op. So pretend it wasn't a listop */
2523 if (type == OP_GLOB)
2526 family = PL_opargs[type] & OA_CLASS_MASK;
2528 has_last = ( family == OA_BINOP
2529 || family == OA_LISTOP
2530 || family == OA_PMOP
2531 || family == OA_LOOP
2533 assert( has_last /* has op_first and op_last, or ...
2534 ... has (or may have) op_first: */
2535 || family == OA_UNOP
2536 || family == OA_UNOP_AUX
2537 || family == OA_LOGOP
2538 || family == OA_BASEOP_OR_UNOP
2539 || family == OA_FILESTATOP
2540 || family == OA_LOOPEXOP
2541 || family == OA_METHOP
2542 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */
2543 || type == OP_SASSIGN
2544 || type == OP_CUSTOM
2545 || type == OP_NULL /* new_logop does this */
2548 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2549 # ifdef PERL_OP_PARENT
2550 if (!OpHAS_SIBLING(kid)) {
2552 assert(kid == cLISTOPo->op_last);
2553 assert(kid->op_sibling == o);
2556 if (OpHAS_SIBLING(kid)) {
2557 assert(!kid->op_lastsib);
2560 assert(kid->op_lastsib);
2562 assert(kid == cLISTOPo->op_last);
2568 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
2574 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2576 Propagate lvalue ("modifiable") context to an op and its children.
2577 I<type> represents the context type, roughly based on the type of op that
2578 would do the modifying, although C<local()> is represented by OP_NULL,
2579 because it has no op type of its own (it is signalled by a flag on
2582 This function detects things that can't be modified, such as C<$x+1>, and
2583 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2584 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2586 It also flags things that need to behave specially in an lvalue context,
2587 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2593 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2596 PadnameLVALUE_on(pn);
2597 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2600 assert(CvPADLIST(cv));
2602 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2603 assert(PadnameLEN(pn));
2604 PadnameLVALUE_on(pn);
2609 S_vivifies(const OPCODE type)
2612 case OP_RV2AV: case OP_ASLICE:
2613 case OP_RV2HV: case OP_KVASLICE:
2614 case OP_RV2SV: case OP_HSLICE:
2615 case OP_AELEMFAST: case OP_KVHSLICE:
2624 S_lvref(pTHX_ OP *o, I32 type)
2628 switch (o->op_type) {
2630 for (kid = OpSIBLING(cUNOPo->op_first); kid;
2631 kid = OpSIBLING(kid))
2632 S_lvref(aTHX_ kid, type);
2637 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2638 o->op_flags |= OPf_STACKED;
2639 if (o->op_flags & OPf_PARENS) {
2640 if (o->op_private & OPpLVAL_INTRO) {
2641 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2642 "localized parenthesized array in list assignment"));
2646 CHANGE_TYPE(o, OP_LVAVREF);
2647 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2648 o->op_flags |= OPf_MOD|OPf_REF;
2651 o->op_private |= OPpLVREF_AV;
2654 kid = cUNOPo->op_first;
2655 if (kid->op_type == OP_NULL)
2656 kid = cUNOPx(kUNOP->op_first->op_sibling)
2658 o->op_private = OPpLVREF_CV;
2659 if (kid->op_type == OP_GV)
2660 o->op_flags |= OPf_STACKED;
2661 else if (kid->op_type == OP_PADCV) {
2662 o->op_targ = kid->op_targ;
2664 op_free(cUNOPo->op_first);
2665 cUNOPo->op_first = NULL;
2666 o->op_flags &=~ OPf_KIDS;
2671 if (o->op_flags & OPf_PARENS) {
2673 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2674 "parenthesized hash in list assignment"));
2677 o->op_private |= OPpLVREF_HV;
2681 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2682 o->op_flags |= OPf_STACKED;
2685 if (o->op_flags & OPf_PARENS) goto parenhash;
2686 o->op_private |= OPpLVREF_HV;
2689 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2692 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2693 if (o->op_flags & OPf_PARENS) goto slurpy;
2694 o->op_private |= OPpLVREF_AV;
2698 o->op_private |= OPpLVREF_ELEM;
2699 o->op_flags |= OPf_STACKED;
2703 CHANGE_TYPE(o, OP_LVREFSLICE);
2704 o->op_private &= OPpLVAL_INTRO|OPpLVREF_ELEM;
2707 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2709 else if (!(o->op_flags & OPf_KIDS))
2711 if (o->op_targ != OP_LIST) {
2712 S_lvref(aTHX_ cBINOPo->op_first, type);
2717 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
2718 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2719 S_lvref(aTHX_ kid, type);
2723 if (o->op_flags & OPf_PARENS)
2728 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2729 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2730 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2735 CHANGE_TYPE(o, OP_LVREF);
2737 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2738 if (type == OP_ENTERLOOP)
2739 o->op_private |= OPpLVREF_ITER;
2743 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2747 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2750 if (!o || (PL_parser && PL_parser->error_count))
2753 if ((o->op_private & OPpTARGET_MY)
2754 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2759 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2761 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2763 switch (o->op_type) {
2768 if ((o->op_flags & OPf_PARENS))
2772 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2773 !(o->op_flags & OPf_STACKED)) {
2774 CHANGE_TYPE(o, OP_RV2CV); /* entersub => rv2cv */
2775 assert(cUNOPo->op_first->op_type == OP_NULL);
2776 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2779 else { /* lvalue subroutine call */
2780 o->op_private |= OPpLVAL_INTRO;
2781 PL_modcount = RETURN_UNLIMITED_NUMBER;
2782 if (type == OP_GREPSTART || type == OP_ENTERSUB
2783 || type == OP_REFGEN || type == OP_LEAVESUBLV) {
2784 /* Potential lvalue context: */
2785 o->op_private |= OPpENTERSUB_INARGS;
2788 else { /* Compile-time error message: */
2789 OP *kid = cUNOPo->op_first;
2793 if (kid->op_type != OP_PUSHMARK) {
2794 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2796 "panic: unexpected lvalue entersub "
2797 "args: type/targ %ld:%"UVuf,
2798 (long)kid->op_type, (UV)kid->op_targ);
2799 kid = kLISTOP->op_first;
2801 while (OpHAS_SIBLING(kid))
2802 kid = OpSIBLING(kid);
2803 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2804 break; /* Postpone until runtime */
2807 kid = kUNOP->op_first;
2808 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2809 kid = kUNOP->op_first;
2810 if (kid->op_type == OP_NULL)
2812 "Unexpected constant lvalue entersub "
2813 "entry via type/targ %ld:%"UVuf,
2814 (long)kid->op_type, (UV)kid->op_targ);
2815 if (kid->op_type != OP_GV) {
2822 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2823 ? MUTABLE_CV(SvRV(gv))
2834 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2835 /* grep, foreach, subcalls, refgen */
2836 if (type == OP_GREPSTART || type == OP_ENTERSUB
2837 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2839 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2840 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2842 : (o->op_type == OP_ENTERSUB
2843 ? "non-lvalue subroutine call"
2845 type ? PL_op_desc[type] : "local"));
2858 case OP_RIGHT_SHIFT:
2867 if (!(o->op_flags & OPf_STACKED))
2873 if (o->op_flags & OPf_STACKED) {
2877 if (!(o->op_private & OPpREPEAT_DOLIST))
2880 const I32 mods = PL_modcount;
2881 modkids(cBINOPo->op_first, type);
2882 if (type != OP_AASSIGN)
2884 kid = cBINOPo->op_last;
2885 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
2886 const IV iv = SvIV(kSVOP_sv);
2887 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
2889 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
2892 PL_modcount = RETURN_UNLIMITED_NUMBER;
2898 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2899 op_lvalue(kid, type);
2904 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2905 PL_modcount = RETURN_UNLIMITED_NUMBER;
2906 return o; /* Treat \(@foo) like ordinary list. */
2910 if (scalar_mod_type(o, type))
2912 ref(cUNOPo->op_first, o->op_type);
2919 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2920 if (type == OP_LEAVESUBLV && (
2921 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2922 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2924 o->op_private |= OPpMAYBE_LVSUB;
2928 PL_modcount = RETURN_UNLIMITED_NUMBER;
2932 if (type == OP_LEAVESUBLV)
2933 o->op_private |= OPpMAYBE_LVSUB;
2936 PL_hints |= HINT_BLOCK_SCOPE;
2937 if (type == OP_LEAVESUBLV)
2938 o->op_private |= OPpMAYBE_LVSUB;
2942 ref(cUNOPo->op_first, o->op_type);
2946 PL_hints |= HINT_BLOCK_SCOPE;
2956 case OP_AELEMFAST_LEX:
2963 PL_modcount = RETURN_UNLIMITED_NUMBER;
2964 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2965 return o; /* Treat \(@foo) like ordinary list. */
2966 if (scalar_mod_type(o, type))
2968 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2969 && type == OP_LEAVESUBLV)
2970 o->op_private |= OPpMAYBE_LVSUB;
2974 if (!type) /* local() */
2975 Perl_croak(aTHX_ "Can't localize lexical variable %"PNf,
2976 PNfARG(PAD_COMPNAME(o->op_targ)));
2977 if (!(o->op_private & OPpLVAL_INTRO)
2978 || ( type != OP_SASSIGN && type != OP_AASSIGN
2979 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
2980 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
2989 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2993 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2999 if (type == OP_LEAVESUBLV)
3000 o->op_private |= OPpMAYBE_LVSUB;
3001 if (o->op_flags & OPf_KIDS)
3002 op_lvalue(OpSIBLING(cBINOPo->op_first), type);
3007 ref(cBINOPo->op_first, o->op_type);
3008 if (type == OP_ENTERSUB &&
3009 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3010 o->op_private |= OPpLVAL_DEFER;
3011 if (type == OP_LEAVESUBLV)
3012 o->op_private |= OPpMAYBE_LVSUB;
3019 o->op_private |= OPpLVALUE;
3025 if (o->op_flags & OPf_KIDS)
3026 op_lvalue(cLISTOPo->op_last, type);
3031 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3033 else if (!(o->op_flags & OPf_KIDS))
3035 if (o->op_targ != OP_LIST) {
3036 op_lvalue(cBINOPo->op_first, type);
3042 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3043 /* elements might be in void context because the list is
3044 in scalar context or because they are attribute sub calls */
3045 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
3046 op_lvalue(kid, type);
3054 if (type == OP_LEAVESUBLV
3055 || !S_vivifies(cLOGOPo->op_first->op_type))
3056 op_lvalue(cLOGOPo->op_first, type);
3057 if (type == OP_LEAVESUBLV
3058 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3059 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
3063 if (type != OP_AASSIGN && type != OP_SASSIGN
3064 && type != OP_ENTERLOOP)
3066 /* Don’t bother applying lvalue context to the ex-list. */
3067 kid = cUNOPx(cUNOPo->op_first)->op_first;
3068 assert (!OpHAS_SIBLING(kid));
3071 if (type != OP_AASSIGN) goto nomod;
3072 kid = cUNOPo->op_first;
3075 const U8 ec = PL_parser ? PL_parser->error_count : 0;
3076 S_lvref(aTHX_ kid, type);
3077 if (!PL_parser || PL_parser->error_count == ec) {
3078 if (!FEATURE_REFALIASING_IS_ENABLED)
3080 "Experimental aliasing via reference not enabled");
3081 Perl_ck_warner_d(aTHX_
3082 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3083 "Aliasing via reference is experimental");
3086 if (o->op_type == OP_REFGEN)
3087 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3092 kid = cLISTOPo->op_first;
3093 if (kid && kid->op_type == OP_PUSHRE &&
3095 || o->op_flags & OPf_STACKED
3097 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
3099 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
3102 /* This is actually @array = split. */
3103 PL_modcount = RETURN_UNLIMITED_NUMBER;
3109 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3113 /* [20011101.069] File test operators interpret OPf_REF to mean that
3114 their argument is a filehandle; thus \stat(".") should not set
3116 if (type == OP_REFGEN &&
3117 PL_check[o->op_type] == Perl_ck_ftst)
3120 if (type != OP_LEAVESUBLV)
3121 o->op_flags |= OPf_MOD;
3123 if (type == OP_AASSIGN || type == OP_SASSIGN)
3124 o->op_flags |= OPf_SPECIAL|OPf_REF;
3125 else if (!type) { /* local() */
3128 o->op_private |= OPpLVAL_INTRO;
3129 o->op_flags &= ~OPf_SPECIAL;
3130 PL_hints |= HINT_BLOCK_SCOPE;
3135 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3136 "Useless localization of %s", OP_DESC(o));
3139 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3140 && type != OP_LEAVESUBLV)
3141 o->op_flags |= OPf_REF;
3146 S_scalar_mod_type(const OP *o, I32 type)
3151 if (o && o->op_type == OP_RV2GV)
3175 case OP_RIGHT_SHIFT:
3196 S_is_handle_constructor(const OP *o, I32 numargs)
3198 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3200 switch (o->op_type) {
3208 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3221 S_refkids(pTHX_ OP *o, I32 type)
3223 if (o && o->op_flags & OPf_KIDS) {
3225 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3232 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3237 PERL_ARGS_ASSERT_DOREF;
3239 if (!o || (PL_parser && PL_parser->error_count))
3242 switch (o->op_type) {
3244 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3245 !(o->op_flags & OPf_STACKED)) {
3246 CHANGE_TYPE(o, OP_RV2CV); /* entersub => rv2cv */
3247 assert(cUNOPo->op_first->op_type == OP_NULL);
3248 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
3249 o->op_flags |= OPf_SPECIAL;
3251 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3252 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3253 : type == OP_RV2HV ? OPpDEREF_HV
3255 o->op_flags |= OPf_MOD;
3261 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3262 doref(kid, type, set_op_ref);
3265 if (type == OP_DEFINED)
3266 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3267 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3270 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3271 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3272 : type == OP_RV2HV ? OPpDEREF_HV
3274 o->op_flags |= OPf_MOD;
3281 o->op_flags |= OPf_REF;
3284 if (type == OP_DEFINED)
3285 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3286 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3292 o->op_flags |= OPf_REF;
3297 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3299 doref(cBINOPo->op_first, type, set_op_ref);
3303 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3304 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3305 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3306 : type == OP_RV2HV ? OPpDEREF_HV
3308 o->op_flags |= OPf_MOD;
3318 if (!(o->op_flags & OPf_KIDS))
3320 doref(cLISTOPo->op_last, type, set_op_ref);
3330 S_dup_attrlist(pTHX_ OP *o)
3334 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3336 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3337 * where the first kid is OP_PUSHMARK and the remaining ones
3338 * are OP_CONST. We need to push the OP_CONST values.
3340 if (o->op_type == OP_CONST)
3341 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3343 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3345 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
3346 if (o->op_type == OP_CONST)
3347 rop = op_append_elem(OP_LIST, rop,
3348 newSVOP(OP_CONST, o->op_flags,
3349 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3356 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3358 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
3360 PERL_ARGS_ASSERT_APPLY_ATTRS;
3362 /* fake up C<use attributes $pkg,$rv,@attrs> */
3364 #define ATTRSMODULE "attributes"
3365 #define ATTRSMODULE_PM "attributes.pm"
3367 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3368 newSVpvs(ATTRSMODULE),
3370 op_prepend_elem(OP_LIST,
3371 newSVOP(OP_CONST, 0, stashsv),
3372 op_prepend_elem(OP_LIST,
3373 newSVOP(OP_CONST, 0,
3375 dup_attrlist(attrs))));
3379 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3381 OP *pack, *imop, *arg;
3382 SV *meth, *stashsv, **svp;
3384 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3389 assert(target->op_type == OP_PADSV ||
3390 target->op_type == OP_PADHV ||
3391 target->op_type == OP_PADAV);
3393 /* Ensure that attributes.pm is loaded. */
3394 /* Don't force the C<use> if we don't need it. */
3395 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3396 if (svp && *svp != &PL_sv_undef)
3397 NOOP; /* already in %INC */
3399 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3400 newSVpvs(ATTRSMODULE), NULL);
3402 /* Need package name for method call. */
3403 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3405 /* Build up the real arg-list. */
3406 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
3408 arg = newOP(OP_PADSV, 0);
3409 arg->op_targ = target->op_targ;
3410 arg = op_prepend_elem(OP_LIST,
3411 newSVOP(OP_CONST, 0, stashsv),
3412 op_prepend_elem(OP_LIST,
3413 newUNOP(OP_REFGEN, 0,
3415 dup_attrlist(attrs)));
3417 /* Fake up a method call to import */
3418 meth = newSVpvs_share("import");
3419 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3420 op_append_elem(OP_LIST,
3421 op_prepend_elem(OP_LIST, pack, arg),
3422 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3424 /* Combine the ops. */
3425 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3429 =notfor apidoc apply_attrs_string
3431 Attempts to apply a list of attributes specified by the C<attrstr> and
3432 C<len> arguments to the subroutine identified by the C<cv> argument which
3433 is expected to be associated with the package identified by the C<stashpv>
3434 argument (see L<attributes>). It gets this wrong, though, in that it
3435 does not correctly identify the boundaries of the individual attribute
3436 specifications within C<attrstr>. This is not really intended for the
3437 public API, but has to be listed here for systems such as AIX which
3438 need an explicit export list for symbols. (It's called from XS code
3439 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3440 to respect attribute syntax properly would be welcome.
3446 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3447 const char *attrstr, STRLEN len)
3451 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3454 len = strlen(attrstr);
3458 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3460 const char * const sstr = attrstr;
3461 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3462 attrs = op_append_elem(OP_LIST, attrs,
3463 newSVOP(OP_CONST, 0,
3464 newSVpvn(sstr, attrstr-sstr)));
3468 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3469 newSVpvs(ATTRSMODULE),
3470 NULL, op_prepend_elem(OP_LIST,
3471 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3472 op_prepend_elem(OP_LIST,
3473 newSVOP(OP_CONST, 0,
3474 newRV(MUTABLE_SV(cv))),
3479 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3481 OP *new_proto = NULL;
3486 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3492 if (o->op_type == OP_CONST) {
3493 pv = SvPV(cSVOPo_sv, pvlen);
3494 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3495 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3496 SV ** const tmpo = cSVOPx_svp(o);
3497 SvREFCNT_dec(cSVOPo_sv);
3502 } else if (o->op_type == OP_LIST) {
3504 assert(o->op_flags & OPf_KIDS);
3505 lasto = cLISTOPo->op_first;
3506 assert(lasto->op_type == OP_PUSHMARK);
3507 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
3508 if (o->op_type == OP_CONST) {
3509 pv = SvPV(cSVOPo_sv, pvlen);
3510 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3511 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3512 SV ** const tmpo = cSVOPx_svp(o);
3513 SvREFCNT_dec(cSVOPo_sv);
3515 if (new_proto && ckWARN(WARN_MISC)) {
3517 const char * newp = SvPV(cSVOPo_sv, new_len);
3518 Perl_warner(aTHX_ packWARN(WARN_MISC),
3519 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
3520 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3526 /* excise new_proto from the list */
3527 op_sibling_splice(*attrs, lasto, 1, NULL);
3534 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3535 would get pulled in with no real need */
3536 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3545 svname = sv_newmortal();
3546 gv_efullname3(svname, name, NULL);
3548 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3549 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3551 svname = (SV *)name;
3552 if (ckWARN(WARN_ILLEGALPROTO))
3553 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3554 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3555 STRLEN old_len, new_len;
3556 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3557 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3559 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3560 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
3562 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3563 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3573 S_cant_declare(pTHX_ OP *o)
3575 if (o->op_type == OP_NULL
3576 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3577 o = cUNOPo->op_first;
3578 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3579 o->op_type == OP_NULL
3580 && o->op_flags & OPf_SPECIAL
3583 PL_parser->in_my == KEY_our ? "our" :
3584 PL_parser->in_my == KEY_state ? "state" :
3589 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3592 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3594 PERL_ARGS_ASSERT_MY_KID;
3596 if (!o || (PL_parser && PL_parser->error_count))
3601 if (type == OP_LIST) {
3603 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3604 my_kid(kid, attrs, imopsp);
3606 } else if (type == OP_UNDEF || type == OP_STUB) {
3608 } else if (type == OP_RV2SV || /* "our" declaration */
3610 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
3611 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3612 S_cant_declare(aTHX_ o);
3614 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3616 PL_parser->in_my = FALSE;
3617 PL_parser->in_my_stash = NULL;
3618 apply_attrs(GvSTASH(gv),
3619 (type == OP_RV2SV ? GvSV(gv) :
3620 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3621 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3624 o->op_private |= OPpOUR_INTRO;
3627 else if (type != OP_PADSV &&
3630 type != OP_PUSHMARK)
3632 S_cant_declare(aTHX_ o);
3635 else if (attrs && type != OP_PUSHMARK) {
3639 PL_parser->in_my = FALSE;
3640 PL_parser->in_my_stash = NULL;
3642 /* check for C<my Dog $spot> when deciding package */
3643 stash = PAD_COMPNAME_TYPE(o->op_targ);
3645 stash = PL_curstash;
3646 apply_attrs_my(stash, o, attrs, imopsp);
3648 o->op_flags |= OPf_MOD;
3649 o->op_private |= OPpLVAL_INTRO;
3651 o->op_private |= OPpPAD_STATE;
3656 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3659 int maybe_scalar = 0;
3661 PERL_ARGS_ASSERT_MY_ATTRS;
3663 /* [perl #17376]: this appears to be premature, and results in code such as
3664 C< our(%x); > executing in list mode rather than void mode */
3666 if (o->op_flags & OPf_PARENS)
3676 o = my_kid(o, attrs, &rops);
3678 if (maybe_scalar && o->op_type == OP_PADSV) {
3679 o = scalar(op_append_list(OP_LIST, rops, o));
3680 o->op_private |= OPpLVAL_INTRO;
3683 /* The listop in rops might have a pushmark at the beginning,
3684 which will mess up list assignment. */
3685 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3686 if (rops->op_type == OP_LIST &&
3687 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3689 OP * const pushmark = lrops->op_first;
3690 /* excise pushmark */
3691 op_sibling_splice(rops, NULL, 1, NULL);
3694 o = op_append_list(OP_LIST, o, rops);
3697 PL_parser->in_my = FALSE;
3698 PL_parser->in_my_stash = NULL;
3703 Perl_sawparens(pTHX_ OP *o)
3705 PERL_UNUSED_CONTEXT;
3707 o->op_flags |= OPf_PARENS;
3712 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3716 const OPCODE ltype = left->op_type;
3717 const OPCODE rtype = right->op_type;
3719 PERL_ARGS_ASSERT_BIND_MATCH;
3721 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3722 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3724 const char * const desc
3726 rtype == OP_SUBST || rtype == OP_TRANS
3727 || rtype == OP_TRANSR
3729 ? (int)rtype : OP_MATCH];
3730 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3732 S_op_varname(aTHX_ left);
3734 Perl_warner(aTHX_ packWARN(WARN_MISC),
3735 "Applying %s to %"SVf" will act on scalar(%"SVf")",
3736 desc, SVfARG(name), SVfARG(name));
3738 const char * const sample = (isary
3739 ? "@array" : "%hash");
3740 Perl_warner(aTHX_ packWARN(WARN_MISC),
3741 "Applying %s to %s will act on scalar(%s)",
3742 desc, sample, sample);
3746 if (rtype == OP_CONST &&
3747 cSVOPx(right)->op_private & OPpCONST_BARE &&
3748 cSVOPx(right)->op_private & OPpCONST_STRICT)
3750 no_bareword_allowed(right);
3753 /* !~ doesn't make sense with /r, so error on it for now */
3754 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3756 /* diag_listed_as: Using !~ with %s doesn't make sense */
3757 yyerror("Using !~ with s///r doesn't make sense");
3758 if (rtype == OP_TRANSR && type == OP_NOT)
3759 /* diag_listed_as: Using !~ with %s doesn't make sense */
3760 yyerror("Using !~ with tr///r doesn't make sense");
3762 ismatchop = (rtype == OP_MATCH ||
3763 rtype == OP_SUBST ||
3764 rtype == OP_TRANS || rtype == OP_TRANSR)
3765 && !(right->op_flags & OPf_SPECIAL);
3766 if (ismatchop && right->op_private & OPpTARGET_MY) {
3768 right->op_private &= ~OPpTARGET_MY;
3770 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
3771 if (left->op_type == OP_PADSV
3772 && !(left->op_private & OPpLVAL_INTRO))
3774 right->op_targ = left->op_targ;
3779 right->op_flags |= OPf_STACKED;
3780 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3781 ! (rtype == OP_TRANS &&
3782 right->op_private & OPpTRANS_IDENTICAL) &&
3783 ! (rtype == OP_SUBST &&
3784 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3785 left = op_lvalue(left, rtype);
3786 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3787 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
3789 o = op_prepend_elem(rtype, scalar(left), right);
3792 return newUNOP(OP_NOT, 0, scalar(o));
3796 return bind_match(type, left,
3797 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
3801 Perl_invert(pTHX_ OP *o)
3805 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3809 =for apidoc Amx|OP *|op_scope|OP *o
3811 Wraps up an op tree with some additional ops so that at runtime a dynamic
3812 scope will be created. The original ops run in the new dynamic scope,
3813 and then, provided that they exit normally, the scope will be unwound.
3814 The additional ops used to create and unwind the dynamic scope will
3815 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3816 instead if the ops are simple enough to not need the full dynamic scope
3823 Perl_op_scope(pTHX_ OP *o)
3827 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3828 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3829 CHANGE_TYPE(o, OP_LEAVE);
3831 else if (o->op_type == OP_LINESEQ) {
3833 CHANGE_TYPE(o, OP_SCOPE);
3834 kid = ((LISTOP*)o)->op_first;
3835 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3838 /* The following deals with things like 'do {1 for 1}' */
3839 kid = OpSIBLING(kid);
3841 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3846 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3852 Perl_op_unscope(pTHX_ OP *o)
3854 if (o && o->op_type == OP_LINESEQ) {
3855 OP *kid = cLISTOPo->op_first;
3856 for(; kid; kid = OpSIBLING(kid))
3857 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3864 =for apidoc Am|int|block_start|int full
3866 Handles compile-time scope entry.
3867 Arranges for hints to be restored on block
3868 exit and also handles pad sequence numbers to make lexical variables scope
3869 right. Returns a savestack index for use with C<block_end>.
3875 Perl_block_start(pTHX_ int full)
3877 const int retval = PL_savestack_ix;
3879 PL_compiling.cop_seq = PL_cop_seqmax;
3881 pad_block_start(full);
3883 PL_hints &= ~HINT_BLOCK_SCOPE;
3884 SAVECOMPILEWARNINGS();
3885 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3886 SAVEI32(PL_compiling.cop_seq);
3887 PL_compiling.cop_seq = 0;
3889 CALL_BLOCK_HOOKS(bhk_start, full);
3895 =for apidoc Am|OP *|block_end|I32 floor|OP *seq
3897 Handles compile-time scope exit. I<floor>
3898 is the savestack index returned by
3899 C<block_start>, and I<seq> is the body of the block. Returns the block,
3906 Perl_block_end(pTHX_ I32 floor, OP *seq)
3908 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3909 OP* retval = scalarseq(seq);
3912 /* XXX Is the null PL_parser check necessary here? */
3913 assert(PL_parser); /* Let’s find out under debugging builds. */
3914 if (PL_parser && PL_parser->parsed_sub) {
3915 o = newSTATEOP(0, NULL, NULL);
3917 retval = op_append_elem(OP_LINESEQ, retval, o);
3920 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3924 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3928 /* pad_leavemy has created a sequence of introcv ops for all my
3929 subs declared in the block. We have to replicate that list with
3930 clonecv ops, to deal with this situation:
3935 sub s1 { state sub foo { \&s2 } }
3938 Originally, I was going to have introcv clone the CV and turn
3939 off the stale flag. Since &s1 is declared before &s2, the
3940 introcv op for &s1 is executed (on sub entry) before the one for
3941 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3942 cloned, since it is a state sub) closes over &s2 and expects
3943 to see it in its outer CV’s pad. If the introcv op clones &s1,
3944 then &s2 is still marked stale. Since &s1 is not active, and
3945 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3946 ble will not stay shared’ warning. Because it is the same stub
3947 that will be used when the introcv op for &s2 is executed, clos-
3948 ing over it is safe. Hence, we have to turn off the stale flag
3949 on all lexical subs in the block before we clone any of them.
3950 Hence, having introcv clone the sub cannot work. So we create a
3951 list of ops like this:
3975 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3976 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3977 for (;; kid = OpSIBLING(kid)) {
3978 OP *newkid = newOP(OP_CLONECV, 0);
3979 newkid->op_targ = kid->op_targ;
3980 o = op_append_elem(OP_LINESEQ, o, newkid);
3981 if (kid == last) break;
3983 retval = op_prepend_elem(OP_LINESEQ, o, retval);
3986 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
3992 =head1 Compile-time scope hooks
3994 =for apidoc Aox||blockhook_register
3996 Register a set of hooks to be called when the Perl lexical scope changes
3997 at compile time. See L<perlguts/"Compile-time scope hooks">.
4003 Perl_blockhook_register(pTHX_ BHK *hk)
4005 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
4007 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
4011 Perl_newPROG(pTHX_ OP *o)
4013 PERL_ARGS_ASSERT_NEWPROG;
4020 PL_eval_root = newUNOP(OP_LEAVEEVAL,
4021 ((PL_in_eval & EVAL_KEEPERR)
4022 ? OPf_SPECIAL : 0), o);
4024 cx = &cxstack[cxstack_ix];
4025 assert(CxTYPE(cx) == CXt_EVAL);
4027 if ((cx->blk_gimme & G_WANT) == G_VOID)
4028 scalarvoid(PL_eval_root);
4029 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
4032 scalar(PL_eval_root);
4034 PL_eval_start = op_linklist(PL_eval_root);
4035 PL_eval_root->op_private |= OPpREFCOUNTED;
4036 OpREFCNT_set(PL_eval_root, 1);
4037 PL_eval_root->op_next = 0;
4038 i = PL_savestack_ix;
4041 CALL_PEEP(PL_eval_start);
4042 finalize_optree(PL_eval_root);
4043 S_prune_chain_head(&PL_eval_start);
4045 PL_savestack_ix = i;
4048 if (o->op_type == OP_STUB) {
4049 /* This block is entered if nothing is compiled for the main
4050 program. This will be the case for an genuinely empty main
4051 program, or one which only has BEGIN blocks etc, so already
4054 Historically (5.000) the guard above was !o. However, commit
4055 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
4056 c71fccf11fde0068, changed perly.y so that newPROG() is now
4057 called with the output of block_end(), which returns a new
4058 OP_STUB for the case of an empty optree. ByteLoader (and
4059 maybe other things) also take this path, because they set up
4060 PL_main_start and PL_main_root directly, without generating an
4063 If the parsing the main program aborts (due to parse errors,
4064 or due to BEGIN or similar calling exit), then newPROG()
4065 isn't even called, and hence this code path and its cleanups
4066 are skipped. This shouldn't make a make a difference:
4067 * a non-zero return from perl_parse is a failure, and
4068 perl_destruct() should be called immediately.
4069 * however, if exit(0) is called during the parse, then
4070 perl_parse() returns 0, and perl_run() is called. As
4071 PL_main_start will be NULL, perl_run() will return
4072 promptly, and the exit code will remain 0.
4075 PL_comppad_name = 0;
4077 S_op_destroy(aTHX_ o);
4080 PL_main_root = op_scope(sawparens(scalarvoid(o)));
4081 PL_curcop = &PL_compiling;
4082 PL_main_start = LINKLIST(PL_main_root);
4083 PL_main_root->op_private |= OPpREFCOUNTED;
4084 OpREFCNT_set(PL_main_root, 1);
4085 PL_main_root->op_next = 0;
4086 CALL_PEEP(PL_main_start);
4087 finalize_optree(PL_main_root);
4088 S_prune_chain_head(&PL_main_start);
4089 cv_forget_slab(PL_compcv);
4092 /* Register with debugger */
4094 CV * const cv = get_cvs("DB::postponed", 0);
4098 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
4100 call_sv(MUTABLE_SV(cv), G_DISCARD);
4107 Perl_localize(pTHX_ OP *o, I32 lex)
4109 PERL_ARGS_ASSERT_LOCALIZE;
4111 if (o->op_flags & OPf_PARENS)
4112 /* [perl #17376]: this appears to be premature, and results in code such as
4113 C< our(%x); > executing in list mode rather than void mode */
4120 if ( PL_parser->bufptr > PL_parser->oldbufptr
4121 && PL_parser->bufptr[-1] == ','
4122 && ckWARN(WARN_PARENTHESIS))
4124 char *s = PL_parser->bufptr;
4127 /* some heuristics to detect a potential error */
4128 while (*s && (strchr(", \t\n", *s)))
4132 if (*s && strchr("@$%*", *s) && *++s
4133 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
4136 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
4138 while (*s && (strchr(", \t\n", *s)))
4144 if (sigil && (*s == ';' || *s == '=')) {
4145 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
4146 "Parentheses missing around \"%s\" list",
4148 ? (PL_parser->in_my == KEY_our
4150 : PL_parser->in_my == KEY_state
4160 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4161 PL_parser->in_my = FALSE;
4162 PL_parser->in_my_stash = NULL;
4167 Perl_jmaybe(pTHX_ OP *o)
4169 PERL_ARGS_ASSERT_JMAYBE;
4171 if (o->op_type == OP_LIST) {
4173 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4174 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4179 PERL_STATIC_INLINE OP *
4180 S_op_std_init(pTHX_ OP *o)
4182 I32 type = o->op_type;
4184 PERL_ARGS_ASSERT_OP_STD_INIT;
4186 if (PL_opargs[type] & OA_RETSCALAR)
4188 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4189 o->op_targ = pad_alloc(type, SVs_PADTMP);
4194 PERL_STATIC_INLINE OP *
4195 S_op_integerize(pTHX_ OP *o)
4197 I32 type = o->op_type;
4199 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4201 /* integerize op. */
4202 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4205 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4208 if (type == OP_NEGATE)
4209 /* XXX might want a ck_negate() for this */
4210 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4216 S_fold_constants(pTHX_ OP *o)
4221 VOL I32 type = o->op_type;
4227 SV * const oldwarnhook = PL_warnhook;
4228 SV * const olddiehook = PL_diehook;
4230 U8 oldwarn = PL_dowarn;
4233 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4235 if (!(PL_opargs[type] & OA_FOLDCONST))
4244 #ifdef USE_LOCALE_CTYPE
4245 if (IN_LC_COMPILETIME(LC_CTYPE))
4254 #ifdef USE_LOCALE_COLLATE
4255 if (IN_LC_COMPILETIME(LC_COLLATE))
4260 /* XXX what about the numeric ops? */
4261 #ifdef USE_LOCALE_NUMERIC
4262 if (IN_LC_COMPILETIME(LC_NUMERIC))
4267 if (!OpHAS_SIBLING(cLISTOPo->op_first)
4268 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4271 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
4272 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4274 const char *s = SvPVX_const(sv);
4275 while (s < SvEND(sv)) {
4276 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4283 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4286 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4287 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4291 if (PL_parser && PL_parser->error_count)
4292 goto nope; /* Don't try to run w/ errors */
4294 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4295 const OPCODE type = curop->op_type;
4296 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
4298 type != OP_SCALAR &&
4300 type != OP_PUSHMARK)
4306 curop = LINKLIST(o);
4307 old_next = o->op_next;
4311 oldscope = PL_scopestack_ix;
4312 create_eval_scope(G_FAKINGEVAL);
4314 /* Verify that we don't need to save it: */
4315 assert(PL_curcop == &PL_compiling);
4316 StructCopy(&PL_compiling, ¬_compiling, COP);
4317 PL_curcop = ¬_compiling;
4318 /* The above ensures that we run with all the correct hints of the
4319 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4320 assert(IN_PERL_RUNTIME);
4321 PL_warnhook = PERL_WARNHOOK_FATAL;
4325 /* Effective $^W=1. */
4326 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4327 PL_dowarn |= G_WARN_ON;
4332 sv = *(PL_stack_sp--);
4333 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4334 pad_swipe(o->op_targ, FALSE);
4336 else if (SvTEMP(sv)) { /* grab mortal temp? */
4337 SvREFCNT_inc_simple_void(sv);
4340 else { assert(SvIMMORTAL(sv)); }
4343 /* Something tried to die. Abandon constant folding. */
4344 /* Pretend the error never happened. */
4346 o->op_next = old_next;
4350 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4351 PL_warnhook = oldwarnhook;
4352 PL_diehook = olddiehook;
4353 /* XXX note that this croak may fail as we've already blown away
4354 * the stack - eg any nested evals */
4355 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4358 PL_dowarn = oldwarn;
4359 PL_warnhook = oldwarnhook;
4360 PL_diehook = olddiehook;
4361 PL_curcop = &PL_compiling;
4363 if (PL_scopestack_ix > oldscope)
4364 delete_eval_scope();
4369 /* OP_STRINGIFY and constant folding are used to implement qq.
4370 Here the constant folding is an implementation detail that we
4371 want to hide. If the stringify op is itself already marked
4372 folded, however, then it is actually a folded join. */
4373 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4378 else if (!SvIMMORTAL(sv)) {
4382 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4383 if (!is_stringify) newop->op_folded = 1;
4391 S_gen_constant_list(pTHX_ OP *o)
4395 const SSize_t oldtmps_floor = PL_tmps_floor;
4400 if (PL_parser && PL_parser->error_count)
4401 return o; /* Don't attempt to run with errors */
4403 curop = LINKLIST(o);
4406 S_prune_chain_head(&curop);
4408 Perl_pp_pushmark(aTHX);
4411 assert (!(curop->op_flags & OPf_SPECIAL));
4412 assert(curop->op_type == OP_RANGE);
4413 Perl_pp_anonlist(aTHX);
4414 PL_tmps_floor = oldtmps_floor;
4416 CHANGE_TYPE(o, OP_RV2AV);
4417 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4418 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4419 o->op_opt = 0; /* needs to be revisited in rpeep() */
4420 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4422 /* replace subtree with an OP_CONST */
4423 curop = ((UNOP*)o)->op_first;
4424 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4427 if (AvFILLp(av) != -1)
4428 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4431 SvREADONLY_on(*svp);
4438 =head1 Optree Manipulation Functions
4441 /* List constructors */
4444 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4446 Append an item to the list of ops contained directly within a list-type
4447 op, returning the lengthened list. I<first> is the list-type op,
4448 and I<last> is the op to append to the list. I<optype> specifies the
4449 intended opcode for the list. If I<first> is not already a list of the
4450 right type, it will be upgraded into one. If either I<first> or I<last>
4451 is null, the other is returned unchanged.
4457 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4465 if (first->op_type != (unsigned)type
4466 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4468 return newLISTOP(type, 0, first, last);
4471 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4472 first->op_flags |= OPf_KIDS;
4477 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4479 Concatenate the lists of ops contained directly within two list-type ops,
4480 returning the combined list. I<first> and I<last> are the list-type ops
4481 to concatenate. I<optype> specifies the intended opcode for the list.
4482 If either I<first> or I<last> is not already a list of the right type,
4483 it will be upgraded into one. If either I<first> or I<last> is null,
4484 the other is returned unchanged.
4490 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4498 if (first->op_type != (unsigned)type)
4499 return op_prepend_elem(type, first, last);
4501 if (last->op_type != (unsigned)type)
4502 return op_append_elem(type, first, last);
4504 ((LISTOP*)first)->op_last->op_lastsib = 0;
4505 OpSIBLING_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4506 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4507 ((LISTOP*)first)->op_last->op_lastsib = 1;
4508 #ifdef PERL_OP_PARENT
4509 ((LISTOP*)first)->op_last->op_sibling = first;
4511 first->op_flags |= (last->op_flags & OPf_KIDS);
4514 S_op_destroy(aTHX_ last);
4520 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4522 Prepend an item to the list of ops contained directly within a list-type
4523 op, returning the lengthened list. I<first> is the op to prepend to the
4524 list, and I<last> is the list-type op. I<optype> specifies the intended
4525 opcode for the list. If I<last> is not already a list of the right type,
4526 it will be upgraded into one. If either I<first> or I<last> is null,
4527 the other is returned unchanged.
4533 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4541 if (last->op_type == (unsigned)type) {
4542 if (type == OP_LIST) { /* already a PUSHMARK there */
4543 /* insert 'first' after pushmark */
4544 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4545 if (!(first->op_flags & OPf_PARENS))
4546 last->op_flags &= ~OPf_PARENS;
4549 op_sibling_splice(last, NULL, 0, first);
4550 last->op_flags |= OPf_KIDS;
4554 return newLISTOP(type, 0, first, last);
4558 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4560 Converts I<o> into a list op if it is not one already, and then converts it
4561 into the specified I<type>, calling its check function, allocating a target if
4562 it needs one, and folding constants.
4564 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4565 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4566 C<op_convert_list> to make it the right type.
4572 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4575 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4576 if (!o || o->op_type != OP_LIST)
4577 o = force_list(o, 0);
4580 o->op_flags &= ~OPf_WANT;
4581 o->op_private &= ~OPpLVAL_INTRO;
4584 if (!(PL_opargs[type] & OA_MARK))
4585 op_null(cLISTOPo->op_first);
4587 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
4588 if (kid2 && kid2->op_type == OP_COREARGS) {
4589 op_null(cLISTOPo->op_first);
4590 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4594 CHANGE_TYPE(o, type);
4595 o->op_flags |= flags;
4596 if (flags & OPf_FOLDED)
4599 o = CHECKOP(type, o);
4600 if (o->op_type != (unsigned)type)
4603 return fold_constants(op_integerize(op_std_init(o)));
4610 =head1 Optree construction
4612 =for apidoc Am|OP *|newNULLLIST
4614 Constructs, checks, and returns a new C<stub> op, which represents an
4615 empty list expression.
4621 Perl_newNULLLIST(pTHX)
4623 return newOP(OP_STUB, 0);
4626 /* promote o and any siblings to be a list if its not already; i.e.
4634 * pushmark - o - A - B
4636 * If nullit it true, the list op is nulled.
4640 S_force_list(pTHX_ OP *o, bool nullit)
4642 if (!o || o->op_type != OP_LIST) {
4645 /* manually detach any siblings then add them back later */
4646 rest = OpSIBLING(o);
4647 OpSIBLING_set(o, NULL);
4650 o = newLISTOP(OP_LIST, 0, o, NULL);
4652 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4660 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4662 Constructs, checks, and returns an op of any list type. I<type> is
4663 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4664 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
4665 supply up to two ops to be direct children of the list op; they are
4666 consumed by this function and become part of the constructed op tree.
4668 For most list operators, the check function expects all the kid ops to be
4669 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
4670 appropriate. What you want to do in that case is create an op of type
4671 OP_LIST, append more children to it, and then call L</op_convert_list>.
4672 See L</op_convert_list> for more information.
4679 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4684 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
4685 || type == OP_CUSTOM);
4687 NewOp(1101, listop, 1, LISTOP);
4689 CHANGE_TYPE(listop, type);
4692 listop->op_flags = (U8)flags;
4696 else if (!first && last)
4699 OpSIBLING_set(first, last);
4700 listop->op_first = first;
4701 listop->op_last = last;
4702 if (type == OP_LIST) {
4703 OP* const pushop = newOP(OP_PUSHMARK, 0);
4704 pushop->op_lastsib = 0;
4705 OpSIBLING_set(pushop, first);
4706 listop->op_first = pushop;
4707 listop->op_flags |= OPf_KIDS;
4709 listop->op_last = pushop;
4712 first->op_lastsib = 0;
4713 if (listop->op_last) {
4714 listop->op_last->op_lastsib = 1;
4715 #ifdef PERL_OP_PARENT
4716 listop->op_last->op_sibling = (OP*)listop;
4720 return CHECKOP(type, listop);
4724 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4726 Constructs, checks, and returns an op of any base type (any type that
4727 has no extra fields). I<type> is the opcode. I<flags> gives the
4728 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4735 Perl_newOP(pTHX_ I32 type, I32 flags)
4740 if (type == -OP_ENTEREVAL) {
4741 type = OP_ENTEREVAL;
4742 flags |= OPpEVAL_BYTES<<8;
4745 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4746 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4747 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4748 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4750 NewOp(1101, o, 1, OP);
4751 CHANGE_TYPE(o, type);
4752 o->op_flags = (U8)flags;
4755 o->op_private = (U8)(0 | (flags >> 8));
4756 if (PL_opargs[type] & OA_RETSCALAR)
4758 if (PL_opargs[type] & OA_TARGET)
4759 o->op_targ = pad_alloc(type, SVs_PADTMP);
4760 return CHECKOP(type, o);
4764 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4766 Constructs, checks, and returns an op of any unary type. I<type> is
4767 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4768 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4769 bits, the eight bits of C<op_private>, except that the bit with value 1
4770 is automatically set. I<first> supplies an optional op to be the direct
4771 child of the unary op; it is consumed by this function and become part
4772 of the constructed op tree.
4778 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4783 if (type == -OP_ENTEREVAL) {
4784 type = OP_ENTEREVAL;
4785 flags |= OPpEVAL_BYTES<<8;
4788 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4789 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4790 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4791 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4792 || type == OP_SASSIGN
4793 || type == OP_ENTERTRY
4794 || type == OP_CUSTOM
4795 || type == OP_NULL );
4798 first = newOP(OP_STUB, 0);
4799 if (PL_opargs[type] & OA_MARK)
4800 first = force_list(first, 1);
4802 NewOp(1101, unop, 1, UNOP);
4803 CHANGE_TYPE(unop, type);
4804 unop->op_first = first;
4805 unop->op_flags = (U8)(flags | OPf_KIDS);
4806 unop->op_private = (U8)(1 | (flags >> 8));
4808 #ifdef PERL_OP_PARENT
4809 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
4810 first->op_sibling = (OP*)unop;
4813 unop = (UNOP*) CHECKOP(type, unop);
4817 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4821 =for apidoc newUNOP_AUX
4823 Similar to C<newUNOP>, but creates an UNOP_AUX struct instead, with op_aux
4830 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux)
4835 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX
4836 || type == OP_CUSTOM);
4838 NewOp(1101, unop, 1, UNOP_AUX);
4839 unop->op_type = (OPCODE)type;
4840 unop->op_ppaddr = PL_ppaddr[type];
4841 unop->op_first = first;
4842 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0));
4843 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8));
4846 #ifdef PERL_OP_PARENT
4847 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */
4848 first->op_sibling = (OP*)unop;
4851 unop = (UNOP_AUX*) CHECKOP(type, unop);