This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
op.c: Missing line break
[perl5.git] / op.c
... / ...
CommitLineData
1#line 2 "op.c"
2/* op.c
3 *
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
6 *
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
9 *
10 */
11
12/*
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
18 *
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
20 */
21
22/* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
24 *
25 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
31 * stack.
32 *
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
37 *
38 * newBINOP(OP_ADD, flags,
39 * newSVREF($a),
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
41 * )
42 *
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
45 */
46
47/*
48Perl's compiler is essentially a 3-pass compiler with interleaved phases:
49
50 A bottom-up pass
51 A top-down pass
52 An execution-order pass
53
54The bottom-up pass is represented by all the "newOP" routines and
55the ck_ routines. The bottom-upness is actually driven by yacc.
56So at the point that a ck_ routine fires, we have no idea what the
57context is, either upward in the syntax tree, or either forward or
58backward in the execution order. (The bottom-up parser builds that
59part of the execution order it knows about, but if you follow the "next"
60links around, you'll find it's actually a closed loop through the
61top level node.)
62
63Whenever the bottom-up parser gets to a node that supplies context to
64its components, it invokes that portion of the top-down pass that applies
65to that part of the subtree (and marks the top node as processed, so
66if a node further up supplies context, it doesn't have to take the
67plunge again). As a particular subcase of this, as the new node is
68built, it takes all the closed execution loops of its subcomponents
69and links them into a new closed loop for the higher level node. But
70it's still not the real execution order.
71
72The actual execution order is not known till we get a grammar reduction
73to a top-level unit like a subroutine or file that will be called by
74"name" rather than via a "next" pointer. At that point, we can call
75into peep() to do that code's portion of the 3rd pass. It has to be
76recursive, but it's recursive on basic blocks, not on tree nodes.
77*/
78
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.
91
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
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.
99*/
100
101#include "EXTERN.h"
102#define PERL_IN_OP_C
103#include "perl.h"
104#include "keywords.h"
105#include "feature.h"
106#include "regcomp.h"
107
108#define CALL_PEEP(o) PL_peepp(aTHX_ o)
109#define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110#define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
111
112/* See the explanatory comments above struct opslab in op.h. */
113
114#ifdef PERL_DEBUG_READONLY_OPS
115# define PERL_SLAB_SIZE 128
116# define PERL_MAX_SLAB_SIZE 4096
117# include <sys/mman.h>
118#endif
119
120#ifndef PERL_SLAB_SIZE
121# define PERL_SLAB_SIZE 64
122#endif
123#ifndef PERL_MAX_SLAB_SIZE
124# define PERL_MAX_SLAB_SIZE 2048
125#endif
126
127/* rounds up to nearest pointer */
128#define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
129#define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
130
131static OPSLAB *
132S_new_slab(pTHX_ size_t sz)
133{
134#ifdef PERL_DEBUG_READONLY_OPS
135 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
136 PROT_READ|PROT_WRITE,
137 MAP_ANON|MAP_PRIVATE, -1, 0);
138 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
139 (unsigned long) sz, slab));
140 if (slab == MAP_FAILED) {
141 perror("mmap failed");
142 abort();
143 }
144 slab->opslab_size = (U16)sz;
145#else
146 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
147#endif
148 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
149 return slab;
150}
151
152/* requires double parens and aTHX_ */
153#define DEBUG_S_warn(args) \
154 DEBUG_S( \
155 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
156 )
157
158void *
159Perl_Slab_Alloc(pTHX_ size_t sz)
160{
161 dVAR;
162 OPSLAB *slab;
163 OPSLAB *slab2;
164 OPSLOT *slot;
165 OP *o;
166 size_t opsz, space;
167
168 if (!PL_compcv || CvROOT(PL_compcv)
169 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
170 return PerlMemShared_calloc(1, sz);
171
172 if (!CvSTART(PL_compcv)) { /* sneak it in here */
173 CvSTART(PL_compcv) =
174 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
175 CvSLABBED_on(PL_compcv);
176 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
177 }
178 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
179
180 opsz = SIZE_TO_PSIZE(sz);
181 sz = opsz + OPSLOT_HEADER_P;
182
183 if (slab->opslab_freed) {
184 OP **too = &slab->opslab_freed;
185 o = *too;
186 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", o, slab));
187 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
188 DEBUG_S_warn((aTHX_ "Alas! too small"));
189 o = *(too = &o->op_next);
190 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", o)); }
191 }
192 if (o) {
193 *too = o->op_next;
194 Zero(o, opsz, I32 *);
195 o->op_slabbed = 1;
196 return (void *)o;
197 }
198 }
199
200#define INIT_OPSLOT \
201 slot->opslot_slab = slab; \
202 slot->opslot_next = slab2->opslab_first; \
203 slab2->opslab_first = slot; \
204 o = &slot->opslot_op; \
205 o->op_slabbed = 1
206
207 /* The partially-filled slab is next in the chain. */
208 slab2 = slab->opslab_next ? slab->opslab_next : slab;
209 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
210 /* Remaining space is too small. */
211
212 /* If we can fit a BASEOP, add it to the free chain, so as not
213 to waste it. */
214 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
215 slot = &slab2->opslab_slots;
216 INIT_OPSLOT;
217 o->op_type = OP_FREED;
218 o->op_next = slab->opslab_freed;
219 slab->opslab_freed = o;
220 }
221
222 /* Create a new slab. Make this one twice as big. */
223 slot = slab2->opslab_first;
224 while (slot->opslot_next) slot = slot->opslot_next;
225 slab2 = S_new_slab(aTHX_
226 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
227 ? PERL_MAX_SLAB_SIZE
228 : (DIFF(slab2, slot)+1)*2);
229 slab2->opslab_next = slab->opslab_next;
230 slab->opslab_next = slab2;
231 }
232 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
233
234 /* Create a new op slot */
235 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
236 assert(slot >= &slab2->opslab_slots);
237 if (DIFF(&slab2->opslab_slots, slot)
238 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
239 slot = &slab2->opslab_slots;
240 INIT_OPSLOT;
241 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", o, slab));
242 return (void *)o;
243}
244
245#undef INIT_OPSLOT
246
247#ifdef PERL_DEBUG_READONLY_OPS
248void
249Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
250{
251 PERL_ARGS_ASSERT_SLAB_TO_RO;
252
253 if (slab->opslab_readonly) return;
254 slab->opslab_readonly = 1;
255 for (; slab; slab = slab->opslab_next) {
256 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
257 (unsigned long) slab->opslab_size, slab));*/
258 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
259 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
260 (unsigned long)slab->opslab_size, errno);
261 }
262}
263
264STATIC void
265S_Slab_to_rw(pTHX_ void *op)
266{
267 OP * const o = (OP *)op;
268 OPSLAB *slab;
269 OPSLAB *slab2;
270
271 PERL_ARGS_ASSERT_SLAB_TO_RW;
272
273 if (!o->op_slabbed) return;
274
275 slab = OpSLAB(o);
276 if (!slab->opslab_readonly) return;
277 slab2 = slab;
278 for (; slab2; slab2 = slab2->opslab_next) {
279 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
280 (unsigned long) size, slab2));*/
281 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
282 PROT_READ|PROT_WRITE)) {
283 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
284 (unsigned long)slab2->opslab_size, errno);
285 }
286 }
287 slab->opslab_readonly = 0;
288}
289
290#else
291# define Slab_to_rw(op)
292#endif
293
294/* This cannot possibly be right, but it was copied from the old slab
295 allocator, to which it was originally added, without explanation, in
296 commit 083fcd5. */
297#ifdef NETWARE
298# define PerlMemShared PerlMem
299#endif
300
301void
302Perl_Slab_Free(pTHX_ void *op)
303{
304 dVAR;
305 OP * const o = (OP *)op;
306 OPSLAB *slab;
307
308 PERL_ARGS_ASSERT_SLAB_FREE;
309
310 if (!o->op_slabbed) {
311 PerlMemShared_free(op);
312 return;
313 }
314
315 slab = OpSLAB(o);
316 /* If this op is already freed, our refcount will get screwy. */
317 assert(o->op_type != OP_FREED);
318 o->op_type = OP_FREED;
319 o->op_next = slab->opslab_freed;
320 slab->opslab_freed = o;
321 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", o, slab));
322 OpslabREFCNT_dec_padok(slab);
323}
324
325void
326Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
327{
328 dVAR;
329 const bool havepad = !!PL_comppad;
330 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
331 if (havepad) {
332 ENTER;
333 PAD_SAVE_SETNULLPAD();
334 }
335 opslab_free(slab);
336 if (havepad) LEAVE;
337}
338
339void
340Perl_opslab_free(pTHX_ OPSLAB *slab)
341{
342 dVAR;
343 OPSLAB *slab2;
344 PERL_ARGS_ASSERT_OPSLAB_FREE;
345 DEBUG_S_warn((aTHX_ "freeing slab %p", slab));
346 assert(slab->opslab_refcnt == 1);
347 for (; slab; slab = slab2) {
348 slab2 = slab->opslab_next;
349#ifdef DEBUGGING
350 slab->opslab_refcnt = ~(size_t)0;
351#endif
352#ifdef PERL_DEBUG_READONLY_OPS
353 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
354 slab));
355 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
356 perror("munmap failed");
357 abort();
358 }
359#else
360 PerlMemShared_free(slab);
361#endif
362 }
363}
364
365void
366Perl_opslab_force_free(pTHX_ OPSLAB *slab)
367{
368 OPSLAB *slab2;
369 OPSLOT *slot;
370#ifdef DEBUGGING
371 size_t savestack_count = 0;
372#endif
373 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
374 slab2 = slab;
375 do {
376 for (slot = slab2->opslab_first;
377 slot->opslot_next;
378 slot = slot->opslot_next) {
379 if (slot->opslot_op.op_type != OP_FREED
380 && !(slot->opslot_op.op_savefree
381#ifdef DEBUGGING
382 && ++savestack_count
383#endif
384 )
385 ) {
386 assert(slot->opslot_op.op_slabbed);
387 slab->opslab_refcnt++; /* op_free may free slab */
388 op_free(&slot->opslot_op);
389 if (!--slab->opslab_refcnt) goto free;
390 }
391 }
392 } while ((slab2 = slab2->opslab_next));
393 /* > 1 because the CV still holds a reference count. */
394 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
395#ifdef DEBUGGING
396 assert(savestack_count == slab->opslab_refcnt-1);
397#endif
398 return;
399 }
400 free:
401 opslab_free(slab);
402}
403
404#ifdef PERL_DEBUG_READONLY_OPS
405OP *
406Perl_op_refcnt_inc(pTHX_ OP *o)
407{
408 if(o) {
409 Slab_to_rw(o);
410 ++o->op_targ;
411 }
412 return o;
413
414}
415
416PADOFFSET
417Perl_op_refcnt_dec(pTHX_ OP *o)
418{
419 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
420 Slab_to_rw(o);
421 return --o->op_targ;
422}
423#endif
424/*
425 * In the following definition, the ", (OP*)0" is just to make the compiler
426 * think the expression is of the right type: croak actually does a Siglongjmp.
427 */
428#define CHECKOP(type,o) \
429 ((PL_op_mask && PL_op_mask[type]) \
430 ? ( op_free((OP*)o), \
431 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
432 (OP*)0 ) \
433 : PL_check[type](aTHX_ (OP*)o))
434
435#define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
436
437#define CHANGE_TYPE(o,type) \
438 STMT_START { \
439 o->op_type = (OPCODE)type; \
440 o->op_ppaddr = PL_ppaddr[type]; \
441 } STMT_END
442
443STATIC SV*
444S_gv_ename(pTHX_ GV *gv)
445{
446 SV* const tmpsv = sv_newmortal();
447
448 PERL_ARGS_ASSERT_GV_ENAME;
449
450 gv_efullname3(tmpsv, gv, NULL);
451 return tmpsv;
452}
453
454STATIC OP *
455S_no_fh_allowed(pTHX_ OP *o)
456{
457 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
458
459 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
460 OP_DESC(o)));
461 return o;
462}
463
464STATIC OP *
465S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
466{
467 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
468 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, namesv),
469 SvUTF8(namesv) | flags);
470 return o;
471}
472
473STATIC OP *
474S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
475{
476 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
477 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
478 return o;
479}
480
481STATIC OP *
482S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
483{
484 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
485
486 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
487 return o;
488}
489
490STATIC OP *
491S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
492{
493 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
494
495 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
496 SvUTF8(namesv) | flags);
497 return o;
498}
499
500STATIC void
501S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
502{
503 PERL_ARGS_ASSERT_BAD_TYPE_PV;
504
505 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
506 (int)n, name, t, OP_DESC(kid)), flags);
507}
508
509STATIC void
510S_bad_type_sv(pTHX_ I32 n, const char *t, SV *namesv, U32 flags, const OP *kid)
511{
512 PERL_ARGS_ASSERT_BAD_TYPE_SV;
513
514 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
515 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
516}
517
518STATIC void
519S_no_bareword_allowed(pTHX_ OP *o)
520{
521 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
522
523 if (PL_madskills)
524 return; /* various ok barewords are hidden in extra OP_NULL */
525 qerror(Perl_mess(aTHX_
526 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
527 SVfARG(cSVOPo_sv)));
528 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
529}
530
531/* "register" allocation */
532
533PADOFFSET
534Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
535{
536 dVAR;
537 PADOFFSET off;
538 const bool is_our = (PL_parser->in_my == KEY_our);
539
540 PERL_ARGS_ASSERT_ALLOCMY;
541
542 if (flags & ~SVf_UTF8)
543 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
544 (UV)flags);
545
546 /* Until we're using the length for real, cross check that we're being
547 told the truth. */
548 assert(strlen(name) == len);
549
550 /* complain about "my $<special_var>" etc etc */
551 if (len &&
552 !(is_our ||
553 isALPHA(name[1]) ||
554 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
555 (name[1] == '_' && (*name == '$' || len > 2))))
556 {
557 /* name[2] is true if strlen(name) > 2 */
558 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
559 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
560 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
561 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
562 PL_parser->in_my == KEY_state ? "state" : "my"));
563 } else {
564 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
565 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
566 }
567 }
568
569 /* allocate a spare slot and store the name in that slot */
570
571 off = pad_add_name_pvn(name, len,
572 (is_our ? padadd_OUR :
573 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
574 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
575 PL_parser->in_my_stash,
576 (is_our
577 /* $_ is always in main::, even with our */
578 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
579 : NULL
580 )
581 );
582 /* anon sub prototypes contains state vars should always be cloned,
583 * otherwise the state var would be shared between anon subs */
584
585 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
586 CvCLONE_on(PL_compcv);
587
588 return off;
589}
590
591/*
592=for apidoc alloccopstash
593
594Available only under threaded builds, this function allocates an entry in
595C<PL_stashpad> for the stash passed to it.
596
597=cut
598*/
599
600#ifdef USE_ITHREADS
601PADOFFSET
602Perl_alloccopstash(pTHX_ HV *hv)
603{
604 PADOFFSET off = 0, o = 1;
605 bool found_slot = FALSE;
606
607 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
608
609 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
610
611 for (; o < PL_stashpadmax; ++o) {
612 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
613 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
614 found_slot = TRUE, off = o;
615 }
616 if (!found_slot) {
617 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
618 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
619 off = PL_stashpadmax;
620 PL_stashpadmax += 10;
621 }
622
623 PL_stashpad[PL_stashpadix = off] = hv;
624 return off;
625}
626#endif
627
628/* free the body of an op without examining its contents.
629 * Always use this rather than FreeOp directly */
630
631static void
632S_op_destroy(pTHX_ OP *o)
633{
634 if (o->op_latefree) {
635 o->op_latefreed = 1;
636 return;
637 }
638 FreeOp(o);
639}
640
641#ifdef USE_ITHREADS
642# define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b)
643#else
644# define forget_pmop(a,b) S_forget_pmop(aTHX_ a)
645#endif
646
647/* Destructor */
648
649void
650Perl_op_free(pTHX_ OP *o)
651{
652 dVAR;
653 OPCODE type;
654
655 /* Though ops may be freed twice, freeing the op after its slab is a
656 big no-no. */
657 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
658 /* During the forced freeing of ops after compilation failure, kidops
659 may be freed before their parents. */
660 if (!o || o->op_type == OP_FREED)
661 return;
662 if (o->op_latefreed) {
663 if (o->op_latefree)
664 return;
665 goto do_free;
666 }
667
668 type = o->op_type;
669 if (o->op_private & OPpREFCOUNTED) {
670 switch (type) {
671 case OP_LEAVESUB:
672 case OP_LEAVESUBLV:
673 case OP_LEAVEEVAL:
674 case OP_LEAVE:
675 case OP_SCOPE:
676 case OP_LEAVEWRITE:
677 {
678 PADOFFSET refcnt;
679 OP_REFCNT_LOCK;
680 refcnt = OpREFCNT_dec(o);
681 OP_REFCNT_UNLOCK;
682 if (refcnt) {
683 /* Need to find and remove any pattern match ops from the list
684 we maintain for reset(). */
685 find_and_forget_pmops(o);
686 return;
687 }
688 }
689 break;
690 default:
691 break;
692 }
693 }
694
695 /* Call the op_free hook if it has been set. Do it now so that it's called
696 * at the right time for refcounted ops, but still before all of the kids
697 * are freed. */
698 CALL_OPFREEHOOK(o);
699
700 if (o->op_flags & OPf_KIDS) {
701 register OP *kid, *nextkid;
702 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
703 nextkid = kid->op_sibling; /* Get before next freeing kid */
704 op_free(kid);
705 }
706 }
707
708 Slab_to_rw(o);
709
710 /* COP* is not cleared by op_clear() so that we may track line
711 * numbers etc even after null() */
712 if (type == OP_NEXTSTATE || type == OP_DBSTATE
713 || (type == OP_NULL /* the COP might have been null'ed */
714 && ((OPCODE)o->op_targ == OP_NEXTSTATE
715 || (OPCODE)o->op_targ == OP_DBSTATE))) {
716 cop_free((COP*)o);
717 }
718
719 if (type == OP_NULL)
720 type = (OPCODE)o->op_targ;
721
722 op_clear(o);
723 if (o->op_latefree) {
724 o->op_latefreed = 1;
725 return;
726 }
727 do_free:
728 FreeOp(o);
729#ifdef DEBUG_LEAKING_SCALARS
730 if (PL_op == o)
731 PL_op = NULL;
732#endif
733}
734
735void
736Perl_op_clear(pTHX_ OP *o)
737{
738
739 dVAR;
740
741 PERL_ARGS_ASSERT_OP_CLEAR;
742
743#ifdef PERL_MAD
744 mad_free(o->op_madprop);
745 o->op_madprop = 0;
746#endif
747
748 retry:
749 switch (o->op_type) {
750 case OP_NULL: /* Was holding old type, if any. */
751 if (PL_madskills && o->op_targ != OP_NULL) {
752 o->op_type = (Optype)o->op_targ;
753 o->op_targ = 0;
754 goto retry;
755 }
756 case OP_ENTERTRY:
757 case OP_ENTEREVAL: /* Was holding hints. */
758 o->op_targ = 0;
759 break;
760 default:
761 if (!(o->op_flags & OPf_REF)
762 || (PL_check[o->op_type] != Perl_ck_ftst))
763 break;
764 /* FALL THROUGH */
765 case OP_GVSV:
766 case OP_GV:
767 case OP_AELEMFAST:
768 {
769 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
770#ifdef USE_ITHREADS
771 && PL_curpad
772#endif
773 ? cGVOPo_gv : NULL;
774 /* It's possible during global destruction that the GV is freed
775 before the optree. Whilst the SvREFCNT_inc is happy to bump from
776 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
777 will trigger an assertion failure, because the entry to sv_clear
778 checks that the scalar is not already freed. A check of for
779 !SvIS_FREED(gv) turns out to be invalid, because during global
780 destruction the reference count can be forced down to zero
781 (with SVf_BREAK set). In which case raising to 1 and then
782 dropping to 0 triggers cleanup before it should happen. I
783 *think* that this might actually be a general, systematic,
784 weakness of the whole idea of SVf_BREAK, in that code *is*
785 allowed to raise and lower references during global destruction,
786 so any *valid* code that happens to do this during global
787 destruction might well trigger premature cleanup. */
788 bool still_valid = gv && SvREFCNT(gv);
789
790 if (still_valid)
791 SvREFCNT_inc_simple_void(gv);
792#ifdef USE_ITHREADS
793 if (cPADOPo->op_padix > 0) {
794 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
795 * may still exist on the pad */
796 pad_swipe(cPADOPo->op_padix, TRUE);
797 cPADOPo->op_padix = 0;
798 }
799#else
800 SvREFCNT_dec(cSVOPo->op_sv);
801 cSVOPo->op_sv = NULL;
802#endif
803 if (still_valid) {
804 int try_downgrade = SvREFCNT(gv) == 2;
805 SvREFCNT_dec(gv);
806 if (try_downgrade)
807 gv_try_downgrade(gv);
808 }
809 }
810 break;
811 case OP_METHOD_NAMED:
812 case OP_CONST:
813 case OP_HINTSEVAL:
814 SvREFCNT_dec(cSVOPo->op_sv);
815 cSVOPo->op_sv = NULL;
816#ifdef USE_ITHREADS
817 /** Bug #15654
818 Even if op_clear does a pad_free for the target of the op,
819 pad_free doesn't actually remove the sv that exists in the pad;
820 instead it lives on. This results in that it could be reused as
821 a target later on when the pad was reallocated.
822 **/
823 if(o->op_targ) {
824 pad_swipe(o->op_targ,1);
825 o->op_targ = 0;
826 }
827#endif
828 break;
829 case OP_GOTO:
830 case OP_NEXT:
831 case OP_LAST:
832 case OP_REDO:
833 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
834 break;
835 /* FALL THROUGH */
836 case OP_TRANS:
837 case OP_TRANSR:
838 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
839#ifdef USE_ITHREADS
840 if (cPADOPo->op_padix > 0) {
841 pad_swipe(cPADOPo->op_padix, TRUE);
842 cPADOPo->op_padix = 0;
843 }
844#else
845 SvREFCNT_dec(cSVOPo->op_sv);
846 cSVOPo->op_sv = NULL;
847#endif
848 }
849 else {
850 PerlMemShared_free(cPVOPo->op_pv);
851 cPVOPo->op_pv = NULL;
852 }
853 break;
854 case OP_SUBST:
855 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
856 goto clear_pmop;
857 case OP_PUSHRE:
858#ifdef USE_ITHREADS
859 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
860 /* No GvIN_PAD_off here, because other references may still
861 * exist on the pad */
862 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
863 }
864#else
865 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
866#endif
867 /* FALL THROUGH */
868 case OP_MATCH:
869 case OP_QR:
870clear_pmop:
871 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
872 op_free(cPMOPo->op_code_list);
873 cPMOPo->op_code_list = NULL;
874 forget_pmop(cPMOPo, 1);
875 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
876 /* we use the same protection as the "SAFE" version of the PM_ macros
877 * here since sv_clean_all might release some PMOPs
878 * after PL_regex_padav has been cleared
879 * and the clearing of PL_regex_padav needs to
880 * happen before sv_clean_all
881 */
882#ifdef USE_ITHREADS
883 if(PL_regex_pad) { /* We could be in destruction */
884 const IV offset = (cPMOPo)->op_pmoffset;
885 ReREFCNT_dec(PM_GETRE(cPMOPo));
886 PL_regex_pad[offset] = &PL_sv_undef;
887 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
888 sizeof(offset));
889 }
890#else
891 ReREFCNT_dec(PM_GETRE(cPMOPo));
892 PM_SETRE(cPMOPo, NULL);
893#endif
894
895 break;
896 }
897
898 if (o->op_targ > 0) {
899 pad_free(o->op_targ);
900 o->op_targ = 0;
901 }
902}
903
904STATIC void
905S_cop_free(pTHX_ COP* cop)
906{
907 PERL_ARGS_ASSERT_COP_FREE;
908
909 CopFILE_free(cop);
910 if (! specialWARN(cop->cop_warnings))
911 PerlMemShared_free(cop->cop_warnings);
912 cophh_free(CopHINTHASH_get(cop));
913}
914
915STATIC void
916S_forget_pmop(pTHX_ PMOP *const o
917#ifdef USE_ITHREADS
918 , U32 flags
919#endif
920 )
921{
922 HV * const pmstash = PmopSTASH(o);
923
924 PERL_ARGS_ASSERT_FORGET_PMOP;
925
926 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
927 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
928 if (mg) {
929 PMOP **const array = (PMOP**) mg->mg_ptr;
930 U32 count = mg->mg_len / sizeof(PMOP**);
931 U32 i = count;
932
933 while (i--) {
934 if (array[i] == o) {
935 /* Found it. Move the entry at the end to overwrite it. */
936 array[i] = array[--count];
937 mg->mg_len = count * sizeof(PMOP**);
938 /* Could realloc smaller at this point always, but probably
939 not worth it. Probably worth free()ing if we're the
940 last. */
941 if(!count) {
942 Safefree(mg->mg_ptr);
943 mg->mg_ptr = NULL;
944 }
945 break;
946 }
947 }
948 }
949 }
950 if (PL_curpm == o)
951 PL_curpm = NULL;
952#ifdef USE_ITHREADS
953 if (flags)
954 PmopSTASH_free(o);
955#endif
956}
957
958STATIC void
959S_find_and_forget_pmops(pTHX_ OP *o)
960{
961 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
962
963 if (o->op_flags & OPf_KIDS) {
964 OP *kid = cUNOPo->op_first;
965 while (kid) {
966 switch (kid->op_type) {
967 case OP_SUBST:
968 case OP_PUSHRE:
969 case OP_MATCH:
970 case OP_QR:
971 forget_pmop((PMOP*)kid, 0);
972 }
973 find_and_forget_pmops(kid);
974 kid = kid->op_sibling;
975 }
976 }
977}
978
979void
980Perl_op_null(pTHX_ OP *o)
981{
982 dVAR;
983
984 PERL_ARGS_ASSERT_OP_NULL;
985
986 if (o->op_type == OP_NULL)
987 return;
988 if (!PL_madskills)
989 op_clear(o);
990 o->op_targ = o->op_type;
991 o->op_type = OP_NULL;
992 o->op_ppaddr = PL_ppaddr[OP_NULL];
993}
994
995void
996Perl_op_refcnt_lock(pTHX)
997{
998 dVAR;
999 PERL_UNUSED_CONTEXT;
1000 OP_REFCNT_LOCK;
1001}
1002
1003void
1004Perl_op_refcnt_unlock(pTHX)
1005{
1006 dVAR;
1007 PERL_UNUSED_CONTEXT;
1008 OP_REFCNT_UNLOCK;
1009}
1010
1011/* Contextualizers */
1012
1013/*
1014=for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1015
1016Applies a syntactic context to an op tree representing an expression.
1017I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1018or C<G_VOID> to specify the context to apply. The modified op tree
1019is returned.
1020
1021=cut
1022*/
1023
1024OP *
1025Perl_op_contextualize(pTHX_ OP *o, I32 context)
1026{
1027 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1028 switch (context) {
1029 case G_SCALAR: return scalar(o);
1030 case G_ARRAY: return list(o);
1031 case G_VOID: return scalarvoid(o);
1032 default:
1033 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1034 (long) context);
1035 return o;
1036 }
1037}
1038
1039/*
1040=head1 Optree Manipulation Functions
1041
1042=for apidoc Am|OP*|op_linklist|OP *o
1043This function is the implementation of the L</LINKLIST> macro. It should
1044not be called directly.
1045
1046=cut
1047*/
1048
1049OP *
1050Perl_op_linklist(pTHX_ OP *o)
1051{
1052 OP *first;
1053
1054 PERL_ARGS_ASSERT_OP_LINKLIST;
1055
1056 if (o->op_next)
1057 return o->op_next;
1058
1059 /* establish postfix order */
1060 first = cUNOPo->op_first;
1061 if (first) {
1062 register OP *kid;
1063 o->op_next = LINKLIST(first);
1064 kid = first;
1065 for (;;) {
1066 if (kid->op_sibling) {
1067 kid->op_next = LINKLIST(kid->op_sibling);
1068 kid = kid->op_sibling;
1069 } else {
1070 kid->op_next = o;
1071 break;
1072 }
1073 }
1074 }
1075 else
1076 o->op_next = o;
1077
1078 return o->op_next;
1079}
1080
1081static OP *
1082S_scalarkids(pTHX_ OP *o)
1083{
1084 if (o && o->op_flags & OPf_KIDS) {
1085 OP *kid;
1086 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1087 scalar(kid);
1088 }
1089 return o;
1090}
1091
1092STATIC OP *
1093S_scalarboolean(pTHX_ OP *o)
1094{
1095 dVAR;
1096
1097 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1098
1099 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1100 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1101 if (ckWARN(WARN_SYNTAX)) {
1102 const line_t oldline = CopLINE(PL_curcop);
1103
1104 if (PL_parser && PL_parser->copline != NOLINE)
1105 CopLINE_set(PL_curcop, PL_parser->copline);
1106 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1107 CopLINE_set(PL_curcop, oldline);
1108 }
1109 }
1110 return scalar(o);
1111}
1112
1113OP *
1114Perl_scalar(pTHX_ OP *o)
1115{
1116 dVAR;
1117 OP *kid;
1118
1119 /* assumes no premature commitment */
1120 if (!o || (PL_parser && PL_parser->error_count)
1121 || (o->op_flags & OPf_WANT)
1122 || o->op_type == OP_RETURN)
1123 {
1124 return o;
1125 }
1126
1127 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1128
1129 switch (o->op_type) {
1130 case OP_REPEAT:
1131 scalar(cBINOPo->op_first);
1132 break;
1133 case OP_OR:
1134 case OP_AND:
1135 case OP_COND_EXPR:
1136 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1137 scalar(kid);
1138 break;
1139 /* FALL THROUGH */
1140 case OP_SPLIT:
1141 case OP_MATCH:
1142 case OP_QR:
1143 case OP_SUBST:
1144 case OP_NULL:
1145 default:
1146 if (o->op_flags & OPf_KIDS) {
1147 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1148 scalar(kid);
1149 }
1150 break;
1151 case OP_LEAVE:
1152 case OP_LEAVETRY:
1153 kid = cLISTOPo->op_first;
1154 scalar(kid);
1155 kid = kid->op_sibling;
1156 do_kids:
1157 while (kid) {
1158 OP *sib = kid->op_sibling;
1159 if (sib && kid->op_type != OP_LEAVEWHEN)
1160 scalarvoid(kid);
1161 else
1162 scalar(kid);
1163 kid = sib;
1164 }
1165 PL_curcop = &PL_compiling;
1166 break;
1167 case OP_SCOPE:
1168 case OP_LINESEQ:
1169 case OP_LIST:
1170 kid = cLISTOPo->op_first;
1171 goto do_kids;
1172 case OP_SORT:
1173 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1174 break;
1175 }
1176 return o;
1177}
1178
1179OP *
1180Perl_scalarvoid(pTHX_ OP *o)
1181{
1182 dVAR;
1183 OP *kid;
1184 const char* useless = NULL;
1185 U32 useless_is_utf8 = 0;
1186 SV* sv;
1187 U8 want;
1188
1189 PERL_ARGS_ASSERT_SCALARVOID;
1190
1191 /* trailing mad null ops don't count as "there" for void processing */
1192 if (PL_madskills &&
1193 o->op_type != OP_NULL &&
1194 o->op_sibling &&
1195 o->op_sibling->op_type == OP_NULL)
1196 {
1197 OP *sib;
1198 for (sib = o->op_sibling;
1199 sib && sib->op_type == OP_NULL;
1200 sib = sib->op_sibling) ;
1201
1202 if (!sib)
1203 return o;
1204 }
1205
1206 if (o->op_type == OP_NEXTSTATE
1207 || o->op_type == OP_DBSTATE
1208 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1209 || o->op_targ == OP_DBSTATE)))
1210 PL_curcop = (COP*)o; /* for warning below */
1211
1212 /* assumes no premature commitment */
1213 want = o->op_flags & OPf_WANT;
1214 if ((want && want != OPf_WANT_SCALAR)
1215 || (PL_parser && PL_parser->error_count)
1216 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1217 {
1218 return o;
1219 }
1220
1221 if ((o->op_private & OPpTARGET_MY)
1222 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1223 {
1224 return scalar(o); /* As if inside SASSIGN */
1225 }
1226
1227 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1228
1229 switch (o->op_type) {
1230 default:
1231 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1232 break;
1233 /* FALL THROUGH */
1234 case OP_REPEAT:
1235 if (o->op_flags & OPf_STACKED)
1236 break;
1237 goto func_ops;
1238 case OP_SUBSTR:
1239 if (o->op_private == 4)
1240 break;
1241 /* FALL THROUGH */
1242 case OP_GVSV:
1243 case OP_WANTARRAY:
1244 case OP_GV:
1245 case OP_SMARTMATCH:
1246 case OP_PADSV:
1247 case OP_PADAV:
1248 case OP_PADHV:
1249 case OP_PADANY:
1250 case OP_AV2ARYLEN:
1251 case OP_REF:
1252 case OP_REFGEN:
1253 case OP_SREFGEN:
1254 case OP_DEFINED:
1255 case OP_HEX:
1256 case OP_OCT:
1257 case OP_LENGTH:
1258 case OP_VEC:
1259 case OP_INDEX:
1260 case OP_RINDEX:
1261 case OP_SPRINTF:
1262 case OP_AELEM:
1263 case OP_AELEMFAST:
1264 case OP_AELEMFAST_LEX:
1265 case OP_ASLICE:
1266 case OP_HELEM:
1267 case OP_HSLICE:
1268 case OP_UNPACK:
1269 case OP_PACK:
1270 case OP_JOIN:
1271 case OP_LSLICE:
1272 case OP_ANONLIST:
1273 case OP_ANONHASH:
1274 case OP_SORT:
1275 case OP_REVERSE:
1276 case OP_RANGE:
1277 case OP_FLIP:
1278 case OP_FLOP:
1279 case OP_CALLER:
1280 case OP_FILENO:
1281 case OP_EOF:
1282 case OP_TELL:
1283 case OP_GETSOCKNAME:
1284 case OP_GETPEERNAME:
1285 case OP_READLINK:
1286 case OP_TELLDIR:
1287 case OP_GETPPID:
1288 case OP_GETPGRP:
1289 case OP_GETPRIORITY:
1290 case OP_TIME:
1291 case OP_TMS:
1292 case OP_LOCALTIME:
1293 case OP_GMTIME:
1294 case OP_GHBYNAME:
1295 case OP_GHBYADDR:
1296 case OP_GHOSTENT:
1297 case OP_GNBYNAME:
1298 case OP_GNBYADDR:
1299 case OP_GNETENT:
1300 case OP_GPBYNAME:
1301 case OP_GPBYNUMBER:
1302 case OP_GPROTOENT:
1303 case OP_GSBYNAME:
1304 case OP_GSBYPORT:
1305 case OP_GSERVENT:
1306 case OP_GPWNAM:
1307 case OP_GPWUID:
1308 case OP_GGRNAM:
1309 case OP_GGRGID:
1310 case OP_GETLOGIN:
1311 case OP_PROTOTYPE:
1312 case OP_RUNCV:
1313 func_ops:
1314 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1315 /* Otherwise it's "Useless use of grep iterator" */
1316 useless = OP_DESC(o);
1317 break;
1318
1319 case OP_SPLIT:
1320 kid = cLISTOPo->op_first;
1321 if (kid && kid->op_type == OP_PUSHRE
1322#ifdef USE_ITHREADS
1323 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1324#else
1325 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1326#endif
1327 useless = OP_DESC(o);
1328 break;
1329
1330 case OP_NOT:
1331 kid = cUNOPo->op_first;
1332 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1333 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1334 goto func_ops;
1335 }
1336 useless = "negative pattern binding (!~)";
1337 break;
1338
1339 case OP_SUBST:
1340 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1341 useless = "non-destructive substitution (s///r)";
1342 break;
1343
1344 case OP_TRANSR:
1345 useless = "non-destructive transliteration (tr///r)";
1346 break;
1347
1348 case OP_RV2GV:
1349 case OP_RV2SV:
1350 case OP_RV2AV:
1351 case OP_RV2HV:
1352 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1353 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1354 useless = "a variable";
1355 break;
1356
1357 case OP_CONST:
1358 sv = cSVOPo_sv;
1359 if (cSVOPo->op_private & OPpCONST_STRICT)
1360 no_bareword_allowed(o);
1361 else {
1362 if (ckWARN(WARN_VOID)) {
1363 /* don't warn on optimised away booleans, eg
1364 * use constant Foo, 5; Foo || print; */
1365 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1366 useless = NULL;
1367 /* the constants 0 and 1 are permitted as they are
1368 conventionally used as dummies in constructs like
1369 1 while some_condition_with_side_effects; */
1370 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1371 useless = NULL;
1372 else if (SvPOK(sv)) {
1373 /* perl4's way of mixing documentation and code
1374 (before the invention of POD) was based on a
1375 trick to mix nroff and perl code. The trick was
1376 built upon these three nroff macros being used in
1377 void context. The pink camel has the details in
1378 the script wrapman near page 319. */
1379 const char * const maybe_macro = SvPVX_const(sv);
1380 if (strnEQ(maybe_macro, "di", 2) ||
1381 strnEQ(maybe_macro, "ds", 2) ||
1382 strnEQ(maybe_macro, "ig", 2))
1383 useless = NULL;
1384 else {
1385 SV * const dsv = newSVpvs("");
1386 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1387 "a constant (%s)",
1388 pv_pretty(dsv, maybe_macro, SvCUR(sv), 32, NULL, NULL,
1389 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_NOCLEAR | PERL_PV_ESCAPE_UNI_DETECT )));
1390 SvREFCNT_dec(dsv);
1391 useless = SvPV_nolen(msv);
1392 useless_is_utf8 = SvUTF8(msv);
1393 }
1394 }
1395 else if (SvOK(sv)) {
1396 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1397 "a constant (%"SVf")", sv));
1398 useless = SvPV_nolen(msv);
1399 }
1400 else
1401 useless = "a constant (undef)";
1402 }
1403 }
1404 op_null(o); /* don't execute or even remember it */
1405 break;
1406
1407 case OP_POSTINC:
1408 o->op_type = OP_PREINC; /* pre-increment is faster */
1409 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1410 break;
1411
1412 case OP_POSTDEC:
1413 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1414 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1415 break;
1416
1417 case OP_I_POSTINC:
1418 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1419 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1420 break;
1421
1422 case OP_I_POSTDEC:
1423 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1424 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1425 break;
1426
1427 case OP_SASSIGN: {
1428 OP *rv2gv;
1429 UNOP *refgen, *rv2cv;
1430 LISTOP *exlist;
1431
1432 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1433 break;
1434
1435 rv2gv = ((BINOP *)o)->op_last;
1436 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1437 break;
1438
1439 refgen = (UNOP *)((BINOP *)o)->op_first;
1440
1441 if (!refgen || refgen->op_type != OP_REFGEN)
1442 break;
1443
1444 exlist = (LISTOP *)refgen->op_first;
1445 if (!exlist || exlist->op_type != OP_NULL
1446 || exlist->op_targ != OP_LIST)
1447 break;
1448
1449 if (exlist->op_first->op_type != OP_PUSHMARK)
1450 break;
1451
1452 rv2cv = (UNOP*)exlist->op_last;
1453
1454 if (rv2cv->op_type != OP_RV2CV)
1455 break;
1456
1457 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1458 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1459 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1460
1461 o->op_private |= OPpASSIGN_CV_TO_GV;
1462 rv2gv->op_private |= OPpDONT_INIT_GV;
1463 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1464
1465 break;
1466 }
1467
1468 case OP_AASSIGN: {
1469 inplace_aassign(o);
1470 break;
1471 }
1472
1473 case OP_OR:
1474 case OP_AND:
1475 kid = cLOGOPo->op_first;
1476 if (kid->op_type == OP_NOT
1477 && (kid->op_flags & OPf_KIDS)
1478 && !PL_madskills) {
1479 if (o->op_type == OP_AND) {
1480 o->op_type = OP_OR;
1481 o->op_ppaddr = PL_ppaddr[OP_OR];
1482 } else {
1483 o->op_type = OP_AND;
1484 o->op_ppaddr = PL_ppaddr[OP_AND];
1485 }
1486 op_null(kid);
1487 }
1488
1489 case OP_DOR:
1490 case OP_COND_EXPR:
1491 case OP_ENTERGIVEN:
1492 case OP_ENTERWHEN:
1493 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1494 scalarvoid(kid);
1495 break;
1496
1497 case OP_NULL:
1498 if (o->op_flags & OPf_STACKED)
1499 break;
1500 /* FALL THROUGH */
1501 case OP_NEXTSTATE:
1502 case OP_DBSTATE:
1503 case OP_ENTERTRY:
1504 case OP_ENTER:
1505 if (!(o->op_flags & OPf_KIDS))
1506 break;
1507 /* FALL THROUGH */
1508 case OP_SCOPE:
1509 case OP_LEAVE:
1510 case OP_LEAVETRY:
1511 case OP_LEAVELOOP:
1512 case OP_LINESEQ:
1513 case OP_LIST:
1514 case OP_LEAVEGIVEN:
1515 case OP_LEAVEWHEN:
1516 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1517 scalarvoid(kid);
1518 break;
1519 case OP_ENTEREVAL:
1520 scalarkids(o);
1521 break;
1522 case OP_SCALAR:
1523 return scalar(o);
1524 }
1525 if (useless)
1526 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %"SVf" in void context",
1527 newSVpvn_flags(useless, strlen(useless),
1528 SVs_TEMP | ( useless_is_utf8 ? SVf_UTF8 : 0 )));
1529 return o;
1530}
1531
1532static OP *
1533S_listkids(pTHX_ OP *o)
1534{
1535 if (o && o->op_flags & OPf_KIDS) {
1536 OP *kid;
1537 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1538 list(kid);
1539 }
1540 return o;
1541}
1542
1543OP *
1544Perl_list(pTHX_ OP *o)
1545{
1546 dVAR;
1547 OP *kid;
1548
1549 /* assumes no premature commitment */
1550 if (!o || (o->op_flags & OPf_WANT)
1551 || (PL_parser && PL_parser->error_count)
1552 || o->op_type == OP_RETURN)
1553 {
1554 return o;
1555 }
1556
1557 if ((o->op_private & OPpTARGET_MY)
1558 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1559 {
1560 return o; /* As if inside SASSIGN */
1561 }
1562
1563 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1564
1565 switch (o->op_type) {
1566 case OP_FLOP:
1567 case OP_REPEAT:
1568 list(cBINOPo->op_first);
1569 break;
1570 case OP_OR:
1571 case OP_AND:
1572 case OP_COND_EXPR:
1573 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1574 list(kid);
1575 break;
1576 default:
1577 case OP_MATCH:
1578 case OP_QR:
1579 case OP_SUBST:
1580 case OP_NULL:
1581 if (!(o->op_flags & OPf_KIDS))
1582 break;
1583 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1584 list(cBINOPo->op_first);
1585 return gen_constant_list(o);
1586 }
1587 case OP_LIST:
1588 listkids(o);
1589 break;
1590 case OP_LEAVE:
1591 case OP_LEAVETRY:
1592 kid = cLISTOPo->op_first;
1593 list(kid);
1594 kid = kid->op_sibling;
1595 do_kids:
1596 while (kid) {
1597 OP *sib = kid->op_sibling;
1598 if (sib && kid->op_type != OP_LEAVEWHEN)
1599 scalarvoid(kid);
1600 else
1601 list(kid);
1602 kid = sib;
1603 }
1604 PL_curcop = &PL_compiling;
1605 break;
1606 case OP_SCOPE:
1607 case OP_LINESEQ:
1608 kid = cLISTOPo->op_first;
1609 goto do_kids;
1610 }
1611 return o;
1612}
1613
1614static OP *
1615S_scalarseq(pTHX_ OP *o)
1616{
1617 dVAR;
1618 if (o) {
1619 const OPCODE type = o->op_type;
1620
1621 if (type == OP_LINESEQ || type == OP_SCOPE ||
1622 type == OP_LEAVE || type == OP_LEAVETRY)
1623 {
1624 OP *kid;
1625 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1626 if (kid->op_sibling) {
1627 scalarvoid(kid);
1628 }
1629 }
1630 PL_curcop = &PL_compiling;
1631 }
1632 o->op_flags &= ~OPf_PARENS;
1633 if (PL_hints & HINT_BLOCK_SCOPE)
1634 o->op_flags |= OPf_PARENS;
1635 }
1636 else
1637 o = newOP(OP_STUB, 0);
1638 return o;
1639}
1640
1641STATIC OP *
1642S_modkids(pTHX_ OP *o, I32 type)
1643{
1644 if (o && o->op_flags & OPf_KIDS) {
1645 OP *kid;
1646 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1647 op_lvalue(kid, type);
1648 }
1649 return o;
1650}
1651
1652/*
1653=for apidoc finalize_optree
1654
1655This function finalizes the optree. Should be called directly after
1656the complete optree is built. It does some additional
1657checking which can't be done in the normal ck_xxx functions and makes
1658the tree thread-safe.
1659
1660=cut
1661*/
1662void
1663Perl_finalize_optree(pTHX_ OP* o)
1664{
1665 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1666
1667 ENTER;
1668 SAVEVPTR(PL_curcop);
1669
1670 finalize_op(o);
1671
1672 LEAVE;
1673}
1674
1675STATIC void
1676S_finalize_op(pTHX_ OP* o)
1677{
1678 PERL_ARGS_ASSERT_FINALIZE_OP;
1679
1680#if defined(PERL_MAD) && defined(USE_ITHREADS)
1681 {
1682 /* Make sure mad ops are also thread-safe */
1683 MADPROP *mp = o->op_madprop;
1684 while (mp) {
1685 if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1686 OP *prop_op = (OP *) mp->mad_val;
1687 /* We only need "Relocate sv to the pad for thread safety.", but this
1688 easiest way to make sure it traverses everything */
1689 if (prop_op->op_type == OP_CONST)
1690 cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1691 finalize_op(prop_op);
1692 }
1693 mp = mp->mad_next;
1694 }
1695 }
1696#endif
1697
1698 switch (o->op_type) {
1699 case OP_NEXTSTATE:
1700 case OP_DBSTATE:
1701 PL_curcop = ((COP*)o); /* for warnings */
1702 break;
1703 case OP_EXEC:
1704 if ( o->op_sibling
1705 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1706 && ckWARN(WARN_SYNTAX))
1707 {
1708 if (o->op_sibling->op_sibling) {
1709 const OPCODE type = o->op_sibling->op_sibling->op_type;
1710 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1711 const line_t oldline = CopLINE(PL_curcop);
1712 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1713 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1714 "Statement unlikely to be reached");
1715 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1716 "\t(Maybe you meant system() when you said exec()?)\n");
1717 CopLINE_set(PL_curcop, oldline);
1718 }
1719 }
1720 }
1721 break;
1722
1723 case OP_GV:
1724 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1725 GV * const gv = cGVOPo_gv;
1726 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1727 /* XXX could check prototype here instead of just carping */
1728 SV * const sv = sv_newmortal();
1729 gv_efullname3(sv, gv, NULL);
1730 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1731 "%"SVf"() called too early to check prototype",
1732 SVfARG(sv));
1733 }
1734 }
1735 break;
1736
1737 case OP_CONST:
1738 if (cSVOPo->op_private & OPpCONST_STRICT)
1739 no_bareword_allowed(o);
1740 /* FALLTHROUGH */
1741#ifdef USE_ITHREADS
1742 case OP_HINTSEVAL:
1743 case OP_METHOD_NAMED:
1744 /* Relocate sv to the pad for thread safety.
1745 * Despite being a "constant", the SV is written to,
1746 * for reference counts, sv_upgrade() etc. */
1747 if (cSVOPo->op_sv) {
1748 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
1749 if (o->op_type != OP_METHOD_NAMED &&
1750 (SvPADTMP(cSVOPo->op_sv) || SvPADMY(cSVOPo->op_sv)))
1751 {
1752 /* If op_sv is already a PADTMP/MY then it is being used by
1753 * some pad, so make a copy. */
1754 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
1755 SvREADONLY_on(PAD_SVl(ix));
1756 SvREFCNT_dec(cSVOPo->op_sv);
1757 }
1758 else if (o->op_type != OP_METHOD_NAMED
1759 && cSVOPo->op_sv == &PL_sv_undef) {
1760 /* PL_sv_undef is hack - it's unsafe to store it in the
1761 AV that is the pad, because av_fetch treats values of
1762 PL_sv_undef as a "free" AV entry and will merrily
1763 replace them with a new SV, causing pad_alloc to think
1764 that this pad slot is free. (When, clearly, it is not)
1765 */
1766 SvOK_off(PAD_SVl(ix));
1767 SvPADTMP_on(PAD_SVl(ix));
1768 SvREADONLY_on(PAD_SVl(ix));
1769 }
1770 else {
1771 SvREFCNT_dec(PAD_SVl(ix));
1772 SvPADTMP_on(cSVOPo->op_sv);
1773 PAD_SETSV(ix, cSVOPo->op_sv);
1774 /* XXX I don't know how this isn't readonly already. */
1775 SvREADONLY_on(PAD_SVl(ix));
1776 }
1777 cSVOPo->op_sv = NULL;
1778 o->op_targ = ix;
1779 }
1780#endif
1781 break;
1782
1783 case OP_HELEM: {
1784 UNOP *rop;
1785 SV *lexname;
1786 GV **fields;
1787 SV **svp, *sv;
1788 const char *key = NULL;
1789 STRLEN keylen;
1790
1791 if (((BINOP*)o)->op_last->op_type != OP_CONST)
1792 break;
1793
1794 /* Make the CONST have a shared SV */
1795 svp = cSVOPx_svp(((BINOP*)o)->op_last);
1796 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv))
1797 && SvTYPE(sv) < SVt_PVMG && !SvROK(sv)) {
1798 key = SvPV_const(sv, keylen);
1799 lexname = newSVpvn_share(key,
1800 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
1801 0);
1802 SvREFCNT_dec(sv);
1803 *svp = lexname;
1804 }
1805
1806 if ((o->op_private & (OPpLVAL_INTRO)))
1807 break;
1808
1809 rop = (UNOP*)((BINOP*)o)->op_first;
1810 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
1811 break;
1812 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
1813 if (!SvPAD_TYPED(lexname))
1814 break;
1815 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1816 if (!fields || !GvHV(*fields))
1817 break;
1818 key = SvPV_const(*svp, keylen);
1819 if (!hv_fetch(GvHV(*fields), key,
1820 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1821 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1822 "in variable %"SVf" of type %"HEKf,
1823 SVfARG(*svp), SVfARG(lexname),
1824 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1825 }
1826 break;
1827 }
1828
1829 case OP_HSLICE: {
1830 UNOP *rop;
1831 SV *lexname;
1832 GV **fields;
1833 SV **svp;
1834 const char *key;
1835 STRLEN keylen;
1836 SVOP *first_key_op, *key_op;
1837
1838 if ((o->op_private & (OPpLVAL_INTRO))
1839 /* I bet there's always a pushmark... */
1840 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
1841 /* hmmm, no optimization if list contains only one key. */
1842 break;
1843 rop = (UNOP*)((LISTOP*)o)->op_last;
1844 if (rop->op_type != OP_RV2HV)
1845 break;
1846 if (rop->op_first->op_type == OP_PADSV)
1847 /* @$hash{qw(keys here)} */
1848 rop = (UNOP*)rop->op_first;
1849 else {
1850 /* @{$hash}{qw(keys here)} */
1851 if (rop->op_first->op_type == OP_SCOPE
1852 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
1853 {
1854 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
1855 }
1856 else
1857 break;
1858 }
1859
1860 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
1861 if (!SvPAD_TYPED(lexname))
1862 break;
1863 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1864 if (!fields || !GvHV(*fields))
1865 break;
1866 /* Again guessing that the pushmark can be jumped over.... */
1867 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
1868 ->op_first->op_sibling;
1869 for (key_op = first_key_op; key_op;
1870 key_op = (SVOP*)key_op->op_sibling) {
1871 if (key_op->op_type != OP_CONST)
1872 continue;
1873 svp = cSVOPx_svp(key_op);
1874 key = SvPV_const(*svp, keylen);
1875 if (!hv_fetch(GvHV(*fields), key,
1876 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1877 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1878 "in variable %"SVf" of type %"HEKf,
1879 SVfARG(*svp), SVfARG(lexname),
1880 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1881 }
1882 }
1883 break;
1884 }
1885 case OP_SUBST: {
1886 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1887 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1888 break;
1889 }
1890 default:
1891 break;
1892 }
1893
1894 if (o->op_flags & OPf_KIDS) {
1895 OP *kid;
1896 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1897 finalize_op(kid);
1898 }
1899}
1900
1901/*
1902=for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
1903
1904Propagate lvalue ("modifiable") context to an op and its children.
1905I<type> represents the context type, roughly based on the type of op that
1906would do the modifying, although C<local()> is represented by OP_NULL,
1907because it has no op type of its own (it is signalled by a flag on
1908the lvalue op).
1909
1910This function detects things that can't be modified, such as C<$x+1>, and
1911generates errors for them. For example, C<$x+1 = 2> would cause it to be
1912called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
1913
1914It also flags things that need to behave specially in an lvalue context,
1915such as C<$$x = 5> which might have to vivify a reference in C<$x>.
1916
1917=cut
1918*/
1919
1920OP *
1921Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
1922{
1923 dVAR;
1924 OP *kid;
1925 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1926 int localize = -1;
1927
1928 if (!o || (PL_parser && PL_parser->error_count))
1929 return o;
1930
1931 if ((o->op_private & OPpTARGET_MY)
1932 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1933 {
1934 return o;
1935 }
1936
1937 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
1938
1939 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
1940
1941 switch (o->op_type) {
1942 case OP_UNDEF:
1943 PL_modcount++;
1944 return o;
1945 case OP_STUB:
1946 if ((o->op_flags & OPf_PARENS) || PL_madskills)
1947 break;
1948 goto nomod;
1949 case OP_ENTERSUB:
1950 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
1951 !(o->op_flags & OPf_STACKED)) {
1952 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1953 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
1954 poses, so we need it clear. */
1955 o->op_private &= ~1;
1956 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1957 assert(cUNOPo->op_first->op_type == OP_NULL);
1958 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1959 break;
1960 }
1961 else { /* lvalue subroutine call */
1962 o->op_private |= OPpLVAL_INTRO
1963 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
1964 PL_modcount = RETURN_UNLIMITED_NUMBER;
1965 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1966 /* Potential lvalue context: */
1967 o->op_private |= OPpENTERSUB_INARGS;
1968 break;
1969 }
1970 else { /* Compile-time error message: */
1971 OP *kid = cUNOPo->op_first;
1972 CV *cv;
1973
1974 if (kid->op_type != OP_PUSHMARK) {
1975 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1976 Perl_croak(aTHX_
1977 "panic: unexpected lvalue entersub "
1978 "args: type/targ %ld:%"UVuf,
1979 (long)kid->op_type, (UV)kid->op_targ);
1980 kid = kLISTOP->op_first;
1981 }
1982 while (kid->op_sibling)
1983 kid = kid->op_sibling;
1984 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1985 break; /* Postpone until runtime */
1986 }
1987
1988 kid = kUNOP->op_first;
1989 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1990 kid = kUNOP->op_first;
1991 if (kid->op_type == OP_NULL)
1992 Perl_croak(aTHX_
1993 "Unexpected constant lvalue entersub "
1994 "entry via type/targ %ld:%"UVuf,
1995 (long)kid->op_type, (UV)kid->op_targ);
1996 if (kid->op_type != OP_GV) {
1997 break;
1998 }
1999
2000 cv = GvCV(kGVOP_gv);
2001 if (!cv)
2002 break;
2003 if (CvLVALUE(cv))
2004 break;
2005 }
2006 }
2007 /* FALL THROUGH */
2008 default:
2009 nomod:
2010 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2011 /* grep, foreach, subcalls, refgen */
2012 if (type == OP_GREPSTART || type == OP_ENTERSUB
2013 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2014 break;
2015 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2016 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2017 ? "do block"
2018 : (o->op_type == OP_ENTERSUB
2019 ? "non-lvalue subroutine call"
2020 : OP_DESC(o))),
2021 type ? PL_op_desc[type] : "local"));
2022 return o;
2023
2024 case OP_PREINC:
2025 case OP_PREDEC:
2026 case OP_POW:
2027 case OP_MULTIPLY:
2028 case OP_DIVIDE:
2029 case OP_MODULO:
2030 case OP_REPEAT:
2031 case OP_ADD:
2032 case OP_SUBTRACT:
2033 case OP_CONCAT:
2034 case OP_LEFT_SHIFT:
2035 case OP_RIGHT_SHIFT:
2036 case OP_BIT_AND:
2037 case OP_BIT_XOR:
2038 case OP_BIT_OR:
2039 case OP_I_MULTIPLY:
2040 case OP_I_DIVIDE:
2041 case OP_I_MODULO:
2042 case OP_I_ADD:
2043 case OP_I_SUBTRACT:
2044 if (!(o->op_flags & OPf_STACKED))
2045 goto nomod;
2046 PL_modcount++;
2047 break;
2048
2049 case OP_COND_EXPR:
2050 localize = 1;
2051 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2052 op_lvalue(kid, type);
2053 break;
2054
2055 case OP_RV2AV:
2056 case OP_RV2HV:
2057 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2058 PL_modcount = RETURN_UNLIMITED_NUMBER;
2059 return o; /* Treat \(@foo) like ordinary list. */
2060 }
2061 /* FALL THROUGH */
2062 case OP_RV2GV:
2063 if (scalar_mod_type(o, type))
2064 goto nomod;
2065 ref(cUNOPo->op_first, o->op_type);
2066 /* FALL THROUGH */
2067 case OP_ASLICE:
2068 case OP_HSLICE:
2069 if (type == OP_LEAVESUBLV)
2070 o->op_private |= OPpMAYBE_LVSUB;
2071 localize = 1;
2072 /* FALL THROUGH */
2073 case OP_AASSIGN:
2074 case OP_NEXTSTATE:
2075 case OP_DBSTATE:
2076 PL_modcount = RETURN_UNLIMITED_NUMBER;
2077 break;
2078 case OP_AV2ARYLEN:
2079 PL_hints |= HINT_BLOCK_SCOPE;
2080 if (type == OP_LEAVESUBLV)
2081 o->op_private |= OPpMAYBE_LVSUB;
2082 PL_modcount++;
2083 break;
2084 case OP_RV2SV:
2085 ref(cUNOPo->op_first, o->op_type);
2086 localize = 1;
2087 /* FALL THROUGH */
2088 case OP_GV:
2089 PL_hints |= HINT_BLOCK_SCOPE;
2090 case OP_SASSIGN:
2091 case OP_ANDASSIGN:
2092 case OP_ORASSIGN:
2093 case OP_DORASSIGN:
2094 PL_modcount++;
2095 break;
2096
2097 case OP_AELEMFAST:
2098 case OP_AELEMFAST_LEX:
2099 localize = -1;
2100 PL_modcount++;
2101 break;
2102
2103 case OP_PADAV:
2104 case OP_PADHV:
2105 PL_modcount = RETURN_UNLIMITED_NUMBER;
2106 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2107 return o; /* Treat \(@foo) like ordinary list. */
2108 if (scalar_mod_type(o, type))
2109 goto nomod;
2110 if (type == OP_LEAVESUBLV)
2111 o->op_private |= OPpMAYBE_LVSUB;
2112 /* FALL THROUGH */
2113 case OP_PADSV:
2114 PL_modcount++;
2115 if (!type) /* local() */
2116 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2117 PAD_COMPNAME_SV(o->op_targ));
2118 break;
2119
2120 case OP_PUSHMARK:
2121 localize = 0;
2122 break;
2123
2124 case OP_KEYS:
2125 case OP_RKEYS:
2126 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2127 goto nomod;
2128 goto lvalue_func;
2129 case OP_SUBSTR:
2130 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2131 goto nomod;
2132 /* FALL THROUGH */
2133 case OP_POS:
2134 case OP_VEC:
2135 lvalue_func:
2136 if (type == OP_LEAVESUBLV)
2137 o->op_private |= OPpMAYBE_LVSUB;
2138 pad_free(o->op_targ);
2139 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
2140 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
2141 if (o->op_flags & OPf_KIDS)
2142 op_lvalue(cBINOPo->op_first->op_sibling, type);
2143 break;
2144
2145 case OP_AELEM:
2146 case OP_HELEM:
2147 ref(cBINOPo->op_first, o->op_type);
2148 if (type == OP_ENTERSUB &&
2149 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2150 o->op_private |= OPpLVAL_DEFER;
2151 if (type == OP_LEAVESUBLV)
2152 o->op_private |= OPpMAYBE_LVSUB;
2153 localize = 1;
2154 PL_modcount++;
2155 break;
2156
2157 case OP_SCOPE:
2158 case OP_LEAVE:
2159 case OP_ENTER:
2160 case OP_LINESEQ:
2161 localize = 0;
2162 if (o->op_flags & OPf_KIDS)
2163 op_lvalue(cLISTOPo->op_last, type);
2164 break;
2165
2166 case OP_NULL:
2167 localize = 0;
2168 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2169 goto nomod;
2170 else if (!(o->op_flags & OPf_KIDS))
2171 break;
2172 if (o->op_targ != OP_LIST) {
2173 op_lvalue(cBINOPo->op_first, type);
2174 break;
2175 }
2176 /* FALL THROUGH */
2177 case OP_LIST:
2178 localize = 0;
2179 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2180 /* elements might be in void context because the list is
2181 in scalar context or because they are attribute sub calls */
2182 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2183 op_lvalue(kid, type);
2184 break;
2185
2186 case OP_RETURN:
2187 if (type != OP_LEAVESUBLV)
2188 goto nomod;
2189 break; /* op_lvalue()ing was handled by ck_return() */
2190
2191 case OP_COREARGS:
2192 return o;
2193 }
2194
2195 /* [20011101.069] File test operators interpret OPf_REF to mean that
2196 their argument is a filehandle; thus \stat(".") should not set
2197 it. AMS 20011102 */
2198 if (type == OP_REFGEN &&
2199 PL_check[o->op_type] == Perl_ck_ftst)
2200 return o;
2201
2202 if (type != OP_LEAVESUBLV)
2203 o->op_flags |= OPf_MOD;
2204
2205 if (type == OP_AASSIGN || type == OP_SASSIGN)
2206 o->op_flags |= OPf_SPECIAL|OPf_REF;
2207 else if (!type) { /* local() */
2208 switch (localize) {
2209 case 1:
2210 o->op_private |= OPpLVAL_INTRO;
2211 o->op_flags &= ~OPf_SPECIAL;
2212 PL_hints |= HINT_BLOCK_SCOPE;
2213 break;
2214 case 0:
2215 break;
2216 case -1:
2217 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2218 "Useless localization of %s", OP_DESC(o));
2219 }
2220 }
2221 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2222 && type != OP_LEAVESUBLV)
2223 o->op_flags |= OPf_REF;
2224 return o;
2225}
2226
2227STATIC bool
2228S_scalar_mod_type(const OP *o, I32 type)
2229{
2230 switch (type) {
2231 case OP_POS:
2232 case OP_SASSIGN:
2233 if (o && o->op_type == OP_RV2GV)
2234 return FALSE;
2235 /* FALL THROUGH */
2236 case OP_PREINC:
2237 case OP_PREDEC:
2238 case OP_POSTINC:
2239 case OP_POSTDEC:
2240 case OP_I_PREINC:
2241 case OP_I_PREDEC:
2242 case OP_I_POSTINC:
2243 case OP_I_POSTDEC:
2244 case OP_POW:
2245 case OP_MULTIPLY:
2246 case OP_DIVIDE:
2247 case OP_MODULO:
2248 case OP_REPEAT:
2249 case OP_ADD:
2250 case OP_SUBTRACT:
2251 case OP_I_MULTIPLY:
2252 case OP_I_DIVIDE:
2253 case OP_I_MODULO:
2254 case OP_I_ADD:
2255 case OP_I_SUBTRACT:
2256 case OP_LEFT_SHIFT:
2257 case OP_RIGHT_SHIFT:
2258 case OP_BIT_AND:
2259 case OP_BIT_XOR:
2260 case OP_BIT_OR:
2261 case OP_CONCAT:
2262 case OP_SUBST:
2263 case OP_TRANS:
2264 case OP_TRANSR:
2265 case OP_READ:
2266 case OP_SYSREAD:
2267 case OP_RECV:
2268 case OP_ANDASSIGN:
2269 case OP_ORASSIGN:
2270 case OP_DORASSIGN:
2271 return TRUE;
2272 default:
2273 return FALSE;
2274 }
2275}
2276
2277STATIC bool
2278S_is_handle_constructor(const OP *o, I32 numargs)
2279{
2280 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2281
2282 switch (o->op_type) {
2283 case OP_PIPE_OP:
2284 case OP_SOCKPAIR:
2285 if (numargs == 2)
2286 return TRUE;
2287 /* FALL THROUGH */
2288 case OP_SYSOPEN:
2289 case OP_OPEN:
2290 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2291 case OP_SOCKET:
2292 case OP_OPEN_DIR:
2293 case OP_ACCEPT:
2294 if (numargs == 1)
2295 return TRUE;
2296 /* FALLTHROUGH */
2297 default:
2298 return FALSE;
2299 }
2300}
2301
2302static OP *
2303S_refkids(pTHX_ OP *o, I32 type)
2304{
2305 if (o && o->op_flags & OPf_KIDS) {
2306 OP *kid;
2307 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2308 ref(kid, type);
2309 }
2310 return o;
2311}
2312
2313OP *
2314Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2315{
2316 dVAR;
2317 OP *kid;
2318
2319 PERL_ARGS_ASSERT_DOREF;
2320
2321 if (!o || (PL_parser && PL_parser->error_count))
2322 return o;
2323
2324 switch (o->op_type) {
2325 case OP_ENTERSUB:
2326 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2327 !(o->op_flags & OPf_STACKED)) {
2328 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2329 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2330 assert(cUNOPo->op_first->op_type == OP_NULL);
2331 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2332 o->op_flags |= OPf_SPECIAL;
2333 o->op_private &= ~1;
2334 }
2335 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2336 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2337 : type == OP_RV2HV ? OPpDEREF_HV
2338 : OPpDEREF_SV);
2339 o->op_flags |= OPf_MOD;
2340 }
2341
2342 break;
2343
2344 case OP_COND_EXPR:
2345 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2346 doref(kid, type, set_op_ref);
2347 break;
2348 case OP_RV2SV:
2349 if (type == OP_DEFINED)
2350 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2351 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2352 /* FALL THROUGH */
2353 case OP_PADSV:
2354 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2355 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2356 : type == OP_RV2HV ? OPpDEREF_HV
2357 : OPpDEREF_SV);
2358 o->op_flags |= OPf_MOD;
2359 }
2360 break;
2361
2362 case OP_RV2AV:
2363 case OP_RV2HV:
2364 if (set_op_ref)
2365 o->op_flags |= OPf_REF;
2366 /* FALL THROUGH */
2367 case OP_RV2GV:
2368 if (type == OP_DEFINED)
2369 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2370 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2371 break;
2372
2373 case OP_PADAV:
2374 case OP_PADHV:
2375 if (set_op_ref)
2376 o->op_flags |= OPf_REF;
2377 break;
2378
2379 case OP_SCALAR:
2380 case OP_NULL:
2381 if (!(o->op_flags & OPf_KIDS))
2382 break;
2383 doref(cBINOPo->op_first, type, set_op_ref);
2384 break;
2385 case OP_AELEM:
2386 case OP_HELEM:
2387 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2388 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2389 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2390 : type == OP_RV2HV ? OPpDEREF_HV
2391 : OPpDEREF_SV);
2392 o->op_flags |= OPf_MOD;
2393 }
2394 break;
2395
2396 case OP_SCOPE:
2397 case OP_LEAVE:
2398 set_op_ref = FALSE;
2399 /* FALL THROUGH */
2400 case OP_ENTER:
2401 case OP_LIST:
2402 if (!(o->op_flags & OPf_KIDS))
2403 break;
2404 doref(cLISTOPo->op_last, type, set_op_ref);
2405 break;
2406 default:
2407 break;
2408 }
2409 return scalar(o);
2410
2411}
2412
2413STATIC OP *
2414S_dup_attrlist(pTHX_ OP *o)
2415{
2416 dVAR;
2417 OP *rop;
2418
2419 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2420
2421 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2422 * where the first kid is OP_PUSHMARK and the remaining ones
2423 * are OP_CONST. We need to push the OP_CONST values.
2424 */
2425 if (o->op_type == OP_CONST)
2426 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2427#ifdef PERL_MAD
2428 else if (o->op_type == OP_NULL)
2429 rop = NULL;
2430#endif
2431 else {
2432 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2433 rop = NULL;
2434 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2435 if (o->op_type == OP_CONST)
2436 rop = op_append_elem(OP_LIST, rop,
2437 newSVOP(OP_CONST, o->op_flags,
2438 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2439 }
2440 }
2441 return rop;
2442}
2443
2444STATIC void
2445S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
2446{
2447 dVAR;
2448 SV *stashsv;
2449
2450 PERL_ARGS_ASSERT_APPLY_ATTRS;
2451
2452 /* fake up C<use attributes $pkg,$rv,@attrs> */
2453 ENTER; /* need to protect against side-effects of 'use' */
2454 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2455
2456#define ATTRSMODULE "attributes"
2457#define ATTRSMODULE_PM "attributes.pm"
2458
2459 if (for_my) {
2460 /* Don't force the C<use> if we don't need it. */
2461 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2462 if (svp && *svp != &PL_sv_undef)
2463 NOOP; /* already in %INC */
2464 else
2465 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2466 newSVpvs(ATTRSMODULE), NULL);
2467 }
2468 else {
2469 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2470 newSVpvs(ATTRSMODULE),
2471 NULL,
2472 op_prepend_elem(OP_LIST,
2473 newSVOP(OP_CONST, 0, stashsv),
2474 op_prepend_elem(OP_LIST,
2475 newSVOP(OP_CONST, 0,
2476 newRV(target)),
2477 dup_attrlist(attrs))));
2478 }
2479 LEAVE;
2480}
2481
2482STATIC void
2483S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2484{
2485 dVAR;
2486 OP *pack, *imop, *arg;
2487 SV *meth, *stashsv;
2488
2489 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2490
2491 if (!attrs)
2492 return;
2493
2494 assert(target->op_type == OP_PADSV ||
2495 target->op_type == OP_PADHV ||
2496 target->op_type == OP_PADAV);
2497
2498 /* Ensure that attributes.pm is loaded. */
2499 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
2500
2501 /* Need package name for method call. */
2502 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2503
2504 /* Build up the real arg-list. */
2505 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2506
2507 arg = newOP(OP_PADSV, 0);
2508 arg->op_targ = target->op_targ;
2509 arg = op_prepend_elem(OP_LIST,
2510 newSVOP(OP_CONST, 0, stashsv),
2511 op_prepend_elem(OP_LIST,
2512 newUNOP(OP_REFGEN, 0,
2513 op_lvalue(arg, OP_REFGEN)),
2514 dup_attrlist(attrs)));
2515
2516 /* Fake up a method call to import */
2517 meth = newSVpvs_share("import");
2518 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2519 op_append_elem(OP_LIST,
2520 op_prepend_elem(OP_LIST, pack, list(arg)),
2521 newSVOP(OP_METHOD_NAMED, 0, meth)));
2522
2523 /* Combine the ops. */
2524 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2525}
2526
2527/*
2528=notfor apidoc apply_attrs_string
2529
2530Attempts to apply a list of attributes specified by the C<attrstr> and
2531C<len> arguments to the subroutine identified by the C<cv> argument which
2532is expected to be associated with the package identified by the C<stashpv>
2533argument (see L<attributes>). It gets this wrong, though, in that it
2534does not correctly identify the boundaries of the individual attribute
2535specifications within C<attrstr>. This is not really intended for the
2536public API, but has to be listed here for systems such as AIX which
2537need an explicit export list for symbols. (It's called from XS code
2538in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2539to respect attribute syntax properly would be welcome.
2540
2541=cut
2542*/
2543
2544void
2545Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2546 const char *attrstr, STRLEN len)
2547{
2548 OP *attrs = NULL;
2549
2550 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2551
2552 if (!len) {
2553 len = strlen(attrstr);
2554 }
2555
2556 while (len) {
2557 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2558 if (len) {
2559 const char * const sstr = attrstr;
2560 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2561 attrs = op_append_elem(OP_LIST, attrs,
2562 newSVOP(OP_CONST, 0,
2563 newSVpvn(sstr, attrstr-sstr)));
2564 }
2565 }
2566
2567 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2568 newSVpvs(ATTRSMODULE),
2569 NULL, op_prepend_elem(OP_LIST,
2570 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2571 op_prepend_elem(OP_LIST,
2572 newSVOP(OP_CONST, 0,
2573 newRV(MUTABLE_SV(cv))),
2574 attrs)));
2575}
2576
2577STATIC OP *
2578S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2579{
2580 dVAR;
2581 I32 type;
2582 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2583
2584 PERL_ARGS_ASSERT_MY_KID;
2585
2586 if (!o || (PL_parser && PL_parser->error_count))
2587 return o;
2588
2589 type = o->op_type;
2590 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2591 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2592 return o;
2593 }
2594
2595 if (type == OP_LIST) {
2596 OP *kid;
2597 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2598 my_kid(kid, attrs, imopsp);
2599 return o;
2600 } else if (type == OP_UNDEF || type == OP_STUB) {
2601 return o;
2602 } else if (type == OP_RV2SV || /* "our" declaration */
2603 type == OP_RV2AV ||
2604 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2605 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2606 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2607 OP_DESC(o),
2608 PL_parser->in_my == KEY_our
2609 ? "our"
2610 : PL_parser->in_my == KEY_state ? "state" : "my"));
2611 } else if (attrs) {
2612 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2613 PL_parser->in_my = FALSE;
2614 PL_parser->in_my_stash = NULL;
2615 apply_attrs(GvSTASH(gv),
2616 (type == OP_RV2SV ? GvSV(gv) :
2617 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2618 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2619 attrs, FALSE);
2620 }
2621 o->op_private |= OPpOUR_INTRO;
2622 return o;
2623 }
2624 else if (type != OP_PADSV &&
2625 type != OP_PADAV &&
2626 type != OP_PADHV &&
2627 type != OP_PUSHMARK)
2628 {
2629 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2630 OP_DESC(o),
2631 PL_parser->in_my == KEY_our
2632 ? "our"
2633 : PL_parser->in_my == KEY_state ? "state" : "my"));
2634 return o;
2635 }
2636 else if (attrs && type != OP_PUSHMARK) {
2637 HV *stash;
2638
2639 PL_parser->in_my = FALSE;
2640 PL_parser->in_my_stash = NULL;
2641
2642 /* check for C<my Dog $spot> when deciding package */
2643 stash = PAD_COMPNAME_TYPE(o->op_targ);
2644 if (!stash)
2645 stash = PL_curstash;
2646 apply_attrs_my(stash, o, attrs, imopsp);
2647 }
2648 o->op_flags |= OPf_MOD;
2649 o->op_private |= OPpLVAL_INTRO;
2650 if (stately)
2651 o->op_private |= OPpPAD_STATE;
2652 return o;
2653}
2654
2655OP *
2656Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2657{
2658 dVAR;
2659 OP *rops;
2660 int maybe_scalar = 0;
2661
2662 PERL_ARGS_ASSERT_MY_ATTRS;
2663
2664/* [perl #17376]: this appears to be premature, and results in code such as
2665 C< our(%x); > executing in list mode rather than void mode */
2666#if 0
2667 if (o->op_flags & OPf_PARENS)
2668 list(o);
2669 else
2670 maybe_scalar = 1;
2671#else
2672 maybe_scalar = 1;
2673#endif
2674 if (attrs)
2675 SAVEFREEOP(attrs);
2676 rops = NULL;
2677 o = my_kid(o, attrs, &rops);
2678 if (rops) {
2679 if (maybe_scalar && o->op_type == OP_PADSV) {
2680 o = scalar(op_append_list(OP_LIST, rops, o));
2681 o->op_private |= OPpLVAL_INTRO;
2682 }
2683 else {
2684 /* The listop in rops might have a pushmark at the beginning,
2685 which will mess up list assignment. */
2686 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2687 if (rops->op_type == OP_LIST &&
2688 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2689 {
2690 OP * const pushmark = lrops->op_first;
2691 lrops->op_first = pushmark->op_sibling;
2692 op_free(pushmark);
2693 }
2694 o = op_append_list(OP_LIST, o, rops);
2695 }
2696 }
2697 PL_parser->in_my = FALSE;
2698 PL_parser->in_my_stash = NULL;
2699 return o;
2700}
2701
2702OP *
2703Perl_sawparens(pTHX_ OP *o)
2704{
2705 PERL_UNUSED_CONTEXT;
2706 if (o)
2707 o->op_flags |= OPf_PARENS;
2708 return o;
2709}
2710
2711OP *
2712Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2713{
2714 OP *o;
2715 bool ismatchop = 0;
2716 const OPCODE ltype = left->op_type;
2717 const OPCODE rtype = right->op_type;
2718
2719 PERL_ARGS_ASSERT_BIND_MATCH;
2720
2721 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2722 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2723 {
2724 const char * const desc
2725 = PL_op_desc[(
2726 rtype == OP_SUBST || rtype == OP_TRANS
2727 || rtype == OP_TRANSR
2728 )
2729 ? (int)rtype : OP_MATCH];
2730 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
2731 GV *gv;
2732 SV * const name =
2733 (ltype == OP_RV2AV || ltype == OP_RV2HV)
2734 ? cUNOPx(left)->op_first->op_type == OP_GV
2735 && (gv = cGVOPx_gv(cUNOPx(left)->op_first))
2736 ? varname(gv, isary ? '@' : '%', 0, NULL, 0, 1)
2737 : NULL
2738 : varname(
2739 (GV *)PL_compcv, isary ? '@' : '%', left->op_targ, NULL, 0, 1
2740 );
2741 if (name)
2742 Perl_warner(aTHX_ packWARN(WARN_MISC),
2743 "Applying %s to %"SVf" will act on scalar(%"SVf")",
2744 desc, name, name);
2745 else {
2746 const char * const sample = (isary
2747 ? "@array" : "%hash");
2748 Perl_warner(aTHX_ packWARN(WARN_MISC),
2749 "Applying %s to %s will act on scalar(%s)",
2750 desc, sample, sample);
2751 }
2752 }
2753
2754 if (rtype == OP_CONST &&
2755 cSVOPx(right)->op_private & OPpCONST_BARE &&
2756 cSVOPx(right)->op_private & OPpCONST_STRICT)
2757 {
2758 no_bareword_allowed(right);
2759 }
2760
2761 /* !~ doesn't make sense with /r, so error on it for now */
2762 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
2763 type == OP_NOT)
2764 yyerror("Using !~ with s///r doesn't make sense");
2765 if (rtype == OP_TRANSR && type == OP_NOT)
2766 yyerror("Using !~ with tr///r doesn't make sense");
2767
2768 ismatchop = (rtype == OP_MATCH ||
2769 rtype == OP_SUBST ||
2770 rtype == OP_TRANS || rtype == OP_TRANSR)
2771 && !(right->op_flags & OPf_SPECIAL);
2772 if (ismatchop && right->op_private & OPpTARGET_MY) {
2773 right->op_targ = 0;
2774 right->op_private &= ~OPpTARGET_MY;
2775 }
2776 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2777 OP *newleft;
2778
2779 right->op_flags |= OPf_STACKED;
2780 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
2781 ! (rtype == OP_TRANS &&
2782 right->op_private & OPpTRANS_IDENTICAL) &&
2783 ! (rtype == OP_SUBST &&
2784 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
2785 newleft = op_lvalue(left, rtype);
2786 else
2787 newleft = left;
2788 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
2789 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2790 else
2791 o = op_prepend_elem(rtype, scalar(newleft), right);
2792 if (type == OP_NOT)
2793 return newUNOP(OP_NOT, 0, scalar(o));
2794 return o;
2795 }
2796 else
2797 return bind_match(type, left,
2798 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
2799}
2800
2801OP *
2802Perl_invert(pTHX_ OP *o)
2803{
2804 if (!o)
2805 return NULL;
2806 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2807}
2808
2809/*
2810=for apidoc Amx|OP *|op_scope|OP *o
2811
2812Wraps up an op tree with some additional ops so that at runtime a dynamic
2813scope will be created. The original ops run in the new dynamic scope,
2814and then, provided that they exit normally, the scope will be unwound.
2815The additional ops used to create and unwind the dynamic scope will
2816normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
2817instead if the ops are simple enough to not need the full dynamic scope
2818structure.
2819
2820=cut
2821*/
2822
2823OP *
2824Perl_op_scope(pTHX_ OP *o)
2825{
2826 dVAR;
2827 if (o) {
2828 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2829 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2830 o->op_type = OP_LEAVE;
2831 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2832 }
2833 else if (o->op_type == OP_LINESEQ) {
2834 OP *kid;
2835 o->op_type = OP_SCOPE;
2836 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2837 kid = ((LISTOP*)o)->op_first;
2838 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2839 op_null(kid);
2840
2841 /* The following deals with things like 'do {1 for 1}' */
2842 kid = kid->op_sibling;
2843 if (kid &&
2844 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2845 op_null(kid);
2846 }
2847 }
2848 else
2849 o = newLISTOP(OP_SCOPE, 0, o, NULL);
2850 }
2851 return o;
2852}
2853
2854int
2855Perl_block_start(pTHX_ int full)
2856{
2857 dVAR;
2858 const int retval = PL_savestack_ix;
2859
2860 pad_block_start(full);
2861 SAVEHINTS();
2862 PL_hints &= ~HINT_BLOCK_SCOPE;
2863 SAVECOMPILEWARNINGS();
2864 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2865
2866 CALL_BLOCK_HOOKS(bhk_start, full);
2867
2868 return retval;
2869}
2870
2871OP*
2872Perl_block_end(pTHX_ I32 floor, OP *seq)
2873{
2874 dVAR;
2875 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2876 OP* retval = scalarseq(seq);
2877
2878 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
2879
2880 LEAVE_SCOPE(floor);
2881 CopHINTS_set(&PL_compiling, PL_hints);
2882 if (needblockscope)
2883 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2884 pad_leavemy();
2885
2886 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
2887
2888 return retval;
2889}
2890
2891/*
2892=head1 Compile-time scope hooks
2893
2894=for apidoc Aox||blockhook_register
2895
2896Register a set of hooks to be called when the Perl lexical scope changes
2897at compile time. See L<perlguts/"Compile-time scope hooks">.
2898
2899=cut
2900*/
2901
2902void
2903Perl_blockhook_register(pTHX_ BHK *hk)
2904{
2905 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
2906
2907 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
2908}
2909
2910STATIC OP *
2911S_newDEFSVOP(pTHX)
2912{
2913 dVAR;
2914 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
2915 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2916 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2917 }
2918 else {
2919 OP * const o = newOP(OP_PADSV, 0);
2920 o->op_targ = offset;
2921 return o;
2922 }
2923}
2924
2925void
2926Perl_newPROG(pTHX_ OP *o)
2927{
2928 dVAR;
2929
2930 PERL_ARGS_ASSERT_NEWPROG;
2931
2932 if (PL_in_eval) {
2933 PERL_CONTEXT *cx;
2934 I32 i;
2935 if (PL_eval_root)
2936 return;
2937 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2938 ((PL_in_eval & EVAL_KEEPERR)
2939 ? OPf_SPECIAL : 0), o);
2940
2941 cx = &cxstack[cxstack_ix];
2942 assert(CxTYPE(cx) == CXt_EVAL);
2943
2944 if ((cx->blk_gimme & G_WANT) == G_VOID)
2945 scalarvoid(PL_eval_root);
2946 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
2947 list(PL_eval_root);
2948 else
2949 scalar(PL_eval_root);
2950
2951 PL_eval_start = op_linklist(PL_eval_root);
2952 PL_eval_root->op_private |= OPpREFCOUNTED;
2953 OpREFCNT_set(PL_eval_root, 1);
2954 PL_eval_root->op_next = 0;
2955 i = PL_savestack_ix;
2956 SAVEFREEOP(o);
2957 ENTER;
2958 CALL_PEEP(PL_eval_start);
2959 finalize_optree(PL_eval_root);
2960 LEAVE;
2961 PL_savestack_ix = i;
2962 }
2963 else {
2964 if (o->op_type == OP_STUB) {
2965 PL_comppad_name = 0;
2966 PL_compcv = 0;
2967 S_op_destroy(aTHX_ o);
2968 return;
2969 }
2970 PL_main_root = op_scope(sawparens(scalarvoid(o)));
2971 PL_curcop = &PL_compiling;
2972 PL_main_start = LINKLIST(PL_main_root);
2973 PL_main_root->op_private |= OPpREFCOUNTED;
2974 OpREFCNT_set(PL_main_root, 1);
2975 PL_main_root->op_next = 0;
2976 CALL_PEEP(PL_main_start);
2977 finalize_optree(PL_main_root);
2978 cv_forget_slab(PL_compcv);
2979 PL_compcv = 0;
2980
2981 /* Register with debugger */
2982 if (PERLDB_INTER) {
2983 CV * const cv = get_cvs("DB::postponed", 0);
2984 if (cv) {
2985 dSP;
2986 PUSHMARK(SP);
2987 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
2988 PUTBACK;
2989 call_sv(MUTABLE_SV(cv), G_DISCARD);
2990 }
2991 }
2992 }
2993}
2994
2995OP *
2996Perl_localize(pTHX_ OP *o, I32 lex)
2997{
2998 dVAR;
2999
3000 PERL_ARGS_ASSERT_LOCALIZE;
3001
3002 if (o->op_flags & OPf_PARENS)
3003/* [perl #17376]: this appears to be premature, and results in code such as
3004 C< our(%x); > executing in list mode rather than void mode */
3005#if 0
3006 list(o);
3007#else
3008 NOOP;
3009#endif
3010 else {
3011 if ( PL_parser->bufptr > PL_parser->oldbufptr
3012 && PL_parser->bufptr[-1] == ','
3013 && ckWARN(WARN_PARENTHESIS))
3014 {
3015 char *s = PL_parser->bufptr;
3016 bool sigil = FALSE;
3017
3018 /* some heuristics to detect a potential error */
3019 while (*s && (strchr(", \t\n", *s)))
3020 s++;
3021
3022 while (1) {
3023 if (*s && strchr("@$%*", *s) && *++s
3024 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
3025 s++;
3026 sigil = TRUE;
3027 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
3028 s++;
3029 while (*s && (strchr(", \t\n", *s)))
3030 s++;
3031 }
3032 else
3033 break;
3034 }
3035 if (sigil && (*s == ';' || *s == '=')) {
3036 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3037 "Parentheses missing around \"%s\" list",
3038 lex
3039 ? (PL_parser->in_my == KEY_our
3040 ? "our"
3041 : PL_parser->in_my == KEY_state
3042 ? "state"
3043 : "my")
3044 : "local");
3045 }
3046 }
3047 }
3048 if (lex)
3049 o = my(o);
3050 else
3051 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3052 PL_parser->in_my = FALSE;
3053 PL_parser->in_my_stash = NULL;
3054 return o;
3055}
3056
3057OP *
3058Perl_jmaybe(pTHX_ OP *o)
3059{
3060 PERL_ARGS_ASSERT_JMAYBE;
3061
3062 if (o->op_type == OP_LIST) {
3063 OP * const o2
3064 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3065 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3066 }
3067 return o;
3068}
3069
3070PERL_STATIC_INLINE OP *
3071S_op_std_init(pTHX_ OP *o)
3072{
3073 I32 type = o->op_type;
3074
3075 PERL_ARGS_ASSERT_OP_STD_INIT;
3076
3077 if (PL_opargs[type] & OA_RETSCALAR)
3078 scalar(o);
3079 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3080 o->op_targ = pad_alloc(type, SVs_PADTMP);
3081
3082 return o;
3083}
3084
3085PERL_STATIC_INLINE OP *
3086S_op_integerize(pTHX_ OP *o)
3087{
3088 I32 type = o->op_type;
3089
3090 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3091
3092 /* integerize op. */
3093 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3094 {
3095 dVAR;
3096 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
3097 }
3098
3099 if (type == OP_NEGATE)
3100 /* XXX might want a ck_negate() for this */
3101 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3102
3103 return o;
3104}
3105
3106static OP *
3107S_fold_constants(pTHX_ register OP *o)
3108{
3109 dVAR;
3110 register OP * VOL curop;
3111 OP *newop;
3112 VOL I32 type = o->op_type;
3113 SV * VOL sv = NULL;
3114 int ret = 0;
3115 I32 oldscope;
3116 OP *old_next;
3117 SV * const oldwarnhook = PL_warnhook;
3118 SV * const olddiehook = PL_diehook;
3119 COP not_compiling;
3120 dJMPENV;
3121
3122 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3123
3124 if (!(PL_opargs[type] & OA_FOLDCONST))
3125 goto nope;
3126
3127 switch (type) {
3128 case OP_UCFIRST:
3129 case OP_LCFIRST:
3130 case OP_UC:
3131 case OP_LC:
3132 case OP_SLT:
3133 case OP_SGT:
3134 case OP_SLE:
3135 case OP_SGE:
3136 case OP_SCMP:
3137 case OP_SPRINTF:
3138 /* XXX what about the numeric ops? */
3139 if (IN_LOCALE_COMPILETIME)
3140 goto nope;
3141 break;
3142 case OP_PACK:
3143 if (!cLISTOPo->op_first->op_sibling
3144 || cLISTOPo->op_first->op_sibling->op_type != OP_CONST)
3145 goto nope;
3146 {
3147 SV * const sv = cSVOPx_sv(cLISTOPo->op_first->op_sibling);
3148 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3149 {
3150 const char *s = SvPVX_const(sv);
3151 while (s < SvEND(sv)) {
3152 if (*s == 'p' || *s == 'P') goto nope;
3153 s++;
3154 }
3155 }
3156 }
3157 break;
3158 case OP_REPEAT:
3159 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3160 }
3161
3162 if (PL_parser && PL_parser->error_count)
3163 goto nope; /* Don't try to run w/ errors */
3164
3165 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3166 const OPCODE type = curop->op_type;
3167 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3168 type != OP_LIST &&
3169 type != OP_SCALAR &&
3170 type != OP_NULL &&
3171 type != OP_PUSHMARK)
3172 {
3173 goto nope;
3174 }
3175 }
3176
3177 curop = LINKLIST(o);
3178 old_next = o->op_next;
3179 o->op_next = 0;
3180 PL_op = curop;
3181
3182 oldscope = PL_scopestack_ix;
3183 create_eval_scope(G_FAKINGEVAL);
3184
3185 /* Verify that we don't need to save it: */
3186 assert(PL_curcop == &PL_compiling);
3187 StructCopy(&PL_compiling, &not_compiling, COP);
3188 PL_curcop = &not_compiling;
3189 /* The above ensures that we run with all the correct hints of the
3190 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3191 assert(IN_PERL_RUNTIME);
3192 PL_warnhook = PERL_WARNHOOK_FATAL;
3193 PL_diehook = NULL;
3194 JMPENV_PUSH(ret);
3195
3196 switch (ret) {
3197 case 0:
3198 CALLRUNOPS(aTHX);
3199 sv = *(PL_stack_sp--);
3200 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3201#ifdef PERL_MAD
3202 /* Can't simply swipe the SV from the pad, because that relies on
3203 the op being freed "real soon now". Under MAD, this doesn't
3204 happen (see the #ifdef below). */
3205 sv = newSVsv(sv);
3206#else
3207 pad_swipe(o->op_targ, FALSE);
3208#endif
3209 }
3210 else if (SvTEMP(sv)) { /* grab mortal temp? */
3211 SvREFCNT_inc_simple_void(sv);
3212 SvTEMP_off(sv);
3213 }
3214 break;
3215 case 3:
3216 /* Something tried to die. Abandon constant folding. */
3217 /* Pretend the error never happened. */
3218 CLEAR_ERRSV();
3219 o->op_next = old_next;
3220 break;
3221 default:
3222 JMPENV_POP;
3223 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3224 PL_warnhook = oldwarnhook;
3225 PL_diehook = olddiehook;
3226 /* XXX note that this croak may fail as we've already blown away
3227 * the stack - eg any nested evals */
3228 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3229 }
3230 JMPENV_POP;
3231 PL_warnhook = oldwarnhook;
3232 PL_diehook = olddiehook;
3233 PL_curcop = &PL_compiling;
3234
3235 if (PL_scopestack_ix > oldscope)
3236 delete_eval_scope();
3237
3238 if (ret)
3239 goto nope;
3240
3241#ifndef PERL_MAD
3242 op_free(o);
3243#endif
3244 assert(sv);
3245 if (type == OP_RV2GV)
3246 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3247 else
3248 newop = newSVOP(OP_CONST, OPpCONST_FOLDED<<8, MUTABLE_SV(sv));
3249 op_getmad(o,newop,'f');
3250 return newop;
3251
3252 nope:
3253 return o;
3254}
3255
3256static OP *
3257S_gen_constant_list(pTHX_ register OP *o)
3258{
3259 dVAR;
3260 register OP *curop;
3261 const I32 oldtmps_floor = PL_tmps_floor;
3262
3263 list(o);
3264 if (PL_parser && PL_parser->error_count)
3265 return o; /* Don't attempt to run with errors */
3266
3267 PL_op = curop = LINKLIST(o);
3268 o->op_next = 0;
3269 CALL_PEEP(curop);
3270 Perl_pp_pushmark(aTHX);
3271 CALLRUNOPS(aTHX);
3272 PL_op = curop;
3273 assert (!(curop->op_flags & OPf_SPECIAL));
3274 assert(curop->op_type == OP_RANGE);
3275 Perl_pp_anonlist(aTHX);
3276 PL_tmps_floor = oldtmps_floor;
3277
3278 o->op_type = OP_RV2AV;
3279 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3280 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3281 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3282 o->op_opt = 0; /* needs to be revisited in rpeep() */
3283 curop = ((UNOP*)o)->op_first;
3284 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
3285#ifdef PERL_MAD
3286 op_getmad(curop,o,'O');
3287#else
3288 op_free(curop);
3289#endif
3290 LINKLIST(o);
3291 return list(o);
3292}
3293
3294OP *
3295Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3296{
3297 dVAR;
3298 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3299 if (!o || o->op_type != OP_LIST)
3300 o = newLISTOP(OP_LIST, 0, o, NULL);
3301 else
3302 o->op_flags &= ~OPf_WANT;
3303
3304 if (!(PL_opargs[type] & OA_MARK))
3305 op_null(cLISTOPo->op_first);
3306 else {
3307 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3308 if (kid2 && kid2->op_type == OP_COREARGS) {
3309 op_null(cLISTOPo->op_first);
3310 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3311 }
3312 }
3313
3314 o->op_type = (OPCODE)type;
3315 o->op_ppaddr = PL_ppaddr[type];
3316 o->op_flags |= flags;
3317
3318 o = CHECKOP(type, o);
3319 if (o->op_type != (unsigned)type)
3320 return o;
3321
3322 return fold_constants(op_integerize(op_std_init(o)));
3323}
3324
3325/*
3326=head1 Optree Manipulation Functions
3327*/
3328
3329/* List constructors */
3330
3331/*
3332=for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3333
3334Append an item to the list of ops contained directly within a list-type
3335op, returning the lengthened list. I<first> is the list-type op,
3336and I<last> is the op to append to the list. I<optype> specifies the
3337intended opcode for the list. If I<first> is not already a list of the
3338right type, it will be upgraded into one. If either I<first> or I<last>
3339is null, the other is returned unchanged.
3340
3341=cut
3342*/
3343
3344OP *
3345Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3346{
3347 if (!first)
3348 return last;
3349
3350 if (!last)
3351 return first;
3352
3353 if (first->op_type != (unsigned)type
3354 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3355 {
3356 return newLISTOP(type, 0, first, last);
3357 }
3358
3359 if (first->op_flags & OPf_KIDS)
3360 ((LISTOP*)first)->op_last->op_sibling = last;
3361 else {
3362 first->op_flags |= OPf_KIDS;
3363 ((LISTOP*)first)->op_first = last;
3364 }
3365 ((LISTOP*)first)->op_last = last;
3366 return first;
3367}
3368
3369/*
3370=for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3371
3372Concatenate the lists of ops contained directly within two list-type ops,
3373returning the combined list. I<first> and I<last> are the list-type ops
3374to concatenate. I<optype> specifies the intended opcode for the list.
3375If either I<first> or I<last> is not already a list of the right type,
3376it will be upgraded into one. If either I<first> or I<last> is null,
3377the other is returned unchanged.
3378
3379=cut
3380*/
3381
3382OP *
3383Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3384{
3385 if (!first)
3386 return last;
3387
3388 if (!last)
3389 return first;
3390
3391 if (first->op_type != (unsigned)type)
3392 return op_prepend_elem(type, first, last);
3393
3394 if (last->op_type != (unsigned)type)
3395 return op_append_elem(type, first, last);
3396
3397 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3398 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3399 first->op_flags |= (last->op_flags & OPf_KIDS);
3400
3401#ifdef PERL_MAD
3402 if (((LISTOP*)last)->op_first && first->op_madprop) {
3403 MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3404 if (mp) {
3405 while (mp->mad_next)
3406 mp = mp->mad_next;
3407 mp->mad_next = first->op_madprop;
3408 }
3409 else {
3410 ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3411 }
3412 }
3413 first->op_madprop = last->op_madprop;
3414 last->op_madprop = 0;
3415#endif
3416
3417 S_op_destroy(aTHX_ last);
3418
3419 return first;
3420}
3421
3422/*
3423=for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3424
3425Prepend an item to the list of ops contained directly within a list-type
3426op, returning the lengthened list. I<first> is the op to prepend to the
3427list, and I<last> is the list-type op. I<optype> specifies the intended
3428opcode for the list. If I<last> is not already a list of the right type,
3429it will be upgraded into one. If either I<first> or I<last> is null,
3430the other is returned unchanged.
3431
3432=cut
3433*/
3434
3435OP *
3436Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3437{
3438 if (!first)
3439 return last;
3440
3441 if (!last)
3442 return first;
3443
3444 if (last->op_type == (unsigned)type) {
3445 if (type == OP_LIST) { /* already a PUSHMARK there */
3446 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3447 ((LISTOP*)last)->op_first->op_sibling = first;
3448 if (!(first->op_flags & OPf_PARENS))
3449 last->op_flags &= ~OPf_PARENS;
3450 }
3451 else {
3452 if (!(last->op_flags & OPf_KIDS)) {
3453 ((LISTOP*)last)->op_last = first;
3454 last->op_flags |= OPf_KIDS;
3455 }
3456 first->op_sibling = ((LISTOP*)last)->op_first;
3457 ((LISTOP*)last)->op_first = first;
3458 }
3459 last->op_flags |= OPf_KIDS;
3460 return last;
3461 }
3462
3463 return newLISTOP(type, 0, first, last);
3464}
3465
3466/* Constructors */
3467
3468#ifdef PERL_MAD
3469
3470TOKEN *
3471Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3472{
3473 TOKEN *tk;
3474 Newxz(tk, 1, TOKEN);
3475 tk->tk_type = (OPCODE)optype;
3476 tk->tk_type = 12345;
3477 tk->tk_lval = lval;
3478 tk->tk_mad = madprop;
3479 return tk;
3480}
3481
3482void
3483Perl_token_free(pTHX_ TOKEN* tk)
3484{
3485 PERL_ARGS_ASSERT_TOKEN_FREE;
3486
3487 if (tk->tk_type != 12345)
3488 return;
3489 mad_free(tk->tk_mad);
3490 Safefree(tk);
3491}
3492
3493void
3494Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3495{
3496 MADPROP* mp;
3497 MADPROP* tm;
3498
3499 PERL_ARGS_ASSERT_TOKEN_GETMAD;
3500
3501 if (tk->tk_type != 12345) {
3502 Perl_warner(aTHX_ packWARN(WARN_MISC),
3503 "Invalid TOKEN object ignored");
3504 return;
3505 }
3506 tm = tk->tk_mad;
3507 if (!tm)
3508 return;
3509
3510 /* faked up qw list? */
3511 if (slot == '(' &&
3512 tm->mad_type == MAD_SV &&
3513 SvPVX((SV *)tm->mad_val)[0] == 'q')
3514 slot = 'x';
3515
3516 if (o) {
3517 mp = o->op_madprop;
3518 if (mp) {
3519 for (;;) {
3520 /* pretend constant fold didn't happen? */
3521 if (mp->mad_key == 'f' &&
3522 (o->op_type == OP_CONST ||
3523 o->op_type == OP_GV) )
3524 {
3525 token_getmad(tk,(OP*)mp->mad_val,slot);
3526 return;
3527 }
3528 if (!mp->mad_next)
3529 break;
3530 mp = mp->mad_next;
3531 }
3532 mp->mad_next = tm;
3533 mp = mp->mad_next;
3534 }
3535 else {
3536 o->op_madprop = tm;
3537 mp = o->op_madprop;
3538 }
3539 if (mp->mad_key == 'X')
3540 mp->mad_key = slot; /* just change the first one */
3541
3542 tk->tk_mad = 0;
3543 }
3544 else
3545 mad_free(tm);
3546 Safefree(tk);
3547}
3548
3549void
3550Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3551{
3552 MADPROP* mp;
3553 if (!from)
3554 return;
3555 if (o) {
3556 mp = o->op_madprop;
3557 if (mp) {
3558 for (;;) {
3559 /* pretend constant fold didn't happen? */
3560 if (mp->mad_key == 'f' &&
3561 (o->op_type == OP_CONST ||
3562 o->op_type == OP_GV) )
3563 {
3564 op_getmad(from,(OP*)mp->mad_val,slot);
3565 return;
3566 }
3567 if (!mp->mad_next)
3568 break;
3569 mp = mp->mad_next;
3570 }
3571 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3572 }
3573 else {
3574 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3575 }
3576 }
3577}
3578
3579void
3580Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
3581{
3582 MADPROP* mp;
3583 if (!from)
3584 return;
3585 if (o) {
3586 mp = o->op_madprop;
3587 if (mp) {
3588 for (;;) {
3589 /* pretend constant fold didn't happen? */
3590 if (mp->mad_key == 'f' &&
3591 (o->op_type == OP_CONST ||
3592 o->op_type == OP_GV) )
3593 {
3594 op_getmad(from,(OP*)mp->mad_val,slot);
3595 return;
3596 }
3597 if (!mp->mad_next)
3598 break;
3599 mp = mp->mad_next;
3600 }
3601 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
3602 }
3603 else {
3604 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
3605 }
3606 }
3607 else {
3608 PerlIO_printf(PerlIO_stderr(),
3609 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
3610 op_free(from);
3611 }
3612}
3613
3614void
3615Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
3616{
3617 MADPROP* tm;
3618 if (!mp || !o)
3619 return;
3620 if (slot)
3621 mp->mad_key = slot;
3622 tm = o->op_madprop;
3623 o->op_madprop = mp;
3624 for (;;) {
3625 if (!mp->mad_next)
3626 break;
3627 mp = mp->mad_next;
3628 }
3629 mp->mad_next = tm;
3630}
3631
3632void
3633Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
3634{
3635 if (!o)
3636 return;
3637 addmad(tm, &(o->op_madprop), slot);
3638}
3639
3640void
3641Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
3642{
3643 MADPROP* mp;
3644 if (!tm || !root)
3645 return;
3646 if (slot)
3647 tm->mad_key = slot;
3648 mp = *root;
3649 if (!mp) {
3650 *root = tm;
3651 return;
3652 }
3653 for (;;) {
3654 if (!mp->mad_next)
3655 break;
3656 mp = mp->mad_next;
3657 }
3658 mp->mad_next = tm;
3659}
3660
3661MADPROP *
3662Perl_newMADsv(pTHX_ char key, SV* sv)
3663{
3664 PERL_ARGS_ASSERT_NEWMADSV;
3665
3666 return newMADPROP(key, MAD_SV, sv, 0);
3667}
3668
3669MADPROP *
3670Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
3671{
3672 MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
3673 mp->mad_next = 0;
3674 mp->mad_key = key;
3675 mp->mad_vlen = vlen;
3676 mp->mad_type = type;
3677 mp->mad_val = val;
3678/* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
3679 return mp;
3680}
3681
3682void
3683Perl_mad_free(pTHX_ MADPROP* mp)
3684{
3685/* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
3686 if (!mp)
3687 return;
3688 if (mp->mad_next)
3689 mad_free(mp->mad_next);
3690/* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
3691 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
3692 switch (mp->mad_type) {
3693 case MAD_NULL:
3694 break;
3695 case MAD_PV:
3696 Safefree((char*)mp->mad_val);
3697 break;
3698 case MAD_OP:
3699 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
3700 op_free((OP*)mp->mad_val);
3701 break;
3702 case MAD_SV:
3703 sv_free(MUTABLE_SV(mp->mad_val));
3704 break;
3705 default:
3706 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3707 break;
3708 }
3709 PerlMemShared_free(mp);
3710}
3711
3712#endif
3713
3714/*
3715=head1 Optree construction
3716
3717=for apidoc Am|OP *|newNULLLIST
3718
3719Constructs, checks, and returns a new C<stub> op, which represents an
3720empty list expression.
3721
3722=cut
3723*/
3724
3725OP *
3726Perl_newNULLLIST(pTHX)
3727{
3728 return newOP(OP_STUB, 0);
3729}
3730
3731static OP *
3732S_force_list(pTHX_ OP *o)
3733{
3734 if (!o || o->op_type != OP_LIST)
3735 o = newLISTOP(OP_LIST, 0, o, NULL);
3736 op_null(o);
3737 return o;
3738}
3739
3740/*
3741=for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
3742
3743Constructs, checks, and returns an op of any list type. I<type> is
3744the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3745C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
3746supply up to two ops to be direct children of the list op; they are
3747consumed by this function and become part of the constructed op tree.
3748
3749=cut
3750*/
3751
3752OP *
3753Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3754{
3755 dVAR;
3756 LISTOP *listop;
3757
3758 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3759
3760 NewOp(1101, listop, 1, LISTOP);
3761
3762 listop->op_type = (OPCODE)type;
3763 listop->op_ppaddr = PL_ppaddr[type];
3764 if (first || last)
3765 flags |= OPf_KIDS;
3766 listop->op_flags = (U8)flags;
3767
3768 if (!last && first)
3769 last = first;
3770 else if (!first && last)
3771 first = last;
3772 else if (first)
3773 first->op_sibling = last;
3774 listop->op_first = first;
3775 listop->op_last = last;
3776 if (type == OP_LIST) {
3777 OP* const pushop = newOP(OP_PUSHMARK, 0);
3778 pushop->op_sibling = first;
3779 listop->op_first = pushop;
3780 listop->op_flags |= OPf_KIDS;
3781 if (!last)
3782 listop->op_last = pushop;
3783 }
3784
3785 return CHECKOP(type, listop);
3786}
3787
3788/*
3789=for apidoc Am|OP *|newOP|I32 type|I32 flags
3790
3791Constructs, checks, and returns an op of any base type (any type that
3792has no extra fields). I<type> is the opcode. I<flags> gives the
3793eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
3794of C<op_private>.
3795
3796=cut
3797*/
3798
3799OP *
3800Perl_newOP(pTHX_ I32 type, I32 flags)
3801{
3802 dVAR;
3803 OP *o;
3804
3805 if (type == -OP_ENTEREVAL) {
3806 type = OP_ENTEREVAL;
3807 flags |= OPpEVAL_BYTES<<8;
3808 }
3809
3810 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3811 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3812 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3813 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3814
3815 NewOp(1101, o, 1, OP);
3816 o->op_type = (OPCODE)type;
3817 o->op_ppaddr = PL_ppaddr[type];
3818 o->op_flags = (U8)flags;
3819 o->op_latefree = 0;
3820 o->op_latefreed = 0;
3821 o->op_attached = 0;
3822
3823 o->op_next = o;
3824 o->op_private = (U8)(0 | (flags >> 8));
3825 if (PL_opargs[type] & OA_RETSCALAR)
3826 scalar(o);
3827 if (PL_opargs[type] & OA_TARGET)
3828 o->op_targ = pad_alloc(type, SVs_PADTMP);
3829 return CHECKOP(type, o);
3830}
3831
3832/*
3833=for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
3834
3835Constructs, checks, and returns an op of any unary type. I<type> is
3836the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3837C<OPf_KIDS> will be set automatically if required, and, shifted up eight
3838bits, the eight bits of C<op_private>, except that the bit with value 1
3839is automatically set. I<first> supplies an optional op to be the direct
3840child of the unary op; it is consumed by this function and become part
3841of the constructed op tree.
3842
3843=cut
3844*/
3845
3846OP *
3847Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3848{
3849 dVAR;
3850 UNOP *unop;
3851
3852 if (type == -OP_ENTEREVAL) {
3853 type = OP_ENTEREVAL;
3854 flags |= OPpEVAL_BYTES<<8;
3855 }
3856
3857 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3858 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3859 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3860 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3861 || type == OP_SASSIGN
3862 || type == OP_ENTERTRY
3863 || type == OP_NULL );
3864
3865 if (!first)
3866 first = newOP(OP_STUB, 0);
3867 if (PL_opargs[type] & OA_MARK)
3868 first = force_list(first);
3869
3870 NewOp(1101, unop, 1, UNOP);
3871 unop->op_type = (OPCODE)type;
3872 unop->op_ppaddr = PL_ppaddr[type];
3873 unop->op_first = first;
3874 unop->op_flags = (U8)(flags | OPf_KIDS);
3875 unop->op_private = (U8)(1 | (flags >> 8));
3876 unop = (UNOP*) CHECKOP(type, unop);
3877 if (unop->op_next)
3878 return (OP*)unop;
3879
3880 return fold_constants(op_integerize(op_std_init((OP *) unop)));
3881}
3882
3883/*
3884=for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
3885
3886Constructs, checks, and returns an op of any binary type. I<type>
3887is the opcode. I<flags> gives the eight bits of C<op_flags>, except
3888that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
3889the eight bits of C<op_private>, except that the bit with value 1 or
38902 is automatically set as required. I<first> and I<last> supply up to
3891two ops to be the direct children of the binary op; they are consumed
3892by this function and become part of the constructed op tree.
3893
3894=cut
3895*/
3896
3897OP *
3898Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3899{
3900 dVAR;
3901 BINOP *binop;
3902
3903 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3904 || type == OP_SASSIGN || type == OP_NULL );
3905
3906 NewOp(1101, binop, 1, BINOP);
3907
3908 if (!first)
3909 first = newOP(OP_NULL, 0);
3910
3911 binop->op_type = (OPCODE)type;
3912 binop->op_ppaddr = PL_ppaddr[type];
3913 binop->op_first = first;
3914 binop->op_flags = (U8)(flags | OPf_KIDS);
3915 if (!last) {
3916 last = first;
3917 binop->op_private = (U8)(1 | (flags >> 8));
3918 }
3919 else {
3920 binop->op_private = (U8)(2 | (flags >> 8));
3921 first->op_sibling = last;
3922 }
3923
3924 binop = (BINOP*)CHECKOP(type, binop);
3925 if (binop->op_next || binop->op_type != (OPCODE)type)
3926 return (OP*)binop;
3927
3928 binop->op_last = binop->op_first->op_sibling;
3929
3930 return fold_constants(op_integerize(op_std_init((OP *)binop)));
3931}
3932
3933static int uvcompare(const void *a, const void *b)
3934 __attribute__nonnull__(1)
3935 __attribute__nonnull__(2)
3936 __attribute__pure__;
3937static int uvcompare(const void *a, const void *b)
3938{
3939 if (*((const UV *)a) < (*(const UV *)b))
3940 return -1;
3941 if (*((const UV *)a) > (*(const UV *)b))
3942 return 1;
3943 if (*((const UV *)a+1) < (*(const UV *)b+1))
3944 return -1;
3945 if (*((const UV *)a+1) > (*(const UV *)b+1))
3946 return 1;
3947 return 0;
3948}
3949
3950static OP *
3951S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
3952{
3953 dVAR;
3954 SV * const tstr = ((SVOP*)expr)->op_sv;
3955 SV * const rstr =
3956#ifdef PERL_MAD
3957 (repl->op_type == OP_NULL)
3958 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
3959#endif
3960 ((SVOP*)repl)->op_sv;
3961 STRLEN tlen;
3962 STRLEN rlen;
3963 const U8 *t = (U8*)SvPV_const(tstr, tlen);
3964 const U8 *r = (U8*)SvPV_const(rstr, rlen);
3965 register I32 i;
3966 register I32 j;
3967 I32 grows = 0;
3968 register short *tbl;
3969
3970 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
3971 const I32 squash = o->op_private & OPpTRANS_SQUASH;
3972 I32 del = o->op_private & OPpTRANS_DELETE;
3973 SV* swash;
3974
3975 PERL_ARGS_ASSERT_PMTRANS;
3976
3977 PL_hints |= HINT_BLOCK_SCOPE;
3978
3979 if (SvUTF8(tstr))
3980 o->op_private |= OPpTRANS_FROM_UTF;
3981
3982 if (SvUTF8(rstr))
3983 o->op_private |= OPpTRANS_TO_UTF;
3984
3985 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
3986 SV* const listsv = newSVpvs("# comment\n");
3987 SV* transv = NULL;
3988 const U8* tend = t + tlen;
3989 const U8* rend = r + rlen;
3990 STRLEN ulen;
3991 UV tfirst = 1;
3992 UV tlast = 0;
3993 IV tdiff;
3994 UV rfirst = 1;
3995 UV rlast = 0;
3996 IV rdiff;
3997 IV diff;
3998 I32 none = 0;
3999 U32 max = 0;
4000 I32 bits;
4001 I32 havefinal = 0;
4002 U32 final = 0;
4003 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4004 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4005 U8* tsave = NULL;
4006 U8* rsave = NULL;
4007 const U32 flags = UTF8_ALLOW_DEFAULT;
4008
4009 if (!from_utf) {
4010 STRLEN len = tlen;
4011 t = tsave = bytes_to_utf8(t, &len);
4012 tend = t + len;
4013 }
4014 if (!to_utf && rlen) {
4015 STRLEN len = rlen;
4016 r = rsave = bytes_to_utf8(r, &len);
4017 rend = r + len;
4018 }
4019
4020/* There are several snags with this code on EBCDIC:
4021 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
4022 2. scan_const() in toke.c has encoded chars in native encoding which makes
4023 ranges at least in EBCDIC 0..255 range the bottom odd.
4024*/
4025
4026 if (complement) {
4027 U8 tmpbuf[UTF8_MAXBYTES+1];
4028 UV *cp;
4029 UV nextmin = 0;
4030 Newx(cp, 2*tlen, UV);
4031 i = 0;
4032 transv = newSVpvs("");
4033 while (t < tend) {
4034 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4035 t += ulen;
4036 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
4037 t++;
4038 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4039 t += ulen;
4040 }
4041 else {
4042 cp[2*i+1] = cp[2*i];
4043 }
4044 i++;
4045 }
4046 qsort(cp, i, 2*sizeof(UV), uvcompare);
4047 for (j = 0; j < i; j++) {
4048 UV val = cp[2*j];
4049 diff = val - nextmin;
4050 if (diff > 0) {
4051 t = uvuni_to_utf8(tmpbuf,nextmin);
4052 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4053 if (diff > 1) {
4054 U8 range_mark = UTF_TO_NATIVE(0xff);
4055 t = uvuni_to_utf8(tmpbuf, val - 1);
4056 sv_catpvn(transv, (char *)&range_mark, 1);
4057 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4058 }
4059 }
4060 val = cp[2*j+1];
4061 if (val >= nextmin)
4062 nextmin = val + 1;
4063 }
4064 t = uvuni_to_utf8(tmpbuf,nextmin);
4065 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4066 {
4067 U8 range_mark = UTF_TO_NATIVE(0xff);
4068 sv_catpvn(transv, (char *)&range_mark, 1);
4069 }
4070 t = uvuni_to_utf8(tmpbuf, 0x7fffffff);
4071 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4072 t = (const U8*)SvPVX_const(transv);
4073 tlen = SvCUR(transv);
4074 tend = t + tlen;
4075 Safefree(cp);
4076 }
4077 else if (!rlen && !del) {
4078 r = t; rlen = tlen; rend = tend;
4079 }
4080 if (!squash) {
4081 if ((!rlen && !del) || t == r ||
4082 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4083 {
4084 o->op_private |= OPpTRANS_IDENTICAL;
4085 }
4086 }
4087
4088 while (t < tend || tfirst <= tlast) {
4089 /* see if we need more "t" chars */
4090 if (tfirst > tlast) {
4091 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4092 t += ulen;
4093 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
4094 t++;
4095 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4096 t += ulen;
4097 }
4098 else
4099 tlast = tfirst;
4100 }
4101
4102 /* now see if we need more "r" chars */
4103 if (rfirst > rlast) {
4104 if (r < rend) {
4105 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4106 r += ulen;
4107 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
4108 r++;
4109 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4110 r += ulen;
4111 }
4112 else
4113 rlast = rfirst;
4114 }
4115 else {
4116 if (!havefinal++)
4117 final = rlast;
4118 rfirst = rlast = 0xffffffff;
4119 }
4120 }
4121
4122 /* now see which range will peter our first, if either. */
4123 tdiff = tlast - tfirst;
4124 rdiff = rlast - rfirst;
4125
4126 if (tdiff <= rdiff)
4127 diff = tdiff;
4128 else
4129 diff = rdiff;
4130
4131 if (rfirst == 0xffffffff) {
4132 diff = tdiff; /* oops, pretend rdiff is infinite */
4133 if (diff > 0)
4134 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4135 (long)tfirst, (long)tlast);
4136 else
4137 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4138 }
4139 else {
4140 if (diff > 0)
4141 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4142 (long)tfirst, (long)(tfirst + diff),
4143 (long)rfirst);
4144 else
4145 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4146 (long)tfirst, (long)rfirst);
4147
4148 if (rfirst + diff > max)
4149 max = rfirst + diff;
4150 if (!grows)
4151 grows = (tfirst < rfirst &&
4152 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4153 rfirst += diff + 1;
4154 }
4155 tfirst += diff + 1;
4156 }
4157
4158 none = ++max;
4159 if (del)
4160 del = ++max;
4161
4162 if (max > 0xffff)
4163 bits = 32;
4164 else if (max > 0xff)
4165 bits = 16;
4166 else
4167 bits = 8;
4168
4169 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4170#ifdef USE_ITHREADS
4171 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
4172 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4173 PAD_SETSV(cPADOPo->op_padix, swash);
4174 SvPADTMP_on(swash);
4175 SvREADONLY_on(swash);
4176#else
4177 cSVOPo->op_sv = swash;
4178#endif
4179 SvREFCNT_dec(listsv);
4180 SvREFCNT_dec(transv);
4181
4182 if (!del && havefinal && rlen)
4183 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4184 newSVuv((UV)final), 0);
4185
4186 if (grows)
4187 o->op_private |= OPpTRANS_GROWS;
4188
4189 Safefree(tsave);
4190 Safefree(rsave);
4191
4192#ifdef PERL_MAD
4193 op_getmad(expr,o,'e');
4194 op_getmad(repl,o,'r');
4195#else
4196 op_free(expr);
4197 op_free(repl);
4198#endif
4199 return o;
4200 }
4201
4202 tbl = (short*)PerlMemShared_calloc(
4203 (o->op_private & OPpTRANS_COMPLEMENT) &&
4204 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4205 sizeof(short));
4206 cPVOPo->op_pv = (char*)tbl;
4207 if (complement) {
4208 for (i = 0; i < (I32)tlen; i++)
4209 tbl[t[i]] = -1;
4210 for (i = 0, j = 0; i < 256; i++) {
4211 if (!tbl[i]) {
4212 if (j >= (I32)rlen) {
4213 if (del)
4214 tbl[i] = -2;
4215 else if (rlen)
4216 tbl[i] = r[j-1];
4217 else
4218 tbl[i] = (short)i;
4219 }
4220 else {
4221 if (i < 128 && r[j] >= 128)
4222 grows = 1;
4223 tbl[i] = r[j++];
4224 }
4225 }
4226 }
4227 if (!del) {
4228 if (!rlen) {
4229 j = rlen;
4230 if (!squash)
4231 o->op_private |= OPpTRANS_IDENTICAL;
4232 }
4233 else if (j >= (I32)rlen)
4234 j = rlen - 1;
4235 else {
4236 tbl =
4237 (short *)
4238 PerlMemShared_realloc(tbl,
4239 (0x101+rlen-j) * sizeof(short));
4240 cPVOPo->op_pv = (char*)tbl;
4241 }
4242 tbl[0x100] = (short)(rlen - j);
4243 for (i=0; i < (I32)rlen - j; i++)
4244 tbl[0x101+i] = r[j+i];
4245 }
4246 }
4247 else {
4248 if (!rlen && !del) {
4249 r = t; rlen = tlen;
4250 if (!squash)
4251 o->op_private |= OPpTRANS_IDENTICAL;
4252 }
4253 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4254 o->op_private |= OPpTRANS_IDENTICAL;
4255 }
4256 for (i = 0; i < 256; i++)
4257 tbl[i] = -1;
4258 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4259 if (j >= (I32)rlen) {
4260 if (del) {
4261 if (tbl[t[i]] == -1)
4262 tbl[t[i]] = -2;
4263 continue;
4264 }
4265 --j;
4266 }
4267 if (tbl[t[i]] == -1) {
4268 if (t[i] < 128 && r[j] >= 128)
4269 grows = 1;
4270 tbl[t[i]] = r[j];
4271 }
4272 }
4273 }
4274
4275 if(del && rlen == tlen) {
4276 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4277 } else if(rlen > tlen) {
4278 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4279 }
4280
4281 if (grows)
4282 o->op_private |= OPpTRANS_GROWS;
4283#ifdef PERL_MAD
4284 op_getmad(expr,o,'e');
4285 op_getmad(repl,o,'r');
4286#else
4287 op_free(expr);
4288 op_free(repl);
4289#endif
4290
4291 return o;
4292}
4293
4294/*
4295=for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4296
4297Constructs, checks, and returns an op of any pattern matching type.
4298I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4299and, shifted up eight bits, the eight bits of C<op_private>.
4300
4301=cut
4302*/
4303
4304OP *
4305Perl_newPMOP(pTHX_ I32 type, I32 flags)
4306{
4307 dVAR;
4308 PMOP *pmop;
4309
4310 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4311
4312 NewOp(1101, pmop, 1, PMOP);
4313 pmop->op_type = (OPCODE)type;
4314 pmop->op_ppaddr = PL_ppaddr[type];
4315 pmop->op_flags = (U8)flags;
4316 pmop->op_private = (U8)(0 | (flags >> 8));
4317
4318 if (PL_hints & HINT_RE_TAINT)
4319 pmop->op_pmflags |= PMf_RETAINT;
4320 if (IN_LOCALE_COMPILETIME) {
4321 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4322 }
4323 else if ((! (PL_hints & HINT_BYTES))
4324 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4325 && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4326 {
4327 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4328 }
4329 if (PL_hints & HINT_RE_FLAGS) {
4330 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4331 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4332 );
4333 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4334 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4335 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4336 );
4337 if (reflags && SvOK(reflags)) {
4338 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4339 }
4340 }
4341
4342
4343#ifdef USE_ITHREADS
4344 assert(SvPOK(PL_regex_pad[0]));
4345 if (SvCUR(PL_regex_pad[0])) {
4346 /* Pop off the "packed" IV from the end. */
4347 SV *const repointer_list = PL_regex_pad[0];
4348 const char *p = SvEND(repointer_list) - sizeof(IV);
4349 const IV offset = *((IV*)p);
4350
4351 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4352
4353 SvEND_set(repointer_list, p);
4354
4355 pmop->op_pmoffset = offset;
4356 /* This slot should be free, so assert this: */
4357 assert(PL_regex_pad[offset] == &PL_sv_undef);
4358 } else {
4359 SV * const repointer = &PL_sv_undef;
4360 av_push(PL_regex_padav, repointer);
4361 pmop->op_pmoffset = av_len(PL_regex_padav);
4362 PL_regex_pad = AvARRAY(PL_regex_padav);
4363 }
4364#endif
4365
4366 return CHECKOP(type, pmop);
4367}
4368
4369/* Given some sort of match op o, and an expression expr containing a
4370 * pattern, either compile expr into a regex and attach it to o (if it's
4371 * constant), or convert expr into a runtime regcomp op sequence (if it's
4372 * not)
4373 *
4374 * isreg indicates that the pattern is part of a regex construct, eg
4375 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4376 * split "pattern", which aren't. In the former case, expr will be a list
4377 * if the pattern contains more than one term (eg /a$b/) or if it contains
4378 * a replacement, ie s/// or tr///.
4379 *
4380 * When the pattern has been compiled within a new anon CV (for
4381 * qr/(?{...})/ ), then floor indicates the savestack level just before
4382 * the new sub was created
4383 */
4384
4385OP *
4386Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4387{
4388 dVAR;
4389 PMOP *pm;
4390 LOGOP *rcop;
4391 I32 repl_has_vars = 0;
4392 OP* repl = NULL;
4393 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4394 bool is_compiletime;
4395 bool has_code;
4396
4397 PERL_ARGS_ASSERT_PMRUNTIME;
4398
4399 /* for s/// and tr///, last element in list is the replacement; pop it */
4400
4401 if (is_trans || o->op_type == OP_SUBST) {
4402 OP* kid;
4403 repl = cLISTOPx(expr)->op_last;
4404 kid = cLISTOPx(expr)->op_first;
4405 while (kid->op_sibling != repl)
4406 kid = kid->op_sibling;
4407 kid->op_sibling = NULL;
4408 cLISTOPx(expr)->op_last = kid;
4409 }
4410
4411 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4412
4413 if (is_trans) {
4414 OP* const oe = expr;
4415 assert(expr->op_type == OP_LIST);
4416 assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4417 assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4418 expr = cLISTOPx(oe)->op_last;
4419 cLISTOPx(oe)->op_first->op_sibling = NULL;
4420 cLISTOPx(oe)->op_last = NULL;
4421 op_free(oe);
4422
4423 return pmtrans(o, expr, repl);
4424 }
4425
4426 /* find whether we have any runtime or code elements;
4427 * at the same time, temporarily set the op_next of each DO block;
4428 * then when we LINKLIST, this will cause the DO blocks to be excluded
4429 * from the op_next chain (and from having LINKLIST recursively
4430 * applied to them). We fix up the DOs specially later */
4431
4432 is_compiletime = 1;
4433 has_code = 0;
4434 if (expr->op_type == OP_LIST) {
4435 OP *o;
4436 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4437 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4438 has_code = 1;
4439 assert(!o->op_next && o->op_sibling);
4440 o->op_next = o->op_sibling;
4441 }
4442 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4443 is_compiletime = 0;
4444 }
4445 }
4446 else if (expr->op_type != OP_CONST)
4447 is_compiletime = 0;
4448
4449 LINKLIST(expr);
4450
4451 /* fix up DO blocks; treat each one as a separate little sub */
4452
4453 if (expr->op_type == OP_LIST) {
4454 OP *o;
4455 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4456 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
4457 continue;
4458 o->op_next = NULL; /* undo temporary hack from above */
4459 scalar(o);
4460 LINKLIST(o);
4461 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
4462 LISTOP *leave = cLISTOPx(cLISTOPo->op_first);
4463 /* skip ENTER */
4464 assert(leave->op_first->op_type == OP_ENTER);
4465 assert(leave->op_first->op_sibling);
4466 o->op_next = leave->op_first->op_sibling;
4467 /* skip LEAVE */
4468 assert(leave->op_flags & OPf_KIDS);
4469 assert(leave->op_last->op_next = (OP*)leave);
4470 leave->op_next = NULL; /* stop on last op */
4471 op_null((OP*)leave);
4472 }
4473 else {
4474 /* skip SCOPE */
4475 OP *scope = cLISTOPo->op_first;
4476 assert(scope->op_type == OP_SCOPE);
4477 assert(scope->op_flags & OPf_KIDS);
4478 scope->op_next = NULL; /* stop on last op */
4479 op_null(scope);
4480 }
4481 /* have to peep the DOs individually as we've removed it from
4482 * the op_next chain */
4483 CALL_PEEP(o);
4484 if (is_compiletime)
4485 /* runtime finalizes as part of finalizing whole tree */
4486 finalize_optree(o);
4487 }
4488 }
4489
4490 PL_hints |= HINT_BLOCK_SCOPE;
4491 pm = (PMOP*)o;
4492 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
4493
4494 if (is_compiletime) {
4495 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4496 regexp_engine const *eng = current_re_engine();
4497
4498 if (o->op_flags & OPf_SPECIAL)
4499 rx_flags |= RXf_SPLIT;
4500
4501 if (!has_code || !eng->op_comp) {
4502 /* compile-time simple constant pattern */
4503
4504 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
4505 /* whoops! we guessed that a qr// had a code block, but we
4506 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
4507 * that isn't required now. Note that we have to be pretty
4508 * confident that nothing used that CV's pad while the
4509 * regex was parsed */
4510 assert(AvFILLp(PL_comppad) == 0); /* just @_ */
4511 /* But we know that one op is using this CV's slab. */
4512 cv_forget_slab(PL_compcv);
4513 LEAVE_SCOPE(floor);
4514 pm->op_pmflags &= ~PMf_HAS_CV;
4515 }
4516
4517 PM_SETRE(pm,
4518 eng->op_comp
4519 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4520 rx_flags, pm->op_pmflags)
4521 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4522 rx_flags, pm->op_pmflags)
4523 );
4524#ifdef PERL_MAD
4525 op_getmad(expr,(OP*)pm,'e');
4526#else
4527 op_free(expr);
4528#endif
4529 }
4530 else {
4531 /* compile-time pattern that includes literal code blocks */
4532 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4533 rx_flags,
4534 (pm->op_pmflags |
4535 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
4536 );
4537 PM_SETRE(pm, re);
4538 if (pm->op_pmflags & PMf_HAS_CV) {
4539 CV *cv;
4540 /* this QR op (and the anon sub we embed it in) is never
4541 * actually executed. It's just a placeholder where we can
4542 * squirrel away expr in op_code_list without the peephole
4543 * optimiser etc processing it for a second time */
4544 OP *qr = newPMOP(OP_QR, 0);
4545 ((PMOP*)qr)->op_code_list = expr;
4546
4547 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
4548 SvREFCNT_inc_simple_void(PL_compcv);
4549 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
4550 ((struct regexp *)SvANY(re))->qr_anoncv = cv;
4551
4552 /* attach the anon CV to the pad so that
4553 * pad_fixup_inner_anons() can find it */
4554 (void)pad_add_anon(cv, o->op_type);
4555 SvREFCNT_inc_simple_void(cv);
4556 }
4557 else {
4558 pm->op_code_list = expr;
4559 }
4560 }
4561 }
4562 else {
4563 /* runtime pattern: build chain of regcomp etc ops */
4564 bool reglist;
4565 PADOFFSET cv_targ = 0;
4566
4567 reglist = isreg && expr->op_type == OP_LIST;
4568 if (reglist)
4569 op_null(expr);
4570
4571 if (has_code) {
4572 pm->op_code_list = expr;
4573 /* don't free op_code_list; its ops are embedded elsewhere too */
4574 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
4575 }
4576
4577 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
4578 * to allow its op_next to be pointed past the regcomp and
4579 * preceding stacking ops;
4580 * OP_REGCRESET is there to reset taint before executing the
4581 * stacking ops */
4582 if (pm->op_pmflags & PMf_KEEP || PL_tainting)
4583 expr = newUNOP((PL_tainting ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
4584
4585 if (pm->op_pmflags & PMf_HAS_CV) {
4586 /* we have a runtime qr with literal code. This means
4587 * that the qr// has been wrapped in a new CV, which
4588 * means that runtime consts, vars etc will have been compiled
4589 * against a new pad. So... we need to execute those ops
4590 * within the environment of the new CV. So wrap them in a call
4591 * to a new anon sub. i.e. for
4592 *
4593 * qr/a$b(?{...})/,
4594 *
4595 * we build an anon sub that looks like
4596 *
4597 * sub { "a", $b, '(?{...})' }
4598 *
4599 * and call it, passing the returned list to regcomp.
4600 * Or to put it another way, the list of ops that get executed
4601 * are:
4602 *
4603 * normal PMf_HAS_CV
4604 * ------ -------------------
4605 * pushmark (for regcomp)
4606 * pushmark (for entersub)
4607 * pushmark (for refgen)
4608 * anoncode
4609 * refgen
4610 * entersub
4611 * regcreset regcreset
4612 * pushmark pushmark
4613 * const("a") const("a")
4614 * gvsv(b) gvsv(b)
4615 * const("(?{...})") const("(?{...})")
4616 * leavesub
4617 * regcomp regcomp
4618 */
4619
4620 SvREFCNT_inc_simple_void(PL_compcv);
4621 /* these lines are just an unrolled newANONATTRSUB */
4622 expr = newSVOP(OP_ANONCODE, 0,
4623 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
4624 cv_targ = expr->op_targ;
4625 expr = newUNOP(OP_REFGEN, 0, expr);
4626
4627 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr))));
4628 }
4629
4630 NewOp(1101, rcop, 1, LOGOP);
4631 rcop->op_type = OP_REGCOMP;
4632 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
4633 rcop->op_first = scalar(expr);
4634 rcop->op_flags |= OPf_KIDS
4635 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
4636 | (reglist ? OPf_STACKED : 0);
4637 rcop->op_private = 0;
4638 rcop->op_other = o;
4639 rcop->op_targ = cv_targ;
4640
4641 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
4642 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
4643
4644 /* establish postfix order */
4645 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
4646 LINKLIST(expr);
4647 rcop->op_next = expr;
4648 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
4649 }
4650 else {
4651 rcop->op_next = LINKLIST(expr);
4652 expr->op_next = (OP*)rcop;
4653 }
4654
4655 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
4656 }
4657
4658 if (repl) {
4659 OP *curop;
4660 if (pm->op_pmflags & PMf_EVAL) {
4661 curop = NULL;
4662 if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
4663 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
4664 }
4665 else if (repl->op_type == OP_CONST)
4666 curop = repl;
4667 else {
4668 OP *lastop = NULL;
4669 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
4670 if (curop->op_type == OP_SCOPE
4671 || curop->op_type == OP_LEAVE
4672 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
4673 if (curop->op_type == OP_GV) {
4674 GV * const gv = cGVOPx_gv(curop);
4675 repl_has_vars = 1;
4676 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
4677 break;
4678 }
4679 else if (curop->op_type == OP_RV2CV)
4680 break;
4681 else if (curop->op_type == OP_RV2SV ||
4682 curop->op_type == OP_RV2AV ||
4683 curop->op_type == OP_RV2HV ||
4684 curop->op_type == OP_RV2GV) {
4685 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
4686 break;
4687 }
4688 else if (curop->op_type == OP_PADSV ||
4689 curop->op_type == OP_PADAV ||
4690 curop->op_type == OP_PADHV ||
4691 curop->op_type == OP_PADANY)
4692 {
4693 repl_has_vars = 1;
4694 }
4695 else if (curop->op_type == OP_PUSHRE)
4696 NOOP; /* Okay here, dangerous in newASSIGNOP */
4697 else
4698 break;
4699 }
4700 lastop = curop;
4701 }
4702 }
4703 if (curop == repl
4704 && !(repl_has_vars
4705 && (!PM_GETRE(pm)
4706 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
4707 {
4708 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4709 op_prepend_elem(o->op_type, scalar(repl), o);
4710 }
4711 else {
4712 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
4713 pm->op_pmflags |= PMf_MAYBE_CONST;
4714 }
4715 NewOp(1101, rcop, 1, LOGOP);
4716 rcop->op_type = OP_SUBSTCONT;
4717 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
4718 rcop->op_first = scalar(repl);
4719 rcop->op_flags |= OPf_KIDS;
4720 rcop->op_private = 1;
4721 rcop->op_other = o;
4722
4723 /* establish postfix order */
4724 rcop->op_next = LINKLIST(repl);
4725 repl->op_next = (OP*)rcop;
4726
4727 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
4728 assert(!(pm->op_pmflags & PMf_ONCE));
4729 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
4730 rcop->op_next = 0;
4731 }
4732 }
4733
4734 return (OP*)pm;
4735}
4736
4737/*
4738=for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
4739
4740Constructs, checks, and returns an op of any type that involves an
4741embedded SV. I<type> is the opcode. I<flags> gives the eight bits
4742of C<op_flags>. I<sv> gives the SV to embed in the op; this function
4743takes ownership of one reference to it.
4744
4745=cut
4746*/
4747
4748OP *
4749Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
4750{
4751 dVAR;
4752 SVOP *svop;
4753
4754 PERL_ARGS_ASSERT_NEWSVOP;
4755
4756 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4757 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4758 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4759
4760 NewOp(1101, svop, 1, SVOP);
4761 svop->op_type = (OPCODE)type;
4762 svop->op_ppaddr = PL_ppaddr[type];
4763 svop->op_sv = sv;
4764 svop->op_next = (OP*)svop;
4765 svop->op_flags = (U8)flags;
4766 svop->op_private = (U8)(0 | (flags >> 8));
4767 if (PL_opargs[type] & OA_RETSCALAR)
4768 scalar((OP*)svop);
4769 if (PL_opargs[type] & OA_TARGET)
4770 svop->op_targ = pad_alloc(type, SVs_PADTMP);
4771 return CHECKOP(type, svop);
4772}
4773
4774#ifdef USE_ITHREADS
4775
4776/*
4777=for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
4778
4779Constructs, checks, and returns an op of any type that involves a
4780reference to a pad element. I<type> is the opcode. I<flags> gives the
4781eight bits of C<op_flags>. A pad slot is automatically allocated, and
4782is populated with I<sv>; this function takes ownership of one reference
4783to it.
4784
4785This function only exists if Perl has been compiled to use ithreads.
4786
4787=cut
4788*/
4789
4790OP *
4791Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
4792{
4793 dVAR;
4794 PADOP *padop;
4795
4796 PERL_ARGS_ASSERT_NEWPADOP;
4797
4798 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4799 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4800 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4801
4802 NewOp(1101, padop, 1, PADOP);
4803 padop->op_type = (OPCODE)type;
4804 padop->op_ppaddr = PL_ppaddr[type];
4805 padop->op_padix = pad_alloc(type, SVs_PADTMP);
4806 SvREFCNT_dec(PAD_SVl(padop->op_padix));
4807 PAD_SETSV(padop->op_padix, sv);
4808 assert(sv);
4809 SvPADTMP_on(sv);
4810 padop->op_next = (OP*)padop;
4811 padop->op_flags = (U8)flags;
4812 if (PL_opargs[type] & OA_RETSCALAR)
4813 scalar((OP*)padop);
4814 if (PL_opargs[type] & OA_TARGET)
4815 padop->op_targ = pad_alloc(type, SVs_PADTMP);
4816 return CHECKOP(type, padop);
4817}
4818
4819#endif /* !USE_ITHREADS */
4820
4821/*
4822=for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
4823
4824Constructs, checks, and returns an op of any type that involves an
4825embedded reference to a GV. I<type> is the opcode. I<flags> gives the
4826eight bits of C<op_flags>. I<gv> identifies the GV that the op should
4827reference; calling this function does not transfer ownership of any
4828reference to it.
4829
4830=cut
4831*/
4832
4833OP *
4834Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
4835{
4836 dVAR;
4837
4838 PERL_ARGS_ASSERT_NEWGVOP;
4839
4840#ifdef USE_ITHREADS
4841 GvIN_PAD_on(gv);
4842 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4843#else
4844 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4845#endif
4846}
4847
4848/*
4849=for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
4850
4851Constructs, checks, and returns an op of any type that involves an
4852embedded C-level pointer (PV). I<type> is the opcode. I<flags> gives
4853the eight bits of C<op_flags>. I<pv> supplies the C-level pointer, which
4854must have been allocated using L</PerlMemShared_malloc>; the memory will
4855be freed when the op is destroyed.
4856
4857=cut
4858*/
4859
4860OP *
4861Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
4862{
4863 dVAR;
4864 const bool utf8 = cBOOL(flags & SVf_UTF8);
4865 PVOP *pvop;
4866
4867 flags &= ~SVf_UTF8;
4868
4869 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4870 || type == OP_RUNCV
4871 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4872
4873 NewOp(1101, pvop, 1, PVOP);
4874 pvop->op_type = (OPCODE)type;
4875 pvop->op_ppaddr = PL_ppaddr[type];
4876 pvop->op_pv = pv;
4877 pvop->op_next = (OP*)pvop;
4878 pvop->op_flags = (U8)flags;
4879 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
4880 if (PL_opargs[type] & OA_RETSCALAR)
4881 scalar((OP*)pvop);
4882 if (PL_opargs[type] & OA_TARGET)
4883 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
4884 return CHECKOP(type, pvop);
4885}
4886
4887#ifdef PERL_MAD
4888OP*
4889#else
4890void
4891#endif
4892Perl_package(pTHX_ OP *o)
4893{
4894 dVAR;
4895 SV *const sv = cSVOPo->op_sv;
4896#ifdef PERL_MAD
4897 OP *pegop;
4898#endif
4899
4900 PERL_ARGS_ASSERT_PACKAGE;
4901
4902 SAVEGENERICSV(PL_curstash);
4903 save_item(PL_curstname);
4904
4905 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
4906
4907 sv_setsv(PL_curstname, sv);
4908
4909 PL_hints |= HINT_BLOCK_SCOPE;
4910 PL_parser->copline = NOLINE;
4911 PL_parser->expect = XSTATE;
4912
4913#ifndef PERL_MAD
4914 op_free(o);
4915#else
4916 if (!PL_madskills) {
4917 op_free(o);
4918 return NULL;
4919 }
4920
4921 pegop = newOP(OP_NULL,0);
4922 op_getmad(o,pegop,'P');
4923 return pegop;
4924#endif
4925}
4926
4927void
4928Perl_package_version( pTHX_ OP *v )
4929{
4930 dVAR;
4931 U32 savehints = PL_hints;
4932 PERL_ARGS_ASSERT_PACKAGE_VERSION;
4933 PL_hints &= ~HINT_STRICT_VARS;
4934 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
4935 PL_hints = savehints;
4936 op_free(v);
4937}
4938
4939#ifdef PERL_MAD
4940OP*
4941#else
4942void
4943#endif
4944Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
4945{
4946 dVAR;
4947 OP *pack;
4948 OP *imop;
4949 OP *veop;
4950#ifdef PERL_MAD
4951 OP *pegop = PL_madskills ? newOP(OP_NULL,0) : NULL;
4952#endif
4953 SV *use_version = NULL;
4954
4955 PERL_ARGS_ASSERT_UTILIZE;
4956
4957 if (idop->op_type != OP_CONST)
4958 Perl_croak(aTHX_ "Module name must be constant");
4959
4960 if (PL_madskills)
4961 op_getmad(idop,pegop,'U');
4962
4963 veop = NULL;
4964
4965 if (version) {
4966 SV * const vesv = ((SVOP*)version)->op_sv;
4967
4968 if (PL_madskills)
4969 op_getmad(version,pegop,'V');
4970 if (!arg && !SvNIOKp(vesv)) {
4971 arg = version;
4972 }
4973 else {
4974 OP *pack;
4975 SV *meth;
4976
4977 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
4978 Perl_croak(aTHX_ "Version number must be a constant number");
4979
4980 /* Make copy of idop so we don't free it twice */
4981 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
4982
4983 /* Fake up a method call to VERSION */
4984 meth = newSVpvs_share("VERSION");
4985 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
4986 op_append_elem(OP_LIST,
4987 op_prepend_elem(OP_LIST, pack, list(version)),
4988 newSVOP(OP_METHOD_NAMED, 0, meth)));
4989 }
4990 }
4991
4992 /* Fake up an import/unimport */
4993 if (arg && arg->op_type == OP_STUB) {
4994 if (PL_madskills)
4995 op_getmad(arg,pegop,'S');
4996 imop = arg; /* no import on explicit () */
4997 }
4998 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
4999 imop = NULL; /* use 5.0; */
5000 if (aver)
5001 use_version = ((SVOP*)idop)->op_sv;
5002 else
5003 idop->op_private |= OPpCONST_NOVER;
5004 }
5005 else {
5006 SV *meth;
5007
5008 if (PL_madskills)
5009 op_getmad(arg,pegop,'A');
5010
5011 /* Make copy of idop so we don't free it twice */
5012 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
5013
5014 /* Fake up a method call to import/unimport */
5015 meth = aver
5016 ? newSVpvs_share("import") : newSVpvs_share("unimport");
5017 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
5018 op_append_elem(OP_LIST,
5019 op_prepend_elem(OP_LIST, pack, list(arg)),
5020 newSVOP(OP_METHOD_NAMED, 0, meth)));
5021 }
5022
5023 /* Fake up the BEGIN {}, which does its thing immediately. */
5024 newATTRSUB(floor,
5025 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
5026 NULL,
5027 NULL,
5028 op_append_elem(OP_LINESEQ,
5029 op_append_elem(OP_LINESEQ,
5030 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
5031 newSTATEOP(0, NULL, veop)),
5032 newSTATEOP(0, NULL, imop) ));
5033
5034 if (use_version) {
5035 /* Enable the
5036 * feature bundle that corresponds to the required version. */
5037 use_version = sv_2mortal(new_version(use_version));
5038 S_enable_feature_bundle(aTHX_ use_version);
5039
5040 /* If a version >= 5.11.0 is requested, strictures are on by default! */
5041 if (vcmp(use_version,
5042 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
5043 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
5044 PL_hints |= HINT_STRICT_REFS;
5045 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
5046 PL_hints |= HINT_STRICT_SUBS;
5047 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
5048 PL_hints |= HINT_STRICT_VARS;
5049 }
5050 /* otherwise they are off */
5051 else {
5052 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
5053 PL_hints &= ~HINT_STRICT_REFS;
5054 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
5055 PL_hints &= ~HINT_STRICT_SUBS;
5056 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
5057 PL_hints &= ~HINT_STRICT_VARS;
5058 }
5059 }
5060
5061 /* The "did you use incorrect case?" warning used to be here.
5062 * The problem is that on case-insensitive filesystems one
5063 * might get false positives for "use" (and "require"):
5064 * "use Strict" or "require CARP" will work. This causes
5065 * portability problems for the script: in case-strict
5066 * filesystems the script will stop working.
5067 *
5068 * The "incorrect case" warning checked whether "use Foo"
5069 * imported "Foo" to your namespace, but that is wrong, too:
5070 * there is no requirement nor promise in the language that
5071 * a Foo.pm should or would contain anything in package "Foo".
5072 *
5073 * There is very little Configure-wise that can be done, either:
5074 * the case-sensitivity of the build filesystem of Perl does not
5075 * help in guessing the case-sensitivity of the runtime environment.
5076 */
5077
5078 PL_hints |= HINT_BLOCK_SCOPE;
5079 PL_parser->copline = NOLINE;
5080 PL_parser->expect = XSTATE;
5081 PL_cop_seqmax++; /* Purely for B::*'s benefit */
5082 if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */
5083 PL_cop_seqmax++;
5084
5085#ifdef PERL_MAD
5086 return pegop;
5087#endif
5088}
5089
5090/*
5091=head1 Embedding Functions
5092
5093=for apidoc load_module
5094
5095Loads the module whose name is pointed to by the string part of name.
5096Note that the actual module name, not its filename, should be given.
5097Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
5098PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
5099(or 0 for no flags). ver, if specified and not NULL, provides version semantics
5100similar to C<use Foo::Bar VERSION>. The optional trailing SV*
5101arguments can be used to specify arguments to the module's import()
5102method, similar to C<use Foo::Bar VERSION LIST>. They must be
5103terminated with a final NULL pointer. Note that this list can only
5104be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
5105Otherwise at least a single NULL pointer to designate the default
5106import list is required.
5107
5108The reference count for each specified C<SV*> parameter is decremented.
5109
5110=cut */
5111
5112void
5113Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
5114{
5115 va_list args;
5116
5117 PERL_ARGS_ASSERT_LOAD_MODULE;
5118
5119 va_start(args, ver);
5120 vload_module(flags, name, ver, &args);
5121 va_end(args);
5122}
5123
5124#ifdef PERL_IMPLICIT_CONTEXT
5125void
5126Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
5127{
5128 dTHX;
5129 va_list args;
5130 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
5131 va_start(args, ver);
5132 vload_module(flags, name, ver, &args);
5133 va_end(args);
5134}
5135#endif
5136
5137void
5138Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
5139{
5140 dVAR;
5141 OP *veop, *imop;
5142 OP * const modname = newSVOP(OP_CONST, 0, name);
5143
5144 PERL_ARGS_ASSERT_VLOAD_MODULE;
5145
5146 modname->op_private |= OPpCONST_BARE;
5147 if (ver) {
5148 veop = newSVOP(OP_CONST, 0, ver);
5149 }
5150 else
5151 veop = NULL;
5152 if (flags & PERL_LOADMOD_NOIMPORT) {
5153 imop = sawparens(newNULLLIST());
5154 }
5155 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
5156 imop = va_arg(*args, OP*);
5157 }
5158 else {
5159 SV *sv;
5160 imop = NULL;
5161 sv = va_arg(*args, SV*);
5162 while (sv) {
5163 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
5164 sv = va_arg(*args, SV*);
5165 }
5166 }
5167
5168 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
5169 * that it has a PL_parser to play with while doing that, and also
5170 * that it doesn't mess with any existing parser, by creating a tmp
5171 * new parser with lex_start(). This won't actually be used for much,
5172 * since pp_require() will create another parser for the real work. */
5173
5174 ENTER;
5175 SAVEVPTR(PL_curcop);
5176 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
5177 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
5178 veop, modname, imop);
5179 LEAVE;
5180}
5181
5182OP *
5183Perl_dofile(pTHX_ OP *term, I32 force_builtin)
5184{
5185 dVAR;
5186 OP *doop;
5187 GV *gv = NULL;
5188
5189 PERL_ARGS_ASSERT_DOFILE;
5190
5191 if (!force_builtin) {
5192 gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
5193 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
5194 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
5195 gv = gvp ? *gvp : NULL;
5196 }
5197 }
5198
5199 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5200 doop = newUNOP(OP_ENTERSUB, OPf_STACKED,
5201 op_append_elem(OP_LIST, term,
5202 scalar(newUNOP(OP_RV2CV, 0,
5203 newGVOP(OP_GV, 0, gv)))));
5204 }
5205 else {
5206 doop = newUNOP(OP_DOFILE, 0, scalar(term));
5207 }
5208 return doop;
5209}
5210
5211/*
5212=head1 Optree construction
5213
5214=for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval
5215
5216Constructs, checks, and returns an C<lslice> (list slice) op. I<flags>
5217gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
5218be set automatically, and, shifted up eight bits, the eight bits of
5219C<op_private>, except that the bit with value 1 or 2 is automatically
5220set as required. I<listval> and I<subscript> supply the parameters of
5221the slice; they are consumed by this function and become part of the
5222constructed op tree.
5223
5224=cut
5225*/
5226
5227OP *
5228Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
5229{
5230 return newBINOP(OP_LSLICE, flags,
5231 list(force_list(subscript)),
5232 list(force_list(listval)) );
5233}
5234
5235STATIC I32
5236S_is_list_assignment(pTHX_ register const OP *o)
5237{
5238 unsigned type;
5239 U8 flags;
5240
5241 if (!o)
5242 return TRUE;
5243
5244 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
5245 o = cUNOPo->op_first;
5246
5247 flags = o->op_flags;
5248 type = o->op_type;
5249 if (type == OP_COND_EXPR) {
5250 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
5251 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
5252
5253 if (t && f)
5254 return TRUE;
5255 if (t || f)
5256 yyerror("Assignment to both a list and a scalar");
5257 return FALSE;
5258 }
5259
5260 if (type == OP_LIST &&
5261 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
5262 o->op_private & OPpLVAL_INTRO)
5263 return FALSE;
5264
5265 if (type == OP_LIST || flags & OPf_PARENS ||
5266 type == OP_RV2AV || type == OP_RV2HV ||
5267 type == OP_ASLICE || type == OP_HSLICE)
5268 return TRUE;
5269
5270 if (type == OP_PADAV || type == OP_PADHV)
5271 return TRUE;
5272
5273 if (type == OP_RV2SV)
5274 return FALSE;
5275
5276 return FALSE;
5277}
5278
5279/*
5280 Helper function for newASSIGNOP to detection commonality between the
5281 lhs and the rhs. Marks all variables with PL_generation. If it
5282 returns TRUE the assignment must be able to handle common variables.
5283*/
5284PERL_STATIC_INLINE bool
5285S_aassign_common_vars(pTHX_ OP* o)
5286{
5287 OP *curop;
5288 for (curop = cUNOPo->op_first; curop; curop=curop->op_sibling) {
5289 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
5290 if (curop->op_type == OP_GV) {
5291 GV *gv = cGVOPx_gv(curop);
5292 if (gv == PL_defgv
5293 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
5294 return TRUE;
5295 GvASSIGN_GENERATION_set(gv, PL_generation);
5296 }
5297 else if (curop->op_type == OP_PADSV ||
5298 curop->op_type == OP_PADAV ||
5299 curop->op_type == OP_PADHV ||
5300 curop->op_type == OP_PADANY)
5301 {
5302 if (PAD_COMPNAME_GEN(curop->op_targ)
5303 == (STRLEN)PL_generation)
5304 return TRUE;
5305 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
5306
5307 }
5308 else if (curop->op_type == OP_RV2CV)
5309 return TRUE;
5310 else if (curop->op_type == OP_RV2SV ||
5311 curop->op_type == OP_RV2AV ||
5312 curop->op_type == OP_RV2HV ||
5313 curop->op_type == OP_RV2GV) {
5314 if (cUNOPx(curop)->op_first->op_type != OP_GV) /* funny deref? */
5315 return TRUE;
5316 }
5317 else if (curop->op_type == OP_PUSHRE) {
5318#ifdef USE_ITHREADS
5319 if (((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff) {
5320 GV *const gv = MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff));
5321 if (gv == PL_defgv
5322 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
5323 return TRUE;
5324 GvASSIGN_GENERATION_set(gv, PL_generation);
5325 }
5326#else
5327 GV *const gv
5328 = ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv;
5329 if (gv) {
5330 if (gv == PL_defgv
5331 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
5332 return TRUE;
5333 GvASSIGN_GENERATION_set(gv, PL_generation);
5334 }
5335#endif
5336 }
5337 else
5338 return TRUE;
5339 }
5340
5341 if (curop->op_flags & OPf_KIDS) {
5342 if (aassign_common_vars(curop))
5343 return TRUE;
5344 }
5345 }
5346 return FALSE;
5347}
5348
5349/*
5350=for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right
5351
5352Constructs, checks, and returns an assignment op. I<left> and I<right>
5353supply the parameters of the assignment; they are consumed by this
5354function and become part of the constructed op tree.
5355
5356If I<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
5357a suitable conditional optree is constructed. If I<optype> is the opcode
5358of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
5359performs the binary operation and assigns the result to the left argument.
5360Either way, if I<optype> is non-zero then I<flags> has no effect.
5361
5362If I<optype> is zero, then a plain scalar or list assignment is
5363constructed. Which type of assignment it is is automatically determined.
5364I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
5365will be set automatically, and, shifted up eight bits, the eight bits
5366of C<op_private>, except that the bit with value 1 or 2 is automatically
5367set as required.
5368
5369=cut
5370*/
5371
5372OP *
5373Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
5374{
5375 dVAR;
5376 OP *o;
5377
5378 if (optype) {
5379 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
5380 return newLOGOP(optype, 0,
5381 op_lvalue(scalar(left), optype),
5382 newUNOP(OP_SASSIGN, 0, scalar(right)));
5383 }
5384 else {
5385 return newBINOP(optype, OPf_STACKED,
5386 op_lvalue(scalar(left), optype), scalar(right));
5387 }
5388 }
5389
5390 if (is_list_assignment(left)) {
5391 static const char no_list_state[] = "Initialization of state variables"
5392 " in list context currently forbidden";
5393 OP *curop;
5394 bool maybe_common_vars = TRUE;
5395
5396 PL_modcount = 0;
5397 left = op_lvalue(left, OP_AASSIGN);
5398 curop = list(force_list(left));
5399 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
5400 o->op_private = (U8)(0 | (flags >> 8));
5401
5402 if ((left->op_type == OP_LIST
5403 || (left->op_type == OP_NULL && left->op_targ == OP_LIST)))
5404 {
5405 OP* lop = ((LISTOP*)left)->op_first;
5406 maybe_common_vars = FALSE;
5407 while (lop) {
5408 if (lop->op_type == OP_PADSV ||
5409 lop->op_type == OP_PADAV ||
5410 lop->op_type == OP_PADHV ||
5411 lop->op_type == OP_PADANY) {
5412 if (!(lop->op_private & OPpLVAL_INTRO))
5413 maybe_common_vars = TRUE;
5414
5415 if (lop->op_private & OPpPAD_STATE) {
5416 if (left->op_private & OPpLVAL_INTRO) {
5417 /* Each variable in state($a, $b, $c) = ... */
5418 }
5419 else {
5420 /* Each state variable in
5421 (state $a, my $b, our $c, $d, undef) = ... */
5422 }
5423 yyerror(no_list_state);
5424 } else {
5425 /* Each my variable in
5426 (state $a, my $b, our $c, $d, undef) = ... */
5427 }
5428 } else if (lop->op_type == OP_UNDEF ||
5429 lop->op_type == OP_PUSHMARK) {
5430 /* undef may be interesting in
5431 (state $a, undef, state $c) */
5432 } else {
5433 /* Other ops in the list. */
5434 maybe_common_vars = TRUE;
5435 }
5436 lop = lop->op_sibling;
5437 }
5438 }
5439 else if ((left->op_private & OPpLVAL_INTRO)
5440 && ( left->op_type == OP_PADSV
5441 || left->op_type == OP_PADAV
5442 || left->op_type == OP_PADHV
5443 || left->op_type == OP_PADANY))
5444 {
5445 if (left->op_type == OP_PADSV) maybe_common_vars = FALSE;
5446 if (left->op_private & OPpPAD_STATE) {
5447 /* All single variable list context state assignments, hence
5448 state ($a) = ...
5449 (state $a) = ...
5450 state @a = ...
5451 state (@a) = ...
5452 (state @a) = ...
5453 state %a = ...
5454 state (%a) = ...
5455 (state %a) = ...
5456 */
5457 yyerror(no_list_state);
5458 }
5459 }
5460
5461 /* PL_generation sorcery:
5462 * an assignment like ($a,$b) = ($c,$d) is easier than
5463 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
5464 * To detect whether there are common vars, the global var
5465 * PL_generation is incremented for each assign op we compile.
5466 * Then, while compiling the assign op, we run through all the
5467 * variables on both sides of the assignment, setting a spare slot
5468 * in each of them to PL_generation. If any of them already have
5469 * that value, we know we've got commonality. We could use a
5470 * single bit marker, but then we'd have to make 2 passes, first
5471 * to clear the flag, then to test and set it. To find somewhere
5472 * to store these values, evil chicanery is done with SvUVX().
5473 */
5474
5475 if (maybe_common_vars) {
5476 PL_generation++;
5477 if (aassign_common_vars(o))
5478 o->op_private |= OPpASSIGN_COMMON;
5479 LINKLIST(o);
5480 }
5481
5482 if (right && right->op_type == OP_SPLIT && !PL_madskills) {
5483 OP* tmpop = ((LISTOP*)right)->op_first;
5484 if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
5485 PMOP * const pm = (PMOP*)tmpop;
5486 if (left->op_type == OP_RV2AV &&
5487 !(left->op_private & OPpLVAL_INTRO) &&
5488 !(o->op_private & OPpASSIGN_COMMON) )
5489 {
5490 tmpop = ((UNOP*)left)->op_first;
5491 if (tmpop->op_type == OP_GV
5492#ifdef USE_ITHREADS
5493 && !pm->op_pmreplrootu.op_pmtargetoff
5494#else
5495 && !pm->op_pmreplrootu.op_pmtargetgv
5496#endif
5497 ) {
5498#ifdef USE_ITHREADS
5499 pm->op_pmreplrootu.op_pmtargetoff
5500 = cPADOPx(tmpop)->op_padix;
5501 cPADOPx(tmpop)->op_padix = 0; /* steal it */
5502#else
5503 pm->op_pmreplrootu.op_pmtargetgv
5504 = MUTABLE_GV(cSVOPx(tmpop)->op_sv);
5505 cSVOPx(tmpop)->op_sv = NULL; /* steal it */
5506#endif
5507 pm->op_pmflags |= PMf_ONCE;
5508 tmpop = cUNOPo->op_first; /* to list (nulled) */
5509 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
5510 tmpop->op_sibling = NULL; /* don't free split */
5511 right->op_next = tmpop->op_next; /* fix starting loc */
5512 op_free(o); /* blow off assign */
5513 right->op_flags &= ~OPf_WANT;
5514 /* "I don't know and I don't care." */
5515 return right;
5516 }
5517 }
5518 else {
5519 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
5520 ((LISTOP*)right)->op_last->op_type == OP_CONST)
5521 {
5522 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
5523 if (SvIOK(sv) && SvIVX(sv) == 0)
5524 sv_setiv(sv, PL_modcount+1);
5525 }
5526 }
5527 }
5528 }
5529 return o;
5530 }
5531 if (!right)
5532 right = newOP(OP_UNDEF, 0);
5533 if (right->op_type == OP_READLINE) {
5534 right->op_flags |= OPf_STACKED;
5535 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
5536 scalar(right));
5537 }
5538 else {
5539 o = newBINOP(OP_SASSIGN, flags,
5540 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
5541 }
5542 return o;
5543}
5544
5545/*
5546=for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o
5547
5548Constructs a state op (COP). The state op is normally a C<nextstate> op,
5549but will be a C<dbstate> op if debugging is enabled for currently-compiled
5550code. The state op is populated from L</PL_curcop> (or L</PL_compiling>).
5551If I<label> is non-null, it supplies the name of a label to attach to
5552the state op; this function takes ownership of the memory pointed at by
5553I<label>, and will free it. I<flags> gives the eight bits of C<op_flags>
5554for the state op.
5555
5556If I<o> is null, the state op is returned. Otherwise the state op is
5557combined with I<o> into a C<lineseq> list op, which is returned. I<o>
5558is consumed by this function and becomes part of the returned op tree.
5559
5560=cut
5561*/
5562
5563OP *
5564Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
5565{
5566 dVAR;
5567 const U32 seq = intro_my();
5568 const U32 utf8 = flags & SVf_UTF8;
5569 register COP *cop;
5570
5571 flags &= ~SVf_UTF8;
5572
5573 NewOp(1101, cop, 1, COP);
5574 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
5575 cop->op_type = OP_DBSTATE;
5576 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
5577 }
5578 else {
5579 cop->op_type = OP_NEXTSTATE;
5580 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
5581 }
5582 cop->op_flags = (U8)flags;
5583 CopHINTS_set(cop, PL_hints);
5584#ifdef NATIVE_HINTS
5585 cop->op_private |= NATIVE_HINTS;
5586#endif
5587 CopHINTS_set(&PL_compiling, CopHINTS_get(cop));
5588 cop->op_next = (OP*)cop;
5589
5590 cop->cop_seq = seq;
5591 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
5592 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
5593 if (label) {
5594 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
5595
5596 PL_hints |= HINT_BLOCK_SCOPE;
5597 /* It seems that we need to defer freeing this pointer, as other parts
5598 of the grammar end up wanting to copy it after this op has been
5599 created. */
5600 SAVEFREEPV(label);
5601 }
5602
5603 if (PL_parser && PL_parser->copline == NOLINE)
5604 CopLINE_set(cop, CopLINE(PL_curcop));
5605 else {
5606 CopLINE_set(cop, PL_parser->copline);
5607 if (PL_parser)
5608 PL_parser->copline = NOLINE;
5609 }
5610#ifdef USE_ITHREADS
5611 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
5612#else
5613 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
5614#endif
5615 CopSTASH_set(cop, PL_curstash);
5616
5617 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash) {
5618 /* this line can have a breakpoint - store the cop in IV */
5619 AV *av = CopFILEAVx(PL_curcop);
5620 if (av) {
5621 SV * const * const svp = av_fetch(av, (I32)CopLINE(cop), FALSE);
5622 if (svp && *svp != &PL_sv_undef ) {
5623 (void)SvIOK_on(*svp);
5624 SvIV_set(*svp, PTR2IV(cop));
5625 }
5626 }
5627 }
5628
5629 if (flags & OPf_SPECIAL)
5630 op_null((OP*)cop);
5631 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
5632}
5633
5634/*
5635=for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other
5636
5637Constructs, checks, and returns a logical (flow control) op. I<type>
5638is the opcode. I<flags> gives the eight bits of C<op_flags>, except
5639that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
5640the eight bits of C<op_private>, except that the bit with value 1 is
5641automatically set. I<first> supplies the expression controlling the
5642flow, and I<other> supplies the side (alternate) chain of ops; they are
5643consumed by this function and become part of the constructed op tree.
5644
5645=cut
5646*/
5647
5648OP *
5649Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
5650{
5651 dVAR;
5652
5653 PERL_ARGS_ASSERT_NEWLOGOP;
5654
5655 return new_logop(type, flags, &first, &other);
5656}
5657
5658STATIC OP *
5659S_search_const(pTHX_ OP *o)
5660{
5661 PERL_ARGS_ASSERT_SEARCH_CONST;
5662
5663 switch (o->op_type) {
5664 case OP_CONST:
5665 return o;
5666 case OP_NULL:
5667 if (o->op_flags & OPf_KIDS)
5668 return search_const(cUNOPo->op_first);
5669 break;
5670 case OP_LEAVE:
5671 case OP_SCOPE:
5672 case OP_LINESEQ:
5673 {
5674 OP *kid;
5675 if (!(o->op_flags & OPf_KIDS))
5676 return NULL;
5677 kid = cLISTOPo->op_first;
5678 do {
5679 switch (kid->op_type) {
5680 case OP_ENTER:
5681 case OP_NULL:
5682 case OP_NEXTSTATE:
5683 kid = kid->op_sibling;
5684 break;
5685 default:
5686 if (kid != cLISTOPo->op_last)
5687 return NULL;
5688 goto last;
5689 }
5690 } while (kid);
5691 if (!kid)
5692 kid = cLISTOPo->op_last;
5693last:
5694 return search_const(kid);
5695 }
5696 }
5697
5698 return NULL;
5699}
5700
5701STATIC OP *
5702S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
5703{
5704 dVAR;
5705 LOGOP *logop;
5706 OP *o;
5707 OP *first;
5708 OP *other;
5709 OP *cstop = NULL;
5710 int prepend_not = 0;
5711
5712 PERL_ARGS_ASSERT_NEW_LOGOP;
5713
5714 first = *firstp;
5715 other = *otherp;
5716
5717 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
5718 return newBINOP(type, flags, scalar(first), scalar(other));
5719
5720 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP);
5721
5722 scalarboolean(first);
5723 /* optimize AND and OR ops that have NOTs as children */
5724 if (first->op_type == OP_NOT
5725 && (first->op_flags & OPf_KIDS)
5726 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
5727 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
5728 && !PL_madskills) {
5729 if (type == OP_AND || type == OP_OR) {
5730 if (type == OP_AND)
5731 type = OP_OR;
5732 else
5733 type = OP_AND;
5734 op_null(first);
5735 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
5736 op_null(other);
5737 prepend_not = 1; /* prepend a NOT op later */
5738 }
5739 }
5740 }
5741 /* search for a constant op that could let us fold the test */
5742 if ((cstop = search_const(first))) {
5743 if (cstop->op_private & OPpCONST_STRICT)
5744 no_bareword_allowed(cstop);
5745 else if ((cstop->op_private & OPpCONST_BARE))
5746 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
5747 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) ||
5748 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) ||
5749 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) {
5750 *firstp = NULL;
5751 if (other->op_type == OP_CONST)
5752 other->op_private |= OPpCONST_SHORTCIRCUIT;
5753 if (PL_madskills) {
5754 OP *newop = newUNOP(OP_NULL, 0, other);
5755 op_getmad(first, newop, '1');
5756 newop->op_targ = type; /* set "was" field */
5757 return newop;
5758 }
5759 op_free(first);
5760 if (other->op_type == OP_LEAVE)
5761 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
5762 else if (other->op_type == OP_MATCH
5763 || other->op_type == OP_SUBST
5764 || other->op_type == OP_TRANSR
5765 || other->op_type == OP_TRANS)
5766 /* Mark the op as being unbindable with =~ */
5767 other->op_flags |= OPf_SPECIAL;
5768 else if (other->op_type == OP_CONST)
5769 other->op_private |= OPpCONST_FOLDED;
5770 return other;
5771 }
5772 else {
5773 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
5774 const OP *o2 = other;
5775 if ( ! (o2->op_type == OP_LIST
5776 && (( o2 = cUNOPx(o2)->op_first))
5777 && o2->op_type == OP_PUSHMARK
5778 && (( o2 = o2->op_sibling)) )
5779 )
5780 o2 = other;
5781 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
5782 || o2->op_type == OP_PADHV)
5783 && o2->op_private & OPpLVAL_INTRO
5784 && !(o2->op_private & OPpPAD_STATE))
5785 {
5786 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
5787 "Deprecated use of my() in false conditional");
5788 }
5789
5790 *otherp = NULL;
5791 if (first->op_type == OP_CONST)
5792 first->op_private |= OPpCONST_SHORTCIRCUIT;
5793 if (PL_madskills) {
5794 first = newUNOP(OP_NULL, 0, first);
5795 op_getmad(other, first, '2');
5796 first->op_targ = type; /* set "was" field */
5797 }
5798 else
5799 op_free(other);
5800 return first;
5801 }
5802 }
5803 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
5804 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
5805 {
5806 const OP * const k1 = ((UNOP*)first)->op_first;
5807 const OP * const k2 = k1->op_sibling;
5808 OPCODE warnop = 0;
5809 switch (first->op_type)
5810 {
5811 case OP_NULL:
5812 if (k2 && k2->op_type == OP_READLINE
5813 && (k2->op_flags & OPf_STACKED)
5814 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
5815 {
5816 warnop = k2->op_type;
5817 }
5818 break;
5819
5820 case OP_SASSIGN:
5821 if (k1->op_type == OP_READDIR
5822 || k1->op_type == OP_GLOB
5823 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
5824 || k1->op_type == OP_EACH
5825 || k1->op_type == OP_AEACH)
5826 {
5827 warnop = ((k1->op_type == OP_NULL)
5828 ? (OPCODE)k1->op_targ : k1->op_type);
5829 }
5830 break;
5831 }
5832 if (warnop) {
5833 const line_t oldline = CopLINE(PL_curcop);
5834 CopLINE_set(PL_curcop, PL_parser->copline);
5835 Perl_warner(aTHX_ packWARN(WARN_MISC),
5836 "Value of %s%s can be \"0\"; test with defined()",
5837 PL_op_desc[warnop],
5838 ((warnop == OP_READLINE || warnop == OP_GLOB)
5839 ? " construct" : "() operator"));
5840 CopLINE_set(PL_curcop, oldline);
5841 }
5842 }
5843
5844 if (!other)
5845 return first;
5846
5847 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
5848 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
5849
5850 NewOp(1101, logop, 1, LOGOP);
5851
5852 logop->op_type = (OPCODE)type;
5853 logop->op_ppaddr = PL_ppaddr[type];
5854 logop->op_first = first;
5855 logop->op_flags = (U8)(flags | OPf_KIDS);
5856 logop->op_other = LINKLIST(other);
5857 logop->op_private = (U8)(1 | (flags >> 8));
5858
5859 /* establish postfix order */
5860 logop->op_next = LINKLIST(first);
5861 first->op_next = (OP*)logop;
5862 first->op_sibling = other;
5863
5864 CHECKOP(type,logop);
5865
5866 o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 0, (OP*)logop);
5867 other->op_next = o;
5868
5869 return o;
5870}
5871
5872/*
5873=for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop
5874
5875Constructs, checks, and returns a conditional-expression (C<cond_expr>)
5876op. I<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
5877will be set automatically, and, shifted up eight bits, the eight bits of
5878C<op_private>, except that the bit with value 1 is automatically set.
5879I<first> supplies the expression selecting between the two branches,
5880and I<trueop> and I<falseop> supply the branches; they are consumed by
5881this function and become part of the constructed op tree.
5882
5883=cut
5884*/
5885
5886OP *
5887Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
5888{
5889 dVAR;
5890 LOGOP *logop;
5891 OP *start;
5892 OP *o;
5893 OP *cstop;
5894
5895 PERL_ARGS_ASSERT_NEWCONDOP;
5896
5897 if (!falseop)
5898 return newLOGOP(OP_AND, 0, first, trueop);
5899 if (!trueop)
5900 return newLOGOP(OP_OR, 0, first, falseop);
5901
5902 scalarboolean(first);
5903 if ((cstop = search_const(first))) {
5904 /* Left or right arm of the conditional? */
5905 const bool left = SvTRUE(((SVOP*)cstop)->op_sv);
5906 OP *live = left ? trueop : falseop;
5907 OP *const dead = left ? falseop : trueop;
5908 if (cstop->op_private & OPpCONST_BARE &&
5909 cstop->op_private & OPpCONST_STRICT) {
5910 no_bareword_allowed(cstop);
5911 }
5912 if (PL_madskills) {
5913 /* This is all dead code when PERL_MAD is not defined. */
5914 live = newUNOP(OP_NULL, 0, live);
5915 op_getmad(first, live, 'C');
5916 op_getmad(dead, live, left ? 'e' : 't');
5917 } else {
5918 op_free(first);
5919 op_free(dead);
5920 }
5921 if (live->op_type == OP_LEAVE)
5922 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
5923 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
5924 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
5925 /* Mark the op as being unbindable with =~ */
5926 live->op_flags |= OPf_SPECIAL;
5927 else if (live->op_type == OP_CONST)
5928 live->op_private |= OPpCONST_FOLDED;
5929 return live;
5930 }
5931 NewOp(1101, logop, 1, LOGOP);
5932 logop->op_type = OP_COND_EXPR;
5933 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
5934 logop->op_first = first;
5935 logop->op_flags = (U8)(flags | OPf_KIDS);
5936 logop->op_private = (U8)(1 | (flags >> 8));
5937 logop->op_other = LINKLIST(trueop);
5938 logop->op_next = LINKLIST(falseop);
5939
5940 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
5941 logop);
5942
5943 /* establish postfix order */
5944 start = LINKLIST(first);
5945 first->op_next = (OP*)logop;
5946
5947 first->op_sibling = trueop;
5948 trueop->op_sibling = falseop;
5949 o = newUNOP(OP_NULL, 0, (OP*)logop);
5950
5951 trueop->op_next = falseop->op_next = o;
5952
5953 o->op_next = start;
5954 return o;
5955}
5956
5957/*
5958=for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right
5959
5960Constructs and returns a C<range> op, with subordinate C<flip> and
5961C<flop> ops. I<flags> gives the eight bits of C<op_flags> for the
5962C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
5963for both the C<flip> and C<range> ops, except that the bit with value
59641 is automatically set. I<left> and I<right> supply the expressions
5965controlling the endpoints of the range; they are consumed by this function
5966and become part of the constructed op tree.
5967
5968=cut
5969*/
5970
5971OP *
5972Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
5973{
5974 dVAR;
5975 LOGOP *range;
5976 OP *flip;
5977 OP *flop;
5978 OP *leftstart;
5979 OP *o;
5980
5981 PERL_ARGS_ASSERT_NEWRANGE;
5982
5983 NewOp(1101, range, 1, LOGOP);
5984
5985 range->op_type = OP_RANGE;
5986 range->op_ppaddr = PL_ppaddr[OP_RANGE];
5987 range->op_first = left;
5988 range->op_flags = OPf_KIDS;
5989 leftstart = LINKLIST(left);
5990 range->op_other = LINKLIST(right);
5991 range->op_private = (U8)(1 | (flags >> 8));
5992
5993 left->op_sibling = right;
5994
5995 range->op_next = (OP*)range;
5996 flip = newUNOP(OP_FLIP, flags, (OP*)range);
5997 flop = newUNOP(OP_FLOP, 0, flip);
5998 o = newUNOP(OP_NULL, 0, flop);
5999 LINKLIST(flop);
6000 range->op_next = leftstart;
6001
6002 left->op_next = flip;
6003 right->op_next = flop;
6004
6005 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
6006 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
6007 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
6008 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
6009
6010 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6011 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
6012
6013 /* check barewords before they might be optimized aways */
6014 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
6015 no_bareword_allowed(left);
6016 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
6017 no_bareword_allowed(right);
6018
6019 flip->op_next = o;
6020 if (!flip->op_private || !flop->op_private)
6021 LINKLIST(o); /* blow off optimizer unless constant */
6022
6023 return o;
6024}
6025
6026/*
6027=for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block
6028
6029Constructs, checks, and returns an op tree expressing a loop. This is
6030only a loop in the control flow through the op tree; it does not have
6031the heavyweight loop structure that allows exiting the loop by C<last>
6032and suchlike. I<flags> gives the eight bits of C<op_flags> for the
6033top-level op, except that some bits will be set automatically as required.
6034I<expr> supplies the expression controlling loop iteration, and I<block>
6035supplies the body of the loop; they are consumed by this function and
6036become part of the constructed op tree. I<debuggable> is currently
6037unused and should always be 1.
6038
6039=cut
6040*/
6041
6042OP *
6043Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
6044{
6045 dVAR;
6046 OP* listop;
6047 OP* o;
6048 const bool once = block && block->op_flags & OPf_SPECIAL &&
6049 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
6050
6051 PERL_UNUSED_ARG(debuggable);
6052
6053 if (expr) {
6054 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
6055 return block; /* do {} while 0 does once */
6056 if (expr->op_type == OP_READLINE
6057 || expr->op_type == OP_READDIR
6058 || expr->op_type == OP_GLOB
6059 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
6060 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
6061 expr = newUNOP(OP_DEFINED, 0,
6062 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
6063 } else if (expr->op_flags & OPf_KIDS) {
6064 const OP * const k1 = ((UNOP*)expr)->op_first;
6065 const OP * const k2 = k1 ? k1->op_sibling : NULL;
6066 switch (expr->op_type) {
6067 case OP_NULL:
6068 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
6069 && (k2->op_flags & OPf_STACKED)
6070 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6071 expr = newUNOP(OP_DEFINED, 0, expr);
6072 break;
6073
6074 case OP_SASSIGN:
6075 if (k1 && (k1->op_type == OP_READDIR
6076 || k1->op_type == OP_GLOB
6077 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6078 || k1->op_type == OP_EACH
6079 || k1->op_type == OP_AEACH))
6080 expr = newUNOP(OP_DEFINED, 0, expr);
6081 break;
6082 }
6083 }
6084 }
6085
6086 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
6087 * op, in listop. This is wrong. [perl #27024] */
6088 if (!block)
6089 block = newOP(OP_NULL, 0);
6090 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
6091 o = new_logop(OP_AND, 0, &expr, &listop);
6092
6093 if (listop)
6094 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
6095
6096 if (once && o != listop)
6097 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
6098
6099 if (o == listop)
6100 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
6101
6102 o->op_flags |= flags;
6103 o = op_scope(o);
6104 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
6105 return o;
6106}
6107
6108/*
6109=for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my
6110
6111Constructs, checks, and returns an op tree expressing a C<while> loop.
6112This is a heavyweight loop, with structure that allows exiting the loop
6113by C<last> and suchlike.
6114
6115I<loop> is an optional preconstructed C<enterloop> op to use in the
6116loop; if it is null then a suitable op will be constructed automatically.
6117I<expr> supplies the loop's controlling expression. I<block> supplies the
6118main body of the loop, and I<cont> optionally supplies a C<continue> block
6119that operates as a second half of the body. All of these optree inputs
6120are consumed by this function and become part of the constructed op tree.
6121
6122I<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
6123op and, shifted up eight bits, the eight bits of C<op_private> for
6124the C<leaveloop> op, except that (in both cases) some bits will be set
6125automatically. I<debuggable> is currently unused and should always be 1.
6126I<has_my> can be supplied as true to force the
6127loop body to be enclosed in its own scope.
6128
6129=cut
6130*/
6131
6132OP *
6133Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
6134 OP *expr, OP *block, OP *cont, I32 has_my)
6135{
6136 dVAR;
6137 OP *redo;
6138 OP *next = NULL;
6139 OP *listop;
6140 OP *o;
6141 U8 loopflags = 0;
6142
6143 PERL_UNUSED_ARG(debuggable);
6144
6145 if (expr) {
6146 if (expr->op_type == OP_READLINE
6147 || expr->op_type == OP_READDIR
6148 || expr->op_type == OP_GLOB
6149 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
6150 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
6151 expr = newUNOP(OP_DEFINED, 0,
6152 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
6153 } else if (expr->op_flags & OPf_KIDS) {
6154 const OP * const k1 = ((UNOP*)expr)->op_first;
6155 const OP * const k2 = (k1) ? k1->op_sibling : NULL;
6156 switch (expr->op_type) {
6157 case OP_NULL:
6158 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
6159 && (k2->op_flags & OPf_STACKED)
6160 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
6161 expr = newUNOP(OP_DEFINED, 0, expr);
6162 break;
6163
6164 case OP_SASSIGN:
6165 if (k1 && (k1->op_type == OP_READDIR
6166 || k1->op_type == OP_GLOB
6167 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
6168 || k1->op_type == OP_EACH
6169 || k1->op_type == OP_AEACH))
6170 expr = newUNOP(OP_DEFINED, 0, expr);
6171 break;
6172 }
6173 }
6174 }
6175
6176 if (!block)
6177 block = newOP(OP_NULL, 0);
6178 else if (cont || has_my) {
6179 block = op_scope(block);
6180 }
6181
6182 if (cont) {
6183 next = LINKLIST(cont);
6184 }
6185 if (expr) {
6186 OP * const unstack = newOP(OP_UNSTACK, 0);
6187 if (!next)
6188 next = unstack;
6189 cont = op_append_elem(OP_LINESEQ, cont, unstack);
6190 }
6191
6192 assert(block);
6193 listop = op_append_list(OP_LINESEQ, block, cont);
6194 assert(listop);
6195 redo = LINKLIST(listop);
6196
6197 if (expr) {
6198 scalar(listop);
6199 o = new_logop(OP_AND, 0, &expr, &listop);
6200 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
6201 op_free((OP*)loop);
6202 return expr; /* listop already freed by new_logop */
6203 }
6204 if (listop)
6205 ((LISTOP*)listop)->op_last->op_next =
6206 (o == listop ? redo : LINKLIST(o));
6207 }
6208 else
6209 o = listop;
6210
6211 if (!loop) {
6212 NewOp(1101,loop,1,LOOP);
6213 loop->op_type = OP_ENTERLOOP;
6214 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
6215 loop->op_private = 0;
6216 loop->op_next = (OP*)loop;
6217 }
6218
6219 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
6220
6221 loop->op_redoop = redo;
6222 loop->op_lastop = o;
6223 o->op_private |= loopflags;
6224
6225 if (next)
6226 loop->op_nextop = next;
6227 else
6228 loop->op_nextop = o;
6229
6230 o->op_flags |= flags;
6231 o->op_private |= (flags >> 8);
6232 return o;
6233}
6234
6235/*
6236=for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont
6237
6238Constructs, checks, and returns an op tree expressing a C<foreach>
6239loop (iteration through a list of values). This is a heavyweight loop,
6240with structure that allows exiting the loop by C<last> and suchlike.
6241
6242I<sv> optionally supplies the variable that will be aliased to each
6243item in turn; if null, it defaults to C<$_> (either lexical or global).
6244I<expr> supplies the list of values to iterate over. I<block> supplies
6245the main body of the loop, and I<cont> optionally supplies a C<continue>
6246block that operates as a second half of the body. All of these optree
6247inputs are consumed by this function and become part of the constructed
6248op tree.
6249
6250I<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
6251op and, shifted up eight bits, the eight bits of C<op_private> for
6252the C<leaveloop> op, except that (in both cases) some bits will be set
6253automatically.
6254
6255=cut
6256*/
6257
6258OP *
6259Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
6260{
6261 dVAR;
6262 LOOP *loop;
6263 OP *wop;
6264 PADOFFSET padoff = 0;
6265 I32 iterflags = 0;
6266 I32 iterpflags = 0;
6267 OP *madsv = NULL;
6268
6269 PERL_ARGS_ASSERT_NEWFOROP;
6270
6271 if (sv) {
6272 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
6273 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
6274 sv->op_type = OP_RV2GV;
6275 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
6276
6277 /* The op_type check is needed to prevent a possible segfault
6278 * if the loop variable is undeclared and 'strict vars' is in
6279 * effect. This is illegal but is nonetheless parsed, so we
6280 * may reach this point with an OP_CONST where we're expecting
6281 * an OP_GV.
6282 */
6283 if (cUNOPx(sv)->op_first->op_type == OP_GV
6284 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
6285 iterpflags |= OPpITER_DEF;
6286 }
6287 else if (sv->op_type == OP_PADSV) { /* private variable */
6288 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
6289 padoff = sv->op_targ;
6290 if (PL_madskills)
6291 madsv = sv;
6292 else {
6293 sv->op_targ = 0;
6294 op_free(sv);
6295 }
6296 sv = NULL;
6297 }
6298 else
6299 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
6300 if (padoff) {
6301 SV *const namesv = PAD_COMPNAME_SV(padoff);
6302 STRLEN len;
6303 const char *const name = SvPV_const(namesv, len);
6304
6305 if (len == 2 && name[0] == '$' && name[1] == '_')
6306 iterpflags |= OPpITER_DEF;
6307 }
6308 }
6309 else {
6310 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
6311 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
6312 sv = newGVOP(OP_GV, 0, PL_defgv);
6313 }
6314 else {
6315 padoff = offset;
6316 }
6317 iterpflags |= OPpITER_DEF;
6318 }
6319 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
6320 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
6321 iterflags |= OPf_STACKED;
6322 }
6323 else if (expr->op_type == OP_NULL &&
6324 (expr->op_flags & OPf_KIDS) &&
6325 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
6326 {
6327 /* Basically turn for($x..$y) into the same as for($x,$y), but we
6328 * set the STACKED flag to indicate that these values are to be
6329 * treated as min/max values by 'pp_iterinit'.
6330 */
6331 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
6332 LOGOP* const range = (LOGOP*) flip->op_first;
6333 OP* const left = range->op_first;
6334 OP* const right = left->op_sibling;
6335 LISTOP* listop;
6336
6337 range->op_flags &= ~OPf_KIDS;
6338 range->op_first = NULL;
6339
6340 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
6341 listop->op_first->op_next = range->op_next;
6342 left->op_next = range->op_other;
6343 right->op_next = (OP*)listop;
6344 listop->op_next = listop->op_first;
6345
6346#ifdef PERL_MAD
6347 op_getmad(expr,(OP*)listop,'O');
6348#else
6349 op_free(expr);
6350#endif
6351 expr = (OP*)(listop);
6352 op_null(expr);
6353 iterflags |= OPf_STACKED;
6354 }
6355 else {
6356 expr = op_lvalue(force_list(expr), OP_GREPSTART);
6357 }
6358
6359 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
6360 op_append_elem(OP_LIST, expr, scalar(sv))));
6361 assert(!loop->op_next);
6362 /* for my $x () sets OPpLVAL_INTRO;
6363 * for our $x () sets OPpOUR_INTRO */
6364 loop->op_private = (U8)iterpflags;
6365 if (loop->op_slabbed
6366 && DIFF(loop, OpSLOT(loop)->opslot_next)
6367 < SIZE_TO_PSIZE(sizeof(LOOP)))
6368 {
6369 LOOP *tmp;
6370 NewOp(1234,tmp,1,LOOP);
6371 Copy(loop,tmp,1,LISTOP);
6372 S_op_destroy(aTHX_ (OP*)loop);
6373 loop = tmp;
6374 }
6375 else if (!loop->op_slabbed)
6376 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
6377 loop->op_targ = padoff;
6378 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0);
6379 if (madsv)
6380 op_getmad(madsv, (OP*)loop, 'v');
6381 return wop;
6382}
6383
6384/*
6385=for apidoc Am|OP *|newLOOPEX|I32 type|OP *label
6386
6387Constructs, checks, and returns a loop-exiting op (such as C<goto>
6388or C<last>). I<type> is the opcode. I<label> supplies the parameter
6389determining the target of the op; it is consumed by this function and
6390become part of the constructed op tree.
6391
6392=cut
6393*/
6394
6395OP*
6396Perl_newLOOPEX(pTHX_ I32 type, OP *label)
6397{
6398 dVAR;
6399 OP *o;
6400
6401 PERL_ARGS_ASSERT_NEWLOOPEX;
6402
6403 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
6404
6405 if (type != OP_GOTO) {
6406 /* "last()" means "last" */
6407 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
6408 o = newOP(type, OPf_SPECIAL);
6409 else {
6410 const_label:
6411 o = newPVOP(type,
6412 label->op_type == OP_CONST
6413 ? SvUTF8(((SVOP*)label)->op_sv)
6414 : 0,
6415 savesharedpv(label->op_type == OP_CONST
6416 ? SvPV_nolen_const(((SVOP*)label)->op_sv)
6417 : ""));
6418 }
6419#ifdef PERL_MAD
6420 op_getmad(label,o,'L');
6421#else
6422 op_free(label);
6423#endif
6424 }
6425 else {
6426 /* Check whether it's going to be a goto &function */
6427 if (label->op_type == OP_ENTERSUB
6428 && !(label->op_flags & OPf_STACKED))
6429 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
6430 else if (label->op_type == OP_CONST) {
6431 SV * const sv = ((SVOP *)label)->op_sv;
6432 STRLEN l;
6433 const char *s = SvPV_const(sv,l);
6434 if (l == strlen(s)) goto const_label;
6435 }
6436 o = newUNOP(type, OPf_STACKED, label);
6437 }
6438 PL_hints |= HINT_BLOCK_SCOPE;
6439 return o;
6440}
6441
6442/* if the condition is a literal array or hash
6443 (or @{ ... } etc), make a reference to it.
6444 */
6445STATIC OP *
6446S_ref_array_or_hash(pTHX_ OP *cond)
6447{
6448 if (cond
6449 && (cond->op_type == OP_RV2AV
6450 || cond->op_type == OP_PADAV
6451 || cond->op_type == OP_RV2HV
6452 || cond->op_type == OP_PADHV))
6453
6454 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
6455
6456 else if(cond
6457 && (cond->op_type == OP_ASLICE
6458 || cond->op_type == OP_HSLICE)) {
6459
6460 /* anonlist now needs a list from this op, was previously used in
6461 * scalar context */
6462 cond->op_flags |= ~(OPf_WANT_SCALAR | OPf_REF);
6463 cond->op_flags |= OPf_WANT_LIST;
6464
6465 return newANONLIST(op_lvalue(cond, OP_ANONLIST));
6466 }
6467
6468 else
6469 return cond;
6470}
6471
6472/* These construct the optree fragments representing given()
6473 and when() blocks.
6474
6475 entergiven and enterwhen are LOGOPs; the op_other pointer
6476 points up to the associated leave op. We need this so we
6477 can put it in the context and make break/continue work.
6478 (Also, of course, pp_enterwhen will jump straight to
6479 op_other if the match fails.)
6480 */
6481
6482STATIC OP *
6483S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
6484 I32 enter_opcode, I32 leave_opcode,
6485 PADOFFSET entertarg)
6486{
6487 dVAR;
6488 LOGOP *enterop;
6489 OP *o;
6490
6491 PERL_ARGS_ASSERT_NEWGIVWHENOP;
6492
6493 NewOp(1101, enterop, 1, LOGOP);
6494 enterop->op_type = (Optype)enter_opcode;
6495 enterop->op_ppaddr = PL_ppaddr[enter_opcode];
6496 enterop->op_flags = (U8) OPf_KIDS;
6497 enterop->op_targ = ((entertarg == NOT_IN_PAD) ? 0 : entertarg);
6498 enterop->op_private = 0;
6499
6500 o = newUNOP(leave_opcode, 0, (OP *) enterop);
6501
6502 if (cond) {
6503 enterop->op_first = scalar(cond);
6504 cond->op_sibling = block;
6505
6506 o->op_next = LINKLIST(cond);
6507 cond->op_next = (OP *) enterop;
6508 }
6509 else {
6510 /* This is a default {} block */
6511 enterop->op_first = block;
6512 enterop->op_flags |= OPf_SPECIAL;
6513 o ->op_flags |= OPf_SPECIAL;
6514
6515 o->op_next = (OP *) enterop;
6516 }
6517
6518 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
6519 entergiven and enterwhen both
6520 use ck_null() */
6521
6522 enterop->op_next = LINKLIST(block);
6523 block->op_next = enterop->op_other = o;
6524
6525 return o;
6526}
6527
6528/* Does this look like a boolean operation? For these purposes
6529 a boolean operation is:
6530 - a subroutine call [*]
6531 - a logical connective
6532 - a comparison operator
6533 - a filetest operator, with the exception of -s -M -A -C
6534 - defined(), exists() or eof()
6535 - /$re/ or $foo =~ /$re/
6536
6537 [*] possibly surprising
6538 */
6539STATIC bool
6540S_looks_like_bool(pTHX_ const OP *o)
6541{
6542 dVAR;
6543
6544 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
6545
6546 switch(o->op_type) {
6547 case OP_OR:
6548 case OP_DOR:
6549 return looks_like_bool(cLOGOPo->op_first);
6550
6551 case OP_AND:
6552 return (
6553 looks_like_bool(cLOGOPo->op_first)
6554 && looks_like_bool(cLOGOPo->op_first->op_sibling));
6555
6556 case OP_NULL:
6557 case OP_SCALAR:
6558 return (
6559 o->op_flags & OPf_KIDS
6560 && looks_like_bool(cUNOPo->op_first));
6561
6562 case OP_ENTERSUB:
6563
6564 case OP_NOT: case OP_XOR:
6565
6566 case OP_EQ: case OP_NE: case OP_LT:
6567 case OP_GT: case OP_LE: case OP_GE:
6568
6569 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
6570 case OP_I_GT: case OP_I_LE: case OP_I_GE:
6571
6572 case OP_SEQ: case OP_SNE: case OP_SLT:
6573 case OP_SGT: case OP_SLE: case OP_SGE:
6574
6575 case OP_SMARTMATCH:
6576
6577 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
6578 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
6579 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
6580 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
6581 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
6582 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
6583 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
6584 case OP_FTTEXT: case OP_FTBINARY:
6585
6586 case OP_DEFINED: case OP_EXISTS:
6587 case OP_MATCH: case OP_EOF:
6588
6589 case OP_FLOP:
6590
6591 return TRUE;
6592
6593 case OP_CONST:
6594 /* Detect comparisons that have been optimized away */
6595 if (cSVOPo->op_sv == &PL_sv_yes
6596 || cSVOPo->op_sv == &PL_sv_no)
6597
6598 return TRUE;
6599 else
6600 return FALSE;
6601
6602 /* FALL THROUGH */
6603 default:
6604 return FALSE;
6605 }
6606}
6607
6608/*
6609=for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off
6610
6611Constructs, checks, and returns an op tree expressing a C<given> block.
6612I<cond> supplies the expression that will be locally assigned to a lexical
6613variable, and I<block> supplies the body of the C<given> construct; they
6614are consumed by this function and become part of the constructed op tree.
6615I<defsv_off> is the pad offset of the scalar lexical variable that will
6616be affected.
6617
6618=cut
6619*/
6620
6621OP *
6622Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
6623{
6624 dVAR;
6625 PERL_ARGS_ASSERT_NEWGIVENOP;
6626 return newGIVWHENOP(
6627 ref_array_or_hash(cond),
6628 block,
6629 OP_ENTERGIVEN, OP_LEAVEGIVEN,
6630 defsv_off);
6631}
6632
6633/*
6634=for apidoc Am|OP *|newWHENOP|OP *cond|OP *block
6635
6636Constructs, checks, and returns an op tree expressing a C<when> block.
6637I<cond> supplies the test expression, and I<block> supplies the block
6638that will be executed if the test evaluates to true; they are consumed
6639by this function and become part of the constructed op tree. I<cond>
6640will be interpreted DWIMically, often as a comparison against C<$_>,
6641and may be null to generate a C<default> block.
6642
6643=cut
6644*/
6645
6646OP *
6647Perl_newWHENOP(pTHX_ OP *cond, OP *block)
6648{
6649 const bool cond_llb = (!cond || looks_like_bool(cond));
6650 OP *cond_op;
6651
6652 PERL_ARGS_ASSERT_NEWWHENOP;
6653
6654 if (cond_llb)
6655 cond_op = cond;
6656 else {
6657 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
6658 newDEFSVOP(),
6659 scalar(ref_array_or_hash(cond)));
6660 }
6661
6662 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
6663}
6664
6665void
6666Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
6667 const STRLEN len, const U32 flags)
6668{
6669 const char * const cvp = CvPROTO(cv);
6670 const STRLEN clen = CvPROTOLEN(cv);
6671
6672 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
6673
6674 if (((!p != !cvp) /* One has prototype, one has not. */
6675 || (p && (
6676 (flags & SVf_UTF8) == SvUTF8(cv)
6677 ? len != clen || memNE(cvp, p, len)
6678 : flags & SVf_UTF8
6679 ? bytes_cmp_utf8((const U8 *)cvp, clen,
6680 (const U8 *)p, len)
6681 : bytes_cmp_utf8((const U8 *)p, len,
6682 (const U8 *)cvp, clen)
6683 )
6684 )
6685 )
6686 && ckWARN_d(WARN_PROTOTYPE)) {
6687 SV* const msg = sv_newmortal();
6688 SV* name = NULL;
6689
6690 if (gv)
6691 gv_efullname3(name = sv_newmortal(), gv, NULL);
6692 sv_setpvs(msg, "Prototype mismatch:");
6693 if (name)
6694 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, SVfARG(name));
6695 if (SvPOK(cv))
6696 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")",
6697 SVfARG(newSVpvn_flags(cvp,clen, SvUTF8(cv)|SVs_TEMP))
6698 );
6699 else
6700 sv_catpvs(msg, ": none");
6701 sv_catpvs(msg, " vs ");
6702 if (p)
6703 Perl_sv_catpvf(aTHX_ msg, "(%"SVf")", SVfARG(newSVpvn_flags(p, len, flags | SVs_TEMP)));
6704 else
6705 sv_catpvs(msg, "none");
6706 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, SVfARG(msg));
6707 }
6708}
6709
6710static void const_sv_xsub(pTHX_ CV* cv);
6711
6712/*
6713
6714=head1 Optree Manipulation Functions
6715
6716=for apidoc cv_const_sv
6717
6718If C<cv> is a constant sub eligible for inlining. returns the constant
6719value returned by the sub. Otherwise, returns NULL.
6720
6721Constant subs can be created with C<newCONSTSUB> or as described in
6722L<perlsub/"Constant Functions">.
6723
6724=cut
6725*/
6726SV *
6727Perl_cv_const_sv(pTHX_ const CV *const cv)
6728{
6729 PERL_UNUSED_CONTEXT;
6730 if (!cv)
6731 return NULL;
6732 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
6733 return NULL;
6734 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
6735}
6736
6737/* op_const_sv: examine an optree to determine whether it's in-lineable.
6738 * Can be called in 3 ways:
6739 *
6740 * !cv
6741 * look for a single OP_CONST with attached value: return the value
6742 *
6743 * cv && CvCLONE(cv) && !CvCONST(cv)
6744 *
6745 * examine the clone prototype, and if contains only a single
6746 * OP_CONST referencing a pad const, or a single PADSV referencing
6747 * an outer lexical, return a non-zero value to indicate the CV is
6748 * a candidate for "constizing" at clone time
6749 *
6750 * cv && CvCONST(cv)
6751 *
6752 * We have just cloned an anon prototype that was marked as a const
6753 * candidate. Try to grab the current value, and in the case of
6754 * PADSV, ignore it if it has multiple references. Return the value.
6755 */
6756
6757SV *
6758Perl_op_const_sv(pTHX_ const OP *o, CV *cv)
6759{
6760 dVAR;
6761 SV *sv = NULL;
6762
6763 if (PL_madskills)
6764 return NULL;
6765
6766 if (!o)
6767 return NULL;
6768
6769 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
6770 o = cLISTOPo->op_first->op_sibling;
6771
6772 for (; o; o = o->op_next) {
6773 const OPCODE type = o->op_type;
6774
6775 if (sv && o->op_next == o)
6776 return sv;
6777 if (o->op_next != o) {
6778 if (type == OP_NEXTSTATE
6779 || (type == OP_NULL && !(o->op_flags & OPf_KIDS))
6780 || type == OP_PUSHMARK)
6781 continue;
6782 if (type == OP_DBSTATE)
6783 continue;
6784 }
6785 if (type == OP_LEAVESUB || type == OP_RETURN)
6786 break;
6787 if (sv)
6788 return NULL;
6789 if (type == OP_CONST && cSVOPo->op_sv)
6790 sv = cSVOPo->op_sv;
6791 else if (cv && type == OP_CONST) {
6792 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
6793 if (!sv)
6794 return NULL;
6795 }
6796 else if (cv && type == OP_PADSV) {
6797 if (CvCONST(cv)) { /* newly cloned anon */
6798 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
6799 /* the candidate should have 1 ref from this pad and 1 ref
6800 * from the parent */
6801 if (!sv || SvREFCNT(sv) != 2)
6802 return NULL;
6803 sv = newSVsv(sv);
6804 SvREADONLY_on(sv);
6805 return sv;
6806 }
6807 else {
6808 if (PAD_COMPNAME_FLAGS(o->op_targ) & SVf_FAKE)
6809 sv = &PL_sv_undef; /* an arbitrary non-null value */
6810 }
6811 }
6812 else {
6813 return NULL;
6814 }
6815 }
6816 return sv;
6817}
6818
6819#ifdef PERL_MAD
6820OP *
6821#else
6822void
6823#endif
6824Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
6825{
6826#if 0
6827 /* This would be the return value, but the return cannot be reached. */
6828 OP* pegop = newOP(OP_NULL, 0);
6829#endif
6830
6831 PERL_UNUSED_ARG(floor);
6832
6833 if (o)
6834 SAVEFREEOP(o);
6835 if (proto)
6836 SAVEFREEOP(proto);
6837 if (attrs)
6838 SAVEFREEOP(attrs);
6839 if (block)
6840 SAVEFREEOP(block);
6841 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
6842#ifdef PERL_MAD
6843 NORETURN_FUNCTION_END;
6844#endif
6845}
6846
6847CV *
6848Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
6849{
6850 return newATTRSUB_flags(floor, o, proto, attrs, block, 0);
6851}
6852
6853CV *
6854Perl_newATTRSUB_flags(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
6855 OP *block, U32 flags)
6856{
6857 dVAR;
6858 GV *gv;
6859 const char *ps;
6860 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
6861 U32 ps_utf8 = 0;
6862 register CV *cv = NULL;
6863 SV *const_sv;
6864 const bool ec = PL_parser && PL_parser->error_count;
6865 /* If the subroutine has no body, no attributes, and no builtin attributes
6866 then it's just a sub declaration, and we may be able to get away with
6867 storing with a placeholder scalar in the symbol table, rather than a
6868 full GV and CV. If anything is present then it will take a full CV to
6869 store it. */
6870 const I32 gv_fetch_flags
6871 = ec ? GV_NOADD_NOINIT :
6872 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
6873 || PL_madskills)
6874 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
6875 STRLEN namlen = 0;
6876 const bool o_is_gv = flags & 1;
6877 const char * const name =
6878 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
6879 bool has_name;
6880 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
6881#ifdef PERL_DEBUG_READONLY_OPS
6882 OPSLAB *slab = NULL;
6883#endif
6884
6885 if (proto) {
6886 assert(proto->op_type == OP_CONST);
6887 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len);
6888 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv);
6889 }
6890 else
6891 ps = NULL;
6892
6893 if (o_is_gv) {
6894 gv = (GV*)o;
6895 o = NULL;
6896 has_name = TRUE;
6897 } else if (name) {
6898 gv = gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV);
6899 has_name = TRUE;
6900 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
6901 SV * const sv = sv_newmortal();
6902 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
6903 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
6904 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
6905 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
6906 has_name = TRUE;
6907 } else if (PL_curstash) {
6908 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
6909 has_name = FALSE;
6910 } else {
6911 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
6912 has_name = FALSE;
6913 }
6914
6915 if (!PL_madskills) {
6916 if (o)
6917 SAVEFREEOP(o);
6918 if (proto)
6919 SAVEFREEOP(proto);
6920 if (attrs)
6921 SAVEFREEOP(attrs);
6922 }
6923
6924 if (ec) {
6925 op_free(block);
6926 if (name && block) {
6927 const char *s = strrchr(name, ':');
6928 s = s ? s+1 : name;
6929 if (strEQ(s, "BEGIN")) {
6930 const char not_safe[] =
6931 "BEGIN not safe after errors--compilation aborted";
6932 if (PL_in_eval & EVAL_KEEPERR)
6933 Perl_croak(aTHX_ not_safe);
6934 else {
6935 /* force display of errors found but not reported */
6936 sv_catpv(ERRSV, not_safe);
6937 Perl_croak(aTHX_ "%"SVf, SVfARG(ERRSV));
6938 }
6939 }
6940 }
6941 cv = PL_compcv;
6942 goto done;
6943 }
6944
6945 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
6946 maximum a prototype before. */
6947 if (SvTYPE(gv) > SVt_NULL) {
6948 cv_ckproto_len_flags((const CV *)gv, NULL, ps, ps_len, ps_utf8);
6949 }
6950 if (ps) {
6951 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
6952 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(gv));
6953 }
6954 else
6955 sv_setiv(MUTABLE_SV(gv), -1);
6956
6957 SvREFCNT_dec(PL_compcv);
6958 cv = PL_compcv = NULL;
6959 goto done;
6960 }
6961
6962 cv = (!name || GvCVGEN(gv)) ? NULL : GvCV(gv);
6963
6964 if (!block || !ps || *ps || attrs
6965 || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)
6966#ifdef PERL_MAD
6967 || block->op_type == OP_NULL
6968#endif
6969 )
6970 const_sv = NULL;
6971 else
6972 const_sv = op_const_sv(block, NULL);
6973
6974 if (cv) {
6975 const bool exists = CvROOT(cv) || CvXSUB(cv);
6976
6977 /* if the subroutine doesn't exist and wasn't pre-declared
6978 * with a prototype, assume it will be AUTOLOADed,
6979 * skipping the prototype check
6980 */
6981 if (exists || SvPOK(cv))
6982 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
6983 /* already defined (or promised)? */
6984 if (exists || GvASSUMECV(gv)) {
6985 if ((!block
6986#ifdef PERL_MAD
6987 || block->op_type == OP_NULL
6988#endif
6989 )) {
6990 if (CvFLAGS(PL_compcv)) {
6991 /* might have had built-in attrs applied */
6992 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
6993 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
6994 && ckWARN(WARN_MISC))
6995 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
6996 CvFLAGS(cv) |=
6997 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
6998 & ~(CVf_LVALUE * pureperl));
6999 }
7000 if (attrs) goto attrs;
7001 /* just a "sub foo;" when &foo is already defined */
7002 SAVEFREESV(PL_compcv);
7003 goto done;
7004 }
7005 if (block
7006#ifdef PERL_MAD
7007 && block->op_type != OP_NULL
7008#endif
7009 ) {
7010 const line_t oldline = CopLINE(PL_curcop);
7011 if (PL_parser && PL_parser->copline != NOLINE)
7012 CopLINE_set(PL_curcop, PL_parser->copline);
7013 report_redefined_cv(cSVOPo->op_sv, cv, &const_sv);
7014 CopLINE_set(PL_curcop, oldline);
7015#ifdef PERL_MAD
7016 if (!PL_minus_c) /* keep old one around for madskills */
7017#endif
7018 {
7019 /* (PL_madskills unset in used file.) */
7020 SvREFCNT_dec(cv);
7021 }
7022 cv = NULL;
7023 }
7024 }
7025 }
7026 if (const_sv) {
7027 SvREFCNT_inc_simple_void_NN(const_sv);
7028 if (cv) {
7029 assert(!CvROOT(cv) && !CvCONST(cv));
7030 cv_forget_slab(cv);
7031 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */
7032 CvXSUBANY(cv).any_ptr = const_sv;
7033 CvXSUB(cv) = const_sv_xsub;
7034 CvCONST_on(cv);
7035 CvISXSUB_on(cv);
7036 }
7037 else {
7038 GvCV_set(gv, NULL);
7039 cv = newCONSTSUB_flags(
7040 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
7041 const_sv
7042 );
7043 }
7044 if (PL_madskills)
7045 goto install_block;
7046 op_free(block);
7047 SvREFCNT_dec(PL_compcv);
7048 PL_compcv = NULL;
7049 goto done;
7050 }
7051 if (cv) { /* must reuse cv if autoloaded */
7052 /* transfer PL_compcv to cv */
7053 if (block
7054#ifdef PERL_MAD
7055 && block->op_type != OP_NULL
7056#endif
7057 ) {
7058 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
7059 AV *const temp_av = CvPADLIST(cv);
7060 CV *const temp_cv = CvOUTSIDE(cv);
7061 const cv_flags_t slabbed = CvSLABBED(cv);
7062 OP * const cvstart = CvSTART(cv);
7063
7064 assert(!CvWEAKOUTSIDE(cv));
7065 assert(!CvCVGV_RC(cv));
7066 assert(CvGV(cv) == gv);
7067
7068 SvPOK_off(cv);
7069 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs;
7070 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
7071 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
7072 CvPADLIST(cv) = CvPADLIST(PL_compcv);
7073 CvOUTSIDE(PL_compcv) = temp_cv;
7074 CvPADLIST(PL_compcv) = temp_av;
7075 CvSTART(cv) = CvSTART(PL_compcv);
7076 CvSTART(PL_compcv) = cvstart;
7077 if (slabbed) CvSLABBED_on(PL_compcv);
7078 else CvSLABBED_off(PL_compcv);
7079
7080 if (CvFILE(cv) && CvDYNFILE(cv)) {
7081 Safefree(CvFILE(cv));
7082 }
7083 CvFILE_set_from_cop(cv, PL_curcop);
7084 CvSTASH_set(cv, PL_curstash);
7085
7086 /* inner references to PL_compcv must be fixed up ... */
7087 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
7088 if (PERLDB_INTER)/* Advice debugger on the new sub. */
7089 ++PL_sub_generation;
7090 }
7091 else {
7092 /* Might have had built-in attributes applied -- propagate them. */
7093 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
7094 }
7095 /* ... before we throw it away */
7096 SvREFCNT_dec(PL_compcv);
7097 PL_compcv = cv;
7098 }
7099 else {
7100 cv = PL_compcv;
7101 if (name) {
7102 GvCV_set(gv, cv);
7103 if (PL_madskills) {
7104 if (strEQ(name, "import")) {
7105 PL_formfeed = MUTABLE_SV(cv);
7106 /* diag_listed_as: SKIPME */
7107 Perl_warner(aTHX_ packWARN(WARN_VOID), "0x%"UVxf"\n", PTR2UV(cv));
7108 }
7109 }
7110 GvCVGEN(gv) = 0;
7111 if (HvENAME_HEK(GvSTASH(gv)))
7112 /* sub Foo::bar { (shift)+1 } */
7113 mro_method_changed_in(GvSTASH(gv));
7114 }
7115 }
7116 if (!CvGV(cv)) {
7117 CvGV_set(cv, gv);
7118 CvFILE_set_from_cop(cv, PL_curcop);
7119 CvSTASH_set(cv, PL_curstash);
7120 }
7121
7122 if (ps) {
7123 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
7124 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
7125 }
7126
7127 install_block:
7128 if (!block)
7129 goto attrs;
7130
7131 /* If we assign an optree to a PVCV, then we've defined a subroutine that
7132 the debugger could be able to set a breakpoint in, so signal to
7133 pp_entereval that it should not throw away any saved lines at scope
7134 exit. */
7135
7136 PL_breakable_sub_gen++;
7137 /* This makes sub {}; work as expected. */
7138 if (block->op_type == OP_STUB) {
7139 OP* const newblock = newSTATEOP(0, NULL, 0);
7140#ifdef PERL_MAD
7141 op_getmad(block,newblock,'B');
7142#else
7143 op_free(block);
7144#endif
7145 block = newblock;
7146 }
7147 else block->op_attached = 1;
7148 CvROOT(cv) = CvLVALUE(cv)
7149 ? newUNOP(OP_LEAVESUBLV, 0,
7150 op_lvalue(scalarseq(block), OP_LEAVESUBLV))
7151 : newUNOP(OP_LEAVESUB, 0, scalarseq(block));
7152 CvROOT(cv)->op_private |= OPpREFCOUNTED;
7153 OpREFCNT_set(CvROOT(cv), 1);
7154 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
7155 itself has a refcount. */
7156 CvSLABBED_off(cv);
7157 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
7158#ifdef PERL_DEBUG_READONLY_OPS
7159 slab = (OPSLAB *)CvSTART(cv);
7160#endif
7161 CvSTART(cv) = LINKLIST(CvROOT(cv));
7162 CvROOT(cv)->op_next = 0;
7163 CALL_PEEP(CvSTART(cv));
7164 finalize_optree(CvROOT(cv));
7165
7166 /* now that optimizer has done its work, adjust pad values */
7167
7168 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
7169
7170 if (CvCLONE(cv)) {
7171 assert(!CvCONST(cv));
7172 if (ps && !*ps && op_const_sv(block, cv))
7173 CvCONST_on(cv);
7174 }
7175
7176 attrs:
7177 if (attrs) {
7178 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
7179 HV *stash = name && GvSTASH(CvGV(cv)) ? GvSTASH(CvGV(cv)) : PL_curstash;
7180 apply_attrs(stash, MUTABLE_SV(cv), attrs, FALSE);
7181 }
7182
7183 if (block && has_name) {
7184 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
7185 SV * const tmpstr = sv_newmortal();
7186 GV * const db_postponed = gv_fetchpvs("DB::postponed",
7187 GV_ADDMULTI, SVt_PVHV);
7188 HV *hv;
7189 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
7190 CopFILE(PL_curcop),
7191 (long)PL_subline,
7192 (long)CopLINE(PL_curcop));
7193 gv_efullname3(tmpstr, gv, NULL);
7194 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr),
7195 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0);
7196 hv = GvHVn(db_postponed);
7197 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) {
7198 CV * const pcv = GvCV(db_postponed);
7199 if (pcv) {
7200 dSP;
7201 PUSHMARK(SP);
7202 XPUSHs(tmpstr);
7203 PUTBACK;
7204 call_sv(MUTABLE_SV(pcv), G_DISCARD);
7205 }
7206 }
7207 }
7208
7209 if (name && ! (PL_parser && PL_parser->error_count))
7210 process_special_blocks(name, gv, cv);
7211 }
7212
7213 done:
7214 if (PL_parser)
7215 PL_parser->copline = NOLINE;
7216 LEAVE_SCOPE(floor);
7217#ifdef PERL_DEBUG_READONLY_OPS
7218 /* Watch out for BEGIN blocks */
7219 if (slab && gv && isGV(gv) && GvCV(gv)) Slab_to_ro(slab);
7220#endif
7221 return cv;
7222}
7223
7224STATIC void
7225S_process_special_blocks(pTHX_ const char *const fullname, GV *const gv,
7226 CV *const cv)
7227{
7228 const char *const colon = strrchr(fullname,':');
7229 const char *const name = colon ? colon + 1 : fullname;
7230
7231 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
7232
7233 if (*name == 'B') {
7234 if (strEQ(name, "BEGIN")) {
7235 const I32 oldscope = PL_scopestack_ix;
7236 ENTER;
7237 SAVECOPFILE(&PL_compiling);
7238 SAVECOPLINE(&PL_compiling);
7239 SAVEVPTR(PL_curcop);
7240
7241 DEBUG_x( dump_sub(gv) );
7242 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
7243 GvCV_set(gv,0); /* cv has been hijacked */
7244 call_list(oldscope, PL_beginav);
7245
7246 CopHINTS_set(&PL_compiling, PL_hints);
7247 LEAVE;
7248 }
7249 else
7250 return;
7251 } else {
7252 if (*name == 'E') {
7253 if strEQ(name, "END") {
7254 DEBUG_x( dump_sub(gv) );
7255 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
7256 } else
7257 return;
7258 } else if (*name == 'U') {
7259 if (strEQ(name, "UNITCHECK")) {
7260 /* It's never too late to run a unitcheck block */
7261 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
7262 }
7263 else
7264 return;
7265 } else if (*name == 'C') {
7266 if (strEQ(name, "CHECK")) {
7267 if (PL_main_start)
7268 /* diag_listed_as: Too late to run %s block */
7269 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
7270 "Too late to run CHECK block");
7271 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
7272 }
7273 else
7274 return;
7275 } else if (*name == 'I') {
7276 if (strEQ(name, "INIT")) {
7277 if (PL_main_start)
7278 /* diag_listed_as: Too late to run %s block */
7279 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
7280 "Too late to run INIT block");
7281 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
7282 }
7283 else
7284 return;
7285 } else
7286 return;
7287 DEBUG_x( dump_sub(gv) );
7288 GvCV_set(gv,0); /* cv has been hijacked */
7289 }
7290}
7291
7292/*
7293=for apidoc newCONSTSUB
7294
7295See L</newCONSTSUB_flags>.
7296
7297=cut
7298*/
7299
7300CV *
7301Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
7302{
7303 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
7304}
7305
7306/*
7307=for apidoc newCONSTSUB_flags
7308
7309Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
7310eligible for inlining at compile-time.
7311
7312Currently, the only useful value for C<flags> is SVf_UTF8.
7313
7314Passing NULL for SV creates a constant sub equivalent to C<sub BAR () {}>,
7315which won't be called if used as a destructor, but will suppress the overhead
7316of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at
7317compile time.)
7318
7319=cut
7320*/
7321
7322CV *
7323Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
7324 U32 flags, SV *sv)
7325{
7326 dVAR;
7327 CV* cv;
7328#ifdef USE_ITHREADS
7329 const char *const file = CopFILE(PL_curcop);
7330#else
7331 SV *const temp_sv = CopFILESV(PL_curcop);
7332 const char *const file = temp_sv ? SvPV_nolen_const(temp_sv) : NULL;
7333#endif
7334
7335 ENTER;
7336
7337 if (IN_PERL_RUNTIME) {
7338 /* at runtime, it's not safe to manipulate PL_curcop: it may be
7339 * an op shared between threads. Use a non-shared COP for our
7340 * dirty work */
7341 SAVEVPTR(PL_curcop);
7342 SAVECOMPILEWARNINGS();
7343 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
7344 PL_curcop = &PL_compiling;
7345 }
7346 SAVECOPLINE(PL_curcop);
7347 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
7348
7349 SAVEHINTS();
7350 PL_hints &= ~HINT_BLOCK_SCOPE;
7351
7352 if (stash) {
7353 SAVEGENERICSV(PL_curstash);
7354 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
7355 }
7356
7357 /* file becomes the CvFILE. For an XS, it's usually static storage,
7358 and so doesn't get free()d. (It's expected to be from the C pre-
7359 processor __FILE__ directive). But we need a dynamically allocated one,
7360 and we need it to get freed. */
7361 cv = newXS_len_flags(name, len, const_sv_xsub, file ? file : "", "",
7362 &sv, XS_DYNAMIC_FILENAME | flags);
7363 CvXSUBANY(cv).any_ptr = sv;
7364 CvCONST_on(cv);
7365
7366 LEAVE;
7367
7368 return cv;
7369}
7370
7371CV *
7372Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
7373 const char *const filename, const char *const proto,
7374 U32 flags)
7375{
7376 PERL_ARGS_ASSERT_NEWXS_FLAGS;
7377 return newXS_len_flags(
7378 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
7379 );
7380}
7381
7382CV *
7383Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
7384 XSUBADDR_t subaddr, const char *const filename,
7385 const char *const proto, SV **const_svp,
7386 U32 flags)
7387{
7388 CV *cv;
7389
7390 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
7391
7392 {
7393 GV * const gv = name
7394 ? gv_fetchpvn(
7395 name,len,GV_ADDMULTI|flags,SVt_PVCV
7396 )
7397 : gv_fetchpv(
7398 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
7399 GV_ADDMULTI | flags, SVt_PVCV);
7400
7401 if (!subaddr)
7402 Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
7403
7404 if ((cv = (name ? GvCV(gv) : NULL))) {
7405 if (GvCVGEN(gv)) {
7406 /* just a cached method */
7407 SvREFCNT_dec(cv);
7408 cv = NULL;
7409 }
7410 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
7411 /* already defined (or promised) */
7412 /* Redundant check that allows us to avoid creating an SV
7413 most of the time: */
7414 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
7415 const line_t oldline = CopLINE(PL_curcop);
7416 if (PL_parser && PL_parser->copline != NOLINE)
7417 CopLINE_set(PL_curcop, PL_parser->copline);
7418 report_redefined_cv(newSVpvn_flags(
7419 name,len,(flags&SVf_UTF8)|SVs_TEMP
7420 ),
7421 cv, const_svp);
7422 CopLINE_set(PL_curcop, oldline);
7423 }
7424 SvREFCNT_dec(cv);
7425 cv = NULL;
7426 }
7427 }
7428
7429 if (cv) /* must reuse cv if autoloaded */
7430 cv_undef(cv);
7431 else {
7432 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
7433 if (name) {
7434 GvCV_set(gv,cv);
7435 GvCVGEN(gv) = 0;
7436 if (HvENAME_HEK(GvSTASH(gv)))
7437 mro_method_changed_in(GvSTASH(gv)); /* newXS */
7438 }
7439 }
7440 if (!name)
7441 CvANON_on(cv);
7442 CvGV_set(cv, gv);
7443 (void)gv_fetchfile(filename);
7444 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
7445 an external constant string */
7446 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
7447 CvISXSUB_on(cv);
7448 CvXSUB(cv) = subaddr;
7449
7450 if (name)
7451 process_special_blocks(name, gv, cv);
7452 }
7453
7454 if (flags & XS_DYNAMIC_FILENAME) {
7455 CvFILE(cv) = savepv(filename);
7456 CvDYNFILE_on(cv);
7457 }
7458 sv_setpv(MUTABLE_SV(cv), proto);
7459 return cv;
7460}
7461
7462CV *
7463Perl_newSTUB(pTHX_ GV *gv, bool fake)
7464{
7465 register CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
7466 PERL_ARGS_ASSERT_NEWSTUB;
7467 assert(!GvCVu(gv));
7468 GvCV_set(gv, cv);
7469 GvCVGEN(gv) = 0;
7470 if (!fake && HvENAME_HEK(GvSTASH(gv)))
7471 mro_method_changed_in(GvSTASH(gv));
7472 CvGV_set(cv, gv);
7473 CvFILE_set_from_cop(cv, PL_curcop);
7474 CvSTASH_set(cv, PL_curstash);
7475 GvMULTI_on(gv);
7476 return cv;
7477}
7478
7479/*
7480=for apidoc U||newXS
7481
7482Used by C<xsubpp> to hook up XSUBs as Perl subs. I<filename> needs to be
7483static storage, as it is used directly as CvFILE(), without a copy being made.
7484
7485=cut
7486*/
7487
7488CV *
7489Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
7490{
7491 PERL_ARGS_ASSERT_NEWXS;
7492 return newXS_len_flags(
7493 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
7494 );
7495}
7496
7497#ifdef PERL_MAD
7498OP *
7499#else
7500void
7501#endif
7502Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
7503{
7504 dVAR;
7505 register CV *cv;
7506#ifdef PERL_MAD
7507 OP* pegop = newOP(OP_NULL, 0);
7508#endif
7509
7510 GV * const gv = o
7511 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
7512 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
7513
7514 GvMULTI_on(gv);
7515 if ((cv = GvFORM(gv))) {
7516 if (ckWARN(WARN_REDEFINE)) {
7517 const line_t oldline = CopLINE(PL_curcop);
7518 if (PL_parser && PL_parser->copline != NOLINE)
7519 CopLINE_set(PL_curcop, PL_parser->copline);
7520 if (o) {
7521 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
7522 "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv));
7523 } else {
7524 /* diag_listed_as: Format %s redefined */
7525 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
7526 "Format STDOUT redefined");
7527 }
7528 CopLINE_set(PL_curcop, oldline);
7529 }
7530 SvREFCNT_dec(cv);
7531 }
7532 cv = PL_compcv;
7533 GvFORM(gv) = cv;
7534 CvGV_set(cv, gv);
7535 CvFILE_set_from_cop(cv, PL_curcop);
7536
7537
7538 pad_tidy(padtidy_FORMAT);
7539 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
7540 CvROOT(cv)->op_private |= OPpREFCOUNTED;
7541 OpREFCNT_set(CvROOT(cv), 1);
7542 CvSTART(cv) = LINKLIST(CvROOT(cv));
7543 CvROOT(cv)->op_next = 0;
7544 CALL_PEEP(CvSTART(cv));
7545 finalize_optree(CvROOT(cv));
7546#ifdef PERL_MAD
7547 op_getmad(o,pegop,'n');
7548 op_getmad_weak(block, pegop, 'b');
7549#else
7550 op_free(o);
7551#endif
7552 cv_forget_slab(cv);
7553 if (PL_parser)
7554 PL_parser->copline = NOLINE;
7555 LEAVE_SCOPE(floor);
7556#ifdef PERL_MAD
7557 return pegop;
7558#endif
7559}
7560
7561OP *
7562Perl_newANONLIST(pTHX_ OP *o)
7563{
7564 return convert(OP_ANONLIST, OPf_SPECIAL, o);
7565}
7566
7567OP *
7568Perl_newANONHASH(pTHX_ OP *o)
7569{
7570 return convert(OP_ANONHASH, OPf_SPECIAL, o);
7571}
7572
7573OP *
7574Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
7575{
7576 return newANONATTRSUB(floor, proto, NULL, block);
7577}
7578
7579OP *
7580Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
7581{
7582 return newUNOP(OP_REFGEN, 0,
7583 newSVOP(OP_ANONCODE, 0,
7584 MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block))));
7585}
7586
7587OP *
7588Perl_oopsAV(pTHX_ OP *o)
7589{
7590 dVAR;
7591
7592 PERL_ARGS_ASSERT_OOPSAV;
7593
7594 switch (o->op_type) {
7595 case OP_PADSV:
7596 o->op_type = OP_PADAV;
7597 o->op_ppaddr = PL_ppaddr[OP_PADAV];
7598 return ref(o, OP_RV2AV);
7599
7600 case OP_RV2SV:
7601 o->op_type = OP_RV2AV;
7602 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
7603 ref(o, OP_RV2AV);
7604 break;
7605
7606 default:
7607 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
7608 break;
7609 }
7610 return o;
7611}
7612
7613OP *
7614Perl_oopsHV(pTHX_ OP *o)
7615{
7616 dVAR;
7617
7618 PERL_ARGS_ASSERT_OOPSHV;
7619
7620 switch (o->op_type) {
7621 case OP_PADSV:
7622 case OP_PADAV:
7623 o->op_type = OP_PADHV;
7624 o->op_ppaddr = PL_ppaddr[OP_PADHV];
7625 return ref(o, OP_RV2HV);
7626
7627 case OP_RV2SV:
7628 case OP_RV2AV:
7629 o->op_type = OP_RV2HV;
7630 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
7631 ref(o, OP_RV2HV);
7632 break;
7633
7634 default:
7635 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
7636 break;
7637 }
7638 return o;
7639}
7640
7641OP *
7642Perl_newAVREF(pTHX_ OP *o)
7643{
7644 dVAR;
7645
7646 PERL_ARGS_ASSERT_NEWAVREF;
7647
7648 if (o->op_type == OP_PADANY) {
7649 o->op_type = OP_PADAV;
7650 o->op_ppaddr = PL_ppaddr[OP_PADAV];
7651 return o;
7652 }
7653 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
7654 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7655 "Using an array as a reference is deprecated");
7656 }
7657 return newUNOP(OP_RV2AV, 0, scalar(o));
7658}
7659
7660OP *
7661Perl_newGVREF(pTHX_ I32 type, OP *o)
7662{
7663 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
7664 return newUNOP(OP_NULL, 0, o);
7665 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
7666}
7667
7668OP *
7669Perl_newHVREF(pTHX_ OP *o)
7670{
7671 dVAR;
7672
7673 PERL_ARGS_ASSERT_NEWHVREF;
7674
7675 if (o->op_type == OP_PADANY) {
7676 o->op_type = OP_PADHV;
7677 o->op_ppaddr = PL_ppaddr[OP_PADHV];
7678 return o;
7679 }
7680 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
7681 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
7682 "Using a hash as a reference is deprecated");
7683 }
7684 return newUNOP(OP_RV2HV, 0, scalar(o));
7685}
7686
7687OP *
7688Perl_newCVREF(pTHX_ I32 flags, OP *o)
7689{
7690 return newUNOP(OP_RV2CV, flags, scalar(o));
7691}
7692
7693OP *
7694Perl_newSVREF(pTHX_ OP *o)
7695{
7696 dVAR;
7697
7698 PERL_ARGS_ASSERT_NEWSVREF;
7699
7700 if (o->op_type == OP_PADANY) {
7701 o->op_type = OP_PADSV;
7702 o->op_ppaddr = PL_ppaddr[OP_PADSV];
7703 return o;
7704 }
7705 return newUNOP(OP_RV2SV, 0, scalar(o));
7706}
7707
7708/* Check routines. See the comments at the top of this file for details
7709 * on when these are called */
7710
7711OP *
7712Perl_ck_anoncode(pTHX_ OP *o)
7713{
7714 PERL_ARGS_ASSERT_CK_ANONCODE;
7715
7716 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
7717 if (!PL_madskills)
7718 cSVOPo->op_sv = NULL;
7719 return o;
7720}
7721
7722OP *
7723Perl_ck_bitop(pTHX_ OP *o)
7724{
7725 dVAR;
7726
7727 PERL_ARGS_ASSERT_CK_BITOP;
7728
7729 o->op_private = (U8)(PL_hints & HINT_INTEGER);
7730 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
7731 && (o->op_type == OP_BIT_OR
7732 || o->op_type == OP_BIT_AND
7733 || o->op_type == OP_BIT_XOR))
7734 {
7735 const OP * const left = cBINOPo->op_first;
7736 const OP * const right = left->op_sibling;
7737 if ((OP_IS_NUMCOMPARE(left->op_type) &&
7738 (left->op_flags & OPf_PARENS) == 0) ||
7739 (OP_IS_NUMCOMPARE(right->op_type) &&
7740 (right->op_flags & OPf_PARENS) == 0))
7741 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
7742 "Possible precedence problem on bitwise %c operator",
7743 o->op_type == OP_BIT_OR ? '|'
7744 : o->op_type == OP_BIT_AND ? '&' : '^'
7745 );
7746 }
7747 return o;
7748}
7749
7750PERL_STATIC_INLINE bool
7751is_dollar_bracket(pTHX_ const OP * const o)
7752{
7753 const OP *kid;
7754 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
7755 && (kid = cUNOPx(o)->op_first)
7756 && kid->op_type == OP_GV
7757 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
7758}
7759
7760OP *
7761Perl_ck_cmp(pTHX_ OP *o)
7762{
7763 PERL_ARGS_ASSERT_CK_CMP;
7764 if (ckWARN(WARN_SYNTAX)) {
7765 const OP *kid = cUNOPo->op_first;
7766 if (kid && (
7767 (
7768 is_dollar_bracket(aTHX_ kid)
7769 && kid->op_sibling && kid->op_sibling->op_type == OP_CONST
7770 )
7771 || ( kid->op_type == OP_CONST
7772 && (kid = kid->op_sibling) && is_dollar_bracket(aTHX_ kid))
7773 ))
7774 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
7775 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
7776 }
7777 return o;
7778}
7779
7780OP *
7781Perl_ck_concat(pTHX_ OP *o)
7782{
7783 const OP * const kid = cUNOPo->op_first;
7784
7785 PERL_ARGS_ASSERT_CK_CONCAT;
7786 PERL_UNUSED_CONTEXT;
7787
7788 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
7789 !(kUNOP->op_first->op_flags & OPf_MOD))
7790 o->op_flags |= OPf_STACKED;
7791 return o;
7792}
7793
7794OP *
7795Perl_ck_spair(pTHX_ OP *o)
7796{
7797 dVAR;
7798
7799 PERL_ARGS_ASSERT_CK_SPAIR;
7800
7801 if (o->op_flags & OPf_KIDS) {
7802 OP* newop;
7803 OP* kid;
7804 const OPCODE type = o->op_type;
7805 o = modkids(ck_fun(o), type);
7806 kid = cUNOPo->op_first;
7807 newop = kUNOP->op_first->op_sibling;
7808 if (newop) {
7809 const OPCODE type = newop->op_type;
7810 if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
7811 type == OP_PADAV || type == OP_PADHV ||
7812 type == OP_RV2AV || type == OP_RV2HV)
7813 return o;
7814 }
7815#ifdef PERL_MAD
7816 op_getmad(kUNOP->op_first,newop,'K');
7817#else
7818 op_free(kUNOP->op_first);
7819#endif
7820 kUNOP->op_first = newop;
7821 }
7822 o->op_ppaddr = PL_ppaddr[++o->op_type];
7823 return ck_fun(o);
7824}
7825
7826OP *
7827Perl_ck_delete(pTHX_ OP *o)
7828{
7829 PERL_ARGS_ASSERT_CK_DELETE;
7830
7831 o = ck_fun(o);
7832 o->op_private = 0;
7833 if (o->op_flags & OPf_KIDS) {
7834 OP * const kid = cUNOPo->op_first;
7835 switch (kid->op_type) {
7836 case OP_ASLICE:
7837 o->op_flags |= OPf_SPECIAL;
7838 /* FALL THROUGH */
7839 case OP_HSLICE:
7840 o->op_private |= OPpSLICE;
7841 break;
7842 case OP_AELEM:
7843 o->op_flags |= OPf_SPECIAL;
7844 /* FALL THROUGH */
7845 case OP_HELEM:
7846 break;
7847 default:
7848 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
7849 OP_DESC(o));
7850 }
7851 if (kid->op_private & OPpLVAL_INTRO)
7852 o->op_private |= OPpLVAL_INTRO;
7853 op_null(kid);
7854 }
7855 return o;
7856}
7857
7858OP *
7859Perl_ck_die(pTHX_ OP *o)
7860{
7861 PERL_ARGS_ASSERT_CK_DIE;
7862
7863#ifdef VMS
7864 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
7865#endif
7866 return ck_fun(o);
7867}
7868
7869OP *
7870Perl_ck_eof(pTHX_ OP *o)
7871{
7872 dVAR;
7873
7874 PERL_ARGS_ASSERT_CK_EOF;
7875
7876 if (o->op_flags & OPf_KIDS) {
7877 OP *kid;
7878 if (cLISTOPo->op_first->op_type == OP_STUB) {
7879 OP * const newop
7880 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
7881#ifdef PERL_MAD
7882 op_getmad(o,newop,'O');
7883#else
7884 op_free(o);
7885#endif
7886 o = newop;
7887 }
7888 o = ck_fun(o);
7889 kid = cLISTOPo->op_first;
7890 if (kid->op_type == OP_RV2GV)
7891 kid->op_private |= OPpALLOW_FAKE;
7892 }
7893 return o;
7894}
7895
7896OP *
7897Perl_ck_eval(pTHX_ OP *o)
7898{
7899 dVAR;
7900
7901 PERL_ARGS_ASSERT_CK_EVAL;
7902
7903 PL_hints |= HINT_BLOCK_SCOPE;
7904 if (o->op_flags & OPf_KIDS) {
7905 SVOP * const kid = (SVOP*)cUNOPo->op_first;
7906
7907 if (!kid) {
7908 o->op_flags &= ~OPf_KIDS;
7909 op_null(o);
7910 }
7911 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
7912 LOGOP *enter;
7913#ifdef PERL_MAD
7914 OP* const oldo = o;
7915#endif
7916
7917 cUNOPo->op_first = 0;
7918#ifndef PERL_MAD
7919 op_free(o);
7920#endif
7921
7922 NewOp(1101, enter, 1, LOGOP);
7923 enter->op_type = OP_ENTERTRY;
7924 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
7925 enter->op_private = 0;
7926
7927 /* establish postfix order */
7928 enter->op_next = (OP*)enter;
7929
7930 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
7931 o->op_type = OP_LEAVETRY;
7932 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
7933 enter->op_other = o;
7934 op_getmad(oldo,o,'O');
7935 return o;
7936 }
7937 else {
7938 scalar((OP*)kid);
7939 PL_cv_has_eval = 1;
7940 }
7941 }
7942 else {
7943 const U8 priv = o->op_private;
7944#ifdef PERL_MAD
7945 OP* const oldo = o;
7946#else
7947 op_free(o);
7948#endif
7949 o = newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
7950 op_getmad(oldo,o,'O');
7951 }
7952 o->op_targ = (PADOFFSET)PL_hints;
7953 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
7954 if ((PL_hints & HINT_LOCALIZE_HH) != 0
7955 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
7956 /* Store a copy of %^H that pp_entereval can pick up. */
7957 OP *hhop = newSVOP(OP_HINTSEVAL, 0,
7958 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv))));
7959 cUNOPo->op_first->op_sibling = hhop;
7960 o->op_private |= OPpEVAL_HAS_HH;
7961 }
7962 if (!(o->op_private & OPpEVAL_BYTES)
7963 && FEATURE_UNIEVAL_IS_ENABLED)
7964 o->op_private |= OPpEVAL_UNICODE;
7965 return o;
7966}
7967
7968OP *
7969Perl_ck_exit(pTHX_ OP *o)
7970{
7971 PERL_ARGS_ASSERT_CK_EXIT;
7972
7973#ifdef VMS
7974 HV * const table = GvHV(PL_hintgv);
7975 if (table) {
7976 SV * const * const svp = hv_fetchs(table, "vmsish_exit", FALSE);
7977 if (svp && *svp && SvTRUE(*svp))
7978 o->op_private |= OPpEXIT_VMSISH;
7979 }
7980 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
7981#endif
7982 return ck_fun(o);
7983}
7984
7985OP *
7986Perl_ck_exec(pTHX_ OP *o)
7987{
7988 PERL_ARGS_ASSERT_CK_EXEC;
7989
7990 if (o->op_flags & OPf_STACKED) {
7991 OP *kid;
7992 o = ck_fun(o);
7993 kid = cUNOPo->op_first->op_sibling;
7994 if (kid->op_type == OP_RV2GV)
7995 op_null(kid);
7996 }
7997 else
7998 o = listkids(o);
7999 return o;
8000}
8001
8002OP *
8003Perl_ck_exists(pTHX_ OP *o)
8004{
8005 dVAR;
8006
8007 PERL_ARGS_ASSERT_CK_EXISTS;
8008
8009 o = ck_fun(o);
8010 if (o->op_flags & OPf_KIDS) {
8011 OP * const kid = cUNOPo->op_first;
8012 if (kid->op_type == OP_ENTERSUB) {
8013 (void) ref(kid, o->op_type);
8014 if (kid->op_type != OP_RV2CV
8015 && !(PL_parser && PL_parser->error_count))
8016 Perl_croak(aTHX_ "%s argument is not a subroutine name",
8017 OP_DESC(o));
8018 o->op_private |= OPpEXISTS_SUB;
8019 }
8020 else if (kid->op_type == OP_AELEM)
8021 o->op_flags |= OPf_SPECIAL;
8022 else if (kid->op_type != OP_HELEM)
8023 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or a subroutine",
8024 OP_DESC(o));
8025 op_null(kid);
8026 }
8027 return o;
8028}
8029
8030OP *
8031Perl_ck_rvconst(pTHX_ register OP *o)
8032{
8033 dVAR;
8034 SVOP * const kid = (SVOP*)cUNOPo->op_first;
8035
8036 PERL_ARGS_ASSERT_CK_RVCONST;
8037
8038 o->op_private |= (PL_hints & HINT_STRICT_REFS);
8039 if (o->op_type == OP_RV2CV)
8040 o->op_private &= ~1;
8041
8042 if (kid->op_type == OP_CONST) {
8043 int iscv;
8044 GV *gv;
8045 SV * const kidsv = kid->op_sv;
8046
8047 /* Is it a constant from cv_const_sv()? */
8048 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
8049 SV * const rsv = SvRV(kidsv);
8050 const svtype type = SvTYPE(rsv);
8051 const char *badtype = NULL;
8052
8053 switch (o->op_type) {
8054 case OP_RV2SV:
8055 if (type > SVt_PVMG)
8056 badtype = "a SCALAR";
8057 break;
8058 case OP_RV2AV:
8059 if (type != SVt_PVAV)
8060 badtype = "an ARRAY";
8061 break;
8062 case OP_RV2HV:
8063 if (type != SVt_PVHV)
8064 badtype = "a HASH";
8065 break;
8066 case OP_RV2CV:
8067 if (type != SVt_PVCV)
8068 badtype = "a CODE";
8069 break;
8070 }
8071 if (badtype)
8072 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
8073 return o;
8074 }
8075 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
8076 const char *badthing;
8077 switch (o->op_type) {
8078 case OP_RV2SV:
8079 badthing = "a SCALAR";
8080 break;
8081 case OP_RV2AV:
8082 badthing = "an ARRAY";
8083 break;
8084 case OP_RV2HV:
8085 badthing = "a HASH";
8086 break;
8087 default:
8088 badthing = NULL;
8089 break;
8090 }
8091 if (badthing)
8092 Perl_croak(aTHX_
8093 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
8094 SVfARG(kidsv), badthing);
8095 }
8096 /*
8097 * This is a little tricky. We only want to add the symbol if we
8098 * didn't add it in the lexer. Otherwise we get duplicate strict
8099 * warnings. But if we didn't add it in the lexer, we must at
8100 * least pretend like we wanted to add it even if it existed before,
8101 * or we get possible typo warnings. OPpCONST_ENTERED says
8102 * whether the lexer already added THIS instance of this symbol.
8103 */
8104 iscv = (o->op_type == OP_RV2CV) * 2;
8105 do {
8106 gv = gv_fetchsv(kidsv,
8107 iscv | !(kid->op_private & OPpCONST_ENTERED),
8108 iscv
8109 ? SVt_PVCV
8110 : o->op_type == OP_RV2SV
8111 ? SVt_PV
8112 : o->op_type == OP_RV2AV
8113 ? SVt_PVAV
8114 : o->op_type == OP_RV2HV
8115 ? SVt_PVHV
8116 : SVt_PVGV);
8117 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
8118 if (gv) {
8119 kid->op_type = OP_GV;
8120 SvREFCNT_dec(kid->op_sv);
8121#ifdef USE_ITHREADS
8122 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
8123 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
8124 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
8125 GvIN_PAD_on(gv);
8126 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
8127#else
8128 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
8129#endif
8130 kid->op_private = 0;
8131 kid->op_ppaddr = PL_ppaddr[OP_GV];
8132 /* FAKE globs in the symbol table cause weird bugs (#77810) */
8133 SvFAKE_off(gv);
8134 }
8135 }
8136 return o;
8137}
8138
8139OP *
8140Perl_ck_ftst(pTHX_ OP *o)
8141{
8142 dVAR;
8143 const I32 type = o->op_type;
8144
8145 PERL_ARGS_ASSERT_CK_FTST;
8146
8147 if (o->op_flags & OPf_REF) {
8148 NOOP;
8149 }
8150 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
8151 SVOP * const kid = (SVOP*)cUNOPo->op_first;
8152 const OPCODE kidtype = kid->op_type;
8153
8154 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
8155 OP * const newop = newGVOP(type, OPf_REF,
8156 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
8157#ifdef PERL_MAD
8158 op_getmad(o,newop,'O');
8159#else
8160 op_free(o);
8161#endif
8162 return newop;
8163 }
8164 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
8165 o->op_private |= OPpFT_ACCESS;
8166 if (PL_check[kidtype] == Perl_ck_ftst
8167 && kidtype != OP_STAT && kidtype != OP_LSTAT) {
8168 o->op_private |= OPpFT_STACKED;
8169 kid->op_private |= OPpFT_STACKING;
8170 if (kidtype == OP_FTTTY && (
8171 !(kid->op_private & OPpFT_STACKED)
8172 || kid->op_private & OPpFT_AFTER_t
8173 ))
8174 o->op_private |= OPpFT_AFTER_t;
8175 }
8176 }
8177 else {
8178#ifdef PERL_MAD
8179 OP* const oldo = o;
8180#else
8181 op_free(o);
8182#endif
8183 if (type == OP_FTTTY)
8184 o = newGVOP(type, OPf_REF, PL_stdingv);
8185 else
8186 o = newUNOP(type, 0, newDEFSVOP());
8187 op_getmad(oldo,o,'O');
8188 }
8189 return o;
8190}
8191
8192OP *
8193Perl_ck_fun(pTHX_ OP *o)
8194{
8195 dVAR;
8196 const int type = o->op_type;
8197 register I32 oa = PL_opargs[type] >> OASHIFT;
8198
8199 PERL_ARGS_ASSERT_CK_FUN;
8200
8201 if (o->op_flags & OPf_STACKED) {
8202 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
8203 oa &= ~OA_OPTIONAL;
8204 else
8205 return no_fh_allowed(o);
8206 }
8207
8208 if (o->op_flags & OPf_KIDS) {
8209 OP **tokid = &cLISTOPo->op_first;
8210 register OP *kid = cLISTOPo->op_first;
8211 OP *sibl;
8212 I32 numargs = 0;
8213 bool seen_optional = FALSE;
8214
8215 if (kid->op_type == OP_PUSHMARK ||
8216 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
8217 {
8218 tokid = &kid->op_sibling;
8219 kid = kid->op_sibling;
8220 }
8221 if (kid && kid->op_type == OP_COREARGS) {
8222 bool optional = FALSE;
8223 while (oa) {
8224 numargs++;
8225 if (oa & OA_OPTIONAL) optional = TRUE;
8226 oa = oa >> 4;
8227 }
8228 if (optional) o->op_private |= numargs;
8229 return o;
8230 }
8231
8232 while (oa) {
8233 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
8234 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV)
8235 *tokid = kid = newDEFSVOP();
8236 seen_optional = TRUE;
8237 }
8238 if (!kid) break;
8239
8240 numargs++;
8241 sibl = kid->op_sibling;
8242#ifdef PERL_MAD
8243 if (!sibl && kid->op_type == OP_STUB) {
8244 numargs--;
8245 break;
8246 }
8247#endif
8248 switch (oa & 7) {
8249 case OA_SCALAR:
8250 /* list seen where single (scalar) arg expected? */
8251 if (numargs == 1 && !(oa >> 4)
8252 && kid->op_type == OP_LIST && type != OP_SCALAR)
8253 {
8254 return too_many_arguments_pv(o,PL_op_desc[type], 0);
8255 }
8256 scalar(kid);
8257 break;
8258 case OA_LIST:
8259 if (oa < 16) {
8260 kid = 0;
8261 continue;
8262 }
8263 else
8264 list(kid);
8265 break;
8266 case OA_AVREF:
8267 if ((type == OP_PUSH || type == OP_UNSHIFT)
8268 && !kid->op_sibling)
8269 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
8270 "Useless use of %s with no values",
8271 PL_op_desc[type]);
8272
8273 if (kid->op_type == OP_CONST &&
8274 (kid->op_private & OPpCONST_BARE))
8275 {
8276 OP * const newop = newAVREF(newGVOP(OP_GV, 0,
8277 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
8278 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8279 "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
8280 SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
8281#ifdef PERL_MAD
8282 op_getmad(kid,newop,'K');
8283#else
8284 op_free(kid);
8285#endif
8286 kid = newop;
8287 kid->op_sibling = sibl;
8288 *tokid = kid;
8289 }
8290 else if (kid->op_type == OP_CONST
8291 && ( !SvROK(cSVOPx_sv(kid))
8292 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
8293 )
8294 bad_type_pv(numargs, "array", PL_op_desc[type], 0, kid);
8295 /* Defer checks to run-time if we have a scalar arg */
8296 if (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV)
8297 op_lvalue(kid, type);
8298 else scalar(kid);
8299 break;
8300 case OA_HVREF:
8301 if (kid->op_type == OP_CONST &&
8302 (kid->op_private & OPpCONST_BARE))
8303 {
8304 OP * const newop = newHVREF(newGVOP(OP_GV, 0,
8305 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
8306 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8307 "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
8308 SVfARG(((SVOP*)kid)->op_sv), (IV)numargs, PL_op_desc[type]);
8309#ifdef PERL_MAD
8310 op_getmad(kid,newop,'K');
8311#else
8312 op_free(kid);
8313#endif
8314 kid = newop;
8315 kid->op_sibling = sibl;
8316 *tokid = kid;
8317 }
8318 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
8319 bad_type_pv(numargs, "hash", PL_op_desc[type], 0, kid);
8320 op_lvalue(kid, type);
8321 break;
8322 case OA_CVREF:
8323 {
8324 OP * const newop = newUNOP(OP_NULL, 0, kid);
8325 kid->op_sibling = 0;
8326 LINKLIST(kid);
8327 newop->op_next = newop;
8328 kid = newop;
8329 kid->op_sibling = sibl;
8330 *tokid = kid;
8331 }
8332 break;
8333 case OA_FILEREF:
8334 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
8335 if (kid->op_type == OP_CONST &&
8336 (kid->op_private & OPpCONST_BARE))
8337 {
8338 OP * const newop = newGVOP(OP_GV, 0,
8339 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
8340 if (!(o->op_private & 1) && /* if not unop */
8341 kid == cLISTOPo->op_last)
8342 cLISTOPo->op_last = newop;
8343#ifdef PERL_MAD
8344 op_getmad(kid,newop,'K');
8345#else
8346 op_free(kid);
8347#endif
8348 kid = newop;
8349 }
8350 else if (kid->op_type == OP_READLINE) {
8351 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
8352 bad_type_pv(numargs, "HANDLE", OP_DESC(o), 0, kid);
8353 }
8354 else {
8355 I32 flags = OPf_SPECIAL;
8356 I32 priv = 0;
8357 PADOFFSET targ = 0;
8358
8359 /* is this op a FH constructor? */
8360 if (is_handle_constructor(o,numargs)) {
8361 const char *name = NULL;
8362 STRLEN len = 0;
8363 U32 name_utf8 = 0;
8364 bool want_dollar = TRUE;
8365
8366 flags = 0;
8367 /* Set a flag to tell rv2gv to vivify
8368 * need to "prove" flag does not mean something
8369 * else already - NI-S 1999/05/07
8370 */
8371 priv = OPpDEREF;
8372 if (kid->op_type == OP_PADSV) {
8373 SV *const namesv
8374 = PAD_COMPNAME_SV(kid->op_targ);
8375 name = SvPV_const(namesv, len);
8376 name_utf8 = SvUTF8(namesv);
8377 }
8378 else if (kid->op_type == OP_RV2SV
8379 && kUNOP->op_first->op_type == OP_GV)
8380 {
8381 GV * const gv = cGVOPx_gv(kUNOP->op_first);
8382 name = GvNAME(gv);
8383 len = GvNAMELEN(gv);
8384 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
8385 }
8386 else if (kid->op_type == OP_AELEM
8387 || kid->op_type == OP_HELEM)
8388 {
8389 OP *firstop;
8390 OP *op = ((BINOP*)kid)->op_first;
8391 name = NULL;
8392 if (op) {
8393 SV *tmpstr = NULL;
8394 const char * const a =
8395 kid->op_type == OP_AELEM ?
8396 "[]" : "{}";
8397 if (((op->op_type == OP_RV2AV) ||
8398 (op->op_type == OP_RV2HV)) &&
8399 (firstop = ((UNOP*)op)->op_first) &&
8400 (firstop->op_type == OP_GV)) {
8401 /* packagevar $a[] or $h{} */
8402 GV * const gv = cGVOPx_gv(firstop);
8403 if (gv)
8404 tmpstr =
8405 Perl_newSVpvf(aTHX_
8406 "%s%c...%c",
8407 GvNAME(gv),
8408 a[0], a[1]);
8409 }
8410 else if (op->op_type == OP_PADAV
8411 || op->op_type == OP_PADHV) {
8412 /* lexicalvar $a[] or $h{} */
8413 const char * const padname =
8414 PAD_COMPNAME_PV(op->op_targ);
8415 if (padname)
8416 tmpstr =
8417 Perl_newSVpvf(aTHX_
8418 "%s%c...%c",
8419 padname + 1,
8420 a[0], a[1]);
8421 }
8422 if (tmpstr) {
8423 name = SvPV_const(tmpstr, len);
8424 name_utf8 = SvUTF8(tmpstr);
8425 sv_2mortal(tmpstr);
8426 }
8427 }
8428 if (!name) {
8429 name = "__ANONIO__";
8430 len = 10;
8431 want_dollar = FALSE;
8432 }
8433 op_lvalue(kid, type);
8434 }
8435 if (name) {
8436 SV *namesv;
8437 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
8438 namesv = PAD_SVl(targ);
8439 SvUPGRADE(namesv, SVt_PV);
8440 if (want_dollar && *name != '$')
8441 sv_setpvs(namesv, "$");
8442 sv_catpvn(namesv, name, len);
8443 if ( name_utf8 ) SvUTF8_on(namesv);
8444 }
8445 }
8446 kid->op_sibling = 0;
8447 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
8448 kid->op_targ = targ;
8449 kid->op_private |= priv;
8450 }
8451 kid->op_sibling = sibl;
8452 *tokid = kid;
8453 }
8454 scalar(kid);
8455 break;
8456 case OA_SCALARREF:
8457 if ((type == OP_UNDEF || type == OP_POS)
8458 && numargs == 1 && !(oa >> 4)
8459 && kid->op_type == OP_LIST)
8460 return too_many_arguments_pv(o,PL_op_desc[type], 0);
8461 op_lvalue(scalar(kid), type);
8462 break;
8463 }
8464 oa >>= 4;
8465 tokid = &kid->op_sibling;
8466 kid = kid->op_sibling;
8467 }
8468#ifdef PERL_MAD
8469 if (kid && kid->op_type != OP_STUB)
8470 return too_many_arguments_pv(o,OP_DESC(o), 0);
8471 o->op_private |= numargs;
8472#else
8473 /* FIXME - should the numargs move as for the PERL_MAD case? */
8474 o->op_private |= numargs;
8475 if (kid)
8476 return too_many_arguments_pv(o,OP_DESC(o), 0);
8477#endif
8478 listkids(o);
8479 }
8480 else if (PL_opargs[type] & OA_DEFGV) {
8481#ifdef PERL_MAD
8482 OP *newop = newUNOP(type, 0, newDEFSVOP());
8483 op_getmad(o,newop,'O');
8484 return newop;
8485#else
8486 /* Ordering of these two is important to keep f_map.t passing. */
8487 op_free(o);
8488 return newUNOP(type, 0, newDEFSVOP());
8489#endif
8490 }
8491
8492 if (oa) {
8493 while (oa & OA_OPTIONAL)
8494 oa >>= 4;
8495 if (oa && oa != OA_LIST)
8496 return too_few_arguments_pv(o,OP_DESC(o), 0);
8497 }
8498 return o;
8499}
8500
8501OP *
8502Perl_ck_glob(pTHX_ OP *o)
8503{
8504 dVAR;
8505 GV *gv;
8506 const bool core = o->op_flags & OPf_SPECIAL;
8507
8508 PERL_ARGS_ASSERT_CK_GLOB;
8509
8510 o = ck_fun(o);
8511 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
8512 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
8513
8514 if (core) gv = NULL;
8515 else if (!((gv = gv_fetchpvs("glob", GV_NOTQUAL, SVt_PVCV))
8516 && GvCVu(gv) && GvIMPORTED_CV(gv)))
8517 {
8518 GV * const * const gvp =
8519 (GV **)hv_fetchs(PL_globalstash, "glob", FALSE);
8520 gv = gvp ? *gvp : NULL;
8521 }
8522
8523 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
8524 /* convert
8525 * glob
8526 * \ null - const(wildcard)
8527 * into
8528 * null
8529 * \ enter
8530 * \ list
8531 * \ mark - glob - rv2cv
8532 * | \ gv(CORE::GLOBAL::glob)
8533 * |
8534 * \ null - const(wildcard) - const(ix)
8535 */
8536 o->op_flags |= OPf_SPECIAL;
8537 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
8538 op_append_elem(OP_GLOB, o,
8539 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
8540 o = newLISTOP(OP_LIST, 0, o, NULL);
8541 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
8542 op_append_elem(OP_LIST, o,
8543 scalar(newUNOP(OP_RV2CV, 0,
8544 newGVOP(OP_GV, 0, gv)))));
8545 o = newUNOP(OP_NULL, 0, o);
8546 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
8547 return o;
8548 }
8549 else o->op_flags &= ~OPf_SPECIAL;
8550#if !defined(PERL_EXTERNAL_GLOB)
8551 if (!PL_globhook) {
8552 ENTER;
8553 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
8554 newSVpvs("File::Glob"), NULL, NULL, NULL);
8555 LEAVE;
8556 }
8557#endif /* !PERL_EXTERNAL_GLOB */
8558 gv = newGVgen("main");
8559 gv_IOadd(gv);
8560#ifndef PERL_EXTERNAL_GLOB
8561 sv_setiv(GvSVn(gv),PL_glob_index++);
8562#endif
8563 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
8564 scalarkids(o);
8565 return o;
8566}
8567
8568OP *
8569Perl_ck_grep(pTHX_ OP *o)
8570{
8571 dVAR;
8572 LOGOP *gwop = NULL;
8573 OP *kid;
8574 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
8575 PADOFFSET offset;
8576
8577 PERL_ARGS_ASSERT_CK_GREP;
8578
8579 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
8580 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
8581
8582 if (o->op_flags & OPf_STACKED) {
8583 OP* k;
8584 OP *firstkid = cLISTOPo->op_first->op_sibling;
8585 kid = cUNOPx(firstkid)->op_first;
8586 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
8587 return no_fh_allowed(o);
8588 if (o->op_flags & OPf_STACKED) {
8589 LINKLIST(kid);
8590 firstkid->op_next = kLISTOP->op_first;
8591 kid->op_next = 0; /* just disconnect the leave/scope */
8592 o->op_flags |= OPf_SPECIAL;
8593 }
8594 for (k = kid; k; k = k->op_next) {
8595 kid = k;
8596 }
8597 NewOp(1101, gwop, 1, LOGOP);
8598 kid->op_next = (OP*)gwop;
8599 o->op_flags &= ~OPf_STACKED;
8600 }
8601 kid = cLISTOPo->op_first->op_sibling;
8602 if (type == OP_MAPWHILE)
8603 list(kid);
8604 else
8605 scalar(kid);
8606 o = ck_fun(o);
8607 if (PL_parser && PL_parser->error_count)
8608 return o;
8609 kid = cLISTOPo->op_first->op_sibling;
8610 if (kid->op_type != OP_NULL)
8611 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
8612 kid = kUNOP->op_first;
8613
8614 if (!gwop)
8615 NewOp(1101, gwop, 1, LOGOP);
8616 gwop->op_type = type;
8617 gwop->op_ppaddr = PL_ppaddr[type];
8618 gwop->op_first = listkids(o);
8619 gwop->op_flags |= OPf_KIDS;
8620 gwop->op_other = LINKLIST(kid);
8621 kid->op_next = (OP*)gwop;
8622 offset = pad_findmy_pvs("$_", 0);
8623 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
8624 o->op_private = gwop->op_private = 0;
8625 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
8626 }
8627 else {
8628 o->op_private = gwop->op_private = OPpGREP_LEX;
8629 gwop->op_targ = o->op_targ = offset;
8630 }
8631
8632 kid = cLISTOPo->op_first->op_sibling;
8633 if (!kid || !kid->op_sibling)
8634 return too_few_arguments_pv(o,OP_DESC(o), 0);
8635 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
8636 op_lvalue(kid, OP_GREPSTART);
8637
8638 return (OP*)gwop;
8639}
8640
8641OP *
8642Perl_ck_index(pTHX_ OP *o)
8643{
8644 PERL_ARGS_ASSERT_CK_INDEX;
8645
8646 if (o->op_flags & OPf_KIDS) {
8647 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
8648 if (kid)
8649 kid = kid->op_sibling; /* get past "big" */
8650 if (kid && kid->op_type == OP_CONST) {
8651 const bool save_taint = PL_tainted;
8652 fbm_compile(((SVOP*)kid)->op_sv, 0);
8653 PL_tainted = save_taint;
8654 }
8655 }
8656 return ck_fun(o);
8657}
8658
8659OP *
8660Perl_ck_lfun(pTHX_ OP *o)
8661{
8662 const OPCODE type = o->op_type;
8663
8664 PERL_ARGS_ASSERT_CK_LFUN;
8665
8666 return modkids(ck_fun(o), type);
8667}
8668
8669OP *
8670Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
8671{
8672 PERL_ARGS_ASSERT_CK_DEFINED;
8673
8674 if ((o->op_flags & OPf_KIDS)) {
8675 switch (cUNOPo->op_first->op_type) {
8676 case OP_RV2AV:
8677 case OP_PADAV:
8678 case OP_AASSIGN: /* Is this a good idea? */
8679 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8680 "defined(@array) is deprecated");
8681 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8682 "\t(Maybe you should just omit the defined()?)\n");
8683 break;
8684 case OP_RV2HV:
8685 case OP_PADHV:
8686 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8687 "defined(%%hash) is deprecated");
8688 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
8689 "\t(Maybe you should just omit the defined()?)\n");
8690 break;
8691 default:
8692 /* no warning */
8693 break;
8694 }
8695 }
8696 return ck_rfun(o);
8697}
8698
8699OP *
8700Perl_ck_readline(pTHX_ OP *o)
8701{
8702 PERL_ARGS_ASSERT_CK_READLINE;
8703
8704 if (o->op_flags & OPf_KIDS) {
8705 OP *kid = cLISTOPo->op_first;
8706 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
8707 }
8708 else {
8709 OP * const newop
8710 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
8711#ifdef PERL_MAD
8712 op_getmad(o,newop,'O');
8713#else
8714 op_free(o);
8715#endif
8716 return newop;
8717 }
8718 return o;
8719}
8720
8721OP *
8722Perl_ck_rfun(pTHX_ OP *o)
8723{
8724 const OPCODE type = o->op_type;
8725
8726 PERL_ARGS_ASSERT_CK_RFUN;
8727
8728 return refkids(ck_fun(o), type);
8729}
8730
8731OP *
8732Perl_ck_listiob(pTHX_ OP *o)
8733{
8734 register OP *kid;
8735
8736 PERL_ARGS_ASSERT_CK_LISTIOB;
8737
8738 kid = cLISTOPo->op_first;
8739 if (!kid) {
8740 o = force_list(o);
8741 kid = cLISTOPo->op_first;
8742 }
8743 if (kid->op_type == OP_PUSHMARK)
8744 kid = kid->op_sibling;
8745 if (kid && o->op_flags & OPf_STACKED)
8746 kid = kid->op_sibling;
8747 else if (kid && !kid->op_sibling) { /* print HANDLE; */
8748 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
8749 && !(kid->op_private & OPpCONST_FOLDED)) {
8750 o->op_flags |= OPf_STACKED; /* make it a filehandle */
8751 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
8752 cLISTOPo->op_first->op_sibling = kid;
8753 cLISTOPo->op_last = kid;
8754 kid = kid->op_sibling;
8755 }
8756 }
8757
8758 if (!kid)
8759 op_append_elem(o->op_type, o, newDEFSVOP());
8760
8761 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
8762 return listkids(o);
8763}
8764
8765OP *
8766Perl_ck_smartmatch(pTHX_ OP *o)
8767{
8768 dVAR;
8769 PERL_ARGS_ASSERT_CK_SMARTMATCH;
8770 if (0 == (o->op_flags & OPf_SPECIAL)) {
8771 OP *first = cBINOPo->op_first;
8772 OP *second = first->op_sibling;
8773
8774 /* Implicitly take a reference to an array or hash */
8775 first->op_sibling = NULL;
8776 first = cBINOPo->op_first = ref_array_or_hash(first);
8777 second = first->op_sibling = ref_array_or_hash(second);
8778
8779 /* Implicitly take a reference to a regular expression */
8780 if (first->op_type == OP_MATCH) {
8781 first->op_type = OP_QR;
8782 first->op_ppaddr = PL_ppaddr[OP_QR];
8783 }
8784 if (second->op_type == OP_MATCH) {
8785 second->op_type = OP_QR;
8786 second->op_ppaddr = PL_ppaddr[OP_QR];
8787 }
8788 }
8789
8790 return o;
8791}
8792
8793
8794OP *
8795Perl_ck_sassign(pTHX_ OP *o)
8796{
8797 dVAR;
8798 OP * const kid = cLISTOPo->op_first;
8799
8800 PERL_ARGS_ASSERT_CK_SASSIGN;
8801
8802 /* has a disposable target? */
8803 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
8804 && !(kid->op_flags & OPf_STACKED)
8805 /* Cannot steal the second time! */
8806 && !(kid->op_private & OPpTARGET_MY)
8807 /* Keep the full thing for madskills */
8808 && !PL_madskills
8809 )
8810 {
8811 OP * const kkid = kid->op_sibling;
8812
8813 /* Can just relocate the target. */
8814 if (kkid && kkid->op_type == OP_PADSV
8815 && !(kkid->op_private & OPpLVAL_INTRO))
8816 {
8817 kid->op_targ = kkid->op_targ;
8818 kkid->op_targ = 0;
8819 /* Now we do not need PADSV and SASSIGN. */
8820 kid->op_sibling = o->op_sibling; /* NULL */
8821 cLISTOPo->op_first = NULL;
8822 op_free(o);
8823 op_free(kkid);
8824 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
8825 return kid;
8826 }
8827 }
8828 if (kid->op_sibling) {
8829 OP *kkid = kid->op_sibling;
8830 /* For state variable assignment, kkid is a list op whose op_last
8831 is a padsv. */
8832 if ((kkid->op_type == OP_PADSV ||
8833 (kkid->op_type == OP_LIST &&
8834 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
8835 )
8836 )
8837 && (kkid->op_private & OPpLVAL_INTRO)
8838 && SvPAD_STATE(*av_fetch(PL_comppad_name, kkid->op_targ, FALSE))) {
8839 const PADOFFSET target = kkid->op_targ;
8840 OP *const other = newOP(OP_PADSV,
8841 kkid->op_flags
8842 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8));
8843 OP *const first = newOP(OP_NULL, 0);
8844 OP *const nullop = newCONDOP(0, first, o, other);
8845 OP *const condop = first->op_next;
8846 /* hijacking PADSTALE for uninitialized state variables */
8847 SvPADSTALE_on(PAD_SVl(target));
8848
8849 condop->op_type = OP_ONCE;
8850 condop->op_ppaddr = PL_ppaddr[OP_ONCE];
8851 condop->op_targ = target;
8852 other->op_targ = target;
8853
8854 /* Because we change the type of the op here, we will skip the
8855 assignment binop->op_last = binop->op_first->op_sibling; at the
8856 end of Perl_newBINOP(). So need to do it here. */
8857 cBINOPo->op_last = cBINOPo->op_first->op_sibling;
8858
8859 return nullop;
8860 }
8861 }
8862 return o;
8863}
8864
8865OP *
8866Perl_ck_match(pTHX_ OP *o)
8867{
8868 dVAR;
8869
8870 PERL_ARGS_ASSERT_CK_MATCH;
8871
8872 if (o->op_type != OP_QR && PL_compcv) {
8873 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
8874 if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS_isOUR(offset))) {
8875 o->op_targ = offset;
8876 o->op_private |= OPpTARGET_MY;
8877 }
8878 }
8879 if (o->op_type == OP_MATCH || o->op_type == OP_QR)
8880 o->op_private |= OPpRUNTIME;
8881 return o;
8882}
8883
8884OP *
8885Perl_ck_method(pTHX_ OP *o)
8886{
8887 OP * const kid = cUNOPo->op_first;
8888
8889 PERL_ARGS_ASSERT_CK_METHOD;
8890
8891 if (kid->op_type == OP_CONST) {
8892 SV* sv = kSVOP->op_sv;
8893 const char * const method = SvPVX_const(sv);
8894 if (!(strchr(method, ':') || strchr(method, '\''))) {
8895 OP *cmop;
8896 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
8897 sv = newSVpvn_share(method, SvUTF8(sv) ? -(I32)SvCUR(sv) : (I32)SvCUR(sv), 0);
8898 }
8899 else {
8900 kSVOP->op_sv = NULL;
8901 }
8902 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
8903#ifdef PERL_MAD
8904 op_getmad(o,cmop,'O');
8905#else
8906 op_free(o);
8907#endif
8908 return cmop;
8909 }
8910 }
8911 return o;
8912}
8913
8914OP *
8915Perl_ck_null(pTHX_ OP *o)
8916{
8917 PERL_ARGS_ASSERT_CK_NULL;
8918 PERL_UNUSED_CONTEXT;
8919 return o;
8920}
8921
8922OP *
8923Perl_ck_open(pTHX_ OP *o)
8924{
8925 dVAR;
8926 HV * const table = GvHV(PL_hintgv);
8927
8928 PERL_ARGS_ASSERT_CK_OPEN;
8929
8930 if (table) {
8931 SV **svp = hv_fetchs(table, "open_IN", FALSE);
8932 if (svp && *svp) {
8933 STRLEN len = 0;
8934 const char *d = SvPV_const(*svp, len);
8935 const I32 mode = mode_from_discipline(d, len);
8936 if (mode & O_BINARY)
8937 o->op_private |= OPpOPEN_IN_RAW;
8938 else if (mode & O_TEXT)
8939 o->op_private |= OPpOPEN_IN_CRLF;
8940 }
8941
8942 svp = hv_fetchs(table, "open_OUT", FALSE);
8943 if (svp && *svp) {
8944 STRLEN len = 0;
8945 const char *d = SvPV_const(*svp, len);
8946 const I32 mode = mode_from_discipline(d, len);
8947 if (mode & O_BINARY)
8948 o->op_private |= OPpOPEN_OUT_RAW;
8949 else if (mode & O_TEXT)
8950 o->op_private |= OPpOPEN_OUT_CRLF;
8951 }
8952 }
8953 if (o->op_type == OP_BACKTICK) {
8954 if (!(o->op_flags & OPf_KIDS)) {
8955 OP * const newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
8956#ifdef PERL_MAD
8957 op_getmad(o,newop,'O');
8958#else
8959 op_free(o);
8960#endif
8961 return newop;
8962 }
8963 return o;
8964 }
8965 {
8966 /* In case of three-arg dup open remove strictness
8967 * from the last arg if it is a bareword. */
8968 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
8969 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
8970 OP *oa;
8971 const char *mode;
8972
8973 if ((last->op_type == OP_CONST) && /* The bareword. */
8974 (last->op_private & OPpCONST_BARE) &&
8975 (last->op_private & OPpCONST_STRICT) &&
8976 (oa = first->op_sibling) && /* The fh. */
8977 (oa = oa->op_sibling) && /* The mode. */
8978 (oa->op_type == OP_CONST) &&
8979 SvPOK(((SVOP*)oa)->op_sv) &&
8980 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
8981 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
8982 (last == oa->op_sibling)) /* The bareword. */
8983 last->op_private &= ~OPpCONST_STRICT;
8984 }
8985 return ck_fun(o);
8986}
8987
8988OP *
8989Perl_ck_repeat(pTHX_ OP *o)
8990{
8991 PERL_ARGS_ASSERT_CK_REPEAT;
8992
8993 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
8994 o->op_private |= OPpREPEAT_DOLIST;
8995 cBINOPo->op_first = force_list(cBINOPo->op_first);
8996 }
8997 else
8998 scalar(o);
8999 return o;
9000}
9001
9002OP *
9003Perl_ck_require(pTHX_ OP *o)
9004{
9005 dVAR;
9006 GV* gv = NULL;
9007
9008 PERL_ARGS_ASSERT_CK_REQUIRE;
9009
9010 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
9011 SVOP * const kid = (SVOP*)cUNOPo->op_first;
9012
9013 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
9014 SV * const sv = kid->op_sv;
9015 U32 was_readonly = SvREADONLY(sv);
9016 char *s;
9017 STRLEN len;
9018 const char *end;
9019
9020 if (was_readonly) {
9021 if (SvFAKE(sv)) {
9022 sv_force_normal_flags(sv, 0);
9023 assert(!SvREADONLY(sv));
9024 was_readonly = 0;
9025 } else {
9026 SvREADONLY_off(sv);
9027 }
9028 }
9029
9030 s = SvPVX(sv);
9031 len = SvCUR(sv);
9032 end = s + len;
9033 for (; s < end; s++) {
9034 if (*s == ':' && s[1] == ':') {
9035 *s = '/';
9036 Move(s+2, s+1, end - s - 1, char);
9037 --end;
9038 }
9039 }
9040 SvEND_set(sv, end);
9041 sv_catpvs(sv, ".pm");
9042 SvFLAGS(sv) |= was_readonly;
9043 }
9044 }
9045
9046 if (!(o->op_flags & OPf_SPECIAL)) { /* Wasn't written as CORE::require */
9047 /* handle override, if any */
9048 gv = gv_fetchpvs("require", GV_NOTQUAL, SVt_PVCV);
9049 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
9050 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "require", FALSE);
9051 gv = gvp ? *gvp : NULL;
9052 }
9053 }
9054
9055 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
9056 OP *kid, *newop;
9057 if (o->op_flags & OPf_KIDS) {
9058 kid = cUNOPo->op_first;
9059 cUNOPo->op_first = NULL;
9060 }
9061 else {
9062 kid = newDEFSVOP();
9063 }
9064#ifndef PERL_MAD
9065 op_free(o);
9066#endif
9067 newop = newUNOP(OP_ENTERSUB, OPf_STACKED,
9068 op_append_elem(OP_LIST, kid,
9069 scalar(newUNOP(OP_RV2CV, 0,
9070 newGVOP(OP_GV, 0,
9071 gv)))));
9072 op_getmad(o,newop,'O');
9073 return newop;
9074 }
9075
9076 return scalar(ck_fun(o));
9077}
9078
9079OP *
9080Perl_ck_return(pTHX_ OP *o)
9081{
9082 dVAR;
9083 OP *kid;
9084
9085 PERL_ARGS_ASSERT_CK_RETURN;
9086
9087 kid = cLISTOPo->op_first->op_sibling;
9088 if (CvLVALUE(PL_compcv)) {
9089 for (; kid; kid = kid->op_sibling)
9090 op_lvalue(kid, OP_LEAVESUBLV);
9091 }
9092
9093 return o;
9094}
9095
9096OP *
9097Perl_ck_select(pTHX_ OP *o)
9098{
9099 dVAR;
9100 OP* kid;
9101
9102 PERL_ARGS_ASSERT_CK_SELECT;
9103
9104 if (o->op_flags & OPf_KIDS) {
9105 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9106 if (kid && kid->op_sibling) {
9107 o->op_type = OP_SSELECT;
9108 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
9109 o = ck_fun(o);
9110 return fold_constants(op_integerize(op_std_init(o)));
9111 }
9112 }
9113 o = ck_fun(o);
9114 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9115 if (kid && kid->op_type == OP_RV2GV)
9116 kid->op_private &= ~HINT_STRICT_REFS;
9117 return o;
9118}
9119
9120OP *
9121Perl_ck_shift(pTHX_ OP *o)
9122{
9123 dVAR;
9124 const I32 type = o->op_type;
9125
9126 PERL_ARGS_ASSERT_CK_SHIFT;
9127
9128 if (!(o->op_flags & OPf_KIDS)) {
9129 OP *argop;
9130
9131 if (!CvUNIQUE(PL_compcv)) {
9132 o->op_flags |= OPf_SPECIAL;
9133 return o;
9134 }
9135
9136 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
9137#ifdef PERL_MAD
9138 {
9139 OP * const oldo = o;
9140 o = newUNOP(type, 0, scalar(argop));
9141 op_getmad(oldo,o,'O');
9142 return o;
9143 }
9144#else
9145 op_free(o);
9146 return newUNOP(type, 0, scalar(argop));
9147#endif
9148 }
9149 return scalar(ck_fun(o));
9150}
9151
9152OP *
9153Perl_ck_sort(pTHX_ OP *o)
9154{
9155 dVAR;
9156 OP *firstkid;
9157 HV * const hinthv = GvHV(PL_hintgv);
9158
9159 PERL_ARGS_ASSERT_CK_SORT;
9160
9161 if (hinthv) {
9162 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE);
9163 if (svp) {
9164 const I32 sorthints = (I32)SvIV(*svp);
9165 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
9166 o->op_private |= OPpSORT_QSORT;
9167 if ((sorthints & HINT_SORT_STABLE) != 0)
9168 o->op_private |= OPpSORT_STABLE;
9169 }
9170 }
9171
9172 if (o->op_flags & OPf_STACKED)
9173 simplify_sort(o);
9174 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9175 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
9176 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
9177
9178 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
9179 LINKLIST(kid);
9180 if (kid->op_type == OP_SCOPE) {
9181 kid->op_next = 0;
9182 }
9183 else if (kid->op_type == OP_LEAVE) {
9184 OP *k;
9185 op_null(kid); /* wipe out leave */
9186 kid->op_next = kid;
9187
9188 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
9189 if (k->op_next == kid)
9190 k->op_next = 0;
9191 /* don't descend into loops */
9192 else if (k->op_type == OP_ENTERLOOP
9193 || k->op_type == OP_ENTERITER)
9194 {
9195 k = cLOOPx(k)->op_lastop;
9196 }
9197 }
9198 }
9199
9200 /* provide scalar context for comparison function/block */
9201 kid = scalar(firstkid);
9202 kid->op_next = kid;
9203 o->op_flags |= OPf_SPECIAL;
9204 }
9205
9206 firstkid = firstkid->op_sibling;
9207 }
9208
9209 /* provide list context for arguments */
9210 list(firstkid);
9211
9212 return o;
9213}
9214
9215STATIC void
9216S_simplify_sort(pTHX_ OP *o)
9217{
9218 dVAR;
9219 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
9220 OP *k;
9221 int descending;
9222 GV *gv;
9223 const char *gvname;
9224 bool have_scopeop;
9225
9226 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
9227
9228 if (!(o->op_flags & OPf_STACKED))
9229 return;
9230 GvMULTI_on(gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV));
9231 GvMULTI_on(gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV));
9232 kid = kUNOP->op_first; /* get past null */
9233 if (!(have_scopeop = kid->op_type == OP_SCOPE)
9234 && kid->op_type != OP_LEAVE)
9235 return;
9236 kid = kLISTOP->op_last; /* get past scope */
9237 switch(kid->op_type) {
9238 case OP_NCMP:
9239 case OP_I_NCMP:
9240 case OP_SCMP:
9241 if (!have_scopeop) goto padkids;
9242 break;
9243 default:
9244 return;
9245 }
9246 k = kid; /* remember this node*/
9247 if (kBINOP->op_first->op_type != OP_RV2SV
9248 || kBINOP->op_last ->op_type != OP_RV2SV)
9249 {
9250 /*
9251 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
9252 then used in a comparison. This catches most, but not
9253 all cases. For instance, it catches
9254 sort { my($a); $a <=> $b }
9255 but not
9256 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
9257 (although why you'd do that is anyone's guess).
9258 */
9259
9260 padkids:
9261 if (!ckWARN(WARN_SYNTAX)) return;
9262 kid = kBINOP->op_first;
9263 do {
9264 if (kid->op_type == OP_PADSV) {
9265 SV * const name = AvARRAY(PL_comppad_name)[kid->op_targ];
9266 if (SvCUR(name) == 2 && *SvPVX(name) == '$'
9267 && (SvPVX(name)[1] == 'a' || SvPVX(name)[1] == 'b'))
9268 /* diag_listed_as: "my %s" used in sort comparison */
9269 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9270 "\"%s %s\" used in sort comparison",
9271 SvPAD_STATE(name) ? "state" : "my",
9272 SvPVX(name));
9273 }
9274 } while ((kid = kid->op_sibling));
9275 return;
9276 }
9277 kid = kBINOP->op_first; /* get past cmp */
9278 if (kUNOP->op_first->op_type != OP_GV)
9279 return;
9280 kid = kUNOP->op_first; /* get past rv2sv */
9281 gv = kGVOP_gv;
9282 if (GvSTASH(gv) != PL_curstash)
9283 return;
9284 gvname = GvNAME(gv);
9285 if (*gvname == 'a' && gvname[1] == '\0')
9286 descending = 0;
9287 else if (*gvname == 'b' && gvname[1] == '\0')
9288 descending = 1;
9289 else
9290 return;
9291
9292 kid = k; /* back to cmp */
9293 /* already checked above that it is rv2sv */
9294 kid = kBINOP->op_last; /* down to 2nd arg */
9295 if (kUNOP->op_first->op_type != OP_GV)
9296 return;
9297 kid = kUNOP->op_first; /* get past rv2sv */
9298 gv = kGVOP_gv;
9299 if (GvSTASH(gv) != PL_curstash)
9300 return;
9301 gvname = GvNAME(gv);
9302 if ( descending
9303 ? !(*gvname == 'a' && gvname[1] == '\0')
9304 : !(*gvname == 'b' && gvname[1] == '\0'))
9305 return;
9306 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
9307 if (descending)
9308 o->op_private |= OPpSORT_DESCEND;
9309 if (k->op_type == OP_NCMP)
9310 o->op_private |= OPpSORT_NUMERIC;
9311 if (k->op_type == OP_I_NCMP)
9312 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
9313 kid = cLISTOPo->op_first->op_sibling;
9314 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
9315#ifdef PERL_MAD
9316 op_getmad(kid,o,'S'); /* then delete it */
9317#else
9318 op_free(kid); /* then delete it */
9319#endif
9320}
9321
9322OP *
9323Perl_ck_split(pTHX_ OP *o)
9324{
9325 dVAR;
9326 register OP *kid;
9327
9328 PERL_ARGS_ASSERT_CK_SPLIT;
9329
9330 if (o->op_flags & OPf_STACKED)
9331 return no_fh_allowed(o);
9332
9333 kid = cLISTOPo->op_first;
9334 if (kid->op_type != OP_NULL)
9335 Perl_croak(aTHX_ "panic: ck_split, type=%u", (unsigned) kid->op_type);
9336 kid = kid->op_sibling;
9337 op_free(cLISTOPo->op_first);
9338 if (kid)
9339 cLISTOPo->op_first = kid;
9340 else {
9341 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
9342 cLISTOPo->op_last = kid; /* There was only one element previously */
9343 }
9344
9345 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
9346 OP * const sibl = kid->op_sibling;
9347 kid->op_sibling = 0;
9348 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, 0, 0);
9349 if (cLISTOPo->op_first == cLISTOPo->op_last)
9350 cLISTOPo->op_last = kid;
9351 cLISTOPo->op_first = kid;
9352 kid->op_sibling = sibl;
9353 }
9354
9355 kid->op_type = OP_PUSHRE;
9356 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
9357 scalar(kid);
9358 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) {
9359 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
9360 "Use of /g modifier is meaningless in split");
9361 }
9362
9363 if (!kid->op_sibling)
9364 op_append_elem(OP_SPLIT, o, newDEFSVOP());
9365
9366 kid = kid->op_sibling;
9367 scalar(kid);
9368
9369 if (!kid->op_sibling)
9370 op_append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
9371 assert(kid->op_sibling);
9372
9373 kid = kid->op_sibling;
9374 scalar(kid);
9375
9376 if (kid->op_sibling)
9377 return too_many_arguments_pv(o,OP_DESC(o), 0);
9378
9379 return o;
9380}
9381
9382OP *
9383Perl_ck_join(pTHX_ OP *o)
9384{
9385 const OP * const kid = cLISTOPo->op_first->op_sibling;
9386
9387 PERL_ARGS_ASSERT_CK_JOIN;
9388
9389 if (kid && kid->op_type == OP_MATCH) {
9390 if (ckWARN(WARN_SYNTAX)) {
9391 const REGEXP *re = PM_GETRE(kPMOP);
9392 const SV *msg = re
9393 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
9394 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
9395 : newSVpvs_flags( "STRING", SVs_TEMP );
9396 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
9397 "/%"SVf"/ should probably be written as \"%"SVf"\"",
9398 SVfARG(msg), SVfARG(msg));
9399 }
9400 }
9401 return ck_fun(o);
9402}
9403
9404/*
9405=for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags
9406
9407Examines an op, which is expected to identify a subroutine at runtime,
9408and attempts to determine at compile time which subroutine it identifies.
9409This is normally used during Perl compilation to determine whether
9410a prototype can be applied to a function call. I<cvop> is the op
9411being considered, normally an C<rv2cv> op. A pointer to the identified
9412subroutine is returned, if it could be determined statically, and a null
9413pointer is returned if it was not possible to determine statically.
9414
9415Currently, the subroutine can be identified statically if the RV that the
9416C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
9417A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
9418suitable if the constant value must be an RV pointing to a CV. Details of
9419this process may change in future versions of Perl. If the C<rv2cv> op
9420has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
9421the subroutine statically: this flag is used to suppress compile-time
9422magic on a subroutine call, forcing it to use default runtime behaviour.
9423
9424If I<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
9425of a GV reference is modified. If a GV was examined and its CV slot was
9426found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
9427If the op is not optimised away, and the CV slot is later populated with
9428a subroutine having a prototype, that flag eventually triggers the warning
9429"called too early to check prototype".
9430
9431If I<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
9432of returning a pointer to the subroutine it returns a pointer to the
9433GV giving the most appropriate name for the subroutine in this context.
9434Normally this is just the C<CvGV> of the subroutine, but for an anonymous
9435(C<CvANON>) subroutine that is referenced through a GV it will be the
9436referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
9437A null pointer is returned as usual if there is no statically-determinable
9438subroutine.
9439
9440=cut
9441*/
9442
9443CV *
9444Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
9445{
9446 OP *rvop;
9447 CV *cv;
9448 GV *gv;
9449 PERL_ARGS_ASSERT_RV2CV_OP_CV;
9450 if (flags & ~(RV2CVOPCV_MARK_EARLY|RV2CVOPCV_RETURN_NAME_GV))
9451 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
9452 if (cvop->op_type != OP_RV2CV)
9453 return NULL;
9454 if (cvop->op_private & OPpENTERSUB_AMPER)
9455 return NULL;
9456 if (!(cvop->op_flags & OPf_KIDS))
9457 return NULL;
9458 rvop = cUNOPx(cvop)->op_first;
9459 switch (rvop->op_type) {
9460 case OP_GV: {
9461 gv = cGVOPx_gv(rvop);
9462 cv = GvCVu(gv);
9463 if (!cv) {
9464 if (flags & RV2CVOPCV_MARK_EARLY)
9465 rvop->op_private |= OPpEARLY_CV;
9466 return NULL;
9467 }
9468 } break;
9469 case OP_CONST: {
9470 SV *rv = cSVOPx_sv(rvop);
9471 if (!SvROK(rv))
9472 return NULL;
9473 cv = (CV*)SvRV(rv);
9474 gv = NULL;
9475 } break;
9476 default: {
9477 return NULL;
9478 } break;
9479 }
9480 if (SvTYPE((SV*)cv) != SVt_PVCV)
9481 return NULL;
9482 if (flags & RV2CVOPCV_RETURN_NAME_GV) {
9483 if (!CvANON(cv) || !gv)
9484 gv = CvGV(cv);
9485 return (CV*)gv;
9486 } else {
9487 return cv;
9488 }
9489}
9490
9491/*
9492=for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop
9493
9494Performs the default fixup of the arguments part of an C<entersub>
9495op tree. This consists of applying list context to each of the
9496argument ops. This is the standard treatment used on a call marked
9497with C<&>, or a method call, or a call through a subroutine reference,
9498or any other call where the callee can't be identified at compile time,
9499or a call where the callee has no prototype.
9500
9501=cut
9502*/
9503
9504OP *
9505Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
9506{
9507 OP *aop;
9508 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
9509 aop = cUNOPx(entersubop)->op_first;
9510 if (!aop->op_sibling)
9511 aop = cUNOPx(aop)->op_first;
9512 for (aop = aop->op_sibling; aop->op_sibling; aop = aop->op_sibling) {
9513 if (!(PL_madskills && aop->op_type == OP_STUB)) {
9514 list(aop);
9515 op_lvalue(aop, OP_ENTERSUB);
9516 }
9517 }
9518 return entersubop;
9519}
9520
9521/*
9522=for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv
9523
9524Performs the fixup of the arguments part of an C<entersub> op tree
9525based on a subroutine prototype. This makes various modifications to
9526the argument ops, from applying context up to inserting C<refgen> ops,
9527and checking the number and syntactic types of arguments, as directed by
9528the prototype. This is the standard treatment used on a subroutine call,
9529not marked with C<&>, where the callee can be identified at compile time
9530and has a prototype.
9531
9532I<protosv> supplies the subroutine prototype to be applied to the call.
9533It may be a normal defined scalar, of which the string value will be used.
9534Alternatively, for convenience, it may be a subroutine object (a C<CV*>
9535that has been cast to C<SV*>) which has a prototype. The prototype
9536supplied, in whichever form, does not need to match the actual callee
9537referenced by the op tree.
9538
9539If the argument ops disagree with the prototype, for example by having
9540an unacceptable number of arguments, a valid op tree is returned anyway.
9541The error is reflected in the parser state, normally resulting in a single
9542exception at the top level of parsing which covers all the compilation
9543errors that occurred. In the error message, the callee is referred to
9544by the name defined by the I<namegv> parameter.
9545
9546=cut
9547*/
9548
9549OP *
9550Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
9551{
9552 STRLEN proto_len;
9553 const char *proto, *proto_end;
9554 OP *aop, *prev, *cvop;
9555 int optional = 0;
9556 I32 arg = 0;
9557 I32 contextclass = 0;
9558 const char *e = NULL;
9559 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
9560 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
9561 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
9562 "flags=%lx", (unsigned long) SvFLAGS(protosv));
9563 if (SvTYPE(protosv) == SVt_PVCV)
9564 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
9565 else proto = SvPV(protosv, proto_len);
9566 proto_end = proto + proto_len;
9567 aop = cUNOPx(entersubop)->op_first;
9568 if (!aop->op_sibling)
9569 aop = cUNOPx(aop)->op_first;
9570 prev = aop;
9571 aop = aop->op_sibling;
9572 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
9573 while (aop != cvop) {
9574 OP* o3;
9575 if (PL_madskills && aop->op_type == OP_STUB) {
9576 aop = aop->op_sibling;
9577 continue;
9578 }
9579 if (PL_madskills && aop->op_type == OP_NULL)
9580 o3 = ((UNOP*)aop)->op_first;
9581 else
9582 o3 = aop;
9583
9584 if (proto >= proto_end)
9585 return too_many_arguments_sv(entersubop, gv_ename(namegv), 0);
9586
9587 switch (*proto) {
9588 case ';':
9589 optional = 1;
9590 proto++;
9591 continue;
9592 case '_':
9593 /* _ must be at the end */
9594 if (proto[1] && !strchr(";@%", proto[1]))
9595 goto oops;
9596 case '$':
9597 proto++;
9598 arg++;
9599 scalar(aop);
9600 break;
9601 case '%':
9602 case '@':
9603 list(aop);
9604 arg++;
9605 break;
9606 case '&':
9607 proto++;
9608 arg++;
9609 if (o3->op_type != OP_REFGEN && o3->op_type != OP_UNDEF)
9610 bad_type_sv(arg,
9611 arg == 1 ? "block or sub {}" : "sub {}",
9612 gv_ename(namegv), 0, o3);
9613 break;
9614 case '*':
9615 /* '*' allows any scalar type, including bareword */
9616 proto++;
9617 arg++;
9618 if (o3->op_type == OP_RV2GV)
9619 goto wrapref; /* autoconvert GLOB -> GLOBref */
9620 else if (o3->op_type == OP_CONST)
9621 o3->op_private &= ~OPpCONST_STRICT;
9622 else if (o3->op_type == OP_ENTERSUB) {
9623 /* accidental subroutine, revert to bareword */
9624 OP *gvop = ((UNOP*)o3)->op_first;
9625 if (gvop && gvop->op_type == OP_NULL) {
9626 gvop = ((UNOP*)gvop)->op_first;
9627 if (gvop) {
9628 for (; gvop->op_sibling; gvop = gvop->op_sibling)
9629 ;
9630 if (gvop &&
9631 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
9632 (gvop = ((UNOP*)gvop)->op_first) &&
9633 gvop->op_type == OP_GV)
9634 {
9635 GV * const gv = cGVOPx_gv(gvop);
9636 OP * const sibling = aop->op_sibling;
9637 SV * const n = newSVpvs("");
9638#ifdef PERL_MAD
9639 OP * const oldaop = aop;
9640#else
9641 op_free(aop);
9642#endif
9643 gv_fullname4(n, gv, "", FALSE);
9644 aop = newSVOP(OP_CONST, 0, n);
9645 op_getmad(oldaop,aop,'O');
9646 prev->op_sibling = aop;
9647 aop->op_sibling = sibling;
9648 }
9649 }
9650 }
9651 }
9652 scalar(aop);
9653 break;
9654 case '+':
9655 proto++;
9656 arg++;
9657 if (o3->op_type == OP_RV2AV ||
9658 o3->op_type == OP_PADAV ||
9659 o3->op_type == OP_RV2HV ||
9660 o3->op_type == OP_PADHV
9661 ) {
9662 goto wrapref;
9663 }
9664 scalar(aop);
9665 break;
9666 case '[': case ']':
9667 goto oops;
9668 break;
9669 case '\\':
9670 proto++;
9671 arg++;
9672 again:
9673 switch (*proto++) {
9674 case '[':
9675 if (contextclass++ == 0) {
9676 e = strchr(proto, ']');
9677 if (!e || e == proto)
9678 goto oops;
9679 }
9680 else
9681 goto oops;
9682 goto again;
9683 break;
9684 case ']':
9685 if (contextclass) {
9686 const char *p = proto;
9687 const char *const end = proto;
9688 contextclass = 0;
9689 while (*--p != '[')
9690 /* \[$] accepts any scalar lvalue */
9691 if (*p == '$'
9692 && Perl_op_lvalue_flags(aTHX_
9693 scalar(o3),
9694 OP_READ, /* not entersub */
9695 OP_LVALUE_NO_CROAK
9696 )) goto wrapref;
9697 bad_type_sv(arg, Perl_form(aTHX_ "one of %.*s",
9698 (int)(end - p), p),
9699 gv_ename(namegv), 0, o3);
9700 } else
9701 goto oops;
9702 break;
9703 case '*':
9704 if (o3->op_type == OP_RV2GV)
9705 goto wrapref;
9706 if (!contextclass)
9707 bad_type_sv(arg, "symbol", gv_ename(namegv), 0, o3);
9708 break;
9709 case '&':
9710 if (o3->op_type == OP_ENTERSUB)
9711 goto wrapref;
9712 if (!contextclass)
9713 bad_type_sv(arg, "subroutine entry", gv_ename(namegv), 0,
9714 o3);
9715 break;
9716 case '$':
9717 if (o3->op_type == OP_RV2SV ||
9718 o3->op_type == OP_PADSV ||
9719 o3->op_type == OP_HELEM ||
9720 o3->op_type == OP_AELEM)
9721 goto wrapref;
9722 if (!contextclass) {
9723 /* \$ accepts any scalar lvalue */
9724 if (Perl_op_lvalue_flags(aTHX_
9725 scalar(o3),
9726 OP_READ, /* not entersub */
9727 OP_LVALUE_NO_CROAK
9728 )) goto wrapref;
9729 bad_type_sv(arg, "scalar", gv_ename(namegv), 0, o3);
9730 }
9731 break;
9732 case '@':
9733 if (o3->op_type == OP_RV2AV ||
9734 o3->op_type == OP_PADAV)
9735 goto wrapref;
9736 if (!contextclass)
9737 bad_type_sv(arg, "array", gv_ename(namegv), 0, o3);
9738 break;
9739 case '%':
9740 if (o3->op_type == OP_RV2HV ||
9741 o3->op_type == OP_PADHV)
9742 goto wrapref;
9743 if (!contextclass)
9744 bad_type_sv(arg, "hash", gv_ename(namegv), 0, o3);
9745 break;
9746 wrapref:
9747 {
9748 OP* const kid = aop;
9749 OP* const sib = kid->op_sibling;
9750 kid->op_sibling = 0;
9751 aop = newUNOP(OP_REFGEN, 0, kid);
9752 aop->op_sibling = sib;
9753 prev->op_sibling = aop;
9754 }
9755 if (contextclass && e) {
9756 proto = e + 1;
9757 contextclass = 0;
9758 }
9759 break;
9760 default: goto oops;
9761 }
9762 if (contextclass)
9763 goto again;
9764 break;
9765 case ' ':
9766 proto++;
9767 continue;
9768 default:
9769 oops: {
9770 SV* const tmpsv = sv_newmortal();
9771 gv_efullname3(tmpsv, namegv, NULL);
9772 Perl_croak(aTHX_ "Malformed prototype for %"SVf": %"SVf,
9773 SVfARG(tmpsv), SVfARG(protosv));
9774 }
9775 }
9776
9777 op_lvalue(aop, OP_ENTERSUB);
9778 prev = aop;
9779 aop = aop->op_sibling;
9780 }
9781 if (aop == cvop && *proto == '_') {
9782 /* generate an access to $_ */
9783 aop = newDEFSVOP();
9784 aop->op_sibling = prev->op_sibling;
9785 prev->op_sibling = aop; /* instead of cvop */
9786 }
9787 if (!optional && proto_end > proto &&
9788 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
9789 return too_few_arguments_sv(entersubop, gv_ename(namegv), 0);
9790 return entersubop;
9791}
9792
9793/*
9794=for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv
9795
9796Performs the fixup of the arguments part of an C<entersub> op tree either
9797based on a subroutine prototype or using default list-context processing.
9798This is the standard treatment used on a subroutine call, not marked
9799with C<&>, where the callee can be identified at compile time.
9800
9801I<protosv> supplies the subroutine prototype to be applied to the call,
9802or indicates that there is no prototype. It may be a normal scalar,
9803in which case if it is defined then the string value will be used
9804as a prototype, and if it is undefined then there is no prototype.
9805Alternatively, for convenience, it may be a subroutine object (a C<CV*>
9806that has been cast to C<SV*>), of which the prototype will be used if it
9807has one. The prototype (or lack thereof) supplied, in whichever form,
9808does not need to match the actual callee referenced by the op tree.
9809
9810If the argument ops disagree with the prototype, for example by having
9811an unacceptable number of arguments, a valid op tree is returned anyway.
9812The error is reflected in the parser state, normally resulting in a single
9813exception at the top level of parsing which covers all the compilation
9814errors that occurred. In the error message, the callee is referred to
9815by the name defined by the I<namegv> parameter.
9816
9817=cut
9818*/
9819
9820OP *
9821Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
9822 GV *namegv, SV *protosv)
9823{
9824 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
9825 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
9826 return ck_entersub_args_proto(entersubop, namegv, protosv);
9827 else
9828 return ck_entersub_args_list(entersubop);
9829}
9830
9831OP *
9832Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
9833{
9834 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv);
9835 OP *aop = cUNOPx(entersubop)->op_first;
9836
9837 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
9838
9839 if (!opnum) {
9840 OP *cvop;
9841 if (!aop->op_sibling)
9842 aop = cUNOPx(aop)->op_first;
9843 aop = aop->op_sibling;
9844 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
9845 if (PL_madskills) while (aop != cvop && aop->op_type == OP_STUB) {
9846 aop = aop->op_sibling;
9847 }
9848 if (aop != cvop)
9849 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0);
9850
9851 op_free(entersubop);
9852 switch(GvNAME(namegv)[2]) {
9853 case 'F': return newSVOP(OP_CONST, 0,
9854 newSVpv(CopFILE(PL_curcop),0));
9855 case 'L': return newSVOP(
9856 OP_CONST, 0,
9857 Perl_newSVpvf(aTHX_
9858 "%"IVdf, (IV)CopLINE(PL_curcop)
9859 )
9860 );
9861 case 'P': return newSVOP(OP_CONST, 0,
9862 (PL_curstash
9863 ? newSVhek(HvNAME_HEK(PL_curstash))
9864 : &PL_sv_undef
9865 )
9866 );
9867 }
9868 assert(0);
9869 }
9870 else {
9871 OP *prev, *cvop;
9872 U32 flags;
9873#ifdef PERL_MAD
9874 bool seenarg = FALSE;
9875#endif
9876 if (!aop->op_sibling)
9877 aop = cUNOPx(aop)->op_first;
9878
9879 prev = aop;
9880 aop = aop->op_sibling;
9881 prev->op_sibling = NULL;
9882 for (cvop = aop;
9883 cvop->op_sibling;
9884 prev=cvop, cvop = cvop->op_sibling)
9885#ifdef PERL_MAD
9886 if (PL_madskills && cvop->op_sibling
9887 && cvop->op_type != OP_STUB) seenarg = TRUE
9888#endif
9889 ;
9890 prev->op_sibling = NULL;
9891 flags = OPf_SPECIAL * !(cvop->op_private & OPpENTERSUB_NOPAREN);
9892 op_free(cvop);
9893 if (aop == cvop) aop = NULL;
9894 op_free(entersubop);
9895
9896 if (opnum == OP_ENTEREVAL
9897 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9))
9898 flags |= OPpEVAL_BYTES <<8;
9899
9900 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
9901 case OA_UNOP:
9902 case OA_BASEOP_OR_UNOP:
9903 case OA_FILESTATOP:
9904 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags);
9905 case OA_BASEOP:
9906 if (aop) {
9907#ifdef PERL_MAD
9908 if (!PL_madskills || seenarg)
9909#endif
9910 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0);
9911 op_free(aop);
9912 }
9913 return opnum == OP_RUNCV
9914 ? newPVOP(OP_RUNCV,0,NULL)
9915 : newOP(opnum,0);
9916 default:
9917 return convert(opnum,0,aop);
9918 }
9919 }
9920 assert(0);
9921 return entersubop;
9922}
9923
9924/*
9925=for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p
9926
9927Retrieves the function that will be used to fix up a call to I<cv>.
9928Specifically, the function is applied to an C<entersub> op tree for a
9929subroutine call, not marked with C<&>, where the callee can be identified
9930at compile time as I<cv>.
9931
9932The C-level function pointer is returned in I<*ckfun_p>, and an SV
9933argument for it is returned in I<*ckobj_p>. The function is intended
9934to be called in this manner:
9935
9936 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
9937
9938In this call, I<entersubop> is a pointer to the C<entersub> op,
9939which may be replaced by the check function, and I<namegv> is a GV
9940supplying the name that should be used by the check function to refer
9941to the callee of the C<entersub> op if it needs to emit any diagnostics.
9942It is permitted to apply the check function in non-standard situations,
9943such as to a call to a different subroutine or to a method call.
9944
9945By default, the function is
9946L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
9947and the SV parameter is I<cv> itself. This implements standard
9948prototype processing. It can be changed, for a particular subroutine,
9949by L</cv_set_call_checker>.
9950
9951=cut
9952*/
9953
9954void
9955Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
9956{
9957 MAGIC *callmg;
9958 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
9959 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
9960 if (callmg) {
9961 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
9962 *ckobj_p = callmg->mg_obj;
9963 } else {
9964 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
9965 *ckobj_p = (SV*)cv;
9966 }
9967}
9968
9969/*
9970=for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj
9971
9972Sets the function that will be used to fix up a call to I<cv>.
9973Specifically, the function is applied to an C<entersub> op tree for a
9974subroutine call, not marked with C<&>, where the callee can be identified
9975at compile time as I<cv>.
9976
9977The C-level function pointer is supplied in I<ckfun>, and an SV argument
9978for it is supplied in I<ckobj>. The function is intended to be called
9979in this manner:
9980
9981 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
9982
9983In this call, I<entersubop> is a pointer to the C<entersub> op,
9984which may be replaced by the check function, and I<namegv> is a GV
9985supplying the name that should be used by the check function to refer
9986to the callee of the C<entersub> op if it needs to emit any diagnostics.
9987It is permitted to apply the check function in non-standard situations,
9988such as to a call to a different subroutine or to a method call.
9989
9990The current setting for a particular CV can be retrieved by
9991L</cv_get_call_checker>.
9992
9993=cut
9994*/
9995
9996void
9997Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
9998{
9999 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
10000 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
10001 if (SvMAGICAL((SV*)cv))
10002 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
10003 } else {
10004 MAGIC *callmg;
10005 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
10006 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
10007 if (callmg->mg_flags & MGf_REFCOUNTED) {
10008 SvREFCNT_dec(callmg->mg_obj);
10009 callmg->mg_flags &= ~MGf_REFCOUNTED;
10010 }
10011 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
10012 callmg->mg_obj = ckobj;
10013 if (ckobj != (SV*)cv) {
10014 SvREFCNT_inc_simple_void_NN(ckobj);
10015 callmg->mg_flags |= MGf_REFCOUNTED;
10016 }
10017 callmg->mg_flags |= MGf_COPY;
10018 }
10019}
10020
10021OP *
10022Perl_ck_subr(pTHX_ OP *o)
10023{
10024 OP *aop, *cvop;
10025 CV *cv;
10026 GV *namegv;
10027
10028 PERL_ARGS_ASSERT_CK_SUBR;
10029
10030 aop = cUNOPx(o)->op_first;
10031 if (!aop->op_sibling)
10032 aop = cUNOPx(aop)->op_first;
10033 aop = aop->op_sibling;
10034 for (cvop = aop; cvop->op_sibling; cvop = cvop->op_sibling) ;
10035 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
10036 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_RETURN_NAME_GV) : NULL;
10037
10038 o->op_private &= ~1;
10039 o->op_private |= OPpENTERSUB_HASTARG;
10040 o->op_private |= (PL_hints & HINT_STRICT_REFS);
10041 if (PERLDB_SUB && PL_curstash != PL_debstash)
10042 o->op_private |= OPpENTERSUB_DB;
10043 if (cvop->op_type == OP_RV2CV) {
10044 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
10045 op_null(cvop);
10046 } else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
10047 if (aop->op_type == OP_CONST)
10048 aop->op_private &= ~OPpCONST_STRICT;
10049 else if (aop->op_type == OP_LIST) {
10050 OP * const sib = ((UNOP*)aop)->op_first->op_sibling;
10051 if (sib && sib->op_type == OP_CONST)
10052 sib->op_private &= ~OPpCONST_STRICT;
10053 }
10054 }
10055
10056 if (!cv) {
10057 return ck_entersub_args_list(o);
10058 } else {
10059 Perl_call_checker ckfun;
10060 SV *ckobj;
10061 cv_get_call_checker(cv, &ckfun, &ckobj);
10062 return ckfun(aTHX_ o, namegv, ckobj);
10063 }
10064}
10065
10066OP *
10067Perl_ck_svconst(pTHX_ OP *o)
10068{
10069 PERL_ARGS_ASSERT_CK_SVCONST;
10070 PERL_UNUSED_CONTEXT;
10071 SvREADONLY_on(cSVOPo->op_sv);
10072 return o;
10073}
10074
10075OP *
10076Perl_ck_chdir(pTHX_ OP *o)
10077{
10078 PERL_ARGS_ASSERT_CK_CHDIR;
10079 if (o->op_flags & OPf_KIDS) {
10080 SVOP * const kid = (SVOP*)cUNOPo->op_first;
10081
10082 if (kid && kid->op_type == OP_CONST &&
10083 (kid->op_private & OPpCONST_BARE))
10084 {
10085 o->op_flags |= OPf_SPECIAL;
10086 kid->op_private &= ~OPpCONST_STRICT;
10087 }
10088 }
10089 return ck_fun(o);
10090}
10091
10092OP *
10093Perl_ck_trunc(pTHX_ OP *o)
10094{
10095 PERL_ARGS_ASSERT_CK_TRUNC;
10096
10097 if (o->op_flags & OPf_KIDS) {
10098 SVOP *kid = (SVOP*)cUNOPo->op_first;
10099
10100 if (kid->op_type == OP_NULL)
10101 kid = (SVOP*)kid->op_sibling;
10102 if (kid && kid->op_type == OP_CONST &&
10103 (kid->op_private & OPpCONST_BARE))
10104 {
10105 o->op_flags |= OPf_SPECIAL;
10106 kid->op_private &= ~OPpCONST_STRICT;
10107 }
10108 }
10109 return ck_fun(o);
10110}
10111
10112OP *
10113Perl_ck_substr(pTHX_ OP *o)
10114{
10115 PERL_ARGS_ASSERT_CK_SUBSTR;
10116
10117 o = ck_fun(o);
10118 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
10119 OP *kid = cLISTOPo->op_first;
10120
10121 if (kid->op_type == OP_NULL)
10122 kid = kid->op_sibling;
10123 if (kid)
10124 kid->op_flags |= OPf_MOD;
10125
10126 }
10127 return o;
10128}
10129
10130OP *
10131Perl_ck_tell(pTHX_ OP *o)
10132{
10133 PERL_ARGS_ASSERT_CK_TELL;
10134 o = ck_fun(o);
10135 if (o->op_flags & OPf_KIDS) {
10136 OP *kid = cLISTOPo->op_first;
10137 if (kid->op_type == OP_NULL && kid->op_sibling) kid = kid->op_sibling;
10138 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
10139 }
10140 return o;
10141}
10142
10143OP *
10144Perl_ck_each(pTHX_ OP *o)
10145{
10146 dVAR;
10147 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
10148 const unsigned orig_type = o->op_type;
10149 const unsigned array_type = orig_type == OP_EACH ? OP_AEACH
10150 : orig_type == OP_KEYS ? OP_AKEYS : OP_AVALUES;
10151 const unsigned ref_type = orig_type == OP_EACH ? OP_REACH
10152 : orig_type == OP_KEYS ? OP_RKEYS : OP_RVALUES;
10153
10154 PERL_ARGS_ASSERT_CK_EACH;
10155
10156 if (kid) {
10157 switch (kid->op_type) {
10158 case OP_PADHV:
10159 case OP_RV2HV:
10160 break;
10161 case OP_PADAV:
10162 case OP_RV2AV:
10163 CHANGE_TYPE(o, array_type);
10164 break;
10165 case OP_CONST:
10166 if (kid->op_private == OPpCONST_BARE
10167 || !SvROK(cSVOPx_sv(kid))
10168 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
10169 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
10170 )
10171 /* we let ck_fun handle it */
10172 break;
10173 default:
10174 CHANGE_TYPE(o, ref_type);
10175 scalar(kid);
10176 }
10177 }
10178 /* if treating as a reference, defer additional checks to runtime */
10179 return o->op_type == ref_type ? o : ck_fun(o);
10180}
10181
10182OP *
10183Perl_ck_length(pTHX_ OP *o)
10184{
10185 PERL_ARGS_ASSERT_CK_LENGTH;
10186
10187 o = ck_fun(o);
10188
10189 if (ckWARN(WARN_SYNTAX)) {
10190 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
10191
10192 if (kid) {
10193 SV *name = NULL;
10194 const bool hash = kid->op_type == OP_PADHV
10195 || kid->op_type == OP_RV2HV;
10196 switch (kid->op_type) {
10197 case OP_PADHV:
10198 case OP_PADAV:
10199 name = varname(
10200 (GV *)PL_compcv, hash ? '%' : '@', kid->op_targ,
10201 NULL, 0, 1
10202 );
10203 break;
10204 case OP_RV2HV:
10205 case OP_RV2AV:
10206 if (cUNOPx(kid)->op_first->op_type != OP_GV) break;
10207 {
10208 GV *gv = cGVOPx_gv(cUNOPx(kid)->op_first);
10209 if (!gv) break;
10210 name = varname(gv, hash?'%':'@', 0, NULL, 0, 1);
10211 }
10212 break;
10213 default:
10214 return o;
10215 }
10216 if (name)
10217 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10218 "length() used on %"SVf" (did you mean \"scalar(%s%"SVf
10219 ")\"?)",
10220 name, hash ? "keys " : "", name
10221 );
10222 else if (hash)
10223 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10224 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
10225 else
10226 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
10227 "length() used on @array (did you mean \"scalar(@array)\"?)");
10228 }
10229 }
10230
10231 return o;
10232}
10233
10234/* caller is supposed to assign the return to the
10235 container of the rep_op var */
10236STATIC OP *
10237S_opt_scalarhv(pTHX_ OP *rep_op) {
10238 dVAR;
10239 UNOP *unop;
10240
10241 PERL_ARGS_ASSERT_OPT_SCALARHV;
10242
10243 NewOp(1101, unop, 1, UNOP);
10244 unop->op_type = (OPCODE)OP_BOOLKEYS;
10245 unop->op_ppaddr = PL_ppaddr[OP_BOOLKEYS];
10246 unop->op_flags = (U8)(OPf_WANT_SCALAR | OPf_KIDS );
10247 unop->op_private = (U8)(1 | ((OPf_WANT_SCALAR | OPf_KIDS) >> 8));
10248 unop->op_first = rep_op;
10249 unop->op_next = rep_op->op_next;
10250 rep_op->op_next = (OP*)unop;
10251 rep_op->op_flags|=(OPf_REF | OPf_MOD);
10252 unop->op_sibling = rep_op->op_sibling;
10253 rep_op->op_sibling = NULL;
10254 /* unop->op_targ = pad_alloc(OP_BOOLKEYS, SVs_PADTMP); */
10255 if (rep_op->op_type == OP_PADHV) {
10256 rep_op->op_flags &= ~OPf_WANT_SCALAR;
10257 rep_op->op_flags |= OPf_WANT_LIST;
10258 }
10259 return (OP*)unop;
10260}
10261
10262/* Check for in place reverse and sort assignments like "@a = reverse @a"
10263 and modify the optree to make them work inplace */
10264
10265STATIC void
10266S_inplace_aassign(pTHX_ OP *o) {
10267
10268 OP *modop, *modop_pushmark;
10269 OP *oright;
10270 OP *oleft, *oleft_pushmark;
10271
10272 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
10273
10274 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
10275
10276 assert(cUNOPo->op_first->op_type == OP_NULL);
10277 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
10278 assert(modop_pushmark->op_type == OP_PUSHMARK);
10279 modop = modop_pushmark->op_sibling;
10280
10281 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
10282 return;
10283
10284 /* no other operation except sort/reverse */
10285 if (modop->op_sibling)
10286 return;
10287
10288 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
10289 if (!(oright = cUNOPx(modop)->op_first->op_sibling)) return;
10290
10291 if (modop->op_flags & OPf_STACKED) {
10292 /* skip sort subroutine/block */
10293 assert(oright->op_type == OP_NULL);
10294 oright = oright->op_sibling;
10295 }
10296
10297 assert(cUNOPo->op_first->op_sibling->op_type == OP_NULL);
10298 oleft_pushmark = cUNOPx(cUNOPo->op_first->op_sibling)->op_first;
10299 assert(oleft_pushmark->op_type == OP_PUSHMARK);
10300 oleft = oleft_pushmark->op_sibling;
10301
10302 /* Check the lhs is an array */
10303 if (!oleft ||
10304 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
10305 || oleft->op_sibling
10306 || (oleft->op_private & OPpLVAL_INTRO)
10307 )
10308 return;
10309
10310 /* Only one thing on the rhs */
10311 if (oright->op_sibling)
10312 return;
10313
10314 /* check the array is the same on both sides */
10315 if (oleft->op_type == OP_RV2AV) {
10316 if (oright->op_type != OP_RV2AV
10317 || !cUNOPx(oright)->op_first
10318 || cUNOPx(oright)->op_first->op_type != OP_GV
10319 || cUNOPx(oleft )->op_first->op_type != OP_GV
10320 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
10321 cGVOPx_gv(cUNOPx(oright)->op_first)
10322 )
10323 return;
10324 }
10325 else if (oright->op_type != OP_PADAV
10326 || oright->op_targ != oleft->op_targ
10327 )
10328 return;
10329
10330 /* This actually is an inplace assignment */
10331
10332 modop->op_private |= OPpSORT_INPLACE;
10333
10334 /* transfer MODishness etc from LHS arg to RHS arg */
10335 oright->op_flags = oleft->op_flags;
10336
10337 /* remove the aassign op and the lhs */
10338 op_null(o);
10339 op_null(oleft_pushmark);
10340 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
10341 op_null(cUNOPx(oleft)->op_first);
10342 op_null(oleft);
10343}
10344
10345#define MAX_DEFERRED 4
10346
10347#define DEFER(o) \
10348 STMT_START { \
10349 if (defer_ix == (MAX_DEFERRED-1)) { \
10350 CALL_RPEEP(defer_queue[defer_base]); \
10351 defer_base = (defer_base + 1) % MAX_DEFERRED; \
10352 defer_ix--; \
10353 } \
10354 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = o; \
10355 } STMT_END
10356
10357/* A peephole optimizer. We visit the ops in the order they're to execute.
10358 * See the comments at the top of this file for more details about when
10359 * peep() is called */
10360
10361void
10362Perl_rpeep(pTHX_ register OP *o)
10363{
10364 dVAR;
10365 register OP* oldop = NULL;
10366 OP* defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */
10367 int defer_base = 0;
10368 int defer_ix = -1;
10369
10370 if (!o || o->op_opt)
10371 return;
10372 ENTER;
10373 SAVEOP();
10374 SAVEVPTR(PL_curcop);
10375 for (;; o = o->op_next) {
10376 if (o && o->op_opt)
10377 o = NULL;
10378 if (!o) {
10379 while (defer_ix >= 0)
10380 CALL_RPEEP(defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED]);
10381 break;
10382 }
10383
10384 /* By default, this op has now been optimised. A couple of cases below
10385 clear this again. */
10386 o->op_opt = 1;
10387 PL_op = o;
10388 switch (o->op_type) {
10389 case OP_DBSTATE:
10390 PL_curcop = ((COP*)o); /* for warnings */
10391 break;
10392 case OP_NEXTSTATE:
10393 PL_curcop = ((COP*)o); /* for warnings */
10394
10395 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen
10396 to carry two labels. For now, take the easier option, and skip
10397 this optimisation if the first NEXTSTATE has a label. */
10398 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) {
10399 OP *nextop = o->op_next;
10400 while (nextop && nextop->op_type == OP_NULL)
10401 nextop = nextop->op_next;
10402
10403 if (nextop && (nextop->op_type == OP_NEXTSTATE)) {
10404 COP *firstcop = (COP *)o;
10405 COP *secondcop = (COP *)nextop;
10406 /* We want the COP pointed to by o (and anything else) to
10407 become the next COP down the line. */
10408 cop_free(firstcop);
10409
10410 firstcop->op_next = secondcop->op_next;
10411
10412 /* Now steal all its pointers, and duplicate the other
10413 data. */
10414 firstcop->cop_line = secondcop->cop_line;
10415#ifdef USE_ITHREADS
10416 firstcop->cop_stashoff = secondcop->cop_stashoff;
10417 firstcop->cop_file = secondcop->cop_file;
10418#else
10419 firstcop->cop_stash = secondcop->cop_stash;
10420 firstcop->cop_filegv = secondcop->cop_filegv;
10421#endif
10422 firstcop->cop_hints = secondcop->cop_hints;
10423 firstcop->cop_seq = secondcop->cop_seq;
10424 firstcop->cop_warnings = secondcop->cop_warnings;
10425 firstcop->cop_hints_hash = secondcop->cop_hints_hash;
10426
10427#ifdef USE_ITHREADS
10428 secondcop->cop_stashoff = 0;
10429 secondcop->cop_file = NULL;
10430#else
10431 secondcop->cop_stash = NULL;
10432 secondcop->cop_filegv = NULL;
10433#endif
10434 secondcop->cop_warnings = NULL;
10435 secondcop->cop_hints_hash = NULL;
10436
10437 /* If we use op_null(), and hence leave an ex-COP, some
10438 warnings are misreported. For example, the compile-time
10439 error in 'use strict; no strict refs;' */
10440 secondcop->op_type = OP_NULL;
10441 secondcop->op_ppaddr = PL_ppaddr[OP_NULL];
10442 }
10443 }
10444 break;
10445
10446 case OP_CONCAT:
10447 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
10448 if (o->op_next->op_private & OPpTARGET_MY) {
10449 if (o->op_flags & OPf_STACKED) /* chained concats */
10450 break; /* ignore_optimization */
10451 else {
10452 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
10453 o->op_targ = o->op_next->op_targ;
10454 o->op_next->op_targ = 0;
10455 o->op_private |= OPpTARGET_MY;
10456 }
10457 }
10458 op_null(o->op_next);
10459 }
10460 break;
10461 case OP_STUB:
10462 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
10463 break; /* Scalar stub must produce undef. List stub is noop */
10464 }
10465 goto nothin;
10466 case OP_NULL:
10467 if (o->op_targ == OP_NEXTSTATE
10468 || o->op_targ == OP_DBSTATE)
10469 {
10470 PL_curcop = ((COP*)o);
10471 }
10472 /* XXX: We avoid setting op_seq here to prevent later calls
10473 to rpeep() from mistakenly concluding that optimisation
10474 has already occurred. This doesn't fix the real problem,
10475 though (See 20010220.007). AMS 20010719 */
10476 /* op_seq functionality is now replaced by op_opt */
10477 o->op_opt = 0;
10478 /* FALL THROUGH */
10479 case OP_SCALAR:
10480 case OP_LINESEQ:
10481 case OP_SCOPE:
10482 nothin:
10483 if (oldop && o->op_next) {
10484 oldop->op_next = o->op_next;
10485 o->op_opt = 0;
10486 continue;
10487 }
10488 break;
10489
10490 case OP_PADAV:
10491 case OP_GV:
10492 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
10493 OP* const pop = (o->op_type == OP_PADAV) ?
10494 o->op_next : o->op_next->op_next;
10495 IV i;
10496 if (pop && pop->op_type == OP_CONST &&
10497 ((PL_op = pop->op_next)) &&
10498 pop->op_next->op_type == OP_AELEM &&
10499 !(pop->op_next->op_private &
10500 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
10501 (i = SvIV(((SVOP*)pop)->op_sv)) <= 255 && i >= 0)
10502 {
10503 GV *gv;
10504 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
10505 no_bareword_allowed(pop);
10506 if (o->op_type == OP_GV)
10507 op_null(o->op_next);
10508 op_null(pop->op_next);
10509 op_null(pop);
10510 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
10511 o->op_next = pop->op_next->op_next;
10512 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
10513 o->op_private = (U8)i;
10514 if (o->op_type == OP_GV) {
10515 gv = cGVOPo_gv;
10516 GvAVn(gv);
10517 o->op_type = OP_AELEMFAST;
10518 }
10519 else
10520 o->op_type = OP_AELEMFAST_LEX;
10521 }
10522 break;
10523 }
10524
10525 if (o->op_next->op_type == OP_RV2SV) {
10526 if (!(o->op_next->op_private & OPpDEREF)) {
10527 op_null(o->op_next);
10528 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
10529 | OPpOUR_INTRO);
10530 o->op_next = o->op_next->op_next;
10531 o->op_type = OP_GVSV;
10532 o->op_ppaddr = PL_ppaddr[OP_GVSV];
10533 }
10534 }
10535 else if (o->op_next->op_type == OP_READLINE
10536 && o->op_next->op_next->op_type == OP_CONCAT
10537 && (o->op_next->op_next->op_flags & OPf_STACKED))
10538 {
10539 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
10540 o->op_type = OP_RCATLINE;
10541 o->op_flags |= OPf_STACKED;
10542 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
10543 op_null(o->op_next->op_next);
10544 op_null(o->op_next);
10545 }
10546
10547 break;
10548
10549 {
10550 OP *fop;
10551 OP *sop;
10552
10553 case OP_NOT:
10554 fop = cUNOP->op_first;
10555 sop = NULL;
10556 goto stitch_keys;
10557 break;
10558
10559 case OP_AND:
10560 case OP_OR:
10561 case OP_DOR:
10562 fop = cLOGOP->op_first;
10563 sop = fop->op_sibling;
10564 while (cLOGOP->op_other->op_type == OP_NULL)
10565 cLOGOP->op_other = cLOGOP->op_other->op_next;
10566 while (o->op_next && ( o->op_type == o->op_next->op_type
10567 || o->op_next->op_type == OP_NULL))
10568 o->op_next = o->op_next->op_next;
10569 DEFER(cLOGOP->op_other);
10570
10571 stitch_keys:
10572 o->op_opt = 1;
10573 if ((fop->op_type == OP_PADHV || fop->op_type == OP_RV2HV)
10574 || ( sop &&
10575 (sop->op_type == OP_PADHV || sop->op_type == OP_RV2HV)
10576 )
10577 ){
10578 OP * nop = o;
10579 OP * lop = o;
10580 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) {
10581 while (nop && nop->op_next) {
10582 switch (nop->op_next->op_type) {
10583 case OP_NOT:
10584 case OP_AND:
10585 case OP_OR:
10586 case OP_DOR:
10587 lop = nop = nop->op_next;
10588 break;
10589 case OP_NULL:
10590 nop = nop->op_next;
10591 break;
10592 default:
10593 nop = NULL;
10594 break;
10595 }
10596 }
10597 }
10598 if ((lop->op_flags & OPf_WANT) == OPf_WANT_VOID) {
10599 if (fop->op_type == OP_PADHV || fop->op_type == OP_RV2HV)
10600 cLOGOP->op_first = opt_scalarhv(fop);
10601 if (sop && (sop->op_type == OP_PADHV || sop->op_type == OP_RV2HV))
10602 cLOGOP->op_first->op_sibling = opt_scalarhv(sop);
10603 }
10604 }
10605
10606
10607 break;
10608 }
10609
10610 case OP_MAPWHILE:
10611 case OP_GREPWHILE:
10612 case OP_ANDASSIGN:
10613 case OP_ORASSIGN:
10614 case OP_DORASSIGN:
10615 case OP_COND_EXPR:
10616 case OP_RANGE:
10617 case OP_ONCE:
10618 while (cLOGOP->op_other->op_type == OP_NULL)
10619 cLOGOP->op_other = cLOGOP->op_other->op_next;
10620 DEFER(cLOGOP->op_other);
10621 break;
10622
10623 case OP_ENTERLOOP:
10624 case OP_ENTERITER:
10625 while (cLOOP->op_redoop->op_type == OP_NULL)
10626 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
10627 while (cLOOP->op_nextop->op_type == OP_NULL)
10628 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
10629 while (cLOOP->op_lastop->op_type == OP_NULL)
10630 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
10631 /* a while(1) loop doesn't have an op_next that escapes the
10632 * loop, so we have to explicitly follow the op_lastop to
10633 * process the rest of the code */
10634 DEFER(cLOOP->op_lastop);
10635 break;
10636
10637 case OP_SUBST:
10638 assert(!(cPMOP->op_pmflags & PMf_ONCE));
10639 while (cPMOP->op_pmstashstartu.op_pmreplstart &&
10640 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL)
10641 cPMOP->op_pmstashstartu.op_pmreplstart
10642 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next;
10643 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart);
10644 break;
10645
10646 case OP_SORT: {
10647 OP *oright;
10648
10649 if (o->op_flags & OPf_STACKED) {
10650 OP * const kid =
10651 cUNOPx(cLISTOP->op_first->op_sibling)->op_first;
10652 if (kid->op_type == OP_SCOPE
10653 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE))
10654 DEFER(kLISTOP->op_first);
10655 }
10656
10657 /* check that RHS of sort is a single plain array */
10658 oright = cUNOPo->op_first;
10659 if (!oright || oright->op_type != OP_PUSHMARK)
10660 break;
10661
10662 if (o->op_private & OPpSORT_INPLACE)
10663 break;
10664
10665 /* reverse sort ... can be optimised. */
10666 if (!cUNOPo->op_sibling) {
10667 /* Nothing follows us on the list. */
10668 OP * const reverse = o->op_next;
10669
10670 if (reverse->op_type == OP_REVERSE &&
10671 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
10672 OP * const pushmark = cUNOPx(reverse)->op_first;
10673 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
10674 && (cUNOPx(pushmark)->op_sibling == o)) {
10675 /* reverse -> pushmark -> sort */
10676 o->op_private |= OPpSORT_REVERSE;
10677 op_null(reverse);
10678 pushmark->op_next = oright->op_next;
10679 op_null(oright);
10680 }
10681 }
10682 }
10683
10684 break;
10685 }
10686
10687 case OP_REVERSE: {
10688 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
10689 OP *gvop = NULL;
10690 LISTOP *enter, *exlist;
10691
10692 if (o->op_private & OPpSORT_INPLACE)
10693 break;
10694
10695 enter = (LISTOP *) o->op_next;
10696 if (!enter)
10697 break;
10698 if (enter->op_type == OP_NULL) {
10699 enter = (LISTOP *) enter->op_next;
10700 if (!enter)
10701 break;
10702 }
10703 /* for $a (...) will have OP_GV then OP_RV2GV here.
10704 for (...) just has an OP_GV. */
10705 if (enter->op_type == OP_GV) {
10706 gvop = (OP *) enter;
10707 enter = (LISTOP *) enter->op_next;
10708 if (!enter)
10709 break;
10710 if (enter->op_type == OP_RV2GV) {
10711 enter = (LISTOP *) enter->op_next;
10712 if (!enter)
10713 break;
10714 }
10715 }
10716
10717 if (enter->op_type != OP_ENTERITER)
10718 break;
10719
10720 iter = enter->op_next;
10721 if (!iter || iter->op_type != OP_ITER)
10722 break;
10723
10724 expushmark = enter->op_first;
10725 if (!expushmark || expushmark->op_type != OP_NULL
10726 || expushmark->op_targ != OP_PUSHMARK)
10727 break;
10728
10729 exlist = (LISTOP *) expushmark->op_sibling;
10730 if (!exlist || exlist->op_type != OP_NULL
10731 || exlist->op_targ != OP_LIST)
10732 break;
10733
10734 if (exlist->op_last != o) {
10735 /* Mmm. Was expecting to point back to this op. */
10736 break;
10737 }
10738 theirmark = exlist->op_first;
10739 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
10740 break;
10741
10742 if (theirmark->op_sibling != o) {
10743 /* There's something between the mark and the reverse, eg
10744 for (1, reverse (...))
10745 so no go. */
10746 break;
10747 }
10748
10749 ourmark = ((LISTOP *)o)->op_first;
10750 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
10751 break;
10752
10753 ourlast = ((LISTOP *)o)->op_last;
10754 if (!ourlast || ourlast->op_next != o)
10755 break;
10756
10757 rv2av = ourmark->op_sibling;
10758 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
10759 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
10760 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
10761 /* We're just reversing a single array. */
10762 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
10763 enter->op_flags |= OPf_STACKED;
10764 }
10765
10766 /* We don't have control over who points to theirmark, so sacrifice
10767 ours. */
10768 theirmark->op_next = ourmark->op_next;
10769 theirmark->op_flags = ourmark->op_flags;
10770 ourlast->op_next = gvop ? gvop : (OP *) enter;
10771 op_null(ourmark);
10772 op_null(o);
10773 enter->op_private |= OPpITER_REVERSED;
10774 iter->op_private |= OPpITER_REVERSED;
10775
10776 break;
10777 }
10778
10779 case OP_QR:
10780 case OP_MATCH:
10781 if (!(cPMOP->op_pmflags & PMf_ONCE)) {
10782 assert (!cPMOP->op_pmstashstartu.op_pmreplstart);
10783 }
10784 break;
10785
10786 case OP_RUNCV:
10787 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv)) {
10788 SV *sv;
10789 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef;
10790 else {
10791 sv = newRV((SV *)PL_compcv);
10792 sv_rvweaken(sv);
10793 SvREADONLY_on(sv);
10794 }
10795 o->op_type = OP_CONST;
10796 o->op_ppaddr = PL_ppaddr[OP_CONST];
10797 o->op_flags |= OPf_SPECIAL;
10798 cSVOPo->op_sv = sv;
10799 }
10800 break;
10801
10802 case OP_SASSIGN:
10803 if (OP_GIMME(o,0) == G_VOID) {
10804 OP *right = cBINOP->op_first;
10805 if (right) {
10806 OP *left = right->op_sibling;
10807 if (left->op_type == OP_SUBSTR
10808 && (left->op_private & 7) < 4) {
10809 op_null(o);
10810 cBINOP->op_first = left;
10811 right->op_sibling =
10812 cBINOPx(left)->op_first->op_sibling;
10813 cBINOPx(left)->op_first->op_sibling = right;
10814 left->op_private |= OPpSUBSTR_REPL_FIRST;
10815 left->op_flags =
10816 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
10817 }
10818 }
10819 }
10820 break;
10821
10822 case OP_CUSTOM: {
10823 Perl_cpeep_t cpeep =
10824 XopENTRY(Perl_custom_op_xop(aTHX_ o), xop_peep);
10825 if (cpeep)
10826 cpeep(aTHX_ o, oldop);
10827 break;
10828 }
10829
10830 }
10831 oldop = o;
10832 }
10833 LEAVE;
10834}
10835
10836void
10837Perl_peep(pTHX_ register OP *o)
10838{
10839 CALL_RPEEP(o);
10840}
10841
10842/*
10843=head1 Custom Operators
10844
10845=for apidoc Ao||custom_op_xop
10846Return the XOP structure for a given custom op. This function should be
10847considered internal to OP_NAME and the other access macros: use them instead.
10848
10849=cut
10850*/
10851
10852const XOP *
10853Perl_custom_op_xop(pTHX_ const OP *o)
10854{
10855 SV *keysv;
10856 HE *he = NULL;
10857 XOP *xop;
10858
10859 static const XOP xop_null = { 0, 0, 0, 0, 0 };
10860
10861 PERL_ARGS_ASSERT_CUSTOM_OP_XOP;
10862 assert(o->op_type == OP_CUSTOM);
10863
10864 /* This is wrong. It assumes a function pointer can be cast to IV,
10865 * which isn't guaranteed, but this is what the old custom OP code
10866 * did. In principle it should be safer to Copy the bytes of the
10867 * pointer into a PV: since the new interface is hidden behind
10868 * functions, this can be changed later if necessary. */
10869 /* Change custom_op_xop if this ever happens */
10870 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
10871
10872 if (PL_custom_ops)
10873 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
10874
10875 /* assume noone will have just registered a desc */
10876 if (!he && PL_custom_op_names &&
10877 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
10878 ) {
10879 const char *pv;
10880 STRLEN l;
10881
10882 /* XXX does all this need to be shared mem? */
10883 Newxz(xop, 1, XOP);
10884 pv = SvPV(HeVAL(he), l);
10885 XopENTRY_set(xop, xop_name, savepvn(pv, l));
10886 if (PL_custom_op_descs &&
10887 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
10888 ) {
10889 pv = SvPV(HeVAL(he), l);
10890 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
10891 }
10892 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
10893 return xop;
10894 }
10895
10896 if (!he) return &xop_null;
10897
10898 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
10899 return xop;
10900}
10901
10902/*
10903=for apidoc Ao||custom_op_register
10904Register a custom op. See L<perlguts/"Custom Operators">.
10905
10906=cut
10907*/
10908
10909void
10910Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
10911{
10912 SV *keysv;
10913
10914 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
10915
10916 /* see the comment in custom_op_xop */
10917 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
10918
10919 if (!PL_custom_ops)
10920 PL_custom_ops = newHV();
10921
10922 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
10923 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
10924}
10925
10926/*
10927=head1 Functions in file op.c
10928
10929=for apidoc core_prototype
10930This function assigns the prototype of the named core function to C<sv>, or
10931to a new mortal SV if C<sv> is NULL. It returns the modified C<sv>, or
10932NULL if the core function has no prototype. C<code> is a code as returned
10933by C<keyword()>. It must not be equal to 0 or -KEY_CORE.
10934
10935=cut
10936*/
10937
10938SV *
10939Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
10940 int * const opnum)
10941{
10942 int i = 0, n = 0, seen_question = 0, defgv = 0;
10943 I32 oa;
10944#define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
10945 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
10946 bool nullret = FALSE;
10947
10948 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
10949
10950 assert (code && code != -KEY_CORE);
10951
10952 if (!sv) sv = sv_newmortal();
10953
10954#define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
10955
10956 switch (code < 0 ? -code : code) {
10957 case KEY_and : case KEY_chop: case KEY_chomp:
10958 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
10959 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
10960 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
10961 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
10962 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
10963 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
10964 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
10965 case KEY_x : case KEY_xor :
10966 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
10967 case KEY_glob: retsetpvs("_;", OP_GLOB);
10968 case KEY_keys: retsetpvs("+", OP_KEYS);
10969 case KEY_values: retsetpvs("+", OP_VALUES);
10970 case KEY_each: retsetpvs("+", OP_EACH);
10971 case KEY_push: retsetpvs("+@", OP_PUSH);
10972 case KEY_unshift: retsetpvs("+@", OP_UNSHIFT);
10973 case KEY_pop: retsetpvs(";+", OP_POP);
10974 case KEY_shift: retsetpvs(";+", OP_SHIFT);
10975 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
10976 case KEY_splice:
10977 retsetpvs("+;$$@", OP_SPLICE);
10978 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
10979 retsetpvs("", 0);
10980 case KEY_evalbytes:
10981 name = "entereval"; break;
10982 case KEY_readpipe:
10983 name = "backtick";
10984 }
10985
10986#undef retsetpvs
10987
10988 findopnum:
10989 while (i < MAXO) { /* The slow way. */
10990 if (strEQ(name, PL_op_name[i])
10991 || strEQ(name, PL_op_desc[i]))
10992 {
10993 if (nullret) { assert(opnum); *opnum = i; return NULL; }
10994 goto found;
10995 }
10996 i++;
10997 }
10998 return NULL;
10999 found:
11000 defgv = PL_opargs[i] & OA_DEFGV;
11001 oa = PL_opargs[i] >> OASHIFT;
11002 while (oa) {
11003 if (oa & OA_OPTIONAL && !seen_question && (
11004 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
11005 )) {
11006 seen_question = 1;
11007 str[n++] = ';';
11008 }
11009 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
11010 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
11011 /* But globs are already references (kinda) */
11012 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
11013 ) {
11014 str[n++] = '\\';
11015 }
11016 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
11017 && !scalar_mod_type(NULL, i)) {
11018 str[n++] = '[';
11019 str[n++] = '$';
11020 str[n++] = '@';
11021 str[n++] = '%';
11022 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
11023 str[n++] = '*';
11024 str[n++] = ']';
11025 }
11026 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
11027 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
11028 str[n-1] = '_'; defgv = 0;
11029 }
11030 oa = oa >> 4;
11031 }
11032 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
11033 str[n++] = '\0';
11034 sv_setpvn(sv, str, n - 1);
11035 if (opnum) *opnum = i;
11036 return sv;
11037}
11038
11039OP *
11040Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
11041 const int opnum)
11042{
11043 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv);
11044 OP *o;
11045
11046 PERL_ARGS_ASSERT_CORESUB_OP;
11047
11048 switch(opnum) {
11049 case 0:
11050 return op_append_elem(OP_LINESEQ,
11051 argop,
11052 newSLICEOP(0,
11053 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
11054 newOP(OP_CALLER,0)
11055 )
11056 );
11057 case OP_SELECT: /* which represents OP_SSELECT as well */
11058 if (code)
11059 return newCONDOP(
11060 0,
11061 newBINOP(OP_GT, 0,
11062 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
11063 newSVOP(OP_CONST, 0, newSVuv(1))
11064 ),
11065 coresub_op(newSVuv((UV)OP_SSELECT), 0,
11066 OP_SSELECT),
11067 coresub_op(coreargssv, 0, OP_SELECT)
11068 );
11069 /* FALL THROUGH */
11070 default:
11071 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
11072 case OA_BASEOP:
11073 return op_append_elem(
11074 OP_LINESEQ, argop,
11075 newOP(opnum,
11076 opnum == OP_WANTARRAY || opnum == OP_RUNCV
11077 ? OPpOFFBYONE << 8 : 0)
11078 );
11079 case OA_BASEOP_OR_UNOP:
11080 if (opnum == OP_ENTEREVAL) {
11081 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
11082 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
11083 }
11084 else o = newUNOP(opnum,0,argop);
11085 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
11086 else {
11087 onearg:
11088 if (is_handle_constructor(o, 1))
11089 argop->op_private |= OPpCOREARGS_DEREF1;
11090 if (scalar_mod_type(NULL, opnum))
11091 argop->op_private |= OPpCOREARGS_SCALARMOD;
11092 }
11093 return o;
11094 default:
11095 o = convert(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
11096 if (is_handle_constructor(o, 2))
11097 argop->op_private |= OPpCOREARGS_DEREF2;
11098 if (opnum == OP_SUBSTR) {
11099 o->op_private |= OPpMAYBE_LVSUB;
11100 return o;
11101 }
11102 else goto onearg;
11103 }
11104 }
11105}
11106
11107void
11108Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
11109 SV * const *new_const_svp)
11110{
11111 const char *hvname;
11112 bool is_const = !!CvCONST(old_cv);
11113 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
11114
11115 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
11116
11117 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
11118 return;
11119 /* They are 2 constant subroutines generated from
11120 the same constant. This probably means that
11121 they are really the "same" proxy subroutine
11122 instantiated in 2 places. Most likely this is
11123 when a constant is exported twice. Don't warn.
11124 */
11125 if (
11126 (ckWARN(WARN_REDEFINE)
11127 && !(
11128 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
11129 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
11130 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
11131 strEQ(hvname, "autouse"))
11132 )
11133 )
11134 || (is_const
11135 && ckWARN_d(WARN_REDEFINE)
11136 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
11137 )
11138 )
11139 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
11140 is_const
11141 ? "Constant subroutine %"SVf" redefined"
11142 : "Subroutine %"SVf" redefined",
11143 name);
11144}
11145
11146/*
11147=head1 Hook manipulation
11148
11149These functions provide convenient and thread-safe means of manipulating
11150hook variables.
11151
11152=cut
11153*/
11154
11155/*
11156=for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p
11157
11158Puts a C function into the chain of check functions for a specified op
11159type. This is the preferred way to manipulate the L</PL_check> array.
11160I<opcode> specifies which type of op is to be affected. I<new_checker>
11161is a pointer to the C function that is to be added to that opcode's
11162check chain, and I<old_checker_p> points to the storage location where a
11163pointer to the next function in the chain will be stored. The value of
11164I<new_pointer> is written into the L</PL_check> array, while the value
11165previously stored there is written to I<*old_checker_p>.
11166
11167L</PL_check> is global to an entire process, and a module wishing to
11168hook op checking may find itself invoked more than once per process,
11169typically in different threads. To handle that situation, this function
11170is idempotent. The location I<*old_checker_p> must initially (once
11171per process) contain a null pointer. A C variable of static duration
11172(declared at file scope, typically also marked C<static> to give
11173it internal linkage) will be implicitly initialised appropriately,
11174if it does not have an explicit initialiser. This function will only
11175actually modify the check chain if it finds I<*old_checker_p> to be null.
11176This function is also thread safe on the small scale. It uses appropriate
11177locking to avoid race conditions in accessing L</PL_check>.
11178
11179When this function is called, the function referenced by I<new_checker>
11180must be ready to be called, except for I<*old_checker_p> being unfilled.
11181In a threading situation, I<new_checker> may be called immediately,
11182even before this function has returned. I<*old_checker_p> will always
11183be appropriately set before I<new_checker> is called. If I<new_checker>
11184decides not to do anything special with an op that it is given (which
11185is the usual case for most uses of op check hooking), it must chain the
11186check function referenced by I<*old_checker_p>.
11187
11188If you want to influence compilation of calls to a specific subroutine,
11189then use L</cv_set_call_checker> rather than hooking checking of all
11190C<entersub> ops.
11191
11192=cut
11193*/
11194
11195void
11196Perl_wrap_op_checker(pTHX_ Optype opcode,
11197 Perl_check_t new_checker, Perl_check_t *old_checker_p)
11198{
11199 dVAR;
11200
11201 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
11202 if (*old_checker_p) return;
11203 OP_CHECK_MUTEX_LOCK;
11204 if (!*old_checker_p) {
11205 *old_checker_p = PL_check[opcode];
11206 PL_check[opcode] = new_checker;
11207 }
11208 OP_CHECK_MUTEX_UNLOCK;
11209}
11210
11211#include "XSUB.h"
11212
11213/* Efficient sub that returns a constant scalar value. */
11214static void
11215const_sv_xsub(pTHX_ CV* cv)
11216{
11217 dVAR;
11218 dXSARGS;
11219 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
11220 if (items != 0) {
11221 NOOP;
11222#if 0
11223 /* diag_listed_as: SKIPME */
11224 Perl_croak(aTHX_ "usage: %s::%s()",
11225 HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
11226#endif
11227 }
11228 if (!sv) {
11229 XSRETURN(0);
11230 }
11231 EXTEND(sp, 1);
11232 ST(0) = sv;
11233 XSRETURN(1);
11234}
11235
11236/*
11237 * Local variables:
11238 * c-indentation-style: bsd
11239 * c-basic-offset: 4
11240 * indent-tabs-mode: nil
11241 * End:
11242 *
11243 * ex: set ts=8 sts=4 sw=4 et:
11244 */