This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add declared_refs feature feature
[perl5.git] / op.c
CommitLineData
4b88f280 1#line 2 "op.c"
a0d0e21e 2/* op.c
79072805 3 *
1129b882
NC
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
79072805
LW
6 *
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.
9 *
a0d0e21e
LW
10 */
11
12/*
4ac71550
TC
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
18 *
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
79072805
LW
20 */
21
166f8a29
DM
22/* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
24 *
9b7bf845
DM
25 * Note that during the build of miniperl, a temporary copy of this file
26 * is made, called opmini.c.
27 *
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.
166f8a29
DM
38 *
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):
43 *
44 * newBINOP(OP_ADD, flags,
45 * newSVREF($a),
46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
47 * )
48 *
9b7bf845
DM
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
53 * parse tree left.
54 *
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:
58 *
59 * [+]
60 * |
61 * [*]------[/]
62 * | |
63 * A---B C---D
64 *
65 * with the intended execution order being:
66 *
67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
68 *
69 * At this point all the nodes' op_next pointers will have been set,
70 * except that:
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
83 * initially have had:
84 * [*] => A; A => B; B => [*]
85 * and
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.
90 *
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.
96 *
97 * In summary: given a subtree, its top-level node's op_next will either
98 * be:
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
166f8a29 102 */
ccfc67b7 103
61b743bb 104/*
9b7bf845
DM
105
106Here's an older description from Larry.
107
61b743bb
DM
108Perl's compiler is essentially a 3-pass compiler with interleaved phases:
109
110 A bottom-up pass
111 A top-down pass
112 An execution-order pass
113
114The bottom-up pass is represented by all the "newOP" routines and
115the ck_ routines. The bottom-upness is actually driven by yacc.
116So at the point that a ck_ routine fires, we have no idea what the
117context is, either upward in the syntax tree, or either forward or
118backward in the execution order. (The bottom-up parser builds that
119part of the execution order it knows about, but if you follow the "next"
120links around, you'll find it's actually a closed loop through the
ef9da979 121top level node.)
61b743bb
DM
122
123Whenever the bottom-up parser gets to a node that supplies context to
124its components, it invokes that portion of the top-down pass that applies
125to that part of the subtree (and marks the top node as processed, so
126if a node further up supplies context, it doesn't have to take the
127plunge again). As a particular subcase of this, as the new node is
128built, it takes all the closed execution loops of its subcomponents
129and links them into a new closed loop for the higher level node. But
130it's still not the real execution order.
131
132The actual execution order is not known till we get a grammar reduction
133to 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
135into peep() to do that code's portion of the 3rd pass. It has to be
136recursive, but it's recursive on basic blocks, not on tree nodes.
137*/
138
06e0342d 139/* To implement user lexical pragmas, there needs to be a way at run time to
b3ca2e83
NC
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
06e0342d 147 leaf, ignoring any key you've already seen (placeholder or not), storing
b3ca2e83
NC
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.
151
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.
c28fe1ec 155 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
34795b44 156 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
c28fe1ec
NC
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.
b3ca2e83
NC
159*/
160
79072805 161#include "EXTERN.h"
864dbfa3 162#define PERL_IN_OP_C
79072805 163#include "perl.h"
77ca0c92 164#include "keywords.h"
2846acbf 165#include "feature.h"
74529a43 166#include "regcomp.h"
79072805 167
16c91539 168#define CALL_PEEP(o) PL_peepp(aTHX_ o)
1a0a2ba9 169#define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
16c91539 170#define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
a2efc822 171
5068f264 172static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
637494ac 173
aa9d1253
TC
174/* Used to avoid recursion through the op tree in scalarvoid() and
175 op_free()
176*/
177
178#define DEFERRED_OP_STEP 100
179#define DEFER_OP(o) \
180 STMT_START { \
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 *); \
185 } \
186 defer_stack[++defer_ix] = o; \
187 } STMT_END
188
189#define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
190
72621f84
DM
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.
194 */
195
196STATIC void
dc3bf405 197S_prune_chain_head(OP** op_p)
72621f84
DM
198{
199 while (*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)
204 )
205 *op_p = (*op_p)->op_next;
206}
207
208
8be227ab
FC
209/* See the explanatory comments above struct opslab in op.h. */
210
7aef8e5b 211#ifdef PERL_DEBUG_READONLY_OPS
3107b51f
FC
212# define PERL_SLAB_SIZE 128
213# define PERL_MAX_SLAB_SIZE 4096
214# include <sys/mman.h>
7aef8e5b 215#endif
3107b51f 216
7aef8e5b 217#ifndef PERL_SLAB_SIZE
8be227ab 218# define PERL_SLAB_SIZE 64
7aef8e5b
FC
219#endif
220#ifndef PERL_MAX_SLAB_SIZE
e6cee8c0 221# define PERL_MAX_SLAB_SIZE 2048
7aef8e5b 222#endif
8be227ab
FC
223
224/* rounds up to nearest pointer */
7aef8e5b
FC
225#define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
226#define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
8be227ab
FC
227
228static OPSLAB *
229S_new_slab(pTHX_ size_t sz)
230{
7aef8e5b 231#ifdef PERL_DEBUG_READONLY_OPS
3107b51f
FC
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");
239 abort();
240 }
241 slab->opslab_size = (U16)sz;
7aef8e5b 242#else
8be227ab 243 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
7aef8e5b 244#endif
dc3bf405
BF
245#ifndef WIN32
246 /* The context is unused in non-Windows */
247 PERL_UNUSED_CONTEXT;
248#endif
8be227ab
FC
249 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
250 return slab;
251}
252
e7372881
FC
253/* requires double parens and aTHX_ */
254#define DEBUG_S_warn(args) \
255 DEBUG_S( \
256 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
257 )
258
8be227ab
FC
259void *
260Perl_Slab_Alloc(pTHX_ size_t sz)
261{
8be227ab
FC
262 OPSLAB *slab;
263 OPSLAB *slab2;
264 OPSLOT *slot;
265 OP *o;
5cb52f30 266 size_t opsz, space;
8be227ab 267
2073970f
NC
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. */
8be227ab
FC
274 if (!PL_compcv || CvROOT(PL_compcv)
275 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
29e61fd9
DM
276 {
277 o = (OP*)PerlMemShared_calloc(1, sz);
278 goto gotit;
279 }
8be227ab 280
2073970f
NC
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
286 details. */
287 if (!CvSTART(PL_compcv)) {
8be227ab
FC
288 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 */
292 }
293 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
294
5cb52f30
FC
295 opsz = SIZE_TO_PSIZE(sz);
296 sz = opsz + OPSLOT_HEADER_P;
8be227ab 297
2073970f
NC
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. */
8be227ab
FC
301 if (slab->opslab_freed) {
302 OP **too = &slab->opslab_freed;
303 o = *too;
eb212a1c 304 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
8be227ab 305 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
e7372881 306 DEBUG_S_warn((aTHX_ "Alas! too small"));
8be227ab 307 o = *(too = &o->op_next);
eb212a1c 308 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
8be227ab
FC
309 }
310 if (o) {
311 *too = o->op_next;
5cb52f30 312 Zero(o, opsz, I32 *);
8be227ab 313 o->op_slabbed = 1;
29e61fd9 314 goto gotit;
8be227ab
FC
315 }
316 }
317
7aef8e5b 318#define INIT_OPSLOT \
8be227ab
FC
319 slot->opslot_slab = slab; \
320 slot->opslot_next = slab2->opslab_first; \
321 slab2->opslab_first = slot; \
322 o = &slot->opslot_op; \
323 o->op_slabbed = 1
324
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. */
329
8be227ab
FC
330 /* If we can fit a BASEOP, add it to the free chain, so as not
331 to waste it. */
332 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
333 slot = &slab2->opslab_slots;
334 INIT_OPSLOT;
335 o->op_type = OP_FREED;
336 o->op_next = slab->opslab_freed;
337 slab->opslab_freed = o;
338 }
339
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;
af7751f6
FC
343 slab2 = S_new_slab(aTHX_
344 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
e6cee8c0 345 ? PERL_MAX_SLAB_SIZE
af7751f6 346 : (DIFF(slab2, slot)+1)*2);
9963ffa2
FC
347 slab2->opslab_next = slab->opslab_next;
348 slab->opslab_next = slab2;
8be227ab
FC
349 }
350 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
351
352 /* Create a new op slot */
353 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
354 assert(slot >= &slab2->opslab_slots);
51c777ca
FC
355 if (DIFF(&slab2->opslab_slots, slot)
356 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
357 slot = &slab2->opslab_slots;
8be227ab 358 INIT_OPSLOT;
eb212a1c 359 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
29e61fd9
DM
360
361 gotit:
93059c1a 362#ifdef PERL_OP_PARENT
87b5a8b9
DM
363 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
364 assert(!o->op_moresib);
86cd3a13 365 assert(!o->op_sibparent);
93059c1a 366#endif
29e61fd9 367
8be227ab
FC
368 return (void *)o;
369}
370
7aef8e5b 371#undef INIT_OPSLOT
8be227ab 372
7aef8e5b 373#ifdef PERL_DEBUG_READONLY_OPS
3107b51f
FC
374void
375Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
376{
377 PERL_ARGS_ASSERT_SLAB_TO_RO;
378
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);
387 }
388}
389
7bbbc3c0
NC
390void
391Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
3107b51f 392{
3107b51f
FC
393 OPSLAB *slab2;
394
395 PERL_ARGS_ASSERT_SLAB_TO_RW;
396
3107b51f
FC
397 if (!slab->opslab_readonly) return;
398 slab2 = slab;
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);
406 }
407 }
408 slab->opslab_readonly = 0;
409}
410
411#else
9e4d7a13 412# define Slab_to_rw(op) NOOP
3107b51f
FC
413#endif
414
8be227ab
FC
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
417 commit 083fcd5. */
7aef8e5b 418#ifdef NETWARE
8be227ab 419# define PerlMemShared PerlMem
7aef8e5b 420#endif
8be227ab
FC
421
422void
423Perl_Slab_Free(pTHX_ void *op)
424{
425 OP * const o = (OP *)op;
426 OPSLAB *slab;
427
428 PERL_ARGS_ASSERT_SLAB_FREE;
429
430 if (!o->op_slabbed) {
90840c5d
RU
431 if (!o->op_static)
432 PerlMemShared_free(op);
8be227ab
FC
433 return;
434 }
435
436 slab = OpSLAB(o);
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;
eb212a1c 442 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
8be227ab
FC
443 OpslabREFCNT_dec_padok(slab);
444}
445
446void
447Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
448{
8be227ab
FC
449 const bool havepad = !!PL_comppad;
450 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
451 if (havepad) {
452 ENTER;
453 PAD_SAVE_SETNULLPAD();
454 }
455 opslab_free(slab);
456 if (havepad) LEAVE;
457}
458
459void
460Perl_opslab_free(pTHX_ OPSLAB *slab)
461{
462 OPSLAB *slab2;
463 PERL_ARGS_ASSERT_OPSLAB_FREE;
81611534 464 PERL_UNUSED_CONTEXT;
eb212a1c 465 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
8be227ab 466 assert(slab->opslab_refcnt == 1);
3dc78631 467 do {
8be227ab 468 slab2 = slab->opslab_next;
7aef8e5b 469#ifdef DEBUGGING
8be227ab 470 slab->opslab_refcnt = ~(size_t)0;
7aef8e5b
FC
471#endif
472#ifdef PERL_DEBUG_READONLY_OPS
3107b51f 473 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
eb212a1c 474 (void*)slab));
3107b51f
FC
475 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
476 perror("munmap failed");
477 abort();
478 }
7aef8e5b 479#else
8be227ab 480 PerlMemShared_free(slab);
7aef8e5b 481#endif
3dc78631
DM
482 slab = slab2;
483 } while (slab);
8be227ab
FC
484}
485
486void
487Perl_opslab_force_free(pTHX_ OPSLAB *slab)
488{
489 OPSLAB *slab2;
490 OPSLOT *slot;
7aef8e5b 491#ifdef DEBUGGING
8be227ab 492 size_t savestack_count = 0;
7aef8e5b 493#endif
8be227ab
FC
494 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
495 slab2 = slab;
496 do {
497 for (slot = slab2->opslab_first;
498 slot->opslot_next;
499 slot = slot->opslot_next) {
500 if (slot->opslot_op.op_type != OP_FREED
501 && !(slot->opslot_op.op_savefree
7aef8e5b 502#ifdef DEBUGGING
8be227ab 503 && ++savestack_count
7aef8e5b 504#endif
8be227ab
FC
505 )
506 ) {
507 assert(slot->opslot_op.op_slabbed);
8be227ab 508 op_free(&slot->opslot_op);
3bf28c7e 509 if (slab->opslab_refcnt == 1) goto free;
8be227ab
FC
510 }
511 }
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 */
7aef8e5b 515#ifdef DEBUGGING
8be227ab 516 assert(savestack_count == slab->opslab_refcnt-1);
7aef8e5b 517#endif
ee5ee853
FC
518 /* Remove the CV’s reference count. */
519 slab->opslab_refcnt--;
8be227ab
FC
520 return;
521 }
522 free:
523 opslab_free(slab);
524}
525
3107b51f
FC
526#ifdef PERL_DEBUG_READONLY_OPS
527OP *
528Perl_op_refcnt_inc(pTHX_ OP *o)
529{
530 if(o) {
372eab01
NC
531 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
532 if (slab && slab->opslab_readonly) {
83519873 533 Slab_to_rw(slab);
372eab01
NC
534 ++o->op_targ;
535 Slab_to_ro(slab);
536 } else {
537 ++o->op_targ;
538 }
3107b51f
FC
539 }
540 return o;
541
542}
543
544PADOFFSET
545Perl_op_refcnt_dec(pTHX_ OP *o)
546{
372eab01
NC
547 PADOFFSET result;
548 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
549
3107b51f 550 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
372eab01
NC
551
552 if (slab && slab->opslab_readonly) {
83519873 553 Slab_to_rw(slab);
372eab01
NC
554 result = --o->op_targ;
555 Slab_to_ro(slab);
556 } else {
557 result = --o->op_targ;
558 }
559 return result;
3107b51f
FC
560}
561#endif
e50aee73 562/*
ce6f1cbc 563 * In the following definition, the ", (OP*)0" is just to make the compiler
a5f75d66 564 * think the expression is of the right type: croak actually does a Siglongjmp.
e50aee73 565 */
11343788 566#define CHECKOP(type,o) \
ce6f1cbc 567 ((PL_op_mask && PL_op_mask[type]) \
5dc0d613 568 ? ( op_free((OP*)o), \
cb77fdf0 569 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
ce6f1cbc 570 (OP*)0 ) \
16c91539 571 : PL_check[type](aTHX_ (OP*)o))
e50aee73 572
e6438c1a 573#define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
c53d7c7d 574
b9a07097 575#define OpTYPE_set(o,type) \
cba5a3b0
DG
576 STMT_START { \
577 o->op_type = (OPCODE)type; \
578 o->op_ppaddr = PL_ppaddr[type]; \
579 } STMT_END
580
76e3520e 581STATIC OP *
cea2e8a9 582S_no_fh_allowed(pTHX_ OP *o)
79072805 583{
7918f24d
NC
584 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
585
cea2e8a9 586 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
53e06cf0 587 OP_DESC(o)));
11343788 588 return o;
79072805
LW
589}
590
76e3520e 591STATIC OP *
ce16c625
BF
592S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
593{
594 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
595 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
596 return o;
597}
598
599STATIC OP *
600S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
601{
602 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
7918f24d 603
ce16c625 604 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
11343788 605 return o;
79072805
LW
606}
607
76e3520e 608STATIC void
ed9feedd 609S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
8990e307 610{
ce16c625
BF
611 PERL_ARGS_ASSERT_BAD_TYPE_PV;
612
613 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
ed9feedd 614 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
ce16c625 615}
7918f24d 616
ed9feedd
DD
617/* remove flags var, its unused in all callers, move to to right end since gv
618 and kid are always the same */
ce16c625 619STATIC void
ed9feedd 620S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
ce16c625 621{
ecf05a58 622 SV * const namesv = cv_name((CV *)gv, NULL, 0);
7b3b0904 623 PERL_ARGS_ASSERT_BAD_TYPE_GV;
ce16c625
BF
624
625 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
ed9feedd 626 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
8990e307
LW
627}
628
7a52d87a 629STATIC void
eb796c7f 630S_no_bareword_allowed(pTHX_ OP *o)
7a52d87a 631{
7918f24d
NC
632 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
633
5a844595 634 qerror(Perl_mess(aTHX_
35c1215d 635 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
be2597df 636 SVfARG(cSVOPo_sv)));
eb796c7f 637 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
7a52d87a
GS
638}
639
79072805
LW
640/* "register" allocation */
641
642PADOFFSET
d6447115 643Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
93a17b20 644{
a0d0e21e 645 PADOFFSET off;
12bd6ede 646 const bool is_our = (PL_parser->in_my == KEY_our);
a0d0e21e 647
7918f24d
NC
648 PERL_ARGS_ASSERT_ALLOCMY;
649
48d0d1be 650 if (flags & ~SVf_UTF8)
d6447115
NC
651 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
652 (UV)flags);
653
59f00321 654 /* complain about "my $<special_var>" etc etc */
d6447115 655 if (len &&
3edf23ff 656 !(is_our ||
155aba94 657 isALPHA(name[1]) ||
b14845b4 658 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
7fba2966 659 (name[1] == '_' && len > 2)))
834a4ddd 660 {
b14845b4 661 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
f6a1dc93 662 && isASCII(name[1])
b14845b4 663 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
d6447115
NC
664 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
665 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
aab6a793 666 PL_parser->in_my == KEY_state ? "state" : "my"));
d1544d85 667 } else {
ce16c625
BF
668 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
669 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
46fc3d4c 670 }
a0d0e21e 671 }
748a9306 672
dd2155a4 673 /* allocate a spare slot and store the name in that slot */
93a17b20 674
cc76b5cc 675 off = pad_add_name_pvn(name, len,
48d0d1be 676 (is_our ? padadd_OUR :
2502ffdf 677 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
12bd6ede 678 PL_parser->in_my_stash,
3edf23ff 679 (is_our
133706a6 680 /* $_ is always in main::, even with our */
ef00320b
FC
681 ? (PL_curstash && !memEQs(name,len,"$_")
682 ? PL_curstash
683 : PL_defstash)
5c284bb0 684 : NULL
cca43f78 685 )
dd2155a4 686 );
a74073ad
DM
687 /* anon sub prototypes contains state vars should always be cloned,
688 * otherwise the state var would be shared between anon subs */
689
690 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
691 CvCLONE_on(PL_compcv);
692
dd2155a4 693 return off;
79072805
LW
694}
695
c0b8aebd 696/*
dcccc8ff
KW
697=head1 Optree Manipulation Functions
698
c0b8aebd
FC
699=for apidoc alloccopstash
700
701Available only under threaded builds, this function allocates an entry in
702C<PL_stashpad> for the stash passed to it.
703
704=cut
705*/
706
d4d03940
FC
707#ifdef USE_ITHREADS
708PADOFFSET
1dc74fdb 709Perl_alloccopstash(pTHX_ HV *hv)
d4d03940
FC
710{
711 PADOFFSET off = 0, o = 1;
712 bool found_slot = FALSE;
713
1dc74fdb
FC
714 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
715
716 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
d4d03940 717
1dc74fdb
FC
718 for (; o < PL_stashpadmax; ++o) {
719 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
720 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
d4d03940
FC
721 found_slot = TRUE, off = o;
722 }
723 if (!found_slot) {
1dc74fdb
FC
724 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
725 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
726 off = PL_stashpadmax;
727 PL_stashpadmax += 10;
d4d03940
FC
728 }
729
1dc74fdb 730 PL_stashpad[PL_stashpadix = off] = hv;
d4d03940
FC
731 return off;
732}
733#endif
734
d2c837a0
DM
735/* free the body of an op without examining its contents.
736 * Always use this rather than FreeOp directly */
737
4136a0f7 738static void
d2c837a0
DM
739S_op_destroy(pTHX_ OP *o)
740{
d2c837a0
DM
741 FreeOp(o);
742}
743
79072805
LW
744/* Destructor */
745
6e53b6ca
DD
746/*
747=for apidoc Am|void|op_free|OP *o
748
cc41839b
FC
749Free an op. Only use this when an op is no longer linked to from any
750optree.
6e53b6ca
DD
751
752=cut
753*/
754
79072805 755void
864dbfa3 756Perl_op_free(pTHX_ OP *o)
79072805 757{
27da23d5 758 dVAR;
acb36ea4 759 OPCODE type;
0997db6f
TC
760 SSize_t defer_ix = -1;
761 SSize_t defer_stack_alloc = 0;
762 OP **defer_stack = NULL;
79072805 763
0997db6f 764 do {
79072805 765
0997db6f
TC
766 /* Though ops may be freed twice, freeing the op after its slab is a
767 big no-no. */
768 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
769 /* During the forced freeing of ops after compilation failure, kidops
770 may be freed before their parents. */
771 if (!o || o->op_type == OP_FREED)
772 continue;
d0c8136d 773
0997db6f 774 type = o->op_type;
d0c8136d 775
0997db6f 776 /* an op should only ever acquire op_private flags that we know about.
09681a13
DM
777 * If this fails, you may need to fix something in regen/op_private.
778 * Don't bother testing if:
779 * * the op_ppaddr doesn't match the op; someone may have
780 * overridden the op and be doing strange things with it;
781 * * we've errored, as op flags are often left in an
782 * inconsistent state then. Note that an error when
783 * compiling the main program leaves PL_parser NULL, so
ad53d4d4 784 * we can't spot faults in the main code, only
09681a13
DM
785 * evaled/required code */
786#ifdef DEBUGGING
787 if ( o->op_ppaddr == PL_ppaddr[o->op_type]
788 && PL_parser
789 && !PL_parser->error_count)
790 {
0997db6f
TC
791 assert(!(o->op_private & ~PL_op_private_valid[type]));
792 }
09681a13 793#endif
7934575e 794
0997db6f
TC
795 if (o->op_private & OPpREFCOUNTED) {
796 switch (type) {
797 case OP_LEAVESUB:
798 case OP_LEAVESUBLV:
799 case OP_LEAVEEVAL:
800 case OP_LEAVE:
801 case OP_SCOPE:
802 case OP_LEAVEWRITE:
803 {
804 PADOFFSET refcnt;
805 OP_REFCNT_LOCK;
806 refcnt = OpREFCNT_dec(o);
807 OP_REFCNT_UNLOCK;
808 if (refcnt) {
809 /* Need to find and remove any pattern match ops from the list
810 we maintain for reset(). */
811 find_and_forget_pmops(o);
812 continue;
813 }
814 }
815 break;
816 default:
817 break;
818 }
819 }
f37b8c3f 820
0997db6f
TC
821 /* Call the op_free hook if it has been set. Do it now so that it's called
822 * at the right time for refcounted ops, but still before all of the kids
823 * are freed. */
824 CALL_OPFREEHOOK(o);
825
826 if (o->op_flags & OPf_KIDS) {
827 OP *kid, *nextkid;
828 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
e6dae479 829 nextkid = OpSIBLING(kid); /* Get before next freeing kid */
0997db6f
TC
830 if (!kid || kid->op_type == OP_FREED)
831 /* During the forced freeing of ops after
832 compilation failure, kidops may be freed before
833 their parents. */
834 continue;
835 if (!(kid->op_flags & OPf_KIDS))
836 /* If it has no kids, just free it now */
837 op_free(kid);
838 else
aa9d1253 839 DEFER_OP(kid);
0997db6f
TC
840 }
841 }
842 if (type == OP_NULL)
843 type = (OPCODE)o->op_targ;
acb36ea4 844
0997db6f
TC
845 if (o->op_slabbed)
846 Slab_to_rw(OpSLAB(o));
fc97af9c 847
0997db6f
TC
848 /* COP* is not cleared by op_clear() so that we may track line
849 * numbers etc even after null() */
850 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
851 cop_free((COP*)o);
852 }
acb36ea4 853
0997db6f
TC
854 op_clear(o);
855 FreeOp(o);
4d494880 856#ifdef DEBUG_LEAKING_SCALARS
0997db6f
TC
857 if (PL_op == o)
858 PL_op = NULL;
4d494880 859#endif
aa9d1253 860 } while ( (o = POP_DEFERRED_OP()) );
0997db6f
TC
861
862 Safefree(defer_stack);
acb36ea4 863}
79072805 864
ab576797
DM
865/* S_op_clear_gv(): free a GV attached to an OP */
866
f9db5646 867STATIC
ab576797
DM
868#ifdef USE_ITHREADS
869void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
870#else
871void S_op_clear_gv(pTHX_ OP *o, SV**svp)
872#endif
873{
874
fedf30e1
DM
875 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
876 || o->op_type == OP_MULTIDEREF)
ab576797
DM
877#ifdef USE_ITHREADS
878 && PL_curpad
879 ? ((GV*)PAD_SVl(*ixp)) : NULL;
880#else
881 ? (GV*)(*svp) : NULL;
882#endif
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);
898
899 if (still_valid)
900 SvREFCNT_inc_simple_void(gv);
901#ifdef USE_ITHREADS
902 if (*ixp > 0) {
903 pad_swipe(*ixp, TRUE);
904 *ixp = 0;
905 }
906#else
907 SvREFCNT_dec(*svp);
908 *svp = NULL;
909#endif
910 if (still_valid) {
911 int try_downgrade = SvREFCNT(gv) == 2;
912 SvREFCNT_dec_NN(gv);
913 if (try_downgrade)
914 gv_try_downgrade(gv);
915 }
916}
917
918
93c66552
DM
919void
920Perl_op_clear(pTHX_ OP *o)
acb36ea4 921{
13137afc 922
27da23d5 923 dVAR;
7918f24d
NC
924
925 PERL_ARGS_ASSERT_OP_CLEAR;
926
11343788 927 switch (o->op_type) {
acb36ea4 928 case OP_NULL: /* Was holding old type, if any. */
c67159e1 929 /* FALLTHROUGH */
4d193d44 930 case OP_ENTERTRY:
acb36ea4 931 case OP_ENTEREVAL: /* Was holding hints. */
acb36ea4 932 o->op_targ = 0;
a0d0e21e 933 break;
a6006777 934 default:
ac4c12e7 935 if (!(o->op_flags & OPf_REF)
ef69c8fc 936 || (PL_check[o->op_type] != Perl_ck_ftst))
a6006777 937 break;
924ba076 938 /* FALLTHROUGH */
463ee0b2 939 case OP_GVSV:
79072805 940 case OP_GV:
a6006777 941 case OP_AELEMFAST:
f7461760 942#ifdef USE_ITHREADS
ab576797 943 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
350de78d 944#else
ab576797 945 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
350de78d 946#endif
79072805 947 break;
810bd8b7 948 case OP_METHOD_REDIR:
949 case OP_METHOD_REDIR_SUPER:
950#ifdef USE_ITHREADS
951 if (cMETHOPx(o)->op_rclass_targ) {
952 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1);
953 cMETHOPx(o)->op_rclass_targ = 0;
954 }
955#else
956 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv);
957 cMETHOPx(o)->op_rclass_sv = NULL;
958#endif
a1ae71d2 959 case OP_METHOD_NAMED:
7d6c333c 960 case OP_METHOD_SUPER:
b46e009d 961 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
962 cMETHOPx(o)->op_u.op_meth_sv = NULL;
963#ifdef USE_ITHREADS
964 if (o->op_targ) {
965 pad_swipe(o->op_targ, 1);
966 o->op_targ = 0;
967 }
968#endif
969 break;
79072805 970 case OP_CONST:
996c9baa 971 case OP_HINTSEVAL:
11343788 972 SvREFCNT_dec(cSVOPo->op_sv);
a0714e2c 973 cSVOPo->op_sv = NULL;
3b1c21fa
AB
974#ifdef USE_ITHREADS
975 /** Bug #15654
976 Even if op_clear does a pad_free for the target of the op,
6a077020 977 pad_free doesn't actually remove the sv that exists in the pad;
3b1c21fa
AB
978 instead it lives on. This results in that it could be reused as
979 a target later on when the pad was reallocated.
980 **/
981 if(o->op_targ) {
982 pad_swipe(o->op_targ,1);
983 o->op_targ = 0;
984 }
985#endif
79072805 986 break;
c9df4fda 987 case OP_DUMP:
748a9306
LW
988 case OP_GOTO:
989 case OP_NEXT:
990 case OP_LAST:
991 case OP_REDO:
11343788 992 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
748a9306 993 break;
924ba076 994 /* FALLTHROUGH */
a0d0e21e 995 case OP_TRANS:
bb16bae8 996 case OP_TRANSR:
acb36ea4 997 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
99a1d0d1 998 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
043e41b8
DM
999#ifdef USE_ITHREADS
1000 if (cPADOPo->op_padix > 0) {
1001 pad_swipe(cPADOPo->op_padix, TRUE);
1002 cPADOPo->op_padix = 0;
1003 }
1004#else
a0ed51b3 1005 SvREFCNT_dec(cSVOPo->op_sv);
a0714e2c 1006 cSVOPo->op_sv = NULL;
043e41b8 1007#endif
acb36ea4
GS
1008 }
1009 else {
ea71c68d 1010 PerlMemShared_free(cPVOPo->op_pv);
bd61b366 1011 cPVOPo->op_pv = NULL;
acb36ea4 1012 }
a0d0e21e
LW
1013 break;
1014 case OP_SUBST:
20e98b0f 1015 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
971a9dd3 1016 goto clear_pmop;
748a9306 1017 case OP_PUSHRE:
971a9dd3 1018#ifdef USE_ITHREADS
20e98b0f 1019 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
20e98b0f 1020 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
971a9dd3
GS
1021 }
1022#else
ad64d0ec 1023 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
971a9dd3 1024#endif
924ba076 1025 /* FALLTHROUGH */
a0d0e21e 1026 case OP_MATCH:
8782bef2 1027 case OP_QR:
7b52d656 1028 clear_pmop:
867940b8
DM
1029 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1030 op_free(cPMOPo->op_code_list);
68e2671b 1031 cPMOPo->op_code_list = NULL;
23083432 1032 forget_pmop(cPMOPo);
20e98b0f 1033 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
9cddf794
NC
1034 /* we use the same protection as the "SAFE" version of the PM_ macros
1035 * here since sv_clean_all might release some PMOPs
5f8cb046
DM
1036 * after PL_regex_padav has been cleared
1037 * and the clearing of PL_regex_padav needs to
1038 * happen before sv_clean_all
1039 */
13137afc
AB
1040#ifdef USE_ITHREADS
1041 if(PL_regex_pad) { /* We could be in destruction */
402d2eb1 1042 const IV offset = (cPMOPo)->op_pmoffset;
9cddf794 1043 ReREFCNT_dec(PM_GETRE(cPMOPo));
402d2eb1
NC
1044 PL_regex_pad[offset] = &PL_sv_undef;
1045 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1046 sizeof(offset));
13137afc 1047 }
9cddf794
NC
1048#else
1049 ReREFCNT_dec(PM_GETRE(cPMOPo));
1050 PM_SETRE(cPMOPo, NULL);
1eb1540c 1051#endif
13137afc 1052
a0d0e21e 1053 break;
fedf30e1
DM
1054
1055 case OP_MULTIDEREF:
1056 {
1057 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1058 UV actions = items->uv;
1059 bool last = 0;
1060 bool is_hash = FALSE;
1061
1062 while (!last) {
1063 switch (actions & MDEREF_ACTION_MASK) {
1064
1065 case MDEREF_reload:
1066 actions = (++items)->uv;
1067 continue;
1068
1069 case MDEREF_HV_padhv_helem:
1070 is_hash = TRUE;
1071 case MDEREF_AV_padav_aelem:
1072 pad_free((++items)->pad_offset);
1073 goto do_elem;
1074
1075 case MDEREF_HV_gvhv_helem:
1076 is_hash = TRUE;
1077 case MDEREF_AV_gvav_aelem:
1078#ifdef USE_ITHREADS
1079 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1080#else
1081 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1082#endif
1083 goto do_elem;
1084
1085 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1086 is_hash = TRUE;
1087 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1088#ifdef USE_ITHREADS
1089 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1090#else
1091 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1092#endif
1093 goto do_vivify_rv2xv_elem;
1094
1095 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1096 is_hash = TRUE;
1097 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1098 pad_free((++items)->pad_offset);
1099 goto do_vivify_rv2xv_elem;
1100
1101 case MDEREF_HV_pop_rv2hv_helem:
1102 case MDEREF_HV_vivify_rv2hv_helem:
1103 is_hash = TRUE;
1104 do_vivify_rv2xv_elem:
1105 case MDEREF_AV_pop_rv2av_aelem:
1106 case MDEREF_AV_vivify_rv2av_aelem:
1107 do_elem:
1108 switch (actions & MDEREF_INDEX_MASK) {
1109 case MDEREF_INDEX_none:
1110 last = 1;
1111 break;
1112 case MDEREF_INDEX_const:
1113 if (is_hash) {
1114#ifdef USE_ITHREADS
1115 /* see RT #15654 */
1116 pad_swipe((++items)->pad_offset, 1);
1117#else
1118 SvREFCNT_dec((++items)->sv);
1119#endif
1120 }
1121 else
1122 items++;
1123 break;
1124 case MDEREF_INDEX_padsv:
1125 pad_free((++items)->pad_offset);
1126 break;
1127 case MDEREF_INDEX_gvsv:
1128#ifdef USE_ITHREADS
1129 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1130#else
1131 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1132#endif
1133 break;
1134 }
1135
1136 if (actions & MDEREF_FLAG_last)
1137 last = 1;
1138 is_hash = FALSE;
1139
1140 break;
1141
1142 default:
1143 assert(0);
1144 last = 1;
1145 break;
1146
1147 } /* switch */
1148
1149 actions >>= MDEREF_SHIFT;
1150 } /* while */
1151
1152 /* start of malloc is at op_aux[-1], where the length is
1153 * stored */
1154 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1155 }
1156 break;
79072805
LW
1157 }
1158
743e66e6 1159 if (o->op_targ > 0) {
11343788 1160 pad_free(o->op_targ);
743e66e6
GS
1161 o->op_targ = 0;
1162 }
79072805
LW
1163}
1164
76e3520e 1165STATIC void
3eb57f73
HS
1166S_cop_free(pTHX_ COP* cop)
1167{
7918f24d
NC
1168 PERL_ARGS_ASSERT_COP_FREE;
1169
05ec9bb3 1170 CopFILE_free(cop);
0453d815 1171 if (! specialWARN(cop->cop_warnings))
72dc9ed5 1172 PerlMemShared_free(cop->cop_warnings);
20439bc7 1173 cophh_free(CopHINTHASH_get(cop));
515abc43
FC
1174 if (PL_curcop == cop)
1175 PL_curcop = NULL;
3eb57f73
HS
1176}
1177
c2b1997a 1178STATIC void
c4bd3ae5 1179S_forget_pmop(pTHX_ PMOP *const o
c4bd3ae5 1180 )
c2b1997a
NC
1181{
1182 HV * const pmstash = PmopSTASH(o);
7918f24d
NC
1183
1184 PERL_ARGS_ASSERT_FORGET_PMOP;
1185
e39a6381 1186 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
ad64d0ec 1187 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
c2b1997a
NC
1188 if (mg) {
1189 PMOP **const array = (PMOP**) mg->mg_ptr;
1190 U32 count = mg->mg_len / sizeof(PMOP**);
1191 U32 i = count;
1192
1193 while (i--) {
1194 if (array[i] == o) {
1195 /* Found it. Move the entry at the end to overwrite it. */
1196 array[i] = array[--count];
1197 mg->mg_len = count * sizeof(PMOP**);
1198 /* Could realloc smaller at this point always, but probably
1199 not worth it. Probably worth free()ing if we're the
1200 last. */
1201 if(!count) {
1202 Safefree(mg->mg_ptr);
1203 mg->mg_ptr = NULL;
1204 }
1205 break;
1206 }
1207 }
1208 }
1209 }
1cdf7faf
NC
1210 if (PL_curpm == o)
1211 PL_curpm = NULL;
c2b1997a
NC
1212}
1213
bfd0ff22
NC
1214STATIC void
1215S_find_and_forget_pmops(pTHX_ OP *o)
1216{
7918f24d
NC
1217 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1218
bfd0ff22
NC
1219 if (o->op_flags & OPf_KIDS) {
1220 OP *kid = cUNOPo->op_first;
1221 while (kid) {
1222 switch (kid->op_type) {
1223 case OP_SUBST:
1224 case OP_PUSHRE:
1225 case OP_MATCH:
1226 case OP_QR:
23083432 1227 forget_pmop((PMOP*)kid);
bfd0ff22
NC
1228 }
1229 find_and_forget_pmops(kid);
e6dae479 1230 kid = OpSIBLING(kid);
bfd0ff22
NC
1231 }
1232 }
1233}
1234
6e53b6ca
DD
1235/*
1236=for apidoc Am|void|op_null|OP *o
1237
1238Neutralizes an op when it is no longer needed, but is still linked to from
1239other ops.
1240
1241=cut
1242*/
1243
93c66552
DM
1244void
1245Perl_op_null(pTHX_ OP *o)
8990e307 1246{
27da23d5 1247 dVAR;
7918f24d
NC
1248
1249 PERL_ARGS_ASSERT_OP_NULL;
1250
acb36ea4
GS
1251 if (o->op_type == OP_NULL)
1252 return;
b5bbe64a 1253 op_clear(o);
11343788 1254 o->op_targ = o->op_type;
b9a07097 1255 OpTYPE_set(o, OP_NULL);
8990e307
LW
1256}
1257
4026c95a
SH
1258void
1259Perl_op_refcnt_lock(pTHX)
e1fc825d 1260 PERL_TSA_ACQUIRE(PL_op_mutex)
4026c95a 1261{
20b7effb 1262#ifdef USE_ITHREADS
27da23d5 1263 dVAR;
20b7effb 1264#endif
96a5add6 1265 PERL_UNUSED_CONTEXT;
4026c95a
SH
1266 OP_REFCNT_LOCK;
1267}
1268
1269void
1270Perl_op_refcnt_unlock(pTHX)
e1fc825d 1271 PERL_TSA_RELEASE(PL_op_mutex)
4026c95a 1272{
20b7effb 1273#ifdef USE_ITHREADS
27da23d5 1274 dVAR;
20b7effb 1275#endif
96a5add6 1276 PERL_UNUSED_CONTEXT;
4026c95a
SH
1277 OP_REFCNT_UNLOCK;
1278}
1279
3253bf85
DM
1280
1281/*
1282=for apidoc op_sibling_splice
1283
1284A general function for editing the structure of an existing chain of
796b6530 1285op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
3253bf85
DM
1286you to delete zero or more sequential nodes, replacing them with zero or
1287more different nodes. Performs the necessary op_first/op_last
29e61fd9 1288housekeeping on the parent node and op_sibling manipulation on the
7e234f81 1289children. The last deleted node will be marked as as the last node by
87b5a8b9 1290updating the op_sibling/op_sibparent or op_moresib field as appropriate.
3253bf85
DM
1291
1292Note that op_next is not manipulated, and nodes are not freed; that is the
7e234f81 1293responsibility of the caller. It also won't create a new list op for an
8ae26bff 1294empty list etc; use higher-level functions like op_append_elem() for that.
3253bf85 1295
796b6530 1296C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
3269ea41 1297the splicing doesn't affect the first or last op in the chain.
3253bf85 1298
796b6530 1299C<start> is the node preceding the first node to be spliced. Node(s)
7e234f81 1300following it will be deleted, and ops will be inserted after it. If it is
796b6530 1301C<NULL>, the first node onwards is deleted, and nodes are inserted at the
3253bf85
DM
1302beginning.
1303
796b6530 1304C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
3253bf85
DM
1305If -1 or greater than or equal to the number of remaining kids, all
1306remaining kids are deleted.
1307
796b6530
KW
1308C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1309If C<NULL>, no nodes are inserted.
3253bf85 1310
796b6530 1311The head of the chain of deleted ops is returned, or C<NULL> if no ops were
3253bf85
DM
1312deleted.
1313
1314For example:
1315
1316 action before after returns
1317 ------ ----- ----- -------
1318
1319 P P
8ae26bff
DM
1320 splice(P, A, 2, X-Y-Z) | | B-C
1321 A-B-C-D A-X-Y-Z-D
3253bf85
DM
1322
1323 P P
1324 splice(P, NULL, 1, X-Y) | | A
1325 A-B-C-D X-Y-B-C-D
1326
1327 P P
8ae26bff
DM
1328 splice(P, NULL, 3, NULL) | | A-B-C
1329 A-B-C-D D
3253bf85
DM
1330
1331 P P
1332 splice(P, B, 0, X-Y) | | NULL
1333 A-B-C-D A-B-X-Y-C-D
1334
5e24af7d
DM
1335
1336For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
fbe13c60 1337see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
5e24af7d 1338
3253bf85
DM
1339=cut
1340*/
1341
1342OP *
8ae26bff 1343Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
3253bf85 1344{
3269ea41 1345 OP *first;
3253bf85
DM
1346 OP *rest;
1347 OP *last_del = NULL;
1348 OP *last_ins = NULL;
1349
3269ea41
DM
1350 if (start)
1351 first = OpSIBLING(start);
1352 else if (!parent)
1353 goto no_parent;
1354 else
1355 first = cLISTOPx(parent)->op_first;
3253bf85
DM
1356
1357 assert(del_count >= -1);
1358
1359 if (del_count && first) {
1360 last_del = first;
e6dae479
FC
1361 while (--del_count && OpHAS_SIBLING(last_del))
1362 last_del = OpSIBLING(last_del);
1363 rest = OpSIBLING(last_del);
5e24af7d 1364 OpLASTSIB_set(last_del, NULL);
3253bf85
DM
1365 }
1366 else
1367 rest = first;
1368
1369 if (insert) {
1370 last_ins = insert;
e6dae479
FC
1371 while (OpHAS_SIBLING(last_ins))
1372 last_ins = OpSIBLING(last_ins);
5e24af7d 1373 OpMAYBESIB_set(last_ins, rest, NULL);
3253bf85
DM
1374 }
1375 else
1376 insert = rest;
1377
29e61fd9 1378 if (start) {
5e24af7d 1379 OpMAYBESIB_set(start, insert, NULL);
29e61fd9 1380 }
b3e29a8d 1381 else {
3269ea41
DM
1382 if (!parent)
1383 goto no_parent;
3253bf85 1384 cLISTOPx(parent)->op_first = insert;
b3e29a8d
DM
1385 if (insert)
1386 parent->op_flags |= OPf_KIDS;
1387 else
1388 parent->op_flags &= ~OPf_KIDS;
1389 }
3253bf85
DM
1390
1391 if (!rest) {
29e61fd9 1392 /* update op_last etc */
3269ea41 1393 U32 type;
29e61fd9 1394 OP *lastop;
3253bf85 1395
3269ea41
DM
1396 if (!parent)
1397 goto no_parent;
1398
05039abd
DM
1399 /* ought to use OP_CLASS(parent) here, but that can't handle
1400 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1401 * either */
3269ea41 1402 type = parent->op_type;
05039abd
DM
1403 if (type == OP_CUSTOM) {
1404 dTHX;
1405 type = XopENTRYCUSTOM(parent, xop_class);
1406 }
1407 else {
1408 if (type == OP_NULL)
1409 type = parent->op_targ;
1410 type = PL_opargs[type] & OA_CLASS_MASK;
1411 }
3253bf85 1412
29e61fd9 1413 lastop = last_ins ? last_ins : start ? start : NULL;
3253bf85
DM
1414 if ( type == OA_BINOP
1415 || type == OA_LISTOP
1416 || type == OA_PMOP
1417 || type == OA_LOOP
1418 )
29e61fd9
DM
1419 cLISTOPx(parent)->op_last = lastop;
1420
5e24af7d
DM
1421 if (lastop)
1422 OpLASTSIB_set(lastop, parent);
3253bf85
DM
1423 }
1424 return last_del ? first : NULL;
3269ea41
DM
1425
1426 no_parent:
1427 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
3253bf85
DM
1428}
1429
3269ea41 1430
1fafe688
DM
1431#ifdef PERL_OP_PARENT
1432
29e61fd9
DM
1433/*
1434=for apidoc op_parent
1435
796b6530 1436Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1fafe688 1437This function is only available on perls built with C<-DPERL_OP_PARENT>.
29e61fd9
DM
1438
1439=cut
1440*/
1441
1442OP *
8ae26bff 1443Perl_op_parent(OP *o)
29e61fd9
DM
1444{
1445 PERL_ARGS_ASSERT_OP_PARENT;
e6dae479
FC
1446 while (OpHAS_SIBLING(o))
1447 o = OpSIBLING(o);
86cd3a13 1448 return o->op_sibparent;
29e61fd9
DM
1449}
1450
1fafe688
DM
1451#endif
1452
3253bf85
DM
1453
1454/* replace the sibling following start with a new UNOP, which becomes
1455 * the parent of the original sibling; e.g.
1456 *
1457 * op_sibling_newUNOP(P, A, unop-args...)
1458 *
1459 * P P
1460 * | becomes |
1461 * A-B-C A-U-C
1462 * |
1463 * B
1464 *
1465 * where U is the new UNOP.
1466 *
1467 * parent and start args are the same as for op_sibling_splice();
1468 * type and flags args are as newUNOP().
1469 *
1470 * Returns the new UNOP.
1471 */
1472
f9db5646 1473STATIC OP *
3253bf85
DM
1474S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1475{
1476 OP *kid, *newop;
1477
1478 kid = op_sibling_splice(parent, start, 1, NULL);
1479 newop = newUNOP(type, flags, kid);
1480 op_sibling_splice(parent, start, 0, newop);
1481 return newop;
1482}
1483
1484
1485/* lowest-level newLOGOP-style function - just allocates and populates
1486 * the struct. Higher-level stuff should be done by S_new_logop() /
1487 * newLOGOP(). This function exists mainly to avoid op_first assignment
1488 * being spread throughout this file.
1489 */
1490
f9db5646 1491STATIC LOGOP *
3253bf85
DM
1492S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1493{
1e8db68a 1494 dVAR;
3253bf85 1495 LOGOP *logop;
29e61fd9 1496 OP *kid = first;
3253bf85 1497 NewOp(1101, logop, 1, LOGOP);
b9a07097 1498 OpTYPE_set(logop, type);
3253bf85
DM
1499 logop->op_first = first;
1500 logop->op_other = other;
1501 logop->op_flags = OPf_KIDS;
e6dae479
FC
1502 while (kid && OpHAS_SIBLING(kid))
1503 kid = OpSIBLING(kid);
5e24af7d
DM
1504 if (kid)
1505 OpLASTSIB_set(kid, (OP*)logop);
3253bf85
DM
1506 return logop;
1507}
1508
1509
79072805
LW
1510/* Contextualizers */
1511
d9088386
Z
1512/*
1513=for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1514
1515Applies a syntactic context to an op tree representing an expression.
2d7f6611 1516C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>,
d9088386
Z
1517or C<G_VOID> to specify the context to apply. The modified op tree
1518is returned.
1519
1520=cut
1521*/
1522
1523OP *
1524Perl_op_contextualize(pTHX_ OP *o, I32 context)
1525{
1526 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1527 switch (context) {
1528 case G_SCALAR: return scalar(o);
1529 case G_ARRAY: return list(o);
1530 case G_VOID: return scalarvoid(o);
1531 default:
5637ef5b
NC
1532 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1533 (long) context);
d9088386
Z
1534 }
1535}
1536
5983a79d 1537/*
79072805 1538
5983a79d 1539=for apidoc Am|OP*|op_linklist|OP *o
72d33970 1540This function is the implementation of the L</LINKLIST> macro. It should
5983a79d
BM
1541not be called directly.
1542
1543=cut
1544*/
1545
1546OP *
1547Perl_op_linklist(pTHX_ OP *o)
79072805 1548{
3edf23ff 1549 OP *first;
79072805 1550
5983a79d 1551 PERL_ARGS_ASSERT_OP_LINKLIST;
7918f24d 1552
11343788
MB
1553 if (o->op_next)
1554 return o->op_next;
79072805
LW
1555
1556 /* establish postfix order */
3edf23ff
AL
1557 first = cUNOPo->op_first;
1558 if (first) {
eb578fdb 1559 OP *kid;
3edf23ff
AL
1560 o->op_next = LINKLIST(first);
1561 kid = first;
1562 for (;;) {
e6dae479 1563 OP *sibl = OpSIBLING(kid);
29e61fd9
DM
1564 if (sibl) {
1565 kid->op_next = LINKLIST(sibl);
1566 kid = sibl;
3edf23ff 1567 } else {
11343788 1568 kid->op_next = o;
3edf23ff
AL
1569 break;
1570 }
79072805
LW
1571 }
1572 }
1573 else
11343788 1574 o->op_next = o;
79072805 1575
11343788 1576 return o->op_next;
79072805
LW
1577}
1578
1f676739 1579static OP *
2dd5337b 1580S_scalarkids(pTHX_ OP *o)
79072805 1581{
11343788 1582 if (o && o->op_flags & OPf_KIDS) {
bfed75c6 1583 OP *kid;
e6dae479 1584 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
79072805
LW
1585 scalar(kid);
1586 }
11343788 1587 return o;
79072805
LW
1588}
1589
76e3520e 1590STATIC OP *
cea2e8a9 1591S_scalarboolean(pTHX_ OP *o)
8990e307 1592{
7918f24d
NC
1593 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1594
0a44e30b
DC
1595 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1596 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1597 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1598 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1599 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
d008e5eb 1600 if (ckWARN(WARN_SYNTAX)) {
6867be6d 1601 const line_t oldline = CopLINE(PL_curcop);
a0d0e21e 1602
2b7cddde
NC
1603 if (PL_parser && PL_parser->copline != NOLINE) {
1604 /* This ensures that warnings are reported at the first line
1605 of the conditional, not the last. */
53a7735b 1606 CopLINE_set(PL_curcop, PL_parser->copline);
2b7cddde 1607 }
9014280d 1608 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
57843af0 1609 CopLINE_set(PL_curcop, oldline);
d008e5eb 1610 }
a0d0e21e 1611 }
11343788 1612 return scalar(o);
8990e307
LW
1613}
1614
0920b7fa 1615static SV *
637494ac 1616S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
0920b7fa
FC
1617{
1618 assert(o);
1619 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1620 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1621 {
1622 const char funny = o->op_type == OP_PADAV
1623 || o->op_type == OP_RV2AV ? '@' : '%';
1624 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1625 GV *gv;
1626 if (cUNOPo->op_first->op_type != OP_GV
1627 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1628 return NULL;
637494ac 1629 return varname(gv, funny, 0, NULL, 0, subscript_type);
0920b7fa
FC
1630 }
1631 return
637494ac 1632 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
0920b7fa
FC
1633 }
1634}
1635
637494ac
TC
1636static SV *
1637S_op_varname(pTHX_ const OP *o)
1638{
1639 return S_op_varname_subscript(aTHX_ o, 1);
1640}
1641
429a2555 1642static void
2186f873
FC
1643S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1644{ /* or not so pretty :-) */
2186f873
FC
1645 if (o->op_type == OP_CONST) {
1646 *retsv = cSVOPo_sv;
1647 if (SvPOK(*retsv)) {
1648 SV *sv = *retsv;
1649 *retsv = sv_newmortal();
1650 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1651 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1652 }
1653 else if (!SvOK(*retsv))
1654 *retpv = "undef";
1655 }
1656 else *retpv = "...";
1657}
1658
1659static void
429a2555
FC
1660S_scalar_slice_warning(pTHX_ const OP *o)
1661{
1662 OP *kid;
1663 const char lbrack =
2186f873 1664 o->op_type == OP_HSLICE ? '{' : '[';
429a2555 1665 const char rbrack =
2186f873 1666 o->op_type == OP_HSLICE ? '}' : ']';
429a2555 1667 SV *name;
32e9ec8f 1668 SV *keysv = NULL; /* just to silence compiler warnings */
429a2555
FC
1669 const char *key = NULL;
1670
1671 if (!(o->op_private & OPpSLICEWARNING))
1672 return;
1673 if (PL_parser && PL_parser->error_count)
1674 /* This warning can be nonsensical when there is a syntax error. */
1675 return;
1676
1677 kid = cLISTOPo->op_first;
e6dae479 1678 kid = OpSIBLING(kid); /* get past pushmark */
429a2555
FC
1679 /* weed out false positives: any ops that can return lists */
1680 switch (kid->op_type) {
1681 case OP_BACKTICK:
1682 case OP_GLOB:
1683 case OP_READLINE:
1684 case OP_MATCH:
1685 case OP_RV2AV:
1686 case OP_EACH:
1687 case OP_VALUES:
1688 case OP_KEYS:
1689 case OP_SPLIT:
1690 case OP_LIST:
1691 case OP_SORT:
1692 case OP_REVERSE:
1693 case OP_ENTERSUB:
1694 case OP_CALLER:
1695 case OP_LSTAT:
1696 case OP_STAT:
1697 case OP_READDIR:
1698 case OP_SYSTEM:
1699 case OP_TMS:
1700 case OP_LOCALTIME:
1701 case OP_GMTIME:
1702 case OP_ENTEREVAL:
429a2555
FC
1703 return;
1704 }
7d3c8a68
SM
1705
1706 /* Don't warn if we have a nulled list either. */
1707 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1708 return;
1709
e6dae479
FC
1710 assert(OpSIBLING(kid));
1711 name = S_op_varname(aTHX_ OpSIBLING(kid));
429a2555
FC
1712 if (!name) /* XS module fiddling with the op tree */
1713 return;
2186f873 1714 S_op_pretty(aTHX_ kid, &keysv, &key);
429a2555
FC
1715 assert(SvPOK(name));
1716 sv_chop(name,SvPVX(name)+1);
1717 if (key)
2186f873 1718 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
429a2555 1719 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2186f873 1720 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
429a2555 1721 "%c%s%c",
2186f873 1722 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
429a2555
FC
1723 lbrack, key, rbrack);
1724 else
2186f873 1725 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
429a2555 1726 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
2186f873 1727 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
429a2555 1728 SVf"%c%"SVf"%c",
c1f6cd39
BF
1729 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1730 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
429a2555
FC
1731}
1732
8990e307 1733OP *
864dbfa3 1734Perl_scalar(pTHX_ OP *o)
79072805
LW
1735{
1736 OP *kid;
1737
a0d0e21e 1738 /* assumes no premature commitment */
13765c85
DM
1739 if (!o || (PL_parser && PL_parser->error_count)
1740 || (o->op_flags & OPf_WANT)
5dc0d613 1741 || o->op_type == OP_RETURN)
7e363e51 1742 {
11343788 1743 return o;
7e363e51 1744 }
79072805 1745
5dc0d613 1746 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
79072805 1747
11343788 1748 switch (o->op_type) {
79072805 1749 case OP_REPEAT:
11343788 1750 scalar(cBINOPo->op_first);
82e4f303
FC
1751 if (o->op_private & OPpREPEAT_DOLIST) {
1752 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1753 assert(kid->op_type == OP_PUSHMARK);
e6dae479 1754 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
82e4f303
FC
1755 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1756 o->op_private &=~ OPpREPEAT_DOLIST;
1757 }
1758 }
8990e307 1759 break;
79072805
LW
1760 case OP_OR:
1761 case OP_AND:
1762 case OP_COND_EXPR:
e6dae479 1763 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
8990e307 1764 scalar(kid);
79072805 1765 break;
924ba076 1766 /* FALLTHROUGH */
a6d8037e 1767 case OP_SPLIT:
79072805 1768 case OP_MATCH:
8782bef2 1769 case OP_QR:
79072805
LW
1770 case OP_SUBST:
1771 case OP_NULL:
8990e307 1772 default:
11343788 1773 if (o->op_flags & OPf_KIDS) {
e6dae479 1774 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
8990e307
LW
1775 scalar(kid);
1776 }
79072805
LW
1777 break;
1778 case OP_LEAVE:
1779 case OP_LEAVETRY:
5dc0d613 1780 kid = cLISTOPo->op_first;
54310121 1781 scalar(kid);
e6dae479 1782 kid = OpSIBLING(kid);
25b991bf
VP
1783 do_kids:
1784 while (kid) {
e6dae479 1785 OP *sib = OpSIBLING(kid);
34b54951 1786 if (sib && kid->op_type != OP_LEAVEWHEN
e6dae479 1787 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
34b54951
FC
1788 || ( sib->op_targ != OP_NEXTSTATE
1789 && sib->op_targ != OP_DBSTATE )))
c08f093b
VP
1790 scalarvoid(kid);
1791 else
54310121 1792 scalar(kid);
25b991bf 1793 kid = sib;
54310121 1794 }
11206fdd 1795 PL_curcop = &PL_compiling;
54310121 1796 break;
748a9306 1797 case OP_SCOPE:
79072805 1798 case OP_LINESEQ:
8990e307 1799 case OP_LIST:
25b991bf
VP
1800 kid = cLISTOPo->op_first;
1801 goto do_kids;
a801c63c 1802 case OP_SORT:
a2a5de95 1803 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
553e7bb0 1804 break;
95a31aad
FC
1805 case OP_KVHSLICE:
1806 case OP_KVASLICE:
2186f873
FC
1807 {
1808 /* Warn about scalar context */
1809 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1810 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1811 SV *name;
1812 SV *keysv;
1813 const char *key = NULL;
1814
1815 /* This warning can be nonsensical when there is a syntax error. */
1816 if (PL_parser && PL_parser->error_count)
1817 break;
1818
1819 if (!ckWARN(WARN_SYNTAX)) break;
1820
1821 kid = cLISTOPo->op_first;
e6dae479
FC
1822 kid = OpSIBLING(kid); /* get past pushmark */
1823 assert(OpSIBLING(kid));
1824 name = S_op_varname(aTHX_ OpSIBLING(kid));
2186f873
FC
1825 if (!name) /* XS module fiddling with the op tree */
1826 break;
1827 S_op_pretty(aTHX_ kid, &keysv, &key);
1828 assert(SvPOK(name));
1829 sv_chop(name,SvPVX(name)+1);
1830 if (key)
1831 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1832 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1833 "%%%"SVf"%c%s%c in scalar context better written "
1834 "as $%"SVf"%c%s%c",
1835 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1836 lbrack, key, rbrack);
1837 else
1838 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1839 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1840 "%%%"SVf"%c%"SVf"%c in scalar context better "
1841 "written as $%"SVf"%c%"SVf"%c",
c1f6cd39
BF
1842 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1843 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
2186f873 1844 }
79072805 1845 }
11343788 1846 return o;
79072805
LW
1847}
1848
1849OP *
aa9d1253 1850Perl_scalarvoid(pTHX_ OP *arg)
79072805 1851{
27da23d5 1852 dVAR;
79072805 1853 OP *kid;
8990e307 1854 SV* sv;
2ebea0a1 1855 U8 want;
aa9d1253
TC
1856 SSize_t defer_stack_alloc = 0;
1857 SSize_t defer_ix = -1;
1858 OP **defer_stack = NULL;
1859 OP *o = arg;
2ebea0a1 1860
7918f24d
NC
1861 PERL_ARGS_ASSERT_SCALARVOID;
1862
aa9d1253
TC
1863 do {
1864 SV *useless_sv = NULL;
1865 const char* useless = NULL;
1866
26f0e7d5
TC
1867 if (o->op_type == OP_NEXTSTATE
1868 || o->op_type == OP_DBSTATE
1869 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1870 || o->op_targ == OP_DBSTATE)))
1871 PL_curcop = (COP*)o; /* for warning below */
1872
1873 /* assumes no premature commitment */
1874 want = o->op_flags & OPf_WANT;
1875 if ((want && want != OPf_WANT_SCALAR)
1876 || (PL_parser && PL_parser->error_count)
1877 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1878 {
1879 continue;
1880 }
1c846c1f 1881
26f0e7d5
TC
1882 if ((o->op_private & OPpTARGET_MY)
1883 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1884 {
0d18dd72
FC
1885 /* newASSIGNOP has already applied scalar context, which we
1886 leave, as if this op is inside SASSIGN. */
26f0e7d5
TC
1887 continue;
1888 }
79072805 1889
26f0e7d5 1890 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
75068674 1891
26f0e7d5
TC
1892 switch (o->op_type) {
1893 default:
1894 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1895 break;
1896 /* FALLTHROUGH */
1897 case OP_REPEAT:
1898 if (o->op_flags & OPf_STACKED)
1899 break;
1e2dd519
FC
1900 if (o->op_type == OP_REPEAT)
1901 scalar(cBINOPo->op_first);
26f0e7d5
TC
1902 goto func_ops;
1903 case OP_SUBSTR:
1904 if (o->op_private == 4)
1905 break;
1906 /* FALLTHROUGH */
26f0e7d5
TC
1907 case OP_WANTARRAY:
1908 case OP_GV:
1909 case OP_SMARTMATCH:
26f0e7d5
TC
1910 case OP_AV2ARYLEN:
1911 case OP_REF:
1912 case OP_REFGEN:
1913 case OP_SREFGEN:
1914 case OP_DEFINED:
1915 case OP_HEX:
1916 case OP_OCT:
1917 case OP_LENGTH:
1918 case OP_VEC:
1919 case OP_INDEX:
1920 case OP_RINDEX:
1921 case OP_SPRINTF:
26f0e7d5 1922 case OP_KVASLICE:
26f0e7d5
TC
1923 case OP_KVHSLICE:
1924 case OP_UNPACK:
1925 case OP_PACK:
1926 case OP_JOIN:
1927 case OP_LSLICE:
1928 case OP_ANONLIST:
1929 case OP_ANONHASH:
1930 case OP_SORT:
1931 case OP_REVERSE:
1932 case OP_RANGE:
1933 case OP_FLIP:
1934 case OP_FLOP:
1935 case OP_CALLER:
1936 case OP_FILENO:
1937 case OP_EOF:
1938 case OP_TELL:
1939 case OP_GETSOCKNAME:
1940 case OP_GETPEERNAME:
1941 case OP_READLINK:
1942 case OP_TELLDIR:
1943 case OP_GETPPID:
1944 case OP_GETPGRP:
1945 case OP_GETPRIORITY:
1946 case OP_TIME:
1947 case OP_TMS:
1948 case OP_LOCALTIME:
1949 case OP_GMTIME:
1950 case OP_GHBYNAME:
1951 case OP_GHBYADDR:
1952 case OP_GHOSTENT:
1953 case OP_GNBYNAME:
1954 case OP_GNBYADDR:
1955 case OP_GNETENT:
1956 case OP_GPBYNAME:
1957 case OP_GPBYNUMBER:
1958 case OP_GPROTOENT:
1959 case OP_GSBYNAME:
1960 case OP_GSBYPORT:
1961 case OP_GSERVENT:
1962 case OP_GPWNAM:
1963 case OP_GPWUID:
1964 case OP_GGRNAM:
1965 case OP_GGRGID:
1966 case OP_GETLOGIN:
1967 case OP_PROTOTYPE:
1968 case OP_RUNCV:
1969 func_ops:
9e209402
FC
1970 useless = OP_DESC(o);
1971 break;
1972
1973 case OP_GVSV:
1974 case OP_PADSV:
1975 case OP_PADAV:
1976 case OP_PADHV:
1977 case OP_PADANY:
1978 case OP_AELEM:
1979 case OP_AELEMFAST:
1980 case OP_AELEMFAST_LEX:
1981 case OP_ASLICE:
1982 case OP_HELEM:
1983 case OP_HSLICE:
26f0e7d5 1984 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
ea5519d6 1985 /* Otherwise it's "Useless use of grep iterator" */
3c3f8cd6 1986 useless = OP_DESC(o);
ea5519d6 1987 break;
26f0e7d5
TC
1988
1989 case OP_SPLIT:
1990 kid = cLISTOPo->op_first;
1991 if (kid && kid->op_type == OP_PUSHRE
1992 && !kid->op_targ
1993 && !(o->op_flags & OPf_STACKED)
75068674 1994#ifdef USE_ITHREADS
26f0e7d5 1995 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
75068674 1996#else
26f0e7d5 1997 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
75068674 1998#endif
26f0e7d5
TC
1999 )
2000 useless = OP_DESC(o);
2001 break;
2002
2003 case OP_NOT:
2004 kid = cUNOPo->op_first;
2005 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2006 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2007 goto func_ops;
2008 }
2009 useless = "negative pattern binding (!~)";
2010 break;
2011
2012 case OP_SUBST:
2013 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2014 useless = "non-destructive substitution (s///r)";
2015 break;
2016
2017 case OP_TRANSR:
2018 useless = "non-destructive transliteration (tr///r)";
2019 break;
2020
2021 case OP_RV2GV:
2022 case OP_RV2SV:
2023 case OP_RV2AV:
2024 case OP_RV2HV:
2025 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
e6dae479 2026 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
26f0e7d5
TC
2027 useless = "a variable";
2028 break;
2029
2030 case OP_CONST:
2031 sv = cSVOPo_sv;
2032 if (cSVOPo->op_private & OPpCONST_STRICT)
2033 no_bareword_allowed(o);
2034 else {
2035 if (ckWARN(WARN_VOID)) {
2036 NV nv;
2037 /* don't warn on optimised away booleans, eg
2038 * use constant Foo, 5; Foo || print; */
2039 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2040 useless = NULL;
2041 /* the constants 0 and 1 are permitted as they are
2042 conventionally used as dummies in constructs like
2043 1 while some_condition_with_side_effects; */
2044 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2045 useless = NULL;
2046 else if (SvPOK(sv)) {
2047 SV * const dsv = newSVpvs("");
2048 useless_sv
2049 = Perl_newSVpvf(aTHX_
2050 "a constant (%s)",
2051 pv_pretty(dsv, SvPVX_const(sv),
2052 SvCUR(sv), 32, NULL, NULL,
2053 PERL_PV_PRETTY_DUMP
2054 | PERL_PV_ESCAPE_NOCLEAR
2055 | PERL_PV_ESCAPE_UNI_DETECT));
2056 SvREFCNT_dec_NN(dsv);
2057 }
2058 else if (SvOK(sv)) {
2059 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
2060 }
2061 else
2062 useless = "a constant (undef)";
2063 }
2064 }
2065 op_null(o); /* don't execute or even remember it */
2066 break;
79072805 2067
26f0e7d5 2068 case OP_POSTINC:
b9a07097 2069 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
26f0e7d5 2070 break;
79072805 2071
26f0e7d5 2072 case OP_POSTDEC:
b9a07097 2073 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
26f0e7d5 2074 break;
79072805 2075
26f0e7d5 2076 case OP_I_POSTINC:
b9a07097 2077 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
26f0e7d5 2078 break;
79072805 2079
26f0e7d5 2080 case OP_I_POSTDEC:
b9a07097 2081 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
26f0e7d5 2082 break;
679d6c4e 2083
26f0e7d5
TC
2084 case OP_SASSIGN: {
2085 OP *rv2gv;
2086 UNOP *refgen, *rv2cv;
2087 LISTOP *exlist;
679d6c4e 2088
26f0e7d5
TC
2089 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2090 break;
f2f8fd84 2091
26f0e7d5
TC
2092 rv2gv = ((BINOP *)o)->op_last;
2093 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2094 break;
f2f8fd84 2095
26f0e7d5 2096 refgen = (UNOP *)((BINOP *)o)->op_first;
f2f8fd84 2097
26f0e7d5
TC
2098 if (!refgen || (refgen->op_type != OP_REFGEN
2099 && refgen->op_type != OP_SREFGEN))
2100 break;
f2f8fd84 2101
26f0e7d5
TC
2102 exlist = (LISTOP *)refgen->op_first;
2103 if (!exlist || exlist->op_type != OP_NULL
2104 || exlist->op_targ != OP_LIST)
2105 break;
f2f8fd84 2106
26f0e7d5
TC
2107 if (exlist->op_first->op_type != OP_PUSHMARK
2108 && exlist->op_first != exlist->op_last)
2109 break;
f2f8fd84 2110
26f0e7d5 2111 rv2cv = (UNOP*)exlist->op_last;
f2f8fd84 2112
26f0e7d5
TC
2113 if (rv2cv->op_type != OP_RV2CV)
2114 break;
f2f8fd84 2115
26f0e7d5
TC
2116 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2117 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2118 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
f2f8fd84 2119
26f0e7d5
TC
2120 o->op_private |= OPpASSIGN_CV_TO_GV;
2121 rv2gv->op_private |= OPpDONT_INIT_GV;
2122 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
f2f8fd84 2123
26f0e7d5
TC
2124 break;
2125 }
540dd770 2126
26f0e7d5
TC
2127 case OP_AASSIGN: {
2128 inplace_aassign(o);
2129 break;
2130 }
edbe35ea 2131
26f0e7d5
TC
2132 case OP_OR:
2133 case OP_AND:
2134 kid = cLOGOPo->op_first;
2135 if (kid->op_type == OP_NOT
2136 && (kid->op_flags & OPf_KIDS)) {
2137 if (o->op_type == OP_AND) {
b9a07097 2138 OpTYPE_set(o, OP_OR);
26f0e7d5 2139 } else {
b9a07097 2140 OpTYPE_set(o, OP_AND);
26f0e7d5
TC
2141 }
2142 op_null(kid);
2143 }
2144 /* FALLTHROUGH */
5aabfad6 2145
26f0e7d5
TC
2146 case OP_DOR:
2147 case OP_COND_EXPR:
2148 case OP_ENTERGIVEN:
2149 case OP_ENTERWHEN:
e6dae479 2150 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
26f0e7d5
TC
2151 if (!(kid->op_flags & OPf_KIDS))
2152 scalarvoid(kid);
2153 else
2154 DEFER_OP(kid);
aa9d1253 2155 break;
095b19d1 2156
26f0e7d5
TC
2157 case OP_NULL:
2158 if (o->op_flags & OPf_STACKED)
2159 break;
2160 /* FALLTHROUGH */
2161 case OP_NEXTSTATE:
2162 case OP_DBSTATE:
2163 case OP_ENTERTRY:
2164 case OP_ENTER:
2165 if (!(o->op_flags & OPf_KIDS))
2166 break;
2167 /* FALLTHROUGH */
2168 case OP_SCOPE:
2169 case OP_LEAVE:
2170 case OP_LEAVETRY:
2171 case OP_LEAVELOOP:
2172 case OP_LINESEQ:
2173 case OP_LEAVEGIVEN:
2174 case OP_LEAVEWHEN:
2175 kids:
e6dae479 2176 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
26f0e7d5
TC
2177 if (!(kid->op_flags & OPf_KIDS))
2178 scalarvoid(kid);
2179 else
2180 DEFER_OP(kid);
2181 break;
2182 case OP_LIST:
2183 /* If the first kid after pushmark is something that the padrange
2184 optimisation would reject, then null the list and the pushmark.
2185 */
2186 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
e6dae479 2187 && ( !(kid = OpSIBLING(kid))
26f0e7d5
TC
2188 || ( kid->op_type != OP_PADSV
2189 && kid->op_type != OP_PADAV
2190 && kid->op_type != OP_PADHV)
2191 || kid->op_private & ~OPpLVAL_INTRO
e6dae479 2192 || !(kid = OpSIBLING(kid))
26f0e7d5
TC
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 ) {
2198 op_null(cUNOPo->op_first); /* NULL the pushmark */
2199 op_null(o); /* NULL the list */
2200 }
2201 goto kids;
2202 case OP_ENTEREVAL:
2203 scalarkids(o);
2204 break;
2205 case OP_SCALAR:
2206 scalar(o);
2207 break;
2208 }
2209
2210 if (useless_sv) {
2211 /* mortalise it, in case warnings are fatal. */
2212 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2213 "Useless use of %"SVf" in void context",
2214 SVfARG(sv_2mortal(useless_sv)));
2215 }
2216 else if (useless) {
3c3f8cd6
AB
2217 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2218 "Useless use of %s in void context",
2219 useless);
26f0e7d5 2220 }
aa9d1253
TC
2221 } while ( (o = POP_DEFERRED_OP()) );
2222
2223 Safefree(defer_stack);
2224
2225 return arg;
79072805
LW
2226}
2227
1f676739 2228static OP *
412da003 2229S_listkids(pTHX_ OP *o)
79072805 2230{
11343788 2231 if (o && o->op_flags & OPf_KIDS) {
6867be6d 2232 OP *kid;
e6dae479 2233 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
79072805
LW
2234 list(kid);
2235 }
11343788 2236 return o;
79072805
LW
2237}
2238
2239OP *
864dbfa3 2240Perl_list(pTHX_ OP *o)
79072805
LW
2241{
2242 OP *kid;
2243
a0d0e21e 2244 /* assumes no premature commitment */
13765c85
DM
2245 if (!o || (o->op_flags & OPf_WANT)
2246 || (PL_parser && PL_parser->error_count)
5dc0d613 2247 || o->op_type == OP_RETURN)
7e363e51 2248 {
11343788 2249 return o;
7e363e51 2250 }
79072805 2251
b162f9ea 2252 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
2253 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2254 {
b162f9ea 2255 return o; /* As if inside SASSIGN */
7e363e51 2256 }
1c846c1f 2257
5dc0d613 2258 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
79072805 2259
11343788 2260 switch (o->op_type) {
79072805 2261 case OP_FLOP:
11343788 2262 list(cBINOPo->op_first);
79072805 2263 break;
c57eecc5
FC
2264 case OP_REPEAT:
2265 if (o->op_private & OPpREPEAT_DOLIST
2266 && !(o->op_flags & OPf_STACKED))
2267 {
2268 list(cBINOPo->op_first);
2269 kid = cBINOPo->op_last;
2270 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2271 && SvIVX(kSVOP_sv) == 1)
2272 {
2273 op_null(o); /* repeat */
2274 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2275 /* const (rhs): */
2276 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2277 }
2278 }
2279 break;
79072805
LW
2280 case OP_OR:
2281 case OP_AND:
2282 case OP_COND_EXPR:
e6dae479 2283 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
79072805
LW
2284 list(kid);
2285 break;
2286 default:
2287 case OP_MATCH:
8782bef2 2288 case OP_QR:
79072805
LW
2289 case OP_SUBST:
2290 case OP_NULL:
11343788 2291 if (!(o->op_flags & OPf_KIDS))
79072805 2292 break;
11343788
MB
2293 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2294 list(cBINOPo->op_first);
2295 return gen_constant_list(o);
79072805 2296 }
6aa68307
FC
2297 listkids(o);
2298 break;
79072805 2299 case OP_LIST:
11343788 2300 listkids(o);
6aa68307
FC
2301 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2302 op_null(cUNOPo->op_first); /* NULL the pushmark */
2303 op_null(o); /* NULL the list */
2304 }
79072805
LW
2305 break;
2306 case OP_LEAVE:
2307 case OP_LEAVETRY:
5dc0d613 2308 kid = cLISTOPo->op_first;
54310121 2309 list(kid);
e6dae479 2310 kid = OpSIBLING(kid);
25b991bf
VP
2311 do_kids:
2312 while (kid) {
e6dae479 2313 OP *sib = OpSIBLING(kid);
c08f093b
VP
2314 if (sib && kid->op_type != OP_LEAVEWHEN)
2315 scalarvoid(kid);
2316 else
54310121 2317 list(kid);
25b991bf 2318 kid = sib;
54310121 2319 }
11206fdd 2320 PL_curcop = &PL_compiling;
54310121 2321 break;
748a9306 2322 case OP_SCOPE:
79072805 2323 case OP_LINESEQ:
25b991bf
VP
2324 kid = cLISTOPo->op_first;
2325 goto do_kids;
79072805 2326 }
11343788 2327 return o;
79072805
LW
2328}
2329
1f676739 2330static OP *
2dd5337b 2331S_scalarseq(pTHX_ OP *o)
79072805 2332{
11343788 2333 if (o) {
1496a290
AL
2334 const OPCODE type = o->op_type;
2335
2336 if (type == OP_LINESEQ || type == OP_SCOPE ||
2337 type == OP_LEAVE || type == OP_LEAVETRY)
463ee0b2 2338 {
b7bea5da
FC
2339 OP *kid, *sib;
2340 for (kid = cLISTOPo->op_first; kid; kid = sib) {
e6dae479
FC
2341 if ((sib = OpSIBLING(kid))
2342 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
b7bea5da
FC
2343 || ( sib->op_targ != OP_NEXTSTATE
2344 && sib->op_targ != OP_DBSTATE )))
2345 {
463ee0b2 2346 scalarvoid(kid);
ed6116ce 2347 }
463ee0b2 2348 }
3280af22 2349 PL_curcop = &PL_compiling;
79072805 2350 }
11343788 2351 o->op_flags &= ~OPf_PARENS;
3280af22 2352 if (PL_hints & HINT_BLOCK_SCOPE)
11343788 2353 o->op_flags |= OPf_PARENS;
79072805 2354 }
8990e307 2355 else
11343788
MB
2356 o = newOP(OP_STUB, 0);
2357 return o;
79072805
LW
2358}
2359
76e3520e 2360STATIC OP *
cea2e8a9 2361S_modkids(pTHX_ OP *o, I32 type)
79072805 2362{
11343788 2363 if (o && o->op_flags & OPf_KIDS) {
6867be6d 2364 OP *kid;
e6dae479 2365 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3ad73efd 2366 op_lvalue(kid, type);
79072805 2367 }
11343788 2368 return o;
79072805
LW
2369}
2370
12ee5d32
DM
2371
2372/* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2373 * const fields. Also, convert CONST keys to HEK-in-SVs.
2374 * rop is the op that retrieves the hash;
2375 * key_op is the first key
2376 */
2377
f9db5646 2378STATIC void
fedf30e1 2379S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op)
12ee5d32
DM
2380{
2381 PADNAME *lexname;
2382 GV **fields;
2383 bool check_fields;
2384
2385 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2386 if (rop) {
2387 if (rop->op_first->op_type == OP_PADSV)
2388 /* @$hash{qw(keys here)} */
2389 rop = (UNOP*)rop->op_first;
2390 else {
2391 /* @{$hash}{qw(keys here)} */
2392 if (rop->op_first->op_type == OP_SCOPE
2393 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2394 {
2395 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2396 }
2397 else
2398 rop = NULL;
2399 }
2400 }
2401
2402 lexname = NULL; /* just to silence compiler warnings */
2403 fields = NULL; /* just to silence compiler warnings */
2404
2405 check_fields =
2406 rop
2407 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2408 SvPAD_TYPED(lexname))
2409 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2410 && isGV(*fields) && GvHV(*fields);
2411
e6dae479 2412 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) {
12ee5d32
DM
2413 SV **svp, *sv;
2414 if (key_op->op_type != OP_CONST)
2415 continue;
2416 svp = cSVOPx_svp(key_op);
2417
e1ccd220
DIM
2418 /* make sure it's not a bareword under strict subs */
2419 if (key_op->op_private & OPpCONST_BARE &&
2420 key_op->op_private & OPpCONST_STRICT)
2421 {
2422 no_bareword_allowed((OP*)key_op);
2423 }
2424
12ee5d32
DM
2425 /* Make the CONST have a shared SV */
2426 if ( !SvIsCOW_shared_hash(sv = *svp)
2427 && SvTYPE(sv) < SVt_PVMG
2428 && SvOK(sv)
2429 && !SvROK(sv))
2430 {
2431 SSize_t keylen;
2432 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2433 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2434 SvREFCNT_dec_NN(sv);
2435 *svp = nsv;
2436 }
2437
2438 if ( check_fields
2439 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2440 {
2441 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2442 "in variable %"PNf" of type %"HEKf,
2443 SVfARG(*svp), PNfARG(lexname),
2444 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2445 }
2446 }
2447}
2448
2449
3ad73efd 2450/*
d164302a
GG
2451=for apidoc finalize_optree
2452
72d33970
FC
2453This function finalizes the optree. Should be called directly after
2454the complete optree is built. It does some additional
796b6530 2455checking which can't be done in the normal C<ck_>xxx functions and makes
d164302a
GG
2456the tree thread-safe.
2457
2458=cut
2459*/
2460void
2461Perl_finalize_optree(pTHX_ OP* o)
2462{
2463 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2464
2465 ENTER;
2466 SAVEVPTR(PL_curcop);
2467
2468 finalize_op(o);
2469
2470 LEAVE;
2471}
2472
b46e009d 2473#ifdef USE_ITHREADS
2474/* Relocate sv to the pad for thread safety.
2475 * Despite being a "constant", the SV is written to,
2476 * for reference counts, sv_upgrade() etc. */
2477PERL_STATIC_INLINE void
2478S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2479{
2480 PADOFFSET ix;
2481 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2482 if (!*svp) return;
2483 ix = pad_alloc(OP_CONST, SVf_READONLY);
2484 SvREFCNT_dec(PAD_SVl(ix));
2485 PAD_SETSV(ix, *svp);
2486 /* XXX I don't know how this isn't readonly already. */
2487 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2488 *svp = NULL;
2489 *targp = ix;
2490}
2491#endif
2492
2493
60dde6b2 2494STATIC void
d164302a
GG
2495S_finalize_op(pTHX_ OP* o)
2496{
2497 PERL_ARGS_ASSERT_FINALIZE_OP;
2498
d164302a
GG
2499
2500 switch (o->op_type) {
2501 case OP_NEXTSTATE:
2502 case OP_DBSTATE:
2503 PL_curcop = ((COP*)o); /* for warnings */
2504 break;
2505 case OP_EXEC:
e6dae479
FC
2506 if (OpHAS_SIBLING(o)) {
2507 OP *sib = OpSIBLING(o);
1ed44841
DM
2508 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2509 && ckWARN(WARN_EXEC)
e6dae479 2510 && OpHAS_SIBLING(sib))
1ed44841 2511 {
e6dae479 2512 const OPCODE type = OpSIBLING(sib)->op_type;
d164302a
GG
2513 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2514 const line_t oldline = CopLINE(PL_curcop);
1ed44841 2515 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
d164302a
GG
2516 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2517 "Statement unlikely to be reached");
2518 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2519 "\t(Maybe you meant system() when you said exec()?)\n");
2520 CopLINE_set(PL_curcop, oldline);
2521 }
d164302a 2522 }
1ed44841 2523 }
d164302a
GG
2524 break;
2525
2526 case OP_GV:
2527 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2528 GV * const gv = cGVOPo_gv;
2529 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2530 /* XXX could check prototype here instead of just carping */
2531 SV * const sv = sv_newmortal();
2532 gv_efullname3(sv, gv, NULL);
2533 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2534 "%"SVf"() called too early to check prototype",
2535 SVfARG(sv));
2536 }
2537 }
2538 break;
2539
2540 case OP_CONST:
eb796c7f
GG
2541 if (cSVOPo->op_private & OPpCONST_STRICT)
2542 no_bareword_allowed(o);
2543 /* FALLTHROUGH */
d164302a
GG
2544#ifdef USE_ITHREADS
2545 case OP_HINTSEVAL:
b46e009d 2546 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2547#endif
2548 break;
2549
2550#ifdef USE_ITHREADS
2551 /* Relocate all the METHOP's SVs to the pad for thread safety. */
d164302a 2552 case OP_METHOD_NAMED:
7d6c333c 2553 case OP_METHOD_SUPER:
810bd8b7 2554 case OP_METHOD_REDIR:
2555 case OP_METHOD_REDIR_SUPER:
b46e009d 2556 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2557 break;
d164302a 2558#endif
d164302a
GG
2559
2560 case OP_HELEM: {
2561 UNOP *rop;
565e6f7e
FC
2562 SVOP *key_op;
2563 OP *kid;
d164302a 2564
565e6f7e 2565 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
d164302a
GG
2566 break;
2567
2568 rop = (UNOP*)((BINOP*)o)->op_first;
e6307ed0 2569
565e6f7e 2570 goto check_keys;
d164302a 2571
565e6f7e 2572 case OP_HSLICE:
429a2555 2573 S_scalar_slice_warning(aTHX_ o);
c67159e1 2574 /* FALLTHROUGH */
429a2555 2575
c5f75dba 2576 case OP_KVHSLICE:
e6dae479 2577 kid = OpSIBLING(cLISTOPo->op_first);
71323522 2578 if (/* I bet there's always a pushmark... */
7d3c8a68
SM
2579 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2580 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2581 {
d164302a 2582 break;
7d3c8a68 2583 }
565e6f7e
FC
2584
2585 key_op = (SVOP*)(kid->op_type == OP_CONST
2586 ? kid
e6dae479 2587 : OpSIBLING(kLISTOP->op_first));
565e6f7e
FC
2588
2589 rop = (UNOP*)((LISTOP*)o)->op_last;
2590
2591 check_keys:
12ee5d32
DM
2592 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2593 rop = NULL;
fedf30e1 2594 S_check_hash_fields_and_hekify(aTHX_ rop, key_op);
d164302a
GG
2595 break;
2596 }
429a2555
FC
2597 case OP_ASLICE:
2598 S_scalar_slice_warning(aTHX_ o);
2599 break;
a7fd8ef6 2600
d164302a
GG
2601 case OP_SUBST: {
2602 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2603 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2604 break;
2605 }
2606 default:
2607 break;
2608 }
2609
2610 if (o->op_flags & OPf_KIDS) {
2611 OP *kid;
c4b20975
DM
2612
2613#ifdef DEBUGGING
20220689 2614 /* check that op_last points to the last sibling, and that
86cd3a13
DM
2615 * the last op_sibling/op_sibparent field points back to the
2616 * parent, and that the only ops with KIDS are those which are
2617 * entitled to them */
c4b20975
DM
2618 U32 type = o->op_type;
2619 U32 family;
20220689 2620 bool has_last;
c4b20975
DM
2621
2622 if (type == OP_NULL) {
2623 type = o->op_targ;
2624 /* ck_glob creates a null UNOP with ex-type GLOB
2625 * (which is a list op. So pretend it wasn't a listop */
2626 if (type == OP_GLOB)
2627 type = OP_NULL;
2628 }
2629 family = PL_opargs[type] & OA_CLASS_MASK;
2630
20220689
DM
2631 has_last = ( family == OA_BINOP
2632 || family == OA_LISTOP
2633 || family == OA_PMOP
2634 || family == OA_LOOP
2635 );
2636 assert( has_last /* has op_first and op_last, or ...
2637 ... has (or may have) op_first: */
2638 || family == OA_UNOP
2f7c6295 2639 || family == OA_UNOP_AUX
20220689
DM
2640 || family == OA_LOGOP
2641 || family == OA_BASEOP_OR_UNOP
2642 || family == OA_FILESTATOP
2643 || family == OA_LOOPEXOP
b46e009d 2644 || family == OA_METHOP
20220689
DM
2645 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */
2646 || type == OP_SASSIGN
2647 || type == OP_CUSTOM
2648 || type == OP_NULL /* new_logop does this */
2649 );
20220689 2650
e6dae479 2651 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) {
29e61fd9 2652# ifdef PERL_OP_PARENT
e6dae479 2653 if (!OpHAS_SIBLING(kid)) {
20220689 2654 if (has_last)
29e61fd9 2655 assert(kid == cLISTOPo->op_last);
86cd3a13 2656 assert(kid->op_sibparent == o);
20220689 2657 }
29e61fd9 2658# else
93059c1a
DM
2659 if (has_last && !OpHAS_SIBLING(kid))
2660 assert(kid == cLISTOPo->op_last);
20220689 2661# endif
c4b20975
DM
2662 }
2663#endif
2664
e6dae479 2665 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid))
d164302a
GG
2666 finalize_op(kid);
2667 }
2668}
2669
2670/*
3ad73efd
Z
2671=for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2672
2673Propagate lvalue ("modifiable") context to an op and its children.
2d7f6611 2674C<type> represents the context type, roughly based on the type of op that
796b6530 2675would do the modifying, although C<local()> is represented by C<OP_NULL>,
3ad73efd 2676because it has no op type of its own (it is signalled by a flag on
001c3c51
FC
2677the lvalue op).
2678
2679This function detects things that can't be modified, such as C<$x+1>, and
72d33970 2680generates errors for them. For example, C<$x+1 = 2> would cause it to be
796b6530 2681called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
001c3c51
FC
2682
2683It also flags things that need to behave specially in an lvalue context,
2684such as C<$$x = 5> which might have to vivify a reference in C<$x>.
3ad73efd
Z
2685
2686=cut
2687*/
ddeae0f1 2688
03414f05
FC
2689static void
2690S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2691{
2692 CV *cv = PL_compcv;
2693 PadnameLVALUE_on(pn);
2694 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2695 cv = CvOUTSIDE(cv);
aea0412a
DM
2696 /* RT #127786: cv can be NULL due to an eval within the DB package
2697 * called from an anon sub - anon subs don't have CvOUTSIDE() set
2698 * unless they contain an eval, but calling eval within DB
2699 * pretends the eval was done in the caller's scope.
2700 */
2701 if (!cv)
2702 break;
03414f05
FC
2703 assert(CvPADLIST(cv));
2704 pn =
2705 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2706 assert(PadnameLEN(pn));
2707 PadnameLVALUE_on(pn);
2708 }
2709}
2710
375879aa
FC
2711static bool
2712S_vivifies(const OPCODE type)
2713{
2714 switch(type) {
2715 case OP_RV2AV: case OP_ASLICE:
2716 case OP_RV2HV: case OP_KVASLICE:
2717 case OP_RV2SV: case OP_HSLICE:
2718 case OP_AELEMFAST: case OP_KVHSLICE:
2719 case OP_HELEM:
2720 case OP_AELEM:
2721 return 1;
2722 }
2723 return 0;
2724}
2725
7664512e 2726static void
63702de8 2727S_lvref(pTHX_ OP *o, I32 type)
7664512e 2728{
727d2dc6 2729 dVAR;
7664512e
FC
2730 OP *kid;
2731 switch (o->op_type) {
2732 case OP_COND_EXPR:
e6dae479
FC
2733 for (kid = OpSIBLING(cUNOPo->op_first); kid;
2734 kid = OpSIBLING(kid))
63702de8 2735 S_lvref(aTHX_ kid, type);
7664512e
FC
2736 /* FALLTHROUGH */
2737 case OP_PUSHMARK:
2738 return;
2739 case OP_RV2AV:
2740 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2741 o->op_flags |= OPf_STACKED;
2742 if (o->op_flags & OPf_PARENS) {
2743 if (o->op_private & OPpLVAL_INTRO) {
7664512e
FC
2744 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2745 "localized parenthesized array in list assignment"));
2746 return;
2747 }
2748 slurpy:
b9a07097 2749 OpTYPE_set(o, OP_LVAVREF);
7664512e
FC
2750 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2751 o->op_flags |= OPf_MOD|OPf_REF;
2752 return;
2753 }
2754 o->op_private |= OPpLVREF_AV;
2755 goto checkgv;
408e9044 2756 case OP_RV2CV:
19abb1ea
FC
2757 kid = cUNOPo->op_first;
2758 if (kid->op_type == OP_NULL)
cb748240 2759 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
408e9044
FC
2760 ->op_first;
2761 o->op_private = OPpLVREF_CV;
2762 if (kid->op_type == OP_GV)
2763 o->op_flags |= OPf_STACKED;
2764 else if (kid->op_type == OP_PADCV) {
2765 o->op_targ = kid->op_targ;
2766 kid->op_targ = 0;
2767 op_free(cUNOPo->op_first);
2768 cUNOPo->op_first = NULL;
2769 o->op_flags &=~ OPf_KIDS;
2770 }
2771 else goto badref;
2772 break;
7664512e
FC
2773 case OP_RV2HV:
2774 if (o->op_flags & OPf_PARENS) {
2775 parenhash:
7664512e
FC
2776 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2777 "parenthesized hash in list assignment"));
2778 return;
2779 }
2780 o->op_private |= OPpLVREF_HV;
2781 /* FALLTHROUGH */
2782 case OP_RV2SV:
2783 checkgv:
2784 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2785 o->op_flags |= OPf_STACKED;
6f5dab3c
FC
2786 break;
2787 case OP_PADHV:
2788 if (o->op_flags & OPf_PARENS) goto parenhash;
2789 o->op_private |= OPpLVREF_HV;
7664512e
FC
2790 /* FALLTHROUGH */
2791 case OP_PADSV:
6f5dab3c 2792 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
7664512e
FC
2793 break;
2794 case OP_PADAV:
6f5dab3c 2795 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
7664512e
FC
2796 if (o->op_flags & OPf_PARENS) goto slurpy;
2797 o->op_private |= OPpLVREF_AV;
2798 break;
7664512e
FC
2799 case OP_AELEM:
2800 case OP_HELEM:
2801 o->op_private |= OPpLVREF_ELEM;
2802 o->op_flags |= OPf_STACKED;
2803 break;
2804 case OP_ASLICE:
2805 case OP_HSLICE:
b9a07097 2806 OpTYPE_set(o, OP_LVREFSLICE);
36efb5a6 2807 o->op_private &= OPpLVAL_INTRO;
7664512e
FC
2808 return;
2809 case OP_NULL:
2810 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2811 goto badref;
2812 else if (!(o->op_flags & OPf_KIDS))
2813 return;
2814 if (o->op_targ != OP_LIST) {
63702de8 2815 S_lvref(aTHX_ cBINOPo->op_first, type);
7664512e
FC
2816 return;
2817 }
2818 /* FALLTHROUGH */
2819 case OP_LIST:
e6dae479 2820 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) {
7664512e 2821 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
63702de8 2822 S_lvref(aTHX_ kid, type);
7664512e
FC
2823 }
2824 return;
2825 case OP_STUB:
2826 if (o->op_flags & OPf_PARENS)
2827 return;
2828 /* FALLTHROUGH */
2829 default:
2830 badref:
cf6e1fa1 2831 /* diag_listed_as: Can't modify reference to %s in %s assignment */
63702de8 2832 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
7664512e
FC
2833 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2834 ? "do block"
63702de8
FC
2835 : OP_DESC(o),
2836 PL_op_desc[type]));
7664512e 2837 }
b9a07097 2838 OpTYPE_set(o, OP_LVREF);
3ad7d304
FC
2839 o->op_private &=
2840 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
d39c26a6
FC
2841 if (type == OP_ENTERLOOP)
2842 o->op_private |= OPpLVREF_ITER;
7664512e
FC
2843}
2844
1a3e9724
FC
2845PERL_STATIC_INLINE bool
2846S_potential_mod_type(I32 type)
2847{
2848 /* Types that only potentially result in modification. */
2849 return type == OP_GREPSTART || type == OP_ENTERSUB
2850 || type == OP_REFGEN || type == OP_LEAVESUBLV;
2851}
2852
79072805 2853OP *
d3d7d28f 2854Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
79072805 2855{
27da23d5 2856 dVAR;
79072805 2857 OP *kid;
ddeae0f1
DM
2858 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2859 int localize = -1;
79072805 2860
13765c85 2861 if (!o || (PL_parser && PL_parser->error_count))
11343788 2862 return o;
79072805 2863
b162f9ea 2864 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
2865 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2866 {
b162f9ea 2867 return o;
7e363e51 2868 }
1c846c1f 2869
5c906035
GG
2870 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2871
69974ce6
FC
2872 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2873
11343788 2874 switch (o->op_type) {
68dc0745 2875 case OP_UNDEF:
3280af22 2876 PL_modcount++;
5dc0d613 2877 return o;
5f05dabc 2878 case OP_STUB:
b5bbe64a 2879 if ((o->op_flags & OPf_PARENS))
5f05dabc
PP
2880 break;
2881 goto nomod;
a0d0e21e 2882 case OP_ENTERSUB:
f79aa60b 2883 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
11343788 2884 !(o->op_flags & OPf_STACKED)) {
b9a07097 2885 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
11343788 2886 assert(cUNOPo->op_first->op_type == OP_NULL);
93c66552 2887 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
79072805
LW
2888 break;
2889 }
cd06dffe 2890 else { /* lvalue subroutine call */
9411a3c7 2891 o->op_private |= OPpLVAL_INTRO;
e6438c1a 2892 PL_modcount = RETURN_UNLIMITED_NUMBER;
1a3e9724 2893 if (S_potential_mod_type(type)) {
cd06dffe
GS
2894 o->op_private |= OPpENTERSUB_INARGS;
2895 break;
2896 }
2897 else { /* Compile-time error message: */
2898 OP *kid = cUNOPo->op_first;
2899 CV *cv;
2eaf799e 2900 GV *gv;
0f948285 2901 SV *namesv;
cd06dffe 2902
3ea285d1
AL
2903 if (kid->op_type != OP_PUSHMARK) {
2904 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2905 Perl_croak(aTHX_
2906 "panic: unexpected lvalue entersub "
2907 "args: type/targ %ld:%"UVuf,
2908 (long)kid->op_type, (UV)kid->op_targ);
2909 kid = kLISTOP->op_first;
2910 }
e6dae479
FC
2911 while (OpHAS_SIBLING(kid))
2912 kid = OpSIBLING(kid);
cd06dffe 2913 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
cd06dffe
GS
2914 break; /* Postpone until runtime */
2915 }
b2ffa427 2916
cd06dffe
GS
2917 kid = kUNOP->op_first;
2918 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2919 kid = kUNOP->op_first;
b2ffa427 2920 if (kid->op_type == OP_NULL)
cd06dffe
GS
2921 Perl_croak(aTHX_
2922 "Unexpected constant lvalue entersub "
55140b79 2923 "entry via type/targ %ld:%"UVuf,
3d811634 2924 (long)kid->op_type, (UV)kid->op_targ);
cd06dffe 2925 if (kid->op_type != OP_GV) {
cd06dffe
GS
2926 break;
2927 }
b2ffa427 2928
2eaf799e
FC
2929 gv = kGVOP_gv;
2930 cv = isGV(gv)
2931 ? GvCV(gv)
2932 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2933 ? MUTABLE_CV(SvRV(gv))
2934 : NULL;
1c846c1f 2935 if (!cv)
da1dff94 2936 break;
cd06dffe
GS
2937 if (CvLVALUE(cv))
2938 break;
0f948285
DIM
2939 if (flags & OP_LVALUE_NO_CROAK)
2940 return NULL;
2941
2942 namesv = cv_name(cv, NULL, 0);
2943 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
2944 "subroutine call of &%"SVf" in %s",
2945 SVfARG(namesv), PL_op_desc[type]),
2946 SvUTF8(namesv));
2947 return o;
cd06dffe
GS
2948 }
2949 }
924ba076 2950 /* FALLTHROUGH */
79072805 2951 default:
a0d0e21e 2952 nomod:
f5d552b4 2953 if (flags & OP_LVALUE_NO_CROAK) return NULL;
6fbb66d6 2954 /* grep, foreach, subcalls, refgen */
1a3e9724 2955 if (S_potential_mod_type(type))
a0d0e21e 2956 break;
cea2e8a9 2957 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
638bc118 2958 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
cd06dffe 2959 ? "do block"
0f948285 2960 : OP_DESC(o)),
22c35a8c 2961 type ? PL_op_desc[type] : "local"));
11343788 2962 return o;
79072805 2963
a0d0e21e
LW
2964 case OP_PREINC:
2965 case OP_PREDEC:
2966 case OP_POW:
2967 case OP_MULTIPLY:
2968 case OP_DIVIDE:
2969 case OP_MODULO:
a0d0e21e
LW
2970 case OP_ADD:
2971 case OP_SUBTRACT:
2972 case OP_CONCAT:
2973 case OP_LEFT_SHIFT:
2974 case OP_RIGHT_SHIFT:
2975 case OP_BIT_AND:
2976 case OP_BIT_XOR:
2977 case OP_BIT_OR:
2978 case OP_I_MULTIPLY:
2979 case OP_I_DIVIDE:
2980 case OP_I_MODULO:
2981 case OP_I_ADD:
2982 case OP_I_SUBTRACT:
11343788 2983 if (!(o->op_flags & OPf_STACKED))
a0d0e21e 2984 goto nomod;
3280af22 2985 PL_modcount++;
a0d0e21e 2986 break;
b2ffa427 2987
82209a5d
FC
2988 case OP_REPEAT:
2989 if (o->op_flags & OPf_STACKED) {
2990 PL_modcount++;
2991 break;
2992 }
ff781254 2993 if (!(o->op_private & OPpREPEAT_DOLIST))
82209a5d
FC
2994 goto nomod;
2995 else {
2996 const I32 mods = PL_modcount;
ff781254
FC
2997 modkids(cBINOPo->op_first, type);
2998 if (type != OP_AASSIGN)
2999 goto nomod;
5e462669 3000 kid = cBINOPo->op_last;
82209a5d 3001 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
565e104c 3002 const IV iv = SvIV(kSVOP_sv);
82209a5d
FC
3003 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
3004 PL_modcount =
3005 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
3006 }
3007 else
3008 PL_modcount = RETURN_UNLIMITED_NUMBER;
3009 }
3010 break;
3011
79072805 3012 case OP_COND_EXPR:
ddeae0f1 3013 localize = 1;
e6dae479 3014 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
3ad73efd 3015 op_lvalue(kid, type);
79072805
LW
3016 break;
3017
3018 case OP_RV2AV:
3019 case OP_RV2HV:
11343788 3020 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
e6438c1a 3021 PL_modcount = RETURN_UNLIMITED_NUMBER;
11343788 3022 return o; /* Treat \(@foo) like ordinary list. */
748a9306 3023 }
924ba076 3024 /* FALLTHROUGH */
79072805 3025 case OP_RV2GV:
5dc0d613 3026 if (scalar_mod_type(o, type))
3fe9a6f1 3027 goto nomod;
11343788 3028 ref(cUNOPo->op_first, o->op_type);
924ba076 3029 /* FALLTHROUGH */
79072805
LW
3030 case OP_ASLICE:
3031 case OP_HSLICE:
ddeae0f1 3032 localize = 1;
924ba076 3033 /* FALLTHROUGH */
78f9721b 3034 case OP_AASSIGN:
32cbae3f
FC
3035 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
3036 if (type == OP_LEAVESUBLV && (
3037 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
3038 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3039 ))
631dbaa2 3040 o->op_private |= OPpMAYBE_LVSUB;
924ba076 3041 /* FALLTHROUGH */
93a17b20
LW
3042 case OP_NEXTSTATE:
3043 case OP_DBSTATE:
e6438c1a 3044 PL_modcount = RETURN_UNLIMITED_NUMBER;
79072805 3045 break;
5cae3edb 3046 case OP_KVHSLICE:
6dd3e0f2 3047 case OP_KVASLICE:
738155d2 3048 case OP_AKEYS:
5cae3edb
RZ
3049 if (type == OP_LEAVESUBLV)
3050 o->op_private |= OPpMAYBE_LVSUB;
3051 goto nomod;
cd642408
FC
3052 case OP_AVHVSWITCH:
3053 if (type == OP_LEAVESUBLV
3054 && (o->op_private & 3) + OP_EACH == OP_KEYS)
3055 o->op_private |= OPpMAYBE_LVSUB;
3056 goto nomod;
28c5b5bc
RGS
3057 case OP_AV2ARYLEN:
3058 PL_hints |= HINT_BLOCK_SCOPE;
3059 if (type == OP_LEAVESUBLV)
3060 o->op_private |= OPpMAYBE_LVSUB;
3061 PL_modcount++;
3062 break;
463ee0b2 3063 case OP_RV2SV:
aeea060c 3064 ref(cUNOPo->op_first, o->op_type);
ddeae0f1 3065 localize = 1;
924ba076 3066 /* FALLTHROUGH */
79072805 3067 case OP_GV:
3280af22 3068 PL_hints |= HINT_BLOCK_SCOPE;
924ba076 3069 /* FALLTHROUGH */
463ee0b2 3070 case OP_SASSIGN:
bf4b1e52
GS
3071 case OP_ANDASSIGN:
3072 case OP_ORASSIGN:
c963b151 3073 case OP_DORASSIGN:
ddeae0f1
DM
3074 PL_modcount++;
3075 break;
3076
8990e307 3077 case OP_AELEMFAST:
93bad3fd 3078 case OP_AELEMFAST_LEX:
6a077020 3079 localize = -1;
3280af22 3080 PL_modcount++;
8990e307
LW
3081 break;
3082
748a9306
LW
3083 case OP_PADAV:
3084 case OP_PADHV:
e6438c1a 3085 PL_modcount = RETURN_UNLIMITED_NUMBER;
5196be3e
MB
3086 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
3087 return o; /* Treat \(@foo) like ordinary list. */
3088 if (scalar_mod_type(o, type))
3fe9a6f1 3089 goto nomod;
32cbae3f
FC
3090 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3091 && type == OP_LEAVESUBLV)
78f9721b 3092 o->op_private |= OPpMAYBE_LVSUB;
924ba076 3093 /* FALLTHROUGH */
748a9306 3094 case OP_PADSV:
3280af22 3095 PL_modcount++;
ddeae0f1 3096 if (!type) /* local() */
ea9a9e77
FC
3097 Perl_croak(aTHX_ "Can't localize lexical variable %"PNf,
3098 PNfARG(PAD_COMPNAME(o->op_targ)));
e4211fee
FC
3099 if (!(o->op_private & OPpLVAL_INTRO)
3100 || ( type != OP_SASSIGN && type != OP_AASSIGN
3101 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
03414f05 3102 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
463ee0b2
LW
3103 break;
3104
748a9306 3105 case OP_PUSHMARK:
ddeae0f1 3106 localize = 0;
748a9306 3107 break;
b2ffa427 3108
69969c6f 3109 case OP_KEYS:
e4fc7082 3110 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
69969c6f 3111 goto nomod;
5d82c453
GA
3112 goto lvalue_func;
3113 case OP_SUBSTR:
3114 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
3115 goto nomod;
924ba076 3116 /* FALLTHROUGH */
a0d0e21e 3117 case OP_POS:
463ee0b2 3118 case OP_VEC:
fad4a2e4 3119 lvalue_func:
78f9721b
SM
3120 if (type == OP_LEAVESUBLV)
3121 o->op_private |= OPpMAYBE_LVSUB;
79409ac8
FC
3122 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
3123 /* substr and vec */
3124 /* If this op is in merely potential (non-fatal) modifiable
33a10326
FC
3125 context, then apply OP_ENTERSUB context to
3126 the kid op (to avoid croaking). Other-
79409ac8
FC
3127 wise pass this op’s own type so the correct op is mentioned
3128 in error messages. */
3129 op_lvalue(OpSIBLING(cBINOPo->op_first),
33a10326 3130 S_potential_mod_type(type)
ee4b19b9 3131 ? (I32)OP_ENTERSUB
33a10326 3132 : o->op_type);
79409ac8 3133 }
463ee0b2 3134 break;
a0d0e21e 3135
463ee0b2
LW
3136 case OP_AELEM:
3137 case OP_HELEM:
11343788 3138 ref(cBINOPo->op_first, o->op_type);
68dc0745 3139 if (type == OP_ENTERSUB &&
5dc0d613
MB
3140 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3141 o->op_private |= OPpLVAL_DEFER;
78f9721b
SM
3142 if (type == OP_LEAVESUBLV)
3143 o->op_private |= OPpMAYBE_LVSUB;
ddeae0f1 3144 localize = 1;
3280af22 3145 PL_modcount++;
463ee0b2
LW
3146 break;
3147
463ee0b2 3148 case OP_LEAVE:
a373464f 3149 case OP_LEAVELOOP:
2ec7f6f2 3150 o->op_private |= OPpLVALUE;
924ba076 3151 /* FALLTHROUGH */
2ec7f6f2 3152 case OP_SCOPE:
463ee0b2 3153 case OP_ENTER:
78f9721b 3154 case OP_LINESEQ:
ddeae0f1 3155 localize = 0;
11343788 3156 if (o->op_flags & OPf_KIDS)
3ad73efd 3157 op_lvalue(cLISTOPo->op_last, type);
a0d0e21e
LW
3158 break;
3159
3160 case OP_NULL:
ddeae0f1 3161 localize = 0;
638bc118
GS
3162 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3163 goto nomod;
3164 else if (!(o->op_flags & OPf_KIDS))
463ee0b2 3165 break;
11343788 3166 if (o->op_targ != OP_LIST) {
3ad73efd 3167 op_lvalue(cBINOPo->op_first, type);
a0d0e21e
LW
3168 break;
3169 }
924ba076 3170 /* FALLTHROUGH */
463ee0b2 3171 case OP_LIST:
ddeae0f1 3172 localize = 0;
e6dae479 3173 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
5c906035
GG
3174 /* elements might be in void context because the list is
3175 in scalar context or because they are attribute sub calls */
3176 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
3177 op_lvalue(kid, type);
463ee0b2 3178 break;
78f9721b 3179
1efec5ed
FC
3180 case OP_COREARGS:
3181 return o;
2ec7f6f2
FC
3182
3183 case OP_AND:
3184 case OP_OR:
375879aa
FC
3185 if (type == OP_LEAVESUBLV
3186 || !S_vivifies(cLOGOPo->op_first->op_type))
3187 op_lvalue(cLOGOPo->op_first, type);
3188 if (type == OP_LEAVESUBLV
e6dae479
FC
3189 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3190 op_lvalue(OpSIBLING(cLOGOPo->op_first), type);
2ec7f6f2 3191 goto nomod;
26a50d99
FC
3192
3193 case OP_SREFGEN:
e118fea3
FC
3194 if (type == OP_NULL) { /* local */
3195 local_refgen:
3196 op_lvalue(cUNOPo->op_first, OP_NULL);
3197 return o;
3198 }
d39c26a6
FC
3199 if (type != OP_AASSIGN && type != OP_SASSIGN
3200 && type != OP_ENTERLOOP)
3201 goto nomod;
7664512e 3202 /* Don’t bother applying lvalue context to the ex-list. */
26a50d99 3203 kid = cUNOPx(cUNOPo->op_first)->op_first;
e6dae479 3204 assert (!OpHAS_SIBLING(kid));
217e3565
FC
3205 goto kid_2lvref;
3206 case OP_REFGEN:
e118fea3
FC
3207 if (type == OP_NULL) /* local */
3208 goto local_refgen;
217e3565 3209 if (type != OP_AASSIGN) goto nomod;
7664512e
FC
3210 kid = cUNOPo->op_first;
3211 kid_2lvref:
3212 {
3213 const U8 ec = PL_parser ? PL_parser->error_count : 0;
63702de8 3214 S_lvref(aTHX_ kid, type);
7664512e 3215 if (!PL_parser || PL_parser->error_count == ec) {
baabe3fb 3216 if (!FEATURE_REFALIASING_IS_ENABLED)
7664512e 3217 Perl_croak(aTHX_
baabe3fb 3218 "Experimental aliasing via reference not enabled");
7664512e 3219 Perl_ck_warner_d(aTHX_
baabe3fb
FC
3220 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3221 "Aliasing via reference is experimental");
7664512e
FC
3222 }
3223 }
217e3565
FC
3224 if (o->op_type == OP_REFGEN)
3225 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3226 op_null(o);
26a50d99 3227 return o;
e4e95921
FC
3228
3229 case OP_SPLIT:
3230 kid = cLISTOPo->op_first;
3231 if (kid && kid->op_type == OP_PUSHRE &&
3232 ( kid->op_targ
3233 || o->op_flags & OPf_STACKED
3234#ifdef USE_ITHREADS
3235 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
3236#else
3237 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
3238#endif
3239 )) {
3240 /* This is actually @array = split. */
3241 PL_modcount = RETURN_UNLIMITED_NUMBER;
3242 break;
3243 }
3244 goto nomod;
569ddb4a
FC
3245
3246 case OP_SCALAR:
3247 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3248 goto nomod;
463ee0b2 3249 }
58d95175 3250
8be1be90
AMS
3251 /* [20011101.069] File test operators interpret OPf_REF to mean that
3252 their argument is a filehandle; thus \stat(".") should not set
3253 it. AMS 20011102 */
3254 if (type == OP_REFGEN &&
ef69c8fc 3255 PL_check[o->op_type] == Perl_ck_ftst)
8be1be90
AMS
3256 return o;
3257
3258 if (type != OP_LEAVESUBLV)
3259 o->op_flags |= OPf_MOD;
3260
3261 if (type == OP_AASSIGN || type == OP_SASSIGN)
3262 o->op_flags |= OPf_SPECIAL|OPf_REF;
ddeae0f1
DM
3263 else if (!type) { /* local() */
3264 switch (localize) {
3265 case 1:
3266 o->op_private |= OPpLVAL_INTRO;
3267 o->op_flags &= ~OPf_SPECIAL;
3268 PL_hints |= HINT_BLOCK_SCOPE;
3269 break;
3270 case 0:
3271 break;
3272 case -1:
a2a5de95
NC
3273 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3274 "Useless localization of %s", OP_DESC(o));
ddeae0f1 3275 }
463ee0b2 3276 }
8be1be90
AMS
3277 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3278 && type != OP_LEAVESUBLV)
3279 o->op_flags |= OPf_REF;
11343788 3280 return o;
463ee0b2
LW
3281}
3282
864dbfa3 3283STATIC bool
5f66b61c 3284S_scalar_mod_type(const OP *o, I32 type)
3fe9a6f1
PP
3285{
3286 switch (type) {
32a60974 3287 case OP_POS:
3fe9a6f1 3288 case OP_SASSIGN:
1efec5ed 3289 if (o && o->op_type == OP_RV2GV)
3fe9a6f1 3290 return FALSE;
924ba076 3291 /* FALLTHROUGH */
3fe9a6f1
PP
3292 case OP_PREINC:
3293 case OP_PREDEC:
3294 case OP_POSTINC:
3295 case OP_POSTDEC:
3296 case OP_I_PREINC:
3297 case OP_I_PREDEC:
3298 case OP_I_POSTINC:
3299 case OP_I_POSTDEC:
3300 case OP_POW:
3301 case OP_MULTIPLY:
3302 case OP_DIVIDE:
3303 case OP_MODULO:
3304 case OP_REPEAT:
3305 case OP_ADD:
3306 case OP_SUBTRACT:
3307 case OP_I_MULTIPLY:
3308 case OP_I_DIVIDE:
3309 case OP_I_MODULO:
3310 case OP_I_ADD:
3311 case OP_I_SUBTRACT:
3312 case OP_LEFT_SHIFT:
3313 case OP_RIGHT_SHIFT:
3314 case OP_BIT_AND:
3315 case OP_BIT_XOR:
3316 case OP_BIT_OR:
76734a32
FC
3317 case OP_NBIT_AND:
3318 case OP_NBIT_XOR:
3319 case OP_NBIT_OR:
3320 case OP_SBIT_AND:
3321 case OP_SBIT_XOR:
3322 case OP_SBIT_OR:
3fe9a6f1
PP
3323 case OP_CONCAT:
3324 case OP_SUBST:
3325 case OP_TRANS:
bb16bae8 3326 case OP_TRANSR:
49e9fbe6
GS
3327 case OP_READ:
3328 case OP_SYSREAD:
3329 case OP_RECV:
bf4b1e52
GS
3330 case OP_ANDASSIGN:
3331 case OP_ORASSIGN:
410d09fe 3332 case OP_DORASSIGN:
79409ac8
FC
3333 case OP_VEC:
3334 case OP_SUBSTR:
3fe9a6f1
PP
3335 return TRUE;
3336 default:
3337 return FALSE;
3338 }
3339}
3340
35cd451c 3341STATIC bool
5f66b61c 3342S_is_handle_constructor(const OP *o, I32 numargs)
35cd451c 3343{
7918f24d
NC
3344 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3345
35cd451c
GS
3346 switch (o->op_type) {
3347 case OP_PIPE_OP:
3348 case OP_SOCKPAIR:
504618e9 3349 if (numargs == 2)
35cd451c 3350 return TRUE;
924ba076 3351 /* FALLTHROUGH */
35cd451c
GS
3352 case OP_SYSOPEN:
3353 case OP_OPEN:
ded8aa31 3354 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
35cd451c
GS
3355 case OP_SOCKET:
3356 case OP_OPEN_DIR:
3357 case OP_ACCEPT:
504618e9 3358 if (numargs == 1)
35cd451c 3359 return TRUE;
5f66b61c 3360 /* FALLTHROUGH */
35cd451c
GS
3361 default:
3362 return FALSE;
3363 }
3364}
3365
0d86688d
NC
3366static OP *
3367S_refkids(pTHX_ OP *o, I32 type)
463ee0b2 3368{
11343788 3369 if (o && o->op_flags & OPf_KIDS) {
6867be6d 3370 OP *kid;
e6dae479 3371 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
463ee0b2
LW
3372 ref(kid, type);
3373 }
11343788 3374 return o;
463ee0b2
LW
3375}
3376
3377OP *
e4c5ccf3 3378Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
463ee0b2 3379{
27da23d5 3380 dVAR;
463ee0b2 3381 OP *kid;
463ee0b2 3382
7918f24d
NC
3383 PERL_ARGS_ASSERT_DOREF;
3384
3dc78631 3385 if (PL_parser && PL_parser->error_count)
11343788 3386 return o;
463ee0b2 3387
11343788 3388 switch (o->op_type) {
a0d0e21e 3389 case OP_ENTERSUB:
f4df43b5 3390 if ((type == OP_EXISTS || type == OP_DEFINED) &&
11343788 3391 !(o->op_flags & OPf_STACKED)) {
b9a07097 3392 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
11343788 3393 assert(cUNOPo->op_first->op_type == OP_NULL);
93c66552 3394 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
11343788 3395 o->op_flags |= OPf_SPECIAL;
8990e307 3396 }
767eda44 3397 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
0e9700df
GG
3398 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3399 : type == OP_RV2HV ? OPpDEREF_HV
3400 : OPpDEREF_SV);
767eda44
FC
3401 o->op_flags |= OPf_MOD;
3402 }
3403
8990e307 3404 break;
aeea060c 3405
463ee0b2 3406 case OP_COND_EXPR:
e6dae479 3407 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid))
e4c5ccf3 3408 doref(kid, type, set_op_ref);
463ee0b2 3409 break;
8990e307 3410 case OP_RV2SV:
35cd451c
GS
3411 if (type == OP_DEFINED)
3412 o->op_flags |= OPf_SPECIAL; /* don't create GV */
e4c5ccf3 3413 doref(cUNOPo->op_first, o->op_type, set_op_ref);
924ba076 3414 /* FALLTHROUGH */
4633a7c4 3415 case OP_PADSV:
5f05dabc 3416 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
3417 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3418 : type == OP_RV2HV ? OPpDEREF_HV
3419 : OPpDEREF_SV);
11343788 3420 o->op_flags |= OPf_MOD;
a0d0e21e 3421 }
8990e307 3422 break;
1c846c1f 3423
463ee0b2
LW
3424 case OP_RV2AV:
3425 case OP_RV2HV:
e4c5ccf3
RH
3426 if (set_op_ref)
3427 o->op_flags |= OPf_REF;
924ba076 3428 /* FALLTHROUGH */
463ee0b2 3429 case OP_RV2GV:
35cd451c
GS
3430 if (type == OP_DEFINED)
3431 o->op_flags |= OPf_SPECIAL; /* don't create GV */
e4c5ccf3 3432 doref(cUNOPo->op_first, o->op_type, set_op_ref);
463ee0b2 3433 break;
8990e307 3434
463ee0b2
LW
3435 case OP_PADAV:
3436 case OP_PADHV:
e4c5ccf3
RH
3437 if (set_op_ref)
3438 o->op_flags |= OPf_REF;
79072805 3439 break;
aeea060c 3440
8990e307 3441 case OP_SCALAR:
79072805 3442 case OP_NULL:
518618af 3443 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
463ee0b2 3444 break;
e4c5ccf3 3445 doref(cBINOPo->op_first, type, set_op_ref);
79072805
LW
3446 break;
3447 case OP_AELEM:
3448 case OP_HELEM:
e4c5ccf3 3449 doref(cBINOPo->op_first, o->op_type, set_op_ref);
5f05dabc 3450 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
3451 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3452 : type == OP_RV2HV ? OPpDEREF_HV
3453 : OPpDEREF_SV);
11343788 3454 o->op_flags |= OPf_MOD;
8990e307 3455 }
79072805
LW
3456 break;
3457
463ee0b2 3458 case OP_SCOPE:
79072805 3459 case OP_LEAVE:
e4c5ccf3 3460 set_op_ref = FALSE;
924ba076 3461 /* FALLTHROUGH */
79072805 3462 case OP_ENTER:
8990e307 3463 case OP_LIST:
11343788 3464 if (!(o->op_flags & OPf_KIDS))
79072805 3465 break;
e4c5ccf3 3466 doref(cLISTOPo->op_last, type, set_op_ref);
79072805 3467 break;
a0d0e21e
LW
3468 default:
3469 break;
79072805 3470 }
11343788 3471 return scalar(o);
8990e307 3472
79072805
LW
3473}
3474
09bef843
SB
3475STATIC OP *
3476S_dup_attrlist(pTHX_ OP *o)
3477{
0bd48802 3478 OP *rop;
09bef843 3479
7918f24d
NC
3480 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3481
09bef843
SB
3482 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3483 * where the first kid is OP_PUSHMARK and the remaining ones
3484 * are OP_CONST. We need to push the OP_CONST values.
3485 */
3486 if (o->op_type == OP_CONST)
b37c2d43 3487 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
09bef843
SB
3488 else {
3489 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
5f66b61c 3490 rop = NULL;
e6dae479 3491 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
09bef843 3492 if (o->op_type == OP_CONST)
2fcb4757 3493 rop = op_append_elem(OP_LIST, rop,
09bef843 3494 newSVOP(OP_CONST, o->op_flags,
b37c2d43 3495 SvREFCNT_inc_NN(cSVOPo->op_sv)));
09bef843
SB
3496 }
3497 }
3498 return rop;
3499}
3500
3501STATIC void
ad0dc73b 3502S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
09bef843 3503{
7918f24d 3504 PERL_ARGS_ASSERT_APPLY_ATTRS;
976258ec
JH
3505 {
3506 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
7918f24d 3507
976258ec 3508 /* fake up C<use attributes $pkg,$rv,@attrs> */
e4783991 3509
09bef843 3510#define ATTRSMODULE "attributes"
95f0a2f1
SB
3511#define ATTRSMODULE_PM "attributes.pm"
3512
976258ec
JH
3513 Perl_load_module(
3514 aTHX_ PERL_LOADMOD_IMPORT_OPS,
3515 newSVpvs(ATTRSMODULE),
3516 NULL,
3517 op_prepend_elem(OP_LIST,
3518 newSVOP(OP_CONST, 0, stashsv),
3519 op_prepend_elem(OP_LIST,
3520 newSVOP(OP_CONST, 0,
3521 newRV(target)),
3522 dup_attrlist(attrs))));
3523 }
09bef843
SB
3524}
3525
95f0a2f1
SB
3526STATIC void
3527S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3528{
3529 OP *pack, *imop, *arg;
ad0dc73b 3530 SV *meth, *stashsv, **svp;
95f0a2f1 3531
7918f24d
NC
3532 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3533
95f0a2f1
SB
3534 if (!attrs)
3535 return;
3536
3537 assert(target->op_type == OP_PADSV ||
3538 target->op_type == OP_PADHV ||
3539 target->op_type == OP_PADAV);
3540
3541 /* Ensure that attributes.pm is loaded. */
ad0dc73b
FC
3542 /* Don't force the C<use> if we don't need it. */
3543 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3544 if (svp && *svp != &PL_sv_undef)
3545 NOOP; /* already in %INC */
3546 else
3547 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3548 newSVpvs(ATTRSMODULE), NULL);
95f0a2f1
SB
3549
3550 /* Need package name for method call. */
6136c704 3551 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
95f0a2f1
SB
3552
3553 /* Build up the real arg-list. */
976258ec 3554 stashsv = newSVhek(HvNAME_HEK(stash));
5aaec2b4 3555
95f0a2f1
SB
3556 arg = newOP(OP_PADSV, 0);
3557 arg->op_targ = target->op_targ;
2fcb4757 3558 arg = op_prepend_elem(OP_LIST,
95f0a2f1 3559 newSVOP(OP_CONST, 0, stashsv),
2fcb4757 3560 op_prepend_elem(OP_LIST,
95f0a2f1 3561 newUNOP(OP_REFGEN, 0,
a282984d 3562 arg),
95f0a2f1
SB
3563 dup_attrlist(attrs)));
3564
3565 /* Fake up a method call to import */
18916d0d 3566 meth = newSVpvs_share("import");
03d05f6e 3567 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2fcb4757 3568 op_append_elem(OP_LIST,
6aa68307 3569 op_prepend_elem(OP_LIST, pack, arg),
b46e009d 3570 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
95f0a2f1
SB
3571
3572 /* Combine the ops. */
2fcb4757 3573 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
95f0a2f1
SB
3574}
3575
3576/*
3577=notfor apidoc apply_attrs_string
3578
3579Attempts to apply a list of attributes specified by the C<attrstr> and
3580C<len> arguments to the subroutine identified by the C<cv> argument which
3581is expected to be associated with the package identified by the C<stashpv>
3582argument (see L<attributes>). It gets this wrong, though, in that it
3583does not correctly identify the boundaries of the individual attribute
3584specifications within C<attrstr>. This is not really intended for the
3585public API, but has to be listed here for systems such as AIX which
3586need an explicit export list for symbols. (It's called from XS code
3587in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3588to respect attribute syntax properly would be welcome.
3589
3590=cut
3591*/
3592
be3174d2 3593void
6867be6d
AL
3594Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3595 const char *attrstr, STRLEN len)
be3174d2 3596{
5f66b61c 3597 OP *attrs = NULL;
be3174d2 3598
7918f24d
NC
3599 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3600
be3174d2
GS
3601 if (!len) {
3602 len = strlen(attrstr);
3603 }
3604
3605 while (len) {
3606 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3607 if (len) {
890ce7af 3608 const char * const sstr = attrstr;
be3174d2 3609 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2fcb4757 3610 attrs = op_append_elem(OP_LIST, attrs,
be3174d2
GS
3611 newSVOP(OP_CONST, 0,
3612 newSVpvn(sstr, attrstr-sstr)));
3613 }
3614 }
3615
3616 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
6136c704 3617 newSVpvs(ATTRSMODULE),
2fcb4757 3618 NULL, op_prepend_elem(OP_LIST,
be3174d2 3619 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2fcb4757 3620 op_prepend_elem(OP_LIST,
be3174d2 3621 newSVOP(OP_CONST, 0,
ad64d0ec 3622 newRV(MUTABLE_SV(cv))),
be3174d2
GS
3623 attrs)));
3624}
3625
eedb00fa
PM
3626STATIC void
3627S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3628{
3629 OP *new_proto = NULL;
3630 STRLEN pvlen;
3631 char *pv;
3632 OP *o;
3633
3634 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3635
3636 if (!*attrs)
3637 return;
3638
3639 o = *attrs;
3640 if (o->op_type == OP_CONST) {
3641 pv = SvPV(cSVOPo_sv, pvlen);
3642 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3643 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3644 SV ** const tmpo = cSVOPx_svp(o);
3645 SvREFCNT_dec(cSVOPo_sv);
3646 *tmpo = tmpsv;
3647 new_proto = o;
3648 *attrs = NULL;
3649 }
3650 } else if (o->op_type == OP_LIST) {
e78bc664 3651 OP * lasto;
eedb00fa 3652 assert(o->op_flags & OPf_KIDS);
e78bc664
PM
3653 lasto = cLISTOPo->op_first;
3654 assert(lasto->op_type == OP_PUSHMARK);
e6dae479 3655 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
eedb00fa
PM
3656 if (o->op_type == OP_CONST) {
3657 pv = SvPV(cSVOPo_sv, pvlen);
3658 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3659 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3660 SV ** const tmpo = cSVOPx_svp(o);
3661 SvREFCNT_dec(cSVOPo_sv);
3662 *tmpo = tmpsv;
3663 if (new_proto && ckWARN(WARN_MISC)) {
3664 STRLEN new_len;
3665 const char * newp = SvPV(cSVOPo_sv, new_len);
3666 Perl_warner(aTHX_ packWARN(WARN_MISC),
3667 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
3668 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3669 op_free(new_proto);
3670 }
3671 else if (new_proto)
3672 op_free(new_proto);
3673 new_proto = o;
3253bf85
DM
3674 /* excise new_proto from the list */
3675 op_sibling_splice(*attrs, lasto, 1, NULL);
3676 o = lasto;
eedb00fa
PM
3677 continue;
3678 }
3679 }
3680 lasto = o;
3681 }
3682 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3683 would get pulled in with no real need */
e6dae479 3684 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
eedb00fa
PM
3685 op_free(*attrs);
3686 *attrs = NULL;
3687 }
3688 }
3689
3690 if (new_proto) {
3691 SV *svname;
3692 if (isGV(name)) {
3693 svname = sv_newmortal();
3694 gv_efullname3(svname, name, NULL);
3695 }
3696 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3697 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3698 else
3699 svname = (SV *)name;
3700 if (ckWARN(WARN_ILLEGALPROTO))
3701 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3702 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3703 STRLEN old_len, new_len;
3704 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3705 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3706
3707 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3708 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
3709 " in %"SVf,
3710 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3711 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3712 SVfARG(svname));
3713 }
3714 if (*proto)
3715 op_free(*proto);
3716 *proto = new_proto;
3717 }
3718}
3719