4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
38 * newBINOP(OP_ADD, flags,
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
52 An execution-order pass
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines. The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order. (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again). As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node. But
70 it's still not the real execution order.
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer. At that point, we can call
75 into peep() to do that code's portion of the 3rd pass. It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80 get the compile time state of %^H for that block. Storing %^H in every
81 block (or even COP) would be very expensive, so a different approach is
82 taken. The (running) state of %^H is serialised into a tree of HE-like
83 structs. Stores into %^H are chained onto the current leaf as a struct
84 refcounted_he * with the key and the value. Deletes from %^H are saved
85 with a value of PL_sv_placeholder. The state of %^H at any point can be
86 turned back into a regular HV by walking back up the tree from that point's
87 leaf, ignoring any key you've already seen (placeholder or not), storing
88 the rest into the HV structure, then removing the placeholders. Hence
89 memory is only used to store the %^H deltas from the enclosing COP, rather
90 than the entire %^H on each COP.
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98 it will be correctly restored when any inner compiling scope is exited.
104 #include "keywords.h"
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
112 /* Used to avoid recursion through the op tree in scalarvoid() and
116 #define DEFERRED_OP_STEP 100
117 #define DEFER_OP(o) \
119 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \
120 defer_stack_alloc += DEFERRED_OP_STEP; \
121 assert(defer_stack_alloc > 0); \
122 Renew(defer_stack, defer_stack_alloc, OP *); \
124 defer_stack[++defer_ix] = o; \
127 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
129 /* remove any leading "empty" ops from the op_next chain whose first
130 * node's address is stored in op_p. Store the updated address of the
131 * first node in op_p.
135 S_prune_chain_head(OP** op_p)
138 && ( (*op_p)->op_type == OP_NULL
139 || (*op_p)->op_type == OP_SCOPE
140 || (*op_p)->op_type == OP_SCALAR
141 || (*op_p)->op_type == OP_LINESEQ)
143 *op_p = (*op_p)->op_next;
147 /* See the explanatory comments above struct opslab in op.h. */
149 #ifdef PERL_DEBUG_READONLY_OPS
150 # define PERL_SLAB_SIZE 128
151 # define PERL_MAX_SLAB_SIZE 4096
152 # include <sys/mman.h>
155 #ifndef PERL_SLAB_SIZE
156 # define PERL_SLAB_SIZE 64
158 #ifndef PERL_MAX_SLAB_SIZE
159 # define PERL_MAX_SLAB_SIZE 2048
162 /* rounds up to nearest pointer */
163 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
164 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
167 S_new_slab(pTHX_ size_t sz)
169 #ifdef PERL_DEBUG_READONLY_OPS
170 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
171 PROT_READ|PROT_WRITE,
172 MAP_ANON|MAP_PRIVATE, -1, 0);
173 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
174 (unsigned long) sz, slab));
175 if (slab == MAP_FAILED) {
176 perror("mmap failed");
179 slab->opslab_size = (U16)sz;
181 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
184 /* The context is unused in non-Windows */
187 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
191 /* requires double parens and aTHX_ */
192 #define DEBUG_S_warn(args) \
194 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
198 Perl_Slab_Alloc(pTHX_ size_t sz)
206 /* We only allocate ops from the slab during subroutine compilation.
207 We find the slab via PL_compcv, hence that must be non-NULL. It could
208 also be pointing to a subroutine which is now fully set up (CvROOT()
209 pointing to the top of the optree for that sub), or a subroutine
210 which isn't using the slab allocator. If our sanity checks aren't met,
211 don't use a slab, but allocate the OP directly from the heap. */
212 if (!PL_compcv || CvROOT(PL_compcv)
213 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
215 o = (OP*)PerlMemShared_calloc(1, sz);
219 /* While the subroutine is under construction, the slabs are accessed via
220 CvSTART(), to avoid needing to expand PVCV by one pointer for something
221 unneeded at runtime. Once a subroutine is constructed, the slabs are
222 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
223 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
225 if (!CvSTART(PL_compcv)) {
227 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
228 CvSLABBED_on(PL_compcv);
229 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
231 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
233 opsz = SIZE_TO_PSIZE(sz);
234 sz = opsz + OPSLOT_HEADER_P;
236 /* The slabs maintain a free list of OPs. In particular, constant folding
237 will free up OPs, so it makes sense to re-use them where possible. A
238 freed up slot is used in preference to a new allocation. */
239 if (slab->opslab_freed) {
240 OP **too = &slab->opslab_freed;
242 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
243 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
244 DEBUG_S_warn((aTHX_ "Alas! too small"));
245 o = *(too = &o->op_next);
246 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
250 Zero(o, opsz, I32 *);
256 #define INIT_OPSLOT \
257 slot->opslot_slab = slab; \
258 slot->opslot_next = slab2->opslab_first; \
259 slab2->opslab_first = slot; \
260 o = &slot->opslot_op; \
263 /* The partially-filled slab is next in the chain. */
264 slab2 = slab->opslab_next ? slab->opslab_next : slab;
265 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
266 /* Remaining space is too small. */
268 /* If we can fit a BASEOP, add it to the free chain, so as not
270 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
271 slot = &slab2->opslab_slots;
273 o->op_type = OP_FREED;
274 o->op_next = slab->opslab_freed;
275 slab->opslab_freed = o;
278 /* Create a new slab. Make this one twice as big. */
279 slot = slab2->opslab_first;
280 while (slot->opslot_next) slot = slot->opslot_next;
281 slab2 = S_new_slab(aTHX_
282 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
284 : (DIFF(slab2, slot)+1)*2);
285 slab2->opslab_next = slab->opslab_next;
286 slab->opslab_next = slab2;
288 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
290 /* Create a new op slot */
291 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
292 assert(slot >= &slab2->opslab_slots);
293 if (DIFF(&slab2->opslab_slots, slot)
294 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
295 slot = &slab2->opslab_slots;
297 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
300 #ifdef PERL_OP_PARENT
301 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
302 assert(!o->op_moresib);
303 assert(!o->op_sibparent);
311 #ifdef PERL_DEBUG_READONLY_OPS
313 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
315 PERL_ARGS_ASSERT_SLAB_TO_RO;
317 if (slab->opslab_readonly) return;
318 slab->opslab_readonly = 1;
319 for (; slab; slab = slab->opslab_next) {
320 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
321 (unsigned long) slab->opslab_size, slab));*/
322 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
323 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
324 (unsigned long)slab->opslab_size, errno);
329 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
333 PERL_ARGS_ASSERT_SLAB_TO_RW;
335 if (!slab->opslab_readonly) return;
337 for (; slab2; slab2 = slab2->opslab_next) {
338 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
339 (unsigned long) size, slab2));*/
340 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
341 PROT_READ|PROT_WRITE)) {
342 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
343 (unsigned long)slab2->opslab_size, errno);
346 slab->opslab_readonly = 0;
350 # define Slab_to_rw(op) NOOP
353 /* This cannot possibly be right, but it was copied from the old slab
354 allocator, to which it was originally added, without explanation, in
357 # define PerlMemShared PerlMem
361 Perl_Slab_Free(pTHX_ void *op)
363 OP * const o = (OP *)op;
366 PERL_ARGS_ASSERT_SLAB_FREE;
368 if (!o->op_slabbed) {
370 PerlMemShared_free(op);
375 /* If this op is already freed, our refcount will get screwy. */
376 assert(o->op_type != OP_FREED);
377 o->op_type = OP_FREED;
378 o->op_next = slab->opslab_freed;
379 slab->opslab_freed = o;
380 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
381 OpslabREFCNT_dec_padok(slab);
385 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
387 const bool havepad = !!PL_comppad;
388 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
391 PAD_SAVE_SETNULLPAD();
398 Perl_opslab_free(pTHX_ OPSLAB *slab)
401 PERL_ARGS_ASSERT_OPSLAB_FREE;
403 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
404 assert(slab->opslab_refcnt == 1);
406 slab2 = slab->opslab_next;
408 slab->opslab_refcnt = ~(size_t)0;
410 #ifdef PERL_DEBUG_READONLY_OPS
411 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
413 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
414 perror("munmap failed");
418 PerlMemShared_free(slab);
425 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
430 size_t savestack_count = 0;
432 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
435 for (slot = slab2->opslab_first;
437 slot = slot->opslot_next) {
438 if (slot->opslot_op.op_type != OP_FREED
439 && !(slot->opslot_op.op_savefree
445 assert(slot->opslot_op.op_slabbed);
446 op_free(&slot->opslot_op);
447 if (slab->opslab_refcnt == 1) goto free;
450 } while ((slab2 = slab2->opslab_next));
451 /* > 1 because the CV still holds a reference count. */
452 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
454 assert(savestack_count == slab->opslab_refcnt-1);
456 /* Remove the CV’s reference count. */
457 slab->opslab_refcnt--;
464 #ifdef PERL_DEBUG_READONLY_OPS
466 Perl_op_refcnt_inc(pTHX_ OP *o)
469 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
470 if (slab && slab->opslab_readonly) {
483 Perl_op_refcnt_dec(pTHX_ OP *o)
486 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
488 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
490 if (slab && slab->opslab_readonly) {
492 result = --o->op_targ;
495 result = --o->op_targ;
501 * In the following definition, the ", (OP*)0" is just to make the compiler
502 * think the expression is of the right type: croak actually does a Siglongjmp.
504 #define CHECKOP(type,o) \
505 ((PL_op_mask && PL_op_mask[type]) \
506 ? ( op_free((OP*)o), \
507 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
509 : PL_check[type](aTHX_ (OP*)o))
511 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
513 #define OpTYPE_set(o,type) \
515 o->op_type = (OPCODE)type; \
516 o->op_ppaddr = PL_ppaddr[type]; \
520 S_no_fh_allowed(pTHX_ OP *o)
522 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
524 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
530 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
532 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
533 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
538 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
540 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
542 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
547 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
549 PERL_ARGS_ASSERT_BAD_TYPE_PV;
551 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
552 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
555 /* remove flags var, its unused in all callers, move to to right end since gv
556 and kid are always the same */
558 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
560 SV * const namesv = cv_name((CV *)gv, NULL, 0);
561 PERL_ARGS_ASSERT_BAD_TYPE_GV;
563 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
564 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
568 S_no_bareword_allowed(pTHX_ OP *o)
570 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
572 qerror(Perl_mess(aTHX_
573 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
575 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
578 /* "register" allocation */
581 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
584 const bool is_our = (PL_parser->in_my == KEY_our);
586 PERL_ARGS_ASSERT_ALLOCMY;
588 if (flags & ~SVf_UTF8)
589 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
592 /* complain about "my $<special_var>" etc etc */
596 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
597 (name[1] == '_' && (*name == '$' || len > 2))))
599 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
601 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
602 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
603 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
604 PL_parser->in_my == KEY_state ? "state" : "my"));
606 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
607 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
610 else if (len == 2 && name[1] == '_' && !is_our)
611 /* diag_listed_as: Use of my $_ is experimental */
612 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
613 "Use of %s $_ is experimental",
614 PL_parser->in_my == KEY_state
618 /* allocate a spare slot and store the name in that slot */
620 off = pad_add_name_pvn(name, len,
621 (is_our ? padadd_OUR :
622 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
623 PL_parser->in_my_stash,
625 /* $_ is always in main::, even with our */
626 ? (PL_curstash && !memEQs(name,len,"$_")
632 /* anon sub prototypes contains state vars should always be cloned,
633 * otherwise the state var would be shared between anon subs */
635 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
636 CvCLONE_on(PL_compcv);
642 =head1 Optree Manipulation Functions
644 =for apidoc alloccopstash
646 Available only under threaded builds, this function allocates an entry in
647 C<PL_stashpad> for the stash passed to it.
654 Perl_alloccopstash(pTHX_ HV *hv)
656 PADOFFSET off = 0, o = 1;
657 bool found_slot = FALSE;
659 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
661 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
663 for (; o < PL_stashpadmax; ++o) {
664 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
665 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
666 found_slot = TRUE, off = o;
669 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
670 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
671 off = PL_stashpadmax;
672 PL_stashpadmax += 10;
675 PL_stashpad[PL_stashpadix = off] = hv;
680 /* free the body of an op without examining its contents.
681 * Always use this rather than FreeOp directly */
684 S_op_destroy(pTHX_ OP *o)
692 =for apidoc Am|void|op_free|OP *o
694 Free an op. Only use this when an op is no longer linked to from any
701 Perl_op_free(pTHX_ OP *o)
705 SSize_t defer_ix = -1;
706 SSize_t defer_stack_alloc = 0;
707 OP **defer_stack = NULL;
711 /* Though ops may be freed twice, freeing the op after its slab is a
713 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
714 /* During the forced freeing of ops after compilation failure, kidops
715 may be freed before their parents. */
716 if (!o || o->op_type == OP_FREED)
721 /* an op should only ever acquire op_private flags that we know about.
722 * If this fails, you may need to fix something in regen/op_private */
723 if (o->op_ppaddr == PL_ppaddr[o->op_type]) {
724 assert(!(o->op_private & ~PL_op_private_valid[type]));
727 if (o->op_private & OPpREFCOUNTED) {
738 refcnt = OpREFCNT_dec(o);
741 /* Need to find and remove any pattern match ops from the list
742 we maintain for reset(). */
743 find_and_forget_pmops(o);
753 /* Call the op_free hook if it has been set. Do it now so that it's called
754 * at the right time for refcounted ops, but still before all of the kids
758 if (o->op_flags & OPf_KIDS) {
760 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
761 nextkid = OpSIBLING(kid); /* Get before next freeing kid */
762 if (!kid || kid->op_type == OP_FREED)
763 /* During the forced freeing of ops after
764 compilation failure, kidops may be freed before
767 if (!(kid->op_flags & OPf_KIDS))
768 /* If it has no kids, just free it now */
775 type = (OPCODE)o->op_targ;
778 Slab_to_rw(OpSLAB(o));
780 /* COP* is not cleared by op_clear() so that we may track line
781 * numbers etc even after null() */
782 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
788 #ifdef DEBUG_LEAKING_SCALARS
792 } while ( (o = POP_DEFERRED_OP()) );
794 Safefree(defer_stack);
797 /* S_op_clear_gv(): free a GV attached to an OP */
800 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
802 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
806 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
807 || o->op_type == OP_MULTIDEREF)
810 ? ((GV*)PAD_SVl(*ixp)) : NULL;
812 ? (GV*)(*svp) : NULL;
814 /* It's possible during global destruction that the GV is freed
815 before the optree. Whilst the SvREFCNT_inc is happy to bump from
816 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
817 will trigger an assertion failure, because the entry to sv_clear
818 checks that the scalar is not already freed. A check of for
819 !SvIS_FREED(gv) turns out to be invalid, because during global
820 destruction the reference count can be forced down to zero
821 (with SVf_BREAK set). In which case raising to 1 and then
822 dropping to 0 triggers cleanup before it should happen. I
823 *think* that this might actually be a general, systematic,
824 weakness of the whole idea of SVf_BREAK, in that code *is*
825 allowed to raise and lower references during global destruction,
826 so any *valid* code that happens to do this during global
827 destruction might well trigger premature cleanup. */
828 bool still_valid = gv && SvREFCNT(gv);
831 SvREFCNT_inc_simple_void(gv);
834 pad_swipe(*ixp, TRUE);
842 int try_downgrade = SvREFCNT(gv) == 2;
845 gv_try_downgrade(gv);
851 Perl_op_clear(pTHX_ OP *o)
856 PERL_ARGS_ASSERT_OP_CLEAR;
858 switch (o->op_type) {
859 case OP_NULL: /* Was holding old type, if any. */
862 case OP_ENTEREVAL: /* Was holding hints. */
866 if (!(o->op_flags & OPf_REF)
867 || (PL_check[o->op_type] != Perl_ck_ftst))
874 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
876 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
879 case OP_METHOD_REDIR:
880 case OP_METHOD_REDIR_SUPER:
882 if (cMETHOPx(o)->op_rclass_targ) {
883 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
884 cMETHOPx(o)->op_rclass_targ = 0;
887 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
888 cMETHOPx(o)->op_rclass_sv = NULL;
890 case OP_METHOD_NAMED:
891 case OP_METHOD_SUPER:
892 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
893 cMETHOPx(o)->op_u.op_meth_sv = NULL;
896 pad_swipe(o->op_targ, 1);
903 SvREFCNT_dec(cSVOPo->op_sv);
904 cSVOPo->op_sv = NULL;
907 Even if op_clear does a pad_free for the target of the op,
908 pad_free doesn't actually remove the sv that exists in the pad;
909 instead it lives on. This results in that it could be reused as
910 a target later on when the pad was reallocated.
913 pad_swipe(o->op_targ,1);
923 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
928 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
929 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
931 if (cPADOPo->op_padix > 0) {
932 pad_swipe(cPADOPo->op_padix, TRUE);
933 cPADOPo->op_padix = 0;
936 SvREFCNT_dec(cSVOPo->op_sv);
937 cSVOPo->op_sv = NULL;
941 PerlMemShared_free(cPVOPo->op_pv);
942 cPVOPo->op_pv = NULL;
946 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
950 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
951 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
954 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
960 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
961 op_free(cPMOPo->op_code_list);
962 cPMOPo->op_code_list = NULL;
964 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
965 /* we use the same protection as the "SAFE" version of the PM_ macros
966 * here since sv_clean_all might release some PMOPs
967 * after PL_regex_padav has been cleared
968 * and the clearing of PL_regex_padav needs to
969 * happen before sv_clean_all
972 if(PL_regex_pad) { /* We could be in destruction */
973 const IV offset = (cPMOPo)->op_pmoffset;
974 ReREFCNT_dec(PM_GETRE(cPMOPo));
975 PL_regex_pad[offset] = &PL_sv_undef;
976 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
980 ReREFCNT_dec(PM_GETRE(cPMOPo));
981 PM_SETRE(cPMOPo, NULL);
988 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
989 UV actions = items->uv;
991 bool is_hash = FALSE;
994 switch (actions & MDEREF_ACTION_MASK) {
997 actions = (++items)->uv;
1000 case MDEREF_HV_padhv_helem:
1002 case MDEREF_AV_padav_aelem:
1003 pad_free((++items)->pad_offset);
1006 case MDEREF_HV_gvhv_helem:
1008 case MDEREF_AV_gvav_aelem:
1010 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1012 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1016 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1018 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1020 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1022 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1024 goto do_vivify_rv2xv_elem;
1026 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1028 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1029 pad_free((++items)->pad_offset);
1030 goto do_vivify_rv2xv_elem;
1032 case MDEREF_HV_pop_rv2hv_helem:
1033 case MDEREF_HV_vivify_rv2hv_helem:
1035 do_vivify_rv2xv_elem:
1036 case MDEREF_AV_pop_rv2av_aelem:
1037 case MDEREF_AV_vivify_rv2av_aelem:
1039 switch (actions & MDEREF_INDEX_MASK) {
1040 case MDEREF_INDEX_none:
1043 case MDEREF_INDEX_const:
1047 pad_swipe((++items)->pad_offset, 1);
1049 SvREFCNT_dec((++items)->sv);
1055 case MDEREF_INDEX_padsv:
1056 pad_free((++items)->pad_offset);
1058 case MDEREF_INDEX_gvsv:
1060 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1062 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1067 if (actions & MDEREF_FLAG_last)
1080 actions >>= MDEREF_SHIFT;
1083 /* start of malloc is at op_aux[-1], where the length is
1085 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1090 if (o->op_targ > 0) {
1091 pad_free(o->op_targ);
1097 S_cop_free(pTHX_ COP* cop)
1099 PERL_ARGS_ASSERT_COP_FREE;
1102 if (! specialWARN(cop->cop_warnings))
1103 PerlMemShared_free(cop->cop_warnings);
1104 cophh_free(CopHINTHASH_get(cop));
1105 if (PL_curcop == cop)
1110 S_forget_pmop(pTHX_ PMOP *const o
1113 HV * const pmstash = PmopSTASH(o);
1115 PERL_ARGS_ASSERT_FORGET_PMOP;
1117 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1118 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1120 PMOP **const array = (PMOP**) mg->mg_ptr;
1121 U32 count = mg->mg_len / sizeof(PMOP**);
1125 if (array[i] == o) {
1126 /* Found it. Move the entry at the end to overwrite it. */
1127 array[i] = array[--count];
1128 mg->mg_len = count * sizeof(PMOP**);
1129 /* Could realloc smaller at this point always, but probably
1130 not worth it. Probably worth free()ing if we're the
1133 Safefree(mg->mg_ptr);
1146 S_find_and_forget_pmops(pTHX_ OP *o)
1148 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1150 if (o->op_flags & OPf_KIDS) {
1151 OP *kid = cUNOPo->op_first;
1153 switch (kid->op_type) {
1158 forget_pmop((PMOP*)kid);
1160 find_and_forget_pmops(kid);
1161 kid = OpSIBLING(kid);
1167 =for apidoc Am|void|op_null|OP *o
1169 Neutralizes an op when it is no longer needed, but is still linked to from
1176 Perl_op_null(pTHX_ OP *o)
1180 PERL_ARGS_ASSERT_OP_NULL;
1182 if (o->op_type == OP_NULL)
1185 o->op_targ = o->op_type;
1186 OpTYPE_set(o, OP_NULL);
1190 Perl_op_refcnt_lock(pTHX)
1195 PERL_UNUSED_CONTEXT;
1200 Perl_op_refcnt_unlock(pTHX)
1205 PERL_UNUSED_CONTEXT;
1211 =for apidoc op_sibling_splice
1213 A general function for editing the structure of an existing chain of
1214 op_sibling nodes. By analogy with the perl-level splice() function, allows
1215 you to delete zero or more sequential nodes, replacing them with zero or
1216 more different nodes. Performs the necessary op_first/op_last
1217 housekeeping on the parent node and op_sibling manipulation on the
1218 children. The last deleted node will be marked as as the last node by
1219 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1221 Note that op_next is not manipulated, and nodes are not freed; that is the
1222 responsibility of the caller. It also won't create a new list op for an
1223 empty list etc; use higher-level functions like op_append_elem() for that.
1225 parent is the parent node of the sibling chain. It may passed as NULL if
1226 the splicing doesn't affect the first or last op in the chain.
1228 start is the node preceding the first node to be spliced. Node(s)
1229 following it will be deleted, and ops will be inserted after it. If it is
1230 NULL, the first node onwards is deleted, and nodes are inserted at the
1233 del_count is the number of nodes to delete. If zero, no nodes are deleted.
1234 If -1 or greater than or equal to the number of remaining kids, all
1235 remaining kids are deleted.
1237 insert is the first of a chain of nodes to be inserted in place of the nodes.
1238 If NULL, no nodes are inserted.
1240 The head of the chain of deleted ops is returned, or NULL if no ops were
1245 action before after returns
1246 ------ ----- ----- -------
1249 splice(P, A, 2, X-Y-Z) | | B-C
1253 splice(P, NULL, 1, X-Y) | | A
1257 splice(P, NULL, 3, NULL) | | A-B-C
1261 splice(P, B, 0, X-Y) | | NULL
1265 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1266 see C<OpMORESIB_set>, C<OpLASTSIB_set>, C<OpMAYBESIB_set>.
1272 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1276 OP *last_del = NULL;
1277 OP *last_ins = NULL;
1280 first = OpSIBLING(start);
1284 first = cLISTOPx(parent)->op_first;
1286 assert(del_count >= -1);
1288 if (del_count && first) {
1290 while (--del_count && OpHAS_SIBLING(last_del))
1291 last_del = OpSIBLING(last_del);
1292 rest = OpSIBLING(last_del);
1293 OpLASTSIB_set(last_del, NULL);
1300 while (OpHAS_SIBLING(last_ins))
1301 last_ins = OpSIBLING(last_ins);
1302 OpMAYBESIB_set(last_ins, rest, NULL);
1308 OpMAYBESIB_set(start, insert, NULL);
1313 cLISTOPx(parent)->op_first = insert;
1315 parent->op_flags |= OPf_KIDS;
1317 parent->op_flags &= ~OPf_KIDS;
1321 /* update op_last etc */
1328 /* ought to use OP_CLASS(parent) here, but that can't handle
1329 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1331 type = parent->op_type;
1332 if (type == OP_CUSTOM) {
1334 type = XopENTRYCUSTOM(parent, xop_class);
1337 if (type == OP_NULL)
1338 type = parent->op_targ;
1339 type = PL_opargs[type] & OA_CLASS_MASK;
1342 lastop = last_ins ? last_ins : start ? start : NULL;
1343 if ( type == OA_BINOP
1344 || type == OA_LISTOP
1348 cLISTOPx(parent)->op_last = lastop;
1351 OpLASTSIB_set(lastop, parent);
1353 return last_del ? first : NULL;
1356 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1360 #ifdef PERL_OP_PARENT
1363 =for apidoc op_parent
1365 Returns the parent OP of o, if it has a parent. Returns NULL otherwise.
1366 This function is only available on perls built with C<-DPERL_OP_PARENT>.
1372 Perl_op_parent(OP *o)
1374 PERL_ARGS_ASSERT_OP_PARENT;
1375 while (OpHAS_SIBLING(o))
1377 return o->op_sibparent;
1383 /* replace the sibling following start with a new UNOP, which becomes
1384 * the parent of the original sibling; e.g.
1386 * op_sibling_newUNOP(P, A, unop-args...)
1394 * where U is the new UNOP.
1396 * parent and start args are the same as for op_sibling_splice();
1397 * type and flags args are as newUNOP().
1399 * Returns the new UNOP.
1403 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1407 kid = op_sibling_splice(parent, start, 1, NULL);
1408 newop = newUNOP(type, flags, kid);
1409 op_sibling_splice(parent, start, 0, newop);
1414 /* lowest-level newLOGOP-style function - just allocates and populates
1415 * the struct. Higher-level stuff should be done by S_new_logop() /
1416 * newLOGOP(). This function exists mainly to avoid op_first assignment
1417 * being spread throughout this file.
1421 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1426 NewOp(1101, logop, 1, LOGOP);
1427 OpTYPE_set(logop, type);
1428 logop->op_first = first;
1429 logop->op_other = other;
1430 logop->op_flags = OPf_KIDS;
1431 while (kid && OpHAS_SIBLING(kid))
1432 kid = OpSIBLING(kid);
1434 OpLASTSIB_set(kid, (OP*)logop);
1439 /* Contextualizers */
1442 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1444 Applies a syntactic context to an op tree representing an expression.
1445 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1446 or C<G_VOID> to specify the context to apply. The modified op tree
1453 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1455 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1457 case G_SCALAR: return scalar(o);
1458 case G_ARRAY: return list(o);
1459 case G_VOID: return scalarvoid(o);
1461 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1468 =for apidoc Am|OP*|op_linklist|OP *o
1469 This function is the implementation of the L</LINKLIST> macro. It should
1470 not be called directly.
1476 Perl_op_linklist(pTHX_ OP *o)
1480 PERL_ARGS_ASSERT_OP_LINKLIST;
1485 /* establish postfix order */
1486 first = cUNOPo->op_first;
1489 o->op_next = LINKLIST(first);
1492 OP *sibl = OpSIBLING(kid);
1494 kid->op_next = LINKLIST(sibl);
1509 S_scalarkids(pTHX_ OP *o)
1511 if (o && o->op_flags & OPf_KIDS) {
1513 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1520 S_scalarboolean(pTHX_ OP *o)
1522 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1524 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1525 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1526 if (ckWARN(WARN_SYNTAX)) {
1527 const line_t oldline = CopLINE(PL_curcop);
1529 if (PL_parser && PL_parser->copline != NOLINE) {
1530 /* This ensures that warnings are reported at the first line
1531 of the conditional, not the last. */
1532 CopLINE_set(PL_curcop, PL_parser->copline);
1534 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1535 CopLINE_set(PL_curcop, oldline);
1542 S_op_varname(pTHX_ const OP *o)
1545 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1546 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1548 const char funny = o->op_type == OP_PADAV
1549 || o->op_type == OP_RV2AV ? '@' : '%';
1550 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1552 if (cUNOPo->op_first->op_type != OP_GV
1553 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1555 return varname(gv, funny, 0, NULL, 0, 1);
1558 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1563 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1564 { /* or not so pretty :-) */
1565 if (o->op_type == OP_CONST) {
1567 if (SvPOK(*retsv)) {
1569 *retsv = sv_newmortal();
1570 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1571 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1573 else if (!SvOK(*retsv))
1576 else *retpv = "...";
1580 S_scalar_slice_warning(pTHX_ const OP *o)
1584 o->op_type == OP_HSLICE ? '{' : '[';
1586 o->op_type == OP_HSLICE ? '}' : ']';
1588 SV *keysv = NULL; /* just to silence compiler warnings */
1589 const char *key = NULL;
1591 if (!(o->op_private & OPpSLICEWARNING))
1593 if (PL_parser && PL_parser->error_count)
1594 /* This warning can be nonsensical when there is a syntax error. */
1597 kid = cLISTOPo->op_first;
1598 kid = OpSIBLING(kid); /* get past pushmark */
1599 /* weed out false positives: any ops that can return lists */
1600 switch (kid->op_type) {
1626 /* Don't warn if we have a nulled list either. */
1627 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1630 assert(OpSIBLING(kid));
1631 name = S_op_varname(aTHX_ OpSIBLING(kid));
1632 if (!name) /* XS module fiddling with the op tree */
1634 S_op_pretty(aTHX_ kid, &keysv, &key);
1635 assert(SvPOK(name));
1636 sv_chop(name,SvPVX(name)+1);
1638 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1639 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1640 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1642 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1643 lbrack, key, rbrack);
1645 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1646 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1647 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1649 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1650 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1654 Perl_scalar(pTHX_ OP *o)
1658 /* assumes no premature commitment */
1659 if (!o || (PL_parser && PL_parser->error_count)
1660 || (o->op_flags & OPf_WANT)
1661 || o->op_type == OP_RETURN)
1666 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1668 switch (o->op_type) {
1670 scalar(cBINOPo->op_first);
1671 if (o->op_private & OPpREPEAT_DOLIST) {
1672 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1673 assert(kid->op_type == OP_PUSHMARK);
1674 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1675 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1676 o->op_private &=~ OPpREPEAT_DOLIST;
1683 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
1693 if (o->op_flags & OPf_KIDS) {
1694 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
1700 kid = cLISTOPo->op_first;
1702 kid = OpSIBLING(kid);
1705 OP *sib = OpSIBLING(kid);
1706 if (sib && kid->op_type != OP_LEAVEWHEN
1707 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
1708 || ( sib->op_targ != OP_NEXTSTATE
1709 && sib->op_targ != OP_DBSTATE )))
1715 PL_curcop = &PL_compiling;
1720 kid = cLISTOPo->op_first;
1723 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1728 /* Warn about scalar context */
1729 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1730 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1733 const char *key = NULL;
1735 /* This warning can be nonsensical when there is a syntax error. */
1736 if (PL_parser && PL_parser->error_count)
1739 if (!ckWARN(WARN_SYNTAX)) break;
1741 kid = cLISTOPo->op_first;
1742 kid = OpSIBLING(kid); /* get past pushmark */
1743 assert(OpSIBLING(kid));
1744 name = S_op_varname(aTHX_ OpSIBLING(kid));
1745 if (!name) /* XS module fiddling with the op tree */
1747 S_op_pretty(aTHX_ kid, &keysv, &key);
1748 assert(SvPOK(name));
1749 sv_chop(name,SvPVX(name)+1);
1751 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1752 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1753 "%%%"SVf"%c%s%c in scalar context better written "
1755 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1756 lbrack, key, rbrack);
1758 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1759 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1760 "%%%"SVf"%c%"SVf"%c in scalar context better "
1761 "written as $%"SVf"%c%"SVf"%c",
1762 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1763 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1770 Perl_scalarvoid(pTHX_ OP *arg)
1776 SSize_t defer_stack_alloc = 0;
1777 SSize_t defer_ix = -1;
1778 OP **defer_stack = NULL;
1781 PERL_ARGS_ASSERT_SCALARVOID;
1784 SV *useless_sv = NULL;
1785 const char* useless = NULL;
1787 if (o->op_type == OP_NEXTSTATE
1788 || o->op_type == OP_DBSTATE
1789 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1790 || o->op_targ == OP_DBSTATE)))
1791 PL_curcop = (COP*)o; /* for warning below */
1793 /* assumes no premature commitment */
1794 want = o->op_flags & OPf_WANT;
1795 if ((want && want != OPf_WANT_SCALAR)
1796 || (PL_parser && PL_parser->error_count)
1797 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1802 if ((o->op_private & OPpTARGET_MY)
1803 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1805 /* newASSIGNOP has already applied scalar context, which we
1806 leave, as if this op is inside SASSIGN. */
1810 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1812 switch (o->op_type) {
1814 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1818 if (o->op_flags & OPf_STACKED)
1820 if (o->op_type == OP_REPEAT)
1821 scalar(cBINOPo->op_first);
1824 if (o->op_private == 4)
1859 case OP_GETSOCKNAME:
1860 case OP_GETPEERNAME:
1865 case OP_GETPRIORITY:
1890 useless = OP_DESC(o);
1900 case OP_AELEMFAST_LEX:
1904 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1905 /* Otherwise it's "Useless use of grep iterator" */
1906 useless = OP_DESC(o);
1910 kid = cLISTOPo->op_first;
1911 if (kid && kid->op_type == OP_PUSHRE
1913 && !(o->op_flags & OPf_STACKED)
1915 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
1917 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
1920 useless = OP_DESC(o);
1924 kid = cUNOPo->op_first;
1925 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1926 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1929 useless = "negative pattern binding (!~)";
1933 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1934 useless = "non-destructive substitution (s///r)";
1938 useless = "non-destructive transliteration (tr///r)";
1945 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1946 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
1947 useless = "a variable";
1952 if (cSVOPo->op_private & OPpCONST_STRICT)
1953 no_bareword_allowed(o);
1955 if (ckWARN(WARN_VOID)) {
1957 /* don't warn on optimised away booleans, eg
1958 * use constant Foo, 5; Foo || print; */
1959 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1961 /* the constants 0 and 1 are permitted as they are
1962 conventionally used as dummies in constructs like
1963 1 while some_condition_with_side_effects; */
1964 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
1966 else if (SvPOK(sv)) {
1967 SV * const dsv = newSVpvs("");
1969 = Perl_newSVpvf(aTHX_
1971 pv_pretty(dsv, SvPVX_const(sv),
1972 SvCUR(sv), 32, NULL, NULL,
1974 | PERL_PV_ESCAPE_NOCLEAR
1975 | PERL_PV_ESCAPE_UNI_DETECT));
1976 SvREFCNT_dec_NN(dsv);
1978 else if (SvOK(sv)) {
1979 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
1982 useless = "a constant (undef)";
1985 op_null(o); /* don't execute or even remember it */
1989 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
1993 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
1997 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2001 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2006 UNOP *refgen, *rv2cv;
2009 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2012 rv2gv = ((BINOP *)o)->op_last;
2013 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2016 refgen = (UNOP *)((BINOP *)o)->op_first;
2018 if (!refgen || (refgen->op_type != OP_REFGEN
2019 && refgen->op_type != OP_SREFGEN))
2022 exlist = (LISTOP *)refgen->op_first;
2023 if (!exlist || exlist->op_type != OP_NULL
2024 || exlist->op_targ != OP_LIST)
2027 if (exlist->op_first->op_type != OP_PUSHMARK
2028 && exlist->op_first != exlist->op_last)
2031 rv2cv = (UNOP*)exlist->op_last;
2033 if (rv2cv->op_type != OP_RV2CV)
2036 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2037 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2038 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2040 o->op_private |= OPpASSIGN_CV_TO_GV;
2041 rv2gv->op_private |= OPpDONT_INIT_GV;
2042 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2054 kid = cLOGOPo->op_first;
2055 if (kid->op_type == OP_NOT
2056 && (kid->op_flags & OPf_KIDS)) {
2057 if (o->op_type == OP_AND) {
2058 OpTYPE_set(o, OP_OR);
2060 OpTYPE_set(o, OP_AND);
2070 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2071 if (!(kid->op_flags & OPf_KIDS))
2078 if (o->op_flags & OPf_STACKED)
2085 if (!(o->op_flags & OPf_KIDS))
2096 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2097 if (!(kid->op_flags & OPf_KIDS))
2103 /* If the first kid after pushmark is something that the padrange
2104 optimisation would reject, then null the list and the pushmark.
2106 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2107 && ( !(kid = OpSIBLING(kid))
2108 || ( kid->op_type != OP_PADSV
2109 && kid->op_type != OP_PADAV
2110 && kid->op_type != OP_PADHV)
2111 || kid->op_private & ~OPpLVAL_INTRO
2112 || !(kid = OpSIBLING(kid))
2113 || ( kid->op_type != OP_PADSV
2114 && kid->op_type != OP_PADAV
2115 && kid->op_type != OP_PADHV)
2116 || kid->op_private & ~OPpLVAL_INTRO)
2118 op_null(cUNOPo->op_first); /* NULL the pushmark */
2119 op_null(o); /* NULL the list */
2131 /* mortalise it, in case warnings are fatal. */
2132 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2133 "Useless use of %"SVf" in void context",
2134 SVfARG(sv_2mortal(useless_sv)));
2137 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2138 "Useless use of %s in void context",
2141 } while ( (o = POP_DEFERRED_OP()) );
2143 Safefree(defer_stack);
2149 S_listkids(pTHX_ OP *o)
2151 if (o && o->op_flags & OPf_KIDS) {
2153 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2160 Perl_list(pTHX_ OP *o)
2164 /* assumes no premature commitment */
2165 if (!o || (o->op_flags & OPf_WANT)
2166 || (PL_parser && PL_parser->error_count)
2167 || o->op_type == OP_RETURN)
2172 if ((o->op_private & OPpTARGET_MY)
2173 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2175 return o; /* As if inside SASSIGN */
2178 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2180 switch (o->op_type) {
2182 list(cBINOPo->op_first);
2185 if (o->op_private & OPpREPEAT_DOLIST
2186 && !(o->op_flags & OPf_STACKED))
2188 list(cBINOPo->op_first);
2189 kid = cBINOPo->op_last;
2190 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2191 && SvIVX(kSVOP_sv) == 1)
2193 op_null(o); /* repeat */
2194 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2196 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2203 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2211 if (!(o->op_flags & OPf_KIDS))
2213 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2214 list(cBINOPo->op_first);
2215 return gen_constant_list(o);
2221 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2222 op_null(cUNOPo->op_first); /* NULL the pushmark */
2223 op_null(o); /* NULL the list */
2228 kid = cLISTOPo->op_first;
2230 kid = OpSIBLING(kid);
2233 OP *sib = OpSIBLING(kid);
2234 if (sib && kid->op_type != OP_LEAVEWHEN)
2240 PL_curcop = &PL_compiling;
2244 kid = cLISTOPo->op_first;
2251 S_scalarseq(pTHX_ OP *o)
2254 const OPCODE type = o->op_type;
2256 if (type == OP_LINESEQ || type == OP_SCOPE ||
2257 type == OP_LEAVE || type == OP_LEAVETRY)
2260 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2261 if ((sib = OpSIBLING(kid))
2262 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2263 || ( sib->op_targ != OP_NEXTSTATE
2264 && sib->op_targ != OP_DBSTATE )))
2269 PL_curcop = &PL_compiling;
2271 o->op_flags &= ~OPf_PARENS;
2272 if (PL_hints & HINT_BLOCK_SCOPE)
2273 o->op_flags |= OPf_PARENS;
2276 o = newOP(OP_STUB, 0);
2281 S_modkids(pTHX_ OP *o, I32 type)
2283 if (o && o->op_flags & OPf_KIDS) {
2285 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2286 op_lvalue(kid, type);
2292 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2293 * const fields. Also, convert CONST keys to HEK-in-SVs.
2294 * rop is the op that retrieves the hash;
2295 * key_op is the first key
2299 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op)
2305 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2307 if (rop->op_first->op_type == OP_PADSV)
2308 /* @$hash{qw(keys here)} */
2309 rop = (UNOP*)rop->op_first;
2311 /* @{$hash}{qw(keys here)} */
2312 if (rop->op_first->op_type == OP_SCOPE
2313 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2315 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2322 lexname = NULL; /* just to silence compiler warnings */
2323 fields = NULL; /* just to silence compiler warnings */
2327 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2328 SvPAD_TYPED(lexname))
2329 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2330 && isGV(*fields) && GvHV(*fields);
2332 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2334 if (key_op->op_type != OP_CONST)
2336 svp = cSVOPx_svp(key_op);
2338 /* Make the CONST have a shared SV */
2339 if ( !SvIsCOW_shared_hash(sv = *svp)
2340 && SvTYPE(sv) < SVt_PVMG
2345 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2346 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2347 SvREFCNT_dec_NN(sv);
2352 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2354 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2355 "in variable %"PNf" of type %"HEKf,
2356 SVfARG(*svp), PNfARG(lexname),
2357 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2364 =for apidoc finalize_optree
2366 This function finalizes the optree. Should be called directly after
2367 the complete optree is built. It does some additional
2368 checking which can't be done in the normal ck_xxx functions and makes
2369 the tree thread-safe.
2374 Perl_finalize_optree(pTHX_ OP* o)
2376 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2379 SAVEVPTR(PL_curcop);
2387 /* Relocate sv to the pad for thread safety.
2388 * Despite being a "constant", the SV is written to,
2389 * for reference counts, sv_upgrade() etc. */
2390 PERL_STATIC_INLINE void
2391 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2394 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2396 ix = pad_alloc(OP_CONST, SVf_READONLY);
2397 SvREFCNT_dec(PAD_SVl(ix));
2398 PAD_SETSV(ix, *svp);
2399 /* XXX I don't know how this isn't readonly already. */
2400 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2408 S_finalize_op(pTHX_ OP* o)
2410 PERL_ARGS_ASSERT_FINALIZE_OP;
2413 switch (o->op_type) {
2416 PL_curcop = ((COP*)o); /* for warnings */
2419 if (OpHAS_SIBLING(o)) {
2420 OP *sib = OpSIBLING(o);
2421 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2422 && ckWARN(WARN_EXEC)
2423 && OpHAS_SIBLING(sib))
2425 const OPCODE type = OpSIBLING(sib)->op_type;
2426 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2427 const line_t oldline = CopLINE(PL_curcop);
2428 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2429 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2430 "Statement unlikely to be reached");
2431 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2432 "\t(Maybe you meant system() when you said exec()?)\n");
2433 CopLINE_set(PL_curcop, oldline);
2440 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2441 GV * const gv = cGVOPo_gv;
2442 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2443 /* XXX could check prototype here instead of just carping */
2444 SV * const sv = sv_newmortal();
2445 gv_efullname3(sv, gv, NULL);
2446 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2447 "%"SVf"() called too early to check prototype",
2454 if (cSVOPo->op_private & OPpCONST_STRICT)
2455 no_bareword_allowed(o);
2459 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2464 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2465 case OP_METHOD_NAMED:
2466 case OP_METHOD_SUPER:
2467 case OP_METHOD_REDIR:
2468 case OP_METHOD_REDIR_SUPER:
2469 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2478 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2481 rop = (UNOP*)((BINOP*)o)->op_first;
2486 S_scalar_slice_warning(aTHX_ o);
2490 kid = OpSIBLING(cLISTOPo->op_first);
2491 if (/* I bet there's always a pushmark... */
2492 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2493 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2498 key_op = (SVOP*)(kid->op_type == OP_CONST
2500 : OpSIBLING(kLISTOP->op_first));
2502 rop = (UNOP*)((LISTOP*)o)->op_last;
2505 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2507 S_check_hash_fields_and_hekify(aTHX_ rop, key_op);
2511 S_scalar_slice_warning(aTHX_ o);
2515 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2516 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2523 if (o->op_flags & OPf_KIDS) {
2527 /* check that op_last points to the last sibling, and that
2528 * the last op_sibling/op_sibparent field points back to the
2529 * parent, and that the only ops with KIDS are those which are
2530 * entitled to them */
2531 U32 type = o->op_type;
2535 if (type == OP_NULL) {
2537 /* ck_glob creates a null UNOP with ex-type GLOB
2538 * (which is a list op. So pretend it wasn't a listop */
2539 if (type == OP_GLOB)
2542 family = PL_opargs[type] & OA_CLASS_MASK;
2544 has_last = ( family == OA_BINOP
2545 || family == OA_LISTOP
2546 || family == OA_PMOP
2547 || family == OA_LOOP
2549 assert( has_last /* has op_first and op_last, or ...
2550 ... has (or may have) op_first: */
2551 || family == OA_UNOP
2552 || family == OA_UNOP_AUX
2553 || family == OA_LOGOP
2554 || family == OA_BASEOP_OR_UNOP
2555 || family == OA_FILESTATOP
2556 || family == OA_LOOPEXOP
2557 || family == OA_METHOP
2558 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */
2559 || type == OP_SASSIGN
2560 || type == OP_CUSTOM
2561 || type == OP_NULL /* new_logop does this */
2564 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2565 # ifdef PERL_OP_PARENT
2566 if (!OpHAS_SIBLING(kid)) {
2568 assert(kid == cLISTOPo->op_last);
2569 assert(kid->op_sibparent == o);
2572 if (has_last && !OpHAS_SIBLING(kid))
2573 assert(kid == cLISTOPo->op_last);
2578 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
2584 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2586 Propagate lvalue ("modifiable") context to an op and its children.
2587 C<type> represents the context type, roughly based on the type of op that
2588 would do the modifying, although C<local()> is represented by OP_NULL,
2589 because it has no op type of its own (it is signalled by a flag on
2592 This function detects things that can't be modified, such as C<$x+1>, and
2593 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2594 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2596 It also flags things that need to behave specially in an lvalue context,
2597 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2603 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2606 PadnameLVALUE_on(pn);
2607 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2610 assert(CvPADLIST(cv));
2612 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2613 assert(PadnameLEN(pn));
2614 PadnameLVALUE_on(pn);
2619 S_vivifies(const OPCODE type)
2622 case OP_RV2AV: case OP_ASLICE:
2623 case OP_RV2HV: case OP_KVASLICE:
2624 case OP_RV2SV: case OP_HSLICE:
2625 case OP_AELEMFAST: case OP_KVHSLICE:
2634 S_lvref(pTHX_ OP *o, I32 type)
2638 switch (o->op_type) {
2640 for (kid = OpSIBLING(cUNOPo->op_first); kid;
2641 kid = OpSIBLING(kid))
2642 S_lvref(aTHX_ kid, type);
2647 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2648 o->op_flags |= OPf_STACKED;
2649 if (o->op_flags & OPf_PARENS) {
2650 if (o->op_private & OPpLVAL_INTRO) {
2651 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2652 "localized parenthesized array in list assignment"));
2656 OpTYPE_set(o, OP_LVAVREF);
2657 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2658 o->op_flags |= OPf_MOD|OPf_REF;
2661 o->op_private |= OPpLVREF_AV;
2664 kid = cUNOPo->op_first;
2665 if (kid->op_type == OP_NULL)
2666 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
2668 o->op_private = OPpLVREF_CV;
2669 if (kid->op_type == OP_GV)
2670 o->op_flags |= OPf_STACKED;
2671 else if (kid->op_type == OP_PADCV) {
2672 o->op_targ = kid->op_targ;
2674 op_free(cUNOPo->op_first);
2675 cUNOPo->op_first = NULL;
2676 o->op_flags &=~ OPf_KIDS;
2681 if (o->op_flags & OPf_PARENS) {
2683 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2684 "parenthesized hash in list assignment"));
2687 o->op_private |= OPpLVREF_HV;
2691 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2692 o->op_flags |= OPf_STACKED;
2695 if (o->op_flags & OPf_PARENS) goto parenhash;
2696 o->op_private |= OPpLVREF_HV;
2699 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2702 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2703 if (o->op_flags & OPf_PARENS) goto slurpy;
2704 o->op_private |= OPpLVREF_AV;
2708 o->op_private |= OPpLVREF_ELEM;
2709 o->op_flags |= OPf_STACKED;
2713 OpTYPE_set(o, OP_LVREFSLICE);
2714 o->op_private &= OPpLVAL_INTRO|OPpLVREF_ELEM;
2717 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2719 else if (!(o->op_flags & OPf_KIDS))
2721 if (o->op_targ != OP_LIST) {
2722 S_lvref(aTHX_ cBINOPo->op_first, type);
2727 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
2728 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2729 S_lvref(aTHX_ kid, type);
2733 if (o->op_flags & OPf_PARENS)
2738 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2739 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2740 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2745 OpTYPE_set(o, OP_LVREF);
2747 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2748 if (type == OP_ENTERLOOP)
2749 o->op_private |= OPpLVREF_ITER;
2753 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2757 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2760 if (!o || (PL_parser && PL_parser->error_count))
2763 if ((o->op_private & OPpTARGET_MY)
2764 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2769 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2771 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2773 switch (o->op_type) {
2778 if ((o->op_flags & OPf_PARENS))
2782 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2783 !(o->op_flags & OPf_STACKED)) {
2784 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
2785 assert(cUNOPo->op_first->op_type == OP_NULL);
2786 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2789 else { /* lvalue subroutine call */
2790 o->op_private |= OPpLVAL_INTRO;
2791 PL_modcount = RETURN_UNLIMITED_NUMBER;
2792 if (type == OP_GREPSTART || type == OP_ENTERSUB
2793 || type == OP_REFGEN || type == OP_LEAVESUBLV) {
2794 /* Potential lvalue context: */
2795 o->op_private |= OPpENTERSUB_INARGS;
2798 else { /* Compile-time error message: */
2799 OP *kid = cUNOPo->op_first;
2803 if (kid->op_type != OP_PUSHMARK) {
2804 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2806 "panic: unexpected lvalue entersub "
2807 "args: type/targ %ld:%"UVuf,
2808 (long)kid->op_type, (UV)kid->op_targ);
2809 kid = kLISTOP->op_first;
2811 while (OpHAS_SIBLING(kid))
2812 kid = OpSIBLING(kid);
2813 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2814 break; /* Postpone until runtime */
2817 kid = kUNOP->op_first;
2818 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2819 kid = kUNOP->op_first;
2820 if (kid->op_type == OP_NULL)
2822 "Unexpected constant lvalue entersub "
2823 "entry via type/targ %ld:%"UVuf,
2824 (long)kid->op_type, (UV)kid->op_targ);
2825 if (kid->op_type != OP_GV) {
2832 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2833 ? MUTABLE_CV(SvRV(gv))
2844 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2845 /* grep, foreach, subcalls, refgen */
2846 if (type == OP_GREPSTART || type == OP_ENTERSUB
2847 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2849 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2850 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2852 : (o->op_type == OP_ENTERSUB
2853 ? "non-lvalue subroutine call"
2855 type ? PL_op_desc[type] : "local"));
2868 case OP_RIGHT_SHIFT:
2877 if (!(o->op_flags & OPf_STACKED))
2883 if (o->op_flags & OPf_STACKED) {
2887 if (!(o->op_private & OPpREPEAT_DOLIST))
2890 const I32 mods = PL_modcount;
2891 modkids(cBINOPo->op_first, type);
2892 if (type != OP_AASSIGN)
2894 kid = cBINOPo->op_last;
2895 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
2896 const IV iv = SvIV(kSVOP_sv);
2897 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
2899 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
2902 PL_modcount = RETURN_UNLIMITED_NUMBER;
2908 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2909 op_lvalue(kid, type);
2914 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2915 PL_modcount = RETURN_UNLIMITED_NUMBER;
2916 return o; /* Treat \(@foo) like ordinary list. */
2920 if (scalar_mod_type(o, type))
2922 ref(cUNOPo->op_first, o->op_type);
2929 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2930 if (type == OP_LEAVESUBLV && (
2931 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2932 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2934 o->op_private |= OPpMAYBE_LVSUB;
2938 PL_modcount = RETURN_UNLIMITED_NUMBER;
2942 if (type == OP_LEAVESUBLV)
2943 o->op_private |= OPpMAYBE_LVSUB;
2946 PL_hints |= HINT_BLOCK_SCOPE;
2947 if (type == OP_LEAVESUBLV)
2948 o->op_private |= OPpMAYBE_LVSUB;
2952 ref(cUNOPo->op_first, o->op_type);
2956 PL_hints |= HINT_BLOCK_SCOPE;
2966 case OP_AELEMFAST_LEX:
2973 PL_modcount = RETURN_UNLIMITED_NUMBER;
2974 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2975 return o; /* Treat \(@foo) like ordinary list. */
2976 if (scalar_mod_type(o, type))
2978 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2979 && type == OP_LEAVESUBLV)
2980 o->op_private |= OPpMAYBE_LVSUB;
2984 if (!type) /* local() */
2985 Perl_croak(aTHX_ "Can't localize lexical variable %"PNf,
2986 PNfARG(PAD_COMPNAME(o->op_targ)));
2987 if (!(o->op_private & OPpLVAL_INTRO)
2988 || ( type != OP_SASSIGN && type != OP_AASSIGN
2989 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
2990 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
2998 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
3002 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
3008 if (type == OP_LEAVESUBLV)
3009 o->op_private |= OPpMAYBE_LVSUB;
3010 if (o->op_flags & OPf_KIDS)
3011 op_lvalue(OpSIBLING(cBINOPo->op_first), type);
3016 ref(cBINOPo->op_first, o->op_type);
3017 if (type == OP_ENTERSUB &&
3018 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3019 o->op_private |= OPpLVAL_DEFER;
3020 if (type == OP_LEAVESUBLV)
3021 o->op_private |= OPpMAYBE_LVSUB;
3028 o->op_private |= OPpLVALUE;
3034 if (o->op_flags & OPf_KIDS)
3035 op_lvalue(cLISTOPo->op_last, type);
3040 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3042 else if (!(o->op_flags & OPf_KIDS))
3044 if (o->op_targ != OP_LIST) {
3045 op_lvalue(cBINOPo->op_first, type);
3051 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3052 /* elements might be in void context because the list is
3053 in scalar context or because they are attribute sub calls */
3054 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
3055 op_lvalue(kid, type);
3063 if (type == OP_LEAVESUBLV
3064 || !S_vivifies(cLOGOPo->op_first->op_type))
3065 op_lvalue(cLOGOPo->op_first, type);
3066 if (type == OP_LEAVESUBLV
3067 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3068 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
3072 if (type != OP_AASSIGN && type != OP_SASSIGN
3073 && type != OP_ENTERLOOP)
3075 /* Don’t bother applying lvalue context to the ex-list. */
3076 kid = cUNOPx(cUNOPo->op_first)->op_first;
3077 assert (!OpHAS_SIBLING(kid));
3080 if (type != OP_AASSIGN) goto nomod;
3081 kid = cUNOPo->op_first;
3084 const U8 ec = PL_parser ? PL_parser->error_count : 0;
3085 S_lvref(aTHX_ kid, type);
3086 if (!PL_parser || PL_parser->error_count == ec) {
3087 if (!FEATURE_REFALIASING_IS_ENABLED)
3089 "Experimental aliasing via reference not enabled");
3090 Perl_ck_warner_d(aTHX_
3091 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3092 "Aliasing via reference is experimental");
3095 if (o->op_type == OP_REFGEN)
3096 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3101 kid = cLISTOPo->op_first;
3102 if (kid && kid->op_type == OP_PUSHRE &&
3104 || o->op_flags & OPf_STACKED
3106 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
3108 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
3111 /* This is actually @array = split. */
3112 PL_modcount = RETURN_UNLIMITED_NUMBER;
3118 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3122 /* [20011101.069] File test operators interpret OPf_REF to mean that
3123 their argument is a filehandle; thus \stat(".") should not set
3125 if (type == OP_REFGEN &&
3126 PL_check[o->op_type] == Perl_ck_ftst)
3129 if (type != OP_LEAVESUBLV)
3130 o->op_flags |= OPf_MOD;
3132 if (type == OP_AASSIGN || type == OP_SASSIGN)
3133 o->op_flags |= OPf_SPECIAL|OPf_REF;
3134 else if (!type) { /* local() */
3137 o->op_private |= OPpLVAL_INTRO;
3138 o->op_flags &= ~OPf_SPECIAL;
3139 PL_hints |= HINT_BLOCK_SCOPE;
3144 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3145 "Useless localization of %s", OP_DESC(o));
3148 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3149 && type != OP_LEAVESUBLV)
3150 o->op_flags |= OPf_REF;
3155 S_scalar_mod_type(const OP *o, I32 type)
3160 if (o && o->op_type == OP_RV2GV)
3184 case OP_RIGHT_SHIFT:
3205 S_is_handle_constructor(const OP *o, I32 numargs)
3207 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3209 switch (o->op_type) {
3217 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3230 S_refkids(pTHX_ OP *o, I32 type)
3232 if (o && o->op_flags & OPf_KIDS) {
3234 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3241 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3246 PERL_ARGS_ASSERT_DOREF;
3248 if (PL_parser && PL_parser->error_count)
3251 switch (o->op_type) {
3253 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3254 !(o->op_flags & OPf_STACKED)) {
3255 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
3256 assert(cUNOPo->op_first->op_type == OP_NULL);
3257 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
3258 o->op_flags |= OPf_SPECIAL;
3260 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3261 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3262 : type == OP_RV2HV ? OPpDEREF_HV
3264 o->op_flags |= OPf_MOD;
3270 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3271 doref(kid, type, set_op_ref);
3274 if (type == OP_DEFINED)
3275 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3276 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3279 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3280 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3281 : type == OP_RV2HV ? OPpDEREF_HV
3283 o->op_flags |= OPf_MOD;
3290 o->op_flags |= OPf_REF;
3293 if (type == OP_DEFINED)
3294 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3295 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3301 o->op_flags |= OPf_REF;
3306 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3308 doref(cBINOPo->op_first, type, set_op_ref);
3312 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3313 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3314 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3315 : type == OP_RV2HV ? OPpDEREF_HV
3317 o->op_flags |= OPf_MOD;
3327 if (!(o->op_flags & OPf_KIDS))
3329 doref(cLISTOPo->op_last, type, set_op_ref);
3339 S_dup_attrlist(pTHX_ OP *o)
3343 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3345 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3346 * where the first kid is OP_PUSHMARK and the remaining ones
3347 * are OP_CONST. We need to push the OP_CONST values.
3349 if (o->op_type == OP_CONST)
3350 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3352 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3354 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
3355 if (o->op_type == OP_CONST)
3356 rop = op_append_elem(OP_LIST, rop,
3357 newSVOP(OP_CONST, o->op_flags,
3358 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3365 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3367 PERL_ARGS_ASSERT_APPLY_ATTRS;
3369 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
3371 /* fake up C<use attributes $pkg,$rv,@attrs> */
3373 #define ATTRSMODULE "attributes"
3374 #define ATTRSMODULE_PM "attributes.pm"
3377 aTHX_ PERL_LOADMOD_IMPORT_OPS,
3378 newSVpvs(ATTRSMODULE),
3380 op_prepend_elem(OP_LIST,
3381 newSVOP(OP_CONST, 0, stashsv),
3382 op_prepend_elem(OP_LIST,
3383 newSVOP(OP_CONST, 0,
3385 dup_attrlist(attrs))));
3390 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3392 OP *pack, *imop, *arg;
3393 SV *meth, *stashsv, **svp;
3395 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3400 assert(target->op_type == OP_PADSV ||
3401 target->op_type == OP_PADHV ||
3402 target->op_type == OP_PADAV);
3404 /* Ensure that attributes.pm is loaded. */
3405 /* Don't force the C<use> if we don't need it. */
3406 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3407 if (svp && *svp != &PL_sv_undef)
3408 NOOP; /* already in %INC */
3410 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3411 newSVpvs(ATTRSMODULE), NULL);
3413 /* Need package name for method call. */
3414 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3416 /* Build up the real arg-list. */
3417 stashsv = newSVhek(HvNAME_HEK(stash));
3419 arg = newOP(OP_PADSV, 0);
3420 arg->op_targ = target->op_targ;
3421 arg = op_prepend_elem(OP_LIST,
3422 newSVOP(OP_CONST, 0, stashsv),
3423 op_prepend_elem(OP_LIST,
3424 newUNOP(OP_REFGEN, 0,
3426 dup_attrlist(attrs)));
3428 /* Fake up a method call to import */
3429 meth = newSVpvs_share("import");
3430 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3431 op_append_elem(OP_LIST,
3432 op_prepend_elem(OP_LIST, pack, arg),
3433 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3435 /* Combine the ops. */
3436 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3440 =notfor apidoc apply_attrs_string
3442 Attempts to apply a list of attributes specified by the C<attrstr> and
3443 C<len> arguments to the subroutine identified by the C<cv> argument which
3444 is expected to be associated with the package identified by the C<stashpv>
3445 argument (see L<attributes>). It gets this wrong, though, in that it
3446 does not correctly identify the boundaries of the individual attribute
3447 specifications within C<attrstr>. This is not really intended for the
3448 public API, but has to be listed here for systems such as AIX which
3449 need an explicit export list for symbols. (It's called from XS code
3450 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3451 to respect attribute syntax properly would be welcome.
3457 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3458 const char *attrstr, STRLEN len)
3462 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3465 len = strlen(attrstr);
3469 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3471 const char * const sstr = attrstr;
3472 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3473 attrs = op_append_elem(OP_LIST, attrs,
3474 newSVOP(OP_CONST, 0,
3475 newSVpvn(sstr, attrstr-sstr)));
3479 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3480 newSVpvs(ATTRSMODULE),
3481 NULL, op_prepend_elem(OP_LIST,
3482 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3483 op_prepend_elem(OP_LIST,
3484 newSVOP(OP_CONST, 0,
3485 newRV(MUTABLE_SV(cv))),
3490 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3492 OP *new_proto = NULL;
3497 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3503 if (o->op_type == OP_CONST) {
3504 pv = SvPV(cSVOPo_sv, pvlen);
3505 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3506 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3507 SV ** const tmpo = cSVOPx_svp(o);
3508 SvREFCNT_dec(cSVOPo_sv);
3513 } else if (o->op_type == OP_LIST) {
3515 assert(o->op_flags & OPf_KIDS);
3516 lasto = cLISTOPo->op_first;
3517 assert(lasto->op_type == OP_PUSHMARK);
3518 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
3519 if (o->op_type == OP_CONST) {
3520 pv = SvPV(cSVOPo_sv, pvlen);
3521 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3522 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3523 SV ** const tmpo = cSVOPx_svp(o);
3524 SvREFCNT_dec(cSVOPo_sv);
3526 if (new_proto && ckWARN(WARN_MISC)) {
3528 const char * newp = SvPV(cSVOPo_sv, new_len);
3529 Perl_warner(aTHX_ packWARN(WARN_MISC),
3530 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
3531 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3537 /* excise new_proto from the list */
3538 op_sibling_splice(*attrs, lasto, 1, NULL);
3545 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3546 would get pulled in with no real need */
3547 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3556 svname = sv_newmortal();
3557 gv_efullname3(svname, name, NULL);
3559 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3560 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3562 svname = (SV *)name;
3563 if (ckWARN(WARN_ILLEGALPROTO))
3564 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3565 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3566 STRLEN old_len, new_len;
3567 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3568 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3570 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3571 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
3573 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3574 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3584 S_cant_declare(pTHX_ OP *o)
3586 if (o->op_type == OP_NULL
3587 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3588 o = cUNOPo->op_first;
3589 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3590 o->op_type == OP_NULL
3591 && o->op_flags & OPf_SPECIAL
3594 PL_parser->in_my == KEY_our ? "our" :
3595 PL_parser->in_my == KEY_state ? "state" :
3600 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3603 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3605 PERL_ARGS_ASSERT_MY_KID;
3607 if (!o || (PL_parser && PL_parser->error_count))
3612 if (type == OP_LIST) {
3614 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3615 my_kid(kid, attrs, imopsp);
3617 } else if (type == OP_UNDEF || type == OP_STUB) {
3619 } else if (type == OP_RV2SV || /* "our" declaration */
3621 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
3622 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3623 S_cant_declare(aTHX_ o);
3625 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3627 PL_parser->in_my = FALSE;
3628 PL_parser->in_my_stash = NULL;
3629 apply_attrs(GvSTASH(gv),
3630 (type == OP_RV2SV ? GvSV(gv) :
3631 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3632 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3635 o->op_private |= OPpOUR_INTRO;
3638 else if (type != OP_PADSV &&
3641 type != OP_PUSHMARK)
3643 S_cant_declare(aTHX_ o);
3646 else if (attrs && type != OP_PUSHMARK) {
3650 PL_parser->in_my = FALSE;
3651 PL_parser->in_my_stash = NULL;
3653 /* check for C<my Dog $spot> when deciding package */
3654 stash = PAD_COMPNAME_TYPE(o->op_targ);
3656 stash = PL_curstash;
3657 apply_attrs_my(stash, o, attrs, imopsp);
3659 o->op_flags |= OPf_MOD;
3660 o->op_private |= OPpLVAL_INTRO;
3662 o->op_private |= OPpPAD_STATE;
3667 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3670 int maybe_scalar = 0;
3672 PERL_ARGS_ASSERT_MY_ATTRS;
3674 /* [perl #17376]: this appears to be premature, and results in code such as
3675 C< our(%x); > executing in list mode rather than void mode */
3677 if (o->op_flags & OPf_PARENS)
3687 o = my_kid(o, attrs, &rops);
3689 if (maybe_scalar && o->op_type == OP_PADSV) {
3690 o = scalar(op_append_list(OP_LIST, rops, o));
3691 o->op_private |= OPpLVAL_INTRO;
3694 /* The listop in rops might have a pushmark at the beginning,
3695 which will mess up list assignment. */
3696 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3697 if (rops->op_type == OP_LIST &&
3698 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3700 OP * const pushmark = lrops->op_first;
3701 /* excise pushmark */
3702 op_sibling_splice(rops, NULL, 1, NULL);
3705 o = op_append_list(OP_LIST, o, rops);
3708 PL_parser->in_my = FALSE;
3709 PL_parser->in_my_stash = NULL;
3714 Perl_sawparens(pTHX_ OP *o)
3716 PERL_UNUSED_CONTEXT;
3718 o->op_flags |= OPf_PARENS;
3723 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3727 const OPCODE ltype = left->op_type;
3728 const OPCODE rtype = right->op_type;
3730 PERL_ARGS_ASSERT_BIND_MATCH;
3732 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3733 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3735 const char * const desc
3737 rtype == OP_SUBST || rtype == OP_TRANS
3738 || rtype == OP_TRANSR
3740 ? (int)rtype : OP_MATCH];
3741 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3743 S_op_varname(aTHX_ left);
3745 Perl_warner(aTHX_ packWARN(WARN_MISC),
3746 "Applying %s to %"SVf" will act on scalar(%"SVf")",
3747 desc, SVfARG(name), SVfARG(name));
3749 const char * const sample = (isary
3750 ? "@array" : "%hash");
3751 Perl_warner(aTHX_ packWARN(WARN_MISC),
3752 "Applying %s to %s will act on scalar(%s)",
3753 desc, sample, sample);
3757 if (rtype == OP_CONST &&
3758 cSVOPx(right)->op_private & OPpCONST_BARE &&
3759 cSVOPx(right)->op_private & OPpCONST_STRICT)
3761 no_bareword_allowed(right);
3764 /* !~ doesn't make sense with /r, so error on it for now */
3765 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3767 /* diag_listed_as: Using !~ with %s doesn't make sense */
3768 yyerror("Using !~ with s///r doesn't make sense");
3769 if (rtype == OP_TRANSR && type == OP_NOT)
3770 /* diag_listed_as: Using !~ with %s doesn't make sense */
3771 yyerror("Using !~ with tr///r doesn't make sense");
3773 ismatchop = (rtype == OP_MATCH ||
3774 rtype == OP_SUBST ||
3775 rtype == OP_TRANS || rtype == OP_TRANSR)
3776 && !(right->op_flags & OPf_SPECIAL);
3777 if (ismatchop && right->op_private & OPpTARGET_MY) {
3779 right->op_private &= ~OPpTARGET_MY;
3781 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
3782 if (left->op_type == OP_PADSV
3783 && !(left->op_private & OPpLVAL_INTRO))
3785 right->op_targ = left->op_targ;
3790 right->op_flags |= OPf_STACKED;
3791 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3792 ! (rtype == OP_TRANS &&
3793 right->op_private & OPpTRANS_IDENTICAL) &&
3794 ! (rtype == OP_SUBST &&
3795 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3796 left = op_lvalue(left, rtype);
3797 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3798 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
3800 o = op_prepend_elem(rtype, scalar(left), right);
3803 return newUNOP(OP_NOT, 0, scalar(o));
3807 return bind_match(type, left,
3808 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
3812 Perl_invert(pTHX_ OP *o)
3816 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3820 =for apidoc Amx|OP *|op_scope|OP *o
3822 Wraps up an op tree with some additional ops so that at runtime a dynamic
3823 scope will be created. The original ops run in the new dynamic scope,
3824 and then, provided that they exit normally, the scope will be unwound.
3825 The additional ops used to create and unwind the dynamic scope will
3826 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3827 instead if the ops are simple enough to not need the full dynamic scope
3834 Perl_op_scope(pTHX_ OP *o)
3838 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3839 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3840 OpTYPE_set(o, OP_LEAVE);
3842 else if (o->op_type == OP_LINESEQ) {
3844 OpTYPE_set(o, OP_SCOPE);
3845 kid = ((LISTOP*)o)->op_first;
3846 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3849 /* The following deals with things like 'do {1 for 1}' */
3850 kid = OpSIBLING(kid);
3852 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3857 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3863 Perl_op_unscope(pTHX_ OP *o)
3865 if (o && o->op_type == OP_LINESEQ) {
3866 OP *kid = cLISTOPo->op_first;
3867 for(; kid; kid = OpSIBLING(kid))
3868 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3875 =for apidoc Am|int|block_start|int full
3877 Handles compile-time scope entry.
3878 Arranges for hints to be restored on block
3879 exit and also handles pad sequence numbers to make lexical variables scope
3880 right. Returns a savestack index for use with C<block_end>.
3886 Perl_block_start(pTHX_ int full)
3888 const int retval = PL_savestack_ix;
3890 PL_compiling.cop_seq = PL_cop_seqmax;
3892 pad_block_start(full);
3894 PL_hints &= ~HINT_BLOCK_SCOPE;
3895 SAVECOMPILEWARNINGS();
3896 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3897 SAVEI32(PL_compiling.cop_seq);
3898 PL_compiling.cop_seq = 0;
3900 CALL_BLOCK_HOOKS(bhk_start, full);
3906 =for apidoc Am|OP *|block_end|I32 floor|OP *seq
3908 Handles compile-time scope exit. C<floor>
3909 is the savestack index returned by
3910 C<block_start>, and C<seq> is the body of the block. Returns the block,
3917 Perl_block_end(pTHX_ I32 floor, OP *seq)
3919 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3920 OP* retval = scalarseq(seq);
3923 /* XXX Is the null PL_parser check necessary here? */
3924 assert(PL_parser); /* Let’s find out under debugging builds. */
3925 if (PL_parser && PL_parser->parsed_sub) {
3926 o = newSTATEOP(0, NULL, NULL);
3928 retval = op_append_elem(OP_LINESEQ, retval, o);
3931 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3935 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3939 /* pad_leavemy has created a sequence of introcv ops for all my
3940 subs declared in the block. We have to replicate that list with
3941 clonecv ops, to deal with this situation:
3946 sub s1 { state sub foo { \&s2 } }
3949 Originally, I was going to have introcv clone the CV and turn
3950 off the stale flag. Since &s1 is declared before &s2, the
3951 introcv op for &s1 is executed (on sub entry) before the one for
3952 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3953 cloned, since it is a state sub) closes over &s2 and expects
3954 to see it in its outer CV’s pad. If the introcv op clones &s1,
3955 then &s2 is still marked stale. Since &s1 is not active, and
3956 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3957 ble will not stay shared’ warning. Because it is the same stub
3958 that will be used when the introcv op for &s2 is executed, clos-
3959 ing over it is safe. Hence, we have to turn off the stale flag
3960 on all lexical subs in the block before we clone any of them.
3961 Hence, having introcv clone the sub cannot work. So we create a
3962 list of ops like this:
3986 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3987 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3988 for (;; kid = OpSIBLING(kid)) {
3989 OP *newkid = newOP(OP_CLONECV, 0);
3990 newkid->op_targ = kid->op_targ;
3991 o = op_append_elem(OP_LINESEQ, o, newkid);
3992 if (kid == last) break;
3994 retval = op_prepend_elem(OP_LINESEQ, o, retval);
3997 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
4003 =head1 Compile-time scope hooks
4005 =for apidoc Aox||blockhook_register
4007 Register a set of hooks to be called when the Perl lexical scope changes
4008 at compile time. See L<perlguts/"Compile-time scope hooks">.
4014 Perl_blockhook_register(pTHX_ BHK *hk)
4016 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
4018 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
4022 Perl_newPROG(pTHX_ OP *o)
4024 PERL_ARGS_ASSERT_NEWPROG;
4031 PL_eval_root = newUNOP(OP_LEAVEEVAL,
4032 ((PL_in_eval & EVAL_KEEPERR)
4033 ? OPf_SPECIAL : 0), o);
4035 cx = &cxstack[cxstack_ix];
4036 assert(CxTYPE(cx) == CXt_EVAL);
4038 if ((cx->blk_gimme & G_WANT) == G_VOID)
4039 scalarvoid(PL_eval_root);
4040 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
4043 scalar(PL_eval_root);
4045 PL_eval_start = op_linklist(PL_eval_root);
4046 PL_eval_root->op_private |= OPpREFCOUNTED;
4047 OpREFCNT_set(PL_eval_root, 1);
4048 PL_eval_root->op_next = 0;
4049 i = PL_savestack_ix;
4052 CALL_PEEP(PL_eval_start);
4053 finalize_optree(PL_eval_root);
4054 S_prune_chain_head(&PL_eval_start);
4056 PL_savestack_ix = i;
4059 if (o->op_type == OP_STUB) {
4060 /* This block is entered if nothing is compiled for the main
4061 program. This will be the case for an genuinely empty main
4062 program, or one which only has BEGIN blocks etc, so already
4065 Historically (5.000) the guard above was !o. However, commit
4066 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
4067 c71fccf11fde0068, changed perly.y so that newPROG() is now
4068 called with the output of block_end(), which returns a new
4069 OP_STUB for the case of an empty optree. ByteLoader (and
4070 maybe other things) also take this path, because they set up
4071 PL_main_start and PL_main_root directly, without generating an
4074 If the parsing the main program aborts (due to parse errors,
4075 or due to BEGIN or similar calling exit), then newPROG()
4076 isn't even called, and hence this code path and its cleanups
4077 are skipped. This shouldn't make a make a difference:
4078 * a non-zero return from perl_parse is a failure, and
4079 perl_destruct() should be called immediately.
4080 * however, if exit(0) is called during the parse, then
4081 perl_parse() returns 0, and perl_run() is called. As
4082 PL_main_start will be NULL, perl_run() will return
4083 promptly, and the exit code will remain 0.
4086 PL_comppad_name = 0;
4088 S_op_destroy(aTHX_ o);
4091 PL_main_root = op_scope(sawparens(scalarvoid(o)));
4092 PL_curcop = &PL_compiling;
4093 PL_main_start = LINKLIST(PL_main_root);
4094 PL_main_root->op_private |= OPpREFCOUNTED;
4095 OpREFCNT_set(PL_main_root, 1);
4096 PL_main_root->op_next = 0;
4097 CALL_PEEP(PL_main_start);
4098 finalize_optree(PL_main_root);
4099 S_prune_chain_head(&PL_main_start);
4100 cv_forget_slab(PL_compcv);
4103 /* Register with debugger */
4105 CV * const cv = get_cvs("DB::postponed", 0);
4109 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
4111 call_sv(MUTABLE_SV(cv), G_DISCARD);
4118 Perl_localize(pTHX_ OP *o, I32 lex)
4120 PERL_ARGS_ASSERT_LOCALIZE;
4122 if (o->op_flags & OPf_PARENS)
4123 /* [perl #17376]: this appears to be premature, and results in code such as
4124 C< our(%x); > executing in list mode rather than void mode */
4131 if ( PL_parser->bufptr > PL_parser->oldbufptr
4132 && PL_parser->bufptr[-1] == ','
4133 && ckWARN(WARN_PARENTHESIS))
4135 char *s = PL_parser->bufptr;
4138 /* some heuristics to detect a potential error */
4139 while (*s && (strchr(", \t\n", *s)))
4143 if (*s && strchr("@$%*", *s) && *++s
4144 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
4147 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
4149 while (*s && (strchr(", \t\n", *s)))
4155 if (sigil && (*s == ';' || *s == '=')) {
4156 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
4157 "Parentheses missing around \"%s\" list",
4159 ? (PL_parser->in_my == KEY_our
4161 : PL_parser->in_my == KEY_state
4171 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4172 PL_parser->in_my = FALSE;
4173 PL_parser->in_my_stash = NULL;
4178 Perl_jmaybe(pTHX_ OP *o)
4180 PERL_ARGS_ASSERT_JMAYBE;
4182 if (o->op_type == OP_LIST) {
4184 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4185 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4190 PERL_STATIC_INLINE OP *
4191 S_op_std_init(pTHX_ OP *o)
4193 I32 type = o->op_type;
4195 PERL_ARGS_ASSERT_OP_STD_INIT;
4197 if (PL_opargs[type] & OA_RETSCALAR)
4199 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4200 o->op_targ = pad_alloc(type, SVs_PADTMP);
4205 PERL_STATIC_INLINE OP *
4206 S_op_integerize(pTHX_ OP *o)
4208 I32 type = o->op_type;
4210 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4212 /* integerize op. */
4213 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4216 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4219 if (type == OP_NEGATE)
4220 /* XXX might want a ck_negate() for this */
4221 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4227 S_fold_constants(pTHX_ OP *o)
4232 VOL I32 type = o->op_type;
4238 SV * const oldwarnhook = PL_warnhook;
4239 SV * const olddiehook = PL_diehook;
4241 U8 oldwarn = PL_dowarn;
4244 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4246 if (!(PL_opargs[type] & OA_FOLDCONST))
4255 #ifdef USE_LOCALE_CTYPE
4256 if (IN_LC_COMPILETIME(LC_CTYPE))
4265 #ifdef USE_LOCALE_COLLATE
4266 if (IN_LC_COMPILETIME(LC_COLLATE))
4271 /* XXX what about the numeric ops? */
4272 #ifdef USE_LOCALE_NUMERIC
4273 if (IN_LC_COMPILETIME(LC_NUMERIC))
4278 if (!OpHAS_SIBLING(cLISTOPo->op_first)
4279 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4282 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
4283 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4285 const char *s = SvPVX_const(sv);
4286 while (s < SvEND(sv)) {
4287 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4294 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4297 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4298 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4302 if (PL_parser && PL_parser->error_count)
4303 goto nope; /* Don't try to run w/ errors */
4305 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4306 const OPCODE type = curop->op_type;
4307 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
4309 type != OP_SCALAR &&
4311 type != OP_PUSHMARK)
4317 curop = LINKLIST(o);
4318 old_next = o->op_next;
4322 oldscope = PL_scopestack_ix;
4323 create_eval_scope(G_FAKINGEVAL);
4325 /* Verify that we don't need to save it: */
4326 assert(PL_curcop == &PL_compiling);
4327 StructCopy(&PL_compiling, ¬_compiling, COP);
4328 PL_curcop = ¬_compiling;
4329 /* The above ensures that we run with all the correct hints of the
4330 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4331 assert(IN_PERL_RUNTIME);
4332 PL_warnhook = PERL_WARNHOOK_FATAL;
4336 /* Effective $^W=1. */
4337 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4338 PL_dowarn |= G_WARN_ON;
4343 sv = *(PL_stack_sp--);
4344 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4345 pad_swipe(o->op_targ, FALSE);
4347 else if (SvTEMP(sv)) { /* grab mortal temp? */
4348 SvREFCNT_inc_simple_void(sv);
4351 else { assert(SvIMMORTAL(sv)); }
4354 /* Something tried to die. Abandon constant folding. */
4355 /* Pretend the error never happened. */
4357 o->op_next = old_next;
4361 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4362 PL_warnhook = oldwarnhook;
4363 PL_diehook = olddiehook;
4364 /* XXX note that this croak may fail as we've already blown away
4365 * the stack - eg any nested evals */
4366 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4369 PL_dowarn = oldwarn;
4370 PL_warnhook = oldwarnhook;
4371 PL_diehook = olddiehook;
4372 PL_curcop = &PL_compiling;
4374 if (PL_scopestack_ix > oldscope)
4375 delete_eval_scope();
4380 /* OP_STRINGIFY and constant folding are used to implement qq.
4381 Here the constant folding is an implementation detail that we
4382 want to hide. If the stringify op is itself already marked
4383 folded, however, then it is actually a folded join. */
4384 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4389 else if (!SvIMMORTAL(sv)) {
4393 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4394 if (!is_stringify) newop->op_folded = 1;
4402 S_gen_constant_list(pTHX_ OP *o)
4406 const SSize_t oldtmps_floor = PL_tmps_floor;
4411 if (PL_parser && PL_parser->error_count)
4412 return o; /* Don't attempt to run with errors */
4414 curop = LINKLIST(o);
4417 S_prune_chain_head(&curop);
4419 Perl_pp_pushmark(aTHX);
4422 assert (!(curop->op_flags & OPf_SPECIAL));
4423 assert(curop->op_type == OP_RANGE);
4424 Perl_pp_anonlist(aTHX);
4425 PL_tmps_floor = oldtmps_floor;
4427 OpTYPE_set(o, OP_RV2AV);
4428 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4429 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4430 o->op_opt = 0; /* needs to be revisited in rpeep() */
4431 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4433 /* replace subtree with an OP_CONST */
4434 curop = ((UNOP*)o)->op_first;
4435 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4438 if (AvFILLp(av) != -1)
4439 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4442 SvREADONLY_on(*svp);
4449 =head1 Optree Manipulation Functions
4452 /* List constructors */
4455 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4457 Append an item to the list of ops contained directly within a list-type
4458 op, returning the lengthened list. C<first> is the list-type op,
4459 and C<last> is the op to append to the list. C<optype> specifies the
4460 intended opcode for the list. If C<first> is not already a list of the
4461 right type, it will be upgraded into one. If either C<first> or C<last>
4462 is null, the other is returned unchanged.
4468 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4476 if (first->op_type != (unsigned)type
4477 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4479 return newLISTOP(type, 0, first, last);
4482 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4483 first->op_flags |= OPf_KIDS;
4488 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4490 Concatenate the lists of ops contained directly within two list-type ops,
4491 returning the combined list. C<first> and C<last> are the list-type ops
4492 to concatenate. C<optype> specifies the intended opcode for the list.
4493 If either C<first> or C<last> is not already a list of the right type,
4494 it will be upgraded into one. If either C<first> or C<last> is null,
4495 the other is returned unchanged.
4501 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4509 if (first->op_type != (unsigned)type)
4510 return op_prepend_elem(type, first, last);
4512 if (last->op_type != (unsigned)type)
4513 return op_append_elem(type, first, last);
4515 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4516 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4517 OpLASTSIB_set(((LISTOP*)first)->op_last, first);
4518 first->op_flags |= (last->op_flags & OPf_KIDS);
4520 S_op_destroy(aTHX_ last);
4526 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4528 Prepend an item to the list of ops contained directly within a list-type
4529 op, returning the lengthened list. C<first> is the op to prepend to the
4530 list, and C<last> is the list-type op. C<optype> specifies the intended
4531 opcode for the list. If C<last> is not already a list of the right type,
4532 it will be upgraded into one. If either C<first> or C<last> is null,
4533 the other is returned unchanged.
4539 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4547 if (last->op_type == (unsigned)type) {
4548 if (type == OP_LIST) { /* already a PUSHMARK there */
4549 /* insert 'first' after pushmark */
4550 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4551 if (!(first->op_flags & OPf_PARENS))
4552 last->op_flags &= ~OPf_PARENS;
4555 op_sibling_splice(last, NULL, 0, first);
4556 last->op_flags |= OPf_KIDS;
4560 return newLISTOP(type, 0, first, last);
4564 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4566 Converts C<o> into a list op if it is not one already, and then converts it
4567 into the specified C<type>, calling its check function, allocating a target if
4568 it needs one, and folding constants.
4570 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4571 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4572 C<op_convert_list> to make it the right type.
4578 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4581 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4582 if (!o || o->op_type != OP_LIST)
4583 o = force_list(o, 0);
4586 o->op_flags &= ~OPf_WANT;
4587 o->op_private &= ~OPpLVAL_INTRO;
4590 if (!(PL_opargs[type] & OA_MARK))
4591 op_null(cLISTOPo->op_first);
4593 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
4594 if (kid2 && kid2->op_type == OP_COREARGS) {
4595 op_null(cLISTOPo->op_first);
4596 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4600 OpTYPE_set(o, type);
4601 o->op_flags |= flags;
4602 if (flags & OPf_FOLDED)
4605 o = CHECKOP(type, o);
4606 if (o->op_type != (unsigned)type)
4609 return fold_constants(op_integerize(op_std_init(o)));
4616 =head1 Optree construction
4618 =for apidoc Am|OP *|newNULLLIST
4620 Constructs, checks, and returns a new C<stub> op, which represents an
4621 empty list expression.
4627 Perl_newNULLLIST(pTHX)
4629 return newOP(OP_STUB, 0);
4632 /* promote o and any siblings to be a list if its not already; i.e.
4640 * pushmark - o - A - B
4642 * If nullit it true, the list op is nulled.
4646 S_force_list(pTHX_ OP *o, bool nullit)
4648 if (!o || o->op_type != OP_LIST) {
4651 /* manually detach any siblings then add them back later */
4652 rest = OpSIBLING(o);
4653 OpLASTSIB_set(o, NULL);
4655 o = newLISTOP(OP_LIST, 0, o, NULL);
4657 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4665 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4667 Constructs, checks, and returns an op of any list type. C<type> is
4668 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4669 C<OPf_KIDS> will be set automatically if required. C<first> and C<last>
4670 supply up to two ops to be direct children of the list op; they are
4671 consumed by this function and become part of the constructed op tree.
4673 For most list operators, the check function expects all the kid ops to be
4674 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
4675 appropriate. What you want to do in that case is create an op of type
4676 OP_LIST, append more children to it, and then call L</op_convert_list>.
4677 See L</op_convert_list> for more information.
4684 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4689 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
4690 || type == OP_CUSTOM);
4692 NewOp(1101, listop, 1, LISTOP);
4694 OpTYPE_set(listop, type);
4697 listop->op_flags = (U8)flags;
4701 else if (!first && last)
4704 OpMORESIB_set(first, last);
4705 listop->op_first = first;
4706 listop->op_last = last;
4707 if (type == OP_LIST) {
4708 OP* const pushop = newOP(OP_PUSHMARK, 0);
4709 OpMORESIB_set(pushop, first);
4710 listop->op_first = pushop;
4711 listop->op_flags |= OPf_KIDS;
4713 listop->op_last = pushop;
4715 if (listop->op_last)
4716 OpLASTSIB_set(listop->op_last, (OP*)listop);
4718 return CHECKOP(type, listop);
4722 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4724 Constructs, checks, and returns an op of any base type (any type that
4725 has no extra fields). C<type> is the opcode. C<flags> gives the
4726 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4733 Perl_newOP(pTHX_ I32 type, I32 flags)
4738 if (type == -OP_ENTEREVAL) {
4739 type = OP_ENTEREVAL;
4740 flags |= OPpEVAL_BYTES<<8;
4743 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4744 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4745 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4746 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4748 NewOp(1101, o, 1, OP);
4749 OpTYPE_set(o, type);
4750 o->op_flags = (U8)flags;
4753 o->op_private = (U8)(0 | (flags >> 8));
4754 if (PL_opargs[type] & OA_RETSCALAR)
4756 if (PL_opargs[type] & OA_TARGET)
4757 o->op_targ = pad_alloc(type, SVs_PADTMP);
4758 return CHECKOP(type, o);
4762 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4764 Constructs, checks, and returns an op of any unary type. C<type> is
4765 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
4766 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4767 bits, the eight bits of C<op_private>, except that the bit with value 1
4768 is automatically set. C<first> supplies an optional op to be the direct
4769 child of the unary op; it is consumed by this function and become part
4770 of the constructed op tree.
4776 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4781 if (type == -OP_ENTEREVAL) {
4782 type = OP_ENTEREVAL;
4783 flags |= OPpEVAL_BYTES<<8;
4786 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4787 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4788 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4789 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4790 || type == OP_SASSIGN
4791 || type == OP_ENTERTRY
4792 || type == OP_CUSTOM
4793 || type == OP_NULL );
4796 first = newOP(OP_STUB, 0);
4797 if (PL_opargs[type] & OA_MARK)
4798 first = force_list(first, 1);
4800 NewOp(1101, unop, 1, UNOP);
4801 OpTYPE_set(unop, type);
4802 unop->op_first = first;
4803 unop->op_flags = (U8)(flags | OPf_KIDS);
4804 unop->op_private = (U8)(1 | (flags >> 8));
4806 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
4807 OpLASTSIB_set(first, (OP*)unop);
4809 unop = (UNOP*) CHECKOP(type, unop);
4813 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4817 =for apidoc newUNOP_AUX
4819 Similar to C<newUNOP>, but creates an UNOP_AUX struct instead, with op_aux
4826 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux)
4831 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX
4832 || type == OP_CUSTOM);
4834 NewOp(1101, unop, 1, UNOP_AUX);
4835 unop->op_type = (OPCODE)type;
4836 unop->op_ppaddr = PL_ppaddr[type];
4837 unop->op_first = first;
4838 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0));
4839 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8));
4842 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */
4843 OpLASTSIB_set(first, (OP*)unop);
4845 unop = (UNOP_AUX*) CHECKOP(type, unop);
4847 return op_std_init((OP *) unop);
4851 =for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first
4853 Constructs, checks, and returns an op of method type with a method name
4854 evaluated at runtime. C<type> is the&n