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"
106 #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
107 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) CALL_FPTR(PL_opfreehook)(aTHX_ o)
109 #if defined(PL_OP_SLAB_ALLOC)
111 #ifdef PERL_DEBUG_READONLY_OPS
112 # define PERL_SLAB_SIZE 4096
113 # include <sys/mman.h>
116 #ifndef PERL_SLAB_SIZE
117 #define PERL_SLAB_SIZE 2048
121 Perl_Slab_Alloc(pTHX_ size_t sz)
125 * To make incrementing use count easy PL_OpSlab is an I32 *
126 * To make inserting the link to slab PL_OpPtr is I32 **
127 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
128 * Add an overhead for pointer to slab and round up as a number of pointers
130 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
131 if ((PL_OpSpace -= sz) < 0) {
132 #ifdef PERL_DEBUG_READONLY_OPS
133 /* We need to allocate chunk by chunk so that we can control the VM
135 PL_OpPtr = (I32**) mmap(0, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE,
136 MAP_ANON|MAP_PRIVATE, -1, 0);
138 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
139 (unsigned long) PERL_SLAB_SIZE*sizeof(I32*),
141 if(PL_OpPtr == MAP_FAILED) {
142 perror("mmap failed");
147 PL_OpPtr = (I32 **) PerlMemShared_calloc(PERL_SLAB_SIZE,sizeof(I32*));
152 /* We reserve the 0'th I32 sized chunk as a use count */
153 PL_OpSlab = (I32 *) PL_OpPtr;
154 /* Reduce size by the use count word, and by the size we need.
155 * Latter is to mimic the '-=' in the if() above
157 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
158 /* Allocation pointer starts at the top.
159 Theory: because we build leaves before trunk allocating at end
160 means that at run time access is cache friendly upward
162 PL_OpPtr += PERL_SLAB_SIZE;
164 #ifdef PERL_DEBUG_READONLY_OPS
165 /* We remember this slab. */
166 /* This implementation isn't efficient, but it is simple. */
167 PL_slabs = (I32**) realloc(PL_slabs, sizeof(I32**) * (PL_slab_count + 1));
168 PL_slabs[PL_slab_count++] = PL_OpSlab;
169 DEBUG_m(PerlIO_printf(Perl_debug_log, "Allocate %p\n", PL_OpSlab));
172 assert( PL_OpSpace >= 0 );
173 /* Move the allocation pointer down */
175 assert( PL_OpPtr > (I32 **) PL_OpSlab );
176 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
177 (*PL_OpSlab)++; /* Increment use count of slab */
178 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
179 assert( *PL_OpSlab > 0 );
180 return (void *)(PL_OpPtr + 1);
183 #ifdef PERL_DEBUG_READONLY_OPS
185 Perl_pending_Slabs_to_ro(pTHX) {
186 /* Turn all the allocated op slabs read only. */
187 U32 count = PL_slab_count;
188 I32 **const slabs = PL_slabs;
190 /* Reset the array of pending OP slabs, as we're about to turn this lot
191 read only. Also, do it ahead of the loop in case the warn triggers,
192 and a warn handler has an eval */
197 /* Force a new slab for any further allocation. */
201 void *const start = slabs[count];
202 const size_t size = PERL_SLAB_SIZE* sizeof(I32*);
203 if(mprotect(start, size, PROT_READ)) {
204 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d",
205 start, (unsigned long) size, errno);
213 S_Slab_to_rw(pTHX_ void *op)
215 I32 * const * const ptr = (I32 **) op;
216 I32 * const slab = ptr[-1];
218 PERL_ARGS_ASSERT_SLAB_TO_RW;
220 assert( ptr-1 > (I32 **) slab );
221 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
223 if(mprotect(slab, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE)) {
224 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d",
225 slab, (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), errno);
230 Perl_op_refcnt_inc(pTHX_ OP *o)
241 Perl_op_refcnt_dec(pTHX_ OP *o)
243 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
248 # define Slab_to_rw(op)
252 Perl_Slab_Free(pTHX_ void *op)
254 I32 * const * const ptr = (I32 **) op;
255 I32 * const slab = ptr[-1];
256 PERL_ARGS_ASSERT_SLAB_FREE;
257 assert( ptr-1 > (I32 **) slab );
258 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
261 if (--(*slab) == 0) {
263 # define PerlMemShared PerlMem
266 #ifdef PERL_DEBUG_READONLY_OPS
267 U32 count = PL_slab_count;
268 /* Need to remove this slab from our list of slabs */
271 if (PL_slabs[count] == slab) {
273 /* Found it. Move the entry at the end to overwrite it. */
274 DEBUG_m(PerlIO_printf(Perl_debug_log,
275 "Deallocate %p by moving %p from %lu to %lu\n",
277 PL_slabs[PL_slab_count - 1],
278 PL_slab_count, count));
279 PL_slabs[count] = PL_slabs[--PL_slab_count];
280 /* Could realloc smaller at this point, but probably not
282 if(munmap(slab, PERL_SLAB_SIZE*sizeof(I32*))) {
283 perror("munmap failed");
291 PerlMemShared_free(slab);
293 if (slab == PL_OpSlab) {
300 * In the following definition, the ", (OP*)0" is just to make the compiler
301 * think the expression is of the right type: croak actually does a Siglongjmp.
303 #define CHECKOP(type,o) \
304 ((PL_op_mask && PL_op_mask[type]) \
305 ? ( op_free((OP*)o), \
306 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
308 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
310 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
313 S_gv_ename(pTHX_ GV *gv)
315 SV* const tmpsv = sv_newmortal();
317 PERL_ARGS_ASSERT_GV_ENAME;
319 gv_efullname3(tmpsv, gv, NULL);
320 return SvPV_nolen_const(tmpsv);
324 S_no_fh_allowed(pTHX_ OP *o)
326 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
328 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
334 S_too_few_arguments(pTHX_ OP *o, const char *name)
336 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS;
338 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
343 S_too_many_arguments(pTHX_ OP *o, const char *name)
345 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS;
347 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
352 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
354 PERL_ARGS_ASSERT_BAD_TYPE;
356 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
357 (int)n, name, t, OP_DESC(kid)));
361 S_no_bareword_allowed(pTHX_ const OP *o)
363 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
366 return; /* various ok barewords are hidden in extra OP_NULL */
367 qerror(Perl_mess(aTHX_
368 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
372 /* "register" allocation */
375 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
379 const bool is_our = (PL_parser->in_my == KEY_our);
381 PERL_ARGS_ASSERT_ALLOCMY;
384 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
387 /* Until we're using the length for real, cross check that we're being
389 assert(strlen(name) == len);
391 /* complain about "my $<special_var>" etc etc */
395 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
396 (name[1] == '_' && (*name == '$' || len > 2))))
398 /* name[2] is true if strlen(name) > 2 */
399 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
400 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
401 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
402 PL_parser->in_my == KEY_state ? "state" : "my"));
404 yyerror(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
405 PL_parser->in_my == KEY_state ? "state" : "my"));
409 /* allocate a spare slot and store the name in that slot */
411 off = pad_add_name(name, len,
412 is_our ? padadd_OUR :
413 PL_parser->in_my == KEY_state ? padadd_STATE : 0,
414 PL_parser->in_my_stash,
416 /* $_ is always in main::, even with our */
417 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
421 /* anon sub prototypes contains state vars should always be cloned,
422 * otherwise the state var would be shared between anon subs */
424 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
425 CvCLONE_on(PL_compcv);
430 /* free the body of an op without examining its contents.
431 * Always use this rather than FreeOp directly */
434 S_op_destroy(pTHX_ OP *o)
436 if (o->op_latefree) {
444 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b)
446 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a)
452 Perl_op_free(pTHX_ OP *o)
459 if (o->op_latefreed) {
466 if (o->op_private & OPpREFCOUNTED) {
477 refcnt = OpREFCNT_dec(o);
480 /* Need to find and remove any pattern match ops from the list
481 we maintain for reset(). */
482 find_and_forget_pmops(o);
492 /* Call the op_free hook if it has been set. Do it now so that it's called
493 * at the right time for refcounted ops, but still before all of the kids
497 if (o->op_flags & OPf_KIDS) {
498 register OP *kid, *nextkid;
499 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
500 nextkid = kid->op_sibling; /* Get before next freeing kid */
505 #ifdef PERL_DEBUG_READONLY_OPS
509 /* COP* is not cleared by op_clear() so that we may track line
510 * numbers etc even after null() */
511 if (type == OP_NEXTSTATE || type == OP_DBSTATE
512 || (type == OP_NULL /* the COP might have been null'ed */
513 && ((OPCODE)o->op_targ == OP_NEXTSTATE
514 || (OPCODE)o->op_targ == OP_DBSTATE))) {
519 type = (OPCODE)o->op_targ;
522 if (o->op_latefree) {
528 #ifdef DEBUG_LEAKING_SCALARS
535 Perl_op_clear(pTHX_ OP *o)
540 PERL_ARGS_ASSERT_OP_CLEAR;
543 /* if (o->op_madprop && o->op_madprop->mad_next)
545 /* FIXME for MAD - if I uncomment these two lines t/op/pack.t fails with
546 "modification of a read only value" for a reason I can't fathom why.
547 It's the "" stringification of $_, where $_ was set to '' in a foreach
548 loop, but it defies simplification into a small test case.
549 However, commenting them out has caused ext/List/Util/t/weak.t to fail
552 mad_free(o->op_madprop);
558 switch (o->op_type) {
559 case OP_NULL: /* Was holding old type, if any. */
560 if (PL_madskills && o->op_targ != OP_NULL) {
561 o->op_type = (Optype)o->op_targ;
565 case OP_ENTEREVAL: /* Was holding hints. */
569 if (!(o->op_flags & OPf_REF)
570 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
576 if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
577 /* not an OP_PADAV replacement */
578 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
583 /* It's possible during global destruction that the GV is freed
584 before the optree. Whilst the SvREFCNT_inc is happy to bump from
585 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
586 will trigger an assertion failure, because the entry to sv_clear
587 checks that the scalar is not already freed. A check of for
588 !SvIS_FREED(gv) turns out to be invalid, because during global
589 destruction the reference count can be forced down to zero
590 (with SVf_BREAK set). In which case raising to 1 and then
591 dropping to 0 triggers cleanup before it should happen. I
592 *think* that this might actually be a general, systematic,
593 weakness of the whole idea of SVf_BREAK, in that code *is*
594 allowed to raise and lower references during global destruction,
595 so any *valid* code that happens to do this during global
596 destruction might well trigger premature cleanup. */
597 bool still_valid = gv && SvREFCNT(gv);
600 SvREFCNT_inc_simple_void(gv);
602 if (cPADOPo->op_padix > 0) {
603 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
604 * may still exist on the pad */
605 pad_swipe(cPADOPo->op_padix, TRUE);
606 cPADOPo->op_padix = 0;
609 SvREFCNT_dec(cSVOPo->op_sv);
610 cSVOPo->op_sv = NULL;
613 int try_downgrade = SvREFCNT(gv) == 2;
616 gv_try_downgrade(gv);
620 case OP_METHOD_NAMED:
623 SvREFCNT_dec(cSVOPo->op_sv);
624 cSVOPo->op_sv = NULL;
627 Even if op_clear does a pad_free for the target of the op,
628 pad_free doesn't actually remove the sv that exists in the pad;
629 instead it lives on. This results in that it could be reused as
630 a target later on when the pad was reallocated.
633 pad_swipe(o->op_targ,1);
642 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
646 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
648 if (cPADOPo->op_padix > 0) {
649 pad_swipe(cPADOPo->op_padix, TRUE);
650 cPADOPo->op_padix = 0;
653 SvREFCNT_dec(cSVOPo->op_sv);
654 cSVOPo->op_sv = NULL;
658 PerlMemShared_free(cPVOPo->op_pv);
659 cPVOPo->op_pv = NULL;
663 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
667 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
668 /* No GvIN_PAD_off here, because other references may still
669 * exist on the pad */
670 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
673 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
679 forget_pmop(cPMOPo, 1);
680 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
681 /* we use the same protection as the "SAFE" version of the PM_ macros
682 * here since sv_clean_all might release some PMOPs
683 * after PL_regex_padav has been cleared
684 * and the clearing of PL_regex_padav needs to
685 * happen before sv_clean_all
688 if(PL_regex_pad) { /* We could be in destruction */
689 const IV offset = (cPMOPo)->op_pmoffset;
690 ReREFCNT_dec(PM_GETRE(cPMOPo));
691 PL_regex_pad[offset] = &PL_sv_undef;
692 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
696 ReREFCNT_dec(PM_GETRE(cPMOPo));
697 PM_SETRE(cPMOPo, NULL);
703 if (o->op_targ > 0) {
704 pad_free(o->op_targ);
710 S_cop_free(pTHX_ COP* cop)
712 PERL_ARGS_ASSERT_COP_FREE;
716 if (! specialWARN(cop->cop_warnings))
717 PerlMemShared_free(cop->cop_warnings);
718 Perl_refcounted_he_free(aTHX_ cop->cop_hints_hash);
722 S_forget_pmop(pTHX_ PMOP *const o
728 HV * const pmstash = PmopSTASH(o);
730 PERL_ARGS_ASSERT_FORGET_PMOP;
732 if (pmstash && !SvIS_FREED(pmstash)) {
733 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
735 PMOP **const array = (PMOP**) mg->mg_ptr;
736 U32 count = mg->mg_len / sizeof(PMOP**);
741 /* Found it. Move the entry at the end to overwrite it. */
742 array[i] = array[--count];
743 mg->mg_len = count * sizeof(PMOP**);
744 /* Could realloc smaller at this point always, but probably
745 not worth it. Probably worth free()ing if we're the
748 Safefree(mg->mg_ptr);
765 S_find_and_forget_pmops(pTHX_ OP *o)
767 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
769 if (o->op_flags & OPf_KIDS) {
770 OP *kid = cUNOPo->op_first;
772 switch (kid->op_type) {
777 forget_pmop((PMOP*)kid, 0);
779 find_and_forget_pmops(kid);
780 kid = kid->op_sibling;
786 Perl_op_null(pTHX_ OP *o)
790 PERL_ARGS_ASSERT_OP_NULL;
792 if (o->op_type == OP_NULL)
796 o->op_targ = o->op_type;
797 o->op_type = OP_NULL;
798 o->op_ppaddr = PL_ppaddr[OP_NULL];
802 Perl_op_refcnt_lock(pTHX)
810 Perl_op_refcnt_unlock(pTHX)
817 /* Contextualizers */
819 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
822 S_linklist(pTHX_ OP *o)
826 PERL_ARGS_ASSERT_LINKLIST;
831 /* establish postfix order */
832 first = cUNOPo->op_first;
835 o->op_next = LINKLIST(first);
838 if (kid->op_sibling) {
839 kid->op_next = LINKLIST(kid->op_sibling);
840 kid = kid->op_sibling;
854 S_scalarkids(pTHX_ OP *o)
856 if (o && o->op_flags & OPf_KIDS) {
858 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
865 S_scalarboolean(pTHX_ OP *o)
869 PERL_ARGS_ASSERT_SCALARBOOLEAN;
871 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
872 if (ckWARN(WARN_SYNTAX)) {
873 const line_t oldline = CopLINE(PL_curcop);
875 if (PL_parser && PL_parser->copline != NOLINE)
876 CopLINE_set(PL_curcop, PL_parser->copline);
877 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
878 CopLINE_set(PL_curcop, oldline);
885 Perl_scalar(pTHX_ OP *o)
890 /* assumes no premature commitment */
891 if (!o || (PL_parser && PL_parser->error_count)
892 || (o->op_flags & OPf_WANT)
893 || o->op_type == OP_RETURN)
898 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
900 switch (o->op_type) {
902 scalar(cBINOPo->op_first);
907 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
917 if (o->op_flags & OPf_KIDS) {
918 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
924 kid = cLISTOPo->op_first;
926 kid = kid->op_sibling;
929 OP *sib = kid->op_sibling;
930 if (sib && kid->op_type != OP_LEAVEWHEN) {
931 if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) {
941 PL_curcop = &PL_compiling;
946 kid = cLISTOPo->op_first;
949 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
956 Perl_scalarvoid(pTHX_ OP *o)
960 const char* useless = NULL;
964 PERL_ARGS_ASSERT_SCALARVOID;
966 /* trailing mad null ops don't count as "there" for void processing */
968 o->op_type != OP_NULL &&
970 o->op_sibling->op_type == OP_NULL)
973 for (sib = o->op_sibling;
974 sib && sib->op_type == OP_NULL;
975 sib = sib->op_sibling) ;
981 if (o->op_type == OP_NEXTSTATE
982 || o->op_type == OP_DBSTATE
983 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
984 || o->op_targ == OP_DBSTATE)))
985 PL_curcop = (COP*)o; /* for warning below */
987 /* assumes no premature commitment */
988 want = o->op_flags & OPf_WANT;
989 if ((want && want != OPf_WANT_SCALAR)
990 || (PL_parser && PL_parser->error_count)
991 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
996 if ((o->op_private & OPpTARGET_MY)
997 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
999 return scalar(o); /* As if inside SASSIGN */
1002 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1004 switch (o->op_type) {
1006 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1010 if (o->op_flags & OPf_STACKED)
1014 if (o->op_private == 4)
1057 case OP_GETSOCKNAME:
1058 case OP_GETPEERNAME:
1063 case OP_GETPRIORITY:
1087 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1088 /* Otherwise it's "Useless use of grep iterator" */
1089 useless = OP_DESC(o);
1093 kid = cLISTOPo->op_first;
1094 if (kid && kid->op_type == OP_PUSHRE
1096 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1098 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1100 useless = OP_DESC(o);
1104 kid = cUNOPo->op_first;
1105 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1106 kid->op_type != OP_TRANS) {
1109 useless = "negative pattern binding (!~)";
1116 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1117 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1118 useless = "a variable";
1123 if (cSVOPo->op_private & OPpCONST_STRICT)
1124 no_bareword_allowed(o);
1126 if (ckWARN(WARN_VOID)) {
1128 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1129 "a constant (%"SVf")", sv));
1130 useless = SvPV_nolen(msv);
1133 useless = "a constant (undef)";
1134 if (o->op_private & OPpCONST_ARYBASE)
1136 /* don't warn on optimised away booleans, eg
1137 * use constant Foo, 5; Foo || print; */
1138 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1140 /* the constants 0 and 1 are permitted as they are
1141 conventionally used as dummies in constructs like
1142 1 while some_condition_with_side_effects; */
1143 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1145 else if (SvPOK(sv)) {
1146 /* perl4's way of mixing documentation and code
1147 (before the invention of POD) was based on a
1148 trick to mix nroff and perl code. The trick was
1149 built upon these three nroff macros being used in
1150 void context. The pink camel has the details in
1151 the script wrapman near page 319. */
1152 const char * const maybe_macro = SvPVX_const(sv);
1153 if (strnEQ(maybe_macro, "di", 2) ||
1154 strnEQ(maybe_macro, "ds", 2) ||
1155 strnEQ(maybe_macro, "ig", 2))
1160 op_null(o); /* don't execute or even remember it */
1164 o->op_type = OP_PREINC; /* pre-increment is faster */
1165 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1169 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1170 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1174 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1175 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1179 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1180 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1185 kid = cLOGOPo->op_first;
1186 if (kid->op_type == OP_NOT
1187 && (kid->op_flags & OPf_KIDS)
1189 if (o->op_type == OP_AND) {
1191 o->op_ppaddr = PL_ppaddr[OP_OR];
1193 o->op_type = OP_AND;
1194 o->op_ppaddr = PL_ppaddr[OP_AND];
1203 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1208 if (o->op_flags & OPf_STACKED)
1215 if (!(o->op_flags & OPf_KIDS))
1226 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1236 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
1241 S_listkids(pTHX_ OP *o)
1243 if (o && o->op_flags & OPf_KIDS) {
1245 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1252 Perl_list(pTHX_ OP *o)
1257 /* assumes no premature commitment */
1258 if (!o || (o->op_flags & OPf_WANT)
1259 || (PL_parser && PL_parser->error_count)
1260 || o->op_type == OP_RETURN)
1265 if ((o->op_private & OPpTARGET_MY)
1266 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1268 return o; /* As if inside SASSIGN */
1271 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1273 switch (o->op_type) {
1276 list(cBINOPo->op_first);
1281 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1289 if (!(o->op_flags & OPf_KIDS))
1291 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1292 list(cBINOPo->op_first);
1293 return gen_constant_list(o);
1300 kid = cLISTOPo->op_first;
1302 kid = kid->op_sibling;
1305 OP *sib = kid->op_sibling;
1306 if (sib && kid->op_type != OP_LEAVEWHEN) {
1307 if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) {
1317 PL_curcop = &PL_compiling;
1321 kid = cLISTOPo->op_first;
1328 S_scalarseq(pTHX_ OP *o)
1332 const OPCODE type = o->op_type;
1334 if (type == OP_LINESEQ || type == OP_SCOPE ||
1335 type == OP_LEAVE || type == OP_LEAVETRY)
1338 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1339 if (kid->op_sibling) {
1343 PL_curcop = &PL_compiling;
1345 o->op_flags &= ~OPf_PARENS;
1346 if (PL_hints & HINT_BLOCK_SCOPE)
1347 o->op_flags |= OPf_PARENS;
1350 o = newOP(OP_STUB, 0);
1355 S_modkids(pTHX_ OP *o, I32 type)
1357 if (o && o->op_flags & OPf_KIDS) {
1359 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1365 /* Propagate lvalue ("modifiable") context to an op and its children.
1366 * 'type' represents the context type, roughly based on the type of op that
1367 * would do the modifying, although local() is represented by OP_NULL.
1368 * It's responsible for detecting things that can't be modified, flag
1369 * things that need to behave specially in an lvalue context (e.g., "$$x = 5"
1370 * might have to vivify a reference in $x), and so on.
1372 * For example, "$a+1 = 2" would cause mod() to be called with o being
1373 * OP_ADD and type being OP_SASSIGN, and would output an error.
1377 Perl_mod(pTHX_ OP *o, I32 type)
1381 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1384 if (!o || (PL_parser && PL_parser->error_count))
1387 if ((o->op_private & OPpTARGET_MY)
1388 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1393 switch (o->op_type) {
1399 if (!(o->op_private & OPpCONST_ARYBASE))
1402 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1403 CopARYBASE_set(&PL_compiling,
1404 (I32)SvIV(cSVOPx(PL_eval_start)->op_sv));
1408 SAVECOPARYBASE(&PL_compiling);
1409 CopARYBASE_set(&PL_compiling, 0);
1411 else if (type == OP_REFGEN)
1414 Perl_croak(aTHX_ "That use of $[ is unsupported");
1417 if ((o->op_flags & OPf_PARENS) || PL_madskills)
1421 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1422 !(o->op_flags & OPf_STACKED)) {
1423 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1424 /* The default is to set op_private to the number of children,
1425 which for a UNOP such as RV2CV is always 1. And w're using
1426 the bit for a flag in RV2CV, so we need it clear. */
1427 o->op_private &= ~1;
1428 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1429 assert(cUNOPo->op_first->op_type == OP_NULL);
1430 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1433 else if (o->op_private & OPpENTERSUB_NOMOD)
1435 else { /* lvalue subroutine call */
1436 o->op_private |= OPpLVAL_INTRO;
1437 PL_modcount = RETURN_UNLIMITED_NUMBER;
1438 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1439 /* Backward compatibility mode: */
1440 o->op_private |= OPpENTERSUB_INARGS;
1443 else { /* Compile-time error message: */
1444 OP *kid = cUNOPo->op_first;
1448 if (kid->op_type != OP_PUSHMARK) {
1449 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1451 "panic: unexpected lvalue entersub "
1452 "args: type/targ %ld:%"UVuf,
1453 (long)kid->op_type, (UV)kid->op_targ);
1454 kid = kLISTOP->op_first;
1456 while (kid->op_sibling)
1457 kid = kid->op_sibling;
1458 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1460 if (kid->op_type == OP_METHOD_NAMED
1461 || kid->op_type == OP_METHOD)
1465 NewOp(1101, newop, 1, UNOP);
1466 newop->op_type = OP_RV2CV;
1467 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1468 newop->op_first = NULL;
1469 newop->op_next = (OP*)newop;
1470 kid->op_sibling = (OP*)newop;
1471 newop->op_private |= OPpLVAL_INTRO;
1472 newop->op_private &= ~1;
1476 if (kid->op_type != OP_RV2CV)
1478 "panic: unexpected lvalue entersub "
1479 "entry via type/targ %ld:%"UVuf,
1480 (long)kid->op_type, (UV)kid->op_targ);
1481 kid->op_private |= OPpLVAL_INTRO;
1482 break; /* Postpone until runtime */
1486 kid = kUNOP->op_first;
1487 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1488 kid = kUNOP->op_first;
1489 if (kid->op_type == OP_NULL)
1491 "Unexpected constant lvalue entersub "
1492 "entry via type/targ %ld:%"UVuf,
1493 (long)kid->op_type, (UV)kid->op_targ);
1494 if (kid->op_type != OP_GV) {
1495 /* Restore RV2CV to check lvalueness */
1497 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1498 okid->op_next = kid->op_next;
1499 kid->op_next = okid;
1502 okid->op_next = NULL;
1503 okid->op_type = OP_RV2CV;
1505 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1506 okid->op_private |= OPpLVAL_INTRO;
1507 okid->op_private &= ~1;
1511 cv = GvCV(kGVOP_gv);
1521 /* grep, foreach, subcalls, refgen */
1522 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1524 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1525 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1527 : (o->op_type == OP_ENTERSUB
1528 ? "non-lvalue subroutine call"
1530 type ? PL_op_desc[type] : "local"));
1544 case OP_RIGHT_SHIFT:
1553 if (!(o->op_flags & OPf_STACKED))
1560 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1566 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1567 PL_modcount = RETURN_UNLIMITED_NUMBER;
1568 return o; /* Treat \(@foo) like ordinary list. */
1572 if (scalar_mod_type(o, type))
1574 ref(cUNOPo->op_first, o->op_type);
1578 if (type == OP_LEAVESUBLV)
1579 o->op_private |= OPpMAYBE_LVSUB;
1585 PL_modcount = RETURN_UNLIMITED_NUMBER;
1588 PL_hints |= HINT_BLOCK_SCOPE;
1589 if (type == OP_LEAVESUBLV)
1590 o->op_private |= OPpMAYBE_LVSUB;
1594 ref(cUNOPo->op_first, o->op_type);
1598 PL_hints |= HINT_BLOCK_SCOPE;
1613 PL_modcount = RETURN_UNLIMITED_NUMBER;
1614 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1615 return o; /* Treat \(@foo) like ordinary list. */
1616 if (scalar_mod_type(o, type))
1618 if (type == OP_LEAVESUBLV)
1619 o->op_private |= OPpMAYBE_LVSUB;
1623 if (!type) /* local() */
1624 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1625 PAD_COMPNAME_PV(o->op_targ));
1633 if (type != OP_SASSIGN)
1637 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1642 if (type == OP_LEAVESUBLV)
1643 o->op_private |= OPpMAYBE_LVSUB;
1645 pad_free(o->op_targ);
1646 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1647 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1648 if (o->op_flags & OPf_KIDS)
1649 mod(cBINOPo->op_first->op_sibling, type);
1654 ref(cBINOPo->op_first, o->op_type);
1655 if (type == OP_ENTERSUB &&
1656 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1657 o->op_private |= OPpLVAL_DEFER;
1658 if (type == OP_LEAVESUBLV)
1659 o->op_private |= OPpMAYBE_LVSUB;
1669 if (o->op_flags & OPf_KIDS)
1670 mod(cLISTOPo->op_last, type);
1675 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1677 else if (!(o->op_flags & OPf_KIDS))
1679 if (o->op_targ != OP_LIST) {
1680 mod(cBINOPo->op_first, type);
1686 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1691 if (type != OP_LEAVESUBLV)
1693 break; /* mod()ing was handled by ck_return() */
1696 /* [20011101.069] File test operators interpret OPf_REF to mean that
1697 their argument is a filehandle; thus \stat(".") should not set
1699 if (type == OP_REFGEN &&
1700 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1703 if (type != OP_LEAVESUBLV)
1704 o->op_flags |= OPf_MOD;
1706 if (type == OP_AASSIGN || type == OP_SASSIGN)
1707 o->op_flags |= OPf_SPECIAL|OPf_REF;
1708 else if (!type) { /* local() */
1711 o->op_private |= OPpLVAL_INTRO;
1712 o->op_flags &= ~OPf_SPECIAL;
1713 PL_hints |= HINT_BLOCK_SCOPE;
1718 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
1719 "Useless localization of %s", OP_DESC(o));
1722 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1723 && type != OP_LEAVESUBLV)
1724 o->op_flags |= OPf_REF;
1729 S_scalar_mod_type(const OP *o, I32 type)
1731 PERL_ARGS_ASSERT_SCALAR_MOD_TYPE;
1735 if (o->op_type == OP_RV2GV)
1759 case OP_RIGHT_SHIFT:
1779 S_is_handle_constructor(const OP *o, I32 numargs)
1781 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
1783 switch (o->op_type) {
1791 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1804 S_refkids(pTHX_ OP *o, I32 type)
1806 if (o && o->op_flags & OPf_KIDS) {
1808 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1815 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
1820 PERL_ARGS_ASSERT_DOREF;
1822 if (!o || (PL_parser && PL_parser->error_count))
1825 switch (o->op_type) {
1827 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1828 !(o->op_flags & OPf_STACKED)) {
1829 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1830 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1831 assert(cUNOPo->op_first->op_type == OP_NULL);
1832 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1833 o->op_flags |= OPf_SPECIAL;
1834 o->op_private &= ~1;
1839 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1840 doref(kid, type, set_op_ref);
1843 if (type == OP_DEFINED)
1844 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1845 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1848 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1849 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1850 : type == OP_RV2HV ? OPpDEREF_HV
1852 o->op_flags |= OPf_MOD;
1859 o->op_flags |= OPf_REF;
1862 if (type == OP_DEFINED)
1863 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1864 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1870 o->op_flags |= OPf_REF;
1875 if (!(o->op_flags & OPf_KIDS))
1877 doref(cBINOPo->op_first, type, set_op_ref);
1881 doref(cBINOPo->op_first, o->op_type, set_op_ref);
1882 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1883 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1884 : type == OP_RV2HV ? OPpDEREF_HV
1886 o->op_flags |= OPf_MOD;
1896 if (!(o->op_flags & OPf_KIDS))
1898 doref(cLISTOPo->op_last, type, set_op_ref);
1908 S_dup_attrlist(pTHX_ OP *o)
1913 PERL_ARGS_ASSERT_DUP_ATTRLIST;
1915 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1916 * where the first kid is OP_PUSHMARK and the remaining ones
1917 * are OP_CONST. We need to push the OP_CONST values.
1919 if (o->op_type == OP_CONST)
1920 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
1922 else if (o->op_type == OP_NULL)
1926 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1928 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1929 if (o->op_type == OP_CONST)
1930 rop = append_elem(OP_LIST, rop,
1931 newSVOP(OP_CONST, o->op_flags,
1932 SvREFCNT_inc_NN(cSVOPo->op_sv)));
1939 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1944 PERL_ARGS_ASSERT_APPLY_ATTRS;
1946 /* fake up C<use attributes $pkg,$rv,@attrs> */
1947 ENTER; /* need to protect against side-effects of 'use' */
1948 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
1950 #define ATTRSMODULE "attributes"
1951 #define ATTRSMODULE_PM "attributes.pm"
1954 /* Don't force the C<use> if we don't need it. */
1955 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
1956 if (svp && *svp != &PL_sv_undef)
1957 NOOP; /* already in %INC */
1959 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1960 newSVpvs(ATTRSMODULE), NULL);
1963 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1964 newSVpvs(ATTRSMODULE),
1966 prepend_elem(OP_LIST,
1967 newSVOP(OP_CONST, 0, stashsv),
1968 prepend_elem(OP_LIST,
1969 newSVOP(OP_CONST, 0,
1971 dup_attrlist(attrs))));
1977 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1980 OP *pack, *imop, *arg;
1983 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
1988 assert(target->op_type == OP_PADSV ||
1989 target->op_type == OP_PADHV ||
1990 target->op_type == OP_PADAV);
1992 /* Ensure that attributes.pm is loaded. */
1993 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1995 /* Need package name for method call. */
1996 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
1998 /* Build up the real arg-list. */
1999 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2001 arg = newOP(OP_PADSV, 0);
2002 arg->op_targ = target->op_targ;
2003 arg = prepend_elem(OP_LIST,
2004 newSVOP(OP_CONST, 0, stashsv),
2005 prepend_elem(OP_LIST,
2006 newUNOP(OP_REFGEN, 0,
2007 mod(arg, OP_REFGEN)),
2008 dup_attrlist(attrs)));
2010 /* Fake up a method call to import */
2011 meth = newSVpvs_share("import");
2012 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2013 append_elem(OP_LIST,
2014 prepend_elem(OP_LIST, pack, list(arg)),
2015 newSVOP(OP_METHOD_NAMED, 0, meth)));
2016 imop->op_private |= OPpENTERSUB_NOMOD;
2018 /* Combine the ops. */
2019 *imopsp = append_elem(OP_LIST, *imopsp, imop);
2023 =notfor apidoc apply_attrs_string
2025 Attempts to apply a list of attributes specified by the C<attrstr> and
2026 C<len> arguments to the subroutine identified by the C<cv> argument which
2027 is expected to be associated with the package identified by the C<stashpv>
2028 argument (see L<attributes>). It gets this wrong, though, in that it
2029 does not correctly identify the boundaries of the individual attribute
2030 specifications within C<attrstr>. This is not really intended for the
2031 public API, but has to be listed here for systems such as AIX which
2032 need an explicit export list for symbols. (It's called from XS code
2033 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2034 to respect attribute syntax properly would be welcome.
2040 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2041 const char *attrstr, STRLEN len)
2045 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2048 len = strlen(attrstr);
2052 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2054 const char * const sstr = attrstr;
2055 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2056 attrs = append_elem(OP_LIST, attrs,
2057 newSVOP(OP_CONST, 0,
2058 newSVpvn(sstr, attrstr-sstr)));
2062 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2063 newSVpvs(ATTRSMODULE),
2064 NULL, prepend_elem(OP_LIST,
2065 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2066 prepend_elem(OP_LIST,
2067 newSVOP(OP_CONST, 0,
2068 newRV(MUTABLE_SV(cv))),
2073 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2078 PERL_ARGS_ASSERT_MY_KID;
2080 if (!o || (PL_parser && PL_parser->error_count))
2084 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2085 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2089 if (type == OP_LIST) {
2091 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2092 my_kid(kid, attrs, imopsp);
2093 } else if (type == OP_UNDEF
2099 } else if (type == OP_RV2SV || /* "our" declaration */
2101 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2102 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2103 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2105 PL_parser->in_my == KEY_our
2107 : PL_parser->in_my == KEY_state ? "state" : "my"));
2109 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2110 PL_parser->in_my = FALSE;
2111 PL_parser->in_my_stash = NULL;
2112 apply_attrs(GvSTASH(gv),
2113 (type == OP_RV2SV ? GvSV(gv) :
2114 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2115 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2118 o->op_private |= OPpOUR_INTRO;
2121 else if (type != OP_PADSV &&
2124 type != OP_PUSHMARK)
2126 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2128 PL_parser->in_my == KEY_our
2130 : PL_parser->in_my == KEY_state ? "state" : "my"));
2133 else if (attrs && type != OP_PUSHMARK) {
2136 PL_parser->in_my = FALSE;
2137 PL_parser->in_my_stash = NULL;
2139 /* check for C<my Dog $spot> when deciding package */
2140 stash = PAD_COMPNAME_TYPE(o->op_targ);
2142 stash = PL_curstash;
2143 apply_attrs_my(stash, o, attrs, imopsp);
2145 o->op_flags |= OPf_MOD;
2146 o->op_private |= OPpLVAL_INTRO;
2147 if (PL_parser->in_my == KEY_state)
2148 o->op_private |= OPpPAD_STATE;
2153 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2157 int maybe_scalar = 0;
2159 PERL_ARGS_ASSERT_MY_ATTRS;
2161 /* [perl #17376]: this appears to be premature, and results in code such as
2162 C< our(%x); > executing in list mode rather than void mode */
2164 if (o->op_flags & OPf_PARENS)
2174 o = my_kid(o, attrs, &rops);
2176 if (maybe_scalar && o->op_type == OP_PADSV) {
2177 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
2178 o->op_private |= OPpLVAL_INTRO;
2181 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
2183 PL_parser->in_my = FALSE;
2184 PL_parser->in_my_stash = NULL;
2189 Perl_sawparens(pTHX_ OP *o)
2191 PERL_UNUSED_CONTEXT;
2193 o->op_flags |= OPf_PARENS;
2198 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2202 const OPCODE ltype = left->op_type;
2203 const OPCODE rtype = right->op_type;
2205 PERL_ARGS_ASSERT_BIND_MATCH;
2207 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2208 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2210 const char * const desc
2211 = PL_op_desc[(rtype == OP_SUBST || rtype == OP_TRANS)
2212 ? (int)rtype : OP_MATCH];
2213 const char * const sample = ((ltype == OP_RV2AV || ltype == OP_PADAV)
2214 ? "@array" : "%hash");
2215 Perl_warner(aTHX_ packWARN(WARN_MISC),
2216 "Applying %s to %s will act on scalar(%s)",
2217 desc, sample, sample);
2220 if (rtype == OP_CONST &&
2221 cSVOPx(right)->op_private & OPpCONST_BARE &&
2222 cSVOPx(right)->op_private & OPpCONST_STRICT)
2224 no_bareword_allowed(right);
2227 ismatchop = rtype == OP_MATCH ||
2228 rtype == OP_SUBST ||
2230 if (ismatchop && right->op_private & OPpTARGET_MY) {
2232 right->op_private &= ~OPpTARGET_MY;
2234 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2237 right->op_flags |= OPf_STACKED;
2238 if (rtype != OP_MATCH &&
2239 ! (rtype == OP_TRANS &&
2240 right->op_private & OPpTRANS_IDENTICAL))
2241 newleft = mod(left, rtype);
2244 if (right->op_type == OP_TRANS)
2245 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2247 o = prepend_elem(rtype, scalar(newleft), right);
2249 return newUNOP(OP_NOT, 0, scalar(o));
2253 return bind_match(type, left,
2254 pmruntime(newPMOP(OP_MATCH, 0), right, 0));
2258 Perl_invert(pTHX_ OP *o)
2262 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2266 Perl_scope(pTHX_ OP *o)
2270 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2271 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2272 o->op_type = OP_LEAVE;
2273 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2275 else if (o->op_type == OP_LINESEQ) {
2277 o->op_type = OP_SCOPE;
2278 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2279 kid = ((LISTOP*)o)->op_first;
2280 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2283 /* The following deals with things like 'do {1 for 1}' */
2284 kid = kid->op_sibling;
2286 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2291 o = newLISTOP(OP_SCOPE, 0, o, NULL);
2297 Perl_block_start(pTHX_ int full)
2300 const int retval = PL_savestack_ix;
2301 pad_block_start(full);
2303 PL_hints &= ~HINT_BLOCK_SCOPE;
2304 SAVECOMPILEWARNINGS();
2305 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2310 Perl_block_end(pTHX_ I32 floor, OP *seq)
2313 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2314 OP* const retval = scalarseq(seq);
2316 CopHINTS_set(&PL_compiling, PL_hints);
2318 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2327 const PADOFFSET offset = Perl_pad_findmy(aTHX_ STR_WITH_LEN("$_"), 0);
2328 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2329 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2332 OP * const o = newOP(OP_PADSV, 0);
2333 o->op_targ = offset;
2339 Perl_newPROG(pTHX_ OP *o)
2343 PERL_ARGS_ASSERT_NEWPROG;
2348 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2349 ((PL_in_eval & EVAL_KEEPERR)
2350 ? OPf_SPECIAL : 0), o);
2351 PL_eval_start = linklist(PL_eval_root);
2352 PL_eval_root->op_private |= OPpREFCOUNTED;
2353 OpREFCNT_set(PL_eval_root, 1);
2354 PL_eval_root->op_next = 0;
2355 CALL_PEEP(PL_eval_start);
2358 if (o->op_type == OP_STUB) {
2359 PL_comppad_name = 0;
2361 S_op_destroy(aTHX_ o);
2364 PL_main_root = scope(sawparens(scalarvoid(o)));
2365 PL_curcop = &PL_compiling;
2366 PL_main_start = LINKLIST(PL_main_root);
2367 PL_main_root->op_private |= OPpREFCOUNTED;
2368 OpREFCNT_set(PL_main_root, 1);
2369 PL_main_root->op_next = 0;
2370 CALL_PEEP(PL_main_start);
2373 /* Register with debugger */
2375 CV * const cv = get_cvs("DB::postponed", 0);
2379 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
2381 call_sv(MUTABLE_SV(cv), G_DISCARD);
2388 Perl_localize(pTHX_ OP *o, I32 lex)
2392 PERL_ARGS_ASSERT_LOCALIZE;
2394 if (o->op_flags & OPf_PARENS)
2395 /* [perl #17376]: this appears to be premature, and results in code such as
2396 C< our(%x); > executing in list mode rather than void mode */
2403 if ( PL_parser->bufptr > PL_parser->oldbufptr
2404 && PL_parser->bufptr[-1] == ','
2405 && ckWARN(WARN_PARENTHESIS))
2407 char *s = PL_parser->bufptr;
2410 /* some heuristics to detect a potential error */
2411 while (*s && (strchr(", \t\n", *s)))
2415 if (*s && strchr("@$%*", *s) && *++s
2416 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2419 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2421 while (*s && (strchr(", \t\n", *s)))
2427 if (sigil && (*s == ';' || *s == '=')) {
2428 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2429 "Parentheses missing around \"%s\" list",
2431 ? (PL_parser->in_my == KEY_our
2433 : PL_parser->in_my == KEY_state
2443 o = mod(o, OP_NULL); /* a bit kludgey */
2444 PL_parser->in_my = FALSE;
2445 PL_parser->in_my_stash = NULL;
2450 Perl_jmaybe(pTHX_ OP *o)
2452 PERL_ARGS_ASSERT_JMAYBE;
2454 if (o->op_type == OP_LIST) {
2456 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2457 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2463 S_fold_constants(pTHX_ register OP *o)
2466 register OP * VOL curop;
2468 VOL I32 type = o->op_type;
2473 SV * const oldwarnhook = PL_warnhook;
2474 SV * const olddiehook = PL_diehook;
2478 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
2480 if (PL_opargs[type] & OA_RETSCALAR)
2482 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2483 o->op_targ = pad_alloc(type, SVs_PADTMP);
2485 /* integerize op, unless it happens to be C<-foo>.
2486 * XXX should pp_i_negate() do magic string negation instead? */
2487 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2488 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2489 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2491 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2494 if (!(PL_opargs[type] & OA_FOLDCONST))
2499 /* XXX might want a ck_negate() for this */
2500 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2511 /* XXX what about the numeric ops? */
2512 if (PL_hints & HINT_LOCALE)
2517 if (PL_parser && PL_parser->error_count)
2518 goto nope; /* Don't try to run w/ errors */
2520 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2521 const OPCODE type = curop->op_type;
2522 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
2524 type != OP_SCALAR &&
2526 type != OP_PUSHMARK)
2532 curop = LINKLIST(o);
2533 old_next = o->op_next;
2537 oldscope = PL_scopestack_ix;
2538 create_eval_scope(G_FAKINGEVAL);
2540 /* Verify that we don't need to save it: */
2541 assert(PL_curcop == &PL_compiling);
2542 StructCopy(&PL_compiling, ¬_compiling, COP);
2543 PL_curcop = ¬_compiling;
2544 /* The above ensures that we run with all the correct hints of the
2545 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
2546 assert(IN_PERL_RUNTIME);
2547 PL_warnhook = PERL_WARNHOOK_FATAL;
2554 sv = *(PL_stack_sp--);
2555 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2556 pad_swipe(o->op_targ, FALSE);
2557 else if (SvTEMP(sv)) { /* grab mortal temp? */
2558 SvREFCNT_inc_simple_void(sv);
2563 /* Something tried to die. Abandon constant folding. */
2564 /* Pretend the error never happened. */
2566 o->op_next = old_next;
2570 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
2571 PL_warnhook = oldwarnhook;
2572 PL_diehook = olddiehook;
2573 /* XXX note that this croak may fail as we've already blown away
2574 * the stack - eg any nested evals */
2575 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
2578 PL_warnhook = oldwarnhook;
2579 PL_diehook = olddiehook;
2580 PL_curcop = &PL_compiling;
2582 if (PL_scopestack_ix > oldscope)
2583 delete_eval_scope();
2592 if (type == OP_RV2GV)
2593 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
2595 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
2596 op_getmad(o,newop,'f');
2604 S_gen_constant_list(pTHX_ register OP *o)
2608 const I32 oldtmps_floor = PL_tmps_floor;
2611 if (PL_parser && PL_parser->error_count)
2612 return o; /* Don't attempt to run with errors */
2614 PL_op = curop = LINKLIST(o);
2620 assert (!(curop->op_flags & OPf_SPECIAL));
2621 assert(curop->op_type == OP_RANGE);
2623 PL_tmps_floor = oldtmps_floor;
2625 o->op_type = OP_RV2AV;
2626 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2627 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2628 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2629 o->op_opt = 0; /* needs to be revisited in peep() */
2630 curop = ((UNOP*)o)->op_first;
2631 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
2633 op_getmad(curop,o,'O');
2642 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2645 if (!o || o->op_type != OP_LIST)
2646 o = newLISTOP(OP_LIST, 0, o, NULL);
2648 o->op_flags &= ~OPf_WANT;
2650 if (!(PL_opargs[type] & OA_MARK))
2651 op_null(cLISTOPo->op_first);
2653 o->op_type = (OPCODE)type;
2654 o->op_ppaddr = PL_ppaddr[type];
2655 o->op_flags |= flags;
2657 o = CHECKOP(type, o);
2658 if (o->op_type != (unsigned)type)
2661 return fold_constants(o);
2664 /* List constructors */
2667 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2675 if (first->op_type != (unsigned)type
2676 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2678 return newLISTOP(type, 0, first, last);
2681 if (first->op_flags & OPf_KIDS)
2682 ((LISTOP*)first)->op_last->op_sibling = last;
2684 first->op_flags |= OPf_KIDS;
2685 ((LISTOP*)first)->op_first = last;
2687 ((LISTOP*)first)->op_last = last;
2692 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2700 if (first->op_type != (unsigned)type)
2701 return prepend_elem(type, (OP*)first, (OP*)last);
2703 if (last->op_type != (unsigned)type)
2704 return append_elem(type, (OP*)first, (OP*)last);
2706 first->op_last->op_sibling = last->op_first;
2707 first->op_last = last->op_last;
2708 first->op_flags |= (last->op_flags & OPf_KIDS);
2711 if (last->op_first && first->op_madprop) {
2712 MADPROP *mp = last->op_first->op_madprop;
2714 while (mp->mad_next)
2716 mp->mad_next = first->op_madprop;
2719 last->op_first->op_madprop = first->op_madprop;
2722 first->op_madprop = last->op_madprop;
2723 last->op_madprop = 0;
2726 S_op_destroy(aTHX_ (OP*)last);
2732 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2740 if (last->op_type == (unsigned)type) {
2741 if (type == OP_LIST) { /* already a PUSHMARK there */
2742 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2743 ((LISTOP*)last)->op_first->op_sibling = first;
2744 if (!(first->op_flags & OPf_PARENS))
2745 last->op_flags &= ~OPf_PARENS;
2748 if (!(last->op_flags & OPf_KIDS)) {
2749 ((LISTOP*)last)->op_last = first;
2750 last->op_flags |= OPf_KIDS;
2752 first->op_sibling = ((LISTOP*)last)->op_first;
2753 ((LISTOP*)last)->op_first = first;
2755 last->op_flags |= OPf_KIDS;
2759 return newLISTOP(type, 0, first, last);
2767 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
2770 Newxz(tk, 1, TOKEN);
2771 tk->tk_type = (OPCODE)optype;
2772 tk->tk_type = 12345;
2774 tk->tk_mad = madprop;
2779 Perl_token_free(pTHX_ TOKEN* tk)
2781 PERL_ARGS_ASSERT_TOKEN_FREE;
2783 if (tk->tk_type != 12345)
2785 mad_free(tk->tk_mad);
2790 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
2795 PERL_ARGS_ASSERT_TOKEN_GETMAD;
2797 if (tk->tk_type != 12345) {
2798 Perl_warner(aTHX_ packWARN(WARN_MISC),
2799 "Invalid TOKEN object ignored");
2806 /* faked up qw list? */
2808 tm->mad_type == MAD_SV &&
2809 SvPVX((SV *)tm->mad_val)[0] == 'q')
2816 /* pretend constant fold didn't happen? */
2817 if (mp->mad_key == 'f' &&
2818 (o->op_type == OP_CONST ||
2819 o->op_type == OP_GV) )
2821 token_getmad(tk,(OP*)mp->mad_val,slot);
2835 if (mp->mad_key == 'X')
2836 mp->mad_key = slot; /* just change the first one */
2846 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
2855 /* pretend constant fold didn't happen? */
2856 if (mp->mad_key == 'f' &&
2857 (o->op_type == OP_CONST ||
2858 o->op_type == OP_GV) )
2860 op_getmad(from,(OP*)mp->mad_val,slot);
2867 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
2870 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
2876 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
2885 /* pretend constant fold didn't happen? */
2886 if (mp->mad_key == 'f' &&
2887 (o->op_type == OP_CONST ||
2888 o->op_type == OP_GV) )
2890 op_getmad(from,(OP*)mp->mad_val,slot);
2897 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
2900 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
2904 PerlIO_printf(PerlIO_stderr(),
2905 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
2911 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
2929 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
2933 addmad(tm, &(o->op_madprop), slot);
2937 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
2958 Perl_newMADsv(pTHX_ char key, SV* sv)
2960 PERL_ARGS_ASSERT_NEWMADSV;
2962 return newMADPROP(key, MAD_SV, sv, 0);
2966 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
2969 Newxz(mp, 1, MADPROP);
2972 mp->mad_vlen = vlen;
2973 mp->mad_type = type;
2975 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
2980 Perl_mad_free(pTHX_ MADPROP* mp)
2982 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
2986 mad_free(mp->mad_next);
2987 /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
2988 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
2989 switch (mp->mad_type) {
2993 Safefree((char*)mp->mad_val);
2996 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
2997 op_free((OP*)mp->mad_val);
3000 sv_free(MUTABLE_SV(mp->mad_val));
3003 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3012 Perl_newNULLLIST(pTHX)
3014 return newOP(OP_STUB, 0);
3018 S_force_list(pTHX_ OP *o)
3020 if (!o || o->op_type != OP_LIST)
3021 o = newLISTOP(OP_LIST, 0, o, NULL);
3027 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3032 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3034 NewOp(1101, listop, 1, LISTOP);
3036 listop->op_type = (OPCODE)type;
3037 listop->op_ppaddr = PL_ppaddr[type];
3040 listop->op_flags = (U8)flags;
3044 else if (!first && last)
3047 first->op_sibling = last;
3048 listop->op_first = first;
3049 listop->op_last = last;
3050 if (type == OP_LIST) {
3051 OP* const pushop = newOP(OP_PUSHMARK, 0);
3052 pushop->op_sibling = first;
3053 listop->op_first = pushop;
3054 listop->op_flags |= OPf_KIDS;
3056 listop->op_last = pushop;
3059 return CHECKOP(type, listop);
3063 Perl_newOP(pTHX_ I32 type, I32 flags)
3068 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3069 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3070 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3071 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3073 NewOp(1101, o, 1, OP);
3074 o->op_type = (OPCODE)type;
3075 o->op_ppaddr = PL_ppaddr[type];
3076 o->op_flags = (U8)flags;
3078 o->op_latefreed = 0;
3082 o->op_private = (U8)(0 | (flags >> 8));
3083 if (PL_opargs[type] & OA_RETSCALAR)
3085 if (PL_opargs[type] & OA_TARGET)
3086 o->op_targ = pad_alloc(type, SVs_PADTMP);
3087 return CHECKOP(type, o);
3091 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3096 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3097 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3098 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3099 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3100 || type == OP_SASSIGN
3101 || type == OP_ENTERTRY
3102 || type == OP_NULL );
3105 first = newOP(OP_STUB, 0);
3106 if (PL_opargs[type] & OA_MARK)
3107 first = force_list(first);
3109 NewOp(1101, unop, 1, UNOP);
3110 unop->op_type = (OPCODE)type;
3111 unop->op_ppaddr = PL_ppaddr[type];
3112 unop->op_first = first;
3113 unop->op_flags = (U8)(flags | OPf_KIDS);
3114 unop->op_private = (U8)(1 | (flags >> 8));
3115 unop = (UNOP*) CHECKOP(type, unop);
3119 return fold_constants((OP *) unop);
3123 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3128 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3129 || type == OP_SASSIGN || type == OP_NULL );
3131 NewOp(1101, binop, 1, BINOP);
3134 first = newOP(OP_NULL, 0);
3136 binop->op_type = (OPCODE)type;
3137 binop->op_ppaddr = PL_ppaddr[type];
3138 binop->op_first = first;
3139 binop->op_flags = (U8)(flags | OPf_KIDS);
3142 binop->op_private = (U8)(1 | (flags >> 8));
3145 binop->op_private = (U8)(2 | (flags >> 8));
3146 first->op_sibling = last;
3149 binop = (BINOP*)CHECKOP(type, binop);
3150 if (binop->op_next || binop->op_type != (OPCODE)type)
3153 binop->op_last = binop->op_first->op_sibling;
3155 return fold_constants((OP *)binop);
3158 static int uvcompare(const void *a, const void *b)
3159 __attribute__nonnull__(1)
3160 __attribute__nonnull__(2)
3161 __attribute__pure__;
3162 static int uvcompare(const void *a, const void *b)
3164 if (*((const UV *)a) < (*(const UV *)b))
3166 if (*((const UV *)a) > (*(const UV *)b))
3168 if (*((const UV *)a+1) < (*(const UV *)b+1))
3170 if (*((const UV *)a+1) > (*(const UV *)b+1))
3176 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
3179 SV * const tstr = ((SVOP*)expr)->op_sv;
3182 (repl->op_type == OP_NULL)
3183 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
3185 ((SVOP*)repl)->op_sv;
3188 const U8 *t = (U8*)SvPV_const(tstr, tlen);
3189 const U8 *r = (U8*)SvPV_const(rstr, rlen);
3193 register short *tbl;
3195 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
3196 const I32 squash = o->op_private & OPpTRANS_SQUASH;
3197 I32 del = o->op_private & OPpTRANS_DELETE;
3200 PERL_ARGS_ASSERT_PMTRANS;
3202 PL_hints |= HINT_BLOCK_SCOPE;
3205 o->op_private |= OPpTRANS_FROM_UTF;
3208 o->op_private |= OPpTRANS_TO_UTF;
3210 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
3211 SV* const listsv = newSVpvs("# comment\n");
3213 const U8* tend = t + tlen;
3214 const U8* rend = r + rlen;
3228 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
3229 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
3232 const U32 flags = UTF8_ALLOW_DEFAULT;
3236 t = tsave = bytes_to_utf8(t, &len);
3239 if (!to_utf && rlen) {
3241 r = rsave = bytes_to_utf8(r, &len);
3245 /* There are several snags with this code on EBCDIC:
3246 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
3247 2. scan_const() in toke.c has encoded chars in native encoding which makes
3248 ranges at least in EBCDIC 0..255 range the bottom odd.
3252 U8 tmpbuf[UTF8_MAXBYTES+1];
3255 Newx(cp, 2*tlen, UV);
3257 transv = newSVpvs("");
3259 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3261 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
3263 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3267 cp[2*i+1] = cp[2*i];
3271 qsort(cp, i, 2*sizeof(UV), uvcompare);
3272 for (j = 0; j < i; j++) {
3274 diff = val - nextmin;
3276 t = uvuni_to_utf8(tmpbuf,nextmin);
3277 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3279 U8 range_mark = UTF_TO_NATIVE(0xff);
3280 t = uvuni_to_utf8(tmpbuf, val - 1);
3281 sv_catpvn(transv, (char *)&range_mark, 1);
3282 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3289 t = uvuni_to_utf8(tmpbuf,nextmin);
3290 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3292 U8 range_mark = UTF_TO_NATIVE(0xff);
3293 sv_catpvn(transv, (char *)&range_mark, 1);
3295 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
3296 UNICODE_ALLOW_SUPER);
3297 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3298 t = (const U8*)SvPVX_const(transv);
3299 tlen = SvCUR(transv);
3303 else if (!rlen && !del) {
3304 r = t; rlen = tlen; rend = tend;
3307 if ((!rlen && !del) || t == r ||
3308 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
3310 o->op_private |= OPpTRANS_IDENTICAL;
3314 while (t < tend || tfirst <= tlast) {
3315 /* see if we need more "t" chars */
3316 if (tfirst > tlast) {
3317 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3319 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
3321 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3328 /* now see if we need more "r" chars */
3329 if (rfirst > rlast) {
3331 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3333 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
3335 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3344 rfirst = rlast = 0xffffffff;
3348 /* now see which range will peter our first, if either. */
3349 tdiff = tlast - tfirst;
3350 rdiff = rlast - rfirst;
3357 if (rfirst == 0xffffffff) {
3358 diff = tdiff; /* oops, pretend rdiff is infinite */
3360 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
3361 (long)tfirst, (long)tlast);
3363 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
3367 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
3368 (long)tfirst, (long)(tfirst + diff),
3371 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
3372 (long)tfirst, (long)rfirst);
3374 if (rfirst + diff > max)
3375 max = rfirst + diff;
3377 grows = (tfirst < rfirst &&
3378 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
3390 else if (max > 0xff)
3395 PerlMemShared_free(cPVOPo->op_pv);
3396 cPVOPo->op_pv = NULL;
3398 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
3400 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
3401 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
3402 PAD_SETSV(cPADOPo->op_padix, swash);
3404 SvREADONLY_on(swash);
3406 cSVOPo->op_sv = swash;
3408 SvREFCNT_dec(listsv);
3409 SvREFCNT_dec(transv);
3411 if (!del && havefinal && rlen)
3412 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
3413 newSVuv((UV)final), 0);
3416 o->op_private |= OPpTRANS_GROWS;
3422 op_getmad(expr,o,'e');
3423 op_getmad(repl,o,'r');
3431 tbl = (short*)cPVOPo->op_pv;
3433 Zero(tbl, 256, short);
3434 for (i = 0; i < (I32)tlen; i++)
3436 for (i = 0, j = 0; i < 256; i++) {
3438 if (j >= (I32)rlen) {
3447 if (i < 128 && r[j] >= 128)
3457 o->op_private |= OPpTRANS_IDENTICAL;
3459 else if (j >= (I32)rlen)
3464 PerlMemShared_realloc(tbl,
3465 (0x101+rlen-j) * sizeof(short));
3466 cPVOPo->op_pv = (char*)tbl;
3468 tbl[0x100] = (short)(rlen - j);
3469 for (i=0; i < (I32)rlen - j; i++)
3470 tbl[0x101+i] = r[j+i];
3474 if (!rlen && !del) {
3477 o->op_private |= OPpTRANS_IDENTICAL;
3479 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
3480 o->op_private |= OPpTRANS_IDENTICAL;
3482 for (i = 0; i < 256; i++)
3484 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
3485 if (j >= (I32)rlen) {
3487 if (tbl[t[i]] == -1)
3493 if (tbl[t[i]] == -1) {
3494 if (t[i] < 128 && r[j] >= 128)
3501 if(del && rlen == tlen) {
3502 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
3503 } else if(rlen > tlen) {
3504 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
3508 o->op_private |= OPpTRANS_GROWS;
3510 op_getmad(expr,o,'e');
3511 op_getmad(repl,o,'r');
3521 Perl_newPMOP(pTHX_ I32 type, I32 flags)
3526 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
3528 NewOp(1101, pmop, 1, PMOP);
3529 pmop->op_type = (OPCODE)type;
3530 pmop->op_ppaddr = PL_ppaddr[type];
3531 pmop->op_flags = (U8)flags;
3532 pmop->op_private = (U8)(0 | (flags >> 8));
3534 if (PL_hints & HINT_RE_TAINT)
3535 pmop->op_pmflags |= PMf_RETAINT;
3536 if (PL_hints & HINT_LOCALE)
3537 pmop->op_pmflags |= PMf_LOCALE;
3541 assert(SvPOK(PL_regex_pad[0]));
3542 if (SvCUR(PL_regex_pad[0])) {
3543 /* Pop off the "packed" IV from the end. */
3544 SV *const repointer_list = PL_regex_pad[0];
3545 const char *p = SvEND(repointer_list) - sizeof(IV);
3546 const IV offset = *((IV*)p);
3548 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
3550 SvEND_set(repointer_list, p);
3552 pmop->op_pmoffset = offset;
3553 /* This slot should be free, so assert this: */
3554 assert(PL_regex_pad[offset] == &PL_sv_undef);
3556 SV * const repointer = &PL_sv_undef;
3557 av_push(PL_regex_padav, repointer);
3558 pmop->op_pmoffset = av_len(PL_regex_padav);
3559 PL_regex_pad = AvARRAY(PL_regex_padav);
3563 return CHECKOP(type, pmop);
3566 /* Given some sort of match op o, and an expression expr containing a
3567 * pattern, either compile expr into a regex and attach it to o (if it's
3568 * constant), or convert expr into a runtime regcomp op sequence (if it's
3571 * isreg indicates that the pattern is part of a regex construct, eg
3572 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
3573 * split "pattern", which aren't. In the former case, expr will be a list
3574 * if the pattern contains more than one term (eg /a$b/) or if it contains
3575 * a replacement, ie s/// or tr///.
3579 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg)
3584 I32 repl_has_vars = 0;
3588 PERL_ARGS_ASSERT_PMRUNTIME;
3590 if (o->op_type == OP_SUBST || o->op_type == OP_TRANS) {
3591 /* last element in list is the replacement; pop it */
3593 repl = cLISTOPx(expr)->op_last;
3594 kid = cLISTOPx(expr)->op_first;
3595 while (kid->op_sibling != repl)
3596 kid = kid->op_sibling;
3597 kid->op_sibling = NULL;
3598 cLISTOPx(expr)->op_last = kid;
3601 if (isreg && expr->op_type == OP_LIST &&
3602 cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last)
3604 /* convert single element list to element */
3605 OP* const oe = expr;
3606 expr = cLISTOPx(oe)->op_first->op_sibling;
3607 cLISTOPx(oe)->op_first->op_sibling = NULL;
3608 cLISTOPx(oe)->op_last = NULL;
3612 if (o->op_type == OP_TRANS) {
3613 return pmtrans(o, expr, repl);
3616 reglist = isreg && expr->op_type == OP_LIST;
3620 PL_hints |= HINT_BLOCK_SCOPE;
3623 if (expr->op_type == OP_CONST) {
3624 SV *pat = ((SVOP*)expr)->op_sv;
3625 U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
3627 if (o->op_flags & OPf_SPECIAL)
3628 pm_flags |= RXf_SPLIT;
3631 assert (SvUTF8(pat));
3632 } else if (SvUTF8(pat)) {
3633 /* Not doing UTF-8, despite what the SV says. Is this only if we're
3634 trapped in use 'bytes'? */
3635 /* Make a copy of the octet sequence, but without the flag on, as
3636 the compiler now honours the SvUTF8 flag on pat. */
3638 const char *const p = SvPV(pat, len);
3639 pat = newSVpvn_flags(p, len, SVs_TEMP);
3642 PM_SETRE(pm, CALLREGCOMP(pat, pm_flags));
3645 op_getmad(expr,(OP*)pm,'e');
3651 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
3652 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
3654 : OP_REGCMAYBE),0,expr);
3656 NewOp(1101, rcop, 1, LOGOP);
3657 rcop->op_type = OP_REGCOMP;
3658 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
3659 rcop->op_first = scalar(expr);
3660 rcop->op_flags |= OPf_KIDS
3661 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
3662 | (reglist ? OPf_STACKED : 0);
3663 rcop->op_private = 1;
3666 rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP);
3668 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
3671 /* establish postfix order */
3672 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
3674 rcop->op_next = expr;
3675 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
3678 rcop->op_next = LINKLIST(expr);
3679 expr->op_next = (OP*)rcop;
3682 prepend_elem(o->op_type, scalar((OP*)rcop), o);
3687 if (pm->op_pmflags & PMf_EVAL) {
3689 if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
3690 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
3692 else if (repl->op_type == OP_CONST)
3696 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
3697 if (curop->op_type == OP_SCOPE
3698 || curop->op_type == OP_LEAVE
3699 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
3700 if (curop->op_type == OP_GV) {
3701 GV * const gv = cGVOPx_gv(curop);
3703 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
3706 else if (curop->op_type == OP_RV2CV)
3708 else if (curop->op_type == OP_RV2SV ||
3709 curop->op_type == OP_RV2AV ||
3710 curop->op_type == OP_RV2HV ||
3711 curop->op_type == OP_RV2GV) {
3712 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
3715 else if (curop->op_type == OP_PADSV ||
3716 curop->op_type == OP_PADAV ||
3717 curop->op_type == OP_PADHV ||
3718 curop->op_type == OP_PADANY)
3722 else if (curop->op_type == OP_PUSHRE)
3723 NOOP; /* Okay here, dangerous in newASSIGNOP */
3733 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
3735 pm->op_pmflags |= PMf_CONST; /* const for long enough */
3736 prepend_elem(o->op_type, scalar(repl), o);
3739 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
3740 pm->op_pmflags |= PMf_MAYBE_CONST;
3742 NewOp(1101, rcop, 1, LOGOP);
3743 rcop->op_type = OP_SUBSTCONT;
3744 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
3745 rcop->op_first = scalar(repl);
3746 rcop->op_flags |= OPf_KIDS;
3747 rcop->op_private = 1;
3750 /* establish postfix order */
3751 rcop->op_next = LINKLIST(repl);
3752 repl->op_next = (OP*)rcop;
3754 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
3755 assert(!(pm->op_pmflags & PMf_ONCE));
3756 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
3765 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3770 PERL_ARGS_ASSERT_NEWSVOP;
3772 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
3773 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3774 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
3776 NewOp(1101, svop, 1, SVOP);
3777 svop->op_type = (OPCODE)type;
3778 svop->op_ppaddr = PL_ppaddr[type];
3780 svop->op_next = (OP*)svop;
3781 svop->op_flags = (U8)flags;
3782 if (PL_opargs[type] & OA_RETSCALAR)
3784 if (PL_opargs[type] & OA_TARGET)
3785 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3786 return CHECKOP(type, svop);
3791 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3796 PERL_ARGS_ASSERT_NEWPADOP;
3798 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
3799 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3800 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
3802 NewOp(1101, padop, 1, PADOP);
3803 padop->op_type = (OPCODE)type;
3804 padop->op_ppaddr = PL_ppaddr[type];
3805 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3806 SvREFCNT_dec(PAD_SVl(padop->op_padix));
3807 PAD_SETSV(padop->op_padix, sv);
3810 padop->op_next = (OP*)padop;
3811 padop->op_flags = (U8)flags;
3812 if (PL_opargs[type] & OA_RETSCALAR)
3814 if (PL_opargs[type] & OA_TARGET)
3815 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3816 return CHECKOP(type, padop);
3821 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3825 PERL_ARGS_ASSERT_NEWGVOP;
3829 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
3831 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
3836 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3841 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
3842 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3844 NewOp(1101, pvop, 1, PVOP);
3845 pvop->op_type = (OPCODE)type;
3846 pvop->op_ppaddr = PL_ppaddr[type];
3848 pvop->op_next = (OP*)pvop;
3849 pvop->op_flags = (U8)flags;
3850 if (PL_opargs[type] & OA_RETSCALAR)
3852 if (PL_opargs[type] & OA_TARGET)
3853 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3854 return CHECKOP(type, pvop);
3862 Perl_package(pTHX_ OP *o)
3865 SV *const sv = cSVOPo->op_sv;
3870 PERL_ARGS_ASSERT_PACKAGE;
3872 save_hptr(&PL_curstash);
3873 save_item(PL_curstname);
3875 PL_curstash = gv_stashsv(sv, GV_ADD);
3877 sv_setsv(PL_curstname, sv);
3879 PL_hints |= HINT_BLOCK_SCOPE;
3880 PL_parser->copline = NOLINE;
3881 PL_parser->expect = XSTATE;
3886 if (!PL_madskills) {
3891 pegop = newOP(OP_NULL,0);
3892 op_getmad(o,pegop,'P');
3898 Perl_package_version( pTHX_ OP *v )
3901 U32 savehints = PL_hints;
3902 PERL_ARGS_ASSERT_PACKAGE_VERSION;
3903 PL_hints &= ~HINT_STRICT_VARS;
3904 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
3905 PL_hints = savehints;
3914 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3921 OP *pegop = newOP(OP_NULL,0);
3924 PERL_ARGS_ASSERT_UTILIZE;
3926 if (idop->op_type != OP_CONST)
3927 Perl_croak(aTHX_ "Module name must be constant");
3930 op_getmad(idop,pegop,'U');
3935 SV * const vesv = ((SVOP*)version)->op_sv;
3938 op_getmad(version,pegop,'V');
3939 if (!arg && !SvNIOKp(vesv)) {
3946 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3947 Perl_croak(aTHX_ "Version number must be a constant number");
3949 /* Make copy of idop so we don't free it twice */
3950 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3952 /* Fake up a method call to VERSION */
3953 meth = newSVpvs_share("VERSION");
3954 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3955 append_elem(OP_LIST,
3956 prepend_elem(OP_LIST, pack, list(version)),
3957 newSVOP(OP_METHOD_NAMED, 0, meth)));
3961 /* Fake up an import/unimport */
3962 if (arg && arg->op_type == OP_STUB) {
3964 op_getmad(arg,pegop,'S');
3965 imop = arg; /* no import on explicit () */
3967 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3968 imop = NULL; /* use 5.0; */
3970 idop->op_private |= OPpCONST_NOVER;
3976 op_getmad(arg,pegop,'A');
3978 /* Make copy of idop so we don't free it twice */
3979 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3981 /* Fake up a method call to import/unimport */
3983 ? newSVpvs_share("import") : newSVpvs_share("unimport");
3984 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3985 append_elem(OP_LIST,
3986 prepend_elem(OP_LIST, pack, list(arg)),
3987 newSVOP(OP_METHOD_NAMED, 0, meth)));
3990 /* Fake up the BEGIN {}, which does its thing immediately. */
3992 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
3995 append_elem(OP_LINESEQ,
3996 append_elem(OP_LINESEQ,
3997 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
3998 newSTATEOP(0, NULL, veop)),
3999 newSTATEOP(0, NULL, imop) ));
4001 /* The "did you use incorrect case?" warning used to be here.
4002 * The problem is that on case-insensitive filesystems one
4003 * might get false positives for "use" (and "require"):
4004 * "use Strict" or "require CARP" will work. This causes
4005 * portability problems for the script: in case-strict
4006 * filesystems the script will stop working.
4008 * The "incorrect case" warning checked whether "use Foo"
4009 * imported "Foo" to your namespace, but that is wrong, too:
4010 * there is no requirement nor promise in the language that
4011 * a Foo.pm should or would contain anything in package "Foo".
4013 * There is very little Configure-wise that can be done, either:
4014 * the case-sensitivity of the build filesystem of Perl does not
4015 * help in guessing the case-sensitivity of the runtime environment.
4018 PL_hints |= HINT_BLOCK_SCOPE;
4019 PL_parser->copline = NOLINE;
4020 PL_parser->expect = XSTATE;
4021 PL_cop_seqmax++; /* Purely for B::*'s benefit */
4024 if (!PL_madskills) {
4025 /* FIXME - don't allocate pegop if !PL_madskills */
4034 =head1 Embedding Functions
4036 =for apidoc load_module
4038 Loads the module whose name is pointed to by the string part of name.
4039 Note that the actual module name, not its filename, should be given.
4040 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
4041 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
4042 (or 0 for no flags). ver, if specified, provides version semantics
4043 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
4044 arguments can be used to specify arguments to the module's import()
4045 method, similar to C<use Foo::Bar VERSION LIST>. They must be
4046 terminated with a final NULL pointer. Note that this list can only
4047 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
4048 Otherwise at least a single NULL pointer to designate the default
4049 import list is required.
4054 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
4058 PERL_ARGS_ASSERT_LOAD_MODULE;
4060 va_start(args, ver);
4061 vload_module(flags, name, ver, &args);
4065 #ifdef PERL_IMPLICIT_CONTEXT
4067 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
4071 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
4072 va_start(args, ver);
4073 vload_module(flags, name, ver, &args);
4079 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
4083 OP * const modname = newSVOP(OP_CONST, 0, name);
4085 PERL_ARGS_ASSERT_VLOAD_MODULE;
4087 modname->op_private |= OPpCONST_BARE;
4089 veop = newSVOP(OP_CONST, 0, ver);
4093 if (flags & PERL_LOADMOD_NOIMPORT) {
4094 imop = sawparens(newNULLLIST());
4096 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
4097 imop = va_arg(*args, OP*);
4102 sv = va_arg(*args, SV*);
4104 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
4105 sv = va_arg(*args, SV*);
4109 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
4110 * that it has a PL_parser to play with while doing that, and also
4111 * that it doesn't mess with any existing parser, by creating a tmp
4112 * new parser with lex_start(). This won't actually be used for much,
4113 * since pp_require() will create another parser for the real work. */
4116 SAVEVPTR(PL_curcop);
4117 lex_start(NULL, NULL, FALSE);
4118 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
4119 veop, modname, imop);
4124 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
4130 PERL_ARGS_ASSERT_DOFILE;
4132 if (!force_builtin) {
4133 gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
4134 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
4135 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
4136 gv = gvp ? *gvp : NULL;
4140 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
4141 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
4142 append_elem(OP_LIST, term,
4143 scalar(newUNOP(OP_RV2CV, 0,
4144 newGVOP(OP_GV, 0, gv))))));
4147 doop = newUNOP(OP_DOFILE, 0, scalar(term));
4153 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
4155 return newBINOP(OP_LSLICE, flags,
4156 list(force_list(subscript)),
4157 list(force_list(listval)) );
4161 S_is_list_assignment(pTHX_ register const OP *o)
4169 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
4170 o = cUNOPo->op_first;
4172 flags = o->op_flags;
4174 if (type == OP_COND_EXPR) {
4175 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
4176 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
4181 yyerror("Assignment to both a list and a scalar");
4185 if (type == OP_LIST &&
4186 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
4187 o->op_private & OPpLVAL_INTRO)
4190 if (type == OP_LIST || flags & OPf_PARENS ||
4191 type == OP_RV2AV || type == OP_RV2HV ||
4192 type == OP_ASLICE || type == OP_HSLICE)
4195 if (type == OP_PADAV || type == OP_PADHV)
4198 if (type == OP_RV2SV)
4205 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
4211 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
4212 return newLOGOP(optype, 0,
4213 mod(scalar(left), optype),
4214 newUNOP(OP_SASSIGN, 0, scalar(right)));
4217 return newBINOP(optype, OPf_STACKED,
4218 mod(scalar(left), optype), scalar(right));
4222 if (is_list_assignment(left)) {
4223 static const char no_list_state[] = "Initialization of state variables"
4224 " in list context currently forbidden";
4226 bool maybe_common_vars = TRUE;
4229 /* Grandfathering $[ assignment here. Bletch.*/
4230 /* Only simple assignments like C<< ($[) = 1 >> are allowed */
4231 PL_eval_start = (left->op_type == OP_CONST) ? right : NULL;
4232 left = mod(left, OP_AASSIGN);
4235 else if (left->op_type == OP_CONST) {
4237 /* Result of assignment is always 1 (or we'd be dead already) */
4238 return newSVOP(OP_CONST, 0, newSViv(1));
4240 curop = list(force_list(left));
4241 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
4242 o->op_private = (U8)(0 | (flags >> 8));
4244 if ((left->op_type == OP_LIST
4245 || (left->op_type == OP_NULL && left->op_targ == OP_LIST)))
4247 OP* lop = ((LISTOP*)left)->op_first;
4248 maybe_common_vars = FALSE;
4250 if (lop->op_type == OP_PADSV ||
4251 lop->op_type == OP_PADAV ||
4252 lop->op_type == OP_PADHV ||
4253 lop->op_type == OP_PADANY) {
4254 if (!(lop->op_private & OPpLVAL_INTRO))
4255 maybe_common_vars = TRUE;
4257 if (lop->op_private & OPpPAD_STATE) {
4258 if (left->op_private & OPpLVAL_INTRO) {
4259 /* Each variable in state($a, $b, $c) = ... */
4262 /* Each state variable in
4263 (state $a, my $b, our $c, $d, undef) = ... */
4265 yyerror(no_list_state);
4267 /* Each my variable in
4268 (state $a, my $b, our $c, $d, undef) = ... */
4270 } else if (lop->op_type == OP_UNDEF ||
4271 lop->op_type == OP_PUSHMARK) {
4272 /* undef may be interesting in
4273 (state $a, undef, state $c) */
4275 /* Other ops in the list. */
4276 maybe_common_vars = TRUE;
4278 lop = lop->op_sibling;
4281 else if ((left->op_private & OPpLVAL_INTRO)
4282 && ( left->op_type == OP_PADSV
4283 || left->op_type == OP_PADAV
4284 || left->op_type == OP_PADHV
4285 || left->op_type == OP_PADANY))
4287 if (left->op_type == OP_PADSV) maybe_common_vars = FALSE;
4288 if (left->op_private & OPpPAD_STATE) {
4289 /* All single variable list context state assignments, hence
4299 yyerror(no_list_state);
4303 /* PL_generation sorcery:
4304 * an assignment like ($a,$b) = ($c,$d) is easier than
4305 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
4306 * To detect whether there are common vars, the global var
4307 * PL_generation is incremented for each assign op we compile.
4308 * Then, while compiling the assign op, we run through all the
4309 * variables on both sides of the assignment, setting a spare slot
4310 * in each of them to PL_generation. If any of them already have
4311 * that value, we know we've got commonality. We could use a
4312 * single bit marker, but then we'd have to make 2 passes, first
4313 * to clear the flag, then to test and set it. To find somewhere
4314 * to store these values, evil chicanery is done with SvUVX().
4317 if (maybe_common_vars) {
4320 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4321 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
4322 if (curop->op_type == OP_GV) {
4323 GV *gv = cGVOPx_gv(curop);
4325 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4327 GvASSIGN_GENERATION_set(gv, PL_generation);
4329 else if (curop->op_type == OP_PADSV ||
4330 curop->op_type == OP_PADAV ||
4331 curop->op_type == OP_PADHV ||
4332 curop->op_type == OP_PADANY)
4334 if (PAD_COMPNAME_GEN(curop->op_targ)
4335 == (STRLEN)PL_generation)
4337 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
4340 else if (curop->op_type == OP_RV2CV)
4342 else if (curop->op_type == OP_RV2SV ||
4343 curop->op_type == OP_RV2AV ||
4344 curop->op_type == OP_RV2HV ||
4345 curop->op_type == OP_RV2GV) {
4346 if (lastop->op_type != OP_GV) /* funny deref? */
4349 else if (curop->op_type == OP_PUSHRE) {
4351 if (((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff) {
4352 GV *const gv = MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff));
4354 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4356 GvASSIGN_GENERATION_set(gv, PL_generation);
4360 = ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv;
4363 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
4365 GvASSIGN_GENERATION_set(gv, PL_generation);
4375 o->op_private |= OPpASSIGN_COMMON;
4378 if (right && right->op_type == OP_SPLIT && !PL_madskills) {
4379 OP* tmpop = ((LISTOP*)right)->op_first;
4380 if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
4381 PMOP * const pm = (PMOP*)tmpop;
4382 if (left->op_type == OP_RV2AV &&
4383 !(left->op_private & OPpLVAL_INTRO) &&
4384 !(o->op_private & OPpASSIGN_COMMON) )
4386 tmpop = ((UNOP*)left)->op_first;
4387 if (tmpop->op_type == OP_GV
4389 && !pm->op_pmreplrootu.op_pmtargetoff
4391 && !pm->op_pmreplrootu.op_pmtargetgv
4395 pm->op_pmreplrootu.op_pmtargetoff
4396 = cPADOPx(tmpop)->op_padix;
4397 cPADOPx(tmpop)->op_padix = 0; /* steal it */
4399 pm->op_pmreplrootu.op_pmtargetgv
4400 = MUTABLE_GV(cSVOPx(tmpop)->op_sv);
4401 cSVOPx(tmpop)->op_sv = NULL; /* steal it */
4403 pm->op_pmflags |= PMf_ONCE;
4404 tmpop = cUNOPo->op_first; /* to list (nulled) */
4405 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
4406 tmpop->op_sibling = NULL; /* don't free split */
4407 right->op_next = tmpop->op_next; /* fix starting loc */
4408 op_free(o); /* blow off assign */
4409 right->op_flags &= ~OPf_WANT;
4410 /* "I don't know and I don't care." */
4415 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
4416 ((LISTOP*)right)->op_last->op_type == OP_CONST)
4418 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
4419 if (SvIOK(sv) && SvIVX(sv) == 0)
4420 sv_setiv(sv, PL_modcount+1);
4428 right = newOP(OP_UNDEF, 0);
4429 if (right->op_type == OP_READLINE) {
4430 right->op_flags |= OPf_STACKED;
4431 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
4434 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
4435 o = newBINOP(OP_SASSIGN, flags,
4436 scalar(right), mod(scalar(left), OP_SASSIGN) );
4440 if (!PL_madskills) { /* assignment to $[ is ignored when making a mad dump */
4441 deprecate("assignment to $[");
4443 o = newSVOP(OP_CONST, 0, newSViv(CopARYBASE_get(&PL_compiling)));
4444 o->op_private |= OPpCONST_ARYBASE;
4452 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
4455 const U32 seq = intro_my();
4458 NewOp(1101, cop, 1, COP);
4459 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
4460 cop->op_type = OP_DBSTATE;
4461 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
4464 cop->op_type = OP_NEXTSTATE;
4465 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
4467 cop->op_flags = (U8)flags;
4468 CopHINTS_set(cop, PL_hints);
4470 cop->op_private |= NATIVE_HINTS;
4472 CopHINTS_set(&PL_compiling, CopHINTS_get(cop));
4473 cop->op_next = (OP*)cop;
4476 /* CopARYBASE is now "virtual", in that it's stored as a flag bit in
4477 CopHINTS and a possible value in cop_hints_hash, so no need to copy it.
4479 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
4480 cop->cop_hints_hash = PL_curcop->cop_hints_hash;
4481 if (cop->cop_hints_hash) {
4483 cop->cop_hints_hash->refcounted_he_refcnt++;
4484 HINTS_REFCNT_UNLOCK;
4488 = Perl_store_cop_label(aTHX_ cop->cop_hints_hash, label);
4490 PL_hints |= HINT_BLOCK_SCOPE;
4491 /* It seems that we need to defer freeing this pointer, as other parts
4492 of the grammar end up wanting to copy it after this op has been
4497 if (PL_parser && PL_parser->copline == NOLINE)
4498 CopLINE_set(cop, CopLINE(PL_curcop));
4500 CopLINE_set(cop, PL_parser->copline);
4502 PL_parser->copline = NOLINE;
4505 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
4507 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
4509 CopSTASH_set(cop, PL_curstash);
4511 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash) {
4512 /* this line can have a breakpoint - store the cop in IV */
4513 AV *av = CopFILEAVx(PL_curcop);
4515 SV * const * const svp = av_fetch(av, (I32)CopLINE(cop), FALSE);
4516 if (svp && *svp != &PL_sv_undef ) {
4517 (void)SvIOK_on(*svp);
4518 SvIV_set(*svp, PTR2IV(cop));
4523 if (flags & OPf_SPECIAL)
4525 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
4530 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
4534 PERL_ARGS_ASSERT_NEWLOGOP;
4536 return new_logop(type, flags, &first, &other);
4540 S_search_const(pTHX_ OP *o)
4542 PERL_ARGS_ASSERT_SEARCH_CONST;
4544 switch (o->op_type) {
4548 if (o->op_flags & OPf_KIDS)
4549 return search_const(cUNOPo->op_first);
4556 if (!(o->op_flags & OPf_KIDS))
4558 kid = cLISTOPo->op_first;
4560 switch (kid->op_type) {
4564 kid = kid->op_sibling;
4567 if (kid != cLISTOPo->op_last)
4573 kid = cLISTOPo->op_last;
4575 return search_const(kid);
4583 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
4591 int prepend_not = 0;
4593 PERL_ARGS_ASSERT_NEW_LOGOP;
4598 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
4599 return newBINOP(type, flags, scalar(first), scalar(other));
4601 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP);
4603 scalarboolean(first);
4604 /* optimize AND and OR ops that have NOTs as children */
4605 if (first->op_type == OP_NOT
4606 && (first->op_flags & OPf_KIDS)
4607 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
4608 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
4610 if (type == OP_AND || type == OP_OR) {
4616 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
4618 prepend_not = 1; /* prepend a NOT op later */
4622 /* search for a constant op that could let us fold the test */
4623 if ((cstop = search_const(first))) {
4624 if (cstop->op_private & OPpCONST_STRICT)
4625 no_bareword_allowed(cstop);
4626 else if ((cstop->op_private & OPpCONST_BARE))
4627 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
4628 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) ||
4629 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
4630 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
4632 if (other->op_type == OP_CONST)
4633 other->op_private |= OPpCONST_SHORTCIRCUIT;
4635 OP *newop = newUNOP(OP_NULL, 0, other);
4636 op_getmad(first, newop, '1');
4637 newop->op_targ = type; /* set "was" field */
4641 if (other->op_type == OP_LEAVE)
4642 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
4646 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
4647 const OP *o2 = other;
4648 if ( ! (o2->op_type == OP_LIST
4649 && (( o2 = cUNOPx(o2)->op_first))
4650 && o2->op_type == OP_PUSHMARK
4651 && (( o2 = o2->op_sibling)) )
4654 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
4655 || o2->op_type == OP_PADHV)
4656 && o2->op_private & OPpLVAL_INTRO
4657 && !(o2->op_private & OPpPAD_STATE))
4659 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
4660 "Deprecated use of my() in false conditional");
4664 if (first->op_type == OP_CONST)
4665 first->op_private |= OPpCONST_SHORTCIRCUIT;
4667 first = newUNOP(OP_NULL, 0, first);
4668 op_getmad(other, first, '2');
4669 first->op_targ = type; /* set "was" field */
4676 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
4677 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
4679 const OP * const k1 = ((UNOP*)first)->op_first;
4680 const OP * const k2 = k1->op_sibling;
4682 switch (first->op_type)
4685 if (k2 && k2->op_type == OP_READLINE
4686 && (k2->op_flags & OPf_STACKED)
4687 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
4689 warnop = k2->op_type;
4694 if (k1->op_type == OP_READDIR
4695 || k1->op_type == OP_GLOB
4696 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
4697 || k1->op_type == OP_EACH)
4699 warnop = ((k1->op_type == OP_NULL)
4700 ? (OPCODE)k1->op_targ : k1->op_type);
4705 const line_t oldline = CopLINE(PL_curcop);
4706 CopLINE_set(PL_curcop, PL_parser->copline);
4707 Perl_warner(aTHX_ packWARN(WARN_MISC),
4708 "Value of %s%s can be \"0\"; test with defined()",
4710 ((warnop == OP_READLINE || warnop == OP_GLOB)
4711 ? " construct" : "() operator"));
4712 CopLINE_set(PL_curcop, oldline);
4719 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
4720 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
4722 NewOp(1101, logop, 1, LOGOP);
4724 logop->op_type = (OPCODE)type;
4725 logop->op_ppaddr = PL_ppaddr[type];
4726 logop->op_first = first;
4727 logop->op_flags = (U8)(flags | OPf_KIDS);
4728 logop->op_other = LINKLIST(other);
4729 logop->op_private = (U8)(1 | (flags >> 8));
4731 /* establish postfix order */
4732 logop->op_next = LINKLIST(first);
4733 first->op_next = (OP*)logop;
4734 first->op_sibling = other;
4736 CHECKOP(type,logop);
4738 o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 0, (OP*)logop);
4745 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
4753 PERL_ARGS_ASSERT_NEWCONDOP;
4756 return newLOGOP(OP_AND, 0, first, trueop);
4758 return newLOGOP(OP_OR, 0, first, falseop);
4760 scalarboolean(first);
4761 if ((cstop = search_const(first))) {
4762 /* Left or right arm of the conditional? */
4763 const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
4764 OP *live = left ? trueop : falseop;
4765 OP *const dead = left ? falseop : trueop;
4766 if (cstop->op_private & OPpCONST_BARE &&
4767 cstop->op_private & OPpCONST_STRICT) {
4768 no_bareword_allowed(cstop);
4771 /* This is all dead code when PERL_MAD is not defined. */
4772 live = newUNOP(OP_NULL, 0, live);
4773 op_getmad(first, live, 'C');
4774 op_getmad(dead, live, left ? 'e' : 't');
4779 if (live->op_type == OP_LEAVE)
4780 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
4783 NewOp(1101, logop, 1, LOGOP);
4784 logop->op_type = OP_COND_EXPR;
4785 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
4786 logop->op_first = first;
4787 logop->op_flags = (U8)(flags | OPf_KIDS);
4788 logop->op_private = (U8)(1 | (flags >> 8));
4789 logop->op_other = LINKLIST(trueop);
4790 logop->op_next = LINKLIST(falseop);
4792 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
4795 /* establish postfix order */
4796 start = LINKLIST(first);
4797 first->op_next = (OP*)logop;
4799 first->op_sibling = trueop;
4800 trueop->op_sibling = falseop;
4801 o = newUNOP(OP_NULL, 0, (OP*)logop);
4803 trueop->op_next = falseop->op_next = o;
4810 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)