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.
7251 * Can be called in 3 ways:
7254 * look for a single OP_CONST with attached value: return the value
7256 * cv && CvCLONE(cv) && !CvCONST(cv)
7258 * examine the clone prototype, and if contains only a single
7259 * OP_CONST referencing a pad const, or a single PADSV referencing
7260 * an outer lexical, return a non-zero value to indicate the CV is
7261 * a candidate for "constizing" at clone time
7265 * We have just cloned an anon prototype that was marked as a const
7266 * candidate. Try to grab the current value, and in the case of
7267 * PADSV, ignore it if it has multiple references. In this case we
7268 * return a newly created *copy* of the value.
7272 Perl_op_const_sv(pTHX_ const OP *o, CV *cv)
7283 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
7284 o = cLISTOPo->op_first->op_sibling;
7286 for (; o; o = o->op_next) {
7287 const OPCODE type = o->op_type;
7289 if (sv && o->op_next == o)
7291 if (o->op_next != o) {
7292 if (type == OP_NEXTSTATE
7293 || (type == OP_NULL && !(o->op_flags & OPf_KIDS))
7294 || type == OP_PUSHMARK)
7296 if (type == OP_DBSTATE)
7299 if (type == OP_LEAVESUB || type == OP_RETURN)
7303 if (type == OP_CONST && cSVOPo->op_sv)
7305 else if (cv && type == OP_CONST) {
7306 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
7310 else if (cv && type == OP_PADSV) {
7311 if (CvCONST(cv)) { /* newly cloned anon */
7312 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
7313 /* the candidate should have 1 ref from this pad and 1 ref
7314 * from the parent */
7315 if (!sv || SvREFCNT(sv) != 2)
7322 if (PAD_COMPNAME_FLAGS(o->op_targ) & SVf_FAKE)
7323 sv = &PL_sv_undef; /* an arbitrary non-null value */
7334 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
7335 PADNAME * const name, SV ** const const_svp)
7342 || block->op_type == OP_NULL
7345 if (CvFLAGS(PL_compcv)) {
7346 /* might have had built-in attrs applied */
7347 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
7348 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
7349 && ckWARN(WARN_MISC))
7351 /* protect against fatal warnings leaking compcv */
7352 SAVEFREESV(PL_compcv);
7353 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
7354 SvREFCNT_inc_simple_void_NN(PL_compcv);
7357 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
7358 & ~(CVf_LVALUE * pureperl));
7363 /* redundant check for speed: */
7364 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
7365 const line_t oldline = CopLINE(PL_curcop);
7368 : sv_2mortal(newSVpvn_utf8(
7369 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name)
7371 if (PL_parser && PL_parser->copline != NOLINE)
7372 /* This ensures that warnings are reported at the first
7373 line of a redefinition, not the last. */
7374 CopLINE_set(PL_curcop, PL_parser->copline);
7375 /* protect against fatal warnings leaking compcv */
7376 SAVEFREESV(PL_compcv);
7377 report_redefined_cv(namesv, cv, const_svp);
7378 SvREFCNT_inc_simple_void_NN(PL_compcv);
7379 CopLINE_set(PL_curcop, oldline);
7382 if (!PL_minus_c) /* keep old one around for madskills */
7385 /* (PL_madskills unset in used file.) */
7392 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
7398 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
7401 CV *compcv = PL_compcv;
7404 PADOFFSET pax = o->op_targ;
7405 CV *outcv = CvOUTSIDE(PL_compcv);
7408 bool reusable = FALSE;
7410 PERL_ARGS_ASSERT_NEWMYSUB;
7412 /* Find the pad slot for storing the new sub.
7413 We cannot use PL_comppad, as it is the pad owned by the new sub. We
7414 need to look in CvOUTSIDE and find the pad belonging to the enclos-
7415 ing sub. And then we need to dig deeper if this is a lexical from
7417 my sub foo; sub { sub foo { } }
7420 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
7421 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
7422 pax = PARENT_PAD_INDEX(name);
7423 outcv = CvOUTSIDE(outcv);
7428 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
7429 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
7430 spot = (CV **)svspot;
7432 if (!(PL_parser && PL_parser->error_count))
7433 move_proto_attr(&proto, &attrs, (GV *)name);
7436 assert(proto->op_type == OP_CONST);
7437 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
7438 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
7443 if (!PL_madskills) {
7450 if (PL_parser && PL_parser->error_count) {
7452 SvREFCNT_dec(PL_compcv);
7457 if (CvDEPTH(outcv) && CvCLONE(compcv)) {
7459 svspot = (SV **)(spot = &clonee);
7461 else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
7465 SvUPGRADE(name, SVt_PVMG);
7466 mg = mg_find(name, PERL_MAGIC_proto);
7467 assert (SvTYPE(*spot) == SVt_PVCV);
7469 hek = CvNAME_HEK(*spot);
7471 CvNAME_HEK_set(*spot, hek =
7474 PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1), 0
7480 cv = (CV *)mg->mg_obj;
7483 sv_magic(name, &PL_sv_undef, PERL_MAGIC_proto, NULL, 0);
7484 mg = mg_find(name, PERL_MAGIC_proto);
7486 spot = (CV **)(svspot = &mg->mg_obj);
7489 if (!block || !ps || *ps || attrs
7490 || (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS)
7492 || block->op_type == OP_NULL
7497 const_sv = op_const_sv(block, NULL);
7500 const bool exists = CvROOT(cv) || CvXSUB(cv);
7502 /* if the subroutine doesn't exist and wasn't pre-declared
7503 * with a prototype, assume it will be AUTOLOADed,
7504 * skipping the prototype check
7506 if (exists || SvPOK(cv))
7507 cv_ckproto_len_flags(cv, (GV *)name, ps, ps_len, ps_utf8);
7508 /* already defined? */
7510 if (S_already_defined(aTHX_ cv,block,NULL,name,&const_sv))
7513 if (attrs) goto attrs;
7514 /* just a "sub foo;" when &foo is already defined */
7519 else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
7525 SvREFCNT_inc_simple_void_NN(const_sv);
7526 SvFLAGS(const_sv) = (SvFLAGS(const_sv) & ~SVs_PADMY) | SVs_PADTMP;
7528 assert(!CvROOT(cv) && !CvCONST(cv));
7532 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
7533 CvFILE_set_from_cop(cv, PL_curcop);
7534 CvSTASH_set(cv, PL_curstash);
7537 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */
7538 CvXSUBANY(cv).any_ptr = const_sv;
7539 CvXSUB(cv) = const_sv_xsub;
7545 SvREFCNT_dec(compcv);
7549 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
7550 determine whether this sub definition is in the same scope as its
7551 declaration. If this sub definition is inside an inner named pack-
7552 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
7553 the package sub. So check PadnameOUTER(name) too.
7555 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) {
7556 assert(!CvWEAKOUTSIDE(compcv));
7557 SvREFCNT_dec(CvOUTSIDE(compcv));
7558 CvWEAKOUTSIDE_on(compcv);
7560 /* XXX else do we have a circular reference? */
7561 if (cv) { /* must reuse cv in case stub is referenced elsewhere */
7562 /* transfer PL_compcv to cv */
7565 && block->op_type != OP_NULL
7568 cv_flags_t preserved_flags =
7569 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
7570 PADLIST *const temp_padl = CvPADLIST(cv);
7571 CV *const temp_cv = CvOUTSIDE(cv);
7572 const cv_flags_t other_flags =
7573 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
7574 OP * const cvstart = CvSTART(cv);
7578 CvFLAGS(compcv) | preserved_flags;
7579 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
7580 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
7581 CvPADLIST(cv) = CvPADLIST(compcv);
7582 CvOUTSIDE(compcv) = temp_cv;
7583 CvPADLIST(compcv) = temp_padl;
7584 CvSTART(cv) = CvSTART(compcv);
7585 CvSTART(compcv) = cvstart;
7586 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
7587 CvFLAGS(compcv) |= other_flags;
7589 if (CvFILE(cv) && CvDYNFILE(cv)) {
7590 Safefree(CvFILE(cv));
7593 /* inner references to compcv must be fixed up ... */
7594 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
7595 if (PERLDB_INTER)/* Advice debugger on the new sub. */
7596 ++PL_sub_generation;
7599 /* Might have had built-in attributes applied -- propagate them. */
7600 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
7602 /* ... before we throw it away */
7603 SvREFCNT_dec(compcv);
7604 PL_compcv = compcv = cv;
7611 if (!CvNAME_HEK(cv)) {
7614 ? share_hek_hek(hek)
7615 : share_hek(PadnamePV(name)+1,
7616 PadnameLEN(name)-1 * (PadnameUTF8(name) ? -1 : 1),
7620 if (const_sv) goto clone;
7622 CvFILE_set_from_cop(cv, PL_curcop);
7623 CvSTASH_set(cv, PL_curstash);
7626 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
7627 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
7634 /* If we assign an optree to a PVCV, then we've defined a subroutine that
7635 the debugger could be able to set a breakpoint in, so signal to
7636 pp_entereval that it should not throw away any saved lines at scope
7639 PL_breakable_sub_gen++;
7640 /* This makes sub {}; work as expected. */
7641 if (block->op_type == OP_STUB) {
7642 OP* const newblock = newSTATEOP(0, NULL, 0);
7644 op_getmad(block,newblock,'B');
7650 CvROOT(cv) = CvLVALUE(cv)
7651 ? newUNOP(OP_LEAVESUBLV, 0,
7652 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
7653 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
7654 CvROOT(cv)->op_private |= OPpREFCOUNTED;
7655 OpREFCNT_set(CvROOT(cv), 1);
7656 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
7657 itself has a refcount. */
7659 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
7660 CvSTART(cv) = LINKLIST(CvROOT(cv));
7661 CvROOT(cv)->op_next = 0;
7662 CALL_PEEP(CvSTART(cv));
7663 finalize_optree(CvROOT(cv));
7664 S_prune_chain_head(aTHX_ &CvSTART(cv));
7666 /* now that optimizer has done its work, adjust pad values */
7668 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
7671 assert(!CvCONST(cv));
7672 if (ps && !*ps && op_const_sv(block, cv))
7678 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
7679 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
7683 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
7684 SV * const tmpstr = sv_newmortal();
7685 GV * const db_postponed = gv_fetchpvs("DB::postponed",
7686 GV_ADDMULTI, SVt_PVHV);
7688 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
7691 (long)CopLINE(PL_curcop));
7692 if (HvNAME_HEK(PL_curstash)) {
7693 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
7694 sv_catpvs(tmpstr, "::");
7696 else sv_setpvs(tmpstr, "__ANON__::");
7697 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
7698 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
7699 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
7700 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
7701 hv = GvHVn(db_postponed);
7702 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
7703 CV * const pcv = GvCV(db_postponed);
7709 call_sv(MUTABLE_SV(pcv), G_DISCARD);
7717 assert(CvDEPTH(outcv));
7719 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
7720 if (reusable) cv_clone_into(clonee, *spot);
7721 else *spot = cv_clone(clonee);
7722 SvREFCNT_dec_NN(clonee);
7726 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
7727 PADOFFSET depth = CvDEPTH(outcv);
7730 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
7732 *svspot = SvREFCNT_inc_simple_NN(cv);
7733 SvREFCNT_dec(oldcv);
7739 PL_parser->copline = NOLINE;
7747 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
7748 OP *block, bool o_is_gv)
7753 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
7757 const bool ec = PL_parser && PL_parser->error_count;
7758 /* If the subroutine has no body, no attributes, and no builtin attributes
7759 then it's just a sub declaration, and we may be able to get away with
7760 storing with a placeholder scalar in the symbol table, rather than a
7761 full GV and CV. If anything is present then it will take a full CV to
7763 const I32 gv_fetch_flags
7764 = ec ? GV_NOADD_NOINIT :
7765 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
7767 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
7769 const char * const name =
7770 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
7772 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
7773 #ifdef PERL_DEBUG_READONLY_OPS
7774 OPSLAB *slab = NULL;
7782 gv = gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV);
7784 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
7785 SV * const sv = sv_newmortal();
7786 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
7787 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
7788 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
7789 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
7791 } else if (PL_curstash) {
7792 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
7795 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
7800 move_proto_attr(&proto, &attrs, gv);
7803 assert(proto->op_type == OP_CONST);
7804 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
7805 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
7810 if (!PL_madskills) {
7821 if (name) SvREFCNT_dec(PL_compcv);
7822 else cv = PL_compcv;
7824 if (name && block) {
7825 const char *s = strrchr(name, ':');
7827 if (strEQ(s, "BEGIN")) {
7828 if (PL_in_eval & EVAL_KEEPERR)
7829 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
7831 SV * const errsv = ERRSV;
7832 /* force display of errors found but not reported */
7833 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
7834 Perl_croak_nocontext("%"SVf, SVfARG(errsv));
7841 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
7842 maximum a prototype before. */
7843 if (SvTYPE(gv) > SVt_NULL) {
7844 cv_ckproto_len_flags((const CV *)gv,
7845 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
7849 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
7850 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(gv));
7853 sv_setiv(MUTABLE_SV(gv), -1);
7855 SvREFCNT_dec(PL_compcv);
7856 cv = PL_compcv = NULL;
7860 cv = (!name || GvCVGEN(gv)) ? NULL : GvCV(gv);
7862 if (!block || !ps || *ps || attrs
7863 || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
7865 || block->op_type == OP_NULL
7870 const_sv = op_const_sv(block, NULL);
7873 const bool exists = CvROOT(cv) || CvXSUB(cv);
7875 /* if the subroutine doesn't exist and wasn't pre-declared
7876 * with a prototype, assume it will be AUTOLOADed,
7877 * skipping the prototype check
7879 if (exists || SvPOK(cv))
7880 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
7881 /* already defined (or promised)? */
7882 if (exists || GvASSUMECV(gv)) {
7883 if (S_already_defined(aTHX_ cv, block, o, NULL, &const_sv))
7886 if (attrs) goto attrs;
7887 /* just a "sub foo;" when &foo is already defined */
7888 SAVEFREESV(PL_compcv);
7894 SvREFCNT_inc_simple_void_NN(const_sv);
7895 SvFLAGS(const_sv) = (SvFLAGS(const_sv) & ~SVs_PADMY) | SVs_PADTMP;
7897 assert(!CvROOT(cv) && !CvCONST(cv));
7899 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */
7900 CvXSUBANY(cv).any_ptr = const_sv;
7901 CvXSUB(cv) = const_sv_xsub;
7907 cv = newCONSTSUB_flags(
7908 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
7915 SvREFCNT_dec(PL_compcv);
7919 if (cv) { /* must reuse cv if autoloaded */
7920 /* transfer PL_compcv to cv */
7923 && block->op_type != OP_NULL
7926 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
7927 PADLIST *const temp_av = CvPADLIST(cv);
7928 CV *const temp_cv = CvOUTSIDE(cv);
7929 const cv_flags_t other_flags =
7930 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
7931 OP * const cvstart = CvSTART(cv);
7934 assert(!CvCVGV_RC(cv));
7935 assert(CvGV(cv) == gv);
7938 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs;
7939 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
7940 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
7941 CvPADLIST(cv) = CvPADLIST(PL_compcv);
7942 CvOUTSIDE(PL_compcv) = temp_cv;
7943 CvPADLIST(PL_compcv) = temp_av;
7944 CvSTART(cv) = CvSTART(PL_compcv);
7945 CvSTART(PL_compcv) = cvstart;
7946 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
7947 CvFLAGS(PL_compcv) |= other_flags;
7949 if (CvFILE(cv) && CvDYNFILE(cv)) {
7950 Safefree(CvFILE(cv));
7952 CvFILE_set_from_cop(cv, PL_curcop);
7953 CvSTASH_set(cv, PL_curstash);
7955 /* inner references to PL_compcv must be fixed up ... */
7956 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
7957 if (PERLDB_INTER)/* Advice debugger on the new sub. */
7958 ++PL_sub_generation;
7961 /* Might have had built-in attributes applied -- propagate them. */
7962 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
7964 /* ... before we throw it away */
7965 SvREFCNT_dec(PL_compcv);
7973 if (HvENAME_HEK(GvSTASH(gv)))
7974 /* sub Foo::bar { (shift)+1 } */
7975 gv_method_changed(gv);
7980 CvFILE_set_from_cop(cv, PL_curcop);
7981 CvSTASH_set(cv, PL_curstash);
7985 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
7986 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
7993 /* If we assign an optree to a PVCV, then we've defined a subroutine that
7994 the debugger could be able to set a breakpoint in, so signal to
7995 pp_entereval that it should not throw away any saved lines at scope
7998 PL_breakable_sub_gen++;
7999 /* This makes sub {}; work as expected. */
8000 if (block->op_type == OP_STUB) {
8001 OP* const newblock = newSTATEOP(0, NULL, 0);
8003 op_getmad(block,newblock,'B');
8009 CvROOT(cv) = CvLVALUE(cv)
8010 ? newUNOP(OP_LEAVESUBLV, 0,
8011 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
8012 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
8013 CvROOT(cv)->op_private |= OPpREFCOUNTED;
8014 OpREFCNT_set(CvROOT(cv), 1);
8015 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
8016 itself has a refcount. */
8018 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
8019 #ifdef PERL_DEBUG_READONLY_OPS
8020 slab = (OPSLAB *)CvSTART(cv);
8022 CvSTART(cv) = LINKLIST(CvROOT(cv));
8023 CvROOT(cv)->op_next = 0;
8024 CALL_PEEP(CvSTART(cv));
8025 finalize_optree(CvROOT(cv));
8026 S_prune_chain_head(aTHX_ &CvSTART(cv));
8028 /* now that optimizer has done its work, adjust pad values */
8030 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
8033 assert(!CvCONST(cv));
8034 if (ps && !*ps && op_const_sv(block, cv))
8040 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
8041 HV *stash = name && GvSTASH(CvGV(cv)) ? GvSTASH(CvGV(cv)) : PL_curstash;
8042 if (!name) SAVEFREESV(cv);
8043 apply_attrs(stash, MUTABLE_SV(cv), attrs);
8044 if (!name) SvREFCNT_inc_simple_void_NN(cv);
8047 if (block && has_name) {
8048 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
8049 SV * const tmpstr = sv_newmortal();
8050 GV * const db_postponed = gv_fetchpvs("DB::postponed",
8051 GV_ADDMULTI, SVt_PVHV);
8053 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
8056 (long)CopLINE(PL_curcop));
8057 gv_efullname3(tmpstr, gv, NULL);
8058 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
8059 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
8060 hv = GvHVn(db_postponed);
8061 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
8062 CV * const pcv = GvCV(db_postponed);
8068 call_sv(MUTABLE_SV(pcv), G_DISCARD);
8073 if (name && ! (PL_parser && PL_parser->error_count))
8074 process_special_blocks(floor, name, gv, cv);
8079 PL_parser->copline = NOLINE;
8081 #ifdef PERL_DEBUG_READONLY_OPS
8082 /* Watch out for BEGIN blocks */
8083 if (slab && gv && isGV(gv) && GvCV(gv)) Slab_to_ro(slab);
8089 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
8093 const char *const colon = strrchr(fullname,':');
8094 const char *const name = colon ? colon + 1 : fullname;
8096 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
8099 if (strEQ(name, "BEGIN")) {
8100 const I32 oldscope = PL_scopestack_ix;
8102 if (floor) LEAVE_SCOPE(floor);
8104 PUSHSTACKi(PERLSI_REQUIRE);
8105 SAVECOPFILE(&PL_compiling);
8106 SAVECOPLINE(&PL_compiling);
8107 SAVEVPTR(PL_curcop);
8109 DEBUG_x( dump_sub(gv) );
8110 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
8111 GvCV_set(gv,0); /* cv has been hijacked */
8112 call_list(oldscope, PL_beginav);
8121 if strEQ(name, "END") {
8122 DEBUG_x( dump_sub(gv) );
8123 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
8126 } else if (*name == 'U') {
8127 if (strEQ(name, "UNITCHECK")) {
8128 /* It's never too late to run a unitcheck block */
8129 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
8133 } else if (*name == 'C') {
8134 if (strEQ(name, "CHECK")) {
8136 /* diag_listed_as: Too late to run %s block */
8137 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8138 "Too late to run CHECK block");
8139 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
8143 } else if (*name == 'I') {
8144 if (strEQ(name, "INIT")) {
8146 /* diag_listed_as: Too late to run %s block */
8147 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
8148 "Too late to run INIT block");
8149 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
8155 DEBUG_x( dump_sub(gv) );
8156 GvCV_set(gv,0); /* cv has been hijacked */
8161 =for apidoc newCONSTSUB
8163 See L</newCONSTSUB_flags>.
8169 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
8171 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
8175 =for apidoc newCONSTSUB_flags
8177 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
8178 eligible for inlining at compile-time.
8180 Currently, the only useful value for C<flags> is SVf_UTF8.
8182 The newly created subroutine takes ownership of a reference to the passed in
8185 Passing NULL for SV creates a constant sub equivalent to C<sub BAR () {}>,
8186 which won't be called if used as a destructor, but will suppress the overhead
8187 of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at
8194 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
8199 const char *const file = CopFILE(PL_curcop);
8203 if (IN_PERL_RUNTIME) {
8204 /* at runtime, it's not safe to manipulate PL_curcop: it may be
8205 * an op shared between threads. Use a non-shared COP for our
8207 SAVEVPTR(PL_curcop);
8208 SAVECOMPILEWARNINGS();
8209 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
8210 PL_curcop = &PL_compiling;
8212 SAVECOPLINE(PL_curcop);
8213 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
8216 PL_hints &= ~HINT_BLOCK_SCOPE;
8219 SAVEGENERICSV(PL_curstash);
8220 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
8223 /* Protect sv against leakage caused by fatal warnings. */
8224 if (sv) SAVEFREESV(sv);
8226 /* file becomes the CvFILE. For an XS, it's usually static storage,
8227 and so doesn't get free()d. (It's expected to be from the C pre-
8228 processor __FILE__ directive). But we need a dynamically allocated one,
8229 and we need it to get freed. */
8230 cv = newXS_len_flags(name, len,
8231 sv && SvTYPE(sv) == SVt_PVAV
8234 file ? file : "", "",
8235 &sv, XS_DYNAMIC_FILENAME | flags);
8236 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
8245 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
8246 const char *const filename, const char *const proto,
8249 PERL_ARGS_ASSERT_NEWXS_FLAGS;
8250 return newXS_len_flags(
8251 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
8256 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
8257 XSUBADDR_t subaddr, const char *const filename,
8258 const char *const proto, SV **const_svp,
8262 bool interleave = FALSE;
8264 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
8267 GV * const gv = gv_fetchpvn(
8268 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
8269 name ? len : PL_curstash ? sizeof("__ANON__") - 1:
8270 sizeof("__ANON__::__ANON__") - 1,
8271 GV_ADDMULTI | flags, SVt_PVCV);
8274 Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
8276 if ((cv = (name ? GvCV(gv) : NULL))) {
8278 /* just a cached method */
8282 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
8283 /* already defined (or promised) */
8284 /* Redundant check that allows us to avoid creating an SV
8285 most of the time: */
8286 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
8287 report_redefined_cv(newSVpvn_flags(
8288 name,len,(flags&SVf_UTF8)|SVs_TEMP
8299 if (cv) /* must reuse cv if autoloaded */
8302 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8306 if (HvENAME_HEK(GvSTASH(gv)))
8307 gv_method_changed(gv); /* newXS */
8313 (void)gv_fetchfile(filename);
8314 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
8315 an external constant string */
8316 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
8318 CvXSUB(cv) = subaddr;
8321 process_special_blocks(0, name, gv, cv);
8324 if (flags & XS_DYNAMIC_FILENAME) {
8325 CvFILE(cv) = savepv(filename);
8328 sv_setpv(MUTABLE_SV(cv), proto);
8329 if (interleave) LEAVE;
8334 Perl_newSTUB(pTHX_ GV *gv, bool fake)
8336 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
8338 PERL_ARGS_ASSERT_NEWSTUB;
8342 if (!fake && HvENAME_HEK(GvSTASH(gv)))
8343 gv_method_changed(gv);
8345 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
8350 CvFILE_set_from_cop(cv, PL_curcop);
8351 CvSTASH_set(cv, PL_curstash);
8357 =for apidoc U||newXS
8359 Used by C<xsubpp> to hook up XSUBs as Perl subs. I<filename> needs to be
8360 static storage, as it is used directly as CvFILE(), without a copy being made.
8366 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
8368 PERL_ARGS_ASSERT_NEWXS;
8369 return newXS_len_flags(
8370 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
8379 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
8384 OP* pegop = newOP(OP_NULL, 0);
8389 if (PL_parser && PL_parser->error_count) {
8395 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
8396 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
8399 if ((cv = GvFORM(gv))) {
8400 if (ckWARN(WARN_REDEFINE)) {
8401 const line_t oldline = CopLINE(PL_curcop);
8402 if (PL_parser && PL_parser->copline != NOLINE)
8403 CopLINE_set(PL_curcop, PL_parser->copline);
8405 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8406 "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv));
8408 /* diag_listed_as: Format %s redefined */
8409 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
8410 "Format STDOUT redefined");
8412 CopLINE_set(PL_curcop, oldline);
8417 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
8419 CvFILE_set_from_cop(cv, PL_curcop);
8422 pad_tidy(padtidy_FORMAT);
8423 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
8424 CvROOT(cv)->op_private |= OPpREFCOUNTED;
8425 OpREFCNT_set(CvROOT(cv), 1);
8426 CvSTART(cv) = LINKLIST(CvROOT(cv));
8427 CvROOT(cv)->op_next = 0;
8428 CALL_PEEP(CvSTART(cv));
8429 finalize_optree(CvROOT(cv));
8430 S_prune_chain_head(aTHX_ &CvSTART(cv));
8435 op_getmad(o,pegop,'n');
8436 op_getmad_weak(block, pegop, 'b');
8441 PL_parser->copline = NOLINE;
8449 Perl_newANONLIST(pTHX_ OP *o)
8451 return convert(OP_ANONLIST, OPf_SPECIAL, o);
8455 Perl_newANONHASH(pTHX_ OP *o)
8457 return convert(OP_ANONHASH, OPf_SPECIAL, o);
8461 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
8463 return newANONATTRSUB(floor, proto, NULL, block);
8467 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
8469 return newUNOP(OP_REFGEN, 0,
8470 newSVOP(OP_ANONCODE, 0,
8471 MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block))));
8475 Perl_oopsAV(pTHX_ OP *o)
8479 PERL_ARGS_ASSERT_OOPSAV;
8481 switch (o->op_type) {
8484 o->op_type = OP_PADAV;
8485 o->op_ppaddr = PL_ppaddr[OP_PADAV];
8486 return ref(o, OP_RV2AV);
8490 o->op_type = OP_RV2AV;
8491 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
8496 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
8503 Perl_oopsHV(pTHX_ OP *o)
8507 PERL_ARGS_ASSERT_OOPSHV;
8509 switch (o->op_type) {
8512 o->op_type = OP_PADHV;
8513 o->op_ppaddr = PL_ppaddr[OP_PADHV];
8514 return ref(o, OP_RV2HV);
8518 o->op_type = OP_RV2HV;
8519 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
8524 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
8531 Perl_newAVREF(pTHX_ OP *o)
8535 PERL_ARGS_ASSERT_NEWAVREF;
8537 if (o->op_type == OP_PADANY) {
8538 o->op_type = OP_PADAV;
8539 o->op_ppaddr = PL_ppaddr[OP_PADAV];
8542 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
8543 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8544 "Using an array as a reference is deprecated");
8546 return newUNOP(OP_RV2AV, 0, scalar(o));
8550 Perl_newGVREF(pTHX_ I32 type, OP *o)
8552 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
8553 return newUNOP(OP_NULL, 0, o);
8554 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
8558 Perl_newHVREF(pTHX_ OP *o)
8562 PERL_ARGS_ASSERT_NEWHVREF;
8564 if (o->op_type == OP_PADANY) {
8565 o->op_type = OP_PADHV;
8566 o->op_ppaddr = PL_ppaddr[OP_PADHV];
8569 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
8570 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8571 "Using a hash as a reference is deprecated");
8573 return newUNOP(OP_RV2HV, 0, scalar(o));
8577 Perl_newCVREF(pTHX_ I32 flags, OP *o)
8579 if (o->op_type == OP_PADANY) {
8581 o->op_type = OP_PADCV;
8582 o->op_ppaddr = PL_ppaddr[OP_PADCV];
8584 return newUNOP(OP_RV2CV, flags, scalar(o));
8588 Perl_newSVREF(pTHX_ OP *o)
8592 PERL_ARGS_ASSERT_NEWSVREF;
8594 if (o->op_type == OP_PADANY) {
8595 o->op_type = OP_PADSV;
8596 o->op_ppaddr = PL_ppaddr[OP_PADSV];
8599 return newUNOP(OP_RV2SV, 0, scalar(o));
8602 /* Check routines. See the comments at the top of this file for details
8603 * on when these are called */
8606 Perl_ck_anoncode(pTHX_ OP *o)
8608 PERL_ARGS_ASSERT_CK_ANONCODE;
8610 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
8612 cSVOPo->op_sv = NULL;
8617 S_io_hints(pTHX_ OP *o)
8620 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
8622 SV **svp = hv_fetchs(table, "open_IN", FALSE);
8625 const char *d = SvPV_const(*svp, len);
8626 const I32 mode = mode_from_discipline(d, len);
8627 if (mode & O_BINARY)
8628 o->op_private |= OPpOPEN_IN_RAW;
8629 else if (mode & O_TEXT)
8630 o->op_private |= OPpOPEN_IN_CRLF;
8633 svp = hv_fetchs(table, "open_OUT", FALSE);
8636 const char *d = SvPV_const(*svp, len);
8637 const I32 mode = mode_from_discipline(d, len);
8638 if (mode & O_BINARY)
8639 o->op_private |= OPpOPEN_OUT_RAW;
8640 else if (mode & O_TEXT)
8641 o->op_private |= OPpOPEN_OUT_CRLF;
8647 Perl_ck_backtick(pTHX_ OP *o)
8651 PERL_ARGS_ASSERT_CK_BACKTICK;
8652 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
8653 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_sibling
8654 && (gv = gv_override("readpipe",8))) {
8655 newop = S_new_entersubop(aTHX_ gv, cUNOPo->op_first->op_sibling);
8656 cUNOPo->op_first->op_sibling = NULL;
8658 else if (!(o->op_flags & OPf_KIDS))
8659 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
8662 op_getmad(o,newop,'O');
8668 S_io_hints(aTHX_ o);
8673 Perl_ck_bitop(pTHX_ OP *o)
8677 PERL_ARGS_ASSERT_CK_BITOP;
8679 o->op_private = (U8)(PL_hints & HINT_INTEGER);
8680 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
8681 && (o->op_type == OP_BIT_OR
8682 || o->op_type == OP_BIT_AND
8683 || o->op_type == OP_BIT_XOR))
8685 const OP * const left = cBINOPo->op_first;
8686 const OP * const right = left->op_sibling;
8687 if ((OP_IS_NUMCOMPARE(left->op_type) &&
8688 (left->op_flags & OPf_PARENS) == 0) ||
8689 (OP_IS_NUMCOMPARE(right->op_type) &&
8690 (right->op_flags & OPf_PARENS) == 0))
8691 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
8692 "Possible precedence problem on bitwise %c operator",
8693 o->op_type == OP_BIT_OR ? '|'
8694 : o->op_type == OP_BIT_AND ? '&' : '^'
8700 PERL_STATIC_INLINE bool
8701 is_dollar_bracket(pTHX_ const OP * const o)
8704 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
8705 && (kid = cUNOPx(o)->op_first)
8706 && kid->op_type == OP_GV
8707 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
8711 Perl_ck_cmp(pTHX_ OP *o)
8713 PERL_ARGS_ASSERT_CK_CMP;
8714 if (ckWARN(WARN_SYNTAX)) {
8715 const OP *kid = cUNOPo->op_first;
8718 is_dollar_bracket(aTHX_ kid)
8719 && kid->op_sibling && kid->op_sibling->op_type == OP_CONST
8721 || ( kid->op_type == OP_CONST
8722 && (kid = kid->op_sibling) && is_dollar_bracket(aTHX_ kid))
8724 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
8725 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
8731 Perl_ck_concat(pTHX_ OP *o)
8733 const OP * const kid = cUNOPo->op_first;
8735 PERL_ARGS_ASSERT_CK_CONCAT;
8736 PERL_UNUSED_CONTEXT;
8738 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
8739 !(kUNOP->op_first->op_flags & OPf_MOD))
8740 o->op_flags |= OPf_STACKED;
8745 Perl_ck_spair(pTHX_ OP *o)
8749 PERL_ARGS_ASSERT_CK_SPAIR;
8751 if (o->op_flags & OPf_KIDS) {
8754 const OPCODE type = o->op_type;
8755 o = modkids(ck_fun(o), type);
8756 kid = cUNOPo->op_first;
8757 newop = kUNOP->op_first->op_sibling;
8759 const OPCODE type = newop->op_type;
8760 if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
8761 type == OP_PADAV || type == OP_PADHV ||
8762 type == OP_RV2AV || type == OP_RV2HV)
8766 op_getmad(kUNOP->op_first,newop,'K');
8768 op_free(kUNOP->op_first);
8770 kUNOP->op_first = newop;
8772 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
8773 * and OP_CHOMP into OP_SCHOMP */
8774 o->op_ppaddr = PL_ppaddr[++o->op_type];
8779 Perl_ck_delete(pTHX_ OP *o)
8781 PERL_ARGS_ASSERT_CK_DELETE;
8785 if (o->op_flags & OPf_KIDS) {
8786 OP * const kid = cUNOPo->op_first;
8787 switch (kid->op_type) {
8789 o->op_flags |= OPf_SPECIAL;
8792 o->op_private |= OPpSLICE;
8795 o->op_flags |= OPf_SPECIAL;
8800 Perl_croak(aTHX_ "delete argument is index/value array slice,"
8801 " use array slice");
8803 Perl_croak(aTHX_ "delete argument is key/value hash slice, use"
8806 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
8807 "element or slice");
8809 if (kid->op_private & OPpLVAL_INTRO)
8810 o->op_private |= OPpLVAL_INTRO;
8817 Perl_ck_eof(pTHX_ OP *o)
8821 PERL_ARGS_ASSERT_CK_EOF;
8823 if (o->op_flags & OPf_KIDS) {
8825 if (cLISTOPo->op_first->op_type == OP_STUB) {
8827 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
8829 op_getmad(o,newop,'O');
8836 kid = cLISTOPo->op_first;
8837 if (kid->op_type == OP_RV2GV)
8838 kid->op_private |= OPpALLOW_FAKE;
8844 Perl_ck_eval(pTHX_ OP *o)
8848 PERL_ARGS_ASSERT_CK_EVAL;
8850 PL_hints |= HINT_BLOCK_SCOPE;
8851 if (o->op_flags & OPf_KIDS) {
8852 SVOP * const kid = (SVOP*)cUNOPo->op_first;
8855 if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
8861 cUNOPo->op_first = 0;
8866 NewOp(1101, enter, 1, LOGOP);
8867 enter->op_type = OP_ENTERTRY;
8868 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
8869 enter->op_private = 0;
8871 /* establish postfix order */
8872 enter->op_next = (OP*)enter;
8874 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
8875 o->op_type = OP_LEAVETRY;
8876 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
8877 enter->op_other = o;
8878 op_getmad(oldo,o,'O');
8887 const U8 priv = o->op_private;
8893 o = newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
8894 op_getmad(oldo,o,'O');
8896 o->op_targ = (PADOFFSET)PL_hints;
8897 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
8898 if ((PL_hints & HINT_LOCALIZE_HH) != 0
8899 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
8900 /* Store a copy of %^H that pp_entereval can pick up. */
8901 OP *hhop = newSVOP(OP_HINTSEVAL, 0,
8902 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv))));
8903 cUNOPo->op_first->op_sibling = hhop;
8904 o->op_private |= OPpEVAL_HAS_HH;
8906 if (!(o->op_private & OPpEVAL_BYTES)
8907 && FEATURE_UNIEVAL_IS_ENABLED)
8908 o->op_private |= OPpEVAL_UNICODE;
8913 Perl_ck_exec(pTHX_ OP *o)
8915 PERL_ARGS_ASSERT_CK_EXEC;
8917 if (o->op_flags & OPf_STACKED) {
8920 kid = cUNOPo->op_first->op_sibling;
8921 if (kid->op_type == OP_RV2GV)
8930 Perl_ck_exists(pTHX_ OP *o)
8934 PERL_ARGS_ASSERT_CK_EXISTS;
8937 if (o->op_flags & OPf_KIDS) {
8938 OP * const kid = cUNOPo->op_first;
8939 if (kid->op_type == OP_ENTERSUB) {
8940 (void) ref(kid, o->op_type);
8941 if (kid->op_type != OP_RV2CV
8942 && !(PL_parser && PL_parser->error_count))
8944 "exists argument is not a subroutine name");
8945 o->op_private |= OPpEXISTS_SUB;
8947 else if (kid->op_type == OP_AELEM)
8948 o->op_flags |= OPf_SPECIAL;
8949 else if (kid->op_type != OP_HELEM)
8950 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
8951 "element or a subroutine");
8958 Perl_ck_rvconst(pTHX_ OP *o)
8961 SVOP * const kid = (SVOP*)cUNOPo->op_first;
8963 PERL_ARGS_ASSERT_CK_RVCONST;
8965 o->op_private |= (PL_hints & HINT_STRICT_REFS);
8966 if (o->op_type == OP_RV2CV)
8967 o->op_private &= ~1;
8969 if (kid->op_type == OP_CONST) {
8972 SV * const kidsv = kid->op_sv;
8974 /* Is it a constant from cv_const_sv()? */
8975 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
8976 SV * const rsv = SvRV(kidsv);
8977 const svtype type = SvTYPE(rsv);
8978 const char *badtype = NULL;
8980 switch (o->op_type) {
8982 if (type > SVt_PVMG)
8983 badtype = "a SCALAR";
8986 if (type != SVt_PVAV)
8987 badtype = "an ARRAY";
8990 if (type != SVt_PVHV)
8994 if (type != SVt_PVCV)
8999 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
9002 if (SvTYPE(kidsv) == SVt_PVAV) return o;
9003 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
9004 const char *badthing;
9005 switch (o->op_type) {
9007 badthing = "a SCALAR";
9010 badthing = "an ARRAY";
9013 badthing = "a HASH";
9021 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
9022 SVfARG(kidsv), badthing);
9025 * This is a little tricky. We only want to add the symbol if we
9026 * didn't add it in the lexer. Otherwise we get duplicate strict
9027 * warnings. But if we didn't add it in the lexer, we must at
9028 * least pretend like we wanted to add it even if it existed before,
9029 * or we get possible typo warnings. OPpCONST_ENTERED says
9030 * whether the lexer already added THIS instance of this symbol.
9032 iscv = (o->op_type == OP_RV2CV) * 2;
9034 gv = gv_fetchsv(kidsv,
9035 iscv | !(kid->op_private & OPpCONST_ENTERED),
9038 : o->op_type == OP_RV2SV
9040 : o->op_type == OP_RV2AV
9042 : o->op_type == OP_RV2HV
9045 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
9047 kid->op_type = OP_GV;
9048 SvREFCNT_dec(kid->op_sv);
9050 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
9051 assert (sizeof(PADOP) <= sizeof(SVOP));
9052 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
9053 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
9055 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
9057 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
9059 kid->op_private = 0;
9060 kid->op_ppaddr = PL_ppaddr[OP_GV];
9061 /* FAKE globs in the symbol table cause weird bugs (#77810) */
9069 Perl_ck_ftst(pTHX_ OP *o)
9072 const I32 type = o->op_type;
9074 PERL_ARGS_ASSERT_CK_FTST;
9076 if (o->op_flags & OPf_REF) {
9079 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
9080 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9081 const OPCODE kidtype = kid->op_type;
9083 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
9084 && !kid->op_folded) {
9085 OP * const newop = newGVOP(type, OPf_REF,
9086 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
9088 op_getmad(o,newop,'O');
9094 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
9095 o->op_private |= OPpFT_ACCESS;
9096 if (PL_check[kidtype] == Perl_ck_ftst
9097 && kidtype != OP_STAT && kidtype != OP_LSTAT) {
9098 o->op_private |= OPpFT_STACKED;
9099 kid->op_private |= OPpFT_STACKING;
9100 if (kidtype == OP_FTTTY && (
9101 !(kid->op_private & OPpFT_STACKED)
9102 || kid->op_private & OPpFT_AFTER_t
9104 o->op_private |= OPpFT_AFTER_t;
9113 if (type == OP_FTTTY)
9114 o = newGVOP(type, OPf_REF, PL_stdingv);
9116 o = newUNOP(type, 0, newDEFSVOP());
9117 op_getmad(oldo,o,'O');
9123 Perl_ck_fun(pTHX_ OP *o)
9126 const int type = o->op_type;
9127 I32 oa = PL_opargs[type] >> OASHIFT;
9129 PERL_ARGS_ASSERT_CK_FUN;
9131 if (o->op_flags & OPf_STACKED) {
9132 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
9135 return no_fh_allowed(o);
9138 if (o->op_flags & OPf_KIDS) {
9139 OP **tokid = &cLISTOPo->op_first;
9140 OP *kid = cLISTOPo->op_first;
9143 bool seen_optional = FALSE;
9145 if (kid->op_type == OP_PUSHMARK ||
9146 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
9148 tokid = &kid->op_sibling;
9149 kid = kid->op_sibling;
9151 if (kid && kid->op_type == OP_COREARGS) {
9152 bool optional = FALSE;
9155 if (oa & OA_OPTIONAL) optional = TRUE;
9158 if (optional) o->op_private |= numargs;
9163 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
9164 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV)
9165 *tokid = kid = newDEFSVOP();
9166 seen_optional = TRUE;
9171 sibl = kid->op_sibling;
9173 if (!sibl && kid->op_type == OP_STUB) {
9180 /* list seen where single (scalar) arg expected? */
9181 if (numargs == 1 && !(oa >> 4)
9182 && kid->op_type == OP_LIST && type != OP_SCALAR)
9184 return too_many_arguments_pv(o,PL_op_desc[type], 0);
9186 if (type != OP_DELETE) scalar(kid);
9197 if ((type == OP_PUSH || type == OP_UNSHIFT)
9198 && !kid->op_sibling)
9199 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
9200 "Useless use of %s with no values",
9203 if (kid->op_type == OP_CONST &&
9204 (kid->op_private & OPpCONST_BARE))
9206 OP * const newop = newAVREF(newGVOP(OP_GV, 0,
9207 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
9208 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9209 "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
9210 SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
9212 op_getmad(kid,newop,'K');
9217 kid->op_sibling = sibl;
9220 else if (kid->op_type == OP_CONST
9221 && ( !SvROK(cSVOPx_sv(kid))
9222 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
9224 bad_type_pv(numargs, "array", PL_op_desc[type], 0, kid);
9225 /* Defer checks to run-time if we have a scalar arg */
9226 if (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV)
9227 op_lvalue(kid, type);
9230 /* diag_listed_as: push on reference is experimental */
9231 Perl_ck_warner_d(aTHX_
9232 packWARN(WARN_EXPERIMENTAL__AUTODEREF),
9233 "%s on reference is experimental",
9238 if (kid->op_type == OP_CONST &&
9239 (kid->op_private & OPpCONST_BARE))
9241 OP * const newop = newHVREF(newGVOP(OP_GV, 0,
9242 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
9243 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9244 "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
9245 SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
9247 op_getmad(kid,newop,'K');
9252 kid->op_sibling = sibl;
9255 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
9256 bad_type_pv(numargs, "hash", PL_op_desc[type], 0, kid);
9257 op_lvalue(kid, type);
9261 OP * const newop = newUNOP(OP_NULL, 0, kid);
9262 kid->op_sibling = 0;
9263 newop->op_next = newop;
9265 kid->op_sibling = sibl;
9270 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
9271 if (kid->op_type == OP_CONST &&
9272 (kid->op_private & OPpCONST_BARE))
9274 OP * const newop = newGVOP(OP_GV, 0,
9275 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
9276 if (!(o->op_private & 1) && /* if not unop */
9277 kid == cLISTOPo->op_last)
9278 cLISTOPo->op_last = newop;
9280 op_getmad(kid,newop,'K');
9286 else if (kid->op_type == OP_READLINE) {
9287 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
9288 bad_type_pv(numargs, "HANDLE", OP_DESC(o), 0, kid);
9291 I32 flags = OPf_SPECIAL;
9295 /* is this op a FH constructor? */
9296 if (is_handle_constructor(o,numargs)) {
9297 const char *name = NULL;
9300 bool want_dollar = TRUE;
9303 /* Set a flag to tell rv2gv to vivify
9304 * need to "prove" flag does not mean something
9305 * else already - NI-S 1999/05/07
9308 if (kid->op_type == OP_PADSV) {
9310 = PAD_COMPNAME_SV(kid->op_targ);
9311 name = SvPV_const(namesv, len);
9312 name_utf8 = SvUTF8(namesv);
9314 else if (kid->op_type == OP_RV2SV
9315 && kUNOP->op_first->op_type == OP_GV)
9317 GV * const gv = cGVOPx_gv(kUNOP->op_first);
9319 len = GvNAMELEN(gv);
9320 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
9322 else if (kid->op_type == OP_AELEM
9323 || kid->op_type == OP_HELEM)
9326 OP *op = ((BINOP*)kid)->op_first;
9330 const char * const a =
9331 kid->op_type == OP_AELEM ?
9333 if (((op->op_type == OP_RV2AV) ||
9334 (op->op_type == OP_RV2HV)) &&
9335 (firstop = ((UNOP*)op)->op_first) &&
9336 (firstop->op_type == OP_GV)) {
9337 /* packagevar $a[] or $h{} */
9338 GV * const gv = cGVOPx_gv(firstop);
9346 else if (op->op_type == OP_PADAV
9347 || op->op_type == OP_PADHV) {
9348 /* lexicalvar $a[] or $h{} */
9349 const char * const padname =
9350 PAD_COMPNAME_PV(op->op_targ);
9359 name = SvPV_const(tmpstr, len);
9360 name_utf8 = SvUTF8(tmpstr);
9365 name = "__ANONIO__";
9367 want_dollar = FALSE;
9369 op_lvalue(kid, type);
9373 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
9374 namesv = PAD_SVl(targ);
9375 if (want_dollar && *name != '$')
9376 sv_setpvs(namesv, "$");
9378 sv_setpvs(namesv, "");
9379 sv_catpvn(namesv, name, len);
9380 if ( name_utf8 ) SvUTF8_on(namesv);
9383 kid->op_sibling = 0;
9384 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
9385 kid->op_targ = targ;
9386 kid->op_private |= priv;
9388 kid->op_sibling = sibl;
9394 if ((type == OP_UNDEF || type == OP_POS)
9395 && numargs == 1 && !(oa >> 4)
9396 && kid->op_type == OP_LIST)
9397 return too_many_arguments_pv(o,PL_op_desc[type], 0);
9398 op_lvalue(scalar(kid), type);
9402 tokid = &kid->op_sibling;
9403 kid = kid->op_sibling;
9406 if (kid && kid->op_type != OP_STUB)
9407 return too_many_arguments_pv(o,OP_DESC(o), 0);
9408 o->op_private |= numargs;
9410 /* FIXME - should the numargs move as for the PERL_MAD case? */
9411 o->op_private |= numargs;
9413 return too_many_arguments_pv(o,OP_DESC(o), 0);
9417 else if (PL_opargs[type] & OA_DEFGV) {
9419 OP *newop = newUNOP(type, 0, newDEFSVOP());
9420 op_getmad(o,newop,'O');
9423 /* Ordering of these two is important to keep f_map.t passing. */
9425 return newUNOP(type, 0, newDEFSVOP());
9430 while (oa & OA_OPTIONAL)
9432 if (oa && oa != OA_LIST)
9433 return too_few_arguments_pv(o,OP_DESC(o), 0);
9439 Perl_ck_glob(pTHX_ OP *o)
9444 PERL_ARGS_ASSERT_CK_GLOB;
9447 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
9448 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
9450 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
9454 * \ null - const(wildcard)
9459 * \ mark - glob - rv2cv
9460 * | \ gv(CORE::GLOBAL::glob)
9462 * \ null - const(wildcard)
9464 o->op_flags |= OPf_SPECIAL;
9465 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
9466 o = S_new_entersubop(aTHX_ gv, o);
9467 o = newUNOP(OP_NULL, 0, o);
9468 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
9471 else o->op_flags &= ~OPf_SPECIAL;
9472 #if !defined(PERL_EXTERNAL_GLOB)
9475 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
9476 newSVpvs("File::Glob"), NULL, NULL, NULL);
9479 #endif /* !PERL_EXTERNAL_GLOB */
9480 gv = (GV *)newSV(0);
9481 gv_init(gv, 0, "", 0, 0);
9483 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
9484 SvREFCNT_dec_NN(gv); /* newGVOP increased it */
9490 Perl_ck_grep(pTHX_ OP *o)
9495 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
9498 PERL_ARGS_ASSERT_CK_GREP;
9500 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
9501 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
9503 if (o->op_flags & OPf_STACKED) {
9504 kid = cUNOPx(cLISTOPo->op_first->op_sibling)->op_first;
9505 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
9506 return no_fh_allowed(o);
9507 o->op_flags &= ~OPf_STACKED;
9509 kid = cLISTOPo->op_first->op_sibling;
9510 if (type == OP_MAPWHILE)
9515 if (PL_parser && PL_parser->error_count)
9517 kid = cLISTOPo->op_first->op_sibling;
9518 if (kid->op_type != OP_NULL)
9519 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
9520 kid = kUNOP->op_first;
9522 NewOp(1101, gwop, 1, LOGOP);
9523 gwop->op_type = type;
9524 gwop->op_ppaddr = PL_ppaddr[type];
9526 gwop->op_flags |= OPf_KIDS;
9527 gwop->op_other = LINKLIST(kid);
9528 kid->op_next = (OP*)gwop;
9529 offset = pad_findmy_pvs("$_", 0);
9530 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
9531 o->op_private = gwop->op_private = 0;
9532 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
9535 o->op_private = gwop->op_private = OPpGREP_LEX;
9536 gwop->op_targ = o->op_targ = offset;
9539 kid = cLISTOPo->op_first->op_sibling;
9540 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
9541 op_lvalue(kid, OP_GREPSTART);
9547 Perl_ck_index(pTHX_ OP *o)
9549 PERL_ARGS_ASSERT_CK_INDEX;
9551 if (o->op_flags & OPf_KIDS) {
9552 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9554 kid = kid->op_sibling; /* get past "big" */
9555 if (kid && kid->op_type == OP_CONST) {
9556 const bool save_taint = TAINT_get;
9557 SV *sv = kSVOP->op_sv;
9558 if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) {
9560 sv_copypv(sv, kSVOP->op_sv);
9561 SvREFCNT_dec_NN(kSVOP->op_sv);
9564 if (SvOK(sv)) fbm_compile(sv, 0);
9565 TAINT_set(save_taint);
9566 #ifdef NO_TAINT_SUPPORT
9567 PERL_UNUSED_VAR(save_taint);
9575 Perl_ck_lfun(pTHX_ OP *o)
9577 const OPCODE type = o->op_type;
9579 PERL_ARGS_ASSERT_CK_LFUN;
9581 return modkids(ck_fun(o), type);
9585 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
9587 PERL_ARGS_ASSERT_CK_DEFINED;
9589 if ((o->op_flags & OPf_KIDS)) {
9590 switch (cUNOPo->op_first->op_type) {
9593 case OP_AASSIGN: /* Is this a good idea? */
9594 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9595 "defined(@array) is deprecated");
9596 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9597 "\t(Maybe you should just omit the defined()?)\n");
9601 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9602 "defined(%%hash) is deprecated");
9603 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
9604 "\t(Maybe you should just omit the defined()?)\n");
9615 Perl_ck_readline(pTHX_ OP *o)
9617 PERL_ARGS_ASSERT_CK_READLINE;
9619 if (o->op_flags & OPf_KIDS) {
9620 OP *kid = cLISTOPo->op_first;
9621 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
9625 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
9627 op_getmad(o,newop,'O');
9637 Perl_ck_rfun(pTHX_ OP *o)
9639 const OPCODE type = o->op_type;
9641 PERL_ARGS_ASSERT_CK_RFUN;
9643 return refkids(ck_fun(o), type);
9647 Perl_ck_listiob(pTHX_ OP *o)
9651 PERL_ARGS_ASSERT_CK_LISTIOB;
9653 kid = cLISTOPo->op_first;
9656 kid = cLISTOPo->op_first;
9658 if (kid->op_type == OP_PUSHMARK)
9659 kid = kid->op_sibling;
9660 if (kid && o->op_flags & OPf_STACKED)
9661 kid = kid->op_sibling;
9662 else if (kid && !kid->op_sibling) { /* print HANDLE; */
9663 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
9664 && !kid->op_folded) {
9665 o->op_flags |= OPf_STACKED; /* make it a filehandle */
9666 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
9667 cLISTOPo->op_first->op_sibling = kid;
9668 cLISTOPo->op_last = kid;
9669 kid = kid->op_sibling;
9674 op_append_elem(o->op_type, o, newDEFSVOP());
9676 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
9681 Perl_ck_smartmatch(pTHX_ OP *o)
9684 PERL_ARGS_ASSERT_CK_SMARTMATCH;
9685 if (0 == (o->op_flags & OPf_SPECIAL)) {
9686 OP *first = cBINOPo->op_first;
9687 OP *second = first->op_sibling;
9689 /* Implicitly take a reference to an array or hash */
9690 first->op_sibling = NULL;
9691 first = cBINOPo->op_first = ref_array_or_hash(first);
9692 second = first->op_sibling = ref_array_or_hash(second);
9694 /* Implicitly take a reference to a regular expression */
9695 if (first->op_type == OP_MATCH) {
9696 first->op_type = OP_QR;
9697 first->op_ppaddr = PL_ppaddr[OP_QR];
9699 if (second->op_type == OP_MATCH) {
9700 second->op_type = OP_QR;
9701 second->op_ppaddr = PL_ppaddr[OP_QR];
9710 Perl_ck_sassign(pTHX_ OP *o)
9713 OP * const kid = cLISTOPo->op_first;
9715 PERL_ARGS_ASSERT_CK_SASSIGN;
9717 /* has a disposable target? */
9718 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
9719 && !(kid->op_flags & OPf_STACKED)
9720 /* Cannot steal the second time! */
9721 && !(kid->op_private & OPpTARGET_MY)
9722 /* Keep the full thing for madskills */
9726 OP * const kkid = kid->op_sibling;
9728 /* Can just relocate the target. */
9729 if (kkid && kkid->op_type == OP_PADSV
9730 && !(kkid->op_private & OPpLVAL_INTRO))
9732 kid->op_targ = kkid->op_targ;
9734 /* Now we do not need PADSV and SASSIGN. */
9735 kid->op_sibling = o->op_sibling; /* NULL */
9736 cLISTOPo->op_first = NULL;
9739 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
9743 if (kid->op_sibling) {
9744 OP *kkid = kid->op_sibling;
9745 /* For state variable assignment, kkid is a list op whose op_last
9747 if ((kkid->op_type == OP_PADSV ||
9748 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
9749 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
9752 && (kkid->op_private & OPpLVAL_INTRO)
9753 && SvPAD_STATE(*av_fetch(PL_comppad_name, kkid->op_targ, FALSE))) {
9754 const PADOFFSET target = kkid->op_targ;
9755 OP *const other = newOP(OP_PADSV,
9757 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
9758 OP *const first = newOP(OP_NULL, 0);
9759 OP *const nullop = newCONDOP(0, first, o, other);
9760 OP *const condop = first->op_next;
9761 /* hijacking PADSTALE for uninitialized state variables */
9762 SvPADSTALE_on(PAD_SVl(target));
9764 condop->op_type = OP_ONCE;
9765 condop->op_ppaddr = PL_ppaddr[OP_ONCE];
9766 condop->op_targ = target;
9767 other->op_targ = target;
9769 /* Because we change the type of the op here, we will skip the
9770 assignment binop->op_last = binop->op_first->op_sibling; at the
9771 end of Perl_newBINOP(). So need to do it here. */
9772 cBINOPo->op_last = cBINOPo->op_first->op_sibling;
9781 Perl_ck_match(pTHX_ OP *o)
9785 PERL_ARGS_ASSERT_CK_MATCH;
9787 if (o->op_type != OP_QR && PL_compcv) {
9788 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
9789 if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS_isOUR(offset))) {
9790 o->op_targ = offset;
9791 o->op_private |= OPpTARGET_MY;
9794 if (o->op_type == OP_MATCH || o->op_type == OP_QR)
9795 o->op_private |= OPpRUNTIME;
9800 Perl_ck_method(pTHX_ OP *o)
9802 OP * const kid = cUNOPo->op_first;
9804 PERL_ARGS_ASSERT_CK_METHOD;
9806 if (kid->op_type == OP_CONST) {
9807 SV* sv = kSVOP->op_sv;
9808 const char * const method = SvPVX_const(sv);
9809 if (!(strchr(method, ':') || strchr(method, '\''))) {
9811 if (!SvIsCOW_shared_hash(sv)) {
9812 sv = newSVpvn_share(method, SvUTF8(sv) ? -(I32)SvCUR(sv) : (I32)SvCUR(sv), 0);
9815 kSVOP->op_sv = NULL;
9817 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
9819 op_getmad(o,cmop,'O');
9830 Perl_ck_null(pTHX_ OP *o)
9832 PERL_ARGS_ASSERT_CK_NULL;
9833 PERL_UNUSED_CONTEXT;
9838 Perl_ck_open(pTHX_ OP *o)
9842 PERL_ARGS_ASSERT_CK_OPEN;
9844 S_io_hints(aTHX_ o);
9846 /* In case of three-arg dup open remove strictness
9847 * from the last arg if it is a bareword. */
9848 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
9849 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
9853 if ((last->op_type == OP_CONST) && /* The bareword. */
9854 (last->op_private & OPpCONST_BARE) &&
9855 (last->op_private & OPpCONST_STRICT) &&
9856 (oa = first->op_sibling) && /* The fh. */
9857 (oa = oa->op_sibling) && /* The mode. */
9858 (oa->op_type == OP_CONST) &&
9859 SvPOK(((SVOP*)oa)->op_sv) &&
9860 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
9861 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
9862 (last == oa->op_sibling)) /* The bareword. */
9863 last->op_private &= ~OPpCONST_STRICT;
9869 Perl_ck_repeat(pTHX_ OP *o)
9871 PERL_ARGS_ASSERT_CK_REPEAT;
9873 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
9874 o->op_private |= OPpREPEAT_DOLIST;
9875 cBINOPo->op_first = force_list(cBINOPo->op_first);
9883 Perl_ck_require(pTHX_ OP *o)
9888 PERL_ARGS_ASSERT_CK_REQUIRE;
9890 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
9891 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9893 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
9894 SV * const sv = kid->op_sv;
9895 U32 was_readonly = SvREADONLY(sv);
9903 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
9908 for (; s < end; s++) {
9909 if (*s == ':' && s[1] == ':') {
9911 Move(s+2, s+1, end - s - 1, char);
9916 sv_catpvs(sv, ".pm");
9917 SvFLAGS(sv) |= was_readonly;
9921 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
9922 /* handle override, if any */
9923 && (gv = gv_override("require", 7))) {
9925 if (o->op_flags & OPf_KIDS) {
9926 kid = cUNOPo->op_first;
9927 cUNOPo->op_first = NULL;
9935 newop = S_new_entersubop(aTHX_ gv, kid);
9936 op_getmad(o,newop,'O');
9940 return scalar(ck_fun(o));
9944 Perl_ck_return(pTHX_ OP *o)
9949 PERL_ARGS_ASSERT_CK_RETURN;
9951 kid = cLISTOPo->op_first->op_sibling;
9952 if (CvLVALUE(PL_compcv)) {
9953 for (; kid; kid = kid->op_sibling)
9954 op_lvalue(kid, OP_LEAVESUBLV);
9961 Perl_ck_select(pTHX_ OP *o)
9966 PERL_ARGS_ASSERT_CK_SELECT;
9968 if (o->op_flags & OPf_KIDS) {
9969 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9970 if (kid && kid->op_sibling) {
9971 o->op_type = OP_SSELECT;
9972 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
9974 return fold_constants(op_integerize(op_std_init(o)));
9978 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9979 if (kid && kid->op_type == OP_RV2GV)
9980 kid->op_private &= ~HINT_STRICT_REFS;
9985 Perl_ck_shift(pTHX_ OP *o)
9988 const I32 type = o->op_type;
9990 PERL_ARGS_ASSERT_CK_SHIFT;
9992 if (!(o->op_flags & OPf_KIDS)) {
9995 if (!CvUNIQUE(PL_compcv)) {
9996 o->op_flags |= OPf_SPECIAL;
10000 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
10003 OP * const oldo = o;
10004 o = newUNOP(type, 0, scalar(argop));
10005 op_getmad(oldo,o,'O');
10010 return newUNOP(type, 0, scalar(argop));
10013 return scalar(ck_fun(o));
10017 Perl_ck_sort(pTHX_ OP *o)
10022 HV * const hinthv =
10023 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;
10026 PERL_ARGS_ASSERT_CK_SORT;
10029 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
10031 const I32 sorthints = (I32)SvIV(*svp);
10032 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
10033 o->op_private |= OPpSORT_QSORT;
10034 if ((sorthints & HINT_SORT_STABLE) != 0)
10035 o->op_private |= OPpSORT_STABLE;
10039 if (o->op_flags & OPf_STACKED)
10041 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
10043 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */
10044 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
10046 /* if the first arg is a code block, process it and mark sort as
10048 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
10050 if (kid->op_type == OP_LEAVE)
10051 op_null(kid); /* wipe out leave */
10052 /* Prevent execution from escaping out of the sort block. */
10055 /* provide scalar context for comparison function/block */
10056 kid = scalar(firstkid);
10057 kid->op_next = kid;
10058 o->op_flags |= OPf_SPECIAL;
10061 firstkid = firstkid->op_sibling;
10064 for (kid = firstkid; kid; kid = kid->op_sibling) {
10065 /* provide list context for arguments */
10068 op_lvalue(kid, OP_GREPSTART);
10074 /* for sort { X } ..., where X is one of
10075 * $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a
10076 * elide the second child of the sort (the one containing X),
10077 * and set these flags as appropriate
10081 * Also, check and warn on lexical $a, $b.
10085 S_simplify_sort(pTHX_ OP *o)
10088 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
10092 const char *gvname;
10095 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
10097 kid = kUNOP->op_first; /* get past null */
10098 if (!(have_scopeop = kid->op_type == OP_SCOPE)
10099 && kid->op_type != OP_LEAVE)
10101 kid = kLISTOP->op_last; /* get past scope */
10102 switch(kid->op_type) {
10106 if (!have_scopeop) goto padkids;
10111 k = kid; /* remember this node*/
10112 if (kBINOP->op_first->op_type != OP_RV2SV
10113 || kBINOP->op_last ->op_type != OP_RV2SV)
10116 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
10117 then used in a comparison. This catches most, but not
10118 all cases. For instance, it catches
10119 sort { my($a); $a <=> $b }
10121 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
10122 (although why you'd do that is anyone's guess).
10126 if (!ckWARN(WARN_SYNTAX)) return;
10127 kid = kBINOP->op_first;
10129 if (kid->op_type == OP_PADSV) {
10130 SV * const name = AvARRAY(PL_comppad_name)[kid->op_targ];
10131 if (SvCUR(name) == 2 && *SvPVX(name) == '$'
10132 && (SvPVX(name)[1] == 'a' || SvPVX(name)[1] == 'b'))
10133 /* diag_listed_as: "my %s" used in sort comparison */
10134 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10135 "\"%s %s\" used in sort comparison",
10136 SvPAD_STATE(name) ? "state" : "my",
10139 } while ((kid = kid->op_sibling));
10142 kid = kBINOP->op_first; /* get past cmp */
10143 if (kUNOP->op_first->op_type != OP_GV)
10145 kid = kUNOP->op_first; /* get past rv2sv */
10147 if (GvSTASH(gv) != PL_curstash)
10149 gvname = GvNAME(gv);
10150 if (*gvname == 'a' && gvname[1] == '\0')
10152 else if (*gvname == 'b' && gvname[1] == '\0')
10157 kid = k; /* back to cmp */
10158 /* already checked above that it is rv2sv */
10159 kid = kBINOP->op_last; /* down to 2nd arg */
10160 if (kUNOP->op_first->op_type != OP_GV)
10162 kid = kUNOP->op_first; /* get past rv2sv */
10164 if (GvSTASH(gv) != PL_curstash)
10166 gvname = GvNAME(gv);
10168 ? !(*gvname == 'a' && gvname[1] == '\0')
10169 : !(*gvname == 'b' && gvname[1] == '\0'))
10171 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
10173 o->op_private |= OPpSORT_DESCEND;
10174 if (k->op_type == OP_NCMP)
10175 o->op_private |= OPpSORT_NUMERIC;
10176 if (k->op_type == OP_I_NCMP)
10177 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
10178 kid = cLISTOPo->op_first->op_sibling;
10179 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
10181 op_getmad(kid,o,'S'); /* then delete it */
10183 op_free(kid); /* then delete it */
10188 Perl_ck_split(pTHX_ OP *o)
10193 PERL_ARGS_ASSERT_CK_SPLIT;
10195 if (o->op_flags & OPf_STACKED)
10196 return no_fh_allowed(o);
10198 kid = cLISTOPo->op_first;
10199 if (kid->op_type != OP_NULL)
10200 Perl_croak(aTHX_ "panic: ck_split, type=%u", (unsigned) kid->op_type);
10201 kid = kid->op_sibling;
10202 op_free(cLISTOPo->op_first);
10204 cLISTOPo->op_first = kid;
10206 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
10207 cLISTOPo->op_last = kid; /* There was only one element previously */
10210 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
10211 OP * const sibl = kid->op_sibling;
10212 kid->op_sibling = 0;
10213 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, 0, 0); /* OPf_SPECIAL is used to trigger split " " behavior */
10214 if (cLISTOPo->op_first == cLISTOPo->op_last)
10215 cLISTOPo->op_last = kid;
10216 cLISTOPo->op_first = kid;
10217 kid->op_sibling = sibl;
10220 kid->op_type = OP_PUSHRE;
10221 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
10223 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
10224 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
10225 "Use of /g modifier is meaningless in split");
10228 if (!kid->op_sibling)
10229 op_append_elem(OP_SPLIT, o, newDEFSVOP());
10231 kid = kid->op_sibling;
10234 if (!kid->op_sibling)
10236 op_append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
10237 o->op_private |= OPpSPLIT_IMPLIM;
10239 assert(kid->op_sibling);
10241 kid = kid->op_sibling;
10244 if (kid->op_sibling)
10245 return too_many_arguments_pv(o,OP_DESC(o), 0);
10251 Perl_ck_join(pTHX_ OP *o)
10253 const OP * const kid = cLISTOPo->op_first->op_sibling;
10255 PERL_ARGS_ASSERT_CK_JOIN;
10257 if (kid && kid->op_type == OP_MATCH) {
10258 if (ckWARN(WARN_SYNTAX)) {
10259 const REGEXP *re = PM_GETRE(kPMOP);
10261 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
10262 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
10263 : newSVpvs_flags( "STRING", SVs_TEMP );
10264 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10265 "/%"SVf"/ should probably be written as \"%"SVf"\"",
10266 SVfARG(msg), SVfARG(msg));
10273 =for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags
10275 Examines an op, which is expected to identify a subroutine at runtime,
10276 and attempts to determine at compile time which subroutine it identifies.
10277 This is normally used during Perl compilation to determine whether
10278 a prototype can be applied to a function call. I<cvop> is the op
10279 being considered, normally an C<rv2cv> op. A pointer to the identified
10280 subroutine is returned, if it could be determined statically, and a null
10281 pointer is returned if it was not possible to determine statically.
10283 Currently, the subroutine can be identified statically if the RV that the
10284 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
10285 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
10286 suitable if the constant value must be an RV pointing to a CV. Details of
10287 this process may change in future versions of Perl. If the C<rv2cv> op
10288 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
10289 the subroutine statically: this flag is used to suppress compile-time
10290 magic on a subroutine call, forcing it to use default runtime behaviour.
10292 If I<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
10293 of a GV reference is modified. If a GV was examined and its CV slot was
10294 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
10295 If the op is not optimised away, and the CV slot is later populated with
10296 a subroutine having a prototype, that flag eventually triggers the warning
10297 "called too early to check prototype".
10299 If I<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
10300 of returning a pointer to the subroutine it returns a pointer to the
10301 GV giving the most appropriate name for the subroutine in this context.
10302 Normally this is just the C<CvGV> of the subroutine, but for an anonymous
10303 (C<CvANON>) subroutine that is referenced through a GV it will be the
10304 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
10305 A null pointer is returned as usual if there is no statically-determinable
10311 /* shared by toke.c:yylex */
10313 Perl_find_lexical_cv(pTHX_ PADOFFSET off)
10315 PADNAME *name = PAD_COMPNAME(off);
10316 CV *compcv = PL_compcv;
10317 while (PadnameOUTER(name)) {
10318 assert(PARENT_PAD_INDEX(name));
10319 compcv = CvOUTSIDE(PL_compcv);
10320 name = PadlistNAMESARRAY(CvPADLIST(compcv))
10321 [off = PARENT_PAD_INDEX(name)];
10323 assert(!PadnameIsOUR(name));
10324 if (!PadnameIsSTATE(name) && SvMAGICAL(name)) {
10325 MAGIC * mg = mg_find(name, PERL_MAGIC_proto);
10327 assert(mg->mg_obj);
10328 return (CV *)mg->mg_obj;
10330 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
10334 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
10339 PERL_ARGS_ASSERT_RV2CV_OP_CV;
10340 if (flags & ~(RV2CVOPCV_MARK_EARLY|RV2CVOPCV_RETURN_NAME_GV))
10341 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
10342 if (cvop->op_type != OP_RV2CV)
10344 if (cvop->op_private & OPpENTERSUB_AMPER)
10346 if (!(cvop->op_flags & OPf_KIDS))
10348 rvop = cUNOPx(cvop)->op_first;
10349 switch (rvop->op_type) {
10351 gv = cGVOPx_gv(rvop);
10354 if (flags & RV2CVOPCV_MARK_EARLY)
10355 rvop->op_private |= OPpEARLY_CV;
10360 SV *rv = cSVOPx_sv(rvop);
10363 cv = (CV*)SvRV(rv);
10367 cv = find_lexical_cv(rvop->op_targ);
10374 if (SvTYPE((SV*)cv) != SVt_PVCV)
10376 if (flags & RV2CVOPCV_RETURN_NAME_GV) {
10377 if (!CvANON(cv) || !gv)
10386 =for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop
10388 Performs the default fixup of the arguments part of an C<entersub>
10389 op tree. This consists of applying list context to each of the
10390 argument ops. This is the standard treatment used on a call marked
10391 with C<&>, or a method call, or a call through a subroutine reference,
10392 or any other call where the callee can't be identified at compile time,
10393 or a call where the callee has no prototype.
10399 Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
10402 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
10403 aop = cUNOPx(entersubop)->op_first;
10404 if (!aop->op_sibling)
10405 aop = cUNOPx(aop)->op_first;
10406 for (aop = aop->op_sibling; aop->op_sibling; aop = aop->op_sibling) {
10407 if (!(PL_madskills && aop->op_type == OP_STUB)) {
10409 op_lvalue(aop, OP_ENTERSUB);
10416 =for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv
10418 Performs the fixup of the arguments part of an C<entersub> op tree
10419 based on a subroutine prototype. This makes various modifications to
10420 the argument ops, from applying context up to inserting C<refgen> ops,
10421 and checking the number and syntactic types of arguments, as directed by
10422 the prototype. This is the standard treatment used on a subroutine call,
10423 not marked with C<&>, where the callee can be identified at compile time
10424 and has a prototype.
10426 I<protosv> supplies the subroutine prototype to be applied to the call.
10427 It may be a normal defined scalar, of which the string value will be used.
10428 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
10429 that has been cast to C<SV*>) which has a prototype. The prototype
10430 supplied, in whichever form, does not need to match the actual callee
10431 referenced by the op tree.
10433 If the argument ops disagree with the prototype, for example by having
10434 an unacceptable number of arguments, a valid op tree is returned anyway.
10435 The error is reflected in the parser state, normally resulting in a single
10436 exception at the top level of parsing which covers all the compilation
10437 errors that occurred. In the error message, the callee is referred to
10438 by the name defined by the I<namegv> parameter.
10444 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
10447 const char *proto, *proto_end;
10448 OP *aop, *prev, *cvop;
10451 I32 contextclass = 0;
10452 const char *e = NULL;
10453 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
10454 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
10455 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
10456 "flags=%lx", (unsigned long) SvFLAGS(protosv));
10457 if (SvTYPE(protosv) == SVt_PVCV)
10458 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
10459 else proto = SvPV(protosv, proto_len);
10460 proto = S_strip_spaces(aTHX_ proto, &proto_len);
10461 proto_end = proto + proto_len;
10462 aop = cUNOPx(entersubop)->op_first;
10463 if (!aop->op_sibling)
10464 aop = cUNOPx(aop)->op_first;
10466 aop = aop->op_sibling;
10467 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10468 while (aop != cvop) {
10470 if (PL_madskills && aop->op_type == OP_STUB) {
10471 aop = aop->op_sibling;
10474 if (PL_madskills && aop->op_type == OP_NULL)
10475 o3 = ((UNOP*)aop)->op_first;
10479 if (proto >= proto_end)
10480 return too_many_arguments_sv(entersubop, gv_ename(namegv), 0);
10488 /* _ must be at the end */
10489 if (proto[1] && !strchr(";@%", proto[1]))
10504 if (o3->op_type != OP_REFGEN && o3->op_type != OP_UNDEF)
10506 arg == 1 ? "block or sub {}" : "sub {}",
10510 /* '*' allows any scalar type, including bareword */
10513 if (o3->op_type == OP_RV2GV)
10514 goto wrapref; /* autoconvert GLOB -> GLOBref */
10515 else if (o3->op_type == OP_CONST)
10516 o3->op_private &= ~OPpCONST_STRICT;
10517 else if (o3->op_type == OP_ENTERSUB) {
10518 /* accidental subroutine, revert to bareword */
10519 OP *gvop = ((UNOP*)o3)->op_first;
10520 if (gvop && gvop->op_type == OP_NULL) {
10521 gvop = ((UNOP*)gvop)->op_first;
10523 for (; gvop->op_sibling; gvop = gvop->op_sibling)
10526 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
10527 (gvop = ((UNOP*)gvop)->op_first) &&
10528 gvop->op_type == OP_GV)
10530 GV * const gv = cGVOPx_gv(gvop);
10531 OP * const sibling = aop->op_sibling;
10532 SV * const n = newSVpvs("");
10534 OP * const oldaop = aop;
10538 gv_fullname4(n, gv, "", FALSE);
10539 aop = newSVOP(OP_CONST, 0, n);
10540 op_getmad(oldaop,aop,'O');
10541 prev->op_sibling = aop;
10542 aop->op_sibling = sibling;
10552 if (o3->op_type == OP_RV2AV ||
10553 o3->op_type == OP_PADAV ||
10554 o3->op_type == OP_RV2HV ||
10555 o3->op_type == OP_PADHV
10561 case '[': case ']':
10568 switch (*proto++) {
10570 if (contextclass++ == 0) {
10571 e = strchr(proto, ']');
10572 if (!e || e == proto)
10580 if (contextclass) {
10581 const char *p = proto;
10582 const char *const end = proto;
10584 while (*--p != '[')
10585 /* \[$] accepts any scalar lvalue */
10587 && Perl_op_lvalue_flags(aTHX_
10589 OP_READ, /* not entersub */
10592 bad_type_gv(arg, Perl_form(aTHX_ "one of %.*s",
10593 (int)(end - p), p),
10599 if (o3->op_type == OP_RV2GV)
10602 bad_type_gv(arg, "symbol", namegv, 0, o3);
10605 if (o3->op_type == OP_ENTERSUB)
10608 bad_type_gv(arg, "subroutine entry", namegv, 0,
10612 if (o3->op_type == OP_RV2SV ||
10613 o3->op_type == OP_PADSV ||
10614 o3->op_type == OP_HELEM ||
10615 o3->op_type == OP_AELEM)
10617 if (!contextclass) {
10618 /* \$ accepts any scalar lvalue */
10619 if (Perl_op_lvalue_flags(aTHX_
10621 OP_READ, /* not entersub */
10624 bad_type_gv(arg, "scalar", namegv, 0, o3);
10628 if (o3->op_type == OP_RV2AV ||
10629 o3->op_type == OP_PADAV)
10632 bad_type_gv(arg, "array", namegv, 0, o3);
10635 if (o3->op_type == OP_RV2HV ||
10636 o3->op_type == OP_PADHV)
10639 bad_type_gv(arg, "hash", namegv, 0, o3);
10643 OP* const kid = aop;
10644 OP* const sib = kid->op_sibling;
10645 kid->op_sibling = 0;
10646 aop = newUNOP(OP_REFGEN, 0, kid);
10647 aop->op_sibling = sib;
10648 prev->op_sibling = aop;
10650 if (contextclass && e) {
10655 default: goto oops;
10665 SV* const tmpsv = sv_newmortal();
10666 gv_efullname3(tmpsv, namegv, NULL);
10667 Perl_croak(aTHX_ "Malformed prototype for %"SVf": %"SVf,
10668 SVfARG(tmpsv), SVfARG(protosv));
10672 op_lvalue(aop, OP_ENTERSUB);
10674 aop = aop->op_sibling;
10676 if (aop == cvop && *proto == '_') {
10677 /* generate an access to $_ */
10678 aop = newDEFSVOP();
10679 aop->op_sibling = prev->op_sibling;
10680 prev->op_sibling = aop; /* instead of cvop */
10682 if (!optional && proto_end > proto &&
10683 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
10684 return too_few_arguments_sv(entersubop, gv_ename(namegv), 0);
10689 =for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv
10691 Performs the fixup of the arguments part of an C<entersub> op tree either
10692 based on a subroutine prototype or using default list-context processing.
10693 This is the standard treatment used on a subroutine call, not marked
10694 with C<&>, where the callee can be identified at compile time.
10696 I<protosv> supplies the subroutine prototype to be applied to the call,
10697 or indicates that there is no prototype. It may be a normal scalar,
10698 in which case if it is defined then the string value will be used
10699 as a prototype, and if it is undefined then there is no prototype.
10700 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
10701 that has been cast to C<SV*>), of which the prototype will be used if it
10702 has one. The prototype (or lack thereof) supplied, in whichever form,
10703 does not need to match the actual callee referenced by the op tree.
10705 If the argument ops disagree with the prototype, for example by having
10706 an unacceptable number of arguments, a valid op tree is returned anyway.
10707 The error is reflected in the parser state, normally resulting in a single
10708 exception at the top level of parsing which covers all the compilation
10709 errors that occurred. In the error message, the callee is referred to
10710 by the name defined by the I<namegv> parameter.
10716 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
10717 GV *namegv, SV *protosv)
10719 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
10720 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
10721 return ck_entersub_args_proto(entersubop, namegv, protosv);
10723 return ck_entersub_args_list(entersubop);
10727 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
10729 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv);
10730 OP *aop = cUNOPx(entersubop)->op_first;
10732 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
10736 if (!aop->op_sibling)
10737 aop = cUNOPx(aop)->op_first;
10738 aop = aop->op_sibling;
10739 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10740 if (PL_madskills) while (aop != cvop && aop->op_type == OP_STUB) {
10741 aop = aop->op_sibling;
10744 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0);
10746 op_free(entersubop);
10747 switch(GvNAME(namegv)[2]) {
10748 case 'F': return newSVOP(OP_CONST, 0,
10749 newSVpv(CopFILE(PL_curcop),0));
10750 case 'L': return newSVOP(
10752 Perl_newSVpvf(aTHX_
10753 "%"IVdf, (IV)CopLINE(PL_curcop)
10756 case 'P': return newSVOP(OP_CONST, 0,
10758 ? newSVhek(HvNAME_HEK(PL_curstash))
10769 bool seenarg = FALSE;
10771 if (!aop->op_sibling)
10772 aop = cUNOPx(aop)->op_first;
10775 aop = aop->op_sibling;
10776 prev->op_sibling = NULL;
10779 prev=cvop, cvop = cvop->op_sibling)
10781 if (PL_madskills && cvop->op_sibling
10782 && cvop->op_type != OP_STUB) seenarg = TRUE
10785 prev->op_sibling = NULL;
10786 flags = OPf_SPECIAL * !(cvop->op_private & OPpENTERSUB_NOPAREN);
10788 if (aop == cvop) aop = NULL;
10789 op_free(entersubop);
10791 if (opnum == OP_ENTEREVAL
10792 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9))
10793 flags |= OPpEVAL_BYTES <<8;
10795 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
10797 case OA_BASEOP_OR_UNOP:
10798 case OA_FILESTATOP:
10799 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags);
10803 if (!PL_madskills || seenarg)
10805 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0);
10808 return opnum == OP_RUNCV
10809 ? newPVOP(OP_RUNCV,0,NULL)
10812 return convert(opnum,0,aop);
10820 =for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p
10822 Retrieves the function that will be used to fix up a call to I<cv>.
10823 Specifically, the function is applied to an C<entersub> op tree for a
10824 subroutine call, not marked with C<&>, where the callee can be identified
10825 at compile time as I<cv>.
10827 The C-level function pointer is returned in I<*ckfun_p>, and an SV
10828 argument for it is returned in I<*ckobj_p>. The function is intended
10829 to be called in this manner:
10831 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
10833 In this call, I<entersubop> is a pointer to the C<entersub> op,
10834 which may be replaced by the check function, and I<namegv> is a GV
10835 supplying the name that should be used by the check function to refer
10836 to the callee of the C<entersub> op if it needs to emit any diagnostics.
10837 It is permitted to apply the check function in non-standard situations,
10838 such as to a call to a different subroutine or to a method call.
10840 By default, the function is
10841 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
10842 and the SV parameter is I<cv> itself. This implements standard
10843 prototype processing. It can be changed, for a particular subroutine,
10844 by L</cv_set_call_checker>.
10850 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
10853 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
10854 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
10856 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
10857 *ckobj_p = callmg->mg_obj;
10859 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
10860 *ckobj_p = (SV*)cv;
10865 =for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj
10867 Sets the function that will be used to fix up a call to I<cv>.
10868 Specifically, the function is applied to an C<entersub> op tree for a
10869 subroutine call, not marked with C<&>, where the callee can be identified
10870 at compile time as I<cv>.
10872 The C-level function pointer is supplied in I<ckfun>, and an SV argument
10873 for it is supplied in I<ckobj>. The function should be defined like this:
10875 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
10877 It is intended to be called in this manner:
10879 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
10881 In this call, I<entersubop> is a pointer to the C<entersub> op,
10882 which may be replaced by the check function, and I<namegv> is a GV
10883 supplying the name that should be used by the check function to refer
10884 to the callee of the C<entersub> op if it needs to emit any diagnostics.
10885 It is permitted to apply the check function in non-standard situations,
10886 such as to a call to a different subroutine or to a method call.
10888 The current setting for a particular CV can be retrieved by
10889 L</cv_get_call_checker>.
10895 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
10897 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
10898 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
10899 if (SvMAGICAL((SV*)cv))
10900 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
10903 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
10904 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
10905 if (callmg->mg_flags & MGf_REFCOUNTED) {
10906 SvREFCNT_dec(callmg->mg_obj);
10907 callmg->mg_flags &= ~MGf_REFCOUNTED;
10909 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
10910 callmg->mg_obj = ckobj;
10911 if (ckobj != (SV*)cv) {
10912 SvREFCNT_inc_simple_void_NN(ckobj);
10913 callmg->mg_flags |= MGf_REFCOUNTED;
10915 callmg->mg_flags |= MGf_COPY;
10920 Perl_ck_subr(pTHX_ OP *o)
10926 PERL_ARGS_ASSERT_CK_SUBR;
10928 aop = cUNOPx(o)->op_first;
10929 if (!aop->op_sibling)
10930 aop = cUNOPx(aop)->op_first;
10931 aop = aop->op_sibling;
10932 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10933 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
10934 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_RETURN_NAME_GV) : NULL;
10936 o->op_private &= ~1;
10937 o->op_private |= OPpENTERSUB_HASTARG;
10938 o->op_private |= (PL_hints & HINT_STRICT_REFS);
10939 if (PERLDB_SUB && PL_curstash != PL_debstash)
10940 o->op_private |= OPpENTERSUB_DB;
10941 if (cvop->op_type == OP_RV2CV) {
10942 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
10944 } else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
10945 if (aop->op_type == OP_CONST)
10946 aop->op_private &= ~OPpCONST_STRICT;
10947 else if (aop->op_type == OP_LIST) {
10948 OP * const sib = ((UNOP*)aop)->op_first->op_sibling;
10949 if (sib && sib->op_type == OP_CONST)
10950 sib->op_private &= ~OPpCONST_STRICT;
10955 return ck_entersub_args_list(o);
10957 Perl_call_checker ckfun;
10959 cv_get_call_checker(cv, &ckfun, &ckobj);
10960 if (!namegv) { /* expletive! */
10961 /* XXX The call checker API is public. And it guarantees that
10962 a GV will be provided with the right name. So we have
10963 to create a GV. But it is still not correct, as its
10964 stringification will include the package. What we
10965 really need is a new call checker API that accepts a
10966 GV or string (or GV or CV). */
10967 HEK * const hek = CvNAME_HEK(cv);
10968 /* After a syntax error in a lexical sub, the cv that
10969 rv2cv_op_cv returns may be a nameless stub. */
10970 if (!hek) return ck_entersub_args_list(o);;
10971 namegv = (GV *)sv_newmortal();
10972 gv_init_pvn(namegv, PL_curstash, HEK_KEY(hek), HEK_LEN(hek),
10973 SVf_UTF8 * !!HEK_UTF8(hek));
10975 return ckfun(aTHX_ o, namegv, ckobj);
10980 Perl_ck_svconst(pTHX_ OP *o)
10982 SV * const sv = cSVOPo->op_sv;
10983 PERL_ARGS_ASSERT_CK_SVCONST;
10984 PERL_UNUSED_CONTEXT;
10985 #ifdef PERL_OLD_COPY_ON_WRITE
10986 if (SvIsCOW(sv)) sv_force_normal(sv);
10987 #elif defined(PERL_NEW_COPY_ON_WRITE)
10988 /* Since the read-only flag may be used to protect a string buffer, we
10989 cannot do copy-on-write with existing read-only scalars that are not
10990 already copy-on-write scalars. To allow $_ = "hello" to do COW with
10991 that constant, mark the constant as COWable here, if it is not
10992 already read-only. */
10993 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
10996 # ifdef PERL_DEBUG_READONLY_COW
11006 Perl_ck_trunc(pTHX_ OP *o)
11008 PERL_ARGS_ASSERT_CK_TRUNC;
11010 if (o->op_flags & OPf_KIDS) {
11011 SVOP *kid = (SVOP*)cUNOPo->op_first;
11013 if (kid->op_type == OP_NULL)
11014 kid = (SVOP*)kid->op_sibling;
11015 if (kid && kid->op_type == OP_CONST &&
11016 (kid->op_private & OPpCONST_BARE) &&
11019 o->op_flags |= OPf_SPECIAL;
11020 kid->op_private &= ~OPpCONST_STRICT;
11027 Perl_ck_substr(pTHX_ OP *o)
11029 PERL_ARGS_ASSERT_CK_SUBSTR;
11032 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
11033 OP *kid = cLISTOPo->op_first;
11035 if (kid->op_type == OP_NULL)
11036 kid = kid->op_sibling;
11038 kid->op_flags |= OPf_MOD;
11045 Perl_ck_tell(pTHX_ OP *o)
11047 PERL_ARGS_ASSERT_CK_TELL;
11049 if (o->op_flags & OPf_KIDS) {
11050 OP *kid = cLISTOPo->op_first;
11051 if (kid->op_type == OP_NULL && kid->op_sibling) kid = kid->op_sibling;
11052 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
11058 Perl_ck_each(pTHX_ OP *o)
11061 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
11062 const unsigned orig_type = o->op_type;
11063 const unsigned array_type = orig_type == OP_EACH ? OP_AEACH
11064 : orig_type == OP_KEYS ? OP_AKEYS : OP_AVALUES;
11065 const unsigned ref_type = orig_type == OP_EACH ? OP_REACH
11066 : orig_type == OP_KEYS ? OP_RKEYS : OP_RVALUES;
11068 PERL_ARGS_ASSERT_CK_EACH;
11071 switch (kid->op_type) {
11077 CHANGE_TYPE(o, array_type);
11080 if (kid->op_private == OPpCONST_BARE
11081 || !SvROK(cSVOPx_sv(kid))
11082 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
11083 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
11085 /* we let ck_fun handle it */
11088 CHANGE_TYPE(o, ref_type);
11092 /* if treating as a reference, defer additional checks to runtime */
11093 if (o->op_type == ref_type) {
11094 /* diag_listed_as: keys on reference is experimental */
11095 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__AUTODEREF),
11096 "%s is experimental", PL_op_desc[ref_type]);
11103 Perl_ck_length(pTHX_ OP *o)
11105 PERL_ARGS_ASSERT_CK_LENGTH;
11109 if (ckWARN(WARN_SYNTAX)) {
11110 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
11114 const bool hash = kid->op_type == OP_PADHV
11115 || kid->op_type == OP_RV2HV;
11116 switch (kid->op_type) {
11121 name = S_op_varname(aTHX_ kid);
11127 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11128 "length() used on %"SVf" (did you mean \"scalar(%s%"SVf
11130 name, hash ? "keys " : "", name
11133 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11134 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11135 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
11137 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
11138 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11139 "length() used on @array (did you mean \"scalar(@array)\"?)");
11146 /* Check for in place reverse and sort assignments like "@a = reverse @a"
11147 and modify the optree to make them work inplace */
11150 S_inplace_aassign(pTHX_ OP *o) {
11152 OP *modop, *modop_pushmark;
11154 OP *oleft, *oleft_pushmark;
11156 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
11158 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
11160 assert(cUNOPo->op_first->op_type == OP_NULL);
11161 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
11162 assert(modop_pushmark->op_type == OP_PUSHMARK);
11163 modop = modop_pushmark->op_sibling;
11165 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
11168 /* no other operation except sort/reverse */
11169 if (modop->op_sibling)
11172 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
11173 if (!(oright = cUNOPx(modop)->op_first->op_sibling)) return;
11175 if (modop->op_flags & OPf_STACKED) {
11176 /* skip sort subroutine/block */
11177 assert(oright->op_type == OP_NULL);
11178 oright = oright->op_sibling;
11181 assert(cUNOPo->op_first->op_sibling->op_type == OP_NULL);
11182 oleft_pushmark = cUNOPx(cUNOPo->op_first->op_sibling)->op_first;
11183 assert(oleft_pushmark->op_type == OP_PUSHMARK);
11184 oleft = oleft_pushmark->op_sibling;
11186 /* Check the lhs is an array */
11188 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
11189 || oleft->op_sibling
11190 || (oleft->op_private & OPpLVAL_INTRO)
11194 /* Only one thing on the rhs */
11195 if (oright->op_sibling)
11198 /* check the array is the same on both sides */
11199 if (oleft->op_type == OP_RV2AV) {
11200 if (oright->op_type != OP_RV2AV
11201 || !cUNOPx(oright)->op_first
11202 || cUNOPx(oright)->op_first->op_type != OP_GV
11203 || cUNOPx(oleft )->op_first->op_type != OP_GV
11204 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
11205 cGVOPx_gv(cUNOPx(oright)->op_first)
11209 else if (oright->op_type != OP_PADAV
11210 || oright->op_targ != oleft->op_targ
11214 /* This actually is an inplace assignment */
11216 modop->op_private |= OPpSORT_INPLACE;
11218 /* transfer MODishness etc from LHS arg to RHS arg */
11219 oright->op_flags = oleft->op_flags;
11221 /* remove the aassign op and the lhs */
11223 op_null(oleft_pushmark);
11224 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
11225 op_null(cUNOPx(oleft)->op_first);
11231 /* mechanism for deferring recursion in rpeep() */
11233 #define MAX_DEFERRED 4
11237 if (defer_ix == (MAX_DEFERRED-1)) { \
11238 OP **defer = defer_queue[defer_base]; \
11239 CALL_RPEEP(*defer); \
11240 S_prune_chain_head(aTHX_ defer); \
11241 defer_base = (defer_base + 1) % MAX_DEFERRED; \
11244 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \
11247 #define IS_AND_OP(o) (o->op_type == OP_AND)
11248 #define IS_OR_OP(o) (o->op_type == OP_OR)
11252 S_null_listop_in_list_context(pTHX_ OP *o)
11256 PERL_ARGS_ASSERT_NULL_LISTOP_IN_LIST_CONTEXT;
11258 /* This is an OP_LIST in list context. That means we
11259 * can ditch the OP_LIST and the OP_PUSHMARK within. */
11261 kid = cLISTOPo->op_first;
11262 /* Find the end of the chain of OPs executed within the OP_LIST. */
11263 while (kid->op_next != o)
11264 kid = kid->op_next;
11266 kid->op_next = o->op_next; /* patch list out of exec chain */
11267 op_null(cUNOPo->op_first); /* NULL the pushmark */
11268 op_null(o); /* NULL the list */
11271 /* A peephole optimizer. We visit the ops in the order they're to execute.
11272 * See the comments at the top of this file for more details about when
11273 * peep() is called */
11276 Perl_rpeep(pTHX_ OP *o)
11280 OP* oldoldop = NULL;
11281 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
11282 int defer_base = 0;
11285 if (!o || o->op_opt)
11289 SAVEVPTR(PL_curcop);
11290 for (;; o = o->op_next) {
11291 if (o && o->op_opt)
11294 while (defer_ix >= 0) {
11296 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED];
11297 CALL_RPEEP(*defer);
11298 S_prune_chain_head(aTHX_ defer);
11303 /* By default, this op has now been optimised. A couple of cases below
11304 clear this again. */
11309 /* The following will have the OP_LIST and OP_PUSHMARK
11310 * patched out later IF the OP_LIST is in list context.
11311 * So in that case, we can set the this OP's op_next
11312 * to skip to after the OP_PUSHMARK:
11318 * will eventually become:
11321 * - ex-pushmark -> -
11327 OP *other_pushmark;
11328 if (OP_TYPE_IS(o->op_next, OP_PUSHMARK)
11329 && (sibling = o->op_sibling)
11330 && sibling->op_type == OP_LIST
11331 /* This KIDS check is likely superfluous since OP_LIST
11332 * would otherwise be an OP_STUB. */
11333 && sibling->op_flags & OPf_KIDS
11334 && (sibling->op_flags & OPf_WANT) == OPf_WANT_LIST
11335 && (other_pushmark = cLISTOPx(sibling)->op_first)
11336 /* Pointer equality also effectively checks that it's a
11338 && other_pushmark == o->op_next)
11340 o->op_next = other_pushmark->op_next;
11341 null_listop_in_list_context(sibling);
11345 switch (o->op_type) {
11347 PL_curcop = ((COP*)o); /* for warnings */
11350 PL_curcop = ((COP*)o); /* for warnings */
11352 /* Optimise a "return ..." at the end of a sub to just be "...".
11353 * This saves 2 ops. Before:
11354 * 1 <;> nextstate(main 1 -e:1) v ->2
11355 * 4 <@> return K ->5
11356 * 2 <0> pushmark s ->3
11357 * - <1> ex-rv2sv sK/1 ->4
11358 * 3 <#> gvsv[*cat] s ->4
11361 * - <@> return K ->-
11362 * - <0> pushmark s ->2
11363 * - <1> ex-rv2sv sK/1 ->-
11364 * 2 <$> gvsv(*cat) s ->3
11367 OP *next = o->op_next;
11368 OP *sibling = o->op_sibling;
11369 if ( OP_TYPE_IS(next, OP_PUSHMARK)
11370 && OP_TYPE_IS(sibling, OP_RETURN)
11371 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ)
11372 && OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB)
11373 && cUNOPx(sibling)->op_first == next
11374 && next->op_sibling && next->op_sibling->op_next
11377 /* Look through the PUSHMARK's siblings for one that
11378 * points to the RETURN */
11379 OP *top = next->op_sibling;
11380 while (top && top->op_next) {
11381 if (top->op_next == sibling) {
11382 top->op_next = sibling->op_next;
11383 o->op_next = next->op_next;
11386 top = top->op_sibling;
11391 /* Optimise 'my $x; my $y;' into 'my ($x, $y);'
11393 * This latter form is then suitable for conversion into padrange
11394 * later on. Convert:
11396 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3
11400 * nextstate1 -> listop -> nextstate3
11402 * pushmark -> padop1 -> padop2
11404 if (o->op_next && (
11405 o->op_next->op_type == OP_PADSV
11406 || o->op_next->op_type == OP_PADAV
11407 || o->op_next->op_type == OP_PADHV
11409 && !(o->op_next->op_private & ~OPpLVAL_INTRO)
11410 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE
11411 && o->op_next->op_next->op_next && (
11412 o->op_next->op_next->op_next->op_type == OP_PADSV
11413 || o->op_next->op_next->op_next->op_type == OP_PADAV
11414 || o->op_next->op_next->op_next->op_type == OP_PADHV
11416 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO)
11417 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE
11418 && (!CopLABEL((COP*)o)) /* Don't mess with labels */
11419 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */
11425 first = o->op_next;
11426 last = o->op_next->op_next->op_next;
11428 newop = newLISTOP(OP_LIST, 0, first, last);
11429 newop->op_flags |= OPf_PARENS;
11430 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
11432 /* Kill nextstate2 between padop1/padop2 */
11433 op_free(first->op_next);
11435 first->op_next = last; /* padop2 */
11436 first->op_sibling = last; /* ... */
11437 o->op_next = cUNOPx(newop)->op_first; /* pushmark */
11438 o->op_next->op_next = first; /* padop1 */
11439 o->op_next->op_sibling = first; /* ... */
11440 newop->op_next = last->op_next; /* nextstate3 */
11441 newop->op_sibling = last->op_sibling;
11442 last->op_next = newop; /* listop */
11443 last->op_sibling = NULL;
11444 o->op_sibling = newop; /* ... */
11446 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
11448 /* Ensure pushmark has this flag if padops do */
11449 if (first->op_flags & OPf_MOD && last->op_flags & OPf_MOD) {
11450 o->op_next->op_flags |= OPf_MOD;
11456 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
11457 to carry two labels. For now, take the easier option, and skip
11458 this optimisation if the first NEXTSTATE has a label. */
11459 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
11460 OP *nextop = o->op_next;
11461 while (nextop && nextop->op_type == OP_NULL)
11462 nextop = nextop->op_next;
11464 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
11465 COP *firstcop = (COP *)o;
11466 COP *secondcop = (COP *)nextop;
11467 /* We want the COP pointed to by o (and anything else) to
11468 become the next COP down the line. */
11469 cop_free(firstcop);
11471 firstcop->op_next = secondcop->op_next;
11473 /* Now steal all its pointers, and duplicate the other
11475 firstcop->cop_line = secondcop->cop_line;
11476 #ifdef USE_ITHREADS
11477 firstcop->cop_stashoff = secondcop->cop_stashoff;
11478 firstcop->cop_file = secondcop->cop_file;
11480 firstcop->cop_stash = secondcop->cop_stash;
11481 firstcop->cop_filegv = secondcop->cop_filegv;
11483 firstcop->cop_hints = secondcop->cop_hints;
11484 firstcop->cop_seq = secondcop->cop_seq;
11485 firstcop->cop_warnings = secondcop->cop_warnings;
11486 firstcop->cop_hints_hash = secondcop->cop_hints_hash;
11488 #ifdef USE_ITHREADS
11489 secondcop->cop_stashoff = 0;
11490 secondcop->cop_file = NULL;
11492 secondcop->cop_stash = NULL;
11493 secondcop->cop_filegv = NULL;
11495 secondcop->cop_warnings = NULL;
11496 secondcop->cop_hints_hash = NULL;
11498 /* If we use op_null(), and hence leave an ex-COP, some
11499 warnings are misreported. For example, the compile-time
11500 error in 'use strict; no strict refs;' */
11501 secondcop->op_type = OP_NULL;
11502 secondcop->op_ppaddr = PL_ppaddr[OP_NULL];
11508 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
11509 if (o->op_next->op_private & OPpTARGET_MY) {
11510 if (o->op_flags & OPf_STACKED) /* chained concats */
11511 break; /* ignore_optimization */
11513 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
11514 o->op_targ = o->op_next->op_targ;
11515 o->op_next->op_targ = 0;
11516 o->op_private |= OPpTARGET_MY;
11519 op_null(o->op_next);
11523 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
11524 break; /* Scalar stub must produce undef. List stub is noop */
11528 if (o->op_targ == OP_NEXTSTATE
11529 || o->op_targ == OP_DBSTATE)
11531 PL_curcop = ((COP*)o);
11533 /* XXX: We avoid setting op_seq here to prevent later calls
11534 to rpeep() from mistakenly concluding that optimisation
11535 has already occurred. This doesn't fix the real problem,
11536 though (See 20010220.007). AMS 20010719 */
11537 /* op_seq functionality is now replaced by op_opt */
11545 oldop->op_next = o->op_next;
11553 /* Convert a series of PAD ops for my vars plus support into a
11554 * single padrange op. Basically
11556 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest
11558 * becomes, depending on circumstances, one of
11560 * padrange ----------------------------------> (list) -> rest
11561 * padrange --------------------------------------------> rest
11563 * where all the pad indexes are sequential and of the same type
11565 * We convert the pushmark into a padrange op, then skip
11566 * any other pad ops, and possibly some trailing ops.
11567 * Note that we don't null() the skipped ops, to make it
11568 * easier for Deparse to undo this optimisation (and none of
11569 * the skipped ops are holding any resourses). It also makes
11570 * it easier for find_uninit_var(), as it can just ignore
11571 * padrange, and examine the original pad ops.
11575 OP *followop = NULL; /* the op that will follow the padrange op */
11578 PADOFFSET base = 0; /* init only to stop compiler whining */
11579 U8 gimme = 0; /* init only to stop compiler whining */
11580 bool defav = 0; /* seen (...) = @_ */
11581 bool reuse = 0; /* reuse an existing padrange op */
11583 /* look for a pushmark -> gv[_] -> rv2av */
11589 if ( p->op_type == OP_GV
11590 && (gv = cGVOPx_gv(p))
11591 && GvNAMELEN_get(gv) == 1
11592 && *GvNAME_get(gv) == '_'
11593 && GvSTASH(gv) == PL_defstash
11594 && (rv2av = p->op_next)
11595 && rv2av->op_type == OP_RV2AV
11596 && !(rv2av->op_flags & OPf_REF)
11597 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB))
11598 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST)
11599 && o->op_sibling == rv2av /* these two for Deparse */
11600 && cUNOPx(rv2av)->op_first == p
11602 q = rv2av->op_next;
11603 if (q->op_type == OP_NULL)
11605 if (q->op_type == OP_PUSHMARK) {
11612 /* To allow Deparse to pessimise this, it needs to be able
11613 * to restore the pushmark's original op_next, which it
11614 * will assume to be the same as op_sibling. */
11615 if (o->op_next != o->op_sibling)
11620 /* scan for PAD ops */
11622 for (p = p->op_next; p; p = p->op_next) {
11623 if (p->op_type == OP_NULL)
11626 if (( p->op_type != OP_PADSV
11627 && p->op_type != OP_PADAV
11628 && p->op_type != OP_PADHV
11630 /* any private flag other than INTRO? e.g. STATE */
11631 || (p->op_private & ~OPpLVAL_INTRO)
11635 /* let $a[N] potentially be optimised into AELEMFAST_LEX
11637 if ( p->op_type == OP_PADAV
11639 && p->op_next->op_type == OP_CONST
11640 && p->op_next->op_next
11641 && p->op_next->op_next->op_type == OP_AELEM
11645 /* for 1st padop, note what type it is and the range
11646 * start; for the others, check that it's the same type
11647 * and that the targs are contiguous */
11649 intro = (p->op_private & OPpLVAL_INTRO);
11651 gimme = (p->op_flags & OPf_WANT);
11654 if ((p->op_private & OPpLVAL_INTRO) != intro)
11656 /* Note that you'd normally expect targs to be
11657 * contiguous in my($a,$b,$c), but that's not the case
11658 * when external modules start doing things, e.g.
11659 i* Function::Parameters */
11660 if (p->op_targ != base + count)
11662 assert(p->op_targ == base + count);
11663 /* all the padops should be in the same context */
11664 if (gimme != (p->op_flags & OPf_WANT))
11668 /* for AV, HV, only when we're not flattening */
11669 if ( p->op_type != OP_PADSV
11670 && gimme != OPf_WANT_VOID
11671 && !(p->op_flags & OPf_REF)
11675 if (count >= OPpPADRANGE_COUNTMASK)
11678 /* there's a biggest base we can fit into a
11679 * SAVEt_CLEARPADRANGE in pp_padrange */
11680 if (intro && base >
11681 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)))
11684 /* Success! We've got another valid pad op to optimise away */
11686 followop = p->op_next;
11692 /* pp_padrange in specifically compile-time void context
11693 * skips pushing a mark and lexicals; in all other contexts
11694 * (including unknown till runtime) it pushes a mark and the
11695 * lexicals. We must be very careful then, that the ops we
11696 * optimise away would have exactly the same effect as the
11698 * In particular in void context, we can only optimise to
11699 * a padrange if see see the complete sequence
11700 * pushmark, pad*v, ...., list, nextstate
11701 * which has the net effect of of leaving the stack empty
11702 * (for now we leave the nextstate in the execution chain, for
11703 * its other side-effects).
11706 if (gimme == OPf_WANT_VOID) {
11707 if (OP_TYPE_IS_OR_WAS(followop, OP_LIST)
11708 && gimme == (followop->op_flags & OPf_WANT)
11709 && ( followop->op_next->op_type == OP_NEXTSTATE
11710 || followop->op_next->op_type == OP_DBSTATE))
11712 followop = followop->op_next; /* skip OP_LIST */
11714 /* consolidate two successive my(...);'s */
11717 && oldoldop->op_type == OP_PADRANGE
11718 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID
11719 && (oldoldop->op_private & OPpLVAL_INTRO) == intro
11720 && !(oldoldop->op_flags & OPf_SPECIAL)
11723 assert(oldoldop->op_next == oldop);
11724 assert( oldop->op_type == OP_NEXTSTATE
11725 || oldop->op_type == OP_DBSTATE);
11726 assert(oldop->op_next == o);
11729 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK);
11731 /* Do not assume pad offsets for $c and $d are con-
11736 if ( oldoldop->op_targ + old_count == base
11737 && old_count < OPpPADRANGE_COUNTMASK - count) {
11738 base = oldoldop->op_targ;
11739 count += old_count;
11744 /* if there's any immediately following singleton
11745 * my var's; then swallow them and the associated
11747 * my ($a,$b); my $c; my $d;
11749 * my ($a,$b,$c,$d);
11752 while ( ((p = followop->op_next))
11753 && ( p->op_type == OP_PADSV
11754 || p->op_type == OP_PADAV
11755 || p->op_type == OP_PADHV)
11756 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID
11757 && (p->op_private & OPpLVAL_INTRO) == intro
11758 && !(p->op_private & ~OPpLVAL_INTRO)
11760 && ( p->op_next->op_type == OP_NEXTSTATE
11761 || p->op_next->op_type == OP_DBSTATE)
11762 && count < OPpPADRANGE_COUNTMASK
11763 && base + count == p->op_targ
11766 followop = p->op_next;
11774 assert(oldoldop->op_type == OP_PADRANGE);
11775 oldoldop->op_next = followop;
11776 oldoldop->op_private = (intro | count);
11782 /* Convert the pushmark into a padrange.
11783 * To make Deparse easier, we guarantee that a padrange was
11784 * *always* formerly a pushmark */
11785 assert(o->op_type == OP_PUSHMARK);
11786 o->op_next = followop;
11787 o->op_type = OP_PADRANGE;
11788 o->op_ppaddr = PL_ppaddr[OP_PADRANGE];
11790 /* bit 7: INTRO; bit 6..0: count */
11791 o->op_private = (intro | count);
11792 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL))
11793 | gimme | (defav ? OPf_SPECIAL : 0));
11800 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
11801 OP* const pop = (o->op_type == OP_PADAV) ?
11802 o->op_next : o->op_next->op_next;
11804 if (pop && pop->op_type == OP_CONST &&
11805 ((PL_op = pop->op_next)) &&
11806 pop->op_next->op_type == OP_AELEM &&
11807 !(pop->op_next->op_private &
11808 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
11809 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127)
11812 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
11813 no_bareword_allowed(pop);
11814 if (o->op_type == OP_GV)
11815 op_null(o->op_next);
11816 op_null(pop->op_next);
11818 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
11819 o->op_next = pop->op_next->op_next;
11820 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
11821 o->op_private = (U8)i;
11822 if (o->op_type == OP_GV) {
11825 o->op_type = OP_AELEMFAST;
11828 o->op_type = OP_AELEMFAST_LEX;
11833 if (o->op_next->op_type == OP_RV2SV) {
11834 if (!(o->op_next->op_private & OPpDEREF)) {
11835 op_null(o->op_next);
11836 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
11838 o->op_next = o->op_next->op_next;
11839 o->op_type = OP_GVSV;
11840 o->op_ppaddr = PL_ppaddr[OP_GVSV];
11843 else if (o->op_next->op_type == OP_READLINE
11844 && o->op_next->op_next->op_type == OP_CONCAT
11845 && (o->op_next->op_next->op_flags & OPf_STACKED))
11847 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
11848 o->op_type = OP_RCATLINE;
11849 o->op_flags |= OPf_STACKED;
11850 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
11851 op_null(o->op_next->op_next);
11852 op_null(o->op_next);
11861 #define HV_OR_SCALARHV(op) \
11862 ( (op)->op_type == OP_PADHV || (op)->op_type == OP_RV2HV \
11864 : (op)->op_type == OP_SCALAR && (op)->op_flags & OPf_KIDS \
11865 && ( cUNOPx(op)->op_first->op_type == OP_PADHV \
11866 || cUNOPx(op)->op_first->op_type == OP_RV2HV) \
11867 ? cUNOPx(op)->op_first \
11871 if ((fop = HV_OR_SCALARHV(cUNOP->op_first)))
11872 fop->op_private |= OPpTRUEBOOL;
11878 fop = cLOGOP->op_first;
11879 sop = fop->op_sibling;
11880 while (cLOGOP->op_other->op_type == OP_NULL)
11881 cLOGOP->op_other = cLOGOP->op_other->op_next;
11882 while (o->op_next && ( o->op_type == o->op_next->op_type
11883 || o->op_next->op_type == OP_NULL))
11884 o->op_next = o->op_next->op_next;
11886 /* if we're an OR and our next is a AND in void context, we'll
11887 follow it's op_other on short circuit, same for reverse.
11888 We can't do this with OP_DOR since if it's true, its return
11889 value is the underlying value which must be evaluated
11893 (IS_AND_OP(o) && IS_OR_OP(o->op_next))
11894 || (IS_OR_OP(o) && IS_AND_OP(o->op_next))
11896 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID
11898 o->op_next = ((LOGOP*)o->op_next)->op_other;
11900 DEFER(cLOGOP->op_other);
11903 fop = HV_OR_SCALARHV(fop);
11904 if (sop) sop = HV_OR_SCALARHV(sop);
11909 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) {
11910 while (nop && nop->op_next) {
11911 switch (nop->op_next->op_type) {
11916 lop = nop = nop->op_next;
11919 nop = nop->op_next;
11928 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
11929 || o->op_type == OP_AND )
11930 fop->op_private |= OPpTRUEBOOL;
11931 else if (!(lop->op_flags & OPf_WANT))
11932 fop->op_private |= OPpMAYBE_TRUEBOOL;
11934 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID
11936 sop->op_private |= OPpTRUEBOOL;
11943 if ((fop = HV_OR_SCALARHV(cLOGOP->op_first)))
11944 fop->op_private |= OPpTRUEBOOL;
11945 #undef HV_OR_SCALARHV
11956 while (cLOGOP->op_other->op_type == OP_NULL)
11957 cLOGOP->op_other = cLOGOP->op_other->op_next;
11958 DEFER(cLOGOP->op_other);
11963 while (cLOOP->op_redoop->op_type == OP_NULL)
11964 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
11965 while (cLOOP->op_nextop->op_type == OP_NULL)
11966 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
11967 while (cLOOP->op_lastop->op_type == OP_NULL)
11968 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
11969 /* a while(1) loop doesn't have an op_next that escapes the
11970 * loop, so we have to explicitly follow the op_lastop to
11971 * process the rest of the code */
11972 DEFER(cLOOP->op_lastop);
11976 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY);
11977 DEFER(cLOGOPo->op_other);
11981 assert(!(cPMOP->op_pmflags & PMf_ONCE));
11982 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
11983 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
11984 cPMOP->op_pmstashstartu.op_pmreplstart
11985 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
11986 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
11992 if (o->op_flags & OPf_SPECIAL) {
11993 /* first arg is a code block */
11994 OP * const nullop = cLISTOP->op_first->op_sibling;
11995 OP * kid = cUNOPx(nullop)->op_first;
11997 assert(nullop->op_type == OP_NULL);
11998 assert(kid->op_type == OP_SCOPE
11999 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE));
12000 /* since OP_SORT doesn't have a handy op_other-style
12001 * field that can point directly to the start of the code
12002 * block, store it in the otherwise-unused op_next field
12003 * of the top-level OP_NULL. This will be quicker at
12004 * run-time, and it will also allow us to remove leading
12005 * OP_NULLs by just messing with op_nexts without
12006 * altering the basic op_first/op_sibling layout. */
12007 kid = kLISTOP->op_first;
12009 (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE)
12010 || kid->op_type == OP_STUB
12011 || kid->op_type == OP_ENTER);
12012 nullop->op_next = kLISTOP->op_next;
12013 DEFER(nullop->op_next);
12016 /* check that RHS of sort is a single plain array */
12017 oright = cUNOPo->op_first;
12018 if (!oright || oright->op_type != OP_PUSHMARK)
12021 if (o->op_private & OPpSORT_INPLACE)
12024 /* reverse sort ... can be optimised. */
12025 if (!cUNOPo->op_sibling) {
12026 /* Nothing follows us on the list. */
12027 OP * const reverse = o->op_next;
12029 if (reverse->op_type == OP_REVERSE &&
12030 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
12031 OP * const pushmark = cUNOPx(reverse)->op_first;
12032 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
12033 && (cUNOPx(pushmark)->op_sibling == o)) {
12034 /* reverse -> pushmark -> sort */
12035 o->op_private |= OPpSORT_REVERSE;
12037 pushmark->op_next = oright->op_next;
12047 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
12049 LISTOP *enter, *exlist;
12051 if (o->op_private & OPpSORT_INPLACE)
12054 enter = (LISTOP *) o->op_next;
12057 if (enter->op_type == OP_NULL) {
12058 enter = (LISTOP *) enter->op_next;
12062 /* for $a (...) will have OP_GV then OP_RV2GV here.
12063 for (...) just has an OP_GV. */
12064 if (enter->op_type == OP_GV) {
12065 gvop = (OP *) enter;
12066 enter = (LISTOP *) enter->op_next;
12069 if (enter->op_type == OP_RV2GV) {
12070 enter = (LISTOP *) enter->op_next;
12076 if (enter->op_type != OP_ENTERITER)
12079 iter = enter->op_next;
12080 if (!iter || iter->op_type != OP_ITER)
12083 expushmark = enter->op_first;
12084 if (!expushmark || expushmark->op_type != OP_NULL
12085 || expushmark->op_targ != OP_PUSHMARK)
12088 exlist = (LISTOP *) expushmark->op_sibling;
12089 if (!exlist || exlist->op_type != OP_NULL
12090 || exlist->op_targ != OP_LIST)
12093 if (exlist->op_last != o) {
12094 /* Mmm. Was expecting to point back to this op. */
12097 theirmark = exlist->op_first;
12098 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
12101 if (theirmark->op_sibling != o) {
12102 /* There's something between the mark and the reverse, eg
12103 for (1, reverse (...))
12108 ourmark = ((LISTOP *)o)->op_first;
12109 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
12112 ourlast = ((LISTOP *)o)->op_last;
12113 if (!ourlast || ourlast->op_next != o)
12116 rv2av = ourmark->op_sibling;
12117 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
12118 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
12119 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
12120 /* We're just reversing a single array. */
12121 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
12122 enter->op_flags |= OPf_STACKED;
12125 /* We don't have control over who points to theirmark, so sacrifice
12127 theirmark->op_next = ourmark->op_next;
12128 theirmark->op_flags = ourmark->op_flags;
12129 ourlast->op_next = gvop ? gvop : (OP *) enter;
12132 enter->op_private |= OPpITER_REVERSED;
12133 iter->op_private |= OPpITER_REVERSED;
12140 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
12141 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
12146 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)) {
12148 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
12150 sv = newRV((SV *)PL_compcv);
12154 o->op_type = OP_CONST;
12155 o->op_ppaddr = PL_ppaddr[OP_CONST];
12156 o->op_flags |= OPf_SPECIAL;
12157 cSVOPo->op_sv = sv;
12162 if (OP_GIMME(o,0) == G_VOID) {
12163 OP *right = cBINOP->op_first;
12182 OP *left = right->op_sibling;
12183 if (left->op_type == OP_SUBSTR
12184 && (left->op_private & 7) < 4) {
12186 cBINOP->op_first = left;
12187 right->op_sibling =
12188 cBINOPx(left)->op_first->op_sibling;
12189 cBINOPx(left)->op_first->op_sibling = right;
12190 left->op_private |= OPpSUBSTR_REPL_FIRST;
12192 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
12199 Perl_cpeep_t cpeep =
12200 XopENTRYCUSTOM(o, xop_peep);
12202 cpeep(aTHX_ o, oldop);
12207 /* did we just null the current op? If so, re-process it to handle
12208 * eliding "empty" ops from the chain */
12209 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) {
12222 Perl_peep(pTHX_ OP *o)
12228 =head1 Custom Operators
12230 =for apidoc Ao||custom_op_xop
12231 Return the XOP structure for a given custom op. This macro should be
12232 considered internal to OP_NAME and the other access macros: use them instead.
12233 This macro does call a function. Prior
12234 to 5.19.6, this was implemented as a
12241 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
12247 static const XOP xop_null = { 0, 0, 0, 0, 0 };
12249 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
12250 assert(o->op_type == OP_CUSTOM);
12252 /* This is wrong. It assumes a function pointer can be cast to IV,
12253 * which isn't guaranteed, but this is what the old custom OP code
12254 * did. In principle it should be safer to Copy the bytes of the
12255 * pointer into a PV: since the new interface is hidden behind
12256 * functions, this can be changed later if necessary. */
12257 /* Change custom_op_xop if this ever happens */
12258 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
12261 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
12263 /* assume noone will have just registered a desc */
12264 if (!he && PL_custom_op_names &&
12265 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
12270 /* XXX does all this need to be shared mem? */
12271 Newxz(xop, 1, XOP);
12272 pv = SvPV(HeVAL(he), l);
12273 XopENTRY_set(xop, xop_name, savepvn(pv, l));
12274 if (PL_custom_op_descs &&
12275 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
12277 pv = SvPV(HeVAL(he), l);
12278 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
12280 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
12284 xop = (XOP *)&xop_null;
12286 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
12290 if(field == XOPe_xop_ptr) {
12293 const U32 flags = XopFLAGS(xop);
12294 if(flags & field) {
12296 case XOPe_xop_name:
12297 any.xop_name = xop->xop_name;
12299 case XOPe_xop_desc:
12300 any.xop_desc = xop->xop_desc;
12302 case XOPe_xop_class:
12303 any.xop_class = xop->xop_class;
12305 case XOPe_xop_peep:
12306 any.xop_peep = xop->xop_peep;
12314 case XOPe_xop_name:
12315 any.xop_name = XOPd_xop_name;
12317 case XOPe_xop_desc:
12318 any.xop_desc = XOPd_xop_desc;
12320 case XOPe_xop_class:
12321 any.xop_class = XOPd_xop_class;
12323 case XOPe_xop_peep:
12324 any.xop_peep = XOPd_xop_peep;
12337 =for apidoc Ao||custom_op_register
12338 Register a custom op. See L<perlguts/"Custom Operators">.
12344 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
12348 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
12350 /* see the comment in custom_op_xop */
12351 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
12353 if (!PL_custom_ops)
12354 PL_custom_ops = newHV();
12356 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
12357 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
12361 =head1 Functions in file op.c
12363 =for apidoc core_prototype
12364 This function assigns the prototype of the named core function to C<sv>, or
12365 to a new mortal SV if C<sv> is NULL. It returns the modified C<sv>, or
12366 NULL if the core function has no prototype. C<code> is a code as returned
12367 by C<keyword()>. It must not be equal to 0.
12373 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
12376 int i = 0, n = 0, seen_question = 0, defgv = 0;
12378 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
12379 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
12380 bool nullret = FALSE;
12382 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
12386 if (!sv) sv = sv_newmortal();
12388 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
12390 switch (code < 0 ? -code : code) {
12391 case KEY_and : case KEY_chop: case KEY_chomp:
12392 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
12393 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
12394 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
12395 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
12396 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
12397 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
12398 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
12399 case KEY_x : case KEY_xor :
12400 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
12401 case KEY_glob: retsetpvs("_;", OP_GLOB);
12402 case KEY_keys: retsetpvs("+", OP_KEYS);
12403 case KEY_values: retsetpvs("+", OP_VALUES);
12404 case KEY_each: retsetpvs("+", OP_EACH);
12405 case KEY_push: retsetpvs("+@", OP_PUSH);
12406 case KEY_unshift: retsetpvs("+@", OP_UNSHIFT);
12407 case KEY_pop: retsetpvs(";+", OP_POP);
12408 case KEY_shift: retsetpvs(";+", OP_SHIFT);
12409 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
12411 retsetpvs("+;$$@", OP_SPLICE);
12412 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
12414 case KEY_evalbytes:
12415 name = "entereval"; break;
12423 while (i < MAXO) { /* The slow way. */
12424 if (strEQ(name, PL_op_name[i])
12425 || strEQ(name, PL_op_desc[i]))
12427 if (nullret) { assert(opnum); *opnum = i; return NULL; }
12434 defgv = PL_opargs[i] & OA_DEFGV;
12435 oa = PL_opargs[i] >> OASHIFT;
12437 if (oa & OA_OPTIONAL && !seen_question && (
12438 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
12443 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
12444 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
12445 /* But globs are already references (kinda) */
12446 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
12450 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
12451 && !scalar_mod_type(NULL, i)) {
12456 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
12460 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
12461 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
12462 str[n-1] = '_'; defgv = 0;
12466 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
12468 sv_setpvn(sv, str, n - 1);
12469 if (opnum) *opnum = i;
12474 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
12477 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv);
12480 PERL_ARGS_ASSERT_CORESUB_OP;
12484 return op_append_elem(OP_LINESEQ,
12487 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
12491 case OP_SELECT: /* which represents OP_SSELECT as well */
12496 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
12497 newSVOP(OP_CONST, 0, newSVuv(1))
12499 coresub_op(newSVuv((UV)OP_SSELECT), 0,
12501 coresub_op(coreargssv, 0, OP_SELECT)
12505 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
12507 return op_append_elem(
12510 opnum == OP_WANTARRAY || opnum == OP_RUNCV
12511 ? OPpOFFBYONE << 8 : 0)
12513 case OA_BASEOP_OR_UNOP:
12514 if (opnum == OP_ENTEREVAL) {
12515 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
12516 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
12518 else o = newUNOP(opnum,0,argop);
12519 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
12522 if (is_handle_constructor(o, 1))
12523 argop->op_private |= OPpCOREARGS_DEREF1;
12524 if (scalar_mod_type(NULL, opnum))
12525 argop->op_private |= OPpCOREARGS_SCALARMOD;
12529 o = convert(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
12530 if (is_handle_constructor(o, 2))
12531 argop->op_private |= OPpCOREARGS_DEREF2;
12532 if (opnum == OP_SUBSTR) {
12533 o->op_private |= OPpMAYBE_LVSUB;
12542 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
12543 SV * const *new_const_svp)
12545 const char *hvname;
12546 bool is_const = !!CvCONST(old_cv);
12547 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
12549 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
12551 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
12553 /* They are 2 constant subroutines generated from
12554 the same constant. This probably means that
12555 they are really the "same" proxy subroutine
12556 instantiated in 2 places. Most likely this is
12557 when a constant is exported twice. Don't warn.
12560 (ckWARN(WARN_REDEFINE)
12562 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
12563 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
12564 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
12565 strEQ(hvname, "autouse"))
12569 && ckWARN_d(WARN_REDEFINE)
12570 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
12573 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
12575 ? "Constant subroutine %"SVf" redefined"
12576 : "Subroutine %"SVf" redefined",
12581 =head1 Hook manipulation
12583 These functions provide convenient and thread-safe means of manipulating
12590 =for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p
12592 Puts a C function into the chain of check functions for a specified op
12593 type. This is the preferred way to manipulate the L</PL_check> array.
12594 I<opcode> specifies which type of op is to be affected. I<new_checker>
12595 is a pointer to the C function that is to be added to that opcode's
12596 check chain, and I<old_checker_p> points to the storage location where a
12597 pointer to the next function in the chain will be stored. The value of
12598 I<new_pointer> is written into the L</PL_check> array, while the value
12599 previously stored there is written to I<*old_checker_p>.
12601 The function should be defined like this:
12603 static OP *new_checker(pTHX_ OP *op) { ... }
12605 It is intended to be called in this manner:
12607 new_checker(aTHX_ op)
12609 I<old_checker_p> should be defined like this:
12611 static Perl_check_t old_checker_p;
12613 L</PL_check> is global to an entire process, and a module wishing to
12614 hook op checking may find itself invoked more than once per process,
12615 typically in different threads. To handle that situation, this function
12616 is idempotent. The location I<*old_checker_p> must initially (once
12617 per process) contain a null pointer. A C variable of static duration
12618 (declared at file scope, typically also marked C<static> to give
12619 it internal linkage) will be implicitly initialised appropriately,
12620 if it does not have an explicit initialiser. This function will only
12621 actually modify the check chain if it finds I<*old_checker_p> to be null.
12622 This function is also thread safe on the small scale. It uses appropriate
12623 locking to avoid race conditions in accessing L</PL_check>.
12625 When this function is called, the function referenced by I<new_checker>
12626 must be ready to be called, except for I<*old_checker_p> being unfilled.
12627 In a threading situation, I<new_checker> may be called immediately,
12628 even before this function has returned. I<*old_checker_p> will always
12629 be appropriately set before I<new_checker> is called. If I<new_checker>
12630 decides not to do anything special with an op that it is given (which
12631 is the usual case for most uses of op check hooking), it must chain the
12632 check function referenced by I<*old_checker_p>.
12634 If you want to influence compilation of calls to a specific subroutine,
12635 then use L</cv_set_call_checker> rather than hooking checking of all
12642 Perl_wrap_op_checker(pTHX_ Optype opcode,
12643 Perl_check_t new_checker, Perl_check_t *old_checker_p)
12647 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
12648 if (*old_checker_p) return;
12649 OP_CHECK_MUTEX_LOCK;
12650 if (!*old_checker_p) {
12651 *old_checker_p = PL_check[opcode];
12652 PL_check[opcode] = new_checker;
12654 OP_CHECK_MUTEX_UNLOCK;
12659 /* Efficient sub that returns a constant scalar value. */
12661 const_sv_xsub(pTHX_ CV* cv)
12665 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
12666 PERL_UNUSED_ARG(items);
12676 const_av_xsub(pTHX_ CV* cv)
12680 AV * const av = MUTABLE_AV(XSANY.any_ptr);
12688 if (SvRMAGICAL(av))
12689 Perl_croak(aTHX_ "Magical list constants are not supported");
12690 if (GIMME_V != G_ARRAY) {
12692 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
12695 EXTEND(SP, AvFILLp(av)+1);
12696 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
12697 XSRETURN(AvFILLp(av)+1);
12702 * c-indentation-style: bsd
12703 * c-basic-offset: 4
12704 * indent-tabs-mode: nil
12707 * ex: set ts=8 sts=4 sw=4 et: