4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * Note that during the build of miniperl, a temporary copy of this file
26 * is made, called opmini.c.
28 * A Perl program is compiled into a tree of OP nodes. Each op contains:
29 * * structural OP pointers to its children and siblings (op_sibling,
30 * op_first etc) that define the tree structure;
31 * * execution order OP pointers (op_next, plus sometimes op_other,
32 * op_lastop etc) that define the execution sequence plus variants;
33 * * a pointer to the C "pp" function that would execute the op;
34 * * any data specific to that op.
35 * For example, an OP_CONST op points to the pp_const() function and to an
36 * SV containing the constant value. When pp_const() is executed, its job
37 * is to push that SV onto the stack.
39 * OPs are mainly created by the newFOO() functions, which are mainly
40 * called from the parser (in perly.y) as the code is parsed. For example
41 * the Perl code $a + $b * $c would cause the equivalent of the following
42 * to be called (oversimplifying a bit):
44 * newBINOP(OP_ADD, flags,
46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
49 * As the parser reduces low-level rules, it creates little op subtrees;
50 * as higher-level rules are resolved, these subtrees get joined together
51 * as branches on a bigger subtree, until eventually a top-level rule like
52 * a subroutine definition is reduced, at which point there is one large
55 * The execution order pointers (op_next) are generated as the subtrees
56 * are joined together. Consider this sub-expression: A*B + C/D: at the
57 * point when it's just been parsed, the op tree looks like:
65 * with the intended execution order being:
67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
69 * At this point all the nodes' op_next pointers will have been set,
71 * * we don't know what the [NEXT] node will be yet;
72 * * we don't know what the [PREV] node will be yet, but when it gets
73 * created and needs its op_next set, it needs to be set to point to
74 * A, which is non-obvious.
75 * To handle both those cases, we temporarily set the top node's
76 * op_next to point to the first node to be executed in this subtree (A in
77 * this case). This means that initially a subtree's op_next chain,
78 * starting from the top node, will visit each node in execution sequence
79 * then point back at the top node.
80 * When we embed this subtree in a larger tree, its top op_next is used
81 * to get the start node, then is set to point to its new neighbour.
82 * For example the two separate [*],A,B and [/],C,D subtrees would
84 * [*] => A; A => B; B => [*]
86 * [/] => C; C => D; D => [/]
87 * When these two subtrees were joined together to make the [+] subtree,
88 * [+]'s op_next was set to [*]'s op_next, i.e. A; then [*]'s op_next was
89 * set to point to [/]'s op_next, i.e. C.
91 * This op_next linking is done by the LINKLIST() macro and its underlying
92 * op_linklist() function. Given a top-level op, if its op_next is
93 * non-null, it's already been linked, so leave it. Otherwise link it with
94 * its children as described above, possibly recursively if any of the
95 * children have a null op_next.
97 * In summary: given a subtree, its top-level node's op_next will either
99 * NULL: the subtree hasn't been LINKLIST()ed yet;
100 * fake: points to the start op for this subtree;
101 * real: once the subtree has been embedded into a larger tree
106 Here's an older description from Larry.
108 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
112 An execution-order pass
114 The bottom-up pass is represented by all the "newOP" routines and
115 the ck_ routines. The bottom-upness is actually driven by yacc.
116 So at the point that a ck_ routine fires, we have no idea what the
117 context is, either upward in the syntax tree, or either forward or
118 backward in the execution order. (The bottom-up parser builds that
119 part of the execution order it knows about, but if you follow the "next"
120 links around, you'll find it's actually a closed loop through the
123 Whenever the bottom-up parser gets to a node that supplies context to
124 its components, it invokes that portion of the top-down pass that applies
125 to that part of the subtree (and marks the top node as processed, so
126 if a node further up supplies context, it doesn't have to take the
127 plunge again). As a particular subcase of this, as the new node is
128 built, it takes all the closed execution loops of its subcomponents
129 and links them into a new closed loop for the higher level node. But
130 it's still not the real execution order.
132 The actual execution order is not known till we get a grammar reduction
133 to a top-level unit like a subroutine or file that will be called by
134 "name" rather than via a "next" pointer. At that point, we can call
135 into peep() to do that code's portion of the 3rd pass. It has to be
136 recursive, but it's recursive on basic blocks, not on tree nodes.
139 /* To implement user lexical pragmas, there needs to be a way at run time to
140 get the compile time state of %^H for that block. Storing %^H in every
141 block (or even COP) would be very expensive, so a different approach is
142 taken. The (running) state of %^H is serialised into a tree of HE-like
143 structs. Stores into %^H are chained onto the current leaf as a struct
144 refcounted_he * with the key and the value. Deletes from %^H are saved
145 with a value of PL_sv_placeholder. The state of %^H at any point can be
146 turned back into a regular HV by walking back up the tree from that point's
147 leaf, ignoring any key you've already seen (placeholder or not), storing
148 the rest into the HV structure, then removing the placeholders. Hence
149 memory is only used to store the %^H deltas from the enclosing COP, rather
150 than the entire %^H on each COP.
152 To cause actions on %^H to write out the serialisation records, it has
153 magic type 'H'. This magic (itself) does nothing, but its presence causes
154 the values to gain magic type 'h', which has entries for set and clear.
155 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
156 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
157 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
158 it will be correctly restored when any inner compiling scope is exited.
164 #include "keywords.h"
168 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
169 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
170 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
172 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
174 /* Used to avoid recursion through the op tree in scalarvoid() and
178 #define DEFERRED_OP_STEP 100
179 #define DEFER_OP(o) \
181 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \
182 defer_stack_alloc += DEFERRED_OP_STEP; \
183 assert(defer_stack_alloc > 0); \
184 Renew(defer_stack, defer_stack_alloc, OP *); \
186 defer_stack[++defer_ix] = o; \
189 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
191 /* remove any leading "empty" ops from the op_next chain whose first
192 * node's address is stored in op_p. Store the updated address of the
193 * first node in op_p.
197 S_prune_chain_head(OP** op_p)
200 && ( (*op_p)->op_type == OP_NULL
201 || (*op_p)->op_type == OP_SCOPE
202 || (*op_p)->op_type == OP_SCALAR
203 || (*op_p)->op_type == OP_LINESEQ)
205 *op_p = (*op_p)->op_next;
209 /* See the explanatory comments above struct opslab in op.h. */
211 #ifdef PERL_DEBUG_READONLY_OPS
212 # define PERL_SLAB_SIZE 128
213 # define PERL_MAX_SLAB_SIZE 4096
214 # include <sys/mman.h>
217 #ifndef PERL_SLAB_SIZE
218 # define PERL_SLAB_SIZE 64
220 #ifndef PERL_MAX_SLAB_SIZE
221 # define PERL_MAX_SLAB_SIZE 2048
224 /* rounds up to nearest pointer */
225 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
226 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
229 S_new_slab(pTHX_ size_t sz)
231 #ifdef PERL_DEBUG_READONLY_OPS
232 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
233 PROT_READ|PROT_WRITE,
234 MAP_ANON|MAP_PRIVATE, -1, 0);
235 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
236 (unsigned long) sz, slab));
237 if (slab == MAP_FAILED) {
238 perror("mmap failed");
241 slab->opslab_size = (U16)sz;
243 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
246 /* The context is unused in non-Windows */
249 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
253 /* requires double parens and aTHX_ */
254 #define DEBUG_S_warn(args) \
256 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
260 Perl_Slab_Alloc(pTHX_ size_t sz)
268 /* We only allocate ops from the slab during subroutine compilation.
269 We find the slab via PL_compcv, hence that must be non-NULL. It could
270 also be pointing to a subroutine which is now fully set up (CvROOT()
271 pointing to the top of the optree for that sub), or a subroutine
272 which isn't using the slab allocator. If our sanity checks aren't met,
273 don't use a slab, but allocate the OP directly from the heap. */
274 if (!PL_compcv || CvROOT(PL_compcv)
275 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
277 o = (OP*)PerlMemShared_calloc(1, sz);
281 /* While the subroutine is under construction, the slabs are accessed via
282 CvSTART(), to avoid needing to expand PVCV by one pointer for something
283 unneeded at runtime. Once a subroutine is constructed, the slabs are
284 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
285 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
287 if (!CvSTART(PL_compcv)) {
289 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
290 CvSLABBED_on(PL_compcv);
291 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
293 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
295 opsz = SIZE_TO_PSIZE(sz);
296 sz = opsz + OPSLOT_HEADER_P;
298 /* The slabs maintain a free list of OPs. In particular, constant folding
299 will free up OPs, so it makes sense to re-use them where possible. A
300 freed up slot is used in preference to a new allocation. */
301 if (slab->opslab_freed) {
302 OP **too = &slab->opslab_freed;
304 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
305 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
306 DEBUG_S_warn((aTHX_ "Alas! too small"));
307 o = *(too = &o->op_next);
308 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
312 Zero(o, opsz, I32 *);
318 #define INIT_OPSLOT \
319 slot->opslot_slab = slab; \
320 slot->opslot_next = slab2->opslab_first; \
321 slab2->opslab_first = slot; \
322 o = &slot->opslot_op; \
325 /* The partially-filled slab is next in the chain. */
326 slab2 = slab->opslab_next ? slab->opslab_next : slab;
327 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
328 /* Remaining space is too small. */
330 /* If we can fit a BASEOP, add it to the free chain, so as not
332 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
333 slot = &slab2->opslab_slots;
335 o->op_type = OP_FREED;
336 o->op_next = slab->opslab_freed;
337 slab->opslab_freed = o;
340 /* Create a new slab. Make this one twice as big. */
341 slot = slab2->opslab_first;
342 while (slot->opslot_next) slot = slot->opslot_next;
343 slab2 = S_new_slab(aTHX_
344 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
346 : (DIFF(slab2, slot)+1)*2);
347 slab2->opslab_next = slab->opslab_next;
348 slab->opslab_next = slab2;
350 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
352 /* Create a new op slot */
353 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
354 assert(slot >= &slab2->opslab_slots);
355 if (DIFF(&slab2->opslab_slots, slot)
356 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
357 slot = &slab2->opslab_slots;
359 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
362 #ifdef PERL_OP_PARENT
363 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
364 assert(!o->op_moresib);
365 assert(!o->op_sibparent);
373 #ifdef PERL_DEBUG_READONLY_OPS
375 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
377 PERL_ARGS_ASSERT_SLAB_TO_RO;
379 if (slab->opslab_readonly) return;
380 slab->opslab_readonly = 1;
381 for (; slab; slab = slab->opslab_next) {
382 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
383 (unsigned long) slab->opslab_size, slab));*/
384 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
385 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
386 (unsigned long)slab->opslab_size, errno);
391 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
395 PERL_ARGS_ASSERT_SLAB_TO_RW;
397 if (!slab->opslab_readonly) return;
399 for (; slab2; slab2 = slab2->opslab_next) {
400 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
401 (unsigned long) size, slab2));*/
402 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
403 PROT_READ|PROT_WRITE)) {
404 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
405 (unsigned long)slab2->opslab_size, errno);
408 slab->opslab_readonly = 0;
412 # define Slab_to_rw(op) NOOP
415 /* This cannot possibly be right, but it was copied from the old slab
416 allocator, to which it was originally added, without explanation, in
419 # define PerlMemShared PerlMem
423 Perl_Slab_Free(pTHX_ void *op)
425 OP * const o = (OP *)op;
428 PERL_ARGS_ASSERT_SLAB_FREE;
430 if (!o->op_slabbed) {
432 PerlMemShared_free(op);
437 /* If this op is already freed, our refcount will get screwy. */
438 assert(o->op_type != OP_FREED);
439 o->op_type = OP_FREED;
440 o->op_next = slab->opslab_freed;
441 slab->opslab_freed = o;
442 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
443 OpslabREFCNT_dec_padok(slab);
447 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
449 const bool havepad = !!PL_comppad;
450 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
453 PAD_SAVE_SETNULLPAD();
460 Perl_opslab_free(pTHX_ OPSLAB *slab)
463 PERL_ARGS_ASSERT_OPSLAB_FREE;
465 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
466 assert(slab->opslab_refcnt == 1);
468 slab2 = slab->opslab_next;
470 slab->opslab_refcnt = ~(size_t)0;
472 #ifdef PERL_DEBUG_READONLY_OPS
473 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
475 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
476 perror("munmap failed");
480 PerlMemShared_free(slab);
487 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
491 size_t savestack_count = 0;
493 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
497 for (slot = slab2->opslab_first;
499 slot = slot->opslot_next) {
500 if (slot->opslot_op.op_type != OP_FREED
501 && !(slot->opslot_op.op_savefree
507 assert(slot->opslot_op.op_slabbed);
508 op_free(&slot->opslot_op);
509 if (slab->opslab_refcnt == 1) goto free;
512 } while ((slab2 = slab2->opslab_next));
513 /* > 1 because the CV still holds a reference count. */
514 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
516 assert(savestack_count == slab->opslab_refcnt-1);
518 /* Remove the CV’s reference count. */
519 slab->opslab_refcnt--;
526 #ifdef PERL_DEBUG_READONLY_OPS
528 Perl_op_refcnt_inc(pTHX_ OP *o)
531 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
532 if (slab && slab->opslab_readonly) {
545 Perl_op_refcnt_dec(pTHX_ OP *o)
548 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
550 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
552 if (slab && slab->opslab_readonly) {
554 result = --o->op_targ;
557 result = --o->op_targ;
563 * In the following definition, the ", (OP*)0" is just to make the compiler
564 * think the expression is of the right type: croak actually does a Siglongjmp.
566 #define CHECKOP(type,o) \
567 ((PL_op_mask && PL_op_mask[type]) \
568 ? ( op_free((OP*)o), \
569 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
571 : PL_check[type](aTHX_ (OP*)o))
573 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
575 #define OpTYPE_set(o,type) \
577 o->op_type = (OPCODE)type; \
578 o->op_ppaddr = PL_ppaddr[type]; \
582 S_no_fh_allowed(pTHX_ OP *o)
584 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
586 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
592 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
594 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
595 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
600 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
602 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
604 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
609 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
611 PERL_ARGS_ASSERT_BAD_TYPE_PV;
613 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
614 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
617 /* remove flags var, its unused in all callers, move to to right end since gv
618 and kid are always the same */
620 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
622 SV * const namesv = cv_name((CV *)gv, NULL, 0);
623 PERL_ARGS_ASSERT_BAD_TYPE_GV;
625 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
626 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
630 S_no_bareword_allowed(pTHX_ OP *o)
632 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
634 qerror(Perl_mess(aTHX_
635 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
637 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
640 /* "register" allocation */
643 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
646 const bool is_our = (PL_parser->in_my == KEY_our);
648 PERL_ARGS_ASSERT_ALLOCMY;
650 if (flags & ~SVf_UTF8)
651 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
654 /* complain about "my $<special_var>" etc etc */
658 || ( (flags & SVf_UTF8)
659 && isIDFIRST_utf8_safe((U8 *)name+1, name + len))
660 || (name[1] == '_' && len > 2)))
662 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
664 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
665 /* diag_listed_as: Can't use global %s in "%s" */
666 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
667 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
668 PL_parser->in_my == KEY_state ? "state" : "my"));
670 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
671 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
675 /* allocate a spare slot and store the name in that slot */
677 off = pad_add_name_pvn(name, len,
678 (is_our ? padadd_OUR :
679 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
680 PL_parser->in_my_stash,
682 /* $_ is always in main::, even with our */
683 ? (PL_curstash && !memEQs(name,len,"$_")
689 /* anon sub prototypes contains state vars should always be cloned,
690 * otherwise the state var would be shared between anon subs */
692 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
693 CvCLONE_on(PL_compcv);
699 =head1 Optree Manipulation Functions
701 =for apidoc alloccopstash
703 Available only under threaded builds, this function allocates an entry in
704 C<PL_stashpad> for the stash passed to it.
711 Perl_alloccopstash(pTHX_ HV *hv)
713 PADOFFSET off = 0, o = 1;
714 bool found_slot = FALSE;
716 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
718 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
720 for (; o < PL_stashpadmax; ++o) {
721 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
722 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
723 found_slot = TRUE, off = o;
726 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
727 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
728 off = PL_stashpadmax;
729 PL_stashpadmax += 10;
732 PL_stashpad[PL_stashpadix = off] = hv;
737 /* free the body of an op without examining its contents.
738 * Always use this rather than FreeOp directly */
741 S_op_destroy(pTHX_ OP *o)
749 =for apidoc Am|void|op_free|OP *o
751 Free an op. Only use this when an op is no longer linked to from any
758 Perl_op_free(pTHX_ OP *o)
762 SSize_t defer_ix = -1;
763 SSize_t defer_stack_alloc = 0;
764 OP **defer_stack = NULL;
768 /* Though ops may be freed twice, freeing the op after its slab is a
770 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
771 /* During the forced freeing of ops after compilation failure, kidops
772 may be freed before their parents. */
773 if (!o || o->op_type == OP_FREED)
778 /* an op should only ever acquire op_private flags that we know about.
779 * If this fails, you may need to fix something in regen/op_private.
780 * Don't bother testing if:
781 * * the op_ppaddr doesn't match the op; someone may have
782 * overridden the op and be doing strange things with it;
783 * * we've errored, as op flags are often left in an
784 * inconsistent state then. Note that an error when
785 * compiling the main program leaves PL_parser NULL, so
786 * we can't spot faults in the main code, only
787 * evaled/required code */
789 if ( o->op_ppaddr == PL_ppaddr[o->op_type]
791 && !PL_parser->error_count)
793 assert(!(o->op_private & ~PL_op_private_valid[type]));
797 if (o->op_private & OPpREFCOUNTED) {
808 refcnt = OpREFCNT_dec(o);
811 /* Need to find and remove any pattern match ops from the list
812 we maintain for reset(). */
813 find_and_forget_pmops(o);
823 /* Call the op_free hook if it has been set. Do it now so that it's called
824 * at the right time for refcounted ops, but still before all of the kids
828 if (o->op_flags & OPf_KIDS) {
830 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
831 nextkid = OpSIBLING(kid); /* Get before next freeing kid */
832 if (!kid || kid->op_type == OP_FREED)
833 /* During the forced freeing of ops after
834 compilation failure, kidops may be freed before
837 if (!(kid->op_flags & OPf_KIDS))
838 /* If it has no kids, just free it now */
845 type = (OPCODE)o->op_targ;
848 Slab_to_rw(OpSLAB(o));
850 /* COP* is not cleared by op_clear() so that we may track line
851 * numbers etc even after null() */
852 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
860 } while ( (o = POP_DEFERRED_OP()) );
862 Safefree(defer_stack);
865 /* S_op_clear_gv(): free a GV attached to an OP */
869 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
871 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
875 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
876 || o->op_type == OP_MULTIDEREF)
879 ? ((GV*)PAD_SVl(*ixp)) : NULL;
881 ? (GV*)(*svp) : NULL;
883 /* It's possible during global destruction that the GV is freed
884 before the optree. Whilst the SvREFCNT_inc is happy to bump from
885 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
886 will trigger an assertion failure, because the entry to sv_clear
887 checks that the scalar is not already freed. A check of for
888 !SvIS_FREED(gv) turns out to be invalid, because during global
889 destruction the reference count can be forced down to zero
890 (with SVf_BREAK set). In which case raising to 1 and then
891 dropping to 0 triggers cleanup before it should happen. I
892 *think* that this might actually be a general, systematic,
893 weakness of the whole idea of SVf_BREAK, in that code *is*
894 allowed to raise and lower references during global destruction,
895 so any *valid* code that happens to do this during global
896 destruction might well trigger premature cleanup. */
897 bool still_valid = gv && SvREFCNT(gv);
900 SvREFCNT_inc_simple_void(gv);
903 pad_swipe(*ixp, TRUE);
911 int try_downgrade = SvREFCNT(gv) == 2;
914 gv_try_downgrade(gv);
920 Perl_op_clear(pTHX_ OP *o)
925 PERL_ARGS_ASSERT_OP_CLEAR;
927 switch (o->op_type) {
928 case OP_NULL: /* Was holding old type, if any. */
931 case OP_ENTEREVAL: /* Was holding hints. */
932 case OP_ARGDEFELEM: /* Was holding signature index. */
936 if (!(o->op_flags & OPf_REF)
937 || (PL_check[o->op_type] != Perl_ck_ftst))
944 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
946 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
949 case OP_METHOD_REDIR:
950 case OP_METHOD_REDIR_SUPER:
952 if (cMETHOPx(o)->op_rclass_targ) {
953 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
954 cMETHOPx(o)->op_rclass_targ = 0;
957 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
958 cMETHOPx(o)->op_rclass_sv = NULL;
960 case OP_METHOD_NAMED:
961 case OP_METHOD_SUPER:
962 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
963 cMETHOPx(o)->op_u.op_meth_sv = NULL;
966 pad_swipe(o->op_targ, 1);
973 SvREFCNT_dec(cSVOPo->op_sv);
974 cSVOPo->op_sv = NULL;
977 Even if op_clear does a pad_free for the target of the op,
978 pad_free doesn't actually remove the sv that exists in the pad;
979 instead it lives on. This results in that it could be reused as
980 a target later on when the pad was reallocated.
983 pad_swipe(o->op_targ,1);
993 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
998 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR)
999 && (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)))
1002 if (cPADOPo->op_padix > 0) {
1003 pad_swipe(cPADOPo->op_padix, TRUE);
1004 cPADOPo->op_padix = 0;
1007 SvREFCNT_dec(cSVOPo->op_sv);
1008 cSVOPo->op_sv = NULL;
1012 PerlMemShared_free(cPVOPo->op_pv);
1013 cPVOPo->op_pv = NULL;
1017 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1021 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1022 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1024 if (o->op_private & OPpSPLIT_LEX)
1025 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1028 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1030 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1037 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1038 op_free(cPMOPo->op_code_list);
1039 cPMOPo->op_code_list = NULL;
1040 forget_pmop(cPMOPo);
1041 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1042 /* we use the same protection as the "SAFE" version of the PM_ macros
1043 * here since sv_clean_all might release some PMOPs
1044 * after PL_regex_padav has been cleared
1045 * and the clearing of PL_regex_padav needs to
1046 * happen before sv_clean_all
1049 if(PL_regex_pad) { /* We could be in destruction */
1050 const IV offset = (cPMOPo)->op_pmoffset;
1051 ReREFCNT_dec(PM_GETRE(cPMOPo));
1052 PL_regex_pad[offset] = &PL_sv_undef;
1053 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1057 ReREFCNT_dec(PM_GETRE(cPMOPo));
1058 PM_SETRE(cPMOPo, NULL);
1064 PerlMemShared_free(cUNOP_AUXo->op_aux);
1069 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1070 UV actions = items->uv;
1072 bool is_hash = FALSE;
1075 switch (actions & MDEREF_ACTION_MASK) {
1078 actions = (++items)->uv;
1081 case MDEREF_HV_padhv_helem:
1083 case MDEREF_AV_padav_aelem:
1084 pad_free((++items)->pad_offset);
1087 case MDEREF_HV_gvhv_helem:
1089 case MDEREF_AV_gvav_aelem:
1091 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1093 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1097 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1099 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1101 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1103 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1105 goto do_vivify_rv2xv_elem;
1107 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1109 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1110 pad_free((++items)->pad_offset);
1111 goto do_vivify_rv2xv_elem;
1113 case MDEREF_HV_pop_rv2hv_helem:
1114 case MDEREF_HV_vivify_rv2hv_helem:
1116 do_vivify_rv2xv_elem:
1117 case MDEREF_AV_pop_rv2av_aelem:
1118 case MDEREF_AV_vivify_rv2av_aelem:
1120 switch (actions & MDEREF_INDEX_MASK) {
1121 case MDEREF_INDEX_none:
1124 case MDEREF_INDEX_const:
1128 pad_swipe((++items)->pad_offset, 1);
1130 SvREFCNT_dec((++items)->sv);
1136 case MDEREF_INDEX_padsv:
1137 pad_free((++items)->pad_offset);
1139 case MDEREF_INDEX_gvsv:
1141 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1143 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1148 if (actions & MDEREF_FLAG_last)
1161 actions >>= MDEREF_SHIFT;
1164 /* start of malloc is at op_aux[-1], where the length is
1166 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1171 if (o->op_targ > 0) {
1172 pad_free(o->op_targ);
1178 S_cop_free(pTHX_ COP* cop)
1180 PERL_ARGS_ASSERT_COP_FREE;
1183 if (! specialWARN(cop->cop_warnings))
1184 PerlMemShared_free(cop->cop_warnings);
1185 cophh_free(CopHINTHASH_get(cop));
1186 if (PL_curcop == cop)
1191 S_forget_pmop(pTHX_ PMOP *const o
1194 HV * const pmstash = PmopSTASH(o);
1196 PERL_ARGS_ASSERT_FORGET_PMOP;
1198 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1199 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1201 PMOP **const array = (PMOP**) mg->mg_ptr;
1202 U32 count = mg->mg_len / sizeof(PMOP**);
1206 if (array[i] == o) {
1207 /* Found it. Move the entry at the end to overwrite it. */
1208 array[i] = array[--count];
1209 mg->mg_len = count * sizeof(PMOP**);
1210 /* Could realloc smaller at this point always, but probably
1211 not worth it. Probably worth free()ing if we're the
1214 Safefree(mg->mg_ptr);
1227 S_find_and_forget_pmops(pTHX_ OP *o)
1229 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1231 if (o->op_flags & OPf_KIDS) {
1232 OP *kid = cUNOPo->op_first;
1234 switch (kid->op_type) {
1239 forget_pmop((PMOP*)kid);
1241 find_and_forget_pmops(kid);
1242 kid = OpSIBLING(kid);
1248 =for apidoc Am|void|op_null|OP *o
1250 Neutralizes an op when it is no longer needed, but is still linked to from
1257 Perl_op_null(pTHX_ OP *o)
1261 PERL_ARGS_ASSERT_OP_NULL;
1263 if (o->op_type == OP_NULL)
1266 o->op_targ = o->op_type;
1267 OpTYPE_set(o, OP_NULL);
1271 Perl_op_refcnt_lock(pTHX)
1272 PERL_TSA_ACQUIRE(PL_op_mutex)
1277 PERL_UNUSED_CONTEXT;
1282 Perl_op_refcnt_unlock(pTHX)
1283 PERL_TSA_RELEASE(PL_op_mutex)
1288 PERL_UNUSED_CONTEXT;
1294 =for apidoc op_sibling_splice
1296 A general function for editing the structure of an existing chain of
1297 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1298 you to delete zero or more sequential nodes, replacing them with zero or
1299 more different nodes. Performs the necessary op_first/op_last
1300 housekeeping on the parent node and op_sibling manipulation on the
1301 children. The last deleted node will be marked as as the last node by
1302 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1304 Note that op_next is not manipulated, and nodes are not freed; that is the
1305 responsibility of the caller. It also won't create a new list op for an
1306 empty list etc; use higher-level functions like op_append_elem() for that.
1308 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1309 the splicing doesn't affect the first or last op in the chain.
1311 C<start> is the node preceding the first node to be spliced. Node(s)
1312 following it will be deleted, and ops will be inserted after it. If it is
1313 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1316 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1317 If -1 or greater than or equal to the number of remaining kids, all
1318 remaining kids are deleted.
1320 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1321 If C<NULL>, no nodes are inserted.
1323 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1328 action before after returns
1329 ------ ----- ----- -------
1332 splice(P, A, 2, X-Y-Z) | | B-C
1336 splice(P, NULL, 1, X-Y) | | A
1340 splice(P, NULL, 3, NULL) | | A-B-C
1344 splice(P, B, 0, X-Y) | | NULL
1348 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1349 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1355 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1359 OP *last_del = NULL;
1360 OP *last_ins = NULL;
1363 first = OpSIBLING(start);
1367 first = cLISTOPx(parent)->op_first;
1369 assert(del_count >= -1);
1371 if (del_count && first) {
1373 while (--del_count && OpHAS_SIBLING(last_del))
1374 last_del = OpSIBLING(last_del);
1375 rest = OpSIBLING(last_del);
1376 OpLASTSIB_set(last_del, NULL);
1383 while (OpHAS_SIBLING(last_ins))
1384 last_ins = OpSIBLING(last_ins);
1385 OpMAYBESIB_set(last_ins, rest, NULL);
1391 OpMAYBESIB_set(start, insert, NULL);
1396 cLISTOPx(parent)->op_first = insert;
1398 parent->op_flags |= OPf_KIDS;
1400 parent->op_flags &= ~OPf_KIDS;
1404 /* update op_last etc */
1411 /* ought to use OP_CLASS(parent) here, but that can't handle
1412 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1414 type = parent->op_type;
1415 if (type == OP_CUSTOM) {
1417 type = XopENTRYCUSTOM(parent, xop_class);
1420 if (type == OP_NULL)
1421 type = parent->op_targ;
1422 type = PL_opargs[type] & OA_CLASS_MASK;
1425 lastop = last_ins ? last_ins : start ? start : NULL;
1426 if ( type == OA_BINOP
1427 || type == OA_LISTOP
1431 cLISTOPx(parent)->op_last = lastop;
1434 OpLASTSIB_set(lastop, parent);
1436 return last_del ? first : NULL;
1439 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1443 #ifdef PERL_OP_PARENT
1446 =for apidoc op_parent
1448 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1449 This function is only available on perls built with C<-DPERL_OP_PARENT>.
1455 Perl_op_parent(OP *o)
1457 PERL_ARGS_ASSERT_OP_PARENT;
1458 while (OpHAS_SIBLING(o))
1460 return o->op_sibparent;
1466 /* replace the sibling following start with a new UNOP, which becomes
1467 * the parent of the original sibling; e.g.
1469 * op_sibling_newUNOP(P, A, unop-args...)
1477 * where U is the new UNOP.
1479 * parent and start args are the same as for op_sibling_splice();
1480 * type and flags args are as newUNOP().
1482 * Returns the new UNOP.
1486 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1490 kid = op_sibling_splice(parent, start, 1, NULL);
1491 newop = newUNOP(type, flags, kid);
1492 op_sibling_splice(parent, start, 0, newop);
1497 /* lowest-level newLOGOP-style function - just allocates and populates
1498 * the struct. Higher-level stuff should be done by S_new_logop() /
1499 * newLOGOP(). This function exists mainly to avoid op_first assignment
1500 * being spread throughout this file.
1504 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1509 NewOp(1101, logop, 1, LOGOP);
1510 OpTYPE_set(logop, type);
1511 logop->op_first = first;
1512 logop->op_other = other;
1513 logop->op_flags = OPf_KIDS;
1514 while (kid && OpHAS_SIBLING(kid))
1515 kid = OpSIBLING(kid);
1517 OpLASTSIB_set(kid, (OP*)logop);
1522 /* Contextualizers */
1525 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1527 Applies a syntactic context to an op tree representing an expression.
1528 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
1529 or C<G_VOID> to specify the context to apply. The modified op tree
1536 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1538 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1540 case G_SCALAR: return scalar(o);
1541 case G_ARRAY: return list(o);
1542 case G_VOID: return scalarvoid(o);
1544 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1551 =for apidoc Am|OP*|op_linklist|OP *o
1552 This function is the implementation of the L</LINKLIST> macro. It should
1553 not be called directly.
1559 Perl_op_linklist(pTHX_ OP *o)
1563 PERL_ARGS_ASSERT_OP_LINKLIST;
1568 /* establish postfix order */
1569 first = cUNOPo->op_first;
1572 o->op_next = LINKLIST(first);
1575 OP *sibl = OpSIBLING(kid);
1577 kid->op_next = LINKLIST(sibl);
1592 S_scalarkids(pTHX_ OP *o)
1594 if (o && o->op_flags & OPf_KIDS) {
1596 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1603 S_scalarboolean(pTHX_ OP *o)
1605 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1607 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1608 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1609 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1610 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1611 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1612 if (ckWARN(WARN_SYNTAX)) {
1613 const line_t oldline = CopLINE(PL_curcop);
1615 if (PL_parser && PL_parser->copline != NOLINE) {
1616 /* This ensures that warnings are reported at the first line
1617 of the conditional, not the last. */
1618 CopLINE_set(PL_curcop, PL_parser->copline);
1620 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1621 CopLINE_set(PL_curcop, oldline);
1628 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1631 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1632 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1634 const char funny = o->op_type == OP_PADAV
1635 || o->op_type == OP_RV2AV ? '@' : '%';
1636 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1638 if (cUNOPo->op_first->op_type != OP_GV
1639 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1641 return varname(gv, funny, 0, NULL, 0, subscript_type);
1644 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1649 S_op_varname(pTHX_ const OP *o)
1651 return S_op_varname_subscript(aTHX_ o, 1);
1655 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1656 { /* or not so pretty :-) */
1657 if (o->op_type == OP_CONST) {
1659 if (SvPOK(*retsv)) {
1661 *retsv = sv_newmortal();
1662 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1663 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1665 else if (!SvOK(*retsv))
1668 else *retpv = "...";
1672 S_scalar_slice_warning(pTHX_ const OP *o)
1675 const bool h = o->op_type == OP_HSLICE
1676 || (o->op_type == OP_NULL && o->op_targ == OP_HSLICE);
1682 SV *keysv = NULL; /* just to silence compiler warnings */
1683 const char *key = NULL;
1685 if (!(o->op_private & OPpSLICEWARNING))
1687 if (PL_parser && PL_parser->error_count)
1688 /* This warning can be nonsensical when there is a syntax error. */
1691 kid = cLISTOPo->op_first;
1692 kid = OpSIBLING(kid); /* get past pushmark */
1693 /* weed out false positives: any ops that can return lists */
1694 switch (kid->op_type) {
1720 /* Don't warn if we have a nulled list either. */
1721 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1724 assert(OpSIBLING(kid));
1725 name = S_op_varname(aTHX_ OpSIBLING(kid));
1726 if (!name) /* XS module fiddling with the op tree */
1728 S_op_pretty(aTHX_ kid, &keysv, &key);
1729 assert(SvPOK(name));
1730 sv_chop(name,SvPVX(name)+1);
1732 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1733 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1734 "Scalar value @%" SVf "%c%s%c better written as $%" SVf
1736 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1737 lbrack, key, rbrack);
1739 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1740 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1741 "Scalar value @%" SVf "%c%" SVf "%c better written as $%"
1743 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1744 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1748 Perl_scalar(pTHX_ OP *o)
1752 /* assumes no premature commitment */
1753 if (!o || (PL_parser && PL_parser->error_count)
1754 || (o->op_flags & OPf_WANT)
1755 || o->op_type == OP_RETURN)
1760 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1762 switch (o->op_type) {
1764 scalar(cBINOPo->op_first);
1765 if (o->op_private & OPpREPEAT_DOLIST) {
1766 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1767 assert(kid->op_type == OP_PUSHMARK);
1768 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1769 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1770 o->op_private &=~ OPpREPEAT_DOLIST;
1777 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
1787 if (o->op_flags & OPf_KIDS) {
1788 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
1794 kid = cLISTOPo->op_first;
1796 kid = OpSIBLING(kid);
1799 OP *sib = OpSIBLING(kid);
1800 if (sib && kid->op_type != OP_LEAVEWHEN
1801 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
1802 || ( sib->op_targ != OP_NEXTSTATE
1803 && sib->op_targ != OP_DBSTATE )))
1809 PL_curcop = &PL_compiling;
1814 kid = cLISTOPo->op_first;
1817 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1822 /* Warn about scalar context */
1823 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1824 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1827 const char *key = NULL;
1829 /* This warning can be nonsensical when there is a syntax error. */
1830 if (PL_parser && PL_parser->error_count)
1833 if (!ckWARN(WARN_SYNTAX)) break;
1835 kid = cLISTOPo->op_first;
1836 kid = OpSIBLING(kid); /* get past pushmark */
1837 assert(OpSIBLING(kid));
1838 name = S_op_varname(aTHX_ OpSIBLING(kid));
1839 if (!name) /* XS module fiddling with the op tree */
1841 S_op_pretty(aTHX_ kid, &keysv, &key);
1842 assert(SvPOK(name));
1843 sv_chop(name,SvPVX(name)+1);
1845 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1846 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1847 "%%%" SVf "%c%s%c in scalar context better written "
1848 "as $%" SVf "%c%s%c",
1849 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1850 lbrack, key, rbrack);
1852 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1853 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1854 "%%%" SVf "%c%" SVf "%c in scalar context better "
1855 "written as $%" SVf "%c%" SVf "%c",
1856 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1857 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1864 Perl_scalarvoid(pTHX_ OP *arg)
1869 SSize_t defer_stack_alloc = 0;
1870 SSize_t defer_ix = -1;
1871 OP **defer_stack = NULL;
1874 PERL_ARGS_ASSERT_SCALARVOID;
1878 SV *useless_sv = NULL;
1879 const char* useless = NULL;
1881 if (o->op_type == OP_NEXTSTATE
1882 || o->op_type == OP_DBSTATE
1883 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1884 || o->op_targ == OP_DBSTATE)))
1885 PL_curcop = (COP*)o; /* for warning below */
1887 /* assumes no premature commitment */
1888 want = o->op_flags & OPf_WANT;
1889 if ((want && want != OPf_WANT_SCALAR)
1890 || (PL_parser && PL_parser->error_count)
1891 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1896 if ((o->op_private & OPpTARGET_MY)
1897 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1899 /* newASSIGNOP has already applied scalar context, which we
1900 leave, as if this op is inside SASSIGN. */
1904 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1906 switch (o->op_type) {
1908 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1912 if (o->op_flags & OPf_STACKED)
1914 if (o->op_type == OP_REPEAT)
1915 scalar(cBINOPo->op_first);
1918 if (o->op_private == 4)
1953 case OP_GETSOCKNAME:
1954 case OP_GETPEERNAME:
1959 case OP_GETPRIORITY:
1984 useless = OP_DESC(o);
1994 case OP_AELEMFAST_LEX:
1998 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1999 /* Otherwise it's "Useless use of grep iterator" */
2000 useless = OP_DESC(o);
2004 if (!(o->op_private & OPpSPLIT_ASSIGN))
2005 useless = OP_DESC(o);
2009 kid = cUNOPo->op_first;
2010 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2011 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2014 useless = "negative pattern binding (!~)";
2018 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2019 useless = "non-destructive substitution (s///r)";
2023 useless = "non-destructive transliteration (tr///r)";
2030 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2031 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2032 useless = "a variable";
2037 if (cSVOPo->op_private & OPpCONST_STRICT)
2038 no_bareword_allowed(o);
2040 if (ckWARN(WARN_VOID)) {
2042 /* don't warn on optimised away booleans, eg
2043 * use constant Foo, 5; Foo || print; */
2044 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2046 /* the constants 0 and 1 are permitted as they are
2047 conventionally used as dummies in constructs like
2048 1 while some_condition_with_side_effects; */
2049 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2051 else if (SvPOK(sv)) {
2052 SV * const dsv = newSVpvs("");
2054 = Perl_newSVpvf(aTHX_
2056 pv_pretty(dsv, SvPVX_const(sv),
2057 SvCUR(sv), 32, NULL, NULL,
2059 | PERL_PV_ESCAPE_NOCLEAR
2060 | PERL_PV_ESCAPE_UNI_DETECT));
2061 SvREFCNT_dec_NN(dsv);
2063 else if (SvOK(sv)) {
2064 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2067 useless = "a constant (undef)";
2070 op_null(o); /* don't execute or even remember it */
2074 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2078 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2082 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2086 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2091 UNOP *refgen, *rv2cv;
2094 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2097 rv2gv = ((BINOP *)o)->op_last;
2098 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2101 refgen = (UNOP *)((BINOP *)o)->op_first;
2103 if (!refgen || (refgen->op_type != OP_REFGEN
2104 && refgen->op_type != OP_SREFGEN))
2107 exlist = (LISTOP *)refgen->op_first;
2108 if (!exlist || exlist->op_type != OP_NULL
2109 || exlist->op_targ != OP_LIST)
2112 if (exlist->op_first->op_type != OP_PUSHMARK
2113 && exlist->op_first != exlist->op_last)
2116 rv2cv = (UNOP*)exlist->op_last;
2118 if (rv2cv->op_type != OP_RV2CV)
2121 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2122 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2123 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2125 o->op_private |= OPpASSIGN_CV_TO_GV;
2126 rv2gv->op_private |= OPpDONT_INIT_GV;
2127 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2139 kid = cLOGOPo->op_first;
2140 if (kid->op_type == OP_NOT
2141 && (kid->op_flags & OPf_KIDS)) {
2142 if (o->op_type == OP_AND) {
2143 OpTYPE_set(o, OP_OR);
2145 OpTYPE_set(o, OP_AND);
2155 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2156 if (!(kid->op_flags & OPf_KIDS))
2163 if (o->op_flags & OPf_STACKED)
2170 if (!(o->op_flags & OPf_KIDS))
2181 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2182 if (!(kid->op_flags & OPf_KIDS))
2188 /* If the first kid after pushmark is something that the padrange
2189 optimisation would reject, then null the list and the pushmark.
2191 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2192 && ( !(kid = OpSIBLING(kid))
2193 || ( kid->op_type != OP_PADSV
2194 && kid->op_type != OP_PADAV
2195 && kid->op_type != OP_PADHV)
2196 || kid->op_private & ~OPpLVAL_INTRO
2197 || !(kid = OpSIBLING(kid))
2198 || ( kid->op_type != OP_PADSV
2199 && kid->op_type != OP_PADAV
2200 && kid->op_type != OP_PADHV)
2201 || kid->op_private & ~OPpLVAL_INTRO)
2203 op_null(cUNOPo->op_first); /* NULL the pushmark */
2204 op_null(o); /* NULL the list */
2216 /* mortalise it, in case warnings are fatal. */
2217 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2218 "Useless use of %" SVf " in void context",
2219 SVfARG(sv_2mortal(useless_sv)));
2222 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2223 "Useless use of %s in void context",
2226 } while ( (o = POP_DEFERRED_OP()) );
2228 Safefree(defer_stack);
2234 S_listkids(pTHX_ OP *o)
2236 if (o && o->op_flags & OPf_KIDS) {
2238 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2245 Perl_list(pTHX_ OP *o)
2249 /* assumes no premature commitment */
2250 if (!o || (o->op_flags & OPf_WANT)
2251 || (PL_parser && PL_parser->error_count)
2252 || o->op_type == OP_RETURN)
2257 if ((o->op_private & OPpTARGET_MY)
2258 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2260 return o; /* As if inside SASSIGN */
2263 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2265 switch (o->op_type) {
2267 list(cBINOPo->op_first);
2270 if (o->op_private & OPpREPEAT_DOLIST
2271 && !(o->op_flags & OPf_STACKED))
2273 list(cBINOPo->op_first);
2274 kid = cBINOPo->op_last;
2275 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2276 && SvIVX(kSVOP_sv) == 1)
2278 op_null(o); /* repeat */
2279 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2281 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2288 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
2296 if (!(o->op_flags & OPf_KIDS))
2298 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2299 list(cBINOPo->op_first);
2300 return gen_constant_list(o);
2306 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2307 op_null(cUNOPo->op_first); /* NULL the pushmark */
2308 op_null(o); /* NULL the list */
2313 kid = cLISTOPo->op_first;
2315 kid = OpSIBLING(kid);
2318 OP *sib = OpSIBLING(kid);
2319 if (sib && kid->op_type != OP_LEAVEWHEN)
2325 PL_curcop = &PL_compiling;
2329 kid = cLISTOPo->op_first;
2336 S_scalarseq(pTHX_ OP *o)
2339 const OPCODE type = o->op_type;
2341 if (type == OP_LINESEQ || type == OP_SCOPE ||
2342 type == OP_LEAVE || type == OP_LEAVETRY)
2345 for (kid = cLISTOPo->op_first; kid; kid = sib) {
2346 if ((sib = OpSIBLING(kid))
2347 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2348 || ( sib->op_targ != OP_NEXTSTATE
2349 && sib->op_targ != OP_DBSTATE )))
2354 PL_curcop = &PL_compiling;
2356 o->op_flags &= ~OPf_PARENS;
2357 if (PL_hints & HINT_BLOCK_SCOPE)
2358 o->op_flags |= OPf_PARENS;
2361 o = newOP(OP_STUB, 0);
2366 S_modkids(pTHX_ OP *o, I32 type)
2368 if (o && o->op_flags & OPf_KIDS) {
2370 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2371 op_lvalue(kid, type);
2377 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2378 * const fields. Also, convert CONST keys to HEK-in-SVs.
2379 * rop is the op that retrieves the hash;
2380 * key_op is the first key
2384 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op)
2390 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2392 if (rop->op_first->op_type == OP_PADSV)
2393 /* @$hash{qw(keys here)} */
2394 rop = (UNOP*)rop->op_first;
2396 /* @{$hash}{qw(keys here)} */
2397 if (rop->op_first->op_type == OP_SCOPE
2398 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2400 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2407 lexname = NULL; /* just to silence compiler warnings */
2408 fields = NULL; /* just to silence compiler warnings */
2412 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2413 SvPAD_TYPED(lexname))
2414 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2415 && isGV(*fields) && GvHV(*fields);
2417 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
2419 if (key_op->op_type != OP_CONST)
2421 svp = cSVOPx_svp(key_op);
2423 /* make sure it's not a bareword under strict subs */
2424 if (key_op->op_private & OPpCONST_BARE &&
2425 key_op->op_private & OPpCONST_STRICT)
2427 no_bareword_allowed((OP*)key_op);
2430 /* Make the CONST have a shared SV */
2431 if ( !SvIsCOW_shared_hash(sv = *svp)
2432 && SvTYPE(sv) < SVt_PVMG
2437 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2438 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2439 SvREFCNT_dec_NN(sv);
2444 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2446 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2447 "in variable %" PNf " of type %" HEKf,
2448 SVfARG(*svp), PNfARG(lexname),
2449 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2455 /* do all the final processing on an optree (e.g. running the peephole
2456 * optimiser on it), then attach it to cv (if cv is non-null)
2460 S_process_optree(pTHX_ CV *cv, OP *optree, OP* start)
2464 /* XXX for some reason, evals, require and main optrees are
2465 * never attached to their CV; instead they just hang off
2466 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start
2467 * and get manually freed when appropriate */
2469 startp = &CvSTART(cv);
2471 startp = PL_in_eval? &PL_eval_start : &PL_main_start;
2474 optree->op_private |= OPpREFCOUNTED;
2475 OpREFCNT_set(optree, 1);
2477 finalize_optree(optree);
2478 S_prune_chain_head(startp);
2481 /* now that optimizer has done its work, adjust pad values */
2482 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT
2483 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
2489 =for apidoc finalize_optree
2491 This function finalizes the optree. Should be called directly after
2492 the complete optree is built. It does some additional
2493 checking which can't be done in the normal C<ck_>xxx functions and makes
2494 the tree thread-safe.
2499 Perl_finalize_optree(pTHX_ OP* o)
2501 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2504 SAVEVPTR(PL_curcop);
2512 /* Relocate sv to the pad for thread safety.
2513 * Despite being a "constant", the SV is written to,
2514 * for reference counts, sv_upgrade() etc. */
2515 PERL_STATIC_INLINE void
2516 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2519 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2521 ix = pad_alloc(OP_CONST, SVf_READONLY);
2522 SvREFCNT_dec(PAD_SVl(ix));
2523 PAD_SETSV(ix, *svp);
2524 /* XXX I don't know how this isn't readonly already. */
2525 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2533 S_finalize_op(pTHX_ OP* o)
2535 PERL_ARGS_ASSERT_FINALIZE_OP;
2537 assert(o->op_type != OP_FREED);
2539 switch (o->op_type) {
2542 PL_curcop = ((COP*)o); /* for warnings */
2545 if (OpHAS_SIBLING(o)) {
2546 OP *sib = OpSIBLING(o);
2547 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2548 && ckWARN(WARN_EXEC)
2549 && OpHAS_SIBLING(sib))
2551 const OPCODE type = OpSIBLING(sib)->op_type;
2552 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2553 const line_t oldline = CopLINE(PL_curcop);
2554 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2555 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2556 "Statement unlikely to be reached");
2557 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2558 "\t(Maybe you meant system() when you said exec()?)\n");
2559 CopLINE_set(PL_curcop, oldline);
2566 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2567 GV * const gv = cGVOPo_gv;
2568 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2569 /* XXX could check prototype here instead of just carping */
2570 SV * const sv = sv_newmortal();
2571 gv_efullname3(sv, gv, NULL);
2572 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2573 "%" SVf "() called too early to check prototype",
2580 if (cSVOPo->op_private & OPpCONST_STRICT)
2581 no_bareword_allowed(o);
2585 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2590 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2591 case OP_METHOD_NAMED:
2592 case OP_METHOD_SUPER:
2593 case OP_METHOD_REDIR:
2594 case OP_METHOD_REDIR_SUPER:
2595 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2604 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2607 rop = (UNOP*)((BINOP*)o)->op_first;
2612 S_scalar_slice_warning(aTHX_ o);
2616 kid = OpSIBLING(cLISTOPo->op_first);
2617 if (/* I bet there's always a pushmark... */
2618 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2619 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2624 key_op = (SVOP*)(kid->op_type == OP_CONST
2626 : OpSIBLING(kLISTOP->op_first));
2628 rop = (UNOP*)((LISTOP*)o)->op_last;
2631 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2633 S_check_hash_fields_and_hekify(aTHX_ rop, key_op);
2637 if (o->op_targ != OP_HSLICE && o->op_targ != OP_ASLICE)
2641 S_scalar_slice_warning(aTHX_ o);
2645 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2646 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2653 if (o->op_flags & OPf_KIDS) {
2657 /* check that op_last points to the last sibling, and that
2658 * the last op_sibling/op_sibparent field points back to the
2659 * parent, and that the only ops with KIDS are those which are
2660 * entitled to them */
2661 U32 type = o->op_type;
2665 if (type == OP_NULL) {
2667 /* ck_glob creates a null UNOP with ex-type GLOB
2668 * (which is a list op. So pretend it wasn't a listop */
2669 if (type == OP_GLOB)
2672 family = PL_opargs[type] & OA_CLASS_MASK;
2674 has_last = ( family == OA_BINOP
2675 || family == OA_LISTOP
2676 || family == OA_PMOP
2677 || family == OA_LOOP
2679 assert( has_last /* has op_first and op_last, or ...
2680 ... has (or may have) op_first: */
2681 || family == OA_UNOP
2682 || family == OA_UNOP_AUX
2683 || family == OA_LOGOP
2684 || family == OA_BASEOP_OR_UNOP
2685 || family == OA_FILESTATOP
2686 || family == OA_LOOPEXOP
2687 || family == OA_METHOP
2688 || type == OP_CUSTOM
2689 || type == OP_NULL /* new_logop does this */
2692 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
2693 # ifdef PERL_OP_PARENT
2694 if (!OpHAS_SIBLING(kid)) {
2696 assert(kid == cLISTOPo->op_last);
2697 assert(kid->op_sibparent == o);
2700 if (has_last && !OpHAS_SIBLING(kid))
2701 assert(kid == cLISTOPo->op_last);
2706 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
2712 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2714 Propagate lvalue ("modifiable") context to an op and its children.
2715 C<type> represents the context type, roughly based on the type of op that
2716 would do the modifying, although C<local()> is represented by C<OP_NULL>,
2717 because it has no op type of its own (it is signalled by a flag on
2720 This function detects things that can't be modified, such as C<$x+1>, and
2721 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2722 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
2724 It also flags things that need to behave specially in an lvalue context,
2725 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2731 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2734 PadnameLVALUE_on(pn);
2735 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2737 /* RT #127786: cv can be NULL due to an eval within the DB package
2738 * called from an anon sub - anon subs don't have CvOUTSIDE() set
2739 * unless they contain an eval, but calling eval within DB
2740 * pretends the eval was done in the caller's scope.
2744 assert(CvPADLIST(cv));
2746 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2747 assert(PadnameLEN(pn));
2748 PadnameLVALUE_on(pn);
2753 S_vivifies(const OPCODE type)
2756 case OP_RV2AV: case OP_ASLICE:
2757 case OP_RV2HV: case OP_KVASLICE:
2758 case OP_RV2SV: case OP_HSLICE:
2759 case OP_AELEMFAST: case OP_KVHSLICE:
2768 S_lvref(pTHX_ OP *o, I32 type)
2772 switch (o->op_type) {
2774 for (kid = OpSIBLING(cUNOPo->op_first); kid;
2775 kid = OpSIBLING(kid))
2776 S_lvref(aTHX_ kid, type);
2781 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2782 o->op_flags |= OPf_STACKED;
2783 if (o->op_flags & OPf_PARENS) {
2784 if (o->op_private & OPpLVAL_INTRO) {
2785 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2786 "localized parenthesized array in list assignment"));
2790 OpTYPE_set(o, OP_LVAVREF);
2791 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2792 o->op_flags |= OPf_MOD|OPf_REF;
2795 o->op_private |= OPpLVREF_AV;
2798 kid = cUNOPo->op_first;
2799 if (kid->op_type == OP_NULL)
2800 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
2802 o->op_private = OPpLVREF_CV;
2803 if (kid->op_type == OP_GV)
2804 o->op_flags |= OPf_STACKED;
2805 else if (kid->op_type == OP_PADCV) {
2806 o->op_targ = kid->op_targ;
2808 op_free(cUNOPo->op_first);
2809 cUNOPo->op_first = NULL;
2810 o->op_flags &=~ OPf_KIDS;
2815 if (o->op_flags & OPf_PARENS) {
2817 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2818 "parenthesized hash in list assignment"));
2821 o->op_private |= OPpLVREF_HV;
2825 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2826 o->op_flags |= OPf_STACKED;
2829 if (o->op_flags & OPf_PARENS) goto parenhash;
2830 o->op_private |= OPpLVREF_HV;
2833 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2836 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2837 if (o->op_flags & OPf_PARENS) goto slurpy;
2838 o->op_private |= OPpLVREF_AV;
2842 o->op_private |= OPpLVREF_ELEM;
2843 o->op_flags |= OPf_STACKED;
2847 OpTYPE_set(o, OP_LVREFSLICE);
2848 o->op_private &= OPpLVAL_INTRO;
2851 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2853 else if (!(o->op_flags & OPf_KIDS))
2855 if (o->op_targ != OP_LIST) {
2856 S_lvref(aTHX_ cBINOPo->op_first, type);
2861 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
2862 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2863 S_lvref(aTHX_ kid, type);
2867 if (o->op_flags & OPf_PARENS)
2872 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2873 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2874 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2880 OpTYPE_set(o, OP_LVREF);
2882 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2883 if (type == OP_ENTERLOOP)
2884 o->op_private |= OPpLVREF_ITER;
2887 PERL_STATIC_INLINE bool
2888 S_potential_mod_type(I32 type)
2890 /* Types that only potentially result in modification. */
2891 return type == OP_GREPSTART || type == OP_ENTERSUB
2892 || type == OP_REFGEN || type == OP_LEAVESUBLV;
2896 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2900 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2903 if (!o || (PL_parser && PL_parser->error_count))
2906 if ((o->op_private & OPpTARGET_MY)
2907 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2912 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2914 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2916 switch (o->op_type) {
2921 if ((o->op_flags & OPf_PARENS))
2925 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2926 !(o->op_flags & OPf_STACKED)) {
2927 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
2928 assert(cUNOPo->op_first->op_type == OP_NULL);
2929 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2932 else { /* lvalue subroutine call */
2933 o->op_private |= OPpLVAL_INTRO;
2934 PL_modcount = RETURN_UNLIMITED_NUMBER;
2935 if (S_potential_mod_type(type)) {
2936 o->op_private |= OPpENTERSUB_INARGS;
2939 else { /* Compile-time error message: */
2940 OP *kid = cUNOPo->op_first;
2945 if (kid->op_type != OP_PUSHMARK) {
2946 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2948 "panic: unexpected lvalue entersub "
2949 "args: type/targ %ld:%" UVuf,
2950 (long)kid->op_type, (UV)kid->op_targ);
2951 kid = kLISTOP->op_first;
2953 while (OpHAS_SIBLING(kid))
2954 kid = OpSIBLING(kid);
2955 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2956 break; /* Postpone until runtime */
2959 kid = kUNOP->op_first;
2960 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2961 kid = kUNOP->op_first;
2962 if (kid->op_type == OP_NULL)
2964 "Unexpected constant lvalue entersub "
2965 "entry via type/targ %ld:%" UVuf,
2966 (long)kid->op_type, (UV)kid->op_targ);
2967 if (kid->op_type != OP_GV) {
2974 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2975 ? MUTABLE_CV(SvRV(gv))
2981 if (flags & OP_LVALUE_NO_CROAK)
2984 namesv = cv_name(cv, NULL, 0);
2985 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
2986 "subroutine call of &%" SVf " in %s",
2987 SVfARG(namesv), PL_op_desc[type]),
2995 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2996 /* grep, foreach, subcalls, refgen */
2997 if (S_potential_mod_type(type))
2999 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
3000 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
3003 type ? PL_op_desc[type] : "local"));
3016 case OP_RIGHT_SHIFT:
3025 if (!(o->op_flags & OPf_STACKED))
3031 if (o->op_flags & OPf_STACKED) {
3035 if (!(o->op_private & OPpREPEAT_DOLIST))
3038 const I32 mods = PL_modcount;
3039 modkids(cBINOPo->op_first, type);
3040 if (type != OP_AASSIGN)
3042 kid = cBINOPo->op_last;
3043 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
3044 const IV iv = SvIV(kSVOP_sv);
3045 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
3047 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
3050 PL_modcount = RETURN_UNLIMITED_NUMBER;
3056 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3057 op_lvalue(kid, type);
3062 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3063 PL_modcount = RETURN_UNLIMITED_NUMBER;
3064 return o; /* Treat \(@foo) like ordinary list. */
3068 if (scalar_mod_type(o, type))
3070 ref(cUNOPo->op_first, o->op_type);
3077 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
3078 if (type == OP_LEAVESUBLV && (
3079 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
3080 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3082 o->op_private |= OPpMAYBE_LVSUB;
3086 PL_modcount = RETURN_UNLIMITED_NUMBER;
3091 if (type == OP_LEAVESUBLV)
3092 o->op_private |= OPpMAYBE_LVSUB;
3095 if (type == OP_LEAVESUBLV
3096 && (o->op_private & OPpAVHVSWITCH_MASK) + OP_EACH == OP_KEYS)
3097 o->op_private |= OPpMAYBE_LVSUB;
3100 PL_hints |= HINT_BLOCK_SCOPE;
3101 if (type == OP_LEAVESUBLV)
3102 o->op_private |= OPpMAYBE_LVSUB;
3106 ref(cUNOPo->op_first, o->op_type);
3110 PL_hints |= HINT_BLOCK_SCOPE;
3120 case OP_AELEMFAST_LEX:
3127 PL_modcount = RETURN_UNLIMITED_NUMBER;
3128 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
3129 return o; /* Treat \(@foo) like ordinary list. */
3130 if (scalar_mod_type(o, type))
3132 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3133 && type == OP_LEAVESUBLV)
3134 o->op_private |= OPpMAYBE_LVSUB;
3138 if (!type) /* local() */
3139 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
3140 PNfARG(PAD_COMPNAME(o->op_targ)));
3141 if (!(o->op_private & OPpLVAL_INTRO)
3142 || ( type != OP_SASSIGN && type != OP_AASSIGN
3143 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
3144 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
3152 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
3156 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
3162 if (type == OP_LEAVESUBLV)
3163 o->op_private |= OPpMAYBE_LVSUB;
3164 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
3165 /* substr and vec */
3166 /* If this op is in merely potential (non-fatal) modifiable
3167 context, then apply OP_ENTERSUB context to
3168 the kid op (to avoid croaking). Other-
3169 wise pass this op’s own type so the correct op is mentioned
3170 in error messages. */
3171 op_lvalue(OpSIBLING(cBINOPo->op_first),
3172 S_potential_mod_type(type)
3180 ref(cBINOPo->op_first, o->op_type);
3181 if (type == OP_ENTERSUB &&
3182 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3183 o->op_private |= OPpLVAL_DEFER;
3184 if (type == OP_LEAVESUBLV)
3185 o->op_private |= OPpMAYBE_LVSUB;
3192 o->op_private |= OPpLVALUE;
3198 if (o->op_flags & OPf_KIDS)
3199 op_lvalue(cLISTOPo->op_last, type);
3204 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3206 else if (!(o->op_flags & OPf_KIDS))
3209 if (o->op_targ != OP_LIST) {
3210 OP *sib = OpSIBLING(cLISTOPo->op_first);
3211 /* OP_TRANS and OP_TRANSR with argument have a weird optree
3218 * compared with things like OP_MATCH which have the argument
3224 * so handle specially to correctly get "Can't modify" croaks etc
3227 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
3229 /* this should trigger a "Can't modify transliteration" err */
3230 op_lvalue(sib, type);
3232 op_lvalue(cBINOPo->op_first, type);
3238 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3239 /* elements might be in void context because the list is
3240 in scalar context or because they are attribute sub calls */
3241 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
3242 op_lvalue(kid, type);
3250 if (type == OP_LEAVESUBLV
3251 || !S_vivifies(cLOGOPo->op_first->op_type))
3252 op_lvalue(cLOGOPo->op_first, type);
3253 if (type == OP_LEAVESUBLV
3254 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3255 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
3259 if (type == OP_NULL) { /* local */
3261 if (!FEATURE_MYREF_IS_ENABLED)
3262 Perl_croak(aTHX_ "The experimental declared_refs "
3263 "feature is not enabled");
3264 Perl_ck_warner_d(aTHX_
3265 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
3266 "Declaring references is experimental");
3267 op_lvalue(cUNOPo->op_first, OP_NULL);
3270 if (type != OP_AASSIGN && type != OP_SASSIGN
3271 && type != OP_ENTERLOOP)
3273 /* Don’t bother applying lvalue context to the ex-list. */
3274 kid = cUNOPx(cUNOPo->op_first)->op_first;
3275 assert (!OpHAS_SIBLING(kid));
3278 if (type == OP_NULL) /* local */
3280 if (type != OP_AASSIGN) goto nomod;
3281 kid = cUNOPo->op_first;
3284 const U8 ec = PL_parser ? PL_parser->error_count : 0;
3285 S_lvref(aTHX_ kid, type);
3286 if (!PL_parser || PL_parser->error_count == ec) {
3287 if (!FEATURE_REFALIASING_IS_ENABLED)
3289 "Experimental aliasing via reference not enabled");
3290 Perl_ck_warner_d(aTHX_
3291 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3292 "Aliasing via reference is experimental");
3295 if (o->op_type == OP_REFGEN)
3296 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3301 if ((o->op_private & OPpSPLIT_ASSIGN)) {
3302 /* This is actually @array = split. */
3303 PL_modcount = RETURN_UNLIMITED_NUMBER;
3309 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3313 /* [20011101.069 (#7861)] File test operators interpret OPf_REF to mean that
3314 their argument is a filehandle; thus \stat(".") should not set
3316 if (type == OP_REFGEN &&
3317 PL_check[o->op_type] == Perl_ck_ftst)
3320 if (type != OP_LEAVESUBLV)
3321 o->op_flags |= OPf_MOD;
3323 if (type == OP_AASSIGN || type == OP_SASSIGN)
3324 o->op_flags |= OPf_SPECIAL
3325 |(o->op_type == OP_ENTERSUB ? 0 : OPf_REF);
3326 else if (!type) { /* local() */
3329 o->op_private |= OPpLVAL_INTRO;
3330 o->op_flags &= ~OPf_SPECIAL;
3331 PL_hints |= HINT_BLOCK_SCOPE;
3336 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3337 "Useless localization of %s", OP_DESC(o));
3340 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3341 && type != OP_LEAVESUBLV && o->op_type != OP_ENTERSUB)
3342 o->op_flags |= OPf_REF;
3347 S_scalar_mod_type(const OP *o, I32 type)
3352 if (o && o->op_type == OP_RV2GV)
3376 case OP_RIGHT_SHIFT:
3405 S_is_handle_constructor(const OP *o, I32 numargs)
3407 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3409 switch (o->op_type) {
3417 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3430 S_refkids(pTHX_ OP *o, I32 type)
3432 if (o && o->op_flags & OPf_KIDS) {
3434 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3441 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3446 PERL_ARGS_ASSERT_DOREF;
3448 if (PL_parser && PL_parser->error_count)
3451 switch (o->op_type) {
3453 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3454 !(o->op_flags & OPf_STACKED)) {
3455 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
3456 assert(cUNOPo->op_first->op_type == OP_NULL);
3457 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
3458 o->op_flags |= OPf_SPECIAL;
3460 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3461 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3462 : type == OP_RV2HV ? OPpDEREF_HV
3464 o->op_flags |= OPf_MOD;
3470 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3471 doref(kid, type, set_op_ref);
3474 if (type == OP_DEFINED)
3475 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3476 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3479 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3480 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3481 : type == OP_RV2HV ? OPpDEREF_HV
3483 o->op_flags |= OPf_MOD;
3490 o->op_flags |= OPf_REF;
3493 if (type == OP_DEFINED)
3494 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3495 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3501 o->op_flags |= OPf_REF;
3506 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3508 doref(cBINOPo->op_first, type, set_op_ref);
3512 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3513 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3514 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3515 : type == OP_RV2HV ? OPpDEREF_HV
3517 o->op_flags |= OPf_MOD;
3527 if (!(o->op_flags & OPf_KIDS))
3529 doref(cLISTOPo->op_last, type, set_op_ref);
3539 S_dup_attrlist(pTHX_ OP *o)
3543 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3545 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3546 * where the first kid is OP_PUSHMARK and the remaining ones
3547 * are OP_CONST. We need to push the OP_CONST values.
3549 if (o->op_type == OP_CONST)
3550 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3552 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3554 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
3555 if (o->op_type == OP_CONST)
3556 rop = op_append_elem(OP_LIST, rop,
3557 newSVOP(OP_CONST, o->op_flags,
3558 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3565 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3567 PERL_ARGS_ASSERT_APPLY_ATTRS;
3569 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
3571 /* fake up C<use attributes $pkg,$rv,@attrs> */
3573 #define ATTRSMODULE "attributes"
3574 #define ATTRSMODULE_PM "attributes.pm"
3577 aTHX_ PERL_LOADMOD_IMPORT_OPS,
3578 newSVpvs(ATTRSMODULE),
3580 op_prepend_elem(OP_LIST,
3581 newSVOP(OP_CONST, 0, stashsv),
3582 op_prepend_elem(OP_LIST,
3583 newSVOP(OP_CONST, 0,
3585 dup_attrlist(attrs))));
3590 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3592 OP *pack, *imop, *arg;
3593 SV *meth, *stashsv, **svp;
3595 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3600 assert(target->op_type == OP_PADSV ||
3601 target->op_type == OP_PADHV ||
3602 target->op_type == OP_PADAV);
3604 /* Ensure that attributes.pm is loaded. */
3605 /* Don't force the C<use> if we don't need it. */
3606 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3607 if (svp && *svp != &PL_sv_undef)
3608 NOOP; /* already in %INC */
3610 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3611 newSVpvs(ATTRSMODULE), NULL);
3613 /* Need package name for method call. */
3614 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3616 /* Build up the real arg-list. */
3617 stashsv = newSVhek(HvNAME_HEK(stash));
3619 arg = newOP(OP_PADSV, 0);
3620 arg->op_targ = target->op_targ;
3621 arg = op_prepend_elem(OP_LIST,
3622 newSVOP(OP_CONST, 0, stashsv),
3623 op_prepend_elem(OP_LIST,
3624 newUNOP(OP_REFGEN, 0,
3626 dup_attrlist(attrs)));
3628 /* Fake up a method call to import */
3629 meth = newSVpvs_share("import");
3630 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3631 op_append_elem(OP_LIST,
3632 op_prepend_elem(OP_LIST, pack, arg),
3633 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3635 /* Combine the ops. */
3636 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3640 =notfor apidoc apply_attrs_string
3642 Attempts to apply a list of attributes specified by the C<attrstr> and
3643 C<len> arguments to the subroutine identified by the C<cv> argument which
3644 is expected to be associated with the package identified by the C<stashpv>
3645 argument (see L<attributes>). It gets this wrong, though, in that it
3646 does not correctly identify the boundaries of the individual attribute
3647 specifications within C<attrstr>. This is not really intended for the
3648 public API, but has to be listed here for systems such as AIX which
3649 need an explicit export list for symbols. (It's called from XS code
3650 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3651 to respect attribute syntax properly would be welcome.
3657 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3658 const char *attrstr, STRLEN len)
3662 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3665 len = strlen(attrstr);
3669 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3671 const char * const sstr = attrstr;
3672 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3673 attrs = op_append_elem(OP_LIST, attrs,
3674 newSVOP(OP_CONST, 0,
3675 newSVpvn(sstr, attrstr-sstr)));
3679 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3680 newSVpvs(ATTRSMODULE),
3681 NULL, op_prepend_elem(OP_LIST,
3682 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3683 op_prepend_elem(OP_LIST,
3684 newSVOP(OP_CONST, 0,
3685 newRV(MUTABLE_SV(cv))),
3690 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name,
3693 OP *new_proto = NULL;
3698 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3704 if (o->op_type == OP_CONST) {
3705 pv = SvPV(cSVOPo_sv, pvlen);
3706 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3707 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3708 SV ** const tmpo = cSVOPx_svp(o);
3709 SvREFCNT_dec(cSVOPo_sv);
3714 } else if (o->op_type == OP_LIST) {
3716 assert(o->op_flags & OPf_KIDS);
3717 lasto = cLISTOPo->op_first;
3718 assert(lasto->op_type == OP_PUSHMARK);
3719 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
3720 if (o->op_type == OP_CONST) {
3721 pv = SvPV(cSVOPo_sv, pvlen);
3722 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3723 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3724 SV ** const tmpo = cSVOPx_svp(o);
3725 SvREFCNT_dec(cSVOPo_sv);
3727 if (new_proto && ckWARN(WARN_MISC)) {
3729 const char * newp = SvPV(cSVOPo_sv, new_len);
3730 Perl_warner(aTHX_ packWARN(WARN_MISC),
3731 "Attribute prototype(%" UTF8f ") discards earlier prototype attribute in same sub",
3732 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3738 /* excise new_proto from the list */
3739 op_sibling_splice(*attrs, lasto, 1, NULL);
3746 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3747 would get pulled in with no real need */
3748 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3757 svname = sv_newmortal();
3758 gv_efullname3(svname, name, NULL);
3760 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3761 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3763 svname = (SV *)name;
3764 if (ckWARN(WARN_ILLEGALPROTO))
3765 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE,
3767 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3768 STRLEN old_len, new_len;
3769 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3770 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3772 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3773 "Prototype '%" UTF8f "' overridden by attribute 'prototype(%" UTF8f ")'"
3775 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3776 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3786 S_cant_declare(pTHX_ OP *o)
3788 if (o->op_type == OP_NULL
3789 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3790 o = cUNOPo->op_first;
3791 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3792 o->op_type == OP_NULL
3793 && o->op_flags & OPf_SPECIAL
3796 PL_parser->in_my == KEY_our ? "our" :
3797 PL_parser->in_my == KEY_state ? "state" :
3802 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3805 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3807 PERL_ARGS_ASSERT_MY_KID;
3809 if (!o || (PL_parser && PL_parser->error_count))
3814 if (OP_TYPE_IS_OR_WAS(o, OP_LIST)) {
3816 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3817 my_kid(kid, attrs, imopsp);
3819 } else if (type == OP_UNDEF || type == OP_STUB) {
3821 } else if (type == OP_RV2SV || /* "our" declaration */
3824 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3825 S_cant_declare(aTHX_ o);
3827 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3829 PL_parser->in_my = FALSE;
3830 PL_parser->in_my_stash = NULL;
3831 apply_attrs(GvSTASH(gv),
3832 (type == OP_RV2SV ? GvSVn(gv) :
3833 type == OP_RV2AV ? MUTABLE_SV(GvAVn(gv)) :
3834 type == OP_RV2HV ? MUTABLE_SV(GvHVn(gv)) : MUTABLE_SV(gv)),
3837 o->op_private |= OPpOUR_INTRO;
3840 else if (type == OP_REFGEN || type == OP_SREFGEN) {
3841 if (!FEATURE_MYREF_IS_ENABLED)
3842 Perl_croak(aTHX_ "The experimental declared_refs "
3843 "feature is not enabled");
3844 Perl_ck_warner_d(aTHX_
3845 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
3846 "Declaring references is experimental");
3847 /* Kid is a nulled OP_LIST, handled above. */
3848 my_kid(cUNOPo->op_first, attrs, imopsp);
3851 else if (type != OP_PADSV &&
3854 type != OP_PUSHMARK)
3856 S_cant_declare(aTHX_ o);
3859 else if (attrs && type != OP_PUSHMARK) {
3863 PL_parser->in_my = FALSE;
3864 PL_parser->in_my_stash = NULL;
3866 /* check for C<my Dog $spot> when deciding package */
3867 stash = PAD_COMPNAME_TYPE(o->op_targ);
3869 stash = PL_curstash;
3870 apply_attrs_my(stash, o, attrs, imopsp);
3872 o->op_flags |= OPf_MOD;
3873 o->op_private |= OPpLVAL_INTRO;
3875 o->op_private |= OPpPAD_STATE;
3880 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3883 int maybe_scalar = 0;
3885 PERL_ARGS_ASSERT_MY_ATTRS;
3887 /* [perl #17376]: this appears to be premature, and results in code such as
3888 C< our(%x); > executing in list mode rather than void mode */
3890 if (o->op_flags & OPf_PARENS)
3900 o = my_kid(o, attrs, &rops);
3902 if (maybe_scalar && o->op_type == OP_PADSV) {
3903 o = scalar(op_append_list(OP_LIST, rops, o));
3904 o->op_private |= OPpLVAL_INTRO;
3907 /* The listop in rops might have a pushmark at the beginning,
3908 which will mess up list assignment. */
3909 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3910 if (rops->op_type == OP_LIST &&
3911 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3913 OP * const pushmark = lrops->op_first;
3914 /* excise pushmark */
3915 op_sibling_splice(rops, NULL, 1, NULL);
3918 o = op_append_list(OP_LIST, o, rops);
3921 PL_parser->in_my = FALSE;
3922 PL_parser->in_my_stash = NULL;
3927 Perl_sawparens(pTHX_ OP *o)
3929 PERL_UNUSED_CONTEXT;
3931 o->op_flags |= OPf_PARENS;
3936 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3940 const OPCODE ltype = left->op_type;
3941 const OPCODE rtype = right->op_type;
3943 PERL_ARGS_ASSERT_BIND_MATCH;
3945 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3946 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3948 const char * const desc
3950 rtype == OP_SUBST || rtype == OP_TRANS
3951 || rtype == OP_TRANSR
3953 ? (int)rtype : OP_MATCH];
3954 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3956 S_op_varname(aTHX_ left);
3958 Perl_warner(aTHX_ packWARN(WARN_MISC),
3959 "Applying %s to %" SVf " will act on scalar(%" SVf ")",
3960 desc, SVfARG(name), SVfARG(name));
3962 const char * const sample = (isary
3963 ? "@array" : "%hash");
3964 Perl_warner(aTHX_ packWARN(WARN_MISC),
3965 "Applying %s to %s will act on scalar(%s)",
3966 desc, sample, sample);
3970 if (rtype == OP_CONST &&
3971 cSVOPx(right)->op_private & OPpCONST_BARE &&
3972 cSVOPx(right)->op_private & OPpCONST_STRICT)
3974 no_bareword_allowed(right);
3977 /* !~ doesn't make sense with /r, so error on it for now */
3978 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3980 /* diag_listed_as: Using !~ with %s doesn't make sense */
3981 yyerror("Using !~ with s///r doesn't make sense");
3982 if (rtype == OP_TRANSR && type == OP_NOT)
3983 /* diag_listed_as: Using !~ with %s doesn't make sense */
3984 yyerror("Using !~ with tr///r doesn't make sense");
3986 ismatchop = (rtype == OP_MATCH ||
3987 rtype == OP_SUBST ||
3988 rtype == OP_TRANS || rtype == OP_TRANSR)
3989 && !(right->op_flags & OPf_SPECIAL);
3990 if (ismatchop && right->op_private & OPpTARGET_MY) {
3992 right->op_private &= ~OPpTARGET_MY;
3994 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
3995 if (left->op_type == OP_PADSV
3996 && !(left->op_private & OPpLVAL_INTRO))
3998 right->op_targ = left->op_targ;
4003 right->op_flags |= OPf_STACKED;
4004 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
4005 ! (rtype == OP_TRANS &&
4006 right->op_private & OPpTRANS_IDENTICAL) &&
4007 ! (rtype == OP_SUBST &&
4008 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
4009 left = op_lvalue(left, rtype);
4010 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
4011 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
4013 o = op_prepend_elem(rtype, scalar(left), right);
4016 return newUNOP(OP_NOT, 0, scalar(o));
4020 return bind_match(type, left,
4021 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
4025 Perl_invert(pTHX_ OP *o)
4029 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
4033 =for apidoc Amx|OP *|op_scope|OP *o
4035 Wraps up an op tree with some additional ops so that at runtime a dynamic
4036 scope will be created. The original ops run in the new dynamic scope,
4037 and then, provided that they exit normally, the scope will be unwound.
4038 The additional ops used to create and unwind the dynamic scope will
4039 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
4040 instead if the ops are simple enough to not need the full dynamic scope
4047 Perl_op_scope(pTHX_ OP *o)
4051 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
4052 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
4053 OpTYPE_set(o, OP_LEAVE);
4055 else if (o->op_type == OP_LINESEQ) {
4057 OpTYPE_set(o, OP_SCOPE);
4058 kid = ((LISTOP*)o)->op_first;
4059 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
4062 /* The following deals with things like 'do {1 for 1}' */
4063 kid = OpSIBLING(kid);
4065 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
4070 o = newLISTOP(OP_SCOPE, 0, o, NULL);
4076 Perl_op_unscope(pTHX_ OP *o)
4078 if (o && o->op_type == OP_LINESEQ) {
4079 OP *kid = cLISTOPo->op_first;
4080 for(; kid; kid = OpSIBLING(kid))
4081 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
4088 =for apidoc Am|int|block_start|int full
4090 Handles compile-time scope entry.
4091 Arranges for hints to be restored on block
4092 exit and also handles pad sequence numbers to make lexical variables scope
4093 right. Returns a savestack index for use with C<block_end>.
4099 Perl_block_start(pTHX_ int full)
4101 const int retval = PL_savestack_ix;
4103 PL_compiling.cop_seq = PL_cop_seqmax;
4105 pad_block_start(full);
4107 PL_hints &= ~HINT_BLOCK_SCOPE;
4108 SAVECOMPILEWARNINGS();
4109 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
4110 SAVEI32(PL_compiling.cop_seq);
4111 PL_compiling.cop_seq = 0;
4113 CALL_BLOCK_HOOKS(bhk_start, full);
4119 =for apidoc Am|OP *|block_end|I32 floor|OP *seq
4121 Handles compile-time scope exit. C<floor>
4122 is the savestack index returned by
4123 C<block_start>, and C<seq> is the body of the block. Returns the block,
4130 Perl_block_end(pTHX_ I32 floor, OP *seq)
4132 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
4133 OP* retval = scalarseq(seq);
4136 /* XXX Is the null PL_parser check necessary here? */
4137 assert(PL_parser); /* Let’s find out under debugging builds. */
4138 if (PL_parser && PL_parser->parsed_sub) {
4139 o = newSTATEOP(0, NULL, NULL);
4141 retval = op_append_elem(OP_LINESEQ, retval, o);
4144 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
4148 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
4152 /* pad_leavemy has created a sequence of introcv ops for all my
4153 subs declared in the block. We have to replicate that list with
4154 clonecv ops, to deal with this situation:
4159 sub s1 { state sub foo { \&s2 } }
4162 Originally, I was going to have introcv clone the CV and turn
4163 off the stale flag. Since &s1 is declared before &s2, the
4164 introcv op for &s1 is executed (on sub entry) before the one for
4165 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
4166 cloned, since it is a state sub) closes over &s2 and expects
4167 to see it in its outer CV’s pad. If the introcv op clones &s1,
4168 then &s2 is still marked stale. Since &s1 is not active, and
4169 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
4170 ble will not stay shared’ warning. Because it is the same stub
4171 that will be used when the introcv op for &s2 is executed, clos-
4172 ing over it is safe. Hence, we have to turn off the stale flag
4173 on all lexical subs in the block before we clone any of them.
4174 Hence, having introcv clone the sub cannot work. So we create a
4175 list of ops like this:
4199 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
4200 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
4201 for (;; kid = OpSIBLING(kid)) {
4202 OP *newkid = newOP(OP_CLONECV, 0);
4203 newkid->op_targ = kid->op_targ;
4204 o = op_append_elem(OP_LINESEQ, o, newkid);
4205 if (kid == last) break;
4207 retval = op_prepend_elem(OP_LINESEQ, o, retval);
4210 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
4216 =head1 Compile-time scope hooks
4218 =for apidoc Aox||blockhook_register
4220 Register a set of hooks to be called when the Perl lexical scope changes
4221 at compile time. See L<perlguts/"Compile-time scope hooks">.
4227 Perl_blockhook_register(pTHX_ BHK *hk)
4229 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
4231 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
4235 Perl_newPROG(pTHX_ OP *o)
4239 PERL_ARGS_ASSERT_NEWPROG;
4246 PL_eval_root = newUNOP(OP_LEAVEEVAL,
4247 ((PL_in_eval & EVAL_KEEPERR)
4248 ? OPf_SPECIAL : 0), o);
4251 assert(CxTYPE(cx) == CXt_EVAL);
4253 if ((cx->blk_gimme & G_WANT) == G_VOID)
4254 scalarvoid(PL_eval_root);
4255 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
4258 scalar(PL_eval_root);
4260 start = op_linklist(PL_eval_root);
4261 PL_eval_root->op_next = 0;
4262 i = PL_savestack_ix;
4265 S_process_optree(aTHX_ NULL, PL_eval_root, start);
4267 PL_savestack_ix = i;
4270 if (o->op_type == OP_STUB) {
4271 /* This block is entered if nothing is compiled for the main
4272 program. This will be the case for an genuinely empty main
4273 program, or one which only has BEGIN blocks etc, so already
4276 Historically (5.000) the guard above was !o. However, commit
4277 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
4278 c71fccf11fde0068, changed perly.y so that newPROG() is now
4279 called with the output of block_end(), which returns a new
4280 OP_STUB for the case of an empty optree. ByteLoader (and
4281 maybe other things) also take this path, because they set up
4282 PL_main_start and PL_main_root directly, without generating an
4285 If the parsing the main program aborts (due to parse errors,
4286 or due to BEGIN or similar calling exit), then newPROG()
4287 isn't even called, and hence this code path and its cleanups
4288 are skipped. This shouldn't make a make a difference:
4289 * a non-zero return from perl_parse is a failure, and
4290 perl_destruct() should be called immediately.
4291 * however, if exit(0) is called during the parse, then
4292 perl_parse() returns 0, and perl_run() is called. As
4293 PL_main_start will be NULL, perl_run() will return
4294 promptly, and the exit code will remain 0.
4297 PL_comppad_name = 0;
4299 S_op_destroy(aTHX_ o);
4302 PL_main_root = op_scope(sawparens(scalarvoid(o)));
4303 PL_curcop = &PL_compiling;
4304 start = LINKLIST(PL_main_root);
4305 PL_main_root->op_next = 0;
4306 S_process_optree(aTHX_ NULL, PL_main_root, start);
4307 cv_forget_slab(PL_compcv);
4310 /* Register with debugger */
4312 CV * const cv = get_cvs("DB::postponed", 0);
4316 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
4318 call_sv(MUTABLE_SV(cv), G_DISCARD);
4325 Perl_localize(pTHX_ OP *o, I32 lex)
4327 PERL_ARGS_ASSERT_LOCALIZE;
4329 if (o->op_flags & OPf_PARENS)
4330 /* [perl #17376]: this appears to be premature, and results in code such as
4331 C< our(%x); > executing in list mode rather than void mode */
4338 if ( PL_parser->bufptr > PL_parser->oldbufptr
4339 && PL_parser->bufptr[-1] == ','
4340 && ckWARN(WARN_PARENTHESIS))
4342 char *s = PL_parser->bufptr;
4345 /* some heuristics to detect a potential error */
4346 while (*s && (strchr(", \t\n", *s)))
4350 if (*s && (strchr("@$%", *s) || (!lex && *s == '*'))
4352 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
4355 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
4357 while (*s && (strchr(", \t\n", *s)))
4363 if (sigil && (*s == ';' || *s == '=')) {
4364 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
4365 "Parentheses missing around \"%s\" list",
4367 ? (PL_parser->in_my == KEY_our
4369 : PL_parser->in_my == KEY_state
4379 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4380 PL_parser->in_my = FALSE;
4381 PL_parser->in_my_stash = NULL;
4386 Perl_jmaybe(pTHX_ OP *o)
4388 PERL_ARGS_ASSERT_JMAYBE;
4390 if (o->op_type == OP_LIST) {
4392 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4393 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4398 PERL_STATIC_INLINE OP *
4399 S_op_std_init(pTHX_ OP *o)
4401 I32 type = o->op_type;
4403 PERL_ARGS_ASSERT_OP_STD_INIT;
4405 if (PL_opargs[type] & OA_RETSCALAR)
4407 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4408 o->op_targ = pad_alloc(type, SVs_PADTMP);
4413 PERL_STATIC_INLINE OP *
4414 S_op_integerize(pTHX_ OP *o)
4416 I32 type = o->op_type;
4418 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4420 /* integerize op. */
4421 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4424 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4427 if (type == OP_NEGATE)
4428 /* XXX might want a ck_negate() for this */
4429 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4435 S_fold_constants(pTHX_ OP *const o)
4440 VOL I32 type = o->op_type;
4445 SV * const oldwarnhook = PL_warnhook;
4446 SV * const olddiehook = PL_diehook;
4448 U8 oldwarn = PL_dowarn;
4452 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4454 if (!(PL_opargs[type] & OA_FOLDCONST))
4463 #ifdef USE_LOCALE_CTYPE
4464 if (IN_LC_COMPILETIME(LC_CTYPE))
4473 #ifdef USE_LOCALE_COLLATE
4474 if (IN_LC_COMPILETIME(LC_COLLATE))
4479 /* XXX what about the numeric ops? */
4480 #ifdef USE_LOCALE_NUMERIC
4481 if (IN_LC_COMPILETIME(LC_NUMERIC))
4486 if (!OpHAS_SIBLING(cLISTOPo->op_first)
4487 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4490 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
4491 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4493 const char *s = SvPVX_const(sv);
4494 while (s < SvEND(sv)) {
4495 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4502 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4505 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4506 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4510 if (PL_parser && PL_parser->error_count)
4511 goto nope; /* Don't try to run w/ errors */
4513 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4514 switch (curop->op_type) {
4516 if ( (curop->op_private & OPpCONST_BARE)
4517 && (curop->op_private & OPpCONST_STRICT)) {
4518 no_bareword_allowed(curop);
4526 /* Foldable; move to next op in list */
4530 /* No other op types are considered foldable */
4535 curop = LINKLIST(o);
4536 old_next = o->op_next;
4540 old_cxix = cxstack_ix;
4541 create_eval_scope(NULL, G_FAKINGEVAL);
4543 /* Verify that we don't need to save it: */
4544 assert(PL_curcop == &PL_compiling);
4545 StructCopy(&PL_compiling, ¬_compiling, COP);
4546 PL_curcop = ¬_compiling;
4547 /* The above ensures that we run with all the correct hints of the
4548 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4549 assert(IN_PERL_RUNTIME);
4550 PL_warnhook = PERL_WARNHOOK_FATAL;
4554 /* Effective $^W=1. */
4555 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4556 PL_dowarn |= G_WARN_ON;
4561 sv = *(PL_stack_sp--);
4562 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4563 pad_swipe(o->op_targ, FALSE);
4565 else if (SvTEMP(sv)) { /* grab mortal temp? */
4566 SvREFCNT_inc_simple_void(sv);
4569 else { assert(SvIMMORTAL(sv)); }
4572 /* Something tried to die. Abandon constant folding. */
4573 /* Pretend the error never happened. */
4575 o->op_next = old_next;
4579 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4580 PL_warnhook = oldwarnhook;
4581 PL_diehook = olddiehook;
4582 /* XXX note that this croak may fail as we've already blown away
4583 * the stack - eg any nested evals */
4584 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4587 PL_dowarn = oldwarn;
4588 PL_warnhook = oldwarnhook;
4589 PL_diehook = olddiehook;
4590 PL_curcop = &PL_compiling;
4592 /* if we croaked, depending on how we croaked the eval scope
4593 * may or may not have already been popped */
4594 if (cxstack_ix > old_cxix) {
4595 assert(cxstack_ix == old_cxix + 1);
4596 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
4597 delete_eval_scope();
4602 /* OP_STRINGIFY and constant folding are used to implement qq.
4603 Here the constant folding is an implementation detail that we
4604 want to hide. If the stringify op is itself already marked
4605 folded, however, then it is actually a folded join. */
4606 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4611 else if (!SvIMMORTAL(sv)) {
4615 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4616 if (!is_stringify) newop->op_folded = 1;
4624 S_gen_constant_list(pTHX_ OP *o)
4627 OP *curop, *old_next;
4628 SV * const oldwarnhook = PL_warnhook;
4629 SV * const olddiehook = PL_diehook;
4631 U8 oldwarn = PL_dowarn;
4641 if (PL_parser && PL_parser->error_count)
4642 return o; /* Don't attempt to run with errors */
4644 curop = LINKLIST(o);
4645 old_next = o->op_next;
4647 op_was_null = o->op_type == OP_NULL;
4648 if (op_was_null) /* b3698342565fb462291fba4b432cfcd05b6eb4e1 */
4649 o->op_type = OP_CUSTOM;
4652 o->op_type = OP_NULL;
4653 S_prune_chain_head(&curop);
4656 old_cxix = cxstack_ix;
4657 create_eval_scope(NULL, G_FAKINGEVAL);
4659 old_curcop = PL_curcop;
4660 StructCopy(old_curcop, ¬_compiling, COP);
4661 PL_curcop = ¬_compiling;
4662 /* The above ensures that we run with all the correct hints of the
4663 current COP, but that IN_PERL_RUNTIME is true. */
4664 assert(IN_PERL_RUNTIME);
4665 PL_warnhook = PERL_WARNHOOK_FATAL;
4669 /* Effective $^W=1. */
4670 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4671 PL_dowarn |= G_WARN_ON;
4675 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
4676 PL_curstackinfo->si_stack_hwm = 0; /* stop valgrind complaining */
4678 Perl_pp_pushmark(aTHX);
4681 assert (!(curop->op_flags & OPf_SPECIAL));
4682 assert(curop->op_type == OP_RANGE);
4683 Perl_pp_anonlist(aTHX);
4687 o->op_next = old_next;
4691 PL_warnhook = oldwarnhook;
4692 PL_diehook = olddiehook;
4693 Perl_croak(aTHX_ "panic: gen_constant_list JMPENV_PUSH returned %d",
4698 PL_dowarn = oldwarn;
4699 PL_warnhook = oldwarnhook;
4700 PL_diehook = olddiehook;
4701 PL_curcop = old_curcop;
4703 if (cxstack_ix > old_cxix) {
4704 assert(cxstack_ix == old_cxix + 1);
4705 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
4706 delete_eval_scope();
4711 OpTYPE_set(o, OP_RV2AV);
4712 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4713 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4714 o->op_opt = 0; /* needs to be revisited in rpeep() */
4715 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4717 /* replace subtree with an OP_CONST */
4718 curop = ((UNOP*)o)->op_first;
4719 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4722 if (AvFILLp(av) != -1)
4723 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4726 SvREADONLY_on(*svp);
4733 =head1 Optree Manipulation Functions
4736 /* List constructors */
4739 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4741 Append an item to the list of ops contained directly within a list-type
4742 op, returning the lengthened list. C<first> is the list-type op,
4743 and C<last> is the op to append to the list. C<optype> specifies the
4744 intended opcode for the list. If C<first> is not already a list of the
4745 right type, it will be upgraded into one. If either C<first> or C<last>
4746 is null, the other is returned unchanged.
4752 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4760 if (first->op_type != (unsigned)type
4761 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4763 return newLISTOP(type, 0, first, last);
4766 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4767 first->op_flags |= OPf_KIDS;
4772 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4774 Concatenate the lists of ops contained directly within two list-type ops,
4775 returning the combined list. C<first> and C<last> are the list-type ops
4776 to concatenate. C<optype> specifies the intended opcode for the list.
4777 If either C<first> or C<last> is not already a list of the right type,
4778 it will be upgraded into one. If either C<first> or C<last> is null,
4779 the other is returned unchanged.
4785 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4793 if (first->op_type != (unsigned)type)
4794 return op_prepend_elem(type, first, last);
4796 if (last->op_type != (unsigned)type)
4797 return op_append_elem(type, first, last);
4799 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4800 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4801 OpLASTSIB_set(((LISTOP*)first)->op_last, first);
4802 first->op_flags |= (last->op_flags & OPf_KIDS);
4804 S_op_destroy(aTHX_ last);
4810 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4812 Prepend an item to the list of ops contained directly within a list-type
4813 op, returning the lengthened list. C<first> is the op to prepend to the
4814 list, and C<last> is the list-type op. C<optype> specifies the intended
4815 opcode for the list. If C<last> is not already a list of the right type,
4816 it will be upgraded into one. If either C<first> or C<last> is null,
4817 the other is returned unchanged.
4823 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)