4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
38 * newBINOP(OP_ADD, flags,
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
52 An execution-order pass
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines. The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order. (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again). As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node. But
70 it's still not the real execution order.
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer. At that point, we can call
75 into peep() to do that code's portion of the 3rd pass. It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80 get the compile time state of %^H for that block. Storing %^H in every
81 block (or even COP) would be very expensive, so a different approach is
82 taken. The (running) state of %^H is serialised into a tree of HE-like
83 structs. Stores into %^H are chained onto the current leaf as a struct
84 refcounted_he * with the key and the value. Deletes from %^H are saved
85 with a value of PL_sv_placeholder. The state of %^H at any point can be
86 turned back into a regular HV by walking back up the tree from that point's
87 leaf, ignoring any key you've already seen (placeholder or not), storing
88 the rest into the HV structure, then removing the placeholders. Hence
89 memory is only used to store the %^H deltas from the enclosing COP, rather
90 than the entire %^H on each COP.
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98 it will be correctly restored when any inner compiling scope is exited.
104 #include "keywords.h"
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
112 /* remove any leading "empty" ops from the op_next chain whose first
113 * node's address is stored in op_p. Store the updated address of the
114 * first node in op_p.
118 S_prune_chain_head(pTHX_ OP** op_p)
121 && ( (*op_p)->op_type == OP_NULL
122 || (*op_p)->op_type == OP_SCOPE
123 || (*op_p)->op_type == OP_SCALAR
124 || (*op_p)->op_type == OP_LINESEQ)
126 *op_p = (*op_p)->op_next;
130 /* See the explanatory comments above struct opslab in op.h. */
132 #ifdef PERL_DEBUG_READONLY_OPS
133 # define PERL_SLAB_SIZE 128
134 # define PERL_MAX_SLAB_SIZE 4096
135 # include <sys/mman.h>
138 #ifndef PERL_SLAB_SIZE
139 # define PERL_SLAB_SIZE 64
141 #ifndef PERL_MAX_SLAB_SIZE
142 # define PERL_MAX_SLAB_SIZE 2048
145 /* rounds up to nearest pointer */
146 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
147 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
150 S_new_slab(pTHX_ size_t sz)
152 #ifdef PERL_DEBUG_READONLY_OPS
153 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
154 PROT_READ|PROT_WRITE,
155 MAP_ANON|MAP_PRIVATE, -1, 0);
156 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
157 (unsigned long) sz, slab));
158 if (slab == MAP_FAILED) {
159 perror("mmap failed");
162 slab->opslab_size = (U16)sz;
164 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
166 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
170 /* requires double parens and aTHX_ */
171 #define DEBUG_S_warn(args) \
173 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
177 Perl_Slab_Alloc(pTHX_ size_t sz)
186 /* We only allocate ops from the slab during subroutine compilation.
187 We find the slab via PL_compcv, hence that must be non-NULL. It could
188 also be pointing to a subroutine which is now fully set up (CvROOT()
189 pointing to the top of the optree for that sub), or a subroutine
190 which isn't using the slab allocator. If our sanity checks aren't met,
191 don't use a slab, but allocate the OP directly from the heap. */
192 if (!PL_compcv || CvROOT(PL_compcv)
193 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
194 return PerlMemShared_calloc(1, sz);
196 /* While the subroutine is under construction, the slabs are accessed via
197 CvSTART(), to avoid needing to expand PVCV by one pointer for something
198 unneeded at runtime. Once a subroutine is constructed, the slabs are
199 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
200 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
202 if (!CvSTART(PL_compcv)) {
204 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
205 CvSLABBED_on(PL_compcv);
206 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
208 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
210 opsz = SIZE_TO_PSIZE(sz);
211 sz = opsz + OPSLOT_HEADER_P;
213 /* The slabs maintain a free list of OPs. In particular, constant folding
214 will free up OPs, so it makes sense to re-use them where possible. A
215 freed up slot is used in preference to a new allocation. */
216 if (slab->opslab_freed) {
217 OP **too = &slab->opslab_freed;
219 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", o, slab));
220 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
221 DEBUG_S_warn((aTHX_ "Alas! too small"));
222 o = *(too = &o->op_next);
223 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", o)); }
227 Zero(o, opsz, I32 *);
233 #define INIT_OPSLOT \
234 slot->opslot_slab = slab; \
235 slot->opslot_next = slab2->opslab_first; \
236 slab2->opslab_first = slot; \
237 o = &slot->opslot_op; \
240 /* The partially-filled slab is next in the chain. */
241 slab2 = slab->opslab_next ? slab->opslab_next : slab;
242 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
243 /* Remaining space is too small. */
245 /* If we can fit a BASEOP, add it to the free chain, so as not
247 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
248 slot = &slab2->opslab_slots;
250 o->op_type = OP_FREED;
251 o->op_next = slab->opslab_freed;
252 slab->opslab_freed = o;
255 /* Create a new slab. Make this one twice as big. */
256 slot = slab2->opslab_first;
257 while (slot->opslot_next) slot = slot->opslot_next;
258 slab2 = S_new_slab(aTHX_
259 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
261 : (DIFF(slab2, slot)+1)*2);
262 slab2->opslab_next = slab->opslab_next;
263 slab->opslab_next = slab2;
265 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
267 /* Create a new op slot */
268 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
269 assert(slot >= &slab2->opslab_slots);
270 if (DIFF(&slab2->opslab_slots, slot)
271 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
272 slot = &slab2->opslab_slots;
274 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", o, slab));
280 #ifdef PERL_DEBUG_READONLY_OPS
282 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
284 PERL_ARGS_ASSERT_SLAB_TO_RO;
286 if (slab->opslab_readonly) return;
287 slab->opslab_readonly = 1;
288 for (; slab; slab = slab->opslab_next) {
289 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
290 (unsigned long) slab->opslab_size, slab));*/
291 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
292 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
293 (unsigned long)slab->opslab_size, errno);
298 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
302 PERL_ARGS_ASSERT_SLAB_TO_RW;
304 if (!slab->opslab_readonly) return;
306 for (; slab2; slab2 = slab2->opslab_next) {
307 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
308 (unsigned long) size, slab2));*/
309 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
310 PROT_READ|PROT_WRITE)) {
311 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
312 (unsigned long)slab2->opslab_size, errno);
315 slab->opslab_readonly = 0;
319 # define Slab_to_rw(op) NOOP
322 /* This cannot possibly be right, but it was copied from the old slab
323 allocator, to which it was originally added, without explanation, in
326 # define PerlMemShared PerlMem
330 Perl_Slab_Free(pTHX_ void *op)
333 OP * const o = (OP *)op;
336 PERL_ARGS_ASSERT_SLAB_FREE;
338 if (!o->op_slabbed) {
340 PerlMemShared_free(op);
345 /* If this op is already freed, our refcount will get screwy. */
346 assert(o->op_type != OP_FREED);
347 o->op_type = OP_FREED;
348 o->op_next = slab->opslab_freed;
349 slab->opslab_freed = o;
350 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", o, slab));
351 OpslabREFCNT_dec_padok(slab);
355 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
358 const bool havepad = !!PL_comppad;
359 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
362 PAD_SAVE_SETNULLPAD();
369 Perl_opslab_free(pTHX_ OPSLAB *slab)
373 PERL_ARGS_ASSERT_OPSLAB_FREE;
374 DEBUG_S_warn((aTHX_ "freeing slab %p", slab));
375 assert(slab->opslab_refcnt == 1);
376 for (; slab; slab = slab2) {
377 slab2 = slab->opslab_next;
379 slab->opslab_refcnt = ~(size_t)0;
381 #ifdef PERL_DEBUG_READONLY_OPS
382 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
384 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
385 perror("munmap failed");
389 PerlMemShared_free(slab);
395 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
400 size_t savestack_count = 0;
402 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
405 for (slot = slab2->opslab_first;
407 slot = slot->opslot_next) {
408 if (slot->opslot_op.op_type != OP_FREED
409 && !(slot->opslot_op.op_savefree
415 assert(slot->opslot_op.op_slabbed);
416 op_free(&slot->opslot_op);
417 if (slab->opslab_refcnt == 1) goto free;
420 } while ((slab2 = slab2->opslab_next));
421 /* > 1 because the CV still holds a reference count. */
422 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
424 assert(savestack_count == slab->opslab_refcnt-1);
426 /* Remove the CV’s reference count. */
427 slab->opslab_refcnt--;
434 #ifdef PERL_DEBUG_READONLY_OPS
436 Perl_op_refcnt_inc(pTHX_ OP *o)
439 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
440 if (slab && slab->opslab_readonly) {
453 Perl_op_refcnt_dec(pTHX_ OP *o)
456 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
458 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
460 if (slab && slab->opslab_readonly) {
462 result = --o->op_targ;
465 result = --o->op_targ;
471 * In the following definition, the ", (OP*)0" is just to make the compiler
472 * think the expression is of the right type: croak actually does a Siglongjmp.
474 #define CHECKOP(type,o) \
475 ((PL_op_mask && PL_op_mask[type]) \
476 ? ( op_free((OP*)o), \
477 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
479 : PL_check[type](aTHX_ (OP*)o))
481 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
483 #define CHANGE_TYPE(o,type) \
485 o->op_type = (OPCODE)type; \
486 o->op_ppaddr = PL_ppaddr[type]; \
490 S_gv_ename(pTHX_ GV *gv)
492 SV* const tmpsv = sv_newmortal();
494 PERL_ARGS_ASSERT_GV_ENAME;
496 gv_efullname3(tmpsv, gv, NULL);
501 S_no_fh_allowed(pTHX_ OP *o)
503 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
505 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
511 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
513 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
514 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, namesv),
515 SvUTF8(namesv) | flags);
520 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
522 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
523 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
528 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
530 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
532 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
537 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
539 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
541 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
542 SvUTF8(namesv) | flags);
547 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
549 PERL_ARGS_ASSERT_BAD_TYPE_PV;
551 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
552 (int)n, name, t, OP_DESC(kid)), flags);
556 S_bad_type_gv(pTHX_ I32 n, const char *t, GV *gv, U32 flags, const OP *kid)
558 SV * const namesv = gv_ename(gv);
559 PERL_ARGS_ASSERT_BAD_TYPE_GV;
561 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
562 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
566 S_no_bareword_allowed(pTHX_ OP *o)
568 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
571 return; /* various ok barewords are hidden in extra OP_NULL */
572 qerror(Perl_mess(aTHX_
573 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
575 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
578 /* "register" allocation */
581 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
585 const bool is_our = (PL_parser->in_my == KEY_our);
587 PERL_ARGS_ASSERT_ALLOCMY;
589 if (flags & ~SVf_UTF8)
590 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
593 /* Until we're using the length for real, cross check that we're being
595 assert(strlen(name) == len);
597 /* complain about "my $<special_var>" etc etc */
601 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
602 (name[1] == '_' && (*name == '$' || len > 2))))
604 /* name[2] is true if strlen(name) > 2 */
605 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
606 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
607 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
608 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
609 PL_parser->in_my == KEY_state ? "state" : "my"));
611 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
612 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
615 else if (len == 2 && name[1] == '_' && !is_our)
616 /* diag_listed_as: Use of my $_ is experimental */
617 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
618 "Use of %s $_ is experimental",
619 PL_parser->in_my == KEY_state
623 /* allocate a spare slot and store the name in that slot */
625 off = pad_add_name_pvn(name, len,
626 (is_our ? padadd_OUR :
627 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
628 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
629 PL_parser->in_my_stash,
631 /* $_ is always in main::, even with our */
632 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
636 /* anon sub prototypes contains state vars should always be cloned,
637 * otherwise the state var would be shared between anon subs */
639 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
640 CvCLONE_on(PL_compcv);
646 =for apidoc alloccopstash
648 Available only under threaded builds, this function allocates an entry in
649 C<PL_stashpad> for the stash passed to it.
656 Perl_alloccopstash(pTHX_ HV *hv)
658 PADOFFSET off = 0, o = 1;
659 bool found_slot = FALSE;
661 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
663 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
665 for (; o < PL_stashpadmax; ++o) {
666 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
667 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
668 found_slot = TRUE, off = o;
671 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
672 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
673 off = PL_stashpadmax;
674 PL_stashpadmax += 10;
677 PL_stashpad[PL_stashpadix = off] = hv;
682 /* free the body of an op without examining its contents.
683 * Always use this rather than FreeOp directly */
686 S_op_destroy(pTHX_ OP *o)
694 =for apidoc Am|void|op_free|OP *o
696 Free an op. Only use this when an op is no longer linked to from any
703 Perl_op_free(pTHX_ OP *o)
708 /* Though ops may be freed twice, freeing the op after its slab is a
710 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
711 /* During the forced freeing of ops after compilation failure, kidops
712 may be freed before their parents. */
713 if (!o || o->op_type == OP_FREED)
717 if (o->op_private & OPpREFCOUNTED) {
728 refcnt = OpREFCNT_dec(o);
731 /* Need to find and remove any pattern match ops from the list
732 we maintain for reset(). */
733 find_and_forget_pmops(o);
743 /* Call the op_free hook if it has been set. Do it now so that it's called
744 * at the right time for refcounted ops, but still before all of the kids
748 if (o->op_flags & OPf_KIDS) {
750 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
751 nextkid = kid->op_sibling; /* Get before next freeing kid */
756 type = (OPCODE)o->op_targ;
759 Slab_to_rw(OpSLAB(o));
761 /* COP* is not cleared by op_clear() so that we may track line
762 * numbers etc even after null() */
763 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
769 #ifdef DEBUG_LEAKING_SCALARS
776 Perl_op_clear(pTHX_ OP *o)
781 PERL_ARGS_ASSERT_OP_CLEAR;
784 mad_free(o->op_madprop);
789 switch (o->op_type) {
790 case OP_NULL: /* Was holding old type, if any. */
791 if (PL_madskills && o->op_targ != OP_NULL) {
792 o->op_type = (Optype)o->op_targ;
797 case OP_ENTEREVAL: /* Was holding hints. */
801 if (!(o->op_flags & OPf_REF)
802 || (PL_check[o->op_type] != Perl_ck_ftst))
809 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
814 /* It's possible during global destruction that the GV is freed
815 before the optree. Whilst the SvREFCNT_inc is happy to bump from
816 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
817 will trigger an assertion failure, because the entry to sv_clear
818 checks that the scalar is not already freed. A check of for
819 !SvIS_FREED(gv) turns out to be invalid, because during global
820 destruction the reference count can be forced down to zero
821 (with SVf_BREAK set). In which case raising to 1 and then
822 dropping to 0 triggers cleanup before it should happen. I
823 *think* that this might actually be a general, systematic,
824 weakness of the whole idea of SVf_BREAK, in that code *is*
825 allowed to raise and lower references during global destruction,
826 so any *valid* code that happens to do this during global
827 destruction might well trigger premature cleanup. */
828 bool still_valid = gv && SvREFCNT(gv);
831 SvREFCNT_inc_simple_void(gv);
833 if (cPADOPo->op_padix > 0) {
834 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
835 * may still exist on the pad */
836 pad_swipe(cPADOPo->op_padix, TRUE);
837 cPADOPo->op_padix = 0;
840 SvREFCNT_dec(cSVOPo->op_sv);
841 cSVOPo->op_sv = NULL;
844 int try_downgrade = SvREFCNT(gv) == 2;
847 gv_try_downgrade(gv);
851 case OP_METHOD_NAMED:
854 SvREFCNT_dec(cSVOPo->op_sv);
855 cSVOPo->op_sv = NULL;
858 Even if op_clear does a pad_free for the target of the op,
859 pad_free doesn't actually remove the sv that exists in the pad;
860 instead it lives on. This results in that it could be reused as
861 a target later on when the pad was reallocated.
864 pad_swipe(o->op_targ,1);
874 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
879 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
880 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
882 if (cPADOPo->op_padix > 0) {
883 pad_swipe(cPADOPo->op_padix, TRUE);
884 cPADOPo->op_padix = 0;
887 SvREFCNT_dec(cSVOPo->op_sv);
888 cSVOPo->op_sv = NULL;
892 PerlMemShared_free(cPVOPo->op_pv);
893 cPVOPo->op_pv = NULL;
897 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
901 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
902 /* No GvIN_PAD_off here, because other references may still
903 * exist on the pad */
904 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
907 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
913 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
914 op_free(cPMOPo->op_code_list);
915 cPMOPo->op_code_list = NULL;
917 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
918 /* we use the same protection as the "SAFE" version of the PM_ macros
919 * here since sv_clean_all might release some PMOPs
920 * after PL_regex_padav has been cleared
921 * and the clearing of PL_regex_padav needs to
922 * happen before sv_clean_all
925 if(PL_regex_pad) { /* We could be in destruction */
926 const IV offset = (cPMOPo)->op_pmoffset;
927 ReREFCNT_dec(PM_GETRE(cPMOPo));
928 PL_regex_pad[offset] = &PL_sv_undef;
929 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
933 ReREFCNT_dec(PM_GETRE(cPMOPo));
934 PM_SETRE(cPMOPo, NULL);
940 if (o->op_targ > 0) {
941 pad_free(o->op_targ);
947 S_cop_free(pTHX_ COP* cop)
949 PERL_ARGS_ASSERT_COP_FREE;
952 if (! specialWARN(cop->cop_warnings))
953 PerlMemShared_free(cop->cop_warnings);
954 cophh_free(CopHINTHASH_get(cop));
955 if (PL_curcop == cop)
960 S_forget_pmop(pTHX_ PMOP *const o
963 HV * const pmstash = PmopSTASH(o);
965 PERL_ARGS_ASSERT_FORGET_PMOP;
967 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
968 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
970 PMOP **const array = (PMOP**) mg->mg_ptr;
971 U32 count = mg->mg_len / sizeof(PMOP**);
976 /* Found it. Move the entry at the end to overwrite it. */
977 array[i] = array[--count];
978 mg->mg_len = count * sizeof(PMOP**);
979 /* Could realloc smaller at this point always, but probably
980 not worth it. Probably worth free()ing if we're the
983 Safefree(mg->mg_ptr);
996 S_find_and_forget_pmops(pTHX_ OP *o)
998 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1000 if (o->op_flags & OPf_KIDS) {
1001 OP *kid = cUNOPo->op_first;
1003 switch (kid->op_type) {
1008 forget_pmop((PMOP*)kid);
1010 find_and_forget_pmops(kid);
1011 kid = kid->op_sibling;
1017 =for apidoc Am|void|op_null|OP *o
1019 Neutralizes an op when it is no longer needed, but is still linked to from
1026 Perl_op_null(pTHX_ OP *o)
1030 PERL_ARGS_ASSERT_OP_NULL;
1032 if (o->op_type == OP_NULL)
1036 o->op_targ = o->op_type;
1037 o->op_type = OP_NULL;
1038 o->op_ppaddr = PL_ppaddr[OP_NULL];
1042 Perl_op_refcnt_lock(pTHX)
1045 PERL_UNUSED_CONTEXT;
1050 Perl_op_refcnt_unlock(pTHX)
1053 PERL_UNUSED_CONTEXT;
1057 /* Contextualizers */
1060 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1062 Applies a syntactic context to an op tree representing an expression.
1063 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1064 or C<G_VOID> to specify the context to apply. The modified op tree
1071 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1073 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1075 case G_SCALAR: return scalar(o);
1076 case G_ARRAY: return list(o);
1077 case G_VOID: return scalarvoid(o);
1079 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1086 =head1 Optree Manipulation Functions
1088 =for apidoc Am|OP*|op_linklist|OP *o
1089 This function is the implementation of the L</LINKLIST> macro. It should
1090 not be called directly.
1096 Perl_op_linklist(pTHX_ OP *o)
1100 PERL_ARGS_ASSERT_OP_LINKLIST;
1105 /* establish postfix order */
1106 first = cUNOPo->op_first;
1109 o->op_next = LINKLIST(first);
1112 if (kid->op_sibling) {
1113 kid->op_next = LINKLIST(kid->op_sibling);
1114 kid = kid->op_sibling;
1128 S_scalarkids(pTHX_ OP *o)
1130 if (o && o->op_flags & OPf_KIDS) {
1132 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1139 S_scalarboolean(pTHX_ OP *o)
1143 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1145 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1146 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1147 if (ckWARN(WARN_SYNTAX)) {
1148 const line_t oldline = CopLINE(PL_curcop);
1150 if (PL_parser && PL_parser->copline != NOLINE) {
1151 /* This ensures that warnings are reported at the first line
1152 of the conditional, not the last. */
1153 CopLINE_set(PL_curcop, PL_parser->copline);
1155 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1156 CopLINE_set(PL_curcop, oldline);
1163 S_op_varname(pTHX_ const OP *o)
1166 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1167 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1169 const char funny = o->op_type == OP_PADAV
1170 || o->op_type == OP_RV2AV ? '@' : '%';
1171 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1173 if (cUNOPo->op_first->op_type != OP_GV
1174 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1176 return varname(gv, funny, 0, NULL, 0, 1);
1179 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1184 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1185 { /* or not so pretty :-) */
1186 if (o->op_type == OP_CONST) {
1188 if (SvPOK(*retsv)) {
1190 *retsv = sv_newmortal();
1191 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1192 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1194 else if (!SvOK(*retsv))
1197 else *retpv = "...";
1201 S_scalar_slice_warning(pTHX_ const OP *o)
1205 o->op_type == OP_HSLICE ? '{' : '[';
1207 o->op_type == OP_HSLICE ? '}' : ']';
1209 SV *keysv = NULL; /* just to silence compiler warnings */
1210 const char *key = NULL;
1212 if (!(o->op_private & OPpSLICEWARNING))
1214 if (PL_parser && PL_parser->error_count)
1215 /* This warning can be nonsensical when there is a syntax error. */
1218 kid = cLISTOPo->op_first;
1219 kid = kid->op_sibling; /* get past pushmark */
1220 /* weed out false positives: any ops that can return lists */
1221 switch (kid->op_type) {
1250 /* Don't warn if we have a nulled list either. */
1251 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1254 assert(kid->op_sibling);
1255 name = S_op_varname(aTHX_ kid->op_sibling);
1256 if (!name) /* XS module fiddling with the op tree */
1258 S_op_pretty(aTHX_ kid, &keysv, &key);
1259 assert(SvPOK(name));
1260 sv_chop(name,SvPVX(name)+1);
1262 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1263 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1264 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1266 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1267 lbrack, key, rbrack);
1269 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1270 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1271 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1273 SVfARG(name), lbrack, keysv, rbrack,
1274 SVfARG(name), lbrack, keysv, rbrack);
1278 Perl_scalar(pTHX_ OP *o)
1283 /* assumes no premature commitment */
1284 if (!o || (PL_parser && PL_parser->error_count)
1285 || (o->op_flags & OPf_WANT)
1286 || o->op_type == OP_RETURN)
1291 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1293 switch (o->op_type) {
1295 scalar(cBINOPo->op_first);
1300 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1310 if (o->op_flags & OPf_KIDS) {
1311 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1317 kid = cLISTOPo->op_first;
1319 kid = kid->op_sibling;
1322 OP *sib = kid->op_sibling;
1323 if (sib && kid->op_type != OP_LEAVEWHEN)
1329 PL_curcop = &PL_compiling;
1334 kid = cLISTOPo->op_first;
1337 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1342 /* Warn about scalar context */
1343 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1344 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1347 const char *key = NULL;
1349 /* This warning can be nonsensical when there is a syntax error. */
1350 if (PL_parser && PL_parser->error_count)
1353 if (!ckWARN(WARN_SYNTAX)) break;
1355 kid = cLISTOPo->op_first;
1356 kid = kid->op_sibling; /* get past pushmark */
1357 assert(kid->op_sibling);
1358 name = S_op_varname(aTHX_ kid->op_sibling);
1359 if (!name) /* XS module fiddling with the op tree */
1361 S_op_pretty(aTHX_ kid, &keysv, &key);
1362 assert(SvPOK(name));
1363 sv_chop(name,SvPVX(name)+1);
1365 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1366 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1367 "%%%"SVf"%c%s%c in scalar context better written "
1369 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1370 lbrack, key, rbrack);
1372 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1373 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1374 "%%%"SVf"%c%"SVf"%c in scalar context better "
1375 "written as $%"SVf"%c%"SVf"%c",
1376 SVfARG(name), lbrack, keysv, rbrack,
1377 SVfARG(name), lbrack, keysv, rbrack);
1384 Perl_scalarvoid(pTHX_ OP *o)
1388 SV *useless_sv = NULL;
1389 const char* useless = NULL;
1393 PERL_ARGS_ASSERT_SCALARVOID;
1395 /* trailing mad null ops don't count as "there" for void processing */
1397 o->op_type != OP_NULL &&
1399 o->op_sibling->op_type == OP_NULL)
1402 for (sib = o->op_sibling;
1403 sib && sib->op_type == OP_NULL;
1404 sib = sib->op_sibling) ;
1410 if (o->op_type == OP_NEXTSTATE
1411 || o->op_type == OP_DBSTATE
1412 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1413 || o->op_targ == OP_DBSTATE)))
1414 PL_curcop = (COP*)o; /* for warning below */
1416 /* assumes no premature commitment */
1417 want = o->op_flags & OPf_WANT;
1418 if ((want && want != OPf_WANT_SCALAR)
1419 || (PL_parser && PL_parser->error_count)
1420 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1425 if ((o->op_private & OPpTARGET_MY)
1426 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1428 return scalar(o); /* As if inside SASSIGN */
1431 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1433 switch (o->op_type) {
1435 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1439 if (o->op_flags & OPf_STACKED)
1443 if (o->op_private == 4)
1468 case OP_AELEMFAST_LEX:
1489 case OP_GETSOCKNAME:
1490 case OP_GETPEERNAME:
1495 case OP_GETPRIORITY:
1520 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1521 /* Otherwise it's "Useless use of grep iterator" */
1522 useless = OP_DESC(o);
1526 kid = cLISTOPo->op_first;
1527 if (kid && kid->op_type == OP_PUSHRE
1529 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1531 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1533 useless = OP_DESC(o);
1537 kid = cUNOPo->op_first;
1538 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1539 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1542 useless = "negative pattern binding (!~)";
1546 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1547 useless = "non-destructive substitution (s///r)";
1551 useless = "non-destructive transliteration (tr///r)";
1558 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1559 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1560 useless = "a variable";
1565 if (cSVOPo->op_private & OPpCONST_STRICT)
1566 no_bareword_allowed(o);
1568 if (ckWARN(WARN_VOID)) {
1569 /* don't warn on optimised away booleans, eg
1570 * use constant Foo, 5; Foo || print; */
1571 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1573 /* the constants 0 and 1 are permitted as they are
1574 conventionally used as dummies in constructs like
1575 1 while some_condition_with_side_effects; */
1576 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1578 else if (SvPOK(sv)) {
1579 SV * const dsv = newSVpvs("");
1581 = Perl_newSVpvf(aTHX_
1583 pv_pretty(dsv, SvPVX_const(sv),
1584 SvCUR(sv), 32, NULL, NULL,
1586 | PERL_PV_ESCAPE_NOCLEAR
1587 | PERL_PV_ESCAPE_UNI_DETECT));
1588 SvREFCNT_dec_NN(dsv);
1590 else if (SvOK(sv)) {
1591 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", sv);
1594 useless = "a constant (undef)";
1597 op_null(o); /* don't execute or even remember it */
1601 o->op_type = OP_PREINC; /* pre-increment is faster */
1602 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1606 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1607 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1611 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1612 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1616 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1617 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1622 UNOP *refgen, *rv2cv;
1625 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1628 rv2gv = ((BINOP *)o)->op_last;
1629 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1632 refgen = (UNOP *)((BINOP *)o)->op_first;
1634 if (!refgen || refgen->op_type != OP_REFGEN)
1637 exlist = (LISTOP *)refgen->op_first;
1638 if (!exlist || exlist->op_type != OP_NULL
1639 || exlist->op_targ != OP_LIST)
1642 if (exlist->op_first->op_type != OP_PUSHMARK)
1645 rv2cv = (UNOP*)exlist->op_last;
1647 if (rv2cv->op_type != OP_RV2CV)
1650 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1651 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1652 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1654 o->op_private |= OPpASSIGN_CV_TO_GV;
1655 rv2gv->op_private |= OPpDONT_INIT_GV;
1656 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1668 kid = cLOGOPo->op_first;
1669 if (kid->op_type == OP_NOT
1670 && (kid->op_flags & OPf_KIDS)
1672 if (o->op_type == OP_AND) {
1674 o->op_ppaddr = PL_ppaddr[OP_OR];
1676 o->op_type = OP_AND;
1677 o->op_ppaddr = PL_ppaddr[OP_AND];
1686 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1691 if (o->op_flags & OPf_STACKED)
1698 if (!(o->op_flags & OPf_KIDS))
1709 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1720 /* mortalise it, in case warnings are fatal. */
1721 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1722 "Useless use of %"SVf" in void context",
1723 sv_2mortal(useless_sv));
1726 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1727 "Useless use of %s in void context",
1734 S_listkids(pTHX_ OP *o)
1736 if (o && o->op_flags & OPf_KIDS) {
1738 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1745 Perl_list(pTHX_ OP *o)
1750 /* assumes no premature commitment */
1751 if (!o || (o->op_flags & OPf_WANT)
1752 || (PL_parser && PL_parser->error_count)
1753 || o->op_type == OP_RETURN)
1758 if ((o->op_private & OPpTARGET_MY)
1759 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1761 return o; /* As if inside SASSIGN */
1764 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1766 switch (o->op_type) {
1769 list(cBINOPo->op_first);
1774 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1782 if (!(o->op_flags & OPf_KIDS))
1784 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1785 list(cBINOPo->op_first);
1786 return gen_constant_list(o);
1793 kid = cLISTOPo->op_first;
1795 kid = kid->op_sibling;
1798 OP *sib = kid->op_sibling;
1799 if (sib && kid->op_type != OP_LEAVEWHEN)
1805 PL_curcop = &PL_compiling;
1809 kid = cLISTOPo->op_first;
1816 S_scalarseq(pTHX_ OP *o)
1820 const OPCODE type = o->op_type;
1822 if (type == OP_LINESEQ || type == OP_SCOPE ||
1823 type == OP_LEAVE || type == OP_LEAVETRY)
1826 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1827 if (kid->op_sibling) {
1831 PL_curcop = &PL_compiling;
1833 o->op_flags &= ~OPf_PARENS;
1834 if (PL_hints & HINT_BLOCK_SCOPE)
1835 o->op_flags |= OPf_PARENS;
1838 o = newOP(OP_STUB, 0);
1843 S_modkids(pTHX_ OP *o, I32 type)
1845 if (o && o->op_flags & OPf_KIDS) {
1847 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1848 op_lvalue(kid, type);
1854 =for apidoc finalize_optree
1856 This function finalizes the optree. Should be called directly after
1857 the complete optree is built. It does some additional
1858 checking which can't be done in the normal ck_xxx functions and makes
1859 the tree thread-safe.
1864 Perl_finalize_optree(pTHX_ OP* o)
1866 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1869 SAVEVPTR(PL_curcop);
1877 S_finalize_op(pTHX_ OP* o)
1879 PERL_ARGS_ASSERT_FINALIZE_OP;
1881 #if defined(PERL_MAD) && defined(USE_ITHREADS)
1883 /* Make sure mad ops are also thread-safe */
1884 MADPROP *mp = o->op_madprop;
1886 if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1887 OP *prop_op = (OP *) mp->mad_val;
1888 /* We only need "Relocate sv to the pad for thread safety.", but this
1889 easiest way to make sure it traverses everything */
1890 if (prop_op->op_type == OP_CONST)
1891 cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1892 finalize_op(prop_op);
1899 switch (o->op_type) {
1902 PL_curcop = ((COP*)o); /* for warnings */
1906 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1907 && ckWARN(WARN_EXEC))
1909 if (o->op_sibling->op_sibling) {
1910 const OPCODE type = o->op_sibling->op_sibling->op_type;
1911 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1912 const line_t oldline = CopLINE(PL_curcop);
1913 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1914 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1915 "Statement unlikely to be reached");
1916 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1917 "\t(Maybe you meant system() when you said exec()?)\n");
1918 CopLINE_set(PL_curcop, oldline);
1925 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1926 GV * const gv = cGVOPo_gv;
1927 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1928 /* XXX could check prototype here instead of just carping */
1929 SV * const sv = sv_newmortal();
1930 gv_efullname3(sv, gv, NULL);
1931 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1932 "%"SVf"() called too early to check prototype",
1939 if (cSVOPo->op_private & OPpCONST_STRICT)
1940 no_bareword_allowed(o);
1944 case OP_METHOD_NAMED:
1945 /* Relocate sv to the pad for thread safety.
1946 * Despite being a "constant", the SV is written to,
1947 * for reference counts, sv_upgrade() etc. */
1948 if (cSVOPo->op_sv) {
1949 const PADOFFSET ix = pad_alloc(OP_CONST, SVf_READONLY);
1950 SvREFCNT_dec(PAD_SVl(ix));
1951 PAD_SETSV(ix, cSVOPo->op_sv);
1952 /* XXX I don't know how this isn't readonly already. */
1953 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
1954 cSVOPo->op_sv = NULL;
1968 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
1971 rop = (UNOP*)((BINOP*)o)->op_first;
1976 S_scalar_slice_warning(aTHX_ o);
1979 kid = cLISTOPo->op_first->op_sibling;
1980 if (/* I bet there's always a pushmark... */
1981 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
1982 && OP_TYPE_ISNT_NN(kid, OP_CONST))
1987 key_op = (SVOP*)(kid->op_type == OP_CONST
1989 : kLISTOP->op_first->op_sibling);
1991 rop = (UNOP*)((LISTOP*)o)->op_last;
1994 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
1996 else if (rop->op_first->op_type == OP_PADSV)
1997 /* @$hash{qw(keys here)} */
1998 rop = (UNOP*)rop->op_first;
2000 /* @{$hash}{qw(keys here)} */
2001 if (rop->op_first->op_type == OP_SCOPE
2002 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2004 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2010 lexname = NULL; /* just to silence compiler warnings */
2011 fields = NULL; /* just to silence compiler warnings */
2015 && (lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE),
2016 SvPAD_TYPED(lexname))
2017 && (fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE))
2018 && isGV(*fields) && GvHV(*fields);
2020 key_op = (SVOP*)key_op->op_sibling) {
2022 if (key_op->op_type != OP_CONST)
2024 svp = cSVOPx_svp(key_op);
2026 /* Make the CONST have a shared SV */
2027 if ((!SvIsCOW_shared_hash(sv = *svp))
2028 && SvTYPE(sv) < SVt_PVMG && SvOK(sv) && !SvROK(sv)) {
2030 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2031 SV *nsv = newSVpvn_share(key,
2032 SvUTF8(sv) ? -keylen : keylen, 0);
2033 SvREFCNT_dec_NN(sv);
2038 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) {
2039 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2040 "in variable %"SVf" of type %"HEKf,
2041 SVfARG(*svp), SVfARG(lexname),
2042 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
2048 S_scalar_slice_warning(aTHX_ o);
2052 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2053 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2060 if (o->op_flags & OPf_KIDS) {
2062 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
2068 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2070 Propagate lvalue ("modifiable") context to an op and its children.
2071 I<type> represents the context type, roughly based on the type of op that
2072 would do the modifying, although C<local()> is represented by OP_NULL,
2073 because it has no op type of its own (it is signalled by a flag on
2076 This function detects things that can't be modified, such as C<$x+1>, and
2077 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2078 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2080 It also flags things that need to behave specially in an lvalue context,
2081 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2087 S_vivifies(const OPCODE type)
2090 case OP_RV2AV: case OP_ASLICE:
2091 case OP_RV2HV: case OP_KVASLICE:
2092 case OP_RV2SV: case OP_HSLICE:
2093 case OP_AELEMFAST: case OP_KVHSLICE:
2102 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2106 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2109 if (!o || (PL_parser && PL_parser->error_count))
2112 if ((o->op_private & OPpTARGET_MY)
2113 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2118 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2120 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2122 switch (o->op_type) {
2127 if ((o->op_flags & OPf_PARENS) || PL_madskills)
2131 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2132 !(o->op_flags & OPf_STACKED)) {
2133 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2134 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
2135 poses, so we need it clear. */
2136 o->op_private &= ~1;
2137 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2138 assert(cUNOPo->op_first->op_type == OP_NULL);
2139 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2142 else { /* lvalue subroutine call */
2143 o->op_private |= OPpLVAL_INTRO
2144 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
2145 PL_modcount = RETURN_UNLIMITED_NUMBER;
2146 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
2147 /* Potential lvalue context: */
2148 o->op_private |= OPpENTERSUB_INARGS;
2151 else { /* Compile-time error message: */
2152 OP *kid = cUNOPo->op_first;
2155 if (kid->op_type != OP_PUSHMARK) {
2156 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2158 "panic: unexpected lvalue entersub "
2159 "args: type/targ %ld:%"UVuf,
2160 (long)kid->op_type, (UV)kid->op_targ);
2161 kid = kLISTOP->op_first;
2163 while (kid->op_sibling)
2164 kid = kid->op_sibling;
2165 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2166 break; /* Postpone until runtime */
2169 kid = kUNOP->op_first;
2170 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2171 kid = kUNOP->op_first;
2172 if (kid->op_type == OP_NULL)
2174 "Unexpected constant lvalue entersub "
2175 "entry via type/targ %ld:%"UVuf,
2176 (long)kid->op_type, (UV)kid->op_targ);
2177 if (kid->op_type != OP_GV) {
2181 cv = GvCV(kGVOP_gv);
2191 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2192 /* grep, foreach, subcalls, refgen */
2193 if (type == OP_GREPSTART || type == OP_ENTERSUB
2194 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2196 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2197 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2199 : (o->op_type == OP_ENTERSUB
2200 ? "non-lvalue subroutine call"
2202 type ? PL_op_desc[type] : "local"));
2216 case OP_RIGHT_SHIFT:
2225 if (!(o->op_flags & OPf_STACKED))
2232 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2233 op_lvalue(kid, type);
2238 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2239 PL_modcount = RETURN_UNLIMITED_NUMBER;
2240 return o; /* Treat \(@foo) like ordinary list. */
2244 if (scalar_mod_type(o, type))
2246 ref(cUNOPo->op_first, o->op_type);
2253 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2254 if (type == OP_LEAVESUBLV && (
2255 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2256 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2258 o->op_private |= OPpMAYBE_LVSUB;
2262 PL_modcount = RETURN_UNLIMITED_NUMBER;
2266 if (type == OP_LEAVESUBLV)
2267 o->op_private |= OPpMAYBE_LVSUB;
2270 PL_hints |= HINT_BLOCK_SCOPE;
2271 if (type == OP_LEAVESUBLV)
2272 o->op_private |= OPpMAYBE_LVSUB;
2276 ref(cUNOPo->op_first, o->op_type);
2280 PL_hints |= HINT_BLOCK_SCOPE;
2289 case OP_AELEMFAST_LEX:
2296 PL_modcount = RETURN_UNLIMITED_NUMBER;
2297 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2298 return o; /* Treat \(@foo) like ordinary list. */
2299 if (scalar_mod_type(o, type))
2301 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2302 && type == OP_LEAVESUBLV)
2303 o->op_private |= OPpMAYBE_LVSUB;
2307 if (!type) /* local() */
2308 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2309 PAD_COMPNAME_SV(o->op_targ));
2318 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2322 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2328 if (type == OP_LEAVESUBLV)
2329 o->op_private |= OPpMAYBE_LVSUB;
2330 if (o->op_flags & OPf_KIDS)
2331 op_lvalue(cBINOPo->op_first->op_sibling, type);
2336 ref(cBINOPo->op_first, o->op_type);
2337 if (type == OP_ENTERSUB &&
2338 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2339 o->op_private |= OPpLVAL_DEFER;
2340 if (type == OP_LEAVESUBLV)
2341 o->op_private |= OPpMAYBE_LVSUB;
2348 o->op_private |= OPpLVALUE;
2353 if (o->op_flags & OPf_KIDS)
2354 op_lvalue(cLISTOPo->op_last, type);
2359 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2361 else if (!(o->op_flags & OPf_KIDS))
2363 if (o->op_targ != OP_LIST) {
2364 op_lvalue(cBINOPo->op_first, type);
2370 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2371 /* elements might be in void context because the list is
2372 in scalar context or because they are attribute sub calls */
2373 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2374 op_lvalue(kid, type);
2378 if (type != OP_LEAVESUBLV)
2380 break; /* op_lvalue()ing was handled by ck_return() */
2387 if (type == OP_LEAVESUBLV
2388 || !S_vivifies(cLOGOPo->op_first->op_type))
2389 op_lvalue(cLOGOPo->op_first, type);
2390 if (type == OP_LEAVESUBLV
2391 || !S_vivifies(cLOGOPo->op_first->op_sibling->op_type))
2392 op_lvalue(cLOGOPo->op_first->op_sibling, type);
2396 /* [20011101.069] File test operators interpret OPf_REF to mean that
2397 their argument is a filehandle; thus \stat(".") should not set
2399 if (type == OP_REFGEN &&
2400 PL_check[o->op_type] == Perl_ck_ftst)
2403 if (type != OP_LEAVESUBLV)
2404 o->op_flags |= OPf_MOD;
2406 if (type == OP_AASSIGN || type == OP_SASSIGN)
2407 o->op_flags |= OPf_SPECIAL|OPf_REF;
2408 else if (!type) { /* local() */
2411 o->op_private |= OPpLVAL_INTRO;
2412 o->op_flags &= ~OPf_SPECIAL;
2413 PL_hints |= HINT_BLOCK_SCOPE;
2418 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2419 "Useless localization of %s", OP_DESC(o));
2422 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2423 && type != OP_LEAVESUBLV)
2424 o->op_flags |= OPf_REF;
2429 S_scalar_mod_type(const OP *o, I32 type)
2434 if (o && o->op_type == OP_RV2GV)
2458 case OP_RIGHT_SHIFT:
2479 S_is_handle_constructor(const OP *o, I32 numargs)
2481 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2483 switch (o->op_type) {
2491 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2504 S_refkids(pTHX_ OP *o, I32 type)
2506 if (o && o->op_flags & OPf_KIDS) {
2508 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2515 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2520 PERL_ARGS_ASSERT_DOREF;
2522 if (!o || (PL_parser && PL_parser->error_count))
2525 switch (o->op_type) {
2527 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2528 !(o->op_flags & OPf_STACKED)) {
2529 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2530 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2531 assert(cUNOPo->op_first->op_type == OP_NULL);
2532 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2533 o->op_flags |= OPf_SPECIAL;
2534 o->op_private &= ~1;
2536 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2537 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2538 : type == OP_RV2HV ? OPpDEREF_HV
2540 o->op_flags |= OPf_MOD;
2546 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2547 doref(kid, type, set_op_ref);
2550 if (type == OP_DEFINED)
2551 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2552 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2555 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2556 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2557 : type == OP_RV2HV ? OPpDEREF_HV
2559 o->op_flags |= OPf_MOD;
2566 o->op_flags |= OPf_REF;
2569 if (type == OP_DEFINED)
2570 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2571 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2577 o->op_flags |= OPf_REF;
2582 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
2584 doref(cBINOPo->op_first, type, set_op_ref);
2588 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2589 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2590 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2591 : type == OP_RV2HV ? OPpDEREF_HV
2593 o->op_flags |= OPf_MOD;
2603 if (!(o->op_flags & OPf_KIDS))
2605 doref(cLISTOPo->op_last, type, set_op_ref);
2615 S_dup_attrlist(pTHX_ OP *o)
2620 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2622 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2623 * where the first kid is OP_PUSHMARK and the remaining ones
2624 * are OP_CONST. We need to push the OP_CONST values.
2626 if (o->op_type == OP_CONST)
2627 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2629 else if (o->op_type == OP_NULL)
2633 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2635 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2636 if (o->op_type == OP_CONST)
2637 rop = op_append_elem(OP_LIST, rop,
2638 newSVOP(OP_CONST, o->op_flags,
2639 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2646 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
2649 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2651 PERL_ARGS_ASSERT_APPLY_ATTRS;
2653 /* fake up C<use attributes $pkg,$rv,@attrs> */
2655 #define ATTRSMODULE "attributes"
2656 #define ATTRSMODULE_PM "attributes.pm"
2658 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2659 newSVpvs(ATTRSMODULE),
2661 op_prepend_elem(OP_LIST,
2662 newSVOP(OP_CONST, 0, stashsv),
2663 op_prepend_elem(OP_LIST,
2664 newSVOP(OP_CONST, 0,
2666 dup_attrlist(attrs))));
2670 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2673 OP *pack, *imop, *arg;
2674 SV *meth, *stashsv, **svp;
2676 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2681 assert(target->op_type == OP_PADSV ||
2682 target->op_type == OP_PADHV ||
2683 target->op_type == OP_PADAV);
2685 /* Ensure that attributes.pm is loaded. */
2686 /* Don't force the C<use> if we don't need it. */
2687 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2688 if (svp && *svp != &PL_sv_undef)
2689 NOOP; /* already in %INC */
2691 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2692 newSVpvs(ATTRSMODULE), NULL);
2694 /* Need package name for method call. */
2695 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2697 /* Build up the real arg-list. */
2698 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2700 arg = newOP(OP_PADSV, 0);
2701 arg->op_targ = target->op_targ;
2702 arg = op_prepend_elem(OP_LIST,
2703 newSVOP(OP_CONST, 0, stashsv),
2704 op_prepend_elem(OP_LIST,
2705 newUNOP(OP_REFGEN, 0,
2706 op_lvalue(arg, OP_REFGEN)),
2707 dup_attrlist(attrs)));
2709 /* Fake up a method call to import */
2710 meth = newSVpvs_share("import");
2711 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2712 op_append_elem(OP_LIST,
2713 op_prepend_elem(OP_LIST, pack, list(arg)),
2714 newSVOP(OP_METHOD_NAMED, 0, meth)));
2716 /* Combine the ops. */
2717 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2721 =notfor apidoc apply_attrs_string
2723 Attempts to apply a list of attributes specified by the C<attrstr> and
2724 C<len> arguments to the subroutine identified by the C<cv> argument which
2725 is expected to be associated with the package identified by the C<stashpv>
2726 argument (see L<attributes>). It gets this wrong, though, in that it
2727 does not correctly identify the boundaries of the individual attribute
2728 specifications within C<attrstr>. This is not really intended for the
2729 public API, but has to be listed here for systems such as AIX which
2730 need an explicit export list for symbols. (It's called from XS code
2731 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2732 to respect attribute syntax properly would be welcome.
2738 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2739 const char *attrstr, STRLEN len)
2743 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2746 len = strlen(attrstr);
2750 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2752 const char * const sstr = attrstr;
2753 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2754 attrs = op_append_elem(OP_LIST, attrs,
2755 newSVOP(OP_CONST, 0,
2756 newSVpvn(sstr, attrstr-sstr)));
2760 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2761 newSVpvs(ATTRSMODULE),
2762 NULL, op_prepend_elem(OP_LIST,
2763 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2764 op_prepend_elem(OP_LIST,
2765 newSVOP(OP_CONST, 0,
2766 newRV(MUTABLE_SV(cv))),
2771 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
2773 OP *new_proto = NULL;
2778 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
2784 if (o->op_type == OP_CONST) {
2785 pv = SvPV(cSVOPo_sv, pvlen);
2786 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
2787 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
2788 SV ** const tmpo = cSVOPx_svp(o);
2789 SvREFCNT_dec(cSVOPo_sv);
2794 } else if (o->op_type == OP_LIST) {
2796 assert(o->op_flags & OPf_KIDS);
2797 assert(cLISTOPo->op_first->op_type == OP_PUSHMARK);
2798 /* Counting on the first op to hit the lasto = o line */
2799 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2800 if (o->op_type == OP_CONST) {
2801 pv = SvPV(cSVOPo_sv, pvlen);
2802 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
2803 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
2804 SV ** const tmpo = cSVOPx_svp(o);
2805 SvREFCNT_dec(cSVOPo_sv);
2807 if (new_proto && ckWARN(WARN_MISC)) {
2809 const char * newp = SvPV(cSVOPo_sv, new_len);
2810 Perl_warner(aTHX_ packWARN(WARN_MISC),
2811 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
2812 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
2818 lasto->op_sibling = o->op_sibling;
2824 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
2825 would get pulled in with no real need */
2826 if (!cLISTOPx(*attrs)->op_first->op_sibling) {
2835 svname = sv_newmortal();
2836 gv_efullname3(svname, name, NULL);
2838 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
2839 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
2841 svname = (SV *)name;
2842 if (ckWARN(WARN_ILLEGALPROTO))
2843 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
2844 if (*proto && ckWARN(WARN_PROTOTYPE)) {
2845 STRLEN old_len, new_len;
2846 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
2847 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
2849 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2850 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
2852 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
2853 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
2863 S_cant_declare(pTHX_ OP *o)
2865 if (o->op_type == OP_NULL
2866 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
2867 o = cUNOPo->op_first;
2868 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2869 o->op_type == OP_NULL
2870 && o->op_flags & OPf_SPECIAL
2873 PL_parser->in_my == KEY_our ? "our" :
2874 PL_parser->in_my == KEY_state ? "state" :
2879 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2883 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2885 PERL_ARGS_ASSERT_MY_KID;
2887 if (!o || (PL_parser && PL_parser->error_count))
2891 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2892 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2896 if (type == OP_LIST) {
2898 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2899 my_kid(kid, attrs, imopsp);
2901 } else if (type == OP_UNDEF || type == OP_STUB) {
2903 } else if (type == OP_RV2SV || /* "our" declaration */
2905 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2906 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2907 S_cant_declare(aTHX_ o);
2909 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2910 PL_parser->in_my = FALSE;
2911 PL_parser->in_my_stash = NULL;
2912 apply_attrs(GvSTASH(gv),
2913 (type == OP_RV2SV ? GvSV(gv) :
2914 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2915 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2918 o->op_private |= OPpOUR_INTRO;
2921 else if (type != OP_PADSV &&
2924 type != OP_PUSHMARK)
2926 S_cant_declare(aTHX_ o);
2929 else if (attrs && type != OP_PUSHMARK) {
2932 PL_parser->in_my = FALSE;
2933 PL_parser->in_my_stash = NULL;
2935 /* check for C<my Dog $spot> when deciding package */
2936 stash = PAD_COMPNAME_TYPE(o->op_targ);
2938 stash = PL_curstash;
2939 apply_attrs_my(stash, o, attrs, imopsp);
2941 o->op_flags |= OPf_MOD;
2942 o->op_private |= OPpLVAL_INTRO;
2944 o->op_private |= OPpPAD_STATE;
2949 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2953 int maybe_scalar = 0;
2955 PERL_ARGS_ASSERT_MY_ATTRS;
2957 /* [perl #17376]: this appears to be premature, and results in code such as
2958 C< our(%x); > executing in list mode rather than void mode */
2960 if (o->op_flags & OPf_PARENS)
2970 o = my_kid(o, attrs, &rops);
2972 if (maybe_scalar && o->op_type == OP_PADSV) {
2973 o = scalar(op_append_list(OP_LIST, rops, o));
2974 o->op_private |= OPpLVAL_INTRO;
2977 /* The listop in rops might have a pushmark at the beginning,
2978 which will mess up list assignment. */
2979 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2980 if (rops->op_type == OP_LIST &&
2981 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2983 OP * const pushmark = lrops->op_first;
2984 lrops->op_first = pushmark->op_sibling;
2987 o = op_append_list(OP_LIST, o, rops);
2990 PL_parser->in_my = FALSE;
2991 PL_parser->in_my_stash = NULL;
2996 Perl_sawparens(pTHX_ OP *o)
2998 PERL_UNUSED_CONTEXT;
3000 o->op_flags |= OPf_PARENS;
3005 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3009 const OPCODE ltype = left->op_type;
3010 const OPCODE rtype = right->op_type;
3012 PERL_ARGS_ASSERT_BIND_MATCH;
3014 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3015 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3017 const char * const desc
3019 rtype == OP_SUBST || rtype == OP_TRANS
3020 || rtype == OP_TRANSR
3022 ? (int)rtype : OP_MATCH];
3023 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3025 S_op_varname(aTHX_ left);
3027 Perl_warner(aTHX_ packWARN(WARN_MISC),
3028 "Applying %s to %"SVf" will act on scalar(%"SVf")",
3031 const char * const sample = (isary
3032 ? "@array" : "%hash");
3033 Perl_warner(aTHX_ packWARN(WARN_MISC),
3034 "Applying %s to %s will act on scalar(%s)",
3035 desc, sample, sample);
3039 if (rtype == OP_CONST &&
3040 cSVOPx(right)->op_private & OPpCONST_BARE &&
3041 cSVOPx(right)->op_private & OPpCONST_STRICT)
3043 no_bareword_allowed(right);
3046 /* !~ doesn't make sense with /r, so error on it for now */
3047 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3049 /* diag_listed_as: Using !~ with %s doesn't make sense */
3050 yyerror("Using !~ with s///r doesn't make sense");
3051 if (rtype == OP_TRANSR && type == OP_NOT)
3052 /* diag_listed_as: Using !~ with %s doesn't make sense */
3053 yyerror("Using !~ with tr///r doesn't make sense");
3055 ismatchop = (rtype == OP_MATCH ||
3056 rtype == OP_SUBST ||
3057 rtype == OP_TRANS || rtype == OP_TRANSR)
3058 && !(right->op_flags & OPf_SPECIAL);
3059 if (ismatchop && right->op_private & OPpTARGET_MY) {
3061 right->op_private &= ~OPpTARGET_MY;
3063 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
3066 right->op_flags |= OPf_STACKED;
3067 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3068 ! (rtype == OP_TRANS &&
3069 right->op_private & OPpTRANS_IDENTICAL) &&
3070 ! (rtype == OP_SUBST &&
3071 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3072 newleft = op_lvalue(left, rtype);
3075 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3076 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
3078 o = op_prepend_elem(rtype, scalar(newleft), right);
3080 return newUNOP(OP_NOT, 0, scalar(o));
3084 return bind_match(type, left,
3085 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
3089 Perl_invert(pTHX_ OP *o)
3093 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3097 =for apidoc Amx|OP *|op_scope|OP *o
3099 Wraps up an op tree with some additional ops so that at runtime a dynamic
3100 scope will be created. The original ops run in the new dynamic scope,
3101 and then, provided that they exit normally, the scope will be unwound.
3102 The additional ops used to create and unwind the dynamic scope will
3103 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3104 instead if the ops are simple enough to not need the full dynamic scope
3111 Perl_op_scope(pTHX_ OP *o)
3115 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3116 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3117 o->op_type = OP_LEAVE;
3118 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
3120 else if (o->op_type == OP_LINESEQ) {
3122 o->op_type = OP_SCOPE;
3123 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
3124 kid = ((LISTOP*)o)->op_first;
3125 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3128 /* The following deals with things like 'do {1 for 1}' */
3129 kid = kid->op_sibling;
3131 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3136 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3142 Perl_op_unscope(pTHX_ OP *o)
3144 if (o && o->op_type == OP_LINESEQ) {
3145 OP *kid = cLISTOPo->op_first;
3146 for(; kid; kid = kid->op_sibling)
3147 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3154 Perl_block_start(pTHX_ int full)
3157 const int retval = PL_savestack_ix;
3159 pad_block_start(full);
3161 PL_hints &= ~HINT_BLOCK_SCOPE;
3162 SAVECOMPILEWARNINGS();
3163 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3165 CALL_BLOCK_HOOKS(bhk_start, full);
3171 Perl_block_end(pTHX_ I32 floor, OP *seq)
3174 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3175 OP* retval = scalarseq(seq);
3178 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3182 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3186 /* pad_leavemy has created a sequence of introcv ops for all my
3187 subs declared in the block. We have to replicate that list with
3188 clonecv ops, to deal with this situation:
3193 sub s1 { state sub foo { \&s2 } }
3196 Originally, I was going to have introcv clone the CV and turn
3197 off the stale flag. Since &s1 is declared before &s2, the
3198 introcv op for &s1 is executed (on sub entry) before the one for
3199 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3200 cloned, since it is a state sub) closes over &s2 and expects
3201 to see it in its outer CV’s pad. If the introcv op clones &s1,
3202 then &s2 is still marked stale. Since &s1 is not active, and
3203 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3204 ble will not stay shared’ warning. Because it is the same stub
3205 that will be used when the introcv op for &s2 is executed, clos-
3206 ing over it is safe. Hence, we have to turn off the stale flag
3207 on all lexical subs in the block before we clone any of them.
3208 Hence, having introcv clone the sub cannot work. So we create a
3209 list of ops like this:
3233 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3234 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3235 for (;; kid = kid->op_sibling) {
3236 OP *newkid = newOP(OP_CLONECV, 0);
3237 newkid->op_targ = kid->op_targ;
3238 o = op_append_elem(OP_LINESEQ, o, newkid);
3239 if (kid == last) break;
3241 retval = op_prepend_elem(OP_LINESEQ, o, retval);
3244 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
3250 =head1 Compile-time scope hooks
3252 =for apidoc Aox||blockhook_register
3254 Register a set of hooks to be called when the Perl lexical scope changes
3255 at compile time. See L<perlguts/"Compile-time scope hooks">.
3261 Perl_blockhook_register(pTHX_ BHK *hk)
3263 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
3265 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
3272 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
3273 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
3274 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
3277 OP * const o = newOP(OP_PADSV, 0);
3278 o->op_targ = offset;
3284 Perl_newPROG(pTHX_ OP *o)
3288 PERL_ARGS_ASSERT_NEWPROG;
3295 PL_eval_root = newUNOP(OP_LEAVEEVAL,
3296 ((PL_in_eval & EVAL_KEEPERR)
3297 ? OPf_SPECIAL : 0), o);
3299 cx = &cxstack[cxstack_ix];
3300 assert(CxTYPE(cx) == CXt_EVAL);
3302 if ((cx->blk_gimme & G_WANT) == G_VOID)
3303 scalarvoid(PL_eval_root);
3304 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3307 scalar(PL_eval_root);
3309 PL_eval_start = op_linklist(PL_eval_root);
3310 PL_eval_root->op_private |= OPpREFCOUNTED;
3311 OpREFCNT_set(PL_eval_root, 1);
3312 PL_eval_root->op_next = 0;
3313 i = PL_savestack_ix;
3316 CALL_PEEP(PL_eval_start);
3317 finalize_optree(PL_eval_root);
3318 S_prune_chain_head(aTHX_ &PL_eval_start);
3320 PL_savestack_ix = i;
3323 if (o->op_type == OP_STUB) {
3324 /* This block is entered if nothing is compiled for the main
3325 program. This will be the case for an genuinely empty main
3326 program, or one which only has BEGIN blocks etc, so already
3329 Historically (5.000) the guard above was !o. However, commit
3330 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
3331 c71fccf11fde0068, changed perly.y so that newPROG() is now
3332 called with the output of block_end(), which returns a new
3333 OP_STUB for the case of an empty optree. ByteLoader (and
3334 maybe other things) also take this path, because they set up
3335 PL_main_start and PL_main_root directly, without generating an
3338 If the parsing the main program aborts (due to parse errors,
3339 or due to BEGIN or similar calling exit), then newPROG()
3340 isn't even called, and hence this code path and its cleanups
3341 are skipped. This shouldn't make a make a difference:
3342 * a non-zero return from perl_parse is a failure, and
3343 perl_destruct() should be called immediately.
3344 * however, if exit(0) is called during the parse, then
3345 perl_parse() returns 0, and perl_run() is called. As
3346 PL_main_start will be NULL, perl_run() will return
3347 promptly, and the exit code will remain 0.
3350 PL_comppad_name = 0;
3352 S_op_destroy(aTHX_ o);
3355 PL_main_root = op_scope(sawparens(scalarvoid(o)));
3356 PL_curcop = &PL_compiling;
3357 PL_main_start = LINKLIST(PL_main_root);
3358 PL_main_root->op_private |= OPpREFCOUNTED;
3359 OpREFCNT_set(PL_main_root, 1);
3360 PL_main_root->op_next = 0;
3361 CALL_PEEP(PL_main_start);
3362 finalize_optree(PL_main_root);
3363 S_prune_chain_head(aTHX_ &PL_main_start);
3364 cv_forget_slab(PL_compcv);
3367 /* Register with debugger */
3369 CV * const cv = get_cvs("DB::postponed", 0);
3373 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3375 call_sv(MUTABLE_SV(cv), G_DISCARD);
3382 Perl_localize(pTHX_ OP *o, I32 lex)
3386 PERL_ARGS_ASSERT_LOCALIZE;
3388 if (o->op_flags & OPf_PARENS)
3389 /* [perl #17376]: this appears to be premature, and results in code such as
3390 C< our(%x); > executing in list mode rather than void mode */
3397 if ( PL_parser->bufptr > PL_parser->oldbufptr
3398 && PL_parser->bufptr[-1] == ','
3399 && ckWARN(WARN_PARENTHESIS))
3401 char *s = PL_parser->bufptr;
3404 /* some heuristics to detect a potential error */
3405 while (*s && (strchr(", \t\n", *s)))
3409 if (*s && strchr("@$%*", *s) && *++s
3410 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
3413 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
3415 while (*s && (strchr(", \t\n", *s)))
3421 if (sigil && (*s == ';' || *s == '=')) {
3422 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3423 "Parentheses missing around \"%s\" list",
3425 ? (PL_parser->in_my == KEY_our
3427 : PL_parser->in_my == KEY_state
3437 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3438 PL_parser->in_my = FALSE;
3439 PL_parser->in_my_stash = NULL;
3444 Perl_jmaybe(pTHX_ OP *o)
3446 PERL_ARGS_ASSERT_JMAYBE;
3448 if (o->op_type == OP_LIST) {
3450 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3451 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3456 PERL_STATIC_INLINE OP *
3457 S_op_std_init(pTHX_ OP *o)
3459 I32 type = o->op_type;
3461 PERL_ARGS_ASSERT_OP_STD_INIT;
3463 if (PL_opargs[type] & OA_RETSCALAR)
3465 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3466 o->op_targ = pad_alloc(type, SVs_PADTMP);
3471 PERL_STATIC_INLINE OP *
3472 S_op_integerize(pTHX_ OP *o)
3474 I32 type = o->op_type;
3476 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3478 /* integerize op. */
3479 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3482 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
3485 if (type == OP_NEGATE)
3486 /* XXX might want a ck_negate() for this */
3487 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3493 S_fold_constants(pTHX_ OP *o)
3498 VOL I32 type = o->op_type;
3503 SV * const oldwarnhook = PL_warnhook;
3504 SV * const olddiehook = PL_diehook;
3508 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3510 if (!(PL_opargs[type] & OA_FOLDCONST))
3525 /* XXX what about the numeric ops? */
3526 if (IN_LOCALE_COMPILETIME)
3530 if (!cLISTOPo->op_first->op_sibling
3531 || cLISTOPo->op_first->op_sibling->op_type != OP_CONST)
3534 SV * const sv = cSVOPx_sv(cLISTOPo->op_first->op_sibling);
3535 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3537 const char *s = SvPVX_const(sv);
3538 while (s < SvEND(sv)) {
3539 if (*s == 'p' || *s == 'P') goto nope;
3546 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3549 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
3550 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
3554 if (PL_parser && PL_parser->error_count)
3555 goto nope; /* Don't try to run w/ errors */
3557 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3558 const OPCODE type = curop->op_type;
3559 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3561 type != OP_SCALAR &&
3563 type != OP_PUSHMARK)
3569 curop = LINKLIST(o);
3570 old_next = o->op_next;
3574 oldscope = PL_scopestack_ix;
3575 create_eval_scope(G_FAKINGEVAL);
3577 /* Verify that we don't need to save it: */
3578 assert(PL_curcop == &PL_compiling);
3579 StructCopy(&PL_compiling, ¬_compiling, COP);
3580 PL_curcop = ¬_compiling;
3581 /* The above ensures that we run with all the correct hints of the
3582 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3583 assert(IN_PERL_RUNTIME);
3584 PL_warnhook = PERL_WARNHOOK_FATAL;
3591 sv = *(PL_stack_sp--);
3592 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3594 /* Can't simply swipe the SV from the pad, because that relies on
3595 the op being freed "real soon now". Under MAD, this doesn't
3596 happen (see the #ifdef below). */
3599 pad_swipe(o->op_targ, FALSE);
3602 else if (SvTEMP(sv)) { /* grab mortal temp? */
3603 SvREFCNT_inc_simple_void(sv);
3606 else { assert(SvIMMORTAL(sv)); }
3609 /* Something tried to die. Abandon constant folding. */
3610 /* Pretend the error never happened. */
3612 o->op_next = old_next;
3616 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3617 PL_warnhook = oldwarnhook;
3618 PL_diehook = olddiehook;
3619 /* XXX note that this croak may fail as we've already blown away
3620 * the stack - eg any nested evals */
3621 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3624 PL_warnhook = oldwarnhook;
3625 PL_diehook = olddiehook;
3626 PL_curcop = &PL_compiling;
3628 if (PL_scopestack_ix > oldscope)
3629 delete_eval_scope();
3638 if (type == OP_STRINGIFY) SvPADTMP_off(sv);
3639 else if (!SvIMMORTAL(sv)) {
3643 if (type == OP_RV2GV)
3644 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3647 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
3648 if (type != OP_STRINGIFY) newop->op_folded = 1;
3650 op_getmad(o,newop,'f');
3658 S_gen_constant_list(pTHX_ OP *o)
3662 const SSize_t oldtmps_floor = PL_tmps_floor;
3667 if (PL_parser && PL_parser->error_count)
3668 return o; /* Don't attempt to run with errors */
3670 curop = LINKLIST(o);
3673 S_prune_chain_head(aTHX_ &curop);
3675 Perl_pp_pushmark(aTHX);
3678 assert (!(curop->op_flags & OPf_SPECIAL));
3679 assert(curop->op_type == OP_RANGE);
3680 Perl_pp_anonlist(aTHX);
3681 PL_tmps_floor = oldtmps_floor;
3683 o->op_type = OP_RV2AV;
3684 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3685 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3686 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3687 o->op_opt = 0; /* needs to be revisited in rpeep() */
3688 curop = ((UNOP*)o)->op_first;
3689 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
3690 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, (SV *)av);
3691 if (AvFILLp(av) != -1)
3692 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
3695 SvREADONLY_on(*svp);
3698 op_getmad(curop,o,'O');
3707 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3710 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3711 if (!o || o->op_type != OP_LIST)
3712 o = newLISTOP(OP_LIST, 0, o, NULL);
3714 o->op_flags &= ~OPf_WANT;
3716 if (!(PL_opargs[type] & OA_MARK))
3717 op_null(cLISTOPo->op_first);
3719 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3720 if (kid2 && kid2->op_type == OP_COREARGS) {
3721 op_null(cLISTOPo->op_first);
3722 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3726 o->op_type = (OPCODE)type;
3727 o->op_ppaddr = PL_ppaddr[type];
3728 o->op_flags |= flags;
3730 o = CHECKOP(type, o);
3731 if (o->op_type != (unsigned)type)
3734 return fold_constants(op_integerize(op_std_init(o)));
3738 =head1 Optree Manipulation Functions
3741 /* List constructors */
3744 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3746 Append an item to the list of ops contained directly within a list-type
3747 op, returning the lengthened list. I<first> is the list-type op,
3748 and I<last> is the op to append to the list. I<optype> specifies the
3749 intended opcode for the list. If I<first> is not already a list of the
3750 right type, it will be upgraded into one. If either I<first> or I<last>
3751 is null, the other is returned unchanged.
3757 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3765 if (first->op_type != (unsigned)type
3766 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3768 return newLISTOP(type, 0, first, last);
3771 if (first->op_flags & OPf_KIDS)
3772 ((LISTOP*)first)->op_last->op_sibling = last;
3774 first->op_flags |= OPf_KIDS;
3775 ((LISTOP*)first)->op_first = last;
3777 ((LISTOP*)first)->op_last = last;
3782 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3784 Concatenate the lists of ops contained directly within two list-type ops,
3785 returning the combined list. I<first> and I<last> are the list-type ops
3786 to concatenate. I<optype> specifies the intended opcode for the list.
3787 If either I<first> or I<last> is not already a list of the right type,
3788 it will be upgraded into one. If either I<first> or I<last> is null,
3789 the other is returned unchanged.
3795 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3803 if (first->op_type != (unsigned)type)
3804 return op_prepend_elem(type, first, last);
3806 if (last->op_type != (unsigned)type)
3807 return op_append_elem(type, first, last);
3809 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3810 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3811 first->op_flags |= (last->op_flags & OPf_KIDS);
3814 if (((LISTOP*)last)->op_first && first->op_madprop) {
3815 MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3817 while (mp->mad_next)
3819 mp->mad_next = first->op_madprop;
3822 ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3825 first->op_madprop = last->op_madprop;
3826 last->op_madprop = 0;
3829 S_op_destroy(aTHX_ last);
3835 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3837 Prepend an item to the list of ops contained directly within a list-type
3838 op, returning the lengthened list. I<first> is the op to prepend to the
3839 list, and I<last> is the list-type op. I<optype> specifies the intended
3840 opcode for the list. If I<last> is not already a list of the right type,
3841 it will be upgraded into one. If either I<first> or I<last> is null,
3842 the other is returned unchanged.
3848 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3856 if (last->op_type == (unsigned)type) {
3857 if (type == OP_LIST) { /* already a PUSHMARK there */
3858 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3859 ((LISTOP*)last)->op_first->op_sibling = first;
3860 if (!(first->op_flags & OPf_PARENS))
3861 last->op_flags &= ~OPf_PARENS;
3864 if (!(last->op_flags & OPf_KIDS)) {
3865 ((LISTOP*)last)->op_last = first;
3866 last->op_flags |= OPf_KIDS;
3868 first->op_sibling = ((LISTOP*)last)->op_first;
3869 ((LISTOP*)last)->op_first = first;
3871 last->op_flags |= OPf_KIDS;
3875 return newLISTOP(type, 0, first, last);
3883 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3886 Newxz(tk, 1, TOKEN);
3887 tk->tk_type = (OPCODE)optype;
3888 tk->tk_type = 12345;
3890 tk->tk_mad = madprop;
3895 Perl_token_free(pTHX_ TOKEN* tk)
3897 PERL_ARGS_ASSERT_TOKEN_FREE;
3899 if (tk->tk_type != 12345)
3901 mad_free(tk->tk_mad);
3906 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3911 PERL_ARGS_ASSERT_TOKEN_GETMAD;
3913 if (tk->tk_type != 12345) {
3914 Perl_warner(aTHX_ packWARN(WARN_MISC),
3915 "Invalid TOKEN object ignored");
3922 /* faked up qw list? */
3924 tm->mad_type == MAD_SV &&
3925 SvPVX((SV *)tm->mad_val)[0] == 'q')
3932 /* pretend constant fold didn't happen? */
3933 if (mp->mad_key == 'f' &&
3934 (o->op_type == OP_CONST ||
3935 o->op_type == OP_GV) )
3937 token_getmad(tk,(OP*)mp->mad_val,slot);
3951 if (mp->mad_key == 'X')
3952 mp->mad_key = slot; /* just change the first one */
3962 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3971 /* pretend constant fold didn't happen? */
3972 if (mp->mad_key == 'f' &&
3973 (o->op_type == OP_CONST ||
3974 o->op_type == OP_GV) )
3976 op_getmad(from,(OP*)mp->mad_val,slot);
3983 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3986 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3992 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
4001 /* pretend constant fold didn't happen? */
4002 if (mp->mad_key == 'f' &&
4003 (o->op_type == OP_CONST ||
4004 o->op_type == OP_GV) )
4006 op_getmad(from,(OP*)mp->mad_val,slot);
4013 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
4016 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
4020 PerlIO_printf(PerlIO_stderr(),
4021 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
4027 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
4045 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
4049 addmad(tm, &(o->op_madprop), slot);
4053 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
4074 Perl_newMADsv(pTHX_ char key, SV* sv)
4076 PERL_ARGS_ASSERT_NEWMADSV;
4078 return newMADPROP(key, MAD_SV, sv, 0);
4082 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
4084 MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
4087 mp->mad_vlen = vlen;
4088 mp->mad_type = type;
4090 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
4095 Perl_mad_free(pTHX_ MADPROP* mp)
4097 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
4101 mad_free(mp->mad_next);
4102 /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
4103 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
4104 switch (mp->mad_type) {
4108 Safefree(mp->mad_val);
4111 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
4112 op_free((OP*)mp->mad_val);
4115 sv_free(MUTABLE_SV(mp->mad_val));
4118 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
4121 PerlMemShared_free(mp);
4127 =head1 Optree construction
4129 =for apidoc Am|OP *|newNULLLIST
4131 Constructs, checks, and returns a new C<stub> op, which represents an
4132 empty list expression.
4138 Perl_newNULLLIST(pTHX)
4140 return newOP(OP_STUB, 0);
4144 S_force_list(pTHX_ OP *o)
4146 if (!o || o->op_type != OP_LIST)
4147 o = newLISTOP(OP_LIST, 0, o, NULL);
4153 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4155 Constructs, checks, and returns an op of any list type. I<type> is
4156 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4157 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
4158 supply up to two ops to be direct children of the list op; they are
4159 consumed by this function and become part of the constructed op tree.
4165 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4170 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
4172 NewOp(1101, listop, 1, LISTOP);
4174 listop->op_type = (OPCODE)type;
4175 listop->op_ppaddr = PL_ppaddr[type];
4178 listop->op_flags = (U8)flags;
4182 else if (!first && last)
4185 first->op_sibling = last;
4186 listop->op_first = first;
4187 listop->op_last = last;
4188 if (type == OP_LIST) {
4189 OP* const pushop = newOP(OP_PUSHMARK, 0);
4190 pushop->op_sibling = first;
4191 listop->op_first = pushop;
4192 listop->op_flags |= OPf_KIDS;
4194 listop->op_last = pushop;
4197 return CHECKOP(type, listop);
4201 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4203 Constructs, checks, and returns an op of any base type (any type that
4204 has no extra fields). I<type> is the opcode. I<flags> gives the
4205 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4212 Perl_newOP(pTHX_ I32 type, I32 flags)
4217 if (type == -OP_ENTEREVAL) {
4218 type = OP_ENTEREVAL;
4219 flags |= OPpEVAL_BYTES<<8;
4222 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4223 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4224 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4225 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4227 NewOp(1101, o, 1, OP);
4228 o->op_type = (OPCODE)type;
4229 o->op_ppaddr = PL_ppaddr[type];
4230 o->op_flags = (U8)flags;
4233 o->op_private = (U8)(0 | (flags >> 8));
4234 if (PL_opargs[type] & OA_RETSCALAR)
4236 if (PL_opargs[type] & OA_TARGET)
4237 o->op_targ = pad_alloc(type, SVs_PADTMP);
4238 return CHECKOP(type, o);
4242 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4244 Constructs, checks, and returns an op of any unary type. I<type> is
4245 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4246 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4247 bits, the eight bits of C<op_private>, except that the bit with value 1
4248 is automatically set. I<first> supplies an optional op to be the direct
4249 child of the unary op; it is consumed by this function and become part
4250 of the constructed op tree.
4256 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4261 if (type == -OP_ENTEREVAL) {
4262 type = OP_ENTEREVAL;
4263 flags |= OPpEVAL_BYTES<<8;
4266 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4267 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4268 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4269 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4270 || type == OP_SASSIGN
4271 || type == OP_ENTERTRY
4272 || type == OP_NULL );
4275 first = newOP(OP_STUB, 0);
4276 if (PL_opargs[type] & OA_MARK)
4277 first = force_list(first);
4279 NewOp(1101, unop, 1, UNOP);
4280 unop->op_type = (OPCODE)type;
4281 unop->op_ppaddr = PL_ppaddr[type];
4282 unop->op_first = first;
4283 unop->op_flags = (U8)(flags | OPf_KIDS);
4284 unop->op_private = (U8)(1 | (flags >> 8));
4285 unop = (UNOP*) CHECKOP(type, unop);
4289 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4293 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
4295 Constructs, checks, and returns an op of any binary type. I<type>
4296 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
4297 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
4298 the eight bits of C<op_private>, except that the bit with value 1 or
4299 2 is automatically set as required. I<first> and I<last> supply up to
4300 two ops to be the direct children of the binary op; they are consumed
4301 by this function and become part of the constructed op tree.
4307 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4312 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4313 || type == OP_SASSIGN || type == OP_NULL );
4315 NewOp(1101, binop, 1, BINOP);
4318 first = newOP(OP_NULL, 0);
4320 binop->op_type = (OPCODE)type;
4321 binop->op_ppaddr = PL_ppaddr[type];
4322 binop->op_first = first;
4323 binop->op_flags = (U8)(flags | OPf_KIDS);
4326 binop->op_private = (U8)(1 | (flags >> 8));
4329 binop->op_private = (U8)(2 | (flags >> 8));
4330 first->op_sibling = last;
4333 binop = (BINOP*)CHECKOP(type, binop);
4334 if (binop->op_next || binop->op_type != (OPCODE)type)
4337 binop->op_last = binop->op_first->op_sibling;
4339 return fold_constants(op_integerize(op_std_init((OP *)binop)));
4342 static int uvcompare(const void *a, const void *b)
4343 __attribute__nonnull__(1)
4344 __attribute__nonnull__(2)
4345 __attribute__pure__;
4346 static int uvcompare(const void *a, const void *b)
4348 if (*((const UV *)a) < (*(const UV *)b))
4350 if (*((const UV *)a) > (*(const UV *)b))
4352 if (*((const UV *)a+1) < (*(const UV *)b+1))
4354 if (*((const UV *)a+1) > (*(const UV *)b+1))
4360 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4363 SV * const tstr = ((SVOP*)expr)->op_sv;
4366 (repl->op_type == OP_NULL)
4367 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
4369 ((SVOP*)repl)->op_sv;
4372 const U8 *t = (U8*)SvPV_const(tstr, tlen);
4373 const U8 *r = (U8*)SvPV_const(rstr, rlen);
4379 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4380 const I32 squash = o->op_private & OPpTRANS_SQUASH;
4381 I32 del = o->op_private & OPpTRANS_DELETE;
4384 PERL_ARGS_ASSERT_PMTRANS;
4386 PL_hints |= HINT_BLOCK_SCOPE;
4389 o->op_private |= OPpTRANS_FROM_UTF;
4392 o->op_private |= OPpTRANS_TO_UTF;
4394 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4395 SV* const listsv = newSVpvs("# comment\n");
4397 const U8* tend = t + tlen;
4398 const U8* rend = r + rlen;
4412 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4413 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4416 const U32 flags = UTF8_ALLOW_DEFAULT;
4420 t = tsave = bytes_to_utf8(t, &len);
4423 if (!to_utf && rlen) {
4425 r = rsave = bytes_to_utf8(r, &len);
4429 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has
4430 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
4434 U8 tmpbuf[UTF8_MAXBYTES+1];
4437 Newx(cp, 2*tlen, UV);
4439 transv = newSVpvs("");
4441 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4443 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
4445 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4449 cp[2*i+1] = cp[2*i];
4453 qsort(cp, i, 2*sizeof(UV), uvcompare);
4454 for (j = 0; j < i; j++) {
4456 diff = val - nextmin;
4458 t = uvchr_to_utf8(tmpbuf,nextmin);
4459 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4461 U8 range_mark = ILLEGAL_UTF8_BYTE;
4462 t = uvchr_to_utf8(tmpbuf, val - 1);
4463 sv_catpvn(transv, (char *)&range_mark, 1);
4464 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4471 t = uvchr_to_utf8(tmpbuf,nextmin);
4472 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4474 U8 range_mark = ILLEGAL_UTF8_BYTE;
4475 sv_catpvn(transv, (char *)&range_mark, 1);
4477 t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
4478 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4479 t = (const U8*)SvPVX_const(transv);
4480 tlen = SvCUR(transv);
4484 else if (!rlen && !del) {
4485 r = t; rlen = tlen; rend = tend;
4488 if ((!rlen && !del) || t == r ||
4489 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4491 o->op_private |= OPpTRANS_IDENTICAL;
4495 while (t < tend || tfirst <= tlast) {
4496 /* see if we need more "t" chars */
4497 if (tfirst > tlast) {
4498 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4500 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4502 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4509 /* now see if we need more "r" chars */
4510 if (rfirst > rlast) {
4512 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4514 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4516 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4525 rfirst = rlast = 0xffffffff;
4529 /* now see which range will peter our first, if either. */
4530 tdiff = tlast - tfirst;
4531 rdiff = rlast - rfirst;
4538 if (rfirst == 0xffffffff) {
4539 diff = tdiff; /* oops, pretend rdiff is infinite */
4541 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4542 (long)tfirst, (long)tlast);
4544 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4548 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4549 (long)tfirst, (long)(tfirst + diff),
4552 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4553 (long)tfirst, (long)rfirst);
4555 if (rfirst + diff > max)
4556 max = rfirst + diff;
4558 grows = (tfirst < rfirst &&
4559 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4571 else if (max > 0xff)
4576 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4578 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
4579 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4580 PAD_SETSV(cPADOPo->op_padix, swash);
4582 SvREADONLY_on(swash);
4584 cSVOPo->op_sv = swash;
4586 SvREFCNT_dec(listsv);
4587 SvREFCNT_dec(transv);
4589 if (!del && havefinal && rlen)
4590 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4591 newSVuv((UV)final), 0);
4594 o->op_private |= OPpTRANS_GROWS;
4600 op_getmad(expr,o,'e');
4601 op_getmad(repl,o,'r');
4609 tbl = (short*)PerlMemShared_calloc(
4610 (o->op_private & OPpTRANS_COMPLEMENT) &&
4611 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4613 cPVOPo->op_pv = (char*)tbl;
4615 for (i = 0; i < (I32)tlen; i++)
4617 for (i = 0, j = 0; i < 256; i++) {
4619 if (j >= (I32)rlen) {
4628 if (i < 128 && r[j] >= 128)
4638 o->op_private |= OPpTRANS_IDENTICAL;
4640 else if (j >= (I32)rlen)
4645 PerlMemShared_realloc(tbl,
4646 (0x101+rlen-j) * sizeof(short));
4647 cPVOPo->op_pv = (char*)tbl;
4649 tbl[0x100] = (short)(rlen - j);
4650 for (i=0; i < (I32)rlen - j; i++)
4651 tbl[0x101+i] = r[j+i];
4655 if (!rlen && !del) {
4658 o->op_private |= OPpTRANS_IDENTICAL;
4660 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4661 o->op_private |= OPpTRANS_IDENTICAL;
4663 for (i = 0; i < 256; i++)
4665 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4666 if (j >= (I32)rlen) {
4668 if (tbl[t[i]] == -1)
4674 if (tbl[t[i]] == -1) {
4675 if (t[i] < 128 && r[j] >= 128)
4682 if(del && rlen == tlen) {
4683 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4684 } else if(rlen > tlen && !complement) {
4685 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4689 o->op_private |= OPpTRANS_GROWS;
4691 op_getmad(expr,o,'e');
4692 op_getmad(repl,o,'r');
4702 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4704 Constructs, checks, and returns an op of any pattern matching type.
4705 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4706 and, shifted up eight bits, the eight bits of C<op_private>.
4712 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4717 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4719 NewOp(1101, pmop, 1, PMOP);
4720 pmop->op_type = (OPCODE)type;
4721 pmop->op_ppaddr = PL_ppaddr[type];
4722 pmop->op_flags = (U8)flags;
4723 pmop->op_private = (U8)(0 | (flags >> 8));
4725 if (PL_hints & HINT_RE_TAINT)
4726 pmop->op_pmflags |= PMf_RETAINT;
4727 if (IN_LOCALE_COMPILETIME) {
4728 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4730 else if ((! (PL_hints & HINT_BYTES))
4731 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4732 && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4734 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4736 if (PL_hints & HINT_RE_FLAGS) {
4737 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4738 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4740 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4741 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4742 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4744 if (reflags && SvOK(reflags)) {
4745 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4751 assert(SvPOK(PL_regex_pad[0]));
4752 if (SvCUR(PL_regex_pad[0])) {
4753 /* Pop off the "packed" IV from the end. */
4754 SV *const repointer_list = PL_regex_pad[0];
4755 const char *p = SvEND(repointer_list) - sizeof(IV);
4756 const IV offset = *((IV*)p);
4758 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4760 SvEND_set(repointer_list, p);
4762 pmop->op_pmoffset = offset;
4763 /* This slot should be free, so assert this: */
4764 assert(PL_regex_pad[offset] == &PL_sv_undef);
4766 SV * const repointer = &PL_sv_undef;
4767 av_push(PL_regex_padav, repointer);
4768 pmop->op_pmoffset = av_tindex(PL_regex_padav);
4769 PL_regex_pad = AvARRAY(PL_regex_padav);
4773 return CHECKOP(type, pmop);
4776 /* Given some sort of match op o, and an expression expr containing a
4777 * pattern, either compile expr into a regex and attach it to o (if it's
4778 * constant), or convert expr into a runtime regcomp op sequence (if it's
4781 * isreg indicates that the pattern is part of a regex construct, eg
4782 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4783 * split "pattern", which aren't. In the former case, expr will be a list
4784 * if the pattern contains more than one term (eg /a$b/) or if it contains
4785 * a replacement, ie s/// or tr///.
4787 * When the pattern has been compiled within a new anon CV (for
4788 * qr/(?{...})/ ), then floor indicates the savestack level just before
4789 * the new sub was created
4793 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4798 I32 repl_has_vars = 0;
4800 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4801 bool is_compiletime;
4804 PERL_ARGS_ASSERT_PMRUNTIME;
4806 /* for s/// and tr///, last element in list is the replacement; pop it */
4808 if (is_trans || o->op_type == OP_SUBST) {
4810 repl = cLISTOPx(expr)->op_last;
4811 kid = cLISTOPx(expr)->op_first;
4812 while (kid->op_sibling != repl)
4813 kid = kid->op_sibling;
4814 kid->op_sibling = NULL;
4815 cLISTOPx(expr)->op_last = kid;
4818 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4821 OP* const oe = expr;
4822 assert(expr->op_type == OP_LIST);
4823 assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4824 assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4825 expr = cLISTOPx(oe)->op_last;
4826 cLISTOPx(oe)->op_first->op_sibling = NULL;
4827 cLISTOPx(oe)->op_last = NULL;
4830 return pmtrans(o, expr, repl);
4833 /* find whether we have any runtime or code elements;
4834 * at the same time, temporarily set the op_next of each DO block;
4835 * then when we LINKLIST, this will cause the DO blocks to be excluded
4836 * from the op_next chain (and from having LINKLIST recursively
4837 * applied to them). We fix up the DOs specially later */
4841 if (expr->op_type == OP_LIST) {
4843 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4844 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4846 assert(!o->op_next && o->op_sibling);
4847 o->op_next = o->op_sibling;
4849 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4853 else if (expr->op_type != OP_CONST)
4858 /* fix up DO blocks; treat each one as a separate little sub;
4859 * also, mark any arrays as LIST/REF */
4861 if (expr->op_type == OP_LIST) {
4863 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4865 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) {
4866 assert( !(o->op_flags & OPf_WANT));
4867 /* push the array rather than its contents. The regex
4868 * engine will retrieve and join the elements later */
4869 o->op_flags |= (OPf_WANT_LIST | OPf_REF);
4873 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
4875 o->op_next = NULL; /* undo temporary hack from above */
4878 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
4879 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first);
4881 assert(leaveop->op_first->op_type == OP_ENTER);
4882 assert(leaveop->op_first->op_sibling);
4883 o->op_next = leaveop->op_first->op_sibling;
4885 assert(leaveop->op_flags & OPf_KIDS);
4886 assert(leaveop->op_last->op_next == (OP*)leaveop);
4887 leaveop->op_next = NULL; /* stop on last op */
4888 op_null((OP*)leaveop);
4892 OP *scope = cLISTOPo->op_first;
4893 assert(scope->op_type == OP_SCOPE);
4894 assert(scope->op_flags & OPf_KIDS);
4895 scope->op_next = NULL; /* stop on last op */
4898 /* have to peep the DOs individually as we've removed it from
4899 * the op_next chain */
4901 S_prune_chain_head(aTHX_ &(o->op_next));
4903 /* runtime finalizes as part of finalizing whole tree */
4907 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
4908 assert( !(expr->op_flags & OPf_WANT));
4909 /* push the array rather than its contents. The regex
4910 * engine will retrieve and join the elements later */
4911 expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
4914 PL_hints |= HINT_BLOCK_SCOPE;
4916 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
4918 if (is_compiletime) {
4919 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4920 regexp_engine const *eng = current_re_engine();
4922 if (o->op_flags & OPf_SPECIAL)
4923 rx_flags |= RXf_SPLIT;
4925 if (!has_code || !eng->op_comp) {
4926 /* compile-time simple constant pattern */
4928 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
4929 /* whoops! we guessed that a qr// had a code block, but we
4930 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
4931 * that isn't required now. Note that we have to be pretty
4932 * confident that nothing used that CV's pad while the
4933 * regex was parsed */
4934 assert(AvFILLp(PL_comppad) == 0); /* just @_ */
4935 /* But we know that one op is using this CV's slab. */
4936 cv_forget_slab(PL_compcv);
4938 pm->op_pmflags &= ~PMf_HAS_CV;
4943 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4944 rx_flags, pm->op_pmflags)
4945 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4946 rx_flags, pm->op_pmflags)
4949 op_getmad(expr,(OP*)pm,'e');
4955 /* compile-time pattern that includes literal code blocks */
4956 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4959 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
4962 if (pm->op_pmflags & PMf_HAS_CV) {
4964 /* this QR op (and the anon sub we embed it in) is never
4965 * actually executed. It's just a placeholder where we can
4966 * squirrel away expr in op_code_list without the peephole
4967 * optimiser etc processing it for a second time */
4968 OP *qr = newPMOP(OP_QR, 0);
4969 ((PMOP*)qr)->op_code_list = expr;
4971 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
4972 SvREFCNT_inc_simple_void(PL_compcv);
4973 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
4974 ReANY(re)->qr_anoncv = cv;
4976 /* attach the anon CV to the pad so that
4977 * pad_fixup_inner_anons() can find it */
4978 (void)pad_add_anon(cv, o->op_type);
4979 SvREFCNT_inc_simple_void(cv);
4982 pm->op_code_list = expr;
4987 /* runtime pattern: build chain of regcomp etc ops */
4989 PADOFFSET cv_targ = 0;
4991 reglist = isreg && expr->op_type == OP_LIST;
4996 pm->op_code_list = expr;
4997 /* don't free op_code_list; its ops are embedded elsewhere too */
4998 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
5001 if (o->op_flags & OPf_SPECIAL)
5002 pm->op_pmflags |= PMf_SPLIT;
5004 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
5005 * to allow its op_next to be pointed past the regcomp and
5006 * preceding stacking ops;
5007 * OP_REGCRESET is there to reset taint before executing the
5009 if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
5010 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
5012 if (pm->op_pmflags & PMf_HAS_CV) {
5013 /* we have a runtime qr with literal code. This means
5014 * that the qr// has been wrapped in a new CV, which
5015 * means that runtime consts, vars etc will have been compiled
5016 * against a new pad. So... we need to execute those ops
5017 * within the environment of the new CV. So wrap them in a call
5018 * to a new anon sub. i.e. for
5022 * we build an anon sub that looks like
5024 * sub { "a", $b, '(?{...})' }
5026 * and call it, passing the returned list to regcomp.
5027 * Or to put it another way, the list of ops that get executed
5031 * ------ -------------------
5032 * pushmark (for regcomp)
5033 * pushmark (for entersub)
5034 * pushmark (for refgen)
5038 * regcreset regcreset
5040 * const("a") const("a")
5042 * const("(?{...})") const("(?{...})")
5047 SvREFCNT_inc_simple_void(PL_compcv);
5048 /* these lines are just an unrolled newANONATTRSUB */
5049 expr = newSVOP(OP_ANONCODE, 0,
5050 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
5051 cv_targ = expr->op_targ;
5052 expr = newUNOP(OP_REFGEN, 0, expr);
5054 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr))));
5057 NewOp(1101, rcop, 1, LOGOP);
5058 rcop->op_type = OP_REGCOMP;
5059 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
5060 rcop->op_first = scalar(expr);
5061 rcop->op_flags |= OPf_KIDS
5062 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
5063 | (reglist ? OPf_STACKED : 0);
5064 rcop->op_private = 0;
5066 rcop->op_targ = cv_targ;
5068 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
5069 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
5071 /* establish postfix order */
5072 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
5074 rcop->op_next = expr;
5075 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
5078 rcop->op_next = LINKLIST(expr);
5079 expr->op_next = (OP*)rcop;
5082 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
5088 /* If we are looking at s//.../e with a single statement, get past
5089 the implicit do{}. */
5090 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
5091 && cUNOPx(curop)->op_first->op_type == OP_SCOPE
5092 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS) {
5093 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
5094 if (kid->op_type == OP_NULL && kid->op_sibling
5095 && !kid->op_sibling->op_sibling)
5096 curop = kid->op_sibling;
5098 if (curop->op_type == OP_CONST)
5100 else if (( (curop->op_type == OP_RV2SV ||
5101 curop->op_type == OP_RV2AV ||
5102 curop->op_type == OP_RV2HV ||
5103 curop->op_type == OP_RV2GV)
5104 && cUNOPx(curop)->op_first
5105 && cUNOPx(curop)->op_first->op_type == OP_GV )
5106 || curop->op_type == OP_PADSV
5107 || curop->op_type == OP_PADAV
5108 || curop->op_type == OP_PADHV
5109 || curop->op_type == OP_PADANY) {
5117 || !RX_PRELEN(PM_GETRE(pm))
5118 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
5120 pm->op_pmflags |= PMf_CONST; /* const for long enough */
5121 op_prepend_elem(o->op_type, scalar(repl), o);
5124 NewOp(1101, rcop, 1, LOGOP);
5125 rcop->op_type = OP_SUBSTCONT;
5126 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
5127 rcop->op_first = scalar(repl);
5128 rcop->op_flags |= OPf_KIDS;
5129 rcop->op_private = 1;
5132 /* establish postfix order */
5133 rcop->op_next = LINKLIST(repl);
5134 repl->op_next = (OP*)rcop;
5136 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
5137 assert(!(pm->op_pmflags & PMf_ONCE));
5138 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
5147 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
5149 Constructs, checks, and returns an op of any type that involves an
5150 embedded SV. I<type> is the opcode. I<flags> gives the eight bits
5151 of C<op_flags>. I<sv> gives the SV to embed in the op; this function
5152 takes ownership of one reference to it.
5158 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
5163 PERL_ARGS_ASSERT_NEWSVOP;
5165 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
5166 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5167 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
5169 NewOp(1101, svop, 1, SVOP);
5170 svop->op_type = (OPCODE)type;
5171 svop->op_ppaddr = PL_ppaddr[type];
5173 svop->op_next = (OP*)svop;
5174 svop->op_flags = (U8)flags;
5175 svop->op_private = (U8)(0 | (flags >> 8));
5176 if (PL_opargs[type] & OA_RETSCALAR)
5178 if (PL_opargs[type] & OA_TARGET)
5179 svop->op_targ = pad_alloc(type, SVs_PADTMP);
5180 return CHECKOP(type, svop);
5186 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
5188 Constructs, checks, and returns an op of any type that involves a
5189 reference to a pad element. I<type> is the opcode. I<flags> gives the
5190 eight bits of C<op_flags>. A pad slot is automatically allocated, and
5191 is populated with I<sv>; this function takes ownership of one reference
5194 This function only exists if Perl has been compiled to use ithreads.
5200 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
5205 PERL_ARGS_ASSERT_NEWPADOP;
5207 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
5208 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5209 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
5211 NewOp(1101, padop, 1, PADOP);
5212 padop->op_type = (OPCODE)type;
5213 padop->op_ppaddr = PL_ppaddr[type];
5214 padop->op_padix = pad_alloc(type, SVs_PADTMP);
5215 SvREFCNT_dec(PAD_SVl(padop->op_padix));
5216 PAD_SETSV(padop->op_padix, sv);
5218 padop->op_next = (OP*)padop;
5219 padop->op_flags = (U8)flags;
5220 if (PL_opargs[type] & OA_RETSCALAR)
5222 if (PL_opargs[type] & OA_TARGET)
5223 padop->op_targ = pad_alloc(type, SVs_PADTMP);
5224 return CHECKOP(type, padop);
5227 #endif /* USE_ITHREADS */
5230 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
5232 Constructs, checks, and returns an op of any type that involves an
5233 embedded reference to a GV. I<type> is the opcode. I<flags> gives the
5234 eight bits of C<op_flags>. I<gv> identifies the GV that the op should
5235 reference; calling this function does not transfer ownership of any
5242 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
5246 PERL_ARGS_ASSERT_NEWGVOP;
5250 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
5252 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
5257 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
5259 Constructs, checks, and returns an op of any type that involves an
5260 embedded C-level pointer (PV). I<type> is the opcode. I<flags> gives
5261 the eight bits of C<op_flags>. I<pv> supplies the C-level pointer, which
5262 must have been allocated using C<PerlMemShared_malloc>; the memory will
5263 be freed when the op is destroyed.
5269 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
5272 const bool utf8 = cBOOL(flags & SVf_UTF8);
5277 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
5279 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
5281 NewOp(1101, pvop, 1, PVOP);
5282 pvop->op_type = (OPCODE)type;
5283 pvop->op_ppaddr = PL_ppaddr[type];
5285 pvop->op_next = (OP*)pvop;
5286 pvop->op_flags = (U8)flags;
5287 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
5288 if (PL_opargs[type] & OA_RETSCALAR)
5290 if (PL_opargs[type] & OA_TARGET)
5291 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
5292 return CHECKOP(type, pvop);
5300 Perl_package(pTHX_ OP *o)
5303 SV *const sv = cSVOPo->op_sv;
5308 PERL_ARGS_ASSERT_PACKAGE;
5310 SAVEGENERICSV(PL_curstash);
5311 save_item(PL_curstname);
5313 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
5315 sv_setsv(PL_curstname, sv);
5317 PL_hints |= HINT_BLOCK_SCOPE;
5318 PL_parser->copline = NOLINE;
5319 PL_parser->expect = XSTATE;
5324 if (!PL_madskills) {
5329 pegop = newOP(OP_NULL,0);
5330 op_getmad(o,pegop,'P');
5336 Perl_package_version( pTHX_ OP *v )
5339 U32 savehints = PL_hints;
5340 PERL_ARGS_ASSERT_PACKAGE_VERSION;
5341 PL_hints &= ~HINT_STRICT_VARS;
5342 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
5343 PL_hints = savehints;
5352 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
5359 OP *pegop = PL_madskills ? newOP(OP_NULL,0) : NULL;
5361 SV *use_version = NULL;
5363 PERL_ARGS_ASSERT_UTILIZE;
5365 if (idop->op_type != OP_CONST)
5366 Perl_croak(aTHX_ "Module name must be constant");
5369 op_getmad(idop,pegop,'U');
5374 SV * const vesv = ((SVOP*)version)->op_sv;
5377 op_getmad(version,pegop,'V');
5378 if (!arg && !SvNIOKp(vesv)) {
5385 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
5386 Perl_croak(aTHX_ "Version number must be a constant number");
5388 /* Make copy of idop so we don't free it twice */
5389 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
5391 /* Fake up a method call to VERSION */
5392 meth = newSVpvs_share("VERSION");
5393 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
5394 op_append_elem(OP_LIST,
5395 op_prepend_elem(OP_LIST, pack, list(version)),
5396 newSVOP(OP_METHOD_NAMED, 0, meth)));
5400 /* Fake up an import/unimport */
5401 if (arg && arg->op_type == OP_STUB) {
5403 op_getmad(arg,pegop,'S');
5404 imop = arg; /* no import on explicit () */
5406 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
5407 imop = NULL; /* use 5.0; */
5409 use_version = ((SVOP*)idop)->op_sv;
5411 idop->op_private |= OPpCONST_NOVER;
5417 op_getmad(arg,pegop,'A');
5419 /* Make copy of idop so we don't free it twice */
5420 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
5422 /* Fake up a method call to import/unimport */
5424 ? newSVpvs_share("import") : newSVpvs_share("unimport");
5425 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
5426 op_append_elem(OP_LIST,
5427 op_prepend_elem(OP_LIST, pack, list(arg)),
5428 newSVOP(OP_METHOD_NAMED, 0, meth)));
5431 /* Fake up the BEGIN {}, which does its thing immediately. */
5433 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
5436 op_append_elem(OP_LINESEQ,
5437 op_append_elem(OP_LINESEQ,
5438 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
5439 newSTATEOP(0, NULL, veop)),
5440 newSTATEOP(0, NULL, imop) ));
5444 * feature bundle that corresponds to the required version. */
5445 use_version = sv_2mortal(new_version(use_version));
5446 S_enable_feature_bundle(aTHX_ use_version);
5448 /* If a version >= 5.11.0 is requested, strictures are on by default! */
5449 if (vcmp(use_version,
5450 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
5451 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
5452 PL_hints |= HINT_STRICT_REFS;
5453 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
5454 PL_hints |= HINT_STRICT_SUBS;
5455 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
5456 PL_hints |= HINT_STRICT_VARS;
5458 /* otherwise they are off */
5460 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
5461 PL_hints &= ~HINT_STRICT_REFS;
5462 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
5463 PL_hints &= ~HINT_STRICT_SUBS;
5464 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
5465 PL_hints &= ~HINT_STRICT_VARS;
5469 /* The "did you use incorrect case?" warning used to be here.
5470 * The problem is that on case-insensitive filesystems one
5471 * might get false positives for "use" (and "require"):
5472 * "use Strict" or "require CARP" will work. This causes
5473 * portability problems for the script: in case-strict
5474 * filesystems the script will stop working.
5476 * The "incorrect case" warning checked whether "use Foo"
5477 * imported "Foo" to your namespace, but that is wrong, too:
5478 * there is no requirement nor promise in the language that
5479 * a Foo.pm should or would contain anything in package "Foo".
5481 * There is very little Configure-wise that can be done, either:
5482 * the case-sensitivity of the build filesystem of Perl does not
5483 * help in guessing the case-sensitivity of the runtime environment.
5486 PL_hints |= HINT_BLOCK_SCOPE;
5487 PL_parser->copline = NOLINE;
5488 PL_parser->expect = XSTATE;
5489 PL_cop_seqmax++; /* Purely for B::*'s benefit */
5490 if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */
5499 =head1 Embedding Functions
5501 =for apidoc load_module
5503 Loads the module whose name is pointed to by the string part of name.
5504 Note that the actual module name, not its filename, should be given.
5505 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
5506 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
5507 (or 0 for no flags). ver, if specified
5508 and not NULL, provides version semantics
5509 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
5510 arguments can be used to specify arguments to the module's import()
5511 method, similar to C<use Foo::Bar VERSION LIST>. They must be
5512 terminated with a final NULL pointer. Note that this list can only
5513 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
5514 Otherwise at least a single NULL pointer to designate the default
5515 import list is required.
5517 The reference count for each specified C<SV*> parameter is decremented.
5522 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
5526 PERL_ARGS_ASSERT_LOAD_MODULE;
5528 va_start(args, ver);
5529 vload_module(flags, name, ver, &args);
5533 #ifdef PERL_IMPLICIT_CONTEXT
5535 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
5539 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
5540 va_start(args, ver);
5541 vload_module(flags, name, ver, &args);
5547 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
5551 OP * const modname = newSVOP(OP_CONST, 0, name);
5553 PERL_ARGS_ASSERT_VLOAD_MODULE;
5555 modname->op_private |= OPpCONST_BARE;
5557 veop = newSVOP(OP_CONST, 0, ver);
5561 if (flags & PERL_LOADMOD_NOIMPORT) {
5562 imop = sawparens(newNULLLIST());
5564 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
5565 imop = va_arg(*args, OP*);
5570 sv = va_arg(*args, SV*);
5572 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
5573 sv = va_arg(*args, SV*);
5577 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
5578 * that it has a PL_parser to play with while doing that, and also
5579 * that it doesn't mess with any existing parser, by creating a tmp
5580 * new parser with lex_start(). This won't actually be used for much,
5581 * since pp_require() will create another parser for the real work.
5582 * The ENTER/LEAVE pair protect callers from any side effects of use. */
5585 SAVEVPTR(PL_curcop);
5586 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
5587 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
5588 veop, modname, imop);
5592 PERL_STATIC_INLINE OP *
5593 S_new_entersubop(pTHX_ GV *gv, OP *arg)
5595 return newUNOP(OP_ENTERSUB, OPf_STACKED,
5596 newLISTOP(OP_LIST, 0, arg,
5597 newUNOP(OP_RV2CV, 0,
5598 newGVOP(OP_GV, 0, gv))));
5602 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
5608 PERL_ARGS_ASSERT_DOFILE;
5610 if (!force_builtin && (gv = gv_override("do", 2))) {
5611 doop = S_new_entersubop(aTHX_ gv, term);
5614 doop = newUNOP(OP_DOFILE, 0, scalar(term));
5620 =head1 Optree construction
5622 =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval
5624 Constructs, checks, and returns an C<lslice> (list slice) op. I<flags>
5625 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
5626 be set automatically, and, shifted up eight bits, the eight bits of
5627 C<op_private>, except that the bit with value 1 or 2 is automatically
5628 set as required. I<listval> and I<subscript> supply the parameters of
5629 the slice; they are consumed by this function and become part of the
5630 constructed op tree.
5636 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
5638 return newBINOP(OP_LSLICE, flags,
5639 list(force_list(subscript)),
5640 list(force_list(listval)) );
5644 S_is_list_assignment(pTHX_ const OP *o)
5652 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
5653 o = cUNOPo->op_first;
5655 flags = o->op_flags;
5657 if (type == OP_COND_EXPR) {
5658 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
5659 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
5664 yyerror("Assignment to both a list and a scalar");
5668 if (type == OP_LIST &&
5669 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
5670 o->op_private & OPpLVAL_INTRO)
5673 if (type == OP_LIST || flags & OPf_PARENS ||
5674 type == OP_RV2AV || type == OP_RV2HV ||
5675 type == OP_ASLICE || type == OP_HSLICE ||
5676 type == OP_KVASLICE || type == OP_KVHSLICE)
5679 if (type == OP_PADAV || type == OP_PADHV)
5682 if (type == OP_RV2SV)
5689 Helper function for newASSIGNOP to detection commonality between the
5690 lhs and the rhs. Marks all variables with PL_generation. If it
5691 returns TRUE the assignment must be able to handle common variables.
5693 PERL_STATIC_INLINE bool
5694 S_aassign_common_vars(pTHX_ OP* o)
5697 for (curop = cUNOPo->op_first; curop; curop=curop->op_sibling) {
5698 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
5699 if (curop->op_type == OP_GV) {
5700 GV *gv = cGVOPx_gv(curop);
5702 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
5704 GvASSIGN_GENERATION_set(gv, PL_generation);
5706 else if (curop->op_type == OP_PADSV ||
5707 curop->op_type == OP_PADAV ||
5708 curop->op_type == OP_PADHV ||
5709 curop->op_type == OP_PADANY)
5711 if (PAD_COMPNAME_GEN(curop->op_targ)
5712 == (STRLEN)PL_generation)
5714 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
5717 else if (curop->op_type == OP_RV2CV)
5719 else if (curop->op_type == OP_RV2SV ||
5720 curop->op_type == OP_RV2AV ||
5721 curop->op_type == OP_RV2HV ||
5722 curop->op_type == OP_RV2GV) {
5723 if (cUNOPx(curop)->op_first->op_type != OP_GV) /* funny deref? */
5726 else if (curop->op_type == OP_PUSHRE) {
5729 ((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff
5730 ? MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff))
5733 ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv;
5737 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
5739 GvASSIGN_GENERATION_set(gv, PL_generation);
5746 if (curop->op_flags & OPf_KIDS) {
5747 if (aassign_common_vars(curop))
5755 =for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right
5757 Constructs, checks, and returns an assignment op. I<left> and I<right>
5758 supply the parameters of the assignment; they are consumed by this
5759 function and become part of the constructed op tree.
5761 If I<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
5762 a suitable conditional optree is constructed. If I<optype> is the opcode
5763 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
5764 performs the binary operation and assigns the result to the left argument.
5765 Either way, if I<optype> is non-zero then I<flags> has no effect.
5767 If I<optype> is zero, then a plain scalar or list assignment is
5768 constructed. Which type of assignment it is is automatically determined.
5769 I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
5770 will be set automatically, and, shifted up eight bits, the eight bits
5771 of C<op_private>, except that the bit with value 1 or 2 is automatically
5778 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
5784 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
5785 return newLOGOP(optype, 0,
5786 op_lvalue(scalar(left), optype),
5787 newUNOP(OP_SASSIGN, 0, scalar(right)));
5790 return newBINOP(optype, OPf_STACKED,
5791 op_lvalue(scalar(left), optype), scalar(right));
5795 if (is_list_assignment(left)) {
5796 static const char no_list_state[] = "Initialization of state variables"
5797 " in list context currently forbidden";
5799 bool maybe_common_vars = TRUE;
5801 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE)
5802 left->op_private &= ~ OPpSLICEWARNING;
5805 left = op_lvalue(left, OP_AASSIGN);
5806 curop = list(force_list(left));
5807 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
5808 o->op_private = (U8)(0 | (flags >> 8));
5810 if (OP_TYPE_IS_OR_WAS(left, OP_LIST))
5812 OP* lop = ((LISTOP*)left)->op_first;
5813 maybe_common_vars = FALSE;
5815 if (lop->op_type == OP_PADSV ||
5816 lop->op_type == OP_PADAV ||
5817 lop->op_type == OP_PADHV ||
5818 lop->op_type == OP_PADANY) {
5819 if (!(lop->op_private & OPpLVAL_INTRO))
5820 maybe_common_vars = TRUE;
5822 if (lop->op_private & OPpPAD_STATE) {
5823 if (left->op_private & OPpLVAL_INTRO) {
5824 /* Each variable in state($a, $b, $c) = ... */
5827 /* Each state variable in
5828 (state $a, my $b, our $c, $d, undef) = ... */
5830 yyerror(no_list_state);
5832 /* Each my variable in
5833 (state $a, my $b, our $c, $d, undef) = ... */
5835 } else if (lop->op_type == OP_UNDEF ||
5836 OP_TYPE_IS_OR_WAS(lop, OP_PUSHMARK)) {
5837 /* undef may be interesting in
5838 (state $a, undef, state $c) */
5840 /* Other ops in the list. */
5841 maybe_common_vars = TRUE;
5843 lop = lop->op_sibling;
5846 else if ((left->op_private & OPpLVAL_INTRO)
5847 && ( left->op_type == OP_PADSV
5848 || left->op_type == OP_PADAV
5849 || left->op_type == OP_PADHV
5850 || left->op_type == OP_PADANY))
5852 if (left->op_type == OP_PADSV) maybe_common_vars = FALSE;
5853 if (left->op_private & OPpPAD_STATE) {
5854 /* All single variable list context state assignments, hence
5864 yyerror(no_list_state);
5868 /* PL_generation sorcery:
5869 * an assignment like ($a,$b) = ($c,$d) is easier than
5870 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
5871 * To detect whether there are common vars, the global var
5872 * PL_generation is incremented for each assign op we compile.
5873 * Then, while compiling the assign op, we run through all the
5874 * variables on both sides of the assignment, setting a spare slot
5875 * in each of them to PL_generation. If any of them already have
5876 * that value, we know we've got commonality. We could use a
5877 * single bit marker, but then we'd have to make 2 passes, first
5878 * to clear the flag, then to test and set it. To find somewhere
5879 * to store these values, evil chicanery is done with SvUVX().
5882 if (maybe_common_vars) {
5884 if (aassign_common_vars(o))
5885 o->op_private |= OPpASSIGN_COMMON;
5889 if (right && right->op_type == OP_SPLIT && !PL_madskills) {
5890 OP* tmpop = ((LISTOP*)right)->op_first;
5891 if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
5892 PMOP * const pm = (PMOP*)tmpop;
5893 if (left->op_type == OP_RV2AV &&
5894 !(left->op_private & OPpLVAL_INTRO) &&
5895 !(o->op_private & OPpASSIGN_COMMON) )
5897 tmpop = ((UNOP*)left)->op_first;
5898 if (tmpop->op_type == OP_GV
5900 && !pm->op_pmreplrootu.op_pmtargetoff
5902 && !pm->op_pmreplrootu.op_pmtargetgv
5906 pm->op_pmreplrootu.op_pmtargetoff
5907 = cPADOPx(tmpop)->op_padix;
5908 cPADOPx(tmpop)->op_padix = 0; /* steal it */
5910 pm->op_pmreplrootu.op_pmtargetgv
5911 = MUTABLE_GV(cSVOPx(tmpop)->op_sv);
5912 cSVOPx(tmpop)->op_sv = NULL; /* steal it */
5914 tmpop = cUNOPo->op_first; /* to list (nulled) */
5915 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
5916 tmpop->op_sibling = NULL; /* don't free split */
5917 right->op_next = tmpop->op_next; /* fix starting loc */
5918 op_free(o); /* blow off assign */
5919 right->op_flags &= ~OPf_WANT;
5920 /* "I don't know and I don't care." */
5925 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
5926 ((LISTOP*)right)->op_last->op_type == OP_CONST)
5929 &((SVOP*)((LISTOP*)right)->op_last)->op_sv;
5930 SV * const sv = *svp;
5931 if (SvIOK(sv) && SvIVX(sv) == 0)
5933 if (right->op_private & OPpSPLIT_IMPLIM) {
5934 /* our own SV, created in ck_split */
5936 sv_setiv(sv, PL_modcount+1);
5939 /* SV may belong to someone else */
5941 *svp = newSViv(PL_modcount+1);
5951 right = newOP(OP_UNDEF, 0);
5952 if (right->op_type == OP_READLINE) {
5953 right->op_flags |= OPf_STACKED;
5954 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
5958 o = newBINOP(OP_SASSIGN, flags,
5959 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
5965 =for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o
5967 Constructs a state op (COP). The state op is normally a C<nextstate> op,
5968 but will be a C<dbstate> op if debugging is enabled for currently-compiled
5969 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>).
5970 If I<label> is non-null, it supplies the name of a label to attach to
5971 the state op; this function takes ownership of the memory pointed at by
5972 I<label>, and will free it. I<flags> gives the eight bits of C<op_flags>
5975 If I<o> is null, the state op is returned. Otherwise the state op is
5976 combined with I<o> into a C<lineseq> list op, which is returned. I<o>
5977 is consumed by this function and becomes part of the returned op tree.
5983 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
5986 const U32 seq = intro_my();
5987 const U32 utf8 = flags & SVf_UTF8;
5992 NewOp(1101, cop, 1, COP);
5993 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
5994 cop->op_type = OP_DBSTATE;
5995 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
5998 cop->op_type = OP_NEXTSTATE;
5999 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
6001 cop->op_flags = (U8)flags;
6002 CopHINTS_set(cop, PL_hints);
6004 cop->op_private |= NATIVE_HINTS;
6007 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH;
6009 cop->op_next = (OP*)cop;
6012 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
6013 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
6015 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
6017 PL_hints |= HINT_BLOCK_SCOPE;
6018 /* It seems that we need to defer freeing this pointer, as other parts
6019 of the grammar end up wanting to copy it after this op has been
6024 if (PL_parser->preambling != NOLINE) {
6025 CopLINE_set(cop, PL_parser->preambling);
6026 PL_parser->copline = NOLINE;
6028 else if (PL_parser->copline == NOLINE)
6029 CopLINE_set(cop, CopLINE(PL_curcop));
6031 CopLINE_set(cop, PL_parser->copline);
6032 PL_parser->copline = NOLINE;
6035 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
6037 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
6039 CopSTASH_set(cop, PL_curstash);
6041 if (cop->op_type == OP_DBSTATE) {
6042 /* this line can have a breakpoint - store the cop in IV */
6043 AV *av = CopFILEAVx(PL_curcop);
6045 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
6046 if (svp && *svp != &PL_sv_undef ) {
6047 (void)SvIOK_on(*svp);
6048 SvIV_set(*svp, PTR2IV(cop));
6053 if (flags & OPf_SPECIAL)
6055 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
6059 =for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other
6061 Constructs, checks, and returns a logical (flow control) op. I<type>
6062 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
6063 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
6064 the eight bits of C<op_private>, except that the bit with value 1 is
6065 automatically set. I<first> supplies the expression controlling the
6066 flow, and I<other> supplies the side (alternate) chain of ops; they are
6067 consumed by this function and become part of the constructed op tree.
6073 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
6077 PERL_ARGS_ASSERT_NEWLOGOP;
6079 return new_logop(type, flags, &first, &other);
6083 S_search_const(pTHX_ OP *o)
6085 PERL_ARGS_ASSERT_SEARCH_CONST;
6087 switch (o->op_type) {
6091 if (o->op_flags & OPf_KIDS)
6092 return search_const(cUNOPo->op_first);
6099 if (!(o->op_flags & OPf_KIDS))
6101 kid = cLISTOPo->op_first;
6103 switch (kid->op_type) {
6107 kid = kid->op_sibling;
6110 if (kid != cLISTOPo->op_last)
6116 kid = cLISTOPo->op_last;
6118 return search_const(kid);
6126 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
6134 int prepend_not = 0;
6136 PERL_ARGS_ASSERT_NEW_LOGOP;
6141 /* [perl #59802]: Warn about things like "return $a or $b", which
6142 is parsed as "(return $a) or $b" rather than "return ($a or
6143 $b)". NB: This also applies to xor, which is why we do it
6146 switch (first->op_type) {
6150 /* XXX: Perhaps we should emit a stronger warning for these.
6151 Even with the high-precedence operator they don't seem to do
6154 But until we do, fall through here.
6160 /* XXX: Currently we allow people to "shoot themselves in the
6161 foot" by explicitly writing "(return $a) or $b".
6163 Warn unless we are looking at the result from folding or if
6164 the programmer explicitly grouped the operators like this.
6165 The former can occur with e.g.
6167 use constant FEATURE => ( $] >= ... );
6168 sub { not FEATURE and return or do_stuff(); }
6170 if (!first->op_folded && !(first->op_flags & OPf_PARENS))
6171 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
6172 "Possible precedence issue with control flow operator");
6173 /* XXX: Should we optimze this to "return $a;" (i.e. remove
6179 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
6180 return newBINOP(type, flags, scalar(first), scalar(other));
6182 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP);
6184 scalarboolean(first);
6185 /* optimize AND and OR ops that have NOTs as children */
6186 if (first->op_type == OP_NOT
6187 && (first->op_flags & OPf_KIDS)
6188 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
6189 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
6191 if (type == OP_AND || type == OP_OR) {
6197 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
6199 prepend_not = 1; /* prepend a NOT op later */
6203 /* search for a constant op that could let us fold the test */
6204 if ((cstop = search_const(first))) {
6205 if (cstop->op_private & OPpCONST_STRICT)
6206 no_bareword_allowed(cstop);
6207 else if ((cstop->op_private & OPpCONST_BARE))
6208 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
6209 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) ||
6210 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
6211 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
6213 if (other->op_type == OP_CONST)
6214 other->op_private |= OPpCONST_SHORTCIRCUIT;
6216 OP *newop = newUNOP(OP_NULL, 0, other);
6217 op_getmad(first, newop, '1');
6218 newop->op_targ = type; /* set "was" field */
6222 if (other->op_type == OP_LEAVE)
6223 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
6224 else if (other->op_type == OP_MATCH
6225 || other->op_type == OP_SUBST
6226 || other->op_type == OP_TRANSR
6227 || other->op_type == OP_TRANS)
6228 /* Mark the op as being unbindable with =~ */
6229 other->op_flags |= OPf_SPECIAL;
6231 other->op_folded = 1;
6235 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
6236 const OP *o2 = other;
6237 if ( ! (o2->op_type == OP_LIST
6238 && (( o2 = cUNOPx(o2)->op_first))
6239 && o2->op_type == OP_PUSHMARK
6240 && (( o2 = o2->op_sibling)) )
6243 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
6244 || o2->op_type == OP_PADHV)
6245 && o2->op_private & OPpLVAL_INTRO
6246 && !(o2->op_private & OPpPAD_STATE))
6248 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
6249 "Deprecated use of my() in false conditional");
6253 if (cstop->op_type == OP_CONST)
6254 cstop->op_private |= OPpCONST_SHORTCIRCUIT;
6256 first = newUNOP(OP_NULL, 0, first);
6257 op_getmad(other, first, '2');
6258 first->op_targ = type; /* set "was" field */
6265 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
6266 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
6268 const OP * const k1 = ((UNOP*)first)->op_first;
6269 const OP * const k2 = k1->op_sibling;
6271 switch (first->op_type)
6274 if (k2 && k2->op_type == OP_READLINE
6275 && (k2->op_flags & OPf_STACKED)
6276 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6278 warnop = k2->op_type;
6283 if (k1->op_type == OP_READDIR
6284 || k1->op_type == OP_GLOB
6285 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6286 || k1->op_type == OP_EACH
6287 || k1->op_type == OP_AEACH)
6289 warnop = ((k1->op_type == OP_NULL)
6290 ? (OPCODE)k1->op_targ : k1->op_type);
6295 const line_t oldline = CopLINE(PL_curcop);
6296 /* This ensures that warnings are reported at the first line
6297 of the construction, not the last. */
6298 CopLINE_set(PL_curcop, PL_parser->copline);
6299 Perl_warner(aTHX_ packWARN(WARN_MISC),
6300 "Value of %s%s can be \"0\"; test with defined()",
6302 ((warnop == OP_READLINE || warnop == OP_GLOB)
6303 ? " construct" : "() operator"));
6304 CopLINE_set(PL_curcop, oldline);
6311 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
6312 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
6314 NewOp(1101, logop, 1, LOGOP);
6316 logop->op_type = (OPCODE)type;
6317 logop->op_ppaddr = PL_ppaddr[type];
6318 logop->op_first = first;
6319 logop->op_flags = (U8)(flags | OPf_KIDS);
6320 logop->op_other = LINKLIST(other);
6321 logop->op_private = (U8)(1 | (flags >> 8));
6323 /* establish postfix order */
6324 logop->op_next = LINKLIST(first);
6325 first->op_next = (OP*)logop;
6326 first->op_sibling = other;
6328 CHECKOP(type,logop);
6330 o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 0, (OP*)logop);
6337 =for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop
6339 Constructs, checks, and returns a conditional-expression (C<cond_expr>)
6340 op. I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
6341 will be set automatically, and, shifted up eight bits, the eight bits of
6342 C<op_private>, except that the bit with value 1 is automatically set.
6343 I<first> supplies the expression selecting between the two branches,
6344 and I<trueop> and I<falseop> supply the branches; they are consumed by
6345 this function and become part of the constructed op tree.
6351 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
6359 PERL_ARGS_ASSERT_NEWCONDOP;
6362 return newLOGOP(OP_AND, 0, first, trueop);
6364 return newLOGOP(OP_OR, 0, first, falseop);
6366 scalarboolean(first);
6367 if ((cstop = search_const(first))) {
6368 /* Left or right arm of the conditional? */
6369 const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
6370 OP *live = left ? trueop : falseop;
6371 OP *const dead = left ? falseop : trueop;
6372 if (cstop->op_private & OPpCONST_BARE &&
6373 cstop->op_private & OPpCONST_STRICT) {
6374 no_bareword_allowed(cstop);
6377 /* This is all dead code when PERL_MAD is not defined. */
6378 live = newUNOP(OP_NULL, 0, live);
6379 op_getmad(first, live, 'C');
6380 op_getmad(dead, live, left ? 'e' : 't');
6385 if (live->op_type == OP_LEAVE)
6386 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
6387 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
6388 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
6389 /* Mark the op as being unbindable with =~ */
6390 live->op_flags |= OPf_SPECIAL;
6391 live->op_folded = 1;
6394 NewOp(1101, logop, 1, LOGOP);
6395 logop->op_type = OP_COND_EXPR;
6396 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
6397 logop->op_first = first;
6398 logop->op_flags = (U8)(flags | OPf_KIDS);
6399 logop->op_private = (U8)(1 | (flags >> 8));
6400 logop->op_other = LINKLIST(trueop);
6401 logop->op_next = LINKLIST(falseop);
6403 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
6406 /* establish postfix order */
6407 start = LINKLIST(first);
6408 first->op_next = (OP*)logop;
6410 first->op_sibling = trueop;
6411 trueop->op_sibling = falseop;
6412 o = newUNOP(OP_NULL, 0, (OP*)logop);
6414 trueop->op_next = falseop->op_next = o;
6421 =for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right
6423 Constructs and returns a C<range> op, with subordinate C<flip> and
6424 C<flop> ops. I<flags> gives the eight bits of C<op_flags> for the
6425 C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
6426 for both the C<flip> and C<range> ops, except that the bit with value
6427 1 is automatically set. I<left> and I<right> supply the expressions
6428 controlling the endpoints of the range; they are consumed by this function
6429 and become part of the constructed op tree.
6435 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
6444 PERL_ARGS_ASSERT_NEWRANGE;
6446 NewOp(1101, range, 1, LOGOP);
6448 range->op_type = OP_RANGE;
6449 range->op_ppaddr = PL_ppaddr[OP_RANGE];
6450 range->op_first = left;
6451 range->op_flags = OPf_KIDS;
6452 leftstart = LINKLIST(left);
6453 range->op_other = LINKLIST(right);
6454 range->op_private = (U8)(1 | (flags >> 8));
6456 left->op_sibling = right;
6458 range->op_next = (OP*)range;
6459 flip = newUNOP(OP_FLIP, flags, (OP*)range);
6460 flop = newUNOP(OP_FLOP, 0, flip);
6461 o = newUNOP(OP_NULL, 0, flop);
6463 range->op_next = leftstart;
6465 left->op_next = flip;
6466 right->op_next = flop;
6468 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
6469 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
6470 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
6471 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
6473 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6474 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6476 /* check barewords before they might be optimized aways */
6477 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
6478 no_bareword_allowed(left);
6479 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
6480 no_bareword_allowed(right);
6483 if (!flip->op_private || !flop->op_private)
6484 LINKLIST(o); /* blow off optimizer unless constant */
6490 =for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block
6492 Constructs, checks, and returns an op tree expressing a loop. This is
6493 only a loop in the control flow through the op tree; it does not have
6494 the heavyweight loop structure that allows exiting the loop by C<last>
6495 and suchlike. I<flags> gives the eight bits of C<op_flags> for the
6496 top-level op, except that some bits will be set automatically as required.
6497 I<expr> supplies the expression controlling loop iteration, and I<block>
6498 supplies the body of the loop; they are consumed by this function and
6499 become part of the constructed op tree. I<debuggable> is currently
6500 unused and should always be 1.
6506 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
6511 const bool once = block && block->op_flags & OPf_SPECIAL &&
6512 block->op_type == OP_NULL;
6514 PERL_UNUSED_ARG(debuggable);
6518 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
6519 || ( expr->op_type == OP_NOT
6520 && cUNOPx(expr)->op_first->op_type == OP_CONST
6521 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first))
6524 /* Return the block now, so that S_new_logop does not try to
6526 return block; /* do {} while 0 does once */
6527 if (expr->op_type == OP_READLINE
6528 || expr->op_type == OP_READDIR
6529 || expr->op_type == OP_GLOB
6530 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
6531 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
6532 expr = newUNOP(OP_DEFINED, 0,
6533 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
6534 } else if (expr->op_flags & OPf_KIDS) {
6535 const OP * const k1 = ((UNOP*)expr)->op_first;
6536 const OP * const k2 = k1 ? k1->op_sibling : NULL;
6537 switch (expr->op_type) {
6539 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
6540 && (k2->op_flags & OPf_STACKED)
6541 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6542 expr = newUNOP(OP_DEFINED, 0, expr);
6546 if (k1 && (k1->op_type == OP_READDIR
6547 || k1->op_type == OP_GLOB
6548 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6549 || k1->op_type == OP_EACH
6550 || k1->op_type == OP_AEACH))
6551 expr = newUNOP(OP_DEFINED, 0, expr);
6557 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
6558 * op, in listop. This is wrong. [perl #27024] */
6560 block = newOP(OP_NULL, 0);
6561 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
6562 o = new_logop(OP_AND, 0, &expr, &listop);
6569 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
6571 if (once && o != listop)
6573 assert(cUNOPo->op_first->op_type == OP_AND
6574 || cUNOPo->op_first->op_type == OP_OR);
6575 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
6579 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
6581 o->op_flags |= flags;
6583 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
6588 =for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my
6590 Constructs, checks, and returns an op tree expressing a C<while> loop.
6591 This is a heavyweight loop, with structure that allows exiting the loop
6592 by C<last> and suchlike.
6594 I<loop> is an optional preconstructed C<enterloop> op to use in the
6595 loop; if it is null then a suitable op will be constructed automatically.
6596 I<expr> supplies the loop's controlling expression. I<block> supplies the
6597 main body of the loop, and I<cont> optionally supplies a C<continue> block
6598 that operates as a second half of the body. All of these optree inputs
6599 are consumed by this function and become part of the constructed op tree.
6601 I<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
6602 op and, shifted up eight bits, the eight bits of C<op_private> for
6603 the C<leaveloop> op, except that (in both cases) some bits will be set
6604 automatically. I<debuggable> is currently unused and should always be 1.
6605 I<has_my> can be supplied as true to force the
6606 loop body to be enclosed in its own scope.
6612 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
6613 OP *expr, OP *block, OP *cont, I32 has_my)
6622 PERL_UNUSED_ARG(debuggable);
6625 if (expr->op_type == OP_READLINE
6626 || expr->op_type == OP_READDIR
6627 || expr->op_type == OP_GLOB
6628 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
6629 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
6630 expr = newUNOP(OP_DEFINED, 0,
6631 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
6632 } else if (expr->op_flags & OPf_KIDS) {
6633 const OP * const k1 = ((UNOP*)expr)->op_first;
6634 const OP * const k2 = (k1) ? k1->op_sibling : NULL;
6635 switch (expr->op_type) {
6637 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
6638 && (k2->op_flags & OPf_STACKED)
6639 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6640 expr = newUNOP(OP_DEFINED, 0, expr);
6644 if (k1 && (k1->op_type == OP_READDIR
6645 || k1->op_type == OP_GLOB
6646 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6647 || k1->op_type == OP_EACH
6648 || k1->op_type == OP_AEACH))
6649 expr = newUNOP(OP_DEFINED, 0, expr);
6656 block = newOP(OP_NULL, 0);
6657 else if (cont || has_my) {
6658 block = op_scope(block);
6662 next = LINKLIST(cont);
6665 OP * const unstack = newOP(OP_UNSTACK, 0);
6668 cont = op_append_elem(OP_LINESEQ, cont, unstack);
6672 listop = op_append_list(OP_LINESEQ, block, cont);
6674 redo = LINKLIST(listop);
6678 o = new_logop(OP_AND, 0, &expr, &listop);
6679 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
6681 return expr; /* listop already freed by new_logop */
6684 ((LISTOP*)listop)->op_last->op_next =
6685 (o == listop ? redo : LINKLIST(o));
6691 NewOp(1101,loop,1,LOOP);
6692 loop->op_type = OP_ENTERLOOP;
6693 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
6694 loop->op_private = 0;
6695 loop->op_next = (OP*)loop;
6698 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
6700 loop->op_redoop = redo;
6701 loop->op_lastop = o;
6702 o->op_private |= loopflags;
6705 loop->op_nextop = next;
6707 loop->op_nextop = o;
6709 o->op_flags |= flags;
6710 o->op_private |= (flags >> 8);
6715 =for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont
6717 Constructs, checks, and returns an op tree expressing a C<foreach>
6718 loop (iteration through a list of values). This is a heavyweight loop,
6719 with structure that allows exiting the loop by C<last> and suchlike.
6721 I<sv> optionally supplies the variable that will be aliased to each
6722 item in turn; if null, it defaults to C<$_> (either lexical or global).
6723 I<expr> supplies the list of values to iterate over. I<block> supplies
6724 the main body of the loop, and I<cont> optionally supplies a C<continue>
6725 block that operates as a second half of the body. All of these optree
6726 inputs are consumed by this function and become part of the constructed
6729 I<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
6730 op and, shifted up eight bits, the eight bits of C<op_private> for
6731 the C<leaveloop> op, except that (in both cases) some bits will be set
6738 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
6743 PADOFFSET padoff = 0;
6748 PERL_ARGS_ASSERT_NEWFOROP;
6751 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
6752 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
6753 sv->op_type = OP_RV2GV;
6754 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
6756 /* The op_type check is needed to prevent a possible segfault
6757 * if the loop variable is undeclared and 'strict vars' is in
6758 * effect. This is illegal but is nonetheless parsed, so we
6759 * may reach this point with an OP_CONST where we're expecting
6762 if (cUNOPx(sv)->op_first->op_type == OP_GV
6763 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
6764 iterpflags |= OPpITER_DEF;
6766 else if (sv->op_type == OP_PADSV) { /* private variable */
6767 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
6768 padoff = sv->op_targ;
6778 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
6780 SV *const namesv = PAD_COMPNAME_SV(padoff);
6782 const char *const name = SvPV_const(namesv, len);
6784 if (len == 2 && name[0] == '$' && name[1] == '_')
6785 iterpflags |= OPpITER_DEF;
6789 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
6790 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
6791 sv = newGVOP(OP_GV, 0, PL_defgv);
6796 iterpflags |= OPpITER_DEF;
6798 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
6799 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
6800 iterflags |= OPf_STACKED;
6802 else if (expr->op_type == OP_NULL &&
6803 (expr->op_flags & OPf_KIDS) &&
6804 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
6806 /* Basically turn for($x..$y) into the same as for($x,$y), but we
6807 * set the STACKED flag to indicate that these values are to be
6808 * treated as min/max values by 'pp_enteriter'.
6810 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
6811 LOGOP* const range = (LOGOP*) flip->op_first;
6812 OP* const left = range->op_first;
6813 OP* const right = left->op_sibling;
6816 range->op_flags &= ~OPf_KIDS;
6817 range->op_first = NULL;
6819 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
6820 listop->op_first->op_next = range->op_next;
6821 left->op_next = range->op_other;
6822 right->op_next = (OP*)listop;
6823 listop->op_next = listop->op_first;
6826 op_getmad(expr,(OP*)listop,'O');
6830 expr = (OP*)(listop);
6832 iterflags |= OPf_STACKED;
6835 expr = op_lvalue(force_list(expr), OP_GREPSTART);
6838 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
6839 op_append_elem(OP_LIST, expr, scalar(sv))));
6840 assert(!loop->op_next);
6841 /* for my $x () sets OPpLVAL_INTRO;
6842 * for our $x () sets OPpOUR_INTRO */
6843 loop->op_private = (U8)iterpflags;
6844 if (loop->op_slabbed
6845 && DIFF(loop, OpSLOT(loop)->opslot_next)
6846 < SIZE_TO_PSIZE(sizeof(LOOP)))
6849 NewOp(1234,tmp,1,LOOP);
6850 Copy(loop,tmp,1,LISTOP);
6851 S_op_destroy(aTHX_ (OP*)loop);
6854 else if (!loop->op_slabbed)
6855 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
6856 loop->op_targ = padoff;
6857 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0);
6859 op_getmad(madsv, (OP*)loop, 'v');
6864 =for apidoc Am|OP *|newLOOPEX|I32 type|OP *label
6866 Constructs, checks, and returns a loop-exiting op (such as C<goto>
6867 or C<last>). I<type> is the opcode. I<label> supplies the parameter
6868 determining the target of the op; it is consumed by this function and
6869 becomes part of the constructed op tree.
6875 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
6880 PERL_ARGS_ASSERT_NEWLOOPEX;
6882 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
6884 if (type != OP_GOTO) {
6885 /* "last()" means "last" */
6886 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) {
6887 o = newOP(type, OPf_SPECIAL);
6891 /* Check whether it's going to be a goto &function */
6892 if (label->op_type == OP_ENTERSUB
6893 && !(label->op_flags & OPf_STACKED))
6894 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
6897 /* Check for a constant argument */
6898 if (label->op_type == OP_CONST) {
6899 SV * const sv = ((SVOP *)label)->op_sv;
6901 const char *s = SvPV_const(sv,l);
6902 if (l == strlen(s)) {
6904 SvUTF8(((SVOP*)label)->op_sv),
6906 SvPV_nolen_const(((SVOP*)label)->op_sv)));
6910 /* If we have already created an op, we do not need the label. */
6913 op_getmad(label,o,'L');
6917 else o = newUNOP(type, OPf_STACKED, label);
6919 PL_hints |= HINT_BLOCK_SCOPE;
6923 /* if the condition is a literal array or hash
6924 (or @{ ... } etc), make a reference to it.
6927 S_ref_array_or_hash(pTHX_ OP *cond)
6930 && (cond->op_type == OP_RV2AV
6931 || cond->op_type == OP_PADAV
6932 || cond->op_type == OP_RV2HV
6933 || cond->op_type == OP_PADHV))
6935 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
6938 && (cond->op_type == OP_ASLICE
6939 || cond->op_type == OP_KVASLICE
6940 || cond->op_type == OP_HSLICE
6941 || cond->op_type == OP_KVHSLICE)) {
6943 /* anonlist now needs a list from this op, was previously used in
6945 cond->op_flags |= ~(OPf_WANT_SCALAR | OPf_REF);
6946 cond->op_flags |= OPf_WANT_LIST;
6948 return newANONLIST(op_lvalue(cond, OP_ANONLIST));
6955 /* These construct the optree fragments representing given()
6958 entergiven and enterwhen are LOGOPs; the op_other pointer
6959 points up to the associated leave op. We need this so we
6960 can put it in the context and make break/continue work.
6961 (Also, of course, pp_enterwhen will jump straight to
6962 op_other if the match fails.)
6966 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
6967 I32 enter_opcode, I32 leave_opcode,
6968 PADOFFSET entertarg)
6974 PERL_ARGS_ASSERT_NEWGIVWHENOP;
6976 NewOp(1101, enterop, 1, LOGOP);
6977 enterop->op_type = (Optype)enter_opcode;
6978 enterop->op_ppaddr = PL_ppaddr[enter_opcode];
6979 enterop->op_flags = (U8) OPf_KIDS;
6980 enterop->op_targ = ((entertarg == NOT_IN_PAD) ? 0 : entertarg);
6981 enterop->op_private = 0;
6983 o = newUNOP(leave_opcode, 0, (OP *) enterop);
6986 enterop->op_first = scalar(cond);
6987 cond->op_sibling = block;
6989 o->op_next = LINKLIST(cond);
6990 cond->op_next = (OP *) enterop;
6993 /* This is a default {} block */
6994 enterop->op_first = block;
6995 enterop->op_flags |= OPf_SPECIAL;
6996 o ->op_flags |= OPf_SPECIAL;
6998 o->op_next = (OP *) enterop;
7001 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
7002 entergiven and enterwhen both
7005 enterop->op_next = LINKLIST(block);
7006 block->op_next = enterop->op_other = o;
7011 /* Does this look like a boolean operation? For these purposes
7012 a boolean operation is:
7013 - a subroutine call [*]
7014 - a logical connective
7015 - a comparison operator
7016 - a filetest operator, with the exception of -s -M -A -C
7017 - defined(), exists() or eof()
7018 - /$re/ or $foo =~ /$re/
7020 [*] possibly surprising
7023 S_looks_like_bool(pTHX_ const OP *o)
7027 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
7029 switch(o->op_type) {
7032 return looks_like_bool(cLOGOPo->op_first);
7036 looks_like_bool(cLOGOPo->op_first)
7037 && looks_like_bool(cLOGOPo->op_first->op_sibling));
7042 o->op_flags & OPf_KIDS
7043 && looks_like_bool(cUNOPo->op_first));
7047 case OP_NOT: case OP_XOR:
7049 case OP_EQ: case OP_NE: case OP_LT:
7050 case OP_GT: case OP_LE: case OP_GE:
7052 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
7053 case OP_I_GT: case OP_I_LE: case OP_I_GE:
7055 case OP_SEQ: case OP_SNE: case OP_SLT:
7056 case OP_SGT: case OP_SLE: case OP_SGE:
7060 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
7061 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
7062 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
7063 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
7064 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
7065 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
7066 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
7067 case OP_FTTEXT: case OP_FTBINARY:
7069 case OP_DEFINED: case OP_EXISTS:
7070 case OP_MATCH: case OP_EOF:
7077 /* Detect comparisons that have been optimized away */
7078 if (cSVOPo->op_sv == &PL_sv_yes
7079 || cSVOPo->op_sv == &PL_sv_no)
7092 =for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off
7094 Constructs, checks, and returns an op tree expressing a C<given> block.
7095 I<cond> supplies the expression that will be locally assigned to a lexical
7096 variable, and I<block> supplies the body of the C<given> construct; they
7097 are consumed by this function and become part of the constructed op tree.
7098 I<defsv_off> is the pad offset of the scalar lexical variable that will
7099 be affected. If it is 0, the global $_ will be used.
7105 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
7108 PERL_ARGS_ASSERT_NEWGIVENOP;
7109 return newGIVWHENOP(
7110 ref_array_or_hash(cond),
7112 OP_ENTERGIVEN, OP_LEAVEGIVEN,
7117 =for apidoc Am|OP *|newWHENOP|OP *cond|OP *block
7119 Constructs, checks, and returns an op tree expressing a C<when> block.
7120 I<cond> supplies the test expression, and I<block> supplies the block
7121 that will be executed if the test evaluates to true; they are consumed
7122 by this function and become part of the constructed op tree. I<cond>
7123 will be interpreted DWIMically, often as a comparison against C<$_>,
7124 and may be null to generate a C<default> block.
7130 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
7132 const bool cond_llb = (!cond || looks_like_bool(cond));
7135 PERL_ARGS_ASSERT_NEWWHENOP;
7140 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
7142 scalar(ref_array_or_hash(cond)));
7145 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
7149 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
7150 const STRLEN len, const U32 flags)
7152 SV *name = NULL, *msg;
7153 const char * cvp = SvROK(cv) ? "" : CvPROTO(cv);
7154 STRLEN clen = CvPROTOLEN(cv), plen = len;
7156 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
7158 if (p == NULL && cvp == NULL)
7161 if (!ckWARN_d(WARN_PROTOTYPE))
7165 p = S_strip_spaces(aTHX_ p, &plen);
7166 cvp = S_strip_spaces(aTHX_ cvp, &clen);
7167 if ((flags & SVf_UTF8) == SvUTF8(cv)) {
7168 if (plen == clen && memEQ(cvp, p, plen))
7171 if (flags & SVf_UTF8) {
7172 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0)
7176 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0)
7182 msg = sv_newmortal();
7187 gv_efullname3(name = sv_newmortal(), gv, NULL);
7188 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&')
7189 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP);
7190 else name = (SV *)gv;
7192 sv_setpvs(msg, "Prototype mismatch:");
7194 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, SVfARG(name));
7196 Perl_sv_catpvf(aTHX_ msg, " (%"UTF8f")",
7197 UTF8fARG(SvUTF8(cv),clen,cvp)
7200 sv_catpvs(msg, ": none");
7201 sv_catpvs(msg, " vs ");
7203 Perl_sv_catpvf(aTHX_ msg, "(%"UTF8f")", UTF8fARG(flags & SVf_UTF8,len,p));
7205 sv_catpvs(msg, "none");
7206 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, SVfARG(msg));
7209 static void const_sv_xsub(pTHX_ CV* cv);
7210 static void const_av_xsub(pTHX_ CV* cv);
7214 =head1 Optree Manipulation Functions
7216 =for apidoc cv_const_sv
7218 If C<cv> is a constant sub eligible for inlining, returns the constant
7219 value returned by the sub. Otherwise, returns NULL.
7221 Constant subs can be created with C<newCONSTSUB> or as described in
7222 L<perlsub/"Constant Functions">.
7227 Perl_cv_const_sv(pTHX_ const CV *const cv)
7230 PERL_UNUSED_CONTEXT;
7233 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
7235 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7236 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL;
7241 Perl_cv_const_sv_or_av(pTHX_ const CV * const cv)
7243 PERL_UNUSED_CONTEXT;
7246 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
7247 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
7250 /* op_const_sv: examine an optree to determine whether it's in-lineable.
7254 Perl_op_const_sv(pTHX_ const OP *o)
7265 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
7266 o = cLISTOPo->op_first->op_sibling;
7268 for (; o; o = o->op_next) {
7269 const OPCODE type = o->op_type;
7271 if (sv && o->op_next == o)
7273 if (o->op_next != o) {
7274 if (type == OP_NEXTSTATE
7275 || (type == OP_NULL && !(o->op_flags & OPf_KIDS))
7276 || type == OP_PUSHMARK)
7278 if (type == OP_DBSTATE)
7281 if (type == OP_LEAVESUB || type == OP_RETURN)
7285 if (type == OP_CONST && cSVOPo->op_sv)
7295 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
7296 PADNAME * const name, SV ** const const_svp)
7303 || block->op_type == OP_NULL
7306 if (CvFLAGS(PL_compcv)) {
7307 /* might have had built-in attrs applied */
7308 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
7309 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
7310 && ckWARN(WARN_MISC))
7312 /* protect against fatal warnings leaking compcv */
7313 SAVEFREESV(PL_compcv);
7314 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
7315 SvREFCNT_inc_simple_void_NN(PL_compcv);
7318 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
7319 & ~(CVf_LVALUE * pureperl));
7324 /* redundant check for speed: */
7325 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
7326 const line_t oldline = CopLINE(PL_curcop);
7329 : sv_2mortal(newSVpvn_utf8(
7330 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name)
7332 if (PL_parser && PL_parser->copline != NOLINE)
7333 /* This ensures that warnings are reported at the first
7334 line of a redefinition, not the last. */
7335 CopLINE_set(PL_curcop, PL_parser->copline);
7336 /* protect against fatal warnings leaking compcv */
7337 SAVEFREESV(PL_compcv);
7338 report_redefined_cv(namesv, cv, const_svp);
7339 SvREFCNT_inc_simple_void_NN(PL_compcv);
7340 CopLINE_set(PL_curcop, oldline);
7343 if (!PL_minus_c) /* keep old one around for madskills */
7346 /* (PL_madskills unset in used file.) */
7353 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
7359 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
7362 CV *compcv = PL_compcv;
7365 PADOFFSET pax = o->op_targ;
7366 CV *outcv = CvOUTSIDE(PL_compcv);
7369 bool reusable = FALSE;
7371 PERL_ARGS_ASSERT_NEWMYSUB;
7373 /* Find the pad slot for storing the new sub.
7374 We cannot use PL_comppad, as it is the pad owned by the new sub. We
7375 need to look in CvOUTSIDE and find the pad belonging to the enclos-
7376 ing sub. And then we need to dig deeper if this is a lexical from
7378 my sub foo; sub { sub foo { } }
7381 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
7382 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
7383 pax = PARENT_PAD_INDEX(name);
7384 outcv = CvOUTSIDE(outcv);
7389 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
7390 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
7391 spot = (CV **)svspot;
7393 if (!(PL_parser && PL_parser->error_count))
7394 move_proto_attr(&proto, &attrs, (GV *)name);
7397 assert(proto->op_type == OP_CONST);
7398 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
7399 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
7404 if (!PL_madskills) {
7411 if (PL_parser && PL_parser->error_count) {
7413 SvREFCNT_dec(PL_compcv);
7418 if (CvDEPTH(outcv) && CvCLONE(compcv)) {
7420 svspot = (SV **)(spot = &clonee);
7422 else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
7426 SvUPGRADE(name, SVt_PVMG);
7427 mg = mg_find(name, PERL_MAGIC_proto);
7428 assert (SvTYPE(*spot) == SVt_PVCV);
7430 hek = CvNAME_HEK(*spot);
7432 CvNAME_HEK_set(*spot, hek =
7435 PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1), 0
7441 cv = (CV *)mg->mg_obj;
7444 sv_magic(name, &PL_sv_undef, PERL_MAGIC_proto, NULL, 0);
7445 mg = mg_find(name, PERL_MAGIC_proto);
7447 spot = (CV **)(svspot = &mg->mg_obj);
7450 if (!block || !ps || *ps || attrs
7451 || (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS)
7453 || block->op_type == OP_NULL
7458 const_sv = op_const_sv(block);
7461 const bool exists = CvROOT(cv) || CvXSUB(cv);
7463 /* if the subroutine doesn't exist and wasn't pre-declared
7464 * with a prototype, assume it will be AUTOLOADed,
7465 * skipping the prototype check
7467 if (exists || SvPOK(cv))
7468 cv_ckproto_len_flags(cv, (GV *)name, ps, ps_len, ps_utf8);
7469 /* already defined? */
7471 if (S_already_defined(aTHX_ cv,block,NULL,name,&const_sv))
7474 if (attrs) goto attrs;
7475 /* just a "sub foo;" when &foo is already defined */
7480 else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
7486 SvREFCNT_inc_simple_void_NN(const_sv);
7487 SvFLAGS(const_sv) = (SvFLAGS(const_sv) & ~SVs_PADMY) | SVs_PADTMP;
7489 assert(!CvROOT(cv) && !CvCONST(cv));
7493 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
7494 CvFILE_set_from_cop(cv, PL_curcop);
7495 CvSTASH_set(cv, PL_curstash);
7498 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */
7499 CvXSUBANY(cv).any_ptr = const_sv;
7500 CvXSUB(cv) = const_sv_xsub;
7506 SvREFCNT_dec(compcv);
7510 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
7511 determine whether this sub definition is in the same scope as its
7512 declaration. If this sub definition is inside an inner named pack-
7513 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
7514 the package sub. So check PadnameOUTER(name) too.
7516 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) {
7517 assert(!CvWEAKOUTSIDE(compcv));
7518 SvREFCNT_dec(CvOUTSIDE(compcv));
7519 CvWEAKOUTSIDE_on(compcv);
7521 /* XXX else do we have a circular reference? */
7522 if (cv) { /* must reuse cv in case stub is referenced elsewhere */
7523 /* transfer PL_compcv to cv */
7526 && block->op_type != OP_NULL
7529 cv_flags_t preserved_flags =
7530 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
7531 PADLIST *const temp_padl = CvPADLIST(cv);
7532 CV *const temp_cv = CvOUTSIDE(cv);
7533 const cv_flags_t other_flags =
7534 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
7535 OP * const cvstart = CvSTART(cv);
7539 CvFLAGS(compcv) | preserved_flags;
7540 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
7541 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
7542 CvPADLIST(cv) = CvPADLIST(compcv);
7543 CvOUTSIDE(compcv) = temp_cv;
7544 CvPADLIST(compcv) = temp_padl;
7545 CvSTART(cv) = CvSTART(compcv);
7546 CvSTART(compcv) = cvstart;
7547 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
7548 CvFLAGS(compcv) |= other_flags;
7550 if (CvFILE(cv) && CvDYNFILE(cv)) {
7551 Safefree(CvFILE(cv));
7554 /* inner references to compcv must be fixed up ... */
7555 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
7556 if (PERLDB_INTER)/* Advice debugger on the new sub. */
7557 ++PL_sub_generation;
7560 /* Might have had built-in attributes applied -- propagate them. */
7561 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
7563 /* ... before we throw it away */
7564 SvREFCNT_dec(compcv);
7565 PL_compcv = compcv = cv;
7572 if (!CvNAME_HEK(cv)) {
7575 ? share_hek_hek(hek)
7576 : share_hek(PadnamePV(name)+1,
7577 PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1),
7581 if (const_sv) goto clone;
7583 CvFILE_set_from_cop(cv, PL_curcop);
7584 CvSTASH_set(cv, PL_curstash);
7587 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
7588 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
7595 /* If we assign an optree to a PVCV, then we've defined a subroutine that
7596 the debugger could be able to set a breakpoint in, so signal to
7597 pp_entereval that it should not throw away any saved lines at scope
7600 PL_breakable_sub_gen++;
7601 /* This makes sub {}; work as expected. */
7602 if (block->op_type == OP_STUB) {
7603 OP* const newblock = newSTATEOP(0, NULL, 0);
7605 op_getmad(block,newblock,'B');
7611 CvROOT(cv) = CvLVALUE(cv)
7612 ? newUNOP(OP_LEAVESUBLV, 0,
7613 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
7614 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
7615 CvROOT(cv)->op_private |= OPpREFCOUNTED;
7616 OpREFCNT_set(CvROOT(cv), 1);
7617 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
7618 itself has a refcount. */
7620 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
7621 CvSTART(cv) = LINKLIST(CvROOT(cv));
7622 CvROOT(cv)->op_next = 0;
7623 CALL_PEEP(CvSTART(cv));
7624 finalize_optree(CvROOT(cv));
7625 S_prune_chain_head(aTHX_ &CvSTART(cv));
7627 /* now that optimizer has done its work, adjust pad values */
7629 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
7633 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
7634 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
7638 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
7639 SV * const tmpstr = sv_newmortal();
7640 GV * const db_postponed = gv_fetchpvs("DB::postponed",
7641 GV_ADDMULTI, SVt_PVHV);
7643 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
7646 (long)CopLINE(PL_curcop));
7647 if (HvNAME_HEK(PL_curstash)) {
7648 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
7649 sv_catpvs(tmpstr, "::");
7651 else sv_setpvs(tmpstr, "__ANON__::");
7652 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
7653 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
7654 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
7655 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
7656 hv = GvHVn(db_postponed);
7657 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
7658 CV * const pcv = GvCV(db_postponed);
7664 call_sv(MUTABLE_SV(pcv), G_DISCARD);
7672 assert(CvDEPTH(outcv));
7674 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
7675 if (reusable) cv_clone_into(clonee, *spot);
7676 else *spot = cv_clone(clonee);
7677 SvREFCNT_dec_NN(clonee);
7681 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
7682 PADOFFSET depth = CvDEPTH(outcv);
7685 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
7687 *svspot = SvREFCNT_inc_simple_NN(cv);
7688 SvREFCNT_dec(oldcv);
7694 PL_parser->copline = NOLINE;
7702 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
7703 OP *block, bool o_is_gv)
7708 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
7712 const bool ec = PL_parser && PL_parser->error_count;
7713 /* If the subroutine has no body, no attributes, and no builtin attributes
7714 then it's just a sub declaration, and we may be able to get away with
7715 storing with a placeholder scalar in the symbol table, rather than a
7716 full GV and CV. If anything is present then it will take a full CV to
7718 const I32 gv_fetch_flags
7719 = ec ? GV_NOADD_NOINIT :
7720 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
7722 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
7724 const char * const name =
7725 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
7727 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
7728 #ifdef PERL_DEBUG_READONLY_OPS
7729 OPSLAB *slab = NULL;
7737 gv = gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV);
7739 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
7740 SV * const sv = sv_newmortal();
7741 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
7742 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
7743 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
7744 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
7746 } else if (PL_curstash) {
7747 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
7750 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
7755 move_proto_attr(&proto, &attrs, gv);
7758 assert(proto->op_type == OP_CONST);
7759 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
7760 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
7765 if (!PL_madskills) {
7776 if (name) SvREFCNT_dec(PL_compcv);
7777 else cv = PL_compcv;
7779 if (name && block) {
7780 const char *s = strrchr(name, ':');
7782 if (strEQ(s, "BEGIN")) {
7783 if (PL_in_eval & EVAL_KEEPERR)
7784 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
7786 SV * const errsv = ERRSV;
7787 /* force display of errors found but not reported */
7788 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
7789 Perl_croak_nocontext("%"SVf, SVfARG(errsv));
7796 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
7797 maximum a prototype before. */
7798 if (SvTYPE(gv) > SVt_NULL) {
7799 cv_ckproto_len_flags((const CV *)gv,
7800 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
7804 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
7805 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(gv));
7808 sv_setiv(MUTABLE_SV(gv), -1);
7810 SvREFCNT_dec(PL_compcv);
7811 cv = PL_compcv = NULL;
7815 cv = (!name || GvCVGEN(gv)) ? NULL : GvCV(gv);
7817 if (!block || !ps || *ps || attrs
7818 || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
7820 || block->op_type == OP_NULL
7825 const_sv = op_const_sv(block);
7828 const bool exists = CvROOT(cv) || CvXSUB(cv);
7830 /* if the subroutine doesn't exist and wasn't pre-declared
7831 * with a prototype, assume it will be AUTOLOADed,
7832 * skipping the prototype check
7834 if (exists || SvPOK(cv))
7835 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
7836 /* already defined (or promised)? */
7837 if (exists || GvASSUMECV(gv)) {
7838 if (S_already_defined(aTHX_ cv, block, o, NULL, &const_sv))
7841 if (attrs) goto attrs;
7842 /* just a "sub foo;" when &foo is already defined */
7843 SAVEFREESV(PL_compcv);
7849 SvREFCNT_inc_simple_void_NN(const_sv);
7850 SvFLAGS(const_sv) = (SvFLAGS(const_sv) & ~SVs_PADMY) | SVs_PADTMP;
7852 assert(!CvROOT(cv) && !CvCONST(cv));
7854 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */
7855 CvXSUBANY(cv).any_ptr = const_sv;
7856 CvXSUB(cv) = const_sv_xsub;
7862 cv = newCONSTSUB_flags(
7863 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
7870 SvREFCNT_dec(PL_compcv);
7874 if (cv) { /* must reuse cv if autoloaded */
7875 /* transfer PL_compcv to cv */
7878 && block->op_type != OP_NULL
7881 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
7882 PADLIST *const temp_av = CvPADLIST(cv);
7883 CV *const temp_cv = CvOUTSIDE(cv);
7884 const cv_flags_t other_flags =
7885 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
7886 OP * const cvstart = CvSTART(cv);
7889 assert(!CvCVGV_RC(cv));
7890 assert(CvGV(cv) == gv);
7893 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs;
7894 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
7895 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
7896 CvPADLIST(cv) = CvPADLIST(PL_compcv);
7897 CvOUTSIDE(PL_compcv) = temp_cv;
7898 CvPADLIST(PL_compcv) = temp_av;
7899 CvSTART(cv) = CvSTART(PL_compcv);
7900 CvSTART(PL_compcv) = cvstart;
7901 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
7902 CvFLAGS(PL_compcv) |= other_flags;
7904 if (CvFILE(cv) && CvDYNFILE(cv)) {
7905 Safefree(CvFILE(cv));
7907 CvFILE_set_from_cop(cv, PL_curcop);
7908 CvSTASH_set(cv, PL_curstash);
7910 /* inner references to PL_compcv must be fixed up ... */
7911 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
7912 if (PERLDB_INTER)/* Advice debugger on the new sub. */
7913 ++PL_sub_generation;
7916 /* Might have had built-in attributes applied -- propagate them. */
7917 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
7919 /* ... before we throw it away */
7920 SvREFCNT_dec(PL_compcv);
7928 if (HvENAME_HEK(GvSTASH(gv)))
7929 /* sub Foo::bar { (shift)+1 } */
7930 gv_method_changed(gv);
7935 CvFILE_set_from_cop(cv, PL_curcop);
7936 CvSTASH_set(cv, PL_curstash);
7940 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
7941 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
7948 /* If we assign an optree to a PVCV, then we've defined a subroutine that
7949 the debugger could be able to set a breakpoint in, so signal to
7950 pp_entereval that it should not throw away any saved lines at scope
7953 PL_breakable_sub_gen++;
7954 /* This makes sub {}; work as expected. */
7955 if (block->op_type == OP_STUB) {
7956 OP* const newblock = newSTATEOP(0, NULL, 0);
7958 op_getmad(block,newblock,'B');
7964 CvROOT(cv) = CvLVALUE(cv)
7965 ? newUNOP(OP_LEAVESUBLV, 0,
7966 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
7967 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
7968 CvROOT(cv)->op_private |= OPpREFCOUNTED;
7969 OpREFCNT_set(CvROOT(cv), 1);
7970 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
7971 itself has a refcount. */
7973 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
7974 #ifdef PERL_DEBUG_READONLY_OPS
7975 slab = (OPSLAB *)CvSTART(cv);
7977 CvSTART(cv) = LINKLIST(CvROOT(cv));
7978 CvROOT(cv)->op_next = 0;
7979 CALL_PEEP(CvSTART(cv));
7980 finalize_optree(CvROOT(cv));
7981 S_prune_chain_head(aTHX_ &CvSTART(cv));
7983 /* now that optimizer has done its work, adjust pad values */
7985 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
7989 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
7990 HV *stash = name && GvSTASH(CvGV(cv)) ? GvSTASH(CvGV(cv)) : PL_curstash;
7991 if (!name) SAVEFREESV(cv);
7992 apply_attrs(stash, MUTABLE_SV(cv), attrs);
7993 if (!name) SvREFCNT_inc_simple_void_NN(cv);
7996 if (block && has_name) {
7997 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
7998 SV * const tmpstr = sv_newmortal();
7999 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8000 GV_ADDMULTI, SVt_PVHV);
8002 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8005 (long)CopLINE(PL_curcop));
8006 gv_efullname3(tmpstr, gv, NULL);
8007 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8008 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8009 hv = GvHVn(db_postponed);
8010 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8011 CV * const pcv = GvCV(db_postponed);
8017 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8022 if (name && ! (PL_parser && PL_parser->error_count))
8023 process_special_blocks(floor, name, gv, cv);
8028 PL_parser->copline = NOLINE;
8030 #ifdef PERL_DEBUG_READONLY_OPS
8031 /* Watch out for BEGIN blocks */
8032 if (slab && gv && isGV(gv) && GvCV(gv)) Slab_to_ro(slab);
8038 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
8042 const char *const colon = strrchr(fullname,':');
8043 const char *const name = colon ? colon + 1 : fullname;
8045 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
8048 if (strEQ(name, "BEGIN")) {
8049 const I32 oldscope = PL_scopestack_ix;
8051 if (floor) LEAVE_SCOPE(floor);
8053 PUSHSTACKi(PERLSI_REQUIRE);
8054 SAVECOPFILE(&PL_compiling);
8055 SAVECOPLINE(&PL_compiling);
8056 SAVEVPTR(PL_curcop);
8058 DEBUG_x( dump_sub(gv) );
8059 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
8060 GvCV_set(gv,0); /* cv has been hijacked */
8061 call_list(oldscope, PL_beginav);
8070 if strEQ(name, "END") {
8071 DEBUG_x( dump_sub(gv) );
8072 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
8075 } else if (*name == 'U') {
8076 if (strEQ(name, "UNITCHECK")) {
8077 /* It's never too late to run a unitcheck block */
8078 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
8082 } else if (*name == 'C') {
8083 if (strEQ(name, "CHECK")) {
8085 /* diag_listed_as: Too late to run %s block */
8086 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8087 "Too late to run CHECK block");
8088 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
8092 } else if (*name == 'I') {
8093 if (strEQ(name, "INIT")) {
8095 /* diag_listed_as: Too late to run %s block */
8096 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8097 "Too late to run INIT block");
8098 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
8104 DEBUG_x( dump_sub(gv) );
8105 GvCV_set(gv,0); /* cv has been hijacked */
8110 =for apidoc newCONSTSUB
8112 See L</newCONSTSUB_flags>.
8118 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
8120 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
8124 =for apidoc newCONSTSUB_flags
8126 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
8127 eligible for inlining at compile-time.
8129 Currently, the only useful value for C<flags> is SVf_UTF8.
8131 The newly created subroutine takes ownership of a reference to the passed in
8134 Passing NULL for SV creates a constant sub equivalent to C<sub BAR () {}>,
8135 which won't be called if used as a destructor, but will suppress the overhead
8136 of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at
8143 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
8148 const char *const file = CopFILE(PL_curcop);
8152 if (IN_PERL_RUNTIME) {
8153 /* at runtime, it's not safe to manipulate PL_curcop: it may be
8154 * an op shared between threads. Use a non-shared COP for our
8156 SAVEVPTR(PL_curcop);
8157 SAVECOMPILEWARNINGS();
8158 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
8159 PL_curcop = &PL_compiling;
8161 SAVECOPLINE(PL_curcop);
8162 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
8165 PL_hints &= ~HINT_BLOCK_SCOPE;
8168 SAVEGENERICSV(PL_curstash);
8169 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
8172 /* Protect sv against leakage caused by fatal warnings. */
8173 if (sv) SAVEFREESV(sv);
8175 /* file becomes the CvFILE. For an XS, it's usually static storage,
8176 and so doesn't get free()d. (It's expected to be from the C pre-
8177 processor __FILE__ directive). But we need a dynamically allocated one,
8178 and we need it to get freed. */
8179 cv = newXS_len_flags(name, len,
8180 sv && SvTYPE(sv) == SVt_PVAV
8183 file ? file : "", "",
8184 &sv, XS_DYNAMIC_FILENAME | flags);
8185 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
8194 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
8195 const char *const filename, const char *const proto,
8198 PERL_ARGS_ASSERT_NEWXS_FLAGS;
8199 return newXS_len_flags(
8200 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
8205 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
8206 XSUBADDR_t subaddr, const char *const filename,
8207 const char *const proto, SV **const_svp,
8211 bool interleave = FALSE;
8213 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
8216 GV * const gv = gv_fetchpvn(
8217 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
8218 name ? len : PL_curstash ? sizeof("__ANON__") - 1:
8219 sizeof("__ANON__::__ANON__") - 1,
8220 GV_ADDMULTI | flags, SVt_PVCV);
8223 Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
8225 if ((cv = (name ? GvCV(gv) : NULL))) {
8227 /* just a cached method */
8231 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
8232 /* already defined (or promised) */
8233 /* Redundant check that allows us to avoid creating an SV
8234 most of the time: */
8235 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
8236 report_redefined_cv(newSVpvn_flags(
8237 name,len,(flags&SVf_UTF8)|SVs_TEMP
8248 if (cv) /* must reuse cv if autoloaded */
8251 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8255 if (HvENAME_HEK(GvSTASH(gv)))
8256 gv_method_changed(gv); /* newXS */
8262 (void)gv_fetchfile(filename);
8263 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
8264 an external constant string */
8265 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
8267 CvXSUB(cv) = subaddr;
8270 process_special_blocks(0, name, gv, cv);
8273 if (flags & XS_DYNAMIC_FILENAME) {
8274 CvFILE(cv) = savepv(filename);
8277 sv_setpv(MUTABLE_SV(cv), proto);
8278 if (interleave) LEAVE;
8283 Perl_newSTUB(pTHX_ GV *gv, bool fake)
8285 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8287 PERL_ARGS_ASSERT_NEWSTUB;
8291 if (!fake && HvENAME_HEK(GvSTASH(gv)))
8292 gv_method_changed(gv);
8294 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
8299 CvFILE_set_from_cop(cv, PL_curcop);
8300 CvSTASH_set(cv, PL_curstash);
8306 =for apidoc U||newXS
8308 Used by C<xsubpp> to hook up XSUBs as Perl subs. I<filename> needs to be
8309 static storage, as it is used directly as CvFILE(), without a copy being made.
8315 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
8317 PERL_ARGS_ASSERT_NEWXS;
8318 return newXS_len_flags(
8319 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
8328 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
8333 OP* pegop = newOP(OP_NULL, 0);
8338 if (PL_parser && PL_parser->error_count) {
8344 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
8345 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
8348 if ((cv = GvFORM(gv))) {
8349 if (ckWARN(WARN_REDEFINE)) {
8350 const line_t oldline = CopLINE(PL_curcop);
8351 if (PL_parser && PL_parser->copline != NOLINE)
8352 CopLINE_set(PL_curcop, PL_parser->copline);
8354 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8355 "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv));
8357 /* diag_listed_as: Format %s redefined */
8358 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8359 "Format STDOUT redefined");
8361 CopLINE_set(PL_curcop, oldline);
8366 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
8368 CvFILE_set_from_cop(cv, PL_curcop);
8371 pad_tidy(padtidy_FORMAT);
8372 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
8373 CvROOT(cv)->op_private |= OPpREFCOUNTED;
8374 OpREFCNT_set(CvROOT(cv), 1);
8375 CvSTART(cv) = LINKLIST(CvROOT(cv));
8376 CvROOT(cv)->op_next = 0;
8377 CALL_PEEP(CvSTART(cv));
8378 finalize_optree(CvROOT(cv));
8379 S_prune_chain_head(aTHX_ &CvSTART(cv));
8384 op_getmad(o,pegop,'n');
8385 op_getmad_weak(block, pegop, 'b');
8390 PL_parser->copline = NOLINE;
8398 Perl_newANONLIST(pTHX_ OP *o)
8400 return convert(OP_ANONLIST, OPf_SPECIAL, o);
8404 Perl_newANONHASH(pTHX_ OP *o)
8406 return convert(OP_ANONHASH, OPf_SPECIAL, o);
8410 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
8412 return newANONATTRSUB(floor, proto, NULL, block);
8416 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
8418 return newUNOP(OP_REFGEN, 0,
8419 newSVOP(OP_ANONCODE, 0,
8420 MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block))));
8424 Perl_oopsAV(pTHX_ OP *o)
8428 PERL_ARGS_ASSERT_OOPSAV;
8430 switch (o->op_type) {
8433 o->op_type = OP_PADAV;
8434 o->op_ppaddr = PL_ppaddr[OP_PADAV];
8435 return ref(o, OP_RV2AV);
8439 o->op_type = OP_RV2AV;
8440 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
8445 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
8452 Perl_oopsHV(pTHX_ OP *o)
8456 PERL_ARGS_ASSERT_OOPSHV;
8458 switch (o->op_type) {
8461 o->op_type = OP_PADHV;
8462 o->op_ppaddr = PL_ppaddr[OP_PADHV];
8463 return ref(o, OP_RV2HV);
8467 o->op_type = OP_RV2HV;
8468 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
8473 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
8480 Perl_newAVREF(pTHX_ OP *o)
8484 PERL_ARGS_ASSERT_NEWAVREF;
8486 if (o->op_type == OP_PADANY) {
8487 o->op_type = OP_PADAV;
8488 o->op_ppaddr = PL_ppaddr[OP_PADAV];
8491 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
8492 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8493 "Using an array as a reference is deprecated");
8495 return newUNOP(OP_RV2AV, 0, scalar(o));
8499 Perl_newGVREF(pTHX_ I32 type, OP *o)
8501 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
8502 return newUNOP(OP_NULL, 0, o);
8503 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
8507 Perl_newHVREF(pTHX_ OP *o)
8511 PERL_ARGS_ASSERT_NEWHVREF;
8513 if (o->op_type == OP_PADANY) {
8514 o->op_type = OP_PADHV;
8515 o->op_ppaddr = PL_ppaddr[OP_PADHV];
8518 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
8519 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8520 "Using a hash as a reference is deprecated");
8522 return newUNOP(OP_RV2HV, 0, scalar(o));
8526 Perl_newCVREF(pTHX_ I32 flags, OP *o)
8528 if (o->op_type == OP_PADANY) {
8530 o->op_type = OP_PADCV;
8531 o->op_ppaddr = PL_ppaddr[OP_PADCV];
8533 return newUNOP(OP_RV2CV, flags, scalar(o));
8537 Perl_newSVREF(pTHX_ OP *o)
8541 PERL_ARGS_ASSERT_NEWSVREF;
8543 if (o->op_type == OP_PADANY) {
8544 o->op_type = OP_PADSV;
8545 o->op_ppaddr = PL_ppaddr[OP_PADSV];
8548 return newUNOP(OP_RV2SV, 0, scalar(o));
8551 /* Check routines. See the comments at the top of this file for details
8552 * on when these are called */
8555 Perl_ck_anoncode(pTHX_ OP *o)
8557 PERL_ARGS_ASSERT_CK_ANONCODE;
8559 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
8561 cSVOPo->op_sv = NULL;
8566 S_io_hints(pTHX_ OP *o)
8569 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
8571 SV **svp = hv_fetchs(table, "open_IN", FALSE);
8574 const char *d = SvPV_const(*svp, len);
8575 const I32 mode = mode_from_discipline(d, len);
8576 if (mode & O_BINARY)
8577 o->op_private |= OPpOPEN_IN_RAW;
8578 else if (mode & O_TEXT)
8579 o->op_private |= OPpOPEN_IN_CRLF;
8582 svp = hv_fetchs(table, "open_OUT", FALSE);
8585 const char *d = SvPV_const(*svp, len);
8586 const I32 mode = mode_from_discipline(d, len);
8587 if (mode & O_BINARY)
8588 o->op_private |= OPpOPEN_OUT_RAW;
8589 else if (mode & O_TEXT)
8590 o->op_private |= OPpOPEN_OUT_CRLF;
8596 Perl_ck_backtick(pTHX_ OP *o)
8600 PERL_ARGS_ASSERT_CK_BACKTICK;
8601 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
8602 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_sibling
8603 && (gv = gv_override("readpipe",8))) {
8604 newop = S_new_entersubop(aTHX_ gv, cUNOPo->op_first->op_sibling);
8605 cUNOPo->op_first->op_sibling = NULL;
8607 else if (!(o->op_flags & OPf_KIDS))
8608 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
8611 op_getmad(o,newop,'O');
8617 S_io_hints(aTHX_ o);
8622 Perl_ck_bitop(pTHX_ OP *o)
8626 PERL_ARGS_ASSERT_CK_BITOP;
8628 o->op_private = (U8)(PL_hints & HINT_INTEGER);
8629 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
8630 && (o->op_type == OP_BIT_OR
8631 || o->op_type == OP_BIT_AND
8632 || o->op_type == OP_BIT_XOR))
8634 const OP * const left = cBINOPo->op_first;
8635 const OP * const right = left->op_sibling;
8636 if ((OP_IS_NUMCOMPARE(left->op_type) &&
8637 (left->op_flags & OPf_PARENS) == 0) ||
8638 (OP_IS_NUMCOMPARE(right->op_type) &&
8639 (right->op_flags & OPf_PARENS) == 0))
8640 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
8641 "Possible precedence problem on bitwise %c operator",
8642 o->op_type == OP_BIT_OR ? '|'
8643 : o->op_type == OP_BIT_AND ? '&' : '^'
8649 PERL_STATIC_INLINE bool
8650 is_dollar_bracket(pTHX_ const OP * const o)
8653 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
8654 && (kid = cUNOPx(o)->op_first)
8655 && kid->op_type == OP_GV
8656 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
8660 Perl_ck_cmp(pTHX_ OP *o)
8662 PERL_ARGS_ASSERT_CK_CMP;
8663 if (ckWARN(WARN_SYNTAX)) {
8664 const OP *kid = cUNOPo->op_first;
8667 is_dollar_bracket(aTHX_ kid)
8668 && kid->op_sibling && kid->op_sibling->op_type == OP_CONST
8670 || ( kid->op_type == OP_CONST
8671 && (kid = kid->op_sibling) && is_dollar_bracket(aTHX_ kid))
8673 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
8674 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
8680 Perl_ck_concat(pTHX_ OP *o)
8682 const OP * const kid = cUNOPo->op_first;
8684 PERL_ARGS_ASSERT_CK_CONCAT;
8685 PERL_UNUSED_CONTEXT;
8687 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
8688 !(kUNOP->op_first->op_flags & OPf_MOD))
8689 o->op_flags |= OPf_STACKED;
8694 Perl_ck_spair(pTHX_ OP *o)
8698 PERL_ARGS_ASSERT_CK_SPAIR;
8700 if (o->op_flags & OPf_KIDS) {
8703 const OPCODE type = o->op_type;
8704 o = modkids(ck_fun(o), type);
8705 kid = cUNOPo->op_first;
8706 newop = kUNOP->op_first->op_sibling;
8708 const OPCODE type = newop->op_type;
8709 if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
8710 type == OP_PADAV || type == OP_PADHV ||
8711 type == OP_RV2AV || type == OP_RV2HV)
8715 op_getmad(kUNOP->op_first,newop,'K');
8717 op_free(kUNOP->op_first);
8719 kUNOP->op_first = newop;
8721 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
8722 * and OP_CHOMP into OP_SCHOMP */
8723 o->op_ppaddr = PL_ppaddr[++o->op_type];
8728 Perl_ck_delete(pTHX_ OP *o)
8730 PERL_ARGS_ASSERT_CK_DELETE;
8734 if (o->op_flags & OPf_KIDS) {
8735 OP * const kid = cUNOPo->op_first;
8736 switch (kid->op_type) {
8738 o->op_flags |= OPf_SPECIAL;
8741 o->op_private |= OPpSLICE;
8744 o->op_flags |= OPf_SPECIAL;
8749 Perl_croak(aTHX_ "delete argument is index/value array slice,"
8750 " use array slice");
8752 Perl_croak(aTHX_ "delete argument is key/value hash slice, use"
8755 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
8756 "element or slice");
8758 if (kid->op_private & OPpLVAL_INTRO)
8759 o->op_private |= OPpLVAL_INTRO;
8766 Perl_ck_eof(pTHX_ OP *o)
8770 PERL_ARGS_ASSERT_CK_EOF;
8772 if (o->op_flags & OPf_KIDS) {
8774 if (cLISTOPo->op_first->op_type == OP_STUB) {
8776 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
8778 op_getmad(o,newop,'O');
8785 kid = cLISTOPo->op_first;
8786 if (kid->op_type == OP_RV2GV)
8787 kid->op_private |= OPpALLOW_FAKE;
8793 Perl_ck_eval(pTHX_ OP *o)
8797 PERL_ARGS_ASSERT_CK_EVAL;
8799 PL_hints |= HINT_BLOCK_SCOPE;
8800 if (o->op_flags & OPf_KIDS) {
8801 SVOP * const kid = (SVOP*)cUNOPo->op_first;
8804 if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
8810 cUNOPo->op_first = 0;
8815 NewOp(1101, enter, 1, LOGOP);
8816 enter->op_type = OP_ENTERTRY;
8817 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
8818 enter->op_private = 0;
8820 /* establish postfix order */
8821 enter->op_next = (OP*)enter;
8823 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
8824 o->op_type = OP_LEAVETRY;
8825 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
8826 enter->op_other = o;
8827 op_getmad(oldo,o,'O');
8836 const U8 priv = o->op_private;
8842 o = newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
8843 op_getmad(oldo,o,'O');
8845 o->op_targ = (PADOFFSET)PL_hints;
8846 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
8847 if ((PL_hints & HINT_LOCALIZE_HH) != 0
8848 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
8849 /* Store a copy of %^H that pp_entereval can pick up. */
8850 OP *hhop = newSVOP(OP_HINTSEVAL, 0,
8851 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv))));
8852 cUNOPo->op_first->op_sibling = hhop;
8853 o->op_private |= OPpEVAL_HAS_HH;
8855 if (!(o->op_private & OPpEVAL_BYTES)
8856 && FEATURE_UNIEVAL_IS_ENABLED)
8857 o->op_private |= OPpEVAL_UNICODE;
8862 Perl_ck_exec(pTHX_ OP *o)
8864 PERL_ARGS_ASSERT_CK_EXEC;
8866 if (o->op_flags & OPf_STACKED) {
8869 kid = cUNOPo->op_first->op_sibling;
8870 if (kid->op_type == OP_RV2GV)
8879 Perl_ck_exists(pTHX_ OP *o)
8883 PERL_ARGS_ASSERT_CK_EXISTS;
8886 if (o->op_flags & OPf_KIDS) {
8887 OP * const kid = cUNOPo->op_first;
8888 if (kid->op_type == OP_ENTERSUB) {
8889 (void) ref(kid, o->op_type);
8890 if (kid->op_type != OP_RV2CV
8891 && !(PL_parser && PL_parser->error_count))
8893 "exists argument is not a subroutine name");
8894 o->op_private |= OPpEXISTS_SUB;
8896 else if (kid->op_type == OP_AELEM)
8897 o->op_flags |= OPf_SPECIAL;
8898 else if (kid->op_type != OP_HELEM)
8899 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
8900 "element or a subroutine");
8907 Perl_ck_rvconst(pTHX_ OP *o)
8910 SVOP * const kid = (SVOP*)cUNOPo->op_first;
8912 PERL_ARGS_ASSERT_CK_RVCONST;
8914 o->op_private |= (PL_hints & HINT_STRICT_REFS);
8915 if (o->op_type == OP_RV2CV)
8916 o->op_private &= ~1;
8918 if (kid->op_type == OP_CONST) {
8921 SV * const kidsv = kid->op_sv;
8923 /* Is it a constant from cv_const_sv()? */
8924 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
8925 SV * const rsv = SvRV(kidsv);
8926 const svtype type = SvTYPE(rsv);
8927 const char *badtype = NULL;
8929 switch (o->op_type) {
8931 if (type > SVt_PVMG)
8932 badtype = "a SCALAR";
8935 if (type != SVt_PVAV)
8936 badtype = "an ARRAY";
8939 if (type != SVt_PVHV)
8943 if (type != SVt_PVCV)
8948 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
8951 if (SvTYPE(kidsv) == SVt_PVAV) return o;
8952 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
8953 const char *badthing;
8954 switch (o->op_type) {
8956 badthing = "a SCALAR";
8959 badthing = "an ARRAY";
8962 badthing = "a HASH";
8970 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
8971 SVfARG(kidsv), badthing);
8974 * This is a little tricky. We only want to add the symbol if we
8975 * didn't add it in the lexer. Otherwise we get duplicate strict
8976 * warnings. But if we didn't add it in the lexer, we must at
8977 * least pretend like we wanted to add it even if it existed before,
8978 * or we get possible typo warnings. OPpCONST_ENTERED says
8979 * whether the lexer already added THIS instance of this symbol.
8981 iscv = (o->op_type == OP_RV2CV) * 2;
8983 gv = gv_fetchsv(kidsv,
8984 iscv | !(kid->op_private & OPpCONST_ENTERED),
8987 : o->op_type == OP_RV2SV
8989 : o->op_type == OP_RV2AV
8991 : o->op_type == OP_RV2HV
8994 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
8996 kid->op_type = OP_GV;
8997 SvREFCNT_dec(kid->op_sv);
8999 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
9000 assert (sizeof(PADOP) <= sizeof(SVOP));
9001 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
9002 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
9004 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
9006 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
9008 kid->op_private = 0;
9009 kid->op_ppaddr = PL_ppaddr[OP_GV];
9010 /* FAKE globs in the symbol table cause weird bugs (#77810) */
9018 Perl_ck_ftst(pTHX_ OP *o)
9021 const I32 type = o->op_type;
9023 PERL_ARGS_ASSERT_CK_FTST;
9025 if (o->op_flags & OPf_REF) {
9028 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
9029 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9030 const OPCODE kidtype = kid->op_type;
9032 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
9033 && !kid->op_folded) {
9034 OP * const newop = newGVOP(type, OPf_REF,
9035 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
9037 op_getmad(o,newop,'O');
9043 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
9044 o->op_private |= OPpFT_ACCESS;
9045 if (PL_check[kidtype] == Perl_ck_ftst
9046 && kidtype != OP_STAT && kidtype != OP_LSTAT) {
9047 o->op_private |= OPpFT_STACKED;
9048 kid->op_private |= OPpFT_STACKING;
9049 if (kidtype == OP_FTTTY && (
9050 !(kid->op_private & OPpFT_STACKED)
9051 || kid->op_private & OPpFT_AFTER_t
9053 o->op_private |= OPpFT_AFTER_t;
9062 if (type == OP_FTTTY)
9063 o = newGVOP(type, OPf_REF, PL_stdingv);
9065 o = newUNOP(type, 0, newDEFSVOP());
9066 op_getmad(oldo,o,'O');
9072 Perl_ck_fun(pTHX_ OP *o)
9075 const int type = o->op_type;
9076 I32 oa = PL_opargs[type] >> OASHIFT;
9078 PERL_ARGS_ASSERT_CK_FUN;
9080 if (o->op_flags & OPf_STACKED) {
9081 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
9084 return no_fh_allowed(o);
9087 if (o->op_flags & OPf_KIDS) {
9088 OP **tokid = &cLISTOPo->op_first;
9089 OP *kid = cLISTOPo->op_first;
9092 bool seen_optional = FALSE;
9094 if (kid->op_type == OP_PUSHMARK ||
9095 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
9097 tokid = &kid->op_sibling;
9098 kid = kid->op_sibling;
9100 if (kid && kid->op_type == OP_COREARGS) {
9101 bool optional = FALSE;
9104 if (oa & OA_OPTIONAL) optional = TRUE;
9107 if (optional) o->op_private |= numargs;
9112 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
9113 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV)
9114 *tokid = kid = newDEFSVOP();
9115 seen_optional = TRUE;
9120 sibl = kid->op_sibling;
9122 if (!sibl && kid->op_type == OP_STUB) {
9129 /* list seen where single (scalar) arg expected? */
9130 if (numargs == 1 && !(oa >> 4)
9131 && kid->op_type == OP_LIST && type != OP_SCALAR)
9133 return too_many_arguments_pv(o,PL_op_desc[type], 0);
9135 if (type != OP_DELETE) scalar(kid);
9146 if ((type == OP_PUSH || type == OP_UNSHIFT)
9147 && !kid->op_sibling)
9148 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
9149 "Useless use of %s with no values",
9152 if (kid->op_type == OP_CONST &&
9153 (kid->op_private & OPpCONST_BARE))
9155 OP * const newop = newAVREF(newGVOP(OP_GV, 0,
9156 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
9157 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9158 "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
9159 SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
9161 op_getmad(kid,newop,'K');
9166 kid->op_sibling = sibl;
9169 else if (kid->op_type == OP_CONST
9170 && ( !SvROK(cSVOPx_sv(kid))
9171 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
9173 bad_type_pv(numargs, "array", PL_op_desc[type], 0, kid);
9174 /* Defer checks to run-time if we have a scalar arg */
9175 if (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV)
9176 op_lvalue(kid, type);
9179 /* diag_listed_as: push on reference is experimental */
9180 Perl_ck_warner_d(aTHX_
9181 packWARN(WARN_EXPERIMENTAL__AUTODEREF),
9182 "%s on reference is experimental",
9187 if (kid->op_type == OP_CONST &&
9188 (kid->op_private & OPpCONST_BARE))
9190 OP * const newop = newHVREF(newGVOP(OP_GV, 0,
9191 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
9192 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9193 "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
9194 SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
9196 op_getmad(kid,newop,'K');
9201 kid->op_sibling = sibl;
9204 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
9205 bad_type_pv(numargs, "hash", PL_op_desc[type], 0, kid);
9206 op_lvalue(kid, type);
9210 OP * const newop = newUNOP(OP_NULL, 0, kid);
9211 kid->op_sibling = 0;
9212 newop->op_next = newop;
9214 kid->op_sibling = sibl;
9219 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
9220 if (kid->op_type == OP_CONST &&
9221 (kid->op_private & OPpCONST_BARE))
9223 OP * const newop = newGVOP(OP_GV, 0,
9224 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
9225 if (!(o->op_private & 1) && /* if not unop */
9226 kid == cLISTOPo->op_last)
9227 cLISTOPo->op_last = newop;
9229 op_getmad(kid,newop,'K');
9235 else if (kid->op_type == OP_READLINE) {
9236 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
9237 bad_type_pv(numargs, "HANDLE", OP_DESC(o), 0, kid);
9240 I32 flags = OPf_SPECIAL;
9244 /* is this op a FH constructor? */
9245 if (is_handle_constructor(o,numargs)) {
9246 const char *name = NULL;
9249 bool want_dollar = TRUE;
9252 /* Set a flag to tell rv2gv to vivify
9253 * need to "prove" flag does not mean something
9254 * else already - NI-S 1999/05/07
9257 if (kid->op_type == OP_PADSV) {
9259 = PAD_COMPNAME_SV(kid->op_targ);
9260 name = SvPV_const(namesv, len);
9261 name_utf8 = SvUTF8(namesv);
9263 else if (kid->op_type == OP_RV2SV
9264 && kUNOP->op_first->op_type == OP_GV)
9266 GV * const gv = cGVOPx_gv(kUNOP->op_first);
9268 len = GvNAMELEN(gv);
9269 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
9271 else if (kid->op_type == OP_AELEM
9272 || kid->op_type == OP_HELEM)
9275 OP *op = ((BINOP*)kid)->op_first;
9279 const char * const a =
9280 kid->op_type == OP_AELEM ?
9282 if (((op->op_type == OP_RV2AV) ||
9283 (op->op_type == OP_RV2HV)) &&
9284 (firstop = ((UNOP*)op)->op_first) &&
9285 (firstop->op_type == OP_GV)) {
9286 /* packagevar $a[] or $h{} */
9287 GV * const gv = cGVOPx_gv(firstop);
9295 else if (op->op_type == OP_PADAV
9296 || op->op_type == OP_PADHV) {
9297 /* lexicalvar $a[] or $h{} */
9298 const char * const padname =
9299 PAD_COMPNAME_PV(op->op_targ);
9308 name = SvPV_const(tmpstr, len);
9309 name_utf8 = SvUTF8(tmpstr);
9314 name = "__ANONIO__";
9316 want_dollar = FALSE;
9318 op_lvalue(kid, type);
9322 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
9323 namesv = PAD_SVl(targ);
9324 if (want_dollar && *name != '$')
9325 sv_setpvs(namesv, "$");
9327 sv_setpvs(namesv, "");
9328 sv_catpvn(namesv, name, len);
9329 if ( name_utf8 ) SvUTF8_on(namesv);
9332 kid->op_sibling = 0;
9333 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
9334 kid->op_targ = targ;
9335 kid->op_private |= priv;
9337 kid->op_sibling = sibl;
9343 if ((type == OP_UNDEF || type == OP_POS)
9344 && numargs == 1 && !(oa >> 4)
9345 && kid->op_type == OP_LIST)
9346 return too_many_arguments_pv(o,PL_op_desc[type], 0);
9347 op_lvalue(scalar(kid), type);
9351 tokid = &kid->op_sibling;
9352 kid = kid->op_sibling;
9355 if (kid && kid->op_type != OP_STUB)
9356 return too_many_arguments_pv(o,OP_DESC(o), 0);
9357 o->op_private |= numargs;
9359 /* FIXME - should the numargs move as for the PERL_MAD case? */
9360 o->op_private |= numargs;
9362 return too_many_arguments_pv(o,OP_DESC(o), 0);
9366 else if (PL_opargs[type] & OA_DEFGV) {
9368 OP *newop = newUNOP(type, 0, newDEFSVOP());
9369 op_getmad(o,newop,'O');
9372 /* Ordering of these two is important to keep f_map.t passing. */
9374 return newUNOP(type, 0, newDEFSVOP());
9379 while (oa & OA_OPTIONAL)
9381 if (oa && oa != OA_LIST)
9382 return too_few_arguments_pv(o,OP_DESC(o), 0);
9388 Perl_ck_glob(pTHX_ OP *o)
9393 PERL_ARGS_ASSERT_CK_GLOB;
9396 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
9397 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
9399 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
9403 * \ null - const(wildcard)
9408 * \ mark - glob - rv2cv
9409 * | \ gv(CORE::GLOBAL::glob)
9411 * \ null - const(wildcard)
9413 o->op_flags |= OPf_SPECIAL;
9414 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
9415 o = S_new_entersubop(aTHX_ gv, o);
9416 o = newUNOP(OP_NULL, 0, o);
9417 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
9420 else o->op_flags &= ~OPf_SPECIAL;
9421 #if !defined(PERL_EXTERNAL_GLOB)
9424 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
9425 newSVpvs("File::Glob"), NULL, NULL, NULL);
9428 #endif /* !PERL_EXTERNAL_GLOB */
9429 gv = (GV *)newSV(0);
9430 gv_init(gv, 0, "", 0, 0);
9432 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
9433 SvREFCNT_dec_NN(gv); /* newGVOP increased it */
9439 Perl_ck_grep(pTHX_ OP *o)
9444 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
9447 PERL_ARGS_ASSERT_CK_GREP;
9449 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
9450 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
9452 if (o->op_flags & OPf_STACKED) {
9453 kid = cUNOPx(cLISTOPo->op_first->op_sibling)->op_first;
9454 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
9455 return no_fh_allowed(o);
9456 o->op_flags &= ~OPf_STACKED;
9458 kid = cLISTOPo->op_first->op_sibling;
9459 if (type == OP_MAPWHILE)
9464 if (PL_parser && PL_parser->error_count)
9466 kid = cLISTOPo->op_first->op_sibling;
9467 if (kid->op_type != OP_NULL)
9468 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
9469 kid = kUNOP->op_first;
9471 NewOp(1101, gwop, 1, LOGOP);
9472 gwop->op_type = type;
9473 gwop->op_ppaddr = PL_ppaddr[type];
9475 gwop->op_flags |= OPf_KIDS;
9476 gwop->op_other = LINKLIST(kid);
9477 kid->op_next = (OP*)gwop;
9478 offset = pad_findmy_pvs("$_", 0);
9479 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
9480 o->op_private = gwop->op_private = 0;
9481 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
9484 o->op_private = gwop->op_private = OPpGREP_LEX;
9485 gwop->op_targ = o->op_targ = offset;
9488 kid = cLISTOPo->op_first->op_sibling;
9489 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
9490 op_lvalue(kid, OP_GREPSTART);
9496 Perl_ck_index(pTHX_ OP *o)
9498 PERL_ARGS_ASSERT_CK_INDEX;
9500 if (o->op_flags & OPf_KIDS) {
9501 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9503 kid = kid->op_sibling; /* get past "big" */
9504 if (kid && kid->op_type == OP_CONST) {
9505 const bool save_taint = TAINT_get;
9506 SV *sv = kSVOP->op_sv;
9507 if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) {
9509 sv_copypv(sv, kSVOP->op_sv);
9510 SvREFCNT_dec_NN(kSVOP->op_sv);
9513 if (SvOK(sv)) fbm_compile(sv, 0);
9514 TAINT_set(save_taint);
9515 #ifdef NO_TAINT_SUPPORT
9516 PERL_UNUSED_VAR(save_taint);
9524 Perl_ck_lfun(pTHX_ OP *o)
9526 const OPCODE type = o->op_type;
9528 PERL_ARGS_ASSERT_CK_LFUN;
9530 return modkids(ck_fun(o), type);
9534 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
9536 PERL_ARGS_ASSERT_CK_DEFINED;
9538 if ((o->op_flags & OPf_KIDS)) {
9539 switch (cUNOPo->op_first->op_type) {
9542 case OP_AASSIGN: /* Is this a good idea? */
9543 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9544 "defined(@array) is deprecated");
9545 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9546 "\t(Maybe you should just omit the defined()?)\n");
9550 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9551 "defined(%%hash) is deprecated");
9552 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9553 "\t(Maybe you should just omit the defined()?)\n");
9564 Perl_ck_readline(pTHX_ OP *o)
9566 PERL_ARGS_ASSERT_CK_READLINE;
9568 if (o->op_flags & OPf_KIDS) {
9569 OP *kid = cLISTOPo->op_first;
9570 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
9574 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
9576 op_getmad(o,newop,'O');
9586 Perl_ck_rfun(pTHX_ OP *o)
9588 const OPCODE type = o->op_type;
9590 PERL_ARGS_ASSERT_CK_RFUN;
9592 return refkids(ck_fun(o), type);
9596 Perl_ck_listiob(pTHX_ OP *o)
9600 PERL_ARGS_ASSERT_CK_LISTIOB;
9602 kid = cLISTOPo->op_first;
9605 kid = cLISTOPo->op_first;
9607 if (kid->op_type == OP_PUSHMARK)
9608 kid = kid->op_sibling;
9609 if (kid && o->op_flags & OPf_STACKED)
9610 kid = kid->op_sibling;
9611 else if (kid && !kid->op_sibling) { /* print HANDLE; */
9612 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
9613 && !kid->op_folded) {
9614 o->op_flags |= OPf_STACKED; /* make it a filehandle */
9615 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
9616 cLISTOPo->op_first->op_sibling = kid;
9617 cLISTOPo->op_last = kid;
9618 kid = kid->op_sibling;
9623 op_append_elem(o->op_type, o, newDEFSVOP());
9625 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
9630 Perl_ck_smartmatch(pTHX_ OP *o)
9633 PERL_ARGS_ASSERT_CK_SMARTMATCH;
9634 if (0 == (o->op_flags & OPf_SPECIAL)) {
9635 OP *first = cBINOPo->op_first;
9636 OP *second = first->op_sibling;
9638 /* Implicitly take a reference to an array or hash */
9639 first->op_sibling = NULL;
9640 first = cBINOPo->op_first = ref_array_or_hash(first);
9641 second = first->op_sibling = ref_array_or_hash(second);
9643 /* Implicitly take a reference to a regular expression */
9644 if (first->op_type == OP_MATCH) {
9645 first->op_type = OP_QR;
9646 first->op_ppaddr = PL_ppaddr[OP_QR];
9648 if (second->op_type == OP_MATCH) {
9649 second->op_type = OP_QR;
9650 second->op_ppaddr = PL_ppaddr[OP_QR];
9659 Perl_ck_sassign(pTHX_ OP *o)
9662 OP * const kid = cLISTOPo->op_first;
9664 PERL_ARGS_ASSERT_CK_SASSIGN;
9666 /* has a disposable target? */
9667 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
9668 && !(kid->op_flags & OPf_STACKED)
9669 /* Cannot steal the second time! */
9670 && !(kid->op_private & OPpTARGET_MY)
9671 /* Keep the full thing for madskills */
9675 OP * const kkid = kid->op_sibling;
9677 /* Can just relocate the target. */
9678 if (kkid && kkid->op_type == OP_PADSV
9679 && !(kkid->op_private & OPpLVAL_INTRO))
9681 kid->op_targ = kkid->op_targ;
9683 /* Now we do not need PADSV and SASSIGN. */
9684 kid->op_sibling = o->op_sibling; /* NULL */
9685 cLISTOPo->op_first = NULL;
9688 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
9692 if (kid->op_sibling) {
9693 OP *kkid = kid->op_sibling;
9694 /* For state variable assignment, kkid is a list op whose op_last
9696 if ((kkid->op_type == OP_PADSV ||
9697 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
9698 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
9701 && (kkid->op_private & OPpLVAL_INTRO)
9702 && SvPAD_STATE(*av_fetch(PL_comppad_name, kkid->op_targ, FALSE))) {
9703 const PADOFFSET target = kkid->op_targ;
9704 OP *const other = newOP(OP_PADSV,
9706 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
9707 OP *const first = newOP(OP_NULL, 0);
9708 OP *const nullop = newCONDOP(0, first, o, other);
9709 OP *const condop = first->op_next;
9710 /* hijacking PADSTALE for uninitialized state variables */
9711 SvPADSTALE_on(PAD_SVl(target));
9713 condop->op_type = OP_ONCE;
9714 condop->op_ppaddr = PL_ppaddr[OP_ONCE];
9715 condop->op_targ = target;
9716 other->op_targ = target;
9718 /* Because we change the type of the op here, we will skip the
9719 assignment binop->op_last = binop->op_first->op_sibling; at the
9720 end of Perl_newBINOP(). So need to do it here. */
9721 cBINOPo->op_last = cBINOPo->op_first->op_sibling;
9730 Perl_ck_match(pTHX_ OP *o)
9734 PERL_ARGS_ASSERT_CK_MATCH;
9736 if (o->op_type != OP_QR && PL_compcv) {
9737 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
9738 if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS_isOUR(offset))) {
9739 o->op_targ = offset;
9740 o->op_private |= OPpTARGET_MY;
9743 if (o->op_type == OP_MATCH || o->op_type == OP_QR)
9744 o->op_private |= OPpRUNTIME;
9749 Perl_ck_method(pTHX_ OP *o)
9751 OP * const kid = cUNOPo->op_first;
9753 PERL_ARGS_ASSERT_CK_METHOD;
9755 if (kid->op_type == OP_CONST) {
9756 SV* sv = kSVOP->op_sv;
9757 const char * const method = SvPVX_const(sv);
9758 if (!(strchr(method, ':') || strchr(method, '\''))) {
9760 if (!SvIsCOW_shared_hash(sv)) {
9761 sv = newSVpvn_share(method, SvUTF8(sv) ? -(I32)SvCUR(sv) : (I32)SvCUR(sv), 0);
9764 kSVOP->op_sv = NULL;
9766 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
9768 op_getmad(o,cmop,'O');
9779 Perl_ck_null(pTHX_ OP *o)
9781 PERL_ARGS_ASSERT_CK_NULL;
9782 PERL_UNUSED_CONTEXT;
9787 Perl_ck_open(pTHX_ OP *o)
9791 PERL_ARGS_ASSERT_CK_OPEN;
9793 S_io_hints(aTHX_ o);
9795 /* In case of three-arg dup open remove strictness
9796 * from the last arg if it is a bareword. */
9797 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
9798 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
9802 if ((last->op_type == OP_CONST) && /* The bareword. */
9803 (last->op_private & OPpCONST_BARE) &&
9804 (last->op_private & OPpCONST_STRICT) &&
9805 (oa = first->op_sibling) && /* The fh. */
9806 (oa = oa->op_sibling) && /* The mode. */
9807 (oa->op_type == OP_CONST) &&
9808 SvPOK(((SVOP*)oa)->op_sv) &&
9809 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
9810 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
9811 (last == oa->op_sibling)) /* The bareword. */
9812 last->op_private &= ~OPpCONST_STRICT;
9818 Perl_ck_repeat(pTHX_ OP *o)
9820 PERL_ARGS_ASSERT_CK_REPEAT;
9822 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
9823 o->op_private |= OPpREPEAT_DOLIST;
9824 cBINOPo->op_first = force_list(cBINOPo->op_first);
9832 Perl_ck_require(pTHX_ OP *o)
9837 PERL_ARGS_ASSERT_CK_REQUIRE;
9839 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
9840 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9842 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
9843 SV * const sv = kid->op_sv;
9844 U32 was_readonly = SvREADONLY(sv);
9852 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
9857 for (; s < end; s++) {
9858 if (*s == ':' && s[1] == ':') {
9860 Move(s+2, s+1, end - s - 1, char);
9865 sv_catpvs(sv, ".pm");
9866 SvFLAGS(sv) |= was_readonly;
9870 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
9871 /* handle override, if any */
9872 && (gv = gv_override("require", 7))) {
9874 if (o->op_flags & OPf_KIDS) {
9875 kid = cUNOPo->op_first;
9876 cUNOPo->op_first = NULL;
9884 newop = S_new_entersubop(aTHX_ gv, kid);
9885 op_getmad(o,newop,'O');
9889 return scalar(ck_fun(o));
9893 Perl_ck_return(pTHX_ OP *o)
9898 PERL_ARGS_ASSERT_CK_RETURN;
9900 kid = cLISTOPo->op_first->op_sibling;
9901 if (CvLVALUE(PL_compcv)) {
9902 for (; kid; kid = kid->op_sibling)
9903 op_lvalue(kid, OP_LEAVESUBLV);
9910 Perl_ck_select(pTHX_ OP *o)
9915 PERL_ARGS_ASSERT_CK_SELECT;
9917 if (o->op_flags & OPf_KIDS) {
9918 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9919 if (kid && kid->op_sibling) {
9920 o->op_type = OP_SSELECT;
9921 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
9923 return fold_constants(op_integerize(op_std_init(o)));
9927 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9928 if (kid && kid->op_type == OP_RV2GV)
9929 kid->op_private &= ~HINT_STRICT_REFS;
9934 Perl_ck_shift(pTHX_ OP *o)
9937 const I32 type = o->op_type;
9939 PERL_ARGS_ASSERT_CK_SHIFT;
9941 if (!(o->op_flags & OPf_KIDS)) {
9944 if (!CvUNIQUE(PL_compcv)) {
9945 o->op_flags |= OPf_SPECIAL;
9949 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
9952 OP * const oldo = o;
9953 o = newUNOP(type, 0, scalar(argop));
9954 op_getmad(oldo,o,'O');
9959 return newUNOP(type, 0, scalar(argop));
9962 return scalar(ck_fun(o));
9966 Perl_ck_sort(pTHX_ OP *o)
9972 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;
9975 PERL_ARGS_ASSERT_CK_SORT;
9978 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
9980 const I32 sorthints = (I32)SvIV(*svp);
9981 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
9982 o->op_private |= OPpSORT_QSORT;
9983 if ((sorthints & HINT_SORT_STABLE) != 0)
9984 o->op_private |= OPpSORT_STABLE;
9988 if (o->op_flags & OPf_STACKED)
9990 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9992 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */
9993 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
9995 /* if the first arg is a code block, process it and mark sort as
9997 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
9999 if (kid->op_type == OP_LEAVE)
10000 op_null(kid); /* wipe out leave */
10001 /* Prevent execution from escaping out of the sort block. */
10004 /* provide scalar context for comparison function/block */
10005 kid = scalar(firstkid);
10006 kid->op_next = kid;
10007 o->op_flags |= OPf_SPECIAL;
10010 firstkid = firstkid->op_sibling;
10013 for (kid = firstkid; kid; kid = kid->op_sibling) {
10014 /* provide list context for arguments */
10017 op_lvalue(kid, OP_GREPSTART);
10023 /* for sort { X } ..., where X is one of
10024 * $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a
10025 * elide the second child of the sort (the one containing X),
10026 * and set these flags as appropriate
10030 * Also, check and warn on lexical $a, $b.
10034 S_simplify_sort(pTHX_ OP *o)
10037 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
10041 const char *gvname;
10044 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
10046 kid = kUNOP->op_first; /* get past null */
10047 if (!(have_scopeop = kid->op_type == OP_SCOPE)
10048 && kid->op_type != OP_LEAVE)
10050 kid = kLISTOP->op_last; /* get past scope */
10051 switch(kid->op_type) {
10055 if (!have_scopeop) goto padkids;
10060 k = kid; /* remember this node*/
10061 if (kBINOP->op_first->op_type != OP_RV2SV
10062 || kBINOP->op_last ->op_type != OP_RV2SV)
10065 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
10066 then used in a comparison. This catches most, but not
10067 all cases. For instance, it catches
10068 sort { my($a); $a <=> $b }
10070 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
10071 (although why you'd do that is anyone's guess).
10075 if (!ckWARN(WARN_SYNTAX)) return;
10076 kid = kBINOP->op_first;
10078 if (kid->op_type == OP_PADSV) {
10079 SV * const name = AvARRAY(PL_comppad_name)[kid->op_targ];
10080 if (SvCUR(name) == 2 && *SvPVX(name) == '$'
10081 && (SvPVX(name)[1] == 'a' || SvPVX(name)[1] == 'b'))
10082 /* diag_listed_as: "my %s" used in sort comparison */
10083 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10084 "\"%s %s\" used in sort comparison",
10085 SvPAD_STATE(name) ? "state" : "my",
10088 } while ((kid = kid->op_sibling));
10091 kid = kBINOP->op_first; /* get past cmp */
10092 if (kUNOP->op_first->op_type != OP_GV)
10094 kid = kUNOP->op_first; /* get past rv2sv */
10096 if (GvSTASH(gv) != PL_curstash)
10098 gvname = GvNAME(gv);
10099 if (*gvname == 'a' && gvname[1] == '\0')
10101 else if (*gvname == 'b' && gvname[1] == '\0')
10106 kid = k; /* back to cmp */
10107 /* already checked above that it is rv2sv */
10108 kid = kBINOP->op_last; /* down to 2nd arg */
10109 if (kUNOP->op_first->op_type != OP_GV)
10111 kid = kUNOP->op_first; /* get past rv2sv */
10113 if (GvSTASH(gv) != PL_curstash)
10115 gvname = GvNAME(gv);
10117 ? !(*gvname == 'a' && gvname[1] == '\0')
10118 : !(*gvname == 'b' && gvname[1] == '\0'))
10120 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
10122 o->op_private |= OPpSORT_DESCEND;
10123 if (k->op_type == OP_NCMP)
10124 o->op_private |= OPpSORT_NUMERIC;
10125 if (k->op_type == OP_I_NCMP)
10126 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
10127 kid = cLISTOPo->op_first->op_sibling;
10128 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
10130 op_getmad(kid,o,'S'); /* then delete it */
10132 op_free(kid); /* then delete it */
10137 Perl_ck_split(pTHX_ OP *o)
10142 PERL_ARGS_ASSERT_CK_SPLIT;
10144 if (o->op_flags & OPf_STACKED)
10145 return no_fh_allowed(o);
10147 kid = cLISTOPo->op_first;
10148 if (kid->op_type != OP_NULL)
10149 Perl_croak(aTHX_ "panic: ck_split, type=%u", (unsigned) kid->op_type);
10150 kid = kid->op_sibling;
10151 op_free(cLISTOPo->op_first);
10153 cLISTOPo->op_first = kid;
10155 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
10156 cLISTOPo->op_last = kid; /* There was only one element previously */
10159 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
10160 OP * const sibl = kid->op_sibling;
10161 kid->op_sibling = 0;
10162 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, 0, 0); /* OPf_SPECIAL is used to trigger split " " behavior */
10163 if (cLISTOPo->op_first == cLISTOPo->op_last)
10164 cLISTOPo->op_last = kid;
10165 cLISTOPo->op_first = kid;
10166 kid->op_sibling = sibl;
10169 kid->op_type = OP_PUSHRE;
10170 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
10172 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
10173 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
10174 "Use of /g modifier is meaningless in split");
10177 if (!kid->op_sibling)
10178 op_append_elem(OP_SPLIT, o, newDEFSVOP());
10180 kid = kid->op_sibling;
10183 if (!kid->op_sibling)
10185 op_append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
10186 o->op_private |= OPpSPLIT_IMPLIM;
10188 assert(kid->op_sibling);
10190 kid = kid->op_sibling;
10193 if (kid->op_sibling)
10194 return too_many_arguments_pv(o,OP_DESC(o), 0);
10200 Perl_ck_join(pTHX_ OP *o)
10202 const OP * const kid = cLISTOPo->op_first->op_sibling;
10204 PERL_ARGS_ASSERT_CK_JOIN;
10206 if (kid && kid->op_type == OP_MATCH) {
10207 if (ckWARN(WARN_SYNTAX)) {
10208 const REGEXP *re = PM_GETRE(kPMOP);
10210 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
10211 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
10212 : newSVpvs_flags( "STRING", SVs_TEMP );
10213 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10214 "/%"SVf"/ should probably be written as \"%"SVf"\"",
10215 SVfARG(msg), SVfARG(msg));
10222 =for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags
10224 Examines an op, which is expected to identify a subroutine at runtime,
10225 and attempts to determine at compile time which subroutine it identifies.
10226 This is normally used during Perl compilation to determine whether
10227 a prototype can be applied to a function call. I<cvop> is the op
10228 being considered, normally an C<rv2cv> op. A pointer to the identified
10229 subroutine is returned, if it could be determined statically, and a null
10230 pointer is returned if it was not possible to determine statically.
10232 Currently, the subroutine can be identified statically if the RV that the
10233 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
10234 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
10235 suitable if the constant value must be an RV pointing to a CV. Details of
10236 this process may change in future versions of Perl. If the C<rv2cv> op
10237 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
10238 the subroutine statically: this flag is used to suppress compile-time
10239 magic on a subroutine call, forcing it to use default runtime behaviour.
10241 If I<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
10242 of a GV reference is modified. If a GV was examined and its CV slot was
10243 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
10244 If the op is not optimised away, and the CV slot is later populated with
10245 a subroutine having a prototype, that flag eventually triggers the warning
10246 "called too early to check prototype".
10248 If I<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
10249 of returning a pointer to the subroutine it returns a pointer to the
10250 GV giving the most appropriate name for the subroutine in this context.
10251 Normally this is just the C<CvGV> of the subroutine, but for an anonymous
10252 (C<CvANON>) subroutine that is referenced through a GV it will be the
10253 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
10254 A null pointer is returned as usual if there is no statically-determinable
10260 /* shared by toke.c:yylex */
10262 Perl_find_lexical_cv(pTHX_ PADOFFSET off)
10264 PADNAME *name = PAD_COMPNAME(off);
10265 CV *compcv = PL_compcv;
10266 while (PadnameOUTER(name)) {
10267 assert(PARENT_PAD_INDEX(name));
10268 compcv = CvOUTSIDE(PL_compcv);
10269 name = PadlistNAMESARRAY(CvPADLIST(compcv))
10270 [off = PARENT_PAD_INDEX(name)];
10272 assert(!PadnameIsOUR(name));
10273 if (!PadnameIsSTATE(name) && SvMAGICAL(name)) {
10274 MAGIC * mg = mg_find(name, PERL_MAGIC_proto);
10276 assert(mg->mg_obj);
10277 return (CV *)mg->mg_obj;
10279 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
10283 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
10288 PERL_ARGS_ASSERT_RV2CV_OP_CV;
10289 if (flags & ~(RV2CVOPCV_MARK_EARLY|RV2CVOPCV_RETURN_NAME_GV))
10290 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
10291 if (cvop->op_type != OP_RV2CV)
10293 if (cvop->op_private & OPpENTERSUB_AMPER)
10295 if (!(cvop->op_flags & OPf_KIDS))
10297 rvop = cUNOPx(cvop)->op_first;
10298 switch (rvop->op_type) {
10300 gv = cGVOPx_gv(rvop);
10303 if (flags & RV2CVOPCV_MARK_EARLY)
10304 rvop->op_private |= OPpEARLY_CV;
10309 SV *rv = cSVOPx_sv(rvop);
10312 cv = (CV*)SvRV(rv);
10316 cv = find_lexical_cv(rvop->op_targ);
10323 if (SvTYPE((SV*)cv) != SVt_PVCV)
10325 if (flags & RV2CVOPCV_RETURN_NAME_GV) {
10326 if (!CvANON(cv) || !gv)
10335 =for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop
10337 Performs the default fixup of the arguments part of an C<entersub>
10338 op tree. This consists of applying list context to each of the
10339 argument ops. This is the standard treatment used on a call marked
10340 with C<&>, or a method call, or a call through a subroutine reference,
10341 or any other call where the callee can't be identified at compile time,
10342 or a call where the callee has no prototype.
10348 Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
10351 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
10352 aop = cUNOPx(entersubop)->op_first;
10353 if (!aop->op_sibling)
10354 aop = cUNOPx(aop)->op_first;
10355 for (aop = aop->op_sibling; aop->op_sibling; aop = aop->op_sibling) {
10356 if (!(PL_madskills && aop->op_type == OP_STUB)) {
10358 op_lvalue(aop, OP_ENTERSUB);
10365 =for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv
10367 Performs the fixup of the arguments part of an C<entersub> op tree
10368 based on a subroutine prototype. This makes various modifications to
10369 the argument ops, from applying context up to inserting C<refgen> ops,
10370 and checking the number and syntactic types of arguments, as directed by
10371 the prototype. This is the standard treatment used on a subroutine call,
10372 not marked with C<&>, where the callee can be identified at compile time
10373 and has a prototype.
10375 I<protosv> supplies the subroutine prototype to be applied to the call.
10376 It may be a normal defined scalar, of which the string value will be used.
10377 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
10378 that has been cast to C<SV*>) which has a prototype. The prototype
10379 supplied, in whichever form, does not need to match the actual callee
10380 referenced by the op tree.
10382 If the argument ops disagree with the prototype, for example by having
10383 an unacceptable number of arguments, a valid op tree is returned anyway.
10384 The error is reflected in the parser state, normally resulting in a single
10385 exception at the top level of parsing which covers all the compilation
10386 errors that occurred. In the error message, the callee is referred to
10387 by the name defined by the I<namegv> parameter.
10393 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
10396 const char *proto, *proto_end;
10397 OP *aop, *prev, *cvop;
10400 I32 contextclass = 0;
10401 const char *e = NULL;
10402 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
10403 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
10404 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
10405 "flags=%lx", (unsigned long) SvFLAGS(protosv));
10406 if (SvTYPE(protosv) == SVt_PVCV)
10407 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
10408 else proto = SvPV(protosv, proto_len);
10409 proto = S_strip_spaces(aTHX_ proto, &proto_len);
10410 proto_end = proto + proto_len;
10411 aop = cUNOPx(entersubop)->op_first;
10412 if (!aop->op_sibling)
10413 aop = cUNOPx(aop)->op_first;
10415 aop = aop->op_sibling;
10416 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10417 while (aop != cvop) {
10419 if (PL_madskills && aop->op_type == OP_STUB) {
10420 aop = aop->op_sibling;
10423 if (PL_madskills && aop->op_type == OP_NULL)
10424 o3 = ((UNOP*)aop)->op_first;
10428 if (proto >= proto_end)
10429 return too_many_arguments_sv(entersubop, gv_ename(namegv), 0);
10437 /* _ must be at the end */
10438 if (proto[1] && !strchr(";@%", proto[1]))
10453 if (o3->op_type != OP_REFGEN && o3->op_type != OP_UNDEF)
10455 arg == 1 ? "block or sub {}" : "sub {}",
10459 /* '*' allows any scalar type, including bareword */
10462 if (o3->op_type == OP_RV2GV)
10463 goto wrapref; /* autoconvert GLOB -> GLOBref */
10464 else if (o3->op_type == OP_CONST)
10465 o3->op_private &= ~OPpCONST_STRICT;
10466 else if (o3->op_type == OP_ENTERSUB) {
10467 /* accidental subroutine, revert to bareword */
10468 OP *gvop = ((UNOP*)o3)->op_first;
10469 if (gvop && gvop->op_type == OP_NULL) {
10470 gvop = ((UNOP*)gvop)->op_first;
10472 for (; gvop->op_sibling; gvop = gvop->op_sibling)
10475 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
10476 (gvop = ((UNOP*)gvop)->op_first) &&
10477 gvop->op_type == OP_GV)
10479 GV * const gv = cGVOPx_gv(gvop);
10480 OP * const sibling = aop->op_sibling;
10481 SV * const n = newSVpvs("");
10483 OP * const oldaop = aop;
10487 gv_fullname4(n, gv, "", FALSE);
10488 aop = newSVOP(OP_CONST, 0, n);
10489 op_getmad(oldaop,aop,'O');
10490 prev->op_sibling = aop;
10491 aop->op_sibling = sibling;
10501 if (o3->op_type == OP_RV2AV ||
10502 o3->op_type == OP_PADAV ||
10503 o3->op_type == OP_RV2HV ||
10504 o3->op_type == OP_PADHV
10510 case '[': case ']':
10517 switch (*proto++) {
10519 if (contextclass++ == 0) {
10520 e = strchr(proto, ']');
10521 if (!e || e == proto)
10529 if (contextclass) {
10530 const char *p = proto;
10531 const char *const end = proto;
10533 while (*--p != '[')
10534 /* \[$] accepts any scalar lvalue */
10536 && Perl_op_lvalue_flags(aTHX_
10538 OP_READ, /* not entersub */
10541 bad_type_gv(arg, Perl_form(aTHX_ "one of %.*s",
10542 (int)(end - p), p),
10548 if (o3->op_type == OP_RV2GV)
10551 bad_type_gv(arg, "symbol", namegv, 0, o3);
10554 if (o3->op_type == OP_ENTERSUB)
10557 bad_type_gv(arg, "subroutine entry", namegv, 0,
10561 if (o3->op_type == OP_RV2SV ||
10562 o3->op_type == OP_PADSV ||
10563 o3->op_type == OP_HELEM ||
10564 o3->op_type == OP_AELEM)
10566 if (!contextclass) {
10567 /* \$ accepts any scalar lvalue */
10568 if (Perl_op_lvalue_flags(aTHX_
10570 OP_READ, /* not entersub */
10573 bad_type_gv(arg, "scalar", namegv, 0, o3);
10577 if (o3->op_type == OP_RV2AV ||
10578 o3->op_type == OP_PADAV)
10581 bad_type_gv(arg, "array", namegv, 0, o3);
10584 if (o3->op_type == OP_RV2HV ||
10585 o3->op_type == OP_PADHV)
10588 bad_type_gv(arg, "hash", namegv, 0, o3);
10592 OP* const kid = aop;
10593 OP* const sib = kid->op_sibling;
10594 kid->op_sibling = 0;
10595 aop = newUNOP(OP_REFGEN, 0, kid);
10596 aop->op_sibling = sib;
10597 prev->op_sibling = aop;
10599 if (contextclass && e) {
10604 default: goto oops;
10614 SV* const tmpsv = sv_newmortal();
10615 gv_efullname3(tmpsv, namegv, NULL);
10616 Perl_croak(aTHX_ "Malformed prototype for %"SVf": %"SVf,
10617 SVfARG(tmpsv), SVfARG(protosv));
10621 op_lvalue(aop, OP_ENTERSUB);
10623 aop = aop->op_sibling;
10625 if (aop == cvop && *proto == '_') {
10626 /* generate an access to $_ */
10627 aop = newDEFSVOP();
10628 aop->op_sibling = prev->op_sibling;
10629 prev->op_sibling = aop; /* instead of cvop */
10631 if (!optional && proto_end > proto &&
10632 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
10633 return too_few_arguments_sv(entersubop, gv_ename(namegv), 0);
10638 =for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv
10640 Performs the fixup of the arguments part of an C<entersub> op tree either
10641 based on a subroutine prototype or using default list-context processing.
10642 This is the standard treatment used on a subroutine call, not marked
10643 with C<&>, where the callee can be identified at compile time.
10645 I<protosv> supplies the subroutine prototype to be applied to the call,
10646 or indicates that there is no prototype. It may be a normal scalar,
10647 in which case if it is defined then the string value will be used
10648 as a prototype, and if it is undefined then there is no prototype.
10649 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
10650 that has been cast to C<SV*>), of which the prototype will be used if it
10651 has one. The prototype (or lack thereof) supplied, in whichever form,
10652 does not need to match the actual callee referenced by the op tree.
10654 If the argument ops disagree with the prototype, for example by having
10655 an unacceptable number of arguments, a valid op tree is returned anyway.
10656 The error is reflected in the parser state, normally resulting in a single
10657 exception at the top level of parsing which covers all the compilation
10658 errors that occurred. In the error message, the callee is referred to
10659 by the name defined by the I<namegv> parameter.
10665 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
10666 GV *namegv, SV *protosv)
10668 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
10669 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
10670 return ck_entersub_args_proto(entersubop, namegv, protosv);
10672 return ck_entersub_args_list(entersubop);
10676 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
10678 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv);
10679 OP *aop = cUNOPx(entersubop)->op_first;
10681 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
10685 if (!aop->op_sibling)
10686 aop = cUNOPx(aop)->op_first;
10687 aop = aop->op_sibling;
10688 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10689 if (PL_madskills) while (aop != cvop && aop->op_type == OP_STUB) {
10690 aop = aop->op_sibling;
10693 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0);
10695 op_free(entersubop);
10696 switch(GvNAME(namegv)[2]) {
10697 case 'F': return newSVOP(OP_CONST, 0,
10698 newSVpv(CopFILE(PL_curcop),0));
10699 case 'L': return newSVOP(
10701 Perl_newSVpvf(aTHX_
10702 "%"IVdf, (IV)CopLINE(PL_curcop)
10705 case 'P': return newSVOP(OP_CONST, 0,
10707 ? newSVhek(HvNAME_HEK(PL_curstash))
10718 bool seenarg = FALSE;
10720 if (!aop->op_sibling)
10721 aop = cUNOPx(aop)->op_first;
10724 aop = aop->op_sibling;
10725 prev->op_sibling = NULL;
10728 prev=cvop, cvop = cvop->op_sibling)
10730 if (PL_madskills && cvop->op_sibling
10731 && cvop->op_type != OP_STUB) seenarg = TRUE
10734 prev->op_sibling = NULL;
10735 flags = OPf_SPECIAL * !(cvop->op_private & OPpENTERSUB_NOPAREN);
10737 if (aop == cvop) aop = NULL;
10738 op_free(entersubop);
10740 if (opnum == OP_ENTEREVAL
10741 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9))
10742 flags |= OPpEVAL_BYTES <<8;
10744 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
10746 case OA_BASEOP_OR_UNOP:
10747 case OA_FILESTATOP:
10748 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags);
10752 if (!PL_madskills || seenarg)
10754 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0);
10757 return opnum == OP_RUNCV
10758 ? newPVOP(OP_RUNCV,0,NULL)
10761 return convert(opnum,0,aop);
10769 =for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p
10771 Retrieves the function that will be used to fix up a call to I<cv>.
10772 Specifically, the function is applied to an C<entersub> op tree for a
10773 subroutine call, not marked with C<&>, where the callee can be identified
10774 at compile time as I<cv>.
10776 The C-level function pointer is returned in I<*ckfun_p>, and an SV
10777 argument for it is returned in I<*ckobj_p>. The function is intended
10778 to be called in this manner:
10780 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
10782 In this call, I<entersubop> is a pointer to the C<entersub> op,
10783 which may be replaced by the check function, and I<namegv> is a GV
10784 supplying the name that should be used by the check function to refer
10785 to the callee of the C<entersub> op if it needs to emit any diagnostics.
10786 It is permitted to apply the check function in non-standard situations,
10787 such as to a call to a different subroutine or to a method call.
10789 By default, the function is
10790 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
10791 and the SV parameter is I<cv> itself. This implements standard
10792 prototype processing. It can be changed, for a particular subroutine,
10793 by L</cv_set_call_checker>.
10799 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
10802 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
10803 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
10805 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
10806 *ckobj_p = callmg->mg_obj;
10808 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
10809 *ckobj_p = (SV*)cv;
10814 =for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj
10816 Sets the function that will be used to fix up a call to I<cv>.
10817 Specifically, the function is applied to an C<entersub> op tree for a
10818 subroutine call, not marked with C<&>, where the callee can be identified
10819 at compile time as I<cv>.
10821 The C-level function pointer is supplied in I<ckfun>, and an SV argument
10822 for it is supplied in I<ckobj>. The function should be defined like this:
10824 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
10826 It is intended to be called in this manner:
10828 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
10830 In this call, I<entersubop> is a pointer to the C<entersub> op,
10831 which may be replaced by the check function, and I<namegv> is a GV
10832 supplying the name that should be used by the check function to refer
10833 to the callee of the C<entersub> op if it needs to emit any diagnostics.
10834 It is permitted to apply the check function in non-standard situations,
10835 such as to a call to a different subroutine or to a method call.
10837 The current setting for a particular CV can be retrieved by
10838 L</cv_get_call_checker>.
10844 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
10846 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
10847 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
10848 if (SvMAGICAL((SV*)cv))
10849 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
10852 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
10853 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
10854 if (callmg->mg_flags & MGf_REFCOUNTED) {
10855 SvREFCNT_dec(callmg->mg_obj);
10856 callmg->mg_flags &= ~MGf_REFCOUNTED;
10858 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
10859 callmg->mg_obj = ckobj;
10860 if (ckobj != (SV*)cv) {
10861 SvREFCNT_inc_simple_void_NN(ckobj);
10862 callmg->mg_flags |= MGf_REFCOUNTED;
10864 callmg->mg_flags |= MGf_COPY;
10869 Perl_ck_subr(pTHX_ OP *o)
10875 PERL_ARGS_ASSERT_CK_SUBR;
10877 aop = cUNOPx(o)->op_first;
10878 if (!aop->op_sibling)
10879 aop = cUNOPx(aop)->op_first;
10880 aop = aop->op_sibling;
10881 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10882 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
10883 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_RETURN_NAME_GV) : NULL;
10885 o->op_private &= ~1;
10886 o->op_private |= OPpENTERSUB_HASTARG;
10887 o->op_private |= (PL_hints & HINT_STRICT_REFS);
10888 if (PERLDB_SUB && PL_curstash != PL_debstash)
10889 o->op_private |= OPpENTERSUB_DB;
10890 if (cvop->op_type == OP_RV2CV) {
10891 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
10893 } else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
10894 if (aop->op_type == OP_CONST)
10895 aop->op_private &= ~OPpCONST_STRICT;
10896 else if (aop->op_type == OP_LIST) {
10897 OP * const sib = ((UNOP*)aop)->op_first->op_sibling;
10898 if (sib && sib->op_type == OP_CONST)
10899 sib->op_private &= ~OPpCONST_STRICT;
10904 return ck_entersub_args_list(o);
10906 Perl_call_checker ckfun;
10908 cv_get_call_checker(cv, &ckfun, &ckobj);
10909 if (!namegv) { /* expletive! */
10910 /* XXX The call checker API is public. And it guarantees that
10911 a GV will be provided with the right name. So we have
10912 to create a GV. But it is still not correct, as its
10913 stringification will include the package. What we
10914 really need is a new call checker API that accepts a
10915 GV or string (or GV or CV). */
10916 HEK * const hek = CvNAME_HEK(cv);
10917 /* After a syntax error in a lexical sub, the cv that
10918 rv2cv_op_cv returns may be a nameless stub. */
10919 if (!hek) return ck_entersub_args_list(o);;
10920 namegv = (GV *)sv_newmortal();
10921 gv_init_pvn(namegv, PL_curstash, HEK_KEY(hek), HEK_LEN(hek),
10922 SVf_UTF8 * !!HEK_UTF8(hek));
10924 return ckfun(aTHX_ o, namegv, ckobj);
10929 Perl_ck_svconst(pTHX_ OP *o)
10931 SV * const sv = cSVOPo->op_sv;
10932 PERL_ARGS_ASSERT_CK_SVCONST;
10933 PERL_UNUSED_CONTEXT;
10934 #ifdef PERL_OLD_COPY_ON_WRITE
10935 if (SvIsCOW(sv)) sv_force_normal(sv);
10936 #elif defined(PERL_NEW_COPY_ON_WRITE)
10937 /* Since the read-only flag may be used to protect a string buffer, we
10938 cannot do copy-on-write with existing read-only scalars that are not
10939 already copy-on-write scalars. To allow $_ = "hello" to do COW with
10940 that constant, mark the constant as COWable here, if it is not
10941 already read-only. */
10942 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
10945 # ifdef PERL_DEBUG_READONLY_COW
10955 Perl_ck_trunc(pTHX_ OP *o)
10957 PERL_ARGS_ASSERT_CK_TRUNC;
10959 if (o->op_flags & OPf_KIDS) {
10960 SVOP *kid = (SVOP*)cUNOPo->op_first;
10962 if (kid->op_type == OP_NULL)
10963 kid = (SVOP*)kid->op_sibling;
10964 if (kid && kid->op_type == OP_CONST &&
10965 (kid->op_private & OPpCONST_BARE) &&
10968 o->op_flags |= OPf_SPECIAL;
10969 kid->op_private &= ~OPpCONST_STRICT;
10976 Perl_ck_substr(pTHX_ OP *o)
10978 PERL_ARGS_ASSERT_CK_SUBSTR;
10981 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
10982 OP *kid = cLISTOPo->op_first;
10984 if (kid->op_type == OP_NULL)
10985 kid = kid->op_sibling;
10987 kid->op_flags |= OPf_MOD;
10994 Perl_ck_tell(pTHX_ OP *o)
10996 PERL_ARGS_ASSERT_CK_TELL;
10998 if (o->op_flags & OPf_KIDS) {
10999 OP *kid = cLISTOPo->op_first;
11000 if (kid->op_type == OP_NULL && kid->op_sibling) kid = kid->op_sibling;
11001 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
11007 Perl_ck_each(pTHX_ OP *o)
11010 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
11011 const unsigned orig_type = o->op_type;
11012 const unsigned array_type = orig_type == OP_EACH ? OP_AEACH
11013 : orig_type == OP_KEYS ? OP_AKEYS : OP_AVALUES;
11014 const unsigned ref_type = orig_type == OP_EACH ? OP_REACH
11015 : orig_type == OP_KEYS ? OP_RKEYS : OP_RVALUES;
11017 PERL_ARGS_ASSERT_CK_EACH;
11020 switch (kid->op_type) {
11026 CHANGE_TYPE(o, array_type);
11029 if (kid->op_private == OPpCONST_BARE
11030 || !SvROK(cSVOPx_sv(kid))
11031 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
11032 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
11034 /* we let ck_fun handle it */
11037 CHANGE_TYPE(o, ref_type);
11041 /* if treating as a reference, defer additional checks to runtime */
11042 if (o->op_type == ref_type) {
11043 /* diag_listed_as: keys on reference is experimental */
11044 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__AUTODEREF),
11045 "%s is experimental", PL_op_desc[ref_type]);
11052 Perl_ck_length(pTHX_ OP *o)
11054 PERL_ARGS_ASSERT_CK_LENGTH;
11058 if (ckWARN(WARN_SYNTAX)) {
11059 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
11063 const bool hash = kid->op_type == OP_PADHV
11064 || kid->op_type == OP_RV2HV;
11065 switch (kid->op_type) {
11070 name = S_op_varname(aTHX_ kid);
11076 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11077 "length() used on %"SVf" (did you mean \"scalar(%s%"SVf
11079 name, hash ? "keys " : "", name
11082 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11083 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11084 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
11086 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11087 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11088 "length() used on @array (did you mean \"scalar(@array)\"?)");
11095 /* Check for in place reverse and sort assignments like "@a = reverse @a"
11096 and modify the optree to make them work inplace */
11099 S_inplace_aassign(pTHX_ OP *o) {
11101 OP *modop, *modop_pushmark;
11103 OP *oleft, *oleft_pushmark;
11105 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
11107 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
11109 assert(cUNOPo->op_first->op_type == OP_NULL);
11110 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
11111 assert(modop_pushmark->op_type == OP_PUSHMARK);
11112 modop = modop_pushmark->op_sibling;
11114 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
11117 /* no other operation except sort/reverse */
11118 if (modop->op_sibling)
11121 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
11122 if (!(oright = cUNOPx(modop)->op_first->op_sibling)) return;
11124 if (modop->op_flags & OPf_STACKED) {
11125 /* skip sort subroutine/block */
11126 assert(oright->op_type == OP_NULL);
11127 oright = oright->op_sibling;
11130 assert(cUNOPo->op_first->op_sibling->op_type == OP_NULL);
11131 oleft_pushmark = cUNOPx(cUNOPo->op_first->op_sibling)->op_first;
11132 assert(oleft_pushmark->op_type == OP_PUSHMARK);
11133 oleft = oleft_pushmark->op_sibling;
11135 /* Check the lhs is an array */
11137 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
11138 || oleft->op_sibling
11139 || (oleft->op_private & OPpLVAL_INTRO)
11143 /* Only one thing on the rhs */
11144 if (oright->op_sibling)
11147 /* check the array is the same on both sides */
11148 if (oleft->op_type == OP_RV2AV) {
11149 if (oright->op_type != OP_RV2AV
11150 || !cUNOPx(oright)->op_first
11151 || cUNOPx(oright)->op_first->op_type != OP_GV
11152 || cUNOPx(oleft )->op_first->op_type != OP_GV
11153 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
11154 cGVOPx_gv(cUNOPx(oright)->op_first)
11158 else if (oright->op_type != OP_PADAV
11159 || oright->op_targ != oleft->op_targ
11163 /* This actually is an inplace assignment */
11165 modop->op_private |= OPpSORT_INPLACE;
11167 /* transfer MODishness etc from LHS arg to RHS arg */
11168 oright->op_flags = oleft->op_flags;
11170 /* remove the aassign op and the lhs */
11172 op_null(oleft_pushmark);
11173 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
11174 op_null(cUNOPx(oleft)->op_first);
11180 /* mechanism for deferring recursion in rpeep() */
11182 #define MAX_DEFERRED 4
11186 if (defer_ix == (MAX_DEFERRED-1)) { \
11187 OP **defer = defer_queue[defer_base]; \
11188 CALL_RPEEP(*defer); \
11189 S_prune_chain_head(aTHX_ defer); \
11190 defer_base = (defer_base + 1) % MAX_DEFERRED; \
11193 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
11196 #define IS_AND_OP(o) (o->op_type == OP_AND)
11197 #define IS_OR_OP(o) (o->op_type == OP_OR)
11201 S_null_listop_in_list_context(pTHX_ OP *o)
11205 PERL_ARGS_ASSERT_NULL_LISTOP_IN_LIST_CONTEXT;
11207 /* This is an OP_LIST in list context. That means we
11208 * can ditch the OP_LIST and the OP_PUSHMARK within. */
11210 kid = cLISTOPo->op_first;
11211 /* Find the end of the chain of OPs executed within the OP_LIST. */
11212 while (kid->op_next != o)
11213 kid = kid->op_next;
11215 kid->op_next = o->op_next; /* patch list out of exec chain */
11216 op_null(cUNOPo->op_first); /* NULL the pushmark */
11217 op_null(o); /* NULL the list */
11220 /* A peephole optimizer. We visit the ops in the order they're to execute.
11221 * See the comments at the top of this file for more details about when
11222 * peep() is called */
11225 Perl_rpeep(pTHX_ OP *o)
11229 OP* oldoldop = NULL;
11230 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
11231 int defer_base = 0;
11234 if (!o || o->op_opt)
11238 SAVEVPTR(PL_curcop);
11239 for (;; o = o->op_next) {
11240 if (o && o->op_opt)
11243 while (defer_ix >= 0) {
11245 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
11246 CALL_RPEEP(*defer);
11247 S_prune_chain_head(aTHX_ defer);
11252 /* By default, this op has now been optimised. A couple of cases below
11253 clear this again. */
11258 /* The following will have the OP_LIST and OP_PUSHMARK
11259 * patched out later IF the OP_LIST is in list context.
11260 * So in that case, we can set the this OP's op_next
11261 * to skip to after the OP_PUSHMARK:
11267 * will eventually become:
11270 * - ex-pushmark -> -
11276 OP *other_pushmark;
11277 if (OP_TYPE_IS(o->op_next, OP_PUSHMARK)
11278 && (sibling = o->op_sibling)
11279 && sibling->op_type == OP_LIST
11280 /* This KIDS check is likely superfluous since OP_LIST
11281 * would otherwise be an OP_STUB. */
11282 && sibling->op_flags & OPf_KIDS
11283 && (sibling->op_flags & OPf_WANT) == OPf_WANT_LIST
11284 && (other_pushmark = cLISTOPx(sibling)->op_first)
11285 /* Pointer equality also effectively checks that it's a
11287 && other_pushmark == o->op_next)
11289 o->op_next = other_pushmark->op_next;
11290 null_listop_in_list_context(sibling);
11294 switch (o->op_type) {
11296 PL_curcop = ((COP*)o); /* for warnings */
11299 PL_curcop = ((COP*)o); /* for warnings */
11301 /* Optimise a "return ..." at the end of a sub to just be "...".
11302 * This saves 2 ops. Before:
11303 * 1 <;> nextstate(main 1 -e:1) v ->2
11304 * 4 <@> return K ->5
11305 * 2 <0> pushmark s ->3
11306 * - <1> ex-rv2sv sK/1 ->4
11307 * 3 <#> gvsv[*cat] s ->4
11310 * - <@> return K ->-
11311 * - <0> pushmark s ->2
11312 * - <1> ex-rv2sv sK/1 ->-
11313 * 2 <$> gvsv(*cat) s ->3
11316 OP *next = o->op_next;
11317 OP *sibling = o->op_sibling;
11318 if ( OP_TYPE_IS(next, OP_PUSHMARK)
11319 && OP_TYPE_IS(sibling, OP_RETURN)
11320 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
11321 && OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
11322 && cUNOPx(sibling)->op_first == next
11323 && next->op_sibling && next->op_sibling->op_next
11326 /* Look through the PUSHMARK's siblings for one that
11327 * points to the RETURN */
11328 OP *top = next->op_sibling;
11329 while (top && top->op_next) {
11330 if (top->op_next == sibling) {
11331 top->op_next = sibling->op_next;
11332 o->op_next = next->op_next;
11335 top = top->op_sibling;
11340 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
11342 * This latter form is then suitable for conversion into padrange
11343 * later on. Convert:
11345 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
11349 * nextstate1 -> listop -> nextstate3
11351 * pushmark -> padop1 -> padop2
11353 if (o->op_next && (
11354 o->op_next->op_type == OP_PADSV
11355 || o->op_next->op_type == OP_PADAV
11356 || o->op_next->op_type == OP_PADHV
11358 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
11359 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
11360 && o->op_next->op_next->op_next && (
11361 o->op_next->op_next->op_next->op_type == OP_PADSV
11362 || o->op_next->op_next->op_next->op_type == OP_PADAV
11363 || o->op_next->op_next->op_next->op_type == OP_PADHV
11365 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
11366 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
11367 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
11368 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
11374 first = o->op_next;
11375 last = o->op_next->op_next->op_next;
11377 newop = newLISTOP(OP_LIST, 0, first, last);
11378 newop->op_flags |= OPf_PARENS;
11379 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
11381 /* Kill nextstate2 between padop1/padop2 */
11382 op_free(first->op_next);
11384 first->op_next = last; /* padop2 */
11385 first->op_sibling = last; /* ... */
11386 o->op_next = cUNOPx(newop)->op_first; /* pushmark */
11387 o->op_next->op_next = first; /* padop1 */
11388 o->op_next->op_sibling = first; /* ... */
11389 newop->op_next = last->op_next; /* nextstate3 */
11390 newop->op_sibling = last->op_sibling;
11391 last->op_next = newop; /* listop */
11392 last->op_sibling = NULL;
11393 o->op_sibling = newop; /* ... */
11395 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
11397 /* Ensure pushmark has this flag if padops do */
11398 if (first->op_flags & OPf_MOD && last->op_flags & OPf_MOD) {
11399 o->op_next->op_flags |= OPf_MOD;
11405 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
11406 to carry two labels. For now, take the easier option, and skip
11407 this optimisation if the first NEXTSTATE has a label. */
11408 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
11409 OP *nextop = o->op_next;
11410 while (nextop && nextop->op_type == OP_NULL)
11411 nextop = nextop->op_next;
11413 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
11414 COP *firstcop = (COP *)o;
11415 COP *secondcop = (COP *)nextop;
11416 /* We want the COP pointed to by o (and anything else) to
11417 become the next COP down the line. */
11418 cop_free(firstcop);
11420 firstcop->op_next = secondcop->op_next;
11422 /* Now steal all its pointers, and duplicate the other
11424 firstcop->cop_line = secondcop->cop_line;
11425 #ifdef USE_ITHREADS
11426 firstcop->cop_stashoff = secondcop->cop_stashoff;
11427 firstcop->cop_file = secondcop->cop_file;
11429 firstcop->cop_stash = secondcop->cop_stash;
11430 firstcop->cop_filegv = secondcop->cop_filegv;
11432 firstcop->cop_hints = secondcop->cop_hints;
11433 firstcop->cop_seq = secondcop->cop_seq;
11434 firstcop->cop_warnings = secondcop->cop_warnings;
11435 firstcop->cop_hints_hash = secondcop->cop_hints_hash;
11437 #ifdef USE_ITHREADS
11438 secondcop->cop_stashoff = 0;
11439 secondcop->cop_file = NULL;
11441 secondcop->cop_stash = NULL;
11442 secondcop->cop_filegv = NULL;
11444 secondcop->cop_warnings = NULL;
11445 secondcop->cop_hints_hash = NULL;
11447 /* If we use op_null(), and hence leave an ex-COP, some
11448 warnings are misreported. For example, the compile-time
11449 error in 'use strict; no strict refs;' */
11450 secondcop->op_type = OP_NULL;
11451 secondcop->op_ppaddr = PL_ppaddr[OP_NULL];
11457 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
11458 if (o->op_next->op_private & OPpTARGET_MY) {
11459 if (o->op_flags & OPf_STACKED) /* chained concats */
11460 break; /* ignore_optimization */
11462 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
11463 o->op_targ = o->op_next->op_targ;
11464 o->op_next->op_targ = 0;
11465 o->op_private |= OPpTARGET_MY;
11468 op_null(o->op_next);
11472 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
11473 break; /* Scalar stub must produce undef. List stub is noop */
11477 if (o->op_targ == OP_NEXTSTATE
11478 || o->op_targ == OP_DBSTATE)
11480 PL_curcop = ((COP*)o);
11482 /* XXX: We avoid setting op_seq here to prevent later calls
11483 to rpeep() from mistakenly concluding that optimisation
11484 has already occurred. This doesn't fix the real problem,
11485 though (See 20010220.007). AMS 20010719 */
11486 /* op_seq functionality is now replaced by op_opt */
11494 oldop->op_next = o->op_next;
11502 /* Convert a series of PAD ops for my vars plus support into a
11503 * single padrange op. Basically
11505 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
11507 * becomes, depending on circumstances, one of
11509 * padrange ----------------------------------> (list) -> rest
11510 * padrange --------------------------------------------> rest
11512 * where all the pad indexes are sequential and of the same type
11514 * We convert the pushmark into a padrange op, then skip
11515 * any other pad ops, and possibly some trailing ops.
11516 * Note that we don't null() the skipped ops, to make it
11517 * easier for Deparse to undo this optimisation (and none of
11518 * the skipped ops are holding any resourses). It also makes
11519 * it easier for find_uninit_var(), as it can just ignore
11520 * padrange, and examine the original pad ops.
11524 OP *followop = NULL; /* the op that will follow the padrange op */
11527 PADOFFSET base = 0; /* init only to stop compiler whining */
11528 U8 gimme = 0; /* init only to stop compiler whining */
11529 bool defav = 0; /* seen (...) = @_ */
11530 bool reuse = 0; /* reuse an existing padrange op */
11532 /* look for a pushmark -> gv[_] -> rv2av */
11538 if ( p->op_type == OP_GV
11539 && (gv = cGVOPx_gv(p))
11540 && GvNAMELEN_get(gv) == 1
11541 && *GvNAME_get(gv) == '_'
11542 && GvSTASH(gv) == PL_defstash
11543 && (rv2av = p->op_next)
11544 && rv2av->op_type == OP_RV2AV
11545 && !(rv2av->op_flags & OPf_REF)
11546 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
11547 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
11548 && o->op_sibling == rv2av /* these two for Deparse */
11549 && cUNOPx(rv2av)->op_first == p
11551 q = rv2av->op_next;
11552 if (q->op_type == OP_NULL)
11554 if (q->op_type == OP_PUSHMARK) {
11561 /* To allow Deparse to pessimise this, it needs to be able
11562 * to restore the pushmark's original op_next, which it
11563 * will assume to be the same as op_sibling. */
11564 if (o->op_next != o->op_sibling)
11569 /* scan for PAD ops */
11571 for (p = p->op_next; p; p = p->op_next) {
11572 if (p->op_type == OP_NULL)
11575 if (( p->op_type != OP_PADSV
11576 && p->op_type != OP_PADAV
11577 && p->op_type != OP_PADHV
11579 /* any private flag other than INTRO? e.g. STATE */
11580 || (p->op_private & ~OPpLVAL_INTRO)
11584 /* let $a[N] potentially be optimised into AELEMFAST_LEX
11586 if ( p->op_type == OP_PADAV
11588 && p->op_next->op_type == OP_CONST
11589 && p->op_next->op_next
11590 && p->op_next->op_next->op_type == OP_AELEM
11594 /* for 1st padop, note what type it is and the range
11595 * start; for the others, check that it's the same type
11596 * and that the targs are contiguous */
11598 intro = (p->op_private & OPpLVAL_INTRO);
11600 gimme = (p->op_flags & OPf_WANT);
11603 if ((p->op_private & OPpLVAL_INTRO) != intro)
11605 /* Note that you'd normally expect targs to be
11606 * contiguous in my($a,$b,$c), but that's not the case
11607 * when external modules start doing things, e.g.
11608 i* Function::Parameters */
11609 if (p->op_targ != base + count)
11611 assert(p->op_targ == base + count);
11612 /* all the padops should be in the same context */
11613 if (gimme != (p->op_flags & OPf_WANT))
11617 /* for AV, HV, only when we're not flattening */
11618 if ( p->op_type != OP_PADSV
11619 && gimme != OPf_WANT_VOID
11620 && !(p->op_flags & OPf_REF)
11624 if (count >= OPpPADRANGE_COUNTMASK)
11627 /* there's a biggest base we can fit into a
11628 * SAVEt_CLEARPADRANGE in pp_padrange */
11629 if (intro && base >
11630 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)))
11633 /* Success! We've got another valid pad op to optimise away */
11635 followop = p->op_next;
11641 /* pp_padrange in specifically compile-time void context
11642 * skips pushing a mark and lexicals; in all other contexts
11643 * (including unknown till runtime) it pushes a mark and the
11644 * lexicals. We must be very careful then, that the ops we
11645 * optimise away would have exactly the same effect as the
11647 * In particular in void context, we can only optimise to
11648 * a padrange if see see the complete sequence
11649 * pushmark, pad*v, ...., list, nextstate
11650 * which has the net effect of of leaving the stack empty
11651 * (for now we leave the nextstate in the execution chain, for
11652 * its other side-effects).
11655 if (gimme == OPf_WANT_VOID) {
11656 if (OP_TYPE_IS_OR_WAS(followop, OP_LIST)
11657 && gimme == (followop->op_flags & OPf_WANT)
11658 && ( followop->op_next->op_type == OP_NEXTSTATE
11659 || followop->op_next->op_type == OP_DBSTATE))
11661 followop = followop->op_next; /* skip OP_LIST */
11663 /* consolidate two successive my(...);'s */
11666 && oldoldop->op_type == OP_PADRANGE
11667 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
11668 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
11669 && !(oldoldop->op_flags & OPf_SPECIAL)
11672 assert(oldoldop->op_next == oldop);
11673 assert( oldop->op_type == OP_NEXTSTATE
11674 || oldop->op_type == OP_DBSTATE);
11675 assert(oldop->op_next == o);
11678 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
11680 /* Do not assume pad offsets for $c and $d are con-
11685 if ( oldoldop->op_targ + old_count == base
11686 && old_count < OPpPADRANGE_COUNTMASK - count) {
11687 base = oldoldop->op_targ;
11688 count += old_count;
11693 /* if there's any immediately following singleton
11694 * my var's; then swallow them and the associated
11696 * my ($a,$b); my $c; my $d;
11698 * my ($a,$b,$c,$d);
11701 while ( ((p = followop->op_next))
11702 && ( p->op_type == OP_PADSV
11703 || p->op_type == OP_PADAV
11704 || p->op_type == OP_PADHV)
11705 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
11706 && (p->op_private & OPpLVAL_INTRO) == intro
11707 && !(p->op_private & ~OPpLVAL_INTRO)
11709 && ( p->op_next->op_type == OP_NEXTSTATE
11710 || p->op_next->op_type == OP_DBSTATE)
11711 && count < OPpPADRANGE_COUNTMASK
11712 && base + count == p->op_targ
11715 followop = p->op_next;
11723 assert(oldoldop->op_type == OP_PADRANGE);
11724 oldoldop->op_next = followop;
11725 oldoldop->op_private = (intro | count);
11731 /* Convert the pushmark into a padrange.
11732 * To make Deparse easier, we guarantee that a padrange was
11733 * *always* formerly a pushmark */
11734 assert(o->op_type == OP_PUSHMARK);
11735 o->op_next = followop;
11736 o->op_type = OP_PADRANGE;
11737 o->op_ppaddr = PL_ppaddr[OP_PADRANGE];
11739 /* bit 7: INTRO; bit 6..0: count */
11740 o->op_private = (intro | count);
11741 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
11742 | gimme | (defav ? OPf_SPECIAL : 0));
11749 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
11750 OP* const pop = (o->op_type == OP_PADAV) ?
11751 o->op_next : o->op_next->op_next;
11753 if (pop && pop->op_type == OP_CONST &&
11754 ((PL_op = pop->op_next)) &&
11755 pop->op_next->op_type == OP_AELEM &&
11756 !(pop->op_next->op_private &
11757 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
11758 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
11761 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
11762 no_bareword_allowed(pop);
11763 if (o->op_type == OP_GV)
11764 op_null(o->op_next);
11765 op_null(pop->op_next);
11767 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
11768 o->op_next = pop->op_next->op_next;
11769 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
11770 o->op_private = (U8)i;
11771 if (o->op_type == OP_GV) {
11774 o->op_type = OP_AELEMFAST;
11777 o->op_type = OP_AELEMFAST_LEX;
11782 if (o->op_next->op_type == OP_RV2SV) {
11783 if (!(o->op_next->op_private & OPpDEREF)) {
11784 op_null(o->op_next);
11785 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
11787 o->op_next = o->op_next->op_next;
11788 o->op_type = OP_GVSV;
11789 o->op_ppaddr = PL_ppaddr[OP_GVSV];
11792 else if (o->op_next->op_type == OP_READLINE
11793 && o->op_next->op_next->op_type == OP_CONCAT
11794 && (o->op_next->op_next->op_flags & OPf_STACKED))
11796 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
11797 o->op_type = OP_RCATLINE;
11798 o->op_flags |= OPf_STACKED;
11799 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
11800 op_null(o->op_next->op_next);
11801 op_null(o->op_next);
11810 #define HV_OR_SCALARHV(op) \
11811 ( (op)->op_type == OP_PADHV || (op)->op_type == OP_RV2HV \
11813 : (op)->op_type == OP_SCALAR && (op)->op_flags & OPf_KIDS \
11814 && ( cUNOPx(op)->op_first->op_type == OP_PADHV \
11815 || cUNOPx(op)->op_first->op_type == OP_RV2HV) \
11816 ? cUNOPx(op)->op_first \
11820 if ((fop = HV_OR_SCALARHV(cUNOP->op_first)))
11821 fop->op_private |= OPpTRUEBOOL;
11827 fop = cLOGOP->op_first;
11828 sop = fop->op_sibling;
11829 while (cLOGOP->op_other->op_type == OP_NULL)
11830 cLOGOP->op_other = cLOGOP->op_other->op_next;
11831 while (o->op_next && ( o->op_type == o->op_next->op_type
11832 || o->op_next->op_type == OP_NULL))
11833 o->op_next = o->op_next->op_next;
11835 /* if we're an OR and our next is a AND in void context, we'll
11836 follow it's op_other on short circuit, same for reverse.
11837 We can't do this with OP_DOR since if it's true, its return
11838 value is the underlying value which must be evaluated
11842 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
11843 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
11845 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
11847 o->op_next = ((LOGOP*)o->op_next)->op_other;
11849 DEFER(cLOGOP->op_other);
11852 fop = HV_OR_SCALARHV(fop);
11853 if (sop) sop = HV_OR_SCALARHV(sop);
11858 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) {
11859 while (nop && nop->op_next) {
11860 switch (nop->op_next->op_type) {
11865 lop = nop = nop->op_next;
11868 nop = nop->op_next;
11877 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
11878 || o->op_type == OP_AND )
11879 fop->op_private |= OPpTRUEBOOL;
11880 else if (!(lop->op_flags & OPf_WANT))
11881 fop->op_private |= OPpMAYBE_TRUEBOOL;
11883 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
11885 sop->op_private |= OPpTRUEBOOL;
11892 if ((fop = HV_OR_SCALARHV(cLOGOP->op_first)))
11893 fop->op_private |= OPpTRUEBOOL;
11894 #undef HV_OR_SCALARHV
11905 while (cLOGOP->op_other->op_type == OP_NULL)
11906 cLOGOP->op_other = cLOGOP->op_other->op_next;
11907 DEFER(cLOGOP->op_other);
11912 while (cLOOP->op_redoop->op_type == OP_NULL)
11913 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
11914 while (cLOOP->op_nextop->op_type == OP_NULL)
11915 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
11916 while (cLOOP->op_lastop->op_type == OP_NULL)
11917 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
11918 /* a while(1) loop doesn't have an op_next that escapes the
11919 * loop, so we have to explicitly follow the op_lastop to
11920 * process the rest of the code */
11921 DEFER(cLOOP->op_lastop);
11925 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
11926 DEFER(cLOGOPo->op_other);
11930 assert(!(cPMOP->op_pmflags & PMf_ONCE));
11931 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
11932 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
11933 cPMOP->op_pmstashstartu.op_pmreplstart
11934 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
11935 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
11941 if (o->op_flags & OPf_SPECIAL) {
11942 /* first arg is a code block */
11943 OP * const nullop = cLISTOP->op_first->op_sibling;
11944 OP * kid = cUNOPx(nullop)->op_first;
11946 assert(nullop->op_type == OP_NULL);
11947 assert(kid->op_type == OP_SCOPE
11948 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
11949 /* since OP_SORT doesn't have a handy op_other-style
11950 * field that can point directly to the start of the code
11951 * block, store it in the otherwise-unused op_next field
11952 * of the top-level OP_NULL. This will be quicker at
11953 * run-time, and it will also allow us to remove leading
11954 * OP_NULLs by just messing with op_nexts without
11955 * altering the basic op_first/op_sibling layout. */
11956 kid = kLISTOP->op_first;
11958 (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
11959 || kid->op_type == OP_STUB
11960 || kid->op_type == OP_ENTER);
11961 nullop->op_next = kLISTOP->op_next;
11962 DEFER(nullop->op_next);
11965 /* check that RHS of sort is a single plain array */
11966 oright = cUNOPo->op_first;
11967 if (!oright || oright->op_type != OP_PUSHMARK)
11970 if (o->op_private & OPpSORT_INPLACE)
11973 /* reverse sort ... can be optimised. */
11974 if (!cUNOPo->op_sibling) {
11975 /* Nothing follows us on the list. */
11976 OP * const reverse = o->op_next;
11978 if (reverse->op_type == OP_REVERSE &&
11979 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
11980 OP * const pushmark = cUNOPx(reverse)->op_first;
11981 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
11982 && (cUNOPx(pushmark)->op_sibling == o)) {
11983 /* reverse -> pushmark -> sort */
11984 o->op_private |= OPpSORT_REVERSE;
11986 pushmark->op_next = oright->op_next;
11996 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
11998 LISTOP *enter, *exlist;
12000 if (o->op_private & OPpSORT_INPLACE)
12003 enter = (LISTOP *) o->op_next;
12006 if (enter->op_type == OP_NULL) {
12007 enter = (LISTOP *) enter->op_next;
12011 /* for $a (...) will have OP_GV then OP_RV2GV here.
12012 for (...) just has an OP_GV. */
12013 if (enter->op_type == OP_GV) {
12014 gvop = (OP *) enter;
12015 enter = (LISTOP *) enter->op_next;
12018 if (enter->op_type == OP_RV2GV) {
12019 enter = (LISTOP *) enter->op_next;
12025 if (enter->op_type != OP_ENTERITER)
12028 iter = enter->op_next;
12029 if (!iter || iter->op_type != OP_ITER)
12032 expushmark = enter->op_first;
12033 if (!expushmark || expushmark->op_type != OP_NULL
12034 || expushmark->op_targ != OP_PUSHMARK)
12037 exlist = (LISTOP *) expushmark->op_sibling;
12038 if (!exlist || exlist->op_type != OP_NULL
12039 || exlist->op_targ != OP_LIST)
12042 if (exlist->op_last != o) {
12043 /* Mmm. Was expecting to point back to this op. */
12046 theirmark = exlist->op_first;
12047 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
12050 if (theirmark->op_sibling != o) {
12051 /* There's something between the mark and the reverse, eg
12052 for (1, reverse (...))
12057 ourmark = ((LISTOP *)o)->op_first;
12058 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
12061 ourlast = ((LISTOP *)o)->op_last;
12062 if (!ourlast || ourlast->op_next != o)
12065 rv2av = ourmark->op_sibling;
12066 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
12067 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
12068 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
12069 /* We're just reversing a single array. */
12070 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
12071 enter->op_flags |= OPf_STACKED;
12074 /* We don't have control over who points to theirmark, so sacrifice
12076 theirmark->op_next = ourmark->op_next;
12077 theirmark->op_flags = ourmark->op_flags;
12078 ourlast->op_next = gvop ? gvop : (OP *) enter;
12081 enter->op_private |= OPpITER_REVERSED;
12082 iter->op_private |= OPpITER_REVERSED;
12089 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
12090 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
12095 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)) {
12097 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
12099 sv = newRV((SV *)PL_compcv);
12103 o->op_type = OP_CONST;
12104 o->op_ppaddr = PL_ppaddr[OP_CONST];
12105 o->op_flags |= OPf_SPECIAL;
12106 cSVOPo->op_sv = sv;
12111 if (OP_GIMME(o,0) == G_VOID) {
12112 OP *right = cBINOP->op_first;
12131 OP *left = right->op_sibling;
12132 if (left->op_type == OP_SUBSTR
12133 && (left->op_private & 7) < 4) {
12135 cBINOP->op_first = left;
12136 right->op_sibling =
12137 cBINOPx(left)->op_first->op_sibling;
12138 cBINOPx(left)->op_first->op_sibling = right;
12139 left->op_private |= OPpSUBSTR_REPL_FIRST;
12141 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
12148 Perl_cpeep_t cpeep =
12149 XopENTRYCUSTOM(o, xop_peep);
12151 cpeep(aTHX_ o, oldop);
12156 /* did we just null the current op? If so, re-process it to handle
12157 * eliding "empty" ops from the chain */
12158 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
12171 Perl_peep(pTHX_ OP *o)
12177 =head1 Custom Operators
12179 =for apidoc Ao||custom_op_xop
12180 Return the XOP structure for a given custom op. This macro should be
12181 considered internal to OP_NAME and the other access macros: use them instead.
12182 This macro does call a function. Prior
12183 to 5.19.6, this was implemented as a
12190 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
12196 static const XOP xop_null = { 0, 0, 0, 0, 0 };
12198 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
12199 assert(o->op_type == OP_CUSTOM);
12201 /* This is wrong. It assumes a function pointer can be cast to IV,
12202 * which isn't guaranteed, but this is what the old custom OP code
12203 * did. In principle it should be safer to Copy the bytes of the
12204 * pointer into a PV: since the new interface is hidden behind
12205 * functions, this can be changed later if necessary. */
12206 /* Change custom_op_xop if this ever happens */
12207 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
12210 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
12212 /* assume noone will have just registered a desc */
12213 if (!he && PL_custom_op_names &&
12214 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
12219 /* XXX does all this need to be shared mem? */
12220 Newxz(xop, 1, XOP);
12221 pv = SvPV(HeVAL(he), l);
12222 XopENTRY_set(xop, xop_name, savepvn(pv, l));
12223 if (PL_custom_op_descs &&
12224 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
12226 pv = SvPV(HeVAL(he), l);
12227 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
12229 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
12233 xop = (XOP *)&xop_null;
12235 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
12239 if(field == XOPe_xop_ptr) {
12242 const U32 flags = XopFLAGS(xop);
12243 if(flags & field) {
12245 case XOPe_xop_name:
12246 any.xop_name = xop->xop_name;
12248 case XOPe_xop_desc:
12249 any.xop_desc = xop->xop_desc;
12251 case XOPe_xop_class:
12252 any.xop_class = xop->xop_class;
12254 case XOPe_xop_peep:
12255 any.xop_peep = xop->xop_peep;
12263 case XOPe_xop_name:
12264 any.xop_name = XOPd_xop_name;
12266 case XOPe_xop_desc:
12267 any.xop_desc = XOPd_xop_desc;
12269 case XOPe_xop_class:
12270 any.xop_class = XOPd_xop_class;
12272 case XOPe_xop_peep:
12273 any.xop_peep = XOPd_xop_peep;
12286 =for apidoc Ao||custom_op_register
12287 Register a custom op. See L<perlguts/"Custom Operators">.
12293 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
12297 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
12299 /* see the comment in custom_op_xop */
12300 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
12302 if (!PL_custom_ops)
12303 PL_custom_ops = newHV();
12305 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
12306 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
12310 =head1 Functions in file op.c
12312 =for apidoc core_prototype
12313 This function assigns the prototype of the named core function to C<sv>, or
12314 to a new mortal SV if C<sv> is NULL. It returns the modified C<sv>, or
12315 NULL if the core function has no prototype. C<code> is a code as returned
12316 by C<keyword()>. It must not be equal to 0.
12322 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
12325 int i = 0, n = 0, seen_question = 0, defgv = 0;
12327 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
12328 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
12329 bool nullret = FALSE;
12331 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
12335 if (!sv) sv = sv_newmortal();
12337 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
12339 switch (code < 0 ? -code : code) {
12340 case KEY_and : case KEY_chop: case KEY_chomp:
12341 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
12342 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
12343 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
12344 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
12345 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
12346 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
12347 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
12348 case KEY_x : case KEY_xor :
12349 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
12350 case KEY_glob: retsetpvs("_;", OP_GLOB);
12351 case KEY_keys: retsetpvs("+", OP_KEYS);
12352 case KEY_values: retsetpvs("+", OP_VALUES);
12353 case KEY_each: retsetpvs("+", OP_EACH);
12354 case KEY_push: retsetpvs("+@", OP_PUSH);
12355 case KEY_unshift: retsetpvs("+@", OP_UNSHIFT);
12356 case KEY_pop: retsetpvs(";+", OP_POP);
12357 case KEY_shift: retsetpvs(";+", OP_SHIFT);
12358 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
12360 retsetpvs("+;$$@", OP_SPLICE);
12361 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
12363 case KEY_evalbytes:
12364 name = "entereval"; break;
12372 while (i < MAXO) { /* The slow way. */
12373 if (strEQ(name, PL_op_name[i])
12374 || strEQ(name, PL_op_desc[i]))
12376 if (nullret) { assert(opnum); *opnum = i; return NULL; }
12383 defgv = PL_opargs[i] & OA_DEFGV;
12384 oa = PL_opargs[i] >> OASHIFT;
12386 if (oa & OA_OPTIONAL && !seen_question && (
12387 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
12392 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
12393 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
12394 /* But globs are already references (kinda) */
12395 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
12399 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
12400 && !scalar_mod_type(NULL, i)) {
12405 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
12409 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
12410 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
12411 str[n-1] = '_'; defgv = 0;
12415 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
12417 sv_setpvn(sv, str, n - 1);
12418 if (opnum) *opnum = i;
12423 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
12426 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv);
12429 PERL_ARGS_ASSERT_CORESUB_OP;
12433 return op_append_elem(OP_LINESEQ,
12436 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
12440 case OP_SELECT: /* which represents OP_SSELECT as well */
12445 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
12446 newSVOP(OP_CONST, 0, newSVuv(1))
12448 coresub_op(newSVuv((UV)OP_SSELECT), 0,
12450 coresub_op(coreargssv, 0, OP_SELECT)
12454 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
12456 return op_append_elem(
12459 opnum == OP_WANTARRAY || opnum == OP_RUNCV
12460 ? OPpOFFBYONE << 8 : 0)
12462 case OA_BASEOP_OR_UNOP:
12463 if (opnum == OP_ENTEREVAL) {
12464 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
12465 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
12467 else o = newUNOP(opnum,0,argop);
12468 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
12471 if (is_handle_constructor(o, 1))
12472 argop->op_private |= OPpCOREARGS_DEREF1;
12473 if (scalar_mod_type(NULL, opnum))
12474 argop->op_private |= OPpCOREARGS_SCALARMOD;
12478 o = convert(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
12479 if (is_handle_constructor(o, 2))
12480 argop->op_private |= OPpCOREARGS_DEREF2;
12481 if (opnum == OP_SUBSTR) {
12482 o->op_private |= OPpMAYBE_LVSUB;
12491 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
12492 SV * const *new_const_svp)
12494 const char *hvname;
12495 bool is_const = !!CvCONST(old_cv);
12496 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
12498 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
12500 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
12502 /* They are 2 constant subroutines generated from
12503 the same constant. This probably means that
12504 they are really the "same" proxy subroutine
12505 instantiated in 2 places. Most likely this is
12506 when a constant is exported twice. Don't warn.
12509 (ckWARN(WARN_REDEFINE)
12511 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
12512 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
12513 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
12514 strEQ(hvname, "autouse"))
12518 && ckWARN_d(WARN_REDEFINE)
12519 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
12522 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
12524 ? "Constant subroutine %"SVf" redefined"
12525 : "Subroutine %"SVf" redefined",
12530 =head1 Hook manipulation
12532 These functions provide convenient and thread-safe means of manipulating
12539 =for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p
12541 Puts a C function into the chain of check functions for a specified op
12542 type. This is the preferred way to manipulate the L</PL_check> array.
12543 I<opcode> specifies which type of op is to be affected. I<new_checker>
12544 is a pointer to the C function that is to be added to that opcode's
12545 check chain, and I<old_checker_p> points to the storage location where a
12546 pointer to the next function in the chain will be stored. The value of
12547 I<new_pointer> is written into the L</PL_check> array, while the value
12548 previously stored there is written to I<*old_checker_p>.
12550 The function should be defined like this:
12552 static OP *new_checker(pTHX_ OP *op) { ... }
12554 It is intended to be called in this manner:
12556 new_checker(aTHX_ op)
12558 I<old_checker_p> should be defined like this:
12560 static Perl_check_t old_checker_p;
12562 L</PL_check> is global to an entire process, and a module wishing to
12563 hook op checking may find itself invoked more than once per process,
12564 typically in different threads. To handle that situation, this function
12565 is idempotent. The location I<*old_checker_p> must initially (once
12566 per process) contain a null pointer. A C variable of static duration
12567 (declared at file scope, typically also marked C<static> to give
12568 it internal linkage) will be implicitly initialised appropriately,
12569 if it does not have an explicit initialiser. This function will only
12570 actually modify the check chain if it finds I<*old_checker_p> to be null.
12571 This function is also thread safe on the small scale. It uses appropriate
12572 locking to avoid race conditions in accessing L</PL_check>.
12574 When this function is called, the function referenced by I<new_checker>
12575 must be ready to be called, except for I<*old_checker_p> being unfilled.
12576 In a threading situation, I<new_checker> may be called immediately,
12577 even before this function has returned. I<*old_checker_p> will always
12578 be appropriately set before I<new_checker> is called. If I<new_checker>
12579 decides not to do anything special with an op that it is given (which
12580 is the usual case for most uses of op check hooking), it must chain the
12581 check function referenced by I<*old_checker_p>.
12583 If you want to influence compilation of calls to a specific subroutine,
12584 then use L</cv_set_call_checker> rather than hooking checking of all
12591 Perl_wrap_op_checker(pTHX_ Optype opcode,
12592 Perl_check_t new_checker, Perl_check_t *old_checker_p)
12596 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
12597 if (*old_checker_p) return;
12598 OP_CHECK_MUTEX_LOCK;
12599 if (!*old_checker_p) {
12600 *old_checker_p = PL_check[opcode];
12601 PL_check[opcode] = new_checker;
12603 OP_CHECK_MUTEX_UNLOCK;
12608 /* Efficient sub that returns a constant scalar value. */
12610 const_sv_xsub(pTHX_ CV* cv)
12614 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
12615 PERL_UNUSED_ARG(items);
12625 const_av_xsub(pTHX_ CV* cv)
12629 AV * const av = MUTABLE_AV(XSANY.any_ptr);
12637 if (SvRMAGICAL(av))
12638 Perl_croak(aTHX_ "Magical list constants are not supported");
12639 if (GIMME_V != G_ARRAY) {
12641 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
12644 EXTEND(SP, AvFILLp(av)+1);
12645 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
12646 XSRETURN(AvFILLp(av)+1);
12651 * c-indentation-style: bsd
12652 * c-basic-offset: 4
12653 * indent-tabs-mode: nil
12656 * ex: set ts=8 sts=4 sw=4 et: