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